1 // SPDX-License-Identifier: GPL-2.0 2 #include <inttypes.h> 3 #include <linux/bpf.h> 4 #include <bpf/bpf_endian.h> 5 #include <bpf/bpf_helpers.h> 6 #include <linux/if_ether.h> 7 #include <linux/ip.h> 8 9 /* This function extracts the last byte of the daddr, and uses it 10 * as output dev index. 11 */ 12 SEC("lwt_xmit") test_lwt_reroute(struct __sk_buff * skb)13int test_lwt_reroute(struct __sk_buff *skb) 14 { 15 struct iphdr *iph = NULL; 16 void *start = (void *)(long)skb->data; 17 void *end = (void *)(long)skb->data_end; 18 19 /* set mark at most once */ 20 if (skb->mark != 0) 21 return BPF_OK; 22 23 if (start + sizeof(*iph) > end) 24 return BPF_DROP; 25 26 iph = (struct iphdr *)start; 27 skb->mark = bpf_ntohl(iph->daddr) & 0xff; 28 29 /* do not reroute x.x.x.0 packets */ 30 if (skb->mark == 0) 31 return BPF_OK; 32 33 return BPF_LWT_REROUTE; 34 } 35 36 char _license[] SEC("license") = "GPL"; 37