bind.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <net/sock.h>
  35. #include <linux/in.h>
  36. #include <linux/ipv6.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/jhash.h>
  39. #include <linux/ratelimit.h>
  40. #include "rds.h"
  41. static struct rhashtable bind_hash_table;
  42. static const struct rhashtable_params ht_parms = {
  43. .nelem_hint = 768,
  44. .key_len = RDS_BOUND_KEY_LEN,
  45. .key_offset = offsetof(struct rds_sock, rs_bound_key),
  46. .head_offset = offsetof(struct rds_sock, rs_bound_node),
  47. .max_size = 16384,
  48. .min_size = 1024,
  49. };
  50. /* Create a key for the bind hash table manipulation. Port is in network byte
  51. * order.
  52. */
  53. static inline void __rds_create_bind_key(u8 *key, const struct in6_addr *addr,
  54. __be16 port, __u32 scope_id)
  55. {
  56. memcpy(key, addr, sizeof(*addr));
  57. key += sizeof(*addr);
  58. memcpy(key, &port, sizeof(port));
  59. key += sizeof(port);
  60. memcpy(key, &scope_id, sizeof(scope_id));
  61. }
  62. /*
  63. * Return the rds_sock bound at the given local address.
  64. *
  65. * The rx path can race with rds_release. We notice if rds_release() has
  66. * marked this socket and don't return a rs ref to the rx path.
  67. */
  68. struct rds_sock *rds_find_bound(const struct in6_addr *addr, __be16 port,
  69. __u32 scope_id)
  70. {
  71. u8 key[RDS_BOUND_KEY_LEN];
  72. struct rds_sock *rs;
  73. __rds_create_bind_key(key, addr, port, scope_id);
  74. rcu_read_lock();
  75. rs = rhashtable_lookup(&bind_hash_table, key, ht_parms);
  76. if (rs && !sock_flag(rds_rs_to_sk(rs), SOCK_DEAD))
  77. rds_sock_addref(rs);
  78. else
  79. rs = NULL;
  80. rcu_read_unlock();
  81. rdsdebug("returning rs %p for %pI6c:%u\n", rs, addr,
  82. ntohs(port));
  83. return rs;
  84. }
  85. /* returns -ve errno or +ve port */
  86. static int rds_add_bound(struct rds_sock *rs, const struct in6_addr *addr,
  87. __be16 *port, __u32 scope_id)
  88. {
  89. int ret = -EADDRINUSE;
  90. u16 rover, last;
  91. u8 key[RDS_BOUND_KEY_LEN];
  92. if (*port != 0) {
  93. rover = be16_to_cpu(*port);
  94. if (rover == RDS_FLAG_PROBE_PORT)
  95. return -EINVAL;
  96. last = rover;
  97. } else {
  98. rover = max_t(u16, prandom_u32(), 2);
  99. last = rover - 1;
  100. }
  101. do {
  102. if (rover == 0)
  103. rover++;
  104. if (rover == RDS_FLAG_PROBE_PORT)
  105. continue;
  106. __rds_create_bind_key(key, addr, cpu_to_be16(rover),
  107. scope_id);
  108. if (rhashtable_lookup_fast(&bind_hash_table, key, ht_parms))
  109. continue;
  110. memcpy(rs->rs_bound_key, key, sizeof(rs->rs_bound_key));
  111. rs->rs_bound_addr = *addr;
  112. net_get_random_once(&rs->rs_hash_initval,
  113. sizeof(rs->rs_hash_initval));
  114. rs->rs_bound_port = cpu_to_be16(rover);
  115. rs->rs_bound_node.next = NULL;
  116. rds_sock_addref(rs);
  117. if (!rhashtable_insert_fast(&bind_hash_table,
  118. &rs->rs_bound_node, ht_parms)) {
  119. *port = rs->rs_bound_port;
  120. rs->rs_bound_scope_id = scope_id;
  121. ret = 0;
  122. rdsdebug("rs %p binding to %pI6c:%d\n",
  123. rs, addr, (int)ntohs(*port));
  124. break;
  125. } else {
  126. rs->rs_bound_addr = in6addr_any;
  127. rds_sock_put(rs);
  128. ret = -ENOMEM;
  129. break;
  130. }
  131. } while (rover++ != last);
  132. return ret;
  133. }
  134. void rds_remove_bound(struct rds_sock *rs)
  135. {
  136. if (ipv6_addr_any(&rs->rs_bound_addr))
  137. return;
  138. rdsdebug("rs %p unbinding from %pI6c:%d\n",
  139. rs, &rs->rs_bound_addr,
  140. ntohs(rs->rs_bound_port));
  141. rhashtable_remove_fast(&bind_hash_table, &rs->rs_bound_node, ht_parms);
  142. rds_sock_put(rs);
  143. rs->rs_bound_addr = in6addr_any;
  144. }
  145. int rds_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  146. {
  147. struct sock *sk = sock->sk;
  148. struct rds_sock *rs = rds_sk_to_rs(sk);
  149. struct in6_addr v6addr, *binding_addr;
  150. struct rds_transport *trans;
  151. __u32 scope_id = 0;
  152. int ret = 0;
  153. __be16 port;
  154. /* We allow an RDS socket to be bound to either IPv4 or IPv6
  155. * address.
  156. */
  157. if (uaddr->sa_family == AF_INET) {
  158. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  159. if (addr_len < sizeof(struct sockaddr_in) ||
  160. sin->sin_addr.s_addr == htonl(INADDR_ANY) ||
  161. sin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
  162. IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
  163. return -EINVAL;
  164. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &v6addr);
  165. binding_addr = &v6addr;
  166. port = sin->sin_port;
  167. #if IS_ENABLED(CONFIG_IPV6)
  168. } else if (uaddr->sa_family == AF_INET6) {
  169. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)uaddr;
  170. int addr_type;
  171. if (addr_len < sizeof(struct sockaddr_in6))
  172. return -EINVAL;
  173. addr_type = ipv6_addr_type(&sin6->sin6_addr);
  174. if (!(addr_type & IPV6_ADDR_UNICAST)) {
  175. __be32 addr4;
  176. if (!(addr_type & IPV6_ADDR_MAPPED))
  177. return -EINVAL;
  178. /* It is a mapped address. Need to do some sanity
  179. * checks.
  180. */
  181. addr4 = sin6->sin6_addr.s6_addr32[3];
  182. if (addr4 == htonl(INADDR_ANY) ||
  183. addr4 == htonl(INADDR_BROADCAST) ||
  184. IN_MULTICAST(ntohl(addr4)))
  185. return -EINVAL;
  186. }
  187. /* The scope ID must be specified for link local address. */
  188. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  189. if (sin6->sin6_scope_id == 0)
  190. return -EINVAL;
  191. scope_id = sin6->sin6_scope_id;
  192. }
  193. binding_addr = &sin6->sin6_addr;
  194. port = sin6->sin6_port;
  195. #endif
  196. } else {
  197. return -EINVAL;
  198. }
  199. lock_sock(sk);
  200. /* RDS socket does not allow re-binding. */
  201. if (!ipv6_addr_any(&rs->rs_bound_addr)) {
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. /* Socket is connected. The binding address should have the same
  206. * scope ID as the connected address, except the case when one is
  207. * non-link local address (scope_id is 0).
  208. */
  209. if (!ipv6_addr_any(&rs->rs_conn_addr) && scope_id &&
  210. rs->rs_bound_scope_id &&
  211. scope_id != rs->rs_bound_scope_id) {
  212. ret = -EINVAL;
  213. goto out;
  214. }
  215. sock_set_flag(sk, SOCK_RCU_FREE);
  216. ret = rds_add_bound(rs, binding_addr, &port, scope_id);
  217. if (ret)
  218. goto out;
  219. if (rs->rs_transport) { /* previously bound */
  220. trans = rs->rs_transport;
  221. if (trans->laddr_check(sock_net(sock->sk),
  222. binding_addr, scope_id) != 0) {
  223. ret = -ENOPROTOOPT;
  224. rds_remove_bound(rs);
  225. } else {
  226. ret = 0;
  227. }
  228. goto out;
  229. }
  230. trans = rds_trans_get_preferred(sock_net(sock->sk), binding_addr,
  231. scope_id);
  232. if (!trans) {
  233. ret = -EADDRNOTAVAIL;
  234. rds_remove_bound(rs);
  235. pr_info_ratelimited("RDS: %s could not find a transport for %pI6c, load rds_tcp or rds_rdma?\n",
  236. __func__, binding_addr);
  237. goto out;
  238. }
  239. rs->rs_transport = trans;
  240. ret = 0;
  241. out:
  242. release_sock(sk);
  243. return ret;
  244. }
  245. void rds_bind_lock_destroy(void)
  246. {
  247. rhashtable_destroy(&bind_hash_table);
  248. }
  249. int rds_bind_lock_init(void)
  250. {
  251. return rhashtable_init(&bind_hash_table, &ht_parms);
  252. }