Revert "Bluetooth: Store advertising handle so it can be re-enabled"
[platform/kernel/linux-rpi.git] / net / rxrpc / local_object.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Local endpoint object management
3  *
4  * Copyright (C) 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/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <linux/udp.h>
15 #include <linux/ip.h>
16 #include <linux/hashtable.h>
17 #include <net/sock.h>
18 #include <net/udp.h>
19 #include <net/udp_tunnel.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
22
23 static void rxrpc_local_processor(struct work_struct *);
24 static void rxrpc_local_rcu(struct rcu_head *);
25
26 /*
27  * Compare a local to an address.  Return -ve, 0 or +ve to indicate less than,
28  * same or greater than.
29  *
30  * We explicitly don't compare the RxRPC service ID as we want to reject
31  * conflicting uses by differing services.  Further, we don't want to share
32  * addresses with different options (IPv6), so we don't compare those bits
33  * either.
34  */
35 static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
36                                 const struct sockaddr_rxrpc *srx)
37 {
38         long diff;
39
40         diff = ((local->srx.transport_type - srx->transport_type) ?:
41                 (local->srx.transport_len - srx->transport_len) ?:
42                 (local->srx.transport.family - srx->transport.family));
43         if (diff != 0)
44                 return diff;
45
46         switch (srx->transport.family) {
47         case AF_INET:
48                 /* If the choice of UDP port is left up to the transport, then
49                  * the endpoint record doesn't match.
50                  */
51                 return ((u16 __force)local->srx.transport.sin.sin_port -
52                         (u16 __force)srx->transport.sin.sin_port) ?:
53                         memcmp(&local->srx.transport.sin.sin_addr,
54                                &srx->transport.sin.sin_addr,
55                                sizeof(struct in_addr));
56 #ifdef CONFIG_AF_RXRPC_IPV6
57         case AF_INET6:
58                 /* If the choice of UDP6 port is left up to the transport, then
59                  * the endpoint record doesn't match.
60                  */
61                 return ((u16 __force)local->srx.transport.sin6.sin6_port -
62                         (u16 __force)srx->transport.sin6.sin6_port) ?:
63                         memcmp(&local->srx.transport.sin6.sin6_addr,
64                                &srx->transport.sin6.sin6_addr,
65                                sizeof(struct in6_addr));
66 #endif
67         default:
68                 BUG();
69         }
70 }
71
72 /*
73  * Allocate a new local endpoint.
74  */
75 static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
76                                              const struct sockaddr_rxrpc *srx)
77 {
78         struct rxrpc_local *local;
79
80         local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
81         if (local) {
82                 atomic_set(&local->usage, 1);
83                 atomic_set(&local->active_users, 1);
84                 local->rxnet = rxnet;
85                 INIT_LIST_HEAD(&local->link);
86                 INIT_WORK(&local->processor, rxrpc_local_processor);
87                 init_rwsem(&local->defrag_sem);
88                 skb_queue_head_init(&local->reject_queue);
89                 skb_queue_head_init(&local->event_queue);
90                 local->client_bundles = RB_ROOT;
91                 spin_lock_init(&local->client_bundles_lock);
92                 spin_lock_init(&local->lock);
93                 rwlock_init(&local->services_lock);
94                 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
95                 memcpy(&local->srx, srx, sizeof(*srx));
96                 local->srx.srx_service = 0;
97                 trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
98         }
99
100         _leave(" = %p", local);
101         return local;
102 }
103
104 /*
105  * create the local socket
106  * - must be called with rxrpc_local_mutex locked
107  */
108 static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
109 {
110         struct udp_tunnel_sock_cfg tuncfg = {NULL};
111         struct sockaddr_rxrpc *srx = &local->srx;
112         struct udp_port_cfg udp_conf = {0};
113         struct sock *usk;
114         int ret;
115
116         _enter("%p{%d,%d}",
117                local, srx->transport_type, srx->transport.family);
118
119         udp_conf.family = srx->transport.family;
120         udp_conf.use_udp_checksums = true;
121         if (udp_conf.family == AF_INET) {
122                 udp_conf.local_ip = srx->transport.sin.sin_addr;
123                 udp_conf.local_udp_port = srx->transport.sin.sin_port;
124 #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
125         } else {
126                 udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
127                 udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
128                 udp_conf.use_udp6_tx_checksums = true;
129                 udp_conf.use_udp6_rx_checksums = true;
130 #endif
131         }
132         ret = udp_sock_create(net, &udp_conf, &local->socket);
133         if (ret < 0) {
134                 _leave(" = %d [socket]", ret);
135                 return ret;
136         }
137
138         tuncfg.encap_type = UDP_ENCAP_RXRPC;
139         tuncfg.encap_rcv = rxrpc_input_packet;
140         tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
141         tuncfg.sk_user_data = local;
142         setup_udp_tunnel_sock(net, local->socket, &tuncfg);
143
144         /* set the socket up */
145         usk = local->socket->sk;
146         usk->sk_error_report = rxrpc_error_report;
147
148         switch (srx->transport.family) {
149         case AF_INET6:
150                 /* we want to receive ICMPv6 errors */
151                 ip6_sock_set_recverr(usk);
152
153                 /* Fall through and set IPv4 options too otherwise we don't get
154                  * errors from IPv4 packets sent through the IPv6 socket.
155                  */
156                 fallthrough;
157         case AF_INET:
158                 /* we want to receive ICMP errors */
159                 ip_sock_set_recverr(usk);
160
161                 /* we want to set the don't fragment bit */
162                 ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
163
164                 /* We want receive timestamps. */
165                 sock_enable_timestamps(usk);
166                 break;
167
168         default:
169                 BUG();
170         }
171
172         _leave(" = 0");
173         return 0;
174 }
175
176 /*
177  * Look up or create a new local endpoint using the specified local address.
178  */
179 struct rxrpc_local *rxrpc_lookup_local(struct net *net,
180                                        const struct sockaddr_rxrpc *srx)
181 {
182         struct rxrpc_local *local;
183         struct rxrpc_net *rxnet = rxrpc_net(net);
184         struct list_head *cursor;
185         const char *age;
186         long diff;
187         int ret;
188
189         _enter("{%d,%d,%pISp}",
190                srx->transport_type, srx->transport.family, &srx->transport);
191
192         mutex_lock(&rxnet->local_mutex);
193
194         for (cursor = rxnet->local_endpoints.next;
195              cursor != &rxnet->local_endpoints;
196              cursor = cursor->next) {
197                 local = list_entry(cursor, struct rxrpc_local, link);
198
199                 diff = rxrpc_local_cmp_key(local, srx);
200                 if (diff < 0)
201                         continue;
202                 if (diff > 0)
203                         break;
204
205                 /* Services aren't allowed to share transport sockets, so
206                  * reject that here.  It is possible that the object is dying -
207                  * but it may also still have the local transport address that
208                  * we want bound.
209                  */
210                 if (srx->srx_service) {
211                         local = NULL;
212                         goto addr_in_use;
213                 }
214
215                 /* Found a match.  We replace a dying object.  Attempting to
216                  * bind the transport socket may still fail if we're attempting
217                  * to use a local address that the dying object is still using.
218                  */
219                 if (!rxrpc_use_local(local))
220                         break;
221
222                 age = "old";
223                 goto found;
224         }
225
226         local = rxrpc_alloc_local(rxnet, srx);
227         if (!local)
228                 goto nomem;
229
230         ret = rxrpc_open_socket(local, net);
231         if (ret < 0)
232                 goto sock_error;
233
234         if (cursor != &rxnet->local_endpoints)
235                 list_replace_init(cursor, &local->link);
236         else
237                 list_add_tail(&local->link, cursor);
238         age = "new";
239
240 found:
241         mutex_unlock(&rxnet->local_mutex);
242
243         _net("LOCAL %s %d {%pISp}",
244              age, local->debug_id, &local->srx.transport);
245
246         _leave(" = %p", local);
247         return local;
248
249 nomem:
250         ret = -ENOMEM;
251 sock_error:
252         mutex_unlock(&rxnet->local_mutex);
253         if (local)
254                 call_rcu(&local->rcu, rxrpc_local_rcu);
255         _leave(" = %d", ret);
256         return ERR_PTR(ret);
257
258 addr_in_use:
259         mutex_unlock(&rxnet->local_mutex);
260         _leave(" = -EADDRINUSE");
261         return ERR_PTR(-EADDRINUSE);
262 }
263
264 /*
265  * Get a ref on a local endpoint.
266  */
267 struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
268 {
269         const void *here = __builtin_return_address(0);
270         int n;
271
272         n = atomic_inc_return(&local->usage);
273         trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
274         return local;
275 }
276
277 /*
278  * Get a ref on a local endpoint unless its usage has already reached 0.
279  */
280 struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
281 {
282         const void *here = __builtin_return_address(0);
283
284         if (local) {
285                 int n = atomic_fetch_add_unless(&local->usage, 1, 0);
286                 if (n > 0)
287                         trace_rxrpc_local(local->debug_id, rxrpc_local_got,
288                                           n + 1, here);
289                 else
290                         local = NULL;
291         }
292         return local;
293 }
294
295 /*
296  * Queue a local endpoint and pass the caller's reference to the work item.
297  */
298 void rxrpc_queue_local(struct rxrpc_local *local)
299 {
300         const void *here = __builtin_return_address(0);
301         unsigned int debug_id = local->debug_id;
302         int n = atomic_read(&local->usage);
303
304         if (rxrpc_queue_work(&local->processor))
305                 trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
306         else
307                 rxrpc_put_local(local);
308 }
309
310 /*
311  * Drop a ref on a local endpoint.
312  */
313 void rxrpc_put_local(struct rxrpc_local *local)
314 {
315         const void *here = __builtin_return_address(0);
316         unsigned int debug_id;
317         int n;
318
319         if (local) {
320                 debug_id = local->debug_id;
321
322                 n = atomic_dec_return(&local->usage);
323                 trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
324
325                 if (n == 0)
326                         call_rcu(&local->rcu, rxrpc_local_rcu);
327         }
328 }
329
330 /*
331  * Start using a local endpoint.
332  */
333 struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
334 {
335         local = rxrpc_get_local_maybe(local);
336         if (!local)
337                 return NULL;
338
339         if (!__rxrpc_use_local(local)) {
340                 rxrpc_put_local(local);
341                 return NULL;
342         }
343
344         return local;
345 }
346
347 /*
348  * Cease using a local endpoint.  Once the number of active users reaches 0, we
349  * start the closure of the transport in the work processor.
350  */
351 void rxrpc_unuse_local(struct rxrpc_local *local)
352 {
353         if (local) {
354                 if (__rxrpc_unuse_local(local)) {
355                         rxrpc_get_local(local);
356                         rxrpc_queue_local(local);
357                 }
358         }
359 }
360
361 /*
362  * Destroy a local endpoint's socket and then hand the record to RCU to dispose
363  * of.
364  *
365  * Closing the socket cannot be done from bottom half context or RCU callback
366  * context because it might sleep.
367  */
368 static void rxrpc_local_destroyer(struct rxrpc_local *local)
369 {
370         struct socket *socket = local->socket;
371         struct rxrpc_net *rxnet = local->rxnet;
372
373         _enter("%d", local->debug_id);
374
375         local->dead = true;
376
377         mutex_lock(&rxnet->local_mutex);
378         list_del_init(&local->link);
379         mutex_unlock(&rxnet->local_mutex);
380
381         rxrpc_clean_up_local_conns(local);
382         rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
383         ASSERT(!local->service);
384
385         if (socket) {
386                 local->socket = NULL;
387                 kernel_sock_shutdown(socket, SHUT_RDWR);
388                 socket->sk->sk_user_data = NULL;
389                 sock_release(socket);
390         }
391
392         /* At this point, there should be no more packets coming in to the
393          * local endpoint.
394          */
395         rxrpc_purge_queue(&local->reject_queue);
396         rxrpc_purge_queue(&local->event_queue);
397 }
398
399 /*
400  * Process events on an endpoint.  The work item carries a ref which
401  * we must release.
402  */
403 static void rxrpc_local_processor(struct work_struct *work)
404 {
405         struct rxrpc_local *local =
406                 container_of(work, struct rxrpc_local, processor);
407         bool again;
408
409         if (local->dead)
410                 return;
411
412         trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
413                           atomic_read(&local->usage), NULL);
414
415         do {
416                 again = false;
417                 if (!__rxrpc_use_local(local)) {
418                         rxrpc_local_destroyer(local);
419                         break;
420                 }
421
422                 if (!skb_queue_empty(&local->reject_queue)) {
423                         rxrpc_reject_packets(local);
424                         again = true;
425                 }
426
427                 if (!skb_queue_empty(&local->event_queue)) {
428                         rxrpc_process_local_events(local);
429                         again = true;
430                 }
431
432                 __rxrpc_unuse_local(local);
433         } while (again);
434
435         rxrpc_put_local(local);
436 }
437
438 /*
439  * Destroy a local endpoint after the RCU grace period expires.
440  */
441 static void rxrpc_local_rcu(struct rcu_head *rcu)
442 {
443         struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
444
445         _enter("%d", local->debug_id);
446
447         ASSERT(!work_pending(&local->processor));
448
449         _net("DESTROY LOCAL %d", local->debug_id);
450         kfree(local);
451         _leave("");
452 }
453
454 /*
455  * Verify the local endpoint list is empty by this point.
456  */
457 void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
458 {
459         struct rxrpc_local *local;
460
461         _enter("");
462
463         flush_workqueue(rxrpc_workqueue);
464
465         if (!list_empty(&rxnet->local_endpoints)) {
466                 mutex_lock(&rxnet->local_mutex);
467                 list_for_each_entry(local, &rxnet->local_endpoints, link) {
468                         pr_err("AF_RXRPC: Leaked local %p {%d}\n",
469                                local, atomic_read(&local->usage));
470                 }
471                 mutex_unlock(&rxnet->local_mutex);
472                 BUG();
473         }
474 }