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