blob: a4111408ffd0c7e3964a5cebc31899eca6537b68 [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;
Vadim Fedorenko295f8302021-02-12 13:48:14 +0300123#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
Xin Long1a9b86c2021-02-07 16:23:14 +0800124 } else {
125 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
126 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
Vadim Fedorenko295f8302021-02-12 13:48:14 +0300127#endif
Xin Long1a9b86c2021-02-07 16:23:14 +0800128 }
129 ret = udp_sock_create(net, &udp_conf, &local->socket);
David Howells17926a72007-04-26 15:48:28 -0700130 if (ret < 0) {
131 _leave(" = %d [socket]", ret);
132 return ret;
133 }
134
Xin Long1a9b86c2021-02-07 16:23:14 +0800135 tuncfg.encap_type = UDP_ENCAP_RXRPC;
136 tuncfg.encap_rcv = rxrpc_input_packet;
137 tuncfg.sk_user_data = local;
138 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
139
David Howells2cfa2272018-10-05 14:05:35 +0100140 /* set the socket up */
David Howells52719532018-10-04 11:10:51 +0100141 usk = local->socket->sk;
David Howells52719532018-10-04 11:10:51 +0100142 usk->sk_error_report = rxrpc_error_report;
David Howells2cfa2272018-10-05 14:05:35 +0100143
Xin Long1a9b86c2021-02-07 16:23:14 +0800144 switch (srx->transport.family) {
David Howells37a675e2018-09-27 15:13:09 +0100145 case AF_INET6:
146 /* we want to receive ICMPv6 errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800147 ip6_sock_set_recverr(usk);
David Howells37a675e2018-09-27 15:13:09 +0100148
David Howells37a675e2018-09-27 15:13:09 +0100149 /* Fall through and set IPv4 options too otherwise we don't get
150 * errors from IPv4 packets sent through the IPv6 socket.
151 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500152 fallthrough;
David Howellsf2aeed32018-05-10 23:26:00 +0100153 case AF_INET:
154 /* we want to receive ICMP errors */
Xin Long1a9b86c2021-02-07 16:23:14 +0800155 ip_sock_set_recverr(usk);
David Howells17926a72007-04-26 15:48:28 -0700156
David Howellsf2aeed32018-05-10 23:26:00 +0100157 /* we want to set the don't fragment bit */
Xin Long1a9b86c2021-02-07 16:23:14 +0800158 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
David Howellsb604dd92018-09-27 15:13:08 +0100159
160 /* We want receive timestamps. */
Xin Long1a9b86c2021-02-07 16:23:14 +0800161 sock_enable_timestamps(usk);
David Howellsf2aeed32018-05-10 23:26:00 +0100162 break;
163
164 default:
165 BUG();
David Howells17926a72007-04-26 15:48:28 -0700166 }
167
David Howells17926a72007-04-26 15:48:28 -0700168 _leave(" = 0");
169 return 0;
David Howells17926a72007-04-26 15:48:28 -0700170}
171
172/*
David Howells4f95dd72016-04-04 14:00:35 +0100173 * Look up or create a new local endpoint using the specified local address.
David Howells17926a72007-04-26 15:48:28 -0700174 */
David Howells2baec2c2017-05-24 17:02:32 +0100175struct rxrpc_local *rxrpc_lookup_local(struct net *net,
176 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700177{
178 struct rxrpc_local *local;
David Howells2baec2c2017-05-24 17:02:32 +0100179 struct rxrpc_net *rxnet = rxrpc_net(net);
David Howells4f95dd72016-04-04 14:00:35 +0100180 struct list_head *cursor;
181 const char *age;
182 long diff;
David Howells17926a72007-04-26 15:48:28 -0700183 int ret;
184
David Howells75b54cb2016-09-13 08:49:05 +0100185 _enter("{%d,%d,%pISp}",
186 srx->transport_type, srx->transport.family, &srx->transport);
David Howells17926a72007-04-26 15:48:28 -0700187
David Howells2baec2c2017-05-24 17:02:32 +0100188 mutex_lock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700189
David Howells2baec2c2017-05-24 17:02:32 +0100190 for (cursor = rxnet->local_endpoints.next;
191 cursor != &rxnet->local_endpoints;
David Howells4f95dd72016-04-04 14:00:35 +0100192 cursor = cursor->next) {
193 local = list_entry(cursor, struct rxrpc_local, link);
David Howells17926a72007-04-26 15:48:28 -0700194
David Howells4f95dd72016-04-04 14:00:35 +0100195 diff = rxrpc_local_cmp_key(local, srx);
196 if (diff < 0)
David Howells17926a72007-04-26 15:48:28 -0700197 continue;
David Howells4f95dd72016-04-04 14:00:35 +0100198 if (diff > 0)
199 break;
David Howells17926a72007-04-26 15:48:28 -0700200
David Howells4f95dd72016-04-04 14:00:35 +0100201 /* Services aren't allowed to share transport sockets, so
202 * reject that here. It is possible that the object is dying -
203 * but it may also still have the local transport address that
204 * we want bound.
205 */
206 if (srx->srx_service) {
207 local = NULL;
208 goto addr_in_use;
David Howells17926a72007-04-26 15:48:28 -0700209 }
David Howells4f95dd72016-04-04 14:00:35 +0100210
211 /* Found a match. We replace a dying object. Attempting to
212 * bind the transport socket may still fail if we're attempting
213 * to use a local address that the dying object is still using.
214 */
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 Howells730c5fd2019-08-09 15:20:41 +0100230 if (cursor != &rxnet->local_endpoints)
David Howellsb00df842019-08-12 23:30:06 +0100231 list_replace_init(cursor, &local->link);
David Howells730c5fd2019-08-09 15:20:41 +0100232 else
233 list_add_tail(&local->link, cursor);
David Howells4f95dd72016-04-04 14:00:35 +0100234 age = "new";
David Howells17926a72007-04-26 15:48:28 -0700235
David Howells4f95dd72016-04-04 14:00:35 +0100236found:
David Howells2baec2c2017-05-24 17:02:32 +0100237 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100238
David Howells75b54cb2016-09-13 08:49:05 +0100239 _net("LOCAL %s %d {%pISp}",
240 age, local->debug_id, &local->srx.transport);
David Howells17926a72007-04-26 15:48:28 -0700241
David Howells4f95dd72016-04-04 14:00:35 +0100242 _leave(" = %p", local);
David Howells17926a72007-04-26 15:48:28 -0700243 return local;
244
David Howells4f95dd72016-04-04 14:00:35 +0100245nomem:
246 ret = -ENOMEM;
247sock_error:
David Howells2baec2c2017-05-24 17:02:32 +0100248 mutex_unlock(&rxnet->local_mutex);
Eric Dumazet032be5f2019-04-24 09:44:11 -0700249 if (local)
250 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells4f95dd72016-04-04 14:00:35 +0100251 _leave(" = %d", ret);
252 return ERR_PTR(ret);
David Howells17926a72007-04-26 15:48:28 -0700253
David Howells4f95dd72016-04-04 14:00:35 +0100254addr_in_use:
David Howells2baec2c2017-05-24 17:02:32 +0100255 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100256 _leave(" = -EADDRINUSE");
257 return ERR_PTR(-EADDRINUSE);
David Howells17926a72007-04-26 15:48:28 -0700258}
259
260/*
David Howells09d2bf52018-03-30 21:05:28 +0100261 * Get a ref on a local endpoint.
262 */
263struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
264{
265 const void *here = __builtin_return_address(0);
266 int n;
267
268 n = atomic_inc_return(&local->usage);
David Howells06d95322019-08-13 22:26:36 +0100269 trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
David Howells09d2bf52018-03-30 21:05:28 +0100270 return local;
271}
272
273/*
274 * Get a ref on a local endpoint unless its usage has already reached 0.
275 */
276struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
277{
278 const void *here = __builtin_return_address(0);
279
280 if (local) {
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100281 int n = atomic_fetch_add_unless(&local->usage, 1, 0);
David Howells09d2bf52018-03-30 21:05:28 +0100282 if (n > 0)
David Howells06d95322019-08-13 22:26:36 +0100283 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
284 n + 1, here);
David Howells09d2bf52018-03-30 21:05:28 +0100285 else
286 local = NULL;
287 }
288 return local;
289}
290
291/*
David Howells06d95322019-08-13 22:26:36 +0100292 * Queue a local endpoint and pass the caller's reference to the work item.
David Howells09d2bf52018-03-30 21:05:28 +0100293 */
294void rxrpc_queue_local(struct rxrpc_local *local)
295{
296 const void *here = __builtin_return_address(0);
David Howells06d95322019-08-13 22:26:36 +0100297 unsigned int debug_id = local->debug_id;
298 int n = atomic_read(&local->usage);
David Howells09d2bf52018-03-30 21:05:28 +0100299
300 if (rxrpc_queue_work(&local->processor))
David Howells06d95322019-08-13 22:26:36 +0100301 trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
David Howells730c5fd2019-08-09 15:20:41 +0100302 else
303 rxrpc_put_local(local);
David Howells17926a72007-04-26 15:48:28 -0700304}
305
306/*
David Howells09d2bf52018-03-30 21:05:28 +0100307 * Drop a ref on a local endpoint.
308 */
309void rxrpc_put_local(struct rxrpc_local *local)
310{
311 const void *here = __builtin_return_address(0);
David Howellsfac20b92020-01-30 21:50:35 +0000312 unsigned int debug_id;
David Howells09d2bf52018-03-30 21:05:28 +0100313 int n;
314
315 if (local) {
David Howellsfac20b92020-01-30 21:50:35 +0000316 debug_id = local->debug_id;
317
David Howells09d2bf52018-03-30 21:05:28 +0100318 n = atomic_dec_return(&local->usage);
David Howellsfac20b92020-01-30 21:50:35 +0000319 trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
David Howells09d2bf52018-03-30 21:05:28 +0100320
321 if (n == 0)
David Howells730c5fd2019-08-09 15:20:41 +0100322 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells09d2bf52018-03-30 21:05:28 +0100323 }
324}
325
326/*
David Howells730c5fd2019-08-09 15:20:41 +0100327 * Start using a local endpoint.
328 */
329struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
330{
David Howells730c5fd2019-08-09 15:20:41 +0100331 local = rxrpc_get_local_maybe(local);
332 if (!local)
333 return NULL;
334
David Howells04d36d72020-01-30 21:50:36 +0000335 if (!__rxrpc_use_local(local)) {
David Howells730c5fd2019-08-09 15:20:41 +0100336 rxrpc_put_local(local);
337 return NULL;
338 }
339
340 return local;
341}
342
343/*
344 * Cease using a local endpoint. Once the number of active users reaches 0, we
345 * start the closure of the transport in the work processor.
346 */
347void rxrpc_unuse_local(struct rxrpc_local *local)
348{
David Howells68553f12019-08-09 22:47:47 +0100349 if (local) {
David Howells04d36d72020-01-30 21:50:36 +0000350 if (__rxrpc_unuse_local(local)) {
351 rxrpc_get_local(local);
David Howells68553f12019-08-09 22:47:47 +0100352 rxrpc_queue_local(local);
David Howells04d36d72020-01-30 21:50:36 +0000353 }
David Howells68553f12019-08-09 22:47:47 +0100354 }
David Howells730c5fd2019-08-09 15:20:41 +0100355}
356
357/*
David Howells4f95dd72016-04-04 14:00:35 +0100358 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
359 * of.
360 *
361 * Closing the socket cannot be done from bottom half context or RCU callback
362 * context because it might sleep.
David Howells17926a72007-04-26 15:48:28 -0700363 */
David Howells4f95dd72016-04-04 14:00:35 +0100364static void rxrpc_local_destroyer(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700365{
David Howells4f95dd72016-04-04 14:00:35 +0100366 struct socket *socket = local->socket;
David Howells2baec2c2017-05-24 17:02:32 +0100367 struct rxrpc_net *rxnet = local->rxnet;
David Howells17926a72007-04-26 15:48:28 -0700368
David Howells4f95dd72016-04-04 14:00:35 +0100369 _enter("%d", local->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700370
David Howellsd12040b2019-08-29 14:12:11 +0100371 local->dead = true;
372
David Howells2baec2c2017-05-24 17:02:32 +0100373 mutex_lock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100374 list_del_init(&local->link);
David Howells2baec2c2017-05-24 17:02:32 +0100375 mutex_unlock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700376
David Howellsd12040b2019-08-29 14:12:11 +0100377 rxrpc_clean_up_local_conns(local);
378 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
David Howells1e9e5c92016-09-29 22:37:15 +0100379 ASSERT(!local->service);
David Howells17926a72007-04-26 15:48:28 -0700380
David Howells4f95dd72016-04-04 14:00:35 +0100381 if (socket) {
382 local->socket = NULL;
383 kernel_sock_shutdown(socket, SHUT_RDWR);
384 socket->sk->sk_user_data = NULL;
385 sock_release(socket);
386 }
387
388 /* At this point, there should be no more packets coming in to the
389 * local endpoint.
390 */
David Howells17926a72007-04-26 15:48:28 -0700391 rxrpc_purge_queue(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +0100392 rxrpc_purge_queue(&local->event_queue);
David Howells4f95dd72016-04-04 14:00:35 +0100393}
394
395/*
David Howells730c5fd2019-08-09 15:20:41 +0100396 * Process events on an endpoint. The work item carries a ref which
397 * we must release.
David Howells4f95dd72016-04-04 14:00:35 +0100398 */
399static void rxrpc_local_processor(struct work_struct *work)
400{
401 struct rxrpc_local *local =
402 container_of(work, struct rxrpc_local, processor);
403 bool again;
404
David Howells06d95322019-08-13 22:26:36 +0100405 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
David Howells09d2bf52018-03-30 21:05:28 +0100406 atomic_read(&local->usage), NULL);
David Howells4f95dd72016-04-04 14:00:35 +0100407
408 do {
409 again = false;
David Howells04d36d72020-01-30 21:50:36 +0000410 if (!__rxrpc_use_local(local)) {
David Howells730c5fd2019-08-09 15:20:41 +0100411 rxrpc_local_destroyer(local);
412 break;
413 }
David Howells4f95dd72016-04-04 14:00:35 +0100414
David Howells4f95dd72016-04-04 14:00:35 +0100415 if (!skb_queue_empty(&local->reject_queue)) {
416 rxrpc_reject_packets(local);
417 again = true;
418 }
419
420 if (!skb_queue_empty(&local->event_queue)) {
421 rxrpc_process_local_events(local);
422 again = true;
423 }
David Howells04d36d72020-01-30 21:50:36 +0000424
425 __rxrpc_unuse_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100426 } while (again);
David Howells730c5fd2019-08-09 15:20:41 +0100427
428 rxrpc_put_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100429}
430
431/*
432 * Destroy a local endpoint after the RCU grace period expires.
433 */
434static void rxrpc_local_rcu(struct rcu_head *rcu)
435{
436 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
437
438 _enter("%d", local->debug_id);
439
440 ASSERT(!work_pending(&local->processor));
David Howells17926a72007-04-26 15:48:28 -0700441
442 _net("DESTROY LOCAL %d", local->debug_id);
443 kfree(local);
David Howells17926a72007-04-26 15:48:28 -0700444 _leave("");
445}
446
447/*
David Howells4f95dd72016-04-04 14:00:35 +0100448 * Verify the local endpoint list is empty by this point.
David Howells17926a72007-04-26 15:48:28 -0700449 */
David Howells2baec2c2017-05-24 17:02:32 +0100450void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
David Howells17926a72007-04-26 15:48:28 -0700451{
David Howells4f95dd72016-04-04 14:00:35 +0100452 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700453
454 _enter("");
455
David Howellsdee46362016-06-27 17:11:19 +0100456 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700457
David Howells2baec2c2017-05-24 17:02:32 +0100458 if (!list_empty(&rxnet->local_endpoints)) {
459 mutex_lock(&rxnet->local_mutex);
460 list_for_each_entry(local, &rxnet->local_endpoints, link) {
David Howellsdee46362016-06-27 17:11:19 +0100461 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
462 local, atomic_read(&local->usage));
463 }
David Howells2baec2c2017-05-24 17:02:32 +0100464 mutex_unlock(&rxnet->local_mutex);
David Howellsdee46362016-06-27 17:11:19 +0100465 BUG();
David Howells17926a72007-04-26 15:48:28 -0700466 }
David Howells17926a72007-04-26 15:48:28 -0700467}