53 lines
1.4 KiB
C
53 lines
1.4 KiB
C
#include <linux/bpf.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_endian.h>
|
|
#include <linux/if_ether.h>
|
|
#include "bridge_xdp.h"
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
|
|
__uint(max_entries, 1);
|
|
__type(key, __u32);
|
|
__type(value, struct packet_stats);
|
|
} stats_map SEC(".maps");
|
|
|
|
SEC("xdp")
|
|
int bridge_filter(struct xdp_md *ctx)
|
|
{
|
|
void *data_end = (void *)(long)ctx->data_end;
|
|
void *data = (void *)(long)ctx->data;
|
|
struct ethhdr *eth = data;
|
|
|
|
// Verificação básica do header ethernet
|
|
if ((void*)(eth + 1) > data_end)
|
|
return XDP_ABORTED;
|
|
|
|
// Atualizar estatísticas
|
|
__u32 key = 0;
|
|
struct packet_stats *stats = bpf_map_lookup_elem(&stats_map, &key);
|
|
if (stats) {
|
|
stats->packets++;
|
|
stats->bytes += (data_end - data);
|
|
}
|
|
|
|
// Sempre permitir tráfego broadcast/multicast
|
|
if (eth->h_dest[0] & 1)
|
|
return XDP_PASS;
|
|
|
|
// Verificar se é tráfego IPv4
|
|
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
|
|
struct iphdr *iph = (void*)(eth + 1);
|
|
if ((void*)(iph + 1) > data_end)
|
|
return XDP_PASS; // Se não conseguir verificar, passa pro kernel
|
|
|
|
// Permitir todo tráfego local (entre containers)
|
|
if ((iph->daddr & 0xffff0000) == 0x0a0f0000) // 10.15.0.0/16
|
|
return XDP_PASS;
|
|
}
|
|
|
|
// Para outros protocolos, deixa passar
|
|
return XDP_PASS;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|