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