blob: 8bfb34e9ea32fe60519d7134350563e3a60f879d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP fragmentation functionality.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
Alan Cox113aa832008-10-13 19:01:08 -07009 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Fixes:
12 * Alan Cox : Split from ip.c , see ip_input.c for history.
13 * David S. Miller : Begin massive cleanup...
14 * Andi Kleen : Add sysctls.
15 * xxxx : Overlapfrag bug.
16 * Ultima : ip_expire() kernel panic.
17 * Bill Hawes : Frag accounting and evictor fixes.
18 * John McDonald : 0 length frag bug.
19 * Alexey Kuznetsov: SMP races, threading, cleanup.
20 * Patrick McHardy : LRU queue of frag heads for evictor.
21 */
22
Joe Perchesafd465032012-03-12 07:03:32 +000023#define pr_fmt(fmt) "IPv4: " fmt
24
Herbert Xu89cee8b2005-12-13 23:14:27 -080025#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/jiffies.h>
30#include <linux/skbuff.h>
31#include <linux/list.h>
32#include <linux/ip.h>
33#include <linux/icmp.h>
34#include <linux/netdevice.h>
35#include <linux/jhash.h>
36#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Shan Weie9017b52010-01-23 01:57:42 -080038#include <net/route.h>
39#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <net/sock.h>
41#include <net/ip.h>
42#include <net/icmp.h>
43#include <net/checksum.h>
Herbert Xu89cee8b2005-12-13 23:14:27 -080044#include <net/inetpeer.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070045#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/tcp.h>
47#include <linux/udp.h>
48#include <linux/inet.h>
49#include <linux/netfilter_ipv4.h>
Eric Dumazet6623e3b2011-01-05 07:52:55 +000050#include <net/inet_ecn.h>
David Ahern385add92015-09-29 20:07:13 -070051#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
54 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
55 * as well. Or notify me, at least. --ANK
56 */
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020057static const char ip_frag_cache_name[] = "ip4-frags";
Herbert Xu89cee8b2005-12-13 23:14:27 -080058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* Describe an entry in the "incomplete datagrams" queue. */
60struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070061 struct inet_frag_queue q;
62
Eric Dumazet6623e3b2011-01-05 07:52:55 +000063 u8 ecn; /* RFC3168 support */
Florian Westphald6b915e2015-05-22 16:32:51 +020064 u16 max_df_size; /* largest frag with DF set seen */
Herbert Xu89cee8b2005-12-13 23:14:27 -080065 int iif;
66 unsigned int rid;
67 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Fabian Frederickaa1f7312014-11-04 20:44:04 +010070static u8 ip4_frag_ecn(u8 tos)
Eric Dumazet6623e3b2011-01-05 07:52:55 +000071{
Eric Dumazet5173cc02011-05-16 08:37:37 +000072 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +000073}
74
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070075static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Herbert Xu1706d582007-10-14 00:38:15 -070077static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
78 struct net_device *dev);
79
Pavel Emelyanovabd65232007-10-17 19:47:21 -070080
Florian Westphal36c77782014-07-24 16:50:29 +020081static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070082{
83 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +000084 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
85 frags);
86 struct net *net = container_of(ipv4, struct net, ipv4);
87
Eric Dumazet23ce9c52018-10-10 12:29:56 -070088 const struct frag_v4_compare_key *key = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070089
Eric Dumazet23ce9c52018-10-10 12:29:56 -070090 q->key.v4 = *key;
91 qp->ecn = 0;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020092 qp->peer = q->net->max_dist ?
Eric Dumazet23ce9c52018-10-10 12:29:56 -070093 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
David Ahern192132b2015-08-27 16:07:03 -070094 NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070095}
96
Fabian Frederickaa1f7312014-11-04 20:44:04 +010097static void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070099 struct ipq *qp;
100
101 qp = container_of(q, struct ipq, q);
102 if (qp->peer)
103 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Destruction primitives. */
108
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100109static void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700111 inet_frag_put(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/* Kill ipq entry. It is not destroyed immediately,
115 * because caller (and someone more) holds reference count.
116 */
117static void ipq_kill(struct ipq *ipq)
118{
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700119 inet_frag_kill(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Andy Zhou5cf42282015-05-15 14:15:35 -0700122static bool frag_expire_skip_icmp(u32 user)
123{
124 return user == IP_DEFRAG_AF_PACKET ||
125 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
Andy Zhou8bc04862015-05-15 14:15:36 -0700126 __IP_DEFRAG_CONNTRACK_IN_END) ||
127 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
128 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
Andy Zhou5cf42282015-05-15 14:15:35 -0700129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
132 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
133 */
134static void ip_expire(unsigned long arg)
135{
Eric Dumazet49106f32018-10-10 12:29:58 -0700136 const struct iphdr *iph;
Eric Dumazet7a87ec92018-10-10 12:30:01 -0700137 struct sk_buff *head;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700138 struct net *net;
Eric Dumazet49106f32018-10-10 12:29:58 -0700139 struct ipq *qp;
140 int err;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700141
142 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700143 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Eric Dumazet76568712017-03-22 08:57:15 -0700145 rcu_read_lock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700146 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200148 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out;
150
151 ipq_kill(qp);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700152 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric Dumazet49106f32018-10-10 12:29:58 -0700154 head = qp->q.fragments;
Denis V. Lunevcb846632008-03-24 15:31:00 -0700155
Eric Dumazet49106f32018-10-10 12:29:58 -0700156 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200157
Eric Dumazet49106f32018-10-10 12:29:58 -0700158 if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
159 goto out;
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200160
Eric Dumazet49106f32018-10-10 12:29:58 -0700161 head->dev = dev_get_by_index_rcu(net, qp->iif);
162 if (!head->dev)
163 goto out;
Eric Dumazet76568712017-03-22 08:57:15 -0700164
Shan Weie9017b52010-01-23 01:57:42 -0800165
Eric Dumazet49106f32018-10-10 12:29:58 -0700166 /* skb has no dst, perform route lookup again */
167 iph = ip_hdr(head);
168 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
David S. Millerc6cffba2012-07-26 11:14:38 +0000169 iph->tos, head->dev);
Eric Dumazet49106f32018-10-10 12:29:58 -0700170 if (err)
171 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000172
Eric Dumazet49106f32018-10-10 12:29:58 -0700173 /* Only an end host needs to send an ICMP
174 * "Fragment Reassembly Timeout" message, per RFC792.
175 */
176 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
177 (skb_rtable(head)->rt_type != RTN_LOCAL))
178 goto out;
Eric Dumazet76568712017-03-22 08:57:15 -0700179
Eric Dumazet7a87ec92018-10-10 12:30:01 -0700180 skb_get(head);
181 spin_unlock(&qp->q.lock);
182 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
183 kfree_skb(head);
184 goto out_rcu_unlock;
Shan Weie9017b52010-01-23 01:57:42 -0800185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700187 spin_unlock(&qp->q.lock);
Eric Dumazet76568712017-03-22 08:57:15 -0700188out_rcu_unlock:
189 rcu_read_unlock();
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700190 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700193/* Find the correct entry in the "incomplete datagrams" queue for
194 * this IP datagram, and create new one, if nothing is found.
195 */
David Ahern9972f132015-08-13 14:59:09 -0600196static struct ipq *ip_find(struct net *net, struct iphdr *iph,
197 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700199 struct frag_v4_compare_key key = {
200 .saddr = iph->saddr,
201 .daddr = iph->daddr,
202 .user = user,
203 .vif = vif,
204 .id = iph->id,
205 .protocol = iph->protocol,
206 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700207 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700209 q = inet_frag_find(&net->ipv4.frags, &key);
Eric Dumazet965e2ad2018-10-10 12:29:59 -0700210 if (!q)
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000211 return NULL;
Eric Dumazet965e2ad2018-10-10 12:29:59 -0700212
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700213 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Herbert Xu89cee8b2005-12-13 23:14:27 -0800216/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100217static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800218{
219 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200220 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800221 unsigned int start, end;
222
223 int rc;
224
225 if (!peer || !max)
226 return 0;
227
228 start = qp->rid;
229 end = atomic_inc_return(&peer->rid);
230 qp->rid = end;
231
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700232 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800233
234 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700235 struct net *net;
236
237 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700238 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800239 }
240
241 return rc;
242}
243
244static int ip_frag_reinit(struct ipq *qp)
245{
246 struct sk_buff *fp;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000247 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800248
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800249 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700250 atomic_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800251 return -ETIMEDOUT;
252 }
253
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700254 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800255 do {
256 struct sk_buff *xp = fp->next;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000257
258 sum_truesize += fp->truesize;
259 kfree_skb(fp);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800260 fp = xp;
261 } while (fp);
Florian Westphal0e60d242015-07-23 12:05:38 +0200262 sub_frag_mem_limit(qp->q.net, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800263
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200264 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700265 qp->q.len = 0;
266 qp->q.meat = 0;
267 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000268 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800269 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000270 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800271
272 return 0;
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700276static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700278 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700280 struct net_device *dev;
Florian Westphald6b915e2015-05-22 16:32:51 +0200281 unsigned int fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int flags, offset;
283 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700284 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000285 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200287 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto err;
289
Herbert Xu89cee8b2005-12-13 23:14:27 -0800290 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700291 unlikely(ip_frag_too_far(qp)) &&
292 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800293 ipq_kill(qp);
294 goto err;
295 }
296
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000297 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700298 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 flags = offset & ~IP_OFFSET;
300 offset &= IP_OFFSET;
301 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300302 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Determine the position of this fragment. */
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200305 end = offset + skb->len - skb_network_offset(skb) - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700306 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* Is this the final fragment? */
309 if ((flags & IP_MF) == 0) {
310 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800311 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700313 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200314 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200316 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700317 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 } else {
319 if (end&7) {
320 end &= ~7;
321 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
322 skb->ip_summed = CHECKSUM_NONE;
323 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700324 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200326 if (qp->q.flags & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700328 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 }
331 if (end == offset)
332 goto err;
333
Herbert Xu1706d582007-10-14 00:38:15 -0700334 err = -ENOMEM;
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200335 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700337
338 err = pskb_trim_rcsum(skb, end - offset);
339 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto err;
341
342 /* Find out which fragments are in front and at the back of us
343 * in the chain of fragments so far. We must know where to put
344 * this fragment, right?
345 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000346 prev = qp->q.fragments_tail;
Eric Dumazetd8384862018-10-10 12:30:06 -0700347 if (!prev || prev->ip_defrag_offset < offset) {
Changli Gaod6bebca2010-06-29 04:39:37 +0000348 next = NULL;
349 goto found;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 for (next = qp->q.fragments; next != NULL; next = next->next) {
Eric Dumazetd8384862018-10-10 12:30:06 -0700353 if (next->ip_defrag_offset >= offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break; /* bingo! */
355 prev = next;
356 }
357
Changli Gaod6bebca2010-06-29 04:39:37 +0000358found:
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700359 /* RFC5722, Section 4, amended by Errata ID : 3089
360 * When reassembling an IPv6 datagram, if
361 * one or more its constituent fragments is determined to be an
362 * overlapping fragment, the entire datagram (and any constituent
363 * fragments) MUST be silently discarded.
364 *
365 * We do the same here for IPv4.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700368 /* Is there an overlap with the previous fragment? */
369 if (prev &&
370 (prev->ip_defrag_offset + prev->len) > offset)
371 goto discard_qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700373 /* Is there an overlap with the next fragment? */
374 if (next && next->ip_defrag_offset < end)
375 goto discard_qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Eric Dumazetd8384862018-10-10 12:30:06 -0700377 /* Note : skb->ip_defrag_offset and skb->dev share the same location */
378 dev = skb->dev;
379 if (dev)
380 qp->iif = dev->ifindex;
381 /* Makes sure compiler wont do silly aliasing games */
382 barrier();
383 skb->ip_defrag_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* Insert this fragment in the chain of fragments. */
386 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000387 if (!next)
388 qp->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (prev)
390 prev->next = skb;
391 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700392 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700394 qp->q.stamp = skb->tstamp;
395 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000396 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200397 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200399 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Florian Westphald6b915e2015-05-22 16:32:51 +0200401 fragsize = skb->len + ihl;
402
403 if (fragsize > qp->q.max_size)
404 qp->q.max_size = fragsize;
405
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200406 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200407 fragsize > qp->max_df_size)
408 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200409
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200410 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000411 qp->q.meat == qp->q.len) {
412 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700413
Eric Dumazet97599dc2013-04-16 12:55:41 +0000414 skb->_skb_refdst = 0UL;
415 err = ip_frag_reasm(qp, prev, dev);
416 skb->_skb_refdst = orefdst;
417 return err;
418 }
419
420 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700421 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700423discard_qp:
424 inet_frag_kill(&qp->q);
425 err = -EINVAL;
426 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427err:
428 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432
433/* Build a new IP datagram from all its fragments. */
434
Herbert Xu1706d582007-10-14 00:38:15 -0700435static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
436 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700438 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700440 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int len;
442 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700443 int err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000444 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 ipq_kill(qp);
447
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000448 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000449 if (unlikely(ecn == 0xff)) {
450 err = -EINVAL;
451 goto out_fail;
452 }
Herbert Xu1706d582007-10-14 00:38:15 -0700453 /* Make the one we just received the head. */
454 if (prev) {
455 head = prev->next;
456 fp = skb_clone(head, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700457 if (!fp)
458 goto out_nomem;
459
460 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000461 if (!fp->next)
462 qp->q.fragments_tail = fp;
Herbert Xu1706d582007-10-14 00:38:15 -0700463 prev->next = fp;
464
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700465 skb_morph(head, qp->q.fragments);
466 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700467
Eric Dumazetcbf8f7b2012-04-19 06:10:26 +0000468 consume_skb(qp->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700469 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700470 }
471
Ian Morris51456b22015-04-03 09:17:26 +0100472 WARN_ON(!head);
Eric Dumazetd8384862018-10-10 12:30:06 -0700473 WARN_ON(head->ip_defrag_offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300476 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700477 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Herbert Xu1706d582007-10-14 00:38:15 -0700479 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800480 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto out_oversize;
482
483 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000484 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out_nomem;
486
487 /* If the first fragment is fragmented itself, we split
488 * it to two chunks: the first with data and paged part
489 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700490 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct sk_buff *clone;
492 int i, plen = 0;
493
Ian Morris51456b22015-04-03 09:17:26 +0100494 clone = alloc_skb(0, GFP_ATOMIC);
495 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto out_nomem;
497 clone->next = head->next;
498 head->next = clone;
499 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700500 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000501 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
502 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 clone->len = clone->data_len = head->data_len - plen;
504 head->data_len -= clone->len;
505 head->len -= clone->len;
506 clone->csum = 0;
507 clone->ip_summed = head->ip_summed;
Florian Westphal0e60d242015-07-23 12:05:38 +0200508 add_frag_mem_limit(qp->q.net, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Florian Westphal14fe22e2015-07-11 01:37:36 +0200511 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700512 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Florian Westphal14fe22e2015-07-11 01:37:36 +0200514 for (fp=head->next; fp; fp = fp->next) {
515 head->data_len += fp->len;
516 head->len += fp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (head->ip_summed != fp->ip_summed)
518 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700519 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 head->csum = csum_add(head->csum, fp->csum);
Florian Westphal14fe22e2015-07-11 01:37:36 +0200521 head->truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
David S. Miller5510b3c2015-07-31 23:52:20 -0700523 sub_frag_mem_limit(qp->q.net, head->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 head->next = NULL;
526 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700527 head->tstamp = qp->q.stamp;
Florian Westphald6b915e2015-05-22 16:32:51 +0200528 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700530 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000532 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200533
534 /* When we set IP_DF on a refragmented skb we must also force a
535 * call to ip_fragment to avoid forwarding a DF-skb of size s while
536 * original sender only sent fragments of size f (where f < s).
537 *
538 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
539 * frag seen to avoid sending tiny DF-fragments in case skb was built
540 * from one very small df-fragment and one large non-df frag.
541 */
542 if (qp->max_df_size == qp->q.max_size) {
543 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
544 iph->frag_off = htons(IP_DF);
545 } else {
546 iph->frag_off = 0;
547 }
548
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200549 ip_send_check(iph);
550
Eric Dumazetb45386e2016-04-27 16:44:35 -0700551 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700552 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000553 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800557 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700558 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out_fail;
560out_oversize:
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700561 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700563 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500568int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
David Ahern9972f132015-08-13 14:59:09 -0600570 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700571 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900573
Eric Dumazetb45386e2016-04-27 16:44:35 -0700574 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800575 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600578 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100579 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700580 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700582 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Herbert Xu1706d582007-10-14 00:38:15 -0700584 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700586 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700587 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700588 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Eric Dumazetb45386e2016-04-27 16:44:35 -0700591 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700593 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000595EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500597struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000598{
Johannes Berg1bf37512012-12-09 23:41:06 +0000599 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300600 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000601 u32 len;
602
603 if (skb->protocol != htons(ETH_P_IP))
604 return skb;
605
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300606 netoff = skb_network_offset(skb);
607
608 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000609 return skb;
610
Johannes Berg1bf37512012-12-09 23:41:06 +0000611 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000612 return skb;
613
Johannes Berg1bf37512012-12-09 23:41:06 +0000614 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300615 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000616 return skb;
617
618 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000619 skb = skb_share_check(skb, GFP_ATOMIC);
620 if (skb) {
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300621 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000622 return skb;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300623 if (pskb_trim_rcsum(skb, netoff + len))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000624 return skb;
625 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500626 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000627 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800628 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000629 }
630 }
631 return skb;
632}
633EXPORT_SYMBOL(ip_check_defrag);
634
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800635#ifdef CONFIG_SYSCTL
Eric Dumazetd8384862018-10-10 12:30:06 -0700636static int dist_min;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800637
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700638static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800639 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800640 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800641 .data = &init_net.ipv4.frags.high_thresh,
Eric Dumazet7f617062018-10-10 12:30:00 -0700642 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800643 .mode = 0644,
Eric Dumazet7f617062018-10-10 12:30:00 -0700644 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200645 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800646 },
647 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800648 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800649 .data = &init_net.ipv4.frags.low_thresh,
Eric Dumazet7f617062018-10-10 12:30:00 -0700650 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800651 .mode = 0644,
Eric Dumazet7f617062018-10-10 12:30:00 -0700652 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200653 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800654 },
655 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800656 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800657 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800658 .maxlen = sizeof(int),
659 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800660 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800661 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200662 {
663 .procname = "ipfrag_max_dist",
664 .data = &init_net.ipv4.frags.max_dist,
665 .maxlen = sizeof(int),
666 .mode = 0644,
667 .proc_handler = proc_dointvec_minmax,
Eric Dumazetd8384862018-10-10 12:30:06 -0700668 .extra1 = &dist_min,
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200669 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700670 { }
671};
672
Florian Westphale3a57d12014-07-24 16:50:35 +0200673/* secret interval has been deprecated */
674static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700675static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800676 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800677 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200678 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800679 .maxlen = sizeof(int),
680 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800681 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800682 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800683 { }
684};
685
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000686static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800687{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800688 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800689 struct ctl_table_header *hdr;
690
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700691 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800692 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700693 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100694 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800695 goto err_alloc;
696
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800697 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200698 table[0].extra1 = &net->ipv4.frags.low_thresh;
699 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800700 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200701 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800702 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200703 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800704 }
705
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000706 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100707 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800708 goto err_reg;
709
710 net->ipv4.frags_hdr = hdr;
711 return 0;
712
713err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800714 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800715 kfree(table);
716err_alloc:
717 return -ENOMEM;
718}
719
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000720static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800721{
722 struct ctl_table *table;
723
724 table = net->ipv4.frags_hdr->ctl_table_arg;
725 unregister_net_sysctl_table(net->ipv4.frags_hdr);
726 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800727}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700728
Fabian Frederick57a02c32014-10-01 19:18:57 +0200729static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700730{
Eric W. Biederman43444752012-04-19 13:22:55 +0000731 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700732}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800733#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100734static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800735{
736 return 0;
737}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800738
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100739static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800740{
741}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700742
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100743static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700744{
745}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800746#endif
747
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000748static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800749{
Eric Dumazet7fca7712018-10-10 12:29:49 -0700750 int res;
751
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000752 /* Fragment cache limits.
753 *
754 * The fragment memory accounting code, (tries to) account for
755 * the real memory usage, by measuring both the size of frag
756 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
757 * and the SKB's truesize.
758 *
759 * A 64K fragment consumes 129736 bytes (44*2944)+200
760 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
761 *
762 * We will commit 4MB at one time. Should we cross that limit
763 * we will prune down to 3MB, making room for approx 8 big 64K
764 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800765 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000766 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
767 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800768 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800769 * Important NOTE! Fragment queue must be destroyed before MSL expires.
770 * RFC791 is wrong proposing to prolongate timer each fragment arrival
771 * by TTL.
772 */
773 net->ipv4.frags.timeout = IP_FRAG_TIME;
774
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200775 net->ipv4.frags.max_dist = 64;
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700776 net->ipv4.frags.f = &ip4_frags;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200777
Eric Dumazet7fca7712018-10-10 12:29:49 -0700778 res = inet_frags_init_net(&net->ipv4.frags);
779 if (res < 0)
780 return res;
781 res = ip4_frags_ns_ctl_register(net);
782 if (res < 0)
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700783 inet_frags_exit_net(&net->ipv4.frags);
Eric Dumazet7fca7712018-10-10 12:29:49 -0700784 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800785}
786
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000787static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800788{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700789 ip4_frags_ns_ctl_unregister(net);
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700790 inet_frags_exit_net(&net->ipv4.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800791}
792
793static struct pernet_operations ip4_frags_ops = {
794 .init = ipv4_frags_init_net,
795 .exit = ipv4_frags_exit_net,
796};
797
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700798
799static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
800{
801 return jhash2(data,
802 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
803}
804
805static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
806{
807 const struct inet_frag_queue *fq = data;
808
809 return jhash2((const u32 *)&fq->key.v4,
810 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
811}
812
813static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
814{
815 const struct frag_v4_compare_key *key = arg->key;
816 const struct inet_frag_queue *fq = ptr;
817
818 return !!memcmp(&fq->key, key, sizeof(*key));
819}
820
821static const struct rhashtable_params ip4_rhash_params = {
822 .head_offset = offsetof(struct inet_frag_queue, node),
823 .key_offset = offsetof(struct inet_frag_queue, key),
824 .key_len = sizeof(struct frag_v4_compare_key),
825 .hashfn = ip4_key_hashfn,
826 .obj_hashfn = ip4_obj_hashfn,
827 .obj_cmpfn = ip4_obj_cmpfn,
828 .automatic_shrinking = true,
829};
830
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700831void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700833 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700834 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700835 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700836 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200837 ip4_frags.frags_cache_name = ip_frag_cache_name;
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700838 ip4_frags.rhash_params = ip4_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200839 if (inet_frags_init(&ip4_frags))
840 panic("IP: failed to allocate ip4_frags cache\n");
Eric Dumazetbbf6d862018-10-10 12:29:51 -0700841 ip4_frags_ctl_register();
842 register_pernet_subsys(&ip4_frags_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}