inetpeer: fix uninit-value in inet_getpeer
[linux/fpc-iii.git] / net / ipv4 / inetpeer.c
blob1ef8f86072bd0f2c9aa51a139decf98018c80a40
1 /*
2 * INETPEER - A storage for permanent information about peers
4 * This source is covered by the GNU GPL, the same as all kernel sources.
6 * Authors: Andrey V. Savochkin <saw@msu.ru>
7 */
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>
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>
20 #include <linux/workqueue.h>
21 #include <net/ip.h>
22 #include <net/inetpeer.h>
23 #include <net/secure_seq.h>
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.
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.
36 * Node pool is organised as an RB tree.
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.
42 * Serialisation issues.
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
45 * AND reference count being 0.
46 * 3. Global variable peer_total is modified under the pool lock.
47 * 4. struct inet_peer fields modification:
48 * rb_node: pool lock
49 * refcnt: atomically against modifications on other CPU;
50 * usually under some other lock to prevent node disappearing
51 * daddr: unchangeable
54 static struct kmem_cache *peer_cachep __read_mostly;
56 void inet_peer_base_init(struct inet_peer_base *bp)
58 bp->rb_root = RB_ROOT;
59 seqlock_init(&bp->lock);
60 bp->total = 0;
62 EXPORT_SYMBOL_GPL(inet_peer_base_init);
64 #define PEER_MAX_GC 32
66 /* Exported for sysctl_net_ipv4. */
67 int inet_peer_threshold __read_mostly = 65536 + 128; /* start to throw entries more
68 * aggressively at this stage */
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 */
72 /* Called from ip_output.c:ip_init */
73 void __init inet_initpeers(void)
75 struct sysinfo si;
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
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 */
90 peer_cachep = kmem_cache_create("inet_peer_cache",
91 sizeof(struct inet_peer),
92 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
93 NULL);
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)
105 struct rb_node **pp, *parent, *next;
106 struct inet_peer *p;
108 pp = &base->rb_root.rb_node;
109 parent = NULL;
110 while (1) {
111 int cmp;
113 next = rcu_dereference_raw(*pp);
114 if (!next)
115 break;
116 parent = next;
117 p = rb_entry(parent, struct inet_peer, rb_node);
118 cmp = inetpeer_addr_cmp(daddr, &p->daddr);
119 if (cmp == 0) {
120 if (!refcount_inc_not_zero(&p->refcnt))
121 break;
122 return p;
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;
130 if (cmp == -1)
131 pp = &next->rb_left;
132 else
133 pp = &next->rb_right;
135 *parent_p = parent;
136 *pp_p = pp;
137 return NULL;
140 static void inetpeer_free_rcu(struct rcu_head *head)
142 kmem_cache_free(peer_cachep, container_of(head, struct inet_peer, rcu));
145 /* perform garbage collect on all items stacked during a lookup */
146 static void inet_peer_gc(struct inet_peer_base *base,
147 struct inet_peer *gc_stack[],
148 unsigned int gc_cnt)
150 struct inet_peer *p;
151 __u32 delta, ttl;
152 int i;
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;
160 for (i = 0; i < gc_cnt; i++) {
161 p = gc_stack[i];
162 delta = (__u32)jiffies - p->dtime;
163 if (delta < ttl || !refcount_dec_if_one(&p->refcnt))
164 gc_stack[i] = NULL;
166 for (i = 0; i < gc_cnt; i++) {
167 p = gc_stack[i];
168 if (p) {
169 rb_erase(&p->rb_node, &base->rb_root);
170 base->total--;
171 call_rcu(&p->rcu, inetpeer_free_rcu);
176 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
177 const struct inetpeer_addr *daddr,
178 int create)
180 struct inet_peer *p, *gc_stack[PEER_MAX_GC];
181 struct rb_node **pp, *parent;
182 unsigned int gc_cnt, seq;
183 int invalidated;
185 /* Attempt a lockless lookup first.
186 * Because of a concurrent writer, we might not find an existing entry.
188 rcu_read_lock();
189 seq = read_seqbegin(&base->lock);
190 p = lookup(daddr, base, seq, NULL, &gc_cnt, &parent, &pp);
191 invalidated = read_seqretry(&base->lock, seq);
192 rcu_read_unlock();
194 if (p)
195 return p;
197 /* If no writer did a change during our lookup, we can return early. */
198 if (!create && !invalidated)
199 return NULL;
201 /* retry an exact lookup, taking the lock before.
202 * At least, nodes should be hot in our cache.
204 parent = NULL;
205 write_seqlock_bh(&base->lock);
207 gc_cnt = 0;
208 p = lookup(daddr, base, seq, gc_stack, &gc_cnt, &parent, &pp);
209 if (!p && create) {
210 p = kmem_cache_alloc(peer_cachep, GFP_ATOMIC);
211 if (p) {
212 p->daddr = *daddr;
213 p->dtime = (__u32)jiffies;
214 refcount_set(&p->refcnt, 2);
215 atomic_set(&p->rid, 0);
216 p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
217 p->rate_tokens = 0;
218 /* 60*HZ is arbitrary, but chosen enough high so that the first
219 * calculation of tokens is at its maximum.
221 p->rate_last = jiffies - 60*HZ;
223 rb_link_node(&p->rb_node, parent, pp);
224 rb_insert_color(&p->rb_node, &base->rb_root);
225 base->total++;
228 if (gc_cnt)
229 inet_peer_gc(base, gc_stack, gc_cnt);
230 write_sequnlock_bh(&base->lock);
232 return p;
234 EXPORT_SYMBOL_GPL(inet_getpeer);
236 void inet_putpeer(struct inet_peer *p)
238 p->dtime = (__u32)jiffies;
240 if (refcount_dec_and_test(&p->refcnt))
241 call_rcu(&p->rcu, inetpeer_free_rcu);
243 EXPORT_SYMBOL_GPL(inet_putpeer);
246 * Check transmit rate limitation for given message.
247 * The rate information is held in the inet_peer entries now.
248 * This function is generic and could be used for other purposes
249 * too. It uses a Token bucket filter as suggested by Alexey Kuznetsov.
251 * Note that the same inet_peer fields are modified by functions in
252 * route.c too, but these work for packet destinations while xrlim_allow
253 * works for icmp destinations. This means the rate limiting information
254 * for one "ip object" is shared - and these ICMPs are twice limited:
255 * by source and by destination.
257 * RFC 1812: 4.3.2.8 SHOULD be able to limit error message rate
258 * SHOULD allow setting of rate limits
260 * Shared between ICMPv4 and ICMPv6.
262 #define XRLIM_BURST_FACTOR 6
263 bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout)
265 unsigned long now, token;
266 bool rc = false;
268 if (!peer)
269 return true;
271 token = peer->rate_tokens;
272 now = jiffies;
273 token += now - peer->rate_last;
274 peer->rate_last = now;
275 if (token > XRLIM_BURST_FACTOR * timeout)
276 token = XRLIM_BURST_FACTOR * timeout;
277 if (token >= timeout) {
278 token -= timeout;
279 rc = true;
281 peer->rate_tokens = token;
282 return rc;
284 EXPORT_SYMBOL(inet_peer_xrlim_allow);
286 void inetpeer_invalidate_tree(struct inet_peer_base *base)
288 struct rb_node *p = rb_first(&base->rb_root);
290 while (p) {
291 struct inet_peer *peer = rb_entry(p, struct inet_peer, rb_node);
293 p = rb_next(p);
294 rb_erase(&peer->rb_node, &base->rb_root);
295 inet_putpeer(peer);
296 cond_resched();
299 base->total = 0;
301 EXPORT_SYMBOL(inetpeer_invalidate_tree);