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 unsigned int nf_hash_frag(__be32 id
, const struct in6_addr
*saddr
,
156 const struct in6_addr
*daddr
)
158 net_get_random_once(&nf_frags
.rnd
, sizeof(nf_frags
.rnd
));
159 return jhash_3words(ipv6_addr_hash(saddr
), ipv6_addr_hash(daddr
),
160 (__force u32
)id
, nf_frags
.rnd
);
164 static unsigned int nf_hashfn(const struct inet_frag_queue
*q
)
166 const struct frag_queue
*nq
;
168 nq
= container_of(q
, struct frag_queue
, q
);
169 return nf_hash_frag(nq
->id
, &nq
->saddr
, &nq
->daddr
);
172 static void nf_ct_frag6_expire(struct timer_list
*t
)
174 struct inet_frag_queue
*frag
= from_timer(frag
, t
, timer
);
175 struct frag_queue
*fq
;
178 fq
= container_of(frag
, struct frag_queue
, q
);
179 net
= container_of(fq
->q
.net
, struct net
, nf_frag
.frags
);
181 ip6_expire_frag_queue(net
, fq
);
184 /* Creation primitives. */
185 static inline struct frag_queue
*fq_find(struct net
*net
, __be32 id
,
186 u32 user
, struct in6_addr
*src
,
187 struct in6_addr
*dst
, int iif
, u8 ecn
)
189 struct inet_frag_queue
*q
;
190 struct ip6_create_arg arg
;
201 hash
= nf_hash_frag(id
, src
, dst
);
203 q
= inet_frag_find(&net
->nf_frag
.frags
, &nf_frags
, &arg
, hash
);
205 if (IS_ERR_OR_NULL(q
)) {
206 inet_frag_maybe_warn_overflow(q
, pr_fmt());
209 return container_of(q
, struct frag_queue
, q
);
213 static int nf_ct_frag6_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
214 const struct frag_hdr
*fhdr
, int nhoff
)
216 struct sk_buff
*prev
, *next
;
217 unsigned int payload_len
;
221 if (fq
->q
.flags
& INET_FRAG_COMPLETE
) {
222 pr_debug("Already completed\n");
226 payload_len
= ntohs(ipv6_hdr(skb
)->payload_len
);
228 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
229 end
= offset
+ (payload_len
-
230 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
232 if ((unsigned int)end
> IPV6_MAXPLEN
) {
233 pr_debug("offset is too large.\n");
237 ecn
= ip6_frag_ecn(ipv6_hdr(skb
));
239 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
240 const unsigned char *nh
= skb_network_header(skb
);
241 skb
->csum
= csum_sub(skb
->csum
,
242 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
246 /* Is this the final fragment? */
247 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
248 /* If we already have some bits beyond end
249 * or have different end, the segment is corrupted.
251 if (end
< fq
->q
.len
||
252 ((fq
->q
.flags
& INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
253 pr_debug("already received last fragment\n");
256 fq
->q
.flags
|= INET_FRAG_LAST_IN
;
259 /* Check if the fragment is rounded to 8 bytes.
260 * Required by the RFC.
263 /* RFC2460 says always send parameter problem in
266 pr_debug("end of fragment not rounded to 8 bytes.\n");
267 inet_frag_kill(&fq
->q
);
270 if (end
> fq
->q
.len
) {
271 /* Some bits beyond end -> corruption. */
272 if (fq
->q
.flags
& INET_FRAG_LAST_IN
) {
273 pr_debug("last packet already reached.\n");
283 /* Point into the IP datagram 'data' part. */
284 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
)) {
285 pr_debug("queue: message is too short.\n");
288 if (pskb_trim_rcsum(skb
, end
- offset
)) {
289 pr_debug("Can't trim\n");
293 /* Find out which fragments are in front and at the back of us
294 * in the chain of fragments so far. We must know where to put
295 * this fragment, right?
297 prev
= fq
->q
.fragments_tail
;
298 if (!prev
|| NFCT_FRAG6_CB(prev
)->offset
< offset
) {
303 for (next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
304 if (NFCT_FRAG6_CB(next
)->offset
>= offset
)
310 /* RFC5722, Section 4:
311 * When reassembling an IPv6 datagram, if
312 * one or more its constituent fragments is determined to be an
313 * overlapping fragment, the entire datagram (and any constituent
314 * fragments, including those not yet received) MUST be silently
318 /* Check for overlap with preceding fragment. */
320 (NFCT_FRAG6_CB(prev
)->offset
+ prev
->len
) > offset
)
323 /* Look for overlap with succeeding segment. */
324 if (next
&& NFCT_FRAG6_CB(next
)->offset
< end
)
327 NFCT_FRAG6_CB(skb
)->offset
= offset
;
329 /* Insert this fragment in the chain of fragments. */
332 fq
->q
.fragments_tail
= skb
;
336 fq
->q
.fragments
= skb
;
339 fq
->iif
= skb
->dev
->ifindex
;
342 fq
->q
.stamp
= skb
->tstamp
;
343 fq
->q
.meat
+= skb
->len
;
345 if (payload_len
> fq
->q
.max_size
)
346 fq
->q
.max_size
= payload_len
;
347 add_frag_mem_limit(fq
->q
.net
, skb
->truesize
);
349 /* The first fragment.
350 * nhoffset is obtained from the first fragment, of course.
353 fq
->nhoffset
= nhoff
;
354 fq
->q
.flags
|= INET_FRAG_FIRST_IN
;
360 inet_frag_kill(&fq
->q
);
366 * Check if this packet is complete.
368 * It is called with locked fq, and caller must check that
369 * queue is eligible for reassembly i.e. it is not COMPLETE,
370 * the last and the first frames arrived and all the bits are here.
372 * returns true if *prev skb has been transformed into the reassembled
373 * skb, false otherwise.
376 nf_ct_frag6_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
, struct net_device
*dev
)
378 struct sk_buff
*fp
, *head
= fq
->q
.fragments
;
382 inet_frag_kill(&fq
->q
);
384 WARN_ON(head
== NULL
);
385 WARN_ON(NFCT_FRAG6_CB(head
)->offset
!= 0);
387 ecn
= ip_frag_ecn_table
[fq
->ecn
];
388 if (unlikely(ecn
== 0xff))
391 /* Unfragmented part is taken from the first segment. */
392 payload_len
= ((head
->data
- skb_network_header(head
)) -
393 sizeof(struct ipv6hdr
) + fq
->q
.len
-
394 sizeof(struct frag_hdr
));
395 if (payload_len
> IPV6_MAXPLEN
) {
396 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
401 /* Head of list must not be cloned. */
402 if (skb_unclone(head
, GFP_ATOMIC
))
405 /* If the first fragment is fragmented itself, we split
406 * it to two chunks: the first with data and paged part
407 * and the second, holding only fragments. */
408 if (skb_has_frag_list(head
)) {
409 struct sk_buff
*clone
;
412 clone
= alloc_skb(0, GFP_ATOMIC
);
416 clone
->next
= head
->next
;
418 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
419 skb_frag_list_init(head
);
420 for (i
= 0; i
< skb_shinfo(head
)->nr_frags
; i
++)
421 plen
+= skb_frag_size(&skb_shinfo(head
)->frags
[i
]);
422 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
423 head
->data_len
-= clone
->len
;
424 head
->len
-= clone
->len
;
426 clone
->ip_summed
= head
->ip_summed
;
428 add_frag_mem_limit(fq
->q
.net
, clone
->truesize
);
431 /* morph head into last received skb: prev.
433 * This allows callers of ipv6 conntrack defrag to continue
434 * to use the last skb(frag) passed into the reasm engine.
435 * The last skb frag 'silently' turns into the full reassembled skb.
437 * Since prev is also part of q->fragments we have to clone it first.
440 struct sk_buff
*iter
;
442 fp
= skb_clone(prev
, GFP_ATOMIC
);
446 fp
->next
= prev
->next
;
450 if (iter
->next
== prev
) {
457 skb_morph(prev
, head
);
458 prev
->next
= head
->next
;
463 /* We have to remove fragment header from datagram and to relocate
464 * header in order to calculate ICV correctly. */
465 skb_network_header(head
)[fq
->nhoffset
] = skb_transport_header(head
)[0];
466 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
467 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
468 head
->mac_header
+= sizeof(struct frag_hdr
);
469 head
->network_header
+= sizeof(struct frag_hdr
);
471 skb_shinfo(head
)->frag_list
= head
->next
;
472 skb_reset_transport_header(head
);
473 skb_push(head
, head
->data
- skb_network_header(head
));
475 for (fp
= head
->next
; fp
; fp
= fp
->next
) {
476 head
->data_len
+= fp
->len
;
477 head
->len
+= fp
->len
;
478 if (head
->ip_summed
!= fp
->ip_summed
)
479 head
->ip_summed
= CHECKSUM_NONE
;
480 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
481 head
->csum
= csum_add(head
->csum
, fp
->csum
);
482 head
->truesize
+= fp
->truesize
;
484 sub_frag_mem_limit(fq
->q
.net
, head
->truesize
);
489 head
->tstamp
= fq
->q
.stamp
;
490 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
491 ipv6_change_dsfield(ipv6_hdr(head
), 0xff, ecn
);
492 IP6CB(head
)->frag_max_size
= sizeof(struct ipv6hdr
) + fq
->q
.max_size
;
494 /* Yes, and fold redundant checksum back. 8) */
495 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
496 head
->csum
= csum_partial(skb_network_header(head
),
497 skb_network_header_len(head
),
500 fq
->q
.fragments
= NULL
;
501 fq
->q
.fragments_tail
= NULL
;
507 * find the header just before Fragment Header.
509 * if success return 0 and set ...
510 * (*prevhdrp): the value of "Next Header Field" in the header
511 * just before Fragment Header.
512 * (*prevhoff): the offset of "Next Header Field" in the header
513 * just before Fragment Header.
514 * (*fhoff) : the offset of Fragment Header.
516 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
520 find_prev_fhdr(struct sk_buff
*skb
, u8
*prevhdrp
, int *prevhoff
, int *fhoff
)
522 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
523 const int netoff
= skb_network_offset(skb
);
524 u8 prev_nhoff
= netoff
+ offsetof(struct ipv6hdr
, nexthdr
);
525 int start
= netoff
+ sizeof(struct ipv6hdr
);
526 int len
= skb
->len
- start
;
527 u8 prevhdr
= NEXTHDR_IPV6
;
529 while (nexthdr
!= NEXTHDR_FRAGMENT
) {
530 struct ipv6_opt_hdr hdr
;
533 if (!ipv6_ext_hdr(nexthdr
)) {
536 if (nexthdr
== NEXTHDR_NONE
) {
537 pr_debug("next header is none\n");
540 if (len
< (int)sizeof(struct ipv6_opt_hdr
)) {
541 pr_debug("too short\n");
544 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
546 if (nexthdr
== NEXTHDR_AUTH
)
547 hdrlen
= (hdr
.hdrlen
+2)<<2;
549 hdrlen
= ipv6_optlen(&hdr
);
554 nexthdr
= hdr
.nexthdr
;
563 *prevhoff
= prev_nhoff
;
569 int nf_ct_frag6_gather(struct net
*net
, struct sk_buff
*skb
, u32 user
)
571 u16 savethdr
= skb
->transport_header
;
572 struct net_device
*dev
= skb
->dev
;
573 int fhoff
, nhoff
, ret
;
574 struct frag_hdr
*fhdr
;
575 struct frag_queue
*fq
;
579 /* Jumbo payload inhibits frag. header */
580 if (ipv6_hdr(skb
)->payload_len
== 0) {
581 pr_debug("payload len = 0\n");
585 if (find_prev_fhdr(skb
, &prevhdr
, &nhoff
, &fhoff
) < 0)
588 if (!pskb_may_pull(skb
, fhoff
+ sizeof(*fhdr
)))
591 skb_set_transport_header(skb
, fhoff
);
593 fhdr
= (struct frag_hdr
*)skb_transport_header(skb
);
596 fq
= fq_find(net
, fhdr
->identification
, user
, &hdr
->saddr
, &hdr
->daddr
,
597 skb
->dev
? skb
->dev
->ifindex
: 0, ip6_frag_ecn(hdr
));
599 pr_debug("Can't find and can't create new queue\n");
603 spin_lock_bh(&fq
->q
.lock
);
605 ret
= nf_ct_frag6_queue(fq
, skb
, fhdr
, nhoff
);
607 if (ret
== -EPROTO
) {
608 skb
->transport_header
= savethdr
;
614 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
618 if (fq
->q
.flags
== (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
619 fq
->q
.meat
== fq
->q
.len
&&
620 nf_ct_frag6_reasm(fq
, skb
, dev
))
626 spin_unlock_bh(&fq
->q
.lock
);
627 inet_frag_put(&fq
->q
);
630 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather
);
632 static int nf_ct_net_init(struct net
*net
)
636 net
->nf_frag
.frags
.high_thresh
= IPV6_FRAG_HIGH_THRESH
;
637 net
->nf_frag
.frags
.low_thresh
= IPV6_FRAG_LOW_THRESH
;
638 net
->nf_frag
.frags
.timeout
= IPV6_FRAG_TIMEOUT
;
639 net
->nf_frag
.frags
.f
= &nf_frags
;
641 res
= inet_frags_init_net(&net
->nf_frag
.frags
);
644 res
= nf_ct_frag6_sysctl_register(net
);
646 inet_frags_exit_net(&net
->nf_frag
.frags
);
650 static void nf_ct_net_exit(struct net
*net
)
652 nf_ct_frags6_sysctl_unregister(net
);
653 inet_frags_exit_net(&net
->nf_frag
.frags
);
656 static struct pernet_operations nf_ct_net_ops
= {
657 .init
= nf_ct_net_init
,
658 .exit
= nf_ct_net_exit
,
661 int nf_ct_frag6_init(void)
665 nf_frags
.hashfn
= nf_hashfn
;
666 nf_frags
.constructor
= ip6_frag_init
;
667 nf_frags
.destructor
= NULL
;
668 nf_frags
.qsize
= sizeof(struct frag_queue
);
669 nf_frags
.match
= ip6_frag_match
;
670 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
671 nf_frags
.frags_cache_name
= nf_frags_cache_name
;
672 ret
= inet_frags_init(&nf_frags
);
675 ret
= register_pernet_subsys(&nf_ct_net_ops
);
677 inet_frags_fini(&nf_frags
);
683 void nf_ct_frag6_cleanup(void)
685 unregister_pernet_subsys(&nf_ct_net_ops
);
686 inet_frags_fini(&nf_frags
);