]> git.ipfire.org Git - people/ms/linux.git/blame - net/rxrpc/local_object.c
rxrpc: Allow list of in-use local UDP endpoints to be viewed in /proc
[people/ms/linux.git] / net / rxrpc / local_object.c
CommitLineData
b4d0d230 1// SPDX-License-Identifier: GPL-2.0-or-later
87563616 2/* Local endpoint object management
17926a79 3 *
4f95dd78 4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
17926a79 5 * Written by David Howells (dhowells@redhat.com)
17926a79
DH
6 */
7
9b6d5398
JP
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
17926a79
DH
10#include <linux/module.h>
11#include <linux/net.h>
12#include <linux/skbuff.h>
5a0e3ad6 13#include <linux/slab.h>
44ba0698
DH
14#include <linux/udp.h>
15#include <linux/ip.h>
4f95dd78 16#include <linux/hashtable.h>
17926a79 17#include <net/sock.h>
5271953c 18#include <net/udp.h>
5d30c626 19#include <net/udp_tunnel.h>
17926a79
DH
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
4f95dd78
DH
23static void rxrpc_local_processor(struct work_struct *);
24static void rxrpc_local_rcu(struct rcu_head *);
17926a79 25
17926a79 26/*
4f95dd78
DH
27 * 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.
17926a79 34 */
4f95dd78
DH
35static 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));
d1912747 56#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
57 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));
d1912747 66#endif
4f95dd78
DH
67 default:
68 BUG();
69 }
70}
71
72/*
73 * Allocate a new local endpoint.
74 */
2baec2c3
DH
75static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
76 const struct sockaddr_rxrpc *srx)
17926a79
DH
77{
78 struct rxrpc_local *local;
79
80 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
81 if (local) {
4f95dd78 82 atomic_set(&local->usage, 1);
730c5fd4 83 atomic_set(&local->active_users, 1);
2baec2c3 84 local->rxnet = rxnet;
33912c26 85 INIT_HLIST_NODE(&local->link);
4f95dd78 86 INIT_WORK(&local->processor, rxrpc_local_processor);
17926a79 87 init_rwsem(&local->defrag_sem);
17926a79 88 skb_queue_head_init(&local->reject_queue);
44ba0698 89 skb_queue_head_init(&local->event_queue);
245500d8
DH
90 local->client_bundles = RB_ROOT;
91 spin_lock_init(&local->client_bundles_lock);
17926a79
DH
92 spin_lock_init(&local->lock);
93 rwlock_init(&local->services_lock);
17926a79
DH
94 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
95 memcpy(&local->srx, srx, sizeof(*srx));
28036f44 96 local->srx.srx_service = 0;
06d9532f 97 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
17926a79
DH
98 }
99
100 _leave(" = %p", local);
101 return local;
102}
103
104/*
105 * create the local socket
4f95dd78 106 * - must be called with rxrpc_local_mutex locked
17926a79 107 */
2baec2c3 108static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
17926a79 109{
1a9b86c9
XL
110 struct udp_tunnel_sock_cfg tuncfg = {NULL};
111 struct sockaddr_rxrpc *srx = &local->srx;
112 struct udp_port_cfg udp_conf = {0};
5271953c 113 struct sock *usk;
fce93494 114 int ret;
17926a79 115
75b54cb5 116 _enter("%p{%d,%d}",
1a9b86c9
XL
117 local, srx->transport_type, srx->transport.family);
118
119 udp_conf.family = srx->transport.family;
39cb9faa 120 udp_conf.use_udp_checksums = true;
1a9b86c9
XL
121 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;
295f830e 124#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
1a9b86c9
XL
125 } else {
126 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
127 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
39cb9faa
DH
128 udp_conf.use_udp6_tx_checksums = true;
129 udp_conf.use_udp6_rx_checksums = true;
295f830e 130#endif
1a9b86c9
XL
131 }
132 ret = udp_sock_create(net, &udp_conf, &local->socket);
17926a79
DH
133 if (ret < 0) {
134 _leave(" = %d [socket]", ret);
135 return ret;
136 }
137
1a9b86c9
XL
138 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
2cfa2271 143 /* set the socket up */
5271953c 144 usk = local->socket->sk;
5271953c 145 usk->sk_error_report = rxrpc_error_report;
2cfa2271 146
1a9b86c9 147 switch (srx->transport.family) {
37a675e7
DH
148 case AF_INET6:
149 /* we want to receive ICMPv6 errors */
1a9b86c9 150 ip6_sock_set_recverr(usk);
17926a79 151
37a675e7
DH
152 /* Fall through and set IPv4 options too otherwise we don't get
153 * errors from IPv4 packets sent through the IPv6 socket.
154 */
df561f66 155 fallthrough;
37a675e7 156 case AF_INET:
f2aeed3a 157 /* we want to receive ICMP errors */
1a9b86c9 158 ip_sock_set_recverr(usk);
f2aeed3a
DH
159
160 /* we want to set the don't fragment bit */
1a9b86c9 161 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
b604dd98
DH
162
163 /* We want receive timestamps. */
1a9b86c9 164 sock_enable_timestamps(usk);
f2aeed3a
DH
165 break;
166
167 default:
168 BUG();
17926a79
DH
169 }
170
17926a79
DH
171 _leave(" = 0");
172 return 0;
17926a79
DH
173}
174
175/*
4f95dd78 176 * Look up or create a new local endpoint using the specified local address.
17926a79 177 */
2baec2c3
DH
178struct rxrpc_local *rxrpc_lookup_local(struct net *net,
179 const struct sockaddr_rxrpc *srx)
17926a79
DH
180{
181 struct rxrpc_local *local;
2baec2c3 182 struct rxrpc_net *rxnet = rxrpc_net(net);
33912c26 183 struct hlist_node *cursor;
4f95dd78
DH
184 const char *age;
185 long diff;
17926a79
DH
186 int ret;
187
75b54cb5
DH
188 _enter("{%d,%d,%pISp}",
189 srx->transport_type, srx->transport.family, &srx->transport);
17926a79 190
2baec2c3 191 mutex_lock(&rxnet->local_mutex);
17926a79 192
33912c26
DH
193 hlist_for_each(cursor, &rxnet->local_endpoints) {
194 local = hlist_entry(cursor, struct rxrpc_local, link);
17926a79 195
4f95dd78 196 diff = rxrpc_local_cmp_key(local, srx);
33912c26 197 if (diff != 0)
17926a79 198 continue;
4f95dd78
DH
199
200 /* 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;
208 }
17926a79 209
33912c26
DH
210 /* 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.
4f95dd78 214 */
730c5fd4 215 if (!rxrpc_use_local(local))
4f95dd78 216 break;
17926a79 217
4f95dd78
DH
218 age = "old";
219 goto found;
220 }
17926a79 221
2baec2c3 222 local = rxrpc_alloc_local(rxnet, srx);
4f95dd78
DH
223 if (!local)
224 goto nomem;
17926a79 225
2baec2c3 226 ret = rxrpc_open_socket(local, net);
4f95dd78
DH
227 if (ret < 0)
228 goto sock_error;
229
33912c26
DH
230 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 }
4f95dd78 236 age = "new";
17926a79 237
4f95dd78 238found:
2baec2c3 239 mutex_unlock(&rxnet->local_mutex);
17926a79 240
75b54cb5
DH
241 _net("LOCAL %s %d {%pISp}",
242 age, local->debug_id, &local->srx.transport);
17926a79 243
4f95dd78 244 _leave(" = %p", local);
17926a79
DH
245 return local;
246
4f95dd78
DH
247nomem:
248 ret = -ENOMEM;
249sock_error:
2baec2c3 250 mutex_unlock(&rxnet->local_mutex);
032be5f1
ED
251 if (local)
252 call_rcu(&local->rcu, rxrpc_local_rcu);
4f95dd78
DH
253 _leave(" = %d", ret);
254 return ERR_PTR(ret);
17926a79 255
4f95dd78 256addr_in_use:
2baec2c3 257 mutex_unlock(&rxnet->local_mutex);
4f95dd78
DH
258 _leave(" = -EADDRINUSE");
259 return ERR_PTR(-EADDRINUSE);
260}
17926a79 261
09d2bf59
DH
262/*
263 * 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);
268 int n;
269
270 n = atomic_inc_return(&local->usage);
06d9532f 271 trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
09d2bf59
DH
272 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);
281
282 if (local) {
bfc18e38 283 int n = atomic_fetch_add_unless(&local->usage, 1, 0);
09d2bf59 284 if (n > 0)
06d9532f
DH
285 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
286 n + 1, here);
09d2bf59
DH
287 else
288 local = NULL;
289 }
290 return local;
291}
292
293/*
06d9532f 294 * Queue a local endpoint and pass the caller's reference to the work item.
09d2bf59
DH
295 */
296void rxrpc_queue_local(struct rxrpc_local *local)
297{
298 const void *here = __builtin_return_address(0);
06d9532f
DH
299 unsigned int debug_id = local->debug_id;
300 int n = atomic_read(&local->usage);
09d2bf59
DH
301
302 if (rxrpc_queue_work(&local->processor))
06d9532f 303 trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
730c5fd4
DH
304 else
305 rxrpc_put_local(local);
17926a79
DH
306}
307
09d2bf59
DH
308/*
309 * 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);
fac20b9e 314 unsigned int debug_id;
09d2bf59
DH
315 int n;
316
317 if (local) {
fac20b9e
DH
318 debug_id = local->debug_id;
319
09d2bf59 320 n = atomic_dec_return(&local->usage);
fac20b9e 321 trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
09d2bf59
DH
322
323 if (n == 0)
730c5fd4 324 call_rcu(&local->rcu, rxrpc_local_rcu);
09d2bf59
DH
325 }
326}
327
730c5fd4
DH
328/*
329 * Start using a local endpoint.
330 */
331struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
332{
730c5fd4
DH
333 local = rxrpc_get_local_maybe(local);
334 if (!local)
335 return NULL;
336
04d36d74 337 if (!__rxrpc_use_local(local)) {
730c5fd4
DH
338 rxrpc_put_local(local);
339 return NULL;
340 }
341
342 return local;
343}
344
345/*
346 * Cease using a local endpoint. Once the number of active users reaches 0, we
347 * start the closure of the transport in the work processor.
348 */
349void rxrpc_unuse_local(struct rxrpc_local *local)
350{
68553f1a 351 if (local) {
04d36d74
DH
352 if (__rxrpc_unuse_local(local)) {
353 rxrpc_get_local(local);
68553f1a 354 rxrpc_queue_local(local);
04d36d74 355 }
68553f1a 356 }
730c5fd4
DH
357}
358
17926a79 359/*
4f95dd78
DH
360 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
361 * of.
362 *
363 * Closing the socket cannot be done from bottom half context or RCU callback
364 * context because it might sleep.
17926a79 365 */
4f95dd78 366static void rxrpc_local_destroyer(struct rxrpc_local *local)
17926a79 367{
4f95dd78 368 struct socket *socket = local->socket;
2baec2c3 369 struct rxrpc_net *rxnet = local->rxnet;
17926a79 370
4f95dd78 371 _enter("%d", local->debug_id);
17926a79 372
d12040b6
DH
373 local->dead = true;
374
2baec2c3 375 mutex_lock(&rxnet->local_mutex);
33912c26 376 hlist_del_init_rcu(&local->link);
2baec2c3 377 mutex_unlock(&rxnet->local_mutex);
4f95dd78 378
d12040b6
DH
379 rxrpc_clean_up_local_conns(local);
380 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
1e9e5c95 381 ASSERT(!local->service);
4f95dd78
DH
382
383 if (socket) {
384 local->socket = NULL;
385 kernel_sock_shutdown(socket, SHUT_RDWR);
386 socket->sk->sk_user_data = NULL;
387 sock_release(socket);
388 }
389
390 /* At this point, there should be no more packets coming in to the
391 * local endpoint.
392 */
4f95dd78
DH
393 rxrpc_purge_queue(&local->reject_queue);
394 rxrpc_purge_queue(&local->event_queue);
17926a79
DH
395}
396
397/*
730c5fd4
DH
398 * Process events on an endpoint. The work item carries a ref which
399 * we must release.
17926a79 400 */
4f95dd78 401static void rxrpc_local_processor(struct work_struct *work)
17926a79
DH
402{
403 struct rxrpc_local *local =
4f95dd78
DH
404 container_of(work, struct rxrpc_local, processor);
405 bool again;
17926a79 406
06d9532f 407 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
09d2bf59 408 atomic_read(&local->usage), NULL);
17926a79 409
4f95dd78
DH
410 do {
411 again = false;
04d36d74 412 if (!__rxrpc_use_local(local)) {
730c5fd4
DH
413 rxrpc_local_destroyer(local);
414 break;
415 }
17926a79 416
4f95dd78
DH
417 if (!skb_queue_empty(&local->reject_queue)) {
418 rxrpc_reject_packets(local);
419 again = true;
420 }
17926a79 421
4f95dd78
DH
422 if (!skb_queue_empty(&local->event_queue)) {
423 rxrpc_process_local_events(local);
424 again = true;
425 }
04d36d74
DH
426
427 __rxrpc_unuse_local(local);
4f95dd78 428 } while (again);
730c5fd4
DH
429
430 rxrpc_put_local(local);
4f95dd78 431}
17926a79 432
4f95dd78
DH
433/*
434 * Destroy a local endpoint after the RCU grace period expires.
435 */
436static void rxrpc_local_rcu(struct rcu_head *rcu)
437{
438 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
17926a79 439
4f95dd78 440 _enter("%d", local->debug_id);
17926a79 441
4f95dd78 442 ASSERT(!work_pending(&local->processor));
17926a79
DH
443
444 _net("DESTROY LOCAL %d", local->debug_id);
445 kfree(local);
17926a79
DH
446 _leave("");
447}
448
449/*
4f95dd78 450 * Verify the local endpoint list is empty by this point.
17926a79 451 */
2baec2c3 452void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
17926a79 453{
4f95dd78 454 struct rxrpc_local *local;
17926a79
DH
455
456 _enter("");
457
dee46364 458 flush_workqueue(rxrpc_workqueue);
17926a79 459
33912c26 460 if (!hlist_empty(&rxnet->local_endpoints)) {
2baec2c3 461 mutex_lock(&rxnet->local_mutex);
33912c26 462 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
dee46364
DH
463 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
464 local, atomic_read(&local->usage));
465 }
2baec2c3 466 mutex_unlock(&rxnet->local_mutex);
dee46364 467 BUG();
17926a79 468 }
17926a79 469}