blob: da21dfce24d7390bdc51899e03a0c7b3efbc3cec [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. */
Yejune Deng8bd2a052021-03-01 14:05:48 +080068int inet_peer_threshold __read_mostly; /* 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{
Yejune Deng8bd2a052021-03-01 14:05:48 +080076 u64 nr_entries;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Yejune Deng8bd2a052021-03-01 14:05:48 +080078 /* 1% of physical memory */
79 nr_entries = div64_ul((u64)totalram_pages() << PAGE_SHIFT,
80 100 * L1_CACHE_ALIGN(sizeof(struct inet_peer)));
81
82 inet_peer_threshold = clamp_val(nr_entries, 4096, 65536 + 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 peer_cachep = kmem_cache_create("inet_peer_cache",
85 sizeof(struct inet_peer),
Eric Dumazet317fe0e2010-06-16 04:52:13 +000086 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +090087 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Eric Dumazetb1454252017-07-17 02:56:10 -070090/* Called with rcu_read_lock() or base->lock held */
91static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
92 struct inet_peer_base *base,
93 unsigned int seq,
94 struct inet_peer *gc_stack[],
95 unsigned int *gc_cnt,
96 struct rb_node **parent_p,
97 struct rb_node ***pp_p)
Eric Dumazetaa1039e2010-06-15 08:23:14 +000098{
Eric Dumazet4cc5b442017-09-01 14:03:32 -070099 struct rb_node **pp, *parent, *next;
Eric Dumazetb1454252017-07-17 02:56:10 -0700100 struct inet_peer *p;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000101
Eric Dumazetb1454252017-07-17 02:56:10 -0700102 pp = &base->rb_root.rb_node;
103 parent = NULL;
Eric Dumazet4cc5b442017-09-01 14:03:32 -0700104 while (1) {
Eric Dumazetb1454252017-07-17 02:56:10 -0700105 int cmp;
106
Eric Dumazet4cc5b442017-09-01 14:03:32 -0700107 next = rcu_dereference_raw(*pp);
108 if (!next)
109 break;
110 parent = next;
Eric Dumazetb1454252017-07-17 02:56:10 -0700111 p = rb_entry(parent, struct inet_peer, rb_node);
112 cmp = inetpeer_addr_cmp(daddr, &p->daddr);
David S. Miller02663042010-11-30 12:08:53 -0800113 if (cmp == 0) {
Eric Dumazetb1454252017-07-17 02:56:10 -0700114 if (!refcount_inc_not_zero(&p->refcnt))
115 break;
116 return p;
117 }
118 if (gc_stack) {
119 if (*gc_cnt < PEER_MAX_GC)
120 gc_stack[(*gc_cnt)++] = p;
121 } else if (unlikely(read_seqretry(&base->lock, seq))) {
122 break;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000123 }
David S. Miller02663042010-11-30 12:08:53 -0800124 if (cmp == -1)
Eric Dumazet35f493b2017-09-25 08:40:02 -0700125 pp = &next->rb_left;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000126 else
Eric Dumazet35f493b2017-09-25 08:40:02 -0700127 pp = &next->rb_right;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000128 }
Eric Dumazetb1454252017-07-17 02:56:10 -0700129 *parent_p = parent;
130 *pp_p = pp;
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000131 return NULL;
132}
133
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000134static void inetpeer_free_rcu(struct rcu_head *head)
135{
136 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
137}
138
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000139/* perform garbage collect on all items stacked during a lookup */
Eric Dumazetb1454252017-07-17 02:56:10 -0700140static void inet_peer_gc(struct inet_peer_base *base,
141 struct inet_peer *gc_stack[],
142 unsigned int gc_cnt)
David S. Miller98158f52010-11-30 11:41:59 -0800143{
Eric Dumazetb1454252017-07-17 02:56:10 -0700144 struct inet_peer *p;
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000145 __u32 delta, ttl;
Eric Dumazetb1454252017-07-17 02:56:10 -0700146 int i;
David S. Miller98158f52010-11-30 11:41:59 -0800147
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000148 if (base->total >= inet_peer_threshold)
149 ttl = 0; /* be aggressive */
150 else
151 ttl = inet_peer_maxttl
152 - (inet_peer_maxttl - inet_peer_minttl) / HZ *
153 base->total / inet_peer_threshold * HZ;
Eric Dumazetb1454252017-07-17 02:56:10 -0700154 for (i = 0; i < gc_cnt; i++) {
155 p = gc_stack[i];
Eric Dumazet71685eb2019-11-07 10:30:42 -0800156
157 /* The READ_ONCE() pairs with the WRITE_ONCE()
158 * in inet_putpeer()
159 */
160 delta = (__u32)jiffies - READ_ONCE(p->dtime);
161
Eric Dumazetb1454252017-07-17 02:56:10 -0700162 if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
163 gc_stack[i] = NULL;
164 }
165 for (i = 0; i < gc_cnt; i++) {
166 p = gc_stack[i];
167 if (p) {
168 rb_erase(&p->rb_node, &base->rb_root);
169 base->total--;
170 call_rcu(&p->rcu, inetpeer_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
David S. Millerc0efc882012-06-09 19:12:36 -0700175struct inet_peer *inet_getpeer(struct inet_peer_base *base,
Gao fengc8a627e2012-06-08 01:20:41 +0000176 const struct inetpeer_addr *daddr,
177 int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Eric Dumazetb1454252017-07-17 02:56:10 -0700179 struct inet_peer *p, *gc_stack[PEER_MAX_GC];
180 struct rb_node **pp, *parent;
181 unsigned int gc_cnt, seq;
182 int invalidated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000184 /* Attempt a lockless lookup first.
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000185 * Because of a concurrent writer, we might not find an existing entry.
186 */
David S. Miller7b46ac42011-03-08 14:59:28 -0800187 rcu_read_lock();
Eric Dumazetb1454252017-07-17 02:56:10 -0700188 seq = read_seqbegin(&base->lock);
189 p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
190 invalidated = read_seqretry(&base->lock, seq);
David S. Miller7b46ac42011-03-08 14:59:28 -0800191 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric Dumazet4b9d9be2011-06-08 13:35:34 +0000193 if (p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Eric Dumazet65e83542011-03-04 14:33:59 -0800196 /* If no writer did a change during our lookup, we can return early. */
197 if (!create && !invalidated)
198 return NULL;
199
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000200 /* retry an exact lookup, taking the lock before.
201 * At least, nodes should be hot in our cache.
202 */
Eric Dumazetb1454252017-07-17 02:56:10 -0700203 parent = NULL;
Eric Dumazet65e83542011-03-04 14:33:59 -0800204 write_seqlock_bh(&base->lock);
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000205
Eric Dumazetb1454252017-07-17 02:56:10 -0700206 gc_cnt = 0;
207 p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
208 if (!p && create) {
209 p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
210 if (p) {
211 p->daddr = *daddr;
Eric Dumazetb6a37e52018-04-09 06:43:27 -0700212 p->dtime = (__u32)jiffies;
Eric Dumazetb1454252017-07-17 02:56:10 -0700213 refcount_set(&p->refcnt, 2);
214 atomic_set(&p->rid, 0);
215 p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
216 p->rate_tokens = 0;
Lorenzo Bianconic09551c62019-02-06 19:18:04 +0100217 p->n_redirects = 0;
Eric Dumazetb1454252017-07-17 02:56:10 -0700218 /* 60*HZ is arbitrary, but chosen enough high so that the first
219 * calculation of tokens is at its maximum.
220 */
221 p->rate_last = jiffies - 60*HZ;
222
223 rb_link_node(&p->rb_node, parent, pp);
224 rb_insert_color(&p->rb_node, &base->rb_root);
225 base->total++;
226 }
Eric Dumazetaa1039e2010-06-15 08:23:14 +0000227 }
Eric Dumazetb1454252017-07-17 02:56:10 -0700228 if (gc_cnt)
229 inet_peer_gc(base, gc_stack, gc_cnt);
Eric Dumazet65e83542011-03-04 14:33:59 -0800230 write_sequnlock_bh(&base->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return p;
233}
David S. Millerb3419362010-11-30 12:27:11 -0800234EXPORT_SYMBOL_GPL(inet_getpeer);
David S. Miller98158f52010-11-30 11:41:59 -0800235
Eric Dumazet4663afe2006-10-12 21:21:06 -0700236void inet_putpeer(struct inet_peer *p)
237{
Eric Dumazet71685eb2019-11-07 10:30:42 -0800238 /* The WRITE_ONCE() pairs with itself (we run lockless)
239 * and the READ_ONCE() in inet_peer_gc()
240 */
241 WRITE_ONCE(p->dtime, (__u32)jiffies);
Eric Dumazetb1454252017-07-17 02:56:10 -0700242
243 if (refcount_dec_and_test(&p->refcnt))
244 call_rcu(&p->rcu, inetpeer_free_rcu);
Eric Dumazet4663afe2006-10-12 21:21:06 -0700245}
David S. Millerb3419362010-11-30 12:27:11 -0800246EXPORT_SYMBOL_GPL(inet_putpeer);
David S. Miller92d86822011-02-04 15:55:25 -0800247
248/*
249 * Check transmit rate limitation for given message.
250 * The rate information is held in the inet_peer entries now.
251 * This function is generic and could be used for other purposes
252 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
253 *
254 * Note that the same inet_peer fields are modified by functions in
255 * route.c too, but these work for packet destinations while xrlim_allow
256 * works for icmp destinations. This means the rate limiting information
257 * for one "ip object" is shared - and these ICMPs are twice limited:
258 * by source and by destination.
259 *
260 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
261 * SHOULD allow setting of rate limits
262 *
263 * Shared between ICMPv4 and ICMPv6.
264 */
265#define XRLIM_BURST_FACTOR 6
266bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
267{
268 unsigned long now, token;
269 bool rc = false;
270
271 if (!peer)
272 return true;
273
274 token = peer->rate_tokens;
275 now = jiffies;
276 token += now - peer->rate_last;
277 peer->rate_last = now;
278 if (token > XRLIM_BURST_FACTOR * timeout)
279 token = XRLIM_BURST_FACTOR * timeout;
280 if (token >= timeout) {
281 token -= timeout;
282 rc = true;
283 }
284 peer->rate_tokens = token;
285 return rc;
286}
287EXPORT_SYMBOL(inet_peer_xrlim_allow);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000288
David S. Miller56a6b242012-06-09 16:32:41 -0700289void inetpeer_invalidate_tree(struct inet_peer_base *base)
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000290{
Eric Dumazet8f1975e2017-09-25 09:14:14 -0700291 struct rb_node *p = rb_first(&base->rb_root);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000292
Eric Dumazet8f1975e2017-09-25 09:14:14 -0700293 while (p) {
294 struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
295
296 p = rb_next(p);
297 rb_erase(&peer->rb_node, &base->rb_root);
298 inet_putpeer(peer);
Eric Dumazetb1454252017-07-17 02:56:10 -0700299 cond_resched();
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000300 }
301
Eric Dumazetb1454252017-07-17 02:56:10 -0700302 base->total = 0;
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000303}
304EXPORT_SYMBOL(inetpeer_invalidate_tree);