]> Git Repo - linux.git/blame - net/ipv4/inet_hashtables.c
soreuseport: TCP/IPv4 implementation
[linux.git] / net / ipv4 / inet_hashtables.c
CommitLineData
77d8bf9c
ACM
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
2d8c4ce5 16#include <linux/module.h>
a7f5e7f1 17#include <linux/random.h>
f3f05f70 18#include <linux/sched.h>
77d8bf9c 19#include <linux/slab.h>
f3f05f70 20#include <linux/wait.h>
77d8bf9c 21
463c84b9 22#include <net/inet_connection_sock.h>
77d8bf9c 23#include <net/inet_hashtables.h>
6e5714ea 24#include <net/secure_seq.h>
a7f5e7f1 25#include <net/ip.h>
77d8bf9c
ACM
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 */
e18b890b 31struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
941b1d22 32 struct net *net,
77d8bf9c
ACM
33 struct inet_bind_hashbucket *head,
34 const unsigned short snum)
35{
54e6ecb2 36 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
77d8bf9c
ACM
37
38 if (tb != NULL) {
7a9546ee 39 write_pnet(&tb->ib_net, hold_net(net));
77d8bf9c
ACM
40 tb->port = snum;
41 tb->fastreuse = 0;
da5e3630 42 tb->fastreuseport = 0;
a9d8f911 43 tb->num_owners = 0;
77d8bf9c
ACM
44 INIT_HLIST_HEAD(&tb->owners);
45 hlist_add_head(&tb->node, &head->chain);
46 }
47 return tb;
48}
49
77d8bf9c
ACM
50/*
51 * Caller must hold hashbucket lock for this tb with local BH disabled
52 */
e18b890b 53void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
77d8bf9c
ACM
54{
55 if (hlist_empty(&tb->owners)) {
56 __hlist_del(&tb->node);
7a9546ee 57 release_net(ib_net(tb));
77d8bf9c
ACM
58 kmem_cache_free(cachep, tb);
59 }
60}
2d8c4ce5
ACM
61
62void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
63 const unsigned short snum)
64{
a9d8f911
EP
65 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
66
24dd1fa1 67 atomic_inc(&hashinfo->bsockets);
a9d8f911 68
c720c7e8 69 inet_sk(sk)->inet_num = snum;
2d8c4ce5 70 sk_add_bind_node(sk, &tb->owners);
a9d8f911 71 tb->num_owners++;
463c84b9 72 inet_csk(sk)->icsk_bind_hash = tb;
2d8c4ce5
ACM
73}
74
2d8c4ce5
ACM
75/*
76 * Get rid of any references to a local port held by the given sock.
77 */
ab1e0a13 78static void __inet_put_port(struct sock *sk)
2d8c4ce5 79{
39d8cda7 80 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
c720c7e8 81 const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
7f635ab7 82 hashinfo->bhash_size);
2d8c4ce5
ACM
83 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
84 struct inet_bind_bucket *tb;
85
24dd1fa1 86 atomic_dec(&hashinfo->bsockets);
a9d8f911 87
2d8c4ce5 88 spin_lock(&head->lock);
463c84b9 89 tb = inet_csk(sk)->icsk_bind_hash;
2d8c4ce5 90 __sk_del_bind_node(sk);
a9d8f911 91 tb->num_owners--;
463c84b9 92 inet_csk(sk)->icsk_bind_hash = NULL;
c720c7e8 93 inet_sk(sk)->inet_num = 0;
2d8c4ce5
ACM
94 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
95 spin_unlock(&head->lock);
96}
97
ab1e0a13 98void inet_put_port(struct sock *sk)
2d8c4ce5
ACM
99{
100 local_bh_disable();
ab1e0a13 101 __inet_put_port(sk);
2d8c4ce5
ACM
102 local_bh_enable();
103}
2d8c4ce5 104EXPORT_SYMBOL(inet_put_port);
f3f05f70 105
093d2823 106int __inet_inherit_port(struct sock *sk, struct sock *child)
53083773
PE
107{
108 struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
093d2823
BS
109 unsigned short port = inet_sk(child)->inet_num;
110 const int bhash = inet_bhashfn(sock_net(sk), port,
7f635ab7 111 table->bhash_size);
53083773
PE
112 struct inet_bind_hashbucket *head = &table->bhash[bhash];
113 struct inet_bind_bucket *tb;
114
115 spin_lock(&head->lock);
116 tb = inet_csk(sk)->icsk_bind_hash;
093d2823
BS
117 if (tb->port != port) {
118 /* NOTE: using tproxy and redirecting skbs to a proxy
119 * on a different listener port breaks the assumption
120 * that the listener socket's icsk_bind_hash is the same
121 * as that of the child socket. We have to look up or
122 * create a new bind bucket for the child here. */
123 struct hlist_node *node;
124 inet_bind_bucket_for_each(tb, node, &head->chain) {
125 if (net_eq(ib_net(tb), sock_net(sk)) &&
126 tb->port == port)
127 break;
128 }
129 if (!node) {
130 tb = inet_bind_bucket_create(table->bind_bucket_cachep,
131 sock_net(sk), head, port);
132 if (!tb) {
133 spin_unlock(&head->lock);
134 return -ENOMEM;
135 }
136 }
137 }
b4ff3c90 138 inet_bind_hash(child, tb, port);
53083773 139 spin_unlock(&head->lock);
093d2823
BS
140
141 return 0;
53083773 142}
53083773
PE
143EXPORT_SYMBOL_GPL(__inet_inherit_port);
144
c25eb3bf
ED
145static inline int compute_score(struct sock *sk, struct net *net,
146 const unsigned short hnum, const __be32 daddr,
147 const int dif)
148{
149 int score = -1;
150 struct inet_sock *inet = inet_sk(sk);
151
c720c7e8 152 if (net_eq(sock_net(sk), net) && inet->inet_num == hnum &&
c25eb3bf 153 !ipv6_only_sock(sk)) {
c720c7e8 154 __be32 rcv_saddr = inet->inet_rcv_saddr;
da5e3630 155 score = sk->sk_family == PF_INET ? 2 : 1;
c25eb3bf
ED
156 if (rcv_saddr) {
157 if (rcv_saddr != daddr)
158 return -1;
da5e3630 159 score += 4;
c25eb3bf
ED
160 }
161 if (sk->sk_bound_dev_if) {
162 if (sk->sk_bound_dev_if != dif)
163 return -1;
da5e3630 164 score += 4;
c25eb3bf
ED
165 }
166 }
167 return score;
168}
169
33b62231
ACM
170/*
171 * Don't inline this cruft. Here are some nice properties to exploit here. The
172 * BSD API does not allow a listening sock to specify the remote port nor the
173 * remote address for the connection. So always assume those are both
174 * wildcarded during the search since they can never be otherwise.
175 */
e48c414e 176
c25eb3bf 177
c67499c0
PE
178struct sock *__inet_lookup_listener(struct net *net,
179 struct inet_hashinfo *hashinfo,
da5e3630 180 const __be32 saddr, __be16 sport,
fb99c848 181 const __be32 daddr, const unsigned short hnum,
8f491069 182 const int dif)
99a92ff5 183{
c25eb3bf
ED
184 struct sock *sk, *result;
185 struct hlist_nulls_node *node;
186 unsigned int hash = inet_lhashfn(net, hnum);
187 struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
da5e3630
TH
188 int score, hiscore, matches = 0, reuseport = 0;
189 u32 phash = 0;
99a92ff5 190
c25eb3bf
ED
191 rcu_read_lock();
192begin:
193 result = NULL;
da5e3630 194 hiscore = 0;
c25eb3bf
ED
195 sk_nulls_for_each_rcu(sk, node, &ilb->head) {
196 score = compute_score(sk, net, hnum, daddr, dif);
197 if (score > hiscore) {
198 result = sk;
199 hiscore = score;
da5e3630
TH
200 reuseport = sk->sk_reuseport;
201 if (reuseport) {
202 phash = inet_ehashfn(net, daddr, hnum,
203 saddr, sport);
204 matches = 1;
205 }
206 } else if (score == hiscore && reuseport) {
207 matches++;
208 if (((u64)phash * matches) >> 32 == 0)
209 result = sk;
210 phash = next_pseudo_random32(phash);
c25eb3bf 211 }
99a92ff5 212 }
c25eb3bf
ED
213 /*
214 * if the nulls value we got at the end of this lookup is
215 * not the expected one, we must restart lookup.
216 * We probably met an item that was moved to another chain.
217 */
218 if (get_nulls_value(node) != hash + LISTENING_NULLS_BASE)
219 goto begin;
220 if (result) {
221 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
222 result = NULL;
223 else if (unlikely(compute_score(result, net, hnum, daddr,
224 dif) < hiscore)) {
225 sock_put(result);
226 goto begin;
227 }
99a92ff5 228 }
c25eb3bf
ED
229 rcu_read_unlock();
230 return result;
99a92ff5 231}
8f491069 232EXPORT_SYMBOL_GPL(__inet_lookup_listener);
a7f5e7f1 233
5e73ea1a 234struct sock *__inet_lookup_established(struct net *net,
c67499c0 235 struct inet_hashinfo *hashinfo,
77a5ba55
PE
236 const __be32 saddr, const __be16 sport,
237 const __be32 daddr, const u16 hnum,
238 const int dif)
239{
240 INET_ADDR_COOKIE(acookie, saddr, daddr)
241 const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
242 struct sock *sk;
3ab5aee7 243 const struct hlist_nulls_node *node;
77a5ba55
PE
244 /* Optimize here for direct hit, only listening connections can
245 * have wildcards anyways.
246 */
9f26b3ad 247 unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
f373b53b 248 unsigned int slot = hash & hashinfo->ehash_mask;
3ab5aee7 249 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
77a5ba55 250
3ab5aee7
ED
251 rcu_read_lock();
252begin:
253 sk_nulls_for_each_rcu(sk, node, &head->chain) {
ce43b03e
ED
254 if (sk->sk_hash != hash)
255 continue;
256 if (likely(INET_MATCH(sk, net, acookie,
257 saddr, daddr, ports, dif))) {
3ab5aee7
ED
258 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
259 goto begintw;
ce43b03e
ED
260 if (unlikely(!INET_MATCH(sk, net, acookie,
261 saddr, daddr, ports, dif))) {
3ab5aee7
ED
262 sock_put(sk);
263 goto begin;
264 }
265 goto out;
266 }
77a5ba55 267 }
3ab5aee7
ED
268 /*
269 * if the nulls value we got at the end of this lookup is
270 * not the expected one, we must restart lookup.
271 * We probably met an item that was moved to another chain.
272 */
273 if (get_nulls_value(node) != slot)
274 goto begin;
77a5ba55 275
3ab5aee7 276begintw:
77a5ba55 277 /* Must check for a TIME_WAIT'er before going to listener hash. */
3ab5aee7 278 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
ce43b03e
ED
279 if (sk->sk_hash != hash)
280 continue;
281 if (likely(INET_TW_MATCH(sk, net, acookie,
282 saddr, daddr, ports,
283 dif))) {
3ab5aee7
ED
284 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
285 sk = NULL;
286 goto out;
287 }
ce43b03e
ED
288 if (unlikely(!INET_TW_MATCH(sk, net, acookie,
289 saddr, daddr, ports,
290 dif))) {
3ab5aee7
ED
291 sock_put(sk);
292 goto begintw;
293 }
294 goto out;
295 }
77a5ba55 296 }
3ab5aee7
ED
297 /*
298 * if the nulls value we got at the end of this lookup is
299 * not the expected one, we must restart lookup.
300 * We probably met an item that was moved to another chain.
301 */
302 if (get_nulls_value(node) != slot)
303 goto begintw;
77a5ba55
PE
304 sk = NULL;
305out:
3ab5aee7 306 rcu_read_unlock();
77a5ba55 307 return sk;
77a5ba55
PE
308}
309EXPORT_SYMBOL_GPL(__inet_lookup_established);
310
a7f5e7f1
ACM
311/* called with local bh disabled */
312static int __inet_check_established(struct inet_timewait_death_row *death_row,
313 struct sock *sk, __u16 lport,
314 struct inet_timewait_sock **twp)
315{
316 struct inet_hashinfo *hinfo = death_row->hashinfo;
317 struct inet_sock *inet = inet_sk(sk);
c720c7e8
ED
318 __be32 daddr = inet->inet_rcv_saddr;
319 __be32 saddr = inet->inet_daddr;
a7f5e7f1
ACM
320 int dif = sk->sk_bound_dev_if;
321 INET_ADDR_COOKIE(acookie, saddr, daddr)
c720c7e8 322 const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
9f26b3ad 323 struct net *net = sock_net(sk);
c720c7e8
ED
324 unsigned int hash = inet_ehashfn(net, daddr, lport,
325 saddr, inet->inet_dport);
a7f5e7f1 326 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
9db66bdc 327 spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
a7f5e7f1 328 struct sock *sk2;
3ab5aee7 329 const struct hlist_nulls_node *node;
a7f5e7f1 330 struct inet_timewait_sock *tw;
13475a30 331 int twrefcnt = 0;
a7f5e7f1 332
9db66bdc 333 spin_lock(lock);
a7f5e7f1
ACM
334
335 /* Check TIME-WAIT sockets first. */
3ab5aee7 336 sk_nulls_for_each(sk2, node, &head->twchain) {
ce43b03e
ED
337 if (sk2->sk_hash != hash)
338 continue;
a7f5e7f1 339
ce43b03e
ED
340 if (likely(INET_TW_MATCH(sk2, net, acookie,
341 saddr, daddr, ports, dif))) {
342 tw = inet_twsk(sk2);
a7f5e7f1
ACM
343 if (twsk_unique(sk, sk2, twp))
344 goto unique;
345 else
346 goto not_unique;
347 }
348 }
349 tw = NULL;
350
351 /* And established part... */
3ab5aee7 352 sk_nulls_for_each(sk2, node, &head->chain) {
ce43b03e
ED
353 if (sk2->sk_hash != hash)
354 continue;
355 if (likely(INET_MATCH(sk2, net, acookie,
356 saddr, daddr, ports, dif)))
a7f5e7f1
ACM
357 goto not_unique;
358 }
359
360unique:
361 /* Must record num and sport now. Otherwise we will see
362 * in hash table socket with a funny identity. */
c720c7e8
ED
363 inet->inet_num = lport;
364 inet->inet_sport = htons(lport);
a7f5e7f1 365 sk->sk_hash = hash;
547b792c 366 WARN_ON(!sk_unhashed(sk));
3ab5aee7 367 __sk_nulls_add_node_rcu(sk, &head->chain);
13475a30
ED
368 if (tw) {
369 twrefcnt = inet_twsk_unhash(tw);
370 NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
371 }
9db66bdc 372 spin_unlock(lock);
13475a30
ED
373 if (twrefcnt)
374 inet_twsk_put(tw);
c29a0bc4 375 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
a7f5e7f1
ACM
376
377 if (twp) {
378 *twp = tw;
a7f5e7f1
ACM
379 } else if (tw) {
380 /* Silly. Should hash-dance instead... */
381 inet_twsk_deschedule(tw, death_row);
a7f5e7f1
ACM
382
383 inet_twsk_put(tw);
384 }
a7f5e7f1
ACM
385 return 0;
386
387not_unique:
9db66bdc 388 spin_unlock(lock);
a7f5e7f1
ACM
389 return -EADDRNOTAVAIL;
390}
391
392static inline u32 inet_sk_port_offset(const struct sock *sk)
393{
394 const struct inet_sock *inet = inet_sk(sk);
c720c7e8
ED
395 return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
396 inet->inet_daddr,
397 inet->inet_dport);
a7f5e7f1
ACM
398}
399
9327f705 400int __inet_hash_nolisten(struct sock *sk, struct inet_timewait_sock *tw)
152da81d 401{
39d8cda7 402 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
3ab5aee7 403 struct hlist_nulls_head *list;
9db66bdc 404 spinlock_t *lock;
152da81d 405 struct inet_ehash_bucket *head;
9327f705 406 int twrefcnt = 0;
152da81d 407
547b792c 408 WARN_ON(!sk_unhashed(sk));
152da81d
PE
409
410 sk->sk_hash = inet_sk_ehashfn(sk);
411 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
412 list = &head->chain;
413 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
414
9db66bdc 415 spin_lock(lock);
3ab5aee7 416 __sk_nulls_add_node_rcu(sk, list);
9327f705
ED
417 if (tw) {
418 WARN_ON(sk->sk_hash != tw->tw_hash);
419 twrefcnt = inet_twsk_unhash(tw);
420 }
9db66bdc 421 spin_unlock(lock);
c29a0bc4 422 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
9327f705 423 return twrefcnt;
152da81d
PE
424}
425EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
426
ab1e0a13 427static void __inet_hash(struct sock *sk)
152da81d 428{
39d8cda7 429 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
5caea4ea 430 struct inet_listen_hashbucket *ilb;
152da81d
PE
431
432 if (sk->sk_state != TCP_LISTEN) {
9327f705 433 __inet_hash_nolisten(sk, NULL);
152da81d
PE
434 return;
435 }
436
547b792c 437 WARN_ON(!sk_unhashed(sk));
5caea4ea 438 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
152da81d 439
5caea4ea 440 spin_lock(&ilb->lock);
c25eb3bf 441 __sk_nulls_add_node_rcu(sk, &ilb->head);
c29a0bc4 442 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
5caea4ea 443 spin_unlock(&ilb->lock);
152da81d 444}
ab1e0a13
ACM
445
446void inet_hash(struct sock *sk)
447{
448 if (sk->sk_state != TCP_CLOSE) {
449 local_bh_disable();
450 __inet_hash(sk);
451 local_bh_enable();
452 }
453}
454EXPORT_SYMBOL_GPL(inet_hash);
455
456void inet_unhash(struct sock *sk)
457{
39d8cda7 458 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
c25eb3bf
ED
459 spinlock_t *lock;
460 int done;
ab1e0a13
ACM
461
462 if (sk_unhashed(sk))
5caea4ea 463 return;
ab1e0a13 464
c25eb3bf
ED
465 if (sk->sk_state == TCP_LISTEN)
466 lock = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)].lock;
467 else
468 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
5caea4ea 469
c25eb3bf
ED
470 spin_lock_bh(lock);
471 done =__sk_nulls_del_node_init_rcu(sk);
c25eb3bf
ED
472 if (done)
473 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
920de804 474 spin_unlock_bh(lock);
ab1e0a13
ACM
475}
476EXPORT_SYMBOL_GPL(inet_unhash);
152da81d 477
5ee31fc1 478int __inet_hash_connect(struct inet_timewait_death_row *death_row,
5d8c0aa9 479 struct sock *sk, u32 port_offset,
5ee31fc1
PE
480 int (*check_established)(struct inet_timewait_death_row *,
481 struct sock *, __u16, struct inet_timewait_sock **),
9327f705 482 int (*hash)(struct sock *sk, struct inet_timewait_sock *twp))
a7f5e7f1
ACM
483{
484 struct inet_hashinfo *hinfo = death_row->hashinfo;
c720c7e8 485 const unsigned short snum = inet_sk(sk)->inet_num;
e905a9ed
YH
486 struct inet_bind_hashbucket *head;
487 struct inet_bind_bucket *tb;
a7f5e7f1 488 int ret;
3b1e0a65 489 struct net *net = sock_net(sk);
9327f705 490 int twrefcnt = 1;
a7f5e7f1 491
e905a9ed 492 if (!snum) {
227b60f5 493 int i, remaining, low, high, port;
a7f5e7f1 494 static u32 hint;
5d8c0aa9 495 u32 offset = hint + port_offset;
a7f5e7f1 496 struct hlist_node *node;
e905a9ed 497 struct inet_timewait_sock *tw = NULL;
a7f5e7f1 498
227b60f5 499 inet_get_local_port_range(&low, &high);
a25de534 500 remaining = (high - low) + 1;
227b60f5 501
e905a9ed 502 local_bh_disable();
227b60f5
SH
503 for (i = 1; i <= remaining; i++) {
504 port = low + (i + offset) % remaining;
e3826f1e
AW
505 if (inet_is_reserved_local_port(port))
506 continue;
7f635ab7
PE
507 head = &hinfo->bhash[inet_bhashfn(net, port,
508 hinfo->bhash_size)];
e905a9ed 509 spin_lock(&head->lock);
a7f5e7f1 510
e905a9ed
YH
511 /* Does not bother with rcv_saddr checks,
512 * because the established check is already
513 * unique enough.
514 */
a7f5e7f1 515 inet_bind_bucket_for_each(tb, node, &head->chain) {
09ad9bc7
OP
516 if (net_eq(ib_net(tb), net) &&
517 tb->port == port) {
da5e3630
TH
518 if (tb->fastreuse >= 0 ||
519 tb->fastreuseport >= 0)
e905a9ed 520 goto next_port;
a9d8f911 521 WARN_ON(hlist_empty(&tb->owners));
5ee31fc1
PE
522 if (!check_established(death_row, sk,
523 port, &tw))
e905a9ed
YH
524 goto ok;
525 goto next_port;
526 }
527 }
528
941b1d22
PE
529 tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
530 net, head, port);
e905a9ed
YH
531 if (!tb) {
532 spin_unlock(&head->lock);
533 break;
534 }
535 tb->fastreuse = -1;
da5e3630 536 tb->fastreuseport = -1;
e905a9ed
YH
537 goto ok;
538
539 next_port:
540 spin_unlock(&head->lock);
541 }
542 local_bh_enable();
543
544 return -EADDRNOTAVAIL;
a7f5e7f1
ACM
545
546ok:
547 hint += i;
548
e905a9ed
YH
549 /* Head lock still held and bh's disabled */
550 inet_bind_hash(sk, tb, port);
a7f5e7f1 551 if (sk_unhashed(sk)) {
c720c7e8 552 inet_sk(sk)->inet_sport = htons(port);
9327f705 553 twrefcnt += hash(sk, tw);
e905a9ed 554 }
3cdaedae
ED
555 if (tw)
556 twrefcnt += inet_twsk_bind_unhash(tw, hinfo);
e905a9ed 557 spin_unlock(&head->lock);
a7f5e7f1 558
e905a9ed
YH
559 if (tw) {
560 inet_twsk_deschedule(tw, death_row);
9327f705
ED
561 while (twrefcnt) {
562 twrefcnt--;
563 inet_twsk_put(tw);
564 }
e905a9ed 565 }
a7f5e7f1
ACM
566
567 ret = 0;
568 goto out;
e905a9ed 569 }
a7f5e7f1 570
7f635ab7 571 head = &hinfo->bhash[inet_bhashfn(net, snum, hinfo->bhash_size)];
e905a9ed 572 tb = inet_csk(sk)->icsk_bind_hash;
a7f5e7f1
ACM
573 spin_lock_bh(&head->lock);
574 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
9327f705 575 hash(sk, NULL);
a7f5e7f1
ACM
576 spin_unlock_bh(&head->lock);
577 return 0;
578 } else {
579 spin_unlock(&head->lock);
580 /* No definite answer... Walk to established hash table */
5ee31fc1 581 ret = check_established(death_row, sk, snum, NULL);
a7f5e7f1
ACM
582out:
583 local_bh_enable();
584 return ret;
585 }
586}
5ee31fc1
PE
587
588/*
589 * Bind a port for a connect operation and hash it.
590 */
591int inet_hash_connect(struct inet_timewait_death_row *death_row,
592 struct sock *sk)
593{
5d8c0aa9 594 return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
5ee31fc1
PE
595 __inet_check_established, __inet_hash_nolisten);
596}
a7f5e7f1 597EXPORT_SYMBOL_GPL(inet_hash_connect);
5caea4ea
ED
598
599void inet_hashinfo_init(struct inet_hashinfo *h)
600{
601 int i;
602
24dd1fa1 603 atomic_set(&h->bsockets, 0);
c25eb3bf 604 for (i = 0; i < INET_LHTABLE_SIZE; i++) {
5caea4ea 605 spin_lock_init(&h->listening_hash[i].lock);
c25eb3bf
ED
606 INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].head,
607 i + LISTENING_NULLS_BASE);
608 }
5caea4ea 609}
5caea4ea 610EXPORT_SYMBOL_GPL(inet_hashinfo_init);
This page took 0.816867 seconds and 4 git commands to generate.