]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/conn_object.c
rxrpc: Provide more refcount helper functions
[mirror_ubuntu-bionic-kernel.git] / net / rxrpc / conn_object.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79 14#include <linux/module.h>
5a0e3ad6 15#include <linux/slab.h>
17926a79
DH
16#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
5873c083
DH
23/*
24 * Time till a connection expires after last use (in seconds).
25 */
dad8aff7 26unsigned int rxrpc_connection_expiry = 10 * 60;
5873c083 27
17926a79
DH
28static void rxrpc_connection_reaper(struct work_struct *work);
29
30LIST_HEAD(rxrpc_connections);
31DEFINE_RWLOCK(rxrpc_connection_lock);
17926a79
DH
32static 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,
0d12f8a4 62 struct key *key, u16 service_id)
17926a79
DH
63{
64 return (bundle->service_id - service_id) ?:
0d12f8a4 65 ((unsigned long)bundle->key - (unsigned long)key);
17926a79
DH
66}
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,
0d12f8a4 74 u16 service_id,
17926a79
DH
75 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,",
0d12f8a4 81 rx, key_serial(key), trans->debug_id, service_id);
17926a79 82
17926a79
DH
83 /* 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);
17926a79
DH
136 _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);
17926a79
DH
144 _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);
17926a79
DH
153 _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);
e0e4d82f 195 conn->security = &rxrpc_no_security;
17926a79
DH
196 rwlock_init(&conn->lock);
197 spin_lock_init(&conn->state_lock);
198 atomic_set(&conn->usage, 1);
199 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
200 conn->avail_calls = RXRPC_MAXCALLS;
201 conn->size_align = 4;
0d12f8a4 202 conn->header_size = sizeof(struct rxrpc_wire_header);
17926a79
DH
203 }
204
16c61add 205 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
17926a79
DH
206 return conn;
207}
208
17926a79
DH
209/*
210 * add a call to a connection's call-by-ID tree
211 */
212static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection *conn,
213 struct rxrpc_call *call)
214{
215 struct rxrpc_call *xcall;
216 struct rb_node *parent, **p;
217 __be32 call_id;
218
219 write_lock_bh(&conn->lock);
220
221 call_id = call->call_id;
222 p = &conn->calls.rb_node;
223 parent = NULL;
224 while (*p) {
225 parent = *p;
226 xcall = rb_entry(parent, struct rxrpc_call, conn_node);
227
228 if (call_id < xcall->call_id)
229 p = &(*p)->rb_left;
230 else if (call_id > xcall->call_id)
231 p = &(*p)->rb_right;
232 else
233 BUG();
234 }
235
236 rb_link_node(&call->conn_node, parent, p);
237 rb_insert_color(&call->conn_node, &conn->calls);
238
239 write_unlock_bh(&conn->lock);
240}
241
242/*
4a3388c8 243 * Allocate a client connection.
17926a79 244 */
4a3388c8
DH
245static struct rxrpc_connection *
246rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp,
247 struct rxrpc_transport *trans,
248 gfp_t gfp)
17926a79
DH
249{
250 struct rxrpc_connection *conn;
4a3388c8 251 int ret;
17926a79
DH
252
253 _enter("");
254
cc8feb8e 255 conn = rxrpc_alloc_connection(gfp);
17926a79 256 if (!conn) {
cc8feb8e 257 _leave(" = -ENOMEM");
4a3388c8 258 return ERR_PTR(-ENOMEM);
cc8feb8e 259 }
17926a79 260
cc8feb8e
DH
261 conn->params = *cp;
262 conn->proto.local = cp->local;
263 conn->proto.epoch = rxrpc_epoch;
264 conn->proto.cid = 0;
265 conn->proto.in_clientflag = 0;
266 conn->proto.family = cp->peer->srx.transport.family;
267 conn->out_clientflag = RXRPC_CLIENT_INITIATED;
268 conn->state = RXRPC_CONN_CLIENT;
cc8feb8e 269
4a3388c8
DH
270 switch (conn->proto.family) {
271 case AF_INET:
272 conn->proto.addr_size = sizeof(conn->proto.ipv4_addr);
273 conn->proto.ipv4_addr = cp->peer->srx.transport.sin.sin_addr;
274 conn->proto.port = cp->peer->srx.transport.sin.sin_port;
275 break;
276 }
277
278 ret = rxrpc_get_client_connection_id(conn, trans, gfp);
279 if (ret < 0)
280 goto error_0;
cc8feb8e
DH
281
282 ret = rxrpc_init_client_conn_security(conn);
4a3388c8
DH
283 if (ret < 0)
284 goto error_1;
285
286 conn->security->prime_packet_security(conn);
17926a79 287
b3f57504 288 write_lock(&rxrpc_connection_lock);
cc8feb8e 289 list_add_tail(&conn->link, &rxrpc_connections);
b3f57504 290 write_unlock(&rxrpc_connection_lock);
17926a79 291
4a3388c8
DH
292 key_get(conn->params.key);
293
294 _leave(" = %p", conn);
295 return conn;
296
297error_1:
298 rxrpc_put_client_connection_id(conn);
299error_0:
300 kfree(conn);
301 _leave(" = %d", ret);
302 return ERR_PTR(ret);
303}
304
305/*
306 * connect a call on an exclusive connection
307 */
308static int rxrpc_connect_exclusive(struct rxrpc_sock *rx,
309 struct rxrpc_conn_parameters *cp,
310 struct rxrpc_transport *trans,
311 struct rxrpc_call *call,
312 gfp_t gfp)
313{
314 struct rxrpc_connection *conn;
315 int chan;
316
317 _enter("");
318
319 conn = rxrpc_alloc_client_connection(cp, trans, gfp);
320 if (IS_ERR(conn)) {
321 _leave(" = %ld", PTR_ERR(conn));
322 return PTR_ERR(conn);
323 }
324
cc8feb8e 325 atomic_inc(&trans->usage);
4a3388c8
DH
326 conn->trans = trans;
327 conn->bundle = NULL;
17926a79 328
cc8feb8e
DH
329 _net("CONNECT EXCL new %d on TRANS %d",
330 conn->debug_id, conn->trans->debug_id);
17926a79 331
cc8feb8e
DH
332 /* Since no one else can use the connection, we just use the first
333 * channel.
17926a79 334 */
cc8feb8e 335 chan = 0;
5627cc8b 336 rxrpc_get_connection(conn);
4a3388c8 337 conn->avail_calls = RXRPC_MAXCALLS - 1;
17926a79 338 conn->channels[chan] = call;
cc8feb8e 339 conn->call_counter = 1;
17926a79
DH
340 call->conn = conn;
341 call->channel = chan;
19ffa01c 342 call->cid = conn->proto.cid | chan;
cc8feb8e 343 call->call_id = 1;
17926a79
DH
344
345 _net("CONNECT client on conn %d chan %d as call %x",
0d12f8a4 346 conn->debug_id, chan, call->call_id);
17926a79 347
17926a79
DH
348 rxrpc_add_call_ID_to_conn(conn, call);
349 _leave(" = 0");
350 return 0;
17926a79
DH
351}
352
353/*
354 * find a connection for a call
355 * - called in process context with IRQs enabled
356 */
357int rxrpc_connect_call(struct rxrpc_sock *rx,
19ffa01c 358 struct rxrpc_conn_parameters *cp,
17926a79
DH
359 struct rxrpc_transport *trans,
360 struct rxrpc_conn_bundle *bundle,
361 struct rxrpc_call *call,
362 gfp_t gfp)
363{
364 struct rxrpc_connection *conn, *candidate;
4a3388c8 365 int chan;
17926a79
DH
366
367 DECLARE_WAITQUEUE(myself, current);
368
369 _enter("%p,%lx,", rx, call->user_call_ID);
370
cc8feb8e 371 if (cp->exclusive)
19ffa01c 372 return rxrpc_connect_exclusive(rx, cp, trans, call, gfp);
17926a79
DH
373
374 spin_lock(&trans->client_lock);
375 for (;;) {
376 /* see if the bundle has a call slot available */
377 if (!list_empty(&bundle->avail_conns)) {
378 _debug("avail");
379 conn = list_entry(bundle->avail_conns.next,
380 struct rxrpc_connection,
381 bundle_link);
519d2567
DH
382 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
383 list_del_init(&conn->bundle_link);
384 bundle->num_conns--;
385 continue;
386 }
17926a79
DH
387 if (--conn->avail_calls == 0)
388 list_move(&conn->bundle_link,
389 &bundle->busy_conns);
651350d1
DH
390 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
391 ASSERT(conn->channels[0] == NULL ||
392 conn->channels[1] == NULL ||
393 conn->channels[2] == NULL ||
394 conn->channels[3] == NULL);
5627cc8b 395 rxrpc_get_connection(conn);
17926a79
DH
396 break;
397 }
398
399 if (!list_empty(&bundle->unused_conns)) {
400 _debug("unused");
401 conn = list_entry(bundle->unused_conns.next,
402 struct rxrpc_connection,
403 bundle_link);
519d2567
DH
404 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
405 list_del_init(&conn->bundle_link);
406 bundle->num_conns--;
407 continue;
408 }
651350d1
DH
409 ASSERTCMP(conn->avail_calls, ==, RXRPC_MAXCALLS);
410 conn->avail_calls = RXRPC_MAXCALLS - 1;
411 ASSERT(conn->channels[0] == NULL &&
412 conn->channels[1] == NULL &&
413 conn->channels[2] == NULL &&
414 conn->channels[3] == NULL);
5627cc8b 415 rxrpc_get_connection(conn);
17926a79
DH
416 list_move(&conn->bundle_link, &bundle->avail_conns);
417 break;
418 }
419
420 /* need to allocate a new connection */
421 _debug("get new conn [%d]", bundle->num_conns);
422
423 spin_unlock(&trans->client_lock);
424
425 if (signal_pending(current))
426 goto interrupted;
427
428 if (bundle->num_conns >= 20) {
429 _debug("too many conns");
430
d0164adc 431 if (!gfpflags_allow_blocking(gfp)) {
17926a79
DH
432 _leave(" = -EAGAIN");
433 return -EAGAIN;
434 }
435
436 add_wait_queue(&bundle->chanwait, &myself);
437 for (;;) {
438 set_current_state(TASK_INTERRUPTIBLE);
439 if (bundle->num_conns < 20 ||
440 !list_empty(&bundle->unused_conns) ||
441 !list_empty(&bundle->avail_conns))
442 break;
443 if (signal_pending(current))
444 goto interrupted_dequeue;
445 schedule();
446 }
447 remove_wait_queue(&bundle->chanwait, &myself);
448 __set_current_state(TASK_RUNNING);
449 spin_lock(&trans->client_lock);
450 continue;
451 }
452
453 /* not yet present - create a candidate for a new connection and then
454 * redo the check */
4a3388c8 455 candidate = rxrpc_alloc_client_connection(cp, trans, gfp);
0975ecba
DC
456 if (!candidate) {
457 _leave(" = -ENOMEM");
458 return -ENOMEM;
17926a79
DH
459 }
460
4a3388c8
DH
461 atomic_inc(&bundle->usage);
462 atomic_inc(&trans->usage);
17926a79
DH
463 candidate->trans = trans;
464 candidate->bundle = bundle;
17926a79
DH
465
466 spin_lock(&trans->client_lock);
467
468 list_add(&candidate->bundle_link, &bundle->unused_conns);
469 bundle->num_conns++;
17926a79
DH
470
471 _net("CONNECT new %d on TRANS %d",
472 candidate->debug_id, candidate->trans->debug_id);
473
17926a79
DH
474 /* leave the candidate lurking in zombie mode attached to the
475 * bundle until we're ready for it */
476 rxrpc_put_connection(candidate);
477 candidate = NULL;
478 }
479
480 /* we've got a connection with a free channel and we can now attach the
481 * call to it
482 * - we're holding the transport's client lock
483 * - we're holding a reference on the connection
484 * - we're holding a reference on the bundle
485 */
486 for (chan = 0; chan < RXRPC_MAXCALLS; chan++)
487 if (!conn->channels[chan])
488 goto found_channel;
651350d1
DH
489 ASSERT(conn->channels[0] == NULL ||
490 conn->channels[1] == NULL ||
491 conn->channels[2] == NULL ||
492 conn->channels[3] == NULL);
17926a79
DH
493 BUG();
494
495found_channel:
496 conn->channels[chan] = call;
497 call->conn = conn;
498 call->channel = chan;
19ffa01c 499 call->cid = conn->proto.cid | chan;
0d12f8a4 500 call->call_id = ++conn->call_counter;
17926a79
DH
501
502 _net("CONNECT client on conn %d chan %d as call %x",
0d12f8a4 503 conn->debug_id, chan, call->call_id);
17926a79 504
651350d1 505 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
17926a79
DH
506 spin_unlock(&trans->client_lock);
507
508 rxrpc_add_call_ID_to_conn(conn, call);
509
510 _leave(" = 0");
511 return 0;
512
513interrupted_dequeue:
514 remove_wait_queue(&bundle->chanwait, &myself);
515 __set_current_state(TASK_RUNNING);
516interrupted:
517 _leave(" = -ERESTARTSYS");
518 return -ERESTARTSYS;
519}
520
521/*
522 * get a record of an incoming connection
523 */
524struct rxrpc_connection *
42886ffe 525rxrpc_incoming_connection(struct rxrpc_transport *trans, struct sk_buff *skb)
17926a79
DH
526{
527 struct rxrpc_connection *conn, *candidate = NULL;
42886ffe 528 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79
DH
529 struct rb_node *p, **pp;
530 const char *new = "old";
531 __be32 epoch;
0d12f8a4 532 u32 cid;
17926a79
DH
533
534 _enter("");
535
42886ffe 536 ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED);
17926a79 537
42886ffe
DH
538 epoch = sp->hdr.epoch;
539 cid = sp->hdr.cid & RXRPC_CIDMASK;
17926a79
DH
540
541 /* search the connection list first */
542 read_lock_bh(&trans->conn_lock);
543
544 p = trans->server_conns.rb_node;
545 while (p) {
546 conn = rb_entry(p, struct rxrpc_connection, node);
547
19ffa01c 548 _debug("maybe %x", conn->proto.cid);
17926a79 549
19ffa01c 550 if (epoch < conn->proto.epoch)
17926a79 551 p = p->rb_left;
19ffa01c 552 else if (epoch > conn->proto.epoch)
17926a79 553 p = p->rb_right;
19ffa01c 554 else if (cid < conn->proto.cid)
17926a79 555 p = p->rb_left;
19ffa01c 556 else if (cid > conn->proto.cid)
17926a79
DH
557 p = p->rb_right;
558 else
559 goto found_extant_connection;
560 }
561 read_unlock_bh(&trans->conn_lock);
562
563 /* not yet present - create a candidate for a new record and then
564 * redo the search */
843099ca 565 candidate = rxrpc_alloc_connection(GFP_NOIO);
17926a79
DH
566 if (!candidate) {
567 _leave(" = -ENOMEM");
568 return ERR_PTR(-ENOMEM);
569 }
570
42886ffe
DH
571 candidate->trans = trans;
572 candidate->proto.local = trans->local;
573 candidate->proto.epoch = sp->hdr.epoch;
574 candidate->proto.cid = sp->hdr.cid & RXRPC_CIDMASK;
575 candidate->proto.in_clientflag = RXRPC_CLIENT_INITIATED;
576 candidate->params.local = trans->local;
577 candidate->params.peer = trans->peer;
578 candidate->params.service_id = sp->hdr.serviceId;
579 candidate->security_ix = sp->hdr.securityIndex;
580 candidate->out_clientflag = 0;
581 candidate->state = RXRPC_CONN_SERVER;
19ffa01c 582 if (candidate->params.service_id)
42886ffe 583 candidate->state = RXRPC_CONN_SERVER_UNSECURED;
17926a79
DH
584
585 write_lock_bh(&trans->conn_lock);
586
587 pp = &trans->server_conns.rb_node;
588 p = NULL;
589 while (*pp) {
590 p = *pp;
591 conn = rb_entry(p, struct rxrpc_connection, node);
592
19ffa01c 593 if (epoch < conn->proto.epoch)
17926a79 594 pp = &(*pp)->rb_left;
19ffa01c 595 else if (epoch > conn->proto.epoch)
17926a79 596 pp = &(*pp)->rb_right;
19ffa01c 597 else if (cid < conn->proto.cid)
17926a79 598 pp = &(*pp)->rb_left;
19ffa01c 599 else if (cid > conn->proto.cid)
17926a79
DH
600 pp = &(*pp)->rb_right;
601 else
602 goto found_extant_second;
603 }
604
605 /* we can now add the new candidate to the list */
606 conn = candidate;
607 candidate = NULL;
608 rb_link_node(&conn->node, p, pp);
609 rb_insert_color(&conn->node, &trans->server_conns);
610 atomic_inc(&conn->trans->usage);
611
612 write_unlock_bh(&trans->conn_lock);
613
b3f57504 614 write_lock(&rxrpc_connection_lock);
17926a79 615 list_add_tail(&conn->link, &rxrpc_connections);
b3f57504 616 write_unlock(&rxrpc_connection_lock);
17926a79
DH
617
618 new = "new";
619
620success:
19ffa01c 621 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
17926a79
DH
622
623 _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
624 return conn;
625
626 /* we found the connection in the list immediately */
627found_extant_connection:
42886ffe 628 if (sp->hdr.securityIndex != conn->security_ix) {
17926a79
DH
629 read_unlock_bh(&trans->conn_lock);
630 goto security_mismatch;
631 }
5627cc8b 632 rxrpc_get_connection(conn);
17926a79
DH
633 read_unlock_bh(&trans->conn_lock);
634 goto success;
635
636 /* we found the connection on the second time through the list */
637found_extant_second:
42886ffe 638 if (sp->hdr.securityIndex != conn->security_ix) {
17926a79
DH
639 write_unlock_bh(&trans->conn_lock);
640 goto security_mismatch;
641 }
5627cc8b 642 rxrpc_get_connection(conn);
17926a79
DH
643 write_unlock_bh(&trans->conn_lock);
644 kfree(candidate);
645 goto success;
646
647security_mismatch:
648 kfree(candidate);
649 _leave(" = -EKEYREJECTED");
650 return ERR_PTR(-EKEYREJECTED);
651}
652
653/*
654 * find a connection based on transport and RxRPC connection ID for an incoming
655 * packet
656 */
657struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans,
42886ffe 658 struct sk_buff *skb)
17926a79
DH
659{
660 struct rxrpc_connection *conn;
42886ffe 661 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 662 struct rb_node *p;
0d12f8a4 663 u32 epoch, cid;
17926a79 664
42886ffe 665 _enter(",{%x,%x}", sp->hdr.cid, sp->hdr.flags);
17926a79
DH
666
667 read_lock_bh(&trans->conn_lock);
668
42886ffe
DH
669 cid = sp->hdr.cid & RXRPC_CIDMASK;
670 epoch = sp->hdr.epoch;
17926a79 671
4a3388c8 672 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) {
17926a79 673 p = trans->server_conns.rb_node;
4a3388c8
DH
674 while (p) {
675 conn = rb_entry(p, struct rxrpc_connection, node);
676
677 _debug("maybe %x", conn->proto.cid);
678
679 if (epoch < conn->proto.epoch)
680 p = p->rb_left;
681 else if (epoch > conn->proto.epoch)
682 p = p->rb_right;
683 else if (cid < conn->proto.cid)
684 p = p->rb_left;
685 else if (cid > conn->proto.cid)
686 p = p->rb_right;
687 else
688 goto found;
689 }
690 } else {
691 conn = idr_find(&rxrpc_client_conn_ids, cid >> RXRPC_CIDSHIFT);
692 if (conn && conn->proto.epoch == epoch)
17926a79
DH
693 goto found;
694 }
695
696 read_unlock_bh(&trans->conn_lock);
697 _leave(" = NULL");
698 return NULL;
699
700found:
5627cc8b 701 rxrpc_get_connection(conn);
17926a79
DH
702 read_unlock_bh(&trans->conn_lock);
703 _leave(" = %p", conn);
704 return conn;
705}
706
707/*
708 * release a virtual connection
709 */
710void rxrpc_put_connection(struct rxrpc_connection *conn)
711{
712 _enter("%p{u=%d,d=%d}",
713 conn, atomic_read(&conn->usage), conn->debug_id);
714
715 ASSERTCMP(atomic_read(&conn->usage), >, 0);
716
22a3f9a2 717 conn->put_time = ktime_get_seconds();
17926a79
DH
718 if (atomic_dec_and_test(&conn->usage)) {
719 _debug("zombie");
651350d1 720 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
17926a79
DH
721 }
722
723 _leave("");
724}
725
726/*
727 * destroy a virtual connection
728 */
729static void rxrpc_destroy_connection(struct rxrpc_connection *conn)
730{
731 _enter("%p{%d}", conn, atomic_read(&conn->usage));
732
733 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
734
735 _net("DESTROY CONN %d", conn->debug_id);
736
737 if (conn->bundle)
738 rxrpc_put_bundle(conn->trans, conn->bundle);
739
740 ASSERT(RB_EMPTY_ROOT(&conn->calls));
741 rxrpc_purge_queue(&conn->rx_queue);
742
e0e4d82f 743 conn->security->clear(conn);
19ffa01c 744 key_put(conn->params.key);
e0e4d82f
DH
745 key_put(conn->server_key);
746
17926a79
DH
747 rxrpc_put_transport(conn->trans);
748 kfree(conn);
749 _leave("");
750}
751
752/*
753 * reap dead connections
754 */
5eaa65b2 755static void rxrpc_connection_reaper(struct work_struct *work)
17926a79
DH
756{
757 struct rxrpc_connection *conn, *_p;
758 unsigned long now, earliest, reap_time;
759
760 LIST_HEAD(graveyard);
761
762 _enter("");
763
22a3f9a2 764 now = ktime_get_seconds();
17926a79
DH
765 earliest = ULONG_MAX;
766
b3f57504 767 write_lock(&rxrpc_connection_lock);
17926a79
DH
768 list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) {
769 _debug("reap CONN %d { u=%d,t=%ld }",
770 conn->debug_id, atomic_read(&conn->usage),
771 (long) now - (long) conn->put_time);
772
773 if (likely(atomic_read(&conn->usage) > 0))
774 continue;
775
776 spin_lock(&conn->trans->client_lock);
b3f57504 777 write_lock_bh(&conn->trans->conn_lock);
5873c083 778 reap_time = conn->put_time + rxrpc_connection_expiry;
17926a79
DH
779
780 if (atomic_read(&conn->usage) > 0) {
781 ;
782 } else if (reap_time <= now) {
783 list_move_tail(&conn->link, &graveyard);
784 if (conn->out_clientflag)
4a3388c8 785 rxrpc_put_client_connection_id(conn);
17926a79
DH
786 else
787 rb_erase(&conn->node,
788 &conn->trans->server_conns);
789 if (conn->bundle) {
790 list_del_init(&conn->bundle_link);
791 conn->bundle->num_conns--;
792 }
793
794 } else if (reap_time < earliest) {
795 earliest = reap_time;
796 }
797
b3f57504 798 write_unlock_bh(&conn->trans->conn_lock);
17926a79
DH
799 spin_unlock(&conn->trans->client_lock);
800 }
b3f57504 801 write_unlock(&rxrpc_connection_lock);
17926a79
DH
802
803 if (earliest != ULONG_MAX) {
804 _debug("reschedule reaper %ld", (long) earliest - now);
805 ASSERTCMP(earliest, >, now);
651350d1
DH
806 rxrpc_queue_delayed_work(&rxrpc_connection_reap,
807 (earliest - now) * HZ);
17926a79
DH
808 }
809
810 /* then destroy all those pulled out */
811 while (!list_empty(&graveyard)) {
812 conn = list_entry(graveyard.next, struct rxrpc_connection,
813 link);
814 list_del_init(&conn->link);
815
816 ASSERTCMP(atomic_read(&conn->usage), ==, 0);
817 rxrpc_destroy_connection(conn);
818 }
819
820 _leave("");
821}
822
823/*
824 * preemptively destroy all the connection records rather than waiting for them
825 * to time out
826 */
827void __exit rxrpc_destroy_all_connections(void)
828{
829 _enter("");
830
5873c083 831 rxrpc_connection_expiry = 0;
17926a79 832 cancel_delayed_work(&rxrpc_connection_reap);
651350d1 833 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
17926a79
DH
834
835 _leave("");
836}