1 // SPDX-License-Identifier: GPL-2.0
2 #include <limits.h>
3 #include <stddef.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <linux/pkt_cls.h>
7 #include <linux/bpf.h>
8 #include <linux/in.h>
9 #include <linux/if_ether.h>
10 #include <linux/icmp.h>
11 #include <linux/ip.h>
12 #include <linux/ipv6.h>
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
15 #include <linux/if_packet.h>
16 #include <sys/socket.h>
17 #include <linux/if_tunnel.h>
18 #include <linux/mpls.h>
19 #include "bpf_helpers.h"
20 #include "bpf_endian.h"
21 
22 int _version SEC("version") = 1;
23 #define PROG(F) SEC(#F) int bpf_func_##F
24 
25 /* These are the identifiers of the BPF programs that will be used in tail
26  * calls. Name is limited to 16 characters, with the terminating character and
27  * bpf_func_ above, we have only 6 to work with, anything after will be cropped.
28  */
29 enum {
30 	IP,
31 	IPV6,
32 	IPV6OP,	/* Destination/Hop-by-Hop Options IPv6 Extension header */
33 	IPV6FR,	/* Fragmentation IPv6 Extension Header */
34 	MPLS,
35 	VLAN,
36 };
37 
38 #define IP_MF		0x2000
39 #define IP_OFFSET	0x1FFF
40 #define IP6_MF		0x0001
41 #define IP6_OFFSET	0xFFF8
42 
43 struct vlan_hdr {
44 	__be16 h_vlan_TCI;
45 	__be16 h_vlan_encapsulated_proto;
46 };
47 
48 struct gre_hdr {
49 	__be16 flags;
50 	__be16 proto;
51 };
52 
53 struct frag_hdr {
54 	__u8 nexthdr;
55 	__u8 reserved;
56 	__be16 frag_off;
57 	__be32 identification;
58 };
59 
60 struct bpf_map_def SEC("maps") jmp_table = {
61 	.type = BPF_MAP_TYPE_PROG_ARRAY,
62 	.key_size = sizeof(__u32),
63 	.value_size = sizeof(__u32),
64 	.max_entries = 8
65 };
66 
67 static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb,
68 							 __u16 hdr_size,
69 							 void *buffer)
70 {
71 	void *data_end = (void *)(long)skb->data_end;
72 	void *data = (void *)(long)skb->data;
73 	__u16 thoff = skb->flow_keys->thoff;
74 	__u8 *hdr;
75 
76 	/* Verifies this variable offset does not overflow */
77 	if (thoff > (USHRT_MAX - hdr_size))
78 		return NULL;
79 
80 	hdr = data + thoff;
81 	if (hdr + hdr_size <= data_end)
82 		return hdr;
83 
84 	if (bpf_skb_load_bytes(skb, thoff, buffer, hdr_size))
85 		return NULL;
86 
87 	return buffer;
88 }
89 
90 /* Dispatches on ETHERTYPE */
91 static __always_inline int parse_eth_proto(struct __sk_buff *skb, __be16 proto)
92 {
93 	struct bpf_flow_keys *keys = skb->flow_keys;
94 
95 	switch (proto) {
96 	case bpf_htons(ETH_P_IP):
97 		bpf_tail_call(skb, &jmp_table, IP);
98 		break;
99 	case bpf_htons(ETH_P_IPV6):
100 		bpf_tail_call(skb, &jmp_table, IPV6);
101 		break;
102 	case bpf_htons(ETH_P_MPLS_MC):
103 	case bpf_htons(ETH_P_MPLS_UC):
104 		bpf_tail_call(skb, &jmp_table, MPLS);
105 		break;
106 	case bpf_htons(ETH_P_8021Q):
107 	case bpf_htons(ETH_P_8021AD):
108 		bpf_tail_call(skb, &jmp_table, VLAN);
109 		break;
110 	default:
111 		/* Protocol not supported */
112 		return BPF_DROP;
113 	}
114 
115 	return BPF_DROP;
116 }
117 
118 SEC("flow_dissector")
119 int _dissect(struct __sk_buff *skb)
120 {
121 	struct bpf_flow_keys *keys = skb->flow_keys;
122 
123 	return parse_eth_proto(skb, keys->n_proto);
124 }
125 
126 /* Parses on IPPROTO_* */
127 static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto)
128 {
129 	struct bpf_flow_keys *keys = skb->flow_keys;
130 	void *data_end = (void *)(long)skb->data_end;
131 	struct icmphdr *icmp, _icmp;
132 	struct gre_hdr *gre, _gre;
133 	struct ethhdr *eth, _eth;
134 	struct tcphdr *tcp, _tcp;
135 	struct udphdr *udp, _udp;
136 
137 	keys->ip_proto = proto;
138 	switch (proto) {
139 	case IPPROTO_ICMP:
140 		icmp = bpf_flow_dissect_get_header(skb, sizeof(*icmp), &_icmp);
141 		if (!icmp)
142 			return BPF_DROP;
143 		return BPF_OK;
144 	case IPPROTO_IPIP:
145 		keys->is_encap = true;
146 		return parse_eth_proto(skb, bpf_htons(ETH_P_IP));
147 	case IPPROTO_IPV6:
148 		keys->is_encap = true;
149 		return parse_eth_proto(skb, bpf_htons(ETH_P_IPV6));
150 	case IPPROTO_GRE:
151 		gre = bpf_flow_dissect_get_header(skb, sizeof(*gre), &_gre);
152 		if (!gre)
153 			return BPF_DROP;
154 
155 		if (bpf_htons(gre->flags & GRE_VERSION))
156 			/* Only inspect standard GRE packets with version 0 */
157 			return BPF_OK;
158 
159 		keys->thoff += sizeof(*gre); /* Step over GRE Flags and Proto */
160 		if (GRE_IS_CSUM(gre->flags))
161 			keys->thoff += 4; /* Step over chksum and Padding */
162 		if (GRE_IS_KEY(gre->flags))
163 			keys->thoff += 4; /* Step over key */
164 		if (GRE_IS_SEQ(gre->flags))
165 			keys->thoff += 4; /* Step over sequence number */
166 
167 		keys->is_encap = true;
168 
169 		if (gre->proto == bpf_htons(ETH_P_TEB)) {
170 			eth = bpf_flow_dissect_get_header(skb, sizeof(*eth),
171 							  &_eth);
172 			if (!eth)
173 				return BPF_DROP;
174 
175 			keys->thoff += sizeof(*eth);
176 
177 			return parse_eth_proto(skb, eth->h_proto);
178 		} else {
179 			return parse_eth_proto(skb, gre->proto);
180 		}
181 	case IPPROTO_TCP:
182 		tcp = bpf_flow_dissect_get_header(skb, sizeof(*tcp), &_tcp);
183 		if (!tcp)
184 			return BPF_DROP;
185 
186 		if (tcp->doff < 5)
187 			return BPF_DROP;
188 
189 		if ((__u8 *)tcp + (tcp->doff << 2) > data_end)
190 			return BPF_DROP;
191 
192 		keys->sport = tcp->source;
193 		keys->dport = tcp->dest;
194 		return BPF_OK;
195 	case IPPROTO_UDP:
196 	case IPPROTO_UDPLITE:
197 		udp = bpf_flow_dissect_get_header(skb, sizeof(*udp), &_udp);
198 		if (!udp)
199 			return BPF_DROP;
200 
201 		keys->sport = udp->source;
202 		keys->dport = udp->dest;
203 		return BPF_OK;
204 	default:
205 		return BPF_DROP;
206 	}
207 
208 	return BPF_DROP;
209 }
210 
211 static __always_inline int parse_ipv6_proto(struct __sk_buff *skb, __u8 nexthdr)
212 {
213 	struct bpf_flow_keys *keys = skb->flow_keys;
214 
215 	keys->ip_proto = nexthdr;
216 	switch (nexthdr) {
217 	case IPPROTO_HOPOPTS:
218 	case IPPROTO_DSTOPTS:
219 		bpf_tail_call(skb, &jmp_table, IPV6OP);
220 		break;
221 	case IPPROTO_FRAGMENT:
222 		bpf_tail_call(skb, &jmp_table, IPV6FR);
223 		break;
224 	default:
225 		return parse_ip_proto(skb, nexthdr);
226 	}
227 
228 	return BPF_DROP;
229 }
230 
231 PROG(IP)(struct __sk_buff *skb)
232 {
233 	void *data_end = (void *)(long)skb->data_end;
234 	struct bpf_flow_keys *keys = skb->flow_keys;
235 	void *data = (void *)(long)skb->data;
236 	struct iphdr *iph, _iph;
237 	bool done = false;
238 
239 	iph = bpf_flow_dissect_get_header(skb, sizeof(*iph), &_iph);
240 	if (!iph)
241 		return BPF_DROP;
242 
243 	/* IP header cannot be smaller than 20 bytes */
244 	if (iph->ihl < 5)
245 		return BPF_DROP;
246 
247 	keys->addr_proto = ETH_P_IP;
248 	keys->ipv4_src = iph->saddr;
249 	keys->ipv4_dst = iph->daddr;
250 
251 	keys->thoff += iph->ihl << 2;
252 	if (data + keys->thoff > data_end)
253 		return BPF_DROP;
254 
255 	if (iph->frag_off & bpf_htons(IP_MF | IP_OFFSET)) {
256 		keys->is_frag = true;
257 		if (iph->frag_off & bpf_htons(IP_OFFSET))
258 			/* From second fragment on, packets do not have headers
259 			 * we can parse.
260 			 */
261 			done = true;
262 		else
263 			keys->is_first_frag = true;
264 	}
265 
266 	if (done)
267 		return BPF_OK;
268 
269 	return parse_ip_proto(skb, iph->protocol);
270 }
271 
272 PROG(IPV6)(struct __sk_buff *skb)
273 {
274 	struct bpf_flow_keys *keys = skb->flow_keys;
275 	struct ipv6hdr *ip6h, _ip6h;
276 
277 	ip6h = bpf_flow_dissect_get_header(skb, sizeof(*ip6h), &_ip6h);
278 	if (!ip6h)
279 		return BPF_DROP;
280 
281 	keys->addr_proto = ETH_P_IPV6;
282 	memcpy(&keys->ipv6_src, &ip6h->saddr, 2*sizeof(ip6h->saddr));
283 
284 	keys->thoff += sizeof(struct ipv6hdr);
285 
286 	return parse_ipv6_proto(skb, ip6h->nexthdr);
287 }
288 
289 PROG(IPV6OP)(struct __sk_buff *skb)
290 {
291 	struct ipv6_opt_hdr *ip6h, _ip6h;
292 
293 	ip6h = bpf_flow_dissect_get_header(skb, sizeof(*ip6h), &_ip6h);
294 	if (!ip6h)
295 		return BPF_DROP;
296 
297 	/* hlen is in 8-octets and does not include the first 8 bytes
298 	 * of the header
299 	 */
300 	skb->flow_keys->thoff += (1 + ip6h->hdrlen) << 3;
301 
302 	return parse_ipv6_proto(skb, ip6h->nexthdr);
303 }
304 
305 PROG(IPV6FR)(struct __sk_buff *skb)
306 {
307 	struct bpf_flow_keys *keys = skb->flow_keys;
308 	struct frag_hdr *fragh, _fragh;
309 
310 	fragh = bpf_flow_dissect_get_header(skb, sizeof(*fragh), &_fragh);
311 	if (!fragh)
312 		return BPF_DROP;
313 
314 	keys->thoff += sizeof(*fragh);
315 	keys->is_frag = true;
316 	if (!(fragh->frag_off & bpf_htons(IP6_OFFSET)))
317 		keys->is_first_frag = true;
318 
319 	return parse_ipv6_proto(skb, fragh->nexthdr);
320 }
321 
322 PROG(MPLS)(struct __sk_buff *skb)
323 {
324 	struct mpls_label *mpls, _mpls;
325 
326 	mpls = bpf_flow_dissect_get_header(skb, sizeof(*mpls), &_mpls);
327 	if (!mpls)
328 		return BPF_DROP;
329 
330 	return BPF_OK;
331 }
332 
333 PROG(VLAN)(struct __sk_buff *skb)
334 {
335 	struct bpf_flow_keys *keys = skb->flow_keys;
336 	struct vlan_hdr *vlan, _vlan;
337 
338 	/* Account for double-tagging */
339 	if (keys->n_proto == bpf_htons(ETH_P_8021AD)) {
340 		vlan = bpf_flow_dissect_get_header(skb, sizeof(*vlan), &_vlan);
341 		if (!vlan)
342 			return BPF_DROP;
343 
344 		if (vlan->h_vlan_encapsulated_proto != bpf_htons(ETH_P_8021Q))
345 			return BPF_DROP;
346 
347 		keys->nhoff += sizeof(*vlan);
348 		keys->thoff += sizeof(*vlan);
349 	}
350 
351 	vlan = bpf_flow_dissect_get_header(skb, sizeof(*vlan), &_vlan);
352 	if (!vlan)
353 		return BPF_DROP;
354 
355 	keys->nhoff += sizeof(*vlan);
356 	keys->thoff += sizeof(*vlan);
357 	/* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/
358 	if (vlan->h_vlan_encapsulated_proto == bpf_htons(ETH_P_8021AD) ||
359 	    vlan->h_vlan_encapsulated_proto == bpf_htons(ETH_P_8021Q))
360 		return BPF_DROP;
361 
362 	keys->n_proto = vlan->h_vlan_encapsulated_proto;
363 	return parse_eth_proto(skb, vlan->h_vlan_encapsulated_proto);
364 }
365 
366 char __license[] SEC("license") = "GPL";
367