blob: eef985e010ea7f85b06927d11fd181099825227f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 fragment reassembly
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
9 *
10 * Based on: net/ipv4/ip_fragment.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18/*
19 * Fixes:
20 * Andi Kleen Make it work with multiple hosts.
21 * More RFC compliance.
22 *
23 * Horst von Brand Add missing #include <linux/string.h>
24 * Alexey Kuznetsov SMP races, threading, cleanup.
25 * Patrick McHardy LRU queue of frag heads for evictor.
26 * Mitsuru KANDA @USAGI Register inet6_protocol{}.
27 * David Stevens and
28 * YOSHIFUJI,H. @USAGI Always remove fragment header to
29 * calculate ICV correctly.
30 */
31#include <linux/config.h>
32#include <linux/errno.h>
33#include <linux/types.h>
34#include <linux/string.h>
35#include <linux/socket.h>
36#include <linux/sockios.h>
37#include <linux/jiffies.h>
38#include <linux/net.h>
39#include <linux/list.h>
40#include <linux/netdevice.h>
41#include <linux/in6.h>
42#include <linux/ipv6.h>
43#include <linux/icmpv6.h>
44#include <linux/random.h>
45#include <linux/jhash.h>
46
47#include <net/sock.h>
48#include <net/snmp.h>
49
50#include <net/ipv6.h>
51#include <net/protocol.h>
52#include <net/transp_v6.h>
53#include <net/rawv6.h>
54#include <net/ndisc.h>
55#include <net/addrconf.h>
56
57int sysctl_ip6frag_high_thresh = 256*1024;
58int sysctl_ip6frag_low_thresh = 192*1024;
59
60int sysctl_ip6frag_time = IPV6_FRAG_TIMEOUT;
61
62struct ip6frag_skb_cb
63{
64 struct inet6_skb_parm h;
65 int offset;
66};
67
68#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
69
70
71/*
72 * Equivalent of ipv4 struct ipq
73 */
74
75struct frag_queue
76{
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -080077 struct hlist_node list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct list_head lru_list; /* lru list member */
79
80 __u32 id; /* fragment id */
81 struct in6_addr saddr;
82 struct in6_addr daddr;
83
84 spinlock_t lock;
85 atomic_t refcnt;
86 struct timer_list timer; /* expire timer */
87 struct sk_buff *fragments;
88 int len;
89 int meat;
90 int iif;
91 struct timeval stamp;
92 unsigned int csum;
93 __u8 last_in; /* has first/last segment arrived? */
94#define COMPLETE 4
95#define FIRST_IN 2
96#define LAST_IN 1
97 __u16 nhoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100/* Hash table. */
101
102#define IP6Q_HASHSZ 64
103
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800104static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static DEFINE_RWLOCK(ip6_frag_lock);
106static u32 ip6_frag_hash_rnd;
107static LIST_HEAD(ip6_frag_lru_list);
108int ip6_frag_nqueues = 0;
109
110static __inline__ void __fq_unlink(struct frag_queue *fq)
111{
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800112 hlist_del(&fq->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 list_del(&fq->lru_list);
114 ip6_frag_nqueues--;
115}
116
117static __inline__ void fq_unlink(struct frag_queue *fq)
118{
119 write_lock(&ip6_frag_lock);
120 __fq_unlink(fq);
121 write_unlock(&ip6_frag_lock);
122}
123
Zach Brownf6596f92006-04-10 16:05:34 -0700124/*
125 * callers should be careful not to use the hash value outside the ipfrag_lock
126 * as doing so could race with ipfrag_hash_rnd being recalculated.
127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr,
129 struct in6_addr *daddr)
130{
131 u32 a, b, c;
132
133 a = saddr->s6_addr32[0];
134 b = saddr->s6_addr32[1];
135 c = saddr->s6_addr32[2];
136
137 a += JHASH_GOLDEN_RATIO;
138 b += JHASH_GOLDEN_RATIO;
139 c += ip6_frag_hash_rnd;
140 __jhash_mix(a, b, c);
141
142 a += saddr->s6_addr32[3];
143 b += daddr->s6_addr32[0];
144 c += daddr->s6_addr32[1];
145 __jhash_mix(a, b, c);
146
147 a += daddr->s6_addr32[2];
148 b += daddr->s6_addr32[3];
149 c += id;
150 __jhash_mix(a, b, c);
151
152 return c & (IP6Q_HASHSZ - 1);
153}
154
155static struct timer_list ip6_frag_secret_timer;
156int sysctl_ip6frag_secret_interval = 10 * 60 * HZ;
157
158static void ip6_frag_secret_rebuild(unsigned long dummy)
159{
160 unsigned long now = jiffies;
161 int i;
162
163 write_lock(&ip6_frag_lock);
164 get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
165 for (i = 0; i < IP6Q_HASHSZ; i++) {
166 struct frag_queue *q;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800167 struct hlist_node *p, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800169 hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int hval = ip6qhashfn(q->id,
171 &q->saddr,
172 &q->daddr);
173
174 if (hval != i) {
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800175 hlist_del(&q->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Relink to new hash chain. */
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800178 hlist_add_head(&q->list,
179 &ip6_frag_hash[hval]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183 }
184 write_unlock(&ip6_frag_lock);
185
186 mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
187}
188
189atomic_t ip6_frag_mem = ATOMIC_INIT(0);
190
191/* Memory Tracking Functions. */
192static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
193{
194 if (work)
195 *work -= skb->truesize;
196 atomic_sub(skb->truesize, &ip6_frag_mem);
197 kfree_skb(skb);
198}
199
200static inline void frag_free_queue(struct frag_queue *fq, int *work)
201{
202 if (work)
203 *work -= sizeof(struct frag_queue);
204 atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
205 kfree(fq);
206}
207
208static inline struct frag_queue *frag_alloc_queue(void)
209{
Ingo Oeser78c784c2006-03-20 23:01:17 -0800210 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if(!fq)
213 return NULL;
214 atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
215 return fq;
216}
217
218/* Destruction primitives. */
219
220/* Complete destruction of fq. */
221static void ip6_frag_destroy(struct frag_queue *fq, int *work)
222{
223 struct sk_buff *fp;
224
225 BUG_TRAP(fq->last_in&COMPLETE);
226 BUG_TRAP(del_timer(&fq->timer) == 0);
227
228 /* Release all fragment data. */
229 fp = fq->fragments;
230 while (fp) {
231 struct sk_buff *xp = fp->next;
232
233 frag_kfree_skb(fp, work);
234 fp = xp;
235 }
236
237 frag_free_queue(fq, work);
238}
239
240static __inline__ void fq_put(struct frag_queue *fq, int *work)
241{
242 if (atomic_dec_and_test(&fq->refcnt))
243 ip6_frag_destroy(fq, work);
244}
245
246/* Kill fq entry. It is not destroyed immediately,
247 * because caller (and someone more) holds reference count.
248 */
249static __inline__ void fq_kill(struct frag_queue *fq)
250{
251 if (del_timer(&fq->timer))
252 atomic_dec(&fq->refcnt);
253
254 if (!(fq->last_in & COMPLETE)) {
255 fq_unlink(fq);
256 atomic_dec(&fq->refcnt);
257 fq->last_in |= COMPLETE;
258 }
259}
260
261static void ip6_evictor(void)
262{
263 struct frag_queue *fq;
264 struct list_head *tmp;
265 int work;
266
267 work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
268 if (work <= 0)
269 return;
270
271 while(work > 0) {
272 read_lock(&ip6_frag_lock);
273 if (list_empty(&ip6_frag_lru_list)) {
274 read_unlock(&ip6_frag_lock);
275 return;
276 }
277 tmp = ip6_frag_lru_list.next;
278 fq = list_entry(tmp, struct frag_queue, lru_list);
279 atomic_inc(&fq->refcnt);
280 read_unlock(&ip6_frag_lock);
281
282 spin_lock(&fq->lock);
283 if (!(fq->last_in&COMPLETE))
284 fq_kill(fq);
285 spin_unlock(&fq->lock);
286
287 fq_put(fq, &work);
288 IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
289 }
290}
291
292static void ip6_frag_expire(unsigned long data)
293{
294 struct frag_queue *fq = (struct frag_queue *) data;
Ingo Oeser78c784c2006-03-20 23:01:17 -0800295 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 spin_lock(&fq->lock);
298
299 if (fq->last_in & COMPLETE)
300 goto out;
301
302 fq_kill(fq);
303
304 IP6_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
305 IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
306
Ingo Oeser78c784c2006-03-20 23:01:17 -0800307 /* Don't send error if the first segment did not arrive. */
308 if (!(fq->last_in&FIRST_IN) || !fq->fragments)
309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Ingo Oeser78c784c2006-03-20 23:01:17 -0800311 dev = dev_get_by_index(fq->iif);
312 if (!dev)
313 goto out;
314
315 /*
316 But use as source device on which LAST ARRIVED
317 segment was received. And do not use fq->dev
318 pointer directly, device might already disappeared.
319 */
320 fq->fragments->dev = dev;
321 icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
322 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out:
324 spin_unlock(&fq->lock);
325 fq_put(fq, NULL);
326}
327
328/* Creation primitives. */
329
330
Zach Brownf6596f92006-04-10 16:05:34 -0700331static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 struct frag_queue *fq;
Zach Brownf6596f92006-04-10 16:05:34 -0700334 unsigned int hash;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800335#ifdef CONFIG_SMP
336 struct hlist_node *n;
337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 write_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700340 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#ifdef CONFIG_SMP
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800342 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (fq->id == fq_in->id &&
344 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
345 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
346 atomic_inc(&fq->refcnt);
347 write_unlock(&ip6_frag_lock);
348 fq_in->last_in |= COMPLETE;
349 fq_put(fq_in, NULL);
350 return fq;
351 }
352 }
353#endif
354 fq = fq_in;
355
356 if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
357 atomic_inc(&fq->refcnt);
358
359 atomic_inc(&fq->refcnt);
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800360 hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 INIT_LIST_HEAD(&fq->lru_list);
362 list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
363 ip6_frag_nqueues++;
364 write_unlock(&ip6_frag_lock);
365 return fq;
366}
367
368
369static struct frag_queue *
Zach Brownf6596f92006-04-10 16:05:34 -0700370ip6_frag_create(u32 id, struct in6_addr *src, struct in6_addr *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct frag_queue *fq;
373
374 if ((fq = frag_alloc_queue()) == NULL)
375 goto oom;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 fq->id = id;
378 ipv6_addr_copy(&fq->saddr, src);
379 ipv6_addr_copy(&fq->daddr, dst);
380
381 init_timer(&fq->timer);
382 fq->timer.function = ip6_frag_expire;
383 fq->timer.data = (long) fq;
384 spin_lock_init(&fq->lock);
385 atomic_set(&fq->refcnt, 1);
386
Zach Brownf6596f92006-04-10 16:05:34 -0700387 return ip6_frag_intern(fq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389oom:
390 IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
391 return NULL;
392}
393
394static __inline__ struct frag_queue *
395fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst)
396{
397 struct frag_queue *fq;
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800398 struct hlist_node *n;
Zach Brownf6596f92006-04-10 16:05:34 -0700399 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 read_lock(&ip6_frag_lock);
Zach Brownf6596f92006-04-10 16:05:34 -0700402 hash = ip6qhashfn(id, src, dst);
Yasuyuki Kozakaie7c8a412005-11-16 12:55:37 -0800403 hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (fq->id == id &&
405 ipv6_addr_equal(src, &fq->saddr) &&
406 ipv6_addr_equal(dst, &fq->daddr)) {
407 atomic_inc(&fq->refcnt);
408 read_unlock(&ip6_frag_lock);
409 return fq;
410 }
411 }
412 read_unlock(&ip6_frag_lock);
413
Zach Brownf6596f92006-04-10 16:05:34 -0700414 return ip6_frag_create(id, src, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417
418static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
419 struct frag_hdr *fhdr, int nhoff)
420{
421 struct sk_buff *prev, *next;
422 int offset, end;
423
424 if (fq->last_in & COMPLETE)
425 goto err;
426
427 offset = ntohs(fhdr->frag_off) & ~0x7;
428 end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
429 ((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
430
431 if ((unsigned int)end > IPV6_MAXPLEN) {
432 IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
433 icmpv6_param_prob(skb,ICMPV6_HDR_FIELD, (u8*)&fhdr->frag_off - skb->nh.raw);
434 return;
435 }
436
437 if (skb->ip_summed == CHECKSUM_HW)
438 skb->csum = csum_sub(skb->csum,
439 csum_partial(skb->nh.raw, (u8*)(fhdr+1)-skb->nh.raw, 0));
440
441 /* Is this the final fragment? */
442 if (!(fhdr->frag_off & htons(IP6_MF))) {
443 /* If we already have some bits beyond end
444 * or have different end, the segment is corrupted.
445 */
446 if (end < fq->len ||
447 ((fq->last_in & LAST_IN) && end != fq->len))
448 goto err;
449 fq->last_in |= LAST_IN;
450 fq->len = end;
451 } else {
452 /* Check if the fragment is rounded to 8 bytes.
453 * Required by the RFC.
454 */
455 if (end & 0x7) {
456 /* RFC2460 says always send parameter problem in
457 * this case. -DaveM
458 */
459 IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
460 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
461 offsetof(struct ipv6hdr, payload_len));
462 return;
463 }
464 if (end > fq->len) {
465 /* Some bits beyond end -> corruption. */
466 if (fq->last_in & LAST_IN)
467 goto err;
468 fq->len = end;
469 }
470 }
471
472 if (end == offset)
473 goto err;
474
475 /* Point into the IP datagram 'data' part. */
476 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
477 goto err;
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700478
479 if (pskb_trim_rcsum(skb, end - offset))
480 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* Find out which fragments are in front and at the back of us
483 * in the chain of fragments so far. We must know where to put
484 * this fragment, right?
485 */
486 prev = NULL;
487 for(next = fq->fragments; next != NULL; next = next->next) {
488 if (FRAG6_CB(next)->offset >= offset)
489 break; /* bingo! */
490 prev = next;
491 }
492
493 /* We found where to put this one. Check for overlap with
494 * preceding fragment, and, if needed, align things so that
495 * any overlaps are eliminated.
496 */
497 if (prev) {
498 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
499
500 if (i > 0) {
501 offset += i;
502 if (end <= offset)
503 goto err;
504 if (!pskb_pull(skb, i))
505 goto err;
506 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
507 skb->ip_summed = CHECKSUM_NONE;
508 }
509 }
510
511 /* Look for overlap with succeeding segments.
512 * If we can merge fragments, do it.
513 */
514 while (next && FRAG6_CB(next)->offset < end) {
515 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
516
517 if (i < next->len) {
518 /* Eat head of the next overlapped fragment
519 * and leave the loop. The next ones cannot overlap.
520 */
521 if (!pskb_pull(next, i))
522 goto err;
523 FRAG6_CB(next)->offset += i; /* next fragment */
524 fq->meat -= i;
525 if (next->ip_summed != CHECKSUM_UNNECESSARY)
526 next->ip_summed = CHECKSUM_NONE;
527 break;
528 } else {
529 struct sk_buff *free_it = next;
530
531 /* Old fragment is completely overridden with
532 * new one drop it.
533 */
534 next = next->next;
535
536 if (prev)
537 prev->next = next;
538 else
539 fq->fragments = next;
540
541 fq->meat -= free_it->len;
542 frag_kfree_skb(free_it, NULL);
543 }
544 }
545
546 FRAG6_CB(skb)->offset = offset;
547
548 /* Insert this fragment in the chain of fragments. */
549 skb->next = next;
550 if (prev)
551 prev->next = skb;
552 else
553 fq->fragments = skb;
554
555 if (skb->dev)
556 fq->iif = skb->dev->ifindex;
557 skb->dev = NULL;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700558 skb_get_timestamp(skb, &fq->stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 fq->meat += skb->len;
560 atomic_add(skb->truesize, &ip6_frag_mem);
561
562 /* The first fragment.
563 * nhoffset is obtained from the first fragment, of course.
564 */
565 if (offset == 0) {
566 fq->nhoffset = nhoff;
567 fq->last_in |= FIRST_IN;
568 }
569 write_lock(&ip6_frag_lock);
570 list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
571 write_unlock(&ip6_frag_lock);
572 return;
573
574err:
575 IP6_INC_STATS(IPSTATS_MIB_REASMFAILS);
576 kfree_skb(skb);
577}
578
579/*
580 * Check if this packet is complete.
581 * Returns NULL on failure by any reason, and pointer
582 * to current nexthdr field in reassembled frame.
583 *
584 * It is called with locked fq, and caller must check that
585 * queue is eligible for reassembly i.e. it is not COMPLETE,
586 * the last and the first frames arrived and all the bits are here.
587 */
588static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct net_device *dev)
590{
591 struct sk_buff *fp, *head = fq->fragments;
592 int payload_len;
593 unsigned int nhoff;
594
595 fq_kill(fq);
596
597 BUG_TRAP(head != NULL);
598 BUG_TRAP(FRAG6_CB(head)->offset == 0);
599
600 /* Unfragmented part is taken from the first segment. */
601 payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
602 if (payload_len > IPV6_MAXPLEN)
603 goto out_oversize;
604
605 /* Head of list must not be cloned. */
606 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
607 goto out_oom;
608
609 /* If the first fragment is fragmented itself, we split
610 * it to two chunks: the first with data and paged part
611 * and the second, holding only fragments. */
612 if (skb_shinfo(head)->frag_list) {
613 struct sk_buff *clone;
614 int i, plen = 0;
615
616 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
617 goto out_oom;
618 clone->next = head->next;
619 head->next = clone;
620 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
621 skb_shinfo(head)->frag_list = NULL;
622 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
623 plen += skb_shinfo(head)->frags[i].size;
624 clone->len = clone->data_len = head->data_len - plen;
625 head->data_len -= clone->len;
626 head->len -= clone->len;
627 clone->csum = 0;
628 clone->ip_summed = head->ip_summed;
629 atomic_add(clone->truesize, &ip6_frag_mem);
630 }
631
632 /* We have to remove fragment header from datagram and to relocate
633 * header in order to calculate ICV correctly. */
634 nhoff = fq->nhoffset;
635 head->nh.raw[nhoff] = head->h.raw[0];
636 memmove(head->head + sizeof(struct frag_hdr), head->head,
637 (head->data - head->head) - sizeof(struct frag_hdr));
638 head->mac.raw += sizeof(struct frag_hdr);
639 head->nh.raw += sizeof(struct frag_hdr);
640
641 skb_shinfo(head)->frag_list = head->next;
642 head->h.raw = head->data;
643 skb_push(head, head->data - head->nh.raw);
644 atomic_sub(head->truesize, &ip6_frag_mem);
645
646 for (fp=head->next; fp; fp = fp->next) {
647 head->data_len += fp->len;
648 head->len += fp->len;
649 if (head->ip_summed != fp->ip_summed)
650 head->ip_summed = CHECKSUM_NONE;
651 else if (head->ip_summed == CHECKSUM_HW)
652 head->csum = csum_add(head->csum, fp->csum);
653 head->truesize += fp->truesize;
654 atomic_sub(fp->truesize, &ip6_frag_mem);
655 }
656
657 head->next = NULL;
658 head->dev = dev;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700659 skb_set_timestamp(head, &fq->stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 head->nh.ipv6h->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800661 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 *skb_in = head;
664
665 /* Yes, and fold redundant checksum back. 8) */
666 if (head->ip_summed == CHECKSUM_HW)
667 head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
668
669 IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
670 fq->fragments = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return 1;
672
673out_oversize:
674 if (net_ratelimit())
675 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
676 goto out_fail;
677out_oom:
678 if (net_ratelimit())
679 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
680out_fail:
681 IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
682 return -1;
683}
684
Patrick McHardy951dbc82006-01-06 23:02:34 -0800685static int ipv6_frag_rcv(struct sk_buff **skbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 struct sk_buff *skb = *skbp;
688 struct net_device *dev = skb->dev;
689 struct frag_hdr *fhdr;
690 struct frag_queue *fq;
691 struct ipv6hdr *hdr;
692
693 hdr = skb->nh.ipv6h;
694
695 IP6_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
696
697 /* Jumbo payload inhibits frag. header */
698 if (hdr->payload_len==0) {
699 IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
700 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
701 return -1;
702 }
703 if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+sizeof(struct frag_hdr))) {
704 IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
705 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
706 return -1;
707 }
708
709 hdr = skb->nh.ipv6h;
710 fhdr = (struct frag_hdr *)skb->h.raw;
711
712 if (!(fhdr->frag_off & htons(0xFFF9))) {
713 /* It is not a fragmented frame */
714 skb->h.raw += sizeof(struct frag_hdr);
715 IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
716
Patrick McHardy951dbc82006-01-06 23:02:34 -0800717 IP6CB(skb)->nhoff = (u8*)fhdr - skb->nh.raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 1;
719 }
720
721 if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
722 ip6_evictor();
723
724 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr)) != NULL) {
725 int ret = -1;
726
727 spin_lock(&fq->lock);
728
Patrick McHardy951dbc82006-01-06 23:02:34 -0800729 ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (fq->last_in == (FIRST_IN|LAST_IN) &&
732 fq->meat == fq->len)
Patrick McHardy951dbc82006-01-06 23:02:34 -0800733 ret = ip6_frag_reasm(fq, skbp, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 spin_unlock(&fq->lock);
736 fq_put(fq, NULL);
737 return ret;
738 }
739
740 IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
741 kfree_skb(skb);
742 return -1;
743}
744
745static struct inet6_protocol frag_protocol =
746{
747 .handler = ipv6_frag_rcv,
748 .flags = INET6_PROTO_NOPOLICY,
749};
750
751void __init ipv6_frag_init(void)
752{
753 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
754 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
755
756 ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
757 (jiffies ^ (jiffies >> 6)));
758
759 init_timer(&ip6_frag_secret_timer);
760 ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
761 ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
762 add_timer(&ip6_frag_secret_timer);
763}