xref: /openbmc/linux/net/ipv4/ip_fragment.c (revision 8282f274)
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  */
571da177e4SLinus Torvalds 
588d8354d2SPavel Emelyanov static int sysctl_ipfrag_max_dist __read_mostly = 64;
59d4ad4d22SNikolay Aleksandrov static const char ip_frag_cache_name[] = "ip4-frags";
6089cee8b1SHerbert Xu 
611da177e4SLinus Torvalds struct ipfrag_skb_cb
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	struct inet_skb_parm	h;
641da177e4SLinus Torvalds 	int			offset;
651da177e4SLinus Torvalds };
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #define FRAG_CB(skb)	((struct ipfrag_skb_cb *)((skb)->cb))
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */
701da177e4SLinus Torvalds struct ipq {
715ab11c98SPavel Emelyanov 	struct inet_frag_queue q;
725ab11c98SPavel Emelyanov 
731da177e4SLinus Torvalds 	u32		user;
7418277770SAl Viro 	__be32		saddr;
7518277770SAl Viro 	__be32		daddr;
7618277770SAl Viro 	__be16		id;
771da177e4SLinus Torvalds 	u8		protocol;
786623e3b2SEric Dumazet 	u8		ecn; /* RFC3168 support */
79d6b915e2SFlorian Westphal 	u16		max_df_size; /* largest frag with DF set seen */
8089cee8b1SHerbert Xu 	int             iif;
81385add90SDavid Ahern 	int             vif;   /* L3 master device index */
8289cee8b1SHerbert Xu 	unsigned int    rid;
8389cee8b1SHerbert Xu 	struct inet_peer *peer;
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
86aa1f731eSFabian Frederick static u8 ip4_frag_ecn(u8 tos)
876623e3b2SEric Dumazet {
885173cc05SEric Dumazet 	return 1 << (tos & INET_ECN_MASK);
896623e3b2SEric Dumazet }
906623e3b2SEric Dumazet 
917eb95156SPavel Emelyanov static struct inet_frags ip4_frags;
921da177e4SLinus Torvalds 
936ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net)
947eb95156SPavel Emelyanov {
95d433673eSJesper Dangaard Brouer 	return sum_frag_mem_limit(&net->ipv4.frags);
967eb95156SPavel Emelyanov }
971da177e4SLinus Torvalds 
981706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
991706d587SHerbert Xu 			 struct net_device *dev);
1001706d587SHerbert Xu 
101c6fda282SPavel Emelyanov struct ip4_create_arg {
102c6fda282SPavel Emelyanov 	struct iphdr *iph;
103c6fda282SPavel Emelyanov 	u32 user;
1049972f134SDavid Ahern 	int vif;
105c6fda282SPavel Emelyanov };
106c6fda282SPavel Emelyanov 
10718277770SAl Viro static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
1081da177e4SLinus Torvalds {
109e7b519baSHannes Frederic Sowa 	net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
11018277770SAl Viro 	return jhash_3words((__force u32)id << 16 | prot,
11118277770SAl Viro 			    (__force u32)saddr, (__force u32)daddr,
112fb3cfe6eSFlorian Westphal 			    ip4_frags.rnd);
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
11536c77782SFlorian Westphal static unsigned int ip4_hashfn(const struct inet_frag_queue *q)
1161da177e4SLinus Torvalds {
11736c77782SFlorian Westphal 	const struct ipq *ipq;
1181da177e4SLinus Torvalds 
119321a3a99SPavel Emelyanov 	ipq = container_of(q, struct ipq, q);
120321a3a99SPavel Emelyanov 	return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
12336c77782SFlorian Westphal static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
124abd6523dSPavel Emelyanov {
12536c77782SFlorian Westphal 	const struct ipq *qp;
12636c77782SFlorian Westphal 	const struct ip4_create_arg *arg = a;
127abd6523dSPavel Emelyanov 
128abd6523dSPavel Emelyanov 	qp = container_of(q, struct ipq, q);
129a02cec21SEric Dumazet 	return	qp->id == arg->iph->id &&
130abd6523dSPavel Emelyanov 		qp->saddr == arg->iph->saddr &&
131abd6523dSPavel Emelyanov 		qp->daddr == arg->iph->daddr &&
132abd6523dSPavel Emelyanov 		qp->protocol == arg->iph->protocol &&
1339972f134SDavid Ahern 		qp->user == arg->user &&
1349972f134SDavid Ahern 		qp->vif == arg->vif;
135abd6523dSPavel Emelyanov }
136abd6523dSPavel Emelyanov 
13736c77782SFlorian Westphal static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
138c6fda282SPavel Emelyanov {
139c6fda282SPavel Emelyanov 	struct ipq *qp = container_of(q, struct ipq, q);
14054db0cc2SGao feng 	struct netns_ipv4 *ipv4 = container_of(q->net, struct netns_ipv4,
14154db0cc2SGao feng 					       frags);
14254db0cc2SGao feng 	struct net *net = container_of(ipv4, struct net, ipv4);
14354db0cc2SGao feng 
14436c77782SFlorian Westphal 	const struct ip4_create_arg *arg = a;
145c6fda282SPavel Emelyanov 
146c6fda282SPavel Emelyanov 	qp->protocol = arg->iph->protocol;
147c6fda282SPavel Emelyanov 	qp->id = arg->iph->id;
1486623e3b2SEric Dumazet 	qp->ecn = ip4_frag_ecn(arg->iph->tos);
149c6fda282SPavel Emelyanov 	qp->saddr = arg->iph->saddr;
150c6fda282SPavel Emelyanov 	qp->daddr = arg->iph->daddr;
1519972f134SDavid Ahern 	qp->vif = arg->vif;
152c6fda282SPavel Emelyanov 	qp->user = arg->user;
153c6fda282SPavel Emelyanov 	qp->peer = sysctl_ipfrag_max_dist ?
154192132b9SDavid Ahern 		inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, arg->vif, 1) :
155192132b9SDavid Ahern 		NULL;
156c6fda282SPavel Emelyanov }
157c6fda282SPavel Emelyanov 
158aa1f731eSFabian Frederick static void ip4_frag_free(struct inet_frag_queue *q)
1591da177e4SLinus Torvalds {
1601e4b8287SPavel Emelyanov 	struct ipq *qp;
1611e4b8287SPavel Emelyanov 
1621e4b8287SPavel Emelyanov 	qp = container_of(q, struct ipq, q);
1631e4b8287SPavel Emelyanov 	if (qp->peer)
1641e4b8287SPavel Emelyanov 		inet_putpeer(qp->peer);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* Destruction primitives. */
1691da177e4SLinus Torvalds 
170aa1f731eSFabian Frederick static void ipq_put(struct ipq *ipq)
1711da177e4SLinus Torvalds {
172762cc408SPavel Emelyanov 	inet_frag_put(&ipq->q, &ip4_frags);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately,
1761da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq)
1791da177e4SLinus Torvalds {
180277e650dSPavel Emelyanov 	inet_frag_kill(&ipq->q, &ip4_frags);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1835cf42280SAndy Zhou static bool frag_expire_skip_icmp(u32 user)
1845cf42280SAndy Zhou {
1855cf42280SAndy Zhou 	return user == IP_DEFRAG_AF_PACKET ||
1865cf42280SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_IN,
1878bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_IN_END) ||
1888bc04864SAndy Zhou 	       ip_defrag_user_in_between(user, IP_DEFRAG_CONNTRACK_BRIDGE_IN,
1898bc04864SAndy Zhou 					 __IP_DEFRAG_CONNTRACK_BRIDGE_IN);
1905cf42280SAndy Zhou }
1915cf42280SAndy Zhou 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static void ip_expire(unsigned long arg)
1961da177e4SLinus Torvalds {
197e521db9dSPavel Emelyanov 	struct ipq *qp;
19884a3aa00SPavel Emelyanov 	struct net *net;
199e521db9dSPavel Emelyanov 
200e521db9dSPavel Emelyanov 	qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
20184a3aa00SPavel Emelyanov 	net = container_of(qp->q.net, struct net, ipv4.frags);
2021da177e4SLinus Torvalds 
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);
2097c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
2101da177e4SLinus Torvalds 
211caaecdd3SNikolay Aleksandrov 	if (!inet_frag_evicting(&qp->q)) {
2125ab11c98SPavel Emelyanov 		struct sk_buff *head = qp->q.fragments;
21364f3b9e2SEric Dumazet 		const struct iphdr *iph;
21464f3b9e2SEric Dumazet 		int err;
215cb84663eSDenis V. Lunev 
2162e404f63SNikolay Aleksandrov 		IP_INC_STATS_BH(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 		rcu_read_lock();
22269df9d59SEric Dumazet 		head->dev = dev_get_by_index_rcu(net, qp->iif);
223e9017b55SShan Wei 		if (!head->dev)
224e9017b55SShan Wei 			goto out_rcu_unlock;
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)
231e9017b55SShan Wei 			goto out_rcu_unlock;
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))
238e9017b55SShan Wei 			goto out_rcu_unlock;
239e9017b55SShan Wei 
240e9017b55SShan Wei 		/* Send an ICMP "Fragment Reassembly Timeout" message. */
241e9017b55SShan Wei 		icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
242e9017b55SShan Wei out_rcu_unlock:
243e9017b55SShan Wei 		rcu_read_unlock();
244d1c9ae6dSPatrick McHardy 	}
2451da177e4SLinus Torvalds out:
2465ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
2474b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
250abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
251abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
252abd6523dSPavel Emelyanov  */
2539972f134SDavid Ahern static struct ipq *ip_find(struct net *net, struct iphdr *iph,
2549972f134SDavid Ahern 			   u32 user, int vif)
2551da177e4SLinus Torvalds {
256c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
257c6fda282SPavel Emelyanov 	struct ip4_create_arg arg;
258abd6523dSPavel Emelyanov 	unsigned int hash;
2591da177e4SLinus Torvalds 
260c6fda282SPavel Emelyanov 	arg.iph = iph;
261c6fda282SPavel Emelyanov 	arg.user = user;
2629972f134SDavid Ahern 	arg.vif = vif;
2639a375803SPavel Emelyanov 
264abd6523dSPavel Emelyanov 	hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
265c6fda282SPavel Emelyanov 
266ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
2675a3da1feSHannes Frederic Sowa 	if (IS_ERR_OR_NULL(q)) {
2685a3da1feSHannes Frederic Sowa 		inet_frag_maybe_warn_overflow(q, pr_fmt());
2691da177e4SLinus Torvalds 		return NULL;
2701da177e4SLinus Torvalds 	}
2715a3da1feSHannes Frederic Sowa 	return container_of(q, struct ipq, q);
2725a3da1feSHannes Frederic Sowa }
2731da177e4SLinus Torvalds 
27489cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
275aa1f731eSFabian Frederick static int ip_frag_too_far(struct ipq *qp)
27689cee8b1SHerbert Xu {
27789cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
27889cee8b1SHerbert Xu 	unsigned int max = sysctl_ipfrag_max_dist;
27989cee8b1SHerbert Xu 	unsigned int start, end;
28089cee8b1SHerbert Xu 
28189cee8b1SHerbert Xu 	int rc;
28289cee8b1SHerbert Xu 
28389cee8b1SHerbert Xu 	if (!peer || !max)
28489cee8b1SHerbert Xu 		return 0;
28589cee8b1SHerbert Xu 
28689cee8b1SHerbert Xu 	start = qp->rid;
28789cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
28889cee8b1SHerbert Xu 	qp->rid = end;
28989cee8b1SHerbert Xu 
2905ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
29189cee8b1SHerbert Xu 
29289cee8b1SHerbert Xu 	if (rc) {
2937c73a6faSPavel Emelyanov 		struct net *net;
2947c73a6faSPavel Emelyanov 
2957c73a6faSPavel Emelyanov 		net = container_of(qp->q.net, struct net, ipv4.frags);
2967c73a6faSPavel Emelyanov 		IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
29789cee8b1SHerbert Xu 	}
29889cee8b1SHerbert Xu 
29989cee8b1SHerbert Xu 	return rc;
30089cee8b1SHerbert Xu }
30189cee8b1SHerbert Xu 
30289cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
30389cee8b1SHerbert Xu {
30489cee8b1SHerbert Xu 	struct sk_buff *fp;
305d433673eSJesper Dangaard Brouer 	unsigned int sum_truesize = 0;
30689cee8b1SHerbert Xu 
307b2fd5321SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
3085ab11c98SPavel Emelyanov 		atomic_inc(&qp->q.refcnt);
30989cee8b1SHerbert Xu 		return -ETIMEDOUT;
31089cee8b1SHerbert Xu 	}
31189cee8b1SHerbert Xu 
3125ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
31389cee8b1SHerbert Xu 	do {
31489cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
315d433673eSJesper Dangaard Brouer 
316d433673eSJesper Dangaard Brouer 		sum_truesize += fp->truesize;
317d433673eSJesper Dangaard Brouer 		kfree_skb(fp);
31889cee8b1SHerbert Xu 		fp = xp;
31989cee8b1SHerbert Xu 	} while (fp);
3200e60d245SFlorian Westphal 	sub_frag_mem_limit(qp->q.net, sum_truesize);
32189cee8b1SHerbert Xu 
32206aa8b8aSNikolay Aleksandrov 	qp->q.flags = 0;
3235ab11c98SPavel Emelyanov 	qp->q.len = 0;
3245ab11c98SPavel Emelyanov 	qp->q.meat = 0;
3255ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
326d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
32789cee8b1SHerbert Xu 	qp->iif = 0;
3286623e3b2SEric Dumazet 	qp->ecn = 0;
32989cee8b1SHerbert Xu 
33089cee8b1SHerbert Xu 	return 0;
33189cee8b1SHerbert Xu }
33289cee8b1SHerbert Xu 
3331da177e4SLinus Torvalds /* Add new segment to existing queue. */
3341706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
3371706d587SHerbert Xu 	struct net_device *dev;
338d6b915e2SFlorian Westphal 	unsigned int fragsize;
3391da177e4SLinus Torvalds 	int flags, offset;
3401da177e4SLinus Torvalds 	int ihl, end;
3411706d587SHerbert Xu 	int err = -ENOENT;
3426623e3b2SEric Dumazet 	u8 ecn;
3431da177e4SLinus Torvalds 
34406aa8b8aSNikolay Aleksandrov 	if (qp->q.flags & INET_FRAG_COMPLETE)
3451da177e4SLinus Torvalds 		goto err;
3461da177e4SLinus Torvalds 
34789cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3481706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3491706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
35089cee8b1SHerbert Xu 		ipq_kill(qp);
35189cee8b1SHerbert Xu 		goto err;
35289cee8b1SHerbert Xu 	}
35389cee8b1SHerbert Xu 
3546623e3b2SEric Dumazet 	ecn = ip4_frag_ecn(ip_hdr(skb)->tos);
355eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3561da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3571da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3581da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
359c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3620848f642SEdward Hyunkoo Jee 	end = offset + skb->len - skb_network_offset(skb) - ihl;
3631706d587SHerbert Xu 	err = -EINVAL;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* Is this the final fragment? */
3661da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3671da177e4SLinus Torvalds 		/* If we already have some bits beyond end
36842b2aa86SJustin P. Mattock 		 * or have different end, the segment is corrupted.
3691da177e4SLinus Torvalds 		 */
3705ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
37106aa8b8aSNikolay Aleksandrov 		    ((qp->q.flags & INET_FRAG_LAST_IN) && end != qp->q.len))
3721da177e4SLinus Torvalds 			goto err;
37306aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_LAST_IN;
3745ab11c98SPavel Emelyanov 		qp->q.len = end;
3751da177e4SLinus Torvalds 	} else {
3761da177e4SLinus Torvalds 		if (end&7) {
3771da177e4SLinus Torvalds 			end &= ~7;
3781da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3791da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3801da177e4SLinus Torvalds 		}
3815ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3821da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
38306aa8b8aSNikolay Aleksandrov 			if (qp->q.flags & INET_FRAG_LAST_IN)
3841da177e4SLinus Torvalds 				goto err;
3855ab11c98SPavel Emelyanov 			qp->q.len = end;
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 	if (end == offset)
3891da177e4SLinus Torvalds 		goto err;
3901da177e4SLinus Torvalds 
3911706d587SHerbert Xu 	err = -ENOMEM;
3920848f642SEdward Hyunkoo Jee 	if (!pskb_pull(skb, skb_network_offset(skb) + ihl))
3931da177e4SLinus Torvalds 		goto err;
3941706d587SHerbert Xu 
3951706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3961706d587SHerbert Xu 	if (err)
3971da177e4SLinus Torvalds 		goto err;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
4001da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
4011da177e4SLinus Torvalds 	 * this fragment, right?
4021da177e4SLinus Torvalds 	 */
403d6bebca9SChangli Gao 	prev = qp->q.fragments_tail;
404d6bebca9SChangli Gao 	if (!prev || FRAG_CB(prev)->offset < offset) {
405d6bebca9SChangli Gao 		next = NULL;
406d6bebca9SChangli Gao 		goto found;
407d6bebca9SChangli Gao 	}
4081da177e4SLinus Torvalds 	prev = NULL;
4095ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
4101da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
4111da177e4SLinus Torvalds 			break;	/* bingo! */
4121da177e4SLinus Torvalds 		prev = next;
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds 
415d6bebca9SChangli Gao found:
4161da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
4171da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
4181da177e4SLinus Torvalds 	 * any overlaps are eliminated.
4191da177e4SLinus Torvalds 	 */
4201da177e4SLinus Torvalds 	if (prev) {
4211da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		if (i > 0) {
4241da177e4SLinus Torvalds 			offset += i;
4251706d587SHerbert Xu 			err = -EINVAL;
4261da177e4SLinus Torvalds 			if (end <= offset)
4271da177e4SLinus Torvalds 				goto err;
4281706d587SHerbert Xu 			err = -ENOMEM;
4291da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
4301da177e4SLinus Torvalds 				goto err;
4311da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
4321da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361706d587SHerbert Xu 	err = -ENOMEM;
4371706d587SHerbert Xu 
4381da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
4391da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		if (i < next->len) {
4421da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
4431da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
4441da177e4SLinus Torvalds 			 */
4451da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
4461da177e4SLinus Torvalds 				goto err;
4471da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4485ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4491da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4501da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4511da177e4SLinus Torvalds 			break;
4521da177e4SLinus Torvalds 		} else {
4531da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4541da177e4SLinus Torvalds 
45547c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4561da177e4SLinus Torvalds 			 * new one drop it.
4571da177e4SLinus Torvalds 			 */
4581da177e4SLinus Torvalds 			next = next->next;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 			if (prev)
4611da177e4SLinus Torvalds 				prev->next = next;
4621da177e4SLinus Torvalds 			else
4635ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4641da177e4SLinus Torvalds 
4655ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4660e60d245SFlorian Westphal 			sub_frag_mem_limit(qp->q.net, free_it->truesize);
467d433673eSJesper Dangaard Brouer 			kfree_skb(free_it);
4681da177e4SLinus Torvalds 		}
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4741da177e4SLinus Torvalds 	skb->next = next;
475d6bebca9SChangli Gao 	if (!next)
476d6bebca9SChangli Gao 		qp->q.fragments_tail = skb;
4771da177e4SLinus Torvalds 	if (prev)
4781da177e4SLinus Torvalds 		prev->next = skb;
4791da177e4SLinus Torvalds 	else
4805ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4811da177e4SLinus Torvalds 
4821706d587SHerbert Xu 	dev = skb->dev;
4831706d587SHerbert Xu 	if (dev) {
4841706d587SHerbert Xu 		qp->iif = dev->ifindex;
4851da177e4SLinus Torvalds 		skb->dev = NULL;
4861706d587SHerbert Xu 	}
4875ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4885ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4896623e3b2SEric Dumazet 	qp->ecn |= ecn;
4900e60d245SFlorian Westphal 	add_frag_mem_limit(qp->q.net, skb->truesize);
4911da177e4SLinus Torvalds 	if (offset == 0)
49206aa8b8aSNikolay Aleksandrov 		qp->q.flags |= INET_FRAG_FIRST_IN;
4931da177e4SLinus Torvalds 
494d6b915e2SFlorian Westphal 	fragsize = skb->len + ihl;
495d6b915e2SFlorian Westphal 
496d6b915e2SFlorian Westphal 	if (fragsize > qp->q.max_size)
497d6b915e2SFlorian Westphal 		qp->q.max_size = fragsize;
498d6b915e2SFlorian Westphal 
4995f2d04f1SPatrick McHardy 	if (ip_hdr(skb)->frag_off & htons(IP_DF) &&
500d6b915e2SFlorian Westphal 	    fragsize > qp->max_df_size)
501d6b915e2SFlorian Westphal 		qp->max_df_size = fragsize;
5025f2d04f1SPatrick McHardy 
50306aa8b8aSNikolay Aleksandrov 	if (qp->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
50497599dc7SEric Dumazet 	    qp->q.meat == qp->q.len) {
50597599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
5061706d587SHerbert Xu 
50797599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
50897599dc7SEric Dumazet 		err = ip_frag_reasm(qp, prev, dev);
50997599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
51097599dc7SEric Dumazet 		return err;
51197599dc7SEric Dumazet 	}
51297599dc7SEric Dumazet 
51397599dc7SEric Dumazet 	skb_dst_drop(skb);
5141706d587SHerbert Xu 	return -EINPROGRESS;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds err:
5171da177e4SLinus Torvalds 	kfree_skb(skb);
5181706d587SHerbert Xu 	return err;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
5231da177e4SLinus Torvalds 
5241706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
5251706d587SHerbert Xu 			 struct net_device *dev)
5261da177e4SLinus Torvalds {
5272bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
5281da177e4SLinus Torvalds 	struct iphdr *iph;
5295ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
5301da177e4SLinus Torvalds 	int len;
5311da177e4SLinus Torvalds 	int ihlen;
5321706d587SHerbert Xu 	int err;
5335173cc05SEric Dumazet 	u8 ecn;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	ipq_kill(qp);
5361da177e4SLinus Torvalds 
537be991971SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[qp->ecn];
5385173cc05SEric Dumazet 	if (unlikely(ecn == 0xff)) {
5395173cc05SEric Dumazet 		err = -EINVAL;
5405173cc05SEric Dumazet 		goto out_fail;
5415173cc05SEric Dumazet 	}
5421706d587SHerbert Xu 	/* Make the one we just received the head. */
5431706d587SHerbert Xu 	if (prev) {
5441706d587SHerbert Xu 		head = prev->next;
5451706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
5461706d587SHerbert Xu 		if (!fp)
5471706d587SHerbert Xu 			goto out_nomem;
5481706d587SHerbert Xu 
5491706d587SHerbert Xu 		fp->next = head->next;
550d6bebca9SChangli Gao 		if (!fp->next)
551d6bebca9SChangli Gao 			qp->q.fragments_tail = fp;
5521706d587SHerbert Xu 		prev->next = fp;
5531706d587SHerbert Xu 
5545ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
5555ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
5561706d587SHerbert Xu 
557cbf8f7bbSEric Dumazet 		consume_skb(qp->q.fragments);
5585ab11c98SPavel Emelyanov 		qp->q.fragments = head;
5591706d587SHerbert Xu 	}
5601706d587SHerbert Xu 
56151456b29SIan Morris 	WARN_ON(!head);
562547b792cSIlpo Järvinen 	WARN_ON(FRAG_CB(head)->offset != 0);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
565c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
5665ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
5671da177e4SLinus Torvalds 
5681706d587SHerbert Xu 	err = -E2BIG;
5691da177e4SLinus Torvalds 	if (len > 65535)
5701da177e4SLinus Torvalds 		goto out_oversize;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
57314bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
5741da177e4SLinus Torvalds 		goto out_nomem;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5771da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5781da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
57921dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
5801da177e4SLinus Torvalds 		struct sk_buff *clone;
5811da177e4SLinus Torvalds 		int i, plen = 0;
5821da177e4SLinus Torvalds 
58351456b29SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
58451456b29SIan Morris 		if (!clone)
5851da177e4SLinus Torvalds 			goto out_nomem;
5861da177e4SLinus Torvalds 		clone->next = head->next;
5871da177e4SLinus Torvalds 		head->next = clone;
5881da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
589d7fcf1a5SDavid S. Miller 		skb_frag_list_init(head);
5901da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
5919e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
5921da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5931da177e4SLinus Torvalds 		head->data_len -= clone->len;
5941da177e4SLinus Torvalds 		head->len -= clone->len;
5951da177e4SLinus Torvalds 		clone->csum = 0;
5961da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5970e60d245SFlorian Westphal 		add_frag_mem_limit(qp->q.net, clone->truesize);
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
60014fe22e3SFlorian Westphal 	skb_shinfo(head)->frag_list = head->next;
601d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
6021da177e4SLinus Torvalds 
60314fe22e3SFlorian Westphal 	for (fp=head->next; fp; fp = fp->next) {
60414fe22e3SFlorian Westphal 		head->data_len += fp->len;
60514fe22e3SFlorian Westphal 		head->len += fp->len;
6061da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6071da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
60884fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
6091da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6101da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6111da177e4SLinus Torvalds 	}
6125510b3c2SDavid S. Miller 	sub_frag_mem_limit(qp->q.net, head->truesize);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	head->next = NULL;
6151da177e4SLinus Torvalds 	head->dev = dev;
6165ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
617d6b915e2SFlorian Westphal 	IPCB(head)->frag_max_size = max(qp->max_df_size, qp->q.max_size);
6181da177e4SLinus Torvalds 
619eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
6201da177e4SLinus Torvalds 	iph->tot_len = htons(len);
6215173cc05SEric Dumazet 	iph->tos |= ecn;
622d6b915e2SFlorian Westphal 
623d6b915e2SFlorian Westphal 	/* When we set IP_DF on a refragmented skb we must also force a
624d6b915e2SFlorian Westphal 	 * call to ip_fragment to avoid forwarding a DF-skb of size s while
625d6b915e2SFlorian Westphal 	 * original sender only sent fragments of size f (where f < s).
626d6b915e2SFlorian Westphal 	 *
627d6b915e2SFlorian Westphal 	 * We only set DF/IPSKB_FRAG_PMTU if such DF fragment was the largest
628d6b915e2SFlorian Westphal 	 * frag seen to avoid sending tiny DF-fragments in case skb was built
629d6b915e2SFlorian Westphal 	 * from one very small df-fragment and one large non-df frag.
630d6b915e2SFlorian Westphal 	 */
631d6b915e2SFlorian Westphal 	if (qp->max_df_size == qp->q.max_size) {
632d6b915e2SFlorian Westphal 		IPCB(head)->flags |= IPSKB_FRAG_PMTU;
633d6b915e2SFlorian Westphal 		iph->frag_off = htons(IP_DF);
634d6b915e2SFlorian Westphal 	} else {
635d6b915e2SFlorian Westphal 		iph->frag_off = 0;
636d6b915e2SFlorian Westphal 	}
637d6b915e2SFlorian Westphal 
6380848f642SEdward Hyunkoo Jee 	ip_send_check(iph);
6390848f642SEdward Hyunkoo Jee 
6402bad35b7SJorge Boncompte [DTI2] 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
6415ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
642d6bebca9SChangli Gao 	qp->q.fragments_tail = NULL;
6431706d587SHerbert Xu 	return 0;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds out_nomem:
646ba7a46f1SJoe Perches 	net_dbg_ratelimited("queue_glue: no memory for gluing queue %p\n", qp);
64745542479SDavid Howells 	err = -ENOMEM;
6481da177e4SLinus Torvalds 	goto out_fail;
6491da177e4SLinus Torvalds out_oversize:
650e87cc472SJoe Perches 	net_info_ratelimited("Oversized IP packet from %pI4\n", &qp->saddr);
6511da177e4SLinus Torvalds out_fail:
652bbf31bf1SDavid Ford 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
6531706d587SHerbert Xu 	return err;
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
65719bcf9f2SEric W. Biederman int ip_defrag(struct net *net, struct sk_buff *skb, u32 user)
6581da177e4SLinus Torvalds {
6599972f134SDavid Ahern 	struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
660385add90SDavid Ahern 	int vif = l3mdev_master_ifindex_rcu(dev);
6611da177e4SLinus Torvalds 	struct ipq *qp;
6621da177e4SLinus Torvalds 
6637c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
6648282f274SJoe Stringer 	skb_orphan(skb);
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
6679972f134SDavid Ahern 	qp = ip_find(net, ip_hdr(skb), user, vif);
66800db4124SIan Morris 	if (qp) {
6691706d587SHerbert Xu 		int ret;
6701da177e4SLinus Torvalds 
6715ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
6721da177e4SLinus Torvalds 
6731706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
6741da177e4SLinus Torvalds 
6755ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
6764b6cb5d8SPavel Emelyanov 		ipq_put(qp);
677776c729eSHerbert Xu 		return ret;
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 
6807c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
6811da177e4SLinus Torvalds 	kfree_skb(skb);
682776c729eSHerbert Xu 	return -ENOMEM;
6831da177e4SLinus Torvalds }
6844bc2f18bSEric Dumazet EXPORT_SYMBOL(ip_defrag);
6851da177e4SLinus Torvalds 
68619bcf9f2SEric W. Biederman struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
687bc416d97SEric Dumazet {
6881bf3751eSJohannes Berg 	struct iphdr iph;
6893e32e733SAlexander Drozdov 	int netoff;
690bc416d97SEric Dumazet 	u32 len;
691bc416d97SEric Dumazet 
692bc416d97SEric Dumazet 	if (skb->protocol != htons(ETH_P_IP))
693bc416d97SEric Dumazet 		return skb;
694bc416d97SEric Dumazet 
6953e32e733SAlexander Drozdov 	netoff = skb_network_offset(skb);
6963e32e733SAlexander Drozdov 
6973e32e733SAlexander Drozdov 	if (skb_copy_bits(skb, netoff, &iph, sizeof(iph)) < 0)
698bc416d97SEric Dumazet 		return skb;
699bc416d97SEric Dumazet 
7001bf3751eSJohannes Berg 	if (iph.ihl < 5 || iph.version != 4)
701bc416d97SEric Dumazet 		return skb;
702bc416d97SEric Dumazet 
7031bf3751eSJohannes Berg 	len = ntohs(iph.tot_len);
7043e32e733SAlexander Drozdov 	if (skb->len < netoff + len || len < (iph.ihl * 4))
7051bf3751eSJohannes Berg 		return skb;
7061bf3751eSJohannes Berg 
7071bf3751eSJohannes Berg 	if (ip_is_fragment(&iph)) {
708bc416d97SEric Dumazet 		skb = skb_share_check(skb, GFP_ATOMIC);
709bc416d97SEric Dumazet 		if (skb) {
7103e32e733SAlexander Drozdov 			if (!pskb_may_pull(skb, netoff + iph.ihl * 4))
7111bf3751eSJohannes Berg 				return skb;
7123e32e733SAlexander Drozdov 			if (pskb_trim_rcsum(skb, netoff + len))
713bc416d97SEric Dumazet 				return skb;
714bc416d97SEric Dumazet 			memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
71519bcf9f2SEric W. Biederman 			if (ip_defrag(net, skb, user))
716bc416d97SEric Dumazet 				return NULL;
7177539fadcSTom Herbert 			skb_clear_hash(skb);
718bc416d97SEric Dumazet 		}
719bc416d97SEric Dumazet 	}
720bc416d97SEric Dumazet 	return skb;
721bc416d97SEric Dumazet }
722bc416d97SEric Dumazet EXPORT_SYMBOL(ip_check_defrag);
723bc416d97SEric Dumazet 
7248d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
7258d8354d2SPavel Emelyanov static int zero;
7268d8354d2SPavel Emelyanov 
7270a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = {
7288d8354d2SPavel Emelyanov 	{
7298d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
730e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.high_thresh,
7318d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7328d8354d2SPavel Emelyanov 		.mode		= 0644,
7331bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
7341bab4c75SNikolay Aleksandrov 		.extra1		= &init_net.ipv4.frags.low_thresh
7358d8354d2SPavel Emelyanov 	},
7368d8354d2SPavel Emelyanov 	{
7378d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
738e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.low_thresh,
7398d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7408d8354d2SPavel Emelyanov 		.mode		= 0644,
7411bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
7421bab4c75SNikolay Aleksandrov 		.extra1		= &zero,
7431bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv4.frags.high_thresh
7448d8354d2SPavel Emelyanov 	},
7458d8354d2SPavel Emelyanov 	{
7468d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
747b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv4.frags.timeout,
7488d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7498d8354d2SPavel Emelyanov 		.mode		= 0644,
7506d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
7518d8354d2SPavel Emelyanov 	},
7527d291ebbSPavel Emelyanov 	{ }
7537d291ebbSPavel Emelyanov };
7547d291ebbSPavel Emelyanov 
755e3a57d18SFlorian Westphal /* secret interval has been deprecated */
756e3a57d18SFlorian Westphal static int ip4_frags_secret_interval_unused;
7577d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
7588d8354d2SPavel Emelyanov 	{
7598d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
760e3a57d18SFlorian Westphal 		.data		= &ip4_frags_secret_interval_unused,
7618d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7628d8354d2SPavel Emelyanov 		.mode		= 0644,
7636d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
7648d8354d2SPavel Emelyanov 	},
7658d8354d2SPavel Emelyanov 	{
7668d8354d2SPavel Emelyanov 		.procname	= "ipfrag_max_dist",
7678d8354d2SPavel Emelyanov 		.data		= &sysctl_ipfrag_max_dist,
7688d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
7698d8354d2SPavel Emelyanov 		.mode		= 0644,
7706d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_minmax,
7718d8354d2SPavel Emelyanov 		.extra1		= &zero
7728d8354d2SPavel Emelyanov 	},
7738d8354d2SPavel Emelyanov 	{ }
7748d8354d2SPavel Emelyanov };
7758d8354d2SPavel Emelyanov 
7762c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net)
7778d8354d2SPavel Emelyanov {
778e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
7798d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
7808d8354d2SPavel Emelyanov 
7810a64b4b8SPavel Emelyanov 	table = ip4_frags_ns_ctl_table;
78209ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
7830a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
78451456b29SIan Morris 		if (!table)
785e4a2d5c2SPavel Emelyanov 			goto err_alloc;
786e4a2d5c2SPavel Emelyanov 
787e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv4.frags.high_thresh;
7881bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv4.frags.low_thresh;
7891bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv4.frags.high_thresh;
790e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv4.frags.low_thresh;
7911bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv4.frags.high_thresh;
792b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv4.frags.timeout;
793464dc801SEric W. Biederman 
794464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
795464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
796464dc801SEric W. Biederman 			table[0].procname = NULL;
797e4a2d5c2SPavel Emelyanov 	}
798e4a2d5c2SPavel Emelyanov 
799ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv4", table);
80051456b29SIan Morris 	if (!hdr)
801e4a2d5c2SPavel Emelyanov 		goto err_reg;
802e4a2d5c2SPavel Emelyanov 
803e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
804e4a2d5c2SPavel Emelyanov 	return 0;
805e4a2d5c2SPavel Emelyanov 
806e4a2d5c2SPavel Emelyanov err_reg:
80709ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
808e4a2d5c2SPavel Emelyanov 		kfree(table);
809e4a2d5c2SPavel Emelyanov err_alloc:
810e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
811e4a2d5c2SPavel Emelyanov }
812e4a2d5c2SPavel Emelyanov 
8132c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
814e4a2d5c2SPavel Emelyanov {
815e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
816e4a2d5c2SPavel Emelyanov 
817e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
818e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
819e4a2d5c2SPavel Emelyanov 	kfree(table);
8208d8354d2SPavel Emelyanov }
8217d291ebbSPavel Emelyanov 
82257a02c39SFabian Frederick static void __init ip4_frags_ctl_register(void)
8237d291ebbSPavel Emelyanov {
82443444757SEric W. Biederman 	register_net_sysctl(&init_net, "net/ipv4", ip4_frags_ctl_table);
8257d291ebbSPavel Emelyanov }
8268d8354d2SPavel Emelyanov #else
827aa1f731eSFabian Frederick static int ip4_frags_ns_ctl_register(struct net *net)
8288d8354d2SPavel Emelyanov {
8298d8354d2SPavel Emelyanov 	return 0;
8308d8354d2SPavel Emelyanov }
831e4a2d5c2SPavel Emelyanov 
832aa1f731eSFabian Frederick static void ip4_frags_ns_ctl_unregister(struct net *net)
833e4a2d5c2SPavel Emelyanov {
834e4a2d5c2SPavel Emelyanov }
8357d291ebbSPavel Emelyanov 
836aa1f731eSFabian Frederick static void __init ip4_frags_ctl_register(void)
8377d291ebbSPavel Emelyanov {
8387d291ebbSPavel Emelyanov }
8398d8354d2SPavel Emelyanov #endif
8408d8354d2SPavel Emelyanov 
8412c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net)
8428d8354d2SPavel Emelyanov {
8431d6119baSEric Dumazet 	int res;
8441d6119baSEric Dumazet 
845c2a93660SJesper Dangaard Brouer 	/* Fragment cache limits.
846c2a93660SJesper Dangaard Brouer 	 *
847c2a93660SJesper Dangaard Brouer 	 * The fragment memory accounting code, (tries to) account for
848c2a93660SJesper Dangaard Brouer 	 * the real memory usage, by measuring both the size of frag
849c2a93660SJesper Dangaard Brouer 	 * queue struct (inet_frag_queue (ipv4:ipq/ipv6:frag_queue))
850c2a93660SJesper Dangaard Brouer 	 * and the SKB's truesize.
851c2a93660SJesper Dangaard Brouer 	 *
852c2a93660SJesper Dangaard Brouer 	 * A 64K fragment consumes 129736 bytes (44*2944)+200
853c2a93660SJesper Dangaard Brouer 	 * (1500 truesize == 2944, sizeof(struct ipq) == 200)
854c2a93660SJesper Dangaard Brouer 	 *
855c2a93660SJesper Dangaard Brouer 	 * We will commit 4MB at one time. Should we cross that limit
856c2a93660SJesper Dangaard Brouer 	 * we will prune down to 3MB, making room for approx 8 big 64K
857c2a93660SJesper Dangaard Brouer 	 * fragments 8x128k.
858e31e0bdcSPavel Emelyanov 	 */
859c2a93660SJesper Dangaard Brouer 	net->ipv4.frags.high_thresh = 4 * 1024 * 1024;
860c2a93660SJesper Dangaard Brouer 	net->ipv4.frags.low_thresh  = 3 * 1024 * 1024;
861e31e0bdcSPavel Emelyanov 	/*
862b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
863b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
864b2fd5321SPavel Emelyanov 	 * by TTL.
865b2fd5321SPavel Emelyanov 	 */
866b2fd5321SPavel Emelyanov 	net->ipv4.frags.timeout = IP_FRAG_TIME;
867b2fd5321SPavel Emelyanov 
8681d6119baSEric Dumazet 	res = inet_frags_init_net(&net->ipv4.frags);
8691d6119baSEric Dumazet 	if (res)
8701d6119baSEric Dumazet 		return res;
8711d6119baSEric Dumazet 	res = ip4_frags_ns_ctl_register(net);
8721d6119baSEric Dumazet 	if (res)
8731d6119baSEric Dumazet 		inet_frags_uninit_net(&net->ipv4.frags);
8741d6119baSEric Dumazet 	return res;
8758d8354d2SPavel Emelyanov }
8768d8354d2SPavel Emelyanov 
8772c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net)
87881566e83SPavel Emelyanov {
8790a64b4b8SPavel Emelyanov 	ip4_frags_ns_ctl_unregister(net);
88081566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
88181566e83SPavel Emelyanov }
88281566e83SPavel Emelyanov 
88381566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
88481566e83SPavel Emelyanov 	.init = ipv4_frags_init_net,
88581566e83SPavel Emelyanov 	.exit = ipv4_frags_exit_net,
88681566e83SPavel Emelyanov };
88781566e83SPavel Emelyanov 
888b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
8891da177e4SLinus Torvalds {
8907d291ebbSPavel Emelyanov 	ip4_frags_ctl_register();
89181566e83SPavel Emelyanov 	register_pernet_subsys(&ip4_frags_ops);
892321a3a99SPavel Emelyanov 	ip4_frags.hashfn = ip4_hashfn;
893c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
8941e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
8951e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
896abd6523dSPavel Emelyanov 	ip4_frags.match = ip4_frag_match;
897e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
898d4ad4d22SNikolay Aleksandrov 	ip4_frags.frags_cache_name = ip_frag_cache_name;
899d4ad4d22SNikolay Aleksandrov 	if (inet_frags_init(&ip4_frags))
900d4ad4d22SNikolay Aleksandrov 		panic("IP: failed to allocate ip4_frags cache\n");
9011da177e4SLinus Torvalds }
902