inet_hashtables.c 22 KB

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