inet6_hashtables.c 9.0 KB

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