]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/ipv6/reassembly.c
[INET]: Consolidate the xxx_frag_kill
[mirror_ubuntu-eoan-kernel.git] / net / ipv6 / reassembly.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 fragment reassembly
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
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
1ab1457c
YH
18/*
19 * Fixes:
1da177e4
LT
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 */
1da177e4
LT
31#include <linux/errno.h>
32#include <linux/types.h>
33#include <linux/string.h>
34#include <linux/socket.h>
35#include <linux/sockios.h>
36#include <linux/jiffies.h>
37#include <linux/net.h>
38#include <linux/list.h>
39#include <linux/netdevice.h>
40#include <linux/in6.h>
41#include <linux/ipv6.h>
42#include <linux/icmpv6.h>
43#include <linux/random.h>
44#include <linux/jhash.h>
f61944ef 45#include <linux/skbuff.h>
1da177e4
LT
46
47#include <net/sock.h>
48#include <net/snmp.h>
49
50#include <net/ipv6.h>
a11d206d 51#include <net/ip6_route.h>
1da177e4
LT
52#include <net/protocol.h>
53#include <net/transp_v6.h>
54#include <net/rawv6.h>
55#include <net/ndisc.h>
56#include <net/addrconf.h>
5ab11c98 57#include <net/inet_frag.h>
1da177e4 58
1da177e4
LT
59struct ip6frag_skb_cb
60{
61 struct inet6_skb_parm h;
62 int offset;
63};
64
65#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
66
67
68/*
69 * Equivalent of ipv4 struct ipq
70 */
71
72struct frag_queue
73{
5ab11c98 74 struct inet_frag_queue q;
1da177e4 75
e69a4adc 76 __be32 id; /* fragment id */
1da177e4
LT
77 struct in6_addr saddr;
78 struct in6_addr daddr;
79
1da177e4 80 int iif;
1da177e4 81 unsigned int csum;
1da177e4 82 __u16 nhoffset;
1da177e4
LT
83};
84
04128f23
PE
85struct inet_frags_ctl ip6_frags_ctl __read_mostly = {
86 .high_thresh = 256 * 1024,
87 .low_thresh = 192 * 1024,
88 .timeout = IPV6_FRAG_TIMEOUT,
89 .secret_interval = 10 * 60 * HZ,
90};
91
7eb95156 92static struct inet_frags ip6_frags;
1da177e4 93
7eb95156
PE
94int ip6_frag_nqueues(void)
95{
96 return ip6_frags.nqueues;
97}
1da177e4 98
7eb95156
PE
99int ip6_frag_mem(void)
100{
101 return atomic_read(&ip6_frags.mem);
102}
1da177e4 103
f61944ef
HX
104static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
105 struct net_device *dev);
106
f6596f9d
ZB
107/*
108 * callers should be careful not to use the hash value outside the ipfrag_lock
109 * as doing so could race with ipfrag_hash_rnd being recalculated.
110 */
e69a4adc 111static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
1da177e4
LT
112 struct in6_addr *daddr)
113{
114 u32 a, b, c;
115
e69a4adc
AV
116 a = (__force u32)saddr->s6_addr32[0];
117 b = (__force u32)saddr->s6_addr32[1];
118 c = (__force u32)saddr->s6_addr32[2];
1da177e4
LT
119
120 a += JHASH_GOLDEN_RATIO;
121 b += JHASH_GOLDEN_RATIO;
7eb95156 122 c += ip6_frags.rnd;
1da177e4
LT
123 __jhash_mix(a, b, c);
124
e69a4adc
AV
125 a += (__force u32)saddr->s6_addr32[3];
126 b += (__force u32)daddr->s6_addr32[0];
127 c += (__force u32)daddr->s6_addr32[1];
1da177e4
LT
128 __jhash_mix(a, b, c);
129
e69a4adc
AV
130 a += (__force u32)daddr->s6_addr32[2];
131 b += (__force u32)daddr->s6_addr32[3];
132 c += (__force u32)id;
1da177e4
LT
133 __jhash_mix(a, b, c);
134
7eb95156 135 return c & (INETFRAGS_HASHSZ - 1);
1da177e4
LT
136}
137
1da177e4
LT
138static void ip6_frag_secret_rebuild(unsigned long dummy)
139{
140 unsigned long now = jiffies;
141 int i;
142
7eb95156
PE
143 write_lock(&ip6_frags.lock);
144 get_random_bytes(&ip6_frags.rnd, sizeof(u32));
145 for (i = 0; i < INETFRAGS_HASHSZ; i++) {
1da177e4 146 struct frag_queue *q;
e7c8a41e 147 struct hlist_node *p, *n;
1da177e4 148
7eb95156 149 hlist_for_each_entry_safe(q, p, n, &ip6_frags.hash[i], q.list) {
1da177e4
LT
150 unsigned int hval = ip6qhashfn(q->id,
151 &q->saddr,
152 &q->daddr);
153
154 if (hval != i) {
5ab11c98 155 hlist_del(&q->q.list);
1da177e4
LT
156
157 /* Relink to new hash chain. */
5ab11c98 158 hlist_add_head(&q->q.list,
7eb95156 159 &ip6_frags.hash[hval]);
1da177e4 160
e7c8a41e 161 }
1da177e4
LT
162 }
163 }
7eb95156 164 write_unlock(&ip6_frags.lock);
1da177e4 165
04128f23 166 mod_timer(&ip6_frags.secret_timer, now + ip6_frags_ctl.secret_interval);
1da177e4
LT
167}
168
1da177e4
LT
169/* Memory Tracking Functions. */
170static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
171{
172 if (work)
173 *work -= skb->truesize;
7eb95156 174 atomic_sub(skb->truesize, &ip6_frags.mem);
1da177e4
LT
175 kfree_skb(skb);
176}
177
178static inline void frag_free_queue(struct frag_queue *fq, int *work)
179{
180 if (work)
181 *work -= sizeof(struct frag_queue);
7eb95156 182 atomic_sub(sizeof(struct frag_queue), &ip6_frags.mem);
1da177e4
LT
183 kfree(fq);
184}
185
186static inline struct frag_queue *frag_alloc_queue(void)
187{
78c784c4 188 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
1da177e4
LT
189
190 if(!fq)
191 return NULL;
7eb95156 192 atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
1da177e4
LT
193 return fq;
194}
195
196/* Destruction primitives. */
197
198/* Complete destruction of fq. */
199static void ip6_frag_destroy(struct frag_queue *fq, int *work)
200{
201 struct sk_buff *fp;
202
5ab11c98
PE
203 BUG_TRAP(fq->q.last_in&COMPLETE);
204 BUG_TRAP(del_timer(&fq->q.timer) == 0);
1da177e4
LT
205
206 /* Release all fragment data. */
5ab11c98 207 fp = fq->q.fragments;
1da177e4
LT
208 while (fp) {
209 struct sk_buff *xp = fp->next;
210
211 frag_kfree_skb(fp, work);
212 fp = xp;
213 }
214
215 frag_free_queue(fq, work);
216}
217
218static __inline__ void fq_put(struct frag_queue *fq, int *work)
219{
5ab11c98 220 if (atomic_dec_and_test(&fq->q.refcnt))
1da177e4
LT
221 ip6_frag_destroy(fq, work);
222}
223
224/* Kill fq entry. It is not destroyed immediately,
225 * because caller (and someone more) holds reference count.
226 */
227static __inline__ void fq_kill(struct frag_queue *fq)
228{
277e650d 229 inet_frag_kill(&fq->q, &ip6_frags);
1da177e4
LT
230}
231
a11d206d 232static void ip6_evictor(struct inet6_dev *idev)
1da177e4
LT
233{
234 struct frag_queue *fq;
235 struct list_head *tmp;
236 int work;
237
04128f23 238 work = atomic_read(&ip6_frags.mem) - ip6_frags_ctl.low_thresh;
1da177e4
LT
239 if (work <= 0)
240 return;
241
242 while(work > 0) {
7eb95156
PE
243 read_lock(&ip6_frags.lock);
244 if (list_empty(&ip6_frags.lru_list)) {
245 read_unlock(&ip6_frags.lock);
1da177e4
LT
246 return;
247 }
7eb95156 248 tmp = ip6_frags.lru_list.next;
5ab11c98
PE
249 fq = list_entry(tmp, struct frag_queue, q.lru_list);
250 atomic_inc(&fq->q.refcnt);
7eb95156 251 read_unlock(&ip6_frags.lock);
1da177e4 252
5ab11c98
PE
253 spin_lock(&fq->q.lock);
254 if (!(fq->q.last_in&COMPLETE))
1da177e4 255 fq_kill(fq);
5ab11c98 256 spin_unlock(&fq->q.lock);
1da177e4
LT
257
258 fq_put(fq, &work);
a11d206d 259 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
1da177e4
LT
260 }
261}
262
263static void ip6_frag_expire(unsigned long data)
264{
265 struct frag_queue *fq = (struct frag_queue *) data;
a11d206d 266 struct net_device *dev = NULL;
1da177e4 267
5ab11c98 268 spin_lock(&fq->q.lock);
1da177e4 269
5ab11c98 270 if (fq->q.last_in & COMPLETE)
1da177e4
LT
271 goto out;
272
273 fq_kill(fq);
274
881d966b 275 dev = dev_get_by_index(&init_net, fq->iif);
a11d206d
YH
276 if (!dev)
277 goto out;
278
279 rcu_read_lock();
280 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
281 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
282 rcu_read_unlock();
1da177e4 283
78c784c4 284 /* Don't send error if the first segment did not arrive. */
5ab11c98 285 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
78c784c4
IO
286 goto out;
287
78c784c4
IO
288 /*
289 But use as source device on which LAST ARRIVED
290 segment was received. And do not use fq->dev
291 pointer directly, device might already disappeared.
292 */
5ab11c98
PE
293 fq->q.fragments->dev = dev;
294 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
1da177e4 295out:
a11d206d
YH
296 if (dev)
297 dev_put(dev);
5ab11c98 298 spin_unlock(&fq->q.lock);
1da177e4
LT
299 fq_put(fq, NULL);
300}
301
302/* Creation primitives. */
303
304
f6596f9d 305static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
1da177e4
LT
306{
307 struct frag_queue *fq;
f6596f9d 308 unsigned int hash;
e7c8a41e
YK
309#ifdef CONFIG_SMP
310 struct hlist_node *n;
311#endif
1da177e4 312
7eb95156 313 write_lock(&ip6_frags.lock);
f6596f9d 314 hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
1da177e4 315#ifdef CONFIG_SMP
7eb95156 316 hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
1ab1457c 317 if (fq->id == fq_in->id &&
1da177e4
LT
318 ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
319 ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
5ab11c98 320 atomic_inc(&fq->q.refcnt);
7eb95156 321 write_unlock(&ip6_frags.lock);
5ab11c98 322 fq_in->q.last_in |= COMPLETE;
1da177e4
LT
323 fq_put(fq_in, NULL);
324 return fq;
325 }
326 }
327#endif
328 fq = fq_in;
329
04128f23 330 if (!mod_timer(&fq->q.timer, jiffies + ip6_frags_ctl.timeout))
5ab11c98 331 atomic_inc(&fq->q.refcnt);
1da177e4 332
5ab11c98 333 atomic_inc(&fq->q.refcnt);
7eb95156 334 hlist_add_head(&fq->q.list, &ip6_frags.hash[hash]);
5ab11c98 335 INIT_LIST_HEAD(&fq->q.lru_list);
7eb95156
PE
336 list_add_tail(&fq->q.lru_list, &ip6_frags.lru_list);
337 ip6_frags.nqueues++;
338 write_unlock(&ip6_frags.lock);
1da177e4
LT
339 return fq;
340}
341
342
343static struct frag_queue *
e69a4adc 344ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
a11d206d 345 struct inet6_dev *idev)
1da177e4
LT
346{
347 struct frag_queue *fq;
348
349 if ((fq = frag_alloc_queue()) == NULL)
350 goto oom;
351
1da177e4
LT
352 fq->id = id;
353 ipv6_addr_copy(&fq->saddr, src);
354 ipv6_addr_copy(&fq->daddr, dst);
355
5ab11c98
PE
356 init_timer(&fq->q.timer);
357 fq->q.timer.function = ip6_frag_expire;
358 fq->q.timer.data = (long) fq;
359 spin_lock_init(&fq->q.lock);
360 atomic_set(&fq->q.refcnt, 1);
1da177e4 361
f6596f9d 362 return ip6_frag_intern(fq);
1da177e4
LT
363
364oom:
a11d206d 365 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
1da177e4
LT
366 return NULL;
367}
368
369static __inline__ struct frag_queue *
e69a4adc 370fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
a11d206d 371 struct inet6_dev *idev)
1da177e4
LT
372{
373 struct frag_queue *fq;
e7c8a41e 374 struct hlist_node *n;
f6596f9d 375 unsigned int hash;
1da177e4 376
7eb95156 377 read_lock(&ip6_frags.lock);
f6596f9d 378 hash = ip6qhashfn(id, src, dst);
7eb95156 379 hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
1ab1457c 380 if (fq->id == id &&
1da177e4
LT
381 ipv6_addr_equal(src, &fq->saddr) &&
382 ipv6_addr_equal(dst, &fq->daddr)) {
5ab11c98 383 atomic_inc(&fq->q.refcnt);
7eb95156 384 read_unlock(&ip6_frags.lock);
1da177e4
LT
385 return fq;
386 }
387 }
7eb95156 388 read_unlock(&ip6_frags.lock);
1da177e4 389
a11d206d 390 return ip6_frag_create(id, src, dst, idev);
1da177e4
LT
391}
392
393
f61944ef 394static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1da177e4
LT
395 struct frag_hdr *fhdr, int nhoff)
396{
397 struct sk_buff *prev, *next;
f61944ef 398 struct net_device *dev;
1da177e4
LT
399 int offset, end;
400
5ab11c98 401 if (fq->q.last_in & COMPLETE)
1da177e4
LT
402 goto err;
403
404 offset = ntohs(fhdr->frag_off) & ~0x7;
0660e03f
ACM
405 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
406 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1da177e4
LT
407
408 if ((unsigned int)end > IPV6_MAXPLEN) {
a11d206d
YH
409 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
410 IPSTATS_MIB_INHDRERRORS);
d56f90a7
ACM
411 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
412 ((u8 *)&fhdr->frag_off -
413 skb_network_header(skb)));
f61944ef 414 return -1;
1da177e4
LT
415 }
416
d56f90a7
ACM
417 if (skb->ip_summed == CHECKSUM_COMPLETE) {
418 const unsigned char *nh = skb_network_header(skb);
1ab1457c 419 skb->csum = csum_sub(skb->csum,
d56f90a7
ACM
420 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
421 0));
422 }
1da177e4
LT
423
424 /* Is this the final fragment? */
425 if (!(fhdr->frag_off & htons(IP6_MF))) {
426 /* If we already have some bits beyond end
427 * or have different end, the segment is corrupted.
428 */
5ab11c98
PE
429 if (end < fq->q.len ||
430 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
1da177e4 431 goto err;
5ab11c98
PE
432 fq->q.last_in |= LAST_IN;
433 fq->q.len = end;
1da177e4
LT
434 } else {
435 /* Check if the fragment is rounded to 8 bytes.
436 * Required by the RFC.
437 */
438 if (end & 0x7) {
439 /* RFC2460 says always send parameter problem in
440 * this case. -DaveM
441 */
a11d206d
YH
442 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
443 IPSTATS_MIB_INHDRERRORS);
1ab1457c 444 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
1da177e4 445 offsetof(struct ipv6hdr, payload_len));
f61944ef 446 return -1;
1da177e4 447 }
5ab11c98 448 if (end > fq->q.len) {
1da177e4 449 /* Some bits beyond end -> corruption. */
5ab11c98 450 if (fq->q.last_in & LAST_IN)
1da177e4 451 goto err;
5ab11c98 452 fq->q.len = end;
1da177e4
LT
453 }
454 }
455
456 if (end == offset)
457 goto err;
458
459 /* Point into the IP datagram 'data' part. */
460 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
461 goto err;
1ab1457c 462
42ca89c1
SH
463 if (pskb_trim_rcsum(skb, end - offset))
464 goto err;
1da177e4
LT
465
466 /* Find out which fragments are in front and at the back of us
467 * in the chain of fragments so far. We must know where to put
468 * this fragment, right?
469 */
470 prev = NULL;
5ab11c98 471 for(next = fq->q.fragments; next != NULL; next = next->next) {
1da177e4
LT
472 if (FRAG6_CB(next)->offset >= offset)
473 break; /* bingo! */
474 prev = next;
475 }
476
477 /* We found where to put this one. Check for overlap with
478 * preceding fragment, and, if needed, align things so that
479 * any overlaps are eliminated.
480 */
481 if (prev) {
482 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
483
484 if (i > 0) {
485 offset += i;
486 if (end <= offset)
487 goto err;
488 if (!pskb_pull(skb, i))
489 goto err;
490 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
491 skb->ip_summed = CHECKSUM_NONE;
492 }
493 }
494
495 /* Look for overlap with succeeding segments.
496 * If we can merge fragments, do it.
497 */
498 while (next && FRAG6_CB(next)->offset < end) {
499 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
500
501 if (i < next->len) {
502 /* Eat head of the next overlapped fragment
503 * and leave the loop. The next ones cannot overlap.
504 */
505 if (!pskb_pull(next, i))
506 goto err;
507 FRAG6_CB(next)->offset += i; /* next fragment */
5ab11c98 508 fq->q.meat -= i;
1da177e4
LT
509 if (next->ip_summed != CHECKSUM_UNNECESSARY)
510 next->ip_summed = CHECKSUM_NONE;
511 break;
512 } else {
513 struct sk_buff *free_it = next;
514
515 /* Old fragment is completely overridden with
516 * new one drop it.
517 */
518 next = next->next;
519
520 if (prev)
521 prev->next = next;
522 else
5ab11c98 523 fq->q.fragments = next;
1da177e4 524
5ab11c98 525 fq->q.meat -= free_it->len;
1da177e4
LT
526 frag_kfree_skb(free_it, NULL);
527 }
528 }
529
530 FRAG6_CB(skb)->offset = offset;
531
532 /* Insert this fragment in the chain of fragments. */
533 skb->next = next;
534 if (prev)
535 prev->next = skb;
536 else
5ab11c98 537 fq->q.fragments = skb;
1da177e4 538
f61944ef
HX
539 dev = skb->dev;
540 if (dev) {
541 fq->iif = dev->ifindex;
542 skb->dev = NULL;
543 }
5ab11c98
PE
544 fq->q.stamp = skb->tstamp;
545 fq->q.meat += skb->len;
7eb95156 546 atomic_add(skb->truesize, &ip6_frags.mem);
1da177e4
LT
547
548 /* The first fragment.
549 * nhoffset is obtained from the first fragment, of course.
550 */
551 if (offset == 0) {
552 fq->nhoffset = nhoff;
5ab11c98 553 fq->q.last_in |= FIRST_IN;
1da177e4 554 }
f61944ef 555
5ab11c98 556 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
f61944ef
HX
557 return ip6_frag_reasm(fq, prev, dev);
558
7eb95156
PE
559 write_lock(&ip6_frags.lock);
560 list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
561 write_unlock(&ip6_frags.lock);
f61944ef 562 return -1;
1da177e4
LT
563
564err:
a11d206d 565 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
1da177e4 566 kfree_skb(skb);
f61944ef 567 return -1;
1da177e4
LT
568}
569
570/*
571 * Check if this packet is complete.
572 * Returns NULL on failure by any reason, and pointer
573 * to current nexthdr field in reassembled frame.
574 *
575 * It is called with locked fq, and caller must check that
576 * queue is eligible for reassembly i.e. it is not COMPLETE,
577 * the last and the first frames arrived and all the bits are here.
578 */
f61944ef 579static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
1da177e4
LT
580 struct net_device *dev)
581{
5ab11c98 582 struct sk_buff *fp, *head = fq->q.fragments;
1da177e4
LT
583 int payload_len;
584 unsigned int nhoff;
585
586 fq_kill(fq);
587
f61944ef
HX
588 /* Make the one we just received the head. */
589 if (prev) {
590 head = prev->next;
591 fp = skb_clone(head, GFP_ATOMIC);
592
593 if (!fp)
594 goto out_oom;
595
596 fp->next = head->next;
597 prev->next = fp;
598
5ab11c98
PE
599 skb_morph(head, fq->q.fragments);
600 head->next = fq->q.fragments->next;
f61944ef 601
5ab11c98
PE
602 kfree_skb(fq->q.fragments);
603 fq->q.fragments = head;
f61944ef
HX
604 }
605
1da177e4
LT
606 BUG_TRAP(head != NULL);
607 BUG_TRAP(FRAG6_CB(head)->offset == 0);
608
609 /* Unfragmented part is taken from the first segment. */
d56f90a7 610 payload_len = ((head->data - skb_network_header(head)) -
5ab11c98 611 sizeof(struct ipv6hdr) + fq->q.len -
d56f90a7 612 sizeof(struct frag_hdr));
1da177e4
LT
613 if (payload_len > IPV6_MAXPLEN)
614 goto out_oversize;
615
616 /* Head of list must not be cloned. */
617 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
618 goto out_oom;
619
620 /* If the first fragment is fragmented itself, we split
621 * it to two chunks: the first with data and paged part
622 * and the second, holding only fragments. */
623 if (skb_shinfo(head)->frag_list) {
624 struct sk_buff *clone;
625 int i, plen = 0;
626
627 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
628 goto out_oom;
629 clone->next = head->next;
630 head->next = clone;
631 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
632 skb_shinfo(head)->frag_list = NULL;
633 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
634 plen += skb_shinfo(head)->frags[i].size;
635 clone->len = clone->data_len = head->data_len - plen;
636 head->data_len -= clone->len;
637 head->len -= clone->len;
638 clone->csum = 0;
639 clone->ip_summed = head->ip_summed;
7eb95156 640 atomic_add(clone->truesize, &ip6_frags.mem);
1da177e4
LT
641 }
642
643 /* We have to remove fragment header from datagram and to relocate
644 * header in order to calculate ICV correctly. */
645 nhoff = fq->nhoffset;
b0e380b1 646 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
1ab1457c 647 memmove(head->head + sizeof(struct frag_hdr), head->head,
1da177e4 648 (head->data - head->head) - sizeof(struct frag_hdr));
b0e380b1
ACM
649 head->mac_header += sizeof(struct frag_hdr);
650 head->network_header += sizeof(struct frag_hdr);
1da177e4
LT
651
652 skb_shinfo(head)->frag_list = head->next;
badff6d0 653 skb_reset_transport_header(head);
d56f90a7 654 skb_push(head, head->data - skb_network_header(head));
7eb95156 655 atomic_sub(head->truesize, &ip6_frags.mem);
1da177e4
LT
656
657 for (fp=head->next; fp; fp = fp->next) {
658 head->data_len += fp->len;
659 head->len += fp->len;
660 if (head->ip_summed != fp->ip_summed)
661 head->ip_summed = CHECKSUM_NONE;
84fa7933 662 else if (head->ip_summed == CHECKSUM_COMPLETE)
1da177e4
LT
663 head->csum = csum_add(head->csum, fp->csum);
664 head->truesize += fp->truesize;
7eb95156 665 atomic_sub(fp->truesize, &ip6_frags.mem);
1da177e4
LT
666 }
667
668 head->next = NULL;
669 head->dev = dev;
5ab11c98 670 head->tstamp = fq->q.stamp;
0660e03f 671 ipv6_hdr(head)->payload_len = htons(payload_len);
951dbc8a 672 IP6CB(head)->nhoff = nhoff;
1da177e4 673
1da177e4 674 /* Yes, and fold redundant checksum back. 8) */
84fa7933 675 if (head->ip_summed == CHECKSUM_COMPLETE)
d56f90a7 676 head->csum = csum_partial(skb_network_header(head),
cfe1fc77 677 skb_network_header_len(head),
d56f90a7 678 head->csum);
1da177e4 679
a11d206d
YH
680 rcu_read_lock();
681 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
682 rcu_read_unlock();
5ab11c98 683 fq->q.fragments = NULL;
1da177e4
LT
684 return 1;
685
686out_oversize:
687 if (net_ratelimit())
688 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
689 goto out_fail;
690out_oom:
691 if (net_ratelimit())
692 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
693out_fail:
a11d206d
YH
694 rcu_read_lock();
695 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
696 rcu_read_unlock();
1da177e4
LT
697 return -1;
698}
699
951dbc8a 700static int ipv6_frag_rcv(struct sk_buff **skbp)
1da177e4 701{
1ab1457c 702 struct sk_buff *skb = *skbp;
1da177e4
LT
703 struct frag_hdr *fhdr;
704 struct frag_queue *fq;
0660e03f 705 struct ipv6hdr *hdr = ipv6_hdr(skb);
1da177e4 706
a11d206d 707 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
1da177e4
LT
708
709 /* Jumbo payload inhibits frag. header */
710 if (hdr->payload_len==0) {
a11d206d 711 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
cfe1fc77
ACM
712 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
713 skb_network_header_len(skb));
1da177e4
LT
714 return -1;
715 }
ea2ae17d
ACM
716 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
717 sizeof(struct frag_hdr)))) {
a11d206d 718 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
cfe1fc77
ACM
719 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
720 skb_network_header_len(skb));
1da177e4
LT
721 return -1;
722 }
723
0660e03f 724 hdr = ipv6_hdr(skb);
9c70220b 725 fhdr = (struct frag_hdr *)skb_transport_header(skb);
1da177e4
LT
726
727 if (!(fhdr->frag_off & htons(0xFFF9))) {
728 /* It is not a fragmented frame */
b0e380b1 729 skb->transport_header += sizeof(struct frag_hdr);
a11d206d 730 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
1da177e4 731
d56f90a7 732 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
1da177e4
LT
733 return 1;
734 }
735
04128f23 736 if (atomic_read(&ip6_frags.mem) > ip6_frags_ctl.high_thresh)
a11d206d 737 ip6_evictor(ip6_dst_idev(skb->dst));
1da177e4 738
a11d206d
YH
739 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
740 ip6_dst_idev(skb->dst))) != NULL) {
f61944ef 741 int ret;
1da177e4 742
5ab11c98 743 spin_lock(&fq->q.lock);
1da177e4 744
f61944ef 745 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
1da177e4 746
5ab11c98 747 spin_unlock(&fq->q.lock);
1da177e4
LT
748 fq_put(fq, NULL);
749 return ret;
750 }
751
a11d206d 752 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
1da177e4
LT
753 kfree_skb(skb);
754 return -1;
755}
756
757static struct inet6_protocol frag_protocol =
758{
759 .handler = ipv6_frag_rcv,
760 .flags = INET6_PROTO_NOPOLICY,
761};
762
763void __init ipv6_frag_init(void)
764{
765 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
766 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
767
7eb95156
PE
768 init_timer(&ip6_frags.secret_timer);
769 ip6_frags.secret_timer.function = ip6_frag_secret_rebuild;
04128f23 770 ip6_frags.secret_timer.expires = jiffies + ip6_frags_ctl.secret_interval;
7eb95156 771 add_timer(&ip6_frags.secret_timer);
1da177e4 772
04128f23 773 ip6_frags.ctl = &ip6_frags_ctl;
7eb95156 774 inet_frags_init(&ip6_frags);
1da177e4 775}