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. Same for the HWADDR attribute: it is parsed with the ingress device's header_ops, but ip_output() and xfrm4_output() pass the pre-switch skb->dev as indev while the packet may have been re-routed or re-encapsulated, so the source address can be stale. 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 hwaddr/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. nfnetlink_queue uses the hook number only; its only supported in the ipv4/ipv6/inet/bridge families. Fixes: 72961ecf84d6 ("netfilter: nfnetlink_log: send complete hardware header") Signed-off-by: Florian Westphal --- net/netfilter/nfnetlink_log.c | 43 +++++++++++++++++++++++++++++---- net/netfilter/nfnetlink_queue.c | 3 ++- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c index b54fbfe56795..5645297b4b3b 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. @@ -647,12 +678,14 @@ __build_packet_message(struct nfnl_log_net *log, nla_put_be32(inst->skb, NFULA_MARK, htonl(skb->mark))) goto nla_put_failure; - if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR)) - goto nla_put_failure; + if (nfulnl_pre_egress(pf, hooknum)) { + if (nfnetlink_put_hwaddr(inst->skb, skb, indev, NFULA_HWADDR)) + goto nla_put_failure; - if (indev && skb_mac_header_was_set(skb) && - nfulnl_put_hwheader(inst->skb, skb, indev)) - goto nla_put_failure; + if (indev && skb_mac_header_was_set(skb) && + nfulnl_put_hwheader(inst->skb, skb, indev)) + goto nla_put_failure; + } if (hooknum <= NF_INET_FORWARD) { struct timespec64 kts = ktime_to_timespec64(skb_tstamp_cond(skb, true)); diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c index 37f8953b7c49..c1afeb0c3e2a 100644 --- a/net/netfilter/nfnetlink_queue.c +++ b/net/netfilter/nfnetlink_queue.c @@ -919,7 +919,8 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue, nla_put_be32(skb, NFQA_PRIORITY, htonl(entskb->priority))) goto nla_put_failure; - if (nfnetlink_put_hwaddr(skb, entskb, indev, NFQA_HWADDR)) + if (entry->state.hook <= NF_INET_FORWARD && + nfnetlink_put_hwaddr(skb, entskb, indev, NFQA_HWADDR)) goto nla_put_failure; if (nfqnl_put_bridge(entry, skb) < 0) -- 2.56.1