xref: /linux/net/ipv6/reassembly.c (revision d6f6d7123355388f2f41c1b6c108bfdba18b0cfc)
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 	return -EINPROGRESS;
236 
237 insert_error:
238 	if (err == IPFRAG_DUP) {
239 		SKB_DR_SET(reason, DUP_FRAG);
240 		err = -EINVAL;
241 		goto err;
242 	}
243 	err = -EINVAL;
244 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASM_OVERLAPS);
245 discard_fq:
246 	inet_frag_kill(&fq->q, refs);
247 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
248 err:
249 	kfree_skb_reason(skb, reason);
250 	return err;
251 }
252 
253 /*
254  *	Check if this packet is complete.
255  *
256  *	It is called with locked fq, and caller must check that
257  *	queue is eligible for reassembly i.e. it is not COMPLETE,
258  *	the last and the first frames arrived and all the bits are here.
259  */
260 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
261 			  struct sk_buff *prev_tail, struct net_device *dev,
262 			  struct inet6_dev *idev, int *refs)
263 {
264 	struct net *net = fq->q.fqdir->net;
265 	unsigned int nhoff;
266 	void *reasm_data;
267 	int payload_len;
268 	u8 ecn;
269 
270 	inet_frag_kill(&fq->q, refs);
271 
272 	ecn = ip_frag_ecn_table[fq->ecn];
273 	if (unlikely(ecn == 0xff))
274 		goto out_fail;
275 
276 	reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
277 	if (!reasm_data)
278 		goto out_oom;
279 
280 	payload_len = -skb_network_offset(skb) -
281 		       sizeof(struct ipv6hdr) + fq->q.len -
282 		       sizeof(struct frag_hdr);
283 	if (payload_len > IPV6_MAXPLEN)
284 		goto out_oversize;
285 
286 	/* We have to remove fragment header from datagram and to relocate
287 	 * header in order to calculate ICV correctly. */
288 	nhoff = fq->nhoffset;
289 	skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
290 	memmove(skb->head + sizeof(struct frag_hdr), skb->head,
291 		(skb->data - skb->head) - sizeof(struct frag_hdr));
292 	if (skb_mac_header_was_set(skb))
293 		skb->mac_header += sizeof(struct frag_hdr);
294 	skb->network_header += sizeof(struct frag_hdr);
295 
296 	skb_reset_transport_header(skb);
297 
298 	inet_frag_reasm_finish(&fq->q, skb, reasm_data, true);
299 
300 	skb->dev = dev;
301 	ipv6_hdr(skb)->payload_len = htons(payload_len);
302 	ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
303 	IP6CB(skb)->nhoff = nhoff;
304 	IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
305 	IP6CB(skb)->frag_max_size = fq->q.max_size;
306 
307 	/* Yes, and fold redundant checksum back. 8) */
308 	skb_postpush_rcsum(skb, skb_network_header(skb),
309 			   skb_network_header_len(skb));
310 
311 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMOKS);
312 	fq->q.rb_fragments = RB_ROOT;
313 	fq->q.fragments_tail = NULL;
314 	fq->q.last_run_head = NULL;
315 	return 1;
316 
317 out_oversize:
318 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
319 	goto out_fail;
320 out_oom:
321 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
322 out_fail:
323 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
324 	inet_frag_kill(&fq->q, refs);
325 	return -1;
326 }
327 
328 static int ipv6_frag_rcv(struct sk_buff *skb)
329 {
330 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
331 	struct net *net = skb_dst_dev_net(skb);
332 	struct inet6_dev *idev;
333 	struct frag_hdr *fhdr;
334 	struct frag_queue *fq;
335 	u8 nexthdr;
336 	int iif;
337 
338 	idev = skb->dev ? __in6_dev_stats_get(skb->dev, skb) : NULL;
339 
340 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
341 		goto fail_hdr;
342 
343 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMREQDS);
344 
345 	/* Jumbo payload inhibits frag. header */
346 	if (hdr->payload_len == 0)
347 		goto fail_hdr;
348 
349 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
350 				 sizeof(struct frag_hdr))))
351 		goto fail_hdr;
352 
353 	hdr = ipv6_hdr(skb);
354 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
355 
356 	if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) {
357 		/* It is not a fragmented frame */
358 		skb->transport_header += sizeof(struct frag_hdr);
359 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMOKS);
360 
361 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
362 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
363 		IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) +
364 					    sizeof(struct ipv6hdr);
365 		return 1;
366 	}
367 
368 	/* RFC 8200, Section 4.5 Fragment Header:
369 	 * If the first fragment does not include all headers through an
370 	 * Upper-Layer header, then that fragment should be discarded and
371 	 * an ICMP Parameter Problem, Code 3, message should be sent to
372 	 * the source of the fragment, with the Pointer field set to zero.
373 	 */
374 	nexthdr = hdr->nexthdr;
375 	if (ipv6frag_thdr_truncated(skb, skb_network_offset(skb) + sizeof(struct ipv6hdr), &nexthdr)) {
376 		__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
377 		icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0);
378 		return -1;
379 	}
380 
381 	iif = skb->dev ? skb->dev->ifindex : 0;
382 	rcu_read_lock();
383 	fq = fq_find(net, fhdr->identification, hdr, iif);
384 	if (fq) {
385 		u32 prob_offset = 0;
386 		int ret, refs = 0;
387 
388 		spin_lock(&fq->q.lock);
389 
390 		fq->iif = iif;
391 		ret = ip6_frag_queue(net, fq, skb, fhdr, IP6CB(skb)->nhoff,
392 				     &prob_offset, &refs, idev);
393 
394 		spin_unlock(&fq->q.lock);
395 		rcu_read_unlock();
396 		inet_frag_putn(&fq->q, refs);
397 		if (prob_offset) {
398 			__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
399 			/* icmpv6_param_prob() calls kfree_skb(skb) */
400 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
401 		}
402 		return ret;
403 	}
404 	rcu_read_unlock();
405 
406 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_REASMFAILS);
407 	kfree_skb(skb);
408 	return -1;
409 
410 fail_hdr:
411 	__IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
412 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
413 	return -1;
414 }
415 
416 static const struct inet6_protocol frag_protocol = {
417 	.handler	=	ipv6_frag_rcv,
418 	.flags		=	INET6_PROTO_NOPOLICY,
419 };
420 
421 #ifdef CONFIG_SYSCTL
422 
423 static struct ctl_table ip6_frags_ns_ctl_table[] = {
424 	{
425 		.procname	= "ip6frag_high_thresh",
426 		.maxlen		= sizeof(unsigned long),
427 		.mode		= 0644,
428 		.proc_handler	= proc_doulongvec_minmax,
429 	},
430 	{
431 		.procname	= "ip6frag_low_thresh",
432 		.maxlen		= sizeof(unsigned long),
433 		.mode		= 0644,
434 		.proc_handler	= proc_doulongvec_minmax,
435 	},
436 	{
437 		.procname	= "ip6frag_time",
438 		.maxlen		= sizeof(int),
439 		.mode		= 0644,
440 		.proc_handler	= proc_dointvec_jiffies,
441 	},
442 };
443 
444 /* secret interval has been deprecated */
445 static int ip6_frags_secret_interval_unused;
446 static struct ctl_table ip6_frags_ctl_table[] = {
447 	{
448 		.procname	= "ip6frag_secret_interval",
449 		.data		= &ip6_frags_secret_interval_unused,
450 		.maxlen		= sizeof(int),
451 		.mode		= 0644,
452 		.proc_handler	= proc_dointvec_jiffies,
453 	},
454 };
455 
456 static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
457 {
458 	struct ctl_table *table;
459 	struct ctl_table_header *hdr;
460 
461 	table = ip6_frags_ns_ctl_table;
462 	if (!net_eq(net, &init_net)) {
463 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
464 		if (!table)
465 			goto err_alloc;
466 
467 	}
468 	table[0].data	= &net->ipv6.fqdir->high_thresh;
469 	table[0].extra1	= &net->ipv6.fqdir->low_thresh;
470 	table[1].data	= &net->ipv6.fqdir->low_thresh;
471 	table[1].extra2	= &net->ipv6.fqdir->high_thresh;
472 	table[2].data	= &net->ipv6.fqdir->timeout;
473 
474 	hdr = register_net_sysctl_sz(net, "net/ipv6", table,
475 				     ARRAY_SIZE(ip6_frags_ns_ctl_table));
476 	if (!hdr)
477 		goto err_reg;
478 
479 	net->ipv6.sysctl.frags_hdr = hdr;
480 	return 0;
481 
482 err_reg:
483 	if (!net_eq(net, &init_net))
484 		kfree(table);
485 err_alloc:
486 	return -ENOMEM;
487 }
488 
489 static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
490 {
491 	const struct ctl_table *table;
492 
493 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
494 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
495 	if (!net_eq(net, &init_net))
496 		kfree(table);
497 }
498 
499 static struct ctl_table_header *ip6_ctl_header;
500 
501 static int ip6_frags_sysctl_register(void)
502 {
503 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
504 			ip6_frags_ctl_table);
505 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
506 }
507 
508 static void ip6_frags_sysctl_unregister(void)
509 {
510 	unregister_net_sysctl_table(ip6_ctl_header);
511 }
512 #else
513 static int ip6_frags_ns_sysctl_register(struct net *net)
514 {
515 	return 0;
516 }
517 
518 static void ip6_frags_ns_sysctl_unregister(struct net *net)
519 {
520 }
521 
522 static int ip6_frags_sysctl_register(void)
523 {
524 	return 0;
525 }
526 
527 static void ip6_frags_sysctl_unregister(void)
528 {
529 }
530 #endif
531 
532 static int __net_init ipv6_frags_init_net(struct net *net)
533 {
534 	int res;
535 
536 	res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
537 	if (res < 0)
538 		return res;
539 
540 	net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
541 	net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
542 	net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
543 
544 	res = ip6_frags_ns_sysctl_register(net);
545 	if (res < 0)
546 		fqdir_exit(net->ipv6.fqdir);
547 	return res;
548 }
549 
550 static void __net_exit ipv6_frags_pre_exit_net(struct net *net)
551 {
552 	fqdir_pre_exit(net->ipv6.fqdir);
553 }
554 
555 static void __net_exit ipv6_frags_exit_net(struct net *net)
556 {
557 	ip6_frags_ns_sysctl_unregister(net);
558 	fqdir_exit(net->ipv6.fqdir);
559 }
560 
561 static struct pernet_operations ip6_frags_ops = {
562 	.init		= ipv6_frags_init_net,
563 	.pre_exit	= ipv6_frags_pre_exit_net,
564 	.exit		= ipv6_frags_exit_net,
565 };
566 
567 static const struct rhashtable_params ip6_rhash_params = {
568 	.head_offset		= offsetof(struct inet_frag_queue, node),
569 	.hashfn			= ip6frag_key_hashfn,
570 	.obj_hashfn		= ip6frag_obj_hashfn,
571 	.obj_cmpfn		= ip6frag_obj_cmpfn,
572 	.automatic_shrinking	= true,
573 };
574 
575 int __init ipv6_frag_init(void)
576 {
577 	int ret;
578 
579 	ip6_frags.constructor = ip6frag_init;
580 	ip6_frags.destructor = NULL;
581 	ip6_frags.qsize = sizeof(struct frag_queue);
582 	ip6_frags.frag_expire = ip6_frag_expire;
583 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
584 	ip6_frags.rhash_params = ip6_rhash_params;
585 	ret = inet_frags_init(&ip6_frags);
586 	if (ret)
587 		goto out;
588 
589 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
590 	if (ret)
591 		goto err_protocol;
592 
593 	ret = ip6_frags_sysctl_register();
594 	if (ret)
595 		goto err_sysctl;
596 
597 	ret = register_pernet_subsys(&ip6_frags_ops);
598 	if (ret)
599 		goto err_pernet;
600 
601 out:
602 	return ret;
603 
604 err_pernet:
605 	ip6_frags_sysctl_unregister();
606 err_sysctl:
607 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
608 err_protocol:
609 	inet_frags_fini(&ip6_frags);
610 	goto out;
611 }
612 
613 void ipv6_frag_exit(void)
614 {
615 	ip6_frags_sysctl_unregister();
616 	unregister_pernet_subsys(&ip6_frags_ops);
617 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
618 	inet_frags_fini(&ip6_frags);
619 }
620