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