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