inet6_hashtables.c 8.9 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 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/module.h>
  17. #include <linux/random.h>
  18. #include <net/inet_connection_sock.h>
  19. #include <net/inet_hashtables.h>
  20. #include <net/inet6_hashtables.h>
  21. #include <net/ip.h>
  22. void __inet6_hash(struct inet_hashinfo *hashinfo,
  23. struct sock *sk)
  24. {
  25. struct hlist_head *list;
  26. rwlock_t *lock;
  27. BUG_TRAP(sk_unhashed(sk));
  28. if (sk->sk_state == TCP_LISTEN) {
  29. list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
  30. lock = &hashinfo->lhash_lock;
  31. inet_listen_wlock(hashinfo);
  32. } else {
  33. unsigned int hash;
  34. sk->sk_hash = hash = inet6_sk_ehashfn(sk);
  35. hash &= (hashinfo->ehash_size - 1);
  36. list = &hashinfo->ehash[hash].chain;
  37. lock = &hashinfo->ehash[hash].lock;
  38. write_lock(lock);
  39. }
  40. __sk_add_node(sk, list);
  41. sock_prot_inc_use(sk->sk_prot);
  42. write_unlock(lock);
  43. }
  44. EXPORT_SYMBOL(__inet6_hash);
  45. /*
  46. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  47. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  48. *
  49. * The sockhash lock must be held as a reader here.
  50. */
  51. struct sock *__inet6_lookup_established(struct inet_hashinfo *hashinfo,
  52. const struct in6_addr *saddr,
  53. const __be16 sport,
  54. const struct in6_addr *daddr,
  55. const u16 hnum,
  56. const int dif)
  57. {
  58. struct sock *sk;
  59. const struct hlist_node *node;
  60. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  61. /* Optimize here for direct hit, only listening connections can
  62. * have wildcards anyways.
  63. */
  64. unsigned int hash = inet6_ehashfn(daddr, hnum, saddr, sport);
  65. struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
  66. prefetch(head->chain.first);
  67. read_lock(&head->lock);
  68. sk_for_each(sk, node, &head->chain) {
  69. /* For IPV6 do the cheaper port and family tests first. */
  70. if (INET6_MATCH(sk, hash, saddr, daddr, ports, dif))
  71. goto hit; /* You sunk my battleship! */
  72. }
  73. /* Must check for a TIME_WAIT'er before going to listener hash. */
  74. sk_for_each(sk, node, &head->twchain) {
  75. const struct inet_timewait_sock *tw = inet_twsk(sk);
  76. if(*((__portpair *)&(tw->tw_dport)) == ports &&
  77. sk->sk_family == PF_INET6) {
  78. const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
  79. if (ipv6_addr_equal(&tw6->tw_v6_daddr, saddr) &&
  80. ipv6_addr_equal(&tw6->tw_v6_rcv_saddr, daddr) &&
  81. (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif))
  82. goto hit;
  83. }
  84. }
  85. read_unlock(&head->lock);
  86. return NULL;
  87. hit:
  88. sock_hold(sk);
  89. read_unlock(&head->lock);
  90. return sk;
  91. }
  92. EXPORT_SYMBOL(__inet6_lookup_established);
  93. struct sock *inet6_lookup_listener(struct inet_hashinfo *hashinfo,
  94. const struct in6_addr *daddr,
  95. const unsigned short hnum, const int dif)
  96. {
  97. struct sock *sk;
  98. const struct hlist_node *node;
  99. struct sock *result = NULL;
  100. int score, hiscore = 0;
  101. read_lock(&hashinfo->lhash_lock);
  102. sk_for_each(sk, node, &hashinfo->listening_hash[inet_lhashfn(hnum)]) {
  103. if (inet_sk(sk)->num == hnum && sk->sk_family == PF_INET6) {
  104. const struct ipv6_pinfo *np = inet6_sk(sk);
  105. score = 1;
  106. if (!ipv6_addr_any(&np->rcv_saddr)) {
  107. if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
  108. continue;
  109. score++;
  110. }
  111. if (sk->sk_bound_dev_if) {
  112. if (sk->sk_bound_dev_if != dif)
  113. continue;
  114. score++;
  115. }
  116. if (score == 3) {
  117. result = sk;
  118. break;
  119. }
  120. if (score > hiscore) {
  121. hiscore = score;
  122. result = sk;
  123. }
  124. }
  125. }
  126. if (result)
  127. sock_hold(result);
  128. read_unlock(&hashinfo->lhash_lock);
  129. return result;
  130. }
  131. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  132. struct sock *inet6_lookup(struct inet_hashinfo *hashinfo,
  133. const struct in6_addr *saddr, const __be16 sport,
  134. const struct in6_addr *daddr, const __be16 dport,
  135. const int dif)
  136. {
  137. struct sock *sk;
  138. local_bh_disable();
  139. sk = __inet6_lookup(hashinfo, saddr, sport, daddr, ntohs(dport), dif);
  140. local_bh_enable();
  141. return sk;
  142. }
  143. EXPORT_SYMBOL_GPL(inet6_lookup);
  144. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  145. struct sock *sk, const __u16 lport,
  146. struct inet_timewait_sock **twp)
  147. {
  148. struct inet_hashinfo *hinfo = death_row->hashinfo;
  149. struct inet_sock *inet = inet_sk(sk);
  150. const struct ipv6_pinfo *np = inet6_sk(sk);
  151. const struct in6_addr *daddr = &np->rcv_saddr;
  152. const struct in6_addr *saddr = &np->daddr;
  153. const int dif = sk->sk_bound_dev_if;
  154. const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
  155. const unsigned int hash = inet6_ehashfn(daddr, lport, saddr,
  156. 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->twchain) {
  165. const struct inet6_timewait_sock *tw6 = inet6_twsk(sk2);
  166. tw = inet_twsk(sk2);
  167. if(*((__portpair *)&(tw->tw_dport)) == ports &&
  168. sk2->sk_family == PF_INET6 &&
  169. ipv6_addr_equal(&tw6->tw_v6_daddr, saddr) &&
  170. ipv6_addr_equal(&tw6->tw_v6_rcv_saddr, daddr) &&
  171. sk2->sk_bound_dev_if == sk->sk_bound_dev_if) {
  172. if (twsk_unique(sk, sk2, twp))
  173. goto unique;
  174. else
  175. goto not_unique;
  176. }
  177. }
  178. tw = NULL;
  179. /* And established part... */
  180. sk_for_each(sk2, node, &head->chain) {
  181. if (INET6_MATCH(sk2, hash, saddr, daddr, ports, dif))
  182. goto not_unique;
  183. }
  184. unique:
  185. /* Must record num and sport now. Otherwise we will see
  186. * in hash table socket with a funny identity. */
  187. inet->num = lport;
  188. inet->sport = htons(lport);
  189. BUG_TRAP(sk_unhashed(sk));
  190. __sk_add_node(sk, &head->chain);
  191. sk->sk_hash = hash;
  192. sock_prot_inc_use(sk->sk_prot);
  193. write_unlock(&head->lock);
  194. if (twp != NULL) {
  195. *twp = tw;
  196. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  197. } else if (tw != NULL) {
  198. /* Silly. Should hash-dance instead... */
  199. inet_twsk_deschedule(tw, death_row);
  200. NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
  201. inet_twsk_put(tw);
  202. }
  203. return 0;
  204. not_unique:
  205. write_unlock(&head->lock);
  206. return -EADDRNOTAVAIL;
  207. }
  208. static inline u32 inet6_sk_port_offset(const struct sock *sk)
  209. {
  210. const struct inet_sock *inet = inet_sk(sk);
  211. const struct ipv6_pinfo *np = inet6_sk(sk);
  212. return secure_ipv6_port_ephemeral(np->rcv_saddr.s6_addr32,
  213. np->daddr.s6_addr32,
  214. inet->dport);
  215. }
  216. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  217. struct sock *sk)
  218. {
  219. struct inet_hashinfo *hinfo = death_row->hashinfo;
  220. const unsigned short snum = inet_sk(sk)->num;
  221. struct inet_bind_hashbucket *head;
  222. struct inet_bind_bucket *tb;
  223. int ret;
  224. if (snum == 0) {
  225. const int low = sysctl_local_port_range[0];
  226. const int high = sysctl_local_port_range[1];
  227. const int range = high - low;
  228. int i, port;
  229. static u32 hint;
  230. const u32 offset = hint + inet6_sk_port_offset(sk);
  231. struct hlist_node *node;
  232. struct inet_timewait_sock *tw = NULL;
  233. local_bh_disable();
  234. for (i = 1; i <= range; i++) {
  235. port = low + (i + offset) % range;
  236. head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
  237. spin_lock(&head->lock);
  238. /* Does not bother with rcv_saddr checks,
  239. * because the established check is already
  240. * unique enough.
  241. */
  242. inet_bind_bucket_for_each(tb, node, &head->chain) {
  243. if (tb->port == port) {
  244. BUG_TRAP(!hlist_empty(&tb->owners));
  245. if (tb->fastreuse >= 0)
  246. goto next_port;
  247. if (!__inet6_check_established(death_row,
  248. sk, port,
  249. &tw))
  250. goto ok;
  251. goto next_port;
  252. }
  253. }
  254. tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
  255. head, port);
  256. if (!tb) {
  257. spin_unlock(&head->lock);
  258. break;
  259. }
  260. tb->fastreuse = -1;
  261. goto ok;
  262. next_port:
  263. spin_unlock(&head->lock);
  264. }
  265. local_bh_enable();
  266. return -EADDRNOTAVAIL;
  267. ok:
  268. hint += i;
  269. /* Head lock still held and bh's disabled */
  270. inet_bind_hash(sk, tb, port);
  271. if (sk_unhashed(sk)) {
  272. inet_sk(sk)->sport = htons(port);
  273. __inet6_hash(hinfo, sk);
  274. }
  275. spin_unlock(&head->lock);
  276. if (tw) {
  277. inet_twsk_deschedule(tw, death_row);
  278. inet_twsk_put(tw);
  279. }
  280. ret = 0;
  281. goto out;
  282. }
  283. head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
  284. tb = inet_csk(sk)->icsk_bind_hash;
  285. spin_lock_bh(&head->lock);
  286. if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
  287. __inet6_hash(hinfo, sk);
  288. spin_unlock_bh(&head->lock);
  289. return 0;
  290. } else {
  291. spin_unlock(&head->lock);
  292. /* No definite answer... Walk to established hash table */
  293. ret = __inet6_check_established(death_row, sk, snum, NULL);
  294. out:
  295. local_bh_enable();
  296. return ret;
  297. }
  298. }
  299. EXPORT_SYMBOL_GPL(inet6_hash_connect);