2 * IPv6 fragment reassembly
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
10 * Based on: net/ipv4/ip_fragment.c
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.
20 * Andi Kleen Make it work with multiple hosts.
21 * More RFC compliance.
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{}.
28 * YOSHIFUJI,H. @USAGI Always remove fragment header to
29 * calculate ICV correctly.
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>
45 #include <linux/skbuff.h>
51 #include <net/ip6_route.h>
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>
57 #include <net/inet_frag.h>
61 struct inet6_skb_parm h
;
65 #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
69 * Equivalent of ipv4 struct ipq
74 struct inet_frag_queue q
;
76 __be32 id
; /* fragment id */
77 struct in6_addr saddr
;
78 struct in6_addr daddr
;
85 static struct inet_frags ip6_frags
;
87 int ip6_frag_nqueues(void)
89 return ip6_frags
.nqueues
;
92 int ip6_frag_mem(void)
94 return atomic_read(&ip6_frags
.mem
);
97 static int ip6_frag_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
,
98 struct net_device
*dev
);
101 * callers should be careful not to use the hash value outside the ipfrag_lock
102 * as doing so could race with ipfrag_hash_rnd being recalculated.
104 static unsigned int ip6qhashfn(__be32 id
, struct in6_addr
*saddr
,
105 struct in6_addr
*daddr
)
109 a
= (__force u32
)saddr
->s6_addr32
[0];
110 b
= (__force u32
)saddr
->s6_addr32
[1];
111 c
= (__force u32
)saddr
->s6_addr32
[2];
113 a
+= JHASH_GOLDEN_RATIO
;
114 b
+= JHASH_GOLDEN_RATIO
;
116 __jhash_mix(a
, b
, c
);
118 a
+= (__force u32
)saddr
->s6_addr32
[3];
119 b
+= (__force u32
)daddr
->s6_addr32
[0];
120 c
+= (__force u32
)daddr
->s6_addr32
[1];
121 __jhash_mix(a
, b
, c
);
123 a
+= (__force u32
)daddr
->s6_addr32
[2];
124 b
+= (__force u32
)daddr
->s6_addr32
[3];
125 c
+= (__force u32
)id
;
126 __jhash_mix(a
, b
, c
);
128 return c
& (INETFRAGS_HASHSZ
- 1);
131 static unsigned int ip6_hashfn(struct inet_frag_queue
*q
)
133 struct frag_queue
*fq
;
135 fq
= container_of(q
, struct frag_queue
, q
);
136 return ip6qhashfn(fq
->id
, &fq
->saddr
, &fq
->daddr
);
139 int ip6_frag_match(struct inet_frag_queue
*q
, void *a
)
141 struct frag_queue
*fq
;
142 struct ip6_create_arg
*arg
= a
;
144 fq
= container_of(q
, struct frag_queue
, q
);
145 return (fq
->id
== arg
->id
&&
146 ipv6_addr_equal(&fq
->saddr
, arg
->src
) &&
147 ipv6_addr_equal(&fq
->daddr
, arg
->dst
));
149 EXPORT_SYMBOL(ip6_frag_match
);
151 /* Memory Tracking Functions. */
152 static inline void frag_kfree_skb(struct sk_buff
*skb
, int *work
)
155 *work
-= skb
->truesize
;
156 atomic_sub(skb
->truesize
, &ip6_frags
.mem
);
160 void ip6_frag_init(struct inet_frag_queue
*q
, void *a
)
162 struct frag_queue
*fq
= container_of(q
, struct frag_queue
, q
);
163 struct ip6_create_arg
*arg
= a
;
166 ipv6_addr_copy(&fq
->saddr
, arg
->src
);
167 ipv6_addr_copy(&fq
->daddr
, arg
->dst
);
169 EXPORT_SYMBOL(ip6_frag_init
);
171 /* Destruction primitives. */
173 static __inline__
void fq_put(struct frag_queue
*fq
)
175 inet_frag_put(&fq
->q
, &ip6_frags
);
178 /* Kill fq entry. It is not destroyed immediately,
179 * because caller (and someone more) holds reference count.
181 static __inline__
void fq_kill(struct frag_queue
*fq
)
183 inet_frag_kill(&fq
->q
, &ip6_frags
);
186 static void ip6_evictor(struct inet6_dev
*idev
)
190 evicted
= inet_frag_evictor(&ip6_frags
);
192 IP6_ADD_STATS_BH(idev
, IPSTATS_MIB_REASMFAILS
, evicted
);
195 static void ip6_frag_expire(unsigned long data
)
197 struct frag_queue
*fq
;
198 struct net_device
*dev
= NULL
;
200 fq
= container_of((struct inet_frag_queue
*)data
, struct frag_queue
, q
);
202 spin_lock(&fq
->q
.lock
);
204 if (fq
->q
.last_in
& COMPLETE
)
209 dev
= dev_get_by_index(&init_net
, fq
->iif
);
214 IP6_INC_STATS_BH(__in6_dev_get(dev
), IPSTATS_MIB_REASMTIMEOUT
);
215 IP6_INC_STATS_BH(__in6_dev_get(dev
), IPSTATS_MIB_REASMFAILS
);
218 /* Don't send error if the first segment did not arrive. */
219 if (!(fq
->q
.last_in
&FIRST_IN
) || !fq
->q
.fragments
)
223 But use as source device on which LAST ARRIVED
224 segment was received. And do not use fq->dev
225 pointer directly, device might already disappeared.
227 fq
->q
.fragments
->dev
= dev
;
228 icmpv6_send(fq
->q
.fragments
, ICMPV6_TIME_EXCEED
, ICMPV6_EXC_FRAGTIME
, 0, dev
);
232 spin_unlock(&fq
->q
.lock
);
236 static __inline__
struct frag_queue
*
237 fq_find(__be32 id
, struct in6_addr
*src
, struct in6_addr
*dst
,
238 struct inet6_dev
*idev
)
240 struct inet_frag_queue
*q
;
241 struct ip6_create_arg arg
;
247 hash
= ip6qhashfn(id
, src
, dst
);
249 q
= inet_frag_find(&ip6_frags
, &arg
, hash
);
253 return container_of(q
, struct frag_queue
, q
);
256 IP6_INC_STATS_BH(idev
, IPSTATS_MIB_REASMFAILS
);
260 static int ip6_frag_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
261 struct frag_hdr
*fhdr
, int nhoff
)
263 struct sk_buff
*prev
, *next
;
264 struct net_device
*dev
;
267 if (fq
->q
.last_in
& COMPLETE
)
270 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
271 end
= offset
+ (ntohs(ipv6_hdr(skb
)->payload_len
) -
272 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
274 if ((unsigned int)end
> IPV6_MAXPLEN
) {
275 IP6_INC_STATS_BH(ip6_dst_idev(skb
->dst
),
276 IPSTATS_MIB_INHDRERRORS
);
277 icmpv6_param_prob(skb
, ICMPV6_HDR_FIELD
,
278 ((u8
*)&fhdr
->frag_off
-
279 skb_network_header(skb
)));
283 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
284 const unsigned char *nh
= skb_network_header(skb
);
285 skb
->csum
= csum_sub(skb
->csum
,
286 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
290 /* Is this the final fragment? */
291 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
292 /* If we already have some bits beyond end
293 * or have different end, the segment is corrupted.
295 if (end
< fq
->q
.len
||
296 ((fq
->q
.last_in
& LAST_IN
) && end
!= fq
->q
.len
))
298 fq
->q
.last_in
|= LAST_IN
;
301 /* Check if the fragment is rounded to 8 bytes.
302 * Required by the RFC.
305 /* RFC2460 says always send parameter problem in
308 IP6_INC_STATS_BH(ip6_dst_idev(skb
->dst
),
309 IPSTATS_MIB_INHDRERRORS
);
310 icmpv6_param_prob(skb
, ICMPV6_HDR_FIELD
,
311 offsetof(struct ipv6hdr
, payload_len
));
314 if (end
> fq
->q
.len
) {
315 /* Some bits beyond end -> corruption. */
316 if (fq
->q
.last_in
& LAST_IN
)
325 /* Point into the IP datagram 'data' part. */
326 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
))
329 if (pskb_trim_rcsum(skb
, end
- offset
))
332 /* Find out which fragments are in front and at the back of us
333 * in the chain of fragments so far. We must know where to put
334 * this fragment, right?
337 for(next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
338 if (FRAG6_CB(next
)->offset
>= offset
)
343 /* We found where to put this one. Check for overlap with
344 * preceding fragment, and, if needed, align things so that
345 * any overlaps are eliminated.
348 int i
= (FRAG6_CB(prev
)->offset
+ prev
->len
) - offset
;
354 if (!pskb_pull(skb
, i
))
356 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
357 skb
->ip_summed
= CHECKSUM_NONE
;
361 /* Look for overlap with succeeding segments.
362 * If we can merge fragments, do it.
364 while (next
&& FRAG6_CB(next
)->offset
< end
) {
365 int i
= end
- FRAG6_CB(next
)->offset
; /* overlap is 'i' bytes */
368 /* Eat head of the next overlapped fragment
369 * and leave the loop. The next ones cannot overlap.
371 if (!pskb_pull(next
, i
))
373 FRAG6_CB(next
)->offset
+= i
; /* next fragment */
375 if (next
->ip_summed
!= CHECKSUM_UNNECESSARY
)
376 next
->ip_summed
= CHECKSUM_NONE
;
379 struct sk_buff
*free_it
= next
;
381 /* Old fragment is completely overridden with
389 fq
->q
.fragments
= next
;
391 fq
->q
.meat
-= free_it
->len
;
392 frag_kfree_skb(free_it
, NULL
);
396 FRAG6_CB(skb
)->offset
= offset
;
398 /* Insert this fragment in the chain of fragments. */
403 fq
->q
.fragments
= skb
;
407 fq
->iif
= dev
->ifindex
;
410 fq
->q
.stamp
= skb
->tstamp
;
411 fq
->q
.meat
+= skb
->len
;
412 atomic_add(skb
->truesize
, &ip6_frags
.mem
);
414 /* The first fragment.
415 * nhoffset is obtained from the first fragment, of course.
418 fq
->nhoffset
= nhoff
;
419 fq
->q
.last_in
|= FIRST_IN
;
422 if (fq
->q
.last_in
== (FIRST_IN
| LAST_IN
) && fq
->q
.meat
== fq
->q
.len
)
423 return ip6_frag_reasm(fq
, prev
, dev
);
425 write_lock(&ip6_frags
.lock
);
426 list_move_tail(&fq
->q
.lru_list
, &ip6_frags
.lru_list
);
427 write_unlock(&ip6_frags
.lock
);
431 IP6_INC_STATS(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_REASMFAILS
);
437 * Check if this packet is complete.
438 * Returns NULL on failure by any reason, and pointer
439 * to current nexthdr field in reassembled frame.
441 * It is called with locked fq, and caller must check that
442 * queue is eligible for reassembly i.e. it is not COMPLETE,
443 * the last and the first frames arrived and all the bits are here.
445 static int ip6_frag_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
,
446 struct net_device
*dev
)
448 struct sk_buff
*fp
, *head
= fq
->q
.fragments
;
454 /* Make the one we just received the head. */
457 fp
= skb_clone(head
, GFP_ATOMIC
);
462 fp
->next
= head
->next
;
465 skb_morph(head
, fq
->q
.fragments
);
466 head
->next
= fq
->q
.fragments
->next
;
468 kfree_skb(fq
->q
.fragments
);
469 fq
->q
.fragments
= head
;
472 BUG_TRAP(head
!= NULL
);
473 BUG_TRAP(FRAG6_CB(head
)->offset
== 0);
475 /* Unfragmented part is taken from the first segment. */
476 payload_len
= ((head
->data
- skb_network_header(head
)) -
477 sizeof(struct ipv6hdr
) + fq
->q
.len
-
478 sizeof(struct frag_hdr
));
479 if (payload_len
> IPV6_MAXPLEN
)
482 /* Head of list must not be cloned. */
483 if (skb_cloned(head
) && pskb_expand_head(head
, 0, 0, GFP_ATOMIC
))
486 /* If the first fragment is fragmented itself, we split
487 * it to two chunks: the first with data and paged part
488 * and the second, holding only fragments. */
489 if (skb_shinfo(head
)->frag_list
) {
490 struct sk_buff
*clone
;
493 if ((clone
= alloc_skb(0, GFP_ATOMIC
)) == NULL
)
495 clone
->next
= head
->next
;
497 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
498 skb_shinfo(head
)->frag_list
= NULL
;
499 for (i
=0; i
<skb_shinfo(head
)->nr_frags
; i
++)
500 plen
+= skb_shinfo(head
)->frags
[i
].size
;
501 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
502 head
->data_len
-= clone
->len
;
503 head
->len
-= clone
->len
;
505 clone
->ip_summed
= head
->ip_summed
;
506 atomic_add(clone
->truesize
, &ip6_frags
.mem
);
509 /* We have to remove fragment header from datagram and to relocate
510 * header in order to calculate ICV correctly. */
511 nhoff
= fq
->nhoffset
;
512 skb_network_header(head
)[nhoff
] = skb_transport_header(head
)[0];
513 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
514 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
515 head
->mac_header
+= sizeof(struct frag_hdr
);
516 head
->network_header
+= sizeof(struct frag_hdr
);
518 skb_shinfo(head
)->frag_list
= head
->next
;
519 skb_reset_transport_header(head
);
520 skb_push(head
, head
->data
- skb_network_header(head
));
521 atomic_sub(head
->truesize
, &ip6_frags
.mem
);
523 for (fp
=head
->next
; fp
; fp
= fp
->next
) {
524 head
->data_len
+= fp
->len
;
525 head
->len
+= fp
->len
;
526 if (head
->ip_summed
!= fp
->ip_summed
)
527 head
->ip_summed
= CHECKSUM_NONE
;
528 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
529 head
->csum
= csum_add(head
->csum
, fp
->csum
);
530 head
->truesize
+= fp
->truesize
;
531 atomic_sub(fp
->truesize
, &ip6_frags
.mem
);
536 head
->tstamp
= fq
->q
.stamp
;
537 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
538 IP6CB(head
)->nhoff
= nhoff
;
540 /* Yes, and fold redundant checksum back. 8) */
541 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
542 head
->csum
= csum_partial(skb_network_header(head
),
543 skb_network_header_len(head
),
547 IP6_INC_STATS_BH(__in6_dev_get(dev
), IPSTATS_MIB_REASMOKS
);
549 fq
->q
.fragments
= NULL
;
554 printk(KERN_DEBUG
"ip6_frag_reasm: payload len = %d\n", payload_len
);
558 printk(KERN_DEBUG
"ip6_frag_reasm: no memory for reassembly\n");
561 IP6_INC_STATS_BH(__in6_dev_get(dev
), IPSTATS_MIB_REASMFAILS
);
566 static int ipv6_frag_rcv(struct sk_buff
*skb
)
568 struct frag_hdr
*fhdr
;
569 struct frag_queue
*fq
;
570 struct ipv6hdr
*hdr
= ipv6_hdr(skb
);
572 IP6_INC_STATS_BH(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_REASMREQDS
);
574 /* Jumbo payload inhibits frag. header */
575 if (hdr
->payload_len
==0) {
576 IP6_INC_STATS(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_INHDRERRORS
);
577 icmpv6_param_prob(skb
, ICMPV6_HDR_FIELD
,
578 skb_network_header_len(skb
));
581 if (!pskb_may_pull(skb
, (skb_transport_offset(skb
) +
582 sizeof(struct frag_hdr
)))) {
583 IP6_INC_STATS(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_INHDRERRORS
);
584 icmpv6_param_prob(skb
, ICMPV6_HDR_FIELD
,
585 skb_network_header_len(skb
));
590 fhdr
= (struct frag_hdr
*)skb_transport_header(skb
);
592 if (!(fhdr
->frag_off
& htons(0xFFF9))) {
593 /* It is not a fragmented frame */
594 skb
->transport_header
+= sizeof(struct frag_hdr
);
595 IP6_INC_STATS_BH(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_REASMOKS
);
597 IP6CB(skb
)->nhoff
= (u8
*)fhdr
- skb_network_header(skb
);
601 if (atomic_read(&ip6_frags
.mem
) > init_net
.ipv6
.sysctl
.frags
.high_thresh
)
602 ip6_evictor(ip6_dst_idev(skb
->dst
));
604 if ((fq
= fq_find(fhdr
->identification
, &hdr
->saddr
, &hdr
->daddr
,
605 ip6_dst_idev(skb
->dst
))) != NULL
) {
608 spin_lock(&fq
->q
.lock
);
610 ret
= ip6_frag_queue(fq
, skb
, fhdr
, IP6CB(skb
)->nhoff
);
612 spin_unlock(&fq
->q
.lock
);
617 IP6_INC_STATS_BH(ip6_dst_idev(skb
->dst
), IPSTATS_MIB_REASMFAILS
);
622 static struct inet6_protocol frag_protocol
=
624 .handler
= ipv6_frag_rcv
,
625 .flags
= INET6_PROTO_NOPOLICY
,
628 void ipv6_frag_sysctl_init(struct net
*net
)
630 ip6_frags
.ctl
= &net
->ipv6
.sysctl
.frags
;
633 int __init
ipv6_frag_init(void)
637 ret
= inet6_add_protocol(&frag_protocol
, IPPROTO_FRAGMENT
);
641 ip6_frags
.hashfn
= ip6_hashfn
;
642 ip6_frags
.constructor
= ip6_frag_init
;
643 ip6_frags
.destructor
= NULL
;
644 ip6_frags
.skb_free
= NULL
;
645 ip6_frags
.qsize
= sizeof(struct frag_queue
);
646 ip6_frags
.match
= ip6_frag_match
;
647 ip6_frags
.frag_expire
= ip6_frag_expire
;
648 inet_frags_init(&ip6_frags
);
653 void ipv6_frag_exit(void)
655 inet_frags_fini(&ip6_frags
);
656 inet6_del_protocol(&frag_protocol
, IPPROTO_FRAGMENT
);