xref: /openbmc/linux/net/rxrpc/local_object.c (revision 966a9b49033b472dcfb453abdc34bca7df17adce)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Local endpoint object management
3  *
4  * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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>
15 #include <linux/ip.h>
16 #include <linux/hashtable.h>
17 #include <net/sock.h>
18 #include <net/udp.h>
19 #include <net/udp_tunnel.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
22 
23 static void rxrpc_local_processor(struct work_struct *);
24 static void rxrpc_local_rcu(struct rcu_head *);
25 
26 /*
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().
30  */
31 static void rxrpc_encap_err_rcv(struct sock *sk, struct sk_buff *skb, int err,
32 				__be16 port, u32 info, u8 *payload)
33 {
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);
37 }
38 
39 /*
40  * Compare a local to an address.  Return -ve, 0 or +ve to indicate less than,
41  * same or greater than.
42  *
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
46  * either.
47  */
48 static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
49 				const struct sockaddr_rxrpc *srx)
50 {
51 	long diff;
52 
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));
56 	if (diff != 0)
57 		return diff;
58 
59 	switch (srx->transport.family) {
60 	case AF_INET:
61 		/* If the choice of UDP port is left up to the transport, then
62 		 * the endpoint record doesn't match.
63 		 */
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
70 	case AF_INET6:
71 		/* If the choice of UDP6 port is left up to the transport, then
72 		 * the endpoint record doesn't match.
73 		 */
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));
79 #endif
80 	default:
81 		BUG();
82 	}
83 }
84 
85 /*
86  * Allocate a new local endpoint.
87  */
88 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
89 					     const struct sockaddr_rxrpc *srx)
90 {
91 	struct rxrpc_local *local;
92 
93 	local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
94 	if (local) {
95 		refcount_set(&local->ref, 1);
96 		atomic_set(&local->active_users, 1);
97 		local->rxnet = rxnet;
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);
113 	}
114 
115 	_leave(" = %p", local);
116 	return local;
117 }
118 
119 /*
120  * create the local socket
121  * - must be called with rxrpc_local_mutex locked
122  */
123 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
124 {
125 	struct udp_tunnel_sock_cfg tuncfg = {NULL};
126 	struct sockaddr_rxrpc *srx = &local->srx;
127 	struct udp_port_cfg udp_conf = {0};
128 	struct sock *usk;
129 	int ret;
130 
131 	_enter("%p{%d,%d}",
132 	       local, srx->transport_type, srx->transport.family);
133 
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)
140 	} else {
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;
145 #endif
146 	}
147 	ret = udp_sock_create(net, &udp_conf, &local->socket);
148 	if (ret < 0) {
149 		_leave(" = %d [socket]", ret);
150 		return ret;
151 	}
152 
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);
158 
159 	/* set the socket up */
160 	usk = local->socket->sk;
161 	usk->sk_error_report = rxrpc_error_report;
162 
163 	switch (srx->transport.family) {
164 	case AF_INET6:
165 		/* we want to receive ICMPv6 errors */
166 		ip6_sock_set_recverr(usk);
167 
168 		/* Fall through and set IPv4 options too otherwise we don't get
169 		 * errors from IPv4 packets sent through the IPv6 socket.
170 		 */
171 		fallthrough;
172 	case AF_INET:
173 		/* we want to receive ICMP errors */
174 		ip_sock_set_recverr(usk);
175 
176 		/* we want to set the don't fragment bit */
177 		ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
178 
179 		/* We want receive timestamps. */
180 		sock_enable_timestamps(usk);
181 		break;
182 
183 	default:
184 		BUG();
185 	}
186 
187 	_leave(" = 0");
188 	return 0;
189 }
190 
191 /*
192  * Look up or create a new local endpoint using the specified local address.
193  */
194 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
195 				       const struct sockaddr_rxrpc *srx)
196 {
197 	struct rxrpc_local *local;
198 	struct rxrpc_net *rxnet = rxrpc_net(net);
199 	struct hlist_node *cursor;
200 	const char *age;
201 	long diff;
202 	int ret;
203 
204 	_enter("{%d,%d,%pISp}",
205 	       srx->transport_type, srx->transport.family, &srx->transport);
206 
207 	mutex_lock(&rxnet->local_mutex);
208 
209 	hlist_for_each(cursor, &rxnet->local_endpoints) {
210 		local = hlist_entry(cursor, struct rxrpc_local, link);
211 
212 		diff = rxrpc_local_cmp_key(local, srx);
213 		if (diff != 0)
214 			continue;
215 
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
219 		 * we want bound.
220 		 */
221 		if (srx->srx_service) {
222 			local = NULL;
223 			goto addr_in_use;
224 		}
225 
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.
230 		 */
231 		if (!rxrpc_use_local(local))
232 			break;
233 
234 		age = "old";
235 		goto found;
236 	}
237 
238 	local = rxrpc_alloc_local(rxnet, srx);
239 	if (!local)
240 		goto nomem;
241 
242 	ret = rxrpc_open_socket(local, net);
243 	if (ret < 0)
244 		goto sock_error;
245 
246 	if (cursor) {
247 		hlist_replace_rcu(cursor, &local->link);
248 		cursor->pprev = NULL;
249 	} else {
250 		hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
251 	}
252 	age = "new";
253 
254 found:
255 	mutex_unlock(&rxnet->local_mutex);
256 
257 	_net("LOCAL %s %d {%pISp}",
258 	     age, local->debug_id, &local->srx.transport);
259 
260 	_leave(" = %p", local);
261 	return local;
262 
263 nomem:
264 	ret = -ENOMEM;
265 sock_error:
266 	mutex_unlock(&rxnet->local_mutex);
267 	if (local)
268 		call_rcu(&local->rcu, rxrpc_local_rcu);
269 	_leave(" = %d", ret);
270 	return ERR_PTR(ret);
271 
272 addr_in_use:
273 	mutex_unlock(&rxnet->local_mutex);
274 	_leave(" = -EADDRINUSE");
275 	return ERR_PTR(-EADDRINUSE);
276 }
277 
278 /*
279  * Get a ref on a local endpoint.
280  */
281 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
282 {
283 	const void *here = __builtin_return_address(0);
284 	int r;
285 
286 	__refcount_inc(&local->ref, &r);
287 	trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
288 	return local;
289 }
290 
291 /*
292  * Get a ref on a local endpoint unless its usage has already reached 0.
293  */
294 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
295 {
296 	const void *here = __builtin_return_address(0);
297 	int r;
298 
299 	if (local) {
300 		if (__refcount_inc_not_zero(&local->ref, &r))
301 			trace_rxrpc_local(local->debug_id, rxrpc_local_got,
302 					  r + 1, here);
303 		else
304 			local = NULL;
305 	}
306 	return local;
307 }
308 
309 /*
310  * Queue a local endpoint and pass the caller's reference to the work item.
311  */
312 void rxrpc_queue_local(struct rxrpc_local *local)
313 {
314 	const void *here = __builtin_return_address(0);
315 	unsigned int debug_id = local->debug_id;
316 	int r = refcount_read(&local->ref);
317 
318 	if (rxrpc_queue_work(&local->processor))
319 		trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
320 	else
321 		rxrpc_put_local(local);
322 }
323 
324 /*
325  * Drop a ref on a local endpoint.
326  */
327 void rxrpc_put_local(struct rxrpc_local *local)
328 {
329 	const void *here = __builtin_return_address(0);
330 	unsigned int debug_id;
331 	bool dead;
332 	int r;
333 
334 	if (local) {
335 		debug_id = local->debug_id;
336 
337 		dead = __refcount_dec_and_test(&local->ref, &r);
338 		trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
339 
340 		if (dead)
341 			call_rcu(&local->rcu, rxrpc_local_rcu);
342 	}
343 }
344 
345 /*
346  * Start using a local endpoint.
347  */
348 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
349 {
350 	local = rxrpc_get_local_maybe(local);
351 	if (!local)
352 		return NULL;
353 
354 	if (!__rxrpc_use_local(local)) {
355 		rxrpc_put_local(local);
356 		return NULL;
357 	}
358 
359 	return local;
360 }
361 
362 /*
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.
365  */
366 void rxrpc_unuse_local(struct rxrpc_local *local)
367 {
368 	if (local) {
369 		if (__rxrpc_unuse_local(local)) {
370 			rxrpc_get_local(local);
371 			rxrpc_queue_local(local);
372 		}
373 	}
374 }
375 
376 /*
377  * Destroy a local endpoint's socket and then hand the record to RCU to dispose
378  * of.
379  *
380  * Closing the socket cannot be done from bottom half context or RCU callback
381  * context because it might sleep.
382  */
383 static void rxrpc_local_destroyer(struct rxrpc_local *local)
384 {
385 	struct socket *socket = local->socket;
386 	struct rxrpc_net *rxnet = local->rxnet;
387 
388 	_enter("%d", local->debug_id);
389 
390 	local->dead = true;
391 
392 	mutex_lock(&rxnet->local_mutex);
393 	hlist_del_init_rcu(&local->link);
394 	mutex_unlock(&rxnet->local_mutex);
395 
396 	rxrpc_clean_up_local_conns(local);
397 	rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
398 	ASSERT(!local->service);
399 
400 	if (socket) {
401 		local->socket = NULL;
402 		kernel_sock_shutdown(socket, SHUT_RDWR);
403 		socket->sk->sk_user_data = NULL;
404 		sock_release(socket);
405 	}
406 
407 	/* At this point, there should be no more packets coming in to the
408 	 * local endpoint.
409 	 */
410 	rxrpc_purge_queue(&local->reject_queue);
411 	rxrpc_purge_queue(&local->event_queue);
412 }
413 
414 /*
415  * Process events on an endpoint.  The work item carries a ref which
416  * we must release.
417  */
418 static void rxrpc_local_processor(struct work_struct *work)
419 {
420 	struct rxrpc_local *local =
421 		container_of(work, struct rxrpc_local, processor);
422 	bool again;
423 
424 	if (local->dead)
425 		return;
426 
427 	trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
428 			  refcount_read(&local->ref), NULL);
429 
430 	do {
431 		again = false;
432 		if (!__rxrpc_use_local(local)) {
433 			rxrpc_local_destroyer(local);
434 			break;
435 		}
436 
437 		if (!list_empty(&local->ack_tx_queue)) {
438 			rxrpc_transmit_ack_packets(local);
439 			again = true;
440 		}
441 
442 		if (!skb_queue_empty(&local->reject_queue)) {
443 			rxrpc_reject_packets(local);
444 			again = true;
445 		}
446 
447 		if (!skb_queue_empty(&local->event_queue)) {
448 			rxrpc_process_local_events(local);
449 			again = true;
450 		}
451 
452 		__rxrpc_unuse_local(local);
453 	} while (again);
454 
455 	rxrpc_put_local(local);
456 }
457 
458 /*
459  * Destroy a local endpoint after the RCU grace period expires.
460  */
461 static void rxrpc_local_rcu(struct rcu_head *rcu)
462 {
463 	struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
464 
465 	_enter("%d", local->debug_id);
466 
467 	ASSERT(!work_pending(&local->processor));
468 
469 	_net("DESTROY LOCAL %d", local->debug_id);
470 	kfree(local);
471 	_leave("");
472 }
473 
474 /*
475  * Verify the local endpoint list is empty by this point.
476  */
477 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
478 {
479 	struct rxrpc_local *local;
480 
481 	_enter("");
482 
483 	flush_workqueue(rxrpc_workqueue);
484 
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));
490 		}
491 		mutex_unlock(&rxnet->local_mutex);
492 		BUG();
493 	}
494 }
495