1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * IPv6 fragment reassembly for connection tracking
4  *
5  * Copyright (C)2004 USAGI/WIDE Project
6  *
7  * Author:
8  *	Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9  *
10  * Based on: net/ipv6/reassembly.c
11  */
12 
13 #define pr_fmt(fmt) "IPv6-nf: " fmt
14 
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 #include <linux/net.h>
19 #include <linux/netdevice.h>
20 #include <linux/ipv6.h>
21 #include <linux/slab.h>
22 
23 #include <net/ipv6_frag.h>
24 
25 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
26 #include <linux/sysctl.h>
27 #include <linux/netfilter.h>
28 #include <linux/netfilter_ipv6.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
32 #include <net/netns/generic.h>
33 
34 static const char nf_frags_cache_name[] = "nf-frags";
35 
36 static unsigned int nf_frag_pernet_id __read_mostly;
37 static struct inet_frags nf_frags;
38 
39 static struct nft_ct_frag6_pernet *nf_frag_pernet(struct net *net)
40 {
41 	return net_generic(net, nf_frag_pernet_id);
42 }
43 
44 #ifdef CONFIG_SYSCTL
45 
46 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
47 	{
48 		.procname	= "nf_conntrack_frag6_timeout",
49 		.maxlen		= sizeof(unsigned int),
50 		.mode		= 0644,
51 		.proc_handler	= proc_dointvec_jiffies,
52 	},
53 	{
54 		.procname	= "nf_conntrack_frag6_low_thresh",
55 		.maxlen		= sizeof(unsigned long),
56 		.mode		= 0644,
57 		.proc_handler	= proc_doulongvec_minmax,
58 	},
59 	{
60 		.procname	= "nf_conntrack_frag6_high_thresh",
61 		.maxlen		= sizeof(unsigned long),
62 		.mode		= 0644,
63 		.proc_handler	= proc_doulongvec_minmax,
64 	},
65 	{ }
66 };
67 
68 static int nf_ct_frag6_sysctl_register(struct net *net)
69 {
70 	struct nft_ct_frag6_pernet *nf_frag;
71 	struct ctl_table *table;
72 	struct ctl_table_header *hdr;
73 
74 	table = nf_ct_frag6_sysctl_table;
75 	if (!net_eq(net, &init_net)) {
76 		table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
77 				GFP_KERNEL);
78 		if (table == NULL)
79 			goto err_alloc;
80 	}
81 
82 	nf_frag = nf_frag_pernet(net);
83 
84 	table[0].data	= &nf_frag->fqdir->timeout;
85 	table[1].data	= &nf_frag->fqdir->low_thresh;
86 	table[1].extra2	= &nf_frag->fqdir->high_thresh;
87 	table[2].data	= &nf_frag->fqdir->high_thresh;
88 	table[2].extra1	= &nf_frag->fqdir->low_thresh;
89 
90 	hdr = register_net_sysctl(net, "net/netfilter", table);
91 	if (hdr == NULL)
92 		goto err_reg;
93 
94 	nf_frag->nf_frag_frags_hdr = hdr;
95 	return 0;
96 
97 err_reg:
98 	if (!net_eq(net, &init_net))
99 		kfree(table);
100 err_alloc:
101 	return -ENOMEM;
102 }
103 
104 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
105 {
106 	struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
107 	struct ctl_table *table;
108 
109 	table = nf_frag->nf_frag_frags_hdr->ctl_table_arg;
110 	unregister_net_sysctl_table(nf_frag->nf_frag_frags_hdr);
111 	if (!net_eq(net, &init_net))
112 		kfree(table);
113 }
114 
115 #else
116 static int nf_ct_frag6_sysctl_register(struct net *net)
117 {
118 	return 0;
119 }
120 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
121 {
122 }
123 #endif
124 
125 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
126 			     struct sk_buff *prev_tail, struct net_device *dev);
127 
128 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
129 {
130 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
131 }
132 
133 static void nf_ct_frag6_expire(struct timer_list *t)
134 {
135 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
136 	struct frag_queue *fq;
137 
138 	fq = container_of(frag, struct frag_queue, q);
139 
140 	ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
141 }
142 
143 /* Creation primitives. */
144 static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
145 				  const struct ipv6hdr *hdr, int iif)
146 {
147 	struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
148 	struct frag_v6_compare_key key = {
149 		.id = id,
150 		.saddr = hdr->saddr,
151 		.daddr = hdr->daddr,
152 		.user = user,
153 		.iif = iif,
154 	};
155 	struct inet_frag_queue *q;
156 
157 	q = inet_frag_find(nf_frag->fqdir, &key);
158 	if (!q)
159 		return NULL;
160 
161 	return container_of(q, struct frag_queue, q);
162 }
163 
164 
165 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
166 			     const struct frag_hdr *fhdr, int nhoff)
167 {
168 	unsigned int payload_len;
169 	struct net_device *dev;
170 	struct sk_buff *prev;
171 	int offset, end, err;
172 	u8 ecn;
173 
174 	if (fq->q.flags & INET_FRAG_COMPLETE) {
175 		pr_debug("Already completed\n");
176 		goto err;
177 	}
178 
179 	payload_len = ntohs(ipv6_hdr(skb)->payload_len);
180 
181 	offset = ntohs(fhdr->frag_off) & ~0x7;
182 	end = offset + (payload_len -
183 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
184 
185 	if ((unsigned int)end > IPV6_MAXPLEN) {
186 		pr_debug("offset is too large.\n");
187 		return -EINVAL;
188 	}
189 
190 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
191 
192 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
193 		const unsigned char *nh = skb_network_header(skb);
194 		skb->csum = csum_sub(skb->csum,
195 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
196 						  0));
197 	}
198 
199 	/* Is this the final fragment? */
200 	if (!(fhdr->frag_off & htons(IP6_MF))) {
201 		/* If we already have some bits beyond end
202 		 * or have different end, the segment is corrupted.
203 		 */
204 		if (end < fq->q.len ||
205 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
206 			pr_debug("already received last fragment\n");
207 			goto err;
208 		}
209 		fq->q.flags |= INET_FRAG_LAST_IN;
210 		fq->q.len = end;
211 	} else {
212 		/* Check if the fragment is rounded to 8 bytes.
213 		 * Required by the RFC.
214 		 */
215 		if (end & 0x7) {
216 			/* RFC2460 says always send parameter problem in
217 			 * this case. -DaveM
218 			 */
219 			pr_debug("end of fragment not rounded to 8 bytes.\n");
220 			inet_frag_kill(&fq->q);
221 			return -EPROTO;
222 		}
223 		if (end > fq->q.len) {
224 			/* Some bits beyond end -> corruption. */
225 			if (fq->q.flags & INET_FRAG_LAST_IN) {
226 				pr_debug("last packet already reached.\n");
227 				goto err;
228 			}
229 			fq->q.len = end;
230 		}
231 	}
232 
233 	if (end == offset)
234 		goto err;
235 
236 	/* Point into the IP datagram 'data' part. */
237 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
238 		pr_debug("queue: message is too short.\n");
239 		goto err;
240 	}
241 	if (pskb_trim_rcsum(skb, end - offset)) {
242 		pr_debug("Can't trim\n");
243 		goto err;
244 	}
245 
246 	/* Note : skb->rbnode and skb->dev share the same location. */
247 	dev = skb->dev;
248 	/* Makes sure compiler wont do silly aliasing games */
249 	barrier();
250 
251 	prev = fq->q.fragments_tail;
252 	err = inet_frag_queue_insert(&fq->q, skb, offset, end);
253 	if (err) {
254 		if (err == IPFRAG_DUP) {
255 			/* No error for duplicates, pretend they got queued. */
256 			kfree_skb_reason(skb, SKB_DROP_REASON_DUP_FRAG);
257 			return -EINPROGRESS;
258 		}
259 		goto insert_error;
260 	}
261 
262 	if (dev)
263 		fq->iif = dev->ifindex;
264 
265 	fq->q.stamp = skb->tstamp;
266 	fq->q.mono_delivery_time = skb->mono_delivery_time;
267 	fq->q.meat += skb->len;
268 	fq->ecn |= ecn;
269 	if (payload_len > fq->q.max_size)
270 		fq->q.max_size = payload_len;
271 	add_frag_mem_limit(fq->q.fqdir, skb->truesize);
272 
273 	/* The first fragment.
274 	 * nhoffset is obtained from the first fragment, of course.
275 	 */
276 	if (offset == 0) {
277 		fq->nhoffset = nhoff;
278 		fq->q.flags |= INET_FRAG_FIRST_IN;
279 	}
280 
281 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
282 	    fq->q.meat == fq->q.len) {
283 		unsigned long orefdst = skb->_skb_refdst;
284 
285 		skb->_skb_refdst = 0UL;
286 		err = nf_ct_frag6_reasm(fq, skb, prev, dev);
287 		skb->_skb_refdst = orefdst;
288 
289 		/* After queue has assumed skb ownership, only 0 or
290 		 * -EINPROGRESS must be returned.
291 		 */
292 		return err ? -EINPROGRESS : 0;
293 	}
294 
295 	skb_dst_drop(skb);
296 	return -EINPROGRESS;
297 
298 insert_error:
299 	inet_frag_kill(&fq->q);
300 err:
301 	skb_dst_drop(skb);
302 	return -EINVAL;
303 }
304 
305 /*
306  *	Check if this packet is complete.
307  *
308  *	It is called with locked fq, and caller must check that
309  *	queue is eligible for reassembly i.e. it is not COMPLETE,
310  *	the last and the first frames arrived and all the bits are here.
311  */
312 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
313 			     struct sk_buff *prev_tail, struct net_device *dev)
314 {
315 	void *reasm_data;
316 	int payload_len;
317 	u8 ecn;
318 
319 	inet_frag_kill(&fq->q);
320 
321 	ecn = ip_frag_ecn_table[fq->ecn];
322 	if (unlikely(ecn == 0xff))
323 		goto err;
324 
325 	reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
326 	if (!reasm_data)
327 		goto err;
328 
329 	payload_len = ((skb->data - skb_network_header(skb)) -
330 		       sizeof(struct ipv6hdr) + fq->q.len -
331 		       sizeof(struct frag_hdr));
332 	if (payload_len > IPV6_MAXPLEN) {
333 		net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
334 				    payload_len);
335 		goto err;
336 	}
337 
338 	/* We have to remove fragment header from datagram and to relocate
339 	 * header in order to calculate ICV correctly. */
340 	skb_network_header(skb)[fq->nhoffset] = skb_transport_header(skb)[0];
341 	memmove(skb->head + sizeof(struct frag_hdr), skb->head,
342 		(skb->data - skb->head) - sizeof(struct frag_hdr));
343 	skb->mac_header += sizeof(struct frag_hdr);
344 	skb->network_header += sizeof(struct frag_hdr);
345 
346 	skb_reset_transport_header(skb);
347 
348 	inet_frag_reasm_finish(&fq->q, skb, reasm_data, false);
349 
350 	skb->ignore_df = 1;
351 	skb->dev = dev;
352 	ipv6_hdr(skb)->payload_len = htons(payload_len);
353 	ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
354 	IP6CB(skb)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
355 	IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
356 
357 	/* Yes, and fold redundant checksum back. 8) */
358 	if (skb->ip_summed == CHECKSUM_COMPLETE)
359 		skb->csum = csum_partial(skb_network_header(skb),
360 					 skb_network_header_len(skb),
361 					 skb->csum);
362 
363 	fq->q.rb_fragments = RB_ROOT;
364 	fq->q.fragments_tail = NULL;
365 	fq->q.last_run_head = NULL;
366 
367 	return 0;
368 
369 err:
370 	inet_frag_kill(&fq->q);
371 	return -EINVAL;
372 }
373 
374 /*
375  * find the header just before Fragment Header.
376  *
377  * if success return 0 and set ...
378  * (*prevhdrp): the value of "Next Header Field" in the header
379  *		just before Fragment Header.
380  * (*prevhoff): the offset of "Next Header Field" in the header
381  *		just before Fragment Header.
382  * (*fhoff)   : the offset of Fragment Header.
383  *
384  * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
385  *
386  */
387 static int
388 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
389 {
390 	u8 nexthdr = ipv6_hdr(skb)->nexthdr;
391 	const int netoff = skb_network_offset(skb);
392 	u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
393 	int start = netoff + sizeof(struct ipv6hdr);
394 	int len = skb->len - start;
395 	u8 prevhdr = NEXTHDR_IPV6;
396 
397 	while (nexthdr != NEXTHDR_FRAGMENT) {
398 		struct ipv6_opt_hdr hdr;
399 		int hdrlen;
400 
401 		if (!ipv6_ext_hdr(nexthdr)) {
402 			return -1;
403 		}
404 		if (nexthdr == NEXTHDR_NONE) {
405 			pr_debug("next header is none\n");
406 			return -1;
407 		}
408 		if (len < (int)sizeof(struct ipv6_opt_hdr)) {
409 			pr_debug("too short\n");
410 			return -1;
411 		}
412 		if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
413 			BUG();
414 		if (nexthdr == NEXTHDR_AUTH)
415 			hdrlen = ipv6_authlen(&hdr);
416 		else
417 			hdrlen = ipv6_optlen(&hdr);
418 
419 		prevhdr = nexthdr;
420 		prev_nhoff = start;
421 
422 		nexthdr = hdr.nexthdr;
423 		len -= hdrlen;
424 		start += hdrlen;
425 	}
426 
427 	if (len < 0)
428 		return -1;
429 
430 	*prevhdrp = prevhdr;
431 	*prevhoff = prev_nhoff;
432 	*fhoff = start;
433 
434 	return 0;
435 }
436 
437 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
438 {
439 	u16 savethdr = skb->transport_header;
440 	u8 nexthdr = NEXTHDR_FRAGMENT;
441 	int fhoff, nhoff, ret;
442 	struct frag_hdr *fhdr;
443 	struct frag_queue *fq;
444 	struct ipv6hdr *hdr;
445 	u8 prevhdr;
446 
447 	/* Jumbo payload inhibits frag. header */
448 	if (ipv6_hdr(skb)->payload_len == 0) {
449 		pr_debug("payload len = 0\n");
450 		return 0;
451 	}
452 
453 	if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
454 		return 0;
455 
456 	/* Discard the first fragment if it does not include all headers
457 	 * RFC 8200, Section 4.5
458 	 */
459 	if (ipv6frag_thdr_truncated(skb, fhoff, &nexthdr)) {
460 		pr_debug("Drop incomplete fragment\n");
461 		return 0;
462 	}
463 
464 	if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
465 		return -ENOMEM;
466 
467 	skb_set_transport_header(skb, fhoff);
468 	hdr = ipv6_hdr(skb);
469 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
470 
471 	skb_orphan(skb);
472 	fq = fq_find(net, fhdr->identification, user, hdr,
473 		     skb->dev ? skb->dev->ifindex : 0);
474 	if (fq == NULL) {
475 		pr_debug("Can't find and can't create new queue\n");
476 		return -ENOMEM;
477 	}
478 
479 	spin_lock_bh(&fq->q.lock);
480 
481 	ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
482 	if (ret == -EPROTO) {
483 		skb->transport_header = savethdr;
484 		ret = 0;
485 	}
486 
487 	spin_unlock_bh(&fq->q.lock);
488 	inet_frag_put(&fq->q);
489 	return ret;
490 }
491 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
492 
493 static int nf_ct_net_init(struct net *net)
494 {
495 	struct nft_ct_frag6_pernet *nf_frag  = nf_frag_pernet(net);
496 	int res;
497 
498 	res = fqdir_init(&nf_frag->fqdir, &nf_frags, net);
499 	if (res < 0)
500 		return res;
501 
502 	nf_frag->fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
503 	nf_frag->fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
504 	nf_frag->fqdir->timeout = IPV6_FRAG_TIMEOUT;
505 
506 	res = nf_ct_frag6_sysctl_register(net);
507 	if (res < 0)
508 		fqdir_exit(nf_frag->fqdir);
509 	return res;
510 }
511 
512 static void nf_ct_net_pre_exit(struct net *net)
513 {
514 	struct nft_ct_frag6_pernet *nf_frag  = nf_frag_pernet(net);
515 
516 	fqdir_pre_exit(nf_frag->fqdir);
517 }
518 
519 static void nf_ct_net_exit(struct net *net)
520 {
521 	struct nft_ct_frag6_pernet *nf_frag  = nf_frag_pernet(net);
522 
523 	nf_ct_frags6_sysctl_unregister(net);
524 	fqdir_exit(nf_frag->fqdir);
525 }
526 
527 static struct pernet_operations nf_ct_net_ops = {
528 	.init		= nf_ct_net_init,
529 	.pre_exit	= nf_ct_net_pre_exit,
530 	.exit		= nf_ct_net_exit,
531 	.id		= &nf_frag_pernet_id,
532 	.size		= sizeof(struct nft_ct_frag6_pernet),
533 };
534 
535 static const struct rhashtable_params nfct_rhash_params = {
536 	.head_offset		= offsetof(struct inet_frag_queue, node),
537 	.hashfn			= ip6frag_key_hashfn,
538 	.obj_hashfn		= ip6frag_obj_hashfn,
539 	.obj_cmpfn		= ip6frag_obj_cmpfn,
540 	.automatic_shrinking	= true,
541 };
542 
543 int nf_ct_frag6_init(void)
544 {
545 	int ret = 0;
546 
547 	nf_frags.constructor = ip6frag_init;
548 	nf_frags.destructor = NULL;
549 	nf_frags.qsize = sizeof(struct frag_queue);
550 	nf_frags.frag_expire = nf_ct_frag6_expire;
551 	nf_frags.frags_cache_name = nf_frags_cache_name;
552 	nf_frags.rhash_params = nfct_rhash_params;
553 	ret = inet_frags_init(&nf_frags);
554 	if (ret)
555 		goto out;
556 	ret = register_pernet_subsys(&nf_ct_net_ops);
557 	if (ret)
558 		inet_frags_fini(&nf_frags);
559 
560 out:
561 	return ret;
562 }
563 
564 void nf_ct_frag6_cleanup(void)
565 {
566 	unregister_pernet_subsys(&nf_ct_net_ops);
567 	inet_frags_fini(&nf_frags);
568 }
569