xref: /openbmc/linux/net/rxrpc/peer_object.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2be6e6707SDavid Howells /* RxRPC remote transport endpoint record management
38c3e34a4SDavid Howells  *
4be6e6707SDavid Howells  * Copyright (C) 2007, 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/udp.h>
148c3e34a4SDavid Howells #include <linux/in.h>
1575b54cb5SDavid Howells #include <linux/in6.h>
168c3e34a4SDavid Howells #include <linux/slab.h>
17be6e6707SDavid Howells #include <linux/hashtable.h>
188c3e34a4SDavid Howells #include <net/sock.h>
198c3e34a4SDavid Howells #include <net/af_rxrpc.h>
208c3e34a4SDavid Howells #include <net/ip.h>
218c3e34a4SDavid Howells #include <net/route.h>
2275b54cb5SDavid Howells #include <net/ip6_route.h>
238c3e34a4SDavid Howells #include "ar-internal.h"
248c3e34a4SDavid Howells 
25be6e6707SDavid Howells /*
26be6e6707SDavid Howells  * Hash a peer key.
27be6e6707SDavid Howells  */
rxrpc_peer_hash_key(struct rxrpc_local * local,const struct sockaddr_rxrpc * srx)28be6e6707SDavid Howells static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
29be6e6707SDavid Howells 					 const struct sockaddr_rxrpc *srx)
30be6e6707SDavid Howells {
31be6e6707SDavid Howells 	const u16 *p;
32be6e6707SDavid Howells 	unsigned int i, size;
33be6e6707SDavid Howells 	unsigned long hash_key;
34be6e6707SDavid Howells 
35be6e6707SDavid Howells 	_enter("");
36be6e6707SDavid Howells 
37be6e6707SDavid Howells 	hash_key = (unsigned long)local / __alignof__(*local);
38be6e6707SDavid Howells 	hash_key += srx->transport_type;
39be6e6707SDavid Howells 	hash_key += srx->transport_len;
40be6e6707SDavid Howells 	hash_key += srx->transport.family;
41be6e6707SDavid Howells 
42be6e6707SDavid Howells 	switch (srx->transport.family) {
43be6e6707SDavid Howells 	case AF_INET:
44be6e6707SDavid Howells 		hash_key += (u16 __force)srx->transport.sin.sin_port;
45be6e6707SDavid Howells 		size = sizeof(srx->transport.sin.sin_addr);
46be6e6707SDavid Howells 		p = (u16 *)&srx->transport.sin.sin_addr;
47be6e6707SDavid Howells 		break;
48d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
4975b54cb5SDavid Howells 	case AF_INET6:
5075b54cb5SDavid Howells 		hash_key += (u16 __force)srx->transport.sin.sin_port;
5175b54cb5SDavid Howells 		size = sizeof(srx->transport.sin6.sin6_addr);
5275b54cb5SDavid Howells 		p = (u16 *)&srx->transport.sin6.sin6_addr;
5375b54cb5SDavid Howells 		break;
54d1912747SDavid Howells #endif
552f9f9f52SArnd Bergmann 	default:
562f9f9f52SArnd Bergmann 		WARN(1, "AF_RXRPC: Unsupported transport address family\n");
572f9f9f52SArnd Bergmann 		return 0;
58be6e6707SDavid Howells 	}
59be6e6707SDavid Howells 
60be6e6707SDavid Howells 	/* Step through the peer address in 16-bit portions for speed */
61be6e6707SDavid Howells 	for (i = 0; i < size; i += sizeof(*p), p++)
62be6e6707SDavid Howells 		hash_key += *p;
63be6e6707SDavid Howells 
64be6e6707SDavid Howells 	_leave(" 0x%lx", hash_key);
65be6e6707SDavid Howells 	return hash_key;
66be6e6707SDavid Howells }
67be6e6707SDavid Howells 
68be6e6707SDavid Howells /*
69be6e6707SDavid Howells  * Compare a peer to a key.  Return -ve, 0 or +ve to indicate less than, same
70be6e6707SDavid Howells  * or greater than.
71be6e6707SDavid Howells  *
72be6e6707SDavid Howells  * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
73be6e6707SDavid Howells  * buckets and mid-bucket insertion, so we don't make full use of this
74be6e6707SDavid Howells  * information at this point.
75be6e6707SDavid Howells  */
rxrpc_peer_cmp_key(const struct rxrpc_peer * peer,struct rxrpc_local * local,const struct sockaddr_rxrpc * srx,unsigned long hash_key)76be6e6707SDavid Howells static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
77be6e6707SDavid Howells 			       struct rxrpc_local *local,
78be6e6707SDavid Howells 			       const struct sockaddr_rxrpc *srx,
79be6e6707SDavid Howells 			       unsigned long hash_key)
80be6e6707SDavid Howells {
81be6e6707SDavid Howells 	long diff;
82be6e6707SDavid Howells 
83be6e6707SDavid Howells 	diff = ((peer->hash_key - hash_key) ?:
84be6e6707SDavid Howells 		((unsigned long)peer->local - (unsigned long)local) ?:
85be6e6707SDavid Howells 		(peer->srx.transport_type - srx->transport_type) ?:
86be6e6707SDavid Howells 		(peer->srx.transport_len - srx->transport_len) ?:
87be6e6707SDavid Howells 		(peer->srx.transport.family - srx->transport.family));
88be6e6707SDavid Howells 	if (diff != 0)
89be6e6707SDavid Howells 		return diff;
90be6e6707SDavid Howells 
91be6e6707SDavid Howells 	switch (srx->transport.family) {
92be6e6707SDavid Howells 	case AF_INET:
93be6e6707SDavid Howells 		return ((u16 __force)peer->srx.transport.sin.sin_port -
94be6e6707SDavid Howells 			(u16 __force)srx->transport.sin.sin_port) ?:
95be6e6707SDavid Howells 			memcmp(&peer->srx.transport.sin.sin_addr,
96be6e6707SDavid Howells 			       &srx->transport.sin.sin_addr,
97be6e6707SDavid Howells 			       sizeof(struct in_addr));
98d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
9975b54cb5SDavid Howells 	case AF_INET6:
10075b54cb5SDavid Howells 		return ((u16 __force)peer->srx.transport.sin6.sin6_port -
10175b54cb5SDavid Howells 			(u16 __force)srx->transport.sin6.sin6_port) ?:
10275b54cb5SDavid Howells 			memcmp(&peer->srx.transport.sin6.sin6_addr,
10375b54cb5SDavid Howells 			       &srx->transport.sin6.sin6_addr,
10475b54cb5SDavid Howells 			       sizeof(struct in6_addr));
105d1912747SDavid Howells #endif
106be6e6707SDavid Howells 	default:
107be6e6707SDavid Howells 		BUG();
108be6e6707SDavid Howells 	}
109be6e6707SDavid Howells }
110be6e6707SDavid Howells 
111be6e6707SDavid Howells /*
112be6e6707SDavid Howells  * Look up a remote transport endpoint for the specified address using RCU.
113be6e6707SDavid Howells  */
__rxrpc_lookup_peer_rcu(struct rxrpc_local * local,const struct sockaddr_rxrpc * srx,unsigned long hash_key)114be6e6707SDavid Howells static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
115be6e6707SDavid Howells 	struct rxrpc_local *local,
116be6e6707SDavid Howells 	const struct sockaddr_rxrpc *srx,
117be6e6707SDavid Howells 	unsigned long hash_key)
118be6e6707SDavid Howells {
119be6e6707SDavid Howells 	struct rxrpc_peer *peer;
1202baec2c3SDavid Howells 	struct rxrpc_net *rxnet = local->rxnet;
121be6e6707SDavid Howells 
1222baec2c3SDavid Howells 	hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) {
1230099dc58SDavid Howells 		if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 &&
124a0575429SDavid Howells 		    refcount_read(&peer->ref) > 0)
125be6e6707SDavid Howells 			return peer;
126be6e6707SDavid Howells 	}
127be6e6707SDavid Howells 
128be6e6707SDavid Howells 	return NULL;
129be6e6707SDavid Howells }
130be6e6707SDavid Howells 
131be6e6707SDavid Howells /*
132be6e6707SDavid Howells  * Look up a remote transport endpoint for the specified address using RCU.
133be6e6707SDavid Howells  */
rxrpc_lookup_peer_rcu(struct rxrpc_local * local,const struct sockaddr_rxrpc * srx)134be6e6707SDavid Howells struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
135be6e6707SDavid Howells 					 const struct sockaddr_rxrpc *srx)
136be6e6707SDavid Howells {
137be6e6707SDavid Howells 	struct rxrpc_peer *peer;
138be6e6707SDavid Howells 	unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
139be6e6707SDavid Howells 
140be6e6707SDavid Howells 	peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
141e969c92cSDavid Howells 	if (peer)
142a0575429SDavid Howells 		_leave(" = %p {u=%d}", peer, refcount_read(&peer->ref));
143be6e6707SDavid Howells 	return peer;
144be6e6707SDavid Howells }
1458c3e34a4SDavid Howells 
1468c3e34a4SDavid Howells /*
1478c3e34a4SDavid Howells  * assess the MTU size for the network interface through which this peer is
1488c3e34a4SDavid Howells  * reached
1498c3e34a4SDavid Howells  */
rxrpc_assess_MTU_size(struct rxrpc_local * local,struct rxrpc_peer * peer)150*8a758d98SDavid Howells static void rxrpc_assess_MTU_size(struct rxrpc_local *local,
1515e33a23bSDavid Howells 				  struct rxrpc_peer *peer)
1528c3e34a4SDavid Howells {
153*8a758d98SDavid Howells 	struct net *net = local->net;
15475b54cb5SDavid Howells 	struct dst_entry *dst;
1558c3e34a4SDavid Howells 	struct rtable *rt;
15675b54cb5SDavid Howells 	struct flowi fl;
15775b54cb5SDavid Howells 	struct flowi4 *fl4 = &fl.u.ip4;
158d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
15975b54cb5SDavid Howells 	struct flowi6 *fl6 = &fl.u.ip6;
160d1912747SDavid Howells #endif
1618c3e34a4SDavid Howells 
1628c3e34a4SDavid Howells 	peer->if_mtu = 1500;
1638c3e34a4SDavid Howells 
16475b54cb5SDavid Howells 	memset(&fl, 0, sizeof(fl));
16575b54cb5SDavid Howells 	switch (peer->srx.transport.family) {
16675b54cb5SDavid Howells 	case AF_INET:
16775b54cb5SDavid Howells 		rt = ip_route_output_ports(
1685e33a23bSDavid Howells 			net, fl4, NULL,
1698c3e34a4SDavid Howells 			peer->srx.transport.sin.sin_addr.s_addr, 0,
17075b54cb5SDavid Howells 			htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
1718c3e34a4SDavid Howells 		if (IS_ERR(rt)) {
1728c3e34a4SDavid Howells 			_leave(" [route err %ld]", PTR_ERR(rt));
1738c3e34a4SDavid Howells 			return;
1748c3e34a4SDavid Howells 		}
17575b54cb5SDavid Howells 		dst = &rt->dst;
17675b54cb5SDavid Howells 		break;
1778c3e34a4SDavid Howells 
178d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
17975b54cb5SDavid Howells 	case AF_INET6:
18075b54cb5SDavid Howells 		fl6->flowi6_iif = LOOPBACK_IFINDEX;
18175b54cb5SDavid Howells 		fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
18275b54cb5SDavid Howells 		fl6->flowi6_proto = IPPROTO_UDP;
18375b54cb5SDavid Howells 		memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
18475b54cb5SDavid Howells 		       sizeof(struct in6_addr));
18575b54cb5SDavid Howells 		fl6->fl6_dport = htons(7001);
18675b54cb5SDavid Howells 		fl6->fl6_sport = htons(7000);
1875e33a23bSDavid Howells 		dst = ip6_route_output(net, NULL, fl6);
18807096f61SDavid Howells 		if (dst->error) {
18907096f61SDavid Howells 			_leave(" [route err %d]", dst->error);
19075b54cb5SDavid Howells 			return;
19175b54cb5SDavid Howells 		}
19275b54cb5SDavid Howells 		break;
193d1912747SDavid Howells #endif
19475b54cb5SDavid Howells 
19575b54cb5SDavid Howells 	default:
19675b54cb5SDavid Howells 		BUG();
19775b54cb5SDavid Howells 	}
19875b54cb5SDavid Howells 
19975b54cb5SDavid Howells 	peer->if_mtu = dst_mtu(dst);
20075b54cb5SDavid Howells 	dst_release(dst);
2018c3e34a4SDavid Howells 
2028c3e34a4SDavid Howells 	_leave(" [if_mtu %u]", peer->if_mtu);
2038c3e34a4SDavid Howells }
2048c3e34a4SDavid Howells 
2058c3e34a4SDavid Howells /*
206be6e6707SDavid Howells  * Allocate a peer.
2078c3e34a4SDavid Howells  */
rxrpc_alloc_peer(struct rxrpc_local * local,gfp_t gfp,enum rxrpc_peer_trace why)20847c810a7SDavid Howells struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp,
20947c810a7SDavid Howells 				    enum rxrpc_peer_trace why)
2108c3e34a4SDavid Howells {
2118c3e34a4SDavid Howells 	struct rxrpc_peer *peer;
2128c3e34a4SDavid Howells 
2138c3e34a4SDavid Howells 	_enter("");
2148c3e34a4SDavid Howells 
2158c3e34a4SDavid Howells 	peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
2168c3e34a4SDavid Howells 	if (peer) {
217a0575429SDavid Howells 		refcount_set(&peer->ref, 1);
2180fde882fSDavid Howells 		peer->local = rxrpc_get_local(local, rxrpc_local_get_peer);
219f66d7490SDavid Howells 		INIT_HLIST_HEAD(&peer->error_targets);
220aa390bbeSDavid Howells 		peer->service_conns = RB_ROOT;
2218496af50SDavid Howells 		seqlock_init(&peer->service_conn_lock);
2228c3e34a4SDavid Howells 		spin_lock_init(&peer->lock);
223c1e15b49SDavid Howells 		spin_lock_init(&peer->rtt_input_lock);
2248c3e34a4SDavid Howells 		peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
225f7aec129SDavid Howells 
226c410bf01SDavid Howells 		rxrpc_peer_init_rtt(peer);
227c410bf01SDavid Howells 
2281fc4fa2aSDavid Howells 		peer->cong_ssthresh = RXRPC_TX_MAX_WINDOW;
229c838f1a7SDavid Howells 		trace_rxrpc_peer(peer->debug_id, 1, why);
230be6e6707SDavid Howells 	}
231be6e6707SDavid Howells 
232be6e6707SDavid Howells 	_leave(" = %p", peer);
233be6e6707SDavid Howells 	return peer;
234be6e6707SDavid Howells }
235be6e6707SDavid Howells 
236be6e6707SDavid Howells /*
237248f219cSDavid Howells  * Initialise peer record.
238be6e6707SDavid Howells  */
rxrpc_init_peer(struct rxrpc_local * local,struct rxrpc_peer * peer,unsigned long hash_key)239*8a758d98SDavid Howells static void rxrpc_init_peer(struct rxrpc_local *local, struct rxrpc_peer *peer,
2405e33a23bSDavid Howells 			    unsigned long hash_key)
241be6e6707SDavid Howells {
24208a39685SDavid Howells 	peer->hash_key = hash_key;
243*8a758d98SDavid Howells 	rxrpc_assess_MTU_size(local, peer);
2448c3e34a4SDavid Howells 	peer->mtu = peer->if_mtu;
2450d4b103cSDavid Howells 	peer->rtt_last_req = ktime_get_real();
2468c3e34a4SDavid Howells 
24775b54cb5SDavid Howells 	switch (peer->srx.transport.family) {
24875b54cb5SDavid Howells 	case AF_INET:
2498c3e34a4SDavid Howells 		peer->hdrsize = sizeof(struct iphdr);
25075b54cb5SDavid Howells 		break;
251d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
25275b54cb5SDavid Howells 	case AF_INET6:
25375b54cb5SDavid Howells 		peer->hdrsize = sizeof(struct ipv6hdr);
25475b54cb5SDavid Howells 		break;
255d1912747SDavid Howells #endif
25675b54cb5SDavid Howells 	default:
25775b54cb5SDavid Howells 		BUG();
25875b54cb5SDavid Howells 	}
25975b54cb5SDavid Howells 
260248f219cSDavid Howells 	switch (peer->srx.transport_type) {
2618c3e34a4SDavid Howells 	case SOCK_DGRAM:
2628c3e34a4SDavid Howells 		peer->hdrsize += sizeof(struct udphdr);
2638c3e34a4SDavid Howells 		break;
2648c3e34a4SDavid Howells 	default:
2658c3e34a4SDavid Howells 		BUG();
2668c3e34a4SDavid Howells 	}
2678c3e34a4SDavid Howells 
2688c3e34a4SDavid Howells 	peer->hdrsize += sizeof(struct rxrpc_wire_header);
2698c3e34a4SDavid Howells 	peer->maxdata = peer->mtu - peer->hdrsize;
2708c3e34a4SDavid Howells }
2718c3e34a4SDavid Howells 
272248f219cSDavid Howells /*
273248f219cSDavid Howells  * Set up a new peer.
274248f219cSDavid Howells  */
rxrpc_create_peer(struct rxrpc_local * local,struct sockaddr_rxrpc * srx,unsigned long hash_key,gfp_t gfp)275*8a758d98SDavid Howells static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
276248f219cSDavid Howells 					    struct sockaddr_rxrpc *srx,
277248f219cSDavid Howells 					    unsigned long hash_key,
278248f219cSDavid Howells 					    gfp_t gfp)
279248f219cSDavid Howells {
280248f219cSDavid Howells 	struct rxrpc_peer *peer;
281248f219cSDavid Howells 
282248f219cSDavid Howells 	_enter("");
283248f219cSDavid Howells 
28447c810a7SDavid Howells 	peer = rxrpc_alloc_peer(local, gfp, rxrpc_peer_new_client);
285248f219cSDavid Howells 	if (peer) {
286248f219cSDavid Howells 		memcpy(&peer->srx, srx, sizeof(*srx));
287*8a758d98SDavid Howells 		rxrpc_init_peer(local, peer, hash_key);
288248f219cSDavid Howells 	}
289248f219cSDavid Howells 
2908c3e34a4SDavid Howells 	_leave(" = %p", peer);
2918c3e34a4SDavid Howells 	return peer;
2928c3e34a4SDavid Howells }
2938c3e34a4SDavid Howells 
rxrpc_free_peer(struct rxrpc_peer * peer)294beacff50SEiichi Tsukata static void rxrpc_free_peer(struct rxrpc_peer *peer)
295beacff50SEiichi Tsukata {
29647c810a7SDavid Howells 	trace_rxrpc_peer(peer->debug_id, 0, rxrpc_peer_free);
2970fde882fSDavid Howells 	rxrpc_put_local(peer->local, rxrpc_local_put_peer);
298beacff50SEiichi Tsukata 	kfree_rcu(peer, rcu);
299beacff50SEiichi Tsukata }
300beacff50SEiichi Tsukata 
3018c3e34a4SDavid Howells /*
3020099dc58SDavid Howells  * Set up a new incoming peer.  There shouldn't be any other matching peers
3030099dc58SDavid Howells  * since we've already done a search in the list from the non-reentrant context
3040099dc58SDavid Howells  * (the data_ready handler) that is the only place we can add new peers.
305248f219cSDavid Howells  */
rxrpc_new_incoming_peer(struct rxrpc_local * local,struct rxrpc_peer * peer)306*8a758d98SDavid Howells void rxrpc_new_incoming_peer(struct rxrpc_local *local, struct rxrpc_peer *peer)
307248f219cSDavid Howells {
3082baec2c3SDavid Howells 	struct rxrpc_net *rxnet = local->rxnet;
309248f219cSDavid Howells 	unsigned long hash_key;
310248f219cSDavid Howells 
3110099dc58SDavid Howells 	hash_key = rxrpc_peer_hash_key(local, &peer->srx);
312*8a758d98SDavid Howells 	rxrpc_init_peer(local, peer, hash_key);
313248f219cSDavid Howells 
3142baec2c3SDavid Howells 	spin_lock(&rxnet->peer_hash_lock);
3152baec2c3SDavid Howells 	hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key);
316330bdcfaSDavid Howells 	list_add_tail(&peer->keepalive_link, &rxnet->peer_keepalive_new);
3172baec2c3SDavid Howells 	spin_unlock(&rxnet->peer_hash_lock);
318248f219cSDavid Howells }
319248f219cSDavid Howells 
320248f219cSDavid Howells /*
3218c3e34a4SDavid Howells  * obtain a remote transport endpoint for the specified address
3228c3e34a4SDavid Howells  */
rxrpc_lookup_peer(struct rxrpc_local * local,struct sockaddr_rxrpc * srx,gfp_t gfp)323*8a758d98SDavid Howells struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
324be6e6707SDavid Howells 				     struct sockaddr_rxrpc *srx, gfp_t gfp)
3258c3e34a4SDavid Howells {
3268c3e34a4SDavid Howells 	struct rxrpc_peer *peer, *candidate;
3272baec2c3SDavid Howells 	struct rxrpc_net *rxnet = local->rxnet;
328be6e6707SDavid Howells 	unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
3298c3e34a4SDavid Howells 
33075b54cb5SDavid Howells 	_enter("{%pISp}", &srx->transport);
3318c3e34a4SDavid Howells 
3328c3e34a4SDavid Howells 	/* search the peer list first */
333be6e6707SDavid Howells 	rcu_read_lock();
334be6e6707SDavid Howells 	peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
33547c810a7SDavid Howells 	if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client))
336be6e6707SDavid Howells 		peer = NULL;
337be6e6707SDavid Howells 	rcu_read_unlock();
3388c3e34a4SDavid Howells 
339be6e6707SDavid Howells 	if (!peer) {
340be6e6707SDavid Howells 		/* The peer is not yet present in hash - create a candidate
341be6e6707SDavid Howells 		 * for a new record and then redo the search.
342be6e6707SDavid Howells 		 */
343*8a758d98SDavid Howells 		candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
3448c3e34a4SDavid Howells 		if (!candidate) {
345be6e6707SDavid Howells 			_leave(" = NULL [nomem]");
346be6e6707SDavid Howells 			return NULL;
3478c3e34a4SDavid Howells 		}
3488c3e34a4SDavid Howells 
3493dd9c8b5SDavid Howells 		spin_lock(&rxnet->peer_hash_lock);
3508c3e34a4SDavid Howells 
351be6e6707SDavid Howells 		/* Need to check that we aren't racing with someone else */
352be6e6707SDavid Howells 		peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
35347c810a7SDavid Howells 		if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client))
354be6e6707SDavid Howells 			peer = NULL;
355ace45becSDavid Howells 		if (!peer) {
3562baec2c3SDavid Howells 			hash_add_rcu(rxnet->peer_hash,
357be6e6707SDavid Howells 				     &candidate->hash_link, hash_key);
358330bdcfaSDavid Howells 			list_add_tail(&candidate->keepalive_link,
359ace45becSDavid Howells 				      &rxnet->peer_keepalive_new);
360ace45becSDavid Howells 		}
3618c3e34a4SDavid Howells 
3623dd9c8b5SDavid Howells 		spin_unlock(&rxnet->peer_hash_lock);
363be6e6707SDavid Howells 
364be6e6707SDavid Howells 		if (peer)
365beacff50SEiichi Tsukata 			rxrpc_free_peer(candidate);
366be6e6707SDavid Howells 		else
3678c3e34a4SDavid Howells 			peer = candidate;
368be6e6707SDavid Howells 	}
3698c3e34a4SDavid Howells 
370a0575429SDavid Howells 	_leave(" = %p {u=%d}", peer, refcount_read(&peer->ref));
3718c3e34a4SDavid Howells 	return peer;
3728c3e34a4SDavid Howells }
3738c3e34a4SDavid Howells 
3748c3e34a4SDavid Howells /*
3751159d4b4SDavid Howells  * Get a ref on a peer record.
3768c3e34a4SDavid Howells  */
rxrpc_get_peer(struct rxrpc_peer * peer,enum rxrpc_peer_trace why)37747c810a7SDavid Howells struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why)
3781159d4b4SDavid Howells {
379a0575429SDavid Howells 	int r;
3801159d4b4SDavid Howells 
381a0575429SDavid Howells 	__refcount_inc(&peer->ref, &r);
382c838f1a7SDavid Howells 	trace_rxrpc_peer(peer->debug_id, r + 1, why);
3831159d4b4SDavid Howells 	return peer;
3841159d4b4SDavid Howells }
3851159d4b4SDavid Howells 
3861159d4b4SDavid Howells /*
3871159d4b4SDavid Howells  * Get a ref on a peer record unless its usage has already reached 0.
3881159d4b4SDavid Howells  */
rxrpc_get_peer_maybe(struct rxrpc_peer * peer,enum rxrpc_peer_trace why)38947c810a7SDavid Howells struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer,
39047c810a7SDavid Howells 					enum rxrpc_peer_trace why)
3911159d4b4SDavid Howells {
392a0575429SDavid Howells 	int r;
3931159d4b4SDavid Howells 
3941159d4b4SDavid Howells 	if (peer) {
395a0575429SDavid Howells 		if (__refcount_inc_not_zero(&peer->ref, &r))
39647c810a7SDavid Howells 			trace_rxrpc_peer(peer->debug_id, r + 1, why);
3971159d4b4SDavid Howells 		else
3981159d4b4SDavid Howells 			peer = NULL;
3991159d4b4SDavid Howells 	}
4001159d4b4SDavid Howells 	return peer;
4011159d4b4SDavid Howells }
4021159d4b4SDavid Howells 
4031159d4b4SDavid Howells /*
4041159d4b4SDavid Howells  * Discard a peer record.
4051159d4b4SDavid Howells  */
__rxrpc_put_peer(struct rxrpc_peer * peer)4061159d4b4SDavid Howells static void __rxrpc_put_peer(struct rxrpc_peer *peer)
4078c3e34a4SDavid Howells {
4082baec2c3SDavid Howells 	struct rxrpc_net *rxnet = peer->local->rxnet;
4092baec2c3SDavid Howells 
410f66d7490SDavid Howells 	ASSERT(hlist_empty(&peer->error_targets));
4118c3e34a4SDavid Howells 
4123dd9c8b5SDavid Howells 	spin_lock(&rxnet->peer_hash_lock);
413be6e6707SDavid Howells 	hash_del_rcu(&peer->hash_link);
414330bdcfaSDavid Howells 	list_del_init(&peer->keepalive_link);
4153dd9c8b5SDavid Howells 	spin_unlock(&rxnet->peer_hash_lock);
4168c3e34a4SDavid Howells 
417beacff50SEiichi Tsukata 	rxrpc_free_peer(peer);
4188c3e34a4SDavid Howells }
4198324f0bcSDavid Howells 
4201159d4b4SDavid Howells /*
4211159d4b4SDavid Howells  * Drop a ref on a peer record.
4221159d4b4SDavid Howells  */
rxrpc_put_peer(struct rxrpc_peer * peer,enum rxrpc_peer_trace why)42347c810a7SDavid Howells void rxrpc_put_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why)
4241159d4b4SDavid Howells {
42555f6c98eSDavid Howells 	unsigned int debug_id;
426a0575429SDavid Howells 	bool dead;
427a0575429SDavid Howells 	int r;
4281159d4b4SDavid Howells 
4291159d4b4SDavid Howells 	if (peer) {
43055f6c98eSDavid Howells 		debug_id = peer->debug_id;
431a0575429SDavid Howells 		dead = __refcount_dec_and_test(&peer->ref, &r);
43247c810a7SDavid Howells 		trace_rxrpc_peer(debug_id, r - 1, why);
433a0575429SDavid Howells 		if (dead)
4341159d4b4SDavid Howells 			__rxrpc_put_peer(peer);
4351159d4b4SDavid Howells 	}
4361159d4b4SDavid Howells }
4371159d4b4SDavid Howells 
43817226f12SDavid Howells /*
43917226f12SDavid Howells  * Make sure all peer records have been discarded.
44017226f12SDavid Howells  */
rxrpc_destroy_all_peers(struct rxrpc_net * rxnet)44117226f12SDavid Howells void rxrpc_destroy_all_peers(struct rxrpc_net *rxnet)
44217226f12SDavid Howells {
44317226f12SDavid Howells 	struct rxrpc_peer *peer;
44417226f12SDavid Howells 	int i;
44517226f12SDavid Howells 
44617226f12SDavid Howells 	for (i = 0; i < HASH_SIZE(rxnet->peer_hash); i++) {
44717226f12SDavid Howells 		if (hlist_empty(&rxnet->peer_hash[i]))
44817226f12SDavid Howells 			continue;
44917226f12SDavid Howells 
45017226f12SDavid Howells 		hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) {
45117226f12SDavid Howells 			pr_err("Leaked peer %u {%u} %pISp\n",
45217226f12SDavid Howells 			       peer->debug_id,
453a0575429SDavid Howells 			       refcount_read(&peer->ref),
45417226f12SDavid Howells 			       &peer->srx.transport);
45517226f12SDavid Howells 		}
45617226f12SDavid Howells 	}
45717226f12SDavid Howells }
45817226f12SDavid Howells 
4598324f0bcSDavid Howells /**
4608324f0bcSDavid Howells  * rxrpc_kernel_get_peer - Get the peer address of a call
4618324f0bcSDavid Howells  * @sock: The socket on which the call is in progress.
4628324f0bcSDavid Howells  * @call: The call to query
4638324f0bcSDavid Howells  * @_srx: Where to place the result
4648324f0bcSDavid Howells  *
4658324f0bcSDavid Howells  * Get the address of the remote peer in a call.
4668324f0bcSDavid Howells  */
rxrpc_kernel_get_peer(struct socket * sock,struct rxrpc_call * call,struct sockaddr_rxrpc * _srx)4678324f0bcSDavid Howells void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
4688324f0bcSDavid Howells 			   struct sockaddr_rxrpc *_srx)
4698324f0bcSDavid Howells {
4708324f0bcSDavid Howells 	*_srx = call->peer->srx;
4718324f0bcSDavid Howells }
4728324f0bcSDavid Howells EXPORT_SYMBOL(rxrpc_kernel_get_peer);
473f4d15fb6SDavid Howells 
474f4d15fb6SDavid Howells /**
475c410bf01SDavid Howells  * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT
476f4d15fb6SDavid Howells  * @sock: The socket on which the call is in progress.
477f4d15fb6SDavid Howells  * @call: The call to query
4781d4adfafSDavid Howells  * @_srtt: Where to store the SRTT value.
479f4d15fb6SDavid Howells  *
4801d4adfafSDavid Howells  * Get the call's peer smoothed RTT in uS.
481f4d15fb6SDavid Howells  */
rxrpc_kernel_get_srtt(struct socket * sock,struct rxrpc_call * call,u32 * _srtt)4821d4adfafSDavid Howells bool rxrpc_kernel_get_srtt(struct socket *sock, struct rxrpc_call *call,
4831d4adfafSDavid Howells 			   u32 *_srtt)
484f4d15fb6SDavid Howells {
4851d4adfafSDavid Howells 	struct rxrpc_peer *peer = call->peer;
4861d4adfafSDavid Howells 
4871d4adfafSDavid Howells 	if (peer->rtt_count == 0) {
4881d4adfafSDavid Howells 		*_srtt = 1000000; /* 1S */
4891d4adfafSDavid Howells 		return false;
4901d4adfafSDavid Howells 	}
4911d4adfafSDavid Howells 
4921d4adfafSDavid Howells 	*_srtt = call->peer->srtt_us >> 3;
4931d4adfafSDavid Howells 	return true;
494f4d15fb6SDavid Howells }
495c410bf01SDavid Howells EXPORT_SYMBOL(rxrpc_kernel_get_srtt);
496