0001
0002
0003 #include <stddef.h>
0004 #include <string.h>
0005 #include <linux/bpf.h>
0006 #include <linux/if_ether.h>
0007 #include <linux/ip.h>
0008 #include <linux/pkt_cls.h>
0009 #include <bpf/bpf_helpers.h>
0010
0011 struct {
0012 __uint(type, MAP_TYPE);
0013 __uint(max_entries, 32);
0014 __uint(map_flags, 0);
0015 __uint(key_size, 0);
0016 __uint(value_size, sizeof(__u32));
0017 } map_in SEC(".maps");
0018
0019 struct {
0020 __uint(type, MAP_TYPE);
0021 __uint(max_entries, 32);
0022 __uint(map_flags, 0);
0023 __uint(key_size, 0);
0024 __uint(value_size, sizeof(__u32));
0025 } map_out SEC(".maps");
0026
0027 SEC("tc")
0028 int _test(struct __sk_buff *skb)
0029 {
0030 void *data_end = (void *)(long)skb->data_end;
0031 void *data = (void *)(long)skb->data;
0032 struct ethhdr *eth = (struct ethhdr *)(data);
0033 __u32 value;
0034 int err;
0035
0036 if (eth + 1 > data_end)
0037 return TC_ACT_SHOT;
0038
0039 struct iphdr *iph = (struct iphdr *)(eth + 1);
0040
0041 if (iph + 1 > data_end)
0042 return TC_ACT_SHOT;
0043
0044 err = bpf_map_pop_elem(&map_in, &value);
0045 if (err)
0046 return TC_ACT_SHOT;
0047
0048 iph->daddr = value;
0049
0050 err = bpf_map_push_elem(&map_out, &iph->saddr, 0);
0051 if (err)
0052 return TC_ACT_SHOT;
0053
0054 return TC_ACT_OK;
0055 }
0056
0057 char _license[] SEC("license") = "GPL";