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, NULL);
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))
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)
277 const void *here = __builtin_return_address(0);
280 __refcount_inc(&local->ref, &r);
281 trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
286 * Get a ref on a local endpoint unless its usage has already reached 0.
288 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
290 const void *here = __builtin_return_address(0);
294 if (__refcount_inc_not_zero(&local->ref, &r))
295 trace_rxrpc_local(local->debug_id, rxrpc_local_got,
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 const void *here = __builtin_return_address(0);
309 unsigned int debug_id = local->debug_id;
310 int r = refcount_read(&local->ref);
312 if (rxrpc_queue_work(&local->processor))
313 trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
315 rxrpc_put_local(local);
319 * Drop a ref on a local endpoint.
321 void rxrpc_put_local(struct rxrpc_local *local)
323 const void *here = __builtin_return_address(0);
324 unsigned int debug_id;
329 debug_id = local->debug_id;
331 dead = __refcount_dec_and_test(&local->ref, &r);
332 trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
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)
344 local = rxrpc_get_local_maybe(local);
348 if (!__rxrpc_use_local(local)) {
349 rxrpc_put_local(local);
357 * Cease using a local endpoint. Once the number of active users reaches 0, we
358 * start the closure of the transport in the work processor.
360 void rxrpc_unuse_local(struct rxrpc_local *local)
363 if (__rxrpc_unuse_local(local)) {
364 rxrpc_get_local(local);
365 rxrpc_queue_local(local);
371 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
374 * Closing the socket cannot be done from bottom half context or RCU callback
375 * context because it might sleep.
377 static void rxrpc_local_destroyer(struct rxrpc_local *local)
379 struct socket *socket = local->socket;
380 struct rxrpc_net *rxnet = local->rxnet;
382 _enter("%d", local->debug_id);
386 mutex_lock(&rxnet->local_mutex);
387 hlist_del_init_rcu(&local->link);
388 mutex_unlock(&rxnet->local_mutex);
390 rxrpc_clean_up_local_conns(local);
391 rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
392 ASSERT(!local->service);
395 local->socket = NULL;
396 kernel_sock_shutdown(socket, SHUT_RDWR);
397 socket->sk->sk_user_data = NULL;
398 sock_release(socket);
401 /* At this point, there should be no more packets coming in to the
404 rxrpc_purge_queue(&local->reject_queue);
405 rxrpc_purge_queue(&local->event_queue);
409 * Process events on an endpoint. The work item carries a ref which
412 static void rxrpc_local_processor(struct work_struct *work)
414 struct rxrpc_local *local =
415 container_of(work, struct rxrpc_local, processor);
421 trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
422 refcount_read(&local->ref), NULL);
426 if (!__rxrpc_use_local(local)) {
427 rxrpc_local_destroyer(local);
431 if (!list_empty(&local->ack_tx_queue)) {
432 rxrpc_transmit_ack_packets(local);
436 if (!skb_queue_empty(&local->reject_queue)) {
437 rxrpc_reject_packets(local);
441 if (!skb_queue_empty(&local->event_queue)) {
442 rxrpc_process_local_events(local);
446 __rxrpc_unuse_local(local);
449 rxrpc_put_local(local);
453 * Destroy a local endpoint after the RCU grace period expires.
455 static void rxrpc_local_rcu(struct rcu_head *rcu)
457 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
459 _enter("%d", local->debug_id);
461 ASSERT(!work_pending(&local->processor));
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);