18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* RxRPC remote transport endpoint record management 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. 58c2ecf20Sopenharmony_ci * Written by David Howells (dhowells@redhat.com) 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/net.h> 128c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 138c2ecf20Sopenharmony_ci#include <linux/udp.h> 148c2ecf20Sopenharmony_ci#include <linux/in.h> 158c2ecf20Sopenharmony_ci#include <linux/in6.h> 168c2ecf20Sopenharmony_ci#include <linux/slab.h> 178c2ecf20Sopenharmony_ci#include <linux/hashtable.h> 188c2ecf20Sopenharmony_ci#include <net/sock.h> 198c2ecf20Sopenharmony_ci#include <net/af_rxrpc.h> 208c2ecf20Sopenharmony_ci#include <net/ip.h> 218c2ecf20Sopenharmony_ci#include <net/route.h> 228c2ecf20Sopenharmony_ci#include <net/ip6_route.h> 238c2ecf20Sopenharmony_ci#include "ar-internal.h" 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* 268c2ecf20Sopenharmony_ci * Hash a peer key. 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_cistatic unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local, 298c2ecf20Sopenharmony_ci const struct sockaddr_rxrpc *srx) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci const u16 *p; 328c2ecf20Sopenharmony_ci unsigned int i, size; 338c2ecf20Sopenharmony_ci unsigned long hash_key; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci _enter(""); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci hash_key = (unsigned long)local / __alignof__(*local); 388c2ecf20Sopenharmony_ci hash_key += srx->transport_type; 398c2ecf20Sopenharmony_ci hash_key += srx->transport_len; 408c2ecf20Sopenharmony_ci hash_key += srx->transport.family; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci switch (srx->transport.family) { 438c2ecf20Sopenharmony_ci case AF_INET: 448c2ecf20Sopenharmony_ci hash_key += (u16 __force)srx->transport.sin.sin_port; 458c2ecf20Sopenharmony_ci size = sizeof(srx->transport.sin.sin_addr); 468c2ecf20Sopenharmony_ci p = (u16 *)&srx->transport.sin.sin_addr; 478c2ecf20Sopenharmony_ci break; 488c2ecf20Sopenharmony_ci#ifdef CONFIG_AF_RXRPC_IPV6 498c2ecf20Sopenharmony_ci case AF_INET6: 508c2ecf20Sopenharmony_ci hash_key += (u16 __force)srx->transport.sin.sin_port; 518c2ecf20Sopenharmony_ci size = sizeof(srx->transport.sin6.sin6_addr); 528c2ecf20Sopenharmony_ci p = (u16 *)&srx->transport.sin6.sin6_addr; 538c2ecf20Sopenharmony_ci break; 548c2ecf20Sopenharmony_ci#endif 558c2ecf20Sopenharmony_ci default: 568c2ecf20Sopenharmony_ci WARN(1, "AF_RXRPC: Unsupported transport address family\n"); 578c2ecf20Sopenharmony_ci return 0; 588c2ecf20Sopenharmony_ci } 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci /* Step through the peer address in 16-bit portions for speed */ 618c2ecf20Sopenharmony_ci for (i = 0; i < size; i += sizeof(*p), p++) 628c2ecf20Sopenharmony_ci hash_key += *p; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci _leave(" 0x%lx", hash_key); 658c2ecf20Sopenharmony_ci return hash_key; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/* 698c2ecf20Sopenharmony_ci * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same 708c2ecf20Sopenharmony_ci * or greater than. 718c2ecf20Sopenharmony_ci * 728c2ecf20Sopenharmony_ci * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted 738c2ecf20Sopenharmony_ci * buckets and mid-bucket insertion, so we don't make full use of this 748c2ecf20Sopenharmony_ci * information at this point. 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_cistatic long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer, 778c2ecf20Sopenharmony_ci struct rxrpc_local *local, 788c2ecf20Sopenharmony_ci const struct sockaddr_rxrpc *srx, 798c2ecf20Sopenharmony_ci unsigned long hash_key) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci long diff; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci diff = ((peer->hash_key - hash_key) ?: 848c2ecf20Sopenharmony_ci ((unsigned long)peer->local - (unsigned long)local) ?: 858c2ecf20Sopenharmony_ci (peer->srx.transport_type - srx->transport_type) ?: 868c2ecf20Sopenharmony_ci (peer->srx.transport_len - srx->transport_len) ?: 878c2ecf20Sopenharmony_ci (peer->srx.transport.family - srx->transport.family)); 888c2ecf20Sopenharmony_ci if (diff != 0) 898c2ecf20Sopenharmony_ci return diff; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci switch (srx->transport.family) { 928c2ecf20Sopenharmony_ci case AF_INET: 938c2ecf20Sopenharmony_ci return ((u16 __force)peer->srx.transport.sin.sin_port - 948c2ecf20Sopenharmony_ci (u16 __force)srx->transport.sin.sin_port) ?: 958c2ecf20Sopenharmony_ci memcmp(&peer->srx.transport.sin.sin_addr, 968c2ecf20Sopenharmony_ci &srx->transport.sin.sin_addr, 978c2ecf20Sopenharmony_ci sizeof(struct in_addr)); 988c2ecf20Sopenharmony_ci#ifdef CONFIG_AF_RXRPC_IPV6 998c2ecf20Sopenharmony_ci case AF_INET6: 1008c2ecf20Sopenharmony_ci return ((u16 __force)peer->srx.transport.sin6.sin6_port - 1018c2ecf20Sopenharmony_ci (u16 __force)srx->transport.sin6.sin6_port) ?: 1028c2ecf20Sopenharmony_ci memcmp(&peer->srx.transport.sin6.sin6_addr, 1038c2ecf20Sopenharmony_ci &srx->transport.sin6.sin6_addr, 1048c2ecf20Sopenharmony_ci sizeof(struct in6_addr)); 1058c2ecf20Sopenharmony_ci#endif 1068c2ecf20Sopenharmony_ci default: 1078c2ecf20Sopenharmony_ci BUG(); 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci/* 1128c2ecf20Sopenharmony_ci * Look up a remote transport endpoint for the specified address using RCU. 1138c2ecf20Sopenharmony_ci */ 1148c2ecf20Sopenharmony_cistatic struct rxrpc_peer *__rxrpc_lookup_peer_rcu( 1158c2ecf20Sopenharmony_ci struct rxrpc_local *local, 1168c2ecf20Sopenharmony_ci const struct sockaddr_rxrpc *srx, 1178c2ecf20Sopenharmony_ci unsigned long hash_key) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct rxrpc_peer *peer; 1208c2ecf20Sopenharmony_ci struct rxrpc_net *rxnet = local->rxnet; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) { 1238c2ecf20Sopenharmony_ci if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 && 1248c2ecf20Sopenharmony_ci refcount_read(&peer->ref) > 0) 1258c2ecf20Sopenharmony_ci return peer; 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci return NULL; 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/* 1328c2ecf20Sopenharmony_ci * Look up a remote transport endpoint for the specified address using RCU. 1338c2ecf20Sopenharmony_ci */ 1348c2ecf20Sopenharmony_cistruct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local, 1358c2ecf20Sopenharmony_ci const struct sockaddr_rxrpc *srx) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct rxrpc_peer *peer; 1388c2ecf20Sopenharmony_ci unsigned long hash_key = rxrpc_peer_hash_key(local, srx); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key); 1418c2ecf20Sopenharmony_ci if (peer) { 1428c2ecf20Sopenharmony_ci _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport); 1438c2ecf20Sopenharmony_ci _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref)); 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci return peer; 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/* 1498c2ecf20Sopenharmony_ci * assess the MTU size for the network interface through which this peer is 1508c2ecf20Sopenharmony_ci * reached 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_cistatic void rxrpc_assess_MTU_size(struct rxrpc_sock *rx, 1538c2ecf20Sopenharmony_ci struct rxrpc_peer *peer) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct net *net = sock_net(&rx->sk); 1568c2ecf20Sopenharmony_ci struct dst_entry *dst; 1578c2ecf20Sopenharmony_ci struct rtable *rt; 1588c2ecf20Sopenharmony_ci struct flowi fl; 1598c2ecf20Sopenharmony_ci struct flowi4 *fl4 = &fl.u.ip4; 1608c2ecf20Sopenharmony_ci#ifdef CONFIG_AF_RXRPC_IPV6 1618c2ecf20Sopenharmony_ci struct flowi6 *fl6 = &fl.u.ip6; 1628c2ecf20Sopenharmony_ci#endif 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci peer->if_mtu = 1500; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci memset(&fl, 0, sizeof(fl)); 1678c2ecf20Sopenharmony_ci switch (peer->srx.transport.family) { 1688c2ecf20Sopenharmony_ci case AF_INET: 1698c2ecf20Sopenharmony_ci rt = ip_route_output_ports( 1708c2ecf20Sopenharmony_ci net, fl4, NULL, 1718c2ecf20Sopenharmony_ci peer->srx.transport.sin.sin_addr.s_addr, 0, 1728c2ecf20Sopenharmony_ci htons(7000), htons(7001), IPPROTO_UDP, 0, 0); 1738c2ecf20Sopenharmony_ci if (IS_ERR(rt)) { 1748c2ecf20Sopenharmony_ci _leave(" [route err %ld]", PTR_ERR(rt)); 1758c2ecf20Sopenharmony_ci return; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci dst = &rt->dst; 1788c2ecf20Sopenharmony_ci break; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci#ifdef CONFIG_AF_RXRPC_IPV6 1818c2ecf20Sopenharmony_ci case AF_INET6: 1828c2ecf20Sopenharmony_ci fl6->flowi6_iif = LOOPBACK_IFINDEX; 1838c2ecf20Sopenharmony_ci fl6->flowi6_scope = RT_SCOPE_UNIVERSE; 1848c2ecf20Sopenharmony_ci fl6->flowi6_proto = IPPROTO_UDP; 1858c2ecf20Sopenharmony_ci memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr, 1868c2ecf20Sopenharmony_ci sizeof(struct in6_addr)); 1878c2ecf20Sopenharmony_ci fl6->fl6_dport = htons(7001); 1888c2ecf20Sopenharmony_ci fl6->fl6_sport = htons(7000); 1898c2ecf20Sopenharmony_ci dst = ip6_route_output(net, NULL, fl6); 1908c2ecf20Sopenharmony_ci if (dst->error) { 1918c2ecf20Sopenharmony_ci _leave(" [route err %d]", dst->error); 1928c2ecf20Sopenharmony_ci return; 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci break; 1958c2ecf20Sopenharmony_ci#endif 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci default: 1988c2ecf20Sopenharmony_ci BUG(); 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci peer->if_mtu = dst_mtu(dst); 2028c2ecf20Sopenharmony_ci dst_release(dst); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci _leave(" [if_mtu %u]", peer->if_mtu); 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/* 2088c2ecf20Sopenharmony_ci * Allocate a peer. 2098c2ecf20Sopenharmony_ci */ 2108c2ecf20Sopenharmony_cistruct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci const void *here = __builtin_return_address(0); 2138c2ecf20Sopenharmony_ci struct rxrpc_peer *peer; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci _enter(""); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci peer = kzalloc(sizeof(struct rxrpc_peer), gfp); 2188c2ecf20Sopenharmony_ci if (peer) { 2198c2ecf20Sopenharmony_ci refcount_set(&peer->ref, 1); 2208c2ecf20Sopenharmony_ci peer->local = rxrpc_get_local(local); 2218c2ecf20Sopenharmony_ci INIT_HLIST_HEAD(&peer->error_targets); 2228c2ecf20Sopenharmony_ci peer->service_conns = RB_ROOT; 2238c2ecf20Sopenharmony_ci seqlock_init(&peer->service_conn_lock); 2248c2ecf20Sopenharmony_ci spin_lock_init(&peer->lock); 2258c2ecf20Sopenharmony_ci spin_lock_init(&peer->rtt_input_lock); 2268c2ecf20Sopenharmony_ci peer->debug_id = atomic_inc_return(&rxrpc_debug_id); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci rxrpc_peer_init_rtt(peer); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci if (RXRPC_TX_SMSS > 2190) 2318c2ecf20Sopenharmony_ci peer->cong_cwnd = 2; 2328c2ecf20Sopenharmony_ci else if (RXRPC_TX_SMSS > 1095) 2338c2ecf20Sopenharmony_ci peer->cong_cwnd = 3; 2348c2ecf20Sopenharmony_ci else 2358c2ecf20Sopenharmony_ci peer->cong_cwnd = 4; 2368c2ecf20Sopenharmony_ci trace_rxrpc_peer(peer->debug_id, rxrpc_peer_new, 1, here); 2378c2ecf20Sopenharmony_ci } 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci _leave(" = %p", peer); 2408c2ecf20Sopenharmony_ci return peer; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci/* 2448c2ecf20Sopenharmony_ci * Initialise peer record. 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_cistatic void rxrpc_init_peer(struct rxrpc_sock *rx, struct rxrpc_peer *peer, 2478c2ecf20Sopenharmony_ci unsigned long hash_key) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci peer->hash_key = hash_key; 2508c2ecf20Sopenharmony_ci rxrpc_assess_MTU_size(rx, peer); 2518c2ecf20Sopenharmony_ci peer->mtu = peer->if_mtu; 2528c2ecf20Sopenharmony_ci peer->rtt_last_req = ktime_get_real(); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci switch (peer->srx.transport.family) { 2558c2ecf20Sopenharmony_ci case AF_INET: 2568c2ecf20Sopenharmony_ci peer->hdrsize = sizeof(struct iphdr); 2578c2ecf20Sopenharmony_ci break; 2588c2ecf20Sopenharmony_ci#ifdef CONFIG_AF_RXRPC_IPV6 2598c2ecf20Sopenharmony_ci case AF_INET6: 2608c2ecf20Sopenharmony_ci peer->hdrsize = sizeof(struct ipv6hdr); 2618c2ecf20Sopenharmony_ci break; 2628c2ecf20Sopenharmony_ci#endif 2638c2ecf20Sopenharmony_ci default: 2648c2ecf20Sopenharmony_ci BUG(); 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci switch (peer->srx.transport_type) { 2688c2ecf20Sopenharmony_ci case SOCK_DGRAM: 2698c2ecf20Sopenharmony_ci peer->hdrsize += sizeof(struct udphdr); 2708c2ecf20Sopenharmony_ci break; 2718c2ecf20Sopenharmony_ci default: 2728c2ecf20Sopenharmony_ci BUG(); 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci peer->hdrsize += sizeof(struct rxrpc_wire_header); 2768c2ecf20Sopenharmony_ci peer->maxdata = peer->mtu - peer->hdrsize; 2778c2ecf20Sopenharmony_ci} 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci/* 2808c2ecf20Sopenharmony_ci * Set up a new peer. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_cistatic struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_sock *rx, 2838c2ecf20Sopenharmony_ci struct rxrpc_local *local, 2848c2ecf20Sopenharmony_ci struct sockaddr_rxrpc *srx, 2858c2ecf20Sopenharmony_ci unsigned long hash_key, 2868c2ecf20Sopenharmony_ci gfp_t gfp) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci struct rxrpc_peer *peer; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci _enter(""); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci peer = rxrpc_alloc_peer(local, gfp); 2938c2ecf20Sopenharmony_ci if (peer) { 2948c2ecf20Sopenharmony_ci memcpy(&peer->srx, srx, sizeof(*srx)); 2958c2ecf20Sopenharmony_ci rxrpc_init_peer(rx, peer, hash_key); 2968c2ecf20Sopenharmony_ci } 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci _leave(" = %p", peer); 2998c2ecf20Sopenharmony_ci return peer; 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_cistatic void rxrpc_free_peer(struct rxrpc_peer *peer) 3038c2ecf20Sopenharmony_ci{ 3048c2ecf20Sopenharmony_ci rxrpc_put_local(peer->local); 3058c2ecf20Sopenharmony_ci kfree_rcu(peer, rcu); 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci/* 3098c2ecf20Sopenharmony_ci * Set up a new incoming peer. There shouldn't be any other matching peers 3108c2ecf20Sopenharmony_ci * since we've already done a search in the list from the non-reentrant context 3118c2ecf20Sopenharmony_ci * (the data_ready handler) that is the only place we can add new peers. 3128c2ecf20Sopenharmony_ci */ 3138c2ecf20Sopenharmony_civoid rxrpc_new_incoming_peer(struct rxrpc_sock *rx, struct rxrpc_local *local, 3148c2ecf20Sopenharmony_ci struct rxrpc_peer *peer) 3158c2ecf20Sopenharmony_ci{ 3168c2ecf20Sopenharmony_ci struct rxrpc_net *rxnet = local->rxnet; 3178c2ecf20Sopenharmony_ci unsigned long hash_key; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci hash_key = rxrpc_peer_hash_key(local, &peer->srx); 3208c2ecf20Sopenharmony_ci rxrpc_init_peer(rx, peer, hash_key); 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci spin_lock(&rxnet->peer_hash_lock); 3238c2ecf20Sopenharmony_ci hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key); 3248c2ecf20Sopenharmony_ci list_add_tail(&peer->keepalive_link, &rxnet->peer_keepalive_new); 3258c2ecf20Sopenharmony_ci spin_unlock(&rxnet->peer_hash_lock); 3268c2ecf20Sopenharmony_ci} 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci/* 3298c2ecf20Sopenharmony_ci * obtain a remote transport endpoint for the specified address 3308c2ecf20Sopenharmony_ci */ 3318c2ecf20Sopenharmony_cistruct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_sock *rx, 3328c2ecf20Sopenharmony_ci struct rxrpc_local *local, 3338c2ecf20Sopenharmony_ci struct sockaddr_rxrpc *srx, gfp_t gfp) 3348c2ecf20Sopenharmony_ci{ 3358c2ecf20Sopenharmony_ci struct rxrpc_peer *peer, *candidate; 3368c2ecf20Sopenharmony_ci struct rxrpc_net *rxnet = local->rxnet; 3378c2ecf20Sopenharmony_ci unsigned long hash_key = rxrpc_peer_hash_key(local, srx); 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci _enter("{%pISp}", &srx->transport); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci /* search the peer list first */ 3428c2ecf20Sopenharmony_ci rcu_read_lock(); 3438c2ecf20Sopenharmony_ci peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key); 3448c2ecf20Sopenharmony_ci if (peer && !rxrpc_get_peer_maybe(peer)) 3458c2ecf20Sopenharmony_ci peer = NULL; 3468c2ecf20Sopenharmony_ci rcu_read_unlock(); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci if (!peer) { 3498c2ecf20Sopenharmony_ci /* The peer is not yet present in hash - create a candidate 3508c2ecf20Sopenharmony_ci * for a new record and then redo the search. 3518c2ecf20Sopenharmony_ci */ 3528c2ecf20Sopenharmony_ci candidate = rxrpc_create_peer(rx, local, srx, hash_key, gfp); 3538c2ecf20Sopenharmony_ci if (!candidate) { 3548c2ecf20Sopenharmony_ci _leave(" = NULL [nomem]"); 3558c2ecf20Sopenharmony_ci return NULL; 3568c2ecf20Sopenharmony_ci } 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci spin_lock_bh(&rxnet->peer_hash_lock); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci /* Need to check that we aren't racing with someone else */ 3618c2ecf20Sopenharmony_ci peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key); 3628c2ecf20Sopenharmony_ci if (peer && !rxrpc_get_peer_maybe(peer)) 3638c2ecf20Sopenharmony_ci peer = NULL; 3648c2ecf20Sopenharmony_ci if (!peer) { 3658c2ecf20Sopenharmony_ci hash_add_rcu(rxnet->peer_hash, 3668c2ecf20Sopenharmony_ci &candidate->hash_link, hash_key); 3678c2ecf20Sopenharmony_ci list_add_tail(&candidate->keepalive_link, 3688c2ecf20Sopenharmony_ci &rxnet->peer_keepalive_new); 3698c2ecf20Sopenharmony_ci } 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci spin_unlock_bh(&rxnet->peer_hash_lock); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci if (peer) 3748c2ecf20Sopenharmony_ci rxrpc_free_peer(candidate); 3758c2ecf20Sopenharmony_ci else 3768c2ecf20Sopenharmony_ci peer = candidate; 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport); 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref)); 3828c2ecf20Sopenharmony_ci return peer; 3838c2ecf20Sopenharmony_ci} 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci/* 3868c2ecf20Sopenharmony_ci * Get a ref on a peer record. 3878c2ecf20Sopenharmony_ci */ 3888c2ecf20Sopenharmony_cistruct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer) 3898c2ecf20Sopenharmony_ci{ 3908c2ecf20Sopenharmony_ci const void *here = __builtin_return_address(0); 3918c2ecf20Sopenharmony_ci int r; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci __refcount_inc(&peer->ref, &r); 3948c2ecf20Sopenharmony_ci trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, r + 1, here); 3958c2ecf20Sopenharmony_ci return peer; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci/* 3998c2ecf20Sopenharmony_ci * Get a ref on a peer record unless its usage has already reached 0. 4008c2ecf20Sopenharmony_ci */ 4018c2ecf20Sopenharmony_cistruct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer) 4028c2ecf20Sopenharmony_ci{ 4038c2ecf20Sopenharmony_ci const void *here = __builtin_return_address(0); 4048c2ecf20Sopenharmony_ci int r; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci if (peer) { 4078c2ecf20Sopenharmony_ci if (__refcount_inc_not_zero(&peer->ref, &r)) 4088c2ecf20Sopenharmony_ci trace_rxrpc_peer(peer->debug_id, rxrpc_peer_got, r + 1, here); 4098c2ecf20Sopenharmony_ci else 4108c2ecf20Sopenharmony_ci peer = NULL; 4118c2ecf20Sopenharmony_ci } 4128c2ecf20Sopenharmony_ci return peer; 4138c2ecf20Sopenharmony_ci} 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci/* 4168c2ecf20Sopenharmony_ci * Discard a peer record. 4178c2ecf20Sopenharmony_ci */ 4188c2ecf20Sopenharmony_cistatic void __rxrpc_put_peer(struct rxrpc_peer *peer) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci struct rxrpc_net *rxnet = peer->local->rxnet; 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci ASSERT(hlist_empty(&peer->error_targets)); 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci spin_lock_bh(&rxnet->peer_hash_lock); 4258c2ecf20Sopenharmony_ci hash_del_rcu(&peer->hash_link); 4268c2ecf20Sopenharmony_ci list_del_init(&peer->keepalive_link); 4278c2ecf20Sopenharmony_ci spin_unlock_bh(&rxnet->peer_hash_lock); 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci rxrpc_free_peer(peer); 4308c2ecf20Sopenharmony_ci} 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci/* 4338c2ecf20Sopenharmony_ci * Drop a ref on a peer record. 4348c2ecf20Sopenharmony_ci */ 4358c2ecf20Sopenharmony_civoid rxrpc_put_peer(struct rxrpc_peer *peer) 4368c2ecf20Sopenharmony_ci{ 4378c2ecf20Sopenharmony_ci const void *here = __builtin_return_address(0); 4388c2ecf20Sopenharmony_ci unsigned int debug_id; 4398c2ecf20Sopenharmony_ci bool dead; 4408c2ecf20Sopenharmony_ci int r; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci if (peer) { 4438c2ecf20Sopenharmony_ci debug_id = peer->debug_id; 4448c2ecf20Sopenharmony_ci dead = __refcount_dec_and_test(&peer->ref, &r); 4458c2ecf20Sopenharmony_ci trace_rxrpc_peer(debug_id, rxrpc_peer_put, r - 1, here); 4468c2ecf20Sopenharmony_ci if (dead) 4478c2ecf20Sopenharmony_ci __rxrpc_put_peer(peer); 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci} 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci/* 4528c2ecf20Sopenharmony_ci * Drop a ref on a peer record where the caller already holds the 4538c2ecf20Sopenharmony_ci * peer_hash_lock. 4548c2ecf20Sopenharmony_ci */ 4558c2ecf20Sopenharmony_civoid rxrpc_put_peer_locked(struct rxrpc_peer *peer) 4568c2ecf20Sopenharmony_ci{ 4578c2ecf20Sopenharmony_ci const void *here = __builtin_return_address(0); 4588c2ecf20Sopenharmony_ci unsigned int debug_id = peer->debug_id; 4598c2ecf20Sopenharmony_ci bool dead; 4608c2ecf20Sopenharmony_ci int r; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci dead = __refcount_dec_and_test(&peer->ref, &r); 4638c2ecf20Sopenharmony_ci trace_rxrpc_peer(debug_id, rxrpc_peer_put, r - 1, here); 4648c2ecf20Sopenharmony_ci if (dead) { 4658c2ecf20Sopenharmony_ci hash_del_rcu(&peer->hash_link); 4668c2ecf20Sopenharmony_ci list_del_init(&peer->keepalive_link); 4678c2ecf20Sopenharmony_ci rxrpc_free_peer(peer); 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci/* 4728c2ecf20Sopenharmony_ci * Make sure all peer records have been discarded. 4738c2ecf20Sopenharmony_ci */ 4748c2ecf20Sopenharmony_civoid rxrpc_destroy_all_peers(struct rxrpc_net *rxnet) 4758c2ecf20Sopenharmony_ci{ 4768c2ecf20Sopenharmony_ci struct rxrpc_peer *peer; 4778c2ecf20Sopenharmony_ci int i; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci for (i = 0; i < HASH_SIZE(rxnet->peer_hash); i++) { 4808c2ecf20Sopenharmony_ci if (hlist_empty(&rxnet->peer_hash[i])) 4818c2ecf20Sopenharmony_ci continue; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) { 4848c2ecf20Sopenharmony_ci pr_err("Leaked peer %u {%u} %pISp\n", 4858c2ecf20Sopenharmony_ci peer->debug_id, 4868c2ecf20Sopenharmony_ci refcount_read(&peer->ref), 4878c2ecf20Sopenharmony_ci &peer->srx.transport); 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci/** 4938c2ecf20Sopenharmony_ci * rxrpc_kernel_get_peer - Get the peer address of a call 4948c2ecf20Sopenharmony_ci * @sock: The socket on which the call is in progress. 4958c2ecf20Sopenharmony_ci * @call: The call to query 4968c2ecf20Sopenharmony_ci * @_srx: Where to place the result 4978c2ecf20Sopenharmony_ci * 4988c2ecf20Sopenharmony_ci * Get the address of the remote peer in a call. 4998c2ecf20Sopenharmony_ci */ 5008c2ecf20Sopenharmony_civoid rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call, 5018c2ecf20Sopenharmony_ci struct sockaddr_rxrpc *_srx) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci *_srx = call->peer->srx; 5048c2ecf20Sopenharmony_ci} 5058c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rxrpc_kernel_get_peer); 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci/** 5088c2ecf20Sopenharmony_ci * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT 5098c2ecf20Sopenharmony_ci * @sock: The socket on which the call is in progress. 5108c2ecf20Sopenharmony_ci * @call: The call to query 5118c2ecf20Sopenharmony_ci * @_srtt: Where to store the SRTT value. 5128c2ecf20Sopenharmony_ci * 5138c2ecf20Sopenharmony_ci * Get the call's peer smoothed RTT in uS. 5148c2ecf20Sopenharmony_ci */ 5158c2ecf20Sopenharmony_cibool rxrpc_kernel_get_srtt(struct socket *sock, struct rxrpc_call *call, 5168c2ecf20Sopenharmony_ci u32 *_srtt) 5178c2ecf20Sopenharmony_ci{ 5188c2ecf20Sopenharmony_ci struct rxrpc_peer *peer = call->peer; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci if (peer->rtt_count == 0) { 5218c2ecf20Sopenharmony_ci *_srtt = 1000000; /* 1S */ 5228c2ecf20Sopenharmony_ci return false; 5238c2ecf20Sopenharmony_ci } 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci *_srtt = call->peer->srtt_us >> 3; 5268c2ecf20Sopenharmony_ci return true; 5278c2ecf20Sopenharmony_ci} 5288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rxrpc_kernel_get_srtt); 529