xref: /openbmc/linux/net/ipv4/ip_fragment.c (revision f4877225)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		The IP fragmentation functionality.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
10113aa838SAlan Cox  *		Alan Cox <alan@lxorguk.ukuu.org.uk>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Fixes:
131da177e4SLinus Torvalds  *		Alan Cox	:	Split from ip.c , see ip_input.c for history.
141da177e4SLinus Torvalds  *		David S. Miller :	Begin massive cleanup...
151da177e4SLinus Torvalds  *		Andi Kleen	:	Add sysctls.
161da177e4SLinus Torvalds  *		xxxx		:	Overlapfrag bug.
171da177e4SLinus Torvalds  *		Ultima          :       ip_expire() kernel panic.
181da177e4SLinus Torvalds  *		Bill Hawes	:	Frag accounting and evictor fixes.
191da177e4SLinus Torvalds  *		John McDonald	:	0 length frag bug.
201da177e4SLinus Torvalds  *		Alexey Kuznetsov:	SMP races, threading, cleanup.
211da177e4SLinus Torvalds  *		Patrick McHardy :	LRU queue of frag heads for evictor.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
24afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt
25afd46503SJoe Perches 
2689cee8b1SHerbert Xu #include <linux/compiler.h>
271da177e4SLinus Torvalds #include <linux/module.h>
281da177e4SLinus Torvalds #include <linux/types.h>
291da177e4SLinus Torvalds #include <linux/mm.h>
301da177e4SLinus Torvalds #include <linux/jiffies.h>
311da177e4SLinus Torvalds #include <linux/skbuff.h>
321da177e4SLinus Torvalds #include <linux/list.h>
331da177e4SLinus Torvalds #include <linux/ip.h>
341da177e4SLinus Torvalds #include <linux/icmp.h>
351da177e4SLinus Torvalds #include <linux/netdevice.h>
361da177e4SLinus Torvalds #include <linux/jhash.h>
371da177e4SLinus Torvalds #include <linux/random.h>
385a0e3ad6STejun Heo #include <linux/slab.h>
39e9017b55SShan Wei #include <net/route.h>
40e9017b55SShan Wei #include <net/dst.h>
411da177e4SLinus Torvalds #include <net/sock.h>
421da177e4SLinus Torvalds #include <net/ip.h>
431da177e4SLinus Torvalds #include <net/icmp.h>
441da177e4SLinus Torvalds #include <net/checksum.h>
4589cee8b1SHerbert Xu #include <net/inetpeer.h>
465ab11c98SPavel Emelyanov #include <net/inet_frag.h>
471da177e4SLinus Torvalds #include <linux/tcp.h>
481da177e4SLinus Torvalds #include <linux/udp.h>
491da177e4SLinus Torvalds #include <linux/inet.h>
501da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
516623e3b2SEric Dumazet #include <net/inet_ecn.h>
52385add90SDavid Ahern #include <net/l3mdev.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
551da177e4SLinus Torvalds  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
561da177e4SLinus Torvalds  * as well. Or notify me, at least. --ANK
571da177e4SLinus Torvalds  */
58d4ad4d22SNikolay Aleksandrov static const char ip_frag_cache_name[] = "ip4-frags";
5989cee8b1SHerbert Xu 
601da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */
611da177e4SLinus Torvalds struct ipq {
625ab11c98SPavel Emelyanov 	struct inet_frag_queue q;
635ab11c98SPavel Emelyanov 
646623e3b2SEric Dumazet 	u8		ecn; /* RFC3168 support */
65d6b915e2SFlorian Westphal 	u16		max_df_size; /* largest frag with DF set seen */
6689cee8b1SHerbert Xu 	int             iif;
6789cee8b1SHerbert Xu 	unsigned int    rid;
6889cee8b1SHerbert Xu 	struct inet_peer *peer;
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds 
ip4_frag_ecn(u8 tos)71aa1f731eSFabian Frederick static u8 ip4_frag_ecn(u8 tos)
726623e3b2SEric Dumazet {
735173cc05SEric Dumazet 	return 1 << (tos & INET_ECN_MASK);
746623e3b2SEric Dumazet }
756623e3b2SEric Dumazet 
767eb95156SPavel Emelyanov static struct inet_frags ip4_frags;
771da177e4SLinus Torvalds 
78a4fd284aSPeter Oskolkov static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
79a4fd284aSPeter Oskolkov 			 struct sk_buff *prev_tail, struct net_device *dev);
801706d587SHerbert Xu 
81abd6523dSPavel Emelyanov 
ip4_frag_init(struct inet_frag_queue * q,const void * a)8236c77782SFlorian Westphal static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
83c6fda282SPavel Emelyanov {
84c6fda282SPavel Emelyanov 	struct ipq *qp = container_of(q, struct ipq, q);
85a39aca67SEric Dumazet 	struct net *net = q->fqdir->net;
8654db0cc2SGao feng 
87648700f7SEric Dumazet 	const struct frag_v4_compare_key *key = a;
88c6fda282SPavel Emelyanov 
89648700f7SEric Dumazet 	q->key.v4 = *key;
90648700f7SEric Dumazet 	qp->ecn = 0;
916ce3b4dcSEric Dumazet 	qp->peer = q->fqdir->max_dist ?
92648700f7SEric Dumazet 		inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
93192132b9SDavid Ahern 		NULL;
94c6fda282SPavel Emelyanov }
95c6fda282SPavel Emelyanov 
ip4_frag_free(struct inet_frag_queue * q)96aa1f731eSFabian Frederick static void ip4_frag_free(struct inet_frag_queue *q)
971da177e4SLinus Torvalds {
981e4b8287SPavel Emelyanov 	struct ipq *qp;
991e4b8287SPavel Emelyanov 
1001e4b8287SPavel Emelyanov 	qp = container_of(q, struct ipq, q);
1011e4b8287SPavel Emelyanov 	if (qp->peer)
1021e4b8287SPavel Emelyanov 		inet_putpeer(qp->peer);
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* Destruction primitives. */
1071da177e4SLinus Torvalds 
ipq_put(struct ipq * ipq)108aa1f731eSFabian Frederick static void ipq_put(struct ipq *ipq)
1091da177e4SLinus Torvalds {
110093ba729SEric Dumazet 	inet_frag_put(&ipq->q);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately,
1141da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1151da177e4SLinus Torvalds  */
ipq_kill(struct ipq * ipq)1161da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq)
1171da177e4SLinus Torvalds {
118093ba729SEric Dumazet 	inet_frag_kill(&ipq->q);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
frag_expire_skip_icmp(u32 user)1215cf42280SAndy Zhou static bool frag_expire_skip_icmp(u32 user)
1225cf42280SAndy Zhou {
1235cf42280SAndy Zhou 	return user == IP_DEFRAG_AF_PACKET ||
1245cf42280SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
1258bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_IN_END) ||
1268bc04864SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
1278bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
1285cf42280SAndy Zhou }
1295cf42280SAndy Zhou 
1301da177e4SLinus Torvalds /*
1311da177e4SLinus Torvalds  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
1321da177e4SLinus Torvalds  */
ip_expire(struct timer_list * t)13378802011SKees Cook static void ip_expire(struct timer_list *t)
1341da177e4SLinus Torvalds {
13578802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
136399d1404SEric Dumazet 	const struct iphdr *iph;
137fa0f5273SPeter Oskolkov 	struct sk_buff *head = NULL;
13884a3aa00SPavel Emelyanov 	struct net *net;
139399d1404SEric Dumazet 	struct ipq *qp;
140399d1404SEric Dumazet 	int err;
141e521db9dSPavel Emelyanov 
14278802011SKees Cook 	qp = container_of(frag, struct ipq, q);
143a39aca67SEric Dumazet 	net = qp->q.fqdir->net;
1441da177e4SLinus Torvalds 
145ec4fbd64SEric Dumazet 	rcu_read_lock();
146d5dd8879SEric Dumazet 
14791341fa0SEric Dumazet 	/* Paired with WRITE_ONCE() in fqdir_pre_exit(). */
14891341fa0SEric Dumazet 	if (READ_ONCE(qp->q.fqdir->dead))
149d5dd8879SEric Dumazet 		goto out_rcu_unlock;
150d5dd8879SEric Dumazet 
1515ab11c98SPavel Emelyanov 	spin_lock(&qp->q.lock);
1521da177e4SLinus Torvalds 
15306aa8b8aSNikolay Aleksandrov 	if (qp->q.flags & INET_FRAG_COMPLETE)
1541da177e4SLinus Torvalds 		goto out;
1551da177e4SLinus Torvalds 
15677adfd3aSEric Dumazet 	qp->q.flags |= INET_FRAG_DROP;
1571da177e4SLinus Torvalds 	ipq_kill(qp);
158b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
159b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
1602e404f63SNikolay Aleksandrov 
16170837ffeSDan Carpenter 	if (!(qp->q.flags & INET_FRAG_FIRST_IN))
1622e404f63SNikolay Aleksandrov 		goto out;
1632e404f63SNikolay Aleksandrov 
164fa0f5273SPeter Oskolkov 	/* sk_buff::dev and sk_buff::rbnode are unionized. So we
165fa0f5273SPeter Oskolkov 	 * pull the head out of the tree in order to be able to
166fa0f5273SPeter Oskolkov 	 * deal with head->dev.
167fa0f5273SPeter Oskolkov 	 */
168c23f35d1SPeter Oskolkov 	head = inet_frag_pull_head(&qp->q);
169fa0f5273SPeter Oskolkov 	if (!head)
170fa0f5273SPeter Oskolkov 		goto out;
17169df9d59SEric Dumazet 	head->dev = dev_get_by_index_rcu(net, qp->iif);
172e9017b55SShan Wei 	if (!head->dev)
173ec4fbd64SEric Dumazet 		goto out;
174ec4fbd64SEric Dumazet 
175e9017b55SShan Wei 
17697599dc7SEric Dumazet 	/* skb has no dst, perform route lookup again */
17764f3b9e2SEric Dumazet 	iph = ip_hdr(head);
178c6cffba4SDavid S. Miller 	err = ip_route_input_noref(head, iph->daddr, iph->saddr,
179c10237e0SDavid S. Miller 					   iph->tos, head->dev);
18064f3b9e2SEric Dumazet 	if (err)
181ec4fbd64SEric Dumazet 		goto out;
182e9017b55SShan Wei 
1832e404f63SNikolay Aleksandrov 	/* Only an end host needs to send an ICMP
184e9017b55SShan Wei 	 * "Fragment Reassembly Timeout" message, per RFC792.
185e9017b55SShan Wei 	 */
186648700f7SEric Dumazet 	if (frag_expire_skip_icmp(qp->q.key.v4.user) &&
1875cf42280SAndy Zhou 	    (skb_rtable(head)->rt_type != RTN_LOCAL))
188ec4fbd64SEric Dumazet 		goto out;
189ec4fbd64SEric Dumazet 
190ec4fbd64SEric Dumazet 	spin_unlock(&qp->q.lock);
1911eec5d56SEric Dumazet 	icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
192ec4fbd64SEric Dumazet 	goto out_rcu_unlock;
1931eec5d56SEric Dumazet 
1941da177e4SLinus Torvalds out:
1955ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
196ec4fbd64SEric Dumazet out_rcu_unlock:
197ec4fbd64SEric Dumazet 	rcu_read_unlock();
19877adfd3aSEric Dumazet 	kfree_skb_reason(head, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
1994b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
202abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
203abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
204abd6523dSPavel Emelyanov  */
ip_find(struct net * net,struct iphdr * iph,u32 user,int vif)2059972f134SDavid Ahern static struct ipq *ip_find(struct net *net, struct iphdr *iph,
2069972f134SDavid Ahern 			   u32 user, int vif)
2071da177e4SLinus Torvalds {
208648700f7SEric Dumazet 	struct frag_v4_compare_key key = {
209648700f7SEric Dumazet 		.saddr = iph->saddr,
210648700f7SEric Dumazet 		.daddr = iph->daddr,
211648700f7SEric Dumazet 		.user = user,
212648700f7SEric Dumazet 		.vif = vif,
213648700f7SEric Dumazet 		.id = iph->id,
214648700f7SEric Dumazet 		.protocol = iph->protocol,
215648700f7SEric Dumazet 	};
216c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
2171da177e4SLinus Torvalds 
2184907abc6SEric Dumazet 	q = inet_frag_find(net->ipv4.fqdir, &key);
2192d44ed22SEric Dumazet 	if (!q)
2201da177e4SLinus Torvalds 		return NULL;
2212d44ed22SEric Dumazet 
2225a3da1feSHannes Frederic Sowa 	return container_of(q, struct ipq, q);
2235a3da1feSHannes Frederic Sowa }
2241da177e4SLinus Torvalds 
22589cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
ip_frag_too_far(struct ipq * qp)226aa1f731eSFabian Frederick static int ip_frag_too_far(struct ipq *qp)
22789cee8b1SHerbert Xu {
22889cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
2296ce3b4dcSEric Dumazet 	unsigned int max = qp->q.fqdir->max_dist;
23089cee8b1SHerbert Xu 	unsigned int start, end;
23189cee8b1SHerbert Xu 
23289cee8b1SHerbert Xu 	int rc;
23389cee8b1SHerbert Xu 
23489cee8b1SHerbert Xu 	if (!peer || !max)
23589cee8b1SHerbert Xu 		return 0;
23689cee8b1SHerbert Xu 
23789cee8b1SHerbert Xu 	start = qp->rid;
23889cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
23989cee8b1SHerbert Xu 	qp->rid = end;
24089cee8b1SHerbert Xu 
241fa0f5273SPeter Oskolkov 	rc = qp->q.fragments_tail && (end - start) > max;
24289cee8b1SHerbert Xu 
243a39aca67SEric Dumazet 	if (rc)
244a39aca67SEric Dumazet 		__IP_INC_STATS(qp->q.fqdir->net, IPSTATS_MIB_REASMFAILS);
24589cee8b1SHerbert Xu 
24689cee8b1SHerbert Xu 	return rc;
24789cee8b1SHerbert Xu }
24889cee8b1SHerbert Xu 
ip_frag_reinit(struct ipq * qp)24989cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
25089cee8b1SHerbert Xu {
251d433673eSJesper Dangaard Brouer 	unsigned int sum_truesize = 0;
25289cee8b1SHerbert Xu 
2536ce3b4dcSEric Dumazet 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.fqdir->timeout)) {
254edcb6918SReshetova, Elena 		refcount_inc(&qp->q.refcnt);
25589cee8b1SHerbert Xu 		return -ETIMEDOUT;
25689cee8b1SHerbert Xu 	}
25789cee8b1SHerbert Xu 
25877adfd3aSEric Dumazet 	sum_truesize = inet_frag_rbtree_purge(&qp->q.rb_fragments,
2593bdfb04fSEric Dumazet 					      SKB_DROP_REASON_FRAG_TOO_FAR);
2606ce3b4dcSEric Dumazet 	sub_frag_mem_limit(qp->q.fqdir, sum_truesize);
26189cee8b1SHerbert Xu 
26206aa8b8aSNikolay Aleksandrov 	qp->q.flags = 0;
2635ab11c98SPavel Emelyanov 	qp->q.len = 0;
2645ab11c98SPavel Emelyanov 	qp->q.meat = 0;
265fa0f5273SPeter Oskolkov 	qp->q.rb_fragments = RB_ROOT;
266d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
267a4fd284aSPeter Oskolkov 	qp->q.last_run_head = NULL;
26889cee8b1SHerbert Xu 	qp->iif = 0;
2696623e3b2SEric Dumazet 	qp->ecn = 0;
27089cee8b1SHerbert Xu 
27189cee8b1SHerbert Xu 	return 0;
27289cee8b1SHerbert Xu }
27389cee8b1SHerbert Xu 
2741da177e4SLinus Torvalds /* Add new segment to existing queue. */
ip_frag_queue(struct ipq * qp,struct sk_buff * skb)2751706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
2761da177e4SLinus Torvalds {
277a39aca67SEric Dumazet 	struct net *net = qp->q.fqdir->net;
278c23f35d1SPeter Oskolkov 	int ihl, end, flags, offset;
279c23f35d1SPeter Oskolkov 	struct sk_buff *prev_tail;
2801706d587SHerbert Xu 	struct net_device *dev;
281d6b915e2SFlorian Westphal 	unsigned int fragsize;
2821706d587SHerbert Xu 	int err = -ENOENT;
2834ecbb1c2SEric Dumazet 	SKB_DR(reason);
2846623e3b2SEric Dumazet 	u8 ecn;
2851da177e4SLinus Torvalds 
2864ecbb1c2SEric Dumazet 	/* If reassembly is already done, @skb must be a duplicate frag. */
2874ecbb1c2SEric Dumazet 	if (qp->q.flags & INET_FRAG_COMPLETE) {
2884ecbb1c2SEric Dumazet 		SKB_DR_SET(reason, DUP_FRAG);
2891da177e4SLinus Torvalds 		goto err;
2904ecbb1c2SEric Dumazet 	}
2911da177e4SLinus Torvalds 
29289cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
2931706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
2941706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
29589cee8b1SHerbert Xu 		ipq_kill(qp);
29689cee8b1SHerbert Xu 		goto err;
29789cee8b1SHerbert Xu 	}
29889cee8b1SHerbert Xu 
2996623e3b2SEric Dumazet 	ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
300eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3011da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3021da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3031da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
304c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3070848f642SEdward Hyunkoo Jee 	end = offset + skb->len - skb_network_offset(skb) - ihl;
3081706d587SHerbert Xu 	err = -EINVAL;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	/* Is this the final fragment? */
3111da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3121da177e4SLinus Torvalds 		/* If we already have some bits beyond end
31342b2aa86SJustin P. Mattock 		 * or have different end, the segment is corrupted.
3141da177e4SLinus Torvalds 		 */
3155ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
31606aa8b8aSNikolay Aleksandrov 		    ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
3170ff89efbSPeter Oskolkov 			goto discard_qp;
31806aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_LAST_IN;
3195ab11c98SPavel Emelyanov 		qp->q.len = end;
3201da177e4SLinus Torvalds 	} else {
3211da177e4SLinus Torvalds 		if (end&7) {
3221da177e4SLinus Torvalds 			end &= ~7;
3231da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3241da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3251da177e4SLinus Torvalds 		}
3265ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3271da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
32806aa8b8aSNikolay Aleksandrov 			if (qp->q.flags & INET_FRAG_LAST_IN)
3290ff89efbSPeter Oskolkov 				goto discard_qp;
3305ab11c98SPavel Emelyanov 			qp->q.len = end;
3311da177e4SLinus Torvalds 		}
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	if (end == offset)
3340ff89efbSPeter Oskolkov 		goto discard_qp;
3351da177e4SLinus Torvalds 
3361706d587SHerbert Xu 	err = -ENOMEM;
3370848f642SEdward Hyunkoo Jee 	if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
3380ff89efbSPeter Oskolkov 		goto discard_qp;
3391706d587SHerbert Xu 
3401706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3411706d587SHerbert Xu 	if (err)
3420ff89efbSPeter Oskolkov 		goto discard_qp;
3431da177e4SLinus Torvalds 
344fa0f5273SPeter Oskolkov 	/* Note : skb->rbnode and skb->dev share the same location. */
345fa0f5273SPeter Oskolkov 	dev = skb->dev;
346fa0f5273SPeter Oskolkov 	/* Makes sure compiler wont do silly aliasing games */
347fa0f5273SPeter Oskolkov 	barrier();
3481da177e4SLinus Torvalds 
349a4fd284aSPeter Oskolkov 	prev_tail = qp->q.fragments_tail;
350c23f35d1SPeter Oskolkov 	err = inet_frag_queue_insert(&qp->q, skb, offset, end);
351c23f35d1SPeter Oskolkov 	if (err)
352c23f35d1SPeter Oskolkov 		goto insert_error;
3531da177e4SLinus Torvalds 
354bf663371SEric Dumazet 	if (dev)
355bf663371SEric Dumazet 		qp->iif = dev->ifindex;
3561da177e4SLinus Torvalds 
3575ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
3588672406eSMartin KaFai Lau 	qp->q.mono_delivery_time = skb->mono_delivery_time;
3595ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
3606623e3b2SEric Dumazet 	qp->ecn |= ecn;
3616ce3b4dcSEric Dumazet 	add_frag_mem_limit(qp->q.fqdir, skb->truesize);
3621da177e4SLinus Torvalds 	if (offset == 0)
36306aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_FIRST_IN;
3641da177e4SLinus Torvalds 
365d6b915e2SFlorian Westphal 	fragsize = skb->len + ihl;
366d6b915e2SFlorian Westphal 
367d6b915e2SFlorian Westphal 	if (fragsize > qp->q.max_size)
368d6b915e2SFlorian Westphal 		qp->q.max_size = fragsize;
369d6b915e2SFlorian Westphal 
3705f2d04f1SPatrick McHardy 	if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
371d6b915e2SFlorian Westphal 	    fragsize > qp->max_df_size)
372d6b915e2SFlorian Westphal 		qp->max_df_size = fragsize;
3735f2d04f1SPatrick McHardy 
37406aa8b8aSNikolay Aleksandrov 	if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
37597599dc7SEric Dumazet 	    qp->q.meat == qp->q.len) {
37697599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
3771706d587SHerbert Xu 
37897599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
379a4fd284aSPeter Oskolkov 		err = ip_frag_reasm(qp, skb, prev_tail, dev);
38097599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
3810ff89efbSPeter Oskolkov 		if (err)
3820ff89efbSPeter Oskolkov 			inet_frag_kill(&qp->q);
38397599dc7SEric Dumazet 		return err;
38497599dc7SEric Dumazet 	}
38597599dc7SEric Dumazet 
38697599dc7SEric Dumazet 	skb_dst_drop(skb);
387*f4877225SFlorian Westphal 	skb_orphan(skb);
3881706d587SHerbert Xu 	return -EINPROGRESS;
3891da177e4SLinus Torvalds 
390c23f35d1SPeter Oskolkov insert_error:
391c23f35d1SPeter Oskolkov 	if (err == IPFRAG_DUP) {
3924ecbb1c2SEric Dumazet 		SKB_DR_SET(reason, DUP_FRAG);
3934ecbb1c2SEric Dumazet 		err = -EINVAL;
3944ecbb1c2SEric Dumazet 		goto err;
395c23f35d1SPeter Oskolkov 	}
396c23f35d1SPeter Oskolkov 	err = -EINVAL;
3970ff89efbSPeter Oskolkov 	__IP_INC_STATS(net, IPSTATS_MIB_REASM_OVERLAPS);
3987969e5c4SPeter Oskolkov discard_qp:
3997969e5c4SPeter Oskolkov 	inet_frag_kill(&qp->q);
400c23f35d1SPeter Oskolkov 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
4011da177e4SLinus Torvalds err:
4024ecbb1c2SEric Dumazet 	kfree_skb_reason(skb, reason);
4031706d587SHerbert Xu 	return err;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
ip_frag_coalesce_ok(const struct ipq * qp)406891584f4SGuillaume Nault static bool ip_frag_coalesce_ok(const struct ipq *qp)
407891584f4SGuillaume Nault {
408891584f4SGuillaume Nault 	return qp->q.key.v4.user == IP_DEFRAG_LOCAL_DELIVER;
409891584f4SGuillaume Nault }
410891584f4SGuillaume Nault 
4111da177e4SLinus Torvalds /* 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)412fa0f5273SPeter Oskolkov static int ip_frag_reasm(struct ipq *qp, struct sk_buff *skb,
413a4fd284aSPeter Oskolkov 			 struct sk_buff *prev_tail, struct net_device *dev)
4141da177e4SLinus Torvalds {
415a39aca67SEric Dumazet 	struct net *net = qp->q.fqdir->net;
4161da177e4SLinus Torvalds 	struct iphdr *iph;
417c23f35d1SPeter Oskolkov 	void *reasm_data;
418c23f35d1SPeter Oskolkov 	int len, err;
4195173cc05SEric Dumazet 	u8 ecn;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	ipq_kill(qp);
4221da177e4SLinus Torvalds 
423be991971SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[qp->ecn];
4245173cc05SEric Dumazet 	if (unlikely(ecn == 0xff)) {
4255173cc05SEric Dumazet 		err = -EINVAL;
4265173cc05SEric Dumazet 		goto out_fail;
4275173cc05SEric Dumazet 	}
428c23f35d1SPeter Oskolkov 
4291706d587SHerbert Xu 	/* Make the one we just received the head. */
430c23f35d1SPeter Oskolkov 	reasm_data = inet_frag_reasm_prepare(&qp->q, skb, prev_tail);
431c23f35d1SPeter Oskolkov 	if (!reasm_data)
4321706d587SHerbert Xu 		goto out_nomem;
4331706d587SHerbert Xu 
434c23f35d1SPeter Oskolkov 	len = ip_hdrlen(skb) + qp->q.len;
4351706d587SHerbert Xu 	err = -E2BIG;
4361da177e4SLinus Torvalds 	if (len > 65535)
4371da177e4SLinus Torvalds 		goto out_oversize;
4381da177e4SLinus Torvalds 
439891584f4SGuillaume Nault 	inet_frag_reasm_finish(&qp->q, skb, reasm_data,
440891584f4SGuillaume Nault 			       ip_frag_coalesce_ok(qp));
441ebaf39e6SJiri Wiesner 
442c23f35d1SPeter Oskolkov 	skb->dev = dev;
443c23f35d1SPeter Oskolkov 	IPCB(skb)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
4441da177e4SLinus Torvalds 
445c23f35d1SPeter Oskolkov 	iph = ip_hdr(skb);
4461da177e4SLinus Torvalds 	iph->tot_len = htons(len);
4475173cc05SEric Dumazet 	iph->tos |= ecn;
448d6b915e2SFlorian Westphal 
449d6b915e2SFlorian Westphal 	/* When we set IP_DF on a refragmented skb we must also force a
450d6b915e2SFlorian Westphal 	 * call to ip_fragment to avoid forwarding a DF-skb of size s while
451d6b915e2SFlorian Westphal 	 * original sender only sent fragments of size f (where f < s).
452d6b915e2SFlorian Westphal 	 *
453d6b915e2SFlorian Westphal 	 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
454d6b915e2SFlorian Westphal 	 * frag seen to avoid sending tiny DF-fragments in case skb was built
455d6b915e2SFlorian Westphal 	 * from one very small df-fragment and one large non-df frag.
456d6b915e2SFlorian Westphal 	 */
457d6b915e2SFlorian Westphal 	if (qp->max_df_size == qp->q.max_size) {
458c23f35d1SPeter Oskolkov 		IPCB(skb)->flags |= IPSKB_FRAG_PMTU;
459d6b915e2SFlorian Westphal 		iph->frag_off = htons(IP_DF);
460d6b915e2SFlorian Westphal 	} else {
461d6b915e2SFlorian Westphal 		iph->frag_off = 0;
462d6b915e2SFlorian Westphal 	}
463d6b915e2SFlorian Westphal 
4640848f642SEdward Hyunkoo Jee 	ip_send_check(iph);
4650848f642SEdward Hyunkoo Jee 
466b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
467fa0f5273SPeter Oskolkov 	qp->q.rb_fragments = RB_ROOT;
468d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
469a4fd284aSPeter Oskolkov 	qp->q.last_run_head = NULL;
4701706d587SHerbert Xu 	return 0;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds out_nomem:
473ba7a46f1SJoe Perches 	net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
47445542479SDavid Howells 	err = -ENOMEM;
4751da177e4SLinus Torvalds 	goto out_fail;
4761da177e4SLinus Torvalds out_oversize:
477648700f7SEric Dumazet 	net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
4781da177e4SLinus Torvalds out_fail:
479b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
4801706d587SHerbert Xu 	return err;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
ip_defrag(struct net * net,struct sk_buff * skb,u32 user)48419bcf9f2SEric W. Biederman int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
4851da177e4SLinus Torvalds {
4869972f134SDavid Ahern 	struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
487385add90SDavid Ahern 	int vif = l3mdev_master_ifindex_rcu(dev);
4881da177e4SLinus Torvalds 	struct ipq *qp;
4891da177e4SLinus Torvalds 
490b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
4939972f134SDavid Ahern 	qp = ip_find(net, ip_hdr(skb), user, vif);
49400db4124SIan Morris 	if (qp) {
4951706d587SHerbert Xu 		int ret;
4961da177e4SLinus Torvalds 
4975ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
4981da177e4SLinus Torvalds 
4991706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
5001da177e4SLinus Torvalds 
5015ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
5024b6cb5d8SPavel Emelyanov 		ipq_put(qp);
503776c729eSHerbert Xu 		return ret;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
506b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
5071da177e4SLinus Torvalds 	kfree_skb(skb);
508776c729eSHerbert Xu 	return -ENOMEM;
5091da177e4SLinus Torvalds }
5104bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_defrag);
5111da177e4SLinus Torvalds 
ip_check_defrag(struct net * net,struct sk_buff * skb,u32 user)51219bcf9f2SEric W. Biederman struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
513bc416d97SEric Dumazet {
5141bf3751eSJohannes Berg 	struct iphdr iph;
5153e32e733SAlexander Drozdov 	int netoff;
516bc416d97SEric Dumazet 	u32 len;
517bc416d97SEric Dumazet 
518bc416d97SEric Dumazet 	if (skb->protocol != htons(ETH_P_IP))
519bc416d97SEric Dumazet 		return skb;
520bc416d97SEric Dumazet 
5213e32e733SAlexander Drozdov 	netoff = skb_network_offset(skb);
5223e32e733SAlexander Drozdov 
5233e32e733SAlexander Drozdov 	if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
524bc416d97SEric Dumazet 		return skb;
525bc416d97SEric Dumazet 
5261bf3751eSJohannes Berg 	if (iph.ihl < 5 || iph.version != 4)
527bc416d97SEric Dumazet 		return skb;
528bc416d97SEric Dumazet 
5291bf3751eSJohannes Berg 	len = ntohs(iph.tot_len);
5303e32e733SAlexander Drozdov 	if (skb->len < netoff + len || len < (iph.ihl * 4))
5311bf3751eSJohannes Berg 		return skb;
5321bf3751eSJohannes Berg 
5331bf3751eSJohannes Berg 	if (ip_is_fragment(&iph)) {
534bc416d97SEric Dumazet 		skb = skb_share_check(skb, GFP_ATOMIC);
535bc416d97SEric Dumazet 		if (skb) {
5367de414a9SCong Wang 			if (!pskb_may_pull(skb, netoff + iph.ihl * 4)) {
5377de414a9SCong Wang 				kfree_skb(skb);
5387de414a9SCong Wang 				return NULL;
5397de414a9SCong Wang 			}
5407de414a9SCong Wang 			if (pskb_trim_rcsum(skb, netoff + len)) {
5417de414a9SCong Wang 				kfree_skb(skb);
5427de414a9SCong Wang 				return NULL;
5437de414a9SCong Wang 			}
544bc416d97SEric Dumazet 			memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
54519bcf9f2SEric W. Biederman 			if (ip_defrag(net, skb, user))
546bc416d97SEric Dumazet 				return NULL;
5477539fadcSTom Herbert 			skb_clear_hash(skb);
548bc416d97SEric Dumazet 		}
549bc416d97SEric Dumazet 	}
550bc416d97SEric Dumazet 	return skb;
551bc416d97SEric Dumazet }
552bc416d97SEric Dumazet EXPORT_SYMBOL(ip_check_defrag);
553bc416d97SEric Dumazet 
5548d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5553d234012SEric Dumazet static int dist_min;
5568d8354d2SPavel Emelyanov 
5570a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = {
5588d8354d2SPavel Emelyanov 	{
5598d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
5603e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
5618d8354d2SPavel Emelyanov 		.mode		= 0644,
5623e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
5638d8354d2SPavel Emelyanov 	},
5648d8354d2SPavel Emelyanov 	{
5658d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
5663e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
5678d8354d2SPavel Emelyanov 		.mode		= 0644,
5683e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
5698d8354d2SPavel Emelyanov 	},
5708d8354d2SPavel Emelyanov 	{
5718d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
5728d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5738d8354d2SPavel Emelyanov 		.mode		= 0644,
5746d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5758d8354d2SPavel Emelyanov 	},
5760fbf4cb2SNikolay Borisov 	{
5770fbf4cb2SNikolay Borisov 		.procname	= "ipfrag_max_dist",
5780fbf4cb2SNikolay Borisov 		.maxlen		= sizeof(int),
5790fbf4cb2SNikolay Borisov 		.mode		= 0644,
5800fbf4cb2SNikolay Borisov 		.proc_handler	= proc_dointvec_minmax,
5813d234012SEric Dumazet 		.extra1		= &dist_min,
5820fbf4cb2SNikolay Borisov 	},
5837d291ebbSPavel Emelyanov 	{ }
5847d291ebbSPavel Emelyanov };
5857d291ebbSPavel Emelyanov 
586e3a57d18SFlorian Westphal /* secret interval has been deprecated */
587e3a57d18SFlorian Westphal static int ip4_frags_secret_interval_unused;
5887d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
5898d8354d2SPavel Emelyanov 	{
5908d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
591e3a57d18SFlorian Westphal 		.data		= &ip4_frags_secret_interval_unused,
5928d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5938d8354d2SPavel Emelyanov 		.mode		= 0644,
5946d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5958d8354d2SPavel Emelyanov 	},
5968d8354d2SPavel Emelyanov 	{ }
5978d8354d2SPavel Emelyanov };
5988d8354d2SPavel Emelyanov 
ip4_frags_ns_ctl_register(struct net * net)5992c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net)
6008d8354d2SPavel Emelyanov {
601e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6028d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6038d8354d2SPavel Emelyanov 
6040a64b4b8SPavel Emelyanov 	table = ip4_frags_ns_ctl_table;
60509ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6060a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
60751456b29SIan Morris 		if (!table)
608e4a2d5c2SPavel Emelyanov 			goto err_alloc;
609e4a2d5c2SPavel Emelyanov 
6108dfdb313SEric Dumazet 	}
6114907abc6SEric Dumazet 	table[0].data	= &net->ipv4.fqdir->high_thresh;
6124907abc6SEric Dumazet 	table[0].extra1	= &net->ipv4.fqdir->low_thresh;
6134907abc6SEric Dumazet 	table[1].data	= &net->ipv4.fqdir->low_thresh;
6144907abc6SEric Dumazet 	table[1].extra2	= &net->ipv4.fqdir->high_thresh;
6154907abc6SEric Dumazet 	table[2].data	= &net->ipv4.fqdir->timeout;
6164907abc6SEric Dumazet 	table[3].data	= &net->ipv4.fqdir->max_dist;
617e4a2d5c2SPavel Emelyanov 
618c899710fSJoel Granados 	hdr = register_net_sysctl_sz(net, "net/ipv4", table,
619c899710fSJoel Granados 				     ARRAY_SIZE(ip4_frags_ns_ctl_table));
62051456b29SIan Morris 	if (!hdr)
621e4a2d5c2SPavel Emelyanov 		goto err_reg;
622e4a2d5c2SPavel Emelyanov 
623e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
624e4a2d5c2SPavel Emelyanov 	return 0;
625e4a2d5c2SPavel Emelyanov 
626e4a2d5c2SPavel Emelyanov err_reg:
62709ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
628e4a2d5c2SPavel Emelyanov 		kfree(table);
629e4a2d5c2SPavel Emelyanov err_alloc:
630e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
631e4a2d5c2SPavel Emelyanov }
632e4a2d5c2SPavel Emelyanov 
ip4_frags_ns_ctl_unregister(struct net * net)6332c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
634e4a2d5c2SPavel Emelyanov {
635e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
636e4a2d5c2SPavel Emelyanov 
637e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
638e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
639e4a2d5c2SPavel Emelyanov 	kfree(table);
6408d8354d2SPavel Emelyanov }
6417d291ebbSPavel Emelyanov 
ip4_frags_ctl_register(void)64257a02c39SFabian Frederick static void __init ip4_frags_ctl_register(void)
6437d291ebbSPavel Emelyanov {
64443444757SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
6457d291ebbSPavel Emelyanov }
6468d8354d2SPavel Emelyanov #else
ip4_frags_ns_ctl_register(struct net * net)647aa1f731eSFabian Frederick static int ip4_frags_ns_ctl_register(struct net *net)
6488d8354d2SPavel Emelyanov {
6498d8354d2SPavel Emelyanov 	return 0;
6508d8354d2SPavel Emelyanov }
651e4a2d5c2SPavel Emelyanov 
ip4_frags_ns_ctl_unregister(struct net * net)652aa1f731eSFabian Frederick static void ip4_frags_ns_ctl_unregister(struct net *net)
653e4a2d5c2SPavel Emelyanov {
654e4a2d5c2SPavel Emelyanov }
6557d291ebbSPavel Emelyanov 
ip4_frags_ctl_register(void)656aa1f731eSFabian Frederick static void __init ip4_frags_ctl_register(void)
6577d291ebbSPavel Emelyanov {
6587d291ebbSPavel Emelyanov }
6598d8354d2SPavel Emelyanov #endif
6608d8354d2SPavel Emelyanov 
ipv4_frags_init_net(struct net * net)6612c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net)
6628d8354d2SPavel Emelyanov {
663787bea77SEric Dumazet 	int res;
664787bea77SEric Dumazet 
6654907abc6SEric Dumazet 	res = fqdir_init(&net->ipv4.fqdir, &ip4_frags, net);
6664907abc6SEric Dumazet 	if (res < 0)
6674907abc6SEric Dumazet 		return res;
668c2a93660SJesper Dangaard Brouer 	/* Fragment cache limits.
669c2a93660SJesper Dangaard Brouer 	 *
670c2a93660SJesper Dangaard Brouer 	 * The fragment memory accounting code, (tries to) account for
671c2a93660SJesper Dangaard Brouer 	 * the real memory usage, by measuring both the size of frag
672c2a93660SJesper Dangaard Brouer 	 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
673c2a93660SJesper Dangaard Brouer 	 * and the SKB's truesize.
674c2a93660SJesper Dangaard Brouer 	 *
675c2a93660SJesper Dangaard Brouer 	 * A 64K fragment consumes 129736 bytes (44*2944)+200
676c2a93660SJesper Dangaard Brouer 	 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
677c2a93660SJesper Dangaard Brouer 	 *
678c2a93660SJesper Dangaard Brouer 	 * We will commit 4MB at one time. Should we cross that limit
679c2a93660SJesper Dangaard Brouer 	 * we will prune down to 3MB, making room for approx 8 big 64K
680c2a93660SJesper Dangaard Brouer 	 * fragments 8x128k.
681e31e0bdcSPavel Emelyanov 	 */
6824907abc6SEric Dumazet 	net->ipv4.fqdir->high_thresh = 4 * 1024 * 1024;
6834907abc6SEric Dumazet 	net->ipv4.fqdir->low_thresh  = 3 * 1024 * 1024;
684e31e0bdcSPavel Emelyanov 	/*
685b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
686b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
687b2fd5321SPavel Emelyanov 	 * by TTL.
688b2fd5321SPavel Emelyanov 	 */
6894907abc6SEric Dumazet 	net->ipv4.fqdir->timeout = IP_FRAG_TIME;
690b2fd5321SPavel Emelyanov 
6914907abc6SEric Dumazet 	net->ipv4.fqdir->max_dist = 64;
6920fbf4cb2SNikolay Borisov 
693787bea77SEric Dumazet 	res = ip4_frags_ns_ctl_register(net);
694787bea77SEric Dumazet 	if (res < 0)
6954907abc6SEric Dumazet 		fqdir_exit(net->ipv4.fqdir);
696787bea77SEric Dumazet 	return res;
6978d8354d2SPavel Emelyanov }
6988d8354d2SPavel Emelyanov 
ipv4_frags_pre_exit_net(struct net * net)699d5dd8879SEric Dumazet static void __net_exit ipv4_frags_pre_exit_net(struct net *net)
700d5dd8879SEric Dumazet {
701d5dd8879SEric Dumazet 	fqdir_pre_exit(net->ipv4.fqdir);
702d5dd8879SEric Dumazet }
703d5dd8879SEric Dumazet 
ipv4_frags_exit_net(struct net * net)7042c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net)
70581566e83SPavel Emelyanov {
7060a64b4b8SPavel Emelyanov 	ip4_frags_ns_ctl_unregister(net);
7074907abc6SEric Dumazet 	fqdir_exit(net->ipv4.fqdir);
70881566e83SPavel Emelyanov }
70981566e83SPavel Emelyanov 
71081566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
71181566e83SPavel Emelyanov 	.init		= ipv4_frags_init_net,
712d5dd8879SEric Dumazet 	.pre_exit	= ipv4_frags_pre_exit_net,
71381566e83SPavel Emelyanov 	.exit		= ipv4_frags_exit_net,
71481566e83SPavel Emelyanov };
71581566e83SPavel Emelyanov 
716648700f7SEric Dumazet 
ip4_key_hashfn(const void * data,u32 len,u32 seed)717648700f7SEric Dumazet static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
718648700f7SEric Dumazet {
719648700f7SEric Dumazet 	return jhash2(data,
720648700f7SEric Dumazet 		      sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
721648700f7SEric Dumazet }
722648700f7SEric Dumazet 
ip4_obj_hashfn(const void * data,u32 len,u32 seed)723648700f7SEric Dumazet static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
724648700f7SEric Dumazet {
725648700f7SEric Dumazet 	const struct inet_frag_queue *fq = data;
726648700f7SEric Dumazet 
727648700f7SEric Dumazet 	return jhash2((const u32 *)&fq->key.v4,
728648700f7SEric Dumazet 		      sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
729648700f7SEric Dumazet }
730648700f7SEric Dumazet 
ip4_obj_cmpfn(struct rhashtable_compare_arg * arg,const void * ptr)731648700f7SEric Dumazet static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
732648700f7SEric Dumazet {
733648700f7SEric Dumazet 	const struct frag_v4_compare_key *key = arg->key;
734648700f7SEric Dumazet 	const struct inet_frag_queue *fq = ptr;
735648700f7SEric Dumazet 
736648700f7SEric Dumazet 	return !!memcmp(&fq->key, key, sizeof(*key));
737648700f7SEric Dumazet }
738648700f7SEric Dumazet 
739648700f7SEric Dumazet static const struct rhashtable_params ip4_rhash_params = {
740648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
741648700f7SEric Dumazet 	.key_offset		= offsetof(struct inet_frag_queue, key),
742648700f7SEric Dumazet 	.key_len		= sizeof(struct frag_v4_compare_key),
743648700f7SEric Dumazet 	.hashfn			= ip4_key_hashfn,
744648700f7SEric Dumazet 	.obj_hashfn		= ip4_obj_hashfn,
745648700f7SEric Dumazet 	.obj_cmpfn		= ip4_obj_cmpfn,
746648700f7SEric Dumazet 	.automatic_shrinking	= true,
747648700f7SEric Dumazet };
748648700f7SEric Dumazet 
ipfrag_init(void)749b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
7501da177e4SLinus Torvalds {
751c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
7521e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
7531e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
754e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
755d4ad4d22SNikolay Aleksandrov 	ip4_frags.frags_cache_name = ip_frag_cache_name;
756648700f7SEric Dumazet 	ip4_frags.rhash_params = ip4_rhash_params;
757d4ad4d22SNikolay Aleksandrov 	if (inet_frags_init(&ip4_frags))
758d4ad4d22SNikolay Aleksandrov 		panic("IP: failed to allocate ip4_frags cache\n");
759483a6e4fSEric Dumazet 	ip4_frags_ctl_register();
760483a6e4fSEric Dumazet 	register_pernet_subsys(&ip4_frags_ops);
7611da177e4SLinus Torvalds }
762