xref: /linux/net/ipv4/ip_fragment.c (revision 78802011fbe34331bdef6f2dfb1634011f0e4c32)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		The IP fragmentation functionality.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
9113aa838SAlan Cox  *		Alan Cox <alan@lxorguk.ukuu.org.uk>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Fixes:
121da177e4SLinus Torvalds  *		Alan Cox	:	Split from ip.c , see ip_input.c for history.
131da177e4SLinus Torvalds  *		David S. Miller :	Begin massive cleanup...
141da177e4SLinus Torvalds  *		Andi Kleen	:	Add sysctls.
151da177e4SLinus Torvalds  *		xxxx		:	Overlapfrag bug.
161da177e4SLinus Torvalds  *		Ultima          :       ip_expire() kernel panic.
171da177e4SLinus Torvalds  *		Bill Hawes	:	Frag accounting and evictor fixes.
181da177e4SLinus Torvalds  *		John McDonald	:	0 length frag bug.
191da177e4SLinus Torvalds  *		Alexey Kuznetsov:	SMP races, threading, cleanup.
201da177e4SLinus Torvalds  *		Patrick McHardy :	LRU queue of frag heads for evictor.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
23afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt
24afd46503SJoe Perches 
2589cee8b1SHerbert Xu #include <linux/compiler.h>
261da177e4SLinus Torvalds #include <linux/module.h>
271da177e4SLinus Torvalds #include <linux/types.h>
281da177e4SLinus Torvalds #include <linux/mm.h>
291da177e4SLinus Torvalds #include <linux/jiffies.h>
301da177e4SLinus Torvalds #include <linux/skbuff.h>
311da177e4SLinus Torvalds #include <linux/list.h>
321da177e4SLinus Torvalds #include <linux/ip.h>
331da177e4SLinus Torvalds #include <linux/icmp.h>
341da177e4SLinus Torvalds #include <linux/netdevice.h>
351da177e4SLinus Torvalds #include <linux/jhash.h>
361da177e4SLinus Torvalds #include <linux/random.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
38e9017b55SShan Wei #include <net/route.h>
39e9017b55SShan Wei #include <net/dst.h>
401da177e4SLinus Torvalds #include <net/sock.h>
411da177e4SLinus Torvalds #include <net/ip.h>
421da177e4SLinus Torvalds #include <net/icmp.h>
431da177e4SLinus Torvalds #include <net/checksum.h>
4489cee8b1SHerbert Xu #include <net/inetpeer.h>
455ab11c98SPavel Emelyanov #include <net/inet_frag.h>
461da177e4SLinus Torvalds #include <linux/tcp.h>
471da177e4SLinus Torvalds #include <linux/udp.h>
481da177e4SLinus Torvalds #include <linux/inet.h>
491da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
506623e3b2SEric Dumazet #include <net/inet_ecn.h>
51385add90SDavid Ahern #include <net/l3mdev.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
541da177e4SLinus Torvalds  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
551da177e4SLinus Torvalds  * as well. Or notify me, at least. --ANK
561da177e4SLinus Torvalds  */
57d4ad4d22SNikolay Aleksandrov static const char ip_frag_cache_name[] = "ip4-frags";
5889cee8b1SHerbert Xu 
591da177e4SLinus Torvalds struct ipfrag_skb_cb
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	struct inet_skb_parm	h;
621da177e4SLinus Torvalds 	int			offset;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define FRAG_CB(skb)	((struct ipfrag_skb_cb *)((skb)->cb))
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */
681da177e4SLinus Torvalds struct ipq {
695ab11c98SPavel Emelyanov 	struct inet_frag_queue q;
705ab11c98SPavel Emelyanov 
711da177e4SLinus Torvalds 	u32		user;
7218277770SAl Viro 	__be32		saddr;
7318277770SAl Viro 	__be32		daddr;
7418277770SAl Viro 	__be16		id;
751da177e4SLinus Torvalds 	u8		protocol;
766623e3b2SEric Dumazet 	u8		ecn; /* RFC3168 support */
77d6b915e2SFlorian Westphal 	u16		max_df_size; /* largest frag with DF set seen */
7889cee8b1SHerbert Xu 	int             iif;
79385add90SDavid Ahern 	int             vif;   /* L3 master device index */
8089cee8b1SHerbert Xu 	unsigned int    rid;
8189cee8b1SHerbert Xu 	struct inet_peer *peer;
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
84aa1f731eSFabian Frederick static u8 ip4_frag_ecn(u8 tos)
856623e3b2SEric Dumazet {
865173cc05SEric Dumazet 	return 1 << (tos & INET_ECN_MASK);
876623e3b2SEric Dumazet }
886623e3b2SEric Dumazet 
897eb95156SPavel Emelyanov static struct inet_frags ip4_frags;
901da177e4SLinus Torvalds 
916ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net)
927eb95156SPavel Emelyanov {
93d433673eSJesper Dangaard Brouer 	return sum_frag_mem_limit(&net->ipv4.frags);
947eb95156SPavel Emelyanov }
951da177e4SLinus Torvalds 
961706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
971706d587SHerbert Xu 			 struct net_device *dev);
981706d587SHerbert Xu 
99c6fda282SPavel Emelyanov struct ip4_create_arg {
100c6fda282SPavel Emelyanov 	struct iphdr *iph;
101c6fda282SPavel Emelyanov 	u32 user;
1029972f134SDavid Ahern 	int vif;
103c6fda282SPavel Emelyanov };
104c6fda282SPavel Emelyanov 
10518277770SAl Viro static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
1061da177e4SLinus Torvalds {
107e7b519baSHannes Frederic Sowa 	net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
10818277770SAl Viro 	return jhash_3words((__force u32)id << 16 | prot,
10918277770SAl Viro 			    (__force u32)saddr, (__force u32)daddr,
110fb3cfe6eSFlorian Westphal 			    ip4_frags.rnd);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
11336c77782SFlorian Westphal static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
1141da177e4SLinus Torvalds {
11536c77782SFlorian Westphal 	const struct ipq *ipq;
1161da177e4SLinus Torvalds 
117321a3a99SPavel Emelyanov 	ipq = container_of(q, struct ipq, q);
118321a3a99SPavel Emelyanov 	return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
12136c77782SFlorian Westphal static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
122abd6523dSPavel Emelyanov {
12336c77782SFlorian Westphal 	const struct ipq *qp;
12436c77782SFlorian Westphal 	const struct ip4_create_arg *arg = a;
125abd6523dSPavel Emelyanov 
126abd6523dSPavel Emelyanov 	qp = container_of(q, struct ipq, q);
127a02cec21SEric Dumazet 	return	qp->id == arg->iph->id &&
128abd6523dSPavel Emelyanov 		qp->saddr == arg->iph->saddr &&
129abd6523dSPavel Emelyanov 		qp->daddr == arg->iph->daddr &&
130abd6523dSPavel Emelyanov 		qp->protocol == arg->iph->protocol &&
1319972f134SDavid Ahern 		qp->user == arg->user &&
1329972f134SDavid Ahern 		qp->vif == arg->vif;
133abd6523dSPavel Emelyanov }
134abd6523dSPavel Emelyanov 
13536c77782SFlorian Westphal static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
136c6fda282SPavel Emelyanov {
137c6fda282SPavel Emelyanov 	struct ipq *qp = container_of(q, struct ipq, q);
13854db0cc2SGao feng 	struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
13954db0cc2SGao feng 					       frags);
14054db0cc2SGao feng 	struct net *net = container_of(ipv4, struct net, ipv4);
14154db0cc2SGao feng 
14236c77782SFlorian Westphal 	const struct ip4_create_arg *arg = a;
143c6fda282SPavel Emelyanov 
144c6fda282SPavel Emelyanov 	qp->protocol = arg->iph->protocol;
145c6fda282SPavel Emelyanov 	qp->id = arg->iph->id;
1466623e3b2SEric Dumazet 	qp->ecn = ip4_frag_ecn(arg->iph->tos);
147c6fda282SPavel Emelyanov 	qp->saddr = arg->iph->saddr;
148c6fda282SPavel Emelyanov 	qp->daddr = arg->iph->daddr;
1499972f134SDavid Ahern 	qp->vif = arg->vif;
150c6fda282SPavel Emelyanov 	qp->user = arg->user;
1510fbf4cb2SNikolay Borisov 	qp->peer = q->net->max_dist ?
152192132b9SDavid Ahern 		inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, arg->vif, 1) :
153192132b9SDavid Ahern 		NULL;
154c6fda282SPavel Emelyanov }
155c6fda282SPavel Emelyanov 
156aa1f731eSFabian Frederick static void ip4_frag_free(struct inet_frag_queue *q)
1571da177e4SLinus Torvalds {
1581e4b8287SPavel Emelyanov 	struct ipq *qp;
1591e4b8287SPavel Emelyanov 
1601e4b8287SPavel Emelyanov 	qp = container_of(q, struct ipq, q);
1611e4b8287SPavel Emelyanov 	if (qp->peer)
1621e4b8287SPavel Emelyanov 		inet_putpeer(qp->peer);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Destruction primitives. */
1671da177e4SLinus Torvalds 
168aa1f731eSFabian Frederick static void ipq_put(struct ipq *ipq)
1691da177e4SLinus Torvalds {
170762cc408SPavel Emelyanov 	inet_frag_put(&ipq->q, &ip4_frags);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately,
1741da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1751da177e4SLinus Torvalds  */
1761da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq)
1771da177e4SLinus Torvalds {
178277e650dSPavel Emelyanov 	inet_frag_kill(&ipq->q, &ip4_frags);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1815cf42280SAndy Zhou static bool frag_expire_skip_icmp(u32 user)
1825cf42280SAndy Zhou {
1835cf42280SAndy Zhou 	return user == IP_DEFRAG_AF_PACKET ||
1845cf42280SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
1858bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_IN_END) ||
1868bc04864SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
1878bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
1885cf42280SAndy Zhou }
1895cf42280SAndy Zhou 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
1921da177e4SLinus Torvalds  */
193*78802011SKees Cook static void ip_expire(struct timer_list *t)
1941da177e4SLinus Torvalds {
195*78802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
196e521db9dSPavel Emelyanov 	struct ipq *qp;
19784a3aa00SPavel Emelyanov 	struct net *net;
198e521db9dSPavel Emelyanov 
199*78802011SKees Cook 	qp = container_of(frag, struct ipq, q);
20084a3aa00SPavel Emelyanov 	net = container_of(qp->q.net, struct net, ipv4.frags);
2011da177e4SLinus Torvalds 
202ec4fbd64SEric Dumazet 	rcu_read_lock();
2035ab11c98SPavel Emelyanov 	spin_lock(&qp->q.lock);
2041da177e4SLinus Torvalds 
20506aa8b8aSNikolay Aleksandrov 	if (qp->q.flags & INET_FRAG_COMPLETE)
2061da177e4SLinus Torvalds 		goto out;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	ipq_kill(qp);
209b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
2101da177e4SLinus Torvalds 
211caaecdd3SNikolay Aleksandrov 	if (!inet_frag_evicting(&qp->q)) {
212ec4fbd64SEric Dumazet 		struct sk_buff *clone, *head = qp->q.fragments;
21364f3b9e2SEric Dumazet 		const struct iphdr *iph;
21464f3b9e2SEric Dumazet 		int err;
215cb84663eSDenis V. Lunev 
216b45386efSEric Dumazet 		__IP_INC_STATS(net, IPSTATS_MIB_REASMTIMEOUT);
2172e404f63SNikolay Aleksandrov 
2182e404f63SNikolay Aleksandrov 		if (!(qp->q.flags & INET_FRAG_FIRST_IN) || !qp->q.fragments)
2192e404f63SNikolay Aleksandrov 			goto out;
2202e404f63SNikolay Aleksandrov 
22169df9d59SEric Dumazet 		head->dev = dev_get_by_index_rcu(net, qp->iif);
222e9017b55SShan Wei 		if (!head->dev)
223ec4fbd64SEric Dumazet 			goto out;
224ec4fbd64SEric Dumazet 
225e9017b55SShan Wei 
22697599dc7SEric Dumazet 		/* skb has no dst, perform route lookup again */
22764f3b9e2SEric Dumazet 		iph = ip_hdr(head);
228c6cffba4SDavid S. Miller 		err = ip_route_input_noref(head, iph->daddr, iph->saddr,
229c10237e0SDavid S. Miller 					   iph->tos, head->dev);
23064f3b9e2SEric Dumazet 		if (err)
231ec4fbd64SEric Dumazet 			goto out;
232e9017b55SShan Wei 
2332e404f63SNikolay Aleksandrov 		/* Only an end host needs to send an ICMP
234e9017b55SShan Wei 		 * "Fragment Reassembly Timeout" message, per RFC792.
235e9017b55SShan Wei 		 */
2365cf42280SAndy Zhou 		if (frag_expire_skip_icmp(qp->user) &&
2375cf42280SAndy Zhou 		    (skb_rtable(head)->rt_type != RTN_LOCAL))
238ec4fbd64SEric Dumazet 			goto out;
239ec4fbd64SEric Dumazet 
240ec4fbd64SEric Dumazet 		clone = skb_clone(head, GFP_ATOMIC);
241e9017b55SShan Wei 
242e9017b55SShan Wei 		/* Send an ICMP "Fragment Reassembly Timeout" message. */
243ec4fbd64SEric Dumazet 		if (clone) {
244ec4fbd64SEric Dumazet 			spin_unlock(&qp->q.lock);
245ec4fbd64SEric Dumazet 			icmp_send(clone, ICMP_TIME_EXCEEDED,
246ec4fbd64SEric Dumazet 				  ICMP_EXC_FRAGTIME, 0);
247ec4fbd64SEric Dumazet 			consume_skb(clone);
248ec4fbd64SEric Dumazet 			goto out_rcu_unlock;
249ec4fbd64SEric Dumazet 		}
250d1c9ae6dSPatrick McHardy 	}
2511da177e4SLinus Torvalds out:
2525ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
253ec4fbd64SEric Dumazet out_rcu_unlock:
254ec4fbd64SEric Dumazet 	rcu_read_unlock();
2554b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
258abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
259abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
260abd6523dSPavel Emelyanov  */
2619972f134SDavid Ahern static struct ipq *ip_find(struct net *net, struct iphdr *iph,
2629972f134SDavid Ahern 			   u32 user, int vif)
2631da177e4SLinus Torvalds {
264c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
265c6fda282SPavel Emelyanov 	struct ip4_create_arg arg;
266abd6523dSPavel Emelyanov 	unsigned int hash;
2671da177e4SLinus Torvalds 
268c6fda282SPavel Emelyanov 	arg.iph = iph;
269c6fda282SPavel Emelyanov 	arg.user = user;
2709972f134SDavid Ahern 	arg.vif = vif;
2719a375803SPavel Emelyanov 
272abd6523dSPavel Emelyanov 	hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
273c6fda282SPavel Emelyanov 
274ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
2755a3da1feSHannes Frederic Sowa 	if (IS_ERR_OR_NULL(q)) {
2765a3da1feSHannes Frederic Sowa 		inet_frag_maybe_warn_overflow(q, pr_fmt());
2771da177e4SLinus Torvalds 		return NULL;
2781da177e4SLinus Torvalds 	}
2795a3da1feSHannes Frederic Sowa 	return container_of(q, struct ipq, q);
2805a3da1feSHannes Frederic Sowa }
2811da177e4SLinus Torvalds 
28289cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
283aa1f731eSFabian Frederick static int ip_frag_too_far(struct ipq *qp)
28489cee8b1SHerbert Xu {
28589cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
2860fbf4cb2SNikolay Borisov 	unsigned int max = qp->q.net->max_dist;
28789cee8b1SHerbert Xu 	unsigned int start, end;
28889cee8b1SHerbert Xu 
28989cee8b1SHerbert Xu 	int rc;
29089cee8b1SHerbert Xu 
29189cee8b1SHerbert Xu 	if (!peer || !max)
29289cee8b1SHerbert Xu 		return 0;
29389cee8b1SHerbert Xu 
29489cee8b1SHerbert Xu 	start = qp->rid;
29589cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
29689cee8b1SHerbert Xu 	qp->rid = end;
29789cee8b1SHerbert Xu 
2985ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
29989cee8b1SHerbert Xu 
30089cee8b1SHerbert Xu 	if (rc) {
3017c73a6faSPavel Emelyanov 		struct net *net;
3027c73a6faSPavel Emelyanov 
3037c73a6faSPavel Emelyanov 		net = container_of(qp->q.net, struct net, ipv4.frags);
304b45386efSEric Dumazet 		__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
30589cee8b1SHerbert Xu 	}
30689cee8b1SHerbert Xu 
30789cee8b1SHerbert Xu 	return rc;
30889cee8b1SHerbert Xu }
30989cee8b1SHerbert Xu 
31089cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
31189cee8b1SHerbert Xu {
31289cee8b1SHerbert Xu 	struct sk_buff *fp;
313d433673eSJesper Dangaard Brouer 	unsigned int sum_truesize = 0;
31489cee8b1SHerbert Xu 
315b2fd5321SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
316edcb6918SReshetova, Elena 		refcount_inc(&qp->q.refcnt);
31789cee8b1SHerbert Xu 		return -ETIMEDOUT;
31889cee8b1SHerbert Xu 	}
31989cee8b1SHerbert Xu 
3205ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
32189cee8b1SHerbert Xu 	do {
32289cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
323d433673eSJesper Dangaard Brouer 
324d433673eSJesper Dangaard Brouer 		sum_truesize += fp->truesize;
325d433673eSJesper Dangaard Brouer 		kfree_skb(fp);
32689cee8b1SHerbert Xu 		fp = xp;
32789cee8b1SHerbert Xu 	} while (fp);
3280e60d245SFlorian Westphal 	sub_frag_mem_limit(qp->q.net, sum_truesize);
32989cee8b1SHerbert Xu 
33006aa8b8aSNikolay Aleksandrov 	qp->q.flags = 0;
3315ab11c98SPavel Emelyanov 	qp->q.len = 0;
3325ab11c98SPavel Emelyanov 	qp->q.meat = 0;
3335ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
334d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
33589cee8b1SHerbert Xu 	qp->iif = 0;
3366623e3b2SEric Dumazet 	qp->ecn = 0;
33789cee8b1SHerbert Xu 
33889cee8b1SHerbert Xu 	return 0;
33989cee8b1SHerbert Xu }
34089cee8b1SHerbert Xu 
3411da177e4SLinus Torvalds /* Add new segment to existing queue. */
3421706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
3451706d587SHerbert Xu 	struct net_device *dev;
346d6b915e2SFlorian Westphal 	unsigned int fragsize;
3471da177e4SLinus Torvalds 	int flags, offset;
3481da177e4SLinus Torvalds 	int ihl, end;
3491706d587SHerbert Xu 	int err = -ENOENT;
3506623e3b2SEric Dumazet 	u8 ecn;
3511da177e4SLinus Torvalds 
35206aa8b8aSNikolay Aleksandrov 	if (qp->q.flags & INET_FRAG_COMPLETE)
3531da177e4SLinus Torvalds 		goto err;
3541da177e4SLinus Torvalds 
35589cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3561706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3571706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
35889cee8b1SHerbert Xu 		ipq_kill(qp);
35989cee8b1SHerbert Xu 		goto err;
36089cee8b1SHerbert Xu 	}
36189cee8b1SHerbert Xu 
3626623e3b2SEric Dumazet 	ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
363eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3641da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3651da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3661da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
367c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3700848f642SEdward Hyunkoo Jee 	end = offset + skb->len - skb_network_offset(skb) - ihl;
3711706d587SHerbert Xu 	err = -EINVAL;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* Is this the final fragment? */
3741da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3751da177e4SLinus Torvalds 		/* If we already have some bits beyond end
37642b2aa86SJustin P. Mattock 		 * or have different end, the segment is corrupted.
3771da177e4SLinus Torvalds 		 */
3785ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
37906aa8b8aSNikolay Aleksandrov 		    ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
3801da177e4SLinus Torvalds 			goto err;
38106aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_LAST_IN;
3825ab11c98SPavel Emelyanov 		qp->q.len = end;
3831da177e4SLinus Torvalds 	} else {
3841da177e4SLinus Torvalds 		if (end&7) {
3851da177e4SLinus Torvalds 			end &= ~7;
3861da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3871da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3881da177e4SLinus Torvalds 		}
3895ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3901da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
39106aa8b8aSNikolay Aleksandrov 			if (qp->q.flags & INET_FRAG_LAST_IN)
3921da177e4SLinus Torvalds 				goto err;
3935ab11c98SPavel Emelyanov 			qp->q.len = end;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 	if (end == offset)
3971da177e4SLinus Torvalds 		goto err;
3981da177e4SLinus Torvalds 
3991706d587SHerbert Xu 	err = -ENOMEM;
4000848f642SEdward Hyunkoo Jee 	if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
4011da177e4SLinus Torvalds 		goto err;
4021706d587SHerbert Xu 
4031706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
4041706d587SHerbert Xu 	if (err)
4051da177e4SLinus Torvalds 		goto err;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
4081da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
4091da177e4SLinus Torvalds 	 * this fragment, right?
4101da177e4SLinus Torvalds 	 */
411d6bebca9SChangli Gao 	prev = qp->q.fragments_tail;
412d6bebca9SChangli Gao 	if (!prev || FRAG_CB(prev)->offset < offset) {
413d6bebca9SChangli Gao 		next = NULL;
414d6bebca9SChangli Gao 		goto found;
415d6bebca9SChangli Gao 	}
4161da177e4SLinus Torvalds 	prev = NULL;
4175ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
4181da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
4191da177e4SLinus Torvalds 			break;	/* bingo! */
4201da177e4SLinus Torvalds 		prev = next;
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
423d6bebca9SChangli Gao found:
4241da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
4251da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
4261da177e4SLinus Torvalds 	 * any overlaps are eliminated.
4271da177e4SLinus Torvalds 	 */
4281da177e4SLinus Torvalds 	if (prev) {
4291da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		if (i > 0) {
4321da177e4SLinus Torvalds 			offset += i;
4331706d587SHerbert Xu 			err = -EINVAL;
4341da177e4SLinus Torvalds 			if (end <= offset)
4351da177e4SLinus Torvalds 				goto err;
4361706d587SHerbert Xu 			err = -ENOMEM;
4371da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
4381da177e4SLinus Torvalds 				goto err;
4391da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
4401da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
4411da177e4SLinus Torvalds 		}
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 
4441706d587SHerbert Xu 	err = -ENOMEM;
4451706d587SHerbert Xu 
4461da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
4471da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		if (i < next->len) {
4501da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
4511da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
4521da177e4SLinus Torvalds 			 */
4531da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
4541da177e4SLinus Torvalds 				goto err;
4551da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4565ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4571da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4581da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4591da177e4SLinus Torvalds 			break;
4601da177e4SLinus Torvalds 		} else {
4611da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4621da177e4SLinus Torvalds 
46347c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4641da177e4SLinus Torvalds 			 * new one drop it.
4651da177e4SLinus Torvalds 			 */
4661da177e4SLinus Torvalds 			next = next->next;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 			if (prev)
4691da177e4SLinus Torvalds 				prev->next = next;
4701da177e4SLinus Torvalds 			else
4715ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4721da177e4SLinus Torvalds 
4735ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4740e60d245SFlorian Westphal 			sub_frag_mem_limit(qp->q.net, free_it->truesize);
475d433673eSJesper Dangaard Brouer 			kfree_skb(free_it);
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4821da177e4SLinus Torvalds 	skb->next = next;
483d6bebca9SChangli Gao 	if (!next)
484d6bebca9SChangli Gao 		qp->q.fragments_tail = skb;
4851da177e4SLinus Torvalds 	if (prev)
4861da177e4SLinus Torvalds 		prev->next = skb;
4871da177e4SLinus Torvalds 	else
4885ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4891da177e4SLinus Torvalds 
4901706d587SHerbert Xu 	dev = skb->dev;
4911706d587SHerbert Xu 	if (dev) {
4921706d587SHerbert Xu 		qp->iif = dev->ifindex;
4931da177e4SLinus Torvalds 		skb->dev = NULL;
4941706d587SHerbert Xu 	}
4955ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4965ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4976623e3b2SEric Dumazet 	qp->ecn |= ecn;
4980e60d245SFlorian Westphal 	add_frag_mem_limit(qp->q.net, skb->truesize);
4991da177e4SLinus Torvalds 	if (offset == 0)
50006aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_FIRST_IN;
5011da177e4SLinus Torvalds 
502d6b915e2SFlorian Westphal 	fragsize = skb->len + ihl;
503d6b915e2SFlorian Westphal 
504d6b915e2SFlorian Westphal 	if (fragsize > qp->q.max_size)
505d6b915e2SFlorian Westphal 		qp->q.max_size = fragsize;
506d6b915e2SFlorian Westphal 
5075f2d04f1SPatrick McHardy 	if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
508d6b915e2SFlorian Westphal 	    fragsize > qp->max_df_size)
509d6b915e2SFlorian Westphal 		qp->max_df_size = fragsize;
5105f2d04f1SPatrick McHardy 
51106aa8b8aSNikolay Aleksandrov 	if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
51297599dc7SEric Dumazet 	    qp->q.meat == qp->q.len) {
51397599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
5141706d587SHerbert Xu 
51597599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
51697599dc7SEric Dumazet 		err = ip_frag_reasm(qp, prev, dev);
51797599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
51897599dc7SEric Dumazet 		return err;
51997599dc7SEric Dumazet 	}
52097599dc7SEric Dumazet 
52197599dc7SEric Dumazet 	skb_dst_drop(skb);
5221706d587SHerbert Xu 	return -EINPROGRESS;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds err:
5251da177e4SLinus Torvalds 	kfree_skb(skb);
5261706d587SHerbert Xu 	return err;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
5311da177e4SLinus Torvalds 
5321706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
5331706d587SHerbert Xu 			 struct net_device *dev)
5341da177e4SLinus Torvalds {
5352bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
5361da177e4SLinus Torvalds 	struct iphdr *iph;
5375ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
5381da177e4SLinus Torvalds 	int len;
5391da177e4SLinus Torvalds 	int ihlen;
5401706d587SHerbert Xu 	int err;
5415173cc05SEric Dumazet 	u8 ecn;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	ipq_kill(qp);
5441da177e4SLinus Torvalds 
545be991971SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[qp->ecn];
5465173cc05SEric Dumazet 	if (unlikely(ecn == 0xff)) {
5475173cc05SEric Dumazet 		err = -EINVAL;
5485173cc05SEric Dumazet 		goto out_fail;
5495173cc05SEric Dumazet 	}
5501706d587SHerbert Xu 	/* Make the one we just received the head. */
5511706d587SHerbert Xu 	if (prev) {
5521706d587SHerbert Xu 		head = prev->next;
5531706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
5541706d587SHerbert Xu 		if (!fp)
5551706d587SHerbert Xu 			goto out_nomem;
5561706d587SHerbert Xu 
5571706d587SHerbert Xu 		fp->next = head->next;
558d6bebca9SChangli Gao 		if (!fp->next)
559d6bebca9SChangli Gao 			qp->q.fragments_tail = fp;
5601706d587SHerbert Xu 		prev->next = fp;
5611706d587SHerbert Xu 
5625ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
5635ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
5641706d587SHerbert Xu 
565cbf8f7bbSEric Dumazet 		consume_skb(qp->q.fragments);
5665ab11c98SPavel Emelyanov 		qp->q.fragments = head;
5671706d587SHerbert Xu 	}
5681706d587SHerbert Xu 
56951456b29SIan Morris 	WARN_ON(!head);
570547b792cSIlpo Järvinen 	WARN_ON(FRAG_CB(head)->offset != 0);
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
573c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
5745ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
5751da177e4SLinus Torvalds 
5761706d587SHerbert Xu 	err = -E2BIG;
5771da177e4SLinus Torvalds 	if (len > 65535)
5781da177e4SLinus Torvalds 		goto out_oversize;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
58114bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
5821da177e4SLinus Torvalds 		goto out_nomem;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5851da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5861da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
58721dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
5881da177e4SLinus Torvalds 		struct sk_buff *clone;
5891da177e4SLinus Torvalds 		int i, plen = 0;
5901da177e4SLinus Torvalds 
59151456b29SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
59251456b29SIan Morris 		if (!clone)
5931da177e4SLinus Torvalds 			goto out_nomem;
5941da177e4SLinus Torvalds 		clone->next = head->next;
5951da177e4SLinus Torvalds 		head->next = clone;
5961da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
597d7fcf1a5SDavid S. Miller 		skb_frag_list_init(head);
5981da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
5999e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
6001da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
6011da177e4SLinus Torvalds 		head->data_len -= clone->len;
6021da177e4SLinus Torvalds 		head->len -= clone->len;
6031da177e4SLinus Torvalds 		clone->csum = 0;
6041da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
6050e60d245SFlorian Westphal 		add_frag_mem_limit(qp->q.net, clone->truesize);
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds 
60814fe22e3SFlorian Westphal 	skb_shinfo(head)->frag_list = head->next;
609d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
6101da177e4SLinus Torvalds 
61114fe22e3SFlorian Westphal 	for (fp=head->next; fp; fp = fp->next) {
61214fe22e3SFlorian Westphal 		head->data_len += fp->len;
61314fe22e3SFlorian Westphal 		head->len += fp->len;
6141da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6151da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
61684fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
6171da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6181da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6191da177e4SLinus Torvalds 	}
6205510b3c2SDavid S. Miller 	sub_frag_mem_limit(qp->q.net, head->truesize);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	head->next = NULL;
6231da177e4SLinus Torvalds 	head->dev = dev;
6245ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
625d6b915e2SFlorian Westphal 	IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
6261da177e4SLinus Torvalds 
627eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
6281da177e4SLinus Torvalds 	iph->tot_len = htons(len);
6295173cc05SEric Dumazet 	iph->tos |= ecn;
630d6b915e2SFlorian Westphal 
631d6b915e2SFlorian Westphal 	/* When we set IP_DF on a refragmented skb we must also force a
632d6b915e2SFlorian Westphal 	 * call to ip_fragment to avoid forwarding a DF-skb of size s while
633d6b915e2SFlorian Westphal 	 * original sender only sent fragments of size f (where f < s).
634d6b915e2SFlorian Westphal 	 *
635d6b915e2SFlorian Westphal 	 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
636d6b915e2SFlorian Westphal 	 * frag seen to avoid sending tiny DF-fragments in case skb was built
637d6b915e2SFlorian Westphal 	 * from one very small df-fragment and one large non-df frag.
638d6b915e2SFlorian Westphal 	 */
639d6b915e2SFlorian Westphal 	if (qp->max_df_size == qp->q.max_size) {
640d6b915e2SFlorian Westphal 		IPCB(head)->flags |= IPSKB_FRAG_PMTU;
641d6b915e2SFlorian Westphal 		iph->frag_off = htons(IP_DF);
642d6b915e2SFlorian Westphal 	} else {
643d6b915e2SFlorian Westphal 		iph->frag_off = 0;
644d6b915e2SFlorian Westphal 	}
645d6b915e2SFlorian Westphal 
6460848f642SEdward Hyunkoo Jee 	ip_send_check(iph);
6470848f642SEdward Hyunkoo Jee 
648b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMOKS);
6495ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
650d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
6511706d587SHerbert Xu 	return 0;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds out_nomem:
654ba7a46f1SJoe Perches 	net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
65545542479SDavid Howells 	err = -ENOMEM;
6561da177e4SLinus Torvalds 	goto out_fail;
6571da177e4SLinus Torvalds out_oversize:
658e87cc472SJoe Perches 	net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
6591da177e4SLinus Torvalds out_fail:
660b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
6611706d587SHerbert Xu 	return err;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
66519bcf9f2SEric W. Biederman int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
6661da177e4SLinus Torvalds {
6679972f134SDavid Ahern 	struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
668385add90SDavid Ahern 	int vif = l3mdev_master_ifindex_rcu(dev);
6691da177e4SLinus Torvalds 	struct ipq *qp;
6701da177e4SLinus Torvalds 
671b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMREQDS);
6728282f274SJoe Stringer 	skb_orphan(skb);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
6759972f134SDavid Ahern 	qp = ip_find(net, ip_hdr(skb), user, vif);
67600db4124SIan Morris 	if (qp) {
6771706d587SHerbert Xu 		int ret;
6781da177e4SLinus Torvalds 
6795ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
6801da177e4SLinus Torvalds 
6811706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
6821da177e4SLinus Torvalds 
6835ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
6844b6cb5d8SPavel Emelyanov 		ipq_put(qp);
685776c729eSHerbert Xu 		return ret;
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 
688b45386efSEric Dumazet 	__IP_INC_STATS(net, IPSTATS_MIB_REASMFAILS);
6891da177e4SLinus Torvalds 	kfree_skb(skb);
690776c729eSHerbert Xu 	return -ENOMEM;
6911da177e4SLinus Torvalds }
6924bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_defrag);
6931da177e4SLinus Torvalds 
69419bcf9f2SEric W. Biederman struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
695bc416d97SEric Dumazet {
6961bf3751eSJohannes Berg 	struct iphdr iph;
6973e32e733SAlexander Drozdov 	int netoff;
698bc416d97SEric Dumazet 	u32 len;
699bc416d97SEric Dumazet 
700bc416d97SEric Dumazet 	if (skb->protocol != htons(ETH_P_IP))
701bc416d97SEric Dumazet 		return skb;
702bc416d97SEric Dumazet 
7033e32e733SAlexander Drozdov 	netoff = skb_network_offset(skb);
7043e32e733SAlexander Drozdov 
7053e32e733SAlexander Drozdov 	if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
706bc416d97SEric Dumazet 		return skb;
707bc416d97SEric Dumazet 
7081bf3751eSJohannes Berg 	if (iph.ihl < 5 || iph.version != 4)
709bc416d97SEric Dumazet 		return skb;
710bc416d97SEric Dumazet 
7111bf3751eSJohannes Berg 	len = ntohs(iph.tot_len);
7123e32e733SAlexander Drozdov 	if (skb->len < netoff + len || len < (iph.ihl * 4))
7131bf3751eSJohannes Berg 		return skb;
7141bf3751eSJohannes Berg 
7151bf3751eSJohannes Berg 	if (ip_is_fragment(&iph)) {
716bc416d97SEric Dumazet 		skb = skb_share_check(skb, GFP_ATOMIC);
717bc416d97SEric Dumazet 		if (skb) {
7183e32e733SAlexander Drozdov 			if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
7191bf3751eSJohannes Berg 				return skb;
7203e32e733SAlexander Drozdov 			if (pskb_trim_rcsum(skb, netoff + len))
721bc416d97SEric Dumazet 				return skb;
722bc416d97SEric Dumazet 			memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
72319bcf9f2SEric W. Biederman 			if (ip_defrag(net, skb, user))
724bc416d97SEric Dumazet 				return NULL;
7257539fadcSTom Herbert 			skb_clear_hash(skb);
726bc416d97SEric Dumazet 		}
727bc416d97SEric Dumazet 	}
728bc416d97SEric Dumazet 	return skb;
729bc416d97SEric Dumazet }
730bc416d97SEric Dumazet EXPORT_SYMBOL(ip_check_defrag);
731bc416d97SEric Dumazet 
7328d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
7338d8354d2SPavel Emelyanov static int zero;
7348d8354d2SPavel Emelyanov 
7350a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = {
7368d8354d2SPavel Emelyanov 	{
7378d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
738e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.high_thresh,
7398d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7408d8354d2SPavel Emelyanov 		.mode		= 0644,
7411bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
7421bab4c75SNikolay Aleksandrov 		.extra1		= &init_net.ipv4.frags.low_thresh
7438d8354d2SPavel Emelyanov 	},
7448d8354d2SPavel Emelyanov 	{
7458d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
746e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.low_thresh,
7478d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7488d8354d2SPavel Emelyanov 		.mode		= 0644,
7491bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
7501bab4c75SNikolay Aleksandrov 		.extra1		= &zero,
7511bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv4.frags.high_thresh
7528d8354d2SPavel Emelyanov 	},
7538d8354d2SPavel Emelyanov 	{
7548d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
755b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv4.frags.timeout,
7568d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7578d8354d2SPavel Emelyanov 		.mode		= 0644,
7586d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
7598d8354d2SPavel Emelyanov 	},
7600fbf4cb2SNikolay Borisov 	{
7610fbf4cb2SNikolay Borisov 		.procname	= "ipfrag_max_dist",
7620fbf4cb2SNikolay Borisov 		.data		= &init_net.ipv4.frags.max_dist,
7630fbf4cb2SNikolay Borisov 		.maxlen		= sizeof(int),
7640fbf4cb2SNikolay Borisov 		.mode		= 0644,
7650fbf4cb2SNikolay Borisov 		.proc_handler	= proc_dointvec_minmax,
7660fbf4cb2SNikolay Borisov 		.extra1		= &zero
7670fbf4cb2SNikolay Borisov 	},
7687d291ebbSPavel Emelyanov 	{ }
7697d291ebbSPavel Emelyanov };
7707d291ebbSPavel Emelyanov 
771e3a57d18SFlorian Westphal /* secret interval has been deprecated */
772e3a57d18SFlorian Westphal static int ip4_frags_secret_interval_unused;
7737d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
7748d8354d2SPavel Emelyanov 	{
7758d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
776e3a57d18SFlorian Westphal 		.data		= &ip4_frags_secret_interval_unused,
7778d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7788d8354d2SPavel Emelyanov 		.mode		= 0644,
7796d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
7808d8354d2SPavel Emelyanov 	},
7818d8354d2SPavel Emelyanov 	{ }
7828d8354d2SPavel Emelyanov };
7838d8354d2SPavel Emelyanov 
7842c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net)
7858d8354d2SPavel Emelyanov {
786e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
7878d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
7888d8354d2SPavel Emelyanov 
7890a64b4b8SPavel Emelyanov 	table = ip4_frags_ns_ctl_table;
79009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
7910a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
79251456b29SIan Morris 		if (!table)
793e4a2d5c2SPavel Emelyanov 			goto err_alloc;
794e4a2d5c2SPavel Emelyanov 
795e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv4.frags.high_thresh;
7961bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv4.frags.low_thresh;
7971bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv4.frags.high_thresh;
798e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv4.frags.low_thresh;
7991bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv4.frags.high_thresh;
800b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv4.frags.timeout;
8010fbf4cb2SNikolay Borisov 		table[3].data = &net->ipv4.frags.max_dist;
802e4a2d5c2SPavel Emelyanov 	}
803e4a2d5c2SPavel Emelyanov 
804ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv4", table);
80551456b29SIan Morris 	if (!hdr)
806e4a2d5c2SPavel Emelyanov 		goto err_reg;
807e4a2d5c2SPavel Emelyanov 
808e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
809e4a2d5c2SPavel Emelyanov 	return 0;
810e4a2d5c2SPavel Emelyanov 
811e4a2d5c2SPavel Emelyanov err_reg:
81209ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
813e4a2d5c2SPavel Emelyanov 		kfree(table);
814e4a2d5c2SPavel Emelyanov err_alloc:
815e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
816e4a2d5c2SPavel Emelyanov }
817e4a2d5c2SPavel Emelyanov 
8182c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
819e4a2d5c2SPavel Emelyanov {
820e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
821e4a2d5c2SPavel Emelyanov 
822e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
823e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
824e4a2d5c2SPavel Emelyanov 	kfree(table);
8258d8354d2SPavel Emelyanov }
8267d291ebbSPavel Emelyanov 
82757a02c39SFabian Frederick static void __init ip4_frags_ctl_register(void)
8287d291ebbSPavel Emelyanov {
82943444757SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
8307d291ebbSPavel Emelyanov }
8318d8354d2SPavel Emelyanov #else
832aa1f731eSFabian Frederick static int ip4_frags_ns_ctl_register(struct net *net)
8338d8354d2SPavel Emelyanov {
8348d8354d2SPavel Emelyanov 	return 0;
8358d8354d2SPavel Emelyanov }
836e4a2d5c2SPavel Emelyanov 
837aa1f731eSFabian Frederick static void ip4_frags_ns_ctl_unregister(struct net *net)
838e4a2d5c2SPavel Emelyanov {
839e4a2d5c2SPavel Emelyanov }
8407d291ebbSPavel Emelyanov 
841aa1f731eSFabian Frederick static void __init ip4_frags_ctl_register(void)
8427d291ebbSPavel Emelyanov {
8437d291ebbSPavel Emelyanov }
8448d8354d2SPavel Emelyanov #endif
8458d8354d2SPavel Emelyanov 
8462c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net)
8478d8354d2SPavel Emelyanov {
848c2a93660SJesper Dangaard Brouer 	/* Fragment cache limits.
849c2a93660SJesper Dangaard Brouer 	 *
850c2a93660SJesper Dangaard Brouer 	 * The fragment memory accounting code, (tries to) account for
851c2a93660SJesper Dangaard Brouer 	 * the real memory usage, by measuring both the size of frag
852c2a93660SJesper Dangaard Brouer 	 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
853c2a93660SJesper Dangaard Brouer 	 * and the SKB's truesize.
854c2a93660SJesper Dangaard Brouer 	 *
855c2a93660SJesper Dangaard Brouer 	 * A 64K fragment consumes 129736 bytes (44*2944)+200
856c2a93660SJesper Dangaard Brouer 	 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
857c2a93660SJesper Dangaard Brouer 	 *
858c2a93660SJesper Dangaard Brouer 	 * We will commit 4MB at one time. Should we cross that limit
859c2a93660SJesper Dangaard Brouer 	 * we will prune down to 3MB, making room for approx 8 big 64K
860c2a93660SJesper Dangaard Brouer 	 * fragments 8x128k.
861e31e0bdcSPavel Emelyanov 	 */
862c2a93660SJesper Dangaard Brouer 	net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
863c2a93660SJesper Dangaard Brouer 	net->ipv4.frags.low_thresh  = 3 * 1024 * 1024;
864e31e0bdcSPavel Emelyanov 	/*
865b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
866b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
867b2fd5321SPavel Emelyanov 	 * by TTL.
868b2fd5321SPavel Emelyanov 	 */
869b2fd5321SPavel Emelyanov 	net->ipv4.frags.timeout = IP_FRAG_TIME;
870b2fd5321SPavel Emelyanov 
8710fbf4cb2SNikolay Borisov 	net->ipv4.frags.max_dist = 64;
8720fbf4cb2SNikolay Borisov 
8735a63643eSJesper Dangaard Brouer 	inet_frags_init_net(&net->ipv4.frags);
8745a63643eSJesper Dangaard Brouer 
8755a63643eSJesper Dangaard Brouer 	return ip4_frags_ns_ctl_register(net);
8768d8354d2SPavel Emelyanov }
8778d8354d2SPavel Emelyanov 
8782c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net)
87981566e83SPavel Emelyanov {
8800a64b4b8SPavel Emelyanov 	ip4_frags_ns_ctl_unregister(net);
88181566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
88281566e83SPavel Emelyanov }
88381566e83SPavel Emelyanov 
88481566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
88581566e83SPavel Emelyanov 	.init = ipv4_frags_init_net,
88681566e83SPavel Emelyanov 	.exit = ipv4_frags_exit_net,
88781566e83SPavel Emelyanov };
88881566e83SPavel Emelyanov 
889b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
8901da177e4SLinus Torvalds {
8917d291ebbSPavel Emelyanov 	ip4_frags_ctl_register();
89281566e83SPavel Emelyanov 	register_pernet_subsys(&ip4_frags_ops);
893321a3a99SPavel Emelyanov 	ip4_frags.hashfn = ip4_hashfn;
894c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
8951e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
8961e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
897abd6523dSPavel Emelyanov 	ip4_frags.match = ip4_frag_match;
898e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
899d4ad4d22SNikolay Aleksandrov 	ip4_frags.frags_cache_name = ip_frag_cache_name;
900d4ad4d22SNikolay Aleksandrov 	if (inet_frags_init(&ip4_frags))
901d4ad4d22SNikolay Aleksandrov 		panic("IP: failed to allocate ip4_frags cache\n");
9021da177e4SLinus Torvalds }
903