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