inet_hashtables.c 19 KB

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