-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean-dns.kern.c
375 lines (344 loc) · 10.8 KB
/
clean-dns.kern.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/if_pppox.h>
#include <linux/udp.h>
#include <linux/in.h>
#include <linux/ipv6.h>
#include <linux/ip.h>
#include <linux/pkt_cls.h>
#include <stdint.h>
#include <stdbool.h>
#define _AA (1 << 10)
#define _AD (1 << 5)
#define PPP_IP (0x21)
#define PPP_IP6 (0x57)
char __license[] SEC("license") = "GPL";
/* Map for blocking IP addresses from userspace */
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(key_size, sizeof(__u32));
__type(value, sizeof(__u32));
__uint(max_entries, 1);
} flowlabel_map SEC(".maps");
/* Ingress hook - handle incoming packets */
SEC("tc")
int tc_ingress(struct __sk_buff *skb) {
void *data_end = (void *)(long)skb->data_end;
void *data = (void *)(long)skb->data;
uint16_t daddr16[8],saddr16[8];
int h_proto, l4proto;
int hdrsize = sizeof(struct ethhdr);
if (data + hdrsize > data_end) {
return TC_ACT_SHOT;
}
struct ethhdr *eth = data;
h_proto = bpf_ntohs(eth->h_proto);
if (h_proto == ETH_P_PPP_SES) {
struct pppoe_hdr *pppoe;
pppoe = (void*)(eth + 1);
if ((void*)pppoe + PPPOE_SES_HLEN > data_end) {
return TC_ACT_SHOT;
}
h_proto = bpf_ntohs(pppoe->tag[0].tag_type);
switch (h_proto) {
case PPP_IP:
h_proto = ETH_P_IP;
break;
case PPP_IP6:
h_proto = ETH_P_IPV6;
break;
default:
return TC_ACT_OK;
}
hdrsize += PPPOE_SES_HLEN;
} else if (h_proto == ETH_P_PPP_DISC) {
return TC_ACT_OK;
}
if (h_proto == ETH_P_IP) {
struct iphdr iph;
bpf_skb_load_bytes(skb, hdrsize, &iph, sizeof(struct iphdr));
l4proto = iph.protocol;
uint32_t saddr = iph.saddr;
int iphsize = iph.ihl * 4;
int ipid = iph.id;
int frag_off = iph.frag_off;
if (l4proto != IPPROTO_UDP) {
return TC_ACT_OK;
}
if (saddr != 0x08080808 && saddr != 0x04040808) {
return TC_ACT_OK;
}
// drop if id is 0
if (ipid == 0) {
bpf_printk("[ingress]*** dropped due to id == 0: %0x", saddr);
return TC_ACT_SHOT;
}
// drop if flag is 0x40 (Don't fragment)
if (frag_off & 0x40) {
bpf_printk("[ingress]*** dropped due to flag is 0x40: %0x", frag_off);
return TC_ACT_SHOT;
}
hdrsize += iphsize;
} else if (h_proto == ETH_P_IPV6) {
struct ipv6hdr ip6h;
bpf_skb_load_bytes(skb, hdrsize, &ip6h, sizeof(struct ipv6hdr));
l4proto = ip6h.nexthdr;
if (l4proto != IPPROTO_UDP) {
return TC_ACT_OK;
}
uint32_t flowlabel = (uint32_t)(ip6h.flow_lbl[0]) << 16 | (uint32_t)(ip6h.flow_lbl[1]) << 8 | (uint32_t)(ip6h.flow_lbl[2]);
for (int i = 0; i < 8; i++) {
daddr16[i] = bpf_ntohs(ip6h.daddr.in6_u.u6_addr16[i]);
saddr16[i] = bpf_ntohs(ip6h.saddr.in6_u.u6_addr16[i]);
}
if (saddr16[7] != 0x8888 && saddr16[7] != 0x8844) {
return TC_ACT_OK;
}
uint32_t key = ((uint32_t)daddr16[7])<<16 | (uint32_t)saddr16[7];
void* rec = bpf_map_lookup_elem(&flowlabel_map, &key);
if (rec) {
if (flowlabel == *(uint32_t*)rec) {
bpf_printk("[ingress]*** dropped due to flowlabel duplicated: %0lx", *(uint32_t*)rec);
return TC_ACT_SHOT;
}
}
if (flowlabel == 0) {
bpf_printk("[ingress]*** dropped due to flowlable is zero: flowlabel: %0lx", flowlabel) ;
return TC_ACT_SHOT;
}
hdrsize += sizeof(struct ipv6hdr);
} else {
return TC_ACT_OK;
}
struct udphdr udph;
bpf_skb_load_bytes(skb, hdrsize, &udph, sizeof(struct udphdr));
if (udph.source != bpf_htons(53)) {
return TC_ACT_OK;
}
uint8_t *udp_data = (uint8_t*)(long)((&udph) + 1);
uint8_t *udp_end = (uint8_t*) (long)(skb->data_end);
if (udp_data + 10 > udp_end) {
return TC_ACT_OK;
}
// pass if the dns packet has multiple answers
if (udp_data[6] != 0 || udp_data[7] > 1) {
// Answer RRs != 1
return TC_ACT_OK;
}
// pass if the dns packet has authority answer
if (udp_data[8] != 0 || udp_data[9] != 0) {
// Authority RRs != 0
return TC_ACT_OK;
}
uint16_t flags = ((uint16_t)udp_data[2])<<8 | (uint16_t)(udp_data[3]);
// drop if dns flag has Authoritative mark
if (flags & _AA) {
bpf_printk("[ingress]*** dropped due to flags has authoriative mark: %0x", flags);
return TC_ACT_SHOT;
}
return TC_ACT_OK;
}
/* Egress hook - handle outgoing packets */
SEC("tc")
int tc_egress(struct __sk_buff *skb) {
void *data = (void *)(long)skb->data;
void *data_end = (void *)(long)skb->data_end;
int i, h_proto, l4proto;
uint16_t daddr16[8],saddr16[8];
struct pppoe_hdr *pppoe;
int hdrsize = sizeof(struct ethhdr);
if (data + hdrsize > data_end) {
return TC_ACT_SHOT;
}
struct ethhdr *eth = data;
h_proto = bpf_ntohs(eth->h_proto);
if (h_proto == ETH_P_PPP_SES) {
pppoe = (void*)(eth + 1);
if ((void*)pppoe + PPPOE_SES_HLEN > data_end) {
return TC_ACT_SHOT;
}
h_proto = bpf_ntohs(pppoe->tag[0].tag_type);
switch (h_proto) {
case PPP_IP:
h_proto = ETH_P_IP;
break;
case PPP_IP6:
h_proto = ETH_P_IPV6;
break;
default:
return TC_ACT_OK;
}
hdrsize += PPPOE_SES_HLEN;
} else if (h_proto == ETH_P_PPP_DISC) {
return TC_ACT_OK;
}
if (h_proto != ETH_P_IPV6) {
return TC_ACT_OK;
}
struct ipv6hdr ip6h;
bpf_skb_load_bytes(skb, hdrsize, &ip6h, sizeof(struct ipv6hdr));
l4proto = ip6h.nexthdr;
if (l4proto != IPPROTO_UDP) {
return TC_ACT_OK;
}
uint32_t flowlabel = (uint32_t)(ip6h.flow_lbl[0]) << 16 | (uint32_t)(ip6h.flow_lbl[1]) << 8 | (uint32_t)(ip6h.flow_lbl[2]);
for (i = 0; i < 8; i++) {
daddr16[i] = bpf_ntohs(ip6h.daddr.in6_u.u6_addr16[i]);
saddr16[i] = bpf_ntohs(ip6h.saddr.in6_u.u6_addr16[i]);
}
if (daddr16[7] != 0x8888 && daddr16[7] != 0x8844) {
return TC_ACT_OK;
}
uint32_t key = ((uint32_t)saddr16[7])<<16 | (uint32_t)daddr16[7];
if (bpf_map_update_elem(&flowlabel_map, &key, &flowlabel, BPF_NOEXIST) == 0 ) {
bpf_printk("[egress] google dst: [%0x:%0x:%0x:%0x:%0x:%0x:%0x:%0x]: flowlabel: %0lx",
daddr16[0], daddr16[1], daddr16[2], daddr16[3],
daddr16[4], daddr16[5], daddr16[6], daddr16[7], flowlabel) ;
}
return TC_ACT_OK;
}
SEC("xdp")
int clean_dns(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth;
struct ipv6hdr *ip6h;
struct iphdr *iph;
struct udphdr *udph;
uint16_t flags;
uint32_t saddr;
uint16_t saddr16[8];
uint16_t daddr16[8];
int i;
int h_proto, l4proto;
/* Parse Ethernet and IP/IPv6 headers */
int hdrsize = sizeof(struct ethhdr);
if (data + hdrsize > data_end) {
return XDP_DROP;
}
eth = data;
h_proto = bpf_ntohs(eth->h_proto);
if (h_proto == ETH_P_PPP_SES) {
struct pppoe_hdr *pppoe;
pppoe = (void*)(eth + 1);
if ((void*)pppoe + PPPOE_SES_HLEN > data_end) {
return XDP_DROP;
}
h_proto = bpf_ntohs(pppoe->tag[0].tag_type);
switch (h_proto) {
case PPP_IP:
h_proto = ETH_P_IP;
break;
case PPP_IP6:
h_proto = ETH_P_IPV6;
break;
default:
return XDP_PASS;
}
hdrsize += PPPOE_SES_HLEN;
} else if (h_proto == ETH_P_PPP_DISC) {
return XDP_PASS;
}
if (h_proto == ETH_P_IP) {
iph = data + hdrsize;
if ((void*)(iph + 1) > data_end) {
return XDP_DROP;
}
hdrsize = iph->ihl * 4;
/* Sanity check packet field is valid */
if (hdrsize < sizeof(struct iphdr)) {
return XDP_DROP;
}
/* Variable-length IPv4 header, need to use byte-based arithmetic */
if ((void*)iph + hdrsize > data_end) {
return XDP_DROP;
}
l4proto = iph->protocol;
if (l4proto != IPPROTO_UDP) {
return XDP_PASS;
}
saddr = iph->saddr;
if (saddr != 0x08080808 && saddr != 0x04040808) {
return XDP_PASS;
}
// drop if id is 0
if (iph->id == 0) {
bpf_printk("[xdp]*** dropped due to id is zero: %0x", iph->saddr);
return XDP_DROP;
}
// drop if flag is 0x40 (Don't fragment)
if (iph->frag_off & 0x40) {
bpf_printk("[xdp]*** dropped duto to flag is DF: %0x", iph->frag_off);
return XDP_DROP;
}
} else if (h_proto == ETH_P_IPV6) {
ip6h = data + hdrsize;
if ((void*)(ip6h + 1) > data_end) {
return XDP_DROP;
}
l4proto = ip6h->nexthdr;
if (l4proto != IPPROTO_UDP) {
return XDP_PASS;
}
for (i = 0; i < 8; i ++) {
daddr16[i] = bpf_ntohs(ip6h->daddr.in6_u.u6_addr16[i]);
saddr16[i] = bpf_ntohs(ip6h->saddr.in6_u.u6_addr16[i]);
}
uint32_t flowlabel = (uint32_t)(ip6h->flow_lbl[0]) << 16 | (uint32_t)(ip6h->flow_lbl[1]) << 8 | (uint32_t)(ip6h->flow_lbl[2]);
if (saddr16[7] != 0x8888 && saddr16[7] != 0x8844) {
return XDP_PASS;
}
uint32_t key = ((uint32_t)daddr16[7])<<16 | (uint32_t)saddr16[7];
void* rec = bpf_map_lookup_elem(&flowlabel_map, &key);
if (rec) {
if (flowlabel == *(uint32_t*)rec) {
bpf_printk("[xdp]*** dropped due to flowlabel duplicated: %0lx", *(uint32_t*)rec);
return XDP_DROP;
}
}
if (flowlabel == 0) {
bpf_printk("[xdp]*** dropped due to flowlabel is zero: %0x", flowlabel);
return XDP_DROP;
}
hdrsize = sizeof(struct ipv6hdr);
} else {
return XDP_PASS;
}
udph = data + hdrsize;
if ((void*)(udph + 1) > data_end) {
return XDP_PASS;
}
if (bpf_ntohs(udph->len) - sizeof(struct udphdr) < 0) {
return XDP_PASS;
}
if (udph->source != bpf_htons(53)) {
return XDP_PASS;
}
// get first 10 bytes of udp data (7,8 is Answer RRs, 8, 9 is Authority RRs)
uint8_t *udp_data = (uint8_t *)(long)(udph+1);
uint8_t *udp_data_end = (uint8_t *)(long)ctx->data_end;
if (udp_data + 10 > udp_data_end) {
return XDP_DROP;
}
// pass if the dns packet has multiple answers
if (udp_data[6] != 0 || udp_data[7] > 1) {
// Answer RRs != 1
return XDP_PASS;
}
// pass if the dns packet has authority answer
if (udp_data[8] != 0 || udp_data[9] != 0) {
// Authority RRs != 0
return XDP_PASS;
}
flags = ((uint16_t)udp_data[2])<<8 | (uint16_t)(udp_data[3]);
// bpf_printk("flags = %0x", flags);
// drop if dns flag has Authoritative mark
if (flags & _AA) {
bpf_printk("[xdp]*** dropped due to flags has authoriative mark: %0x", flags);
return XDP_DROP;
}
return XDP_PASS;
}