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 skb_queue_head_init(&local->rx_queue);
107 local->client_bundles = RB_ROOT;
108 spin_lock_init(&local->client_bundles_lock);
109 spin_lock_init(&local->lock);
110 rwlock_init(&local->services_lock);
111 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
112 memcpy(&local->srx, srx, sizeof(*srx));
113 local->srx.srx_service = 0;
114 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, 1);
117 _leave(" = %p", local);
122 * create the local socket
123 * - must be called with rxrpc_local_mutex locked
125 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
127 struct udp_tunnel_sock_cfg tuncfg = {NULL};
128 struct sockaddr_rxrpc *srx = &local->srx;
129 struct udp_port_cfg udp_conf = {0};
130 struct task_struct *io_thread;
135 local, srx->transport_type, srx->transport.family);
137 udp_conf.family = srx->transport.family;
138 udp_conf.use_udp_checksums = true;
139 if (udp_conf.family == AF_INET) {
140 udp_conf.local_ip = srx->transport.sin.sin_addr;
141 udp_conf.local_udp_port = srx->transport.sin.sin_port;
142 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
144 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
145 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
146 udp_conf.use_udp6_tx_checksums = true;
147 udp_conf.use_udp6_rx_checksums = true;
150 ret = udp_sock_create(net, &udp_conf, &local->socket);
152 _leave(" = %d [socket]", ret);
156 tuncfg.encap_type = UDP_ENCAP_RXRPC;
157 tuncfg.encap_rcv = rxrpc_encap_rcv;
158 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
159 tuncfg.sk_user_data = local;
160 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
162 /* set the socket up */
163 usk = local->socket->sk;
164 usk->sk_error_report = rxrpc_error_report;
166 switch (srx->transport.family) {
168 /* we want to receive ICMPv6 errors */
169 ip6_sock_set_recverr(usk);
171 /* Fall through and set IPv4 options too otherwise we don't get
172 * errors from IPv4 packets sent through the IPv6 socket.
176 /* we want to receive ICMP errors */
177 ip_sock_set_recverr(usk);
179 /* we want to set the don't fragment bit */
180 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
182 /* We want receive timestamps. */
183 sock_enable_timestamps(usk);
190 io_thread = kthread_run(rxrpc_io_thread, local,
191 "krxrpcio/%u", ntohs(udp_conf.local_udp_port));
192 if (IS_ERR(io_thread)) {
193 ret = PTR_ERR(io_thread);
197 local->io_thread = io_thread;
202 kernel_sock_shutdown(local->socket, SHUT_RDWR);
203 local->socket->sk->sk_user_data = NULL;
204 sock_release(local->socket);
205 local->socket = NULL;
210 * Look up or create a new local endpoint using the specified local address.
212 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
213 const struct sockaddr_rxrpc *srx)
215 struct rxrpc_local *local;
216 struct rxrpc_net *rxnet = rxrpc_net(net);
217 struct hlist_node *cursor;
221 _enter("{%d,%d,%pISp}",
222 srx->transport_type, srx->transport.family, &srx->transport);
224 mutex_lock(&rxnet->local_mutex);
226 hlist_for_each(cursor, &rxnet->local_endpoints) {
227 local = hlist_entry(cursor, struct rxrpc_local, link);
229 diff = rxrpc_local_cmp_key(local, srx);
233 /* Services aren't allowed to share transport sockets, so
234 * reject that here. It is possible that the object is dying -
235 * but it may also still have the local transport address that
238 if (srx->srx_service) {
243 /* Found a match. We want to replace a dying object.
244 * Attempting to bind the transport socket may still fail if
245 * we're attempting to use a local address that the dying
246 * object is still using.
248 if (!rxrpc_use_local(local, rxrpc_local_use_lookup))
254 local = rxrpc_alloc_local(rxnet, srx);
258 ret = rxrpc_open_socket(local, net);
263 hlist_replace_rcu(cursor, &local->link);
264 cursor->pprev = NULL;
266 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
270 mutex_unlock(&rxnet->local_mutex);
271 _leave(" = %p", local);
277 mutex_unlock(&rxnet->local_mutex);
279 call_rcu(&local->rcu, rxrpc_local_rcu);
280 _leave(" = %d", ret);
284 mutex_unlock(&rxnet->local_mutex);
285 _leave(" = -EADDRINUSE");
286 return ERR_PTR(-EADDRINUSE);
290 * Get a ref on a local endpoint.
292 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local,
293 enum rxrpc_local_trace why)
297 u = atomic_read(&local->active_users);
298 __refcount_inc(&local->ref, &r);
299 trace_rxrpc_local(local->debug_id, why, r + 1, u);
304 * Get a ref on a local endpoint unless its usage has already reached 0.
306 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local,
307 enum rxrpc_local_trace why)
311 if (local && __refcount_inc_not_zero(&local->ref, &r)) {
312 u = atomic_read(&local->active_users);
313 trace_rxrpc_local(local->debug_id, why, r + 1, u);
321 * Queue a local endpoint and pass the caller's reference to the work item.
323 void rxrpc_queue_local(struct rxrpc_local *local)
325 unsigned int debug_id = local->debug_id;
326 int r = refcount_read(&local->ref);
327 int u = atomic_read(&local->active_users);
329 if (rxrpc_queue_work(&local->processor))
330 trace_rxrpc_local(debug_id, rxrpc_local_queued, r, u);
332 rxrpc_put_local(local, rxrpc_local_put_already_queued);
336 * Drop a ref on a local endpoint.
338 void rxrpc_put_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
340 unsigned int debug_id;
345 debug_id = local->debug_id;
347 u = atomic_read(&local->active_users);
348 dead = __refcount_dec_and_test(&local->ref, &r);
349 trace_rxrpc_local(debug_id, why, r, u);
352 call_rcu(&local->rcu, rxrpc_local_rcu);
357 * Start using a local endpoint.
359 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local,
360 enum rxrpc_local_trace why)
362 local = rxrpc_get_local_maybe(local, rxrpc_local_get_for_use);
366 if (!__rxrpc_use_local(local, why)) {
367 rxrpc_put_local(local, rxrpc_local_put_for_use);
375 * Cease using a local endpoint. Once the number of active users reaches 0, we
376 * start the closure of the transport in the work processor.
378 void rxrpc_unuse_local(struct rxrpc_local *local, enum rxrpc_local_trace why)
380 if (local && __rxrpc_unuse_local(local, why))
381 kthread_stop(local->io_thread);
385 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
388 * Closing the socket cannot be done from bottom half context or RCU callback
389 * context because it might sleep.
391 void rxrpc_destroy_local(struct rxrpc_local *local)
393 struct socket *socket = local->socket;
394 struct rxrpc_net *rxnet = local->rxnet;
396 _enter("%d", local->debug_id);
400 mutex_lock(&rxnet->local_mutex);
401 hlist_del_init_rcu(&local->link);
402 mutex_unlock(&rxnet->local_mutex);
404 rxrpc_clean_up_local_conns(local);
405 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
406 ASSERT(!local->service);
409 local->socket = NULL;
410 kernel_sock_shutdown(socket, SHUT_RDWR);
411 socket->sk->sk_user_data = NULL;
412 sock_release(socket);
415 /* At this point, there should be no more packets coming in to the
418 rxrpc_purge_queue(&local->reject_queue);
419 rxrpc_purge_queue(&local->event_queue);
420 rxrpc_purge_queue(&local->rx_queue);
424 * Process events on an endpoint. The work item carries a ref which
427 static void rxrpc_local_processor(struct work_struct *work)
429 struct rxrpc_local *local =
430 container_of(work, struct rxrpc_local, processor);
436 rxrpc_see_local(local, rxrpc_local_processing);
440 if (!__rxrpc_use_local(local, rxrpc_local_use_work))
443 if (!list_empty(&local->ack_tx_queue)) {
444 rxrpc_transmit_ack_packets(local);
448 if (!skb_queue_empty(&local->reject_queue)) {
449 rxrpc_reject_packets(local);
453 if (!skb_queue_empty(&local->event_queue)) {
454 rxrpc_process_local_events(local);
458 __rxrpc_unuse_local(local, rxrpc_local_unuse_work);
461 rxrpc_put_local(local, rxrpc_local_put_queue);
465 * Destroy a local endpoint after the RCU grace period expires.
467 static void rxrpc_local_rcu(struct rcu_head *rcu)
469 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
471 _enter("%d", local->debug_id);
473 ASSERT(!work_pending(&local->processor));
475 rxrpc_see_local(local, rxrpc_local_free);
481 * Verify the local endpoint list is empty by this point.
483 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
485 struct rxrpc_local *local;
489 flush_workqueue(rxrpc_workqueue);
491 if (!hlist_empty(&rxnet->local_endpoints)) {
492 mutex_lock(&rxnet->local_mutex);
493 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
494 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
495 local, refcount_read(&local->ref));
497 mutex_unlock(&rxnet->local_mutex);