RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / ipv4 / inet_hashtables.c
bloba29426f221119e8613975f820ea4d481cd4ea114
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Generic INET transport hashtables
8 * Authors: Lotsa people, from code originally in tcp
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/wait.h>
22 #include <net/inet_connection_sock.h>
23 #include <net/inet_hashtables.h>
24 #include <net/ip.h>
27 * Allocate and initialize a new local port bind bucket.
28 * The bindhash mutex for snum's hash chain must be held here.
30 struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
31 struct inet_bind_hashbucket *head,
32 const unsigned short snum)
34 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
36 if (tb != NULL) {
37 tb->port = snum;
38 tb->fastreuse = 0;
39 INIT_HLIST_HEAD(&tb->owners);
40 hlist_add_head(&tb->node, &head->chain);
42 return tb;
46 * Caller must hold hashbucket lock for this tb with local BH disabled
48 void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
50 if (hlist_empty(&tb->owners)) {
51 __hlist_del(&tb->node);
52 kmem_cache_free(cachep, tb);
56 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
57 const unsigned short snum)
59 inet_sk(sk)->num = snum;
60 sk_add_bind_node(sk, &tb->owners);
61 inet_csk(sk)->icsk_bind_hash = tb;
65 * Get rid of any references to a local port held by the given sock.
67 static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
69 const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size);
70 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
71 struct inet_bind_bucket *tb;
73 spin_lock(&head->lock);
74 tb = inet_csk(sk)->icsk_bind_hash;
75 __sk_del_bind_node(sk);
76 inet_csk(sk)->icsk_bind_hash = NULL;
77 inet_sk(sk)->num = 0;
78 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
79 spin_unlock(&head->lock);
82 void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
84 local_bh_disable();
85 __inet_put_port(hashinfo, sk);
86 local_bh_enable();
89 EXPORT_SYMBOL(inet_put_port);
92 * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
93 * Look, when several writers sleep and reader wakes them up, all but one
94 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
95 * this, _but_ remember, it adds useless work on UP machines (wake up each
96 * exclusive lock release). It should be ifdefed really.
98 void inet_listen_wlock(struct inet_hashinfo *hashinfo)
100 write_lock(&hashinfo->lhash_lock);
102 if (atomic_read(&hashinfo->lhash_users)) {
103 DEFINE_WAIT(wait);
105 for (;;) {
106 prepare_to_wait_exclusive(&hashinfo->lhash_wait,
107 &wait, TASK_UNINTERRUPTIBLE);
108 if (!atomic_read(&hashinfo->lhash_users))
109 break;
110 write_unlock_bh(&hashinfo->lhash_lock);
111 schedule();
112 write_lock_bh(&hashinfo->lhash_lock);
115 finish_wait(&hashinfo->lhash_wait, &wait);
119 EXPORT_SYMBOL(inet_listen_wlock);
122 * Don't inline this cruft. Here are some nice properties to exploit here. The
123 * BSD API does not allow a listening sock to specify the remote port nor the
124 * remote address for the connection. So always assume those are both
125 * wildcarded during the search since they can never be otherwise.
127 static struct sock *inet_lookup_listener_slow(const struct hlist_head *head,
128 const __be32 daddr,
129 const unsigned short hnum,
130 const int dif)
132 struct sock *result = NULL, *sk;
133 const struct hlist_node *node;
134 int hiscore = -1;
136 sk_for_each(sk, node, head) {
137 const struct inet_sock *inet = inet_sk(sk);
139 if (inet->num == hnum && !ipv6_only_sock(sk)) {
140 const __be32 rcv_saddr = inet->rcv_saddr;
141 int score = sk->sk_family == PF_INET ? 1 : 0;
143 if (rcv_saddr) {
144 if (rcv_saddr != daddr)
145 continue;
146 score += 2;
148 if (sk->sk_bound_dev_if) {
149 if (sk->sk_bound_dev_if != dif)
150 continue;
151 score += 2;
153 if (score == 5)
154 return sk;
155 if (score > hiscore) {
156 hiscore = score;
157 result = sk;
161 return result;
164 /* Optimize the common listener case. */
165 struct sock *__inet_lookup_listener(struct inet_hashinfo *hashinfo,
166 const __be32 daddr, const unsigned short hnum,
167 const int dif)
169 struct sock *sk = NULL;
170 const struct hlist_head *head;
172 read_lock(&hashinfo->lhash_lock);
173 head = &hashinfo->listening_hash[inet_lhashfn(hnum)];
174 if (!hlist_empty(head)) {
175 const struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
177 if (inet->num == hnum && !sk->sk_node.next &&
178 (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
179 (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
180 !sk->sk_bound_dev_if)
181 goto sherry_cache;
182 sk = inet_lookup_listener_slow(head, daddr, hnum, dif);
184 if (sk) {
185 sherry_cache:
186 sock_hold(sk);
188 read_unlock(&hashinfo->lhash_lock);
189 return sk;
191 EXPORT_SYMBOL_GPL(__inet_lookup_listener);
193 struct sock * __inet_lookup_established(struct inet_hashinfo *hashinfo,
194 const __be32 saddr, const __be16 sport,
195 const __be32 daddr, const u16 hnum,
196 const int dif)
198 INET_ADDR_COOKIE(acookie, saddr, daddr)
199 const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
200 struct sock *sk;
201 const struct hlist_node *node;
202 /* Optimize here for direct hit, only listening connections can
203 * have wildcards anyways.
205 unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
206 struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
208 prefetch(head->chain.first);
209 read_lock(&head->lock);
210 sk_for_each(sk, node, &head->chain) {
211 if (INET_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
212 goto hit; /* You sunk my battleship! */
215 /* Must check for a TIME_WAIT'er before going to listener hash. */
216 sk_for_each(sk, node, &head->twchain) {
217 if (INET_TW_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
218 goto hit;
220 sk = NULL;
221 out:
222 read_unlock(&head->lock);
223 return sk;
224 hit:
225 sock_hold(sk);
226 goto out;
228 EXPORT_SYMBOL_GPL(__inet_lookup_established);
230 /* called with local bh disabled */
231 static int __inet_check_established(struct inet_timewait_death_row *death_row,
232 struct sock *sk, __u16 lport,
233 struct inet_timewait_sock **twp)
235 struct inet_hashinfo *hinfo = death_row->hashinfo;
236 struct inet_sock *inet = inet_sk(sk);
237 __be32 daddr = inet->rcv_saddr;
238 __be32 saddr = inet->daddr;
239 int dif = sk->sk_bound_dev_if;
240 INET_ADDR_COOKIE(acookie, saddr, daddr)
241 const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
242 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
243 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
244 struct sock *sk2;
245 const struct hlist_node *node;
246 struct inet_timewait_sock *tw;
248 prefetch(head->chain.first);
249 write_lock(&head->lock);
251 /* Check TIME-WAIT sockets first. */
252 sk_for_each(sk2, node, &head->twchain) {
253 tw = inet_twsk(sk2);
255 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) {
256 if (twsk_unique(sk, sk2, twp))
257 goto unique;
258 else
259 goto not_unique;
262 tw = NULL;
264 /* And established part... */
265 sk_for_each(sk2, node, &head->chain) {
266 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
267 goto not_unique;
270 unique:
271 /* Must record num and sport now. Otherwise we will see
272 * in hash table socket with a funny identity. */
273 inet->num = lport;
274 inet->sport = htons(lport);
275 sk->sk_hash = hash;
276 BUG_TRAP(sk_unhashed(sk));
277 __sk_add_node(sk, &head->chain);
278 sock_prot_inc_use(sk->sk_prot);
279 write_unlock(&head->lock);
281 if (twp) {
282 *twp = tw;
283 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
284 } else if (tw) {
285 /* Silly. Should hash-dance instead... */
286 inet_twsk_deschedule(tw, death_row);
287 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
289 inet_twsk_put(tw);
292 return 0;
294 not_unique:
295 write_unlock(&head->lock);
296 return -EADDRNOTAVAIL;
299 static inline u32 inet_sk_port_offset(const struct sock *sk)
301 const struct inet_sock *inet = inet_sk(sk);
302 return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
303 inet->dport);
306 void __inet_hash_nolisten(struct inet_hashinfo *hashinfo, struct sock *sk)
308 struct hlist_head *list;
309 rwlock_t *lock;
310 struct inet_ehash_bucket *head;
312 BUG_TRAP(sk_unhashed(sk));
314 sk->sk_hash = inet_sk_ehashfn(sk);
315 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
316 list = &head->chain;
317 lock = &head->lock;
319 write_lock(lock);
320 __sk_add_node(sk, list);
321 sock_prot_inc_use(sk->sk_prot);
322 write_unlock(lock);
324 EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
326 void __inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk)
328 struct hlist_head *list;
329 rwlock_t *lock;
331 if (sk->sk_state != TCP_LISTEN) {
332 __inet_hash_nolisten(hashinfo, sk);
333 return;
336 BUG_TRAP(sk_unhashed(sk));
337 list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
338 lock = &hashinfo->lhash_lock;
340 inet_listen_wlock(hashinfo);
341 __sk_add_node(sk, list);
342 sock_prot_inc_use(sk->sk_prot);
343 write_unlock(lock);
344 wake_up(&hashinfo->lhash_wait);
346 EXPORT_SYMBOL_GPL(__inet_hash);
348 int __inet_hash_connect(struct inet_timewait_death_row *death_row,
349 struct sock *sk, u32 port_offset,
350 int (*check_established)(struct inet_timewait_death_row *,
351 struct sock *, __u16, struct inet_timewait_sock **),
352 void (*hash)(struct inet_hashinfo *, struct sock *))
354 struct inet_hashinfo *hinfo = death_row->hashinfo;
355 const unsigned short snum = inet_sk(sk)->num;
356 struct inet_bind_hashbucket *head;
357 struct inet_bind_bucket *tb;
358 int ret;
360 if (!snum) {
361 int low = sysctl_local_port_range[0];
362 int high = sysctl_local_port_range[1];
363 int range = high - low;
364 int i;
365 int port;
366 static u32 hint;
367 u32 offset = hint + port_offset;
368 struct hlist_node *node;
369 struct inet_timewait_sock *tw = NULL;
371 local_bh_disable();
372 for (i = 1; i <= range; i++) {
373 port = low + (i + offset) % range;
374 head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
375 spin_lock(&head->lock);
377 /* Does not bother with rcv_saddr checks,
378 * because the established check is already
379 * unique enough.
381 inet_bind_bucket_for_each(tb, node, &head->chain) {
382 if (tb->port == port) {
383 BUG_TRAP(!hlist_empty(&tb->owners));
384 if (tb->fastreuse >= 0)
385 goto next_port;
386 if (!check_established(death_row, sk,
387 port, &tw))
388 goto ok;
389 goto next_port;
393 tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep, head, port);
394 if (!tb) {
395 spin_unlock(&head->lock);
396 break;
398 tb->fastreuse = -1;
399 goto ok;
401 next_port:
402 spin_unlock(&head->lock);
404 local_bh_enable();
406 return -EADDRNOTAVAIL;
409 hint += i;
411 /* Head lock still held and bh's disabled */
412 inet_bind_hash(sk, tb, port);
413 if (sk_unhashed(sk)) {
414 inet_sk(sk)->sport = htons(port);
415 hash(hinfo, sk);
417 spin_unlock(&head->lock);
419 if (tw) {
420 inet_twsk_deschedule(tw, death_row);
421 inet_twsk_put(tw);
424 ret = 0;
425 goto out;
428 head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
429 tb = inet_csk(sk)->icsk_bind_hash;
430 spin_lock_bh(&head->lock);
431 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
432 hash(hinfo, sk);
433 spin_unlock_bh(&head->lock);
434 return 0;
435 } else {
436 spin_unlock(&head->lock);
437 /* No definite answer... Walk to established hash table */
438 ret = check_established(death_row, sk, snum, NULL);
439 out:
440 local_bh_enable();
441 return ret;
444 EXPORT_SYMBOL_GPL(__inet_hash_connect);
447 * Bind a port for a connect operation and hash it.
449 int inet_hash_connect(struct inet_timewait_death_row *death_row,
450 struct sock *sk)
452 return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
453 __inet_check_established, __inet_hash_nolisten);
456 EXPORT_SYMBOL_GPL(inet_hash_connect);