blob: 38512a200db683e643df6f5f185cd0d86405949f [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
36static int sysctl_rxrpc_max_qlen __read_mostly = 10;
37
38static struct proto rxrpc_proto;
39static const struct proto_ops rxrpc_rpc_ops;
40
41/* local epoch for detecting local-end reset */
David Howells0d12f8a2016-03-04 15:53:46 +000042u32 rxrpc_epoch;
David Howells17926a72007-04-26 15:48:28 -070043
44/* current debugging ID */
45atomic_t rxrpc_debug_id;
46
47/* count of skbs currently in use */
48atomic_t rxrpc_n_skbs;
49
David Howells651350d2007-04-26 15:50:17 -070050struct workqueue_struct *rxrpc_workqueue;
51
David Howells17926a72007-04-26 15:48:28 -070052static void rxrpc_sock_destructor(struct sock *);
53
54/*
55 * see if an RxRPC socket is currently writable
56 */
57static inline int rxrpc_writable(struct sock *sk)
58{
59 return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
60}
61
62/*
63 * wait for write bufferage to become available
64 */
65static void rxrpc_write_space(struct sock *sk)
66{
67 _enter("%p", sk);
Eric Dumazet43815482010-04-29 11:01:49 +000068 rcu_read_lock();
David Howells17926a72007-04-26 15:48:28 -070069 if (rxrpc_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +000070 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
71
Herbert Xu1ce0bf52015-11-26 13:55:39 +080072 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +000073 wake_up_interruptible(&wq->wait);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +080074 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
David Howells17926a72007-04-26 15:48:28 -070075 }
Eric Dumazet43815482010-04-29 11:01:49 +000076 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -070077}
78
79/*
80 * validate an RxRPC address
81 */
82static int rxrpc_validate_address(struct rxrpc_sock *rx,
83 struct sockaddr_rxrpc *srx,
84 int len)
85{
David Howellsdad8aff2016-03-09 23:22:56 +000086 unsigned int tail;
David Howellsab802ee2016-03-04 15:59:49 +000087
David Howells17926a72007-04-26 15:48:28 -070088 if (len < sizeof(struct sockaddr_rxrpc))
89 return -EINVAL;
90
91 if (srx->srx_family != AF_RXRPC)
92 return -EAFNOSUPPORT;
93
94 if (srx->transport_type != SOCK_DGRAM)
95 return -ESOCKTNOSUPPORT;
96
97 len -= offsetof(struct sockaddr_rxrpc, transport);
98 if (srx->transport_len < sizeof(sa_family_t) ||
99 srx->transport_len > len)
100 return -EINVAL;
101
102 if (srx->transport.family != rx->proto)
103 return -EAFNOSUPPORT;
104
105 switch (srx->transport.family) {
106 case AF_INET:
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);
194 int ret;
195
196 _enter("%p,%d", rx, backlog);
197
198 lock_sock(&rx->sk);
199
200 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100201 case RXRPC_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700202 ret = -EADDRNOTAVAIL;
203 break;
David Howells2341e072016-06-09 23:02:51 +0100204 case RXRPC_CLIENT_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700205 case RXRPC_CLIENT_BOUND:
David Howells17926a72007-04-26 15:48:28 -0700206 default:
207 ret = -EBUSY;
208 break;
209 case RXRPC_SERVER_BOUND:
210 ASSERT(rx->local != NULL);
211 sk->sk_max_ack_backlog = backlog;
212 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
213 ret = 0;
214 break;
215 }
216
217 release_sock(&rx->sk);
218 _leave(" = %d", ret);
219 return ret;
220}
221
222/*
223 * find a transport by address
224 */
David Howells2341e072016-06-09 23:02:51 +0100225struct rxrpc_transport *rxrpc_name_to_transport(struct rxrpc_sock *rx,
226 struct sockaddr *addr,
227 int addr_len, int flags,
228 gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700229{
230 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
231 struct rxrpc_transport *trans;
David Howells17926a72007-04-26 15:48:28 -0700232 struct rxrpc_peer *peer;
233
234 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
235
236 ASSERT(rx->local != NULL);
David Howells17926a72007-04-26 15:48:28 -0700237
238 if (rx->srx.transport_type != srx->transport_type)
239 return ERR_PTR(-ESOCKTNOSUPPORT);
240 if (rx->srx.transport.family != srx->transport.family)
241 return ERR_PTR(-EAFNOSUPPORT);
242
243 /* find a remote transport endpoint from the local one */
David Howells651350d2007-04-26 15:50:17 -0700244 peer = rxrpc_get_peer(srx, gfp);
David Howells17926a72007-04-26 15:48:28 -0700245 if (IS_ERR(peer))
David Howellse231c2e2008-02-07 00:15:26 -0800246 return ERR_CAST(peer);
David Howells17926a72007-04-26 15:48:28 -0700247
248 /* find a transport */
David Howells651350d2007-04-26 15:50:17 -0700249 trans = rxrpc_get_transport(rx->local, peer, gfp);
David Howells17926a72007-04-26 15:48:28 -0700250 rxrpc_put_peer(peer);
251 _leave(" = %p", trans);
252 return trans;
253}
254
David Howells651350d2007-04-26 15:50:17 -0700255/**
256 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
257 * @sock: The socket on which to make the call
David Howells2341e072016-06-09 23:02:51 +0100258 * @srx: The address of the peer to contact
David Howells651350d2007-04-26 15:50:17 -0700259 * @key: The security context to use (defaults to socket setting)
260 * @user_call_ID: The ID to use
261 *
262 * Allow a kernel service to begin a call on the nominated socket. This just
263 * sets up all the internal tracking structures and allocates connection and
264 * call IDs as appropriate. The call to be used is returned.
265 *
266 * The default socket destination address and security may be overridden by
267 * supplying @srx and @key.
268 */
269struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
270 struct sockaddr_rxrpc *srx,
271 struct key *key,
272 unsigned long user_call_ID,
273 gfp_t gfp)
274{
275 struct rxrpc_conn_bundle *bundle;
276 struct rxrpc_transport *trans;
277 struct rxrpc_call *call;
278 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells651350d2007-04-26 15:50:17 -0700279
280 _enter(",,%x,%lx", key_serial(key), user_call_ID);
281
282 lock_sock(&rx->sk);
283
David Howells2341e072016-06-09 23:02:51 +0100284 trans = rxrpc_name_to_transport(rx, (struct sockaddr *)srx,
285 sizeof(*srx), 0, gfp);
286 if (IS_ERR(trans)) {
287 call = ERR_CAST(trans);
288 trans = NULL;
289 goto out_notrans;
David Howells651350d2007-04-26 15:50:17 -0700290 }
291
David Howells651350d2007-04-26 15:50:17 -0700292 if (!key)
293 key = rx->key;
David Howells146aa8b2015-10-21 14:04:48 +0100294 if (key && !key->payload.data[0])
David Howells651350d2007-04-26 15:50:17 -0700295 key = NULL; /* a no-security key */
296
David Howells0d12f8a2016-03-04 15:53:46 +0000297 bundle = rxrpc_get_bundle(rx, trans, key, srx->srx_service, gfp);
David Howells651350d2007-04-26 15:50:17 -0700298 if (IS_ERR(bundle)) {
David Howellse231c2e2008-02-07 00:15:26 -0800299 call = ERR_CAST(bundle);
David Howells651350d2007-04-26 15:50:17 -0700300 goto out;
301 }
302
David Howells2341e072016-06-09 23:02:51 +0100303 call = rxrpc_new_client_call(rx, trans, bundle, user_call_ID, gfp);
David Howells651350d2007-04-26 15:50:17 -0700304 rxrpc_put_bundle(trans, bundle);
305out:
306 rxrpc_put_transport(trans);
David Howells15bde722009-02-06 21:50:52 -0800307out_notrans:
David Howells651350d2007-04-26 15:50:17 -0700308 release_sock(&rx->sk);
309 _leave(" = %p", call);
310 return call;
311}
David Howells651350d2007-04-26 15:50:17 -0700312EXPORT_SYMBOL(rxrpc_kernel_begin_call);
313
314/**
315 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
316 * @call: The call to end
317 *
318 * Allow a kernel service to end a call it was using. The call must be
319 * complete before this is called (the call should be aborted if necessary).
320 */
321void rxrpc_kernel_end_call(struct rxrpc_call *call)
322{
323 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
324 rxrpc_remove_user_ID(call->socket, call);
325 rxrpc_put_call(call);
326}
David Howells651350d2007-04-26 15:50:17 -0700327EXPORT_SYMBOL(rxrpc_kernel_end_call);
328
329/**
330 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
331 * @sock: The socket to intercept received messages on
332 * @interceptor: The function to pass the messages to
333 *
334 * Allow a kernel service to intercept messages heading for the Rx queue on an
335 * RxRPC socket. They get passed to the specified function instead.
336 * @interceptor should free the socket buffers it is given. @interceptor is
337 * called with the socket receive queue spinlock held and softirqs disabled -
338 * this ensures that the messages will be delivered in the right order.
339 */
340void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
341 rxrpc_interceptor_t interceptor)
342{
343 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
344
345 _enter("");
346 rx->interceptor = interceptor;
347}
348
349EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
350
David Howells17926a72007-04-26 15:48:28 -0700351/*
352 * connect an RxRPC socket
353 * - this just targets it at a specific destination; no actual connection
354 * negotiation takes place
355 */
356static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
357 int addr_len, int flags)
358{
David Howells2341e072016-06-09 23:02:51 +0100359 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
360 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells17926a72007-04-26 15:48:28 -0700361 int ret;
362
363 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
364
365 ret = rxrpc_validate_address(rx, srx, addr_len);
366 if (ret < 0) {
367 _leave(" = %d [bad addr]", ret);
368 return ret;
369 }
370
371 lock_sock(&rx->sk);
372
David Howells2341e072016-06-09 23:02:51 +0100373 ret = -EISCONN;
374 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
375 goto error;
376
David Howells17926a72007-04-26 15:48:28 -0700377 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100378 case RXRPC_UNBOUND:
379 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
380 case RXRPC_CLIENT_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700381 case RXRPC_CLIENT_BOUND:
382 break;
David Howells17926a72007-04-26 15:48:28 -0700383 default:
David Howells2341e072016-06-09 23:02:51 +0100384 ret = -EBUSY;
385 goto error;
David Howells17926a72007-04-26 15:48:28 -0700386 }
387
David Howells2341e072016-06-09 23:02:51 +0100388 rx->connect_srx = *srx;
389 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
390 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700391
David Howells2341e072016-06-09 23:02:51 +0100392error:
David Howells17926a72007-04-26 15:48:28 -0700393 release_sock(&rx->sk);
David Howells2341e072016-06-09 23:02:51 +0100394 return ret;
David Howells17926a72007-04-26 15:48:28 -0700395}
396
397/*
398 * send a message through an RxRPC socket
399 * - in a client this does a number of things:
400 * - finds/sets up a connection for the security specified (if any)
401 * - initiates a call (ID in control data)
402 * - ends the request phase of a call (if MSG_MORE is not set)
403 * - sends a call data packet
404 * - may send an abort (abort code in control data)
405 */
Ying Xue1b784142015-03-02 15:37:48 +0800406static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700407{
David Howells2341e072016-06-09 23:02:51 +0100408 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700409 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
410 int ret;
411
412 _enter(",{%d},,%zu", rx->sk.sk_state, len);
413
414 if (m->msg_flags & MSG_OOB)
415 return -EOPNOTSUPP;
416
417 if (m->msg_name) {
418 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
419 if (ret < 0) {
420 _leave(" = %d [bad addr]", ret);
421 return ret;
422 }
423 }
424
David Howells17926a72007-04-26 15:48:28 -0700425 lock_sock(&rx->sk);
426
David Howells17926a72007-04-26 15:48:28 -0700427 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100428 case RXRPC_UNBOUND:
429 local = rxrpc_lookup_local(&rx->srx);
430 if (IS_ERR(local)) {
431 ret = PTR_ERR(local);
432 goto error_unlock;
433 }
434
435 rx->local = local;
436 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
437 /* Fall through */
438
439 case RXRPC_CLIENT_UNBOUND:
440 case RXRPC_CLIENT_BOUND:
441 if (!m->msg_name &&
442 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
443 m->msg_name = &rx->connect_srx;
444 m->msg_namelen = sizeof(rx->connect_srx);
David Howells17926a72007-04-26 15:48:28 -0700445 }
446 case RXRPC_SERVER_BOUND:
David Howells2341e072016-06-09 23:02:51 +0100447 case RXRPC_SERVER_LISTENING:
448 ret = rxrpc_do_sendmsg(rx, m, len);
David Howells17926a72007-04-26 15:48:28 -0700449 break;
450 default:
David Howells2341e072016-06-09 23:02:51 +0100451 ret = -EINVAL;
David Howells17926a72007-04-26 15:48:28 -0700452 break;
453 }
454
David Howells2341e072016-06-09 23:02:51 +0100455error_unlock:
David Howells17926a72007-04-26 15:48:28 -0700456 release_sock(&rx->sk);
David Howells17926a72007-04-26 15:48:28 -0700457 _leave(" = %d", ret);
458 return ret;
459}
460
461/*
462 * set RxRPC socket options
463 */
464static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700465 char __user *optval, unsigned int optlen)
David Howells17926a72007-04-26 15:48:28 -0700466{
467 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000468 unsigned int min_sec_level;
David Howells17926a72007-04-26 15:48:28 -0700469 int ret;
470
471 _enter(",%d,%d,,%d", level, optname, optlen);
472
473 lock_sock(&rx->sk);
474 ret = -EOPNOTSUPP;
475
476 if (level == SOL_RXRPC) {
477 switch (optname) {
478 case RXRPC_EXCLUSIVE_CONNECTION:
479 ret = -EINVAL;
480 if (optlen != 0)
481 goto error;
482 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100483 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700484 goto error;
485 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
486 goto success;
487
488 case RXRPC_SECURITY_KEY:
489 ret = -EINVAL;
490 if (rx->key)
491 goto error;
492 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100493 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700494 goto error;
495 ret = rxrpc_request_key(rx, optval, optlen);
496 goto error;
497
498 case RXRPC_SECURITY_KEYRING:
499 ret = -EINVAL;
500 if (rx->key)
501 goto error;
502 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100503 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700504 goto error;
505 ret = rxrpc_server_keyring(rx, optval, optlen);
506 goto error;
507
508 case RXRPC_MIN_SECURITY_LEVEL:
509 ret = -EINVAL;
Eric Dumazet95c96172012-04-15 05:58:06 +0000510 if (optlen != sizeof(unsigned int))
David Howells17926a72007-04-26 15:48:28 -0700511 goto error;
512 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100513 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700514 goto error;
515 ret = get_user(min_sec_level,
Eric Dumazet95c96172012-04-15 05:58:06 +0000516 (unsigned int __user *) optval);
David Howells17926a72007-04-26 15:48:28 -0700517 if (ret < 0)
518 goto error;
519 ret = -EINVAL;
520 if (min_sec_level > RXRPC_SECURITY_MAX)
521 goto error;
522 rx->min_sec_level = min_sec_level;
523 goto success;
524
525 default:
526 break;
527 }
528 }
529
530success:
531 ret = 0;
532error:
533 release_sock(&rx->sk);
534 return ret;
535}
536
537/*
538 * permit an RxRPC socket to be polled
539 */
540static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
541 poll_table *wait)
542{
543 unsigned int mask;
544 struct sock *sk = sock->sk;
545
Eric Dumazetaa395142010-04-20 13:03:51 +0000546 sock_poll_wait(file, sk_sleep(sk), wait);
David Howells17926a72007-04-26 15:48:28 -0700547 mask = 0;
548
549 /* the socket is readable if there are any messages waiting on the Rx
550 * queue */
551 if (!skb_queue_empty(&sk->sk_receive_queue))
552 mask |= POLLIN | POLLRDNORM;
553
554 /* the socket is writable if there is space to add new data to the
555 * socket; there is no guarantee that any particular call in progress
556 * on the socket may have space in the Tx ACK window */
557 if (rxrpc_writable(sk))
558 mask |= POLLOUT | POLLWRNORM;
559
560 return mask;
561}
562
563/*
564 * create an RxRPC socket
565 */
Eric Paris3f378b62009-11-05 22:18:14 -0800566static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
567 int kern)
David Howells17926a72007-04-26 15:48:28 -0700568{
569 struct rxrpc_sock *rx;
570 struct sock *sk;
571
572 _enter("%p,%d", sock, protocol);
573
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800574 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700575 return -EAFNOSUPPORT;
576
David Howellsb4f13422016-03-04 15:56:19 +0000577 /* we support transport protocol UDP/UDP6 only */
David Howells17926a72007-04-26 15:48:28 -0700578 if (protocol != PF_INET)
579 return -EPROTONOSUPPORT;
580
581 if (sock->type != SOCK_DGRAM)
582 return -ESOCKTNOSUPPORT;
583
584 sock->ops = &rxrpc_rpc_ops;
585 sock->state = SS_UNCONNECTED;
586
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500587 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
David Howells17926a72007-04-26 15:48:28 -0700588 if (!sk)
589 return -ENOMEM;
590
591 sock_init_data(sock, sk);
David Howells2341e072016-06-09 23:02:51 +0100592 sk->sk_state = RXRPC_UNBOUND;
David Howells17926a72007-04-26 15:48:28 -0700593 sk->sk_write_space = rxrpc_write_space;
594 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
595 sk->sk_destruct = rxrpc_sock_destructor;
596
597 rx = rxrpc_sk(sk);
598 rx->proto = protocol;
599 rx->calls = RB_ROOT;
600
601 INIT_LIST_HEAD(&rx->listen_link);
602 INIT_LIST_HEAD(&rx->secureq);
603 INIT_LIST_HEAD(&rx->acceptq);
604 rwlock_init(&rx->call_lock);
605 memset(&rx->srx, 0, sizeof(rx->srx));
606
607 _leave(" = 0 [%p]", rx);
608 return 0;
609}
610
611/*
612 * RxRPC socket destructor
613 */
614static void rxrpc_sock_destructor(struct sock *sk)
615{
616 _enter("%p", sk);
617
618 rxrpc_purge_queue(&sk->sk_receive_queue);
619
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700620 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
621 WARN_ON(!sk_unhashed(sk));
622 WARN_ON(sk->sk_socket);
David Howells17926a72007-04-26 15:48:28 -0700623
624 if (!sock_flag(sk, SOCK_DEAD)) {
625 printk("Attempt to release alive rxrpc socket: %p\n", sk);
626 return;
627 }
628}
629
630/*
631 * release an RxRPC socket
632 */
633static int rxrpc_release_sock(struct sock *sk)
634{
635 struct rxrpc_sock *rx = rxrpc_sk(sk);
636
637 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
638
639 /* declare the socket closed for business */
640 sock_orphan(sk);
641 sk->sk_shutdown = SHUTDOWN_MASK;
642
643 spin_lock_bh(&sk->sk_receive_queue.lock);
644 sk->sk_state = RXRPC_CLOSE;
645 spin_unlock_bh(&sk->sk_receive_queue.lock);
646
647 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
648
649 if (!list_empty(&rx->listen_link)) {
650 write_lock_bh(&rx->local->services_lock);
651 list_del(&rx->listen_link);
652 write_unlock_bh(&rx->local->services_lock);
653 }
654
655 /* try to flush out this socket */
656 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700657 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700658 rxrpc_purge_queue(&sk->sk_receive_queue);
659
660 if (rx->conn) {
661 rxrpc_put_connection(rx->conn);
662 rx->conn = NULL;
663 }
664
David Howells17926a72007-04-26 15:48:28 -0700665 if (rx->local) {
666 rxrpc_put_local(rx->local);
667 rx->local = NULL;
668 }
669
670 key_put(rx->key);
671 rx->key = NULL;
672 key_put(rx->securities);
673 rx->securities = NULL;
674 sock_put(sk);
675
676 _leave(" = 0");
677 return 0;
678}
679
680/*
681 * release an RxRPC BSD socket on close() or equivalent
682 */
683static int rxrpc_release(struct socket *sock)
684{
685 struct sock *sk = sock->sk;
686
687 _enter("%p{%p}", sock, sk);
688
689 if (!sk)
690 return 0;
691
692 sock->sk = NULL;
693
694 return rxrpc_release_sock(sk);
695}
696
697/*
698 * RxRPC network protocol
699 */
700static const struct proto_ops rxrpc_rpc_ops = {
David Howellse33b3d92016-03-04 15:54:27 +0000701 .family = PF_RXRPC,
David Howells17926a72007-04-26 15:48:28 -0700702 .owner = THIS_MODULE,
703 .release = rxrpc_release,
704 .bind = rxrpc_bind,
705 .connect = rxrpc_connect,
706 .socketpair = sock_no_socketpair,
707 .accept = sock_no_accept,
708 .getname = sock_no_getname,
709 .poll = rxrpc_poll,
710 .ioctl = sock_no_ioctl,
711 .listen = rxrpc_listen,
712 .shutdown = sock_no_shutdown,
713 .setsockopt = rxrpc_setsockopt,
714 .getsockopt = sock_no_getsockopt,
715 .sendmsg = rxrpc_sendmsg,
716 .recvmsg = rxrpc_recvmsg,
717 .mmap = sock_no_mmap,
718 .sendpage = sock_no_sendpage,
719};
720
721static struct proto rxrpc_proto = {
722 .name = "RXRPC",
723 .owner = THIS_MODULE,
724 .obj_size = sizeof(struct rxrpc_sock),
David Howells0d12f8a2016-03-04 15:53:46 +0000725 .max_header = sizeof(struct rxrpc_wire_header),
David Howells17926a72007-04-26 15:48:28 -0700726};
727
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000728static const struct net_proto_family rxrpc_family_ops = {
David Howells17926a72007-04-26 15:48:28 -0700729 .family = PF_RXRPC,
730 .create = rxrpc_create,
731 .owner = THIS_MODULE,
732};
733
734/*
735 * initialise and register the RxRPC protocol
736 */
737static int __init af_rxrpc_init(void)
738{
David Howells17926a72007-04-26 15:48:28 -0700739 int ret = -1;
740
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +0000741 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
David Howells17926a72007-04-26 15:48:28 -0700742
David Howells0d12f8a2016-03-04 15:53:46 +0000743 rxrpc_epoch = get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700744
David Howells651350d2007-04-26 15:50:17 -0700745 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700746 rxrpc_call_jar = kmem_cache_create(
747 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900748 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700749 if (!rxrpc_call_jar) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700750 pr_notice("Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700751 goto error_call_jar;
752 }
753
Tejun Heoe1fcc7e2011-01-14 15:56:31 +0000754 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
David Howells651350d2007-04-26 15:50:17 -0700755 if (!rxrpc_workqueue) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700756 pr_notice("Failed to allocate work queue\n");
David Howells651350d2007-04-26 15:50:17 -0700757 goto error_work_queue;
758 }
759
David Howells648af7f2016-04-07 17:23:51 +0100760 ret = rxrpc_init_security();
761 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700762 pr_crit("Cannot initialise security\n");
David Howells648af7f2016-04-07 17:23:51 +0100763 goto error_security;
764 }
765
David Howells17926a72007-04-26 15:48:28 -0700766 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900767 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700768 pr_crit("Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700769 goto error_proto;
770 }
771
772 ret = sock_register(&rxrpc_family_ops);
773 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700774 pr_crit("Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700775 goto error_sock;
776 }
777
778 ret = register_key_type(&key_type_rxrpc);
779 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700780 pr_crit("Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700781 goto error_key_type;
782 }
783
784 ret = register_key_type(&key_type_rxrpc_s);
785 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700786 pr_crit("Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700787 goto error_key_type_s;
788 }
789
David Howells5873c082014-02-07 18:58:44 +0000790 ret = rxrpc_sysctl_init();
791 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700792 pr_crit("Cannot register sysctls\n");
David Howells5873c082014-02-07 18:58:44 +0000793 goto error_sysctls;
794 }
795
David Howells17926a72007-04-26 15:48:28 -0700796#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +0000797 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
798 proc_create("rxrpc_conns", 0, init_net.proc_net,
799 &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700800#endif
801 return 0;
802
David Howells5873c082014-02-07 18:58:44 +0000803error_sysctls:
804 unregister_key_type(&key_type_rxrpc_s);
David Howells17926a72007-04-26 15:48:28 -0700805error_key_type_s:
806 unregister_key_type(&key_type_rxrpc);
807error_key_type:
808 sock_unregister(PF_RXRPC);
809error_sock:
810 proto_unregister(&rxrpc_proto);
811error_proto:
David Howells651350d2007-04-26 15:50:17 -0700812 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100813error_security:
814 rxrpc_exit_security();
David Howells651350d2007-04-26 15:50:17 -0700815error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700816 kmem_cache_destroy(rxrpc_call_jar);
817error_call_jar:
818 return ret;
819}
820
821/*
822 * unregister the RxRPC protocol
823 */
824static void __exit af_rxrpc_exit(void)
825{
826 _enter("");
David Howells5873c082014-02-07 18:58:44 +0000827 rxrpc_sysctl_exit();
David Howells17926a72007-04-26 15:48:28 -0700828 unregister_key_type(&key_type_rxrpc_s);
829 unregister_key_type(&key_type_rxrpc);
830 sock_unregister(PF_RXRPC);
831 proto_unregister(&rxrpc_proto);
832 rxrpc_destroy_all_calls();
833 rxrpc_destroy_all_connections();
834 rxrpc_destroy_all_transports();
835 rxrpc_destroy_all_peers();
836 rxrpc_destroy_all_locals();
837
838 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
839
840 _debug("flush scheduled work");
David Howells651350d2007-04-26 15:50:17 -0700841 flush_workqueue(rxrpc_workqueue);
Gao fengece31ff2013-02-18 01:34:56 +0000842 remove_proc_entry("rxrpc_conns", init_net.proc_net);
843 remove_proc_entry("rxrpc_calls", init_net.proc_net);
David Howells651350d2007-04-26 15:50:17 -0700844 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100845 rxrpc_exit_security();
David Howells17926a72007-04-26 15:48:28 -0700846 kmem_cache_destroy(rxrpc_call_jar);
847 _leave("");
848}
849
850module_init(af_rxrpc_init);
851module_exit(af_rxrpc_exit);