inet_hashtables.c 15 KB

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