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