blob: ff327a62c9ce9b1794104c3c924f5f2b9820ac8b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INETPEER - A storage for permanent information about peers
3 *
4 * This source is covered by the GNU GPL, the same as all kernel sources.
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Authors: Andrey V. Savochkin <saw@msu.ru>
7 */
8
Alexey Dobriyan08009a72018-02-24 21:20:33 +03009#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/slab.h>
13#include <linux/interrupt.h>
14#include <linux/spinlock.h>
15#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/timer.h>
17#include <linux/time.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/net.h>
Steffen Klassert5faa5df2012-03-06 21:20:26 +000021#include <linux/workqueue.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030022#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/inetpeer.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070024#include <net/secure_seq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Theory of operations.
28 * We keep one entry for each peer IP address. The nodes contains long-living
29 * information about the peer which doesn't depend on routes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * Nodes are removed only when reference counter goes to 0.
32 * When it's happened the node may be removed when a sufficient amount of
33 * time has been passed since its last use. The less-recently-used entry can
34 * also be removed if the pool is overloaded i.e. if the total amount of
35 * entries is greater-or-equal than the threshold.
36 *
Eric Dumazetb1454252017-07-17 02:56:10 -070037 * Node pool is organised as an RB tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Such an implementation has been chosen not just for fun. It's a way to
39 * prevent easy and efficient DoS attacks by creating hash collisions. A huge
40 * amount of long living nodes in a single hash slot would significantly delay
41 * lookups performed with disabled BHs.
42 *
43 * Serialisation issues.
Eric Dumazetaa1039e2010-06-15 08:23:14 +000044 * 1. Nodes may appear in the tree only with the pool lock held.
45 * 2. Nodes may disappear from the tree only with the pool lock held
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * AND reference count being 0.
Eric Dumazet4b9d9be2011-06-08 13:35:34 +000047 * 3. Global variable peer_total is modified under the pool lock.
48 * 4. struct inet_peer fields modification:
Eric Dumazetb1454252017-07-17 02:56:10 -070049 * rb_node: pool lock
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * refcnt: atomically against modifications on other CPU;
51 * usually under some other lock to prevent node disappearing
David S. Miller582a72d2010-11-30 11:53:55 -080052 * daddr: unchangeable
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
Alexey Dobriyan08009a72018-02-24 21:20:33 +030055static struct kmem_cache *peer_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David S. Millerc3426b42012-06-09 16:27:05 -070057void inet_peer_base_init(struct inet_peer_base *bp)
58{
Eric Dumazetb1454252017-07-17 02:56:10 -070059 bp->rb_root = RB_ROOT;
David S. Millerc3426b42012-06-09 16:27:05 -070060 seqlock_init(&bp->lock);
61 bp->total = 0;
62}
63EXPORT_SYMBOL_GPL(inet_peer_base_init);
David S. Miller021e9292010-11-30 12:12:23 -080064
Eric Dumazetb1454252017-07-17 02:56:10 -070065#define PEER_MAX_GC 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/* Exported for sysctl_net_ipv4. */
Eric Dumazet243bbca2007-03-06 20:23:10 -080068int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * aggressively at this stage */
Eric Dumazet243bbca2007-03-06 20:23:10 -080070int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
71int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Called from ip_output.c:ip_init */
74void __init inet_initpeers(void)
75{
76 struct sysinfo si;
77
78 /* Use the straight interface to information about memory. */
79 si_meminfo(&si);
80 /* The values below were suggested by Alexey Kuznetsov
81 * <kuznet@ms2.inr.ac.ru>. I don't have any opinion about the values
82 * myself. --SAW
83 */
84 if (si.totalram <= (32768*1024)/PAGE_SIZE)
85 inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
86 if (si.totalram <= (16384*1024)/PAGE_SIZE)
87 inet_peer_threshold >>= 1; /* about 512KB */
88 if (si.totalram <= (8192*1024)/PAGE_SIZE)
89 inet_peer_threshold >>= 2; /* about 128KB */
90
91 peer_cachep = kmem_cache_create("inet_peer_cache",
92 sizeof(struct inet_peer),
Eric Dumazet317fe0e2010-06-16 04:52:13 +000093 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +090094 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Eric Dumazetb1454252017-07-17 02:56:10 -070097/* Called with rcu_read_lock() or base->lock held */
98static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
99 struct inet_peer_base *base,
100 unsigned int seq,
101 struct inet_peer *gc_stack[],
102 unsigned int *gc_cnt,
103 struct rb_node **parent_p,
104 struct rb_node ***pp_p)
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000105{
Eric Dumazet4cc5b442017-09-01 14:03:32 -0700106 struct rb_node **pp, *parent, *next;
Eric Dumazetb1454252017-07-17 02:56:10 -0700107 struct inet_peer *p;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000108
Eric Dumazetb1454252017-07-17 02:56:10 -0700109 pp = &base->rb_root.rb_node;
110 parent = NULL;
Eric Dumazet4cc5b442017-09-01 14:03:32 -0700111 while (1) {
Eric Dumazetb1454252017-07-17 02:56:10 -0700112 int cmp;
113
Eric Dumazet4cc5b442017-09-01 14:03:32 -0700114 next = rcu_dereference_raw(*pp);
115 if (!next)
116 break;
117 parent = next;
Eric Dumazetb1454252017-07-17 02:56:10 -0700118 p = rb_entry(parent, struct inet_peer, rb_node);
119 cmp = inetpeer_addr_cmp(daddr, &p->daddr);
David S. Miller02663042010-11-30 12:08:53 -0800120 if (cmp == 0) {
Eric Dumazetb1454252017-07-17 02:56:10 -0700121 if (!refcount_inc_not_zero(&p->refcnt))
122 break;
123 return p;
124 }
125 if (gc_stack) {
126 if (*gc_cnt < PEER_MAX_GC)
127 gc_stack[(*gc_cnt)++] = p;
128 } else if (unlikely(read_seqretry(&base->lock, seq))) {
129 break;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000130 }
David S. Miller02663042010-11-30 12:08:53 -0800131 if (cmp == -1)
Eric Dumazet35f493b2017-09-25 08:40:02 -0700132 pp = &next->rb_left;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000133 else
Eric Dumazet35f493b2017-09-25 08:40:02 -0700134 pp = &next->rb_right;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000135 }
Eric Dumazetb1454252017-07-17 02:56:10 -0700136 *parent_p = parent;
137 *pp_p = pp;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000138 return NULL;
139}
140
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000141static void inetpeer_free_rcu(struct rcu_head *head)
142{
143 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
144}
145
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000146/* perform garbage collect on all items stacked during a lookup */
Eric Dumazetb1454252017-07-17 02:56:10 -0700147static void inet_peer_gc(struct inet_peer_base *base,
148 struct inet_peer *gc_stack[],
149 unsigned int gc_cnt)
David S. Miller98158f52010-11-30 11:41:59 -0800150{
Eric Dumazetb1454252017-07-17 02:56:10 -0700151 struct inet_peer *p;
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000152 __u32 delta, ttl;
Eric Dumazetb1454252017-07-17 02:56:10 -0700153 int i;
David S. Miller98158f52010-11-30 11:41:59 -0800154
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000155 if (base->total >= inet_peer_threshold)
156 ttl = 0; /* be aggressive */
157 else
158 ttl = inet_peer_maxttl
159 - (inet_peer_maxttl - inet_peer_minttl) / HZ *
160 base->total / inet_peer_threshold * HZ;
Eric Dumazetb1454252017-07-17 02:56:10 -0700161 for (i = 0; i < gc_cnt; i++) {
162 p = gc_stack[i];
Eric Dumazet71685eb2019-11-07 10:30:42 -0800163
164 /* The READ_ONCE() pairs with the WRITE_ONCE()
165 * in inet_putpeer()
166 */
167 delta = (__u32)jiffies - READ_ONCE(p->dtime);
168
Eric Dumazetb1454252017-07-17 02:56:10 -0700169 if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
170 gc_stack[i] = NULL;
171 }
172 for (i = 0; i < gc_cnt; i++) {
173 p = gc_stack[i];
174 if (p) {
175 rb_erase(&p->rb_node, &base->rb_root);
176 base->total--;
177 call_rcu(&p->rcu, inetpeer_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
David S. Millerc0efc882012-06-09 19:12:36 -0700182struct inet_peer *inet_getpeer(struct inet_peer_base *base,
Gao fengc8a627e2012-06-08 01:20:41 +0000183 const struct inetpeer_addr *daddr,
184 int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Eric Dumazetb1454252017-07-17 02:56:10 -0700186 struct inet_peer *p, *gc_stack[PEER_MAX_GC];
187 struct rb_node **pp, *parent;
188 unsigned int gc_cnt, seq;
189 int invalidated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000191 /* Attempt a lockless lookup first.
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000192 * Because of a concurrent writer, we might not find an existing entry.
193 */
David S. Miller7b46ac42011-03-08 14:59:28 -0800194 rcu_read_lock();
Eric Dumazetb1454252017-07-17 02:56:10 -0700195 seq = read_seqbegin(&base->lock);
196 p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
197 invalidated = read_seqretry(&base->lock, seq);
David S. Miller7b46ac42011-03-08 14:59:28 -0800198 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000200 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Eric Dumazet65e83542011-03-04 14:33:59 -0800203 /* If no writer did a change during our lookup, we can return early. */
204 if (!create && !invalidated)
205 return NULL;
206
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000207 /* retry an exact lookup, taking the lock before.
208 * At least, nodes should be hot in our cache.
209 */
Eric Dumazetb1454252017-07-17 02:56:10 -0700210 parent = NULL;
Eric Dumazet65e83542011-03-04 14:33:59 -0800211 write_seqlock_bh(&base->lock);
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000212
Eric Dumazetb1454252017-07-17 02:56:10 -0700213 gc_cnt = 0;
214 p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
215 if (!p && create) {
216 p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
217 if (p) {
218 p->daddr = *daddr;
Eric Dumazetb6a37e52018-04-09 06:43:27 -0700219 p->dtime = (__u32)jiffies;
Eric Dumazetb1454252017-07-17 02:56:10 -0700220 refcount_set(&p->refcnt, 2);
221 atomic_set(&p->rid, 0);
222 p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
223 p->rate_tokens = 0;
Lorenzo Bianconic09551c62019-02-06 19:18:04 +0100224 p->n_redirects = 0;
Eric Dumazetb1454252017-07-17 02:56:10 -0700225 /* 60*HZ is arbitrary, but chosen enough high so that the first
226 * calculation of tokens is at its maximum.
227 */
228 p->rate_last = jiffies - 60*HZ;
229
230 rb_link_node(&p->rb_node, parent, pp);
231 rb_insert_color(&p->rb_node, &base->rb_root);
232 base->total++;
233 }
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000234 }
Eric Dumazetb1454252017-07-17 02:56:10 -0700235 if (gc_cnt)
236 inet_peer_gc(base, gc_stack, gc_cnt);
Eric Dumazet65e83542011-03-04 14:33:59 -0800237 write_sequnlock_bh(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return p;
240}
David S. Millerb3419362010-11-30 12:27:11 -0800241EXPORT_SYMBOL_GPL(inet_getpeer);
David S. Miller98158f52010-11-30 11:41:59 -0800242
Eric Dumazet4663afe2006-10-12 21:21:06 -0700243void inet_putpeer(struct inet_peer *p)
244{
Eric Dumazet71685eb2019-11-07 10:30:42 -0800245 /* The WRITE_ONCE() pairs with itself (we run lockless)
246 * and the READ_ONCE() in inet_peer_gc()
247 */
248 WRITE_ONCE(p->dtime, (__u32)jiffies);
Eric Dumazetb1454252017-07-17 02:56:10 -0700249
250 if (refcount_dec_and_test(&p->refcnt))
251 call_rcu(&p->rcu, inetpeer_free_rcu);
Eric Dumazet4663afe2006-10-12 21:21:06 -0700252}
David S. Millerb3419362010-11-30 12:27:11 -0800253EXPORT_SYMBOL_GPL(inet_putpeer);
David S. Miller92d86822011-02-04 15:55:25 -0800254
255/*
256 * Check transmit rate limitation for given message.
257 * The rate information is held in the inet_peer entries now.
258 * This function is generic and could be used for other purposes
259 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
260 *
261 * Note that the same inet_peer fields are modified by functions in
262 * route.c too, but these work for packet destinations while xrlim_allow
263 * works for icmp destinations. This means the rate limiting information
264 * for one "ip object" is shared - and these ICMPs are twice limited:
265 * by source and by destination.
266 *
267 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
268 * SHOULD allow setting of rate limits
269 *
270 * Shared between ICMPv4 and ICMPv6.
271 */
272#define XRLIM_BURST_FACTOR 6
273bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
274{
275 unsigned long now, token;
276 bool rc = false;
277
278 if (!peer)
279 return true;
280
281 token = peer->rate_tokens;
282 now = jiffies;
283 token += now - peer->rate_last;
284 peer->rate_last = now;
285 if (token > XRLIM_BURST_FACTOR * timeout)
286 token = XRLIM_BURST_FACTOR * timeout;
287 if (token >= timeout) {
288 token -= timeout;
289 rc = true;
290 }
291 peer->rate_tokens = token;
292 return rc;
293}
294EXPORT_SYMBOL(inet_peer_xrlim_allow);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000295
David S. Miller56a6b242012-06-09 16:32:41 -0700296void inetpeer_invalidate_tree(struct inet_peer_base *base)
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000297{
Eric Dumazet8f1975e2017-09-25 09:14:14 -0700298 struct rb_node *p = rb_first(&base->rb_root);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000299
Eric Dumazet8f1975e2017-09-25 09:14:14 -0700300 while (p) {
301 struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
302
303 p = rb_next(p);
304 rb_erase(&peer->rb_node, &base->rb_root);
305 inet_putpeer(peer);
Eric Dumazetb1454252017-07-17 02:56:10 -0700306 cond_resched();
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000307 }
308
Eric Dumazetb1454252017-07-17 02:56:10 -0700309 base->total = 0;
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000310}
311EXPORT_SYMBOL(inetpeer_invalidate_tree);