blob: b29bb50af5de8ff2d08cf4063c5828987a471ef2 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* AF_RXRPC implementation
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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
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>
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +000015#include <linux/kernel.h>
David Howells17926a72007-04-26 15:48:28 -070016#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070018#include <linux/skbuff.h>
19#include <linux/poll.h>
20#include <linux/proc_fs.h>
David Howells76181c12007-10-16 23:29:46 -070021#include <linux/key-type.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020022#include <net/net_namespace.h>
David Howells17926a72007-04-26 15:48:28 -070023#include <net/sock.h>
24#include <net/af_rxrpc.h>
25#include "ar-internal.h"
26
27MODULE_DESCRIPTION("RxRPC network protocol");
28MODULE_AUTHOR("Red Hat, Inc.");
29MODULE_LICENSE("GPL");
30MODULE_ALIAS_NETPROTO(PF_RXRPC);
31
Eric Dumazet95c96172012-04-15 05:58:06 +000032unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
David Howells17926a72007-04-26 15:48:28 -070033module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
Paul Bolle424b00e2008-04-16 11:08:22 +010034MODULE_PARM_DESC(debug, "RxRPC debugging mask");
David Howells17926a72007-04-26 15:48:28 -070035
David Howells17926a72007-04-26 15:48:28 -070036static struct proto rxrpc_proto;
37static const struct proto_ops rxrpc_rpc_ops;
38
39/* local epoch for detecting local-end reset */
David Howells0d12f8a2016-03-04 15:53:46 +000040u32 rxrpc_epoch;
David Howells17926a72007-04-26 15:48:28 -070041
42/* current debugging ID */
43atomic_t rxrpc_debug_id;
44
45/* count of skbs currently in use */
46atomic_t rxrpc_n_skbs;
47
David Howells651350d2007-04-26 15:50:17 -070048struct workqueue_struct *rxrpc_workqueue;
49
David Howells17926a72007-04-26 15:48:28 -070050static void rxrpc_sock_destructor(struct sock *);
51
52/*
53 * see if an RxRPC socket is currently writable
54 */
55static inline int rxrpc_writable(struct sock *sk)
56{
57 return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
58}
59
60/*
61 * wait for write bufferage to become available
62 */
63static void rxrpc_write_space(struct sock *sk)
64{
65 _enter("%p", sk);
Eric Dumazet43815482010-04-29 11:01:49 +000066 rcu_read_lock();
David Howells17926a72007-04-26 15:48:28 -070067 if (rxrpc_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +000068 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
69
Herbert Xu1ce0bf52015-11-26 13:55:39 +080070 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +000071 wake_up_interruptible(&wq->wait);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +080072 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
David Howells17926a72007-04-26 15:48:28 -070073 }
Eric Dumazet43815482010-04-29 11:01:49 +000074 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -070075}
76
77/*
78 * validate an RxRPC address
79 */
80static int rxrpc_validate_address(struct rxrpc_sock *rx,
81 struct sockaddr_rxrpc *srx,
82 int len)
83{
David Howellsdad8aff2016-03-09 23:22:56 +000084 unsigned int tail;
David Howellsab802ee2016-03-04 15:59:49 +000085
David Howells17926a72007-04-26 15:48:28 -070086 if (len < sizeof(struct sockaddr_rxrpc))
87 return -EINVAL;
88
89 if (srx->srx_family != AF_RXRPC)
90 return -EAFNOSUPPORT;
91
92 if (srx->transport_type != SOCK_DGRAM)
93 return -ESOCKTNOSUPPORT;
94
95 len -= offsetof(struct sockaddr_rxrpc, transport);
96 if (srx->transport_len < sizeof(sa_family_t) ||
97 srx->transport_len > len)
98 return -EINVAL;
99
David Howells19ffa012016-04-04 14:00:36 +0100100 if (srx->transport.family != rx->family)
David Howells17926a72007-04-26 15:48:28 -0700101 return -EAFNOSUPPORT;
102
103 switch (srx->transport.family) {
104 case AF_INET:
David Howells4f95dd72016-04-04 14:00:35 +0100105 if (srx->transport_len < sizeof(struct sockaddr_in))
106 return -EINVAL;
Harvey Harrison21454aa2008-10-31 00:54:56 -0700107 _debug("INET: %x @ %pI4",
David Howells17926a72007-04-26 15:48:28 -0700108 ntohs(srx->transport.sin.sin_port),
Harvey Harrison21454aa2008-10-31 00:54:56 -0700109 &srx->transport.sin.sin_addr);
David Howellsab802ee2016-03-04 15:59:49 +0000110 tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
David Howells17926a72007-04-26 15:48:28 -0700111 break;
112
113 case AF_INET6:
114 default:
115 return -EAFNOSUPPORT;
116 }
117
David Howellsab802ee2016-03-04 15:59:49 +0000118 if (tail < len)
119 memset((void *)srx + tail, 0, len - tail);
David Howells17926a72007-04-26 15:48:28 -0700120 return 0;
121}
122
123/*
124 * bind a local address to an RxRPC socket
125 */
126static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
127{
David Howellsb4f13422016-03-04 15:56:19 +0000128 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
David Howells17926a72007-04-26 15:48:28 -0700129 struct sock *sk = sock->sk;
130 struct rxrpc_local *local;
131 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
David Howells17926a72007-04-26 15:48:28 -0700132 int ret;
133
134 _enter("%p,%p,%d", rx, saddr, len);
135
136 ret = rxrpc_validate_address(rx, srx, len);
137 if (ret < 0)
138 goto error;
139
140 lock_sock(&rx->sk);
141
David Howells2341e072016-06-09 23:02:51 +0100142 if (rx->sk.sk_state != RXRPC_UNBOUND) {
David Howells17926a72007-04-26 15:48:28 -0700143 ret = -EINVAL;
144 goto error_unlock;
145 }
146
147 memcpy(&rx->srx, srx, sizeof(rx->srx));
148
David Howells17926a72007-04-26 15:48:28 -0700149 local = rxrpc_lookup_local(&rx->srx);
150 if (IS_ERR(local)) {
151 ret = PTR_ERR(local);
152 goto error_unlock;
153 }
154
David Howells2341e072016-06-09 23:02:51 +0100155 if (rx->srx.srx_service) {
David Howells17926a72007-04-26 15:48:28 -0700156 write_lock_bh(&local->services_lock);
157 list_for_each_entry(prx, &local->services, listen_link) {
David Howells2341e072016-06-09 23:02:51 +0100158 if (prx->srx.srx_service == rx->srx.srx_service)
David Howells17926a72007-04-26 15:48:28 -0700159 goto service_in_use;
160 }
161
David Howells2341e072016-06-09 23:02:51 +0100162 rx->local = local;
David Howells17926a72007-04-26 15:48:28 -0700163 list_add_tail(&rx->listen_link, &local->services);
164 write_unlock_bh(&local->services_lock);
165
166 rx->sk.sk_state = RXRPC_SERVER_BOUND;
167 } else {
David Howells2341e072016-06-09 23:02:51 +0100168 rx->local = local;
David Howells17926a72007-04-26 15:48:28 -0700169 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
170 }
171
172 release_sock(&rx->sk);
173 _leave(" = 0");
174 return 0;
175
176service_in_use:
David Howells17926a72007-04-26 15:48:28 -0700177 write_unlock_bh(&local->services_lock);
David Howells2341e072016-06-09 23:02:51 +0100178 rxrpc_put_local(local);
179 ret = -EADDRINUSE;
David Howells17926a72007-04-26 15:48:28 -0700180error_unlock:
181 release_sock(&rx->sk);
182error:
183 _leave(" = %d", ret);
184 return ret;
185}
186
187/*
188 * set the number of pending calls permitted on a listening socket
189 */
190static int rxrpc_listen(struct socket *sock, int backlog)
191{
192 struct sock *sk = sock->sk;
193 struct rxrpc_sock *rx = rxrpc_sk(sk);
David Howells0e119b42016-06-10 22:30:37 +0100194 unsigned int max;
David Howells17926a72007-04-26 15:48:28 -0700195 int ret;
196
197 _enter("%p,%d", rx, backlog);
198
199 lock_sock(&rx->sk);
200
201 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100202 case RXRPC_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700203 ret = -EADDRNOTAVAIL;
204 break;
David Howells17926a72007-04-26 15:48:28 -0700205 case RXRPC_SERVER_BOUND:
206 ASSERT(rx->local != NULL);
David Howells0e119b42016-06-10 22:30:37 +0100207 max = READ_ONCE(rxrpc_max_backlog);
208 ret = -EINVAL;
209 if (backlog == INT_MAX)
210 backlog = max;
211 else if (backlog < 0 || backlog > max)
212 break;
David Howells17926a72007-04-26 15:48:28 -0700213 sk->sk_max_ack_backlog = backlog;
214 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
215 ret = 0;
216 break;
David Howells0e119b42016-06-10 22:30:37 +0100217 default:
218 ret = -EBUSY;
219 break;
David Howells17926a72007-04-26 15:48:28 -0700220 }
221
222 release_sock(&rx->sk);
223 _leave(" = %d", ret);
224 return ret;
225}
226
227/*
228 * find a transport by address
229 */
David Howells19ffa012016-04-04 14:00:36 +0100230struct rxrpc_transport *
231rxrpc_name_to_transport(struct rxrpc_conn_parameters *cp,
232 struct sockaddr *addr,
233 int addr_len,
234 gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700235{
236 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
237 struct rxrpc_transport *trans;
David Howells17926a72007-04-26 15:48:28 -0700238
David Howells19ffa012016-04-04 14:00:36 +0100239 _enter("%p,%d", addr, addr_len);
David Howells17926a72007-04-26 15:48:28 -0700240
David Howells19ffa012016-04-04 14:00:36 +0100241 if (cp->local->srx.transport_type != srx->transport_type)
David Howells17926a72007-04-26 15:48:28 -0700242 return ERR_PTR(-ESOCKTNOSUPPORT);
David Howells19ffa012016-04-04 14:00:36 +0100243 if (cp->local->srx.transport.family != srx->transport.family)
David Howells17926a72007-04-26 15:48:28 -0700244 return ERR_PTR(-EAFNOSUPPORT);
245
246 /* find a remote transport endpoint from the local one */
David Howells19ffa012016-04-04 14:00:36 +0100247 cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
248 if (!cp->peer)
Dan Carpenter0e4699e2016-06-18 11:44:03 +0300249 return ERR_PTR(-ENOMEM);
David Howells17926a72007-04-26 15:48:28 -0700250
251 /* find a transport */
David Howells19ffa012016-04-04 14:00:36 +0100252 trans = rxrpc_get_transport(cp->local, cp->peer, gfp);
253 rxrpc_put_peer(cp->peer);
David Howells17926a72007-04-26 15:48:28 -0700254 _leave(" = %p", trans);
255 return trans;
256}
257
David Howells651350d2007-04-26 15:50:17 -0700258/**
259 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
260 * @sock: The socket on which to make the call
David Howells2341e072016-06-09 23:02:51 +0100261 * @srx: The address of the peer to contact
David Howells651350d2007-04-26 15:50:17 -0700262 * @key: The security context to use (defaults to socket setting)
263 * @user_call_ID: The ID to use
264 *
265 * Allow a kernel service to begin a call on the nominated socket. This just
266 * sets up all the internal tracking structures and allocates connection and
267 * call IDs as appropriate. The call to be used is returned.
268 *
269 * The default socket destination address and security may be overridden by
270 * supplying @srx and @key.
271 */
272struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
273 struct sockaddr_rxrpc *srx,
274 struct key *key,
275 unsigned long user_call_ID,
276 gfp_t gfp)
277{
David Howells19ffa012016-04-04 14:00:36 +0100278 struct rxrpc_conn_parameters cp;
David Howells651350d2007-04-26 15:50:17 -0700279 struct rxrpc_conn_bundle *bundle;
280 struct rxrpc_transport *trans;
281 struct rxrpc_call *call;
282 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howellsf4552c22016-06-17 11:00:48 +0100283 int ret;
David Howells651350d2007-04-26 15:50:17 -0700284
285 _enter(",,%x,%lx", key_serial(key), user_call_ID);
286
David Howellsf4552c22016-06-17 11:00:48 +0100287 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
288 if (ret < 0)
289 return ERR_PTR(ret);
290
David Howells651350d2007-04-26 15:50:17 -0700291 lock_sock(&rx->sk);
292
David Howells19ffa012016-04-04 14:00:36 +0100293 if (!key)
294 key = rx->key;
295 if (key && !key->payload.data[0])
296 key = NULL; /* a no-security key */
297
298 memset(&cp, 0, sizeof(cp));
299 cp.local = rx->local;
300 cp.key = key;
301 cp.security_level = 0;
302 cp.exclusive = false;
303 cp.service_id = srx->srx_service;
304
305 trans = rxrpc_name_to_transport(&cp, (struct sockaddr *)srx,
306 sizeof(*srx), gfp);
David Howells2341e072016-06-09 23:02:51 +0100307 if (IS_ERR(trans)) {
308 call = ERR_CAST(trans);
309 trans = NULL;
310 goto out_notrans;
David Howells651350d2007-04-26 15:50:17 -0700311 }
David Howells19ffa012016-04-04 14:00:36 +0100312 cp.peer = trans->peer;
David Howells651350d2007-04-26 15:50:17 -0700313
David Howells0d12f8a2016-03-04 15:53:46 +0000314 bundle = rxrpc_get_bundle(rx, trans, key, srx->srx_service, gfp);
David Howells651350d2007-04-26 15:50:17 -0700315 if (IS_ERR(bundle)) {
David Howellse231c2e2008-02-07 00:15:26 -0800316 call = ERR_CAST(bundle);
David Howells651350d2007-04-26 15:50:17 -0700317 goto out;
318 }
319
David Howells19ffa012016-04-04 14:00:36 +0100320 call = rxrpc_new_client_call(rx, &cp, trans, bundle, user_call_ID, gfp);
David Howells651350d2007-04-26 15:50:17 -0700321 rxrpc_put_bundle(trans, bundle);
322out:
323 rxrpc_put_transport(trans);
David Howells15bde722009-02-06 21:50:52 -0800324out_notrans:
David Howells651350d2007-04-26 15:50:17 -0700325 release_sock(&rx->sk);
326 _leave(" = %p", call);
327 return call;
328}
David Howells651350d2007-04-26 15:50:17 -0700329EXPORT_SYMBOL(rxrpc_kernel_begin_call);
330
331/**
332 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
333 * @call: The call to end
334 *
335 * Allow a kernel service to end a call it was using. The call must be
336 * complete before this is called (the call should be aborted if necessary).
337 */
338void rxrpc_kernel_end_call(struct rxrpc_call *call)
339{
340 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
341 rxrpc_remove_user_ID(call->socket, call);
342 rxrpc_put_call(call);
343}
David Howells651350d2007-04-26 15:50:17 -0700344EXPORT_SYMBOL(rxrpc_kernel_end_call);
345
346/**
347 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
348 * @sock: The socket to intercept received messages on
349 * @interceptor: The function to pass the messages to
350 *
351 * Allow a kernel service to intercept messages heading for the Rx queue on an
352 * RxRPC socket. They get passed to the specified function instead.
353 * @interceptor should free the socket buffers it is given. @interceptor is
354 * called with the socket receive queue spinlock held and softirqs disabled -
355 * this ensures that the messages will be delivered in the right order.
356 */
357void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
358 rxrpc_interceptor_t interceptor)
359{
360 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
361
362 _enter("");
363 rx->interceptor = interceptor;
364}
365
366EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
367
David Howells17926a72007-04-26 15:48:28 -0700368/*
369 * connect an RxRPC socket
370 * - this just targets it at a specific destination; no actual connection
371 * negotiation takes place
372 */
373static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
374 int addr_len, int flags)
375{
David Howells2341e072016-06-09 23:02:51 +0100376 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
377 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells17926a72007-04-26 15:48:28 -0700378 int ret;
379
380 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
381
382 ret = rxrpc_validate_address(rx, srx, addr_len);
383 if (ret < 0) {
384 _leave(" = %d [bad addr]", ret);
385 return ret;
386 }
387
388 lock_sock(&rx->sk);
389
David Howells2341e072016-06-09 23:02:51 +0100390 ret = -EISCONN;
391 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
392 goto error;
393
David Howells17926a72007-04-26 15:48:28 -0700394 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100395 case RXRPC_UNBOUND:
396 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
397 case RXRPC_CLIENT_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700398 case RXRPC_CLIENT_BOUND:
399 break;
David Howells17926a72007-04-26 15:48:28 -0700400 default:
David Howells2341e072016-06-09 23:02:51 +0100401 ret = -EBUSY;
402 goto error;
David Howells17926a72007-04-26 15:48:28 -0700403 }
404
David Howells2341e072016-06-09 23:02:51 +0100405 rx->connect_srx = *srx;
406 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
407 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700408
David Howells2341e072016-06-09 23:02:51 +0100409error:
David Howells17926a72007-04-26 15:48:28 -0700410 release_sock(&rx->sk);
David Howells2341e072016-06-09 23:02:51 +0100411 return ret;
David Howells17926a72007-04-26 15:48:28 -0700412}
413
414/*
415 * send a message through an RxRPC socket
416 * - in a client this does a number of things:
417 * - finds/sets up a connection for the security specified (if any)
418 * - initiates a call (ID in control data)
419 * - ends the request phase of a call (if MSG_MORE is not set)
420 * - sends a call data packet
421 * - may send an abort (abort code in control data)
422 */
Ying Xue1b784142015-03-02 15:37:48 +0800423static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700424{
David Howells2341e072016-06-09 23:02:51 +0100425 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700426 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
427 int ret;
428
429 _enter(",{%d},,%zu", rx->sk.sk_state, len);
430
431 if (m->msg_flags & MSG_OOB)
432 return -EOPNOTSUPP;
433
434 if (m->msg_name) {
435 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
436 if (ret < 0) {
437 _leave(" = %d [bad addr]", ret);
438 return ret;
439 }
440 }
441
David Howells17926a72007-04-26 15:48:28 -0700442 lock_sock(&rx->sk);
443
David Howells17926a72007-04-26 15:48:28 -0700444 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100445 case RXRPC_UNBOUND:
446 local = rxrpc_lookup_local(&rx->srx);
447 if (IS_ERR(local)) {
448 ret = PTR_ERR(local);
449 goto error_unlock;
450 }
451
452 rx->local = local;
453 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
454 /* Fall through */
455
456 case RXRPC_CLIENT_UNBOUND:
457 case RXRPC_CLIENT_BOUND:
458 if (!m->msg_name &&
459 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
460 m->msg_name = &rx->connect_srx;
461 m->msg_namelen = sizeof(rx->connect_srx);
David Howells17926a72007-04-26 15:48:28 -0700462 }
463 case RXRPC_SERVER_BOUND:
David Howells2341e072016-06-09 23:02:51 +0100464 case RXRPC_SERVER_LISTENING:
465 ret = rxrpc_do_sendmsg(rx, m, len);
David Howells17926a72007-04-26 15:48:28 -0700466 break;
467 default:
David Howells2341e072016-06-09 23:02:51 +0100468 ret = -EINVAL;
David Howells17926a72007-04-26 15:48:28 -0700469 break;
470 }
471
David Howells2341e072016-06-09 23:02:51 +0100472error_unlock:
David Howells17926a72007-04-26 15:48:28 -0700473 release_sock(&rx->sk);
David Howells17926a72007-04-26 15:48:28 -0700474 _leave(" = %d", ret);
475 return ret;
476}
477
478/*
479 * set RxRPC socket options
480 */
481static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700482 char __user *optval, unsigned int optlen)
David Howells17926a72007-04-26 15:48:28 -0700483{
484 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000485 unsigned int min_sec_level;
David Howells17926a72007-04-26 15:48:28 -0700486 int ret;
487
488 _enter(",%d,%d,,%d", level, optname, optlen);
489
490 lock_sock(&rx->sk);
491 ret = -EOPNOTSUPP;
492
493 if (level == SOL_RXRPC) {
494 switch (optname) {
495 case RXRPC_EXCLUSIVE_CONNECTION:
496 ret = -EINVAL;
497 if (optlen != 0)
498 goto error;
499 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100500 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700501 goto error;
David Howellscc8feb82016-04-04 14:00:37 +0100502 rx->exclusive = true;
David Howells17926a72007-04-26 15:48:28 -0700503 goto success;
504
505 case RXRPC_SECURITY_KEY:
506 ret = -EINVAL;
507 if (rx->key)
508 goto error;
509 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100510 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700511 goto error;
512 ret = rxrpc_request_key(rx, optval, optlen);
513 goto error;
514
515 case RXRPC_SECURITY_KEYRING:
516 ret = -EINVAL;
517 if (rx->key)
518 goto error;
519 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100520 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700521 goto error;
522 ret = rxrpc_server_keyring(rx, optval, optlen);
523 goto error;
524
525 case RXRPC_MIN_SECURITY_LEVEL:
526 ret = -EINVAL;
Eric Dumazet95c96172012-04-15 05:58:06 +0000527 if (optlen != sizeof(unsigned int))
David Howells17926a72007-04-26 15:48:28 -0700528 goto error;
529 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100530 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700531 goto error;
532 ret = get_user(min_sec_level,
Eric Dumazet95c96172012-04-15 05:58:06 +0000533 (unsigned int __user *) optval);
David Howells17926a72007-04-26 15:48:28 -0700534 if (ret < 0)
535 goto error;
536 ret = -EINVAL;
537 if (min_sec_level > RXRPC_SECURITY_MAX)
538 goto error;
539 rx->min_sec_level = min_sec_level;
540 goto success;
541
542 default:
543 break;
544 }
545 }
546
547success:
548 ret = 0;
549error:
550 release_sock(&rx->sk);
551 return ret;
552}
553
554/*
555 * permit an RxRPC socket to be polled
556 */
557static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
558 poll_table *wait)
559{
560 unsigned int mask;
561 struct sock *sk = sock->sk;
562
Eric Dumazetaa395142010-04-20 13:03:51 +0000563 sock_poll_wait(file, sk_sleep(sk), wait);
David Howells17926a72007-04-26 15:48:28 -0700564 mask = 0;
565
566 /* the socket is readable if there are any messages waiting on the Rx
567 * queue */
568 if (!skb_queue_empty(&sk->sk_receive_queue))
569 mask |= POLLIN | POLLRDNORM;
570
571 /* the socket is writable if there is space to add new data to the
572 * socket; there is no guarantee that any particular call in progress
573 * on the socket may have space in the Tx ACK window */
574 if (rxrpc_writable(sk))
575 mask |= POLLOUT | POLLWRNORM;
576
577 return mask;
578}
579
580/*
581 * create an RxRPC socket
582 */
Eric Paris3f378b62009-11-05 22:18:14 -0800583static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
584 int kern)
David Howells17926a72007-04-26 15:48:28 -0700585{
586 struct rxrpc_sock *rx;
587 struct sock *sk;
588
589 _enter("%p,%d", sock, protocol);
590
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800591 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700592 return -EAFNOSUPPORT;
593
David Howellsb4f13422016-03-04 15:56:19 +0000594 /* we support transport protocol UDP/UDP6 only */
David Howells17926a72007-04-26 15:48:28 -0700595 if (protocol != PF_INET)
596 return -EPROTONOSUPPORT;
597
598 if (sock->type != SOCK_DGRAM)
599 return -ESOCKTNOSUPPORT;
600
601 sock->ops = &rxrpc_rpc_ops;
602 sock->state = SS_UNCONNECTED;
603
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500604 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
David Howells17926a72007-04-26 15:48:28 -0700605 if (!sk)
606 return -ENOMEM;
607
608 sock_init_data(sock, sk);
David Howells2341e072016-06-09 23:02:51 +0100609 sk->sk_state = RXRPC_UNBOUND;
David Howells17926a72007-04-26 15:48:28 -0700610 sk->sk_write_space = rxrpc_write_space;
David Howells0e119b42016-06-10 22:30:37 +0100611 sk->sk_max_ack_backlog = 0;
David Howells17926a72007-04-26 15:48:28 -0700612 sk->sk_destruct = rxrpc_sock_destructor;
613
614 rx = rxrpc_sk(sk);
David Howells19ffa012016-04-04 14:00:36 +0100615 rx->family = protocol;
David Howells17926a72007-04-26 15:48:28 -0700616 rx->calls = RB_ROOT;
617
618 INIT_LIST_HEAD(&rx->listen_link);
619 INIT_LIST_HEAD(&rx->secureq);
620 INIT_LIST_HEAD(&rx->acceptq);
621 rwlock_init(&rx->call_lock);
622 memset(&rx->srx, 0, sizeof(rx->srx));
623
624 _leave(" = 0 [%p]", rx);
625 return 0;
626}
627
628/*
629 * RxRPC socket destructor
630 */
631static void rxrpc_sock_destructor(struct sock *sk)
632{
633 _enter("%p", sk);
634
635 rxrpc_purge_queue(&sk->sk_receive_queue);
636
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700637 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
638 WARN_ON(!sk_unhashed(sk));
639 WARN_ON(sk->sk_socket);
David Howells17926a72007-04-26 15:48:28 -0700640
641 if (!sock_flag(sk, SOCK_DEAD)) {
642 printk("Attempt to release alive rxrpc socket: %p\n", sk);
643 return;
644 }
645}
646
647/*
648 * release an RxRPC socket
649 */
650static int rxrpc_release_sock(struct sock *sk)
651{
652 struct rxrpc_sock *rx = rxrpc_sk(sk);
653
654 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
655
656 /* declare the socket closed for business */
657 sock_orphan(sk);
658 sk->sk_shutdown = SHUTDOWN_MASK;
659
660 spin_lock_bh(&sk->sk_receive_queue.lock);
661 sk->sk_state = RXRPC_CLOSE;
662 spin_unlock_bh(&sk->sk_receive_queue.lock);
663
664 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
665
666 if (!list_empty(&rx->listen_link)) {
667 write_lock_bh(&rx->local->services_lock);
668 list_del(&rx->listen_link);
669 write_unlock_bh(&rx->local->services_lock);
670 }
671
672 /* try to flush out this socket */
673 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700674 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700675 rxrpc_purge_queue(&sk->sk_receive_queue);
676
David Howells17926a72007-04-26 15:48:28 -0700677 if (rx->local) {
678 rxrpc_put_local(rx->local);
679 rx->local = NULL;
680 }
681
682 key_put(rx->key);
683 rx->key = NULL;
684 key_put(rx->securities);
685 rx->securities = NULL;
686 sock_put(sk);
687
688 _leave(" = 0");
689 return 0;
690}
691
692/*
693 * release an RxRPC BSD socket on close() or equivalent
694 */
695static int rxrpc_release(struct socket *sock)
696{
697 struct sock *sk = sock->sk;
698
699 _enter("%p{%p}", sock, sk);
700
701 if (!sk)
702 return 0;
703
704 sock->sk = NULL;
705
706 return rxrpc_release_sock(sk);
707}
708
709/*
710 * RxRPC network protocol
711 */
712static const struct proto_ops rxrpc_rpc_ops = {
David Howellse33b3d92016-03-04 15:54:27 +0000713 .family = PF_RXRPC,
David Howells17926a72007-04-26 15:48:28 -0700714 .owner = THIS_MODULE,
715 .release = rxrpc_release,
716 .bind = rxrpc_bind,
717 .connect = rxrpc_connect,
718 .socketpair = sock_no_socketpair,
719 .accept = sock_no_accept,
720 .getname = sock_no_getname,
721 .poll = rxrpc_poll,
722 .ioctl = sock_no_ioctl,
723 .listen = rxrpc_listen,
724 .shutdown = sock_no_shutdown,
725 .setsockopt = rxrpc_setsockopt,
726 .getsockopt = sock_no_getsockopt,
727 .sendmsg = rxrpc_sendmsg,
728 .recvmsg = rxrpc_recvmsg,
729 .mmap = sock_no_mmap,
730 .sendpage = sock_no_sendpage,
731};
732
733static struct proto rxrpc_proto = {
734 .name = "RXRPC",
735 .owner = THIS_MODULE,
736 .obj_size = sizeof(struct rxrpc_sock),
David Howells0d12f8a2016-03-04 15:53:46 +0000737 .max_header = sizeof(struct rxrpc_wire_header),
David Howells17926a72007-04-26 15:48:28 -0700738};
739
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000740static const struct net_proto_family rxrpc_family_ops = {
David Howells17926a72007-04-26 15:48:28 -0700741 .family = PF_RXRPC,
742 .create = rxrpc_create,
743 .owner = THIS_MODULE,
744};
745
746/*
747 * initialise and register the RxRPC protocol
748 */
749static int __init af_rxrpc_init(void)
750{
David Howells17926a72007-04-26 15:48:28 -0700751 int ret = -1;
752
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +0000753 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
David Howells17926a72007-04-26 15:48:28 -0700754
David Howells0d12f8a2016-03-04 15:53:46 +0000755 rxrpc_epoch = get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700756
David Howells651350d2007-04-26 15:50:17 -0700757 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700758 rxrpc_call_jar = kmem_cache_create(
759 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900760 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700761 if (!rxrpc_call_jar) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700762 pr_notice("Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700763 goto error_call_jar;
764 }
765
Tejun Heoe1fcc7e2011-01-14 15:56:31 +0000766 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
David Howells651350d2007-04-26 15:50:17 -0700767 if (!rxrpc_workqueue) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700768 pr_notice("Failed to allocate work queue\n");
David Howells651350d2007-04-26 15:50:17 -0700769 goto error_work_queue;
770 }
771
David Howells648af7f2016-04-07 17:23:51 +0100772 ret = rxrpc_init_security();
773 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700774 pr_crit("Cannot initialise security\n");
David Howells648af7f2016-04-07 17:23:51 +0100775 goto error_security;
776 }
777
David Howells17926a72007-04-26 15:48:28 -0700778 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900779 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700780 pr_crit("Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700781 goto error_proto;
782 }
783
784 ret = sock_register(&rxrpc_family_ops);
785 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700786 pr_crit("Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700787 goto error_sock;
788 }
789
790 ret = register_key_type(&key_type_rxrpc);
791 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700792 pr_crit("Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700793 goto error_key_type;
794 }
795
796 ret = register_key_type(&key_type_rxrpc_s);
797 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700798 pr_crit("Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700799 goto error_key_type_s;
800 }
801
David Howells5873c082014-02-07 18:58:44 +0000802 ret = rxrpc_sysctl_init();
803 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700804 pr_crit("Cannot register sysctls\n");
David Howells5873c082014-02-07 18:58:44 +0000805 goto error_sysctls;
806 }
807
David Howells17926a72007-04-26 15:48:28 -0700808#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +0000809 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
810 proc_create("rxrpc_conns", 0, init_net.proc_net,
811 &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700812#endif
813 return 0;
814
David Howells5873c082014-02-07 18:58:44 +0000815error_sysctls:
816 unregister_key_type(&key_type_rxrpc_s);
David Howells17926a72007-04-26 15:48:28 -0700817error_key_type_s:
818 unregister_key_type(&key_type_rxrpc);
819error_key_type:
820 sock_unregister(PF_RXRPC);
821error_sock:
822 proto_unregister(&rxrpc_proto);
823error_proto:
David Howells651350d2007-04-26 15:50:17 -0700824 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100825error_security:
826 rxrpc_exit_security();
David Howells651350d2007-04-26 15:50:17 -0700827error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700828 kmem_cache_destroy(rxrpc_call_jar);
829error_call_jar:
830 return ret;
831}
832
833/*
834 * unregister the RxRPC protocol
835 */
836static void __exit af_rxrpc_exit(void)
837{
838 _enter("");
David Howells5873c082014-02-07 18:58:44 +0000839 rxrpc_sysctl_exit();
David Howells17926a72007-04-26 15:48:28 -0700840 unregister_key_type(&key_type_rxrpc_s);
841 unregister_key_type(&key_type_rxrpc);
842 sock_unregister(PF_RXRPC);
843 proto_unregister(&rxrpc_proto);
844 rxrpc_destroy_all_calls();
845 rxrpc_destroy_all_connections();
846 rxrpc_destroy_all_transports();
David Howells17926a72007-04-26 15:48:28 -0700847
848 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
849
David Howells4f95dd72016-04-04 14:00:35 +0100850 /* We need to flush the scheduled work twice because the local endpoint
851 * records involve a work item in their destruction as they can only be
852 * destroyed from process context. However, a connection may have a
853 * work item outstanding - and this will pin the local endpoint record
854 * until the connection goes away.
855 *
856 * Peers don't pin locals and calls pin sockets - which prevents the
857 * module from being unloaded - so we should only need two flushes.
858 */
David Howells17926a72007-04-26 15:48:28 -0700859 _debug("flush scheduled work");
David Howells651350d2007-04-26 15:50:17 -0700860 flush_workqueue(rxrpc_workqueue);
David Howells4f95dd72016-04-04 14:00:35 +0100861 _debug("flush scheduled work 2");
862 flush_workqueue(rxrpc_workqueue);
863 _debug("synchronise RCU");
864 rcu_barrier();
865 _debug("destroy locals");
David Howells4a3388c2016-04-04 14:00:37 +0100866 ASSERT(idr_is_empty(&rxrpc_client_conn_ids));
867 idr_destroy(&rxrpc_client_conn_ids);
David Howells4f95dd72016-04-04 14:00:35 +0100868 rxrpc_destroy_all_locals();
869
Gao fengece31ff2013-02-18 01:34:56 +0000870 remove_proc_entry("rxrpc_conns", init_net.proc_net);
871 remove_proc_entry("rxrpc_calls", init_net.proc_net);
David Howells651350d2007-04-26 15:50:17 -0700872 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100873 rxrpc_exit_security();
David Howells17926a72007-04-26 15:48:28 -0700874 kmem_cache_destroy(rxrpc_call_jar);
875 _leave("");
876}
877
878module_init(af_rxrpc_init);
879module_exit(af_rxrpc_exit);