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