[TG3]: Fix performance regression on 5705.
[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
9fb9cbb1
YK
17#include <linux/errno.h>
18#include <linux/types.h>
19#include <linux/string.h>
20#include <linux/socket.h>
21#include <linux/sockios.h>
22#include <linux/jiffies.h>
23#include <linux/net.h>
24#include <linux/list.h>
25#include <linux/netdevice.h>
26#include <linux/in6.h>
27#include <linux/ipv6.h>
28#include <linux/icmpv6.h>
29#include <linux/random.h>
30#include <linux/jhash.h>
31
32#include <net/sock.h>
33#include <net/snmp.h>
34
35#include <net/ipv6.h>
36#include <net/protocol.h>
37#include <net/transp_v6.h>
38#include <net/rawv6.h>
39#include <net/ndisc.h>
40#include <net/addrconf.h>
41#include <linux/sysctl.h>
42#include <linux/netfilter.h>
43#include <linux/netfilter_ipv6.h>
44#include <linux/kernel.h>
45#include <linux/module.h>
46
9fb9cbb1
YK
47#define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */
48#define NF_CT_FRAG6_LOW_THRESH 196608 /* == 192*1024 */
49#define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUT
50
94aec08e
BH
51unsigned int nf_ct_frag6_high_thresh __read_mostly = 256*1024;
52unsigned int nf_ct_frag6_low_thresh __read_mostly = 192*1024;
53unsigned long nf_ct_frag6_timeout __read_mostly = IPV6_FRAG_TIMEOUT;
9fb9cbb1
YK
54
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
64struct nf_ct_frag6_queue
65{
2e4e6a17
HW
66 struct hlist_node list;
67 struct list_head lru_list; /* lru list member */
9fb9cbb1 68
bff9a89b 69 __be32 id; /* fragment id */
9fb9cbb1
YK
70 struct in6_addr saddr;
71 struct in6_addr daddr;
72
73 spinlock_t lock;
74 atomic_t refcnt;
75 struct timer_list timer; /* expire timer */
76 struct sk_buff *fragments;
77 int len;
78 int meat;
b7aa0bf7 79 ktime_t stamp;
9fb9cbb1
YK
80 unsigned int csum;
81 __u8 last_in; /* has first/last segment arrived? */
82#define COMPLETE 4
83#define FIRST_IN 2
84#define LAST_IN 1
85 __u16 nhoffset;
9fb9cbb1
YK
86};
87
88/* Hash table. */
89
90#define FRAG6Q_HASHSZ 64
91
2e4e6a17 92static struct hlist_head nf_ct_frag6_hash[FRAG6Q_HASHSZ];
181a46a5 93static DEFINE_RWLOCK(nf_ct_frag6_lock);
9fb9cbb1
YK
94static u32 nf_ct_frag6_hash_rnd;
95static LIST_HEAD(nf_ct_frag6_lru_list);
96int nf_ct_frag6_nqueues = 0;
97
98static __inline__ void __fq_unlink(struct nf_ct_frag6_queue *fq)
99{
2e4e6a17 100 hlist_del(&fq->list);
9fb9cbb1
YK
101 list_del(&fq->lru_list);
102 nf_ct_frag6_nqueues--;
103}
104
105static __inline__ void fq_unlink(struct nf_ct_frag6_queue *fq)
106{
107 write_lock(&nf_ct_frag6_lock);
108 __fq_unlink(fq);
109 write_unlock(&nf_ct_frag6_lock);
110}
111
bff9a89b 112static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
9fb9cbb1
YK
113 struct in6_addr *daddr)
114{
115 u32 a, b, c;
116
bff9a89b
PM
117 a = (__force u32)saddr->s6_addr32[0];
118 b = (__force u32)saddr->s6_addr32[1];
119 c = (__force u32)saddr->s6_addr32[2];
9fb9cbb1
YK
120
121 a += JHASH_GOLDEN_RATIO;
122 b += JHASH_GOLDEN_RATIO;
123 c += nf_ct_frag6_hash_rnd;
124 __jhash_mix(a, b, c);
125
bff9a89b
PM
126 a += (__force u32)saddr->s6_addr32[3];
127 b += (__force u32)daddr->s6_addr32[0];
128 c += (__force u32)daddr->s6_addr32[1];
9fb9cbb1
YK
129 __jhash_mix(a, b, c);
130
bff9a89b
PM
131 a += (__force u32)daddr->s6_addr32[2];
132 b += (__force u32)daddr->s6_addr32[3];
133 c += (__force u32)id;
9fb9cbb1
YK
134 __jhash_mix(a, b, c);
135
136 return c & (FRAG6Q_HASHSZ - 1);
137}
138
139static struct timer_list nf_ct_frag6_secret_timer;
140int nf_ct_frag6_secret_interval = 10 * 60 * HZ;
141
142static void nf_ct_frag6_secret_rebuild(unsigned long dummy)
143{
144 unsigned long now = jiffies;
145 int i;
146
147 write_lock(&nf_ct_frag6_lock);
148 get_random_bytes(&nf_ct_frag6_hash_rnd, sizeof(u32));
149 for (i = 0; i < FRAG6Q_HASHSZ; i++) {
150 struct nf_ct_frag6_queue *q;
2e4e6a17 151 struct hlist_node *p, *n;
9fb9cbb1 152
2e4e6a17 153 hlist_for_each_entry_safe(q, p, n, &nf_ct_frag6_hash[i], list) {
9fb9cbb1
YK
154 unsigned int hval = ip6qhashfn(q->id,
155 &q->saddr,
156 &q->daddr);
9fb9cbb1 157 if (hval != i) {
2e4e6a17 158 hlist_del(&q->list);
9fb9cbb1 159 /* Relink to new hash chain. */
2e4e6a17
HW
160 hlist_add_head(&q->list,
161 &nf_ct_frag6_hash[hval]);
9fb9cbb1 162 }
9fb9cbb1
YK
163 }
164 }
165 write_unlock(&nf_ct_frag6_lock);
166
167 mod_timer(&nf_ct_frag6_secret_timer, now + nf_ct_frag6_secret_interval);
168}
169
170atomic_t nf_ct_frag6_mem = ATOMIC_INIT(0);
171
172/* Memory Tracking Functions. */
1ba430bc 173static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work)
9fb9cbb1 174{
1ba430bc
YK
175 if (work)
176 *work -= skb->truesize;
9fb9cbb1
YK
177 atomic_sub(skb->truesize, &nf_ct_frag6_mem);
178 if (NFCT_FRAG6_CB(skb)->orig)
179 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
180
181 kfree_skb(skb);
182}
183
1ba430bc
YK
184static inline void frag_free_queue(struct nf_ct_frag6_queue *fq,
185 unsigned int *work)
9fb9cbb1 186{
1ba430bc
YK
187 if (work)
188 *work -= sizeof(struct nf_ct_frag6_queue);
9fb9cbb1
YK
189 atomic_sub(sizeof(struct nf_ct_frag6_queue), &nf_ct_frag6_mem);
190 kfree(fq);
191}
192
193static inline struct nf_ct_frag6_queue *frag_alloc_queue(void)
194{
195 struct nf_ct_frag6_queue *fq = kmalloc(sizeof(struct nf_ct_frag6_queue), GFP_ATOMIC);
196
197 if (!fq)
198 return NULL;
199 atomic_add(sizeof(struct nf_ct_frag6_queue), &nf_ct_frag6_mem);
200 return fq;
201}
202
203/* Destruction primitives. */
204
205/* Complete destruction of fq. */
1ba430bc
YK
206static void nf_ct_frag6_destroy(struct nf_ct_frag6_queue *fq,
207 unsigned int *work)
9fb9cbb1
YK
208{
209 struct sk_buff *fp;
210
211 BUG_TRAP(fq->last_in&COMPLETE);
212 BUG_TRAP(del_timer(&fq->timer) == 0);
213
214 /* Release all fragment data. */
215 fp = fq->fragments;
216 while (fp) {
217 struct sk_buff *xp = fp->next;
218
1ba430bc 219 frag_kfree_skb(fp, work);
9fb9cbb1
YK
220 fp = xp;
221 }
222
1ba430bc 223 frag_free_queue(fq, work);
9fb9cbb1
YK
224}
225
1ba430bc 226static __inline__ void fq_put(struct nf_ct_frag6_queue *fq, unsigned int *work)
9fb9cbb1
YK
227{
228 if (atomic_dec_and_test(&fq->refcnt))
1ba430bc 229 nf_ct_frag6_destroy(fq, work);
9fb9cbb1
YK
230}
231
232/* Kill fq entry. It is not destroyed immediately,
233 * because caller (and someone more) holds reference count.
234 */
235static __inline__ void fq_kill(struct nf_ct_frag6_queue *fq)
236{
237 if (del_timer(&fq->timer))
238 atomic_dec(&fq->refcnt);
239
240 if (!(fq->last_in & COMPLETE)) {
241 fq_unlink(fq);
242 atomic_dec(&fq->refcnt);
243 fq->last_in |= COMPLETE;
244 }
245}
246
247static void nf_ct_frag6_evictor(void)
248{
249 struct nf_ct_frag6_queue *fq;
250 struct list_head *tmp;
1ba430bc 251 unsigned int work;
9fb9cbb1 252
1ba430bc
YK
253 work = atomic_read(&nf_ct_frag6_mem);
254 if (work <= nf_ct_frag6_low_thresh)
255 return;
256
257 work -= nf_ct_frag6_low_thresh;
258 while (work > 0) {
9fb9cbb1
YK
259 read_lock(&nf_ct_frag6_lock);
260 if (list_empty(&nf_ct_frag6_lru_list)) {
261 read_unlock(&nf_ct_frag6_lock);
262 return;
263 }
264 tmp = nf_ct_frag6_lru_list.next;
302fe175 265 BUG_ON(tmp == NULL);
9fb9cbb1
YK
266 fq = list_entry(tmp, struct nf_ct_frag6_queue, lru_list);
267 atomic_inc(&fq->refcnt);
268 read_unlock(&nf_ct_frag6_lock);
269
270 spin_lock(&fq->lock);
271 if (!(fq->last_in&COMPLETE))
272 fq_kill(fq);
273 spin_unlock(&fq->lock);
274
1ba430bc 275 fq_put(fq, &work);
9fb9cbb1
YK
276 }
277}
278
279static void nf_ct_frag6_expire(unsigned long data)
280{
281 struct nf_ct_frag6_queue *fq = (struct nf_ct_frag6_queue *) data;
282
283 spin_lock(&fq->lock);
284
285 if (fq->last_in & COMPLETE)
286 goto out;
287
288 fq_kill(fq);
289
290out:
291 spin_unlock(&fq->lock);
1ba430bc 292 fq_put(fq, NULL);
9fb9cbb1
YK
293}
294
295/* Creation primitives. */
296
9fb9cbb1
YK
297static struct nf_ct_frag6_queue *nf_ct_frag6_intern(unsigned int hash,
298 struct nf_ct_frag6_queue *fq_in)
299{
300 struct nf_ct_frag6_queue *fq;
2e4e6a17
HW
301#ifdef CONFIG_SMP
302 struct hlist_node *n;
303#endif
9fb9cbb1
YK
304
305 write_lock(&nf_ct_frag6_lock);
306#ifdef CONFIG_SMP
2e4e6a17 307 hlist_for_each_entry(fq, n, &nf_ct_frag6_hash[hash], list) {
1ab1457c 308 if (fq->id == fq_in->id &&
6ea46c9c
YK
309 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
310 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
9fb9cbb1
YK
311 atomic_inc(&fq->refcnt);
312 write_unlock(&nf_ct_frag6_lock);
313 fq_in->last_in |= COMPLETE;
1ba430bc 314 fq_put(fq_in, NULL);
9fb9cbb1
YK
315 return fq;
316 }
317 }
318#endif
319 fq = fq_in;
320
321 if (!mod_timer(&fq->timer, jiffies + nf_ct_frag6_timeout))
322 atomic_inc(&fq->refcnt);
323
324 atomic_inc(&fq->refcnt);
2e4e6a17 325 hlist_add_head(&fq->list, &nf_ct_frag6_hash[hash]);
9fb9cbb1
YK
326 INIT_LIST_HEAD(&fq->lru_list);
327 list_add_tail(&fq->lru_list, &nf_ct_frag6_lru_list);
328 nf_ct_frag6_nqueues++;
329 write_unlock(&nf_ct_frag6_lock);
330 return fq;
331}
332
333
334static struct nf_ct_frag6_queue *
bff9a89b 335nf_ct_frag6_create(unsigned int hash, __be32 id, struct in6_addr *src, struct in6_addr *dst)
9fb9cbb1
YK
336{
337 struct nf_ct_frag6_queue *fq;
338
339 if ((fq = frag_alloc_queue()) == NULL) {
0d53778e 340 pr_debug("Can't alloc new queue\n");
9fb9cbb1
YK
341 goto oom;
342 }
343
344 memset(fq, 0, sizeof(struct nf_ct_frag6_queue));
345
346 fq->id = id;
347 ipv6_addr_copy(&fq->saddr, src);
348 ipv6_addr_copy(&fq->daddr, dst);
349
e6f689db 350 setup_timer(&fq->timer, nf_ct_frag6_expire, (unsigned long)fq);
181a46a5 351 spin_lock_init(&fq->lock);
9fb9cbb1
YK
352 atomic_set(&fq->refcnt, 1);
353
354 return nf_ct_frag6_intern(hash, fq);
355
356oom:
357 return NULL;
358}
359
360static __inline__ struct nf_ct_frag6_queue *
bff9a89b 361fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst)
9fb9cbb1
YK
362{
363 struct nf_ct_frag6_queue *fq;
2e4e6a17 364 struct hlist_node *n;
9fb9cbb1
YK
365 unsigned int hash = ip6qhashfn(id, src, dst);
366
367 read_lock(&nf_ct_frag6_lock);
2e4e6a17 368 hlist_for_each_entry(fq, n, &nf_ct_frag6_hash[hash], list) {
1ab1457c 369 if (fq->id == id &&
6ea46c9c
YK
370 ipv6_addr_equal(src, &fq->saddr) &&
371 ipv6_addr_equal(dst, &fq->daddr)) {
9fb9cbb1
YK
372 atomic_inc(&fq->refcnt);
373 read_unlock(&nf_ct_frag6_lock);
374 return fq;
375 }
376 }
377 read_unlock(&nf_ct_frag6_lock);
378
379 return nf_ct_frag6_create(hash, id, src, dst);
380}
381
382
1ab1457c 383static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb,
9fb9cbb1
YK
384 struct frag_hdr *fhdr, int nhoff)
385{
386 struct sk_buff *prev, *next;
387 int offset, end;
388
389 if (fq->last_in & COMPLETE) {
0d53778e 390 pr_debug("Allready completed\n");
9fb9cbb1
YK
391 goto err;
392 }
393
394 offset = ntohs(fhdr->frag_off) & ~0x7;
0660e03f
ACM
395 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
396 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
9fb9cbb1
YK
397
398 if ((unsigned int)end > IPV6_MAXPLEN) {
0d53778e 399 pr_debug("offset is too large.\n");
1ab1457c 400 return -1;
9fb9cbb1
YK
401 }
402
d56f90a7
ACM
403 if (skb->ip_summed == CHECKSUM_COMPLETE) {
404 const unsigned char *nh = skb_network_header(skb);
1ab1457c 405 skb->csum = csum_sub(skb->csum,
d56f90a7 406 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
9fb9cbb1 407 0));
d56f90a7 408 }
9fb9cbb1
YK
409
410 /* Is this the final fragment? */
411 if (!(fhdr->frag_off & htons(IP6_MF))) {
412 /* If we already have some bits beyond end
413 * or have different end, the segment is corrupted.
414 */
415 if (end < fq->len ||
416 ((fq->last_in & LAST_IN) && end != fq->len)) {
0d53778e 417 pr_debug("already received last fragment\n");
9fb9cbb1
YK
418 goto err;
419 }
420 fq->last_in |= LAST_IN;
421 fq->len = end;
422 } else {
423 /* Check if the fragment is rounded to 8 bytes.
424 * Required by the RFC.
425 */
426 if (end & 0x7) {
427 /* RFC2460 says always send parameter problem in
428 * this case. -DaveM
429 */
0d53778e 430 pr_debug("end of fragment not rounded to 8 bytes.\n");
9fb9cbb1
YK
431 return -1;
432 }
433 if (end > fq->len) {
434 /* Some bits beyond end -> corruption. */
435 if (fq->last_in & LAST_IN) {
0d53778e 436 pr_debug("last packet already reached.\n");
9fb9cbb1
YK
437 goto err;
438 }
439 fq->len = end;
440 }
441 }
442
443 if (end == offset)
444 goto err;
445
446 /* Point into the IP datagram 'data' part. */
447 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
0d53778e 448 pr_debug("queue: message is too short.\n");
9fb9cbb1
YK
449 goto err;
450 }
b38dfee3 451 if (pskb_trim_rcsum(skb, end - offset)) {
0d53778e 452 pr_debug("Can't trim\n");
b38dfee3 453 goto err;
9fb9cbb1
YK
454 }
455
456 /* Find out which fragments are in front and at the back of us
457 * in the chain of fragments so far. We must know where to put
458 * this fragment, right?
459 */
460 prev = NULL;
461 for (next = fq->fragments; next != NULL; next = next->next) {
462 if (NFCT_FRAG6_CB(next)->offset >= offset)
463 break; /* bingo! */
464 prev = next;
465 }
466
467 /* We found where to put this one. Check for overlap with
468 * preceding fragment, and, if needed, align things so that
469 * any overlaps are eliminated.
470 */
471 if (prev) {
472 int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset;
473
474 if (i > 0) {
475 offset += i;
476 if (end <= offset) {
0d53778e 477 pr_debug("overlap\n");
9fb9cbb1
YK
478 goto err;
479 }
480 if (!pskb_pull(skb, i)) {
0d53778e 481 pr_debug("Can't pull\n");
9fb9cbb1
YK
482 goto err;
483 }
484 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
485 skb->ip_summed = CHECKSUM_NONE;
486 }
487 }
488
489 /* Look for overlap with succeeding segments.
490 * If we can merge fragments, do it.
491 */
492 while (next && NFCT_FRAG6_CB(next)->offset < end) {
493 /* overlap is 'i' bytes */
494 int i = end - NFCT_FRAG6_CB(next)->offset;
495
496 if (i < next->len) {
497 /* Eat head of the next overlapped fragment
498 * and leave the loop. The next ones cannot overlap.
499 */
0d53778e 500 pr_debug("Eat head of the overlapped parts.: %d", i);
9fb9cbb1
YK
501 if (!pskb_pull(next, i))
502 goto err;
503
504 /* next fragment */
505 NFCT_FRAG6_CB(next)->offset += i;
506 fq->meat -= i;
507 if (next->ip_summed != CHECKSUM_UNNECESSARY)
508 next->ip_summed = CHECKSUM_NONE;
509 break;
510 } else {
511 struct sk_buff *free_it = next;
512
513 /* Old fragmnet is completely overridden with
514 * new one drop it.
515 */
516 next = next->next;
517
518 if (prev)
519 prev->next = next;
520 else
521 fq->fragments = next;
522
523 fq->meat -= free_it->len;
1ba430bc 524 frag_kfree_skb(free_it, NULL);
9fb9cbb1
YK
525 }
526 }
527
528 NFCT_FRAG6_CB(skb)->offset = offset;
529
530 /* Insert this fragment in the chain of fragments. */
531 skb->next = next;
532 if (prev)
533 prev->next = skb;
534 else
535 fq->fragments = skb;
536
537 skb->dev = NULL;
b7aa0bf7 538 fq->stamp = skb->tstamp;
9fb9cbb1
YK
539 fq->meat += skb->len;
540 atomic_add(skb->truesize, &nf_ct_frag6_mem);
541
542 /* The first fragment.
543 * nhoffset is obtained from the first fragment, of course.
544 */
545 if (offset == 0) {
546 fq->nhoffset = nhoff;
547 fq->last_in |= FIRST_IN;
548 }
549 write_lock(&nf_ct_frag6_lock);
550 list_move_tail(&fq->lru_list, &nf_ct_frag6_lru_list);
551 write_unlock(&nf_ct_frag6_lock);
552 return 0;
553
554err:
555 return -1;
556}
557
558/*
559 * Check if this packet is complete.
560 * Returns NULL on failure by any reason, and pointer
561 * to current nexthdr field in reassembled frame.
562 *
563 * It is called with locked fq, and caller must check that
564 * queue is eligible for reassembly i.e. it is not COMPLETE,
565 * the last and the first frames arrived and all the bits are here.
566 */
567static struct sk_buff *
568nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev)
569{
570 struct sk_buff *fp, *op, *head = fq->fragments;
571 int payload_len;
572
573 fq_kill(fq);
574
575 BUG_TRAP(head != NULL);
576 BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0);
577
578 /* Unfragmented part is taken from the first segment. */
d56f90a7
ACM
579 payload_len = ((head->data - skb_network_header(head)) -
580 sizeof(struct ipv6hdr) + fq->len -
581 sizeof(struct frag_hdr));
9fb9cbb1 582 if (payload_len > IPV6_MAXPLEN) {
0d53778e 583 pr_debug("payload len is too large.\n");
9fb9cbb1
YK
584 goto out_oversize;
585 }
586
587 /* Head of list must not be cloned. */
588 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) {
0d53778e 589 pr_debug("skb is cloned but can't expand head");
9fb9cbb1
YK
590 goto out_oom;
591 }
592
593 /* If the first fragment is fragmented itself, we split
594 * it to two chunks: the first with data and paged part
595 * and the second, holding only fragments. */
596 if (skb_shinfo(head)->frag_list) {
597 struct sk_buff *clone;
598 int i, plen = 0;
599
600 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) {
0d53778e 601 pr_debug("Can't alloc skb\n");
9fb9cbb1
YK
602 goto out_oom;
603 }
604 clone->next = head->next;
605 head->next = clone;
606 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
607 skb_shinfo(head)->frag_list = NULL;
608 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
609 plen += skb_shinfo(head)->frags[i].size;
610 clone->len = clone->data_len = head->data_len - plen;
611 head->data_len -= clone->len;
612 head->len -= clone->len;
613 clone->csum = 0;
614 clone->ip_summed = head->ip_summed;
615
616 NFCT_FRAG6_CB(clone)->orig = NULL;
617 atomic_add(clone->truesize, &nf_ct_frag6_mem);
618 }
619
620 /* We have to remove fragment header from datagram and to relocate
621 * header in order to calculate ICV correctly. */
bff9b61c 622 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
1ab1457c 623 memmove(head->head + sizeof(struct frag_hdr), head->head,
9fb9cbb1 624 (head->data - head->head) - sizeof(struct frag_hdr));
b0e380b1
ACM
625 head->mac_header += sizeof(struct frag_hdr);
626 head->network_header += sizeof(struct frag_hdr);
9fb9cbb1
YK
627
628 skb_shinfo(head)->frag_list = head->next;
badff6d0 629 skb_reset_transport_header(head);
d56f90a7 630 skb_push(head, head->data - skb_network_header(head));
9fb9cbb1
YK
631 atomic_sub(head->truesize, &nf_ct_frag6_mem);
632
633 for (fp=head->next; fp; fp = fp->next) {
634 head->data_len += fp->len;
635 head->len += fp->len;
636 if (head->ip_summed != fp->ip_summed)
637 head->ip_summed = CHECKSUM_NONE;
84fa7933 638 else if (head->ip_summed == CHECKSUM_COMPLETE)
9fb9cbb1
YK
639 head->csum = csum_add(head->csum, fp->csum);
640 head->truesize += fp->truesize;
641 atomic_sub(fp->truesize, &nf_ct_frag6_mem);
642 }
643
644 head->next = NULL;
645 head->dev = dev;
b7aa0bf7 646 head->tstamp = fq->stamp;
0660e03f 647 ipv6_hdr(head)->payload_len = htons(payload_len);
9fb9cbb1
YK
648
649 /* Yes, and fold redundant checksum back. 8) */
84fa7933 650 if (head->ip_summed == CHECKSUM_COMPLETE)
d56f90a7 651 head->csum = csum_partial(skb_network_header(head),
cfe1fc77 652 skb_network_header_len(head),
d56f90a7 653 head->csum);
9fb9cbb1
YK
654
655 fq->fragments = NULL;
656
657 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
658 fp = skb_shinfo(head)->frag_list;
659 if (NFCT_FRAG6_CB(fp)->orig == NULL)
660 /* at above code, head skb is divided into two skbs. */
661 fp = fp->next;
662
663 op = NFCT_FRAG6_CB(head)->orig;
664 for (; fp; fp = fp->next) {
665 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
666
667 op->next = orig;
668 op = orig;
669 NFCT_FRAG6_CB(fp)->orig = NULL;
670 }
671
672 return head;
673
674out_oversize:
675 if (net_ratelimit())
676 printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len);
677 goto out_fail;
678out_oom:
679 if (net_ratelimit())
680 printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n");
681out_fail:
682 return NULL;
683}
684
685/*
686 * find the header just before Fragment Header.
687 *
688 * if success return 0 and set ...
689 * (*prevhdrp): the value of "Next Header Field" in the header
690 * just before Fragment Header.
691 * (*prevhoff): the offset of "Next Header Field" in the header
692 * just before Fragment Header.
693 * (*fhoff) : the offset of Fragment Header.
694 *
695 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
696 *
697 */
698static int
699find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
700{
0660e03f 701 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
6b88dd96
ACM
702 const int netoff = skb_network_offset(skb);
703 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
704 int start = netoff + sizeof(struct ipv6hdr);
9fb9cbb1
YK
705 int len = skb->len - start;
706 u8 prevhdr = NEXTHDR_IPV6;
707
1ab1457c
YH
708 while (nexthdr != NEXTHDR_FRAGMENT) {
709 struct ipv6_opt_hdr hdr;
710 int hdrlen;
9fb9cbb1
YK
711
712 if (!ipv6_ext_hdr(nexthdr)) {
713 return -1;
714 }
1ab1457c 715 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
0d53778e 716 pr_debug("too short\n");
9fb9cbb1
YK
717 return -1;
718 }
1ab1457c 719 if (nexthdr == NEXTHDR_NONE) {
0d53778e 720 pr_debug("next header is none\n");
9fb9cbb1
YK
721 return -1;
722 }
1ab1457c
YH
723 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
724 BUG();
725 if (nexthdr == NEXTHDR_AUTH)
726 hdrlen = (hdr.hdrlen+2)<<2;
727 else
728 hdrlen = ipv6_optlen(&hdr);
9fb9cbb1
YK
729
730 prevhdr = nexthdr;
731 prev_nhoff = start;
732
1ab1457c
YH
733 nexthdr = hdr.nexthdr;
734 len -= hdrlen;
735 start += hdrlen;
736 }
9fb9cbb1
YK
737
738 if (len < 0)
739 return -1;
740
741 *prevhdrp = prevhdr;
742 *prevhoff = prev_nhoff;
743 *fhoff = start;
744
745 return 0;
746}
747
748struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb)
749{
1ab1457c 750 struct sk_buff *clone;
9fb9cbb1
YK
751 struct net_device *dev = skb->dev;
752 struct frag_hdr *fhdr;
753 struct nf_ct_frag6_queue *fq;
754 struct ipv6hdr *hdr;
755 int fhoff, nhoff;
756 u8 prevhdr;
757 struct sk_buff *ret_skb = NULL;
758
759 /* Jumbo payload inhibits frag. header */
0660e03f 760 if (ipv6_hdr(skb)->payload_len == 0) {
0d53778e 761 pr_debug("payload len = 0\n");
9fb9cbb1
YK
762 return skb;
763 }
764
765 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
766 return skb;
767
768 clone = skb_clone(skb, GFP_ATOMIC);
769 if (clone == NULL) {
0d53778e 770 pr_debug("Can't clone skb\n");
9fb9cbb1
YK
771 return skb;
772 }
773
774 NFCT_FRAG6_CB(clone)->orig = skb;
775
776 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
0d53778e 777 pr_debug("message is too short.\n");
9fb9cbb1
YK
778 goto ret_orig;
779 }
780
967b05f6 781 skb_set_transport_header(clone, fhoff);
0660e03f 782 hdr = ipv6_hdr(clone);
bff9b61c 783 fhdr = (struct frag_hdr *)skb_transport_header(clone);
9fb9cbb1
YK
784
785 if (!(fhdr->frag_off & htons(0xFFF9))) {
0d53778e 786 pr_debug("Invalid fragment offset\n");
9fb9cbb1
YK
787 /* It is not a fragmented frame */
788 goto ret_orig;
789 }
790
791 if (atomic_read(&nf_ct_frag6_mem) > nf_ct_frag6_high_thresh)
792 nf_ct_frag6_evictor();
793
794 fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr);
795 if (fq == NULL) {
0d53778e 796 pr_debug("Can't find and can't create new queue\n");
9fb9cbb1
YK
797 goto ret_orig;
798 }
799
800 spin_lock(&fq->lock);
801
802 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
803 spin_unlock(&fq->lock);
0d53778e 804 pr_debug("Can't insert skb to queue\n");
1ba430bc 805 fq_put(fq, NULL);
9fb9cbb1
YK
806 goto ret_orig;
807 }
808
809 if (fq->last_in == (FIRST_IN|LAST_IN) && fq->meat == fq->len) {
810 ret_skb = nf_ct_frag6_reasm(fq, dev);
811 if (ret_skb == NULL)
0d53778e 812 pr_debug("Can't reassemble fragmented packets\n");
9fb9cbb1
YK
813 }
814 spin_unlock(&fq->lock);
815
1ba430bc 816 fq_put(fq, NULL);
9fb9cbb1
YK
817 return ret_skb;
818
819ret_orig:
820 kfree_skb(clone);
821 return skb;
822}
823
824void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb,
825 struct net_device *in, struct net_device *out,
826 int (*okfn)(struct sk_buff *))
827{
828 struct sk_buff *s, *s2;
829
830 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
831 nf_conntrack_put_reasm(s->nfct_reasm);
832 nf_conntrack_get_reasm(skb);
833 s->nfct_reasm = skb;
834
835 s2 = s->next;
f9f02cca
PM
836 s->next = NULL;
837
9fb9cbb1
YK
838 NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn,
839 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
840 s = s2;
841 }
842 nf_conntrack_put_reasm(skb);
843}
844
845int nf_ct_frag6_kfree_frags(struct sk_buff *skb)
846{
847 struct sk_buff *s, *s2;
848
849 for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) {
850
851 s2 = s->next;
852 kfree_skb(s);
853 }
854
855 kfree_skb(skb);
856
857 return 0;
858}
859
860int nf_ct_frag6_init(void)
861{
862 nf_ct_frag6_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
863 (jiffies ^ (jiffies >> 6)));
864
e6f689db 865 setup_timer(&nf_ct_frag6_secret_timer, nf_ct_frag6_secret_rebuild, 0);
9fb9cbb1
YK
866 nf_ct_frag6_secret_timer.expires = jiffies
867 + nf_ct_frag6_secret_interval;
868 add_timer(&nf_ct_frag6_secret_timer);
869
870 return 0;
871}
872
873void nf_ct_frag6_cleanup(void)
874{
875 del_timer(&nf_ct_frag6_secret_timer);
302fe175 876 nf_ct_frag6_low_thresh = 0;
9fb9cbb1
YK
877 nf_ct_frag6_evictor();
878}
This page took 0.267894 seconds and 5 git commands to generate.