-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdp-drop-icmp.c
52 lines (36 loc) · 1.05 KB
/
xdp-drop-icmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <uapi/linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/in.h>
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long long u64;
#define htons(x) __cpu_to_be16(x)
#define SEC(NAME) __attribute__((section(NAME), used))
static int parse_ipv4(void *data, u64 nh_off, void *data_end)
{
struct iphdr *iph = data + nh_off;
if ((void *)(iph + 1) > data_end)
return 0;
return iph->protocol;
}
SEC("xdp-icmp")
int xdp_drop_icmp(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
u16 h_proto;
u64 nh_off;
u32 ipproto;
nh_off = sizeof(*eth);
if (data + nh_off > data_end)
return XDP_DROP;
h_proto = eth->h_proto;
if (h_proto == htons(ETH_P_IP)) {
ipproto = parse_ipv4(data, nh_off, data_end);
if (IPPROTO_ICMP == ipproto || 0 == ipproto)
return XDP_DROP;
}
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";