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
);
181 if (IS_ERR_OR_NULL(q
)) {
182 inet_frag_maybe_warn_overflow(q
, pr_fmt());
185 return container_of(q
, struct frag_queue
, q
);
189 static int nf_ct_frag6_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
190 const struct frag_hdr
*fhdr
, int nhoff
)
192 struct sk_buff
*prev
, *next
;
193 unsigned int payload_len
;
197 if (fq
->q
.flags
& INET_FRAG_COMPLETE
) {
198 pr_debug("Already completed\n");
202 payload_len
= ntohs(ipv6_hdr(skb
)->payload_len
);
204 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
205 end
= offset
+ (payload_len
-
206 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
208 if ((unsigned int)end
> IPV6_MAXPLEN
) {
209 pr_debug("offset is too large.\n");
213 ecn
= ip6_frag_ecn(ipv6_hdr(skb
));
215 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
216 const unsigned char *nh
= skb_network_header(skb
);
217 skb
->csum
= csum_sub(skb
->csum
,
218 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
222 /* Is this the final fragment? */
223 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
224 /* If we already have some bits beyond end
225 * or have different end, the segment is corrupted.
227 if (end
< fq
->q
.len
||
228 ((fq
->q
.flags
& INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
229 pr_debug("already received last fragment\n");
232 fq
->q
.flags
|= INET_FRAG_LAST_IN
;
235 /* Check if the fragment is rounded to 8 bytes.
236 * Required by the RFC.
239 /* RFC2460 says always send parameter problem in
242 pr_debug("end of fragment not rounded to 8 bytes.\n");
243 inet_frag_kill(&fq
->q
);
246 if (end
> fq
->q
.len
) {
247 /* Some bits beyond end -> corruption. */
248 if (fq
->q
.flags
& 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
;
321 if (payload_len
> fq
->q
.max_size
)
322 fq
->q
.max_size
= payload_len
;
323 add_frag_mem_limit(fq
->q
.net
, skb
->truesize
);
325 /* The first fragment.
326 * nhoffset is obtained from the first fragment, of course.
329 fq
->nhoffset
= nhoff
;
330 fq
->q
.flags
|= INET_FRAG_FIRST_IN
;
336 inet_frag_kill(&fq
->q
);
342 * Check if this packet is complete.
344 * It is called with locked fq, and caller must check that
345 * queue is eligible for reassembly i.e. it is not COMPLETE,
346 * the last and the first frames arrived and all the bits are here.
348 * returns true if *prev skb has been transformed into the reassembled
349 * skb, false otherwise.
352 nf_ct_frag6_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
, struct net_device
*dev
)
354 struct sk_buff
*fp
, *head
= fq
->q
.fragments
;
358 inet_frag_kill(&fq
->q
);
360 WARN_ON(head
== NULL
);
361 WARN_ON(NFCT_FRAG6_CB(head
)->offset
!= 0);
363 ecn
= ip_frag_ecn_table
[fq
->ecn
];
364 if (unlikely(ecn
== 0xff))
367 /* Unfragmented part is taken from the first segment. */
368 payload_len
= ((head
->data
- skb_network_header(head
)) -
369 sizeof(struct ipv6hdr
) + fq
->q
.len
-
370 sizeof(struct frag_hdr
));
371 if (payload_len
> IPV6_MAXPLEN
) {
372 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
377 /* Head of list must not be cloned. */
378 if (skb_unclone(head
, GFP_ATOMIC
))
381 /* If the first fragment is fragmented itself, we split
382 * it to two chunks: the first with data and paged part
383 * and the second, holding only fragments. */
384 if (skb_has_frag_list(head
)) {
385 struct sk_buff
*clone
;
388 clone
= alloc_skb(0, GFP_ATOMIC
);
392 clone
->next
= head
->next
;
394 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
395 skb_frag_list_init(head
);
396 for (i
= 0; i
< skb_shinfo(head
)->nr_frags
; i
++)
397 plen
+= skb_frag_size(&skb_shinfo(head
)->frags
[i
]);
398 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
399 head
->data_len
-= clone
->len
;
400 head
->len
-= clone
->len
;
402 clone
->ip_summed
= head
->ip_summed
;
404 add_frag_mem_limit(fq
->q
.net
, clone
->truesize
);
407 /* morph head into last received skb: prev.
409 * This allows callers of ipv6 conntrack defrag to continue
410 * to use the last skb(frag) passed into the reasm engine.
411 * The last skb frag 'silently' turns into the full reassembled skb.
413 * Since prev is also part of q->fragments we have to clone it first.
416 struct sk_buff
*iter
;
418 fp
= skb_clone(prev
, GFP_ATOMIC
);
422 fp
->next
= prev
->next
;
426 if (iter
->next
== prev
) {
433 skb_morph(prev
, head
);
434 prev
->next
= head
->next
;
439 /* We have to remove fragment header from datagram and to relocate
440 * header in order to calculate ICV correctly. */
441 skb_network_header(head
)[fq
->nhoffset
] = skb_transport_header(head
)[0];
442 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
443 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
444 head
->mac_header
+= sizeof(struct frag_hdr
);
445 head
->network_header
+= sizeof(struct frag_hdr
);
447 skb_shinfo(head
)->frag_list
= head
->next
;
448 skb_reset_transport_header(head
);
449 skb_push(head
, head
->data
- skb_network_header(head
));
451 for (fp
= head
->next
; fp
; fp
= fp
->next
) {
452 head
->data_len
+= fp
->len
;
453 head
->len
+= fp
->len
;
454 if (head
->ip_summed
!= fp
->ip_summed
)
455 head
->ip_summed
= CHECKSUM_NONE
;
456 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
457 head
->csum
= csum_add(head
->csum
, fp
->csum
);
458 head
->truesize
+= fp
->truesize
;
460 sub_frag_mem_limit(fq
->q
.net
, head
->truesize
);
465 head
->tstamp
= fq
->q
.stamp
;
466 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
467 ipv6_change_dsfield(ipv6_hdr(head
), 0xff, ecn
);
468 IP6CB(head
)->frag_max_size
= sizeof(struct ipv6hdr
) + fq
->q
.max_size
;
470 /* Yes, and fold redundant checksum back. 8) */
471 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
472 head
->csum
= csum_partial(skb_network_header(head
),
473 skb_network_header_len(head
),
476 fq
->q
.fragments
= NULL
;
477 fq
->q
.fragments_tail
= NULL
;
483 * find the header just before Fragment Header.
485 * if success return 0 and set ...
486 * (*prevhdrp): the value of "Next Header Field" in the header
487 * just before Fragment Header.
488 * (*prevhoff): the offset of "Next Header Field" in the header
489 * just before Fragment Header.
490 * (*fhoff) : the offset of Fragment Header.
492 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
496 find_prev_fhdr(struct sk_buff
*skb
, u8
*prevhdrp
, int *prevhoff
, int *fhoff
)
498 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
499 const int netoff
= skb_network_offset(skb
);
500 u8 prev_nhoff
= netoff
+ offsetof(struct ipv6hdr
, nexthdr
);
501 int start
= netoff
+ sizeof(struct ipv6hdr
);
502 int len
= skb
->len
- start
;
503 u8 prevhdr
= NEXTHDR_IPV6
;
505 while (nexthdr
!= NEXTHDR_FRAGMENT
) {
506 struct ipv6_opt_hdr hdr
;
509 if (!ipv6_ext_hdr(nexthdr
)) {
512 if (nexthdr
== NEXTHDR_NONE
) {
513 pr_debug("next header is none\n");
516 if (len
< (int)sizeof(struct ipv6_opt_hdr
)) {
517 pr_debug("too short\n");
520 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
522 if (nexthdr
== NEXTHDR_AUTH
)
523 hdrlen
= (hdr
.hdrlen
+2)<<2;
525 hdrlen
= ipv6_optlen(&hdr
);
530 nexthdr
= hdr
.nexthdr
;
539 *prevhoff
= prev_nhoff
;
545 int nf_ct_frag6_gather(struct net
*net
, struct sk_buff
*skb
, u32 user
)
547 u16 savethdr
= skb
->transport_header
;
548 struct net_device
*dev
= skb
->dev
;
549 int fhoff
, nhoff
, ret
;
550 struct frag_hdr
*fhdr
;
551 struct frag_queue
*fq
;
555 /* Jumbo payload inhibits frag. header */
556 if (ipv6_hdr(skb
)->payload_len
== 0) {
557 pr_debug("payload len = 0\n");
561 if (find_prev_fhdr(skb
, &prevhdr
, &nhoff
, &fhoff
) < 0)
564 if (!pskb_may_pull(skb
, fhoff
+ sizeof(*fhdr
)))
567 skb_set_transport_header(skb
, fhoff
);
569 fhdr
= (struct frag_hdr
*)skb_transport_header(skb
);
572 fq
= fq_find(net
, fhdr
->identification
, user
, hdr
,
573 skb
->dev
? skb
->dev
->ifindex
: 0);
575 pr_debug("Can't find and can't create new queue\n");
579 spin_lock_bh(&fq
->q
.lock
);
581 ret
= nf_ct_frag6_queue(fq
, skb
, fhdr
, nhoff
);
583 if (ret
== -EPROTO
) {
584 skb
->transport_header
= savethdr
;
590 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
594 if (fq
->q
.flags
== (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
595 fq
->q
.meat
== fq
->q
.len
&&
596 nf_ct_frag6_reasm(fq
, skb
, dev
))
602 spin_unlock_bh(&fq
->q
.lock
);
603 inet_frag_put(&fq
->q
);
606 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather
);
608 static int nf_ct_net_init(struct net
*net
)
612 net
->nf_frag
.frags
.high_thresh
= IPV6_FRAG_HIGH_THRESH
;
613 net
->nf_frag
.frags
.low_thresh
= IPV6_FRAG_LOW_THRESH
;
614 net
->nf_frag
.frags
.timeout
= IPV6_FRAG_TIMEOUT
;
615 net
->nf_frag
.frags
.f
= &nf_frags
;
617 res
= inet_frags_init_net(&net
->nf_frag
.frags
);
620 res
= nf_ct_frag6_sysctl_register(net
);
622 inet_frags_exit_net(&net
->nf_frag
.frags
);
626 static void nf_ct_net_exit(struct net
*net
)
628 nf_ct_frags6_sysctl_unregister(net
);
629 inet_frags_exit_net(&net
->nf_frag
.frags
);
632 static struct pernet_operations nf_ct_net_ops
= {
633 .init
= nf_ct_net_init
,
634 .exit
= nf_ct_net_exit
,
637 int nf_ct_frag6_init(void)
641 nf_frags
.constructor
= ip6_frag_init
;
642 nf_frags
.destructor
= NULL
;
643 nf_frags
.qsize
= sizeof(struct frag_queue
);
644 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
645 nf_frags
.frags_cache_name
= nf_frags_cache_name
;
646 nf_frags
.rhash_params
= ip6_rhash_params
;
647 ret
= inet_frags_init(&nf_frags
);
650 ret
= register_pernet_subsys(&nf_ct_net_ops
);
652 inet_frags_fini(&nf_frags
);
658 void nf_ct_frag6_cleanup(void)
660 unregister_pernet_subsys(&nf_ct_net_ops
);
661 inet_frags_fini(&nf_frags
);