2 * IPv6 fragment reassembly for connection tracking
4 * Copyright (C)2004 USAGI/WIDE Project
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * Based on: net/ipv6/reassembly.c
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.
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>
34 #include <net/inet_frag.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 <net/netfilter/ipv6/nf_conntrack_ipv6.h>
43 #include <linux/sysctl.h>
44 #include <linux/netfilter.h>
45 #include <linux/netfilter_ipv6.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
49 #define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */
50 #define NF_CT_FRAG6_LOW_THRESH 196608 /* == 192*1024 */
51 #define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUT
53 struct nf_ct_frag6_skb_cb
55 struct inet6_skb_parm h
;
60 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
62 struct nf_ct_frag6_queue
64 struct inet_frag_queue q
;
66 __be32 id
; /* fragment id */
67 struct in6_addr saddr
;
68 struct in6_addr daddr
;
74 static struct inet_frags nf_frags
;
75 static struct netns_frags nf_init_frags
;
78 struct ctl_table nf_ct_ipv6_sysctl_table
[] = {
80 .procname
= "nf_conntrack_frag6_timeout",
81 .data
= &nf_init_frags
.timeout
,
82 .maxlen
= sizeof(unsigned int),
84 .proc_handler
= &proc_dointvec_jiffies
,
87 .ctl_name
= NET_NF_CONNTRACK_FRAG6_LOW_THRESH
,
88 .procname
= "nf_conntrack_frag6_low_thresh",
89 .data
= &nf_init_frags
.low_thresh
,
90 .maxlen
= sizeof(unsigned int),
92 .proc_handler
= &proc_dointvec
,
95 .ctl_name
= NET_NF_CONNTRACK_FRAG6_HIGH_THRESH
,
96 .procname
= "nf_conntrack_frag6_high_thresh",
97 .data
= &nf_init_frags
.high_thresh
,
98 .maxlen
= sizeof(unsigned int),
100 .proc_handler
= &proc_dointvec
,
106 static unsigned int ip6qhashfn(__be32 id
, struct in6_addr
*saddr
,
107 struct in6_addr
*daddr
)
111 a
= (__force u32
)saddr
->s6_addr32
[0];
112 b
= (__force u32
)saddr
->s6_addr32
[1];
113 c
= (__force u32
)saddr
->s6_addr32
[2];
115 a
+= JHASH_GOLDEN_RATIO
;
116 b
+= JHASH_GOLDEN_RATIO
;
118 __jhash_mix(a
, b
, c
);
120 a
+= (__force u32
)saddr
->s6_addr32
[3];
121 b
+= (__force u32
)daddr
->s6_addr32
[0];
122 c
+= (__force u32
)daddr
->s6_addr32
[1];
123 __jhash_mix(a
, b
, c
);
125 a
+= (__force u32
)daddr
->s6_addr32
[2];
126 b
+= (__force u32
)daddr
->s6_addr32
[3];
127 c
+= (__force u32
)id
;
128 __jhash_mix(a
, b
, c
);
130 return c
& (INETFRAGS_HASHSZ
- 1);
133 static unsigned int nf_hashfn(struct inet_frag_queue
*q
)
135 struct nf_ct_frag6_queue
*nq
;
137 nq
= container_of(q
, struct nf_ct_frag6_queue
, q
);
138 return ip6qhashfn(nq
->id
, &nq
->saddr
, &nq
->daddr
);
141 static void nf_skb_free(struct sk_buff
*skb
)
143 if (NFCT_FRAG6_CB(skb
)->orig
)
144 kfree_skb(NFCT_FRAG6_CB(skb
)->orig
);
147 /* Memory Tracking Functions. */
148 static inline void frag_kfree_skb(struct sk_buff
*skb
, unsigned int *work
)
151 *work
-= skb
->truesize
;
152 atomic_sub(skb
->truesize
, &nf_init_frags
.mem
);
157 /* Destruction primitives. */
159 static __inline__
void fq_put(struct nf_ct_frag6_queue
*fq
)
161 inet_frag_put(&fq
->q
, &nf_frags
);
164 /* Kill fq entry. It is not destroyed immediately,
165 * because caller (and someone more) holds reference count.
167 static __inline__
void fq_kill(struct nf_ct_frag6_queue
*fq
)
169 inet_frag_kill(&fq
->q
, &nf_frags
);
172 static void nf_ct_frag6_evictor(void)
175 inet_frag_evictor(&nf_init_frags
, &nf_frags
);
179 static void nf_ct_frag6_expire(unsigned long data
)
181 struct nf_ct_frag6_queue
*fq
;
183 fq
= container_of((struct inet_frag_queue
*)data
,
184 struct nf_ct_frag6_queue
, q
);
186 spin_lock(&fq
->q
.lock
);
188 if (fq
->q
.last_in
& INET_FRAG_COMPLETE
)
194 spin_unlock(&fq
->q
.lock
);
198 /* Creation primitives. */
200 static __inline__
struct nf_ct_frag6_queue
*
201 fq_find(__be32 id
, struct in6_addr
*src
, struct in6_addr
*dst
)
203 struct inet_frag_queue
*q
;
204 struct ip6_create_arg arg
;
210 hash
= ip6qhashfn(id
, src
, dst
);
212 q
= inet_frag_find(&nf_init_frags
, &nf_frags
, &arg
, hash
);
216 return container_of(q
, struct nf_ct_frag6_queue
, q
);
219 pr_debug("Can't alloc new queue\n");
224 static int nf_ct_frag6_queue(struct nf_ct_frag6_queue
*fq
, struct sk_buff
*skb
,
225 struct frag_hdr
*fhdr
, int nhoff
)
227 struct sk_buff
*prev
, *next
;
230 if (fq
->q
.last_in
& INET_FRAG_COMPLETE
) {
231 pr_debug("Allready completed\n");
235 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
236 end
= offset
+ (ntohs(ipv6_hdr(skb
)->payload_len
) -
237 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
239 if ((unsigned int)end
> IPV6_MAXPLEN
) {
240 pr_debug("offset is too large.\n");
244 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
245 const unsigned char *nh
= skb_network_header(skb
);
246 skb
->csum
= csum_sub(skb
->csum
,
247 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
251 /* Is this the final fragment? */
252 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
253 /* If we already have some bits beyond end
254 * or have different end, the segment is corrupted.
256 if (end
< fq
->q
.len
||
257 ((fq
->q
.last_in
& INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
258 pr_debug("already received last fragment\n");
261 fq
->q
.last_in
|= INET_FRAG_LAST_IN
;
264 /* Check if the fragment is rounded to 8 bytes.
265 * Required by the RFC.
268 /* RFC2460 says always send parameter problem in
271 pr_debug("end of fragment not rounded to 8 bytes.\n");
274 if (end
> fq
->q
.len
) {
275 /* Some bits beyond end -> corruption. */
276 if (fq
->q
.last_in
& INET_FRAG_LAST_IN
) {
277 pr_debug("last packet already reached.\n");
287 /* Point into the IP datagram 'data' part. */
288 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
)) {
289 pr_debug("queue: message is too short.\n");
292 if (pskb_trim_rcsum(skb
, end
- offset
)) {
293 pr_debug("Can't trim\n");
297 /* Find out which fragments are in front and at the back of us
298 * in the chain of fragments so far. We must know where to put
299 * this fragment, right?
302 for (next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
303 if (NFCT_FRAG6_CB(next
)->offset
>= offset
)
308 /* We found where to put this one. Check for overlap with
309 * preceding fragment, and, if needed, align things so that
310 * any overlaps are eliminated.
313 int i
= (NFCT_FRAG6_CB(prev
)->offset
+ prev
->len
) - offset
;
318 pr_debug("overlap\n");
321 if (!pskb_pull(skb
, i
)) {
322 pr_debug("Can't pull\n");
325 if (skb
->ip_summed
!= CHECKSUM_UNNECESSARY
)
326 skb
->ip_summed
= CHECKSUM_NONE
;
330 /* Look for overlap with succeeding segments.
331 * If we can merge fragments, do it.
333 while (next
&& NFCT_FRAG6_CB(next
)->offset
< end
) {
334 /* overlap is 'i' bytes */
335 int i
= end
- NFCT_FRAG6_CB(next
)->offset
;
338 /* Eat head of the next overlapped fragment
339 * and leave the loop. The next ones cannot overlap.
341 pr_debug("Eat head of the overlapped parts.: %d", i
);
342 if (!pskb_pull(next
, i
))
346 NFCT_FRAG6_CB(next
)->offset
+= i
;
348 if (next
->ip_summed
!= CHECKSUM_UNNECESSARY
)
349 next
->ip_summed
= CHECKSUM_NONE
;
352 struct sk_buff
*free_it
= next
;
354 /* Old fragmnet is completely overridden with
362 fq
->q
.fragments
= next
;
364 fq
->q
.meat
-= free_it
->len
;
365 frag_kfree_skb(free_it
, NULL
);
369 NFCT_FRAG6_CB(skb
)->offset
= offset
;
371 /* Insert this fragment in the chain of fragments. */
376 fq
->q
.fragments
= skb
;
379 fq
->q
.stamp
= skb
->tstamp
;
380 fq
->q
.meat
+= skb
->len
;
381 atomic_add(skb
->truesize
, &nf_init_frags
.mem
);
383 /* The first fragment.
384 * nhoffset is obtained from the first fragment, of course.
387 fq
->nhoffset
= nhoff
;
388 fq
->q
.last_in
|= INET_FRAG_FIRST_IN
;
390 write_lock(&nf_frags
.lock
);
391 list_move_tail(&fq
->q
.lru_list
, &nf_init_frags
.lru_list
);
392 write_unlock(&nf_frags
.lock
);
400 * Check if this packet is complete.
401 * Returns NULL on failure by any reason, and pointer
402 * to current nexthdr field in reassembled frame.
404 * It is called with locked fq, and caller must check that
405 * queue is eligible for reassembly i.e. it is not COMPLETE,
406 * the last and the first frames arrived and all the bits are here.
408 static struct sk_buff
*
409 nf_ct_frag6_reasm(struct nf_ct_frag6_queue
*fq
, struct net_device
*dev
)
411 struct sk_buff
*fp
, *op
, *head
= fq
->q
.fragments
;
416 BUG_TRAP(head
!= NULL
);
417 BUG_TRAP(NFCT_FRAG6_CB(head
)->offset
== 0);
419 /* Unfragmented part is taken from the first segment. */
420 payload_len
= ((head
->data
- skb_network_header(head
)) -
421 sizeof(struct ipv6hdr
) + fq
->q
.len
-
422 sizeof(struct frag_hdr
));
423 if (payload_len
> IPV6_MAXPLEN
) {
424 pr_debug("payload len is too large.\n");
428 /* Head of list must not be cloned. */
429 if (skb_cloned(head
) && pskb_expand_head(head
, 0, 0, GFP_ATOMIC
)) {
430 pr_debug("skb is cloned but can't expand head");
434 /* If the first fragment is fragmented itself, we split
435 * it to two chunks: the first with data and paged part
436 * and the second, holding only fragments. */
437 if (skb_shinfo(head
)->frag_list
) {
438 struct sk_buff
*clone
;
441 if ((clone
= alloc_skb(0, GFP_ATOMIC
)) == NULL
) {
442 pr_debug("Can't alloc skb\n");
445 clone
->next
= head
->next
;
447 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
448 skb_shinfo(head
)->frag_list
= NULL
;
449 for (i
=0; i
<skb_shinfo(head
)->nr_frags
; i
++)
450 plen
+= skb_shinfo(head
)->frags
[i
].size
;
451 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
452 head
->data_len
-= clone
->len
;
453 head
->len
-= clone
->len
;
455 clone
->ip_summed
= head
->ip_summed
;
457 NFCT_FRAG6_CB(clone
)->orig
= NULL
;
458 atomic_add(clone
->truesize
, &nf_init_frags
.mem
);
461 /* We have to remove fragment header from datagram and to relocate
462 * header in order to calculate ICV correctly. */
463 skb_network_header(head
)[fq
->nhoffset
] = skb_transport_header(head
)[0];
464 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
465 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
466 head
->mac_header
+= sizeof(struct frag_hdr
);
467 head
->network_header
+= sizeof(struct frag_hdr
);
469 skb_shinfo(head
)->frag_list
= head
->next
;
470 skb_reset_transport_header(head
);
471 skb_push(head
, head
->data
- skb_network_header(head
));
472 atomic_sub(head
->truesize
, &nf_init_frags
.mem
);
474 for (fp
=head
->next
; fp
; fp
= fp
->next
) {
475 head
->data_len
+= fp
->len
;
476 head
->len
+= fp
->len
;
477 if (head
->ip_summed
!= fp
->ip_summed
)
478 head
->ip_summed
= CHECKSUM_NONE
;
479 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
480 head
->csum
= csum_add(head
->csum
, fp
->csum
);
481 head
->truesize
+= fp
->truesize
;
482 atomic_sub(fp
->truesize
, &nf_init_frags
.mem
);
487 head
->tstamp
= fq
->q
.stamp
;
488 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
490 /* Yes, and fold redundant checksum back. 8) */
491 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
492 head
->csum
= csum_partial(skb_network_header(head
),
493 skb_network_header_len(head
),
496 fq
->q
.fragments
= NULL
;
498 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
499 fp
= skb_shinfo(head
)->frag_list
;
500 if (NFCT_FRAG6_CB(fp
)->orig
== NULL
)
501 /* at above code, head skb is divided into two skbs. */
504 op
= NFCT_FRAG6_CB(head
)->orig
;
505 for (; fp
; fp
= fp
->next
) {
506 struct sk_buff
*orig
= NFCT_FRAG6_CB(fp
)->orig
;
510 NFCT_FRAG6_CB(fp
)->orig
= NULL
;
517 printk(KERN_DEBUG
"nf_ct_frag6_reasm: payload len = %d\n", payload_len
);
521 printk(KERN_DEBUG
"nf_ct_frag6_reasm: no memory for reassembly\n");
527 * find the header just before Fragment Header.
529 * if success return 0 and set ...
530 * (*prevhdrp): the value of "Next Header Field" in the header
531 * just before Fragment Header.
532 * (*prevhoff): the offset of "Next Header Field" in the header
533 * just before Fragment Header.
534 * (*fhoff) : the offset of Fragment Header.
536 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
540 find_prev_fhdr(struct sk_buff
*skb
, u8
*prevhdrp
, int *prevhoff
, int *fhoff
)
542 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
543 const int netoff
= skb_network_offset(skb
);
544 u8 prev_nhoff
= netoff
+ offsetof(struct ipv6hdr
, nexthdr
);
545 int start
= netoff
+ sizeof(struct ipv6hdr
);
546 int len
= skb
->len
- start
;
547 u8 prevhdr
= NEXTHDR_IPV6
;
549 while (nexthdr
!= NEXTHDR_FRAGMENT
) {
550 struct ipv6_opt_hdr hdr
;
553 if (!ipv6_ext_hdr(nexthdr
)) {
556 if (len
< (int)sizeof(struct ipv6_opt_hdr
)) {
557 pr_debug("too short\n");
560 if (nexthdr
== NEXTHDR_NONE
) {
561 pr_debug("next header is none\n");
564 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
566 if (nexthdr
== NEXTHDR_AUTH
)
567 hdrlen
= (hdr
.hdrlen
+2)<<2;
569 hdrlen
= ipv6_optlen(&hdr
);
574 nexthdr
= hdr
.nexthdr
;
583 *prevhoff
= prev_nhoff
;
589 struct sk_buff
*nf_ct_frag6_gather(struct sk_buff
*skb
)
591 struct sk_buff
*clone
;
592 struct net_device
*dev
= skb
->dev
;
593 struct frag_hdr
*fhdr
;
594 struct nf_ct_frag6_queue
*fq
;
598 struct sk_buff
*ret_skb
= NULL
;
600 /* Jumbo payload inhibits frag. header */
601 if (ipv6_hdr(skb
)->payload_len
== 0) {
602 pr_debug("payload len = 0\n");
606 if (find_prev_fhdr(skb
, &prevhdr
, &nhoff
, &fhoff
) < 0)
609 clone
= skb_clone(skb
, GFP_ATOMIC
);
611 pr_debug("Can't clone skb\n");
615 NFCT_FRAG6_CB(clone
)->orig
= skb
;
617 if (!pskb_may_pull(clone
, fhoff
+ sizeof(*fhdr
))) {
618 pr_debug("message is too short.\n");
622 skb_set_transport_header(clone
, fhoff
);
623 hdr
= ipv6_hdr(clone
);
624 fhdr
= (struct frag_hdr
*)skb_transport_header(clone
);
626 if (!(fhdr
->frag_off
& htons(0xFFF9))) {
627 pr_debug("Invalid fragment offset\n");
628 /* It is not a fragmented frame */
632 if (atomic_read(&nf_init_frags
.mem
) > nf_init_frags
.high_thresh
)
633 nf_ct_frag6_evictor();
635 fq
= fq_find(fhdr
->identification
, &hdr
->saddr
, &hdr
->daddr
);
637 pr_debug("Can't find and can't create new queue\n");
641 spin_lock(&fq
->q
.lock
);
643 if (nf_ct_frag6_queue(fq
, clone
, fhdr
, nhoff
) < 0) {
644 spin_unlock(&fq
->q
.lock
);
645 pr_debug("Can't insert skb to queue\n");
650 if (fq
->q
.last_in
== (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
651 fq
->q
.meat
== fq
->q
.len
) {
652 ret_skb
= nf_ct_frag6_reasm(fq
, dev
);
654 pr_debug("Can't reassemble fragmented packets\n");
656 spin_unlock(&fq
->q
.lock
);
666 void nf_ct_frag6_output(unsigned int hooknum
, struct sk_buff
*skb
,
667 struct net_device
*in
, struct net_device
*out
,
668 int (*okfn
)(struct sk_buff
*))
670 struct sk_buff
*s
, *s2
;
672 for (s
= NFCT_FRAG6_CB(skb
)->orig
; s
;) {
673 nf_conntrack_put_reasm(s
->nfct_reasm
);
674 nf_conntrack_get_reasm(skb
);
680 NF_HOOK_THRESH(PF_INET6
, hooknum
, s
, in
, out
, okfn
,
681 NF_IP6_PRI_CONNTRACK_DEFRAG
+ 1);
684 nf_conntrack_put_reasm(skb
);
687 int nf_ct_frag6_init(void)
689 nf_frags
.hashfn
= nf_hashfn
;
690 nf_frags
.constructor
= ip6_frag_init
;
691 nf_frags
.destructor
= NULL
;
692 nf_frags
.skb_free
= nf_skb_free
;
693 nf_frags
.qsize
= sizeof(struct nf_ct_frag6_queue
);
694 nf_frags
.match
= ip6_frag_match
;
695 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
696 nf_frags
.secret_interval
= 10 * 60 * HZ
;
697 nf_init_frags
.timeout
= IPV6_FRAG_TIMEOUT
;
698 nf_init_frags
.high_thresh
= 256 * 1024;
699 nf_init_frags
.low_thresh
= 192 * 1024;
700 inet_frags_init_net(&nf_init_frags
);
701 inet_frags_init(&nf_frags
);
706 void nf_ct_frag6_cleanup(void)
708 inet_frags_fini(&nf_frags
);
710 nf_init_frags
.low_thresh
= 0;
711 nf_ct_frag6_evictor();