Merge tag 'riscv-for-linus-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / net / rxrpc / peer_object.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
be6e6707 2/* RxRPC remote transport endpoint record management
17926a79 3 *
be6e6707 4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
17926a79 5 * Written by David Howells (dhowells@redhat.com)
17926a79
DH
6 */
7
9b6d5398
JP
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
17926a79
DH
10#include <linux/module.h>
11#include <linux/net.h>
12#include <linux/skbuff.h>
13#include <linux/udp.h>
14#include <linux/in.h>
75b54cb5 15#include <linux/in6.h>
5a0e3ad6 16#include <linux/slab.h>
be6e6707 17#include <linux/hashtable.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include <net/ip.h>
224711df 21#include <net/route.h>
75b54cb5 22#include <net/ip6_route.h>
17926a79
DH
23#include "ar-internal.h"
24
72904d7b
DH
25static const struct sockaddr_rxrpc rxrpc_null_addr;
26
be6e6707
DH
27/*
28 * Hash a peer key.
29 */
30static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
31 const struct sockaddr_rxrpc *srx)
32{
33 const u16 *p;
34 unsigned int i, size;
35 unsigned long hash_key;
36
37 _enter("");
38
39 hash_key = (unsigned long)local / __alignof__(*local);
40 hash_key += srx->transport_type;
41 hash_key += srx->transport_len;
42 hash_key += srx->transport.family;
43
44 switch (srx->transport.family) {
45 case AF_INET:
46 hash_key += (u16 __force)srx->transport.sin.sin_port;
47 size = sizeof(srx->transport.sin.sin_addr);
48 p = (u16 *)&srx->transport.sin.sin_addr;
49 break;
d1912747 50#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
51 case AF_INET6:
52 hash_key += (u16 __force)srx->transport.sin.sin_port;
53 size = sizeof(srx->transport.sin6.sin6_addr);
54 p = (u16 *)&srx->transport.sin6.sin6_addr;
55 break;
d1912747 56#endif
2f9f9f52
AB
57 default:
58 WARN(1, "AF_RXRPC: Unsupported transport address family\n");
59 return 0;
be6e6707
DH
60 }
61
62 /* Step through the peer address in 16-bit portions for speed */
63 for (i = 0; i < size; i += sizeof(*p), p++)
64 hash_key += *p;
65
66 _leave(" 0x%lx", hash_key);
67 return hash_key;
68}
69
70/*
71 * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
72 * or greater than.
73 *
74 * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
75 * buckets and mid-bucket insertion, so we don't make full use of this
76 * information at this point.
77 */
78static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
79 struct rxrpc_local *local,
80 const struct sockaddr_rxrpc *srx,
81 unsigned long hash_key)
82{
83 long diff;
84
85 diff = ((peer->hash_key - hash_key) ?:
86 ((unsigned long)peer->local - (unsigned long)local) ?:
87 (peer->srx.transport_type - srx->transport_type) ?:
88 (peer->srx.transport_len - srx->transport_len) ?:
89 (peer->srx.transport.family - srx->transport.family));
90 if (diff != 0)
91 return diff;
92
93 switch (srx->transport.family) {
94 case AF_INET:
95 return ((u16 __force)peer->srx.transport.sin.sin_port -
96 (u16 __force)srx->transport.sin.sin_port) ?:
97 memcmp(&peer->srx.transport.sin.sin_addr,
98 &srx->transport.sin.sin_addr,
99 sizeof(struct in_addr));
d1912747 100#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
101 case AF_INET6:
102 return ((u16 __force)peer->srx.transport.sin6.sin6_port -
103 (u16 __force)srx->transport.sin6.sin6_port) ?:
104 memcmp(&peer->srx.transport.sin6.sin6_addr,
105 &srx->transport.sin6.sin6_addr,
106 sizeof(struct in6_addr));
d1912747 107#endif
be6e6707
DH
108 default:
109 BUG();
110 }
111}
112
113/*
114 * Look up a remote transport endpoint for the specified address using RCU.
115 */
116static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
117 struct rxrpc_local *local,
118 const struct sockaddr_rxrpc *srx,
119 unsigned long hash_key)
120{
121 struct rxrpc_peer *peer;
2baec2c3 122 struct rxrpc_net *rxnet = local->rxnet;
be6e6707 123
2baec2c3 124 hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) {
0099dc58 125 if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 &&
a0575429 126 refcount_read(&peer->ref) > 0)
be6e6707 127 return peer;
be6e6707
DH
128 }
129
130 return NULL;
131}
132
133/*
134 * Look up a remote transport endpoint for the specified address using RCU.
135 */
136struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
137 const struct sockaddr_rxrpc *srx)
138{
139 struct rxrpc_peer *peer;
140 unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
141
142 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
e969c92c 143 if (peer)
a0575429 144 _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref));
be6e6707
DH
145 return peer;
146}
17926a79 147
224711df
DH
148/*
149 * assess the MTU size for the network interface through which this peer is
150 * reached
151 */
8a758d98 152static void rxrpc_assess_MTU_size(struct rxrpc_local *local,
5e33a23b 153 struct rxrpc_peer *peer)
224711df 154{
8a758d98 155 struct net *net = local->net;
75b54cb5 156 struct dst_entry *dst;
224711df 157 struct rtable *rt;
75b54cb5
DH
158 struct flowi fl;
159 struct flowi4 *fl4 = &fl.u.ip4;
d1912747 160#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5 161 struct flowi6 *fl6 = &fl.u.ip6;
d1912747 162#endif
224711df
DH
163
164 peer->if_mtu = 1500;
eeaedc54
DH
165 if (peer->max_data < peer->if_mtu - peer->hdrsize) {
166 trace_rxrpc_pmtud_reduce(peer, 0, peer->if_mtu - peer->hdrsize,
167 rxrpc_pmtud_reduce_route);
168 peer->max_data = peer->if_mtu - peer->hdrsize;
169 }
224711df 170
75b54cb5
DH
171 memset(&fl, 0, sizeof(fl));
172 switch (peer->srx.transport.family) {
173 case AF_INET:
174 rt = ip_route_output_ports(
5e33a23b 175 net, fl4, NULL,
75b54cb5
DH
176 peer->srx.transport.sin.sin_addr.s_addr, 0,
177 htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
178 if (IS_ERR(rt)) {
179 _leave(" [route err %ld]", PTR_ERR(rt));
180 return;
181 }
182 dst = &rt->dst;
183 break;
184
d1912747 185#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
186 case AF_INET6:
187 fl6->flowi6_iif = LOOPBACK_IFINDEX;
188 fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
189 fl6->flowi6_proto = IPPROTO_UDP;
190 memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
191 sizeof(struct in6_addr));
192 fl6->fl6_dport = htons(7001);
193 fl6->fl6_sport = htons(7000);
5e33a23b 194 dst = ip6_route_output(net, NULL, fl6);
07096f61
DH
195 if (dst->error) {
196 _leave(" [route err %d]", dst->error);
75b54cb5
DH
197 return;
198 }
199 break;
d1912747 200#endif
75b54cb5
DH
201
202 default:
203 BUG();
224711df
DH
204 }
205
75b54cb5 206 peer->if_mtu = dst_mtu(dst);
eeaedc54
DH
207 peer->hdrsize += dst->header_len + dst->trailer_len;
208 peer->tx_seg_max = dst->dev->gso_max_segs;
75b54cb5 209 dst_release(dst);
224711df 210
eeaedc54
DH
211 peer->max_data = umin(RXRPC_JUMBO(1), peer->if_mtu - peer->hdrsize);
212 peer->pmtud_good = 500;
213 peer->pmtud_bad = peer->if_mtu - peer->hdrsize + 1;
214 peer->pmtud_trial = umin(peer->max_data, peer->pmtud_bad - 1);
215 peer->pmtud_pending = true;
216
a6a62b69 217 _leave(" [if_mtu %u]", peer->if_mtu);
224711df
DH
218}
219
17926a79 220/*
be6e6707 221 * Allocate a peer.
17926a79 222 */
47c810a7
DH
223struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp,
224 enum rxrpc_peer_trace why)
17926a79
DH
225{
226 struct rxrpc_peer *peer;
227
228 _enter("");
229
230 peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
231 if (peer) {
a0575429 232 refcount_set(&peer->ref, 1);
0fde882f 233 peer->local = rxrpc_get_local(local, rxrpc_local_get_peer);
f66d7490 234 INIT_HLIST_HEAD(&peer->error_targets);
aa390bbe 235 peer->service_conns = RB_ROOT;
8496af50 236 seqlock_init(&peer->service_conn_lock);
17926a79 237 spin_lock_init(&peer->lock);
17926a79 238 peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
b40ef2b8 239 peer->recent_srtt_us = UINT_MAX;
1fc4fa2a 240 peer->cong_ssthresh = RXRPC_TX_MAX_WINDOW;
c838f1a7 241 trace_rxrpc_peer(peer->debug_id, 1, why);
be6e6707
DH
242 }
243
244 _leave(" = %p", peer);
245 return peer;
246}
247
248f219c
DH
248/*
249 * Initialise peer record.
250 */
8a758d98 251static void rxrpc_init_peer(struct rxrpc_local *local, struct rxrpc_peer *peer,
5e33a23b 252 unsigned long hash_key)
248f219c 253{
08a39685 254 peer->hash_key = hash_key;
eeaedc54 255
248f219c 256
75b54cb5
DH
257 switch (peer->srx.transport.family) {
258 case AF_INET:
248f219c 259 peer->hdrsize = sizeof(struct iphdr);
75b54cb5 260 break;
d1912747 261#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
262 case AF_INET6:
263 peer->hdrsize = sizeof(struct ipv6hdr);
264 break;
d1912747 265#endif
75b54cb5
DH
266 default:
267 BUG();
268 }
269
270 switch (peer->srx.transport_type) {
271 case SOCK_DGRAM:
272 peer->hdrsize += sizeof(struct udphdr);
273 break;
274 default:
248f219c
DH
275 BUG();
276 }
277
278 peer->hdrsize += sizeof(struct rxrpc_wire_header);
eeaedc54
DH
279 peer->max_data = peer->if_mtu - peer->hdrsize;
280
281 rxrpc_assess_MTU_size(local, peer);
248f219c
DH
282}
283
be6e6707
DH
284/*
285 * Set up a new peer.
286 */
8a758d98 287static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
be6e6707
DH
288 struct sockaddr_rxrpc *srx,
289 unsigned long hash_key,
290 gfp_t gfp)
291{
292 struct rxrpc_peer *peer;
293
294 _enter("");
295
47c810a7 296 peer = rxrpc_alloc_peer(local, gfp, rxrpc_peer_new_client);
be6e6707 297 if (peer) {
17926a79 298 memcpy(&peer->srx, srx, sizeof(*srx));
8a758d98 299 rxrpc_init_peer(local, peer, hash_key);
248f219c 300 }
17926a79 301
248f219c
DH
302 _leave(" = %p", peer);
303 return peer;
304}
17926a79 305
beacff50
ET
306static void rxrpc_free_peer(struct rxrpc_peer *peer)
307{
47c810a7 308 trace_rxrpc_peer(peer->debug_id, 0, rxrpc_peer_free);
0fde882f 309 rxrpc_put_local(peer->local, rxrpc_local_put_peer);
beacff50
ET
310 kfree_rcu(peer, rcu);
311}
312
248f219c 313/*
0099dc58
DH
314 * Set up a new incoming peer. There shouldn't be any other matching peers
315 * since we've already done a search in the list from the non-reentrant context
316 * (the data_ready handler) that is the only place we can add new peers.
a2ea9a90 317 * Called with interrupts disabled.
248f219c 318 */
8a758d98 319void rxrpc_new_incoming_peer(struct rxrpc_local *local, struct rxrpc_peer *peer)
248f219c 320{
2baec2c3 321 struct rxrpc_net *rxnet = local->rxnet;
248f219c
DH
322 unsigned long hash_key;
323
0099dc58 324 hash_key = rxrpc_peer_hash_key(local, &peer->srx);
8a758d98 325 rxrpc_init_peer(local, peer, hash_key);
248f219c 326
71f54091 327 spin_lock(&rxnet->peer_hash_lock);
0099dc58
DH
328 hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key);
329 list_add_tail(&peer->keepalive_link, &rxnet->peer_keepalive_new);
71f54091 330 spin_unlock(&rxnet->peer_hash_lock);
17926a79
DH
331}
332
333/*
334 * obtain a remote transport endpoint for the specified address
335 */
8a758d98 336struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
be6e6707 337 struct sockaddr_rxrpc *srx, gfp_t gfp)
17926a79
DH
338{
339 struct rxrpc_peer *peer, *candidate;
2baec2c3 340 struct rxrpc_net *rxnet = local->rxnet;
be6e6707 341 unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
17926a79 342
75b54cb5 343 _enter("{%pISp}", &srx->transport);
17926a79
DH
344
345 /* search the peer list first */
be6e6707
DH
346 rcu_read_lock();
347 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
47c810a7 348 if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client))
be6e6707
DH
349 peer = NULL;
350 rcu_read_unlock();
351
352 if (!peer) {
353 /* The peer is not yet present in hash - create a candidate
354 * for a new record and then redo the search.
355 */
8a758d98 356 candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
be6e6707
DH
357 if (!candidate) {
358 _leave(" = NULL [nomem]");
359 return NULL;
360 }
17926a79 361
79d458c1 362 spin_lock_bh(&rxnet->peer_hash_lock);
17926a79 363
be6e6707
DH
364 /* Need to check that we aren't racing with someone else */
365 peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
47c810a7 366 if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client))
be6e6707 367 peer = NULL;
ace45bec 368 if (!peer) {
2baec2c3 369 hash_add_rcu(rxnet->peer_hash,
be6e6707 370 &candidate->hash_link, hash_key);
330bdcfa
DH
371 list_add_tail(&candidate->keepalive_link,
372 &rxnet->peer_keepalive_new);
ace45bec 373 }
17926a79 374
79d458c1 375 spin_unlock_bh(&rxnet->peer_hash_lock);
17926a79 376
be6e6707 377 if (peer)
beacff50 378 rxrpc_free_peer(candidate);
be6e6707
DH
379 else
380 peer = candidate;
381 }
17926a79 382
a0575429 383 _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref));
17926a79 384 return peer;
17926a79
DH
385}
386
387/*
1159d4b4 388 * Get a ref on a peer record.
17926a79 389 */
47c810a7 390struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why)
1159d4b4 391{
a0575429 392 int r;
1159d4b4 393
a0575429 394 __refcount_inc(&peer->ref, &r);
c838f1a7 395 trace_rxrpc_peer(peer->debug_id, r + 1, why);
1159d4b4
DH
396 return peer;
397}
398
399/*
400 * Get a ref on a peer record unless its usage has already reached 0.
401 */
47c810a7
DH
402struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer,
403 enum rxrpc_peer_trace why)
1159d4b4 404{
a0575429 405 int r;
1159d4b4
DH
406
407 if (peer) {
a0575429 408 if (__refcount_inc_not_zero(&peer->ref, &r))
47c810a7 409 trace_rxrpc_peer(peer->debug_id, r + 1, why);
1159d4b4
DH
410 else
411 peer = NULL;
412 }
413 return peer;
414}
415
1159d4b4
DH
416/*
417 * Discard a peer record.
418 */
419static void __rxrpc_put_peer(struct rxrpc_peer *peer)
17926a79 420{
2baec2c3
DH
421 struct rxrpc_net *rxnet = peer->local->rxnet;
422
f66d7490 423 ASSERT(hlist_empty(&peer->error_targets));
17926a79 424
79d458c1 425 spin_lock_bh(&rxnet->peer_hash_lock);
be6e6707 426 hash_del_rcu(&peer->hash_link);
330bdcfa 427 list_del_init(&peer->keepalive_link);
79d458c1 428 spin_unlock_bh(&rxnet->peer_hash_lock);
17926a79 429
beacff50 430 rxrpc_free_peer(peer);
17926a79 431}
8324f0bc 432
1159d4b4
DH
433/*
434 * Drop a ref on a peer record.
435 */
47c810a7 436void rxrpc_put_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why)
1159d4b4 437{
55f6c98e 438 unsigned int debug_id;
a0575429
DH
439 bool dead;
440 int r;
1159d4b4
DH
441
442 if (peer) {
55f6c98e 443 debug_id = peer->debug_id;
a0575429 444 dead = __refcount_dec_and_test(&peer->ref, &r);
47c810a7 445 trace_rxrpc_peer(debug_id, r - 1, why);
a0575429 446 if (dead)
1159d4b4
DH
447 __rxrpc_put_peer(peer);
448 }
449}
450
17226f12
DH
451/*
452 * Make sure all peer records have been discarded.
453 */
454void rxrpc_destroy_all_peers(struct rxrpc_net *rxnet)
455{
456 struct rxrpc_peer *peer;
457 int i;
458
459 for (i = 0; i < HASH_SIZE(rxnet->peer_hash); i++) {
460 if (hlist_empty(&rxnet->peer_hash[i]))
461 continue;
462
463 hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) {
40e8b52f 464 pr_err("Leaked peer %x {%u} %pISp\n",
17226f12 465 peer->debug_id,
a0575429 466 refcount_read(&peer->ref),
17226f12
DH
467 &peer->srx.transport);
468 }
469 }
470}
471
8324f0bc 472/**
72904d7b 473 * rxrpc_kernel_get_call_peer - Get the peer address of a call
8324f0bc
DH
474 * @sock: The socket on which the call is in progress.
475 * @call: The call to query
8324f0bc 476 *
72904d7b 477 * Get a record for the remote peer in a call.
28a79fc9
DH
478 *
479 * Return: The call's peer record.
8324f0bc 480 */
72904d7b 481struct rxrpc_peer *rxrpc_kernel_get_call_peer(struct socket *sock, struct rxrpc_call *call)
8324f0bc 482{
40e8b52f 483 return rxrpc_get_peer(call->peer, rxrpc_peer_get_application);
8324f0bc 484}
72904d7b 485EXPORT_SYMBOL(rxrpc_kernel_get_call_peer);
f4d15fb6
DH
486
487/**
c410bf01 488 * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT
72904d7b 489 * @peer: The peer to query
f4d15fb6 490 *
28a79fc9
DH
491 * Get the call's peer smoothed RTT.
492 *
493 * Return: The RTT in uS or %UINT_MAX if we have no samples.
f4d15fb6 494 */
72904d7b 495unsigned int rxrpc_kernel_get_srtt(const struct rxrpc_peer *peer)
f4d15fb6 496{
b40ef2b8 497 return READ_ONCE(peer->recent_srtt_us);
72904d7b
DH
498}
499EXPORT_SYMBOL(rxrpc_kernel_get_srtt);
1d4adfaf 500
72904d7b
DH
501/**
502 * rxrpc_kernel_remote_srx - Get the address of a peer
503 * @peer: The peer to query
504 *
505 * Get a pointer to the address from a peer record. The caller is responsible
28a79fc9
DH
506 * for making sure that the address is not deallocated. A fake address will be
507 * substituted if %peer in NULL.
508 *
509 * Return: The rxrpc address record or a fake record.
72904d7b
DH
510 */
511const struct sockaddr_rxrpc *rxrpc_kernel_remote_srx(const struct rxrpc_peer *peer)
512{
513 return peer ? &peer->srx : &rxrpc_null_addr;
514}
515EXPORT_SYMBOL(rxrpc_kernel_remote_srx);
1d4adfaf 516
72904d7b
DH
517/**
518 * rxrpc_kernel_remote_addr - Get the peer transport address of a call
519 * @peer: The peer to query
520 *
521 * Get a pointer to the transport address from a peer record. The caller is
28a79fc9
DH
522 * responsible for making sure that the address is not deallocated. A fake
523 * address will be substituted if %peer in NULL.
524 *
525 * Return: The transport address record or a fake record.
72904d7b
DH
526 */
527const struct sockaddr *rxrpc_kernel_remote_addr(const struct rxrpc_peer *peer)
528{
529 return (const struct sockaddr *)
530 (peer ? &peer->srx.transport : &rxrpc_null_addr.transport);
f4d15fb6 531}
72904d7b 532EXPORT_SYMBOL(rxrpc_kernel_remote_addr);
f3a123b2
DH
533
534/**
535 * rxrpc_kernel_set_peer_data - Set app-specific data on a peer.
536 * @peer: The peer to alter
537 * @app_data: The data to set
538 *
539 * Set the app-specific data on a peer. AF_RXRPC makes no effort to retain
28a79fc9
DH
540 * anything the data might refer to.
541 *
542 * Return: The previous app_data.
f3a123b2
DH
543 */
544unsigned long rxrpc_kernel_set_peer_data(struct rxrpc_peer *peer, unsigned long app_data)
545{
546 return xchg(&peer->app_data, app_data);
547}
548EXPORT_SYMBOL(rxrpc_kernel_set_peer_data);
549
550/**
551 * rxrpc_kernel_get_peer_data - Get app-specific data from a peer.
552 * @peer: The peer to query
553 *
554 * Retrieve the app-specific data from a peer.
28a79fc9
DH
555 *
556 * Return: The peer's app data.
f3a123b2
DH
557 */
558unsigned long rxrpc_kernel_get_peer_data(const struct rxrpc_peer *peer)
559{
560 return peer->app_data;
561}
562EXPORT_SYMBOL(rxrpc_kernel_get_peer_data);