xref: /openbmc/linux/net/ipv4/inetpeer.c (revision c0efc887dcadbdbfe171f028acfab9c7c00e9dde)
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>
205faa5df1SSteffen Klassert #include <linux/workqueue.h>
2120380731SArnaldo Carvalho de Melo #include <net/ip.h>
221da177e4SLinus Torvalds #include <net/inetpeer.h>
236e5714eaSDavid S. Miller #include <net/secure_seq.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  *  Theory of operations.
271da177e4SLinus Torvalds  *  We keep one entry for each peer IP address.  The nodes contains long-living
281da177e4SLinus Torvalds  *  information about the peer which doesn't depend on routes.
291da177e4SLinus Torvalds  *  At this moment this information consists only of ID field for the next
301da177e4SLinus Torvalds  *  outgoing IP packet.  This field is incremented with each packet as encoded
311da177e4SLinus Torvalds  *  in inet_getid() function (include/net/inetpeer.h).
321da177e4SLinus Torvalds  *  At the moment of writing this notes identifier of IP packets is generated
331da177e4SLinus Torvalds  *  to be unpredictable using this code only for packets subjected
341da177e4SLinus Torvalds  *  (actually or potentially) to defragmentation.  I.e. DF packets less than
351da177e4SLinus Torvalds  *  PMTU in size uses a constant ID and do not use this code (see
361da177e4SLinus Torvalds  *  ip_select_ident() in include/net/ip.h).
371da177e4SLinus Torvalds  *
381da177e4SLinus Torvalds  *  Route cache entries hold references to our nodes.
391da177e4SLinus Torvalds  *  New cache entries get references via lookup by destination IP address in
401da177e4SLinus Torvalds  *  the avl tree.  The reference is grabbed only when it's needed i.e. only
411da177e4SLinus Torvalds  *  when we try to output IP packet which needs an unpredictable ID (see
421da177e4SLinus Torvalds  *  __ip_select_ident() in net/ipv4/route.c).
431da177e4SLinus Torvalds  *  Nodes are removed only when reference counter goes to 0.
441da177e4SLinus Torvalds  *  When it's happened the node may be removed when a sufficient amount of
451da177e4SLinus Torvalds  *  time has been passed since its last use.  The less-recently-used entry can
461da177e4SLinus Torvalds  *  also be removed if the pool is overloaded i.e. if the total amount of
471da177e4SLinus Torvalds  *  entries is greater-or-equal than the threshold.
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  *  Node pool is organised as an AVL tree.
501da177e4SLinus Torvalds  *  Such an implementation has been chosen not just for fun.  It's a way to
511da177e4SLinus Torvalds  *  prevent easy and efficient DoS attacks by creating hash collisions.  A huge
521da177e4SLinus Torvalds  *  amount of long living nodes in a single hash slot would significantly delay
531da177e4SLinus Torvalds  *  lookups performed with disabled BHs.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  *  Serialisation issues.
56aa1039e7SEric Dumazet  *  1.  Nodes may appear in the tree only with the pool lock held.
57aa1039e7SEric Dumazet  *  2.  Nodes may disappear from the tree only with the pool lock held
581da177e4SLinus Torvalds  *      AND reference count being 0.
594b9d9be8SEric Dumazet  *  3.  Global variable peer_total is modified under the pool lock.
604b9d9be8SEric Dumazet  *  4.  struct inet_peer fields modification:
611da177e4SLinus Torvalds  *		avl_left, avl_right, avl_parent, avl_height: pool lock
621da177e4SLinus Torvalds  *		refcnt: atomically against modifications on other CPU;
631da177e4SLinus Torvalds  *		   usually under some other lock to prevent node disappearing
64582a72daSDavid S. Miller  *		daddr: unchangeable
65317fe0e6SEric Dumazet  *		ip_id_count: atomic value (no lock needed)
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds 
68e18b890bSChristoph Lameter static struct kmem_cache *peer_cachep __read_mostly;
691da177e4SLinus Torvalds 
705faa5df1SSteffen Klassert static LIST_HEAD(gc_list);
715faa5df1SSteffen Klassert static const int gc_delay = 60 * HZ;
725faa5df1SSteffen Klassert static struct delayed_work gc_work;
735faa5df1SSteffen Klassert static DEFINE_SPINLOCK(gc_lock);
745faa5df1SSteffen Klassert 
751da177e4SLinus Torvalds #define node_height(x) x->avl_height
76d6cc1d64SEric Dumazet 
77d6cc1d64SEric Dumazet #define peer_avl_empty ((struct inet_peer *)&peer_fake_node)
78b914c4eaSEric Dumazet #define peer_avl_empty_rcu ((struct inet_peer __rcu __force *)&peer_fake_node)
79d6cc1d64SEric Dumazet static const struct inet_peer peer_fake_node = {
80b914c4eaSEric Dumazet 	.avl_left	= peer_avl_empty_rcu,
81b914c4eaSEric Dumazet 	.avl_right	= peer_avl_empty_rcu,
821da177e4SLinus Torvalds 	.avl_height	= 0
831da177e4SLinus Torvalds };
84d6cc1d64SEric Dumazet 
85c3426b47SDavid S. Miller void inet_peer_base_init(struct inet_peer_base *bp)
86c3426b47SDavid S. Miller {
87c3426b47SDavid S. Miller 	bp->root = peer_avl_empty_rcu;
88c3426b47SDavid S. Miller 	seqlock_init(&bp->lock);
89c3426b47SDavid S. Miller 	bp->total = 0;
90c3426b47SDavid S. Miller }
91c3426b47SDavid S. Miller EXPORT_SYMBOL_GPL(inet_peer_base_init);
92021e9299SDavid S. Miller 
931da177e4SLinus Torvalds #define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
96243bbcaaSEric Dumazet int inet_peer_threshold __read_mostly = 65536 + 128;	/* start to throw entries more
971da177e4SLinus Torvalds 					 * aggressively at this stage */
98243bbcaaSEric Dumazet int inet_peer_minttl __read_mostly = 120 * HZ;	/* TTL under high load: 120 sec */
99243bbcaaSEric Dumazet int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;	/* usual time to live: 10 min */
1001da177e4SLinus Torvalds 
1015faa5df1SSteffen Klassert static void inetpeer_gc_worker(struct work_struct *work)
1025faa5df1SSteffen Klassert {
1035faa5df1SSteffen Klassert 	struct inet_peer *p, *n;
1045faa5df1SSteffen Klassert 	LIST_HEAD(list);
1055faa5df1SSteffen Klassert 
1065faa5df1SSteffen Klassert 	spin_lock_bh(&gc_lock);
1075faa5df1SSteffen Klassert 	list_replace_init(&gc_list, &list);
1085faa5df1SSteffen Klassert 	spin_unlock_bh(&gc_lock);
1095faa5df1SSteffen Klassert 
1105faa5df1SSteffen Klassert 	if (list_empty(&list))
1115faa5df1SSteffen Klassert 		return;
1125faa5df1SSteffen Klassert 
1135faa5df1SSteffen Klassert 	list_for_each_entry_safe(p, n, &list, gc_list) {
1145faa5df1SSteffen Klassert 
1155faa5df1SSteffen Klassert 		if(need_resched())
1165faa5df1SSteffen Klassert 			cond_resched();
1175faa5df1SSteffen Klassert 
1185faa5df1SSteffen Klassert 		if (p->avl_left != peer_avl_empty) {
1195faa5df1SSteffen Klassert 			list_add_tail(&p->avl_left->gc_list, &list);
1205faa5df1SSteffen Klassert 			p->avl_left = peer_avl_empty;
1215faa5df1SSteffen Klassert 		}
1225faa5df1SSteffen Klassert 
1235faa5df1SSteffen Klassert 		if (p->avl_right != peer_avl_empty) {
1245faa5df1SSteffen Klassert 			list_add_tail(&p->avl_right->gc_list, &list);
1255faa5df1SSteffen Klassert 			p->avl_right = peer_avl_empty;
1265faa5df1SSteffen Klassert 		}
1275faa5df1SSteffen Klassert 
1285faa5df1SSteffen Klassert 		n = list_entry(p->gc_list.next, struct inet_peer, gc_list);
1295faa5df1SSteffen Klassert 
1305faa5df1SSteffen Klassert 		if (!atomic_read(&p->refcnt)) {
1315faa5df1SSteffen Klassert 			list_del(&p->gc_list);
1325faa5df1SSteffen Klassert 			kmem_cache_free(peer_cachep, p);
1335faa5df1SSteffen Klassert 		}
1345faa5df1SSteffen Klassert 	}
1355faa5df1SSteffen Klassert 
1365faa5df1SSteffen Klassert 	if (list_empty(&list))
1375faa5df1SSteffen Klassert 		return;
1385faa5df1SSteffen Klassert 
1395faa5df1SSteffen Klassert 	spin_lock_bh(&gc_lock);
1405faa5df1SSteffen Klassert 	list_splice(&list, &gc_list);
1415faa5df1SSteffen Klassert 	spin_unlock_bh(&gc_lock);
1425faa5df1SSteffen Klassert 
1435faa5df1SSteffen Klassert 	schedule_delayed_work(&gc_work, gc_delay);
1445faa5df1SSteffen Klassert }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /* Called from ip_output.c:ip_init  */
1471da177e4SLinus Torvalds void __init inet_initpeers(void)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct sysinfo si;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	/* Use the straight interface to information about memory. */
1521da177e4SLinus Torvalds 	si_meminfo(&si);
1531da177e4SLinus Torvalds 	/* The values below were suggested by Alexey Kuznetsov
1541da177e4SLinus Torvalds 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
1551da177e4SLinus Torvalds 	 * myself.  --SAW
1561da177e4SLinus Torvalds 	 */
1571da177e4SLinus Torvalds 	if (si.totalram <= (32768*1024)/PAGE_SIZE)
1581da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
1591da177e4SLinus Torvalds 	if (si.totalram <= (16384*1024)/PAGE_SIZE)
1601da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* about 512KB */
1611da177e4SLinus Torvalds 	if (si.totalram <= (8192*1024)/PAGE_SIZE)
1621da177e4SLinus Torvalds 		inet_peer_threshold >>= 2; /* about 128KB */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	peer_cachep = kmem_cache_create("inet_peer_cache",
1651da177e4SLinus Torvalds 			sizeof(struct inet_peer),
166317fe0e6SEric Dumazet 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
16720c2df83SPaul Mundt 			NULL);
1681da177e4SLinus Torvalds 
1695faa5df1SSteffen Klassert 	INIT_DELAYED_WORK_DEFERRABLE(&gc_work, inetpeer_gc_worker);
170d6cc1d64SEric Dumazet }
1711da177e4SLinus Torvalds 
1728790ca17SDavid S. Miller static int addr_compare(const struct inetpeer_addr *a,
1738790ca17SDavid S. Miller 			const struct inetpeer_addr *b)
17402663045SDavid S. Miller {
17502663045SDavid S. Miller 	int i, n = (a->family == AF_INET ? 1 : 4);
17602663045SDavid S. Miller 
17702663045SDavid S. Miller 	for (i = 0; i < n; i++) {
1787a71ed89SDavid S. Miller 		if (a->addr.a6[i] == b->addr.a6[i])
17902663045SDavid S. Miller 			continue;
180747465efSEric Dumazet 		if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i])
18102663045SDavid S. Miller 			return -1;
18202663045SDavid S. Miller 		return 1;
18302663045SDavid S. Miller 	}
18402663045SDavid S. Miller 
18502663045SDavid S. Miller 	return 0;
18602663045SDavid S. Miller }
18702663045SDavid S. Miller 
18865e8354eSEric Dumazet #define rcu_deref_locked(X, BASE)				\
18965e8354eSEric Dumazet 	rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock))
19065e8354eSEric Dumazet 
191243bbcaaSEric Dumazet /*
192243bbcaaSEric Dumazet  * Called with local BH disabled and the pool lock held.
193243bbcaaSEric Dumazet  */
19498158f5aSDavid S. Miller #define lookup(_daddr, _stack, _base)				\
1951da177e4SLinus Torvalds ({								\
196b914c4eaSEric Dumazet 	struct inet_peer *u;					\
197b914c4eaSEric Dumazet 	struct inet_peer __rcu **v;				\
198aa1039e7SEric Dumazet 								\
199243bbcaaSEric Dumazet 	stackptr = _stack;					\
20098158f5aSDavid S. Miller 	*stackptr++ = &_base->root;				\
20165e8354eSEric Dumazet 	for (u = rcu_deref_locked(_base->root, _base);		\
202b914c4eaSEric Dumazet 	     u != peer_avl_empty; ) {				\
20302663045SDavid S. Miller 		int cmp = addr_compare(_daddr, &u->daddr);	\
20402663045SDavid S. Miller 		if (cmp == 0)					\
2051da177e4SLinus Torvalds 			break;					\
20602663045SDavid S. Miller 		if (cmp == -1)					\
2071da177e4SLinus Torvalds 			v = &u->avl_left;			\
2081da177e4SLinus Torvalds 		else						\
2091da177e4SLinus Torvalds 			v = &u->avl_right;			\
2101da177e4SLinus Torvalds 		*stackptr++ = v;				\
21165e8354eSEric Dumazet 		u = rcu_deref_locked(*v, _base);		\
2121da177e4SLinus Torvalds 	}							\
2131da177e4SLinus Torvalds 	u;							\
2141da177e4SLinus Torvalds })
2151da177e4SLinus Torvalds 
216aa1039e7SEric Dumazet /*
2177b46ac4eSDavid S. Miller  * Called with rcu_read_lock()
218aa1039e7SEric Dumazet  * Because we hold no lock against a writer, its quite possible we fall
219aa1039e7SEric Dumazet  * in an endless loop.
220aa1039e7SEric Dumazet  * But every pointer we follow is guaranteed to be valid thanks to RCU.
221aa1039e7SEric Dumazet  * We exit from this function if number of links exceeds PEER_MAXDEPTH
222aa1039e7SEric Dumazet  */
2237b46ac4eSDavid S. Miller static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr,
2244b9d9be8SEric Dumazet 				    struct inet_peer_base *base)
225aa1039e7SEric Dumazet {
2267b46ac4eSDavid S. Miller 	struct inet_peer *u = rcu_dereference(base->root);
227aa1039e7SEric Dumazet 	int count = 0;
228aa1039e7SEric Dumazet 
229aa1039e7SEric Dumazet 	while (u != peer_avl_empty) {
23002663045SDavid S. Miller 		int cmp = addr_compare(daddr, &u->daddr);
23102663045SDavid S. Miller 		if (cmp == 0) {
2325f2f8920SEric Dumazet 			/* Before taking a reference, check if this entry was
2334b9d9be8SEric Dumazet 			 * deleted (refcnt=-1)
2345f2f8920SEric Dumazet 			 */
2354b9d9be8SEric Dumazet 			if (!atomic_add_unless(&u->refcnt, 1, -1))
236aa1039e7SEric Dumazet 				u = NULL;
237aa1039e7SEric Dumazet 			return u;
238aa1039e7SEric Dumazet 		}
23902663045SDavid S. Miller 		if (cmp == -1)
2407b46ac4eSDavid S. Miller 			u = rcu_dereference(u->avl_left);
241aa1039e7SEric Dumazet 		else
2427b46ac4eSDavid S. Miller 			u = rcu_dereference(u->avl_right);
243aa1039e7SEric Dumazet 		if (unlikely(++count == PEER_MAXDEPTH))
244aa1039e7SEric Dumazet 			break;
245aa1039e7SEric Dumazet 	}
246aa1039e7SEric Dumazet 	return NULL;
247aa1039e7SEric Dumazet }
248aa1039e7SEric Dumazet 
249aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */
25098158f5aSDavid S. Miller #define lookup_rightempty(start, base)				\
2511da177e4SLinus Torvalds ({								\
252b914c4eaSEric Dumazet 	struct inet_peer *u;					\
253b914c4eaSEric Dumazet 	struct inet_peer __rcu **v;				\
2541da177e4SLinus Torvalds 	*stackptr++ = &start->avl_left;				\
2551da177e4SLinus Torvalds 	v = &start->avl_left;					\
25665e8354eSEric Dumazet 	for (u = rcu_deref_locked(*v, base);			\
257b914c4eaSEric Dumazet 	     u->avl_right != peer_avl_empty_rcu; ) {		\
2581da177e4SLinus Torvalds 		v = &u->avl_right;				\
2591da177e4SLinus Torvalds 		*stackptr++ = v;				\
26065e8354eSEric Dumazet 		u = rcu_deref_locked(*v, base);			\
2611da177e4SLinus Torvalds 	}							\
2621da177e4SLinus Torvalds 	u;							\
2631da177e4SLinus Torvalds })
2641da177e4SLinus Torvalds 
265aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held.
2661da177e4SLinus Torvalds  * Variable names are the proof of operation correctness.
267aa1039e7SEric Dumazet  * Look into mm/map_avl.c for more detail description of the ideas.
268aa1039e7SEric Dumazet  */
269b914c4eaSEric Dumazet static void peer_avl_rebalance(struct inet_peer __rcu **stack[],
27098158f5aSDavid S. Miller 			       struct inet_peer __rcu ***stackend,
27198158f5aSDavid S. Miller 			       struct inet_peer_base *base)
2721da177e4SLinus Torvalds {
273b914c4eaSEric Dumazet 	struct inet_peer __rcu **nodep;
274b914c4eaSEric Dumazet 	struct inet_peer *node, *l, *r;
2751da177e4SLinus Torvalds 	int lh, rh;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	while (stackend > stack) {
2781da177e4SLinus Torvalds 		nodep = *--stackend;
27965e8354eSEric Dumazet 		node = rcu_deref_locked(*nodep, base);
28065e8354eSEric Dumazet 		l = rcu_deref_locked(node->avl_left, base);
28165e8354eSEric Dumazet 		r = rcu_deref_locked(node->avl_right, base);
2821da177e4SLinus Torvalds 		lh = node_height(l);
2831da177e4SLinus Torvalds 		rh = node_height(r);
2841da177e4SLinus Torvalds 		if (lh > rh + 1) { /* l: RH+2 */
2851da177e4SLinus Torvalds 			struct inet_peer *ll, *lr, *lrl, *lrr;
2861da177e4SLinus Torvalds 			int lrh;
28765e8354eSEric Dumazet 			ll = rcu_deref_locked(l->avl_left, base);
28865e8354eSEric Dumazet 			lr = rcu_deref_locked(l->avl_right, base);
2891da177e4SLinus Torvalds 			lrh = node_height(lr);
2901da177e4SLinus Torvalds 			if (lrh <= node_height(ll)) {	/* ll: RH+1 */
291b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, lr);	/* lr: RH or RH+1 */
292b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, r);	/* r: RH */
2931da177e4SLinus Torvalds 				node->avl_height = lrh + 1; /* RH+1 or RH+2 */
294b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_left, ll);       /* ll: RH+1 */
295b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_right, node);	/* node: RH+1 or RH+2 */
2961da177e4SLinus Torvalds 				l->avl_height = node->avl_height + 1;
297b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, l);
2981da177e4SLinus Torvalds 			} else { /* ll: RH, lr: RH+1 */
29965e8354eSEric Dumazet 				lrl = rcu_deref_locked(lr->avl_left, base);/* lrl: RH or RH-1 */
30065e8354eSEric Dumazet 				lrr = rcu_deref_locked(lr->avl_right, base);/* lrr: RH or RH-1 */
301b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, lrr);	/* lrr: RH or RH-1 */
302b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, r);	/* r: RH */
3031da177e4SLinus Torvalds 				node->avl_height = rh + 1; /* node: RH+1 */
304b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_left, ll);	/* ll: RH */
305b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_right, lrl);	/* lrl: RH or RH-1 */
3061da177e4SLinus Torvalds 				l->avl_height = rh + 1;	/* l: RH+1 */
307b914c4eaSEric Dumazet 				RCU_INIT_POINTER(lr->avl_left, l);	/* l: RH+1 */
308b914c4eaSEric Dumazet 				RCU_INIT_POINTER(lr->avl_right, node);	/* node: RH+1 */
3091da177e4SLinus Torvalds 				lr->avl_height = rh + 2;
310b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, lr);
3111da177e4SLinus Torvalds 			}
3121da177e4SLinus Torvalds 		} else if (rh > lh + 1) { /* r: LH+2 */
3131da177e4SLinus Torvalds 			struct inet_peer *rr, *rl, *rlr, *rll;
3141da177e4SLinus Torvalds 			int rlh;
31565e8354eSEric Dumazet 			rr = rcu_deref_locked(r->avl_right, base);
31665e8354eSEric Dumazet 			rl = rcu_deref_locked(r->avl_left, base);
3171da177e4SLinus Torvalds 			rlh = node_height(rl);
3181da177e4SLinus Torvalds 			if (rlh <= node_height(rr)) {	/* rr: LH+1 */
319b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, rl);	/* rl: LH or LH+1 */
320b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, l);	/* l: LH */
3211da177e4SLinus Torvalds 				node->avl_height = rlh + 1; /* LH+1 or LH+2 */
322b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_right, rr);	/* rr: LH+1 */
323b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_left, node);	/* node: LH+1 or LH+2 */
3241da177e4SLinus Torvalds 				r->avl_height = node->avl_height + 1;
325b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, r);
3261da177e4SLinus Torvalds 			} else { /* rr: RH, rl: RH+1 */
32765e8354eSEric Dumazet 				rlr = rcu_deref_locked(rl->avl_right, base);/* rlr: LH or LH-1 */
32865e8354eSEric Dumazet 				rll = rcu_deref_locked(rl->avl_left, base);/* rll: LH or LH-1 */
329b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, rll);	/* rll: LH or LH-1 */
330b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, l);	/* l: LH */
3311da177e4SLinus Torvalds 				node->avl_height = lh + 1; /* node: LH+1 */
332b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_right, rr);	/* rr: LH */
333b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_left, rlr);	/* rlr: LH or LH-1 */
3341da177e4SLinus Torvalds 				r->avl_height = lh + 1;	/* r: LH+1 */
335b914c4eaSEric Dumazet 				RCU_INIT_POINTER(rl->avl_right, r);	/* r: LH+1 */
336b914c4eaSEric Dumazet 				RCU_INIT_POINTER(rl->avl_left, node);	/* node: LH+1 */
3371da177e4SLinus Torvalds 				rl->avl_height = lh + 2;
338b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, rl);
3391da177e4SLinus Torvalds 			}
3401da177e4SLinus Torvalds 		} else {
3411da177e4SLinus Torvalds 			node->avl_height = (lh > rh ? lh : rh) + 1;
3421da177e4SLinus Torvalds 		}
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
346aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */
34798158f5aSDavid S. Miller #define link_to_pool(n, base)					\
3481da177e4SLinus Torvalds do {								\
3491da177e4SLinus Torvalds 	n->avl_height = 1;					\
350b914c4eaSEric Dumazet 	n->avl_left = peer_avl_empty_rcu;			\
351b914c4eaSEric Dumazet 	n->avl_right = peer_avl_empty_rcu;			\
352b914c4eaSEric Dumazet 	/* lockless readers can catch us now */			\
353b914c4eaSEric Dumazet 	rcu_assign_pointer(**--stackptr, n);			\
35498158f5aSDavid S. Miller 	peer_avl_rebalance(stack, stackptr, base);		\
3551da177e4SLinus Torvalds } while (0)
3561da177e4SLinus Torvalds 
357aa1039e7SEric Dumazet static void inetpeer_free_rcu(struct rcu_head *head)
358aa1039e7SEric Dumazet {
359aa1039e7SEric Dumazet 	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
360aa1039e7SEric Dumazet }
361aa1039e7SEric Dumazet 
36266944e1cSEric Dumazet static void unlink_from_pool(struct inet_peer *p, struct inet_peer_base *base,
36366944e1cSEric Dumazet 			     struct inet_peer __rcu **stack[PEER_MAXDEPTH])
3641da177e4SLinus Torvalds {
365b914c4eaSEric Dumazet 	struct inet_peer __rcu ***stackptr, ***delp;
3664b9d9be8SEric Dumazet 
36702663045SDavid S. Miller 	if (lookup(&p->daddr, stack, base) != p)
3681da177e4SLinus Torvalds 		BUG();
3691da177e4SLinus Torvalds 	delp = stackptr - 1; /* *delp[0] == p */
370b914c4eaSEric Dumazet 	if (p->avl_left == peer_avl_empty_rcu) {
3711da177e4SLinus Torvalds 		*delp[0] = p->avl_right;
3721da177e4SLinus Torvalds 		--stackptr;
3731da177e4SLinus Torvalds 	} else {
3741da177e4SLinus Torvalds 		/* look for a node to insert instead of p */
3751da177e4SLinus Torvalds 		struct inet_peer *t;
37698158f5aSDavid S. Miller 		t = lookup_rightempty(p, base);
37765e8354eSEric Dumazet 		BUG_ON(rcu_deref_locked(*stackptr[-1], base) != t);
3781da177e4SLinus Torvalds 		**--stackptr = t->avl_left;
379582a72daSDavid S. Miller 		/* t is removed, t->daddr > x->daddr for any
3801da177e4SLinus Torvalds 		 * x in p->avl_left subtree.
3811da177e4SLinus Torvalds 		 * Put t in the old place of p. */
382b914c4eaSEric Dumazet 		RCU_INIT_POINTER(*delp[0], t);
3831da177e4SLinus Torvalds 		t->avl_left = p->avl_left;
3841da177e4SLinus Torvalds 		t->avl_right = p->avl_right;
3851da177e4SLinus Torvalds 		t->avl_height = p->avl_height;
38609a62660SKris Katterjohn 		BUG_ON(delp[1] != &p->avl_left);
3871da177e4SLinus Torvalds 		delp[1] = &t->avl_left; /* was &p->avl_left */
3881da177e4SLinus Torvalds 	}
38998158f5aSDavid S. Miller 	peer_avl_rebalance(stack, stackptr, base);
39098158f5aSDavid S. Miller 	base->total--;
3914e75db2eSEric Dumazet 	call_rcu(&p->rcu, inetpeer_free_rcu);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3944b9d9be8SEric Dumazet /* perform garbage collect on all items stacked during a lookup */
3954b9d9be8SEric Dumazet static int inet_peer_gc(struct inet_peer_base *base,
3964b9d9be8SEric Dumazet 			struct inet_peer __rcu **stack[PEER_MAXDEPTH],
3974b9d9be8SEric Dumazet 			struct inet_peer __rcu ***stackptr)
39898158f5aSDavid S. Miller {
3994b9d9be8SEric Dumazet 	struct inet_peer *p, *gchead = NULL;
4004b9d9be8SEric Dumazet 	__u32 delta, ttl;
4014b9d9be8SEric Dumazet 	int cnt = 0;
40298158f5aSDavid S. Miller 
4034b9d9be8SEric Dumazet 	if (base->total >= inet_peer_threshold)
4044b9d9be8SEric Dumazet 		ttl = 0; /* be aggressive */
4054b9d9be8SEric Dumazet 	else
4064b9d9be8SEric Dumazet 		ttl = inet_peer_maxttl
4074b9d9be8SEric Dumazet 				- (inet_peer_maxttl - inet_peer_minttl) / HZ *
4084b9d9be8SEric Dumazet 					base->total / inet_peer_threshold * HZ;
4094b9d9be8SEric Dumazet 	stackptr--; /* last stack slot is peer_avl_empty */
4104b9d9be8SEric Dumazet 	while (stackptr > stack) {
4114b9d9be8SEric Dumazet 		stackptr--;
4124b9d9be8SEric Dumazet 		p = rcu_deref_locked(**stackptr, base);
4136d1a3e04SEric Dumazet 		if (atomic_read(&p->refcnt) == 0) {
4146d1a3e04SEric Dumazet 			smp_rmb();
415d71209deSPavel Emelyanov 			delta = (__u32)jiffies - p->dtime;
4166d1a3e04SEric Dumazet 			if (delta >= ttl &&
4174b9d9be8SEric Dumazet 			    atomic_cmpxchg(&p->refcnt, 0, -1) == 0) {
4184b9d9be8SEric Dumazet 				p->gc_next = gchead;
4194b9d9be8SEric Dumazet 				gchead = p;
4204b9d9be8SEric Dumazet 			}
4214b9d9be8SEric Dumazet 		}
4226d1a3e04SEric Dumazet 	}
4234b9d9be8SEric Dumazet 	while ((p = gchead) != NULL) {
4244b9d9be8SEric Dumazet 		gchead = p->gc_next;
4254b9d9be8SEric Dumazet 		cnt++;
4264b9d9be8SEric Dumazet 		unlink_from_pool(p, base, stack);
4274b9d9be8SEric Dumazet 	}
4284b9d9be8SEric Dumazet 	return cnt;
4291da177e4SLinus Torvalds }
430d71209deSPavel Emelyanov 
431*c0efc887SDavid S. Miller struct inet_peer *inet_getpeer(struct inet_peer_base *base,
432c8a627edSGao feng 			       const struct inetpeer_addr *daddr,
433c8a627edSGao feng 			       int create)
4341da177e4SLinus Torvalds {
435b914c4eaSEric Dumazet 	struct inet_peer __rcu **stack[PEER_MAXDEPTH], ***stackptr;
43698158f5aSDavid S. Miller 	struct inet_peer *p;
43765e8354eSEric Dumazet 	unsigned int sequence;
4384b9d9be8SEric Dumazet 	int invalidated, gccnt = 0;
4391da177e4SLinus Torvalds 
4404b9d9be8SEric Dumazet 	/* Attempt a lockless lookup first.
441aa1039e7SEric Dumazet 	 * Because of a concurrent writer, we might not find an existing entry.
442aa1039e7SEric Dumazet 	 */
4437b46ac4eSDavid S. Miller 	rcu_read_lock();
44465e8354eSEric Dumazet 	sequence = read_seqbegin(&base->lock);
4454b9d9be8SEric Dumazet 	p = lookup_rcu(daddr, base);
44665e8354eSEric Dumazet 	invalidated = read_seqretry(&base->lock, sequence);
4477b46ac4eSDavid S. Miller 	rcu_read_unlock();
4481da177e4SLinus Torvalds 
4494b9d9be8SEric Dumazet 	if (p)
4501da177e4SLinus Torvalds 		return p;
4511da177e4SLinus Torvalds 
45265e8354eSEric Dumazet 	/* If no writer did a change during our lookup, we can return early. */
45365e8354eSEric Dumazet 	if (!create && !invalidated)
45465e8354eSEric Dumazet 		return NULL;
45565e8354eSEric Dumazet 
456aa1039e7SEric Dumazet 	/* retry an exact lookup, taking the lock before.
457aa1039e7SEric Dumazet 	 * At least, nodes should be hot in our cache.
458aa1039e7SEric Dumazet 	 */
45965e8354eSEric Dumazet 	write_seqlock_bh(&base->lock);
4604b9d9be8SEric Dumazet relookup:
46102663045SDavid S. Miller 	p = lookup(daddr, stack, base);
462aa1039e7SEric Dumazet 	if (p != peer_avl_empty) {
4634b9d9be8SEric Dumazet 		atomic_inc(&p->refcnt);
46465e8354eSEric Dumazet 		write_sequnlock_bh(&base->lock);
4654b9d9be8SEric Dumazet 		return p;
4664b9d9be8SEric Dumazet 	}
4674b9d9be8SEric Dumazet 	if (!gccnt) {
4684b9d9be8SEric Dumazet 		gccnt = inet_peer_gc(base, stack, stackptr);
4694b9d9be8SEric Dumazet 		if (gccnt && create)
4704b9d9be8SEric Dumazet 			goto relookup;
471aa1039e7SEric Dumazet 	}
472aa1039e7SEric Dumazet 	p = create ? kmem_cache_alloc(peer_cachep, GFP_ATOMIC) : NULL;
473aa1039e7SEric Dumazet 	if (p) {
474b534ecf1SDavid S. Miller 		p->daddr = *daddr;
475aa1039e7SEric Dumazet 		atomic_set(&p->refcnt, 1);
476aa1039e7SEric Dumazet 		atomic_set(&p->rid, 0);
47787c48fa3SEric Dumazet 		atomic_set(&p->ip_id_count,
47887c48fa3SEric Dumazet 				(daddr->family == AF_INET) ?
47987c48fa3SEric Dumazet 					secure_ip_id(daddr->addr.a4) :
48087c48fa3SEric Dumazet 					secure_ipv6_id(daddr->addr.a6));
481aa1039e7SEric Dumazet 		p->tcp_ts_stamp = 0;
482144001bdSDavid S. Miller 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
48392d86829SDavid S. Miller 		p->rate_tokens = 0;
48492d86829SDavid S. Miller 		p->rate_last = 0;
485ddd4aa42SDavid S. Miller 		p->pmtu_expires = 0;
48646af3180SHiroaki SHIMODA 		p->pmtu_orig = 0;
487ddd4aa42SDavid S. Miller 		memset(&p->redirect_learned, 0, sizeof(p->redirect_learned));
4885faa5df1SSteffen Klassert 		INIT_LIST_HEAD(&p->gc_list);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 		/* Link the node. */
49198158f5aSDavid S. Miller 		link_to_pool(p, base);
49298158f5aSDavid S. Miller 		base->total++;
493aa1039e7SEric Dumazet 	}
49465e8354eSEric Dumazet 	write_sequnlock_bh(&base->lock);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	return p;
4971da177e4SLinus Torvalds }
498b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_getpeer);
49998158f5aSDavid S. Miller 
5004663afe2SEric Dumazet void inet_putpeer(struct inet_peer *p)
5014663afe2SEric Dumazet {
5024663afe2SEric Dumazet 	p->dtime = (__u32)jiffies;
5036d1a3e04SEric Dumazet 	smp_mb__before_atomic_dec();
5044b9d9be8SEric Dumazet 	atomic_dec(&p->refcnt);
5054663afe2SEric Dumazet }
506b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_putpeer);
50792d86829SDavid S. Miller 
50892d86829SDavid S. Miller /*
50992d86829SDavid S. Miller  *	Check transmit rate limitation for given message.
51092d86829SDavid S. Miller  *	The rate information is held in the inet_peer entries now.
51192d86829SDavid S. Miller  *	This function is generic and could be used for other purposes
51292d86829SDavid S. Miller  *	too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
51392d86829SDavid S. Miller  *
51492d86829SDavid S. Miller  *	Note that the same inet_peer fields are modified by functions in
51592d86829SDavid S. Miller  *	route.c too, but these work for packet destinations while xrlim_allow
51692d86829SDavid S. Miller  *	works for icmp destinations. This means the rate limiting information
51792d86829SDavid S. Miller  *	for one "ip object" is shared - and these ICMPs are twice limited:
51892d86829SDavid S. Miller  *	by source and by destination.
51992d86829SDavid S. Miller  *
52092d86829SDavid S. Miller  *	RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
52192d86829SDavid S. Miller  *			  SHOULD allow setting of rate limits
52292d86829SDavid S. Miller  *
52392d86829SDavid S. Miller  * 	Shared between ICMPv4 and ICMPv6.
52492d86829SDavid S. Miller  */
52592d86829SDavid S. Miller #define XRLIM_BURST_FACTOR 6
52692d86829SDavid S. Miller bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
52792d86829SDavid S. Miller {
52892d86829SDavid S. Miller 	unsigned long now, token;
52992d86829SDavid S. Miller 	bool rc = false;
53092d86829SDavid S. Miller 
53192d86829SDavid S. Miller 	if (!peer)
53292d86829SDavid S. Miller 		return true;
53392d86829SDavid S. Miller 
53492d86829SDavid S. Miller 	token = peer->rate_tokens;
53592d86829SDavid S. Miller 	now = jiffies;
53692d86829SDavid S. Miller 	token += now - peer->rate_last;
53792d86829SDavid S. Miller 	peer->rate_last = now;
53892d86829SDavid S. Miller 	if (token > XRLIM_BURST_FACTOR * timeout)
53992d86829SDavid S. Miller 		token = XRLIM_BURST_FACTOR * timeout;
54092d86829SDavid S. Miller 	if (token >= timeout) {
54192d86829SDavid S. Miller 		token -= timeout;
54292d86829SDavid S. Miller 		rc = true;
54392d86829SDavid S. Miller 	}
54492d86829SDavid S. Miller 	peer->rate_tokens = token;
54592d86829SDavid S. Miller 	return rc;
54692d86829SDavid S. Miller }
54792d86829SDavid S. Miller EXPORT_SYMBOL(inet_peer_xrlim_allow);
5485faa5df1SSteffen Klassert 
54955432d2bSEric Dumazet static void inetpeer_inval_rcu(struct rcu_head *head)
55055432d2bSEric Dumazet {
55155432d2bSEric Dumazet 	struct inet_peer *p = container_of(head, struct inet_peer, gc_rcu);
55255432d2bSEric Dumazet 
55355432d2bSEric Dumazet 	spin_lock_bh(&gc_lock);
55455432d2bSEric Dumazet 	list_add_tail(&p->gc_list, &gc_list);
55555432d2bSEric Dumazet 	spin_unlock_bh(&gc_lock);
55655432d2bSEric Dumazet 
55755432d2bSEric Dumazet 	schedule_delayed_work(&gc_work, gc_delay);
55855432d2bSEric Dumazet }
55955432d2bSEric Dumazet 
56056a6b248SDavid S. Miller void inetpeer_invalidate_tree(struct inet_peer_base *base)
5615faa5df1SSteffen Klassert {
5625faa5df1SSteffen Klassert 	struct inet_peer *old, *new, *prev;
5635faa5df1SSteffen Klassert 
5645faa5df1SSteffen Klassert 	write_seqlock_bh(&base->lock);
5655faa5df1SSteffen Klassert 
5665faa5df1SSteffen Klassert 	old = base->root;
5675faa5df1SSteffen Klassert 	if (old == peer_avl_empty_rcu)
5685faa5df1SSteffen Klassert 		goto out;
5695faa5df1SSteffen Klassert 
5705faa5df1SSteffen Klassert 	new = peer_avl_empty_rcu;
5715faa5df1SSteffen Klassert 
5725faa5df1SSteffen Klassert 	prev = cmpxchg(&base->root, old, new);
5735faa5df1SSteffen Klassert 	if (prev == old) {
5745faa5df1SSteffen Klassert 		base->total = 0;
57555432d2bSEric Dumazet 		call_rcu(&prev->gc_rcu, inetpeer_inval_rcu);
5765faa5df1SSteffen Klassert 	}
5775faa5df1SSteffen Klassert 
5785faa5df1SSteffen Klassert out:
5795faa5df1SSteffen Klassert 	write_sequnlock_bh(&base->lock);
5805faa5df1SSteffen Klassert }
5815faa5df1SSteffen Klassert EXPORT_SYMBOL(inetpeer_invalidate_tree);
582