12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 277d8bf9cSArnaldo Carvalho de Melo /* 377d8bf9cSArnaldo Carvalho de Melo * INET An implementation of the TCP/IP protocol suite for the LINUX 477d8bf9cSArnaldo Carvalho de Melo * operating system. INET is implemented using the BSD Socket 577d8bf9cSArnaldo Carvalho de Melo * interface as the means of communication with the user level. 677d8bf9cSArnaldo Carvalho de Melo * 777d8bf9cSArnaldo Carvalho de Melo * Generic INET transport hashtables 877d8bf9cSArnaldo Carvalho de Melo * 977d8bf9cSArnaldo Carvalho de Melo * Authors: Lotsa people, from code originally in tcp 1077d8bf9cSArnaldo Carvalho de Melo */ 1177d8bf9cSArnaldo Carvalho de Melo 122d8c4ce5SArnaldo Carvalho de Melo #include <linux/module.h> 13a7f5e7f1SArnaldo Carvalho de Melo #include <linux/random.h> 14f3f05f70SArnaldo Carvalho de Melo #include <linux/sched.h> 1577d8bf9cSArnaldo Carvalho de Melo #include <linux/slab.h> 16f3f05f70SArnaldo Carvalho de Melo #include <linux/wait.h> 17095dc8e0SEric Dumazet #include <linux/vmalloc.h> 1857c8a661SMike Rapoport #include <linux/memblock.h> 1977d8bf9cSArnaldo Carvalho de Melo 20c125e80bSCraig Gallek #include <net/addrconf.h> 21463c84b9SArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 2277d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 2301770a16SRicardo Dias #if IS_ENABLED(CONFIG_IPV6) 2401770a16SRicardo Dias #include <net/inet6_hashtables.h> 2501770a16SRicardo Dias #endif 266e5714eaSDavid S. Miller #include <net/secure_seq.h> 27a7f5e7f1SArnaldo Carvalho de Melo #include <net/ip.h> 28a04a480dSDavid Ahern #include <net/tcp.h> 29c125e80bSCraig Gallek #include <net/sock_reuseport.h> 3077d8bf9cSArnaldo Carvalho de Melo 310f495f76SLorenz Bauer u32 inet_ehashfn(const struct net *net, const __be32 laddr, 3265cd8033SHannes Frederic Sowa const __u16 lport, const __be32 faddr, 3365cd8033SHannes Frederic Sowa const __be16 fport) 3465cd8033SHannes Frederic Sowa { 351bbdceefSHannes Frederic Sowa static u32 inet_ehash_secret __read_mostly; 361bbdceefSHannes Frederic Sowa 371bbdceefSHannes Frederic Sowa net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret)); 381bbdceefSHannes Frederic Sowa 3965cd8033SHannes Frederic Sowa return __inet_ehashfn(laddr, lport, faddr, fport, 4065cd8033SHannes Frederic Sowa inet_ehash_secret + net_hash_mix(net)); 4165cd8033SHannes Frederic Sowa } 420f495f76SLorenz Bauer EXPORT_SYMBOL_GPL(inet_ehashfn); 4365cd8033SHannes Frederic Sowa 44d1e559d0SEric Dumazet /* This function handles inet_sock, but also timewait and request sockets 45d1e559d0SEric Dumazet * for IPv4/IPv6. 46d1e559d0SEric Dumazet */ 47784c372aSEric Dumazet static u32 sk_ehashfn(const struct sock *sk) 4865cd8033SHannes Frederic Sowa { 49d1e559d0SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 50d1e559d0SEric Dumazet if (sk->sk_family == AF_INET6 && 51d1e559d0SEric Dumazet !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) 52d1e559d0SEric Dumazet return inet6_ehashfn(sock_net(sk), 53d1e559d0SEric Dumazet &sk->sk_v6_rcv_saddr, sk->sk_num, 54d1e559d0SEric Dumazet &sk->sk_v6_daddr, sk->sk_dport); 55d1e559d0SEric Dumazet #endif 565b441f76SEric Dumazet return inet_ehashfn(sock_net(sk), 575b441f76SEric Dumazet sk->sk_rcv_saddr, sk->sk_num, 585b441f76SEric Dumazet sk->sk_daddr, sk->sk_dport); 5965cd8033SHannes Frederic Sowa } 6065cd8033SHannes Frederic Sowa 6177d8bf9cSArnaldo Carvalho de Melo /* 6277d8bf9cSArnaldo Carvalho de Melo * Allocate and initialize a new local port bind bucket. 6377d8bf9cSArnaldo Carvalho de Melo * The bindhash mutex for snum's hash chain must be held here. 6477d8bf9cSArnaldo Carvalho de Melo */ 65e18b890bSChristoph Lameter struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep, 66941b1d22SPavel Emelyanov struct net *net, 6777d8bf9cSArnaldo Carvalho de Melo struct inet_bind_hashbucket *head, 683c82a21fSRobert Shearman const unsigned short snum, 693c82a21fSRobert Shearman int l3mdev) 7077d8bf9cSArnaldo Carvalho de Melo { 7154e6ecb2SChristoph Lameter struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC); 7277d8bf9cSArnaldo Carvalho de Melo 7300db4124SIan Morris if (tb) { 74efd7ef1cSEric W. Biederman write_pnet(&tb->ib_net, net); 753c82a21fSRobert Shearman tb->l3mdev = l3mdev; 7677d8bf9cSArnaldo Carvalho de Melo tb->port = snum; 7777d8bf9cSArnaldo Carvalho de Melo tb->fastreuse = 0; 78da5e3630STom Herbert tb->fastreuseport = 0; 7977d8bf9cSArnaldo Carvalho de Melo INIT_HLIST_HEAD(&tb->owners); 8077d8bf9cSArnaldo Carvalho de Melo hlist_add_head(&tb->node, &head->chain); 8177d8bf9cSArnaldo Carvalho de Melo } 8277d8bf9cSArnaldo Carvalho de Melo return tb; 8377d8bf9cSArnaldo Carvalho de Melo } 8477d8bf9cSArnaldo Carvalho de Melo 8577d8bf9cSArnaldo Carvalho de Melo /* 8677d8bf9cSArnaldo Carvalho de Melo * Caller must hold hashbucket lock for this tb with local BH disabled 8777d8bf9cSArnaldo Carvalho de Melo */ 88e18b890bSChristoph Lameter void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb) 8977d8bf9cSArnaldo Carvalho de Melo { 9077d8bf9cSArnaldo Carvalho de Melo if (hlist_empty(&tb->owners)) { 9177d8bf9cSArnaldo Carvalho de Melo __hlist_del(&tb->node); 9277d8bf9cSArnaldo Carvalho de Melo kmem_cache_free(cachep, tb); 9377d8bf9cSArnaldo Carvalho de Melo } 9477d8bf9cSArnaldo Carvalho de Melo } 952d8c4ce5SArnaldo Carvalho de Melo 9628044fc1SJoanne Koong bool inet_bind_bucket_match(const struct inet_bind_bucket *tb, const struct net *net, 9728044fc1SJoanne Koong unsigned short port, int l3mdev) 982d8c4ce5SArnaldo Carvalho de Melo { 9928044fc1SJoanne Koong return net_eq(ib_net(tb), net) && tb->port == port && 10028044fc1SJoanne Koong tb->l3mdev == l3mdev; 10128044fc1SJoanne Koong } 10228044fc1SJoanne Koong 10328044fc1SJoanne Koong static void inet_bind2_bucket_init(struct inet_bind2_bucket *tb, 10428044fc1SJoanne Koong struct net *net, 10528044fc1SJoanne Koong struct inet_bind_hashbucket *head, 10628044fc1SJoanne Koong unsigned short port, int l3mdev, 10728044fc1SJoanne Koong const struct sock *sk) 10828044fc1SJoanne Koong { 10928044fc1SJoanne Koong write_pnet(&tb->ib_net, net); 11028044fc1SJoanne Koong tb->l3mdev = l3mdev; 11128044fc1SJoanne Koong tb->port = port; 11228044fc1SJoanne Koong #if IS_ENABLED(CONFIG_IPV6) 11328044fc1SJoanne Koong if (sk->sk_family == AF_INET6) 11428044fc1SJoanne Koong tb->v6_rcv_saddr = sk->sk_v6_rcv_saddr; 11528044fc1SJoanne Koong else 116*06a8c04fSKuniyuki Iwashima ipv6_addr_set_v4mapped(sk->sk_rcv_saddr, &tb->v6_rcv_saddr); 117*06a8c04fSKuniyuki Iwashima #else 11828044fc1SJoanne Koong tb->rcv_saddr = sk->sk_rcv_saddr; 119*06a8c04fSKuniyuki Iwashima #endif 12028044fc1SJoanne Koong INIT_HLIST_HEAD(&tb->owners); 121936a192fSKuniyuki Iwashima INIT_HLIST_HEAD(&tb->deathrow); 12228044fc1SJoanne Koong hlist_add_head(&tb->node, &head->chain); 12328044fc1SJoanne Koong } 12428044fc1SJoanne Koong 12528044fc1SJoanne Koong struct inet_bind2_bucket *inet_bind2_bucket_create(struct kmem_cache *cachep, 12628044fc1SJoanne Koong struct net *net, 12728044fc1SJoanne Koong struct inet_bind_hashbucket *head, 12828044fc1SJoanne Koong unsigned short port, 12928044fc1SJoanne Koong int l3mdev, 13028044fc1SJoanne Koong const struct sock *sk) 13128044fc1SJoanne Koong { 13228044fc1SJoanne Koong struct inet_bind2_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC); 13328044fc1SJoanne Koong 13428044fc1SJoanne Koong if (tb) 13528044fc1SJoanne Koong inet_bind2_bucket_init(tb, net, head, port, l3mdev, sk); 13628044fc1SJoanne Koong 13728044fc1SJoanne Koong return tb; 13828044fc1SJoanne Koong } 13928044fc1SJoanne Koong 14028044fc1SJoanne Koong /* Caller must hold hashbucket lock for this tb with local BH disabled */ 14128044fc1SJoanne Koong void inet_bind2_bucket_destroy(struct kmem_cache *cachep, struct inet_bind2_bucket *tb) 14228044fc1SJoanne Koong { 143936a192fSKuniyuki Iwashima if (hlist_empty(&tb->owners) && hlist_empty(&tb->deathrow)) { 14428044fc1SJoanne Koong __hlist_del(&tb->node); 14528044fc1SJoanne Koong kmem_cache_free(cachep, tb); 14628044fc1SJoanne Koong } 14728044fc1SJoanne Koong } 14828044fc1SJoanne Koong 14928044fc1SJoanne Koong static bool inet_bind2_bucket_addr_match(const struct inet_bind2_bucket *tb2, 15028044fc1SJoanne Koong const struct sock *sk) 15128044fc1SJoanne Koong { 15228044fc1SJoanne Koong #if IS_ENABLED(CONFIG_IPV6) 153*06a8c04fSKuniyuki Iwashima if (sk->sk_family == AF_INET6) 15456f3e3f0SKuniyuki Iwashima return ipv6_addr_equal(&tb2->v6_rcv_saddr, &sk->sk_v6_rcv_saddr); 1558702cf12SKuniyuki Iwashima 156*06a8c04fSKuniyuki Iwashima if (!ipv6_addr_v4mapped(&tb2->v6_rcv_saddr)) 157*06a8c04fSKuniyuki Iwashima return false; 15828044fc1SJoanne Koong #endif 15928044fc1SJoanne Koong return tb2->rcv_saddr == sk->sk_rcv_saddr; 16028044fc1SJoanne Koong } 16128044fc1SJoanne Koong 16228044fc1SJoanne Koong void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb, 16328044fc1SJoanne Koong struct inet_bind2_bucket *tb2, unsigned short port) 16428044fc1SJoanne Koong { 16528044fc1SJoanne Koong inet_sk(sk)->inet_num = port; 1662d8c4ce5SArnaldo Carvalho de Melo sk_add_bind_node(sk, &tb->owners); 167463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_bind_hash = tb; 16828044fc1SJoanne Koong sk_add_bind2_node(sk, &tb2->owners); 16928044fc1SJoanne Koong inet_csk(sk)->icsk_bind2_hash = tb2; 1702d8c4ce5SArnaldo Carvalho de Melo } 1712d8c4ce5SArnaldo Carvalho de Melo 1722d8c4ce5SArnaldo Carvalho de Melo /* 1732d8c4ce5SArnaldo Carvalho de Melo * Get rid of any references to a local port held by the given sock. 1742d8c4ce5SArnaldo Carvalho de Melo */ 175ab1e0a13SArnaldo Carvalho de Melo static void __inet_put_port(struct sock *sk) 1762d8c4ce5SArnaldo Carvalho de Melo { 177429e42c1SKuniyuki Iwashima struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk); 17808eaef90SKuniyuki Iwashima struct inet_bind_hashbucket *head, *head2; 17908eaef90SKuniyuki Iwashima struct net *net = sock_net(sk); 1802d8c4ce5SArnaldo Carvalho de Melo struct inet_bind_bucket *tb; 18108eaef90SKuniyuki Iwashima int bhash; 18208eaef90SKuniyuki Iwashima 18308eaef90SKuniyuki Iwashima bhash = inet_bhashfn(net, inet_sk(sk)->inet_num, hashinfo->bhash_size); 18408eaef90SKuniyuki Iwashima head = &hashinfo->bhash[bhash]; 18508eaef90SKuniyuki Iwashima head2 = inet_bhashfn_portaddr(hashinfo, sk, net, inet_sk(sk)->inet_num); 1862d8c4ce5SArnaldo Carvalho de Melo 1872d8c4ce5SArnaldo Carvalho de Melo spin_lock(&head->lock); 188463c84b9SArnaldo Carvalho de Melo tb = inet_csk(sk)->icsk_bind_hash; 1892d8c4ce5SArnaldo Carvalho de Melo __sk_del_bind_node(sk); 190463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_bind_hash = NULL; 191c720c7e8SEric Dumazet inet_sk(sk)->inet_num = 0; 1922d8c4ce5SArnaldo Carvalho de Melo inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); 19328044fc1SJoanne Koong 19428044fc1SJoanne Koong spin_lock(&head2->lock); 19528044fc1SJoanne Koong if (inet_csk(sk)->icsk_bind2_hash) { 19628044fc1SJoanne Koong struct inet_bind2_bucket *tb2 = inet_csk(sk)->icsk_bind2_hash; 19728044fc1SJoanne Koong 19828044fc1SJoanne Koong __sk_del_bind2_node(sk); 19928044fc1SJoanne Koong inet_csk(sk)->icsk_bind2_hash = NULL; 20028044fc1SJoanne Koong inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2); 20128044fc1SJoanne Koong } 20228044fc1SJoanne Koong spin_unlock(&head2->lock); 20328044fc1SJoanne Koong 2042d8c4ce5SArnaldo Carvalho de Melo spin_unlock(&head->lock); 2052d8c4ce5SArnaldo Carvalho de Melo } 2062d8c4ce5SArnaldo Carvalho de Melo 207ab1e0a13SArnaldo Carvalho de Melo void inet_put_port(struct sock *sk) 2082d8c4ce5SArnaldo Carvalho de Melo { 2092d8c4ce5SArnaldo Carvalho de Melo local_bh_disable(); 210ab1e0a13SArnaldo Carvalho de Melo __inet_put_port(sk); 2112d8c4ce5SArnaldo Carvalho de Melo local_bh_enable(); 2122d8c4ce5SArnaldo Carvalho de Melo } 2132d8c4ce5SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_put_port); 214f3f05f70SArnaldo Carvalho de Melo 2151ce31c9eSEric Dumazet int __inet_inherit_port(const struct sock *sk, struct sock *child) 21653083773SPavel Emelyanov { 217429e42c1SKuniyuki Iwashima struct inet_hashinfo *table = tcp_or_dccp_get_hashinfo(sk); 218093d2823SBalazs Scheidler unsigned short port = inet_sk(child)->inet_num; 21908eaef90SKuniyuki Iwashima struct inet_bind_hashbucket *head, *head2; 22028044fc1SJoanne Koong bool created_inet_bind_bucket = false; 22128044fc1SJoanne Koong struct net *net = sock_net(sk); 22208eaef90SKuniyuki Iwashima bool update_fastreuse = false; 22328044fc1SJoanne Koong struct inet_bind2_bucket *tb2; 22453083773SPavel Emelyanov struct inet_bind_bucket *tb; 22508eaef90SKuniyuki Iwashima int bhash, l3mdev; 22608eaef90SKuniyuki Iwashima 22708eaef90SKuniyuki Iwashima bhash = inet_bhashfn(net, port, table->bhash_size); 22808eaef90SKuniyuki Iwashima head = &table->bhash[bhash]; 22908eaef90SKuniyuki Iwashima head2 = inet_bhashfn_portaddr(table, child, net, port); 23053083773SPavel Emelyanov 23153083773SPavel Emelyanov spin_lock(&head->lock); 23228044fc1SJoanne Koong spin_lock(&head2->lock); 23353083773SPavel Emelyanov tb = inet_csk(sk)->icsk_bind_hash; 23428044fc1SJoanne Koong tb2 = inet_csk(sk)->icsk_bind2_hash; 23528044fc1SJoanne Koong if (unlikely(!tb || !tb2)) { 23628044fc1SJoanne Koong spin_unlock(&head2->lock); 237c2f34a65SEric Dumazet spin_unlock(&head->lock); 238c2f34a65SEric Dumazet return -ENOENT; 239c2f34a65SEric Dumazet } 240093d2823SBalazs Scheidler if (tb->port != port) { 2413c82a21fSRobert Shearman l3mdev = inet_sk_bound_l3mdev(sk); 2423c82a21fSRobert Shearman 243093d2823SBalazs Scheidler /* NOTE: using tproxy and redirecting skbs to a proxy 244093d2823SBalazs Scheidler * on a different listener port breaks the assumption 245093d2823SBalazs Scheidler * that the listener socket's icsk_bind_hash is the same 246093d2823SBalazs Scheidler * as that of the child socket. We have to look up or 247093d2823SBalazs Scheidler * create a new bind bucket for the child here. */ 248b67bfe0dSSasha Levin inet_bind_bucket_for_each(tb, &head->chain) { 24928044fc1SJoanne Koong if (inet_bind_bucket_match(tb, net, port, l3mdev)) 250093d2823SBalazs Scheidler break; 251093d2823SBalazs Scheidler } 252b67bfe0dSSasha Levin if (!tb) { 253093d2823SBalazs Scheidler tb = inet_bind_bucket_create(table->bind_bucket_cachep, 25428044fc1SJoanne Koong net, head, port, l3mdev); 255093d2823SBalazs Scheidler if (!tb) { 25628044fc1SJoanne Koong spin_unlock(&head2->lock); 257093d2823SBalazs Scheidler spin_unlock(&head->lock); 258093d2823SBalazs Scheidler return -ENOMEM; 259093d2823SBalazs Scheidler } 26028044fc1SJoanne Koong created_inet_bind_bucket = true; 261093d2823SBalazs Scheidler } 26228044fc1SJoanne Koong update_fastreuse = true; 26328044fc1SJoanne Koong 26428044fc1SJoanne Koong goto bhash2_find; 26528044fc1SJoanne Koong } else if (!inet_bind2_bucket_addr_match(tb2, child)) { 26628044fc1SJoanne Koong l3mdev = inet_sk_bound_l3mdev(sk); 26728044fc1SJoanne Koong 26828044fc1SJoanne Koong bhash2_find: 26928044fc1SJoanne Koong tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, child); 27028044fc1SJoanne Koong if (!tb2) { 27128044fc1SJoanne Koong tb2 = inet_bind2_bucket_create(table->bind2_bucket_cachep, 27228044fc1SJoanne Koong net, head2, port, 27328044fc1SJoanne Koong l3mdev, child); 27428044fc1SJoanne Koong if (!tb2) 27528044fc1SJoanne Koong goto error; 27628044fc1SJoanne Koong } 27728044fc1SJoanne Koong } 27828044fc1SJoanne Koong if (update_fastreuse) 279d76f3351STim Froidcoeur inet_csk_update_fastreuse(tb, child); 28028044fc1SJoanne Koong inet_bind_hash(child, tb, tb2, port); 28128044fc1SJoanne Koong spin_unlock(&head2->lock); 28253083773SPavel Emelyanov spin_unlock(&head->lock); 283093d2823SBalazs Scheidler 284093d2823SBalazs Scheidler return 0; 28528044fc1SJoanne Koong 28628044fc1SJoanne Koong error: 28728044fc1SJoanne Koong if (created_inet_bind_bucket) 28828044fc1SJoanne Koong inet_bind_bucket_destroy(table->bind_bucket_cachep, tb); 28928044fc1SJoanne Koong spin_unlock(&head2->lock); 29028044fc1SJoanne Koong spin_unlock(&head->lock); 29128044fc1SJoanne Koong return -ENOMEM; 29253083773SPavel Emelyanov } 29353083773SPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_inherit_port); 29453083773SPavel Emelyanov 29561b7c691SMartin KaFai Lau static struct inet_listen_hashbucket * 29661b7c691SMartin KaFai Lau inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk) 29761b7c691SMartin KaFai Lau { 29861b7c691SMartin KaFai Lau u32 hash; 29961b7c691SMartin KaFai Lau 30061b7c691SMartin KaFai Lau #if IS_ENABLED(CONFIG_IPV6) 30161b7c691SMartin KaFai Lau if (sk->sk_family == AF_INET6) 30261b7c691SMartin KaFai Lau hash = ipv6_portaddr_hash(sock_net(sk), 30361b7c691SMartin KaFai Lau &sk->sk_v6_rcv_saddr, 30461b7c691SMartin KaFai Lau inet_sk(sk)->inet_num); 30561b7c691SMartin KaFai Lau else 30661b7c691SMartin KaFai Lau #endif 30761b7c691SMartin KaFai Lau hash = ipv4_portaddr_hash(sock_net(sk), 30861b7c691SMartin KaFai Lau inet_sk(sk)->inet_rcv_saddr, 30961b7c691SMartin KaFai Lau inet_sk(sk)->inet_num); 31061b7c691SMartin KaFai Lau return inet_lhash2_bucket(h, hash); 31161b7c691SMartin KaFai Lau } 31261b7c691SMartin KaFai Lau 313c25eb3bfSEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, 314c25eb3bfSEric Dumazet const unsigned short hnum, const __be32 daddr, 31534e1ec31SMiaohe Lin const int dif, const int sdif) 316c25eb3bfSEric Dumazet { 317c25eb3bfSEric Dumazet int score = -1; 318c25eb3bfSEric Dumazet 319d9fbc7f6SPeter Oskolkov if (net_eq(sock_net(sk), net) && sk->sk_num == hnum && 320c25eb3bfSEric Dumazet !ipv6_only_sock(sk)) { 321d9fbc7f6SPeter Oskolkov if (sk->sk_rcv_saddr != daddr) 322c25eb3bfSEric Dumazet return -1; 323e7819058SMike Manning 324d9fbc7f6SPeter Oskolkov if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif)) 325d9fbc7f6SPeter Oskolkov return -1; 3268d6c414cSMike Manning score = sk->sk_bound_dev_if ? 2 : 1; 327d9fbc7f6SPeter Oskolkov 3288d6c414cSMike Manning if (sk->sk_family == PF_INET) 3298d6c414cSMike Manning score++; 3307170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 33170da268bSEric Dumazet score++; 332c25eb3bfSEric Dumazet } 333c25eb3bfSEric Dumazet return score; 334c25eb3bfSEric Dumazet } 335c25eb3bfSEric Dumazet 3362a617763SLorenz Bauer /** 3372a617763SLorenz Bauer * inet_lookup_reuseport() - execute reuseport logic on AF_INET socket if necessary. 3382a617763SLorenz Bauer * @net: network namespace. 3392a617763SLorenz Bauer * @sk: AF_INET socket, must be in TCP_LISTEN state for TCP or TCP_CLOSE for UDP. 3402a617763SLorenz Bauer * @skb: context for a potential SK_REUSEPORT program. 3412a617763SLorenz Bauer * @doff: header offset. 3422a617763SLorenz Bauer * @saddr: source address. 3432a617763SLorenz Bauer * @sport: source port. 3442a617763SLorenz Bauer * @daddr: destination address. 3452a617763SLorenz Bauer * @hnum: destination port in host byte order. 3462a617763SLorenz Bauer * @ehashfn: hash function used to generate the fallback hash. 3472a617763SLorenz Bauer * 3482a617763SLorenz Bauer * Return: NULL if sk doesn't have SO_REUSEPORT set, otherwise a pointer to 3492a617763SLorenz Bauer * the selected sock or an error. 3502a617763SLorenz Bauer */ 351ce796e60SLorenz Bauer struct sock *inet_lookup_reuseport(struct net *net, struct sock *sk, 35280b373f7SJakub Sitnicki struct sk_buff *skb, int doff, 35380b373f7SJakub Sitnicki __be32 saddr, __be16 sport, 3540f495f76SLorenz Bauer __be32 daddr, unsigned short hnum, 3550f495f76SLorenz Bauer inet_ehashfn_t *ehashfn) 35680b373f7SJakub Sitnicki { 35780b373f7SJakub Sitnicki struct sock *reuse_sk = NULL; 35880b373f7SJakub Sitnicki u32 phash; 35980b373f7SJakub Sitnicki 36080b373f7SJakub Sitnicki if (sk->sk_reuseport) { 3610f495f76SLorenz Bauer phash = INDIRECT_CALL_2(ehashfn, udp_ehashfn, inet_ehashfn, 3620f495f76SLorenz Bauer net, daddr, hnum, saddr, sport); 36380b373f7SJakub Sitnicki reuse_sk = reuseport_select_sock(sk, phash, skb, doff); 36480b373f7SJakub Sitnicki } 36580b373f7SJakub Sitnicki return reuse_sk; 36680b373f7SJakub Sitnicki } 367ce796e60SLorenz Bauer EXPORT_SYMBOL_GPL(inet_lookup_reuseport); 36880b373f7SJakub Sitnicki 369f3f05f70SArnaldo Carvalho de Melo /* 3703b24d854SEric Dumazet * Here are some nice properties to exploit here. The BSD API 3713b24d854SEric Dumazet * does not allow a listening sock to specify the remote port nor the 37233b62231SArnaldo Carvalho de Melo * remote address for the connection. So always assume those are both 37333b62231SArnaldo Carvalho de Melo * wildcarded during the search since they can never be otherwise. 37433b62231SArnaldo Carvalho de Melo */ 37533b62231SArnaldo Carvalho de Melo 3763b24d854SEric Dumazet /* called with rcu_read_lock() : No refcount taken on the socket */ 37761b7c691SMartin KaFai Lau static struct sock *inet_lhash2_lookup(struct net *net, 37861b7c691SMartin KaFai Lau struct inet_listen_hashbucket *ilb2, 37961b7c691SMartin KaFai Lau struct sk_buff *skb, int doff, 38061b7c691SMartin KaFai Lau const __be32 saddr, __be16 sport, 38161b7c691SMartin KaFai Lau const __be32 daddr, const unsigned short hnum, 38261b7c691SMartin KaFai Lau const int dif, const int sdif) 38361b7c691SMartin KaFai Lau { 38461b7c691SMartin KaFai Lau struct sock *sk, *result = NULL; 385cae3873cSMartin KaFai Lau struct hlist_nulls_node *node; 38661b7c691SMartin KaFai Lau int score, hiscore = 0; 38761b7c691SMartin KaFai Lau 388cae3873cSMartin KaFai Lau sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) { 38934e1ec31SMiaohe Lin score = compute_score(sk, net, hnum, daddr, dif, sdif); 39061b7c691SMartin KaFai Lau if (score > hiscore) { 391ce796e60SLorenz Bauer result = inet_lookup_reuseport(net, sk, skb, doff, 3920f495f76SLorenz Bauer saddr, sport, daddr, hnum, inet_ehashfn); 39361b7c691SMartin KaFai Lau if (result) 39461b7c691SMartin KaFai Lau return result; 39580b373f7SJakub Sitnicki 39661b7c691SMartin KaFai Lau result = sk; 39761b7c691SMartin KaFai Lau hiscore = score; 39861b7c691SMartin KaFai Lau } 39961b7c691SMartin KaFai Lau } 40061b7c691SMartin KaFai Lau 40161b7c691SMartin KaFai Lau return result; 40261b7c691SMartin KaFai Lau } 40361b7c691SMartin KaFai Lau 4046c886db2SLorenz Bauer struct sock *inet_lookup_run_sk_lookup(struct net *net, 4056c886db2SLorenz Bauer int protocol, 4061559b4aaSJakub Sitnicki struct sk_buff *skb, int doff, 4071559b4aaSJakub Sitnicki __be32 saddr, __be16 sport, 4086c886db2SLorenz Bauer __be32 daddr, u16 hnum, const int dif, 4096c886db2SLorenz Bauer inet_ehashfn_t *ehashfn) 4101559b4aaSJakub Sitnicki { 4111559b4aaSJakub Sitnicki struct sock *sk, *reuse_sk; 4121559b4aaSJakub Sitnicki bool no_reuseport; 4131559b4aaSJakub Sitnicki 4146c886db2SLorenz Bauer no_reuseport = bpf_sk_lookup_run_v4(net, protocol, saddr, sport, 415f8931565SMark Pashmfouroush daddr, hnum, dif, &sk); 4161559b4aaSJakub Sitnicki if (no_reuseport || IS_ERR_OR_NULL(sk)) 4171559b4aaSJakub Sitnicki return sk; 4181559b4aaSJakub Sitnicki 4190f495f76SLorenz Bauer reuse_sk = inet_lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum, 4206c886db2SLorenz Bauer ehashfn); 4211559b4aaSJakub Sitnicki if (reuse_sk) 4221559b4aaSJakub Sitnicki sk = reuse_sk; 4231559b4aaSJakub Sitnicki return sk; 4241559b4aaSJakub Sitnicki } 4251559b4aaSJakub Sitnicki 426c67499c0SPavel Emelyanov struct sock *__inet_lookup_listener(struct net *net, 427c67499c0SPavel Emelyanov struct inet_hashinfo *hashinfo, 428a583636aSCraig Gallek struct sk_buff *skb, int doff, 429da5e3630STom Herbert const __be32 saddr, __be16 sport, 430fb99c848SAl Viro const __be32 daddr, const unsigned short hnum, 4313fa6f616SDavid Ahern const int dif, const int sdif) 43299a92ff5SHerbert Xu { 43361b7c691SMartin KaFai Lau struct inet_listen_hashbucket *ilb2; 434d9fbc7f6SPeter Oskolkov struct sock *result = NULL; 43561b7c691SMartin KaFai Lau unsigned int hash2; 43661b7c691SMartin KaFai Lau 4371559b4aaSJakub Sitnicki /* Lookup redirect from BPF */ 4386c886db2SLorenz Bauer if (static_branch_unlikely(&bpf_sk_lookup_enabled) && 4396c886db2SLorenz Bauer hashinfo == net->ipv4.tcp_death_row.hashinfo) { 4406c886db2SLorenz Bauer result = inet_lookup_run_sk_lookup(net, IPPROTO_TCP, skb, doff, 4416c886db2SLorenz Bauer saddr, sport, daddr, hnum, dif, 4426c886db2SLorenz Bauer inet_ehashfn); 4431559b4aaSJakub Sitnicki if (result) 4441559b4aaSJakub Sitnicki goto done; 4451559b4aaSJakub Sitnicki } 4461559b4aaSJakub Sitnicki 44761b7c691SMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, daddr, hnum); 44861b7c691SMartin KaFai Lau ilb2 = inet_lhash2_bucket(hashinfo, hash2); 44961b7c691SMartin KaFai Lau 45061b7c691SMartin KaFai Lau result = inet_lhash2_lookup(net, ilb2, skb, doff, 45161b7c691SMartin KaFai Lau saddr, sport, daddr, hnum, 45261b7c691SMartin KaFai Lau dif, sdif); 45361b7c691SMartin KaFai Lau if (result) 4548217ca65SMartin KaFai Lau goto done; 45561b7c691SMartin KaFai Lau 45661b7c691SMartin KaFai Lau /* Lookup lhash2 with INADDR_ANY */ 45761b7c691SMartin KaFai Lau hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum); 45861b7c691SMartin KaFai Lau ilb2 = inet_lhash2_bucket(hashinfo, hash2); 45961b7c691SMartin KaFai Lau 4608217ca65SMartin KaFai Lau result = inet_lhash2_lookup(net, ilb2, skb, doff, 461d9fbc7f6SPeter Oskolkov saddr, sport, htonl(INADDR_ANY), hnum, 46261b7c691SMartin KaFai Lau dif, sdif); 4638217ca65SMartin KaFai Lau done: 46488e235b8SEnrico Weigelt if (IS_ERR(result)) 4658217ca65SMartin KaFai Lau return NULL; 466c25eb3bfSEric Dumazet return result; 46799a92ff5SHerbert Xu } 4688f491069SHerbert Xu EXPORT_SYMBOL_GPL(__inet_lookup_listener); 469a7f5e7f1SArnaldo Carvalho de Melo 47005dbc7b5SEric Dumazet /* All sockets share common refcount, but have different destructors */ 47105dbc7b5SEric Dumazet void sock_gen_put(struct sock *sk) 47205dbc7b5SEric Dumazet { 47341c6d650SReshetova, Elena if (!refcount_dec_and_test(&sk->sk_refcnt)) 47405dbc7b5SEric Dumazet return; 47505dbc7b5SEric Dumazet 47605dbc7b5SEric Dumazet if (sk->sk_state == TCP_TIME_WAIT) 47705dbc7b5SEric Dumazet inet_twsk_free(inet_twsk(sk)); 47841b822c5SEric Dumazet else if (sk->sk_state == TCP_NEW_SYN_RECV) 47941b822c5SEric Dumazet reqsk_free(inet_reqsk(sk)); 48005dbc7b5SEric Dumazet else 48105dbc7b5SEric Dumazet sk_free(sk); 48205dbc7b5SEric Dumazet } 48305dbc7b5SEric Dumazet EXPORT_SYMBOL_GPL(sock_gen_put); 48405dbc7b5SEric Dumazet 4852c13270bSEric Dumazet void sock_edemux(struct sk_buff *skb) 4862c13270bSEric Dumazet { 4872c13270bSEric Dumazet sock_gen_put(skb->sk); 4882c13270bSEric Dumazet } 4892c13270bSEric Dumazet EXPORT_SYMBOL(sock_edemux); 4902c13270bSEric Dumazet 491c67499c0SPavel Emelyanov struct sock *__inet_lookup_established(struct net *net, 492c67499c0SPavel Emelyanov struct inet_hashinfo *hashinfo, 49377a5ba55SPavel Emelyanov const __be32 saddr, const __be16 sport, 49477a5ba55SPavel Emelyanov const __be32 daddr, const u16 hnum, 4953fa6f616SDavid Ahern const int dif, const int sdif) 49677a5ba55SPavel Emelyanov { 497c7228317SJoe Perches INET_ADDR_COOKIE(acookie, saddr, daddr); 49877a5ba55SPavel Emelyanov const __portpair ports = INET_COMBINED_PORTS(sport, hnum); 49977a5ba55SPavel Emelyanov struct sock *sk; 5003ab5aee7SEric Dumazet const struct hlist_nulls_node *node; 50177a5ba55SPavel Emelyanov /* Optimize here for direct hit, only listening connections can 50277a5ba55SPavel Emelyanov * have wildcards anyways. 50377a5ba55SPavel Emelyanov */ 5049f26b3adSPavel Emelyanov unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport); 505f373b53bSEric Dumazet unsigned int slot = hash & hashinfo->ehash_mask; 5063ab5aee7SEric Dumazet struct inet_ehash_bucket *head = &hashinfo->ehash[slot]; 50777a5ba55SPavel Emelyanov 5083ab5aee7SEric Dumazet begin: 5093ab5aee7SEric Dumazet sk_nulls_for_each_rcu(sk, node, &head->chain) { 510ce43b03eSEric Dumazet if (sk->sk_hash != hash) 511ce43b03eSEric Dumazet continue; 512eda090c3SEric Dumazet if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) { 51341c6d650SReshetova, Elena if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt))) 51405dbc7b5SEric Dumazet goto out; 515eda090c3SEric Dumazet if (unlikely(!inet_match(net, sk, acookie, 5164915d50eSEric Dumazet ports, dif, sdif))) { 51705dbc7b5SEric Dumazet sock_gen_put(sk); 5183ab5aee7SEric Dumazet goto begin; 51977a5ba55SPavel Emelyanov } 52005dbc7b5SEric Dumazet goto found; 5213ab5aee7SEric Dumazet } 5223ab5aee7SEric Dumazet } 5233ab5aee7SEric Dumazet /* 5243ab5aee7SEric Dumazet * if the nulls value we got at the end of this lookup is 5253ab5aee7SEric Dumazet * not the expected one, we must restart lookup. 5263ab5aee7SEric Dumazet * We probably met an item that was moved to another chain. 5273ab5aee7SEric Dumazet */ 5283ab5aee7SEric Dumazet if (get_nulls_value(node) != slot) 5293ab5aee7SEric Dumazet goto begin; 53077a5ba55SPavel Emelyanov out: 53105dbc7b5SEric Dumazet sk = NULL; 53205dbc7b5SEric Dumazet found: 53377a5ba55SPavel Emelyanov return sk; 53477a5ba55SPavel Emelyanov } 53577a5ba55SPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_lookup_established); 53677a5ba55SPavel Emelyanov 537a7f5e7f1SArnaldo Carvalho de Melo /* called with local bh disabled */ 538a7f5e7f1SArnaldo Carvalho de Melo static int __inet_check_established(struct inet_timewait_death_row *death_row, 539a7f5e7f1SArnaldo Carvalho de Melo struct sock *sk, __u16 lport, 540a7f5e7f1SArnaldo Carvalho de Melo struct inet_timewait_sock **twp) 541a7f5e7f1SArnaldo Carvalho de Melo { 542a7f5e7f1SArnaldo Carvalho de Melo struct inet_hashinfo *hinfo = death_row->hashinfo; 543a7f5e7f1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 544c720c7e8SEric Dumazet __be32 daddr = inet->inet_rcv_saddr; 545c720c7e8SEric Dumazet __be32 saddr = inet->inet_daddr; 546a7f5e7f1SArnaldo Carvalho de Melo int dif = sk->sk_bound_dev_if; 5473fa6f616SDavid Ahern struct net *net = sock_net(sk); 5483fa6f616SDavid Ahern int sdif = l3mdev_master_ifindex_by_index(net, dif); 549c7228317SJoe Perches INET_ADDR_COOKIE(acookie, saddr, daddr); 550c720c7e8SEric Dumazet const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport); 551c720c7e8SEric Dumazet unsigned int hash = inet_ehashfn(net, daddr, lport, 552c720c7e8SEric Dumazet saddr, inet->inet_dport); 553a7f5e7f1SArnaldo Carvalho de Melo struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash); 5549db66bdcSEric Dumazet spinlock_t *lock = inet_ehash_lockp(hinfo, hash); 555a7f5e7f1SArnaldo Carvalho de Melo struct sock *sk2; 5563ab5aee7SEric Dumazet const struct hlist_nulls_node *node; 55705dbc7b5SEric Dumazet struct inet_timewait_sock *tw = NULL; 558a7f5e7f1SArnaldo Carvalho de Melo 5599db66bdcSEric Dumazet spin_lock(lock); 560a7f5e7f1SArnaldo Carvalho de Melo 5613ab5aee7SEric Dumazet sk_nulls_for_each(sk2, node, &head->chain) { 562ce43b03eSEric Dumazet if (sk2->sk_hash != hash) 563ce43b03eSEric Dumazet continue; 56405dbc7b5SEric Dumazet 565eda090c3SEric Dumazet if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) { 56605dbc7b5SEric Dumazet if (sk2->sk_state == TCP_TIME_WAIT) { 56705dbc7b5SEric Dumazet tw = inet_twsk(sk2); 56805dbc7b5SEric Dumazet if (twsk_unique(sk, sk2, twp)) 56905dbc7b5SEric Dumazet break; 57005dbc7b5SEric Dumazet } 571a7f5e7f1SArnaldo Carvalho de Melo goto not_unique; 572a7f5e7f1SArnaldo Carvalho de Melo } 57305dbc7b5SEric Dumazet } 574a7f5e7f1SArnaldo Carvalho de Melo 575a7f5e7f1SArnaldo Carvalho de Melo /* Must record num and sport now. Otherwise we will see 57605dbc7b5SEric Dumazet * in hash table socket with a funny identity. 57705dbc7b5SEric Dumazet */ 578c720c7e8SEric Dumazet inet->inet_num = lport; 579c720c7e8SEric Dumazet inet->inet_sport = htons(lport); 580a7f5e7f1SArnaldo Carvalho de Melo sk->sk_hash = hash; 581547b792cSIlpo Järvinen WARN_ON(!sk_unhashed(sk)); 5823ab5aee7SEric Dumazet __sk_nulls_add_node_rcu(sk, &head->chain); 58313475a30SEric Dumazet if (tw) { 584fc01538fSEric Dumazet sk_nulls_del_node_init_rcu((struct sock *)tw); 58502a1d6e7SEric Dumazet __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED); 58613475a30SEric Dumazet } 5879db66bdcSEric Dumazet spin_unlock(lock); 588c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 589a7f5e7f1SArnaldo Carvalho de Melo 590a7f5e7f1SArnaldo Carvalho de Melo if (twp) { 591a7f5e7f1SArnaldo Carvalho de Melo *twp = tw; 592a7f5e7f1SArnaldo Carvalho de Melo } else if (tw) { 593a7f5e7f1SArnaldo Carvalho de Melo /* Silly. Should hash-dance instead... */ 594dbe7faa4SEric Dumazet inet_twsk_deschedule_put(tw); 595a7f5e7f1SArnaldo Carvalho de Melo } 596a7f5e7f1SArnaldo Carvalho de Melo return 0; 597a7f5e7f1SArnaldo Carvalho de Melo 598a7f5e7f1SArnaldo Carvalho de Melo not_unique: 5999db66bdcSEric Dumazet spin_unlock(lock); 600a7f5e7f1SArnaldo Carvalho de Melo return -EADDRNOTAVAIL; 601a7f5e7f1SArnaldo Carvalho de Melo } 602a7f5e7f1SArnaldo Carvalho de Melo 603b2d05756SWilly Tarreau static u64 inet_sk_port_offset(const struct sock *sk) 604a7f5e7f1SArnaldo Carvalho de Melo { 605a7f5e7f1SArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 606e2baad9eSEric Dumazet 607c720c7e8SEric Dumazet return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr, 608c720c7e8SEric Dumazet inet->inet_daddr, 609c720c7e8SEric Dumazet inet->inet_dport); 610a7f5e7f1SArnaldo Carvalho de Melo } 611a7f5e7f1SArnaldo Carvalho de Melo 61201770a16SRicardo Dias /* Searches for an exsiting socket in the ehash bucket list. 61301770a16SRicardo Dias * Returns true if found, false otherwise. 614079096f1SEric Dumazet */ 61501770a16SRicardo Dias static bool inet_ehash_lookup_by_sk(struct sock *sk, 61601770a16SRicardo Dias struct hlist_nulls_head *list) 61701770a16SRicardo Dias { 61801770a16SRicardo Dias const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num); 61901770a16SRicardo Dias const int sdif = sk->sk_bound_dev_if; 62001770a16SRicardo Dias const int dif = sk->sk_bound_dev_if; 62101770a16SRicardo Dias const struct hlist_nulls_node *node; 62201770a16SRicardo Dias struct net *net = sock_net(sk); 62301770a16SRicardo Dias struct sock *esk; 62401770a16SRicardo Dias 62501770a16SRicardo Dias INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr); 62601770a16SRicardo Dias 62701770a16SRicardo Dias sk_nulls_for_each_rcu(esk, node, list) { 62801770a16SRicardo Dias if (esk->sk_hash != sk->sk_hash) 62901770a16SRicardo Dias continue; 63001770a16SRicardo Dias if (sk->sk_family == AF_INET) { 631eda090c3SEric Dumazet if (unlikely(inet_match(net, esk, acookie, 63201770a16SRicardo Dias ports, dif, sdif))) { 63301770a16SRicardo Dias return true; 63401770a16SRicardo Dias } 63501770a16SRicardo Dias } 63601770a16SRicardo Dias #if IS_ENABLED(CONFIG_IPV6) 63701770a16SRicardo Dias else if (sk->sk_family == AF_INET6) { 6385d368f03SEric Dumazet if (unlikely(inet6_match(net, esk, 63901770a16SRicardo Dias &sk->sk_v6_daddr, 64001770a16SRicardo Dias &sk->sk_v6_rcv_saddr, 64101770a16SRicardo Dias ports, dif, sdif))) { 64201770a16SRicardo Dias return true; 64301770a16SRicardo Dias } 64401770a16SRicardo Dias } 64501770a16SRicardo Dias #endif 64601770a16SRicardo Dias } 64701770a16SRicardo Dias return false; 64801770a16SRicardo Dias } 64901770a16SRicardo Dias 65001770a16SRicardo Dias /* Insert a socket into ehash, and eventually remove another one 65101770a16SRicardo Dias * (The another one can be a SYN_RECV or TIMEWAIT) 65201770a16SRicardo Dias * If an existing socket already exists, socket sk is not inserted, 65301770a16SRicardo Dias * and sets found_dup_sk parameter to true. 65401770a16SRicardo Dias */ 65501770a16SRicardo Dias bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk) 656152da81dSPavel Emelyanov { 657429e42c1SKuniyuki Iwashima struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk); 658152da81dSPavel Emelyanov struct inet_ehash_bucket *head; 65908eaef90SKuniyuki Iwashima struct hlist_nulls_head *list; 6605b441f76SEric Dumazet spinlock_t *lock; 6615e0724d0SEric Dumazet bool ret = true; 662152da81dSPavel Emelyanov 663079096f1SEric Dumazet WARN_ON_ONCE(!sk_unhashed(sk)); 664152da81dSPavel Emelyanov 6655b441f76SEric Dumazet sk->sk_hash = sk_ehashfn(sk); 666152da81dSPavel Emelyanov head = inet_ehash_bucket(hashinfo, sk->sk_hash); 667152da81dSPavel Emelyanov list = &head->chain; 668152da81dSPavel Emelyanov lock = inet_ehash_lockp(hashinfo, sk->sk_hash); 669152da81dSPavel Emelyanov 6709db66bdcSEric Dumazet spin_lock(lock); 671fc01538fSEric Dumazet if (osk) { 6725e0724d0SEric Dumazet WARN_ON_ONCE(sk->sk_hash != osk->sk_hash); 67381b3ade5SKuniyuki Iwashima ret = sk_nulls_del_node_init_rcu(osk); 67481b3ade5SKuniyuki Iwashima } else if (found_dup_sk) { 67501770a16SRicardo Dias *found_dup_sk = inet_ehash_lookup_by_sk(sk, list); 67601770a16SRicardo Dias if (*found_dup_sk) 67701770a16SRicardo Dias ret = false; 6789327f705SEric Dumazet } 67901770a16SRicardo Dias 6805e0724d0SEric Dumazet if (ret) 6815e0724d0SEric Dumazet __sk_nulls_add_node_rcu(sk, list); 68201770a16SRicardo Dias 6839db66bdcSEric Dumazet spin_unlock(lock); 68401770a16SRicardo Dias 685079096f1SEric Dumazet return ret; 686079096f1SEric Dumazet } 687079096f1SEric Dumazet 68801770a16SRicardo Dias bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk) 689079096f1SEric Dumazet { 69001770a16SRicardo Dias bool ok = inet_ehash_insert(sk, osk, found_dup_sk); 6915e0724d0SEric Dumazet 6925e0724d0SEric Dumazet if (ok) { 693c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 6945e0724d0SEric Dumazet } else { 69519757cebSEric Dumazet this_cpu_inc(*sk->sk_prot->orphan_count); 696563e0bb0SYafang Shao inet_sk_set_state(sk, TCP_CLOSE); 6975e0724d0SEric Dumazet sock_set_flag(sk, SOCK_DEAD); 6985e0724d0SEric Dumazet inet_csk_destroy_sock(sk); 699152da81dSPavel Emelyanov } 7005e0724d0SEric Dumazet return ok; 7015e0724d0SEric Dumazet } 7025e0724d0SEric Dumazet EXPORT_SYMBOL_GPL(inet_ehash_nolisten); 703152da81dSPavel Emelyanov 704c125e80bSCraig Gallek static int inet_reuseport_add_sock(struct sock *sk, 705fe38d2a1SJosef Bacik struct inet_listen_hashbucket *ilb) 706c125e80bSCraig Gallek { 70790e5d0dbSCraig Gallek struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash; 7088dbd76e7SEric Dumazet const struct hlist_nulls_node *node; 709c125e80bSCraig Gallek struct sock *sk2; 710c125e80bSCraig Gallek kuid_t uid = sock_i_uid(sk); 711c125e80bSCraig Gallek 7128dbd76e7SEric Dumazet sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) { 713c125e80bSCraig Gallek if (sk2 != sk && 714c125e80bSCraig Gallek sk2->sk_family == sk->sk_family && 715c125e80bSCraig Gallek ipv6_only_sock(sk2) == ipv6_only_sock(sk) && 716c125e80bSCraig Gallek sk2->sk_bound_dev_if == sk->sk_bound_dev_if && 71790e5d0dbSCraig Gallek inet_csk(sk2)->icsk_bind_hash == tb && 718c125e80bSCraig Gallek sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && 719fe38d2a1SJosef Bacik inet_rcv_saddr_equal(sk, sk2, false)) 7202dbb9b9eSMartin KaFai Lau return reuseport_add_sock(sk, sk2, 7212dbb9b9eSMartin KaFai Lau inet_rcv_saddr_any(sk)); 722c125e80bSCraig Gallek } 723c125e80bSCraig Gallek 7242dbb9b9eSMartin KaFai Lau return reuseport_alloc(sk, inet_rcv_saddr_any(sk)); 725c125e80bSCraig Gallek } 726c125e80bSCraig Gallek 727fe38d2a1SJosef Bacik int __inet_hash(struct sock *sk, struct sock *osk) 728152da81dSPavel Emelyanov { 729429e42c1SKuniyuki Iwashima struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk); 730e8d00590SMartin KaFai Lau struct inet_listen_hashbucket *ilb2; 731c125e80bSCraig Gallek int err = 0; 732152da81dSPavel Emelyanov 7335e0724d0SEric Dumazet if (sk->sk_state != TCP_LISTEN) { 7344f9bf2a2SSebastian Andrzej Siewior local_bh_disable(); 73501770a16SRicardo Dias inet_ehash_nolisten(sk, osk, NULL); 7364f9bf2a2SSebastian Andrzej Siewior local_bh_enable(); 737c125e80bSCraig Gallek return 0; 7385e0724d0SEric Dumazet } 739547b792cSIlpo Järvinen WARN_ON(!sk_unhashed(sk)); 740e8d00590SMartin KaFai Lau ilb2 = inet_lhash2_bucket_sk(hashinfo, sk); 741152da81dSPavel Emelyanov 742e8d00590SMartin KaFai Lau spin_lock(&ilb2->lock); 743c125e80bSCraig Gallek if (sk->sk_reuseport) { 744cae3873cSMartin KaFai Lau err = inet_reuseport_add_sock(sk, ilb2); 745c125e80bSCraig Gallek if (err) 746c125e80bSCraig Gallek goto unlock; 747c125e80bSCraig Gallek } 748871019b2SStanislav Fomichev sock_set_flag(sk, SOCK_RCU_FREE); 749d296ba60SCraig Gallek if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport && 750cae3873cSMartin KaFai Lau sk->sk_family == AF_INET6) 751cae3873cSMartin KaFai Lau __sk_nulls_add_node_tail_rcu(sk, &ilb2->nulls_head); 752cae3873cSMartin KaFai Lau else 753cae3873cSMartin KaFai Lau __sk_nulls_add_node_rcu(sk, &ilb2->nulls_head); 754c29a0bc4SPavel Emelyanov sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 755c125e80bSCraig Gallek unlock: 756e8d00590SMartin KaFai Lau spin_unlock(&ilb2->lock); 757c125e80bSCraig Gallek 758c125e80bSCraig Gallek return err; 759152da81dSPavel Emelyanov } 76077a6a471SEric Dumazet EXPORT_SYMBOL(__inet_hash); 761ab1e0a13SArnaldo Carvalho de Melo 762086c653fSCraig Gallek int inet_hash(struct sock *sk) 763ab1e0a13SArnaldo Carvalho de Melo { 764c125e80bSCraig Gallek int err = 0; 765c125e80bSCraig Gallek 7664f9bf2a2SSebastian Andrzej Siewior if (sk->sk_state != TCP_CLOSE) 767fe38d2a1SJosef Bacik err = __inet_hash(sk, NULL); 768086c653fSCraig Gallek 769c125e80bSCraig Gallek return err; 770ab1e0a13SArnaldo Carvalho de Melo } 771ab1e0a13SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_hash); 772ab1e0a13SArnaldo Carvalho de Melo 7734f9bf2a2SSebastian Andrzej Siewior void inet_unhash(struct sock *sk) 7744f9bf2a2SSebastian Andrzej Siewior { 775429e42c1SKuniyuki Iwashima struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk); 7764f9bf2a2SSebastian Andrzej Siewior 7774f9bf2a2SSebastian Andrzej Siewior if (sk_unhashed(sk)) 7784f9bf2a2SSebastian Andrzej Siewior return; 7794f9bf2a2SSebastian Andrzej Siewior 7804f9bf2a2SSebastian Andrzej Siewior if (sk->sk_state == TCP_LISTEN) { 781e8d00590SMartin KaFai Lau struct inet_listen_hashbucket *ilb2; 7824f9bf2a2SSebastian Andrzej Siewior 783e8d00590SMartin KaFai Lau ilb2 = inet_lhash2_bucket_sk(hashinfo, sk); 7844f9bf2a2SSebastian Andrzej Siewior /* Don't disable bottom halves while acquiring the lock to 7854f9bf2a2SSebastian Andrzej Siewior * avoid circular locking dependency on PREEMPT_RT. 7864f9bf2a2SSebastian Andrzej Siewior */ 787e8d00590SMartin KaFai Lau spin_lock(&ilb2->lock); 788e8d00590SMartin KaFai Lau if (sk_unhashed(sk)) { 789e8d00590SMartin KaFai Lau spin_unlock(&ilb2->lock); 790e8d00590SMartin KaFai Lau return; 791e8d00590SMartin KaFai Lau } 792e8d00590SMartin KaFai Lau 793e8d00590SMartin KaFai Lau if (rcu_access_pointer(sk->sk_reuseport_cb)) 794e8d00590SMartin KaFai Lau reuseport_stop_listen_sock(sk); 795e8d00590SMartin KaFai Lau 796e8d00590SMartin KaFai Lau __sk_nulls_del_node_init_rcu(sk); 797e8d00590SMartin KaFai Lau sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 798e8d00590SMartin KaFai Lau spin_unlock(&ilb2->lock); 7994f9bf2a2SSebastian Andrzej Siewior } else { 8004f9bf2a2SSebastian Andrzej Siewior spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); 8014f9bf2a2SSebastian Andrzej Siewior 8024f9bf2a2SSebastian Andrzej Siewior spin_lock_bh(lock); 803e8d00590SMartin KaFai Lau if (sk_unhashed(sk)) { 804e8d00590SMartin KaFai Lau spin_unlock_bh(lock); 805e8d00590SMartin KaFai Lau return; 806e8d00590SMartin KaFai Lau } 807e8d00590SMartin KaFai Lau __sk_nulls_del_node_init_rcu(sk); 808e8d00590SMartin KaFai Lau sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 809920de804SEric Dumazet spin_unlock_bh(lock); 810ab1e0a13SArnaldo Carvalho de Melo } 8114f9bf2a2SSebastian Andrzej Siewior } 812ab1e0a13SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_unhash); 813152da81dSPavel Emelyanov 81428044fc1SJoanne Koong static bool inet_bind2_bucket_match(const struct inet_bind2_bucket *tb, 81528044fc1SJoanne Koong const struct net *net, unsigned short port, 81628044fc1SJoanne Koong int l3mdev, const struct sock *sk) 81728044fc1SJoanne Koong { 818c6d27706SKuniyuki Iwashima if (!net_eq(ib2_net(tb), net) || tb->port != port || 819c6d27706SKuniyuki Iwashima tb->l3mdev != l3mdev) 820c6d27706SKuniyuki Iwashima return false; 821c6d27706SKuniyuki Iwashima 8228702cf12SKuniyuki Iwashima return inet_bind2_bucket_addr_match(tb, sk); 82328044fc1SJoanne Koong } 82428044fc1SJoanne Koong 82528044fc1SJoanne Koong bool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, const struct net *net, 82628044fc1SJoanne Koong unsigned short port, int l3mdev, const struct sock *sk) 82728044fc1SJoanne Koong { 828c6d27706SKuniyuki Iwashima if (!net_eq(ib2_net(tb), net) || tb->port != port || 829c6d27706SKuniyuki Iwashima tb->l3mdev != l3mdev) 830c6d27706SKuniyuki Iwashima return false; 831c6d27706SKuniyuki Iwashima 83228044fc1SJoanne Koong #if IS_ENABLED(CONFIG_IPV6) 83356f3e3f0SKuniyuki Iwashima if (sk->sk_family == AF_INET6) { 834*06a8c04fSKuniyuki Iwashima if (ipv6_addr_any(&tb->v6_rcv_saddr)) 835*06a8c04fSKuniyuki Iwashima return true; 836*06a8c04fSKuniyuki Iwashima 837*06a8c04fSKuniyuki Iwashima if (!ipv6_addr_v4mapped(&tb->v6_rcv_saddr)) 838*06a8c04fSKuniyuki Iwashima return false; 839d9ba9934SKuniyuki Iwashima 8405e07e672SKuniyuki Iwashima return ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr) && 8415e07e672SKuniyuki Iwashima tb->rcv_saddr == 0; 842d9ba9934SKuniyuki Iwashima } 8435456262dSMartin KaFai Lau 844*06a8c04fSKuniyuki Iwashima if (ipv6_addr_any(&tb->v6_rcv_saddr)) 845*06a8c04fSKuniyuki Iwashima return true; 846*06a8c04fSKuniyuki Iwashima 847*06a8c04fSKuniyuki Iwashima if (!ipv6_addr_v4mapped(&tb->v6_rcv_saddr)) 848*06a8c04fSKuniyuki Iwashima return false; 84928044fc1SJoanne Koong #endif 850c6d27706SKuniyuki Iwashima return tb->rcv_saddr == 0; 85128044fc1SJoanne Koong } 85228044fc1SJoanne Koong 85328044fc1SJoanne Koong /* The socket's bhash2 hashbucket spinlock must be held when this is called */ 85428044fc1SJoanne Koong struct inet_bind2_bucket * 85528044fc1SJoanne Koong inet_bind2_bucket_find(const struct inet_bind_hashbucket *head, const struct net *net, 85628044fc1SJoanne Koong unsigned short port, int l3mdev, const struct sock *sk) 85728044fc1SJoanne Koong { 85828044fc1SJoanne Koong struct inet_bind2_bucket *bhash2 = NULL; 85928044fc1SJoanne Koong 86028044fc1SJoanne Koong inet_bind_bucket_for_each(bhash2, &head->chain) 86128044fc1SJoanne Koong if (inet_bind2_bucket_match(bhash2, net, port, l3mdev, sk)) 86228044fc1SJoanne Koong break; 86328044fc1SJoanne Koong 86428044fc1SJoanne Koong return bhash2; 86528044fc1SJoanne Koong } 86628044fc1SJoanne Koong 86728044fc1SJoanne Koong struct inet_bind_hashbucket * 86828044fc1SJoanne Koong inet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port) 86928044fc1SJoanne Koong { 870429e42c1SKuniyuki Iwashima struct inet_hashinfo *hinfo = tcp_or_dccp_get_hashinfo(sk); 87128044fc1SJoanne Koong u32 hash; 87228044fc1SJoanne Koong 8738cdc3223SKuniyuki Iwashima #if IS_ENABLED(CONFIG_IPV6) 87428044fc1SJoanne Koong if (sk->sk_family == AF_INET6) 8758cdc3223SKuniyuki Iwashima hash = ipv6_portaddr_hash(net, &in6addr_any, port); 87628044fc1SJoanne Koong else 87728044fc1SJoanne Koong #endif 87828044fc1SJoanne Koong hash = ipv4_portaddr_hash(net, 0, port); 87928044fc1SJoanne Koong 88028044fc1SJoanne Koong return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)]; 88128044fc1SJoanne Koong } 88228044fc1SJoanne Koong 8838c5dae4cSKuniyuki Iwashima static void inet_update_saddr(struct sock *sk, void *saddr, int family) 8848c5dae4cSKuniyuki Iwashima { 8858c5dae4cSKuniyuki Iwashima if (family == AF_INET) { 8868c5dae4cSKuniyuki Iwashima inet_sk(sk)->inet_saddr = *(__be32 *)saddr; 8878c5dae4cSKuniyuki Iwashima sk_rcv_saddr_set(sk, inet_sk(sk)->inet_saddr); 8888c5dae4cSKuniyuki Iwashima } 8898c5dae4cSKuniyuki Iwashima #if IS_ENABLED(CONFIG_IPV6) 8908c5dae4cSKuniyuki Iwashima else { 8918c5dae4cSKuniyuki Iwashima sk->sk_v6_rcv_saddr = *(struct in6_addr *)saddr; 8928c5dae4cSKuniyuki Iwashima } 8938c5dae4cSKuniyuki Iwashima #endif 8948c5dae4cSKuniyuki Iwashima } 8958c5dae4cSKuniyuki Iwashima 896e0833d1fSKuniyuki Iwashima static int __inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family, bool reset) 89728044fc1SJoanne Koong { 898429e42c1SKuniyuki Iwashima struct inet_hashinfo *hinfo = tcp_or_dccp_get_hashinfo(sk); 8998c5dae4cSKuniyuki Iwashima struct inet_bind_hashbucket *head, *head2; 90028044fc1SJoanne Koong struct inet_bind2_bucket *tb2, *new_tb2; 90128044fc1SJoanne Koong int l3mdev = inet_sk_bound_l3mdev(sk); 90228044fc1SJoanne Koong int port = inet_sk(sk)->inet_num; 90328044fc1SJoanne Koong struct net *net = sock_net(sk); 9048c5dae4cSKuniyuki Iwashima int bhash; 9058c5dae4cSKuniyuki Iwashima 9068c5dae4cSKuniyuki Iwashima if (!inet_csk(sk)->icsk_bind2_hash) { 9078c5dae4cSKuniyuki Iwashima /* Not bind()ed before. */ 908e0833d1fSKuniyuki Iwashima if (reset) 909e0833d1fSKuniyuki Iwashima inet_reset_saddr(sk); 910e0833d1fSKuniyuki Iwashima else 9118c5dae4cSKuniyuki Iwashima inet_update_saddr(sk, saddr, family); 912e0833d1fSKuniyuki Iwashima 9138c5dae4cSKuniyuki Iwashima return 0; 9148c5dae4cSKuniyuki Iwashima } 91528044fc1SJoanne Koong 91628044fc1SJoanne Koong /* Allocate a bind2 bucket ahead of time to avoid permanently putting 91728044fc1SJoanne Koong * the bhash2 table in an inconsistent state if a new tb2 bucket 91828044fc1SJoanne Koong * allocation fails. 91928044fc1SJoanne Koong */ 92028044fc1SJoanne Koong new_tb2 = kmem_cache_alloc(hinfo->bind2_bucket_cachep, GFP_ATOMIC); 921e0833d1fSKuniyuki Iwashima if (!new_tb2) { 922e0833d1fSKuniyuki Iwashima if (reset) { 923e0833d1fSKuniyuki Iwashima /* The (INADDR_ANY, port) bucket might have already 924e0833d1fSKuniyuki Iwashima * been freed, then we cannot fixup icsk_bind2_hash, 925e0833d1fSKuniyuki Iwashima * so we give up and unlink sk from bhash/bhash2 not 926e0833d1fSKuniyuki Iwashima * to leave inconsistency in bhash2. 927e0833d1fSKuniyuki Iwashima */ 928e0833d1fSKuniyuki Iwashima inet_put_port(sk); 929e0833d1fSKuniyuki Iwashima inet_reset_saddr(sk); 930e0833d1fSKuniyuki Iwashima } 931e0833d1fSKuniyuki Iwashima 93228044fc1SJoanne Koong return -ENOMEM; 933e0833d1fSKuniyuki Iwashima } 93428044fc1SJoanne Koong 9358c5dae4cSKuniyuki Iwashima bhash = inet_bhashfn(net, port, hinfo->bhash_size); 9368c5dae4cSKuniyuki Iwashima head = &hinfo->bhash[bhash]; 93728044fc1SJoanne Koong head2 = inet_bhashfn_portaddr(hinfo, sk, net, port); 93828044fc1SJoanne Koong 9398c5dae4cSKuniyuki Iwashima /* If we change saddr locklessly, another thread 9408c5dae4cSKuniyuki Iwashima * iterating over bhash might see corrupted address. 9418c5dae4cSKuniyuki Iwashima */ 9428c5dae4cSKuniyuki Iwashima spin_lock_bh(&head->lock); 9438c5dae4cSKuniyuki Iwashima 9448c5dae4cSKuniyuki Iwashima spin_lock(&head2->lock); 94528044fc1SJoanne Koong __sk_del_bind2_node(sk); 9468acdad37SKuniyuki Iwashima inet_bind2_bucket_destroy(hinfo->bind2_bucket_cachep, inet_csk(sk)->icsk_bind2_hash); 9478c5dae4cSKuniyuki Iwashima spin_unlock(&head2->lock); 94828044fc1SJoanne Koong 949e0833d1fSKuniyuki Iwashima if (reset) 950e0833d1fSKuniyuki Iwashima inet_reset_saddr(sk); 951e0833d1fSKuniyuki Iwashima else 9528c5dae4cSKuniyuki Iwashima inet_update_saddr(sk, saddr, family); 9538c5dae4cSKuniyuki Iwashima 9548c5dae4cSKuniyuki Iwashima head2 = inet_bhashfn_portaddr(hinfo, sk, net, port); 9558c5dae4cSKuniyuki Iwashima 9568c5dae4cSKuniyuki Iwashima spin_lock(&head2->lock); 95728044fc1SJoanne Koong tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk); 95828044fc1SJoanne Koong if (!tb2) { 95928044fc1SJoanne Koong tb2 = new_tb2; 96028044fc1SJoanne Koong inet_bind2_bucket_init(tb2, net, head2, port, l3mdev, sk); 96128044fc1SJoanne Koong } 96228044fc1SJoanne Koong sk_add_bind2_node(sk, &tb2->owners); 96328044fc1SJoanne Koong inet_csk(sk)->icsk_bind2_hash = tb2; 9648c5dae4cSKuniyuki Iwashima spin_unlock(&head2->lock); 9658c5dae4cSKuniyuki Iwashima 9668c5dae4cSKuniyuki Iwashima spin_unlock_bh(&head->lock); 96728044fc1SJoanne Koong 96828044fc1SJoanne Koong if (tb2 != new_tb2) 96928044fc1SJoanne Koong kmem_cache_free(hinfo->bind2_bucket_cachep, new_tb2); 97028044fc1SJoanne Koong 97128044fc1SJoanne Koong return 0; 97228044fc1SJoanne Koong } 973e0833d1fSKuniyuki Iwashima 974e0833d1fSKuniyuki Iwashima int inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family) 975e0833d1fSKuniyuki Iwashima { 976e0833d1fSKuniyuki Iwashima return __inet_bhash2_update_saddr(sk, saddr, family, false); 977e0833d1fSKuniyuki Iwashima } 97828044fc1SJoanne Koong EXPORT_SYMBOL_GPL(inet_bhash2_update_saddr); 97928044fc1SJoanne Koong 980e0833d1fSKuniyuki Iwashima void inet_bhash2_reset_saddr(struct sock *sk) 981e0833d1fSKuniyuki Iwashima { 982e0833d1fSKuniyuki Iwashima if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) 983e0833d1fSKuniyuki Iwashima __inet_bhash2_update_saddr(sk, NULL, 0, true); 984e0833d1fSKuniyuki Iwashima } 985e0833d1fSKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet_bhash2_reset_saddr); 986e0833d1fSKuniyuki Iwashima 987190cc824SEric Dumazet /* RFC 6056 3.3.4. Algorithm 4: Double-Hash Port Selection Algorithm 988190cc824SEric Dumazet * Note that we use 32bit integers (vs RFC 'short integers') 989190cc824SEric Dumazet * because 2^16 is not a multiple of num_ephemeral and this 990190cc824SEric Dumazet * property might be used by clever attacker. 991aeac4ec8SGleb Mazovetskiy * 9924c2c8f03SWilly Tarreau * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though 993aeac4ec8SGleb Mazovetskiy * attacks were since demonstrated, thus we use 65536 by default instead 994aeac4ec8SGleb Mazovetskiy * to really give more isolation and privacy, at the expense of 256kB 995aeac4ec8SGleb Mazovetskiy * of kernel memory. 996190cc824SEric Dumazet */ 997aeac4ec8SGleb Mazovetskiy #define INET_TABLE_PERTURB_SIZE (1 << CONFIG_INET_TABLE_PERTURB_ORDER) 998e9261476SWilly Tarreau static u32 *table_perturb; 999190cc824SEric Dumazet 10005ee31fc1SPavel Emelyanov int __inet_hash_connect(struct inet_timewait_death_row *death_row, 1001b2d05756SWilly Tarreau struct sock *sk, u64 port_offset, 10025ee31fc1SPavel Emelyanov int (*check_established)(struct inet_timewait_death_row *, 1003b4d6444eSEric Dumazet struct sock *, __u16, struct inet_timewait_sock **)) 1004a7f5e7f1SArnaldo Carvalho de Melo { 1005a7f5e7f1SArnaldo Carvalho de Melo struct inet_hashinfo *hinfo = death_row->hashinfo; 100628044fc1SJoanne Koong struct inet_bind_hashbucket *head, *head2; 1007a7f5e7f1SArnaldo Carvalho de Melo struct inet_timewait_sock *tw = NULL; 10081580ab63SEric Dumazet int port = inet_sk(sk)->inet_num; 10091580ab63SEric Dumazet struct net *net = sock_net(sk); 101028044fc1SJoanne Koong struct inet_bind2_bucket *tb2; 10111580ab63SEric Dumazet struct inet_bind_bucket *tb; 101228044fc1SJoanne Koong bool tb_created = false; 10131580ab63SEric Dumazet u32 remaining, offset; 10141580ab63SEric Dumazet int ret, i, low, high; 101520718485SEric Dumazet bool local_ports; 101620718485SEric Dumazet int step, l3mdev; 1017190cc824SEric Dumazet u32 index; 10181580ab63SEric Dumazet 10191580ab63SEric Dumazet if (port) { 102021cbd90aSPietro Borrello local_bh_disable(); 10211580ab63SEric Dumazet ret = check_established(death_row, sk, port, NULL); 10221580ab63SEric Dumazet local_bh_enable(); 10231580ab63SEric Dumazet return ret; 10241580ab63SEric Dumazet } 1025a7f5e7f1SArnaldo Carvalho de Melo 10263c82a21fSRobert Shearman l3mdev = inet_sk_bound_l3mdev(sk); 10273c82a21fSRobert Shearman 102820718485SEric Dumazet local_ports = inet_sk_get_local_port_range(sk, &low, &high); 102920718485SEric Dumazet step = local_ports ? 1 : 2; 103020718485SEric Dumazet 10311580ab63SEric Dumazet high++; /* [32768, 60999] -> [32768, 61000[ */ 10321580ab63SEric Dumazet remaining = high - low; 103320718485SEric Dumazet if (!local_ports && remaining > 1) 10341580ab63SEric Dumazet remaining &= ~1U; 1035227b60f5SStephen Hemminger 10362a4187f4SJason A. Donenfeld get_random_sleepable_once(table_perturb, 1037e9261476SWilly Tarreau INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb)); 1038e8161345SWilly Tarreau index = port_offset & (INET_TABLE_PERTURB_SIZE - 1); 1039190cc824SEric Dumazet 10409e9b70aeSWilly Tarreau offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32); 1041b2d05756SWilly Tarreau offset %= remaining; 1042b2d05756SWilly Tarreau 10431580ab63SEric Dumazet /* In first pass we try ports of @low parity. 10441580ab63SEric Dumazet * inet_csk_get_port() does the opposite choice. 104507f4c900SEric Dumazet */ 104620718485SEric Dumazet if (!local_ports) 10471580ab63SEric Dumazet offset &= ~1U; 10481580ab63SEric Dumazet other_parity_scan: 10491580ab63SEric Dumazet port = low + offset; 105020718485SEric Dumazet for (i = 0; i < remaining; i += step, port += step) { 10511580ab63SEric Dumazet if (unlikely(port >= high)) 10521580ab63SEric Dumazet port -= remaining; 1053122ff243SWANG Cong if (inet_is_local_reserved_port(net, port)) 1054e3826f1eSAmerigo Wang continue; 10557f635ab7SPavel Emelyanov head = &hinfo->bhash[inet_bhashfn(net, port, 10567f635ab7SPavel Emelyanov hinfo->bhash_size)]; 10571580ab63SEric Dumazet spin_lock_bh(&head->lock); 1058a7f5e7f1SArnaldo Carvalho de Melo 10591580ab63SEric Dumazet /* Does not bother with rcv_saddr checks, because 10601580ab63SEric Dumazet * the established check is already unique enough. 1061a7f5e7f1SArnaldo Carvalho de Melo */ 1062b67bfe0dSSasha Levin inet_bind_bucket_for_each(tb, &head->chain) { 106328044fc1SJoanne Koong if (inet_bind_bucket_match(tb, net, port, l3mdev)) { 1064da5e3630STom Herbert if (tb->fastreuse >= 0 || 1065da5e3630STom Herbert tb->fastreuseport >= 0) 1066a7f5e7f1SArnaldo Carvalho de Melo goto next_port; 1067a9d8f911SEvgeniy Polyakov WARN_ON(hlist_empty(&tb->owners)); 10685ee31fc1SPavel Emelyanov if (!check_established(death_row, sk, 10695ee31fc1SPavel Emelyanov port, &tw)) 1070a7f5e7f1SArnaldo Carvalho de Melo goto ok; 1071a7f5e7f1SArnaldo Carvalho de Melo goto next_port; 1072a7f5e7f1SArnaldo Carvalho de Melo } 1073a7f5e7f1SArnaldo Carvalho de Melo } 1074a7f5e7f1SArnaldo Carvalho de Melo 1075941b1d22SPavel Emelyanov tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep, 10763c82a21fSRobert Shearman net, head, port, l3mdev); 1077a7f5e7f1SArnaldo Carvalho de Melo if (!tb) { 10781580ab63SEric Dumazet spin_unlock_bh(&head->lock); 10791580ab63SEric Dumazet return -ENOMEM; 1080a7f5e7f1SArnaldo Carvalho de Melo } 108128044fc1SJoanne Koong tb_created = true; 1082a7f5e7f1SArnaldo Carvalho de Melo tb->fastreuse = -1; 1083da5e3630STom Herbert tb->fastreuseport = -1; 1084a7f5e7f1SArnaldo Carvalho de Melo goto ok; 1085a7f5e7f1SArnaldo Carvalho de Melo next_port: 10861580ab63SEric Dumazet spin_unlock_bh(&head->lock); 10871580ab63SEric Dumazet cond_resched(); 1088a7f5e7f1SArnaldo Carvalho de Melo } 10891580ab63SEric Dumazet 109020718485SEric Dumazet if (!local_ports) { 10911580ab63SEric Dumazet offset++; 10921580ab63SEric Dumazet if ((offset & 1) && remaining > 1) 10931580ab63SEric Dumazet goto other_parity_scan; 109420718485SEric Dumazet } 1095a7f5e7f1SArnaldo Carvalho de Melo return -EADDRNOTAVAIL; 1096a7f5e7f1SArnaldo Carvalho de Melo 1097a7f5e7f1SArnaldo Carvalho de Melo ok: 109828044fc1SJoanne Koong /* Find the corresponding tb2 bucket since we need to 109928044fc1SJoanne Koong * add the socket to the bhash2 table as well 110028044fc1SJoanne Koong */ 110128044fc1SJoanne Koong head2 = inet_bhashfn_portaddr(hinfo, sk, net, port); 110228044fc1SJoanne Koong spin_lock(&head2->lock); 110328044fc1SJoanne Koong 110428044fc1SJoanne Koong tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk); 110528044fc1SJoanne Koong if (!tb2) { 110628044fc1SJoanne Koong tb2 = inet_bind2_bucket_create(hinfo->bind2_bucket_cachep, net, 110728044fc1SJoanne Koong head2, port, l3mdev, sk); 110828044fc1SJoanne Koong if (!tb2) 110928044fc1SJoanne Koong goto error; 111028044fc1SJoanne Koong } 111128044fc1SJoanne Koong 1112ca7af040SWilly Tarreau /* Here we want to add a little bit of randomness to the next source 1113ca7af040SWilly Tarreau * port that will be chosen. We use a max() with a random here so that 1114ca7af040SWilly Tarreau * on low contention the randomness is maximal and on high contention 1115ca7af040SWilly Tarreau * it may be inexistent. 1116c579bd1bSEric Dumazet */ 111720718485SEric Dumazet i = max_t(int, i, get_random_u32_below(8) * step); 111820718485SEric Dumazet WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + step); 1119a7f5e7f1SArnaldo Carvalho de Melo 1120a7f5e7f1SArnaldo Carvalho de Melo /* Head lock still held and bh's disabled */ 112128044fc1SJoanne Koong inet_bind_hash(sk, tb, tb2, port); 112228044fc1SJoanne Koong 1123a7f5e7f1SArnaldo Carvalho de Melo if (sk_unhashed(sk)) { 1124c720c7e8SEric Dumazet inet_sk(sk)->inet_sport = htons(port); 112501770a16SRicardo Dias inet_ehash_nolisten(sk, (struct sock *)tw, NULL); 1126a7f5e7f1SArnaldo Carvalho de Melo } 11273cdaedaeSEric Dumazet if (tw) 1128fc01538fSEric Dumazet inet_twsk_bind_unhash(tw, hinfo); 1129936a192fSKuniyuki Iwashima 1130936a192fSKuniyuki Iwashima spin_unlock(&head2->lock); 1131a7f5e7f1SArnaldo Carvalho de Melo spin_unlock(&head->lock); 1132936a192fSKuniyuki Iwashima 1133dbe7faa4SEric Dumazet if (tw) 1134dbe7faa4SEric Dumazet inet_twsk_deschedule_put(tw); 1135a7f5e7f1SArnaldo Carvalho de Melo local_bh_enable(); 11361580ab63SEric Dumazet return 0; 113728044fc1SJoanne Koong 113828044fc1SJoanne Koong error: 113928044fc1SJoanne Koong spin_unlock(&head2->lock); 114028044fc1SJoanne Koong if (tb_created) 114128044fc1SJoanne Koong inet_bind_bucket_destroy(hinfo->bind_bucket_cachep, tb); 114228044fc1SJoanne Koong spin_unlock_bh(&head->lock); 114328044fc1SJoanne Koong return -ENOMEM; 1144a7f5e7f1SArnaldo Carvalho de Melo } 11455ee31fc1SPavel Emelyanov 11465ee31fc1SPavel Emelyanov /* 11475ee31fc1SPavel Emelyanov * Bind a port for a connect operation and hash it. 11485ee31fc1SPavel Emelyanov */ 11495ee31fc1SPavel Emelyanov int inet_hash_connect(struct inet_timewait_death_row *death_row, 11505ee31fc1SPavel Emelyanov struct sock *sk) 11515ee31fc1SPavel Emelyanov { 1152b2d05756SWilly Tarreau u64 port_offset = 0; 1153e2baad9eSEric Dumazet 1154e2baad9eSEric Dumazet if (!inet_sk(sk)->inet_num) 1155e2baad9eSEric Dumazet port_offset = inet_sk_port_offset(sk); 1156e2baad9eSEric Dumazet return __inet_hash_connect(death_row, sk, port_offset, 1157b4d6444eSEric Dumazet __inet_check_established); 11585ee31fc1SPavel Emelyanov } 1159a7f5e7f1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_hash_connect); 11605caea4eaSEric Dumazet 1161c92c81dfSPeter Oskolkov static void init_hashinfo_lhash2(struct inet_hashinfo *h) 1162c92c81dfSPeter Oskolkov { 1163c92c81dfSPeter Oskolkov int i; 1164c92c81dfSPeter Oskolkov 1165c92c81dfSPeter Oskolkov for (i = 0; i <= h->lhash2_mask; i++) { 1166c92c81dfSPeter Oskolkov spin_lock_init(&h->lhash2[i].lock); 1167cae3873cSMartin KaFai Lau INIT_HLIST_NULLS_HEAD(&h->lhash2[i].nulls_head, 1168cae3873cSMartin KaFai Lau i + LISTENING_NULLS_BASE); 1169c92c81dfSPeter Oskolkov } 1170c92c81dfSPeter Oskolkov } 1171c92c81dfSPeter Oskolkov 117261b7c691SMartin KaFai Lau void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name, 117361b7c691SMartin KaFai Lau unsigned long numentries, int scale, 117461b7c691SMartin KaFai Lau unsigned long low_limit, 117561b7c691SMartin KaFai Lau unsigned long high_limit) 117661b7c691SMartin KaFai Lau { 117761b7c691SMartin KaFai Lau h->lhash2 = alloc_large_system_hash(name, 117861b7c691SMartin KaFai Lau sizeof(*h->lhash2), 117961b7c691SMartin KaFai Lau numentries, 118061b7c691SMartin KaFai Lau scale, 118161b7c691SMartin KaFai Lau 0, 118261b7c691SMartin KaFai Lau NULL, 118361b7c691SMartin KaFai Lau &h->lhash2_mask, 118461b7c691SMartin KaFai Lau low_limit, 118561b7c691SMartin KaFai Lau high_limit); 1186c92c81dfSPeter Oskolkov init_hashinfo_lhash2(h); 1187e9261476SWilly Tarreau 1188e9261476SWilly Tarreau /* this one is used for source ports of outgoing connections */ 1189e67b72b9SMuchun Song table_perturb = alloc_large_system_hash("Table-perturb", 1190e67b72b9SMuchun Song sizeof(*table_perturb), 1191e67b72b9SMuchun Song INET_TABLE_PERTURB_SIZE, 1192e67b72b9SMuchun Song 0, 0, NULL, NULL, 1193e67b72b9SMuchun Song INET_TABLE_PERTURB_SIZE, 1194e67b72b9SMuchun Song INET_TABLE_PERTURB_SIZE); 1195c92c81dfSPeter Oskolkov } 119661b7c691SMartin KaFai Lau 1197c92c81dfSPeter Oskolkov int inet_hashinfo2_init_mod(struct inet_hashinfo *h) 1198c92c81dfSPeter Oskolkov { 1199c92c81dfSPeter Oskolkov h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL); 1200c92c81dfSPeter Oskolkov if (!h->lhash2) 1201c92c81dfSPeter Oskolkov return -ENOMEM; 1202c92c81dfSPeter Oskolkov 1203c92c81dfSPeter Oskolkov h->lhash2_mask = INET_LHTABLE_SIZE - 1; 1204c92c81dfSPeter Oskolkov /* INET_LHTABLE_SIZE must be a power of 2 */ 1205c92c81dfSPeter Oskolkov BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask); 1206c92c81dfSPeter Oskolkov 1207c92c81dfSPeter Oskolkov init_hashinfo_lhash2(h); 1208c92c81dfSPeter Oskolkov return 0; 120961b7c691SMartin KaFai Lau } 1210c92c81dfSPeter Oskolkov EXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod); 121161b7c691SMartin KaFai Lau 1212095dc8e0SEric Dumazet int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo) 1213095dc8e0SEric Dumazet { 121489e478a2SEric Dumazet unsigned int locksz = sizeof(spinlock_t); 1215095dc8e0SEric Dumazet unsigned int i, nblocks = 1; 1216095dc8e0SEric Dumazet 121789e478a2SEric Dumazet if (locksz != 0) { 1218095dc8e0SEric Dumazet /* allocate 2 cache lines or at least one spinlock per cpu */ 121989e478a2SEric Dumazet nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U); 1220095dc8e0SEric Dumazet nblocks = roundup_pow_of_two(nblocks * num_possible_cpus()); 1221095dc8e0SEric Dumazet 1222095dc8e0SEric Dumazet /* no more locks than number of hash buckets */ 1223095dc8e0SEric Dumazet nblocks = min(nblocks, hashinfo->ehash_mask + 1); 1224095dc8e0SEric Dumazet 1225752ade68SMichal Hocko hashinfo->ehash_locks = kvmalloc_array(nblocks, locksz, GFP_KERNEL); 1226095dc8e0SEric Dumazet if (!hashinfo->ehash_locks) 1227095dc8e0SEric Dumazet return -ENOMEM; 1228095dc8e0SEric Dumazet 1229095dc8e0SEric Dumazet for (i = 0; i < nblocks; i++) 1230095dc8e0SEric Dumazet spin_lock_init(&hashinfo->ehash_locks[i]); 1231095dc8e0SEric Dumazet } 1232095dc8e0SEric Dumazet hashinfo->ehash_locks_mask = nblocks - 1; 1233095dc8e0SEric Dumazet return 0; 1234095dc8e0SEric Dumazet } 1235095dc8e0SEric Dumazet EXPORT_SYMBOL_GPL(inet_ehash_locks_alloc); 1236d1e5e640SKuniyuki Iwashima 1237d1e5e640SKuniyuki Iwashima struct inet_hashinfo *inet_pernet_hashinfo_alloc(struct inet_hashinfo *hashinfo, 1238d1e5e640SKuniyuki Iwashima unsigned int ehash_entries) 1239d1e5e640SKuniyuki Iwashima { 1240d1e5e640SKuniyuki Iwashima struct inet_hashinfo *new_hashinfo; 1241d1e5e640SKuniyuki Iwashima int i; 1242d1e5e640SKuniyuki Iwashima 1243d1e5e640SKuniyuki Iwashima new_hashinfo = kmemdup(hashinfo, sizeof(*hashinfo), GFP_KERNEL); 1244d1e5e640SKuniyuki Iwashima if (!new_hashinfo) 1245d1e5e640SKuniyuki Iwashima goto err; 1246d1e5e640SKuniyuki Iwashima 1247d1e5e640SKuniyuki Iwashima new_hashinfo->ehash = vmalloc_huge(ehash_entries * sizeof(struct inet_ehash_bucket), 1248d1e5e640SKuniyuki Iwashima GFP_KERNEL_ACCOUNT); 1249d1e5e640SKuniyuki Iwashima if (!new_hashinfo->ehash) 1250d1e5e640SKuniyuki Iwashima goto free_hashinfo; 1251d1e5e640SKuniyuki Iwashima 1252d1e5e640SKuniyuki Iwashima new_hashinfo->ehash_mask = ehash_entries - 1; 1253d1e5e640SKuniyuki Iwashima 1254d1e5e640SKuniyuki Iwashima if (inet_ehash_locks_alloc(new_hashinfo)) 1255d1e5e640SKuniyuki Iwashima goto free_ehash; 1256d1e5e640SKuniyuki Iwashima 1257d1e5e640SKuniyuki Iwashima for (i = 0; i < ehash_entries; i++) 1258d1e5e640SKuniyuki Iwashima INIT_HLIST_NULLS_HEAD(&new_hashinfo->ehash[i].chain, i); 1259d1e5e640SKuniyuki Iwashima 1260d1e5e640SKuniyuki Iwashima new_hashinfo->pernet = true; 1261d1e5e640SKuniyuki Iwashima 1262d1e5e640SKuniyuki Iwashima return new_hashinfo; 1263d1e5e640SKuniyuki Iwashima 1264d1e5e640SKuniyuki Iwashima free_ehash: 1265d1e5e640SKuniyuki Iwashima vfree(new_hashinfo->ehash); 1266d1e5e640SKuniyuki Iwashima free_hashinfo: 1267d1e5e640SKuniyuki Iwashima kfree(new_hashinfo); 1268d1e5e640SKuniyuki Iwashima err: 1269d1e5e640SKuniyuki Iwashima return NULL; 1270d1e5e640SKuniyuki Iwashima } 1271d1e5e640SKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet_pernet_hashinfo_alloc); 1272d1e5e640SKuniyuki Iwashima 1273d1e5e640SKuniyuki Iwashima void inet_pernet_hashinfo_free(struct inet_hashinfo *hashinfo) 1274d1e5e640SKuniyuki Iwashima { 1275d1e5e640SKuniyuki Iwashima if (!hashinfo->pernet) 1276d1e5e640SKuniyuki Iwashima return; 1277d1e5e640SKuniyuki Iwashima 1278d1e5e640SKuniyuki Iwashima inet_ehash_locks_free(hashinfo); 1279d1e5e640SKuniyuki Iwashima vfree(hashinfo->ehash); 1280d1e5e640SKuniyuki Iwashima kfree(hashinfo); 1281d1e5e640SKuniyuki Iwashima } 1282d1e5e640SKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet_pernet_hashinfo_free); 1283