-
Notifications
You must be signed in to change notification settings - Fork 922
/
Copy pathxdp.h
195 lines (170 loc) · 3.93 KB
/
xdp.h
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
#ifndef __XDP_H__
#define __XDP_H__
#include <net/sock.h>
#include <uapi/linux/udp.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/ipv6.h>
#define DNS_PORT 53
// do not use libc includes because this causes clang
// to include 32bit headers on 64bit ( only ) systems.
typedef __u8 uint8_t;
typedef __u16 uint16_t;
typedef __u32 uint32_t;
typedef __u64 uint64_t;
#define memcpy __builtin_memcpy
/*
* Helper pointer to parse the incoming packets
* Copyright 2020, NLnet Labs, All rights reserved.
*/
struct cursor {
void *pos;
void *end;
};
/*
* Store the VLAN header
* Copyright 2020, NLnet Labs, All rights reserved.
*/
struct vlanhdr {
uint16_t tci;
uint16_t encap_proto;
};
/*
* Store the DNS header
* Copyright 2020, NLnet Labs, All rights reserved.
*/
struct dnshdr {
uint16_t id;
union {
struct {
#if BYTE_ORDER == LITTLE_ENDIAN
uint8_t rd : 1;
uint8_t tc : 1;
uint8_t aa : 1;
uint8_t opcode : 4;
uint8_t qr : 1;
uint8_t rcode : 4;
uint8_t cd : 1;
uint8_t ad : 1;
uint8_t z : 1;
uint8_t ra : 1;
#elif BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == PDP_ENDIAN
uint8_t qr : 1;
uint8_t opcode : 4;
uint8_t aa : 1;
uint8_t tc : 1;
uint8_t rd : 1;
uint8_t ra : 1;
uint8_t z : 1;
uint8_t ad : 1;
uint8_t cd : 1;
uint8_t rcode : 4;
#endif
} as_bits_and_pieces;
uint16_t as_value;
} flags;
uint16_t qdcount;
uint16_t ancount;
uint16_t nscount;
uint16_t arcount;
};
/*
* Store the qname and qtype
*/
struct dns_qname
{
uint8_t qname[255];
uint16_t qtype;
};
/*
* The possible actions to perform on the packet
* PASS: XDP_PASS
* DROP: XDP_DROP
* TC: set TC bit and XDP_TX
*/
enum dns_action : uint8_t {
PASS = 0,
DROP = 1,
TC = 2
};
struct CIDR4
{
uint32_t cidr;
uint32_t addr;
};
struct CIDR6
{
uint32_t cidr;
struct in6_addr addr;
};
struct IPv4AndPort
{
uint32_t addr;
uint16_t port;
};
struct IPv6AndPort
{
struct in6_addr addr;
uint16_t port;
};
/*
* Store the matching counter and the associated action for a blocked element
*/
struct map_value
{
uint64_t counter;
enum dns_action action;
};
/*
* Initializer of a cursor pointer
* Copyright 2020, NLnet Labs, All rights reserved.
*/
static inline void cursor_init(struct cursor *c, struct xdp_md *ctx)
{
c->end = (void *)(long)ctx->data_end;
c->pos = (void *)(long)ctx->data;
}
/*
* Header parser functions
* Copyright 2020, NLnet Labs, All rights reserved.
*/
#define PARSE_FUNC_DECLARATION(STRUCT) \
static inline struct STRUCT *parse_ ## STRUCT (struct cursor *c) \
{ \
struct STRUCT *ret = c->pos; \
if (c->pos + sizeof(struct STRUCT) > c->end) \
return 0; \
c->pos += sizeof(struct STRUCT); \
return ret; \
}
PARSE_FUNC_DECLARATION(ethhdr)
PARSE_FUNC_DECLARATION(vlanhdr)
PARSE_FUNC_DECLARATION(iphdr)
PARSE_FUNC_DECLARATION(ipv6hdr)
PARSE_FUNC_DECLARATION(udphdr)
PARSE_FUNC_DECLARATION(dnshdr)
/*
* Parse ethernet frame and fill the struct
* Copyright 2020, NLnet Labs, All rights reserved.
*/
static inline struct ethhdr *parse_eth(struct cursor *c, uint16_t *eth_proto)
{
struct ethhdr *eth;
if (!(eth = parse_ethhdr(c)))
return 0;
*eth_proto = eth->h_proto;
if (*eth_proto == bpf_htons(ETH_P_8021Q)
|| *eth_proto == bpf_htons(ETH_P_8021AD)) {
struct vlanhdr *vlan;
if (!(vlan = parse_vlanhdr(c)))
return 0;
*eth_proto = vlan->encap_proto;
if (*eth_proto == bpf_htons(ETH_P_8021Q)
|| *eth_proto == bpf_htons(ETH_P_8021AD)) {
if (!(vlan = parse_vlanhdr(c)))
return 0;
*eth_proto = vlan->encap_proto;
}
}
return eth;
}
#endif