inet_hashtables.c 8.8 KB

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