xref: /linux/net/ipv4/inet_hashtables.c (revision 77de28cd7cf172e782319a144bf64e693794d78b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Generic INET transport hashtables
8  *
9  * Authors:	Lotsa people, from code originally in tcp
10  */
11 
12 #include <linux/module.h>
13 #include <linux/random.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/wait.h>
17 #include <linux/vmalloc.h>
18 #include <linux/memblock.h>
19 #include <linux/gcd.h>
20 
21 #include <net/addrconf.h>
22 #include <net/inet_connection_sock.h>
23 #include <net/inet_hashtables.h>
24 #if IS_ENABLED(CONFIG_IPV6)
25 #include <net/inet6_hashtables.h>
26 #endif
27 #include <net/hotdata.h>
28 #include <net/ip.h>
29 #include <net/rps.h>
30 #include <net/secure_seq.h>
31 #include <net/sock_reuseport.h>
32 #include <net/tcp.h>
33 
34 static void inet_init_ehash_secret(void)
35 {
36 	net_get_random_sleepable_once(&inet_ehash_secret,
37 				      sizeof(inet_ehash_secret));
38 }
39 
40 u32 inet_ehashfn(const struct net *net, const __be32 laddr,
41 		 const __u16 lport, const __be32 faddr,
42 		 const __be16 fport)
43 {
44 	return lport + __inet_ehashfn(laddr, 0, faddr, fport,
45 				      inet_ehash_secret + net_hash_mix(net));
46 }
47 EXPORT_SYMBOL_GPL(inet_ehashfn);
48 
49 /* This function handles inet_sock, but also timewait and request sockets
50  * for IPv4/IPv6.
51  */
52 static u32 sk_ehashfn(const struct sock *sk)
53 {
54 #if IS_ENABLED(CONFIG_IPV6)
55 	if (sk->sk_family == AF_INET6 &&
56 	    !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
57 		return inet6_ehashfn(sock_net(sk),
58 				     &sk->sk_v6_rcv_saddr, sk->sk_num,
59 				     &sk->sk_v6_daddr, sk->sk_dport);
60 #endif
61 	return inet_ehashfn(sock_net(sk),
62 			    sk->sk_rcv_saddr, sk->sk_num,
63 			    sk->sk_daddr, sk->sk_dport);
64 }
65 
66 static bool sk_is_connect_bind(const struct sock *sk)
67 {
68 	if (sk->sk_state == TCP_TIME_WAIT)
69 		return inet_twsk(sk)->tw_connect_bind;
70 	else
71 		return sk->sk_userlocks & SOCK_CONNECT_BIND;
72 }
73 
74 /*
75  * Allocate and initialize a new local port bind bucket.
76  * The bindhash mutex for snum's hash chain must be held here.
77  */
78 struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
79 						 struct net *net,
80 						 struct inet_bind_hashbucket *head,
81 						 const unsigned short snum,
82 						 int l3mdev)
83 {
84 	struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
85 
86 	if (tb) {
87 		write_pnet(&tb->ib_net, net);
88 		tb->l3mdev    = l3mdev;
89 		tb->port      = snum;
90 		tb->fastreuse = 0;
91 		tb->fastreuseport = 0;
92 		INIT_HLIST_HEAD(&tb->bhash2);
93 		hlist_add_head_rcu(&tb->node, &head->chain);
94 	}
95 	return tb;
96 }
97 
98 /*
99  * Caller must hold hashbucket lock for this tb with local BH disabled
100  */
101 void inet_bind_bucket_destroy(struct inet_bind_bucket *tb)
102 {
103 	const struct inet_bind2_bucket *tb2;
104 
105 	if (hlist_empty(&tb->bhash2)) {
106 		hlist_del_rcu(&tb->node);
107 		kfree_rcu(tb, rcu);
108 		return;
109 	}
110 
111 	if (tb->fastreuse == -1 && tb->fastreuseport == -1)
112 		return;
113 	hlist_for_each_entry(tb2, &tb->bhash2, bhash_node) {
114 		if (tb2->fastreuse != -1 || tb2->fastreuseport != -1)
115 			return;
116 	}
117 	tb->fastreuse = -1;
118 	tb->fastreuseport = -1;
119 }
120 
121 bool inet_bind_bucket_match(const struct inet_bind_bucket *tb, const struct net *net,
122 			    unsigned short port, int l3mdev)
123 {
124 	return net_eq(ib_net(tb), net) && tb->port == port &&
125 		tb->l3mdev == l3mdev;
126 }
127 
128 static void inet_bind2_bucket_init(struct inet_bind2_bucket *tb2,
129 				   struct net *net,
130 				   struct inet_bind_hashbucket *head,
131 				   struct inet_bind_bucket *tb,
132 				   const struct sock *sk)
133 {
134 	write_pnet(&tb2->ib_net, net);
135 	tb2->l3mdev = tb->l3mdev;
136 	tb2->port = tb->port;
137 #if IS_ENABLED(CONFIG_IPV6)
138 	BUILD_BUG_ON(USHRT_MAX < (IPV6_ADDR_ANY | IPV6_ADDR_MAPPED));
139 	if (sk->sk_family == AF_INET6) {
140 		tb2->addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
141 		tb2->v6_rcv_saddr = sk->sk_v6_rcv_saddr;
142 	} else {
143 		tb2->addr_type = IPV6_ADDR_MAPPED;
144 		ipv6_addr_set_v4mapped(sk->sk_rcv_saddr, &tb2->v6_rcv_saddr);
145 	}
146 #else
147 	tb2->rcv_saddr = sk->sk_rcv_saddr;
148 #endif
149 	tb2->fastreuse = 0;
150 	tb2->fastreuseport = 0;
151 	INIT_HLIST_HEAD(&tb2->owners);
152 	hlist_add_head(&tb2->node, &head->chain);
153 	hlist_add_head(&tb2->bhash_node, &tb->bhash2);
154 }
155 
156 struct inet_bind2_bucket *inet_bind2_bucket_create(struct kmem_cache *cachep,
157 						   struct net *net,
158 						   struct inet_bind_hashbucket *head,
159 						   struct inet_bind_bucket *tb,
160 						   const struct sock *sk)
161 {
162 	struct inet_bind2_bucket *tb2 = kmem_cache_alloc(cachep, GFP_ATOMIC);
163 
164 	if (tb2)
165 		inet_bind2_bucket_init(tb2, net, head, tb, sk);
166 
167 	return tb2;
168 }
169 
170 /* Caller must hold hashbucket lock for this tb with local BH disabled */
171 void inet_bind2_bucket_destroy(struct kmem_cache *cachep, struct inet_bind2_bucket *tb)
172 {
173 	const struct sock *sk;
174 
175 	if (hlist_empty(&tb->owners)) {
176 		__hlist_del(&tb->node);
177 		__hlist_del(&tb->bhash_node);
178 		kmem_cache_free(cachep, tb);
179 		return;
180 	}
181 
182 	if (tb->fastreuse == -1 && tb->fastreuseport == -1)
183 		return;
184 	sk_for_each_bound(sk, &tb->owners) {
185 		if (!sk_is_connect_bind(sk))
186 			return;
187 	}
188 	tb->fastreuse = -1;
189 	tb->fastreuseport = -1;
190 }
191 
192 static bool inet_bind2_bucket_addr_match(const struct inet_bind2_bucket *tb2,
193 					 const struct sock *sk)
194 {
195 #if IS_ENABLED(CONFIG_IPV6)
196 	if (sk->sk_family == AF_INET6)
197 		return ipv6_addr_equal(&tb2->v6_rcv_saddr, &sk->sk_v6_rcv_saddr);
198 
199 	if (tb2->addr_type != IPV6_ADDR_MAPPED)
200 		return false;
201 #endif
202 	return tb2->rcv_saddr == sk->sk_rcv_saddr;
203 }
204 
205 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
206 		    struct inet_bind2_bucket *tb2, unsigned short port)
207 {
208 	WRITE_ONCE(inet_sk(sk)->inet_num, port);
209 	inet_csk(sk)->icsk_bind_hash = tb;
210 	inet_csk(sk)->icsk_bind2_hash = tb2;
211 	sk_add_bind_node(sk, &tb2->owners);
212 }
213 
214 /*
215  * Get rid of any references to a local port held by the given sock.
216  */
217 static void __inet_put_port(struct sock *sk)
218 {
219 	struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
220 	struct inet_bind_hashbucket *head, *head2;
221 	struct net *net = sock_net(sk);
222 	struct inet_bind_bucket *tb;
223 	int bhash;
224 
225 	bhash = inet_bhashfn(net, inet_sk(sk)->inet_num, hashinfo->bhash_size);
226 	head = &hashinfo->bhash[bhash];
227 	head2 = inet_bhashfn_portaddr(hashinfo, sk, net, inet_sk(sk)->inet_num);
228 
229 	spin_lock(&head->lock);
230 	tb = inet_csk(sk)->icsk_bind_hash;
231 	inet_csk(sk)->icsk_bind_hash = NULL;
232 	WRITE_ONCE(inet_sk(sk)->inet_num, 0);
233 	sk->sk_userlocks &= ~SOCK_CONNECT_BIND;
234 
235 	spin_lock(&head2->lock);
236 	if (inet_csk(sk)->icsk_bind2_hash) {
237 		struct inet_bind2_bucket *tb2 = inet_csk(sk)->icsk_bind2_hash;
238 
239 		__sk_del_bind_node(sk);
240 		inet_csk(sk)->icsk_bind2_hash = NULL;
241 		inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2);
242 	}
243 	spin_unlock(&head2->lock);
244 
245 	inet_bind_bucket_destroy(tb);
246 	spin_unlock(&head->lock);
247 }
248 
249 void inet_put_port(struct sock *sk)
250 {
251 	local_bh_disable();
252 	__inet_put_port(sk);
253 	local_bh_enable();
254 }
255 EXPORT_SYMBOL(inet_put_port);
256 
257 int __inet_inherit_port(const struct sock *sk, struct sock *child)
258 {
259 	struct inet_hashinfo *table = tcp_get_hashinfo(sk);
260 	unsigned short port = inet_sk(child)->inet_num;
261 	struct inet_bind_hashbucket *head, *head2;
262 	bool created_inet_bind_bucket = false;
263 	struct net *net = sock_net(sk);
264 	bool update_fastreuse = false;
265 	struct inet_bind2_bucket *tb2;
266 	struct inet_bind_bucket *tb;
267 	int bhash, l3mdev;
268 
269 	bhash = inet_bhashfn(net, port, table->bhash_size);
270 	head = &table->bhash[bhash];
271 	head2 = inet_bhashfn_portaddr(table, child, net, port);
272 
273 	spin_lock(&head->lock);
274 	spin_lock(&head2->lock);
275 	tb = inet_csk(sk)->icsk_bind_hash;
276 	tb2 = inet_csk(sk)->icsk_bind2_hash;
277 	if (unlikely(!tb || !tb2)) {
278 		spin_unlock(&head2->lock);
279 		spin_unlock(&head->lock);
280 		return -ENOENT;
281 	}
282 	if (tb->port != port) {
283 		l3mdev = inet_sk_bound_l3mdev(sk);
284 
285 		/* NOTE: using tproxy and redirecting skbs to a proxy
286 		 * on a different listener port breaks the assumption
287 		 * that the listener socket's icsk_bind_hash is the same
288 		 * as that of the child socket. We have to look up or
289 		 * create a new bind bucket for the child here. */
290 		inet_bind_bucket_for_each(tb, &head->chain) {
291 			if (inet_bind_bucket_match(tb, net, port, l3mdev))
292 				break;
293 		}
294 		if (!tb) {
295 			tb = inet_bind_bucket_create(table->bind_bucket_cachep,
296 						     net, head, port, l3mdev);
297 			if (!tb) {
298 				spin_unlock(&head2->lock);
299 				spin_unlock(&head->lock);
300 				return -ENOMEM;
301 			}
302 			created_inet_bind_bucket = true;
303 		}
304 		update_fastreuse = true;
305 
306 		goto bhash2_find;
307 	} else if (!inet_bind2_bucket_addr_match(tb2, child)) {
308 		l3mdev = inet_sk_bound_l3mdev(sk);
309 
310 bhash2_find:
311 		tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, child);
312 		if (!tb2) {
313 			tb2 = inet_bind2_bucket_create(table->bind2_bucket_cachep,
314 						       net, head2, tb, child);
315 			if (!tb2)
316 				goto error;
317 		}
318 	}
319 	if (update_fastreuse)
320 		inet_csk_update_fastreuse(child, tb, tb2);
321 	inet_bind_hash(child, tb, tb2, port);
322 	spin_unlock(&head2->lock);
323 	spin_unlock(&head->lock);
324 
325 	return 0;
326 
327 error:
328 	if (created_inet_bind_bucket)
329 		inet_bind_bucket_destroy(tb);
330 	spin_unlock(&head2->lock);
331 	spin_unlock(&head->lock);
332 	return -ENOMEM;
333 }
334 EXPORT_SYMBOL_GPL(__inet_inherit_port);
335 
336 static struct inet_listen_hashbucket *
337 inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
338 {
339 	u32 hash;
340 
341 #if IS_ENABLED(CONFIG_IPV6)
342 	if (sk->sk_family == AF_INET6)
343 		hash = ipv6_portaddr_hash(sock_net(sk),
344 					  &sk->sk_v6_rcv_saddr,
345 					  inet_sk(sk)->inet_num);
346 	else
347 #endif
348 		hash = ipv4_portaddr_hash(sock_net(sk),
349 					  inet_sk(sk)->inet_rcv_saddr,
350 					  inet_sk(sk)->inet_num);
351 	return inet_lhash2_bucket(h, hash);
352 }
353 
354 static inline int compute_score(struct sock *sk, const struct net *net,
355 				const unsigned short hnum, const __be32 daddr,
356 				const int dif, const int sdif)
357 {
358 	int score = -1;
359 
360 	if (net_eq(sock_net(sk), net) && READ_ONCE(sk->sk_num) == hnum &&
361 			!ipv6_only_sock(sk)) {
362 		if (sk->sk_rcv_saddr != daddr)
363 			return -1;
364 
365 		if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
366 			return -1;
367 		score =  sk->sk_bound_dev_if ? 2 : 1;
368 
369 		if (sk->sk_family == PF_INET)
370 			score++;
371 		if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
372 			score++;
373 	}
374 	return score;
375 }
376 
377 /**
378  * inet_lookup_reuseport() - execute reuseport logic on AF_INET socket if necessary.
379  * @net: network namespace.
380  * @sk: AF_INET socket, must be in TCP_LISTEN state for TCP or TCP_CLOSE for UDP.
381  * @skb: context for a potential SK_REUSEPORT program.
382  * @doff: header offset.
383  * @saddr: source address.
384  * @sport: source port.
385  * @daddr: destination address.
386  * @hnum: destination port in host byte order.
387  * @ehashfn: hash function used to generate the fallback hash.
388  *
389  * Return: NULL if sk doesn't have SO_REUSEPORT set, otherwise a pointer to
390  *         the selected sock or an error.
391  */
392 struct sock *inet_lookup_reuseport(const struct net *net, struct sock *sk,
393 				   struct sk_buff *skb, int doff,
394 				   __be32 saddr, __be16 sport,
395 				   __be32 daddr, unsigned short hnum,
396 				   inet_ehashfn_t *ehashfn)
397 {
398 	struct sock *reuse_sk = NULL;
399 	u32 phash;
400 
401 	if (sk->sk_reuseport) {
402 		phash = INDIRECT_CALL_2(ehashfn, udp_ehashfn, inet_ehashfn,
403 					net, daddr, hnum, saddr, sport);
404 		reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
405 	}
406 	return reuse_sk;
407 }
408 EXPORT_SYMBOL_GPL(inet_lookup_reuseport);
409 
410 /*
411  * Here are some nice properties to exploit here. The BSD API
412  * does not allow a listening sock to specify the remote port nor the
413  * remote address for the connection. So always assume those are both
414  * wildcarded during the search since they can never be otherwise.
415  */
416 
417 /* called with rcu_read_lock() : No refcount taken on the socket */
418 static struct sock *inet_lhash2_lookup(const struct net *net,
419 				struct inet_listen_hashbucket *ilb2,
420 				struct sk_buff *skb, int doff,
421 				const __be32 saddr, __be16 sport,
422 				const __be32 daddr, const unsigned short hnum,
423 				const int dif, const int sdif)
424 {
425 	struct sock *sk, *result = NULL;
426 	struct hlist_nulls_node *node;
427 	int score, hiscore = 0;
428 
429 	sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
430 		score = compute_score(sk, net, hnum, daddr, dif, sdif);
431 		if (score > hiscore) {
432 			result = inet_lookup_reuseport(net, sk, skb, doff,
433 						       saddr, sport, daddr, hnum, inet_ehashfn);
434 			if (result)
435 				return result;
436 
437 			result = sk;
438 			hiscore = score;
439 		}
440 	}
441 
442 	return result;
443 }
444 
445 struct sock *inet_lookup_run_sk_lookup(const struct net *net,
446 				       int protocol,
447 				       struct sk_buff *skb, int doff,
448 				       __be32 saddr, __be16 sport,
449 				       __be32 daddr, u16 hnum, const int dif,
450 				       inet_ehashfn_t *ehashfn)
451 {
452 	struct sock *sk, *reuse_sk;
453 	bool no_reuseport;
454 
455 	no_reuseport = bpf_sk_lookup_run_v4(net, protocol, saddr, sport,
456 					    daddr, hnum, dif, &sk);
457 	if (no_reuseport || IS_ERR_OR_NULL(sk))
458 		return sk;
459 
460 	reuse_sk = inet_lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum,
461 					 ehashfn);
462 	if (reuse_sk)
463 		sk = reuse_sk;
464 	return sk;
465 }
466 
467 struct sock *__inet_lookup_listener(const struct net *net,
468 				    struct sk_buff *skb, int doff,
469 				    const __be32 saddr, __be16 sport,
470 				    const __be32 daddr, const unsigned short hnum,
471 				    const int dif, const int sdif)
472 {
473 	struct inet_listen_hashbucket *ilb2;
474 	struct inet_hashinfo *hashinfo;
475 	struct sock *result = NULL;
476 	unsigned int hash2;
477 
478 	/* Lookup redirect from BPF */
479 	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
480 		result = inet_lookup_run_sk_lookup(net, IPPROTO_TCP, skb, doff,
481 						   saddr, sport, daddr, hnum, dif,
482 						   inet_ehashfn);
483 		if (result)
484 			goto done;
485 	}
486 
487 	hashinfo = net->ipv4.tcp_death_row.hashinfo;
488 	hash2 = ipv4_portaddr_hash(net, daddr, hnum);
489 	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
490 
491 	result = inet_lhash2_lookup(net, ilb2, skb, doff,
492 				    saddr, sport, daddr, hnum,
493 				    dif, sdif);
494 	if (result)
495 		goto done;
496 
497 	/* Lookup lhash2 with INADDR_ANY */
498 	hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
499 	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
500 
501 	result = inet_lhash2_lookup(net, ilb2, skb, doff,
502 				    saddr, sport, htonl(INADDR_ANY), hnum,
503 				    dif, sdif);
504 done:
505 	if (IS_ERR(result))
506 		return NULL;
507 	return result;
508 }
509 EXPORT_SYMBOL_GPL(__inet_lookup_listener);
510 
511 /* All sockets share common refcount, but have different destructors */
512 void sock_gen_put(struct sock *sk)
513 {
514 	if (!refcount_dec_and_test(&sk->sk_refcnt))
515 		return;
516 
517 	if (sk->sk_state == TCP_TIME_WAIT)
518 		inet_twsk_free(inet_twsk(sk));
519 	else if (sk->sk_state == TCP_NEW_SYN_RECV)
520 		reqsk_free(inet_reqsk(sk));
521 	else
522 		sk_free(sk);
523 }
524 EXPORT_SYMBOL_GPL(sock_gen_put);
525 
526 void sock_edemux(struct sk_buff *skb)
527 {
528 	sock_gen_put(skb->sk);
529 }
530 EXPORT_SYMBOL(sock_edemux);
531 
532 struct sock *__inet_lookup_established(const struct net *net,
533 				       const __be32 saddr, const __be16 sport,
534 				       const __be32 daddr, const u16 hnum,
535 				       const int dif, const int sdif)
536 {
537 	const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
538 	INET_ADDR_COOKIE(acookie, saddr, daddr);
539 	const struct hlist_nulls_node *node;
540 	struct inet_ehash_bucket *head;
541 	struct inet_hashinfo *hashinfo;
542 	unsigned int hash, slot;
543 	struct sock *sk;
544 
545 	hashinfo = net->ipv4.tcp_death_row.hashinfo;
546 	hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
547 	slot = hash & hashinfo->ehash_mask;
548 	head = &hashinfo->ehash[slot];
549 
550 begin:
551 	sk_nulls_for_each_rcu(sk, node, &head->chain) {
552 		if (sk->sk_hash != hash)
553 			continue;
554 		if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
555 			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
556 				goto out;
557 			if (unlikely(!inet_match(net, sk, acookie,
558 						 ports, dif, sdif))) {
559 				sock_gen_put(sk);
560 				goto begin;
561 			}
562 			goto found;
563 		}
564 	}
565 	/*
566 	 * if the nulls value we got at the end of this lookup is
567 	 * not the expected one, we must restart lookup.
568 	 * We probably met an item that was moved to another chain.
569 	 */
570 	if (get_nulls_value(node) != slot)
571 		goto begin;
572 out:
573 	sk = NULL;
574 found:
575 	return sk;
576 }
577 EXPORT_SYMBOL_GPL(__inet_lookup_established);
578 
579 /* called with local bh disabled */
580 static int __inet_check_established(struct inet_timewait_death_row *death_row,
581 				    struct sock *sk, __u16 lport,
582 				    struct inet_timewait_sock **twp,
583 				    bool rcu_lookup,
584 				    u32 hash)
585 {
586 	struct inet_hashinfo *hinfo = death_row->hashinfo;
587 	struct inet_sock *inet = inet_sk(sk);
588 	__be32 daddr = inet->inet_rcv_saddr;
589 	__be32 saddr = inet->inet_daddr;
590 	int dif = sk->sk_bound_dev_if;
591 	struct net *net = sock_net(sk);
592 	int sdif = l3mdev_master_ifindex_by_index(net, dif);
593 	INET_ADDR_COOKIE(acookie, saddr, daddr);
594 	const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
595 	struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
596 	struct inet_timewait_sock *tw = NULL;
597 	const struct hlist_nulls_node *node;
598 	struct sock *sk2;
599 	spinlock_t *lock;
600 
601 	if (rcu_lookup) {
602 		sk_nulls_for_each(sk2, node, &head->chain) {
603 			if (sk2->sk_hash != hash ||
604 			    !inet_match(net, sk2, acookie, ports, dif, sdif))
605 				continue;
606 			if (sk2->sk_state == TCP_TIME_WAIT)
607 				break;
608 			return -EADDRNOTAVAIL;
609 		}
610 		return 0;
611 	}
612 
613 	lock = inet_ehash_lockp(hinfo, hash);
614 	spin_lock(lock);
615 
616 	sk_nulls_for_each(sk2, node, &head->chain) {
617 		if (sk2->sk_hash != hash)
618 			continue;
619 
620 		if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) {
621 			if (sk2->sk_state == TCP_TIME_WAIT) {
622 				tw = inet_twsk(sk2);
623 				if (tcp_twsk_unique(sk, sk2, twp))
624 					break;
625 			}
626 			goto not_unique;
627 		}
628 	}
629 
630 	/* Must record num and sport now. Otherwise we will see
631 	 * in hash table socket with a funny identity.
632 	 */
633 	inet->inet_num = lport;
634 	inet->inet_sport = htons(lport);
635 	sk->sk_hash = hash;
636 	WARN_ON(!sk_unhashed(sk));
637 	__sk_nulls_add_node_rcu(sk, &head->chain);
638 	if (tw) {
639 		sk_nulls_del_node_init_rcu((struct sock *)tw);
640 		__NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
641 	}
642 	spin_unlock(lock);
643 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
644 
645 	if (twp) {
646 		*twp = tw;
647 	} else if (tw) {
648 		/* Silly. Should hash-dance instead... */
649 		inet_twsk_deschedule_put(tw);
650 	}
651 	return 0;
652 
653 not_unique:
654 	spin_unlock(lock);
655 	return -EADDRNOTAVAIL;
656 }
657 
658 static u64 inet_sk_port_offset(const struct sock *sk)
659 {
660 	const struct inet_sock *inet = inet_sk(sk);
661 
662 	return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
663 					  inet->inet_daddr,
664 					  inet->inet_dport);
665 }
666 
667 /* Searches for an exsiting socket in the ehash bucket list.
668  * Returns true if found, false otherwise.
669  */
670 static bool inet_ehash_lookup_by_sk(struct sock *sk,
671 				    struct hlist_nulls_head *list)
672 {
673 	const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num);
674 	const int sdif = sk->sk_bound_dev_if;
675 	const int dif = sk->sk_bound_dev_if;
676 	const struct hlist_nulls_node *node;
677 	struct net *net = sock_net(sk);
678 	struct sock *esk;
679 
680 	INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
681 
682 	sk_nulls_for_each_rcu(esk, node, list) {
683 		if (esk->sk_hash != sk->sk_hash)
684 			continue;
685 		if (sk->sk_family == AF_INET) {
686 			if (unlikely(inet_match(net, esk, acookie,
687 						ports, dif, sdif))) {
688 				return true;
689 			}
690 		}
691 #if IS_ENABLED(CONFIG_IPV6)
692 		else if (sk->sk_family == AF_INET6) {
693 			if (unlikely(inet6_match(net, esk,
694 						 &sk->sk_v6_daddr,
695 						 &sk->sk_v6_rcv_saddr,
696 						 ports, dif, sdif))) {
697 				return true;
698 			}
699 		}
700 #endif
701 	}
702 	return false;
703 }
704 
705 /* Insert a socket into ehash, and eventually remove another one
706  * (The another one can be a SYN_RECV or TIMEWAIT)
707  * If an existing socket already exists, socket sk is not inserted,
708  * and sets found_dup_sk parameter to true.
709  */
710 bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
711 {
712 	struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
713 	struct inet_ehash_bucket *head;
714 	struct hlist_nulls_head *list;
715 	spinlock_t *lock;
716 	bool ret = true;
717 
718 	WARN_ON_ONCE(!sk_unhashed(sk));
719 
720 	sk->sk_hash = sk_ehashfn(sk);
721 	head = inet_ehash_bucket(hashinfo, sk->sk_hash);
722 	list = &head->chain;
723 	lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
724 
725 	spin_lock(lock);
726 	if (osk) {
727 		WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
728 		ret = sk_nulls_replace_node_init_rcu(osk, sk);
729 		goto unlock;
730 	}
731 
732 	if (found_dup_sk) {
733 		*found_dup_sk = inet_ehash_lookup_by_sk(sk, list);
734 		if (*found_dup_sk)
735 			ret = false;
736 	}
737 
738 	if (ret)
739 		__sk_nulls_add_node_rcu(sk, list);
740 
741 unlock:
742 	spin_unlock(lock);
743 
744 	return ret;
745 }
746 
747 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
748 {
749 	bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
750 
751 	if (ok) {
752 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
753 	} else {
754 		tcp_orphan_count_inc();
755 		inet_sk_set_state(sk, TCP_CLOSE);
756 		sock_set_flag(sk, SOCK_DEAD);
757 		inet_csk_destroy_sock(sk);
758 	}
759 	return ok;
760 }
761 EXPORT_IPV6_MOD(inet_ehash_nolisten);
762 
763 static int inet_reuseport_add_sock(struct sock *sk,
764 				   struct inet_listen_hashbucket *ilb)
765 {
766 	struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
767 	const struct hlist_nulls_node *node;
768 	kuid_t uid = sk_uid(sk);
769 	struct sock *sk2;
770 
771 	sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
772 		if (sk2 != sk &&
773 		    sk2->sk_family == sk->sk_family &&
774 		    ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
775 		    sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
776 		    inet_csk(sk2)->icsk_bind_hash == tb &&
777 		    sk2->sk_reuseport && uid_eq(uid, sk_uid(sk2)) &&
778 		    inet_rcv_saddr_equal(sk, sk2, false))
779 			return reuseport_add_sock(sk, sk2,
780 						  inet_rcv_saddr_any(sk));
781 	}
782 
783 	return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
784 }
785 
786 int inet_hash(struct sock *sk)
787 {
788 	struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
789 	struct inet_listen_hashbucket *ilb2;
790 	int err = 0;
791 
792 	if (sk->sk_state == TCP_CLOSE)
793 		return 0;
794 
795 	if (sk->sk_state != TCP_LISTEN) {
796 		local_bh_disable();
797 		inet_ehash_nolisten(sk, NULL, NULL);
798 		local_bh_enable();
799 		return 0;
800 	}
801 
802 #if IS_ENABLED(CONFIG_IPV6)
803 	if (sk->sk_family == AF_INET6)
804 		inet6_init_ehash_secret();
805 #endif
806 	inet_init_ehash_secret();
807 
808 	WARN_ON(!sk_unhashed(sk));
809 	ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
810 
811 	spin_lock(&ilb2->lock);
812 	if (sk->sk_reuseport) {
813 		err = inet_reuseport_add_sock(sk, ilb2);
814 		if (err)
815 			goto unlock;
816 	}
817 	sock_set_flag(sk, SOCK_RCU_FREE);
818 	if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
819 		sk->sk_family == AF_INET6)
820 		__sk_nulls_add_node_tail_rcu(sk, &ilb2->nulls_head);
821 	else
822 		__sk_nulls_add_node_rcu(sk, &ilb2->nulls_head);
823 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
824 unlock:
825 	spin_unlock(&ilb2->lock);
826 
827 	return err;
828 }
829 EXPORT_IPV6_MOD(inet_hash);
830 
831 void inet_unhash(struct sock *sk)
832 {
833 	struct inet_hashinfo *hashinfo = tcp_get_hashinfo(sk);
834 
835 	if (sk_unhashed(sk))
836 		return;
837 
838 	sock_rps_delete_flow(sk);
839 	if (sk->sk_state == TCP_LISTEN) {
840 		struct inet_listen_hashbucket *ilb2;
841 
842 		ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
843 		/* Don't disable bottom halves while acquiring the lock to
844 		 * avoid circular locking dependency on PREEMPT_RT.
845 		 */
846 		spin_lock(&ilb2->lock);
847 		if (rcu_access_pointer(sk->sk_reuseport_cb))
848 			reuseport_stop_listen_sock(sk);
849 
850 		__sk_nulls_del_node_init_rcu(sk);
851 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
852 		spin_unlock(&ilb2->lock);
853 	} else {
854 		spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
855 
856 		spin_lock_bh(lock);
857 		__sk_nulls_del_node_init_rcu(sk);
858 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
859 		spin_unlock_bh(lock);
860 	}
861 }
862 EXPORT_IPV6_MOD(inet_unhash);
863 
864 static bool inet_bind2_bucket_match(const struct inet_bind2_bucket *tb,
865 				    const struct net *net, unsigned short port,
866 				    int l3mdev, const struct sock *sk)
867 {
868 	if (!net_eq(ib2_net(tb), net) || tb->port != port ||
869 	    tb->l3mdev != l3mdev)
870 		return false;
871 
872 	return inet_bind2_bucket_addr_match(tb, sk);
873 }
874 
875 bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, const struct net *net,
876 				      unsigned short port, int l3mdev, const struct sock *sk)
877 {
878 	if (!net_eq(ib2_net(tb), net) || tb->port != port ||
879 	    tb->l3mdev != l3mdev)
880 		return false;
881 
882 #if IS_ENABLED(CONFIG_IPV6)
883 	if (tb->addr_type == IPV6_ADDR_ANY)
884 		return true;
885 
886 	if (tb->addr_type != IPV6_ADDR_MAPPED)
887 		return false;
888 
889 	if (sk->sk_family == AF_INET6 &&
890 	    !ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
891 		return false;
892 #endif
893 	return tb->rcv_saddr == 0;
894 }
895 
896 /* The socket's bhash2 hashbucket spinlock must be held when this is called */
897 struct inet_bind2_bucket *
898 inet_bind2_bucket_find(const struct inet_bind_hashbucket *head, const struct net *net,
899 		       unsigned short port, int l3mdev, const struct sock *sk)
900 {
901 	struct inet_bind2_bucket *bhash2 = NULL;
902 
903 	inet_bind_bucket_for_each(bhash2, &head->chain)
904 		if (inet_bind2_bucket_match(bhash2, net, port, l3mdev, sk))
905 			break;
906 
907 	return bhash2;
908 }
909 
910 struct inet_bind_hashbucket *
911 inet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port)
912 {
913 	struct inet_hashinfo *hinfo = tcp_get_hashinfo(sk);
914 	u32 hash;
915 
916 #if IS_ENABLED(CONFIG_IPV6)
917 	if (sk->sk_family == AF_INET6)
918 		hash = ipv6_portaddr_hash(net, &in6addr_any, port);
919 	else
920 #endif
921 		hash = ipv4_portaddr_hash(net, 0, port);
922 
923 	return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
924 }
925 
926 static void inet_update_saddr(struct sock *sk, void *saddr, int family)
927 {
928 	if (family == AF_INET) {
929 		inet_sk(sk)->inet_saddr = *(__be32 *)saddr;
930 		sk_rcv_saddr_set(sk, inet_sk(sk)->inet_saddr);
931 	}
932 #if IS_ENABLED(CONFIG_IPV6)
933 	else {
934 		sk->sk_v6_rcv_saddr = *(struct in6_addr *)saddr;
935 	}
936 #endif
937 }
938 
939 static int __inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family, bool reset)
940 {
941 	struct inet_hashinfo *hinfo = tcp_get_hashinfo(sk);
942 	struct inet_bind_hashbucket *head, *head2;
943 	struct inet_bind2_bucket *tb2, *new_tb2;
944 	int l3mdev = inet_sk_bound_l3mdev(sk);
945 	int port = inet_sk(sk)->inet_num;
946 	struct net *net = sock_net(sk);
947 	int bhash;
948 
949 	if (!inet_csk(sk)->icsk_bind2_hash) {
950 		/* Not bind()ed before. */
951 		if (reset)
952 			inet_reset_saddr(sk);
953 		else
954 			inet_update_saddr(sk, saddr, family);
955 
956 		return 0;
957 	}
958 
959 	/* Allocate a bind2 bucket ahead of time to avoid permanently putting
960 	 * the bhash2 table in an inconsistent state if a new tb2 bucket
961 	 * allocation fails.
962 	 */
963 	new_tb2 = kmem_cache_alloc(hinfo->bind2_bucket_cachep, GFP_ATOMIC);
964 	if (!new_tb2) {
965 		if (reset) {
966 			/* The (INADDR_ANY, port) bucket might have already
967 			 * been freed, then we cannot fixup icsk_bind2_hash,
968 			 * so we give up and unlink sk from bhash/bhash2 not
969 			 * to leave inconsistency in bhash2.
970 			 */
971 			inet_put_port(sk);
972 			inet_reset_saddr(sk);
973 		}
974 
975 		return -ENOMEM;
976 	}
977 
978 	bhash = inet_bhashfn(net, port, hinfo->bhash_size);
979 	head = &hinfo->bhash[bhash];
980 	head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
981 
982 	/* If we change saddr locklessly, another thread
983 	 * iterating over bhash might see corrupted address.
984 	 */
985 	spin_lock_bh(&head->lock);
986 
987 	spin_lock(&head2->lock);
988 	__sk_del_bind_node(sk);
989 	inet_bind2_bucket_destroy(hinfo->bind2_bucket_cachep, inet_csk(sk)->icsk_bind2_hash);
990 	spin_unlock(&head2->lock);
991 
992 	if (reset)
993 		inet_reset_saddr(sk);
994 	else
995 		inet_update_saddr(sk, saddr, family);
996 
997 	head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
998 
999 	spin_lock(&head2->lock);
1000 	tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk);
1001 	if (!tb2) {
1002 		tb2 = new_tb2;
1003 		inet_bind2_bucket_init(tb2, net, head2, inet_csk(sk)->icsk_bind_hash, sk);
1004 		if (sk_is_connect_bind(sk)) {
1005 			tb2->fastreuse = -1;
1006 			tb2->fastreuseport = -1;
1007 		}
1008 	}
1009 	inet_csk(sk)->icsk_bind2_hash = tb2;
1010 	sk_add_bind_node(sk, &tb2->owners);
1011 	spin_unlock(&head2->lock);
1012 
1013 	spin_unlock_bh(&head->lock);
1014 
1015 	if (tb2 != new_tb2)
1016 		kmem_cache_free(hinfo->bind2_bucket_cachep, new_tb2);
1017 
1018 	return 0;
1019 }
1020 
1021 int inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family)
1022 {
1023 	return __inet_bhash2_update_saddr(sk, saddr, family, false);
1024 }
1025 EXPORT_IPV6_MOD(inet_bhash2_update_saddr);
1026 
1027 void inet_bhash2_reset_saddr(struct sock *sk)
1028 {
1029 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
1030 		__inet_bhash2_update_saddr(sk, NULL, 0, true);
1031 }
1032 EXPORT_IPV6_MOD(inet_bhash2_reset_saddr);
1033 
1034 /* RFC 6056 3.3.4.  Algorithm 4: Double-Hash Port Selection Algorithm
1035  * Note that we use 32bit integers (vs RFC 'short integers')
1036  * because 2^16 is not a multiple of num_ephemeral and this
1037  * property might be used by clever attacker.
1038  *
1039  * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though
1040  * attacks were since demonstrated, thus we use 65536 by default instead
1041  * to really give more isolation and privacy, at the expense of 256kB
1042  * of kernel memory.
1043  */
1044 #define INET_TABLE_PERTURB_SIZE (1 << CONFIG_INET_TABLE_PERTURB_ORDER)
1045 static u32 *table_perturb;
1046 
1047 int __inet_hash_connect(struct inet_timewait_death_row *death_row,
1048 		struct sock *sk, u64 port_offset,
1049 		u32 hash_port0,
1050 		int (*check_established)(struct inet_timewait_death_row *,
1051 			struct sock *, __u16, struct inet_timewait_sock **,
1052 			bool rcu_lookup, u32 hash))
1053 {
1054 	struct inet_hashinfo *hinfo = death_row->hashinfo;
1055 	struct inet_bind_hashbucket *head, *head2;
1056 	struct inet_timewait_sock *tw = NULL;
1057 	int port = inet_sk(sk)->inet_num;
1058 	struct net *net = sock_net(sk);
1059 	struct inet_bind2_bucket *tb2;
1060 	struct inet_bind_bucket *tb;
1061 	int step, scan_step, l3mdev;
1062 	u32 index, max_rand_step;
1063 	bool tb_created = false;
1064 	u32 remaining, offset;
1065 	int ret, i, low, high;
1066 	bool local_ports;
1067 
1068 	if (port) {
1069 		local_bh_disable();
1070 		ret = check_established(death_row, sk, port, NULL, false,
1071 					hash_port0 + port);
1072 		local_bh_enable();
1073 		return ret;
1074 	}
1075 
1076 	l3mdev = inet_sk_bound_l3mdev(sk);
1077 
1078 	local_ports = inet_sk_get_local_port_range(sk, &low, &high);
1079 	step = local_ports ? 1 : 2;
1080 	scan_step = step;
1081 	max_rand_step = READ_ONCE(net->ipv4.sysctl_ip_local_port_step_width);
1082 
1083 	high++; /* [32768, 60999] -> [32768, 61000[ */
1084 	remaining = high - low;
1085 	if (!local_ports && remaining > 1)
1086 		remaining &= ~1U;
1087 
1088 	get_random_sleepable_once(table_perturb,
1089 				  INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
1090 	index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
1091 
1092 	offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
1093 	offset %= remaining;
1094 
1095 	/* In first pass we try ports of @low parity.
1096 	 * inet_csk_get_port() does the opposite choice.
1097 	 */
1098 	if (!local_ports)
1099 		offset &= ~1U;
1100 
1101 	if (max_rand_step && remaining > 1) {
1102 		u32 range = remaining / step;
1103 		u32 upper_bound;
1104 
1105 		upper_bound = min(range, max_rand_step);
1106 		scan_step = get_random_u32_inclusive(1, upper_bound);
1107 		while (gcd(scan_step, range) != 1) {
1108 			scan_step++;
1109 			/* if both scan_step and range are even gcd won't be 1 */
1110 			if (!(scan_step & 1) && !(range & 1))
1111 				scan_step++;
1112 			if (unlikely(scan_step > upper_bound)) {
1113 				scan_step = 1;
1114 				break;
1115 			}
1116 		}
1117 		scan_step *= step;
1118 	}
1119 other_parity_scan:
1120 	port = low + offset;
1121 	for (i = 0; i < remaining; i += step, port += scan_step) {
1122 		if (unlikely(port >= high))
1123 			port -= remaining;
1124 		if (inet_is_local_reserved_port(net, port))
1125 			continue;
1126 		head = &hinfo->bhash[inet_bhashfn(net, port,
1127 						  hinfo->bhash_size)];
1128 		rcu_read_lock();
1129 		hlist_for_each_entry_rcu(tb, &head->chain, node) {
1130 			if (!inet_bind_bucket_match(tb, net, port, l3mdev))
1131 				continue;
1132 			if (tb->fastreuse >= 0 || tb->fastreuseport >= 0) {
1133 				rcu_read_unlock();
1134 				goto next_port;
1135 			}
1136 			if (!check_established(death_row, sk, port, &tw, true,
1137 					       hash_port0 + port))
1138 				break;
1139 			rcu_read_unlock();
1140 			goto next_port;
1141 		}
1142 		rcu_read_unlock();
1143 
1144 		spin_lock_bh(&head->lock);
1145 
1146 		/* Does not bother with rcv_saddr checks, because
1147 		 * the established check is already unique enough.
1148 		 */
1149 		inet_bind_bucket_for_each(tb, &head->chain) {
1150 			if (inet_bind_bucket_match(tb, net, port, l3mdev)) {
1151 				if (tb->fastreuse >= 0 ||
1152 				    tb->fastreuseport >= 0)
1153 					goto next_port_unlock;
1154 				WARN_ON(hlist_empty(&tb->bhash2));
1155 				if (!check_established(death_row, sk,
1156 						       port, &tw, false,
1157 						       hash_port0 + port))
1158 					goto ok;
1159 				goto next_port_unlock;
1160 			}
1161 		}
1162 
1163 		tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
1164 					     net, head, port, l3mdev);
1165 		if (!tb) {
1166 			spin_unlock_bh(&head->lock);
1167 			return -ENOMEM;
1168 		}
1169 		tb_created = true;
1170 		tb->fastreuse = -1;
1171 		tb->fastreuseport = -1;
1172 		goto ok;
1173 next_port_unlock:
1174 		spin_unlock_bh(&head->lock);
1175 next_port:
1176 		cond_resched();
1177 	}
1178 
1179 	if (!local_ports) {
1180 		offset++;
1181 		if ((offset & 1) && remaining > 1)
1182 			goto other_parity_scan;
1183 	}
1184 	return -EADDRNOTAVAIL;
1185 
1186 ok:
1187 	/* Find the corresponding tb2 bucket since we need to
1188 	 * add the socket to the bhash2 table as well
1189 	 */
1190 	head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
1191 	spin_lock(&head2->lock);
1192 
1193 	tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk);
1194 	if (!tb2) {
1195 		tb2 = inet_bind2_bucket_create(hinfo->bind2_bucket_cachep, net,
1196 					       head2, tb, sk);
1197 		if (!tb2)
1198 			goto error;
1199 		tb2->fastreuse = -1;
1200 		tb2->fastreuseport = -1;
1201 	}
1202 
1203 	/* Here we want to add a little bit of randomness to the next source
1204 	 * port that will be chosen. We use a max() with a random here so that
1205 	 * on low contention the randomness is maximal and on high contention
1206 	 * it may be inexistent.
1207 	 */
1208 	i = max_t(int, i, get_random_u32_below(8) * step);
1209 	WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + step);
1210 
1211 	/* Head lock still held and bh's disabled */
1212 	inet_bind_hash(sk, tb, tb2, port);
1213 	sk->sk_userlocks |= SOCK_CONNECT_BIND;
1214 
1215 	if (sk_unhashed(sk)) {
1216 		inet_sk(sk)->inet_sport = htons(port);
1217 		inet_ehash_nolisten(sk, (struct sock *)tw, NULL);
1218 	}
1219 	if (tw)
1220 		inet_twsk_bind_unhash(tw, hinfo);
1221 
1222 	spin_unlock(&head2->lock);
1223 	spin_unlock(&head->lock);
1224 
1225 	if (tw)
1226 		inet_twsk_deschedule_put(tw);
1227 	local_bh_enable();
1228 	return 0;
1229 
1230 error:
1231 	if (sk_hashed(sk)) {
1232 		spinlock_t *lock = inet_ehash_lockp(hinfo, sk->sk_hash);
1233 
1234 		sock_prot_inuse_add(net, sk->sk_prot, -1);
1235 
1236 		spin_lock(lock);
1237 		__sk_nulls_del_node_init_rcu(sk);
1238 		spin_unlock(lock);
1239 
1240 		sk->sk_hash = 0;
1241 		inet_sk(sk)->inet_sport = 0;
1242 		WRITE_ONCE(inet_sk(sk)->inet_num, 0);
1243 
1244 		if (tw)
1245 			inet_twsk_bind_unhash(tw, hinfo);
1246 	}
1247 
1248 	spin_unlock(&head2->lock);
1249 	if (tb_created)
1250 		inet_bind_bucket_destroy(tb);
1251 	spin_unlock(&head->lock);
1252 
1253 	if (tw)
1254 		inet_twsk_deschedule_put(tw);
1255 
1256 	local_bh_enable();
1257 
1258 	return -ENOMEM;
1259 }
1260 
1261 /*
1262  * Bind a port for a connect operation and hash it.
1263  */
1264 int inet_hash_connect(struct inet_timewait_death_row *death_row,
1265 		      struct sock *sk)
1266 {
1267 	const struct inet_sock *inet = inet_sk(sk);
1268 	const struct net *net = sock_net(sk);
1269 	u64 port_offset = 0;
1270 	u32 hash_port0;
1271 
1272 	if (!inet_sk(sk)->inet_num)
1273 		port_offset = inet_sk_port_offset(sk);
1274 
1275 	inet_init_ehash_secret();
1276 
1277 	hash_port0 = inet_ehashfn(net, inet->inet_rcv_saddr, 0,
1278 				  inet->inet_daddr, inet->inet_dport);
1279 
1280 	return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
1281 				   __inet_check_established);
1282 }
1283 
1284 void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
1285 				unsigned long numentries, int scale,
1286 				unsigned long low_limit,
1287 				unsigned long high_limit)
1288 {
1289 	unsigned int i;
1290 
1291 	h->lhash2 = alloc_large_system_hash(name,
1292 					    sizeof(*h->lhash2),
1293 					    numentries,
1294 					    scale,
1295 					    0,
1296 					    NULL,
1297 					    &h->lhash2_mask,
1298 					    low_limit,
1299 					    high_limit);
1300 
1301 	for (i = 0; i <= h->lhash2_mask; i++) {
1302 		spin_lock_init(&h->lhash2[i].lock);
1303 		INIT_HLIST_NULLS_HEAD(&h->lhash2[i].nulls_head,
1304 				      i + LISTENING_NULLS_BASE);
1305 	}
1306 
1307 	/* this one is used for source ports of outgoing connections */
1308 	table_perturb = alloc_large_system_hash("Table-perturb",
1309 						sizeof(*table_perturb),
1310 						INET_TABLE_PERTURB_SIZE,
1311 						0, 0, NULL, NULL,
1312 						INET_TABLE_PERTURB_SIZE,
1313 						INET_TABLE_PERTURB_SIZE);
1314 }
1315 
1316 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
1317 {
1318 	unsigned int locksz = sizeof(spinlock_t);
1319 	unsigned int i, nblocks = 1;
1320 	spinlock_t *ptr = NULL;
1321 
1322 	if (locksz == 0)
1323 		goto set_mask;
1324 
1325 	/* Allocate 2 cache lines or at least one spinlock per cpu. */
1326 	nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U) * num_possible_cpus();
1327 
1328 	/* At least one page per NUMA node. */
1329 	nblocks = max(nblocks, num_online_nodes() * PAGE_SIZE / locksz);
1330 
1331 	nblocks = roundup_pow_of_two(nblocks);
1332 
1333 	/* No more locks than number of hash buckets. */
1334 	nblocks = min(nblocks, hashinfo->ehash_mask + 1);
1335 
1336 	if (num_online_nodes() > 1) {
1337 		/* Use vmalloc() to allow NUMA policy to spread pages
1338 		 * on all available nodes if desired.
1339 		 */
1340 		ptr = vmalloc_array(nblocks, locksz);
1341 	}
1342 	if (!ptr) {
1343 		ptr = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
1344 		if (!ptr)
1345 			return -ENOMEM;
1346 	}
1347 	for (i = 0; i < nblocks; i++)
1348 		spin_lock_init(&ptr[i]);
1349 	hashinfo->ehash_locks = ptr;
1350 set_mask:
1351 	hashinfo->ehash_locks_mask = nblocks - 1;
1352 	return 0;
1353 }
1354 
1355 struct inet_hashinfo *inet_pernet_hashinfo_alloc(struct inet_hashinfo *hashinfo,
1356 						 unsigned int ehash_entries)
1357 {
1358 	struct inet_hashinfo *new_hashinfo;
1359 	int i;
1360 
1361 	new_hashinfo = kmemdup(hashinfo, sizeof(*hashinfo), GFP_KERNEL);
1362 	if (!new_hashinfo)
1363 		goto err;
1364 
1365 	new_hashinfo->ehash = vmalloc_huge(ehash_entries * sizeof(struct inet_ehash_bucket),
1366 					   GFP_KERNEL_ACCOUNT);
1367 	if (!new_hashinfo->ehash)
1368 		goto free_hashinfo;
1369 
1370 	new_hashinfo->ehash_mask = ehash_entries - 1;
1371 
1372 	if (inet_ehash_locks_alloc(new_hashinfo))
1373 		goto free_ehash;
1374 
1375 	for (i = 0; i < ehash_entries; i++)
1376 		INIT_HLIST_NULLS_HEAD(&new_hashinfo->ehash[i].chain, i);
1377 
1378 	new_hashinfo->pernet = true;
1379 
1380 	return new_hashinfo;
1381 
1382 free_ehash:
1383 	vfree(new_hashinfo->ehash);
1384 free_hashinfo:
1385 	kfree(new_hashinfo);
1386 err:
1387 	return NULL;
1388 }
1389 
1390 void inet_pernet_hashinfo_free(struct inet_hashinfo *hashinfo)
1391 {
1392 	if (!hashinfo->pernet)
1393 		return;
1394 
1395 	inet_ehash_locks_free(hashinfo);
1396 	vfree(hashinfo->ehash);
1397 	kfree(hashinfo);
1398 }
1399