blob: e0b39d4ecbd411ff4bb72d1ed973e45bd6da9ef1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u32 user;
Al Viro18277772006-09-26 22:19:02 -070073 __be32 saddr;
74 __be32 daddr;
75 __be16 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u8 protocol;
Eric Dumazet6623e3b2011-01-05 07:52:55 +000077 u8 ecn; /* RFC3168 support */
Florian Westphald6b915e2015-05-22 16:32:51 +020078 u16 max_df_size; /* largest frag with DF set seen */
Herbert Xu89cee8b2005-12-13 23:14:27 -080079 int iif;
David Ahern385add92015-09-29 20:07:13 -070080 int vif; /* L3 master device index */
Herbert Xu89cee8b2005-12-13 23:14:27 -080081 unsigned int rid;
82 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Fabian Frederickaa1f7312014-11-04 20:44:04 +010085static u8 ip4_frag_ecn(u8 tos)
Eric Dumazet6623e3b2011-01-05 07:52:55 +000086{
Eric Dumazet5173cc02011-05-16 08:37:37 +000087 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +000088}
89
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070090static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Pavel Emelyanov6ddc0822008-01-22 06:07:25 -080092int ip_frag_mem(struct net *net)
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070093{
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +000094 return sum_frag_mem_limit(&net->ipv4.frags);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070095}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Herbert Xu1706d582007-10-14 00:38:15 -070097static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
98 struct net_device *dev);
99
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700100struct ip4_create_arg {
101 struct iphdr *iph;
102 u32 user;
David Ahern9972f132015-08-13 14:59:09 -0600103 int vif;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700104};
105
Al Viro18277772006-09-26 22:19:02 -0700106static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Hannes Frederic Sowae7b519b2013-10-23 11:06:55 +0200108 net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
Al Viro18277772006-09-26 22:19:02 -0700109 return jhash_3words((__force u32)id << 16 | prot,
110 (__force u32)saddr, (__force u32)daddr,
Florian Westphalfb3cfe62014-07-24 16:50:30 +0200111 ip4_frags.rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Florian Westphal36c77782014-07-24 16:50:29 +0200114static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Florian Westphal36c77782014-07-24 16:50:29 +0200116 const struct ipq *ipq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700118 ipq = container_of(q, struct ipq, q);
119 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Florian Westphal36c77782014-07-24 16:50:29 +0200122static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700123{
Florian Westphal36c77782014-07-24 16:50:29 +0200124 const struct ipq *qp;
125 const struct ip4_create_arg *arg = a;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700126
127 qp = container_of(q, struct ipq, q);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000128 return qp->id == arg->iph->id &&
Eric Dumazetcbc264c2012-05-18 05:57:13 +0200129 qp->saddr == arg->iph->saddr &&
130 qp->daddr == arg->iph->daddr &&
131 qp->protocol == arg->iph->protocol &&
David Ahern9972f132015-08-13 14:59:09 -0600132 qp->user == arg->user &&
133 qp->vif == arg->vif;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700134}
135
Florian Westphal36c77782014-07-24 16:50:29 +0200136static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700137{
138 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +0000139 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
140 frags);
141 struct net *net = container_of(ipv4, struct net, ipv4);
142
Florian Westphal36c77782014-07-24 16:50:29 +0200143 const struct ip4_create_arg *arg = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700144
145 qp->protocol = arg->iph->protocol;
146 qp->id = arg->iph->id;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000147 qp->ecn = ip4_frag_ecn(arg->iph->tos);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700148 qp->saddr = arg->iph->saddr;
149 qp->daddr = arg->iph->daddr;
David Ahern9972f132015-08-13 14:59:09 -0600150 qp->vif = arg->vif;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700151 qp->user = arg->user;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200152 qp->peer = q->net->max_dist ?
David Ahern192132b2015-08-27 16:07:03 -0700153 inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, arg->vif, 1) :
154 NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700155}
156
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100157static void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700159 struct ipq *qp;
160
161 qp = container_of(q, struct ipq, q);
162 if (qp->peer)
163 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* Destruction primitives. */
168
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100169static void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700171 inet_frag_put(&ipq->q, &ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/* Kill ipq entry. It is not destroyed immediately,
175 * because caller (and someone more) holds reference count.
176 */
177static void ipq_kill(struct ipq *ipq)
178{
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700179 inet_frag_kill(&ipq->q, &ip4_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Andy Zhou5cf42282015-05-15 14:15:35 -0700182static bool frag_expire_skip_icmp(u32 user)
183{
184 return user == IP_DEFRAG_AF_PACKET ||
185 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
Andy Zhou8bc04862015-05-15 14:15:36 -0700186 __IP_DEFRAG_CONNTRACK_IN_END) ||
187 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
188 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
Andy Zhou5cf42282015-05-15 14:15:35 -0700189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
193 */
Kees Cook78802012017-10-16 17:29:20 -0700194static void ip_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Kees Cook78802012017-10-16 17:29:20 -0700196 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700197 struct ipq *qp;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700198 struct net *net;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700199
Kees Cook78802012017-10-16 17:29:20 -0700200 qp = container_of(frag, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700201 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700203 rcu_read_lock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700204 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200206 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out;
208
209 ipq_kill(qp);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700210 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Nikolay Aleksandrovcaaecdd2015-07-23 12:05:40 +0200212 if (!inet_frag_evicting(&qp->q)) {
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700213 struct sk_buff *clone, *head = qp->q.fragments;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000214 const struct iphdr *iph;
215 int err;
Denis V. Lunevcb846632008-03-24 15:31:00 -0700216
Eric Dumazetb45386e2016-04-27 16:44:35 -0700217 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200218
219 if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !qp->q.fragments)
220 goto out;
221
Eric Dumazet69df9d52009-11-05 20:59:47 -0800222 head->dev = dev_get_by_index_rcu(net, qp->iif);
Shan Weie9017b52010-01-23 01:57:42 -0800223 if (!head->dev)
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700224 goto out;
225
Shan Weie9017b52010-01-23 01:57:42 -0800226
Eric Dumazet97599dc2013-04-16 12:55:41 +0000227 /* skb has no dst, perform route lookup again */
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000228 iph = ip_hdr(head);
David S. Millerc6cffba2012-07-26 11:14:38 +0000229 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
230 iph->tos, head->dev);
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000231 if (err)
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700232 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000233
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200234 /* Only an end host needs to send an ICMP
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000235 * "Fragment Reassembly Timeout" message, per RFC792.
Shan Weie9017b52010-01-23 01:57:42 -0800236 */
Andy Zhou5cf42282015-05-15 14:15:35 -0700237 if (frag_expire_skip_icmp(qp->user) &&
238 (skb_rtable(head)->rt_type != RTN_LOCAL))
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700239 goto out;
240
241 clone = skb_clone(head, GFP_ATOMIC);
Shan Weie9017b52010-01-23 01:57:42 -0800242
Shan Weie9017b52010-01-23 01:57:42 -0800243 /* Send an ICMP "Fragment Reassembly Timeout" message. */
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700244 if (clone) {
245 spin_unlock(&qp->q.lock);
246 icmp_send(clone, ICMP_TIME_EXCEEDED,
247 ICMP_EXC_FRAGTIME, 0);
248 consume_skb(clone);
249 goto out_rcu_unlock;
250 }
Patrick McHardyd1c9ae62010-02-02 11:46:50 -0800251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700253 spin_unlock(&qp->q.lock);
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700254out_rcu_unlock:
255 rcu_read_unlock();
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700256 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700259/* Find the correct entry in the "incomplete datagrams" queue for
260 * this IP datagram, and create new one, if nothing is found.
261 */
David Ahern9972f132015-08-13 14:59:09 -0600262static struct ipq *ip_find(struct net *net, struct iphdr *iph,
263 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700265 struct inet_frag_queue *q;
266 struct ip4_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700267 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700269 arg.iph = iph;
270 arg.user = user;
David Ahern9972f132015-08-13 14:59:09 -0600271 arg.vif = vif;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700272
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700273 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700274
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800275 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000276 if (IS_ERR_OR_NULL(q)) {
277 inet_frag_maybe_warn_overflow(q, pr_fmt());
278 return NULL;
279 }
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700280 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Herbert Xu89cee8b2005-12-13 23:14:27 -0800283/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100284static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800285{
286 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200287 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800288 unsigned int start, end;
289
290 int rc;
291
292 if (!peer || !max)
293 return 0;
294
295 start = qp->rid;
296 end = atomic_inc_return(&peer->rid);
297 qp->rid = end;
298
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700299 rc = qp->q.fragments && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800300
301 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700302 struct net *net;
303
304 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700305 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800306 }
307
308 return rc;
309}
310
311static int ip_frag_reinit(struct ipq *qp)
312{
313 struct sk_buff *fp;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000314 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800315
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800316 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300317 refcount_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800318 return -ETIMEDOUT;
319 }
320
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700321 fp = qp->q.fragments;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800322 do {
323 struct sk_buff *xp = fp->next;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000324
325 sum_truesize += fp->truesize;
326 kfree_skb(fp);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800327 fp = xp;
328 } while (fp);
Florian Westphal0e60d242015-07-23 12:05:38 +0200329 sub_frag_mem_limit(qp->q.net, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800330
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200331 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700332 qp->q.len = 0;
333 qp->q.meat = 0;
334 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000335 qp->q.fragments_tail = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800336 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000337 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800338
339 return 0;
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700343static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct sk_buff *prev, *next;
Herbert Xu1706d582007-10-14 00:38:15 -0700346 struct net_device *dev;
Florian Westphald6b915e2015-05-22 16:32:51 +0200347 unsigned int fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int flags, offset;
349 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700350 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000351 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200353 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 goto err;
355
Herbert Xu89cee8b2005-12-13 23:14:27 -0800356 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700357 unlikely(ip_frag_too_far(qp)) &&
358 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800359 ipq_kill(qp);
360 goto err;
361 }
362
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000363 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700364 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 flags = offset & ~IP_OFFSET;
366 offset &= IP_OFFSET;
367 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300368 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 /* Determine the position of this fragment. */
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200371 end = offset + skb->len - skb_network_offset(skb) - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700372 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* Is this the final fragment? */
375 if ((flags & IP_MF) == 0) {
376 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800377 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700379 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200380 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200382 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700383 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else {
385 if (end&7) {
386 end &= ~7;
387 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
388 skb->ip_summed = CHECKSUM_NONE;
389 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700390 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200392 if (qp->q.flags & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700394 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397 if (end == offset)
398 goto err;
399
Herbert Xu1706d582007-10-14 00:38:15 -0700400 err = -ENOMEM;
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200401 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700403
404 err = pskb_trim_rcsum(skb, end - offset);
405 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto err;
407
408 /* Find out which fragments are in front and at the back of us
409 * in the chain of fragments so far. We must know where to put
410 * this fragment, right?
411 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000412 prev = qp->q.fragments_tail;
413 if (!prev || FRAG_CB(prev)->offset < offset) {
414 next = NULL;
415 goto found;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700418 for (next = qp->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (FRAG_CB(next)->offset >= offset)
420 break; /* bingo! */
421 prev = next;
422 }
423
Changli Gaod6bebca2010-06-29 04:39:37 +0000424found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* We found where to put this one. Check for overlap with
426 * preceding fragment, and, if needed, align things so that
427 * any overlaps are eliminated.
428 */
429 if (prev) {
430 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
431
432 if (i > 0) {
433 offset += i;
Herbert Xu1706d582007-10-14 00:38:15 -0700434 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (end <= offset)
436 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700437 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!pskb_pull(skb, i))
439 goto err;
440 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
441 skb->ip_summed = CHECKSUM_NONE;
442 }
443 }
444
Herbert Xu1706d582007-10-14 00:38:15 -0700445 err = -ENOMEM;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 while (next && FRAG_CB(next)->offset < end) {
448 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
449
450 if (i < next->len) {
451 /* Eat head of the next overlapped fragment
452 * and leave the loop. The next ones cannot overlap.
453 */
454 if (!pskb_pull(next, i))
455 goto err;
456 FRAG_CB(next)->offset += i;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700457 qp->q.meat -= i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (next->ip_summed != CHECKSUM_UNNECESSARY)
459 next->ip_summed = CHECKSUM_NONE;
460 break;
461 } else {
462 struct sk_buff *free_it = next;
463
Peter Zijlstra47c6bf772006-12-12 19:48:59 +0100464 /* Old fragment is completely overridden with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * new one drop it.
466 */
467 next = next->next;
468
469 if (prev)
470 prev->next = next;
471 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700472 qp->q.fragments = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700474 qp->q.meat -= free_it->len;
Florian Westphal0e60d242015-07-23 12:05:38 +0200475 sub_frag_mem_limit(qp->q.net, free_it->truesize);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000476 kfree_skb(free_it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 }
479
480 FRAG_CB(skb)->offset = offset;
481
482 /* Insert this fragment in the chain of fragments. */
483 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000484 if (!next)
485 qp->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (prev)
487 prev->next = skb;
488 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700489 qp->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Herbert Xu1706d582007-10-14 00:38:15 -0700491 dev = skb->dev;
492 if (dev) {
493 qp->iif = dev->ifindex;
494 skb->dev = NULL;
495 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700496 qp->q.stamp = skb->tstamp;
497 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000498 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200499 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200501 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Florian Westphald6b915e2015-05-22 16:32:51 +0200503 fragsize = skb->len + ihl;
504
505 if (fragsize > qp->q.max_size)
506 qp->q.max_size = fragsize;
507
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200508 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200509 fragsize > qp->max_df_size)
510 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200511
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200512 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000513 qp->q.meat == qp->q.len) {
514 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700515
Eric Dumazet97599dc2013-04-16 12:55:41 +0000516 skb->_skb_refdst = 0UL;
517 err = ip_frag_reasm(qp, prev, dev);
518 skb->_skb_refdst = orefdst;
519 return err;
520 }
521
522 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700523 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525err:
526 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530
531/* Build a new IP datagram from all its fragments. */
532
Herbert Xu1706d582007-10-14 00:38:15 -0700533static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
534 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700536 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct iphdr *iph;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700538 struct sk_buff *fp, *head = qp->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int len;
540 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700541 int err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000542 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 ipq_kill(qp);
545
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000546 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000547 if (unlikely(ecn == 0xff)) {
548 err = -EINVAL;
549 goto out_fail;
550 }
Herbert Xu1706d582007-10-14 00:38:15 -0700551 /* Make the one we just received the head. */
552 if (prev) {
553 head = prev->next;
554 fp = skb_clone(head, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700555 if (!fp)
556 goto out_nomem;
557
558 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000559 if (!fp->next)
560 qp->q.fragments_tail = fp;
Herbert Xu1706d582007-10-14 00:38:15 -0700561 prev->next = fp;
562
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700563 skb_morph(head, qp->q.fragments);
564 head->next = qp->q.fragments->next;
Herbert Xu1706d582007-10-14 00:38:15 -0700565
Eric Dumazetcbf8f7b2012-04-19 06:10:26 +0000566 consume_skb(qp->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700567 qp->q.fragments = head;
Herbert Xu1706d582007-10-14 00:38:15 -0700568 }
569
Ian Morris51456b22015-04-03 09:17:26 +0100570 WARN_ON(!head);
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700571 WARN_ON(FRAG_CB(head)->offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300574 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700575 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Herbert Xu1706d582007-10-14 00:38:15 -0700577 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800578 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out_oversize;
580
581 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000582 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto out_nomem;
584
585 /* If the first fragment is fragmented itself, we split
586 * it to two chunks: the first with data and paged part
587 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700588 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct sk_buff *clone;
590 int i, plen = 0;
591
Ian Morris51456b22015-04-03 09:17:26 +0100592 clone = alloc_skb(0, GFP_ATOMIC);
593 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto out_nomem;
595 clone->next = head->next;
596 head->next = clone;
597 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700598 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000599 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
600 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 clone->len = clone->data_len = head->data_len - plen;
602 head->data_len -= clone->len;
603 head->len -= clone->len;
604 clone->csum = 0;
605 clone->ip_summed = head->ip_summed;
Florian Westphal0e60d242015-07-23 12:05:38 +0200606 add_frag_mem_limit(qp->q.net, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
Florian Westphal14fe22e2015-07-11 01:37:36 +0200609 skb_shinfo(head)->frag_list = head->next;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700610 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Florian Westphal14fe22e2015-07-11 01:37:36 +0200612 for (fp=head->next; fp; fp = fp->next) {
613 head->data_len += fp->len;
614 head->len += fp->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (head->ip_summed != fp->ip_summed)
616 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700617 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 head->csum = csum_add(head->csum, fp->csum);
Florian Westphal14fe22e2015-07-11 01:37:36 +0200619 head->truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
David S. Miller5510b3c2015-07-31 23:52:20 -0700621 sub_frag_mem_limit(qp->q.net, head->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 head->next = NULL;
624 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700625 head->tstamp = qp->q.stamp;
Florian Westphald6b915e2015-05-22 16:32:51 +0200626 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700628 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000630 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200631
632 /* When we set IP_DF on a refragmented skb we must also force a
633 * call to ip_fragment to avoid forwarding a DF-skb of size s while
634 * original sender only sent fragments of size f (where f < s).
635 *
636 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
637 * frag seen to avoid sending tiny DF-fragments in case skb was built
638 * from one very small df-fragment and one large non-df frag.
639 */
640 if (qp->max_df_size == qp->q.max_size) {
641 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
642 iph->frag_off = htons(IP_DF);
643 } else {
644 iph->frag_off = 0;
645 }
646
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200647 ip_send_check(iph);
648
Eric Dumazetb45386e2016-04-27 16:44:35 -0700649 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700650 qp->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000651 qp->q.fragments_tail = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800655 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700656 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto out_fail;
658out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000659 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700661 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500666int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
David Ahern9972f132015-08-13 14:59:09 -0600668 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700669 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900671
Eric Dumazetb45386e2016-04-27 16:44:35 -0700672 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800673 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600676 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100677 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700680 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Herbert Xu1706d582007-10-14 00:38:15 -0700682 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700684 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700685 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
Eric Dumazetb45386e2016-04-27 16:44:35 -0700689 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000693EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500695struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000696{
Johannes Berg1bf37512012-12-09 23:41:06 +0000697 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300698 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000699 u32 len;
700
701 if (skb->protocol != htons(ETH_P_IP))
702 return skb;
703
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300704 netoff = skb_network_offset(skb);
705
706 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000707 return skb;
708
Johannes Berg1bf37512012-12-09 23:41:06 +0000709 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000710 return skb;
711
Johannes Berg1bf37512012-12-09 23:41:06 +0000712 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300713 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000714 return skb;
715
716 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000717 skb = skb_share_check(skb, GFP_ATOMIC);
718 if (skb) {
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300719 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000720 return skb;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300721 if (pskb_trim_rcsum(skb, netoff + len))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000722 return skb;
723 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500724 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000725 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800726 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000727 }
728 }
729 return skb;
730}
731EXPORT_SYMBOL(ip_check_defrag);
732
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800733#ifdef CONFIG_SYSCTL
734static int zero;
735
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700736static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800737 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800738 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800739 .data = &init_net.ipv4.frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800740 .maxlen = sizeof(int),
741 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200742 .proc_handler = proc_dointvec_minmax,
743 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800744 },
745 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800746 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800747 .data = &init_net.ipv4.frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800748 .maxlen = sizeof(int),
749 .mode = 0644,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200750 .proc_handler = proc_dointvec_minmax,
751 .extra1 = &zero,
752 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800753 },
754 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800755 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800756 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800757 .maxlen = sizeof(int),
758 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800759 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800760 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200761 {
762 .procname = "ipfrag_max_dist",
763 .data = &init_net.ipv4.frags.max_dist,
764 .maxlen = sizeof(int),
765 .mode = 0644,
766 .proc_handler = proc_dointvec_minmax,
767 .extra1 = &zero
768 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700769 { }
770};
771
Florian Westphale3a57d12014-07-24 16:50:35 +0200772/* secret interval has been deprecated */
773static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700774static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800775 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800776 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200777 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800778 .maxlen = sizeof(int),
779 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800780 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800781 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800782 { }
783};
784
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000785static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800786{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800787 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800788 struct ctl_table_header *hdr;
789
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700790 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800791 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700792 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100793 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800794 goto err_alloc;
795
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800796 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200797 table[0].extra1 = &net->ipv4.frags.low_thresh;
798 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800799 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200800 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800801 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200802 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800803 }
804
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000805 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100806 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800807 goto err_reg;
808
809 net->ipv4.frags_hdr = hdr;
810 return 0;
811
812err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800813 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800814 kfree(table);
815err_alloc:
816 return -ENOMEM;
817}
818
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000819static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800820{
821 struct ctl_table *table;
822
823 table = net->ipv4.frags_hdr->ctl_table_arg;
824 unregister_net_sysctl_table(net->ipv4.frags_hdr);
825 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800826}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700827
Fabian Frederick57a02c32014-10-01 19:18:57 +0200828static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700829{
Eric W. Biederman43444752012-04-19 13:22:55 +0000830 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700831}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800832#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100833static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800834{
835 return 0;
836}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800837
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100838static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800839{
840}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700841
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100842static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700843{
844}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800845#endif
846
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000847static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800848{
Eric Dumazet787bea72018-03-31 12:58:43 -0700849 int res;
850
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000851 /* Fragment cache limits.
852 *
853 * The fragment memory accounting code, (tries to) account for
854 * the real memory usage, by measuring both the size of frag
855 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
856 * and the SKB's truesize.
857 *
858 * A 64K fragment consumes 129736 bytes (44*2944)+200
859 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
860 *
861 * We will commit 4MB at one time. Should we cross that limit
862 * we will prune down to 3MB, making room for approx 8 big 64K
863 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800864 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000865 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
866 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800867 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800868 * Important NOTE! Fragment queue must be destroyed before MSL expires.
869 * RFC791 is wrong proposing to prolongate timer each fragment arrival
870 * by TTL.
871 */
872 net->ipv4.frags.timeout = IP_FRAG_TIME;
873
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200874 net->ipv4.frags.max_dist = 64;
875
Eric Dumazet787bea72018-03-31 12:58:43 -0700876 res = inet_frags_init_net(&net->ipv4.frags);
877 if (res < 0)
878 return res;
879 res = ip4_frags_ns_ctl_register(net);
880 if (res < 0)
881 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
882 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800883}
884
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000885static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800886{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700887 ip4_frags_ns_ctl_unregister(net);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800888 inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
889}
890
891static struct pernet_operations ip4_frags_ops = {
892 .init = ipv4_frags_init_net,
893 .exit = ipv4_frags_exit_net,
894};
895
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700896void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700898 ip4_frags_ctl_register();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800899 register_pernet_subsys(&ip4_frags_ops);
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700900 ip4_frags.hashfn = ip4_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700901 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700902 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700903 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700904 ip4_frags.match = ip4_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700905 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200906 ip4_frags.frags_cache_name = ip_frag_cache_name;
907 if (inet_frags_init(&ip4_frags))
908 panic("IP: failed to allocate ip4_frags cache\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}