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 #ifdef OVS_FRAGMENT_BACKPORT
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/string.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/jiffies.h>
32 #include <linux/net.h>
33 #include <linux/list.h>
34 #include <linux/netdevice.h>
35 #include <linux/in6.h>
36 #include <linux/ipv6.h>
37 #include <linux/icmpv6.h>
38 #include <linux/random.h>
39 #include <linux/slab.h>
43 #include <net/inet_frag.h>
46 #include <net/protocol.h>
47 #include <net/transp_v6.h>
48 #include <net/rawv6.h>
49 #include <net/ndisc.h>
50 #include <net/addrconf.h>
51 #include <net/inet_ecn.h>
52 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
53 #include <linux/netfilter.h>
54 #include <linux/netfilter_ipv6.h>
55 #include <linux/kernel.h>
56 #include <linux/module.h>
57 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
59 static const char nf_frags_cache_name[] = "nf-frags";
61 struct nf_ct_frag6_skb_cb
63 struct inet6_skb_parm h;
68 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
70 static struct inet_frags nf_frags;
72 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
74 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
77 static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
78 const struct in6_addr *daddr)
80 net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
81 return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
82 (__force u32)id, nf_frags.rnd);
85 #ifdef HAVE_INET_FRAGS_CONST
86 static unsigned int nf_hashfn(const struct inet_frag_queue *q)
88 static unsigned int nf_hashfn(struct inet_frag_queue *q)
91 const struct frag_queue *nq;
93 nq = container_of(q, struct frag_queue, q);
94 return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
97 static void nf_skb_free(struct sk_buff *skb)
99 if (NFCT_FRAG6_CB(skb)->orig)
100 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
103 static void nf_ct_frag6_expire(unsigned long data)
105 struct frag_queue *fq;
108 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
109 net = container_of(fq->q.net, struct net, nf_frag.frags);
111 ip6_expire_frag_queue(net, fq, &nf_frags);
114 /* Creation primitives. */
115 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
116 u32 user, struct in6_addr *src,
117 struct in6_addr *dst, u8 ecn)
119 struct inet_frag_queue *q;
120 struct ip6_create_arg arg;
130 hash = nf_hash_frag(id, src, dst);
132 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
134 if (IS_ERR_OR_NULL(q)) {
135 inet_frag_maybe_warn_overflow(q, pr_fmt());
138 return container_of(q, struct frag_queue, q);
142 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
143 const struct frag_hdr *fhdr, int nhoff)
145 struct sk_buff *prev, *next;
146 unsigned int payload_len;
150 if (qp_flags(fq) & INET_FRAG_COMPLETE) {
151 pr_debug("Already completed\n");
155 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
157 offset = ntohs(fhdr->frag_off) & ~0x7;
158 end = offset + (payload_len -
159 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
161 if ((unsigned int)end > IPV6_MAXPLEN) {
162 pr_debug("offset is too large.\n");
166 ecn = ip6_frag_ecn(ipv6_hdr(skb));
168 if (skb->ip_summed == CHECKSUM_COMPLETE) {
169 const unsigned char *nh = skb_network_header(skb);
170 skb->csum = csum_sub(skb->csum,
171 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
175 /* Is this the final fragment? */
176 if (!(fhdr->frag_off & htons(IP6_MF))) {
177 /* If we already have some bits beyond end
178 * or have different end, the segment is corrupted.
180 if (end < fq->q.len ||
181 ((qp_flags(fq) & INET_FRAG_LAST_IN) && end != fq->q.len)) {
182 pr_debug("already received last fragment\n");
185 qp_flags(fq) |= INET_FRAG_LAST_IN;
188 /* Check if the fragment is rounded to 8 bytes.
189 * Required by the RFC.
192 /* RFC2460 says always send parameter problem in
195 pr_debug("end of fragment not rounded to 8 bytes.\n");
198 if (end > fq->q.len) {
199 /* Some bits beyond end -> corruption. */
200 if (qp_flags(fq) & INET_FRAG_LAST_IN) {
201 pr_debug("last packet already reached.\n");
211 /* Point into the IP datagram 'data' part. */
212 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
213 pr_debug("queue: message is too short.\n");
216 if (pskb_trim_rcsum(skb, end - offset)) {
217 pr_debug("Can't trim\n");
221 /* Find out which fragments are in front and at the back of us
222 * in the chain of fragments so far. We must know where to put
223 * this fragment, right?
225 prev = fq->q.fragments_tail;
226 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
231 for (next = fq->q.fragments; next != NULL; next = next->next) {
232 if (NFCT_FRAG6_CB(next)->offset >= offset)
238 /* RFC5722, Section 4:
239 * When reassembling an IPv6 datagram, if
240 * one or more its constituent fragments is determined to be an
241 * overlapping fragment, the entire datagram (and any constituent
242 * fragments, including those not yet received) MUST be silently
246 /* Check for overlap with preceding fragment. */
248 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
251 /* Look for overlap with succeeding segment. */
252 if (next && NFCT_FRAG6_CB(next)->offset < end)
255 NFCT_FRAG6_CB(skb)->offset = offset;
257 /* Insert this fragment in the chain of fragments. */
260 fq->q.fragments_tail = skb;
264 fq->q.fragments = skb;
267 fq->iif = skb->dev->ifindex;
270 fq->q.stamp = skb->tstamp;
271 fq->q.meat += skb->len;
273 if (payload_len > fq->q.max_size)
274 fq->q.max_size = payload_len;
275 add_frag_mem_limit(fq->q.net, skb->truesize);
277 /* The first fragment.
278 * nhoffset is obtained from the first fragment, of course.
281 fq->nhoffset = nhoff;
282 qp_flags(fq) |= INET_FRAG_FIRST_IN;
288 inet_frag_kill(&fq->q, &nf_frags);
294 * Check if this packet is complete.
295 * Returns NULL on failure by any reason, and pointer
296 * to current nexthdr field in reassembled frame.
298 * It is called with locked fq, and caller must check that
299 * queue is eligible for reassembly i.e. it is not COMPLETE,
300 * the last and the first frames arrived and all the bits are here.
302 static struct sk_buff *
303 nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
305 struct sk_buff *fp, *op, *head = fq->q.fragments;
309 inet_frag_kill(&fq->q, &nf_frags);
311 WARN_ON(head == NULL);
312 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
314 ecn = ip_frag_ecn_table[fq->ecn];
315 if (unlikely(ecn == 0xff))
318 /* Unfragmented part is taken from the first segment. */
319 payload_len = ((head->data - skb_network_header(head)) -
320 sizeof(struct ipv6hdr) + fq->q.len -
321 sizeof(struct frag_hdr));
322 if (payload_len > IPV6_MAXPLEN) {
323 pr_debug("payload len is too large.\n");
327 /* Head of list must not be cloned. */
328 if (skb_unclone(head, GFP_ATOMIC)) {
329 pr_debug("skb is cloned but can't expand head");
333 /* If the first fragment is fragmented itself, we split
334 * it to two chunks: the first with data and paged part
335 * and the second, holding only fragments. */
336 if (skb_has_frag_list(head)) {
337 struct sk_buff *clone;
340 clone = alloc_skb(0, GFP_ATOMIC);
344 clone->next = head->next;
346 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
347 skb_frag_list_init(head);
348 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
349 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
350 clone->len = clone->data_len = head->data_len - plen;
351 head->data_len -= clone->len;
352 head->len -= clone->len;
354 clone->ip_summed = head->ip_summed;
356 NFCT_FRAG6_CB(clone)->orig = NULL;
357 add_frag_mem_limit(fq->q.net, clone->truesize);
360 /* We have to remove fragment header from datagram and to relocate
361 * header in order to calculate ICV correctly. */
362 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
363 memmove(head->head + sizeof(struct frag_hdr), head->head,
364 (head->data - head->head) - sizeof(struct frag_hdr));
365 head->mac_header += sizeof(struct frag_hdr);
366 head->network_header += sizeof(struct frag_hdr);
368 skb_shinfo(head)->frag_list = head->next;
369 skb_reset_transport_header(head);
370 skb_push(head, head->data - skb_network_header(head));
372 for (fp=head->next; fp; fp = fp->next) {
373 head->data_len += fp->len;
374 head->len += fp->len;
375 if (head->ip_summed != fp->ip_summed)
376 head->ip_summed = CHECKSUM_NONE;
377 else if (head->ip_summed == CHECKSUM_COMPLETE)
378 head->csum = csum_add(head->csum, fp->csum);
379 head->truesize += fp->truesize;
381 sub_frag_mem_limit(fq->q.net, head->truesize);
386 head->tstamp = fq->q.stamp;
387 ipv6_hdr(head)->payload_len = htons(payload_len);
388 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
389 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
391 /* Yes, and fold redundant checksum back. 8) */
392 if (head->ip_summed == CHECKSUM_COMPLETE)
393 head->csum = csum_partial(skb_network_header(head),
394 skb_network_header_len(head),
397 fq->q.fragments = NULL;
398 fq->q.fragments_tail = NULL;
400 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
401 fp = skb_shinfo(head)->frag_list;
402 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
403 /* at above code, head skb is divided into two skbs. */
406 op = NFCT_FRAG6_CB(head)->orig;
407 for (; fp; fp = fp->next) {
408 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
412 NFCT_FRAG6_CB(fp)->orig = NULL;
418 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
422 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
428 * find the header just before Fragment Header.
430 * if success return 0 and set ...
431 * (*prevhdrp): the value of "Next Header Field" in the header
432 * just before Fragment Header.
433 * (*prevhoff): the offset of "Next Header Field" in the header
434 * just before Fragment Header.
435 * (*fhoff) : the offset of Fragment Header.
437 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
441 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
443 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
444 const int netoff = skb_network_offset(skb);
445 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
446 int start = netoff + sizeof(struct ipv6hdr);
447 int len = skb->len - start;
448 u8 prevhdr = NEXTHDR_IPV6;
450 while (nexthdr != NEXTHDR_FRAGMENT) {
451 struct ipv6_opt_hdr hdr;
454 if (!ipv6_ext_hdr(nexthdr)) {
457 if (nexthdr == NEXTHDR_NONE) {
458 pr_debug("next header is none\n");
461 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
462 pr_debug("too short\n");
465 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
467 if (nexthdr == NEXTHDR_AUTH)
468 hdrlen = (hdr.hdrlen+2)<<2;
470 hdrlen = ipv6_optlen(&hdr);
475 nexthdr = hdr.nexthdr;
484 *prevhoff = prev_nhoff;
490 struct sk_buff *rpl_nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
492 struct sk_buff *clone;
493 struct net_device *dev = skb->dev;
494 struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev)
496 struct frag_hdr *fhdr;
497 struct frag_queue *fq;
501 struct sk_buff *ret_skb = NULL;
503 /* Jumbo payload inhibits frag. header */
504 if (ipv6_hdr(skb)->payload_len == 0) {
505 pr_debug("payload len = 0\n");
509 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
512 clone = skb_clone(skb, GFP_ATOMIC);
514 pr_debug("Can't clone skb\n");
518 NFCT_FRAG6_CB(clone)->orig = skb;
520 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
521 pr_debug("message is too short.\n");
525 skb_set_transport_header(clone, fhoff);
526 hdr = ipv6_hdr(clone);
527 fhdr = (struct frag_hdr *)skb_transport_header(clone);
529 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
532 pr_debug("Can't find and can't create new queue\n");
536 spin_lock_bh(&fq->q.lock);
538 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
539 spin_unlock_bh(&fq->q.lock);
540 pr_debug("Can't insert skb to queue\n");
541 inet_frag_put(&fq->q, &nf_frags);
545 if (qp_flags(fq) == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
546 fq->q.meat == fq->q.len) {
547 ret_skb = nf_ct_frag6_reasm(fq, dev);
549 pr_debug("Can't reassemble fragmented packets\n");
551 spin_unlock_bh(&fq->q.lock);
553 inet_frag_put(&fq->q, &nf_frags);
560 EXPORT_SYMBOL_GPL(rpl_nf_ct_frag6_gather);
562 #ifdef HAVE_INET_FRAGS_CONST
563 static void rpl_ip6_frag_init(struct inet_frag_queue *q, const void *a)
565 static void rpl_ip6_frag_init(struct inet_frag_queue *q, void *a)
568 struct frag_queue *fq = container_of(q, struct frag_queue, q);
569 const struct ip6_create_arg *arg = a;
572 fq->user = arg->user;
573 fq->saddr = *arg->src;
574 fq->daddr = *arg->dst;
578 #ifdef HAVE_INET_FRAGS_CONST
579 static bool rpl_ip6_frag_match(const struct inet_frag_queue *q, const void *a)
581 static bool rpl_ip6_frag_match(struct inet_frag_queue *q, void *a)
584 const struct frag_queue *fq;
585 const struct ip6_create_arg *arg = a;
587 fq = container_of(q, struct frag_queue, q);
588 return fq->id == arg->id &&
589 fq->user == arg->user &&
590 ipv6_addr_equal(&fq->saddr, arg->src) &&
591 ipv6_addr_equal(&fq->daddr, arg->dst);
594 void nf_ct_frag6_consume_orig(struct sk_buff *skb)
596 struct sk_buff *s, *s2;
598 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
606 static int nf_ct_net_init(struct net *net)
608 nf_defrag_ipv6_enable();
613 static void nf_ct_net_exit(struct net *net)
615 inet_frags_exit_net(&net->ipv6.frags, &nf_frags);
618 static struct pernet_operations nf_ct_net_ops = {
619 .init = nf_ct_net_init,
620 .exit = nf_ct_net_exit,
623 int rpl_nf_ct_frag6_init(void)
627 nf_frags.hashfn = nf_hashfn;
628 nf_frags.constructor = rpl_ip6_frag_init;
629 nf_frags.destructor = NULL;
630 nf_frags.skb_free = nf_skb_free;
631 nf_frags.qsize = sizeof(struct frag_queue);
632 nf_frags.match = rpl_ip6_frag_match;
633 nf_frags.frag_expire = nf_ct_frag6_expire;
634 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
635 nf_frags.frags_cache_name = nf_frags_cache_name;
637 ret = inet_frags_init(&nf_frags);
640 ret = register_pernet_subsys(&nf_ct_net_ops);
642 inet_frags_fini(&nf_frags);
648 void rpl_nf_ct_frag6_cleanup(void)
650 unregister_pernet_subsys(&nf_ct_net_ops);
651 inet_frags_fini(&nf_frags);
654 #endif /* OVS_FRAGMENT_BACKPORT */