main.c aktualisiert

This commit is contained in:
Tizian Maxime Weigt 2025-10-30 22:03:51 +00:00
parent af6147676b
commit 2c14a29229

618
main.c
View File

@ -25,479 +25,287 @@
#define VLAN_MAX_DEPTH 2 #define VLAN_MAX_DEPTH 2
#define IPV6_EXT_MAX_CHAIN 6 #define IPV6_EXT_MAX_CHAIN 6
struct vlan_hdr {
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
};
/* Auto-learned VLAN info */
struct vlan_learning_entry {
__u16 vlan_id;
__u16 confidence;
__u32 last_seen;
};
struct { struct {
__uint(type, BPF_MAP_TYPE_HASH); __uint(type, BPF_MAP_TYPE_DEVMAP);
__type(key, __u32); __uint(key_size, sizeof(int));
__type(value, struct vlan_learning_entry); __uint(value_size, sizeof(int));
__uint(max_entries, 512); __uint(max_entries, 512);
} xdp_vlan_learning SEC(".maps"); } xdp_l3fwd_ports SEC(".maps");
struct vlan_parent_info {
__u32 parent_ifindex;
__u16 vlan_id;
__u16 pad;
};
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u32);
__type(value, struct vlan_parent_info);
__uint(max_entries, 512);
} xdp_vlan_parents SEC(".maps");
struct flow_key { struct flow_key {
__u8 proto; __u8 proto;
__u8 pad[3]; __u8 pad[3];
__u16 vlan_id; __u16 vlan_id;
__u16 pad2; __u16 pad2;
union { union {
__u32 ipv4_src; __u32 ipv4_src;
__u8 ipv6_src[16]; __u8 ipv6_src[16];
}; };
union {
__u32 ipv4_dst;
__u8 ipv6_dst[16];
};
__u16 sport; union {
__u16 dport; __u32 ipv4_dst;
__u8 ipv6_dst[16];
};
__u16 sport;
__u16 dport;
}; };
struct flow_stats { struct flow_stats {
__u64 packets; __u64 packets;
__u64 bytes; __u64 bytes;
}; };
struct { struct {
__uint(type, BPF_MAP_TYPE_PERCPU_HASH); __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
__uint(key_size, sizeof(struct flow_key)); __uint(key_size, sizeof(struct flow_key));
__uint(value_size, sizeof(struct flow_stats)); __uint(value_size, sizeof(struct flow_stats));
__uint(max_entries, 65536); __uint(max_entries, 65536);
} xdp_flow_stats SEC(".maps"); } xdp_flow_stats SEC(".maps");
struct vlan_hdr {
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
};
static __always_inline int ip_decrease_ttl(struct iphdr *iph) static __always_inline int ip_decrease_ttl(struct iphdr *iph)
{ {
__u32 check = (__u32)iph->check; __u32 check = (__u32)iph->check;
check += (__u32)bpf_htons(0x0100); check += (__u32)bpf_htons(0x0100);
iph->check = (__sum16)(check + (check >= 0xFFFF)); iph->check = (__sum16)(check + (check >= 0xFFFF));
return --iph->ttl; return --iph->ttl;
} }
static __always_inline void record_stats(struct flow_key *key, __u64 bytes) static __always_inline void record_stats(struct xdp_md *ctx, struct flow_key *key, __u64 bytes)
{ {
struct flow_stats *stats = bpf_map_lookup_elem(&xdp_flow_stats, key); struct flow_stats *stats;
if (stats) {
stats->packets++;
stats->bytes += bytes;
} else {
struct flow_stats new_stats = {
.packets = 1,
.bytes = bytes,
};
bpf_map_update_elem(&xdp_flow_stats, key, &new_stats, BPF_ANY);
}
}
static __always_inline void learn_vlan(struct xdp_md *ctx, __u16 vlan_id) stats = bpf_map_lookup_elem(&xdp_flow_stats, key);
{ if (stats) {
__u32 ifindex = ctx->ingress_ifindex; stats->packets++;
struct vlan_learning_entry *entry = bpf_map_lookup_elem(&xdp_vlan_learning, &ifindex); stats->bytes += bytes;
} else {
if (entry) { struct flow_stats new_stats = {
if (vlan_id > 0) { .packets = 1,
if (entry->vlan_id == vlan_id) { .bytes = bytes,
if (entry->confidence < 65535) };
entry->confidence++; bpf_map_update_elem(&xdp_flow_stats, key, &new_stats, BPF_ANY);
} else if (entry->confidence > 0) { }
entry->confidence--;
if (entry->confidence == 0) {
entry->vlan_id = vlan_id;
entry->confidence = 1;
}
}
}
} else if (vlan_id > 0) {
struct vlan_learning_entry new_entry = {
.vlan_id = vlan_id,
.confidence = 1,
.last_seen = 0,
};
bpf_map_update_elem(&xdp_vlan_learning, &ifindex, &new_entry, BPF_ANY);
}
}
static __always_inline __u16 get_interface_vlan(struct xdp_md *ctx, __u32 ifindex)
{
struct vlan_parent_info *parent_info = bpf_map_lookup_elem(&xdp_vlan_parents, &ifindex);
if (parent_info && parent_info->vlan_id > 0) {
return parent_info->vlan_id;
}
struct vlan_learning_entry *learned = bpf_map_lookup_elem(&xdp_vlan_learning, &ifindex);
if (learned && learned->confidence > 5) {
return learned->vlan_id;
}
__u32 ingress_idx = ctx->ingress_ifindex;
if (ingress_idx != ifindex) {
struct vlan_learning_entry *ingress_learned = bpf_map_lookup_elem(&xdp_vlan_learning, &ingress_idx);
if (ingress_learned && ingress_learned->confidence > 10) {
struct vlan_learning_entry *egress_learned = bpf_map_lookup_elem(&xdp_vlan_learning, &ifindex);
if (!egress_learned || egress_learned->confidence < 3) {
return 0;
}
}
}
return 0;
} }
static __always_inline int parse_vlan(void *data, void *data_end, __u64 *nh_off, __u16 *h_proto, __u16 *vlan_id) static __always_inline int parse_vlan(void *data, void *data_end, __u64 *nh_off, __u16 *h_proto, __u16 *vlan_id)
{ {
struct vlan_hdr *vh; struct vlan_hdr *vhdr;
#pragma unroll int i, vlan_count = 0;
for (int i = 0; i < VLAN_MAX_DEPTH; i++) {
if (*h_proto != bpf_htons(ETH_P_8021Q) && *h_proto != bpf_htons(ETH_P_8021AD))
break;
vh = (void *)((char *)data + *nh_off); #pragma unroll
if ((void *)(vh + 1) > data_end) for (i = 0; i < VLAN_MAX_DEPTH; i++) {
return -1; if (*h_proto != bpf_htons(ETH_P_8021Q) && *h_proto != bpf_htons(ETH_P_8021AD))
break;
if (i == 0) vhdr = data + *nh_off;
*vlan_id = bpf_ntohs(vh->h_vlan_TCI) & 0x0FFF; if ((void *)(vhdr + 1) > data_end)
return -1;
*nh_off += sizeof(*vh); if (i == 0)
*h_proto = vh->h_vlan_encapsulated_proto; *vlan_id = bpf_ntohs(vhdr->h_vlan_TCI) & 0x0FFF;
}
return 0; *nh_off += sizeof(*vhdr);
*h_proto = vhdr->h_vlan_encapsulated_proto;
vlan_count++;
}
return vlan_count;
} }
static __always_inline int skip_ip6hdrext(void *data, void *data_end, __u64 *nh_off, __u8 next) static __always_inline int skip_ip6hdrext(void *data, void *data_end, __u64 *nh_off, __u8 next_hdr_type)
{ {
struct ipv6_opt_hdr *hdr; struct ipv6_opt_hdr {
#pragma unroll __u8 nexthdr;
for (int i = 0; i < IPV6_EXT_MAX_CHAIN; i++) { __u8 hdrlen;
hdr = (void *)((char *)data + *nh_off); } *hdr;
if ((void *)(hdr + 1) > data_end) int i;
return -1;
switch (next) { #pragma unroll
case IPPROTO_HOPOPTS: for (i = 0; i < IPV6_EXT_MAX_CHAIN; i++) {
case IPPROTO_DSTOPTS: hdr = data + *nh_off;
case IPPROTO_ROUTING:
case IPPROTO_MH: if ((void *)(hdr + 1) > data_end)
*nh_off += (hdr->hdrlen + 1) * 8; return -1;
next = hdr->nexthdr;
break;
case IPPROTO_AH:
*nh_off += (hdr->hdrlen + 2) * 4;
next = hdr->nexthdr;
break;
case IPPROTO_FRAGMENT:
*nh_off += 8;
next = hdr->nexthdr;
break;
default:
return next;
}
}
return -1;
}
/* Insert VLAN tag using head adjustment */ switch (next_hdr_type) {
static __always_inline int insert_vlan_tag(struct xdp_md *ctx, __u16 vlan_id) case IPPROTO_HOPOPTS:
{ case IPPROTO_DSTOPTS:
void *data_end = (void *)(long)ctx->data_end; case IPPROTO_ROUTING:
void *data = (void *)(long)ctx->data; case IPPROTO_MH:
*nh_off += (hdr->hdrlen + 1) * 8;
struct ethhdr *old_eth = data; next_hdr_type = hdr->nexthdr;
break;
if ((void *)(old_eth + 1) > data_end) case IPPROTO_AH:
return -1; *nh_off += (hdr->hdrlen + 2) * 4;
next_hdr_type = hdr->nexthdr;
struct ethhdr orig_eth; break;
__builtin_memcpy(&orig_eth, old_eth, sizeof(orig_eth)); case IPPROTO_FRAGMENT:
*nh_off += 8;
/* Expand headroom */ next_hdr_type = hdr->nexthdr;
if (bpf_xdp_adjust_head(ctx, -(int)sizeof(struct vlan_hdr))) break;
return -1; default:
return next_hdr_type;
/* Re-read pointers after head adjustment */ }
data = (void *)(long)ctx->data; }
data_end = (void *)(long)ctx->data_end;
struct ethhdr *new_eth = data;
struct vlan_hdr *vlan = (struct vlan_hdr *)(new_eth + 1);
if ((void *)(vlan + 1) > data_end)
return -1;
/* Copy ethernet header to new position */
__builtin_memcpy(new_eth->h_dest, orig_eth.h_dest, ETH_ALEN);
__builtin_memcpy(new_eth->h_source, orig_eth.h_source, ETH_ALEN);
/* Set up VLAN header */
vlan->h_vlan_TCI = bpf_htons(vlan_id & 0x0FFF);
vlan->h_vlan_encapsulated_proto = orig_eth.h_proto;
/* Update ethernet proto to VLAN */
new_eth->h_proto = bpf_htons(ETH_P_8021Q);
return 0;
}
/* Remove VLAN tag */ return -1;
static __always_inline int remove_vlan_tag(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
struct vlan_hdr *vlan = (struct vlan_hdr *)(eth + 1);
if ((void *)(vlan + 1) > data_end)
return -1;
__be16 encap_proto = vlan->h_vlan_encapsulated_proto;
struct ethhdr tmp_eth;
__builtin_memcpy(&tmp_eth, eth, sizeof(tmp_eth));
/* Adjust head to remove VLAN header */
if (bpf_xdp_adjust_head(ctx, (int)sizeof(struct vlan_hdr)))
return -1;
/* Re-read pointers after head adjustment */
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
eth = data;
if ((void *)(eth + 1) > data_end)
return -1;
__builtin_memcpy(eth->h_dest, tmp_eth.h_dest, ETH_ALEN);
__builtin_memcpy(eth->h_source, tmp_eth.h_source, ETH_ALEN);
eth->h_proto = encap_proto;
return 0;
} }
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_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data; void *data = (void *)(long)ctx->data;
struct bpf_fib_lookup fib_params;
struct ethhdr *eth = data;
struct ipv6hdr *ip6h;
struct iphdr *iph;
__u16 h_proto;
__u64 nh_off;
int rc, vlan_count;
__u16 vlan_id = 0;
struct ethhdr *eth = data; nh_off = sizeof(*eth);
__u64 nh_off = sizeof(*eth); if (data + nh_off > data_end)
if ((void *)((char *)data + nh_off) > data_end) return XDP_DROP;
return XDP_DROP;
struct bpf_fib_lookup fib_params = {}; __builtin_memset(&fib_params, 0, sizeof(fib_params));
__u16 h_proto = eth->h_proto; h_proto = eth->h_proto;
__u16 vlan_id = 0;
__u16 orig_vlan_id = 0;
int had_vlan = 0;
if (h_proto == bpf_htons(ETH_P_8021Q) || h_proto == bpf_htons(ETH_P_8021AD)) vlan_count = parse_vlan(data, data_end, &nh_off, &h_proto, &vlan_id);
had_vlan = 1; if (vlan_count < 0)
return XDP_DROP;
if (parse_vlan(data, data_end, &nh_off, &h_proto, &vlan_id) < 0) struct flow_key key = {};
return XDP_DROP; key.vlan_id = vlan_id;
__u64 bytes = data_end - data;
orig_vlan_id = vlan_id;
if (vlan_id > 0)
learn_vlan(ctx, vlan_id);
struct flow_key key = {}; if (h_proto == bpf_htons(ETH_P_IP)) {
key.vlan_id = vlan_id; iph = data + nh_off;
__u64 bytes = (char *)data_end - (char *)data; if ((void *)(iph + 1) > data_end)
return XDP_DROP;
if (h_proto == bpf_htons(ETH_P_IP)) { if (iph->ttl <= 1)
struct iphdr *iph = (void *)((char *)data + nh_off); return XDP_PASS;
if ((void *)(iph + 1) > data_end)
return XDP_DROP;
if (iph->ttl <= 1) key.proto = iph->protocol;
return XDP_PASS; key.ipv4_src = iph->saddr;
key.ipv4_dst = iph->daddr;
key.proto = iph->protocol; /* Calculate L4 offset - use pointer arithmetic from iph */
key.ipv4_src = iph->saddr; __u8 ihl = iph->ihl;
key.ipv4_dst = iph->daddr; if (ihl < 5)
return XDP_DROP;
__u8 ihl = iph->ihl; void *l4ptr = (void *)iph + (ihl * 4);
if (ihl < 5)
return XDP_DROP; if (iph->protocol == IPPROTO_TCP) {
struct tcphdr *tcph = l4ptr;
__u64 l4_off = nh_off + (ihl * 4); if ((void *)(tcph + 1) > data_end)
goto skip_v4_ports;
void *l4_hdr = (void *)((char *)data + l4_off); key.sport = tcph->source;
if ((void *)((char *)l4_hdr + 4) <= data_end) { key.dport = tcph->dest;
if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP) { } else if (iph->protocol == IPPROTO_UDP) {
__u16 *ports = l4_hdr; struct udphdr *udph = l4ptr;
key.sport = ports[0]; if ((void *)(udph + 1) > data_end)
key.dport = ports[1]; goto skip_v4_ports;
fib_params.sport = ports[0]; key.sport = udph->source;
fib_params.dport = ports[1]; key.dport = udph->dest;
} }
}
fib_params.family = AF_INET; skip_v4_ports:
fib_params.tos = iph->tos; fib_params.family = AF_INET;
fib_params.l4_protocol = iph->protocol; fib_params.tos = iph->tos;
fib_params.tot_len = bpf_ntohs(iph->tot_len); fib_params.l4_protocol = iph->protocol;
fib_params.ipv4_src = iph->saddr; fib_params.tot_len = bpf_ntohs(iph->tot_len);
fib_params.ipv4_dst = iph->daddr; fib_params.ipv4_src = iph->saddr;
fib_params.ipv4_dst = iph->daddr;
} else if (h_proto == bpf_htons(ETH_P_IPV6)) { } else if (h_proto == bpf_htons(ETH_P_IPV6)) {
struct ipv6hdr *ip6h = (void *)((char *)data + nh_off); ip6h = data + nh_off;
if ((void *)(ip6h + 1) > data_end) if ((void *)(ip6h + 1) > data_end)
return XDP_DROP; return XDP_DROP;
if (ip6h->hop_limit <= 1) if (ip6h->hop_limit <= 1)
return XDP_PASS; return XDP_PASS;
__builtin_memcpy(key.ipv6_src, &ip6h->saddr, 16); __builtin_memcpy(key.ipv6_src, &ip6h->saddr, 16);
__builtin_memcpy(key.ipv6_dst, &ip6h->daddr, 16); __builtin_memcpy(key.ipv6_dst, &ip6h->daddr, 16);
__u64 l4_off = nh_off + sizeof(*ip6h); __u64 l4_off = nh_off + sizeof(*ip6h);
int l4_proto = skip_ip6hdrext(data, data_end, &l4_off, ip6h->nexthdr); int l4_proto = skip_ip6hdrext(data, data_end, &l4_off, ip6h->nexthdr);
if (l4_proto < 0)
l4_proto = ip6h->nexthdr; if (l4_proto < 0)
l4_proto = ip6h->nexthdr;
key.proto = l4_proto; key.proto = l4_proto;
void *l4_hdr = (void *)((char *)data + l4_off); void *l4ptr = data + l4_off;
if ((void *)((char *)l4_hdr + 4) <= data_end) {
if (l4_proto == IPPROTO_TCP || l4_proto == IPPROTO_UDP) {
__u16 *ports = l4_hdr;
key.sport = ports[0];
key.dport = ports[1];
fib_params.sport = ports[0];
fib_params.dport = ports[1];
}
}
fib_params.family = AF_INET6; if (l4_proto == IPPROTO_TCP) {
__be32 flow = *(__be32 *)ip6h & IPV6_FLOWINFO_MASK; struct tcphdr *tcph = l4ptr;
fib_params.flowinfo = flow; if ((void *)(tcph + 1) > data_end)
fib_params.l4_protocol = l4_proto; goto skip_v6_ports;
fib_params.tot_len = bpf_ntohs(ip6h->payload_len); key.sport = tcph->source;
__builtin_memcpy(fib_params.ipv6_src, &ip6h->saddr, 16); key.dport = tcph->dest;
__builtin_memcpy(fib_params.ipv6_dst, &ip6h->daddr, 16); } else if (l4_proto == IPPROTO_UDP) {
} else { struct udphdr *udph = l4ptr;
return XDP_PASS; if ((void *)(udph + 1) > data_end)
} goto skip_v6_ports;
key.sport = udph->source;
key.dport = udph->dest;
}
fib_params.ifindex = ctx->ingress_ifindex; skip_v6_ports:
fib_params.family = AF_INET6;
fib_params.flowinfo = *(__be32 *)ip6h & IPV6_FLOWINFO_MASK;
fib_params.l4_protocol = l4_proto;
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 {
return XDP_PASS;
}
int rc = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags); fib_params.ifindex = ctx->ingress_ifindex;
if (rc == 0) { rc = bpf_fib_lookup(ctx, &fib_params, sizeof(fib_params), flags);
record_stats(&key, bytes);
if (rc == BPF_FIB_LKUP_RET_SUCCESS) {
if (!bpf_map_lookup_elem(&xdp_l3fwd_ports, &fib_params.ifindex))
return XDP_PASS;
__u16 egress_vlan = get_interface_vlan(ctx, fib_params.ifindex); record_stats(ctx, &key, bytes);
if (egress_vlan > 0 && !had_vlan) {
/* Need to add VLAN tag */
if (insert_vlan_tag(ctx, egress_vlan) < 0)
return XDP_DROP;
} else if (egress_vlan == 0 && had_vlan) {
/* Need to remove VLAN tag */
if (remove_vlan_tag(ctx) < 0) {
/* Keep VLAN if removal fails */
}
} else if (egress_vlan > 0 && had_vlan && egress_vlan != orig_vlan_id) {
/* Need to change VLAN ID - reload pointers first */
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_DROP;
if (eth->h_proto == bpf_htons(ETH_P_8021Q) ||
eth->h_proto == bpf_htons(ETH_P_8021AD)) {
struct vlan_hdr *vlan = (struct vlan_hdr *)(eth + 1);
if ((void *)(vlan + 1) > data_end)
return XDP_DROP;
vlan->h_vlan_TCI = bpf_htons(egress_vlan & 0x0FFF);
}
}
/* CRITICAL: Always reload pointers after FIB lookup to satisfy verifier */
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
eth = data;
/* Re-establish packet bounds for verifier */
if ((void *)(eth + 1) > data_end)
return XDP_DROP;
nh_off = sizeof(*eth);
/* Skip VLAN header if present */
if (eth->h_proto == bpf_htons(ETH_P_8021Q) ||
eth->h_proto == bpf_htons(ETH_P_8021AD)) {
nh_off += sizeof(struct vlan_hdr);
}
/* Verify nh_off is within bounds */
if ((void *)((char *)data + nh_off) > data_end)
return XDP_DROP;
/* Decrease TTL/hop_limit */
if (h_proto == bpf_htons(ETH_P_IP)) {
struct iphdr *iph = (void *)((char *)data + nh_off);
if ((void *)(iph + 1) > data_end)
return XDP_DROP;
ip_decrease_ttl(iph);
} else if (h_proto == bpf_htons(ETH_P_IPV6)) {
struct ipv6hdr *ip6h = (void *)((char *)data + nh_off);
if ((void *)(ip6h + 1) > data_end)
return XDP_DROP;
ip6h->hop_limit--;
}
/* Update MAC addresses - verify eth is still valid */ if (h_proto == bpf_htons(ETH_P_IP))
if ((void *)(eth + 1) > data_end) ip_decrease_ttl(iph);
return XDP_DROP; else if (h_proto == bpf_htons(ETH_P_IPV6))
ip6h->hop_limit--;
__builtin_memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
__builtin_memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
return bpf_redirect(fib_params.ifindex, 0); __builtin_memcpy(eth->h_dest, fib_params.dmac, ETH_ALEN);
} __builtin_memcpy(eth->h_source, fib_params.smac, ETH_ALEN);
return bpf_redirect_map(&xdp_l3fwd_ports, fib_params.ifindex, 0);
}
return XDP_PASS; return XDP_PASS;
} }
SEC("xdp") SEC("xdp")
int xdp_l3fwd_prog(struct xdp_md *ctx) int xdp_l3fwd_prog(struct xdp_md *ctx)
{ {
return xdp_l3fwd_flags(ctx, 0); return xdp_l3fwd_flags(ctx, 0);
} }
SEC("xdp") SEC("xdp")
int xdp_l3fwd_direct_prog(struct xdp_md *ctx) int xdp_l3fwd_direct_prog(struct xdp_md *ctx)
{ {
return xdp_l3fwd_flags(ctx, BPF_FIB_LOOKUP_DIRECT); return xdp_l3fwd_flags(ctx, BPF_FIB_LOOKUP_DIRECT);
} }
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";