David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Time till a connection expires after last use (in seconds). |
| 25 | */ |
David Howells | dad8aff | 2016-03-09 23:22:56 +0000 | [diff] [blame] | 26 | unsigned int rxrpc_connection_expiry = 10 * 60; |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 27 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 28 | static void rxrpc_connection_reaper(struct work_struct *work); |
| 29 | |
| 30 | LIST_HEAD(rxrpc_connections); |
| 31 | DEFINE_RWLOCK(rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 32 | static DECLARE_DELAYED_WORK(rxrpc_connection_reap, rxrpc_connection_reaper); |
| 33 | |
| 34 | /* |
| 35 | * allocate a new client connection bundle |
| 36 | */ |
| 37 | static 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 | */ |
| 60 | static inline |
| 61 | int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle *bundle, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 62 | struct key *key, u16 service_id) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 63 | { |
| 64 | return (bundle->service_id - service_id) ?: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 65 | ((unsigned long)bundle->key - (unsigned long)key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * get bundle of client connections that a client socket can make use of |
| 70 | */ |
| 71 | struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *rx, |
| 72 | struct rxrpc_transport *trans, |
| 73 | struct key *key, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 74 | u16 service_id, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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,", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 81 | rx, key_serial(key), trans->debug_id, service_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 82 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 136 | _leave(" = %p [new]", bundle); |
| 137 | return bundle; |
| 138 | |
| 139 | /* we found the bundle in the list immediately */ |
| 140 | found_extant_bundle: |
| 141 | atomic_inc(&bundle->usage); |
| 142 | spin_unlock(&trans->client_lock); |
| 143 | _net("BUNDLE old on trans %d", trans->debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 */ |
| 148 | found_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 Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 153 | _leave(" = %p [second %d]", bundle, atomic_read(&bundle->usage)); |
| 154 | return bundle; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * release a bundle |
| 159 | */ |
| 160 | void 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 | */ |
| 183 | static 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 Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 195 | conn->security = &rxrpc_no_security; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 202 | conn->header_size = sizeof(struct rxrpc_wire_header); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Adrian Bunk | 16c61ad | 2007-06-15 15:15:43 -0700 | [diff] [blame] | 205 | _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 206 | 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 | */ |
| 214 | static 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 Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 219 | u32 cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 220 | |
| 221 | _enter(""); |
| 222 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 223 | epoch = conn->proto.epoch; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 224 | |
| 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 Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 230 | cid = conn->trans->conn_idcounter; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 231 | |
| 232 | attempt_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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 240 | if (epoch < xconn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 241 | p = &(*p)->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 242 | else if (epoch > xconn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 243 | p = &(*p)->rb_right; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 244 | else if (cid < xconn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 245 | p = &(*p)->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 246 | else if (cid > xconn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 247 | 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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 257 | conn->proto.cid = cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 258 | write_unlock_bh(&conn->trans->conn_lock); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 259 | _leave(" [CID %x]", cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 260 | return; |
| 261 | |
| 262 | /* we found a connection with the proposed ID - walk the tree from that |
| 263 | * point looking for the next unused ID */ |
| 264 | id_exists: |
| 265 | for (;;) { |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 266 | cid += RXRPC_CID_INC; |
| 267 | if (cid < RXRPC_CID_INC) { |
| 268 | cid = RXRPC_CID_INC; |
| 269 | conn->trans->conn_idcounter = cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 270 | 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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 278 | if (epoch < xconn->proto.epoch || |
| 279 | cid < xconn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 280 | goto attempt_insertion; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * add a call to a connection's call-by-ID tree |
| 286 | */ |
| 287 | static 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 | */ |
| 320 | static int rxrpc_connect_exclusive(struct rxrpc_sock *rx, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 321 | struct rxrpc_conn_parameters *cp, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 322 | struct rxrpc_transport *trans, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 323 | struct rxrpc_call *call, |
| 324 | gfp_t gfp) |
| 325 | { |
| 326 | struct rxrpc_connection *conn; |
| 327 | int chan, ret; |
| 328 | |
| 329 | _enter(""); |
| 330 | |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 331 | conn = rxrpc_alloc_connection(gfp); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 332 | if (!conn) { |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 333 | _leave(" = -ENOMEM"); |
| 334 | return -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 335 | } |
| 336 | |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 337 | 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 Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 348 | |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 349 | 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 Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 375 | atomic_inc(&conn->usage); |
| 376 | conn->channels[chan] = call; |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 377 | conn->call_counter = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 378 | call->conn = conn; |
| 379 | call->channel = chan; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 380 | call->cid = conn->proto.cid | chan; |
David Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 381 | call->call_id = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 382 | |
| 383 | _net("CONNECT client on conn %d chan %d as call %x", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 384 | conn->debug_id, chan, call->call_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 385 | |
| 386 | spin_unlock(&trans->client_lock); |
| 387 | |
| 388 | rxrpc_add_call_ID_to_conn(conn, call); |
| 389 | _leave(" = 0"); |
| 390 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
| 394 | * find a connection for a call |
| 395 | * - called in process context with IRQs enabled |
| 396 | */ |
| 397 | int rxrpc_connect_call(struct rxrpc_sock *rx, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 398 | struct rxrpc_conn_parameters *cp, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 399 | 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 Howells | cc8feb8 | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 411 | if (cp->exclusive) |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 412 | return rxrpc_connect_exclusive(rx, cp, trans, call, gfp); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 413 | |
| 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 Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 422 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
| 423 | list_del_init(&conn->bundle_link); |
| 424 | bundle->num_conns--; |
| 425 | continue; |
| 426 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 427 | if (--conn->avail_calls == 0) |
| 428 | list_move(&conn->bundle_link, |
| 429 | &bundle->busy_conns); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 430 | 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 Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 435 | 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 Howells | 519d256 | 2009-06-16 21:36:44 +0100 | [diff] [blame] | 444 | if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) { |
| 445 | list_del_init(&conn->bundle_link); |
| 446 | bundle->num_conns--; |
| 447 | continue; |
| 448 | } |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 449 | 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 Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 455 | 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 Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 471 | if (!gfpflags_allow_blocking(gfp)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 472 | _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 Carpenter | 0975ecb | 2009-05-21 15:22:02 -0700 | [diff] [blame] | 496 | if (!candidate) { |
| 497 | _leave(" = -ENOMEM"); |
| 498 | return -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | candidate->trans = trans; |
| 502 | candidate->bundle = bundle; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 503 | 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 Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 509 | candidate->out_clientflag = RXRPC_CLIENT_INITIATED; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 510 | candidate->state = RXRPC_CONN_CLIENT; |
| 511 | candidate->avail_calls = RXRPC_MAXCALLS; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 512 | |
| 513 | key_get(candidate->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 514 | |
| 515 | ret = rxrpc_init_client_conn_security(candidate); |
| 516 | if (ret < 0) { |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 517 | key_put(candidate->params.key); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 518 | 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 Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 538 | candidate->security->prime_packet_security(candidate); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 539 | |
| 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 Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 555 | ASSERT(conn->channels[0] == NULL || |
| 556 | conn->channels[1] == NULL || |
| 557 | conn->channels[2] == NULL || |
| 558 | conn->channels[3] == NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 559 | BUG(); |
| 560 | |
| 561 | found_channel: |
| 562 | conn->channels[chan] = call; |
| 563 | call->conn = conn; |
| 564 | call->channel = chan; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 565 | call->cid = conn->proto.cid | chan; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 566 | call->call_id = ++conn->call_counter; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 567 | |
| 568 | _net("CONNECT client on conn %d chan %d as call %x", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 569 | conn->debug_id, chan, call->call_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 570 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 571 | ASSERTCMP(conn->avail_calls, <, RXRPC_MAXCALLS); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 572 | spin_unlock(&trans->client_lock); |
| 573 | |
| 574 | rxrpc_add_call_ID_to_conn(conn, call); |
| 575 | |
| 576 | _leave(" = 0"); |
| 577 | return 0; |
| 578 | |
| 579 | interrupted_dequeue: |
| 580 | remove_wait_queue(&bundle->chanwait, &myself); |
| 581 | __set_current_state(TASK_RUNNING); |
| 582 | interrupted: |
| 583 | _leave(" = -ERESTARTSYS"); |
| 584 | return -ERESTARTSYS; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * get a record of an incoming connection |
| 589 | */ |
| 590 | struct rxrpc_connection * |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 591 | rxrpc_incoming_connection(struct rxrpc_transport *trans, struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 592 | { |
| 593 | struct rxrpc_connection *conn, *candidate = NULL; |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 594 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 595 | struct rb_node *p, **pp; |
| 596 | const char *new = "old"; |
| 597 | __be32 epoch; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 598 | u32 cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 599 | |
| 600 | _enter(""); |
| 601 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 602 | ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 603 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 604 | epoch = sp->hdr.epoch; |
| 605 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 606 | |
| 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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 614 | _debug("maybe %x", conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 615 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 616 | if (epoch < conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 617 | p = p->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 618 | else if (epoch > conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 619 | p = p->rb_right; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 620 | else if (cid < conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 621 | p = p->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 622 | else if (cid > conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 623 | 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 Howells | 843099c | 2016-04-07 17:23:37 +0100 | [diff] [blame] | 631 | candidate = rxrpc_alloc_connection(GFP_NOIO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 632 | if (!candidate) { |
| 633 | _leave(" = -ENOMEM"); |
| 634 | return ERR_PTR(-ENOMEM); |
| 635 | } |
| 636 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 637 | 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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 648 | if (candidate->params.service_id) |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 649 | candidate->state = RXRPC_CONN_SERVER_UNSECURED; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 650 | |
| 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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 659 | if (epoch < conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 660 | pp = &(*pp)->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 661 | else if (epoch > conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 662 | pp = &(*pp)->rb_right; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 663 | else if (cid < conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 664 | pp = &(*pp)->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 665 | else if (cid > conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 666 | 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 | |
| 686 | success: |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 687 | _net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 688 | |
| 689 | _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage)); |
| 690 | return conn; |
| 691 | |
| 692 | /* we found the connection in the list immediately */ |
| 693 | found_extant_connection: |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 694 | if (sp->hdr.securityIndex != conn->security_ix) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 695 | 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 */ |
| 703 | found_extant_second: |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 704 | if (sp->hdr.securityIndex != conn->security_ix) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 705 | 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 | |
| 713 | security_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 | */ |
| 723 | struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *trans, |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 724 | struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 725 | { |
| 726 | struct rxrpc_connection *conn; |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 727 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 728 | struct rb_node *p; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 729 | u32 epoch, cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 730 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 731 | _enter(",{%x,%x}", sp->hdr.cid, sp->hdr.flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 732 | |
| 733 | read_lock_bh(&trans->conn_lock); |
| 734 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 735 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 736 | epoch = sp->hdr.epoch; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 737 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame^] | 738 | if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 739 | 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 Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 746 | _debug("maybe %x", conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 747 | |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 748 | if (epoch < conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 749 | p = p->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 750 | else if (epoch > conn->proto.epoch) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 751 | p = p->rb_right; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 752 | else if (cid < conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 753 | p = p->rb_left; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 754 | else if (cid > conn->proto.cid) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 755 | 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 | |
| 764 | found: |
| 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 | */ |
| 774 | void 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 Stanojevic | 22a3f9a | 2015-09-17 18:12:53 +0200 | [diff] [blame] | 781 | conn->put_time = ktime_get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 782 | if (atomic_dec_and_test(&conn->usage)) { |
| 783 | _debug("zombie"); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 784 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | _leave(""); |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * destroy a virtual connection |
| 792 | */ |
| 793 | static 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 Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 807 | conn->security->clear(conn); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 808 | key_put(conn->params.key); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 809 | key_put(conn->server_key); |
| 810 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 811 | rxrpc_put_transport(conn->trans); |
| 812 | kfree(conn); |
| 813 | _leave(""); |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * reap dead connections |
| 818 | */ |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 819 | static void rxrpc_connection_reaper(struct work_struct *work) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 820 | { |
| 821 | struct rxrpc_connection *conn, *_p; |
| 822 | unsigned long now, earliest, reap_time; |
| 823 | |
| 824 | LIST_HEAD(graveyard); |
| 825 | |
| 826 | _enter(""); |
| 827 | |
Ksenija Stanojevic | 22a3f9a | 2015-09-17 18:12:53 +0200 | [diff] [blame] | 828 | now = ktime_get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 829 | 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 Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 842 | reap_time = conn->put_time + rxrpc_connection_expiry; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 843 | |
| 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 Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 871 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, |
| 872 | (earliest - now) * HZ); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 873 | } |
| 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 | */ |
| 892 | void __exit rxrpc_destroy_all_connections(void) |
| 893 | { |
| 894 | _enter(""); |
| 895 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 896 | rxrpc_connection_expiry = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 897 | cancel_delayed_work(&rxrpc_connection_reap); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 898 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 899 | |
| 900 | _leave(""); |
| 901 | } |