blob: d67b1f1b5001c498adae114d9c4558ca696f1f69 [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
83 if (rx->trans == trans && rx->bundle) {
84 atomic_inc(&rx->bundle->usage);
85 return rx->bundle;
86 }
87
88 /* search the extant bundles first for one that matches the specified
89 * user ID */
90 spin_lock(&trans->client_lock);
91
92 p = trans->bundles.rb_node;
93 while (p) {
94 bundle = rb_entry(p, struct rxrpc_conn_bundle, node);
95
96 if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
97 p = p->rb_left;
98 else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
99 p = p->rb_right;
100 else
101 goto found_extant_bundle;
102 }
103
104 spin_unlock(&trans->client_lock);
105
106 /* not yet present - create a candidate for a new record and then
107 * redo the search */
108 candidate = rxrpc_alloc_bundle(gfp);
109 if (!candidate) {
110 _leave(" = -ENOMEM");
111 return ERR_PTR(-ENOMEM);
112 }
113
114 candidate->key = key_get(key);
115 candidate->service_id = service_id;
116
117 spin_lock(&trans->client_lock);
118
119 pp = &trans->bundles.rb_node;
120 parent = NULL;
121 while (*pp) {
122 parent = *pp;
123 bundle = rb_entry(parent, struct rxrpc_conn_bundle, node);
124
125 if (rxrpc_cmp_bundle(bundle, key, service_id) < 0)
126 pp = &(*pp)->rb_left;
127 else if (rxrpc_cmp_bundle(bundle, key, service_id) > 0)
128 pp = &(*pp)->rb_right;
129 else
130 goto found_extant_second;
131 }
132
133 /* second search also failed; add the new bundle */
134 bundle = candidate;
135 candidate = NULL;
136
137 rb_link_node(&bundle->node, parent, pp);
138 rb_insert_color(&bundle->node, &trans->bundles);
139 spin_unlock(&trans->client_lock);
140 _net("BUNDLE new on trans %d", trans->debug_id);
141 if (!rx->bundle && rx->sk.sk_state == RXRPC_CLIENT_CONNECTED) {
142 atomic_inc(&bundle->usage);
143 rx->bundle = bundle;
144 }
145 _leave(" = %p [new]", bundle);
146 return bundle;
147
148 /* we found the bundle in the list immediately */
149found_extant_bundle:
150 atomic_inc(&bundle->usage);
151 spin_unlock(&trans->client_lock);
152 _net("BUNDLE old on trans %d", trans->debug_id);
153 if (!rx->bundle && rx->sk.sk_state == RXRPC_CLIENT_CONNECTED) {
154 atomic_inc(&bundle->usage);
155 rx->bundle = bundle;
156 }
157 _leave(" = %p [extant %d]", bundle, atomic_read(&bundle->usage));
158 return bundle;
159
160 /* we found the bundle on the second time through the list */
161found_extant_second:
162 atomic_inc(&bundle->usage);
163 spin_unlock(&trans->client_lock);
164 kfree(candidate);
165 _net("BUNDLE old2 on trans %d", trans->debug_id);
166 if (!rx->bundle && rx->sk.sk_state == RXRPC_CLIENT_CONNECTED) {
167 atomic_inc(&bundle->usage);
168 rx->bundle = bundle;
169 }
170 _leave(" = %p [second %d]", bundle, atomic_read(&bundle->usage));
171 return bundle;
172}
173
174/*
175 * release a bundle
176 */
177void rxrpc_put_bundle(struct rxrpc_transport *trans,
178 struct rxrpc_conn_bundle *bundle)
179{
180 _enter("%p,%p{%d}",trans, bundle, atomic_read(&bundle->usage));
181
182 if (atomic_dec_and_lock(&bundle->usage, &trans->client_lock)) {
183 _debug("Destroy bundle");
184 rb_erase(&bundle->node, &trans->bundles);
185 spin_unlock(&trans->client_lock);
186 ASSERT(list_empty(&bundle->unused_conns));
187 ASSERT(list_empty(&bundle->avail_conns));
188 ASSERT(list_empty(&bundle->busy_conns));
189 ASSERTCMP(bundle->num_conns, ==, 0);
190 key_put(bundle->key);
191 kfree(bundle);
192 }
193
194 _leave("");
195}
196
197/*
198 * allocate a new connection
199 */
200static struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp)
201{
202 struct rxrpc_connection *conn;
203
204 _enter("");
205
206 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
207 if (conn) {
208 INIT_WORK(&conn->processor, &rxrpc_process_connection);
209 INIT_LIST_HEAD(&conn->bundle_link);
210 conn->calls = RB_ROOT;
211 skb_queue_head_init(&conn->rx_queue);
David Howellse0e4d822016-04-07 17:23:58 +0100212 conn->security = &rxrpc_no_security;
David Howells17926a72007-04-26 15:48:28 -0700213 rwlock_init(&conn->lock);
214 spin_lock_init(&conn->state_lock);
215 atomic_set(&conn->usage, 1);
216 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
217 conn->avail_calls = RXRPC_MAXCALLS;
218 conn->size_align = 4;
David Howells0d12f8a2016-03-04 15:53:46 +0000219 conn->header_size = sizeof(struct rxrpc_wire_header);
David Howells17926a72007-04-26 15:48:28 -0700220 }
221
Adrian Bunk16c61ad2007-06-15 15:15:43 -0700222 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
David Howells17926a72007-04-26 15:48:28 -0700223 return conn;
224}
225
226/*
227 * assign a connection ID to a connection and add it to the transport's
228 * connection lookup tree
229 * - called with transport client lock held
230 */
231static void rxrpc_assign_connection_id(struct rxrpc_connection *conn)
232{
233 struct rxrpc_connection *xconn;
234 struct rb_node *parent, **p;
235 __be32 epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000236 u32 cid;
David Howells17926a72007-04-26 15:48:28 -0700237
238 _enter("");
239
240 epoch = conn->epoch;
241
242 write_lock_bh(&conn->trans->conn_lock);
243
244 conn->trans->conn_idcounter += RXRPC_CID_INC;
245 if (conn->trans->conn_idcounter < RXRPC_CID_INC)
246 conn->trans->conn_idcounter = RXRPC_CID_INC;
David Howells0d12f8a2016-03-04 15:53:46 +0000247 cid = conn->trans->conn_idcounter;
David Howells17926a72007-04-26 15:48:28 -0700248
249attempt_insertion:
250 parent = NULL;
251 p = &conn->trans->client_conns.rb_node;
252
253 while (*p) {
254 parent = *p;
255 xconn = rb_entry(parent, struct rxrpc_connection, node);
256
257 if (epoch < xconn->epoch)
258 p = &(*p)->rb_left;
259 else if (epoch > xconn->epoch)
260 p = &(*p)->rb_right;
David Howells0d12f8a2016-03-04 15:53:46 +0000261 else if (cid < xconn->cid)
David Howells17926a72007-04-26 15:48:28 -0700262 p = &(*p)->rb_left;
David Howells0d12f8a2016-03-04 15:53:46 +0000263 else if (cid > xconn->cid)
David Howells17926a72007-04-26 15:48:28 -0700264 p = &(*p)->rb_right;
265 else
266 goto id_exists;
267 }
268
269 /* we've found a suitable hole - arrange for this connection to occupy
270 * it */
271 rb_link_node(&conn->node, parent, p);
272 rb_insert_color(&conn->node, &conn->trans->client_conns);
273
David Howells0d12f8a2016-03-04 15:53:46 +0000274 conn->cid = cid;
David Howells17926a72007-04-26 15:48:28 -0700275 write_unlock_bh(&conn->trans->conn_lock);
David Howells0d12f8a2016-03-04 15:53:46 +0000276 _leave(" [CID %x]", cid);
David Howells17926a72007-04-26 15:48:28 -0700277 return;
278
279 /* we found a connection with the proposed ID - walk the tree from that
280 * point looking for the next unused ID */
281id_exists:
282 for (;;) {
David Howells0d12f8a2016-03-04 15:53:46 +0000283 cid += RXRPC_CID_INC;
284 if (cid < RXRPC_CID_INC) {
285 cid = RXRPC_CID_INC;
286 conn->trans->conn_idcounter = cid;
David Howells17926a72007-04-26 15:48:28 -0700287 goto attempt_insertion;
288 }
289
290 parent = rb_next(parent);
291 if (!parent)
292 goto attempt_insertion;
293
294 xconn = rb_entry(parent, struct rxrpc_connection, node);
295 if (epoch < xconn->epoch ||
David Howells0d12f8a2016-03-04 15:53:46 +0000296 cid < xconn->cid)
David Howells17926a72007-04-26 15:48:28 -0700297 goto attempt_insertion;
298 }
299}
300
301/*
302 * add a call to a connection's call-by-ID tree
303 */
304static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection *conn,
305 struct rxrpc_call *call)
306{
307 struct rxrpc_call *xcall;
308 struct rb_node *parent, **p;
309 __be32 call_id;
310
311 write_lock_bh(&conn->lock);
312
313 call_id = call->call_id;
314 p = &conn->calls.rb_node;
315 parent = NULL;
316 while (*p) {
317 parent = *p;
318 xcall = rb_entry(parent, struct rxrpc_call, conn_node);
319
320 if (call_id < xcall->call_id)
321 p = &(*p)->rb_left;
322 else if (call_id > xcall->call_id)
323 p = &(*p)->rb_right;
324 else
325 BUG();
326 }
327
328 rb_link_node(&call->conn_node, parent, p);
329 rb_insert_color(&call->conn_node, &conn->calls);
330
331 write_unlock_bh(&conn->lock);
332}
333
334/*
335 * connect a call on an exclusive connection
336 */
337static int rxrpc_connect_exclusive(struct rxrpc_sock *rx,
338 struct rxrpc_transport *trans,
David Howells0d12f8a2016-03-04 15:53:46 +0000339 u16 service_id,
David Howells17926a72007-04-26 15:48:28 -0700340 struct rxrpc_call *call,
341 gfp_t gfp)
342{
343 struct rxrpc_connection *conn;
344 int chan, ret;
345
346 _enter("");
347
348 conn = rx->conn;
349 if (!conn) {
350 /* not yet present - create a candidate for a new connection
351 * and then redo the check */
352 conn = rxrpc_alloc_connection(gfp);
Dan Carpenter0975ecb2009-05-21 15:22:02 -0700353 if (!conn) {
354 _leave(" = -ENOMEM");
355 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700356 }
357
358 conn->trans = trans;
359 conn->bundle = NULL;
360 conn->service_id = service_id;
361 conn->epoch = rxrpc_epoch;
362 conn->in_clientflag = 0;
363 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
364 conn->cid = 0;
365 conn->state = RXRPC_CONN_CLIENT;
David Howells651350d2007-04-26 15:50:17 -0700366 conn->avail_calls = RXRPC_MAXCALLS - 1;
David Howells17926a72007-04-26 15:48:28 -0700367 conn->security_level = rx->min_sec_level;
368 conn->key = key_get(rx->key);
369
370 ret = rxrpc_init_client_conn_security(conn);
371 if (ret < 0) {
372 key_put(conn->key);
373 kfree(conn);
374 _leave(" = %d [key]", ret);
375 return ret;
376 }
377
378 write_lock_bh(&rxrpc_connection_lock);
379 list_add_tail(&conn->link, &rxrpc_connections);
380 write_unlock_bh(&rxrpc_connection_lock);
381
382 spin_lock(&trans->client_lock);
383 atomic_inc(&trans->usage);
384
385 _net("CONNECT EXCL new %d on TRANS %d",
386 conn->debug_id, conn->trans->debug_id);
387
388 rxrpc_assign_connection_id(conn);
389 rx->conn = conn;
Alexey Khoroshilov8f22ba62014-01-26 11:39:26 +0000390 } else {
391 spin_lock(&trans->client_lock);
David Howells17926a72007-04-26 15:48:28 -0700392 }
393
394 /* we've got a connection with a free channel and we can now attach the
395 * call to it
396 * - we're holding the transport's client lock
397 * - we're holding a reference on the connection
398 */
399 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
400 if (!conn->channels[chan])
401 goto found_channel;
402 goto no_free_channels;
403
404found_channel:
405 atomic_inc(&conn->usage);
406 conn->channels[chan] = call;
407 call->conn = conn;
408 call->channel = chan;
David Howells0d12f8a2016-03-04 15:53:46 +0000409 call->cid = conn->cid | chan;
410 call->call_id = ++conn->call_counter;
David Howells17926a72007-04-26 15:48:28 -0700411
412 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000413 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700414
415 spin_unlock(&trans->client_lock);
416
417 rxrpc_add_call_ID_to_conn(conn, call);
418 _leave(" = 0");
419 return 0;
420
421no_free_channels:
422 spin_unlock(&trans->client_lock);
423 _leave(" = -ENOSR");
424 return -ENOSR;
425}
426
427/*
428 * find a connection for a call
429 * - called in process context with IRQs enabled
430 */
431int rxrpc_connect_call(struct rxrpc_sock *rx,
432 struct rxrpc_transport *trans,
433 struct rxrpc_conn_bundle *bundle,
434 struct rxrpc_call *call,
435 gfp_t gfp)
436{
437 struct rxrpc_connection *conn, *candidate;
438 int chan, ret;
439
440 DECLARE_WAITQUEUE(myself, current);
441
442 _enter("%p,%lx,", rx, call->user_call_ID);
443
444 if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags))
445 return rxrpc_connect_exclusive(rx, trans, bundle->service_id,
446 call, gfp);
447
448 spin_lock(&trans->client_lock);
449 for (;;) {
450 /* see if the bundle has a call slot available */
451 if (!list_empty(&bundle->avail_conns)) {
452 _debug("avail");
453 conn = list_entry(bundle->avail_conns.next,
454 struct rxrpc_connection,
455 bundle_link);
David Howells519d2562009-06-16 21:36:44 +0100456 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
457 list_del_init(&conn->bundle_link);
458 bundle->num_conns--;
459 continue;
460 }
David Howells17926a72007-04-26 15:48:28 -0700461 if (--conn->avail_calls == 0)
462 list_move(&conn->bundle_link,
463 &bundle->busy_conns);
David Howells651350d2007-04-26 15:50:17 -0700464 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
465 ASSERT(conn->channels[0] == NULL ||
466 conn->channels[1] == NULL ||
467 conn->channels[2] == NULL ||
468 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700469 atomic_inc(&conn->usage);
470 break;
471 }
472
473 if (!list_empty(&bundle->unused_conns)) {
474 _debug("unused");
475 conn = list_entry(bundle->unused_conns.next,
476 struct rxrpc_connection,
477 bundle_link);
David Howells519d2562009-06-16 21:36:44 +0100478 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
479 list_del_init(&conn->bundle_link);
480 bundle->num_conns--;
481 continue;
482 }
David Howells651350d2007-04-26 15:50:17 -0700483 ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
484 conn->avail_calls = RXRPC_MAXCALLS - 1;
485 ASSERT(conn->channels[0] == NULL &&
486 conn->channels[1] == NULL &&
487 conn->channels[2] == NULL &&
488 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700489 atomic_inc(&conn->usage);
490 list_move(&conn->bundle_link, &bundle->avail_conns);
491 break;
492 }
493
494 /* need to allocate a new connection */
495 _debug("get new conn [%d]", bundle->num_conns);
496
497 spin_unlock(&trans->client_lock);
498
499 if (signal_pending(current))
500 goto interrupted;
501
502 if (bundle->num_conns >= 20) {
503 _debug("too many conns");
504
Mel Gormand0164ad2015-11-06 16:28:21 -0800505 if (!gfpflags_allow_blocking(gfp)) {
David Howells17926a72007-04-26 15:48:28 -0700506 _leave(" = -EAGAIN");
507 return -EAGAIN;
508 }
509
510 add_wait_queue(&bundle->chanwait, &myself);
511 for (;;) {
512 set_current_state(TASK_INTERRUPTIBLE);
513 if (bundle->num_conns < 20 ||
514 !list_empty(&bundle->unused_conns) ||
515 !list_empty(&bundle->avail_conns))
516 break;
517 if (signal_pending(current))
518 goto interrupted_dequeue;
519 schedule();
520 }
521 remove_wait_queue(&bundle->chanwait, &myself);
522 __set_current_state(TASK_RUNNING);
523 spin_lock(&trans->client_lock);
524 continue;
525 }
526
527 /* not yet present - create a candidate for a new connection and then
528 * redo the check */
529 candidate = rxrpc_alloc_connection(gfp);
Dan Carpenter0975ecb2009-05-21 15:22:02 -0700530 if (!candidate) {
531 _leave(" = -ENOMEM");
532 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700533 }
534
535 candidate->trans = trans;
536 candidate->bundle = bundle;
537 candidate->service_id = bundle->service_id;
538 candidate->epoch = rxrpc_epoch;
539 candidate->in_clientflag = 0;
540 candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
541 candidate->cid = 0;
542 candidate->state = RXRPC_CONN_CLIENT;
543 candidate->avail_calls = RXRPC_MAXCALLS;
544 candidate->security_level = rx->min_sec_level;
David Howells651350d2007-04-26 15:50:17 -0700545 candidate->key = key_get(bundle->key);
David Howells17926a72007-04-26 15:48:28 -0700546
547 ret = rxrpc_init_client_conn_security(candidate);
548 if (ret < 0) {
549 key_put(candidate->key);
550 kfree(candidate);
551 _leave(" = %d [key]", ret);
552 return ret;
553 }
554
555 write_lock_bh(&rxrpc_connection_lock);
556 list_add_tail(&candidate->link, &rxrpc_connections);
557 write_unlock_bh(&rxrpc_connection_lock);
558
559 spin_lock(&trans->client_lock);
560
561 list_add(&candidate->bundle_link, &bundle->unused_conns);
562 bundle->num_conns++;
563 atomic_inc(&bundle->usage);
564 atomic_inc(&trans->usage);
565
566 _net("CONNECT new %d on TRANS %d",
567 candidate->debug_id, candidate->trans->debug_id);
568
569 rxrpc_assign_connection_id(candidate);
David Howellse0e4d822016-04-07 17:23:58 +0100570 candidate->security->prime_packet_security(candidate);
David Howells17926a72007-04-26 15:48:28 -0700571
572 /* leave the candidate lurking in zombie mode attached to the
573 * bundle until we're ready for it */
574 rxrpc_put_connection(candidate);
575 candidate = NULL;
576 }
577
578 /* we've got a connection with a free channel and we can now attach the
579 * call to it
580 * - we're holding the transport's client lock
581 * - we're holding a reference on the connection
582 * - we're holding a reference on the bundle
583 */
584 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
585 if (!conn->channels[chan])
586 goto found_channel;
David Howells651350d2007-04-26 15:50:17 -0700587 ASSERT(conn->channels[0] == NULL ||
588 conn->channels[1] == NULL ||
589 conn->channels[2] == NULL ||
590 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700591 BUG();
592
593found_channel:
594 conn->channels[chan] = call;
595 call->conn = conn;
596 call->channel = chan;
David Howells0d12f8a2016-03-04 15:53:46 +0000597 call->cid = conn->cid | chan;
598 call->call_id = ++conn->call_counter;
David Howells17926a72007-04-26 15:48:28 -0700599
600 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000601 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700602
David Howells651350d2007-04-26 15:50:17 -0700603 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
David Howells17926a72007-04-26 15:48:28 -0700604 spin_unlock(&trans->client_lock);
605
606 rxrpc_add_call_ID_to_conn(conn, call);
607
608 _leave(" = 0");
609 return 0;
610
611interrupted_dequeue:
612 remove_wait_queue(&bundle->chanwait, &myself);
613 __set_current_state(TASK_RUNNING);
614interrupted:
615 _leave(" = -ERESTARTSYS");
616 return -ERESTARTSYS;
617}
618
619/*
620 * get a record of an incoming connection
621 */
622struct rxrpc_connection *
623rxrpc_incoming_connection(struct rxrpc_transport *trans,
David Howells843099c2016-04-07 17:23:37 +0100624 struct rxrpc_host_header *hdr)
David Howells17926a72007-04-26 15:48:28 -0700625{
626 struct rxrpc_connection *conn, *candidate = NULL;
627 struct rb_node *p, **pp;
628 const char *new = "old";
629 __be32 epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000630 u32 cid;
David Howells17926a72007-04-26 15:48:28 -0700631
632 _enter("");
633
634 ASSERT(hdr->flags & RXRPC_CLIENT_INITIATED);
635
636 epoch = hdr->epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000637 cid = hdr->cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700638
639 /* search the connection list first */
640 read_lock_bh(&trans->conn_lock);
641
642 p = trans->server_conns.rb_node;
643 while (p) {
644 conn = rb_entry(p, struct rxrpc_connection, node);
645
David Howells0d12f8a2016-03-04 15:53:46 +0000646 _debug("maybe %x", conn->cid);
David Howells17926a72007-04-26 15:48:28 -0700647
648 if (epoch < conn->epoch)
649 p = p->rb_left;
650 else if (epoch > conn->epoch)
651 p = p->rb_right;
David Howells0d12f8a2016-03-04 15:53:46 +0000652 else if (cid < conn->cid)
David Howells17926a72007-04-26 15:48:28 -0700653 p = p->rb_left;
David Howells0d12f8a2016-03-04 15:53:46 +0000654 else if (cid > conn->cid)
David Howells17926a72007-04-26 15:48:28 -0700655 p = p->rb_right;
656 else
657 goto found_extant_connection;
658 }
659 read_unlock_bh(&trans->conn_lock);
660
661 /* not yet present - create a candidate for a new record and then
662 * redo the search */
David Howells843099c2016-04-07 17:23:37 +0100663 candidate = rxrpc_alloc_connection(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700664 if (!candidate) {
665 _leave(" = -ENOMEM");
666 return ERR_PTR(-ENOMEM);
667 }
668
669 candidate->trans = trans;
670 candidate->epoch = hdr->epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000671 candidate->cid = hdr->cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700672 candidate->service_id = hdr->serviceId;
673 candidate->security_ix = hdr->securityIndex;
674 candidate->in_clientflag = RXRPC_CLIENT_INITIATED;
675 candidate->out_clientflag = 0;
David Howells17926a72007-04-26 15:48:28 -0700676 candidate->state = RXRPC_CONN_SERVER;
677 if (candidate->service_id)
678 candidate->state = RXRPC_CONN_SERVER_UNSECURED;
679
680 write_lock_bh(&trans->conn_lock);
681
682 pp = &trans->server_conns.rb_node;
683 p = NULL;
684 while (*pp) {
685 p = *pp;
686 conn = rb_entry(p, struct rxrpc_connection, node);
687
688 if (epoch < conn->epoch)
689 pp = &(*pp)->rb_left;
690 else if (epoch > conn->epoch)
691 pp = &(*pp)->rb_right;
David Howells0d12f8a2016-03-04 15:53:46 +0000692 else if (cid < conn->cid)
David Howells17926a72007-04-26 15:48:28 -0700693 pp = &(*pp)->rb_left;
David Howells0d12f8a2016-03-04 15:53:46 +0000694 else if (cid > conn->cid)
David Howells17926a72007-04-26 15:48:28 -0700695 pp = &(*pp)->rb_right;
696 else
697 goto found_extant_second;
698 }
699
700 /* we can now add the new candidate to the list */
701 conn = candidate;
702 candidate = NULL;
703 rb_link_node(&conn->node, p, pp);
704 rb_insert_color(&conn->node, &trans->server_conns);
705 atomic_inc(&conn->trans->usage);
706
707 write_unlock_bh(&trans->conn_lock);
708
709 write_lock_bh(&rxrpc_connection_lock);
710 list_add_tail(&conn->link, &rxrpc_connections);
711 write_unlock_bh(&rxrpc_connection_lock);
712
713 new = "new";
714
715success:
David Howells0d12f8a2016-03-04 15:53:46 +0000716 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->cid);
David Howells17926a72007-04-26 15:48:28 -0700717
718 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
719 return conn;
720
721 /* we found the connection in the list immediately */
722found_extant_connection:
723 if (hdr->securityIndex != conn->security_ix) {
724 read_unlock_bh(&trans->conn_lock);
725 goto security_mismatch;
726 }
727 atomic_inc(&conn->usage);
728 read_unlock_bh(&trans->conn_lock);
729 goto success;
730
731 /* we found the connection on the second time through the list */
732found_extant_second:
733 if (hdr->securityIndex != conn->security_ix) {
734 write_unlock_bh(&trans->conn_lock);
735 goto security_mismatch;
736 }
737 atomic_inc(&conn->usage);
738 write_unlock_bh(&trans->conn_lock);
739 kfree(candidate);
740 goto success;
741
742security_mismatch:
743 kfree(candidate);
744 _leave(" = -EKEYREJECTED");
745 return ERR_PTR(-EKEYREJECTED);
746}
747
748/*
749 * find a connection based on transport and RxRPC connection ID for an incoming
750 * packet
751 */
752struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
David Howells0d12f8a2016-03-04 15:53:46 +0000753 struct rxrpc_host_header *hdr)
David Howells17926a72007-04-26 15:48:28 -0700754{
755 struct rxrpc_connection *conn;
756 struct rb_node *p;
David Howells0d12f8a2016-03-04 15:53:46 +0000757 u32 epoch, cid;
David Howells17926a72007-04-26 15:48:28 -0700758
David Howells0d12f8a2016-03-04 15:53:46 +0000759 _enter(",{%x,%x}", hdr->cid, hdr->flags);
David Howells17926a72007-04-26 15:48:28 -0700760
761 read_lock_bh(&trans->conn_lock);
762
David Howells0d12f8a2016-03-04 15:53:46 +0000763 cid = hdr->cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700764 epoch = hdr->epoch;
765
766 if (hdr->flags & RXRPC_CLIENT_INITIATED)
767 p = trans->server_conns.rb_node;
768 else
769 p = trans->client_conns.rb_node;
770
771 while (p) {
772 conn = rb_entry(p, struct rxrpc_connection, node);
773
David Howells0d12f8a2016-03-04 15:53:46 +0000774 _debug("maybe %x", conn->cid);
David Howells17926a72007-04-26 15:48:28 -0700775
776 if (epoch < conn->epoch)
777 p = p->rb_left;
778 else if (epoch > conn->epoch)
779 p = p->rb_right;
David Howells0d12f8a2016-03-04 15:53:46 +0000780 else if (cid < conn->cid)
David Howells17926a72007-04-26 15:48:28 -0700781 p = p->rb_left;
David Howells0d12f8a2016-03-04 15:53:46 +0000782 else if (cid > conn->cid)
David Howells17926a72007-04-26 15:48:28 -0700783 p = p->rb_right;
784 else
785 goto found;
786 }
787
788 read_unlock_bh(&trans->conn_lock);
789 _leave(" = NULL");
790 return NULL;
791
792found:
793 atomic_inc(&conn->usage);
794 read_unlock_bh(&trans->conn_lock);
795 _leave(" = %p", conn);
796 return conn;
797}
798
799/*
800 * release a virtual connection
801 */
802void rxrpc_put_connection(struct rxrpc_connection *conn)
803{
804 _enter("%p{u=%d,d=%d}",
805 conn, atomic_read(&conn->usage), conn->debug_id);
806
807 ASSERTCMP(atomic_read(&conn->usage), >, 0);
808
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200809 conn->put_time = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700810 if (atomic_dec_and_test(&conn->usage)) {
811 _debug("zombie");
David Howells651350d2007-04-26 15:50:17 -0700812 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700813 }
814
815 _leave("");
816}
817
818/*
819 * destroy a virtual connection
820 */
821static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
822{
823 _enter("%p{%d}", conn, atomic_read(&conn->usage));
824
825 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
826
827 _net("DESTROY CONN %d", conn->debug_id);
828
829 if (conn->bundle)
830 rxrpc_put_bundle(conn->trans, conn->bundle);
831
832 ASSERT(RB_EMPTY_ROOT(&conn->calls));
833 rxrpc_purge_queue(&conn->rx_queue);
834
David Howellse0e4d822016-04-07 17:23:58 +0100835 conn->security->clear(conn);
836 key_put(conn->key);
837 key_put(conn->server_key);
838
David Howells17926a72007-04-26 15:48:28 -0700839 rxrpc_put_transport(conn->trans);
840 kfree(conn);
841 _leave("");
842}
843
844/*
845 * reap dead connections
846 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800847static void rxrpc_connection_reaper(struct work_struct *work)
David Howells17926a72007-04-26 15:48:28 -0700848{
849 struct rxrpc_connection *conn, *_p;
850 unsigned long now, earliest, reap_time;
851
852 LIST_HEAD(graveyard);
853
854 _enter("");
855
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200856 now = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700857 earliest = ULONG_MAX;
858
859 write_lock_bh(&rxrpc_connection_lock);
860 list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
861 _debug("reap CONN %d { u=%d,t=%ld }",
862 conn->debug_id, atomic_read(&conn->usage),
863 (long) now - (long) conn->put_time);
864
865 if (likely(atomic_read(&conn->usage) > 0))
866 continue;
867
868 spin_lock(&conn->trans->client_lock);
869 write_lock(&conn->trans->conn_lock);
David Howells5873c082014-02-07 18:58:44 +0000870 reap_time = conn->put_time + rxrpc_connection_expiry;
David Howells17926a72007-04-26 15:48:28 -0700871
872 if (atomic_read(&conn->usage) > 0) {
873 ;
874 } else if (reap_time <= now) {
875 list_move_tail(&conn->link, &graveyard);
876 if (conn->out_clientflag)
877 rb_erase(&conn->node,
878 &conn->trans->client_conns);
879 else
880 rb_erase(&conn->node,
881 &conn->trans->server_conns);
882 if (conn->bundle) {
883 list_del_init(&conn->bundle_link);
884 conn->bundle->num_conns--;
885 }
886
887 } else if (reap_time < earliest) {
888 earliest = reap_time;
889 }
890
891 write_unlock(&conn->trans->conn_lock);
892 spin_unlock(&conn->trans->client_lock);
893 }
894 write_unlock_bh(&rxrpc_connection_lock);
895
896 if (earliest != ULONG_MAX) {
897 _debug("reschedule reaper %ld", (long) earliest - now);
898 ASSERTCMP(earliest, >, now);
David Howells651350d2007-04-26 15:50:17 -0700899 rxrpc_queue_delayed_work(&rxrpc_connection_reap,
900 (earliest - now) * HZ);
David Howells17926a72007-04-26 15:48:28 -0700901 }
902
903 /* then destroy all those pulled out */
904 while (!list_empty(&graveyard)) {
905 conn = list_entry(graveyard.next, struct rxrpc_connection,
906 link);
907 list_del_init(&conn->link);
908
909 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
910 rxrpc_destroy_connection(conn);
911 }
912
913 _leave("");
914}
915
916/*
917 * preemptively destroy all the connection records rather than waiting for them
918 * to time out
919 */
920void __exit rxrpc_destroy_all_connections(void)
921{
922 _enter("");
923
David Howells5873c082014-02-07 18:58:44 +0000924 rxrpc_connection_expiry = 0;
David Howells17926a72007-04-26 15:48:28 -0700925 cancel_delayed_work(&rxrpc_connection_reap);
David Howells651350d2007-04-26 15:50:17 -0700926 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700927
928 _leave("");
929}