mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
encap-forward: Move setting of the ethertype to the encap_* functions
Makes sure the ethertype is set correctly depending on the type of encapsulation. Fixes #2. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
static void encap_ipv6(volatile void *data, volatile void *data_end)
|
||||
{
|
||||
volatile struct ipv6hdr *ip6h;
|
||||
volatile struct ethhdr *eth;
|
||||
size_t len;
|
||||
|
||||
struct ipv6hdr encap_hdr = {
|
||||
@@ -18,10 +19,12 @@ static void encap_ipv6(volatile void *data, volatile void *data_end)
|
||||
0x00, 0x00, 0x00, 0x01 } },
|
||||
};
|
||||
|
||||
ip6h = data + sizeof(struct ethhdr);
|
||||
eth = data;
|
||||
ip6h = (void *)(eth +1);
|
||||
if (ip6h + 1 > data_end)
|
||||
return;
|
||||
|
||||
eth->h_proto = bpf_htons(ETH_P_IPV6);
|
||||
*ip6h = encap_hdr;
|
||||
|
||||
len = (data_end - data);
|
||||
@@ -39,6 +42,7 @@ static __always_inline __u16 csum_fold_helper(__u32 csum)
|
||||
|
||||
static void encap_ipv4(volatile void *data, volatile void *data_end)
|
||||
{
|
||||
volatile struct ethhdr *eth;
|
||||
volatile struct iphdr *iph;
|
||||
size_t len;
|
||||
|
||||
@@ -51,10 +55,12 @@ static void encap_ipv4(volatile void *data, volatile void *data_end)
|
||||
.daddr = bpf_htonl(0x0a0b0201),
|
||||
};
|
||||
|
||||
iph = data + sizeof(struct ethhdr);
|
||||
eth = data;
|
||||
iph = (void *)(eth +1);
|
||||
if (iph + 1 > data_end)
|
||||
return;
|
||||
|
||||
eth->h_proto = bpf_htons(ETH_P_IP);
|
||||
*iph = encap_hdr;
|
||||
|
||||
len = (data_end - data);
|
||||
|
@@ -28,19 +28,17 @@ SEC("classifier") int tc_encap(struct __sk_buff *skb)
|
||||
|
||||
data = (void *)(long)skb->data;
|
||||
data_end = (void *)(long)skb->data_end;
|
||||
eth = (void *)data;
|
||||
|
||||
#ifdef IPV6
|
||||
encap_ipv6(data, data_end);
|
||||
#else
|
||||
encap_ipv4(data, data_end);
|
||||
|
||||
eth = (void *)data;
|
||||
iph = (void *)(eth +1);
|
||||
if (iph +1 > data_end)
|
||||
goto out;
|
||||
|
||||
eth->h_proto = bpf_htons(ETH_P_IP);
|
||||
|
||||
fib_params.family = AF_INET;
|
||||
fib_params.tos = iph->tos;
|
||||
fib_params.l4_protocol = iph->protocol;
|
||||
|
@@ -36,7 +36,6 @@ SEC("prog") int xdp_encap(struct xdp_md *ctx)
|
||||
if (ehdr + 1 > data_end)
|
||||
goto out;
|
||||
*ehdr = old_ehdr;
|
||||
ehdr->h_proto = bpf_htons(ETH_P_IP);
|
||||
|
||||
#ifdef IPV6
|
||||
encap_ipv6(data, data_end);
|
||||
|
Reference in New Issue
Block a user