]>
Commit | Line | Data |
---|---|---|
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 | */ | |
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 | */ | |
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 | */ | |
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 | */ | |
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 | */ | |
152 | static void rxrpc_assess_MTU_size(struct rxrpc_local *local, | |
153 | struct rxrpc_peer *peer) | |
154 | { | |
155 | struct net *net = local->net; | |
156 | struct dst_entry *dst; | |
157 | struct rtable *rt; | |
158 | struct flowi fl; | |
159 | struct flowi4 *fl4 = &fl.u.ip4; | |
160 | #ifdef CONFIG_AF_RXRPC_IPV6 | |
161 | struct flowi6 *fl6 = &fl.u.ip6; | |
162 | #endif | |
163 | ||
164 | peer->if_mtu = 1500; | |
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 | } | |
170 | ||
171 | memset(&fl, 0, sizeof(fl)); | |
172 | switch (peer->srx.transport.family) { | |
173 | case AF_INET: | |
174 | rt = ip_route_output_ports( | |
175 | net, fl4, NULL, | |
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 | ||
185 | #ifdef CONFIG_AF_RXRPC_IPV6 | |
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); | |
194 | dst = ip6_route_output(net, NULL, fl6); | |
195 | if (dst->error) { | |
196 | _leave(" [route err %d]", dst->error); | |
197 | return; | |
198 | } | |
199 | break; | |
200 | #endif | |
201 | ||
202 | default: | |
203 | BUG(); | |
204 | } | |
205 | ||
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; | |
209 | dst_release(dst); | |
210 | ||
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 | ||
217 | _leave(" [if_mtu %u]", peer->if_mtu); | |
218 | } | |
219 | ||
220 | /* | |
221 | * Allocate a peer. | |
222 | */ | |
223 | struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp, | |
224 | enum rxrpc_peer_trace why) | |
225 | { | |
226 | struct rxrpc_peer *peer; | |
227 | ||
228 | _enter(""); | |
229 | ||
230 | peer = kzalloc(sizeof(struct rxrpc_peer), gfp); | |
231 | if (peer) { | |
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); | |
242 | } | |
243 | ||
244 | _leave(" = %p", peer); | |
245 | return peer; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Initialise peer record. | |
250 | */ | |
251 | static void rxrpc_init_peer(struct rxrpc_local *local, struct rxrpc_peer *peer, | |
252 | unsigned long hash_key) | |
253 | { | |
254 | peer->hash_key = hash_key; | |
255 | ||
256 | ||
257 | switch (peer->srx.transport.family) { | |
258 | case AF_INET: | |
259 | peer->hdrsize = sizeof(struct iphdr); | |
260 | break; | |
261 | #ifdef CONFIG_AF_RXRPC_IPV6 | |
262 | case AF_INET6: | |
263 | peer->hdrsize = sizeof(struct ipv6hdr); | |
264 | break; | |
265 | #endif | |
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: | |
275 | BUG(); | |
276 | } | |
277 | ||
278 | peer->hdrsize += sizeof(struct rxrpc_wire_header); | |
279 | peer->max_data = peer->if_mtu - peer->hdrsize; | |
280 | ||
281 | rxrpc_assess_MTU_size(local, peer); | |
282 | } | |
283 | ||
284 | /* | |
285 | * Set up a new peer. | |
286 | */ | |
287 | static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local, | |
288 | struct sockaddr_rxrpc *srx, | |
289 | unsigned long hash_key, | |
290 | gfp_t gfp) | |
291 | { | |
292 | struct rxrpc_peer *peer; | |
293 | ||
294 | _enter(""); | |
295 | ||
296 | peer = rxrpc_alloc_peer(local, gfp, rxrpc_peer_new_client); | |
297 | if (peer) { | |
298 | memcpy(&peer->srx, srx, sizeof(*srx)); | |
299 | rxrpc_init_peer(local, peer, hash_key); | |
300 | } | |
301 | ||
302 | _leave(" = %p", peer); | |
303 | return peer; | |
304 | } | |
305 | ||
306 | static void rxrpc_free_peer(struct rxrpc_peer *peer) | |
307 | { | |
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); | |
311 | } | |
312 | ||
313 | /* | |
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. | |
318 | */ | |
319 | void rxrpc_new_incoming_peer(struct rxrpc_local *local, struct rxrpc_peer *peer) | |
320 | { | |
321 | struct rxrpc_net *rxnet = local->rxnet; | |
322 | unsigned long hash_key; | |
323 | ||
324 | hash_key = rxrpc_peer_hash_key(local, &peer->srx); | |
325 | rxrpc_init_peer(local, peer, hash_key); | |
326 | ||
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); | |
331 | } | |
332 | ||
333 | /* | |
334 | * obtain a remote transport endpoint for the specified address | |
335 | */ | |
336 | struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local, | |
337 | struct sockaddr_rxrpc *srx, gfp_t gfp) | |
338 | { | |
339 | struct rxrpc_peer *peer, *candidate; | |
340 | struct rxrpc_net *rxnet = local->rxnet; | |
341 | unsigned long hash_key = rxrpc_peer_hash_key(local, srx); | |
342 | ||
343 | _enter("{%pISp}", &srx->transport); | |
344 | ||
345 | /* search the peer list first */ | |
346 | rcu_read_lock(); | |
347 | peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key); | |
348 | if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client)) | |
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 | */ | |
356 | candidate = rxrpc_create_peer(local, srx, hash_key, gfp); | |
357 | if (!candidate) { | |
358 | _leave(" = NULL [nomem]"); | |
359 | return NULL; | |
360 | } | |
361 | ||
362 | spin_lock_bh(&rxnet->peer_hash_lock); | |
363 | ||
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)) | |
367 | peer = NULL; | |
368 | if (!peer) { | |
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); | |
373 | } | |
374 | ||
375 | spin_unlock_bh(&rxnet->peer_hash_lock); | |
376 | ||
377 | if (peer) | |
378 | rxrpc_free_peer(candidate); | |
379 | else | |
380 | peer = candidate; | |
381 | } | |
382 | ||
383 | _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref)); | |
384 | return peer; | |
385 | } | |
386 | ||
387 | /* | |
388 | * Get a ref on a peer record. | |
389 | */ | |
390 | struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why) | |
391 | { | |
392 | int r; | |
393 | ||
394 | __refcount_inc(&peer->ref, &r); | |
395 | trace_rxrpc_peer(peer->debug_id, r + 1, why); | |
396 | return peer; | |
397 | } | |
398 | ||
399 | /* | |
400 | * Get a ref on a peer record unless its usage has already reached 0. | |
401 | */ | |
402 | struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer, | |
403 | enum rxrpc_peer_trace why) | |
404 | { | |
405 | int r; | |
406 | ||
407 | if (peer) { | |
408 | if (__refcount_inc_not_zero(&peer->ref, &r)) | |
409 | trace_rxrpc_peer(peer->debug_id, r + 1, why); | |
410 | else | |
411 | peer = NULL; | |
412 | } | |
413 | return peer; | |
414 | } | |
415 | ||
416 | /* | |
417 | * Discard a peer record. | |
418 | */ | |
419 | static void __rxrpc_put_peer(struct rxrpc_peer *peer) | |
420 | { | |
421 | struct rxrpc_net *rxnet = peer->local->rxnet; | |
422 | ||
423 | ASSERT(hlist_empty(&peer->error_targets)); | |
424 | ||
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); | |
429 | ||
430 | rxrpc_free_peer(peer); | |
431 | } | |
432 | ||
433 | /* | |
434 | * Drop a ref on a peer record. | |
435 | */ | |
436 | void rxrpc_put_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why) | |
437 | { | |
438 | unsigned int debug_id; | |
439 | bool dead; | |
440 | int r; | |
441 | ||
442 | if (peer) { | |
443 | debug_id = peer->debug_id; | |
444 | dead = __refcount_dec_and_test(&peer->ref, &r); | |
445 | trace_rxrpc_peer(debug_id, r - 1, why); | |
446 | if (dead) | |
447 | __rxrpc_put_peer(peer); | |
448 | } | |
449 | } | |
450 | ||
451 | /* | |
452 | * Make sure all peer records have been discarded. | |
453 | */ | |
454 | void 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) { | |
464 | pr_err("Leaked peer %x {%u} %pISp\n", | |
465 | peer->debug_id, | |
466 | refcount_read(&peer->ref), | |
467 | &peer->srx.transport); | |
468 | } | |
469 | } | |
470 | } | |
471 | ||
472 | /** | |
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 | |
476 | * | |
477 | * Get a record for the remote peer in a call. | |
478 | * | |
479 | * Return: The call's peer record. | |
480 | */ | |
481 | struct rxrpc_peer *rxrpc_kernel_get_call_peer(struct socket *sock, struct rxrpc_call *call) | |
482 | { | |
483 | return rxrpc_get_peer(call->peer, rxrpc_peer_get_application); | |
484 | } | |
485 | EXPORT_SYMBOL(rxrpc_kernel_get_call_peer); | |
486 | ||
487 | /** | |
488 | * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT | |
489 | * @peer: The peer to query | |
490 | * | |
491 | * Get the call's peer smoothed RTT. | |
492 | * | |
493 | * Return: The RTT in uS or %UINT_MAX if we have no samples. | |
494 | */ | |
495 | unsigned int rxrpc_kernel_get_srtt(const struct rxrpc_peer *peer) | |
496 | { | |
497 | return READ_ONCE(peer->recent_srtt_us); | |
498 | } | |
499 | EXPORT_SYMBOL(rxrpc_kernel_get_srtt); | |
500 | ||
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 | |
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. | |
510 | */ | |
511 | const struct sockaddr_rxrpc *rxrpc_kernel_remote_srx(const struct rxrpc_peer *peer) | |
512 | { | |
513 | return peer ? &peer->srx : &rxrpc_null_addr; | |
514 | } | |
515 | EXPORT_SYMBOL(rxrpc_kernel_remote_srx); | |
516 | ||
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 | |
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. | |
526 | */ | |
527 | const 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); | |
531 | } | |
532 | EXPORT_SYMBOL(rxrpc_kernel_remote_addr); | |
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 | |
540 | * anything the data might refer to. | |
541 | * | |
542 | * Return: The previous app_data. | |
543 | */ | |
544 | unsigned long rxrpc_kernel_set_peer_data(struct rxrpc_peer *peer, unsigned long app_data) | |
545 | { | |
546 | return xchg(&peer->app_data, app_data); | |
547 | } | |
548 | EXPORT_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. | |
555 | * | |
556 | * Return: The peer's app data. | |
557 | */ | |
558 | unsigned long rxrpc_kernel_get_peer_data(const struct rxrpc_peer *peer) | |
559 | { | |
560 | return peer->app_data; | |
561 | } | |
562 | EXPORT_SYMBOL(rxrpc_kernel_get_peer_data); |