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_rwsem(&local->defrag_sem);
101 skb_queue_head_init(&local->reject_queue);
102 skb_queue_head_init(&local->event_queue);
103 local->client_bundles = RB_ROOT;
104 spin_lock_init(&local->client_bundles_lock);
105 spin_lock_init(&local->lock);
106 rwlock_init(&local->services_lock);
107 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
108 memcpy(&local->srx, srx, sizeof(*srx));
109 local->srx.srx_service = 0;
110 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
113 _leave(" = %p", local);
118 * create the local socket
119 * - must be called with rxrpc_local_mutex locked
121 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
123 struct udp_tunnel_sock_cfg tuncfg = {NULL};
124 struct sockaddr_rxrpc *srx = &local->srx;
125 struct udp_port_cfg udp_conf = {0};
130 local, srx->transport_type, srx->transport.family);
132 udp_conf.family = srx->transport.family;
133 udp_conf.use_udp_checksums = true;
134 if (udp_conf.family == AF_INET) {
135 udp_conf.local_ip = srx->transport.sin.sin_addr;
136 udp_conf.local_udp_port = srx->transport.sin.sin_port;
137 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
139 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
140 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
141 udp_conf.use_udp6_tx_checksums = true;
142 udp_conf.use_udp6_rx_checksums = true;
145 ret = udp_sock_create(net, &udp_conf, &local->socket);
147 _leave(" = %d [socket]", ret);
151 tuncfg.encap_type = UDP_ENCAP_RXRPC;
152 tuncfg.encap_rcv = rxrpc_input_packet;
153 tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
154 tuncfg.sk_user_data = local;
155 setup_udp_tunnel_sock(net, local->socket, &tuncfg);
157 /* set the socket up */
158 usk = local->socket->sk;
159 usk->sk_error_report = rxrpc_error_report;
161 switch (srx->transport.family) {
163 /* we want to receive ICMPv6 errors */
164 ip6_sock_set_recverr(usk);
166 /* Fall through and set IPv4 options too otherwise we don't get
167 * errors from IPv4 packets sent through the IPv6 socket.
171 /* we want to receive ICMP errors */
172 ip_sock_set_recverr(usk);
174 /* we want to set the don't fragment bit */
175 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
177 /* We want receive timestamps. */
178 sock_enable_timestamps(usk);
190 * Look up or create a new local endpoint using the specified local address.
192 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
193 const struct sockaddr_rxrpc *srx)
195 struct rxrpc_local *local;
196 struct rxrpc_net *rxnet = rxrpc_net(net);
197 struct hlist_node *cursor;
202 _enter("{%d,%d,%pISp}",
203 srx->transport_type, srx->transport.family, &srx->transport);
205 mutex_lock(&rxnet->local_mutex);
207 hlist_for_each(cursor, &rxnet->local_endpoints) {
208 local = hlist_entry(cursor, struct rxrpc_local, link);
210 diff = rxrpc_local_cmp_key(local, srx);
214 /* Services aren't allowed to share transport sockets, so
215 * reject that here. It is possible that the object is dying -
216 * but it may also still have the local transport address that
219 if (srx->srx_service) {
224 /* Found a match. We want to replace a dying object.
225 * Attempting to bind the transport socket may still fail if
226 * we're attempting to use a local address that the dying
227 * object is still using.
229 if (!rxrpc_use_local(local))
236 local = rxrpc_alloc_local(rxnet, srx);
240 ret = rxrpc_open_socket(local, net);
245 hlist_replace_rcu(cursor, &local->link);
246 cursor->pprev = NULL;
248 hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
253 mutex_unlock(&rxnet->local_mutex);
255 _net("LOCAL %s %d {%pISp}",
256 age, local->debug_id, &local->srx.transport);
258 _leave(" = %p", local);
264 mutex_unlock(&rxnet->local_mutex);
266 call_rcu(&local->rcu, rxrpc_local_rcu);
267 _leave(" = %d", ret);
271 mutex_unlock(&rxnet->local_mutex);
272 _leave(" = -EADDRINUSE");
273 return ERR_PTR(-EADDRINUSE);
277 * Get a ref on a local endpoint.
279 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
281 const void *here = __builtin_return_address(0);
284 __refcount_inc(&local->ref, &r);
285 trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
290 * Get a ref on a local endpoint unless its usage has already reached 0.
292 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
294 const void *here = __builtin_return_address(0);
298 if (__refcount_inc_not_zero(&local->ref, &r))
299 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
308 * Queue a local endpoint and pass the caller's reference to the work item.
310 void rxrpc_queue_local(struct rxrpc_local *local)
312 const void *here = __builtin_return_address(0);
313 unsigned int debug_id = local->debug_id;
314 int r = refcount_read(&local->ref);
316 if (rxrpc_queue_work(&local->processor))
317 trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
319 rxrpc_put_local(local);
323 * Drop a ref on a local endpoint.
325 void rxrpc_put_local(struct rxrpc_local *local)
327 const void *here = __builtin_return_address(0);
328 unsigned int debug_id;
333 debug_id = local->debug_id;
335 dead = __refcount_dec_and_test(&local->ref, &r);
336 trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
339 call_rcu(&local->rcu, rxrpc_local_rcu);
344 * Start using a local endpoint.
346 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
348 local = rxrpc_get_local_maybe(local);
352 if (!__rxrpc_use_local(local)) {
353 rxrpc_put_local(local);
361 * Cease using a local endpoint. Once the number of active users reaches 0, we
362 * start the closure of the transport in the work processor.
364 void rxrpc_unuse_local(struct rxrpc_local *local)
367 if (__rxrpc_unuse_local(local)) {
368 rxrpc_get_local(local);
369 rxrpc_queue_local(local);
375 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
378 * Closing the socket cannot be done from bottom half context or RCU callback
379 * context because it might sleep.
381 static void rxrpc_local_destroyer(struct rxrpc_local *local)
383 struct socket *socket = local->socket;
384 struct rxrpc_net *rxnet = local->rxnet;
386 _enter("%d", local->debug_id);
390 mutex_lock(&rxnet->local_mutex);
391 hlist_del_init_rcu(&local->link);
392 mutex_unlock(&rxnet->local_mutex);
394 rxrpc_clean_up_local_conns(local);
395 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
396 ASSERT(!local->service);
399 local->socket = NULL;
400 kernel_sock_shutdown(socket, SHUT_RDWR);
401 socket->sk->sk_user_data = NULL;
402 sock_release(socket);
405 /* At this point, there should be no more packets coming in to the
408 rxrpc_purge_queue(&local->reject_queue);
409 rxrpc_purge_queue(&local->event_queue);
413 * Process events on an endpoint. The work item carries a ref which
416 static void rxrpc_local_processor(struct work_struct *work)
418 struct rxrpc_local *local =
419 container_of(work, struct rxrpc_local, processor);
425 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
426 refcount_read(&local->ref), NULL);
430 if (!__rxrpc_use_local(local)) {
431 rxrpc_local_destroyer(local);
435 if (!skb_queue_empty(&local->reject_queue)) {
436 rxrpc_reject_packets(local);
440 if (!skb_queue_empty(&local->event_queue)) {
441 rxrpc_process_local_events(local);
445 __rxrpc_unuse_local(local);
448 rxrpc_put_local(local);
452 * Destroy a local endpoint after the RCU grace period expires.
454 static void rxrpc_local_rcu(struct rcu_head *rcu)
456 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
458 _enter("%d", local->debug_id);
460 ASSERT(!work_pending(&local->processor));
462 _net("DESTROY LOCAL %d", local->debug_id);
468 * Verify the local endpoint list is empty by this point.
470 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
472 struct rxrpc_local *local;
476 flush_workqueue(rxrpc_workqueue);
478 if (!hlist_empty(&rxnet->local_endpoints)) {
479 mutex_lock(&rxnet->local_mutex);
480 hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
481 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
482 local, refcount_read(&local->ref));
484 mutex_unlock(&rxnet->local_mutex);