This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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/random.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/wait.h>
22
23 #include <net/inet_connection_sock.h>
24 #include <net/inet_hashtables.h>
25 #include <net/ip.h>
26
27 /*
28  * Allocate and initialize a new local port bind bucket.
29  * The bindhash mutex for snum's hash chain must be held here.
30  */
31 struct inet_bind_bucket *inet_bind_bucket_create(kmem_cache_t *cachep,
32                                                  struct inet_bind_hashbucket *head,
33                                                  const unsigned short snum)
34 {
35         struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
36
37         if (tb != NULL) {
38                 tb->port      = snum;
39                 tb->fastreuse = 0;
40                 INIT_HLIST_HEAD(&tb->owners);
41                 hlist_add_head(&tb->node, &head->chain);
42         }
43         return tb;
44 }
45
46 EXPORT_SYMBOL(inet_bind_bucket_create);
47
48 /*
49  * Caller must hold hashbucket lock for this tb with local BH disabled
50  */
51 void inet_bind_bucket_destroy(kmem_cache_t *cachep, struct inet_bind_bucket *tb)
52 {
53         if (hlist_empty(&tb->owners)) {
54                 __hlist_del(&tb->node);
55                 kmem_cache_free(cachep, tb);
56         }
57 }
58
59 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
60                     const unsigned short snum)
61 {
62         inet_sk(sk)->num = snum;
63         sk_add_bind_node(sk, &tb->owners);
64         inet_csk(sk)->icsk_bind_hash = tb;
65 }
66
67 EXPORT_SYMBOL(inet_bind_hash);
68
69 /*
70  * Get rid of any references to a local port held by the given sock.
71  */
72 static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
73 {
74         const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size);
75         struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
76         struct inet_bind_bucket *tb;
77
78         spin_lock(&head->lock);
79         tb = inet_csk(sk)->icsk_bind_hash;
80         __sk_del_bind_node(sk);
81         inet_csk(sk)->icsk_bind_hash = NULL;
82         inet_sk(sk)->num = 0;
83         inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
84         spin_unlock(&head->lock);
85 }
86
87 void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
88 {
89         local_bh_disable();
90         __inet_put_port(hashinfo, sk);
91         local_bh_enable();
92 }
93
94 EXPORT_SYMBOL(inet_put_port);
95
96 /*
97  * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
98  * Look, when several writers sleep and reader wakes them up, all but one
99  * immediately hit write lock and grab all the cpus. Exclusive sleep solves
100  * this, _but_ remember, it adds useless work on UP machines (wake up each
101  * exclusive lock release). It should be ifdefed really.
102  */
103 void inet_listen_wlock(struct inet_hashinfo *hashinfo)
104 {
105         write_lock(&hashinfo->lhash_lock);
106
107         if (atomic_read(&hashinfo->lhash_users)) {
108                 DEFINE_WAIT(wait);
109
110                 for (;;) {
111                         prepare_to_wait_exclusive(&hashinfo->lhash_wait,
112                                                   &wait, TASK_UNINTERRUPTIBLE);
113                         if (!atomic_read(&hashinfo->lhash_users))
114                                 break;
115                         write_unlock_bh(&hashinfo->lhash_lock);
116                         schedule();
117                         write_lock_bh(&hashinfo->lhash_lock);
118                 }
119
120                 finish_wait(&hashinfo->lhash_wait, &wait);
121         }
122 }
123
124 EXPORT_SYMBOL(inet_listen_wlock);
125
126 /*
127  * Don't inline this cruft. Here are some nice properties to exploit here. The
128  * BSD API does not allow a listening sock to specify the remote port nor the
129  * remote address for the connection. So always assume those are both
130  * wildcarded during the search since they can never be otherwise.
131  */
132 struct sock *__inet_lookup_listener(const struct hlist_head *head, const u32 daddr,
133                                     const unsigned short hnum, const int dif)
134 {
135         struct sock *result = NULL, *sk;
136         const struct hlist_node *node;
137         int hiscore = -1;
138
139         sk_for_each(sk, node, head) {
140                 const struct inet_sock *inet = inet_sk(sk);
141
142                 if (inet->num == hnum && !ipv6_only_sock(sk)) {
143                         const __u32 rcv_saddr = inet->rcv_saddr;
144                         int score = sk->sk_family == PF_INET ? 1 : 0;
145
146                         if (inet_addr_match(sk->sk_nx_info, daddr, rcv_saddr))
147                                 score += 2;
148                         else
149                                 continue;
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 }
165
166 EXPORT_SYMBOL_GPL(__inet_lookup_listener);
167
168 /* called with local bh disabled */
169 static int __inet_check_established(struct inet_timewait_death_row *death_row,
170                                     struct sock *sk, __u16 lport,
171                                     struct inet_timewait_sock **twp)
172 {
173         struct inet_hashinfo *hinfo = death_row->hashinfo;
174         struct inet_sock *inet = inet_sk(sk);
175         u32 daddr = inet->rcv_saddr;
176         u32 saddr = inet->daddr;
177         int dif = sk->sk_bound_dev_if;
178         INET_ADDR_COOKIE(acookie, saddr, daddr)
179         const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
180         unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
181         struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
182         struct sock *sk2;
183         const struct hlist_node *node;
184         struct inet_timewait_sock *tw;
185
186         prefetch(head->chain.first);
187         write_lock(&head->lock);
188
189         /* Check TIME-WAIT sockets first. */
190         sk_for_each(sk2, node, &(head + hinfo->ehash_size)->chain) {
191                 tw = inet_twsk(sk2);
192
193                 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) {
194                         if (twsk_unique(sk, sk2, twp))
195                                 goto unique;
196                         else
197                                 goto not_unique;
198                 }
199         }
200         tw = NULL;
201
202         /* And established part... */
203         sk_for_each(sk2, node, &head->chain) {
204                 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
205                         goto not_unique;
206         }
207
208 unique:
209         /* Must record num and sport now. Otherwise we will see
210          * in hash table socket with a funny identity. */
211         inet->num = lport;
212         inet->sport = htons(lport);
213         sk->sk_hash = hash;
214         BUG_TRAP(sk_unhashed(sk));
215         __sk_add_node(sk, &head->chain);
216         sock_prot_inc_use(sk->sk_prot);
217         write_unlock(&head->lock);
218
219         if (twp) {
220                 *twp = tw;
221                 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
222         } else if (tw) {
223                 /* Silly. Should hash-dance instead... */
224                 inet_twsk_deschedule(tw, death_row);
225                 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
226
227                 inet_twsk_put(tw);
228         }
229
230         return 0;
231
232 not_unique:
233         write_unlock(&head->lock);
234         return -EADDRNOTAVAIL;
235 }
236
237 static inline u32 inet_sk_port_offset(const struct sock *sk)
238 {
239         const struct inet_sock *inet = inet_sk(sk);
240         return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr, 
241                                           inet->dport);
242 }
243
244 /*
245  * Bind a port for a connect operation and hash it.
246  */
247 int inet_hash_connect(struct inet_timewait_death_row *death_row,
248                       struct sock *sk)
249 {
250         struct inet_hashinfo *hinfo = death_row->hashinfo;
251         const unsigned short snum = inet_sk(sk)->num;
252         struct inet_bind_hashbucket *head;
253         struct inet_bind_bucket *tb;
254         int ret;
255
256         if (!snum) {
257                 int low = sysctl_local_port_range[0];
258                 int high = sysctl_local_port_range[1];
259                 int range = high - low;
260                 int i;
261                 int port;
262                 static u32 hint;
263                 u32 offset = hint + inet_sk_port_offset(sk);
264                 struct hlist_node *node;
265                 struct inet_timewait_sock *tw = NULL;
266
267                 local_bh_disable();
268                 for (i = 1; i <= range; i++) {
269                         port = low + (i + offset) % range;
270                         head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
271                         spin_lock(&head->lock);
272
273                         /* Does not bother with rcv_saddr checks,
274                          * because the established check is already
275                          * unique enough.
276                          */
277                         inet_bind_bucket_for_each(tb, node, &head->chain) {
278                                 if (tb->port == port) {
279                                         BUG_TRAP(!hlist_empty(&tb->owners));
280                                         if (tb->fastreuse >= 0)
281                                                 goto next_port;
282                                         if (!__inet_check_established(death_row,
283                                                                       sk, port,
284                                                                       &tw))
285                                                 goto ok;
286                                         goto next_port;
287                                 }
288                         }
289
290                         tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep, head, port);
291                         if (!tb) {
292                                 spin_unlock(&head->lock);
293                                 break;
294                         }
295                         tb->fastreuse = -1;
296                         goto ok;
297
298                 next_port:
299                         spin_unlock(&head->lock);
300                 }
301                 local_bh_enable();
302
303                 return -EADDRNOTAVAIL;
304
305 ok:
306                 hint += i;
307
308                 /* Head lock still held and bh's disabled */
309                 inet_bind_hash(sk, tb, port);
310                 if (sk_unhashed(sk)) {
311                         inet_sk(sk)->sport = htons(port);
312                         __inet_hash(hinfo, sk, 0);
313                 }
314                 spin_unlock(&head->lock);
315
316                 if (tw) {
317                         inet_twsk_deschedule(tw, death_row);;
318                         inet_twsk_put(tw);
319                 }
320
321                 ret = 0;
322                 goto out;
323         }
324
325         head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
326         tb  = inet_csk(sk)->icsk_bind_hash;
327         spin_lock_bh(&head->lock);
328         if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
329                 __inet_hash(hinfo, sk, 0);
330                 spin_unlock_bh(&head->lock);
331                 return 0;
332         } else {
333                 spin_unlock(&head->lock);
334                 /* No definite answer... Walk to established hash table */
335                 ret = __inet_check_established(death_row, sk, snum, NULL);
336 out:
337                 local_bh_enable();
338                 return ret;
339         }
340 }
341
342 EXPORT_SYMBOL_GPL(inet_hash_connect);