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