inet_hashtables.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/config.h>
  16. #include <linux/module.h>
  17. #include <linux/random.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/wait.h>
  21. #include <net/inet_connection_sock.h>
  22. #include <net/inet_hashtables.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(kmem_cache_t *cachep,
  29. struct inet_bind_hashbucket *head,
  30. const unsigned short snum)
  31. {
  32. struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, SLAB_ATOMIC);
  33. if (tb != NULL) {
  34. tb->port = snum;
  35. tb->fastreuse = 0;
  36. INIT_HLIST_HEAD(&tb->owners);
  37. hlist_add_head(&tb->node, &head->chain);
  38. }
  39. return tb;
  40. }
  41. /*
  42. * Caller must hold hashbucket lock for this tb with local BH disabled
  43. */
  44. void inet_bind_bucket_destroy(kmem_cache_t *cachep, struct inet_bind_bucket *tb)
  45. {
  46. if (hlist_empty(&tb->owners)) {
  47. __hlist_del(&tb->node);
  48. kmem_cache_free(cachep, tb);
  49. }
  50. }
  51. void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
  52. const unsigned short snum)
  53. {
  54. inet_sk(sk)->num = snum;
  55. sk_add_bind_node(sk, &tb->owners);
  56. inet_csk(sk)->icsk_bind_hash = tb;
  57. }
  58. /*
  59. * Get rid of any references to a local port held by the given sock.
  60. */
  61. static void __inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
  62. {
  63. const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size);
  64. struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
  65. struct inet_bind_bucket *tb;
  66. spin_lock(&head->lock);
  67. tb = inet_csk(sk)->icsk_bind_hash;
  68. __sk_del_bind_node(sk);
  69. inet_csk(sk)->icsk_bind_hash = NULL;
  70. inet_sk(sk)->num = 0;
  71. inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
  72. spin_unlock(&head->lock);
  73. }
  74. void inet_put_port(struct inet_hashinfo *hashinfo, struct sock *sk)
  75. {
  76. local_bh_disable();
  77. __inet_put_port(hashinfo, sk);
  78. local_bh_enable();
  79. }
  80. EXPORT_SYMBOL(inet_put_port);
  81. /*
  82. * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
  83. * Look, when several writers sleep and reader wakes them up, all but one
  84. * immediately hit write lock and grab all the cpus. Exclusive sleep solves
  85. * this, _but_ remember, it adds useless work on UP machines (wake up each
  86. * exclusive lock release). It should be ifdefed really.
  87. */
  88. void inet_listen_wlock(struct inet_hashinfo *hashinfo)
  89. {
  90. write_lock(&hashinfo->lhash_lock);
  91. if (atomic_read(&hashinfo->lhash_users)) {
  92. DEFINE_WAIT(wait);
  93. for (;;) {
  94. prepare_to_wait_exclusive(&hashinfo->lhash_wait,
  95. &wait, TASK_UNINTERRUPTIBLE);
  96. if (!atomic_read(&hashinfo->lhash_users))
  97. break;
  98. write_unlock_bh(&hashinfo->lhash_lock);
  99. schedule();
  100. write_lock_bh(&hashinfo->lhash_lock);
  101. }
  102. finish_wait(&hashinfo->lhash_wait, &wait);
  103. }
  104. }
  105. EXPORT_SYMBOL(inet_listen_wlock);
  106. /*
  107. * Don't inline this cruft. Here are some nice properties to exploit here. The
  108. * BSD API does not allow a listening sock to specify the remote port nor the
  109. * remote address for the connection. So always assume those are both
  110. * wildcarded during the search since they can never be otherwise.
  111. */
  112. struct sock *__inet_lookup_listener(const struct hlist_head *head, const u32 daddr,
  113. const unsigned short hnum, const int dif)
  114. {
  115. struct sock *result = NULL, *sk;
  116. const struct hlist_node *node;
  117. int hiscore = -1;
  118. sk_for_each(sk, node, head) {
  119. const struct inet_sock *inet = inet_sk(sk);
  120. if (inet->num == hnum && !ipv6_only_sock(sk)) {
  121. const __u32 rcv_saddr = inet->rcv_saddr;
  122. int score = sk->sk_family == PF_INET ? 1 : 0;
  123. if (rcv_saddr) {
  124. if (rcv_saddr != daddr)
  125. continue;
  126. score += 2;
  127. }
  128. if (sk->sk_bound_dev_if) {
  129. if (sk->sk_bound_dev_if != dif)
  130. continue;
  131. score += 2;
  132. }
  133. if (score == 5)
  134. return sk;
  135. if (score > hiscore) {
  136. hiscore = score;
  137. result = sk;
  138. }
  139. }
  140. }
  141. return result;
  142. }
  143. EXPORT_SYMBOL_GPL(__inet_lookup_listener);
  144. /* called with local bh disabled */
  145. static int __inet_check_established(struct inet_timewait_death_row *death_row,
  146. struct sock *sk, __u16 lport,
  147. struct inet_timewait_sock **twp)
  148. {
  149. struct inet_hashinfo *hinfo = death_row->hashinfo;
  150. struct inet_sock *inet = inet_sk(sk);
  151. u32 daddr = inet->rcv_saddr;
  152. u32 saddr = inet->daddr;
  153. int dif = sk->sk_bound_dev_if;
  154. INET_ADDR_COOKIE(acookie, saddr, daddr)
  155. const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
  156. unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
  157. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  158. struct sock *sk2;
  159. const struct hlist_node *node;
  160. struct inet_timewait_sock *tw;
  161. prefetch(head->chain.first);
  162. write_lock(&head->lock);
  163. /* Check TIME-WAIT sockets first. */
  164. sk_for_each(sk2, node, &(head + hinfo->ehash_size)->chain) {
  165. tw = inet_twsk(sk2);
  166. if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif)) {
  167. if (twsk_unique(sk, sk2, twp))
  168. goto unique;
  169. else
  170. goto not_unique;
  171. }
  172. }
  173. tw = NULL;
  174. /* And established part... */
  175. sk_for_each(sk2, node, &head->chain) {
  176. if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
  177. goto not_unique;
  178. }
  179. unique:
  180. /* Must record num and sport now. Otherwise we will see
  181. * in hash table socket with a funny identity. */
  182. inet->num = lport;
  183. inet->sport = htons(lport);
  184. sk->sk_hash = hash;
  185. BUG_TRAP(sk_unhashed(sk));
  186. __sk_add_node(sk, &head->chain);
  187. sock_prot_inc_use(sk->sk_prot);
  188. write_unlock(&head->lock);
  189. if (twp) {
  190. *twp = tw;
  191. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  192. } else if (tw) {
  193. /* Silly. Should hash-dance instead... */
  194. inet_twsk_deschedule(tw, death_row);
  195. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  196. inet_twsk_put(tw);
  197. }
  198. return 0;
  199. not_unique:
  200. write_unlock(&head->lock);
  201. return -EADDRNOTAVAIL;
  202. }
  203. static inline u32 inet_sk_port_offset(const struct sock *sk)
  204. {
  205. const struct inet_sock *inet = inet_sk(sk);
  206. return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
  207. inet->dport);
  208. }
  209. /*
  210. * Bind a port for a connect operation and hash it.
  211. */
  212. int inet_hash_connect(struct inet_timewait_death_row *death_row,
  213. struct sock *sk)
  214. {
  215. struct inet_hashinfo *hinfo = death_row->hashinfo;
  216. const unsigned short snum = inet_sk(sk)->num;
  217. struct inet_bind_hashbucket *head;
  218. struct inet_bind_bucket *tb;
  219. int ret;
  220. if (!snum) {
  221. int low = sysctl_local_port_range[0];
  222. int high = sysctl_local_port_range[1];
  223. int range = high - low;
  224. int i;
  225. int port;
  226. static u32 hint;
  227. u32 offset = hint + inet_sk_port_offset(sk);
  228. struct hlist_node *node;
  229. struct inet_timewait_sock *tw = NULL;
  230. local_bh_disable();
  231. for (i = 1; i <= range; i++) {
  232. port = low + (i + offset) % range;
  233. head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
  234. spin_lock(&head->lock);
  235. /* Does not bother with rcv_saddr checks,
  236. * because the established check is already
  237. * unique enough.
  238. */
  239. inet_bind_bucket_for_each(tb, node, &head->chain) {
  240. if (tb->port == port) {
  241. BUG_TRAP(!hlist_empty(&tb->owners));
  242. if (tb->fastreuse >= 0)
  243. goto next_port;
  244. if (!__inet_check_established(death_row,
  245. sk, port,
  246. &tw))
  247. goto ok;
  248. goto next_port;
  249. }
  250. }
  251. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep, head, port);
  252. if (!tb) {
  253. spin_unlock(&head->lock);
  254. break;
  255. }
  256. tb->fastreuse = -1;
  257. goto ok;
  258. next_port:
  259. spin_unlock(&head->lock);
  260. }
  261. local_bh_enable();
  262. return -EADDRNOTAVAIL;
  263. ok:
  264. hint += i;
  265. /* Head lock still held and bh's disabled */
  266. inet_bind_hash(sk, tb, port);
  267. if (sk_unhashed(sk)) {
  268. inet_sk(sk)->sport = htons(port);
  269. __inet_hash(hinfo, sk, 0);
  270. }
  271. spin_unlock(&head->lock);
  272. if (tw) {
  273. inet_twsk_deschedule(tw, death_row);
  274. inet_twsk_put(tw);
  275. }
  276. ret = 0;
  277. goto out;
  278. }
  279. head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
  280. tb = inet_csk(sk)->icsk_bind_hash;
  281. spin_lock_bh(&head->lock);
  282. if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
  283. __inet_hash(hinfo, sk, 0);
  284. spin_unlock_bh(&head->lock);
  285. return 0;
  286. } else {
  287. spin_unlock(&head->lock);
  288. /* No definite answer... Walk to established hash table */
  289. ret = __inet_check_established(death_row, sk, snum, NULL);
  290. out:
  291. local_bh_enable();
  292. return ret;
  293. }
  294. }
  295. EXPORT_SYMBOL_GPL(inet_hash_connect);