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