14#include <bpf/bpf_endian.h>
15#include <bpf/bpf_helpers.h>
17#include <linux/if_ether.h>
29 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
30 __uint(max_entries, 65536);
33 __uint(map_flags, BPF_F_NO_PREALLOC);
34} whitelist_map
SEC(
".maps");
38 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
39 __uint(max_entries, 65536);
42 __uint(map_flags, BPF_F_NO_PREALLOC);
43} blacklist_map
SEC(
".maps");
47 __uint(type, BPF_MAP_TYPE_HASH);
48 __uint(max_entries, 65536);
51 __uint(map_flags, BPF_F_NO_PREALLOC);
52} drop_counter
SEC(
".maps");
59 void *data_end = (
void *)(
unsigned long long)ctx->data_end;
60 void *data = (
void *)(
unsigned long long)ctx->data;
62 struct ethhdr *eth = data;
63 if ((
void *)(eth + 1) > data_end)
66 if (bpf_ntohs(eth->h_proto) != ETH_P_IP)
69 struct iphdr *ip = (
struct iphdr *)(eth + 1);
70 if ((
void *)(ip + 1) > data_end)
77 if (bpf_map_lookup_elem(&whitelist_map, &key))
80 if (bpf_map_lookup_elem(&blacklist_map, &key)) {
81 __u64 *count = bpf_map_lookup_elem(&drop_counter, &
ip->saddr);
83 __sync_fetch_and_add(count, 1);
86 bpf_map_update_elem(&drop_counter, &
ip->saddr, &one, BPF_NOEXIST);
95char _license[]
SEC(
"license") =
"GPL";
struct @0 SEC(".maps")
LPM trie чёрного списка: IP-адреса источников для немедленного сброса.
__uint(type, BPF_MAP_TYPE_LPM_TRIE)
__type(key, struct lpm_key)
int xdp_blocker_func(struct xdp_md *ctx)
Ключ для LPM trie-карт: длина префикса + IP в network byte order.
__u32 prefixlen
Количество значащих битов (0-32).
__u32 ip
IP-адрес в network byte order (с маской).