blob: 2e5814a84360977c9fa36337715c0448a16a7d64 [file] [log] [blame]
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -07001/*
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.
5 *
6 * Generic INET transport hashtables
7 *
8 * Authors: Lotsa people, from code originally in tcp
9 *
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.
14 */
15
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070016#include <linux/module.h>
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -080017#include <linux/random.h>
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070018#include <linux/sched.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070019#include <linux/slab.h>
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070020#include <linux/wait.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070021
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070022#include <net/inet_connection_sock.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070023#include <net/inet_hashtables.h>
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -080024#include <net/ip.h>
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070025
26/*
27 * Allocate and initialize a new local port bind bucket.
28 * The bindhash mutex for snum's hash chain must be held here.
29 */
Christoph Lametere18b8902006-12-06 20:33:20 -080030struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070031 struct inet_bind_hashbucket *head,
32 const unsigned short snum)
33{
Christoph Lameter54e6ecb2006-12-06 20:33:16 -080034 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070035
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);
41 }
42 return tb;
43}
44
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070045/*
46 * Caller must hold hashbucket lock for this tb with local BH disabled
47 */
Christoph Lametere18b8902006-12-06 20:33:20 -080048void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
Arnaldo Carvalho de Melo77d8bf92005-08-09 20:00:51 -070049{
50 if (hlist_empty(&tb->owners)) {
51 __hlist_del(&tb->node);
52 kmem_cache_free(cachep, tb);
53 }
54}
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070055
56void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
57 const unsigned short snum)
58{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070059 inet_sk(sk)->num = snum;
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070060 sk_add_bind_node(sk, &tb->owners);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070061 inet_csk(sk)->icsk_bind_hash = tb;
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070062}
63
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070064/*
65 * Get rid of any references to a local port held by the given sock.
66 */
67static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
68{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070069 const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size);
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070070 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
71 struct inet_bind_bucket *tb;
72
73 spin_lock(&head->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070074 tb = inet_csk(sk)->icsk_bind_hash;
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070075 __sk_del_bind_node(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070076 inet_csk(sk)->icsk_bind_hash = NULL;
77 inet_sk(sk)->num = 0;
Arnaldo Carvalho de Melo2d8c4ce2005-08-09 20:07:13 -070078 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
79 spin_unlock(&head->lock);
80}
81
82void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
83{
84 local_bh_disable();
85 __inet_put_port(hashinfo, sk);
86 local_bh_enable();
87}
88
89EXPORT_SYMBOL(inet_put_port);
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -070090
91/*
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.
97 */
98void inet_listen_wlock(struct inet_hashinfo *hashinfo)
99{
100 write_lock(&hashinfo->lhash_lock);
101
102 if (atomic_read(&hashinfo->lhash_users)) {
103 DEFINE_WAIT(wait);
104
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);
113 }
114
115 finish_wait(&hashinfo->lhash_wait, &wait);
116 }
117}
118
119EXPORT_SYMBOL(inet_listen_wlock);
Arnaldo Carvalho de Melo33b62232005-08-09 20:09:06 -0700120
121/*
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.
126 */
Herbert Xu8f4910692006-08-09 15:47:12 -0700127static struct sock *inet_lookup_listener_slow(const struct hlist_head *head,
Al Virofb99c842006-09-27 18:43:33 -0700128 const __be32 daddr,
Herbert Xu8f4910692006-08-09 15:47:12 -0700129 const unsigned short hnum,
130 const int dif)
Arnaldo Carvalho de Melo33b62232005-08-09 20:09:06 -0700131{
132 struct sock *result = NULL, *sk;
133 const struct hlist_node *node;
134 int hiscore = -1;
135
136 sk_for_each(sk, node, head) {
137 const struct inet_sock *inet = inet_sk(sk);
138
139 if (inet->num == hnum && !ipv6_only_sock(sk)) {
Al Virofb99c842006-09-27 18:43:33 -0700140 const __be32 rcv_saddr = inet->rcv_saddr;
Arnaldo Carvalho de Melo33b62232005-08-09 20:09:06 -0700141 int score = sk->sk_family == PF_INET ? 1 : 0;
142
143 if (rcv_saddr) {
144 if (rcv_saddr != daddr)
145 continue;
146 score += 2;
147 }
148 if (sk->sk_bound_dev_if) {
149 if (sk->sk_bound_dev_if != dif)
150 continue;
151 score += 2;
152 }
153 if (score == 5)
154 return sk;
155 if (score > hiscore) {
156 hiscore = score;
157 result = sk;
158 }
159 }
160 }
161 return result;
162}
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700163
Herbert Xu99a92ff2006-08-08 02:18:10 -0700164/* Optimize the common listener case. */
Herbert Xu8f4910692006-08-09 15:47:12 -0700165struct sock *__inet_lookup_listener(struct inet_hashinfo *hashinfo,
Al Virofb99c842006-09-27 18:43:33 -0700166 const __be32 daddr, const unsigned short hnum,
Herbert Xu8f4910692006-08-09 15:47:12 -0700167 const int dif)
Herbert Xu99a92ff2006-08-08 02:18:10 -0700168{
169 struct sock *sk = NULL;
170 const struct hlist_head *head;
171
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)));
176
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;
Herbert Xu8f4910692006-08-09 15:47:12 -0700182 sk = inet_lookup_listener_slow(head, daddr, hnum, dif);
Herbert Xu99a92ff2006-08-08 02:18:10 -0700183 }
184 if (sk) {
185sherry_cache:
186 sock_hold(sk);
187 }
188 read_unlock(&hashinfo->lhash_lock);
189 return sk;
190}
Herbert Xu8f4910692006-08-09 15:47:12 -0700191EXPORT_SYMBOL_GPL(__inet_lookup_listener);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800192
Pavel Emelyanov77a5ba52007-12-20 15:32:17 -0800193struct 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)
197{
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.
204 */
205 unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
206 struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
207 rwlock_t *lock = inet_ehash_lockp(hashinfo, hash);
208
209 prefetch(head->chain.first);
210 read_lock(lock);
211 sk_for_each(sk, node, &head->chain) {
212 if (INET_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
213 goto hit; /* You sunk my battleship! */
214 }
215
216 /* Must check for a TIME_WAIT'er before going to listener hash. */
217 sk_for_each(sk, node, &head->twchain) {
218 if (INET_TW_MATCH(sk, hash, acookie, saddr, daddr, ports, dif))
219 goto hit;
220 }
221 sk = NULL;
222out:
223 read_unlock(lock);
224 return sk;
225hit:
226 sock_hold(sk);
227 goto out;
228}
229EXPORT_SYMBOL_GPL(__inet_lookup_established);
230
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800231/* called with local bh disabled */
232static int __inet_check_established(struct inet_timewait_death_row *death_row,
233 struct sock *sk, __u16 lport,
234 struct inet_timewait_sock **twp)
235{
236 struct inet_hashinfo *hinfo = death_row->hashinfo;
237 struct inet_sock *inet = inet_sk(sk);
Al Virofb99c842006-09-27 18:43:33 -0700238 __be32 daddr = inet->rcv_saddr;
239 __be32 saddr = inet->daddr;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800240 int dif = sk->sk_bound_dev_if;
241 INET_ADDR_COOKIE(acookie, saddr, daddr)
Al Viro4f765d82006-09-27 18:43:07 -0700242 const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800243 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
244 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
Eric Dumazet230140c2007-11-07 02:40:20 -0800245 rwlock_t *lock = inet_ehash_lockp(hinfo, hash);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800246 struct sock *sk2;
247 const struct hlist_node *node;
248 struct inet_timewait_sock *tw;
249
250 prefetch(head->chain.first);
Eric Dumazet230140c2007-11-07 02:40:20 -0800251 write_lock(lock);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800252
253 /* Check TIME-WAIT sockets first. */
Eric Dumazetdbca9b2752007-02-08 14:16:46 -0800254 sk_for_each(sk2, node, &head->twchain) {
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800255 tw = inet_twsk(sk2);
256
257 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) {
258 if (twsk_unique(sk, sk2, twp))
259 goto unique;
260 else
261 goto not_unique;
262 }
263 }
264 tw = NULL;
265
266 /* And established part... */
267 sk_for_each(sk2, node, &head->chain) {
268 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
269 goto not_unique;
270 }
271
272unique:
273 /* Must record num and sport now. Otherwise we will see
274 * in hash table socket with a funny identity. */
275 inet->num = lport;
276 inet->sport = htons(lport);
277 sk->sk_hash = hash;
278 BUG_TRAP(sk_unhashed(sk));
279 __sk_add_node(sk, &head->chain);
280 sock_prot_inc_use(sk->sk_prot);
Eric Dumazet230140c2007-11-07 02:40:20 -0800281 write_unlock(lock);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800282
283 if (twp) {
284 *twp = tw;
285 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
286 } else if (tw) {
287 /* Silly. Should hash-dance instead... */
288 inet_twsk_deschedule(tw, death_row);
289 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
290
291 inet_twsk_put(tw);
292 }
293
294 return 0;
295
296not_unique:
Eric Dumazet230140c2007-11-07 02:40:20 -0800297 write_unlock(lock);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800298 return -EADDRNOTAVAIL;
299}
300
301static inline u32 inet_sk_port_offset(const struct sock *sk)
302{
303 const struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900304 return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800305 inet->dport);
306}
307
Pavel Emelyanov152da812007-12-20 15:31:33 -0800308void __inet_hash_nolisten(struct inet_hashinfo *hashinfo, struct sock *sk)
309{
310 struct hlist_head *list;
311 rwlock_t *lock;
312 struct inet_ehash_bucket *head;
313
314 BUG_TRAP(sk_unhashed(sk));
315
316 sk->sk_hash = inet_sk_ehashfn(sk);
317 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
318 list = &head->chain;
319 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
320
321 write_lock(lock);
322 __sk_add_node(sk, list);
323 sock_prot_inc_use(sk->sk_prot);
324 write_unlock(lock);
325}
326EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
327
328void __inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk)
329{
330 struct hlist_head *list;
331 rwlock_t *lock;
332
333 if (sk->sk_state != TCP_LISTEN) {
334 __inet_hash_nolisten(hashinfo, sk);
335 return;
336 }
337
338 BUG_TRAP(sk_unhashed(sk));
339 list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
340 lock = &hashinfo->lhash_lock;
341
342 inet_listen_wlock(hashinfo);
343 __sk_add_node(sk, list);
344 sock_prot_inc_use(sk->sk_prot);
345 write_unlock(lock);
346 wake_up(&hashinfo->lhash_wait);
347}
348EXPORT_SYMBOL_GPL(__inet_hash);
349
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800350/*
351 * Bind a port for a connect operation and hash it.
352 */
353int inet_hash_connect(struct inet_timewait_death_row *death_row,
354 struct sock *sk)
355{
356 struct inet_hashinfo *hinfo = death_row->hashinfo;
357 const unsigned short snum = inet_sk(sk)->num;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900358 struct inet_bind_hashbucket *head;
359 struct inet_bind_bucket *tb;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800360 int ret;
361
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900362 if (!snum) {
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700363 int i, remaining, low, high, port;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800364 static u32 hint;
365 u32 offset = hint + inet_sk_port_offset(sk);
366 struct hlist_node *node;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900367 struct inet_timewait_sock *tw = NULL;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800368
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700369 inet_get_local_port_range(&low, &high);
Anton Arapova25de532007-10-18 22:00:17 -0700370 remaining = (high - low) + 1;
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700371
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900372 local_bh_disable();
Stephen Hemminger227b60f2007-10-10 17:30:46 -0700373 for (i = 1; i <= remaining; i++) {
374 port = low + (i + offset) % remaining;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900375 head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
376 spin_lock(&head->lock);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800377
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900378 /* Does not bother with rcv_saddr checks,
379 * because the established check is already
380 * unique enough.
381 */
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800382 inet_bind_bucket_for_each(tb, node, &head->chain) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900383 if (tb->port == port) {
384 BUG_TRAP(!hlist_empty(&tb->owners));
385 if (tb->fastreuse >= 0)
386 goto next_port;
387 if (!__inet_check_established(death_row,
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800388 sk, port,
389 &tw))
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900390 goto ok;
391 goto next_port;
392 }
393 }
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800394
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900395 tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep, head, port);
396 if (!tb) {
397 spin_unlock(&head->lock);
398 break;
399 }
400 tb->fastreuse = -1;
401 goto ok;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800402
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900403 next_port:
404 spin_unlock(&head->lock);
405 }
406 local_bh_enable();
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800407
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900408 return -EADDRNOTAVAIL;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800409
410ok:
411 hint += i;
412
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900413 /* Head lock still held and bh's disabled */
414 inet_bind_hash(sk, tb, port);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800415 if (sk_unhashed(sk)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900416 inet_sk(sk)->sport = htons(port);
Pavel Emelyanov152da812007-12-20 15:31:33 -0800417 __inet_hash_nolisten(hinfo, sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900418 }
419 spin_unlock(&head->lock);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800420
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900421 if (tw) {
422 inet_twsk_deschedule(tw, death_row);
423 inet_twsk_put(tw);
424 }
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800425
426 ret = 0;
427 goto out;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900428 }
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800429
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900430 head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
431 tb = inet_csk(sk)->icsk_bind_hash;
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800432 spin_lock_bh(&head->lock);
433 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
Pavel Emelyanov152da812007-12-20 15:31:33 -0800434 __inet_hash_nolisten(hinfo, sk);
Arnaldo Carvalho de Meloa7f5e7f2005-12-13 23:25:31 -0800435 spin_unlock_bh(&head->lock);
436 return 0;
437 } else {
438 spin_unlock(&head->lock);
439 /* No definite answer... Walk to established hash table */
440 ret = __inet_check_established(death_row, sk, snum, NULL);
441out:
442 local_bh_enable();
443 return ret;
444 }
445}
446
447EXPORT_SYMBOL_GPL(inet_hash_connect);