blob: 546fd237a649dab00772a21675896f2b71bc3264 [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_processor(struct work_struct *);
24static void rxrpc_local_rcu(struct rcu_head *);
David Howells17926a72007-04-26 15:48:28 -070025
David Howells17926a72007-04-26 15:48:28 -070026/*
David Howells4f95dd72016-04-04 14:00:35 +010027 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
28 * same or greater than.
29 *
30 * We explicitly don't compare the RxRPC service ID as we want to reject
31 * conflicting uses by differing services. Further, we don't want to share
32 * addresses with different options (IPv6), so we don't compare those bits
33 * either.
David Howells17926a72007-04-26 15:48:28 -070034 */
David Howells4f95dd72016-04-04 14:00:35 +010035static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
36 const struct sockaddr_rxrpc *srx)
37{
38 long diff;
39
40 diff = ((local->srx.transport_type - srx->transport_type) ?:
41 (local->srx.transport_len - srx->transport_len) ?:
42 (local->srx.transport.family - srx->transport.family));
43 if (diff != 0)
44 return diff;
45
46 switch (srx->transport.family) {
47 case AF_INET:
48 /* If the choice of UDP port is left up to the transport, then
49 * the endpoint record doesn't match.
50 */
51 return ((u16 __force)local->srx.transport.sin.sin_port -
52 (u16 __force)srx->transport.sin.sin_port) ?:
53 memcmp(&local->srx.transport.sin.sin_addr,
54 &srx->transport.sin.sin_addr,
55 sizeof(struct in_addr));
David Howellsd1912742016-09-17 07:26:01 +010056#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +010057 case AF_INET6:
58 /* If the choice of UDP6 port is left up to the transport, then
59 * the endpoint record doesn't match.
60 */
61 return ((u16 __force)local->srx.transport.sin6.sin6_port -
62 (u16 __force)srx->transport.sin6.sin6_port) ?:
63 memcmp(&local->srx.transport.sin6.sin6_addr,
64 &srx->transport.sin6.sin6_addr,
65 sizeof(struct in6_addr));
David Howellsd1912742016-09-17 07:26:01 +010066#endif
David Howells4f95dd72016-04-04 14:00:35 +010067 default:
68 BUG();
69 }
70}
71
72/*
73 * Allocate a new local endpoint.
74 */
David Howells2baec2c2017-05-24 17:02:32 +010075static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
76 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -070077{
78 struct rxrpc_local *local;
79
80 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
81 if (local) {
David Howells4f95dd72016-04-04 14:00:35 +010082 atomic_set(&local->usage, 1);
David Howells730c5fd2019-08-09 15:20:41 +010083 atomic_set(&local->active_users, 1);
David Howells2baec2c2017-05-24 17:02:32 +010084 local->rxnet = rxnet;
David Howells17926a72007-04-26 15:48:28 -070085 INIT_LIST_HEAD(&local->link);
David Howells4f95dd72016-04-04 14:00:35 +010086 INIT_WORK(&local->processor, rxrpc_local_processor);
David Howells17926a72007-04-26 15:48:28 -070087 init_rwsem(&local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -070088 skb_queue_head_init(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +010089 skb_queue_head_init(&local->event_queue);
David Howells245500d2020-07-01 11:15:32 +010090 local->client_bundles = RB_ROOT;
91 spin_lock_init(&local->client_bundles_lock);
David Howells17926a72007-04-26 15:48:28 -070092 spin_lock_init(&local->lock);
93 rwlock_init(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -070094 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
95 memcpy(&local->srx, srx, sizeof(*srx));
David Howells28036f42017-06-05 14:30:49 +010096 local->srx.srx_service = 0;
David Howells06d95322019-08-13 22:26:36 +010097 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
David Howells17926a72007-04-26 15:48:28 -070098 }
99
100 _leave(" = %p", local);
101 return local;
102}
103
104/*
105 * create the local socket
David Howells4f95dd72016-04-04 14:00:35 +0100106 * - must be called with rxrpc_local_mutex locked
David Howells17926a72007-04-26 15:48:28 -0700107 */
David Howells2baec2c2017-05-24 17:02:32 +0100108static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
David Howells17926a72007-04-26 15:48:28 -0700109{
Xin Long1a9b86c2021-02-07 16:23:14 +0800110 struct udp_tunnel_sock_cfg tuncfg = {NULL};
111 struct sockaddr_rxrpc *srx = &local->srx;
112 struct udp_port_cfg udp_conf = {0};
David Howells52719532018-10-04 11:10:51 +0100113 struct sock *usk;
Christoph Hellwigfce93492020-05-28 07:12:32 +0200114 int ret;
David Howells17926a72007-04-26 15:48:28 -0700115
David Howells75b54cb2016-09-13 08:49:05 +0100116 _enter("%p{%d,%d}",
Xin Long1a9b86c2021-02-07 16:23:14 +0800117 local, srx->transport_type, srx->transport.family);
David Howells17926a72007-04-26 15:48:28 -0700118
Xin Long1a9b86c2021-02-07 16:23:14 +0800119 udp_conf.family = srx->transport.family;
120 if (udp_conf.family == AF_INET) {
121 udp_conf.local_ip = srx->transport.sin.sin_addr;
122 udp_conf.local_udp_port = srx->transport.sin.sin_port;
123 } else {
124 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
125 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
126 }
127 ret = udp_sock_create(net, &udp_conf, &local->socket);
David Howells17926a72007-04-26 15:48:28 -0700128 if (ret < 0) {
129 _leave(" = %d [socket]", ret);
130 return ret;
131 }
132
Xin Long1a9b86c2021-02-07 16:23:14 +0800133 tuncfg.encap_type = UDP_ENCAP_RXRPC;
134 tuncfg.encap_rcv = rxrpc_input_packet;
135 tuncfg.sk_user_data = local;
136 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
137
David Howells2cfa2272018-10-05 14:05:35 +0100138 /* set the socket up */
David Howells52719532018-10-04 11:10:51 +0100139 usk = local->socket->sk;
David Howells52719532018-10-04 11:10:51 +0100140 usk->sk_error_report = rxrpc_error_report;
David Howells2cfa2272018-10-05 14:05:35 +0100141
Xin Long1a9b86c2021-02-07 16:23:14 +0800142 switch (srx->transport.family) {
David Howells37a675e2018-09-27 15:13:09 +0100143 case AF_INET6:
144 /* we want to receive ICMPv6 errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800145 ip6_sock_set_recverr(usk);
David Howells37a675e2018-09-27 15:13:09 +0100146
David Howells37a675e2018-09-27 15:13:09 +0100147 /* Fall through and set IPv4 options too otherwise we don't get
148 * errors from IPv4 packets sent through the IPv6 socket.
149 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500150 fallthrough;
David Howellsf2aeed32018-05-10 23:26:00 +0100151 case AF_INET:
152 /* we want to receive ICMP errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800153 ip_sock_set_recverr(usk);
David Howells17926a72007-04-26 15:48:28 -0700154
David Howellsf2aeed32018-05-10 23:26:00 +0100155 /* we want to set the don't fragment bit */
Xin Long1a9b86c2021-02-07 16:23:14 +0800156 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
David Howellsb604dd92018-09-27 15:13:08 +0100157
158 /* We want receive timestamps. */
Xin Long1a9b86c2021-02-07 16:23:14 +0800159 sock_enable_timestamps(usk);
David Howellsf2aeed32018-05-10 23:26:00 +0100160 break;
161
162 default:
163 BUG();
David Howells17926a72007-04-26 15:48:28 -0700164 }
165
David Howells17926a72007-04-26 15:48:28 -0700166 _leave(" = 0");
167 return 0;
David Howells17926a72007-04-26 15:48:28 -0700168}
169
170/*
David Howells4f95dd72016-04-04 14:00:35 +0100171 * Look up or create a new local endpoint using the specified local address.
David Howells17926a72007-04-26 15:48:28 -0700172 */
David Howells2baec2c2017-05-24 17:02:32 +0100173struct rxrpc_local *rxrpc_lookup_local(struct net *net,
174 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700175{
176 struct rxrpc_local *local;
David Howells2baec2c2017-05-24 17:02:32 +0100177 struct rxrpc_net *rxnet = rxrpc_net(net);
David Howells4f95dd72016-04-04 14:00:35 +0100178 struct list_head *cursor;
179 const char *age;
180 long diff;
David Howells17926a72007-04-26 15:48:28 -0700181 int ret;
182
David Howells75b54cb2016-09-13 08:49:05 +0100183 _enter("{%d,%d,%pISp}",
184 srx->transport_type, srx->transport.family, &srx->transport);
David Howells17926a72007-04-26 15:48:28 -0700185
David Howells2baec2c2017-05-24 17:02:32 +0100186 mutex_lock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700187
David Howells2baec2c2017-05-24 17:02:32 +0100188 for (cursor = rxnet->local_endpoints.next;
189 cursor != &rxnet->local_endpoints;
David Howells4f95dd72016-04-04 14:00:35 +0100190 cursor = cursor->next) {
191 local = list_entry(cursor, struct rxrpc_local, link);
David Howells17926a72007-04-26 15:48:28 -0700192
David Howells4f95dd72016-04-04 14:00:35 +0100193 diff = rxrpc_local_cmp_key(local, srx);
194 if (diff < 0)
David Howells17926a72007-04-26 15:48:28 -0700195 continue;
David Howells4f95dd72016-04-04 14:00:35 +0100196 if (diff > 0)
197 break;
David Howells17926a72007-04-26 15:48:28 -0700198
David Howells4f95dd72016-04-04 14:00:35 +0100199 /* Services aren't allowed to share transport sockets, so
200 * reject that here. It is possible that the object is dying -
201 * but it may also still have the local transport address that
202 * we want bound.
203 */
204 if (srx->srx_service) {
205 local = NULL;
206 goto addr_in_use;
David Howells17926a72007-04-26 15:48:28 -0700207 }
David Howells4f95dd72016-04-04 14:00:35 +0100208
209 /* Found a match. We replace a dying object. Attempting to
210 * bind the transport socket may still fail if we're attempting
211 * to use a local address that the dying object is still using.
212 */
David Howells730c5fd2019-08-09 15:20:41 +0100213 if (!rxrpc_use_local(local))
David Howells4f95dd72016-04-04 14:00:35 +0100214 break;
David Howells4f95dd72016-04-04 14:00:35 +0100215
216 age = "old";
217 goto found;
David Howells17926a72007-04-26 15:48:28 -0700218 }
219
David Howells2baec2c2017-05-24 17:02:32 +0100220 local = rxrpc_alloc_local(rxnet, srx);
David Howells4f95dd72016-04-04 14:00:35 +0100221 if (!local)
222 goto nomem;
David Howells17926a72007-04-26 15:48:28 -0700223
David Howells2baec2c2017-05-24 17:02:32 +0100224 ret = rxrpc_open_socket(local, net);
David Howells4f95dd72016-04-04 14:00:35 +0100225 if (ret < 0)
226 goto sock_error;
David Howells17926a72007-04-26 15:48:28 -0700227
David Howells730c5fd2019-08-09 15:20:41 +0100228 if (cursor != &rxnet->local_endpoints)
David Howellsb00df842019-08-12 23:30:06 +0100229 list_replace_init(cursor, &local->link);
David Howells730c5fd2019-08-09 15:20:41 +0100230 else
231 list_add_tail(&local->link, cursor);
David Howells4f95dd72016-04-04 14:00:35 +0100232 age = "new";
David Howells17926a72007-04-26 15:48:28 -0700233
David Howells4f95dd72016-04-04 14:00:35 +0100234found:
David Howells2baec2c2017-05-24 17:02:32 +0100235 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100236
David Howells75b54cb2016-09-13 08:49:05 +0100237 _net("LOCAL %s %d {%pISp}",
238 age, local->debug_id, &local->srx.transport);
David Howells17926a72007-04-26 15:48:28 -0700239
David Howells4f95dd72016-04-04 14:00:35 +0100240 _leave(" = %p", local);
David Howells17926a72007-04-26 15:48:28 -0700241 return local;
242
David Howells4f95dd72016-04-04 14:00:35 +0100243nomem:
244 ret = -ENOMEM;
245sock_error:
David Howells2baec2c2017-05-24 17:02:32 +0100246 mutex_unlock(&rxnet->local_mutex);
Eric Dumazet032be5f2019-04-24 09:44:11 -0700247 if (local)
248 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells4f95dd72016-04-04 14:00:35 +0100249 _leave(" = %d", ret);
250 return ERR_PTR(ret);
David Howells17926a72007-04-26 15:48:28 -0700251
David Howells4f95dd72016-04-04 14:00:35 +0100252addr_in_use:
David Howells2baec2c2017-05-24 17:02:32 +0100253 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100254 _leave(" = -EADDRINUSE");
255 return ERR_PTR(-EADDRINUSE);
David Howells17926a72007-04-26 15:48:28 -0700256}
257
258/*
David Howells09d2bf52018-03-30 21:05:28 +0100259 * Get a ref on a local endpoint.
260 */
261struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
262{
263 const void *here = __builtin_return_address(0);
264 int n;
265
266 n = atomic_inc_return(&local->usage);
David Howells06d95322019-08-13 22:26:36 +0100267 trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
David Howells09d2bf52018-03-30 21:05:28 +0100268 return local;
269}
270
271/*
272 * Get a ref on a local endpoint unless its usage has already reached 0.
273 */
274struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
275{
276 const void *here = __builtin_return_address(0);
277
278 if (local) {
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100279 int n = atomic_fetch_add_unless(&local->usage, 1, 0);
David Howells09d2bf52018-03-30 21:05:28 +0100280 if (n > 0)
David Howells06d95322019-08-13 22:26:36 +0100281 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
282 n + 1, here);
David Howells09d2bf52018-03-30 21:05:28 +0100283 else
284 local = NULL;
285 }
286 return local;
287}
288
289/*
David Howells06d95322019-08-13 22:26:36 +0100290 * Queue a local endpoint and pass the caller's reference to the work item.
David Howells09d2bf52018-03-30 21:05:28 +0100291 */
292void rxrpc_queue_local(struct rxrpc_local *local)
293{
294 const void *here = __builtin_return_address(0);
David Howells06d95322019-08-13 22:26:36 +0100295 unsigned int debug_id = local->debug_id;
296 int n = atomic_read(&local->usage);
David Howells09d2bf52018-03-30 21:05:28 +0100297
298 if (rxrpc_queue_work(&local->processor))
David Howells06d95322019-08-13 22:26:36 +0100299 trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
David Howells730c5fd2019-08-09 15:20:41 +0100300 else
301 rxrpc_put_local(local);
David Howells17926a72007-04-26 15:48:28 -0700302}
303
304/*
David Howells09d2bf52018-03-30 21:05:28 +0100305 * Drop a ref on a local endpoint.
306 */
307void rxrpc_put_local(struct rxrpc_local *local)
308{
309 const void *here = __builtin_return_address(0);
David Howellsfac20b92020-01-30 21:50:35 +0000310 unsigned int debug_id;
David Howells09d2bf52018-03-30 21:05:28 +0100311 int n;
312
313 if (local) {
David Howellsfac20b92020-01-30 21:50:35 +0000314 debug_id = local->debug_id;
315
David Howells09d2bf52018-03-30 21:05:28 +0100316 n = atomic_dec_return(&local->usage);
David Howellsfac20b92020-01-30 21:50:35 +0000317 trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
David Howells09d2bf52018-03-30 21:05:28 +0100318
319 if (n == 0)
David Howells730c5fd2019-08-09 15:20:41 +0100320 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells09d2bf52018-03-30 21:05:28 +0100321 }
322}
323
324/*
David Howells730c5fd2019-08-09 15:20:41 +0100325 * Start using a local endpoint.
326 */
327struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
328{
David Howells730c5fd2019-08-09 15:20:41 +0100329 local = rxrpc_get_local_maybe(local);
330 if (!local)
331 return NULL;
332
David Howells04d36d72020-01-30 21:50:36 +0000333 if (!__rxrpc_use_local(local)) {
David Howells730c5fd2019-08-09 15:20:41 +0100334 rxrpc_put_local(local);
335 return NULL;
336 }
337
338 return local;
339}
340
341/*
342 * Cease using a local endpoint. Once the number of active users reaches 0, we
343 * start the closure of the transport in the work processor.
344 */
345void rxrpc_unuse_local(struct rxrpc_local *local)
346{
David Howells68553f12019-08-09 22:47:47 +0100347 if (local) {
David Howells04d36d72020-01-30 21:50:36 +0000348 if (__rxrpc_unuse_local(local)) {
349 rxrpc_get_local(local);
David Howells68553f12019-08-09 22:47:47 +0100350 rxrpc_queue_local(local);
David Howells04d36d72020-01-30 21:50:36 +0000351 }
David Howells68553f12019-08-09 22:47:47 +0100352 }
David Howells730c5fd2019-08-09 15:20:41 +0100353}
354
355/*
David Howells4f95dd72016-04-04 14:00:35 +0100356 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
357 * of.
358 *
359 * Closing the socket cannot be done from bottom half context or RCU callback
360 * context because it might sleep.
David Howells17926a72007-04-26 15:48:28 -0700361 */
David Howells4f95dd72016-04-04 14:00:35 +0100362static void rxrpc_local_destroyer(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700363{
David Howells4f95dd72016-04-04 14:00:35 +0100364 struct socket *socket = local->socket;
David Howells2baec2c2017-05-24 17:02:32 +0100365 struct rxrpc_net *rxnet = local->rxnet;
David Howells17926a72007-04-26 15:48:28 -0700366
David Howells4f95dd72016-04-04 14:00:35 +0100367 _enter("%d", local->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700368
David Howellsd12040b2019-08-29 14:12:11 +0100369 local->dead = true;
370
David Howells2baec2c2017-05-24 17:02:32 +0100371 mutex_lock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100372 list_del_init(&local->link);
David Howells2baec2c2017-05-24 17:02:32 +0100373 mutex_unlock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700374
David Howellsd12040b2019-08-29 14:12:11 +0100375 rxrpc_clean_up_local_conns(local);
376 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
David Howells1e9e5c92016-09-29 22:37:15 +0100377 ASSERT(!local->service);
David Howells17926a72007-04-26 15:48:28 -0700378
David Howells4f95dd72016-04-04 14:00:35 +0100379 if (socket) {
380 local->socket = NULL;
381 kernel_sock_shutdown(socket, SHUT_RDWR);
382 socket->sk->sk_user_data = NULL;
383 sock_release(socket);
384 }
385
386 /* At this point, there should be no more packets coming in to the
387 * local endpoint.
388 */
David Howells17926a72007-04-26 15:48:28 -0700389 rxrpc_purge_queue(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +0100390 rxrpc_purge_queue(&local->event_queue);
David Howells4f95dd72016-04-04 14:00:35 +0100391}
392
393/*
David Howells730c5fd2019-08-09 15:20:41 +0100394 * Process events on an endpoint. The work item carries a ref which
395 * we must release.
David Howells4f95dd72016-04-04 14:00:35 +0100396 */
397static void rxrpc_local_processor(struct work_struct *work)
398{
399 struct rxrpc_local *local =
400 container_of(work, struct rxrpc_local, processor);
401 bool again;
402
David Howells06d95322019-08-13 22:26:36 +0100403 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
David Howells09d2bf52018-03-30 21:05:28 +0100404 atomic_read(&local->usage), NULL);
David Howells4f95dd72016-04-04 14:00:35 +0100405
406 do {
407 again = false;
David Howells04d36d72020-01-30 21:50:36 +0000408 if (!__rxrpc_use_local(local)) {
David Howells730c5fd2019-08-09 15:20:41 +0100409 rxrpc_local_destroyer(local);
410 break;
411 }
David Howells4f95dd72016-04-04 14:00:35 +0100412
David Howells4f95dd72016-04-04 14:00:35 +0100413 if (!skb_queue_empty(&local->reject_queue)) {
414 rxrpc_reject_packets(local);
415 again = true;
416 }
417
418 if (!skb_queue_empty(&local->event_queue)) {
419 rxrpc_process_local_events(local);
420 again = true;
421 }
David Howells04d36d72020-01-30 21:50:36 +0000422
423 __rxrpc_unuse_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100424 } while (again);
David Howells730c5fd2019-08-09 15:20:41 +0100425
426 rxrpc_put_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100427}
428
429/*
430 * Destroy a local endpoint after the RCU grace period expires.
431 */
432static void rxrpc_local_rcu(struct rcu_head *rcu)
433{
434 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
435
436 _enter("%d", local->debug_id);
437
438 ASSERT(!work_pending(&local->processor));
David Howells17926a72007-04-26 15:48:28 -0700439
440 _net("DESTROY LOCAL %d", local->debug_id);
441 kfree(local);
David Howells17926a72007-04-26 15:48:28 -0700442 _leave("");
443}
444
445/*
David Howells4f95dd72016-04-04 14:00:35 +0100446 * Verify the local endpoint list is empty by this point.
David Howells17926a72007-04-26 15:48:28 -0700447 */
David Howells2baec2c2017-05-24 17:02:32 +0100448void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
David Howells17926a72007-04-26 15:48:28 -0700449{
David Howells4f95dd72016-04-04 14:00:35 +0100450 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700451
452 _enter("");
453
David Howellsdee46362016-06-27 17:11:19 +0100454 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700455
David Howells2baec2c2017-05-24 17:02:32 +0100456 if (!list_empty(&rxnet->local_endpoints)) {
457 mutex_lock(&rxnet->local_mutex);
458 list_for_each_entry(local, &rxnet->local_endpoints, link) {
David Howellsdee46362016-06-27 17:11:19 +0100459 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
460 local, atomic_read(&local->usage));
461 }
David Howells2baec2c2017-05-24 17:02:32 +0100462 mutex_unlock(&rxnet->local_mutex);
David Howellsdee46362016-06-27 17:11:19 +0100463 BUG();
David Howells17926a72007-04-26 15:48:28 -0700464 }
David Howells17926a72007-04-26 15:48:28 -0700465}