]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv4/ip_fragment.c
inet: frags: get rif of inet_frag_evicting()
[mirror_ubuntu-bionic-kernel.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 (IS_ERR_OR_NULL(q)) {
225 inet_frag_maybe_warn_overflow(q, pr_fmt());
226 return NULL;
227 }
228 return container_of(q, struct ipq, q);
229 }
230
231 /* Is the fragment too far ahead to be part of ipq? */
232 static int ip_frag_too_far(struct ipq *qp)
233 {
234 struct inet_peer *peer = qp->peer;
235 unsigned int max = qp->q.net->max_dist;
236 unsigned int start, end;
237
238 int rc;
239
240 if (!peer || !max)
241 return 0;
242
243 start = qp->rid;
244 end = atomic_inc_return(&peer->rid);
245 qp->rid = end;
246
247 rc = qp->q.fragments && (end - start) > max;
248
249 if (rc) {
250 struct net *net;
251
252 net = container_of(qp->q.net, struct net, ipv4.frags);
253 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
254 }
255
256 return rc;
257 }
258
259 static int ip_frag_reinit(struct ipq *qp)
260 {
261 struct sk_buff *fp;
262 unsigned int sum_truesize = 0;
263
264 if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
265 refcount_inc(&qp->q.refcnt);
266 return -ETIMEDOUT;
267 }
268
269 fp = qp->q.fragments;
270 do {
271 struct sk_buff *xp = fp->next;
272
273 sum_truesize += fp->truesize;
274 kfree_skb(fp);
275 fp = xp;
276 } while (fp);
277 sub_frag_mem_limit(qp->q.net, sum_truesize);
278
279 qp->q.flags = 0;
280 qp->q.len = 0;
281 qp->q.meat = 0;
282 qp->q.fragments = NULL;
283 qp->q.fragments_tail = NULL;
284 qp->iif = 0;
285 qp->ecn = 0;
286
287 return 0;
288 }
289
290 /* Add new segment to existing queue. */
291 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
292 {
293 struct sk_buff *prev, *next;
294 struct net_device *dev;
295 unsigned int fragsize;
296 int flags, offset;
297 int ihl, end;
298 int err = -ENOENT;
299 u8 ecn;
300
301 if (qp->q.flags & INET_FRAG_COMPLETE)
302 goto err;
303
304 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
305 unlikely(ip_frag_too_far(qp)) &&
306 unlikely(err = ip_frag_reinit(qp))) {
307 ipq_kill(qp);
308 goto err;
309 }
310
311 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
312 offset = ntohs(ip_hdr(skb)->frag_off);
313 flags = offset & ~IP_OFFSET;
314 offset &= IP_OFFSET;
315 offset <<= 3; /* offset is in 8-byte chunks */
316 ihl = ip_hdrlen(skb);
317
318 /* Determine the position of this fragment. */
319 end = offset + skb->len - skb_network_offset(skb) - ihl;
320 err = -EINVAL;
321
322 /* Is this the final fragment? */
323 if ((flags & IP_MF) == 0) {
324 /* If we already have some bits beyond end
325 * or have different end, the segment is corrupted.
326 */
327 if (end < qp->q.len ||
328 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
329 goto err;
330 qp->q.flags |= INET_FRAG_LAST_IN;
331 qp->q.len = end;
332 } else {
333 if (end&7) {
334 end &= ~7;
335 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
336 skb->ip_summed = CHECKSUM_NONE;
337 }
338 if (end > qp->q.len) {
339 /* Some bits beyond end -> corruption. */
340 if (qp->q.flags & INET_FRAG_LAST_IN)
341 goto err;
342 qp->q.len = end;
343 }
344 }
345 if (end == offset)
346 goto err;
347
348 err = -ENOMEM;
349 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
350 goto err;
351
352 err = pskb_trim_rcsum(skb, end - offset);
353 if (err)
354 goto err;
355
356 /* Find out which fragments are in front and at the back of us
357 * in the chain of fragments so far. We must know where to put
358 * this fragment, right?
359 */
360 prev = qp->q.fragments_tail;
361 if (!prev || FRAG_CB(prev)->offset < offset) {
362 next = NULL;
363 goto found;
364 }
365 prev = NULL;
366 for (next = qp->q.fragments; next != NULL; next = next->next) {
367 if (FRAG_CB(next)->offset >= offset)
368 break; /* bingo! */
369 prev = next;
370 }
371
372 found:
373 /* We found where to put this one. Check for overlap with
374 * preceding fragment, and, if needed, align things so that
375 * any overlaps are eliminated.
376 */
377 if (prev) {
378 int i = (FRAG_CB(prev)->offset + prev->len) - offset;
379
380 if (i > 0) {
381 offset += i;
382 err = -EINVAL;
383 if (end <= offset)
384 goto err;
385 err = -ENOMEM;
386 if (!pskb_pull(skb, i))
387 goto err;
388 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
389 skb->ip_summed = CHECKSUM_NONE;
390 }
391 }
392
393 err = -ENOMEM;
394
395 while (next && FRAG_CB(next)->offset < end) {
396 int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
397
398 if (i < next->len) {
399 int delta = -next->truesize;
400
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 delta += next->truesize;
407 if (delta)
408 add_frag_mem_limit(qp->q.net, delta);
409 FRAG_CB(next)->offset += i;
410 qp->q.meat -= i;
411 if (next->ip_summed != CHECKSUM_UNNECESSARY)
412 next->ip_summed = CHECKSUM_NONE;
413 break;
414 } else {
415 struct sk_buff *free_it = next;
416
417 /* Old fragment is completely overridden with
418 * new one drop it.
419 */
420 next = next->next;
421
422 if (prev)
423 prev->next = next;
424 else
425 qp->q.fragments = next;
426
427 qp->q.meat -= free_it->len;
428 sub_frag_mem_limit(qp->q.net, free_it->truesize);
429 kfree_skb(free_it);
430 }
431 }
432
433 FRAG_CB(skb)->offset = offset;
434
435 /* Insert this fragment in the chain of fragments. */
436 skb->next = next;
437 if (!next)
438 qp->q.fragments_tail = skb;
439 if (prev)
440 prev->next = skb;
441 else
442 qp->q.fragments = skb;
443
444 dev = skb->dev;
445 if (dev) {
446 qp->iif = dev->ifindex;
447 skb->dev = NULL;
448 }
449 qp->q.stamp = skb->tstamp;
450 qp->q.meat += skb->len;
451 qp->ecn |= ecn;
452 add_frag_mem_limit(qp->q.net, skb->truesize);
453 if (offset == 0)
454 qp->q.flags |= INET_FRAG_FIRST_IN;
455
456 fragsize = skb->len + ihl;
457
458 if (fragsize > qp->q.max_size)
459 qp->q.max_size = fragsize;
460
461 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
462 fragsize > qp->max_df_size)
463 qp->max_df_size = fragsize;
464
465 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
466 qp->q.meat == qp->q.len) {
467 unsigned long orefdst = skb->_skb_refdst;
468
469 skb->_skb_refdst = 0UL;
470 err = ip_frag_reasm(qp, prev, dev);
471 skb->_skb_refdst = orefdst;
472 return err;
473 }
474
475 skb_dst_drop(skb);
476 return -EINPROGRESS;
477
478 err:
479 kfree_skb(skb);
480 return err;
481 }
482
483
484 /* Build a new IP datagram from all its fragments. */
485
486 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
487 struct net_device *dev)
488 {
489 struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
490 struct iphdr *iph;
491 struct sk_buff *fp, *head = qp->q.fragments;
492 int len;
493 int ihlen;
494 int err;
495 u8 ecn;
496
497 ipq_kill(qp);
498
499 ecn = ip_frag_ecn_table[qp->ecn];
500 if (unlikely(ecn == 0xff)) {
501 err = -EINVAL;
502 goto out_fail;
503 }
504 /* Make the one we just received the head. */
505 if (prev) {
506 head = prev->next;
507 fp = skb_clone(head, GFP_ATOMIC);
508 if (!fp)
509 goto out_nomem;
510
511 fp->next = head->next;
512 if (!fp->next)
513 qp->q.fragments_tail = fp;
514 prev->next = fp;
515
516 skb_morph(head, qp->q.fragments);
517 head->next = qp->q.fragments->next;
518
519 consume_skb(qp->q.fragments);
520 qp->q.fragments = head;
521 }
522
523 WARN_ON(!head);
524 WARN_ON(FRAG_CB(head)->offset != 0);
525
526 /* Allocate a new buffer for the datagram. */
527 ihlen = ip_hdrlen(head);
528 len = ihlen + qp->q.len;
529
530 err = -E2BIG;
531 if (len > 65535)
532 goto out_oversize;
533
534 /* Head of list must not be cloned. */
535 if (skb_unclone(head, GFP_ATOMIC))
536 goto out_nomem;
537
538 /* If the first fragment is fragmented itself, we split
539 * it to two chunks: the first with data and paged part
540 * and the second, holding only fragments. */
541 if (skb_has_frag_list(head)) {
542 struct sk_buff *clone;
543 int i, plen = 0;
544
545 clone = alloc_skb(0, GFP_ATOMIC);
546 if (!clone)
547 goto out_nomem;
548 clone->next = head->next;
549 head->next = clone;
550 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
551 skb_frag_list_init(head);
552 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
553 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
554 clone->len = clone->data_len = head->data_len - plen;
555 head->data_len -= clone->len;
556 head->len -= clone->len;
557 clone->csum = 0;
558 clone->ip_summed = head->ip_summed;
559 add_frag_mem_limit(qp->q.net, clone->truesize);
560 }
561
562 skb_shinfo(head)->frag_list = head->next;
563 skb_push(head, head->data - skb_network_header(head));
564
565 for (fp=head->next; fp; fp = fp->next) {
566 head->data_len += fp->len;
567 head->len += fp->len;
568 if (head->ip_summed != fp->ip_summed)
569 head->ip_summed = CHECKSUM_NONE;
570 else if (head->ip_summed == CHECKSUM_COMPLETE)
571 head->csum = csum_add(head->csum, fp->csum);
572 head->truesize += fp->truesize;
573 }
574 sub_frag_mem_limit(qp->q.net, head->truesize);
575
576 head->next = NULL;
577 head->dev = dev;
578 head->tstamp = qp->q.stamp;
579 IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
580
581 iph = ip_hdr(head);
582 iph->tot_len = htons(len);
583 iph->tos |= ecn;
584
585 /* When we set IP_DF on a refragmented skb we must also force a
586 * call to ip_fragment to avoid forwarding a DF-skb of size s while
587 * original sender only sent fragments of size f (where f < s).
588 *
589 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
590 * frag seen to avoid sending tiny DF-fragments in case skb was built
591 * from one very small df-fragment and one large non-df frag.
592 */
593 if (qp->max_df_size == qp->q.max_size) {
594 IPCB(head)->flags |= IPSKB_FRAG_PMTU;
595 iph->frag_off = htons(IP_DF);
596 } else {
597 iph->frag_off = 0;
598 }
599
600 ip_send_check(iph);
601
602 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
603 qp->q.fragments = NULL;
604 qp->q.fragments_tail = NULL;
605 return 0;
606
607 out_nomem:
608 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
609 err = -ENOMEM;
610 goto out_fail;
611 out_oversize:
612 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
613 out_fail:
614 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
615 return err;
616 }
617
618 /* Process an incoming IP datagram fragment. */
619 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
620 {
621 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
622 int vif = l3mdev_master_ifindex_rcu(dev);
623 struct ipq *qp;
624
625 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
626 skb_orphan(skb);
627
628 /* Lookup (or create) queue header */
629 qp = ip_find(net, ip_hdr(skb), user, vif);
630 if (qp) {
631 int ret;
632
633 spin_lock(&qp->q.lock);
634
635 ret = ip_frag_queue(qp, skb);
636
637 spin_unlock(&qp->q.lock);
638 ipq_put(qp);
639 return ret;
640 }
641
642 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
643 kfree_skb(skb);
644 return -ENOMEM;
645 }
646 EXPORT_SYMBOL(ip_defrag);
647
648 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
649 {
650 struct iphdr iph;
651 int netoff;
652 u32 len;
653
654 if (skb->protocol != htons(ETH_P_IP))
655 return skb;
656
657 netoff = skb_network_offset(skb);
658
659 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
660 return skb;
661
662 if (iph.ihl < 5 || iph.version != 4)
663 return skb;
664
665 len = ntohs(iph.tot_len);
666 if (skb->len < netoff + len || len < (iph.ihl * 4))
667 return skb;
668
669 if (ip_is_fragment(&iph)) {
670 skb = skb_share_check(skb, GFP_ATOMIC);
671 if (skb) {
672 if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
673 return skb;
674 if (pskb_trim_rcsum(skb, netoff + len))
675 return skb;
676 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
677 if (ip_defrag(net, skb, user))
678 return NULL;
679 skb_clear_hash(skb);
680 }
681 }
682 return skb;
683 }
684 EXPORT_SYMBOL(ip_check_defrag);
685
686 #ifdef CONFIG_SYSCTL
687 static int zero;
688
689 static struct ctl_table ip4_frags_ns_ctl_table[] = {
690 {
691 .procname = "ipfrag_high_thresh",
692 .data = &init_net.ipv4.frags.high_thresh,
693 .maxlen = sizeof(int),
694 .mode = 0644,
695 .proc_handler = proc_dointvec_minmax,
696 .extra1 = &init_net.ipv4.frags.low_thresh
697 },
698 {
699 .procname = "ipfrag_low_thresh",
700 .data = &init_net.ipv4.frags.low_thresh,
701 .maxlen = sizeof(int),
702 .mode = 0644,
703 .proc_handler = proc_dointvec_minmax,
704 .extra1 = &zero,
705 .extra2 = &init_net.ipv4.frags.high_thresh
706 },
707 {
708 .procname = "ipfrag_time",
709 .data = &init_net.ipv4.frags.timeout,
710 .maxlen = sizeof(int),
711 .mode = 0644,
712 .proc_handler = proc_dointvec_jiffies,
713 },
714 {
715 .procname = "ipfrag_max_dist",
716 .data = &init_net.ipv4.frags.max_dist,
717 .maxlen = sizeof(int),
718 .mode = 0644,
719 .proc_handler = proc_dointvec_minmax,
720 .extra1 = &zero
721 },
722 { }
723 };
724
725 /* secret interval has been deprecated */
726 static int ip4_frags_secret_interval_unused;
727 static struct ctl_table ip4_frags_ctl_table[] = {
728 {
729 .procname = "ipfrag_secret_interval",
730 .data = &ip4_frags_secret_interval_unused,
731 .maxlen = sizeof(int),
732 .mode = 0644,
733 .proc_handler = proc_dointvec_jiffies,
734 },
735 { }
736 };
737
738 static int __net_init ip4_frags_ns_ctl_register(struct net *net)
739 {
740 struct ctl_table *table;
741 struct ctl_table_header *hdr;
742
743 table = ip4_frags_ns_ctl_table;
744 if (!net_eq(net, &init_net)) {
745 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
746 if (!table)
747 goto err_alloc;
748
749 table[0].data = &net->ipv4.frags.high_thresh;
750 table[0].extra1 = &net->ipv4.frags.low_thresh;
751 table[0].extra2 = &init_net.ipv4.frags.high_thresh;
752 table[1].data = &net->ipv4.frags.low_thresh;
753 table[1].extra2 = &net->ipv4.frags.high_thresh;
754 table[2].data = &net->ipv4.frags.timeout;
755 table[3].data = &net->ipv4.frags.max_dist;
756 }
757
758 hdr = register_net_sysctl(net, "net/ipv4", table);
759 if (!hdr)
760 goto err_reg;
761
762 net->ipv4.frags_hdr = hdr;
763 return 0;
764
765 err_reg:
766 if (!net_eq(net, &init_net))
767 kfree(table);
768 err_alloc:
769 return -ENOMEM;
770 }
771
772 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
773 {
774 struct ctl_table *table;
775
776 table = net->ipv4.frags_hdr->ctl_table_arg;
777 unregister_net_sysctl_table(net->ipv4.frags_hdr);
778 kfree(table);
779 }
780
781 static void __init ip4_frags_ctl_register(void)
782 {
783 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
784 }
785 #else
786 static int ip4_frags_ns_ctl_register(struct net *net)
787 {
788 return 0;
789 }
790
791 static void ip4_frags_ns_ctl_unregister(struct net *net)
792 {
793 }
794
795 static void __init ip4_frags_ctl_register(void)
796 {
797 }
798 #endif
799
800 static int __net_init ipv4_frags_init_net(struct net *net)
801 {
802 int res;
803
804 /*
805 * Fragment cache limits. We will commit 256K at one time. Should we
806 * cross that limit we will prune down to 192K. This should cope with
807 * even the most extreme cases without allowing an attacker to
808 * measurably harm machine performance.
809 */
810 net->ipv4.frags.high_thresh = 256 * 1024;
811 net->ipv4.frags.low_thresh = 192 * 1024;
812 /*
813 * Important NOTE! Fragment queue must be destroyed before MSL expires.
814 * RFC791 is wrong proposing to prolongate timer each fragment arrival
815 * by TTL.
816 */
817 net->ipv4.frags.timeout = IP_FRAG_TIME;
818
819 net->ipv4.frags.max_dist = 64;
820 net->ipv4.frags.f = &ip4_frags;
821
822 res = inet_frags_init_net(&net->ipv4.frags);
823 if (res < 0)
824 return res;
825 res = ip4_frags_ns_ctl_register(net);
826 if (res < 0)
827 inet_frags_exit_net(&net->ipv4.frags);
828 return res;
829 }
830
831 static void __net_exit ipv4_frags_exit_net(struct net *net)
832 {
833 ip4_frags_ns_ctl_unregister(net);
834 inet_frags_exit_net(&net->ipv4.frags);
835 }
836
837 static struct pernet_operations ip4_frags_ops = {
838 .init = ipv4_frags_init_net,
839 .exit = ipv4_frags_exit_net,
840 };
841
842
843 static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
844 {
845 return jhash2(data,
846 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
847 }
848
849 static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
850 {
851 const struct inet_frag_queue *fq = data;
852
853 return jhash2((const u32 *)&fq->key.v4,
854 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
855 }
856
857 static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
858 {
859 const struct frag_v4_compare_key *key = arg->key;
860 const struct inet_frag_queue *fq = ptr;
861
862 return !!memcmp(&fq->key, key, sizeof(*key));
863 }
864
865 static const struct rhashtable_params ip4_rhash_params = {
866 .head_offset = offsetof(struct inet_frag_queue, node),
867 .key_offset = offsetof(struct inet_frag_queue, key),
868 .key_len = sizeof(struct frag_v4_compare_key),
869 .hashfn = ip4_key_hashfn,
870 .obj_hashfn = ip4_obj_hashfn,
871 .obj_cmpfn = ip4_obj_cmpfn,
872 .automatic_shrinking = true,
873 };
874
875 void __init ipfrag_init(void)
876 {
877 ip4_frags.constructor = ip4_frag_init;
878 ip4_frags.destructor = ip4_frag_free;
879 ip4_frags.qsize = sizeof(struct ipq);
880 ip4_frags.frag_expire = ip_expire;
881 ip4_frags.frags_cache_name = ip_frag_cache_name;
882 ip4_frags.rhash_params = ip4_rhash_params;
883 if (inet_frags_init(&ip4_frags))
884 panic("IP: failed to allocate ip4_frags cache\n");
885 ip4_frags_ctl_register();
886 register_pernet_subsys(&ip4_frags_ops);
887 }