xref: /openbmc/linux/net/ipv4/ip_fragment.c (revision d1c9ae6d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		The IP fragmentation functionality.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Authors:	Fred N. van Kempen <waltje@uWalt.NL.Mugnet.ORG>
9113aa838SAlan Cox  *		Alan Cox <alan@lxorguk.ukuu.org.uk>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Fixes:
121da177e4SLinus Torvalds  *		Alan Cox	:	Split from ip.c , see ip_input.c for history.
131da177e4SLinus Torvalds  *		David S. Miller :	Begin massive cleanup...
141da177e4SLinus Torvalds  *		Andi Kleen	:	Add sysctls.
151da177e4SLinus Torvalds  *		xxxx		:	Overlapfrag bug.
161da177e4SLinus Torvalds  *		Ultima          :       ip_expire() kernel panic.
171da177e4SLinus Torvalds  *		Bill Hawes	:	Frag accounting and evictor fixes.
181da177e4SLinus Torvalds  *		John McDonald	:	0 length frag bug.
191da177e4SLinus Torvalds  *		Alexey Kuznetsov:	SMP races, threading, cleanup.
201da177e4SLinus Torvalds  *		Patrick McHardy :	LRU queue of frag heads for evictor.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
2389cee8b1SHerbert Xu #include <linux/compiler.h>
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/types.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/jiffies.h>
281da177e4SLinus Torvalds #include <linux/skbuff.h>
291da177e4SLinus Torvalds #include <linux/list.h>
301da177e4SLinus Torvalds #include <linux/ip.h>
311da177e4SLinus Torvalds #include <linux/icmp.h>
321da177e4SLinus Torvalds #include <linux/netdevice.h>
331da177e4SLinus Torvalds #include <linux/jhash.h>
341da177e4SLinus Torvalds #include <linux/random.h>
35e9017b55SShan Wei #include <net/route.h>
36e9017b55SShan Wei #include <net/dst.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)
183c5346fe3SPavel Emelyanov 		IP_ADD_STATS_BH(net, 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;
19284a3aa00SPavel Emelyanov 	struct net *net;
193e521db9dSPavel Emelyanov 
194e521db9dSPavel Emelyanov 	qp = container_of((struct inet_frag_queue *) arg, struct ipq, q);
19584a3aa00SPavel Emelyanov 	net = container_of(qp->q.net, struct net, ipv4.frags);
1961da177e4SLinus Torvalds 
1975ab11c98SPavel Emelyanov 	spin_lock(&qp->q.lock);
1981da177e4SLinus Torvalds 
199bc578a54SJoe Perches 	if (qp->q.last_in & INET_FRAG_COMPLETE)
2001da177e4SLinus Torvalds 		goto out;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	ipq_kill(qp);
2031da177e4SLinus Torvalds 
2047c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMTIMEOUT);
2057c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
2061da177e4SLinus Torvalds 
207bc578a54SJoe Perches 	if ((qp->q.last_in & INET_FRAG_FIRST_IN) && qp->q.fragments != NULL) {
2085ab11c98SPavel Emelyanov 		struct sk_buff *head = qp->q.fragments;
209cb84663eSDenis V. Lunev 
21069df9d59SEric Dumazet 		rcu_read_lock();
21169df9d59SEric Dumazet 		head->dev = dev_get_by_index_rcu(net, qp->iif);
212e9017b55SShan Wei 		if (!head->dev)
213e9017b55SShan Wei 			goto out_rcu_unlock;
214e9017b55SShan Wei 
215e9017b55SShan Wei 		/*
216e9017b55SShan Wei 		 * Only search router table for the head fragment,
217e9017b55SShan Wei 		 * when defraging timeout at PRE_ROUTING HOOK.
218e9017b55SShan Wei 		 */
219e9017b55SShan Wei 		if (qp->user == IP_DEFRAG_CONNTRACK_IN && !skb_dst(head)) {
220e9017b55SShan Wei 			const struct iphdr *iph = ip_hdr(head);
221e9017b55SShan Wei 			int err = ip_route_input(head, iph->daddr, iph->saddr,
222e9017b55SShan Wei 						 iph->tos, head->dev);
223e9017b55SShan Wei 			if (unlikely(err))
224e9017b55SShan Wei 				goto out_rcu_unlock;
225e9017b55SShan Wei 
226e9017b55SShan Wei 			/*
227e9017b55SShan Wei 			 * Only an end host needs to send an ICMP
228e9017b55SShan Wei 			 * "Fragment Reassembly Timeout" message, per RFC792.
229e9017b55SShan Wei 			 */
230e9017b55SShan Wei 			if (skb_rtable(head)->rt_type != RTN_LOCAL)
231e9017b55SShan Wei 				goto out_rcu_unlock;
232e9017b55SShan Wei 
2331da177e4SLinus Torvalds 		}
234e9017b55SShan Wei 
235e9017b55SShan Wei 		/* Send an ICMP "Fragment Reassembly Timeout" message. */
236e9017b55SShan Wei 		icmp_send(head, ICMP_TIME_EXCEEDED, ICMP_EXC_FRAGTIME, 0);
237e9017b55SShan Wei out_rcu_unlock:
238e9017b55SShan Wei 		rcu_read_unlock();
239d1c9ae6dSPatrick McHardy 	}
2401da177e4SLinus Torvalds out:
2415ab11c98SPavel Emelyanov 	spin_unlock(&qp->q.lock);
2424b6cb5d8SPavel Emelyanov 	ipq_put(qp);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
245abd6523dSPavel Emelyanov /* Find the correct entry in the "incomplete datagrams" queue for
246abd6523dSPavel Emelyanov  * this IP datagram, and create new one, if nothing is found.
247abd6523dSPavel Emelyanov  */
248ac18e750SPavel Emelyanov static inline struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
2491da177e4SLinus Torvalds {
250c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
251c6fda282SPavel Emelyanov 	struct ip4_create_arg arg;
252abd6523dSPavel Emelyanov 	unsigned int hash;
2531da177e4SLinus Torvalds 
254c6fda282SPavel Emelyanov 	arg.iph = iph;
255c6fda282SPavel Emelyanov 	arg.user = user;
2569a375803SPavel Emelyanov 
2579a375803SPavel Emelyanov 	read_lock(&ip4_frags.lock);
258abd6523dSPavel Emelyanov 	hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
259c6fda282SPavel Emelyanov 
260ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv4.frags, &ip4_frags, &arg, hash);
261c6fda282SPavel Emelyanov 	if (q == NULL)
2621da177e4SLinus Torvalds 		goto out_nomem;
2631da177e4SLinus Torvalds 
264c6fda282SPavel Emelyanov 	return container_of(q, struct ipq, q);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds out_nomem:
26764ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "ip_frag_create: no memory left !\n");
2681da177e4SLinus Torvalds 	return NULL;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
27189cee8b1SHerbert Xu /* Is the fragment too far ahead to be part of ipq? */
27289cee8b1SHerbert Xu static inline int ip_frag_too_far(struct ipq *qp)
27389cee8b1SHerbert Xu {
27489cee8b1SHerbert Xu 	struct inet_peer *peer = qp->peer;
27589cee8b1SHerbert Xu 	unsigned int max = sysctl_ipfrag_max_dist;
27689cee8b1SHerbert Xu 	unsigned int start, end;
27789cee8b1SHerbert Xu 
27889cee8b1SHerbert Xu 	int rc;
27989cee8b1SHerbert Xu 
28089cee8b1SHerbert Xu 	if (!peer || !max)
28189cee8b1SHerbert Xu 		return 0;
28289cee8b1SHerbert Xu 
28389cee8b1SHerbert Xu 	start = qp->rid;
28489cee8b1SHerbert Xu 	end = atomic_inc_return(&peer->rid);
28589cee8b1SHerbert Xu 	qp->rid = end;
28689cee8b1SHerbert Xu 
2875ab11c98SPavel Emelyanov 	rc = qp->q.fragments && (end - start) > max;
28889cee8b1SHerbert Xu 
28989cee8b1SHerbert Xu 	if (rc) {
2907c73a6faSPavel Emelyanov 		struct net *net;
2917c73a6faSPavel Emelyanov 
2927c73a6faSPavel Emelyanov 		net = container_of(qp->q.net, struct net, ipv4.frags);
2937c73a6faSPavel Emelyanov 		IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
29489cee8b1SHerbert Xu 	}
29589cee8b1SHerbert Xu 
29689cee8b1SHerbert Xu 	return rc;
29789cee8b1SHerbert Xu }
29889cee8b1SHerbert Xu 
29989cee8b1SHerbert Xu static int ip_frag_reinit(struct ipq *qp)
30089cee8b1SHerbert Xu {
30189cee8b1SHerbert Xu 	struct sk_buff *fp;
30289cee8b1SHerbert Xu 
303b2fd5321SPavel Emelyanov 	if (!mod_timer(&qp->q.timer, jiffies + qp->q.net->timeout)) {
3045ab11c98SPavel Emelyanov 		atomic_inc(&qp->q.refcnt);
30589cee8b1SHerbert Xu 		return -ETIMEDOUT;
30689cee8b1SHerbert Xu 	}
30789cee8b1SHerbert Xu 
3085ab11c98SPavel Emelyanov 	fp = qp->q.fragments;
30989cee8b1SHerbert Xu 	do {
31089cee8b1SHerbert Xu 		struct sk_buff *xp = fp->next;
3116ddc0822SPavel Emelyanov 		frag_kfree_skb(qp->q.net, fp, NULL);
31289cee8b1SHerbert Xu 		fp = xp;
31389cee8b1SHerbert Xu 	} while (fp);
31489cee8b1SHerbert Xu 
3155ab11c98SPavel Emelyanov 	qp->q.last_in = 0;
3165ab11c98SPavel Emelyanov 	qp->q.len = 0;
3175ab11c98SPavel Emelyanov 	qp->q.meat = 0;
3185ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
31989cee8b1SHerbert Xu 	qp->iif = 0;
32089cee8b1SHerbert Xu 
32189cee8b1SHerbert Xu 	return 0;
32289cee8b1SHerbert Xu }
32389cee8b1SHerbert Xu 
3241da177e4SLinus Torvalds /* Add new segment to existing queue. */
3251706d587SHerbert Xu static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
3281706d587SHerbert Xu 	struct net_device *dev;
3291da177e4SLinus Torvalds 	int flags, offset;
3301da177e4SLinus Torvalds 	int ihl, end;
3311706d587SHerbert Xu 	int err = -ENOENT;
3321da177e4SLinus Torvalds 
333bc578a54SJoe Perches 	if (qp->q.last_in & INET_FRAG_COMPLETE)
3341da177e4SLinus Torvalds 		goto err;
3351da177e4SLinus Torvalds 
33689cee8b1SHerbert Xu 	if (!(IPCB(skb)->flags & IPSKB_FRAG_COMPLETE) &&
3371706d587SHerbert Xu 	    unlikely(ip_frag_too_far(qp)) &&
3381706d587SHerbert Xu 	    unlikely(err = ip_frag_reinit(qp))) {
33989cee8b1SHerbert Xu 		ipq_kill(qp);
34089cee8b1SHerbert Xu 		goto err;
34189cee8b1SHerbert Xu 	}
34289cee8b1SHerbert Xu 
343eddc9ec5SArnaldo Carvalho de Melo 	offset = ntohs(ip_hdr(skb)->frag_off);
3441da177e4SLinus Torvalds 	flags = offset & ~IP_OFFSET;
3451da177e4SLinus Torvalds 	offset &= IP_OFFSET;
3461da177e4SLinus Torvalds 	offset <<= 3;		/* offset is in 8-byte chunks */
347c9bdd4b5SArnaldo Carvalho de Melo 	ihl = ip_hdrlen(skb);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	/* Determine the position of this fragment. */
3501da177e4SLinus Torvalds 	end = offset + skb->len - ihl;
3511706d587SHerbert Xu 	err = -EINVAL;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	/* Is this the final fragment? */
3541da177e4SLinus Torvalds 	if ((flags & IP_MF) == 0) {
3551da177e4SLinus Torvalds 		/* If we already have some bits beyond end
3561da177e4SLinus Torvalds 		 * or have different end, the segment is corrrupted.
3571da177e4SLinus Torvalds 		 */
3585ab11c98SPavel Emelyanov 		if (end < qp->q.len ||
359bc578a54SJoe Perches 		    ((qp->q.last_in & INET_FRAG_LAST_IN) && end != qp->q.len))
3601da177e4SLinus Torvalds 			goto err;
361bc578a54SJoe Perches 		qp->q.last_in |= INET_FRAG_LAST_IN;
3625ab11c98SPavel Emelyanov 		qp->q.len = end;
3631da177e4SLinus Torvalds 	} else {
3641da177e4SLinus Torvalds 		if (end&7) {
3651da177e4SLinus Torvalds 			end &= ~7;
3661da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3671da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3681da177e4SLinus Torvalds 		}
3695ab11c98SPavel Emelyanov 		if (end > qp->q.len) {
3701da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
371bc578a54SJoe Perches 			if (qp->q.last_in & INET_FRAG_LAST_IN)
3721da177e4SLinus Torvalds 				goto err;
3735ab11c98SPavel Emelyanov 			qp->q.len = end;
3741da177e4SLinus Torvalds 		}
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 	if (end == offset)
3771da177e4SLinus Torvalds 		goto err;
3781da177e4SLinus Torvalds 
3791706d587SHerbert Xu 	err = -ENOMEM;
3801da177e4SLinus Torvalds 	if (pskb_pull(skb, ihl) == NULL)
3811da177e4SLinus Torvalds 		goto err;
3821706d587SHerbert Xu 
3831706d587SHerbert Xu 	err = pskb_trim_rcsum(skb, end - offset);
3841706d587SHerbert Xu 	if (err)
3851da177e4SLinus Torvalds 		goto err;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3881da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3891da177e4SLinus Torvalds 	 * this fragment, right?
3901da177e4SLinus Torvalds 	 */
3911da177e4SLinus Torvalds 	prev = NULL;
3925ab11c98SPavel Emelyanov 	for (next = qp->q.fragments; next != NULL; next = next->next) {
3931da177e4SLinus Torvalds 		if (FRAG_CB(next)->offset >= offset)
3941da177e4SLinus Torvalds 			break;	/* bingo! */
3951da177e4SLinus Torvalds 		prev = next;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
3991da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
4001da177e4SLinus Torvalds 	 * any overlaps are eliminated.
4011da177e4SLinus Torvalds 	 */
4021da177e4SLinus Torvalds 	if (prev) {
4031da177e4SLinus Torvalds 		int i = (FRAG_CB(prev)->offset + prev->len) - offset;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 		if (i > 0) {
4061da177e4SLinus Torvalds 			offset += i;
4071706d587SHerbert Xu 			err = -EINVAL;
4081da177e4SLinus Torvalds 			if (end <= offset)
4091da177e4SLinus Torvalds 				goto err;
4101706d587SHerbert Xu 			err = -ENOMEM;
4111da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
4121da177e4SLinus Torvalds 				goto err;
4131da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
4141da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4181706d587SHerbert Xu 	err = -ENOMEM;
4191706d587SHerbert Xu 
4201da177e4SLinus Torvalds 	while (next && FRAG_CB(next)->offset < end) {
4211da177e4SLinus Torvalds 		int i = end - FRAG_CB(next)->offset; /* overlap is 'i' bytes */
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 		if (i < next->len) {
4241da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
4251da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
4261da177e4SLinus Torvalds 			 */
4271da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
4281da177e4SLinus Torvalds 				goto err;
4291da177e4SLinus Torvalds 			FRAG_CB(next)->offset += i;
4305ab11c98SPavel Emelyanov 			qp->q.meat -= i;
4311da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
4321da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
4331da177e4SLinus Torvalds 			break;
4341da177e4SLinus Torvalds 		} else {
4351da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
4361da177e4SLinus Torvalds 
43747c6bf77SPeter Zijlstra 			/* Old fragment is completely overridden with
4381da177e4SLinus Torvalds 			 * new one drop it.
4391da177e4SLinus Torvalds 			 */
4401da177e4SLinus Torvalds 			next = next->next;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 			if (prev)
4431da177e4SLinus Torvalds 				prev->next = next;
4441da177e4SLinus Torvalds 			else
4455ab11c98SPavel Emelyanov 				qp->q.fragments = next;
4461da177e4SLinus Torvalds 
4475ab11c98SPavel Emelyanov 			qp->q.meat -= free_it->len;
4486ddc0822SPavel Emelyanov 			frag_kfree_skb(qp->q.net, free_it, NULL);
4491da177e4SLinus Torvalds 		}
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	FRAG_CB(skb)->offset = offset;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4551da177e4SLinus Torvalds 	skb->next = next;
4561da177e4SLinus Torvalds 	if (prev)
4571da177e4SLinus Torvalds 		prev->next = skb;
4581da177e4SLinus Torvalds 	else
4595ab11c98SPavel Emelyanov 		qp->q.fragments = skb;
4601da177e4SLinus Torvalds 
4611706d587SHerbert Xu 	dev = skb->dev;
4621706d587SHerbert Xu 	if (dev) {
4631706d587SHerbert Xu 		qp->iif = dev->ifindex;
4641da177e4SLinus Torvalds 		skb->dev = NULL;
4651706d587SHerbert Xu 	}
4665ab11c98SPavel Emelyanov 	qp->q.stamp = skb->tstamp;
4675ab11c98SPavel Emelyanov 	qp->q.meat += skb->len;
4686ddc0822SPavel Emelyanov 	atomic_add(skb->truesize, &qp->q.net->mem);
4691da177e4SLinus Torvalds 	if (offset == 0)
470bc578a54SJoe Perches 		qp->q.last_in |= INET_FRAG_FIRST_IN;
4711da177e4SLinus Torvalds 
472bc578a54SJoe Perches 	if (qp->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
473bc578a54SJoe Perches 	    qp->q.meat == qp->q.len)
4741706d587SHerbert Xu 		return ip_frag_reasm(qp, prev, dev);
4751706d587SHerbert Xu 
4767eb95156SPavel Emelyanov 	write_lock(&ip4_frags.lock);
4773140c25cSPavel Emelyanov 	list_move_tail(&qp->q.lru_list, &qp->q.net->lru_list);
4787eb95156SPavel Emelyanov 	write_unlock(&ip4_frags.lock);
4791706d587SHerbert Xu 	return -EINPROGRESS;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds err:
4821da177e4SLinus Torvalds 	kfree_skb(skb);
4831706d587SHerbert Xu 	return err;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* Build a new IP datagram from all its fragments. */
4881da177e4SLinus Torvalds 
4891706d587SHerbert Xu static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
4901706d587SHerbert Xu 			 struct net_device *dev)
4911da177e4SLinus Torvalds {
4922bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(qp->q.net, struct net, ipv4.frags);
4931da177e4SLinus Torvalds 	struct iphdr *iph;
4945ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = qp->q.fragments;
4951da177e4SLinus Torvalds 	int len;
4961da177e4SLinus Torvalds 	int ihlen;
4971706d587SHerbert Xu 	int err;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	ipq_kill(qp);
5001da177e4SLinus Torvalds 
5011706d587SHerbert Xu 	/* Make the one we just received the head. */
5021706d587SHerbert Xu 	if (prev) {
5031706d587SHerbert Xu 		head = prev->next;
5041706d587SHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
5051706d587SHerbert Xu 		if (!fp)
5061706d587SHerbert Xu 			goto out_nomem;
5071706d587SHerbert Xu 
5081706d587SHerbert Xu 		fp->next = head->next;
5091706d587SHerbert Xu 		prev->next = fp;
5101706d587SHerbert Xu 
5115ab11c98SPavel Emelyanov 		skb_morph(head, qp->q.fragments);
5125ab11c98SPavel Emelyanov 		head->next = qp->q.fragments->next;
5131706d587SHerbert Xu 
5145ab11c98SPavel Emelyanov 		kfree_skb(qp->q.fragments);
5155ab11c98SPavel Emelyanov 		qp->q.fragments = head;
5161706d587SHerbert Xu 	}
5171706d587SHerbert Xu 
518547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
519547b792cSIlpo Järvinen 	WARN_ON(FRAG_CB(head)->offset != 0);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	/* Allocate a new buffer for the datagram. */
522c9bdd4b5SArnaldo Carvalho de Melo 	ihlen = ip_hdrlen(head);
5235ab11c98SPavel Emelyanov 	len = ihlen + qp->q.len;
5241da177e4SLinus Torvalds 
5251706d587SHerbert Xu 	err = -E2BIG;
5261da177e4SLinus Torvalds 	if (len > 65535)
5271da177e4SLinus Torvalds 		goto out_oversize;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
5301da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
5311da177e4SLinus Torvalds 		goto out_nomem;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
5341da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
5351da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
536d7fcf1a5SDavid S. Miller 	if (skb_has_frags(head)) {
5371da177e4SLinus Torvalds 		struct sk_buff *clone;
5381da177e4SLinus Torvalds 		int i, plen = 0;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
5411da177e4SLinus Torvalds 			goto out_nomem;
5421da177e4SLinus Torvalds 		clone->next = head->next;
5431da177e4SLinus Torvalds 		head->next = clone;
5441da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
545d7fcf1a5SDavid S. Miller 		skb_frag_list_init(head);
5461da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
5471da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
5481da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5491da177e4SLinus Torvalds 		head->data_len -= clone->len;
5501da177e4SLinus Torvalds 		head->len -= clone->len;
5511da177e4SLinus Torvalds 		clone->csum = 0;
5521da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5536ddc0822SPavel Emelyanov 		atomic_add(clone->truesize, &qp->q.net->mem);
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
557d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
5586ddc0822SPavel Emelyanov 	atomic_sub(head->truesize, &qp->q.net->mem);
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
5611da177e4SLinus Torvalds 		head->data_len += fp->len;
5621da177e4SLinus Torvalds 		head->len += fp->len;
5631da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
5641da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
56584fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
5661da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
5671da177e4SLinus Torvalds 		head->truesize += fp->truesize;
5686ddc0822SPavel Emelyanov 		atomic_sub(fp->truesize, &qp->q.net->mem);
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	head->next = NULL;
5721da177e4SLinus Torvalds 	head->dev = dev;
5735ab11c98SPavel Emelyanov 	head->tstamp = qp->q.stamp;
5741da177e4SLinus Torvalds 
575eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(head);
5761da177e4SLinus Torvalds 	iph->frag_off = 0;
5771da177e4SLinus Torvalds 	iph->tot_len = htons(len);
5782bad35b7SJorge Boncompte [DTI2] 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMOKS);
5795ab11c98SPavel Emelyanov 	qp->q.fragments = NULL;
5801706d587SHerbert Xu 	return 0;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds out_nomem:
58364ce2073SPatrick McHardy 	LIMIT_NETDEBUG(KERN_ERR "IP: queue_glue: no memory for gluing "
58464ce2073SPatrick McHardy 			      "queue %p\n", qp);
58545542479SDavid Howells 	err = -ENOMEM;
5861da177e4SLinus Torvalds 	goto out_fail;
5871da177e4SLinus Torvalds out_oversize:
5881da177e4SLinus Torvalds 	if (net_ratelimit())
589673d57e7SHarvey Harrison 		printk(KERN_INFO "Oversized IP packet from %pI4.\n",
590673d57e7SHarvey Harrison 			&qp->saddr);
5911da177e4SLinus Torvalds out_fail:
592bbf31bf1SDavid Ford 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
5931706d587SHerbert Xu 	return err;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds /* Process an incoming IP datagram fragment. */
597776c729eSHerbert Xu int ip_defrag(struct sk_buff *skb, u32 user)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct ipq *qp;
600ac18e750SPavel Emelyanov 	struct net *net;
6011da177e4SLinus Torvalds 
602adf30907SEric Dumazet 	net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
6037c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* Start by cleaning up the memory. */
606e31e0bdcSPavel Emelyanov 	if (atomic_read(&net->ipv4.frags.mem) > net->ipv4.frags.high_thresh)
6076ddc0822SPavel Emelyanov 		ip_evictor(net);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	/* Lookup (or create) queue header */
610ac18e750SPavel Emelyanov 	if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) {
6111706d587SHerbert Xu 		int ret;
6121da177e4SLinus Torvalds 
6135ab11c98SPavel Emelyanov 		spin_lock(&qp->q.lock);
6141da177e4SLinus Torvalds 
6151706d587SHerbert Xu 		ret = ip_frag_queue(qp, skb);
6161da177e4SLinus Torvalds 
6175ab11c98SPavel Emelyanov 		spin_unlock(&qp->q.lock);
6184b6cb5d8SPavel Emelyanov 		ipq_put(qp);
619776c729eSHerbert Xu 		return ret;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 
6227c73a6faSPavel Emelyanov 	IP_INC_STATS_BH(net, IPSTATS_MIB_REASMFAILS);
6231da177e4SLinus Torvalds 	kfree_skb(skb);
624776c729eSHerbert Xu 	return -ENOMEM;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6278d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
6288d8354d2SPavel Emelyanov static int zero;
6298d8354d2SPavel Emelyanov 
6300a64b4b8SPavel Emelyanov static struct ctl_table ip4_frags_ns_ctl_table[] = {
6318d8354d2SPavel Emelyanov 	{
6328d8354d2SPavel Emelyanov 		.procname	= "ipfrag_high_thresh",
633e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.high_thresh,
6348d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6358d8354d2SPavel Emelyanov 		.mode		= 0644,
6366d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
6378d8354d2SPavel Emelyanov 	},
6388d8354d2SPavel Emelyanov 	{
6398d8354d2SPavel Emelyanov 		.procname	= "ipfrag_low_thresh",
640e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv4.frags.low_thresh,
6418d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6428d8354d2SPavel Emelyanov 		.mode		= 0644,
6436d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
6448d8354d2SPavel Emelyanov 	},
6458d8354d2SPavel Emelyanov 	{
6468d8354d2SPavel Emelyanov 		.procname	= "ipfrag_time",
647b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv4.frags.timeout,
6488d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6498d8354d2SPavel Emelyanov 		.mode		= 0644,
6506d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6518d8354d2SPavel Emelyanov 	},
6527d291ebbSPavel Emelyanov 	{ }
6537d291ebbSPavel Emelyanov };
6547d291ebbSPavel Emelyanov 
6557d291ebbSPavel Emelyanov static struct ctl_table ip4_frags_ctl_table[] = {
6568d8354d2SPavel Emelyanov 	{
6578d8354d2SPavel Emelyanov 		.procname	= "ipfrag_secret_interval",
6583b4bc4a2SPavel Emelyanov 		.data		= &ip4_frags.secret_interval,
6598d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6608d8354d2SPavel Emelyanov 		.mode		= 0644,
6616d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6628d8354d2SPavel Emelyanov 	},
6638d8354d2SPavel Emelyanov 	{
6648d8354d2SPavel Emelyanov 		.procname	= "ipfrag_max_dist",
6658d8354d2SPavel Emelyanov 		.data		= &sysctl_ipfrag_max_dist,
6668d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6678d8354d2SPavel Emelyanov 		.mode		= 0644,
6686d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_minmax,
6698d8354d2SPavel Emelyanov 		.extra1		= &zero
6708d8354d2SPavel Emelyanov 	},
6718d8354d2SPavel Emelyanov 	{ }
6728d8354d2SPavel Emelyanov };
6738d8354d2SPavel Emelyanov 
6742c8c1e72SAlexey Dobriyan static int __net_init ip4_frags_ns_ctl_register(struct net *net)
6758d8354d2SPavel Emelyanov {
676e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6778d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6788d8354d2SPavel Emelyanov 
6790a64b4b8SPavel Emelyanov 	table = ip4_frags_ns_ctl_table;
68009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6810a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
682e4a2d5c2SPavel Emelyanov 		if (table == NULL)
683e4a2d5c2SPavel Emelyanov 			goto err_alloc;
684e4a2d5c2SPavel Emelyanov 
685e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv4.frags.high_thresh;
686e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv4.frags.low_thresh;
687b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv4.frags.timeout;
688e4a2d5c2SPavel Emelyanov 	}
689e4a2d5c2SPavel Emelyanov 
690e4a2d5c2SPavel Emelyanov 	hdr = register_net_sysctl_table(net, net_ipv4_ctl_path, table);
691e4a2d5c2SPavel Emelyanov 	if (hdr == NULL)
692e4a2d5c2SPavel Emelyanov 		goto err_reg;
693e4a2d5c2SPavel Emelyanov 
694e4a2d5c2SPavel Emelyanov 	net->ipv4.frags_hdr = hdr;
695e4a2d5c2SPavel Emelyanov 	return 0;
696e4a2d5c2SPavel Emelyanov 
697e4a2d5c2SPavel Emelyanov err_reg:
69809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
699e4a2d5c2SPavel Emelyanov 		kfree(table);
700e4a2d5c2SPavel Emelyanov err_alloc:
701e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
702e4a2d5c2SPavel Emelyanov }
703e4a2d5c2SPavel Emelyanov 
7042c8c1e72SAlexey Dobriyan static void __net_exit ip4_frags_ns_ctl_unregister(struct net *net)
705e4a2d5c2SPavel Emelyanov {
706e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
707e4a2d5c2SPavel Emelyanov 
708e4a2d5c2SPavel Emelyanov 	table = net->ipv4.frags_hdr->ctl_table_arg;
709e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv4.frags_hdr);
710e4a2d5c2SPavel Emelyanov 	kfree(table);
7118d8354d2SPavel Emelyanov }
7127d291ebbSPavel Emelyanov 
7137d291ebbSPavel Emelyanov static void ip4_frags_ctl_register(void)
7147d291ebbSPavel Emelyanov {
7157d291ebbSPavel Emelyanov 	register_net_sysctl_rotable(net_ipv4_ctl_path, ip4_frags_ctl_table);
7167d291ebbSPavel Emelyanov }
7178d8354d2SPavel Emelyanov #else
7180a64b4b8SPavel Emelyanov static inline int ip4_frags_ns_ctl_register(struct net *net)
7198d8354d2SPavel Emelyanov {
7208d8354d2SPavel Emelyanov 	return 0;
7218d8354d2SPavel Emelyanov }
722e4a2d5c2SPavel Emelyanov 
7230a64b4b8SPavel Emelyanov static inline void ip4_frags_ns_ctl_unregister(struct net *net)
724e4a2d5c2SPavel Emelyanov {
725e4a2d5c2SPavel Emelyanov }
7267d291ebbSPavel Emelyanov 
7277d291ebbSPavel Emelyanov static inline void ip4_frags_ctl_register(void)
7287d291ebbSPavel Emelyanov {
7297d291ebbSPavel Emelyanov }
7308d8354d2SPavel Emelyanov #endif
7318d8354d2SPavel Emelyanov 
7322c8c1e72SAlexey Dobriyan static int __net_init ipv4_frags_init_net(struct net *net)
7338d8354d2SPavel Emelyanov {
734b2fd5321SPavel Emelyanov 	/*
735e31e0bdcSPavel Emelyanov 	 * Fragment cache limits. We will commit 256K at one time. Should we
736e31e0bdcSPavel Emelyanov 	 * cross that limit we will prune down to 192K. This should cope with
737e31e0bdcSPavel Emelyanov 	 * even the most extreme cases without allowing an attacker to
738e31e0bdcSPavel Emelyanov 	 * measurably harm machine performance.
739e31e0bdcSPavel Emelyanov 	 */
740e31e0bdcSPavel Emelyanov 	net->ipv4.frags.high_thresh = 256 * 1024;
741e31e0bdcSPavel Emelyanov 	net->ipv4.frags.low_thresh = 192 * 1024;
742e31e0bdcSPavel Emelyanov 	/*
743b2fd5321SPavel Emelyanov 	 * Important NOTE! Fragment queue must be destroyed before MSL expires.
744b2fd5321SPavel Emelyanov 	 * RFC791 is wrong proposing to prolongate timer each fragment arrival
745b2fd5321SPavel Emelyanov 	 * by TTL.
746b2fd5321SPavel Emelyanov 	 */
747b2fd5321SPavel Emelyanov 	net->ipv4.frags.timeout = IP_FRAG_TIME;
748b2fd5321SPavel Emelyanov 
749e5a2bb84SPavel Emelyanov 	inet_frags_init_net(&net->ipv4.frags);
750e5a2bb84SPavel Emelyanov 
7510a64b4b8SPavel Emelyanov 	return ip4_frags_ns_ctl_register(net);
7528d8354d2SPavel Emelyanov }
7538d8354d2SPavel Emelyanov 
7542c8c1e72SAlexey Dobriyan static void __net_exit ipv4_frags_exit_net(struct net *net)
75581566e83SPavel Emelyanov {
7560a64b4b8SPavel Emelyanov 	ip4_frags_ns_ctl_unregister(net);
75781566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv4.frags, &ip4_frags);
75881566e83SPavel Emelyanov }
75981566e83SPavel Emelyanov 
76081566e83SPavel Emelyanov static struct pernet_operations ip4_frags_ops = {
76181566e83SPavel Emelyanov 	.init = ipv4_frags_init_net,
76281566e83SPavel Emelyanov 	.exit = ipv4_frags_exit_net,
76381566e83SPavel Emelyanov };
76481566e83SPavel Emelyanov 
765b7aa0bf7SEric Dumazet void __init ipfrag_init(void)
7661da177e4SLinus Torvalds {
7677d291ebbSPavel Emelyanov 	ip4_frags_ctl_register();
76881566e83SPavel Emelyanov 	register_pernet_subsys(&ip4_frags_ops);
769321a3a99SPavel Emelyanov 	ip4_frags.hashfn = ip4_hashfn;
770c6fda282SPavel Emelyanov 	ip4_frags.constructor = ip4_frag_init;
7711e4b8287SPavel Emelyanov 	ip4_frags.destructor = ip4_frag_free;
7721e4b8287SPavel Emelyanov 	ip4_frags.skb_free = NULL;
7731e4b8287SPavel Emelyanov 	ip4_frags.qsize = sizeof(struct ipq);
774abd6523dSPavel Emelyanov 	ip4_frags.match = ip4_frag_match;
775e521db9dSPavel Emelyanov 	ip4_frags.frag_expire = ip_expire;
7763b4bc4a2SPavel Emelyanov 	ip4_frags.secret_interval = 10 * 60 * HZ;
7777eb95156SPavel Emelyanov 	inet_frags_init(&ip4_frags);
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds EXPORT_SYMBOL(ip_defrag);
781