inet: frags: break the 2GB limit for frags storage
[linux-2.6-microblaze.git] / net / ipv4 / ip_fragment.c
1 // SPDX-License-Identifier: GPL-2.0
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.
8  *
9  * Authors:     Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
10  *              Alan Cox <alan@lxorguk.ukuu.org.uk>
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
24 #define pr_fmt(fmt) "IPv4: " fmt
25
26 #include <linux/compiler.h>
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>
38 #include <linux/slab.h>
39 #include <net/route.h>
40 #include <net/dst.h>
41 #include <net/sock.h>
42 #include <net/ip.h>
43 #include <net/icmp.h>
44 #include <net/checksum.h>
45 #include <net/inetpeer.h>
46 #include <net/inet_frag.h>
47 #include <linux/tcp.h>
48 #include <linux/udp.h>
49 #include <linux/inet.h>
50 #include <linux/netfilter_ipv4.h>
51 #include <net/inet_ecn.h>
52 #include <net/l3mdev.h>
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  */
58 static const char ip_frag_cache_name[] = "ip4-frags";
59
60 struct ipfrag_skb_cb
61 {
62         struct inet_skb_parm    h;
63         int                     offset;
64 };
65
66 #define FRAG_CB(skb)    ((struct ipfrag_skb_cb *)((skb)->cb))
67
68 /* Describe an entry in the "incomplete datagrams" queue. */
69 struct ipq {
70         struct inet_frag_queue q;
71
72         u8              ecn; /* RFC3168 support */
73         u16             max_df_size; /* largest frag with DF set seen */
74         int             iif;
75         unsigned int    rid;
76         struct inet_peer *peer;
77 };
78
79 static u8 ip4_frag_ecn(u8 tos)
80 {
81         return 1 << (tos & INET_ECN_MASK);
82 }
83
84 static struct inet_frags ip4_frags;
85
86 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
87                          struct net_device *dev);
88
89
90 static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
91 {
92         struct ipq *qp = container_of(q, struct ipq, q);
93         struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
94                                                frags);
95         struct net *net = container_of(ipv4, struct net, ipv4);
96
97         const struct frag_v4_compare_key *key = a;
98
99         q->key.v4 = *key;
100         qp->ecn = 0;
101         qp->peer = q->net->max_dist ?
102                 inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
103                 NULL;
104 }
105
106 static void ip4_frag_free(struct inet_frag_queue *q)
107 {
108         struct ipq *qp;
109
110         qp = container_of(q, struct ipq, q);
111         if (qp->peer)
112                 inet_putpeer(qp->peer);
113 }
114
115
116 /* Destruction primitives. */
117
118 static void ipq_put(struct ipq *ipq)
119 {
120         inet_frag_put(&ipq->q);
121 }
122
123 /* Kill ipq entry. It is not destroyed immediately,
124  * because caller (and someone more) holds reference count.
125  */
126 static void ipq_kill(struct ipq *ipq)
127 {
128         inet_frag_kill(&ipq->q);
129 }
130
131 static bool frag_expire_skip_icmp(u32 user)
132 {
133         return user == IP_DEFRAG_AF_PACKET ||
134                ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
135                                          __IP_DEFRAG_CONNTRACK_IN_END) ||
136                ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
137                                          __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
138 }
139
140 /*
141  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
142  */
143 static void ip_expire(struct timer_list *t)
144 {
145         struct inet_frag_queue *frag = from_timer(frag, t, timer);
146         struct sk_buff *clone, *head;
147         const struct iphdr *iph;
148         struct net *net;
149         struct ipq *qp;
150         int err;
151
152         qp = container_of(frag, struct ipq, q);
153         net = container_of(qp->q.net, struct net, ipv4.frags);
154
155         rcu_read_lock();
156         spin_lock(&qp->q.lock);
157
158         if (qp->q.flags & INET_FRAG_COMPLETE)
159                 goto out;
160
161         ipq_kill(qp);
162         __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
163
164         head = qp->q.fragments;
165
166         __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
167
168         if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
169                 goto out;
170
171         head->dev = dev_get_by_index_rcu(net, qp->iif);
172         if (!head->dev)
173                 goto out;
174
175
176         /* skb has no dst, perform route lookup again */
177         iph = ip_hdr(head);
178         err = ip_route_input_noref(head, iph->daddr, iph->saddr,
179                                            iph->tos, head->dev);
180         if (err)
181                 goto out;
182
183         /* Only an end host needs to send an ICMP
184          * "Fragment Reassembly Timeout" message, per RFC792.
185          */
186         if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
187             (skb_rtable(head)->rt_type != RTN_LOCAL))
188                 goto out;
189
190         clone = skb_clone(head, GFP_ATOMIC);
191
192         /* Send an ICMP "Fragment Reassembly Timeout" message. */
193         if (clone) {
194                 spin_unlock(&qp->q.lock);
195                 icmp_send(clone, ICMP_TIME_EXCEEDED,
196                           ICMP_EXC_FRAGTIME, 0);
197                 consume_skb(clone);
198                 goto out_rcu_unlock;
199         }
200 out:
201         spin_unlock(&qp->q.lock);
202 out_rcu_unlock:
203         rcu_read_unlock();
204         ipq_put(qp);
205 }
206
207 /* Find the correct entry in the "incomplete datagrams" queue for
208  * this IP datagram, and create new one, if nothing is found.
209  */
210 static struct ipq *ip_find(struct net *net, struct iphdr *iph,
211                            u32 user, int vif)
212 {
213         struct frag_v4_compare_key key = {
214                 .saddr = iph->saddr,
215                 .daddr = iph->daddr,
216                 .user = user,
217                 .vif = vif,
218                 .id = iph->id,
219                 .protocol = iph->protocol,
220         };
221         struct inet_frag_queue *q;
222
223         q = inet_frag_find(&net->ipv4.frags, &key);
224         if (!q)
225                 return NULL;
226
227         return container_of(q, struct ipq, q);
228 }
229
230 /* Is the fragment too far ahead to be part of ipq? */
231 static int ip_frag_too_far(struct ipq *qp)
232 {
233         struct inet_peer *peer = qp->peer;
234         unsigned int max = qp->q.net->max_dist;
235         unsigned int start, end;
236
237         int rc;
238
239         if (!peer || !max)
240                 return 0;
241
242         start = qp->rid;
243         end = atomic_inc_return(&peer->rid);
244         qp->rid = end;
245
246         rc = qp->q.fragments && (end - start) > max;
247
248         if (rc) {
249                 struct net *net;
250
251                 net = container_of(qp->q.net, struct net, ipv4.frags);
252                 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
253         }
254
255         return rc;
256 }
257
258 static int ip_frag_reinit(struct ipq *qp)
259 {
260         struct sk_buff *fp;
261         unsigned int sum_truesize = 0;
262
263         if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
264                 refcount_inc(&qp->q.refcnt);
265                 return -ETIMEDOUT;
266         }
267
268         fp = qp->q.fragments;
269         do {
270                 struct sk_buff *xp = fp->next;
271
272                 sum_truesize += fp->truesize;
273                 kfree_skb(fp);
274                 fp = xp;
275         } while (fp);
276         sub_frag_mem_limit(qp->q.net, sum_truesize);
277
278         qp->q.flags = 0;
279         qp->q.len = 0;
280         qp->q.meat = 0;
281         qp->q.fragments = NULL;
282         qp->q.fragments_tail = NULL;
283         qp->iif = 0;
284         qp->ecn = 0;
285
286         return 0;
287 }
288
289 /* Add new segment to existing queue. */
290 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
291 {
292         struct sk_buff *prev, *next;
293         struct net_device *dev;
294         unsigned int fragsize;
295         int flags, offset;
296         int ihl, end;
297         int err = -ENOENT;
298         u8 ecn;
299
300         if (qp->q.flags & INET_FRAG_COMPLETE)
301                 goto err;
302
303         if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
304             unlikely(ip_frag_too_far(qp)) &&
305             unlikely(err = ip_frag_reinit(qp))) {
306                 ipq_kill(qp);
307                 goto err;
308         }
309
310         ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
311         offset = ntohs(ip_hdr(skb)->frag_off);
312         flags = offset & ~IP_OFFSET;
313         offset &= IP_OFFSET;
314         offset <<= 3;           /* offset is in 8-byte chunks */
315         ihl = ip_hdrlen(skb);
316
317         /* Determine the position of this fragment. */
318         end = offset + skb->len - skb_network_offset(skb) - ihl;
319         err = -EINVAL;
320
321         /* Is this the final fragment? */
322         if ((flags & IP_MF) == 0) {
323                 /* If we already have some bits beyond end
324                  * or have different end, the segment is corrupted.
325                  */
326                 if (end < qp->q.len ||
327                     ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
328                         goto err;
329                 qp->q.flags |= INET_FRAG_LAST_IN;
330                 qp->q.len = end;
331         } else {
332                 if (end&7) {
333                         end &= ~7;
334                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
335                                 skb->ip_summed = CHECKSUM_NONE;
336                 }
337                 if (end > qp->q.len) {
338                         /* Some bits beyond end -> corruption. */
339                         if (qp->q.flags & INET_FRAG_LAST_IN)
340                                 goto err;
341                         qp->q.len = end;
342                 }
343         }
344         if (end == offset)
345                 goto err;
346
347         err = -ENOMEM;
348         if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
349                 goto err;
350
351         err = pskb_trim_rcsum(skb, end - offset);
352         if (err)
353                 goto err;
354
355         /* Find out which fragments are in front and at the back of us
356          * in the chain of fragments so far.  We must know where to put
357          * this fragment, right?
358          */
359         prev = qp->q.fragments_tail;
360         if (!prev || FRAG_CB(prev)->offset < offset) {
361                 next = NULL;
362                 goto found;
363         }
364         prev = NULL;
365         for (next = qp->q.fragments; next != NULL; next = next->next) {
366                 if (FRAG_CB(next)->offset >= offset)
367                         break;  /* bingo! */
368                 prev = next;
369         }
370
371 found:
372         /* We found where to put this one.  Check for overlap with
373          * preceding fragment, and, if needed, align things so that
374          * any overlaps are eliminated.
375          */
376         if (prev) {
377                 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
378
379                 if (i > 0) {
380                         offset += i;
381                         err = -EINVAL;
382                         if (end <= offset)
383                                 goto err;
384                         err = -ENOMEM;
385                         if (!pskb_pull(skb, i))
386                                 goto err;
387                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
388                                 skb->ip_summed = CHECKSUM_NONE;
389                 }
390         }
391
392         err = -ENOMEM;
393
394         while (next && FRAG_CB(next)->offset < end) {
395                 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
396
397                 if (i < next->len) {
398                         /* Eat head of the next overlapped fragment
399                          * and leave the loop. The next ones cannot overlap.
400                          */
401                         if (!pskb_pull(next, i))
402                                 goto err;
403                         FRAG_CB(next)->offset += i;
404                         qp->q.meat -= i;
405                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
406                                 next->ip_summed = CHECKSUM_NONE;
407                         break;
408                 } else {
409                         struct sk_buff *free_it = next;
410
411                         /* Old fragment is completely overridden with
412                          * new one drop it.
413                          */
414                         next = next->next;
415
416                         if (prev)
417                                 prev->next = next;
418                         else
419                                 qp->q.fragments = next;
420
421                         qp->q.meat -= free_it->len;
422                         sub_frag_mem_limit(qp->q.net, free_it->truesize);
423                         kfree_skb(free_it);
424                 }
425         }
426
427         FRAG_CB(skb)->offset = offset;
428
429         /* Insert this fragment in the chain of fragments. */
430         skb->next = next;
431         if (!next)
432                 qp->q.fragments_tail = skb;
433         if (prev)
434                 prev->next = skb;
435         else
436                 qp->q.fragments = skb;
437
438         dev = skb->dev;
439         if (dev) {
440                 qp->iif = dev->ifindex;
441                 skb->dev = NULL;
442         }
443         qp->q.stamp = skb->tstamp;
444         qp->q.meat += skb->len;
445         qp->ecn |= ecn;
446         add_frag_mem_limit(qp->q.net, skb->truesize);
447         if (offset == 0)
448                 qp->q.flags |= INET_FRAG_FIRST_IN;
449
450         fragsize = skb->len + ihl;
451
452         if (fragsize > qp->q.max_size)
453                 qp->q.max_size = fragsize;
454
455         if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
456             fragsize > qp->max_df_size)
457                 qp->max_df_size = fragsize;
458
459         if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
460             qp->q.meat == qp->q.len) {
461                 unsigned long orefdst = skb->_skb_refdst;
462
463                 skb->_skb_refdst = 0UL;
464                 err = ip_frag_reasm(qp, prev, dev);
465                 skb->_skb_refdst = orefdst;
466                 return err;
467         }
468
469         skb_dst_drop(skb);
470         return -EINPROGRESS;
471
472 err:
473         kfree_skb(skb);
474         return err;
475 }
476
477
478 /* Build a new IP datagram from all its fragments. */
479
480 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
481                          struct net_device *dev)
482 {
483         struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
484         struct iphdr *iph;
485         struct sk_buff *fp, *head = qp->q.fragments;
486         int len;
487         int ihlen;
488         int err;
489         u8 ecn;
490
491         ipq_kill(qp);
492
493         ecn = ip_frag_ecn_table[qp->ecn];
494         if (unlikely(ecn == 0xff)) {
495                 err = -EINVAL;
496                 goto out_fail;
497         }
498         /* Make the one we just received the head. */
499         if (prev) {
500                 head = prev->next;
501                 fp = skb_clone(head, GFP_ATOMIC);
502                 if (!fp)
503                         goto out_nomem;
504
505                 fp->next = head->next;
506                 if (!fp->next)
507                         qp->q.fragments_tail = fp;
508                 prev->next = fp;
509
510                 skb_morph(head, qp->q.fragments);
511                 head->next = qp->q.fragments->next;
512
513                 consume_skb(qp->q.fragments);
514                 qp->q.fragments = head;
515         }
516
517         WARN_ON(!head);
518         WARN_ON(FRAG_CB(head)->offset != 0);
519
520         /* Allocate a new buffer for the datagram. */
521         ihlen = ip_hdrlen(head);
522         len = ihlen + qp->q.len;
523
524         err = -E2BIG;
525         if (len > 65535)
526                 goto out_oversize;
527
528         /* Head of list must not be cloned. */
529         if (skb_unclone(head, GFP_ATOMIC))
530                 goto out_nomem;
531
532         /* If the first fragment is fragmented itself, we split
533          * it to two chunks: the first with data and paged part
534          * and the second, holding only fragments. */
535         if (skb_has_frag_list(head)) {
536                 struct sk_buff *clone;
537                 int i, plen = 0;
538
539                 clone = alloc_skb(0, GFP_ATOMIC);
540                 if (!clone)
541                         goto out_nomem;
542                 clone->next = head->next;
543                 head->next = clone;
544                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
545                 skb_frag_list_init(head);
546                 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
547                         plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
548                 clone->len = clone->data_len = head->data_len - plen;
549                 head->data_len -= clone->len;
550                 head->len -= clone->len;
551                 clone->csum = 0;
552                 clone->ip_summed = head->ip_summed;
553                 add_frag_mem_limit(qp->q.net, clone->truesize);
554         }
555
556         skb_shinfo(head)->frag_list = head->next;
557         skb_push(head, head->data - skb_network_header(head));
558
559         for (fp=head->next; fp; fp = fp->next) {
560                 head->data_len += fp->len;
561                 head->len += fp->len;
562                 if (head->ip_summed != fp->ip_summed)
563                         head->ip_summed = CHECKSUM_NONE;
564                 else if (head->ip_summed == CHECKSUM_COMPLETE)
565                         head->csum = csum_add(head->csum, fp->csum);
566                 head->truesize += fp->truesize;
567         }
568         sub_frag_mem_limit(qp->q.net, head->truesize);
569
570         head->next = NULL;
571         head->dev = dev;
572         head->tstamp = qp->q.stamp;
573         IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
574
575         iph = ip_hdr(head);
576         iph->tot_len = htons(len);
577         iph->tos |= ecn;
578
579         /* When we set IP_DF on a refragmented skb we must also force a
580          * call to ip_fragment to avoid forwarding a DF-skb of size s while
581          * original sender only sent fragments of size f (where f < s).
582          *
583          * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
584          * frag seen to avoid sending tiny DF-fragments in case skb was built
585          * from one very small df-fragment and one large non-df frag.
586          */
587         if (qp->max_df_size == qp->q.max_size) {
588                 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
589                 iph->frag_off = htons(IP_DF);
590         } else {
591                 iph->frag_off = 0;
592         }
593
594         ip_send_check(iph);
595
596         __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
597         qp->q.fragments = NULL;
598         qp->q.fragments_tail = NULL;
599         return 0;
600
601 out_nomem:
602         net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
603         err = -ENOMEM;
604         goto out_fail;
605 out_oversize:
606         net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
607 out_fail:
608         __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
609         return err;
610 }
611
612 /* Process an incoming IP datagram fragment. */
613 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
614 {
615         struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
616         int vif = l3mdev_master_ifindex_rcu(dev);
617         struct ipq *qp;
618
619         __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
620         skb_orphan(skb);
621
622         /* Lookup (or create) queue header */
623         qp = ip_find(net, ip_hdr(skb), user, vif);
624         if (qp) {
625                 int ret;
626
627                 spin_lock(&qp->q.lock);
628
629                 ret = ip_frag_queue(qp, skb);
630
631                 spin_unlock(&qp->q.lock);
632                 ipq_put(qp);
633                 return ret;
634         }
635
636         __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
637         kfree_skb(skb);
638         return -ENOMEM;
639 }
640 EXPORT_SYMBOL(ip_defrag);
641
642 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
643 {
644         struct iphdr iph;
645         int netoff;
646         u32 len;
647
648         if (skb->protocol != htons(ETH_P_IP))
649                 return skb;
650
651         netoff = skb_network_offset(skb);
652
653         if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
654                 return skb;
655
656         if (iph.ihl < 5 || iph.version != 4)
657                 return skb;
658
659         len = ntohs(iph.tot_len);
660         if (skb->len < netoff + len || len < (iph.ihl * 4))
661                 return skb;
662
663         if (ip_is_fragment(&iph)) {
664                 skb = skb_share_check(skb, GFP_ATOMIC);
665                 if (skb) {
666                         if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
667                                 return skb;
668                         if (pskb_trim_rcsum(skb, netoff + len))
669                                 return skb;
670                         memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
671                         if (ip_defrag(net, skb, user))
672                                 return NULL;
673                         skb_clear_hash(skb);
674                 }
675         }
676         return skb;
677 }
678 EXPORT_SYMBOL(ip_check_defrag);
679
680 #ifdef CONFIG_SYSCTL
681 static long zero;
682
683 static struct ctl_table ip4_frags_ns_ctl_table[] = {
684         {
685                 .procname       = "ipfrag_high_thresh",
686                 .data           = &init_net.ipv4.frags.high_thresh,
687                 .maxlen         = sizeof(unsigned long),
688                 .mode           = 0644,
689                 .proc_handler   = proc_doulongvec_minmax,
690                 .extra1         = &init_net.ipv4.frags.low_thresh
691         },
692         {
693                 .procname       = "ipfrag_low_thresh",
694                 .data           = &init_net.ipv4.frags.low_thresh,
695                 .maxlen         = sizeof(unsigned long),
696                 .mode           = 0644,
697                 .proc_handler   = proc_doulongvec_minmax,
698                 .extra1         = &zero,
699                 .extra2         = &init_net.ipv4.frags.high_thresh
700         },
701         {
702                 .procname       = "ipfrag_time",
703                 .data           = &init_net.ipv4.frags.timeout,
704                 .maxlen         = sizeof(int),
705                 .mode           = 0644,
706                 .proc_handler   = proc_dointvec_jiffies,
707         },
708         {
709                 .procname       = "ipfrag_max_dist",
710                 .data           = &init_net.ipv4.frags.max_dist,
711                 .maxlen         = sizeof(int),
712                 .mode           = 0644,
713                 .proc_handler   = proc_dointvec_minmax,
714                 .extra1         = &zero
715         },
716         { }
717 };
718
719 /* secret interval has been deprecated */
720 static int ip4_frags_secret_interval_unused;
721 static struct ctl_table ip4_frags_ctl_table[] = {
722         {
723                 .procname       = "ipfrag_secret_interval",
724                 .data           = &ip4_frags_secret_interval_unused,
725                 .maxlen         = sizeof(int),
726                 .mode           = 0644,
727                 .proc_handler   = proc_dointvec_jiffies,
728         },
729         { }
730 };
731
732 static int __net_init ip4_frags_ns_ctl_register(struct net *net)
733 {
734         struct ctl_table *table;
735         struct ctl_table_header *hdr;
736
737         table = ip4_frags_ns_ctl_table;
738         if (!net_eq(net, &init_net)) {
739                 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
740                 if (!table)
741                         goto err_alloc;
742
743                 table[0].data = &net->ipv4.frags.high_thresh;
744                 table[0].extra1 = &net->ipv4.frags.low_thresh;
745                 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
746                 table[1].data = &net->ipv4.frags.low_thresh;
747                 table[1].extra2 = &net->ipv4.frags.high_thresh;
748                 table[2].data = &net->ipv4.frags.timeout;
749                 table[3].data = &net->ipv4.frags.max_dist;
750         }
751
752         hdr = register_net_sysctl(net, "net/ipv4", table);
753         if (!hdr)
754                 goto err_reg;
755
756         net->ipv4.frags_hdr = hdr;
757         return 0;
758
759 err_reg:
760         if (!net_eq(net, &init_net))
761                 kfree(table);
762 err_alloc:
763         return -ENOMEM;
764 }
765
766 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
767 {
768         struct ctl_table *table;
769
770         table = net->ipv4.frags_hdr->ctl_table_arg;
771         unregister_net_sysctl_table(net->ipv4.frags_hdr);
772         kfree(table);
773 }
774
775 static void __init ip4_frags_ctl_register(void)
776 {
777         register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
778 }
779 #else
780 static int ip4_frags_ns_ctl_register(struct net *net)
781 {
782         return 0;
783 }
784
785 static void ip4_frags_ns_ctl_unregister(struct net *net)
786 {
787 }
788
789 static void __init ip4_frags_ctl_register(void)
790 {
791 }
792 #endif
793
794 static int __net_init ipv4_frags_init_net(struct net *net)
795 {
796         int res;
797
798         /* Fragment cache limits.
799          *
800          * The fragment memory accounting code, (tries to) account for
801          * the real memory usage, by measuring both the size of frag
802          * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
803          * and the SKB's truesize.
804          *
805          * A 64K fragment consumes 129736 bytes (44*2944)+200
806          * (1500 truesize == 2944, sizeof(struct ipq) == 200)
807          *
808          * We will commit 4MB at one time. Should we cross that limit
809          * we will prune down to 3MB, making room for approx 8 big 64K
810          * fragments 8x128k.
811          */
812         net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
813         net->ipv4.frags.low_thresh  = 3 * 1024 * 1024;
814         /*
815          * Important NOTE! Fragment queue must be destroyed before MSL expires.
816          * RFC791 is wrong proposing to prolongate timer each fragment arrival
817          * by TTL.
818          */
819         net->ipv4.frags.timeout = IP_FRAG_TIME;
820
821         net->ipv4.frags.max_dist = 64;
822         net->ipv4.frags.f = &ip4_frags;
823
824         res = inet_frags_init_net(&net->ipv4.frags);
825         if (res < 0)
826                 return res;
827         res = ip4_frags_ns_ctl_register(net);
828         if (res < 0)
829                 inet_frags_exit_net(&net->ipv4.frags);
830         return res;
831 }
832
833 static void __net_exit ipv4_frags_exit_net(struct net *net)
834 {
835         ip4_frags_ns_ctl_unregister(net);
836         inet_frags_exit_net(&net->ipv4.frags);
837 }
838
839 static struct pernet_operations ip4_frags_ops = {
840         .init = ipv4_frags_init_net,
841         .exit = ipv4_frags_exit_net,
842 };
843
844
845 static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
846 {
847         return jhash2(data,
848                       sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
849 }
850
851 static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
852 {
853         const struct inet_frag_queue *fq = data;
854
855         return jhash2((const u32 *)&fq->key.v4,
856                       sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
857 }
858
859 static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
860 {
861         const struct frag_v4_compare_key *key = arg->key;
862         const struct inet_frag_queue *fq = ptr;
863
864         return !!memcmp(&fq->key, key, sizeof(*key));
865 }
866
867 static const struct rhashtable_params ip4_rhash_params = {
868         .head_offset            = offsetof(struct inet_frag_queue, node),
869         .key_offset             = offsetof(struct inet_frag_queue, key),
870         .key_len                = sizeof(struct frag_v4_compare_key),
871         .hashfn                 = ip4_key_hashfn,
872         .obj_hashfn             = ip4_obj_hashfn,
873         .obj_cmpfn              = ip4_obj_cmpfn,
874         .automatic_shrinking    = true,
875 };
876
877 void __init ipfrag_init(void)
878 {
879         ip4_frags.constructor = ip4_frag_init;
880         ip4_frags.destructor = ip4_frag_free;
881         ip4_frags.qsize = sizeof(struct ipq);
882         ip4_frags.frag_expire = ip_expire;
883         ip4_frags.frags_cache_name = ip_frag_cache_name;
884         ip4_frags.rhash_params = ip4_rhash_params;
885         if (inet_frags_init(&ip4_frags))
886                 panic("IP: failed to allocate ip4_frags cache\n");
887         ip4_frags_ctl_register();
888         register_pernet_subsys(&ip4_frags_ops);
889 }