xref: /linux/net/ipv4/inetpeer.c (revision b145425f269a17ed344d737f746b844dfac60c82)
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  *
301da177e4SLinus Torvalds  *  Nodes are removed only when reference counter goes to 0.
311da177e4SLinus Torvalds  *  When it's happened the node may be removed when a sufficient amount of
321da177e4SLinus Torvalds  *  time has been passed since its last use.  The less-recently-used entry can
331da177e4SLinus Torvalds  *  also be removed if the pool is overloaded i.e. if the total amount of
341da177e4SLinus Torvalds  *  entries is greater-or-equal than the threshold.
351da177e4SLinus Torvalds  *
36*b145425fSEric Dumazet  *  Node pool is organised as an RB tree.
371da177e4SLinus Torvalds  *  Such an implementation has been chosen not just for fun.  It's a way to
381da177e4SLinus Torvalds  *  prevent easy and efficient DoS attacks by creating hash collisions.  A huge
391da177e4SLinus Torvalds  *  amount of long living nodes in a single hash slot would significantly delay
401da177e4SLinus Torvalds  *  lookups performed with disabled BHs.
411da177e4SLinus Torvalds  *
421da177e4SLinus Torvalds  *  Serialisation issues.
43aa1039e7SEric Dumazet  *  1.  Nodes may appear in the tree only with the pool lock held.
44aa1039e7SEric Dumazet  *  2.  Nodes may disappear from the tree only with the pool lock held
451da177e4SLinus Torvalds  *      AND reference count being 0.
464b9d9be8SEric Dumazet  *  3.  Global variable peer_total is modified under the pool lock.
474b9d9be8SEric Dumazet  *  4.  struct inet_peer fields modification:
48*b145425fSEric Dumazet  *		rb_node: pool lock
491da177e4SLinus Torvalds  *		refcnt: atomically against modifications on other CPU;
501da177e4SLinus Torvalds  *		   usually under some other lock to prevent node disappearing
51582a72daSDavid S. Miller  *		daddr: unchangeable
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds 
54e18b890bSChristoph Lameter static struct kmem_cache *peer_cachep __read_mostly;
551da177e4SLinus Torvalds 
56c3426b47SDavid S. Miller void inet_peer_base_init(struct inet_peer_base *bp)
57c3426b47SDavid S. Miller {
58*b145425fSEric Dumazet 	bp->rb_root = RB_ROOT;
59c3426b47SDavid S. Miller 	seqlock_init(&bp->lock);
60c3426b47SDavid S. Miller 	bp->total = 0;
61c3426b47SDavid S. Miller }
62c3426b47SDavid S. Miller EXPORT_SYMBOL_GPL(inet_peer_base_init);
63021e9299SDavid S. Miller 
64*b145425fSEric Dumazet #define PEER_MAX_GC 32
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /* Exported for sysctl_net_ipv4.  */
67243bbcaaSEric Dumazet int inet_peer_threshold __read_mostly = 65536 + 128;	/* start to throw entries more
681da177e4SLinus Torvalds 					 * aggressively at this stage */
69243bbcaaSEric Dumazet int inet_peer_minttl __read_mostly = 120 * HZ;	/* TTL under high load: 120 sec */
70243bbcaaSEric Dumazet int inet_peer_maxttl __read_mostly = 10 * 60 * HZ;	/* usual time to live: 10 min */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* Called from ip_output.c:ip_init  */
731da177e4SLinus Torvalds void __init inet_initpeers(void)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct sysinfo si;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	/* Use the straight interface to information about memory. */
781da177e4SLinus Torvalds 	si_meminfo(&si);
791da177e4SLinus Torvalds 	/* The values below were suggested by Alexey Kuznetsov
801da177e4SLinus Torvalds 	 * <kuznet@ms2.inr.ac.ru>.  I don't have any opinion about the values
811da177e4SLinus Torvalds 	 * myself.  --SAW
821da177e4SLinus Torvalds 	 */
831da177e4SLinus Torvalds 	if (si.totalram <= (32768*1024)/PAGE_SIZE)
841da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
851da177e4SLinus Torvalds 	if (si.totalram <= (16384*1024)/PAGE_SIZE)
861da177e4SLinus Torvalds 		inet_peer_threshold >>= 1; /* about 512KB */
871da177e4SLinus Torvalds 	if (si.totalram <= (8192*1024)/PAGE_SIZE)
881da177e4SLinus Torvalds 		inet_peer_threshold >>= 2; /* about 128KB */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	peer_cachep = kmem_cache_create("inet_peer_cache",
911da177e4SLinus Torvalds 			sizeof(struct inet_peer),
92317fe0e6SEric Dumazet 			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
9320c2df83SPaul Mundt 			NULL);
94d6cc1d64SEric Dumazet }
951da177e4SLinus Torvalds 
96*b145425fSEric Dumazet /* Called with rcu_read_lock() or base->lock held */
97*b145425fSEric Dumazet static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
98*b145425fSEric Dumazet 				struct inet_peer_base *base,
99*b145425fSEric Dumazet 				unsigned int seq,
100*b145425fSEric Dumazet 				struct inet_peer *gc_stack[],
101*b145425fSEric Dumazet 				unsigned int *gc_cnt,
102*b145425fSEric Dumazet 				struct rb_node **parent_p,
103*b145425fSEric Dumazet 				struct rb_node ***pp_p)
104aa1039e7SEric Dumazet {
105*b145425fSEric Dumazet 	struct rb_node **pp, *parent;
106*b145425fSEric Dumazet 	struct inet_peer *p;
107aa1039e7SEric Dumazet 
108*b145425fSEric Dumazet 	pp = &base->rb_root.rb_node;
109*b145425fSEric Dumazet 	parent = NULL;
110*b145425fSEric Dumazet 	while (*pp) {
111*b145425fSEric Dumazet 		int cmp;
112*b145425fSEric Dumazet 
113*b145425fSEric Dumazet 		parent = rcu_dereference_raw(*pp);
114*b145425fSEric Dumazet 		p = rb_entry(parent, struct inet_peer, rb_node);
115*b145425fSEric Dumazet 		cmp = inetpeer_addr_cmp(daddr, &p->daddr);
11602663045SDavid S. Miller 		if (cmp == 0) {
117*b145425fSEric Dumazet 			if (!refcount_inc_not_zero(&p->refcnt))
118*b145425fSEric Dumazet 				break;
119*b145425fSEric Dumazet 			return p;
1201cc9a98bSReshetova, Elena 		}
121*b145425fSEric Dumazet 		if (gc_stack) {
122*b145425fSEric Dumazet 			if (*gc_cnt < PEER_MAX_GC)
123*b145425fSEric Dumazet 				gc_stack[(*gc_cnt)++] = p;
124*b145425fSEric Dumazet 		} else if (unlikely(read_seqretry(&base->lock, seq))) {
125aa1039e7SEric Dumazet 			break;
126aa1039e7SEric Dumazet 		}
127*b145425fSEric Dumazet 		if (cmp == -1)
128*b145425fSEric Dumazet 			pp = &(*pp)->rb_left;
129*b145425fSEric Dumazet 		else
130*b145425fSEric Dumazet 			pp = &(*pp)->rb_right;
131*b145425fSEric Dumazet 	}
132*b145425fSEric Dumazet 	*parent_p = parent;
133*b145425fSEric Dumazet 	*pp_p = pp;
134aa1039e7SEric Dumazet 	return NULL;
135aa1039e7SEric Dumazet }
136aa1039e7SEric Dumazet 
137aa1039e7SEric Dumazet static void inetpeer_free_rcu(struct rcu_head *head)
138aa1039e7SEric Dumazet {
139aa1039e7SEric Dumazet 	kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
140aa1039e7SEric Dumazet }
141aa1039e7SEric Dumazet 
1424b9d9be8SEric Dumazet /* perform garbage collect on all items stacked during a lookup */
143*b145425fSEric Dumazet static void inet_peer_gc(struct inet_peer_base *base,
144*b145425fSEric Dumazet 			 struct inet_peer *gc_stack[],
145*b145425fSEric Dumazet 			 unsigned int gc_cnt)
14698158f5aSDavid S. Miller {
147*b145425fSEric Dumazet 	struct inet_peer *p;
1484b9d9be8SEric Dumazet 	__u32 delta, ttl;
149*b145425fSEric Dumazet 	int i;
15098158f5aSDavid S. Miller 
1514b9d9be8SEric Dumazet 	if (base->total >= inet_peer_threshold)
1524b9d9be8SEric Dumazet 		ttl = 0; /* be aggressive */
1534b9d9be8SEric Dumazet 	else
1544b9d9be8SEric Dumazet 		ttl = inet_peer_maxttl
1554b9d9be8SEric Dumazet 				- (inet_peer_maxttl - inet_peer_minttl) / HZ *
1564b9d9be8SEric Dumazet 					base->total / inet_peer_threshold * HZ;
157*b145425fSEric Dumazet 	for (i = 0; i < gc_cnt; i++) {
158*b145425fSEric Dumazet 		p = gc_stack[i];
159d71209deSPavel Emelyanov 		delta = (__u32)jiffies - p->dtime;
160*b145425fSEric Dumazet 		if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
161*b145425fSEric Dumazet 			gc_stack[i] = NULL;
162*b145425fSEric Dumazet 	}
163*b145425fSEric Dumazet 	for (i = 0; i < gc_cnt; i++) {
164*b145425fSEric Dumazet 		p = gc_stack[i];
165*b145425fSEric Dumazet 		if (p) {
166*b145425fSEric Dumazet 			rb_erase(&p->rb_node, &base->rb_root);
167*b145425fSEric Dumazet 			base->total--;
168*b145425fSEric Dumazet 			call_rcu(&p->rcu, inetpeer_free_rcu);
1694b9d9be8SEric Dumazet 		}
1704b9d9be8SEric Dumazet 	}
1716d1a3e04SEric Dumazet }
172d71209deSPavel Emelyanov 
173c0efc887SDavid S. Miller struct inet_peer *inet_getpeer(struct inet_peer_base *base,
174c8a627edSGao feng 			       const struct inetpeer_addr *daddr,
175c8a627edSGao feng 			       int create)
1761da177e4SLinus Torvalds {
177*b145425fSEric Dumazet 	struct inet_peer *p, *gc_stack[PEER_MAX_GC];
178*b145425fSEric Dumazet 	struct rb_node **pp, *parent;
179*b145425fSEric Dumazet 	unsigned int gc_cnt, seq;
180*b145425fSEric Dumazet 	int invalidated;
1811da177e4SLinus Torvalds 
1824b9d9be8SEric Dumazet 	/* Attempt a lockless lookup first.
183aa1039e7SEric Dumazet 	 * Because of a concurrent writer, we might not find an existing entry.
184aa1039e7SEric Dumazet 	 */
1857b46ac4eSDavid S. Miller 	rcu_read_lock();
186*b145425fSEric Dumazet 	seq = read_seqbegin(&base->lock);
187*b145425fSEric Dumazet 	p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
188*b145425fSEric Dumazet 	invalidated = read_seqretry(&base->lock, seq);
1897b46ac4eSDavid S. Miller 	rcu_read_unlock();
1901da177e4SLinus Torvalds 
1914b9d9be8SEric Dumazet 	if (p)
1921da177e4SLinus Torvalds 		return p;
1931da177e4SLinus Torvalds 
19465e8354eSEric Dumazet 	/* If no writer did a change during our lookup, we can return early. */
19565e8354eSEric Dumazet 	if (!create && !invalidated)
19665e8354eSEric Dumazet 		return NULL;
19765e8354eSEric Dumazet 
198aa1039e7SEric Dumazet 	/* retry an exact lookup, taking the lock before.
199aa1039e7SEric Dumazet 	 * At least, nodes should be hot in our cache.
200aa1039e7SEric Dumazet 	 */
201*b145425fSEric Dumazet 	parent = NULL;
20265e8354eSEric Dumazet 	write_seqlock_bh(&base->lock);
203*b145425fSEric Dumazet 
204*b145425fSEric Dumazet 	gc_cnt = 0;
205*b145425fSEric Dumazet 	p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
206*b145425fSEric Dumazet 	if (!p && create) {
207*b145425fSEric Dumazet 		p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
208aa1039e7SEric Dumazet 		if (p) {
209b534ecf1SDavid S. Miller 			p->daddr = *daddr;
2101cc9a98bSReshetova, Elena 			refcount_set(&p->refcnt, 2);
211aa1039e7SEric Dumazet 			atomic_set(&p->rid, 0);
212144001bdSDavid S. Miller 			p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
21392d86829SDavid S. Miller 			p->rate_tokens = 0;
214bc9259a8SNicolas Dichtel 			/* 60*HZ is arbitrary, but chosen enough high so that the first
215bc9259a8SNicolas Dichtel 			 * calculation of tokens is at its maximum.
216bc9259a8SNicolas Dichtel 			 */
217bc9259a8SNicolas Dichtel 			p->rate_last = jiffies - 60*HZ;
2181da177e4SLinus Torvalds 
219*b145425fSEric Dumazet 			rb_link_node(&p->rb_node, parent, pp);
220*b145425fSEric Dumazet 			rb_insert_color(&p->rb_node, &base->rb_root);
22198158f5aSDavid S. Miller 			base->total++;
222aa1039e7SEric Dumazet 		}
223*b145425fSEric Dumazet 	}
224*b145425fSEric Dumazet 	if (gc_cnt)
225*b145425fSEric Dumazet 		inet_peer_gc(base, gc_stack, gc_cnt);
22665e8354eSEric Dumazet 	write_sequnlock_bh(&base->lock);
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	return p;
2291da177e4SLinus Torvalds }
230b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_getpeer);
23198158f5aSDavid S. Miller 
2324663afe2SEric Dumazet void inet_putpeer(struct inet_peer *p)
2334663afe2SEric Dumazet {
2344663afe2SEric Dumazet 	p->dtime = (__u32)jiffies;
235*b145425fSEric Dumazet 
236*b145425fSEric Dumazet 	if (refcount_dec_and_test(&p->refcnt))
237*b145425fSEric Dumazet 		call_rcu(&p->rcu, inetpeer_free_rcu);
2384663afe2SEric Dumazet }
239b3419363SDavid S. Miller EXPORT_SYMBOL_GPL(inet_putpeer);
24092d86829SDavid S. Miller 
24192d86829SDavid S. Miller /*
24292d86829SDavid S. Miller  *	Check transmit rate limitation for given message.
24392d86829SDavid S. Miller  *	The rate information is held in the inet_peer entries now.
24492d86829SDavid S. Miller  *	This function is generic and could be used for other purposes
24592d86829SDavid S. Miller  *	too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
24692d86829SDavid S. Miller  *
24792d86829SDavid S. Miller  *	Note that the same inet_peer fields are modified by functions in
24892d86829SDavid S. Miller  *	route.c too, but these work for packet destinations while xrlim_allow
24992d86829SDavid S. Miller  *	works for icmp destinations. This means the rate limiting information
25092d86829SDavid S. Miller  *	for one "ip object" is shared - and these ICMPs are twice limited:
25192d86829SDavid S. Miller  *	by source and by destination.
25292d86829SDavid S. Miller  *
25392d86829SDavid S. Miller  *	RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
25492d86829SDavid S. Miller  *			  SHOULD allow setting of rate limits
25592d86829SDavid S. Miller  *
25692d86829SDavid S. Miller  * 	Shared between ICMPv4 and ICMPv6.
25792d86829SDavid S. Miller  */
25892d86829SDavid S. Miller #define XRLIM_BURST_FACTOR 6
25992d86829SDavid S. Miller bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
26092d86829SDavid S. Miller {
26192d86829SDavid S. Miller 	unsigned long now, token;
26292d86829SDavid S. Miller 	bool rc = false;
26392d86829SDavid S. Miller 
26492d86829SDavid S. Miller 	if (!peer)
26592d86829SDavid S. Miller 		return true;
26692d86829SDavid S. Miller 
26792d86829SDavid S. Miller 	token = peer->rate_tokens;
26892d86829SDavid S. Miller 	now = jiffies;
26992d86829SDavid S. Miller 	token += now - peer->rate_last;
27092d86829SDavid S. Miller 	peer->rate_last = now;
27192d86829SDavid S. Miller 	if (token > XRLIM_BURST_FACTOR * timeout)
27292d86829SDavid S. Miller 		token = XRLIM_BURST_FACTOR * timeout;
27392d86829SDavid S. Miller 	if (token >= timeout) {
27492d86829SDavid S. Miller 		token -= timeout;
27592d86829SDavid S. Miller 		rc = true;
27692d86829SDavid S. Miller 	}
27792d86829SDavid S. Miller 	peer->rate_tokens = token;
27892d86829SDavid S. Miller 	return rc;
27992d86829SDavid S. Miller }
28092d86829SDavid S. Miller EXPORT_SYMBOL(inet_peer_xrlim_allow);
2815faa5df1SSteffen Klassert 
28256a6b248SDavid S. Miller void inetpeer_invalidate_tree(struct inet_peer_base *base)
2835faa5df1SSteffen Klassert {
284*b145425fSEric Dumazet 	struct inet_peer *p, *n;
2855faa5df1SSteffen Klassert 
286*b145425fSEric Dumazet 	rbtree_postorder_for_each_entry_safe(p, n, &base->rb_root, rb_node) {
287*b145425fSEric Dumazet 		inet_putpeer(p);
288*b145425fSEric Dumazet 		cond_resched();
2895faa5df1SSteffen Klassert 	}
2905faa5df1SSteffen Klassert 
291*b145425fSEric Dumazet 	base->rb_root = RB_ROOT;
292*b145425fSEric Dumazet 	base->total = 0;
2935faa5df1SSteffen Klassert }
2945faa5df1SSteffen Klassert EXPORT_SYMBOL(inetpeer_invalidate_tree);
295