]>
Commit | Line | Data |
---|---|---|
45025bce | 1 | /* RxRPC virtual connection handler, common bits. |
17926a79 | 2 | * |
45025bce | 3 | * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. |
17926a79 DH |
4 | * Written by David Howells (dhowells@redhat.com) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
9b6d5398 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
17926a79 | 14 | #include <linux/module.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
17926a79 DH |
16 | #include <linux/net.h> |
17 | #include <linux/skbuff.h> | |
17926a79 DH |
18 | #include "ar-internal.h" |
19 | ||
5873c083 DH |
20 | /* |
21 | * Time till a connection expires after last use (in seconds). | |
22 | */ | |
1392633b DH |
23 | unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60; |
24 | unsigned int __read_mostly rxrpc_closed_conn_expiry = 10; | |
5873c083 | 25 | |
45025bce DH |
26 | static void rxrpc_destroy_connection(struct rcu_head *); |
27 | ||
17926a79 DH |
28 | /* |
29 | * allocate a new connection | |
30 | */ | |
c6d2b8d7 | 31 | struct rxrpc_connection *rxrpc_alloc_connection(gfp_t gfp) |
17926a79 DH |
32 | { |
33 | struct rxrpc_connection *conn; | |
34 | ||
35 | _enter(""); | |
36 | ||
37 | conn = kzalloc(sizeof(struct rxrpc_connection), gfp); | |
38 | if (conn) { | |
45025bce | 39 | INIT_LIST_HEAD(&conn->cache_link); |
999b69f8 | 40 | spin_lock_init(&conn->channel_lock); |
45025bce | 41 | INIT_LIST_HEAD(&conn->waiting_calls); |
17926a79 | 42 | INIT_WORK(&conn->processor, &rxrpc_process_connection); |
4d028b2c | 43 | INIT_LIST_HEAD(&conn->proc_link); |
999b69f8 | 44 | INIT_LIST_HEAD(&conn->link); |
17926a79 | 45 | skb_queue_head_init(&conn->rx_queue); |
e0e4d82f | 46 | conn->security = &rxrpc_no_security; |
17926a79 | 47 | spin_lock_init(&conn->state_lock); |
17926a79 | 48 | conn->debug_id = atomic_inc_return(&rxrpc_debug_id); |
17926a79 | 49 | conn->size_align = 4; |
f51b4480 | 50 | conn->idle_timestamp = jiffies; |
17926a79 DH |
51 | } |
52 | ||
16c61add | 53 | _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0); |
17926a79 DH |
54 | return conn; |
55 | } | |
56 | ||
17926a79 | 57 | /* |
8496af50 DH |
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. | |
17926a79 | 64 | */ |
8496af50 DH |
65 | struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local, |
66 | struct sk_buff *skb) | |
17926a79 DH |
67 | { |
68 | struct rxrpc_connection *conn; | |
1291e9d1 | 69 | struct rxrpc_conn_proto k; |
42886ffe | 70 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
1291e9d1 DH |
71 | struct sockaddr_rxrpc srx; |
72 | struct rxrpc_peer *peer; | |
17926a79 | 73 | |
8496af50 | 74 | _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK); |
17926a79 | 75 | |
7b674e39 | 76 | if (rxrpc_extract_addr_from_skb(local, &srx, skb) < 0) |
1291e9d1 | 77 | goto not_found; |
17926a79 | 78 | |
8496af50 DH |
79 | k.epoch = sp->hdr.epoch; |
80 | k.cid = sp->hdr.cid & RXRPC_CIDMASK; | |
81 | ||
1291e9d1 DH |
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; | |
17926a79 | 92 | |
4a3388c8 | 93 | if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) { |
1291e9d1 DH |
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; | |
8496af50 DH |
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; | |
4a3388c8 | 106 | } else { |
8496af50 DH |
107 | /* Look up client connections by connection ID alone as their |
108 | * IDs are unique for this machine. | |
109 | */ | |
1291e9d1 | 110 | conn = idr_find(&rxrpc_client_conn_ids, |
8496af50 DH |
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 || | |
1291e9d1 DH |
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; | |
d1912747 | 130 | #ifdef CONFIG_AF_RXRPC_IPV6 |
75b54cb5 DH |
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; | |
d1912747 | 139 | #endif |
1291e9d1 DH |
140 | default: |
141 | BUG(); | |
142 | } | |
143 | ||
1291e9d1 DH |
144 | _leave(" = %p", conn); |
145 | return conn; | |
17926a79 DH |
146 | } |
147 | ||
1291e9d1 | 148 | not_found: |
17926a79 DH |
149 | _leave(" = NULL"); |
150 | return NULL; | |
17926a79 DH |
151 | } |
152 | ||
999b69f8 DH |
153 | /* |
154 | * Disconnect a call and clear any channel it occupies when that call | |
a1399f8b DH |
155 | * terminates. The caller must hold the channel_lock and must release the |
156 | * call's ref on the connection. | |
999b69f8 | 157 | */ |
45025bce DH |
158 | void __rxrpc_disconnect_call(struct rxrpc_connection *conn, |
159 | struct rxrpc_call *call) | |
999b69f8 | 160 | { |
01a90a45 DH |
161 | struct rxrpc_channel *chan = |
162 | &conn->channels[call->cid & RXRPC_CHANNELMASK]; | |
999b69f8 | 163 | |
01a90a45 | 164 | _enter("%d,%x", conn->debug_id, call->cid); |
999b69f8 | 165 | |
a1399f8b DH |
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 | */ | |
b1d9f7fd | 170 | trace_rxrpc_disconnect_call(call); |
f5c17aae DH |
171 | if (call->abort_code) { |
172 | chan->last_abort = call->abort_code; | |
18bfeba5 DH |
173 | chan->last_type = RXRPC_PACKET_TYPE_ABORT; |
174 | } else { | |
248f219c | 175 | chan->last_seq = call->rx_hard_ack; |
18bfeba5 DH |
176 | chan->last_type = RXRPC_PACKET_TYPE_ACK; |
177 | } | |
178 | /* Sync with rxrpc_conn_retransmit(). */ | |
a1399f8b DH |
179 | smp_wmb(); |
180 | chan->last_call = chan->call_id; | |
181 | chan->call_id = chan->call_counter; | |
e653cfe4 | 182 | |
a1399f8b | 183 | rcu_assign_pointer(chan->call, NULL); |
999b69f8 | 184 | } |
e653cfe4 | 185 | |
a1399f8b DH |
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 | ||
f7aec129 DH |
197 | call->peer->cong_cwnd = call->cong_cwnd; |
198 | ||
248f219c DH |
199 | spin_lock_bh(&conn->params.peer->lock); |
200 | hlist_del_init(&call->error_link); | |
201 | spin_unlock_bh(&conn->params.peer->lock); | |
202 | ||
45025bce DH |
203 | if (rxrpc_is_client_call(call)) |
204 | return rxrpc_disconnect_client_call(call); | |
205 | ||
a1399f8b | 206 | spin_lock(&conn->channel_lock); |
45025bce | 207 | __rxrpc_disconnect_call(conn, call); |
e653cfe4 DH |
208 | spin_unlock(&conn->channel_lock); |
209 | ||
210 | call->conn = NULL; | |
f51b4480 | 211 | conn->idle_timestamp = jiffies; |
e653cfe4 | 212 | rxrpc_put_connection(conn); |
999b69f8 DH |
213 | } |
214 | ||
45025bce DH |
215 | /* |
216 | * Kill off a connection. | |
217 | */ | |
218 | void rxrpc_kill_connection(struct rxrpc_connection *conn) | |
219 | { | |
2baec2c3 DH |
220 | struct rxrpc_net *rxnet = conn->params.local->rxnet; |
221 | ||
45025bce DH |
222 | ASSERT(!rcu_access_pointer(conn->channels[0].call) && |
223 | !rcu_access_pointer(conn->channels[1].call) && | |
224 | !rcu_access_pointer(conn->channels[2].call) && | |
225 | !rcu_access_pointer(conn->channels[3].call)); | |
226 | ASSERT(list_empty(&conn->cache_link)); | |
227 | ||
2baec2c3 | 228 | write_lock(&rxnet->conn_lock); |
45025bce | 229 | list_del_init(&conn->proc_link); |
2baec2c3 | 230 | write_unlock(&rxnet->conn_lock); |
45025bce DH |
231 | |
232 | /* Drain the Rx queue. Note that even though we've unpublished, an | |
233 | * incoming packet could still be being added to our Rx queue, so we | |
234 | * will need to drain it again in the RCU cleanup handler. | |
235 | */ | |
236 | rxrpc_purge_queue(&conn->rx_queue); | |
237 | ||
238 | /* Leave final destruction to RCU. The connection processor work item | |
239 | * must carry a ref on the connection to prevent us getting here whilst | |
240 | * it is queued or running. | |
241 | */ | |
242 | call_rcu(&conn->rcu, rxrpc_destroy_connection); | |
243 | } | |
244 | ||
17926a79 | 245 | /* |
363deeab DH |
246 | * Queue a connection's work processor, getting a ref to pass to the work |
247 | * queue. | |
17926a79 | 248 | */ |
363deeab | 249 | bool rxrpc_queue_conn(struct rxrpc_connection *conn) |
17926a79 | 250 | { |
363deeab DH |
251 | const void *here = __builtin_return_address(0); |
252 | int n = __atomic_add_unless(&conn->usage, 1, 0); | |
253 | if (n == 0) | |
254 | return false; | |
255 | if (rxrpc_queue_work(&conn->processor)) | |
256 | trace_rxrpc_conn(conn, rxrpc_conn_queued, n + 1, here); | |
257 | else | |
258 | rxrpc_put_connection(conn); | |
259 | return true; | |
260 | } | |
261 | ||
262 | /* | |
263 | * Note the re-emergence of a connection. | |
264 | */ | |
265 | void rxrpc_see_connection(struct rxrpc_connection *conn) | |
266 | { | |
267 | const void *here = __builtin_return_address(0); | |
268 | if (conn) { | |
269 | int n = atomic_read(&conn->usage); | |
270 | ||
271 | trace_rxrpc_conn(conn, rxrpc_conn_seen, n, here); | |
272 | } | |
273 | } | |
274 | ||
275 | /* | |
276 | * Get a ref on a connection. | |
277 | */ | |
278 | void rxrpc_get_connection(struct rxrpc_connection *conn) | |
279 | { | |
280 | const void *here = __builtin_return_address(0); | |
281 | int n = atomic_inc_return(&conn->usage); | |
282 | ||
283 | trace_rxrpc_conn(conn, rxrpc_conn_got, n, here); | |
284 | } | |
285 | ||
286 | /* | |
287 | * Try to get a ref on a connection. | |
288 | */ | |
289 | struct rxrpc_connection * | |
290 | rxrpc_get_connection_maybe(struct rxrpc_connection *conn) | |
291 | { | |
292 | const void *here = __builtin_return_address(0); | |
293 | ||
294 | if (conn) { | |
295 | int n = __atomic_add_unless(&conn->usage, 1, 0); | |
296 | if (n > 0) | |
297 | trace_rxrpc_conn(conn, rxrpc_conn_got, n + 1, here); | |
298 | else | |
299 | conn = NULL; | |
300 | } | |
301 | return conn; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Release a service connection | |
306 | */ | |
307 | void rxrpc_put_service_conn(struct rxrpc_connection *conn) | |
308 | { | |
2baec2c3 | 309 | struct rxrpc_net *rxnet; |
363deeab DH |
310 | const void *here = __builtin_return_address(0); |
311 | int n; | |
312 | ||
313 | n = atomic_dec_return(&conn->usage); | |
314 | trace_rxrpc_conn(conn, rxrpc_conn_put_service, n, here); | |
315 | ASSERTCMP(n, >=, 0); | |
1392633b | 316 | if (n == 1) { |
2baec2c3 DH |
317 | rxnet = conn->params.local->rxnet; |
318 | rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, 0); | |
319 | } | |
17926a79 DH |
320 | } |
321 | ||
322 | /* | |
323 | * destroy a virtual connection | |
324 | */ | |
dee46364 | 325 | static void rxrpc_destroy_connection(struct rcu_head *rcu) |
17926a79 | 326 | { |
dee46364 DH |
327 | struct rxrpc_connection *conn = |
328 | container_of(rcu, struct rxrpc_connection, rcu); | |
329 | ||
330 | _enter("{%d,u=%d}", conn->debug_id, atomic_read(&conn->usage)); | |
17926a79 DH |
331 | |
332 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); | |
333 | ||
334 | _net("DESTROY CONN %d", conn->debug_id); | |
335 | ||
17926a79 DH |
336 | rxrpc_purge_queue(&conn->rx_queue); |
337 | ||
e0e4d82f | 338 | conn->security->clear(conn); |
19ffa01c | 339 | key_put(conn->params.key); |
e0e4d82f | 340 | key_put(conn->server_key); |
aa390bbe DH |
341 | rxrpc_put_peer(conn->params.peer); |
342 | rxrpc_put_local(conn->params.local); | |
e0e4d82f | 343 | |
17926a79 DH |
344 | kfree(conn); |
345 | _leave(""); | |
346 | } | |
347 | ||
348 | /* | |
45025bce | 349 | * reap dead service connections |
17926a79 | 350 | */ |
2baec2c3 | 351 | void rxrpc_service_connection_reaper(struct work_struct *work) |
17926a79 DH |
352 | { |
353 | struct rxrpc_connection *conn, *_p; | |
2baec2c3 DH |
354 | struct rxrpc_net *rxnet = |
355 | container_of(to_delayed_work(work), | |
356 | struct rxrpc_net, service_conn_reaper); | |
1392633b | 357 | unsigned long expire_at, earliest, idle_timestamp, now; |
17926a79 DH |
358 | |
359 | LIST_HEAD(graveyard); | |
360 | ||
361 | _enter(""); | |
362 | ||
f51b4480 | 363 | now = jiffies; |
1392633b | 364 | earliest = now + MAX_JIFFY_OFFSET; |
17926a79 | 365 | |
2baec2c3 DH |
366 | write_lock(&rxnet->conn_lock); |
367 | list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { | |
001c1122 DH |
368 | ASSERTCMP(atomic_read(&conn->usage), >, 0); |
369 | if (likely(atomic_read(&conn->usage) > 1)) | |
17926a79 | 370 | continue; |
00e90712 DH |
371 | if (conn->state == RXRPC_CONN_SERVICE_PREALLOC) |
372 | continue; | |
17926a79 | 373 | |
1392633b DH |
374 | if (rxnet->live) { |
375 | idle_timestamp = READ_ONCE(conn->idle_timestamp); | |
376 | expire_at = idle_timestamp + rxrpc_connection_expiry * HZ; | |
377 | if (conn->params.local->service_closed) | |
378 | expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ; | |
379 | ||
380 | _debug("reap CONN %d { u=%d,t=%ld }", | |
381 | conn->debug_id, atomic_read(&conn->usage), | |
382 | (long)expire_at - (long)now); | |
383 | ||
384 | if (time_before(now, expire_at)) { | |
385 | if (time_before(expire_at, earliest)) | |
386 | earliest = expire_at; | |
387 | continue; | |
388 | } | |
17926a79 | 389 | } |
001c1122 DH |
390 | |
391 | /* The usage count sits at 1 whilst the object is unused on the | |
392 | * list; we reduce that to 0 to make the object unavailable. | |
393 | */ | |
394 | if (atomic_cmpxchg(&conn->usage, 1, 0) != 1) | |
395 | continue; | |
1392633b | 396 | trace_rxrpc_conn(conn, rxrpc_conn_reap_service, 0, 0); |
001c1122 DH |
397 | |
398 | if (rxrpc_conn_is_client(conn)) | |
45025bce | 399 | BUG(); |
001c1122 DH |
400 | else |
401 | rxrpc_unpublish_service_conn(conn); | |
402 | ||
403 | list_move_tail(&conn->link, &graveyard); | |
17926a79 | 404 | } |
2baec2c3 | 405 | write_unlock(&rxnet->conn_lock); |
17926a79 | 406 | |
1392633b DH |
407 | if (earliest != now + MAX_JIFFY_OFFSET) { |
408 | _debug("reschedule reaper %ld", (long)earliest - (long)now); | |
f51b4480 | 409 | ASSERT(time_after(earliest, now)); |
1392633b | 410 | rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, |
f51b4480 | 411 | earliest - now); |
17926a79 DH |
412 | } |
413 | ||
17926a79 DH |
414 | while (!list_empty(&graveyard)) { |
415 | conn = list_entry(graveyard.next, struct rxrpc_connection, | |
416 | link); | |
417 | list_del_init(&conn->link); | |
418 | ||
419 | ASSERTCMP(atomic_read(&conn->usage), ==, 0); | |
45025bce | 420 | rxrpc_kill_connection(conn); |
17926a79 DH |
421 | } |
422 | ||
423 | _leave(""); | |
424 | } | |
425 | ||
426 | /* | |
45025bce DH |
427 | * preemptively destroy all the service connection records rather than |
428 | * waiting for them to time out | |
17926a79 | 429 | */ |
2baec2c3 | 430 | void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet) |
17926a79 | 431 | { |
dee46364 DH |
432 | struct rxrpc_connection *conn, *_p; |
433 | bool leak = false; | |
434 | ||
17926a79 DH |
435 | _enter(""); |
436 | ||
2baec2c3 | 437 | rxrpc_destroy_all_client_connections(rxnet); |
45025bce | 438 | |
2baec2c3 DH |
439 | cancel_delayed_work(&rxnet->client_conn_reaper); |
440 | rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, 0); | |
dee46364 DH |
441 | flush_workqueue(rxrpc_workqueue); |
442 | ||
2baec2c3 DH |
443 | write_lock(&rxnet->conn_lock); |
444 | list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { | |
dee46364 DH |
445 | pr_err("AF_RXRPC: Leaked conn %p {%d}\n", |
446 | conn, atomic_read(&conn->usage)); | |
447 | leak = true; | |
448 | } | |
2baec2c3 | 449 | write_unlock(&rxnet->conn_lock); |
dee46364 DH |
450 | BUG_ON(leak); |
451 | ||
2baec2c3 | 452 | ASSERT(list_empty(&rxnet->conn_proc_list)); |
17926a79 DH |
453 | |
454 | _leave(""); | |
455 | } |