blob: 504453c688d751fe11cbfbe18e1d20c0623f9a77 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells87563612016-04-04 14:00:34 +01002/* Local endpoint object management
David Howells17926a72007-04-26 15:48:28 -07003 *
David Howells4f95dd72016-04-04 14:00:35 +01004 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
David Howells17926a72007-04-26 15:48:28 -07005 * Written by David Howells (dhowells@redhat.com)
David Howells17926a72007-04-26 15:48:28 -07006 */
7
Joe Perches9b6d5392016-06-02 12:08:52 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
David Howells17926a72007-04-26 15:48:28 -070010#include <linux/module.h>
11#include <linux/net.h>
12#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Howells44ba0692015-04-01 16:31:26 +010014#include <linux/udp.h>
15#include <linux/ip.h>
David Howells4f95dd72016-04-04 14:00:35 +010016#include <linux/hashtable.h>
David Howells17926a72007-04-26 15:48:28 -070017#include <net/sock.h>
David Howells52719532018-10-04 11:10:51 +010018#include <net/udp.h>
Xin Long5d30c622021-02-03 16:54:23 +080019#include <net/udp_tunnel.h>
David Howells17926a72007-04-26 15:48:28 -070020#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
David Howells4f95dd72016-04-04 14:00:35 +010023static void rxrpc_local_rcu(struct rcu_head *);
David Howells17926a72007-04-26 15:48:28 -070024
David Howells17926a72007-04-26 15:48:28 -070025/*
David Howellsb6c66c42022-10-12 09:51:12 +010026 * 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 */
30static 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);
David Howells41cf3a92022-11-10 09:39:39 +000035 if (IS_ENABLED(CONFIG_AF_RXRPC_IPV6))
36 return ipv6_icmp_error(sk, skb, err, port, info, payload);
David Howellsb6c66c42022-10-12 09:51:12 +010037}
38
39/*
David Howells87220142024-01-09 15:10:48 +000040 * Set or clear the Don't Fragment flag on a socket.
41 */
42void rxrpc_local_dont_fragment(const struct rxrpc_local *local, bool set)
43{
44 if (set)
45 ip_sock_set_mtu_discover(local->socket->sk, IP_PMTUDISC_DO);
46 else
47 ip_sock_set_mtu_discover(local->socket->sk, IP_PMTUDISC_DONT);
48}
49
50/*
David Howells4f95dd72016-04-04 14:00:35 +010051 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
52 * same or greater than.
53 *
54 * We explicitly don't compare the RxRPC service ID as we want to reject
55 * conflicting uses by differing services. Further, we don't want to share
56 * addresses with different options (IPv6), so we don't compare those bits
57 * either.
David Howells17926a72007-04-26 15:48:28 -070058 */
David Howells4f95dd72016-04-04 14:00:35 +010059static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
60 const struct sockaddr_rxrpc *srx)
61{
62 long diff;
63
64 diff = ((local->srx.transport_type - srx->transport_type) ?:
65 (local->srx.transport_len - srx->transport_len) ?:
66 (local->srx.transport.family - srx->transport.family));
67 if (diff != 0)
68 return diff;
69
70 switch (srx->transport.family) {
71 case AF_INET:
72 /* If the choice of UDP port is left up to the transport, then
73 * the endpoint record doesn't match.
74 */
75 return ((u16 __force)local->srx.transport.sin.sin_port -
76 (u16 __force)srx->transport.sin.sin_port) ?:
77 memcmp(&local->srx.transport.sin.sin_addr,
78 &srx->transport.sin.sin_addr,
79 sizeof(struct in_addr));
David Howellsd1912742016-09-17 07:26:01 +010080#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +010081 case AF_INET6:
82 /* If the choice of UDP6 port is left up to the transport, then
83 * the endpoint record doesn't match.
84 */
85 return ((u16 __force)local->srx.transport.sin6.sin6_port -
86 (u16 __force)srx->transport.sin6.sin6_port) ?:
87 memcmp(&local->srx.transport.sin6.sin6_addr,
88 &srx->transport.sin6.sin6_addr,
89 sizeof(struct in6_addr));
David Howellsd1912742016-09-17 07:26:01 +010090#endif
David Howells4f95dd72016-04-04 14:00:35 +010091 default:
92 BUG();
93 }
94}
95
David Howells0d6bf312022-11-02 16:46:13 +000096static void rxrpc_client_conn_reap_timeout(struct timer_list *timer)
97{
98 struct rxrpc_local *local =
99 container_of(timer, struct rxrpc_local, client_conn_reap_timer);
100
David Howells61e4a862023-10-27 00:49:34 +0100101 if (!local->kill_all_client_conns &&
David Howells0d6bf312022-11-02 16:46:13 +0000102 test_and_set_bit(RXRPC_CLIENT_CONN_REAP_TIMER, &local->client_conn_flags))
103 rxrpc_wake_up_io_thread(local);
104}
105
David Howells4f95dd72016-04-04 14:00:35 +0100106/*
107 * Allocate a new local endpoint.
108 */
David Howells8a758d92022-10-20 23:17:06 +0100109static struct rxrpc_local *rxrpc_alloc_local(struct net *net,
David Howells2baec2c2017-05-24 17:02:32 +0100110 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700111{
112 struct rxrpc_local *local;
David Howellsf06cb292022-10-20 22:58:56 +0100113 u32 tmp;
David Howells17926a72007-04-26 15:48:28 -0700114
115 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
116 if (local) {
David Howellsa05754292022-05-21 08:45:22 +0100117 refcount_set(&local->ref, 1);
David Howells730c5fd2019-08-09 15:20:41 +0100118 atomic_set(&local->active_users, 1);
David Howells8a758d92022-10-20 23:17:06 +0100119 local->net = net;
120 local->rxnet = rxrpc_net(net);
David Howells33912c22022-05-21 08:45:15 +0100121 INIT_HLIST_NODE(&local->link);
David Howells8fbcc832022-12-15 16:20:13 +0000122 init_completion(&local->io_thread_ready);
David Howellsaf094822022-10-17 08:54:57 +0100123#ifdef CONFIG_AF_RXRPC_INJECT_RX_DELAY
124 skb_queue_head_init(&local->rx_delay_queue);
125#endif
David Howellsa275da62022-10-10 08:45:20 +0100126 skb_queue_head_init(&local->rx_queue);
David Howellsf2cce892022-10-20 09:08:34 +0100127 INIT_LIST_HEAD(&local->conn_attend_q);
David Howells15f661d2022-10-10 15:51:39 +0100128 INIT_LIST_HEAD(&local->call_attend_q);
David Howells0d6bf312022-11-02 16:46:13 +0000129
David Howells245500d2020-07-01 11:15:32 +0100130 local->client_bundles = RB_ROOT;
131 spin_lock_init(&local->client_bundles_lock);
David Howells0d6bf312022-11-02 16:46:13 +0000132 local->kill_all_client_conns = false;
David Howells0d6bf312022-11-02 16:46:13 +0000133 INIT_LIST_HEAD(&local->idle_client_conns);
134 timer_setup(&local->client_conn_reap_timer,
135 rxrpc_client_conn_reap_timeout, 0);
136
David Howells17926a72007-04-26 15:48:28 -0700137 spin_lock_init(&local->lock);
138 rwlock_init(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700139 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
140 memcpy(&local->srx, srx, sizeof(*srx));
David Howells28036f42017-06-05 14:30:49 +0100141 local->srx.srx_service = 0;
David Howellsf06cb292022-10-20 22:58:56 +0100142 idr_init(&local->conn_ids);
143 get_random_bytes(&tmp, sizeof(tmp));
144 tmp &= 0x3fffffff;
145 if (tmp == 0)
146 tmp = 1;
147 idr_set_cursor(&local->conn_ids, tmp);
David Howells9d35d882022-10-19 09:45:43 +0100148 INIT_LIST_HEAD(&local->new_client_calls);
149 spin_lock_init(&local->client_call_lock);
David Howellsf06cb292022-10-20 22:58:56 +0100150
David Howells0fde8822022-10-21 13:00:34 +0100151 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, 1);
David Howells17926a72007-04-26 15:48:28 -0700152 }
153
154 _leave(" = %p", local);
155 return local;
156}
157
158/*
159 * create the local socket
David Howells4f95dd72016-04-04 14:00:35 +0100160 * - must be called with rxrpc_local_mutex locked
David Howells17926a72007-04-26 15:48:28 -0700161 */
David Howells2baec2c2017-05-24 17:02:32 +0100162static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
David Howells17926a72007-04-26 15:48:28 -0700163{
Xin Long1a9b86c2021-02-07 16:23:14 +0800164 struct udp_tunnel_sock_cfg tuncfg = {NULL};
165 struct sockaddr_rxrpc *srx = &local->srx;
166 struct udp_port_cfg udp_conf = {0};
David Howellsa275da62022-10-10 08:45:20 +0100167 struct task_struct *io_thread;
David Howells52719532018-10-04 11:10:51 +0100168 struct sock *usk;
Christoph Hellwigfce93492020-05-28 07:12:32 +0200169 int ret;
David Howells17926a72007-04-26 15:48:28 -0700170
David Howells75b54cb2016-09-13 08:49:05 +0100171 _enter("%p{%d,%d}",
Xin Long1a9b86c2021-02-07 16:23:14 +0800172 local, srx->transport_type, srx->transport.family);
David Howells17926a72007-04-26 15:48:28 -0700173
Xin Long1a9b86c2021-02-07 16:23:14 +0800174 udp_conf.family = srx->transport.family;
David Howells39cb9fa2022-04-29 21:05:16 +0100175 udp_conf.use_udp_checksums = true;
Xin Long1a9b86c2021-02-07 16:23:14 +0800176 if (udp_conf.family == AF_INET) {
177 udp_conf.local_ip = srx->transport.sin.sin_addr;
178 udp_conf.local_udp_port = srx->transport.sin.sin_port;
Vadim Fedorenko295f8302021-02-12 13:48:14 +0300179#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
Xin Long1a9b86c2021-02-07 16:23:14 +0800180 } else {
181 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
182 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
David Howells39cb9fa2022-04-29 21:05:16 +0100183 udp_conf.use_udp6_tx_checksums = true;
184 udp_conf.use_udp6_rx_checksums = true;
Vadim Fedorenko295f8302021-02-12 13:48:14 +0300185#endif
Xin Long1a9b86c2021-02-07 16:23:14 +0800186 }
187 ret = udp_sock_create(net, &udp_conf, &local->socket);
David Howells17926a72007-04-26 15:48:28 -0700188 if (ret < 0) {
189 _leave(" = %d [socket]", ret);
190 return ret;
191 }
192
Xin Long1a9b86c2021-02-07 16:23:14 +0800193 tuncfg.encap_type = UDP_ENCAP_RXRPC;
David Howells446b3e12022-10-10 10:55:24 +0100194 tuncfg.encap_rcv = rxrpc_encap_rcv;
David Howellsac56a0b2022-08-26 15:39:28 +0100195 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
Xin Long1a9b86c2021-02-07 16:23:14 +0800196 tuncfg.sk_user_data = local;
197 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
198
David Howells2cfa2272018-10-05 14:05:35 +0100199 /* set the socket up */
David Howells52719532018-10-04 11:10:51 +0100200 usk = local->socket->sk;
David Howells52719532018-10-04 11:10:51 +0100201 usk->sk_error_report = rxrpc_error_report;
David Howells2cfa2272018-10-05 14:05:35 +0100202
Xin Long1a9b86c2021-02-07 16:23:14 +0800203 switch (srx->transport.family) {
David Howells37a675e2018-09-27 15:13:09 +0100204 case AF_INET6:
205 /* we want to receive ICMPv6 errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800206 ip6_sock_set_recverr(usk);
David Howells37a675e2018-09-27 15:13:09 +0100207
David Howells37a675e2018-09-27 15:13:09 +0100208 /* Fall through and set IPv4 options too otherwise we don't get
209 * errors from IPv4 packets sent through the IPv6 socket.
210 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500211 fallthrough;
David Howellsf2aeed32018-05-10 23:26:00 +0100212 case AF_INET:
213 /* we want to receive ICMP errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800214 ip_sock_set_recverr(usk);
David Howells17926a72007-04-26 15:48:28 -0700215
David Howellsf2aeed32018-05-10 23:26:00 +0100216 /* we want to set the don't fragment bit */
David Howells87220142024-01-09 15:10:48 +0000217 rxrpc_local_dont_fragment(local, true);
David Howellsb604dd92018-09-27 15:13:08 +0100218
219 /* We want receive timestamps. */
Xin Long1a9b86c2021-02-07 16:23:14 +0800220 sock_enable_timestamps(usk);
David Howellsf2aeed32018-05-10 23:26:00 +0100221 break;
222
223 default:
224 BUG();
David Howells17926a72007-04-26 15:48:28 -0700225 }
226
David Howellsa275da62022-10-10 08:45:20 +0100227 io_thread = kthread_run(rxrpc_io_thread, local,
228 "krxrpcio/%u", ntohs(udp_conf.local_udp_port));
229 if (IS_ERR(io_thread)) {
230 ret = PTR_ERR(io_thread);
231 goto error_sock;
232 }
233
David Howells8fbcc832022-12-15 16:20:13 +0000234 wait_for_completion(&local->io_thread_ready);
David Howellsa275da62022-10-10 08:45:20 +0100235 local->io_thread = io_thread;
David Howells17926a72007-04-26 15:48:28 -0700236 _leave(" = 0");
237 return 0;
David Howellsa275da62022-10-10 08:45:20 +0100238
239error_sock:
240 kernel_sock_shutdown(local->socket, SHUT_RDWR);
241 local->socket->sk->sk_user_data = NULL;
242 sock_release(local->socket);
243 local->socket = NULL;
244 return ret;
David Howells17926a72007-04-26 15:48:28 -0700245}
246
247/*
David Howells4f95dd72016-04-04 14:00:35 +0100248 * Look up or create a new local endpoint using the specified local address.
David Howells17926a72007-04-26 15:48:28 -0700249 */
David Howells2baec2c2017-05-24 17:02:32 +0100250struct rxrpc_local *rxrpc_lookup_local(struct net *net,
251 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700252{
253 struct rxrpc_local *local;
David Howells2baec2c2017-05-24 17:02:32 +0100254 struct rxrpc_net *rxnet = rxrpc_net(net);
David Howells33912c22022-05-21 08:45:15 +0100255 struct hlist_node *cursor;
David Howells4f95dd72016-04-04 14:00:35 +0100256 long diff;
David Howells17926a72007-04-26 15:48:28 -0700257 int ret;
258
David Howells75b54cb2016-09-13 08:49:05 +0100259 _enter("{%d,%d,%pISp}",
260 srx->transport_type, srx->transport.family, &srx->transport);
David Howells17926a72007-04-26 15:48:28 -0700261
David Howells2baec2c2017-05-24 17:02:32 +0100262 mutex_lock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700263
David Howells33912c22022-05-21 08:45:15 +0100264 hlist_for_each(cursor, &rxnet->local_endpoints) {
265 local = hlist_entry(cursor, struct rxrpc_local, link);
David Howells17926a72007-04-26 15:48:28 -0700266
David Howells4f95dd72016-04-04 14:00:35 +0100267 diff = rxrpc_local_cmp_key(local, srx);
David Howells33912c22022-05-21 08:45:15 +0100268 if (diff != 0)
David Howells17926a72007-04-26 15:48:28 -0700269 continue;
270
David Howells4f95dd72016-04-04 14:00:35 +0100271 /* Services aren't allowed to share transport sockets, so
272 * reject that here. It is possible that the object is dying -
273 * but it may also still have the local transport address that
274 * we want bound.
275 */
276 if (srx->srx_service) {
277 local = NULL;
278 goto addr_in_use;
David Howells17926a72007-04-26 15:48:28 -0700279 }
David Howells4f95dd72016-04-04 14:00:35 +0100280
David Howells33912c22022-05-21 08:45:15 +0100281 /* Found a match. We want to replace a dying object.
282 * Attempting to bind the transport socket may still fail if
283 * we're attempting to use a local address that the dying
284 * object is still using.
David Howells4f95dd72016-04-04 14:00:35 +0100285 */
David Howells0fde8822022-10-21 13:00:34 +0100286 if (!rxrpc_use_local(local, rxrpc_local_use_lookup))
David Howells4f95dd72016-04-04 14:00:35 +0100287 break;
David Howells4f95dd72016-04-04 14:00:35 +0100288
David Howells4f95dd72016-04-04 14:00:35 +0100289 goto found;
David Howells17926a72007-04-26 15:48:28 -0700290 }
291
David Howells8a758d92022-10-20 23:17:06 +0100292 local = rxrpc_alloc_local(net, srx);
David Howells4f95dd72016-04-04 14:00:35 +0100293 if (!local)
294 goto nomem;
David Howells17926a72007-04-26 15:48:28 -0700295
David Howells2baec2c2017-05-24 17:02:32 +0100296 ret = rxrpc_open_socket(local, net);
David Howells4f95dd72016-04-04 14:00:35 +0100297 if (ret < 0)
298 goto sock_error;
David Howells17926a72007-04-26 15:48:28 -0700299
David Howells33912c22022-05-21 08:45:15 +0100300 if (cursor) {
301 hlist_replace_rcu(cursor, &local->link);
302 cursor->pprev = NULL;
303 } else {
304 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
305 }
David Howells17926a72007-04-26 15:48:28 -0700306
David Howells4f95dd72016-04-04 14:00:35 +0100307found:
David Howells2baec2c2017-05-24 17:02:32 +0100308 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100309 _leave(" = %p", local);
David Howells17926a72007-04-26 15:48:28 -0700310 return local;
311
David Howells4f95dd72016-04-04 14:00:35 +0100312nomem:
313 ret = -ENOMEM;
314sock_error:
David Howells2baec2c2017-05-24 17:02:32 +0100315 mutex_unlock(&rxnet->local_mutex);
Eric Dumazet032be5f2019-04-24 09:44:11 -0700316 if (local)
317 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells4f95dd72016-04-04 14:00:35 +0100318 _leave(" = %d", ret);
319 return ERR_PTR(ret);
David Howells17926a72007-04-26 15:48:28 -0700320
David Howells4f95dd72016-04-04 14:00:35 +0100321addr_in_use:
David Howells2baec2c2017-05-24 17:02:32 +0100322 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100323 _leave(" = -EADDRINUSE");
324 return ERR_PTR(-EADDRINUSE);
David Howells17926a72007-04-26 15:48:28 -0700325}
326
327/*
David Howells09d2bf52018-03-30 21:05:28 +0100328 * Get a ref on a local endpoint.
329 */
David Howells0fde8822022-10-21 13:00:34 +0100330struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local,
331 enum rxrpc_local_trace why)
David Howells09d2bf52018-03-30 21:05:28 +0100332{
David Howells0fde8822022-10-21 13:00:34 +0100333 int r, u;
David Howells09d2bf52018-03-30 21:05:28 +0100334
David Howells0fde8822022-10-21 13:00:34 +0100335 u = atomic_read(&local->active_users);
David Howellsa05754292022-05-21 08:45:22 +0100336 __refcount_inc(&local->ref, &r);
David Howells0fde8822022-10-21 13:00:34 +0100337 trace_rxrpc_local(local->debug_id, why, r + 1, u);
David Howells09d2bf52018-03-30 21:05:28 +0100338 return local;
339}
340
341/*
342 * Get a ref on a local endpoint unless its usage has already reached 0.
343 */
David Howells0fde8822022-10-21 13:00:34 +0100344struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local,
345 enum rxrpc_local_trace why)
David Howells09d2bf52018-03-30 21:05:28 +0100346{
David Howells0fde8822022-10-21 13:00:34 +0100347 int r, u;
David Howells09d2bf52018-03-30 21:05:28 +0100348
David Howells0fde8822022-10-21 13:00:34 +0100349 if (local && __refcount_inc_not_zero(&local->ref, &r)) {
350 u = atomic_read(&local->active_users);
351 trace_rxrpc_local(local->debug_id, why, r + 1, u);
352 return local;
David Howells09d2bf52018-03-30 21:05:28 +0100353 }
David Howells0fde8822022-10-21 13:00:34 +0100354
355 return NULL;
David Howells09d2bf52018-03-30 21:05:28 +0100356}
357
358/*
David Howells09d2bf52018-03-30 21:05:28 +0100359 * Drop a ref on a local endpoint.
360 */
David Howells0fde8822022-10-21 13:00:34 +0100361void rxrpc_put_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
David Howells09d2bf52018-03-30 21:05:28 +0100362{
David Howellsfac20b92020-01-30 21:50:35 +0000363 unsigned int debug_id;
David Howellsa05754292022-05-21 08:45:22 +0100364 bool dead;
David Howells0fde8822022-10-21 13:00:34 +0100365 int r, u;
David Howells09d2bf52018-03-30 21:05:28 +0100366
367 if (local) {
David Howellsfac20b92020-01-30 21:50:35 +0000368 debug_id = local->debug_id;
369
David Howells0fde8822022-10-21 13:00:34 +0100370 u = atomic_read(&local->active_users);
David Howellsa05754292022-05-21 08:45:22 +0100371 dead = __refcount_dec_and_test(&local->ref, &r);
David Howells0fde8822022-10-21 13:00:34 +0100372 trace_rxrpc_local(debug_id, why, r, u);
David Howells09d2bf52018-03-30 21:05:28 +0100373
David Howellsa05754292022-05-21 08:45:22 +0100374 if (dead)
David Howells730c5fd2019-08-09 15:20:41 +0100375 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells09d2bf52018-03-30 21:05:28 +0100376 }
377}
378
379/*
David Howells730c5fd2019-08-09 15:20:41 +0100380 * Start using a local endpoint.
381 */
David Howells0fde8822022-10-21 13:00:34 +0100382struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local,
383 enum rxrpc_local_trace why)
David Howells730c5fd2019-08-09 15:20:41 +0100384{
David Howells0fde8822022-10-21 13:00:34 +0100385 local = rxrpc_get_local_maybe(local, rxrpc_local_get_for_use);
David Howells730c5fd2019-08-09 15:20:41 +0100386 if (!local)
387 return NULL;
388
David Howells0fde8822022-10-21 13:00:34 +0100389 if (!__rxrpc_use_local(local, why)) {
390 rxrpc_put_local(local, rxrpc_local_put_for_use);
David Howells730c5fd2019-08-09 15:20:41 +0100391 return NULL;
392 }
393
394 return local;
395}
396
397/*
398 * Cease using a local endpoint. Once the number of active users reaches 0, we
David Howells5e6ef4f2020-01-23 13:13:41 +0000399 * start the closure of the transport in the I/O thread..
David Howells730c5fd2019-08-09 15:20:41 +0100400 */
David Howells0fde8822022-10-21 13:00:34 +0100401void rxrpc_unuse_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
David Howells730c5fd2019-08-09 15:20:41 +0100402{
David Howellseaa02392022-12-15 16:20:04 +0000403 unsigned int debug_id;
David Howellsa2cf3262022-11-16 12:02:22 +0000404 int r, u;
405
406 if (local) {
David Howellseaa02392022-12-15 16:20:04 +0000407 debug_id = local->debug_id;
David Howellsa2cf3262022-11-16 12:02:22 +0000408 r = refcount_read(&local->ref);
409 u = atomic_dec_return(&local->active_users);
410 trace_rxrpc_local(debug_id, why, r, u);
411 if (u == 0)
412 kthread_stop(local->io_thread);
413 }
David Howells730c5fd2019-08-09 15:20:41 +0100414}
415
416/*
David Howells4f95dd72016-04-04 14:00:35 +0100417 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
418 * of.
419 *
420 * Closing the socket cannot be done from bottom half context or RCU callback
421 * context because it might sleep.
David Howells17926a72007-04-26 15:48:28 -0700422 */
David Howellsa275da62022-10-10 08:45:20 +0100423void rxrpc_destroy_local(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700424{
David Howells4f95dd72016-04-04 14:00:35 +0100425 struct socket *socket = local->socket;
David Howells2baec2c2017-05-24 17:02:32 +0100426 struct rxrpc_net *rxnet = local->rxnet;
David Howells17926a72007-04-26 15:48:28 -0700427
David Howells4f95dd72016-04-04 14:00:35 +0100428 _enter("%d", local->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700429
David Howellsd12040b2019-08-29 14:12:11 +0100430 local->dead = true;
431
David Howells2baec2c2017-05-24 17:02:32 +0100432 mutex_lock(&rxnet->local_mutex);
David Howells33912c22022-05-21 08:45:15 +0100433 hlist_del_init_rcu(&local->link);
David Howells2baec2c2017-05-24 17:02:32 +0100434 mutex_unlock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700435
David Howellsd12040b2019-08-29 14:12:11 +0100436 rxrpc_clean_up_local_conns(local);
437 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
David Howells1e9e5c92016-09-29 22:37:15 +0100438 ASSERT(!local->service);
David Howells17926a72007-04-26 15:48:28 -0700439
David Howells4f95dd72016-04-04 14:00:35 +0100440 if (socket) {
441 local->socket = NULL;
442 kernel_sock_shutdown(socket, SHUT_RDWR);
443 socket->sk->sk_user_data = NULL;
444 sock_release(socket);
445 }
446
447 /* At this point, there should be no more packets coming in to the
448 * local endpoint.
449 */
David Howellsaf094822022-10-17 08:54:57 +0100450#ifdef CONFIG_AF_RXRPC_INJECT_RX_DELAY
451 rxrpc_purge_queue(&local->rx_delay_queue);
452#endif
David Howellsa275da62022-10-10 08:45:20 +0100453 rxrpc_purge_queue(&local->rx_queue);
David Howells9d35d882022-10-19 09:45:43 +0100454 rxrpc_purge_client_connections(local);
David Howells49489bb2024-01-29 23:47:57 +0000455 if (local->tx_alloc.va)
456 __page_frag_cache_drain(virt_to_page(local->tx_alloc.va),
457 local->tx_alloc.pagecnt_bias);
David Howells4f95dd72016-04-04 14:00:35 +0100458}
459
460/*
David Howells4f95dd72016-04-04 14:00:35 +0100461 * Destroy a local endpoint after the RCU grace period expires.
462 */
463static void rxrpc_local_rcu(struct rcu_head *rcu)
464{
465 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
466
David Howells0fde8822022-10-21 13:00:34 +0100467 rxrpc_see_local(local, rxrpc_local_free);
David Howells17926a72007-04-26 15:48:28 -0700468 kfree(local);
David Howells17926a72007-04-26 15:48:28 -0700469}
470
471/*
David Howells4f95dd72016-04-04 14:00:35 +0100472 * Verify the local endpoint list is empty by this point.
David Howells17926a72007-04-26 15:48:28 -0700473 */
David Howells2baec2c2017-05-24 17:02:32 +0100474void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
David Howells17926a72007-04-26 15:48:28 -0700475{
David Howells4f95dd72016-04-04 14:00:35 +0100476 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700477
478 _enter("");
479
David Howellsdee46362016-06-27 17:11:19 +0100480 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700481
David Howells33912c22022-05-21 08:45:15 +0100482 if (!hlist_empty(&rxnet->local_endpoints)) {
David Howells2baec2c2017-05-24 17:02:32 +0100483 mutex_lock(&rxnet->local_mutex);
David Howells33912c22022-05-21 08:45:15 +0100484 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
David Howellsdee46362016-06-27 17:11:19 +0100485 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
David Howellsa05754292022-05-21 08:45:22 +0100486 local, refcount_read(&local->ref));
David Howellsdee46362016-06-27 17:11:19 +0100487 }
David Howells2baec2c2017-05-24 17:02:32 +0100488 mutex_unlock(&rxnet->local_mutex);
David Howellsdee46362016-06-27 17:11:19 +0100489 BUG();
David Howells17926a72007-04-26 15:48:28 -0700490 }
David Howells17926a72007-04-26 15:48:28 -0700491}