Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* RxRPC remote transport endpoint record management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/hashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <net/af_rxrpc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "ar-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Hash a peer key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 					 const struct sockaddr_rxrpc *srx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	const u16 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	unsigned int i, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned long hash_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	_enter("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	hash_key = (unsigned long)local / __alignof__(*local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	hash_key += srx->transport_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	hash_key += srx->transport_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	hash_key += srx->transport.family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	switch (srx->transport.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		hash_key += (u16 __force)srx->transport.sin.sin_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		size = sizeof(srx->transport.sin.sin_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		p = (u16 *)&srx->transport.sin.sin_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #ifdef CONFIG_AF_RXRPC_IPV6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		hash_key += (u16 __force)srx->transport.sin.sin_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		size = sizeof(srx->transport.sin6.sin6_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		p = (u16 *)&srx->transport.sin6.sin6_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		WARN(1, "AF_RXRPC: Unsupported transport address family\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/* Step through the peer address in 16-bit portions for speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	for (i = 0; i < size; i += sizeof(*p), p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		hash_key += *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	_leave(" 0x%lx", hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return hash_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * Compare a peer to a key.  Return -ve, 0 or +ve to indicate less than, same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * or greater than.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * buckets and mid-bucket insertion, so we don't make full use of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * information at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			       struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			       const struct sockaddr_rxrpc *srx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			       unsigned long hash_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	long diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	diff = ((peer->hash_key - hash_key) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		((unsigned long)peer->local - (unsigned long)local) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		(peer->srx.transport_type - srx->transport_type) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		(peer->srx.transport_len - srx->transport_len) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		(peer->srx.transport.family - srx->transport.family));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (diff != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	switch (srx->transport.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return ((u16 __force)peer->srx.transport.sin.sin_port -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			(u16 __force)srx->transport.sin.sin_port) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			memcmp(&peer->srx.transport.sin.sin_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			       &srx->transport.sin.sin_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			       sizeof(struct in_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) #ifdef CONFIG_AF_RXRPC_IPV6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return ((u16 __force)peer->srx.transport.sin6.sin6_port -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			(u16 __force)srx->transport.sin6.sin6_port) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			memcmp(&peer->srx.transport.sin6.sin6_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			       &srx->transport.sin6.sin6_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			       sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * Look up a remote transport endpoint for the specified address using RCU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	const struct sockaddr_rxrpc *srx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	unsigned long hash_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct rxrpc_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct rxrpc_net *rxnet = local->rxnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		    atomic_read(&peer->usage) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * Look up a remote transport endpoint for the specified address using RCU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 					 const struct sockaddr_rxrpc *srx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct rxrpc_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		_net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		_leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * assess the MTU size for the network interface through which this peer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static void rxrpc_assess_MTU_size(struct rxrpc_sock *rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				  struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct net *net = sock_net(&rx->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct dst_entry *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct flowi fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct flowi4 *fl4 = &fl.u.ip4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #ifdef CONFIG_AF_RXRPC_IPV6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct flowi6 *fl6 = &fl.u.ip6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	peer->if_mtu = 1500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	memset(&fl, 0, sizeof(fl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	switch (peer->srx.transport.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		rt = ip_route_output_ports(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			net, fl4, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			peer->srx.transport.sin.sin_addr.s_addr, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		if (IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			_leave(" [route err %ld]", PTR_ERR(rt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		dst = &rt->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #ifdef CONFIG_AF_RXRPC_IPV6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		fl6->flowi6_iif = LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		fl6->flowi6_proto = IPPROTO_UDP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		       sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		fl6->fl6_dport = htons(7001);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		fl6->fl6_sport = htons(7000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		dst = ip6_route_output(net, NULL, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (dst->error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			_leave(" [route err %d]", dst->error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	peer->if_mtu = dst_mtu(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	dst_release(dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	_leave(" [if_mtu %u]", peer->if_mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * Allocate a peer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	const void *here = __builtin_return_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct rxrpc_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	_enter("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		atomic_set(&peer->usage, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		peer->local = rxrpc_get_local(local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		INIT_HLIST_HEAD(&peer->error_targets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		peer->service_conns = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		seqlock_init(&peer->service_conn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		spin_lock_init(&peer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		spin_lock_init(&peer->rtt_input_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		rxrpc_peer_init_rtt(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		if (RXRPC_TX_SMSS > 2190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			peer->cong_cwnd = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		else if (RXRPC_TX_SMSS > 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			peer->cong_cwnd = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			peer->cong_cwnd = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		trace_rxrpc_peer(peer->debug_id, rxrpc_peer_new, 1, here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	_leave(" = %p", peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  * Initialise peer record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static void rxrpc_init_peer(struct rxrpc_sock *rx, struct rxrpc_peer *peer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			    unsigned long hash_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	peer->hash_key = hash_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	rxrpc_assess_MTU_size(rx, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	peer->mtu = peer->if_mtu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	peer->rtt_last_req = ktime_get_real();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	switch (peer->srx.transport.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		peer->hdrsize = sizeof(struct iphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) #ifdef CONFIG_AF_RXRPC_IPV6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		peer->hdrsize = sizeof(struct ipv6hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	switch (peer->srx.transport_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	case SOCK_DGRAM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		peer->hdrsize += sizeof(struct udphdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	peer->hdrsize += sizeof(struct rxrpc_wire_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	peer->maxdata = peer->mtu - peer->hdrsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * Set up a new peer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_sock *rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 					    struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 					    struct sockaddr_rxrpc *srx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 					    unsigned long hash_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 					    gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct rxrpc_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	_enter("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	peer = rxrpc_alloc_peer(local, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		memcpy(&peer->srx, srx, sizeof(*srx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		rxrpc_init_peer(rx, peer, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	_leave(" = %p", peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void rxrpc_free_peer(struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	rxrpc_put_local(peer->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	kfree_rcu(peer, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * Set up a new incoming peer.  There shouldn't be any other matching peers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * since we've already done a search in the list from the non-reentrant context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  * (the data_ready handler) that is the only place we can add new peers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) void rxrpc_new_incoming_peer(struct rxrpc_sock *rx, struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			     struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct rxrpc_net *rxnet = local->rxnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	unsigned long hash_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	hash_key = rxrpc_peer_hash_key(local, &peer->srx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	rxrpc_init_peer(rx, peer, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	spin_lock(&rxnet->peer_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	list_add_tail(&peer->keepalive_link, &rxnet->peer_keepalive_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	spin_unlock(&rxnet->peer_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)  * obtain a remote transport endpoint for the specified address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_sock *rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				     struct rxrpc_local *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				     struct sockaddr_rxrpc *srx, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct rxrpc_peer *peer, *candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	struct rxrpc_net *rxnet = local->rxnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	_enter("{%pISp}", &srx->transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/* search the peer list first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (peer && !rxrpc_get_peer_maybe(peer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		peer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (!peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		/* The peer is not yet present in hash - create a candidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		 * for a new record and then redo the search.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		candidate = rxrpc_create_peer(rx, local, srx, hash_key, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		if (!candidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			_leave(" = NULL [nomem]");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		spin_lock_bh(&rxnet->peer_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		/* Need to check that we aren't racing with someone else */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		if (peer && !rxrpc_get_peer_maybe(peer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			peer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		if (!peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			hash_add_rcu(rxnet->peer_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 				     &candidate->hash_link, hash_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			list_add_tail(&candidate->keepalive_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				      &rxnet->peer_keepalive_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		spin_unlock_bh(&rxnet->peer_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			rxrpc_free_peer(candidate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			peer = candidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	_net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	_leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * Get a ref on a peer record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	const void *here = __builtin_return_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	n = atomic_inc_return(&peer->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, n, here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  * Get a ref on a peer record unless its usage has already reached 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	const void *here = __builtin_return_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		int n = atomic_fetch_add_unless(&peer->usage, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		if (n > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, n + 1, here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			peer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * Discard a peer record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static void __rxrpc_put_peer(struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	struct rxrpc_net *rxnet = peer->local->rxnet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	ASSERT(hlist_empty(&peer->error_targets));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	spin_lock_bh(&rxnet->peer_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	hash_del_rcu(&peer->hash_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	list_del_init(&peer->keepalive_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	spin_unlock_bh(&rxnet->peer_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	rxrpc_free_peer(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * Drop a ref on a peer record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) void rxrpc_put_peer(struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	const void *here = __builtin_return_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	unsigned int debug_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		debug_id = peer->debug_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		n = atomic_dec_return(&peer->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		trace_rxrpc_peer(debug_id, rxrpc_peer_put, n, here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			__rxrpc_put_peer(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * Drop a ref on a peer record where the caller already holds the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  * peer_hash_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) void rxrpc_put_peer_locked(struct rxrpc_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	const void *here = __builtin_return_address(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	unsigned int debug_id = peer->debug_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	n = atomic_dec_return(&peer->usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	trace_rxrpc_peer(debug_id, rxrpc_peer_put, n, here);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (n == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		hash_del_rcu(&peer->hash_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		list_del_init(&peer->keepalive_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		rxrpc_free_peer(peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)  * Make sure all peer records have been discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) void rxrpc_destroy_all_peers(struct rxrpc_net *rxnet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	struct rxrpc_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	for (i = 0; i < HASH_SIZE(rxnet->peer_hash); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		if (hlist_empty(&rxnet->peer_hash[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			pr_err("Leaked peer %u {%u} %pISp\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			       peer->debug_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			       atomic_read(&peer->usage),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			       &peer->srx.transport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * rxrpc_kernel_get_peer - Get the peer address of a call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * @sock: The socket on which the call is in progress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * @call: The call to query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * @_srx: Where to place the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  * Get the address of the remote peer in a call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			   struct sockaddr_rxrpc *_srx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	*_srx = call->peer->srx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) EXPORT_SYMBOL(rxrpc_kernel_get_peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)  * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)  * @sock: The socket on which the call is in progress.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)  * @call: The call to query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)  * @_srtt: Where to store the SRTT value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  * Get the call's peer smoothed RTT in uS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) bool rxrpc_kernel_get_srtt(struct socket *sock, struct rxrpc_call *call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			   u32 *_srtt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct rxrpc_peer *peer = call->peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (peer->rtt_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		*_srtt = 1000000; /* 1S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	*_srtt = call->peer->srtt_us >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) EXPORT_SYMBOL(rxrpc_kernel_get_srtt);