1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Local endpoint object management 3 * 4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/module.h> 11 #include <linux/net.h> 12 #include <linux/skbuff.h> 13 #include <linux/slab.h> 14 #include <linux/udp.h> 15 #include <linux/ip.h> 16 #include <linux/hashtable.h> 17 #include <net/sock.h> 18 #include <net/udp.h> 19 #include <net/udp_tunnel.h> 20 #include <net/af_rxrpc.h> 21 #include "ar-internal.h" 22 23 static void rxrpc_local_rcu(struct rcu_head *); 24 25 /* 26 * Handle an ICMP/ICMP6 error turning up at the tunnel. Push it through the 27 * usual mechanism so that it gets parsed and presented through the UDP 28 * socket's error_report(). 29 */ 30 static void rxrpc_encap_err_rcv(struct sock *sk, struct sk_buff *skb, int err, 31 __be16 port, u32 info, u8 *payload) 32 { 33 if (ip_hdr(skb)->version == IPVERSION) 34 return ip_icmp_error(sk, skb, err, port, info, payload); 35 if (IS_ENABLED(CONFIG_AF_RXRPC_IPV6)) 36 return ipv6_icmp_error(sk, skb, err, port, info, payload); 37 } 38 39 /* 40 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than, 41 * same or greater than. 42 * 43 * We explicitly don't compare the RxRPC service ID as we want to reject 44 * conflicting uses by differing services. Further, we don't want to share 45 * addresses with different options (IPv6), so we don't compare those bits 46 * either. 47 */ 48 static long rxrpc_local_cmp_key(const struct rxrpc_local *local, 49 const struct sockaddr_rxrpc *srx) 50 { 51 long diff; 52 53 diff = ((local->srx.transport_type - srx->transport_type) ?: 54 (local->srx.transport_len - srx->transport_len) ?: 55 (local->srx.transport.family - srx->transport.family)); 56 if (diff != 0) 57 return diff; 58 59 switch (srx->transport.family) { 60 case AF_INET: 61 /* If the choice of UDP port is left up to the transport, then 62 * the endpoint record doesn't match. 63 */ 64 return ((u16 __force)local->srx.transport.sin.sin_port - 65 (u16 __force)srx->transport.sin.sin_port) ?: 66 memcmp(&local->srx.transport.sin.sin_addr, 67 &srx->transport.sin.sin_addr, 68 sizeof(struct in_addr)); 69 #ifdef CONFIG_AF_RXRPC_IPV6 70 case AF_INET6: 71 /* If the choice of UDP6 port is left up to the transport, then 72 * the endpoint record doesn't match. 73 */ 74 return ((u16 __force)local->srx.transport.sin6.sin6_port - 75 (u16 __force)srx->transport.sin6.sin6_port) ?: 76 memcmp(&local->srx.transport.sin6.sin6_addr, 77 &srx->transport.sin6.sin6_addr, 78 sizeof(struct in6_addr)); 79 #endif 80 default: 81 BUG(); 82 } 83 } 84 85 /* 86 * Allocate a new local endpoint. 87 */ 88 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet, 89 const struct sockaddr_rxrpc *srx) 90 { 91 struct rxrpc_local *local; 92 93 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL); 94 if (local) { 95 refcount_set(&local->ref, 1); 96 atomic_set(&local->active_users, 1); 97 local->rxnet = rxnet; 98 INIT_HLIST_NODE(&local->link); 99 init_rwsem(&local->defrag_sem); 100 skb_queue_head_init(&local->rx_queue); 101 INIT_LIST_HEAD(&local->call_attend_q); 102 local->client_bundles = RB_ROOT; 103 spin_lock_init(&local->client_bundles_lock); 104 spin_lock_init(&local->lock); 105 rwlock_init(&local->services_lock); 106 local->debug_id = atomic_inc_return(&rxrpc_debug_id); 107 memcpy(&local->srx, srx, sizeof(*srx)); 108 local->srx.srx_service = 0; 109 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, 1); 110 } 111 112 _leave(" = %p", local); 113 return local; 114 } 115 116 /* 117 * create the local socket 118 * - must be called with rxrpc_local_mutex locked 119 */ 120 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net) 121 { 122 struct udp_tunnel_sock_cfg tuncfg = {NULL}; 123 struct sockaddr_rxrpc *srx = &local->srx; 124 struct udp_port_cfg udp_conf = {0}; 125 struct task_struct *io_thread; 126 struct sock *usk; 127 int ret; 128 129 _enter("%p{%d,%d}", 130 local, srx->transport_type, srx->transport.family); 131 132 udp_conf.family = srx->transport.family; 133 udp_conf.use_udp_checksums = true; 134 if (udp_conf.family == AF_INET) { 135 udp_conf.local_ip = srx->transport.sin.sin_addr; 136 udp_conf.local_udp_port = srx->transport.sin.sin_port; 137 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6) 138 } else { 139 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr; 140 udp_conf.local_udp_port = srx->transport.sin6.sin6_port; 141 udp_conf.use_udp6_tx_checksums = true; 142 udp_conf.use_udp6_rx_checksums = true; 143 #endif 144 } 145 ret = udp_sock_create(net, &udp_conf, &local->socket); 146 if (ret < 0) { 147 _leave(" = %d [socket]", ret); 148 return ret; 149 } 150 151 tuncfg.encap_type = UDP_ENCAP_RXRPC; 152 tuncfg.encap_rcv = rxrpc_encap_rcv; 153 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv; 154 tuncfg.sk_user_data = local; 155 setup_udp_tunnel_sock(net, local->socket, &tuncfg); 156 157 /* set the socket up */ 158 usk = local->socket->sk; 159 usk->sk_error_report = rxrpc_error_report; 160 161 switch (srx->transport.family) { 162 case AF_INET6: 163 /* we want to receive ICMPv6 errors */ 164 ip6_sock_set_recverr(usk); 165 166 /* Fall through and set IPv4 options too otherwise we don't get 167 * errors from IPv4 packets sent through the IPv6 socket. 168 */ 169 fallthrough; 170 case AF_INET: 171 /* we want to receive ICMP errors */ 172 ip_sock_set_recverr(usk); 173 174 /* we want to set the don't fragment bit */ 175 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO); 176 177 /* We want receive timestamps. */ 178 sock_enable_timestamps(usk); 179 break; 180 181 default: 182 BUG(); 183 } 184 185 io_thread = kthread_run(rxrpc_io_thread, local, 186 "krxrpcio/%u", ntohs(udp_conf.local_udp_port)); 187 if (IS_ERR(io_thread)) { 188 ret = PTR_ERR(io_thread); 189 goto error_sock; 190 } 191 192 local->io_thread = io_thread; 193 _leave(" = 0"); 194 return 0; 195 196 error_sock: 197 kernel_sock_shutdown(local->socket, SHUT_RDWR); 198 local->socket->sk->sk_user_data = NULL; 199 sock_release(local->socket); 200 local->socket = NULL; 201 return ret; 202 } 203 204 /* 205 * Look up or create a new local endpoint using the specified local address. 206 */ 207 struct rxrpc_local *rxrpc_lookup_local(struct net *net, 208 const struct sockaddr_rxrpc *srx) 209 { 210 struct rxrpc_local *local; 211 struct rxrpc_net *rxnet = rxrpc_net(net); 212 struct hlist_node *cursor; 213 long diff; 214 int ret; 215 216 _enter("{%d,%d,%pISp}", 217 srx->transport_type, srx->transport.family, &srx->transport); 218 219 mutex_lock(&rxnet->local_mutex); 220 221 hlist_for_each(cursor, &rxnet->local_endpoints) { 222 local = hlist_entry(cursor, struct rxrpc_local, link); 223 224 diff = rxrpc_local_cmp_key(local, srx); 225 if (diff != 0) 226 continue; 227 228 /* Services aren't allowed to share transport sockets, so 229 * reject that here. It is possible that the object is dying - 230 * but it may also still have the local transport address that 231 * we want bound. 232 */ 233 if (srx->srx_service) { 234 local = NULL; 235 goto addr_in_use; 236 } 237 238 /* Found a match. We want to replace a dying object. 239 * Attempting to bind the transport socket may still fail if 240 * we're attempting to use a local address that the dying 241 * object is still using. 242 */ 243 if (!rxrpc_use_local(local, rxrpc_local_use_lookup)) 244 break; 245 246 goto found; 247 } 248 249 local = rxrpc_alloc_local(rxnet, srx); 250 if (!local) 251 goto nomem; 252 253 ret = rxrpc_open_socket(local, net); 254 if (ret < 0) 255 goto sock_error; 256 257 if (cursor) { 258 hlist_replace_rcu(cursor, &local->link); 259 cursor->pprev = NULL; 260 } else { 261 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints); 262 } 263 264 found: 265 mutex_unlock(&rxnet->local_mutex); 266 _leave(" = %p", local); 267 return local; 268 269 nomem: 270 ret = -ENOMEM; 271 sock_error: 272 mutex_unlock(&rxnet->local_mutex); 273 if (local) 274 call_rcu(&local->rcu, rxrpc_local_rcu); 275 _leave(" = %d", ret); 276 return ERR_PTR(ret); 277 278 addr_in_use: 279 mutex_unlock(&rxnet->local_mutex); 280 _leave(" = -EADDRINUSE"); 281 return ERR_PTR(-EADDRINUSE); 282 } 283 284 /* 285 * Get a ref on a local endpoint. 286 */ 287 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local, 288 enum rxrpc_local_trace why) 289 { 290 int r, u; 291 292 u = atomic_read(&local->active_users); 293 __refcount_inc(&local->ref, &r); 294 trace_rxrpc_local(local->debug_id, why, r + 1, u); 295 return local; 296 } 297 298 /* 299 * Get a ref on a local endpoint unless its usage has already reached 0. 300 */ 301 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local, 302 enum rxrpc_local_trace why) 303 { 304 int r, u; 305 306 if (local && __refcount_inc_not_zero(&local->ref, &r)) { 307 u = atomic_read(&local->active_users); 308 trace_rxrpc_local(local->debug_id, why, r + 1, u); 309 return local; 310 } 311 312 return NULL; 313 } 314 315 /* 316 * Drop a ref on a local endpoint. 317 */ 318 void rxrpc_put_local(struct rxrpc_local *local, enum rxrpc_local_trace why) 319 { 320 unsigned int debug_id; 321 bool dead; 322 int r, u; 323 324 if (local) { 325 debug_id = local->debug_id; 326 327 u = atomic_read(&local->active_users); 328 dead = __refcount_dec_and_test(&local->ref, &r); 329 trace_rxrpc_local(debug_id, why, r, u); 330 331 if (dead) 332 call_rcu(&local->rcu, rxrpc_local_rcu); 333 } 334 } 335 336 /* 337 * Start using a local endpoint. 338 */ 339 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local, 340 enum rxrpc_local_trace why) 341 { 342 local = rxrpc_get_local_maybe(local, rxrpc_local_get_for_use); 343 if (!local) 344 return NULL; 345 346 if (!__rxrpc_use_local(local, why)) { 347 rxrpc_put_local(local, rxrpc_local_put_for_use); 348 return NULL; 349 } 350 351 return local; 352 } 353 354 /* 355 * Cease using a local endpoint. Once the number of active users reaches 0, we 356 * start the closure of the transport in the I/O thread.. 357 */ 358 void rxrpc_unuse_local(struct rxrpc_local *local, enum rxrpc_local_trace why) 359 { 360 unsigned int debug_id = local->debug_id; 361 int r, u; 362 363 if (local) { 364 r = refcount_read(&local->ref); 365 u = atomic_dec_return(&local->active_users); 366 trace_rxrpc_local(debug_id, why, r, u); 367 if (u == 0) 368 kthread_stop(local->io_thread); 369 } 370 } 371 372 /* 373 * Destroy a local endpoint's socket and then hand the record to RCU to dispose 374 * of. 375 * 376 * Closing the socket cannot be done from bottom half context or RCU callback 377 * context because it might sleep. 378 */ 379 void rxrpc_destroy_local(struct rxrpc_local *local) 380 { 381 struct socket *socket = local->socket; 382 struct rxrpc_net *rxnet = local->rxnet; 383 384 _enter("%d", local->debug_id); 385 386 local->dead = true; 387 388 mutex_lock(&rxnet->local_mutex); 389 hlist_del_init_rcu(&local->link); 390 mutex_unlock(&rxnet->local_mutex); 391 392 rxrpc_clean_up_local_conns(local); 393 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper); 394 ASSERT(!local->service); 395 396 if (socket) { 397 local->socket = NULL; 398 kernel_sock_shutdown(socket, SHUT_RDWR); 399 socket->sk->sk_user_data = NULL; 400 sock_release(socket); 401 } 402 403 /* At this point, there should be no more packets coming in to the 404 * local endpoint. 405 */ 406 rxrpc_purge_queue(&local->rx_queue); 407 } 408 409 /* 410 * Destroy a local endpoint after the RCU grace period expires. 411 */ 412 static void rxrpc_local_rcu(struct rcu_head *rcu) 413 { 414 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu); 415 416 rxrpc_see_local(local, rxrpc_local_free); 417 kfree(local); 418 } 419 420 /* 421 * Verify the local endpoint list is empty by this point. 422 */ 423 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet) 424 { 425 struct rxrpc_local *local; 426 427 _enter(""); 428 429 flush_workqueue(rxrpc_workqueue); 430 431 if (!hlist_empty(&rxnet->local_endpoints)) { 432 mutex_lock(&rxnet->local_mutex); 433 hlist_for_each_entry(local, &rxnet->local_endpoints, link) { 434 pr_err("AF_RXRPC: Leaked local %p {%d}\n", 435 local, refcount_read(&local->ref)); 436 } 437 mutex_unlock(&rxnet->local_mutex); 438 BUG(); 439 } 440 } 441