diff --git a/main.c b/main.c index fde4c94..b493fa0 100644 --- a/main.c +++ b/main.c @@ -8,18 +8,28 @@ #include #include #include +#include #include +#include -#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF) +#ifndef AF_INET +#define AF_INET 2 +#endif + +#ifndef AF_INET6 +#define AF_INET6 10 +#endif + +#define IPV6_FLOWINFO_MASK bpf_htonl(0x0FFFFFFF) /* Forwarding ports */ -struct bpf_map_def SEC("maps") xdp_l3fwd_ports = { - .type = BPF_MAP_TYPE_DEVMAP, - .key_size = sizeof(int), - .value_size = sizeof(int), - .max_entries = 512, -}; +struct { + __uint(type, BPF_MAP_TYPE_DEVMAP); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); + __uint(max_entries, 512); +} xdp_l3fwd_ports SEC(".maps"); /* Stats key — identifies a connection or flow */ struct flow_key { @@ -44,19 +54,19 @@ struct flow_stats { }; /* Hash map to track stats per flow */ -struct bpf_map_def SEC("maps") xdp_flow_stats = { - .type = BPF_MAP_TYPE_HASH, - .key_size = sizeof(struct flow_key), - .value_size = sizeof(struct flow_stats), - .max_entries = 65536, -}; +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(key_size, sizeof(struct flow_key)); + __uint(value_size, sizeof(struct flow_stats)); + __uint(max_entries, 65536); +} xdp_flow_stats SEC(".maps"); /* Decrease IPv4 TTL helper */ static __always_inline int ip_decrease_ttl(struct iphdr *iph) { - u32 check = (__force u32)iph->check; - check += (__force u32)htons(0x0100); - iph->check = (__force __sum16)(check + (check >= 0xFFFF)); + __u32 check = (__u32)iph->check; + check += (__u32)bpf_htons(0x0100); + iph->check = (__sum16)(check + (check >= 0xFFFF)); return --iph->ttl; } @@ -78,7 +88,7 @@ static __always_inline void record_stats(struct xdp_md *ctx, struct flow_key *ke } } -static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, u32 flags) +static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, __u32 flags) { void *data_end = (void *)(long)ctx->data_end; void *data = (void *)(long)ctx->data; @@ -86,8 +96,8 @@ static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, u32 flags) struct ethhdr *eth = data; struct ipv6hdr *ip6h; struct iphdr *iph; - u16 h_proto; - u64 nh_off; + __u16 h_proto; + __u64 nh_off; int rc; nh_off = sizeof(*eth); @@ -100,7 +110,7 @@ static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, u32 flags) struct flow_key key = {}; __u64 bytes = data_end - data; - if (h_proto == htons(ETH_P_IP)) { + if (h_proto == bpf_htons(ETH_P_IP)) { iph = data + nh_off; if ((void *)(iph + 1) > data_end) return XDP_DROP; @@ -131,11 +141,11 @@ static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, u32 flags) fib_params.family = AF_INET; fib_params.tos = iph->tos; fib_params.l4_protocol = iph->protocol; - fib_params.tot_len = ntohs(iph->tot_len); + fib_params.tot_len = bpf_ntohs(iph->tot_len); fib_params.ipv4_src = iph->saddr; fib_params.ipv4_dst = iph->daddr; - } else if (h_proto == htons(ETH_P_IPV6)) { + } else if (h_proto == bpf_htons(ETH_P_IPV6)) { ip6h = data + nh_off; if ((void *)(ip6h + 1) > data_end) return XDP_DROP; @@ -166,7 +176,7 @@ static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, u32 flags) fib_params.family = AF_INET6; fib_params.flowinfo = *(__be32 *)ip6h & IPV6_FLOWINFO_MASK; fib_params.l4_protocol = ip6h->nexthdr; - fib_params.tot_len = ntohs(ip6h->payload_len); + fib_params.tot_len = bpf_ntohs(ip6h->payload_len); __builtin_memcpy(fib_params.ipv6_src, &ip6h->saddr, 16); __builtin_memcpy(fib_params.ipv6_dst, &ip6h->daddr, 16); } else { @@ -179,9 +189,9 @@ static __always_inline int xdp_l3fwd_flags(struct xdp_md *ctx, u32 flags) if (!bpf_map_lookup_elem(&xdp_l3fwd_ports, &fib_params.ifindex)) return XDP_PASS; - if (h_proto == htons(ETH_P_IP)) + if (h_proto == bpf_htons(ETH_P_IP)) ip_decrease_ttl(iph); - else if (h_proto == htons(ETH_P_IPV6)) + else if (h_proto == bpf_htons(ETH_P_IPV6)) ip6h->hop_limit--; __builtin_memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);