1 // SPDX-License-Identifier: GPL-2.0
3 #include <bpf/bpf_endian.h>
4 #include <bpf/bpf_helpers.h>
6 #include "bpf_tracing_net.h"
8 /* We don't care about whether the packet can be received by network stack.
9 * Just care if the packet is sent to the correct device at correct direction
10 * and not panic the kernel.
12 static int prepend_dummy_mac(struct __sk_buff *skb)
14 char mac[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0xf,
15 0xe, 0xd, 0xc, 0xb, 0xa, 0x08, 0x00};
17 if (bpf_skb_change_head(skb, ETH_HLEN, 0))
20 if (bpf_skb_store_bytes(skb, 0, mac, sizeof(mac), 0))
26 /* Use the last byte of IP address to redirect the packet */
27 static int get_redirect_target(struct __sk_buff *skb)
29 struct iphdr *iph = NULL;
30 void *start = (void *)(long)skb->data;
31 void *end = (void *)(long)skb->data_end;
33 if (start + sizeof(*iph) > end)
36 iph = (struct iphdr *)start;
37 return bpf_ntohl(iph->daddr) & 0xff;
41 int test_lwt_redirect_in(struct __sk_buff *skb)
43 int target = get_redirect_target(skb);
48 if (prepend_dummy_mac(skb))
51 return bpf_redirect(target, BPF_F_INGRESS);
55 int test_lwt_redirect_out(struct __sk_buff *skb)
57 int target = get_redirect_target(skb);
62 if (prepend_dummy_mac(skb))
65 return bpf_redirect(target, 0);
68 SEC("redir_egress_nomac")
69 int test_lwt_redirect_out_nomac(struct __sk_buff *skb)
71 int target = get_redirect_target(skb);
76 return bpf_redirect(target, 0);
79 SEC("redir_ingress_nomac")
80 int test_lwt_redirect_in_nomac(struct __sk_buff *skb)
82 int target = get_redirect_target(skb);
87 return bpf_redirect(target, BPF_F_INGRESS);
90 char _license[] SEC("license") = "GPL";