]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv6/netfilter/nf_conntrack_reasm.c
inet: frags: use INET_FRAG_EVICTED to prevent icmp messages
[mirror_ubuntu-bionic-kernel.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
9fb9cbb1 53
9fb9cbb1
YK
54struct nf_ct_frag6_skb_cb
55{
56 struct inet6_skb_parm h;
57 int offset;
58 struct sk_buff *orig;
59};
60
61#define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
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
1e4b8287
PE
172static void nf_skb_free(struct sk_buff *skb)
173{
174 if (NFCT_FRAG6_CB(skb)->orig)
175 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
176}
177
9fb9cbb1
YK
178static void nf_ct_frag6_expire(unsigned long data)
179{
b836c99f
AW
180 struct frag_queue *fq;
181 struct net *net;
9fb9cbb1 182
b836c99f
AW
183 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
184 net = container_of(fq->q.net, struct net, nf_frag.frags);
9fb9cbb1 185
b836c99f 186 ip6_expire_frag_queue(net, fq, &nf_frags);
9fb9cbb1
YK
187}
188
189/* Creation primitives. */
b836c99f
AW
190static inline struct frag_queue *fq_find(struct net *net, __be32 id,
191 u32 user, struct in6_addr *src,
b8dd6a22 192 struct in6_addr *dst, u8 ecn)
9fb9cbb1 193{
2588fe1d 194 struct inet_frag_queue *q;
c6fda282 195 struct ip6_create_arg arg;
abd6523d 196 unsigned int hash;
9fb9cbb1 197
c6fda282 198 arg.id = id;
0b5ccb2e 199 arg.user = user;
c6fda282
PE
200 arg.src = src;
201 arg.dst = dst;
b8dd6a22 202 arg.ecn = ecn;
9a375803 203
ab1c724f 204 local_bh_disable();
b1190570 205 hash = nf_hash_frag(id, src, dst);
9fb9cbb1 206
c038a767 207 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
b9c69896 208 local_bh_enable();
5a3da1fe
HFS
209 if (IS_ERR_OR_NULL(q)) {
210 inet_frag_maybe_warn_overflow(q, pr_fmt());
211 return NULL;
212 }
b836c99f 213 return container_of(q, struct frag_queue, q);
9fb9cbb1
YK
214}
215
9fb9cbb1 216
b836c99f 217static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
58c0fb0d 218 const struct frag_hdr *fhdr, int nhoff)
9fb9cbb1
YK
219{
220 struct sk_buff *prev, *next;
4cdd3408 221 unsigned int payload_len;
9fb9cbb1 222 int offset, end;
b8dd6a22 223 u8 ecn;
9fb9cbb1 224
06aa8b8a 225 if (fq->q.flags & INET_FRAG_COMPLETE) {
698f9315 226 pr_debug("Already completed\n");
9fb9cbb1
YK
227 goto err;
228 }
229
4cdd3408
PM
230 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
231
9fb9cbb1 232 offset = ntohs(fhdr->frag_off) & ~0x7;
4cdd3408 233 end = offset + (payload_len -
0660e03f 234 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
9fb9cbb1
YK
235
236 if ((unsigned int)end > IPV6_MAXPLEN) {
0d53778e 237 pr_debug("offset is too large.\n");
1ab1457c 238 return -1;
9fb9cbb1
YK
239 }
240
b8dd6a22
HFS
241 ecn = ip6_frag_ecn(ipv6_hdr(skb));
242
d56f90a7
ACM
243 if (skb->ip_summed == CHECKSUM_COMPLETE) {
244 const unsigned char *nh = skb_network_header(skb);
1ab1457c 245 skb->csum = csum_sub(skb->csum,
d56f90a7 246 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
9fb9cbb1 247 0));
d56f90a7 248 }
9fb9cbb1
YK
249
250 /* Is this the final fragment? */
251 if (!(fhdr->frag_off & htons(IP6_MF))) {
252 /* If we already have some bits beyond end
253 * or have different end, the segment is corrupted.
254 */
5ab11c98 255 if (end < fq->q.len ||
06aa8b8a 256 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
0d53778e 257 pr_debug("already received last fragment\n");
9fb9cbb1
YK
258 goto err;
259 }
06aa8b8a 260 fq->q.flags |= INET_FRAG_LAST_IN;
5ab11c98 261 fq->q.len = end;
9fb9cbb1
YK
262 } else {
263 /* Check if the fragment is rounded to 8 bytes.
264 * Required by the RFC.
265 */
266 if (end & 0x7) {
267 /* RFC2460 says always send parameter problem in
268 * this case. -DaveM
269 */
0d53778e 270 pr_debug("end of fragment not rounded to 8 bytes.\n");
9fb9cbb1
YK
271 return -1;
272 }
5ab11c98 273 if (end > fq->q.len) {
9fb9cbb1 274 /* Some bits beyond end -> corruption. */
06aa8b8a 275 if (fq->q.flags & INET_FRAG_LAST_IN) {
0d53778e 276 pr_debug("last packet already reached.\n");
9fb9cbb1
YK
277 goto err;
278 }
5ab11c98 279 fq->q.len = end;
9fb9cbb1
YK
280 }
281 }
282
283 if (end == offset)
284 goto err;
285
286 /* Point into the IP datagram 'data' part. */
287 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
0d53778e 288 pr_debug("queue: message is too short.\n");
9fb9cbb1
YK
289 goto err;
290 }
b38dfee3 291 if (pskb_trim_rcsum(skb, end - offset)) {
0d53778e 292 pr_debug("Can't trim\n");
b38dfee3 293 goto err;
9fb9cbb1
YK
294 }
295
296 /* Find out which fragments are in front and at the back of us
297 * in the chain of fragments so far. We must know where to put
298 * this fragment, right?
299 */
ea8fbe8f
CG
300 prev = fq->q.fragments_tail;
301 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
302 next = NULL;
303 goto found;
304 }
9fb9cbb1 305 prev = NULL;
5ab11c98 306 for (next = fq->q.fragments; next != NULL; next = next->next) {
9fb9cbb1
YK
307 if (NFCT_FRAG6_CB(next)->offset >= offset)
308 break; /* bingo! */
309 prev = next;
310 }
311
ea8fbe8f 312found:
1ee89bd0
ND
313 /* RFC5722, Section 4:
314 * When reassembling an IPv6 datagram, if
315 * one or more its constituent fragments is determined to be an
316 * overlapping fragment, the entire datagram (and any constituent
317 * fragments, including those not yet received) MUST be silently
318 * discarded.
9fb9cbb1 319 */
9fb9cbb1 320
1ee89bd0
ND
321 /* Check for overlap with preceding fragment. */
322 if (prev &&
22e091e5 323 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
1ee89bd0 324 goto discard_fq;
9fb9cbb1 325
1ee89bd0
ND
326 /* Look for overlap with succeeding segment. */
327 if (next && NFCT_FRAG6_CB(next)->offset < end)
328 goto discard_fq;
9fb9cbb1
YK
329
330 NFCT_FRAG6_CB(skb)->offset = offset;
331
332 /* Insert this fragment in the chain of fragments. */
333 skb->next = next;
ea8fbe8f
CG
334 if (!next)
335 fq->q.fragments_tail = skb;
9fb9cbb1
YK
336 if (prev)
337 prev->next = skb;
338 else
5ab11c98 339 fq->q.fragments = skb;
9fb9cbb1 340
97cf00e9
HX
341 if (skb->dev) {
342 fq->iif = skb->dev->ifindex;
343 skb->dev = NULL;
344 }
5ab11c98
PE
345 fq->q.stamp = skb->tstamp;
346 fq->q.meat += skb->len;
b8dd6a22 347 fq->ecn |= ecn;
4cdd3408
PM
348 if (payload_len > fq->q.max_size)
349 fq->q.max_size = payload_len;
d433673e 350 add_frag_mem_limit(&fq->q, skb->truesize);
9fb9cbb1
YK
351
352 /* The first fragment.
353 * nhoffset is obtained from the first fragment, of course.
354 */
355 if (offset == 0) {
356 fq->nhoffset = nhoff;
06aa8b8a 357 fq->q.flags |= INET_FRAG_FIRST_IN;
9fb9cbb1 358 }
3ef0eb0d 359
9fb9cbb1
YK
360 return 0;
361
1ee89bd0 362discard_fq:
b836c99f 363 inet_frag_kill(&fq->q, &nf_frags);
9fb9cbb1
YK
364err:
365 return -1;
366}
367
368/*
369 * Check if this packet is complete.
370 * Returns NULL on failure by any reason, and pointer
371 * to current nexthdr field in reassembled frame.
372 *
373 * It is called with locked fq, and caller must check that
374 * queue is eligible for reassembly i.e. it is not COMPLETE,
375 * the last and the first frames arrived and all the bits are here.
376 */
377static struct sk_buff *
b836c99f 378nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
9fb9cbb1 379{
5ab11c98 380 struct sk_buff *fp, *op, *head = fq->q.fragments;
9fb9cbb1 381 int payload_len;
b8dd6a22 382 u8 ecn;
9fb9cbb1 383
b836c99f 384 inet_frag_kill(&fq->q, &nf_frags);
9fb9cbb1 385
547b792c
IJ
386 WARN_ON(head == NULL);
387 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
9fb9cbb1 388
b8dd6a22
HFS
389 ecn = ip_frag_ecn_table[fq->ecn];
390 if (unlikely(ecn == 0xff))
391 goto out_fail;
392
9fb9cbb1 393 /* Unfragmented part is taken from the first segment. */
d56f90a7 394 payload_len = ((head->data - skb_network_header(head)) -
5ab11c98 395 sizeof(struct ipv6hdr) + fq->q.len -
d56f90a7 396 sizeof(struct frag_hdr));
9fb9cbb1 397 if (payload_len > IPV6_MAXPLEN) {
0d53778e 398 pr_debug("payload len is too large.\n");
9fb9cbb1
YK
399 goto out_oversize;
400 }
401
402 /* Head of list must not be cloned. */
14bbd6a5 403 if (skb_unclone(head, GFP_ATOMIC)) {
0d53778e 404 pr_debug("skb is cloned but can't expand head");
9fb9cbb1
YK
405 goto out_oom;
406 }
407
408 /* If the first fragment is fragmented itself, we split
409 * it to two chunks: the first with data and paged part
410 * and the second, holding only fragments. */
21dc3301 411 if (skb_has_frag_list(head)) {
9fb9cbb1
YK
412 struct sk_buff *clone;
413 int i, plen = 0;
414
0a9ee813
JP
415 clone = alloc_skb(0, GFP_ATOMIC);
416 if (clone == NULL)
9fb9cbb1 417 goto out_oom;
0a9ee813 418
9fb9cbb1
YK
419 clone->next = head->next;
420 head->next = clone;
421 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
343a9972 422 skb_frag_list_init(head);
9e903e08
ED
423 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
424 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
9fb9cbb1
YK
425 clone->len = clone->data_len = head->data_len - plen;
426 head->data_len -= clone->len;
427 head->len -= clone->len;
428 clone->csum = 0;
429 clone->ip_summed = head->ip_summed;
430
431 NFCT_FRAG6_CB(clone)->orig = NULL;
d433673e 432 add_frag_mem_limit(&fq->q, clone->truesize);
9fb9cbb1
YK
433 }
434
435 /* We have to remove fragment header from datagram and to relocate
436 * header in order to calculate ICV correctly. */
bff9b61c 437 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
1ab1457c 438 memmove(head->head + sizeof(struct frag_hdr), head->head,
9fb9cbb1 439 (head->data - head->head) - sizeof(struct frag_hdr));
b0e380b1
ACM
440 head->mac_header += sizeof(struct frag_hdr);
441 head->network_header += sizeof(struct frag_hdr);
9fb9cbb1
YK
442
443 skb_shinfo(head)->frag_list = head->next;
badff6d0 444 skb_reset_transport_header(head);
d56f90a7 445 skb_push(head, head->data - skb_network_header(head));
9fb9cbb1
YK
446
447 for (fp=head->next; fp; fp = fp->next) {
448 head->data_len += fp->len;
449 head->len += fp->len;
450 if (head->ip_summed != fp->ip_summed)
451 head->ip_summed = CHECKSUM_NONE;
84fa7933 452 else if (head->ip_summed == CHECKSUM_COMPLETE)
9fb9cbb1
YK
453 head->csum = csum_add(head->csum, fp->csum);
454 head->truesize += fp->truesize;
9fb9cbb1 455 }
d433673e 456 sub_frag_mem_limit(&fq->q, head->truesize);
9fb9cbb1 457
60ff7467 458 head->ignore_df = 1;
9fb9cbb1
YK
459 head->next = NULL;
460 head->dev = dev;
5ab11c98 461 head->tstamp = fq->q.stamp;
0660e03f 462 ipv6_hdr(head)->payload_len = htons(payload_len);
b8dd6a22 463 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
4cdd3408 464 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
9fb9cbb1
YK
465
466 /* Yes, and fold redundant checksum back. 8) */
84fa7933 467 if (head->ip_summed == CHECKSUM_COMPLETE)
d56f90a7 468 head->csum = csum_partial(skb_network_header(head),
cfe1fc77 469 skb_network_header_len(head),
d56f90a7 470 head->csum);
9fb9cbb1 471
5ab11c98 472 fq->q.fragments = NULL;
ea8fbe8f 473 fq->q.fragments_tail = NULL;
9fb9cbb1
YK
474
475 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
476 fp = skb_shinfo(head)->frag_list;
9e2dcf72 477 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
9fb9cbb1
YK
478 /* at above code, head skb is divided into two skbs. */
479 fp = fp->next;
480
481 op = NFCT_FRAG6_CB(head)->orig;
482 for (; fp; fp = fp->next) {
483 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
484
485 op->next = orig;
486 op = orig;
487 NFCT_FRAG6_CB(fp)->orig = NULL;
488 }
489
490 return head;
491
492out_oversize:
e87cc472
JP
493 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
494 payload_len);
9fb9cbb1
YK
495 goto out_fail;
496out_oom:
e87cc472 497 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
9fb9cbb1
YK
498out_fail:
499 return NULL;
500}
501
502/*
503 * find the header just before Fragment Header.
504 *
505 * if success return 0 and set ...
506 * (*prevhdrp): the value of "Next Header Field" in the header
507 * just before Fragment Header.
508 * (*prevhoff): the offset of "Next Header Field" in the header
509 * just before Fragment Header.
510 * (*fhoff) : the offset of Fragment Header.
511 *
512 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
513 *
514 */
515static int
516find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
517{
0660e03f 518 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
6b88dd96
ACM
519 const int netoff = skb_network_offset(skb);
520 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
521 int start = netoff + sizeof(struct ipv6hdr);
9fb9cbb1
YK
522 int len = skb->len - start;
523 u8 prevhdr = NEXTHDR_IPV6;
524
1ab1457c
YH
525 while (nexthdr != NEXTHDR_FRAGMENT) {
526 struct ipv6_opt_hdr hdr;
527 int hdrlen;
9fb9cbb1
YK
528
529 if (!ipv6_ext_hdr(nexthdr)) {
530 return -1;
531 }
1ab1457c 532 if (nexthdr == NEXTHDR_NONE) {
0d53778e 533 pr_debug("next header is none\n");
9fb9cbb1
YK
534 return -1;
535 }
d1238d53
CP
536 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
537 pr_debug("too short\n");
538 return -1;
539 }
1ab1457c
YH
540 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
541 BUG();
542 if (nexthdr == NEXTHDR_AUTH)
543 hdrlen = (hdr.hdrlen+2)<<2;
544 else
545 hdrlen = ipv6_optlen(&hdr);
9fb9cbb1
YK
546
547 prevhdr = nexthdr;
548 prev_nhoff = start;
549
1ab1457c
YH
550 nexthdr = hdr.nexthdr;
551 len -= hdrlen;
552 start += hdrlen;
553 }
9fb9cbb1
YK
554
555 if (len < 0)
556 return -1;
557
558 *prevhdrp = prevhdr;
559 *prevhoff = prev_nhoff;
560 *fhoff = start;
561
562 return 0;
563}
564
0b5ccb2e 565struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
9fb9cbb1 566{
1ab1457c 567 struct sk_buff *clone;
9fb9cbb1 568 struct net_device *dev = skb->dev;
c038a767
AW
569 struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev)
570 : dev_net(skb->dev);
9fb9cbb1 571 struct frag_hdr *fhdr;
b836c99f 572 struct frag_queue *fq;
9fb9cbb1
YK
573 struct ipv6hdr *hdr;
574 int fhoff, nhoff;
575 u8 prevhdr;
576 struct sk_buff *ret_skb = NULL;
577
578 /* Jumbo payload inhibits frag. header */
0660e03f 579 if (ipv6_hdr(skb)->payload_len == 0) {
0d53778e 580 pr_debug("payload len = 0\n");
9fb9cbb1
YK
581 return skb;
582 }
583
584 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
585 return skb;
586
587 clone = skb_clone(skb, GFP_ATOMIC);
588 if (clone == NULL) {
0d53778e 589 pr_debug("Can't clone skb\n");
9fb9cbb1
YK
590 return skb;
591 }
592
593 NFCT_FRAG6_CB(clone)->orig = skb;
594
595 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
0d53778e 596 pr_debug("message is too short.\n");
9fb9cbb1
YK
597 goto ret_orig;
598 }
599
967b05f6 600 skb_set_transport_header(clone, fhoff);
0660e03f 601 hdr = ipv6_hdr(clone);
bff9b61c 602 fhdr = (struct frag_hdr *)skb_transport_header(clone);
9fb9cbb1 603
b8dd6a22
HFS
604 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
605 ip6_frag_ecn(hdr));
9fb9cbb1 606 if (fq == NULL) {
0d53778e 607 pr_debug("Can't find and can't create new queue\n");
9fb9cbb1
YK
608 goto ret_orig;
609 }
610
b9c69896 611 spin_lock_bh(&fq->q.lock);
9fb9cbb1
YK
612
613 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
b9c69896 614 spin_unlock_bh(&fq->q.lock);
0d53778e 615 pr_debug("Can't insert skb to queue\n");
b836c99f 616 inet_frag_put(&fq->q, &nf_frags);
9fb9cbb1
YK
617 goto ret_orig;
618 }
619
06aa8b8a 620 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
bc578a54 621 fq->q.meat == fq->q.len) {
9fb9cbb1
YK
622 ret_skb = nf_ct_frag6_reasm(fq, dev);
623 if (ret_skb == NULL)
0d53778e 624 pr_debug("Can't reassemble fragmented packets\n");
9fb9cbb1 625 }
b9c69896 626 spin_unlock_bh(&fq->q.lock);
9fb9cbb1 627
b836c99f 628 inet_frag_put(&fq->q, &nf_frags);
9fb9cbb1
YK
629 return ret_skb;
630
631ret_orig:
632 kfree_skb(clone);
633 return skb;
634}
635
6aafeef0 636void nf_ct_frag6_consume_orig(struct sk_buff *skb)
9fb9cbb1
YK
637{
638 struct sk_buff *s, *s2;
639
640 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
9fb9cbb1 641 s2 = s->next;
f9f02cca 642 s->next = NULL;
6aafeef0 643 consume_skb(s);
9fb9cbb1
YK
644 s = s2;
645 }
9fb9cbb1
YK
646}
647
c038a767
AW
648static int nf_ct_net_init(struct net *net)
649{
650 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
651 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
652 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
653 inet_frags_init_net(&net->nf_frag.frags);
654
655 return nf_ct_frag6_sysctl_register(net);
656}
657
658static void nf_ct_net_exit(struct net *net)
659{
660 nf_ct_frags6_sysctl_unregister(net);
661 inet_frags_exit_net(&net->nf_frag.frags, &nf_frags);
662}
663
664static struct pernet_operations nf_ct_net_ops = {
665 .init = nf_ct_net_init,
666 .exit = nf_ct_net_exit,
667};
668
9fb9cbb1
YK
669int nf_ct_frag6_init(void)
670{
c038a767
AW
671 int ret = 0;
672
321a3a99 673 nf_frags.hashfn = nf_hashfn;
c6fda282 674 nf_frags.constructor = ip6_frag_init;
c9547709 675 nf_frags.destructor = NULL;
1e4b8287 676 nf_frags.skb_free = nf_skb_free;
b836c99f 677 nf_frags.qsize = sizeof(struct frag_queue);
abd6523d 678 nf_frags.match = ip6_frag_match;
e521db9d 679 nf_frags.frag_expire = nf_ct_frag6_expire;
7eb95156 680 inet_frags_init(&nf_frags);
9fb9cbb1 681
c038a767
AW
682 ret = register_pernet_subsys(&nf_ct_net_ops);
683 if (ret)
e97c3e27 684 inet_frags_fini(&nf_frags);
e97c3e27 685
c038a767 686 return ret;
9fb9cbb1
YK
687}
688
689void nf_ct_frag6_cleanup(void)
690{
c038a767 691 unregister_pernet_subsys(&nf_ct_net_ops);
7eb95156 692 inet_frags_fini(&nf_frags);
9fb9cbb1 693}