/net/ipv4/inet_hashtables.c

http://github.com/mirrors/linux · C · 827 lines · 659 code · 101 blank · 67 comment · 113 complexity · dc9bfd15f31cefceede0f14dc3be53e3 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Generic INET transport hashtables
  8. *
  9. * Authors: Lotsa people, from code originally in tcp
  10. */
  11. #include <linux/module.h>
  12. #include <linux/random.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/wait.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/memblock.h>
  18. #include <net/addrconf.h>
  19. #include <net/inet_connection_sock.h>
  20. #include <net/inet_hashtables.h>
  21. #include <net/secure_seq.h>
  22. #include <net/ip.h>
  23. #include <net/tcp.h>
  24. #include <net/sock_reuseport.h>
  25. static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
  26. const __u16 lport, const __be32 faddr,
  27. const __be16 fport)
  28. {
  29. static u32 inet_ehash_secret __read_mostly;
  30. net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
  31. return __inet_ehashfn(laddr, lport, faddr, fport,
  32. inet_ehash_secret + net_hash_mix(net));
  33. }
  34. /* This function handles inet_sock, but also timewait and request sockets
  35. * for IPv4/IPv6.
  36. */
  37. static u32 sk_ehashfn(const struct sock *sk)
  38. {
  39. #if IS_ENABLED(CONFIG_IPV6)
  40. if (sk->sk_family == AF_INET6 &&
  41. !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  42. return inet6_ehashfn(sock_net(sk),
  43. &sk->sk_v6_rcv_saddr, sk->sk_num,
  44. &sk->sk_v6_daddr, sk->sk_dport);
  45. #endif
  46. return inet_ehashfn(sock_net(sk),
  47. sk->sk_rcv_saddr, sk->sk_num,
  48. sk->sk_daddr, sk->sk_dport);
  49. }
  50. /*
  51. * Allocate and initialize a new local port bind bucket.
  52. * The bindhash mutex for snum's hash chain must be held here.
  53. */
  54. struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
  55. struct net *net,
  56. struct inet_bind_hashbucket *head,
  57. const unsigned short snum,
  58. int l3mdev)
  59. {
  60. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
  61. if (tb) {
  62. write_pnet(&tb->ib_net, net);
  63. tb->l3mdev = l3mdev;
  64. tb->port = snum;
  65. tb->fastreuse = 0;
  66. tb->fastreuseport = 0;
  67. INIT_HLIST_HEAD(&tb->owners);
  68. hlist_add_head(&tb->node, &head->chain);
  69. }
  70. return tb;
  71. }
  72. /*
  73. * Caller must hold hashbucket lock for this tb with local BH disabled
  74. */
  75. void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
  76. {
  77. if (hlist_empty(&tb->owners)) {
  78. __hlist_del(&tb->node);
  79. kmem_cache_free(cachep, tb);
  80. }
  81. }
  82. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  83. const unsigned short snum)
  84. {
  85. inet_sk(sk)->inet_num = snum;
  86. sk_add_bind_node(sk, &tb->owners);
  87. inet_csk(sk)->icsk_bind_hash = tb;
  88. }
  89. /*
  90. * Get rid of any references to a local port held by the given sock.
  91. */
  92. static void __inet_put_port(struct sock *sk)
  93. {
  94. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  95. const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
  96. hashinfo->bhash_size);
  97. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  98. struct inet_bind_bucket *tb;
  99. spin_lock(&head->lock);
  100. tb = inet_csk(sk)->icsk_bind_hash;
  101. __sk_del_bind_node(sk);
  102. inet_csk(sk)->icsk_bind_hash = NULL;
  103. inet_sk(sk)->inet_num = 0;
  104. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  105. spin_unlock(&head->lock);
  106. }
  107. void inet_put_port(struct sock *sk)
  108. {
  109. local_bh_disable();
  110. __inet_put_port(sk);
  111. local_bh_enable();
  112. }
  113. EXPORT_SYMBOL(inet_put_port);
  114. int __inet_inherit_port(const struct sock *sk, struct sock *child)
  115. {
  116. struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
  117. unsigned short port = inet_sk(child)->inet_num;
  118. const int bhash = inet_bhashfn(sock_net(sk), port,
  119. table->bhash_size);
  120. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  121. struct inet_bind_bucket *tb;
  122. int l3mdev;
  123. spin_lock(&head->lock);
  124. tb = inet_csk(sk)->icsk_bind_hash;
  125. if (unlikely(!tb)) {
  126. spin_unlock(&head->lock);
  127. return -ENOENT;
  128. }
  129. if (tb->port != port) {
  130. l3mdev = inet_sk_bound_l3mdev(sk);
  131. /* NOTE: using tproxy and redirecting skbs to a proxy
  132. * on a different listener port breaks the assumption
  133. * that the listener socket's icsk_bind_hash is the same
  134. * as that of the child socket. We have to look up or
  135. * create a new bind bucket for the child here. */
  136. inet_bind_bucket_for_each(tb, &head->chain) {
  137. if (net_eq(ib_net(tb), sock_net(sk)) &&
  138. tb->l3mdev == l3mdev && tb->port == port)
  139. break;
  140. }
  141. if (!tb) {
  142. tb = inet_bind_bucket_create(table->bind_bucket_cachep,
  143. sock_net(sk), head, port,
  144. l3mdev);
  145. if (!tb) {
  146. spin_unlock(&head->lock);
  147. return -ENOMEM;
  148. }
  149. }
  150. }
  151. inet_bind_hash(child, tb, port);
  152. spin_unlock(&head->lock);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(__inet_inherit_port);
  156. static struct inet_listen_hashbucket *
  157. inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
  158. {
  159. u32 hash;
  160. #if IS_ENABLED(CONFIG_IPV6)
  161. if (sk->sk_family == AF_INET6)
  162. hash = ipv6_portaddr_hash(sock_net(sk),
  163. &sk->sk_v6_rcv_saddr,
  164. inet_sk(sk)->inet_num);
  165. else
  166. #endif
  167. hash = ipv4_portaddr_hash(sock_net(sk),
  168. inet_sk(sk)->inet_rcv_saddr,
  169. inet_sk(sk)->inet_num);
  170. return inet_lhash2_bucket(h, hash);
  171. }
  172. static void inet_hash2(struct inet_hashinfo *h, struct sock *sk)
  173. {
  174. struct inet_listen_hashbucket *ilb2;
  175. if (!h->lhash2)
  176. return;
  177. ilb2 = inet_lhash2_bucket_sk(h, sk);
  178. spin_lock(&ilb2->lock);
  179. if (sk->sk_reuseport && sk->sk_family == AF_INET6)
  180. hlist_add_tail_rcu(&inet_csk(sk)->icsk_listen_portaddr_node,
  181. &ilb2->head);
  182. else
  183. hlist_add_head_rcu(&inet_csk(sk)->icsk_listen_portaddr_node,
  184. &ilb2->head);
  185. ilb2->count++;
  186. spin_unlock(&ilb2->lock);
  187. }
  188. static void inet_unhash2(struct inet_hashinfo *h, struct sock *sk)
  189. {
  190. struct inet_listen_hashbucket *ilb2;
  191. if (!h->lhash2 ||
  192. WARN_ON_ONCE(hlist_unhashed(&inet_csk(sk)->icsk_listen_portaddr_node)))
  193. return;
  194. ilb2 = inet_lhash2_bucket_sk(h, sk);
  195. spin_lock(&ilb2->lock);
  196. hlist_del_init_rcu(&inet_csk(sk)->icsk_listen_portaddr_node);
  197. ilb2->count--;
  198. spin_unlock(&ilb2->lock);
  199. }
  200. static inline int compute_score(struct sock *sk, struct net *net,
  201. const unsigned short hnum, const __be32 daddr,
  202. const int dif, const int sdif, bool exact_dif)
  203. {
  204. int score = -1;
  205. if (net_eq(sock_net(sk), net) && sk->sk_num == hnum &&
  206. !ipv6_only_sock(sk)) {
  207. if (sk->sk_rcv_saddr != daddr)
  208. return -1;
  209. if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
  210. return -1;
  211. score = sk->sk_family == PF_INET ? 2 : 1;
  212. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  213. score++;
  214. }
  215. return score;
  216. }
  217. /*
  218. * Here are some nice properties to exploit here. The BSD API
  219. * does not allow a listening sock to specify the remote port nor the
  220. * remote address for the connection. So always assume those are both
  221. * wildcarded during the search since they can never be otherwise.
  222. */
  223. /* called with rcu_read_lock() : No refcount taken on the socket */
  224. static struct sock *inet_lhash2_lookup(struct net *net,
  225. struct inet_listen_hashbucket *ilb2,
  226. struct sk_buff *skb, int doff,
  227. const __be32 saddr, __be16 sport,
  228. const __be32 daddr, const unsigned short hnum,
  229. const int dif, const int sdif)
  230. {
  231. bool exact_dif = inet_exact_dif_match(net, skb);
  232. struct inet_connection_sock *icsk;
  233. struct sock *sk, *result = NULL;
  234. int score, hiscore = 0;
  235. u32 phash = 0;
  236. inet_lhash2_for_each_icsk_rcu(icsk, &ilb2->head) {
  237. sk = (struct sock *)icsk;
  238. score = compute_score(sk, net, hnum, daddr,
  239. dif, sdif, exact_dif);
  240. if (score > hiscore) {
  241. if (sk->sk_reuseport) {
  242. phash = inet_ehashfn(net, daddr, hnum,
  243. saddr, sport);
  244. result = reuseport_select_sock(sk, phash,
  245. skb, doff);
  246. if (result)
  247. return result;
  248. }
  249. result = sk;
  250. hiscore = score;
  251. }
  252. }
  253. return result;
  254. }
  255. struct sock *__inet_lookup_listener(struct net *net,
  256. struct inet_hashinfo *hashinfo,
  257. struct sk_buff *skb, int doff,
  258. const __be32 saddr, __be16 sport,
  259. const __be32 daddr, const unsigned short hnum,
  260. const int dif, const int sdif)
  261. {
  262. struct inet_listen_hashbucket *ilb2;
  263. struct sock *result = NULL;
  264. unsigned int hash2;
  265. hash2 = ipv4_portaddr_hash(net, daddr, hnum);
  266. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  267. result = inet_lhash2_lookup(net, ilb2, skb, doff,
  268. saddr, sport, daddr, hnum,
  269. dif, sdif);
  270. if (result)
  271. goto done;
  272. /* Lookup lhash2 with INADDR_ANY */
  273. hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
  274. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  275. result = inet_lhash2_lookup(net, ilb2, skb, doff,
  276. saddr, sport, htonl(INADDR_ANY), hnum,
  277. dif, sdif);
  278. done:
  279. if (IS_ERR(result))
  280. return NULL;
  281. return result;
  282. }
  283. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  284. /* All sockets share common refcount, but have different destructors */
  285. void sock_gen_put(struct sock *sk)
  286. {
  287. if (!refcount_dec_and_test(&sk->sk_refcnt))
  288. return;
  289. if (sk->sk_state == TCP_TIME_WAIT)
  290. inet_twsk_free(inet_twsk(sk));
  291. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  292. reqsk_free(inet_reqsk(sk));
  293. else
  294. sk_free(sk);
  295. }
  296. EXPORT_SYMBOL_GPL(sock_gen_put);
  297. void sock_edemux(struct sk_buff *skb)
  298. {
  299. sock_gen_put(skb->sk);
  300. }
  301. EXPORT_SYMBOL(sock_edemux);
  302. struct sock *__inet_lookup_established(struct net *net,
  303. struct inet_hashinfo *hashinfo,
  304. const __be32 saddr, const __be16 sport,
  305. const __be32 daddr, const u16 hnum,
  306. const int dif, const int sdif)
  307. {
  308. INET_ADDR_COOKIE(acookie, saddr, daddr);
  309. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  310. struct sock *sk;
  311. const struct hlist_nulls_node *node;
  312. /* Optimize here for direct hit, only listening connections can
  313. * have wildcards anyways.
  314. */
  315. unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
  316. unsigned int slot = hash & hashinfo->ehash_mask;
  317. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  318. begin:
  319. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  320. if (sk->sk_hash != hash)
  321. continue;
  322. if (likely(INET_MATCH(sk, net, acookie,
  323. saddr, daddr, ports, dif, sdif))) {
  324. if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
  325. goto out;
  326. if (unlikely(!INET_MATCH(sk, net, acookie,
  327. saddr, daddr, ports,
  328. dif, sdif))) {
  329. sock_gen_put(sk);
  330. goto begin;
  331. }
  332. goto found;
  333. }
  334. }
  335. /*
  336. * if the nulls value we got at the end of this lookup is
  337. * not the expected one, we must restart lookup.
  338. * We probably met an item that was moved to another chain.
  339. */
  340. if (get_nulls_value(node) != slot)
  341. goto begin;
  342. out:
  343. sk = NULL;
  344. found:
  345. return sk;
  346. }
  347. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  348. /* called with local bh disabled */
  349. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  350. struct sock *sk, __u16 lport,
  351. struct inet_timewait_sock **twp)
  352. {
  353. struct inet_hashinfo *hinfo = death_row->hashinfo;
  354. struct inet_sock *inet = inet_sk(sk);
  355. __be32 daddr = inet->inet_rcv_saddr;
  356. __be32 saddr = inet->inet_daddr;
  357. int dif = sk->sk_bound_dev_if;
  358. struct net *net = sock_net(sk);
  359. int sdif = l3mdev_master_ifindex_by_index(net, dif);
  360. INET_ADDR_COOKIE(acookie, saddr, daddr);
  361. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  362. unsigned int hash = inet_ehashfn(net, daddr, lport,
  363. saddr, inet->inet_dport);
  364. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  365. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  366. struct sock *sk2;
  367. const struct hlist_nulls_node *node;
  368. struct inet_timewait_sock *tw = NULL;
  369. spin_lock(lock);
  370. sk_nulls_for_each(sk2, node, &head->chain) {
  371. if (sk2->sk_hash != hash)
  372. continue;
  373. if (likely(INET_MATCH(sk2, net, acookie,
  374. saddr, daddr, ports, dif, sdif))) {
  375. if (sk2->sk_state == TCP_TIME_WAIT) {
  376. tw = inet_twsk(sk2);
  377. if (twsk_unique(sk, sk2, twp))
  378. break;
  379. }
  380. goto not_unique;
  381. }
  382. }
  383. /* Must record num and sport now. Otherwise we will see
  384. * in hash table socket with a funny identity.
  385. */
  386. inet->inet_num = lport;
  387. inet->inet_sport = htons(lport);
  388. sk->sk_hash = hash;
  389. WARN_ON(!sk_unhashed(sk));
  390. __sk_nulls_add_node_rcu(sk, &head->chain);
  391. if (tw) {
  392. sk_nulls_del_node_init_rcu((struct sock *)tw);
  393. __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
  394. }
  395. spin_unlock(lock);
  396. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  397. if (twp) {
  398. *twp = tw;
  399. } else if (tw) {
  400. /* Silly. Should hash-dance instead... */
  401. inet_twsk_deschedule_put(tw);
  402. }
  403. return 0;
  404. not_unique:
  405. spin_unlock(lock);
  406. return -EADDRNOTAVAIL;
  407. }
  408. static u32 inet_sk_port_offset(const struct sock *sk)
  409. {
  410. const struct inet_sock *inet = inet_sk(sk);
  411. return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
  412. inet->inet_daddr,
  413. inet->inet_dport);
  414. }
  415. /* insert a socket into ehash, and eventually remove another one
  416. * (The another one can be a SYN_RECV or TIMEWAIT
  417. */
  418. bool inet_ehash_insert(struct sock *sk, struct sock *osk)
  419. {
  420. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  421. struct hlist_nulls_head *list;
  422. struct inet_ehash_bucket *head;
  423. spinlock_t *lock;
  424. bool ret = true;
  425. WARN_ON_ONCE(!sk_unhashed(sk));
  426. sk->sk_hash = sk_ehashfn(sk);
  427. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  428. list = &head->chain;
  429. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  430. spin_lock(lock);
  431. if (osk) {
  432. WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
  433. ret = sk_nulls_del_node_init_rcu(osk);
  434. }
  435. if (ret)
  436. __sk_nulls_add_node_rcu(sk, list);
  437. spin_unlock(lock);
  438. return ret;
  439. }
  440. bool inet_ehash_nolisten(struct sock *sk, struct sock *osk)
  441. {
  442. bool ok = inet_ehash_insert(sk, osk);
  443. if (ok) {
  444. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  445. } else {
  446. percpu_counter_inc(sk->sk_prot->orphan_count);
  447. inet_sk_set_state(sk, TCP_CLOSE);
  448. sock_set_flag(sk, SOCK_DEAD);
  449. inet_csk_destroy_sock(sk);
  450. }
  451. return ok;
  452. }
  453. EXPORT_SYMBOL_GPL(inet_ehash_nolisten);
  454. static int inet_reuseport_add_sock(struct sock *sk,
  455. struct inet_listen_hashbucket *ilb)
  456. {
  457. struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
  458. const struct hlist_nulls_node *node;
  459. struct sock *sk2;
  460. kuid_t uid = sock_i_uid(sk);
  461. sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
  462. if (sk2 != sk &&
  463. sk2->sk_family == sk->sk_family &&
  464. ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
  465. sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
  466. inet_csk(sk2)->icsk_bind_hash == tb &&
  467. sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
  468. inet_rcv_saddr_equal(sk, sk2, false))
  469. return reuseport_add_sock(sk, sk2,
  470. inet_rcv_saddr_any(sk));
  471. }
  472. return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
  473. }
  474. int __inet_hash(struct sock *sk, struct sock *osk)
  475. {
  476. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  477. struct inet_listen_hashbucket *ilb;
  478. int err = 0;
  479. if (sk->sk_state != TCP_LISTEN) {
  480. inet_ehash_nolisten(sk, osk);
  481. return 0;
  482. }
  483. WARN_ON(!sk_unhashed(sk));
  484. ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  485. spin_lock(&ilb->lock);
  486. if (sk->sk_reuseport) {
  487. err = inet_reuseport_add_sock(sk, ilb);
  488. if (err)
  489. goto unlock;
  490. }
  491. if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
  492. sk->sk_family == AF_INET6)
  493. __sk_nulls_add_node_tail_rcu(sk, &ilb->nulls_head);
  494. else
  495. __sk_nulls_add_node_rcu(sk, &ilb->nulls_head);
  496. inet_hash2(hashinfo, sk);
  497. ilb->count++;
  498. sock_set_flag(sk, SOCK_RCU_FREE);
  499. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  500. unlock:
  501. spin_unlock(&ilb->lock);
  502. return err;
  503. }
  504. EXPORT_SYMBOL(__inet_hash);
  505. int inet_hash(struct sock *sk)
  506. {
  507. int err = 0;
  508. if (sk->sk_state != TCP_CLOSE) {
  509. local_bh_disable();
  510. err = __inet_hash(sk, NULL);
  511. local_bh_enable();
  512. }
  513. return err;
  514. }
  515. EXPORT_SYMBOL_GPL(inet_hash);
  516. void inet_unhash(struct sock *sk)
  517. {
  518. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  519. struct inet_listen_hashbucket *ilb = NULL;
  520. spinlock_t *lock;
  521. if (sk_unhashed(sk))
  522. return;
  523. if (sk->sk_state == TCP_LISTEN) {
  524. ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  525. lock = &ilb->lock;
  526. } else {
  527. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  528. }
  529. spin_lock_bh(lock);
  530. if (sk_unhashed(sk))
  531. goto unlock;
  532. if (rcu_access_pointer(sk->sk_reuseport_cb))
  533. reuseport_detach_sock(sk);
  534. if (ilb) {
  535. inet_unhash2(hashinfo, sk);
  536. ilb->count--;
  537. }
  538. __sk_nulls_del_node_init_rcu(sk);
  539. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  540. unlock:
  541. spin_unlock_bh(lock);
  542. }
  543. EXPORT_SYMBOL_GPL(inet_unhash);
  544. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  545. struct sock *sk, u32 port_offset,
  546. int (*check_established)(struct inet_timewait_death_row *,
  547. struct sock *, __u16, struct inet_timewait_sock **))
  548. {
  549. struct inet_hashinfo *hinfo = death_row->hashinfo;
  550. struct inet_timewait_sock *tw = NULL;
  551. struct inet_bind_hashbucket *head;
  552. int port = inet_sk(sk)->inet_num;
  553. struct net *net = sock_net(sk);
  554. struct inet_bind_bucket *tb;
  555. u32 remaining, offset;
  556. int ret, i, low, high;
  557. static u32 hint;
  558. int l3mdev;
  559. if (port) {
  560. head = &hinfo->bhash[inet_bhashfn(net, port,
  561. hinfo->bhash_size)];
  562. tb = inet_csk(sk)->icsk_bind_hash;
  563. spin_lock_bh(&head->lock);
  564. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  565. inet_ehash_nolisten(sk, NULL);
  566. spin_unlock_bh(&head->lock);
  567. return 0;
  568. }
  569. spin_unlock(&head->lock);
  570. /* No definite answer... Walk to established hash table */
  571. ret = check_established(death_row, sk, port, NULL);
  572. local_bh_enable();
  573. return ret;
  574. }
  575. l3mdev = inet_sk_bound_l3mdev(sk);
  576. inet_get_local_port_range(net, &low, &high);
  577. high++; /* [32768, 60999] -> [32768, 61000[ */
  578. remaining = high - low;
  579. if (likely(remaining > 1))
  580. remaining &= ~1U;
  581. offset = (hint + port_offset) % remaining;
  582. /* In first pass we try ports of @low parity.
  583. * inet_csk_get_port() does the opposite choice.
  584. */
  585. offset &= ~1U;
  586. other_parity_scan:
  587. port = low + offset;
  588. for (i = 0; i < remaining; i += 2, port += 2) {
  589. if (unlikely(port >= high))
  590. port -= remaining;
  591. if (inet_is_local_reserved_port(net, port))
  592. continue;
  593. head = &hinfo->bhash[inet_bhashfn(net, port,
  594. hinfo->bhash_size)];
  595. spin_lock_bh(&head->lock);
  596. /* Does not bother with rcv_saddr checks, because
  597. * the established check is already unique enough.
  598. */
  599. inet_bind_bucket_for_each(tb, &head->chain) {
  600. if (net_eq(ib_net(tb), net) && tb->l3mdev == l3mdev &&
  601. tb->port == port) {
  602. if (tb->fastreuse >= 0 ||
  603. tb->fastreuseport >= 0)
  604. goto next_port;
  605. WARN_ON(hlist_empty(&tb->owners));
  606. if (!check_established(death_row, sk,
  607. port, &tw))
  608. goto ok;
  609. goto next_port;
  610. }
  611. }
  612. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  613. net, head, port, l3mdev);
  614. if (!tb) {
  615. spin_unlock_bh(&head->lock);
  616. return -ENOMEM;
  617. }
  618. tb->fastreuse = -1;
  619. tb->fastreuseport = -1;
  620. goto ok;
  621. next_port:
  622. spin_unlock_bh(&head->lock);
  623. cond_resched();
  624. }
  625. offset++;
  626. if ((offset & 1) && remaining > 1)
  627. goto other_parity_scan;
  628. return -EADDRNOTAVAIL;
  629. ok:
  630. hint += i + 2;
  631. /* Head lock still held and bh's disabled */
  632. inet_bind_hash(sk, tb, port);
  633. if (sk_unhashed(sk)) {
  634. inet_sk(sk)->inet_sport = htons(port);
  635. inet_ehash_nolisten(sk, (struct sock *)tw);
  636. }
  637. if (tw)
  638. inet_twsk_bind_unhash(tw, hinfo);
  639. spin_unlock(&head->lock);
  640. if (tw)
  641. inet_twsk_deschedule_put(tw);
  642. local_bh_enable();
  643. return 0;
  644. }
  645. /*
  646. * Bind a port for a connect operation and hash it.
  647. */
  648. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  649. struct sock *sk)
  650. {
  651. u32 port_offset = 0;
  652. if (!inet_sk(sk)->inet_num)
  653. port_offset = inet_sk_port_offset(sk);
  654. return __inet_hash_connect(death_row, sk, port_offset,
  655. __inet_check_established);
  656. }
  657. EXPORT_SYMBOL_GPL(inet_hash_connect);
  658. void inet_hashinfo_init(struct inet_hashinfo *h)
  659. {
  660. int i;
  661. for (i = 0; i < INET_LHTABLE_SIZE; i++) {
  662. spin_lock_init(&h->listening_hash[i].lock);
  663. INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].nulls_head,
  664. i + LISTENING_NULLS_BASE);
  665. h->listening_hash[i].count = 0;
  666. }
  667. h->lhash2 = NULL;
  668. }
  669. EXPORT_SYMBOL_GPL(inet_hashinfo_init);
  670. static void init_hashinfo_lhash2(struct inet_hashinfo *h)
  671. {
  672. int i;
  673. for (i = 0; i <= h->lhash2_mask; i++) {
  674. spin_lock_init(&h->lhash2[i].lock);
  675. INIT_HLIST_HEAD(&h->lhash2[i].head);
  676. h->lhash2[i].count = 0;
  677. }
  678. }
  679. void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
  680. unsigned long numentries, int scale,
  681. unsigned long low_limit,
  682. unsigned long high_limit)
  683. {
  684. h->lhash2 = alloc_large_system_hash(name,
  685. sizeof(*h->lhash2),
  686. numentries,
  687. scale,
  688. 0,
  689. NULL,
  690. &h->lhash2_mask,
  691. low_limit,
  692. high_limit);
  693. init_hashinfo_lhash2(h);
  694. }
  695. int inet_hashinfo2_init_mod(struct inet_hashinfo *h)
  696. {
  697. h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL);
  698. if (!h->lhash2)
  699. return -ENOMEM;
  700. h->lhash2_mask = INET_LHTABLE_SIZE - 1;
  701. /* INET_LHTABLE_SIZE must be a power of 2 */
  702. BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
  703. init_hashinfo_lhash2(h);
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod);
  707. int inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
  708. {
  709. unsigned int locksz = sizeof(spinlock_t);
  710. unsigned int i, nblocks = 1;
  711. if (locksz != 0) {
  712. /* allocate 2 cache lines or at least one spinlock per cpu */
  713. nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U);
  714. nblocks = roundup_pow_of_two(nblocks * num_possible_cpus());
  715. /* no more locks than number of hash buckets */
  716. nblocks = min(nblocks, hashinfo->ehash_mask + 1);
  717. hashinfo->ehash_locks = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
  718. if (!hashinfo->ehash_locks)
  719. return -ENOMEM;
  720. for (i = 0; i < nblocks; i++)
  721. spin_lock_init(&hashinfo->ehash_locks[i]);
  722. }
  723. hashinfo->ehash_locks_mask = nblocks - 1;
  724. return 0;
  725. }
  726. EXPORT_SYMBOL_GPL(inet_ehash_locks_alloc);