blob: 3b42fc4f4fe34f13c4a9dcbf7735b428734233af [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
David Howellscc8feb82016-04-04 14:00:37 +0100331 conn = rxrpc_alloc_connection(gfp);
David Howells17926a72007-04-26 15:48:28 -0700332 if (!conn) {
David Howellscc8feb82016-04-04 14:00:37 +0100333 _leave(" = -ENOMEM");
334 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700335 }
336
David Howellscc8feb82016-04-04 14:00:37 +0100337 conn->trans = trans;
338 conn->bundle = NULL;
339 conn->params = *cp;
340 conn->proto.local = cp->local;
341 conn->proto.epoch = rxrpc_epoch;
342 conn->proto.cid = 0;
343 conn->proto.in_clientflag = 0;
344 conn->proto.family = cp->peer->srx.transport.family;
345 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
346 conn->state = RXRPC_CONN_CLIENT;
347 conn->avail_calls = RXRPC_MAXCALLS - 1;
David Howells17926a72007-04-26 15:48:28 -0700348
David Howellscc8feb82016-04-04 14:00:37 +0100349 key_get(conn->params.key);
350
351 ret = rxrpc_init_client_conn_security(conn);
352 if (ret < 0) {
353 key_put(conn->params.key);
354 kfree(conn);
355 _leave(" = %d [key]", ret);
356 return ret;
357 }
358
359 write_lock_bh(&rxrpc_connection_lock);
360 list_add_tail(&conn->link, &rxrpc_connections);
361 write_unlock_bh(&rxrpc_connection_lock);
362
363 spin_lock(&trans->client_lock);
364 atomic_inc(&trans->usage);
365
366 _net("CONNECT EXCL new %d on TRANS %d",
367 conn->debug_id, conn->trans->debug_id);
368
369 rxrpc_assign_connection_id(conn);
370
371 /* Since no one else can use the connection, we just use the first
372 * channel.
373 */
374 chan = 0;
David Howells17926a72007-04-26 15:48:28 -0700375 atomic_inc(&conn->usage);
376 conn->channels[chan] = call;
David Howellscc8feb82016-04-04 14:00:37 +0100377 conn->call_counter = 1;
David Howells17926a72007-04-26 15:48:28 -0700378 call->conn = conn;
379 call->channel = chan;
David Howells19ffa012016-04-04 14:00:36 +0100380 call->cid = conn->proto.cid | chan;
David Howellscc8feb82016-04-04 14:00:37 +0100381 call->call_id = 1;
David Howells17926a72007-04-26 15:48:28 -0700382
383 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000384 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700385
386 spin_unlock(&trans->client_lock);
387
388 rxrpc_add_call_ID_to_conn(conn, call);
389 _leave(" = 0");
390 return 0;
David Howells17926a72007-04-26 15:48:28 -0700391}
392
393/*
394 * find a connection for a call
395 * - called in process context with IRQs enabled
396 */
397int rxrpc_connect_call(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100398 struct rxrpc_conn_parameters *cp,
David Howells17926a72007-04-26 15:48:28 -0700399 struct rxrpc_transport *trans,
400 struct rxrpc_conn_bundle *bundle,
401 struct rxrpc_call *call,
402 gfp_t gfp)
403{
404 struct rxrpc_connection *conn, *candidate;
405 int chan, ret;
406
407 DECLARE_WAITQUEUE(myself, current);
408
409 _enter("%p,%lx,", rx, call->user_call_ID);
410
David Howellscc8feb82016-04-04 14:00:37 +0100411 if (cp->exclusive)
David Howells19ffa012016-04-04 14:00:36 +0100412 return rxrpc_connect_exclusive(rx, cp, trans, call, gfp);
David Howells17926a72007-04-26 15:48:28 -0700413
414 spin_lock(&trans->client_lock);
415 for (;;) {
416 /* see if the bundle has a call slot available */
417 if (!list_empty(&bundle->avail_conns)) {
418 _debug("avail");
419 conn = list_entry(bundle->avail_conns.next,
420 struct rxrpc_connection,
421 bundle_link);
David Howells519d2562009-06-16 21:36:44 +0100422 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
423 list_del_init(&conn->bundle_link);
424 bundle->num_conns--;
425 continue;
426 }
David Howells17926a72007-04-26 15:48:28 -0700427 if (--conn->avail_calls == 0)
428 list_move(&conn->bundle_link,
429 &bundle->busy_conns);
David Howells651350d2007-04-26 15:50:17 -0700430 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
431 ASSERT(conn->channels[0] == NULL ||
432 conn->channels[1] == NULL ||
433 conn->channels[2] == NULL ||
434 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700435 atomic_inc(&conn->usage);
436 break;
437 }
438
439 if (!list_empty(&bundle->unused_conns)) {
440 _debug("unused");
441 conn = list_entry(bundle->unused_conns.next,
442 struct rxrpc_connection,
443 bundle_link);
David Howells519d2562009-06-16 21:36:44 +0100444 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
445 list_del_init(&conn->bundle_link);
446 bundle->num_conns--;
447 continue;
448 }
David Howells651350d2007-04-26 15:50:17 -0700449 ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
450 conn->avail_calls = RXRPC_MAXCALLS - 1;
451 ASSERT(conn->channels[0] == NULL &&
452 conn->channels[1] == NULL &&
453 conn->channels[2] == NULL &&
454 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700455 atomic_inc(&conn->usage);
456 list_move(&conn->bundle_link, &bundle->avail_conns);
457 break;
458 }
459
460 /* need to allocate a new connection */
461 _debug("get new conn [%d]", bundle->num_conns);
462
463 spin_unlock(&trans->client_lock);
464
465 if (signal_pending(current))
466 goto interrupted;
467
468 if (bundle->num_conns >= 20) {
469 _debug("too many conns");
470
Mel Gormand0164ad2015-11-06 16:28:21 -0800471 if (!gfpflags_allow_blocking(gfp)) {
David Howells17926a72007-04-26 15:48:28 -0700472 _leave(" = -EAGAIN");
473 return -EAGAIN;
474 }
475
476 add_wait_queue(&bundle->chanwait, &myself);
477 for (;;) {
478 set_current_state(TASK_INTERRUPTIBLE);
479 if (bundle->num_conns < 20 ||
480 !list_empty(&bundle->unused_conns) ||
481 !list_empty(&bundle->avail_conns))
482 break;
483 if (signal_pending(current))
484 goto interrupted_dequeue;
485 schedule();
486 }
487 remove_wait_queue(&bundle->chanwait, &myself);
488 __set_current_state(TASK_RUNNING);
489 spin_lock(&trans->client_lock);
490 continue;
491 }
492
493 /* not yet present - create a candidate for a new connection and then
494 * redo the check */
495 candidate = rxrpc_alloc_connection(gfp);
Dan Carpenter0975ecb2009-05-21 15:22:02 -0700496 if (!candidate) {
497 _leave(" = -ENOMEM");
498 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700499 }
500
501 candidate->trans = trans;
502 candidate->bundle = bundle;
David Howells19ffa012016-04-04 14:00:36 +0100503 candidate->params = *cp;
504 candidate->proto.local = cp->local;
505 candidate->proto.epoch = rxrpc_epoch;
506 candidate->proto.cid = 0;
507 candidate->proto.in_clientflag = 0;
508 candidate->proto.family = cp->peer->srx.transport.family;
David Howells17926a72007-04-26 15:48:28 -0700509 candidate->out_clientflag = RXRPC_CLIENT_INITIATED;
David Howells17926a72007-04-26 15:48:28 -0700510 candidate->state = RXRPC_CONN_CLIENT;
511 candidate->avail_calls = RXRPC_MAXCALLS;
David Howells19ffa012016-04-04 14:00:36 +0100512
513 key_get(candidate->params.key);
David Howells17926a72007-04-26 15:48:28 -0700514
515 ret = rxrpc_init_client_conn_security(candidate);
516 if (ret < 0) {
David Howells19ffa012016-04-04 14:00:36 +0100517 key_put(candidate->params.key);
David Howells17926a72007-04-26 15:48:28 -0700518 kfree(candidate);
519 _leave(" = %d [key]", ret);
520 return ret;
521 }
522
523 write_lock_bh(&rxrpc_connection_lock);
524 list_add_tail(&candidate->link, &rxrpc_connections);
525 write_unlock_bh(&rxrpc_connection_lock);
526
527 spin_lock(&trans->client_lock);
528
529 list_add(&candidate->bundle_link, &bundle->unused_conns);
530 bundle->num_conns++;
531 atomic_inc(&bundle->usage);
532 atomic_inc(&trans->usage);
533
534 _net("CONNECT new %d on TRANS %d",
535 candidate->debug_id, candidate->trans->debug_id);
536
537 rxrpc_assign_connection_id(candidate);
David Howellse0e4d822016-04-07 17:23:58 +0100538 candidate->security->prime_packet_security(candidate);
David Howells17926a72007-04-26 15:48:28 -0700539
540 /* leave the candidate lurking in zombie mode attached to the
541 * bundle until we're ready for it */
542 rxrpc_put_connection(candidate);
543 candidate = NULL;
544 }
545
546 /* we've got a connection with a free channel and we can now attach the
547 * call to it
548 * - we're holding the transport's client lock
549 * - we're holding a reference on the connection
550 * - we're holding a reference on the bundle
551 */
552 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
553 if (!conn->channels[chan])
554 goto found_channel;
David Howells651350d2007-04-26 15:50:17 -0700555 ASSERT(conn->channels[0] == NULL ||
556 conn->channels[1] == NULL ||
557 conn->channels[2] == NULL ||
558 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700559 BUG();
560
561found_channel:
562 conn->channels[chan] = call;
563 call->conn = conn;
564 call->channel = chan;
David Howells19ffa012016-04-04 14:00:36 +0100565 call->cid = conn->proto.cid | chan;
David Howells0d12f8a2016-03-04 15:53:46 +0000566 call->call_id = ++conn->call_counter;
David Howells17926a72007-04-26 15:48:28 -0700567
568 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000569 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700570
David Howells651350d2007-04-26 15:50:17 -0700571 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
David Howells17926a72007-04-26 15:48:28 -0700572 spin_unlock(&trans->client_lock);
573
574 rxrpc_add_call_ID_to_conn(conn, call);
575
576 _leave(" = 0");
577 return 0;
578
579interrupted_dequeue:
580 remove_wait_queue(&bundle->chanwait, &myself);
581 __set_current_state(TASK_RUNNING);
582interrupted:
583 _leave(" = -ERESTARTSYS");
584 return -ERESTARTSYS;
585}
586
587/*
588 * get a record of an incoming connection
589 */
590struct rxrpc_connection *
David Howells42886ff2016-06-16 13:31:07 +0100591rxrpc_incoming_connection(struct rxrpc_transport *trans, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700592{
593 struct rxrpc_connection *conn, *candidate = NULL;
David Howells42886ff2016-06-16 13:31:07 +0100594 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700595 struct rb_node *p, **pp;
596 const char *new = "old";
597 __be32 epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000598 u32 cid;
David Howells17926a72007-04-26 15:48:28 -0700599
600 _enter("");
601
David Howells42886ff2016-06-16 13:31:07 +0100602 ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED);
David Howells17926a72007-04-26 15:48:28 -0700603
David Howells42886ff2016-06-16 13:31:07 +0100604 epoch = sp->hdr.epoch;
605 cid = sp->hdr.cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700606
607 /* search the connection list first */
608 read_lock_bh(&trans->conn_lock);
609
610 p = trans->server_conns.rb_node;
611 while (p) {
612 conn = rb_entry(p, struct rxrpc_connection, node);
613
David Howells19ffa012016-04-04 14:00:36 +0100614 _debug("maybe %x", conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700615
David Howells19ffa012016-04-04 14:00:36 +0100616 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700617 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100618 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700619 p = p->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100620 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700621 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100622 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700623 p = p->rb_right;
624 else
625 goto found_extant_connection;
626 }
627 read_unlock_bh(&trans->conn_lock);
628
629 /* not yet present - create a candidate for a new record and then
630 * redo the search */
David Howells843099c2016-04-07 17:23:37 +0100631 candidate = rxrpc_alloc_connection(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700632 if (!candidate) {
633 _leave(" = -ENOMEM");
634 return ERR_PTR(-ENOMEM);
635 }
636
David Howells42886ff2016-06-16 13:31:07 +0100637 candidate->trans = trans;
638 candidate->proto.local = trans->local;
639 candidate->proto.epoch = sp->hdr.epoch;
640 candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK;
641 candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED;
642 candidate->params.local = trans->local;
643 candidate->params.peer = trans->peer;
644 candidate->params.service_id = sp->hdr.serviceId;
645 candidate->security_ix = sp->hdr.securityIndex;
646 candidate->out_clientflag = 0;
647 candidate->state = RXRPC_CONN_SERVER;
David Howells19ffa012016-04-04 14:00:36 +0100648 if (candidate->params.service_id)
David Howells42886ff2016-06-16 13:31:07 +0100649 candidate->state = RXRPC_CONN_SERVER_UNSECURED;
David Howells17926a72007-04-26 15:48:28 -0700650
651 write_lock_bh(&trans->conn_lock);
652
653 pp = &trans->server_conns.rb_node;
654 p = NULL;
655 while (*pp) {
656 p = *pp;
657 conn = rb_entry(p, struct rxrpc_connection, node);
658
David Howells19ffa012016-04-04 14:00:36 +0100659 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700660 pp = &(*pp)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100661 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700662 pp = &(*pp)->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100663 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700664 pp = &(*pp)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100665 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700666 pp = &(*pp)->rb_right;
667 else
668 goto found_extant_second;
669 }
670
671 /* we can now add the new candidate to the list */
672 conn = candidate;
673 candidate = NULL;
674 rb_link_node(&conn->node, p, pp);
675 rb_insert_color(&conn->node, &trans->server_conns);
676 atomic_inc(&conn->trans->usage);
677
678 write_unlock_bh(&trans->conn_lock);
679
680 write_lock_bh(&rxrpc_connection_lock);
681 list_add_tail(&conn->link, &rxrpc_connections);
682 write_unlock_bh(&rxrpc_connection_lock);
683
684 new = "new";
685
686success:
David Howells19ffa012016-04-04 14:00:36 +0100687 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700688
689 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
690 return conn;
691
692 /* we found the connection in the list immediately */
693found_extant_connection:
David Howells42886ff2016-06-16 13:31:07 +0100694 if (sp->hdr.securityIndex != conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700695 read_unlock_bh(&trans->conn_lock);
696 goto security_mismatch;
697 }
698 atomic_inc(&conn->usage);
699 read_unlock_bh(&trans->conn_lock);
700 goto success;
701
702 /* we found the connection on the second time through the list */
703found_extant_second:
David Howells42886ff2016-06-16 13:31:07 +0100704 if (sp->hdr.securityIndex != conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700705 write_unlock_bh(&trans->conn_lock);
706 goto security_mismatch;
707 }
708 atomic_inc(&conn->usage);
709 write_unlock_bh(&trans->conn_lock);
710 kfree(candidate);
711 goto success;
712
713security_mismatch:
714 kfree(candidate);
715 _leave(" = -EKEYREJECTED");
716 return ERR_PTR(-EKEYREJECTED);
717}
718
719/*
720 * find a connection based on transport and RxRPC connection ID for an incoming
721 * packet
722 */
723struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
David Howells42886ff2016-06-16 13:31:07 +0100724 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700725{
726 struct rxrpc_connection *conn;
David Howells42886ff2016-06-16 13:31:07 +0100727 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700728 struct rb_node *p;
David Howells0d12f8a2016-03-04 15:53:46 +0000729 u32 epoch, cid;
David Howells17926a72007-04-26 15:48:28 -0700730
David Howells42886ff2016-06-16 13:31:07 +0100731 _enter(",{%x,%x}", sp->hdr.cid, sp->hdr.flags);
David Howells17926a72007-04-26 15:48:28 -0700732
733 read_lock_bh(&trans->conn_lock);
734
David Howells42886ff2016-06-16 13:31:07 +0100735 cid = sp->hdr.cid & RXRPC_CIDMASK;
736 epoch = sp->hdr.epoch;
David Howells17926a72007-04-26 15:48:28 -0700737
David Howells42886ff2016-06-16 13:31:07 +0100738 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
David Howells17926a72007-04-26 15:48:28 -0700739 p = trans->server_conns.rb_node;
740 else
741 p = trans->client_conns.rb_node;
742
743 while (p) {
744 conn = rb_entry(p, struct rxrpc_connection, node);
745
David Howells19ffa012016-04-04 14:00:36 +0100746 _debug("maybe %x", conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700747
David Howells19ffa012016-04-04 14:00:36 +0100748 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700749 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100750 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700751 p = p->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100752 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700753 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100754 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700755 p = p->rb_right;
756 else
757 goto found;
758 }
759
760 read_unlock_bh(&trans->conn_lock);
761 _leave(" = NULL");
762 return NULL;
763
764found:
765 atomic_inc(&conn->usage);
766 read_unlock_bh(&trans->conn_lock);
767 _leave(" = %p", conn);
768 return conn;
769}
770
771/*
772 * release a virtual connection
773 */
774void rxrpc_put_connection(struct rxrpc_connection *conn)
775{
776 _enter("%p{u=%d,d=%d}",
777 conn, atomic_read(&conn->usage), conn->debug_id);
778
779 ASSERTCMP(atomic_read(&conn->usage), >, 0);
780
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200781 conn->put_time = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700782 if (atomic_dec_and_test(&conn->usage)) {
783 _debug("zombie");
David Howells651350d2007-04-26 15:50:17 -0700784 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700785 }
786
787 _leave("");
788}
789
790/*
791 * destroy a virtual connection
792 */
793static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
794{
795 _enter("%p{%d}", conn, atomic_read(&conn->usage));
796
797 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
798
799 _net("DESTROY CONN %d", conn->debug_id);
800
801 if (conn->bundle)
802 rxrpc_put_bundle(conn->trans, conn->bundle);
803
804 ASSERT(RB_EMPTY_ROOT(&conn->calls));
805 rxrpc_purge_queue(&conn->rx_queue);
806
David Howellse0e4d822016-04-07 17:23:58 +0100807 conn->security->clear(conn);
David Howells19ffa012016-04-04 14:00:36 +0100808 key_put(conn->params.key);
David Howellse0e4d822016-04-07 17:23:58 +0100809 key_put(conn->server_key);
810
David Howells17926a72007-04-26 15:48:28 -0700811 rxrpc_put_transport(conn->trans);
812 kfree(conn);
813 _leave("");
814}
815
816/*
817 * reap dead connections
818 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800819static void rxrpc_connection_reaper(struct work_struct *work)
David Howells17926a72007-04-26 15:48:28 -0700820{
821 struct rxrpc_connection *conn, *_p;
822 unsigned long now, earliest, reap_time;
823
824 LIST_HEAD(graveyard);
825
826 _enter("");
827
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200828 now = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700829 earliest = ULONG_MAX;
830
831 write_lock_bh(&rxrpc_connection_lock);
832 list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
833 _debug("reap CONN %d { u=%d,t=%ld }",
834 conn->debug_id, atomic_read(&conn->usage),
835 (long) now - (long) conn->put_time);
836
837 if (likely(atomic_read(&conn->usage) > 0))
838 continue;
839
840 spin_lock(&conn->trans->client_lock);
841 write_lock(&conn->trans->conn_lock);
David Howells5873c082014-02-07 18:58:44 +0000842 reap_time = conn->put_time + rxrpc_connection_expiry;
David Howells17926a72007-04-26 15:48:28 -0700843
844 if (atomic_read(&conn->usage) > 0) {
845 ;
846 } else if (reap_time <= now) {
847 list_move_tail(&conn->link, &graveyard);
848 if (conn->out_clientflag)
849 rb_erase(&conn->node,
850 &conn->trans->client_conns);
851 else
852 rb_erase(&conn->node,
853 &conn->trans->server_conns);
854 if (conn->bundle) {
855 list_del_init(&conn->bundle_link);
856 conn->bundle->num_conns--;
857 }
858
859 } else if (reap_time < earliest) {
860 earliest = reap_time;
861 }
862
863 write_unlock(&conn->trans->conn_lock);
864 spin_unlock(&conn->trans->client_lock);
865 }
866 write_unlock_bh(&rxrpc_connection_lock);
867
868 if (earliest != ULONG_MAX) {
869 _debug("reschedule reaper %ld", (long) earliest - now);
870 ASSERTCMP(earliest, >, now);
David Howells651350d2007-04-26 15:50:17 -0700871 rxrpc_queue_delayed_work(&rxrpc_connection_reap,
872 (earliest - now) * HZ);
David Howells17926a72007-04-26 15:48:28 -0700873 }
874
875 /* then destroy all those pulled out */
876 while (!list_empty(&graveyard)) {
877 conn = list_entry(graveyard.next, struct rxrpc_connection,
878 link);
879 list_del_init(&conn->link);
880
881 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
882 rxrpc_destroy_connection(conn);
883 }
884
885 _leave("");
886}
887
888/*
889 * preemptively destroy all the connection records rather than waiting for them
890 * to time out
891 */
892void __exit rxrpc_destroy_all_connections(void)
893{
894 _enter("");
895
David Howells5873c082014-02-07 18:58:44 +0000896 rxrpc_connection_expiry = 0;
David Howells17926a72007-04-26 15:48:28 -0700897 cancel_delayed_work(&rxrpc_connection_reap);
David Howells651350d2007-04-26 15:50:17 -0700898 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700899
900 _leave("");
901}