[INET]: Generalise tcp_v4_lookup_listener
[pandora-kernel.git] / net / ipv4 / inet_hashtables.c
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.
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
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>
21
22 #include <net/inet_hashtables.h>
23
24 /*
25  * Allocate and initialize a new local port bind bucket.
26  * The bindhash mutex for snum's hash chain must be held here.
27  */
28 struct inet_bind_bucket *inet_bind_bucket_create(kmem_cache_t *cachep,
29                                                  struct inet_bind_hashbucket *head,
30                                                  const unsigned short snum)
31 {
32         struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
33
34         if (tb != NULL) {
35                 tb->port      = snum;
36                 tb->fastreuse = 0;
37                 INIT_HLIST_HEAD(&tb->owners);
38                 hlist_add_head(&tb->node, &head->chain);
39         }
40         return tb;
41 }
42
43 EXPORT_SYMBOL(inet_bind_bucket_create);
44
45 /*
46  * Caller must hold hashbucket lock for this tb with local BH disabled
47  */
48 void inet_bind_bucket_destroy(kmem_cache_t *cachep, struct inet_bind_bucket *tb)
49 {
50         if (hlist_empty(&tb->owners)) {
51                 __hlist_del(&tb->node);
52                 kmem_cache_free(cachep, tb);
53         }
54 }
55
56 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
57                     const unsigned short snum)
58 {
59         struct inet_sock *inet = inet_sk(sk);
60         inet->num       = snum;
61         sk_add_bind_node(sk, &tb->owners);
62         inet->bind_hash = tb;
63 }
64
65 EXPORT_SYMBOL(inet_bind_hash);
66
67 /*
68  * Get rid of any references to a local port held by the given sock.
69  */
70 static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
71 {
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;
76
77         spin_lock(&head->lock);
78         tb = inet->bind_hash;
79         __sk_del_bind_node(sk);
80         inet->bind_hash = NULL;
81         inet->num = 0;
82         inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
83         spin_unlock(&head->lock);
84 }
85
86 void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
87 {
88         local_bh_disable();
89         __inet_put_port(hashinfo, sk);
90         local_bh_enable();
91 }
92
93 EXPORT_SYMBOL(inet_put_port);
94
95 /*
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.
101  */
102 void inet_listen_wlock(struct inet_hashinfo *hashinfo)
103 {
104         write_lock(&hashinfo->lhash_lock);
105
106         if (atomic_read(&hashinfo->lhash_users)) {
107                 DEFINE_WAIT(wait);
108
109                 for (;;) {
110                         prepare_to_wait_exclusive(&hashinfo->lhash_wait,
111                                                   &wait, TASK_UNINTERRUPTIBLE);
112                         if (!atomic_read(&hashinfo->lhash_users))
113                                 break;
114                         write_unlock_bh(&hashinfo->lhash_lock);
115                         schedule();
116                         write_lock_bh(&hashinfo->lhash_lock);
117                 }
118
119                 finish_wait(&hashinfo->lhash_wait, &wait);
120         }
121 }
122
123 EXPORT_SYMBOL(inet_listen_wlock);
124
125 /*
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.
130  */
131 struct sock *__inet_lookup_listener(const struct hlist_head *head, const u32 daddr,
132                                     const unsigned short hnum, const int dif)
133 {
134         struct sock *result = NULL, *sk;
135         const struct hlist_node *node;
136         int hiscore = -1;
137
138         sk_for_each(sk, node, head) {
139                 const struct inet_sock *inet = inet_sk(sk);
140
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;
144
145                         if (rcv_saddr) {
146                                 if (rcv_saddr != daddr)
147                                         continue;
148                                 score += 2;
149                         }
150                         if (sk->sk_bound_dev_if) {
151                                 if (sk->sk_bound_dev_if != dif)
152                                         continue;
153                                 score += 2;
154                         }
155                         if (score == 5)
156                                 return sk;
157                         if (score > hiscore) {
158                                 hiscore = score;
159                                 result  = sk;
160                         }
161                 }
162         }
163         return result;
164 }