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/config.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/wait.h>
22 #include <net/inet_hashtables.h>
25 * Allocate and initialize a new local port bind bucket.
26 * The bindhash mutex for snum's hash chain must be held here.
28 struct inet_bind_bucket *inet_bind_bucket_create(kmem_cache_t *cachep,
29 struct inet_bind_hashbucket *head,
30 const unsigned short snum)
32 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
37 INIT_HLIST_HEAD(&tb->owners);
38 hlist_add_head(&tb->node, &head->chain);
43 EXPORT_SYMBOL(inet_bind_bucket_create);
46 * Caller must hold hashbucket lock for this tb with local BH disabled
48 void inet_bind_bucket_destroy(kmem_cache_t *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 struct inet_sock *inet = inet_sk(sk);
61 sk_add_bind_node(sk, &tb->owners);
65 EXPORT_SYMBOL(inet_bind_hash);
68 * Get rid of any references to a local port held by the given sock.
70 static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
72 struct inet_sock *inet = inet_sk(sk);
73 const int bhash = inet_bhashfn(inet->num, hashinfo->bhash_size);
74 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
75 struct inet_bind_bucket *tb;
77 spin_lock(&head->lock);
79 __sk_del_bind_node(sk);
80 inet->bind_hash = NULL;
82 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
83 spin_unlock(&head->lock);
86 void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
89 __inet_put_port(hashinfo, sk);
93 EXPORT_SYMBOL(inet_put_port);
96 * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
97 * Look, when several writers sleep and reader wakes them up, all but one
98 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
99 * this, _but_ remember, it adds useless work on UP machines (wake up each
100 * exclusive lock release). It should be ifdefed really.
102 void inet_listen_wlock(struct inet_hashinfo *hashinfo)
104 write_lock(&hashinfo->lhash_lock);
106 if (atomic_read(&hashinfo->lhash_users)) {
110 prepare_to_wait_exclusive(&hashinfo->lhash_wait,
111 &wait, TASK_UNINTERRUPTIBLE);
112 if (!atomic_read(&hashinfo->lhash_users))
114 write_unlock_bh(&hashinfo->lhash_lock);
116 write_lock_bh(&hashinfo->lhash_lock);
119 finish_wait(&hashinfo->lhash_wait, &wait);
123 EXPORT_SYMBOL(inet_listen_wlock);
126 * Don't inline this cruft. Here are some nice properties to exploit here. The
127 * BSD API does not allow a listening sock to specify the remote port nor the
128 * remote address for the connection. So always assume those are both
129 * wildcarded during the search since they can never be otherwise.
131 struct sock *__inet_lookup_listener(const struct hlist_head *head, const u32 daddr,
132 const unsigned short hnum, const int dif)
134 struct sock *result = NULL, *sk;
135 const struct hlist_node *node;
138 sk_for_each(sk, node, head) {
139 const struct inet_sock *inet = inet_sk(sk);
141 if (inet->num == hnum && !ipv6_only_sock(sk)) {
142 const __u32 rcv_saddr = inet->rcv_saddr;
143 int score = sk->sk_family == PF_INET ? 1 : 0;
146 if (rcv_saddr != daddr)
150 if (sk->sk_bound_dev_if) {
151 if (sk->sk_bound_dev_if != dif)
157 if (score > hiscore) {