inet_hashtables.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 <net/inet_connection_sock.h>
  21. #include <net/inet_hashtables.h>
  22. #include <net/secure_seq.h>
  23. #include <net/ip.h>
  24. /*
  25. * Allocate and initialize a new local port bind bucket.
  26. * The bindhash mutex for snum's hash chain must be held here.
  27. */
  28. struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
  29. struct net *net,
  30. struct inet_bind_hashbucket *head,
  31. const unsigned short snum)
  32. {
  33. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
  34. if (tb != NULL) {
  35. write_pnet(&tb->ib_net, hold_net(net));
  36. tb->port = snum;
  37. tb->fastreuse = 0;
  38. tb->fastreuseport = 0;
  39. tb->num_owners = 0;
  40. INIT_HLIST_HEAD(&tb->owners);
  41. hlist_add_head(&tb->node, &head->chain);
  42. }
  43. return tb;
  44. }
  45. /*
  46. * Caller must hold hashbucket lock for this tb with local BH disabled
  47. */
  48. void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
  49. {
  50. if (hlist_empty(&tb->owners)) {
  51. __hlist_del(&tb->node);
  52. release_net(ib_net(tb));
  53. kmem_cache_free(cachep, tb);
  54. }
  55. }
  56. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  57. const unsigned short snum)
  58. {
  59. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  60. atomic_inc(&hashinfo->bsockets);
  61. inet_sk(sk)->inet_num = snum;
  62. sk_add_bind_node(sk, &tb->owners);
  63. tb->num_owners++;
  64. inet_csk(sk)->icsk_bind_hash = tb;
  65. }
  66. /*
  67. * Get rid of any references to a local port held by the given sock.
  68. */
  69. static void __inet_put_port(struct sock *sk)
  70. {
  71. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  72. const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
  73. hashinfo->bhash_size);
  74. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  75. struct inet_bind_bucket *tb;
  76. atomic_dec(&hashinfo->bsockets);
  77. spin_lock(&head->lock);
  78. tb = inet_csk(sk)->icsk_bind_hash;
  79. __sk_del_bind_node(sk);
  80. tb->num_owners--;
  81. inet_csk(sk)->icsk_bind_hash = NULL;
  82. inet_sk(sk)->inet_num = 0;
  83. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  84. spin_unlock(&head->lock);
  85. }
  86. void inet_put_port(struct sock *sk)
  87. {
  88. local_bh_disable();
  89. __inet_put_port(sk);
  90. local_bh_enable();
  91. }
  92. EXPORT_SYMBOL(inet_put_port);
  93. int __inet_inherit_port(struct sock *sk, struct sock *child)
  94. {
  95. struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
  96. unsigned short port = inet_sk(child)->inet_num;
  97. const int bhash = inet_bhashfn(sock_net(sk), port,
  98. table->bhash_size);
  99. struct inet_bind_hashbucket *head = &table->bhash[bhash];
  100. struct inet_bind_bucket *tb;
  101. spin_lock(&head->lock);
  102. tb = inet_csk(sk)->icsk_bind_hash;
  103. if (tb->port != port) {
  104. /* NOTE: using tproxy and redirecting skbs to a proxy
  105. * on a different listener port breaks the assumption
  106. * that the listener socket's icsk_bind_hash is the same
  107. * as that of the child socket. We have to look up or
  108. * create a new bind bucket for the child here. */
  109. struct hlist_node *node;
  110. inet_bind_bucket_for_each(tb, node, &head->chain) {
  111. if (net_eq(ib_net(tb), sock_net(sk)) &&
  112. tb->port == port)
  113. break;
  114. }
  115. if (!node) {
  116. tb = inet_bind_bucket_create(table->bind_bucket_cachep,
  117. sock_net(sk), head, port);
  118. if (!tb) {
  119. spin_unlock(&head->lock);
  120. return -ENOMEM;
  121. }
  122. }
  123. }
  124. inet_bind_hash(child, tb, port);
  125. spin_unlock(&head->lock);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(__inet_inherit_port);
  129. static inline int compute_score(struct sock *sk, struct net *net,
  130. const unsigned short hnum, const __be32 daddr,
  131. const int dif)
  132. {
  133. int score = -1;
  134. struct inet_sock *inet = inet_sk(sk);
  135. if (net_eq(sock_net(sk), net) && inet->inet_num == hnum &&
  136. !ipv6_only_sock(sk)) {
  137. __be32 rcv_saddr = inet->inet_rcv_saddr;
  138. score = sk->sk_family == PF_INET ? 2 : 1;
  139. if (rcv_saddr) {
  140. if (rcv_saddr != daddr)
  141. return -1;
  142. score += 4;
  143. }
  144. if (sk->sk_bound_dev_if) {
  145. if (sk->sk_bound_dev_if != dif)
  146. return -1;
  147. score += 4;
  148. }
  149. }
  150. return score;
  151. }
  152. /*
  153. * Don't inline this cruft. Here are some nice properties to exploit here. The
  154. * BSD API does not allow a listening sock to specify the remote port nor the
  155. * remote address for the connection. So always assume those are both
  156. * wildcarded during the search since they can never be otherwise.
  157. */
  158. struct sock *__inet_lookup_listener(struct net *net,
  159. struct inet_hashinfo *hashinfo,
  160. const __be32 saddr, __be16 sport,
  161. const __be32 daddr, const unsigned short hnum,
  162. const int dif)
  163. {
  164. struct sock *sk, *result;
  165. struct hlist_nulls_node *node;
  166. unsigned int hash = inet_lhashfn(net, hnum);
  167. struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
  168. int score, hiscore, matches = 0, reuseport = 0;
  169. u32 phash = 0;
  170. rcu_read_lock();
  171. begin:
  172. result = NULL;
  173. hiscore = 0;
  174. sk_nulls_for_each_rcu(sk, node, &ilb->head) {
  175. score = compute_score(sk, net, hnum, daddr, dif);
  176. if (score > hiscore) {
  177. result = sk;
  178. hiscore = score;
  179. reuseport = sk->sk_reuseport;
  180. if (reuseport) {
  181. phash = inet_ehashfn(net, daddr, hnum,
  182. saddr, sport);
  183. matches = 1;
  184. }
  185. } else if (score == hiscore && reuseport) {
  186. matches++;
  187. if (((u64)phash * matches) >> 32 == 0)
  188. result = sk;
  189. phash = next_pseudo_random32(phash);
  190. }
  191. }
  192. /*
  193. * if the nulls value we got at the end of this lookup is
  194. * not the expected one, we must restart lookup.
  195. * We probably met an item that was moved to another chain.
  196. */
  197. if (get_nulls_value(node) != hash + LISTENING_NULLS_BASE)
  198. goto begin;
  199. if (result) {
  200. if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
  201. result = NULL;
  202. else if (unlikely(compute_score(result, net, hnum, daddr,
  203. dif) < hiscore)) {
  204. sock_put(result);
  205. goto begin;
  206. }
  207. }
  208. rcu_read_unlock();
  209. return result;
  210. }
  211. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  212. struct sock *__inet_lookup_established(struct net *net,
  213. struct inet_hashinfo *hashinfo,
  214. const __be32 saddr, const __be16 sport,
  215. const __be32 daddr, const u16 hnum,
  216. const int dif)
  217. {
  218. INET_ADDR_COOKIE(acookie, saddr, daddr)
  219. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  220. struct sock *sk;
  221. const struct hlist_nulls_node *node;
  222. /* Optimize here for direct hit, only listening connections can
  223. * have wildcards anyways.
  224. */
  225. unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
  226. unsigned int slot = hash & hashinfo->ehash_mask;
  227. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  228. rcu_read_lock();
  229. begin:
  230. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  231. if (sk->sk_hash != hash)
  232. continue;
  233. if (likely(INET_MATCH(sk, net, acookie,
  234. saddr, daddr, ports, dif))) {
  235. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
  236. goto begintw;
  237. if (unlikely(!INET_MATCH(sk, net, acookie,
  238. saddr, daddr, ports, dif))) {
  239. sock_put(sk);
  240. goto begin;
  241. }
  242. goto out;
  243. }
  244. }
  245. /*
  246. * if the nulls value we got at the end of this lookup is
  247. * not the expected one, we must restart lookup.
  248. * We probably met an item that was moved to another chain.
  249. */
  250. if (get_nulls_value(node) != slot)
  251. goto begin;
  252. begintw:
  253. /* Must check for a TIME_WAIT'er before going to listener hash. */
  254. sk_nulls_for_each_rcu(sk, node, &head->twchain) {
  255. if (sk->sk_hash != hash)
  256. continue;
  257. if (likely(INET_TW_MATCH(sk, net, acookie,
  258. saddr, daddr, ports,
  259. dif))) {
  260. if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
  261. sk = NULL;
  262. goto out;
  263. }
  264. if (unlikely(!INET_TW_MATCH(sk, net, acookie,
  265. saddr, daddr, ports,
  266. dif))) {
  267. sock_put(sk);
  268. goto begintw;
  269. }
  270. goto out;
  271. }
  272. }
  273. /*
  274. * if the nulls value we got at the end of this lookup is
  275. * not the expected one, we must restart lookup.
  276. * We probably met an item that was moved to another chain.
  277. */
  278. if (get_nulls_value(node) != slot)
  279. goto begintw;
  280. sk = NULL;
  281. out:
  282. rcu_read_unlock();
  283. return sk;
  284. }
  285. EXPORT_SYMBOL_GPL(__inet_lookup_established);
  286. /* called with local bh disabled */
  287. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  288. struct sock *sk, __u16 lport,
  289. struct inet_timewait_sock **twp)
  290. {
  291. struct inet_hashinfo *hinfo = death_row->hashinfo;
  292. struct inet_sock *inet = inet_sk(sk);
  293. __be32 daddr = inet->inet_rcv_saddr;
  294. __be32 saddr = inet->inet_daddr;
  295. int dif = sk->sk_bound_dev_if;
  296. INET_ADDR_COOKIE(acookie, saddr, daddr)
  297. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  298. struct net *net = sock_net(sk);
  299. unsigned int hash = inet_ehashfn(net, daddr, lport,
  300. saddr, inet->inet_dport);
  301. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  302. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  303. struct sock *sk2;
  304. const struct hlist_nulls_node *node;
  305. struct inet_timewait_sock *tw;
  306. int twrefcnt = 0;
  307. spin_lock(lock);
  308. /* Check TIME-WAIT sockets first. */
  309. sk_nulls_for_each(sk2, node, &head->twchain) {
  310. if (sk2->sk_hash != hash)
  311. continue;
  312. if (likely(INET_TW_MATCH(sk2, net, acookie,
  313. saddr, daddr, ports, dif))) {
  314. tw = inet_twsk(sk2);
  315. if (twsk_unique(sk, sk2, twp))
  316. goto unique;
  317. else
  318. goto not_unique;
  319. }
  320. }
  321. tw = NULL;
  322. /* And established part... */
  323. sk_nulls_for_each(sk2, node, &head->chain) {
  324. if (sk2->sk_hash != hash)
  325. continue;
  326. if (likely(INET_MATCH(sk2, net, acookie,
  327. saddr, daddr, ports, dif)))
  328. goto not_unique;
  329. }
  330. unique:
  331. /* Must record num and sport now. Otherwise we will see
  332. * in hash table socket with a funny identity. */
  333. inet->inet_num = lport;
  334. inet->inet_sport = htons(lport);
  335. sk->sk_hash = hash;
  336. WARN_ON(!sk_unhashed(sk));
  337. __sk_nulls_add_node_rcu(sk, &head->chain);
  338. if (tw) {
  339. twrefcnt = inet_twsk_unhash(tw);
  340. NET_INC_STATS_BH(net, LINUX_MIB_TIMEWAITRECYCLED);
  341. }
  342. spin_unlock(lock);
  343. if (twrefcnt)
  344. inet_twsk_put(tw);
  345. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  346. if (twp) {
  347. *twp = tw;
  348. } else if (tw) {
  349. /* Silly. Should hash-dance instead... */
  350. inet_twsk_deschedule(tw, death_row);
  351. inet_twsk_put(tw);
  352. }
  353. return 0;
  354. not_unique:
  355. spin_unlock(lock);
  356. return -EADDRNOTAVAIL;
  357. }
  358. static inline u32 inet_sk_port_offset(const struct sock *sk)
  359. {
  360. const struct inet_sock *inet = inet_sk(sk);
  361. return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
  362. inet->inet_daddr,
  363. inet->inet_dport);
  364. }
  365. int __inet_hash_nolisten(struct sock *sk, struct inet_timewait_sock *tw)
  366. {
  367. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  368. struct hlist_nulls_head *list;
  369. spinlock_t *lock;
  370. struct inet_ehash_bucket *head;
  371. int twrefcnt = 0;
  372. WARN_ON(!sk_unhashed(sk));
  373. sk->sk_hash = inet_sk_ehashfn(sk);
  374. head = inet_ehash_bucket(hashinfo, sk->sk_hash);
  375. list = &head->chain;
  376. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  377. spin_lock(lock);
  378. __sk_nulls_add_node_rcu(sk, list);
  379. if (tw) {
  380. WARN_ON(sk->sk_hash != tw->tw_hash);
  381. twrefcnt = inet_twsk_unhash(tw);
  382. }
  383. spin_unlock(lock);
  384. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  385. return twrefcnt;
  386. }
  387. EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
  388. static void __inet_hash(struct sock *sk)
  389. {
  390. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  391. struct inet_listen_hashbucket *ilb;
  392. if (sk->sk_state != TCP_LISTEN) {
  393. __inet_hash_nolisten(sk, NULL);
  394. return;
  395. }
  396. WARN_ON(!sk_unhashed(sk));
  397. ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  398. spin_lock(&ilb->lock);
  399. __sk_nulls_add_node_rcu(sk, &ilb->head);
  400. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  401. spin_unlock(&ilb->lock);
  402. }
  403. void inet_hash(struct sock *sk)
  404. {
  405. if (sk->sk_state != TCP_CLOSE) {
  406. local_bh_disable();
  407. __inet_hash(sk);
  408. local_bh_enable();
  409. }
  410. }
  411. EXPORT_SYMBOL_GPL(inet_hash);
  412. void inet_unhash(struct sock *sk)
  413. {
  414. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  415. spinlock_t *lock;
  416. int done;
  417. if (sk_unhashed(sk))
  418. return;
  419. if (sk->sk_state == TCP_LISTEN)
  420. lock = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)].lock;
  421. else
  422. lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
  423. spin_lock_bh(lock);
  424. done =__sk_nulls_del_node_init_rcu(sk);
  425. if (done)
  426. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  427. spin_unlock_bh(lock);
  428. }
  429. EXPORT_SYMBOL_GPL(inet_unhash);
  430. int __inet_hash_connect(struct inet_timewait_death_row *death_row,
  431. struct sock *sk, u32 port_offset,
  432. int (*check_established)(struct inet_timewait_death_row *,
  433. struct sock *, __u16, struct inet_timewait_sock **),
  434. int (*hash)(struct sock *sk, struct inet_timewait_sock *twp))
  435. {
  436. struct inet_hashinfo *hinfo = death_row->hashinfo;
  437. const unsigned short snum = inet_sk(sk)->inet_num;
  438. struct inet_bind_hashbucket *head;
  439. struct inet_bind_bucket *tb;
  440. int ret;
  441. struct net *net = sock_net(sk);
  442. int twrefcnt = 1;
  443. if (!snum) {
  444. int i, remaining, low, high, port;
  445. static u32 hint;
  446. u32 offset = hint + port_offset;
  447. struct hlist_node *node;
  448. struct inet_timewait_sock *tw = NULL;
  449. inet_get_local_port_range(&low, &high);
  450. remaining = (high - low) + 1;
  451. local_bh_disable();
  452. for (i = 1; i <= remaining; i++) {
  453. port = low + (i + offset) % remaining;
  454. if (inet_is_reserved_local_port(port))
  455. continue;
  456. head = &hinfo->bhash[inet_bhashfn(net, port,
  457. hinfo->bhash_size)];
  458. spin_lock(&head->lock);
  459. /* Does not bother with rcv_saddr checks,
  460. * because the established check is already
  461. * unique enough.
  462. */
  463. inet_bind_bucket_for_each(tb, node, &head->chain) {
  464. if (net_eq(ib_net(tb), net) &&
  465. tb->port == port) {
  466. if (tb->fastreuse >= 0 ||
  467. tb->fastreuseport >= 0)
  468. goto next_port;
  469. WARN_ON(hlist_empty(&tb->owners));
  470. if (!check_established(death_row, sk,
  471. port, &tw))
  472. goto ok;
  473. goto next_port;
  474. }
  475. }
  476. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  477. net, head, port);
  478. if (!tb) {
  479. spin_unlock(&head->lock);
  480. break;
  481. }
  482. tb->fastreuse = -1;
  483. tb->fastreuseport = -1;
  484. goto ok;
  485. next_port:
  486. spin_unlock(&head->lock);
  487. }
  488. local_bh_enable();
  489. return -EADDRNOTAVAIL;
  490. ok:
  491. hint += i;
  492. /* Head lock still held and bh's disabled */
  493. inet_bind_hash(sk, tb, port);
  494. if (sk_unhashed(sk)) {
  495. inet_sk(sk)->inet_sport = htons(port);
  496. twrefcnt += hash(sk, tw);
  497. }
  498. if (tw)
  499. twrefcnt += inet_twsk_bind_unhash(tw, hinfo);
  500. spin_unlock(&head->lock);
  501. if (tw) {
  502. inet_twsk_deschedule(tw, death_row);
  503. while (twrefcnt) {
  504. twrefcnt--;
  505. inet_twsk_put(tw);
  506. }
  507. }
  508. ret = 0;
  509. goto out;
  510. }
  511. head = &hinfo->bhash[inet_bhashfn(net, snum, hinfo->bhash_size)];
  512. tb = inet_csk(sk)->icsk_bind_hash;
  513. spin_lock_bh(&head->lock);
  514. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  515. hash(sk, NULL);
  516. spin_unlock_bh(&head->lock);
  517. return 0;
  518. } else {
  519. spin_unlock(&head->lock);
  520. /* No definite answer... Walk to established hash table */
  521. ret = check_established(death_row, sk, snum, NULL);
  522. out:
  523. local_bh_enable();
  524. return ret;
  525. }
  526. }
  527. /*
  528. * Bind a port for a connect operation and hash it.
  529. */
  530. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  531. struct sock *sk)
  532. {
  533. return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
  534. __inet_check_established, __inet_hash_nolisten);
  535. }
  536. EXPORT_SYMBOL_GPL(inet_hash_connect);
  537. void inet_hashinfo_init(struct inet_hashinfo *h)
  538. {
  539. int i;
  540. atomic_set(&h->bsockets, 0);
  541. for (i = 0; i < INET_LHTABLE_SIZE; i++) {
  542. spin_lock_init(&h->listening_hash[i].lock);
  543. INIT_HLIST_NULLS_HEAD(&h->listening_hash[i].head,
  544. i + LISTENING_NULLS_BASE);
  545. }
  546. }
  547. EXPORT_SYMBOL_GPL(inet_hashinfo_init);