blob: 8e9528ebaa8e1af91172466cc161a83301a217ca [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Hideakie905a9e2007-02-09 23:24:47 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
Alan Cox113aa832008-10-13 19:01:08 -070010 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 Perchesafd465032012-03-12 07:03:32 +000024#define pr_fmt(fmt) "IPv4: " fmt
25
Herbert Xu89cee8b2005-12-13 23:14:27 -080026#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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 Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Shan Weie9017b52010-01-23 01:57:42 -080039#include <net/route.h>
40#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/sock.h>
42#include <net/ip.h>
43#include <net/icmp.h>
44#include <net/checksum.h>
Herbert Xu89cee8b2005-12-13 23:14:27 -080045#include <net/inetpeer.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070046#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/tcp.h>
48#include <linux/udp.h>
49#include <linux/inet.h>
50#include <linux/netfilter_ipv4.h>
Eric Dumazet6623e3b2011-01-05 07:52:55 +000051#include <net/inet_ecn.h>
David Ahern385add92015-09-29 20:07:13 -070052#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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 Aleksandrovd4ad4d22014-08-01 12:29:48 +020058static const char ip_frag_cache_name[] = "ip4-frags";
Herbert Xu89cee8b2005-12-13 23:14:27 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Describe an entry in the "incomplete datagrams" queue. */
61struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070062 struct inet_frag_queue q;
63
Eric Dumazet6623e3b2011-01-05 07:52:55 +000064 u8 ecn; /* RFC3168 support */
Florian Westphald6b915e2015-05-22 16:32:51 +020065 u16 max_df_size; /* largest frag with DF set seen */
Herbert Xu89cee8b2005-12-13 23:14:27 -080066 int iif;
67 unsigned int rid;
68 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Fabian Frederickaa1f7312014-11-04 20:44:04 +010071static u8 ip4_frag_ecn(u8 tos)
Eric Dumazet6623e3b2011-01-05 07:52:55 +000072{
Eric Dumazet5173cc02011-05-16 08:37:37 +000073 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +000074}
75
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070076static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Herbert Xu1706d582007-10-14 00:38:15 -070078static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
79 struct net_device *dev);
80
Pavel Emelyanovabd65232007-10-17 19:47:21 -070081
Florian Westphal36c77782014-07-24 16:50:29 +020082static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070083{
84 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +000085 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 Dumazet648700f2018-03-31 12:58:49 -070089 const struct frag_v4_compare_key *key = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070090
Eric Dumazet648700f2018-03-31 12:58:49 -070091 q->key.v4 = *key;
92 qp->ecn = 0;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020093 qp->peer = q->net->max_dist ?
Eric Dumazet648700f2018-03-31 12:58:49 -070094 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
David Ahern192132b2015-08-27 16:07:03 -070095 NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070096}
97
Fabian Frederickaa1f7312014-11-04 20:44:04 +010098static void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700100 struct ipq *qp;
101
102 qp = container_of(q, struct ipq, q);
103 if (qp->peer)
104 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* Destruction primitives. */
109
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100110static void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Eric Dumazet093ba722018-03-31 12:58:44 -0700112 inet_frag_put(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/* Kill ipq entry. It is not destroyed immediately,
116 * because caller (and someone more) holds reference count.
117 */
118static void ipq_kill(struct ipq *ipq)
119{
Eric Dumazet093ba722018-03-31 12:58:44 -0700120 inet_frag_kill(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Andy Zhou5cf42282015-05-15 14:15:35 -0700123static 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 Zhou8bc04862015-05-15 14:15:36 -0700127 __IP_DEFRAG_CONNTRACK_IN_END) ||
128 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
129 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
Andy Zhou5cf42282015-05-15 14:15:35 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
134 */
Kees Cook78802012017-10-16 17:29:20 -0700135static void ip_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Kees Cook78802012017-10-16 17:29:20 -0700137 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Eric Dumazet399d1402018-03-31 12:58:51 -0700138 const struct iphdr *iph;
Eric Dumazet1eec5d52018-03-31 12:58:54 -0700139 struct sk_buff *head;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700140 struct net *net;
Eric Dumazet399d1402018-03-31 12:58:51 -0700141 struct ipq *qp;
142 int err;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700143
Kees Cook78802012017-10-16 17:29:20 -0700144 qp = container_of(frag, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700145 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700147 rcu_read_lock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700148 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200150 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto out;
152
153 ipq_kill(qp);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700154 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Eric Dumazet399d1402018-03-31 12:58:51 -0700156 head = qp->q.fragments;
Denis V. Lunevcb846632008-03-24 15:31:00 -0700157
Eric Dumazet399d1402018-03-31 12:58:51 -0700158 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200159
Eric Dumazet399d1402018-03-31 12:58:51 -0700160 if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
161 goto out;
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200162
Eric Dumazet399d1402018-03-31 12:58:51 -0700163 head->dev = dev_get_by_index_rcu(net, qp->iif);
164 if (!head->dev)
165 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700166
Shan Weie9017b52010-01-23 01:57:42 -0800167
Eric Dumazet399d1402018-03-31 12:58:51 -0700168 /* 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. Millerc6cffba2012-07-26 11:14:38 +0000171 iph->tos, head->dev);
Eric Dumazet399d1402018-03-31 12:58:51 -0700172 if (err)
173 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000174
Eric Dumazet399d1402018-03-31 12:58:51 -0700175 /* 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 Dumazetec4fbd62017-03-22 08:57:15 -0700181
Eric Dumazet1eec5d52018-03-31 12:58:54 -0700182 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 Weie9017b52010-01-23 01:57:42 -0800187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700189 spin_unlock(&qp->q.lock);
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700190out_rcu_unlock:
191 rcu_read_unlock();
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700192 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700195/* Find the correct entry in the "incomplete datagrams" queue for
196 * this IP datagram, and create new one, if nothing is found.
197 */
David Ahern9972f132015-08-13 14:59:09 -0600198static struct ipq *ip_find(struct net *net, struct iphdr *iph,
199 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Eric Dumazet648700f2018-03-31 12:58:49 -0700201 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 Emelyanovc6fda282007-10-17 19:46:47 -0700209 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Eric Dumazet648700f2018-03-31 12:58:49 -0700211 q = inet_frag_find(&net->ipv4.frags, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700212 if (!q)
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000213 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700214
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700215 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Herbert Xu89cee8b2005-12-13 23:14:27 -0800218/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100219static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800220{
221 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200222 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800223 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 Emelyanov5ab11c92007-10-15 02:24:19 -0700234 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800235
236 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700237 struct net *net;
238
239 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700240 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800241 }
242
243 return rc;
244}
245
246static int ip_frag_reinit(struct ipq *qp)
247{
248 struct sk_buff *fp;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000249 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800250
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800251 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300252 refcount_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800253 return -ETIMEDOUT;
254 }
255
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700256 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800257 do {
258 struct sk_buff *xp = fp->next;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000259
260 sum_truesize += fp->truesize;
261 kfree_skb(fp);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800262 fp = xp;
263 } while (fp);
Florian Westphal0e60d242015-07-23 12:05:38 +0200264 sub_frag_mem_limit(qp->q.net, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800265
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200266 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700267 qp->q.len = 0;
268 qp->q.meat = 0;
269 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000270 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800271 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000272 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800273
274 return 0;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700278static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700281 struct net_device *dev;
Florian Westphald6b915e2015-05-22 16:32:51 +0200282 unsigned int fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int flags, offset;
284 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700285 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000286 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200288 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto err;
290
Herbert Xu89cee8b2005-12-13 23:14:27 -0800291 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700292 unlikely(ip_frag_too_far(qp)) &&
293 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800294 ipq_kill(qp);
295 goto err;
296 }
297
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000298 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700299 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 flags = offset & ~IP_OFFSET;
301 offset &= IP_OFFSET;
302 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300303 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Determine the position of this fragment. */
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200306 end = offset + skb->len - skb_network_offset(skb) - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700307 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Is this the final fragment? */
310 if ((flags & IP_MF) == 0) {
311 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800312 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700314 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200315 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200317 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700318 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else {
320 if (end&7) {
321 end &= ~7;
322 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
323 skb->ip_summed = CHECKSUM_NONE;
324 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700325 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200327 if (qp->q.flags & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700329 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 }
332 if (end == offset)
333 goto err;
334
Herbert Xu1706d582007-10-14 00:38:15 -0700335 err = -ENOMEM;
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200336 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700338
339 err = pskb_trim_rcsum(skb, end - offset);
340 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 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 Gaod6bebca2010-06-29 04:39:37 +0000347 prev = qp->q.fragments_tail;
Eric Dumazetbf663372018-03-31 12:58:58 -0700348 if (!prev || prev->ip_defrag_offset < offset) {
Changli Gaod6bebca2010-06-29 04:39:37 +0000349 next = NULL;
350 goto found;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700353 for (next = qp->q.fragments; next != NULL; next = next->next) {
Eric Dumazetbf663372018-03-31 12:58:58 -0700354 if (next->ip_defrag_offset >= offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break; /* bingo! */
356 prev = next;
357 }
358
Changli Gaod6bebca2010-06-29 04:39:37 +0000359found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* 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 Dumazetbf663372018-03-31 12:58:58 -0700365 int i = (prev->ip_defrag_offset + prev->len) - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (i > 0) {
368 offset += i;
Herbert Xu1706d582007-10-14 00:38:15 -0700369 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (end <= offset)
371 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700372 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 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 Xu1706d582007-10-14 00:38:15 -0700380 err = -ENOMEM;
381
Eric Dumazetbf663372018-03-31 12:58:58 -0700382 while (next && next->ip_defrag_offset < end) {
383 int i = end - next->ip_defrag_offset; /* overlap is 'i' bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
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 Dumazetbf663372018-03-31 12:58:58 -0700391 next->ip_defrag_offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700392 qp->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 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 Zijlstra47c6bf772006-12-12 19:48:59 +0100399 /* Old fragment is completely overridden with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * new one drop it.
401 */
402 next = next->next;
403
404 if (prev)
405 prev->next = next;
406 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700407 qp->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700409 qp->q.meat -= free_it->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200410 sub_frag_mem_limit(qp->q.net, free_it->truesize);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000411 kfree_skb(free_it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413 }
414
Eric Dumazetbf663372018-03-31 12:58:58 -0700415 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* Insert this fragment in the chain of fragments. */
424 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000425 if (!next)
426 qp->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (prev)
428 prev->next = skb;
429 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700430 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700432 qp->q.stamp = skb->tstamp;
433 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000434 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200435 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200437 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Florian Westphald6b915e2015-05-22 16:32:51 +0200439 fragsize = skb->len + ihl;
440
441 if (fragsize > qp->q.max_size)
442 qp->q.max_size = fragsize;
443
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200444 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200445 fragsize > qp->max_df_size)
446 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200447
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200448 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000449 qp->q.meat == qp->q.len) {
450 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700451
Eric Dumazet97599dc2013-04-16 12:55:41 +0000452 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 Xu1706d582007-10-14 00:38:15 -0700459 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461err:
462 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700463 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466
467/* Build a new IP datagram from all its fragments. */
468
Herbert Xu1706d582007-10-14 00:38:15 -0700469static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
470 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700472 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700474 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int len;
476 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700477 int err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000478 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 ipq_kill(qp);
481
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000482 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000483 if (unlikely(ecn == 0xff)) {
484 err = -EINVAL;
485 goto out_fail;
486 }
Herbert Xu1706d582007-10-14 00:38:15 -0700487 /* Make the one we just received the head. */
488 if (prev) {
489 head = prev->next;
490 fp = skb_clone(head, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700491 if (!fp)
492 goto out_nomem;
493
494 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000495 if (!fp->next)
496 qp->q.fragments_tail = fp;
Herbert Xu1706d582007-10-14 00:38:15 -0700497 prev->next = fp;
498
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700499 skb_morph(head, qp->q.fragments);
500 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700501
Eric Dumazetcbf8f7b2012-04-19 06:10:26 +0000502 consume_skb(qp->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700503 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700504 }
505
Ian Morris51456b22015-04-03 09:17:26 +0100506 WARN_ON(!head);
Eric Dumazetbf663372018-03-31 12:58:58 -0700507 WARN_ON(head->ip_defrag_offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300510 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700511 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Herbert Xu1706d582007-10-14 00:38:15 -0700513 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800514 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto out_oversize;
516
517 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000518 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 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. Miller21dc3302010-08-23 00:13:46 -0700524 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct sk_buff *clone;
526 int i, plen = 0;
527
Ian Morris51456b22015-04-03 09:17:26 +0100528 clone = alloc_skb(0, GFP_ATOMIC);
529 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 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. Millerd7fcf1a2009-06-09 00:19:37 -0700534 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000535 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
536 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 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 Westphal0e60d242015-07-23 12:05:38 +0200542 add_frag_mem_limit(qp->q.net, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
Florian Westphal14fe22e2015-07-11 01:37:36 +0200545 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700546 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Florian Westphal14fe22e2015-07-11 01:37:36 +0200548 for (fp=head->next; fp; fp = fp->next) {
549 head->data_len += fp->len;
550 head->len += fp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (head->ip_summed != fp->ip_summed)
552 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700553 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 head->csum = csum_add(head->csum, fp->csum);
Florian Westphal14fe22e2015-07-11 01:37:36 +0200555 head->truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
David S. Miller5510b3c2015-07-31 23:52:20 -0700557 sub_frag_mem_limit(qp->q.net, head->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 head->next = NULL;
560 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700561 head->tstamp = qp->q.stamp;
Florian Westphald6b915e2015-05-22 16:32:51 +0200562 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700564 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000566 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200567
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 Jee0848f642015-07-21 09:43:59 +0200583 ip_send_check(iph);
584
Eric Dumazetb45386e2016-04-27 16:44:35 -0700585 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700586 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000587 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800591 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700592 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto out_fail;
594out_oversize:
Eric Dumazet648700f2018-03-31 12:58:49 -0700595 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700597 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500602int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
David Ahern9972f132015-08-13 14:59:09 -0600604 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700605 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900607
Eric Dumazetb45386e2016-04-27 16:44:35 -0700608 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800609 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600612 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100613 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700614 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700616 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Herbert Xu1706d582007-10-14 00:38:15 -0700618 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700620 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700621 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Eric Dumazetb45386e2016-04-27 16:44:35 -0700625 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700627 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000629EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500631struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000632{
Johannes Berg1bf37512012-12-09 23:41:06 +0000633 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300634 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000635 u32 len;
636
637 if (skb->protocol != htons(ETH_P_IP))
638 return skb;
639
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300640 netoff = skb_network_offset(skb);
641
642 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000643 return skb;
644
Johannes Berg1bf37512012-12-09 23:41:06 +0000645 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000646 return skb;
647
Johannes Berg1bf37512012-12-09 23:41:06 +0000648 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300649 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000650 return skb;
651
652 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000653 skb = skb_share_check(skb, GFP_ATOMIC);
654 if (skb) {
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300655 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000656 return skb;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300657 if (pskb_trim_rcsum(skb, netoff + len))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000658 return skb;
659 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500660 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000661 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800662 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000663 }
664 }
665 return skb;
666}
667EXPORT_SYMBOL(ip_check_defrag);
668
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800669#ifdef CONFIG_SYSCTL
Eric Dumazet3d234012018-04-04 08:35:10 -0700670static int dist_min;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800671
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700672static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800673 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800674 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800675 .data = &init_net.ipv4.frags.high_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700676 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800677 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700678 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200679 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800680 },
681 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800682 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800683 .data = &init_net.ipv4.frags.low_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700684 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800685 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700686 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200687 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800688 },
689 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800690 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800691 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800692 .maxlen = sizeof(int),
693 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800694 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800695 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200696 {
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 Dumazet3d234012018-04-04 08:35:10 -0700702 .extra1 = &dist_min,
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200703 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700704 { }
705};
706
Florian Westphale3a57d12014-07-24 16:50:35 +0200707/* secret interval has been deprecated */
708static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700709static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800710 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800711 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200712 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800713 .maxlen = sizeof(int),
714 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800715 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800716 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800717 { }
718};
719
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000720static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800721{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800722 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800723 struct ctl_table_header *hdr;
724
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700725 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800726 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700727 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100728 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800729 goto err_alloc;
730
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800731 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200732 table[0].extra1 = &net->ipv4.frags.low_thresh;
733 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800734 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200735 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800736 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200737 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800738 }
739
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000740 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100741 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800742 goto err_reg;
743
744 net->ipv4.frags_hdr = hdr;
745 return 0;
746
747err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800748 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800749 kfree(table);
750err_alloc:
751 return -ENOMEM;
752}
753
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000754static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800755{
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 Emelyanov8d8354d2008-01-22 05:58:31 -0800761}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700762
Fabian Frederick57a02c32014-10-01 19:18:57 +0200763static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700764{
Eric W. Biederman43444752012-04-19 13:22:55 +0000765 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700766}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800767#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100768static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800769{
770 return 0;
771}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800772
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100773static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800774{
775}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700776
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100777static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700778{
779}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800780#endif
781
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000782static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800783{
Eric Dumazet787bea72018-03-31 12:58:43 -0700784 int res;
785
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000786 /* 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 Emelyanove31e0bdc72008-01-22 06:10:13 -0800799 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000800 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
801 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800802 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800803 * 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 Borisov0fbf4cb2016-02-15 12:11:31 +0200809 net->ipv4.frags.max_dist = 64;
Eric Dumazet093ba722018-03-31 12:58:44 -0700810 net->ipv4.frags.f = &ip4_frags;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200811
Eric Dumazet787bea72018-03-31 12:58:43 -0700812 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 Dumazet093ba722018-03-31 12:58:44 -0700817 inet_frags_exit_net(&net->ipv4.frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700818 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800819}
820
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000821static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800822{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700823 ip4_frags_ns_ctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700824 inet_frags_exit_net(&net->ipv4.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800825}
826
827static struct pernet_operations ip4_frags_ops = {
828 .init = ipv4_frags_init_net,
829 .exit = ipv4_frags_exit_net,
830};
831
Eric Dumazet648700f2018-03-31 12:58:49 -0700832
833static 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
839static 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
847static 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
855static 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 Dumazetb7aa0bf2007-04-19 16:16:32 -0700865void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700867 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700868 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700869 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700870 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200871 ip4_frags.frags_cache_name = ip_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700872 ip4_frags.rhash_params = ip4_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200873 if (inet_frags_init(&ip4_frags))
874 panic("IP: failed to allocate ip4_frags cache\n");
Eric Dumazet483a6e42018-03-31 12:58:47 -0700875 ip4_frags_ctl_register();
876 register_pernet_subsys(&ip4_frags_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}