xref: /linux/net/ipv4/inetpeer.c (revision 4b9d9be839fdb7dcd7ce7619a623fd9015a50cda)
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.
57*4b9d9be8SEric Dumazet  *  3.  Global variable peer_total is modified under the pool lock.
58*4b9d9be8SEric Dumazet  *  4.  struct inet_peer fields modification:
591da177e4SLinus Torvalds  *		avl_left, avl_right, avl_parent, avl_height: pool lock
601da177e4SLinus Torvalds  *		refcnt: atomically against modifications on other CPU;
611da177e4SLinus Torvalds  *		   usually under some other lock to prevent node disappearing
62582a72daSDavid S. Miller  *		daddr: unchangeable
63317fe0e6SEric Dumazet  *		ip_id_count: atomic value (no lock needed)
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds 
66e18b890bSChristoph Lameter static struct kmem_cache *peer_cachep __read_mostly;
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #define node_height(x) x->avl_height
69d6cc1d64SEric Dumazet 
70d6cc1d64SEric Dumazet #define peer_avl_empty ((struct inet_peer *)&peer_fake_node)
71b914c4eaSEric Dumazet #define peer_avl_empty_rcu ((struct inet_peer __rcu __force *)&peer_fake_node)
72d6cc1d64SEric Dumazet static const struct inet_peer peer_fake_node = {
73b914c4eaSEric Dumazet 	.avl_left	= peer_avl_empty_rcu,
74b914c4eaSEric Dumazet 	.avl_right	= peer_avl_empty_rcu,
751da177e4SLinus Torvalds 	.avl_height	= 0
761da177e4SLinus Torvalds };
77d6cc1d64SEric Dumazet 
78021e9299SDavid S. Miller struct inet_peer_base {
79b914c4eaSEric Dumazet 	struct inet_peer __rcu *root;
8065e8354eSEric Dumazet 	seqlock_t	lock;
81d6cc1d64SEric Dumazet 	int		total;
82021e9299SDavid S. Miller };
83021e9299SDavid S. Miller 
84021e9299SDavid S. Miller static struct inet_peer_base v4_peers = {
85b914c4eaSEric Dumazet 	.root		= peer_avl_empty_rcu,
8665e8354eSEric Dumazet 	.lock		= __SEQLOCK_UNLOCKED(v4_peers.lock),
87d6cc1d64SEric Dumazet 	.total		= 0,
88d6cc1d64SEric Dumazet };
89021e9299SDavid S. Miller 
90021e9299SDavid S. Miller static struct inet_peer_base v6_peers = {
91021e9299SDavid S. Miller 	.root		= peer_avl_empty_rcu,
9265e8354eSEric Dumazet 	.lock		= __SEQLOCK_UNLOCKED(v6_peers.lock),
93021e9299SDavid S. Miller 	.total		= 0,
94021e9299SDavid S. Miller };
95021e9299SDavid S. Miller 
961da177e4SLinus Torvalds #define PEER_MAXDEPTH 40 /* sufficient for about 2^27 nodes */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
99243bbcaaSEric Dumazet int inet_peer_threshold __read_mostly = 65536 + 128;	/* start to throw entries more
1001da177e4SLinus Torvalds 					 * aggressively at this stage */
101243bbcaaSEric Dumazet int inet_peer_minttl __read_mostly = 120 * HZ;	/* TTL under high load: 120 sec */
102243bbcaaSEric Dumazet int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;	/* usual time to live: 10 min */
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /* Called from ip_output.c:ip_init  */
1061da177e4SLinus Torvalds void __init inet_initpeers(void)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	struct sysinfo si;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	/* Use the straight interface to information about memory. */
1111da177e4SLinus Torvalds 	si_meminfo(&si);
1121da177e4SLinus Torvalds 	/* The values below were suggested by Alexey Kuznetsov
1131da177e4SLinus Torvalds 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
1141da177e4SLinus Torvalds 	 * myself.  --SAW
1151da177e4SLinus Torvalds 	 */
1161da177e4SLinus Torvalds 	if (si.totalram <= (32768*1024)/PAGE_SIZE)
1171da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
1181da177e4SLinus Torvalds 	if (si.totalram <= (16384*1024)/PAGE_SIZE)
1191da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* about 512KB */
1201da177e4SLinus Torvalds 	if (si.totalram <= (8192*1024)/PAGE_SIZE)
1211da177e4SLinus Torvalds 		inet_peer_threshold >>= 2; /* about 128KB */
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	peer_cachep = kmem_cache_create("inet_peer_cache",
1241da177e4SLinus Torvalds 			sizeof(struct inet_peer),
125317fe0e6SEric Dumazet 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
12620c2df83SPaul Mundt 			NULL);
1271da177e4SLinus Torvalds 
128d6cc1d64SEric Dumazet }
1291da177e4SLinus Torvalds 
1308790ca17SDavid S. Miller static int addr_compare(const struct inetpeer_addr *a,
1318790ca17SDavid S. Miller 			const struct inetpeer_addr *b)
13202663045SDavid S. Miller {
13302663045SDavid S. Miller 	int i, n = (a->family == AF_INET ? 1 : 4);
13402663045SDavid S. Miller 
13502663045SDavid S. Miller 	for (i = 0; i < n; i++) {
1367a71ed89SDavid S. Miller 		if (a->addr.a6[i] == b->addr.a6[i])
13702663045SDavid S. Miller 			continue;
1387a71ed89SDavid S. Miller 		if (a->addr.a6[i] < b->addr.a6[i])
13902663045SDavid S. Miller 			return -1;
14002663045SDavid S. Miller 		return 1;
14102663045SDavid S. Miller 	}
14202663045SDavid S. Miller 
14302663045SDavid S. Miller 	return 0;
14402663045SDavid S. Miller }
14502663045SDavid S. Miller 
14665e8354eSEric Dumazet #define rcu_deref_locked(X, BASE)				\
14765e8354eSEric Dumazet 	rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock))
14865e8354eSEric Dumazet 
149243bbcaaSEric Dumazet /*
150243bbcaaSEric Dumazet  * Called with local BH disabled and the pool lock held.
151243bbcaaSEric Dumazet  */
15298158f5aSDavid S. Miller #define lookup(_daddr, _stack, _base)				\
1531da177e4SLinus Torvalds ({								\
154b914c4eaSEric Dumazet 	struct inet_peer *u;					\
155b914c4eaSEric Dumazet 	struct inet_peer __rcu **v;				\
156aa1039e7SEric Dumazet 								\
157243bbcaaSEric Dumazet 	stackptr = _stack;					\
15898158f5aSDavid S. Miller 	*stackptr++ = &_base->root;				\
15965e8354eSEric Dumazet 	for (u = rcu_deref_locked(_base->root, _base);		\
160b914c4eaSEric Dumazet 	     u != peer_avl_empty; ) {				\
16102663045SDavid S. Miller 		int cmp = addr_compare(_daddr, &u->daddr);	\
16202663045SDavid S. Miller 		if (cmp == 0)					\
1631da177e4SLinus Torvalds 			break;					\
16402663045SDavid S. Miller 		if (cmp == -1)					\
1651da177e4SLinus Torvalds 			v = &u->avl_left;			\
1661da177e4SLinus Torvalds 		else						\
1671da177e4SLinus Torvalds 			v = &u->avl_right;			\
1681da177e4SLinus Torvalds 		*stackptr++ = v;				\
16965e8354eSEric Dumazet 		u = rcu_deref_locked(*v, _base);		\
1701da177e4SLinus Torvalds 	}							\
1711da177e4SLinus Torvalds 	u;							\
1721da177e4SLinus Torvalds })
1731da177e4SLinus Torvalds 
174aa1039e7SEric Dumazet /*
1757b46ac4eSDavid S. Miller  * Called with rcu_read_lock()
176aa1039e7SEric Dumazet  * Because we hold no lock against a writer, its quite possible we fall
177aa1039e7SEric Dumazet  * in an endless loop.
178aa1039e7SEric Dumazet  * But every pointer we follow is guaranteed to be valid thanks to RCU.
179aa1039e7SEric Dumazet  * We exit from this function if number of links exceeds PEER_MAXDEPTH
180aa1039e7SEric Dumazet  */
1817b46ac4eSDavid S. Miller static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr,
182*4b9d9be8SEric Dumazet 				    struct inet_peer_base *base)
183aa1039e7SEric Dumazet {
1847b46ac4eSDavid S. Miller 	struct inet_peer *u = rcu_dereference(base->root);
185aa1039e7SEric Dumazet 	int count = 0;
186aa1039e7SEric Dumazet 
187aa1039e7SEric Dumazet 	while (u != peer_avl_empty) {
18802663045SDavid S. Miller 		int cmp = addr_compare(daddr, &u->daddr);
18902663045SDavid S. Miller 		if (cmp == 0) {
1905f2f8920SEric Dumazet 			/* Before taking a reference, check if this entry was
191*4b9d9be8SEric Dumazet 			 * deleted (refcnt=-1)
1925f2f8920SEric Dumazet 			 */
193*4b9d9be8SEric Dumazet 			if (!atomic_add_unless(&u->refcnt, 1, -1))
194aa1039e7SEric Dumazet 				u = NULL;
195aa1039e7SEric Dumazet 			return u;
196aa1039e7SEric Dumazet 		}
19702663045SDavid S. Miller 		if (cmp == -1)
1987b46ac4eSDavid S. Miller 			u = rcu_dereference(u->avl_left);
199aa1039e7SEric Dumazet 		else
2007b46ac4eSDavid S. Miller 			u = rcu_dereference(u->avl_right);
201aa1039e7SEric Dumazet 		if (unlikely(++count == PEER_MAXDEPTH))
202aa1039e7SEric Dumazet 			break;
203aa1039e7SEric Dumazet 	}
204aa1039e7SEric Dumazet 	return NULL;
205aa1039e7SEric Dumazet }
206aa1039e7SEric Dumazet 
207aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */
20898158f5aSDavid S. Miller #define lookup_rightempty(start, base)				\
2091da177e4SLinus Torvalds ({								\
210b914c4eaSEric Dumazet 	struct inet_peer *u;					\
211b914c4eaSEric Dumazet 	struct inet_peer __rcu **v;				\
2121da177e4SLinus Torvalds 	*stackptr++ = &start->avl_left;				\
2131da177e4SLinus Torvalds 	v = &start->avl_left;					\
21465e8354eSEric Dumazet 	for (u = rcu_deref_locked(*v, base);			\
215b914c4eaSEric Dumazet 	     u->avl_right != peer_avl_empty_rcu; ) {		\
2161da177e4SLinus Torvalds 		v = &u->avl_right;				\
2171da177e4SLinus Torvalds 		*stackptr++ = v;				\
21865e8354eSEric Dumazet 		u = rcu_deref_locked(*v, base);			\
2191da177e4SLinus Torvalds 	}							\
2201da177e4SLinus Torvalds 	u;							\
2211da177e4SLinus Torvalds })
2221da177e4SLinus Torvalds 
223aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held.
2241da177e4SLinus Torvalds  * Variable names are the proof of operation correctness.
225aa1039e7SEric Dumazet  * Look into mm/map_avl.c for more detail description of the ideas.
226aa1039e7SEric Dumazet  */
227b914c4eaSEric Dumazet static void peer_avl_rebalance(struct inet_peer __rcu **stack[],
22898158f5aSDavid S. Miller 			       struct inet_peer __rcu ***stackend,
22998158f5aSDavid S. Miller 			       struct inet_peer_base *base)
2301da177e4SLinus Torvalds {
231b914c4eaSEric Dumazet 	struct inet_peer __rcu **nodep;
232b914c4eaSEric Dumazet 	struct inet_peer *node, *l, *r;
2331da177e4SLinus Torvalds 	int lh, rh;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	while (stackend > stack) {
2361da177e4SLinus Torvalds 		nodep = *--stackend;
23765e8354eSEric Dumazet 		node = rcu_deref_locked(*nodep, base);
23865e8354eSEric Dumazet 		l = rcu_deref_locked(node->avl_left, base);
23965e8354eSEric Dumazet 		r = rcu_deref_locked(node->avl_right, base);
2401da177e4SLinus Torvalds 		lh = node_height(l);
2411da177e4SLinus Torvalds 		rh = node_height(r);
2421da177e4SLinus Torvalds 		if (lh > rh + 1) { /* l: RH+2 */
2431da177e4SLinus Torvalds 			struct inet_peer *ll, *lr, *lrl, *lrr;
2441da177e4SLinus Torvalds 			int lrh;
24565e8354eSEric Dumazet 			ll = rcu_deref_locked(l->avl_left, base);
24665e8354eSEric Dumazet 			lr = rcu_deref_locked(l->avl_right, base);
2471da177e4SLinus Torvalds 			lrh = node_height(lr);
2481da177e4SLinus Torvalds 			if (lrh <= node_height(ll)) {	/* ll: RH+1 */
249b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, lr);	/* lr: RH or RH+1 */
250b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, r);	/* r: RH */
2511da177e4SLinus Torvalds 				node->avl_height = lrh + 1; /* RH+1 or RH+2 */
252b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_left, ll);       /* ll: RH+1 */
253b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_right, node);	/* node: RH+1 or RH+2 */
2541da177e4SLinus Torvalds 				l->avl_height = node->avl_height + 1;
255b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, l);
2561da177e4SLinus Torvalds 			} else { /* ll: RH, lr: RH+1 */
25765e8354eSEric Dumazet 				lrl = rcu_deref_locked(lr->avl_left, base);/* lrl: RH or RH-1 */
25865e8354eSEric Dumazet 				lrr = rcu_deref_locked(lr->avl_right, base);/* lrr: RH or RH-1 */
259b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, lrr);	/* lrr: RH or RH-1 */
260b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, r);	/* r: RH */
2611da177e4SLinus Torvalds 				node->avl_height = rh + 1; /* node: RH+1 */
262b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_left, ll);	/* ll: RH */
263b914c4eaSEric Dumazet 				RCU_INIT_POINTER(l->avl_right, lrl);	/* lrl: RH or RH-1 */
2641da177e4SLinus Torvalds 				l->avl_height = rh + 1;	/* l: RH+1 */
265b914c4eaSEric Dumazet 				RCU_INIT_POINTER(lr->avl_left, l);	/* l: RH+1 */
266b914c4eaSEric Dumazet 				RCU_INIT_POINTER(lr->avl_right, node);	/* node: RH+1 */
2671da177e4SLinus Torvalds 				lr->avl_height = rh + 2;
268b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, lr);
2691da177e4SLinus Torvalds 			}
2701da177e4SLinus Torvalds 		} else if (rh > lh + 1) { /* r: LH+2 */
2711da177e4SLinus Torvalds 			struct inet_peer *rr, *rl, *rlr, *rll;
2721da177e4SLinus Torvalds 			int rlh;
27365e8354eSEric Dumazet 			rr = rcu_deref_locked(r->avl_right, base);
27465e8354eSEric Dumazet 			rl = rcu_deref_locked(r->avl_left, base);
2751da177e4SLinus Torvalds 			rlh = node_height(rl);
2761da177e4SLinus Torvalds 			if (rlh <= node_height(rr)) {	/* rr: LH+1 */
277b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, rl);	/* rl: LH or LH+1 */
278b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, l);	/* l: LH */
2791da177e4SLinus Torvalds 				node->avl_height = rlh + 1; /* LH+1 or LH+2 */
280b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_right, rr);	/* rr: LH+1 */
281b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_left, node);	/* node: LH+1 or LH+2 */
2821da177e4SLinus Torvalds 				r->avl_height = node->avl_height + 1;
283b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, r);
2841da177e4SLinus Torvalds 			} else { /* rr: RH, rl: RH+1 */
28565e8354eSEric Dumazet 				rlr = rcu_deref_locked(rl->avl_right, base);/* rlr: LH or LH-1 */
28665e8354eSEric Dumazet 				rll = rcu_deref_locked(rl->avl_left, base);/* rll: LH or LH-1 */
287b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_right, rll);	/* rll: LH or LH-1 */
288b914c4eaSEric Dumazet 				RCU_INIT_POINTER(node->avl_left, l);	/* l: LH */
2891da177e4SLinus Torvalds 				node->avl_height = lh + 1; /* node: LH+1 */
290b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_right, rr);	/* rr: LH */
291b914c4eaSEric Dumazet 				RCU_INIT_POINTER(r->avl_left, rlr);	/* rlr: LH or LH-1 */
2921da177e4SLinus Torvalds 				r->avl_height = lh + 1;	/* r: LH+1 */
293b914c4eaSEric Dumazet 				RCU_INIT_POINTER(rl->avl_right, r);	/* r: LH+1 */
294b914c4eaSEric Dumazet 				RCU_INIT_POINTER(rl->avl_left, node);	/* node: LH+1 */
2951da177e4SLinus Torvalds 				rl->avl_height = lh + 2;
296b914c4eaSEric Dumazet 				RCU_INIT_POINTER(*nodep, rl);
2971da177e4SLinus Torvalds 			}
2981da177e4SLinus Torvalds 		} else {
2991da177e4SLinus Torvalds 			node->avl_height = (lh > rh ? lh : rh) + 1;
3001da177e4SLinus Torvalds 		}
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
304aa1039e7SEric Dumazet /* Called with local BH disabled and the pool lock held. */
30598158f5aSDavid S. Miller #define link_to_pool(n, base)					\
3061da177e4SLinus Torvalds do {								\
3071da177e4SLinus Torvalds 	n->avl_height = 1;					\
308b914c4eaSEric Dumazet 	n->avl_left = peer_avl_empty_rcu;			\
309b914c4eaSEric Dumazet 	n->avl_right = peer_avl_empty_rcu;			\
310b914c4eaSEric Dumazet 	/* lockless readers can catch us now */			\
311b914c4eaSEric Dumazet 	rcu_assign_pointer(**--stackptr, n);			\
31298158f5aSDavid S. Miller 	peer_avl_rebalance(stack, stackptr, base);		\
3131da177e4SLinus Torvalds } while (0)
3141da177e4SLinus Torvalds 
315aa1039e7SEric Dumazet static void inetpeer_free_rcu(struct rcu_head *head)
316aa1039e7SEric Dumazet {
317aa1039e7SEric Dumazet 	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
318aa1039e7SEric Dumazet }
319aa1039e7SEric Dumazet 
32066944e1cSEric Dumazet static void unlink_from_pool(struct inet_peer *p, struct inet_peer_base *base,
32166944e1cSEric Dumazet 			     struct inet_peer __rcu **stack[PEER_MAXDEPTH])
3221da177e4SLinus Torvalds {
323b914c4eaSEric Dumazet 	struct inet_peer __rcu ***stackptr, ***delp;
324*4b9d9be8SEric Dumazet 
32502663045SDavid S. Miller 	if (lookup(&p->daddr, stack, base) != p)
3261da177e4SLinus Torvalds 		BUG();
3271da177e4SLinus Torvalds 	delp = stackptr - 1; /* *delp[0] == p */
328b914c4eaSEric Dumazet 	if (p->avl_left == peer_avl_empty_rcu) {
3291da177e4SLinus Torvalds 		*delp[0] = p->avl_right;
3301da177e4SLinus Torvalds 		--stackptr;
3311da177e4SLinus Torvalds 	} else {
3321da177e4SLinus Torvalds 		/* look for a node to insert instead of p */
3331da177e4SLinus Torvalds 		struct inet_peer *t;
33498158f5aSDavid S. Miller 		t = lookup_rightempty(p, base);
33565e8354eSEric Dumazet 		BUG_ON(rcu_deref_locked(*stackptr[-1], base) != t);
3361da177e4SLinus Torvalds 		**--stackptr = t->avl_left;
337582a72daSDavid S. Miller 		/* t is removed, t->daddr > x->daddr for any
3381da177e4SLinus Torvalds 		 * x in p->avl_left subtree.
3391da177e4SLinus Torvalds 		 * Put t in the old place of p. */
340b914c4eaSEric Dumazet 		RCU_INIT_POINTER(*delp[0], t);
3411da177e4SLinus Torvalds 		t->avl_left = p->avl_left;
3421da177e4SLinus Torvalds 		t->avl_right = p->avl_right;
3431da177e4SLinus Torvalds 		t->avl_height = p->avl_height;
34409a62660SKris Katterjohn 		BUG_ON(delp[1] != &p->avl_left);
3451da177e4SLinus Torvalds 		delp[1] = &t->avl_left; /* was &p->avl_left */
3461da177e4SLinus Torvalds 	}
34798158f5aSDavid S. Miller 	peer_avl_rebalance(stack, stackptr, base);
34898158f5aSDavid S. Miller 	base->total--;
3494e75db2eSEric Dumazet 	call_rcu(&p->rcu, inetpeer_free_rcu);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352021e9299SDavid S. Miller static struct inet_peer_base *family_to_base(int family)
353021e9299SDavid S. Miller {
354*4b9d9be8SEric Dumazet 	return family == AF_INET ? &v4_peers : &v6_peers;
355021e9299SDavid S. Miller }
356021e9299SDavid S. Miller 
357*4b9d9be8SEric Dumazet /* perform garbage collect on all items stacked during a lookup */
358*4b9d9be8SEric Dumazet static int inet_peer_gc(struct inet_peer_base *base,
359*4b9d9be8SEric Dumazet 			struct inet_peer __rcu **stack[PEER_MAXDEPTH],
360*4b9d9be8SEric Dumazet 			struct inet_peer __rcu ***stackptr)
36198158f5aSDavid S. Miller {
362*4b9d9be8SEric Dumazet 	struct inet_peer *p, *gchead = NULL;
363*4b9d9be8SEric Dumazet 	__u32 delta, ttl;
364*4b9d9be8SEric Dumazet 	int cnt = 0;
36598158f5aSDavid S. Miller 
366*4b9d9be8SEric Dumazet 	if (base->total >= inet_peer_threshold)
367*4b9d9be8SEric Dumazet 		ttl = 0; /* be aggressive */
368*4b9d9be8SEric Dumazet 	else
369*4b9d9be8SEric Dumazet 		ttl = inet_peer_maxttl
370*4b9d9be8SEric Dumazet 				- (inet_peer_maxttl - inet_peer_minttl) / HZ *
371*4b9d9be8SEric Dumazet 					base->total / inet_peer_threshold * HZ;
372*4b9d9be8SEric Dumazet 	stackptr--; /* last stack slot is peer_avl_empty */
373*4b9d9be8SEric Dumazet 	while (stackptr > stack) {
374*4b9d9be8SEric Dumazet 		stackptr--;
375*4b9d9be8SEric Dumazet 		p = rcu_deref_locked(**stackptr, base);
376d71209deSPavel Emelyanov 		delta = (__u32)jiffies - p->dtime;
377*4b9d9be8SEric Dumazet 		if (atomic_read(&p->refcnt) == 0 && delta >= ttl &&
378*4b9d9be8SEric Dumazet 		    atomic_cmpxchg(&p->refcnt, 0, -1) == 0) {
379*4b9d9be8SEric Dumazet 			p->gc_next = gchead;
380*4b9d9be8SEric Dumazet 			gchead = p;
381*4b9d9be8SEric Dumazet 		}
382*4b9d9be8SEric Dumazet 	}
383*4b9d9be8SEric Dumazet 	while ((p = gchead) != NULL) {
384*4b9d9be8SEric Dumazet 		gchead = p->gc_next;
385*4b9d9be8SEric Dumazet 		cnt++;
386*4b9d9be8SEric Dumazet 		unlink_from_pool(p, base, stack);
387*4b9d9be8SEric Dumazet 	}
388*4b9d9be8SEric Dumazet 	return cnt;
3891da177e4SLinus Torvalds }
390d71209deSPavel Emelyanov 
3918790ca17SDavid S. Miller struct inet_peer *inet_getpeer(struct inetpeer_addr *daddr, int create)
3921da177e4SLinus Torvalds {
393b914c4eaSEric Dumazet 	struct inet_peer __rcu **stack[PEER_MAXDEPTH], ***stackptr;
3943408404aSDavid S. Miller 	struct inet_peer_base *base = family_to_base(daddr->family);
39598158f5aSDavid S. Miller 	struct inet_peer *p;
39665e8354eSEric Dumazet 	unsigned int sequence;
397*4b9d9be8SEric Dumazet 	int invalidated, gccnt = 0;
3981da177e4SLinus Torvalds 
399*4b9d9be8SEric Dumazet 	/* Attempt a lockless lookup first.
400aa1039e7SEric Dumazet 	 * Because of a concurrent writer, we might not find an existing entry.
401aa1039e7SEric Dumazet 	 */
4027b46ac4eSDavid S. Miller 	rcu_read_lock();
40365e8354eSEric Dumazet 	sequence = read_seqbegin(&base->lock);
404*4b9d9be8SEric Dumazet 	p = lookup_rcu(daddr, base);
40565e8354eSEric Dumazet 	invalidated = read_seqretry(&base->lock, sequence);
4067b46ac4eSDavid S. Miller 	rcu_read_unlock();
4071da177e4SLinus Torvalds 
408*4b9d9be8SEric Dumazet 	if (p)
4091da177e4SLinus Torvalds 		return p;
4101da177e4SLinus Torvalds 
41165e8354eSEric Dumazet 	/* If no writer did a change during our lookup, we can return early. */
41265e8354eSEric Dumazet 	if (!create && !invalidated)
41365e8354eSEric Dumazet 		return NULL;
41465e8354eSEric Dumazet 
415aa1039e7SEric Dumazet 	/* retry an exact lookup, taking the lock before.
416aa1039e7SEric Dumazet 	 * At least, nodes should be hot in our cache.
417aa1039e7SEric Dumazet 	 */
41865e8354eSEric Dumazet 	write_seqlock_bh(&base->lock);
419*4b9d9be8SEric Dumazet relookup:
42002663045SDavid S. Miller 	p = lookup(daddr, stack, base);
421aa1039e7SEric Dumazet 	if (p != peer_avl_empty) {
422*4b9d9be8SEric Dumazet 		atomic_inc(&p->refcnt);
42365e8354eSEric Dumazet 		write_sequnlock_bh(&base->lock);
424*4b9d9be8SEric Dumazet 		return p;
425*4b9d9be8SEric Dumazet 	}
426*4b9d9be8SEric Dumazet 	if (!gccnt) {
427*4b9d9be8SEric Dumazet 		gccnt = inet_peer_gc(base, stack, stackptr);
428*4b9d9be8SEric Dumazet 		if (gccnt && create)
429*4b9d9be8SEric Dumazet 			goto relookup;
430aa1039e7SEric Dumazet 	}
431aa1039e7SEric Dumazet 	p = create ? kmem_cache_alloc(peer_cachep, GFP_ATOMIC) : NULL;
432aa1039e7SEric Dumazet 	if (p) {
433b534ecf1SDavid S. Miller 		p->daddr = *daddr;
434aa1039e7SEric Dumazet 		atomic_set(&p->refcnt, 1);
435aa1039e7SEric Dumazet 		atomic_set(&p->rid, 0);
4367a71ed89SDavid S. Miller 		atomic_set(&p->ip_id_count, secure_ip_id(daddr->addr.a4));
437aa1039e7SEric Dumazet 		p->tcp_ts_stamp = 0;
438144001bdSDavid S. Miller 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
43992d86829SDavid S. Miller 		p->rate_tokens = 0;
44092d86829SDavid S. Miller 		p->rate_last = 0;
441ddd4aa42SDavid S. Miller 		p->pmtu_expires = 0;
44246af3180SHiroaki SHIMODA 		p->pmtu_orig = 0;
443ddd4aa42SDavid S. Miller 		memset(&p->redirect_learned, 0, sizeof(p->redirect_learned));
444aa1039e7SEric Dumazet 
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 		/* Link the node. */
44798158f5aSDavid S. Miller 		link_to_pool(p, base);
44898158f5aSDavid S. Miller 		base->total++;
449aa1039e7SEric Dumazet 	}
45065e8354eSEric Dumazet 	write_sequnlock_bh(&base->lock);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	return p;
4531da177e4SLinus Torvalds }
454b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_getpeer);
45598158f5aSDavid S. Miller 
4564663afe2SEric Dumazet void inet_putpeer(struct inet_peer *p)
4574663afe2SEric Dumazet {
4584663afe2SEric Dumazet 	p->dtime = (__u32)jiffies;
459*4b9d9be8SEric Dumazet 	atomic_dec(&p->refcnt);
4604663afe2SEric Dumazet }
461b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_putpeer);
46292d86829SDavid S. Miller 
46392d86829SDavid S. Miller /*
46492d86829SDavid S. Miller  *	Check transmit rate limitation for given message.
46592d86829SDavid S. Miller  *	The rate information is held in the inet_peer entries now.
46692d86829SDavid S. Miller  *	This function is generic and could be used for other purposes
46792d86829SDavid S. Miller  *	too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
46892d86829SDavid S. Miller  *
46992d86829SDavid S. Miller  *	Note that the same inet_peer fields are modified by functions in
47092d86829SDavid S. Miller  *	route.c too, but these work for packet destinations while xrlim_allow
47192d86829SDavid S. Miller  *	works for icmp destinations. This means the rate limiting information
47292d86829SDavid S. Miller  *	for one "ip object" is shared - and these ICMPs are twice limited:
47392d86829SDavid S. Miller  *	by source and by destination.
47492d86829SDavid S. Miller  *
47592d86829SDavid S. Miller  *	RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
47692d86829SDavid S. Miller  *			  SHOULD allow setting of rate limits
47792d86829SDavid S. Miller  *
47892d86829SDavid S. Miller  * 	Shared between ICMPv4 and ICMPv6.
47992d86829SDavid S. Miller  */
48092d86829SDavid S. Miller #define XRLIM_BURST_FACTOR 6
48192d86829SDavid S. Miller bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
48292d86829SDavid S. Miller {
48392d86829SDavid S. Miller 	unsigned long now, token;
48492d86829SDavid S. Miller 	bool rc = false;
48592d86829SDavid S. Miller 
48692d86829SDavid S. Miller 	if (!peer)
48792d86829SDavid S. Miller 		return true;
48892d86829SDavid S. Miller 
48992d86829SDavid S. Miller 	token = peer->rate_tokens;
49092d86829SDavid S. Miller 	now = jiffies;
49192d86829SDavid S. Miller 	token += now - peer->rate_last;
49292d86829SDavid S. Miller 	peer->rate_last = now;
49392d86829SDavid S. Miller 	if (token > XRLIM_BURST_FACTOR * timeout)
49492d86829SDavid S. Miller 		token = XRLIM_BURST_FACTOR * timeout;
49592d86829SDavid S. Miller 	if (token >= timeout) {
49692d86829SDavid S. Miller 		token -= timeout;
49792d86829SDavid S. Miller 		rc = true;
49892d86829SDavid S. Miller 	}
49992d86829SDavid S. Miller 	peer->rate_tokens = token;
50092d86829SDavid S. Miller 	return rc;
50192d86829SDavid S. Miller }
50292d86829SDavid S. Miller EXPORT_SYMBOL(inet_peer_xrlim_allow);
503