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) (((((uint32_t)(v)) >> 3*8) & 0xff) | \
81 ((((uint32_t)(v)) >> 1*8) & 0xff00) | \
82 ((((uint32_t)(v)) << 1*8) & 0xff0000) | \
83 ((((uint32_t)(v)) << 3*8) & 0xff000000))
84 #elif defined(__BIG_ENDIAN)
85 #define BIGEND32(v) ((uint32_t)(v))
87 #error "Need endian info"
90 #if defined(__LITTLE_ENDIAN)
91 #define BIGEND128(a, b, c, d) ( \
92 (((uint128_t)BIGEND32(d)) << 3*32) | \
93 (((uint128_t)BIGEND32(c)) << 2*32) | \
94 (((uint128_t)BIGEND32(b)) << 1*32) | \
95 (((uint128_t)BIGEND32(a)) << 0*32))
96 #define HTON128(a) BIGEND128(a >> 3*32, a >> 2*32, a >> 1*32, a>> 0*32)
97 // Yes, somehow macro'ing this changes LLVM's view of htons...
98 #define BE16(a) (((((uint16_t)a) & 0xff00) >> 8) | ((((uint16_t)a) & 0xff) << 8))
99 #elif defined(__BIG_ENDIAN)
100 #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))
101 #define HTON128(a) ((uint128_t)(a))
102 #define BE16(a) ((uint16_t)(a))
104 #error "Need endian info"
107 #define MASK4(pfxlen) BIGEND32(~((((uint32_t)1) << (32 - pfxlen)) - 1))
108 #define MASK6(pfxlen) HTON128(~((((uint128_t)1) << (128 - pfxlen)) - 1))
109 #define MASK6_OFFS(offs, pfxlen) HTON128((~((((uint128_t)1) << (128 - pfxlen)) - 1)) & ((((uint128_t)1) << (128 - offs)) - 1))
111 // PARSE is used as a preprocessor flag to indicate parsing fields
115 #define unlikely(a) __builtin_expect(a, 0)
116 #define likely(a) __builtin_expect(a, 1)
118 static const uint32_t PKT_LEN_DROP = 0;
119 static const uint32_t VLAN_DROP = 1;
120 static const uint32_t IHL_DROP = 2;
121 static const uint32_t V6FRAG_DROP = 3;
122 #define STATIC_RULE_CNT 4
124 #define DO_RETURN(reason, ret) {\
125 if (ret == XDP_DROP) { INCREMENT_MATCH(reason); } \
129 // It seems (based on drop counts) that data_end points to the last byte, not one-past-the-end.
130 // This feels strange, but some documentation suggests > here as well, so we stick with that.
131 #define CHECK_LEN(start, struc) \
132 if (unlikely((void*)(start) + sizeof(struct struc) > data_end)) DO_RETURN(PKT_LEN_DROP, XDP_DROP);
135 // 64 bit version of xdp_md for testing
140 /* Below access go through struct xdp_rxq_info */
141 __u64 ingress_ifindex; /* rxq->dev->ifindex */
142 __u64 rx_queue_index; /* rxq->queue_index */
144 __u64 egress_ifindex; /* txq->dev->ifindex */
146 static const int XDP_PASS = 0;
147 static const int XDP_DROP = 1;
149 static long drop_cnt_map[RULECNT + STATIC_RULE_CNT];
150 #define INCREMENT_MATCH(reason) { drop_cnt_map[reason] += 1; drop_cnt_map[reason] += data_end - pktdata; }
153 #include <linux/bpf.h>
154 #include <bpf/bpf_helpers.h>
156 struct match_counter {
161 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
162 __uint(max_entries, RULECNT + STATIC_RULE_CNT);
164 struct match_counter *value;
165 } drop_cnt_map SEC(".maps");
167 #define INCREMENT_MATCH(reason) { \
168 struct match_counter *value = bpf_map_lookup_elem(&drop_cnt_map, &reason); \
170 value->bytes += data_end - pktdata; \
171 value->packets += 1; \
177 struct bpf_spin_lock lock;
182 __uint(type, BPF_MAP_TYPE_ARRAY);
183 __uint(max_entries, RATE_CNT);
185 struct ratelimit *value;
186 } rate_map SEC(".maps");
187 #endif /* RATE_CNT */
189 // For per-source rate limiting, we have to use per-CPU hash maps as Linux
190 // doesn't support spinlocks inside of a LRU_HASH (see if block in
191 // map_check_btf as of Linux 5.10).
192 // This isn't exactly accurate, but at least its faster.
193 struct percpu_ratelimit {
198 #define V6_SRC_RATE_DEFINE(n, limit) \
200 __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); \
201 __uint(map_flags, BPF_F_NO_COMMON_LRU); \
202 __uint(max_entries, limit); \
204 struct percpu_ratelimit *value; \
205 } v6_src_rate_##n SEC(".maps");
207 #define V4_SRC_RATE_DEFINE(n, limit) \
209 __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH); \
210 __uint(map_flags, BPF_F_NO_COMMON_LRU); \
211 __uint(max_entries, limit); \
213 struct percpu_ratelimit *value; \
214 } v4_src_rate_##n SEC(".maps");
218 #ifndef HAVE_WRAPPER // Set this to call xdp_drop externally
220 #endif /* HAVE_WRAPPER */
221 #endif /* not TEST */
222 int xdp_drop_prog(struct xdp_md *ctx)
224 const void *const data_end = (void *)(size_t)ctx->data_end;
226 const void * pktdata;
227 unsigned short eth_proto;
230 // DO_RETURN in CHECK_LEN relies on pktdata being set to calculate packet length.
231 // That said, we don't want to overflow, so just set packet length to 0 here.
233 CHECK_LEN((size_t)ctx->data, ethhdr);
234 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
235 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
237 #if PARSE_8021Q == PARSE
238 if (likely(eth->h_proto == BE16(ETH_P_8021Q))) {
239 CHECK_LEN((size_t)ctx->data, ethhdr_vlan);
240 const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
241 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
243 if (unlikely((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q)))
244 DO_RETURN(VLAN_DROP, XDP_DROP);
246 eth_proto = eth_vlan->h_proto;
248 if (unlikely(eth->h_proto == BE16(ETH_P_8021Q))) {
249 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
250 DO_RETURN(VLAN_DROP, PARSE_8021Q);
254 DO_RETURN(VLAN_DROP, XDP_DROP);
256 eth_proto = eth->h_proto;
261 const void *l4hdr = NULL;
262 const struct tcphdr *tcp = NULL;
263 int32_t sport = -1, dport = -1; // Host Endian! Only valid with tcp || udp
266 if (eth_proto == BE16(ETH_P_IP)) {
267 CHECK_LEN(pktdata, iphdr);
268 struct iphdr *ip = (struct iphdr*) pktdata;
270 #if PARSE_IHL == PARSE
271 if (unlikely(ip->ihl < 5)) DO_RETURN(IHL_DROP, XDP_DROP);
272 l4hdr = pktdata + ip->ihl * 4;
274 if (ip->ihl != 5) DO_RETURN(IHL_DROP, PARSE_IHL);
275 l4hdr = pktdata + 5*4;
278 const struct icmphdr *icmp = NULL;
279 if ((ip->frag_off & BE16(IP_OFFSET)) == 0) {
280 if (ip->protocol == IP_PROTO_TCP) {
281 CHECK_LEN(l4hdr, tcphdr);
282 tcp = (struct tcphdr*) l4hdr;
283 sport = BE16(tcp->source);
284 dport = BE16(tcp->dest);
285 } else if (ip->protocol == IP_PROTO_UDP) {
286 CHECK_LEN(l4hdr, udphdr);
287 const struct udphdr *udp = (struct udphdr*) l4hdr;
288 sport = BE16(udp->source);
289 dport = BE16(udp->dest);
290 } else if (ip->protocol == IP_PROTO_ICMP) {
291 CHECK_LEN(l4hdr, icmphdr);
292 icmp = (struct icmphdr*) l4hdr;
300 if (eth_proto == BE16(ETH_P_IPV6)) {
301 CHECK_LEN(pktdata, ip6hdr);
302 struct ip6hdr *ip6 = (struct ip6hdr*) pktdata;
304 l4hdr = pktdata + 40;
306 uint8_t v6nexthdr = ip6->nexthdr;
307 const struct ip6_fraghdr *frag6 = NULL;
309 #if PARSE_V6_FRAG == PARSE
310 if (ip6->nexthdr == IP6_PROTO_FRAG) {
311 CHECK_LEN(l4hdr, ip6_fraghdr);
312 frag6 = (struct ip6_fraghdr*) l4hdr;
313 l4hdr = l4hdr + sizeof(struct ip6_fraghdr);
314 v6nexthdr = frag6->nexthdr;
316 if (unlikely(ip6->nexthdr == IP6_PROTO_FRAG)) {
317 DO_RETURN(V6FRAG_DROP, PARSE_V6_FRAG);
321 // TODO: Handle more options?
323 const struct icmp6hdr *icmpv6 = NULL;
324 if (frag6 == NULL || (frag6->frag_off & BE16(IP6_FRAGOFF)) == 0) {
325 if (v6nexthdr == IP_PROTO_TCP) {
326 CHECK_LEN(l4hdr, tcphdr);
327 tcp = (struct tcphdr*) l4hdr;
328 sport = BE16(tcp->source);
329 dport = BE16(tcp->dest);
330 } else if (v6nexthdr == IP_PROTO_UDP) {
331 CHECK_LEN(l4hdr, udphdr);
332 const struct udphdr *udp = (struct udphdr*) l4hdr;
333 sport = BE16(udp->source);
334 dport = BE16(udp->dest);
335 } else if (v6nexthdr == IP6_PROTO_ICMPV6) {
336 CHECK_LEN(l4hdr, icmp6hdr);
337 icmpv6 = (struct icmp6hdr*) l4hdr;
354 struct xdp_md test = {
356 // -1 because sizeof includes a trailing null in the "string"
357 .data_end = (uint64_t)(d + sizeof(d) - 1),
359 assert(xdp_drop_prog(&test) == TEST_EXP);