1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC remote transport endpoint record management
4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/udp.h>
15 #include <linux/in6.h>
16 #include <linux/slab.h>
17 #include <linux/hashtable.h>
19 #include <net/af_rxrpc.h>
21 #include <net/route.h>
22 #include <net/ip6_route.h>
23 #include "ar-internal.h"
25 static const struct sockaddr_rxrpc rxrpc_null_addr
;
30 static unsigned long rxrpc_peer_hash_key(struct rxrpc_local
*local
,
31 const struct sockaddr_rxrpc
*srx
)
35 unsigned long hash_key
;
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
;
44 switch (srx
->transport
.family
) {
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
;
50 #ifdef CONFIG_AF_RXRPC_IPV6
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
;
58 WARN(1, "AF_RXRPC: Unsupported transport address family\n");
62 /* Step through the peer address in 16-bit portions for speed */
63 for (i
= 0; i
< size
; i
+= sizeof(*p
), p
++)
66 _leave(" 0x%lx", hash_key
);
71 * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
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.
78 static 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
)
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
));
93 switch (srx
->transport
.family
) {
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
));
100 #ifdef CONFIG_AF_RXRPC_IPV6
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
));
114 * Look up a remote transport endpoint for the specified address using RCU.
116 static struct rxrpc_peer
*__rxrpc_lookup_peer_rcu(
117 struct rxrpc_local
*local
,
118 const struct sockaddr_rxrpc
*srx
,
119 unsigned long hash_key
)
121 struct rxrpc_peer
*peer
;
122 struct rxrpc_net
*rxnet
= local
->rxnet
;
124 hash_for_each_possible_rcu(rxnet
->peer_hash
, peer
, hash_link
, hash_key
) {
125 if (rxrpc_peer_cmp_key(peer
, local
, srx
, hash_key
) == 0 &&
126 refcount_read(&peer
->ref
) > 0)
134 * Look up a remote transport endpoint for the specified address using RCU.
136 struct rxrpc_peer
*rxrpc_lookup_peer_rcu(struct rxrpc_local
*local
,
137 const struct sockaddr_rxrpc
*srx
)
139 struct rxrpc_peer
*peer
;
140 unsigned long hash_key
= rxrpc_peer_hash_key(local
, srx
);
142 peer
= __rxrpc_lookup_peer_rcu(local
, srx
, hash_key
);
144 _leave(" = %p {u=%d}", peer
, refcount_read(&peer
->ref
));
149 * assess the MTU size for the network interface through which this peer is
152 static void rxrpc_assess_MTU_size(struct rxrpc_local
*local
,
153 struct rxrpc_peer
*peer
)
155 struct net
*net
= local
->net
;
156 struct dst_entry
*dst
;
159 struct flowi4
*fl4
= &fl
.u
.ip4
;
160 #ifdef CONFIG_AF_RXRPC_IPV6
161 struct flowi6
*fl6
= &fl
.u
.ip6
;
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
;
171 memset(&fl
, 0, sizeof(fl
));
172 switch (peer
->srx
.transport
.family
) {
174 rt
= ip_route_output_ports(
176 peer
->srx
.transport
.sin
.sin_addr
.s_addr
, 0,
177 htons(7000), htons(7001), IPPROTO_UDP
, 0, 0);
179 _leave(" [route err %ld]", PTR_ERR(rt
));
185 #ifdef CONFIG_AF_RXRPC_IPV6
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);
194 dst
= ip6_route_output(net
, NULL
, fl6
);
196 _leave(" [route err %d]", dst
->error
);
206 peer
->if_mtu
= dst_mtu(dst
);
207 peer
->hdrsize
+= dst
->header_len
+ dst
->trailer_len
;
208 peer
->tx_seg_max
= dst
->dev
->gso_max_segs
;
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;
217 _leave(" [if_mtu %u]", peer
->if_mtu
);
223 struct rxrpc_peer
*rxrpc_alloc_peer(struct rxrpc_local
*local
, gfp_t gfp
,
224 enum rxrpc_peer_trace why
)
226 struct rxrpc_peer
*peer
;
230 peer
= kzalloc(sizeof(struct rxrpc_peer
), gfp
);
232 refcount_set(&peer
->ref
, 1);
233 peer
->local
= rxrpc_get_local(local
, rxrpc_local_get_peer
);
234 INIT_HLIST_HEAD(&peer
->error_targets
);
235 peer
->service_conns
= RB_ROOT
;
236 seqlock_init(&peer
->service_conn_lock
);
237 spin_lock_init(&peer
->lock
);
238 peer
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
239 peer
->recent_srtt_us
= UINT_MAX
;
240 peer
->cong_ssthresh
= RXRPC_TX_MAX_WINDOW
;
241 trace_rxrpc_peer(peer
->debug_id
, 1, why
);
244 _leave(" = %p", peer
);
249 * Initialise peer record.
251 static void rxrpc_init_peer(struct rxrpc_local
*local
, struct rxrpc_peer
*peer
,
252 unsigned long hash_key
)
254 peer
->hash_key
= hash_key
;
257 switch (peer
->srx
.transport
.family
) {
259 peer
->hdrsize
= sizeof(struct iphdr
);
261 #ifdef CONFIG_AF_RXRPC_IPV6
263 peer
->hdrsize
= sizeof(struct ipv6hdr
);
270 switch (peer
->srx
.transport_type
) {
272 peer
->hdrsize
+= sizeof(struct udphdr
);
278 peer
->hdrsize
+= sizeof(struct rxrpc_wire_header
);
279 peer
->max_data
= peer
->if_mtu
- peer
->hdrsize
;
281 rxrpc_assess_MTU_size(local
, peer
);
287 static struct rxrpc_peer
*rxrpc_create_peer(struct rxrpc_local
*local
,
288 struct sockaddr_rxrpc
*srx
,
289 unsigned long hash_key
,
292 struct rxrpc_peer
*peer
;
296 peer
= rxrpc_alloc_peer(local
, gfp
, rxrpc_peer_new_client
);
298 memcpy(&peer
->srx
, srx
, sizeof(*srx
));
299 rxrpc_init_peer(local
, peer
, hash_key
);
302 _leave(" = %p", peer
);
306 static void rxrpc_free_peer(struct rxrpc_peer
*peer
)
308 trace_rxrpc_peer(peer
->debug_id
, 0, rxrpc_peer_free
);
309 rxrpc_put_local(peer
->local
, rxrpc_local_put_peer
);
310 kfree_rcu(peer
, rcu
);
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.
317 * Called with interrupts disabled.
319 void rxrpc_new_incoming_peer(struct rxrpc_local
*local
, struct rxrpc_peer
*peer
)
321 struct rxrpc_net
*rxnet
= local
->rxnet
;
322 unsigned long hash_key
;
324 hash_key
= rxrpc_peer_hash_key(local
, &peer
->srx
);
325 rxrpc_init_peer(local
, peer
, hash_key
);
327 spin_lock(&rxnet
->peer_hash_lock
);
328 hash_add_rcu(rxnet
->peer_hash
, &peer
->hash_link
, hash_key
);
329 list_add_tail(&peer
->keepalive_link
, &rxnet
->peer_keepalive_new
);
330 spin_unlock(&rxnet
->peer_hash_lock
);
334 * obtain a remote transport endpoint for the specified address
336 struct rxrpc_peer
*rxrpc_lookup_peer(struct rxrpc_local
*local
,
337 struct sockaddr_rxrpc
*srx
, gfp_t gfp
)
339 struct rxrpc_peer
*peer
, *candidate
;
340 struct rxrpc_net
*rxnet
= local
->rxnet
;
341 unsigned long hash_key
= rxrpc_peer_hash_key(local
, srx
);
343 _enter("{%pISp}", &srx
->transport
);
345 /* search the peer list first */
347 peer
= __rxrpc_lookup_peer_rcu(local
, srx
, hash_key
);
348 if (peer
&& !rxrpc_get_peer_maybe(peer
, rxrpc_peer_get_lookup_client
))
353 /* The peer is not yet present in hash - create a candidate
354 * for a new record and then redo the search.
356 candidate
= rxrpc_create_peer(local
, srx
, hash_key
, gfp
);
358 _leave(" = NULL [nomem]");
362 spin_lock_bh(&rxnet
->peer_hash_lock
);
364 /* Need to check that we aren't racing with someone else */
365 peer
= __rxrpc_lookup_peer_rcu(local
, srx
, hash_key
);
366 if (peer
&& !rxrpc_get_peer_maybe(peer
, rxrpc_peer_get_lookup_client
))
369 hash_add_rcu(rxnet
->peer_hash
,
370 &candidate
->hash_link
, hash_key
);
371 list_add_tail(&candidate
->keepalive_link
,
372 &rxnet
->peer_keepalive_new
);
375 spin_unlock_bh(&rxnet
->peer_hash_lock
);
378 rxrpc_free_peer(candidate
);
383 _leave(" = %p {u=%d}", peer
, refcount_read(&peer
->ref
));
388 * Get a ref on a peer record.
390 struct rxrpc_peer
*rxrpc_get_peer(struct rxrpc_peer
*peer
, enum rxrpc_peer_trace why
)
394 __refcount_inc(&peer
->ref
, &r
);
395 trace_rxrpc_peer(peer
->debug_id
, r
+ 1, why
);
400 * Get a ref on a peer record unless its usage has already reached 0.
402 struct rxrpc_peer
*rxrpc_get_peer_maybe(struct rxrpc_peer
*peer
,
403 enum rxrpc_peer_trace why
)
408 if (__refcount_inc_not_zero(&peer
->ref
, &r
))
409 trace_rxrpc_peer(peer
->debug_id
, r
+ 1, why
);
417 * Discard a peer record.
419 static void __rxrpc_put_peer(struct rxrpc_peer
*peer
)
421 struct rxrpc_net
*rxnet
= peer
->local
->rxnet
;
423 ASSERT(hlist_empty(&peer
->error_targets
));
425 spin_lock_bh(&rxnet
->peer_hash_lock
);
426 hash_del_rcu(&peer
->hash_link
);
427 list_del_init(&peer
->keepalive_link
);
428 spin_unlock_bh(&rxnet
->peer_hash_lock
);
430 rxrpc_free_peer(peer
);
434 * Drop a ref on a peer record.
436 void rxrpc_put_peer(struct rxrpc_peer
*peer
, enum rxrpc_peer_trace why
)
438 unsigned int debug_id
;
443 debug_id
= peer
->debug_id
;
444 dead
= __refcount_dec_and_test(&peer
->ref
, &r
);
445 trace_rxrpc_peer(debug_id
, r
- 1, why
);
447 __rxrpc_put_peer(peer
);
452 * Make sure all peer records have been discarded.
454 void rxrpc_destroy_all_peers(struct rxrpc_net
*rxnet
)
456 struct rxrpc_peer
*peer
;
459 for (i
= 0; i
< HASH_SIZE(rxnet
->peer_hash
); i
++) {
460 if (hlist_empty(&rxnet
->peer_hash
[i
]))
463 hlist_for_each_entry(peer
, &rxnet
->peer_hash
[i
], hash_link
) {
464 pr_err("Leaked peer %x {%u} %pISp\n",
466 refcount_read(&peer
->ref
),
467 &peer
->srx
.transport
);
473 * rxrpc_kernel_get_call_peer - Get the peer address of a call
474 * @sock: The socket on which the call is in progress.
475 * @call: The call to query
477 * Get a record for the remote peer in a call.
479 * Return: The call's peer record.
481 struct rxrpc_peer
*rxrpc_kernel_get_call_peer(struct socket
*sock
, struct rxrpc_call
*call
)
483 return rxrpc_get_peer(call
->peer
, rxrpc_peer_get_application
);
485 EXPORT_SYMBOL(rxrpc_kernel_get_call_peer
);
488 * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT
489 * @peer: The peer to query
491 * Get the call's peer smoothed RTT.
493 * Return: The RTT in uS or %UINT_MAX if we have no samples.
495 unsigned int rxrpc_kernel_get_srtt(const struct rxrpc_peer
*peer
)
497 return READ_ONCE(peer
->recent_srtt_us
);
499 EXPORT_SYMBOL(rxrpc_kernel_get_srtt
);
502 * rxrpc_kernel_remote_srx - Get the address of a peer
503 * @peer: The peer to query
505 * Get a pointer to the address from a peer record. The caller is responsible
506 * for making sure that the address is not deallocated. A fake address will be
507 * substituted if %peer in NULL.
509 * Return: The rxrpc address record or a fake record.
511 const struct sockaddr_rxrpc
*rxrpc_kernel_remote_srx(const struct rxrpc_peer
*peer
)
513 return peer
? &peer
->srx
: &rxrpc_null_addr
;
515 EXPORT_SYMBOL(rxrpc_kernel_remote_srx
);
518 * rxrpc_kernel_remote_addr - Get the peer transport address of a call
519 * @peer: The peer to query
521 * Get a pointer to the transport address from a peer record. The caller is
522 * responsible for making sure that the address is not deallocated. A fake
523 * address will be substituted if %peer in NULL.
525 * Return: The transport address record or a fake record.
527 const struct sockaddr
*rxrpc_kernel_remote_addr(const struct rxrpc_peer
*peer
)
529 return (const struct sockaddr
*)
530 (peer
? &peer
->srx
.transport
: &rxrpc_null_addr
.transport
);
532 EXPORT_SYMBOL(rxrpc_kernel_remote_addr
);
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
539 * Set the app-specific data on a peer. AF_RXRPC makes no effort to retain
540 * anything the data might refer to.
542 * Return: The previous app_data.
544 unsigned long rxrpc_kernel_set_peer_data(struct rxrpc_peer
*peer
, unsigned long app_data
)
546 return xchg(&peer
->app_data
, app_data
);
548 EXPORT_SYMBOL(rxrpc_kernel_set_peer_data
);
551 * rxrpc_kernel_get_peer_data - Get app-specific data from a peer.
552 * @peer: The peer to query
554 * Retrieve the app-specific data from a peer.
556 * Return: The peer's app data.
558 unsigned long rxrpc_kernel_get_peer_data(const struct rxrpc_peer
*peer
)
560 return peer
->app_data
;
562 EXPORT_SYMBOL(rxrpc_kernel_get_peer_data
);