xref: /linux/net/ipv4/inetpeer.c (revision b914c4ea929a4ba6fb97967800dc473c31552b98)
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  *  Authors:	Andrey V. Savochkin <saw@msu.ru>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/interrupt.h>
131da177e4SLinus Torvalds #include <linux/spinlock.h>
141da177e4SLinus Torvalds #include <linux/random.h>
151da177e4SLinus Torvalds #include <linux/timer.h>
161da177e4SLinus Torvalds #include <linux/time.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/mm.h>
191da177e4SLinus Torvalds #include <linux/net.h>
2020380731SArnaldo Carvalho de Melo #include <net/ip.h>
211da177e4SLinus Torvalds #include <net/inetpeer.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  *  Theory of operations.
251da177e4SLinus Torvalds  *  We keep one entry for each peer IP address.  The nodes contains long-living
261da177e4SLinus Torvalds  *  information about the peer which doesn't depend on routes.
271da177e4SLinus Torvalds  *  At this moment this information consists only of ID field for the next
281da177e4SLinus Torvalds  *  outgoing IP packet.  This field is incremented with each packet as encoded
291da177e4SLinus Torvalds  *  in inet_getid() function (include/net/inetpeer.h).
301da177e4SLinus Torvalds  *  At the moment of writing this notes identifier of IP packets is generated
311da177e4SLinus Torvalds  *  to be unpredictable using this code only for packets subjected
321da177e4SLinus Torvalds  *  (actually or potentially) to defragmentation.  I.e. DF packets less than
331da177e4SLinus Torvalds  *  PMTU in size uses a constant ID and do not use this code (see
341da177e4SLinus Torvalds  *  ip_select_ident() in include/net/ip.h).
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *  Route cache entries hold references to our nodes.
371da177e4SLinus Torvalds  *  New cache entries get references via lookup by destination IP address in
381da177e4SLinus Torvalds  *  the avl tree.  The reference is grabbed only when it's needed i.e. only
391da177e4SLinus Torvalds  *  when we try to output IP packet which needs an unpredictable ID (see
401da177e4SLinus Torvalds  *  __ip_select_ident() in net/ipv4/route.c).
411da177e4SLinus Torvalds  *  Nodes are removed only when reference counter goes to 0.
421da177e4SLinus Torvalds  *  When it's happened the node may be removed when a sufficient amount of
431da177e4SLinus Torvalds  *  time has been passed since its last use.  The less-recently-used entry can
441da177e4SLinus Torvalds  *  also be removed if the pool is overloaded i.e. if the total amount of
451da177e4SLinus Torvalds  *  entries is greater-or-equal than the threshold.
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  *  Node pool is organised as an AVL tree.
481da177e4SLinus Torvalds  *  Such an implementation has been chosen not just for fun.  It's a way to
491da177e4SLinus Torvalds  *  prevent easy and efficient DoS attacks by creating hash collisions.  A huge
501da177e4SLinus Torvalds  *  amount of long living nodes in a single hash slot would significantly delay
511da177e4SLinus Torvalds  *  lookups performed with disabled BHs.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  *  Serialisation issues.
54aa1039e7SEric Dumazet  *  1.  Nodes may appear in the tree only with the pool lock held.
55aa1039e7SEric Dumazet  *  2.  Nodes may disappear from the tree only with the pool lock held
561da177e4SLinus Torvalds  *      AND reference count being 0.
571da177e4SLinus Torvalds  *  3.  Nodes appears and disappears from unused node list only under
581da177e4SLinus Torvalds  *      "inet_peer_unused_lock".
591da177e4SLinus Torvalds  *  4.  Global variable peer_total is modified under the pool lock.
601da177e4SLinus Torvalds  *  5.  struct inet_peer fields modification:
611da177e4SLinus Torvalds  *		avl_left, avl_right, avl_parent, avl_height: pool lock
62d71209deSPavel Emelyanov  *		unused: unused node list lock
631da177e4SLinus Torvalds  *		refcnt: atomically against modifications on other CPU;
641da177e4SLinus Torvalds  *		   usually under some other lock to prevent node disappearing
651da177e4SLinus Torvalds  *		dtime: unused node list lock
661da177e4SLinus Torvalds  *		v4daddr: unchangeable
67317fe0e6SEric Dumazet  *		ip_id_count: atomic value (no lock needed)
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
70e18b890bSChristoph Lameter static struct kmem_cache *peer_cachep __read_mostly;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define node_height(x) x->avl_height
73d6cc1d64SEric Dumazet 
74d6cc1d64SEric Dumazet #define peer_avl_empty ((struct inet_peer *)&peer_fake_node)
75*b914c4eaSEric Dumazet #define peer_avl_empty_rcu ((struct inet_peer __rcu __force *)&peer_fake_node)
76d6cc1d64SEric Dumazet static const struct inet_peer peer_fake_node = {
77*b914c4eaSEric Dumazet 	.avl_left	= peer_avl_empty_rcu,
78*b914c4eaSEric Dumazet 	.avl_right	= peer_avl_empty_rcu,
791da177e4SLinus Torvalds 	.avl_height	= 0
801da177e4SLinus Torvalds };
81d6cc1d64SEric Dumazet 
82d6cc1d64SEric Dumazet static struct {
83*b914c4eaSEric Dumazet 	struct inet_peer __rcu *root;
84aa1039e7SEric Dumazet 	spinlock_t	lock;
85d6cc1d64SEric Dumazet 	int		total;
86d6cc1d64SEric Dumazet } peers = {
87*b914c4eaSEric Dumazet 	.root		= peer_avl_empty_rcu,
88aa1039e7SEric Dumazet 	.lock		= __SPIN_LOCK_UNLOCKED(peers.lock),
89d6cc1d64SEric Dumazet 	.total		= 0,
90d6cc1d64SEric Dumazet };
911da177e4SLinus Torvalds #define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
94243bbcaaSEric Dumazet int inet_peer_threshold __read_mostly = 65536 + 128;	/* start to throw entries more
951da177e4SLinus Torvalds 					 * aggressively at this stage */
96243bbcaaSEric Dumazet int inet_peer_minttl __read_mostly = 120 * HZ;	/* TTL under high load: 120 sec */
97243bbcaaSEric Dumazet int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;	/* usual time to live: 10 min */
98243bbcaaSEric Dumazet int inet_peer_gc_mintime __read_mostly = 10 * HZ;
99243bbcaaSEric Dumazet int inet_peer_gc_maxtime __read_mostly = 120 * HZ;
1001da177e4SLinus Torvalds 
101d6cc1d64SEric Dumazet static struct {
102d6cc1d64SEric Dumazet 	struct list_head	list;
103d6cc1d64SEric Dumazet 	spinlock_t		lock;
104d6cc1d64SEric Dumazet } unused_peers = {
105d6cc1d64SEric Dumazet 	.list			= LIST_HEAD_INIT(unused_peers.list),
106d6cc1d64SEric Dumazet 	.lock			= __SPIN_LOCK_UNLOCKED(unused_peers.lock),
107d6cc1d64SEric Dumazet };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static void peer_check_expire(unsigned long dummy);
1108d06afabSIngo Molnar static DEFINE_TIMER(peer_periodic_timer, peer_check_expire, 0, 0);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* Called from ip_output.c:ip_init  */
1141da177e4SLinus Torvalds void __init inet_initpeers(void)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct sysinfo si;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	/* Use the straight interface to information about memory. */
1191da177e4SLinus Torvalds 	si_meminfo(&si);
1201da177e4SLinus Torvalds 	/* The values below were suggested by Alexey Kuznetsov
1211da177e4SLinus Torvalds 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
1221da177e4SLinus Torvalds 	 * myself.  --SAW
1231da177e4SLinus Torvalds 	 */
1241da177e4SLinus Torvalds 	if (si.totalram <= (32768*1024)/PAGE_SIZE)
1251da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
1261da177e4SLinus Torvalds 	if (si.totalram <= (16384*1024)/PAGE_SIZE)
1271da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* about 512KB */
1281da177e4SLinus Torvalds 	if (si.totalram <= (8192*1024)/PAGE_SIZE)
1291da177e4SLinus Torvalds 		inet_peer_threshold >>= 2; /* about 128KB */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	peer_cachep = kmem_cache_create("inet_peer_cache",
1321da177e4SLinus Torvalds 			sizeof(struct inet_peer),
133317fe0e6SEric Dumazet 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
13420c2df83SPaul Mundt 			NULL);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* All the timers, started at system startup tend
1371da177e4SLinus Torvalds 	   to synchronize. Perturb it a bit.
1381da177e4SLinus Torvalds 	 */
1391da177e4SLinus Torvalds 	peer_periodic_timer.expires = jiffies
1401da177e4SLinus Torvalds 		+ net_random() % inet_peer_gc_maxtime
1411da177e4SLinus Torvalds 		+ inet_peer_gc_maxtime;
1421da177e4SLinus Torvalds 	add_timer(&peer_periodic_timer);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /* Called with or without local BH being disabled. */
1461da177e4SLinus Torvalds static void unlink_from_unused(struct inet_peer *p)
1471da177e4SLinus Torvalds {
148d6cc1d64SEric Dumazet 	if (!list_empty(&p->unused)) {
149d6cc1d64SEric Dumazet 		spin_lock_bh(&unused_peers.lock);
150d71209deSPavel Emelyanov 		list_del_init(&p->unused);
151d6cc1d64SEric Dumazet 		spin_unlock_bh(&unused_peers.lock);
152d6cc1d64SEric Dumazet 	}
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
155243bbcaaSEric Dumazet /*
156243bbcaaSEric Dumazet  * Called with local BH disabled and the pool lock held.
157243bbcaaSEric Dumazet  */
158243bbcaaSEric Dumazet #define lookup(_daddr, _stack) 					\
1591da177e4SLinus Torvalds ({								\
160*b914c4eaSEric Dumazet 	struct inet_peer *u;					\
161*b914c4eaSEric Dumazet 	struct inet_peer __rcu **v;				\
162aa1039e7SEric Dumazet 								\
163243bbcaaSEric Dumazet 	stackptr = _stack;					\
164d6cc1d64SEric Dumazet 	*stackptr++ = &peers.root;				\
165*b914c4eaSEric Dumazet 	for (u = rcu_dereference_protected(peers.root,		\
166*b914c4eaSEric Dumazet 			lockdep_is_held(&peers.lock));		\
167*b914c4eaSEric Dumazet 	     u != peer_avl_empty; ) {				\
168243bbcaaSEric Dumazet 		if (_daddr == u->v4daddr)			\
1691da177e4SLinus Torvalds 			break;					\
170243bbcaaSEric Dumazet 		if ((__force __u32)_daddr < (__force __u32)u->v4daddr)	\
1711da177e4SLinus Torvalds 			v = &u->avl_left;			\
1721da177e4SLinus Torvalds 		else						\
1731da177e4SLinus Torvalds 			v = &u->avl_right;			\
1741da177e4SLinus Torvalds 		*stackptr++ = v;				\
175*b914c4eaSEric Dumazet 		u = rcu_dereference_protected(*v,		\
176*b914c4eaSEric Dumazet 			lockdep_is_held(&peers.lock));		\
1771da177e4SLinus Torvalds 	}							\
1781da177e4SLinus Torvalds 	u;							\
1791da177e4SLinus Torvalds })
1801da177e4SLinus Torvalds 
181aa1039e7SEric Dumazet /*
182aa1039e7SEric Dumazet  * Called with rcu_read_lock_bh()
183aa1039e7SEric Dumazet  * Because we hold no lock against a writer, its quite possible we fall
184aa1039e7SEric Dumazet  * in an endless loop.
185aa1039e7SEric Dumazet  * But every pointer we follow is guaranteed to be valid thanks to RCU.
186aa1039e7SEric Dumazet  * We exit from this function if number of links exceeds PEER_MAXDEPTH
187aa1039e7SEric Dumazet  */
188aa1039e7SEric Dumazet static struct inet_peer *lookup_rcu_bh(__be32 daddr)
189aa1039e7SEric Dumazet {
190aa1039e7SEric Dumazet 	struct inet_peer *u = rcu_dereference_bh(peers.root);
191aa1039e7SEric Dumazet 	int count = 0;
192aa1039e7SEric Dumazet 
193aa1039e7SEric Dumazet 	while (u != peer_avl_empty) {
194aa1039e7SEric Dumazet 		if (daddr == u->v4daddr) {
1955f2f8920SEric Dumazet 			/* Before taking a reference, check if this entry was
1965f2f8920SEric Dumazet 			 * deleted, unlink_from_pool() sets refcnt=-1 to make
1975f2f8920SEric Dumazet 			 * distinction between an unused entry (refcnt=0) and
1985f2f8920SEric Dumazet 			 * a freed one.
1995f2f8920SEric Dumazet 			 */
2005f2f8920SEric Dumazet 			if (unlikely(!atomic_add_unless(&u->refcnt, 1, -1)))
201aa1039e7SEric Dumazet 				u = NULL;
202aa1039e7SEric Dumazet 			return u;
203aa1039e7SEric Dumazet 		}
204aa1039e7SEric Dumazet 		if ((__force __u32)daddr < (__force __u32)u->v4daddr)
205aa1039e7SEric Dumazet 			u = rcu_dereference_bh(u->avl_left);
206aa1039e7SEric Dumazet 		else
207aa1039e7SEric Dumazet 			u = rcu_dereference_bh(u->avl_right);
208aa1039e7SEric Dumazet 		if (unlikely(++count == PEER_MAXDEPTH))
209aa1039e7SEric Dumazet 			break;
210aa1039e7SEric Dumazet 	}
211aa1039e7SEric Dumazet 	return NULL;
212aa1039e7SEric Dumazet }
213aa1039e7SEric Dumazet 
214aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */
2151da177e4SLinus Torvalds #define lookup_rightempty(start)				\
2161da177e4SLinus Torvalds ({								\
217*b914c4eaSEric Dumazet 	struct inet_peer *u;					\
218*b914c4eaSEric Dumazet 	struct inet_peer __rcu **v;				\
2191da177e4SLinus Torvalds 	*stackptr++ = &start->avl_left;				\
2201da177e4SLinus Torvalds 	v = &start->avl_left;					\
221*b914c4eaSEric Dumazet 	for (u = rcu_dereference_protected(*v,			\
222*b914c4eaSEric Dumazet 			lockdep_is_held(&peers.lock));		\
223*b914c4eaSEric Dumazet 	     u->avl_right != peer_avl_empty_rcu; ) {		\
2241da177e4SLinus Torvalds 		v = &u->avl_right;				\
2251da177e4SLinus Torvalds 		*stackptr++ = v;				\
226*b914c4eaSEric Dumazet 		u = rcu_dereference_protected(*v,		\
227*b914c4eaSEric Dumazet 			lockdep_is_held(&peers.lock));		\
2281da177e4SLinus Torvalds 	}							\
2291da177e4SLinus Torvalds 	u;							\
2301da177e4SLinus Torvalds })
2311da177e4SLinus Torvalds 
232aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held.
2331da177e4SLinus Torvalds  * Variable names are the proof of operation correctness.
234aa1039e7SEric Dumazet  * Look into mm/map_avl.c for more detail description of the ideas.
235aa1039e7SEric Dumazet  */
236*b914c4eaSEric Dumazet static void peer_avl_rebalance(struct inet_peer __rcu **stack[],
237*b914c4eaSEric Dumazet 		struct inet_peer __rcu ***stackend)
2381da177e4SLinus Torvalds {
239*b914c4eaSEric Dumazet 	struct inet_peer __rcu **nodep;
240*b914c4eaSEric Dumazet 	struct inet_peer *node, *l, *r;
2411da177e4SLinus Torvalds 	int lh, rh;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	while (stackend > stack) {
2441da177e4SLinus Torvalds 		nodep = *--stackend;
245*b914c4eaSEric Dumazet 		node = rcu_dereference_protected(*nodep,
246*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
247*b914c4eaSEric Dumazet 		l = rcu_dereference_protected(node->avl_left,
248*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
249*b914c4eaSEric Dumazet 		r = rcu_dereference_protected(node->avl_right,
250*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
2511da177e4SLinus Torvalds 		lh = node_height(l);
2521da177e4SLinus Torvalds 		rh = node_height(r);
2531da177e4SLinus Torvalds 		if (lh > rh + 1) { /* l: RH+2 */
2541da177e4SLinus Torvalds 			struct inet_peer *ll, *lr, *lrl, *lrr;
2551da177e4SLinus Torvalds 			int lrh;
256*b914c4eaSEric Dumazet 			ll = rcu_dereference_protected(l->avl_left,
257*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
258*b914c4eaSEric Dumazet 			lr = rcu_dereference_protected(l->avl_right,
259*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
2601da177e4SLinus Torvalds 			lrh = node_height(lr);
2611da177e4SLinus Torvalds 			if (lrh <= node_height(ll)) {	/* ll: RH+1 */
262*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, lr);	/* lr: RH or RH+1 */
263*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, r);	/* r: RH */
2641da177e4SLinus Torvalds 				node->avl_height = lrh + 1; /* RH+1 or RH+2 */
265*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_left, ll);       /* ll: RH+1 */
266*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_right, node);	/* node: RH+1 or RH+2 */
2671da177e4SLinus Torvalds 				l->avl_height = node->avl_height + 1;
268*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, l);
2691da177e4SLinus Torvalds 			} else { /* ll: RH, lr: RH+1 */
270*b914c4eaSEric Dumazet 				lrl = rcu_dereference_protected(lr->avl_left,
271*b914c4eaSEric Dumazet 					lockdep_is_held(&peers.lock));	/* lrl: RH or RH-1 */
272*b914c4eaSEric Dumazet 				lrr = rcu_dereference_protected(lr->avl_right,
273*b914c4eaSEric Dumazet 					lockdep_is_held(&peers.lock));	/* lrr: RH or RH-1 */
274*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, lrr);	/* lrr: RH or RH-1 */
275*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, r);	/* r: RH */
2761da177e4SLinus Torvalds 				node->avl_height = rh + 1; /* node: RH+1 */
277*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_left, ll);	/* ll: RH */
278*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_right, lrl);	/* lrl: RH or RH-1 */
2791da177e4SLinus Torvalds 				l->avl_height = rh + 1;	/* l: RH+1 */
280*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(lr->avl_left, l);	/* l: RH+1 */
281*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(lr->avl_right, node);	/* node: RH+1 */
2821da177e4SLinus Torvalds 				lr->avl_height = rh + 2;
283*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, lr);
2841da177e4SLinus Torvalds 			}
2851da177e4SLinus Torvalds 		} else if (rh > lh + 1) { /* r: LH+2 */
2861da177e4SLinus Torvalds 			struct inet_peer *rr, *rl, *rlr, *rll;
2871da177e4SLinus Torvalds 			int rlh;
288*b914c4eaSEric Dumazet 			rr = rcu_dereference_protected(r->avl_right,
289*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
290*b914c4eaSEric Dumazet 			rl = rcu_dereference_protected(r->avl_left,
291*b914c4eaSEric Dumazet 				lockdep_is_held(&peers.lock));
2921da177e4SLinus Torvalds 			rlh = node_height(rl);
2931da177e4SLinus Torvalds 			if (rlh <= node_height(rr)) {	/* rr: LH+1 */
294*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, rl);	/* rl: LH or LH+1 */
295*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, l);	/* l: LH */
2961da177e4SLinus Torvalds 				node->avl_height = rlh + 1; /* LH+1 or LH+2 */
297*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_right, rr);	/* rr: LH+1 */
298*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_left, node);	/* node: LH+1 or LH+2 */
2991da177e4SLinus Torvalds 				r->avl_height = node->avl_height + 1;
300*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, r);
3011da177e4SLinus Torvalds 			} else { /* rr: RH, rl: RH+1 */
302*b914c4eaSEric Dumazet 				rlr = rcu_dereference_protected(rl->avl_right,
303*b914c4eaSEric Dumazet 					lockdep_is_held(&peers.lock));	/* rlr: LH or LH-1 */
304*b914c4eaSEric Dumazet 				rll = rcu_dereference_protected(rl->avl_left,
305*b914c4eaSEric Dumazet 					lockdep_is_held(&peers.lock));	/* rll: LH or LH-1 */
306*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, rll);	/* rll: LH or LH-1 */
307*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, l);	/* l: LH */
3081da177e4SLinus Torvalds 				node->avl_height = lh + 1; /* node: LH+1 */
309*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_right, rr);	/* rr: LH */
310*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_left, rlr);	/* rlr: LH or LH-1 */
3111da177e4SLinus Torvalds 				r->avl_height = lh + 1;	/* r: LH+1 */
312*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(rl->avl_right, r);	/* r: LH+1 */
313*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(rl->avl_left, node);	/* node: LH+1 */
3141da177e4SLinus Torvalds 				rl->avl_height = lh + 2;
315*b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, rl);
3161da177e4SLinus Torvalds 			}
3171da177e4SLinus Torvalds 		} else {
3181da177e4SLinus Torvalds 			node->avl_height = (lh > rh ? lh : rh) + 1;
3191da177e4SLinus Torvalds 		}
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */
3241da177e4SLinus Torvalds #define link_to_pool(n)						\
3251da177e4SLinus Torvalds do {								\
3261da177e4SLinus Torvalds 	n->avl_height = 1;					\
327*b914c4eaSEric Dumazet 	n->avl_left = peer_avl_empty_rcu;			\
328*b914c4eaSEric Dumazet 	n->avl_right = peer_avl_empty_rcu;			\
329*b914c4eaSEric Dumazet 	/* lockless readers can catch us now */			\
330*b914c4eaSEric Dumazet 	rcu_assign_pointer(**--stackptr, n);			\
3311da177e4SLinus Torvalds 	peer_avl_rebalance(stack, stackptr);			\
3321da177e4SLinus Torvalds } while (0)
3331da177e4SLinus Torvalds 
334aa1039e7SEric Dumazet static void inetpeer_free_rcu(struct rcu_head *head)
335aa1039e7SEric Dumazet {
336aa1039e7SEric Dumazet 	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
337aa1039e7SEric Dumazet }
338aa1039e7SEric Dumazet 
3391da177e4SLinus Torvalds /* May be called with local BH enabled. */
3401da177e4SLinus Torvalds static void unlink_from_pool(struct inet_peer *p)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	int do_free;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	do_free = 0;
3451da177e4SLinus Torvalds 
346aa1039e7SEric Dumazet 	spin_lock_bh(&peers.lock);
3471da177e4SLinus Torvalds 	/* Check the reference counter.  It was artificially incremented by 1
348aa1039e7SEric Dumazet 	 * in cleanup() function to prevent sudden disappearing.  If we can
349aa1039e7SEric Dumazet 	 * atomically (because of lockless readers) take this last reference,
350aa1039e7SEric Dumazet 	 * it's safe to remove the node and free it later.
3515f2f8920SEric Dumazet 	 * We use refcnt=-1 to alert lockless readers this entry is deleted.
352aa1039e7SEric Dumazet 	 */
3535f2f8920SEric Dumazet 	if (atomic_cmpxchg(&p->refcnt, 1, -1) == 1) {
354*b914c4eaSEric Dumazet 		struct inet_peer __rcu **stack[PEER_MAXDEPTH];
355*b914c4eaSEric Dumazet 		struct inet_peer __rcu ***stackptr, ***delp;
356243bbcaaSEric Dumazet 		if (lookup(p->v4daddr, stack) != p)
3571da177e4SLinus Torvalds 			BUG();
3581da177e4SLinus Torvalds 		delp = stackptr - 1; /* *delp[0] == p */
359*b914c4eaSEric Dumazet 		if (p->avl_left == peer_avl_empty_rcu) {
3601da177e4SLinus Torvalds 			*delp[0] = p->avl_right;
3611da177e4SLinus Torvalds 			--stackptr;
3621da177e4SLinus Torvalds 		} else {
3631da177e4SLinus Torvalds 			/* look for a node to insert instead of p */
3641da177e4SLinus Torvalds 			struct inet_peer *t;
3651da177e4SLinus Torvalds 			t = lookup_rightempty(p);
366*b914c4eaSEric Dumazet 			BUG_ON(rcu_dereference_protected(*stackptr[-1],
367*b914c4eaSEric Dumazet 					lockdep_is_held(&peers.lock)) != t);
3681da177e4SLinus Torvalds 			**--stackptr = t->avl_left;
3691da177e4SLinus Torvalds 			/* t is removed, t->v4daddr > x->v4daddr for any
3701da177e4SLinus Torvalds 			 * x in p->avl_left subtree.
3711da177e4SLinus Torvalds 			 * Put t in the old place of p. */
372*b914c4eaSEric Dumazet 			RCU_INIT_POINTER(*delp[0], t);
3731da177e4SLinus Torvalds 			t->avl_left = p->avl_left;
3741da177e4SLinus Torvalds 			t->avl_right = p->avl_right;
3751da177e4SLinus Torvalds 			t->avl_height = p->avl_height;
37609a62660SKris Katterjohn 			BUG_ON(delp[1] != &p->avl_left);
3771da177e4SLinus Torvalds 			delp[1] = &t->avl_left; /* was &p->avl_left */
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 		peer_avl_rebalance(stack, stackptr);
380d6cc1d64SEric Dumazet 		peers.total--;
3811da177e4SLinus Torvalds 		do_free = 1;
3821da177e4SLinus Torvalds 	}
383aa1039e7SEric Dumazet 	spin_unlock_bh(&peers.lock);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	if (do_free)
386aa1039e7SEric Dumazet 		call_rcu_bh(&p->rcu, inetpeer_free_rcu);
3871da177e4SLinus Torvalds 	else
3881da177e4SLinus Torvalds 		/* The node is used again.  Decrease the reference counter
3891da177e4SLinus Torvalds 		 * back.  The loop "cleanup -> unlink_from_unused
3901da177e4SLinus Torvalds 		 *   -> unlink_from_pool -> putpeer -> link_to_unused
3911da177e4SLinus Torvalds 		 *   -> cleanup (for the same node)"
3921da177e4SLinus Torvalds 		 * doesn't really exist because the entry will have a
393aa1039e7SEric Dumazet 		 * recent deletion time and will not be cleaned again soon.
394aa1039e7SEric Dumazet 		 */
3951da177e4SLinus Torvalds 		inet_putpeer(p);
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /* May be called with local BH enabled. */
3991da177e4SLinus Torvalds static int cleanup_once(unsigned long ttl)
4001da177e4SLinus Torvalds {
401d71209deSPavel Emelyanov 	struct inet_peer *p = NULL;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	/* Remove the first entry from the list of unused nodes. */
404d6cc1d64SEric Dumazet 	spin_lock_bh(&unused_peers.lock);
405d6cc1d64SEric Dumazet 	if (!list_empty(&unused_peers.list)) {
406d71209deSPavel Emelyanov 		__u32 delta;
407d71209deSPavel Emelyanov 
408d6cc1d64SEric Dumazet 		p = list_first_entry(&unused_peers.list, struct inet_peer, unused);
409d71209deSPavel Emelyanov 		delta = (__u32)jiffies - p->dtime;
410d71209deSPavel Emelyanov 
4114663afe2SEric Dumazet 		if (delta < ttl) {
4121da177e4SLinus Torvalds 			/* Do not prune fresh entries. */
413d6cc1d64SEric Dumazet 			spin_unlock_bh(&unused_peers.lock);
4141da177e4SLinus Torvalds 			return -1;
4151da177e4SLinus Torvalds 		}
416d71209deSPavel Emelyanov 
417d71209deSPavel Emelyanov 		list_del_init(&p->unused);
418d71209deSPavel Emelyanov 
4191da177e4SLinus Torvalds 		/* Grab an extra reference to prevent node disappearing
4201da177e4SLinus Torvalds 		 * before unlink_from_pool() call. */
4211da177e4SLinus Torvalds 		atomic_inc(&p->refcnt);
4221da177e4SLinus Torvalds 	}
423d6cc1d64SEric Dumazet 	spin_unlock_bh(&unused_peers.lock);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	if (p == NULL)
4261da177e4SLinus Torvalds 		/* It means that the total number of USED entries has
4271da177e4SLinus Torvalds 		 * grown over inet_peer_threshold.  It shouldn't really
4281da177e4SLinus Torvalds 		 * happen because of entry limits in route cache. */
4291da177e4SLinus Torvalds 		return -1;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	unlink_from_pool(p);
4321da177e4SLinus Torvalds 	return 0;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /* Called with or without local BH being disabled. */
43653576d9bSAl Viro struct inet_peer *inet_getpeer(__be32 daddr, int create)
4371da177e4SLinus Torvalds {
438aa1039e7SEric Dumazet 	struct inet_peer *p;
439*b914c4eaSEric Dumazet 	struct inet_peer __rcu **stack[PEER_MAXDEPTH], ***stackptr;
4401da177e4SLinus Torvalds 
441aa1039e7SEric Dumazet 	/* Look up for the address quickly, lockless.
442aa1039e7SEric Dumazet 	 * Because of a concurrent writer, we might not find an existing entry.
443aa1039e7SEric Dumazet 	 */
444aa1039e7SEric Dumazet 	rcu_read_lock_bh();
445aa1039e7SEric Dumazet 	p = lookup_rcu_bh(daddr);
446aa1039e7SEric Dumazet 	rcu_read_unlock_bh();
4471da177e4SLinus Torvalds 
448aa1039e7SEric Dumazet 	if (p) {
449aa1039e7SEric Dumazet 		/* The existing node has been found.
450aa1039e7SEric Dumazet 		 * Remove the entry from unused list if it was there.
451aa1039e7SEric Dumazet 		 */
4521da177e4SLinus Torvalds 		unlink_from_unused(p);
4531da177e4SLinus Torvalds 		return p;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 
456aa1039e7SEric Dumazet 	/* retry an exact lookup, taking the lock before.
457aa1039e7SEric Dumazet 	 * At least, nodes should be hot in our cache.
458aa1039e7SEric Dumazet 	 */
459aa1039e7SEric Dumazet 	spin_lock_bh(&peers.lock);
460243bbcaaSEric Dumazet 	p = lookup(daddr, stack);
461aa1039e7SEric Dumazet 	if (p != peer_avl_empty) {
462aa1039e7SEric Dumazet 		atomic_inc(&p->refcnt);
463aa1039e7SEric Dumazet 		spin_unlock_bh(&peers.lock);
464aa1039e7SEric Dumazet 		/* Remove the entry from unused list if it was there. */
465aa1039e7SEric Dumazet 		unlink_from_unused(p);
466aa1039e7SEric Dumazet 		return p;
467aa1039e7SEric Dumazet 	}
468aa1039e7SEric Dumazet 	p = create ? kmem_cache_alloc(peer_cachep, GFP_ATOMIC) : NULL;
469aa1039e7SEric Dumazet 	if (p) {
470aa1039e7SEric Dumazet 		p->v4daddr = daddr;
471aa1039e7SEric Dumazet 		atomic_set(&p->refcnt, 1);
472aa1039e7SEric Dumazet 		atomic_set(&p->rid, 0);
473aa1039e7SEric Dumazet 		atomic_set(&p->ip_id_count, secure_ip_id(daddr));
474aa1039e7SEric Dumazet 		p->tcp_ts_stamp = 0;
475aa1039e7SEric Dumazet 		INIT_LIST_HEAD(&p->unused);
476aa1039e7SEric Dumazet 
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 		/* Link the node. */
479aa1039e7SEric Dumazet 		link_to_pool(p);
480d6cc1d64SEric Dumazet 		peers.total++;
481aa1039e7SEric Dumazet 	}
482aa1039e7SEric Dumazet 	spin_unlock_bh(&peers.lock);
4831da177e4SLinus Torvalds 
484d6cc1d64SEric Dumazet 	if (peers.total >= inet_peer_threshold)
4851da177e4SLinus Torvalds 		/* Remove one less-recently-used entry. */
4861da177e4SLinus Torvalds 		cleanup_once(0);
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	return p;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* Called with local BH disabled. */
4921da177e4SLinus Torvalds static void peer_check_expire(unsigned long dummy)
4931da177e4SLinus Torvalds {
4944663afe2SEric Dumazet 	unsigned long now = jiffies;
4951da177e4SLinus Torvalds 	int ttl;
4961da177e4SLinus Torvalds 
497d6cc1d64SEric Dumazet 	if (peers.total >= inet_peer_threshold)
4981da177e4SLinus Torvalds 		ttl = inet_peer_minttl;
4991da177e4SLinus Torvalds 	else
5001da177e4SLinus Torvalds 		ttl = inet_peer_maxttl
5011da177e4SLinus Torvalds 				- (inet_peer_maxttl - inet_peer_minttl) / HZ *
502d6cc1d64SEric Dumazet 					peers.total / inet_peer_threshold * HZ;
5034663afe2SEric Dumazet 	while (!cleanup_once(ttl)) {
5044663afe2SEric Dumazet 		if (jiffies != now)
5054663afe2SEric Dumazet 			break;
5064663afe2SEric Dumazet 	}
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	/* Trigger the timer after inet_peer_gc_mintime .. inet_peer_gc_maxtime
5091da177e4SLinus Torvalds 	 * interval depending on the total number of entries (more entries,
5101da177e4SLinus Torvalds 	 * less interval). */
511d6cc1d64SEric Dumazet 	if (peers.total >= inet_peer_threshold)
5121344a416SDave Johnson 		peer_periodic_timer.expires = jiffies + inet_peer_gc_mintime;
5131344a416SDave Johnson 	else
5141da177e4SLinus Torvalds 		peer_periodic_timer.expires = jiffies
5151da177e4SLinus Torvalds 			+ inet_peer_gc_maxtime
5161da177e4SLinus Torvalds 			- (inet_peer_gc_maxtime - inet_peer_gc_mintime) / HZ *
517d6cc1d64SEric Dumazet 				peers.total / inet_peer_threshold * HZ;
5181da177e4SLinus Torvalds 	add_timer(&peer_periodic_timer);
5191da177e4SLinus Torvalds }
5204663afe2SEric Dumazet 
5214663afe2SEric Dumazet void inet_putpeer(struct inet_peer *p)
5224663afe2SEric Dumazet {
523d6cc1d64SEric Dumazet 	local_bh_disable();
524d6cc1d64SEric Dumazet 
525d6cc1d64SEric Dumazet 	if (atomic_dec_and_lock(&p->refcnt, &unused_peers.lock)) {
526d6cc1d64SEric Dumazet 		list_add_tail(&p->unused, &unused_peers.list);
5274663afe2SEric Dumazet 		p->dtime = (__u32)jiffies;
528d6cc1d64SEric Dumazet 		spin_unlock(&unused_peers.lock);
5294663afe2SEric Dumazet 	}
530d6cc1d64SEric Dumazet 
531d6cc1d64SEric Dumazet 	local_bh_enable();
5324663afe2SEric Dumazet }
533