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