rxrpc: Clients must accept conn from any address
[linux-2.6-block.git] / net / rxrpc / conn_object.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
45025bce 2/* RxRPC virtual connection handler, common bits.
17926a79 3 *
45025bce 4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
17926a79 5 * Written by David Howells (dhowells@redhat.com)
17926a79
DH
6 */
7
9b6d5398
JP
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
17926a79 10#include <linux/module.h>
5a0e3ad6 11#include <linux/slab.h>
17926a79
DH
12#include <linux/net.h>
13#include <linux/skbuff.h>
17926a79
DH
14#include "ar-internal.h"
15
5873c083
DH
16/*
17 * Time till a connection expires after last use (in seconds).
18 */
f859ab61
DH
19unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
20unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
5873c083 21
3cec055c
DH
22static void rxrpc_clean_up_connection(struct work_struct *work);
23static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
24 unsigned long reap_at);
45025bce 25
f2cce89a
DH
26void rxrpc_poke_conn(struct rxrpc_connection *conn, enum rxrpc_conn_trace why)
27{
28 struct rxrpc_local *local = conn->local;
29 bool busy;
30
31 if (WARN_ON_ONCE(!local))
32 return;
33
34 spin_lock_bh(&local->lock);
35 busy = !list_empty(&conn->attend_link);
36 if (!busy) {
37 rxrpc_get_connection(conn, why);
38 list_add_tail(&conn->attend_link, &local->conn_attend_q);
39 }
40 spin_unlock_bh(&local->lock);
41 rxrpc_wake_up_io_thread(local);
42}
43
3136ef49
DH
44static void rxrpc_connection_timer(struct timer_list *timer)
45{
46 struct rxrpc_connection *conn =
47 container_of(timer, struct rxrpc_connection, timer);
48
f2cce89a 49 rxrpc_poke_conn(conn, rxrpc_conn_get_poke_timer);
3136ef49
DH
50}
51
17926a79
DH
52/*
53 * allocate a new connection
54 */
3cec055c
DH
55struct rxrpc_connection *rxrpc_alloc_connection(struct rxrpc_net *rxnet,
56 gfp_t gfp)
17926a79
DH
57{
58 struct rxrpc_connection *conn;
59
60 _enter("");
61
62 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
63 if (conn) {
45025bce 64 INIT_LIST_HEAD(&conn->cache_link);
3136ef49 65 timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
3cec055c
DH
66 INIT_WORK(&conn->processor, rxrpc_process_connection);
67 INIT_WORK(&conn->destructor, rxrpc_clean_up_connection);
4d028b2c 68 INIT_LIST_HEAD(&conn->proc_link);
999b69f8 69 INIT_LIST_HEAD(&conn->link);
9d35d880 70 mutex_init(&conn->security_lock);
49489bb0 71 mutex_init(&conn->tx_data_alloc_lock);
17926a79 72 skb_queue_head_init(&conn->rx_queue);
3cec055c 73 conn->rxnet = rxnet;
e0e4d82f 74 conn->security = &rxrpc_no_security;
17926a79 75 spin_lock_init(&conn->state_lock);
17926a79 76 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
f51b4480 77 conn->idle_timestamp = jiffies;
17926a79
DH
78 }
79
16c61add 80 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
17926a79
DH
81 return conn;
82}
83
17926a79 84/*
8496af50
DH
85 * Look up a connection in the cache by protocol parameters.
86 *
87 * If successful, a pointer to the connection is returned, but no ref is taken.
88 * NULL is returned if there is no match.
89 *
0099dc58
DH
90 * When searching for a service call, if we find a peer but no connection, we
91 * return that through *_peer in case we need to create a new service call.
92 *
8496af50 93 * The caller must be holding the RCU read lock.
17926a79 94 */
5e6ef4f1
DH
95struct rxrpc_connection *rxrpc_find_client_connection_rcu(struct rxrpc_local *local,
96 struct sockaddr_rxrpc *srx,
97 struct sk_buff *skb)
17926a79
DH
98{
99 struct rxrpc_connection *conn;
42886ffe 100 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1291e9d1 101 struct rxrpc_peer *peer;
17926a79 102
8496af50 103 _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
17926a79 104
f06cb291
DH
105 /* Look up client connections by connection ID alone as their
106 * IDs are unique for this machine.
5e6ef4f1 107 */
f06cb291 108 conn = idr_find(&local->conn_ids, sp->hdr.cid >> RXRPC_CIDSHIFT);
5e6ef4f1
DH
109 if (!conn || refcount_read(&conn->ref) == 0) {
110 _debug("no conn");
111 goto not_found;
112 }
8496af50 113
5e6ef4f1
DH
114 if (conn->proto.epoch != sp->hdr.epoch ||
115 conn->local != local)
116 goto not_found;
117
118 peer = conn->peer;
119 switch (srx->transport.family) {
120 case AF_INET:
121 if (peer->srx.transport.sin.sin_port !=
8953285d 122 srx->transport.sin.sin_port)
1291e9d1 123 goto not_found;
5e6ef4f1 124 break;
d1912747 125#ifdef CONFIG_AF_RXRPC_IPV6
5e6ef4f1
DH
126 case AF_INET6:
127 if (peer->srx.transport.sin6.sin6_port !=
8953285d 128 srx->transport.sin6.sin6_port)
5e6ef4f1
DH
129 goto not_found;
130 break;
d1912747 131#endif
5e6ef4f1
DH
132 default:
133 BUG();
17926a79
DH
134 }
135
5e6ef4f1
DH
136 _leave(" = %p", conn);
137 return conn;
138
1291e9d1 139not_found:
17926a79
DH
140 _leave(" = NULL");
141 return NULL;
17926a79
DH
142}
143
999b69f8
DH
144/*
145 * Disconnect a call and clear any channel it occupies when that call
a1399f8b
DH
146 * terminates. The caller must hold the channel_lock and must release the
147 * call's ref on the connection.
999b69f8 148 */
45025bce
DH
149void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
150 struct rxrpc_call *call)
999b69f8 151{
01a90a45
DH
152 struct rxrpc_channel *chan =
153 &conn->channels[call->cid & RXRPC_CHANNELMASK];
999b69f8 154
01a90a45 155 _enter("%d,%x", conn->debug_id, call->cid);
999b69f8 156
9d35d880 157 if (chan->call == call) {
a1399f8b
DH
158 /* Save the result of the call so that we can repeat it if necessary
159 * through the channel, whilst disposing of the actual call record.
160 */
b1d9f7fd 161 trace_rxrpc_disconnect_call(call);
17e9e23b
DH
162 switch (call->completion) {
163 case RXRPC_CALL_SUCCEEDED:
5d7edbc9 164 chan->last_seq = call->rx_highest_seq;
18bfeba5 165 chan->last_type = RXRPC_PACKET_TYPE_ACK;
17e9e23b
DH
166 break;
167 case RXRPC_CALL_LOCALLY_ABORTED:
168 chan->last_abort = call->abort_code;
169 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
170 break;
171 default:
de696c47 172 chan->last_abort = RX_CALL_DEAD;
17e9e23b
DH
173 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
174 break;
18bfeba5 175 }
17e9e23b 176
a1399f8b
DH
177 chan->last_call = chan->call_id;
178 chan->call_id = chan->call_counter;
9d35d880 179 chan->call = NULL;
999b69f8 180 }
e653cfe4 181
a1399f8b
DH
182 _leave("");
183}
184
185/*
186 * Disconnect a call and clear any channel it occupies when that call
187 * terminates.
188 */
189void rxrpc_disconnect_call(struct rxrpc_call *call)
190{
191 struct rxrpc_connection *conn = call->conn;
192
5040011d
DH
193 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
194 rxrpc_see_call(call, rxrpc_call_see_disconnected);
195
1fc4fa2a 196 call->peer->cong_ssthresh = call->cong_ssthresh;
f7aec129 197
65550098 198 if (!hlist_unhashed(&call->error_link)) {
29fb4ec3
DH
199 spin_lock(&call->peer->lock);
200 hlist_del_init(&call->error_link);
201 spin_unlock(&call->peer->lock);
65550098 202 }
248f219c 203
5040011d 204 if (rxrpc_is_client_call(call)) {
1bab27af 205 rxrpc_disconnect_client_call(call->bundle, call);
5040011d 206 } else {
5040011d 207 __rxrpc_disconnect_call(conn, call);
5040011d
DH
208 conn->idle_timestamp = jiffies;
209 if (atomic_dec_and_test(&conn->active))
210 rxrpc_set_service_reap_timer(conn->rxnet,
61e4a866 211 jiffies + rxrpc_connection_expiry * HZ);
5040011d 212 }
e653cfe4 213
5040011d 214 rxrpc_put_call(call, rxrpc_call_put_io_thread);
45025bce
DH
215}
216
17926a79 217/*
363deeab
DH
218 * Queue a connection's work processor, getting a ref to pass to the work
219 * queue.
17926a79 220 */
3cec055c 221void rxrpc_queue_conn(struct rxrpc_connection *conn, enum rxrpc_conn_trace why)
17926a79 222{
3cec055c
DH
223 if (atomic_read(&conn->active) >= 0 &&
224 rxrpc_queue_work(&conn->processor))
225 rxrpc_see_connection(conn, why);
363deeab
DH
226}
227
228/*
229 * Note the re-emergence of a connection.
230 */
7fa25105
DH
231void rxrpc_see_connection(struct rxrpc_connection *conn,
232 enum rxrpc_conn_trace why)
363deeab 233{
363deeab 234 if (conn) {
7fa25105 235 int r = refcount_read(&conn->ref);
363deeab 236
7fa25105 237 trace_rxrpc_conn(conn->debug_id, r, why);
363deeab
DH
238 }
239}
240
241/*
242 * Get a ref on a connection.
243 */
7fa25105
DH
244struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn,
245 enum rxrpc_conn_trace why)
363deeab 246{
a0575429 247 int r;
363deeab 248
a0575429 249 __refcount_inc(&conn->ref, &r);
7fa25105 250 trace_rxrpc_conn(conn->debug_id, r + 1, why);
245500d8 251 return conn;
363deeab
DH
252}
253
254/*
255 * Try to get a ref on a connection.
256 */
257struct rxrpc_connection *
7fa25105
DH
258rxrpc_get_connection_maybe(struct rxrpc_connection *conn,
259 enum rxrpc_conn_trace why)
363deeab 260{
a0575429 261 int r;
363deeab
DH
262
263 if (conn) {
a0575429 264 if (__refcount_inc_not_zero(&conn->ref, &r))
7fa25105 265 trace_rxrpc_conn(conn->debug_id, r + 1, why);
363deeab
DH
266 else
267 conn = NULL;
268 }
269 return conn;
270}
271
3d18cbb7
DH
272/*
273 * Set the service connection reap timer.
274 */
275static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
276 unsigned long reap_at)
277{
278 if (rxnet->live)
279 timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
280}
281
17926a79
DH
282/*
283 * destroy a virtual connection
284 */
3cec055c 285static void rxrpc_rcu_free_connection(struct rcu_head *rcu)
17926a79 286{
dee46364
DH
287 struct rxrpc_connection *conn =
288 container_of(rcu, struct rxrpc_connection, rcu);
3cec055c 289 struct rxrpc_net *rxnet = conn->rxnet;
dee46364 290
a0575429 291 _enter("{%d,u=%d}", conn->debug_id, refcount_read(&conn->ref));
17926a79 292
7fa25105
DH
293 trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
294 rxrpc_conn_free);
3cec055c 295 kfree(conn);
7fa25105 296
3cec055c
DH
297 if (atomic_dec_and_test(&rxnet->nr_conns))
298 wake_up_var(&rxnet->nr_conns);
299}
300
301/*
302 * Clean up a dead connection.
303 */
304static void rxrpc_clean_up_connection(struct work_struct *work)
305{
306 struct rxrpc_connection *conn =
307 container_of(work, struct rxrpc_connection, destructor);
308 struct rxrpc_net *rxnet = conn->rxnet;
309
9d35d880
DH
310 ASSERT(!conn->channels[0].call &&
311 !conn->channels[1].call &&
312 !conn->channels[2].call &&
313 !conn->channels[3].call);
3cec055c 314 ASSERT(list_empty(&conn->cache_link));
17926a79 315
3136ef49 316 del_timer_sync(&conn->timer);
3cec055c
DH
317 cancel_work_sync(&conn->processor); /* Processing may restart the timer */
318 del_timer_sync(&conn->timer);
319
320 write_lock(&rxnet->conn_lock);
321 list_del_init(&conn->proc_link);
322 write_unlock(&rxnet->conn_lock);
323
17926a79
DH
324 rxrpc_purge_queue(&conn->rx_queue);
325
3cec055c
DH
326 rxrpc_kill_client_conn(conn);
327
e0e4d82f 328 conn->security->clear(conn);
2cc80086 329 key_put(conn->key);
fa3492ab 330 rxrpc_put_bundle(conn->bundle, rxrpc_bundle_put_conn);
47c810a7 331 rxrpc_put_peer(conn->peer, rxrpc_peer_put_conn);
0fde882f 332 rxrpc_put_local(conn->local, rxrpc_local_put_kill_conn);
e0e4d82f 333
3cec055c
DH
334 /* Drain the Rx queue. Note that even though we've unpublished, an
335 * incoming packet could still be being added to our Rx queue, so we
336 * will need to drain it again in the RCU cleanup handler.
337 */
338 rxrpc_purge_queue(&conn->rx_queue);
339
49489bb0
DH
340 if (conn->tx_data_alloc.va)
341 __page_frag_cache_drain(virt_to_page(conn->tx_data_alloc.va),
342 conn->tx_data_alloc.pagecnt_bias);
3cec055c
DH
343 call_rcu(&conn->rcu, rxrpc_rcu_free_connection);
344}
345
346/*
347 * Drop a ref on a connection.
348 */
349void rxrpc_put_connection(struct rxrpc_connection *conn,
350 enum rxrpc_conn_trace why)
351{
352 unsigned int debug_id;
353 bool dead;
354 int r;
355
356 if (!conn)
357 return;
358
359 debug_id = conn->debug_id;
360 dead = __refcount_dec_and_test(&conn->ref, &r);
361 trace_rxrpc_conn(debug_id, r - 1, why);
362 if (dead) {
363 del_timer(&conn->timer);
364 cancel_work(&conn->processor);
365
366 if (in_softirq() || work_busy(&conn->processor) ||
367 timer_pending(&conn->timer))
368 /* Can't use the rxrpc workqueue as we need to cancel/flush
369 * something that may be running/waiting there.
370 */
371 schedule_work(&conn->destructor);
372 else
373 rxrpc_clean_up_connection(&conn->destructor);
374 }
17926a79
DH
375}
376
377/*
45025bce 378 * reap dead service connections
17926a79 379 */
2baec2c3 380void rxrpc_service_connection_reaper(struct work_struct *work)
17926a79
DH
381{
382 struct rxrpc_connection *conn, *_p;
2baec2c3 383 struct rxrpc_net *rxnet =
3d18cbb7 384 container_of(work, struct rxrpc_net, service_conn_reaper);
f859ab61 385 unsigned long expire_at, earliest, idle_timestamp, now;
3cec055c 386 int active;
17926a79
DH
387
388 LIST_HEAD(graveyard);
389
390 _enter("");
391
f51b4480 392 now = jiffies;
f859ab61 393 earliest = now + MAX_JIFFY_OFFSET;
17926a79 394
2baec2c3
DH
395 write_lock(&rxnet->conn_lock);
396 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
3cec055c
DH
397 ASSERTCMP(atomic_read(&conn->active), >=, 0);
398 if (likely(atomic_read(&conn->active) > 0))
17926a79 399 continue;
00e90712
DH
400 if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
401 continue;
17926a79 402
2cc80086 403 if (rxnet->live && !conn->local->dead) {
f859ab61
DH
404 idle_timestamp = READ_ONCE(conn->idle_timestamp);
405 expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
2cc80086 406 if (conn->local->service_closed)
f859ab61
DH
407 expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
408
3cec055c
DH
409 _debug("reap CONN %d { a=%d,t=%ld }",
410 conn->debug_id, atomic_read(&conn->active),
f859ab61
DH
411 (long)expire_at - (long)now);
412
413 if (time_before(now, expire_at)) {
414 if (time_before(expire_at, earliest))
415 earliest = expire_at;
416 continue;
417 }
17926a79 418 }
001c1122 419
3cec055c
DH
420 /* The activity count sits at 0 whilst the conn is unused on
421 * the list; we reduce that to -1 to make the conn unavailable.
001c1122 422 */
3cec055c
DH
423 active = 0;
424 if (!atomic_try_cmpxchg(&conn->active, &active, -1))
001c1122 425 continue;
7fa25105 426 rxrpc_see_connection(conn, rxrpc_conn_see_reap_service);
001c1122
DH
427
428 if (rxrpc_conn_is_client(conn))
45025bce 429 BUG();
001c1122
DH
430 else
431 rxrpc_unpublish_service_conn(conn);
432
433 list_move_tail(&conn->link, &graveyard);
17926a79 434 }
2baec2c3 435 write_unlock(&rxnet->conn_lock);
17926a79 436
f859ab61
DH
437 if (earliest != now + MAX_JIFFY_OFFSET) {
438 _debug("reschedule reaper %ld", (long)earliest - (long)now);
f51b4480 439 ASSERT(time_after(earliest, now));
3d7682af 440 rxrpc_set_service_reap_timer(rxnet, earliest);
17926a79
DH
441 }
442
17926a79
DH
443 while (!list_empty(&graveyard)) {
444 conn = list_entry(graveyard.next, struct rxrpc_connection,
445 link);
446 list_del_init(&conn->link);
447
3cec055c
DH
448 ASSERTCMP(atomic_read(&conn->active), ==, -1);
449 rxrpc_put_connection(conn, rxrpc_conn_put_service_reaped);
17926a79
DH
450 }
451
452 _leave("");
453}
454
455/*
45025bce
DH
456 * preemptively destroy all the service connection records rather than
457 * waiting for them to time out
17926a79 458 */
2baec2c3 459void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
17926a79 460{
dee46364
DH
461 struct rxrpc_connection *conn, *_p;
462 bool leak = false;
463
17926a79
DH
464 _enter("");
465
31f5f9a1 466 atomic_dec(&rxnet->nr_conns);
45025bce 467
3d18cbb7
DH
468 del_timer_sync(&rxnet->service_conn_reap_timer);
469 rxrpc_queue_work(&rxnet->service_conn_reaper);
dee46364
DH
470 flush_workqueue(rxrpc_workqueue);
471
2baec2c3
DH
472 write_lock(&rxnet->conn_lock);
473 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
dee46364 474 pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
a0575429 475 conn, refcount_read(&conn->ref));
dee46364
DH
476 leak = true;
477 }
2baec2c3 478 write_unlock(&rxnet->conn_lock);
dee46364
DH
479 BUG_ON(leak);
480
2baec2c3 481 ASSERT(list_empty(&rxnet->conn_proc_list));
17926a79 482
31f5f9a1
DH
483 /* We need to wait for the connections to be destroyed by RCU as they
484 * pin things that we still need to get rid of.
485 */
5bb053be 486 wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));
17926a79
DH
487 _leave("");
488}