blob: 0e8f8de77e710ce4faa964734fb6ccaef90f9bff [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;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000139 struct sk_buff *head = NULL;
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);
Eric Dumazet399d1402018-03-31 12:58:51 -0700155 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200156
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000157 if (!qp->q.flags & INET_FRAG_FIRST_IN)
Eric Dumazet399d1402018-03-31 12:58:51 -0700158 goto out;
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200159
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000160 /* sk_buff::dev and sk_buff::rbnode are unionized. So we
161 * pull the head out of the tree in order to be able to
162 * deal with head->dev.
163 */
164 if (qp->q.fragments) {
165 head = qp->q.fragments;
166 qp->q.fragments = head->next;
167 } else {
168 head = skb_rb_first(&qp->q.rb_fragments);
169 if (!head)
170 goto out;
171 rb_erase(&head->rbnode, &qp->q.rb_fragments);
172 memset(&head->rbnode, 0, sizeof(head->rbnode));
173 barrier();
174 }
175 if (head == qp->q.fragments_tail)
176 qp->q.fragments_tail = NULL;
177
178 sub_frag_mem_limit(qp->q.net, head->truesize);
179
Eric Dumazet399d1402018-03-31 12:58:51 -0700180 head->dev = dev_get_by_index_rcu(net, qp->iif);
181 if (!head->dev)
182 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700183
Shan Weie9017b52010-01-23 01:57:42 -0800184
Eric Dumazet399d1402018-03-31 12:58:51 -0700185 /* skb has no dst, perform route lookup again */
186 iph = ip_hdr(head);
187 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
David S. Millerc6cffba2012-07-26 11:14:38 +0000188 iph->tos, head->dev);
Eric Dumazet399d1402018-03-31 12:58:51 -0700189 if (err)
190 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000191
Eric Dumazet399d1402018-03-31 12:58:51 -0700192 /* Only an end host needs to send an ICMP
193 * "Fragment Reassembly Timeout" message, per RFC792.
194 */
195 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
196 (skb_rtable(head)->rt_type != RTN_LOCAL))
197 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700198
Eric Dumazet1eec5d52018-03-31 12:58:54 -0700199 spin_unlock(&qp->q.lock);
200 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
Eric Dumazet1eec5d52018-03-31 12:58:54 -0700201 goto out_rcu_unlock;
Shan Weie9017b52010-01-23 01:57:42 -0800202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700204 spin_unlock(&qp->q.lock);
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700205out_rcu_unlock:
206 rcu_read_unlock();
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000207 if (head)
208 kfree_skb(head);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700209 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700212/* Find the correct entry in the "incomplete datagrams" queue for
213 * this IP datagram, and create new one, if nothing is found.
214 */
David Ahern9972f132015-08-13 14:59:09 -0600215static struct ipq *ip_find(struct net *net, struct iphdr *iph,
216 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Eric Dumazet648700f2018-03-31 12:58:49 -0700218 struct frag_v4_compare_key key = {
219 .saddr = iph->saddr,
220 .daddr = iph->daddr,
221 .user = user,
222 .vif = vif,
223 .id = iph->id,
224 .protocol = iph->protocol,
225 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700226 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eric Dumazet648700f2018-03-31 12:58:49 -0700228 q = inet_frag_find(&net->ipv4.frags, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700229 if (!q)
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000230 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700231
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700232 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Herbert Xu89cee8b2005-12-13 23:14:27 -0800235/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100236static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800237{
238 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200239 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800240 unsigned int start, end;
241
242 int rc;
243
244 if (!peer || !max)
245 return 0;
246
247 start = qp->rid;
248 end = atomic_inc_return(&peer->rid);
249 qp->rid = end;
250
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000251 rc = qp->q.fragments_tail && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800252
253 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700254 struct net *net;
255
256 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700257 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800258 }
259
260 return rc;
261}
262
263static int ip_frag_reinit(struct ipq *qp)
264{
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000265 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800266
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800267 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300268 refcount_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800269 return -ETIMEDOUT;
270 }
271
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000272 sum_truesize = skb_rbtree_purge(&qp->q.rb_fragments);
Florian Westphal0e60d242015-07-23 12:05:38 +0200273 sub_frag_mem_limit(qp->q.net, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800274
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200275 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700276 qp->q.len = 0;
277 qp->q.meat = 0;
278 qp->q.fragments = NULL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000279 qp->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000280 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800281 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000282 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800283
284 return 0;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700288static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Peter Oskolkov7969e5c2018-08-02 23:34:37 +0000290 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000291 struct rb_node **rbn, *parent;
292 struct sk_buff *skb1;
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
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000355 /* Note : skb->rbnode and skb->dev share the same location. */
356 dev = skb->dev;
357 /* Makes sure compiler wont do silly aliasing games */
358 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Peter Oskolkov7969e5c2018-08-02 23:34:37 +0000360 /* RFC5722, Section 4, amended by Errata ID : 3089
361 * When reassembling an IPv6 datagram, if
362 * one or more its constituent fragments is determined to be an
363 * overlapping fragment, the entire datagram (and any constituent
364 * fragments) MUST be silently discarded.
365 *
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000366 * We do the same here for IPv4 (and increment an snmp counter).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000369 /* Find out where to put this fragment. */
370 skb1 = qp->q.fragments_tail;
371 if (!skb1) {
372 /* This is the first fragment we've received. */
373 rb_link_node(&skb->rbnode, NULL, &qp->q.rb_fragments.rb_node);
374 qp->q.fragments_tail = skb;
375 } else if ((skb1->ip_defrag_offset + skb1->len) < end) {
376 /* This is the common/special case: skb goes to the end. */
377 /* Detect and discard overlaps. */
378 if (offset < (skb1->ip_defrag_offset + skb1->len))
379 goto discard_qp;
380 /* Insert after skb1. */
381 rb_link_node(&skb->rbnode, &skb1->rbnode, &skb1->rbnode.rb_right);
382 qp->q.fragments_tail = skb;
383 } else {
384 /* Binary search. Note that skb can become the first fragment, but
385 * not the last (covered above). */
386 rbn = &qp->q.rb_fragments.rb_node;
387 do {
388 parent = *rbn;
389 skb1 = rb_to_skb(parent);
390 if (end <= skb1->ip_defrag_offset)
391 rbn = &parent->rb_left;
392 else if (offset >= skb1->ip_defrag_offset + skb1->len)
393 rbn = &parent->rb_right;
394 else /* Found an overlap with skb1. */
395 goto discard_qp;
396 } while (*rbn);
397 /* Here we have parent properly set, and rbn pointing to
398 * one of its NULL left/right children. Insert skb. */
399 rb_link_node(&skb->rbnode, parent, rbn);
400 }
401 rb_insert_color(&skb->rbnode, &qp->q.rb_fragments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Eric Dumazetbf663372018-03-31 12:58:58 -0700403 if (dev)
404 qp->iif = dev->ifindex;
Eric Dumazetbf663372018-03-31 12:58:58 -0700405 skb->ip_defrag_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700407 qp->q.stamp = skb->tstamp;
408 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000409 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200410 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200412 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Florian Westphald6b915e2015-05-22 16:32:51 +0200414 fragsize = skb->len + ihl;
415
416 if (fragsize > qp->q.max_size)
417 qp->q.max_size = fragsize;
418
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200419 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200420 fragsize > qp->max_df_size)
421 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200422
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200423 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000424 qp->q.meat == qp->q.len) {
425 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700426
Eric Dumazet97599dc2013-04-16 12:55:41 +0000427 skb->_skb_refdst = 0UL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000428 err = ip_frag_reasm(qp, skb, dev);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000429 skb->_skb_refdst = orefdst;
430 return err;
431 }
432
433 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700434 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Peter Oskolkov7969e5c2018-08-02 23:34:37 +0000436discard_qp:
437 inet_frag_kill(&qp->q);
438 err = -EINVAL;
439 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440err:
441 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700442 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* Build a new IP datagram from all its fragments. */
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000446static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
Herbert Xu1706d582007-10-14 00:38:15 -0700447 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700449 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct iphdr *iph;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000451 struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments);
452 struct sk_buff **nextp; /* To build frag_list. */
453 struct rb_node *rbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int len;
455 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700456 int err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000457 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 ipq_kill(qp);
460
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000461 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000462 if (unlikely(ecn == 0xff)) {
463 err = -EINVAL;
464 goto out_fail;
465 }
Herbert Xu1706d582007-10-14 00:38:15 -0700466 /* Make the one we just received the head. */
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000467 if (head != skb) {
468 fp = skb_clone(skb, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700469 if (!fp)
470 goto out_nomem;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000471 rb_replace_node(&skb->rbnode, &fp->rbnode, &qp->q.rb_fragments);
472 if (qp->q.fragments_tail == skb)
Changli Gaod6bebca2010-06-29 04:39:37 +0000473 qp->q.fragments_tail = fp;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000474 skb_morph(skb, head);
475 rb_replace_node(&head->rbnode, &skb->rbnode,
476 &qp->q.rb_fragments);
477 consume_skb(head);
478 head = skb;
Herbert Xu1706d582007-10-14 00:38:15 -0700479 }
480
Eric Dumazetbf663372018-03-31 12:58:58 -0700481 WARN_ON(head->ip_defrag_offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300484 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700485 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Herbert Xu1706d582007-10-14 00:38:15 -0700487 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800488 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto out_oversize;
490
491 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000492 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto out_nomem;
494
495 /* If the first fragment is fragmented itself, we split
496 * it to two chunks: the first with data and paged part
497 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700498 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct sk_buff *clone;
500 int i, plen = 0;
501
Ian Morris51456b22015-04-03 09:17:26 +0100502 clone = alloc_skb(0, GFP_ATOMIC);
503 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700506 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000507 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
508 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 clone->len = clone->data_len = head->data_len - plen;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000510 skb->truesize += clone->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 clone->csum = 0;
512 clone->ip_summed = head->ip_summed;
Florian Westphal0e60d242015-07-23 12:05:38 +0200513 add_frag_mem_limit(qp->q.net, clone->truesize);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000514 skb_shinfo(head)->frag_list = clone;
515 nextp = &clone->next;
516 } else {
517 nextp = &skb_shinfo(head)->frag_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700520 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000522 /* Traverse the tree in order, to build frag_list. */
523 rbn = rb_next(&head->rbnode);
524 rb_erase(&head->rbnode, &qp->q.rb_fragments);
525 while (rbn) {
526 struct rb_node *rbnext = rb_next(rbn);
527 fp = rb_to_skb(rbn);
528 rb_erase(rbn, &qp->q.rb_fragments);
529 rbn = rbnext;
530 *nextp = fp;
531 nextp = &fp->next;
532 fp->prev = NULL;
533 memset(&fp->rbnode, 0, sizeof(fp->rbnode));
Florian Westphal14fe22e2015-07-11 01:37:36 +0200534 head->data_len += fp->len;
535 head->len += fp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (head->ip_summed != fp->ip_summed)
537 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700538 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 head->csum = csum_add(head->csum, fp->csum);
Florian Westphal14fe22e2015-07-11 01:37:36 +0200540 head->truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
David S. Miller5510b3c2015-07-31 23:52:20 -0700542 sub_frag_mem_limit(qp->q.net, head->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000544 *nextp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 head->next = NULL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000546 head->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700548 head->tstamp = qp->q.stamp;
Florian Westphald6b915e2015-05-22 16:32:51 +0200549 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700551 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000553 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200554
555 /* When we set IP_DF on a refragmented skb we must also force a
556 * call to ip_fragment to avoid forwarding a DF-skb of size s while
557 * original sender only sent fragments of size f (where f < s).
558 *
559 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
560 * frag seen to avoid sending tiny DF-fragments in case skb was built
561 * from one very small df-fragment and one large non-df frag.
562 */
563 if (qp->max_df_size == qp->q.max_size) {
564 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
565 iph->frag_off = htons(IP_DF);
566 } else {
567 iph->frag_off = 0;
568 }
569
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200570 ip_send_check(iph);
571
Eric Dumazetb45386e2016-04-27 16:44:35 -0700572 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700573 qp->q.fragments = NULL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000574 qp->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000575 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800579 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700580 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto out_fail;
582out_oversize:
Eric Dumazet648700f2018-03-31 12:58:49 -0700583 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700585 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500590int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
David Ahern9972f132015-08-13 14:59:09 -0600592 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700593 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900595
Eric Dumazetb45386e2016-04-27 16:44:35 -0700596 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800597 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600600 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100601 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700602 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700604 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Herbert Xu1706d582007-10-14 00:38:15 -0700606 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700608 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700609 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700610 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Eric Dumazetb45386e2016-04-27 16:44:35 -0700613 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700615 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000617EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500619struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000620{
Johannes Berg1bf37512012-12-09 23:41:06 +0000621 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300622 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000623 u32 len;
624
625 if (skb->protocol != htons(ETH_P_IP))
626 return skb;
627
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300628 netoff = skb_network_offset(skb);
629
630 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000631 return skb;
632
Johannes Berg1bf37512012-12-09 23:41:06 +0000633 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000634 return skb;
635
Johannes Berg1bf37512012-12-09 23:41:06 +0000636 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300637 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000638 return skb;
639
640 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000641 skb = skb_share_check(skb, GFP_ATOMIC);
642 if (skb) {
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300643 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000644 return skb;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300645 if (pskb_trim_rcsum(skb, netoff + len))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000646 return skb;
647 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500648 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000649 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800650 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000651 }
652 }
653 return skb;
654}
655EXPORT_SYMBOL(ip_check_defrag);
656
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800657#ifdef CONFIG_SYSCTL
Eric Dumazet3d234012018-04-04 08:35:10 -0700658static int dist_min;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800659
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700660static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800661 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800662 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800663 .data = &init_net.ipv4.frags.high_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700664 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800665 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700666 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200667 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800668 },
669 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800670 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800671 .data = &init_net.ipv4.frags.low_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700672 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800673 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700674 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200675 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800676 },
677 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800678 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800679 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800680 .maxlen = sizeof(int),
681 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800682 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800683 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200684 {
685 .procname = "ipfrag_max_dist",
686 .data = &init_net.ipv4.frags.max_dist,
687 .maxlen = sizeof(int),
688 .mode = 0644,
689 .proc_handler = proc_dointvec_minmax,
Eric Dumazet3d234012018-04-04 08:35:10 -0700690 .extra1 = &dist_min,
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200691 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700692 { }
693};
694
Florian Westphale3a57d12014-07-24 16:50:35 +0200695/* secret interval has been deprecated */
696static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700697static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800698 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800699 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200700 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800701 .maxlen = sizeof(int),
702 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800703 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800704 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800705 { }
706};
707
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000708static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800709{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800710 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800711 struct ctl_table_header *hdr;
712
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700713 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800714 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700715 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100716 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800717 goto err_alloc;
718
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800719 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200720 table[0].extra1 = &net->ipv4.frags.low_thresh;
721 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800722 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200723 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800724 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200725 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800726 }
727
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000728 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100729 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800730 goto err_reg;
731
732 net->ipv4.frags_hdr = hdr;
733 return 0;
734
735err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800736 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800737 kfree(table);
738err_alloc:
739 return -ENOMEM;
740}
741
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000742static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800743{
744 struct ctl_table *table;
745
746 table = net->ipv4.frags_hdr->ctl_table_arg;
747 unregister_net_sysctl_table(net->ipv4.frags_hdr);
748 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800749}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700750
Fabian Frederick57a02c32014-10-01 19:18:57 +0200751static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700752{
Eric W. Biederman43444752012-04-19 13:22:55 +0000753 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700754}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800755#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100756static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800757{
758 return 0;
759}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800760
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100761static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800762{
763}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700764
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100765static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700766{
767}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800768#endif
769
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000770static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800771{
Eric Dumazet787bea72018-03-31 12:58:43 -0700772 int res;
773
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000774 /* Fragment cache limits.
775 *
776 * The fragment memory accounting code, (tries to) account for
777 * the real memory usage, by measuring both the size of frag
778 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
779 * and the SKB's truesize.
780 *
781 * A 64K fragment consumes 129736 bytes (44*2944)+200
782 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
783 *
784 * We will commit 4MB at one time. Should we cross that limit
785 * we will prune down to 3MB, making room for approx 8 big 64K
786 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800787 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000788 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
789 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800790 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800791 * Important NOTE! Fragment queue must be destroyed before MSL expires.
792 * RFC791 is wrong proposing to prolongate timer each fragment arrival
793 * by TTL.
794 */
795 net->ipv4.frags.timeout = IP_FRAG_TIME;
796
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200797 net->ipv4.frags.max_dist = 64;
Eric Dumazet093ba722018-03-31 12:58:44 -0700798 net->ipv4.frags.f = &ip4_frags;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200799
Eric Dumazet787bea72018-03-31 12:58:43 -0700800 res = inet_frags_init_net(&net->ipv4.frags);
801 if (res < 0)
802 return res;
803 res = ip4_frags_ns_ctl_register(net);
804 if (res < 0)
Eric Dumazet093ba722018-03-31 12:58:44 -0700805 inet_frags_exit_net(&net->ipv4.frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700806 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800807}
808
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000809static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800810{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700811 ip4_frags_ns_ctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700812 inet_frags_exit_net(&net->ipv4.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800813}
814
815static struct pernet_operations ip4_frags_ops = {
816 .init = ipv4_frags_init_net,
817 .exit = ipv4_frags_exit_net,
818};
819
Eric Dumazet648700f2018-03-31 12:58:49 -0700820
821static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
822{
823 return jhash2(data,
824 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
825}
826
827static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
828{
829 const struct inet_frag_queue *fq = data;
830
831 return jhash2((const u32 *)&fq->key.v4,
832 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
833}
834
835static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
836{
837 const struct frag_v4_compare_key *key = arg->key;
838 const struct inet_frag_queue *fq = ptr;
839
840 return !!memcmp(&fq->key, key, sizeof(*key));
841}
842
843static const struct rhashtable_params ip4_rhash_params = {
844 .head_offset = offsetof(struct inet_frag_queue, node),
845 .key_offset = offsetof(struct inet_frag_queue, key),
846 .key_len = sizeof(struct frag_v4_compare_key),
847 .hashfn = ip4_key_hashfn,
848 .obj_hashfn = ip4_obj_hashfn,
849 .obj_cmpfn = ip4_obj_cmpfn,
850 .automatic_shrinking = true,
851};
852
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700853void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700855 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700856 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700857 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700858 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200859 ip4_frags.frags_cache_name = ip_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700860 ip4_frags.rhash_params = ip4_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200861 if (inet_frags_init(&ip4_frags))
862 panic("IP: failed to allocate ip4_frags cache\n");
Eric Dumazet483a6e42018-03-31 12:58:47 -0700863 ip4_frags_ctl_register();
864 register_pernet_subsys(&ip4_frags_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}