blob: 96ecb7356c0fedad3713b628d69f236d6e6684d5 [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 Howellsa05754292022-05-21 08:45:22 +010082 refcount_set(&local->ref, 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 Howells33912c22022-05-21 08:45:15 +010085 INIT_HLIST_NODE(&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;
David Howells39cb9fa2022-04-29 21:05:16 +0100120 udp_conf.use_udp_checksums = true;
Xin Long1a9b86c2021-02-07 16:23:14 +0800121 if (udp_conf.family == AF_INET) {
122 udp_conf.local_ip = srx->transport.sin.sin_addr;
123 udp_conf.local_udp_port = srx->transport.sin.sin_port;
Vadim Fedorenko295f8302021-02-12 13:48:14 +0300124#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
Xin Long1a9b86c2021-02-07 16:23:14 +0800125 } else {
126 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
127 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
David Howells39cb9fa2022-04-29 21:05:16 +0100128 udp_conf.use_udp6_tx_checksums = true;
129 udp_conf.use_udp6_rx_checksums = true;
Vadim Fedorenko295f8302021-02-12 13:48:14 +0300130#endif
Xin Long1a9b86c2021-02-07 16:23:14 +0800131 }
132 ret = udp_sock_create(net, &udp_conf, &local->socket);
David Howells17926a72007-04-26 15:48:28 -0700133 if (ret < 0) {
134 _leave(" = %d [socket]", ret);
135 return ret;
136 }
137
Xin Long1a9b86c2021-02-07 16:23:14 +0800138 tuncfg.encap_type = UDP_ENCAP_RXRPC;
139 tuncfg.encap_rcv = rxrpc_input_packet;
140 tuncfg.sk_user_data = local;
141 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
142
David Howells2cfa2272018-10-05 14:05:35 +0100143 /* set the socket up */
David Howells52719532018-10-04 11:10:51 +0100144 usk = local->socket->sk;
David Howells52719532018-10-04 11:10:51 +0100145 usk->sk_error_report = rxrpc_error_report;
David Howells2cfa2272018-10-05 14:05:35 +0100146
Xin Long1a9b86c2021-02-07 16:23:14 +0800147 switch (srx->transport.family) {
David Howells37a675e2018-09-27 15:13:09 +0100148 case AF_INET6:
149 /* we want to receive ICMPv6 errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800150 ip6_sock_set_recverr(usk);
David Howells37a675e2018-09-27 15:13:09 +0100151
David Howells37a675e2018-09-27 15:13:09 +0100152 /* Fall through and set IPv4 options too otherwise we don't get
153 * errors from IPv4 packets sent through the IPv6 socket.
154 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500155 fallthrough;
David Howellsf2aeed32018-05-10 23:26:00 +0100156 case AF_INET:
157 /* we want to receive ICMP errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800158 ip_sock_set_recverr(usk);
David Howells17926a72007-04-26 15:48:28 -0700159
David Howellsf2aeed32018-05-10 23:26:00 +0100160 /* we want to set the don't fragment bit */
Xin Long1a9b86c2021-02-07 16:23:14 +0800161 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
David Howellsb604dd92018-09-27 15:13:08 +0100162
163 /* We want receive timestamps. */
Xin Long1a9b86c2021-02-07 16:23:14 +0800164 sock_enable_timestamps(usk);
David Howellsf2aeed32018-05-10 23:26:00 +0100165 break;
166
167 default:
168 BUG();
David Howells17926a72007-04-26 15:48:28 -0700169 }
170
David Howells17926a72007-04-26 15:48:28 -0700171 _leave(" = 0");
172 return 0;
David Howells17926a72007-04-26 15:48:28 -0700173}
174
175/*
David Howells4f95dd72016-04-04 14:00:35 +0100176 * Look up or create a new local endpoint using the specified local address.
David Howells17926a72007-04-26 15:48:28 -0700177 */
David Howells2baec2c2017-05-24 17:02:32 +0100178struct rxrpc_local *rxrpc_lookup_local(struct net *net,
179 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700180{
181 struct rxrpc_local *local;
David Howells2baec2c2017-05-24 17:02:32 +0100182 struct rxrpc_net *rxnet = rxrpc_net(net);
David Howells33912c22022-05-21 08:45:15 +0100183 struct hlist_node *cursor;
David Howells4f95dd72016-04-04 14:00:35 +0100184 const char *age;
185 long diff;
David Howells17926a72007-04-26 15:48:28 -0700186 int ret;
187
David Howells75b54cb2016-09-13 08:49:05 +0100188 _enter("{%d,%d,%pISp}",
189 srx->transport_type, srx->transport.family, &srx->transport);
David Howells17926a72007-04-26 15:48:28 -0700190
David Howells2baec2c2017-05-24 17:02:32 +0100191 mutex_lock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700192
David Howells33912c22022-05-21 08:45:15 +0100193 hlist_for_each(cursor, &rxnet->local_endpoints) {
194 local = hlist_entry(cursor, struct rxrpc_local, link);
David Howells17926a72007-04-26 15:48:28 -0700195
David Howells4f95dd72016-04-04 14:00:35 +0100196 diff = rxrpc_local_cmp_key(local, srx);
David Howells33912c22022-05-21 08:45:15 +0100197 if (diff != 0)
David Howells17926a72007-04-26 15:48:28 -0700198 continue;
199
David Howells4f95dd72016-04-04 14:00:35 +0100200 /* Services aren't allowed to share transport sockets, so
201 * reject that here. It is possible that the object is dying -
202 * but it may also still have the local transport address that
203 * we want bound.
204 */
205 if (srx->srx_service) {
206 local = NULL;
207 goto addr_in_use;
David Howells17926a72007-04-26 15:48:28 -0700208 }
David Howells4f95dd72016-04-04 14:00:35 +0100209
David Howells33912c22022-05-21 08:45:15 +0100210 /* Found a match. We want to replace a dying object.
211 * Attempting to bind the transport socket may still fail if
212 * we're attempting to use a local address that the dying
213 * object is still using.
David Howells4f95dd72016-04-04 14:00:35 +0100214 */
David Howells730c5fd2019-08-09 15:20:41 +0100215 if (!rxrpc_use_local(local))
David Howells4f95dd72016-04-04 14:00:35 +0100216 break;
David Howells4f95dd72016-04-04 14:00:35 +0100217
218 age = "old";
219 goto found;
David Howells17926a72007-04-26 15:48:28 -0700220 }
221
David Howells2baec2c2017-05-24 17:02:32 +0100222 local = rxrpc_alloc_local(rxnet, srx);
David Howells4f95dd72016-04-04 14:00:35 +0100223 if (!local)
224 goto nomem;
David Howells17926a72007-04-26 15:48:28 -0700225
David Howells2baec2c2017-05-24 17:02:32 +0100226 ret = rxrpc_open_socket(local, net);
David Howells4f95dd72016-04-04 14:00:35 +0100227 if (ret < 0)
228 goto sock_error;
David Howells17926a72007-04-26 15:48:28 -0700229
David Howells33912c22022-05-21 08:45:15 +0100230 if (cursor) {
231 hlist_replace_rcu(cursor, &local->link);
232 cursor->pprev = NULL;
233 } else {
234 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
235 }
David Howells4f95dd72016-04-04 14:00:35 +0100236 age = "new";
David Howells17926a72007-04-26 15:48:28 -0700237
David Howells4f95dd72016-04-04 14:00:35 +0100238found:
David Howells2baec2c2017-05-24 17:02:32 +0100239 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100240
David Howells75b54cb2016-09-13 08:49:05 +0100241 _net("LOCAL %s %d {%pISp}",
242 age, local->debug_id, &local->srx.transport);
David Howells17926a72007-04-26 15:48:28 -0700243
David Howells4f95dd72016-04-04 14:00:35 +0100244 _leave(" = %p", local);
David Howells17926a72007-04-26 15:48:28 -0700245 return local;
246
David Howells4f95dd72016-04-04 14:00:35 +0100247nomem:
248 ret = -ENOMEM;
249sock_error:
David Howells2baec2c2017-05-24 17:02:32 +0100250 mutex_unlock(&rxnet->local_mutex);
Eric Dumazet032be5f2019-04-24 09:44:11 -0700251 if (local)
252 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells4f95dd72016-04-04 14:00:35 +0100253 _leave(" = %d", ret);
254 return ERR_PTR(ret);
David Howells17926a72007-04-26 15:48:28 -0700255
David Howells4f95dd72016-04-04 14:00:35 +0100256addr_in_use:
David Howells2baec2c2017-05-24 17:02:32 +0100257 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100258 _leave(" = -EADDRINUSE");
259 return ERR_PTR(-EADDRINUSE);
David Howells17926a72007-04-26 15:48:28 -0700260}
261
262/*
David Howells09d2bf52018-03-30 21:05:28 +0100263 * Get a ref on a local endpoint.
264 */
265struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
266{
267 const void *here = __builtin_return_address(0);
David Howellsa05754292022-05-21 08:45:22 +0100268 int r;
David Howells09d2bf52018-03-30 21:05:28 +0100269
David Howellsa05754292022-05-21 08:45:22 +0100270 __refcount_inc(&local->ref, &r);
271 trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
David Howells09d2bf52018-03-30 21:05:28 +0100272 return local;
273}
274
275/*
276 * Get a ref on a local endpoint unless its usage has already reached 0.
277 */
278struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
279{
280 const void *here = __builtin_return_address(0);
David Howellsa05754292022-05-21 08:45:22 +0100281 int r;
David Howells09d2bf52018-03-30 21:05:28 +0100282
283 if (local) {
David Howellsa05754292022-05-21 08:45:22 +0100284 if (__refcount_inc_not_zero(&local->ref, &r))
David Howells06d95322019-08-13 22:26:36 +0100285 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
David Howellsa05754292022-05-21 08:45:22 +0100286 r + 1, here);
David Howells09d2bf52018-03-30 21:05:28 +0100287 else
288 local = NULL;
289 }
290 return local;
291}
292
293/*
David Howells06d95322019-08-13 22:26:36 +0100294 * Queue a local endpoint and pass the caller's reference to the work item.
David Howells09d2bf52018-03-30 21:05:28 +0100295 */
296void rxrpc_queue_local(struct rxrpc_local *local)
297{
298 const void *here = __builtin_return_address(0);
David Howells06d95322019-08-13 22:26:36 +0100299 unsigned int debug_id = local->debug_id;
David Howellsa05754292022-05-21 08:45:22 +0100300 int r = refcount_read(&local->ref);
David Howells09d2bf52018-03-30 21:05:28 +0100301
302 if (rxrpc_queue_work(&local->processor))
David Howellsa05754292022-05-21 08:45:22 +0100303 trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
David Howells730c5fd2019-08-09 15:20:41 +0100304 else
305 rxrpc_put_local(local);
David Howells17926a72007-04-26 15:48:28 -0700306}
307
308/*
David Howells09d2bf52018-03-30 21:05:28 +0100309 * Drop a ref on a local endpoint.
310 */
311void rxrpc_put_local(struct rxrpc_local *local)
312{
313 const void *here = __builtin_return_address(0);
David Howellsfac20b92020-01-30 21:50:35 +0000314 unsigned int debug_id;
David Howellsa05754292022-05-21 08:45:22 +0100315 bool dead;
316 int r;
David Howells09d2bf52018-03-30 21:05:28 +0100317
318 if (local) {
David Howellsfac20b92020-01-30 21:50:35 +0000319 debug_id = local->debug_id;
320
David Howellsa05754292022-05-21 08:45:22 +0100321 dead = __refcount_dec_and_test(&local->ref, &r);
322 trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
David Howells09d2bf52018-03-30 21:05:28 +0100323
David Howellsa05754292022-05-21 08:45:22 +0100324 if (dead)
David Howells730c5fd2019-08-09 15:20:41 +0100325 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells09d2bf52018-03-30 21:05:28 +0100326 }
327}
328
329/*
David Howells730c5fd2019-08-09 15:20:41 +0100330 * Start using a local endpoint.
331 */
332struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
333{
David Howells730c5fd2019-08-09 15:20:41 +0100334 local = rxrpc_get_local_maybe(local);
335 if (!local)
336 return NULL;
337
David Howells04d36d72020-01-30 21:50:36 +0000338 if (!__rxrpc_use_local(local)) {
David Howells730c5fd2019-08-09 15:20:41 +0100339 rxrpc_put_local(local);
340 return NULL;
341 }
342
343 return local;
344}
345
346/*
347 * Cease using a local endpoint. Once the number of active users reaches 0, we
348 * start the closure of the transport in the work processor.
349 */
350void rxrpc_unuse_local(struct rxrpc_local *local)
351{
David Howells68553f12019-08-09 22:47:47 +0100352 if (local) {
David Howells04d36d72020-01-30 21:50:36 +0000353 if (__rxrpc_unuse_local(local)) {
354 rxrpc_get_local(local);
David Howells68553f12019-08-09 22:47:47 +0100355 rxrpc_queue_local(local);
David Howells04d36d72020-01-30 21:50:36 +0000356 }
David Howells68553f12019-08-09 22:47:47 +0100357 }
David Howells730c5fd2019-08-09 15:20:41 +0100358}
359
360/*
David Howells4f95dd72016-04-04 14:00:35 +0100361 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
362 * of.
363 *
364 * Closing the socket cannot be done from bottom half context or RCU callback
365 * context because it might sleep.
David Howells17926a72007-04-26 15:48:28 -0700366 */
David Howells4f95dd72016-04-04 14:00:35 +0100367static void rxrpc_local_destroyer(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700368{
David Howells4f95dd72016-04-04 14:00:35 +0100369 struct socket *socket = local->socket;
David Howells2baec2c2017-05-24 17:02:32 +0100370 struct rxrpc_net *rxnet = local->rxnet;
David Howells17926a72007-04-26 15:48:28 -0700371
David Howells4f95dd72016-04-04 14:00:35 +0100372 _enter("%d", local->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700373
David Howellsd12040b2019-08-29 14:12:11 +0100374 local->dead = true;
375
David Howells2baec2c2017-05-24 17:02:32 +0100376 mutex_lock(&rxnet->local_mutex);
David Howells33912c22022-05-21 08:45:15 +0100377 hlist_del_init_rcu(&local->link);
David Howells2baec2c2017-05-24 17:02:32 +0100378 mutex_unlock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700379
David Howellsd12040b2019-08-29 14:12:11 +0100380 rxrpc_clean_up_local_conns(local);
381 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
David Howells1e9e5c92016-09-29 22:37:15 +0100382 ASSERT(!local->service);
David Howells17926a72007-04-26 15:48:28 -0700383
David Howells4f95dd72016-04-04 14:00:35 +0100384 if (socket) {
385 local->socket = NULL;
386 kernel_sock_shutdown(socket, SHUT_RDWR);
387 socket->sk->sk_user_data = NULL;
388 sock_release(socket);
389 }
390
391 /* At this point, there should be no more packets coming in to the
392 * local endpoint.
393 */
David Howells17926a72007-04-26 15:48:28 -0700394 rxrpc_purge_queue(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +0100395 rxrpc_purge_queue(&local->event_queue);
David Howells4f95dd72016-04-04 14:00:35 +0100396}
397
398/*
David Howells730c5fd2019-08-09 15:20:41 +0100399 * Process events on an endpoint. The work item carries a ref which
400 * we must release.
David Howells4f95dd72016-04-04 14:00:35 +0100401 */
402static void rxrpc_local_processor(struct work_struct *work)
403{
404 struct rxrpc_local *local =
405 container_of(work, struct rxrpc_local, processor);
406 bool again;
407
David Howells06d95322019-08-13 22:26:36 +0100408 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
David Howellsa05754292022-05-21 08:45:22 +0100409 refcount_read(&local->ref), NULL);
David Howells4f95dd72016-04-04 14:00:35 +0100410
411 do {
412 again = false;
David Howells04d36d72020-01-30 21:50:36 +0000413 if (!__rxrpc_use_local(local)) {
David Howells730c5fd2019-08-09 15:20:41 +0100414 rxrpc_local_destroyer(local);
415 break;
416 }
David Howells4f95dd72016-04-04 14:00:35 +0100417
David Howells4f95dd72016-04-04 14:00:35 +0100418 if (!skb_queue_empty(&local->reject_queue)) {
419 rxrpc_reject_packets(local);
420 again = true;
421 }
422
423 if (!skb_queue_empty(&local->event_queue)) {
424 rxrpc_process_local_events(local);
425 again = true;
426 }
David Howells04d36d72020-01-30 21:50:36 +0000427
428 __rxrpc_unuse_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100429 } while (again);
David Howells730c5fd2019-08-09 15:20:41 +0100430
431 rxrpc_put_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100432}
433
434/*
435 * Destroy a local endpoint after the RCU grace period expires.
436 */
437static void rxrpc_local_rcu(struct rcu_head *rcu)
438{
439 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
440
441 _enter("%d", local->debug_id);
442
443 ASSERT(!work_pending(&local->processor));
David Howells17926a72007-04-26 15:48:28 -0700444
445 _net("DESTROY LOCAL %d", local->debug_id);
446 kfree(local);
David Howells17926a72007-04-26 15:48:28 -0700447 _leave("");
448}
449
450/*
David Howells4f95dd72016-04-04 14:00:35 +0100451 * Verify the local endpoint list is empty by this point.
David Howells17926a72007-04-26 15:48:28 -0700452 */
David Howells2baec2c2017-05-24 17:02:32 +0100453void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
David Howells17926a72007-04-26 15:48:28 -0700454{
David Howells4f95dd72016-04-04 14:00:35 +0100455 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700456
457 _enter("");
458
David Howellsdee46362016-06-27 17:11:19 +0100459 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700460
David Howells33912c22022-05-21 08:45:15 +0100461 if (!hlist_empty(&rxnet->local_endpoints)) {
David Howells2baec2c2017-05-24 17:02:32 +0100462 mutex_lock(&rxnet->local_mutex);
David Howells33912c22022-05-21 08:45:15 +0100463 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
David Howellsdee46362016-06-27 17:11:19 +0100464 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
David Howellsa05754292022-05-21 08:45:22 +0100465 local, refcount_read(&local->ref));
David Howellsdee46362016-06-27 17:11:19 +0100466 }
David Howells2baec2c2017-05-24 17:02:32 +0100467 mutex_unlock(&rxnet->local_mutex);
David Howellsdee46362016-06-27 17:11:19 +0100468 BUG();
David Howells17926a72007-04-26 15:48:28 -0700469 }
David Howells17926a72007-04-26 15:48:28 -0700470}