2 * Backported from upstream commit 5b490047240f
3 * ("ipv6: Export nf_ct_frag6_gather()")
5 * IPv6 fragment reassembly for connection tracking
7 * Copyright (C)2004 USAGI/WIDE Project
10 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
12 * Based on: net/ipv6/reassembly.c
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
20 #define pr_fmt(fmt) "IPv6-nf: " fmt
22 #include <linux/version.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/jiffies.h>
30 #include <linux/net.h>
31 #include <linux/list.h>
32 #include <linux/netdevice.h>
33 #include <linux/in6.h>
34 #include <linux/ipv6.h>
35 #include <linux/icmpv6.h>
36 #include <linux/random.h>
37 #include <linux/slab.h>
41 #include <net/inet_frag.h>
44 #include <net/ipv6_frag.h>
45 #include <net/protocol.h>
46 #include <net/transp_v6.h>
47 #include <net/rawv6.h>
48 #include <net/ndisc.h>
49 #include <net/addrconf.h>
50 #include <net/inet_ecn.h>
51 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
52 #include <linux/netfilter.h>
53 #include <linux/netfilter_ipv6.h>
54 #include <linux/kernel.h>
55 #include <linux/module.h>
56 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
57 #include <net/netns/generic.h>
60 #if defined(HAVE_INET_FRAGS_WITH_FRAGS_WORK) || !defined(HAVE_INET_FRAGS_RND)
62 static const char nf_frags_cache_name
[] = "ovs-frag6";
66 #ifdef OVS_NF_DEFRAG6_BACKPORT
67 struct nf_ct_frag6_skb_cb
69 struct inet6_skb_parm h
;
73 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
75 static struct inet_frags nf_frags
;
77 static struct netns_frags
*get_netns_frags6_from_net(struct net
*net
)
79 #ifdef HAVE_INET_FRAG_LRU_MOVE
80 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
81 return &(ovs_net
->nf_frags
);
83 return &(net
->nf_frag
.frags
);
87 static struct net
*get_net_from_netns_frags6(struct netns_frags
*frags
)
90 #ifdef HAVE_INET_FRAG_LRU_MOVE
91 struct ovs_net
*ovs_net
;
93 ovs_net
= container_of(frags
, struct ovs_net
, nf_frags
);
96 net
= container_of(frags
, struct net
, nf_frag
.frags
);
101 static inline u8
ip6_frag_ecn(const struct ipv6hdr
*ipv6h
)
103 return 1 << (ipv6_get_dsfield(ipv6h
) & INET_ECN_MASK
);
106 #ifdef HAVE_INET_FRAGS_RND
107 static unsigned int nf_hash_frag(__be32 id
, const struct in6_addr
*saddr
,
108 const struct in6_addr
*daddr
)
110 net_get_random_once(&nf_frags
.rnd
, sizeof(nf_frags
.rnd
));
111 return jhash_3words(ipv6_addr_hash(saddr
), ipv6_addr_hash(daddr
),
112 (__force u32
)id
, nf_frags
.rnd
);
114 /* fb3cfe6e75b9 ("inet: frag: remove hash size assumptions from callers")
115 * shifted this logic into inet_fragment, but prior kernels still need this.
117 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,17,0)
118 #define nf_hash_frag(a, b, c) (nf_hash_frag(a, b, c) & (INETFRAGS_HASHSZ - 1))
121 #ifdef HAVE_INET_FRAGS_CONST
122 static unsigned int nf_hashfn(const struct inet_frag_queue
*q
)
124 static unsigned int nf_hashfn(struct inet_frag_queue
*q
)
127 const struct frag_queue
*nq
;
129 nq
= container_of(q
, struct frag_queue
, q
);
130 return nf_hash_frag(nq
->id
, &nq
->saddr
, &nq
->daddr
);
133 #endif /* HAVE_INET_FRAGS_RND */
134 static void nf_ct_frag6_expire(unsigned long data
)
136 struct frag_queue
*fq
;
139 fq
= container_of((struct inet_frag_queue
*)data
, struct frag_queue
, q
);
140 net
= get_net_from_netns_frags6(fq
->q
.net
);
142 #ifdef HAVE_INET_FRAGS_RND
143 ip6_expire_frag_queue(net
, fq
, &nf_frags
);
145 #ifdef HAVE_IPV6_FRAG_H
146 ip6frag_expire_frag_queue(net
, fq
);
148 ip6_expire_frag_queue(net
, fq
);
153 #ifdef HAVE_INET_FRAGS_RND
154 /* Creation primitives. */
155 static inline struct frag_queue
*fq_find(struct net
*net
, __be32 id
,
156 u32 user
, struct in6_addr
*src
,
157 struct in6_addr
*dst
, u8 ecn
)
159 struct inet_frag_queue
*q
;
160 struct ip6_create_arg arg
;
162 struct netns_frags
*frags
;
170 #ifdef HAVE_INET_FRAGS_WITH_RWLOCK
171 read_lock_bh(&nf_frags
.lock
);
175 hash
= nf_hash_frag(id
, src
, dst
);
177 frags
= get_netns_frags6_from_net(net
);
178 q
= inet_frag_find(frags
, &nf_frags
, &arg
, hash
);
180 if (IS_ERR_OR_NULL(q
)) {
181 inet_frag_maybe_warn_overflow(q
, pr_fmt());
184 return container_of(q
, struct frag_queue
, q
);
187 static struct frag_queue
*fq_find(struct net
*net
, __be32 id
, u32 user
,
188 const struct ipv6hdr
*hdr
, int iif
)
190 struct frag_v6_compare_key key
= {
197 struct inet_frag_queue
*q
;
199 q
= inet_frag_find(&net
->nf_frag
.frags
, &key
);
203 return container_of(q
, struct frag_queue
, q
);
206 #endif /* HAVE_INET_FRAGS_RND */
208 static int nf_ct_frag6_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
209 const struct frag_hdr
*fhdr
, int nhoff
)
211 struct sk_buff
*prev
, *next
;
212 unsigned int payload_len
;
216 if (qp_flags(fq
) & INET_FRAG_COMPLETE
) {
217 pr_debug("Already completed\n");
221 payload_len
= ntohs(ipv6_hdr(skb
)->payload_len
);
223 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
224 end
= offset
+ (payload_len
-
225 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
227 if ((unsigned int)end
> IPV6_MAXPLEN
) {
228 pr_debug("offset is too large.\n");
232 ecn
= ip6_frag_ecn(ipv6_hdr(skb
));
234 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
235 const unsigned char *nh
= skb_network_header(skb
);
236 skb
->csum
= csum_sub(skb
->csum
,
237 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
241 /* Is this the final fragment? */
242 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
243 /* If we already have some bits beyond end
244 * or have different end, the segment is corrupted.
246 if (end
< fq
->q
.len
||
247 ((qp_flags(fq
) & INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
248 pr_debug("already received last fragment\n");
251 qp_flags(fq
) |= INET_FRAG_LAST_IN
;
254 /* Check if the fragment is rounded to 8 bytes.
255 * Required by the RFC.
258 /* RFC2460 says always send parameter problem in
261 pr_debug("end of fragment not rounded to 8 bytes.\n");
264 if (end
> fq
->q
.len
) {
265 /* Some bits beyond end -> corruption. */
266 if (qp_flags(fq
) & INET_FRAG_LAST_IN
) {
267 pr_debug("last packet already reached.\n");
277 /* Point into the IP datagram 'data' part. */
278 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
)) {
279 pr_debug("queue: message is too short.\n");
282 if (pskb_trim_rcsum(skb
, end
- offset
)) {
283 pr_debug("Can't trim\n");
287 /* Find out which fragments are in front and at the back of us
288 * in the chain of fragments so far. We must know where to put
289 * this fragment, right?
291 prev
= fq
->q
.fragments_tail
;
292 if (!prev
|| NFCT_FRAG6_CB(prev
)->offset
< offset
) {
297 for (next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
298 if (NFCT_FRAG6_CB(next
)->offset
>= offset
)
304 /* RFC5722, Section 4:
305 * When reassembling an IPv6 datagram, if
306 * one or more its constituent fragments is determined to be an
307 * overlapping fragment, the entire datagram (and any constituent
308 * fragments, including those not yet received) MUST be silently
312 /* Check for overlap with preceding fragment. */
314 (NFCT_FRAG6_CB(prev
)->offset
+ prev
->len
) > offset
)
317 /* Look for overlap with succeeding segment. */
318 if (next
&& NFCT_FRAG6_CB(next
)->offset
< end
)
321 NFCT_FRAG6_CB(skb
)->offset
= offset
;
323 /* Insert this fragment in the chain of fragments. */
326 fq
->q
.fragments_tail
= skb
;
330 fq
->q
.fragments
= skb
;
333 fq
->iif
= skb
->dev
->ifindex
;
336 fq
->q
.stamp
= skb
->tstamp
;
337 fq
->q
.meat
+= skb
->len
;
339 if (payload_len
> fq
->q
.max_size
)
340 fq
->q
.max_size
= payload_len
;
341 add_frag_mem_limit(fq
->q
.net
, skb
->truesize
);
343 /* The first fragment.
344 * nhoffset is obtained from the first fragment, of course.
347 fq
->nhoffset
= nhoff
;
348 qp_flags(fq
) |= INET_FRAG_FIRST_IN
;
351 inet_frag_lru_move(&fq
->q
);
355 #ifdef HAVE_INET_FRAGS_RND
356 inet_frag_kill(&fq
->q
, &nf_frags
);
358 inet_frag_kill(&fq
->q
);
365 * Check if this packet is complete.
367 * It is called with locked fq, and caller must check that
368 * queue is eligible for reassembly i.e. it is not COMPLETE,
369 * the last and the first frames arrived and all the bits are here.
371 * returns true if *prev skb has been transformed into the reassembled
372 * skb, false otherwise.
375 nf_ct_frag6_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
, struct net_device
*dev
)
377 struct sk_buff
*fp
, *head
= fq
->q
.fragments
;
381 #ifdef HAVE_INET_FRAGS_RND
382 inet_frag_kill(&fq
->q
, &nf_frags
);
384 inet_frag_kill(&fq
->q
);
387 WARN_ON(head
== NULL
);
388 WARN_ON(NFCT_FRAG6_CB(head
)->offset
!= 0);
390 ecn
= ip_frag_ecn_table
[fq
->ecn
];
391 if (unlikely(ecn
== 0xff))
394 /* Unfragmented part is taken from the first segment. */
395 payload_len
= ((head
->data
- skb_network_header(head
)) -
396 sizeof(struct ipv6hdr
) + fq
->q
.len
-
397 sizeof(struct frag_hdr
));
398 if (payload_len
> IPV6_MAXPLEN
) {
399 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
404 /* Head of list must not be cloned. */
405 if (skb_unclone(head
, GFP_ATOMIC
))
408 /* If the first fragment is fragmented itself, we split
409 * it to two chunks: the first with data and paged part
410 * and the second, holding only fragments. */
411 if (skb_has_frag_list(head
)) {
412 struct sk_buff
*clone
;
415 clone
= alloc_skb(0, GFP_ATOMIC
);
419 clone
->next
= head
->next
;
421 skb_shinfo(clone
)->frag_list
= skb_shinfo(head
)->frag_list
;
422 skb_frag_list_init(head
);
423 for (i
= 0; i
< skb_shinfo(head
)->nr_frags
; i
++)
424 plen
+= skb_frag_size(&skb_shinfo(head
)->frags
[i
]);
425 clone
->len
= clone
->data_len
= head
->data_len
- plen
;
426 head
->data_len
-= clone
->len
;
427 head
->len
-= clone
->len
;
429 clone
->ip_summed
= head
->ip_summed
;
431 add_frag_mem_limit(fq
->q
.net
, clone
->truesize
);
434 /* morph head into last received skb: prev.
436 * This allows callers of ipv6 conntrack defrag to continue
437 * to use the last skb(frag) passed into the reasm engine.
438 * The last skb frag 'silently' turns into the full reassembled skb.
440 * Since prev is also part of q->fragments we have to clone it first.
443 struct sk_buff
*iter
;
445 fp
= skb_clone(prev
, GFP_ATOMIC
);
449 fp
->next
= prev
->next
;
453 if (iter
->next
== prev
) {
460 skb_morph(prev
, head
);
461 prev
->next
= head
->next
;
466 /* We have to remove fragment header from datagram and to relocate
467 * header in order to calculate ICV correctly. */
468 skb_network_header(head
)[fq
->nhoffset
] = skb_transport_header(head
)[0];
469 memmove(head
->head
+ sizeof(struct frag_hdr
), head
->head
,
470 (head
->data
- head
->head
) - sizeof(struct frag_hdr
));
471 head
->mac_header
+= sizeof(struct frag_hdr
);
472 head
->network_header
+= sizeof(struct frag_hdr
);
474 skb_shinfo(head
)->frag_list
= head
->next
;
475 skb_reset_transport_header(head
);
476 skb_push(head
, head
->data
- skb_network_header(head
));
478 for (fp
=head
->next
; fp
; fp
= fp
->next
) {
479 head
->data_len
+= fp
->len
;
480 head
->len
+= fp
->len
;
481 if (head
->ip_summed
!= fp
->ip_summed
)
482 head
->ip_summed
= CHECKSUM_NONE
;
483 else if (head
->ip_summed
== CHECKSUM_COMPLETE
)
484 head
->csum
= csum_add(head
->csum
, fp
->csum
);
485 head
->truesize
+= fp
->truesize
;
487 sub_frag_mem_limit(fq
->q
.net
, head
->truesize
);
492 head
->tstamp
= fq
->q
.stamp
;
493 ipv6_hdr(head
)->payload_len
= htons(payload_len
);
494 ipv6_change_dsfield(ipv6_hdr(head
), 0xff, ecn
);
495 IP6CB(head
)->frag_max_size
= sizeof(struct ipv6hdr
) + fq
->q
.max_size
;
497 /* Yes, and fold redundant checksum back. 8) */
498 if (head
->ip_summed
== CHECKSUM_COMPLETE
)
499 head
->csum
= csum_partial(skb_network_header(head
),
500 skb_network_header_len(head
),
503 fq
->q
.fragments
= NULL
;
504 fq
->q
.fragments_tail
= NULL
;
510 * find the header just before Fragment Header.
512 * if success return 0 and set ...
513 * (*prevhdrp): the value of "Next Header Field" in the header
514 * just before Fragment Header.
515 * (*prevhoff): the offset of "Next Header Field" in the header
516 * just before Fragment Header.
517 * (*fhoff) : the offset of Fragment Header.
519 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
523 find_prev_fhdr(struct sk_buff
*skb
, u8
*prevhdrp
, int *prevhoff
, int *fhoff
)
525 u8 nexthdr
= ipv6_hdr(skb
)->nexthdr
;
526 const int netoff
= skb_network_offset(skb
);
527 u8 prev_nhoff
= netoff
+ offsetof(struct ipv6hdr
, nexthdr
);
528 int start
= netoff
+ sizeof(struct ipv6hdr
);
529 int len
= skb
->len
- start
;
530 u8 prevhdr
= NEXTHDR_IPV6
;
532 while (nexthdr
!= NEXTHDR_FRAGMENT
) {
533 struct ipv6_opt_hdr hdr
;
536 if (!ipv6_ext_hdr(nexthdr
)) {
539 if (nexthdr
== NEXTHDR_NONE
) {
540 pr_debug("next header is none\n");
543 if (len
< (int)sizeof(struct ipv6_opt_hdr
)) {
544 pr_debug("too short\n");
547 if (skb_copy_bits(skb
, start
, &hdr
, sizeof(hdr
)))
549 if (nexthdr
== NEXTHDR_AUTH
)
550 hdrlen
= (hdr
.hdrlen
+2)<<2;
552 hdrlen
= ipv6_optlen(&hdr
);
557 nexthdr
= hdr
.nexthdr
;
566 *prevhoff
= prev_nhoff
;
572 int rpl_nf_ct_frag6_gather(struct net
*net
, struct sk_buff
*skb
, u32 user
)
574 struct net_device
*dev
= skb
->dev
;
575 int fhoff
, nhoff
, ret
;
576 struct frag_hdr
*fhdr
;
577 struct frag_queue
*fq
;
580 struct netns_frags
*frags
;
582 /* Jumbo payload inhibits frag. header */
583 if (ipv6_hdr(skb
)->payload_len
== 0) {
584 pr_debug("payload len = 0\n");
588 if (find_prev_fhdr(skb
, &prevhdr
, &nhoff
, &fhoff
) < 0)
591 if (!pskb_may_pull(skb
, fhoff
+ sizeof(*fhdr
)))
594 skb_set_transport_header(skb
, fhoff
);
596 fhdr
= (struct frag_hdr
*)skb_transport_header(skb
);
598 /* See ip_evictor(). */
599 frags
= get_netns_frags6_from_net(net
);
600 #ifdef HAVE_INET_FRAG_EVICTOR
602 inet_frag_evictor(frags
, &nf_frags
, false);
607 #ifdef HAVE_INET_FRAGS_RND
608 fq
= fq_find(net
, fhdr
->identification
, user
, &hdr
->saddr
, &hdr
->daddr
,
611 fq
= fq_find(net
, fhdr
->identification
, user
, hdr
,
612 skb
->dev
? skb
->dev
->ifindex
: 0);
617 spin_lock_bh(&fq
->q
.lock
);
619 if (nf_ct_frag6_queue(fq
, skb
, fhdr
, nhoff
) < 0) {
624 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
628 if (qp_flags(fq
) == (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
629 fq
->q
.meat
== fq
->q
.len
&&
630 nf_ct_frag6_reasm(fq
, skb
, dev
))
634 spin_unlock_bh(&fq
->q
.lock
);
635 #ifdef HAVE_INET_FRAGS_RND
636 inet_frag_put(&fq
->q
, &nf_frags
);
638 inet_frag_put(&fq
->q
);
643 #ifdef HAVE_DEFRAG_ENABLE_TAKES_NET
644 static int nf_ct_net_init(struct net
*net
)
646 return nf_defrag_ipv6_enable(net
);
650 static void nf_ct_net_exit(struct net
*net
)
654 void ovs_netns_frags6_init(struct net
*net
)
656 #ifdef HAVE_INET_FRAG_LRU_MOVE
657 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
659 ovs_net
->nf_frags
.high_thresh
= IPV6_FRAG_HIGH_THRESH
;
660 ovs_net
->nf_frags
.low_thresh
= IPV6_FRAG_LOW_THRESH
;
661 ovs_net
->nf_frags
.timeout
= IPV6_FRAG_TIMEOUT
;
663 inet_frags_init_net(&(ovs_net
->nf_frags
));
667 void ovs_netns_frags6_exit(struct net
*net
)
669 #ifdef HAVE_INET_FRAGS_RND
670 struct netns_frags
*frags
;
672 frags
= get_netns_frags6_from_net(net
);
673 inet_frags_exit_net(frags
, &nf_frags
);
677 static struct pernet_operations nf_ct_net_ops
= {
678 #ifdef HAVE_DEFRAG_ENABLE_TAKES_NET
679 .init
= nf_ct_net_init
,
681 .exit
= nf_ct_net_exit
,
684 #ifdef HAVE_IPV6_FRAG_H
685 static const struct rhashtable_params nfct_rhash_params
= {
686 .head_offset
= offsetof(struct inet_frag_queue
, node
),
687 .hashfn
= ip6frag_key_hashfn
,
688 .obj_hashfn
= ip6frag_obj_hashfn
,
689 .obj_cmpfn
= ip6frag_obj_cmpfn
,
690 .automatic_shrinking
= true,
694 int rpl_nf_ct_frag6_init(void)
698 #ifndef HAVE_DEFRAG_ENABLE_TAKES_NET
699 nf_defrag_ipv6_enable();
701 #ifdef HAVE_INET_FRAGS_RND
702 nf_frags
.hashfn
= nf_hashfn
;
703 nf_frags
.match
= ip6_frag_match
;
704 nf_frags
.constructor
= ip6_frag_init
;
706 #ifdef HAVE_IPV6_FRAG_H
707 nf_frags
.rhash_params
= nfct_rhash_params
;
708 nf_frags
.constructor
= ip6frag_init
;
710 nf_frags
.rhash_params
= ip6_rhash_params
;
711 nf_frags
.constructor
= ip6_frag_init
;
713 #endif /* HAVE_INET_FRAGS_RND */
714 nf_frags
.destructor
= NULL
;
715 nf_frags
.qsize
= sizeof(struct frag_queue
);
716 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
717 #if defined(HAVE_INET_FRAGS_WITH_FRAGS_WORK) || !defined(HAVE_INET_FRAGS_RND)
718 nf_frags
.frags_cache_name
= nf_frags_cache_name
;
720 #if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(8,0)
721 nf_frags
.secret_interval
= 10 * 60 * HZ
;
723 ret
= inet_frags_init(&nf_frags
);
726 ret
= register_pernet_subsys(&nf_ct_net_ops
);
728 inet_frags_fini(&nf_frags
);
734 void rpl_nf_ct_frag6_cleanup(void)
736 unregister_pernet_subsys(&nf_ct_net_ops
);
737 inet_frags_fini(&nf_frags
);
740 #endif /* OVS_NF_DEFRAG6_BACKPORT */