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