14#include <linux/types.h>
15#include <bpf/bpf_endian.h>
16#include <bpf/bpf_helpers.h>
18#include <linux/if_ether.h>
30 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
31 __uint(max_entries, 65536);
34 __uint(map_flags, BPF_F_NO_PREALLOC);
35} whitelist_map
SEC(
".maps");
39 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
40 __uint(max_entries, 65536);
43 __uint(map_flags, BPF_F_NO_PREALLOC);
44} blacklist_map
SEC(
".maps");
48 __uint(type, BPF_MAP_TYPE_HASH);
49 __uint(max_entries, 65536);
52 __uint(map_flags, BPF_F_NO_PREALLOC);
53} drop_counter
SEC(
".maps");
60 void *data_end = (
void *)(
unsigned long long)ctx->data_end;
61 void *data = (
void *)(
unsigned long long)ctx->data;
63 struct ethhdr *eth = data;
64 if ((
void *)(eth + 1) > data_end)
67 if (bpf_ntohs(eth->h_proto) != ETH_P_IP)
70 struct iphdr *ip = (
struct iphdr *)(eth + 1);
71 if ((
void *)(ip + 1) > data_end)
78 if (bpf_map_lookup_elem(&whitelist_map, &key))
81 if (bpf_map_lookup_elem(&blacklist_map, &key)) {
82 __u64 *count = bpf_map_lookup_elem(&drop_counter, &
ip->saddr);
84 __sync_fetch_and_add(count, 1);
87 bpf_map_update_elem(&drop_counter, &
ip->saddr, &one, BPF_NOEXIST);
96char _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 (с маской).