3 #include <linux/if_ether.h>
6 #include <linux/icmp.h>
7 #include <linux/icmpv6.h>
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 */
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
24 typedef __uint128_t uint128_t;
26 // Our own ipv6hdr that uses uint128_t
28 #if defined(__LITTLE_ENDIAN_BITFIELD)
31 #elif defined(__BIG_ENDIAN_BITFIELD)
35 #error "Please fix <asm/byteorder.h>"
45 } __attribute__((packed));
48 #define IP6_FRAGOFF 0xfff8
52 uint16_t frag_off; // BE low 3 bits flags, last is "more frags"
54 } __attribute__((packed));
56 // Our own ethhdr with optional vlan tags
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));
65 // Our own tcphdr without the flags blown up
75 } __attribute__((packed));
77 // Note that all operations on uint128s *stay* in Network byte order!
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)
84 #error "Need endian info"
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)
101 #error "Need endian info"
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))
108 // PARSE is used as a preprocessor flag to indicate parsing fields
112 #define unlikely(a) __builtin_expect(a, 0)
113 #define likely(a) __builtin_expect(a, 1)
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
121 #define DO_RETURN(reason, ret) {\
122 if (ret == XDP_DROP) { INCREMENT_MATCH(reason); } \
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);
132 // 64 bit version of xdp_md for testing
137 /* Below access go through struct xdp_rxq_info */
138 __u64 ingress_ifindex; /* rxq->dev->ifindex */
139 __u64 rx_queue_index; /* rxq->queue_index */
141 __u64 egress_ifindex; /* txq->dev->ifindex */
143 static const int XDP_PASS = 0;
144 static const int XDP_DROP = 1;
146 static long drop_cnt_map[RULECNT + STATIC_RULE_CNT];
147 #define INCREMENT_MATCH(reason) { drop_cnt_map[reason] += 1; drop_cnt_map[reason] += data_end - pktdata; }
150 #include <linux/bpf.h>
151 #include <bpf/bpf_helpers.h>
153 struct match_counter {
158 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
159 __uint(max_entries, RULECNT + STATIC_RULE_CNT);
161 struct match_counter *value;
162 } drop_cnt_map SEC(".maps");
164 #define INCREMENT_MATCH(reason) { \
165 struct match_counter *value = bpf_map_lookup_elem(&drop_cnt_map, &reason); \
167 value->bytes += data_end - pktdata; \
168 value->packets += 1; \
174 struct bpf_spin_lock lock;
177 int64_t sent_packets;
182 __uint(type, BPF_MAP_TYPE_ARRAY);
183 __uint(max_entries, RATE_CNT);
185 struct ratelimit *value;
186 } rate_map SEC(".maps");
191 int xdp_drop_prog(struct xdp_md *ctx)
193 const void *const data_end = (void *)(size_t)ctx->data_end;
195 const void * pktdata;
196 unsigned short eth_proto;
199 // DO_RETURN in CHECK_LEN relies on pktdata being set to calculate packet length.
200 // That said, we don't want to overflow, so just set packet length to 0 here.
202 CHECK_LEN((size_t)ctx->data, ethhdr);
203 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
204 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
206 #if PARSE_8021Q == PARSE
207 if (likely(eth->h_proto == BE16(ETH_P_8021Q))) {
208 CHECK_LEN((size_t)ctx->data, ethhdr_vlan);
209 const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
210 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
212 if (unlikely((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q)))
213 DO_RETURN(VLAN_DROP, XDP_DROP);
215 eth_proto = eth_vlan->h_proto;
217 if (unlikely(eth->h_proto == BE16(ETH_P_8021Q))) {
218 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
219 DO_RETURN(VLAN_DROP, PARSE_8021Q);
223 DO_RETURN(VLAN_DROP, XDP_DROP);
225 eth_proto = eth->h_proto;
230 const void *l4hdr = NULL;
231 const struct tcphdr *tcp = NULL;
232 uint8_t ports_valid = 0;
233 uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
236 if (eth_proto == BE16(ETH_P_IP)) {
237 CHECK_LEN(pktdata, iphdr);
238 struct iphdr *ip = (struct iphdr*) pktdata;
240 #if PARSE_IHL == PARSE
241 if (unlikely(ip->ihl < 5)) DO_RETURN(IHL_DROP, XDP_DROP);
242 l4hdr = pktdata + ip->ihl * 4;
244 if (ip->ihl != 5) DO_RETURN(IHL_DROP, PARSE_IHL);
245 l4hdr = pktdata + 5*4;
248 const struct icmphdr *icmp = NULL;
249 if ((ip->frag_off & BE16(IP_OFFSET)) == 0) {
250 if (ip->protocol == IP_PROTO_TCP) {
251 CHECK_LEN(l4hdr, tcphdr);
252 tcp = (struct tcphdr*) l4hdr;
253 sport = BE16(tcp->source);
254 dport = BE16(tcp->dest);
256 } else if (ip->protocol == IP_PROTO_UDP) {
257 CHECK_LEN(l4hdr, udphdr);
258 const struct udphdr *udp = (struct udphdr*) l4hdr;
259 sport = BE16(udp->source);
260 dport = BE16(udp->dest);
262 } else if (ip->protocol == IP_PROTO_ICMP) {
263 CHECK_LEN(l4hdr, icmphdr);
264 icmp = (struct icmphdr*) l4hdr;
272 if (eth_proto == BE16(ETH_P_IPV6)) {
273 CHECK_LEN(pktdata, ip6hdr);
274 struct ip6hdr *ip6 = (struct ip6hdr*) pktdata;
276 l4hdr = pktdata + 40;
278 uint8_t v6nexthdr = ip6->nexthdr;
279 const struct ip6_fraghdr *frag6 = NULL;
281 #if PARSE_V6_FRAG == PARSE
282 if (ip6->nexthdr == IP6_PROTO_FRAG) {
283 CHECK_LEN(l4hdr, ip6_fraghdr);
284 frag6 = (struct ip6_fraghdr*) l4hdr;
285 l4hdr = l4hdr + sizeof(struct ip6_fraghdr);
286 v6nexthdr = frag6->nexthdr;
288 if (unlikely(ip6->nexthdr == IP6_PROTO_FRAG)) {
289 DO_RETURN(V6FRAG_DROP, PARSE_V6_FRAG);
293 // TODO: Handle more options?
295 const struct icmp6hdr *icmpv6 = NULL;
296 if (frag6 == NULL || (frag6->frag_off & BE16(IP6_FRAGOFF)) == 0) {
297 if (v6nexthdr == IP_PROTO_TCP) {
298 CHECK_LEN(l4hdr, tcphdr);
299 tcp = (struct tcphdr*) l4hdr;
300 sport = BE16(tcp->source);
301 dport = BE16(tcp->dest);
303 } else if (v6nexthdr == IP_PROTO_UDP) {
304 CHECK_LEN(l4hdr, udphdr);
305 const struct udphdr *udp = (struct udphdr*) l4hdr;
306 sport = BE16(udp->source);
307 dport = BE16(udp->dest);
309 } else if (v6nexthdr == IP6_PROTO_ICMPV6) {
310 CHECK_LEN(l4hdr, icmp6hdr);
311 icmpv6 = (struct icmp6hdr*) l4hdr;
328 struct xdp_md test = {
330 // -1 because sizeof includes a trailing null in the "string"
331 .data_end = (uint64_t)(d + sizeof(d) - 1),
333 assert(xdp_drop_prog(&test) == TEST_EXP);