2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * Generic INET transport hashtables
8 * Authors: Lotsa people, from code originally in tcp
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/wait.h>
21 #include <linux/vmalloc.h>
22 #include <linux/bootmem.h>
24 #include <net/addrconf.h>
25 #include <net/inet_connection_sock.h>
26 #include <net/inet_hashtables.h>
27 #if IS_ENABLED(CONFIG_IPV6)
28 #include <net/inet6_hashtables.h>
30 #include <net/secure_seq.h>
33 #include <net/sock_reuseport.h>
35 static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
36 const __u16 lport, const __be32 faddr,
39 static u32 inet_ehash_secret __read_mostly;
41 net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
43 return __inet_ehashfn(laddr, lport, faddr, fport,
44 inet_ehash_secret + net_hash_mix(net));
47 /* This function handles inet_sock, but also timewait and request sockets
50 static u32 sk_ehashfn(const struct sock *sk)
52 #if IS_ENABLED(CONFIG_IPV6)
53 if (sk->sk_family == AF_INET6 &&
54 !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
55 return inet6_ehashfn(sock_net(sk),
56 &sk->sk_v6_rcv_saddr, sk->sk_num,
57 &sk->sk_v6_daddr, sk->sk_dport);
59 return inet_ehashfn(sock_net(sk),
60 sk->sk_rcv_saddr, sk->sk_num,
61 sk->sk_daddr, sk->sk_dport);
65 * Allocate and initialize a new local port bind bucket.
66 * The bindhash mutex for snum's hash chain must be held here.
68 struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
70 struct inet_bind_hashbucket *head,
71 const unsigned short snum)
73 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
76 write_pnet(&tb->ib_net, net);
79 tb->fastreuseport = 0;
80 INIT_HLIST_HEAD(&tb->owners);
81 hlist_add_head(&tb->node, &head->chain);
87 * Caller must hold hashbucket lock for this tb with local BH disabled
89 void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
91 if (hlist_empty(&tb->owners)) {
92 __hlist_del(&tb->node);
93 kmem_cache_free(cachep, tb);
97 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
98 const unsigned short snum)
100 inet_sk(sk)->inet_num = snum;
101 sk_add_bind_node(sk, &tb->owners);
102 inet_csk(sk)->icsk_bind_hash = tb;
106 * Get rid of any references to a local port held by the given sock.
108 static void __inet_put_port(struct sock *sk)
110 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
111 const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
112 hashinfo->bhash_size);
113 struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
114 struct inet_bind_bucket *tb;
116 spin_lock(&head->lock);
117 tb = inet_csk(sk)->icsk_bind_hash;
118 __sk_del_bind_node(sk);
119 inet_csk(sk)->icsk_bind_hash = NULL;
120 inet_sk(sk)->inet_num = 0;
121 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
122 spin_unlock(&head->lock);
125 void inet_put_port(struct sock *sk)
131 EXPORT_SYMBOL(inet_put_port);
133 int __inet_inherit_port(const struct sock *sk, struct sock *child)
135 struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
136 unsigned short port = inet_sk(child)->inet_num;
137 const int bhash = inet_bhashfn(sock_net(sk), port,
139 struct inet_bind_hashbucket *head = &table->bhash[bhash];
140 struct inet_bind_bucket *tb;
142 spin_lock(&head->lock);
143 tb = inet_csk(sk)->icsk_bind_hash;
145 spin_unlock(&head->lock);
148 if (tb->port != port) {
149 /* NOTE: using tproxy and redirecting skbs to a proxy
150 * on a different listener port breaks the assumption
151 * that the listener socket's icsk_bind_hash is the same
152 * as that of the child socket. We have to look up or
153 * create a new bind bucket for the child here. */
154 inet_bind_bucket_for_each(tb, &head->chain) {
155 if (net_eq(ib_net(tb), sock_net(sk)) &&
160 tb = inet_bind_bucket_create(table->bind_bucket_cachep,
161 sock_net(sk), head, port);
163 spin_unlock(&head->lock);
167 inet_csk_update_fastreuse(tb, child);
169 inet_bind_hash(child, tb, port);
170 spin_unlock(&head->lock);
174 EXPORT_SYMBOL_GPL(__inet_inherit_port);
176 static struct inet_listen_hashbucket *
177 inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
181 #if IS_ENABLED(CONFIG_IPV6)
182 if (sk->sk_family == AF_INET6)
183 hash = ipv6_portaddr_hash(sock_net(sk),
184 &sk->sk_v6_rcv_saddr,
185 inet_sk(sk)->inet_num);
188 hash = ipv4_portaddr_hash(sock_net(sk),
189 inet_sk(sk)->inet_rcv_saddr,
190 inet_sk(sk)->inet_num);
191 return inet_lhash2_bucket(h, hash);
194 static void inet_hash2(struct inet_hashinfo *h, struct sock *sk)
196 struct inet_listen_hashbucket *ilb2;
201 ilb2 = inet_lhash2_bucket_sk(h, sk);
203 spin_lock(&ilb2->lock);
204 if (sk->sk_reuseport && sk->sk_family == AF_INET6)
205 hlist_add_tail_rcu(&inet_csk(sk)->icsk_listen_portaddr_node,
208 hlist_add_head_rcu(&inet_csk(sk)->icsk_listen_portaddr_node,
211 spin_unlock(&ilb2->lock);
214 static void inet_unhash2(struct inet_hashinfo *h, struct sock *sk)
216 struct inet_listen_hashbucket *ilb2;
219 WARN_ON_ONCE(hlist_unhashed(&inet_csk(sk)->icsk_listen_portaddr_node)))
222 ilb2 = inet_lhash2_bucket_sk(h, sk);
224 spin_lock(&ilb2->lock);
225 hlist_del_init_rcu(&inet_csk(sk)->icsk_listen_portaddr_node);
227 spin_unlock(&ilb2->lock);
230 static inline int compute_score(struct sock *sk, struct net *net,
231 const unsigned short hnum, const __be32 daddr,
232 const int dif, const int sdif, bool exact_dif)
235 struct inet_sock *inet = inet_sk(sk);
237 if (net_eq(sock_net(sk), net) && inet->inet_num == hnum &&
238 !ipv6_only_sock(sk)) {
239 __be32 rcv_saddr = inet->inet_rcv_saddr;
240 score = sk->sk_family == PF_INET ? 2 : 1;
242 if (rcv_saddr != daddr)
246 if (sk->sk_bound_dev_if || exact_dif) {
247 bool dev_match = (sk->sk_bound_dev_if == dif ||
248 sk->sk_bound_dev_if == sdif);
252 if (sk->sk_bound_dev_if)
255 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
262 * Here are some nice properties to exploit here. The BSD API
263 * does not allow a listening sock to specify the remote port nor the
264 * remote address for the connection. So always assume those are both
265 * wildcarded during the search since they can never be otherwise.
268 /* called with rcu_read_lock() : No refcount taken on the socket */
269 static struct sock *inet_lhash2_lookup(struct net *net,
270 struct inet_listen_hashbucket *ilb2,
271 struct sk_buff *skb, int doff,
272 const __be32 saddr, __be16 sport,
273 const __be32 daddr, const unsigned short hnum,
274 const int dif, const int sdif)
276 bool exact_dif = inet_exact_dif_match(net, skb);
277 struct inet_connection_sock *icsk;
278 struct sock *sk, *result = NULL;
279 int score, hiscore = 0;
282 inet_lhash2_for_each_icsk_rcu(icsk, &ilb2->head) {
283 sk = (struct sock *)icsk;
284 score = compute_score(sk, net, hnum, daddr,
285 dif, sdif, exact_dif);
286 if (score > hiscore) {
287 if (sk->sk_reuseport) {
288 phash = inet_ehashfn(net, daddr, hnum,
290 result = reuseport_select_sock(sk, phash,
303 struct sock *__inet_lookup_listener(struct net *net,
304 struct inet_hashinfo *hashinfo,
305 struct sk_buff *skb, int doff,
306 const __be32 saddr, __be16 sport,
307 const __be32 daddr, const unsigned short hnum,
308 const int dif, const int sdif)
310 unsigned int hash = inet_lhashfn(net, hnum);
311 struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
312 bool exact_dif = inet_exact_dif_match(net, skb);
313 struct inet_listen_hashbucket *ilb2;
314 struct sock *sk, *result = NULL;
315 struct hlist_nulls_node *node;
316 int score, hiscore = 0;
320 if (ilb->count <= 10 || !hashinfo->lhash2)
323 /* Too many sk in the ilb bucket (which is hashed by port alone).
324 * Try lhash2 (which is hashed by port and addr) instead.
327 hash2 = ipv4_portaddr_hash(net, daddr, hnum);
328 ilb2 = inet_lhash2_bucket(hashinfo, hash2);
329 if (ilb2->count > ilb->count)
332 result = inet_lhash2_lookup(net, ilb2, skb, doff,
333 saddr, sport, daddr, hnum,
338 /* Lookup lhash2 with INADDR_ANY */
340 hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
341 ilb2 = inet_lhash2_bucket(hashinfo, hash2);
342 if (ilb2->count > ilb->count)
345 result = inet_lhash2_lookup(net, ilb2, skb, doff,
346 saddr, sport, daddr, hnum,
351 sk_nulls_for_each_rcu(sk, node, &ilb->nulls_head) {
352 score = compute_score(sk, net, hnum, daddr,
353 dif, sdif, exact_dif);
354 if (score > hiscore) {
355 if (sk->sk_reuseport) {
356 phash = inet_ehashfn(net, daddr, hnum,
358 result = reuseport_select_sock(sk, phash,
368 if (unlikely(IS_ERR(result)))
372 EXPORT_SYMBOL_GPL(__inet_lookup_listener);
374 /* All sockets share common refcount, but have different destructors */
375 void sock_gen_put(struct sock *sk)
377 if (!refcount_dec_and_test(&sk->sk_refcnt))
380 if (sk->sk_state == TCP_TIME_WAIT)
381 inet_twsk_free(inet_twsk(sk));
382 else if (sk->sk_state == TCP_NEW_SYN_RECV)
383 reqsk_free(inet_reqsk(sk));
387 EXPORT_SYMBOL_GPL(sock_gen_put);
389 void sock_edemux(struct sk_buff *skb)
391 sock_gen_put(skb->sk);
393 EXPORT_SYMBOL(sock_edemux);
395 struct sock *__inet_lookup_established(struct net *net,
396 struct inet_hashinfo *hashinfo,
397 const __be32 saddr, const __be16 sport,
398 const __be32 daddr, const u16 hnum,
399 const int dif, const int sdif)
401 INET_ADDR_COOKIE(acookie, saddr, daddr);
402 const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
404 const struct hlist_nulls_node *node;
405 /* Optimize here for direct hit, only listening connections can
406 * have wildcards anyways.
408 unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
409 unsigned int slot = hash & hashinfo->ehash_mask;
410 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
413 sk_nulls_for_each_rcu(sk, node, &head->chain) {
414 if (sk->sk_hash != hash)
416 if (likely(INET_MATCH(sk, net, acookie,
417 saddr, daddr, ports, dif, sdif))) {
418 if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
420 if (unlikely(!INET_MATCH(sk, net, acookie,
430 * if the nulls value we got at the end of this lookup is
431 * not the expected one, we must restart lookup.
432 * We probably met an item that was moved to another chain.
434 if (get_nulls_value(node) != slot)
441 EXPORT_SYMBOL_GPL(__inet_lookup_established);
443 /* called with local bh disabled */
444 static int __inet_check_established(struct inet_timewait_death_row *death_row,
445 struct sock *sk, __u16 lport,
446 struct inet_timewait_sock **twp)
448 struct inet_hashinfo *hinfo = death_row->hashinfo;
449 struct inet_sock *inet = inet_sk(sk);
450 __be32 daddr = inet->inet_rcv_saddr;
451 __be32 saddr = inet->inet_daddr;
452 int dif = sk->sk_bound_dev_if;
453 struct net *net = sock_net(sk);
454 int sdif = l3mdev_master_ifindex_by_index(net, dif);
455 INET_ADDR_COOKIE(acookie, saddr, daddr);
456 const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
457 unsigned int hash = inet_ehashfn(net, daddr, lport,
458 saddr, inet->inet_dport);
459 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
460 spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
462 const struct hlist_nulls_node *node;
463 struct inet_timewait_sock *tw = NULL;
467 sk_nulls_for_each(sk2, node, &head->chain) {
468 if (sk2->sk_hash != hash)
471 if (likely(INET_MATCH(sk2, net, acookie,
472 saddr, daddr, ports, dif, sdif))) {
473 if (sk2->sk_state == TCP_TIME_WAIT) {
475 if (twsk_unique(sk, sk2, twp))
482 /* Must record num and sport now. Otherwise we will see
483 * in hash table socket with a funny identity.
485 inet->inet_num = lport;
486 inet->inet_sport = htons(lport);
488 WARN_ON(!sk_unhashed(sk));
489 __sk_nulls_add_node_rcu(sk, &head->chain);
491 sk_nulls_del_node_init_rcu((struct sock *)tw);
492 __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
495 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
500 /* Silly. Should hash-dance instead... */
501 inet_twsk_deschedule_put(tw);
507 return -EADDRNOTAVAIL;
510 static u32 inet_sk_port_offset(const struct sock *sk)
512 const struct inet_sock *inet = inet_sk(sk);
514 return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
519 /* Searches for an exsiting socket in the ehash bucket list.
520 * Returns true if found, false otherwise.
522 static bool inet_ehash_lookup_by_sk(struct sock *sk,
523 struct hlist_nulls_head *list)
525 const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num);
526 const int sdif = sk->sk_bound_dev_if;
527 const int dif = sk->sk_bound_dev_if;
528 const struct hlist_nulls_node *node;
529 struct net *net = sock_net(sk);
532 INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
534 sk_nulls_for_each_rcu(esk, node, list) {
535 if (esk->sk_hash != sk->sk_hash)
537 if (sk->sk_family == AF_INET) {
538 if (unlikely(INET_MATCH(esk, net, acookie,
541 ports, dif, sdif))) {
545 #if IS_ENABLED(CONFIG_IPV6)
546 else if (sk->sk_family == AF_INET6) {
547 if (unlikely(INET6_MATCH(esk, net,
549 &sk->sk_v6_rcv_saddr,
550 ports, dif, sdif))) {
559 /* Insert a socket into ehash, and eventually remove another one
560 * (The another one can be a SYN_RECV or TIMEWAIT)
561 * If an existing socket already exists, socket sk is not inserted,
562 * and sets found_dup_sk parameter to true.
564 bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
566 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
567 struct hlist_nulls_head *list;
568 struct inet_ehash_bucket *head;
572 WARN_ON_ONCE(!sk_unhashed(sk));
574 sk->sk_hash = sk_ehashfn(sk);
575 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
577 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
581 WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
582 ret = sk_nulls_del_node_init_rcu(osk);
583 } else if (found_dup_sk) {
584 *found_dup_sk = inet_ehash_lookup_by_sk(sk, list);
590 __sk_nulls_add_node_rcu(sk, list);
597 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
599 bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
602 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
604 percpu_counter_inc(sk->sk_prot->orphan_count);
605 inet_sk_set_state(sk, TCP_CLOSE);
606 sock_set_flag(sk, SOCK_DEAD);
607 inet_csk_destroy_sock(sk);
611 EXPORT_SYMBOL_GPL(inet_ehash_nolisten);
613 static int inet_reuseport_add_sock(struct sock *sk,
614 struct inet_listen_hashbucket *ilb)
616 struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
617 const struct hlist_nulls_node *node;
619 kuid_t uid = sock_i_uid(sk);
621 sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
623 sk2->sk_family == sk->sk_family &&
624 ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
625 sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
626 inet_csk(sk2)->icsk_bind_hash == tb &&
627 sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
628 inet_rcv_saddr_equal(sk, sk2, false))
629 return reuseport_add_sock(sk, sk2,
630 inet_rcv_saddr_any(sk));
633 return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
636 int __inet_hash(struct sock *sk, struct sock *osk)
638 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
639 struct inet_listen_hashbucket *ilb;
642 if (sk->sk_state != TCP_LISTEN) {
643 inet_ehash_nolisten(sk, osk, NULL);
646 WARN_ON(!sk_unhashed(sk));
647 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
649 spin_lock(&ilb->lock);
650 if (sk->sk_reuseport) {
651 err = inet_reuseport_add_sock(sk, ilb);
655 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
656 sk->sk_family == AF_INET6)
657 __sk_nulls_add_node_tail_rcu(sk, &ilb->nulls_head);
659 __sk_nulls_add_node_rcu(sk, &ilb->nulls_head);
660 inet_hash2(hashinfo, sk);
662 sock_set_flag(sk, SOCK_RCU_FREE);
663 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
665 spin_unlock(&ilb->lock);
669 EXPORT_SYMBOL(__inet_hash);
671 int inet_hash(struct sock *sk)
675 if (sk->sk_state != TCP_CLOSE) {
677 err = __inet_hash(sk, NULL);
683 EXPORT_SYMBOL_GPL(inet_hash);
685 void inet_unhash(struct sock *sk)
687 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
688 struct inet_listen_hashbucket *ilb = NULL;
694 if (sk->sk_state == TCP_LISTEN) {
695 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
698 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
704 if (rcu_access_pointer(sk->sk_reuseport_cb))
705 reuseport_detach_sock(sk);
707 inet_unhash2(hashinfo, sk);
710 __sk_nulls_del_node_init_rcu(sk);
711 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
713 spin_unlock_bh(lock);
715 EXPORT_SYMBOL_GPL(inet_unhash);
717 int __inet_hash_connect(struct inet_timewait_death_row *death_row,
718 struct sock *sk, u32 port_offset,
719 int (*check_established)(struct inet_timewait_death_row *,
720 struct sock *, __u16, struct inet_timewait_sock **))
722 struct inet_hashinfo *hinfo = death_row->hashinfo;
723 struct inet_timewait_sock *tw = NULL;
724 struct inet_bind_hashbucket *head;
725 int port = inet_sk(sk)->inet_num;
726 struct net *net = sock_net(sk);
727 struct inet_bind_bucket *tb;
728 u32 remaining, offset;
729 int ret, i, low, high;
733 head = &hinfo->bhash[inet_bhashfn(net, port,
735 tb = inet_csk(sk)->icsk_bind_hash;
736 spin_lock_bh(&head->lock);
737 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
738 inet_ehash_nolisten(sk, NULL, NULL);
739 spin_unlock_bh(&head->lock);
742 spin_unlock(&head->lock);
743 /* No definite answer... Walk to established hash table */
744 ret = check_established(death_row, sk, port, NULL);
749 inet_get_local_port_range(net, &low, &high);
750 high++; /* [32768, 60999] -> [32768, 61000[ */
751 remaining = high - low;
752 if (likely(remaining > 1))
755 offset = (hint + port_offset) % remaining;
756 /* In first pass we try ports of @low parity.
757 * inet_csk_get_port() does the opposite choice.
762 for (i = 0; i < remaining; i += 2, port += 2) {
763 if (unlikely(port >= high))
765 if (inet_is_local_reserved_port(net, port))
767 head = &hinfo->bhash[inet_bhashfn(net, port,
769 spin_lock_bh(&head->lock);
771 /* Does not bother with rcv_saddr checks, because
772 * the established check is already unique enough.
774 inet_bind_bucket_for_each(tb, &head->chain) {
775 if (net_eq(ib_net(tb), net) && tb->port == port) {
776 if (tb->fastreuse >= 0 ||
777 tb->fastreuseport >= 0)
779 WARN_ON(hlist_empty(&tb->owners));
780 if (!check_established(death_row, sk,
787 tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
790 spin_unlock_bh(&head->lock);
794 tb->fastreuseport = -1;
797 spin_unlock_bh(&head->lock);
802 if ((offset & 1) && remaining > 1)
803 goto other_parity_scan;
805 return -EADDRNOTAVAIL;
810 /* Head lock still held and bh's disabled */
811 inet_bind_hash(sk, tb, port);
812 if (sk_unhashed(sk)) {
813 inet_sk(sk)->inet_sport = htons(port);
814 inet_ehash_nolisten(sk, (struct sock *)tw, NULL);
817 inet_twsk_bind_unhash(tw, hinfo);
818 spin_unlock(&head->lock);
820 inet_twsk_deschedule_put(tw);
826 * Bind a port for a connect operation and hash it.
828 int inet_hash_connect(struct inet_timewait_death_row *death_row,
833 if (!inet_sk(sk)->inet_num)
834 port_offset = inet_sk_port_offset(sk);
835 return __inet_hash_connect(death_row, sk, port_offset,
836 __inet_check_established);
838 EXPORT_SYMBOL_GPL(inet_hash_connect);
840 void inet_hashinfo_init(struct inet_hashinfo *h)
844 for (i = 0; i < INET_LHTABLE_SIZE; i++) {
845 spin_lock_init(&h->listening_hash[i].lock);
846 INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].nulls_head,
847 i + LISTENING_NULLS_BASE);
848 h->listening_hash[i].count = 0;
853 EXPORT_SYMBOL_GPL(inet_hashinfo_init);
855 void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
856 unsigned long numentries, int scale,
857 unsigned long low_limit,
858 unsigned long high_limit)
862 h->lhash2 = alloc_large_system_hash(name,
872 for (i = 0; i <= h->lhash2_mask; i++) {
873 spin_lock_init(&h->lhash2[i].lock);
874 INIT_HLIST_HEAD(&h->lhash2[i].head);
875 h->lhash2[i].count = 0;
879 int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
881 unsigned int locksz = sizeof(spinlock_t);
882 unsigned int i, nblocks = 1;
885 /* allocate 2 cache lines or at least one spinlock per cpu */
886 nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U);
887 nblocks = roundup_pow_of_two(nblocks * num_possible_cpus());
889 /* no more locks than number of hash buckets */
890 nblocks = min(nblocks, hashinfo->ehash_mask + 1);
892 hashinfo->ehash_locks = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
893 if (!hashinfo->ehash_locks)
896 for (i = 0; i < nblocks; i++)
897 spin_lock_init(&hashinfo->ehash_locks[i]);
899 hashinfo->ehash_locks_mask = nblocks - 1;
902 EXPORT_SYMBOL_GPL(inet_ehash_locks_alloc);