1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Local endpoint object management
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <linux/udp.h>
16 #include <linux/hashtable.h>
19 #include <net/udp_tunnel.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
23 static void rxrpc_local_processor(struct work_struct *);
24 static void rxrpc_local_rcu(struct rcu_head *);
27 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
28 * same or greater than.
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
35 static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
36 const struct sockaddr_rxrpc *srx)
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));
46 switch (srx->transport.family) {
48 /* If the choice of UDP port is left up to the transport, then
49 * the endpoint record doesn't match.
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));
56 #ifdef CONFIG_AF_RXRPC_IPV6
58 /* If the choice of UDP6 port is left up to the transport, then
59 * the endpoint record doesn't match.
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));
73 * Allocate a new local endpoint.
75 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
76 const struct sockaddr_rxrpc *srx)
78 struct rxrpc_local *local;
80 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
82 atomic_set(&local->usage, 1);
83 atomic_set(&local->active_users, 1);
85 INIT_HLIST_NODE(&local->link);
86 INIT_WORK(&local->processor, rxrpc_local_processor);
87 init_rwsem(&local->defrag_sem);
88 skb_queue_head_init(&local->reject_queue);
89 skb_queue_head_init(&local->event_queue);
90 local->client_bundles = RB_ROOT;
91 spin_lock_init(&local->client_bundles_lock);
92 spin_lock_init(&local->lock);
93 rwlock_init(&local->services_lock);
94 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
95 memcpy(&local->srx, srx, sizeof(*srx));
96 local->srx.srx_service = 0;
97 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
100 _leave(" = %p", local);
105 * create the local socket
106 * - must be called with rxrpc_local_mutex locked
108 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
110 struct udp_tunnel_sock_cfg tuncfg = {NULL};
111 struct sockaddr_rxrpc *srx = &local->srx;
112 struct udp_port_cfg udp_conf = {0};
117 local, srx->transport_type, srx->transport.family);
119 udp_conf.family = srx->transport.family;
120 udp_conf.use_udp_checksums = true;
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;
124 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
126 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
127 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
128 udp_conf.use_udp6_tx_checksums = true;
129 udp_conf.use_udp6_rx_checksums = true;
132 ret = udp_sock_create(net, &udp_conf, &local->socket);
134 _leave(" = %d [socket]", ret);
138 tuncfg.encap_type = UDP_ENCAP_RXRPC;
139 tuncfg.encap_rcv = rxrpc_input_packet;
140 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
141 tuncfg.sk_user_data = local;
142 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
144 /* set the socket up */
145 usk = local->socket->sk;
146 usk->sk_error_report = rxrpc_error_report;
148 switch (srx->transport.family) {
150 /* we want to receive ICMPv6 errors */
151 ip6_sock_set_recverr(usk);
153 /* Fall through and set IPv4 options too otherwise we don't get
154 * errors from IPv4 packets sent through the IPv6 socket.
158 /* we want to receive ICMP errors */
159 ip_sock_set_recverr(usk);
161 /* we want to set the don't fragment bit */
162 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
164 /* We want receive timestamps. */
165 sock_enable_timestamps(usk);
177 * Look up or create a new local endpoint using the specified local address.
179 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
180 const struct sockaddr_rxrpc *srx)
182 struct rxrpc_local *local;
183 struct rxrpc_net *rxnet = rxrpc_net(net);
184 struct hlist_node *cursor;
189 _enter("{%d,%d,%pISp}",
190 srx->transport_type, srx->transport.family, &srx->transport);
192 mutex_lock(&rxnet->local_mutex);
194 hlist_for_each(cursor, &rxnet->local_endpoints) {
195 local = hlist_entry(cursor, struct rxrpc_local, link);
197 diff = rxrpc_local_cmp_key(local, srx);
201 /* 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
206 if (srx->srx_service) {
211 /* Found a match. We want to replace a dying object.
212 * Attempting to bind the transport socket may still fail if
213 * we're attempting to use a local address that the dying
214 * object is still using.
216 if (!rxrpc_use_local(local))
223 local = rxrpc_alloc_local(rxnet, srx);
227 ret = rxrpc_open_socket(local, net);
232 hlist_replace_rcu(cursor, &local->link);
233 cursor->pprev = NULL;
235 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
240 mutex_unlock(&rxnet->local_mutex);
242 _net("LOCAL %s %d {%pISp}",
243 age, local->debug_id, &local->srx.transport);
245 _leave(" = %p", local);
251 mutex_unlock(&rxnet->local_mutex);
253 call_rcu(&local->rcu, rxrpc_local_rcu);
254 _leave(" = %d", ret);
258 mutex_unlock(&rxnet->local_mutex);
259 _leave(" = -EADDRINUSE");
260 return ERR_PTR(-EADDRINUSE);
264 * Get a ref on a local endpoint.
266 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
268 const void *here = __builtin_return_address(0);
271 n = atomic_inc_return(&local->usage);
272 trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
277 * Get a ref on a local endpoint unless its usage has already reached 0.
279 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
281 const void *here = __builtin_return_address(0);
284 int n = atomic_fetch_add_unless(&local->usage, 1, 0);
286 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
295 * Queue a local endpoint and pass the caller's reference to the work item.
297 void rxrpc_queue_local(struct rxrpc_local *local)
299 const void *here = __builtin_return_address(0);
300 unsigned int debug_id = local->debug_id;
301 int n = atomic_read(&local->usage);
303 if (rxrpc_queue_work(&local->processor))
304 trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
306 rxrpc_put_local(local);
310 * Drop a ref on a local endpoint.
312 void rxrpc_put_local(struct rxrpc_local *local)
314 const void *here = __builtin_return_address(0);
315 unsigned int debug_id;
319 debug_id = local->debug_id;
321 n = atomic_dec_return(&local->usage);
322 trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
325 call_rcu(&local->rcu, rxrpc_local_rcu);
330 * Start using a local endpoint.
332 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
334 local = rxrpc_get_local_maybe(local);
338 if (!__rxrpc_use_local(local)) {
339 rxrpc_put_local(local);
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.
350 void rxrpc_unuse_local(struct rxrpc_local *local)
353 if (__rxrpc_unuse_local(local)) {
354 rxrpc_get_local(local);
355 rxrpc_queue_local(local);
361 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
364 * Closing the socket cannot be done from bottom half context or RCU callback
365 * context because it might sleep.
367 static void rxrpc_local_destroyer(struct rxrpc_local *local)
369 struct socket *socket = local->socket;
370 struct rxrpc_net *rxnet = local->rxnet;
372 _enter("%d", local->debug_id);
376 mutex_lock(&rxnet->local_mutex);
377 hlist_del_init_rcu(&local->link);
378 mutex_unlock(&rxnet->local_mutex);
380 rxrpc_clean_up_local_conns(local);
381 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
382 ASSERT(!local->service);
385 local->socket = NULL;
386 kernel_sock_shutdown(socket, SHUT_RDWR);
387 socket->sk->sk_user_data = NULL;
388 sock_release(socket);
391 /* At this point, there should be no more packets coming in to the
394 rxrpc_purge_queue(&local->reject_queue);
395 rxrpc_purge_queue(&local->event_queue);
399 * Process events on an endpoint. The work item carries a ref which
402 static void rxrpc_local_processor(struct work_struct *work)
404 struct rxrpc_local *local =
405 container_of(work, struct rxrpc_local, processor);
411 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
412 atomic_read(&local->usage), NULL);
416 if (!__rxrpc_use_local(local)) {
417 rxrpc_local_destroyer(local);
421 if (!skb_queue_empty(&local->reject_queue)) {
422 rxrpc_reject_packets(local);
426 if (!skb_queue_empty(&local->event_queue)) {
427 rxrpc_process_local_events(local);
431 __rxrpc_unuse_local(local);
434 rxrpc_put_local(local);
438 * Destroy a local endpoint after the RCU grace period expires.
440 static void rxrpc_local_rcu(struct rcu_head *rcu)
442 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
444 _enter("%d", local->debug_id);
446 ASSERT(!work_pending(&local->processor));
448 _net("DESTROY LOCAL %d", local->debug_id);
454 * Verify the local endpoint list is empty by this point.
456 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
458 struct rxrpc_local *local;
462 flush_workqueue(rxrpc_workqueue);
464 if (!hlist_empty(&rxnet->local_endpoints)) {
465 mutex_lock(&rxnet->local_mutex);
466 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
467 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
468 local, atomic_read(&local->usage));
470 mutex_unlock(&rxnet->local_mutex);