2 * IP fragmentation backport, heavily based on linux/net/ipv4/ip_fragment.c,
3 * copied from Linux 192132b9a034 net: Add support for VRFs to inetpeer cache
5 * INET An implementation of the TCP/IP protocol suite for the LINUX
6 * operating system. INET is implemented using the BSD Socket
7 * interface as the means of communication with the user level.
9 * The IP fragmentation functionality.
11 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
15 * Alan Cox : Split from ip.c , see ip_input.c for history.
16 * David S. Miller : Begin massive cleanup...
17 * Andi Kleen : Add sysctls.
18 * xxxx : Overlapfrag bug.
19 * Ultima : ip_expire() kernel panic.
20 * Bill Hawes : Frag accounting and evictor fixes.
21 * John McDonald : 0 length frag bug.
22 * Alexey Kuznetsov: SMP races, threading, cleanup.
23 * Patrick McHardy : LRU queue of frag heads for evictor.
26 #include <linux/version.h>
28 #ifdef OVS_FRAGMENT_BACKPORT
30 #define pr_fmt(fmt) "IPv4: " fmt
32 #include <linux/compiler.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
36 #include <linux/jiffies.h>
37 #include <linux/skbuff.h>
38 #include <linux/list.h>
40 #include <linux/icmp.h>
41 #include <linux/netdevice.h>
42 #include <linux/jhash.h>
43 #include <linux/random.h>
44 #include <linux/slab.h>
45 #include <net/route.h>
50 #include <net/checksum.h>
51 #include <net/inetpeer.h>
52 #include <net/inet_frag.h>
53 #include <linux/tcp.h>
54 #include <linux/udp.h>
55 #include <linux/inet.h>
56 #include <linux/netfilter_ipv4.h>
57 #include <net/inet_ecn.h>
59 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
61 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
62 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
63 * as well. Or notify me, at least. --ANK
66 static int sysctl_ipfrag_max_dist __read_mostly = 64;
67 static const char ip_frag_cache_name[] = "ip4-frags";
71 struct inet_skb_parm h;
75 #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
77 /* Describe an entry in the "incomplete datagrams" queue. */
80 struct inet_frag_queue q;
81 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
82 struct ovs_inet_frag_queue oq;
91 u8 ecn; /* RFC3168 support */
92 u16 max_df_size; /* largest frag with DF set seen */
94 int vif; /* VRF device index */
96 struct inet_peer *peer;
99 static u8 ip4_frag_ecn(u8 tos)
101 return 1 << (tos & INET_ECN_MASK);
104 static struct inet_frags ip4_frags;
106 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
107 struct net_device *dev);
109 struct ip4_create_arg {
115 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
117 net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
118 return jhash_3words((__force u32)id << 16 | prot,
119 (__force u32)saddr, (__force u32)daddr,
123 #ifdef HAVE_INET_FRAGS_CONST
124 static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
126 static unsigned int ip4_hashfn(struct inet_frag_queue *q)
129 const struct ipq *ipq;
131 ipq = container_of(q, struct ipq, q);
132 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
135 #ifdef HAVE_INET_FRAGS_CONST
136 static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
138 static bool ip4_frag_match(struct inet_frag_queue *q, void *a)
141 const struct ipq *qp;
142 const struct ip4_create_arg *arg = a;
144 qp = container_of(q, struct ipq, q);
145 return qp->id == arg->iph->id &&
146 qp->saddr == arg->iph->saddr &&
147 qp->daddr == arg->iph->daddr &&
148 qp->protocol == arg->iph->protocol &&
149 qp->user == arg->user &&
153 #ifdef HAVE_INET_FRAGS_CONST
154 static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
156 static void ip4_frag_init(struct inet_frag_queue *q, void *a)
159 struct ipq *qp = container_of(q, struct ipq, q);
160 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
162 struct net *net = container_of(ipv4, struct net, ipv4);
164 const struct ip4_create_arg *arg = a;
166 qp->protocol = arg->iph->protocol;
167 qp->id = arg->iph->id;
168 qp->ecn = ip4_frag_ecn(arg->iph->tos);
169 qp->saddr = arg->iph->saddr;
170 qp->daddr = arg->iph->daddr;
172 qp->user = arg->user;
173 qp->peer = sysctl_ipfrag_max_dist ?
174 inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, arg->vif, 1) :
178 static void ip4_frag_free(struct inet_frag_queue *q)
182 qp = container_of(q, struct ipq, q);
184 inet_putpeer(qp->peer);
188 /* Destruction primitives. */
190 static void ipq_put(struct ipq *ipq)
192 inet_frag_put(&ipq->q, &ip4_frags);
195 /* Kill ipq entry. It is not destroyed immediately,
196 * because caller (and someone more) holds reference count.
198 static void ipq_kill(struct ipq *ipq)
200 inet_frag_kill(&ipq->q, &ip4_frags);
203 static bool frag_expire_skip_icmp(u32 user)
205 return user == IP_DEFRAG_AF_PACKET ||
206 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
207 __IP_DEFRAG_CONNTRACK_IN_END) ||
208 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
209 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
213 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
215 static void ip_expire(unsigned long arg)
220 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
221 net = container_of(qp->q.net, struct net, ipv4.frags);
223 spin_lock(&qp->q.lock);
225 if (qp_flags(qp) & INET_FRAG_COMPLETE)
229 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
231 if (!inet_frag_evicting(&qp->q)) {
232 struct sk_buff *head = qp->q.fragments;
233 const struct iphdr *iph;
236 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
238 if (!(qp_flags(qp) & INET_FRAG_FIRST_IN) || !qp->q.fragments)
242 head->dev = dev_get_by_index_rcu(net, qp->iif);
246 /* skb has no dst, perform route lookup again */
248 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
249 iph->tos, head->dev);
253 /* Only an end host needs to send an ICMP
254 * "Fragment Reassembly Timeout" message, per RFC792.
256 if (frag_expire_skip_icmp(qp->user) &&
257 (skb_rtable(head)->rt_type != RTN_LOCAL))
260 /* Send an ICMP "Fragment Reassembly Timeout" message. */
261 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
266 spin_unlock(&qp->q.lock);
270 /* Find the correct entry in the "incomplete datagrams" queue for
271 * this IP datagram, and create new one, if nothing is found.
273 static struct ipq *ip_find(struct net *net, struct iphdr *iph,
276 struct inet_frag_queue *q;
277 struct ip4_create_arg arg;
284 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
286 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
287 if (IS_ERR_OR_NULL(q)) {
288 inet_frag_maybe_warn_overflow(q, pr_fmt());
291 return container_of(q, struct ipq, q);
294 /* Is the fragment too far ahead to be part of ipq? */
295 static int ip_frag_too_far(struct ipq *qp)
297 struct inet_peer *peer = qp->peer;
298 unsigned int max = sysctl_ipfrag_max_dist;
299 unsigned int start, end;
307 end = atomic_inc_return(&peer->rid);
310 rc = qp->q.fragments && (end - start) > max;
315 net = container_of(qp->q.net, struct net, ipv4.frags);
316 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
322 static int ip_frag_reinit(struct ipq *qp)
325 unsigned int sum_truesize = 0;
327 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
328 atomic_inc(&qp->q.refcnt);
332 fp = qp->q.fragments;
334 struct sk_buff *xp = fp->next;
336 sum_truesize += fp->truesize;
340 sub_frag_mem_limit(qp->q.net, sum_truesize);
345 qp->q.fragments = NULL;
346 qp->q.fragments_tail = NULL;
353 /* Add new segment to existing queue. */
354 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
356 struct sk_buff *prev, *next;
357 struct net_device *dev;
358 unsigned int fragsize;
364 if (qp_flags(qp) & INET_FRAG_COMPLETE)
367 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
368 unlikely(ip_frag_too_far(qp)) &&
369 unlikely(err = ip_frag_reinit(qp))) {
374 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
375 offset = ntohs(ip_hdr(skb)->frag_off);
376 flags = offset & ~IP_OFFSET;
378 offset <<= 3; /* offset is in 8-byte chunks */
379 ihl = ip_hdrlen(skb);
381 /* Determine the position of this fragment. */
382 end = offset + skb->len - skb_network_offset(skb) - ihl;
385 /* Is this the final fragment? */
386 if ((flags & IP_MF) == 0) {
387 /* If we already have some bits beyond end
388 * or have different end, the segment is corrupted.
390 if (end < qp->q.len ||
391 ((qp_flags(qp) & INET_FRAG_LAST_IN) && end != qp->q.len))
393 qp_flags(qp) |= INET_FRAG_LAST_IN;
398 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
399 skb->ip_summed = CHECKSUM_NONE;
401 if (end > qp->q.len) {
402 /* Some bits beyond end -> corruption. */
403 if (qp_flags(qp) & INET_FRAG_LAST_IN)
412 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
415 err = pskb_trim_rcsum(skb, end - offset);
419 /* Find out which fragments are in front and at the back of us
420 * in the chain of fragments so far. We must know where to put
421 * this fragment, right?
423 prev = qp->q.fragments_tail;
424 if (!prev || FRAG_CB(prev)->offset < offset) {
429 for (next = qp->q.fragments; next != NULL; next = next->next) {
430 if (FRAG_CB(next)->offset >= offset)
436 /* We found where to put this one. Check for overlap with
437 * preceding fragment, and, if needed, align things so that
438 * any overlaps are eliminated.
441 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
449 if (!pskb_pull(skb, i))
451 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
452 skb->ip_summed = CHECKSUM_NONE;
458 while (next && FRAG_CB(next)->offset < end) {
459 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
462 /* Eat head of the next overlapped fragment
463 * and leave the loop. The next ones cannot overlap.
465 if (!pskb_pull(next, i))
467 FRAG_CB(next)->offset += i;
469 if (next->ip_summed != CHECKSUM_UNNECESSARY)
470 next->ip_summed = CHECKSUM_NONE;
473 struct sk_buff *free_it = next;
475 /* Old fragment is completely overridden with
483 qp->q.fragments = next;
485 qp->q.meat -= free_it->len;
486 sub_frag_mem_limit(qp->q.net, free_it->truesize);
491 FRAG_CB(skb)->offset = offset;
493 /* Insert this fragment in the chain of fragments. */
496 qp->q.fragments_tail = skb;
500 qp->q.fragments = skb;
504 qp->iif = dev->ifindex;
507 qp->q.stamp = skb->tstamp;
508 qp->q.meat += skb->len;
510 add_frag_mem_limit(qp->q.net, skb->truesize);
512 qp_flags(qp) |= INET_FRAG_FIRST_IN;
514 fragsize = skb->len + ihl;
516 if (fragsize > qp->q.max_size)
517 qp->q.max_size = fragsize;
519 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
520 fragsize > qp->max_df_size)
521 qp->max_df_size = fragsize;
523 if (qp_flags(qp) == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
524 qp->q.meat == qp->q.len) {
525 unsigned long orefdst = skb->_skb_refdst;
527 skb->_skb_refdst = 0UL;
528 err = ip_frag_reasm(qp, prev, dev);
529 skb->_skb_refdst = orefdst;
542 /* Build a new IP datagram from all its fragments. */
544 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
545 struct net_device *dev)
547 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
549 struct sk_buff *fp, *head = qp->q.fragments;
557 ecn = ip_frag_ecn_table[qp->ecn];
558 if (unlikely(ecn == 0xff)) {
562 /* Make the one we just received the head. */
565 fp = skb_clone(head, GFP_ATOMIC);
569 fp->next = head->next;
571 qp->q.fragments_tail = fp;
574 skb_morph(head, qp->q.fragments);
575 head->next = qp->q.fragments->next;
577 consume_skb(qp->q.fragments);
578 qp->q.fragments = head;
582 WARN_ON(FRAG_CB(head)->offset != 0);
584 /* Allocate a new buffer for the datagram. */
585 ihlen = ip_hdrlen(head);
586 len = ihlen + qp->q.len;
592 /* Head of list must not be cloned. */
593 if (skb_unclone(head, GFP_ATOMIC))
596 /* If the first fragment is fragmented itself, we split
597 * it to two chunks: the first with data and paged part
598 * and the second, holding only fragments. */
599 if (skb_has_frag_list(head)) {
600 struct sk_buff *clone;
603 clone = alloc_skb(0, GFP_ATOMIC);
606 clone->next = head->next;
608 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
609 skb_frag_list_init(head);
610 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
611 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
612 clone->len = clone->data_len = head->data_len - plen;
613 head->data_len -= clone->len;
614 head->len -= clone->len;
616 clone->ip_summed = head->ip_summed;
617 add_frag_mem_limit(qp->q.net, clone->truesize);
620 skb_shinfo(head)->frag_list = head->next;
621 skb_push(head, head->data - skb_network_header(head));
623 for (fp=head->next; fp; fp = fp->next) {
624 head->data_len += fp->len;
625 head->len += fp->len;
626 if (head->ip_summed != fp->ip_summed)
627 head->ip_summed = CHECKSUM_NONE;
628 else if (head->ip_summed == CHECKSUM_COMPLETE)
629 head->csum = csum_add(head->csum, fp->csum);
630 head->truesize += fp->truesize;
632 sub_frag_mem_limit(qp->q.net, head->truesize);
636 head->tstamp = qp->q.stamp;
637 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
640 iph->tot_len = htons(len);
643 /* When we set IP_DF on a refragmented skb we must also force a
644 * call to ip_fragment to avoid forwarding a DF-skb of size s while
645 * original sender only sent fragments of size f (where f < s).
647 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
648 * frag seen to avoid sending tiny DF-fragments in case skb was built
649 * from one very small df-fragment and one large non-df frag.
651 if (qp->max_df_size == qp->q.max_size) {
652 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
653 iph->frag_off = htons(IP_DF);
660 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
661 qp->q.fragments = NULL;
662 qp->q.fragments_tail = NULL;
666 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
670 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
672 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
676 /* Process an incoming IP datagram fragment. */
677 int rpl_ip_defrag(struct sk_buff *skb, u32 user)
679 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
680 int vif = vrf_master_ifindex_rcu(dev);
681 struct net *net = dev_net(dev);
684 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
686 /* Lookup (or create) queue header */
687 qp = ip_find(net, ip_hdr(skb), user, vif);
691 spin_lock(&qp->q.lock);
693 ret = ip_frag_queue(qp, skb);
695 spin_unlock(&qp->q.lock);
700 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
704 EXPORT_SYMBOL_GPL(rpl_ip_defrag);
706 static int __net_init ipv4_frags_init_net(struct net *net)
708 nf_defrag_ipv4_enable();
713 static void __net_exit ipv4_frags_exit_net(struct net *net)
715 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
718 static struct pernet_operations ip4_frags_ops = {
719 .init = ipv4_frags_init_net,
720 .exit = ipv4_frags_exit_net,
723 int __init rpl_ipfrag_init(void)
725 register_pernet_subsys(&ip4_frags_ops);
726 ip4_frags.hashfn = ip4_hashfn;
727 ip4_frags.constructor = ip4_frag_init;
728 ip4_frags.destructor = ip4_frag_free;
729 ip4_frags.skb_free = NULL;
730 ip4_frags.qsize = sizeof(struct ipq);
731 ip4_frags.match = ip4_frag_match;
732 ip4_frags.frag_expire = ip_expire;
733 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)
734 ip4_frags.frags_cache_name = ip_frag_cache_name;
736 if (inet_frags_init(&ip4_frags)) {
737 pr_warn("IP: failed to allocate ip4_frags cache\n");
743 void rpl_ipfrag_fini(void)
745 inet_frags_fini(&ip4_frags);
746 unregister_pernet_subsys(&ip4_frags_ops);
749 #endif /* OVS_FRAGMENT_BACKPORT */