1 // SPDX-License-Identifier: GPL-2.0
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * The IP fragmentation functionality.
9 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox <alan@lxorguk.ukuu.org.uk>
13 * Alan Cox : Split from ip.c , see ip_input.c for history.
14 * David S. Miller : Begin massive cleanup...
15 * Andi Kleen : Add sysctls.
16 * xxxx : Overlapfrag bug.
17 * Ultima : ip_expire() kernel panic.
18 * Bill Hawes : Frag accounting and evictor fixes.
19 * John McDonald : 0 length frag bug.
20 * Alexey Kuznetsov: SMP races, threading, cleanup.
21 * Patrick McHardy : LRU queue of frag heads for evictor.
24 #define pr_fmt(fmt) "IPv4: " fmt
26 #include <linux/compiler.h>
27 #include <linux/module.h>
28 #include <linux/types.h>
30 #include <linux/jiffies.h>
31 #include <linux/skbuff.h>
32 #include <linux/list.h>
34 #include <linux/icmp.h>
35 #include <linux/netdevice.h>
36 #include <linux/jhash.h>
37 #include <linux/random.h>
38 #include <linux/slab.h>
39 #include <net/route.h>
44 #include <net/checksum.h>
45 #include <net/inetpeer.h>
46 #include <net/inet_frag.h>
47 #include <linux/tcp.h>
48 #include <linux/udp.h>
49 #include <linux/inet.h>
50 #include <linux/netfilter_ipv4.h>
51 #include <net/inet_ecn.h>
52 #include <net/l3mdev.h>
54 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
55 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
56 * as well. Or notify me, at least. --ANK
58 static const char ip_frag_cache_name[] = "ip4-frags";
62 struct inet_skb_parm h;
66 #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
68 /* Describe an entry in the "incomplete datagrams" queue. */
70 struct inet_frag_queue q;
72 u8 ecn; /* RFC3168 support */
73 u16 max_df_size; /* largest frag with DF set seen */
76 struct inet_peer *peer;
79 static u8 ip4_frag_ecn(u8 tos)
81 return 1 << (tos & INET_ECN_MASK);
84 static struct inet_frags ip4_frags;
86 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
87 struct net_device *dev);
90 static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
92 struct ipq *qp = container_of(q, struct ipq, q);
93 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
95 struct net *net = container_of(ipv4, struct net, ipv4);
97 const struct frag_v4_compare_key *key = a;
101 qp->peer = q->net->max_dist ?
102 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
106 static void ip4_frag_free(struct inet_frag_queue *q)
110 qp = container_of(q, struct ipq, q);
112 inet_putpeer(qp->peer);
116 /* Destruction primitives. */
118 static void ipq_put(struct ipq *ipq)
120 inet_frag_put(&ipq->q);
123 /* Kill ipq entry. It is not destroyed immediately,
124 * because caller (and someone more) holds reference count.
126 static void ipq_kill(struct ipq *ipq)
128 inet_frag_kill(&ipq->q);
131 static bool frag_expire_skip_icmp(u32 user)
133 return user == IP_DEFRAG_AF_PACKET ||
134 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
135 __IP_DEFRAG_CONNTRACK_IN_END) ||
136 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
137 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
141 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
143 static void ip_expire(struct timer_list *t)
145 struct inet_frag_queue *frag = from_timer(frag, t, timer);
146 struct sk_buff *clone, *head;
147 const struct iphdr *iph;
152 qp = container_of(frag, struct ipq, q);
153 net = container_of(qp->q.net, struct net, ipv4.frags);
156 spin_lock(&qp->q.lock);
158 if (qp->q.flags & INET_FRAG_COMPLETE)
162 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
164 head = qp->q.fragments;
166 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
168 if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
171 head->dev = dev_get_by_index_rcu(net, qp->iif);
176 /* skb has no dst, perform route lookup again */
178 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
179 iph->tos, head->dev);
183 /* Only an end host needs to send an ICMP
184 * "Fragment Reassembly Timeout" message, per RFC792.
186 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
187 (skb_rtable(head)->rt_type != RTN_LOCAL))
190 clone = skb_clone(head, GFP_ATOMIC);
192 /* Send an ICMP "Fragment Reassembly Timeout" message. */
194 spin_unlock(&qp->q.lock);
195 icmp_send(clone, ICMP_TIME_EXCEEDED,
196 ICMP_EXC_FRAGTIME, 0);
201 spin_unlock(&qp->q.lock);
207 /* Find the correct entry in the "incomplete datagrams" queue for
208 * this IP datagram, and create new one, if nothing is found.
210 static struct ipq *ip_find(struct net *net, struct iphdr *iph,
213 struct frag_v4_compare_key key = {
219 .protocol = iph->protocol,
221 struct inet_frag_queue *q;
223 q = inet_frag_find(&net->ipv4.frags, &key);
227 return container_of(q, struct ipq, q);
230 /* Is the fragment too far ahead to be part of ipq? */
231 static int ip_frag_too_far(struct ipq *qp)
233 struct inet_peer *peer = qp->peer;
234 unsigned int max = qp->q.net->max_dist;
235 unsigned int start, end;
243 end = atomic_inc_return(&peer->rid);
246 rc = qp->q.fragments && (end - start) > max;
251 net = container_of(qp->q.net, struct net, ipv4.frags);
252 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
258 static int ip_frag_reinit(struct ipq *qp)
261 unsigned int sum_truesize = 0;
263 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
264 refcount_inc(&qp->q.refcnt);
268 fp = qp->q.fragments;
270 struct sk_buff *xp = fp->next;
272 sum_truesize += fp->truesize;
276 sub_frag_mem_limit(qp->q.net, sum_truesize);
281 qp->q.fragments = NULL;
282 qp->q.fragments_tail = NULL;
289 /* Add new segment to existing queue. */
290 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
292 struct sk_buff *prev, *next;
293 struct net_device *dev;
294 unsigned int fragsize;
300 if (qp->q.flags & INET_FRAG_COMPLETE)
303 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
304 unlikely(ip_frag_too_far(qp)) &&
305 unlikely(err = ip_frag_reinit(qp))) {
310 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
311 offset = ntohs(ip_hdr(skb)->frag_off);
312 flags = offset & ~IP_OFFSET;
314 offset <<= 3; /* offset is in 8-byte chunks */
315 ihl = ip_hdrlen(skb);
317 /* Determine the position of this fragment. */
318 end = offset + skb->len - skb_network_offset(skb) - ihl;
321 /* Is this the final fragment? */
322 if ((flags & IP_MF) == 0) {
323 /* If we already have some bits beyond end
324 * or have different end, the segment is corrupted.
326 if (end < qp->q.len ||
327 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
329 qp->q.flags |= INET_FRAG_LAST_IN;
334 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
335 skb->ip_summed = CHECKSUM_NONE;
337 if (end > qp->q.len) {
338 /* Some bits beyond end -> corruption. */
339 if (qp->q.flags & INET_FRAG_LAST_IN)
348 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
351 err = pskb_trim_rcsum(skb, end - offset);
355 /* Find out which fragments are in front and at the back of us
356 * in the chain of fragments so far. We must know where to put
357 * this fragment, right?
359 prev = qp->q.fragments_tail;
360 if (!prev || FRAG_CB(prev)->offset < offset) {
365 for (next = qp->q.fragments; next != NULL; next = next->next) {
366 if (FRAG_CB(next)->offset >= offset)
372 /* We found where to put this one. Check for overlap with
373 * preceding fragment, and, if needed, align things so that
374 * any overlaps are eliminated.
377 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
385 if (!pskb_pull(skb, i))
387 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
388 skb->ip_summed = CHECKSUM_NONE;
394 while (next && FRAG_CB(next)->offset < end) {
395 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
398 /* Eat head of the next overlapped fragment
399 * and leave the loop. The next ones cannot overlap.
401 if (!pskb_pull(next, i))
403 FRAG_CB(next)->offset += i;
405 if (next->ip_summed != CHECKSUM_UNNECESSARY)
406 next->ip_summed = CHECKSUM_NONE;
409 struct sk_buff *free_it = next;
411 /* Old fragment is completely overridden with
419 qp->q.fragments = next;
421 qp->q.meat -= free_it->len;
422 sub_frag_mem_limit(qp->q.net, free_it->truesize);
427 FRAG_CB(skb)->offset = offset;
429 /* Insert this fragment in the chain of fragments. */
432 qp->q.fragments_tail = skb;
436 qp->q.fragments = skb;
440 qp->iif = dev->ifindex;
443 qp->q.stamp = skb->tstamp;
444 qp->q.meat += skb->len;
446 add_frag_mem_limit(qp->q.net, skb->truesize);
448 qp->q.flags |= INET_FRAG_FIRST_IN;
450 fragsize = skb->len + ihl;
452 if (fragsize > qp->q.max_size)
453 qp->q.max_size = fragsize;
455 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
456 fragsize > qp->max_df_size)
457 qp->max_df_size = fragsize;
459 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
460 qp->q.meat == qp->q.len) {
461 unsigned long orefdst = skb->_skb_refdst;
463 skb->_skb_refdst = 0UL;
464 err = ip_frag_reasm(qp, prev, dev);
465 skb->_skb_refdst = orefdst;
478 /* Build a new IP datagram from all its fragments. */
480 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
481 struct net_device *dev)
483 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
485 struct sk_buff *fp, *head = qp->q.fragments;
493 ecn = ip_frag_ecn_table[qp->ecn];
494 if (unlikely(ecn == 0xff)) {
498 /* Make the one we just received the head. */
501 fp = skb_clone(head, GFP_ATOMIC);
505 fp->next = head->next;
507 qp->q.fragments_tail = fp;
510 skb_morph(head, qp->q.fragments);
511 head->next = qp->q.fragments->next;
513 consume_skb(qp->q.fragments);
514 qp->q.fragments = head;
518 WARN_ON(FRAG_CB(head)->offset != 0);
520 /* Allocate a new buffer for the datagram. */
521 ihlen = ip_hdrlen(head);
522 len = ihlen + qp->q.len;
528 /* Head of list must not be cloned. */
529 if (skb_unclone(head, GFP_ATOMIC))
532 /* If the first fragment is fragmented itself, we split
533 * it to two chunks: the first with data and paged part
534 * and the second, holding only fragments. */
535 if (skb_has_frag_list(head)) {
536 struct sk_buff *clone;
539 clone = alloc_skb(0, GFP_ATOMIC);
542 clone->next = head->next;
544 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
545 skb_frag_list_init(head);
546 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
547 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
548 clone->len = clone->data_len = head->data_len - plen;
549 head->data_len -= clone->len;
550 head->len -= clone->len;
552 clone->ip_summed = head->ip_summed;
553 add_frag_mem_limit(qp->q.net, clone->truesize);
556 skb_shinfo(head)->frag_list = head->next;
557 skb_push(head, head->data - skb_network_header(head));
559 for (fp=head->next; fp; fp = fp->next) {
560 head->data_len += fp->len;
561 head->len += fp->len;
562 if (head->ip_summed != fp->ip_summed)
563 head->ip_summed = CHECKSUM_NONE;
564 else if (head->ip_summed == CHECKSUM_COMPLETE)
565 head->csum = csum_add(head->csum, fp->csum);
566 head->truesize += fp->truesize;
568 sub_frag_mem_limit(qp->q.net, head->truesize);
572 head->tstamp = qp->q.stamp;
573 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
576 iph->tot_len = htons(len);
579 /* When we set IP_DF on a refragmented skb we must also force a
580 * call to ip_fragment to avoid forwarding a DF-skb of size s while
581 * original sender only sent fragments of size f (where f < s).
583 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
584 * frag seen to avoid sending tiny DF-fragments in case skb was built
585 * from one very small df-fragment and one large non-df frag.
587 if (qp->max_df_size == qp->q.max_size) {
588 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
589 iph->frag_off = htons(IP_DF);
596 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
597 qp->q.fragments = NULL;
598 qp->q.fragments_tail = NULL;
602 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
606 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
608 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
612 /* Process an incoming IP datagram fragment. */
613 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
615 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
616 int vif = l3mdev_master_ifindex_rcu(dev);
619 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
622 /* Lookup (or create) queue header */
623 qp = ip_find(net, ip_hdr(skb), user, vif);
627 spin_lock(&qp->q.lock);
629 ret = ip_frag_queue(qp, skb);
631 spin_unlock(&qp->q.lock);
636 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
640 EXPORT_SYMBOL(ip_defrag);
642 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
648 if (skb->protocol != htons(ETH_P_IP))
651 netoff = skb_network_offset(skb);
653 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
656 if (iph.ihl < 5 || iph.version != 4)
659 len = ntohs(iph.tot_len);
660 if (skb->len < netoff + len || len < (iph.ihl * 4))
663 if (ip_is_fragment(&iph)) {
664 skb = skb_share_check(skb, GFP_ATOMIC);
666 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
668 if (pskb_trim_rcsum(skb, netoff + len))
670 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
671 if (ip_defrag(net, skb, user))
678 EXPORT_SYMBOL(ip_check_defrag);
683 static struct ctl_table ip4_frags_ns_ctl_table[] = {
685 .procname = "ipfrag_high_thresh",
686 .data = &init_net.ipv4.frags.high_thresh,
687 .maxlen = sizeof(unsigned long),
689 .proc_handler = proc_doulongvec_minmax,
690 .extra1 = &init_net.ipv4.frags.low_thresh
693 .procname = "ipfrag_low_thresh",
694 .data = &init_net.ipv4.frags.low_thresh,
695 .maxlen = sizeof(unsigned long),
697 .proc_handler = proc_doulongvec_minmax,
699 .extra2 = &init_net.ipv4.frags.high_thresh
702 .procname = "ipfrag_time",
703 .data = &init_net.ipv4.frags.timeout,
704 .maxlen = sizeof(int),
706 .proc_handler = proc_dointvec_jiffies,
709 .procname = "ipfrag_max_dist",
710 .data = &init_net.ipv4.frags.max_dist,
711 .maxlen = sizeof(int),
713 .proc_handler = proc_dointvec_minmax,
719 /* secret interval has been deprecated */
720 static int ip4_frags_secret_interval_unused;
721 static struct ctl_table ip4_frags_ctl_table[] = {
723 .procname = "ipfrag_secret_interval",
724 .data = &ip4_frags_secret_interval_unused,
725 .maxlen = sizeof(int),
727 .proc_handler = proc_dointvec_jiffies,
732 static int __net_init ip4_frags_ns_ctl_register(struct net *net)
734 struct ctl_table *table;
735 struct ctl_table_header *hdr;
737 table = ip4_frags_ns_ctl_table;
738 if (!net_eq(net, &init_net)) {
739 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
743 table[0].data = &net->ipv4.frags.high_thresh;
744 table[0].extra1 = &net->ipv4.frags.low_thresh;
745 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
746 table[1].data = &net->ipv4.frags.low_thresh;
747 table[1].extra2 = &net->ipv4.frags.high_thresh;
748 table[2].data = &net->ipv4.frags.timeout;
749 table[3].data = &net->ipv4.frags.max_dist;
752 hdr = register_net_sysctl(net, "net/ipv4", table);
756 net->ipv4.frags_hdr = hdr;
760 if (!net_eq(net, &init_net))
766 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
768 struct ctl_table *table;
770 table = net->ipv4.frags_hdr->ctl_table_arg;
771 unregister_net_sysctl_table(net->ipv4.frags_hdr);
775 static void __init ip4_frags_ctl_register(void)
777 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
780 static int ip4_frags_ns_ctl_register(struct net *net)
785 static void ip4_frags_ns_ctl_unregister(struct net *net)
789 static void __init ip4_frags_ctl_register(void)
794 static int __net_init ipv4_frags_init_net(struct net *net)
798 /* Fragment cache limits.
800 * The fragment memory accounting code, (tries to) account for
801 * the real memory usage, by measuring both the size of frag
802 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
803 * and the SKB's truesize.
805 * A 64K fragment consumes 129736 bytes (44*2944)+200
806 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
808 * We will commit 4MB at one time. Should we cross that limit
809 * we will prune down to 3MB, making room for approx 8 big 64K
812 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
813 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
815 * Important NOTE! Fragment queue must be destroyed before MSL expires.
816 * RFC791 is wrong proposing to prolongate timer each fragment arrival
819 net->ipv4.frags.timeout = IP_FRAG_TIME;
821 net->ipv4.frags.max_dist = 64;
822 net->ipv4.frags.f = &ip4_frags;
824 res = inet_frags_init_net(&net->ipv4.frags);
827 res = ip4_frags_ns_ctl_register(net);
829 inet_frags_exit_net(&net->ipv4.frags);
833 static void __net_exit ipv4_frags_exit_net(struct net *net)
835 ip4_frags_ns_ctl_unregister(net);
836 inet_frags_exit_net(&net->ipv4.frags);
839 static struct pernet_operations ip4_frags_ops = {
840 .init = ipv4_frags_init_net,
841 .exit = ipv4_frags_exit_net,
845 static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
848 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
851 static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
853 const struct inet_frag_queue *fq = data;
855 return jhash2((const u32 *)&fq->key.v4,
856 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
859 static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
861 const struct frag_v4_compare_key *key = arg->key;
862 const struct inet_frag_queue *fq = ptr;
864 return !!memcmp(&fq->key, key, sizeof(*key));
867 static const struct rhashtable_params ip4_rhash_params = {
868 .head_offset = offsetof(struct inet_frag_queue, node),
869 .key_offset = offsetof(struct inet_frag_queue, key),
870 .key_len = sizeof(struct frag_v4_compare_key),
871 .hashfn = ip4_key_hashfn,
872 .obj_hashfn = ip4_obj_hashfn,
873 .obj_cmpfn = ip4_obj_cmpfn,
874 .automatic_shrinking = true,
877 void __init ipfrag_init(void)
879 ip4_frags.constructor = ip4_frag_init;
880 ip4_frags.destructor = ip4_frag_free;
881 ip4_frags.qsize = sizeof(struct ipq);
882 ip4_frags.frag_expire = ip_expire;
883 ip4_frags.frags_cache_name = ip_frag_cache_name;
884 ip4_frags.rhash_params = ip4_rhash_params;
885 if (inet_frags_init(&ip4_frags))
886 panic("IP: failed to allocate ip4_frags cache\n");
887 ip4_frags_ctl_register();
888 register_pernet_subsys(&ip4_frags_ops);