blob: 6e9ba9dfb5b2c227c73ff539db6bf9196a0859ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The IP fragmentation functionality.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
Alan Cox113aa832008-10-13 19:01:08 -07009 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Fixes:
12 * Alan Cox : Split from ip.c , see ip_input.c for history.
13 * David S. Miller : Begin massive cleanup...
14 * Andi Kleen : Add sysctls.
15 * xxxx : Overlapfrag bug.
16 * Ultima : ip_expire() kernel panic.
17 * Bill Hawes : Frag accounting and evictor fixes.
18 * John McDonald : 0 length frag bug.
19 * Alexey Kuznetsov: SMP races, threading, cleanup.
20 * Patrick McHardy : LRU queue of frag heads for evictor.
21 */
22
Joe Perchesafd465032012-03-12 07:03:32 +000023#define pr_fmt(fmt) "IPv4: " fmt
24
Herbert Xu89cee8b2005-12-13 23:14:27 -080025#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/jiffies.h>
30#include <linux/skbuff.h>
31#include <linux/list.h>
32#include <linux/ip.h>
33#include <linux/icmp.h>
34#include <linux/netdevice.h>
35#include <linux/jhash.h>
36#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Shan Weie9017b52010-01-23 01:57:42 -080038#include <net/route.h>
39#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <net/sock.h>
41#include <net/ip.h>
42#include <net/icmp.h>
43#include <net/checksum.h>
Herbert Xu89cee8b2005-12-13 23:14:27 -080044#include <net/inetpeer.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070045#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/tcp.h>
47#include <linux/udp.h>
48#include <linux/inet.h>
49#include <linux/netfilter_ipv4.h>
Eric Dumazet6623e3b2011-01-05 07:52:55 +000050#include <net/inet_ecn.h>
David Ahern385add92015-09-29 20:07:13 -070051#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
54 * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
55 * as well. Or notify me, at least. --ANK
56 */
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020057static const char ip_frag_cache_name[] = "ip4-frags";
Herbert Xu89cee8b2005-12-13 23:14:27 -080058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* Describe an entry in the "incomplete datagrams" queue. */
60struct ipq {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070061 struct inet_frag_queue q;
62
Eric Dumazet6623e3b2011-01-05 07:52:55 +000063 u8 ecn; /* RFC3168 support */
Florian Westphald6b915e2015-05-22 16:32:51 +020064 u16 max_df_size; /* largest frag with DF set seen */
Herbert Xu89cee8b2005-12-13 23:14:27 -080065 int iif;
66 unsigned int rid;
67 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Fabian Frederickaa1f7312014-11-04 20:44:04 +010070static u8 ip4_frag_ecn(u8 tos)
Eric Dumazet6623e3b2011-01-05 07:52:55 +000071{
Eric Dumazet5173cc02011-05-16 08:37:37 +000072 return 1 << (tos & INET_ECN_MASK);
Eric Dumazet6623e3b2011-01-05 07:52:55 +000073}
74
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070075static struct inet_frags ip4_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Peter Oskolkov4077ddb2018-10-10 12:30:15 -070077static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
78 struct sk_buff *prev_tail, struct net_device *dev);
Herbert Xu1706d582007-10-14 00:38:15 -070079
Pavel Emelyanovabd65232007-10-17 19:47:21 -070080
Florian Westphal36c77782014-07-24 16:50:29 +020081static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070082{
83 struct ipq *qp = container_of(q, struct ipq, q);
Gao feng54db0cc2012-06-08 01:21:40 +000084 struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
85 frags);
86 struct net *net = container_of(ipv4, struct net, ipv4);
87
Eric Dumazet23ce9c52018-10-10 12:29:56 -070088 const struct frag_v4_compare_key *key = a;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070089
Eric Dumazet23ce9c52018-10-10 12:29:56 -070090 q->key.v4 = *key;
91 qp->ecn = 0;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020092 qp->peer = q->net->max_dist ?
Eric Dumazet23ce9c52018-10-10 12:29:56 -070093 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
David Ahern192132b2015-08-27 16:07:03 -070094 NULL;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070095}
96
Fabian Frederickaa1f7312014-11-04 20:44:04 +010097static void ip4_frag_free(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070099 struct ipq *qp;
100
101 qp = container_of(q, struct ipq, q);
102 if (qp->peer)
103 inet_putpeer(qp->peer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Destruction primitives. */
108
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100109static void ipq_put(struct ipq *ipq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700111 inet_frag_put(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/* Kill ipq entry. It is not destroyed immediately,
115 * because caller (and someone more) holds reference count.
116 */
117static void ipq_kill(struct ipq *ipq)
118{
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700119 inet_frag_kill(&ipq->q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Andy Zhou5cf42282015-05-15 14:15:35 -0700122static bool frag_expire_skip_icmp(u32 user)
123{
124 return user == IP_DEFRAG_AF_PACKET ||
125 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
Andy Zhou8bc04862015-05-15 14:15:36 -0700126 __IP_DEFRAG_CONNTRACK_IN_END) ||
127 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
128 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
Andy Zhou5cf42282015-05-15 14:15:35 -0700129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
132 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
133 */
134static void ip_expire(unsigned long arg)
135{
Eric Dumazet49106f32018-10-10 12:29:58 -0700136 const struct iphdr *iph;
Peter Oskolkov10043952018-10-10 12:30:13 -0700137 struct sk_buff *head = NULL;
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700138 struct net *net;
Eric Dumazet49106f32018-10-10 12:29:58 -0700139 struct ipq *qp;
140 int err;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700141
142 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
Pavel Emelyanov84a3aa02008-07-16 20:19:08 -0700143 net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Eric Dumazet76568712017-03-22 08:57:15 -0700145 rcu_read_lock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700146 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200148 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out;
150
151 ipq_kill(qp);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700152 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Eric Dumazet49106f32018-10-10 12:29:58 -0700153 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200154
Dan Carpenter5a0f3402018-10-10 12:30:17 -0700155 if (!(qp->q.flags & INET_FRAG_FIRST_IN))
Eric Dumazet49106f32018-10-10 12:29:58 -0700156 goto out;
Nikolay Aleksandrov2e404f62014-08-01 12:29:47 +0200157
Peter Oskolkov10043952018-10-10 12:30:13 -0700158 /* sk_buff::dev and sk_buff::rbnode are unionized. So we
159 * pull the head out of the tree in order to be able to
160 * deal with head->dev.
161 */
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700162 head = inet_frag_pull_head(&qp->q);
163 if (!head)
164 goto out;
Eric Dumazet49106f32018-10-10 12:29:58 -0700165 head->dev = dev_get_by_index_rcu(net, qp->iif);
166 if (!head->dev)
167 goto out;
Eric Dumazet76568712017-03-22 08:57:15 -0700168
Shan Weie9017b52010-01-23 01:57:42 -0800169
Eric Dumazet49106f32018-10-10 12:29:58 -0700170 /* skb has no dst, perform route lookup again */
171 iph = ip_hdr(head);
172 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
David S. Millerc6cffba2012-07-26 11:14:38 +0000173 iph->tos, head->dev);
Eric Dumazet49106f32018-10-10 12:29:58 -0700174 if (err)
175 goto out;
Eric Dumazet64f3b9e2011-05-04 10:02:26 +0000176
Eric Dumazet49106f32018-10-10 12:29:58 -0700177 /* Only an end host needs to send an ICMP
178 * "Fragment Reassembly Timeout" message, per RFC792.
179 */
180 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
181 (skb_rtable(head)->rt_type != RTN_LOCAL))
182 goto out;
Eric Dumazet76568712017-03-22 08:57:15 -0700183
Eric Dumazet7a87ec92018-10-10 12:30:01 -0700184 spin_unlock(&qp->q.lock);
185 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
Eric Dumazet7a87ec92018-10-10 12:30:01 -0700186 goto out_rcu_unlock;
Shan Weie9017b52010-01-23 01:57:42 -0800187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700189 spin_unlock(&qp->q.lock);
Eric Dumazet76568712017-03-22 08:57:15 -0700190out_rcu_unlock:
191 rcu_read_unlock();
Peter Oskolkov10043952018-10-10 12:30:13 -0700192 if (head)
193 kfree_skb(head);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700194 ipq_put(qp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700197/* Find the correct entry in the "incomplete datagrams" queue for
198 * this IP datagram, and create new one, if nothing is found.
199 */
David Ahern9972f132015-08-13 14:59:09 -0600200static struct ipq *ip_find(struct net *net, struct iphdr *iph,
201 u32 user, int vif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700203 struct frag_v4_compare_key key = {
204 .saddr = iph->saddr,
205 .daddr = iph->daddr,
206 .user = user,
207 .vif = vif,
208 .id = iph->id,
209 .protocol = iph->protocol,
210 };
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700211 struct inet_frag_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700213 q = inet_frag_find(&net->ipv4.frags, &key);
Eric Dumazet965e2ad2018-10-10 12:29:59 -0700214 if (!q)
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000215 return NULL;
Eric Dumazet965e2ad2018-10-10 12:29:59 -0700216
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700217 return container_of(q, struct ipq, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Herbert Xu89cee8b2005-12-13 23:14:27 -0800220/* Is the fragment too far ahead to be part of ipq? */
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100221static int ip_frag_too_far(struct ipq *qp)
Herbert Xu89cee8b2005-12-13 23:14:27 -0800222{
223 struct inet_peer *peer = qp->peer;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200224 unsigned int max = qp->q.net->max_dist;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800225 unsigned int start, end;
226
227 int rc;
228
229 if (!peer || !max)
230 return 0;
231
232 start = qp->rid;
233 end = atomic_inc_return(&peer->rid);
234 qp->rid = end;
235
Peter Oskolkov10043952018-10-10 12:30:13 -0700236 rc = qp->q.fragments_tail && (end - start) > max;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800237
238 if (rc) {
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700239 struct net *net;
240
241 net = container_of(qp->q.net, struct net, ipv4.frags);
Eric Dumazetb45386e2016-04-27 16:44:35 -0700242 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800243 }
244
245 return rc;
246}
247
248static int ip_frag_reinit(struct ipq *qp)
249{
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000250 unsigned int sum_truesize = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800251
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800252 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700253 atomic_inc(&qp->q.refcnt);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800254 return -ETIMEDOUT;
255 }
256
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700257 sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments);
Florian Westphal0e60d242015-07-23 12:05:38 +0200258 sub_frag_mem_limit(qp->q.net, sum_truesize);
Herbert Xu89cee8b2005-12-13 23:14:27 -0800259
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200260 qp->q.flags = 0;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700261 qp->q.len = 0;
262 qp->q.meat = 0;
263 qp->q.fragments = NULL;
Peter Oskolkov10043952018-10-10 12:30:13 -0700264 qp->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000265 qp->q.fragments_tail = NULL;
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700266 qp->q.last_run_head = NULL;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800267 qp->iif = 0;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000268 qp->ecn = 0;
Herbert Xu89cee8b2005-12-13 23:14:27 -0800269
270 return 0;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* Add new segment to existing queue. */
Herbert Xu1706d582007-10-14 00:38:15 -0700274static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700276 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700277 int ihl, end, flags, offset;
278 struct sk_buff *prev_tail;
Herbert Xu1706d582007-10-14 00:38:15 -0700279 struct net_device *dev;
Florian Westphald6b915e2015-05-22 16:32:51 +0200280 unsigned int fragsize;
Herbert Xu1706d582007-10-14 00:38:15 -0700281 int err = -ENOENT;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000282 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200284 if (qp->q.flags & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto err;
286
Herbert Xu89cee8b2005-12-13 23:14:27 -0800287 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
Herbert Xu1706d582007-10-14 00:38:15 -0700288 unlikely(ip_frag_too_far(qp)) &&
289 unlikely(err = ip_frag_reinit(qp))) {
Herbert Xu89cee8b2005-12-13 23:14:27 -0800290 ipq_kill(qp);
291 goto err;
292 }
293
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000294 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700295 offset = ntohs(ip_hdr(skb)->frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 flags = offset & ~IP_OFFSET;
297 offset &= IP_OFFSET;
298 offset <<= 3; /* offset is in 8-byte chunks */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300299 ihl = ip_hdrlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Determine the position of this fragment. */
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200302 end = offset + skb->len - skb_network_offset(skb) - ihl;
Herbert Xu1706d582007-10-14 00:38:15 -0700303 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Is this the final fragment? */
306 if ((flags & IP_MF) == 0) {
307 /* If we already have some bits beyond end
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800308 * or have different end, the segment is corrupted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700310 if (end < qp->q.len ||
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200311 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700312 goto discard_qp;
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200313 qp->q.flags |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700314 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else {
316 if (end&7) {
317 end &= ~7;
318 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
319 skb->ip_summed = CHECKSUM_NONE;
320 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700321 if (end > qp->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 /* Some bits beyond end -> corruption. */
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200323 if (qp->q.flags & INET_FRAG_LAST_IN)
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700324 goto discard_qp;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700325 qp->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 }
328 if (end == offset)
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700329 goto discard_qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Herbert Xu1706d582007-10-14 00:38:15 -0700331 err = -ENOMEM;
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200332 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700333 goto discard_qp;
Herbert Xu1706d582007-10-14 00:38:15 -0700334
335 err = pskb_trim_rcsum(skb, end - offset);
336 if (err)
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700337 goto discard_qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Peter Oskolkov10043952018-10-10 12:30:13 -0700339 /* Note : skb->rbnode and skb->dev share the same location. */
340 dev = skb->dev;
341 /* Makes sure compiler wont do silly aliasing games */
342 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700344 prev_tail = qp->q.fragments_tail;
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700345 err = inet_frag_queue_insert(&qp->q, skb, offset, end);
346 if (err)
347 goto insert_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Eric Dumazetd8384862018-10-10 12:30:06 -0700349 if (dev)
350 qp->iif = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700352 qp->q.stamp = skb->tstamp;
353 qp->q.meat += skb->len;
Eric Dumazet6623e3b2011-01-05 07:52:55 +0000354 qp->ecn |= ecn;
Florian Westphal0e60d242015-07-23 12:05:38 +0200355 add_frag_mem_limit(qp->q.net, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (offset == 0)
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200357 qp->q.flags |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Florian Westphald6b915e2015-05-22 16:32:51 +0200359 fragsize = skb->len + ihl;
360
361 if (fragsize > qp->q.max_size)
362 qp->q.max_size = fragsize;
363
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200364 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
Florian Westphald6b915e2015-05-22 16:32:51 +0200365 fragsize > qp->max_df_size)
366 qp->max_df_size = fragsize;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +0200367
Nikolay Aleksandrov06aa8b82014-08-01 12:29:44 +0200368 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
Eric Dumazet97599dc2013-04-16 12:55:41 +0000369 qp->q.meat == qp->q.len) {
370 unsigned long orefdst = skb->_skb_refdst;
Herbert Xu1706d582007-10-14 00:38:15 -0700371
Eric Dumazet97599dc2013-04-16 12:55:41 +0000372 skb->_skb_refdst = 0UL;
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700373 err = ip_frag_reasm(qp, skb, prev_tail, dev);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000374 skb->_skb_refdst = orefdst;
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700375 if (err)
376 inet_frag_kill(&qp->q);
Eric Dumazet97599dc2013-04-16 12:55:41 +0000377 return err;
378 }
379
380 skb_dst_drop(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700381 return -EINPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700383insert_error:
384 if (err == IPFRAG_DUP) {
385 kfree_skb(skb);
386 return -EINVAL;
387 }
388 err = -EINVAL;
389 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
Peter Oskolkov82f36cb2018-10-10 12:30:07 -0700390discard_qp:
391 inet_frag_kill(&qp->q);
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700392 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393err:
394 kfree_skb(skb);
Herbert Xu1706d582007-10-14 00:38:15 -0700395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/* Build a new IP datagram from all its fragments. */
Peter Oskolkov10043952018-10-10 12:30:13 -0700399static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700400 struct sk_buff *prev_tail, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700402 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct iphdr *iph;
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700404 void *reasm_data;
405 int len, err;
Eric Dumazet5173cc02011-05-16 08:37:37 +0000406 u8 ecn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 ipq_kill(qp);
409
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000410 ecn = ip_frag_ecn_table[qp->ecn];
Eric Dumazet5173cc02011-05-16 08:37:37 +0000411 if (unlikely(ecn == 0xff)) {
412 err = -EINVAL;
413 goto out_fail;
414 }
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700415
Herbert Xu1706d582007-10-14 00:38:15 -0700416 /* Make the one we just received the head. */
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700417 reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail);
418 if (!reasm_data)
419 goto out_nomem;
Herbert Xu1706d582007-10-14 00:38:15 -0700420
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700421 len = ip_hdrlen(skb) + qp->q.len;
Herbert Xu1706d582007-10-14 00:38:15 -0700422 err = -E2BIG;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800423 if (len > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto out_oversize;
425
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700426 inet_frag_reasm_finish(&qp->q, skb, reasm_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700428 skb->dev = dev;
429 IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700431 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 iph->tot_len = htons(len);
Eric Dumazet5173cc02011-05-16 08:37:37 +0000433 iph->tos |= ecn;
Florian Westphald6b915e2015-05-22 16:32:51 +0200434
435 /* When we set IP_DF on a refragmented skb we must also force a
436 * call to ip_fragment to avoid forwarding a DF-skb of size s while
437 * original sender only sent fragments of size f (where f < s).
438 *
439 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
440 * frag seen to avoid sending tiny DF-fragments in case skb was built
441 * from one very small df-fragment and one large non-df frag.
442 */
443 if (qp->max_df_size == qp->q.max_size) {
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700444 IPCB(skb)->flags |= IPSKB_FRAG_PMTU;
Florian Westphald6b915e2015-05-22 16:32:51 +0200445 iph->frag_off = htons(IP_DF);
446 } else {
447 iph->frag_off = 0;
448 }
449
Edward Hyunkoo Jee0848f642015-07-21 09:43:59 +0200450 ip_send_check(iph);
451
Eric Dumazetb45386e2016-04-27 16:44:35 -0700452 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700453 qp->q.fragments = NULL;
Peter Oskolkov10043952018-10-10 12:30:13 -0700454 qp->q.rb_fragments = RB_ROOT;
Changli Gaod6bebca2010-06-29 04:39:37 +0000455 qp->q.fragments_tail = NULL;
Peter Oskolkov4077ddb2018-10-10 12:30:15 -0700456 qp->q.last_run_head = NULL;
Herbert Xu1706d582007-10-14 00:38:15 -0700457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459out_nomem:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800460 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
David Howells45542472007-10-17 21:37:22 -0700461 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out_fail;
463out_oversize:
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700464 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465out_fail:
Eric Dumazetb45386e2016-04-27 16:44:35 -0700466 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Herbert Xu1706d582007-10-14 00:38:15 -0700467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/* Process an incoming IP datagram fragment. */
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500471int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David Ahern9972f132015-08-13 14:59:09 -0600473 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
David Ahern385add92015-09-29 20:07:13 -0700474 int vif = l3mdev_master_ifindex_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct ipq *qp;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900476
Eric Dumazetb45386e2016-04-27 16:44:35 -0700477 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
Joe Stringer8282f272016-01-22 15:49:12 -0800478 skb_orphan(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Lookup (or create) queue header */
David Ahern9972f132015-08-13 14:59:09 -0600481 qp = ip_find(net, ip_hdr(skb), user, vif);
Ian Morris00db4122015-04-03 09:17:27 +0100482 if (qp) {
Herbert Xu1706d582007-10-14 00:38:15 -0700483 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700485 spin_lock(&qp->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Herbert Xu1706d582007-10-14 00:38:15 -0700487 ret = ip_frag_queue(qp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700489 spin_unlock(&qp->q.lock);
Pavel Emelyanov4b6cb5d2007-10-15 02:41:09 -0700490 ipq_put(qp);
Herbert Xu776c7292007-10-14 00:38:32 -0700491 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Eric Dumazetb45386e2016-04-27 16:44:35 -0700494 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 kfree_skb(skb);
Herbert Xu776c7292007-10-14 00:38:32 -0700496 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000498EXPORT_SYMBOL(ip_defrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500500struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000501{
Johannes Berg1bf37512012-12-09 23:41:06 +0000502 struct iphdr iph;
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300503 int netoff;
Eric Dumazetbc416d92011-10-06 10:28:31 +0000504 u32 len;
505
506 if (skb->protocol != htons(ETH_P_IP))
507 return skb;
508
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300509 netoff = skb_network_offset(skb);
510
511 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000512 return skb;
513
Johannes Berg1bf37512012-12-09 23:41:06 +0000514 if (iph.ihl < 5 || iph.version != 4)
Eric Dumazetbc416d92011-10-06 10:28:31 +0000515 return skb;
516
Johannes Berg1bf37512012-12-09 23:41:06 +0000517 len = ntohs(iph.tot_len);
Alexander Drozdov3e32e732015-03-05 10:29:39 +0300518 if (skb->len < netoff + len || len < (iph.ihl * 4))
Johannes Berg1bf37512012-12-09 23:41:06 +0000519 return skb;
520
521 if (ip_is_fragment(&iph)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +0000522 skb = skb_share_check(skb, GFP_ATOMIC);
523 if (skb) {
Cong Wang0376b832018-11-01 12:02:37 -0700524 if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
525 kfree_skb(skb);
526 return NULL;
527 }
528 if (pskb_trim_rcsum(skb, netoff + len)) {
529 kfree_skb(skb);
530 return NULL;
531 }
Eric Dumazetbc416d92011-10-06 10:28:31 +0000532 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
Eric W. Biederman19bcf9f2015-10-09 13:44:54 -0500533 if (ip_defrag(net, skb, user))
Eric Dumazetbc416d92011-10-06 10:28:31 +0000534 return NULL;
Tom Herbert7539fad2013-12-15 22:12:18 -0800535 skb_clear_hash(skb);
Eric Dumazetbc416d92011-10-06 10:28:31 +0000536 }
537 }
538 return skb;
539}
540EXPORT_SYMBOL(ip_check_defrag);
541
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800542#ifdef CONFIG_SYSCTL
Eric Dumazetd8384862018-10-10 12:30:06 -0700543static int dist_min;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800544
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700545static struct ctl_table ip4_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800546 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800547 .procname = "ipfrag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800548 .data = &init_net.ipv4.frags.high_thresh,
Eric Dumazet7f617062018-10-10 12:30:00 -0700549 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800550 .mode = 0644,
Eric Dumazet7f617062018-10-10 12:30:00 -0700551 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200552 .extra1 = &init_net.ipv4.frags.low_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800553 },
554 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800555 .procname = "ipfrag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800556 .data = &init_net.ipv4.frags.low_thresh,
Eric Dumazet7f617062018-10-10 12:30:00 -0700557 .maxlen = sizeof(unsigned long),
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800558 .mode = 0644,
Eric Dumazet7f617062018-10-10 12:30:00 -0700559 .proc_handler = proc_doulongvec_minmax,
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200560 .extra2 = &init_net.ipv4.frags.high_thresh
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800561 },
562 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800563 .procname = "ipfrag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800564 .data = &init_net.ipv4.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800565 .maxlen = sizeof(int),
566 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800567 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800568 },
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200569 {
570 .procname = "ipfrag_max_dist",
571 .data = &init_net.ipv4.frags.max_dist,
572 .maxlen = sizeof(int),
573 .mode = 0644,
574 .proc_handler = proc_dointvec_minmax,
Eric Dumazetd8384862018-10-10 12:30:06 -0700575 .extra1 = &dist_min,
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200576 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700577 { }
578};
579
Florian Westphale3a57d12014-07-24 16:50:35 +0200580/* secret interval has been deprecated */
581static int ip4_frags_secret_interval_unused;
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700582static struct ctl_table ip4_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800583 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800584 .procname = "ipfrag_secret_interval",
Florian Westphale3a57d12014-07-24 16:50:35 +0200585 .data = &ip4_frags_secret_interval_unused,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800586 .maxlen = sizeof(int),
587 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800588 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800589 },
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800590 { }
591};
592
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000593static int __net_init ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800594{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800595 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800596 struct ctl_table_header *hdr;
597
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700598 table = ip4_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800599 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700600 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100601 if (!table)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800602 goto err_alloc;
603
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800604 table[0].data = &net->ipv4.frags.high_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200605 table[0].extra1 = &net->ipv4.frags.low_thresh;
606 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800607 table[1].data = &net->ipv4.frags.low_thresh;
Nikolay Aleksandrov1bab4c72014-07-24 16:50:37 +0200608 table[1].extra2 = &net->ipv4.frags.high_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800609 table[2].data = &net->ipv4.frags.timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200610 table[3].data = &net->ipv4.frags.max_dist;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800611 }
612
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000613 hdr = register_net_sysctl(net, "net/ipv4", table);
Ian Morris51456b22015-04-03 09:17:26 +0100614 if (!hdr)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800615 goto err_reg;
616
617 net->ipv4.frags_hdr = hdr;
618 return 0;
619
620err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800621 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800622 kfree(table);
623err_alloc:
624 return -ENOMEM;
625}
626
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000627static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800628{
629 struct ctl_table *table;
630
631 table = net->ipv4.frags_hdr->ctl_table_arg;
632 unregister_net_sysctl_table(net->ipv4.frags_hdr);
633 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800634}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700635
Fabian Frederick57a02c32014-10-01 19:18:57 +0200636static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700637{
Eric W. Biederman43444752012-04-19 13:22:55 +0000638 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700639}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800640#else
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100641static int ip4_frags_ns_ctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800642{
643 return 0;
644}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800645
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100646static void ip4_frags_ns_ctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800647{
648}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700649
Fabian Frederickaa1f7312014-11-04 20:44:04 +0100650static void __init ip4_frags_ctl_register(void)
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700651{
652}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800653#endif
654
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000655static int __net_init ipv4_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800656{
Eric Dumazet7fca7712018-10-10 12:29:49 -0700657 int res;
658
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000659 /* Fragment cache limits.
660 *
661 * The fragment memory accounting code, (tries to) account for
662 * the real memory usage, by measuring both the size of frag
663 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
664 * and the SKB's truesize.
665 *
666 * A 64K fragment consumes 129736 bytes (44*2944)+200
667 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
668 *
669 * We will commit 4MB at one time. Should we cross that limit
670 * we will prune down to 3MB, making room for approx 8 big 64K
671 * fragments 8x128k.
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800672 */
Jesper Dangaard Brouerc2a93662013-01-15 07:16:35 +0000673 net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
674 net->ipv4.frags.low_thresh = 3 * 1024 * 1024;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800675 /*
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800676 * Important NOTE! Fragment queue must be destroyed before MSL expires.
677 * RFC791 is wrong proposing to prolongate timer each fragment arrival
678 * by TTL.
679 */
680 net->ipv4.frags.timeout = IP_FRAG_TIME;
681
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200682 net->ipv4.frags.max_dist = 64;
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700683 net->ipv4.frags.f = &ip4_frags;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +0200684
Eric Dumazet7fca7712018-10-10 12:29:49 -0700685 res = inet_frags_init_net(&net->ipv4.frags);
686 if (res < 0)
687 return res;
688 res = ip4_frags_ns_ctl_register(net);
689 if (res < 0)
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700690 inet_frags_exit_net(&net->ipv4.frags);
Eric Dumazet7fca7712018-10-10 12:29:49 -0700691 return res;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800692}
693
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000694static void __net_exit ipv4_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800695{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700696 ip4_frags_ns_ctl_unregister(net);
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700697 inet_frags_exit_net(&net->ipv4.frags);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800698}
699
700static struct pernet_operations ip4_frags_ops = {
701 .init = ipv4_frags_init_net,
702 .exit = ipv4_frags_exit_net,
703};
704
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700705
706static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
707{
708 return jhash2(data,
709 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
710}
711
712static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
713{
714 const struct inet_frag_queue *fq = data;
715
716 return jhash2((const u32 *)&fq->key.v4,
717 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
718}
719
720static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
721{
722 const struct frag_v4_compare_key *key = arg->key;
723 const struct inet_frag_queue *fq = ptr;
724
725 return !!memcmp(&fq->key, key, sizeof(*key));
726}
727
728static const struct rhashtable_params ip4_rhash_params = {
729 .head_offset = offsetof(struct inet_frag_queue, node),
730 .key_offset = offsetof(struct inet_frag_queue, key),
731 .key_len = sizeof(struct frag_v4_compare_key),
732 .hashfn = ip4_key_hashfn,
733 .obj_hashfn = ip4_obj_hashfn,
734 .obj_cmpfn = ip4_obj_cmpfn,
735 .automatic_shrinking = true,
736};
737
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700738void __init ipfrag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700740 ip4_frags.constructor = ip4_frag_init;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700741 ip4_frags.destructor = ip4_frag_free;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700742 ip4_frags.qsize = sizeof(struct ipq);
Pavel Emelyanove521db92007-10-17 19:45:23 -0700743 ip4_frags.frag_expire = ip_expire;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200744 ip4_frags.frags_cache_name = ip_frag_cache_name;
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700745 ip4_frags.rhash_params = ip4_rhash_params;
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200746 if (inet_frags_init(&ip4_frags))
747 panic("IP: failed to allocate ip4_frags cache\n");
Eric Dumazetbbf6d862018-10-10 12:29:51 -0700748 ip4_frags_ctl_register();
749 register_pernet_subsys(&ip4_frags_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}