xref: /linux/net/ipv4/ip_fragment.c (revision 45542479fb261342d5244869cf3ca4636b7ffd43)
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  * Version:	$Id: ip_fragment.c,v 1.59 2002/01/12 07:54:56 davem Exp $
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Authors:	Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
111da177e4SLinus Torvalds  *		Alan Cox <Alan.Cox@linux.org>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Fixes:
141da177e4SLinus Torvalds  *		Alan Cox	:	Split from ip.c , see ip_input.c for history.
151da177e4SLinus Torvalds  *		David S. Miller :	Begin massive cleanup...
161da177e4SLinus Torvalds  *		Andi Kleen	:	Add sysctls.
171da177e4SLinus Torvalds  *		xxxx		:	Overlapfrag bug.
181da177e4SLinus Torvalds  *		Ultima          :       ip_expire() kernel panic.
191da177e4SLinus Torvalds  *		Bill Hawes	:	Frag accounting and evictor fixes.
201da177e4SLinus Torvalds  *		John McDonald	:	0 length frag bug.
211da177e4SLinus Torvalds  *		Alexey Kuznetsov:	SMP races, threading, cleanup.
221da177e4SLinus Torvalds  *		Patrick McHardy :	LRU queue of frag heads for evictor.
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds 
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>
371da177e4SLinus Torvalds #include <net/sock.h>
381da177e4SLinus Torvalds #include <net/ip.h>
391da177e4SLinus Torvalds #include <net/icmp.h>
401da177e4SLinus Torvalds #include <net/checksum.h>
4189cee8b1SHerbert Xu #include <net/inetpeer.h>
425ab11c98SPavel Emelyanov #include <net/inet_frag.h>
431da177e4SLinus Torvalds #include <linux/tcp.h>
441da177e4SLinus Torvalds #include <linux/udp.h>
451da177e4SLinus Torvalds #include <linux/inet.h>
461da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
491da177e4SLinus Torvalds  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
501da177e4SLinus Torvalds  * as well. Or notify me, at least. --ANK
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds 
53ab32ea5dSBrian Haley int sysctl_ipfrag_max_dist __read_mostly = 64;
5489cee8b1SHerbert Xu 
551da177e4SLinus Torvalds struct ipfrag_skb_cb
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	struct inet_skb_parm	h;
581da177e4SLinus Torvalds 	int			offset;
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #define FRAG_CB(skb)	((struct ipfrag_skb_cb*)((skb)->cb))
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* Describe an entry in the "incomplete datagrams" queue. */
641da177e4SLinus Torvalds struct ipq {
655ab11c98SPavel Emelyanov 	struct inet_frag_queue q;
665ab11c98SPavel Emelyanov 
671da177e4SLinus Torvalds 	u32		user;
6818277770SAl Viro 	__be32		saddr;
6918277770SAl Viro 	__be32		daddr;
7018277770SAl Viro 	__be16		id;
711da177e4SLinus Torvalds 	u8		protocol;
7289cee8b1SHerbert Xu 	int             iif;
7389cee8b1SHerbert Xu 	unsigned int    rid;
7489cee8b1SHerbert Xu 	struct inet_peer *peer;
751da177e4SLinus Torvalds };
761da177e4SLinus Torvalds 
7704128f23SPavel Emelyanov struct inet_frags_ctl ip4_frags_ctl __read_mostly = {
7804128f23SPavel Emelyanov 	/*
7904128f23SPavel Emelyanov 	 * Fragment cache limits. We will commit 256K at one time. Should we
8004128f23SPavel Emelyanov 	 * cross that limit we will prune down to 192K. This should cope with
8104128f23SPavel Emelyanov 	 * even the most extreme cases without allowing an attacker to
8204128f23SPavel Emelyanov 	 * measurably harm machine performance.
8304128f23SPavel Emelyanov 	 */
8404128f23SPavel Emelyanov 	.high_thresh	 = 256 * 1024,
8504128f23SPavel Emelyanov 	.low_thresh	 = 192 * 1024,
8604128f23SPavel Emelyanov 
8704128f23SPavel Emelyanov 	/*
8804128f23SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
8904128f23SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
9004128f23SPavel Emelyanov 	 * by TTL.
9104128f23SPavel Emelyanov 	 */
9204128f23SPavel Emelyanov 	.timeout	 = IP_FRAG_TIME,
9304128f23SPavel Emelyanov 	.secret_interval = 10 * 60 * HZ,
9404128f23SPavel Emelyanov };
9504128f23SPavel Emelyanov 
967eb95156SPavel Emelyanov static struct inet_frags ip4_frags;
971da177e4SLinus Torvalds 
987eb95156SPavel Emelyanov int ip_frag_nqueues(void)
997eb95156SPavel Emelyanov {
1007eb95156SPavel Emelyanov 	return ip4_frags.nqueues;
1017eb95156SPavel Emelyanov }
1021da177e4SLinus Torvalds 
1037eb95156SPavel Emelyanov int ip_frag_mem(void)
1047eb95156SPavel Emelyanov {
1057eb95156SPavel Emelyanov 	return atomic_read(&ip4_frags.mem);
1067eb95156SPavel Emelyanov }
1071da177e4SLinus Torvalds 
1081706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
1091706d587SHerbert Xu 			 struct net_device *dev);
1101706d587SHerbert Xu 
111c6fda282SPavel Emelyanov struct ip4_create_arg {
112c6fda282SPavel Emelyanov 	struct iphdr *iph;
113c6fda282SPavel Emelyanov 	u32 user;
114c6fda282SPavel Emelyanov };
115c6fda282SPavel Emelyanov 
11618277770SAl Viro static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
1171da177e4SLinus Torvalds {
11818277770SAl Viro 	return jhash_3words((__force u32)id << 16 | prot,
11918277770SAl Viro 			    (__force u32)saddr, (__force u32)daddr,
1207eb95156SPavel Emelyanov 			    ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
123321a3a99SPavel Emelyanov static unsigned int ip4_hashfn(struct inet_frag_queue *q)
1241da177e4SLinus Torvalds {
125321a3a99SPavel Emelyanov 	struct ipq *ipq;
1261da177e4SLinus Torvalds 
127321a3a99SPavel Emelyanov 	ipq = container_of(q, struct ipq, q);
128321a3a99SPavel Emelyanov 	return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
131abd6523dSPavel Emelyanov static int ip4_frag_match(struct inet_frag_queue *q, void *a)
132abd6523dSPavel Emelyanov {
133abd6523dSPavel Emelyanov 	struct ipq *qp;
134abd6523dSPavel Emelyanov 	struct ip4_create_arg *arg = a;
135abd6523dSPavel Emelyanov 
136abd6523dSPavel Emelyanov 	qp = container_of(q, struct ipq, q);
137abd6523dSPavel Emelyanov 	return (qp->id == arg->iph->id &&
138abd6523dSPavel Emelyanov 			qp->saddr == arg->iph->saddr &&
139abd6523dSPavel Emelyanov 			qp->daddr == arg->iph->daddr &&
140abd6523dSPavel Emelyanov 			qp->protocol == arg->iph->protocol &&
141abd6523dSPavel Emelyanov 			qp->user == arg->user);
142abd6523dSPavel Emelyanov }
143abd6523dSPavel Emelyanov 
1441da177e4SLinus Torvalds /* Memory Tracking Functions. */
1451da177e4SLinus Torvalds static __inline__ void frag_kfree_skb(struct sk_buff *skb, int *work)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	if (work)
1481da177e4SLinus Torvalds 		*work -= skb->truesize;
1497eb95156SPavel Emelyanov 	atomic_sub(skb->truesize, &ip4_frags.mem);
1501da177e4SLinus Torvalds 	kfree_skb(skb);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153c6fda282SPavel Emelyanov static void ip4_frag_init(struct inet_frag_queue *q, void *a)
154c6fda282SPavel Emelyanov {
155c6fda282SPavel Emelyanov 	struct ipq *qp = container_of(q, struct ipq, q);
156c6fda282SPavel Emelyanov 	struct ip4_create_arg *arg = a;
157c6fda282SPavel Emelyanov 
158c6fda282SPavel Emelyanov 	qp->protocol = arg->iph->protocol;
159c6fda282SPavel Emelyanov 	qp->id = arg->iph->id;
160c6fda282SPavel Emelyanov 	qp->saddr = arg->iph->saddr;
161c6fda282SPavel Emelyanov 	qp->daddr = arg->iph->daddr;
162c6fda282SPavel Emelyanov 	qp->user = arg->user;
163c6fda282SPavel Emelyanov 	qp->peer = sysctl_ipfrag_max_dist ?
164c6fda282SPavel Emelyanov 		inet_getpeer(arg->iph->saddr, 1) : NULL;
165c6fda282SPavel Emelyanov }
166c6fda282SPavel Emelyanov 
1671e4b8287SPavel Emelyanov static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
1681da177e4SLinus Torvalds {
1691e4b8287SPavel Emelyanov 	struct ipq *qp;
1701e4b8287SPavel Emelyanov 
1711e4b8287SPavel Emelyanov 	qp = container_of(q, struct ipq, q);
1721e4b8287SPavel Emelyanov 	if (qp->peer)
1731e4b8287SPavel Emelyanov 		inet_putpeer(qp->peer);
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /* Destruction primitives. */
1781da177e4SLinus Torvalds 
1794b6cb5d8SPavel Emelyanov static __inline__ void ipq_put(struct ipq *ipq)
1801da177e4SLinus Torvalds {
181762cc408SPavel Emelyanov 	inet_frag_put(&ipq->q, &ip4_frags);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately,
1851da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1861da177e4SLinus Torvalds  */
1871da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq)
1881da177e4SLinus Torvalds {
189277e650dSPavel Emelyanov 	inet_frag_kill(&ipq->q, &ip4_frags);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /* Memory limiting on fragments.  Evictor trashes the oldest
1931da177e4SLinus Torvalds  * fragment queue until we are back under the threshold.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static void ip_evictor(void)
1961da177e4SLinus Torvalds {
1978e7999c4SPavel Emelyanov 	int evicted;
1981da177e4SLinus Torvalds 
1998e7999c4SPavel Emelyanov 	evicted = inet_frag_evictor(&ip4_frags);
2008e7999c4SPavel Emelyanov 	if (evicted)
2018e7999c4SPavel Emelyanov 		IP_ADD_STATS_BH(IPSTATS_MIB_REASMFAILS, evicted);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /*
2051da177e4SLinus Torvalds  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
2061da177e4SLinus Torvalds  */
2071da177e4SLinus Torvalds static void ip_expire(unsigned long arg)
2081da177e4SLinus Torvalds {
209e521db9dSPavel Emelyanov 	struct ipq *qp;
210e521db9dSPavel Emelyanov 
211e521db9dSPavel Emelyanov 	qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
2121da177e4SLinus Torvalds 
2135ab11c98SPavel Emelyanov 	spin_lock(&qp->q.lock);
2141da177e4SLinus Torvalds 
2155ab11c98SPavel Emelyanov 	if (qp->q.last_in & COMPLETE)
2161da177e4SLinus Torvalds 		goto out;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	ipq_kill(qp);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
2211da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
2221da177e4SLinus Torvalds 
2235ab11c98SPavel Emelyanov 	if ((qp->q.last_in&FIRST_IN) && qp->q.fragments != NULL) {
2245ab11c98SPavel Emelyanov 		struct sk_buff *head = qp->q.fragments;
2251da177e4SLinus Torvalds 		/* Send an ICMP "Fragment Reassembly Timeout" message. */
226881d966bSEric W. Biederman 		if ((head->dev = dev_get_by_index(&init_net, qp->iif)) != NULL) {
2271da177e4SLinus Torvalds 			icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
2281da177e4SLinus Torvalds 			dev_put(head->dev);
2291da177e4SLinus Torvalds 		}
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds out:
2325ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
2334b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
236abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
237abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
238abd6523dSPavel Emelyanov  */
239abd6523dSPavel Emelyanov static inline struct ipq *ip_find(struct iphdr *iph, u32 user)
2401da177e4SLinus Torvalds {
241c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
242c6fda282SPavel Emelyanov 	struct ip4_create_arg arg;
243abd6523dSPavel Emelyanov 	unsigned int hash;
2441da177e4SLinus Torvalds 
245c6fda282SPavel Emelyanov 	arg.iph = iph;
246c6fda282SPavel Emelyanov 	arg.user = user;
247abd6523dSPavel Emelyanov 	hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
248c6fda282SPavel Emelyanov 
249abd6523dSPavel Emelyanov 	q = inet_frag_find(&ip4_frags, &arg, hash);
250c6fda282SPavel Emelyanov 	if (q == NULL)
2511da177e4SLinus Torvalds 		goto out_nomem;
2521da177e4SLinus Torvalds 
253c6fda282SPavel Emelyanov 	return container_of(q, struct ipq, q);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds out_nomem:
25664ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
2571da177e4SLinus Torvalds 	return NULL;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
26089cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
26189cee8b1SHerbert Xu static inline int ip_frag_too_far(struct ipq *qp)
26289cee8b1SHerbert Xu {
26389cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
26489cee8b1SHerbert Xu 	unsigned int max = sysctl_ipfrag_max_dist;
26589cee8b1SHerbert Xu 	unsigned int start, end;
26689cee8b1SHerbert Xu 
26789cee8b1SHerbert Xu 	int rc;
26889cee8b1SHerbert Xu 
26989cee8b1SHerbert Xu 	if (!peer || !max)
27089cee8b1SHerbert Xu 		return 0;
27189cee8b1SHerbert Xu 
27289cee8b1SHerbert Xu 	start = qp->rid;
27389cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
27489cee8b1SHerbert Xu 	qp->rid = end;
27589cee8b1SHerbert Xu 
2765ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
27789cee8b1SHerbert Xu 
27889cee8b1SHerbert Xu 	if (rc) {
27989cee8b1SHerbert Xu 		IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
28089cee8b1SHerbert Xu 	}
28189cee8b1SHerbert Xu 
28289cee8b1SHerbert Xu 	return rc;
28389cee8b1SHerbert Xu }
28489cee8b1SHerbert Xu 
28589cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
28689cee8b1SHerbert Xu {
28789cee8b1SHerbert Xu 	struct sk_buff *fp;
28889cee8b1SHerbert Xu 
28904128f23SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + ip4_frags_ctl.timeout)) {
2905ab11c98SPavel Emelyanov 		atomic_inc(&qp->q.refcnt);
29189cee8b1SHerbert Xu 		return -ETIMEDOUT;
29289cee8b1SHerbert Xu 	}
29389cee8b1SHerbert Xu 
2945ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
29589cee8b1SHerbert Xu 	do {
29689cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
29789cee8b1SHerbert Xu 		frag_kfree_skb(fp, NULL);
29889cee8b1SHerbert Xu 		fp = xp;
29989cee8b1SHerbert Xu 	} while (fp);
30089cee8b1SHerbert Xu 
3015ab11c98SPavel Emelyanov 	qp->q.last_in = 0;
3025ab11c98SPavel Emelyanov 	qp->q.len = 0;
3035ab11c98SPavel Emelyanov 	qp->q.meat = 0;
3045ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
30589cee8b1SHerbert Xu 	qp->iif = 0;
30689cee8b1SHerbert Xu 
30789cee8b1SHerbert Xu 	return 0;
30889cee8b1SHerbert Xu }
30989cee8b1SHerbert Xu 
3101da177e4SLinus Torvalds /* Add new segment to existing queue. */
3111706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
3141706d587SHerbert Xu 	struct net_device *dev;
3151da177e4SLinus Torvalds 	int flags, offset;
3161da177e4SLinus Torvalds 	int ihl, end;
3171706d587SHerbert Xu 	int err = -ENOENT;
3181da177e4SLinus Torvalds 
3195ab11c98SPavel Emelyanov 	if (qp->q.last_in & COMPLETE)
3201da177e4SLinus Torvalds 		goto err;
3211da177e4SLinus Torvalds 
32289cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3231706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3241706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
32589cee8b1SHerbert Xu 		ipq_kill(qp);
32689cee8b1SHerbert Xu 		goto err;
32789cee8b1SHerbert Xu 	}
32889cee8b1SHerbert Xu 
329eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3301da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3311da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3321da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
333c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3361da177e4SLinus Torvalds 	end = offset + skb->len - ihl;
3371706d587SHerbert Xu 	err = -EINVAL;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/* Is this the final fragment? */
3401da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3411da177e4SLinus Torvalds 		/* If we already have some bits beyond end
3421da177e4SLinus Torvalds 		 * or have different end, the segment is corrrupted.
3431da177e4SLinus Torvalds 		 */
3445ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
3455ab11c98SPavel Emelyanov 		    ((qp->q.last_in & LAST_IN) && end != qp->q.len))
3461da177e4SLinus Torvalds 			goto err;
3475ab11c98SPavel Emelyanov 		qp->q.last_in |= LAST_IN;
3485ab11c98SPavel Emelyanov 		qp->q.len = end;
3491da177e4SLinus Torvalds 	} else {
3501da177e4SLinus Torvalds 		if (end&7) {
3511da177e4SLinus Torvalds 			end &= ~7;
3521da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3531da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3541da177e4SLinus Torvalds 		}
3555ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3561da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
3575ab11c98SPavel Emelyanov 			if (qp->q.last_in & LAST_IN)
3581da177e4SLinus Torvalds 				goto err;
3595ab11c98SPavel Emelyanov 			qp->q.len = end;
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 	if (end == offset)
3631da177e4SLinus Torvalds 		goto err;
3641da177e4SLinus Torvalds 
3651706d587SHerbert Xu 	err = -ENOMEM;
3661da177e4SLinus Torvalds 	if (pskb_pull(skb, ihl) == NULL)
3671da177e4SLinus Torvalds 		goto err;
3681706d587SHerbert Xu 
3691706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3701706d587SHerbert Xu 	if (err)
3711da177e4SLinus Torvalds 		goto err;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3741da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3751da177e4SLinus Torvalds 	 * this fragment, right?
3761da177e4SLinus Torvalds 	 */
3771da177e4SLinus Torvalds 	prev = NULL;
3785ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
3791da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
3801da177e4SLinus Torvalds 			break;	/* bingo! */
3811da177e4SLinus Torvalds 		prev = next;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
3851da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
3861da177e4SLinus Torvalds 	 * any overlaps are eliminated.
3871da177e4SLinus Torvalds 	 */
3881da177e4SLinus Torvalds 	if (prev) {
3891da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 		if (i > 0) {
3921da177e4SLinus Torvalds 			offset += i;
3931706d587SHerbert Xu 			err = -EINVAL;
3941da177e4SLinus Torvalds 			if (end <= offset)
3951da177e4SLinus Torvalds 				goto err;
3961706d587SHerbert Xu 			err = -ENOMEM;
3971da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
3981da177e4SLinus Torvalds 				goto err;
3991da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
4001da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
4011da177e4SLinus Torvalds 		}
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 
4041706d587SHerbert Xu 	err = -ENOMEM;
4051706d587SHerbert Xu 
4061da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
4071da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		if (i < next->len) {
4101da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
4111da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
4121da177e4SLinus Torvalds 			 */
4131da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
4141da177e4SLinus Torvalds 				goto err;
4151da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4165ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4171da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4181da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4191da177e4SLinus Torvalds 			break;
4201da177e4SLinus Torvalds 		} else {
4211da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4221da177e4SLinus Torvalds 
42347c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4241da177e4SLinus Torvalds 			 * new one drop it.
4251da177e4SLinus Torvalds 			 */
4261da177e4SLinus Torvalds 			next = next->next;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 			if (prev)
4291da177e4SLinus Torvalds 				prev->next = next;
4301da177e4SLinus Torvalds 			else
4315ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4321da177e4SLinus Torvalds 
4335ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4341da177e4SLinus Torvalds 			frag_kfree_skb(free_it, NULL);
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4411da177e4SLinus Torvalds 	skb->next = next;
4421da177e4SLinus Torvalds 	if (prev)
4431da177e4SLinus Torvalds 		prev->next = skb;
4441da177e4SLinus Torvalds 	else
4455ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4461da177e4SLinus Torvalds 
4471706d587SHerbert Xu 	dev = skb->dev;
4481706d587SHerbert Xu 	if (dev) {
4491706d587SHerbert Xu 		qp->iif = dev->ifindex;
4501da177e4SLinus Torvalds 		skb->dev = NULL;
4511706d587SHerbert Xu 	}
4525ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4535ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4547eb95156SPavel Emelyanov 	atomic_add(skb->truesize, &ip4_frags.mem);
4551da177e4SLinus Torvalds 	if (offset == 0)
4565ab11c98SPavel Emelyanov 		qp->q.last_in |= FIRST_IN;
4571da177e4SLinus Torvalds 
4585ab11c98SPavel Emelyanov 	if (qp->q.last_in == (FIRST_IN | LAST_IN) && qp->q.meat == qp->q.len)
4591706d587SHerbert Xu 		return ip_frag_reasm(qp, prev, dev);
4601706d587SHerbert Xu 
4617eb95156SPavel Emelyanov 	write_lock(&ip4_frags.lock);
4627eb95156SPavel Emelyanov 	list_move_tail(&qp->q.lru_list, &ip4_frags.lru_list);
4637eb95156SPavel Emelyanov 	write_unlock(&ip4_frags.lock);
4641706d587SHerbert Xu 	return -EINPROGRESS;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds err:
4671da177e4SLinus Torvalds 	kfree_skb(skb);
4681706d587SHerbert Xu 	return err;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
4731da177e4SLinus Torvalds 
4741706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
4751706d587SHerbert Xu 			 struct net_device *dev)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	struct iphdr *iph;
4785ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
4791da177e4SLinus Torvalds 	int len;
4801da177e4SLinus Torvalds 	int ihlen;
4811706d587SHerbert Xu 	int err;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	ipq_kill(qp);
4841da177e4SLinus Torvalds 
4851706d587SHerbert Xu 	/* Make the one we just received the head. */
4861706d587SHerbert Xu 	if (prev) {
4871706d587SHerbert Xu 		head = prev->next;
4881706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
4891706d587SHerbert Xu 		if (!fp)
4901706d587SHerbert Xu 			goto out_nomem;
4911706d587SHerbert Xu 
4921706d587SHerbert Xu 		fp->next = head->next;
4931706d587SHerbert Xu 		prev->next = fp;
4941706d587SHerbert Xu 
4955ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
4965ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
4971706d587SHerbert Xu 
4985ab11c98SPavel Emelyanov 		kfree_skb(qp->q.fragments);
4995ab11c98SPavel Emelyanov 		qp->q.fragments = head;
5001706d587SHerbert Xu 	}
5011706d587SHerbert Xu 
5021da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
5031da177e4SLinus Torvalds 	BUG_TRAP(FRAG_CB(head)->offset == 0);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
506c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
5075ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
5081da177e4SLinus Torvalds 
5091706d587SHerbert Xu 	err = -E2BIG;
5101da177e4SLinus Torvalds 	if (len > 65535)
5111da177e4SLinus Torvalds 		goto out_oversize;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
5141da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
5151da177e4SLinus Torvalds 		goto out_nomem;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5181da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5191da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
5201da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
5211da177e4SLinus Torvalds 		struct sk_buff *clone;
5221da177e4SLinus Torvalds 		int i, plen = 0;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
5251da177e4SLinus Torvalds 			goto out_nomem;
5261da177e4SLinus Torvalds 		clone->next = head->next;
5271da177e4SLinus Torvalds 		head->next = clone;
5281da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
5291da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
5301da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
5311da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
5321da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5331da177e4SLinus Torvalds 		head->data_len -= clone->len;
5341da177e4SLinus Torvalds 		head->len -= clone->len;
5351da177e4SLinus Torvalds 		clone->csum = 0;
5361da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5377eb95156SPavel Emelyanov 		atomic_add(clone->truesize, &ip4_frags.mem);
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
541d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
5427eb95156SPavel Emelyanov 	atomic_sub(head->truesize, &ip4_frags.mem);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
5451da177e4SLinus Torvalds 		head->data_len += fp->len;
5461da177e4SLinus Torvalds 		head->len += fp->len;
5471da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
5481da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
54984fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
5501da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
5511da177e4SLinus Torvalds 		head->truesize += fp->truesize;
5527eb95156SPavel Emelyanov 		atomic_sub(fp->truesize, &ip4_frags.mem);
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	head->next = NULL;
5561da177e4SLinus Torvalds 	head->dev = dev;
5575ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
5581da177e4SLinus Torvalds 
559eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
5601da177e4SLinus Torvalds 	iph->frag_off = 0;
5611da177e4SLinus Torvalds 	iph->tot_len = htons(len);
5621da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
5635ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
5641706d587SHerbert Xu 	return 0;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds out_nomem:
56764ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
56864ce2073SPatrick McHardy 			      "queue %p\n", qp);
569*45542479SDavid Howells 	err = -ENOMEM;
5701da177e4SLinus Torvalds 	goto out_fail;
5711da177e4SLinus Torvalds out_oversize:
5721da177e4SLinus Torvalds 	if (net_ratelimit())
5731da177e4SLinus Torvalds 		printk(KERN_INFO
5741da177e4SLinus Torvalds 			"Oversized IP packet from %d.%d.%d.%d.\n",
5751da177e4SLinus Torvalds 			NIPQUAD(qp->saddr));
5761da177e4SLinus Torvalds out_fail:
5771da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
5781706d587SHerbert Xu 	return err;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
582776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	struct ipq *qp;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	/* Start by cleaning up the memory. */
58904128f23SPavel Emelyanov 	if (atomic_read(&ip4_frags.mem) > ip4_frags_ctl.high_thresh)
5901da177e4SLinus Torvalds 		ip_evictor();
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
593eddc9ec5SArnaldo Carvalho de Melo 	if ((qp = ip_find(ip_hdr(skb), user)) != NULL) {
5941706d587SHerbert Xu 		int ret;
5951da177e4SLinus Torvalds 
5965ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
5971da177e4SLinus Torvalds 
5981706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
5991da177e4SLinus Torvalds 
6005ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
6014b6cb5d8SPavel Emelyanov 		ipq_put(qp);
602776c729eSHerbert Xu 		return ret;
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
6061da177e4SLinus Torvalds 	kfree_skb(skb);
607776c729eSHerbert Xu 	return -ENOMEM;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
6111da177e4SLinus Torvalds {
61204128f23SPavel Emelyanov 	ip4_frags.ctl = &ip4_frags_ctl;
613321a3a99SPavel Emelyanov 	ip4_frags.hashfn = ip4_hashfn;
614c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
6151e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
6161e4b8287SPavel Emelyanov 	ip4_frags.skb_free = NULL;
6171e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
618abd6523dSPavel Emelyanov 	ip4_frags.match = ip4_frag_match;
619e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
6207eb95156SPavel Emelyanov 	inet_frags_init(&ip4_frags);
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds EXPORT_SYMBOL(ip_defrag);
624