inet_hashtables.c 18 KB

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