1 /* RxRPC virtual connection handler, common bits.
3 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include "ar-internal.h"
21 * Time till a connection expires after last use (in seconds).
23 unsigned int rxrpc_connection_expiry = 10 * 60;
25 static void rxrpc_destroy_connection(struct rcu_head *);
28 * allocate a new connection
30 struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
32 struct rxrpc_connection *conn;
36 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
38 INIT_LIST_HEAD(&conn->cache_link);
39 spin_lock_init(&conn->channel_lock);
40 INIT_LIST_HEAD(&conn->waiting_calls);
41 INIT_WORK(&conn->processor, &rxrpc_process_connection);
42 INIT_LIST_HEAD(&conn->proc_link);
43 INIT_LIST_HEAD(&conn->link);
44 skb_queue_head_init(&conn->rx_queue);
45 conn->security = &rxrpc_no_security;
46 spin_lock_init(&conn->state_lock);
47 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
49 conn->idle_timestamp = jiffies;
52 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
57 * Look up a connection in the cache by protocol parameters.
59 * If successful, a pointer to the connection is returned, but no ref is taken.
60 * NULL is returned if there is no match.
62 * The caller must be holding the RCU read lock.
64 struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local,
67 struct rxrpc_connection *conn;
68 struct rxrpc_conn_proto k;
69 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
70 struct sockaddr_rxrpc srx;
71 struct rxrpc_peer *peer;
73 _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
75 if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
78 k.epoch = sp->hdr.epoch;
79 k.cid = sp->hdr.cid & RXRPC_CIDMASK;
81 /* We may have to handle mixing IPv4 and IPv6 */
82 if (srx.transport.family != local->srx.transport.family) {
83 pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n",
85 local->srx.transport.family);
89 k.epoch = sp->hdr.epoch;
90 k.cid = sp->hdr.cid & RXRPC_CIDMASK;
92 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) {
93 /* We need to look up service connections by the full protocol
94 * parameter set. We look up the peer first as an intermediate
95 * step and then the connection from the peer's tree.
97 peer = rxrpc_lookup_peer_rcu(local, &srx);
100 conn = rxrpc_find_service_conn_rcu(peer, skb);
101 if (!conn || atomic_read(&conn->usage) == 0)
103 _leave(" = %p", conn);
106 /* Look up client connections by connection ID alone as their
107 * IDs are unique for this machine.
109 conn = idr_find(&rxrpc_client_conn_ids,
110 sp->hdr.cid >> RXRPC_CIDSHIFT);
111 if (!conn || atomic_read(&conn->usage) == 0) {
116 if (conn->proto.epoch != k.epoch ||
117 conn->params.local != local)
120 peer = conn->params.peer;
121 switch (srx.transport.family) {
123 if (peer->srx.transport.sin.sin_port !=
124 srx.transport.sin.sin_port ||
125 peer->srx.transport.sin.sin_addr.s_addr !=
126 srx.transport.sin.sin_addr.s_addr)
129 #ifdef CONFIG_AF_RXRPC_IPV6
131 if (peer->srx.transport.sin6.sin6_port !=
132 srx.transport.sin6.sin6_port ||
133 memcmp(&peer->srx.transport.sin6.sin6_addr,
134 &srx.transport.sin6.sin6_addr,
135 sizeof(struct in6_addr)) != 0)
143 _leave(" = %p", conn);
153 * Disconnect a call and clear any channel it occupies when that call
154 * terminates. The caller must hold the channel_lock and must release the
155 * call's ref on the connection.
157 void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
158 struct rxrpc_call *call)
160 struct rxrpc_channel *chan =
161 &conn->channels[call->cid & RXRPC_CHANNELMASK];
163 _enter("%d,%x", conn->debug_id, call->cid);
165 if (rcu_access_pointer(chan->call) == call) {
166 /* Save the result of the call so that we can repeat it if necessary
167 * through the channel, whilst disposing of the actual call record.
169 trace_rxrpc_disconnect_call(call);
170 if (call->abort_code) {
171 chan->last_abort = call->abort_code;
172 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
174 chan->last_seq = call->rx_hard_ack;
175 chan->last_type = RXRPC_PACKET_TYPE_ACK;
177 /* Sync with rxrpc_conn_retransmit(). */
179 chan->last_call = chan->call_id;
180 chan->call_id = chan->call_counter;
182 rcu_assign_pointer(chan->call, NULL);
189 * Disconnect a call and clear any channel it occupies when that call
192 void rxrpc_disconnect_call(struct rxrpc_call *call)
194 struct rxrpc_connection *conn = call->conn;
196 spin_lock_bh(&conn->params.peer->lock);
197 hlist_del_init(&call->error_link);
198 spin_unlock_bh(&conn->params.peer->lock);
200 if (rxrpc_is_client_call(call))
201 return rxrpc_disconnect_client_call(call);
203 spin_lock(&conn->channel_lock);
204 __rxrpc_disconnect_call(conn, call);
205 spin_unlock(&conn->channel_lock);
208 conn->idle_timestamp = jiffies;
209 rxrpc_put_connection(conn);
213 * Kill off a connection.
215 void rxrpc_kill_connection(struct rxrpc_connection *conn)
217 struct rxrpc_net *rxnet = conn->params.local->rxnet;
219 ASSERT(!rcu_access_pointer(conn->channels[0].call) &&
220 !rcu_access_pointer(conn->channels[1].call) &&
221 !rcu_access_pointer(conn->channels[2].call) &&
222 !rcu_access_pointer(conn->channels[3].call));
223 ASSERT(list_empty(&conn->cache_link));
225 write_lock(&rxnet->conn_lock);
226 list_del_init(&conn->proc_link);
227 write_unlock(&rxnet->conn_lock);
229 /* Drain the Rx queue. Note that even though we've unpublished, an
230 * incoming packet could still be being added to our Rx queue, so we
231 * will need to drain it again in the RCU cleanup handler.
233 rxrpc_purge_queue(&conn->rx_queue);
235 /* Leave final destruction to RCU. The connection processor work item
236 * must carry a ref on the connection to prevent us getting here whilst
237 * it is queued or running.
239 call_rcu(&conn->rcu, rxrpc_destroy_connection);
243 * Queue a connection's work processor, getting a ref to pass to the work
246 bool rxrpc_queue_conn(struct rxrpc_connection *conn)
248 const void *here = __builtin_return_address(0);
249 int n = __atomic_add_unless(&conn->usage, 1, 0);
252 if (rxrpc_queue_work(&conn->processor))
253 trace_rxrpc_conn(conn, rxrpc_conn_queued, n + 1, here);
255 rxrpc_put_connection(conn);
260 * Note the re-emergence of a connection.
262 void rxrpc_see_connection(struct rxrpc_connection *conn)
264 const void *here = __builtin_return_address(0);
266 int n = atomic_read(&conn->usage);
268 trace_rxrpc_conn(conn, rxrpc_conn_seen, n, here);
273 * Get a ref on a connection.
275 void rxrpc_get_connection(struct rxrpc_connection *conn)
277 const void *here = __builtin_return_address(0);
278 int n = atomic_inc_return(&conn->usage);
280 trace_rxrpc_conn(conn, rxrpc_conn_got, n, here);
284 * Try to get a ref on a connection.
286 struct rxrpc_connection *
287 rxrpc_get_connection_maybe(struct rxrpc_connection *conn)
289 const void *here = __builtin_return_address(0);
292 int n = __atomic_add_unless(&conn->usage, 1, 0);
294 trace_rxrpc_conn(conn, rxrpc_conn_got, n + 1, here);
302 * Release a service connection
304 void rxrpc_put_service_conn(struct rxrpc_connection *conn)
306 struct rxrpc_net *rxnet;
307 const void *here = __builtin_return_address(0);
310 n = atomic_dec_return(&conn->usage);
311 trace_rxrpc_conn(conn, rxrpc_conn_put_service, n, here);
314 rxnet = conn->params.local->rxnet;
315 rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, 0);
320 * destroy a virtual connection
322 static void rxrpc_destroy_connection(struct rcu_head *rcu)
324 struct rxrpc_connection *conn =
325 container_of(rcu, struct rxrpc_connection, rcu);
327 _enter("{%d,u=%d}", conn->debug_id, atomic_read(&conn->usage));
329 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
331 _net("DESTROY CONN %d", conn->debug_id);
333 rxrpc_purge_queue(&conn->rx_queue);
335 conn->security->clear(conn);
336 key_put(conn->params.key);
337 key_put(conn->server_key);
338 rxrpc_put_peer(conn->params.peer);
339 rxrpc_put_local(conn->params.local);
346 * reap dead service connections
348 void rxrpc_service_connection_reaper(struct work_struct *work)
350 struct rxrpc_connection *conn, *_p;
351 struct rxrpc_net *rxnet =
352 container_of(to_delayed_work(work),
353 struct rxrpc_net, service_conn_reaper);
354 unsigned long reap_older_than, earliest, idle_timestamp, now;
356 LIST_HEAD(graveyard);
361 reap_older_than = now - rxrpc_connection_expiry * HZ;
362 earliest = ULONG_MAX;
364 write_lock(&rxnet->conn_lock);
365 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
366 ASSERTCMP(atomic_read(&conn->usage), >, 0);
367 if (likely(atomic_read(&conn->usage) > 1))
369 if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
372 idle_timestamp = READ_ONCE(conn->idle_timestamp);
373 _debug("reap CONN %d { u=%d,t=%ld }",
374 conn->debug_id, atomic_read(&conn->usage),
375 (long)reap_older_than - (long)idle_timestamp);
377 if (time_after(idle_timestamp, reap_older_than)) {
378 if (time_before(idle_timestamp, earliest))
379 earliest = idle_timestamp;
383 /* The usage count sits at 1 whilst the object is unused on the
384 * list; we reduce that to 0 to make the object unavailable.
386 if (atomic_cmpxchg(&conn->usage, 1, 0) != 1)
389 if (rxrpc_conn_is_client(conn))
392 rxrpc_unpublish_service_conn(conn);
394 list_move_tail(&conn->link, &graveyard);
396 write_unlock(&rxnet->conn_lock);
398 if (earliest != ULONG_MAX) {
399 _debug("reschedule reaper %ld", (long) earliest - now);
400 ASSERT(time_after(earliest, now));
401 rxrpc_queue_delayed_work(&rxnet->client_conn_reaper,
405 while (!list_empty(&graveyard)) {
406 conn = list_entry(graveyard.next, struct rxrpc_connection,
408 list_del_init(&conn->link);
410 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
411 rxrpc_kill_connection(conn);
418 * preemptively destroy all the service connection records rather than
419 * waiting for them to time out
421 void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
423 struct rxrpc_connection *conn, *_p;
428 rxrpc_destroy_all_client_connections(rxnet);
430 rxrpc_connection_expiry = 0;
431 cancel_delayed_work(&rxnet->client_conn_reaper);
432 rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, 0);
433 flush_workqueue(rxrpc_workqueue);
435 write_lock(&rxnet->conn_lock);
436 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
437 pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
438 conn, atomic_read(&conn->usage));
441 write_unlock(&rxnet->conn_lock);
444 ASSERT(list_empty(&rxnet->conn_proc_list));