On output hooks (LOCAL_OUT, POST_ROUTING and their per-family equivalents) the mac header is either unset or still belongs to the ingress device, not the egress one, so we would log stale data. Only emit the hardware header attributes while the packet is still on the input path. The check has to use both family and hook value, hook numbers are family specific, e.g. NF_BR_BROUTING has the same value as NF_INET_INGRESS. nfulnl_pre_egress(pf, hooknum) answers the question the check cares about: has the packet left indev's input path yet, i.e. does skb's mac header still belong to indev. This keeps hwheader logging working for the nftables inet ingress hook (nft_do_chain_inet_ingress() sets pf to NFPROTO_IPV4/IPV6 with hook = NF_INET_INGRESS) and for the ebtables broute table (NF_BR_BROUTING, indev and mac header are those of the received frame). For NFPROTO_ARP and NFPROTO_NETDEV only NF_ARP_IN and NF_NETDEV_INGRESS are accepted. Fixes: 72961ecf84d6 ("netfilter: nfnetlink_log: send complete hardware header") Signed-off-by: Florian Westphal --- net/netfilter/nfnetlink_log.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c index edc1e018c869..ee006d9df0a4 100644 --- a/net/netfilter/nfnetlink_log.c +++ b/net/netfilter/nfnetlink_log.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -458,6 +459,36 @@ static int nfulnl_put_bridge(struct nfulnl_instance *inst, const struct sk_buff return -1; } +/** + * nfulnl_pre_egress - Can @skb's mac header still be attributed to @indev? + * @pf: protocol family of the hook this packet is being logged from. + * @hooknum: hook number within @pf's family. + * + * True only for hook points where the packet has not yet left @indev's + * input path, i.e. where @skb's mac header still reflects what @indev + * actually received. Past LOCAL_OUT/POST_ROUTING (and their per-family + * equivalents) the packet may have been re-routed, re-encapsulated, or + * is headed to a different device than it arrived on. + * + * Return: true if mac header can be attributed to indev + */ +static bool nfulnl_pre_egress(u8 pf, unsigned int hooknum) +{ + switch (pf) { + case NFPROTO_IPV4: + case NFPROTO_IPV6: + return hooknum <= NF_INET_FORWARD || hooknum == NF_INET_INGRESS; + case NFPROTO_BRIDGE: + return hooknum <= NF_BR_FORWARD || hooknum == NF_BR_BROUTING; + case NFPROTO_ARP: + return hooknum == NF_ARP_IN; + case NFPROTO_NETDEV: + return hooknum == NF_NETDEV_INGRESS; + default: + return false; + } +} + /** * nfulnl_put_hwheader - Append link-layer header info to the netlink skb. * @nlskb: Target netlink message to append attributes to. @@ -650,7 +681,8 @@ __build_packet_message(struct nfnl_log_net *log, if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR)) goto nla_put_failure; - if (indev && skb_mac_header_was_set(skb) && + if (nfulnl_pre_egress(pf, hooknum) && + indev && skb_mac_header_was_set(skb) && nfulnl_put_hwheader(inst->skb, skb, indev)) goto nla_put_failure; -- 2.56.1