1 /* RxRPC virtual connection handler
3 * Copyright (C) 2007 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 <linux/crypto.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
24 * Time till a connection expires after last use (in seconds).
26 unsigned int rxrpc_connection_expiry = 10 * 60;
28 static void rxrpc_connection_reaper(struct work_struct *work);
30 LIST_HEAD(rxrpc_connections);
31 DEFINE_RWLOCK(rxrpc_connection_lock);
32 static DECLARE_DELAYED_WORK(rxrpc_connection_reap, rxrpc_connection_reaper);
35 * allocate a new client connection bundle
37 static struct rxrpc_conn_bundle *rxrpc_alloc_bundle(gfp_t gfp)
39 struct rxrpc_conn_bundle *bundle;
43 bundle = kzalloc(sizeof(struct rxrpc_conn_bundle), gfp);
45 INIT_LIST_HEAD(&bundle->unused_conns);
46 INIT_LIST_HEAD(&bundle->avail_conns);
47 INIT_LIST_HEAD(&bundle->busy_conns);
48 init_waitqueue_head(&bundle->chanwait);
49 atomic_set(&bundle->usage, 1);
52 _leave(" = %p", bundle);
57 * compare bundle parameters with what we're looking for
58 * - return -ve, 0 or +ve
61 int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle *bundle,
62 struct key *key, u16 service_id)
64 return (bundle->service_id - service_id) ?:
65 ((unsigned long)bundle->key - (unsigned long)key);
69 * get bundle of client connections that a client socket can make use of
71 struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *rx,
72 struct rxrpc_transport *trans,
77 struct rxrpc_conn_bundle *bundle, *candidate;
78 struct rb_node *p, *parent, **pp;
80 _enter("%p{%x},%x,%hx,",
81 rx, key_serial(key), trans->debug_id, service_id);
83 /* search the extant bundles first for one that matches the specified
85 spin_lock(&trans->client_lock);
87 p = trans->bundles.rb_node;
89 bundle = rb_entry(p, struct rxrpc_conn_bundle, node);
91 if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
93 else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
96 goto found_extant_bundle;
99 spin_unlock(&trans->client_lock);
101 /* not yet present - create a candidate for a new record and then
103 candidate = rxrpc_alloc_bundle(gfp);
105 _leave(" = -ENOMEM");
106 return ERR_PTR(-ENOMEM);
109 candidate->key = key_get(key);
110 candidate->service_id = service_id;
112 spin_lock(&trans->client_lock);
114 pp = &trans->bundles.rb_node;
118 bundle = rb_entry(parent, struct rxrpc_conn_bundle, node);
120 if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
121 pp = &(*pp)->rb_left;
122 else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
123 pp = &(*pp)->rb_right;
125 goto found_extant_second;
128 /* second search also failed; add the new bundle */
132 rb_link_node(&bundle->node, parent, pp);
133 rb_insert_color(&bundle->node, &trans->bundles);
134 spin_unlock(&trans->client_lock);
135 _net("BUNDLE new on trans %d", trans->debug_id);
136 _leave(" = %p [new]", bundle);
139 /* we found the bundle in the list immediately */
141 atomic_inc(&bundle->usage);
142 spin_unlock(&trans->client_lock);
143 _net("BUNDLE old on trans %d", trans->debug_id);
144 _leave(" = %p [extant %d]", bundle, atomic_read(&bundle->usage));
147 /* we found the bundle on the second time through the list */
149 atomic_inc(&bundle->usage);
150 spin_unlock(&trans->client_lock);
152 _net("BUNDLE old2 on trans %d", trans->debug_id);
153 _leave(" = %p [second %d]", bundle, atomic_read(&bundle->usage));
160 void rxrpc_put_bundle(struct rxrpc_transport *trans,
161 struct rxrpc_conn_bundle *bundle)
163 _enter("%p,%p{%d}",trans, bundle, atomic_read(&bundle->usage));
165 if (atomic_dec_and_lock(&bundle->usage, &trans->client_lock)) {
166 _debug("Destroy bundle");
167 rb_erase(&bundle->node, &trans->bundles);
168 spin_unlock(&trans->client_lock);
169 ASSERT(list_empty(&bundle->unused_conns));
170 ASSERT(list_empty(&bundle->avail_conns));
171 ASSERT(list_empty(&bundle->busy_conns));
172 ASSERTCMP(bundle->num_conns, ==, 0);
173 key_put(bundle->key);
181 * allocate a new connection
183 static struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
185 struct rxrpc_connection *conn;
189 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
191 INIT_WORK(&conn->processor, &rxrpc_process_connection);
192 INIT_LIST_HEAD(&conn->bundle_link);
193 conn->calls = RB_ROOT;
194 skb_queue_head_init(&conn->rx_queue);
195 conn->security = &rxrpc_no_security;
196 rwlock_init(&conn->lock);
197 spin_lock_init(&conn->state_lock);
198 atomic_set(&conn->usage, 1);
199 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
200 conn->avail_calls = RXRPC_MAXCALLS;
201 conn->size_align = 4;
202 conn->header_size = sizeof(struct rxrpc_wire_header);
205 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
210 * assign a connection ID to a connection and add it to the transport's
211 * connection lookup tree
212 * - called with transport client lock held
214 static void rxrpc_assign_connection_id(struct rxrpc_connection *conn)
216 struct rxrpc_connection *xconn;
217 struct rb_node *parent, **p;
225 write_lock_bh(&conn->trans->conn_lock);
227 conn->trans->conn_idcounter += RXRPC_CID_INC;
228 if (conn->trans->conn_idcounter < RXRPC_CID_INC)
229 conn->trans->conn_idcounter = RXRPC_CID_INC;
230 cid = conn->trans->conn_idcounter;
234 p = &conn->trans->client_conns.rb_node;
238 xconn = rb_entry(parent, struct rxrpc_connection, node);
240 if (epoch < xconn->epoch)
242 else if (epoch > xconn->epoch)
244 else if (cid < xconn->cid)
246 else if (cid > xconn->cid)
252 /* we've found a suitable hole - arrange for this connection to occupy
254 rb_link_node(&conn->node, parent, p);
255 rb_insert_color(&conn->node, &conn->trans->client_conns);
258 write_unlock_bh(&conn->trans->conn_lock);
259 _leave(" [CID %x]", cid);
262 /* we found a connection with the proposed ID - walk the tree from that
263 * point looking for the next unused ID */
266 cid += RXRPC_CID_INC;
267 if (cid < RXRPC_CID_INC) {
269 conn->trans->conn_idcounter = cid;
270 goto attempt_insertion;
273 parent = rb_next(parent);
275 goto attempt_insertion;
277 xconn = rb_entry(parent, struct rxrpc_connection, node);
278 if (epoch < xconn->epoch ||
280 goto attempt_insertion;
285 * add a call to a connection's call-by-ID tree
287 static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection *conn,
288 struct rxrpc_call *call)
290 struct rxrpc_call *xcall;
291 struct rb_node *parent, **p;
294 write_lock_bh(&conn->lock);
296 call_id = call->call_id;
297 p = &conn->calls.rb_node;
301 xcall = rb_entry(parent, struct rxrpc_call, conn_node);
303 if (call_id < xcall->call_id)
305 else if (call_id > xcall->call_id)
311 rb_link_node(&call->conn_node, parent, p);
312 rb_insert_color(&call->conn_node, &conn->calls);
314 write_unlock_bh(&conn->lock);
318 * connect a call on an exclusive connection
320 static int rxrpc_connect_exclusive(struct rxrpc_sock *rx,
321 struct rxrpc_transport *trans,
323 struct rxrpc_call *call,
326 struct rxrpc_connection *conn;
333 /* not yet present - create a candidate for a new connection
334 * and then redo the check */
335 conn = rxrpc_alloc_connection(gfp);
337 _leave(" = -ENOMEM");
343 conn->service_id = service_id;
344 conn->epoch = rxrpc_epoch;
345 conn->in_clientflag = 0;
346 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
348 conn->state = RXRPC_CONN_CLIENT;
349 conn->avail_calls = RXRPC_MAXCALLS - 1;
350 conn->security_level = rx->min_sec_level;
351 conn->key = key_get(rx->key);
353 ret = rxrpc_init_client_conn_security(conn);
357 _leave(" = %d [key]", ret);
361 write_lock_bh(&rxrpc_connection_lock);
362 list_add_tail(&conn->link, &rxrpc_connections);
363 write_unlock_bh(&rxrpc_connection_lock);
365 spin_lock(&trans->client_lock);
366 atomic_inc(&trans->usage);
368 _net("CONNECT EXCL new %d on TRANS %d",
369 conn->debug_id, conn->trans->debug_id);
371 rxrpc_assign_connection_id(conn);
374 spin_lock(&trans->client_lock);
377 /* we've got a connection with a free channel and we can now attach the
379 * - we're holding the transport's client lock
380 * - we're holding a reference on the connection
382 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
383 if (!conn->channels[chan])
385 goto no_free_channels;
388 atomic_inc(&conn->usage);
389 conn->channels[chan] = call;
391 call->channel = chan;
392 call->cid = conn->cid | chan;
393 call->call_id = ++conn->call_counter;
395 _net("CONNECT client on conn %d chan %d as call %x",
396 conn->debug_id, chan, call->call_id);
398 spin_unlock(&trans->client_lock);
400 rxrpc_add_call_ID_to_conn(conn, call);
405 spin_unlock(&trans->client_lock);
411 * find a connection for a call
412 * - called in process context with IRQs enabled
414 int rxrpc_connect_call(struct rxrpc_sock *rx,
415 struct rxrpc_transport *trans,
416 struct rxrpc_conn_bundle *bundle,
417 struct rxrpc_call *call,
420 struct rxrpc_connection *conn, *candidate;
423 DECLARE_WAITQUEUE(myself, current);
425 _enter("%p,%lx,", rx, call->user_call_ID);
427 if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags))
428 return rxrpc_connect_exclusive(rx, trans, bundle->service_id,
431 spin_lock(&trans->client_lock);
433 /* see if the bundle has a call slot available */
434 if (!list_empty(&bundle->avail_conns)) {
436 conn = list_entry(bundle->avail_conns.next,
437 struct rxrpc_connection,
439 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
440 list_del_init(&conn->bundle_link);
444 if (--conn->avail_calls == 0)
445 list_move(&conn->bundle_link,
446 &bundle->busy_conns);
447 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
448 ASSERT(conn->channels[0] == NULL ||
449 conn->channels[1] == NULL ||
450 conn->channels[2] == NULL ||
451 conn->channels[3] == NULL);
452 atomic_inc(&conn->usage);
456 if (!list_empty(&bundle->unused_conns)) {
458 conn = list_entry(bundle->unused_conns.next,
459 struct rxrpc_connection,
461 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
462 list_del_init(&conn->bundle_link);
466 ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
467 conn->avail_calls = RXRPC_MAXCALLS - 1;
468 ASSERT(conn->channels[0] == NULL &&
469 conn->channels[1] == NULL &&
470 conn->channels[2] == NULL &&
471 conn->channels[3] == NULL);
472 atomic_inc(&conn->usage);
473 list_move(&conn->bundle_link, &bundle->avail_conns);
477 /* need to allocate a new connection */
478 _debug("get new conn [%d]", bundle->num_conns);
480 spin_unlock(&trans->client_lock);
482 if (signal_pending(current))
485 if (bundle->num_conns >= 20) {
486 _debug("too many conns");
488 if (!gfpflags_allow_blocking(gfp)) {
489 _leave(" = -EAGAIN");
493 add_wait_queue(&bundle->chanwait, &myself);
495 set_current_state(TASK_INTERRUPTIBLE);
496 if (bundle->num_conns < 20 ||
497 !list_empty(&bundle->unused_conns) ||
498 !list_empty(&bundle->avail_conns))
500 if (signal_pending(current))
501 goto interrupted_dequeue;
504 remove_wait_queue(&bundle->chanwait, &myself);
505 __set_current_state(TASK_RUNNING);
506 spin_lock(&trans->client_lock);
510 /* not yet present - create a candidate for a new connection and then
512 candidate = rxrpc_alloc_connection(gfp);
514 _leave(" = -ENOMEM");
518 candidate->trans = trans;
519 candidate->bundle = bundle;
520 candidate->service_id = bundle->service_id;
521 candidate->epoch = rxrpc_epoch;
522 candidate->in_clientflag = 0;
523 candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
525 candidate->state = RXRPC_CONN_CLIENT;
526 candidate->avail_calls = RXRPC_MAXCALLS;
527 candidate->security_level = rx->min_sec_level;
528 candidate->key = key_get(bundle->key);
530 ret = rxrpc_init_client_conn_security(candidate);
532 key_put(candidate->key);
534 _leave(" = %d [key]", ret);
538 write_lock_bh(&rxrpc_connection_lock);
539 list_add_tail(&candidate->link, &rxrpc_connections);
540 write_unlock_bh(&rxrpc_connection_lock);
542 spin_lock(&trans->client_lock);
544 list_add(&candidate->bundle_link, &bundle->unused_conns);
546 atomic_inc(&bundle->usage);
547 atomic_inc(&trans->usage);
549 _net("CONNECT new %d on TRANS %d",
550 candidate->debug_id, candidate->trans->debug_id);
552 rxrpc_assign_connection_id(candidate);
553 candidate->security->prime_packet_security(candidate);
555 /* leave the candidate lurking in zombie mode attached to the
556 * bundle until we're ready for it */
557 rxrpc_put_connection(candidate);
561 /* we've got a connection with a free channel and we can now attach the
563 * - we're holding the transport's client lock
564 * - we're holding a reference on the connection
565 * - we're holding a reference on the bundle
567 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
568 if (!conn->channels[chan])
570 ASSERT(conn->channels[0] == NULL ||
571 conn->channels[1] == NULL ||
572 conn->channels[2] == NULL ||
573 conn->channels[3] == NULL);
577 conn->channels[chan] = call;
579 call->channel = chan;
580 call->cid = conn->cid | chan;
581 call->call_id = ++conn->call_counter;
583 _net("CONNECT client on conn %d chan %d as call %x",
584 conn->debug_id, chan, call->call_id);
586 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
587 spin_unlock(&trans->client_lock);
589 rxrpc_add_call_ID_to_conn(conn, call);
595 remove_wait_queue(&bundle->chanwait, &myself);
596 __set_current_state(TASK_RUNNING);
598 _leave(" = -ERESTARTSYS");
603 * get a record of an incoming connection
605 struct rxrpc_connection *
606 rxrpc_incoming_connection(struct rxrpc_transport *trans,
607 struct rxrpc_host_header *hdr)
609 struct rxrpc_connection *conn, *candidate = NULL;
610 struct rb_node *p, **pp;
611 const char *new = "old";
617 ASSERT(hdr->flags & RXRPC_CLIENT_INITIATED);
620 cid = hdr->cid & RXRPC_CIDMASK;
622 /* search the connection list first */
623 read_lock_bh(&trans->conn_lock);
625 p = trans->server_conns.rb_node;
627 conn = rb_entry(p, struct rxrpc_connection, node);
629 _debug("maybe %x", conn->cid);
631 if (epoch < conn->epoch)
633 else if (epoch > conn->epoch)
635 else if (cid < conn->cid)
637 else if (cid > conn->cid)
640 goto found_extant_connection;
642 read_unlock_bh(&trans->conn_lock);
644 /* not yet present - create a candidate for a new record and then
646 candidate = rxrpc_alloc_connection(GFP_NOIO);
648 _leave(" = -ENOMEM");
649 return ERR_PTR(-ENOMEM);
652 candidate->trans = trans;
653 candidate->epoch = hdr->epoch;
654 candidate->cid = hdr->cid & RXRPC_CIDMASK;
655 candidate->service_id = hdr->serviceId;
656 candidate->security_ix = hdr->securityIndex;
657 candidate->in_clientflag = RXRPC_CLIENT_INITIATED;
658 candidate->out_clientflag = 0;
659 candidate->state = RXRPC_CONN_SERVER;
660 if (candidate->service_id)
661 candidate->state = RXRPC_CONN_SERVER_UNSECURED;
663 write_lock_bh(&trans->conn_lock);
665 pp = &trans->server_conns.rb_node;
669 conn = rb_entry(p, struct rxrpc_connection, node);
671 if (epoch < conn->epoch)
672 pp = &(*pp)->rb_left;
673 else if (epoch > conn->epoch)
674 pp = &(*pp)->rb_right;
675 else if (cid < conn->cid)
676 pp = &(*pp)->rb_left;
677 else if (cid > conn->cid)
678 pp = &(*pp)->rb_right;
680 goto found_extant_second;
683 /* we can now add the new candidate to the list */
686 rb_link_node(&conn->node, p, pp);
687 rb_insert_color(&conn->node, &trans->server_conns);
688 atomic_inc(&conn->trans->usage);
690 write_unlock_bh(&trans->conn_lock);
692 write_lock_bh(&rxrpc_connection_lock);
693 list_add_tail(&conn->link, &rxrpc_connections);
694 write_unlock_bh(&rxrpc_connection_lock);
699 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->cid);
701 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
704 /* we found the connection in the list immediately */
705 found_extant_connection:
706 if (hdr->securityIndex != conn->security_ix) {
707 read_unlock_bh(&trans->conn_lock);
708 goto security_mismatch;
710 atomic_inc(&conn->usage);
711 read_unlock_bh(&trans->conn_lock);
714 /* we found the connection on the second time through the list */
716 if (hdr->securityIndex != conn->security_ix) {
717 write_unlock_bh(&trans->conn_lock);
718 goto security_mismatch;
720 atomic_inc(&conn->usage);
721 write_unlock_bh(&trans->conn_lock);
727 _leave(" = -EKEYREJECTED");
728 return ERR_PTR(-EKEYREJECTED);
732 * find a connection based on transport and RxRPC connection ID for an incoming
735 struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
736 struct rxrpc_host_header *hdr)
738 struct rxrpc_connection *conn;
742 _enter(",{%x,%x}", hdr->cid, hdr->flags);
744 read_lock_bh(&trans->conn_lock);
746 cid = hdr->cid & RXRPC_CIDMASK;
749 if (hdr->flags & RXRPC_CLIENT_INITIATED)
750 p = trans->server_conns.rb_node;
752 p = trans->client_conns.rb_node;
755 conn = rb_entry(p, struct rxrpc_connection, node);
757 _debug("maybe %x", conn->cid);
759 if (epoch < conn->epoch)
761 else if (epoch > conn->epoch)
763 else if (cid < conn->cid)
765 else if (cid > conn->cid)
771 read_unlock_bh(&trans->conn_lock);
776 atomic_inc(&conn->usage);
777 read_unlock_bh(&trans->conn_lock);
778 _leave(" = %p", conn);
783 * release a virtual connection
785 void rxrpc_put_connection(struct rxrpc_connection *conn)
787 _enter("%p{u=%d,d=%d}",
788 conn, atomic_read(&conn->usage), conn->debug_id);
790 ASSERTCMP(atomic_read(&conn->usage), >, 0);
792 conn->put_time = ktime_get_seconds();
793 if (atomic_dec_and_test(&conn->usage)) {
795 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
802 * destroy a virtual connection
804 static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
806 _enter("%p{%d}", conn, atomic_read(&conn->usage));
808 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
810 _net("DESTROY CONN %d", conn->debug_id);
813 rxrpc_put_bundle(conn->trans, conn->bundle);
815 ASSERT(RB_EMPTY_ROOT(&conn->calls));
816 rxrpc_purge_queue(&conn->rx_queue);
818 conn->security->clear(conn);
820 key_put(conn->server_key);
822 rxrpc_put_transport(conn->trans);
828 * reap dead connections
830 static void rxrpc_connection_reaper(struct work_struct *work)
832 struct rxrpc_connection *conn, *_p;
833 unsigned long now, earliest, reap_time;
835 LIST_HEAD(graveyard);
839 now = ktime_get_seconds();
840 earliest = ULONG_MAX;
842 write_lock_bh(&rxrpc_connection_lock);
843 list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
844 _debug("reap CONN %d { u=%d,t=%ld }",
845 conn->debug_id, atomic_read(&conn->usage),
846 (long) now - (long) conn->put_time);
848 if (likely(atomic_read(&conn->usage) > 0))
851 spin_lock(&conn->trans->client_lock);
852 write_lock(&conn->trans->conn_lock);
853 reap_time = conn->put_time + rxrpc_connection_expiry;
855 if (atomic_read(&conn->usage) > 0) {
857 } else if (reap_time <= now) {
858 list_move_tail(&conn->link, &graveyard);
859 if (conn->out_clientflag)
860 rb_erase(&conn->node,
861 &conn->trans->client_conns);
863 rb_erase(&conn->node,
864 &conn->trans->server_conns);
866 list_del_init(&conn->bundle_link);
867 conn->bundle->num_conns--;
870 } else if (reap_time < earliest) {
871 earliest = reap_time;
874 write_unlock(&conn->trans->conn_lock);
875 spin_unlock(&conn->trans->client_lock);
877 write_unlock_bh(&rxrpc_connection_lock);
879 if (earliest != ULONG_MAX) {
880 _debug("reschedule reaper %ld", (long) earliest - now);
881 ASSERTCMP(earliest, >, now);
882 rxrpc_queue_delayed_work(&rxrpc_connection_reap,
883 (earliest - now) * HZ);
886 /* then destroy all those pulled out */
887 while (!list_empty(&graveyard)) {
888 conn = list_entry(graveyard.next, struct rxrpc_connection,
890 list_del_init(&conn->link);
892 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
893 rxrpc_destroy_connection(conn);
900 * preemptively destroy all the connection records rather than waiting for them
903 void __exit rxrpc_destroy_all_connections(void)
907 rxrpc_connection_expiry = 0;
908 cancel_delayed_work(&rxrpc_connection_reap);
909 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);