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 IP_PROTO_ICMPV6 58
23 typedef __uint128_t uint128_t;
25 // Our own ipv6hdr that uses uint128_t
27 #if defined(__LITTLE_ENDIAN_BITFIELD)
30 #elif defined(__BIG_ENDIAN_BITFIELD)
34 #error "Please fix <asm/byteorder.h>"
44 } __attribute__((packed));
46 // Our own ethhdr with optional vlan tags
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));
55 // Our own tcphdr without the flags blown up
65 } __attribute__((packed));
67 // Note that all operations on uint128s *stay* in Network byte order!
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)
74 #error "Need endian info"
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)
90 #error "Need endian info"
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))
97 // Note rules.h may also define PARSE_8021Q and REQ_8021Q
98 // Note rules.h may also define PARSE_IHL
102 // 64 bit version of xdp_md for testing
107 /* Below access go through struct xdp_rxq_info */
108 __u64 ingress_ifindex; /* rxq->dev->ifindex */
109 __u64 rx_queue_index; /* rxq->queue_index */
111 __u64 egress_ifindex; /* txq->dev->ifindex */
113 static const int XDP_PASS = 0;
114 static const int XDP_DROP = 1;
116 #include <linux/bpf.h>
117 #include <bpf/bpf_helpers.h>
121 int xdp_drop_prog(struct xdp_md *ctx)
123 const void *const data_end = (void *)(size_t)ctx->data_end;
125 const void * pktdata;
126 unsigned short eth_proto;
129 if ((void*)(size_t)ctx->data + sizeof(struct ethhdr) > data_end)
131 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
134 if (eth->h_proto == BE16(ETH_P_8021Q)) {
135 if ((void*)(size_t)ctx->data + sizeof(struct ethhdr_vlan) > data_end)
137 const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
140 if ((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q))
144 eth_proto = eth_vlan->h_proto;
145 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
150 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
151 eth_proto = eth->h_proto;
155 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
156 eth_proto = eth->h_proto;
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)
170 ip = (struct iphdr*) pktdata;
173 if (ip->ihl < 5) return XDP_DROP;
174 l4hdr = pktdata + ip->ihl * 4;
176 if (ip->ihl != 5) return XDP_DROP;
177 l4hdr = pktdata + 5*4;
179 if (ip->protocol == IP_PROTO_TCP) {
180 if (l4hdr + sizeof(struct tcphdr) > data_end)
182 tcp = (struct tcphdr*) l4hdr;
183 } else if (ip->protocol == IP_PROTO_UDP) {
184 if (l4hdr + sizeof(struct udphdr) > data_end)
186 udp = (struct udphdr*) l4hdr;
187 } else if (ip->protocol == IP_PROTO_ICMP) {
188 if (l4hdr + sizeof(struct icmphdr) > data_end)
190 icmp = (struct icmphdr*) l4hdr;
192 } else if (eth_proto == BE16(ETH_P_IPV6)) {
193 if (pktdata + sizeof(struct ip6hdr) > data_end)
195 ip6 = (struct ip6hdr*) pktdata;
197 l4hdr = pktdata + 40;
198 if (ip6->nexthdr == IP_PROTO_TCP) {
199 if (l4hdr + sizeof(struct tcphdr) > data_end)
201 tcp = (struct tcphdr*) l4hdr;
202 } else if (ip6->nexthdr == IP_PROTO_UDP) {
203 if (l4hdr + sizeof(struct udphdr) > data_end)
205 udp = (struct udphdr*) l4hdr;
206 } else if (ip6->nexthdr == IP_PROTO_ICMPV6) {
207 if (l4hdr + sizeof(struct icmp6hdr) > data_end)
209 icmpv6 = (struct icmp6hdr*) l4hdr;
211 // TODO: Handle some options?
216 uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
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);
234 const char d[] = TEST;
236 struct xdp_md test = {
238 // -1 because sizeof includes a trailing null in the "string"
239 .data_end = (uint64_t)(d + sizeof(d) - 1),
241 assert(xdp_drop_prog(&test) == TEST_EXP);