xref: /linux/net/ipv6/reassembly.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	IPv6 fragment reassembly
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Based on: net/ipv4/ip_fragment.c
10  */
11 
12 /*
13  *	Fixes:
14  *	Andi Kleen	Make it work with multiple hosts.
15  *			More RFC compliance.
16  *
17  *      Horst von Brand Add missing #include <linux/string.h>
18  *	Alexey Kuznetsov	SMP races, threading, cleanup.
19  *	Patrick McHardy		LRU queue of frag heads for evictor.
20  *	Mitsuru KANDA @USAGI	Register inet6_protocol{}.
21  *	David Stevens and
22  *	YOSHIFUJI,H. @USAGI	Always remove fragment header to
23  *				calculate ICV correctly.
24  */
25 
26 #define pr_fmt(fmt) "IPv6: " fmt
27 
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
33 #include <linux/jiffies.h>
34 #include <linux/net.h>
35 #include <linux/list.h>
36 #include <linux/netdevice.h>
37 #include <linux/in6.h>
38 #include <linux/ipv6.h>
39 #include <linux/icmpv6.h>
40 #include <linux/random.h>
41 #include <linux/jhash.h>
42 #include <linux/skbuff.h>
43 #include <linux/slab.h>
44 #include <linux/export.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47 
48 #include <net/sock.h>
49 #include <net/snmp.h>
50 
51 #include <net/ipv6.h>
52 #include <net/ip6_route.h>
53 #include <net/protocol.h>
54 #include <net/transp_v6.h>
55 #include <net/rawv6.h>
56 #include <net/ndisc.h>
57 #include <net/addrconf.h>
58 #include <net/ipv6_frag.h>
59 #include <net/inet_ecn.h>
60 
61 static const char ip6_frag_cache_name[] = "ip6-frags";
62 
63 static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
64 {
65 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
66 }
67 
68 static struct inet_frags ip6_frags;
69 
70 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
71 			  struct sk_buff *prev_tail, struct net_device *dev,
72 			  struct inet6_dev *idev, int *refs);
73 
74 static void ip6_frag_expire(struct timer_list *t)
75 {
76 	struct inet_frag_queue *frag = timer_container_of(frag, t, timer);
77 	struct frag_queue *fq;
78 
79 	fq = container_of(frag, struct frag_queue, q);
80 
81 	ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
82 }
83 
84 static struct frag_queue *
85 fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
86 {
87 	struct frag_v6_compare_key key = {
88 		.id = id,
89 		.saddr = hdr->saddr,
90 		.daddr = hdr->daddr,
91 		.user = IP6_DEFRAG_LOCAL_DELIVER,
92 		.iif = iif,
93 	};
94 	struct inet_frag_queue *q;
95 
96 	if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
97 					    IPV6_ADDR_LINKLOCAL)))
98 		key.iif = 0;
99 
100 	q = inet_frag_find(net->ipv6.fqdir, &key);
101 	if (!q)
102 		return NULL;
103 
104 	return container_of(q, struct frag_queue, q);
105 }
106 
107 static int ip6_frag_queue(struct net *net,
108 			  struct frag_queue *fq, struct sk_buff *skb,
109 			  struct frag_hdr *fhdr, int nhoff,
110 			  u32 *prob_offset, int *refs,
111 			  struct inet6_dev *idev)
112 {
113 	int offset, end, fragsize;
114 	struct sk_buff *prev_tail;
115 	struct net_device *dev;
116 	int err = -ENOENT;
117 	SKB_DR(reason);
118 	u8 ecn;
119 
120 	/* If reassembly is already done, @skb must be a duplicate frag. */
121 	if (fq->q.flags & INET_FRAG_COMPLETE) {
122 		SKB_DR_SET(reason, DUP_FRAG);
123 		goto err;
124 	}
125 
126 	err = -EINVAL;
127 	offset = ntohs(fhdr->frag_off) & ~0x7;
128 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
129 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
130 
131 	if ((unsigned int)end > IPV6_MAXPLEN) {
132 		*prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
133 		/* note that if prob_offset is set, the skb is freed elsewhere,
134 		 * we do not free it here.
135 		 */
136 		inet_frag_kill(&fq->q, refs);
137 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
138 		return -1;
139 	}
140 
141 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
142 
143 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
144 		const unsigned char *nh = skb_network_header(skb);
145 		skb->csum = csum_sub(skb->csum,
146 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
147 						  0));
148 	}
149 
150 	/* Is this the final fragment? */
151 	if (!(fhdr->frag_off & htons(IP6_MF))) {
152 		/* If we already have some bits beyond end
153 		 * or have different end, the segment is corrupted.
154 		 */
155 		if (end < fq->q.len ||
156 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
157 			goto discard_fq;
158 		fq->q.flags |= INET_FRAG_LAST_IN;
159 		fq->q.len = end;
160 	} else {
161 		/* Check if the fragment is rounded to 8 bytes.
162 		 * Required by the RFC.
163 		 */
164 		if (end & 0x7) {
165 			/* RFC2460 says always send parameter problem in
166 			 * this case. -DaveM
167 			 */
168 			*prob_offset = offsetof(struct ipv6hdr, payload_len);
169 			inet_frag_kill(&fq->q, refs);
170 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
171 			return -1;
172 		}
173 		if (end > fq->q.len) {
174 			/* Some bits beyond end -> corruption. */
175 			if (fq->q.flags & INET_FRAG_LAST_IN)
176 				goto discard_fq;
177 			fq->q.len = end;
178 		}
179 	}
180 
181 	if (end == offset)
182 		goto discard_fq;
183 
184 	err = -ENOMEM;
185 	/* Point into the IP datagram 'data' part. */
186 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
187 		goto discard_fq;
188 
189 	err = pskb_trim_rcsum(skb, end - offset);
190 	if (err)
191 		goto discard_fq;
192 
193 	/* Note : skb->rbnode and skb->dev share the same location. */
194 	dev = skb->dev;
195 	/* Makes sure compiler wont do silly aliasing games */
196 	barrier();
197 
198 	prev_tail = fq->q.fragments_tail;
199 	err = inet_frag_queue_insert(&fq->q, skb, offset, end);
200 	if (err)
201 		goto insert_error;
202 
203 	if (dev)
204 		fq->iif = dev->ifindex;
205 
206 	fq->q.stamp = skb->tstamp;
207 	fq->q.tstamp_type = skb->tstamp_type;
208 	fq->q.meat += skb->len;
209 	fq->ecn |= ecn;
210 	add_frag_mem_limit(fq->q.fqdir, skb->truesize);
211 
212 	fragsize = -skb_network_offset(skb) + skb->len;
213 	if (fragsize > fq->q.max_size)
214 		fq->q.max_size = fragsize;
215 
216 	/* The first fragment.
217 	 * nhoffset is obtained from the first fragment, of course.
218 	 */
219 	if (offset == 0) {
220 		fq->nhoffset = nhoff;
221 		fq->q.flags |= INET_FRAG_FIRST_IN;
222 	}
223 
224 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
225 	    fq->q.meat == fq->q.len) {
226 		unsigned long orefdst = skb->_skb_refdst;
227 
228 		skb->_skb_refdst = 0UL;
229 		err = ip6_frag_reasm(fq, skb, prev_tail, dev, idev, refs);
230 		skb->_skb_refdst = orefdst;
231 		return err;
232 	}
233 
234 	skb_dst_drop(skb);
235 	skb_orphan(skb);
236 	return -EINPROGRESS;
237 
238 insert_error:
239 	if (err == IPFRAG_DUP) {
240 		SKB_DR_SET(reason, DUP_FRAG);
241 		err = -EINVAL;
242 		goto err;
243 	}
244 	if (err == IPFRAG_OVERLAP)
245 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASM_OVERLAPS);
246 	err = -EINVAL;
247 discard_fq:
248 	inet_frag_kill(&fq->q, refs);
249 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
250 err:
251 	kfree_skb_reason(skb, reason);
252 	return err;
253 }
254 
255 /*
256  *	Check if this packet is complete.
257  *
258  *	It is called with locked fq, and caller must check that
259  *	queue is eligible for reassembly i.e. it is not COMPLETE,
260  *	the last and the first frames arrived and all the bits are here.
261  */
262 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
263 			  struct sk_buff *prev_tail, struct net_device *dev,
264 			  struct inet6_dev *idev, int *refs)
265 {
266 	struct net *net = fq->q.fqdir->net;
267 	unsigned int nhoff;
268 	void *reasm_data;
269 	int payload_len;
270 	u8 ecn;
271 
272 	inet_frag_kill(&fq->q, refs);
273 
274 	ecn = ip_frag_ecn_table[fq->ecn];
275 	if (unlikely(ecn == 0xff))
276 		goto out_fail;
277 
278 	/* Check the size on the first fragment, which will become the head:
279 	 * inet_frag_reasm_finish() must follow inet_frag_reasm_prepare().
280 	 */
281 	payload_len = -skb_network_offset(skb_rb_first(&fq->q.rb_fragments)) -
282 		       sizeof(struct ipv6hdr) + fq->q.len -
283 		       sizeof(struct frag_hdr);
284 	if (payload_len > IPV6_MAXPLEN)
285 		goto out_oversize;
286 
287 	reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
288 	if (!reasm_data)
289 		goto out_oom;
290 
291 	/* We have to remove fragment header from datagram and to relocate
292 	 * header in order to calculate ICV correctly. */
293 	nhoff = fq->nhoffset;
294 	skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
295 	memmove(skb->head + sizeof(struct frag_hdr), skb->head,
296 		(skb->data - skb->head) - sizeof(struct frag_hdr));
297 	if (skb_mac_header_was_set(skb))
298 		skb->mac_header += sizeof(struct frag_hdr);
299 	skb->network_header += sizeof(struct frag_hdr);
300 
301 	skb_reset_transport_header(skb);
302 
303 	inet_frag_reasm_finish(&fq->q, skb, reasm_data, true);
304 
305 	skb->dev = dev;
306 	ipv6_hdr(skb)->payload_len = htons(payload_len);
307 	ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
308 	IP6CB(skb)->nhoff = nhoff;
309 	IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
310 	IP6CB(skb)->frag_max_size = fq->q.max_size;
311 
312 	/* Yes, and fold redundant checksum back. 8) */
313 	skb_postpush_rcsum(skb, skb_network_header(skb),
314 			   skb_network_header_len(skb));
315 
316 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMOKS);
317 	fq->q.rb_fragments = RB_ROOT;
318 	fq->q.fragments_tail = NULL;
319 	fq->q.last_run_head = NULL;
320 	return 1;
321 
322 out_oversize:
323 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
324 	goto out_fail;
325 out_oom:
326 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
327 out_fail:
328 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
329 	inet_frag_kill(&fq->q, refs);
330 	return -1;
331 }
332 
333 static int ipv6_frag_rcv(struct sk_buff *skb)
334 {
335 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
336 	struct net *net = skb_dst_dev_net(skb);
337 	struct inet6_dev *idev;
338 	struct frag_hdr *fhdr;
339 	struct frag_queue *fq;
340 	u8 nexthdr;
341 	int iif;
342 
343 	idev = skb->dev ? __in6_dev_stats_get(skb->dev, skb) : NULL;
344 
345 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
346 		goto fail_hdr;
347 
348 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMREQDS);
349 
350 	/* Jumbo payload inhibits frag. header */
351 	if (hdr->payload_len == 0)
352 		goto fail_hdr;
353 
354 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
355 				 sizeof(struct frag_hdr))))
356 		goto fail_hdr;
357 
358 	hdr = ipv6_hdr(skb);
359 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
360 
361 	if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) {
362 		/* It is not a fragmented frame */
363 		skb->transport_header += sizeof(struct frag_hdr);
364 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMOKS);
365 
366 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
367 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
368 		IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) +
369 					    sizeof(struct ipv6hdr);
370 		return 1;
371 	}
372 
373 	/* RFC 8200, Section 4.5 Fragment Header:
374 	 * If the first fragment does not include all headers through an
375 	 * Upper-Layer header, then that fragment should be discarded and
376 	 * an ICMP Parameter Problem, Code 3, message should be sent to
377 	 * the source of the fragment, with the Pointer field set to zero.
378 	 */
379 	nexthdr = hdr->nexthdr;
380 	if (ipv6frag_thdr_truncated(skb, skb_network_offset(skb) + sizeof(struct ipv6hdr), &nexthdr)) {
381 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
382 		icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0);
383 		return -1;
384 	}
385 
386 	iif = skb->dev ? skb->dev->ifindex : 0;
387 	rcu_read_lock();
388 	fq = fq_find(net, fhdr->identification, hdr, iif);
389 	if (fq) {
390 		u32 prob_offset = 0;
391 		int ret, refs = 0;
392 
393 		spin_lock(&fq->q.lock);
394 
395 		fq->iif = iif;
396 		ret = ip6_frag_queue(net, fq, skb, fhdr, IP6CB(skb)->nhoff,
397 				     &prob_offset, &refs, idev);
398 
399 		spin_unlock(&fq->q.lock);
400 		rcu_read_unlock();
401 		inet_frag_putn(&fq->q, refs);
402 		if (prob_offset) {
403 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
404 			/* icmpv6_param_prob() calls kfree_skb(skb) */
405 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
406 		}
407 		return ret;
408 	}
409 	rcu_read_unlock();
410 
411 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
412 	kfree_skb(skb);
413 	return -1;
414 
415 fail_hdr:
416 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
417 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
418 	return -1;
419 }
420 
421 static const struct inet6_protocol frag_protocol = {
422 	.handler	=	ipv6_frag_rcv,
423 	.flags		=	INET6_PROTO_NOPOLICY,
424 };
425 
426 #ifdef CONFIG_SYSCTL
427 
428 static struct ctl_table ip6_frags_ns_ctl_table[] = {
429 	{
430 		.procname	= "ip6frag_high_thresh",
431 		.maxlen		= sizeof(unsigned long),
432 		.mode		= 0644,
433 		.proc_handler	= proc_doulongvec_minmax,
434 	},
435 	{
436 		.procname	= "ip6frag_low_thresh",
437 		.maxlen		= sizeof(unsigned long),
438 		.mode		= 0644,
439 		.proc_handler	= proc_doulongvec_minmax,
440 	},
441 	{
442 		.procname	= "ip6frag_time",
443 		.maxlen		= sizeof(int),
444 		.mode		= 0644,
445 		.proc_handler	= proc_dointvec_jiffies,
446 	},
447 };
448 
449 /* secret interval has been deprecated */
450 static int ip6_frags_secret_interval_unused;
451 static struct ctl_table ip6_frags_ctl_table[] = {
452 	{
453 		.procname	= "ip6frag_secret_interval",
454 		.data		= &ip6_frags_secret_interval_unused,
455 		.maxlen		= sizeof(int),
456 		.mode		= 0644,
457 		.proc_handler	= proc_dointvec_jiffies,
458 	},
459 };
460 
461 static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
462 {
463 	struct ctl_table *table;
464 	struct ctl_table_header *hdr;
465 
466 	table = ip6_frags_ns_ctl_table;
467 	if (!net_eq(net, &init_net)) {
468 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
469 		if (!table)
470 			goto err_alloc;
471 
472 	}
473 	table[0].data	= &net->ipv6.fqdir->high_thresh;
474 	table[0].extra1	= &net->ipv6.fqdir->low_thresh;
475 	table[1].data	= &net->ipv6.fqdir->low_thresh;
476 	table[1].extra2	= &net->ipv6.fqdir->high_thresh;
477 	table[2].data	= &net->ipv6.fqdir->timeout;
478 
479 	hdr = register_net_sysctl_sz(net, "net/ipv6", table,
480 				     ARRAY_SIZE(ip6_frags_ns_ctl_table));
481 	if (!hdr)
482 		goto err_reg;
483 
484 	net->ipv6.sysctl.frags_hdr = hdr;
485 	return 0;
486 
487 err_reg:
488 	if (!net_eq(net, &init_net))
489 		kfree(table);
490 err_alloc:
491 	return -ENOMEM;
492 }
493 
494 static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
495 {
496 	const struct ctl_table *table;
497 
498 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
499 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
500 	if (!net_eq(net, &init_net))
501 		kfree(table);
502 }
503 
504 static struct ctl_table_header *ip6_ctl_header;
505 
506 static int ip6_frags_sysctl_register(void)
507 {
508 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
509 			ip6_frags_ctl_table);
510 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
511 }
512 
513 static void ip6_frags_sysctl_unregister(void)
514 {
515 	unregister_net_sysctl_table(ip6_ctl_header);
516 }
517 #else
518 static int ip6_frags_ns_sysctl_register(struct net *net)
519 {
520 	return 0;
521 }
522 
523 static void ip6_frags_ns_sysctl_unregister(struct net *net)
524 {
525 }
526 
527 static int ip6_frags_sysctl_register(void)
528 {
529 	return 0;
530 }
531 
532 static void ip6_frags_sysctl_unregister(void)
533 {
534 }
535 #endif
536 
537 static int __net_init ipv6_frags_init_net(struct net *net)
538 {
539 	int res;
540 
541 	res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
542 	if (res < 0)
543 		return res;
544 
545 	net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
546 	net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
547 	net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
548 
549 	res = ip6_frags_ns_sysctl_register(net);
550 	if (res < 0)
551 		fqdir_exit(net->ipv6.fqdir);
552 	return res;
553 }
554 
555 static void __net_exit ipv6_frags_pre_exit_net(struct net *net)
556 {
557 	fqdir_pre_exit(net->ipv6.fqdir);
558 }
559 
560 static void __net_exit ipv6_frags_exit_net(struct net *net)
561 {
562 	ip6_frags_ns_sysctl_unregister(net);
563 	fqdir_exit(net->ipv6.fqdir);
564 }
565 
566 static struct pernet_operations ip6_frags_ops = {
567 	.init		= ipv6_frags_init_net,
568 	.pre_exit	= ipv6_frags_pre_exit_net,
569 	.exit		= ipv6_frags_exit_net,
570 };
571 
572 static const struct rhashtable_params ip6_rhash_params = {
573 	.head_offset		= offsetof(struct inet_frag_queue, node),
574 	.hashfn			= ip6frag_key_hashfn,
575 	.obj_hashfn		= ip6frag_obj_hashfn,
576 	.obj_cmpfn		= ip6frag_obj_cmpfn,
577 	.automatic_shrinking	= true,
578 };
579 
580 int __init ipv6_frag_init(void)
581 {
582 	int ret;
583 
584 	ip6_frags.constructor = ip6frag_init;
585 	ip6_frags.destructor = NULL;
586 	ip6_frags.qsize = sizeof(struct frag_queue);
587 	ip6_frags.frag_expire = ip6_frag_expire;
588 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
589 	ip6_frags.rhash_params = ip6_rhash_params;
590 	ret = inet_frags_init(&ip6_frags);
591 	if (ret)
592 		goto out;
593 
594 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
595 	if (ret)
596 		goto err_protocol;
597 
598 	ret = ip6_frags_sysctl_register();
599 	if (ret)
600 		goto err_sysctl;
601 
602 	ret = register_pernet_subsys(&ip6_frags_ops);
603 	if (ret)
604 		goto err_pernet;
605 
606 out:
607 	return ret;
608 
609 err_pernet:
610 	ip6_frags_sysctl_unregister();
611 err_sysctl:
612 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
613 err_protocol:
614 	inet_frags_fini(&ip6_frags);
615 	goto out;
616 }
617 
618 void ipv6_frag_exit(void)
619 {
620 	ip6_frags_sysctl_unregister();
621 	unregister_pernet_subsys(&ip6_frags_ops);
622 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
623 	inet_frags_fini(&ip6_frags);
624 }
625