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