xref: /openbmc/linux/net/ipv4/inetpeer.c (revision 08009a760213cf6125af9453a51203f4ae108ba1)
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 
9*08009a76SAlexey Dobriyan #include <linux/cache.h>
101da177e4SLinus Torvalds #include <linux/module.h>
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/interrupt.h>
141da177e4SLinus Torvalds #include <linux/spinlock.h>
151da177e4SLinus Torvalds #include <linux/random.h>
161da177e4SLinus Torvalds #include <linux/timer.h>
171da177e4SLinus Torvalds #include <linux/time.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/mm.h>
201da177e4SLinus Torvalds #include <linux/net.h>
215faa5df1SSteffen Klassert #include <linux/workqueue.h>
2220380731SArnaldo Carvalho de Melo #include <net/ip.h>
231da177e4SLinus Torvalds #include <net/inetpeer.h>
246e5714eaSDavid S. Miller #include <net/secure_seq.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  *  Theory of operations.
281da177e4SLinus Torvalds  *  We keep one entry for each peer IP address.  The nodes contains long-living
291da177e4SLinus Torvalds  *  information about the peer which doesn't depend on routes.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *  Nodes are removed only when reference counter goes to 0.
321da177e4SLinus Torvalds  *  When it's happened the node may be removed when a sufficient amount of
331da177e4SLinus Torvalds  *  time has been passed since its last use.  The less-recently-used entry can
341da177e4SLinus Torvalds  *  also be removed if the pool is overloaded i.e. if the total amount of
351da177e4SLinus Torvalds  *  entries is greater-or-equal than the threshold.
361da177e4SLinus Torvalds  *
37b145425fSEric Dumazet  *  Node pool is organised as an RB tree.
381da177e4SLinus Torvalds  *  Such an implementation has been chosen not just for fun.  It's a way to
391da177e4SLinus Torvalds  *  prevent easy and efficient DoS attacks by creating hash collisions.  A huge
401da177e4SLinus Torvalds  *  amount of long living nodes in a single hash slot would significantly delay
411da177e4SLinus Torvalds  *  lookups performed with disabled BHs.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  *  Serialisation issues.
44aa1039e7SEric Dumazet  *  1.  Nodes may appear in the tree only with the pool lock held.
45aa1039e7SEric Dumazet  *  2.  Nodes may disappear from the tree only with the pool lock held
461da177e4SLinus Torvalds  *      AND reference count being 0.
474b9d9be8SEric Dumazet  *  3.  Global variable peer_total is modified under the pool lock.
484b9d9be8SEric Dumazet  *  4.  struct inet_peer fields modification:
49b145425fSEric Dumazet  *		rb_node: pool lock
501da177e4SLinus Torvalds  *		refcnt: atomically against modifications on other CPU;
511da177e4SLinus Torvalds  *		   usually under some other lock to prevent node disappearing
52582a72daSDavid S. Miller  *		daddr: unchangeable
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds 
55*08009a76SAlexey Dobriyan static struct kmem_cache *peer_cachep __ro_after_init;
561da177e4SLinus Torvalds 
57c3426b47SDavid S. Miller void inet_peer_base_init(struct inet_peer_base *bp)
58c3426b47SDavid S. Miller {
59b145425fSEric Dumazet 	bp->rb_root = RB_ROOT;
60c3426b47SDavid S. Miller 	seqlock_init(&bp->lock);
61c3426b47SDavid S. Miller 	bp->total = 0;
62c3426b47SDavid S. Miller }
63c3426b47SDavid S. Miller EXPORT_SYMBOL_GPL(inet_peer_base_init);
64021e9299SDavid S. Miller 
65b145425fSEric Dumazet #define PEER_MAX_GC 32
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
68243bbcaaSEric Dumazet int inet_peer_threshold __read_mostly = 65536 + 128;	/* start to throw entries more
691da177e4SLinus Torvalds 					 * aggressively at this stage */
70243bbcaaSEric Dumazet int inet_peer_minttl __read_mostly = 120 * HZ;	/* TTL under high load: 120 sec */
71243bbcaaSEric Dumazet int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;	/* usual time to live: 10 min */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Called from ip_output.c:ip_init  */
741da177e4SLinus Torvalds void __init inet_initpeers(void)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	struct sysinfo si;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	/* Use the straight interface to information about memory. */
791da177e4SLinus Torvalds 	si_meminfo(&si);
801da177e4SLinus Torvalds 	/* The values below were suggested by Alexey Kuznetsov
811da177e4SLinus Torvalds 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
821da177e4SLinus Torvalds 	 * myself.  --SAW
831da177e4SLinus Torvalds 	 */
841da177e4SLinus Torvalds 	if (si.totalram <= (32768*1024)/PAGE_SIZE)
851da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
861da177e4SLinus Torvalds 	if (si.totalram <= (16384*1024)/PAGE_SIZE)
871da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* about 512KB */
881da177e4SLinus Torvalds 	if (si.totalram <= (8192*1024)/PAGE_SIZE)
891da177e4SLinus Torvalds 		inet_peer_threshold >>= 2; /* about 128KB */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	peer_cachep = kmem_cache_create("inet_peer_cache",
921da177e4SLinus Torvalds 			sizeof(struct inet_peer),
93317fe0e6SEric Dumazet 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
9420c2df83SPaul Mundt 			NULL);
95d6cc1d64SEric Dumazet }
961da177e4SLinus Torvalds 
97b145425fSEric Dumazet /* Called with rcu_read_lock() or base->lock held */
98b145425fSEric Dumazet static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
99b145425fSEric Dumazet 				struct inet_peer_base *base,
100b145425fSEric Dumazet 				unsigned int seq,
101b145425fSEric Dumazet 				struct inet_peer *gc_stack[],
102b145425fSEric Dumazet 				unsigned int *gc_cnt,
103b145425fSEric Dumazet 				struct rb_node **parent_p,
104b145425fSEric Dumazet 				struct rb_node ***pp_p)
105aa1039e7SEric Dumazet {
1064cc5b44bSEric Dumazet 	struct rb_node **pp, *parent, *next;
107b145425fSEric Dumazet 	struct inet_peer *p;
108aa1039e7SEric Dumazet 
109b145425fSEric Dumazet 	pp = &base->rb_root.rb_node;
110b145425fSEric Dumazet 	parent = NULL;
1114cc5b44bSEric Dumazet 	while (1) {
112b145425fSEric Dumazet 		int cmp;
113b145425fSEric Dumazet 
1144cc5b44bSEric Dumazet 		next = rcu_dereference_raw(*pp);
1154cc5b44bSEric Dumazet 		if (!next)
1164cc5b44bSEric Dumazet 			break;
1174cc5b44bSEric Dumazet 		parent = next;
118b145425fSEric Dumazet 		p = rb_entry(parent, struct inet_peer, rb_node);
119b145425fSEric Dumazet 		cmp = inetpeer_addr_cmp(daddr, &p->daddr);
12002663045SDavid S. Miller 		if (cmp == 0) {
121b145425fSEric Dumazet 			if (!refcount_inc_not_zero(&p->refcnt))
122b145425fSEric Dumazet 				break;
123b145425fSEric Dumazet 			return p;
1241cc9a98bSReshetova, Elena 		}
125b145425fSEric Dumazet 		if (gc_stack) {
126b145425fSEric Dumazet 			if (*gc_cnt < PEER_MAX_GC)
127b145425fSEric Dumazet 				gc_stack[(*gc_cnt)++] = p;
128b145425fSEric Dumazet 		} else if (unlikely(read_seqretry(&base->lock, seq))) {
129aa1039e7SEric Dumazet 			break;
130aa1039e7SEric Dumazet 		}
131b145425fSEric Dumazet 		if (cmp == -1)
13235f493b8SEric Dumazet 			pp = &next->rb_left;
133b145425fSEric Dumazet 		else
13435f493b8SEric Dumazet 			pp = &next->rb_right;
135b145425fSEric Dumazet 	}
136b145425fSEric Dumazet 	*parent_p = parent;
137b145425fSEric Dumazet 	*pp_p = pp;
138aa1039e7SEric Dumazet 	return NULL;
139aa1039e7SEric Dumazet }
140aa1039e7SEric Dumazet 
141aa1039e7SEric Dumazet static void inetpeer_free_rcu(struct rcu_head *head)
142aa1039e7SEric Dumazet {
143aa1039e7SEric Dumazet 	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
144aa1039e7SEric Dumazet }
145aa1039e7SEric Dumazet 
1464b9d9be8SEric Dumazet /* perform garbage collect on all items stacked during a lookup */
147b145425fSEric Dumazet static void inet_peer_gc(struct inet_peer_base *base,
148b145425fSEric Dumazet 			 struct inet_peer *gc_stack[],
149b145425fSEric Dumazet 			 unsigned int gc_cnt)
15098158f5aSDavid S. Miller {
151b145425fSEric Dumazet 	struct inet_peer *p;
1524b9d9be8SEric Dumazet 	__u32 delta, ttl;
153b145425fSEric Dumazet 	int i;
15498158f5aSDavid S. Miller 
1554b9d9be8SEric Dumazet 	if (base->total >= inet_peer_threshold)
1564b9d9be8SEric Dumazet 		ttl = 0; /* be aggressive */
1574b9d9be8SEric Dumazet 	else
1584b9d9be8SEric Dumazet 		ttl = inet_peer_maxttl
1594b9d9be8SEric Dumazet 				- (inet_peer_maxttl - inet_peer_minttl) / HZ *
1604b9d9be8SEric Dumazet 					base->total / inet_peer_threshold * HZ;
161b145425fSEric Dumazet 	for (i = 0; i < gc_cnt; i++) {
162b145425fSEric Dumazet 		p = gc_stack[i];
163d71209deSPavel Emelyanov 		delta = (__u32)jiffies - p->dtime;
164b145425fSEric Dumazet 		if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
165b145425fSEric Dumazet 			gc_stack[i] = NULL;
166b145425fSEric Dumazet 	}
167b145425fSEric Dumazet 	for (i = 0; i < gc_cnt; i++) {
168b145425fSEric Dumazet 		p = gc_stack[i];
169b145425fSEric Dumazet 		if (p) {
170b145425fSEric Dumazet 			rb_erase(&p->rb_node, &base->rb_root);
171b145425fSEric Dumazet 			base->total--;
172b145425fSEric Dumazet 			call_rcu(&p->rcu, inetpeer_free_rcu);
1734b9d9be8SEric Dumazet 		}
1744b9d9be8SEric Dumazet 	}
1756d1a3e04SEric Dumazet }
176d71209deSPavel Emelyanov 
177c0efc887SDavid S. Miller struct inet_peer *inet_getpeer(struct inet_peer_base *base,
178c8a627edSGao feng 			       const struct inetpeer_addr *daddr,
179c8a627edSGao feng 			       int create)
1801da177e4SLinus Torvalds {
181b145425fSEric Dumazet 	struct inet_peer *p, *gc_stack[PEER_MAX_GC];
182b145425fSEric Dumazet 	struct rb_node **pp, *parent;
183b145425fSEric Dumazet 	unsigned int gc_cnt, seq;
184b145425fSEric Dumazet 	int invalidated;
1851da177e4SLinus Torvalds 
1864b9d9be8SEric Dumazet 	/* Attempt a lockless lookup first.
187aa1039e7SEric Dumazet 	 * Because of a concurrent writer, we might not find an existing entry.
188aa1039e7SEric Dumazet 	 */
1897b46ac4eSDavid S. Miller 	rcu_read_lock();
190b145425fSEric Dumazet 	seq = read_seqbegin(&base->lock);
191b145425fSEric Dumazet 	p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
192b145425fSEric Dumazet 	invalidated = read_seqretry(&base->lock, seq);
1937b46ac4eSDavid S. Miller 	rcu_read_unlock();
1941da177e4SLinus Torvalds 
1954b9d9be8SEric Dumazet 	if (p)
1961da177e4SLinus Torvalds 		return p;
1971da177e4SLinus Torvalds 
19865e8354eSEric Dumazet 	/* If no writer did a change during our lookup, we can return early. */
19965e8354eSEric Dumazet 	if (!create && !invalidated)
20065e8354eSEric Dumazet 		return NULL;
20165e8354eSEric Dumazet 
202aa1039e7SEric Dumazet 	/* retry an exact lookup, taking the lock before.
203aa1039e7SEric Dumazet 	 * At least, nodes should be hot in our cache.
204aa1039e7SEric Dumazet 	 */
205b145425fSEric Dumazet 	parent = NULL;
20665e8354eSEric Dumazet 	write_seqlock_bh(&base->lock);
207b145425fSEric Dumazet 
208b145425fSEric Dumazet 	gc_cnt = 0;
209b145425fSEric Dumazet 	p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
210b145425fSEric Dumazet 	if (!p && create) {
211b145425fSEric Dumazet 		p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
212aa1039e7SEric Dumazet 		if (p) {
213b534ecf1SDavid S. Miller 			p->daddr = *daddr;
2141cc9a98bSReshetova, Elena 			refcount_set(&p->refcnt, 2);
215aa1039e7SEric Dumazet 			atomic_set(&p->rid, 0);
216144001bdSDavid S. Miller 			p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
21792d86829SDavid S. Miller 			p->rate_tokens = 0;
218bc9259a8SNicolas Dichtel 			/* 60*HZ is arbitrary, but chosen enough high so that the first
219bc9259a8SNicolas Dichtel 			 * calculation of tokens is at its maximum.
220bc9259a8SNicolas Dichtel 			 */
221bc9259a8SNicolas Dichtel 			p->rate_last = jiffies - 60*HZ;
2221da177e4SLinus Torvalds 
223b145425fSEric Dumazet 			rb_link_node(&p->rb_node, parent, pp);
224b145425fSEric Dumazet 			rb_insert_color(&p->rb_node, &base->rb_root);
22598158f5aSDavid S. Miller 			base->total++;
226aa1039e7SEric Dumazet 		}
227b145425fSEric Dumazet 	}
228b145425fSEric Dumazet 	if (gc_cnt)
229b145425fSEric Dumazet 		inet_peer_gc(base, gc_stack, gc_cnt);
23065e8354eSEric Dumazet 	write_sequnlock_bh(&base->lock);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	return p;
2331da177e4SLinus Torvalds }
234b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_getpeer);
23598158f5aSDavid S. Miller 
2364663afe2SEric Dumazet void inet_putpeer(struct inet_peer *p)
2374663afe2SEric Dumazet {
2384663afe2SEric Dumazet 	p->dtime = (__u32)jiffies;
239b145425fSEric Dumazet 
240b145425fSEric Dumazet 	if (refcount_dec_and_test(&p->refcnt))
241b145425fSEric Dumazet 		call_rcu(&p->rcu, inetpeer_free_rcu);
2424663afe2SEric Dumazet }
243b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_putpeer);
24492d86829SDavid S. Miller 
24592d86829SDavid S. Miller /*
24692d86829SDavid S. Miller  *	Check transmit rate limitation for given message.
24792d86829SDavid S. Miller  *	The rate information is held in the inet_peer entries now.
24892d86829SDavid S. Miller  *	This function is generic and could be used for other purposes
24992d86829SDavid S. Miller  *	too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
25092d86829SDavid S. Miller  *
25192d86829SDavid S. Miller  *	Note that the same inet_peer fields are modified by functions in
25292d86829SDavid S. Miller  *	route.c too, but these work for packet destinations while xrlim_allow
25392d86829SDavid S. Miller  *	works for icmp destinations. This means the rate limiting information
25492d86829SDavid S. Miller  *	for one "ip object" is shared - and these ICMPs are twice limited:
25592d86829SDavid S. Miller  *	by source and by destination.
25692d86829SDavid S. Miller  *
25792d86829SDavid S. Miller  *	RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
25892d86829SDavid S. Miller  *			  SHOULD allow setting of rate limits
25992d86829SDavid S. Miller  *
26092d86829SDavid S. Miller  * 	Shared between ICMPv4 and ICMPv6.
26192d86829SDavid S. Miller  */
26292d86829SDavid S. Miller #define XRLIM_BURST_FACTOR 6
26392d86829SDavid S. Miller bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
26492d86829SDavid S. Miller {
26592d86829SDavid S. Miller 	unsigned long now, token;
26692d86829SDavid S. Miller 	bool rc = false;
26792d86829SDavid S. Miller 
26892d86829SDavid S. Miller 	if (!peer)
26992d86829SDavid S. Miller 		return true;
27092d86829SDavid S. Miller 
27192d86829SDavid S. Miller 	token = peer->rate_tokens;
27292d86829SDavid S. Miller 	now = jiffies;
27392d86829SDavid S. Miller 	token += now - peer->rate_last;
27492d86829SDavid S. Miller 	peer->rate_last = now;
27592d86829SDavid S. Miller 	if (token > XRLIM_BURST_FACTOR * timeout)
27692d86829SDavid S. Miller 		token = XRLIM_BURST_FACTOR * timeout;
27792d86829SDavid S. Miller 	if (token >= timeout) {
27892d86829SDavid S. Miller 		token -= timeout;
27992d86829SDavid S. Miller 		rc = true;
28092d86829SDavid S. Miller 	}
28192d86829SDavid S. Miller 	peer->rate_tokens = token;
28292d86829SDavid S. Miller 	return rc;
28392d86829SDavid S. Miller }
28492d86829SDavid S. Miller EXPORT_SYMBOL(inet_peer_xrlim_allow);
2855faa5df1SSteffen Klassert 
28656a6b248SDavid S. Miller void inetpeer_invalidate_tree(struct inet_peer_base *base)
2875faa5df1SSteffen Klassert {
2888f1975e3SEric Dumazet 	struct rb_node *p = rb_first(&base->rb_root);
2895faa5df1SSteffen Klassert 
2908f1975e3SEric Dumazet 	while (p) {
2918f1975e3SEric Dumazet 		struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
2928f1975e3SEric Dumazet 
2938f1975e3SEric Dumazet 		p = rb_next(p);
2948f1975e3SEric Dumazet 		rb_erase(&peer->rb_node, &base->rb_root);
2958f1975e3SEric Dumazet 		inet_putpeer(peer);
296b145425fSEric Dumazet 		cond_resched();
2975faa5df1SSteffen Klassert 	}
2985faa5df1SSteffen Klassert 
299b145425fSEric Dumazet 	base->total = 0;
3005faa5df1SSteffen Klassert }
3015faa5df1SSteffen Klassert EXPORT_SYMBOL(inetpeer_invalidate_tree);
302