1// SPDX-License-Identifier: GPL-2.0-or-later
2/* RxRPC virtual connection handler, common bits.
3 *
4 * Copyright (C) 2007, 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/slab.h>
12#include <linux/net.h>
13#include <linux/skbuff.h>
14#include "ar-internal.h"
15
16/*
17 * Time till a connection expires after last use (in seconds).
18 */
19unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
20unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
21
22static void rxrpc_destroy_connection(struct rcu_head *);
23
24static void rxrpc_connection_timer(struct timer_list *timer)
25{
26	struct rxrpc_connection *conn =
27		container_of(timer, struct rxrpc_connection, timer);
28
29	rxrpc_queue_conn(conn);
30}
31
32/*
33 * allocate a new connection
34 */
35struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
36{
37	struct rxrpc_connection *conn;
38
39	_enter("");
40
41	conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
42	if (conn) {
43		INIT_LIST_HEAD(&conn->cache_link);
44		timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
45		INIT_WORK(&conn->processor, &rxrpc_process_connection);
46		INIT_LIST_HEAD(&conn->proc_link);
47		INIT_LIST_HEAD(&conn->link);
48		skb_queue_head_init(&conn->rx_queue);
49		conn->security = &rxrpc_no_security;
50		spin_lock_init(&conn->state_lock);
51		conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
52		conn->size_align = 4;
53		conn->idle_timestamp = jiffies;
54	}
55
56	_leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
57	return conn;
58}
59
60/*
61 * Look up a connection in the cache by protocol parameters.
62 *
63 * If successful, a pointer to the connection is returned, but no ref is taken.
64 * NULL is returned if there is no match.
65 *
66 * When searching for a service call, if we find a peer but no connection, we
67 * return that through *_peer in case we need to create a new service call.
68 *
69 * The caller must be holding the RCU read lock.
70 */
71struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local,
72						   struct sk_buff *skb,
73						   struct rxrpc_peer **_peer)
74{
75	struct rxrpc_connection *conn;
76	struct rxrpc_conn_proto k;
77	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
78	struct sockaddr_rxrpc srx;
79	struct rxrpc_peer *peer;
80
81	_enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
82
83	if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
84		goto not_found;
85
86	if (srx.transport.family != local->srx.transport.family &&
87	    (srx.transport.family == AF_INET &&
88	     local->srx.transport.family != AF_INET6)) {
89		pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
90				    srx.transport.family,
91				    local->srx.transport.family);
92		goto not_found;
93	}
94
95	k.epoch	= sp->hdr.epoch;
96	k.cid	= sp->hdr.cid & RXRPC_CIDMASK;
97
98	if (rxrpc_to_server(sp)) {
99		/* We need to look up service connections by the full protocol
100		 * parameter set.  We look up the peer first as an intermediate
101		 * step and then the connection from the peer's tree.
102		 */
103		peer = rxrpc_lookup_peer_rcu(local, &srx);
104		if (!peer)
105			goto not_found;
106		*_peer = peer;
107		conn = rxrpc_find_service_conn_rcu(peer, skb);
108		if (!conn || refcount_read(&conn->ref) == 0)
109			goto not_found;
110		_leave(" = %p", conn);
111		return conn;
112	} else {
113		/* Look up client connections by connection ID alone as their
114		 * IDs are unique for this machine.
115		 */
116		conn = idr_find(&rxrpc_client_conn_ids,
117				sp->hdr.cid >> RXRPC_CIDSHIFT);
118		if (!conn || refcount_read(&conn->ref) == 0) {
119			_debug("no conn");
120			goto not_found;
121		}
122
123		if (conn->proto.epoch != k.epoch ||
124		    conn->params.local != local)
125			goto not_found;
126
127		peer = conn->params.peer;
128		switch (srx.transport.family) {
129		case AF_INET:
130			if (peer->srx.transport.sin.sin_port !=
131			    srx.transport.sin.sin_port ||
132			    peer->srx.transport.sin.sin_addr.s_addr !=
133			    srx.transport.sin.sin_addr.s_addr)
134				goto not_found;
135			break;
136#ifdef CONFIG_AF_RXRPC_IPV6
137		case AF_INET6:
138			if (peer->srx.transport.sin6.sin6_port !=
139			    srx.transport.sin6.sin6_port ||
140			    memcmp(&peer->srx.transport.sin6.sin6_addr,
141				   &srx.transport.sin6.sin6_addr,
142				   sizeof(struct in6_addr)) != 0)
143				goto not_found;
144			break;
145#endif
146		default:
147			BUG();
148		}
149
150		_leave(" = %p", conn);
151		return conn;
152	}
153
154not_found:
155	_leave(" = NULL");
156	return NULL;
157}
158
159/*
160 * Disconnect a call and clear any channel it occupies when that call
161 * terminates.  The caller must hold the channel_lock and must release the
162 * call's ref on the connection.
163 */
164void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
165			     struct rxrpc_call *call)
166{
167	struct rxrpc_channel *chan =
168		&conn->channels[call->cid & RXRPC_CHANNELMASK];
169
170	_enter("%d,%x", conn->debug_id, call->cid);
171
172	if (rcu_access_pointer(chan->call) == call) {
173		/* Save the result of the call so that we can repeat it if necessary
174		 * through the channel, whilst disposing of the actual call record.
175		 */
176		trace_rxrpc_disconnect_call(call);
177		switch (call->completion) {
178		case RXRPC_CALL_SUCCEEDED:
179			chan->last_seq = call->rx_hard_ack;
180			chan->last_type = RXRPC_PACKET_TYPE_ACK;
181			break;
182		case RXRPC_CALL_LOCALLY_ABORTED:
183			chan->last_abort = call->abort_code;
184			chan->last_type = RXRPC_PACKET_TYPE_ABORT;
185			break;
186		default:
187			chan->last_abort = RX_CALL_DEAD;
188			chan->last_type = RXRPC_PACKET_TYPE_ABORT;
189			break;
190		}
191
192		/* Sync with rxrpc_conn_retransmit(). */
193		smp_wmb();
194		chan->last_call = chan->call_id;
195		chan->call_id = chan->call_counter;
196
197		rcu_assign_pointer(chan->call, NULL);
198	}
199
200	_leave("");
201}
202
203/*
204 * Disconnect a call and clear any channel it occupies when that call
205 * terminates.
206 */
207void rxrpc_disconnect_call(struct rxrpc_call *call)
208{
209	struct rxrpc_connection *conn = call->conn;
210
211	call->peer->cong_cwnd = call->cong_cwnd;
212
213	if (!hlist_unhashed(&call->error_link)) {
214		spin_lock_bh(&call->peer->lock);
215		hlist_del_rcu(&call->error_link);
216		spin_unlock_bh(&call->peer->lock);
217	}
218
219	if (rxrpc_is_client_call(call))
220		return rxrpc_disconnect_client_call(conn->bundle, call);
221
222	spin_lock(&conn->bundle->channel_lock);
223	__rxrpc_disconnect_call(conn, call);
224	spin_unlock(&conn->bundle->channel_lock);
225
226	set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
227	conn->idle_timestamp = jiffies;
228}
229
230/*
231 * Kill off a connection.
232 */
233void rxrpc_kill_connection(struct rxrpc_connection *conn)
234{
235	struct rxrpc_net *rxnet = conn->params.local->rxnet;
236
237	ASSERT(!rcu_access_pointer(conn->channels[0].call) &&
238	       !rcu_access_pointer(conn->channels[1].call) &&
239	       !rcu_access_pointer(conn->channels[2].call) &&
240	       !rcu_access_pointer(conn->channels[3].call));
241	ASSERT(list_empty(&conn->cache_link));
242
243	write_lock(&rxnet->conn_lock);
244	list_del_init(&conn->proc_link);
245	write_unlock(&rxnet->conn_lock);
246
247	/* Drain the Rx queue.  Note that even though we've unpublished, an
248	 * incoming packet could still be being added to our Rx queue, so we
249	 * will need to drain it again in the RCU cleanup handler.
250	 */
251	rxrpc_purge_queue(&conn->rx_queue);
252
253	/* Leave final destruction to RCU.  The connection processor work item
254	 * must carry a ref on the connection to prevent us getting here whilst
255	 * it is queued or running.
256	 */
257	call_rcu(&conn->rcu, rxrpc_destroy_connection);
258}
259
260/*
261 * Queue a connection's work processor, getting a ref to pass to the work
262 * queue.
263 */
264bool rxrpc_queue_conn(struct rxrpc_connection *conn)
265{
266	const void *here = __builtin_return_address(0);
267	int r;
268
269	if (!__refcount_inc_not_zero(&conn->ref, &r))
270		return false;
271	if (rxrpc_queue_work(&conn->processor))
272		trace_rxrpc_conn(conn->debug_id, rxrpc_conn_queued, r + 1, here);
273	else
274		rxrpc_put_connection(conn);
275	return true;
276}
277
278/*
279 * Note the re-emergence of a connection.
280 */
281void rxrpc_see_connection(struct rxrpc_connection *conn)
282{
283	const void *here = __builtin_return_address(0);
284	if (conn) {
285		int n = refcount_read(&conn->ref);
286
287		trace_rxrpc_conn(conn->debug_id, rxrpc_conn_seen, n, here);
288	}
289}
290
291/*
292 * Get a ref on a connection.
293 */
294struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn)
295{
296	const void *here = __builtin_return_address(0);
297	int r;
298
299	__refcount_inc(&conn->ref, &r);
300	trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, r, here);
301	return conn;
302}
303
304/*
305 * Try to get a ref on a connection.
306 */
307struct rxrpc_connection *
308rxrpc_get_connection_maybe(struct rxrpc_connection *conn)
309{
310	const void *here = __builtin_return_address(0);
311	int r;
312
313	if (conn) {
314		if (__refcount_inc_not_zero(&conn->ref, &r))
315			trace_rxrpc_conn(conn->debug_id, rxrpc_conn_got, r + 1, here);
316		else
317			conn = NULL;
318	}
319	return conn;
320}
321
322/*
323 * Set the service connection reap timer.
324 */
325static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
326					 unsigned long reap_at)
327{
328	if (rxnet->live)
329		timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
330}
331
332/*
333 * Release a service connection
334 */
335void rxrpc_put_service_conn(struct rxrpc_connection *conn)
336{
337	const void *here = __builtin_return_address(0);
338	unsigned int debug_id = conn->debug_id;
339	int r;
340
341	__refcount_dec(&conn->ref, &r);
342	trace_rxrpc_conn(debug_id, rxrpc_conn_put_service, r - 1, here);
343	if (r - 1 == 1)
344		rxrpc_set_service_reap_timer(conn->params.local->rxnet,
345					     jiffies + rxrpc_connection_expiry);
346}
347
348/*
349 * destroy a virtual connection
350 */
351static void rxrpc_destroy_connection(struct rcu_head *rcu)
352{
353	struct rxrpc_connection *conn =
354		container_of(rcu, struct rxrpc_connection, rcu);
355
356	_enter("{%d,u=%d}", conn->debug_id, refcount_read(&conn->ref));
357
358	ASSERTCMP(refcount_read(&conn->ref), ==, 0);
359
360	_net("DESTROY CONN %d", conn->debug_id);
361
362	del_timer_sync(&conn->timer);
363	rxrpc_purge_queue(&conn->rx_queue);
364
365	conn->security->clear(conn);
366	key_put(conn->params.key);
367	key_put(conn->server_key);
368	rxrpc_put_bundle(conn->bundle);
369	rxrpc_put_peer(conn->params.peer);
370
371	if (atomic_dec_and_test(&conn->params.local->rxnet->nr_conns))
372		wake_up_var(&conn->params.local->rxnet->nr_conns);
373	rxrpc_put_local(conn->params.local);
374
375	kfree(conn);
376	_leave("");
377}
378
379/*
380 * reap dead service connections
381 */
382void rxrpc_service_connection_reaper(struct work_struct *work)
383{
384	struct rxrpc_connection *conn, *_p;
385	struct rxrpc_net *rxnet =
386		container_of(work, struct rxrpc_net, service_conn_reaper);
387	unsigned long expire_at, earliest, idle_timestamp, now;
388
389	LIST_HEAD(graveyard);
390
391	_enter("");
392
393	now = jiffies;
394	earliest = now + MAX_JIFFY_OFFSET;
395
396	write_lock(&rxnet->conn_lock);
397	list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
398		ASSERTCMP(refcount_read(&conn->ref), >, 0);
399		if (likely(refcount_read(&conn->ref) > 1))
400			continue;
401		if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
402			continue;
403
404		if (rxnet->live && !conn->params.local->dead) {
405			idle_timestamp = READ_ONCE(conn->idle_timestamp);
406			expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
407			if (conn->params.local->service_closed)
408				expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
409
410			_debug("reap CONN %d { u=%d,t=%ld }",
411			       conn->debug_id, refcount_read(&conn->ref),
412			       (long)expire_at - (long)now);
413
414			if (time_before(now, expire_at)) {
415				if (time_before(expire_at, earliest))
416					earliest = expire_at;
417				continue;
418			}
419		}
420
421		/* The usage count sits at 1 whilst the object is unused on the
422		 * list; we reduce that to 0 to make the object unavailable.
423		 */
424		if (!refcount_dec_if_one(&conn->ref))
425			continue;
426		trace_rxrpc_conn(conn->debug_id, rxrpc_conn_reap_service, 0, NULL);
427
428		if (rxrpc_conn_is_client(conn))
429			BUG();
430		else
431			rxrpc_unpublish_service_conn(conn);
432
433		list_move_tail(&conn->link, &graveyard);
434	}
435	write_unlock(&rxnet->conn_lock);
436
437	if (earliest != now + MAX_JIFFY_OFFSET) {
438		_debug("reschedule reaper %ld", (long)earliest - (long)now);
439		ASSERT(time_after(earliest, now));
440		rxrpc_set_service_reap_timer(rxnet, earliest);
441	}
442
443	while (!list_empty(&graveyard)) {
444		conn = list_entry(graveyard.next, struct rxrpc_connection,
445				  link);
446		list_del_init(&conn->link);
447
448		ASSERTCMP(refcount_read(&conn->ref), ==, 0);
449		rxrpc_kill_connection(conn);
450	}
451
452	_leave("");
453}
454
455/*
456 * preemptively destroy all the service connection records rather than
457 * waiting for them to time out
458 */
459void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
460{
461	struct rxrpc_connection *conn, *_p;
462	bool leak = false;
463
464	_enter("");
465
466	atomic_dec(&rxnet->nr_conns);
467	rxrpc_destroy_all_client_connections(rxnet);
468
469	del_timer_sync(&rxnet->service_conn_reap_timer);
470	rxrpc_queue_work(&rxnet->service_conn_reaper);
471	flush_workqueue(rxrpc_workqueue);
472
473	write_lock(&rxnet->conn_lock);
474	list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
475		pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
476		       conn, refcount_read(&conn->ref));
477		leak = true;
478	}
479	write_unlock(&rxnet->conn_lock);
480	BUG_ON(leak);
481
482	ASSERT(list_empty(&rxnet->conn_proc_list));
483
484	/* We need to wait for the connections to be destroyed by RCU as they
485	 * pin things that we still need to get rid of.
486	 */
487	wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));
488	_leave("");
489}
490