xref: /linux/net/ipv4/inetpeer.c (revision 53576d9b995605a9edc7414b900a9218c8f23b1f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *		INETPEER - A storage for permanent information about peers
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  This source is covered by the GNU GPL, the same as all kernel sources.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Version:	$Id: inetpeer.c,v 1.7 2001/09/20 21:22:50 davem Exp $
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  Authors:	Andrey V. Savochkin <saw@msu.ru>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/types.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/interrupt.h>
151da177e4SLinus Torvalds #include <linux/spinlock.h>
161da177e4SLinus Torvalds #include <linux/random.h>
171da177e4SLinus Torvalds #include <linux/sched.h>
181da177e4SLinus Torvalds #include <linux/timer.h>
191da177e4SLinus Torvalds #include <linux/time.h>
201da177e4SLinus Torvalds #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/mm.h>
221da177e4SLinus Torvalds #include <linux/net.h>
2320380731SArnaldo Carvalho de Melo #include <net/ip.h>
241da177e4SLinus Torvalds #include <net/inetpeer.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  *  Theory of operations.
281da177e4SLinus Torvalds  *  We keep one entry for each peer IP address.  The nodes contains long-living
291da177e4SLinus Torvalds  *  information about the peer which doesn't depend on routes.
301da177e4SLinus Torvalds  *  At this moment this information consists only of ID field for the next
311da177e4SLinus Torvalds  *  outgoing IP packet.  This field is incremented with each packet as encoded
321da177e4SLinus Torvalds  *  in inet_getid() function (include/net/inetpeer.h).
331da177e4SLinus Torvalds  *  At the moment of writing this notes identifier of IP packets is generated
341da177e4SLinus Torvalds  *  to be unpredictable using this code only for packets subjected
351da177e4SLinus Torvalds  *  (actually or potentially) to defragmentation.  I.e. DF packets less than
361da177e4SLinus Torvalds  *  PMTU in size uses a constant ID and do not use this code (see
371da177e4SLinus Torvalds  *  ip_select_ident() in include/net/ip.h).
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  *  Route cache entries hold references to our nodes.
401da177e4SLinus Torvalds  *  New cache entries get references via lookup by destination IP address in
411da177e4SLinus Torvalds  *  the avl tree.  The reference is grabbed only when it's needed i.e. only
421da177e4SLinus Torvalds  *  when we try to output IP packet which needs an unpredictable ID (see
431da177e4SLinus Torvalds  *  __ip_select_ident() in net/ipv4/route.c).
441da177e4SLinus Torvalds  *  Nodes are removed only when reference counter goes to 0.
451da177e4SLinus Torvalds  *  When it's happened the node may be removed when a sufficient amount of
461da177e4SLinus Torvalds  *  time has been passed since its last use.  The less-recently-used entry can
471da177e4SLinus Torvalds  *  also be removed if the pool is overloaded i.e. if the total amount of
481da177e4SLinus Torvalds  *  entries is greater-or-equal than the threshold.
491da177e4SLinus Torvalds  *
501da177e4SLinus Torvalds  *  Node pool is organised as an AVL tree.
511da177e4SLinus Torvalds  *  Such an implementation has been chosen not just for fun.  It's a way to
521da177e4SLinus Torvalds  *  prevent easy and efficient DoS attacks by creating hash collisions.  A huge
531da177e4SLinus Torvalds  *  amount of long living nodes in a single hash slot would significantly delay
541da177e4SLinus Torvalds  *  lookups performed with disabled BHs.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  *  Serialisation issues.
571da177e4SLinus Torvalds  *  1.  Nodes may appear in the tree only with the pool write lock held.
581da177e4SLinus Torvalds  *  2.  Nodes may disappear from the tree only with the pool write lock held
591da177e4SLinus Torvalds  *      AND reference count being 0.
601da177e4SLinus Torvalds  *  3.  Nodes appears and disappears from unused node list only under
611da177e4SLinus Torvalds  *      "inet_peer_unused_lock".
621da177e4SLinus Torvalds  *  4.  Global variable peer_total is modified under the pool lock.
631da177e4SLinus Torvalds  *  5.  struct inet_peer fields modification:
641da177e4SLinus Torvalds  *		avl_left, avl_right, avl_parent, avl_height: pool lock
651da177e4SLinus Torvalds  *		unused_next, unused_prevp: unused node list lock
661da177e4SLinus Torvalds  *		refcnt: atomically against modifications on other CPU;
671da177e4SLinus Torvalds  *		   usually under some other lock to prevent node disappearing
681da177e4SLinus Torvalds  *		dtime: unused node list lock
691da177e4SLinus Torvalds  *		v4daddr: unchangeable
701da177e4SLinus Torvalds  *		ip_id_count: idlock
711da177e4SLinus Torvalds  */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Exported for inet_getid inline function.  */
741da177e4SLinus Torvalds DEFINE_SPINLOCK(inet_peer_idlock);
751da177e4SLinus Torvalds 
76ba89966cSEric Dumazet static kmem_cache_t *peer_cachep __read_mostly;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define node_height(x) x->avl_height
791da177e4SLinus Torvalds static struct inet_peer peer_fake_node = {
801da177e4SLinus Torvalds 	.avl_left	= &peer_fake_node,
811da177e4SLinus Torvalds 	.avl_right	= &peer_fake_node,
821da177e4SLinus Torvalds 	.avl_height	= 0
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds #define peer_avl_empty (&peer_fake_node)
851da177e4SLinus Torvalds static struct inet_peer *peer_root = peer_avl_empty;
861da177e4SLinus Torvalds static DEFINE_RWLOCK(peer_pool_lock);
871da177e4SLinus Torvalds #define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */
881da177e4SLinus Torvalds 
897466d90fSHerbert Xu static int peer_total;
901da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
911da177e4SLinus Torvalds int inet_peer_threshold = 65536 + 128;	/* start to throw entries more
921da177e4SLinus Torvalds 					 * aggressively at this stage */
931da177e4SLinus Torvalds int inet_peer_minttl = 120 * HZ;	/* TTL under high load: 120 sec */
941da177e4SLinus Torvalds int inet_peer_maxttl = 10 * 60 * HZ;	/* usual time to live: 10 min */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static struct inet_peer *inet_peer_unused_head;
971da177e4SLinus Torvalds /* Exported for inet_putpeer inline function.  */
981da177e4SLinus Torvalds struct inet_peer **inet_peer_unused_tailp = &inet_peer_unused_head;
991da177e4SLinus Torvalds DEFINE_SPINLOCK(inet_peer_unused_lock);
1001da177e4SLinus Torvalds #define PEER_MAX_CLEANUP_WORK 30
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static void peer_check_expire(unsigned long dummy);
1038d06afabSIngo Molnar static DEFINE_TIMER(peer_periodic_timer, peer_check_expire, 0, 0);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
1061da177e4SLinus Torvalds int inet_peer_gc_mintime = 10 * HZ,
1071da177e4SLinus Torvalds     inet_peer_gc_maxtime = 120 * HZ;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /* Called from ip_output.c:ip_init  */
1101da177e4SLinus Torvalds void __init inet_initpeers(void)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	struct sysinfo si;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	/* Use the straight interface to information about memory. */
1151da177e4SLinus Torvalds 	si_meminfo(&si);
1161da177e4SLinus Torvalds 	/* The values below were suggested by Alexey Kuznetsov
1171da177e4SLinus Torvalds 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
1181da177e4SLinus Torvalds 	 * myself.  --SAW
1191da177e4SLinus Torvalds 	 */
1201da177e4SLinus Torvalds 	if (si.totalram <= (32768*1024)/PAGE_SIZE)
1211da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
1221da177e4SLinus Torvalds 	if (si.totalram <= (16384*1024)/PAGE_SIZE)
1231da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* about 512KB */
1241da177e4SLinus Torvalds 	if (si.totalram <= (8192*1024)/PAGE_SIZE)
1251da177e4SLinus Torvalds 		inet_peer_threshold >>= 2; /* about 128KB */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	peer_cachep = kmem_cache_create("inet_peer_cache",
1281da177e4SLinus Torvalds 			sizeof(struct inet_peer),
129e5d679f3SAlexey Dobriyan 			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1301da177e4SLinus Torvalds 			NULL, NULL);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* All the timers, started at system startup tend
1331da177e4SLinus Torvalds 	   to synchronize. Perturb it a bit.
1341da177e4SLinus Torvalds 	 */
1351da177e4SLinus Torvalds 	peer_periodic_timer.expires = jiffies
1361da177e4SLinus Torvalds 		+ net_random() % inet_peer_gc_maxtime
1371da177e4SLinus Torvalds 		+ inet_peer_gc_maxtime;
1381da177e4SLinus Torvalds 	add_timer(&peer_periodic_timer);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /* Called with or without local BH being disabled. */
1421da177e4SLinus Torvalds static void unlink_from_unused(struct inet_peer *p)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	spin_lock_bh(&inet_peer_unused_lock);
1451da177e4SLinus Torvalds 	if (p->unused_prevp != NULL) {
1461da177e4SLinus Torvalds 		/* On unused list. */
1471da177e4SLinus Torvalds 		*p->unused_prevp = p->unused_next;
1481da177e4SLinus Torvalds 		if (p->unused_next != NULL)
1491da177e4SLinus Torvalds 			p->unused_next->unused_prevp = p->unused_prevp;
1501da177e4SLinus Torvalds 		else
1511da177e4SLinus Torvalds 			inet_peer_unused_tailp = p->unused_prevp;
1521da177e4SLinus Torvalds 		p->unused_prevp = NULL; /* mark it as removed */
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	spin_unlock_bh(&inet_peer_unused_lock);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /* Called with local BH disabled and the pool lock held. */
1581da177e4SLinus Torvalds #define lookup(daddr) 						\
1591da177e4SLinus Torvalds ({								\
1601da177e4SLinus Torvalds 	struct inet_peer *u, **v;				\
1611da177e4SLinus Torvalds 	stackptr = stack;					\
1621da177e4SLinus Torvalds 	*stackptr++ = &peer_root;				\
1631da177e4SLinus Torvalds 	for (u = peer_root; u != peer_avl_empty; ) {		\
1641da177e4SLinus Torvalds 		if (daddr == u->v4daddr)			\
1651da177e4SLinus Torvalds 			break;					\
166*53576d9bSAl Viro 		if ((__force __u32)daddr < (__force __u32)u->v4daddr)	\
1671da177e4SLinus Torvalds 			v = &u->avl_left;			\
1681da177e4SLinus Torvalds 		else						\
1691da177e4SLinus Torvalds 			v = &u->avl_right;			\
1701da177e4SLinus Torvalds 		*stackptr++ = v;				\
1711da177e4SLinus Torvalds 		u = *v;						\
1721da177e4SLinus Torvalds 	}							\
1731da177e4SLinus Torvalds 	u;							\
1741da177e4SLinus Torvalds })
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds /* Called with local BH disabled and the pool write lock held. */
1771da177e4SLinus Torvalds #define lookup_rightempty(start)				\
1781da177e4SLinus Torvalds ({								\
1791da177e4SLinus Torvalds 	struct inet_peer *u, **v;				\
1801da177e4SLinus Torvalds 	*stackptr++ = &start->avl_left;				\
1811da177e4SLinus Torvalds 	v = &start->avl_left;					\
1821da177e4SLinus Torvalds 	for (u = *v; u->avl_right != peer_avl_empty; ) {	\
1831da177e4SLinus Torvalds 		v = &u->avl_right;				\
1841da177e4SLinus Torvalds 		*stackptr++ = v;				\
1851da177e4SLinus Torvalds 		u = *v;						\
1861da177e4SLinus Torvalds 	}							\
1871da177e4SLinus Torvalds 	u;							\
1881da177e4SLinus Torvalds })
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /* Called with local BH disabled and the pool write lock held.
1911da177e4SLinus Torvalds  * Variable names are the proof of operation correctness.
1921da177e4SLinus Torvalds  * Look into mm/map_avl.c for more detail description of the ideas.  */
1931da177e4SLinus Torvalds static void peer_avl_rebalance(struct inet_peer **stack[],
1941da177e4SLinus Torvalds 		struct inet_peer ***stackend)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	struct inet_peer **nodep, *node, *l, *r;
1971da177e4SLinus Torvalds 	int lh, rh;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	while (stackend > stack) {
2001da177e4SLinus Torvalds 		nodep = *--stackend;
2011da177e4SLinus Torvalds 		node = *nodep;
2021da177e4SLinus Torvalds 		l = node->avl_left;
2031da177e4SLinus Torvalds 		r = node->avl_right;
2041da177e4SLinus Torvalds 		lh = node_height(l);
2051da177e4SLinus Torvalds 		rh = node_height(r);
2061da177e4SLinus Torvalds 		if (lh > rh + 1) { /* l: RH+2 */
2071da177e4SLinus Torvalds 			struct inet_peer *ll, *lr, *lrl, *lrr;
2081da177e4SLinus Torvalds 			int lrh;
2091da177e4SLinus Torvalds 			ll = l->avl_left;
2101da177e4SLinus Torvalds 			lr = l->avl_right;
2111da177e4SLinus Torvalds 			lrh = node_height(lr);
2121da177e4SLinus Torvalds 			if (lrh <= node_height(ll)) {	/* ll: RH+1 */
2131da177e4SLinus Torvalds 				node->avl_left = lr;	/* lr: RH or RH+1 */
2141da177e4SLinus Torvalds 				node->avl_right = r;	/* r: RH */
2151da177e4SLinus Torvalds 				node->avl_height = lrh + 1; /* RH+1 or RH+2 */
2161da177e4SLinus Torvalds 				l->avl_left = ll;	/* ll: RH+1 */
2171da177e4SLinus Torvalds 				l->avl_right = node;	/* node: RH+1 or RH+2 */
2181da177e4SLinus Torvalds 				l->avl_height = node->avl_height + 1;
2191da177e4SLinus Torvalds 				*nodep = l;
2201da177e4SLinus Torvalds 			} else { /* ll: RH, lr: RH+1 */
2211da177e4SLinus Torvalds 				lrl = lr->avl_left;	/* lrl: RH or RH-1 */
2221da177e4SLinus Torvalds 				lrr = lr->avl_right;	/* lrr: RH or RH-1 */
2231da177e4SLinus Torvalds 				node->avl_left = lrr;	/* lrr: RH or RH-1 */
2241da177e4SLinus Torvalds 				node->avl_right = r;	/* r: RH */
2251da177e4SLinus Torvalds 				node->avl_height = rh + 1; /* node: RH+1 */
2261da177e4SLinus Torvalds 				l->avl_left = ll;	/* ll: RH */
2271da177e4SLinus Torvalds 				l->avl_right = lrl;	/* lrl: RH or RH-1 */
2281da177e4SLinus Torvalds 				l->avl_height = rh + 1;	/* l: RH+1 */
2291da177e4SLinus Torvalds 				lr->avl_left = l;	/* l: RH+1 */
2301da177e4SLinus Torvalds 				lr->avl_right = node;	/* node: RH+1 */
2311da177e4SLinus Torvalds 				lr->avl_height = rh + 2;
2321da177e4SLinus Torvalds 				*nodep = lr;
2331da177e4SLinus Torvalds 			}
2341da177e4SLinus Torvalds 		} else if (rh > lh + 1) { /* r: LH+2 */
2351da177e4SLinus Torvalds 			struct inet_peer *rr, *rl, *rlr, *rll;
2361da177e4SLinus Torvalds 			int rlh;
2371da177e4SLinus Torvalds 			rr = r->avl_right;
2381da177e4SLinus Torvalds 			rl = r->avl_left;
2391da177e4SLinus Torvalds 			rlh = node_height(rl);
2401da177e4SLinus Torvalds 			if (rlh <= node_height(rr)) {	/* rr: LH+1 */
2411da177e4SLinus Torvalds 				node->avl_right = rl;	/* rl: LH or LH+1 */
2421da177e4SLinus Torvalds 				node->avl_left = l;	/* l: LH */
2431da177e4SLinus Torvalds 				node->avl_height = rlh + 1; /* LH+1 or LH+2 */
2441da177e4SLinus Torvalds 				r->avl_right = rr;	/* rr: LH+1 */
2451da177e4SLinus Torvalds 				r->avl_left = node;	/* node: LH+1 or LH+2 */
2461da177e4SLinus Torvalds 				r->avl_height = node->avl_height + 1;
2471da177e4SLinus Torvalds 				*nodep = r;
2481da177e4SLinus Torvalds 			} else { /* rr: RH, rl: RH+1 */
2491da177e4SLinus Torvalds 				rlr = rl->avl_right;	/* rlr: LH or LH-1 */
2501da177e4SLinus Torvalds 				rll = rl->avl_left;	/* rll: LH or LH-1 */
2511da177e4SLinus Torvalds 				node->avl_right = rll;	/* rll: LH or LH-1 */
2521da177e4SLinus Torvalds 				node->avl_left = l;	/* l: LH */
2531da177e4SLinus Torvalds 				node->avl_height = lh + 1; /* node: LH+1 */
2541da177e4SLinus Torvalds 				r->avl_right = rr;	/* rr: LH */
2551da177e4SLinus Torvalds 				r->avl_left = rlr;	/* rlr: LH or LH-1 */
2561da177e4SLinus Torvalds 				r->avl_height = lh + 1;	/* r: LH+1 */
2571da177e4SLinus Torvalds 				rl->avl_right = r;	/* r: LH+1 */
2581da177e4SLinus Torvalds 				rl->avl_left = node;	/* node: LH+1 */
2591da177e4SLinus Torvalds 				rl->avl_height = lh + 2;
2601da177e4SLinus Torvalds 				*nodep = rl;
2611da177e4SLinus Torvalds 			}
2621da177e4SLinus Torvalds 		} else {
2631da177e4SLinus Torvalds 			node->avl_height = (lh > rh ? lh : rh) + 1;
2641da177e4SLinus Torvalds 		}
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /* Called with local BH disabled and the pool write lock held. */
2691da177e4SLinus Torvalds #define link_to_pool(n)						\
2701da177e4SLinus Torvalds do {								\
2711da177e4SLinus Torvalds 	n->avl_height = 1;					\
2721da177e4SLinus Torvalds 	n->avl_left = peer_avl_empty;				\
2731da177e4SLinus Torvalds 	n->avl_right = peer_avl_empty;				\
2741da177e4SLinus Torvalds 	**--stackptr = n;					\
2751da177e4SLinus Torvalds 	peer_avl_rebalance(stack, stackptr);			\
2761da177e4SLinus Torvalds } while(0)
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds /* May be called with local BH enabled. */
2791da177e4SLinus Torvalds static void unlink_from_pool(struct inet_peer *p)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int do_free;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	do_free = 0;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	write_lock_bh(&peer_pool_lock);
2861da177e4SLinus Torvalds 	/* Check the reference counter.  It was artificially incremented by 1
2871da177e4SLinus Torvalds 	 * in cleanup() function to prevent sudden disappearing.  If the
2881da177e4SLinus Torvalds 	 * reference count is still 1 then the node is referenced only as `p'
2891da177e4SLinus Torvalds 	 * here and from the pool.  So under the exclusive pool lock it's safe
2901da177e4SLinus Torvalds 	 * to remove the node and free it later. */
2911da177e4SLinus Torvalds 	if (atomic_read(&p->refcnt) == 1) {
2921da177e4SLinus Torvalds 		struct inet_peer **stack[PEER_MAXDEPTH];
2931da177e4SLinus Torvalds 		struct inet_peer ***stackptr, ***delp;
2941da177e4SLinus Torvalds 		if (lookup(p->v4daddr) != p)
2951da177e4SLinus Torvalds 			BUG();
2961da177e4SLinus Torvalds 		delp = stackptr - 1; /* *delp[0] == p */
2971da177e4SLinus Torvalds 		if (p->avl_left == peer_avl_empty) {
2981da177e4SLinus Torvalds 			*delp[0] = p->avl_right;
2991da177e4SLinus Torvalds 			--stackptr;
3001da177e4SLinus Torvalds 		} else {
3011da177e4SLinus Torvalds 			/* look for a node to insert instead of p */
3021da177e4SLinus Torvalds 			struct inet_peer *t;
3031da177e4SLinus Torvalds 			t = lookup_rightempty(p);
30409a62660SKris Katterjohn 			BUG_ON(*stackptr[-1] != t);
3051da177e4SLinus Torvalds 			**--stackptr = t->avl_left;
3061da177e4SLinus Torvalds 			/* t is removed, t->v4daddr > x->v4daddr for any
3071da177e4SLinus Torvalds 			 * x in p->avl_left subtree.
3081da177e4SLinus Torvalds 			 * Put t in the old place of p. */
3091da177e4SLinus Torvalds 			*delp[0] = t;
3101da177e4SLinus Torvalds 			t->avl_left = p->avl_left;
3111da177e4SLinus Torvalds 			t->avl_right = p->avl_right;
3121da177e4SLinus Torvalds 			t->avl_height = p->avl_height;
31309a62660SKris Katterjohn 			BUG_ON(delp[1] != &p->avl_left);
3141da177e4SLinus Torvalds 			delp[1] = &t->avl_left; /* was &p->avl_left */
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 		peer_avl_rebalance(stack, stackptr);
3171da177e4SLinus Torvalds 		peer_total--;
3181da177e4SLinus Torvalds 		do_free = 1;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 	write_unlock_bh(&peer_pool_lock);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	if (do_free)
3231da177e4SLinus Torvalds 		kmem_cache_free(peer_cachep, p);
3241da177e4SLinus Torvalds 	else
3251da177e4SLinus Torvalds 		/* The node is used again.  Decrease the reference counter
3261da177e4SLinus Torvalds 		 * back.  The loop "cleanup -> unlink_from_unused
3271da177e4SLinus Torvalds 		 *   -> unlink_from_pool -> putpeer -> link_to_unused
3281da177e4SLinus Torvalds 		 *   -> cleanup (for the same node)"
3291da177e4SLinus Torvalds 		 * doesn't really exist because the entry will have a
3301da177e4SLinus Torvalds 		 * recent deletion time and will not be cleaned again soon. */
3311da177e4SLinus Torvalds 		inet_putpeer(p);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /* May be called with local BH enabled. */
3351da177e4SLinus Torvalds static int cleanup_once(unsigned long ttl)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	struct inet_peer *p;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/* Remove the first entry from the list of unused nodes. */
3401da177e4SLinus Torvalds 	spin_lock_bh(&inet_peer_unused_lock);
3411da177e4SLinus Torvalds 	p = inet_peer_unused_head;
3421da177e4SLinus Torvalds 	if (p != NULL) {
3431da177e4SLinus Torvalds 		if (time_after(p->dtime + ttl, jiffies)) {
3441da177e4SLinus Torvalds 			/* Do not prune fresh entries. */
3451da177e4SLinus Torvalds 			spin_unlock_bh(&inet_peer_unused_lock);
3461da177e4SLinus Torvalds 			return -1;
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 		inet_peer_unused_head = p->unused_next;
3491da177e4SLinus Torvalds 		if (p->unused_next != NULL)
3501da177e4SLinus Torvalds 			p->unused_next->unused_prevp = p->unused_prevp;
3511da177e4SLinus Torvalds 		else
3521da177e4SLinus Torvalds 			inet_peer_unused_tailp = p->unused_prevp;
3531da177e4SLinus Torvalds 		p->unused_prevp = NULL; /* mark as not on the list */
3541da177e4SLinus Torvalds 		/* Grab an extra reference to prevent node disappearing
3551da177e4SLinus Torvalds 		 * before unlink_from_pool() call. */
3561da177e4SLinus Torvalds 		atomic_inc(&p->refcnt);
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 	spin_unlock_bh(&inet_peer_unused_lock);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (p == NULL)
3611da177e4SLinus Torvalds 		/* It means that the total number of USED entries has
3621da177e4SLinus Torvalds 		 * grown over inet_peer_threshold.  It shouldn't really
3631da177e4SLinus Torvalds 		 * happen because of entry limits in route cache. */
3641da177e4SLinus Torvalds 		return -1;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	unlink_from_pool(p);
3671da177e4SLinus Torvalds 	return 0;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /* Called with or without local BH being disabled. */
371*53576d9bSAl Viro struct inet_peer *inet_getpeer(__be32 daddr, int create)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct inet_peer *p, *n;
3741da177e4SLinus Torvalds 	struct inet_peer **stack[PEER_MAXDEPTH], ***stackptr;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	/* Look up for the address quickly. */
3771da177e4SLinus Torvalds 	read_lock_bh(&peer_pool_lock);
3781da177e4SLinus Torvalds 	p = lookup(daddr);
3791da177e4SLinus Torvalds 	if (p != peer_avl_empty)
3801da177e4SLinus Torvalds 		atomic_inc(&p->refcnt);
3811da177e4SLinus Torvalds 	read_unlock_bh(&peer_pool_lock);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	if (p != peer_avl_empty) {
3841da177e4SLinus Torvalds 		/* The existing node has been found. */
3851da177e4SLinus Torvalds 		/* Remove the entry from unused list if it was there. */
3861da177e4SLinus Torvalds 		unlink_from_unused(p);
3871da177e4SLinus Torvalds 		return p;
3881da177e4SLinus Torvalds 	}
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (!create)
3911da177e4SLinus Torvalds 		return NULL;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/* Allocate the space outside the locked region. */
3941da177e4SLinus Torvalds 	n = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
3951da177e4SLinus Torvalds 	if (n == NULL)
3961da177e4SLinus Torvalds 		return NULL;
3971da177e4SLinus Torvalds 	n->v4daddr = daddr;
3981da177e4SLinus Torvalds 	atomic_set(&n->refcnt, 1);
39989cee8b1SHerbert Xu 	atomic_set(&n->rid, 0);
4001da177e4SLinus Torvalds 	n->ip_id_count = secure_ip_id(daddr);
4011da177e4SLinus Torvalds 	n->tcp_ts_stamp = 0;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	write_lock_bh(&peer_pool_lock);
4041da177e4SLinus Torvalds 	/* Check if an entry has suddenly appeared. */
4051da177e4SLinus Torvalds 	p = lookup(daddr);
4061da177e4SLinus Torvalds 	if (p != peer_avl_empty)
4071da177e4SLinus Torvalds 		goto out_free;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/* Link the node. */
4101da177e4SLinus Torvalds 	link_to_pool(n);
4111da177e4SLinus Torvalds 	n->unused_prevp = NULL; /* not on the list */
4121da177e4SLinus Torvalds 	peer_total++;
4131da177e4SLinus Torvalds 	write_unlock_bh(&peer_pool_lock);
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	if (peer_total >= inet_peer_threshold)
4161da177e4SLinus Torvalds 		/* Remove one less-recently-used entry. */
4171da177e4SLinus Torvalds 		cleanup_once(0);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	return n;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds out_free:
4221da177e4SLinus Torvalds 	/* The appropriate node is already in the pool. */
4231da177e4SLinus Torvalds 	atomic_inc(&p->refcnt);
4241da177e4SLinus Torvalds 	write_unlock_bh(&peer_pool_lock);
4251da177e4SLinus Torvalds 	/* Remove the entry from unused list if it was there. */
4261da177e4SLinus Torvalds 	unlink_from_unused(p);
4271da177e4SLinus Torvalds 	/* Free preallocated the preallocated node. */
4281da177e4SLinus Torvalds 	kmem_cache_free(peer_cachep, n);
4291da177e4SLinus Torvalds 	return p;
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds /* Called with local BH disabled. */
4331da177e4SLinus Torvalds static void peer_check_expire(unsigned long dummy)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	int i;
4361da177e4SLinus Torvalds 	int ttl;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	if (peer_total >= inet_peer_threshold)
4391da177e4SLinus Torvalds 		ttl = inet_peer_minttl;
4401da177e4SLinus Torvalds 	else
4411da177e4SLinus Torvalds 		ttl = inet_peer_maxttl
4421da177e4SLinus Torvalds 				- (inet_peer_maxttl - inet_peer_minttl) / HZ *
4431da177e4SLinus Torvalds 					peer_total / inet_peer_threshold * HZ;
4441da177e4SLinus Torvalds 	for (i = 0; i < PEER_MAX_CLEANUP_WORK && !cleanup_once(ttl); i++);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/* Trigger the timer after inet_peer_gc_mintime .. inet_peer_gc_maxtime
4471da177e4SLinus Torvalds 	 * interval depending on the total number of entries (more entries,
4481da177e4SLinus Torvalds 	 * less interval). */
4491344a416SDave Johnson 	if (peer_total >= inet_peer_threshold)
4501344a416SDave Johnson 		peer_periodic_timer.expires = jiffies + inet_peer_gc_mintime;
4511344a416SDave Johnson 	else
4521da177e4SLinus Torvalds 		peer_periodic_timer.expires = jiffies
4531da177e4SLinus Torvalds 			+ inet_peer_gc_maxtime
4541da177e4SLinus Torvalds 			- (inet_peer_gc_maxtime - inet_peer_gc_mintime) / HZ *
4551da177e4SLinus Torvalds 				peer_total / inet_peer_threshold * HZ;
4561da177e4SLinus Torvalds 	add_timer(&peer_periodic_timer);
4571da177e4SLinus Torvalds }
458