blob: 88281fbce88ce8f1062b99594665766c2a5f5b74 [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
Peter Oskolkov353c9cb2018-08-11 20:27:24 +000060/* Use skb->cb to track consecutive/adjacent fragments coming at
61 * the end of the queue. Nodes in the rb-tree queue will
62 * contain "runs" of one or more adjacent fragments.
63 *
64 * Invariants:
65 * - next_frag is NULL at the tail of a "run";
66 * - the head of a "run" has the sum of all fragment lengths in frag_run_len.
67 */
68struct ipfrag_skb_cb {
69 struct inet_skb_parm h;
70 struct sk_buff *next_frag;
71 int frag_run_len;
72};
73
74#define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
75
76static void ip4_frag_init_run(struct sk_buff *skb)
77{
78 BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb));
79
80 FRAG_CB(skb)->next_frag = NULL;
81 FRAG_CB(skb)->frag_run_len = skb->len;
82}
83
84/* Append skb to the last "run". */
85static void ip4_frag_append_to_last_run(struct inet_frag_queue *q,
86 struct sk_buff *skb)
87{
88 RB_CLEAR_NODE(&skb->rbnode);
89 FRAG_CB(skb)->next_frag = NULL;
90
91 FRAG_CB(q->last_run_head)->frag_run_len += skb->len;
92 FRAG_CB(q->fragments_tail)->next_frag = skb;
93 q->fragments_tail = skb;
94}
95
96/* Create a new "run" with the skb. */
97static void ip4_frag_create_run(struct inet_frag_queue *q, struct sk_buff *skb)
98{
99 if (q->last_run_head)
100 rb_link_node(&skb->rbnode, &q->last_run_head->rbnode,
101 &q->last_run_head->rbnode.rb_right);
102 else
103 rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node);
104 rb_insert_color(&skb->rbnode, &q->rb_fragments);
105
106 ip4_frag_init_run(skb);
107 q->fragments_tail = skb;
108 q->last_run_head = skb;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* Describe an entry in the "incomplete datagrams" queue. */
112struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700113 struct inet_frag_queue q;
114
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000115 u8 ecn; /* RFC3168 support */
Florian Westphald6b915e2015-05-22 16:32:51 +0200116 u16 max_df_size; /* largest frag with DF set seen */
Herbert Xu89cee8b2005-12-13 23:14:27 -0800117 int iif;
118 unsigned int rid;
119 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100122static u8 ip4_frag_ecn(u8 tos)
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000123{
Eric Dumazet5173cc02011-05-16 08:37:37 +0000124 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000125}
126
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700127static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000129static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
130 struct sk_buff *prev_tail, struct net_device *dev);
Herbert Xu1706d582007-10-14 00:38:15 -0700131
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700132
Florian Westphal36c77782014-07-24 16:50:29 +0200133static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700134{
135 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +0000136 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
137 frags);
138 struct net *net = container_of(ipv4, struct net, ipv4);
139
Eric Dumazet648700f2018-03-31 12:58:49 -0700140 const struct frag_v4_compare_key *key = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700141
Eric Dumazet648700f2018-03-31 12:58:49 -0700142 q->key.v4 = *key;
143 qp->ecn = 0;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200144 qp->peer = q->net->max_dist ?
Eric Dumazet648700f2018-03-31 12:58:49 -0700145 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
David Ahern192132b2015-08-27 16:07:03 -0700146 NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700147}
148
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100149static void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700151 struct ipq *qp;
152
153 qp = container_of(q, struct ipq, q);
154 if (qp->peer)
155 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/* Destruction primitives. */
160
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100161static void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Eric Dumazet093ba722018-03-31 12:58:44 -0700163 inet_frag_put(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166/* Kill ipq entry. It is not destroyed immediately,
167 * because caller (and someone more) holds reference count.
168 */
169static void ipq_kill(struct ipq *ipq)
170{
Eric Dumazet093ba722018-03-31 12:58:44 -0700171 inet_frag_kill(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Andy Zhou5cf42282015-05-15 14:15:35 -0700174static bool frag_expire_skip_icmp(u32 user)
175{
176 return user == IP_DEFRAG_AF_PACKET ||
177 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
Andy Zhou8bc04862015-05-15 14:15:36 -0700178 __IP_DEFRAG_CONNTRACK_IN_END) ||
179 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
180 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
Andy Zhou5cf42282015-05-15 14:15:35 -0700181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
185 */
Kees Cook78802012017-10-16 17:29:20 -0700186static void ip_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Kees Cook78802012017-10-16 17:29:20 -0700188 struct inet_frag_queue *frag = from_timer(frag, t, timer);
Eric Dumazet399d1402018-03-31 12:58:51 -0700189 const struct iphdr *iph;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000190 struct sk_buff *head = NULL;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700191 struct net *net;
Eric Dumazet399d1402018-03-31 12:58:51 -0700192 struct ipq *qp;
193 int err;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700194
Kees Cook78802012017-10-16 17:29:20 -0700195 qp = container_of(frag, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700196 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700198 rcu_read_lock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700199 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200201 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto out;
203
204 ipq_kill(qp);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700205 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Eric Dumazet399d1402018-03-31 12:58:51 -0700206 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200207
Dan Carpenter70837ff2018-08-06 22:17:35 +0300208 if (!(qp->q.flags & INET_FRAG_FIRST_IN))
Eric Dumazet399d1402018-03-31 12:58:51 -0700209 goto out;
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200210
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000211 /* sk_buff::dev and sk_buff::rbnode are unionized. So we
212 * pull the head out of the tree in order to be able to
213 * deal with head->dev.
214 */
215 if (qp->q.fragments) {
216 head = qp->q.fragments;
217 qp->q.fragments = head->next;
218 } else {
219 head = skb_rb_first(&qp->q.rb_fragments);
220 if (!head)
221 goto out;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000222 if (FRAG_CB(head)->next_frag)
223 rb_replace_node(&head->rbnode,
224 &FRAG_CB(head)->next_frag->rbnode,
225 &qp->q.rb_fragments);
226 else
227 rb_erase(&head->rbnode, &qp->q.rb_fragments);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000228 memset(&head->rbnode, 0, sizeof(head->rbnode));
229 barrier();
230 }
231 if (head == qp->q.fragments_tail)
232 qp->q.fragments_tail = NULL;
233
234 sub_frag_mem_limit(qp->q.net, head->truesize);
235
Eric Dumazet399d1402018-03-31 12:58:51 -0700236 head->dev = dev_get_by_index_rcu(net, qp->iif);
237 if (!head->dev)
238 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700239
Shan Weie9017b52010-01-23 01:57:42 -0800240
Eric Dumazet399d1402018-03-31 12:58:51 -0700241 /* skb has no dst, perform route lookup again */
242 iph = ip_hdr(head);
243 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
David S. Millerc6cffba2012-07-26 11:14:38 +0000244 iph->tos, head->dev);
Eric Dumazet399d1402018-03-31 12:58:51 -0700245 if (err)
246 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000247
Eric Dumazet399d1402018-03-31 12:58:51 -0700248 /* Only an end host needs to send an ICMP
249 * "Fragment Reassembly Timeout" message, per RFC792.
250 */
251 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
252 (skb_rtable(head)->rt_type != RTN_LOCAL))
253 goto out;
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700254
Eric Dumazet1eec5d52018-03-31 12:58:54 -0700255 spin_unlock(&qp->q.lock);
256 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
Eric Dumazet1eec5d52018-03-31 12:58:54 -0700257 goto out_rcu_unlock;
Shan Weie9017b52010-01-23 01:57:42 -0800258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700260 spin_unlock(&qp->q.lock);
Eric Dumazetec4fbd62017-03-22 08:57:15 -0700261out_rcu_unlock:
262 rcu_read_unlock();
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000263 if (head)
264 kfree_skb(head);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700265 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700268/* Find the correct entry in the "incomplete datagrams" queue for
269 * this IP datagram, and create new one, if nothing is found.
270 */
David Ahern9972f132015-08-13 14:59:09 -0600271static struct ipq *ip_find(struct net *net, struct iphdr *iph,
272 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Eric Dumazet648700f2018-03-31 12:58:49 -0700274 struct frag_v4_compare_key key = {
275 .saddr = iph->saddr,
276 .daddr = iph->daddr,
277 .user = user,
278 .vif = vif,
279 .id = iph->id,
280 .protocol = iph->protocol,
281 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700282 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Eric Dumazet648700f2018-03-31 12:58:49 -0700284 q = inet_frag_find(&net->ipv4.frags, &key);
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700285 if (!q)
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000286 return NULL;
Eric Dumazet2d44ed22018-03-31 12:58:52 -0700287
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700288 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Herbert Xu89cee8b2005-12-13 23:14:27 -0800291/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100292static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800293{
294 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200295 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800296 unsigned int start, end;
297
298 int rc;
299
300 if (!peer || !max)
301 return 0;
302
303 start = qp->rid;
304 end = atomic_inc_return(&peer->rid);
305 qp->rid = end;
306
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000307 rc = qp->q.fragments_tail && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800308
309 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700310 struct net *net;
311
312 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700313 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800314 }
315
316 return rc;
317}
318
319static int ip_frag_reinit(struct ipq *qp)
320{
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000321 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800322
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800323 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300324 refcount_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800325 return -ETIMEDOUT;
326 }
327
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000328 sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments);
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;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000335 qp->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000336 qp->q.fragments_tail = NULL;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000337 qp->q.last_run_head = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800338 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000339 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800340
341 return 0;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700345static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Peter Oskolkov7969e5c2018-08-02 23:34:37 +0000347 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000348 struct rb_node **rbn, *parent;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000349 struct sk_buff *skb1, *prev_tail;
Herbert Xu1706d582007-10-14 00:38:15 -0700350 struct net_device *dev;
Florian Westphald6b915e2015-05-22 16:32:51 +0200351 unsigned int fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int flags, offset;
353 int ihl, end;
Herbert Xu1706d582007-10-14 00:38:15 -0700354 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000355 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200357 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto err;
359
Herbert Xu89cee8b2005-12-13 23:14:27 -0800360 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700361 unlikely(ip_frag_too_far(qp)) &&
362 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800363 ipq_kill(qp);
364 goto err;
365 }
366
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000367 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700368 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 flags = offset & ~IP_OFFSET;
370 offset &= IP_OFFSET;
371 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300372 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* Determine the position of this fragment. */
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200375 end = offset + skb->len - skb_network_offset(skb) - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700376 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* Is this the final fragment? */
379 if ((flags & IP_MF) == 0) {
380 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800381 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700383 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200384 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto err;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200386 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700387 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else {
389 if (end&7) {
390 end &= ~7;
391 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
392 skb->ip_summed = CHECKSUM_NONE;
393 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700394 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200396 if (qp->q.flags & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700398 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 }
401 if (end == offset)
402 goto err;
403
Herbert Xu1706d582007-10-14 00:38:15 -0700404 err = -ENOMEM;
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200405 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto err;
Herbert Xu1706d582007-10-14 00:38:15 -0700407
408 err = pskb_trim_rcsum(skb, end - offset);
409 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto err;
411
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000412 /* Note : skb->rbnode and skb->dev share the same location. */
413 dev = skb->dev;
414 /* Makes sure compiler wont do silly aliasing games */
415 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Peter Oskolkov7969e5c2018-08-02 23:34:37 +0000417 /* RFC5722, Section 4, amended by Errata ID : 3089
418 * When reassembling an IPv6 datagram, if
419 * one or more its constituent fragments is determined to be an
420 * overlapping fragment, the entire datagram (and any constituent
421 * fragments) MUST be silently discarded.
422 *
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000423 * We do the same here for IPv4 (and increment an snmp counter).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000426 /* Find out where to put this fragment. */
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000427 prev_tail = qp->q.fragments_tail;
428 if (!prev_tail)
429 ip4_frag_create_run(&qp->q, skb); /* First fragment. */
430 else if (prev_tail->ip_defrag_offset + prev_tail->len < end) {
431 /* This is the common case: skb goes to the end. */
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000432 /* Detect and discard overlaps. */
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000433 if (offset < prev_tail->ip_defrag_offset + prev_tail->len)
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000434 goto discard_qp;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000435 if (offset == prev_tail->ip_defrag_offset + prev_tail->len)
436 ip4_frag_append_to_last_run(&qp->q, skb);
437 else
438 ip4_frag_create_run(&qp->q, skb);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000439 } else {
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000440 /* Binary search. Note that skb can become the first fragment,
441 * but not the last (covered above).
442 */
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000443 rbn = &qp->q.rb_fragments.rb_node;
444 do {
445 parent = *rbn;
446 skb1 = rb_to_skb(parent);
447 if (end <= skb1->ip_defrag_offset)
448 rbn = &parent->rb_left;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000449 else if (offset >= skb1->ip_defrag_offset +
450 FRAG_CB(skb1)->frag_run_len)
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000451 rbn = &parent->rb_right;
452 else /* Found an overlap with skb1. */
453 goto discard_qp;
454 } while (*rbn);
455 /* Here we have parent properly set, and rbn pointing to
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000456 * one of its NULL left/right children. Insert skb.
457 */
458 ip4_frag_init_run(skb);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000459 rb_link_node(&skb->rbnode, parent, rbn);
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000460 rb_insert_color(&skb->rbnode, &qp->q.rb_fragments);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Eric Dumazetbf663372018-03-31 12:58:58 -0700463 if (dev)
464 qp->iif = dev->ifindex;
Eric Dumazetbf663372018-03-31 12:58:58 -0700465 skb->ip_defrag_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700467 qp->q.stamp = skb->tstamp;
468 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000469 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200470 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200472 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Florian Westphald6b915e2015-05-22 16:32:51 +0200474 fragsize = skb->len + ihl;
475
476 if (fragsize > qp->q.max_size)
477 qp->q.max_size = fragsize;
478
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200479 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200480 fragsize > qp->max_df_size)
481 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200482
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200483 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000484 qp->q.meat == qp->q.len) {
485 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700486
Eric Dumazet97599dc2013-04-16 12:55:41 +0000487 skb->_skb_refdst = 0UL;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000488 err = ip_frag_reasm(qp, skb, prev_tail, dev);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000489 skb->_skb_refdst = orefdst;
490 return err;
491 }
492
493 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700494 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Peter Oskolkov7969e5c2018-08-02 23:34:37 +0000496discard_qp:
497 inet_frag_kill(&qp->q);
498 err = -EINVAL;
499 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500err:
501 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/* Build a new IP datagram from all its fragments. */
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000506static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000507 struct sk_buff *prev_tail, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700509 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct iphdr *iph;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000511 struct sk_buff *fp, *head = skb_rb_first(&qp->q.rb_fragments);
512 struct sk_buff **nextp; /* To build frag_list. */
513 struct rb_node *rbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int len;
515 int ihlen;
Herbert Xu1706d582007-10-14 00:38:15 -0700516 int err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000517 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 ipq_kill(qp);
520
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000521 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000522 if (unlikely(ecn == 0xff)) {
523 err = -EINVAL;
524 goto out_fail;
525 }
Herbert Xu1706d582007-10-14 00:38:15 -0700526 /* Make the one we just received the head. */
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000527 if (head != skb) {
528 fp = skb_clone(skb, GFP_ATOMIC);
Herbert Xu1706d582007-10-14 00:38:15 -0700529 if (!fp)
530 goto out_nomem;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000531 FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
532 if (RB_EMPTY_NODE(&skb->rbnode))
533 FRAG_CB(prev_tail)->next_frag = fp;
534 else
535 rb_replace_node(&skb->rbnode, &fp->rbnode,
536 &qp->q.rb_fragments);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000537 if (qp->q.fragments_tail == skb)
Changli Gaod6bebca2010-06-29 04:39:37 +0000538 qp->q.fragments_tail = fp;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000539 skb_morph(skb, head);
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000540 FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000541 rb_replace_node(&head->rbnode, &skb->rbnode,
542 &qp->q.rb_fragments);
543 consume_skb(head);
544 head = skb;
Herbert Xu1706d582007-10-14 00:38:15 -0700545 }
546
Eric Dumazetbf663372018-03-31 12:58:58 -0700547 WARN_ON(head->ip_defrag_offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* Allocate a new buffer for the datagram. */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300550 ihlen = ip_hdrlen(head);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700551 len = ihlen + qp->q.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Herbert Xu1706d582007-10-14 00:38:15 -0700553 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800554 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto out_oversize;
556
557 /* Head of list must not be cloned. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000558 if (skb_unclone(head, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out_nomem;
560
561 /* If the first fragment is fragmented itself, we split
562 * it to two chunks: the first with data and paged part
563 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700564 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct sk_buff *clone;
566 int i, plen = 0;
567
Ian Morris51456b22015-04-03 09:17:26 +0100568 clone = alloc_skb(0, GFP_ATOMIC);
569 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto out_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700572 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000573 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
574 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 clone->len = clone->data_len = head->data_len - plen;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000576 head->truesize += clone->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 clone->csum = 0;
578 clone->ip_summed = head->ip_summed;
Florian Westphal0e60d242015-07-23 12:05:38 +0200579 add_frag_mem_limit(qp->q.net, clone->truesize);
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000580 skb_shinfo(head)->frag_list = clone;
581 nextp = &clone->next;
582 } else {
583 nextp = &skb_shinfo(head)->frag_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700586 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000588 /* Traverse the tree in order, to build frag_list. */
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000589 fp = FRAG_CB(head)->next_frag;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000590 rbn = rb_next(&head->rbnode);
591 rb_erase(&head->rbnode, &qp->q.rb_fragments);
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000592 while (rbn || fp) {
593 /* fp points to the next sk_buff in the current run;
594 * rbn points to the next run.
595 */
596 /* Go through the current run. */
597 while (fp) {
598 *nextp = fp;
599 nextp = &fp->next;
600 fp->prev = NULL;
601 memset(&fp->rbnode, 0, sizeof(fp->rbnode));
602 head->data_len += fp->len;
603 head->len += fp->len;
604 if (head->ip_summed != fp->ip_summed)
605 head->ip_summed = CHECKSUM_NONE;
606 else if (head->ip_summed == CHECKSUM_COMPLETE)
607 head->csum = csum_add(head->csum, fp->csum);
608 head->truesize += fp->truesize;
609 fp = FRAG_CB(fp)->next_frag;
610 }
611 /* Move to the next run. */
612 if (rbn) {
613 struct rb_node *rbnext = rb_next(rbn);
614
615 fp = rb_to_skb(rbn);
616 rb_erase(rbn, &qp->q.rb_fragments);
617 rbn = rbnext;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
David S. Miller5510b3c2015-07-31 23:52:20 -0700620 sub_frag_mem_limit(qp->q.net, head->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000622 *nextp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 head->next = NULL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000624 head->prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700626 head->tstamp = qp->q.stamp;
Florian Westphald6b915e2015-05-22 16:32:51 +0200627 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700629 iph = ip_hdr(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000631 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200632
633 /* When we set IP_DF on a refragmented skb we must also force a
634 * call to ip_fragment to avoid forwarding a DF-skb of size s while
635 * original sender only sent fragments of size f (where f < s).
636 *
637 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
638 * frag seen to avoid sending tiny DF-fragments in case skb was built
639 * from one very small df-fragment and one large non-df frag.
640 */
641 if (qp->max_df_size == qp->q.max_size) {
642 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
643 iph->frag_off = htons(IP_DF);
644 } else {
645 iph->frag_off = 0;
646 }
647
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200648 ip_send_check(iph);
649
Eric Dumazetb45386e2016-04-27 16:44:35 -0700650 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700651 qp->q.fragments = NULL;
Peter Oskolkovfa0f5272018-08-02 23:34:39 +0000652 qp->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000653 qp->q.fragments_tail = NULL;
Peter Oskolkova4fd2842018-08-11 20:27:25 +0000654 qp->q.last_run_head = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800658 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700659 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto out_fail;
661out_oversize:
Eric Dumazet648700f2018-03-31 12:58:49 -0700662 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700664 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500669int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
David Ahern9972f132015-08-13 14:59:09 -0600671 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700672 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900674
Eric Dumazetb45386e2016-04-27 16:44:35 -0700675 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800676 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600679 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100680 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700681 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700683 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Herbert Xu1706d582007-10-14 00:38:15 -0700685 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700687 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700688 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700689 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Eric Dumazetb45386e2016-04-27 16:44:35 -0700692 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000696EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500698struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000699{
Johannes Berg1bf37512012-12-09 23:41:06 +0000700 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300701 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000702 u32 len;
703
704 if (skb->protocol != htons(ETH_P_IP))
705 return skb;
706
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300707 netoff = skb_network_offset(skb);
708
709 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000710 return skb;
711
Johannes Berg1bf37512012-12-09 23:41:06 +0000712 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000713 return skb;
714
Johannes Berg1bf37512012-12-09 23:41:06 +0000715 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300716 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000717 return skb;
718
719 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000720 skb = skb_share_check(skb, GFP_ATOMIC);
721 if (skb) {
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300722 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000723 return skb;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300724 if (pskb_trim_rcsum(skb, netoff + len))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000725 return skb;
726 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500727 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000728 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800729 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000730 }
731 }
732 return skb;
733}
734EXPORT_SYMBOL(ip_check_defrag);
735
Peter Oskolkov353c9cb2018-08-11 20:27:24 +0000736unsigned int inet_frag_rbtree_purge(struct rb_root *root)
737{
738 struct rb_node *p = rb_first(root);
739 unsigned int sum = 0;
740
741 while (p) {
742 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
743
744 p = rb_next(p);
745 rb_erase(&skb->rbnode, root);
746 while (skb) {
747 struct sk_buff *next = FRAG_CB(skb)->next_frag;
748
749 sum += skb->truesize;
750 kfree_skb(skb);
751 skb = next;
752 }
753 }
754 return sum;
755}
756EXPORT_SYMBOL(inet_frag_rbtree_purge);
757
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800758#ifdef CONFIG_SYSCTL
Eric Dumazet3d234012018-04-04 08:35:10 -0700759static int dist_min;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800760
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700761static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800762 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800763 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800764 .data = &init_net.ipv4.frags.high_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700765 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800766 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700767 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200768 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800769 },
770 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800771 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800772 .data = &init_net.ipv4.frags.low_thresh,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700773 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800774 .mode = 0644,
Eric Dumazet3e67f102018-03-31 12:58:53 -0700775 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200776 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800777 },
778 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800779 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800780 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800781 .maxlen = sizeof(int),
782 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800783 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800784 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200785 {
786 .procname = "ipfrag_max_dist",
787 .data = &init_net.ipv4.frags.max_dist,
788 .maxlen = sizeof(int),
789 .mode = 0644,
790 .proc_handler = proc_dointvec_minmax,
Eric Dumazet3d234012018-04-04 08:35:10 -0700791 .extra1 = &dist_min,
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200792 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700793 { }
794};
795
Florian Westphale3a57d12014-07-24 16:50:35 +0200796/* secret interval has been deprecated */
797static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700798static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800799 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800800 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200801 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800802 .maxlen = sizeof(int),
803 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800804 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800805 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800806 { }
807};
808
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000809static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800810{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800811 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800812 struct ctl_table_header *hdr;
813
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700814 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800815 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700816 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100817 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800818 goto err_alloc;
819
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800820 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200821 table[0].extra1 = &net->ipv4.frags.low_thresh;
822 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800823 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200824 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800825 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200826 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800827 }
828
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000829 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100830 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800831 goto err_reg;
832
833 net->ipv4.frags_hdr = hdr;
834 return 0;
835
836err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800837 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800838 kfree(table);
839err_alloc:
840 return -ENOMEM;
841}
842
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000843static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800844{
845 struct ctl_table *table;
846
847 table = net->ipv4.frags_hdr->ctl_table_arg;
848 unregister_net_sysctl_table(net->ipv4.frags_hdr);
849 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800850}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700851
Fabian Frederick57a02c32014-10-01 19:18:57 +0200852static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700853{
Eric W. Biederman43444752012-04-19 13:22:55 +0000854 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700855}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800856#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100857static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800858{
859 return 0;
860}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800861
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100862static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800863{
864}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700865
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100866static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700867{
868}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800869#endif
870
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000871static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800872{
Eric Dumazet787bea72018-03-31 12:58:43 -0700873 int res;
874
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000875 /* Fragment cache limits.
876 *
877 * The fragment memory accounting code, (tries to) account for
878 * the real memory usage, by measuring both the size of frag
879 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
880 * and the SKB's truesize.
881 *
882 * A 64K fragment consumes 129736 bytes (44*2944)+200
883 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
884 *
885 * We will commit 4MB at one time. Should we cross that limit
886 * we will prune down to 3MB, making room for approx 8 big 64K
887 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800888 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000889 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
890 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800891 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800892 * Important NOTE! Fragment queue must be destroyed before MSL expires.
893 * RFC791 is wrong proposing to prolongate timer each fragment arrival
894 * by TTL.
895 */
896 net->ipv4.frags.timeout = IP_FRAG_TIME;
897
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200898 net->ipv4.frags.max_dist = 64;
Eric Dumazet093ba722018-03-31 12:58:44 -0700899 net->ipv4.frags.f = &ip4_frags;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200900
Eric Dumazet787bea72018-03-31 12:58:43 -0700901 res = inet_frags_init_net(&net->ipv4.frags);
902 if (res < 0)
903 return res;
904 res = ip4_frags_ns_ctl_register(net);
905 if (res < 0)
Eric Dumazet093ba722018-03-31 12:58:44 -0700906 inet_frags_exit_net(&net->ipv4.frags);
Eric Dumazet787bea72018-03-31 12:58:43 -0700907 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800908}
909
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000910static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800911{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700912 ip4_frags_ns_ctl_unregister(net);
Eric Dumazet093ba722018-03-31 12:58:44 -0700913 inet_frags_exit_net(&net->ipv4.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800914}
915
916static struct pernet_operations ip4_frags_ops = {
917 .init = ipv4_frags_init_net,
918 .exit = ipv4_frags_exit_net,
919};
920
Eric Dumazet648700f2018-03-31 12:58:49 -0700921
922static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
923{
924 return jhash2(data,
925 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
926}
927
928static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
929{
930 const struct inet_frag_queue *fq = data;
931
932 return jhash2((const u32 *)&fq->key.v4,
933 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
934}
935
936static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
937{
938 const struct frag_v4_compare_key *key = arg->key;
939 const struct inet_frag_queue *fq = ptr;
940
941 return !!memcmp(&fq->key, key, sizeof(*key));
942}
943
944static const struct rhashtable_params ip4_rhash_params = {
945 .head_offset = offsetof(struct inet_frag_queue, node),
946 .key_offset = offsetof(struct inet_frag_queue, key),
947 .key_len = sizeof(struct frag_v4_compare_key),
948 .hashfn = ip4_key_hashfn,
949 .obj_hashfn = ip4_obj_hashfn,
950 .obj_cmpfn = ip4_obj_cmpfn,
951 .automatic_shrinking = true,
952};
953
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700954void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700956 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700957 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700958 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700959 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200960 ip4_frags.frags_cache_name = ip_frag_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700961 ip4_frags.rhash_params = ip4_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200962 if (inet_frags_init(&ip4_frags))
963 panic("IP: failed to allocate ip4_frags cache\n");
Eric Dumazet483a6e42018-03-31 12:58:47 -0700964 ip4_frags_ctl_register();
965 register_pernet_subsys(&ip4_frags_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}