xref: /linux/net/ipv4/ip_fragment.c (revision 3e67f106f619dcfaf6f4e2039599bdb69848c714)
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 struct ipfrag_skb_cb
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	struct inet_skb_parm	h;
631da177e4SLinus Torvalds 	int			offset;
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define FRAG_CB(skb)	((struct ipfrag_skb_cb *)((skb)->cb))
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */
691da177e4SLinus Torvalds struct ipq {
705ab11c98SPavel Emelyanov 	struct inet_frag_queue q;
715ab11c98SPavel Emelyanov 
726623e3b2SEric Dumazet 	u8		ecn; /* RFC3168 support */
73d6b915e2SFlorian Westphal 	u16		max_df_size; /* largest frag with DF set seen */
7489cee8b1SHerbert Xu 	int             iif;
7589cee8b1SHerbert Xu 	unsigned int    rid;
7689cee8b1SHerbert Xu 	struct inet_peer *peer;
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
79aa1f731eSFabian Frederick static u8 ip4_frag_ecn(u8 tos)
806623e3b2SEric Dumazet {
815173cc05SEric Dumazet 	return 1 << (tos & INET_ECN_MASK);
826623e3b2SEric Dumazet }
836623e3b2SEric Dumazet 
847eb95156SPavel Emelyanov static struct inet_frags ip4_frags;
851da177e4SLinus Torvalds 
861706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
871706d587SHerbert Xu 			 struct net_device *dev);
881706d587SHerbert Xu 
89abd6523dSPavel Emelyanov 
9036c77782SFlorian Westphal static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
91c6fda282SPavel Emelyanov {
92c6fda282SPavel Emelyanov 	struct ipq *qp = container_of(q, struct ipq, q);
9354db0cc2SGao feng 	struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
9454db0cc2SGao feng 					       frags);
9554db0cc2SGao feng 	struct net *net = container_of(ipv4, struct net, ipv4);
9654db0cc2SGao feng 
97648700f7SEric Dumazet 	const struct frag_v4_compare_key *key = a;
98c6fda282SPavel Emelyanov 
99648700f7SEric Dumazet 	q->key.v4 = *key;
100648700f7SEric Dumazet 	qp->ecn = 0;
1010fbf4cb2SNikolay Borisov 	qp->peer = q->net->max_dist ?
102648700f7SEric Dumazet 		inet_getpeer_v4(net->ipv4.peers, key->saddr, key->vif, 1) :
103192132b9SDavid Ahern 		NULL;
104c6fda282SPavel Emelyanov }
105c6fda282SPavel Emelyanov 
106aa1f731eSFabian Frederick static void ip4_frag_free(struct inet_frag_queue *q)
1071da177e4SLinus Torvalds {
1081e4b8287SPavel Emelyanov 	struct ipq *qp;
1091e4b8287SPavel Emelyanov 
1101e4b8287SPavel Emelyanov 	qp = container_of(q, struct ipq, q);
1111e4b8287SPavel Emelyanov 	if (qp->peer)
1121e4b8287SPavel Emelyanov 		inet_putpeer(qp->peer);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /* Destruction primitives. */
1171da177e4SLinus Torvalds 
118aa1f731eSFabian Frederick static void ipq_put(struct ipq *ipq)
1191da177e4SLinus Torvalds {
120093ba729SEric Dumazet 	inet_frag_put(&ipq->q);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately,
1241da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq)
1271da177e4SLinus Torvalds {
128093ba729SEric Dumazet 	inet_frag_kill(&ipq->q);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1315cf42280SAndy Zhou static bool frag_expire_skip_icmp(u32 user)
1325cf42280SAndy Zhou {
1335cf42280SAndy Zhou 	return user == IP_DEFRAG_AF_PACKET ||
1345cf42280SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
1358bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_IN_END) ||
1368bc04864SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
1378bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
1385cf42280SAndy Zhou }
1395cf42280SAndy Zhou 
1401da177e4SLinus Torvalds /*
1411da177e4SLinus Torvalds  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
1421da177e4SLinus Torvalds  */
14378802011SKees Cook static void ip_expire(struct timer_list *t)
1441da177e4SLinus Torvalds {
14578802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
146399d1404SEric Dumazet 	struct sk_buff *clone, *head;
147399d1404SEric Dumazet 	const struct iphdr *iph;
14884a3aa00SPavel Emelyanov 	struct net *net;
149399d1404SEric Dumazet 	struct ipq *qp;
150399d1404SEric Dumazet 	int err;
151e521db9dSPavel Emelyanov 
15278802011SKees Cook 	qp = container_of(frag, struct ipq, q);
15384a3aa00SPavel Emelyanov 	net = container_of(qp->q.net, struct net, ipv4.frags);
1541da177e4SLinus Torvalds 
155ec4fbd64SEric Dumazet 	rcu_read_lock();
1565ab11c98SPavel Emelyanov 	spin_lock(&qp->q.lock);
1571da177e4SLinus Torvalds 
15806aa8b8aSNikolay Aleksandrov 	if (qp->q.flags & INET_FRAG_COMPLETE)
1591da177e4SLinus Torvalds 		goto out;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	ipq_kill(qp);
162b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
1631da177e4SLinus Torvalds 
164399d1404SEric Dumazet 	head = qp->q.fragments;
165cb84663eSDenis V. Lunev 
166b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
1672e404f63SNikolay Aleksandrov 
168399d1404SEric Dumazet 	if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !head)
1692e404f63SNikolay Aleksandrov 		goto out;
1702e404f63SNikolay Aleksandrov 
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 	clone = skb_clone(head, GFP_ATOMIC);
191e9017b55SShan Wei 
192e9017b55SShan Wei 	/* Send an ICMP "Fragment Reassembly Timeout" message. */
193ec4fbd64SEric Dumazet 	if (clone) {
194ec4fbd64SEric Dumazet 		spin_unlock(&qp->q.lock);
195ec4fbd64SEric Dumazet 		icmp_send(clone, ICMP_TIME_EXCEEDED,
196ec4fbd64SEric Dumazet 			  ICMP_EXC_FRAGTIME, 0);
197ec4fbd64SEric Dumazet 		consume_skb(clone);
198ec4fbd64SEric Dumazet 		goto out_rcu_unlock;
199ec4fbd64SEric Dumazet 	}
2001da177e4SLinus Torvalds out:
2015ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
202ec4fbd64SEric Dumazet out_rcu_unlock:
203ec4fbd64SEric Dumazet 	rcu_read_unlock();
2044b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
208abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
209abd6523dSPavel Emelyanov  */
2109972f134SDavid Ahern static struct ipq *ip_find(struct net *net, struct iphdr *iph,
2119972f134SDavid Ahern 			   u32 user, int vif)
2121da177e4SLinus Torvalds {
213648700f7SEric Dumazet 	struct frag_v4_compare_key key = {
214648700f7SEric Dumazet 		.saddr = iph->saddr,
215648700f7SEric Dumazet 		.daddr = iph->daddr,
216648700f7SEric Dumazet 		.user = user,
217648700f7SEric Dumazet 		.vif = vif,
218648700f7SEric Dumazet 		.id = iph->id,
219648700f7SEric Dumazet 		.protocol = iph->protocol,
220648700f7SEric Dumazet 	};
221c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
2221da177e4SLinus Torvalds 
223648700f7SEric Dumazet 	q = inet_frag_find(&net->ipv4.frags, &key);
2242d44ed22SEric Dumazet 	if (!q)
2251da177e4SLinus Torvalds 		return NULL;
2262d44ed22SEric Dumazet 
2275a3da1feSHannes Frederic Sowa 	return container_of(q, struct ipq, q);
2285a3da1feSHannes Frederic Sowa }
2291da177e4SLinus Torvalds 
23089cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
231aa1f731eSFabian Frederick static int ip_frag_too_far(struct ipq *qp)
23289cee8b1SHerbert Xu {
23389cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
2340fbf4cb2SNikolay Borisov 	unsigned int max = qp->q.net->max_dist;
23589cee8b1SHerbert Xu 	unsigned int start, end;
23689cee8b1SHerbert Xu 
23789cee8b1SHerbert Xu 	int rc;
23889cee8b1SHerbert Xu 
23989cee8b1SHerbert Xu 	if (!peer || !max)
24089cee8b1SHerbert Xu 		return 0;
24189cee8b1SHerbert Xu 
24289cee8b1SHerbert Xu 	start = qp->rid;
24389cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
24489cee8b1SHerbert Xu 	qp->rid = end;
24589cee8b1SHerbert Xu 
2465ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
24789cee8b1SHerbert Xu 
24889cee8b1SHerbert Xu 	if (rc) {
2497c73a6faSPavel Emelyanov 		struct net *net;
2507c73a6faSPavel Emelyanov 
2517c73a6faSPavel Emelyanov 		net = container_of(qp->q.net, struct net, ipv4.frags);
252b45386efSEric Dumazet 		__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
25389cee8b1SHerbert Xu 	}
25489cee8b1SHerbert Xu 
25589cee8b1SHerbert Xu 	return rc;
25689cee8b1SHerbert Xu }
25789cee8b1SHerbert Xu 
25889cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
25989cee8b1SHerbert Xu {
26089cee8b1SHerbert Xu 	struct sk_buff *fp;
261d433673eSJesper Dangaard Brouer 	unsigned int sum_truesize = 0;
26289cee8b1SHerbert Xu 
263b2fd5321SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
264edcb6918SReshetova, Elena 		refcount_inc(&qp->q.refcnt);
26589cee8b1SHerbert Xu 		return -ETIMEDOUT;
26689cee8b1SHerbert Xu 	}
26789cee8b1SHerbert Xu 
2685ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
26989cee8b1SHerbert Xu 	do {
27089cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
271d433673eSJesper Dangaard Brouer 
272d433673eSJesper Dangaard Brouer 		sum_truesize += fp->truesize;
273d433673eSJesper Dangaard Brouer 		kfree_skb(fp);
27489cee8b1SHerbert Xu 		fp = xp;
27589cee8b1SHerbert Xu 	} while (fp);
2760e60d245SFlorian Westphal 	sub_frag_mem_limit(qp->q.net, sum_truesize);
27789cee8b1SHerbert Xu 
27806aa8b8aSNikolay Aleksandrov 	qp->q.flags = 0;
2795ab11c98SPavel Emelyanov 	qp->q.len = 0;
2805ab11c98SPavel Emelyanov 	qp->q.meat = 0;
2815ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
282d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
28389cee8b1SHerbert Xu 	qp->iif = 0;
2846623e3b2SEric Dumazet 	qp->ecn = 0;
28589cee8b1SHerbert Xu 
28689cee8b1SHerbert Xu 	return 0;
28789cee8b1SHerbert Xu }
28889cee8b1SHerbert Xu 
2891da177e4SLinus Torvalds /* Add new segment to existing queue. */
2901706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
2931706d587SHerbert Xu 	struct net_device *dev;
294d6b915e2SFlorian Westphal 	unsigned int fragsize;
2951da177e4SLinus Torvalds 	int flags, offset;
2961da177e4SLinus Torvalds 	int ihl, end;
2971706d587SHerbert Xu 	int err = -ENOENT;
2986623e3b2SEric Dumazet 	u8 ecn;
2991da177e4SLinus Torvalds 
30006aa8b8aSNikolay Aleksandrov 	if (qp->q.flags & INET_FRAG_COMPLETE)
3011da177e4SLinus Torvalds 		goto err;
3021da177e4SLinus Torvalds 
30389cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3041706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3051706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
30689cee8b1SHerbert Xu 		ipq_kill(qp);
30789cee8b1SHerbert Xu 		goto err;
30889cee8b1SHerbert Xu 	}
30989cee8b1SHerbert Xu 
3106623e3b2SEric Dumazet 	ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
311eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3121da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3131da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3141da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
315c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3180848f642SEdward Hyunkoo Jee 	end = offset + skb->len - skb_network_offset(skb) - ihl;
3191706d587SHerbert Xu 	err = -EINVAL;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	/* Is this the final fragment? */
3221da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3231da177e4SLinus Torvalds 		/* If we already have some bits beyond end
32442b2aa86SJustin P. Mattock 		 * or have different end, the segment is corrupted.
3251da177e4SLinus Torvalds 		 */
3265ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
32706aa8b8aSNikolay Aleksandrov 		    ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
3281da177e4SLinus Torvalds 			goto err;
32906aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_LAST_IN;
3305ab11c98SPavel Emelyanov 		qp->q.len = end;
3311da177e4SLinus Torvalds 	} else {
3321da177e4SLinus Torvalds 		if (end&7) {
3331da177e4SLinus Torvalds 			end &= ~7;
3341da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3351da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3361da177e4SLinus Torvalds 		}
3375ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3381da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
33906aa8b8aSNikolay Aleksandrov 			if (qp->q.flags & INET_FRAG_LAST_IN)
3401da177e4SLinus Torvalds 				goto err;
3415ab11c98SPavel Emelyanov 			qp->q.len = end;
3421da177e4SLinus Torvalds 		}
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds 	if (end == offset)
3451da177e4SLinus Torvalds 		goto err;
3461da177e4SLinus Torvalds 
3471706d587SHerbert Xu 	err = -ENOMEM;
3480848f642SEdward Hyunkoo Jee 	if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
3491da177e4SLinus Torvalds 		goto err;
3501706d587SHerbert Xu 
3511706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3521706d587SHerbert Xu 	if (err)
3531da177e4SLinus Torvalds 		goto err;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3561da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3571da177e4SLinus Torvalds 	 * this fragment, right?
3581da177e4SLinus Torvalds 	 */
359d6bebca9SChangli Gao 	prev = qp->q.fragments_tail;
360d6bebca9SChangli Gao 	if (!prev || FRAG_CB(prev)->offset < offset) {
361d6bebca9SChangli Gao 		next = NULL;
362d6bebca9SChangli Gao 		goto found;
363d6bebca9SChangli Gao 	}
3641da177e4SLinus Torvalds 	prev = NULL;
3655ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
3661da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
3671da177e4SLinus Torvalds 			break;	/* bingo! */
3681da177e4SLinus Torvalds 		prev = next;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
371d6bebca9SChangli Gao found:
3721da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
3731da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
3741da177e4SLinus Torvalds 	 * any overlaps are eliminated.
3751da177e4SLinus Torvalds 	 */
3761da177e4SLinus Torvalds 	if (prev) {
3771da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		if (i > 0) {
3801da177e4SLinus Torvalds 			offset += i;
3811706d587SHerbert Xu 			err = -EINVAL;
3821da177e4SLinus Torvalds 			if (end <= offset)
3831da177e4SLinus Torvalds 				goto err;
3841706d587SHerbert Xu 			err = -ENOMEM;
3851da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
3861da177e4SLinus Torvalds 				goto err;
3871da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3881da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 
3921706d587SHerbert Xu 	err = -ENOMEM;
3931706d587SHerbert Xu 
3941da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
3951da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 		if (i < next->len) {
3981da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
3991da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
4001da177e4SLinus Torvalds 			 */
4011da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
4021da177e4SLinus Torvalds 				goto err;
4031da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4045ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4051da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4061da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4071da177e4SLinus Torvalds 			break;
4081da177e4SLinus Torvalds 		} else {
4091da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4101da177e4SLinus Torvalds 
41147c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4121da177e4SLinus Torvalds 			 * new one drop it.
4131da177e4SLinus Torvalds 			 */
4141da177e4SLinus Torvalds 			next = next->next;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 			if (prev)
4171da177e4SLinus Torvalds 				prev->next = next;
4181da177e4SLinus Torvalds 			else
4195ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4201da177e4SLinus Torvalds 
4215ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4220e60d245SFlorian Westphal 			sub_frag_mem_limit(qp->q.net, free_it->truesize);
423d433673eSJesper Dangaard Brouer 			kfree_skb(free_it);
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4301da177e4SLinus Torvalds 	skb->next = next;
431d6bebca9SChangli Gao 	if (!next)
432d6bebca9SChangli Gao 		qp->q.fragments_tail = skb;
4331da177e4SLinus Torvalds 	if (prev)
4341da177e4SLinus Torvalds 		prev->next = skb;
4351da177e4SLinus Torvalds 	else
4365ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4371da177e4SLinus Torvalds 
4381706d587SHerbert Xu 	dev = skb->dev;
4391706d587SHerbert Xu 	if (dev) {
4401706d587SHerbert Xu 		qp->iif = dev->ifindex;
4411da177e4SLinus Torvalds 		skb->dev = NULL;
4421706d587SHerbert Xu 	}
4435ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4445ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4456623e3b2SEric Dumazet 	qp->ecn |= ecn;
4460e60d245SFlorian Westphal 	add_frag_mem_limit(qp->q.net, skb->truesize);
4471da177e4SLinus Torvalds 	if (offset == 0)
44806aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_FIRST_IN;
4491da177e4SLinus Torvalds 
450d6b915e2SFlorian Westphal 	fragsize = skb->len + ihl;
451d6b915e2SFlorian Westphal 
452d6b915e2SFlorian Westphal 	if (fragsize > qp->q.max_size)
453d6b915e2SFlorian Westphal 		qp->q.max_size = fragsize;
454d6b915e2SFlorian Westphal 
4555f2d04f1SPatrick McHardy 	if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
456d6b915e2SFlorian Westphal 	    fragsize > qp->max_df_size)
457d6b915e2SFlorian Westphal 		qp->max_df_size = fragsize;
4585f2d04f1SPatrick McHardy 
45906aa8b8aSNikolay Aleksandrov 	if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
46097599dc7SEric Dumazet 	    qp->q.meat == qp->q.len) {
46197599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
4621706d587SHerbert Xu 
46397599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
46497599dc7SEric Dumazet 		err = ip_frag_reasm(qp, prev, dev);
46597599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
46697599dc7SEric Dumazet 		return err;
46797599dc7SEric Dumazet 	}
46897599dc7SEric Dumazet 
46997599dc7SEric Dumazet 	skb_dst_drop(skb);
4701706d587SHerbert Xu 	return -EINPROGRESS;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds err:
4731da177e4SLinus Torvalds 	kfree_skb(skb);
4741706d587SHerbert Xu 	return err;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
4791da177e4SLinus Torvalds 
4801706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
4811706d587SHerbert Xu 			 struct net_device *dev)
4821da177e4SLinus Torvalds {
4832bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
4841da177e4SLinus Torvalds 	struct iphdr *iph;
4855ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
4861da177e4SLinus Torvalds 	int len;
4871da177e4SLinus Torvalds 	int ihlen;
4881706d587SHerbert Xu 	int err;
4895173cc05SEric Dumazet 	u8 ecn;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	ipq_kill(qp);
4921da177e4SLinus Torvalds 
493be991971SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[qp->ecn];
4945173cc05SEric Dumazet 	if (unlikely(ecn == 0xff)) {
4955173cc05SEric Dumazet 		err = -EINVAL;
4965173cc05SEric Dumazet 		goto out_fail;
4975173cc05SEric Dumazet 	}
4981706d587SHerbert Xu 	/* Make the one we just received the head. */
4991706d587SHerbert Xu 	if (prev) {
5001706d587SHerbert Xu 		head = prev->next;
5011706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
5021706d587SHerbert Xu 		if (!fp)
5031706d587SHerbert Xu 			goto out_nomem;
5041706d587SHerbert Xu 
5051706d587SHerbert Xu 		fp->next = head->next;
506d6bebca9SChangli Gao 		if (!fp->next)
507d6bebca9SChangli Gao 			qp->q.fragments_tail = fp;
5081706d587SHerbert Xu 		prev->next = fp;
5091706d587SHerbert Xu 
5105ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
5115ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
5121706d587SHerbert Xu 
513cbf8f7bbSEric Dumazet 		consume_skb(qp->q.fragments);
5145ab11c98SPavel Emelyanov 		qp->q.fragments = head;
5151706d587SHerbert Xu 	}
5161706d587SHerbert Xu 
51751456b29SIan Morris 	WARN_ON(!head);
518547b792cSIlpo Järvinen 	WARN_ON(FRAG_CB(head)->offset != 0);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
521c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
5225ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
5231da177e4SLinus Torvalds 
5241706d587SHerbert Xu 	err = -E2BIG;
5251da177e4SLinus Torvalds 	if (len > 65535)
5261da177e4SLinus Torvalds 		goto out_oversize;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
52914bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
5301da177e4SLinus Torvalds 		goto out_nomem;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5331da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5341da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
53521dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
5361da177e4SLinus Torvalds 		struct sk_buff *clone;
5371da177e4SLinus Torvalds 		int i, plen = 0;
5381da177e4SLinus Torvalds 
53951456b29SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
54051456b29SIan Morris 		if (!clone)
5411da177e4SLinus Torvalds 			goto out_nomem;
5421da177e4SLinus Torvalds 		clone->next = head->next;
5431da177e4SLinus Torvalds 		head->next = clone;
5441da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
545d7fcf1a5SDavid S. Miller 		skb_frag_list_init(head);
5461da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
5479e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
5481da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5491da177e4SLinus Torvalds 		head->data_len -= clone->len;
5501da177e4SLinus Torvalds 		head->len -= clone->len;
5511da177e4SLinus Torvalds 		clone->csum = 0;
5521da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5530e60d245SFlorian Westphal 		add_frag_mem_limit(qp->q.net, clone->truesize);
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 
55614fe22e3SFlorian Westphal 	skb_shinfo(head)->frag_list = head->next;
557d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
5581da177e4SLinus Torvalds 
55914fe22e3SFlorian Westphal 	for (fp=head->next; fp; fp = fp->next) {
56014fe22e3SFlorian Westphal 		head->data_len += fp->len;
56114fe22e3SFlorian Westphal 		head->len += fp->len;
5621da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
5631da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
56484fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
5651da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
5661da177e4SLinus Torvalds 		head->truesize += fp->truesize;
5671da177e4SLinus Torvalds 	}
5685510b3c2SDavid S. Miller 	sub_frag_mem_limit(qp->q.net, head->truesize);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	head->next = NULL;
5711da177e4SLinus Torvalds 	head->dev = dev;
5725ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
573d6b915e2SFlorian Westphal 	IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
5741da177e4SLinus Torvalds 
575eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
5761da177e4SLinus Torvalds 	iph->tot_len = htons(len);
5775173cc05SEric Dumazet 	iph->tos |= ecn;
578d6b915e2SFlorian Westphal 
579d6b915e2SFlorian Westphal 	/* When we set IP_DF on a refragmented skb we must also force a
580d6b915e2SFlorian Westphal 	 * call to ip_fragment to avoid forwarding a DF-skb of size s while
581d6b915e2SFlorian Westphal 	 * original sender only sent fragments of size f (where f < s).
582d6b915e2SFlorian Westphal 	 *
583d6b915e2SFlorian Westphal 	 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
584d6b915e2SFlorian Westphal 	 * frag seen to avoid sending tiny DF-fragments in case skb was built
585d6b915e2SFlorian Westphal 	 * from one very small df-fragment and one large non-df frag.
586d6b915e2SFlorian Westphal 	 */
587d6b915e2SFlorian Westphal 	if (qp->max_df_size == qp->q.max_size) {
588d6b915e2SFlorian Westphal 		IPCB(head)->flags |= IPSKB_FRAG_PMTU;
589d6b915e2SFlorian Westphal 		iph->frag_off = htons(IP_DF);
590d6b915e2SFlorian Westphal 	} else {
591d6b915e2SFlorian Westphal 		iph->frag_off = 0;
592d6b915e2SFlorian Westphal 	}
593d6b915e2SFlorian Westphal 
5940848f642SEdward Hyunkoo Jee 	ip_send_check(iph);
5950848f642SEdward Hyunkoo Jee 
596b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
5975ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
598d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
5991706d587SHerbert Xu 	return 0;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds out_nomem:
602ba7a46f1SJoe Perches 	net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
60345542479SDavid Howells 	err = -ENOMEM;
6041da177e4SLinus Torvalds 	goto out_fail;
6051da177e4SLinus Torvalds out_oversize:
606648700f7SEric Dumazet 	net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->q.key.v4.saddr);
6071da177e4SLinus Torvalds out_fail:
608b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
6091706d587SHerbert Xu 	return err;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
61319bcf9f2SEric W. Biederman int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
6141da177e4SLinus Torvalds {
6159972f134SDavid Ahern 	struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
616385add90SDavid Ahern 	int vif = l3mdev_master_ifindex_rcu(dev);
6171da177e4SLinus Torvalds 	struct ipq *qp;
6181da177e4SLinus Torvalds 
619b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
6208282f274SJoe Stringer 	skb_orphan(skb);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
6239972f134SDavid Ahern 	qp = ip_find(net, ip_hdr(skb), user, vif);
62400db4124SIan Morris 	if (qp) {
6251706d587SHerbert Xu 		int ret;
6261da177e4SLinus Torvalds 
6275ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
6281da177e4SLinus Torvalds 
6291706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
6301da177e4SLinus Torvalds 
6315ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
6324b6cb5d8SPavel Emelyanov 		ipq_put(qp);
633776c729eSHerbert Xu 		return ret;
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds 
636b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
6371da177e4SLinus Torvalds 	kfree_skb(skb);
638776c729eSHerbert Xu 	return -ENOMEM;
6391da177e4SLinus Torvalds }
6404bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_defrag);
6411da177e4SLinus Torvalds 
64219bcf9f2SEric W. Biederman struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
643bc416d97SEric Dumazet {
6441bf3751eSJohannes Berg 	struct iphdr iph;
6453e32e733SAlexander Drozdov 	int netoff;
646bc416d97SEric Dumazet 	u32 len;
647bc416d97SEric Dumazet 
648bc416d97SEric Dumazet 	if (skb->protocol != htons(ETH_P_IP))
649bc416d97SEric Dumazet 		return skb;
650bc416d97SEric Dumazet 
6513e32e733SAlexander Drozdov 	netoff = skb_network_offset(skb);
6523e32e733SAlexander Drozdov 
6533e32e733SAlexander Drozdov 	if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
654bc416d97SEric Dumazet 		return skb;
655bc416d97SEric Dumazet 
6561bf3751eSJohannes Berg 	if (iph.ihl < 5 || iph.version != 4)
657bc416d97SEric Dumazet 		return skb;
658bc416d97SEric Dumazet 
6591bf3751eSJohannes Berg 	len = ntohs(iph.tot_len);
6603e32e733SAlexander Drozdov 	if (skb->len < netoff + len || len < (iph.ihl * 4))
6611bf3751eSJohannes Berg 		return skb;
6621bf3751eSJohannes Berg 
6631bf3751eSJohannes Berg 	if (ip_is_fragment(&iph)) {
664bc416d97SEric Dumazet 		skb = skb_share_check(skb, GFP_ATOMIC);
665bc416d97SEric Dumazet 		if (skb) {
6663e32e733SAlexander Drozdov 			if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
6671bf3751eSJohannes Berg 				return skb;
6683e32e733SAlexander Drozdov 			if (pskb_trim_rcsum(skb, netoff + len))
669bc416d97SEric Dumazet 				return skb;
670bc416d97SEric Dumazet 			memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
67119bcf9f2SEric W. Biederman 			if (ip_defrag(net, skb, user))
672bc416d97SEric Dumazet 				return NULL;
6737539fadcSTom Herbert 			skb_clear_hash(skb);
674bc416d97SEric Dumazet 		}
675bc416d97SEric Dumazet 	}
676bc416d97SEric Dumazet 	return skb;
677bc416d97SEric Dumazet }
678bc416d97SEric Dumazet EXPORT_SYMBOL(ip_check_defrag);
679bc416d97SEric Dumazet 
6808d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
681*3e67f106SEric Dumazet static long zero;
6828d8354d2SPavel Emelyanov 
6830a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = {
6848d8354d2SPavel Emelyanov 	{
6858d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
686e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.high_thresh,
687*3e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
6888d8354d2SPavel Emelyanov 		.mode		= 0644,
689*3e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
6901bab4c75SNikolay Aleksandrov 		.extra1		= &init_net.ipv4.frags.low_thresh
6918d8354d2SPavel Emelyanov 	},
6928d8354d2SPavel Emelyanov 	{
6938d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
694e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.low_thresh,
695*3e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
6968d8354d2SPavel Emelyanov 		.mode		= 0644,
697*3e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
6981bab4c75SNikolay Aleksandrov 		.extra1		= &zero,
6991bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv4.frags.high_thresh
7008d8354d2SPavel Emelyanov 	},
7018d8354d2SPavel Emelyanov 	{
7028d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
703b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv4.frags.timeout,
7048d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7058d8354d2SPavel Emelyanov 		.mode		= 0644,
7066d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
7078d8354d2SPavel Emelyanov 	},
7080fbf4cb2SNikolay Borisov 	{
7090fbf4cb2SNikolay Borisov 		.procname	= "ipfrag_max_dist",
7100fbf4cb2SNikolay Borisov 		.data		= &init_net.ipv4.frags.max_dist,
7110fbf4cb2SNikolay Borisov 		.maxlen		= sizeof(int),
7120fbf4cb2SNikolay Borisov 		.mode		= 0644,
7130fbf4cb2SNikolay Borisov 		.proc_handler	= proc_dointvec_minmax,
7140fbf4cb2SNikolay Borisov 		.extra1		= &zero
7150fbf4cb2SNikolay Borisov 	},
7167d291ebbSPavel Emelyanov 	{ }
7177d291ebbSPavel Emelyanov };
7187d291ebbSPavel Emelyanov 
719e3a57d18SFlorian Westphal /* secret interval has been deprecated */
720e3a57d18SFlorian Westphal static int ip4_frags_secret_interval_unused;
7217d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
7228d8354d2SPavel Emelyanov 	{
7238d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
724e3a57d18SFlorian Westphal 		.data		= &ip4_frags_secret_interval_unused,
7258d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7268d8354d2SPavel Emelyanov 		.mode		= 0644,
7276d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
7288d8354d2SPavel Emelyanov 	},
7298d8354d2SPavel Emelyanov 	{ }
7308d8354d2SPavel Emelyanov };
7318d8354d2SPavel Emelyanov 
7322c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net)
7338d8354d2SPavel Emelyanov {
734e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
7358d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
7368d8354d2SPavel Emelyanov 
7370a64b4b8SPavel Emelyanov 	table = ip4_frags_ns_ctl_table;
73809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
7390a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
74051456b29SIan Morris 		if (!table)
741e4a2d5c2SPavel Emelyanov 			goto err_alloc;
742e4a2d5c2SPavel Emelyanov 
743e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv4.frags.high_thresh;
7441bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv4.frags.low_thresh;
7451bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv4.frags.high_thresh;
746e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv4.frags.low_thresh;
7471bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv4.frags.high_thresh;
748b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv4.frags.timeout;
7490fbf4cb2SNikolay Borisov 		table[3].data = &net->ipv4.frags.max_dist;
750e4a2d5c2SPavel Emelyanov 	}
751e4a2d5c2SPavel Emelyanov 
752ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv4", table);
75351456b29SIan Morris 	if (!hdr)
754e4a2d5c2SPavel Emelyanov 		goto err_reg;
755e4a2d5c2SPavel Emelyanov 
756e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
757e4a2d5c2SPavel Emelyanov 	return 0;
758e4a2d5c2SPavel Emelyanov 
759e4a2d5c2SPavel Emelyanov err_reg:
76009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
761e4a2d5c2SPavel Emelyanov 		kfree(table);
762e4a2d5c2SPavel Emelyanov err_alloc:
763e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
764e4a2d5c2SPavel Emelyanov }
765e4a2d5c2SPavel Emelyanov 
7662c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
767e4a2d5c2SPavel Emelyanov {
768e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
769e4a2d5c2SPavel Emelyanov 
770e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
771e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
772e4a2d5c2SPavel Emelyanov 	kfree(table);
7738d8354d2SPavel Emelyanov }
7747d291ebbSPavel Emelyanov 
77557a02c39SFabian Frederick static void __init ip4_frags_ctl_register(void)
7767d291ebbSPavel Emelyanov {
77743444757SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
7787d291ebbSPavel Emelyanov }
7798d8354d2SPavel Emelyanov #else
780aa1f731eSFabian Frederick static int ip4_frags_ns_ctl_register(struct net *net)
7818d8354d2SPavel Emelyanov {
7828d8354d2SPavel Emelyanov 	return 0;
7838d8354d2SPavel Emelyanov }
784e4a2d5c2SPavel Emelyanov 
785aa1f731eSFabian Frederick static void ip4_frags_ns_ctl_unregister(struct net *net)
786e4a2d5c2SPavel Emelyanov {
787e4a2d5c2SPavel Emelyanov }
7887d291ebbSPavel Emelyanov 
789aa1f731eSFabian Frederick static void __init ip4_frags_ctl_register(void)
7907d291ebbSPavel Emelyanov {
7917d291ebbSPavel Emelyanov }
7928d8354d2SPavel Emelyanov #endif
7938d8354d2SPavel Emelyanov 
7942c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net)
7958d8354d2SPavel Emelyanov {
796787bea77SEric Dumazet 	int res;
797787bea77SEric Dumazet 
798c2a93660SJesper Dangaard Brouer 	/* Fragment cache limits.
799c2a93660SJesper Dangaard Brouer 	 *
800c2a93660SJesper Dangaard Brouer 	 * The fragment memory accounting code, (tries to) account for
801c2a93660SJesper Dangaard Brouer 	 * the real memory usage, by measuring both the size of frag
802c2a93660SJesper Dangaard Brouer 	 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
803c2a93660SJesper Dangaard Brouer 	 * and the SKB's truesize.
804c2a93660SJesper Dangaard Brouer 	 *
805c2a93660SJesper Dangaard Brouer 	 * A 64K fragment consumes 129736 bytes (44*2944)+200
806c2a93660SJesper Dangaard Brouer 	 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
807c2a93660SJesper Dangaard Brouer 	 *
808c2a93660SJesper Dangaard Brouer 	 * We will commit 4MB at one time. Should we cross that limit
809c2a93660SJesper Dangaard Brouer 	 * we will prune down to 3MB, making room for approx 8 big 64K
810c2a93660SJesper Dangaard Brouer 	 * fragments 8x128k.
811e31e0bdcSPavel Emelyanov 	 */
812c2a93660SJesper Dangaard Brouer 	net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
813c2a93660SJesper Dangaard Brouer 	net->ipv4.frags.low_thresh  = 3 * 1024 * 1024;
814e31e0bdcSPavel Emelyanov 	/*
815b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
816b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
817b2fd5321SPavel Emelyanov 	 * by TTL.
818b2fd5321SPavel Emelyanov 	 */
819b2fd5321SPavel Emelyanov 	net->ipv4.frags.timeout = IP_FRAG_TIME;
820b2fd5321SPavel Emelyanov 
8210fbf4cb2SNikolay Borisov 	net->ipv4.frags.max_dist = 64;
822093ba729SEric Dumazet 	net->ipv4.frags.f = &ip4_frags;
8230fbf4cb2SNikolay Borisov 
824787bea77SEric Dumazet 	res = inet_frags_init_net(&net->ipv4.frags);
825787bea77SEric Dumazet 	if (res < 0)
826787bea77SEric Dumazet 		return res;
827787bea77SEric Dumazet 	res = ip4_frags_ns_ctl_register(net);
828787bea77SEric Dumazet 	if (res < 0)
829093ba729SEric Dumazet 		inet_frags_exit_net(&net->ipv4.frags);
830787bea77SEric Dumazet 	return res;
8318d8354d2SPavel Emelyanov }
8328d8354d2SPavel Emelyanov 
8332c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net)
83481566e83SPavel Emelyanov {
8350a64b4b8SPavel Emelyanov 	ip4_frags_ns_ctl_unregister(net);
836093ba729SEric Dumazet 	inet_frags_exit_net(&net->ipv4.frags);
83781566e83SPavel Emelyanov }
83881566e83SPavel Emelyanov 
83981566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
84081566e83SPavel Emelyanov 	.init = ipv4_frags_init_net,
84181566e83SPavel Emelyanov 	.exit = ipv4_frags_exit_net,
84281566e83SPavel Emelyanov };
84381566e83SPavel Emelyanov 
844648700f7SEric Dumazet 
845648700f7SEric Dumazet static u32 ip4_key_hashfn(const void *data, u32 len, u32 seed)
846648700f7SEric Dumazet {
847648700f7SEric Dumazet 	return jhash2(data,
848648700f7SEric Dumazet 		      sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
849648700f7SEric Dumazet }
850648700f7SEric Dumazet 
851648700f7SEric Dumazet static u32 ip4_obj_hashfn(const void *data, u32 len, u32 seed)
852648700f7SEric Dumazet {
853648700f7SEric Dumazet 	const struct inet_frag_queue *fq = data;
854648700f7SEric Dumazet 
855648700f7SEric Dumazet 	return jhash2((const u32 *)&fq->key.v4,
856648700f7SEric Dumazet 		      sizeof(struct frag_v4_compare_key) / sizeof(u32), seed);
857648700f7SEric Dumazet }
858648700f7SEric Dumazet 
859648700f7SEric Dumazet static int ip4_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
860648700f7SEric Dumazet {
861648700f7SEric Dumazet 	const struct frag_v4_compare_key *key = arg->key;
862648700f7SEric Dumazet 	const struct inet_frag_queue *fq = ptr;
863648700f7SEric Dumazet 
864648700f7SEric Dumazet 	return !!memcmp(&fq->key, key, sizeof(*key));
865648700f7SEric Dumazet }
866648700f7SEric Dumazet 
867648700f7SEric Dumazet static const struct rhashtable_params ip4_rhash_params = {
868648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
869648700f7SEric Dumazet 	.key_offset		= offsetof(struct inet_frag_queue, key),
870648700f7SEric Dumazet 	.key_len		= sizeof(struct frag_v4_compare_key),
871648700f7SEric Dumazet 	.hashfn			= ip4_key_hashfn,
872648700f7SEric Dumazet 	.obj_hashfn		= ip4_obj_hashfn,
873648700f7SEric Dumazet 	.obj_cmpfn		= ip4_obj_cmpfn,
874648700f7SEric Dumazet 	.automatic_shrinking	= true,
875648700f7SEric Dumazet };
876648700f7SEric Dumazet 
877b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
8781da177e4SLinus Torvalds {
879c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
8801e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
8811e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
882e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
883d4ad4d22SNikolay Aleksandrov 	ip4_frags.frags_cache_name = ip_frag_cache_name;
884648700f7SEric Dumazet 	ip4_frags.rhash_params = ip4_rhash_params;
885d4ad4d22SNikolay Aleksandrov 	if (inet_frags_init(&ip4_frags))
886d4ad4d22SNikolay Aleksandrov 		panic("IP: failed to allocate ip4_frags cache\n");
887483a6e4fSEric Dumazet 	ip4_frags_ctl_register();
888483a6e4fSEric Dumazet 	register_pernet_subsys(&ip4_frags_ops);
8891da177e4SLinus Torvalds }
890