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/slab.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>
48 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
51 struct nf_ct_frag6_skb_cb
53 struct inet6_skb_parm h
;
58 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
60 static struct inet_frags nf_frags
;
63 static struct ctl_table nf_ct_frag6_sysctl_table
[] = {
65 .procname
= "nf_conntrack_frag6_timeout",
66 .data
= &init_net
.nf_frag
.frags
.timeout
,
67 .maxlen
= sizeof(unsigned int),
69 .proc_handler
= proc_dointvec_jiffies
,
72 .procname
= "nf_conntrack_frag6_low_thresh",
73 .data
= &init_net
.nf_frag
.frags
.low_thresh
,
74 .maxlen
= sizeof(unsigned int),
76 .proc_handler
= proc_dointvec
,
79 .procname
= "nf_conntrack_frag6_high_thresh",
80 .data
= &init_net
.nf_frag
.frags
.high_thresh
,
81 .maxlen
= sizeof(unsigned int),
83 .proc_handler
= proc_dointvec
,
88 static int nf_ct_frag6_sysctl_register(struct net
*net
)
90 struct ctl_table
*table
;
91 struct ctl_table_header
*hdr
;
93 table
= nf_ct_frag6_sysctl_table
;
94 if (!net_eq(net
, &init_net
)) {
95 table
= kmemdup(table
, sizeof(nf_ct_frag6_sysctl_table
),
100 table
[0].data
= &net
->nf_frag
.frags
.timeout
;
101 table
[1].data
= &net
->nf_frag
.frags
.low_thresh
;
102 table
[2].data
= &net
->nf_frag
.frags
.high_thresh
;
105 hdr
= register_net_sysctl(net
, "net/netfilter", table
);
109 net
->nf_frag
.sysctl
.frags_hdr
= hdr
;
113 if (!net_eq(net
, &init_net
))
119 static void __net_exit
nf_ct_frags6_sysctl_unregister(struct net
*net
)
121 struct ctl_table
*table
;
123 table
= net
->nf_frag
.sysctl
.frags_hdr
->ctl_table_arg
;
124 unregister_net_sysctl_table(net
->nf_frag
.sysctl
.frags_hdr
);
125 if (!net_eq(net
, &init_net
))
130 static int nf_ct_frag6_sysctl_register(struct net
*net
)
134 static void __net_exit
nf_ct_frags6_sysctl_unregister(struct net
*net
)
139 static unsigned int nf_hashfn(struct inet_frag_queue
*q
)
141 const struct frag_queue
*nq
;
143 nq
= container_of(q
, struct frag_queue
, q
);
144 return inet6_hash_frag(nq
->id
, &nq
->saddr
, &nq
->daddr
, nf_frags
.rnd
);
147 static void nf_skb_free(struct sk_buff
*skb
)
149 if (NFCT_FRAG6_CB(skb
)->orig
)
150 kfree_skb(NFCT_FRAG6_CB(skb
)->orig
);
153 static void nf_ct_frag6_expire(unsigned long data
)
155 struct frag_queue
*fq
;
158 fq
= container_of((struct inet_frag_queue
*)data
, struct frag_queue
, q
);
159 net
= container_of(fq
->q
.net
, struct net
, nf_frag
.frags
);
161 ip6_expire_frag_queue(net
, fq
, &nf_frags
);
164 /* Creation primitives. */
165 static inline struct frag_queue
*fq_find(struct net
*net
, __be32 id
,
166 u32 user
, struct in6_addr
*src
,
167 struct in6_addr
*dst
)
169 struct inet_frag_queue
*q
;
170 struct ip6_create_arg arg
;
178 read_lock_bh(&nf_frags
.lock
);
179 hash
= inet6_hash_frag(id
, src
, dst
, nf_frags
.rnd
);
181 q
= inet_frag_find(&net
->nf_frag
.frags
, &nf_frags
, &arg
, hash
);
186 return container_of(q
, struct frag_queue
, q
);
193 static int nf_ct_frag6_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
194 const struct frag_hdr
*fhdr
, int nhoff
)
196 struct sk_buff
*prev
, *next
;
197 unsigned int payload_len
;
200 if (fq
->q
.last_in
& INET_FRAG_COMPLETE
) {
201 pr_debug("Already completed\n");
205 payload_len
= ntohs(ipv6_hdr(skb
)->payload_len
);
207 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
208 end
= offset
+ (payload_len
-
209 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
211 if ((unsigned int)end
> IPV6_MAXPLEN
) {
212 pr_debug("offset is too large.\n");
216 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
217 const unsigned char *nh
= skb_network_header(skb
);
218 skb
->csum
= csum_sub(skb
->csum
,
219 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
223 /* Is this the final fragment? */
224 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
225 /* If we already have some bits beyond end
226 * or have different end, the segment is corrupted.
228 if (end
< fq
->q
.len
||
229 ((fq
->q
.last_in
& INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
230 pr_debug("already received last fragment\n");
233 fq
->q
.last_in
|= INET_FRAG_LAST_IN
;
236 /* Check if the fragment is rounded to 8 bytes.
237 * Required by the RFC.
240 /* RFC2460 says always send parameter problem in
243 pr_debug("end of fragment not rounded to 8 bytes.\n");
246 if (end
> fq
->q
.len
) {
247 /* Some bits beyond end -> corruption. */
248 if (fq
->q
.last_in
& INET_FRAG_LAST_IN
) {
249 pr_debug("last packet already reached.\n");
259 /* Point into the IP datagram 'data' part. */
260 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
)) {
261 pr_debug("queue: message is too short.\n");
264 if (pskb_trim_rcsum(skb
, end
- offset
)) {
265 pr_debug("Can't trim\n");
269 /* Find out which fragments are in front and at the back of us
270 * in the chain of fragments so far. We must know where to put
271 * this fragment, right?
273 prev
= fq
->q
.fragments_tail
;
274 if (!prev
|| NFCT_FRAG6_CB(prev
)->offset
< offset
) {
279 for (next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
280 if (NFCT_FRAG6_CB(next
)->offset
>= offset
)
286 /* RFC5722, Section 4:
287 * When reassembling an IPv6 datagram, if
288 * one or more its constituent fragments is determined to be an
289 * overlapping fragment, the entire datagram (and any constituent
290 * fragments, including those not yet received) MUST be silently
294 /* Check for overlap with preceding fragment. */
296 (NFCT_FRAG6_CB(prev
)->offset
+ prev
->len
) > offset
)
299 /* Look for overlap with succeeding segment. */
300 if (next
&& NFCT_FRAG6_CB(next
)->offset
< end
)
303 NFCT_FRAG6_CB(skb
)->offset
= offset
;
305 /* Insert this fragment in the chain of fragments. */
308 fq
->q
.fragments_tail
= skb
;
312 fq
->q
.fragments
= skb
;
315 fq
->iif
= skb
->dev
->ifindex
;
318 fq
->q
.stamp
= skb
->tstamp
;
319 fq
->q
.meat
+= skb
->len
;
320 if (payload_len
> fq
->q
.max_size
)
321 fq
->q
.max_size
= payload_len
;
322 add_frag_mem_limit(&fq
->q
, skb
->truesize
);
324 /* The first fragment.
325 * nhoffset is obtained from the first fragment, of course.
328 fq
->nhoffset
= nhoff
;
329 fq
->q
.last_in
|= INET_FRAG_FIRST_IN
;
332 inet_frag_lru_move(&fq
->q
);
336 inet_frag_kill(&fq
->q
, &nf_frags
);
342 * Check if this packet is complete.
343 * Returns NULL on failure by any reason, and pointer
344 * to current nexthdr field in reassembled frame.
346 * It is called with locked fq, and caller must check that
347 * queue is eligible for reassembly i.e. it is not COMPLETE,
348 * the last and the first frames arrived and all the bits are here.
350 static struct sk_buff
*
351 nf_ct_frag6_reasm(struct frag_queue
*fq
, struct net_device
*dev
)
353 struct sk_buff
*fp
, *op
, *head
= fq
->q
.fragments
;
356 inet_frag_kill(&fq
->q
, &nf_frags
);
358 WARN_ON(head
== NULL
);
359 WARN_ON(NFCT_FRAG6_CB(head
)->offset
!= 0);
361 /* Unfragmented part is taken from the first segment. */
362 payload_len
= ((head
->data
- skb_network_header(head
)) -
363 sizeof(struct ipv6hdr
) + fq
->q
.len
-
364 sizeof(struct frag_hdr
));
365 if (payload_len
> IPV6_MAXPLEN
) {
366 pr_debug("payload len is too large.\n");
370 /* Head of list must not be cloned. */
371 if (skb_unclone(head
, GFP_ATOMIC
)) {
372 pr_debug("skb is cloned but can't expand head");
376 /* If the first fragment is fragmented itself, we split
377 * it to two chunks: the first with data and paged part
378 * and the second, holding only fragments. */
379 if (skb_has_frag_list(head
)) {
380 struct sk_buff
*clone
;
383 clone
= alloc_skb(0, GFP_ATOMIC
);
387 clone
->next
= head
->next
;
389 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
390 skb_frag_list_init(head
);
391 for (i
= 0; i
< skb_shinfo(head
)->nr_frags
; i
++)
392 plen
+= skb_frag_size(&skb_shinfo(head
)->frags
[i
]);
393 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
394 head
->data_len
-= clone
->len
;
395 head
->len
-= clone
->len
;
397 clone
->ip_summed
= head
->ip_summed
;
399 NFCT_FRAG6_CB(clone
)->orig
= NULL
;
400 add_frag_mem_limit(&fq
->q
, clone
->truesize
);
403 /* We have to remove fragment header from datagram and to relocate
404 * header in order to calculate ICV correctly. */
405 skb_network_header(head
)[fq
->nhoffset
] = skb_transport_header(head
)[0];
406 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
407 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
408 head
->mac_header
+= sizeof(struct frag_hdr
);
409 head
->network_header
+= sizeof(struct frag_hdr
);
411 skb_shinfo(head
)->frag_list
= head
->next
;
412 skb_reset_transport_header(head
);
413 skb_push(head
, head
->data
- skb_network_header(head
));
415 for (fp
=head
->next
; fp
; fp
= fp
->next
) {
416 head
->data_len
+= fp
->len
;
417 head
->len
+= fp
->len
;
418 if (head
->ip_summed
!= fp
->ip_summed
)
419 head
->ip_summed
= CHECKSUM_NONE
;
420 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
421 head
->csum
= csum_add(head
->csum
, fp
->csum
);
422 head
->truesize
+= fp
->truesize
;
424 sub_frag_mem_limit(&fq
->q
, head
->truesize
);
429 head
->tstamp
= fq
->q
.stamp
;
430 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
431 IP6CB(head
)->frag_max_size
= sizeof(struct ipv6hdr
) + fq
->q
.max_size
;
433 /* Yes, and fold redundant checksum back. 8) */
434 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
435 head
->csum
= csum_partial(skb_network_header(head
),
436 skb_network_header_len(head
),
439 fq
->q
.fragments
= NULL
;
440 fq
->q
.fragments_tail
= NULL
;
442 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
443 fp
= skb_shinfo(head
)->frag_list
;
444 if (fp
&& NFCT_FRAG6_CB(fp
)->orig
== NULL
)
445 /* at above code, head skb is divided into two skbs. */
448 op
= NFCT_FRAG6_CB(head
)->orig
;
449 for (; fp
; fp
= fp
->next
) {
450 struct sk_buff
*orig
= NFCT_FRAG6_CB(fp
)->orig
;
454 NFCT_FRAG6_CB(fp
)->orig
= NULL
;
460 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
464 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
470 * find the header just before Fragment Header.
472 * if success return 0 and set ...
473 * (*prevhdrp): the value of "Next Header Field" in the header
474 * just before Fragment Header.
475 * (*prevhoff): the offset of "Next Header Field" in the header
476 * just before Fragment Header.
477 * (*fhoff) : the offset of Fragment Header.
479 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
483 find_prev_fhdr(struct sk_buff
*skb
, u8
*prevhdrp
, int *prevhoff
, int *fhoff
)
485 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
486 const int netoff
= skb_network_offset(skb
);
487 u8 prev_nhoff
= netoff
+ offsetof(struct ipv6hdr
, nexthdr
);
488 int start
= netoff
+ sizeof(struct ipv6hdr
);
489 int len
= skb
->len
- start
;
490 u8 prevhdr
= NEXTHDR_IPV6
;
492 while (nexthdr
!= NEXTHDR_FRAGMENT
) {
493 struct ipv6_opt_hdr hdr
;
496 if (!ipv6_ext_hdr(nexthdr
)) {
499 if (nexthdr
== NEXTHDR_NONE
) {
500 pr_debug("next header is none\n");
503 if (len
< (int)sizeof(struct ipv6_opt_hdr
)) {
504 pr_debug("too short\n");
507 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
509 if (nexthdr
== NEXTHDR_AUTH
)
510 hdrlen
= (hdr
.hdrlen
+2)<<2;
512 hdrlen
= ipv6_optlen(&hdr
);
517 nexthdr
= hdr
.nexthdr
;
526 *prevhoff
= prev_nhoff
;
532 struct sk_buff
*nf_ct_frag6_gather(struct sk_buff
*skb
, u32 user
)
534 struct sk_buff
*clone
;
535 struct net_device
*dev
= skb
->dev
;
536 struct net
*net
= skb_dst(skb
) ? dev_net(skb_dst(skb
)->dev
)
538 struct frag_hdr
*fhdr
;
539 struct frag_queue
*fq
;
543 struct sk_buff
*ret_skb
= NULL
;
545 /* Jumbo payload inhibits frag. header */
546 if (ipv6_hdr(skb
)->payload_len
== 0) {
547 pr_debug("payload len = 0\n");
551 if (find_prev_fhdr(skb
, &prevhdr
, &nhoff
, &fhoff
) < 0)
554 clone
= skb_clone(skb
, GFP_ATOMIC
);
556 pr_debug("Can't clone skb\n");
560 NFCT_FRAG6_CB(clone
)->orig
= skb
;
562 if (!pskb_may_pull(clone
, fhoff
+ sizeof(*fhdr
))) {
563 pr_debug("message is too short.\n");
567 skb_set_transport_header(clone
, fhoff
);
568 hdr
= ipv6_hdr(clone
);
569 fhdr
= (struct frag_hdr
*)skb_transport_header(clone
);
572 inet_frag_evictor(&net
->nf_frag
.frags
, &nf_frags
, false);
575 fq
= fq_find(net
, fhdr
->identification
, user
, &hdr
->saddr
, &hdr
->daddr
);
577 pr_debug("Can't find and can't create new queue\n");
581 spin_lock_bh(&fq
->q
.lock
);
583 if (nf_ct_frag6_queue(fq
, clone
, fhdr
, nhoff
) < 0) {
584 spin_unlock_bh(&fq
->q
.lock
);
585 pr_debug("Can't insert skb to queue\n");
586 inet_frag_put(&fq
->q
, &nf_frags
);
590 if (fq
->q
.last_in
== (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
591 fq
->q
.meat
== fq
->q
.len
) {
592 ret_skb
= nf_ct_frag6_reasm(fq
, dev
);
594 pr_debug("Can't reassemble fragmented packets\n");
596 spin_unlock_bh(&fq
->q
.lock
);
598 inet_frag_put(&fq
->q
, &nf_frags
);
606 void nf_ct_frag6_output(unsigned int hooknum
, struct sk_buff
*skb
,
607 struct net_device
*in
, struct net_device
*out
,
608 int (*okfn
)(struct sk_buff
*))
610 struct sk_buff
*s
, *s2
;
611 unsigned int ret
= 0;
613 for (s
= NFCT_FRAG6_CB(skb
)->orig
; s
;) {
614 nf_conntrack_put_reasm(s
->nfct_reasm
);
615 nf_conntrack_get_reasm(skb
);
621 if (ret
!= -ECANCELED
)
622 ret
= NF_HOOK_THRESH(NFPROTO_IPV6
, hooknum
, s
,
624 NF_IP6_PRI_CONNTRACK_DEFRAG
+ 1);
630 nf_conntrack_put_reasm(skb
);
633 static int nf_ct_net_init(struct net
*net
)
635 net
->nf_frag
.frags
.high_thresh
= IPV6_FRAG_HIGH_THRESH
;
636 net
->nf_frag
.frags
.low_thresh
= IPV6_FRAG_LOW_THRESH
;
637 net
->nf_frag
.frags
.timeout
= IPV6_FRAG_TIMEOUT
;
638 inet_frags_init_net(&net
->nf_frag
.frags
);
640 return nf_ct_frag6_sysctl_register(net
);
643 static void nf_ct_net_exit(struct net
*net
)
645 nf_ct_frags6_sysctl_unregister(net
);
646 inet_frags_exit_net(&net
->nf_frag
.frags
, &nf_frags
);
649 static struct pernet_operations nf_ct_net_ops
= {
650 .init
= nf_ct_net_init
,
651 .exit
= nf_ct_net_exit
,
654 int nf_ct_frag6_init(void)
658 nf_frags
.hashfn
= nf_hashfn
;
659 nf_frags
.constructor
= ip6_frag_init
;
660 nf_frags
.destructor
= NULL
;
661 nf_frags
.skb_free
= nf_skb_free
;
662 nf_frags
.qsize
= sizeof(struct frag_queue
);
663 nf_frags
.match
= ip6_frag_match
;
664 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
665 nf_frags
.secret_interval
= 10 * 60 * HZ
;
666 inet_frags_init(&nf_frags
);
668 ret
= register_pernet_subsys(&nf_ct_net_ops
);
670 inet_frags_fini(&nf_frags
);
675 void nf_ct_frag6_cleanup(void)
677 unregister_pernet_subsys(&nf_ct_net_ops
);
678 inet_frags_fini(&nf_frags
);