1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Local endpoint object management
4 * Copyright (C) 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/slab.h>
14 #include <linux/udp.h>
16 #include <linux/hashtable.h>
19 #include <net/udp_tunnel.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
23 static void rxrpc_local_processor(struct work_struct *);
24 static void rxrpc_local_rcu(struct rcu_head *);
27 * Handle an ICMP/ICMP6 error turning up at the tunnel. Push it through the
28 * usual mechanism so that it gets parsed and presented through the UDP
29 * socket's error_report().
31 static void rxrpc_encap_err_rcv(struct sock *sk, struct sk_buff *skb, int err,
32 __be16 port, u32 info, u8 *payload)
34 if (ip_hdr(skb)->version == IPVERSION)
35 return ip_icmp_error(sk, skb, err, port, info, payload);
36 return ipv6_icmp_error(sk, skb, err, port, info, payload);
40 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
41 * same or greater than.
43 * We explicitly don't compare the RxRPC service ID as we want to reject
44 * conflicting uses by differing services. Further, we don't want to share
45 * addresses with different options (IPv6), so we don't compare those bits
48 static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
49 const struct sockaddr_rxrpc *srx)
53 diff = ((local->srx.transport_type - srx->transport_type) ?:
54 (local->srx.transport_len - srx->transport_len) ?:
55 (local->srx.transport.family - srx->transport.family));
59 switch (srx->transport.family) {
61 /* If the choice of UDP port is left up to the transport, then
62 * the endpoint record doesn't match.
64 return ((u16 __force)local->srx.transport.sin.sin_port -
65 (u16 __force)srx->transport.sin.sin_port) ?:
66 memcmp(&local->srx.transport.sin.sin_addr,
67 &srx->transport.sin.sin_addr,
68 sizeof(struct in_addr));
69 #ifdef CONFIG_AF_RXRPC_IPV6
71 /* If the choice of UDP6 port is left up to the transport, then
72 * the endpoint record doesn't match.
74 return ((u16 __force)local->srx.transport.sin6.sin6_port -
75 (u16 __force)srx->transport.sin6.sin6_port) ?:
76 memcmp(&local->srx.transport.sin6.sin6_addr,
77 &srx->transport.sin6.sin6_addr,
78 sizeof(struct in6_addr));
86 * Allocate a new local endpoint.
88 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
89 const struct sockaddr_rxrpc *srx)
91 struct rxrpc_local *local;
93 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
95 refcount_set(&local->ref, 1);
96 atomic_set(&local->active_users, 1);
98 INIT_HLIST_NODE(&local->link);
99 INIT_WORK(&local->processor, rxrpc_local_processor);
100 INIT_LIST_HEAD(&local->ack_tx_queue);
101 spin_lock_init(&local->ack_tx_lock);
102 init_rwsem(&local->defrag_sem);
103 skb_queue_head_init(&local->reject_queue);
104 skb_queue_head_init(&local->event_queue);
105 local->client_bundles = RB_ROOT;
106 spin_lock_init(&local->client_bundles_lock);
107 spin_lock_init(&local->lock);
108 rwlock_init(&local->services_lock);
109 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
110 memcpy(&local->srx, srx, sizeof(*srx));
111 local->srx.srx_service = 0;
112 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
115 _leave(" = %p", local);
120 * create the local socket
121 * - must be called with rxrpc_local_mutex locked
123 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
125 struct udp_tunnel_sock_cfg tuncfg = {NULL};
126 struct sockaddr_rxrpc *srx = &local->srx;
127 struct udp_port_cfg udp_conf = {0};
132 local, srx->transport_type, srx->transport.family);
134 udp_conf.family = srx->transport.family;
135 udp_conf.use_udp_checksums = true;
136 if (udp_conf.family == AF_INET) {
137 udp_conf.local_ip = srx->transport.sin.sin_addr;
138 udp_conf.local_udp_port = srx->transport.sin.sin_port;
139 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
141 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
142 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
143 udp_conf.use_udp6_tx_checksums = true;
144 udp_conf.use_udp6_rx_checksums = true;
147 ret = udp_sock_create(net, &udp_conf, &local->socket);
149 _leave(" = %d [socket]", ret);
153 tuncfg.encap_type = UDP_ENCAP_RXRPC;
154 tuncfg.encap_rcv = rxrpc_input_packet;
155 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
156 tuncfg.sk_user_data = local;
157 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
159 /* set the socket up */
160 usk = local->socket->sk;
161 usk->sk_error_report = rxrpc_error_report;
163 switch (srx->transport.family) {
165 /* we want to receive ICMPv6 errors */
166 ip6_sock_set_recverr(usk);
168 /* Fall through and set IPv4 options too otherwise we don't get
169 * errors from IPv4 packets sent through the IPv6 socket.
173 /* we want to receive ICMP errors */
174 ip_sock_set_recverr(usk);
176 /* we want to set the don't fragment bit */
177 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
179 /* We want receive timestamps. */
180 sock_enable_timestamps(usk);
192 * Look up or create a new local endpoint using the specified local address.
194 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
195 const struct sockaddr_rxrpc *srx)
197 struct rxrpc_local *local;
198 struct rxrpc_net *rxnet = rxrpc_net(net);
199 struct hlist_node *cursor;
204 _enter("{%d,%d,%pISp}",
205 srx->transport_type, srx->transport.family, &srx->transport);
207 mutex_lock(&rxnet->local_mutex);
209 hlist_for_each(cursor, &rxnet->local_endpoints) {
210 local = hlist_entry(cursor, struct rxrpc_local, link);
212 diff = rxrpc_local_cmp_key(local, srx);
216 /* Services aren't allowed to share transport sockets, so
217 * reject that here. It is possible that the object is dying -
218 * but it may also still have the local transport address that
221 if (srx->srx_service) {
226 /* Found a match. We want to replace a dying object.
227 * Attempting to bind the transport socket may still fail if
228 * we're attempting to use a local address that the dying
229 * object is still using.
231 if (!rxrpc_use_local(local))
238 local = rxrpc_alloc_local(rxnet, srx);
242 ret = rxrpc_open_socket(local, net);
247 hlist_replace_rcu(cursor, &local->link);
248 cursor->pprev = NULL;
250 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
255 mutex_unlock(&rxnet->local_mutex);
257 _net("LOCAL %s %d {%pISp}",
258 age, local->debug_id, &local->srx.transport);
260 _leave(" = %p", local);
266 mutex_unlock(&rxnet->local_mutex);
268 call_rcu(&local->rcu, rxrpc_local_rcu);
269 _leave(" = %d", ret);
273 mutex_unlock(&rxnet->local_mutex);
274 _leave(" = -EADDRINUSE");
275 return ERR_PTR(-EADDRINUSE);
279 * Get a ref on a local endpoint.
281 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
283 const void *here = __builtin_return_address(0);
286 __refcount_inc(&local->ref, &r);
287 trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
292 * Get a ref on a local endpoint unless its usage has already reached 0.
294 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
296 const void *here = __builtin_return_address(0);
300 if (__refcount_inc_not_zero(&local->ref, &r))
301 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
310 * Queue a local endpoint and pass the caller's reference to the work item.
312 void rxrpc_queue_local(struct rxrpc_local *local)
314 const void *here = __builtin_return_address(0);
315 unsigned int debug_id = local->debug_id;
316 int r = refcount_read(&local->ref);
318 if (rxrpc_queue_work(&local->processor))
319 trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
321 rxrpc_put_local(local);
325 * Drop a ref on a local endpoint.
327 void rxrpc_put_local(struct rxrpc_local *local)
329 const void *here = __builtin_return_address(0);
330 unsigned int debug_id;
335 debug_id = local->debug_id;
337 dead = __refcount_dec_and_test(&local->ref, &r);
338 trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
341 call_rcu(&local->rcu, rxrpc_local_rcu);
346 * Start using a local endpoint.
348 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
350 local = rxrpc_get_local_maybe(local);
354 if (!__rxrpc_use_local(local)) {
355 rxrpc_put_local(local);
363 * Cease using a local endpoint. Once the number of active users reaches 0, we
364 * start the closure of the transport in the work processor.
366 void rxrpc_unuse_local(struct rxrpc_local *local)
369 if (__rxrpc_unuse_local(local)) {
370 rxrpc_get_local(local);
371 rxrpc_queue_local(local);
377 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
380 * Closing the socket cannot be done from bottom half context or RCU callback
381 * context because it might sleep.
383 static void rxrpc_local_destroyer(struct rxrpc_local *local)
385 struct socket *socket = local->socket;
386 struct rxrpc_net *rxnet = local->rxnet;
388 _enter("%d", local->debug_id);
392 mutex_lock(&rxnet->local_mutex);
393 hlist_del_init_rcu(&local->link);
394 mutex_unlock(&rxnet->local_mutex);
396 rxrpc_clean_up_local_conns(local);
397 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
398 ASSERT(!local->service);
401 local->socket = NULL;
402 kernel_sock_shutdown(socket, SHUT_RDWR);
403 socket->sk->sk_user_data = NULL;
404 sock_release(socket);
407 /* At this point, there should be no more packets coming in to the
410 rxrpc_purge_queue(&local->reject_queue);
411 rxrpc_purge_queue(&local->event_queue);
415 * Process events on an endpoint. The work item carries a ref which
418 static void rxrpc_local_processor(struct work_struct *work)
420 struct rxrpc_local *local =
421 container_of(work, struct rxrpc_local, processor);
427 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
428 refcount_read(&local->ref), NULL);
432 if (!__rxrpc_use_local(local)) {
433 rxrpc_local_destroyer(local);
437 if (!list_empty(&local->ack_tx_queue)) {
438 rxrpc_transmit_ack_packets(local);
442 if (!skb_queue_empty(&local->reject_queue)) {
443 rxrpc_reject_packets(local);
447 if (!skb_queue_empty(&local->event_queue)) {
448 rxrpc_process_local_events(local);
452 __rxrpc_unuse_local(local);
455 rxrpc_put_local(local);
459 * Destroy a local endpoint after the RCU grace period expires.
461 static void rxrpc_local_rcu(struct rcu_head *rcu)
463 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
465 _enter("%d", local->debug_id);
467 ASSERT(!work_pending(&local->processor));
469 _net("DESTROY LOCAL %d", local->debug_id);
475 * Verify the local endpoint list is empty by this point.
477 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
479 struct rxrpc_local *local;
483 flush_workqueue(rxrpc_workqueue);
485 if (!hlist_empty(&rxnet->local_endpoints)) {
486 mutex_lock(&rxnet->local_mutex);
487 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
488 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
489 local, refcount_read(&local->ref));
491 mutex_unlock(&rxnet->local_mutex);