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