rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* RxRPC virtual connection handler, common bits. |
| 2 | * |
| 3 | * Copyright (C) 2007, 2016 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 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/net.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include "ar-internal.h" |
| 19 | |
| 20 | /* |
| 21 | * Time till a connection expires after last use (in seconds). |
| 22 | */ |
| 23 | unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60; |
| 24 | unsigned int __read_mostly rxrpc_closed_conn_expiry = 10; |
| 25 | |
| 26 | static void rxrpc_destroy_connection(struct rcu_head *); |
| 27 | |
| 28 | /* |
| 29 | * allocate a new connection |
| 30 | */ |
| 31 | struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp) |
| 32 | { |
| 33 | struct rxrpc_connection *conn; |
| 34 | |
| 35 | _enter(""); |
| 36 | |
| 37 | conn = kzalloc(sizeof(struct rxrpc_connection), gfp); |
| 38 | if (conn) { |
| 39 | INIT_LIST_HEAD(&conn->cache_link); |
| 40 | spin_lock_init(&conn->channel_lock); |
| 41 | INIT_LIST_HEAD(&conn->waiting_calls); |
| 42 | INIT_WORK(&conn->processor, &rxrpc_process_connection); |
| 43 | INIT_LIST_HEAD(&conn->proc_link); |
| 44 | INIT_LIST_HEAD(&conn->link); |
| 45 | skb_queue_head_init(&conn->rx_queue); |
| 46 | conn->security = &rxrpc_no_security; |
| 47 | spin_lock_init(&conn->state_lock); |
| 48 | conn->debug_id = atomic_inc_return(&rxrpc_debug_id); |
| 49 | conn->size_align = 4; |
| 50 | conn->idle_timestamp = jiffies; |
| 51 | } |
| 52 | |
| 53 | _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0); |
| 54 | return conn; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Look up a connection in the cache by protocol parameters. |
| 59 | * |
| 60 | * If successful, a pointer to the connection is returned, but no ref is taken. |
| 61 | * NULL is returned if there is no match. |
| 62 | * |
| 63 | * The caller must be holding the RCU read lock. |
| 64 | */ |
| 65 | struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local, |
| 66 | struct sk_buff *skb) |
| 67 | { |
| 68 | struct rxrpc_connection *conn; |
| 69 | struct rxrpc_conn_proto k; |
| 70 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 71 | struct sockaddr_rxrpc srx; |
| 72 | struct rxrpc_peer *peer; |
| 73 | |
| 74 | _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK); |
| 75 | |
| 76 | if (rxrpc_extract_addr_from_skb(local, &srx, skb) < 0) |
| 77 | goto not_found; |
| 78 | |
| 79 | k.epoch = sp->hdr.epoch; |
| 80 | k.cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 81 | |
| 82 | /* We may have to handle mixing IPv4 and IPv6 */ |
| 83 | if (srx.transport.family != local->srx.transport.family) { |
| 84 | pr_warn_ratelimited("AF_RXRPC: Protocol mismatch %u not %u\n", |
| 85 | srx.transport.family, |
| 86 | local->srx.transport.family); |
| 87 | goto not_found; |
| 88 | } |
| 89 | |
| 90 | k.epoch = sp->hdr.epoch; |
| 91 | k.cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 92 | |
| 93 | if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) { |
| 94 | /* We need to look up service connections by the full protocol |
| 95 | * parameter set. We look up the peer first as an intermediate |
| 96 | * step and then the connection from the peer's tree. |
| 97 | */ |
| 98 | peer = rxrpc_lookup_peer_rcu(local, &srx); |
| 99 | if (!peer) |
| 100 | goto not_found; |
| 101 | conn = rxrpc_find_service_conn_rcu(peer, skb); |
| 102 | if (!conn || atomic_read(&conn->usage) == 0) |
| 103 | goto not_found; |
| 104 | _leave(" = %p", conn); |
| 105 | return conn; |
| 106 | } else { |
| 107 | /* Look up client connections by connection ID alone as their |
| 108 | * IDs are unique for this machine. |
| 109 | */ |
| 110 | conn = idr_find(&rxrpc_client_conn_ids, |
| 111 | sp->hdr.cid >> RXRPC_CIDSHIFT); |
| 112 | if (!conn || atomic_read(&conn->usage) == 0) { |
| 113 | _debug("no conn"); |
| 114 | goto not_found; |
| 115 | } |
| 116 | |
| 117 | if (conn->proto.epoch != k.epoch || |
| 118 | conn->params.local != local) |
| 119 | goto not_found; |
| 120 | |
| 121 | peer = conn->params.peer; |
| 122 | switch (srx.transport.family) { |
| 123 | case AF_INET: |
| 124 | if (peer->srx.transport.sin.sin_port != |
| 125 | srx.transport.sin.sin_port || |
| 126 | peer->srx.transport.sin.sin_addr.s_addr != |
| 127 | srx.transport.sin.sin_addr.s_addr) |
| 128 | goto not_found; |
| 129 | break; |
| 130 | #ifdef CONFIG_AF_RXRPC_IPV6 |
| 131 | case AF_INET6: |
| 132 | if (peer->srx.transport.sin6.sin6_port != |
| 133 | srx.transport.sin6.sin6_port || |
| 134 | memcmp(&peer->srx.transport.sin6.sin6_addr, |
| 135 | &srx.transport.sin6.sin6_addr, |
| 136 | sizeof(struct in6_addr)) != 0) |
| 137 | goto not_found; |
| 138 | break; |
| 139 | #endif |
| 140 | default: |
| 141 | BUG(); |
| 142 | } |
| 143 | |
| 144 | _leave(" = %p", conn); |
| 145 | return conn; |
| 146 | } |
| 147 | |
| 148 | not_found: |
| 149 | _leave(" = NULL"); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Disconnect a call and clear any channel it occupies when that call |
| 155 | * terminates. The caller must hold the channel_lock and must release the |
| 156 | * call's ref on the connection. |
| 157 | */ |
| 158 | void __rxrpc_disconnect_call(struct rxrpc_connection *conn, |
| 159 | struct rxrpc_call *call) |
| 160 | { |
| 161 | struct rxrpc_channel *chan = |
| 162 | &conn->channels[call->cid & RXRPC_CHANNELMASK]; |
| 163 | |
| 164 | _enter("%d,%x", conn->debug_id, call->cid); |
| 165 | |
| 166 | if (rcu_access_pointer(chan->call) == call) { |
| 167 | /* Save the result of the call so that we can repeat it if necessary |
| 168 | * through the channel, whilst disposing of the actual call record. |
| 169 | */ |
| 170 | trace_rxrpc_disconnect_call(call); |
| 171 | if (call->abort_code) { |
| 172 | chan->last_abort = call->abort_code; |
| 173 | chan->last_type = RXRPC_PACKET_TYPE_ABORT; |
| 174 | } else { |
| 175 | chan->last_seq = call->rx_hard_ack; |
| 176 | chan->last_type = RXRPC_PACKET_TYPE_ACK; |
| 177 | } |
| 178 | /* Sync with rxrpc_conn_retransmit(). */ |
| 179 | smp_wmb(); |
| 180 | chan->last_call = chan->call_id; |
| 181 | chan->call_id = chan->call_counter; |
| 182 | |
| 183 | rcu_assign_pointer(chan->call, NULL); |
| 184 | } |
| 185 | |
| 186 | _leave(""); |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Disconnect a call and clear any channel it occupies when that call |
| 191 | * terminates. |
| 192 | */ |
| 193 | void rxrpc_disconnect_call(struct rxrpc_call *call) |
| 194 | { |
| 195 | struct rxrpc_connection *conn = call->conn; |
| 196 | |
| 197 | call->peer->cong_cwnd = call->cong_cwnd; |
| 198 | |
| 199 | if (!hlist_unhashed(&call->error_link)) { |
| 200 | spin_lock_bh(&conn->params.peer->lock); |
| 201 | hlist_del_init(&call->error_link); |
| 202 | spin_unlock_bh(&conn->params.peer->lock); |
| 203 | } |
| 204 | |
| 205 | if (rxrpc_is_client_call(call)) |
| 206 | return rxrpc_disconnect_client_call(call); |
| 207 | |
| 208 | spin_lock(&conn->channel_lock); |
| 209 | __rxrpc_disconnect_call(conn, call); |
| 210 | spin_unlock(&conn->channel_lock); |
| 211 | |
| 212 | set_bit(RXRPC_CALL_DISCONNECTED, &call->flags); |
| 213 | conn->idle_timestamp = jiffies; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Kill off a connection. |
| 218 | */ |
| 219 | void rxrpc_kill_connection(struct rxrpc_connection *conn) |
| 220 | { |
| 221 | struct rxrpc_net *rxnet = conn->params.local->rxnet; |
| 222 | |
| 223 | ASSERT(!rcu_access_pointer(conn->channels[0].call) && |
| 224 | !rcu_access_pointer(conn->channels[1].call) && |
| 225 | !rcu_access_pointer(conn->channels[2].call) && |
| 226 | !rcu_access_pointer(conn->channels[3].call)); |
| 227 | ASSERT(list_empty(&conn->cache_link)); |
| 228 | |
| 229 | write_lock(&rxnet->conn_lock); |
| 230 | list_del_init(&conn->proc_link); |
| 231 | write_unlock(&rxnet->conn_lock); |
| 232 | |
| 233 | /* Drain the Rx queue. Note that even though we've unpublished, an |
| 234 | * incoming packet could still be being added to our Rx queue, so we |
| 235 | * will need to drain it again in the RCU cleanup handler. |
| 236 | */ |
| 237 | rxrpc_purge_queue(&conn->rx_queue); |
| 238 | |
| 239 | /* Leave final destruction to RCU. The connection processor work item |
| 240 | * must carry a ref on the connection to prevent us getting here whilst |
| 241 | * it is queued or running. |
| 242 | */ |
| 243 | call_rcu(&conn->rcu, rxrpc_destroy_connection); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Queue a connection's work processor, getting a ref to pass to the work |
| 248 | * queue. |
| 249 | */ |
| 250 | bool rxrpc_queue_conn(struct rxrpc_connection *conn) |
| 251 | { |
| 252 | const void *here = __builtin_return_address(0); |
| 253 | int n = __atomic_add_unless(&conn->usage, 1, 0); |
| 254 | if (n == 0) |
| 255 | return false; |
| 256 | if (rxrpc_queue_work(&conn->processor)) |
| 257 | trace_rxrpc_conn(conn, rxrpc_conn_queued, n + 1, here); |
| 258 | else |
| 259 | rxrpc_put_connection(conn); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Note the re-emergence of a connection. |
| 265 | */ |
| 266 | void rxrpc_see_connection(struct rxrpc_connection *conn) |
| 267 | { |
| 268 | const void *here = __builtin_return_address(0); |
| 269 | if (conn) { |
| 270 | int n = atomic_read(&conn->usage); |
| 271 | |
| 272 | trace_rxrpc_conn(conn, rxrpc_conn_seen, n, here); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Get a ref on a connection. |
| 278 | */ |
| 279 | void rxrpc_get_connection(struct rxrpc_connection *conn) |
| 280 | { |
| 281 | const void *here = __builtin_return_address(0); |
| 282 | int n = atomic_inc_return(&conn->usage); |
| 283 | |
| 284 | trace_rxrpc_conn(conn, rxrpc_conn_got, n, here); |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Try to get a ref on a connection. |
| 289 | */ |
| 290 | struct rxrpc_connection * |
| 291 | rxrpc_get_connection_maybe(struct rxrpc_connection *conn) |
| 292 | { |
| 293 | const void *here = __builtin_return_address(0); |
| 294 | |
| 295 | if (conn) { |
| 296 | int n = __atomic_add_unless(&conn->usage, 1, 0); |
| 297 | if (n > 0) |
| 298 | trace_rxrpc_conn(conn, rxrpc_conn_got, n + 1, here); |
| 299 | else |
| 300 | conn = NULL; |
| 301 | } |
| 302 | return conn; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Release a service connection |
| 307 | */ |
| 308 | void rxrpc_put_service_conn(struct rxrpc_connection *conn) |
| 309 | { |
| 310 | struct rxrpc_net *rxnet; |
| 311 | const void *here = __builtin_return_address(0); |
| 312 | int n; |
| 313 | |
| 314 | n = atomic_dec_return(&conn->usage); |
| 315 | trace_rxrpc_conn(conn, rxrpc_conn_put_service, n, here); |
| 316 | ASSERTCMP(n, >=, 0); |
| 317 | if (n == 1) { |
| 318 | rxnet = conn->params.local->rxnet; |
| 319 | rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, 0); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * destroy a virtual connection |
| 325 | */ |
| 326 | static void rxrpc_destroy_connection(struct rcu_head *rcu) |
| 327 | { |
| 328 | struct rxrpc_connection *conn = |
| 329 | container_of(rcu, struct rxrpc_connection, rcu); |
| 330 | |
| 331 | _enter("{%d,u=%d}", conn->debug_id, atomic_read(&conn->usage)); |
| 332 | |
| 333 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
| 334 | |
| 335 | _net("DESTROY CONN %d", conn->debug_id); |
| 336 | |
| 337 | rxrpc_purge_queue(&conn->rx_queue); |
| 338 | |
| 339 | conn->security->clear(conn); |
| 340 | key_put(conn->params.key); |
| 341 | key_put(conn->server_key); |
| 342 | rxrpc_put_peer(conn->params.peer); |
| 343 | rxrpc_put_local(conn->params.local); |
| 344 | |
| 345 | kfree(conn); |
| 346 | _leave(""); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * reap dead service connections |
| 351 | */ |
| 352 | void rxrpc_service_connection_reaper(struct work_struct *work) |
| 353 | { |
| 354 | struct rxrpc_connection *conn, *_p; |
| 355 | struct rxrpc_net *rxnet = |
| 356 | container_of(to_delayed_work(work), |
| 357 | struct rxrpc_net, service_conn_reaper); |
| 358 | unsigned long expire_at, earliest, idle_timestamp, now; |
| 359 | |
| 360 | LIST_HEAD(graveyard); |
| 361 | |
| 362 | _enter(""); |
| 363 | |
| 364 | now = jiffies; |
| 365 | earliest = now + MAX_JIFFY_OFFSET; |
| 366 | |
| 367 | write_lock(&rxnet->conn_lock); |
| 368 | list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { |
| 369 | ASSERTCMP(atomic_read(&conn->usage), >, 0); |
| 370 | if (likely(atomic_read(&conn->usage) > 1)) |
| 371 | continue; |
| 372 | if (conn->state == RXRPC_CONN_SERVICE_PREALLOC) |
| 373 | continue; |
| 374 | |
| 375 | if (rxnet->live) { |
| 376 | idle_timestamp = READ_ONCE(conn->idle_timestamp); |
| 377 | expire_at = idle_timestamp + rxrpc_connection_expiry * HZ; |
| 378 | if (conn->params.local->service_closed) |
| 379 | expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ; |
| 380 | |
| 381 | _debug("reap CONN %d { u=%d,t=%ld }", |
| 382 | conn->debug_id, atomic_read(&conn->usage), |
| 383 | (long)expire_at - (long)now); |
| 384 | |
| 385 | if (time_before(now, expire_at)) { |
| 386 | if (time_before(expire_at, earliest)) |
| 387 | earliest = expire_at; |
| 388 | continue; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* The usage count sits at 1 whilst the object is unused on the |
| 393 | * list; we reduce that to 0 to make the object unavailable. |
| 394 | */ |
| 395 | if (atomic_cmpxchg(&conn->usage, 1, 0) != 1) |
| 396 | continue; |
| 397 | trace_rxrpc_conn(conn, rxrpc_conn_reap_service, 0, 0); |
| 398 | |
| 399 | if (rxrpc_conn_is_client(conn)) |
| 400 | BUG(); |
| 401 | else |
| 402 | rxrpc_unpublish_service_conn(conn); |
| 403 | |
| 404 | list_move_tail(&conn->link, &graveyard); |
| 405 | } |
| 406 | write_unlock(&rxnet->conn_lock); |
| 407 | |
| 408 | if (earliest != now + MAX_JIFFY_OFFSET) { |
| 409 | _debug("reschedule reaper %ld", (long)earliest - (long)now); |
| 410 | ASSERT(time_after(earliest, now)); |
| 411 | rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, |
| 412 | earliest - now); |
| 413 | } |
| 414 | |
| 415 | while (!list_empty(&graveyard)) { |
| 416 | conn = list_entry(graveyard.next, struct rxrpc_connection, |
| 417 | link); |
| 418 | list_del_init(&conn->link); |
| 419 | |
| 420 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
| 421 | rxrpc_kill_connection(conn); |
| 422 | } |
| 423 | |
| 424 | _leave(""); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * preemptively destroy all the service connection records rather than |
| 429 | * waiting for them to time out |
| 430 | */ |
| 431 | void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet) |
| 432 | { |
| 433 | struct rxrpc_connection *conn, *_p; |
| 434 | bool leak = false; |
| 435 | |
| 436 | _enter(""); |
| 437 | |
| 438 | rxrpc_destroy_all_client_connections(rxnet); |
| 439 | |
| 440 | cancel_delayed_work(&rxnet->client_conn_reaper); |
| 441 | rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, 0); |
| 442 | flush_workqueue(rxrpc_workqueue); |
| 443 | |
| 444 | write_lock(&rxnet->conn_lock); |
| 445 | list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { |
| 446 | pr_err("AF_RXRPC: Leaked conn %p {%d}\n", |
| 447 | conn, atomic_read(&conn->usage)); |
| 448 | leak = true; |
| 449 | } |
| 450 | write_unlock(&rxnet->conn_lock); |
| 451 | BUG_ON(leak); |
| 452 | |
| 453 | ASSERT(list_empty(&rxnet->conn_proc_list)); |
| 454 | |
| 455 | _leave(""); |
| 456 | } |