Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net, xdp: Update pkt_type if generic XDP changes unicast MAC

If a generic XDP program changes the destination MAC address from/to
multicast/broadcast, the skb->pkt_type is updated to properly handle
the packet when passed up the stack. When changing the MAC from/to
the NICs MAC, PACKET_HOST/OTHERHOST is not updated, though, making
the behavior different from that of native XDP.

Remember the PACKET_HOST/OTHERHOST state before calling the program
in generic XDP, and update pkt_type accordingly if the destination
MAC address has changed. As eth_type_trans() assumes a default
pkt_type of PACKET_HOST, restore that before calling it.

The use case for this is when a XDP program wants to push received
packets up the stack by rewriting the MAC to the NICs MAC, for
example by cluster nodes sharing MAC addresses.

Fixes: 297249569932 ("net: fix generic XDP to handle if eth header was mangled")
Signed-off-by: Martin Willi <martin@strongswan.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210419141559.8611-1-martin@strongswan.org

authored by

Martin Willi and committed by
Daniel Borkmann
22b60343 d044d9fc

+5 -1
+5 -1
net/core/dev.c
··· 4723 4723 void *orig_data, *orig_data_end, *hard_start; 4724 4724 struct netdev_rx_queue *rxqueue; 4725 4725 u32 metalen, act = XDP_DROP; 4726 + bool orig_bcast, orig_host; 4726 4727 u32 mac_len, frame_sz; 4727 4728 __be16 orig_eth_type; 4728 4729 struct ethhdr *eth; 4729 - bool orig_bcast; 4730 4730 int off; 4731 4731 4732 4732 /* Reinjected packets coming from act_mirred or similar should ··· 4773 4773 orig_data_end = xdp->data_end; 4774 4774 orig_data = xdp->data; 4775 4775 eth = (struct ethhdr *)xdp->data; 4776 + orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr); 4776 4777 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); 4777 4778 orig_eth_type = eth->h_proto; 4778 4779 ··· 4801 4800 /* check if XDP changed eth hdr such SKB needs update */ 4802 4801 eth = (struct ethhdr *)xdp->data; 4803 4802 if ((orig_eth_type != eth->h_proto) || 4803 + (orig_host != ether_addr_equal_64bits(eth->h_dest, 4804 + skb->dev->dev_addr)) || 4804 4805 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { 4805 4806 __skb_push(skb, ETH_HLEN); 4807 + skb->pkt_type = PACKET_HOST; 4806 4808 skb->protocol = eth_type_trans(skb, skb->dev); 4807 4809 } 4808 4810