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