inet6_hashtables.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 INET6 transport hashtables
  7. *
  8. * Authors: Lotsa people, from code originally in tcp, generalised here
  9. * by Arnaldo Carvalho de Melo <acme@mandriva.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <linux/random.h>
  19. #include <net/inet_connection_sock.h>
  20. #include <net/inet_hashtables.h>
  21. #include <net/inet6_hashtables.h>
  22. #include <net/ip.h>
  23. struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
  24. const struct in6_addr *daddr,
  25. const unsigned short hnum, const int dif)
  26. {
  27. struct sock *sk;
  28. const struct hlist_node *node;
  29. struct sock *result = NULL;
  30. int score, hiscore = 0;
  31. read_lock(&hashinfo->lhash_lock);
  32. sk_for_each(sk, node, &hashinfo->listening_hash[inet_lhashfn(hnum)]) {
  33. if (inet_sk(sk)->num == hnum && sk->sk_family == PF_INET6) {
  34. const struct ipv6_pinfo *np = inet6_sk(sk);
  35. score = 1;
  36. if (!ipv6_addr_any(&np->rcv_saddr)) {
  37. if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
  38. continue;
  39. score++;
  40. }
  41. if (sk->sk_bound_dev_if) {
  42. if (sk->sk_bound_dev_if != dif)
  43. continue;
  44. score++;
  45. }
  46. if (score == 3) {
  47. result = sk;
  48. break;
  49. }
  50. if (score > hiscore) {
  51. hiscore = score;
  52. result = sk;
  53. }
  54. }
  55. }
  56. if (result)
  57. sock_hold(result);
  58. read_unlock(&hashinfo->lhash_lock);
  59. return result;
  60. }
  61. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  62. struct sock *inet6_lookup(struct inet_hashinfo *hashinfo,
  63. const struct in6_addr *saddr, const u16 sport,
  64. const struct in6_addr *daddr, const u16 dport,
  65. const int dif)
  66. {
  67. struct sock *sk;
  68. local_bh_disable();
  69. sk = __inet6_lookup(hashinfo, saddr, sport, daddr, ntohs(dport), dif);
  70. local_bh_enable();
  71. return sk;
  72. }
  73. EXPORT_SYMBOL_GPL(inet6_lookup);
  74. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  75. struct sock *sk, const __u16 lport,
  76. struct inet_timewait_sock **twp)
  77. {
  78. struct inet_hashinfo *hinfo = death_row->hashinfo;
  79. const struct inet_sock *inet = inet_sk(sk);
  80. const struct ipv6_pinfo *np = inet6_sk(sk);
  81. const struct in6_addr *daddr = &np->rcv_saddr;
  82. const struct in6_addr *saddr = &np->daddr;
  83. const int dif = sk->sk_bound_dev_if;
  84. const u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
  85. const unsigned int hash = inet6_ehashfn(daddr, inet->num, saddr,
  86. inet->dport);
  87. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  88. struct sock *sk2;
  89. const struct hlist_node *node;
  90. struct inet_timewait_sock *tw;
  91. prefetch(head->chain.first);
  92. write_lock(&head->lock);
  93. /* Check TIME-WAIT sockets first. */
  94. sk_for_each(sk2, node, &(head + hinfo->ehash_size)->chain) {
  95. const struct inet6_timewait_sock *tw6 = inet6_twsk(sk2);
  96. tw = inet_twsk(sk2);
  97. if(*((__u32 *)&(tw->tw_dport)) == ports &&
  98. sk2->sk_family == PF_INET6 &&
  99. ipv6_addr_equal(&tw6->tw_v6_daddr, saddr) &&
  100. ipv6_addr_equal(&tw6->tw_v6_rcv_saddr, daddr) &&
  101. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) {
  102. if (twsk_unique(sk, sk2, twp))
  103. goto unique;
  104. else
  105. goto not_unique;
  106. }
  107. }
  108. tw = NULL;
  109. /* And established part... */
  110. sk_for_each(sk2, node, &head->chain) {
  111. if (INET6_MATCH(sk2, hash, saddr, daddr, ports, dif))
  112. goto not_unique;
  113. }
  114. unique:
  115. BUG_TRAP(sk_unhashed(sk));
  116. __sk_add_node(sk, &head->chain);
  117. sk->sk_hash = hash;
  118. sock_prot_inc_use(sk->sk_prot);
  119. write_unlock(&head->lock);
  120. if (twp != NULL) {
  121. *twp = tw;
  122. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  123. } else if (tw != NULL) {
  124. /* Silly. Should hash-dance instead... */
  125. inet_twsk_deschedule(tw, death_row);
  126. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  127. inet_twsk_put(tw);
  128. }
  129. return 0;
  130. not_unique:
  131. write_unlock(&head->lock);
  132. return -EADDRNOTAVAIL;
  133. }
  134. static inline u32 inet6_sk_port_offset(const struct sock *sk)
  135. {
  136. const struct inet_sock *inet = inet_sk(sk);
  137. const struct ipv6_pinfo *np = inet6_sk(sk);
  138. return secure_ipv6_port_ephemeral(np->rcv_saddr.s6_addr32,
  139. np->daddr.s6_addr32,
  140. inet->dport);
  141. }
  142. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  143. struct sock *sk)
  144. {
  145. struct inet_hashinfo *hinfo = death_row->hashinfo;
  146. const unsigned short snum = inet_sk(sk)->num;
  147. struct inet_bind_hashbucket *head;
  148. struct inet_bind_bucket *tb;
  149. int ret;
  150. if (snum == 0) {
  151. const int low = sysctl_local_port_range[0];
  152. const int high = sysctl_local_port_range[1];
  153. const int range = high - low;
  154. int i, port;
  155. static u32 hint;
  156. const u32 offset = hint + inet6_sk_port_offset(sk);
  157. struct hlist_node *node;
  158. struct inet_timewait_sock *tw = NULL;
  159. local_bh_disable();
  160. for (i = 1; i <= range; i++) {
  161. port = low + (i + offset) % range;
  162. head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
  163. spin_lock(&head->lock);
  164. /* Does not bother with rcv_saddr checks,
  165. * because the established check is already
  166. * unique enough.
  167. */
  168. inet_bind_bucket_for_each(tb, node, &head->chain) {
  169. if (tb->port == port) {
  170. BUG_TRAP(!hlist_empty(&tb->owners));
  171. if (tb->fastreuse >= 0)
  172. goto next_port;
  173. if (!__inet6_check_established(death_row,
  174. sk, port,
  175. &tw))
  176. goto ok;
  177. goto next_port;
  178. }
  179. }
  180. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  181. head, port);
  182. if (!tb) {
  183. spin_unlock(&head->lock);
  184. break;
  185. }
  186. tb->fastreuse = -1;
  187. goto ok;
  188. next_port:
  189. spin_unlock(&head->lock);
  190. }
  191. local_bh_enable();
  192. return -EADDRNOTAVAIL;
  193. ok:
  194. hint += i;
  195. /* Head lock still held and bh's disabled */
  196. inet_bind_hash(sk, tb, port);
  197. if (sk_unhashed(sk)) {
  198. inet_sk(sk)->sport = htons(port);
  199. __inet6_hash(hinfo, sk);
  200. }
  201. spin_unlock(&head->lock);
  202. if (tw) {
  203. inet_twsk_deschedule(tw, death_row);
  204. inet_twsk_put(tw);
  205. }
  206. ret = 0;
  207. goto out;
  208. }
  209. head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
  210. tb = inet_csk(sk)->icsk_bind_hash;
  211. spin_lock_bh(&head->lock);
  212. if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
  213. __inet6_hash(hinfo, sk);
  214. spin_unlock_bh(&head->lock);
  215. return 0;
  216. } else {
  217. spin_unlock(&head->lock);
  218. /* No definite answer... Walk to established hash table */
  219. ret = __inet6_check_established(death_row, sk, snum, NULL);
  220. out:
  221. local_bh_enable();
  222. return ret;
  223. }
  224. }
  225. EXPORT_SYMBOL_GPL(inet6_hash_connect);