]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv6/reassembly.c
[INET]: Consolidate xxx_frag_intern
[net-next-2.6.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
321a3a99 138static unsigned int ip6_hashfn(struct inet_frag_queue *q)
1da177e4 139{
321a3a99 140 struct frag_queue *fq;
1da177e4 141
321a3a99
PE
142 fq = container_of(q, struct frag_queue, q);
143 return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr);
1da177e4
LT
144}
145
2588fe1d
PE
146int ip6_frag_equal(struct inet_frag_queue *q1, struct inet_frag_queue *q2)
147{
148 struct frag_queue *fq1, *fq2;
149
150 fq1 = container_of(q1, struct frag_queue, q);
151 fq2 = container_of(q2, struct frag_queue, q);
152 return (fq1->id == fq2->id &&
153 ipv6_addr_equal(&fq2->saddr, &fq1->saddr) &&
154 ipv6_addr_equal(&fq2->daddr, &fq1->daddr));
155}
156EXPORT_SYMBOL(ip6_frag_equal);
157
1da177e4
LT
158/* Memory Tracking Functions. */
159static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
160{
161 if (work)
162 *work -= skb->truesize;
7eb95156 163 atomic_sub(skb->truesize, &ip6_frags.mem);
1da177e4
LT
164 kfree_skb(skb);
165}
166
1e4b8287 167static void ip6_frag_free(struct inet_frag_queue *fq)
1da177e4 168{
1e4b8287 169 kfree(container_of(fq, struct frag_queue, q));
1da177e4
LT
170}
171
172static inline struct frag_queue *frag_alloc_queue(void)
173{
78c784c4 174 struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
1da177e4
LT
175
176 if(!fq)
177 return NULL;
7eb95156 178 atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
1da177e4
LT
179 return fq;
180}
181
182/* Destruction primitives. */
183
4b6cb5d8 184static __inline__ void fq_put(struct frag_queue *fq)
1da177e4 185{
762cc408 186 inet_frag_put(&fq->q, &ip6_frags);
1da177e4
LT
187}
188
189/* Kill fq entry. It is not destroyed immediately,
190 * because caller (and someone more) holds reference count.
191 */
192static __inline__ void fq_kill(struct frag_queue *fq)
193{
277e650d 194 inet_frag_kill(&fq->q, &ip6_frags);
1da177e4
LT
195}
196
a11d206d 197static void ip6_evictor(struct inet6_dev *idev)
1da177e4 198{
8e7999c4
PE
199 int evicted;
200
201 evicted = inet_frag_evictor(&ip6_frags);
202 if (evicted)
203 IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted);
1da177e4
LT
204}
205
206static void ip6_frag_expire(unsigned long data)
207{
208 struct frag_queue *fq = (struct frag_queue *) data;
a11d206d 209 struct net_device *dev = NULL;
1da177e4 210
5ab11c98 211 spin_lock(&fq->q.lock);
1da177e4 212
5ab11c98 213 if (fq->q.last_in & COMPLETE)
1da177e4
LT
214 goto out;
215
216 fq_kill(fq);
217
881d966b 218 dev = dev_get_by_index(&init_net, fq->iif);
a11d206d
YH
219 if (!dev)
220 goto out;
221
222 rcu_read_lock();
223 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
224 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
225 rcu_read_unlock();
1da177e4 226
78c784c4 227 /* Don't send error if the first segment did not arrive. */
5ab11c98 228 if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
78c784c4
IO
229 goto out;
230
78c784c4
IO
231 /*
232 But use as source device on which LAST ARRIVED
233 segment was received. And do not use fq->dev
234 pointer directly, device might already disappeared.
235 */
5ab11c98
PE
236 fq->q.fragments->dev = dev;
237 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
1da177e4 238out:
a11d206d
YH
239 if (dev)
240 dev_put(dev);
5ab11c98 241 spin_unlock(&fq->q.lock);
4b6cb5d8 242 fq_put(fq);
1da177e4
LT
243}
244
245/* Creation primitives. */
246
247
fd9e6354
PE
248static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in,
249 unsigned int hash)
1da177e4 250{
2588fe1d 251 struct inet_frag_queue *q;
1da177e4 252
2588fe1d
PE
253 q = inet_frag_intern(&fq_in->q, &ip6_frags, hash);
254 return container_of(q, struct frag_queue, q);
1da177e4
LT
255}
256
257
258static struct frag_queue *
e69a4adc 259ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
fd9e6354 260 struct inet6_dev *idev, unsigned int hash)
1da177e4
LT
261{
262 struct frag_queue *fq;
263
264 if ((fq = frag_alloc_queue()) == NULL)
265 goto oom;
266
1da177e4
LT
267 fq->id = id;
268 ipv6_addr_copy(&fq->saddr, src);
269 ipv6_addr_copy(&fq->daddr, dst);
270
5ab11c98
PE
271 init_timer(&fq->q.timer);
272 fq->q.timer.function = ip6_frag_expire;
273 fq->q.timer.data = (long) fq;
274 spin_lock_init(&fq->q.lock);
275 atomic_set(&fq->q.refcnt, 1);
1da177e4 276
fd9e6354 277 return ip6_frag_intern(fq, hash);
1da177e4
LT
278
279oom:
a11d206d 280 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
1da177e4
LT
281 return NULL;
282}
283
284static __inline__ struct frag_queue *
e69a4adc 285fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
a11d206d 286 struct inet6_dev *idev)
1da177e4
LT
287{
288 struct frag_queue *fq;
e7c8a41e 289 struct hlist_node *n;
f6596f9d 290 unsigned int hash;
1da177e4 291
7eb95156 292 read_lock(&ip6_frags.lock);
f6596f9d 293 hash = ip6qhashfn(id, src, dst);
7eb95156 294 hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
1ab1457c 295 if (fq->id == id &&
1da177e4
LT
296 ipv6_addr_equal(src, &fq->saddr) &&
297 ipv6_addr_equal(dst, &fq->daddr)) {
5ab11c98 298 atomic_inc(&fq->q.refcnt);
7eb95156 299 read_unlock(&ip6_frags.lock);
1da177e4
LT
300 return fq;
301 }
302 }
7eb95156 303 read_unlock(&ip6_frags.lock);
1da177e4 304
fd9e6354 305 return ip6_frag_create(id, src, dst, idev, hash);
1da177e4
LT
306}
307
308
f61944ef 309static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1da177e4
LT
310 struct frag_hdr *fhdr, int nhoff)
311{
312 struct sk_buff *prev, *next;
f61944ef 313 struct net_device *dev;
1da177e4
LT
314 int offset, end;
315
5ab11c98 316 if (fq->q.last_in & COMPLETE)
1da177e4
LT
317 goto err;
318
319 offset = ntohs(fhdr->frag_off) & ~0x7;
0660e03f
ACM
320 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
321 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1da177e4
LT
322
323 if ((unsigned int)end > IPV6_MAXPLEN) {
a11d206d
YH
324 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
325 IPSTATS_MIB_INHDRERRORS);
d56f90a7
ACM
326 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
327 ((u8 *)&fhdr->frag_off -
328 skb_network_header(skb)));
f61944ef 329 return -1;
1da177e4
LT
330 }
331
d56f90a7
ACM
332 if (skb->ip_summed == CHECKSUM_COMPLETE) {
333 const unsigned char *nh = skb_network_header(skb);
1ab1457c 334 skb->csum = csum_sub(skb->csum,
d56f90a7
ACM
335 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
336 0));
337 }
1da177e4
LT
338
339 /* Is this the final fragment? */
340 if (!(fhdr->frag_off & htons(IP6_MF))) {
341 /* If we already have some bits beyond end
342 * or have different end, the segment is corrupted.
343 */
5ab11c98
PE
344 if (end < fq->q.len ||
345 ((fq->q.last_in & LAST_IN) && end != fq->q.len))
1da177e4 346 goto err;
5ab11c98
PE
347 fq->q.last_in |= LAST_IN;
348 fq->q.len = end;
1da177e4
LT
349 } else {
350 /* Check if the fragment is rounded to 8 bytes.
351 * Required by the RFC.
352 */
353 if (end & 0x7) {
354 /* RFC2460 says always send parameter problem in
355 * this case. -DaveM
356 */
a11d206d
YH
357 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
358 IPSTATS_MIB_INHDRERRORS);
1ab1457c 359 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
1da177e4 360 offsetof(struct ipv6hdr, payload_len));
f61944ef 361 return -1;
1da177e4 362 }
5ab11c98 363 if (end > fq->q.len) {
1da177e4 364 /* Some bits beyond end -> corruption. */
5ab11c98 365 if (fq->q.last_in & LAST_IN)
1da177e4 366 goto err;
5ab11c98 367 fq->q.len = end;
1da177e4
LT
368 }
369 }
370
371 if (end == offset)
372 goto err;
373
374 /* Point into the IP datagram 'data' part. */
375 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
376 goto err;
1ab1457c 377
42ca89c1
SH
378 if (pskb_trim_rcsum(skb, end - offset))
379 goto err;
1da177e4
LT
380
381 /* Find out which fragments are in front and at the back of us
382 * in the chain of fragments so far. We must know where to put
383 * this fragment, right?
384 */
385 prev = NULL;
5ab11c98 386 for(next = fq->q.fragments; next != NULL; next = next->next) {
1da177e4
LT
387 if (FRAG6_CB(next)->offset >= offset)
388 break; /* bingo! */
389 prev = next;
390 }
391
392 /* We found where to put this one. Check for overlap with
393 * preceding fragment, and, if needed, align things so that
394 * any overlaps are eliminated.
395 */
396 if (prev) {
397 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
398
399 if (i > 0) {
400 offset += i;
401 if (end <= offset)
402 goto err;
403 if (!pskb_pull(skb, i))
404 goto err;
405 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
406 skb->ip_summed = CHECKSUM_NONE;
407 }
408 }
409
410 /* Look for overlap with succeeding segments.
411 * If we can merge fragments, do it.
412 */
413 while (next && FRAG6_CB(next)->offset < end) {
414 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
415
416 if (i < next->len) {
417 /* Eat head of the next overlapped fragment
418 * and leave the loop. The next ones cannot overlap.
419 */
420 if (!pskb_pull(next, i))
421 goto err;
422 FRAG6_CB(next)->offset += i; /* next fragment */
5ab11c98 423 fq->q.meat -= i;
1da177e4
LT
424 if (next->ip_summed != CHECKSUM_UNNECESSARY)
425 next->ip_summed = CHECKSUM_NONE;
426 break;
427 } else {
428 struct sk_buff *free_it = next;
429
430 /* Old fragment is completely overridden with
431 * new one drop it.
432 */
433 next = next->next;
434
435 if (prev)
436 prev->next = next;
437 else
5ab11c98 438 fq->q.fragments = next;
1da177e4 439
5ab11c98 440 fq->q.meat -= free_it->len;
1da177e4
LT
441 frag_kfree_skb(free_it, NULL);
442 }
443 }
444
445 FRAG6_CB(skb)->offset = offset;
446
447 /* Insert this fragment in the chain of fragments. */
448 skb->next = next;
449 if (prev)
450 prev->next = skb;
451 else
5ab11c98 452 fq->q.fragments = skb;
1da177e4 453
f61944ef
HX
454 dev = skb->dev;
455 if (dev) {
456 fq->iif = dev->ifindex;
457 skb->dev = NULL;
458 }
5ab11c98
PE
459 fq->q.stamp = skb->tstamp;
460 fq->q.meat += skb->len;
7eb95156 461 atomic_add(skb->truesize, &ip6_frags.mem);
1da177e4
LT
462
463 /* The first fragment.
464 * nhoffset is obtained from the first fragment, of course.
465 */
466 if (offset == 0) {
467 fq->nhoffset = nhoff;
5ab11c98 468 fq->q.last_in |= FIRST_IN;
1da177e4 469 }
f61944ef 470
5ab11c98 471 if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
f61944ef
HX
472 return ip6_frag_reasm(fq, prev, dev);
473
7eb95156
PE
474 write_lock(&ip6_frags.lock);
475 list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
476 write_unlock(&ip6_frags.lock);
f61944ef 477 return -1;
1da177e4
LT
478
479err:
a11d206d 480 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
1da177e4 481 kfree_skb(skb);
f61944ef 482 return -1;
1da177e4
LT
483}
484
485/*
486 * Check if this packet is complete.
487 * Returns NULL on failure by any reason, and pointer
488 * to current nexthdr field in reassembled frame.
489 *
490 * It is called with locked fq, and caller must check that
491 * queue is eligible for reassembly i.e. it is not COMPLETE,
492 * the last and the first frames arrived and all the bits are here.
493 */
f61944ef 494static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
1da177e4
LT
495 struct net_device *dev)
496{
5ab11c98 497 struct sk_buff *fp, *head = fq->q.fragments;
1da177e4
LT
498 int payload_len;
499 unsigned int nhoff;
500
501 fq_kill(fq);
502
f61944ef
HX
503 /* Make the one we just received the head. */
504 if (prev) {
505 head = prev->next;
506 fp = skb_clone(head, GFP_ATOMIC);
507
508 if (!fp)
509 goto out_oom;
510
511 fp->next = head->next;
512 prev->next = fp;
513
5ab11c98
PE
514 skb_morph(head, fq->q.fragments);
515 head->next = fq->q.fragments->next;
f61944ef 516
5ab11c98
PE
517 kfree_skb(fq->q.fragments);
518 fq->q.fragments = head;
f61944ef
HX
519 }
520
1da177e4
LT
521 BUG_TRAP(head != NULL);
522 BUG_TRAP(FRAG6_CB(head)->offset == 0);
523
524 /* Unfragmented part is taken from the first segment. */
d56f90a7 525 payload_len = ((head->data - skb_network_header(head)) -
5ab11c98 526 sizeof(struct ipv6hdr) + fq->q.len -
d56f90a7 527 sizeof(struct frag_hdr));
1da177e4
LT
528 if (payload_len > IPV6_MAXPLEN)
529 goto out_oversize;
530
531 /* Head of list must not be cloned. */
532 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
533 goto out_oom;
534
535 /* If the first fragment is fragmented itself, we split
536 * it to two chunks: the first with data and paged part
537 * and the second, holding only fragments. */
538 if (skb_shinfo(head)->frag_list) {
539 struct sk_buff *clone;
540 int i, plen = 0;
541
542 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
543 goto out_oom;
544 clone->next = head->next;
545 head->next = clone;
546 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
547 skb_shinfo(head)->frag_list = NULL;
548 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
549 plen += skb_shinfo(head)->frags[i].size;
550 clone->len = clone->data_len = head->data_len - plen;
551 head->data_len -= clone->len;
552 head->len -= clone->len;
553 clone->csum = 0;
554 clone->ip_summed = head->ip_summed;
7eb95156 555 atomic_add(clone->truesize, &ip6_frags.mem);
1da177e4
LT
556 }
557
558 /* We have to remove fragment header from datagram and to relocate
559 * header in order to calculate ICV correctly. */
560 nhoff = fq->nhoffset;
b0e380b1 561 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
1ab1457c 562 memmove(head->head + sizeof(struct frag_hdr), head->head,
1da177e4 563 (head->data - head->head) - sizeof(struct frag_hdr));
b0e380b1
ACM
564 head->mac_header += sizeof(struct frag_hdr);
565 head->network_header += sizeof(struct frag_hdr);
1da177e4
LT
566
567 skb_shinfo(head)->frag_list = head->next;
badff6d0 568 skb_reset_transport_header(head);
d56f90a7 569 skb_push(head, head->data - skb_network_header(head));
7eb95156 570 atomic_sub(head->truesize, &ip6_frags.mem);
1da177e4
LT
571
572 for (fp=head->next; fp; fp = fp->next) {
573 head->data_len += fp->len;
574 head->len += fp->len;
575 if (head->ip_summed != fp->ip_summed)
576 head->ip_summed = CHECKSUM_NONE;
84fa7933 577 else if (head->ip_summed == CHECKSUM_COMPLETE)
1da177e4
LT
578 head->csum = csum_add(head->csum, fp->csum);
579 head->truesize += fp->truesize;
7eb95156 580 atomic_sub(fp->truesize, &ip6_frags.mem);
1da177e4
LT
581 }
582
583 head->next = NULL;
584 head->dev = dev;
5ab11c98 585 head->tstamp = fq->q.stamp;
0660e03f 586 ipv6_hdr(head)->payload_len = htons(payload_len);
951dbc8a 587 IP6CB(head)->nhoff = nhoff;
1da177e4 588
1da177e4 589 /* Yes, and fold redundant checksum back. 8) */
84fa7933 590 if (head->ip_summed == CHECKSUM_COMPLETE)
d56f90a7 591 head->csum = csum_partial(skb_network_header(head),
cfe1fc77 592 skb_network_header_len(head),
d56f90a7 593 head->csum);
1da177e4 594
a11d206d
YH
595 rcu_read_lock();
596 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
597 rcu_read_unlock();
5ab11c98 598 fq->q.fragments = NULL;
1da177e4
LT
599 return 1;
600
601out_oversize:
602 if (net_ratelimit())
603 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
604 goto out_fail;
605out_oom:
606 if (net_ratelimit())
607 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
608out_fail:
a11d206d
YH
609 rcu_read_lock();
610 IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
611 rcu_read_unlock();
1da177e4
LT
612 return -1;
613}
614
e5bbef20 615static int ipv6_frag_rcv(struct sk_buff *skb)
1da177e4 616{
1da177e4
LT
617 struct frag_hdr *fhdr;
618 struct frag_queue *fq;
0660e03f 619 struct ipv6hdr *hdr = ipv6_hdr(skb);
1da177e4 620
a11d206d 621 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
1da177e4
LT
622
623 /* Jumbo payload inhibits frag. header */
624 if (hdr->payload_len==0) {
a11d206d 625 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
cfe1fc77
ACM
626 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
627 skb_network_header_len(skb));
1da177e4
LT
628 return -1;
629 }
ea2ae17d
ACM
630 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
631 sizeof(struct frag_hdr)))) {
a11d206d 632 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
cfe1fc77
ACM
633 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
634 skb_network_header_len(skb));
1da177e4
LT
635 return -1;
636 }
637
0660e03f 638 hdr = ipv6_hdr(skb);
9c70220b 639 fhdr = (struct frag_hdr *)skb_transport_header(skb);
1da177e4
LT
640
641 if (!(fhdr->frag_off & htons(0xFFF9))) {
642 /* It is not a fragmented frame */
b0e380b1 643 skb->transport_header += sizeof(struct frag_hdr);
a11d206d 644 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
1da177e4 645
d56f90a7 646 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
1da177e4
LT
647 return 1;
648 }
649
04128f23 650 if (atomic_read(&ip6_frags.mem) > ip6_frags_ctl.high_thresh)
a11d206d 651 ip6_evictor(ip6_dst_idev(skb->dst));
1da177e4 652
a11d206d
YH
653 if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
654 ip6_dst_idev(skb->dst))) != NULL) {
f61944ef 655 int ret;
1da177e4 656
5ab11c98 657 spin_lock(&fq->q.lock);
1da177e4 658
f61944ef 659 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
1da177e4 660
5ab11c98 661 spin_unlock(&fq->q.lock);
4b6cb5d8 662 fq_put(fq);
1da177e4
LT
663 return ret;
664 }
665
a11d206d 666 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
1da177e4
LT
667 kfree_skb(skb);
668 return -1;
669}
670
671static struct inet6_protocol frag_protocol =
672{
673 .handler = ipv6_frag_rcv,
674 .flags = INET6_PROTO_NOPOLICY,
675};
676
677void __init ipv6_frag_init(void)
678{
679 if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
680 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
681
04128f23 682 ip6_frags.ctl = &ip6_frags_ctl;
321a3a99 683 ip6_frags.hashfn = ip6_hashfn;
1e4b8287
PE
684 ip6_frags.destructor = ip6_frag_free;
685 ip6_frags.skb_free = NULL;
686 ip6_frags.qsize = sizeof(struct frag_queue);
2588fe1d 687 ip6_frags.equal = ip6_frag_equal;
7eb95156 688 inet_frags_init(&ip6_frags);
1da177e4 689}