[NETNS][FRAGS]: Make the LRU list per namespace.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / ip_fragment.c
CommitLineData
1da177e4
LT
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.
e905a9ed 7 *
1da177e4
LT
8 * Version: $Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
9 *
10 * Authors: Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
11 * Alan Cox <Alan.Cox@linux.org>
12 *
13 * Fixes:
14 * Alan Cox : Split from ip.c , see ip_input.c for history.
15 * David S. Miller : Begin massive cleanup...
16 * Andi Kleen : Add sysctls.
17 * xxxx : Overlapfrag bug.
18 * Ultima : ip_expire() kernel panic.
19 * Bill Hawes : Frag accounting and evictor fixes.
20 * John McDonald : 0 length frag bug.
21 * Alexey Kuznetsov: SMP races, threading, cleanup.
22 * Patrick McHardy : LRU queue of frag heads for evictor.
23 */
24
89cee8b1 25#include <linux/compiler.h>
1da177e4
LT
26#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>
37#include <net/sock.h>
38#include <net/ip.h>
39#include <net/icmp.h>
40#include <net/checksum.h>
89cee8b1 41#include <net/inetpeer.h>
5ab11c98 42#include <net/inet_frag.h>
1da177e4
LT
43#include <linux/tcp.h>
44#include <linux/udp.h>
45#include <linux/inet.h>
46#include <linux/netfilter_ipv4.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
8d8354d2 53static int sysctl_ipfrag_max_dist __read_mostly = 64;
89cee8b1 54
1da177e4
LT
55struct 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. */
64struct ipq {
5ab11c98
PE
65 struct inet_frag_queue q;
66
1da177e4 67 u32 user;
18277770
AV
68 __be32 saddr;
69 __be32 daddr;
70 __be16 id;
1da177e4 71 u8 protocol;
89cee8b1
HX
72 int iif;
73 unsigned int rid;
74 struct inet_peer *peer;
1da177e4
LT
75};
76
7eb95156 77static struct inet_frags ip4_frags;
1da177e4 78
e5a2bb84 79int ip_frag_nqueues(struct net *net)
7eb95156 80{
e5a2bb84 81 return net->ipv4.frags.nqueues;
7eb95156 82}
1da177e4 83
6ddc0822 84int ip_frag_mem(struct net *net)
7eb95156 85{
6ddc0822 86 return atomic_read(&net->ipv4.frags.mem);
7eb95156 87}
1da177e4 88
1706d587
HX
89static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
90 struct net_device *dev);
91
c6fda282
PE
92struct ip4_create_arg {
93 struct iphdr *iph;
94 u32 user;
95};
96
18277770 97static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
1da177e4 98{
18277770
AV
99 return jhash_3words((__force u32)id << 16 | prot,
100 (__force u32)saddr, (__force u32)daddr,
7eb95156 101 ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
1da177e4
LT
102}
103
321a3a99 104static unsigned int ip4_hashfn(struct inet_frag_queue *q)
1da177e4 105{
321a3a99 106 struct ipq *ipq;
1da177e4 107
321a3a99
PE
108 ipq = container_of(q, struct ipq, q);
109 return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
1da177e4
LT
110}
111
abd6523d
PE
112static 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
1da177e4 125/* Memory Tracking Functions. */
6ddc0822
PE
126static __inline__ void frag_kfree_skb(struct netns_frags *nf,
127 struct sk_buff *skb, int *work)
1da177e4
LT
128{
129 if (work)
130 *work -= skb->truesize;
6ddc0822 131 atomic_sub(skb->truesize, &nf->mem);
1da177e4
LT
132 kfree_skb(skb);
133}
134
c6fda282
PE
135static 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
1e4b8287 149static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
1da177e4 150{
1e4b8287
PE
151 struct ipq *qp;
152
153 qp = container_of(q, struct ipq, q);
154 if (qp->peer)
155 inet_putpeer(qp->peer);
1da177e4
LT
156}
157
1da177e4
LT
158
159/* Destruction primitives. */
160
4b6cb5d8 161static __inline__ void ipq_put(struct ipq *ipq)
1da177e4 162{
762cc408 163 inet_frag_put(&ipq->q, &ip4_frags);
1da177e4
LT
164}
165
166/* Kill ipq entry. It is not destroyed immediately,
167 * because caller (and someone more) holds reference count.
168 */
169static void ipq_kill(struct ipq *ipq)
170{
277e650d 171 inet_frag_kill(&ipq->q, &ip4_frags);
1da177e4
LT
172}
173
e905a9ed 174/* Memory limiting on fragments. Evictor trashes the oldest
1da177e4
LT
175 * fragment queue until we are back under the threshold.
176 */
6ddc0822 177static void ip_evictor(struct net *net)
1da177e4 178{
8e7999c4
PE
179 int evicted;
180
6ddc0822 181 evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags);
8e7999c4
PE
182 if (evicted)
183 IP_ADD_STATS_BH(IPSTATS_MIB_REASMFAILS, evicted);
1da177e4
LT
184}
185
186/*
187 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
188 */
189static void ip_expire(unsigned long arg)
190{
e521db9d
PE
191 struct ipq *qp;
192
193 qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
1da177e4 194
5ab11c98 195 spin_lock(&qp->q.lock);
1da177e4 196
5ab11c98 197 if (qp->q.last_in & COMPLETE)
1da177e4
LT
198 goto out;
199
200 ipq_kill(qp);
201
202 IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
203 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
204
5ab11c98
PE
205 if ((qp->q.last_in&FIRST_IN) && qp->q.fragments != NULL) {
206 struct sk_buff *head = qp->q.fragments;
1da177e4 207 /* Send an ICMP "Fragment Reassembly Timeout" message. */
881d966b 208 if ((head->dev = dev_get_by_index(&init_net, qp->iif)) != NULL) {
1da177e4
LT
209 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
210 dev_put(head->dev);
211 }
212 }
213out:
5ab11c98 214 spin_unlock(&qp->q.lock);
4b6cb5d8 215 ipq_put(qp);
1da177e4
LT
216}
217
abd6523d
PE
218/* Find the correct entry in the "incomplete datagrams" queue for
219 * this IP datagram, and create new one, if nothing is found.
220 */
ac18e750 221static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
1da177e4 222{
c6fda282
PE
223 struct inet_frag_queue *q;
224 struct ip4_create_arg arg;
abd6523d 225 unsigned int hash;
1da177e4 226
c6fda282
PE
227 arg.iph = iph;
228 arg.user = user;
abd6523d 229 hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
1da177e4 230
ac18e750 231 q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
c6fda282
PE
232 if (q == NULL)
233 goto out_nomem;
1da177e4 234
c6fda282 235 return container_of(q, struct ipq, q);
1da177e4
LT
236
237out_nomem:
64ce2073 238 LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
1da177e4
LT
239 return NULL;
240}
241
89cee8b1
HX
242/* Is the fragment too far ahead to be part of ipq? */
243static inline int ip_frag_too_far(struct ipq *qp)
244{
245 struct inet_peer *peer = qp->peer;
246 unsigned int max = sysctl_ipfrag_max_dist;
247 unsigned int start, end;
248
249 int rc;
250
251 if (!peer || !max)
252 return 0;
253
254 start = qp->rid;
255 end = atomic_inc_return(&peer->rid);
256 qp->rid = end;
257
5ab11c98 258 rc = qp->q.fragments && (end - start) > max;
89cee8b1
HX
259
260 if (rc) {
261 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
262 }
263
264 return rc;
265}
266
267static int ip_frag_reinit(struct ipq *qp)
268{
269 struct sk_buff *fp;
270
b2fd5321 271 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
5ab11c98 272 atomic_inc(&qp->q.refcnt);
89cee8b1
HX
273 return -ETIMEDOUT;
274 }
275
5ab11c98 276 fp = qp->q.fragments;
89cee8b1
HX
277 do {
278 struct sk_buff *xp = fp->next;
6ddc0822 279 frag_kfree_skb(qp->q.net, fp, NULL);
89cee8b1
HX
280 fp = xp;
281 } while (fp);
282
5ab11c98
PE
283 qp->q.last_in = 0;
284 qp->q.len = 0;
285 qp->q.meat = 0;
286 qp->q.fragments = NULL;
89cee8b1
HX
287 qp->iif = 0;
288
289 return 0;
290}
291
1da177e4 292/* Add new segment to existing queue. */
1706d587 293static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
1da177e4
LT
294{
295 struct sk_buff *prev, *next;
1706d587 296 struct net_device *dev;
1da177e4
LT
297 int flags, offset;
298 int ihl, end;
1706d587 299 int err = -ENOENT;
1da177e4 300
5ab11c98 301 if (qp->q.last_in & COMPLETE)
1da177e4
LT
302 goto err;
303
89cee8b1 304 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
1706d587
HX
305 unlikely(ip_frag_too_far(qp)) &&
306 unlikely(err = ip_frag_reinit(qp))) {
89cee8b1
HX
307 ipq_kill(qp);
308 goto err;
309 }
310
eddc9ec5 311 offset = ntohs(ip_hdr(skb)->frag_off);
1da177e4
LT
312 flags = offset & ~IP_OFFSET;
313 offset &= IP_OFFSET;
314 offset <<= 3; /* offset is in 8-byte chunks */
c9bdd4b5 315 ihl = ip_hdrlen(skb);
1da177e4
LT
316
317 /* Determine the position of this fragment. */
e905a9ed 318 end = offset + skb->len - ihl;
1706d587 319 err = -EINVAL;
1da177e4
LT
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 corrrupted.
325 */
5ab11c98
PE
326 if (end < qp->q.len ||
327 ((qp->q.last_in & LAST_IN) && end != qp->q.len))
1da177e4 328 goto err;
5ab11c98
PE
329 qp->q.last_in |= LAST_IN;
330 qp->q.len = end;
1da177e4
LT
331 } else {
332 if (end&7) {
333 end &= ~7;
334 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
335 skb->ip_summed = CHECKSUM_NONE;
336 }
5ab11c98 337 if (end > qp->q.len) {
1da177e4 338 /* Some bits beyond end -> corruption. */
5ab11c98 339 if (qp->q.last_in & LAST_IN)
1da177e4 340 goto err;
5ab11c98 341 qp->q.len = end;
1da177e4
LT
342 }
343 }
344 if (end == offset)
345 goto err;
346
1706d587 347 err = -ENOMEM;
1da177e4
LT
348 if (pskb_pull(skb, ihl) == NULL)
349 goto err;
1706d587
HX
350
351 err = pskb_trim_rcsum(skb, end - offset);
352 if (err)
1da177e4
LT
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 = NULL;
5ab11c98 360 for (next = qp->q.fragments; next != NULL; next = next->next) {
1da177e4
LT
361 if (FRAG_CB(next)->offset >= offset)
362 break; /* bingo! */
363 prev = next;
364 }
365
366 /* We found where to put this one. Check for overlap with
367 * preceding fragment, and, if needed, align things so that
368 * any overlaps are eliminated.
369 */
370 if (prev) {
371 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
372
373 if (i > 0) {
374 offset += i;
1706d587 375 err = -EINVAL;
1da177e4
LT
376 if (end <= offset)
377 goto err;
1706d587 378 err = -ENOMEM;
1da177e4
LT
379 if (!pskb_pull(skb, i))
380 goto err;
381 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
382 skb->ip_summed = CHECKSUM_NONE;
383 }
384 }
385
1706d587
HX
386 err = -ENOMEM;
387
1da177e4
LT
388 while (next && FRAG_CB(next)->offset < end) {
389 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
390
391 if (i < next->len) {
392 /* Eat head of the next overlapped fragment
393 * and leave the loop. The next ones cannot overlap.
394 */
395 if (!pskb_pull(next, i))
396 goto err;
397 FRAG_CB(next)->offset += i;
5ab11c98 398 qp->q.meat -= i;
1da177e4
LT
399 if (next->ip_summed != CHECKSUM_UNNECESSARY)
400 next->ip_summed = CHECKSUM_NONE;
401 break;
402 } else {
403 struct sk_buff *free_it = next;
404
47c6bf77 405 /* Old fragment is completely overridden with
1da177e4
LT
406 * new one drop it.
407 */
408 next = next->next;
409
410 if (prev)
411 prev->next = next;
412 else
5ab11c98 413 qp->q.fragments = next;
1da177e4 414
5ab11c98 415 qp->q.meat -= free_it->len;
6ddc0822 416 frag_kfree_skb(qp->q.net, free_it, NULL);
1da177e4
LT
417 }
418 }
419
420 FRAG_CB(skb)->offset = offset;
421
422 /* Insert this fragment in the chain of fragments. */
423 skb->next = next;
424 if (prev)
425 prev->next = skb;
426 else
5ab11c98 427 qp->q.fragments = skb;
1da177e4 428
1706d587
HX
429 dev = skb->dev;
430 if (dev) {
431 qp->iif = dev->ifindex;
432 skb->dev = NULL;
433 }
5ab11c98
PE
434 qp->q.stamp = skb->tstamp;
435 qp->q.meat += skb->len;
6ddc0822 436 atomic_add(skb->truesize, &qp->q.net->mem);
1da177e4 437 if (offset == 0)
5ab11c98 438 qp->q.last_in |= FIRST_IN;
1da177e4 439
5ab11c98 440 if (qp->q.last_in == (FIRST_IN | LAST_IN) && qp->q.meat == qp->q.len)
1706d587
HX
441 return ip_frag_reasm(qp, prev, dev);
442
7eb95156 443 write_lock(&ip4_frags.lock);
3140c25c 444 list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
7eb95156 445 write_unlock(&ip4_frags.lock);
1706d587 446 return -EINPROGRESS;
1da177e4
LT
447
448err:
449 kfree_skb(skb);
1706d587 450 return err;
1da177e4
LT
451}
452
453
454/* Build a new IP datagram from all its fragments. */
455
1706d587
HX
456static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
457 struct net_device *dev)
1da177e4
LT
458{
459 struct iphdr *iph;
5ab11c98 460 struct sk_buff *fp, *head = qp->q.fragments;
1da177e4
LT
461 int len;
462 int ihlen;
1706d587 463 int err;
1da177e4
LT
464
465 ipq_kill(qp);
466
1706d587
HX
467 /* Make the one we just received the head. */
468 if (prev) {
469 head = prev->next;
470 fp = skb_clone(head, GFP_ATOMIC);
1706d587
HX
471 if (!fp)
472 goto out_nomem;
473
474 fp->next = head->next;
475 prev->next = fp;
476
5ab11c98
PE
477 skb_morph(head, qp->q.fragments);
478 head->next = qp->q.fragments->next;
1706d587 479
5ab11c98
PE
480 kfree_skb(qp->q.fragments);
481 qp->q.fragments = head;
1706d587
HX
482 }
483
1da177e4
LT
484 BUG_TRAP(head != NULL);
485 BUG_TRAP(FRAG_CB(head)->offset == 0);
486
487 /* Allocate a new buffer for the datagram. */
c9bdd4b5 488 ihlen = ip_hdrlen(head);
5ab11c98 489 len = ihlen + qp->q.len;
1da177e4 490
1706d587 491 err = -E2BIG;
132adf54 492 if (len > 65535)
1da177e4
LT
493 goto out_oversize;
494
495 /* Head of list must not be cloned. */
496 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
497 goto out_nomem;
498
499 /* If the first fragment is fragmented itself, we split
500 * it to two chunks: the first with data and paged part
501 * and the second, holding only fragments. */
502 if (skb_shinfo(head)->frag_list) {
503 struct sk_buff *clone;
504 int i, plen = 0;
505
506 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
507 goto out_nomem;
508 clone->next = head->next;
509 head->next = clone;
510 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
511 skb_shinfo(head)->frag_list = NULL;
512 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
513 plen += skb_shinfo(head)->frags[i].size;
514 clone->len = clone->data_len = head->data_len - plen;
515 head->data_len -= clone->len;
516 head->len -= clone->len;
517 clone->csum = 0;
518 clone->ip_summed = head->ip_summed;
6ddc0822 519 atomic_add(clone->truesize, &qp->q.net->mem);
1da177e4
LT
520 }
521
522 skb_shinfo(head)->frag_list = head->next;
d56f90a7 523 skb_push(head, head->data - skb_network_header(head));
6ddc0822 524 atomic_sub(head->truesize, &qp->q.net->mem);
1da177e4
LT
525
526 for (fp=head->next; fp; fp = fp->next) {
527 head->data_len += fp->len;
528 head->len += fp->len;
529 if (head->ip_summed != fp->ip_summed)
530 head->ip_summed = CHECKSUM_NONE;
84fa7933 531 else if (head->ip_summed == CHECKSUM_COMPLETE)
1da177e4
LT
532 head->csum = csum_add(head->csum, fp->csum);
533 head->truesize += fp->truesize;
6ddc0822 534 atomic_sub(fp->truesize, &qp->q.net->mem);
1da177e4
LT
535 }
536
537 head->next = NULL;
538 head->dev = dev;
5ab11c98 539 head->tstamp = qp->q.stamp;
1da177e4 540
eddc9ec5 541 iph = ip_hdr(head);
1da177e4
LT
542 iph->frag_off = 0;
543 iph->tot_len = htons(len);
544 IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
5ab11c98 545 qp->q.fragments = NULL;
1706d587 546 return 0;
1da177e4
LT
547
548out_nomem:
e905a9ed 549 LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
64ce2073 550 "queue %p\n", qp);
45542479 551 err = -ENOMEM;
1da177e4
LT
552 goto out_fail;
553out_oversize:
554 if (net_ratelimit())
555 printk(KERN_INFO
556 "Oversized IP packet from %d.%d.%d.%d.\n",
557 NIPQUAD(qp->saddr));
558out_fail:
559 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
1706d587 560 return err;
1da177e4
LT
561}
562
563/* Process an incoming IP datagram fragment. */
776c729e 564int ip_defrag(struct sk_buff *skb, u32 user)
1da177e4 565{
1da177e4 566 struct ipq *qp;
ac18e750 567 struct net *net;
e905a9ed 568
1da177e4
LT
569 IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
570
ac18e750 571 net = skb->dev->nd_net;
1da177e4 572 /* Start by cleaning up the memory. */
e31e0bdc 573 if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
6ddc0822 574 ip_evictor(net);
1da177e4 575
1da177e4 576 /* Lookup (or create) queue header */
ac18e750 577 if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
1706d587 578 int ret;
1da177e4 579
5ab11c98 580 spin_lock(&qp->q.lock);
1da177e4 581
1706d587 582 ret = ip_frag_queue(qp, skb);
1da177e4 583
5ab11c98 584 spin_unlock(&qp->q.lock);
4b6cb5d8 585 ipq_put(qp);
776c729e 586 return ret;
1da177e4
LT
587 }
588
589 IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
590 kfree_skb(skb);
776c729e 591 return -ENOMEM;
1da177e4
LT
592}
593
8d8354d2
PE
594#ifdef CONFIG_SYSCTL
595static int zero;
596
597static struct ctl_table ip4_frags_ctl_table[] = {
598 {
599 .ctl_name = NET_IPV4_IPFRAG_HIGH_THRESH,
600 .procname = "ipfrag_high_thresh",
e31e0bdc 601 .data = &init_net.ipv4.frags.high_thresh,
8d8354d2
PE
602 .maxlen = sizeof(int),
603 .mode = 0644,
604 .proc_handler = &proc_dointvec
605 },
606 {
607 .ctl_name = NET_IPV4_IPFRAG_LOW_THRESH,
608 .procname = "ipfrag_low_thresh",
e31e0bdc 609 .data = &init_net.ipv4.frags.low_thresh,
8d8354d2
PE
610 .maxlen = sizeof(int),
611 .mode = 0644,
612 .proc_handler = &proc_dointvec
613 },
614 {
615 .ctl_name = NET_IPV4_IPFRAG_TIME,
616 .procname = "ipfrag_time",
b2fd5321 617 .data = &init_net.ipv4.frags.timeout,
8d8354d2
PE
618 .maxlen = sizeof(int),
619 .mode = 0644,
620 .proc_handler = &proc_dointvec_jiffies,
621 .strategy = &sysctl_jiffies
622 },
623 {
624 .ctl_name = NET_IPV4_IPFRAG_SECRET_INTERVAL,
625 .procname = "ipfrag_secret_interval",
3b4bc4a2 626 .data = &ip4_frags.secret_interval,
8d8354d2
PE
627 .maxlen = sizeof(int),
628 .mode = 0644,
629 .proc_handler = &proc_dointvec_jiffies,
630 .strategy = &sysctl_jiffies
631 },
632 {
633 .procname = "ipfrag_max_dist",
634 .data = &sysctl_ipfrag_max_dist,
635 .maxlen = sizeof(int),
636 .mode = 0644,
637 .proc_handler = &proc_dointvec_minmax,
638 .extra1 = &zero
639 },
640 { }
641};
642
643static int ip4_frags_ctl_register(struct net *net)
644{
e4a2d5c2 645 struct ctl_table *table;
8d8354d2
PE
646 struct ctl_table_header *hdr;
647
e4a2d5c2
PE
648 table = ip4_frags_ctl_table;
649 if (net != &init_net) {
650 table = kmemdup(table, sizeof(ip4_frags_ctl_table), GFP_KERNEL);
651 if (table == NULL)
652 goto err_alloc;
653
e31e0bdc
PE
654 table[0].data = &net->ipv4.frags.high_thresh;
655 table[1].data = &net->ipv4.frags.low_thresh;
b2fd5321 656 table[2].data = &net->ipv4.frags.timeout;
e4a2d5c2
PE
657 table[3].mode &= ~0222;
658 table[4].mode &= ~0222;
659 }
660
661 hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
662 if (hdr == NULL)
663 goto err_reg;
664
665 net->ipv4.frags_hdr = hdr;
666 return 0;
667
668err_reg:
669 if (net != &init_net)
670 kfree(table);
671err_alloc:
672 return -ENOMEM;
673}
674
675static void ip4_frags_ctl_unregister(struct net *net)
676{
677 struct ctl_table *table;
678
679 table = net->ipv4.frags_hdr->ctl_table_arg;
680 unregister_net_sysctl_table(net->ipv4.frags_hdr);
681 kfree(table);
8d8354d2
PE
682}
683#else
684static inline int ip4_frags_ctl_register(struct net *net)
685{
686 return 0;
687}
e4a2d5c2
PE
688
689static inline void ip4_frags_ctl_unregister(struct net *net)
690{
691}
8d8354d2
PE
692#endif
693
694static int ipv4_frags_init_net(struct net *net)
695{
e31e0bdc
PE
696 /*
697 * Fragment cache limits. We will commit 256K at one time. Should we
698 * cross that limit we will prune down to 192K. This should cope with
699 * even the most extreme cases without allowing an attacker to
700 * measurably harm machine performance.
701 */
702 net->ipv4.frags.high_thresh = 256 * 1024;
703 net->ipv4.frags.low_thresh = 192 * 1024;
b2fd5321
PE
704 /*
705 * Important NOTE! Fragment queue must be destroyed before MSL expires.
706 * RFC791 is wrong proposing to prolongate timer each fragment arrival
707 * by TTL.
708 */
709 net->ipv4.frags.timeout = IP_FRAG_TIME;
710
e5a2bb84
PE
711 inet_frags_init_net(&net->ipv4.frags);
712
8d8354d2
PE
713 return ip4_frags_ctl_register(net);
714}
715
b7aa0bf7 716void __init ipfrag_init(void)
1da177e4 717{
8d8354d2 718 ipv4_frags_init_net(&init_net);
321a3a99 719 ip4_frags.hashfn = ip4_hashfn;
c6fda282 720 ip4_frags.constructor = ip4_frag_init;
1e4b8287
PE
721 ip4_frags.destructor = ip4_frag_free;
722 ip4_frags.skb_free = NULL;
723 ip4_frags.qsize = sizeof(struct ipq);
abd6523d 724 ip4_frags.match = ip4_frag_match;
e521db9d 725 ip4_frags.frag_expire = ip_expire;
3b4bc4a2 726 ip4_frags.secret_interval = 10 * 60 * HZ;
7eb95156 727 inet_frags_init(&ip4_frags);
1da177e4
LT
728}
729
730EXPORT_SYMBOL(ip_defrag);