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)
100 #error "Need endian info"
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))
107 // Note rules.h may also define PARSE_8021Q and REQ_8021Q
108 // Note rules.h may also define PARSE_IHL
112 // 64 bit version of xdp_md for testing
117 /* Below access go through struct xdp_rxq_info */
118 __u64 ingress_ifindex; /* rxq->dev->ifindex */
119 __u64 rx_queue_index; /* rxq->queue_index */
121 __u64 egress_ifindex; /* txq->dev->ifindex */
123 static const int XDP_PASS = 0;
124 static const int XDP_DROP = 1;
126 #include <linux/bpf.h>
127 #include <bpf/bpf_helpers.h>
131 int xdp_drop_prog(struct xdp_md *ctx)
133 const void *const data_end = (void *)(size_t)ctx->data_end;
135 const void * pktdata;
136 unsigned short eth_proto;
139 if ((void*)(size_t)ctx->data + sizeof(struct ethhdr) > data_end)
141 const struct ethhdr *const eth = (void*)(size_t)ctx->data;
144 if (eth->h_proto == BE16(ETH_P_8021Q)) {
145 if ((void*)(size_t)ctx->data + sizeof(struct ethhdr_vlan) > data_end)
147 const struct ethhdr_vlan *const eth_vlan = (void*)(size_t)ctx->data;
150 if ((eth_vlan->tci & BE16(0xfff)) != BE16(REQ_8021Q))
154 eth_proto = eth_vlan->h_proto;
155 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr_vlan);
160 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
161 eth_proto = eth->h_proto;
165 pktdata = (const void *)(long)ctx->data + sizeof(struct ethhdr);
166 eth_proto = eth->h_proto;
170 const struct tcphdr *tcp = NULL;
171 const struct udphdr *udp = NULL;
172 const struct icmphdr *icmp = NULL;
173 const struct icmp6hdr *icmpv6 = NULL;
174 const struct ip6_fraghdr *frag6 = NULL;
175 const struct iphdr *ip = NULL;
176 const struct ip6hdr *ip6 = NULL;
177 const void *l4hdr = NULL;
178 if (eth_proto == BE16(ETH_P_IP)) {
179 if (pktdata + sizeof(struct iphdr) > data_end)
181 ip = (struct iphdr*) pktdata;
184 if (ip->ihl < 5) return XDP_DROP;
185 l4hdr = pktdata + ip->ihl * 4;
187 if (ip->ihl != 5) return XDP_DROP;
188 l4hdr = pktdata + 5*4;
190 if (ip->protocol == IP_PROTO_TCP) {
191 if (l4hdr + sizeof(struct tcphdr) > data_end)
193 tcp = (struct tcphdr*) l4hdr;
194 } else if (ip->protocol == IP_PROTO_UDP) {
195 if (l4hdr + sizeof(struct udphdr) > data_end)
197 udp = (struct udphdr*) l4hdr;
198 } else if (ip->protocol == IP_PROTO_ICMP) {
199 if (l4hdr + sizeof(struct icmphdr) > data_end)
201 icmp = (struct icmphdr*) l4hdr;
203 } else if (eth_proto == BE16(ETH_P_IPV6)) {
204 if (pktdata + sizeof(struct ip6hdr) > data_end)
206 ip6 = (struct ip6hdr*) pktdata;
208 l4hdr = pktdata + 40;
211 if (ip6->nexthdr == IP6_PROTO_FRAG) {
212 if (l4hdr + sizeof(struct ip6_fraghdr) > data_end)
214 frag6 = (struct ip6_fraghdr*) l4hdr;
215 l4hdr = l4hdr + sizeof(struct ip6_fraghdr);
216 v6nexthdr = frag6->nexthdr;
218 v6nexthdr = ip6->nexthdr;
221 if (v6nexthdr == IP_PROTO_TCP) {
222 if (l4hdr + sizeof(struct tcphdr) > data_end)
224 tcp = (struct tcphdr*) l4hdr;
225 } else if (v6nexthdr == IP_PROTO_UDP) {
226 if (l4hdr + sizeof(struct udphdr) > data_end)
228 udp = (struct udphdr*) l4hdr;
229 } else if (v6nexthdr == IP6_PROTO_ICMPV6) {
230 if (l4hdr + sizeof(struct icmp6hdr) > data_end)
232 icmpv6 = (struct icmp6hdr*) l4hdr;
234 // TODO: Handle some options?
239 uint16_t sport, dport; // Host Endian! Only valid with tcp || udp
241 sport = BE16(tcp->source);
242 dport = BE16(tcp->dest);
243 } else if (udp != NULL) {
244 sport = BE16(udp->source);
245 dport = BE16(udp->dest);
257 const char d[] = TEST;
259 struct xdp_md test = {
261 // -1 because sizeof includes a trailing null in the "string"
262 .data_end = (uint64_t)(d + sizeof(d) - 1),
264 assert(xdp_drop_prog(&test) == TEST_EXP);