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 #define pr_fmt(fmt) "IPv6-nf: " fmt
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/jiffies.h>
25 #include <linux/net.h>
26 #include <linux/list.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/ipv6.h>
30 #include <linux/icmpv6.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
36 #include <net/inet_frag.h>
39 #include <net/protocol.h>
40 #include <net/transp_v6.h>
41 #include <net/rawv6.h>
42 #include <net/ndisc.h>
43 #include <net/addrconf.h>
44 #include <net/inet_ecn.h>
45 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
46 #include <linux/sysctl.h>
47 #include <linux/netfilter.h>
48 #include <linux/netfilter_ipv6.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
53 static const char nf_frags_cache_name
[] = "nf-frags";
55 struct nf_ct_frag6_skb_cb
57 struct inet6_skb_parm h
;
61 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb *)((skb)->cb))
63 static struct inet_frags nf_frags
;
68 static struct ctl_table nf_ct_frag6_sysctl_table
[] = {
70 .procname
= "nf_conntrack_frag6_timeout",
71 .data
= &init_net
.nf_frag
.frags
.timeout
,
72 .maxlen
= sizeof(unsigned int),
74 .proc_handler
= proc_dointvec_jiffies
,
77 .procname
= "nf_conntrack_frag6_low_thresh",
78 .data
= &init_net
.nf_frag
.frags
.low_thresh
,
79 .maxlen
= sizeof(unsigned int),
81 .proc_handler
= proc_dointvec_minmax
,
83 .extra2
= &init_net
.nf_frag
.frags
.high_thresh
86 .procname
= "nf_conntrack_frag6_high_thresh",
87 .data
= &init_net
.nf_frag
.frags
.high_thresh
,
88 .maxlen
= sizeof(unsigned int),
90 .proc_handler
= proc_dointvec_minmax
,
91 .extra1
= &init_net
.nf_frag
.frags
.low_thresh
96 static int nf_ct_frag6_sysctl_register(struct net
*net
)
98 struct ctl_table
*table
;
99 struct ctl_table_header
*hdr
;
101 table
= nf_ct_frag6_sysctl_table
;
102 if (!net_eq(net
, &init_net
)) {
103 table
= kmemdup(table
, sizeof(nf_ct_frag6_sysctl_table
),
108 table
[0].data
= &net
->nf_frag
.frags
.timeout
;
109 table
[1].data
= &net
->nf_frag
.frags
.low_thresh
;
110 table
[1].extra2
= &net
->nf_frag
.frags
.high_thresh
;
111 table
[2].data
= &net
->nf_frag
.frags
.high_thresh
;
112 table
[2].extra1
= &net
->nf_frag
.frags
.low_thresh
;
113 table
[2].extra2
= &init_net
.nf_frag
.frags
.high_thresh
;
116 hdr
= register_net_sysctl(net
, "net/netfilter", table
);
120 net
->nf_frag_frags_hdr
= hdr
;
124 if (!net_eq(net
, &init_net
))
130 static void __net_exit
nf_ct_frags6_sysctl_unregister(struct net
*net
)
132 struct ctl_table
*table
;
134 table
= net
->nf_frag_frags_hdr
->ctl_table_arg
;
135 unregister_net_sysctl_table(net
->nf_frag_frags_hdr
);
136 if (!net_eq(net
, &init_net
))
141 static int nf_ct_frag6_sysctl_register(struct net
*net
)
145 static void __net_exit
nf_ct_frags6_sysctl_unregister(struct net
*net
)
150 static inline u8
ip6_frag_ecn(const struct ipv6hdr
*ipv6h
)
152 return 1 << (ipv6_get_dsfield(ipv6h
) & INET_ECN_MASK
);
155 static void nf_ct_frag6_expire(struct timer_list
*t
)
157 struct inet_frag_queue
*frag
= from_timer(frag
, t
, timer
);
158 struct frag_queue
*fq
;
161 fq
= container_of(frag
, struct frag_queue
, q
);
162 net
= container_of(fq
->q
.net
, struct net
, nf_frag
.frags
);
164 ip6_expire_frag_queue(net
, fq
);
167 /* Creation primitives. */
168 static struct frag_queue
*fq_find(struct net
*net
, __be32 id
, u32 user
,
169 const struct ipv6hdr
*hdr
, int iif
)
171 struct frag_v6_compare_key key
= {
178 struct inet_frag_queue
*q
;
180 q
= inet_frag_find(&net
->nf_frag
.frags
, &key
);
184 return container_of(q
, struct frag_queue
, q
);
188 static int nf_ct_frag6_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
189 const struct frag_hdr
*fhdr
, int nhoff
)
191 struct sk_buff
*prev
, *next
;
192 unsigned int payload_len
;
196 if (fq
->q
.flags
& INET_FRAG_COMPLETE
) {
197 pr_debug("Already completed\n");
201 payload_len
= ntohs(ipv6_hdr(skb
)->payload_len
);
203 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
204 end
= offset
+ (payload_len
-
205 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
207 if ((unsigned int)end
> IPV6_MAXPLEN
) {
208 pr_debug("offset is too large.\n");
212 ecn
= ip6_frag_ecn(ipv6_hdr(skb
));
214 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
215 const unsigned char *nh
= skb_network_header(skb
);
216 skb
->csum
= csum_sub(skb
->csum
,
217 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
221 /* Is this the final fragment? */
222 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
223 /* If we already have some bits beyond end
224 * or have different end, the segment is corrupted.
226 if (end
< fq
->q
.len
||
227 ((fq
->q
.flags
& INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
228 pr_debug("already received last fragment\n");
231 fq
->q
.flags
|= INET_FRAG_LAST_IN
;
234 /* Check if the fragment is rounded to 8 bytes.
235 * Required by the RFC.
238 /* RFC2460 says always send parameter problem in
241 pr_debug("end of fragment not rounded to 8 bytes.\n");
242 inet_frag_kill(&fq
->q
);
245 if (end
> fq
->q
.len
) {
246 /* Some bits beyond end -> corruption. */
247 if (fq
->q
.flags
& INET_FRAG_LAST_IN
) {
248 pr_debug("last packet already reached.\n");
258 /* Point into the IP datagram 'data' part. */
259 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
)) {
260 pr_debug("queue: message is too short.\n");
263 if (pskb_trim_rcsum(skb
, end
- offset
)) {
264 pr_debug("Can't trim\n");
268 /* Find out which fragments are in front and at the back of us
269 * in the chain of fragments so far. We must know where to put
270 * this fragment, right?
272 prev
= fq
->q
.fragments_tail
;
273 if (!prev
|| NFCT_FRAG6_CB(prev
)->offset
< offset
) {
278 for (next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
279 if (NFCT_FRAG6_CB(next
)->offset
>= offset
)
285 /* RFC5722, Section 4:
286 * When reassembling an IPv6 datagram, if
287 * one or more its constituent fragments is determined to be an
288 * overlapping fragment, the entire datagram (and any constituent
289 * fragments, including those not yet received) MUST be silently
293 /* Check for overlap with preceding fragment. */
295 (NFCT_FRAG6_CB(prev
)->offset
+ prev
->len
) > offset
)
298 /* Look for overlap with succeeding segment. */
299 if (next
&& NFCT_FRAG6_CB(next
)->offset
< end
)
302 NFCT_FRAG6_CB(skb
)->offset
= offset
;
304 /* Insert this fragment in the chain of fragments. */
307 fq
->q
.fragments_tail
= skb
;
311 fq
->q
.fragments
= skb
;
314 fq
->iif
= skb
->dev
->ifindex
;
317 fq
->q
.stamp
= skb
->tstamp
;
318 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
.net
, skb
->truesize
);
324 /* The first fragment.
325 * nhoffset is obtained from the first fragment, of course.
328 fq
->nhoffset
= nhoff
;
329 fq
->q
.flags
|= INET_FRAG_FIRST_IN
;
335 inet_frag_kill(&fq
->q
);
341 * Check if this packet is complete.
343 * It is called with locked fq, and caller must check that
344 * queue is eligible for reassembly i.e. it is not COMPLETE,
345 * the last and the first frames arrived and all the bits are here.
347 * returns true if *prev skb has been transformed into the reassembled
348 * skb, false otherwise.
351 nf_ct_frag6_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
, struct net_device
*dev
)
353 struct sk_buff
*fp
, *head
= fq
->q
.fragments
;
357 inet_frag_kill(&fq
->q
);
359 WARN_ON(head
== NULL
);
360 WARN_ON(NFCT_FRAG6_CB(head
)->offset
!= 0);
362 ecn
= ip_frag_ecn_table
[fq
->ecn
];
363 if (unlikely(ecn
== 0xff))
366 /* Unfragmented part is taken from the first segment. */
367 payload_len
= ((head
->data
- skb_network_header(head
)) -
368 sizeof(struct ipv6hdr
) + fq
->q
.len
-
369 sizeof(struct frag_hdr
));
370 if (payload_len
> IPV6_MAXPLEN
) {
371 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
376 /* Head of list must not be cloned. */
377 if (skb_unclone(head
, GFP_ATOMIC
))
380 /* If the first fragment is fragmented itself, we split
381 * it to two chunks: the first with data and paged part
382 * and the second, holding only fragments. */
383 if (skb_has_frag_list(head
)) {
384 struct sk_buff
*clone
;
387 clone
= alloc_skb(0, GFP_ATOMIC
);
391 clone
->next
= head
->next
;
393 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
394 skb_frag_list_init(head
);
395 for (i
= 0; i
< skb_shinfo(head
)->nr_frags
; i
++)
396 plen
+= skb_frag_size(&skb_shinfo(head
)->frags
[i
]);
397 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
398 head
->data_len
-= clone
->len
;
399 head
->len
-= clone
->len
;
401 clone
->ip_summed
= head
->ip_summed
;
403 add_frag_mem_limit(fq
->q
.net
, clone
->truesize
);
406 /* morph head into last received skb: prev.
408 * This allows callers of ipv6 conntrack defrag to continue
409 * to use the last skb(frag) passed into the reasm engine.
410 * The last skb frag 'silently' turns into the full reassembled skb.
412 * Since prev is also part of q->fragments we have to clone it first.
415 struct sk_buff
*iter
;
417 fp
= skb_clone(prev
, GFP_ATOMIC
);
421 fp
->next
= prev
->next
;
425 if (iter
->next
== prev
) {
432 skb_morph(prev
, head
);
433 prev
->next
= head
->next
;
438 /* We have to remove fragment header from datagram and to relocate
439 * header in order to calculate ICV correctly. */
440 skb_network_header(head
)[fq
->nhoffset
] = skb_transport_header(head
)[0];
441 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
442 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
443 head
->mac_header
+= sizeof(struct frag_hdr
);
444 head
->network_header
+= sizeof(struct frag_hdr
);
446 skb_shinfo(head
)->frag_list
= head
->next
;
447 skb_reset_transport_header(head
);
448 skb_push(head
, head
->data
- skb_network_header(head
));
450 for (fp
= head
->next
; fp
; fp
= fp
->next
) {
451 head
->data_len
+= fp
->len
;
452 head
->len
+= fp
->len
;
453 if (head
->ip_summed
!= fp
->ip_summed
)
454 head
->ip_summed
= CHECKSUM_NONE
;
455 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
456 head
->csum
= csum_add(head
->csum
, fp
->csum
);
457 head
->truesize
+= fp
->truesize
;
459 sub_frag_mem_limit(fq
->q
.net
, head
->truesize
);
464 head
->tstamp
= fq
->q
.stamp
;
465 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
466 ipv6_change_dsfield(ipv6_hdr(head
), 0xff, ecn
);
467 IP6CB(head
)->frag_max_size
= sizeof(struct ipv6hdr
) + fq
->q
.max_size
;
469 /* Yes, and fold redundant checksum back. 8) */
470 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
471 head
->csum
= csum_partial(skb_network_header(head
),
472 skb_network_header_len(head
),
475 fq
->q
.fragments
= NULL
;
476 fq
->q
.fragments_tail
= NULL
;
482 * find the header just before Fragment Header.
484 * if success return 0 and set ...
485 * (*prevhdrp): the value of "Next Header Field" in the header
486 * just before Fragment Header.
487 * (*prevhoff): the offset of "Next Header Field" in the header
488 * just before Fragment Header.
489 * (*fhoff) : the offset of Fragment Header.
491 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
495 find_prev_fhdr(struct sk_buff
*skb
, u8
*prevhdrp
, int *prevhoff
, int *fhoff
)
497 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
498 const int netoff
= skb_network_offset(skb
);
499 u8 prev_nhoff
= netoff
+ offsetof(struct ipv6hdr
, nexthdr
);
500 int start
= netoff
+ sizeof(struct ipv6hdr
);
501 int len
= skb
->len
- start
;
502 u8 prevhdr
= NEXTHDR_IPV6
;
504 while (nexthdr
!= NEXTHDR_FRAGMENT
) {
505 struct ipv6_opt_hdr hdr
;
508 if (!ipv6_ext_hdr(nexthdr
)) {
511 if (nexthdr
== NEXTHDR_NONE
) {
512 pr_debug("next header is none\n");
515 if (len
< (int)sizeof(struct ipv6_opt_hdr
)) {
516 pr_debug("too short\n");
519 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
521 if (nexthdr
== NEXTHDR_AUTH
)
522 hdrlen
= (hdr
.hdrlen
+2)<<2;
524 hdrlen
= ipv6_optlen(&hdr
);
529 nexthdr
= hdr
.nexthdr
;
538 *prevhoff
= prev_nhoff
;
544 int nf_ct_frag6_gather(struct net
*net
, struct sk_buff
*skb
, u32 user
)
546 u16 savethdr
= skb
->transport_header
;
547 struct net_device
*dev
= skb
->dev
;
548 int fhoff
, nhoff
, ret
;
549 struct frag_hdr
*fhdr
;
550 struct frag_queue
*fq
;
554 /* Jumbo payload inhibits frag. header */
555 if (ipv6_hdr(skb
)->payload_len
== 0) {
556 pr_debug("payload len = 0\n");
560 if (find_prev_fhdr(skb
, &prevhdr
, &nhoff
, &fhoff
) < 0)
563 if (!pskb_may_pull(skb
, fhoff
+ sizeof(*fhdr
)))
566 skb_set_transport_header(skb
, fhoff
);
568 fhdr
= (struct frag_hdr
*)skb_transport_header(skb
);
571 fq
= fq_find(net
, fhdr
->identification
, user
, hdr
,
572 skb
->dev
? skb
->dev
->ifindex
: 0);
574 pr_debug("Can't find and can't create new queue\n");
578 spin_lock_bh(&fq
->q
.lock
);
580 ret
= nf_ct_frag6_queue(fq
, skb
, fhdr
, nhoff
);
582 if (ret
== -EPROTO
) {
583 skb
->transport_header
= savethdr
;
589 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
593 if (fq
->q
.flags
== (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
594 fq
->q
.meat
== fq
->q
.len
&&
595 nf_ct_frag6_reasm(fq
, skb
, dev
))
601 spin_unlock_bh(&fq
->q
.lock
);
602 inet_frag_put(&fq
->q
);
605 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather
);
607 static int nf_ct_net_init(struct net
*net
)
611 net
->nf_frag
.frags
.high_thresh
= IPV6_FRAG_HIGH_THRESH
;
612 net
->nf_frag
.frags
.low_thresh
= IPV6_FRAG_LOW_THRESH
;
613 net
->nf_frag
.frags
.timeout
= IPV6_FRAG_TIMEOUT
;
614 net
->nf_frag
.frags
.f
= &nf_frags
;
616 res
= inet_frags_init_net(&net
->nf_frag
.frags
);
619 res
= nf_ct_frag6_sysctl_register(net
);
621 inet_frags_exit_net(&net
->nf_frag
.frags
);
625 static void nf_ct_net_exit(struct net
*net
)
627 nf_ct_frags6_sysctl_unregister(net
);
628 inet_frags_exit_net(&net
->nf_frag
.frags
);
631 static struct pernet_operations nf_ct_net_ops
= {
632 .init
= nf_ct_net_init
,
633 .exit
= nf_ct_net_exit
,
636 int nf_ct_frag6_init(void)
640 nf_frags
.constructor
= ip6_frag_init
;
641 nf_frags
.destructor
= NULL
;
642 nf_frags
.qsize
= sizeof(struct frag_queue
);
643 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
644 nf_frags
.frags_cache_name
= nf_frags_cache_name
;
645 nf_frags
.rhash_params
= ip6_rhash_params
;
646 ret
= inet_frags_init(&nf_frags
);
649 ret
= register_pernet_subsys(&nf_ct_net_ops
);
651 inet_frags_fini(&nf_frags
);
657 void nf_ct_frag6_cleanup(void)
659 unregister_pernet_subsys(&nf_ct_net_ops
);
660 inet_frags_fini(&nf_frags
);