1 // SPDX-License-Identifier: GPL-2.0-or-later
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.
7 * Generic INET transport hashtables
9 * Authors: Lotsa people, from code originally in tcp
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>
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>
26 #include <net/secure_seq.h>
29 #include <net/sock_reuseport.h>
31 static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
32 const __u16 lport, const __be32 faddr,
35 static u32 inet_ehash_secret __read_mostly;
37 net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
39 return __inet_ehashfn(laddr, lport, faddr, fport,
40 inet_ehash_secret + net_hash_mix(net));
43 /* This function handles inet_sock, but also timewait and request sockets
46 static u32 sk_ehashfn(const struct sock *sk)
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);
55 return inet_ehashfn(sock_net(sk),
56 sk->sk_rcv_saddr, sk->sk_num,
57 sk->sk_daddr, sk->sk_dport);
61 * Allocate and initialize a new local port bind bucket.
62 * The bindhash mutex for snum's hash chain must be held here.
64 struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
66 struct inet_bind_hashbucket *head,
67 const unsigned short snum,
70 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
73 write_pnet(&tb->ib_net, net);
77 tb->fastreuseport = 0;
78 INIT_HLIST_HEAD(&tb->owners);
79 hlist_add_head(&tb->node, &head->chain);
84 struct inet_bind2_bucket *inet_bind2_bucket_create(struct kmem_cache *cachep,
86 struct inet_bind2_hashbucket *head,
87 const unsigned short port,
89 const struct sock *sk)
91 struct inet_bind2_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
94 write_pnet(&tb->ib_net, net);
97 #if IS_ENABLED(CONFIG_IPV6)
98 if (sk->sk_family == AF_INET6)
99 tb->v6_rcv_saddr = sk->sk_v6_rcv_saddr;
102 tb->rcv_saddr = sk->sk_rcv_saddr;
103 INIT_HLIST_HEAD(&tb->owners);
104 hlist_add_head(&tb->node, &head->chain);
109 static bool bind2_bucket_addr_match(struct inet_bind2_bucket *tb2, struct sock *sk)
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);
116 return tb2->rcv_saddr == sk->sk_rcv_saddr;
120 * Caller must hold hashbucket lock for this tb with local BH disabled
122 void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
124 if (hlist_empty(&tb->owners)) {
125 __hlist_del(&tb->node);
126 kmem_cache_free(cachep, tb);
130 /* Caller must hold the lock for the corresponding hashbucket in the bhash table
131 * with local BH disabled
133 void inet_bind2_bucket_destroy(struct kmem_cache *cachep, struct inet_bind2_bucket *tb)
135 if (hlist_empty(&tb->owners)) {
136 __hlist_del(&tb->node);
137 kmem_cache_free(cachep, tb);
141 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
142 struct inet_bind2_bucket *tb2, const unsigned short snum)
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;
152 * Get rid of any references to a local port held by the given sock.
154 static void __inet_put_port(struct sock *sk)
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;
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);
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);
176 spin_unlock(&head->lock);
179 void inet_put_port(struct sock *sk)
185 EXPORT_SYMBOL(inet_put_port);
187 int __inet_inherit_port(const struct sock *sk, struct sock *child)
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,
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;
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);
208 if (tb->port != port) {
209 l3mdev = inet_sk_bound_l3mdev(sk);
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))
221 tb = inet_bind_bucket_create(table->bind_bucket_cachep,
222 net, head, port, l3mdev);
224 spin_unlock(&head->lock);
227 created_inet_bind_bucket = true;
229 inet_csk_update_fastreuse(tb, child);
232 } else if (!bind2_bucket_addr_match(tb2, child)) {
233 l3mdev = inet_sk_bound_l3mdev(sk);
236 tb2 = inet_bind2_bucket_find(table, net, port, l3mdev, child,
239 tb2 = inet_bind2_bucket_create(table->bind2_bucket_cachep,
240 net, head_bhash2, port,
246 inet_bind_hash(child, tb, tb2, port);
247 spin_unlock(&head->lock);
252 if (created_inet_bind_bucket)
253 inet_bind_bucket_destroy(table->bind_bucket_cachep, tb);
254 spin_unlock(&head->lock);
257 EXPORT_SYMBOL_GPL(__inet_inherit_port);
259 static struct inet_listen_hashbucket *
260 inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
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);
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);
277 static 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)
283 if (net_eq(sock_net(sk), net) && sk->sk_num == hnum &&
284 !ipv6_only_sock(sk)) {
285 if (sk->sk_rcv_saddr != daddr)
288 if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
290 score = sk->sk_bound_dev_if ? 2 : 1;
292 if (sk->sk_family == PF_INET)
294 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
300 static 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)
305 struct sock *reuse_sk = NULL;
308 if (sk->sk_reuseport) {
309 phash = inet_ehashfn(net, daddr, hnum, saddr, sport);
310 reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
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.
322 /* called with rcu_read_lock() : No refcount taken on the socket */
323 static 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)
330 struct sock *sk, *result = NULL;
331 struct hlist_nulls_node *node;
332 int score, hiscore = 0;
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);
350 static 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)
356 struct sock *sk, *reuse_sk;
359 if (hashinfo != &tcp_hashinfo)
360 return NULL; /* only TCP is supported */
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))
367 reuse_sk = lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum);
373 struct 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)
380 struct inet_listen_hashbucket *ilb2;
381 struct sock *result = NULL;
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);
392 hash2 = ipv4_portaddr_hash(net, daddr, hnum);
393 ilb2 = inet_lhash2_bucket(hashinfo, hash2);
395 result = inet_lhash2_lookup(net, ilb2, skb, doff,
396 saddr, sport, daddr, hnum,
401 /* Lookup lhash2 with INADDR_ANY */
402 hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
403 ilb2 = inet_lhash2_bucket(hashinfo, hash2);
405 result = inet_lhash2_lookup(net, ilb2, skb, doff,
406 saddr, sport, htonl(INADDR_ANY), hnum,
413 EXPORT_SYMBOL_GPL(__inet_lookup_listener);
415 /* All sockets share common refcount, but have different destructors */
416 void sock_gen_put(struct sock *sk)
418 if (!refcount_dec_and_test(&sk->sk_refcnt))
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));
428 EXPORT_SYMBOL_GPL(sock_gen_put);
430 void sock_edemux(struct sk_buff *skb)
432 sock_gen_put(skb->sk);
434 EXPORT_SYMBOL(sock_edemux);
436 struct 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)
442 INET_ADDR_COOKIE(acookie, saddr, daddr);
443 const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
445 const struct hlist_nulls_node *node;
446 /* Optimize here for direct hit, only listening connections can
447 * have wildcards anyways.
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];
454 sk_nulls_for_each_rcu(sk, node, &head->chain) {
455 if (sk->sk_hash != hash)
457 if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
458 if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
460 if (unlikely(!inet_match(net, sk, acookie,
461 ports, dif, sdif))) {
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.
473 if (get_nulls_value(node) != slot)
480 EXPORT_SYMBOL_GPL(__inet_lookup_established);
482 /* called with local bh disabled */
483 static int __inet_check_established(struct inet_timewait_death_row *death_row,
484 struct sock *sk, __u16 lport,
485 struct inet_timewait_sock **twp)
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);
501 const struct hlist_nulls_node *node;
502 struct inet_timewait_sock *tw = NULL;
506 sk_nulls_for_each(sk2, node, &head->chain) {
507 if (sk2->sk_hash != hash)
510 if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) {
511 if (sk2->sk_state == TCP_TIME_WAIT) {
513 if (twsk_unique(sk, sk2, twp))
520 /* Must record num and sport now. Otherwise we will see
521 * in hash table socket with a funny identity.
523 inet->inet_num = lport;
524 inet->inet_sport = htons(lport);
526 WARN_ON(!sk_unhashed(sk));
527 __sk_nulls_add_node_rcu(sk, &head->chain);
529 sk_nulls_del_node_init_rcu((struct sock *)tw);
530 __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
533 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
538 /* Silly. Should hash-dance instead... */
539 inet_twsk_deschedule_put(tw);
545 return -EADDRNOTAVAIL;
548 static u64 inet_sk_port_offset(const struct sock *sk)
550 const struct inet_sock *inet = inet_sk(sk);
552 return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
557 /* Searches for an exsiting socket in the ehash bucket list.
558 * Returns true if found, false otherwise.
560 static bool inet_ehash_lookup_by_sk(struct sock *sk,
561 struct hlist_nulls_head *list)
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);
570 INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
572 sk_nulls_for_each_rcu(esk, node, list) {
573 if (esk->sk_hash != sk->sk_hash)
575 if (sk->sk_family == AF_INET) {
576 if (unlikely(inet_match(net, esk, acookie,
577 ports, dif, sdif))) {
581 #if IS_ENABLED(CONFIG_IPV6)
582 else if (sk->sk_family == AF_INET6) {
583 if (unlikely(inet6_match(net, esk,
585 &sk->sk_v6_rcv_saddr,
586 ports, dif, sdif))) {
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.
600 bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
602 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
603 struct hlist_nulls_head *list;
604 struct inet_ehash_bucket *head;
608 WARN_ON_ONCE(!sk_unhashed(sk));
610 sk->sk_hash = sk_ehashfn(sk);
611 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
613 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
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);
626 __sk_nulls_add_node_rcu(sk, list);
633 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
635 bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
638 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
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);
647 EXPORT_SYMBOL_GPL(inet_ehash_nolisten);
649 static int inet_reuseport_add_sock(struct sock *sk,
650 struct inet_listen_hashbucket *ilb)
652 struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
653 const struct hlist_nulls_node *node;
655 kuid_t uid = sock_i_uid(sk);
657 sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
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));
669 return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
672 int __inet_hash(struct sock *sk, struct sock *osk)
674 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
675 struct inet_listen_hashbucket *ilb2;
678 if (sk->sk_state != TCP_LISTEN) {
680 inet_ehash_nolisten(sk, osk, NULL);
684 WARN_ON(!sk_unhashed(sk));
685 ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
687 spin_lock(&ilb2->lock);
688 if (sk->sk_reuseport) {
689 err = inet_reuseport_add_sock(sk, ilb2);
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);
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);
701 spin_unlock(&ilb2->lock);
705 EXPORT_SYMBOL(__inet_hash);
707 int inet_hash(struct sock *sk)
711 if (sk->sk_state != TCP_CLOSE)
712 err = __inet_hash(sk, NULL);
716 EXPORT_SYMBOL_GPL(inet_hash);
718 void inet_unhash(struct sock *sk)
720 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
725 if (sk->sk_state == TCP_LISTEN) {
726 struct inet_listen_hashbucket *ilb2;
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.
732 spin_lock(&ilb2->lock);
733 if (sk_unhashed(sk)) {
734 spin_unlock(&ilb2->lock);
738 if (rcu_access_pointer(sk->sk_reuseport_cb))
739 reuseport_stop_listen_sock(sk);
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);
745 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
748 if (sk_unhashed(sk)) {
749 spin_unlock_bh(lock);
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);
757 EXPORT_SYMBOL_GPL(inet_unhash);
759 static bool check_bind2_bucket_match(struct inet_bind2_bucket *tb,
760 struct net *net, unsigned short port,
761 int l3mdev, struct sock *sk)
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);
770 return net_eq(ib2_net(tb), net) && tb->port == port &&
771 tb->l3mdev == l3mdev && tb->rcv_saddr == sk->sk_rcv_saddr;
774 bool 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)
778 #if IS_ENABLED(CONFIG_IPV6)
779 struct in6_addr nulladdr = {};
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);
787 return net_eq(ib2_net(tb), net) && tb->port == port &&
788 tb->l3mdev == l3mdev && tb->rcv_saddr == 0;
791 static struct inet_bind2_hashbucket *
792 inet_bhashfn_portaddr(struct inet_hashinfo *hinfo, const struct sock *sk,
793 const struct net *net, unsigned short port)
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);
802 hash = ipv4_portaddr_hash(net, sk->sk_rcv_saddr, port);
803 return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
806 /* This should only be called when the spinlock for the socket's corresponding
807 * bind_hashbucket is held
809 struct inet_bind2_bucket *
810 inet_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)
814 struct inet_bind2_bucket *bhash2 = NULL;
815 struct inet_bind2_hashbucket *h;
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))
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
838 #define INET_TABLE_PERTURB_SHIFT 16
839 #define INET_TABLE_PERTURB_SIZE (1 << INET_TABLE_PERTURB_SHIFT)
840 static u32 *table_perturb;
842 int __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 **))
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;
862 head = &hinfo->bhash[inet_bhashfn(net, port,
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);
871 spin_unlock(&head->lock);
872 /* No definite answer... Walk to established hash table */
873 ret = check_established(death_row, sk, port, NULL);
878 l3mdev = inet_sk_bound_l3mdev(sk);
880 inet_get_local_port_range(net, &low, &high);
881 high++; /* [32768, 60999] -> [32768, 61000[ */
882 remaining = high - low;
883 if (likely(remaining > 1))
886 net_get_random_once(table_perturb,
887 INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
888 index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
890 offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
893 /* In first pass we try ports of @low parity.
894 * inet_csk_get_port() does the opposite choice.
899 for (i = 0; i < remaining; i += 2, port += 2) {
900 if (unlikely(port >= high))
902 if (inet_is_local_reserved_port(net, port))
904 head = &hinfo->bhash[inet_bhashfn(net, port,
906 spin_lock_bh(&head->lock);
908 /* Does not bother with rcv_saddr checks, because
909 * the established check is already unique enough.
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)
916 WARN_ON(hlist_empty(&tb->owners));
917 if (!check_established(death_row, sk,
924 tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
925 net, head, port, l3mdev);
927 spin_unlock_bh(&head->lock);
932 tb->fastreuseport = -1;
935 spin_unlock_bh(&head->lock);
940 if ((offset & 1) && remaining > 1)
941 goto other_parity_scan;
943 return -EADDRNOTAVAIL;
946 /* Find the corresponding tb2 bucket since we need to
947 * add the socket to the bhash2 table as well
949 tb2 = inet_bind2_bucket_find(hinfo, net, port, l3mdev, sk, &head2);
951 tb2 = inet_bind2_bucket_create(hinfo->bind2_bucket_cachep, net,
952 head2, port, l3mdev, sk);
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.
962 i = max_t(int, i, (prandom_u32() & 7) * 2);
963 WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
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);
972 inet_twsk_bind_unhash(tw, hinfo);
973 spin_unlock(&head->lock);
975 inet_twsk_deschedule_put(tw);
981 inet_bind_bucket_destroy(hinfo->bind_bucket_cachep, tb);
982 spin_unlock_bh(&head->lock);
987 * Bind a port for a connect operation and hash it.
989 int inet_hash_connect(struct inet_timewait_death_row *death_row,
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);
999 EXPORT_SYMBOL_GPL(inet_hash_connect);
1001 static void init_hashinfo_lhash2(struct inet_hashinfo *h)
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);
1012 void __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)
1017 h->lhash2 = alloc_large_system_hash(name,
1026 init_hashinfo_lhash2(h);
1028 /* this one is used for source ports of outgoing connections */
1029 table_perturb = kmalloc_array(INET_TABLE_PERTURB_SIZE,
1030 sizeof(*table_perturb), GFP_KERNEL);
1032 panic("TCP: failed to alloc table_perturb");
1035 int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
1037 h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL);
1041 h->lhash2_mask = INET_LHTABLE_SIZE - 1;
1042 /* INET_LHTABLE_SIZE must be a power of 2 */
1043 BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
1045 init_hashinfo_lhash2(h);
1048 EXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod);
1050 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
1052 unsigned int locksz = sizeof(spinlock_t);
1053 unsigned int i, nblocks = 1;
1056 /* allocate 2 cache lines or at least one spinlock per cpu */
1057 nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U);
1058 nblocks = roundup_pow_of_two(nblocks * num_possible_cpus());
1060 /* no more locks than number of hash buckets */
1061 nblocks = min(nblocks, hashinfo->ehash_mask + 1);
1063 hashinfo->ehash_locks = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
1064 if (!hashinfo->ehash_locks)
1067 for (i = 0; i < nblocks; i++)
1068 spin_lock_init(&hashinfo->ehash_locks[i]);
1070 hashinfo->ehash_locks_mask = nblocks - 1;
1073 EXPORT_SYMBOL_GPL(inet_ehash_locks_alloc);