177d8bf9cSArnaldo Carvalho de Melo /* 277d8bf9cSArnaldo Carvalho de Melo * INET An implementation of the TCP/IP protocol suite for the LINUX 377d8bf9cSArnaldo Carvalho de Melo * operating system. INET is implemented using the BSD Socket 477d8bf9cSArnaldo Carvalho de Melo * interface as the means of communication with the user level. 577d8bf9cSArnaldo Carvalho de Melo * 677d8bf9cSArnaldo Carvalho de Melo * Generic INET transport hashtables 777d8bf9cSArnaldo Carvalho de Melo * 877d8bf9cSArnaldo Carvalho de Melo * Authors: Lotsa people, from code originally in tcp 977d8bf9cSArnaldo Carvalho de Melo * 1077d8bf9cSArnaldo Carvalho de Melo * This program is free software; you can redistribute it and/or 1177d8bf9cSArnaldo Carvalho de Melo * modify it under the terms of the GNU General Public License 1277d8bf9cSArnaldo Carvalho de Melo * as published by the Free Software Foundation; either version 1377d8bf9cSArnaldo Carvalho de Melo * 2 of the License, or (at your option) any later version. 1477d8bf9cSArnaldo Carvalho de Melo */ 1577d8bf9cSArnaldo Carvalho de Melo 162d8c4ce5SArnaldo Carvalho de Melo #include <linux/module.h> 17a7f5e7f1SArnaldo Carvalho de Melo #include <linux/random.h> 18f3f05f70SArnaldo Carvalho de Melo #include <linux/sched.h> 1977d8bf9cSArnaldo Carvalho de Melo #include <linux/slab.h> 20f3f05f70SArnaldo Carvalho de Melo #include <linux/wait.h> 2177d8bf9cSArnaldo Carvalho de Melo 22463c84b9SArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 2377d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 24a7f5e7f1SArnaldo Carvalho de Melo #include <net/ip.h> 2577d8bf9cSArnaldo Carvalho de Melo 2677d8bf9cSArnaldo Carvalho de Melo /* 2777d8bf9cSArnaldo Carvalho de Melo * Allocate and initialize a new local port bind bucket. 2877d8bf9cSArnaldo Carvalho de Melo * The bindhash mutex for snum's hash chain must be held here. 2977d8bf9cSArnaldo Carvalho de Melo */ 30e18b890bSChristoph Lameter struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep, 3177d8bf9cSArnaldo Carvalho de Melo struct inet_bind_hashbucket *head, 3277d8bf9cSArnaldo Carvalho de Melo const unsigned short snum) 3377d8bf9cSArnaldo Carvalho de Melo { 3454e6ecb2SChristoph Lameter struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC); 3577d8bf9cSArnaldo Carvalho de Melo 3677d8bf9cSArnaldo Carvalho de Melo if (tb != NULL) { 3777d8bf9cSArnaldo Carvalho de Melo tb->port = snum; 3877d8bf9cSArnaldo Carvalho de Melo tb->fastreuse = 0; 3977d8bf9cSArnaldo Carvalho de Melo INIT_HLIST_HEAD(&tb->owners); 4077d8bf9cSArnaldo Carvalho de Melo hlist_add_head(&tb->node, &head->chain); 4177d8bf9cSArnaldo Carvalho de Melo } 4277d8bf9cSArnaldo Carvalho de Melo return tb; 4377d8bf9cSArnaldo Carvalho de Melo } 4477d8bf9cSArnaldo Carvalho de Melo 4577d8bf9cSArnaldo Carvalho de Melo /* 4677d8bf9cSArnaldo Carvalho de Melo * Caller must hold hashbucket lock for this tb with local BH disabled 4777d8bf9cSArnaldo Carvalho de Melo */ 48e18b890bSChristoph Lameter void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb) 4977d8bf9cSArnaldo Carvalho de Melo { 5077d8bf9cSArnaldo Carvalho de Melo if (hlist_empty(&tb->owners)) { 5177d8bf9cSArnaldo Carvalho de Melo __hlist_del(&tb->node); 5277d8bf9cSArnaldo Carvalho de Melo kmem_cache_free(cachep, tb); 5377d8bf9cSArnaldo Carvalho de Melo } 5477d8bf9cSArnaldo Carvalho de Melo } 552d8c4ce5SArnaldo Carvalho de Melo 562d8c4ce5SArnaldo Carvalho de Melo void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb, 572d8c4ce5SArnaldo Carvalho de Melo const unsigned short snum) 582d8c4ce5SArnaldo Carvalho de Melo { 59463c84b9SArnaldo Carvalho de Melo inet_sk(sk)->num = snum; 602d8c4ce5SArnaldo Carvalho de Melo sk_add_bind_node(sk, &tb->owners); 61463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_bind_hash = tb; 622d8c4ce5SArnaldo Carvalho de Melo } 632d8c4ce5SArnaldo Carvalho de Melo 642d8c4ce5SArnaldo Carvalho de Melo /* 652d8c4ce5SArnaldo Carvalho de Melo * Get rid of any references to a local port held by the given sock. 662d8c4ce5SArnaldo Carvalho de Melo */ 672d8c4ce5SArnaldo Carvalho de Melo static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk) 682d8c4ce5SArnaldo Carvalho de Melo { 69463c84b9SArnaldo Carvalho de Melo const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size); 702d8c4ce5SArnaldo Carvalho de Melo struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash]; 712d8c4ce5SArnaldo Carvalho de Melo struct inet_bind_bucket *tb; 722d8c4ce5SArnaldo Carvalho de Melo 732d8c4ce5SArnaldo Carvalho de Melo spin_lock(&head->lock); 74463c84b9SArnaldo Carvalho de Melo tb = inet_csk(sk)->icsk_bind_hash; 752d8c4ce5SArnaldo Carvalho de Melo __sk_del_bind_node(sk); 76463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_bind_hash = NULL; 77463c84b9SArnaldo Carvalho de Melo inet_sk(sk)->num = 0; 782d8c4ce5SArnaldo Carvalho de Melo inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); 792d8c4ce5SArnaldo Carvalho de Melo spin_unlock(&head->lock); 802d8c4ce5SArnaldo Carvalho de Melo } 812d8c4ce5SArnaldo Carvalho de Melo 822d8c4ce5SArnaldo Carvalho de Melo void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk) 832d8c4ce5SArnaldo Carvalho de Melo { 842d8c4ce5SArnaldo Carvalho de Melo local_bh_disable(); 852d8c4ce5SArnaldo Carvalho de Melo __inet_put_port(hashinfo, sk); 862d8c4ce5SArnaldo Carvalho de Melo local_bh_enable(); 872d8c4ce5SArnaldo Carvalho de Melo } 882d8c4ce5SArnaldo Carvalho de Melo 892d8c4ce5SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_put_port); 90f3f05f70SArnaldo Carvalho de Melo 91f3f05f70SArnaldo Carvalho de Melo /* 92f3f05f70SArnaldo Carvalho de Melo * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP. 93f3f05f70SArnaldo Carvalho de Melo * Look, when several writers sleep and reader wakes them up, all but one 94f3f05f70SArnaldo Carvalho de Melo * immediately hit write lock and grab all the cpus. Exclusive sleep solves 95f3f05f70SArnaldo Carvalho de Melo * this, _but_ remember, it adds useless work on UP machines (wake up each 96f3f05f70SArnaldo Carvalho de Melo * exclusive lock release). It should be ifdefed really. 97f3f05f70SArnaldo Carvalho de Melo */ 98f3f05f70SArnaldo Carvalho de Melo void inet_listen_wlock(struct inet_hashinfo *hashinfo) 99f3f05f70SArnaldo Carvalho de Melo { 100f3f05f70SArnaldo Carvalho de Melo write_lock(&hashinfo->lhash_lock); 101f3f05f70SArnaldo Carvalho de Melo 102f3f05f70SArnaldo Carvalho de Melo if (atomic_read(&hashinfo->lhash_users)) { 103f3f05f70SArnaldo Carvalho de Melo DEFINE_WAIT(wait); 104f3f05f70SArnaldo Carvalho de Melo 105f3f05f70SArnaldo Carvalho de Melo for (;;) { 106f3f05f70SArnaldo Carvalho de Melo prepare_to_wait_exclusive(&hashinfo->lhash_wait, 107f3f05f70SArnaldo Carvalho de Melo &wait, TASK_UNINTERRUPTIBLE); 108f3f05f70SArnaldo Carvalho de Melo if (!atomic_read(&hashinfo->lhash_users)) 109f3f05f70SArnaldo Carvalho de Melo break; 110f3f05f70SArnaldo Carvalho de Melo write_unlock_bh(&hashinfo->lhash_lock); 111f3f05f70SArnaldo Carvalho de Melo schedule(); 112f3f05f70SArnaldo Carvalho de Melo write_lock_bh(&hashinfo->lhash_lock); 113f3f05f70SArnaldo Carvalho de Melo } 114f3f05f70SArnaldo Carvalho de Melo 115f3f05f70SArnaldo Carvalho de Melo finish_wait(&hashinfo->lhash_wait, &wait); 116f3f05f70SArnaldo Carvalho de Melo } 117f3f05f70SArnaldo Carvalho de Melo } 118f3f05f70SArnaldo Carvalho de Melo 119f3f05f70SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_listen_wlock); 12033b62231SArnaldo Carvalho de Melo 12133b62231SArnaldo Carvalho de Melo /* 12233b62231SArnaldo Carvalho de Melo * Don't inline this cruft. Here are some nice properties to exploit here. The 12333b62231SArnaldo Carvalho de Melo * BSD API does not allow a listening sock to specify the remote port nor the 12433b62231SArnaldo Carvalho de Melo * remote address for the connection. So always assume those are both 12533b62231SArnaldo Carvalho de Melo * wildcarded during the search since they can never be otherwise. 12633b62231SArnaldo Carvalho de Melo */ 1278f491069SHerbert Xu static struct sock *inet_lookup_listener_slow(const struct hlist_head *head, 128fb99c848SAl Viro const __be32 daddr, 12999a92ff5SHerbert Xu const unsigned short hnum, 13099a92ff5SHerbert Xu const int dif) 13133b62231SArnaldo Carvalho de Melo { 13233b62231SArnaldo Carvalho de Melo struct sock *result = NULL, *sk; 13333b62231SArnaldo Carvalho de Melo const struct hlist_node *node; 13433b62231SArnaldo Carvalho de Melo int hiscore = -1; 13533b62231SArnaldo Carvalho de Melo 13633b62231SArnaldo Carvalho de Melo sk_for_each(sk, node, head) { 13733b62231SArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 13833b62231SArnaldo Carvalho de Melo 13933b62231SArnaldo Carvalho de Melo if (inet->num == hnum && !ipv6_only_sock(sk)) { 140fb99c848SAl Viro const __be32 rcv_saddr = inet->rcv_saddr; 14133b62231SArnaldo Carvalho de Melo int score = sk->sk_family == PF_INET ? 1 : 0; 14233b62231SArnaldo Carvalho de Melo 14333b62231SArnaldo Carvalho de Melo if (rcv_saddr) { 14433b62231SArnaldo Carvalho de Melo if (rcv_saddr != daddr) 14533b62231SArnaldo Carvalho de Melo continue; 14633b62231SArnaldo Carvalho de Melo score += 2; 14733b62231SArnaldo Carvalho de Melo } 14833b62231SArnaldo Carvalho de Melo if (sk->sk_bound_dev_if) { 14933b62231SArnaldo Carvalho de Melo if (sk->sk_bound_dev_if != dif) 15033b62231SArnaldo Carvalho de Melo continue; 15133b62231SArnaldo Carvalho de Melo score += 2; 15233b62231SArnaldo Carvalho de Melo } 15333b62231SArnaldo Carvalho de Melo if (score == 5) 15433b62231SArnaldo Carvalho de Melo return sk; 15533b62231SArnaldo Carvalho de Melo if (score > hiscore) { 15633b62231SArnaldo Carvalho de Melo hiscore = score; 15733b62231SArnaldo Carvalho de Melo result = sk; 15833b62231SArnaldo Carvalho de Melo } 15933b62231SArnaldo Carvalho de Melo } 16033b62231SArnaldo Carvalho de Melo } 16133b62231SArnaldo Carvalho de Melo return result; 16233b62231SArnaldo Carvalho de Melo } 163e48c414eSArnaldo Carvalho de Melo 16499a92ff5SHerbert Xu /* Optimize the common listener case. */ 1658f491069SHerbert Xu struct sock *__inet_lookup_listener(struct inet_hashinfo *hashinfo, 166fb99c848SAl Viro const __be32 daddr, const unsigned short hnum, 16799a92ff5SHerbert Xu const int dif) 16899a92ff5SHerbert Xu { 16999a92ff5SHerbert Xu struct sock *sk = NULL; 17099a92ff5SHerbert Xu const struct hlist_head *head; 17199a92ff5SHerbert Xu 17299a92ff5SHerbert Xu read_lock(&hashinfo->lhash_lock); 17399a92ff5SHerbert Xu head = &hashinfo->listening_hash[inet_lhashfn(hnum)]; 17499a92ff5SHerbert Xu if (!hlist_empty(head)) { 17599a92ff5SHerbert Xu const struct inet_sock *inet = inet_sk((sk = __sk_head(head))); 17699a92ff5SHerbert Xu 17799a92ff5SHerbert Xu if (inet->num == hnum && !sk->sk_node.next && 17899a92ff5SHerbert Xu (!inet->rcv_saddr || inet->rcv_saddr == daddr) && 17999a92ff5SHerbert Xu (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) && 18099a92ff5SHerbert Xu !sk->sk_bound_dev_if) 18199a92ff5SHerbert Xu goto sherry_cache; 1828f491069SHerbert Xu sk = inet_lookup_listener_slow(head, daddr, hnum, dif); 18399a92ff5SHerbert Xu } 18499a92ff5SHerbert Xu if (sk) { 18599a92ff5SHerbert Xu sherry_cache: 18699a92ff5SHerbert Xu sock_hold(sk); 18799a92ff5SHerbert Xu } 18899a92ff5SHerbert Xu read_unlock(&hashinfo->lhash_lock); 18999a92ff5SHerbert Xu return sk; 19099a92ff5SHerbert Xu } 1918f491069SHerbert Xu EXPORT_SYMBOL_GPL(__inet_lookup_listener); 192a7f5e7f1SArnaldo Carvalho de Melo 193a7f5e7f1SArnaldo Carvalho de Melo /* called with local bh disabled */ 194a7f5e7f1SArnaldo Carvalho de Melo static int __inet_check_established(struct inet_timewait_death_row *death_row, 195a7f5e7f1SArnaldo Carvalho de Melo struct sock *sk, __u16 lport, 196a7f5e7f1SArnaldo Carvalho de Melo struct inet_timewait_sock **twp) 197a7f5e7f1SArnaldo Carvalho de Melo { 198a7f5e7f1SArnaldo Carvalho de Melo struct inet_hashinfo *hinfo = death_row->hashinfo; 199a7f5e7f1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 200fb99c848SAl Viro __be32 daddr = inet->rcv_saddr; 201fb99c848SAl Viro __be32 saddr = inet->daddr; 202a7f5e7f1SArnaldo Carvalho de Melo int dif = sk->sk_bound_dev_if; 203a7f5e7f1SArnaldo Carvalho de Melo INET_ADDR_COOKIE(acookie, saddr, daddr) 2044f765d84SAl Viro const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport); 205a7f5e7f1SArnaldo Carvalho de Melo unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport); 206a7f5e7f1SArnaldo Carvalho de Melo struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash); 207230140cfSEric Dumazet rwlock_t *lock = inet_ehash_lockp(hinfo, hash); 208a7f5e7f1SArnaldo Carvalho de Melo struct sock *sk2; 209a7f5e7f1SArnaldo Carvalho de Melo const struct hlist_node *node; 210a7f5e7f1SArnaldo Carvalho de Melo struct inet_timewait_sock *tw; 211a7f5e7f1SArnaldo Carvalho de Melo 212a7f5e7f1SArnaldo Carvalho de Melo prefetch(head->chain.first); 213230140cfSEric Dumazet write_lock(lock); 214a7f5e7f1SArnaldo Carvalho de Melo 215a7f5e7f1SArnaldo Carvalho de Melo /* Check TIME-WAIT sockets first. */ 216dbca9b27SEric Dumazet sk_for_each(sk2, node, &head->twchain) { 217a7f5e7f1SArnaldo Carvalho de Melo tw = inet_twsk(sk2); 218a7f5e7f1SArnaldo Carvalho de Melo 219a7f5e7f1SArnaldo Carvalho de Melo if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) { 220a7f5e7f1SArnaldo Carvalho de Melo if (twsk_unique(sk, sk2, twp)) 221a7f5e7f1SArnaldo Carvalho de Melo goto unique; 222a7f5e7f1SArnaldo Carvalho de Melo else 223a7f5e7f1SArnaldo Carvalho de Melo goto not_unique; 224a7f5e7f1SArnaldo Carvalho de Melo } 225a7f5e7f1SArnaldo Carvalho de Melo } 226a7f5e7f1SArnaldo Carvalho de Melo tw = NULL; 227a7f5e7f1SArnaldo Carvalho de Melo 228a7f5e7f1SArnaldo Carvalho de Melo /* And established part... */ 229a7f5e7f1SArnaldo Carvalho de Melo sk_for_each(sk2, node, &head->chain) { 230a7f5e7f1SArnaldo Carvalho de Melo if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) 231a7f5e7f1SArnaldo Carvalho de Melo goto not_unique; 232a7f5e7f1SArnaldo Carvalho de Melo } 233a7f5e7f1SArnaldo Carvalho de Melo 234a7f5e7f1SArnaldo Carvalho de Melo unique: 235a7f5e7f1SArnaldo Carvalho de Melo /* Must record num and sport now. Otherwise we will see 236a7f5e7f1SArnaldo Carvalho de Melo * in hash table socket with a funny identity. */ 237a7f5e7f1SArnaldo Carvalho de Melo inet->num = lport; 238a7f5e7f1SArnaldo Carvalho de Melo inet->sport = htons(lport); 239a7f5e7f1SArnaldo Carvalho de Melo sk->sk_hash = hash; 240a7f5e7f1SArnaldo Carvalho de Melo BUG_TRAP(sk_unhashed(sk)); 241a7f5e7f1SArnaldo Carvalho de Melo __sk_add_node(sk, &head->chain); 242a7f5e7f1SArnaldo Carvalho de Melo sock_prot_inc_use(sk->sk_prot); 243230140cfSEric Dumazet write_unlock(lock); 244a7f5e7f1SArnaldo Carvalho de Melo 245a7f5e7f1SArnaldo Carvalho de Melo if (twp) { 246a7f5e7f1SArnaldo Carvalho de Melo *twp = tw; 247a7f5e7f1SArnaldo Carvalho de Melo NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED); 248a7f5e7f1SArnaldo Carvalho de Melo } else if (tw) { 249a7f5e7f1SArnaldo Carvalho de Melo /* Silly. Should hash-dance instead... */ 250a7f5e7f1SArnaldo Carvalho de Melo inet_twsk_deschedule(tw, death_row); 251a7f5e7f1SArnaldo Carvalho de Melo NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED); 252a7f5e7f1SArnaldo Carvalho de Melo 253a7f5e7f1SArnaldo Carvalho de Melo inet_twsk_put(tw); 254a7f5e7f1SArnaldo Carvalho de Melo } 255a7f5e7f1SArnaldo Carvalho de Melo 256a7f5e7f1SArnaldo Carvalho de Melo return 0; 257a7f5e7f1SArnaldo Carvalho de Melo 258a7f5e7f1SArnaldo Carvalho de Melo not_unique: 259230140cfSEric Dumazet write_unlock(lock); 260a7f5e7f1SArnaldo Carvalho de Melo return -EADDRNOTAVAIL; 261a7f5e7f1SArnaldo Carvalho de Melo } 262a7f5e7f1SArnaldo Carvalho de Melo 263a7f5e7f1SArnaldo Carvalho de Melo static inline u32 inet_sk_port_offset(const struct sock *sk) 264a7f5e7f1SArnaldo Carvalho de Melo { 265a7f5e7f1SArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 266a7f5e7f1SArnaldo Carvalho de Melo return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr, 267a7f5e7f1SArnaldo Carvalho de Melo inet->dport); 268a7f5e7f1SArnaldo Carvalho de Melo } 269a7f5e7f1SArnaldo Carvalho de Melo 270*152da81dSPavel Emelyanov void __inet_hash_nolisten(struct inet_hashinfo *hashinfo, struct sock *sk) 271*152da81dSPavel Emelyanov { 272*152da81dSPavel Emelyanov struct hlist_head *list; 273*152da81dSPavel Emelyanov rwlock_t *lock; 274*152da81dSPavel Emelyanov struct inet_ehash_bucket *head; 275*152da81dSPavel Emelyanov 276*152da81dSPavel Emelyanov BUG_TRAP(sk_unhashed(sk)); 277*152da81dSPavel Emelyanov 278*152da81dSPavel Emelyanov sk->sk_hash = inet_sk_ehashfn(sk); 279*152da81dSPavel Emelyanov head = inet_ehash_bucket(hashinfo, sk->sk_hash); 280*152da81dSPavel Emelyanov list = &head->chain; 281*152da81dSPavel Emelyanov lock = inet_ehash_lockp(hashinfo, sk->sk_hash); 282*152da81dSPavel Emelyanov 283*152da81dSPavel Emelyanov write_lock(lock); 284*152da81dSPavel Emelyanov __sk_add_node(sk, list); 285*152da81dSPavel Emelyanov sock_prot_inc_use(sk->sk_prot); 286*152da81dSPavel Emelyanov write_unlock(lock); 287*152da81dSPavel Emelyanov } 288*152da81dSPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_hash_nolisten); 289*152da81dSPavel Emelyanov 290*152da81dSPavel Emelyanov void __inet_hash(struct inet_hashinfo *hashinfo, struct sock *sk) 291*152da81dSPavel Emelyanov { 292*152da81dSPavel Emelyanov struct hlist_head *list; 293*152da81dSPavel Emelyanov rwlock_t *lock; 294*152da81dSPavel Emelyanov 295*152da81dSPavel Emelyanov if (sk->sk_state != TCP_LISTEN) { 296*152da81dSPavel Emelyanov __inet_hash_nolisten(hashinfo, sk); 297*152da81dSPavel Emelyanov return; 298*152da81dSPavel Emelyanov } 299*152da81dSPavel Emelyanov 300*152da81dSPavel Emelyanov BUG_TRAP(sk_unhashed(sk)); 301*152da81dSPavel Emelyanov list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; 302*152da81dSPavel Emelyanov lock = &hashinfo->lhash_lock; 303*152da81dSPavel Emelyanov 304*152da81dSPavel Emelyanov inet_listen_wlock(hashinfo); 305*152da81dSPavel Emelyanov __sk_add_node(sk, list); 306*152da81dSPavel Emelyanov sock_prot_inc_use(sk->sk_prot); 307*152da81dSPavel Emelyanov write_unlock(lock); 308*152da81dSPavel Emelyanov wake_up(&hashinfo->lhash_wait); 309*152da81dSPavel Emelyanov } 310*152da81dSPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_hash); 311*152da81dSPavel Emelyanov 312a7f5e7f1SArnaldo Carvalho de Melo /* 313a7f5e7f1SArnaldo Carvalho de Melo * Bind a port for a connect operation and hash it. 314a7f5e7f1SArnaldo Carvalho de Melo */ 315a7f5e7f1SArnaldo Carvalho de Melo int inet_hash_connect(struct inet_timewait_death_row *death_row, 316a7f5e7f1SArnaldo Carvalho de Melo struct sock *sk) 317a7f5e7f1SArnaldo Carvalho de Melo { 318a7f5e7f1SArnaldo Carvalho de Melo struct inet_hashinfo *hinfo = death_row->hashinfo; 319a7f5e7f1SArnaldo Carvalho de Melo const unsigned short snum = inet_sk(sk)->num; 320a7f5e7f1SArnaldo Carvalho de Melo struct inet_bind_hashbucket *head; 321a7f5e7f1SArnaldo Carvalho de Melo struct inet_bind_bucket *tb; 322a7f5e7f1SArnaldo Carvalho de Melo int ret; 323a7f5e7f1SArnaldo Carvalho de Melo 324a7f5e7f1SArnaldo Carvalho de Melo if (!snum) { 325227b60f5SStephen Hemminger int i, remaining, low, high, port; 326a7f5e7f1SArnaldo Carvalho de Melo static u32 hint; 327a7f5e7f1SArnaldo Carvalho de Melo u32 offset = hint + inet_sk_port_offset(sk); 328a7f5e7f1SArnaldo Carvalho de Melo struct hlist_node *node; 329a7f5e7f1SArnaldo Carvalho de Melo struct inet_timewait_sock *tw = NULL; 330a7f5e7f1SArnaldo Carvalho de Melo 331227b60f5SStephen Hemminger inet_get_local_port_range(&low, &high); 332a25de534SAnton Arapov remaining = (high - low) + 1; 333227b60f5SStephen Hemminger 334a7f5e7f1SArnaldo Carvalho de Melo local_bh_disable(); 335227b60f5SStephen Hemminger for (i = 1; i <= remaining; i++) { 336227b60f5SStephen Hemminger port = low + (i + offset) % remaining; 337a7f5e7f1SArnaldo Carvalho de Melo head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)]; 338a7f5e7f1SArnaldo Carvalho de Melo spin_lock(&head->lock); 339a7f5e7f1SArnaldo Carvalho de Melo 340a7f5e7f1SArnaldo Carvalho de Melo /* Does not bother with rcv_saddr checks, 341a7f5e7f1SArnaldo Carvalho de Melo * because the established check is already 342a7f5e7f1SArnaldo Carvalho de Melo * unique enough. 343a7f5e7f1SArnaldo Carvalho de Melo */ 344a7f5e7f1SArnaldo Carvalho de Melo inet_bind_bucket_for_each(tb, node, &head->chain) { 345a7f5e7f1SArnaldo Carvalho de Melo if (tb->port == port) { 346a7f5e7f1SArnaldo Carvalho de Melo BUG_TRAP(!hlist_empty(&tb->owners)); 347a7f5e7f1SArnaldo Carvalho de Melo if (tb->fastreuse >= 0) 348a7f5e7f1SArnaldo Carvalho de Melo goto next_port; 349a7f5e7f1SArnaldo Carvalho de Melo if (!__inet_check_established(death_row, 350a7f5e7f1SArnaldo Carvalho de Melo sk, port, 351a7f5e7f1SArnaldo Carvalho de Melo &tw)) 352a7f5e7f1SArnaldo Carvalho de Melo goto ok; 353a7f5e7f1SArnaldo Carvalho de Melo goto next_port; 354a7f5e7f1SArnaldo Carvalho de Melo } 355a7f5e7f1SArnaldo Carvalho de Melo } 356a7f5e7f1SArnaldo Carvalho de Melo 357a7f5e7f1SArnaldo Carvalho de Melo tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep, head, port); 358a7f5e7f1SArnaldo Carvalho de Melo if (!tb) { 359a7f5e7f1SArnaldo Carvalho de Melo spin_unlock(&head->lock); 360a7f5e7f1SArnaldo Carvalho de Melo break; 361a7f5e7f1SArnaldo Carvalho de Melo } 362a7f5e7f1SArnaldo Carvalho de Melo tb->fastreuse = -1; 363a7f5e7f1SArnaldo Carvalho de Melo goto ok; 364a7f5e7f1SArnaldo Carvalho de Melo 365a7f5e7f1SArnaldo Carvalho de Melo next_port: 366a7f5e7f1SArnaldo Carvalho de Melo spin_unlock(&head->lock); 367a7f5e7f1SArnaldo Carvalho de Melo } 368a7f5e7f1SArnaldo Carvalho de Melo local_bh_enable(); 369a7f5e7f1SArnaldo Carvalho de Melo 370a7f5e7f1SArnaldo Carvalho de Melo return -EADDRNOTAVAIL; 371a7f5e7f1SArnaldo Carvalho de Melo 372a7f5e7f1SArnaldo Carvalho de Melo ok: 373a7f5e7f1SArnaldo Carvalho de Melo hint += i; 374a7f5e7f1SArnaldo Carvalho de Melo 375a7f5e7f1SArnaldo Carvalho de Melo /* Head lock still held and bh's disabled */ 376a7f5e7f1SArnaldo Carvalho de Melo inet_bind_hash(sk, tb, port); 377a7f5e7f1SArnaldo Carvalho de Melo if (sk_unhashed(sk)) { 378a7f5e7f1SArnaldo Carvalho de Melo inet_sk(sk)->sport = htons(port); 379*152da81dSPavel Emelyanov __inet_hash_nolisten(hinfo, sk); 380a7f5e7f1SArnaldo Carvalho de Melo } 381a7f5e7f1SArnaldo Carvalho de Melo spin_unlock(&head->lock); 382a7f5e7f1SArnaldo Carvalho de Melo 383a7f5e7f1SArnaldo Carvalho de Melo if (tw) { 38453b3531bSAlexey Dobriyan inet_twsk_deschedule(tw, death_row); 385a7f5e7f1SArnaldo Carvalho de Melo inet_twsk_put(tw); 386a7f5e7f1SArnaldo Carvalho de Melo } 387a7f5e7f1SArnaldo Carvalho de Melo 388a7f5e7f1SArnaldo Carvalho de Melo ret = 0; 389a7f5e7f1SArnaldo Carvalho de Melo goto out; 390a7f5e7f1SArnaldo Carvalho de Melo } 391a7f5e7f1SArnaldo Carvalho de Melo 392a7f5e7f1SArnaldo Carvalho de Melo head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)]; 393a7f5e7f1SArnaldo Carvalho de Melo tb = inet_csk(sk)->icsk_bind_hash; 394a7f5e7f1SArnaldo Carvalho de Melo spin_lock_bh(&head->lock); 395a7f5e7f1SArnaldo Carvalho de Melo if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) { 396*152da81dSPavel Emelyanov __inet_hash_nolisten(hinfo, sk); 397a7f5e7f1SArnaldo Carvalho de Melo spin_unlock_bh(&head->lock); 398a7f5e7f1SArnaldo Carvalho de Melo return 0; 399a7f5e7f1SArnaldo Carvalho de Melo } else { 400a7f5e7f1SArnaldo Carvalho de Melo spin_unlock(&head->lock); 401a7f5e7f1SArnaldo Carvalho de Melo /* No definite answer... Walk to established hash table */ 402a7f5e7f1SArnaldo Carvalho de Melo ret = __inet_check_established(death_row, sk, snum, NULL); 403a7f5e7f1SArnaldo Carvalho de Melo out: 404a7f5e7f1SArnaldo Carvalho de Melo local_bh_enable(); 405a7f5e7f1SArnaldo Carvalho de Melo return ret; 406a7f5e7f1SArnaldo Carvalho de Melo } 407a7f5e7f1SArnaldo Carvalho de Melo } 408a7f5e7f1SArnaldo Carvalho de Melo 409a7f5e7f1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_hash_connect); 410