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