blob: 3aa179efcda43f2245fd2c71a1af219620d7d097 [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>
David Howells17926a72007-04-26 15:48:28 -070019#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
David Howells4f95dd72016-04-04 14:00:35 +010022static void rxrpc_local_processor(struct work_struct *);
23static void rxrpc_local_rcu(struct rcu_head *);
David Howells17926a72007-04-26 15:48:28 -070024
David Howells17926a72007-04-26 15:48:28 -070025/*
David Howells4f95dd72016-04-04 14:00:35 +010026 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
27 * same or greater than.
28 *
29 * We explicitly don't compare the RxRPC service ID as we want to reject
30 * conflicting uses by differing services. Further, we don't want to share
31 * addresses with different options (IPv6), so we don't compare those bits
32 * either.
David Howells17926a72007-04-26 15:48:28 -070033 */
David Howells4f95dd72016-04-04 14:00:35 +010034static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
35 const struct sockaddr_rxrpc *srx)
36{
37 long diff;
38
39 diff = ((local->srx.transport_type - srx->transport_type) ?:
40 (local->srx.transport_len - srx->transport_len) ?:
41 (local->srx.transport.family - srx->transport.family));
42 if (diff != 0)
43 return diff;
44
45 switch (srx->transport.family) {
46 case AF_INET:
47 /* If the choice of UDP port is left up to the transport, then
48 * the endpoint record doesn't match.
49 */
50 return ((u16 __force)local->srx.transport.sin.sin_port -
51 (u16 __force)srx->transport.sin.sin_port) ?:
52 memcmp(&local->srx.transport.sin.sin_addr,
53 &srx->transport.sin.sin_addr,
54 sizeof(struct in_addr));
David Howellsd1912742016-09-17 07:26:01 +010055#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +010056 case AF_INET6:
57 /* If the choice of UDP6 port is left up to the transport, then
58 * the endpoint record doesn't match.
59 */
60 return ((u16 __force)local->srx.transport.sin6.sin6_port -
61 (u16 __force)srx->transport.sin6.sin6_port) ?:
62 memcmp(&local->srx.transport.sin6.sin6_addr,
63 &srx->transport.sin6.sin6_addr,
64 sizeof(struct in6_addr));
David Howellsd1912742016-09-17 07:26:01 +010065#endif
David Howells4f95dd72016-04-04 14:00:35 +010066 default:
67 BUG();
68 }
69}
70
71/*
72 * Allocate a new local endpoint.
73 */
David Howells2baec2c2017-05-24 17:02:32 +010074static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
75 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -070076{
77 struct rxrpc_local *local;
78
79 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
80 if (local) {
David Howells4f95dd72016-04-04 14:00:35 +010081 atomic_set(&local->usage, 1);
David Howells730c5fd2019-08-09 15:20:41 +010082 atomic_set(&local->active_users, 1);
David Howells2baec2c2017-05-24 17:02:32 +010083 local->rxnet = rxnet;
David Howells17926a72007-04-26 15:48:28 -070084 INIT_LIST_HEAD(&local->link);
David Howells4f95dd72016-04-04 14:00:35 +010085 INIT_WORK(&local->processor, rxrpc_local_processor);
David Howells17926a72007-04-26 15:48:28 -070086 init_rwsem(&local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -070087 skb_queue_head_init(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +010088 skb_queue_head_init(&local->event_queue);
David Howells999b69f2016-06-17 15:42:35 +010089 local->client_conns = RB_ROOT;
90 spin_lock_init(&local->client_conns_lock);
David Howells17926a72007-04-26 15:48:28 -070091 spin_lock_init(&local->lock);
92 rwlock_init(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -070093 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
94 memcpy(&local->srx, srx, sizeof(*srx));
David Howells28036f42017-06-05 14:30:49 +010095 local->srx.srx_service = 0;
David Howells06d95322019-08-13 22:26:36 +010096 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
David Howells17926a72007-04-26 15:48:28 -070097 }
98
99 _leave(" = %p", local);
100 return local;
101}
102
103/*
104 * create the local socket
David Howells4f95dd72016-04-04 14:00:35 +0100105 * - must be called with rxrpc_local_mutex locked
David Howells17926a72007-04-26 15:48:28 -0700106 */
David Howells2baec2c2017-05-24 17:02:32 +0100107static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
David Howells17926a72007-04-26 15:48:28 -0700108{
David Howells52719532018-10-04 11:10:51 +0100109 struct sock *usk;
David Howells17926a72007-04-26 15:48:28 -0700110 int ret, opt;
111
David Howells75b54cb2016-09-13 08:49:05 +0100112 _enter("%p{%d,%d}",
113 local, local->srx.transport_type, local->srx.transport.family);
David Howells17926a72007-04-26 15:48:28 -0700114
115 /* create a socket to represent the local endpoint */
David Howells2baec2c2017-05-24 17:02:32 +0100116 ret = sock_create_kern(net, local->srx.transport.family,
David Howellsaaa31cb2016-09-13 08:49:05 +0100117 local->srx.transport_type, 0, &local->socket);
David Howells17926a72007-04-26 15:48:28 -0700118 if (ret < 0) {
119 _leave(" = %d [socket]", ret);
120 return ret;
121 }
122
David Howells2cfa2272018-10-05 14:05:35 +0100123 /* set the socket up */
David Howells52719532018-10-04 11:10:51 +0100124 usk = local->socket->sk;
125 inet_sk(usk)->mc_loop = 0;
126
127 /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
128 inet_inc_convert_csum(usk);
129
130 rcu_assign_sk_user_data(usk, local);
131
132 udp_sk(usk)->encap_type = UDP_ENCAP_RXRPC;
133 udp_sk(usk)->encap_rcv = rxrpc_input_packet;
134 udp_sk(usk)->encap_destroy = NULL;
135 udp_sk(usk)->gro_receive = NULL;
136 udp_sk(usk)->gro_complete = NULL;
137
138 udp_encap_enable();
David Howells7ec8dc92018-10-12 16:38:36 +0100139#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
David Howells52719532018-10-04 11:10:51 +0100140 if (local->srx.transport.family == AF_INET6)
141 udpv6_encap_enable();
142#endif
143 usk->sk_error_report = rxrpc_error_report;
David Howells2cfa2272018-10-05 14:05:35 +0100144
David Howells17926a72007-04-26 15:48:28 -0700145 /* if a local address was supplied then bind it */
146 if (local->srx.transport_len > sizeof(sa_family_t)) {
147 _debug("bind");
148 ret = kernel_bind(local->socket,
David Howells4f95dd72016-04-04 14:00:35 +0100149 (struct sockaddr *)&local->srx.transport,
David Howells17926a72007-04-26 15:48:28 -0700150 local->srx.transport_len);
151 if (ret < 0) {
David Howells4f95dd72016-04-04 14:00:35 +0100152 _debug("bind failed %d", ret);
David Howells17926a72007-04-26 15:48:28 -0700153 goto error;
154 }
155 }
156
David Howellsf2aeed32018-05-10 23:26:00 +0100157 switch (local->srx.transport.family) {
David Howells37a675e2018-09-27 15:13:09 +0100158 case AF_INET6:
159 /* we want to receive ICMPv6 errors */
160 opt = 1;
161 ret = kernel_setsockopt(local->socket, SOL_IPV6, IPV6_RECVERR,
162 (char *) &opt, sizeof(opt));
163 if (ret < 0) {
164 _debug("setsockopt failed");
165 goto error;
166 }
167
168 /* we want to set the don't fragment bit */
169 opt = IPV6_PMTUDISC_DO;
170 ret = kernel_setsockopt(local->socket, SOL_IPV6, IPV6_MTU_DISCOVER,
171 (char *) &opt, sizeof(opt));
172 if (ret < 0) {
173 _debug("setsockopt failed");
174 goto error;
175 }
176
177 /* Fall through and set IPv4 options too otherwise we don't get
178 * errors from IPv4 packets sent through the IPv6 socket.
179 */
Gustavo A. R. Silva936ee652019-04-01 14:39:31 -0500180 /* Fall through */
David Howellsf2aeed32018-05-10 23:26:00 +0100181 case AF_INET:
182 /* we want to receive ICMP errors */
183 opt = 1;
184 ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
185 (char *) &opt, sizeof(opt));
186 if (ret < 0) {
187 _debug("setsockopt failed");
188 goto error;
189 }
David Howells17926a72007-04-26 15:48:28 -0700190
David Howellsf2aeed32018-05-10 23:26:00 +0100191 /* we want to set the don't fragment bit */
192 opt = IP_PMTUDISC_DO;
193 ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
194 (char *) &opt, sizeof(opt));
195 if (ret < 0) {
196 _debug("setsockopt failed");
197 goto error;
198 }
David Howellsb604dd92018-09-27 15:13:08 +0100199
200 /* We want receive timestamps. */
201 opt = 1;
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800202 ret = kernel_setsockopt(local->socket, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
David Howellsb604dd92018-09-27 15:13:08 +0100203 (char *)&opt, sizeof(opt));
204 if (ret < 0) {
205 _debug("setsockopt failed");
206 goto error;
207 }
David Howellsf2aeed32018-05-10 23:26:00 +0100208 break;
209
210 default:
211 BUG();
David Howells17926a72007-04-26 15:48:28 -0700212 }
213
David Howells17926a72007-04-26 15:48:28 -0700214 _leave(" = 0");
215 return 0;
216
217error:
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800218 kernel_sock_shutdown(local->socket, SHUT_RDWR);
David Howells17926a72007-04-26 15:48:28 -0700219 local->socket->sk->sk_user_data = NULL;
220 sock_release(local->socket);
221 local->socket = NULL;
222
223 _leave(" = %d", ret);
224 return ret;
225}
226
227/*
David Howells4f95dd72016-04-04 14:00:35 +0100228 * Look up or create a new local endpoint using the specified local address.
David Howells17926a72007-04-26 15:48:28 -0700229 */
David Howells2baec2c2017-05-24 17:02:32 +0100230struct rxrpc_local *rxrpc_lookup_local(struct net *net,
231 const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700232{
233 struct rxrpc_local *local;
David Howells2baec2c2017-05-24 17:02:32 +0100234 struct rxrpc_net *rxnet = rxrpc_net(net);
David Howells4f95dd72016-04-04 14:00:35 +0100235 struct list_head *cursor;
236 const char *age;
237 long diff;
David Howells17926a72007-04-26 15:48:28 -0700238 int ret;
239
David Howells75b54cb2016-09-13 08:49:05 +0100240 _enter("{%d,%d,%pISp}",
241 srx->transport_type, srx->transport.family, &srx->transport);
David Howells17926a72007-04-26 15:48:28 -0700242
David Howells2baec2c2017-05-24 17:02:32 +0100243 mutex_lock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700244
David Howells2baec2c2017-05-24 17:02:32 +0100245 for (cursor = rxnet->local_endpoints.next;
246 cursor != &rxnet->local_endpoints;
David Howells4f95dd72016-04-04 14:00:35 +0100247 cursor = cursor->next) {
248 local = list_entry(cursor, struct rxrpc_local, link);
David Howells17926a72007-04-26 15:48:28 -0700249
David Howells4f95dd72016-04-04 14:00:35 +0100250 diff = rxrpc_local_cmp_key(local, srx);
251 if (diff < 0)
David Howells17926a72007-04-26 15:48:28 -0700252 continue;
David Howells4f95dd72016-04-04 14:00:35 +0100253 if (diff > 0)
254 break;
David Howells17926a72007-04-26 15:48:28 -0700255
David Howells4f95dd72016-04-04 14:00:35 +0100256 /* Services aren't allowed to share transport sockets, so
257 * reject that here. It is possible that the object is dying -
258 * but it may also still have the local transport address that
259 * we want bound.
260 */
261 if (srx->srx_service) {
262 local = NULL;
263 goto addr_in_use;
David Howells17926a72007-04-26 15:48:28 -0700264 }
David Howells4f95dd72016-04-04 14:00:35 +0100265
266 /* Found a match. We replace a dying object. Attempting to
267 * bind the transport socket may still fail if we're attempting
268 * to use a local address that the dying object is still using.
269 */
David Howells730c5fd2019-08-09 15:20:41 +0100270 if (!rxrpc_use_local(local))
David Howells4f95dd72016-04-04 14:00:35 +0100271 break;
David Howells4f95dd72016-04-04 14:00:35 +0100272
273 age = "old";
274 goto found;
David Howells17926a72007-04-26 15:48:28 -0700275 }
276
David Howells2baec2c2017-05-24 17:02:32 +0100277 local = rxrpc_alloc_local(rxnet, srx);
David Howells4f95dd72016-04-04 14:00:35 +0100278 if (!local)
279 goto nomem;
David Howells17926a72007-04-26 15:48:28 -0700280
David Howells2baec2c2017-05-24 17:02:32 +0100281 ret = rxrpc_open_socket(local, net);
David Howells4f95dd72016-04-04 14:00:35 +0100282 if (ret < 0)
283 goto sock_error;
David Howells17926a72007-04-26 15:48:28 -0700284
David Howells730c5fd2019-08-09 15:20:41 +0100285 if (cursor != &rxnet->local_endpoints)
David Howellsb00df842019-08-12 23:30:06 +0100286 list_replace_init(cursor, &local->link);
David Howells730c5fd2019-08-09 15:20:41 +0100287 else
288 list_add_tail(&local->link, cursor);
David Howells4f95dd72016-04-04 14:00:35 +0100289 age = "new";
David Howells17926a72007-04-26 15:48:28 -0700290
David Howells4f95dd72016-04-04 14:00:35 +0100291found:
David Howells2baec2c2017-05-24 17:02:32 +0100292 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100293
David Howells75b54cb2016-09-13 08:49:05 +0100294 _net("LOCAL %s %d {%pISp}",
295 age, local->debug_id, &local->srx.transport);
David Howells17926a72007-04-26 15:48:28 -0700296
David Howells4f95dd72016-04-04 14:00:35 +0100297 _leave(" = %p", local);
David Howells17926a72007-04-26 15:48:28 -0700298 return local;
299
David Howells4f95dd72016-04-04 14:00:35 +0100300nomem:
301 ret = -ENOMEM;
302sock_error:
David Howells2baec2c2017-05-24 17:02:32 +0100303 mutex_unlock(&rxnet->local_mutex);
Eric Dumazet032be5f2019-04-24 09:44:11 -0700304 if (local)
305 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells4f95dd72016-04-04 14:00:35 +0100306 _leave(" = %d", ret);
307 return ERR_PTR(ret);
David Howells17926a72007-04-26 15:48:28 -0700308
David Howells4f95dd72016-04-04 14:00:35 +0100309addr_in_use:
David Howells2baec2c2017-05-24 17:02:32 +0100310 mutex_unlock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100311 _leave(" = -EADDRINUSE");
312 return ERR_PTR(-EADDRINUSE);
David Howells17926a72007-04-26 15:48:28 -0700313}
314
315/*
David Howells09d2bf52018-03-30 21:05:28 +0100316 * Get a ref on a local endpoint.
317 */
318struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
319{
320 const void *here = __builtin_return_address(0);
321 int n;
322
323 n = atomic_inc_return(&local->usage);
David Howells06d95322019-08-13 22:26:36 +0100324 trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
David Howells09d2bf52018-03-30 21:05:28 +0100325 return local;
326}
327
328/*
329 * Get a ref on a local endpoint unless its usage has already reached 0.
330 */
331struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
332{
333 const void *here = __builtin_return_address(0);
334
335 if (local) {
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100336 int n = atomic_fetch_add_unless(&local->usage, 1, 0);
David Howells09d2bf52018-03-30 21:05:28 +0100337 if (n > 0)
David Howells06d95322019-08-13 22:26:36 +0100338 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
339 n + 1, here);
David Howells09d2bf52018-03-30 21:05:28 +0100340 else
341 local = NULL;
342 }
343 return local;
344}
345
346/*
David Howells06d95322019-08-13 22:26:36 +0100347 * Queue a local endpoint and pass the caller's reference to the work item.
David Howells09d2bf52018-03-30 21:05:28 +0100348 */
349void rxrpc_queue_local(struct rxrpc_local *local)
350{
351 const void *here = __builtin_return_address(0);
David Howells06d95322019-08-13 22:26:36 +0100352 unsigned int debug_id = local->debug_id;
353 int n = atomic_read(&local->usage);
David Howells09d2bf52018-03-30 21:05:28 +0100354
355 if (rxrpc_queue_work(&local->processor))
David Howells06d95322019-08-13 22:26:36 +0100356 trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
David Howells730c5fd2019-08-09 15:20:41 +0100357 else
358 rxrpc_put_local(local);
David Howells17926a72007-04-26 15:48:28 -0700359}
360
361/*
David Howells09d2bf52018-03-30 21:05:28 +0100362 * Drop a ref on a local endpoint.
363 */
364void rxrpc_put_local(struct rxrpc_local *local)
365{
366 const void *here = __builtin_return_address(0);
David Howellsfac20b92020-01-30 21:50:35 +0000367 unsigned int debug_id;
David Howells09d2bf52018-03-30 21:05:28 +0100368 int n;
369
370 if (local) {
David Howellsfac20b92020-01-30 21:50:35 +0000371 debug_id = local->debug_id;
372
David Howells09d2bf52018-03-30 21:05:28 +0100373 n = atomic_dec_return(&local->usage);
David Howellsfac20b92020-01-30 21:50:35 +0000374 trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
David Howells09d2bf52018-03-30 21:05:28 +0100375
376 if (n == 0)
David Howells730c5fd2019-08-09 15:20:41 +0100377 call_rcu(&local->rcu, rxrpc_local_rcu);
David Howells09d2bf52018-03-30 21:05:28 +0100378 }
379}
380
381/*
David Howells730c5fd2019-08-09 15:20:41 +0100382 * Start using a local endpoint.
383 */
384struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
385{
386 unsigned int au;
387
388 local = rxrpc_get_local_maybe(local);
389 if (!local)
390 return NULL;
391
392 au = atomic_fetch_add_unless(&local->active_users, 1, 0);
393 if (au == 0) {
394 rxrpc_put_local(local);
395 return NULL;
396 }
397
398 return local;
399}
400
401/*
402 * Cease using a local endpoint. Once the number of active users reaches 0, we
403 * start the closure of the transport in the work processor.
404 */
405void rxrpc_unuse_local(struct rxrpc_local *local)
406{
407 unsigned int au;
408
David Howells68553f12019-08-09 22:47:47 +0100409 if (local) {
410 au = atomic_dec_return(&local->active_users);
411 if (au == 0)
412 rxrpc_queue_local(local);
413 else
414 rxrpc_put_local(local);
415 }
David Howells730c5fd2019-08-09 15:20:41 +0100416}
417
418/*
David Howells4f95dd72016-04-04 14:00:35 +0100419 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
420 * of.
421 *
422 * Closing the socket cannot be done from bottom half context or RCU callback
423 * context because it might sleep.
David Howells17926a72007-04-26 15:48:28 -0700424 */
David Howells4f95dd72016-04-04 14:00:35 +0100425static void rxrpc_local_destroyer(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700426{
David Howells4f95dd72016-04-04 14:00:35 +0100427 struct socket *socket = local->socket;
David Howells2baec2c2017-05-24 17:02:32 +0100428 struct rxrpc_net *rxnet = local->rxnet;
David Howells17926a72007-04-26 15:48:28 -0700429
David Howells4f95dd72016-04-04 14:00:35 +0100430 _enter("%d", local->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700431
David Howellsd12040b2019-08-29 14:12:11 +0100432 local->dead = true;
433
David Howells2baec2c2017-05-24 17:02:32 +0100434 mutex_lock(&rxnet->local_mutex);
David Howells4f95dd72016-04-04 14:00:35 +0100435 list_del_init(&local->link);
David Howells2baec2c2017-05-24 17:02:32 +0100436 mutex_unlock(&rxnet->local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700437
David Howellsd12040b2019-08-29 14:12:11 +0100438 rxrpc_clean_up_local_conns(local);
439 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
David Howells1e9e5c92016-09-29 22:37:15 +0100440 ASSERT(!local->service);
David Howells17926a72007-04-26 15:48:28 -0700441
David Howells4f95dd72016-04-04 14:00:35 +0100442 if (socket) {
443 local->socket = NULL;
444 kernel_sock_shutdown(socket, SHUT_RDWR);
445 socket->sk->sk_user_data = NULL;
446 sock_release(socket);
447 }
448
449 /* At this point, there should be no more packets coming in to the
450 * local endpoint.
451 */
David Howells17926a72007-04-26 15:48:28 -0700452 rxrpc_purge_queue(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +0100453 rxrpc_purge_queue(&local->event_queue);
David Howells4f95dd72016-04-04 14:00:35 +0100454}
455
456/*
David Howells730c5fd2019-08-09 15:20:41 +0100457 * Process events on an endpoint. The work item carries a ref which
458 * we must release.
David Howells4f95dd72016-04-04 14:00:35 +0100459 */
460static void rxrpc_local_processor(struct work_struct *work)
461{
462 struct rxrpc_local *local =
463 container_of(work, struct rxrpc_local, processor);
464 bool again;
465
David Howells06d95322019-08-13 22:26:36 +0100466 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
David Howells09d2bf52018-03-30 21:05:28 +0100467 atomic_read(&local->usage), NULL);
David Howells4f95dd72016-04-04 14:00:35 +0100468
469 do {
470 again = false;
David Howells730c5fd2019-08-09 15:20:41 +0100471 if (atomic_read(&local->active_users) == 0) {
472 rxrpc_local_destroyer(local);
473 break;
474 }
David Howells4f95dd72016-04-04 14:00:35 +0100475
David Howells4f95dd72016-04-04 14:00:35 +0100476 if (!skb_queue_empty(&local->reject_queue)) {
477 rxrpc_reject_packets(local);
478 again = true;
479 }
480
481 if (!skb_queue_empty(&local->event_queue)) {
482 rxrpc_process_local_events(local);
483 again = true;
484 }
485 } while (again);
David Howells730c5fd2019-08-09 15:20:41 +0100486
487 rxrpc_put_local(local);
David Howells4f95dd72016-04-04 14:00:35 +0100488}
489
490/*
491 * Destroy a local endpoint after the RCU grace period expires.
492 */
493static void rxrpc_local_rcu(struct rcu_head *rcu)
494{
495 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
496
497 _enter("%d", local->debug_id);
498
499 ASSERT(!work_pending(&local->processor));
David Howells17926a72007-04-26 15:48:28 -0700500
501 _net("DESTROY LOCAL %d", local->debug_id);
502 kfree(local);
David Howells17926a72007-04-26 15:48:28 -0700503 _leave("");
504}
505
506/*
David Howells4f95dd72016-04-04 14:00:35 +0100507 * Verify the local endpoint list is empty by this point.
David Howells17926a72007-04-26 15:48:28 -0700508 */
David Howells2baec2c2017-05-24 17:02:32 +0100509void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
David Howells17926a72007-04-26 15:48:28 -0700510{
David Howells4f95dd72016-04-04 14:00:35 +0100511 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700512
513 _enter("");
514
David Howellsdee46362016-06-27 17:11:19 +0100515 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700516
David Howells2baec2c2017-05-24 17:02:32 +0100517 if (!list_empty(&rxnet->local_endpoints)) {
518 mutex_lock(&rxnet->local_mutex);
519 list_for_each_entry(local, &rxnet->local_endpoints, link) {
David Howellsdee46362016-06-27 17:11:19 +0100520 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
521 local, atomic_read(&local->usage));
522 }
David Howells2baec2c2017-05-24 17:02:32 +0100523 mutex_unlock(&rxnet->local_mutex);
David Howellsdee46362016-06-27 17:11:19 +0100524 BUG();
David Howells17926a72007-04-26 15:48:28 -0700525 }
David Howells17926a72007-04-26 15:48:28 -0700526}