inet_hashtables.c 24 KB

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