xref: /openbmc/linux/net/ipv4/ip_fragment.c (revision c346dca1)
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 
1975ab11c98SPavel Emelyanov 	if (qp->q.last_in & 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 
2055ab11c98SPavel Emelyanov 	if ((qp->q.last_in&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;
232abd6523dSPavel Emelyanov 	hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
233c6fda282SPavel Emelyanov 
234ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
235c6fda282SPavel Emelyanov 	if (q == NULL)
2361da177e4SLinus Torvalds 		goto out_nomem;
2371da177e4SLinus Torvalds 
238c6fda282SPavel Emelyanov 	return container_of(q, struct ipq, q);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds out_nomem:
24164ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
2421da177e4SLinus Torvalds 	return NULL;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
24589cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
24689cee8b1SHerbert Xu static inline int ip_frag_too_far(struct ipq *qp)
24789cee8b1SHerbert Xu {
24889cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
24989cee8b1SHerbert Xu 	unsigned int max = sysctl_ipfrag_max_dist;
25089cee8b1SHerbert Xu 	unsigned int start, end;
25189cee8b1SHerbert Xu 
25289cee8b1SHerbert Xu 	int rc;
25389cee8b1SHerbert Xu 
25489cee8b1SHerbert Xu 	if (!peer || !max)
25589cee8b1SHerbert Xu 		return 0;
25689cee8b1SHerbert Xu 
25789cee8b1SHerbert Xu 	start = qp->rid;
25889cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
25989cee8b1SHerbert Xu 	qp->rid = end;
26089cee8b1SHerbert Xu 
2615ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
26289cee8b1SHerbert Xu 
26389cee8b1SHerbert Xu 	if (rc) {
26489cee8b1SHerbert Xu 		IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
26589cee8b1SHerbert Xu 	}
26689cee8b1SHerbert Xu 
26789cee8b1SHerbert Xu 	return rc;
26889cee8b1SHerbert Xu }
26989cee8b1SHerbert Xu 
27089cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
27189cee8b1SHerbert Xu {
27289cee8b1SHerbert Xu 	struct sk_buff *fp;
27389cee8b1SHerbert Xu 
274b2fd5321SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
2755ab11c98SPavel Emelyanov 		atomic_inc(&qp->q.refcnt);
27689cee8b1SHerbert Xu 		return -ETIMEDOUT;
27789cee8b1SHerbert Xu 	}
27889cee8b1SHerbert Xu 
2795ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
28089cee8b1SHerbert Xu 	do {
28189cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
2826ddc0822SPavel Emelyanov 		frag_kfree_skb(qp->q.net, fp, NULL);
28389cee8b1SHerbert Xu 		fp = xp;
28489cee8b1SHerbert Xu 	} while (fp);
28589cee8b1SHerbert Xu 
2865ab11c98SPavel Emelyanov 	qp->q.last_in = 0;
2875ab11c98SPavel Emelyanov 	qp->q.len = 0;
2885ab11c98SPavel Emelyanov 	qp->q.meat = 0;
2895ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
29089cee8b1SHerbert Xu 	qp->iif = 0;
29189cee8b1SHerbert Xu 
29289cee8b1SHerbert Xu 	return 0;
29389cee8b1SHerbert Xu }
29489cee8b1SHerbert Xu 
2951da177e4SLinus Torvalds /* Add new segment to existing queue. */
2961706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
2991706d587SHerbert Xu 	struct net_device *dev;
3001da177e4SLinus Torvalds 	int flags, offset;
3011da177e4SLinus Torvalds 	int ihl, end;
3021706d587SHerbert Xu 	int err = -ENOENT;
3031da177e4SLinus Torvalds 
3045ab11c98SPavel Emelyanov 	if (qp->q.last_in & COMPLETE)
3051da177e4SLinus Torvalds 		goto err;
3061da177e4SLinus Torvalds 
30789cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3081706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3091706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
31089cee8b1SHerbert Xu 		ipq_kill(qp);
31189cee8b1SHerbert Xu 		goto err;
31289cee8b1SHerbert Xu 	}
31389cee8b1SHerbert Xu 
314eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3151da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3161da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3171da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
318c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3211da177e4SLinus Torvalds 	end = offset + skb->len - ihl;
3221706d587SHerbert Xu 	err = -EINVAL;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* Is this the final fragment? */
3251da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3261da177e4SLinus Torvalds 		/* If we already have some bits beyond end
3271da177e4SLinus Torvalds 		 * or have different end, the segment is corrrupted.
3281da177e4SLinus Torvalds 		 */
3295ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
3305ab11c98SPavel Emelyanov 		    ((qp->q.last_in & LAST_IN) && end != qp->q.len))
3311da177e4SLinus Torvalds 			goto err;
3325ab11c98SPavel Emelyanov 		qp->q.last_in |= LAST_IN;
3335ab11c98SPavel Emelyanov 		qp->q.len = end;
3341da177e4SLinus Torvalds 	} else {
3351da177e4SLinus Torvalds 		if (end&7) {
3361da177e4SLinus Torvalds 			end &= ~7;
3371da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3381da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3391da177e4SLinus Torvalds 		}
3405ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3411da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
3425ab11c98SPavel Emelyanov 			if (qp->q.last_in & LAST_IN)
3431da177e4SLinus Torvalds 				goto err;
3445ab11c98SPavel Emelyanov 			qp->q.len = end;
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	if (end == offset)
3481da177e4SLinus Torvalds 		goto err;
3491da177e4SLinus Torvalds 
3501706d587SHerbert Xu 	err = -ENOMEM;
3511da177e4SLinus Torvalds 	if (pskb_pull(skb, ihl) == NULL)
3521da177e4SLinus Torvalds 		goto err;
3531706d587SHerbert Xu 
3541706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3551706d587SHerbert Xu 	if (err)
3561da177e4SLinus Torvalds 		goto err;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3591da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3601da177e4SLinus Torvalds 	 * this fragment, right?
3611da177e4SLinus Torvalds 	 */
3621da177e4SLinus Torvalds 	prev = NULL;
3635ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
3641da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
3651da177e4SLinus Torvalds 			break;	/* bingo! */
3661da177e4SLinus Torvalds 		prev = next;
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
3701da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
3711da177e4SLinus Torvalds 	 * any overlaps are eliminated.
3721da177e4SLinus Torvalds 	 */
3731da177e4SLinus Torvalds 	if (prev) {
3741da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 		if (i > 0) {
3771da177e4SLinus Torvalds 			offset += i;
3781706d587SHerbert Xu 			err = -EINVAL;
3791da177e4SLinus Torvalds 			if (end <= offset)
3801da177e4SLinus Torvalds 				goto err;
3811706d587SHerbert Xu 			err = -ENOMEM;
3821da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
3831da177e4SLinus Torvalds 				goto err;
3841da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3851da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 
3891706d587SHerbert Xu 	err = -ENOMEM;
3901706d587SHerbert Xu 
3911da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
3921da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 		if (i < next->len) {
3951da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
3961da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
3971da177e4SLinus Torvalds 			 */
3981da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
3991da177e4SLinus Torvalds 				goto err;
4001da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4015ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4021da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4031da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4041da177e4SLinus Torvalds 			break;
4051da177e4SLinus Torvalds 		} else {
4061da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4071da177e4SLinus Torvalds 
40847c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4091da177e4SLinus Torvalds 			 * new one drop it.
4101da177e4SLinus Torvalds 			 */
4111da177e4SLinus Torvalds 			next = next->next;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 			if (prev)
4141da177e4SLinus Torvalds 				prev->next = next;
4151da177e4SLinus Torvalds 			else
4165ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4171da177e4SLinus Torvalds 
4185ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4196ddc0822SPavel Emelyanov 			frag_kfree_skb(qp->q.net, free_it, NULL);
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4261da177e4SLinus Torvalds 	skb->next = next;
4271da177e4SLinus Torvalds 	if (prev)
4281da177e4SLinus Torvalds 		prev->next = skb;
4291da177e4SLinus Torvalds 	else
4305ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4311da177e4SLinus Torvalds 
4321706d587SHerbert Xu 	dev = skb->dev;
4331706d587SHerbert Xu 	if (dev) {
4341706d587SHerbert Xu 		qp->iif = dev->ifindex;
4351da177e4SLinus Torvalds 		skb->dev = NULL;
4361706d587SHerbert Xu 	}
4375ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4385ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4396ddc0822SPavel Emelyanov 	atomic_add(skb->truesize, &qp->q.net->mem);
4401da177e4SLinus Torvalds 	if (offset == 0)
4415ab11c98SPavel Emelyanov 		qp->q.last_in |= FIRST_IN;
4421da177e4SLinus Torvalds 
4435ab11c98SPavel Emelyanov 	if (qp->q.last_in == (FIRST_IN | LAST_IN) && qp->q.meat == qp->q.len)
4441706d587SHerbert Xu 		return ip_frag_reasm(qp, prev, dev);
4451706d587SHerbert Xu 
4467eb95156SPavel Emelyanov 	write_lock(&ip4_frags.lock);
4473140c25cSPavel Emelyanov 	list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
4487eb95156SPavel Emelyanov 	write_unlock(&ip4_frags.lock);
4491706d587SHerbert Xu 	return -EINPROGRESS;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds err:
4521da177e4SLinus Torvalds 	kfree_skb(skb);
4531706d587SHerbert Xu 	return err;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
4581da177e4SLinus Torvalds 
4591706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
4601706d587SHerbert Xu 			 struct net_device *dev)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	struct iphdr *iph;
4635ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
4641da177e4SLinus Torvalds 	int len;
4651da177e4SLinus Torvalds 	int ihlen;
4661706d587SHerbert Xu 	int err;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	ipq_kill(qp);
4691da177e4SLinus Torvalds 
4701706d587SHerbert Xu 	/* Make the one we just received the head. */
4711706d587SHerbert Xu 	if (prev) {
4721706d587SHerbert Xu 		head = prev->next;
4731706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
4741706d587SHerbert Xu 		if (!fp)
4751706d587SHerbert Xu 			goto out_nomem;
4761706d587SHerbert Xu 
4771706d587SHerbert Xu 		fp->next = head->next;
4781706d587SHerbert Xu 		prev->next = fp;
4791706d587SHerbert Xu 
4805ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
4815ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
4821706d587SHerbert Xu 
4835ab11c98SPavel Emelyanov 		kfree_skb(qp->q.fragments);
4845ab11c98SPavel Emelyanov 		qp->q.fragments = head;
4851706d587SHerbert Xu 	}
4861706d587SHerbert Xu 
4871da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
4881da177e4SLinus Torvalds 	BUG_TRAP(FRAG_CB(head)->offset == 0);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
491c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
4925ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
4931da177e4SLinus Torvalds 
4941706d587SHerbert Xu 	err = -E2BIG;
4951da177e4SLinus Torvalds 	if (len > 65535)
4961da177e4SLinus Torvalds 		goto out_oversize;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
4991da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
5001da177e4SLinus Torvalds 		goto out_nomem;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5031da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5041da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
5051da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
5061da177e4SLinus Torvalds 		struct sk_buff *clone;
5071da177e4SLinus Torvalds 		int i, plen = 0;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
5101da177e4SLinus Torvalds 			goto out_nomem;
5111da177e4SLinus Torvalds 		clone->next = head->next;
5121da177e4SLinus Torvalds 		head->next = clone;
5131da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
5141da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
5151da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
5161da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
5171da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5181da177e4SLinus Torvalds 		head->data_len -= clone->len;
5191da177e4SLinus Torvalds 		head->len -= clone->len;
5201da177e4SLinus Torvalds 		clone->csum = 0;
5211da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5226ddc0822SPavel Emelyanov 		atomic_add(clone->truesize, &qp->q.net->mem);
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
526d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
5276ddc0822SPavel Emelyanov 	atomic_sub(head->truesize, &qp->q.net->mem);
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
5301da177e4SLinus Torvalds 		head->data_len += fp->len;
5311da177e4SLinus Torvalds 		head->len += fp->len;
5321da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
5331da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
53484fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
5351da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
5361da177e4SLinus Torvalds 		head->truesize += fp->truesize;
5376ddc0822SPavel Emelyanov 		atomic_sub(fp->truesize, &qp->q.net->mem);
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	head->next = NULL;
5411da177e4SLinus Torvalds 	head->dev = dev;
5425ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
5431da177e4SLinus Torvalds 
544eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
5451da177e4SLinus Torvalds 	iph->frag_off = 0;
5461da177e4SLinus Torvalds 	iph->tot_len = htons(len);
5471da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
5485ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
5491706d587SHerbert Xu 	return 0;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds out_nomem:
55264ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
55364ce2073SPatrick McHardy 			      "queue %p\n", qp);
55445542479SDavid Howells 	err = -ENOMEM;
5551da177e4SLinus Torvalds 	goto out_fail;
5561da177e4SLinus Torvalds out_oversize:
5571da177e4SLinus Torvalds 	if (net_ratelimit())
5581da177e4SLinus Torvalds 		printk(KERN_INFO
5591da177e4SLinus Torvalds 			"Oversized IP packet from %d.%d.%d.%d.\n",
5601da177e4SLinus Torvalds 			NIPQUAD(qp->saddr));
5611da177e4SLinus Torvalds out_fail:
5621da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
5631706d587SHerbert Xu 	return err;
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
567776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	struct ipq *qp;
570ac18e750SPavel Emelyanov 	struct net *net;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
5731da177e4SLinus Torvalds 
574c346dca1SYOSHIFUJI Hideaki 	net = skb->dev ? dev_net(skb->dev) : dev_net(skb->dst->dev);
5751da177e4SLinus Torvalds 	/* Start by cleaning up the memory. */
576e31e0bdcSPavel Emelyanov 	if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
5776ddc0822SPavel Emelyanov 		ip_evictor(net);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
580ac18e750SPavel Emelyanov 	if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
5811706d587SHerbert Xu 		int ret;
5821da177e4SLinus Torvalds 
5835ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
5841da177e4SLinus Torvalds 
5851706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
5861da177e4SLinus Torvalds 
5875ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
5884b6cb5d8SPavel Emelyanov 		ipq_put(qp);
589776c729eSHerbert Xu 		return ret;
5901da177e4SLinus Torvalds 	}
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	IP_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
5931da177e4SLinus Torvalds 	kfree_skb(skb);
594776c729eSHerbert Xu 	return -ENOMEM;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5978d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5988d8354d2SPavel Emelyanov static int zero;
5998d8354d2SPavel Emelyanov 
6008d8354d2SPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
6018d8354d2SPavel Emelyanov 	{
6028d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_HIGH_THRESH,
6038d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
604e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.high_thresh,
6058d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6068d8354d2SPavel Emelyanov 		.mode		= 0644,
6078d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec
6088d8354d2SPavel Emelyanov 	},
6098d8354d2SPavel Emelyanov 	{
6108d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_LOW_THRESH,
6118d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
612e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.low_thresh,
6138d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6148d8354d2SPavel Emelyanov 		.mode		= 0644,
6158d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec
6168d8354d2SPavel Emelyanov 	},
6178d8354d2SPavel Emelyanov 	{
6188d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_TIME,
6198d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
620b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv4.frags.timeout,
6218d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6228d8354d2SPavel Emelyanov 		.mode		= 0644,
6238d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec_jiffies,
6248d8354d2SPavel Emelyanov 		.strategy	= &sysctl_jiffies
6258d8354d2SPavel Emelyanov 	},
6268d8354d2SPavel Emelyanov 	{
6278d8354d2SPavel Emelyanov 		.ctl_name	= NET_IPV4_IPFRAG_SECRET_INTERVAL,
6288d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
6293b4bc4a2SPavel Emelyanov 		.data		= &ip4_frags.secret_interval,
6308d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6318d8354d2SPavel Emelyanov 		.mode		= 0644,
6328d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec_jiffies,
6338d8354d2SPavel Emelyanov 		.strategy	= &sysctl_jiffies
6348d8354d2SPavel Emelyanov 	},
6358d8354d2SPavel Emelyanov 	{
6368d8354d2SPavel Emelyanov 		.procname	= "ipfrag_max_dist",
6378d8354d2SPavel Emelyanov 		.data		= &sysctl_ipfrag_max_dist,
6388d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6398d8354d2SPavel Emelyanov 		.mode		= 0644,
6408d8354d2SPavel Emelyanov 		.proc_handler	= &proc_dointvec_minmax,
6418d8354d2SPavel Emelyanov 		.extra1		= &zero
6428d8354d2SPavel Emelyanov 	},
6438d8354d2SPavel Emelyanov 	{ }
6448d8354d2SPavel Emelyanov };
6458d8354d2SPavel Emelyanov 
6468d8354d2SPavel Emelyanov static int ip4_frags_ctl_register(struct net *net)
6478d8354d2SPavel Emelyanov {
648e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6498d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6508d8354d2SPavel Emelyanov 
651e4a2d5c2SPavel Emelyanov 	table = ip4_frags_ctl_table;
652e4a2d5c2SPavel Emelyanov 	if (net != &init_net) {
653e4a2d5c2SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ctl_table), GFP_KERNEL);
654e4a2d5c2SPavel Emelyanov 		if (table == NULL)
655e4a2d5c2SPavel Emelyanov 			goto err_alloc;
656e4a2d5c2SPavel Emelyanov 
657e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv4.frags.high_thresh;
658e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv4.frags.low_thresh;
659b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv4.frags.timeout;
660e4a2d5c2SPavel Emelyanov 		table[3].mode &= ~0222;
661e4a2d5c2SPavel Emelyanov 		table[4].mode &= ~0222;
662e4a2d5c2SPavel Emelyanov 	}
663e4a2d5c2SPavel Emelyanov 
664e4a2d5c2SPavel Emelyanov 	hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
665e4a2d5c2SPavel Emelyanov 	if (hdr == NULL)
666e4a2d5c2SPavel Emelyanov 		goto err_reg;
667e4a2d5c2SPavel Emelyanov 
668e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
669e4a2d5c2SPavel Emelyanov 	return 0;
670e4a2d5c2SPavel Emelyanov 
671e4a2d5c2SPavel Emelyanov err_reg:
672e4a2d5c2SPavel Emelyanov 	if (net != &init_net)
673e4a2d5c2SPavel Emelyanov 		kfree(table);
674e4a2d5c2SPavel Emelyanov err_alloc:
675e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
676e4a2d5c2SPavel Emelyanov }
677e4a2d5c2SPavel Emelyanov 
678e4a2d5c2SPavel Emelyanov static void ip4_frags_ctl_unregister(struct net *net)
679e4a2d5c2SPavel Emelyanov {
680e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
681e4a2d5c2SPavel Emelyanov 
682e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
683e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
684e4a2d5c2SPavel Emelyanov 	kfree(table);
6858d8354d2SPavel Emelyanov }
6868d8354d2SPavel Emelyanov #else
6878d8354d2SPavel Emelyanov static inline int ip4_frags_ctl_register(struct net *net)
6888d8354d2SPavel Emelyanov {
6898d8354d2SPavel Emelyanov 	return 0;
6908d8354d2SPavel Emelyanov }
691e4a2d5c2SPavel Emelyanov 
692e4a2d5c2SPavel Emelyanov static inline void ip4_frags_ctl_unregister(struct net *net)
693e4a2d5c2SPavel Emelyanov {
694e4a2d5c2SPavel Emelyanov }
6958d8354d2SPavel Emelyanov #endif
6968d8354d2SPavel Emelyanov 
6978d8354d2SPavel Emelyanov static int ipv4_frags_init_net(struct net *net)
6988d8354d2SPavel Emelyanov {
699b2fd5321SPavel Emelyanov 	/*
700e31e0bdcSPavel Emelyanov 	 * Fragment cache limits. We will commit 256K at one time. Should we
701e31e0bdcSPavel Emelyanov 	 * cross that limit we will prune down to 192K. This should cope with
702e31e0bdcSPavel Emelyanov 	 * even the most extreme cases without allowing an attacker to
703e31e0bdcSPavel Emelyanov 	 * measurably harm machine performance.
704e31e0bdcSPavel Emelyanov 	 */
705e31e0bdcSPavel Emelyanov 	net->ipv4.frags.high_thresh = 256 * 1024;
706e31e0bdcSPavel Emelyanov 	net->ipv4.frags.low_thresh = 192 * 1024;
707e31e0bdcSPavel Emelyanov 	/*
708b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
709b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
710b2fd5321SPavel Emelyanov 	 * by TTL.
711b2fd5321SPavel Emelyanov 	 */
712b2fd5321SPavel Emelyanov 	net->ipv4.frags.timeout = IP_FRAG_TIME;
713b2fd5321SPavel Emelyanov 
714e5a2bb84SPavel Emelyanov 	inet_frags_init_net(&net->ipv4.frags);
715e5a2bb84SPavel Emelyanov 
7168d8354d2SPavel Emelyanov 	return ip4_frags_ctl_register(net);
7178d8354d2SPavel Emelyanov }
7188d8354d2SPavel Emelyanov 
71981566e83SPavel Emelyanov static void ipv4_frags_exit_net(struct net *net)
72081566e83SPavel Emelyanov {
72181566e83SPavel Emelyanov 	ip4_frags_ctl_unregister(net);
72281566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
72381566e83SPavel Emelyanov }
72481566e83SPavel Emelyanov 
72581566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
72681566e83SPavel Emelyanov 	.init = ipv4_frags_init_net,
72781566e83SPavel Emelyanov 	.exit = ipv4_frags_exit_net,
72881566e83SPavel Emelyanov };
72981566e83SPavel Emelyanov 
730b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
7311da177e4SLinus Torvalds {
73281566e83SPavel Emelyanov 	register_pernet_subsys(&ip4_frags_ops);
733321a3a99SPavel Emelyanov 	ip4_frags.hashfn = ip4_hashfn;
734c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
7351e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
7361e4b8287SPavel Emelyanov 	ip4_frags.skb_free = NULL;
7371e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
738abd6523dSPavel Emelyanov 	ip4_frags.match = ip4_frag_match;
739e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
7403b4bc4a2SPavel Emelyanov 	ip4_frags.secret_interval = 10 * 60 * HZ;
7417eb95156SPavel Emelyanov 	inet_frags_init(&ip4_frags);
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds EXPORT_SYMBOL(ip_defrag);
745