blob: b0366224f314ae521d8c1f8fe04c53e419292b4c [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 -070060struct ipfrag_skb_cb
61{
62 struct inet_skb_parm h;
63 int offset;
64};
65
Jianjun Kongfd3f8c42008-11-03 02:47:38 -080066#define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Describe an entry in the "incomplete datagrams" queue. */
69struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070070 struct inet_frag_queue q;
71
Eric Dumazet6623e3b2011-01-05 07:52:55 +000072 u8 ecn; /* RFC3168 support */
Florian Westphald6b915e2015-05-22 16:32:51 +020073 u16 max_df_size; /* largest frag with DF set seen */
Herbert Xu89cee8b2005-12-13 23:14:27 -080074 int iif;
75 unsigned int rid;
76 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Fabian Frederickaa1f7312014-11-04 20:44:04 +010079static u8 ip4_frag_ecn(u8 tos)
Eric Dumazet6623e3b2011-01-05 07:52:55 +000080{
Eric Dumazet5173cc02011-05-16 08:37:37 +000081 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +000082}
83
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070084static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Herbert Xu1706d582007-10-14 00:38:15 -070086static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
87 struct net_device *dev);
88
Pavel Emelyanovabd65232007-10-17 19:47:21 -070089
Florian Westphal36c77782014-07-24 16:50:29 +020090static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070091{
92 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +000093 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
94 frags);
95 struct net *net = container_of(ipv4, struct net, ipv4);
96
Eric Dumazet648700f2018-03-31 12:58:49 -070097 const struct frag_v4_compare_key *key = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070098
Eric Dumazet648700f2018-03-31 12:58:49 -070099 q->key.v4 = *key;
100 qp->ecn = 0;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200101 qp->peer = q->net->max_dist ?
Eric Dumazet648700f2018-03-31 12:58:49 -0700102 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
David Ahern192132b2015-08-27 16:07:03 -0700103 NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700104}
105
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100106static void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700108 struct ipq *qp;
109
110 qp = container_of(q, struct ipq, q);
111 if (qp->peer)
112 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* Destruction primitives. */
117
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100118static void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Eric Dumazet093ba722018-03-31 12:58:44 -0700120 inet_frag_put(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/* Kill ipq entry. It is not destroyed immediately,
124 * because caller (and someone more) holds reference count.
125 */
126static void ipq_kill(struct ipq *ipq)
127{
Eric Dumazet093ba722018-03-31 12:58:44 -0700128 inet_frag_kill(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Andy Zhou5cf42282015-05-15 14:15:35 -0700131static bool frag_expire_skip_icmp(u32 user)
132{
133 return user == IP_DEFRAG_AF_PACKET ||
134 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
Andy Zhou8bc04862015-05-15 14:15:36 -0700135 __IP_DEFRAG_CONNTRACK_IN_END) ||
136 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
137 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
Andy Zhou5cf42282015-05-15 14:15:35 -0700138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
142 */
Kees Cook78802012017-10-16 17:29:20 -0700143static void ip_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Kees Cook78802012017-10-16 17:29:20 -0700145 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Eric Dumazet399d1402018-03-31 12:58:51 -0700146 struct sk_buff *clone, *head;
147 const struct iphdr *iph;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700148 struct net *net;
Eric Dumazet399d1402018-03-31 12:58:51 -0700149 struct ipq *qp;
150 int err;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700151
Kees Cook78802012017-10-16 17:29:20 -0700152 qp = container_of(frag, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700153 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700155 rcu_read_lock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700156 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200158 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
160
161 ipq_kill(qp);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700162 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Eric Dumazet399d1402018-03-31 12:58:51 -0700164 head = qp->q.fragments;
Denis V. Lunevcb846632008-03-24 15:31:00 -0700165
Eric Dumazet399d1402018-03-31 12:58:51 -0700166 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200167
Eric Dumazet399d1402018-03-31 12:58:51 -0700168 if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
169 goto out;
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200170
Eric Dumazet399d1402018-03-31 12:58:51 -0700171 head->dev = dev_get_by_index_rcu(net, qp->iif);
172 if (!head->dev)
173 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700174
Shan Weie9017b52010-01-23 01:57:42 -0800175
Eric Dumazet399d1402018-03-31 12:58:51 -0700176 /* skb has no dst, perform route lookup again */
177 iph = ip_hdr(head);
178 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
David S. Millerc6cffba2012-07-26 11:14:38 +0000179 iph->tos, head->dev);
Eric Dumazet399d1402018-03-31 12:58:51 -0700180 if (err)
181 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000182
Eric Dumazet399d1402018-03-31 12:58:51 -0700183 /* Only an end host needs to send an ICMP
184 * "Fragment Reassembly Timeout" message, per RFC792.
185 */
186 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
187 (skb_rtable(head)->rt_type != RTN_LOCAL))
188 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700189
Eric Dumazet399d1402018-03-31 12:58:51 -0700190 clone = skb_clone(head, GFP_ATOMIC);
Shan Weie9017b52010-01-23 01:57:42 -0800191
Eric Dumazet399d1402018-03-31 12:58:51 -0700192 /* Send an ICMP "Fragment Reassembly Timeout" message. */
193 if (clone) {
194 spin_unlock(&qp->q.lock);
195 icmp_send(clone, ICMP_TIME_EXCEEDED,
196 ICMP_EXC_FRAGTIME, 0);
197 consume_skb(clone);
198 goto out_rcu_unlock;
Patrick McHardyd1c9ae62010-02-02 11:46:50 -0800199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700201 spin_unlock(&qp->q.lock);
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700202out_rcu_unlock:
203 rcu_read_unlock();
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700204 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700207/* Find the correct entry in the "incomplete datagrams" queue for
208 * this IP datagram, and create new one, if nothing is found.
209 */
David Ahern9972f132015-08-13 14:59:09 -0600210static struct ipq *ip_find(struct net *net, struct iphdr *iph,
211 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Eric Dumazet648700f2018-03-31 12:58:49 -0700213 struct frag_v4_compare_key key = {
214 .saddr = iph->saddr,
215 .daddr = iph->daddr,
216 .user = user,
217 .vif = vif,
218 .id = iph->id,
219 .protocol = iph->protocol,
220 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700221 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Eric Dumazet648700f2018-03-31 12:58:49 -0700223 q = inet_frag_find(&net->ipv4.frags, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700224 if (!q)
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000225 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700226
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700227 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Herbert Xu89cee8b2005-12-13 23:14:27 -0800230/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100231static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800232{
233 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200234 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800235 unsigned int start, end;
236
237 int rc;
238
239 if (!peer || !max)
240 return 0;
241
242 start = qp->rid;
243 end = atomic_inc_return(&peer->rid);
244 qp->rid = end;
245
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700246 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800247
248 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700249 struct net *net;
250
251 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700252 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800253 }
254
255 return rc;
256}
257
258static int ip_frag_reinit(struct ipq *qp)
259{
260 struct sk_buff *fp;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000261 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800262
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800263 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300264 refcount_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800265 return -ETIMEDOUT;
266 }
267
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700268 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800269 do {
270 struct sk_buff *xp = fp->next;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000271
272 sum_truesize += fp->truesize;
273 kfree_skb(fp);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800274 fp = xp;
275 } while (fp);
Florian Westphal0e60d242015-07-23 12:05:38 +0200276 sub_frag_mem_limit(qp->q.net, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800277
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200278 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700279 qp->q.len = 0;
280 qp->q.meat = 0;
281 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000282 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800283 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000284 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800285
286 return 0;
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700290static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700293 struct net_device *dev;
Florian Westphald6b915e2015-05-22 16:32:51 +0200294 unsigned int fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int flags, offset;
296 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700297 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000298 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200300 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 goto err;
302
Herbert Xu89cee8b2005-12-13 23:14:27 -0800303 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700304 unlikely(ip_frag_too_far(qp)) &&
305 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800306 ipq_kill(qp);
307 goto err;
308 }
309
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000310 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700311 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 flags = offset & ~IP_OFFSET;
313 offset &= IP_OFFSET;
314 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300315 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Determine the position of this fragment. */
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200318 end = offset + skb->len - skb_network_offset(skb) - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700319 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* Is this the final fragment? */
322 if ((flags & IP_MF) == 0) {
323 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800324 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700326 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200327 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200329 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700330 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else {
332 if (end&7) {
333 end &= ~7;
334 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
335 skb->ip_summed = CHECKSUM_NONE;
336 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700337 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200339 if (qp->q.flags & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700341 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 }
344 if (end == offset)
345 goto err;
346
Herbert Xu1706d582007-10-14 00:38:15 -0700347 err = -ENOMEM;
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200348 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700350
351 err = pskb_trim_rcsum(skb, end - offset);
352 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto err;
354
355 /* Find out which fragments are in front and at the back of us
356 * in the chain of fragments so far. We must know where to put
357 * this fragment, right?
358 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000359 prev = qp->q.fragments_tail;
360 if (!prev || FRAG_CB(prev)->offset < offset) {
361 next = NULL;
362 goto found;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700365 for (next = qp->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (FRAG_CB(next)->offset >= offset)
367 break; /* bingo! */
368 prev = next;
369 }
370
Changli Gaod6bebca2010-06-29 04:39:37 +0000371found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* We found where to put this one. Check for overlap with
373 * preceding fragment, and, if needed, align things so that
374 * any overlaps are eliminated.
375 */
376 if (prev) {
377 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
378
379 if (i > 0) {
380 offset += i;
Herbert Xu1706d582007-10-14 00:38:15 -0700381 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (end <= offset)
383 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700384 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!pskb_pull(skb, i))
386 goto err;
387 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
388 skb->ip_summed = CHECKSUM_NONE;
389 }
390 }
391
Herbert Xu1706d582007-10-14 00:38:15 -0700392 err = -ENOMEM;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 while (next && FRAG_CB(next)->offset < end) {
395 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
396
397 if (i < next->len) {
398 /* Eat head of the next overlapped fragment
399 * and leave the loop. The next ones cannot overlap.
400 */
401 if (!pskb_pull(next, i))
402 goto err;
403 FRAG_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700404 qp->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (next->ip_summed != CHECKSUM_UNNECESSARY)
406 next->ip_summed = CHECKSUM_NONE;
407 break;
408 } else {
409 struct sk_buff *free_it = next;
410
Peter Zijlstra47c6bf772006-12-12 19:48:59 +0100411 /* Old fragment is completely overridden with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * new one drop it.
413 */
414 next = next->next;
415
416 if (prev)
417 prev->next = next;
418 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700419 qp->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700421 qp->q.meat -= free_it->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200422 sub_frag_mem_limit(qp->q.net, free_it->truesize);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000423 kfree_skb(free_it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
426
427 FRAG_CB(skb)->offset = offset;
428
429 /* Insert this fragment in the chain of fragments. */
430 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000431 if (!next)
432 qp->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (prev)
434 prev->next = skb;
435 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700436 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Herbert Xu1706d582007-10-14 00:38:15 -0700438 dev = skb->dev;
439 if (dev) {
440 qp->iif = dev->ifindex;
441 skb->dev = NULL;
442 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700443 qp->q.stamp = skb->tstamp;
444 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000445 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200446 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200448 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Florian Westphald6b915e2015-05-22 16:32:51 +0200450 fragsize = skb->len + ihl;
451
452 if (fragsize > qp->q.max_size)
453 qp->q.max_size = fragsize;
454
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200455 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200456 fragsize > qp->max_df_size)
457 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200458
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200459 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000460 qp->q.meat == qp->q.len) {
461 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700462
Eric Dumazet97599dc2013-04-16 12:55:41 +0000463 skb->_skb_refdst = 0UL;
464 err = ip_frag_reasm(qp, prev, dev);
465 skb->_skb_refdst = orefdst;
466 return err;
467 }
468
469 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700470 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472err:
473 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477
478/* Build a new IP datagram from all its fragments. */
479
Herbert Xu1706d582007-10-14 00:38:15 -0700480static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
481 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700483 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700485 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int len;
487 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700488 int err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000489 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 ipq_kill(qp);
492
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000493 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000494 if (unlikely(ecn == 0xff)) {
495 err = -EINVAL;
496 goto out_fail;
497 }
Herbert Xu1706d582007-10-14 00:38:15 -0700498 /* Make the one we just received the head. */
499 if (prev) {
500 head = prev->next;
501 fp = skb_clone(head, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700502 if (!fp)
503 goto out_nomem;
504
505 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000506 if (!fp->next)
507 qp->q.fragments_tail = fp;
Herbert Xu1706d582007-10-14 00:38:15 -0700508 prev->next = fp;
509
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700510 skb_morph(head, qp->q.fragments);
511 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700512
Eric Dumazetcbf8f7b2012-04-19 06:10:26 +0000513 consume_skb(qp->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700514 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700515 }
516
Ian Morris51456b22015-04-03 09:17:26 +0100517 WARN_ON(!head);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700518 WARN_ON(FRAG_CB(head)->offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300521 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700522 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Herbert Xu1706d582007-10-14 00:38:15 -0700524 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800525 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto out_oversize;
527
528 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000529 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto out_nomem;
531
532 /* If the first fragment is fragmented itself, we split
533 * it to two chunks: the first with data and paged part
534 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700535 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct sk_buff *clone;
537 int i, plen = 0;
538
Ian Morris51456b22015-04-03 09:17:26 +0100539 clone = alloc_skb(0, GFP_ATOMIC);
540 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 goto out_nomem;
542 clone->next = head->next;
543 head->next = clone;
544 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700545 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000546 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
547 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 clone->len = clone->data_len = head->data_len - plen;
549 head->data_len -= clone->len;
550 head->len -= clone->len;
551 clone->csum = 0;
552 clone->ip_summed = head->ip_summed;
Florian Westphal0e60d242015-07-23 12:05:38 +0200553 add_frag_mem_limit(qp->q.net, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
Florian Westphal14fe22e2015-07-11 01:37:36 +0200556 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700557 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Florian Westphal14fe22e2015-07-11 01:37:36 +0200559 for (fp=head->next; fp; fp = fp->next) {
560 head->data_len += fp->len;
561 head->len += fp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (head->ip_summed != fp->ip_summed)
563 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700564 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 head->csum = csum_add(head->csum, fp->csum);
Florian Westphal14fe22e2015-07-11 01:37:36 +0200566 head->truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
David S. Miller5510b3c2015-07-31 23:52:20 -0700568 sub_frag_mem_limit(qp->q.net, head->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 head->next = NULL;
571 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700572 head->tstamp = qp->q.stamp;
Florian Westphald6b915e2015-05-22 16:32:51 +0200573 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700575 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000577 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200578
579 /* When we set IP_DF on a refragmented skb we must also force a
580 * call to ip_fragment to avoid forwarding a DF-skb of size s while
581 * original sender only sent fragments of size f (where f < s).
582 *
583 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
584 * frag seen to avoid sending tiny DF-fragments in case skb was built
585 * from one very small df-fragment and one large non-df frag.
586 */
587 if (qp->max_df_size == qp->q.max_size) {
588 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
589 iph->frag_off = htons(IP_DF);
590 } else {
591 iph->frag_off = 0;
592 }
593
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200594 ip_send_check(iph);
595
Eric Dumazetb45386e2016-04-27 16:44:35 -0700596 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700597 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000598 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800602 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700603 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto out_fail;
605out_oversize:
Eric Dumazet648700f2018-03-31 12:58:49 -0700606 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700608 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500613int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
David Ahern9972f132015-08-13 14:59:09 -0600615 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700616 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900618
Eric Dumazetb45386e2016-04-27 16:44:35 -0700619 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800620 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600623 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100624 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700625 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700627 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Herbert Xu1706d582007-10-14 00:38:15 -0700629 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700631 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700632 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
Eric Dumazetb45386e2016-04-27 16:44:35 -0700636 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700638 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000640EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500642struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000643{
Johannes Berg1bf37512012-12-09 23:41:06 +0000644 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300645 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000646 u32 len;
647
648 if (skb->protocol != htons(ETH_P_IP))
649 return skb;
650
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300651 netoff = skb_network_offset(skb);
652
653 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000654 return skb;
655
Johannes Berg1bf37512012-12-09 23:41:06 +0000656 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000657 return skb;
658
Johannes Berg1bf37512012-12-09 23:41:06 +0000659 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300660 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000661 return skb;
662
663 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000664 skb = skb_share_check(skb, GFP_ATOMIC);
665 if (skb) {
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300666 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000667 return skb;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300668 if (pskb_trim_rcsum(skb, netoff + len))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000669 return skb;
670 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500671 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000672 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800673 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000674 }
675 }
676 return skb;
677}
678EXPORT_SYMBOL(ip_check_defrag);
679
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800680#ifdef CONFIG_SYSCTL
681static int zero;
682
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700683static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800684 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800685 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800686 .data = &init_net.ipv4.frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800687 .maxlen = sizeof(int),
688 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200689 .proc_handler = proc_dointvec_minmax,
690 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800691 },
692 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800693 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800694 .data = &init_net.ipv4.frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800695 .maxlen = sizeof(int),
696 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200697 .proc_handler = proc_dointvec_minmax,
698 .extra1 = &zero,
699 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800700 },
701 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800702 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800703 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800704 .maxlen = sizeof(int),
705 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800706 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800707 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200708 {
709 .procname = "ipfrag_max_dist",
710 .data = &init_net.ipv4.frags.max_dist,
711 .maxlen = sizeof(int),
712 .mode = 0644,
713 .proc_handler = proc_dointvec_minmax,
714 .extra1 = &zero
715 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700716 { }
717};
718
Florian Westphale3a57d12014-07-24 16:50:35 +0200719/* secret interval has been deprecated */
720static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700721static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800722 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800723 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200724 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800725 .maxlen = sizeof(int),
726 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800727 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800728 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800729 { }
730};
731
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000732static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800733{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800734 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800735 struct ctl_table_header *hdr;
736
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700737 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800738 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700739 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100740 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800741 goto err_alloc;
742
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800743 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200744 table[0].extra1 = &net->ipv4.frags.low_thresh;
745 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800746 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200747 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800748 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200749 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800750 }
751
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000752 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100753 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800754 goto err_reg;
755
756 net->ipv4.frags_hdr = hdr;
757 return 0;
758
759err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800760 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800761 kfree(table);
762err_alloc:
763 return -ENOMEM;
764}
765
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000766static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800767{
768 struct ctl_table *table;
769
770 table = net->ipv4.frags_hdr->ctl_table_arg;
771 unregister_net_sysctl_table(net->ipv4.frags_hdr);
772 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800773}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700774
Fabian Frederick57a02c32014-10-01 19:18:57 +0200775static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700776{
Eric W. Biederman43444752012-04-19 13:22:55 +0000777 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700778}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800779#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100780static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800781{
782 return 0;
783}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800784
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100785static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800786{
787}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700788
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100789static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700790{
791}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800792#endif
793
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000794static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800795{
Eric Dumazet787bea72018-03-31 12:58:43 -0700796 int res;
797
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000798 /* Fragment cache limits.
799 *
800 * The fragment memory accounting code, (tries to) account for
801 * the real memory usage, by measuring both the size of frag
802 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
803 * and the SKB's truesize.
804 *
805 * A 64K fragment consumes 129736 bytes (44*2944)+200
806 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
807 *
808 * We will commit 4MB at one time. Should we cross that limit
809 * we will prune down to 3MB, making room for approx 8 big 64K
810 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800811 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000812 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
813 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800814 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800815 * Important NOTE! Fragment queue must be destroyed before MSL expires.
816 * RFC791 is wrong proposing to prolongate timer each fragment arrival
817 * by TTL.
818 */
819 net->ipv4.frags.timeout = IP_FRAG_TIME;
820
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200821 net->ipv4.frags.max_dist = 64;
Eric Dumazet093ba722018-03-31 12:58:44 -0700822 net->ipv4.frags.f = &ip4_frags;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200823
Eric Dumazet787bea72018-03-31 12:58:43 -0700824 res = inet_frags_init_net(&net->ipv4.frags);
825 if (res < 0)
826 return res;
827 res = ip4_frags_ns_ctl_register(net);
828 if (res < 0)
Eric Dumazet093ba722018-03-31 12:58:44 -0700829 inet_frags_exit_net(&net->ipv4.frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700830 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800831}
832
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000833static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800834{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700835 ip4_frags_ns_ctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700836 inet_frags_exit_net(&net->ipv4.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800837}
838
839static struct pernet_operations ip4_frags_ops = {
840 .init = ipv4_frags_init_net,
841 .exit = ipv4_frags_exit_net,
842};
843
Eric Dumazet648700f2018-03-31 12:58:49 -0700844
845static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
846{
847 return jhash2(data,
848 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
849}
850
851static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
852{
853 const struct inet_frag_queue *fq = data;
854
855 return jhash2((const u32 *)&fq->key.v4,
856 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
857}
858
859static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
860{
861 const struct frag_v4_compare_key *key = arg->key;
862 const struct inet_frag_queue *fq = ptr;
863
864 return !!memcmp(&fq->key, key, sizeof(*key));
865}
866
867static const struct rhashtable_params ip4_rhash_params = {
868 .head_offset = offsetof(struct inet_frag_queue, node),
869 .key_offset = offsetof(struct inet_frag_queue, key),
870 .key_len = sizeof(struct frag_v4_compare_key),
871 .hashfn = ip4_key_hashfn,
872 .obj_hashfn = ip4_obj_hashfn,
873 .obj_cmpfn = ip4_obj_cmpfn,
874 .automatic_shrinking = true,
875};
876
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700877void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700879 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700880 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700881 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700882 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200883 ip4_frags.frags_cache_name = ip_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700884 ip4_frags.rhash_params = ip4_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200885 if (inet_frags_init(&ip4_frags))
886 panic("IP: failed to allocate ip4_frags cache\n");
Eric Dumazet483a6e42018-03-31 12:58:47 -0700887 ip4_frags_ctl_register();
888 register_pernet_subsys(&ip4_frags_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}