87b7eb5edae258cfbe695c21ac30ff4ae9c4a9a5
[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 IP_PROTO_ICMPV6 58
22
23 typedef __uint128_t uint128_t;
24
25 // Our own ipv6hdr that uses uint128_t
26 struct ip6hdr {
27 #if defined(__LITTLE_ENDIAN_BITFIELD)
28         __u8    priority:4,
29                 version:4;
30 #elif defined(__BIG_ENDIAN_BITFIELD)
31         __u8    version:4,
32                 priority:4;
33 #else
34 #error  "Please fix <asm/byteorder.h>"
35 #endif
36         __u8    flow_lbl[3];
37
38         __be16  payload_len;
39         __u8            nexthdr;
40         __u8            hop_limit;
41
42         uint128_t       saddr;
43         uint128_t       daddr;
44 } __attribute__((packed));
45
46 // Our own ethhdr with optional vlan tags
47 struct ethhdr_vlan {
48         unsigned char   h_dest[ETH_ALEN];       /* destination eth addr */
49         unsigned char   h_source[ETH_ALEN];     /* source ether addr    */
50         __be16          vlan_magic;             /* 0x8100 */
51         __be16          tci;            /* PCP (3 bits), DEI (1 bit), and VLAN (12 bits) */
52         __be16          h_proto;                /* packet type ID field */
53 } __attribute__((packed));
54
55 // Our own tcphdr without the flags blown up
56 struct tcphdr {
57         __be16  source;
58         __be16  dest;
59         __be32  seq;
60         __be32  ack_seq;
61         __u16   flags;
62         __be16  window;
63         __sum16 check;
64         __be16  urg_ptr;
65 } __attribute__((packed));
66
67 // Note that all operations on uint128s *stay* in Network byte order!
68
69 #if defined(__LITTLE_ENDIAN)
70 #define BIGEND32(v) ((v >> 3*8) | ((v >> 8) & 0xff00) | ((v << 8) & 0xff0000) | (v << 3*8) & 0xff000000)
71 #elif defined(__BIG_ENDIAN)
72 #define BIGEND32(v) (v)
73 #else
74 #error "Need endian info"
75 #endif
76
77 #if defined(__LITTLE_ENDIAN)
78 #define BIGEND128(a, b, c, d) ( \
79                 (((uint128_t)BIGEND32(d)) << 3*32) | \
80                 (((uint128_t)BIGEND32(c)) << 2*32) | \
81                 (((uint128_t)BIGEND32(b)) << 1*32) | \
82                 (((uint128_t)BIGEND32(a)) << 0*32))
83 #define HTON128(a) BIGEND128(a >> 3*32, a >> 2*32, a >> 1*32, a>> 0*32)
84 // Yes, somehow macro'ing this changes LLVM's view of htons...
85 #define BE16(a) ((((uint16_t)(a & 0xff00)) >> 8) | (((uint16_t)(a & 0xff)) << 8))
86 #elif defined(__BIG_ENDIAN)
87 #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))
88 #define HTON128(a) (a)
89 #else
90 #error "Need endian info"
91 #endif
92
93 #define MASK4(pfxlen) BIGEND32(~((((uint32_t)1) << (32 - pfxlen)) - 1))
94 #define MASK6(pfxlen) HTON128(~((((uint128_t)1) << (128 - pfxlen)) - 1))
95 #define MASK6_OFFS(offs, pfxlen) HTON128((~((((uint128_t)1) << (128 - pfxlen)) - 1)) & ((((uint128_t)1) << (128 - offs)) - 1))
96
97 // Note rules.h may also define PARSE_8021Q and REQ_8021Q
98 // Note rules.h may also define PARSE_IHL
99 #include "rules.h"
100
101 #ifdef TEST
102 // 64 bit version of xdp_md for testing
103 struct xdp_md {
104         __u64 data;
105         __u64 data_end;
106         __u64 data_meta;
107         /* Below access go through struct xdp_rxq_info */
108         __u64 ingress_ifindex; /* rxq->dev->ifindex */
109         __u64 rx_queue_index;  /* rxq->queue_index  */
110
111         __u64 egress_ifindex;  /* txq->dev->ifindex */
112 };
113 static const int XDP_PASS = 0;
114 static const int XDP_DROP = 1;
115 #else
116 #include <linux/bpf.h>
117 #include <bpf/bpf_helpers.h>
118
119 SEC("xdp_drop")
120 #endif
121 int xdp_drop_prog(struct xdp_md *ctx)
122 {
123         const void *const data_end = (void *)(size_t)ctx->data_end;
124
125         const void * pktdata;
126         unsigned short eth_proto;
127
128         {
129                 if ((void*)(size_t)ctx->data + sizeof(struct ethhdr) > data_end)
130                         return XDP_DROP;
131                 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
132
133 #ifdef PARSE_8021Q
134                 if (eth->h_proto == BE16(ETH_P_8021Q)) {
135                         if ((void*)(size_t)ctx->data + sizeof(struct ethhdr_vlan) > data_end)
136                                 return XDP_DROP;
137                         const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
138
139 #ifdef REQ_8021Q
140                         if ((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q))
141                                 return XDP_DROP;
142 #endif
143
144                         eth_proto = eth_vlan->h_proto;
145                         pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
146                 } else {
147 #ifdef REQ_8021Q
148                         return XDP_DROP;
149 #else
150                         pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
151                         eth_proto = eth->h_proto;
152 #endif
153                 }
154 #else
155                 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
156                 eth_proto = eth->h_proto;
157 #endif
158         }
159
160         const struct tcphdr *tcp = NULL;
161         const struct udphdr *udp = NULL;
162         const struct icmphdr *icmp = NULL;
163         const struct icmp6hdr *icmpv6 = NULL;
164         const struct iphdr *ip = NULL;
165         const struct ip6hdr *ip6 = NULL;
166         const void *l4hdr = NULL;
167         if (eth_proto == BE16(ETH_P_IP)) {
168                 if (pktdata + sizeof(struct iphdr) > data_end)
169                         return XDP_DROP;
170                 ip = (struct iphdr*) pktdata;
171
172 #ifdef PARSE_IHL
173                 if (ip->ihl < 5) return XDP_DROP;
174                 l4hdr = pktdata + ip->ihl * 4;
175 #else
176                 if (ip->ihl != 5) return XDP_DROP;
177                 l4hdr = pktdata + 5*4;
178 #endif
179                 if (ip->protocol == IP_PROTO_TCP) {
180                         if (l4hdr + sizeof(struct tcphdr) > data_end)
181                                 return XDP_DROP;
182                         tcp = (struct tcphdr*) l4hdr;
183                 } else if (ip->protocol == IP_PROTO_UDP) {
184                         if (l4hdr + sizeof(struct udphdr) > data_end)
185                                 return XDP_DROP;
186                         udp = (struct udphdr*) l4hdr;
187                 } else if (ip->protocol == IP_PROTO_ICMP) {
188                         if (l4hdr + sizeof(struct icmphdr) > data_end)
189                                 return XDP_DROP;
190                         icmp = (struct icmphdr*) l4hdr;
191                 }
192         } else if (eth_proto == BE16(ETH_P_IPV6)) {
193                 if (pktdata + sizeof(struct ip6hdr) > data_end)
194                         return XDP_DROP;
195                 ip6 = (struct ip6hdr*) pktdata;
196
197                 l4hdr = pktdata + 40;
198                 if (ip6->nexthdr == IP_PROTO_TCP) {
199                         if (l4hdr + sizeof(struct tcphdr) > data_end)
200                                 return XDP_DROP;
201                         tcp = (struct tcphdr*) l4hdr;
202                 } else if (ip6->nexthdr == IP_PROTO_UDP) {
203                         if (l4hdr + sizeof(struct udphdr) > data_end)
204                                 return XDP_DROP;
205                         udp = (struct udphdr*) l4hdr;
206                 } else if (ip6->nexthdr == IP_PROTO_ICMPV6) {
207                         if (l4hdr + sizeof(struct icmp6hdr) > data_end)
208                                 return XDP_DROP;
209                         icmpv6 = (struct icmp6hdr*) l4hdr;
210                 }
211                 // TODO: Handle some options?
212         } else {
213                 return XDP_PASS;
214         }
215
216         uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
217         if (tcp != NULL) {
218                 sport = BE16(tcp->source);
219                 dport = BE16(tcp->dest);
220         } else if (udp != NULL) {
221                 sport = BE16(udp->source);
222                 dport = BE16(udp->dest);
223         }
224
225         RULES
226
227         return XDP_PASS;
228 }
229
230 #ifdef TEST
231 #include <assert.h>
232 #include <string.h>
233
234 const char d[] = TEST;
235 int main() {
236         struct xdp_md test = {
237                 .data = (uint64_t)d,
238                 // -1 because sizeof includes a trailing null in the "string"
239                 .data_end = (uint64_t)(d + sizeof(d) - 1),
240         };
241         assert(xdp_drop_prog(&test) == TEST_EXP);
242 }
243 #endif