Tag short packets as unlikely
[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 #else
100 #error "Need endian info"
101 #endif
102
103 #define MASK4(pfxlen) BIGEND32(~((((uint32_t)1) << (32 - pfxlen)) - 1))
104 #define MASK6(pfxlen) HTON128(~((((uint128_t)1) << (128 - pfxlen)) - 1))
105 #define MASK6_OFFS(offs, pfxlen) HTON128((~((((uint128_t)1) << (128 - pfxlen)) - 1)) & ((((uint128_t)1) << (128 - offs)) - 1))
106
107 // Note rules.h may also define PARSE_8021Q and REQ_8021Q
108 // Note rules.h may also define PARSE_IHL
109 #include "rules.h"
110
111 #define unlikely(a) __builtin_expect(a, 0)
112 #define likely(a) __builtin_expect(a, 1)
113
114 #ifdef TEST
115 // 64 bit version of xdp_md for testing
116 struct xdp_md {
117         __u64 data;
118         __u64 data_end;
119         __u64 data_meta;
120         /* Below access go through struct xdp_rxq_info */
121         __u64 ingress_ifindex; /* rxq->dev->ifindex */
122         __u64 rx_queue_index;  /* rxq->queue_index  */
123
124         __u64 egress_ifindex;  /* txq->dev->ifindex */
125 };
126 static const int XDP_PASS = 0;
127 static const int XDP_DROP = 1;
128 #else
129 #include <linux/bpf.h>
130 #include <bpf/bpf_helpers.h>
131
132 SEC("xdp_drop")
133 #endif
134 int xdp_drop_prog(struct xdp_md *ctx)
135 {
136         const void *const data_end = (void *)(size_t)ctx->data_end;
137
138         const void * pktdata;
139         unsigned short eth_proto;
140
141         {
142                 if (unlikely((void*)(size_t)ctx->data + sizeof(struct ethhdr) > data_end))
143                         return XDP_DROP;
144                 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
145
146 #ifdef PARSE_8021Q
147                 if (likely(eth->h_proto == BE16(ETH_P_8021Q))) {
148                         if (unlikely((void*)(size_t)ctx->data + sizeof(struct ethhdr_vlan) > data_end))
149                                 return XDP_DROP;
150                         const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
151
152 #ifdef REQ_8021Q
153                         if (unlikely((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q)))
154                                 return XDP_DROP;
155 #endif
156
157                         eth_proto = eth_vlan->h_proto;
158                         pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
159                 } else {
160 #ifdef REQ_8021Q
161                         return XDP_DROP;
162 #else
163                         pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
164                         eth_proto = eth->h_proto;
165 #endif
166                 }
167 #else
168                 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
169                 eth_proto = eth->h_proto;
170 #endif
171         }
172
173         const struct tcphdr *tcp = NULL;
174         const struct udphdr *udp = NULL;
175         const struct icmphdr *icmp = NULL;
176         const struct icmp6hdr *icmpv6 = NULL;
177         const struct ip6_fraghdr *frag6 = NULL;
178         const struct iphdr *ip = NULL;
179         const struct ip6hdr *ip6 = NULL;
180         const void *l4hdr = NULL;
181         if (eth_proto == BE16(ETH_P_IP)) {
182                 if (unlikely(pktdata + sizeof(struct iphdr) > data_end))
183                         return XDP_DROP;
184                 ip = (struct iphdr*) pktdata;
185
186 #ifdef PARSE_IHL
187                 if (unlikely(ip->ihl < 5)) return XDP_DROP;
188                 l4hdr = pktdata + ip->ihl * 4;
189 #else
190                 if (ip->ihl != 5) return XDP_DROP;
191                 l4hdr = pktdata + 5*4;
192 #endif
193                 if (ip->protocol == IP_PROTO_TCP) {
194                         if (unlikely(l4hdr + sizeof(struct tcphdr) > data_end))
195                                 return XDP_DROP;
196                         tcp = (struct tcphdr*) l4hdr;
197                 } else if (ip->protocol == IP_PROTO_UDP) {
198                         if (unlikely(l4hdr + sizeof(struct udphdr) > data_end))
199                                 return XDP_DROP;
200                         udp = (struct udphdr*) l4hdr;
201                 } else if (ip->protocol == IP_PROTO_ICMP) {
202                         if (unlikely(l4hdr + sizeof(struct icmphdr) > data_end))
203                                 return XDP_DROP;
204                         icmp = (struct icmphdr*) l4hdr;
205                 }
206         } else if (eth_proto == BE16(ETH_P_IPV6)) {
207                 if (unlikely(pktdata + sizeof(struct ip6hdr) > data_end))
208                         return XDP_DROP;
209                 ip6 = (struct ip6hdr*) pktdata;
210
211                 l4hdr = pktdata + 40;
212
213                 uint8_t v6nexthdr;
214                 if (ip6->nexthdr == IP6_PROTO_FRAG) {
215                         if (unlikely(l4hdr + sizeof(struct ip6_fraghdr) > data_end))
216                                 return XDP_DROP;
217                         frag6 = (struct ip6_fraghdr*) l4hdr;
218                         l4hdr = l4hdr + sizeof(struct ip6_fraghdr);
219                         v6nexthdr = frag6->nexthdr;
220                 } else {
221                         v6nexthdr = ip6->nexthdr;
222                 }
223
224                 if (v6nexthdr == IP_PROTO_TCP) {
225                         if (unlikely(l4hdr + sizeof(struct tcphdr) > data_end))
226                                 return XDP_DROP;
227                         tcp = (struct tcphdr*) l4hdr;
228                 } else if (v6nexthdr == IP_PROTO_UDP) {
229                         if (unlikely(l4hdr + sizeof(struct udphdr) > data_end))
230                                 return XDP_DROP;
231                         udp = (struct udphdr*) l4hdr;
232                 } else if (v6nexthdr == IP6_PROTO_ICMPV6) {
233                         if (unlikely(l4hdr + sizeof(struct icmp6hdr) > data_end))
234                                 return XDP_DROP;
235                         icmpv6 = (struct icmp6hdr*) l4hdr;
236                 }
237                 // TODO: Handle some options?
238         } else {
239                 return XDP_PASS;
240         }
241
242         uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
243         if (tcp != NULL) {
244                 sport = BE16(tcp->source);
245                 dport = BE16(tcp->dest);
246         } else if (udp != NULL) {
247                 sport = BE16(udp->source);
248                 dport = BE16(udp->dest);
249         }
250
251         RULES
252
253         return XDP_PASS;
254 }
255
256 #ifdef TEST
257 #include <assert.h>
258 #include <string.h>
259
260 const char d[] = TEST;
261 int main() {
262         struct xdp_md test = {
263                 .data = (uint64_t)d,
264                 // -1 because sizeof includes a trailing null in the "string"
265                 .data_end = (uint64_t)(d + sizeof(d) - 1),
266         };
267         assert(xdp_drop_prog(&test) == TEST_EXP);
268 }
269 #endif