Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Steffen Klassert | 5faa5df | 2012-03-06 21:20:26 +0000 | [diff] [blame] | 20 | #include <linux/workqueue.h> |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 21 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <net/inetpeer.h> |
David S. Miller | 6e5714e | 2011-08-03 20:50:44 -0700 | [diff] [blame] | 23 | #include <net/secure_seq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 36 | * Node pool is organised as an RB tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * AND reference count being 0. |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 46 | * 3. Global variable peer_total is modified under the pool lock. |
| 47 | * 4. struct inet_peer fields modification: |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 48 | * rb_node: pool lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | * refcnt: atomically against modifications on other CPU; |
| 50 | * usually under some other lock to prevent node disappearing |
David S. Miller | 582a72d | 2010-11-30 11:53:55 -0800 | [diff] [blame] | 51 | * daddr: unchangeable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | */ |
| 53 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 54 | static struct kmem_cache *peer_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 56 | void inet_peer_base_init(struct inet_peer_base *bp) |
| 57 | { |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 58 | bp->rb_root = RB_ROOT; |
David S. Miller | c3426b4 | 2012-06-09 16:27:05 -0700 | [diff] [blame] | 59 | seqlock_init(&bp->lock); |
| 60 | bp->total = 0; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(inet_peer_base_init); |
David S. Miller | 021e929 | 2010-11-30 12:12:23 -0800 | [diff] [blame] | 63 | |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 64 | #define PEER_MAX_GC 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* Exported for sysctl_net_ipv4. */ |
Eric Dumazet | 243bbca | 2007-03-06 20:23:10 -0800 | [diff] [blame] | 67 | int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | * aggressively at this stage */ |
Eric Dumazet | 243bbca | 2007-03-06 20:23:10 -0800 | [diff] [blame] | 69 | int inet_peer_minttl __read_mostly = 120 * HZ; /* TTL under high load: 120 sec */ |
| 70 | int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* Called from ip_output.c:ip_init */ |
| 73 | void __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), |
Eric Dumazet | 317fe0e | 2010-06-16 04:52:13 +0000 | [diff] [blame] | 92 | 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 93 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 96 | /* Called with rcu_read_lock() or base->lock held */ |
| 97 | static 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) |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 104 | { |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 105 | struct rb_node **pp, *parent; |
| 106 | struct inet_peer *p; |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 107 | |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 108 | pp = &base->rb_root.rb_node; |
| 109 | parent = NULL; |
| 110 | while (*pp) { |
| 111 | int cmp; |
| 112 | |
| 113 | parent = rcu_dereference_raw(*pp); |
| 114 | p = rb_entry(parent, struct inet_peer, rb_node); |
| 115 | cmp = inetpeer_addr_cmp(daddr, &p->daddr); |
David S. Miller | 0266304 | 2010-11-30 12:08:53 -0800 | [diff] [blame] | 116 | if (cmp == 0) { |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 117 | if (!refcount_inc_not_zero(&p->refcnt)) |
| 118 | break; |
| 119 | return p; |
| 120 | } |
| 121 | if (gc_stack) { |
| 122 | if (*gc_cnt < PEER_MAX_GC) |
| 123 | gc_stack[(*gc_cnt)++] = p; |
| 124 | } else if (unlikely(read_seqretry(&base->lock, seq))) { |
| 125 | break; |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 126 | } |
David S. Miller | 0266304 | 2010-11-30 12:08:53 -0800 | [diff] [blame] | 127 | if (cmp == -1) |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 128 | pp = &(*pp)->rb_left; |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 129 | else |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 130 | pp = &(*pp)->rb_right; |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 131 | } |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 132 | *parent_p = parent; |
| 133 | *pp_p = pp; |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
| 136 | |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 137 | static void inetpeer_free_rcu(struct rcu_head *head) |
| 138 | { |
| 139 | kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu)); |
| 140 | } |
| 141 | |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 142 | /* perform garbage collect on all items stacked during a lookup */ |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 143 | static void inet_peer_gc(struct inet_peer_base *base, |
| 144 | struct inet_peer *gc_stack[], |
| 145 | unsigned int gc_cnt) |
David S. Miller | 98158f5 | 2010-11-30 11:41:59 -0800 | [diff] [blame] | 146 | { |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 147 | struct inet_peer *p; |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 148 | __u32 delta, ttl; |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 149 | int i; |
David S. Miller | 98158f5 | 2010-11-30 11:41:59 -0800 | [diff] [blame] | 150 | |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 151 | if (base->total >= inet_peer_threshold) |
| 152 | ttl = 0; /* be aggressive */ |
| 153 | else |
| 154 | ttl = inet_peer_maxttl |
| 155 | - (inet_peer_maxttl - inet_peer_minttl) / HZ * |
| 156 | base->total / inet_peer_threshold * HZ; |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 157 | for (i = 0; i < gc_cnt; i++) { |
| 158 | p = gc_stack[i]; |
| 159 | delta = (__u32)jiffies - p->dtime; |
| 160 | if (delta < ttl || !refcount_dec_if_one(&p->refcnt)) |
| 161 | gc_stack[i] = NULL; |
| 162 | } |
| 163 | for (i = 0; i < gc_cnt; i++) { |
| 164 | p = gc_stack[i]; |
| 165 | if (p) { |
| 166 | rb_erase(&p->rb_node, &base->rb_root); |
| 167 | base->total--; |
| 168 | call_rcu(&p->rcu, inetpeer_free_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
David S. Miller | c0efc88 | 2012-06-09 19:12:36 -0700 | [diff] [blame] | 173 | struct inet_peer *inet_getpeer(struct inet_peer_base *base, |
Gao feng | c8a627e | 2012-06-08 01:20:41 +0000 | [diff] [blame] | 174 | const struct inetpeer_addr *daddr, |
| 175 | int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 177 | struct inet_peer *p, *gc_stack[PEER_MAX_GC]; |
| 178 | struct rb_node **pp, *parent; |
| 179 | unsigned int gc_cnt, seq; |
| 180 | int invalidated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 182 | /* Attempt a lockless lookup first. |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 183 | * Because of a concurrent writer, we might not find an existing entry. |
| 184 | */ |
David S. Miller | 7b46ac4 | 2011-03-08 14:59:28 -0800 | [diff] [blame] | 185 | rcu_read_lock(); |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 186 | seq = read_seqbegin(&base->lock); |
| 187 | p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp); |
| 188 | invalidated = read_seqretry(&base->lock, seq); |
David S. Miller | 7b46ac4 | 2011-03-08 14:59:28 -0800 | [diff] [blame] | 189 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 191 | if (p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Eric Dumazet | 65e8354 | 2011-03-04 14:33:59 -0800 | [diff] [blame] | 194 | /* If no writer did a change during our lookup, we can return early. */ |
| 195 | if (!create && !invalidated) |
| 196 | return NULL; |
| 197 | |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 198 | /* retry an exact lookup, taking the lock before. |
| 199 | * At least, nodes should be hot in our cache. |
| 200 | */ |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 201 | parent = NULL; |
Eric Dumazet | 65e8354 | 2011-03-04 14:33:59 -0800 | [diff] [blame] | 202 | write_seqlock_bh(&base->lock); |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 203 | |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 204 | gc_cnt = 0; |
| 205 | p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp); |
| 206 | if (!p && create) { |
| 207 | p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC); |
| 208 | if (p) { |
| 209 | p->daddr = *daddr; |
| 210 | refcount_set(&p->refcnt, 2); |
| 211 | atomic_set(&p->rid, 0); |
| 212 | p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW; |
| 213 | p->rate_tokens = 0; |
| 214 | /* 60*HZ is arbitrary, but chosen enough high so that the first |
| 215 | * calculation of tokens is at its maximum. |
| 216 | */ |
| 217 | p->rate_last = jiffies - 60*HZ; |
| 218 | |
| 219 | rb_link_node(&p->rb_node, parent, pp); |
| 220 | rb_insert_color(&p->rb_node, &base->rb_root); |
| 221 | base->total++; |
| 222 | } |
Eric Dumazet | aa1039e | 2010-06-15 08:23:14 +0000 | [diff] [blame] | 223 | } |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 224 | if (gc_cnt) |
| 225 | inet_peer_gc(base, gc_stack, gc_cnt); |
Eric Dumazet | 65e8354 | 2011-03-04 14:33:59 -0800 | [diff] [blame] | 226 | write_sequnlock_bh(&base->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return p; |
| 229 | } |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(inet_getpeer); |
David S. Miller | 98158f5 | 2010-11-30 11:41:59 -0800 | [diff] [blame] | 231 | |
Eric Dumazet | 4663afe | 2006-10-12 21:21:06 -0700 | [diff] [blame] | 232 | void inet_putpeer(struct inet_peer *p) |
| 233 | { |
Eric Dumazet | 4b9d9be | 2011-06-08 13:35:34 +0000 | [diff] [blame] | 234 | p->dtime = (__u32)jiffies; |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 235 | |
| 236 | if (refcount_dec_and_test(&p->refcnt)) |
| 237 | call_rcu(&p->rcu, inetpeer_free_rcu); |
Eric Dumazet | 4663afe | 2006-10-12 21:21:06 -0700 | [diff] [blame] | 238 | } |
David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 239 | EXPORT_SYMBOL_GPL(inet_putpeer); |
David S. Miller | 92d8682 | 2011-02-04 15:55:25 -0800 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * Check transmit rate limitation for given message. |
| 243 | * The rate information is held in the inet_peer entries now. |
| 244 | * This function is generic and could be used for other purposes |
| 245 | * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov. |
| 246 | * |
| 247 | * Note that the same inet_peer fields are modified by functions in |
| 248 | * route.c too, but these work for packet destinations while xrlim_allow |
| 249 | * works for icmp destinations. This means the rate limiting information |
| 250 | * for one "ip object" is shared - and these ICMPs are twice limited: |
| 251 | * by source and by destination. |
| 252 | * |
| 253 | * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate |
| 254 | * SHOULD allow setting of rate limits |
| 255 | * |
| 256 | * Shared between ICMPv4 and ICMPv6. |
| 257 | */ |
| 258 | #define XRLIM_BURST_FACTOR 6 |
| 259 | bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout) |
| 260 | { |
| 261 | unsigned long now, token; |
| 262 | bool rc = false; |
| 263 | |
| 264 | if (!peer) |
| 265 | return true; |
| 266 | |
| 267 | token = peer->rate_tokens; |
| 268 | now = jiffies; |
| 269 | token += now - peer->rate_last; |
| 270 | peer->rate_last = now; |
| 271 | if (token > XRLIM_BURST_FACTOR * timeout) |
| 272 | token = XRLIM_BURST_FACTOR * timeout; |
| 273 | if (token >= timeout) { |
| 274 | token -= timeout; |
| 275 | rc = true; |
| 276 | } |
| 277 | peer->rate_tokens = token; |
| 278 | return rc; |
| 279 | } |
| 280 | EXPORT_SYMBOL(inet_peer_xrlim_allow); |
Steffen Klassert | 5faa5df | 2012-03-06 21:20:26 +0000 | [diff] [blame] | 281 | |
David S. Miller | 56a6b24 | 2012-06-09 16:32:41 -0700 | [diff] [blame] | 282 | void inetpeer_invalidate_tree(struct inet_peer_base *base) |
Steffen Klassert | 5faa5df | 2012-03-06 21:20:26 +0000 | [diff] [blame] | 283 | { |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 284 | struct inet_peer *p, *n; |
Steffen Klassert | 5faa5df | 2012-03-06 21:20:26 +0000 | [diff] [blame] | 285 | |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 286 | rbtree_postorder_for_each_entry_safe(p, n, &base->rb_root, rb_node) { |
| 287 | inet_putpeer(p); |
| 288 | cond_resched(); |
Steffen Klassert | 5faa5df | 2012-03-06 21:20:26 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Eric Dumazet | b145425 | 2017-07-17 02:56:10 -0700 | [diff] [blame^] | 291 | base->rb_root = RB_ROOT; |
| 292 | base->total = 0; |
Steffen Klassert | 5faa5df | 2012-03-06 21:20:26 +0000 | [diff] [blame] | 293 | } |
| 294 | EXPORT_SYMBOL(inetpeer_invalidate_tree); |