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 #ifdef OVS_NF_DEFRAG6_BACKPORT
62 static const char nf_frags_cache_name
[] = "ovs-frag6";
64 struct nf_ct_frag6_skb_cb
66 struct inet6_skb_parm h
;
70 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
72 static struct inet_frags nf_frags
;
74 static struct netns_frags
*get_netns_frags6_from_net(struct net
*net
)
76 #ifdef HAVE_INET_FRAG_LRU_MOVE
77 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
78 return &(ovs_net
->nf_frags
);
80 return &(net
->nf_frag
.frags
);
84 static struct net
*get_net_from_netns_frags6(struct netns_frags
*frags
)
87 #ifdef HAVE_INET_FRAG_LRU_MOVE
88 struct ovs_net
*ovs_net
;
90 ovs_net
= container_of(frags
, struct ovs_net
, nf_frags
);
93 net
= container_of(frags
, struct net
, nf_frag
.frags
);
98 static inline u8
ip6_frag_ecn(const struct ipv6hdr
*ipv6h
)
100 return 1 << (ipv6_get_dsfield(ipv6h
) & INET_ECN_MASK
);
103 #ifdef HAVE_INET_FRAGS_RND
104 static unsigned int nf_hash_frag(__be32 id
, const struct in6_addr
*saddr
,
105 const struct in6_addr
*daddr
)
107 net_get_random_once(&nf_frags
.rnd
, sizeof(nf_frags
.rnd
));
108 return jhash_3words(ipv6_addr_hash(saddr
), ipv6_addr_hash(daddr
),
109 (__force u32
)id
, nf_frags
.rnd
);
111 /* fb3cfe6e75b9 ("inet: frag: remove hash size assumptions from callers")
112 * shifted this logic into inet_fragment, but prior kernels still need this.
114 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,17,0)
115 #define nf_hash_frag(a, b, c) (nf_hash_frag(a, b, c) & (INETFRAGS_HASHSZ - 1))
118 #ifdef HAVE_INET_FRAGS_CONST
119 static unsigned int nf_hashfn(const struct inet_frag_queue
*q
)
121 static unsigned int nf_hashfn(struct inet_frag_queue
*q
)
124 const struct frag_queue
*nq
;
126 nq
= container_of(q
, struct frag_queue
, q
);
127 return nf_hash_frag(nq
->id
, &nq
->saddr
, &nq
->daddr
);
130 #endif /* HAVE_INET_FRAGS_RND */
131 static void nf_ct_frag6_expire(unsigned long data
)
133 struct frag_queue
*fq
;
136 fq
= container_of((struct inet_frag_queue
*)data
, struct frag_queue
, q
);
137 net
= get_net_from_netns_frags6(fq
->q
.net
);
139 #ifdef HAVE_INET_FRAGS_RND
140 ip6_expire_frag_queue(net
, fq
, &nf_frags
);
142 #ifdef HAVE_IPV6_FRAG_H
143 ip6frag_expire_frag_queue(net
, fq
);
145 ip6_expire_frag_queue(net
, fq
);
150 #ifdef HAVE_INET_FRAGS_RND
151 /* Creation primitives. */
152 static inline struct frag_queue
*fq_find(struct net
*net
, __be32 id
,
153 u32 user
, struct in6_addr
*src
,
154 struct in6_addr
*dst
, u8 ecn
)
156 struct inet_frag_queue
*q
;
157 struct ip6_create_arg arg
;
159 struct netns_frags
*frags
;
167 #ifdef HAVE_INET_FRAGS_WITH_RWLOCK
168 read_lock_bh(&nf_frags
.lock
);
172 hash
= nf_hash_frag(id
, src
, dst
);
174 frags
= get_netns_frags6_from_net(net
);
175 q
= inet_frag_find(frags
, &nf_frags
, &arg
, hash
);
177 if (IS_ERR_OR_NULL(q
)) {
178 inet_frag_maybe_warn_overflow(q
, pr_fmt());
181 return container_of(q
, struct frag_queue
, q
);
184 static struct frag_queue
*fq_find(struct net
*net
, __be32 id
, u32 user
,
185 const struct ipv6hdr
*hdr
, int iif
)
187 struct frag_v6_compare_key key
= {
194 struct inet_frag_queue
*q
;
196 q
= inet_frag_find(&net
->nf_frag
.frags
, &key
);
200 return container_of(q
, struct frag_queue
, q
);
203 #endif /* HAVE_INET_FRAGS_RND */
205 static int nf_ct_frag6_queue(struct frag_queue
*fq
, struct sk_buff
*skb
,
206 const struct frag_hdr
*fhdr
, int nhoff
)
208 struct sk_buff
*prev
, *next
;
209 unsigned int payload_len
;
213 if (qp_flags(fq
) & INET_FRAG_COMPLETE
) {
214 pr_debug("Already completed\n");
218 payload_len
= ntohs(ipv6_hdr(skb
)->payload_len
);
220 offset
= ntohs(fhdr
->frag_off
) & ~0x7;
221 end
= offset
+ (payload_len
-
222 ((u8
*)(fhdr
+ 1) - (u8
*)(ipv6_hdr(skb
) + 1)));
224 if ((unsigned int)end
> IPV6_MAXPLEN
) {
225 pr_debug("offset is too large.\n");
229 ecn
= ip6_frag_ecn(ipv6_hdr(skb
));
231 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
232 const unsigned char *nh
= skb_network_header(skb
);
233 skb
->csum
= csum_sub(skb
->csum
,
234 csum_partial(nh
, (u8
*)(fhdr
+ 1) - nh
,
238 /* Is this the final fragment? */
239 if (!(fhdr
->frag_off
& htons(IP6_MF
))) {
240 /* If we already have some bits beyond end
241 * or have different end, the segment is corrupted.
243 if (end
< fq
->q
.len
||
244 ((qp_flags(fq
) & INET_FRAG_LAST_IN
) && end
!= fq
->q
.len
)) {
245 pr_debug("already received last fragment\n");
248 qp_flags(fq
) |= INET_FRAG_LAST_IN
;
251 /* Check if the fragment is rounded to 8 bytes.
252 * Required by the RFC.
255 /* RFC2460 says always send parameter problem in
258 pr_debug("end of fragment not rounded to 8 bytes.\n");
261 if (end
> fq
->q
.len
) {
262 /* Some bits beyond end -> corruption. */
263 if (qp_flags(fq
) & INET_FRAG_LAST_IN
) {
264 pr_debug("last packet already reached.\n");
274 /* Point into the IP datagram 'data' part. */
275 if (!pskb_pull(skb
, (u8
*) (fhdr
+ 1) - skb
->data
)) {
276 pr_debug("queue: message is too short.\n");
279 if (pskb_trim_rcsum(skb
, end
- offset
)) {
280 pr_debug("Can't trim\n");
284 /* Find out which fragments are in front and at the back of us
285 * in the chain of fragments so far. We must know where to put
286 * this fragment, right?
288 prev
= fq
->q
.fragments_tail
;
289 if (!prev
|| NFCT_FRAG6_CB(prev
)->offset
< offset
) {
294 for (next
= fq
->q
.fragments
; next
!= NULL
; next
= next
->next
) {
295 if (NFCT_FRAG6_CB(next
)->offset
>= offset
)
301 /* RFC5722, Section 4:
302 * When reassembling an IPv6 datagram, if
303 * one or more its constituent fragments is determined to be an
304 * overlapping fragment, the entire datagram (and any constituent
305 * fragments, including those not yet received) MUST be silently
309 /* Check for overlap with preceding fragment. */
311 (NFCT_FRAG6_CB(prev
)->offset
+ prev
->len
) > offset
)
314 /* Look for overlap with succeeding segment. */
315 if (next
&& NFCT_FRAG6_CB(next
)->offset
< end
)
318 NFCT_FRAG6_CB(skb
)->offset
= offset
;
320 /* Insert this fragment in the chain of fragments. */
323 fq
->q
.fragments_tail
= skb
;
327 fq
->q
.fragments
= skb
;
330 fq
->iif
= skb
->dev
->ifindex
;
333 fq
->q
.stamp
= skb
->tstamp
;
334 fq
->q
.meat
+= skb
->len
;
336 if (payload_len
> fq
->q
.max_size
)
337 fq
->q
.max_size
= payload_len
;
338 add_frag_mem_limit(fq
->q
.net
, skb
->truesize
);
340 /* The first fragment.
341 * nhoffset is obtained from the first fragment, of course.
344 fq
->nhoffset
= nhoff
;
345 qp_flags(fq
) |= INET_FRAG_FIRST_IN
;
348 inet_frag_lru_move(&fq
->q
);
352 #ifdef HAVE_INET_FRAGS_RND
353 inet_frag_kill(&fq
->q
, &nf_frags
);
355 inet_frag_kill(&fq
->q
);
362 * Check if this packet is complete.
364 * It is called with locked fq, and caller must check that
365 * queue is eligible for reassembly i.e. it is not COMPLETE,
366 * the last and the first frames arrived and all the bits are here.
368 * returns true if *prev skb has been transformed into the reassembled
369 * skb, false otherwise.
372 nf_ct_frag6_reasm(struct frag_queue
*fq
, struct sk_buff
*prev
, struct net_device
*dev
)
374 struct sk_buff
*fp
, *head
= fq
->q
.fragments
;
378 #ifdef HAVE_INET_FRAGS_RND
379 inet_frag_kill(&fq
->q
, &nf_frags
);
381 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 rpl_nf_ct_frag6_gather(struct net
*net
, struct sk_buff
*skb
, u32 user
)
571 struct net_device
*dev
= skb
->dev
;
572 int fhoff
, nhoff
, ret
;
573 struct frag_hdr
*fhdr
;
574 struct frag_queue
*fq
;
577 struct netns_frags
*frags
;
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
);
595 /* See ip_evictor(). */
596 frags
= get_netns_frags6_from_net(net
);
597 #ifdef HAVE_INET_FRAG_EVICTOR
599 inet_frag_evictor(frags
, &nf_frags
, false);
604 #ifdef HAVE_INET_FRAGS_RND
605 fq
= fq_find(net
, fhdr
->identification
, user
, &hdr
->saddr
, &hdr
->daddr
,
608 fq
= fq_find(net
, fhdr
->identification
, user
, hdr
,
609 skb
->dev
? skb
->dev
->ifindex
: 0);
614 spin_lock_bh(&fq
->q
.lock
);
616 if (nf_ct_frag6_queue(fq
, skb
, fhdr
, nhoff
) < 0) {
621 /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
625 if (qp_flags(fq
) == (INET_FRAG_FIRST_IN
| INET_FRAG_LAST_IN
) &&
626 fq
->q
.meat
== fq
->q
.len
&&
627 nf_ct_frag6_reasm(fq
, skb
, dev
))
631 spin_unlock_bh(&fq
->q
.lock
);
632 #ifdef HAVE_INET_FRAGS_RND
633 inet_frag_put(&fq
->q
, &nf_frags
);
635 inet_frag_put(&fq
->q
);
640 #ifdef HAVE_DEFRAG_ENABLE_TAKES_NET
641 static int nf_ct_net_init(struct net
*net
)
643 return nf_defrag_ipv6_enable(net
);
647 static void nf_ct_net_exit(struct net
*net
)
651 void ovs_netns_frags6_init(struct net
*net
)
653 #ifdef HAVE_INET_FRAG_LRU_MOVE
654 struct ovs_net
*ovs_net
= net_generic(net
, ovs_net_id
);
656 ovs_net
->nf_frags
.high_thresh
= IPV6_FRAG_HIGH_THRESH
;
657 ovs_net
->nf_frags
.low_thresh
= IPV6_FRAG_LOW_THRESH
;
658 ovs_net
->nf_frags
.timeout
= IPV6_FRAG_TIMEOUT
;
660 inet_frags_init_net(&(ovs_net
->nf_frags
));
664 void ovs_netns_frags6_exit(struct net
*net
)
666 #ifdef HAVE_INET_FRAGS_RND
667 struct netns_frags
*frags
;
669 frags
= get_netns_frags6_from_net(net
);
670 inet_frags_exit_net(frags
, &nf_frags
);
674 static struct pernet_operations nf_ct_net_ops
= {
675 #ifdef HAVE_DEFRAG_ENABLE_TAKES_NET
676 .init
= nf_ct_net_init
,
678 .exit
= nf_ct_net_exit
,
681 #ifdef HAVE_IPV6_FRAG_H
682 static const struct rhashtable_params nfct_rhash_params
= {
683 .head_offset
= offsetof(struct inet_frag_queue
, node
),
684 .hashfn
= ip6frag_key_hashfn
,
685 .obj_hashfn
= ip6frag_obj_hashfn
,
686 .obj_cmpfn
= ip6frag_obj_cmpfn
,
687 .automatic_shrinking
= true,
691 int rpl_nf_ct_frag6_init(void)
695 #ifndef HAVE_DEFRAG_ENABLE_TAKES_NET
696 nf_defrag_ipv6_enable();
698 #ifdef HAVE_INET_FRAGS_RND
699 nf_frags
.hashfn
= nf_hashfn
;
700 nf_frags
.match
= ip6_frag_match
;
701 nf_frags
.constructor
= ip6_frag_init
;
703 #ifdef HAVE_IPV6_FRAG_H
704 nf_frags
.rhash_params
= nfct_rhash_params
;
705 nf_frags
.constructor
= ip6frag_init
;
707 nf_frags
.rhash_params
= ip6_rhash_params
;
708 nf_frags
.constructor
= ip6_frag_init
;
710 #endif /* HAVE_INET_FRAGS_RND */
711 nf_frags
.destructor
= NULL
;
712 nf_frags
.qsize
= sizeof(struct frag_queue
);
713 nf_frags
.frag_expire
= nf_ct_frag6_expire
;
714 #if defined(HAVE_INET_FRAGS_WITH_FRAGS_WORK) || !defined(HAVE_INET_FRAGS_RND)
715 nf_frags
.frags_cache_name
= nf_frags_cache_name
;
717 #if RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(8,0)
718 nf_frags
.secret_interval
= 10 * 60 * HZ
;
720 ret
= inet_frags_init(&nf_frags
);
723 ret
= register_pernet_subsys(&nf_ct_net_ops
);
725 inet_frags_fini(&nf_frags
);
731 void rpl_nf_ct_frag6_cleanup(void)
733 unregister_pernet_subsys(&nf_ct_net_ops
);
734 inet_frags_fini(&nf_frags
);
737 #endif /* OVS_NF_DEFRAG6_BACKPORT */