xref: /openbmc/linux/net/rxrpc/local_object.c (revision fac59652993f075d57860769c99045b3ca18780d)
1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
287563616SDavid Howells /* Local endpoint object management
38c3e34a4SDavid Howells  *
44f95dd78SDavid Howells  * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells  */
78c3e34a4SDavid Howells 
88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c3e34a4SDavid Howells 
108c3e34a4SDavid Howells #include <linux/module.h>
118c3e34a4SDavid Howells #include <linux/net.h>
128c3e34a4SDavid Howells #include <linux/skbuff.h>
138c3e34a4SDavid Howells #include <linux/slab.h>
148c3e34a4SDavid Howells #include <linux/udp.h>
158c3e34a4SDavid Howells #include <linux/ip.h>
164f95dd78SDavid Howells #include <linux/hashtable.h>
178c3e34a4SDavid Howells #include <net/sock.h>
185271953cSDavid Howells #include <net/udp.h>
195d30c626SXin Long #include <net/udp_tunnel.h>
208c3e34a4SDavid Howells #include <net/af_rxrpc.h>
218c3e34a4SDavid Howells #include "ar-internal.h"
228c3e34a4SDavid Howells 
234f95dd78SDavid Howells static void rxrpc_local_rcu(struct rcu_head *);
248c3e34a4SDavid Howells 
258c3e34a4SDavid Howells /*
26b6c66c43SDavid Howells  * Handle an ICMP/ICMP6 error turning up at the tunnel.  Push it through the
27b6c66c43SDavid Howells  * usual mechanism so that it gets parsed and presented through the UDP
28b6c66c43SDavid Howells  * socket's error_report().
29b6c66c43SDavid Howells  */
rxrpc_encap_err_rcv(struct sock * sk,struct sk_buff * skb,int err,__be16 port,u32 info,u8 * payload)30b6c66c43SDavid Howells static void rxrpc_encap_err_rcv(struct sock *sk, struct sk_buff *skb, int err,
31b6c66c43SDavid Howells 				__be16 port, u32 info, u8 *payload)
32b6c66c43SDavid Howells {
33b6c66c43SDavid Howells 	if (ip_hdr(skb)->version == IPVERSION)
34b6c66c43SDavid Howells 		return ip_icmp_error(sk, skb, err, port, info, payload);
3541cf3a91SDavid Howells 	if (IS_ENABLED(CONFIG_AF_RXRPC_IPV6))
36b6c66c43SDavid Howells 		return ipv6_icmp_error(sk, skb, err, port, info, payload);
37b6c66c43SDavid Howells }
38b6c66c43SDavid Howells 
39b6c66c43SDavid Howells /*
405c4017a2SDavid Howells  * Set or clear the Don't Fragment flag on a socket.
415c4017a2SDavid Howells  */
rxrpc_local_dont_fragment(const struct rxrpc_local * local,bool set)425c4017a2SDavid Howells void rxrpc_local_dont_fragment(const struct rxrpc_local *local, bool set)
435c4017a2SDavid Howells {
445c4017a2SDavid Howells 	if (set)
455c4017a2SDavid Howells 		ip_sock_set_mtu_discover(local->socket->sk, IP_PMTUDISC_DO);
465c4017a2SDavid Howells 	else
475c4017a2SDavid Howells 		ip_sock_set_mtu_discover(local->socket->sk, IP_PMTUDISC_DONT);
485c4017a2SDavid Howells }
495c4017a2SDavid Howells 
505c4017a2SDavid Howells /*
514f95dd78SDavid Howells  * Compare a local to an address.  Return -ve, 0 or +ve to indicate less than,
524f95dd78SDavid Howells  * same or greater than.
534f95dd78SDavid Howells  *
544f95dd78SDavid Howells  * We explicitly don't compare the RxRPC service ID as we want to reject
554f95dd78SDavid Howells  * conflicting uses by differing services.  Further, we don't want to share
564f95dd78SDavid Howells  * addresses with different options (IPv6), so we don't compare those bits
574f95dd78SDavid Howells  * either.
588c3e34a4SDavid Howells  */
rxrpc_local_cmp_key(const struct rxrpc_local * local,const struct sockaddr_rxrpc * srx)594f95dd78SDavid Howells static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
604f95dd78SDavid Howells 				const struct sockaddr_rxrpc *srx)
614f95dd78SDavid Howells {
624f95dd78SDavid Howells 	long diff;
634f95dd78SDavid Howells 
644f95dd78SDavid Howells 	diff = ((local->srx.transport_type - srx->transport_type) ?:
654f95dd78SDavid Howells 		(local->srx.transport_len - srx->transport_len) ?:
664f95dd78SDavid Howells 		(local->srx.transport.family - srx->transport.family));
674f95dd78SDavid Howells 	if (diff != 0)
684f95dd78SDavid Howells 		return diff;
694f95dd78SDavid Howells 
704f95dd78SDavid Howells 	switch (srx->transport.family) {
714f95dd78SDavid Howells 	case AF_INET:
724f95dd78SDavid Howells 		/* If the choice of UDP port is left up to the transport, then
734f95dd78SDavid Howells 		 * the endpoint record doesn't match.
744f95dd78SDavid Howells 		 */
754f95dd78SDavid Howells 		return ((u16 __force)local->srx.transport.sin.sin_port -
764f95dd78SDavid Howells 			(u16 __force)srx->transport.sin.sin_port) ?:
774f95dd78SDavid Howells 			memcmp(&local->srx.transport.sin.sin_addr,
784f95dd78SDavid Howells 			       &srx->transport.sin.sin_addr,
794f95dd78SDavid Howells 			       sizeof(struct in_addr));
80d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
8175b54cb5SDavid Howells 	case AF_INET6:
8275b54cb5SDavid Howells 		/* If the choice of UDP6 port is left up to the transport, then
8375b54cb5SDavid Howells 		 * the endpoint record doesn't match.
8475b54cb5SDavid Howells 		 */
8575b54cb5SDavid Howells 		return ((u16 __force)local->srx.transport.sin6.sin6_port -
8675b54cb5SDavid Howells 			(u16 __force)srx->transport.sin6.sin6_port) ?:
8775b54cb5SDavid Howells 			memcmp(&local->srx.transport.sin6.sin6_addr,
8875b54cb5SDavid Howells 			       &srx->transport.sin6.sin6_addr,
8975b54cb5SDavid Howells 			       sizeof(struct in6_addr));
90d1912747SDavid Howells #endif
914f95dd78SDavid Howells 	default:
924f95dd78SDavid Howells 		BUG();
934f95dd78SDavid Howells 	}
944f95dd78SDavid Howells }
954f95dd78SDavid Howells 
rxrpc_client_conn_reap_timeout(struct timer_list * timer)960d6bf319SDavid Howells static void rxrpc_client_conn_reap_timeout(struct timer_list *timer)
970d6bf319SDavid Howells {
980d6bf319SDavid Howells 	struct rxrpc_local *local =
990d6bf319SDavid Howells 		container_of(timer, struct rxrpc_local, client_conn_reap_timer);
1000d6bf319SDavid Howells 
101b8a4f0a2SDavid Howells 	if (!local->kill_all_client_conns &&
1020d6bf319SDavid Howells 	    test_and_set_bit(RXRPC_CLIENT_CONN_REAP_TIMER, &local->client_conn_flags))
1030d6bf319SDavid Howells 		rxrpc_wake_up_io_thread(local);
1040d6bf319SDavid Howells }
1050d6bf319SDavid Howells 
1064f95dd78SDavid Howells /*
1074f95dd78SDavid Howells  * Allocate a new local endpoint.
1084f95dd78SDavid Howells  */
rxrpc_alloc_local(struct net * net,const struct sockaddr_rxrpc * srx)1098a758d98SDavid Howells static struct rxrpc_local *rxrpc_alloc_local(struct net *net,
1102baec2c3SDavid Howells 					     const struct sockaddr_rxrpc *srx)
1118c3e34a4SDavid Howells {
1128c3e34a4SDavid Howells 	struct rxrpc_local *local;
113f06cb291SDavid Howells 	u32 tmp;
1148c3e34a4SDavid Howells 
1158c3e34a4SDavid Howells 	local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
1168c3e34a4SDavid Howells 	if (local) {
117a0575429SDavid Howells 		refcount_set(&local->ref, 1);
118730c5fd4SDavid Howells 		atomic_set(&local->active_users, 1);
1198a758d98SDavid Howells 		local->net = net;
1208a758d98SDavid Howells 		local->rxnet = rxrpc_net(net);
12133912c26SDavid Howells 		INIT_HLIST_NODE(&local->link);
1228fbcc833SDavid Howells 		init_completion(&local->io_thread_ready);
123af094824SDavid Howells #ifdef CONFIG_AF_RXRPC_INJECT_RX_DELAY
124af094824SDavid Howells 		skb_queue_head_init(&local->rx_delay_queue);
125af094824SDavid Howells #endif
126a275da62SDavid Howells 		skb_queue_head_init(&local->rx_queue);
127f2cce89aSDavid Howells 		INIT_LIST_HEAD(&local->conn_attend_q);
12815f661dcSDavid Howells 		INIT_LIST_HEAD(&local->call_attend_q);
1290d6bf319SDavid Howells 
130245500d8SDavid Howells 		local->client_bundles = RB_ROOT;
131245500d8SDavid Howells 		spin_lock_init(&local->client_bundles_lock);
1320d6bf319SDavid Howells 		local->kill_all_client_conns = false;
1330d6bf319SDavid Howells 		INIT_LIST_HEAD(&local->idle_client_conns);
1340d6bf319SDavid Howells 		timer_setup(&local->client_conn_reap_timer,
1350d6bf319SDavid Howells 			    rxrpc_client_conn_reap_timeout, 0);
1360d6bf319SDavid Howells 
1378c3e34a4SDavid Howells 		spin_lock_init(&local->lock);
1388c3e34a4SDavid Howells 		rwlock_init(&local->services_lock);
1398c3e34a4SDavid Howells 		local->debug_id = atomic_inc_return(&rxrpc_debug_id);
1408c3e34a4SDavid Howells 		memcpy(&local->srx, srx, sizeof(*srx));
14128036f44SDavid Howells 		local->srx.srx_service = 0;
142f06cb291SDavid Howells 		idr_init(&local->conn_ids);
143f06cb291SDavid Howells 		get_random_bytes(&tmp, sizeof(tmp));
144f06cb291SDavid Howells 		tmp &= 0x3fffffff;
145f06cb291SDavid Howells 		if (tmp == 0)
146f06cb291SDavid Howells 			tmp = 1;
147f06cb291SDavid Howells 		idr_set_cursor(&local->conn_ids, tmp);
1489d35d880SDavid Howells 		INIT_LIST_HEAD(&local->new_client_calls);
1499d35d880SDavid Howells 		spin_lock_init(&local->client_call_lock);
150f06cb291SDavid Howells 
1510fde882fSDavid Howells 		trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, 1);
1528c3e34a4SDavid Howells 	}
1538c3e34a4SDavid Howells 
1548c3e34a4SDavid Howells 	_leave(" = %p", local);
1558c3e34a4SDavid Howells 	return local;
1568c3e34a4SDavid Howells }
1578c3e34a4SDavid Howells 
1588c3e34a4SDavid Howells /*
1598c3e34a4SDavid Howells  * create the local socket
1604f95dd78SDavid Howells  * - must be called with rxrpc_local_mutex locked
1618c3e34a4SDavid Howells  */
rxrpc_open_socket(struct rxrpc_local * local,struct net * net)1622baec2c3SDavid Howells static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
1638c3e34a4SDavid Howells {
1641a9b86c9SXin Long 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
1651a9b86c9SXin Long 	struct sockaddr_rxrpc *srx = &local->srx;
1661a9b86c9SXin Long 	struct udp_port_cfg udp_conf = {0};
167a275da62SDavid Howells 	struct task_struct *io_thread;
1685271953cSDavid Howells 	struct sock *usk;
169fce93494SChristoph Hellwig 	int ret;
1708c3e34a4SDavid Howells 
17175b54cb5SDavid Howells 	_enter("%p{%d,%d}",
1721a9b86c9SXin Long 	       local, srx->transport_type, srx->transport.family);
1738c3e34a4SDavid Howells 
1741a9b86c9SXin Long 	udp_conf.family = srx->transport.family;
17539cb9faaSDavid Howells 	udp_conf.use_udp_checksums = true;
1761a9b86c9SXin Long 	if (udp_conf.family == AF_INET) {
1771a9b86c9SXin Long 		udp_conf.local_ip = srx->transport.sin.sin_addr;
1781a9b86c9SXin Long 		udp_conf.local_udp_port = srx->transport.sin.sin_port;
179295f830eSVadim Fedorenko #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
1801a9b86c9SXin Long 	} else {
1811a9b86c9SXin Long 		udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
1821a9b86c9SXin Long 		udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
18339cb9faaSDavid Howells 		udp_conf.use_udp6_tx_checksums = true;
18439cb9faaSDavid Howells 		udp_conf.use_udp6_rx_checksums = true;
185295f830eSVadim Fedorenko #endif
1861a9b86c9SXin Long 	}
1871a9b86c9SXin Long 	ret = udp_sock_create(net, &udp_conf, &local->socket);
1888c3e34a4SDavid Howells 	if (ret < 0) {
1898c3e34a4SDavid Howells 		_leave(" = %d [socket]", ret);
1908c3e34a4SDavid Howells 		return ret;
1918c3e34a4SDavid Howells 	}
1928c3e34a4SDavid Howells 
1931a9b86c9SXin Long 	tuncfg.encap_type = UDP_ENCAP_RXRPC;
194446b3e14SDavid Howells 	tuncfg.encap_rcv = rxrpc_encap_rcv;
195ac56a0b4SDavid Howells 	tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
1961a9b86c9SXin Long 	tuncfg.sk_user_data = local;
1971a9b86c9SXin Long 	setup_udp_tunnel_sock(net, local->socket, &tuncfg);
1981a9b86c9SXin Long 
1992cfa2271SDavid Howells 	/* set the socket up */
2005271953cSDavid Howells 	usk = local->socket->sk;
2015271953cSDavid Howells 	usk->sk_error_report = rxrpc_error_report;
2022cfa2271SDavid Howells 
2031a9b86c9SXin Long 	switch (srx->transport.family) {
20437a675e7SDavid Howells 	case AF_INET6:
20537a675e7SDavid Howells 		/* we want to receive ICMPv6 errors */
2061a9b86c9SXin Long 		ip6_sock_set_recverr(usk);
20737a675e7SDavid Howells 
20837a675e7SDavid Howells 		/* Fall through and set IPv4 options too otherwise we don't get
20937a675e7SDavid Howells 		 * errors from IPv4 packets sent through the IPv6 socket.
21037a675e7SDavid Howells 		 */
211df561f66SGustavo A. R. Silva 		fallthrough;
212f2aeed3aSDavid Howells 	case AF_INET:
2138c3e34a4SDavid Howells 		/* we want to receive ICMP errors */
2141a9b86c9SXin Long 		ip_sock_set_recverr(usk);
2158c3e34a4SDavid Howells 
2168c3e34a4SDavid Howells 		/* we want to set the don't fragment bit */
2175c4017a2SDavid Howells 		rxrpc_local_dont_fragment(local, true);
218b604dd98SDavid Howells 
219b604dd98SDavid Howells 		/* We want receive timestamps. */
2201a9b86c9SXin Long 		sock_enable_timestamps(usk);
221f2aeed3aSDavid Howells 		break;
222f2aeed3aSDavid Howells 
223f2aeed3aSDavid Howells 	default:
224f2aeed3aSDavid Howells 		BUG();
225f2aeed3aSDavid Howells 	}
2268c3e34a4SDavid Howells 
227a275da62SDavid Howells 	io_thread = kthread_run(rxrpc_io_thread, local,
228a275da62SDavid Howells 				"krxrpcio/%u", ntohs(udp_conf.local_udp_port));
229a275da62SDavid Howells 	if (IS_ERR(io_thread)) {
230a275da62SDavid Howells 		ret = PTR_ERR(io_thread);
231a275da62SDavid Howells 		goto error_sock;
232a275da62SDavid Howells 	}
233a275da62SDavid Howells 
2348fbcc833SDavid Howells 	wait_for_completion(&local->io_thread_ready);
235*cdf4bbbdSDavid Howells 	WRITE_ONCE(local->io_thread, io_thread);
2368c3e34a4SDavid Howells 	_leave(" = 0");
2378c3e34a4SDavid Howells 	return 0;
238a275da62SDavid Howells 
239a275da62SDavid Howells error_sock:
240a275da62SDavid Howells 	kernel_sock_shutdown(local->socket, SHUT_RDWR);
241a275da62SDavid Howells 	local->socket->sk->sk_user_data = NULL;
242a275da62SDavid Howells 	sock_release(local->socket);
243a275da62SDavid Howells 	local->socket = NULL;
244a275da62SDavid Howells 	return ret;
2458c3e34a4SDavid Howells }
2468c3e34a4SDavid Howells 
2478c3e34a4SDavid Howells /*
2484f95dd78SDavid Howells  * Look up or create a new local endpoint using the specified local address.
2498c3e34a4SDavid Howells  */
rxrpc_lookup_local(struct net * net,const struct sockaddr_rxrpc * srx)2502baec2c3SDavid Howells struct rxrpc_local *rxrpc_lookup_local(struct net *net,
2512baec2c3SDavid Howells 				       const struct sockaddr_rxrpc *srx)
2528c3e34a4SDavid Howells {
2538c3e34a4SDavid Howells 	struct rxrpc_local *local;
2542baec2c3SDavid Howells 	struct rxrpc_net *rxnet = rxrpc_net(net);
25533912c26SDavid Howells 	struct hlist_node *cursor;
2564f95dd78SDavid Howells 	long diff;
2578c3e34a4SDavid Howells 	int ret;
2588c3e34a4SDavid Howells 
25975b54cb5SDavid Howells 	_enter("{%d,%d,%pISp}",
26075b54cb5SDavid Howells 	       srx->transport_type, srx->transport.family, &srx->transport);
2618c3e34a4SDavid Howells 
2622baec2c3SDavid Howells 	mutex_lock(&rxnet->local_mutex);
2638c3e34a4SDavid Howells 
26433912c26SDavid Howells 	hlist_for_each(cursor, &rxnet->local_endpoints) {
26533912c26SDavid Howells 		local = hlist_entry(cursor, struct rxrpc_local, link);
2668c3e34a4SDavid Howells 
2674f95dd78SDavid Howells 		diff = rxrpc_local_cmp_key(local, srx);
26833912c26SDavid Howells 		if (diff != 0)
2694f95dd78SDavid Howells 			continue;
2704f95dd78SDavid Howells 
2714f95dd78SDavid Howells 		/* Services aren't allowed to share transport sockets, so
2724f95dd78SDavid Howells 		 * reject that here.  It is possible that the object is dying -
2734f95dd78SDavid Howells 		 * but it may also still have the local transport address that
2744f95dd78SDavid Howells 		 * we want bound.
2754f95dd78SDavid Howells 		 */
2764f95dd78SDavid Howells 		if (srx->srx_service) {
2774f95dd78SDavid Howells 			local = NULL;
2784f95dd78SDavid Howells 			goto addr_in_use;
2794f95dd78SDavid Howells 		}
2804f95dd78SDavid Howells 
28133912c26SDavid Howells 		/* Found a match.  We want to replace a dying object.
28233912c26SDavid Howells 		 * Attempting to bind the transport socket may still fail if
28333912c26SDavid Howells 		 * we're attempting to use a local address that the dying
28433912c26SDavid Howells 		 * object is still using.
2854f95dd78SDavid Howells 		 */
2860fde882fSDavid Howells 		if (!rxrpc_use_local(local, rxrpc_local_use_lookup))
2874f95dd78SDavid Howells 			break;
2884f95dd78SDavid Howells 
2894f95dd78SDavid Howells 		goto found;
2904f95dd78SDavid Howells 	}
2914f95dd78SDavid Howells 
2928a758d98SDavid Howells 	local = rxrpc_alloc_local(net, srx);
2934f95dd78SDavid Howells 	if (!local)
2944f95dd78SDavid Howells 		goto nomem;
2954f95dd78SDavid Howells 
2962baec2c3SDavid Howells 	ret = rxrpc_open_socket(local, net);
2974f95dd78SDavid Howells 	if (ret < 0)
2984f95dd78SDavid Howells 		goto sock_error;
2994f95dd78SDavid Howells 
30033912c26SDavid Howells 	if (cursor) {
30133912c26SDavid Howells 		hlist_replace_rcu(cursor, &local->link);
30233912c26SDavid Howells 		cursor->pprev = NULL;
30333912c26SDavid Howells 	} else {
30433912c26SDavid Howells 		hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
30533912c26SDavid Howells 	}
3064f95dd78SDavid Howells 
3074f95dd78SDavid Howells found:
3082baec2c3SDavid Howells 	mutex_unlock(&rxnet->local_mutex);
3094f95dd78SDavid Howells 	_leave(" = %p", local);
3104f95dd78SDavid Howells 	return local;
3118c3e34a4SDavid Howells 
3124f95dd78SDavid Howells nomem:
3134f95dd78SDavid Howells 	ret = -ENOMEM;
3144f95dd78SDavid Howells sock_error:
3152baec2c3SDavid Howells 	mutex_unlock(&rxnet->local_mutex);
316032be5f1SEric Dumazet 	if (local)
317032be5f1SEric Dumazet 		call_rcu(&local->rcu, rxrpc_local_rcu);
3188c3e34a4SDavid Howells 	_leave(" = %d", ret);
3198c3e34a4SDavid Howells 	return ERR_PTR(ret);
3208c3e34a4SDavid Howells 
3214f95dd78SDavid Howells addr_in_use:
3222baec2c3SDavid Howells 	mutex_unlock(&rxnet->local_mutex);
3234f95dd78SDavid Howells 	_leave(" = -EADDRINUSE");
3244f95dd78SDavid Howells 	return ERR_PTR(-EADDRINUSE);
3258c3e34a4SDavid Howells }
3268c3e34a4SDavid Howells 
3278c3e34a4SDavid Howells /*
32809d2bf59SDavid Howells  * Get a ref on a local endpoint.
32909d2bf59SDavid Howells  */
rxrpc_get_local(struct rxrpc_local * local,enum rxrpc_local_trace why)3300fde882fSDavid Howells struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local,
3310fde882fSDavid Howells 				    enum rxrpc_local_trace why)
33209d2bf59SDavid Howells {
3330fde882fSDavid Howells 	int r, u;
33409d2bf59SDavid Howells 
3350fde882fSDavid Howells 	u = atomic_read(&local->active_users);
336a0575429SDavid Howells 	__refcount_inc(&local->ref, &r);
3370fde882fSDavid Howells 	trace_rxrpc_local(local->debug_id, why, r + 1, u);
33809d2bf59SDavid Howells 	return local;
33909d2bf59SDavid Howells }
34009d2bf59SDavid Howells 
34109d2bf59SDavid Howells /*
34209d2bf59SDavid Howells  * Get a ref on a local endpoint unless its usage has already reached 0.
34309d2bf59SDavid Howells  */
rxrpc_get_local_maybe(struct rxrpc_local * local,enum rxrpc_local_trace why)3440fde882fSDavid Howells struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local,
3450fde882fSDavid Howells 					  enum rxrpc_local_trace why)
34609d2bf59SDavid Howells {
3470fde882fSDavid Howells 	int r, u;
34809d2bf59SDavid Howells 
3490fde882fSDavid Howells 	if (local && __refcount_inc_not_zero(&local->ref, &r)) {
3500fde882fSDavid Howells 		u = atomic_read(&local->active_users);
3510fde882fSDavid Howells 		trace_rxrpc_local(local->debug_id, why, r + 1, u);
35209d2bf59SDavid Howells 		return local;
35309d2bf59SDavid Howells 	}
35409d2bf59SDavid Howells 
3550fde882fSDavid Howells 	return NULL;
3560fde882fSDavid Howells }
3570fde882fSDavid Howells 
35809d2bf59SDavid Howells /*
35909d2bf59SDavid Howells  * Drop a ref on a local endpoint.
36009d2bf59SDavid Howells  */
rxrpc_put_local(struct rxrpc_local * local,enum rxrpc_local_trace why)3610fde882fSDavid Howells void rxrpc_put_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
36209d2bf59SDavid Howells {
363fac20b9eSDavid Howells 	unsigned int debug_id;
364a0575429SDavid Howells 	bool dead;
3650fde882fSDavid Howells 	int r, u;
36609d2bf59SDavid Howells 
36709d2bf59SDavid Howells 	if (local) {
368fac20b9eSDavid Howells 		debug_id = local->debug_id;
369fac20b9eSDavid Howells 
3700fde882fSDavid Howells 		u = atomic_read(&local->active_users);
371a0575429SDavid Howells 		dead = __refcount_dec_and_test(&local->ref, &r);
3720fde882fSDavid Howells 		trace_rxrpc_local(debug_id, why, r, u);
37309d2bf59SDavid Howells 
374a0575429SDavid Howells 		if (dead)
375730c5fd4SDavid Howells 			call_rcu(&local->rcu, rxrpc_local_rcu);
37609d2bf59SDavid Howells 	}
37709d2bf59SDavid Howells }
37809d2bf59SDavid Howells 
37909d2bf59SDavid Howells /*
380730c5fd4SDavid Howells  * Start using a local endpoint.
381730c5fd4SDavid Howells  */
rxrpc_use_local(struct rxrpc_local * local,enum rxrpc_local_trace why)3820fde882fSDavid Howells struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local,
3830fde882fSDavid Howells 				    enum rxrpc_local_trace why)
384730c5fd4SDavid Howells {
3850fde882fSDavid Howells 	local = rxrpc_get_local_maybe(local, rxrpc_local_get_for_use);
386730c5fd4SDavid Howells 	if (!local)
387730c5fd4SDavid Howells 		return NULL;
388730c5fd4SDavid Howells 
3890fde882fSDavid Howells 	if (!__rxrpc_use_local(local, why)) {
3900fde882fSDavid Howells 		rxrpc_put_local(local, rxrpc_local_put_for_use);
391730c5fd4SDavid Howells 		return NULL;
392730c5fd4SDavid Howells 	}
393730c5fd4SDavid Howells 
394730c5fd4SDavid Howells 	return local;
395730c5fd4SDavid Howells }
396730c5fd4SDavid Howells 
397730c5fd4SDavid Howells /*
398730c5fd4SDavid Howells  * Cease using a local endpoint.  Once the number of active users reaches 0, we
3995e6ef4f1SDavid Howells  * start the closure of the transport in the I/O thread..
400730c5fd4SDavid Howells  */
rxrpc_unuse_local(struct rxrpc_local * local,enum rxrpc_local_trace why)4010fde882fSDavid Howells void rxrpc_unuse_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
402730c5fd4SDavid Howells {
403eaa02390SDavid Howells 	unsigned int debug_id;
404a2cf3264SDavid Howells 	int r, u;
405a2cf3264SDavid Howells 
406a2cf3264SDavid Howells 	if (local) {
407eaa02390SDavid Howells 		debug_id = local->debug_id;
408a2cf3264SDavid Howells 		r = refcount_read(&local->ref);
409a2cf3264SDavid Howells 		u = atomic_dec_return(&local->active_users);
410a2cf3264SDavid Howells 		trace_rxrpc_local(debug_id, why, r, u);
411a2cf3264SDavid Howells 		if (u == 0)
412a275da62SDavid Howells 			kthread_stop(local->io_thread);
41368553f1aSDavid Howells 	}
414a2cf3264SDavid Howells }
415730c5fd4SDavid Howells 
416730c5fd4SDavid Howells /*
4174f95dd78SDavid Howells  * Destroy a local endpoint's socket and then hand the record to RCU to dispose
4184f95dd78SDavid Howells  * of.
4194f95dd78SDavid Howells  *
4204f95dd78SDavid Howells  * Closing the socket cannot be done from bottom half context or RCU callback
4214f95dd78SDavid Howells  * context because it might sleep.
4228c3e34a4SDavid Howells  */
rxrpc_destroy_local(struct rxrpc_local * local)423a275da62SDavid Howells void rxrpc_destroy_local(struct rxrpc_local *local)
4248c3e34a4SDavid Howells {
4254f95dd78SDavid Howells 	struct socket *socket = local->socket;
4262baec2c3SDavid Howells 	struct rxrpc_net *rxnet = local->rxnet;
4278c3e34a4SDavid Howells 
4284f95dd78SDavid Howells 	_enter("%d", local->debug_id);
4298c3e34a4SDavid Howells 
430d12040b6SDavid Howells 	local->dead = true;
431d12040b6SDavid Howells 
4322baec2c3SDavid Howells 	mutex_lock(&rxnet->local_mutex);
43333912c26SDavid Howells 	hlist_del_init_rcu(&local->link);
4342baec2c3SDavid Howells 	mutex_unlock(&rxnet->local_mutex);
4358c3e34a4SDavid Howells 
436d12040b6SDavid Howells 	rxrpc_clean_up_local_conns(local);
437d12040b6SDavid Howells 	rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
4381e9e5c95SDavid Howells 	ASSERT(!local->service);
4398c3e34a4SDavid Howells 
4404f95dd78SDavid Howells 	if (socket) {
4414f95dd78SDavid Howells 		local->socket = NULL;
4424f95dd78SDavid Howells 		kernel_sock_shutdown(socket, SHUT_RDWR);
4434f95dd78SDavid Howells 		socket->sk->sk_user_data = NULL;
4444f95dd78SDavid Howells 		sock_release(socket);
4454f95dd78SDavid Howells 	}
4464f95dd78SDavid Howells 
4474f95dd78SDavid Howells 	/* At this point, there should be no more packets coming in to the
4484f95dd78SDavid Howells 	 * local endpoint.
4494f95dd78SDavid Howells 	 */
450af094824SDavid Howells #ifdef CONFIG_AF_RXRPC_INJECT_RX_DELAY
451af094824SDavid Howells 	rxrpc_purge_queue(&local->rx_delay_queue);
452af094824SDavid Howells #endif
453a275da62SDavid Howells 	rxrpc_purge_queue(&local->rx_queue);
4549d35d880SDavid Howells 	rxrpc_purge_client_connections(local);
4554f95dd78SDavid Howells }
4564f95dd78SDavid Howells 
4574f95dd78SDavid Howells /*
4584f95dd78SDavid Howells  * Destroy a local endpoint after the RCU grace period expires.
4594f95dd78SDavid Howells  */
rxrpc_local_rcu(struct rcu_head * rcu)4604f95dd78SDavid Howells static void rxrpc_local_rcu(struct rcu_head *rcu)
4614f95dd78SDavid Howells {
4624f95dd78SDavid Howells 	struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
4634f95dd78SDavid Howells 
4640fde882fSDavid Howells 	rxrpc_see_local(local, rxrpc_local_free);
4658c3e34a4SDavid Howells 	kfree(local);
4668c3e34a4SDavid Howells }
4678c3e34a4SDavid Howells 
4688c3e34a4SDavid Howells /*
4694f95dd78SDavid Howells  * Verify the local endpoint list is empty by this point.
4708c3e34a4SDavid Howells  */
rxrpc_destroy_all_locals(struct rxrpc_net * rxnet)4712baec2c3SDavid Howells void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
4728c3e34a4SDavid Howells {
4734f95dd78SDavid Howells 	struct rxrpc_local *local;
4748c3e34a4SDavid Howells 
4758c3e34a4SDavid Howells 	_enter("");
4768c3e34a4SDavid Howells 
477dee46364SDavid Howells 	flush_workqueue(rxrpc_workqueue);
4788c3e34a4SDavid Howells 
47933912c26SDavid Howells 	if (!hlist_empty(&rxnet->local_endpoints)) {
4802baec2c3SDavid Howells 		mutex_lock(&rxnet->local_mutex);
48133912c26SDavid Howells 		hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
4824f95dd78SDavid Howells 			pr_err("AF_RXRPC: Leaked local %p {%d}\n",
483a0575429SDavid Howells 			       local, refcount_read(&local->ref));
4848c3e34a4SDavid Howells 		}
4852baec2c3SDavid Howells 		mutex_unlock(&rxnet->local_mutex);
4864f95dd78SDavid Howells 		BUG();
4878c3e34a4SDavid Howells 	}
488dee46364SDavid Howells }
489