Less effecient, but much, much less naive rate-limiter
[flowspec-xdp] / xdp.c
1 #include <stdint.h>
2 #include <endian.h>
3 #include <linux/if_ether.h>
4 #include <linux/ip.h>
5 #include <linux/udp.h>
6 #include <linux/icmp.h>
7 #include <linux/icmpv6.h>
8 #include <arpa/inet.h>
9
10 #define NULL (void*)0
11
12 /* IP flags. */
13 #define IP_CE           0x8000          /* Flag: "Congestion"           */
14 #define IP_DF           0x4000          /* Flag: "Don't Fragment"       */
15 #define IP_MF           0x2000          /* Flag: "More Fragments"       */
16 #define IP_OFFSET       0x1FFF          /* "Fragment Offset" part       */
17
18 #define IP_PROTO_TCP 6
19 #define IP_PROTO_UDP 17
20 #define IP_PROTO_ICMP 1
21 #define IP6_PROTO_ICMPV6 58
22 #define IP6_PROTO_FRAG 44
23
24 typedef __uint128_t uint128_t;
25
26 // Our own ipv6hdr that uses uint128_t
27 struct ip6hdr {
28 #if defined(__LITTLE_ENDIAN_BITFIELD)
29         __u8    priority:4,
30                 version:4;
31 #elif defined(__BIG_ENDIAN_BITFIELD)
32         __u8    version:4,
33                 priority:4;
34 #else
35 #error  "Please fix <asm/byteorder.h>"
36 #endif
37         __u8    flow_lbl[3];
38
39         __be16  payload_len;
40         __u8            nexthdr;
41         __u8            hop_limit;
42
43         uint128_t       saddr;
44         uint128_t       daddr;
45 } __attribute__((packed));
46
47 #define IP6_MF 1
48 #define IP6_FRAGOFF 0xfff8
49 struct ip6_fraghdr {
50         uint8_t nexthdr;
51         uint8_t _reserved;
52         uint16_t frag_off; // BE low 3 bits flags, last is "more frags"
53         uint32_t id;
54 } __attribute__((packed));
55
56 // Our own ethhdr with optional vlan tags
57 struct ethhdr_vlan {
58         unsigned char   h_dest[ETH_ALEN];       /* destination eth addr */
59         unsigned char   h_source[ETH_ALEN];     /* source ether addr    */
60         __be16          vlan_magic;             /* 0x8100 */
61         __be16          tci;            /* PCP (3 bits), DEI (1 bit), and VLAN (12 bits) */
62         __be16          h_proto;                /* packet type ID field */
63 } __attribute__((packed));
64
65 // Our own tcphdr without the flags blown up
66 struct tcphdr {
67         __be16  source;
68         __be16  dest;
69         __be32  seq;
70         __be32  ack_seq;
71         __u16   flags;
72         __be16  window;
73         __sum16 check;
74         __be16  urg_ptr;
75 } __attribute__((packed));
76
77 // Note that all operations on uint128s *stay* in Network byte order!
78
79 #if defined(__LITTLE_ENDIAN)
80 #define BIGEND32(v) ((v >> 3*8) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | (v << 3*8) & 0xff000000)
81 #elif defined(__BIG_ENDIAN)
82 #define BIGEND32(v) (v)
83 #else
84 #error "Need endian info"
85 #endif
86
87 #if defined(__LITTLE_ENDIAN)
88 #define BIGEND128(a, b, c, d) ( \
89                 (((uint128_t)BIGEND32(d)) << 3*32) | \
90                 (((uint128_t)BIGEND32(c)) << 2*32) | \
91                 (((uint128_t)BIGEND32(b)) << 1*32) | \
92                 (((uint128_t)BIGEND32(a)) << 0*32))
93 #define HTON128(a) BIGEND128(a >> 3*32, a >> 2*32, a >> 1*32, a>> 0*32)
94 // Yes, somehow macro'ing this changes LLVM's view of htons...
95 #define BE16(a) ((((uint16_t)(a & 0xff00)) >> 8) | (((uint16_t)(a & 0xff)) << 8))
96 #elif defined(__BIG_ENDIAN)
97 #define BIGEND128(a, b, c, d) ((((uint128_t)a) << 3*32) | (((uint128_t)b) << 2*32) | (((uint128_t)c) << 1*32) | (((uint128_t)d) << 0*32))
98 #define HTON128(a) (a)
99 #define BE16(a) ((uint16_t)a)
100 #else
101 #error "Need endian info"
102 #endif
103
104 #define MASK4(pfxlen) BIGEND32(~((((uint32_t)1) << (32 - pfxlen)) - 1))
105 #define MASK6(pfxlen) HTON128(~((((uint128_t)1) << (128 - pfxlen)) - 1))
106 #define MASK6_OFFS(offs, pfxlen) HTON128((~((((uint128_t)1) << (128 - pfxlen)) - 1)) & ((((uint128_t)1) << (128 - offs)) - 1))
107
108 // PARSE is used as a preprocessor flag to indicate parsing fields
109 #define PARSE 42
110 #include "rules.h"
111
112 #define unlikely(a) __builtin_expect(a, 0)
113 #define likely(a) __builtin_expect(a, 1)
114
115 static const uint32_t PKT_LEN_DROP = 0;
116 static const uint32_t VLAN_DROP = 1;
117 static const uint32_t IHL_DROP = 2;
118 static const uint32_t V6FRAG_DROP = 3;
119 #define STATIC_RULE_CNT 4
120
121 #define DO_RETURN(reason, ret) {\
122                 if (ret == XDP_DROP) { INCREMENT_MATCH(reason); } \
123                 return ret; \
124         }
125
126 // It seems (based on drop counts) that data_end points to the last byte, not one-past-the-end.
127 // This feels strange, but some documentation suggests > here as well, so we stick with that.
128 #define CHECK_LEN(start, struc) \
129         if (unlikely((void*)(start) + sizeof(struct struc) > data_end)) DO_RETURN(PKT_LEN_DROP, XDP_DROP);
130
131 #ifdef TEST
132 // 64 bit version of xdp_md for testing
133 struct xdp_md {
134         __u64 data;
135         __u64 data_end;
136         __u64 data_meta;
137         /* Below access go through struct xdp_rxq_info */
138         __u64 ingress_ifindex; /* rxq->dev->ifindex */
139         __u64 rx_queue_index;  /* rxq->queue_index  */
140
141         __u64 egress_ifindex;  /* txq->dev->ifindex */
142 };
143 static const int XDP_PASS = 0;
144 static const int XDP_DROP = 1;
145
146 static long drop_cnt_map[RULECNT + STATIC_RULE_CNT];
147 #define INCREMENT_MATCH(reason) drop_cnt_map[reason] += 1;
148
149 #else
150 #include <linux/bpf.h>
151 #include <bpf/bpf_helpers.h>
152
153 struct bpf_map_def SEC("maps") drop_cnt_map = {
154         .type = BPF_MAP_TYPE_PERCPU_ARRAY,
155         .key_size = sizeof(uint32_t),
156         .value_size = sizeof(long),
157         .max_entries = RULECNT + STATIC_RULE_CNT,
158 };
159 #define INCREMENT_MATCH(reason) { \
160         long *value = bpf_map_lookup_elem(&drop_cnt_map, &reason); \
161         if (value) \
162                 *value += 1; \
163 }
164
165 #ifdef RATE_CNT
166 struct ratelimit {
167         struct bpf_spin_lock lock;
168         int64_t sent_bytes;
169         int64_t sent_time;
170 };
171 struct {
172         __uint(type, BPF_MAP_TYPE_ARRAY);
173         __uint(max_entries, RATE_CNT);
174         __u32 *key;
175         struct ratelimit *value;
176 } rate_map SEC(".maps");
177 #endif
178
179 SEC("xdp_drop")
180 #endif
181 int xdp_drop_prog(struct xdp_md *ctx)
182 {
183         const void *const data_end = (void *)(size_t)ctx->data_end;
184
185         const void * pktdata;
186         unsigned short eth_proto;
187
188         {
189                 CHECK_LEN((size_t)ctx->data, ethhdr);
190                 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
191
192 #if PARSE_8021Q == PARSE
193                 if (likely(eth->h_proto == BE16(ETH_P_8021Q))) {
194                         CHECK_LEN((size_t)ctx->data, ethhdr_vlan);
195                         const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
196
197 #ifdef REQ_8021Q
198                         if (unlikely((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q)))
199                                 DO_RETURN(VLAN_DROP, XDP_DROP);
200 #endif
201
202                         eth_proto = eth_vlan->h_proto;
203                         pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
204 #else
205                 if (unlikely(eth->h_proto == BE16(ETH_P_8021Q))) {
206                         DO_RETURN(VLAN_DROP, PARSE_8021Q);
207 #endif
208                 } else {
209 #ifdef REQ_8021Q
210                         DO_RETURN(VLAN_DROP, XDP_DROP);
211 #else
212                         pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
213                         eth_proto = eth->h_proto;
214 #endif
215                 }
216         }
217
218         const void *l4hdr = NULL;
219         const struct tcphdr *tcp = NULL;
220         uint8_t ports_valid = 0;
221         uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
222
223 #ifdef NEED_V4_PARSE
224         if (eth_proto == BE16(ETH_P_IP)) {
225                 CHECK_LEN(pktdata, iphdr);
226                 struct iphdr *ip = (struct iphdr*) pktdata;
227
228 #if PARSE_IHL == PARSE
229                 if (unlikely(ip->ihl < 5)) DO_RETURN(IHL_DROP, XDP_DROP);
230                 l4hdr = pktdata + ip->ihl * 4;
231 #else
232                 if (ip->ihl != 5) DO_RETURN(IHL_DROP, PARSE_IHL);
233                 l4hdr = pktdata + 5*4;
234 #endif
235
236                 const struct icmphdr *icmp = NULL;
237                 if ((ip->frag_off & BE16(IP_OFFSET)) == 0) {
238                         if (ip->protocol == IP_PROTO_TCP) {
239                                 CHECK_LEN(l4hdr, tcphdr);
240                                 tcp = (struct tcphdr*) l4hdr;
241                                 sport = BE16(tcp->source);
242                                 dport = BE16(tcp->dest);
243                                 ports_valid = 1;
244                         } else if (ip->protocol == IP_PROTO_UDP) {
245                                 CHECK_LEN(l4hdr, udphdr);
246                                 const struct udphdr *udp = (struct udphdr*) l4hdr;
247                                 sport = BE16(udp->source);
248                                 dport = BE16(udp->dest);
249                                 ports_valid = 1;
250                         } else if (ip->protocol == IP_PROTO_ICMP) {
251                                 CHECK_LEN(l4hdr, icmphdr);
252                                 icmp = (struct icmphdr*) l4hdr;
253                         }
254                 }
255
256                 RULES4
257         }
258 #endif
259 #ifdef NEED_V6_PARSE
260         if (eth_proto == BE16(ETH_P_IPV6)) {
261                 CHECK_LEN(pktdata, ip6hdr);
262                 struct ip6hdr *ip6 = (struct ip6hdr*) pktdata;
263
264                 l4hdr = pktdata + 40;
265
266                 uint8_t v6nexthdr = ip6->nexthdr;
267                 const struct ip6_fraghdr *frag6 = NULL;
268 #ifdef PARSE_V6_FRAG
269 #if PARSE_V6_FRAG == PARSE
270                 if (ip6->nexthdr == IP6_PROTO_FRAG) {
271                         CHECK_LEN(l4hdr, ip6_fraghdr);
272                         frag6 = (struct ip6_fraghdr*) l4hdr;
273                         l4hdr = l4hdr + sizeof(struct ip6_fraghdr);
274                         v6nexthdr = frag6->nexthdr;
275 #else
276                 if (unlikely(ip6->nexthdr == IP6_PROTO_FRAG)) {
277                         DO_RETURN(V6FRAG_DROP, PARSE_V6_FRAG);
278 #endif
279                 }
280 #endif
281                 // TODO: Handle more options?
282
283                 const struct icmp6hdr *icmpv6 = NULL;
284                 if (frag6 == NULL || (frag6->frag_off & BE16(IP6_FRAGOFF)) == 0) {
285                         if (v6nexthdr == IP_PROTO_TCP) {
286                                 CHECK_LEN(l4hdr, tcphdr);
287                                 tcp = (struct tcphdr*) l4hdr;
288                                 sport = BE16(tcp->source);
289                                 dport = BE16(tcp->dest);
290                                 ports_valid = 1;
291                         } else if (v6nexthdr == IP_PROTO_UDP) {
292                                 CHECK_LEN(l4hdr, udphdr);
293                                 const struct udphdr *udp = (struct udphdr*) l4hdr;
294                                 sport = BE16(udp->source);
295                                 dport = BE16(udp->dest);
296                                 ports_valid = 1;
297                         } else if (v6nexthdr == IP6_PROTO_ICMPV6) {
298                                 CHECK_LEN(l4hdr, icmp6hdr);
299                                 icmpv6 = (struct icmp6hdr*) l4hdr;
300                         }
301                 }
302
303                 RULES6
304         }
305 #endif
306
307         return XDP_PASS;
308 }
309
310 #ifdef TEST
311 #include <assert.h>
312 #include <string.h>
313
314 char d[] = TEST;
315 int main() {
316         struct xdp_md test = {
317                 .data = (uint64_t)d,
318                 // -1 because sizeof includes a trailing null in the "string"
319                 .data_end = (uint64_t)(d + sizeof(d) - 1),
320         };
321         assert(xdp_drop_prog(&test) == TEST_EXP);
322 }
323 #endif