bind.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2006, 2019 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. !refcount_inc_not_zero(&rds_rs_to_sk(rs)->sk_refcnt)))
  78. rs = NULL;
  79. rcu_read_unlock();
  80. rdsdebug("returning rs %p for %pI6c:%u\n", rs, addr,
  81. ntohs(port));
  82. return rs;
  83. }
  84. /* returns -ve errno or +ve port */
  85. static int rds_add_bound(struct rds_sock *rs, const struct in6_addr *addr,
  86. __be16 *port, __u32 scope_id)
  87. {
  88. int ret = -EADDRINUSE;
  89. u16 rover, last;
  90. u8 key[RDS_BOUND_KEY_LEN];
  91. if (*port != 0) {
  92. rover = be16_to_cpu(*port);
  93. if (rover == RDS_FLAG_PROBE_PORT)
  94. return -EINVAL;
  95. last = rover;
  96. } else {
  97. rover = max_t(u16, prandom_u32(), 2);
  98. last = rover - 1;
  99. }
  100. do {
  101. if (rover == 0)
  102. rover++;
  103. if (rover == RDS_FLAG_PROBE_PORT)
  104. continue;
  105. __rds_create_bind_key(key, addr, cpu_to_be16(rover),
  106. scope_id);
  107. if (rhashtable_lookup_fast(&bind_hash_table, key, ht_parms))
  108. continue;
  109. memcpy(rs->rs_bound_key, key, sizeof(rs->rs_bound_key));
  110. rs->rs_bound_addr = *addr;
  111. net_get_random_once(&rs->rs_hash_initval,
  112. sizeof(rs->rs_hash_initval));
  113. rs->rs_bound_port = cpu_to_be16(rover);
  114. rs->rs_bound_node.next = NULL;
  115. rds_sock_addref(rs);
  116. if (!rhashtable_insert_fast(&bind_hash_table,
  117. &rs->rs_bound_node, ht_parms)) {
  118. *port = rs->rs_bound_port;
  119. rs->rs_bound_scope_id = scope_id;
  120. ret = 0;
  121. rdsdebug("rs %p binding to %pI6c:%d\n",
  122. rs, addr, (int)ntohs(*port));
  123. break;
  124. } else {
  125. rs->rs_bound_addr = in6addr_any;
  126. rds_sock_put(rs);
  127. ret = -ENOMEM;
  128. break;
  129. }
  130. } while (rover++ != last);
  131. return ret;
  132. }
  133. void rds_remove_bound(struct rds_sock *rs)
  134. {
  135. if (ipv6_addr_any(&rs->rs_bound_addr))
  136. return;
  137. rdsdebug("rs %p unbinding from %pI6c:%d\n",
  138. rs, &rs->rs_bound_addr,
  139. ntohs(rs->rs_bound_port));
  140. rhashtable_remove_fast(&bind_hash_table, &rs->rs_bound_node, ht_parms);
  141. rds_sock_put(rs);
  142. rs->rs_bound_addr = in6addr_any;
  143. }
  144. int rds_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  145. {
  146. struct sock *sk = sock->sk;
  147. struct rds_sock *rs = rds_sk_to_rs(sk);
  148. struct in6_addr v6addr, *binding_addr;
  149. struct rds_transport *trans;
  150. __u32 scope_id = 0;
  151. int ret = 0;
  152. __be16 port;
  153. /* We allow an RDS socket to be bound to either IPv4 or IPv6
  154. * address.
  155. */
  156. if (addr_len < offsetofend(struct sockaddr, sa_family))
  157. return -EINVAL;
  158. if (uaddr->sa_family == AF_INET) {
  159. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  160. if (addr_len < sizeof(struct sockaddr_in) ||
  161. sin->sin_addr.s_addr == htonl(INADDR_ANY) ||
  162. sin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
  163. ipv4_is_multicast(sin->sin_addr.s_addr))
  164. return -EINVAL;
  165. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &v6addr);
  166. binding_addr = &v6addr;
  167. port = sin->sin_port;
  168. #if IS_ENABLED(CONFIG_IPV6)
  169. } else if (uaddr->sa_family == AF_INET6) {
  170. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)uaddr;
  171. int addr_type;
  172. if (addr_len < sizeof(struct sockaddr_in6))
  173. return -EINVAL;
  174. addr_type = ipv6_addr_type(&sin6->sin6_addr);
  175. if (!(addr_type & IPV6_ADDR_UNICAST)) {
  176. __be32 addr4;
  177. if (!(addr_type & IPV6_ADDR_MAPPED))
  178. return -EINVAL;
  179. /* It is a mapped address. Need to do some sanity
  180. * checks.
  181. */
  182. addr4 = sin6->sin6_addr.s6_addr32[3];
  183. if (addr4 == htonl(INADDR_ANY) ||
  184. addr4 == htonl(INADDR_BROADCAST) ||
  185. ipv4_is_multicast(addr4))
  186. return -EINVAL;
  187. }
  188. /* The scope ID must be specified for link local address. */
  189. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  190. if (sin6->sin6_scope_id == 0)
  191. return -EINVAL;
  192. scope_id = sin6->sin6_scope_id;
  193. }
  194. binding_addr = &sin6->sin6_addr;
  195. port = sin6->sin6_port;
  196. #endif
  197. } else {
  198. return -EINVAL;
  199. }
  200. lock_sock(sk);
  201. /* RDS socket does not allow re-binding. */
  202. if (!ipv6_addr_any(&rs->rs_bound_addr)) {
  203. ret = -EINVAL;
  204. goto out;
  205. }
  206. /* Socket is connected. The binding address should have the same
  207. * scope ID as the connected address, except the case when one is
  208. * non-link local address (scope_id is 0).
  209. */
  210. if (!ipv6_addr_any(&rs->rs_conn_addr) && scope_id &&
  211. rs->rs_bound_scope_id &&
  212. scope_id != rs->rs_bound_scope_id) {
  213. ret = -EINVAL;
  214. goto out;
  215. }
  216. /* The transport can be set using SO_RDS_TRANSPORT option before the
  217. * socket is bound.
  218. */
  219. if (rs->rs_transport) {
  220. trans = rs->rs_transport;
  221. if (!trans->laddr_check ||
  222. trans->laddr_check(sock_net(sock->sk),
  223. binding_addr, scope_id) != 0) {
  224. ret = -ENOPROTOOPT;
  225. goto out;
  226. }
  227. } else {
  228. trans = rds_trans_get_preferred(sock_net(sock->sk),
  229. binding_addr, scope_id);
  230. if (!trans) {
  231. ret = -EADDRNOTAVAIL;
  232. pr_info_ratelimited("RDS: %s could not find a transport for %pI6c, load rds_tcp or rds_rdma?\n",
  233. __func__, binding_addr);
  234. goto out;
  235. }
  236. rs->rs_transport = trans;
  237. }
  238. sock_set_flag(sk, SOCK_RCU_FREE);
  239. ret = rds_add_bound(rs, binding_addr, &port, scope_id);
  240. if (ret)
  241. rs->rs_transport = NULL;
  242. out:
  243. release_sock(sk);
  244. return ret;
  245. }
  246. void rds_bind_lock_destroy(void)
  247. {
  248. rhashtable_destroy(&bind_hash_table);
  249. }
  250. int rds_bind_lock_init(void)
  251. {
  252. return rhashtable_init(&bind_hash_table, &ht_parms);
  253. }