blob: 8720be2a625037315a2dfa9f44d6a257e167b7cd [file] [log] [blame]
David Howells87563612016-04-04 14:00:34 +01001/* Local endpoint object management
David Howells17926a72007-04-26 15:48:28 -07002 *
David Howells4f95dd72016-04-04 14:00:35 +01003 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
David Howells17926a72007-04-26 15:48:28 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
David Howells87563612016-04-04 14:00:34 +01007 * modify it under the terms of the GNU General Public Licence
David Howells17926a72007-04-26 15:48:28 -07008 * as published by the Free Software Foundation; either version
David Howells87563612016-04-04 14:00:34 +01009 * 2 of the Licence, or (at your option) any later version.
David Howells17926a72007-04-26 15:48:28 -070010 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howells44ba0692015-04-01 16:31:26 +010018#include <linux/udp.h>
19#include <linux/ip.h>
David Howells4f95dd72016-04-04 14:00:35 +010020#include <linux/hashtable.h>
David Howells17926a72007-04-26 15:48:28 -070021#include <net/sock.h>
22#include <net/af_rxrpc.h>
23#include "ar-internal.h"
24
David Howells4f95dd72016-04-04 14:00:35 +010025static void rxrpc_local_processor(struct work_struct *);
26static void rxrpc_local_rcu(struct rcu_head *);
David Howells17926a72007-04-26 15:48:28 -070027
David Howells4f95dd72016-04-04 14:00:35 +010028static DEFINE_MUTEX(rxrpc_local_mutex);
29static LIST_HEAD(rxrpc_local_endpoints);
David Howells17926a72007-04-26 15:48:28 -070030
31/*
David Howells4f95dd72016-04-04 14:00:35 +010032 * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
33 * same or greater than.
34 *
35 * We explicitly don't compare the RxRPC service ID as we want to reject
36 * conflicting uses by differing services. Further, we don't want to share
37 * addresses with different options (IPv6), so we don't compare those bits
38 * either.
David Howells17926a72007-04-26 15:48:28 -070039 */
David Howells4f95dd72016-04-04 14:00:35 +010040static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
41 const struct sockaddr_rxrpc *srx)
42{
43 long diff;
44
45 diff = ((local->srx.transport_type - srx->transport_type) ?:
46 (local->srx.transport_len - srx->transport_len) ?:
47 (local->srx.transport.family - srx->transport.family));
48 if (diff != 0)
49 return diff;
50
51 switch (srx->transport.family) {
52 case AF_INET:
53 /* If the choice of UDP port is left up to the transport, then
54 * the endpoint record doesn't match.
55 */
56 return ((u16 __force)local->srx.transport.sin.sin_port -
57 (u16 __force)srx->transport.sin.sin_port) ?:
58 memcmp(&local->srx.transport.sin.sin_addr,
59 &srx->transport.sin.sin_addr,
60 sizeof(struct in_addr));
61 default:
62 BUG();
63 }
64}
65
66/*
67 * Allocate a new local endpoint.
68 */
69static struct rxrpc_local *rxrpc_alloc_local(const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -070070{
71 struct rxrpc_local *local;
72
73 local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
74 if (local) {
David Howells4f95dd72016-04-04 14:00:35 +010075 atomic_set(&local->usage, 1);
David Howells17926a72007-04-26 15:48:28 -070076 INIT_LIST_HEAD(&local->link);
David Howells4f95dd72016-04-04 14:00:35 +010077 INIT_WORK(&local->processor, rxrpc_local_processor);
David Howellsde8d6c72016-09-08 11:10:11 +010078 INIT_HLIST_HEAD(&local->services);
David Howells17926a72007-04-26 15:48:28 -070079 init_rwsem(&local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -070080 skb_queue_head_init(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +010081 skb_queue_head_init(&local->event_queue);
David Howells999b69f2016-06-17 15:42:35 +010082 local->client_conns = RB_ROOT;
83 spin_lock_init(&local->client_conns_lock);
David Howells17926a72007-04-26 15:48:28 -070084 spin_lock_init(&local->lock);
85 rwlock_init(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -070086 local->debug_id = atomic_inc_return(&rxrpc_debug_id);
87 memcpy(&local->srx, srx, sizeof(*srx));
88 }
89
90 _leave(" = %p", local);
91 return local;
92}
93
94/*
95 * create the local socket
David Howells4f95dd72016-04-04 14:00:35 +010096 * - must be called with rxrpc_local_mutex locked
David Howells17926a72007-04-26 15:48:28 -070097 */
David Howells4f95dd72016-04-04 14:00:35 +010098static int rxrpc_open_socket(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -070099{
100 struct sock *sock;
101 int ret, opt;
102
103 _enter("%p{%d}", local, local->srx.transport_type);
104
105 /* create a socket to represent the local endpoint */
David Howellsaaa31cb2016-09-13 08:49:05 +0100106 ret = sock_create_kern(&init_net, local->srx.transport.family,
107 local->srx.transport_type, 0, &local->socket);
David Howells17926a72007-04-26 15:48:28 -0700108 if (ret < 0) {
109 _leave(" = %d [socket]", ret);
110 return ret;
111 }
112
113 /* if a local address was supplied then bind it */
114 if (local->srx.transport_len > sizeof(sa_family_t)) {
115 _debug("bind");
116 ret = kernel_bind(local->socket,
David Howells4f95dd72016-04-04 14:00:35 +0100117 (struct sockaddr *)&local->srx.transport,
David Howells17926a72007-04-26 15:48:28 -0700118 local->srx.transport_len);
119 if (ret < 0) {
David Howells4f95dd72016-04-04 14:00:35 +0100120 _debug("bind failed %d", ret);
David Howells17926a72007-04-26 15:48:28 -0700121 goto error;
122 }
123 }
124
125 /* we want to receive ICMP errors */
126 opt = 1;
127 ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
128 (char *) &opt, sizeof(opt));
129 if (ret < 0) {
130 _debug("setsockopt failed");
131 goto error;
132 }
133
134 /* we want to set the don't fragment bit */
135 opt = IP_PMTUDISC_DO;
136 ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
137 (char *) &opt, sizeof(opt));
138 if (ret < 0) {
139 _debug("setsockopt failed");
140 goto error;
141 }
142
David Howells17926a72007-04-26 15:48:28 -0700143 /* set the socket up */
144 sock = local->socket->sk;
145 sock->sk_user_data = local;
146 sock->sk_data_ready = rxrpc_data_ready;
David Howellsabe89ef2016-04-04 14:00:32 +0100147 sock->sk_error_report = rxrpc_error_report;
David Howells17926a72007-04-26 15:48:28 -0700148 _leave(" = 0");
149 return 0;
150
151error:
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800152 kernel_sock_shutdown(local->socket, SHUT_RDWR);
David Howells17926a72007-04-26 15:48:28 -0700153 local->socket->sk->sk_user_data = NULL;
154 sock_release(local->socket);
155 local->socket = NULL;
156
157 _leave(" = %d", ret);
158 return ret;
159}
160
161/*
David Howells4f95dd72016-04-04 14:00:35 +0100162 * Look up or create a new local endpoint using the specified local address.
David Howells17926a72007-04-26 15:48:28 -0700163 */
David Howells4f95dd72016-04-04 14:00:35 +0100164struct rxrpc_local *rxrpc_lookup_local(const struct sockaddr_rxrpc *srx)
David Howells17926a72007-04-26 15:48:28 -0700165{
166 struct rxrpc_local *local;
David Howells4f95dd72016-04-04 14:00:35 +0100167 struct list_head *cursor;
168 const char *age;
169 long diff;
David Howells17926a72007-04-26 15:48:28 -0700170 int ret;
171
David Howells4f95dd72016-04-04 14:00:35 +0100172 if (srx->transport.family == AF_INET) {
173 _enter("{%d,%u,%pI4+%hu}",
174 srx->transport_type,
175 srx->transport.family,
176 &srx->transport.sin.sin_addr,
177 ntohs(srx->transport.sin.sin_port));
178 } else {
179 _enter("{%d,%u}",
180 srx->transport_type,
181 srx->transport.family);
182 return ERR_PTR(-EAFNOSUPPORT);
183 }
David Howells17926a72007-04-26 15:48:28 -0700184
David Howells4f95dd72016-04-04 14:00:35 +0100185 mutex_lock(&rxrpc_local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700186
David Howells4f95dd72016-04-04 14:00:35 +0100187 for (cursor = rxrpc_local_endpoints.next;
188 cursor != &rxrpc_local_endpoints;
189 cursor = cursor->next) {
190 local = list_entry(cursor, struct rxrpc_local, link);
David Howells17926a72007-04-26 15:48:28 -0700191
David Howells4f95dd72016-04-04 14:00:35 +0100192 diff = rxrpc_local_cmp_key(local, srx);
193 if (diff < 0)
David Howells17926a72007-04-26 15:48:28 -0700194 continue;
David Howells4f95dd72016-04-04 14:00:35 +0100195 if (diff > 0)
196 break;
David Howells17926a72007-04-26 15:48:28 -0700197
David Howells4f95dd72016-04-04 14:00:35 +0100198 /* Services aren't allowed to share transport sockets, so
199 * reject that here. It is possible that the object is dying -
200 * but it may also still have the local transport address that
201 * we want bound.
202 */
203 if (srx->srx_service) {
204 local = NULL;
205 goto addr_in_use;
David Howells17926a72007-04-26 15:48:28 -0700206 }
David Howells4f95dd72016-04-04 14:00:35 +0100207
208 /* Found a match. We replace a dying object. Attempting to
209 * bind the transport socket may still fail if we're attempting
210 * to use a local address that the dying object is still using.
211 */
David Howells5627cc82016-04-04 14:00:38 +0100212 if (!rxrpc_get_local_maybe(local)) {
David Howells4f95dd72016-04-04 14:00:35 +0100213 cursor = cursor->next;
214 list_del_init(&local->link);
215 break;
216 }
217
218 age = "old";
219 goto found;
David Howells17926a72007-04-26 15:48:28 -0700220 }
221
David Howells17926a72007-04-26 15:48:28 -0700222 local = rxrpc_alloc_local(srx);
David Howells4f95dd72016-04-04 14:00:35 +0100223 if (!local)
224 goto nomem;
David Howells17926a72007-04-26 15:48:28 -0700225
David Howells4f95dd72016-04-04 14:00:35 +0100226 ret = rxrpc_open_socket(local);
227 if (ret < 0)
228 goto sock_error;
David Howells17926a72007-04-26 15:48:28 -0700229
David Howells4f95dd72016-04-04 14:00:35 +0100230 list_add_tail(&local->link, cursor);
231 age = "new";
David Howells17926a72007-04-26 15:48:28 -0700232
David Howells4f95dd72016-04-04 14:00:35 +0100233found:
234 mutex_unlock(&rxrpc_local_mutex);
235
236 _net("LOCAL %s %d {%d,%u,%pI4+%hu}",
237 age,
David Howells17926a72007-04-26 15:48:28 -0700238 local->debug_id,
239 local->srx.transport_type,
240 local->srx.transport.family,
Harvey Harrison21454aa2008-10-31 00:54:56 -0700241 &local->srx.transport.sin.sin_addr,
David Howells17926a72007-04-26 15:48:28 -0700242 ntohs(local->srx.transport.sin.sin_port));
243
David Howells4f95dd72016-04-04 14:00:35 +0100244 _leave(" = %p", local);
David Howells17926a72007-04-26 15:48:28 -0700245 return local;
246
David Howells4f95dd72016-04-04 14:00:35 +0100247nomem:
248 ret = -ENOMEM;
249sock_error:
250 mutex_unlock(&rxrpc_local_mutex);
251 kfree(local);
252 _leave(" = %d", ret);
253 return ERR_PTR(ret);
David Howells17926a72007-04-26 15:48:28 -0700254
David Howells4f95dd72016-04-04 14:00:35 +0100255addr_in_use:
256 mutex_unlock(&rxrpc_local_mutex);
257 _leave(" = -EADDRINUSE");
258 return ERR_PTR(-EADDRINUSE);
David Howells17926a72007-04-26 15:48:28 -0700259}
260
261/*
David Howells4f95dd72016-04-04 14:00:35 +0100262 * A local endpoint reached its end of life.
David Howells17926a72007-04-26 15:48:28 -0700263 */
David Howells4f95dd72016-04-04 14:00:35 +0100264void __rxrpc_put_local(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700265{
David Howells4f95dd72016-04-04 14:00:35 +0100266 _enter("%d", local->debug_id);
267 rxrpc_queue_work(&local->processor);
David Howells17926a72007-04-26 15:48:28 -0700268}
269
270/*
David Howells4f95dd72016-04-04 14:00:35 +0100271 * Destroy a local endpoint's socket and then hand the record to RCU to dispose
272 * of.
273 *
274 * Closing the socket cannot be done from bottom half context or RCU callback
275 * context because it might sleep.
David Howells17926a72007-04-26 15:48:28 -0700276 */
David Howells4f95dd72016-04-04 14:00:35 +0100277static void rxrpc_local_destroyer(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700278{
David Howells4f95dd72016-04-04 14:00:35 +0100279 struct socket *socket = local->socket;
David Howells17926a72007-04-26 15:48:28 -0700280
David Howells4f95dd72016-04-04 14:00:35 +0100281 _enter("%d", local->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700282
David Howells4f95dd72016-04-04 14:00:35 +0100283 /* We can get a race between an incoming call packet queueing the
284 * processor again and the work processor starting the destruction
285 * process which will shut down the UDP socket.
286 */
287 if (local->dead) {
288 _leave(" [already dead]");
David Howells17926a72007-04-26 15:48:28 -0700289 return;
290 }
David Howells4f95dd72016-04-04 14:00:35 +0100291 local->dead = true;
David Howells17926a72007-04-26 15:48:28 -0700292
David Howells4f95dd72016-04-04 14:00:35 +0100293 mutex_lock(&rxrpc_local_mutex);
294 list_del_init(&local->link);
295 mutex_unlock(&rxrpc_local_mutex);
David Howells17926a72007-04-26 15:48:28 -0700296
David Howells999b69f2016-06-17 15:42:35 +0100297 ASSERT(RB_EMPTY_ROOT(&local->client_conns));
David Howellsde8d6c72016-09-08 11:10:11 +0100298 ASSERT(hlist_empty(&local->services));
David Howells17926a72007-04-26 15:48:28 -0700299
David Howells4f95dd72016-04-04 14:00:35 +0100300 if (socket) {
301 local->socket = NULL;
302 kernel_sock_shutdown(socket, SHUT_RDWR);
303 socket->sk->sk_user_data = NULL;
304 sock_release(socket);
305 }
306
307 /* At this point, there should be no more packets coming in to the
308 * local endpoint.
309 */
David Howells17926a72007-04-26 15:48:28 -0700310 rxrpc_purge_queue(&local->reject_queue);
David Howells44ba0692015-04-01 16:31:26 +0100311 rxrpc_purge_queue(&local->event_queue);
David Howells17926a72007-04-26 15:48:28 -0700312
David Howells4f95dd72016-04-04 14:00:35 +0100313 _debug("rcu local %d", local->debug_id);
314 call_rcu(&local->rcu, rxrpc_local_rcu);
315}
316
317/*
318 * Process events on an endpoint
319 */
320static void rxrpc_local_processor(struct work_struct *work)
321{
322 struct rxrpc_local *local =
323 container_of(work, struct rxrpc_local, processor);
324 bool again;
325
326 _enter("%d", local->debug_id);
327
328 do {
329 again = false;
330 if (atomic_read(&local->usage) == 0)
331 return rxrpc_local_destroyer(local);
332
David Howells4f95dd72016-04-04 14:00:35 +0100333 if (!skb_queue_empty(&local->reject_queue)) {
334 rxrpc_reject_packets(local);
335 again = true;
336 }
337
338 if (!skb_queue_empty(&local->event_queue)) {
339 rxrpc_process_local_events(local);
340 again = true;
341 }
342 } while (again);
343}
344
345/*
346 * Destroy a local endpoint after the RCU grace period expires.
347 */
348static void rxrpc_local_rcu(struct rcu_head *rcu)
349{
350 struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
351
352 _enter("%d", local->debug_id);
353
354 ASSERT(!work_pending(&local->processor));
David Howells17926a72007-04-26 15:48:28 -0700355
356 _net("DESTROY LOCAL %d", local->debug_id);
357 kfree(local);
David Howells17926a72007-04-26 15:48:28 -0700358 _leave("");
359}
360
361/*
David Howells4f95dd72016-04-04 14:00:35 +0100362 * Verify the local endpoint list is empty by this point.
David Howells17926a72007-04-26 15:48:28 -0700363 */
364void __exit rxrpc_destroy_all_locals(void)
365{
David Howells4f95dd72016-04-04 14:00:35 +0100366 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700367
368 _enter("");
369
David Howellsdee46362016-06-27 17:11:19 +0100370 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700371
David Howellsdee46362016-06-27 17:11:19 +0100372 if (!list_empty(&rxrpc_local_endpoints)) {
373 mutex_lock(&rxrpc_local_mutex);
374 list_for_each_entry(local, &rxrpc_local_endpoints, link) {
375 pr_err("AF_RXRPC: Leaked local %p {%d}\n",
376 local, atomic_read(&local->usage));
377 }
378 mutex_unlock(&rxrpc_local_mutex);
379 BUG();
David Howells17926a72007-04-26 15:48:28 -0700380 }
David Howellsdee46362016-06-27 17:11:19 +0100381
382 rcu_barrier();
David Howells17926a72007-04-26 15:48:28 -0700383}