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 /* Describe an entry in the "incomplete datagrams" queue. */
61 struct ipq {
62 struct inet_frag_queue q;
63
64 u8 ecn; /* RFC3168 support */
65 u16 max_df_size; /* largest frag with DF set seen */
66 int iif;
67 unsigned int rid;
68 struct inet_peer *peer;
69 };
70
ip4_frag_ecn(u8 tos)71 static u8 ip4_frag_ecn(u8 tos)
72 {
73 return 1 << (tos & INET_ECN_MASK);
74 }
75
76 static struct inet_frags ip4_frags;
77
78 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
79 struct sk_buff *prev_tail, struct net_device *dev);
80
81
ip4_frag_init(struct inet_frag_queue * q,const void * a)82 static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
83 {
84 struct ipq *qp = container_of(q, struct ipq, q);
85 const struct frag_v4_compare_key *key = a;
86 struct net *net = q->fqdir->net;
87 struct inet_peer *p = NULL;
88
89 q->key.v4 = *key;
90 qp->ecn = 0;
91 if (q->fqdir->max_dist) {
92 rcu_read_lock();
93 p = inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif);
94 if (p && !refcount_inc_not_zero(&p->refcnt))
95 p = NULL;
96 rcu_read_unlock();
97 }
98 qp->peer = p;
99 }
100
ip4_frag_free(struct inet_frag_queue * q)101 static void ip4_frag_free(struct inet_frag_queue *q)
102 {
103 struct ipq *qp;
104
105 qp = container_of(q, struct ipq, q);
106 if (qp->peer)
107 inet_putpeer(qp->peer);
108 }
109
110
111 /* Destruction primitives. */
112
ipq_put(struct ipq * ipq)113 static void ipq_put(struct ipq *ipq)
114 {
115 inet_frag_put(&ipq->q);
116 }
117
118 /* Kill ipq entry. It is not destroyed immediately,
119 * because caller (and someone more) holds reference count.
120 */
ipq_kill(struct ipq * ipq)121 static void ipq_kill(struct ipq *ipq)
122 {
123 inet_frag_kill(&ipq->q);
124 }
125
frag_expire_skip_icmp(u32 user)126 static bool frag_expire_skip_icmp(u32 user)
127 {
128 return user == IP_DEFRAG_AF_PACKET ||
129 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
130 __IP_DEFRAG_CONNTRACK_IN_END) ||
131 ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
132 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
133 }
134
135 /*
136 * Oops, a fragment queue timed out. Kill it and send an ICMP reply.
137 */
ip_expire(struct timer_list * t)138 static void ip_expire(struct timer_list *t)
139 {
140 struct inet_frag_queue *frag = from_timer(frag, t, timer);
141 const struct iphdr *iph;
142 struct sk_buff *head = NULL;
143 struct net *net;
144 struct ipq *qp;
145 int err;
146
147 qp = container_of(frag, struct ipq, q);
148 net = qp->q.fqdir->net;
149
150 rcu_read_lock();
151
152 /* Paired with WRITE_ONCE() in fqdir_pre_exit(). */
153 if (READ_ONCE(qp->q.fqdir->dead))
154 goto out_rcu_unlock;
155
156 spin_lock(&qp->q.lock);
157
158 if (qp->q.flags & INET_FRAG_COMPLETE)
159 goto out;
160
161 qp->q.flags |= INET_FRAG_DROP;
162 ipq_kill(qp);
163 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
164 __IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
165
166 if (!(qp->q.flags & INET_FRAG_FIRST_IN))
167 goto out;
168
169 /* sk_buff::dev and sk_buff::rbnode are unionized. So we
170 * pull the head out of the tree in order to be able to
171 * deal with head->dev.
172 */
173 head = inet_frag_pull_head(&qp->q);
174 if (!head)
175 goto out;
176 head->dev = dev_get_by_index_rcu(net, qp->iif);
177 if (!head->dev)
178 goto out;
179
180
181 /* skb has no dst, perform route lookup again */
182 iph = ip_hdr(head);
183 err = ip_route_input_noref(head, iph->daddr, iph->saddr,
184 iph->tos, head->dev);
185 if (err)
186 goto out;
187
188 /* Only an end host needs to send an ICMP
189 * "Fragment Reassembly Timeout" message, per RFC792.
190 */
191 if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
192 (skb_rtable(head)->rt_type != RTN_LOCAL))
193 goto out;
194
195 spin_unlock(&qp->q.lock);
196 icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
197 goto out_rcu_unlock;
198
199 out:
200 spin_unlock(&qp->q.lock);
201 out_rcu_unlock:
202 rcu_read_unlock();
203 kfree_skb_reason(head, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
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 */
ip_find(struct net * net,struct iphdr * iph,u32 user,int vif)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.fqdir, &key);
224 if (!q)
225 return NULL;
226
227 return container_of(q, struct ipq, q);
228 }
229
230 /* Is the fragment too far ahead to be part of ipq? */
ip_frag_too_far(struct ipq * qp)231 static int ip_frag_too_far(struct ipq *qp)
232 {
233 struct inet_peer *peer = qp->peer;
234 unsigned int max = qp->q.fqdir->max_dist;
235 unsigned int start, end;
236
237 int rc;
238
239 if (!peer || !max)
240 return 0;
241
242 start = qp->rid;
243 end = atomic_inc_return(&peer->rid);
244 qp->rid = end;
245
246 rc = qp->q.fragments_tail && (end - start) > max;
247
248 if (rc)
249 __IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS);
250
251 return rc;
252 }
253
ip_frag_reinit(struct ipq * qp)254 static int ip_frag_reinit(struct ipq *qp)
255 {
256 unsigned int sum_truesize = 0;
257
258 if (!mod_timer(&qp->q.timer, jiffies + qp->q.fqdir->timeout)) {
259 refcount_inc(&qp->q.refcnt);
260 return -ETIMEDOUT;
261 }
262
263 sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments,
264 SKB_DROP_REASON_FRAG_TOO_FAR);
265 sub_frag_mem_limit(qp->q.fqdir, sum_truesize);
266
267 qp->q.flags = 0;
268 qp->q.len = 0;
269 qp->q.meat = 0;
270 qp->q.rb_fragments = RB_ROOT;
271 qp->q.fragments_tail = NULL;
272 qp->q.last_run_head = NULL;
273 qp->iif = 0;
274 qp->ecn = 0;
275
276 return 0;
277 }
278
279 /* Add new segment to existing queue. */
ip_frag_queue(struct ipq * qp,struct sk_buff * skb)280 static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
281 {
282 struct net *net = qp->q.fqdir->net;
283 int ihl, end, flags, offset;
284 struct sk_buff *prev_tail;
285 struct net_device *dev;
286 unsigned int fragsize;
287 int err = -ENOENT;
288 SKB_DR(reason);
289 u8 ecn;
290
291 /* If reassembly is already done, @skb must be a duplicate frag. */
292 if (qp->q.flags & INET_FRAG_COMPLETE) {
293 SKB_DR_SET(reason, DUP_FRAG);
294 goto err;
295 }
296
297 if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
298 unlikely(ip_frag_too_far(qp)) &&
299 unlikely(err = ip_frag_reinit(qp))) {
300 ipq_kill(qp);
301 goto err;
302 }
303
304 ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
305 offset = ntohs(ip_hdr(skb)->frag_off);
306 flags = offset & ~IP_OFFSET;
307 offset &= IP_OFFSET;
308 offset <<= 3; /* offset is in 8-byte chunks */
309 ihl = ip_hdrlen(skb);
310
311 /* Determine the position of this fragment. */
312 end = offset + skb->len - skb_network_offset(skb) - ihl;
313 err = -EINVAL;
314
315 /* Is this the final fragment? */
316 if ((flags & IP_MF) == 0) {
317 /* If we already have some bits beyond end
318 * or have different end, the segment is corrupted.
319 */
320 if (end < qp->q.len ||
321 ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
322 goto discard_qp;
323 qp->q.flags |= INET_FRAG_LAST_IN;
324 qp->q.len = end;
325 } else {
326 if (end&7) {
327 end &= ~7;
328 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
329 skb->ip_summed = CHECKSUM_NONE;
330 }
331 if (end > qp->q.len) {
332 /* Some bits beyond end -> corruption. */
333 if (qp->q.flags & INET_FRAG_LAST_IN)
334 goto discard_qp;
335 qp->q.len = end;
336 }
337 }
338 if (end == offset)
339 goto discard_qp;
340
341 err = -ENOMEM;
342 if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
343 goto discard_qp;
344
345 err = pskb_trim_rcsum(skb, end - offset);
346 if (err)
347 goto discard_qp;
348
349 /* Note : skb->rbnode and skb->dev share the same location. */
350 dev = skb->dev;
351 /* Makes sure compiler wont do silly aliasing games */
352 barrier();
353
354 prev_tail = qp->q.fragments_tail;
355 err = inet_frag_queue_insert(&qp->q, skb, offset, end);
356 if (err)
357 goto insert_error;
358
359 if (dev)
360 qp->iif = dev->ifindex;
361
362 qp->q.stamp = skb->tstamp;
363 qp->q.mono_delivery_time = skb->mono_delivery_time;
364 qp->q.meat += skb->len;
365 qp->ecn |= ecn;
366 add_frag_mem_limit(qp->q.fqdir, skb->truesize);
367 if (offset == 0)
368 qp->q.flags |= INET_FRAG_FIRST_IN;
369
370 fragsize = skb->len + ihl;
371
372 if (fragsize > qp->q.max_size)
373 qp->q.max_size = fragsize;
374
375 if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
376 fragsize > qp->max_df_size)
377 qp->max_df_size = fragsize;
378
379 if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
380 qp->q.meat == qp->q.len) {
381 unsigned long orefdst = skb->_skb_refdst;
382
383 skb->_skb_refdst = 0UL;
384 err = ip_frag_reasm(qp, skb, prev_tail, dev);
385 skb->_skb_refdst = orefdst;
386 if (err)
387 inet_frag_kill(&qp->q);
388 return err;
389 }
390
391 skb_dst_drop(skb);
392 skb_orphan(skb);
393 return -EINPROGRESS;
394
395 insert_error:
396 if (err == IPFRAG_DUP) {
397 SKB_DR_SET(reason, DUP_FRAG);
398 err = -EINVAL;
399 goto err;
400 }
401 err = -EINVAL;
402 __IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
403 discard_qp:
404 inet_frag_kill(&qp->q);
405 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
406 err:
407 kfree_skb_reason(skb, reason);
408 return err;
409 }
410
ip_frag_coalesce_ok(const struct ipq * qp)411 static bool ip_frag_coalesce_ok(const struct ipq *qp)
412 {
413 return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER;
414 }
415
416 /* Build a new IP datagram from all its fragments. */
ip_frag_reasm(struct ipq * qp,struct sk_buff * skb,struct sk_buff * prev_tail,struct net_device * dev)417 static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
418 struct sk_buff *prev_tail, struct net_device *dev)
419 {
420 struct net *net = qp->q.fqdir->net;
421 struct iphdr *iph;
422 void *reasm_data;
423 int len, err;
424 u8 ecn;
425
426 ipq_kill(qp);
427
428 ecn = ip_frag_ecn_table[qp->ecn];
429 if (unlikely(ecn == 0xff)) {
430 err = -EINVAL;
431 goto out_fail;
432 }
433
434 /* Make the one we just received the head. */
435 reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail);
436 if (!reasm_data)
437 goto out_nomem;
438
439 len = ip_hdrlen(skb) + qp->q.len;
440 err = -E2BIG;
441 if (len > 65535)
442 goto out_oversize;
443
444 inet_frag_reasm_finish(&qp->q, skb, reasm_data,
445 ip_frag_coalesce_ok(qp));
446
447 skb->dev = dev;
448 IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
449
450 iph = ip_hdr(skb);
451 iph->tot_len = htons(len);
452 iph->tos |= ecn;
453
454 /* When we set IP_DF on a refragmented skb we must also force a
455 * call to ip_fragment to avoid forwarding a DF-skb of size s while
456 * original sender only sent fragments of size f (where f < s).
457 *
458 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
459 * frag seen to avoid sending tiny DF-fragments in case skb was built
460 * from one very small df-fragment and one large non-df frag.
461 */
462 if (qp->max_df_size == qp->q.max_size) {
463 IPCB(skb)->flags |= IPSKB_FRAG_PMTU;
464 iph->frag_off = htons(IP_DF);
465 } else {
466 iph->frag_off = 0;
467 }
468
469 ip_send_check(iph);
470
471 __IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
472 qp->q.rb_fragments = RB_ROOT;
473 qp->q.fragments_tail = NULL;
474 qp->q.last_run_head = NULL;
475 return 0;
476
477 out_nomem:
478 net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
479 err = -ENOMEM;
480 goto out_fail;
481 out_oversize:
482 net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
483 out_fail:
484 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
485 return err;
486 }
487
488 /* Process an incoming IP datagram fragment. */
ip_defrag(struct net * net,struct sk_buff * skb,u32 user)489 int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
490 {
491 struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
492 int vif = l3mdev_master_ifindex_rcu(dev);
493 struct ipq *qp;
494
495 __IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
496
497 /* Lookup (or create) queue header */
498 qp = ip_find(net, ip_hdr(skb), user, vif);
499 if (qp) {
500 int ret;
501
502 spin_lock(&qp->q.lock);
503
504 ret = ip_frag_queue(qp, skb);
505
506 spin_unlock(&qp->q.lock);
507 ipq_put(qp);
508 return ret;
509 }
510
511 __IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
512 kfree_skb(skb);
513 return -ENOMEM;
514 }
515 EXPORT_SYMBOL(ip_defrag);
516
ip_check_defrag(struct net * net,struct sk_buff * skb,u32 user)517 struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
518 {
519 struct iphdr iph;
520 int netoff;
521 u32 len;
522
523 if (skb->protocol != htons(ETH_P_IP))
524 return skb;
525
526 netoff = skb_network_offset(skb);
527
528 if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
529 return skb;
530
531 if (iph.ihl < 5 || iph.version != 4)
532 return skb;
533
534 len = ntohs(iph.tot_len);
535 if (skb->len < netoff + len || len < (iph.ihl * 4))
536 return skb;
537
538 if (ip_is_fragment(&iph)) {
539 skb = skb_share_check(skb, GFP_ATOMIC);
540 if (skb) {
541 if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
542 kfree_skb(skb);
543 return NULL;
544 }
545 if (pskb_trim_rcsum(skb, netoff + len)) {
546 kfree_skb(skb);
547 return NULL;
548 }
549 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
550 if (ip_defrag(net, skb, user))
551 return NULL;
552 skb_clear_hash(skb);
553 }
554 }
555 return skb;
556 }
557 EXPORT_SYMBOL(ip_check_defrag);
558
559 #ifdef CONFIG_SYSCTL
560 static int dist_min;
561
562 static struct ctl_table ip4_frags_ns_ctl_table[] = {
563 {
564 .procname = "ipfrag_high_thresh",
565 .maxlen = sizeof(unsigned long),
566 .mode = 0644,
567 .proc_handler = proc_doulongvec_minmax,
568 },
569 {
570 .procname = "ipfrag_low_thresh",
571 .maxlen = sizeof(unsigned long),
572 .mode = 0644,
573 .proc_handler = proc_doulongvec_minmax,
574 },
575 {
576 .procname = "ipfrag_time",
577 .maxlen = sizeof(int),
578 .mode = 0644,
579 .proc_handler = proc_dointvec_jiffies,
580 },
581 {
582 .procname = "ipfrag_max_dist",
583 .maxlen = sizeof(int),
584 .mode = 0644,
585 .proc_handler = proc_dointvec_minmax,
586 .extra1 = &dist_min,
587 },
588 { }
589 };
590
591 /* secret interval has been deprecated */
592 static int ip4_frags_secret_interval_unused;
593 static struct ctl_table ip4_frags_ctl_table[] = {
594 {
595 .procname = "ipfrag_secret_interval",
596 .data = &ip4_frags_secret_interval_unused,
597 .maxlen = sizeof(int),
598 .mode = 0644,
599 .proc_handler = proc_dointvec_jiffies,
600 },
601 { }
602 };
603
ip4_frags_ns_ctl_register(struct net * net)604 static int __net_init ip4_frags_ns_ctl_register(struct net *net)
605 {
606 struct ctl_table *table;
607 struct ctl_table_header *hdr;
608
609 table = ip4_frags_ns_ctl_table;
610 if (!net_eq(net, &init_net)) {
611 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
612 if (!table)
613 goto err_alloc;
614
615 }
616 table[0].data = &net->ipv4.fqdir->high_thresh;
617 table[0].extra1 = &net->ipv4.fqdir->low_thresh;
618 table[1].data = &net->ipv4.fqdir->low_thresh;
619 table[1].extra2 = &net->ipv4.fqdir->high_thresh;
620 table[2].data = &net->ipv4.fqdir->timeout;
621 table[3].data = &net->ipv4.fqdir->max_dist;
622
623 hdr = register_net_sysctl_sz(net, "net/ipv4", table,
624 ARRAY_SIZE(ip4_frags_ns_ctl_table));
625 if (!hdr)
626 goto err_reg;
627
628 net->ipv4.frags_hdr = hdr;
629 return 0;
630
631 err_reg:
632 if (!net_eq(net, &init_net))
633 kfree(table);
634 err_alloc:
635 return -ENOMEM;
636 }
637
ip4_frags_ns_ctl_unregister(struct net * net)638 static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
639 {
640 struct ctl_table *table;
641
642 table = net->ipv4.frags_hdr->ctl_table_arg;
643 unregister_net_sysctl_table(net->ipv4.frags_hdr);
644 kfree(table);
645 }
646
ip4_frags_ctl_register(void)647 static void __init ip4_frags_ctl_register(void)
648 {
649 register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
650 }
651 #else
ip4_frags_ns_ctl_register(struct net * net)652 static int ip4_frags_ns_ctl_register(struct net *net)
653 {
654 return 0;
655 }
656
ip4_frags_ns_ctl_unregister(struct net * net)657 static void ip4_frags_ns_ctl_unregister(struct net *net)
658 {
659 }
660
ip4_frags_ctl_register(void)661 static void __init ip4_frags_ctl_register(void)
662 {
663 }
664 #endif
665
ipv4_frags_init_net(struct net * net)666 static int __net_init ipv4_frags_init_net(struct net *net)
667 {
668 int res;
669
670 res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net);
671 if (res < 0)
672 return res;
673 /* Fragment cache limits.
674 *
675 * The fragment memory accounting code, (tries to) account for
676 * the real memory usage, by measuring both the size of frag
677 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
678 * and the SKB's truesize.
679 *
680 * A 64K fragment consumes 129736 bytes (44*2944)+200
681 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
682 *
683 * We will commit 4MB at one time. Should we cross that limit
684 * we will prune down to 3MB, making room for approx 8 big 64K
685 * fragments 8x128k.
686 */
687 net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024;
688 net->ipv4.fqdir->low_thresh = 3 * 1024 * 1024;
689 /*
690 * Important NOTE! Fragment queue must be destroyed before MSL expires.
691 * RFC791 is wrong proposing to prolongate timer each fragment arrival
692 * by TTL.
693 */
694 net->ipv4.fqdir->timeout = IP_FRAG_TIME;
695
696 net->ipv4.fqdir->max_dist = 64;
697
698 res = ip4_frags_ns_ctl_register(net);
699 if (res < 0)
700 fqdir_exit(net->ipv4.fqdir);
701 return res;
702 }
703
ipv4_frags_pre_exit_net(struct net * net)704 static void __net_exit ipv4_frags_pre_exit_net(struct net *net)
705 {
706 fqdir_pre_exit(net->ipv4.fqdir);
707 }
708
ipv4_frags_exit_net(struct net * net)709 static void __net_exit ipv4_frags_exit_net(struct net *net)
710 {
711 ip4_frags_ns_ctl_unregister(net);
712 fqdir_exit(net->ipv4.fqdir);
713 }
714
715 static struct pernet_operations ip4_frags_ops = {
716 .init = ipv4_frags_init_net,
717 .pre_exit = ipv4_frags_pre_exit_net,
718 .exit = ipv4_frags_exit_net,
719 };
720
721
ip4_key_hashfn(const void * data,u32 len,u32 seed)722 static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
723 {
724 return jhash2(data,
725 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
726 }
727
ip4_obj_hashfn(const void * data,u32 len,u32 seed)728 static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
729 {
730 const struct inet_frag_queue *fq = data;
731
732 return jhash2((const u32 *)&fq->key.v4,
733 sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
734 }
735
ip4_obj_cmpfn(struct rhashtable_compare_arg * arg,const void * ptr)736 static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
737 {
738 const struct frag_v4_compare_key *key = arg->key;
739 const struct inet_frag_queue *fq = ptr;
740
741 return !!memcmp(&fq->key, key, sizeof(*key));
742 }
743
744 static const struct rhashtable_params ip4_rhash_params = {
745 .head_offset = offsetof(struct inet_frag_queue, node),
746 .key_offset = offsetof(struct inet_frag_queue, key),
747 .key_len = sizeof(struct frag_v4_compare_key),
748 .hashfn = ip4_key_hashfn,
749 .obj_hashfn = ip4_obj_hashfn,
750 .obj_cmpfn = ip4_obj_cmpfn,
751 .automatic_shrinking = true,
752 };
753
ipfrag_init(void)754 void __init ipfrag_init(void)
755 {
756 ip4_frags.constructor = ip4_frag_init;
757 ip4_frags.destructor = ip4_frag_free;
758 ip4_frags.qsize = sizeof(struct ipq);
759 ip4_frags.frag_expire = ip_expire;
760 ip4_frags.frags_cache_name = ip_frag_cache_name;
761 ip4_frags.rhash_params = ip4_rhash_params;
762 if (inet_frags_init(&ip4_frags))
763 panic("IP: failed to allocate ip4_frags cache\n");
764 ip4_frags_ctl_register();
765 register_pernet_subsys(&ip4_frags_ops);
766 }
767