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:
Toke Høiland-Jørgensen
2020-11-25 11:31:06 +01:00
parent f0fce8f62b
commit 583f7a213f
3 changed files with 9 additions and 6 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);