inet_hashtables.c 14 KB

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