Merge tag 'for-linus-2019-08-17' of git://git.kernel.dk/linux-block
[linux-block.git] / net / ipv4 / ip_fragment.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
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.
e905a9ed 8 *
1da177e4 9 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
113aa838 10 * Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
11 *
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
afd46503
JP
24#define pr_fmt(fmt) "IPv4: " fmt
25
89cee8b1 26#include <linux/compiler.h>
1da177e4
LT
27#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>
5a0e3ad6 38#include <linux/slab.h>
e9017b55
SW
39#include <net/route.h>
40#include <net/dst.h>
1da177e4
LT
41#include <net/sock.h>
42#include <net/ip.h>
43#include <net/icmp.h>
44#include <net/checksum.h>
89cee8b1 45#include <net/inetpeer.h>
5ab11c98 46#include <net/inet_frag.h>
1da177e4
LT
47#include <linux/tcp.h>
48#include <linux/udp.h>
49#include <linux/inet.h>
50#include <linux/netfilter_ipv4.h>
6623e3b2 51#include <net/inet_ecn.h>
385add90 52#include <net/l3mdev.h>
1da177e4
LT
53
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 */
d4ad4d22 58static const char ip_frag_cache_name[] = "ip4-frags";
89cee8b1 59
1da177e4
LT
60/* Describe an entry in the "incomplete datagrams" queue. */
61struct ipq {
5ab11c98
PE
62 struct inet_frag_queue q;
63
6623e3b2 64 u8 ecn; /* RFC3168 support */
d6b915e2 65 u16 max_df_size; /* largest frag with DF set seen */
89cee8b1
HX
66 int iif;
67 unsigned int rid;
68 struct inet_peer *peer;
1da177e4
LT
69};
70
aa1f731e 71static u8 ip4_frag_ecn(u8 tos)
6623e3b2 72{
5173cc05 73 return 1 << (tos & INET_ECN_MASK);
6623e3b2
ED
74}
75
7eb95156 76static struct inet_frags ip4_frags;
1da177e4 77
a4fd284a
PO
78static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
79 struct sk_buff *prev_tail, struct net_device *dev);
1706d587 80
abd6523d 81
36c77782 82static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
c6fda282
PE
83{
84 struct ipq *qp = container_of(q, struct ipq, q);
a39aca67 85 struct net *net = q->fqdir->net;
54db0cc2 86
648700f7 87 const struct frag_v4_compare_key *key = a;
c6fda282 88
648700f7
ED
89 q->key.v4 = *key;
90 qp->ecn = 0;
6ce3b4dc 91 qp->peer = q->fqdir->max_dist ?
648700f7 92 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
192132b9 93 NULL;
c6fda282
PE
94}
95
aa1f731e 96static void ip4_frag_free(struct inet_frag_queue *q)
1da177e4 97{
1e4b8287
PE
98 struct ipq *qp;
99
100 qp = container_of(q, struct ipq, q);
101 if (qp->peer)
102 inet_putpeer(qp->peer);
1da177e4
LT
103}
104
1da177e4
LT
105
106/* Destruction primitives. */
107
aa1f731e 108static void ipq_put(struct ipq *ipq)
1da177e4 109{
093ba729 110 inet_frag_put(&ipq->q);
1da177e4
LT
111}
112
113/* Kill ipq entry. It is not destroyed immediately,
114 * because caller (and someone more) holds reference count.
115 */
116static void ipq_kill(struct ipq *ipq)
117{
093ba729 118 inet_frag_kill(&ipq->q);
1da177e4
LT
119}
120
5cf42280
AZ
121static bool frag_expire_skip_icmp(u32 user)
122{
123 return user == IP_DEFRAG_AF_PACKET ||
124 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
8bc04864
AZ
125 __IP_DEFRAG_CONNTRACK_IN_END) ||
126 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
127 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
5cf42280
AZ
128}
129
1da177e4
LT
130/*
131 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
132 */
78802011 133static void ip_expire(struct timer_list *t)
1da177e4 134{
78802011 135 struct inet_frag_queue *frag = from_timer(frag, t, timer);
399d1404 136 const struct iphdr *iph;
fa0f5273 137 struct sk_buff *head = NULL;
84a3aa00 138 struct net *net;
399d1404
ED
139 struct ipq *qp;
140 int err;
e521db9d 141
78802011 142 qp = container_of(frag, struct ipq, q);
a39aca67 143 net = qp->q.fqdir->net;
1da177e4 144
ec4fbd64 145 rcu_read_lock();
d5dd8879
ED
146
147 if (qp->q.fqdir->dead)
148 goto out_rcu_unlock;
149
5ab11c98 150 spin_lock(&qp->q.lock);
1da177e4 151
06aa8b8a 152 if (qp->q.flags & INET_FRAG_COMPLETE)
1da177e4
LT
153 goto out;
154
155 ipq_kill(qp);
b45386ef 156 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
399d1404 157 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
2e404f63 158
70837ffe 159 if (!(qp->q.flags & INET_FRAG_FIRST_IN))
399d1404 160 goto out;
2e404f63 161
fa0f5273
PO
162 /* sk_buff::dev and sk_buff::rbnode are unionized. So we
163 * pull the head out of the tree in order to be able to
164 * deal with head->dev.
165 */
c23f35d1
PO
166 head = inet_frag_pull_head(&qp->q);
167 if (!head)
168 goto out;
399d1404
ED
169 head->dev = dev_get_by_index_rcu(net, qp->iif);
170 if (!head->dev)
171 goto out;
ec4fbd64 172
e9017b55 173
399d1404
ED
174 /* skb has no dst, perform route lookup again */
175 iph = ip_hdr(head);
176 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
c6cffba4 177 iph->tos, head->dev);
399d1404
ED
178 if (err)
179 goto out;
180
181 /* Only an end host needs to send an ICMP
182 * "Fragment Reassembly Timeout" message, per RFC792.
183 */
184 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
185 (skb_rtable(head)->rt_type != RTN_LOCAL))
186 goto out;
187
1eec5d56
ED
188 spin_unlock(&qp->q.lock);
189 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
1eec5d56 190 goto out_rcu_unlock;
399d1404 191
1da177e4 192out:
5ab11c98 193 spin_unlock(&qp->q.lock);
ec4fbd64
ED
194out_rcu_unlock:
195 rcu_read_unlock();
1d08962f 196 kfree_skb(head);
4b6cb5d8 197 ipq_put(qp);
1da177e4
LT
198}
199
abd6523d
PE
200/* Find the correct entry in the "incomplete datagrams" queue for
201 * this IP datagram, and create new one, if nothing is found.
202 */
9972f134
DA
203static struct ipq *ip_find(struct net *net, struct iphdr *iph,
204 u32 user, int vif)
1da177e4 205{
648700f7
ED
206 struct frag_v4_compare_key key = {
207 .saddr = iph->saddr,
208 .daddr = iph->daddr,
209 .user = user,
210 .vif = vif,
211 .id = iph->id,
212 .protocol = iph->protocol,
213 };
c6fda282 214 struct inet_frag_queue *q;
9a375803 215
4907abc6 216 q = inet_frag_find(net->ipv4.fqdir, &key);
2d44ed22 217 if (!q)
5a3da1fe 218 return NULL;
2d44ed22 219
c6fda282 220 return container_of(q, struct ipq, q);
1da177e4
LT
221}
222
89cee8b1 223/* Is the fragment too far ahead to be part of ipq? */
aa1f731e 224static int ip_frag_too_far(struct ipq *qp)
89cee8b1
HX
225{
226 struct inet_peer *peer = qp->peer;
6ce3b4dc 227 unsigned int max = qp->q.fqdir->max_dist;
89cee8b1
HX
228 unsigned int start, end;
229
230 int rc;
231
232 if (!peer || !max)
233 return 0;
234
235 start = qp->rid;
236 end = atomic_inc_return(&peer->rid);
237 qp->rid = end;
238
fa0f5273 239 rc = qp->q.fragments_tail && (end - start) > max;
89cee8b1 240
a39aca67
ED
241 if (rc)
242 __IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS);
89cee8b1
HX
243
244 return rc;
245}
246
247static int ip_frag_reinit(struct ipq *qp)
248{
d433673e 249 unsigned int sum_truesize = 0;
89cee8b1 250
6ce3b4dc 251 if (!mod_timer(&qp->q.timer, jiffies + qp->q.fqdir->timeout)) {
edcb6918 252 refcount_inc(&qp->q.refcnt);
89cee8b1
HX
253 return -ETIMEDOUT;
254 }
255
a4fd284a 256 sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments);
6ce3b4dc 257 sub_frag_mem_limit(qp->q.fqdir, sum_truesize);
89cee8b1 258
06aa8b8a 259 qp->q.flags = 0;
5ab11c98
PE
260 qp->q.len = 0;
261 qp->q.meat = 0;
fa0f5273 262 qp->q.rb_fragments = RB_ROOT;
d6bebca9 263 qp->q.fragments_tail = NULL;
a4fd284a 264 qp->q.last_run_head = NULL;
89cee8b1 265 qp->iif = 0;
6623e3b2 266 qp->ecn = 0;
89cee8b1
HX
267
268 return 0;
269}
270
1da177e4 271/* Add new segment to existing queue. */
1706d587 272static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
1da177e4 273{
a39aca67 274 struct net *net = qp->q.fqdir->net;
c23f35d1
PO
275 int ihl, end, flags, offset;
276 struct sk_buff *prev_tail;
1706d587 277 struct net_device *dev;
d6b915e2 278 unsigned int fragsize;
1706d587 279 int err = -ENOENT;
6623e3b2 280 u8 ecn;
1da177e4 281
06aa8b8a 282 if (qp->q.flags & INET_FRAG_COMPLETE)
1da177e4
LT
283 goto err;
284
89cee8b1 285 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
1706d587
HX
286 unlikely(ip_frag_too_far(qp)) &&
287 unlikely(err = ip_frag_reinit(qp))) {
89cee8b1
HX
288 ipq_kill(qp);
289 goto err;
290 }
291
6623e3b2 292 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
eddc9ec5 293 offset = ntohs(ip_hdr(skb)->frag_off);
1da177e4
LT
294 flags = offset & ~IP_OFFSET;
295 offset &= IP_OFFSET;
296 offset <<= 3; /* offset is in 8-byte chunks */
c9bdd4b5 297 ihl = ip_hdrlen(skb);
1da177e4
LT
298
299 /* Determine the position of this fragment. */
0848f642 300 end = offset + skb->len - skb_network_offset(skb) - ihl;
1706d587 301 err = -EINVAL;
1da177e4
LT
302
303 /* Is this the final fragment? */
304 if ((flags & IP_MF) == 0) {
305 /* If we already have some bits beyond end
42b2aa86 306 * or have different end, the segment is corrupted.
1da177e4 307 */
5ab11c98 308 if (end < qp->q.len ||
06aa8b8a 309 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
0ff89efb 310 goto discard_qp;
06aa8b8a 311 qp->q.flags |= INET_FRAG_LAST_IN;
5ab11c98 312 qp->q.len = end;
1da177e4
LT
313 } else {
314 if (end&7) {
315 end &= ~7;
316 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
317 skb->ip_summed = CHECKSUM_NONE;
318 }
5ab11c98 319 if (end > qp->q.len) {
1da177e4 320 /* Some bits beyond end -> corruption. */
06aa8b8a 321 if (qp->q.flags & INET_FRAG_LAST_IN)
0ff89efb 322 goto discard_qp;
5ab11c98 323 qp->q.len = end;
1da177e4
LT
324 }
325 }
326 if (end == offset)
0ff89efb 327 goto discard_qp;
1da177e4 328
1706d587 329 err = -ENOMEM;
0848f642 330 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
0ff89efb 331 goto discard_qp;
1706d587
HX
332
333 err = pskb_trim_rcsum(skb, end - offset);
334 if (err)
0ff89efb 335 goto discard_qp;
1da177e4 336
fa0f5273
PO
337 /* Note : skb->rbnode and skb->dev share the same location. */
338 dev = skb->dev;
339 /* Makes sure compiler wont do silly aliasing games */
340 barrier();
1da177e4 341
a4fd284a 342 prev_tail = qp->q.fragments_tail;
c23f35d1
PO
343 err = inet_frag_queue_insert(&qp->q, skb, offset, end);
344 if (err)
345 goto insert_error;
1da177e4 346
bf663371
ED
347 if (dev)
348 qp->iif = dev->ifindex;
1da177e4 349
5ab11c98
PE
350 qp->q.stamp = skb->tstamp;
351 qp->q.meat += skb->len;
6623e3b2 352 qp->ecn |= ecn;
6ce3b4dc 353 add_frag_mem_limit(qp->q.fqdir, skb->truesize);
1da177e4 354 if (offset == 0)
06aa8b8a 355 qp->q.flags |= INET_FRAG_FIRST_IN;
1da177e4 356
d6b915e2
FW
357 fragsize = skb->len + ihl;
358
359 if (fragsize > qp->q.max_size)
360 qp->q.max_size = fragsize;
361
5f2d04f1 362 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
d6b915e2
FW
363 fragsize > qp->max_df_size)
364 qp->max_df_size = fragsize;
5f2d04f1 365
06aa8b8a 366 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
97599dc7
ED
367 qp->q.meat == qp->q.len) {
368 unsigned long orefdst = skb->_skb_refdst;
1706d587 369
97599dc7 370 skb->_skb_refdst = 0UL;
a4fd284a 371 err = ip_frag_reasm(qp, skb, prev_tail, dev);
97599dc7 372 skb->_skb_refdst = orefdst;
0ff89efb
PO
373 if (err)
374 inet_frag_kill(&qp->q);
97599dc7
ED
375 return err;
376 }
377
378 skb_dst_drop(skb);
1706d587 379 return -EINPROGRESS;
1da177e4 380
c23f35d1
PO
381insert_error:
382 if (err == IPFRAG_DUP) {
383 kfree_skb(skb);
384 return -EINVAL;
385 }
386 err = -EINVAL;
0ff89efb 387 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
7969e5c4
PO
388discard_qp:
389 inet_frag_kill(&qp->q);
c23f35d1 390 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
1da177e4
LT
391err:
392 kfree_skb(skb);
1706d587 393 return err;
1da177e4
LT
394}
395
1da177e4 396/* Build a new IP datagram from all its fragments. */
fa0f5273 397static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
a4fd284a 398 struct sk_buff *prev_tail, struct net_device *dev)
1da177e4 399{
a39aca67 400 struct net *net = qp->q.fqdir->net;
1da177e4 401 struct iphdr *iph;
c23f35d1
PO
402 void *reasm_data;
403 int len, err;
5173cc05 404 u8 ecn;
1da177e4
LT
405
406 ipq_kill(qp);
407
be991971 408 ecn = ip_frag_ecn_table[qp->ecn];
5173cc05
ED
409 if (unlikely(ecn == 0xff)) {
410 err = -EINVAL;
411 goto out_fail;
412 }
1706d587 413
c23f35d1
PO
414 /* Make the one we just received the head. */
415 reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail);
416 if (!reasm_data)
417 goto out_nomem;
1da177e4 418
c23f35d1 419 len = ip_hdrlen(skb) + qp->q.len;
1706d587 420 err = -E2BIG;
132adf54 421 if (len > 65535)
1da177e4
LT
422 goto out_oversize;
423
c23f35d1 424 inet_frag_reasm_finish(&qp->q, skb, reasm_data);
1da177e4 425
c23f35d1
PO
426 skb->dev = dev;
427 IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
1da177e4 428
c23f35d1 429 iph = ip_hdr(skb);
1da177e4 430 iph->tot_len = htons(len);
5173cc05 431 iph->tos |= ecn;
d6b915e2
FW
432
433 /* When we set IP_DF on a refragmented skb we must also force a
434 * call to ip_fragment to avoid forwarding a DF-skb of size s while
435 * original sender only sent fragments of size f (where f < s).
436 *
437 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
438 * frag seen to avoid sending tiny DF-fragments in case skb was built
439 * from one very small df-fragment and one large non-df frag.
440 */
441 if (qp->max_df_size == qp->q.max_size) {
c23f35d1 442 IPCB(skb)->flags |= IPSKB_FRAG_PMTU;
d6b915e2
FW
443 iph->frag_off = htons(IP_DF);
444 } else {
445 iph->frag_off = 0;
446 }
447
0848f642
EHJ
448 ip_send_check(iph);
449
b45386ef 450 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
fa0f5273 451 qp->q.rb_fragments = RB_ROOT;
d6bebca9 452 qp->q.fragments_tail = NULL;
a4fd284a 453 qp->q.last_run_head = NULL;
1706d587 454 return 0;
1da177e4
LT
455
456out_nomem:
ba7a46f1 457 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
45542479 458 err = -ENOMEM;
1da177e4
LT
459 goto out_fail;
460out_oversize:
648700f7 461 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
1da177e4 462out_fail:
b45386ef 463 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
1706d587 464 return err;
1da177e4
LT
465}
466
467/* Process an incoming IP datagram fragment. */
19bcf9f2 468int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
1da177e4 469{
9972f134 470 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
385add90 471 int vif = l3mdev_master_ifindex_rcu(dev);
1da177e4 472 struct ipq *qp;
e905a9ed 473
b45386ef 474 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
8282f274 475 skb_orphan(skb);
1da177e4 476
1da177e4 477 /* Lookup (or create) queue header */
9972f134 478 qp = ip_find(net, ip_hdr(skb), user, vif);
00db4124 479 if (qp) {
1706d587 480 int ret;
1da177e4 481
5ab11c98 482 spin_lock(&qp->q.lock);
1da177e4 483
1706d587 484 ret = ip_frag_queue(qp, skb);
1da177e4 485
5ab11c98 486 spin_unlock(&qp->q.lock);
4b6cb5d8 487 ipq_put(qp);
776c729e 488 return ret;
1da177e4
LT
489 }
490
b45386ef 491 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
1da177e4 492 kfree_skb(skb);
776c729e 493 return -ENOMEM;
1da177e4 494}
4bc2f18b 495EXPORT_SYMBOL(ip_defrag);
1da177e4 496
19bcf9f2 497struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
bc416d97 498{
1bf3751e 499 struct iphdr iph;
3e32e733 500 int netoff;
bc416d97
ED
501 u32 len;
502
503 if (skb->protocol != htons(ETH_P_IP))
504 return skb;
505
3e32e733
AD
506 netoff = skb_network_offset(skb);
507
508 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
bc416d97
ED
509 return skb;
510
1bf3751e 511 if (iph.ihl < 5 || iph.version != 4)
bc416d97 512 return skb;
1bf3751e
JB
513
514 len = ntohs(iph.tot_len);
3e32e733 515 if (skb->len < netoff + len || len < (iph.ihl * 4))
bc416d97
ED
516 return skb;
517
1bf3751e 518 if (ip_is_fragment(&iph)) {
bc416d97
ED
519 skb = skb_share_check(skb, GFP_ATOMIC);
520 if (skb) {
7de414a9
CW
521 if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
522 kfree_skb(skb);
523 return NULL;
524 }
525 if (pskb_trim_rcsum(skb, netoff + len)) {
526 kfree_skb(skb);
527 return NULL;
528 }
bc416d97 529 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
19bcf9f2 530 if (ip_defrag(net, skb, user))
bc416d97 531 return NULL;
7539fadc 532 skb_clear_hash(skb);
bc416d97
ED
533 }
534 }
535 return skb;
536}
537EXPORT_SYMBOL(ip_check_defrag);
538
8d8354d2 539#ifdef CONFIG_SYSCTL
3d234012 540static int dist_min;
8d8354d2 541
0a64b4b8 542static struct ctl_table ip4_frags_ns_ctl_table[] = {
8d8354d2 543 {
8d8354d2 544 .procname = "ipfrag_high_thresh",
3e67f106 545 .maxlen = sizeof(unsigned long),
8d8354d2 546 .mode = 0644,
3e67f106 547 .proc_handler = proc_doulongvec_minmax,
8d8354d2
PE
548 },
549 {
8d8354d2 550 .procname = "ipfrag_low_thresh",
3e67f106 551 .maxlen = sizeof(unsigned long),
8d8354d2 552 .mode = 0644,
3e67f106 553 .proc_handler = proc_doulongvec_minmax,
8d8354d2
PE
554 },
555 {
8d8354d2 556 .procname = "ipfrag_time",
8d8354d2
PE
557 .maxlen = sizeof(int),
558 .mode = 0644,
6d9f239a 559 .proc_handler = proc_dointvec_jiffies,
8d8354d2 560 },
0fbf4cb2
NB
561 {
562 .procname = "ipfrag_max_dist",
0fbf4cb2
NB
563 .maxlen = sizeof(int),
564 .mode = 0644,
565 .proc_handler = proc_dointvec_minmax,
3d234012 566 .extra1 = &dist_min,
0fbf4cb2 567 },
7d291ebb
PE
568 { }
569};
570
e3a57d18
FW
571/* secret interval has been deprecated */
572static int ip4_frags_secret_interval_unused;
7d291ebb 573static struct ctl_table ip4_frags_ctl_table[] = {
8d8354d2 574 {
8d8354d2 575 .procname = "ipfrag_secret_interval",
e3a57d18 576 .data = &ip4_frags_secret_interval_unused,
8d8354d2
PE
577 .maxlen = sizeof(int),
578 .mode = 0644,
6d9f239a 579 .proc_handler = proc_dointvec_jiffies,
8d8354d2 580 },
8d8354d2
PE
581 { }
582};
583
2c8c1e72 584static int __net_init ip4_frags_ns_ctl_register(struct net *net)
8d8354d2 585{
e4a2d5c2 586 struct ctl_table *table;
8d8354d2
PE
587 struct ctl_table_header *hdr;
588
0a64b4b8 589 table = ip4_frags_ns_ctl_table;
09ad9bc7 590 if (!net_eq(net, &init_net)) {
0a64b4b8 591 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
51456b29 592 if (!table)
e4a2d5c2
PE
593 goto err_alloc;
594
e4a2d5c2 595 }
4907abc6
ED
596 table[0].data = &net->ipv4.fqdir->high_thresh;
597 table[0].extra1 = &net->ipv4.fqdir->low_thresh;
598 table[1].data = &net->ipv4.fqdir->low_thresh;
599 table[1].extra2 = &net->ipv4.fqdir->high_thresh;
600 table[2].data = &net->ipv4.fqdir->timeout;
601 table[3].data = &net->ipv4.fqdir->max_dist;
e4a2d5c2 602
ec8f23ce 603 hdr = register_net_sysctl(net, "net/ipv4", table);
51456b29 604 if (!hdr)
e4a2d5c2
PE
605 goto err_reg;
606
607 net->ipv4.frags_hdr = hdr;
608 return 0;
609
610err_reg:
09ad9bc7 611 if (!net_eq(net, &init_net))
e4a2d5c2
PE
612 kfree(table);
613err_alloc:
614 return -ENOMEM;
615}
616
2c8c1e72 617static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
e4a2d5c2
PE
618{
619 struct ctl_table *table;
620
621 table = net->ipv4.frags_hdr->ctl_table_arg;
622 unregister_net_sysctl_table(net->ipv4.frags_hdr);
623 kfree(table);
8d8354d2 624}
7d291ebb 625
57a02c39 626static void __init ip4_frags_ctl_register(void)
7d291ebb 627{
43444757 628 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
7d291ebb 629}
8d8354d2 630#else
aa1f731e 631static int ip4_frags_ns_ctl_register(struct net *net)
8d8354d2
PE
632{
633 return 0;
634}
e4a2d5c2 635
aa1f731e 636static void ip4_frags_ns_ctl_unregister(struct net *net)
e4a2d5c2
PE
637{
638}
7d291ebb 639
aa1f731e 640static void __init ip4_frags_ctl_register(void)
7d291ebb
PE
641{
642}
8d8354d2
PE
643#endif
644
2c8c1e72 645static int __net_init ipv4_frags_init_net(struct net *net)
8d8354d2 646{
787bea77
ED
647 int res;
648
4907abc6
ED
649 res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net);
650 if (res < 0)
651 return res;
c2a93660
JDB
652 /* Fragment cache limits.
653 *
654 * The fragment memory accounting code, (tries to) account for
655 * the real memory usage, by measuring both the size of frag
656 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
657 * and the SKB's truesize.
658 *
659 * A 64K fragment consumes 129736 bytes (44*2944)+200
660 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
661 *
662 * We will commit 4MB at one time. Should we cross that limit
663 * we will prune down to 3MB, making room for approx 8 big 64K
664 * fragments 8x128k.
e31e0bdc 665 */
4907abc6
ED
666 net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024;
667 net->ipv4.fqdir->low_thresh = 3 * 1024 * 1024;
b2fd5321
PE
668 /*
669 * Important NOTE! Fragment queue must be destroyed before MSL expires.
670 * RFC791 is wrong proposing to prolongate timer each fragment arrival
671 * by TTL.
672 */
4907abc6 673 net->ipv4.fqdir->timeout = IP_FRAG_TIME;
b2fd5321 674
4907abc6 675 net->ipv4.fqdir->max_dist = 64;
0fbf4cb2 676
787bea77
ED
677 res = ip4_frags_ns_ctl_register(net);
678 if (res < 0)
4907abc6 679 fqdir_exit(net->ipv4.fqdir);
787bea77 680 return res;
8d8354d2
PE
681}
682
d5dd8879
ED
683static void __net_exit ipv4_frags_pre_exit_net(struct net *net)
684{
685 fqdir_pre_exit(net->ipv4.fqdir);
686}
687
2c8c1e72 688static void __net_exit ipv4_frags_exit_net(struct net *net)
81566e83 689{
0a64b4b8 690 ip4_frags_ns_ctl_unregister(net);
4907abc6 691 fqdir_exit(net->ipv4.fqdir);
81566e83
PE
692}
693
694static struct pernet_operations ip4_frags_ops = {
d5dd8879
ED
695 .init = ipv4_frags_init_net,
696 .pre_exit = ipv4_frags_pre_exit_net,
697 .exit = ipv4_frags_exit_net,
81566e83
PE
698};
699
648700f7
ED
700
701static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
702{
703 return jhash2(data,
704 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
705}
706
707static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
708{
709 const struct inet_frag_queue *fq = data;
710
711 return jhash2((const u32 *)&fq->key.v4,
712 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
713}
714
715static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
716{
717 const struct frag_v4_compare_key *key = arg->key;
718 const struct inet_frag_queue *fq = ptr;
719
720 return !!memcmp(&fq->key, key, sizeof(*key));
721}
722
723static const struct rhashtable_params ip4_rhash_params = {
724 .head_offset = offsetof(struct inet_frag_queue, node),
725 .key_offset = offsetof(struct inet_frag_queue, key),
726 .key_len = sizeof(struct frag_v4_compare_key),
727 .hashfn = ip4_key_hashfn,
728 .obj_hashfn = ip4_obj_hashfn,
729 .obj_cmpfn = ip4_obj_cmpfn,
730 .automatic_shrinking = true,
731};
732
b7aa0bf7 733void __init ipfrag_init(void)
1da177e4 734{
c6fda282 735 ip4_frags.constructor = ip4_frag_init;
1e4b8287 736 ip4_frags.destructor = ip4_frag_free;
1e4b8287 737 ip4_frags.qsize = sizeof(struct ipq);
e521db9d 738 ip4_frags.frag_expire = ip_expire;
d4ad4d22 739 ip4_frags.frags_cache_name = ip_frag_cache_name;
648700f7 740 ip4_frags.rhash_params = ip4_rhash_params;
d4ad4d22
NA
741 if (inet_frags_init(&ip4_frags))
742 panic("IP: failed to allocate ip4_frags cache\n");
483a6e4f
ED
743 ip4_frags_ctl_register();
744 register_pernet_subsys(&ip4_frags_ops);
1da177e4 745}