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