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