- patches.suse/slab-handle-memoryless-nodes-v2a.patch: Refresh.
[linux-flexiantxendom0-3.2.10.git] / net / ipv4 / ip_fragment.c
1 /*
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.
7  *
8  * Authors:     Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
9  *              Alan Cox <alan@lxorguk.ukuu.org.uk>
10  *
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
23 #include <linux/compiler.h>
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/mm.h>
27 #include <linux/jiffies.h>
28 #include <linux/skbuff.h>
29 #include <linux/list.h>
30 #include <linux/ip.h>
31 #include <linux/icmp.h>
32 #include <linux/netdevice.h>
33 #include <linux/jhash.h>
34 #include <linux/random.h>
35 #include <net/sock.h>
36 #include <net/ip.h>
37 #include <net/icmp.h>
38 #include <net/checksum.h>
39 #include <net/inetpeer.h>
40 #include <net/inet_frag.h>
41 #include <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/inet.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/reserve.h>
46 #include <linux/nsproxy.h>
47
48 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
49  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
50  * as well. Or notify me, at least. --ANK
51  */
52
53 static int sysctl_ipfrag_max_dist __read_mostly = 64;
54
55 struct ipfrag_skb_cb
56 {
57         struct inet_skb_parm    h;
58         int                     offset;
59 };
60
61 #define FRAG_CB(skb)    ((struct ipfrag_skb_cb *)((skb)->cb))
62
63 /* Describe an entry in the "incomplete datagrams" queue. */
64 struct ipq {
65         struct inet_frag_queue q;
66
67         u32             user;
68         __be32          saddr;
69         __be32          daddr;
70         __be16          id;
71         u8              protocol;
72         int             iif;
73         unsigned int    rid;
74         struct inet_peer *peer;
75 };
76
77 static struct inet_frags ip4_frags;
78
79 int ip_frag_nqueues(struct net *net)
80 {
81         return net->ipv4.frags.nqueues;
82 }
83
84 int ip_frag_mem(struct net *net)
85 {
86         return atomic_read(&net->ipv4.frags.mem);
87 }
88
89 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
90                          struct net_device *dev);
91
92 struct ip4_create_arg {
93         struct iphdr *iph;
94         u32 user;
95 };
96
97 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
98 {
99         return jhash_3words((__force u32)id << 16 | prot,
100                             (__force u32)saddr, (__force u32)daddr,
101                             ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
102 }
103
104 static unsigned int ip4_hashfn(struct inet_frag_queue *q)
105 {
106         struct ipq *ipq;
107
108         ipq = container_of(q, struct ipq, q);
109         return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
110 }
111
112 static int ip4_frag_match(struct inet_frag_queue *q, void *a)
113 {
114         struct ipq *qp;
115         struct ip4_create_arg *arg = a;
116
117         qp = container_of(q, struct ipq, q);
118         return (qp->id == arg->iph->id &&
119                         qp->saddr == arg->iph->saddr &&
120                         qp->daddr == arg->iph->daddr &&
121                         qp->protocol == arg->iph->protocol &&
122                         qp->user == arg->user);
123 }
124
125 /* Memory Tracking Functions. */
126 static __inline__ void frag_kfree_skb(struct netns_frags *nf,
127                 struct sk_buff *skb, int *work)
128 {
129         if (work)
130                 *work -= skb->truesize;
131         atomic_sub(skb->truesize, &nf->mem);
132         kfree_skb(skb);
133 }
134
135 static void ip4_frag_init(struct inet_frag_queue *q, void *a)
136 {
137         struct ipq *qp = container_of(q, struct ipq, q);
138         struct ip4_create_arg *arg = a;
139
140         qp->protocol = arg->iph->protocol;
141         qp->id = arg->iph->id;
142         qp->saddr = arg->iph->saddr;
143         qp->daddr = arg->iph->daddr;
144         qp->user = arg->user;
145         qp->peer = sysctl_ipfrag_max_dist ?
146                 inet_getpeer(arg->iph->saddr, 1) : NULL;
147 }
148
149 static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
150 {
151         struct ipq *qp;
152
153         qp = container_of(q, struct ipq, q);
154         if (qp->peer)
155                 inet_putpeer(qp->peer);
156 }
157
158
159 /* Destruction primitives. */
160
161 static __inline__ void ipq_put(struct ipq *ipq)
162 {
163         inet_frag_put(&ipq->q, &ip4_frags);
164 }
165
166 /* Kill ipq entry. It is not destroyed immediately,
167  * because caller (and someone more) holds reference count.
168  */
169 static void ipq_kill(struct ipq *ipq)
170 {
171         inet_frag_kill(&ipq->q, &ip4_frags);
172 }
173
174 /* Memory limiting on fragments.  Evictor trashes the oldest
175  * fragment queue until we are back under the threshold.
176  */
177 static void ip_evictor(struct net *net)
178 {
179         int evicted;
180
181         evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags);
182         if (evicted)
183                 IP_ADD_STATS_BH(net, IPSTATS_MIB_REASMFAILS, evicted);
184 }
185
186 /*
187  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
188  */
189 static void ip_expire(unsigned long arg)
190 {
191         struct ipq *qp;
192         struct net *net;
193
194         qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
195         net = container_of(qp->q.net, struct net, ipv4.frags);
196
197         spin_lock(&qp->q.lock);
198
199         if (qp->q.last_in & INET_FRAG_COMPLETE)
200                 goto out;
201
202         ipq_kill(qp);
203
204         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
205         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
206
207         if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
208                 struct sk_buff *head = qp->q.fragments;
209
210                 /* Send an ICMP "Fragment Reassembly Timeout" message. */
211                 rcu_read_lock();
212                 head->dev = dev_get_by_index_rcu(net, qp->iif);
213                 if (head->dev)
214                         icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
215                 rcu_read_unlock();
216         }
217 out:
218         spin_unlock(&qp->q.lock);
219         ipq_put(qp);
220 }
221
222 /* Find the correct entry in the "incomplete datagrams" queue for
223  * this IP datagram, and create new one, if nothing is found.
224  */
225 static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
226 {
227         struct inet_frag_queue *q;
228         struct ip4_create_arg arg;
229         unsigned int hash;
230
231         arg.iph = iph;
232         arg.user = user;
233
234         read_lock(&ip4_frags.lock);
235         hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
236
237         q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
238         if (q == NULL)
239                 goto out_nomem;
240
241         return container_of(q, struct ipq, q);
242
243 out_nomem:
244         LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
245         return NULL;
246 }
247
248 /* Is the fragment too far ahead to be part of ipq? */
249 static inline int ip_frag_too_far(struct ipq *qp)
250 {
251         struct inet_peer *peer = qp->peer;
252         unsigned int max = sysctl_ipfrag_max_dist;
253         unsigned int start, end;
254
255         int rc;
256
257         if (!peer || !max)
258                 return 0;
259
260         start = qp->rid;
261         end = atomic_inc_return(&peer->rid);
262         qp->rid = end;
263
264         rc = qp->q.fragments && (end - start) > max;
265
266         if (rc) {
267                 struct net *net;
268
269                 net = container_of(qp->q.net, struct net, ipv4.frags);
270                 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
271         }
272
273         return rc;
274 }
275
276 static int ip_frag_reinit(struct ipq *qp)
277 {
278         struct sk_buff *fp;
279
280         if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
281                 atomic_inc(&qp->q.refcnt);
282                 return -ETIMEDOUT;
283         }
284
285         fp = qp->q.fragments;
286         do {
287                 struct sk_buff *xp = fp->next;
288                 frag_kfree_skb(qp->q.net, fp, NULL);
289                 fp = xp;
290         } while (fp);
291
292         qp->q.last_in = 0;
293         qp->q.len = 0;
294         qp->q.meat = 0;
295         qp->q.fragments = NULL;
296         qp->iif = 0;
297
298         return 0;
299 }
300
301 /* Add new segment to existing queue. */
302 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
303 {
304         struct sk_buff *prev, *next;
305         struct net_device *dev;
306         int flags, offset;
307         int ihl, end;
308         int err = -ENOENT;
309
310         if (qp->q.last_in & INET_FRAG_COMPLETE)
311                 goto err;
312
313         if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
314             unlikely(ip_frag_too_far(qp)) &&
315             unlikely(err = ip_frag_reinit(qp))) {
316                 ipq_kill(qp);
317                 goto err;
318         }
319
320         offset = ntohs(ip_hdr(skb)->frag_off);
321         flags = offset & ~IP_OFFSET;
322         offset &= IP_OFFSET;
323         offset <<= 3;           /* offset is in 8-byte chunks */
324         ihl = ip_hdrlen(skb);
325
326         /* Determine the position of this fragment. */
327         end = offset + skb->len - ihl;
328         err = -EINVAL;
329
330         /* Is this the final fragment? */
331         if ((flags & IP_MF) == 0) {
332                 /* If we already have some bits beyond end
333                  * or have different end, the segment is corrrupted.
334                  */
335                 if (end < qp->q.len ||
336                     ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
337                         goto err;
338                 qp->q.last_in |= INET_FRAG_LAST_IN;
339                 qp->q.len = end;
340         } else {
341                 if (end&7) {
342                         end &= ~7;
343                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
344                                 skb->ip_summed = CHECKSUM_NONE;
345                 }
346                 if (end > qp->q.len) {
347                         /* Some bits beyond end -> corruption. */
348                         if (qp->q.last_in & INET_FRAG_LAST_IN)
349                                 goto err;
350                         qp->q.len = end;
351                 }
352         }
353         if (end == offset)
354                 goto err;
355
356         err = -ENOMEM;
357         if (pskb_pull(skb, ihl) == NULL)
358                 goto err;
359
360         err = pskb_trim_rcsum(skb, end - offset);
361         if (err)
362                 goto err;
363
364         /* Find out which fragments are in front and at the back of us
365          * in the chain of fragments so far.  We must know where to put
366          * this fragment, right?
367          */
368         prev = NULL;
369         for (next = qp->q.fragments; next != NULL; next = next->next) {
370                 if (FRAG_CB(next)->offset >= offset)
371                         break;  /* bingo! */
372                 prev = next;
373         }
374
375         /* We found where to put this one.  Check for overlap with
376          * preceding fragment, and, if needed, align things so that
377          * any overlaps are eliminated.
378          */
379         if (prev) {
380                 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
381
382                 if (i > 0) {
383                         offset += i;
384                         err = -EINVAL;
385                         if (end <= offset)
386                                 goto err;
387                         err = -ENOMEM;
388                         if (!pskb_pull(skb, i))
389                                 goto err;
390                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
391                                 skb->ip_summed = CHECKSUM_NONE;
392                 }
393         }
394
395         err = -ENOMEM;
396
397         while (next && FRAG_CB(next)->offset < end) {
398                 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
399
400                 if (i < next->len) {
401                         /* Eat head of the next overlapped fragment
402                          * and leave the loop. The next ones cannot overlap.
403                          */
404                         if (!pskb_pull(next, i))
405                                 goto err;
406                         FRAG_CB(next)->offset += i;
407                         qp->q.meat -= i;
408                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
409                                 next->ip_summed = CHECKSUM_NONE;
410                         break;
411                 } else {
412                         struct sk_buff *free_it = next;
413
414                         /* Old fragment is completely overridden with
415                          * new one drop it.
416                          */
417                         next = next->next;
418
419                         if (prev)
420                                 prev->next = next;
421                         else
422                                 qp->q.fragments = next;
423
424                         qp->q.meat -= free_it->len;
425                         frag_kfree_skb(qp->q.net, free_it, NULL);
426                 }
427         }
428
429         FRAG_CB(skb)->offset = offset;
430
431         /* Insert this fragment in the chain of fragments. */
432         skb->next = next;
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         atomic_add(skb->truesize, &qp->q.net->mem);
446         if (offset == 0)
447                 qp->q.last_in |= INET_FRAG_FIRST_IN;
448
449         if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
450             qp->q.meat == qp->q.len)
451                 return ip_frag_reasm(qp, prev, dev);
452
453         write_lock(&ip4_frags.lock);
454         list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
455         write_unlock(&ip4_frags.lock);
456         return -EINPROGRESS;
457
458 err:
459         kfree_skb(skb);
460         return err;
461 }
462
463
464 /* Build a new IP datagram from all its fragments. */
465
466 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
467                          struct net_device *dev)
468 {
469         struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
470         struct iphdr *iph;
471         struct sk_buff *fp, *head = qp->q.fragments;
472         int len;
473         int ihlen;
474         int err;
475
476         ipq_kill(qp);
477
478         /* Make the one we just received the head. */
479         if (prev) {
480                 head = prev->next;
481                 fp = skb_clone(head, GFP_ATOMIC);
482                 if (!fp)
483                         goto out_nomem;
484
485                 fp->next = head->next;
486                 prev->next = fp;
487
488                 skb_morph(head, qp->q.fragments);
489                 head->next = qp->q.fragments->next;
490
491                 kfree_skb(qp->q.fragments);
492                 qp->q.fragments = head;
493         }
494
495         WARN_ON(head == NULL);
496         WARN_ON(FRAG_CB(head)->offset != 0);
497
498         /* Allocate a new buffer for the datagram. */
499         ihlen = ip_hdrlen(head);
500         len = ihlen + qp->q.len;
501
502         err = -E2BIG;
503         if (len > 65535)
504                 goto out_oversize;
505
506         /* Head of list must not be cloned. */
507         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
508                 goto out_nomem;
509
510         /* If the first fragment is fragmented itself, we split
511          * it to two chunks: the first with data and paged part
512          * and the second, holding only fragments. */
513         if (skb_has_frags(head)) {
514                 struct sk_buff *clone;
515                 int i, plen = 0;
516
517                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
518                         goto out_nomem;
519                 clone->next = head->next;
520                 head->next = clone;
521                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
522                 skb_frag_list_init(head);
523                 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
524                         plen += skb_shinfo(head)->frags[i].size;
525                 clone->len = clone->data_len = head->data_len - plen;
526                 head->data_len -= clone->len;
527                 head->len -= clone->len;
528                 clone->csum = 0;
529                 clone->ip_summed = head->ip_summed;
530                 atomic_add(clone->truesize, &qp->q.net->mem);
531         }
532
533         skb_shinfo(head)->frag_list = head->next;
534         skb_push(head, head->data - skb_network_header(head));
535         atomic_sub(head->truesize, &qp->q.net->mem);
536
537         for (fp=head->next; fp; fp = fp->next) {
538                 head->data_len += fp->len;
539                 head->len += fp->len;
540                 if (head->ip_summed != fp->ip_summed)
541                         head->ip_summed = CHECKSUM_NONE;
542                 else if (head->ip_summed == CHECKSUM_COMPLETE)
543                         head->csum = csum_add(head->csum, fp->csum);
544                 head->truesize += fp->truesize;
545                 atomic_sub(fp->truesize, &qp->q.net->mem);
546         }
547
548         head->next = NULL;
549         head->dev = dev;
550         head->tstamp = qp->q.stamp;
551
552         iph = ip_hdr(head);
553         iph->frag_off = 0;
554         iph->tot_len = htons(len);
555         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
556         qp->q.fragments = NULL;
557         return 0;
558
559 out_nomem:
560         LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
561                               "queue %p\n", qp);
562         err = -ENOMEM;
563         goto out_fail;
564 out_oversize:
565         if (net_ratelimit())
566                 printk(KERN_INFO "Oversized IP packet from %pI4.\n",
567                         &qp->saddr);
568 out_fail:
569         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
570         return err;
571 }
572
573 /* Process an incoming IP datagram fragment. */
574 int ip_defrag(struct sk_buff *skb, u32 user)
575 {
576         struct ipq *qp;
577         struct net *net;
578
579         net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
580         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
581
582         /* Start by cleaning up the memory. */
583         if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
584                 ip_evictor(net);
585
586         /* Lookup (or create) queue header */
587         if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
588                 int ret;
589
590                 spin_lock(&qp->q.lock);
591
592                 ret = ip_frag_queue(qp, skb);
593
594                 spin_unlock(&qp->q.lock);
595                 ipq_put(qp);
596                 return ret;
597         }
598
599         IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
600         kfree_skb(skb);
601         return -ENOMEM;
602 }
603
604 #ifdef CONFIG_SYSCTL
605 static int
606 proc_dointvec_fragment(struct ctl_table *table, int write,
607                 void __user *buffer, size_t *lenp, loff_t *ppos)
608 {
609         struct net *net = container_of(table->data, struct net,
610                                        ipv4.frags.high_thresh);
611         ctl_table tmp = *table;
612         int new_bytes, ret;
613
614         mutex_lock(&net->ipv4.frags.lock);
615         if (write) {
616                 tmp.data = &new_bytes;
617                 table = &tmp;
618         }
619
620         ret = proc_dointvec(table, write, buffer, lenp, ppos);
621
622         if (!ret && write) {
623                 ret = mem_reserve_kmalloc_set(&net->ipv4.frags.reserve,
624                                 new_bytes);
625                 if (!ret)
626                         net->ipv4.frags.high_thresh = new_bytes;
627         }
628         mutex_unlock(&net->ipv4.frags.lock);
629
630         return ret;
631 }
632
633 static int zero;
634
635 static struct ctl_table ip4_frags_ns_ctl_table[] = {
636         {
637                 .procname       = "ipfrag_high_thresh",
638                 .data           = &init_net.ipv4.frags.high_thresh,
639                 .maxlen         = sizeof(int),
640                 .mode           = 0644,
641                 .proc_handler   = proc_dointvec_fragment,
642         },
643         {
644                 .procname       = "ipfrag_low_thresh",
645                 .data           = &init_net.ipv4.frags.low_thresh,
646                 .maxlen         = sizeof(int),
647                 .mode           = 0644,
648                 .proc_handler   = proc_dointvec
649         },
650         {
651                 .procname       = "ipfrag_time",
652                 .data           = &init_net.ipv4.frags.timeout,
653                 .maxlen         = sizeof(int),
654                 .mode           = 0644,
655                 .proc_handler   = proc_dointvec_jiffies,
656         },
657         { }
658 };
659
660 static struct ctl_table ip4_frags_ctl_table[] = {
661         {
662                 .procname       = "ipfrag_secret_interval",
663                 .data           = &ip4_frags.secret_interval,
664                 .maxlen         = sizeof(int),
665                 .mode           = 0644,
666                 .proc_handler   = proc_dointvec_jiffies,
667         },
668         {
669                 .procname       = "ipfrag_max_dist",
670                 .data           = &sysctl_ipfrag_max_dist,
671                 .maxlen         = sizeof(int),
672                 .mode           = 0644,
673                 .proc_handler   = proc_dointvec_minmax,
674                 .extra1         = &zero
675         },
676         { }
677 };
678
679 static int ip4_frags_ns_ctl_register(struct net *net)
680 {
681         struct ctl_table *table;
682         struct ctl_table_header *hdr;
683
684         table = ip4_frags_ns_ctl_table;
685         if (!net_eq(net, &init_net)) {
686                 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
687                 if (table == NULL)
688                         goto err_alloc;
689
690                 table[0].data = &net->ipv4.frags.high_thresh;
691                 table[1].data = &net->ipv4.frags.low_thresh;
692                 table[2].data = &net->ipv4.frags.timeout;
693         }
694
695         hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
696         if (hdr == NULL)
697                 goto err_reg;
698
699         net->ipv4.frags_hdr = hdr;
700         return 0;
701
702 err_reg:
703         if (!net_eq(net, &init_net))
704                 kfree(table);
705 err_alloc:
706         return -ENOMEM;
707 }
708
709 static void ip4_frags_ns_ctl_unregister(struct net *net)
710 {
711         struct ctl_table *table;
712
713         table = net->ipv4.frags_hdr->ctl_table_arg;
714         unregister_net_sysctl_table(net->ipv4.frags_hdr);
715         kfree(table);
716 }
717
718 static void ip4_frags_ctl_register(void)
719 {
720         register_net_sysctl_rotable(net_ipv4_ctl_path, ip4_frags_ctl_table);
721 }
722 #else
723 static inline int ip4_frags_ns_ctl_register(struct net *net)
724 {
725         return 0;
726 }
727
728 static inline void ip4_frags_ns_ctl_unregister(struct net *net)
729 {
730 }
731
732 static inline void ip4_frags_ctl_register(void)
733 {
734 }
735 #endif
736
737 static int ipv4_frags_init_net(struct net *net)
738 {
739         int ret;
740
741         /*
742          * Fragment cache limits. We will commit 256K at one time. Should we
743          * cross that limit we will prune down to 192K. This should cope with
744          * even the most extreme cases without allowing an attacker to
745          * measurably harm machine performance.
746          */
747         net->ipv4.frags.high_thresh = 256 * 1024;
748         net->ipv4.frags.low_thresh = 192 * 1024;
749         /*
750          * Important NOTE! Fragment queue must be destroyed before MSL expires.
751          * RFC791 is wrong proposing to prolongate timer each fragment arrival
752          * by TTL.
753          */
754         net->ipv4.frags.timeout = IP_FRAG_TIME;
755
756         inet_frags_init_net(&net->ipv4.frags);
757
758         ret = ip4_frags_ns_ctl_register(net);
759         if (ret)
760                 goto out_reg;
761
762         mem_reserve_init(&net->ipv4.frags.reserve, "IPv4 fragment cache",
763                         &net_skb_reserve);
764         ret = mem_reserve_kmalloc_set(&net->ipv4.frags.reserve,
765                         net->ipv4.frags.high_thresh);
766         if (ret)
767                 goto out_reserve;
768
769         return 0;
770
771 out_reserve:
772         mem_reserve_disconnect(&net->ipv4.frags.reserve);
773         ip4_frags_ns_ctl_unregister(net);
774 out_reg:
775         inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
776
777         return ret;
778 }
779
780 static void ipv4_frags_exit_net(struct net *net)
781 {
782         mem_reserve_disconnect(&net->ipv4.frags.reserve);
783         ip4_frags_ns_ctl_unregister(net);
784         inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
785 }
786
787 static struct pernet_operations ip4_frags_ops = {
788         .init = ipv4_frags_init_net,
789         .exit = ipv4_frags_exit_net,
790 };
791
792 void __init ipfrag_init(void)
793 {
794         ip4_frags_ctl_register();
795         register_pernet_subsys(&ip4_frags_ops);
796         ip4_frags.hashfn = ip4_hashfn;
797         ip4_frags.constructor = ip4_frag_init;
798         ip4_frags.destructor = ip4_frag_free;
799         ip4_frags.skb_free = NULL;
800         ip4_frags.qsize = sizeof(struct ipq);
801         ip4_frags.match = ip4_frag_match;
802         ip4_frags.frag_expire = ip_expire;
803         ip4_frags.secret_interval = 10 * 60 * HZ;
804         inet_frags_init(&ip4_frags);
805 }
806
807 EXPORT_SYMBOL(ip_defrag);