blob: 312b75091d29bc3b114725685b377dfab6d739a8 [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/*
David Howells17926a72007-04-26 15:48:28 -0700210 * 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/*
David Howells4a3388c2016-04-04 14:00:37 +0100243 * Allocate a client connection.
244 */
245static struct rxrpc_connection *
246rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp,
247 struct rxrpc_transport *trans,
248 gfp_t gfp)
249{
250 struct rxrpc_connection *conn;
251 int ret;
252
253 _enter("");
254
255 conn = rxrpc_alloc_connection(gfp);
256 if (!conn) {
257 _leave(" = -ENOMEM");
258 return ERR_PTR(-ENOMEM);
259 }
260
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;
269
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;
281
282 ret = rxrpc_init_client_conn_security(conn);
283 if (ret < 0)
284 goto error_1;
285
286 conn->security->prime_packet_security(conn);
287
288 write_lock(&rxrpc_connection_lock);
289 list_add_tail(&conn->link, &rxrpc_connections);
290 write_unlock(&rxrpc_connection_lock);
291
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/*
David Howells17926a72007-04-26 15:48:28 -0700306 * connect a call on an exclusive connection
307 */
308static int rxrpc_connect_exclusive(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100309 struct rxrpc_conn_parameters *cp,
David Howells17926a72007-04-26 15:48:28 -0700310 struct rxrpc_transport *trans,
David Howells17926a72007-04-26 15:48:28 -0700311 struct rxrpc_call *call,
312 gfp_t gfp)
313{
314 struct rxrpc_connection *conn;
David Howells4a3388c2016-04-04 14:00:37 +0100315 int chan;
David Howells17926a72007-04-26 15:48:28 -0700316
317 _enter("");
318
David Howells4a3388c2016-04-04 14:00:37 +0100319 conn = rxrpc_alloc_client_connection(cp, trans, gfp);
320 if (IS_ERR(conn)) {
321 _leave(" = %ld", PTR_ERR(conn));
322 return PTR_ERR(conn);
David Howells17926a72007-04-26 15:48:28 -0700323 }
324
David Howellscc8feb82016-04-04 14:00:37 +0100325 atomic_inc(&trans->usage);
David Howells4a3388c2016-04-04 14:00:37 +0100326 conn->trans = trans;
327 conn->bundle = NULL;
David Howellscc8feb82016-04-04 14:00:37 +0100328
329 _net("CONNECT EXCL new %d on TRANS %d",
330 conn->debug_id, conn->trans->debug_id);
331
David Howellscc8feb82016-04-04 14:00:37 +0100332 /* Since no one else can use the connection, we just use the first
333 * channel.
334 */
335 chan = 0;
David Howells17926a72007-04-26 15:48:28 -0700336 atomic_inc(&conn->usage);
David Howells4a3388c2016-04-04 14:00:37 +0100337 conn->avail_calls = RXRPC_MAXCALLS - 1;
David Howells17926a72007-04-26 15:48:28 -0700338 conn->channels[chan] = call;
David Howellscc8feb82016-04-04 14:00:37 +0100339 conn->call_counter = 1;
David Howells17926a72007-04-26 15:48:28 -0700340 call->conn = conn;
341 call->channel = chan;
David Howells19ffa012016-04-04 14:00:36 +0100342 call->cid = conn->proto.cid | chan;
David Howellscc8feb82016-04-04 14:00:37 +0100343 call->call_id = 1;
David Howells17926a72007-04-26 15:48:28 -0700344
345 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000346 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700347
David Howells17926a72007-04-26 15:48:28 -0700348 rxrpc_add_call_ID_to_conn(conn, call);
349 _leave(" = 0");
350 return 0;
David Howells17926a72007-04-26 15:48:28 -0700351}
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,
David Howells19ffa012016-04-04 14:00:36 +0100358 struct rxrpc_conn_parameters *cp,
David Howells17926a72007-04-26 15:48:28 -0700359 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;
David Howells4a3388c2016-04-04 14:00:37 +0100365 int chan;
David Howells17926a72007-04-26 15:48:28 -0700366
367 DECLARE_WAITQUEUE(myself, current);
368
369 _enter("%p,%lx,", rx, call->user_call_ID);
370
David Howellscc8feb82016-04-04 14:00:37 +0100371 if (cp->exclusive)
David Howells19ffa012016-04-04 14:00:36 +0100372 return rxrpc_connect_exclusive(rx, cp, trans, call, gfp);
David Howells17926a72007-04-26 15:48:28 -0700373
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);
David Howells519d2562009-06-16 21:36:44 +0100382 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
383 list_del_init(&conn->bundle_link);
384 bundle->num_conns--;
385 continue;
386 }
David Howells17926a72007-04-26 15:48:28 -0700387 if (--conn->avail_calls == 0)
388 list_move(&conn->bundle_link,
389 &bundle->busy_conns);
David Howells651350d2007-04-26 15:50:17 -0700390 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);
David Howells17926a72007-04-26 15:48:28 -0700395 atomic_inc(&conn->usage);
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);
David Howells519d2562009-06-16 21:36:44 +0100404 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
405 list_del_init(&conn->bundle_link);
406 bundle->num_conns--;
407 continue;
408 }
David Howells651350d2007-04-26 15:50:17 -0700409 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);
David Howells17926a72007-04-26 15:48:28 -0700415 atomic_inc(&conn->usage);
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
Mel Gormand0164ad2015-11-06 16:28:21 -0800431 if (!gfpflags_allow_blocking(gfp)) {
David Howells17926a72007-04-26 15:48:28 -0700432 _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 */
David Howells4a3388c2016-04-04 14:00:37 +0100455 candidate = rxrpc_alloc_client_connection(cp, trans, gfp);
Dan Carpenter0975ecb2009-05-21 15:22:02 -0700456 if (!candidate) {
457 _leave(" = -ENOMEM");
458 return -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700459 }
460
David Howells4a3388c2016-04-04 14:00:37 +0100461 atomic_inc(&bundle->usage);
462 atomic_inc(&trans->usage);
David Howells17926a72007-04-26 15:48:28 -0700463 candidate->trans = trans;
464 candidate->bundle = bundle;
David Howells17926a72007-04-26 15:48:28 -0700465
466 spin_lock(&trans->client_lock);
467
468 list_add(&candidate->bundle_link, &bundle->unused_conns);
469 bundle->num_conns++;
David Howells17926a72007-04-26 15:48:28 -0700470
471 _net("CONNECT new %d on TRANS %d",
472 candidate->debug_id, candidate->trans->debug_id);
473
David Howells17926a72007-04-26 15:48:28 -0700474 /* 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;
David Howells651350d2007-04-26 15:50:17 -0700489 ASSERT(conn->channels[0] == NULL ||
490 conn->channels[1] == NULL ||
491 conn->channels[2] == NULL ||
492 conn->channels[3] == NULL);
David Howells17926a72007-04-26 15:48:28 -0700493 BUG();
494
495found_channel:
496 conn->channels[chan] = call;
497 call->conn = conn;
498 call->channel = chan;
David Howells19ffa012016-04-04 14:00:36 +0100499 call->cid = conn->proto.cid | chan;
David Howells0d12f8a2016-03-04 15:53:46 +0000500 call->call_id = ++conn->call_counter;
David Howells17926a72007-04-26 15:48:28 -0700501
502 _net("CONNECT client on conn %d chan %d as call %x",
David Howells0d12f8a2016-03-04 15:53:46 +0000503 conn->debug_id, chan, call->call_id);
David Howells17926a72007-04-26 15:48:28 -0700504
David Howells651350d2007-04-26 15:50:17 -0700505 ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS);
David Howells17926a72007-04-26 15:48:28 -0700506 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 *
David Howells42886ff2016-06-16 13:31:07 +0100525rxrpc_incoming_connection(struct rxrpc_transport *trans, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700526{
527 struct rxrpc_connection *conn, *candidate = NULL;
David Howells42886ff2016-06-16 13:31:07 +0100528 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700529 struct rb_node *p, **pp;
530 const char *new = "old";
531 __be32 epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000532 u32 cid;
David Howells17926a72007-04-26 15:48:28 -0700533
534 _enter("");
535
David Howells42886ff2016-06-16 13:31:07 +0100536 ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED);
David Howells17926a72007-04-26 15:48:28 -0700537
David Howells42886ff2016-06-16 13:31:07 +0100538 epoch = sp->hdr.epoch;
539 cid = sp->hdr.cid & RXRPC_CIDMASK;
David Howells17926a72007-04-26 15:48:28 -0700540
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
David Howells19ffa012016-04-04 14:00:36 +0100548 _debug("maybe %x", conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700549
David Howells19ffa012016-04-04 14:00:36 +0100550 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700551 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100552 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700553 p = p->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100554 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700555 p = p->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100556 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700557 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 */
David Howells843099c2016-04-07 17:23:37 +0100565 candidate = rxrpc_alloc_connection(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700566 if (!candidate) {
567 _leave(" = -ENOMEM");
568 return ERR_PTR(-ENOMEM);
569 }
570
David Howells42886ff2016-06-16 13:31:07 +0100571 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;
David Howells19ffa012016-04-04 14:00:36 +0100582 if (candidate->params.service_id)
David Howells42886ff2016-06-16 13:31:07 +0100583 candidate->state = RXRPC_CONN_SERVER_UNSECURED;
David Howells17926a72007-04-26 15:48:28 -0700584
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
David Howells19ffa012016-04-04 14:00:36 +0100593 if (epoch < conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700594 pp = &(*pp)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100595 else if (epoch > conn->proto.epoch)
David Howells17926a72007-04-26 15:48:28 -0700596 pp = &(*pp)->rb_right;
David Howells19ffa012016-04-04 14:00:36 +0100597 else if (cid < conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700598 pp = &(*pp)->rb_left;
David Howells19ffa012016-04-04 14:00:36 +0100599 else if (cid > conn->proto.cid)
David Howells17926a72007-04-26 15:48:28 -0700600 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
David Howellsb3f57502016-06-21 16:10:03 +0100614 write_lock(&rxrpc_connection_lock);
David Howells17926a72007-04-26 15:48:28 -0700615 list_add_tail(&conn->link, &rxrpc_connections);
David Howellsb3f57502016-06-21 16:10:03 +0100616 write_unlock(&rxrpc_connection_lock);
David Howells17926a72007-04-26 15:48:28 -0700617
618 new = "new";
619
620success:
David Howells19ffa012016-04-04 14:00:36 +0100621 _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700622
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:
David Howells42886ff2016-06-16 13:31:07 +0100628 if (sp->hdr.securityIndex != conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700629 read_unlock_bh(&trans->conn_lock);
630 goto security_mismatch;
631 }
632 atomic_inc(&conn->usage);
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:
David Howells42886ff2016-06-16 13:31:07 +0100638 if (sp->hdr.securityIndex != conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700639 write_unlock_bh(&trans->conn_lock);
640 goto security_mismatch;
641 }
642 atomic_inc(&conn->usage);
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,
David Howells42886ff2016-06-16 13:31:07 +0100658 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700659{
660 struct rxrpc_connection *conn;
David Howells42886ff2016-06-16 13:31:07 +0100661 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700662 struct rb_node *p;
David Howells0d12f8a2016-03-04 15:53:46 +0000663 u32 epoch, cid;
David Howells17926a72007-04-26 15:48:28 -0700664
David Howells42886ff2016-06-16 13:31:07 +0100665 _enter(",{%x,%x}", sp->hdr.cid, sp->hdr.flags);
David Howells17926a72007-04-26 15:48:28 -0700666
667 read_lock_bh(&trans->conn_lock);
668
David Howells42886ff2016-06-16 13:31:07 +0100669 cid = sp->hdr.cid & RXRPC_CIDMASK;
670 epoch = sp->hdr.epoch;
David Howells17926a72007-04-26 15:48:28 -0700671
David Howells4a3388c2016-04-04 14:00:37 +0100672 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) {
David Howells17926a72007-04-26 15:48:28 -0700673 p = trans->server_conns.rb_node;
David Howells4a3388c2016-04-04 14:00:37 +0100674 while (p) {
675 conn = rb_entry(p, struct rxrpc_connection, node);
David Howells17926a72007-04-26 15:48:28 -0700676
David Howells4a3388c2016-04-04 14:00:37 +0100677 _debug("maybe %x", conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -0700678
David Howells4a3388c2016-04-04 14:00:37 +0100679 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)
David Howells17926a72007-04-26 15:48:28 -0700693 goto found;
694 }
695
696 read_unlock_bh(&trans->conn_lock);
697 _leave(" = NULL");
698 return NULL;
699
700found:
701 atomic_inc(&conn->usage);
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
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200717 conn->put_time = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700718 if (atomic_dec_and_test(&conn->usage)) {
719 _debug("zombie");
David Howells651350d2007-04-26 15:50:17 -0700720 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700721 }
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
David Howellse0e4d822016-04-07 17:23:58 +0100743 conn->security->clear(conn);
David Howells19ffa012016-04-04 14:00:36 +0100744 key_put(conn->params.key);
David Howellse0e4d822016-04-07 17:23:58 +0100745 key_put(conn->server_key);
746
David Howells17926a72007-04-26 15:48:28 -0700747 rxrpc_put_transport(conn->trans);
748 kfree(conn);
749 _leave("");
750}
751
752/*
753 * reap dead connections
754 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800755static void rxrpc_connection_reaper(struct work_struct *work)
David Howells17926a72007-04-26 15:48:28 -0700756{
757 struct rxrpc_connection *conn, *_p;
758 unsigned long now, earliest, reap_time;
759
760 LIST_HEAD(graveyard);
761
762 _enter("");
763
Ksenija Stanojevic22a3f9a2015-09-17 18:12:53 +0200764 now = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700765 earliest = ULONG_MAX;
766
David Howellsb3f57502016-06-21 16:10:03 +0100767 write_lock(&rxrpc_connection_lock);
David Howells17926a72007-04-26 15:48:28 -0700768 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);
David Howellsb3f57502016-06-21 16:10:03 +0100777 write_lock_bh(&conn->trans->conn_lock);
David Howells5873c082014-02-07 18:58:44 +0000778 reap_time = conn->put_time + rxrpc_connection_expiry;
David Howells17926a72007-04-26 15:48:28 -0700779
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)
David Howells4a3388c2016-04-04 14:00:37 +0100785 rxrpc_put_client_connection_id(conn);
David Howells17926a72007-04-26 15:48:28 -0700786 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
David Howellsb3f57502016-06-21 16:10:03 +0100798 write_unlock_bh(&conn->trans->conn_lock);
David Howells17926a72007-04-26 15:48:28 -0700799 spin_unlock(&conn->trans->client_lock);
800 }
David Howellsb3f57502016-06-21 16:10:03 +0100801 write_unlock(&rxrpc_connection_lock);
David Howells17926a72007-04-26 15:48:28 -0700802
803 if (earliest != ULONG_MAX) {
804 _debug("reschedule reaper %ld", (long) earliest - now);
805 ASSERTCMP(earliest, >, now);
David Howells651350d2007-04-26 15:50:17 -0700806 rxrpc_queue_delayed_work(&rxrpc_connection_reap,
807 (earliest - now) * HZ);
David Howells17926a72007-04-26 15:48:28 -0700808 }
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
David Howells5873c082014-02-07 18:58:44 +0000831 rxrpc_connection_expiry = 0;
David Howells17926a72007-04-26 15:48:28 -0700832 cancel_delayed_work(&rxrpc_connection_reap);
David Howells651350d2007-04-26 15:50:17 -0700833 rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0);
David Howells17926a72007-04-26 15:48:28 -0700834
835 _leave("");
836}