1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC virtual connection handler, common bits.
4 * Copyright (C) 2007, 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/slab.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include "ar-internal.h"
17 * Time till a connection expires after last use (in seconds).
19 unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
20 unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
22 static void rxrpc_clean_up_connection(struct work_struct *work);
23 static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
24 unsigned long reap_at);
26 static void rxrpc_connection_timer(struct timer_list *timer)
28 struct rxrpc_connection *conn =
29 container_of(timer, struct rxrpc_connection, timer);
31 rxrpc_queue_conn(conn, rxrpc_conn_queue_timer);
35 * allocate a new connection
37 struct rxrpc_connection *rxrpc_alloc_connection(struct rxrpc_net *rxnet,
40 struct rxrpc_connection *conn;
44 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
46 INIT_LIST_HEAD(&conn->cache_link);
47 timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
48 INIT_WORK(&conn->processor, rxrpc_process_connection);
49 INIT_WORK(&conn->destructor, rxrpc_clean_up_connection);
50 INIT_LIST_HEAD(&conn->proc_link);
51 INIT_LIST_HEAD(&conn->link);
52 skb_queue_head_init(&conn->rx_queue);
54 conn->security = &rxrpc_no_security;
55 spin_lock_init(&conn->state_lock);
56 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
57 conn->idle_timestamp = jiffies;
60 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
65 * Look up a connection in the cache by protocol parameters.
67 * If successful, a pointer to the connection is returned, but no ref is taken.
68 * NULL is returned if there is no match.
70 * When searching for a service call, if we find a peer but no connection, we
71 * return that through *_peer in case we need to create a new service call.
73 * The caller must be holding the RCU read lock.
75 struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local,
77 struct rxrpc_peer **_peer)
79 struct rxrpc_connection *conn;
80 struct rxrpc_conn_proto k;
81 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
82 struct sockaddr_rxrpc srx;
83 struct rxrpc_peer *peer;
85 _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
87 if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
90 if (srx.transport.family != local->srx.transport.family &&
91 (srx.transport.family == AF_INET &&
92 local->srx.transport.family != AF_INET6)) {
93 pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
95 local->srx.transport.family);
99 k.epoch = sp->hdr.epoch;
100 k.cid = sp->hdr.cid & RXRPC_CIDMASK;
102 if (rxrpc_to_server(sp)) {
103 /* We need to look up service connections by the full protocol
104 * parameter set. We look up the peer first as an intermediate
105 * step and then the connection from the peer's tree.
107 peer = rxrpc_lookup_peer_rcu(local, &srx);
111 conn = rxrpc_find_service_conn_rcu(peer, skb);
112 if (!conn || refcount_read(&conn->ref) == 0)
114 _leave(" = %p", conn);
117 /* Look up client connections by connection ID alone as their
118 * IDs are unique for this machine.
120 conn = idr_find(&rxrpc_client_conn_ids,
121 sp->hdr.cid >> RXRPC_CIDSHIFT);
122 if (!conn || refcount_read(&conn->ref) == 0) {
127 if (conn->proto.epoch != k.epoch ||
128 conn->local != local)
132 switch (srx.transport.family) {
134 if (peer->srx.transport.sin.sin_port !=
135 srx.transport.sin.sin_port ||
136 peer->srx.transport.sin.sin_addr.s_addr !=
137 srx.transport.sin.sin_addr.s_addr)
140 #ifdef CONFIG_AF_RXRPC_IPV6
142 if (peer->srx.transport.sin6.sin6_port !=
143 srx.transport.sin6.sin6_port ||
144 memcmp(&peer->srx.transport.sin6.sin6_addr,
145 &srx.transport.sin6.sin6_addr,
146 sizeof(struct in6_addr)) != 0)
154 _leave(" = %p", conn);
164 * Disconnect a call and clear any channel it occupies when that call
165 * terminates. The caller must hold the channel_lock and must release the
166 * call's ref on the connection.
168 void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
169 struct rxrpc_call *call)
171 struct rxrpc_channel *chan =
172 &conn->channels[call->cid & RXRPC_CHANNELMASK];
174 _enter("%d,%x", conn->debug_id, call->cid);
176 if (rcu_access_pointer(chan->call) == call) {
177 /* Save the result of the call so that we can repeat it if necessary
178 * through the channel, whilst disposing of the actual call record.
180 trace_rxrpc_disconnect_call(call);
181 switch (call->completion) {
182 case RXRPC_CALL_SUCCEEDED:
183 chan->last_seq = call->rx_highest_seq;
184 chan->last_type = RXRPC_PACKET_TYPE_ACK;
186 case RXRPC_CALL_LOCALLY_ABORTED:
187 chan->last_abort = call->abort_code;
188 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
191 chan->last_abort = RX_CALL_DEAD;
192 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
196 /* Sync with rxrpc_conn_retransmit(). */
198 chan->last_call = chan->call_id;
199 chan->call_id = chan->call_counter;
201 rcu_assign_pointer(chan->call, NULL);
208 * Disconnect a call and clear any channel it occupies when that call
211 void rxrpc_disconnect_call(struct rxrpc_call *call)
213 struct rxrpc_connection *conn = call->conn;
215 call->peer->cong_ssthresh = call->cong_ssthresh;
217 if (!hlist_unhashed(&call->error_link)) {
218 spin_lock_bh(&call->peer->lock);
219 hlist_del_rcu(&call->error_link);
220 spin_unlock_bh(&call->peer->lock);
223 if (rxrpc_is_client_call(call))
224 return rxrpc_disconnect_client_call(conn->bundle, call);
226 spin_lock(&conn->bundle->channel_lock);
227 __rxrpc_disconnect_call(conn, call);
228 spin_unlock(&conn->bundle->channel_lock);
230 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
231 conn->idle_timestamp = jiffies;
232 if (atomic_dec_and_test(&conn->active))
233 rxrpc_set_service_reap_timer(conn->rxnet,
234 jiffies + rxrpc_connection_expiry);
238 * Queue a connection's work processor, getting a ref to pass to the work
241 void rxrpc_queue_conn(struct rxrpc_connection *conn, enum rxrpc_conn_trace why)
243 if (atomic_read(&conn->active) >= 0 &&
244 rxrpc_queue_work(&conn->processor))
245 rxrpc_see_connection(conn, why);
249 * Note the re-emergence of a connection.
251 void rxrpc_see_connection(struct rxrpc_connection *conn,
252 enum rxrpc_conn_trace why)
255 int r = refcount_read(&conn->ref);
257 trace_rxrpc_conn(conn->debug_id, r, why);
262 * Get a ref on a connection.
264 struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn,
265 enum rxrpc_conn_trace why)
269 __refcount_inc(&conn->ref, &r);
270 trace_rxrpc_conn(conn->debug_id, r + 1, why);
275 * Try to get a ref on a connection.
277 struct rxrpc_connection *
278 rxrpc_get_connection_maybe(struct rxrpc_connection *conn,
279 enum rxrpc_conn_trace why)
284 if (__refcount_inc_not_zero(&conn->ref, &r))
285 trace_rxrpc_conn(conn->debug_id, r + 1, why);
293 * Set the service connection reap timer.
295 static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
296 unsigned long reap_at)
299 timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
303 * destroy a virtual connection
305 static void rxrpc_rcu_free_connection(struct rcu_head *rcu)
307 struct rxrpc_connection *conn =
308 container_of(rcu, struct rxrpc_connection, rcu);
309 struct rxrpc_net *rxnet = conn->rxnet;
311 _enter("{%d,u=%d}", conn->debug_id, refcount_read(&conn->ref));
313 trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
317 if (atomic_dec_and_test(&rxnet->nr_conns))
318 wake_up_var(&rxnet->nr_conns);
322 * Clean up a dead connection.
324 static void rxrpc_clean_up_connection(struct work_struct *work)
326 struct rxrpc_connection *conn =
327 container_of(work, struct rxrpc_connection, destructor);
328 struct rxrpc_net *rxnet = conn->rxnet;
330 ASSERT(!rcu_access_pointer(conn->channels[0].call) &&
331 !rcu_access_pointer(conn->channels[1].call) &&
332 !rcu_access_pointer(conn->channels[2].call) &&
333 !rcu_access_pointer(conn->channels[3].call));
334 ASSERT(list_empty(&conn->cache_link));
336 del_timer_sync(&conn->timer);
337 cancel_work_sync(&conn->processor); /* Processing may restart the timer */
338 del_timer_sync(&conn->timer);
340 write_lock(&rxnet->conn_lock);
341 list_del_init(&conn->proc_link);
342 write_unlock(&rxnet->conn_lock);
344 rxrpc_purge_queue(&conn->rx_queue);
346 rxrpc_kill_client_conn(conn);
348 conn->security->clear(conn);
350 rxrpc_put_bundle(conn->bundle, rxrpc_bundle_put_conn);
351 rxrpc_put_peer(conn->peer, rxrpc_peer_put_conn);
352 rxrpc_put_local(conn->local, rxrpc_local_put_kill_conn);
354 /* Drain the Rx queue. Note that even though we've unpublished, an
355 * incoming packet could still be being added to our Rx queue, so we
356 * will need to drain it again in the RCU cleanup handler.
358 rxrpc_purge_queue(&conn->rx_queue);
360 call_rcu(&conn->rcu, rxrpc_rcu_free_connection);
364 * Drop a ref on a connection.
366 void rxrpc_put_connection(struct rxrpc_connection *conn,
367 enum rxrpc_conn_trace why)
369 unsigned int debug_id;
376 debug_id = conn->debug_id;
377 dead = __refcount_dec_and_test(&conn->ref, &r);
378 trace_rxrpc_conn(debug_id, r - 1, why);
380 del_timer(&conn->timer);
381 cancel_work(&conn->processor);
383 if (in_softirq() || work_busy(&conn->processor) ||
384 timer_pending(&conn->timer))
385 /* Can't use the rxrpc workqueue as we need to cancel/flush
386 * something that may be running/waiting there.
388 schedule_work(&conn->destructor);
390 rxrpc_clean_up_connection(&conn->destructor);
395 * reap dead service connections
397 void rxrpc_service_connection_reaper(struct work_struct *work)
399 struct rxrpc_connection *conn, *_p;
400 struct rxrpc_net *rxnet =
401 container_of(work, struct rxrpc_net, service_conn_reaper);
402 unsigned long expire_at, earliest, idle_timestamp, now;
405 LIST_HEAD(graveyard);
410 earliest = now + MAX_JIFFY_OFFSET;
412 write_lock(&rxnet->conn_lock);
413 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
414 ASSERTCMP(atomic_read(&conn->active), >=, 0);
415 if (likely(atomic_read(&conn->active) > 0))
417 if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
420 if (rxnet->live && !conn->local->dead) {
421 idle_timestamp = READ_ONCE(conn->idle_timestamp);
422 expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
423 if (conn->local->service_closed)
424 expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
426 _debug("reap CONN %d { a=%d,t=%ld }",
427 conn->debug_id, atomic_read(&conn->active),
428 (long)expire_at - (long)now);
430 if (time_before(now, expire_at)) {
431 if (time_before(expire_at, earliest))
432 earliest = expire_at;
437 /* The activity count sits at 0 whilst the conn is unused on
438 * the list; we reduce that to -1 to make the conn unavailable.
441 if (!atomic_try_cmpxchg(&conn->active, &active, -1))
443 rxrpc_see_connection(conn, rxrpc_conn_see_reap_service);
445 if (rxrpc_conn_is_client(conn))
448 rxrpc_unpublish_service_conn(conn);
450 list_move_tail(&conn->link, &graveyard);
452 write_unlock(&rxnet->conn_lock);
454 if (earliest != now + MAX_JIFFY_OFFSET) {
455 _debug("reschedule reaper %ld", (long)earliest - (long)now);
456 ASSERT(time_after(earliest, now));
457 rxrpc_set_service_reap_timer(rxnet, earliest);
460 while (!list_empty(&graveyard)) {
461 conn = list_entry(graveyard.next, struct rxrpc_connection,
463 list_del_init(&conn->link);
465 ASSERTCMP(atomic_read(&conn->active), ==, -1);
466 rxrpc_put_connection(conn, rxrpc_conn_put_service_reaped);
473 * preemptively destroy all the service connection records rather than
474 * waiting for them to time out
476 void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
478 struct rxrpc_connection *conn, *_p;
483 atomic_dec(&rxnet->nr_conns);
484 rxrpc_destroy_all_client_connections(rxnet);
486 del_timer_sync(&rxnet->service_conn_reap_timer);
487 rxrpc_queue_work(&rxnet->service_conn_reaper);
488 flush_workqueue(rxrpc_workqueue);
490 write_lock(&rxnet->conn_lock);
491 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
492 pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
493 conn, refcount_read(&conn->ref));
496 write_unlock(&rxnet->conn_lock);
499 ASSERT(list_empty(&rxnet->conn_proc_list));
501 /* We need to wait for the connections to be destroyed by RCU as they
502 * pin things that we still need to get rid of.
504 wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));