• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IPv6 fragment reassembly for connection tracking
3  *
4  * Copyright (C)2004 USAGI/WIDE Project
5  *
6  * Author:
7  *	Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8  *
9  * Based on: net/ipv6/reassembly.c
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  */
16 
17 #define pr_fmt(fmt) "IPv6-nf: " fmt
18 
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/jiffies.h>
25 #include <linux/net.h>
26 #include <linux/list.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/ipv6.h>
30 #include <linux/icmpv6.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
33 
34 #include <net/sock.h>
35 #include <net/snmp.h>
36 #include <net/inet_frag.h>
37 
38 #include <net/ipv6.h>
39 #include <net/protocol.h>
40 #include <net/transp_v6.h>
41 #include <net/rawv6.h>
42 #include <net/ndisc.h>
43 #include <net/addrconf.h>
44 #include <net/inet_ecn.h>
45 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
46 #include <linux/sysctl.h>
47 #include <linux/netfilter.h>
48 #include <linux/netfilter_ipv6.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
52 
53 static const char nf_frags_cache_name[] = "nf-frags";
54 
55 struct nf_ct_frag6_skb_cb
56 {
57 	struct inet6_skb_parm	h;
58 	int			offset;
59 	struct sk_buff		*orig;
60 };
61 
62 #define NFCT_FRAG6_CB(skb)	((struct nf_ct_frag6_skb_cb*)((skb)->cb))
63 
64 static struct inet_frags nf_frags;
65 
66 #ifdef CONFIG_SYSCTL
67 static int zero;
68 
69 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
70 	{
71 		.procname	= "nf_conntrack_frag6_timeout",
72 		.data		= &init_net.nf_frag.frags.timeout,
73 		.maxlen		= sizeof(unsigned int),
74 		.mode		= 0644,
75 		.proc_handler	= proc_dointvec_jiffies,
76 	},
77 	{
78 		.procname	= "nf_conntrack_frag6_low_thresh",
79 		.data		= &init_net.nf_frag.frags.low_thresh,
80 		.maxlen		= sizeof(unsigned int),
81 		.mode		= 0644,
82 		.proc_handler	= proc_dointvec_minmax,
83 		.extra1		= &zero,
84 		.extra2		= &init_net.nf_frag.frags.high_thresh
85 	},
86 	{
87 		.procname	= "nf_conntrack_frag6_high_thresh",
88 		.data		= &init_net.nf_frag.frags.high_thresh,
89 		.maxlen		= sizeof(unsigned int),
90 		.mode		= 0644,
91 		.proc_handler	= proc_dointvec_minmax,
92 		.extra1		= &init_net.nf_frag.frags.low_thresh
93 	},
94 	{ }
95 };
96 
nf_ct_frag6_sysctl_register(struct net * net)97 static int nf_ct_frag6_sysctl_register(struct net *net)
98 {
99 	struct ctl_table *table;
100 	struct ctl_table_header *hdr;
101 
102 	table = nf_ct_frag6_sysctl_table;
103 	if (!net_eq(net, &init_net)) {
104 		table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
105 				GFP_KERNEL);
106 		if (table == NULL)
107 			goto err_alloc;
108 
109 		table[0].data = &net->nf_frag.frags.timeout;
110 		table[1].data = &net->nf_frag.frags.low_thresh;
111 		table[1].extra2 = &net->nf_frag.frags.high_thresh;
112 		table[2].data = &net->nf_frag.frags.high_thresh;
113 		table[2].extra1 = &net->nf_frag.frags.low_thresh;
114 		table[2].extra2 = &init_net.nf_frag.frags.high_thresh;
115 	}
116 
117 	hdr = register_net_sysctl(net, "net/netfilter", table);
118 	if (hdr == NULL)
119 		goto err_reg;
120 
121 	net->nf_frag.sysctl.frags_hdr = hdr;
122 	return 0;
123 
124 err_reg:
125 	if (!net_eq(net, &init_net))
126 		kfree(table);
127 err_alloc:
128 	return -ENOMEM;
129 }
130 
nf_ct_frags6_sysctl_unregister(struct net * net)131 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
132 {
133 	struct ctl_table *table;
134 
135 	table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg;
136 	unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr);
137 	if (!net_eq(net, &init_net))
138 		kfree(table);
139 }
140 
141 #else
nf_ct_frag6_sysctl_register(struct net * net)142 static int nf_ct_frag6_sysctl_register(struct net *net)
143 {
144 	return 0;
145 }
nf_ct_frags6_sysctl_unregister(struct net * net)146 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
147 {
148 }
149 #endif
150 
ip6_frag_ecn(const struct ipv6hdr * ipv6h)151 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
152 {
153 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
154 }
155 
nf_hash_frag(__be32 id,const struct in6_addr * saddr,const struct in6_addr * daddr)156 static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
157 				 const struct in6_addr *daddr)
158 {
159 	net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
160 	return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
161 			    (__force u32)id, nf_frags.rnd);
162 }
163 
164 
nf_hashfn(const struct inet_frag_queue * q)165 static unsigned int nf_hashfn(const struct inet_frag_queue *q)
166 {
167 	const struct frag_queue *nq;
168 
169 	nq = container_of(q, struct frag_queue, q);
170 	return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
171 }
172 
nf_skb_free(struct sk_buff * skb)173 static void nf_skb_free(struct sk_buff *skb)
174 {
175 	if (NFCT_FRAG6_CB(skb)->orig)
176 		kfree_skb(NFCT_FRAG6_CB(skb)->orig);
177 }
178 
nf_ct_frag6_expire(unsigned long data)179 static void nf_ct_frag6_expire(unsigned long data)
180 {
181 	struct frag_queue *fq;
182 	struct net *net;
183 
184 	fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
185 	net = container_of(fq->q.net, struct net, nf_frag.frags);
186 
187 	ip6_expire_frag_queue(net, fq, &nf_frags);
188 }
189 
190 /* Creation primitives. */
fq_find(struct net * net,__be32 id,u32 user,struct in6_addr * src,struct in6_addr * dst,int iif,u8 ecn)191 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
192 					 u32 user, struct in6_addr *src,
193 					 struct in6_addr *dst, int iif, u8 ecn)
194 {
195 	struct inet_frag_queue *q;
196 	struct ip6_create_arg arg;
197 	unsigned int hash;
198 
199 	arg.id = id;
200 	arg.user = user;
201 	arg.src = src;
202 	arg.dst = dst;
203 	arg.iif = iif;
204 	arg.ecn = ecn;
205 
206 	local_bh_disable();
207 	hash = nf_hash_frag(id, src, dst);
208 
209 	q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
210 	local_bh_enable();
211 	if (IS_ERR_OR_NULL(q)) {
212 		inet_frag_maybe_warn_overflow(q, pr_fmt());
213 		return NULL;
214 	}
215 	return container_of(q, struct frag_queue, q);
216 }
217 
218 
nf_ct_frag6_queue(struct frag_queue * fq,struct sk_buff * skb,const struct frag_hdr * fhdr,int nhoff)219 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
220 			     const struct frag_hdr *fhdr, int nhoff)
221 {
222 	struct sk_buff *prev, *next;
223 	unsigned int payload_len;
224 	int offset, end;
225 	u8 ecn;
226 
227 	if (fq->q.flags & INET_FRAG_COMPLETE) {
228 		pr_debug("Already completed\n");
229 		goto err;
230 	}
231 
232 	payload_len = ntohs(ipv6_hdr(skb)->payload_len);
233 
234 	offset = ntohs(fhdr->frag_off) & ~0x7;
235 	end = offset + (payload_len -
236 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
237 
238 	if ((unsigned int)end > IPV6_MAXPLEN) {
239 		pr_debug("offset is too large.\n");
240 		return -1;
241 	}
242 
243 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
244 
245 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
246 		const unsigned char *nh = skb_network_header(skb);
247 		skb->csum = csum_sub(skb->csum,
248 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
249 						  0));
250 	}
251 
252 	/* Is this the final fragment? */
253 	if (!(fhdr->frag_off & htons(IP6_MF))) {
254 		/* If we already have some bits beyond end
255 		 * or have different end, the segment is corrupted.
256 		 */
257 		if (end < fq->q.len ||
258 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
259 			pr_debug("already received last fragment\n");
260 			goto err;
261 		}
262 		fq->q.flags |= INET_FRAG_LAST_IN;
263 		fq->q.len = end;
264 	} else {
265 		/* Check if the fragment is rounded to 8 bytes.
266 		 * Required by the RFC.
267 		 */
268 		if (end & 0x7) {
269 			/* RFC2460 says always send parameter problem in
270 			 * this case. -DaveM
271 			 */
272 			pr_debug("end of fragment not rounded to 8 bytes.\n");
273 			return -1;
274 		}
275 		if (end > fq->q.len) {
276 			/* Some bits beyond end -> corruption. */
277 			if (fq->q.flags & INET_FRAG_LAST_IN) {
278 				pr_debug("last packet already reached.\n");
279 				goto err;
280 			}
281 			fq->q.len = end;
282 		}
283 	}
284 
285 	if (end == offset)
286 		goto err;
287 
288 	/* Point into the IP datagram 'data' part. */
289 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
290 		pr_debug("queue: message is too short.\n");
291 		goto err;
292 	}
293 	if (pskb_trim_rcsum(skb, end - offset)) {
294 		pr_debug("Can't trim\n");
295 		goto err;
296 	}
297 
298 	/* Find out which fragments are in front and at the back of us
299 	 * in the chain of fragments so far.  We must know where to put
300 	 * this fragment, right?
301 	 */
302 	prev = fq->q.fragments_tail;
303 	if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
304 		next = NULL;
305 		goto found;
306 	}
307 	prev = NULL;
308 	for (next = fq->q.fragments; next != NULL; next = next->next) {
309 		if (NFCT_FRAG6_CB(next)->offset >= offset)
310 			break;	/* bingo! */
311 		prev = next;
312 	}
313 
314 found:
315 	/* RFC5722, Section 4:
316 	 *                                  When reassembling an IPv6 datagram, if
317 	 *   one or more its constituent fragments is determined to be an
318 	 *   overlapping fragment, the entire datagram (and any constituent
319 	 *   fragments, including those not yet received) MUST be silently
320 	 *   discarded.
321 	 */
322 
323 	/* Check for overlap with preceding fragment. */
324 	if (prev &&
325 	    (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
326 		goto discard_fq;
327 
328 	/* Look for overlap with succeeding segment. */
329 	if (next && NFCT_FRAG6_CB(next)->offset < end)
330 		goto discard_fq;
331 
332 	NFCT_FRAG6_CB(skb)->offset = offset;
333 
334 	/* Insert this fragment in the chain of fragments. */
335 	skb->next = next;
336 	if (!next)
337 		fq->q.fragments_tail = skb;
338 	if (prev)
339 		prev->next = skb;
340 	else
341 		fq->q.fragments = skb;
342 
343 	if (skb->dev) {
344 		fq->iif = skb->dev->ifindex;
345 		skb->dev = NULL;
346 	}
347 	fq->q.stamp = skb->tstamp;
348 	fq->q.meat += skb->len;
349 	fq->ecn |= ecn;
350 	if (payload_len > fq->q.max_size)
351 		fq->q.max_size = payload_len;
352 	add_frag_mem_limit(&fq->q, skb->truesize);
353 
354 	/* The first fragment.
355 	 * nhoffset is obtained from the first fragment, of course.
356 	 */
357 	if (offset == 0) {
358 		fq->nhoffset = nhoff;
359 		fq->q.flags |= INET_FRAG_FIRST_IN;
360 	}
361 
362 	return 0;
363 
364 discard_fq:
365 	inet_frag_kill(&fq->q, &nf_frags);
366 err:
367 	return -1;
368 }
369 
370 /*
371  *	Check if this packet is complete.
372  *	Returns NULL on failure by any reason, and pointer
373  *	to current nexthdr field in reassembled frame.
374  *
375  *	It is called with locked fq, and caller must check that
376  *	queue is eligible for reassembly i.e. it is not COMPLETE,
377  *	the last and the first frames arrived and all the bits are here.
378  */
379 static struct sk_buff *
nf_ct_frag6_reasm(struct frag_queue * fq,struct net_device * dev)380 nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
381 {
382 	struct sk_buff *fp, *op, *head = fq->q.fragments;
383 	int    payload_len;
384 	u8 ecn;
385 
386 	inet_frag_kill(&fq->q, &nf_frags);
387 
388 	WARN_ON(head == NULL);
389 	WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
390 
391 	ecn = ip_frag_ecn_table[fq->ecn];
392 	if (unlikely(ecn == 0xff))
393 		goto out_fail;
394 
395 	/* Unfragmented part is taken from the first segment. */
396 	payload_len = ((head->data - skb_network_header(head)) -
397 		       sizeof(struct ipv6hdr) + fq->q.len -
398 		       sizeof(struct frag_hdr));
399 	if (payload_len > IPV6_MAXPLEN) {
400 		pr_debug("payload len is too large.\n");
401 		goto out_oversize;
402 	}
403 
404 	/* Head of list must not be cloned. */
405 	if (skb_unclone(head, GFP_ATOMIC)) {
406 		pr_debug("skb is cloned but can't expand head");
407 		goto out_oom;
408 	}
409 
410 	/* If the first fragment is fragmented itself, we split
411 	 * it to two chunks: the first with data and paged part
412 	 * and the second, holding only fragments. */
413 	if (skb_has_frag_list(head)) {
414 		struct sk_buff *clone;
415 		int i, plen = 0;
416 
417 		clone = alloc_skb(0, GFP_ATOMIC);
418 		if (clone == NULL)
419 			goto out_oom;
420 
421 		clone->next = head->next;
422 		head->next = clone;
423 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
424 		skb_frag_list_init(head);
425 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
426 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
427 		clone->len = clone->data_len = head->data_len - plen;
428 		head->data_len -= clone->len;
429 		head->len -= clone->len;
430 		clone->csum = 0;
431 		clone->ip_summed = head->ip_summed;
432 
433 		NFCT_FRAG6_CB(clone)->orig = NULL;
434 		add_frag_mem_limit(&fq->q, clone->truesize);
435 	}
436 
437 	/* We have to remove fragment header from datagram and to relocate
438 	 * header in order to calculate ICV correctly. */
439 	skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
440 	memmove(head->head + sizeof(struct frag_hdr), head->head,
441 		(head->data - head->head) - sizeof(struct frag_hdr));
442 	head->mac_header += sizeof(struct frag_hdr);
443 	head->network_header += sizeof(struct frag_hdr);
444 
445 	skb_shinfo(head)->frag_list = head->next;
446 	skb_reset_transport_header(head);
447 	skb_push(head, head->data - skb_network_header(head));
448 
449 	for (fp=head->next; fp; fp = fp->next) {
450 		head->data_len += fp->len;
451 		head->len += fp->len;
452 		if (head->ip_summed != fp->ip_summed)
453 			head->ip_summed = CHECKSUM_NONE;
454 		else if (head->ip_summed == CHECKSUM_COMPLETE)
455 			head->csum = csum_add(head->csum, fp->csum);
456 		head->truesize += fp->truesize;
457 	}
458 	sub_frag_mem_limit(&fq->q, head->truesize);
459 
460 	head->ignore_df = 1;
461 	head->next = NULL;
462 	head->dev = dev;
463 	head->tstamp = fq->q.stamp;
464 	ipv6_hdr(head)->payload_len = htons(payload_len);
465 	ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
466 	IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
467 
468 	/* Yes, and fold redundant checksum back. 8) */
469 	if (head->ip_summed == CHECKSUM_COMPLETE)
470 		head->csum = csum_partial(skb_network_header(head),
471 					  skb_network_header_len(head),
472 					  head->csum);
473 
474 	fq->q.fragments = NULL;
475 	fq->q.fragments_tail = NULL;
476 
477 	/* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
478 	fp = skb_shinfo(head)->frag_list;
479 	if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
480 		/* at above code, head skb is divided into two skbs. */
481 		fp = fp->next;
482 
483 	op = NFCT_FRAG6_CB(head)->orig;
484 	for (; fp; fp = fp->next) {
485 		struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
486 
487 		op->next = orig;
488 		op = orig;
489 		NFCT_FRAG6_CB(fp)->orig = NULL;
490 	}
491 
492 	return head;
493 
494 out_oversize:
495 	net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
496 			    payload_len);
497 	goto out_fail;
498 out_oom:
499 	net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
500 out_fail:
501 	return NULL;
502 }
503 
504 /*
505  * find the header just before Fragment Header.
506  *
507  * if success return 0 and set ...
508  * (*prevhdrp): the value of "Next Header Field" in the header
509  *		just before Fragment Header.
510  * (*prevhoff): the offset of "Next Header Field" in the header
511  *		just before Fragment Header.
512  * (*fhoff)   : the offset of Fragment Header.
513  *
514  * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
515  *
516  */
517 static int
find_prev_fhdr(struct sk_buff * skb,u8 * prevhdrp,int * prevhoff,int * fhoff)518 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
519 {
520 	u8 nexthdr = ipv6_hdr(skb)->nexthdr;
521 	const int netoff = skb_network_offset(skb);
522 	u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
523 	int start = netoff + sizeof(struct ipv6hdr);
524 	int len = skb->len - start;
525 	u8 prevhdr = NEXTHDR_IPV6;
526 
527 	while (nexthdr != NEXTHDR_FRAGMENT) {
528 		struct ipv6_opt_hdr hdr;
529 		int hdrlen;
530 
531 		if (!ipv6_ext_hdr(nexthdr)) {
532 			return -1;
533 		}
534 		if (nexthdr == NEXTHDR_NONE) {
535 			pr_debug("next header is none\n");
536 			return -1;
537 		}
538 		if (len < (int)sizeof(struct ipv6_opt_hdr)) {
539 			pr_debug("too short\n");
540 			return -1;
541 		}
542 		if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
543 			BUG();
544 		if (nexthdr == NEXTHDR_AUTH)
545 			hdrlen = (hdr.hdrlen+2)<<2;
546 		else
547 			hdrlen = ipv6_optlen(&hdr);
548 
549 		prevhdr = nexthdr;
550 		prev_nhoff = start;
551 
552 		nexthdr = hdr.nexthdr;
553 		len -= hdrlen;
554 		start += hdrlen;
555 	}
556 
557 	if (len < 0)
558 		return -1;
559 
560 	*prevhdrp = prevhdr;
561 	*prevhoff = prev_nhoff;
562 	*fhoff = start;
563 
564 	return 0;
565 }
566 
nf_ct_frag6_gather(struct sk_buff * skb,u32 user)567 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
568 {
569 	struct sk_buff *clone;
570 	struct net_device *dev = skb->dev;
571 	struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev)
572 				       : dev_net(skb->dev);
573 	struct frag_hdr *fhdr;
574 	struct frag_queue *fq;
575 	struct ipv6hdr *hdr;
576 	int fhoff, nhoff;
577 	u8 prevhdr;
578 	struct sk_buff *ret_skb = NULL;
579 
580 	/* Jumbo payload inhibits frag. header */
581 	if (ipv6_hdr(skb)->payload_len == 0) {
582 		pr_debug("payload len = 0\n");
583 		return skb;
584 	}
585 
586 	if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
587 		return skb;
588 
589 	clone = skb_clone(skb, GFP_ATOMIC);
590 	if (clone == NULL) {
591 		pr_debug("Can't clone skb\n");
592 		return skb;
593 	}
594 
595 	NFCT_FRAG6_CB(clone)->orig = skb;
596 
597 	if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
598 		pr_debug("message is too short.\n");
599 		goto ret_orig;
600 	}
601 
602 	skb_set_transport_header(clone, fhoff);
603 	hdr = ipv6_hdr(clone);
604 	fhdr = (struct frag_hdr *)skb_transport_header(clone);
605 
606 	fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
607 		     skb->dev ? skb->dev->ifindex : 0, ip6_frag_ecn(hdr));
608 	if (fq == NULL) {
609 		pr_debug("Can't find and can't create new queue\n");
610 		goto ret_orig;
611 	}
612 
613 	spin_lock_bh(&fq->q.lock);
614 
615 	if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
616 		spin_unlock_bh(&fq->q.lock);
617 		pr_debug("Can't insert skb to queue\n");
618 		inet_frag_put(&fq->q, &nf_frags);
619 		goto ret_orig;
620 	}
621 
622 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
623 	    fq->q.meat == fq->q.len) {
624 		ret_skb = nf_ct_frag6_reasm(fq, dev);
625 		if (ret_skb == NULL)
626 			pr_debug("Can't reassemble fragmented packets\n");
627 	}
628 	spin_unlock_bh(&fq->q.lock);
629 
630 	inet_frag_put(&fq->q, &nf_frags);
631 	return ret_skb;
632 
633 ret_orig:
634 	kfree_skb(clone);
635 	return skb;
636 }
637 
nf_ct_frag6_consume_orig(struct sk_buff * skb)638 void nf_ct_frag6_consume_orig(struct sk_buff *skb)
639 {
640 	struct sk_buff *s, *s2;
641 
642 	for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
643 		s2 = s->next;
644 		s->next = NULL;
645 		consume_skb(s);
646 		s = s2;
647 	}
648 }
649 
nf_ct_net_init(struct net * net)650 static int nf_ct_net_init(struct net *net)
651 {
652 	net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
653 	net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
654 	net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
655 	inet_frags_init_net(&net->nf_frag.frags);
656 
657 	return nf_ct_frag6_sysctl_register(net);
658 }
659 
nf_ct_net_exit(struct net * net)660 static void nf_ct_net_exit(struct net *net)
661 {
662 	nf_ct_frags6_sysctl_unregister(net);
663 	inet_frags_exit_net(&net->nf_frag.frags, &nf_frags);
664 }
665 
666 static struct pernet_operations nf_ct_net_ops = {
667 	.init = nf_ct_net_init,
668 	.exit = nf_ct_net_exit,
669 };
670 
nf_ct_frag6_init(void)671 int nf_ct_frag6_init(void)
672 {
673 	int ret = 0;
674 
675 	nf_frags.hashfn = nf_hashfn;
676 	nf_frags.constructor = ip6_frag_init;
677 	nf_frags.destructor = NULL;
678 	nf_frags.skb_free = nf_skb_free;
679 	nf_frags.qsize = sizeof(struct frag_queue);
680 	nf_frags.match = ip6_frag_match;
681 	nf_frags.frag_expire = nf_ct_frag6_expire;
682 	nf_frags.frags_cache_name = nf_frags_cache_name;
683 	ret = inet_frags_init(&nf_frags);
684 	if (ret)
685 		goto out;
686 	ret = register_pernet_subsys(&nf_ct_net_ops);
687 	if (ret)
688 		inet_frags_fini(&nf_frags);
689 
690 out:
691 	return ret;
692 }
693 
nf_ct_frag6_cleanup(void)694 void nf_ct_frag6_cleanup(void)
695 {
696 	unregister_pernet_subsys(&nf_ct_net_ops);
697 	inet_frags_fini(&nf_frags);
698 }
699