inet6_hashtables.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/addrconf.h>
  19. #include <net/inet_connection_sock.h>
  20. #include <net/inet_hashtables.h>
  21. #include <net/inet6_hashtables.h>
  22. #include <net/secure_seq.h>
  23. #include <net/ip.h>
  24. #include <net/sock_reuseport.h>
  25. u32 inet6_ehashfn(const struct net *net,
  26. const struct in6_addr *laddr, const u16 lport,
  27. const struct in6_addr *faddr, const __be16 fport)
  28. {
  29. static u32 inet6_ehash_secret __read_mostly;
  30. static u32 ipv6_hash_secret __read_mostly;
  31. u32 lhash, fhash;
  32. net_get_random_once(&inet6_ehash_secret, sizeof(inet6_ehash_secret));
  33. net_get_random_once(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
  34. lhash = (__force u32)laddr->s6_addr32[3];
  35. fhash = __ipv6_addr_jhash(faddr, ipv6_hash_secret);
  36. return __inet6_ehashfn(lhash, lport, fhash, fport,
  37. inet6_ehash_secret + net_hash_mix(net));
  38. }
  39. /*
  40. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  41. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  42. *
  43. * The sockhash lock must be held as a reader here.
  44. */
  45. struct sock *__inet6_lookup_established(struct net *net,
  46. struct inet_hashinfo *hashinfo,
  47. const struct in6_addr *saddr,
  48. const __be16 sport,
  49. const struct in6_addr *daddr,
  50. const u16 hnum,
  51. const int dif, const int sdif)
  52. {
  53. struct sock *sk;
  54. const struct hlist_nulls_node *node;
  55. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  56. /* Optimize here for direct hit, only listening connections can
  57. * have wildcards anyways.
  58. */
  59. unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
  60. unsigned int slot = hash & hashinfo->ehash_mask;
  61. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  62. begin:
  63. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  64. if (sk->sk_hash != hash)
  65. continue;
  66. if (!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif))
  67. continue;
  68. if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
  69. goto out;
  70. if (unlikely(!INET6_MATCH(sk, net, saddr, daddr, ports, dif, sdif))) {
  71. sock_gen_put(sk);
  72. goto begin;
  73. }
  74. goto found;
  75. }
  76. if (get_nulls_value(node) != slot)
  77. goto begin;
  78. out:
  79. sk = NULL;
  80. found:
  81. return sk;
  82. }
  83. EXPORT_SYMBOL(__inet6_lookup_established);
  84. static inline int compute_score(struct sock *sk, struct net *net,
  85. const unsigned short hnum,
  86. const struct in6_addr *daddr,
  87. const int dif, const int sdif, bool exact_dif)
  88. {
  89. int score = -1;
  90. if (net_eq(sock_net(sk), net) && inet_sk(sk)->inet_num == hnum &&
  91. sk->sk_family == PF_INET6) {
  92. score = 1;
  93. if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
  94. if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  95. return -1;
  96. score++;
  97. }
  98. if (sk->sk_bound_dev_if || exact_dif) {
  99. bool dev_match = (sk->sk_bound_dev_if == dif ||
  100. sk->sk_bound_dev_if == sdif);
  101. if (!dev_match)
  102. return -1;
  103. if (sk->sk_bound_dev_if)
  104. score++;
  105. }
  106. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  107. score++;
  108. }
  109. return score;
  110. }
  111. /* called with rcu_read_lock() */
  112. static struct sock *inet6_lhash2_lookup(struct net *net,
  113. struct inet_listen_hashbucket *ilb2,
  114. struct sk_buff *skb, int doff,
  115. const struct in6_addr *saddr,
  116. const __be16 sport, const struct in6_addr *daddr,
  117. const unsigned short hnum, const int dif, const int sdif)
  118. {
  119. bool exact_dif = inet6_exact_dif_match(net, skb);
  120. struct inet_connection_sock *icsk;
  121. struct sock *sk, *result = NULL;
  122. int score, hiscore = 0;
  123. u32 phash = 0;
  124. inet_lhash2_for_each_icsk_rcu(icsk, &ilb2->head) {
  125. sk = (struct sock *)icsk;
  126. score = compute_score(sk, net, hnum, daddr, dif, sdif,
  127. exact_dif);
  128. if (score > hiscore) {
  129. if (sk->sk_reuseport) {
  130. phash = inet6_ehashfn(net, daddr, hnum,
  131. saddr, sport);
  132. result = reuseport_select_sock(sk, phash,
  133. skb, doff);
  134. if (result)
  135. return result;
  136. }
  137. result = sk;
  138. hiscore = score;
  139. }
  140. }
  141. return result;
  142. }
  143. struct sock *inet6_lookup_listener(struct net *net,
  144. struct inet_hashinfo *hashinfo,
  145. struct sk_buff *skb, int doff,
  146. const struct in6_addr *saddr,
  147. const __be16 sport, const struct in6_addr *daddr,
  148. const unsigned short hnum, const int dif, const int sdif)
  149. {
  150. unsigned int hash = inet_lhashfn(net, hnum);
  151. struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
  152. bool exact_dif = inet6_exact_dif_match(net, skb);
  153. struct inet_listen_hashbucket *ilb2;
  154. struct sock *sk, *result = NULL;
  155. struct hlist_nulls_node *node;
  156. int score, hiscore = 0;
  157. unsigned int hash2;
  158. u32 phash = 0;
  159. if (ilb->count <= 10 || !hashinfo->lhash2)
  160. goto port_lookup;
  161. /* Too many sk in the ilb bucket (which is hashed by port alone).
  162. * Try lhash2 (which is hashed by port and addr) instead.
  163. */
  164. hash2 = ipv6_portaddr_hash(net, daddr, hnum);
  165. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  166. if (ilb2->count > ilb->count)
  167. goto port_lookup;
  168. result = inet6_lhash2_lookup(net, ilb2, skb, doff,
  169. saddr, sport, daddr, hnum,
  170. dif, sdif);
  171. if (result)
  172. goto done;
  173. /* Lookup lhash2 with in6addr_any */
  174. hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
  175. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  176. if (ilb2->count > ilb->count)
  177. goto port_lookup;
  178. result = inet6_lhash2_lookup(net, ilb2, skb, doff,
  179. saddr, sport, daddr, hnum,
  180. dif, sdif);
  181. goto done;
  182. port_lookup:
  183. sk_nulls_for_each(sk, node, &ilb->nulls_head) {
  184. score = compute_score(sk, net, hnum, daddr, dif, sdif, exact_dif);
  185. if (score > hiscore) {
  186. if (sk->sk_reuseport) {
  187. phash = inet6_ehashfn(net, daddr, hnum,
  188. saddr, sport);
  189. result = reuseport_select_sock(sk, phash,
  190. skb, doff);
  191. if (result)
  192. goto done;
  193. }
  194. result = sk;
  195. hiscore = score;
  196. }
  197. }
  198. done:
  199. if (unlikely(IS_ERR(result)))
  200. return NULL;
  201. return result;
  202. }
  203. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  204. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  205. struct sk_buff *skb, int doff,
  206. const struct in6_addr *saddr, const __be16 sport,
  207. const struct in6_addr *daddr, const __be16 dport,
  208. const int dif)
  209. {
  210. struct sock *sk;
  211. bool refcounted;
  212. sk = __inet6_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
  213. ntohs(dport), dif, 0, &refcounted);
  214. if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
  215. sk = NULL;
  216. return sk;
  217. }
  218. EXPORT_SYMBOL_GPL(inet6_lookup);
  219. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  220. struct sock *sk, const __u16 lport,
  221. struct inet_timewait_sock **twp)
  222. {
  223. struct inet_hashinfo *hinfo = death_row->hashinfo;
  224. struct inet_sock *inet = inet_sk(sk);
  225. const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
  226. const struct in6_addr *saddr = &sk->sk_v6_daddr;
  227. const int dif = sk->sk_bound_dev_if;
  228. struct net *net = sock_net(sk);
  229. const int sdif = l3mdev_master_ifindex_by_index(net, dif);
  230. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  231. const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr,
  232. inet->inet_dport);
  233. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  234. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  235. struct sock *sk2;
  236. const struct hlist_nulls_node *node;
  237. struct inet_timewait_sock *tw = NULL;
  238. spin_lock(lock);
  239. sk_nulls_for_each(sk2, node, &head->chain) {
  240. if (sk2->sk_hash != hash)
  241. continue;
  242. if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports,
  243. dif, sdif))) {
  244. if (sk2->sk_state == TCP_TIME_WAIT) {
  245. tw = inet_twsk(sk2);
  246. if (twsk_unique(sk, sk2, twp))
  247. break;
  248. }
  249. goto not_unique;
  250. }
  251. }
  252. /* Must record num and sport now. Otherwise we will see
  253. * in hash table socket with a funny identity.
  254. */
  255. inet->inet_num = lport;
  256. inet->inet_sport = htons(lport);
  257. sk->sk_hash = hash;
  258. WARN_ON(!sk_unhashed(sk));
  259. __sk_nulls_add_node_rcu(sk, &head->chain);
  260. if (tw) {
  261. sk_nulls_del_node_init_rcu((struct sock *)tw);
  262. __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
  263. }
  264. spin_unlock(lock);
  265. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  266. if (twp) {
  267. *twp = tw;
  268. } else if (tw) {
  269. /* Silly. Should hash-dance instead... */
  270. inet_twsk_deschedule_put(tw);
  271. }
  272. return 0;
  273. not_unique:
  274. spin_unlock(lock);
  275. return -EADDRNOTAVAIL;
  276. }
  277. static u32 inet6_sk_port_offset(const struct sock *sk)
  278. {
  279. const struct inet_sock *inet = inet_sk(sk);
  280. return secure_ipv6_port_ephemeral(sk->sk_v6_rcv_saddr.s6_addr32,
  281. sk->sk_v6_daddr.s6_addr32,
  282. inet->inet_dport);
  283. }
  284. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  285. struct sock *sk)
  286. {
  287. u32 port_offset = 0;
  288. if (!inet_sk(sk)->inet_num)
  289. port_offset = inet6_sk_port_offset(sk);
  290. return __inet_hash_connect(death_row, sk, port_offset,
  291. __inet6_check_established);
  292. }
  293. EXPORT_SYMBOL_GPL(inet6_hash_connect);
  294. int inet6_hash(struct sock *sk)
  295. {
  296. int err = 0;
  297. if (sk->sk_state != TCP_CLOSE) {
  298. local_bh_disable();
  299. err = __inet_hash(sk, NULL);
  300. local_bh_enable();
  301. }
  302. return err;
  303. }
  304. EXPORT_SYMBOL_GPL(inet6_hash);