xref: /linux/net/ipv4/ip_fragment.c (revision 9a375803feaadb6c34e0807bd9325885dcca5c00)
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 
538d8354d2SPavel Emelyanov static 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 
777eb95156SPavel Emelyanov static struct inet_frags ip4_frags;
781da177e4SLinus Torvalds 
79e5a2bb84SPavel Emelyanov int ip_frag_nqueues(struct net *net)
807eb95156SPavel Emelyanov {
81e5a2bb84SPavel Emelyanov 	return net->ipv4.frags.nqueues;
827eb95156SPavel Emelyanov }
831da177e4SLinus Torvalds 
846ddc0822SPavel Emelyanov int ip_frag_mem(struct net *net)
857eb95156SPavel Emelyanov {
866ddc0822SPavel Emelyanov 	return atomic_read(&net->ipv4.frags.mem);
877eb95156SPavel Emelyanov }
881da177e4SLinus Torvalds 
891706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
901706d587SHerbert Xu 			 struct net_device *dev);
911706d587SHerbert Xu 
92c6fda282SPavel Emelyanov struct ip4_create_arg {
93c6fda282SPavel Emelyanov 	struct iphdr *iph;
94c6fda282SPavel Emelyanov 	u32 user;
95c6fda282SPavel Emelyanov };
96c6fda282SPavel Emelyanov 
9718277770SAl Viro static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
981da177e4SLinus Torvalds {
9918277770SAl Viro 	return jhash_3words((__force u32)id << 16 | prot,
10018277770SAl Viro 			    (__force u32)saddr, (__force u32)daddr,
1017eb95156SPavel Emelyanov 			    ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
104321a3a99SPavel Emelyanov static unsigned int ip4_hashfn(struct inet_frag_queue *q)
1051da177e4SLinus Torvalds {
106321a3a99SPavel Emelyanov 	struct ipq *ipq;
1071da177e4SLinus Torvalds 
108321a3a99SPavel Emelyanov 	ipq = container_of(q, struct ipq, q);
109321a3a99SPavel Emelyanov 	return ipqhashfn(ipq->id, ipq->saddr, ipq->daddr, ipq->protocol);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
112abd6523dSPavel Emelyanov static int ip4_frag_match(struct inet_frag_queue *q, void *a)
113abd6523dSPavel Emelyanov {
114abd6523dSPavel Emelyanov 	struct ipq *qp;
115abd6523dSPavel Emelyanov 	struct ip4_create_arg *arg = a;
116abd6523dSPavel Emelyanov 
117abd6523dSPavel Emelyanov 	qp = container_of(q, struct ipq, q);
118abd6523dSPavel Emelyanov 	return (qp->id == arg->iph->id &&
119abd6523dSPavel Emelyanov 			qp->saddr == arg->iph->saddr &&
120abd6523dSPavel Emelyanov 			qp->daddr == arg->iph->daddr &&
121abd6523dSPavel Emelyanov 			qp->protocol == arg->iph->protocol &&
122abd6523dSPavel Emelyanov 			qp->user == arg->user);
123abd6523dSPavel Emelyanov }
124abd6523dSPavel Emelyanov 
1251da177e4SLinus Torvalds /* Memory Tracking Functions. */
1266ddc0822SPavel Emelyanov static __inline__ void frag_kfree_skb(struct netns_frags *nf,
1276ddc0822SPavel Emelyanov 		struct sk_buff *skb, int *work)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	if (work)
1301da177e4SLinus Torvalds 		*work -= skb->truesize;
1316ddc0822SPavel Emelyanov 	atomic_sub(skb->truesize, &nf->mem);
1321da177e4SLinus Torvalds 	kfree_skb(skb);
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
135c6fda282SPavel Emelyanov static void ip4_frag_init(struct inet_frag_queue *q, void *a)
136c6fda282SPavel Emelyanov {
137c6fda282SPavel Emelyanov 	struct ipq *qp = container_of(q, struct ipq, q);
138c6fda282SPavel Emelyanov 	struct ip4_create_arg *arg = a;
139c6fda282SPavel Emelyanov 
140c6fda282SPavel Emelyanov 	qp->protocol = arg->iph->protocol;
141c6fda282SPavel Emelyanov 	qp->id = arg->iph->id;
142c6fda282SPavel Emelyanov 	qp->saddr = arg->iph->saddr;
143c6fda282SPavel Emelyanov 	qp->daddr = arg->iph->daddr;
144c6fda282SPavel Emelyanov 	qp->user = arg->user;
145c6fda282SPavel Emelyanov 	qp->peer = sysctl_ipfrag_max_dist ?
146c6fda282SPavel Emelyanov 		inet_getpeer(arg->iph->saddr, 1) : NULL;
147c6fda282SPavel Emelyanov }
148c6fda282SPavel Emelyanov 
1491e4b8287SPavel Emelyanov static __inline__ void ip4_frag_free(struct inet_frag_queue *q)
1501da177e4SLinus Torvalds {
1511e4b8287SPavel Emelyanov 	struct ipq *qp;
1521e4b8287SPavel Emelyanov 
1531e4b8287SPavel Emelyanov 	qp = container_of(q, struct ipq, q);
1541e4b8287SPavel Emelyanov 	if (qp->peer)
1551e4b8287SPavel Emelyanov 		inet_putpeer(qp->peer);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /* Destruction primitives. */
1601da177e4SLinus Torvalds 
1614b6cb5d8SPavel Emelyanov static __inline__ void ipq_put(struct ipq *ipq)
1621da177e4SLinus Torvalds {
163762cc408SPavel Emelyanov 	inet_frag_put(&ipq->q, &ip4_frags);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Kill ipq entry. It is not destroyed immediately,
1671da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1681da177e4SLinus Torvalds  */
1691da177e4SLinus Torvalds static void ipq_kill(struct ipq *ipq)
1701da177e4SLinus Torvalds {
171277e650dSPavel Emelyanov 	inet_frag_kill(&ipq->q, &ip4_frags);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /* Memory limiting on fragments.  Evictor trashes the oldest
1751da177e4SLinus Torvalds  * fragment queue until we are back under the threshold.
1761da177e4SLinus Torvalds  */
1776ddc0822SPavel Emelyanov static void ip_evictor(struct net *net)
1781da177e4SLinus Torvalds {
1798e7999c4SPavel Emelyanov 	int evicted;
1801da177e4SLinus Torvalds 
1816ddc0822SPavel Emelyanov 	evicted = inet_frag_evictor(&net->ipv4.frags, &ip4_frags);
1828e7999c4SPavel Emelyanov 	if (evicted)
1838e7999c4SPavel Emelyanov 		IP_ADD_STATS_BH(IPSTATS_MIB_REASMFAILS, evicted);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  * Oops, a fragment queue timed out.  Kill it and send an ICMP reply.
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds static void ip_expire(unsigned long arg)
1901da177e4SLinus Torvalds {
191e521db9dSPavel Emelyanov 	struct ipq *qp;
192e521db9dSPavel Emelyanov 
193e521db9dSPavel Emelyanov 	qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
1941da177e4SLinus Torvalds 
1955ab11c98SPavel Emelyanov 	spin_lock(&qp->q.lock);
1961da177e4SLinus Torvalds 
197bc578a54SJoe Perches 	if (qp->q.last_in & INET_FRAG_COMPLETE)
1981da177e4SLinus Torvalds 		goto out;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	ipq_kill(qp);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
2031da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
2041da177e4SLinus Torvalds 
205bc578a54SJoe Perches 	if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
2065ab11c98SPavel Emelyanov 		struct sk_buff *head = qp->q.fragments;
207cb84663eSDenis V. Lunev 		struct net *net;
208cb84663eSDenis V. Lunev 
209cb84663eSDenis V. Lunev 		net = container_of(qp->q.net, struct net, ipv4.frags);
2101da177e4SLinus Torvalds 		/* Send an ICMP "Fragment Reassembly Timeout" message. */
211cb84663eSDenis V. Lunev 		if ((head->dev = dev_get_by_index(net, qp->iif)) != NULL) {
2121da177e4SLinus Torvalds 			icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
2131da177e4SLinus Torvalds 			dev_put(head->dev);
2141da177e4SLinus Torvalds 		}
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds out:
2175ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
2184b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
221abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
222abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
223abd6523dSPavel Emelyanov  */
224ac18e750SPavel Emelyanov static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
2251da177e4SLinus Torvalds {
226c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
227c6fda282SPavel Emelyanov 	struct ip4_create_arg arg;
228abd6523dSPavel Emelyanov 	unsigned int hash;
2291da177e4SLinus Torvalds 
230c6fda282SPavel Emelyanov 	arg.iph = iph;
231c6fda282SPavel Emelyanov 	arg.user = user;
232*9a375803SPavel Emelyanov 
233*9a375803SPavel Emelyanov 	read_lock(&ip4_frags.lock);
234abd6523dSPavel Emelyanov 	hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
235c6fda282SPavel Emelyanov 
236ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
237c6fda282SPavel Emelyanov 	if (q == NULL)
2381da177e4SLinus Torvalds 		goto out_nomem;
2391da177e4SLinus Torvalds 
240c6fda282SPavel Emelyanov 	return container_of(q, struct ipq, q);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds out_nomem:
24364ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
2441da177e4SLinus Torvalds 	return NULL;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
24789cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
24889cee8b1SHerbert Xu static inline int ip_frag_too_far(struct ipq *qp)
24989cee8b1SHerbert Xu {
25089cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
25189cee8b1SHerbert Xu 	unsigned int max = sysctl_ipfrag_max_dist;
25289cee8b1SHerbert Xu 	unsigned int start, end;
25389cee8b1SHerbert Xu 
25489cee8b1SHerbert Xu 	int rc;
25589cee8b1SHerbert Xu 
25689cee8b1SHerbert Xu 	if (!peer || !max)
25789cee8b1SHerbert Xu 		return 0;
25889cee8b1SHerbert Xu 
25989cee8b1SHerbert Xu 	start = qp->rid;
26089cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
26189cee8b1SHerbert Xu 	qp->rid = end;
26289cee8b1SHerbert Xu 
2635ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
26489cee8b1SHerbert Xu 
26589cee8b1SHerbert Xu 	if (rc) {
26689cee8b1SHerbert Xu 		IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
26789cee8b1SHerbert Xu 	}
26889cee8b1SHerbert Xu 
26989cee8b1SHerbert Xu 	return rc;
27089cee8b1SHerbert Xu }
27189cee8b1SHerbert Xu 
27289cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
27389cee8b1SHerbert Xu {
27489cee8b1SHerbert Xu 	struct sk_buff *fp;
27589cee8b1SHerbert Xu 
276b2fd5321SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
2775ab11c98SPavel Emelyanov 		atomic_inc(&qp->q.refcnt);
27889cee8b1SHerbert Xu 		return -ETIMEDOUT;
27989cee8b1SHerbert Xu 	}
28089cee8b1SHerbert Xu 
2815ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
28289cee8b1SHerbert Xu 	do {
28389cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
2846ddc0822SPavel Emelyanov 		frag_kfree_skb(qp->q.net, fp, NULL);
28589cee8b1SHerbert Xu 		fp = xp;
28689cee8b1SHerbert Xu 	} while (fp);
28789cee8b1SHerbert Xu 
2885ab11c98SPavel Emelyanov 	qp->q.last_in = 0;
2895ab11c98SPavel Emelyanov 	qp->q.len = 0;
2905ab11c98SPavel Emelyanov 	qp->q.meat = 0;
2915ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
29289cee8b1SHerbert Xu 	qp->iif = 0;
29389cee8b1SHerbert Xu 
29489cee8b1SHerbert Xu 	return 0;
29589cee8b1SHerbert Xu }
29689cee8b1SHerbert Xu 
2971da177e4SLinus Torvalds /* Add new segment to existing queue. */
2981706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
3011706d587SHerbert Xu 	struct net_device *dev;
3021da177e4SLinus Torvalds 	int flags, offset;
3031da177e4SLinus Torvalds 	int ihl, end;
3041706d587SHerbert Xu 	int err = -ENOENT;
3051da177e4SLinus Torvalds 
306bc578a54SJoe Perches 	if (qp->q.last_in & INET_FRAG_COMPLETE)
3071da177e4SLinus Torvalds 		goto err;
3081da177e4SLinus Torvalds 
30989cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3101706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3111706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
31289cee8b1SHerbert Xu 		ipq_kill(qp);
31389cee8b1SHerbert Xu 		goto err;
31489cee8b1SHerbert Xu 	}
31589cee8b1SHerbert Xu 
316eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3171da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3181da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3191da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
320c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3231da177e4SLinus Torvalds 	end = offset + skb->len - ihl;
3241706d587SHerbert Xu 	err = -EINVAL;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Is this the final fragment? */
3271da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3281da177e4SLinus Torvalds 		/* If we already have some bits beyond end
3291da177e4SLinus Torvalds 		 * or have different end, the segment is corrrupted.
3301da177e4SLinus Torvalds 		 */
3315ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
332bc578a54SJoe Perches 		    ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
3331da177e4SLinus Torvalds 			goto err;
334bc578a54SJoe Perches 		qp->q.last_in |= INET_FRAG_LAST_IN;
3355ab11c98SPavel Emelyanov 		qp->q.len = end;
3361da177e4SLinus Torvalds 	} else {
3371da177e4SLinus Torvalds 		if (end&7) {
3381da177e4SLinus Torvalds 			end &= ~7;
3391da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3401da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3411da177e4SLinus Torvalds 		}
3425ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3431da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
344bc578a54SJoe Perches 			if (qp->q.last_in & INET_FRAG_LAST_IN)
3451da177e4SLinus Torvalds 				goto err;
3465ab11c98SPavel Emelyanov 			qp->q.len = end;
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	if (end == offset)
3501da177e4SLinus Torvalds 		goto err;
3511da177e4SLinus Torvalds 
3521706d587SHerbert Xu 	err = -ENOMEM;
3531da177e4SLinus Torvalds 	if (pskb_pull(skb, ihl) == NULL)
3541da177e4SLinus Torvalds 		goto err;
3551706d587SHerbert Xu 
3561706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3571706d587SHerbert Xu 	if (err)
3581da177e4SLinus Torvalds 		goto err;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3611da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3621da177e4SLinus Torvalds 	 * this fragment, right?
3631da177e4SLinus Torvalds 	 */
3641da177e4SLinus Torvalds 	prev = NULL;
3655ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
3661da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
3671da177e4SLinus Torvalds 			break;	/* bingo! */
3681da177e4SLinus Torvalds 		prev = next;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
3721da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
3731da177e4SLinus Torvalds 	 * any overlaps are eliminated.
3741da177e4SLinus Torvalds 	 */
3751da177e4SLinus Torvalds 	if (prev) {
3761da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 		if (i > 0) {
3791da177e4SLinus Torvalds 			offset += i;
3801706d587SHerbert Xu 			err = -EINVAL;
3811da177e4SLinus Torvalds 			if (end <= offset)
3821da177e4SLinus Torvalds 				goto err;
3831706d587SHerbert Xu 			err = -ENOMEM;
3841da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
3851da177e4SLinus Torvalds 				goto err;
3861da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3871da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3881da177e4SLinus Torvalds 		}
3891da177e4SLinus Torvalds 	}
3901da177e4SLinus Torvalds 
3911706d587SHerbert Xu 	err = -ENOMEM;
3921706d587SHerbert Xu 
3931da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
3941da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		if (i < next->len) {
3971da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
3981da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
3991da177e4SLinus Torvalds 			 */
4001da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
4011da177e4SLinus Torvalds 				goto err;
4021da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4035ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4041da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4051da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4061da177e4SLinus Torvalds 			break;
4071da177e4SLinus Torvalds 		} else {
4081da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4091da177e4SLinus Torvalds 
41047c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4111da177e4SLinus Torvalds 			 * new one drop it.
4121da177e4SLinus Torvalds 			 */
4131da177e4SLinus Torvalds 			next = next->next;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 			if (prev)
4161da177e4SLinus Torvalds 				prev->next = next;
4171da177e4SLinus Torvalds 			else
4185ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4191da177e4SLinus Torvalds 
4205ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4216ddc0822SPavel Emelyanov 			frag_kfree_skb(qp->q.net, free_it, NULL);
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4281da177e4SLinus Torvalds 	skb->next = next;
4291da177e4SLinus Torvalds 	if (prev)
4301da177e4SLinus Torvalds 		prev->next = skb;
4311da177e4SLinus Torvalds 	else
4325ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4331da177e4SLinus Torvalds 
4341706d587SHerbert Xu 	dev = skb->dev;
4351706d587SHerbert Xu 	if (dev) {
4361706d587SHerbert Xu 		qp->iif = dev->ifindex;
4371da177e4SLinus Torvalds 		skb->dev = NULL;
4381706d587SHerbert Xu 	}
4395ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4405ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4416ddc0822SPavel Emelyanov 	atomic_add(skb->truesize, &qp->q.net->mem);
4421da177e4SLinus Torvalds 	if (offset == 0)
443bc578a54SJoe Perches 		qp->q.last_in |= INET_FRAG_FIRST_IN;
4441da177e4SLinus Torvalds 
445bc578a54SJoe Perches 	if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
446bc578a54SJoe Perches 	    qp->q.meat == qp->q.len)
4471706d587SHerbert Xu 		return ip_frag_reasm(qp, prev, dev);
4481706d587SHerbert Xu 
4497eb95156SPavel Emelyanov 	write_lock(&ip4_frags.lock);
4503140c25cSPavel Emelyanov 	list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
4517eb95156SPavel Emelyanov 	write_unlock(&ip4_frags.lock);
4521706d587SHerbert Xu 	return -EINPROGRESS;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds err:
4551da177e4SLinus Torvalds 	kfree_skb(skb);
4561706d587SHerbert Xu 	return err;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
4611da177e4SLinus Torvalds 
4621706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
4631706d587SHerbert Xu 			 struct net_device *dev)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	struct iphdr *iph;
4665ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
4671da177e4SLinus Torvalds 	int len;
4681da177e4SLinus Torvalds 	int ihlen;
4691706d587SHerbert Xu 	int err;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	ipq_kill(qp);
4721da177e4SLinus Torvalds 
4731706d587SHerbert Xu 	/* Make the one we just received the head. */
4741706d587SHerbert Xu 	if (prev) {
4751706d587SHerbert Xu 		head = prev->next;
4761706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
4771706d587SHerbert Xu 		if (!fp)
4781706d587SHerbert Xu 			goto out_nomem;
4791706d587SHerbert Xu 
4801706d587SHerbert Xu 		fp->next = head->next;
4811706d587SHerbert Xu 		prev->next = fp;
4821706d587SHerbert Xu 
4835ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
4845ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
4851706d587SHerbert Xu 
4865ab11c98SPavel Emelyanov 		kfree_skb(qp->q.fragments);
4875ab11c98SPavel Emelyanov 		qp->q.fragments = head;
4881706d587SHerbert Xu 	}
4891706d587SHerbert Xu 
4901da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
4911da177e4SLinus Torvalds 	BUG_TRAP(FRAG_CB(head)->offset == 0);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
494c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
4955ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
4961da177e4SLinus Torvalds 
4971706d587SHerbert Xu 	err = -E2BIG;
4981da177e4SLinus Torvalds 	if (len > 65535)
4991da177e4SLinus Torvalds 		goto out_oversize;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
5021da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
5031da177e4SLinus Torvalds 		goto out_nomem;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5061da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5071da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
5081da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
5091da177e4SLinus Torvalds 		struct sk_buff *clone;
5101da177e4SLinus Torvalds 		int i, plen = 0;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
5131da177e4SLinus Torvalds 			goto out_nomem;
5141da177e4SLinus Torvalds 		clone->next = head->next;
5151da177e4SLinus Torvalds 		head->next = clone;
5161da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
5171da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
5181da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
5191da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
5201da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5211da177e4SLinus Torvalds 		head->data_len -= clone->len;
5221da177e4SLinus Torvalds 		head->len -= clone->len;
5231da177e4SLinus Torvalds 		clone->csum = 0;
5241da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5256ddc0822SPavel Emelyanov 		atomic_add(clone->truesize, &qp->q.net->mem);
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
529d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
5306ddc0822SPavel Emelyanov 	atomic_sub(head->truesize, &qp->q.net->mem);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
5331da177e4SLinus Torvalds 		head->data_len += fp->len;
5341da177e4SLinus Torvalds 		head->len += fp->len;
5351da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
5361da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
53784fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
5381da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
5391da177e4SLinus Torvalds 		head->truesize += fp->truesize;
5406ddc0822SPavel Emelyanov 		atomic_sub(fp->truesize, &qp->q.net->mem);
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	head->next = NULL;
5441da177e4SLinus Torvalds 	head->dev = dev;
5455ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
5461da177e4SLinus Torvalds 
547eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
5481da177e4SLinus Torvalds 	iph->frag_off = 0;
5491da177e4SLinus Torvalds 	iph->tot_len = htons(len);
5501da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
5515ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
5521706d587SHerbert Xu 	return 0;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds out_nomem:
55564ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
55664ce2073SPatrick McHardy 			      "queue %p\n", qp);
55745542479SDavid Howells 	err = -ENOMEM;
5581da177e4SLinus Torvalds 	goto out_fail;
5591da177e4SLinus Torvalds out_oversize:
5601da177e4SLinus Torvalds 	if (net_ratelimit())
5611da177e4SLinus Torvalds 		printk(KERN_INFO
562a7d632b6SYOSHIFUJI Hideaki 			"Oversized IP packet from " NIPQUAD_FMT ".\n",
5631da177e4SLinus Torvalds 			NIPQUAD(qp->saddr));
5641da177e4SLinus Torvalds out_fail:
5651da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
5661706d587SHerbert Xu 	return err;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
570776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user)
5711da177e4SLinus Torvalds {
5721da177e4SLinus Torvalds 	struct ipq *qp;
573ac18e750SPavel Emelyanov 	struct net *net;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
5761da177e4SLinus Torvalds 
577c346dca1SYOSHIFUJI Hideaki 	net = skb->dev ? dev_net(skb->dev) : dev_net(skb->dst->dev);
5781da177e4SLinus Torvalds 	/* Start by cleaning up the memory. */
579e31e0bdcSPavel Emelyanov 	if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
5806ddc0822SPavel Emelyanov 		ip_evictor(net);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
583ac18e750SPavel Emelyanov 	if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
5841706d587SHerbert Xu 		int ret;
5851da177e4SLinus Torvalds 
5865ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
5871da177e4SLinus Torvalds 
5881706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
5891da177e4SLinus Torvalds 
5905ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
5914b6cb5d8SPavel Emelyanov 		ipq_put(qp);
592776c729eSHerbert Xu 		return ret;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
5961da177e4SLinus Torvalds 	kfree_skb(skb);
597776c729eSHerbert Xu 	return -ENOMEM;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6008d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
6018d8354d2SPavel Emelyanov static int zero;
6028d8354d2SPavel Emelyanov 
6038d8354d2SPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
6048d8354d2SPavel Emelyanov 	{
6058d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_HIGH_THRESH,
6068d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
607e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.high_thresh,
6088d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6098d8354d2SPavel Emelyanov 		.mode		= 0644,
6108d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec
6118d8354d2SPavel Emelyanov 	},
6128d8354d2SPavel Emelyanov 	{
6138d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_LOW_THRESH,
6148d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
615e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.low_thresh,
6168d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6178d8354d2SPavel Emelyanov 		.mode		= 0644,
6188d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec
6198d8354d2SPavel Emelyanov 	},
6208d8354d2SPavel Emelyanov 	{
6218d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_TIME,
6228d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
623b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv4.frags.timeout,
6248d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6258d8354d2SPavel Emelyanov 		.mode		= 0644,
6268d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec_jiffies,
6278d8354d2SPavel Emelyanov 		.strategy	= &sysctl_jiffies
6288d8354d2SPavel Emelyanov 	},
6298d8354d2SPavel Emelyanov 	{
6308d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_SECRET_INTERVAL,
6318d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
6323b4bc4a2SPavel Emelyanov 		.data		= &ip4_frags.secret_interval,
6338d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6348d8354d2SPavel Emelyanov 		.mode		= 0644,
6358d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec_jiffies,
6368d8354d2SPavel Emelyanov 		.strategy	= &sysctl_jiffies
6378d8354d2SPavel Emelyanov 	},
6388d8354d2SPavel Emelyanov 	{
6398d8354d2SPavel Emelyanov 		.procname	= "ipfrag_max_dist",
6408d8354d2SPavel Emelyanov 		.data		= &sysctl_ipfrag_max_dist,
6418d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6428d8354d2SPavel Emelyanov 		.mode		= 0644,
6438d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec_minmax,
6448d8354d2SPavel Emelyanov 		.extra1		= &zero
6458d8354d2SPavel Emelyanov 	},
6468d8354d2SPavel Emelyanov 	{ }
6478d8354d2SPavel Emelyanov };
6488d8354d2SPavel Emelyanov 
6498d8354d2SPavel Emelyanov static int ip4_frags_ctl_register(struct net *net)
6508d8354d2SPavel Emelyanov {
651e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6528d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6538d8354d2SPavel Emelyanov 
654e4a2d5c2SPavel Emelyanov 	table = ip4_frags_ctl_table;
655e4a2d5c2SPavel Emelyanov 	if (net != &init_net) {
656e4a2d5c2SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ctl_table), GFP_KERNEL);
657e4a2d5c2SPavel Emelyanov 		if (table == NULL)
658e4a2d5c2SPavel Emelyanov 			goto err_alloc;
659e4a2d5c2SPavel Emelyanov 
660e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv4.frags.high_thresh;
661e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv4.frags.low_thresh;
662b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv4.frags.timeout;
663e4a2d5c2SPavel Emelyanov 		table[3].mode &= ~0222;
664e4a2d5c2SPavel Emelyanov 		table[4].mode &= ~0222;
665e4a2d5c2SPavel Emelyanov 	}
666e4a2d5c2SPavel Emelyanov 
667e4a2d5c2SPavel Emelyanov 	hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
668e4a2d5c2SPavel Emelyanov 	if (hdr == NULL)
669e4a2d5c2SPavel Emelyanov 		goto err_reg;
670e4a2d5c2SPavel Emelyanov 
671e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
672e4a2d5c2SPavel Emelyanov 	return 0;
673e4a2d5c2SPavel Emelyanov 
674e4a2d5c2SPavel Emelyanov err_reg:
675e4a2d5c2SPavel Emelyanov 	if (net != &init_net)
676e4a2d5c2SPavel Emelyanov 		kfree(table);
677e4a2d5c2SPavel Emelyanov err_alloc:
678e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
679e4a2d5c2SPavel Emelyanov }
680e4a2d5c2SPavel Emelyanov 
681e4a2d5c2SPavel Emelyanov static void ip4_frags_ctl_unregister(struct net *net)
682e4a2d5c2SPavel Emelyanov {
683e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
684e4a2d5c2SPavel Emelyanov 
685e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
686e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
687e4a2d5c2SPavel Emelyanov 	kfree(table);
6888d8354d2SPavel Emelyanov }
6898d8354d2SPavel Emelyanov #else
6908d8354d2SPavel Emelyanov static inline int ip4_frags_ctl_register(struct net *net)
6918d8354d2SPavel Emelyanov {
6928d8354d2SPavel Emelyanov 	return 0;
6938d8354d2SPavel Emelyanov }
694e4a2d5c2SPavel Emelyanov 
695e4a2d5c2SPavel Emelyanov static inline void ip4_frags_ctl_unregister(struct net *net)
696e4a2d5c2SPavel Emelyanov {
697e4a2d5c2SPavel Emelyanov }
6988d8354d2SPavel Emelyanov #endif
6998d8354d2SPavel Emelyanov 
7008d8354d2SPavel Emelyanov static int ipv4_frags_init_net(struct net *net)
7018d8354d2SPavel Emelyanov {
702b2fd5321SPavel Emelyanov 	/*
703e31e0bdcSPavel Emelyanov 	 * Fragment cache limits. We will commit 256K at one time. Should we
704e31e0bdcSPavel Emelyanov 	 * cross that limit we will prune down to 192K. This should cope with
705e31e0bdcSPavel Emelyanov 	 * even the most extreme cases without allowing an attacker to
706e31e0bdcSPavel Emelyanov 	 * measurably harm machine performance.
707e31e0bdcSPavel Emelyanov 	 */
708e31e0bdcSPavel Emelyanov 	net->ipv4.frags.high_thresh = 256 * 1024;
709e31e0bdcSPavel Emelyanov 	net->ipv4.frags.low_thresh = 192 * 1024;
710e31e0bdcSPavel Emelyanov 	/*
711b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
712b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
713b2fd5321SPavel Emelyanov 	 * by TTL.
714b2fd5321SPavel Emelyanov 	 */
715b2fd5321SPavel Emelyanov 	net->ipv4.frags.timeout = IP_FRAG_TIME;
716b2fd5321SPavel Emelyanov 
717e5a2bb84SPavel Emelyanov 	inet_frags_init_net(&net->ipv4.frags);
718e5a2bb84SPavel Emelyanov 
7198d8354d2SPavel Emelyanov 	return ip4_frags_ctl_register(net);
7208d8354d2SPavel Emelyanov }
7218d8354d2SPavel Emelyanov 
72281566e83SPavel Emelyanov static void ipv4_frags_exit_net(struct net *net)
72381566e83SPavel Emelyanov {
72481566e83SPavel Emelyanov 	ip4_frags_ctl_unregister(net);
72581566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
72681566e83SPavel Emelyanov }
72781566e83SPavel Emelyanov 
72881566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
72981566e83SPavel Emelyanov 	.init = ipv4_frags_init_net,
73081566e83SPavel Emelyanov 	.exit = ipv4_frags_exit_net,
73181566e83SPavel Emelyanov };
73281566e83SPavel Emelyanov 
733b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
7341da177e4SLinus Torvalds {
73581566e83SPavel Emelyanov 	register_pernet_subsys(&ip4_frags_ops);
736321a3a99SPavel Emelyanov 	ip4_frags.hashfn = ip4_hashfn;
737c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
7381e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
7391e4b8287SPavel Emelyanov 	ip4_frags.skb_free = NULL;
7401e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
741abd6523dSPavel Emelyanov 	ip4_frags.match = ip4_frag_match;
742e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
7433b4bc4a2SPavel Emelyanov 	ip4_frags.secret_interval = 10 * 60 * HZ;
7447eb95156SPavel Emelyanov 	inet_frags_init(&ip4_frags);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds EXPORT_SYMBOL(ip_defrag);
748