Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
20#include <net/addrconf.h>
21#include <net/inet_connection_sock.h>
22#include <net/inet_hashtables.h>
23#if IS_ENABLED(CONFIG_IPV6)
24#include <net/inet6_hashtables.h>
25#endif
26#include <net/secure_seq.h>
27#include <net/ip.h>
28#include <net/tcp.h>
29#include <net/sock_reuseport.h>
30
31static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
32 const __u16 lport, const __be32 faddr,
33 const __be16 fport)
34{
35 static u32 inet_ehash_secret __read_mostly;
36
37 net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
38
39 return __inet_ehashfn(laddr, lport, faddr, fport,
40 inet_ehash_secret + net_hash_mix(net));
41}
42
43/* This function handles inet_sock, but also timewait and request sockets
44 * for IPv4/IPv6.
45 */
46static u32 sk_ehashfn(const struct sock *sk)
47{
48#if IS_ENABLED(CONFIG_IPV6)
49 if (sk->sk_family == AF_INET6 &&
50 !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
51 return inet6_ehashfn(sock_net(sk),
52 &sk->sk_v6_rcv_saddr, sk->sk_num,
53 &sk->sk_v6_daddr, sk->sk_dport);
54#endif
55 return inet_ehashfn(sock_net(sk),
56 sk->sk_rcv_saddr, sk->sk_num,
57 sk->sk_daddr, sk->sk_dport);
58}
59
60/*
61 * Allocate and initialize a new local port bind bucket.
62 * The bindhash mutex for snum's hash chain must be held here.
63 */
64struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
65 struct net *net,
66 struct inet_bind_hashbucket *head,
67 const unsigned short snum,
68 int l3mdev)
69{
70 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
71
72 if (tb) {
73 write_pnet(&tb->ib_net, net);
74 tb->l3mdev = l3mdev;
75 tb->port = snum;
76 tb->fastreuse = 0;
77 tb->fastreuseport = 0;
78 INIT_HLIST_HEAD(&tb->owners);
79 hlist_add_head(&tb->node, &head->chain);
80 }
81 return tb;
82}
83
84struct inet_bind2_bucket *inet_bind2_bucket_create(struct kmem_cache *cachep,
85 struct net *net,
86 struct inet_bind2_hashbucket *head,
87 const unsigned short port,
88 int l3mdev,
89 const struct sock *sk)
90{
91 struct inet_bind2_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
92
93 if (tb) {
94 write_pnet(&tb->ib_net, net);
95 tb->l3mdev = l3mdev;
96 tb->port = port;
97#if IS_ENABLED(CONFIG_IPV6)
98 if (sk->sk_family == AF_INET6)
99 tb->v6_rcv_saddr = sk->sk_v6_rcv_saddr;
100 else
101#endif
102 tb->rcv_saddr = sk->sk_rcv_saddr;
103 INIT_HLIST_HEAD(&tb->owners);
104 hlist_add_head(&tb->node, &head->chain);
105 }
106 return tb;
107}
108
109static bool bind2_bucket_addr_match(struct inet_bind2_bucket *tb2, struct sock *sk)
110{
111#if IS_ENABLED(CONFIG_IPV6)
112 if (sk->sk_family == AF_INET6)
113 return ipv6_addr_equal(&tb2->v6_rcv_saddr,
114 &sk->sk_v6_rcv_saddr);
115#endif
116 return tb2->rcv_saddr == sk->sk_rcv_saddr;
117}
118
119/*
120 * Caller must hold hashbucket lock for this tb with local BH disabled
121 */
122void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
123{
124 if (hlist_empty(&tb->owners)) {
125 __hlist_del(&tb->node);
126 kmem_cache_free(cachep, tb);
127 }
128}
129
130/* Caller must hold the lock for the corresponding hashbucket in the bhash table
131 * with local BH disabled
132 */
133void inet_bind2_bucket_destroy(struct kmem_cache *cachep, struct inet_bind2_bucket *tb)
134{
135 if (hlist_empty(&tb->owners)) {
136 __hlist_del(&tb->node);
137 kmem_cache_free(cachep, tb);
138 }
139}
140
141void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
142 struct inet_bind2_bucket *tb2, const unsigned short snum)
143{
144 inet_sk(sk)->inet_num = snum;
145 sk_add_bind_node(sk, &tb->owners);
146 inet_csk(sk)->icsk_bind_hash = tb;
147 sk_add_bind2_node(sk, &tb2->owners);
148 inet_csk(sk)->icsk_bind2_hash = tb2;
149}
150
151/*
152 * Get rid of any references to a local port held by the given sock.
153 */
154static void __inet_put_port(struct sock *sk)
155{
156 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
157 const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
158 hashinfo->bhash_size);
159 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
160 struct inet_bind2_bucket *tb2;
161 struct inet_bind_bucket *tb;
162
163 spin_lock(&head->lock);
164 tb = inet_csk(sk)->icsk_bind_hash;
165 __sk_del_bind_node(sk);
166 inet_csk(sk)->icsk_bind_hash = NULL;
167 inet_sk(sk)->inet_num = 0;
168 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
169
170 if (inet_csk(sk)->icsk_bind2_hash) {
171 tb2 = inet_csk(sk)->icsk_bind2_hash;
172 __sk_del_bind2_node(sk);
173 inet_csk(sk)->icsk_bind2_hash = NULL;
174 inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2);
175 }
176 spin_unlock(&head->lock);
177}
178
179void inet_put_port(struct sock *sk)
180{
181 local_bh_disable();
182 __inet_put_port(sk);
183 local_bh_enable();
184}
185EXPORT_SYMBOL(inet_put_port);
186
187int __inet_inherit_port(const struct sock *sk, struct sock *child)
188{
189 struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
190 unsigned short port = inet_sk(child)->inet_num;
191 const int bhash = inet_bhashfn(sock_net(sk), port,
192 table->bhash_size);
193 struct inet_bind_hashbucket *head = &table->bhash[bhash];
194 struct inet_bind2_hashbucket *head_bhash2;
195 bool created_inet_bind_bucket = false;
196 struct net *net = sock_net(sk);
197 struct inet_bind2_bucket *tb2;
198 struct inet_bind_bucket *tb;
199 int l3mdev;
200
201 spin_lock(&head->lock);
202 tb = inet_csk(sk)->icsk_bind_hash;
203 tb2 = inet_csk(sk)->icsk_bind2_hash;
204 if (unlikely(!tb || !tb2)) {
205 spin_unlock(&head->lock);
206 return -ENOENT;
207 }
208 if (tb->port != port) {
209 l3mdev = inet_sk_bound_l3mdev(sk);
210
211 /* NOTE: using tproxy and redirecting skbs to a proxy
212 * on a different listener port breaks the assumption
213 * that the listener socket's icsk_bind_hash is the same
214 * as that of the child socket. We have to look up or
215 * create a new bind bucket for the child here. */
216 inet_bind_bucket_for_each(tb, &head->chain) {
217 if (check_bind_bucket_match(tb, net, port, l3mdev))
218 break;
219 }
220 if (!tb) {
221 tb = inet_bind_bucket_create(table->bind_bucket_cachep,
222 net, head, port, l3mdev);
223 if (!tb) {
224 spin_unlock(&head->lock);
225 return -ENOMEM;
226 }
227 created_inet_bind_bucket = true;
228 }
229 inet_csk_update_fastreuse(tb, child);
230
231 goto bhash2_find;
232 } else if (!bind2_bucket_addr_match(tb2, child)) {
233 l3mdev = inet_sk_bound_l3mdev(sk);
234
235bhash2_find:
236 tb2 = inet_bind2_bucket_find(table, net, port, l3mdev, child,
237 &head_bhash2);
238 if (!tb2) {
239 tb2 = inet_bind2_bucket_create(table->bind2_bucket_cachep,
240 net, head_bhash2, port,
241 l3mdev, child);
242 if (!tb2)
243 goto error;
244 }
245 }
246 inet_bind_hash(child, tb, tb2, port);
247 spin_unlock(&head->lock);
248
249 return 0;
250
251error:
252 if (created_inet_bind_bucket)
253 inet_bind_bucket_destroy(table->bind_bucket_cachep, tb);
254 spin_unlock(&head->lock);
255 return -ENOMEM;
256}
257EXPORT_SYMBOL_GPL(__inet_inherit_port);
258
259static struct inet_listen_hashbucket *
260inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
261{
262 u32 hash;
263
264#if IS_ENABLED(CONFIG_IPV6)
265 if (sk->sk_family == AF_INET6)
266 hash = ipv6_portaddr_hash(sock_net(sk),
267 &sk->sk_v6_rcv_saddr,
268 inet_sk(sk)->inet_num);
269 else
270#endif
271 hash = ipv4_portaddr_hash(sock_net(sk),
272 inet_sk(sk)->inet_rcv_saddr,
273 inet_sk(sk)->inet_num);
274 return inet_lhash2_bucket(h, hash);
275}
276
277static inline int compute_score(struct sock *sk, struct net *net,
278 const unsigned short hnum, const __be32 daddr,
279 const int dif, const int sdif)
280{
281 int score = -1;
282
283 if (net_eq(sock_net(sk), net) && sk->sk_num == hnum &&
284 !ipv6_only_sock(sk)) {
285 if (sk->sk_rcv_saddr != daddr)
286 return -1;
287
288 if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
289 return -1;
290 score = sk->sk_bound_dev_if ? 2 : 1;
291
292 if (sk->sk_family == PF_INET)
293 score++;
294 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
295 score++;
296 }
297 return score;
298}
299
300static inline struct sock *lookup_reuseport(struct net *net, struct sock *sk,
301 struct sk_buff *skb, int doff,
302 __be32 saddr, __be16 sport,
303 __be32 daddr, unsigned short hnum)
304{
305 struct sock *reuse_sk = NULL;
306 u32 phash;
307
308 if (sk->sk_reuseport) {
309 phash = inet_ehashfn(net, daddr, hnum, saddr, sport);
310 reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
311 }
312 return reuse_sk;
313}
314
315/*
316 * Here are some nice properties to exploit here. The BSD API
317 * does not allow a listening sock to specify the remote port nor the
318 * remote address for the connection. So always assume those are both
319 * wildcarded during the search since they can never be otherwise.
320 */
321
322/* called with rcu_read_lock() : No refcount taken on the socket */
323static struct sock *inet_lhash2_lookup(struct net *net,
324 struct inet_listen_hashbucket *ilb2,
325 struct sk_buff *skb, int doff,
326 const __be32 saddr, __be16 sport,
327 const __be32 daddr, const unsigned short hnum,
328 const int dif, const int sdif)
329{
330 struct sock *sk, *result = NULL;
331 struct hlist_nulls_node *node;
332 int score, hiscore = 0;
333
334 sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
335 score = compute_score(sk, net, hnum, daddr, dif, sdif);
336 if (score > hiscore) {
337 result = lookup_reuseport(net, sk, skb, doff,
338 saddr, sport, daddr, hnum);
339 if (result)
340 return result;
341
342 result = sk;
343 hiscore = score;
344 }
345 }
346
347 return result;
348}
349
350static inline struct sock *inet_lookup_run_bpf(struct net *net,
351 struct inet_hashinfo *hashinfo,
352 struct sk_buff *skb, int doff,
353 __be32 saddr, __be16 sport,
354 __be32 daddr, u16 hnum, const int dif)
355{
356 struct sock *sk, *reuse_sk;
357 bool no_reuseport;
358
359 if (hashinfo != &tcp_hashinfo)
360 return NULL; /* only TCP is supported */
361
362 no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_TCP, saddr, sport,
363 daddr, hnum, dif, &sk);
364 if (no_reuseport || IS_ERR_OR_NULL(sk))
365 return sk;
366
367 reuse_sk = lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum);
368 if (reuse_sk)
369 sk = reuse_sk;
370 return sk;
371}
372
373struct sock *__inet_lookup_listener(struct net *net,
374 struct inet_hashinfo *hashinfo,
375 struct sk_buff *skb, int doff,
376 const __be32 saddr, __be16 sport,
377 const __be32 daddr, const unsigned short hnum,
378 const int dif, const int sdif)
379{
380 struct inet_listen_hashbucket *ilb2;
381 struct sock *result = NULL;
382 unsigned int hash2;
383
384 /* Lookup redirect from BPF */
385 if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
386 result = inet_lookup_run_bpf(net, hashinfo, skb, doff,
387 saddr, sport, daddr, hnum, dif);
388 if (result)
389 goto done;
390 }
391
392 hash2 = ipv4_portaddr_hash(net, daddr, hnum);
393 ilb2 = inet_lhash2_bucket(hashinfo, hash2);
394
395 result = inet_lhash2_lookup(net, ilb2, skb, doff,
396 saddr, sport, daddr, hnum,
397 dif, sdif);
398 if (result)
399 goto done;
400
401 /* Lookup lhash2 with INADDR_ANY */
402 hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
403 ilb2 = inet_lhash2_bucket(hashinfo, hash2);
404
405 result = inet_lhash2_lookup(net, ilb2, skb, doff,
406 saddr, sport, htonl(INADDR_ANY), hnum,
407 dif, sdif);
408done:
409 if (IS_ERR(result))
410 return NULL;
411 return result;
412}
413EXPORT_SYMBOL_GPL(__inet_lookup_listener);
414
415/* All sockets share common refcount, but have different destructors */
416void sock_gen_put(struct sock *sk)
417{
418 if (!refcount_dec_and_test(&sk->sk_refcnt))
419 return;
420
421 if (sk->sk_state == TCP_TIME_WAIT)
422 inet_twsk_free(inet_twsk(sk));
423 else if (sk->sk_state == TCP_NEW_SYN_RECV)
424 reqsk_free(inet_reqsk(sk));
425 else
426 sk_free(sk);
427}
428EXPORT_SYMBOL_GPL(sock_gen_put);
429
430void sock_edemux(struct sk_buff *skb)
431{
432 sock_gen_put(skb->sk);
433}
434EXPORT_SYMBOL(sock_edemux);
435
436struct sock *__inet_lookup_established(struct net *net,
437 struct inet_hashinfo *hashinfo,
438 const __be32 saddr, const __be16 sport,
439 const __be32 daddr, const u16 hnum,
440 const int dif, const int sdif)
441{
442 INET_ADDR_COOKIE(acookie, saddr, daddr);
443 const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
444 struct sock *sk;
445 const struct hlist_nulls_node *node;
446 /* Optimize here for direct hit, only listening connections can
447 * have wildcards anyways.
448 */
449 unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
450 unsigned int slot = hash & hashinfo->ehash_mask;
451 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
452
453begin:
454 sk_nulls_for_each_rcu(sk, node, &head->chain) {
455 if (sk->sk_hash != hash)
456 continue;
457 if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
458 if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
459 goto out;
460 if (unlikely(!inet_match(net, sk, acookie,
461 ports, dif, sdif))) {
462 sock_gen_put(sk);
463 goto begin;
464 }
465 goto found;
466 }
467 }
468 /*
469 * if the nulls value we got at the end of this lookup is
470 * not the expected one, we must restart lookup.
471 * We probably met an item that was moved to another chain.
472 */
473 if (get_nulls_value(node) != slot)
474 goto begin;
475out:
476 sk = NULL;
477found:
478 return sk;
479}
480EXPORT_SYMBOL_GPL(__inet_lookup_established);
481
482/* called with local bh disabled */
483static int __inet_check_established(struct inet_timewait_death_row *death_row,
484 struct sock *sk, __u16 lport,
485 struct inet_timewait_sock **twp)
486{
487 struct inet_hashinfo *hinfo = death_row->hashinfo;
488 struct inet_sock *inet = inet_sk(sk);
489 __be32 daddr = inet->inet_rcv_saddr;
490 __be32 saddr = inet->inet_daddr;
491 int dif = sk->sk_bound_dev_if;
492 struct net *net = sock_net(sk);
493 int sdif = l3mdev_master_ifindex_by_index(net, dif);
494 INET_ADDR_COOKIE(acookie, saddr, daddr);
495 const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
496 unsigned int hash = inet_ehashfn(net, daddr, lport,
497 saddr, inet->inet_dport);
498 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
499 spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
500 struct sock *sk2;
501 const struct hlist_nulls_node *node;
502 struct inet_timewait_sock *tw = NULL;
503
504 spin_lock(lock);
505
506 sk_nulls_for_each(sk2, node, &head->chain) {
507 if (sk2->sk_hash != hash)
508 continue;
509
510 if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) {
511 if (sk2->sk_state == TCP_TIME_WAIT) {
512 tw = inet_twsk(sk2);
513 if (twsk_unique(sk, sk2, twp))
514 break;
515 }
516 goto not_unique;
517 }
518 }
519
520 /* Must record num and sport now. Otherwise we will see
521 * in hash table socket with a funny identity.
522 */
523 inet->inet_num = lport;
524 inet->inet_sport = htons(lport);
525 sk->sk_hash = hash;
526 WARN_ON(!sk_unhashed(sk));
527 __sk_nulls_add_node_rcu(sk, &head->chain);
528 if (tw) {
529 sk_nulls_del_node_init_rcu((struct sock *)tw);
530 __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
531 }
532 spin_unlock(lock);
533 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
534
535 if (twp) {
536 *twp = tw;
537 } else if (tw) {
538 /* Silly. Should hash-dance instead... */
539 inet_twsk_deschedule_put(tw);
540 }
541 return 0;
542
543not_unique:
544 spin_unlock(lock);
545 return -EADDRNOTAVAIL;
546}
547
548static u64 inet_sk_port_offset(const struct sock *sk)
549{
550 const struct inet_sock *inet = inet_sk(sk);
551
552 return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
553 inet->inet_daddr,
554 inet->inet_dport);
555}
556
557/* Searches for an exsiting socket in the ehash bucket list.
558 * Returns true if found, false otherwise.
559 */
560static bool inet_ehash_lookup_by_sk(struct sock *sk,
561 struct hlist_nulls_head *list)
562{
563 const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num);
564 const int sdif = sk->sk_bound_dev_if;
565 const int dif = sk->sk_bound_dev_if;
566 const struct hlist_nulls_node *node;
567 struct net *net = sock_net(sk);
568 struct sock *esk;
569
570 INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
571
572 sk_nulls_for_each_rcu(esk, node, list) {
573 if (esk->sk_hash != sk->sk_hash)
574 continue;
575 if (sk->sk_family == AF_INET) {
576 if (unlikely(inet_match(net, esk, acookie,
577 ports, dif, sdif))) {
578 return true;
579 }
580 }
581#if IS_ENABLED(CONFIG_IPV6)
582 else if (sk->sk_family == AF_INET6) {
583 if (unlikely(inet6_match(net, esk,
584 &sk->sk_v6_daddr,
585 &sk->sk_v6_rcv_saddr,
586 ports, dif, sdif))) {
587 return true;
588 }
589 }
590#endif
591 }
592 return false;
593}
594
595/* Insert a socket into ehash, and eventually remove another one
596 * (The another one can be a SYN_RECV or TIMEWAIT)
597 * If an existing socket already exists, socket sk is not inserted,
598 * and sets found_dup_sk parameter to true.
599 */
600bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
601{
602 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
603 struct hlist_nulls_head *list;
604 struct inet_ehash_bucket *head;
605 spinlock_t *lock;
606 bool ret = true;
607
608 WARN_ON_ONCE(!sk_unhashed(sk));
609
610 sk->sk_hash = sk_ehashfn(sk);
611 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
612 list = &head->chain;
613 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
614
615 spin_lock(lock);
616 if (osk) {
617 WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
618 ret = sk_nulls_del_node_init_rcu(osk);
619 } else if (found_dup_sk) {
620 *found_dup_sk = inet_ehash_lookup_by_sk(sk, list);
621 if (*found_dup_sk)
622 ret = false;
623 }
624
625 if (ret)
626 __sk_nulls_add_node_rcu(sk, list);
627
628 spin_unlock(lock);
629
630 return ret;
631}
632
633bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
634{
635 bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
636
637 if (ok) {
638 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
639 } else {
640 this_cpu_inc(*sk->sk_prot->orphan_count);
641 inet_sk_set_state(sk, TCP_CLOSE);
642 sock_set_flag(sk, SOCK_DEAD);
643 inet_csk_destroy_sock(sk);
644 }
645 return ok;
646}
647EXPORT_SYMBOL_GPL(inet_ehash_nolisten);
648
649static int inet_reuseport_add_sock(struct sock *sk,
650 struct inet_listen_hashbucket *ilb)
651{
652 struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
653 const struct hlist_nulls_node *node;
654 struct sock *sk2;
655 kuid_t uid = sock_i_uid(sk);
656
657 sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
658 if (sk2 != sk &&
659 sk2->sk_family == sk->sk_family &&
660 ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
661 sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
662 inet_csk(sk2)->icsk_bind_hash == tb &&
663 sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
664 inet_rcv_saddr_equal(sk, sk2, false))
665 return reuseport_add_sock(sk, sk2,
666 inet_rcv_saddr_any(sk));
667 }
668
669 return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
670}
671
672int __inet_hash(struct sock *sk, struct sock *osk)
673{
674 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
675 struct inet_listen_hashbucket *ilb2;
676 int err = 0;
677
678 if (sk->sk_state != TCP_LISTEN) {
679 local_bh_disable();
680 inet_ehash_nolisten(sk, osk, NULL);
681 local_bh_enable();
682 return 0;
683 }
684 WARN_ON(!sk_unhashed(sk));
685 ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
686
687 spin_lock(&ilb2->lock);
688 if (sk->sk_reuseport) {
689 err = inet_reuseport_add_sock(sk, ilb2);
690 if (err)
691 goto unlock;
692 }
693 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
694 sk->sk_family == AF_INET6)
695 __sk_nulls_add_node_tail_rcu(sk, &ilb2->nulls_head);
696 else
697 __sk_nulls_add_node_rcu(sk, &ilb2->nulls_head);
698 sock_set_flag(sk, SOCK_RCU_FREE);
699 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
700unlock:
701 spin_unlock(&ilb2->lock);
702
703 return err;
704}
705EXPORT_SYMBOL(__inet_hash);
706
707int inet_hash(struct sock *sk)
708{
709 int err = 0;
710
711 if (sk->sk_state != TCP_CLOSE)
712 err = __inet_hash(sk, NULL);
713
714 return err;
715}
716EXPORT_SYMBOL_GPL(inet_hash);
717
718void inet_unhash(struct sock *sk)
719{
720 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
721
722 if (sk_unhashed(sk))
723 return;
724
725 if (sk->sk_state == TCP_LISTEN) {
726 struct inet_listen_hashbucket *ilb2;
727
728 ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
729 /* Don't disable bottom halves while acquiring the lock to
730 * avoid circular locking dependency on PREEMPT_RT.
731 */
732 spin_lock(&ilb2->lock);
733 if (sk_unhashed(sk)) {
734 spin_unlock(&ilb2->lock);
735 return;
736 }
737
738 if (rcu_access_pointer(sk->sk_reuseport_cb))
739 reuseport_stop_listen_sock(sk);
740
741 __sk_nulls_del_node_init_rcu(sk);
742 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
743 spin_unlock(&ilb2->lock);
744 } else {
745 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
746
747 spin_lock_bh(lock);
748 if (sk_unhashed(sk)) {
749 spin_unlock_bh(lock);
750 return;
751 }
752 __sk_nulls_del_node_init_rcu(sk);
753 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
754 spin_unlock_bh(lock);
755 }
756}
757EXPORT_SYMBOL_GPL(inet_unhash);
758
759static bool check_bind2_bucket_match(struct inet_bind2_bucket *tb,
760 struct net *net, unsigned short port,
761 int l3mdev, struct sock *sk)
762{
763#if IS_ENABLED(CONFIG_IPV6)
764 if (sk->sk_family == AF_INET6)
765 return net_eq(ib2_net(tb), net) && tb->port == port &&
766 tb->l3mdev == l3mdev &&
767 ipv6_addr_equal(&tb->v6_rcv_saddr, &sk->sk_v6_rcv_saddr);
768 else
769#endif
770 return net_eq(ib2_net(tb), net) && tb->port == port &&
771 tb->l3mdev == l3mdev && tb->rcv_saddr == sk->sk_rcv_saddr;
772}
773
774bool check_bind2_bucket_match_nulladdr(struct inet_bind2_bucket *tb,
775 struct net *net, const unsigned short port,
776 int l3mdev, const struct sock *sk)
777{
778#if IS_ENABLED(CONFIG_IPV6)
779 struct in6_addr nulladdr = {};
780
781 if (sk->sk_family == AF_INET6)
782 return net_eq(ib2_net(tb), net) && tb->port == port &&
783 tb->l3mdev == l3mdev &&
784 ipv6_addr_equal(&tb->v6_rcv_saddr, &nulladdr);
785 else
786#endif
787 return net_eq(ib2_net(tb), net) && tb->port == port &&
788 tb->l3mdev == l3mdev && tb->rcv_saddr == 0;
789}
790
791static struct inet_bind2_hashbucket *
792inet_bhashfn_portaddr(struct inet_hashinfo *hinfo, const struct sock *sk,
793 const struct net *net, unsigned short port)
794{
795 u32 hash;
796
797#if IS_ENABLED(CONFIG_IPV6)
798 if (sk->sk_family == AF_INET6)
799 hash = ipv6_portaddr_hash(net, &sk->sk_v6_rcv_saddr, port);
800 else
801#endif
802 hash = ipv4_portaddr_hash(net, sk->sk_rcv_saddr, port);
803 return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
804}
805
806/* This should only be called when the spinlock for the socket's corresponding
807 * bind_hashbucket is held
808 */
809struct inet_bind2_bucket *
810inet_bind2_bucket_find(struct inet_hashinfo *hinfo, struct net *net,
811 const unsigned short port, int l3mdev, struct sock *sk,
812 struct inet_bind2_hashbucket **head)
813{
814 struct inet_bind2_bucket *bhash2 = NULL;
815 struct inet_bind2_hashbucket *h;
816
817 h = inet_bhashfn_portaddr(hinfo, sk, net, port);
818 inet_bind_bucket_for_each(bhash2, &h->chain) {
819 if (check_bind2_bucket_match(bhash2, net, port, l3mdev, sk))
820 break;
821 }
822
823 if (head)
824 *head = h;
825
826 return bhash2;
827}
828
829/* RFC 6056 3.3.4. Algorithm 4: Double-Hash Port Selection Algorithm
830 * Note that we use 32bit integers (vs RFC 'short integers')
831 * because 2^16 is not a multiple of num_ephemeral and this
832 * property might be used by clever attacker.
833 * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though
834 * attacks were since demonstrated, thus we use 65536 instead to really
835 * give more isolation and privacy, at the expense of 256kB of kernel
836 * memory.
837 */
838#define INET_TABLE_PERTURB_SHIFT 16
839#define INET_TABLE_PERTURB_SIZE (1 << INET_TABLE_PERTURB_SHIFT)
840static u32 *table_perturb;
841
842int __inet_hash_connect(struct inet_timewait_death_row *death_row,
843 struct sock *sk, u64 port_offset,
844 int (*check_established)(struct inet_timewait_death_row *,
845 struct sock *, __u16, struct inet_timewait_sock **))
846{
847 struct inet_hashinfo *hinfo = death_row->hashinfo;
848 struct inet_timewait_sock *tw = NULL;
849 struct inet_bind2_hashbucket *head2;
850 struct inet_bind_hashbucket *head;
851 int port = inet_sk(sk)->inet_num;
852 struct net *net = sock_net(sk);
853 struct inet_bind2_bucket *tb2;
854 struct inet_bind_bucket *tb;
855 bool tb_created = false;
856 u32 remaining, offset;
857 int ret, i, low, high;
858 int l3mdev;
859 u32 index;
860
861 if (port) {
862 head = &hinfo->bhash[inet_bhashfn(net, port,
863 hinfo->bhash_size)];
864 tb = inet_csk(sk)->icsk_bind_hash;
865 spin_lock_bh(&head->lock);
866 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
867 inet_ehash_nolisten(sk, NULL, NULL);
868 spin_unlock_bh(&head->lock);
869 return 0;
870 }
871 spin_unlock(&head->lock);
872 /* No definite answer... Walk to established hash table */
873 ret = check_established(death_row, sk, port, NULL);
874 local_bh_enable();
875 return ret;
876 }
877
878 l3mdev = inet_sk_bound_l3mdev(sk);
879
880 inet_get_local_port_range(net, &low, &high);
881 high++; /* [32768, 60999] -> [32768, 61000[ */
882 remaining = high - low;
883 if (likely(remaining > 1))
884 remaining &= ~1U;
885
886 net_get_random_once(table_perturb,
887 INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
888 index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
889
890 offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
891 offset %= remaining;
892
893 /* In first pass we try ports of @low parity.
894 * inet_csk_get_port() does the opposite choice.
895 */
896 offset &= ~1U;
897other_parity_scan:
898 port = low + offset;
899 for (i = 0; i < remaining; i += 2, port += 2) {
900 if (unlikely(port >= high))
901 port -= remaining;
902 if (inet_is_local_reserved_port(net, port))
903 continue;
904 head = &hinfo->bhash[inet_bhashfn(net, port,
905 hinfo->bhash_size)];
906 spin_lock_bh(&head->lock);
907
908 /* Does not bother with rcv_saddr checks, because
909 * the established check is already unique enough.
910 */
911 inet_bind_bucket_for_each(tb, &head->chain) {
912 if (check_bind_bucket_match(tb, net, port, l3mdev)) {
913 if (tb->fastreuse >= 0 ||
914 tb->fastreuseport >= 0)
915 goto next_port;
916 WARN_ON(hlist_empty(&tb->owners));
917 if (!check_established(death_row, sk,
918 port, &tw))
919 goto ok;
920 goto next_port;
921 }
922 }
923
924 tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
925 net, head, port, l3mdev);
926 if (!tb) {
927 spin_unlock_bh(&head->lock);
928 return -ENOMEM;
929 }
930 tb_created = true;
931 tb->fastreuse = -1;
932 tb->fastreuseport = -1;
933 goto ok;
934next_port:
935 spin_unlock_bh(&head->lock);
936 cond_resched();
937 }
938
939 offset++;
940 if ((offset & 1) && remaining > 1)
941 goto other_parity_scan;
942
943 return -EADDRNOTAVAIL;
944
945ok:
946 /* Find the corresponding tb2 bucket since we need to
947 * add the socket to the bhash2 table as well
948 */
949 tb2 = inet_bind2_bucket_find(hinfo, net, port, l3mdev, sk, &head2);
950 if (!tb2) {
951 tb2 = inet_bind2_bucket_create(hinfo->bind2_bucket_cachep, net,
952 head2, port, l3mdev, sk);
953 if (!tb2)
954 goto error;
955 }
956
957 /* Here we want to add a little bit of randomness to the next source
958 * port that will be chosen. We use a max() with a random here so that
959 * on low contention the randomness is maximal and on high contention
960 * it may be inexistent.
961 */
962 i = max_t(int, i, (prandom_u32() & 7) * 2);
963 WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
964
965 /* Head lock still held and bh's disabled */
966 inet_bind_hash(sk, tb, tb2, port);
967 if (sk_unhashed(sk)) {
968 inet_sk(sk)->inet_sport = htons(port);
969 inet_ehash_nolisten(sk, (struct sock *)tw, NULL);
970 }
971 if (tw)
972 inet_twsk_bind_unhash(tw, hinfo);
973 spin_unlock(&head->lock);
974 if (tw)
975 inet_twsk_deschedule_put(tw);
976 local_bh_enable();
977 return 0;
978
979error:
980 if (tb_created)
981 inet_bind_bucket_destroy(hinfo->bind_bucket_cachep, tb);
982 spin_unlock_bh(&head->lock);
983 return -ENOMEM;
984}
985
986/*
987 * Bind a port for a connect operation and hash it.
988 */
989int inet_hash_connect(struct inet_timewait_death_row *death_row,
990 struct sock *sk)
991{
992 u64 port_offset = 0;
993
994 if (!inet_sk(sk)->inet_num)
995 port_offset = inet_sk_port_offset(sk);
996 return __inet_hash_connect(death_row, sk, port_offset,
997 __inet_check_established);
998}
999EXPORT_SYMBOL_GPL(inet_hash_connect);
1000
1001static void init_hashinfo_lhash2(struct inet_hashinfo *h)
1002{
1003 int i;
1004
1005 for (i = 0; i <= h->lhash2_mask; i++) {
1006 spin_lock_init(&h->lhash2[i].lock);
1007 INIT_HLIST_NULLS_HEAD(&h->lhash2[i].nulls_head,
1008 i + LISTENING_NULLS_BASE);
1009 }
1010}
1011
1012void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
1013 unsigned long numentries, int scale,
1014 unsigned long low_limit,
1015 unsigned long high_limit)
1016{
1017 h->lhash2 = alloc_large_system_hash(name,
1018 sizeof(*h->lhash2),
1019 numentries,
1020 scale,
1021 0,
1022 NULL,
1023 &h->lhash2_mask,
1024 low_limit,
1025 high_limit);
1026 init_hashinfo_lhash2(h);
1027
1028 /* this one is used for source ports of outgoing connections */
1029 table_perturb = alloc_large_system_hash("Table-perturb",
1030 sizeof(*table_perturb),
1031 INET_TABLE_PERTURB_SIZE,
1032 0, 0, NULL, NULL,
1033 INET_TABLE_PERTURB_SIZE,
1034 INET_TABLE_PERTURB_SIZE);
1035}
1036
1037int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
1038{
1039 h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL);
1040 if (!h->lhash2)
1041 return -ENOMEM;
1042
1043 h->lhash2_mask = INET_LHTABLE_SIZE - 1;
1044 /* INET_LHTABLE_SIZE must be a power of 2 */
1045 BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
1046
1047 init_hashinfo_lhash2(h);
1048 return 0;
1049}
1050EXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod);
1051
1052int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
1053{
1054 unsigned int locksz = sizeof(spinlock_t);
1055 unsigned int i, nblocks = 1;
1056
1057 if (locksz != 0) {
1058 /* allocate 2 cache lines or at least one spinlock per cpu */
1059 nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U);
1060 nblocks = roundup_pow_of_two(nblocks * num_possible_cpus());
1061
1062 /* no more locks than number of hash buckets */
1063 nblocks = min(nblocks, hashinfo->ehash_mask + 1);
1064
1065 hashinfo->ehash_locks = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
1066 if (!hashinfo->ehash_locks)
1067 return -ENOMEM;
1068
1069 for (i = 0; i < nblocks; i++)
1070 spin_lock_init(&hashinfo->ehash_locks[i]);
1071 }
1072 hashinfo->ehash_locks_mask = nblocks - 1;
1073 return 0;
1074}
1075EXPORT_SYMBOL_GPL(inet_ehash_locks_alloc);