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 if (IS_ENABLED(CONFIG_AF_RXRPC_IPV6))
37 return ipv6_icmp_error(sk, skb, err, port, info, payload);
41 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
42 * same or greater than.
44 * We explicitly don't compare the RxRPC service ID as we want to reject
45 * conflicting uses by differing services. Further, we don't want to share
46 * addresses with different options (IPv6), so we don't compare those bits
49 static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
50 const struct sockaddr_rxrpc *srx)
54 diff = ((local->srx.transport_type - srx->transport_type) ?:
55 (local->srx.transport_len - srx->transport_len) ?:
56 (local->srx.transport.family - srx->transport.family));
60 switch (srx->transport.family) {
62 /* If the choice of UDP port is left up to the transport, then
63 * the endpoint record doesn't match.
65 return ((u16 __force)local->srx.transport.sin.sin_port -
66 (u16 __force)srx->transport.sin.sin_port) ?:
67 memcmp(&local->srx.transport.sin.sin_addr,
68 &srx->transport.sin.sin_addr,
69 sizeof(struct in_addr));
70 #ifdef CONFIG_AF_RXRPC_IPV6
72 /* If the choice of UDP6 port is left up to the transport, then
73 * the endpoint record doesn't match.
75 return ((u16 __force)local->srx.transport.sin6.sin6_port -
76 (u16 __force)srx->transport.sin6.sin6_port) ?:
77 memcmp(&local->srx.transport.sin6.sin6_addr,
78 &srx->transport.sin6.sin6_addr,
79 sizeof(struct in6_addr));
87 * Allocate a new local endpoint.
89 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
90 const struct sockaddr_rxrpc *srx)
92 struct rxrpc_local *local;
94 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
96 refcount_set(&local->ref, 1);
97 atomic_set(&local->active_users, 1);
99 INIT_HLIST_NODE(&local->link);
100 INIT_WORK(&local->processor, rxrpc_local_processor);
101 INIT_LIST_HEAD(&local->ack_tx_queue);
102 spin_lock_init(&local->ack_tx_lock);
103 init_rwsem(&local->defrag_sem);
104 skb_queue_head_init(&local->reject_queue);
105 skb_queue_head_init(&local->event_queue);
106 local->client_bundles = RB_ROOT;
107 spin_lock_init(&local->client_bundles_lock);
108 spin_lock_init(&local->lock);
109 rwlock_init(&local->services_lock);
110 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
111 memcpy(&local->srx, srx, sizeof(*srx));
112 local->srx.srx_service = 0;
113 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, 1);
116 _leave(" = %p", local);
121 * create the local socket
122 * - must be called with rxrpc_local_mutex locked
124 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
126 struct udp_tunnel_sock_cfg tuncfg = {NULL};
127 struct sockaddr_rxrpc *srx = &local->srx;
128 struct udp_port_cfg udp_conf = {0};
133 local, srx->transport_type, srx->transport.family);
135 udp_conf.family = srx->transport.family;
136 udp_conf.use_udp_checksums = true;
137 if (udp_conf.family == AF_INET) {
138 udp_conf.local_ip = srx->transport.sin.sin_addr;
139 udp_conf.local_udp_port = srx->transport.sin.sin_port;
140 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
142 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
143 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
144 udp_conf.use_udp6_tx_checksums = true;
145 udp_conf.use_udp6_rx_checksums = true;
148 ret = udp_sock_create(net, &udp_conf, &local->socket);
150 _leave(" = %d [socket]", ret);
154 tuncfg.encap_type = UDP_ENCAP_RXRPC;
155 tuncfg.encap_rcv = rxrpc_input_packet;
156 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
157 tuncfg.sk_user_data = local;
158 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
160 /* set the socket up */
161 usk = local->socket->sk;
162 usk->sk_error_report = rxrpc_error_report;
164 switch (srx->transport.family) {
166 /* we want to receive ICMPv6 errors */
167 ip6_sock_set_recverr(usk);
169 /* Fall through and set IPv4 options too otherwise we don't get
170 * errors from IPv4 packets sent through the IPv6 socket.
174 /* we want to receive ICMP errors */
175 ip_sock_set_recverr(usk);
177 /* we want to set the don't fragment bit */
178 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
180 /* We want receive timestamps. */
181 sock_enable_timestamps(usk);
193 * Look up or create a new local endpoint using the specified local address.
195 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
196 const struct sockaddr_rxrpc *srx)
198 struct rxrpc_local *local;
199 struct rxrpc_net *rxnet = rxrpc_net(net);
200 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, rxrpc_local_use_lookup))
237 local = rxrpc_alloc_local(rxnet, srx);
241 ret = rxrpc_open_socket(local, net);
246 hlist_replace_rcu(cursor, &local->link);
247 cursor->pprev = NULL;
249 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
253 mutex_unlock(&rxnet->local_mutex);
254 _leave(" = %p", local);
260 mutex_unlock(&rxnet->local_mutex);
262 call_rcu(&local->rcu, rxrpc_local_rcu);
263 _leave(" = %d", ret);
267 mutex_unlock(&rxnet->local_mutex);
268 _leave(" = -EADDRINUSE");
269 return ERR_PTR(-EADDRINUSE);
273 * Get a ref on a local endpoint.
275 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local,
276 enum rxrpc_local_trace why)
280 u = atomic_read(&local->active_users);
281 __refcount_inc(&local->ref, &r);
282 trace_rxrpc_local(local->debug_id, why, r + 1, u);
287 * Get a ref on a local endpoint unless its usage has already reached 0.
289 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local,
290 enum rxrpc_local_trace why)
294 if (local && __refcount_inc_not_zero(&local->ref, &r)) {
295 u = atomic_read(&local->active_users);
296 trace_rxrpc_local(local->debug_id, why, r + 1, u);
304 * Queue a local endpoint and pass the caller's reference to the work item.
306 void rxrpc_queue_local(struct rxrpc_local *local)
308 unsigned int debug_id = local->debug_id;
309 int r = refcount_read(&local->ref);
310 int u = atomic_read(&local->active_users);
312 if (rxrpc_queue_work(&local->processor))
313 trace_rxrpc_local(debug_id, rxrpc_local_queued, r, u);
315 rxrpc_put_local(local, rxrpc_local_put_already_queued);
319 * Drop a ref on a local endpoint.
321 void rxrpc_put_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
323 unsigned int debug_id;
328 debug_id = local->debug_id;
330 u = atomic_read(&local->active_users);
331 dead = __refcount_dec_and_test(&local->ref, &r);
332 trace_rxrpc_local(debug_id, why, r, u);
335 call_rcu(&local->rcu, rxrpc_local_rcu);
340 * Start using a local endpoint.
342 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local,
343 enum rxrpc_local_trace why)
345 local = rxrpc_get_local_maybe(local, rxrpc_local_get_for_use);
349 if (!__rxrpc_use_local(local, why)) {
350 rxrpc_put_local(local, rxrpc_local_put_for_use);
358 * Cease using a local endpoint. Once the number of active users reaches 0, we
359 * start the closure of the transport in the work processor.
361 void rxrpc_unuse_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
363 unsigned int debug_id;
367 debug_id = local->debug_id;
368 r = refcount_read(&local->ref);
369 u = atomic_dec_return(&local->active_users);
370 trace_rxrpc_local(debug_id, why, r, u);
372 rxrpc_get_local(local, rxrpc_local_get_queue);
373 rxrpc_queue_local(local);
379 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
382 * Closing the socket cannot be done from bottom half context or RCU callback
383 * context because it might sleep.
385 static void rxrpc_local_destroyer(struct rxrpc_local *local)
387 struct socket *socket = local->socket;
388 struct rxrpc_net *rxnet = local->rxnet;
390 _enter("%d", local->debug_id);
394 mutex_lock(&rxnet->local_mutex);
395 hlist_del_init_rcu(&local->link);
396 mutex_unlock(&rxnet->local_mutex);
398 rxrpc_clean_up_local_conns(local);
399 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
400 ASSERT(!local->service);
403 local->socket = NULL;
404 kernel_sock_shutdown(socket, SHUT_RDWR);
405 socket->sk->sk_user_data = NULL;
406 sock_release(socket);
409 /* At this point, there should be no more packets coming in to the
412 rxrpc_purge_queue(&local->reject_queue);
413 rxrpc_purge_queue(&local->event_queue);
417 * Process events on an endpoint. The work item carries a ref which
420 static void rxrpc_local_processor(struct work_struct *work)
422 struct rxrpc_local *local =
423 container_of(work, struct rxrpc_local, processor);
429 rxrpc_see_local(local, rxrpc_local_processing);
433 if (!__rxrpc_use_local(local, rxrpc_local_use_work)) {
434 rxrpc_local_destroyer(local);
438 if (!list_empty(&local->ack_tx_queue)) {
439 rxrpc_transmit_ack_packets(local);
443 if (!skb_queue_empty(&local->reject_queue)) {
444 rxrpc_reject_packets(local);
448 if (!skb_queue_empty(&local->event_queue)) {
449 rxrpc_process_local_events(local);
453 __rxrpc_unuse_local(local, rxrpc_local_unuse_work);
456 rxrpc_put_local(local, rxrpc_local_put_queue);
460 * Destroy a local endpoint after the RCU grace period expires.
462 static void rxrpc_local_rcu(struct rcu_head *rcu)
464 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
466 _enter("%d", local->debug_id);
468 ASSERT(!work_pending(&local->processor));
470 rxrpc_see_local(local, rxrpc_local_free);
476 * Verify the local endpoint list is empty by this point.
478 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
480 struct rxrpc_local *local;
484 flush_workqueue(rxrpc_workqueue);
486 if (!hlist_empty(&rxnet->local_endpoints)) {
487 mutex_lock(&rxnet->local_mutex);
488 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
489 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
490 local, refcount_read(&local->ref));
492 mutex_unlock(&rxnet->local_mutex);