]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/inetpeer.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / inetpeer.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
6 * Authors: Andrey V. Savochkin <saw@msu.ru>
7 */
8
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/interrupt.h>
13#include <linux/spinlock.h>
14#include <linux/random.h>
1da177e4
LT
15#include <linux/timer.h>
16#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/net.h>
5faa5df1 20#include <linux/workqueue.h>
20380731 21#include <net/ip.h>
1da177e4 22#include <net/inetpeer.h>
6e5714ea 23#include <net/secure_seq.h>
1da177e4
LT
24
25/*
26 * Theory of operations.
27 * We keep one entry for each peer IP address. The nodes contains long-living
28 * information about the peer which doesn't depend on routes.
1da177e4 29 *
1da177e4
LT
30 * Nodes are removed only when reference counter goes to 0.
31 * When it's happened the node may be removed when a sufficient amount of
32 * time has been passed since its last use. The less-recently-used entry can
33 * also be removed if the pool is overloaded i.e. if the total amount of
34 * entries is greater-or-equal than the threshold.
35 *
b145425f 36 * Node pool is organised as an RB tree.
1da177e4
LT
37 * Such an implementation has been chosen not just for fun. It's a way to
38 * prevent easy and efficient DoS attacks by creating hash collisions. A huge
39 * amount of long living nodes in a single hash slot would significantly delay
40 * lookups performed with disabled BHs.
41 *
42 * Serialisation issues.
aa1039e7
ED
43 * 1. Nodes may appear in the tree only with the pool lock held.
44 * 2. Nodes may disappear from the tree only with the pool lock held
1da177e4 45 * AND reference count being 0.
4b9d9be8
ED
46 * 3. Global variable peer_total is modified under the pool lock.
47 * 4. struct inet_peer fields modification:
b145425f 48 * rb_node: pool lock
1da177e4
LT
49 * refcnt: atomically against modifications on other CPU;
50 * usually under some other lock to prevent node disappearing
582a72da 51 * daddr: unchangeable
1da177e4
LT
52 */
53
e18b890b 54static struct kmem_cache *peer_cachep __read_mostly;
1da177e4 55
c3426b47
DM
56void inet_peer_base_init(struct inet_peer_base *bp)
57{
b145425f 58 bp->rb_root = RB_ROOT;
c3426b47
DM
59 seqlock_init(&bp->lock);
60 bp->total = 0;
61}
62EXPORT_SYMBOL_GPL(inet_peer_base_init);
021e9299 63
b145425f 64#define PEER_MAX_GC 32
1da177e4 65
1da177e4 66/* Exported for sysctl_net_ipv4. */
243bbcaa 67int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more
1da177e4 68 * aggressively at this stage */
243bbcaa
ED
69int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */
70int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */
1da177e4 71
1da177e4
LT
72/* Called from ip_output.c:ip_init */
73void __init inet_initpeers(void)
74{
75 struct sysinfo si;
76
77 /* Use the straight interface to information about memory. */
78 si_meminfo(&si);
79 /* The values below were suggested by Alexey Kuznetsov
80 * <kuznet@ms2.inr.ac.ru>. I don't have any opinion about the values
81 * myself. --SAW
82 */
83 if (si.totalram <= (32768*1024)/PAGE_SIZE)
84 inet_peer_threshold >>= 1; /* max pool size about 1MB on IA32 */
85 if (si.totalram <= (16384*1024)/PAGE_SIZE)
86 inet_peer_threshold >>= 1; /* about 512KB */
87 if (si.totalram <= (8192*1024)/PAGE_SIZE)
88 inet_peer_threshold >>= 2; /* about 128KB */
89
90 peer_cachep = kmem_cache_create("inet_peer_cache",
91 sizeof(struct inet_peer),
317fe0e6 92 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
20c2df83 93 NULL);
1da177e4
LT
94}
95
b145425f
ED
96/* Called with rcu_read_lock() or base->lock held */
97static struct inet_peer *lookup(const struct inetpeer_addr *daddr,
98 struct inet_peer_base *base,
99 unsigned int seq,
100 struct inet_peer *gc_stack[],
101 unsigned int *gc_cnt,
102 struct rb_node **parent_p,
103 struct rb_node ***pp_p)
aa1039e7 104{
4cc5b44b 105 struct rb_node **pp, *parent, *next;
b145425f
ED
106 struct inet_peer *p;
107
108 pp = &base->rb_root.rb_node;
109 parent = NULL;
4cc5b44b 110 while (1) {
b145425f 111 int cmp;
aa1039e7 112
4cc5b44b
ED
113 next = rcu_dereference_raw(*pp);
114 if (!next)
115 break;
116 parent = next;
b145425f
ED
117 p = rb_entry(parent, struct inet_peer, rb_node);
118 cmp = inetpeer_addr_cmp(daddr, &p->daddr);
02663045 119 if (cmp == 0) {
b145425f
ED
120 if (!refcount_inc_not_zero(&p->refcnt))
121 break;
122 return p;
123 }
124 if (gc_stack) {
125 if (*gc_cnt < PEER_MAX_GC)
126 gc_stack[(*gc_cnt)++] = p;
127 } else if (unlikely(read_seqretry(&base->lock, seq))) {
128 break;
aa1039e7 129 }
02663045 130 if (cmp == -1)
35f493b8 131 pp = &next->rb_left;
aa1039e7 132 else
35f493b8 133 pp = &next->rb_right;
aa1039e7 134 }
b145425f
ED
135 *parent_p = parent;
136 *pp_p = pp;
aa1039e7
ED
137 return NULL;
138}
139
aa1039e7
ED
140static void inetpeer_free_rcu(struct rcu_head *head)
141{
142 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
143}
144
4b9d9be8 145/* perform garbage collect on all items stacked during a lookup */
b145425f
ED
146static void inet_peer_gc(struct inet_peer_base *base,
147 struct inet_peer *gc_stack[],
148 unsigned int gc_cnt)
98158f5a 149{
b145425f 150 struct inet_peer *p;
4b9d9be8 151 __u32 delta, ttl;
b145425f 152 int i;
d71209de 153
4b9d9be8
ED
154 if (base->total >= inet_peer_threshold)
155 ttl = 0; /* be aggressive */
156 else
157 ttl = inet_peer_maxttl
158 - (inet_peer_maxttl - inet_peer_minttl) / HZ *
159 base->total / inet_peer_threshold * HZ;
b145425f
ED
160 for (i = 0; i < gc_cnt; i++) {
161 p = gc_stack[i];
c48b0f7d
ED
162
163 /* The READ_ONCE() pairs with the WRITE_ONCE()
164 * in inet_putpeer()
165 */
166 delta = (__u32)jiffies - READ_ONCE(p->dtime);
167
b145425f
ED
168 if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
169 gc_stack[i] = NULL;
1da177e4 170 }
b145425f
ED
171 for (i = 0; i < gc_cnt; i++) {
172 p = gc_stack[i];
173 if (p) {
174 rb_erase(&p->rb_node, &base->rb_root);
175 base->total--;
176 call_rcu(&p->rcu, inetpeer_free_rcu);
177 }
4b9d9be8 178 }
1da177e4
LT
179}
180
c0efc887 181struct inet_peer *inet_getpeer(struct inet_peer_base *base,
c8a627ed
G
182 const struct inetpeer_addr *daddr,
183 int create)
1da177e4 184{
b145425f
ED
185 struct inet_peer *p, *gc_stack[PEER_MAX_GC];
186 struct rb_node **pp, *parent;
187 unsigned int gc_cnt, seq;
188 int invalidated;
1da177e4 189
4b9d9be8 190 /* Attempt a lockless lookup first.
aa1039e7
ED
191 * Because of a concurrent writer, we might not find an existing entry.
192 */
7b46ac4e 193 rcu_read_lock();
b145425f
ED
194 seq = read_seqbegin(&base->lock);
195 p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
196 invalidated = read_seqretry(&base->lock, seq);
7b46ac4e 197 rcu_read_unlock();
aa1039e7 198
4b9d9be8 199 if (p)
aa1039e7 200 return p;
1da177e4 201
65e8354e
ED
202 /* If no writer did a change during our lookup, we can return early. */
203 if (!create && !invalidated)
204 return NULL;
205
aa1039e7
ED
206 /* retry an exact lookup, taking the lock before.
207 * At least, nodes should be hot in our cache.
208 */
b145425f 209 parent = NULL;
65e8354e 210 write_seqlock_bh(&base->lock);
aa1039e7 211
b145425f
ED
212 gc_cnt = 0;
213 p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
214 if (!p && create) {
215 p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
216 if (p) {
217 p->daddr = *daddr;
73ff6fa7 218 p->dtime = (__u32)jiffies;
b145425f
ED
219 refcount_set(&p->refcnt, 2);
220 atomic_set(&p->rid, 0);
221 p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
222 p->rate_tokens = 0;
9ce92d57 223 p->n_redirects = 0;
b145425f
ED
224 /* 60*HZ is arbitrary, but chosen enough high so that the first
225 * calculation of tokens is at its maximum.
226 */
227 p->rate_last = jiffies - 60*HZ;
228
229 rb_link_node(&p->rb_node, parent, pp);
230 rb_insert_color(&p->rb_node, &base->rb_root);
231 base->total++;
232 }
aa1039e7 233 }
b145425f
ED
234 if (gc_cnt)
235 inet_peer_gc(base, gc_stack, gc_cnt);
65e8354e 236 write_sequnlock_bh(&base->lock);
1da177e4 237
1da177e4
LT
238 return p;
239}
b3419363 240EXPORT_SYMBOL_GPL(inet_getpeer);
98158f5a 241
4663afe2
ED
242void inet_putpeer(struct inet_peer *p)
243{
c48b0f7d
ED
244 /* The WRITE_ONCE() pairs with itself (we run lockless)
245 * and the READ_ONCE() in inet_peer_gc()
246 */
247 WRITE_ONCE(p->dtime, (__u32)jiffies);
b145425f
ED
248
249 if (refcount_dec_and_test(&p->refcnt))
250 call_rcu(&p->rcu, inetpeer_free_rcu);
4663afe2 251}
b3419363 252EXPORT_SYMBOL_GPL(inet_putpeer);
92d86829
DM
253
254/*
255 * Check transmit rate limitation for given message.
256 * The rate information is held in the inet_peer entries now.
257 * This function is generic and could be used for other purposes
258 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
259 *
260 * Note that the same inet_peer fields are modified by functions in
261 * route.c too, but these work for packet destinations while xrlim_allow
262 * works for icmp destinations. This means the rate limiting information
263 * for one "ip object" is shared - and these ICMPs are twice limited:
264 * by source and by destination.
265 *
266 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
267 * SHOULD allow setting of rate limits
268 *
269 * Shared between ICMPv4 and ICMPv6.
270 */
271#define XRLIM_BURST_FACTOR 6
272bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
273{
274 unsigned long now, token;
275 bool rc = false;
276
277 if (!peer)
278 return true;
279
280 token = peer->rate_tokens;
281 now = jiffies;
282 token += now - peer->rate_last;
283 peer->rate_last = now;
284 if (token > XRLIM_BURST_FACTOR * timeout)
285 token = XRLIM_BURST_FACTOR * timeout;
286 if (token >= timeout) {
287 token -= timeout;
288 rc = true;
289 }
290 peer->rate_tokens = token;
291 return rc;
292}
293EXPORT_SYMBOL(inet_peer_xrlim_allow);
5faa5df1 294
56a6b248 295void inetpeer_invalidate_tree(struct inet_peer_base *base)
5faa5df1 296{
8f1975e3 297 struct rb_node *p = rb_first(&base->rb_root);
5faa5df1 298
8f1975e3
ED
299 while (p) {
300 struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
301
302 p = rb_next(p);
303 rb_erase(&peer->rb_node, &base->rb_root);
304 inet_putpeer(peer);
b145425f 305 cond_resched();
5faa5df1
SK
306 }
307
b145425f 308 base->total = 0;
5faa5df1
SK
309}
310EXPORT_SYMBOL(inetpeer_invalidate_tree);