blob: c6787b6f459f1ff250cbc010572f728af58961a0 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC virtual connection handler
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070016#include <linux/net.h>
17#include <linux/skbuff.h>
18#include <linux/crypto.h>
19#include <net/sock.h>
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
David Howells5873c082014-02-07 18:58:44 +000023/*
24 * Time till a connection expires after last use (in seconds).
25 */
David Howellsdad8aff2016-03-09 23:22:56 +000026unsigned int rxrpc_connection_expiry = 10 * 60;
David Howells5873c082014-02-07 18:58:44 +000027
David Howells17926a72007-04-26 15:48:28 -070028static void rxrpc_connection_reaper(struct work_struct *work);
29
30LIST_HEAD(rxrpc_connections);
31DEFINE_RWLOCK(rxrpc_connection_lock);
David Howells17926a72007-04-26 15:48:28 -070032static DECLARE_DELAYED_WORK(rxrpc_connection_reap, rxrpc_connection_reaper);
33
34/*
35 * allocate a new client connection bundle
36 */
37static struct rxrpc_conn_bundle *rxrpc_alloc_bundle(gfp_t gfp)
38{
39 struct rxrpc_conn_bundle *bundle;
40
41 _enter("");
42
43 bundle = kzalloc(sizeof(struct rxrpc_conn_bundle), gfp);
44 if (bundle) {
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);
50 }
51
52 _leave(" = %p", bundle);
53 return bundle;
54}
55
56/*
57 * compare bundle parameters with what we're looking for
58 * - return -ve, 0 or +ve
59 */
60static inline
61int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle *bundle,
David Howells0d12f8a2016-03-04 15:53:46 +000062 struct key *key, u16 service_id)
David Howells17926a72007-04-26 15:48:28 -070063{
64 return (bundle->service_id - service_id) ?:
David Howells0d12f8a2016-03-04 15:53:46 +000065 ((unsigned long)bundle->key - (unsigned long)key);
David Howells17926a72007-04-26 15:48:28 -070066}
67
68/*
69 * get bundle of client connections that a client socket can make use of
70 */
71struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *rx,
72 struct rxrpc_transport *trans,
73 struct key *key,
David Howells0d12f8a2016-03-04 15:53:46 +000074 u16 service_id,
David Howells17926a72007-04-26 15:48:28 -070075 gfp_t gfp)
76{
77 struct rxrpc_conn_bundle *bundle, *candidate;
78 struct rb_node *p, *parent, **pp;
79
80 _enter("%p{%x},%x,%hx,",
David Howells0d12f8a2016-03-04 15:53:46 +000081 rx, key_serial(key), trans->debug_id, service_id);
David Howells17926a72007-04-26 15:48:28 -070082
David Howells17926a72007-04-26 15:48:28 -070083 /* search the extant bundles first for one that matches the specified
84 * user ID */
85 spin_lock(&trans->client_lock);
86
87 p = trans->bundles.rb_node;
88 while (p) {
89 bundle = rb_entry(p, struct rxrpc_conn_bundle, node);
90
91 if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
92 p = p->rb_left;
93 else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
94 p = p->rb_right;
95 else
96 goto found_extant_bundle;
97 }
98
99 spin_unlock(&trans->client_lock);
100
101 /* not yet present - create a candidate for a new record and then
102 * redo the search */
103 candidate = rxrpc_alloc_bundle(gfp);
104 if (!candidate) {
105 _leave(" = -ENOMEM");
106 return ERR_PTR(-ENOMEM);
107 }
108
109 candidate->key = key_get(key);
110 candidate->service_id = service_id;
111
112 spin_lock(&trans->client_lock);
113
114 pp = &trans->bundles.rb_node;
115 parent = NULL;
116 while (*pp) {
117 parent = *pp;
118 bundle = rb_entry(parent, struct rxrpc_conn_bundle, node);
119
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;
124 else
125 goto found_extant_second;
126 }
127
128 /* second search also failed; add the new bundle */
129 bundle = candidate;
130 candidate = NULL;
131
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);
David Howells17926a72007-04-26 15:48:28 -0700136 _leave(" = %p [new]", bundle);
137 return bundle;
138
139 /* we found the bundle in the list immediately */
140found_extant_bundle:
141 atomic_inc(&bundle->usage);
142 spin_unlock(&trans->client_lock);
143 _net("BUNDLE old on trans %d", trans->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700144 _leave(" = %p [extant %d]", bundle, atomic_read(&bundle->usage));
145 return bundle;
146
147 /* we found the bundle on the second time through the list */
148found_extant_second:
149 atomic_inc(&bundle->usage);
150 spin_unlock(&trans->client_lock);
151 kfree(candidate);
152 _net("BUNDLE old2 on trans %d", trans->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700153 _leave(" = %p [second %d]", bundle, atomic_read(&bundle->usage));
154 return bundle;
155}
156
157/*
158 * release a bundle
159 */
160void rxrpc_put_bundle(struct rxrpc_transport *trans,
161 struct rxrpc_conn_bundle *bundle)
162{
163 _enter("%p,%p{%d}",trans, bundle, atomic_read(&bundle->usage));
164
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);
174 kfree(bundle);
175 }
176
177 _leave("");
178}
179
180/*
181 * allocate a new connection
182 */
183static struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
184{
185 struct rxrpc_connection *conn;
186
187 _enter("");
188
189 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
190 if (conn) {
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);
David Howellse0e4d822016-04-07 17:23:58 +0100195 conn->security = &rxrpc_no_security;
David Howells17926a72007-04-26 15:48:28 -0700196 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;
David Howells0d12f8a2016-03-04 15:53:46 +0000202 conn->header_size = sizeof(struct rxrpc_wire_header);
David Howells17926a72007-04-26 15:48:28 -0700203 }
204
Adrian Bunk16c61ad2007-06-15 15:15:43 -0700205 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
David Howells17926a72007-04-26 15:48:28 -0700206 return conn;
207}
208
209/*
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
213 */
214static void rxrpc_assign_connection_id(struct rxrpc_connection *conn)
215{
216 struct rxrpc_connection *xconn;
217 struct rb_node *parent, **p;
218 __be32 epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000219 u32 cid;
David Howells17926a72007-04-26 15:48:28 -0700220
221 _enter("");
222
David Howells19ffa012016-04-04 14:00:36 +0100223 epoch = conn->proto.epoch;
David Howells17926a72007-04-26 15:48:28 -0700224
225 write_lock_bh(&conn->trans->conn_lock);
226
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;
David Howells0d12f8a2016-03-04 15:53:46 +0000230 cid = conn->trans->conn_idcounter;
David Howells17926a72007-04-26 15:48:28 -0700231
232attempt_insertion:
233 parent = NULL;
234 p = &conn->trans->client_conns.rb_node;
235
236 while (*p) {
237 parent = *p;
238 xconn = rb_entry(parent, struct rxrpc_connection, node);
239
David Howells19ffa012016-04-04 14:00:36 +0100240 if (epoch < xconn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700241 p = &(*p)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100242 else if (epoch > xconn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700243 p = &(*p)->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100244 else if (cid < xconn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700245 p = &(*p)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100246 else if (cid > xconn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700247 p = &(*p)->rb_right;
248 else
249 goto id_exists;
250 }
251
252 /* we've found a suitable hole - arrange for this connection to occupy
253 * it */
254 rb_link_node(&conn->node, parent, p);
255 rb_insert_color(&conn->node, &conn->trans->client_conns);
256
David Howells19ffa012016-04-04 14:00:36 +0100257 conn->proto.cid = cid;
David Howells17926a72007-04-26 15:48:28 -0700258 write_unlock_bh(&conn->trans->conn_lock);
David Howells0d12f8a2016-03-04 15:53:46 +0000259 _leave(" [CID %x]", cid);
David Howells17926a72007-04-26 15:48:28 -0700260 return;
261
262 /* we found a connection with the proposed ID - walk the tree from that
263 * point looking for the next unused ID */
264id_exists:
265 for (;;) {
David Howells0d12f8a2016-03-04 15:53:46 +0000266 cid += RXRPC_CID_INC;
267 if (cid < RXRPC_CID_INC) {
268 cid = RXRPC_CID_INC;
269 conn->trans->conn_idcounter = cid;
David Howells17926a72007-04-26 15:48:28 -0700270 goto attempt_insertion;
271 }
272
273 parent = rb_next(parent);
274 if (!parent)
275 goto attempt_insertion;
276
277 xconn = rb_entry(parent, struct rxrpc_connection, node);
David Howells19ffa012016-04-04 14:00:36 +0100278 if (epoch < xconn->proto.epoch ||
279 cid < xconn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700280 goto attempt_insertion;
281 }
282}
283
284/*
285 * add a call to a connection's call-by-ID tree
286 */
287static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection *conn,
288 struct rxrpc_call *call)
289{
290 struct rxrpc_call *xcall;
291 struct rb_node *parent, **p;
292 __be32 call_id;
293
294 write_lock_bh(&conn->lock);
295
296 call_id = call->call_id;
297 p = &conn->calls.rb_node;
298 parent = NULL;
299 while (*p) {
300 parent = *p;
301 xcall = rb_entry(parent, struct rxrpc_call, conn_node);
302
303 if (call_id < xcall->call_id)
304 p = &(*p)->rb_left;
305 else if (call_id > xcall->call_id)
306 p = &(*p)->rb_right;
307 else
308 BUG();
309 }
310
311 rb_link_node(&call->conn_node, parent, p);
312 rb_insert_color(&call->conn_node, &conn->calls);
313
314 write_unlock_bh(&conn->lock);
315}
316
317/*
318 * connect a call on an exclusive connection
319 */
320static int rxrpc_connect_exclusive(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100321 struct rxrpc_conn_parameters *cp,
David Howells17926a72007-04-26 15:48:28 -0700322 struct rxrpc_transport *trans,
David Howells17926a72007-04-26 15:48:28 -0700323 struct rxrpc_call *call,
324 gfp_t gfp)
325{
326 struct rxrpc_connection *conn;
327 int chan, ret;
328
329 _enter("");
330
331 conn = rx->conn;
332 if (!conn) {
333 /* not yet present - create a candidate for a new connection
334 * and then redo the check */
335 conn = rxrpc_alloc_connection(gfp);
Dan Carpenter0975ecb2009-05-21 15:22:02 -0700336 if (!conn) {
337 _leave(" = -ENOMEM");
338 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700339 }
340
341 conn->trans = trans;
342 conn->bundle = NULL;
David Howells19ffa012016-04-04 14:00:36 +0100343 conn->params = *cp;
344 conn->proto.local = cp->local;
345 conn->proto.epoch = rxrpc_epoch;
346 conn->proto.cid = 0;
347 conn->proto.in_clientflag = 0;
348 conn->proto.family = cp->peer->srx.transport.family;
David Howells17926a72007-04-26 15:48:28 -0700349 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
David Howells17926a72007-04-26 15:48:28 -0700350 conn->state = RXRPC_CONN_CLIENT;
David Howells651350d2007-04-26 15:50:17 -0700351 conn->avail_calls = RXRPC_MAXCALLS - 1;
David Howells19ffa012016-04-04 14:00:36 +0100352
353 key_get(conn->params.key);
David Howells17926a72007-04-26 15:48:28 -0700354
355 ret = rxrpc_init_client_conn_security(conn);
356 if (ret < 0) {
David Howells19ffa012016-04-04 14:00:36 +0100357 key_put(conn->params.key);
David Howells17926a72007-04-26 15:48:28 -0700358 kfree(conn);
359 _leave(" = %d [key]", ret);
360 return ret;
361 }
362
363 write_lock_bh(&rxrpc_connection_lock);
364 list_add_tail(&conn->link, &rxrpc_connections);
365 write_unlock_bh(&rxrpc_connection_lock);
366
367 spin_lock(&trans->client_lock);
368 atomic_inc(&trans->usage);
369
370 _net("CONNECT EXCL new %d on TRANS %d",
371 conn->debug_id, conn->trans->debug_id);
372
373 rxrpc_assign_connection_id(conn);
374 rx->conn = conn;
Alexey Khoroshilov8f22ba62014-01-26 11:39:26 +0000375 } else {
376 spin_lock(&trans->client_lock);
David Howells17926a72007-04-26 15:48:28 -0700377 }
378
379 /* we've got a connection with a free channel and we can now attach the
380 * call to it
381 * - we're holding the transport's client lock
382 * - we're holding a reference on the connection
383 */
384 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
385 if (!conn->channels[chan])
386 goto found_channel;
387 goto no_free_channels;
388
389found_channel:
390 atomic_inc(&conn->usage);
391 conn->channels[chan] = call;
392 call->conn = conn;
393 call->channel = chan;
David Howells19ffa012016-04-04 14:00:36 +0100394 call->cid = conn->proto.cid | chan;
David Howells0d12f8a2016-03-04 15:53:46 +0000395 call->call_id = ++conn->call_counter;
David Howells17926a72007-04-26 15:48:28 -0700396
397 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000398 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700399
400 spin_unlock(&trans->client_lock);
401
402 rxrpc_add_call_ID_to_conn(conn, call);
403 _leave(" = 0");
404 return 0;
405
406no_free_channels:
407 spin_unlock(&trans->client_lock);
408 _leave(" = -ENOSR");
409 return -ENOSR;
410}
411
412/*
413 * find a connection for a call
414 * - called in process context with IRQs enabled
415 */
416int rxrpc_connect_call(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100417 struct rxrpc_conn_parameters *cp,
David Howells17926a72007-04-26 15:48:28 -0700418 struct rxrpc_transport *trans,
419 struct rxrpc_conn_bundle *bundle,
420 struct rxrpc_call *call,
421 gfp_t gfp)
422{
423 struct rxrpc_connection *conn, *candidate;
424 int chan, ret;
425
426 DECLARE_WAITQUEUE(myself, current);
427
428 _enter("%p,%lx,", rx, call->user_call_ID);
429
430 if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags))
David Howells19ffa012016-04-04 14:00:36 +0100431 return rxrpc_connect_exclusive(rx, cp, trans, call, gfp);
David Howells17926a72007-04-26 15:48:28 -0700432
433 spin_lock(&trans->client_lock);
434 for (;;) {
435 /* see if the bundle has a call slot available */
436 if (!list_empty(&bundle->avail_conns)) {
437 _debug("avail");
438 conn = list_entry(bundle->avail_conns.next,
439 struct rxrpc_connection,
440 bundle_link);
David Howells519d2562009-06-16 21:36:44 +0100441 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
442 list_del_init(&conn->bundle_link);
443 bundle->num_conns--;
444 continue;
445 }
David Howells17926a72007-04-26 15:48:28 -0700446 if (--conn->avail_calls == 0)
447 list_move(&conn->bundle_link,
448 &bundle->busy_conns);
David Howells651350d2007-04-26 15:50:17 -0700449 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
450 ASSERT(conn->channels[0] == NULL ||
451 conn->channels[1] == NULL ||
452 conn->channels[2] == NULL ||
453 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700454 atomic_inc(&conn->usage);
455 break;
456 }
457
458 if (!list_empty(&bundle->unused_conns)) {
459 _debug("unused");
460 conn = list_entry(bundle->unused_conns.next,
461 struct rxrpc_connection,
462 bundle_link);
David Howells519d2562009-06-16 21:36:44 +0100463 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
464 list_del_init(&conn->bundle_link);
465 bundle->num_conns--;
466 continue;
467 }
David Howells651350d2007-04-26 15:50:17 -0700468 ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
469 conn->avail_calls = RXRPC_MAXCALLS - 1;
470 ASSERT(conn->channels[0] == NULL &&
471 conn->channels[1] == NULL &&
472 conn->channels[2] == NULL &&
473 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700474 atomic_inc(&conn->usage);
475 list_move(&conn->bundle_link, &bundle->avail_conns);
476 break;
477 }
478
479 /* need to allocate a new connection */
480 _debug("get new conn [%d]", bundle->num_conns);
481
482 spin_unlock(&trans->client_lock);
483
484 if (signal_pending(current))
485 goto interrupted;
486
487 if (bundle->num_conns >= 20) {
488 _debug("too many conns");
489
Mel Gormand0164ad2015-11-06 16:28:21 -0800490 if (!gfpflags_allow_blocking(gfp)) {
David Howells17926a72007-04-26 15:48:28 -0700491 _leave(" = -EAGAIN");
492 return -EAGAIN;
493 }
494
495 add_wait_queue(&bundle->chanwait, &myself);
496 for (;;) {
497 set_current_state(TASK_INTERRUPTIBLE);
498 if (bundle->num_conns < 20 ||
499 !list_empty(&bundle->unused_conns) ||
500 !list_empty(&bundle->avail_conns))
501 break;
502 if (signal_pending(current))
503 goto interrupted_dequeue;
504 schedule();
505 }
506 remove_wait_queue(&bundle->chanwait, &myself);
507 __set_current_state(TASK_RUNNING);
508 spin_lock(&trans->client_lock);
509 continue;
510 }
511
512 /* not yet present - create a candidate for a new connection and then
513 * redo the check */
514 candidate = rxrpc_alloc_connection(gfp);
Dan Carpenter0975ecb2009-05-21 15:22:02 -0700515 if (!candidate) {
516 _leave(" = -ENOMEM");
517 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700518 }
519
520 candidate->trans = trans;
521 candidate->bundle = bundle;
David Howells19ffa012016-04-04 14:00:36 +0100522 candidate->params = *cp;
523 candidate->proto.local = cp->local;
524 candidate->proto.epoch = rxrpc_epoch;
525 candidate->proto.cid = 0;
526 candidate->proto.in_clientflag = 0;
527 candidate->proto.family = cp->peer->srx.transport.family;
David Howells17926a72007-04-26 15:48:28 -0700528 candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
David Howells17926a72007-04-26 15:48:28 -0700529 candidate->state = RXRPC_CONN_CLIENT;
530 candidate->avail_calls = RXRPC_MAXCALLS;
David Howells19ffa012016-04-04 14:00:36 +0100531
532 key_get(candidate->params.key);
David Howells17926a72007-04-26 15:48:28 -0700533
534 ret = rxrpc_init_client_conn_security(candidate);
535 if (ret < 0) {
David Howells19ffa012016-04-04 14:00:36 +0100536 key_put(candidate->params.key);
David Howells17926a72007-04-26 15:48:28 -0700537 kfree(candidate);
538 _leave(" = %d [key]", ret);
539 return ret;
540 }
541
542 write_lock_bh(&rxrpc_connection_lock);
543 list_add_tail(&candidate->link, &rxrpc_connections);
544 write_unlock_bh(&rxrpc_connection_lock);
545
546 spin_lock(&trans->client_lock);
547
548 list_add(&candidate->bundle_link, &bundle->unused_conns);
549 bundle->num_conns++;
550 atomic_inc(&bundle->usage);
551 atomic_inc(&trans->usage);
552
553 _net("CONNECT new %d on TRANS %d",
554 candidate->debug_id, candidate->trans->debug_id);
555
556 rxrpc_assign_connection_id(candidate);
David Howellse0e4d822016-04-07 17:23:58 +0100557 candidate->security->prime_packet_security(candidate);
David Howells17926a72007-04-26 15:48:28 -0700558
559 /* leave the candidate lurking in zombie mode attached to the
560 * bundle until we're ready for it */
561 rxrpc_put_connection(candidate);
562 candidate = NULL;
563 }
564
565 /* we've got a connection with a free channel and we can now attach the
566 * call to it
567 * - we're holding the transport's client lock
568 * - we're holding a reference on the connection
569 * - we're holding a reference on the bundle
570 */
571 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
572 if (!conn->channels[chan])
573 goto found_channel;
David Howells651350d2007-04-26 15:50:17 -0700574 ASSERT(conn->channels[0] == NULL ||
575 conn->channels[1] == NULL ||
576 conn->channels[2] == NULL ||
577 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700578 BUG();
579
580found_channel:
581 conn->channels[chan] = call;
582 call->conn = conn;
583 call->channel = chan;
David Howells19ffa012016-04-04 14:00:36 +0100584 call->cid = conn->proto.cid | chan;
David Howells0d12f8a2016-03-04 15:53:46 +0000585 call->call_id = ++conn->call_counter;
David Howells17926a72007-04-26 15:48:28 -0700586
587 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000588 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700589
David Howells651350d2007-04-26 15:50:17 -0700590 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
David Howells17926a72007-04-26 15:48:28 -0700591 spin_unlock(&trans->client_lock);
592
593 rxrpc_add_call_ID_to_conn(conn, call);
594
595 _leave(" = 0");
596 return 0;
597
598interrupted_dequeue:
599 remove_wait_queue(&bundle->chanwait, &myself);
600 __set_current_state(TASK_RUNNING);
601interrupted:
602 _leave(" = -ERESTARTSYS");
603 return -ERESTARTSYS;
604}
605
606/*
607 * get a record of an incoming connection
608 */
609struct rxrpc_connection *
610rxrpc_incoming_connection(struct rxrpc_transport *trans,
David Howells843099c2016-04-07 17:23:37 +0100611 struct rxrpc_host_header *hdr)
David Howells17926a72007-04-26 15:48:28 -0700612{
613 struct rxrpc_connection *conn, *candidate = NULL;
614 struct rb_node *p, **pp;
615 const char *new = "old";
616 __be32 epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000617 u32 cid;
David Howells17926a72007-04-26 15:48:28 -0700618
619 _enter("");
620
621 ASSERT(hdr->flags & RXRPC_CLIENT_INITIATED);
622
623 epoch = hdr->epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000624 cid = hdr->cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700625
626 /* search the connection list first */
627 read_lock_bh(&trans->conn_lock);
628
629 p = trans->server_conns.rb_node;
630 while (p) {
631 conn = rb_entry(p, struct rxrpc_connection, node);
632
David Howells19ffa012016-04-04 14:00:36 +0100633 _debug("maybe %x", conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700634
David Howells19ffa012016-04-04 14:00:36 +0100635 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700636 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100637 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700638 p = p->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100639 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700640 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100641 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700642 p = p->rb_right;
643 else
644 goto found_extant_connection;
645 }
646 read_unlock_bh(&trans->conn_lock);
647
648 /* not yet present - create a candidate for a new record and then
649 * redo the search */
David Howells843099c2016-04-07 17:23:37 +0100650 candidate = rxrpc_alloc_connection(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700651 if (!candidate) {
652 _leave(" = -ENOMEM");
653 return ERR_PTR(-ENOMEM);
654 }
655
656 candidate->trans = trans;
David Howells19ffa012016-04-04 14:00:36 +0100657 candidate->proto.local = trans->local;
658 candidate->proto.epoch = hdr->epoch;
659 candidate->proto.cid = hdr->cid & RXRPC_CIDMASK;
660 candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED;
661 candidate->params.local = trans->local;
662 candidate->params.peer = trans->peer;
663 candidate->params.service_id = hdr->serviceId;
David Howells17926a72007-04-26 15:48:28 -0700664 candidate->security_ix = hdr->securityIndex;
David Howells17926a72007-04-26 15:48:28 -0700665 candidate->out_clientflag = 0;
David Howells17926a72007-04-26 15:48:28 -0700666 candidate->state = RXRPC_CONN_SERVER;
David Howells19ffa012016-04-04 14:00:36 +0100667 if (candidate->params.service_id)
David Howells17926a72007-04-26 15:48:28 -0700668 candidate->state = RXRPC_CONN_SERVER_UNSECURED;
669
670 write_lock_bh(&trans->conn_lock);
671
672 pp = &trans->server_conns.rb_node;
673 p = NULL;
674 while (*pp) {
675 p = *pp;
676 conn = rb_entry(p, struct rxrpc_connection, node);
677
David Howells19ffa012016-04-04 14:00:36 +0100678 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700679 pp = &(*pp)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100680 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700681 pp = &(*pp)->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100682 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700683 pp = &(*pp)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100684 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700685 pp = &(*pp)->rb_right;
686 else
687 goto found_extant_second;
688 }
689
690 /* we can now add the new candidate to the list */
691 conn = candidate;
692 candidate = NULL;
693 rb_link_node(&conn->node, p, pp);
694 rb_insert_color(&conn->node, &trans->server_conns);
695 atomic_inc(&conn->trans->usage);
696
697 write_unlock_bh(&trans->conn_lock);
698
699 write_lock_bh(&rxrpc_connection_lock);
700 list_add_tail(&conn->link, &rxrpc_connections);
701 write_unlock_bh(&rxrpc_connection_lock);
702
703 new = "new";
704
705success:
David Howells19ffa012016-04-04 14:00:36 +0100706 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700707
708 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
709 return conn;
710
711 /* we found the connection in the list immediately */
712found_extant_connection:
713 if (hdr->securityIndex != conn->security_ix) {
714 read_unlock_bh(&trans->conn_lock);
715 goto security_mismatch;
716 }
717 atomic_inc(&conn->usage);
718 read_unlock_bh(&trans->conn_lock);
719 goto success;
720
721 /* we found the connection on the second time through the list */
722found_extant_second:
723 if (hdr->securityIndex != conn->security_ix) {
724 write_unlock_bh(&trans->conn_lock);
725 goto security_mismatch;
726 }
727 atomic_inc(&conn->usage);
728 write_unlock_bh(&trans->conn_lock);
729 kfree(candidate);
730 goto success;
731
732security_mismatch:
733 kfree(candidate);
734 _leave(" = -EKEYREJECTED");
735 return ERR_PTR(-EKEYREJECTED);
736}
737
738/*
739 * find a connection based on transport and RxRPC connection ID for an incoming
740 * packet
741 */
742struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
David Howells0d12f8a2016-03-04 15:53:46 +0000743 struct rxrpc_host_header *hdr)
David Howells17926a72007-04-26 15:48:28 -0700744{
745 struct rxrpc_connection *conn;
746 struct rb_node *p;
David Howells0d12f8a2016-03-04 15:53:46 +0000747 u32 epoch, cid;
David Howells17926a72007-04-26 15:48:28 -0700748
David Howells0d12f8a2016-03-04 15:53:46 +0000749 _enter(",{%x,%x}", hdr->cid, hdr->flags);
David Howells17926a72007-04-26 15:48:28 -0700750
751 read_lock_bh(&trans->conn_lock);
752
David Howells0d12f8a2016-03-04 15:53:46 +0000753 cid = hdr->cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700754 epoch = hdr->epoch;
755
756 if (hdr->flags & RXRPC_CLIENT_INITIATED)
757 p = trans->server_conns.rb_node;
758 else
759 p = trans->client_conns.rb_node;
760
761 while (p) {
762 conn = rb_entry(p, struct rxrpc_connection, node);
763
David Howells19ffa012016-04-04 14:00:36 +0100764 _debug("maybe %x", conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700765
David Howells19ffa012016-04-04 14:00:36 +0100766 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700767 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100768 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700769 p = p->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100770 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700771 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100772 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700773 p = p->rb_right;
774 else
775 goto found;
776 }
777
778 read_unlock_bh(&trans->conn_lock);
779 _leave(" = NULL");
780 return NULL;
781
782found:
783 atomic_inc(&conn->usage);
784 read_unlock_bh(&trans->conn_lock);
785 _leave(" = %p", conn);
786 return conn;
787}
788
789/*
790 * release a virtual connection
791 */
792void rxrpc_put_connection(struct rxrpc_connection *conn)
793{
794 _enter("%p{u=%d,d=%d}",
795 conn, atomic_read(&conn->usage), conn->debug_id);
796
797 ASSERTCMP(atomic_read(&conn->usage), >, 0);
798
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200799 conn->put_time = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700800 if (atomic_dec_and_test(&conn->usage)) {
801 _debug("zombie");
David Howells651350d2007-04-26 15:50:17 -0700802 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700803 }
804
805 _leave("");
806}
807
808/*
809 * destroy a virtual connection
810 */
811static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
812{
813 _enter("%p{%d}", conn, atomic_read(&conn->usage));
814
815 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
816
817 _net("DESTROY CONN %d", conn->debug_id);
818
819 if (conn->bundle)
820 rxrpc_put_bundle(conn->trans, conn->bundle);
821
822 ASSERT(RB_EMPTY_ROOT(&conn->calls));
823 rxrpc_purge_queue(&conn->rx_queue);
824
David Howellse0e4d822016-04-07 17:23:58 +0100825 conn->security->clear(conn);
David Howells19ffa012016-04-04 14:00:36 +0100826 key_put(conn->params.key);
David Howellse0e4d822016-04-07 17:23:58 +0100827 key_put(conn->server_key);
828
David Howells17926a72007-04-26 15:48:28 -0700829 rxrpc_put_transport(conn->trans);
830 kfree(conn);
831 _leave("");
832}
833
834/*
835 * reap dead connections
836 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800837static void rxrpc_connection_reaper(struct work_struct *work)
David Howells17926a72007-04-26 15:48:28 -0700838{
839 struct rxrpc_connection *conn, *_p;
840 unsigned long now, earliest, reap_time;
841
842 LIST_HEAD(graveyard);
843
844 _enter("");
845
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200846 now = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700847 earliest = ULONG_MAX;
848
849 write_lock_bh(&rxrpc_connection_lock);
850 list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
851 _debug("reap CONN %d { u=%d,t=%ld }",
852 conn->debug_id, atomic_read(&conn->usage),
853 (long) now - (long) conn->put_time);
854
855 if (likely(atomic_read(&conn->usage) > 0))
856 continue;
857
858 spin_lock(&conn->trans->client_lock);
859 write_lock(&conn->trans->conn_lock);
David Howells5873c082014-02-07 18:58:44 +0000860 reap_time = conn->put_time + rxrpc_connection_expiry;
David Howells17926a72007-04-26 15:48:28 -0700861
862 if (atomic_read(&conn->usage) > 0) {
863 ;
864 } else if (reap_time <= now) {
865 list_move_tail(&conn->link, &graveyard);
866 if (conn->out_clientflag)
867 rb_erase(&conn->node,
868 &conn->trans->client_conns);
869 else
870 rb_erase(&conn->node,
871 &conn->trans->server_conns);
872 if (conn->bundle) {
873 list_del_init(&conn->bundle_link);
874 conn->bundle->num_conns--;
875 }
876
877 } else if (reap_time < earliest) {
878 earliest = reap_time;
879 }
880
881 write_unlock(&conn->trans->conn_lock);
882 spin_unlock(&conn->trans->client_lock);
883 }
884 write_unlock_bh(&rxrpc_connection_lock);
885
886 if (earliest != ULONG_MAX) {
887 _debug("reschedule reaper %ld", (long) earliest - now);
888 ASSERTCMP(earliest, >, now);
David Howells651350d2007-04-26 15:50:17 -0700889 rxrpc_queue_delayed_work(&rxrpc_connection_reap,
890 (earliest - now) * HZ);
David Howells17926a72007-04-26 15:48:28 -0700891 }
892
893 /* then destroy all those pulled out */
894 while (!list_empty(&graveyard)) {
895 conn = list_entry(graveyard.next, struct rxrpc_connection,
896 link);
897 list_del_init(&conn->link);
898
899 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
900 rxrpc_destroy_connection(conn);
901 }
902
903 _leave("");
904}
905
906/*
907 * preemptively destroy all the connection records rather than waiting for them
908 * to time out
909 */
910void __exit rxrpc_destroy_all_connections(void)
911{
912 _enter("");
913
David Howells5873c082014-02-07 18:58:44 +0000914 rxrpc_connection_expiry = 0;
David Howells17926a72007-04-26 15:48:28 -0700915 cancel_delayed_work(&rxrpc_connection_reap);
David Howells651350d2007-04-26 15:50:17 -0700916 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700917
918 _leave("");
919}