inet6_hashtables.c 8.4 KB

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