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