Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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. |
| 6 | * |
| 7 | * The IP fragmentation functionality. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG> |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 10 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * Fixes: |
| 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. |
| 22 | */ |
| 23 | |
Joe Perches | afd46503 | 2012-03-12 07:03:32 +0000 | [diff] [blame] | 24 | #define pr_fmt(fmt) "IPv4: " fmt |
| 25 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 26 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/ip.h> |
| 34 | #include <linux/icmp.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/jhash.h> |
| 37 | #include <linux/random.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 38 | #include <linux/slab.h> |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 39 | #include <net/route.h> |
| 40 | #include <net/dst.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/sock.h> |
| 42 | #include <net/ip.h> |
| 43 | #include <net/icmp.h> |
| 44 | #include <net/checksum.h> |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 45 | #include <net/inetpeer.h> |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 46 | #include <net/inet_frag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/tcp.h> |
| 48 | #include <linux/udp.h> |
| 49 | #include <linux/inet.h> |
| 50 | #include <linux/netfilter_ipv4.h> |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 51 | #include <net/inet_ecn.h> |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 52 | #include <net/l3mdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 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 |
| 57 | */ |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 58 | static const char ip_frag_cache_name[] = "ip4-frags"; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* Describe an entry in the "incomplete datagrams" queue. */ |
| 61 | struct ipq { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 62 | struct inet_frag_queue q; |
| 63 | |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 64 | u8 ecn; /* RFC3168 support */ |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 65 | u16 max_df_size; /* largest frag with DF set seen */ |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 66 | int iif; |
| 67 | unsigned int rid; |
| 68 | struct inet_peer *peer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 71 | static u8 ip4_frag_ecn(u8 tos) |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 72 | { |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 73 | return 1 << (tos & INET_ECN_MASK); |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 76 | static struct inet_frags ip4_frags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 78 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, |
| 79 | struct net_device *dev); |
| 80 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 81 | |
Florian Westphal | 36c7778 | 2014-07-24 16:50:29 +0200 | [diff] [blame] | 82 | static void ip4_frag_init(struct inet_frag_queue *q, const void *a) |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 83 | { |
| 84 | struct ipq *qp = container_of(q, struct ipq, q); |
Gao feng | 54db0cc | 2012-06-08 01:21:40 +0000 | [diff] [blame] | 85 | struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4, |
| 86 | frags); |
| 87 | struct net *net = container_of(ipv4, struct net, ipv4); |
| 88 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 89 | const struct frag_v4_compare_key *key = a; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 90 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 91 | q->key.v4 = *key; |
| 92 | qp->ecn = 0; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 93 | qp->peer = q->net->max_dist ? |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 94 | inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) : |
David Ahern | 192132b | 2015-08-27 16:07:03 -0700 | [diff] [blame] | 95 | NULL; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 98 | static void ip4_frag_free(struct inet_frag_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 100 | struct ipq *qp; |
| 101 | |
| 102 | qp = container_of(q, struct ipq, q); |
| 103 | if (qp->peer) |
| 104 | inet_putpeer(qp->peer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | /* Destruction primitives. */ |
| 109 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 110 | static void ipq_put(struct ipq *ipq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 112 | inet_frag_put(&ipq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Kill ipq entry. It is not destroyed immediately, |
| 116 | * because caller (and someone more) holds reference count. |
| 117 | */ |
| 118 | static void ipq_kill(struct ipq *ipq) |
| 119 | { |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 120 | inet_frag_kill(&ipq->q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Andy Zhou | 5cf4228 | 2015-05-15 14:15:35 -0700 | [diff] [blame] | 123 | static bool frag_expire_skip_icmp(u32 user) |
| 124 | { |
| 125 | return user == IP_DEFRAG_AF_PACKET || |
| 126 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN, |
Andy Zhou | 8bc0486 | 2015-05-15 14:15:36 -0700 | [diff] [blame] | 127 | __IP_DEFRAG_CONNTRACK_IN_END) || |
| 128 | ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN, |
| 129 | __IP_DEFRAG_CONNTRACK_BRIDGE_IN); |
Andy Zhou | 5cf4228 | 2015-05-15 14:15:35 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | /* |
| 133 | * Oops, a fragment queue timed out. Kill it and send an ICMP reply. |
| 134 | */ |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 135 | static void ip_expire(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 137 | struct inet_frag_queue *frag = from_timer(frag, t, timer); |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 138 | const struct iphdr *iph; |
Eric Dumazet | 1eec5d5 | 2018-03-31 12:58:54 -0700 | [diff] [blame] | 139 | struct sk_buff *head; |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 140 | struct net *net; |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 141 | struct ipq *qp; |
| 142 | int err; |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 143 | |
Kees Cook | 7880201 | 2017-10-16 17:29:20 -0700 | [diff] [blame] | 144 | qp = container_of(frag, struct ipq, q); |
Pavel Emelyanov | 84a3aa0 | 2008-07-16 20:19:08 -0700 | [diff] [blame] | 145 | net = container_of(qp->q.net, struct net, ipv4.frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 147 | rcu_read_lock(); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 148 | spin_lock(&qp->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 150 | if (qp->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | goto out; |
| 152 | |
| 153 | ipq_kill(qp); |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 154 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 156 | head = qp->q.fragments; |
Denis V. Lunev | cb84663 | 2008-03-24 15:31:00 -0700 | [diff] [blame] | 157 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 158 | __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT); |
Nikolay Aleksandrov | 2e404f6 | 2014-08-01 12:29:47 +0200 | [diff] [blame] | 159 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 160 | if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head) |
| 161 | goto out; |
Nikolay Aleksandrov | 2e404f6 | 2014-08-01 12:29:47 +0200 | [diff] [blame] | 162 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 163 | head->dev = dev_get_by_index_rcu(net, qp->iif); |
| 164 | if (!head->dev) |
| 165 | goto out; |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 166 | |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 167 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 168 | /* skb has no dst, perform route lookup again */ |
| 169 | iph = ip_hdr(head); |
| 170 | err = ip_route_input_noref(head, iph->daddr, iph->saddr, |
David S. Miller | c6cffba | 2012-07-26 11:14:38 +0000 | [diff] [blame] | 171 | iph->tos, head->dev); |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 172 | if (err) |
| 173 | goto out; |
Eric Dumazet | 64f3b9e | 2011-05-04 10:02:26 +0000 | [diff] [blame] | 174 | |
Eric Dumazet | 399d140 | 2018-03-31 12:58:51 -0700 | [diff] [blame] | 175 | /* Only an end host needs to send an ICMP |
| 176 | * "Fragment Reassembly Timeout" message, per RFC792. |
| 177 | */ |
| 178 | if (frag_expire_skip_icmp(qp->q.key.v4.user) && |
| 179 | (skb_rtable(head)->rt_type != RTN_LOCAL)) |
| 180 | goto out; |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 181 | |
Eric Dumazet | 1eec5d5 | 2018-03-31 12:58:54 -0700 | [diff] [blame] | 182 | skb_get(head); |
| 183 | spin_unlock(&qp->q.lock); |
| 184 | icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0); |
| 185 | kfree_skb(head); |
| 186 | goto out_rcu_unlock; |
Shan Wei | e9017b5 | 2010-01-23 01:57:42 -0800 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | out: |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 189 | spin_unlock(&qp->q.lock); |
Eric Dumazet | ec4fbd6 | 2017-03-22 08:57:15 -0700 | [diff] [blame] | 190 | out_rcu_unlock: |
| 191 | rcu_read_unlock(); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 192 | ipq_put(qp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 195 | /* Find the correct entry in the "incomplete datagrams" queue for |
| 196 | * this IP datagram, and create new one, if nothing is found. |
| 197 | */ |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 198 | static struct ipq *ip_find(struct net *net, struct iphdr *iph, |
| 199 | u32 user, int vif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 201 | struct frag_v4_compare_key key = { |
| 202 | .saddr = iph->saddr, |
| 203 | .daddr = iph->daddr, |
| 204 | .user = user, |
| 205 | .vif = vif, |
| 206 | .id = iph->id, |
| 207 | .protocol = iph->protocol, |
| 208 | }; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 209 | struct inet_frag_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 211 | q = inet_frag_find(&net->ipv4.frags, &key); |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 212 | if (!q) |
Hannes Frederic Sowa | 5a3da1f | 2013-03-15 11:32:30 +0000 | [diff] [blame] | 213 | return NULL; |
Eric Dumazet | 2d44ed2 | 2018-03-31 12:58:52 -0700 | [diff] [blame] | 214 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 215 | return container_of(q, struct ipq, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 218 | /* Is the fragment too far ahead to be part of ipq? */ |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 219 | static int ip_frag_too_far(struct ipq *qp) |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 220 | { |
| 221 | struct inet_peer *peer = qp->peer; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 222 | unsigned int max = qp->q.net->max_dist; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 223 | unsigned int start, end; |
| 224 | |
| 225 | int rc; |
| 226 | |
| 227 | if (!peer || !max) |
| 228 | return 0; |
| 229 | |
| 230 | start = qp->rid; |
| 231 | end = atomic_inc_return(&peer->rid); |
| 232 | qp->rid = end; |
| 233 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 234 | rc = qp->q.fragments && (end - start) > max; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 235 | |
| 236 | if (rc) { |
Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 237 | struct net *net; |
| 238 | |
| 239 | net = container_of(qp->q.net, struct net, ipv4.frags); |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 240 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | static int ip_frag_reinit(struct ipq *qp) |
| 247 | { |
| 248 | struct sk_buff *fp; |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 249 | unsigned int sum_truesize = 0; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 250 | |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 251 | if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) { |
Reshetova, Elena | edcb691 | 2017-06-30 13:08:07 +0300 | [diff] [blame] | 252 | refcount_inc(&qp->q.refcnt); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 253 | return -ETIMEDOUT; |
| 254 | } |
| 255 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 256 | fp = qp->q.fragments; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 257 | do { |
| 258 | struct sk_buff *xp = fp->next; |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 259 | |
| 260 | sum_truesize += fp->truesize; |
| 261 | kfree_skb(fp); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 262 | fp = xp; |
| 263 | } while (fp); |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 264 | sub_frag_mem_limit(qp->q.net, sum_truesize); |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 265 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 266 | qp->q.flags = 0; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 267 | qp->q.len = 0; |
| 268 | qp->q.meat = 0; |
| 269 | qp->q.fragments = NULL; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 270 | qp->q.fragments_tail = NULL; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 271 | qp->iif = 0; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 272 | qp->ecn = 0; |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* Add new segment to existing queue. */ |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 278 | static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | struct sk_buff *prev, *next; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 281 | struct net_device *dev; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 282 | unsigned int fragsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | int flags, offset; |
| 284 | int ihl, end; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 285 | int err = -ENOENT; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 286 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 288 | if (qp->q.flags & INET_FRAG_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | goto err; |
| 290 | |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 291 | if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) && |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 292 | unlikely(ip_frag_too_far(qp)) && |
| 293 | unlikely(err = ip_frag_reinit(qp))) { |
Herbert Xu | 89cee8b | 2005-12-13 23:14:27 -0800 | [diff] [blame] | 294 | ipq_kill(qp); |
| 295 | goto err; |
| 296 | } |
| 297 | |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 298 | ecn = ip4_frag_ecn(ip_hdr(skb)->tos); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 299 | offset = ntohs(ip_hdr(skb)->frag_off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | flags = offset & ~IP_OFFSET; |
| 301 | offset &= IP_OFFSET; |
| 302 | offset <<= 3; /* offset is in 8-byte chunks */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 303 | ihl = ip_hdrlen(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | /* Determine the position of this fragment. */ |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 306 | end = offset + skb->len - skb_network_offset(skb) - ihl; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 307 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | /* Is this the final fragment? */ |
| 310 | if ((flags & IP_MF) == 0) { |
| 311 | /* If we already have some bits beyond end |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 312 | * or have different end, the segment is corrupted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 314 | if (end < qp->q.len || |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 315 | ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | goto err; |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 317 | qp->q.flags |= INET_FRAG_LAST_IN; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 318 | qp->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } else { |
| 320 | if (end&7) { |
| 321 | end &= ~7; |
| 322 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
| 323 | skb->ip_summed = CHECKSUM_NONE; |
| 324 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 325 | if (end > qp->q.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | /* Some bits beyond end -> corruption. */ |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 327 | if (qp->q.flags & INET_FRAG_LAST_IN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | goto err; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 329 | qp->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | if (end == offset) |
| 333 | goto err; |
| 334 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 335 | err = -ENOMEM; |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 336 | if (!pskb_pull(skb, skb_network_offset(skb) + ihl)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | goto err; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 338 | |
| 339 | err = pskb_trim_rcsum(skb, end - offset); |
| 340 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | goto err; |
| 342 | |
| 343 | /* Find out which fragments are in front and at the back of us |
| 344 | * in the chain of fragments so far. We must know where to put |
| 345 | * this fragment, right? |
| 346 | */ |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 347 | prev = qp->q.fragments_tail; |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 348 | if (!prev || prev->ip_defrag_offset < offset) { |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 349 | next = NULL; |
| 350 | goto found; |
| 351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | prev = NULL; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 353 | for (next = qp->q.fragments; next != NULL; next = next->next) { |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 354 | if (next->ip_defrag_offset >= offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | break; /* bingo! */ |
| 356 | prev = next; |
| 357 | } |
| 358 | |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 359 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* We found where to put this one. Check for overlap with |
| 361 | * preceding fragment, and, if needed, align things so that |
| 362 | * any overlaps are eliminated. |
| 363 | */ |
| 364 | if (prev) { |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 365 | int i = (prev->ip_defrag_offset + prev->len) - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | if (i > 0) { |
| 368 | offset += i; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 369 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | if (end <= offset) |
| 371 | goto err; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 372 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (!pskb_pull(skb, i)) |
| 374 | goto err; |
| 375 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
| 376 | skb->ip_summed = CHECKSUM_NONE; |
| 377 | } |
| 378 | } |
| 379 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 380 | err = -ENOMEM; |
| 381 | |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 382 | while (next && next->ip_defrag_offset < end) { |
| 383 | int i = end - next->ip_defrag_offset; /* overlap is 'i' bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | if (i < next->len) { |
| 386 | /* Eat head of the next overlapped fragment |
| 387 | * and leave the loop. The next ones cannot overlap. |
| 388 | */ |
| 389 | if (!pskb_pull(next, i)) |
| 390 | goto err; |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 391 | next->ip_defrag_offset += i; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 392 | qp->q.meat -= i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (next->ip_summed != CHECKSUM_UNNECESSARY) |
| 394 | next->ip_summed = CHECKSUM_NONE; |
| 395 | break; |
| 396 | } else { |
| 397 | struct sk_buff *free_it = next; |
| 398 | |
Peter Zijlstra | 47c6bf77 | 2006-12-12 19:48:59 +0100 | [diff] [blame] | 399 | /* Old fragment is completely overridden with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | * new one drop it. |
| 401 | */ |
| 402 | next = next->next; |
| 403 | |
| 404 | if (prev) |
| 405 | prev->next = next; |
| 406 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 407 | qp->q.fragments = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 409 | qp->q.meat -= free_it->len; |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 410 | sub_frag_mem_limit(qp->q.net, free_it->truesize); |
Jesper Dangaard Brouer | d433673 | 2013-01-28 23:45:12 +0000 | [diff] [blame] | 411 | kfree_skb(free_it); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 415 | /* Note : skb->ip_defrag_offset and skb->dev share the same location */ |
| 416 | dev = skb->dev; |
| 417 | if (dev) |
| 418 | qp->iif = dev->ifindex; |
| 419 | /* Makes sure compiler wont do silly aliasing games */ |
| 420 | barrier(); |
| 421 | skb->ip_defrag_offset = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | /* Insert this fragment in the chain of fragments. */ |
| 424 | skb->next = next; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 425 | if (!next) |
| 426 | qp->q.fragments_tail = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (prev) |
| 428 | prev->next = skb; |
| 429 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 430 | qp->q.fragments = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 432 | qp->q.stamp = skb->tstamp; |
| 433 | qp->q.meat += skb->len; |
Eric Dumazet | 6623e3b | 2011-01-05 07:52:55 +0000 | [diff] [blame] | 434 | qp->ecn |= ecn; |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 435 | add_frag_mem_limit(qp->q.net, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (offset == 0) |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 437 | qp->q.flags |= INET_FRAG_FIRST_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 439 | fragsize = skb->len + ihl; |
| 440 | |
| 441 | if (fragsize > qp->q.max_size) |
| 442 | qp->q.max_size = fragsize; |
| 443 | |
Patrick McHardy | 5f2d04f | 2012-08-26 19:13:55 +0200 | [diff] [blame] | 444 | if (ip_hdr(skb)->frag_off & htons(IP_DF) && |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 445 | fragsize > qp->max_df_size) |
| 446 | qp->max_df_size = fragsize; |
Patrick McHardy | 5f2d04f | 2012-08-26 19:13:55 +0200 | [diff] [blame] | 447 | |
Nikolay Aleksandrov | 06aa8b8 | 2014-08-01 12:29:44 +0200 | [diff] [blame] | 448 | if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 449 | qp->q.meat == qp->q.len) { |
| 450 | unsigned long orefdst = skb->_skb_refdst; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 451 | |
Eric Dumazet | 97599dc | 2013-04-16 12:55:41 +0000 | [diff] [blame] | 452 | skb->_skb_refdst = 0UL; |
| 453 | err = ip_frag_reasm(qp, prev, dev); |
| 454 | skb->_skb_refdst = orefdst; |
| 455 | return err; |
| 456 | } |
| 457 | |
| 458 | skb_dst_drop(skb); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 459 | return -EINPROGRESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | err: |
| 462 | kfree_skb(skb); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 463 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
| 467 | /* Build a new IP datagram from all its fragments. */ |
| 468 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 469 | static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev, |
| 470 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Jorge Boncompte [DTI2] | 2bad35b | 2009-03-18 23:26:11 -0700 | [diff] [blame] | 472 | struct net *net = container_of(qp->q.net, struct net, ipv4.frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | struct iphdr *iph; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 474 | struct sk_buff *fp, *head = qp->q.fragments; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | int len; |
| 476 | int ihlen; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 477 | int err; |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 478 | u8 ecn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | ipq_kill(qp); |
| 481 | |
Hannes Frederic Sowa | be99197 | 2013-03-22 08:24:37 +0000 | [diff] [blame] | 482 | ecn = ip_frag_ecn_table[qp->ecn]; |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 483 | if (unlikely(ecn == 0xff)) { |
| 484 | err = -EINVAL; |
| 485 | goto out_fail; |
| 486 | } |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 487 | /* Make the one we just received the head. */ |
| 488 | if (prev) { |
| 489 | head = prev->next; |
| 490 | fp = skb_clone(head, GFP_ATOMIC); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 491 | if (!fp) |
| 492 | goto out_nomem; |
| 493 | |
| 494 | fp->next = head->next; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 495 | if (!fp->next) |
| 496 | qp->q.fragments_tail = fp; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 497 | prev->next = fp; |
| 498 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 499 | skb_morph(head, qp->q.fragments); |
| 500 | head->next = qp->q.fragments->next; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 501 | |
Eric Dumazet | cbf8f7b | 2012-04-19 06:10:26 +0000 | [diff] [blame] | 502 | consume_skb(qp->q.fragments); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 503 | qp->q.fragments = head; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 506 | WARN_ON(!head); |
Eric Dumazet | bf66337 | 2018-03-31 12:58:58 -0700 | [diff] [blame] | 507 | WARN_ON(head->ip_defrag_offset != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | /* Allocate a new buffer for the datagram. */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 510 | ihlen = ip_hdrlen(head); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 511 | len = ihlen + qp->q.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 513 | err = -E2BIG; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 514 | if (len > 65535) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | goto out_oversize; |
| 516 | |
| 517 | /* Head of list must not be cloned. */ |
Pravin B Shelar | 14bbd6a | 2013-02-14 09:44:49 +0000 | [diff] [blame] | 518 | if (skb_unclone(head, GFP_ATOMIC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | goto out_nomem; |
| 520 | |
| 521 | /* If the first fragment is fragmented itself, we split |
| 522 | * it to two chunks: the first with data and paged part |
| 523 | * and the second, holding only fragments. */ |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 524 | if (skb_has_frag_list(head)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct sk_buff *clone; |
| 526 | int i, plen = 0; |
| 527 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 528 | clone = alloc_skb(0, GFP_ATOMIC); |
| 529 | if (!clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | goto out_nomem; |
| 531 | clone->next = head->next; |
| 532 | head->next = clone; |
| 533 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; |
David S. Miller | d7fcf1a | 2009-06-09 00:19:37 -0700 | [diff] [blame] | 534 | skb_frag_list_init(head); |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 535 | for (i = 0; i < skb_shinfo(head)->nr_frags; i++) |
| 536 | plen += skb_frag_size(&skb_shinfo(head)->frags[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | clone->len = clone->data_len = head->data_len - plen; |
| 538 | head->data_len -= clone->len; |
| 539 | head->len -= clone->len; |
| 540 | clone->csum = 0; |
| 541 | clone->ip_summed = head->ip_summed; |
Florian Westphal | 0e60d24 | 2015-07-23 12:05:38 +0200 | [diff] [blame] | 542 | add_frag_mem_limit(qp->q.net, clone->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Florian Westphal | 14fe22e | 2015-07-11 01:37:36 +0200 | [diff] [blame] | 545 | skb_shinfo(head)->frag_list = head->next; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 546 | skb_push(head, head->data - skb_network_header(head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Florian Westphal | 14fe22e | 2015-07-11 01:37:36 +0200 | [diff] [blame] | 548 | for (fp=head->next; fp; fp = fp->next) { |
| 549 | head->data_len += fp->len; |
| 550 | head->len += fp->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (head->ip_summed != fp->ip_summed) |
| 552 | head->ip_summed = CHECKSUM_NONE; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 553 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | head->csum = csum_add(head->csum, fp->csum); |
Florian Westphal | 14fe22e | 2015-07-11 01:37:36 +0200 | [diff] [blame] | 555 | head->truesize += fp->truesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
David S. Miller | 5510b3c | 2015-07-31 23:52:20 -0700 | [diff] [blame] | 557 | sub_frag_mem_limit(qp->q.net, head->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | head->next = NULL; |
| 560 | head->dev = dev; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 561 | head->tstamp = qp->q.stamp; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 562 | IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 564 | iph = ip_hdr(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | iph->tot_len = htons(len); |
Eric Dumazet | 5173cc0 | 2011-05-16 08:37:37 +0000 | [diff] [blame] | 566 | iph->tos |= ecn; |
Florian Westphal | d6b915e | 2015-05-22 16:32:51 +0200 | [diff] [blame] | 567 | |
| 568 | /* When we set IP_DF on a refragmented skb we must also force a |
| 569 | * call to ip_fragment to avoid forwarding a DF-skb of size s while |
| 570 | * original sender only sent fragments of size f (where f < s). |
| 571 | * |
| 572 | * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest |
| 573 | * frag seen to avoid sending tiny DF-fragments in case skb was built |
| 574 | * from one very small df-fragment and one large non-df frag. |
| 575 | */ |
| 576 | if (qp->max_df_size == qp->q.max_size) { |
| 577 | IPCB(head)->flags |= IPSKB_FRAG_PMTU; |
| 578 | iph->frag_off = htons(IP_DF); |
| 579 | } else { |
| 580 | iph->frag_off = 0; |
| 581 | } |
| 582 | |
Edward Hyunkoo Jee | 0848f64 | 2015-07-21 09:43:59 +0200 | [diff] [blame] | 583 | ip_send_check(iph); |
| 584 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 585 | __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 586 | qp->q.fragments = NULL; |
Changli Gao | d6bebca | 2010-06-29 04:39:37 +0000 | [diff] [blame] | 587 | qp->q.fragments_tail = NULL; |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 588 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | out_nomem: |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 591 | net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp); |
David Howells | 4554247 | 2007-10-17 21:37:22 -0700 | [diff] [blame] | 592 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | goto out_fail; |
| 594 | out_oversize: |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 595 | net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | out_fail: |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 597 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 598 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | /* Process an incoming IP datagram fragment. */ |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 602 | int ip_defrag(struct net *net, struct sk_buff *skb, u32 user) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 604 | struct net_device *dev = skb->dev ? : skb_dst(skb)->dev; |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 605 | int vif = l3mdev_master_ifindex_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | struct ipq *qp; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 607 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 608 | __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS); |
Joe Stringer | 8282f27 | 2016-01-22 15:49:12 -0800 | [diff] [blame] | 609 | skb_orphan(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* Lookup (or create) queue header */ |
David Ahern | 9972f13 | 2015-08-13 14:59:09 -0600 | [diff] [blame] | 612 | qp = ip_find(net, ip_hdr(skb), user, vif); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 613 | if (qp) { |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 614 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 616 | spin_lock(&qp->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Herbert Xu | 1706d58 | 2007-10-14 00:38:15 -0700 | [diff] [blame] | 618 | ret = ip_frag_queue(qp, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 620 | spin_unlock(&qp->q.lock); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 621 | ipq_put(qp); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 622 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 625 | __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | kfree_skb(skb); |
Herbert Xu | 776c729 | 2007-10-14 00:38:32 -0700 | [diff] [blame] | 627 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 629 | EXPORT_SYMBOL(ip_defrag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 631 | struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 632 | { |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 633 | struct iphdr iph; |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 634 | int netoff; |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 635 | u32 len; |
| 636 | |
| 637 | if (skb->protocol != htons(ETH_P_IP)) |
| 638 | return skb; |
| 639 | |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 640 | netoff = skb_network_offset(skb); |
| 641 | |
| 642 | if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 643 | return skb; |
| 644 | |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 645 | if (iph.ihl < 5 || iph.version != 4) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 646 | return skb; |
| 647 | |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 648 | len = ntohs(iph.tot_len); |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 649 | if (skb->len < netoff + len || len < (iph.ihl * 4)) |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 650 | return skb; |
| 651 | |
| 652 | if (ip_is_fragment(&iph)) { |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 653 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 654 | if (skb) { |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 655 | if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) |
Johannes Berg | 1bf3751 | 2012-12-09 23:41:06 +0000 | [diff] [blame] | 656 | return skb; |
Alexander Drozdov | 3e32e73 | 2015-03-05 10:29:39 +0300 | [diff] [blame] | 657 | if (pskb_trim_rcsum(skb, netoff + len)) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 658 | return skb; |
| 659 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 660 | if (ip_defrag(net, skb, user)) |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 661 | return NULL; |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 662 | skb_clear_hash(skb); |
Eric Dumazet | bc416d9 | 2011-10-06 10:28:31 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | return skb; |
| 666 | } |
| 667 | EXPORT_SYMBOL(ip_check_defrag); |
| 668 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 669 | #ifdef CONFIG_SYSCTL |
Eric Dumazet | 3d23401 | 2018-04-04 08:35:10 -0700 | [diff] [blame] | 670 | static int dist_min; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 671 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 672 | static struct ctl_table ip4_frags_ns_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 673 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 674 | .procname = "ipfrag_high_thresh", |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 675 | .data = &init_net.ipv4.frags.high_thresh, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 676 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 677 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 678 | .proc_handler = proc_doulongvec_minmax, |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 679 | .extra1 = &init_net.ipv4.frags.low_thresh |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 680 | }, |
| 681 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 682 | .procname = "ipfrag_low_thresh", |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 683 | .data = &init_net.ipv4.frags.low_thresh, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 684 | .maxlen = sizeof(unsigned long), |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 685 | .mode = 0644, |
Eric Dumazet | 3e67f10 | 2018-03-31 12:58:53 -0700 | [diff] [blame] | 686 | .proc_handler = proc_doulongvec_minmax, |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 687 | .extra2 = &init_net.ipv4.frags.high_thresh |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 688 | }, |
| 689 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 690 | .procname = "ipfrag_time", |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 691 | .data = &init_net.ipv4.frags.timeout, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 692 | .maxlen = sizeof(int), |
| 693 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 694 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 695 | }, |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 696 | { |
| 697 | .procname = "ipfrag_max_dist", |
| 698 | .data = &init_net.ipv4.frags.max_dist, |
| 699 | .maxlen = sizeof(int), |
| 700 | .mode = 0644, |
| 701 | .proc_handler = proc_dointvec_minmax, |
Eric Dumazet | 3d23401 | 2018-04-04 08:35:10 -0700 | [diff] [blame] | 702 | .extra1 = &dist_min, |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 703 | }, |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 704 | { } |
| 705 | }; |
| 706 | |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 707 | /* secret interval has been deprecated */ |
| 708 | static int ip4_frags_secret_interval_unused; |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 709 | static struct ctl_table ip4_frags_ctl_table[] = { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 710 | { |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 711 | .procname = "ipfrag_secret_interval", |
Florian Westphal | e3a57d1 | 2014-07-24 16:50:35 +0200 | [diff] [blame] | 712 | .data = &ip4_frags_secret_interval_unused, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 713 | .maxlen = sizeof(int), |
| 714 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 715 | .proc_handler = proc_dointvec_jiffies, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 716 | }, |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 717 | { } |
| 718 | }; |
| 719 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 720 | static int __net_init ip4_frags_ns_ctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 721 | { |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 722 | struct ctl_table *table; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 723 | struct ctl_table_header *hdr; |
| 724 | |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 725 | table = ip4_frags_ns_ctl_table; |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 726 | if (!net_eq(net, &init_net)) { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 727 | table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 728 | if (!table) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 729 | goto err_alloc; |
| 730 | |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 731 | table[0].data = &net->ipv4.frags.high_thresh; |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 732 | table[0].extra1 = &net->ipv4.frags.low_thresh; |
| 733 | table[0].extra2 = &init_net.ipv4.frags.high_thresh; |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 734 | table[1].data = &net->ipv4.frags.low_thresh; |
Nikolay Aleksandrov | 1bab4c7 | 2014-07-24 16:50:37 +0200 | [diff] [blame] | 735 | table[1].extra2 = &net->ipv4.frags.high_thresh; |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 736 | table[2].data = &net->ipv4.frags.timeout; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 737 | table[3].data = &net->ipv4.frags.max_dist; |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 740 | hdr = register_net_sysctl(net, "net/ipv4", table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 741 | if (!hdr) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 742 | goto err_reg; |
| 743 | |
| 744 | net->ipv4.frags_hdr = hdr; |
| 745 | return 0; |
| 746 | |
| 747 | err_reg: |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 748 | if (!net_eq(net, &init_net)) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 749 | kfree(table); |
| 750 | err_alloc: |
| 751 | return -ENOMEM; |
| 752 | } |
| 753 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 754 | static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 755 | { |
| 756 | struct ctl_table *table; |
| 757 | |
| 758 | table = net->ipv4.frags_hdr->ctl_table_arg; |
| 759 | unregister_net_sysctl_table(net->ipv4.frags_hdr); |
| 760 | kfree(table); |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 761 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 762 | |
Fabian Frederick | 57a02c3 | 2014-10-01 19:18:57 +0200 | [diff] [blame] | 763 | static void __init ip4_frags_ctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 764 | { |
Eric W. Biederman | 4344475 | 2012-04-19 13:22:55 +0000 | [diff] [blame] | 765 | register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table); |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 766 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 767 | #else |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 768 | static int ip4_frags_ns_ctl_register(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 769 | { |
| 770 | return 0; |
| 771 | } |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 772 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 773 | static void ip4_frags_ns_ctl_unregister(struct net *net) |
Pavel Emelyanov | e4a2d5c | 2008-01-22 06:08:36 -0800 | [diff] [blame] | 774 | { |
| 775 | } |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 776 | |
Fabian Frederick | aa1f731 | 2014-11-04 20:44:04 +0100 | [diff] [blame] | 777 | static void __init ip4_frags_ctl_register(void) |
Pavel Emelyanov | 7d291eb | 2008-05-19 13:53:02 -0700 | [diff] [blame] | 778 | { |
| 779 | } |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 780 | #endif |
| 781 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 782 | static int __net_init ipv4_frags_init_net(struct net *net) |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 783 | { |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 784 | int res; |
| 785 | |
Jesper Dangaard Brouer | c2a9366 | 2013-01-15 07:16:35 +0000 | [diff] [blame] | 786 | /* Fragment cache limits. |
| 787 | * |
| 788 | * The fragment memory accounting code, (tries to) account for |
| 789 | * the real memory usage, by measuring both the size of frag |
| 790 | * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue)) |
| 791 | * and the SKB's truesize. |
| 792 | * |
| 793 | * A 64K fragment consumes 129736 bytes (44*2944)+200 |
| 794 | * (1500 truesize == 2944, sizeof(struct ipq) == 200) |
| 795 | * |
| 796 | * We will commit 4MB at one time. Should we cross that limit |
| 797 | * we will prune down to 3MB, making room for approx 8 big 64K |
| 798 | * fragments 8x128k. |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 799 | */ |
Jesper Dangaard Brouer | c2a9366 | 2013-01-15 07:16:35 +0000 | [diff] [blame] | 800 | net->ipv4.frags.high_thresh = 4 * 1024 * 1024; |
| 801 | net->ipv4.frags.low_thresh = 3 * 1024 * 1024; |
Pavel Emelyanov | e31e0bdc7 | 2008-01-22 06:10:13 -0800 | [diff] [blame] | 802 | /* |
Pavel Emelyanov | b2fd532 | 2008-01-22 06:09:37 -0800 | [diff] [blame] | 803 | * Important NOTE! Fragment queue must be destroyed before MSL expires. |
| 804 | * RFC791 is wrong proposing to prolongate timer each fragment arrival |
| 805 | * by TTL. |
| 806 | */ |
| 807 | net->ipv4.frags.timeout = IP_FRAG_TIME; |
| 808 | |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 809 | net->ipv4.frags.max_dist = 64; |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 810 | net->ipv4.frags.f = &ip4_frags; |
Nikolay Borisov | 0fbf4cb | 2016-02-15 12:11:31 +0200 | [diff] [blame] | 811 | |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 812 | res = inet_frags_init_net(&net->ipv4.frags); |
| 813 | if (res < 0) |
| 814 | return res; |
| 815 | res = ip4_frags_ns_ctl_register(net); |
| 816 | if (res < 0) |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 817 | inet_frags_exit_net(&net->ipv4.frags); |
Eric Dumazet | 787bea7 | 2018-03-31 12:58:43 -0700 | [diff] [blame] | 818 | return res; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 821 | static void __net_exit ipv4_frags_exit_net(struct net *net) |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 822 | { |
Pavel Emelyanov | 0a64b4b | 2008-05-19 13:51:29 -0700 | [diff] [blame] | 823 | ip4_frags_ns_ctl_unregister(net); |
Eric Dumazet | 093ba72 | 2018-03-31 12:58:44 -0700 | [diff] [blame] | 824 | inet_frags_exit_net(&net->ipv4.frags); |
Pavel Emelyanov | 81566e8 | 2008-01-22 06:12:39 -0800 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static struct pernet_operations ip4_frags_ops = { |
| 828 | .init = ipv4_frags_init_net, |
| 829 | .exit = ipv4_frags_exit_net, |
| 830 | }; |
| 831 | |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 832 | |
| 833 | static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed) |
| 834 | { |
| 835 | return jhash2(data, |
| 836 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); |
| 837 | } |
| 838 | |
| 839 | static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed) |
| 840 | { |
| 841 | const struct inet_frag_queue *fq = data; |
| 842 | |
| 843 | return jhash2((const u32 *)&fq->key.v4, |
| 844 | sizeof(struct frag_v4_compare_key) / sizeof(u32), seed); |
| 845 | } |
| 846 | |
| 847 | static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr) |
| 848 | { |
| 849 | const struct frag_v4_compare_key *key = arg->key; |
| 850 | const struct inet_frag_queue *fq = ptr; |
| 851 | |
| 852 | return !!memcmp(&fq->key, key, sizeof(*key)); |
| 853 | } |
| 854 | |
| 855 | static const struct rhashtable_params ip4_rhash_params = { |
| 856 | .head_offset = offsetof(struct inet_frag_queue, node), |
| 857 | .key_offset = offsetof(struct inet_frag_queue, key), |
| 858 | .key_len = sizeof(struct frag_v4_compare_key), |
| 859 | .hashfn = ip4_key_hashfn, |
| 860 | .obj_hashfn = ip4_obj_hashfn, |
| 861 | .obj_cmpfn = ip4_obj_cmpfn, |
| 862 | .automatic_shrinking = true, |
| 863 | }; |
| 864 | |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 865 | void __init ipfrag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 867 | ip4_frags.constructor = ip4_frag_init; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 868 | ip4_frags.destructor = ip4_frag_free; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 869 | ip4_frags.qsize = sizeof(struct ipq); |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 870 | ip4_frags.frag_expire = ip_expire; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 871 | ip4_frags.frags_cache_name = ip_frag_cache_name; |
Eric Dumazet | 648700f | 2018-03-31 12:58:49 -0700 | [diff] [blame] | 872 | ip4_frags.rhash_params = ip4_rhash_params; |
Nikolay Aleksandrov | d4ad4d2 | 2014-08-01 12:29:48 +0200 | [diff] [blame] | 873 | if (inet_frags_init(&ip4_frags)) |
| 874 | panic("IP: failed to allocate ip4_frags cache\n"); |
Eric Dumazet | 483a6e4 | 2018-03-31 12:58:47 -0700 | [diff] [blame] | 875 | ip4_frags_ctl_register(); |
| 876 | register_pernet_subsys(&ip4_frags_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |