inet_hashtables.c 15 KB

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