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 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 35 | * allocate a new connection |
| 36 | */ |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 37 | struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 38 | { |
| 39 | struct rxrpc_connection *conn; |
| 40 | |
| 41 | _enter(""); |
| 42 | |
| 43 | conn = kzalloc(sizeof(struct rxrpc_connection), gfp); |
| 44 | if (conn) { |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 45 | spin_lock_init(&conn->channel_lock); |
| 46 | init_waitqueue_head(&conn->channel_wq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 47 | INIT_WORK(&conn->processor, &rxrpc_process_connection); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 48 | INIT_LIST_HEAD(&conn->link); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 49 | skb_queue_head_init(&conn->rx_queue); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 50 | conn->security = &rxrpc_no_security; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 51 | spin_lock_init(&conn->state_lock); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 52 | /* We maintain an extra ref on the connection whilst it is |
| 53 | * on the rxrpc_connections list. |
| 54 | */ |
| 55 | atomic_set(&conn->usage, 2); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 56 | conn->debug_id = atomic_inc_return(&rxrpc_debug_id); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 57 | atomic_set(&conn->avail_chans, RXRPC_MAXCALLS); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 58 | conn->size_align = 4; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 59 | conn->header_size = sizeof(struct rxrpc_wire_header); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Adrian Bunk | 16c61ad | 2007-06-15 15:15:43 -0700 | [diff] [blame] | 62 | _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 63 | return conn; |
| 64 | } |
| 65 | |
| 66 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 67 | * find a connection based on transport and RxRPC connection ID for an incoming |
| 68 | * packet |
| 69 | */ |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 70 | struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_local *local, |
| 71 | struct rxrpc_peer *peer, |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 72 | struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 73 | { |
| 74 | struct rxrpc_connection *conn; |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 75 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 76 | struct rb_node *p; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 77 | u32 epoch, cid; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 78 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 79 | _enter(",{%x,%x}", sp->hdr.cid, sp->hdr.flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 80 | |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 81 | read_lock_bh(&peer->conn_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 82 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 83 | cid = sp->hdr.cid & RXRPC_CIDMASK; |
| 84 | epoch = sp->hdr.epoch; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 85 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 86 | if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) { |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 87 | p = peer->service_conns.rb_node; |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 88 | while (p) { |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 89 | conn = rb_entry(p, struct rxrpc_connection, service_node); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 90 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 91 | _debug("maybe %x", conn->proto.cid); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 92 | |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 93 | if (epoch < conn->proto.epoch) |
| 94 | p = p->rb_left; |
| 95 | else if (epoch > conn->proto.epoch) |
| 96 | p = p->rb_right; |
| 97 | else if (cid < conn->proto.cid) |
| 98 | p = p->rb_left; |
| 99 | else if (cid > conn->proto.cid) |
| 100 | p = p->rb_right; |
| 101 | else |
| 102 | goto found; |
| 103 | } |
| 104 | } else { |
| 105 | conn = idr_find(&rxrpc_client_conn_ids, cid >> RXRPC_CIDSHIFT); |
David Howells | 689f4c6 | 2016-06-30 11:34:30 +0100 | [diff] [blame] | 106 | if (conn && |
| 107 | conn->proto.epoch == epoch && |
| 108 | conn->params.peer == peer) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 109 | goto found; |
| 110 | } |
| 111 | |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 112 | read_unlock_bh(&peer->conn_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 113 | _leave(" = NULL"); |
| 114 | return NULL; |
| 115 | |
| 116 | found: |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 117 | conn = rxrpc_get_connection_maybe(conn); |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 118 | read_unlock_bh(&peer->conn_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 119 | _leave(" = %p", conn); |
| 120 | return conn; |
| 121 | } |
| 122 | |
| 123 | /* |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 124 | * Disconnect a call and clear any channel it occupies when that call |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 125 | * terminates. The caller must hold the channel_lock and must release the |
| 126 | * call's ref on the connection. |
| 127 | */ |
| 128 | void __rxrpc_disconnect_call(struct rxrpc_call *call) |
| 129 | { |
| 130 | struct rxrpc_connection *conn = call->conn; |
| 131 | struct rxrpc_channel *chan = &conn->channels[call->channel]; |
| 132 | |
| 133 | _enter("%d,%d", conn->debug_id, call->channel); |
| 134 | |
| 135 | if (rcu_access_pointer(chan->call) == call) { |
| 136 | /* Save the result of the call so that we can repeat it if necessary |
| 137 | * through the channel, whilst disposing of the actual call record. |
| 138 | */ |
| 139 | chan->last_result = call->local_abort; |
| 140 | smp_wmb(); |
| 141 | chan->last_call = chan->call_id; |
| 142 | chan->call_id = chan->call_counter; |
| 143 | |
| 144 | rcu_assign_pointer(chan->call, NULL); |
| 145 | atomic_inc(&conn->avail_chans); |
| 146 | wake_up(&conn->channel_wq); |
| 147 | } |
| 148 | |
| 149 | _leave(""); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Disconnect a call and clear any channel it occupies when that call |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 154 | * terminates. |
| 155 | */ |
| 156 | void rxrpc_disconnect_call(struct rxrpc_call *call) |
| 157 | { |
| 158 | struct rxrpc_connection *conn = call->conn; |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 159 | |
David Howells | e653cfe | 2016-04-04 14:00:38 +0100 | [diff] [blame] | 160 | spin_lock(&conn->channel_lock); |
David Howells | a1399f8 | 2016-06-27 14:39:44 +0100 | [diff] [blame] | 161 | __rxrpc_disconnect_call(call); |
David Howells | e653cfe | 2016-04-04 14:00:38 +0100 | [diff] [blame] | 162 | spin_unlock(&conn->channel_lock); |
| 163 | |
| 164 | call->conn = NULL; |
| 165 | rxrpc_put_connection(conn); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 169 | * release a virtual connection |
| 170 | */ |
| 171 | void rxrpc_put_connection(struct rxrpc_connection *conn) |
| 172 | { |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 173 | if (!conn) |
| 174 | return; |
| 175 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 176 | _enter("%p{u=%d,d=%d}", |
| 177 | conn, atomic_read(&conn->usage), conn->debug_id); |
| 178 | |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 179 | ASSERTCMP(atomic_read(&conn->usage), >, 1); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 180 | |
Ksenija Stanojevic | 22a3f9a | 2015-09-17 18:12:53 +0200 | [diff] [blame] | 181 | conn->put_time = ktime_get_seconds(); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 182 | if (atomic_dec_return(&conn->usage) == 1) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 183 | _debug("zombie"); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 184 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | _leave(""); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * destroy a virtual connection |
| 192 | */ |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 193 | static void rxrpc_destroy_connection(struct rcu_head *rcu) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 194 | { |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 195 | struct rxrpc_connection *conn = |
| 196 | container_of(rcu, struct rxrpc_connection, rcu); |
| 197 | |
| 198 | _enter("{%d,u=%d}", conn->debug_id, atomic_read(&conn->usage)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 199 | |
| 200 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
| 201 | |
| 202 | _net("DESTROY CONN %d", conn->debug_id); |
| 203 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 204 | rxrpc_purge_queue(&conn->rx_queue); |
| 205 | |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 206 | conn->security->clear(conn); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 207 | key_put(conn->params.key); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 208 | key_put(conn->server_key); |
David Howells | aa390bb | 2016-06-17 10:06:56 +0100 | [diff] [blame] | 209 | rxrpc_put_peer(conn->params.peer); |
| 210 | rxrpc_put_local(conn->params.local); |
David Howells | e0e4d82 | 2016-04-07 17:23:58 +0100 | [diff] [blame] | 211 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 212 | kfree(conn); |
| 213 | _leave(""); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * reap dead connections |
| 218 | */ |
Roel Kluin | 5eaa65b | 2008-12-10 15:18:31 -0800 | [diff] [blame] | 219 | static void rxrpc_connection_reaper(struct work_struct *work) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 220 | { |
| 221 | struct rxrpc_connection *conn, *_p; |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 222 | unsigned long reap_older_than, earliest, put_time, now; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 223 | |
| 224 | LIST_HEAD(graveyard); |
| 225 | |
| 226 | _enter(""); |
| 227 | |
Ksenija Stanojevic | 22a3f9a | 2015-09-17 18:12:53 +0200 | [diff] [blame] | 228 | now = ktime_get_seconds(); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 229 | reap_older_than = now - rxrpc_connection_expiry; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 230 | earliest = ULONG_MAX; |
| 231 | |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 232 | write_lock(&rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 233 | list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) { |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 234 | ASSERTCMP(atomic_read(&conn->usage), >, 0); |
| 235 | if (likely(atomic_read(&conn->usage) > 1)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 236 | continue; |
| 237 | |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 238 | put_time = READ_ONCE(conn->put_time); |
| 239 | if (time_after(put_time, reap_older_than)) { |
| 240 | if (time_before(put_time, earliest)) |
| 241 | earliest = put_time; |
| 242 | continue; |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame] | 243 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 244 | |
| 245 | /* The usage count sits at 1 whilst the object is unused on the |
| 246 | * list; we reduce that to 0 to make the object unavailable. |
| 247 | */ |
| 248 | if (atomic_cmpxchg(&conn->usage, 1, 0) != 1) |
| 249 | continue; |
| 250 | |
| 251 | if (rxrpc_conn_is_client(conn)) |
| 252 | rxrpc_unpublish_client_conn(conn); |
| 253 | else |
| 254 | rxrpc_unpublish_service_conn(conn); |
| 255 | |
| 256 | list_move_tail(&conn->link, &graveyard); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 257 | } |
David Howells | b3f5750 | 2016-06-21 16:10:03 +0100 | [diff] [blame] | 258 | write_unlock(&rxrpc_connection_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 259 | |
| 260 | if (earliest != ULONG_MAX) { |
| 261 | _debug("reschedule reaper %ld", (long) earliest - now); |
| 262 | ASSERTCMP(earliest, >, now); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 263 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, |
| 264 | (earliest - now) * HZ); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 265 | } |
| 266 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 267 | while (!list_empty(&graveyard)) { |
| 268 | conn = list_entry(graveyard.next, struct rxrpc_connection, |
| 269 | link); |
| 270 | list_del_init(&conn->link); |
| 271 | |
| 272 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 273 | skb_queue_purge(&conn->rx_queue); |
| 274 | call_rcu(&conn->rcu, rxrpc_destroy_connection); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | _leave(""); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * preemptively destroy all the connection records rather than waiting for them |
| 282 | * to time out |
| 283 | */ |
| 284 | void __exit rxrpc_destroy_all_connections(void) |
| 285 | { |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 286 | struct rxrpc_connection *conn, *_p; |
| 287 | bool leak = false; |
| 288 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 289 | _enter(""); |
| 290 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 291 | rxrpc_connection_expiry = 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 292 | cancel_delayed_work(&rxrpc_connection_reap); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 293 | rxrpc_queue_delayed_work(&rxrpc_connection_reap, 0); |
David Howells | dee4636 | 2016-06-27 17:11:19 +0100 | [diff] [blame] | 294 | flush_workqueue(rxrpc_workqueue); |
| 295 | |
| 296 | write_lock(&rxrpc_connection_lock); |
| 297 | list_for_each_entry_safe(conn, _p, &rxrpc_connections, link) { |
| 298 | pr_err("AF_RXRPC: Leaked conn %p {%d}\n", |
| 299 | conn, atomic_read(&conn->usage)); |
| 300 | leak = true; |
| 301 | } |
| 302 | write_unlock(&rxrpc_connection_lock); |
| 303 | BUG_ON(leak); |
| 304 | |
| 305 | /* Make sure the local and peer records pinned by any dying connections |
| 306 | * are released. |
| 307 | */ |
| 308 | rcu_barrier(); |
| 309 | rxrpc_destroy_client_conn_ids(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 310 | |
| 311 | _leave(""); |
| 312 | } |