]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/rxrpc/af_rxrpc.c
Merge branch 'ipv6-addrconf-hash-improvements-and-cleanups'
[mirror_ubuntu-eoan-kernel.git] / net / rxrpc / af_rxrpc.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79 14#include <linux/module.h>
ce6654cf 15#include <linux/kernel.h>
17926a79 16#include <linux/net.h>
5a0e3ad6 17#include <linux/slab.h>
17926a79 18#include <linux/skbuff.h>
5f2d9c44 19#include <linux/random.h>
17926a79
DH
20#include <linux/poll.h>
21#include <linux/proc_fs.h>
76181c13 22#include <linux/key-type.h>
457c4cbc 23#include <net/net_namespace.h>
17926a79
DH
24#include <net/sock.h>
25#include <net/af_rxrpc.h>
df844fd4 26#define CREATE_TRACE_POINTS
17926a79
DH
27#include "ar-internal.h"
28
29MODULE_DESCRIPTION("RxRPC network protocol");
30MODULE_AUTHOR("Red Hat, Inc.");
31MODULE_LICENSE("GPL");
32MODULE_ALIAS_NETPROTO(PF_RXRPC);
33
95c96174 34unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
17926a79 35module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
424b00e2 36MODULE_PARM_DESC(debug, "RxRPC debugging mask");
17926a79 37
17926a79
DH
38static struct proto rxrpc_proto;
39static const struct proto_ops rxrpc_rpc_ops;
40
17926a79
DH
41/* current debugging ID */
42atomic_t rxrpc_debug_id;
43
44/* count of skbs currently in use */
71f3ca40 45atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
17926a79 46
651350d1
DH
47struct workqueue_struct *rxrpc_workqueue;
48
17926a79
DH
49static void rxrpc_sock_destructor(struct sock *);
50
51/*
52 * see if an RxRPC socket is currently writable
53 */
54static inline int rxrpc_writable(struct sock *sk)
55{
14afee4b 56 return refcount_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
17926a79
DH
57}
58
59/*
60 * wait for write bufferage to become available
61 */
62static void rxrpc_write_space(struct sock *sk)
63{
64 _enter("%p", sk);
43815482 65 rcu_read_lock();
17926a79 66 if (rxrpc_writable(sk)) {
43815482
ED
67 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
68
1ce0bf50 69 if (skwq_has_sleeper(wq))
43815482 70 wake_up_interruptible(&wq->wait);
8d8ad9d7 71 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
17926a79 72 }
43815482 73 rcu_read_unlock();
17926a79
DH
74}
75
76/*
77 * validate an RxRPC address
78 */
79static int rxrpc_validate_address(struct rxrpc_sock *rx,
80 struct sockaddr_rxrpc *srx,
81 int len)
82{
dad8aff7 83 unsigned int tail;
ab802ee0 84
17926a79
DH
85 if (len < sizeof(struct sockaddr_rxrpc))
86 return -EINVAL;
87
88 if (srx->srx_family != AF_RXRPC)
89 return -EAFNOSUPPORT;
90
91 if (srx->transport_type != SOCK_DGRAM)
92 return -ESOCKTNOSUPPORT;
93
94 len -= offsetof(struct sockaddr_rxrpc, transport);
95 if (srx->transport_len < sizeof(sa_family_t) ||
96 srx->transport_len > len)
97 return -EINVAL;
98
19ffa01c 99 if (srx->transport.family != rx->family)
17926a79
DH
100 return -EAFNOSUPPORT;
101
102 switch (srx->transport.family) {
103 case AF_INET:
4f95dd78
DH
104 if (srx->transport_len < sizeof(struct sockaddr_in))
105 return -EINVAL;
ab802ee0 106 tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
17926a79
DH
107 break;
108
d1912747 109#ifdef CONFIG_AF_RXRPC_IPV6
17926a79 110 case AF_INET6:
75b54cb5
DH
111 if (srx->transport_len < sizeof(struct sockaddr_in6))
112 return -EINVAL;
113 tail = offsetof(struct sockaddr_rxrpc, transport) +
114 sizeof(struct sockaddr_in6);
115 break;
d1912747 116#endif
75b54cb5 117
17926a79
DH
118 default:
119 return -EAFNOSUPPORT;
120 }
121
ab802ee0
DH
122 if (tail < len)
123 memset((void *)srx + tail, 0, len - tail);
75b54cb5 124 _debug("INET: %pISp", &srx->transport);
17926a79
DH
125 return 0;
126}
127
128/*
129 * bind a local address to an RxRPC socket
130 */
131static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
132{
b4f1342f 133 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
17926a79 134 struct rxrpc_local *local;
68d6d1ae 135 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
1e9e5c95 136 u16 service_id = srx->srx_service;
17926a79
DH
137 int ret;
138
139 _enter("%p,%p,%d", rx, saddr, len);
140
141 ret = rxrpc_validate_address(rx, srx, len);
142 if (ret < 0)
143 goto error;
144
145 lock_sock(&rx->sk);
146
28036f44
DH
147 switch (rx->sk.sk_state) {
148 case RXRPC_UNBOUND:
149 rx->srx = *srx;
150 local = rxrpc_lookup_local(sock_net(&rx->sk), &rx->srx);
151 if (IS_ERR(local)) {
152 ret = PTR_ERR(local);
153 goto error_unlock;
154 }
155
156 if (service_id) {
157 write_lock(&local->services_lock);
158 if (rcu_access_pointer(local->service))
159 goto service_in_use;
160 rx->local = local;
161 rcu_assign_pointer(local->service, rx);
162 write_unlock(&local->services_lock);
163
164 rx->sk.sk_state = RXRPC_SERVER_BOUND;
165 } else {
166 rx->local = local;
167 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
168 }
169 break;
17926a79 170
28036f44
DH
171 case RXRPC_SERVER_BOUND:
172 ret = -EINVAL;
173 if (service_id == 0)
174 goto error_unlock;
175 ret = -EADDRINUSE;
176 if (service_id == rx->srx.srx_service)
177 goto error_unlock;
178 ret = -EINVAL;
179 srx->srx_service = rx->srx.srx_service;
180 if (memcmp(srx, &rx->srx, sizeof(*srx)) != 0)
181 goto error_unlock;
182 rx->second_service = service_id;
183 rx->sk.sk_state = RXRPC_SERVER_BOUND2;
184 break;
17926a79 185
28036f44
DH
186 default:
187 ret = -EINVAL;
17926a79
DH
188 goto error_unlock;
189 }
190
17926a79
DH
191 release_sock(&rx->sk);
192 _leave(" = 0");
193 return 0;
194
195service_in_use:
248f219c 196 write_unlock(&local->services_lock);
2341e077
DH
197 rxrpc_put_local(local);
198 ret = -EADDRINUSE;
17926a79
DH
199error_unlock:
200 release_sock(&rx->sk);
201error:
202 _leave(" = %d", ret);
203 return ret;
204}
205
206/*
207 * set the number of pending calls permitted on a listening socket
208 */
209static int rxrpc_listen(struct socket *sock, int backlog)
210{
211 struct sock *sk = sock->sk;
212 struct rxrpc_sock *rx = rxrpc_sk(sk);
00e90712 213 unsigned int max, old;
17926a79
DH
214 int ret;
215
216 _enter("%p,%d", rx, backlog);
217
218 lock_sock(&rx->sk);
219
220 switch (rx->sk.sk_state) {
2341e077 221 case RXRPC_UNBOUND:
17926a79
DH
222 ret = -EADDRNOTAVAIL;
223 break;
17926a79 224 case RXRPC_SERVER_BOUND:
28036f44 225 case RXRPC_SERVER_BOUND2:
17926a79 226 ASSERT(rx->local != NULL);
0e119b41
DH
227 max = READ_ONCE(rxrpc_max_backlog);
228 ret = -EINVAL;
229 if (backlog == INT_MAX)
230 backlog = max;
231 else if (backlog < 0 || backlog > max)
232 break;
00e90712 233 old = sk->sk_max_ack_backlog;
17926a79 234 sk->sk_max_ack_backlog = backlog;
00e90712
DH
235 ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
236 if (ret == 0)
237 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
238 else
239 sk->sk_max_ack_backlog = old;
17926a79 240 break;
210f0353
DH
241 case RXRPC_SERVER_LISTENING:
242 if (backlog == 0) {
243 rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
244 sk->sk_max_ack_backlog = 0;
245 rxrpc_discard_prealloc(rx);
246 ret = 0;
247 break;
248 }
0e119b41
DH
249 default:
250 ret = -EBUSY;
251 break;
17926a79
DH
252 }
253
254 release_sock(&rx->sk);
255 _leave(" = %d", ret);
256 return ret;
257}
258
651350d1
DH
259/**
260 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
261 * @sock: The socket on which to make the call
2341e077 262 * @srx: The address of the peer to contact
651350d1
DH
263 * @key: The security context to use (defaults to socket setting)
264 * @user_call_ID: The ID to use
e754eba6 265 * @tx_total_len: Total length of data to transmit during the call (or -1)
d001648e
DH
266 * @gfp: The allocation constraints
267 * @notify_rx: Where to send notifications instead of socket queue
a68f4a27 268 * @upgrade: Request service upgrade for call
651350d1
DH
269 *
270 * Allow a kernel service to begin a call on the nominated socket. This just
271 * sets up all the internal tracking structures and allocates connection and
272 * call IDs as appropriate. The call to be used is returned.
273 *
274 * The default socket destination address and security may be overridden by
275 * supplying @srx and @key.
276 */
277struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
278 struct sockaddr_rxrpc *srx,
279 struct key *key,
280 unsigned long user_call_ID,
e754eba6 281 s64 tx_total_len,
d001648e 282 gfp_t gfp,
a68f4a27
DH
283 rxrpc_notify_rx_t notify_rx,
284 bool upgrade)
651350d1 285{
19ffa01c 286 struct rxrpc_conn_parameters cp;
651350d1
DH
287 struct rxrpc_call *call;
288 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
f4552c2d 289 int ret;
651350d1
DH
290
291 _enter(",,%x,%lx", key_serial(key), user_call_ID);
292
f4552c2d
DH
293 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
294 if (ret < 0)
295 return ERR_PTR(ret);
296
651350d1
DH
297 lock_sock(&rx->sk);
298
19ffa01c
DH
299 if (!key)
300 key = rx->key;
301 if (key && !key->payload.data[0])
302 key = NULL; /* a no-security key */
303
304 memset(&cp, 0, sizeof(cp));
305 cp.local = rx->local;
306 cp.key = key;
307 cp.security_level = 0;
308 cp.exclusive = false;
a68f4a27 309 cp.upgrade = upgrade;
19ffa01c 310 cp.service_id = srx->srx_service;
e754eba6
DH
311 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, tx_total_len,
312 gfp);
540b1c48 313 /* The socket has been unlocked. */
6cb3ece9 314 if (!IS_ERR(call)) {
d001648e 315 call->notify_rx = notify_rx;
6cb3ece9
DH
316 mutex_unlock(&call->user_mutex);
317 }
19ffa01c 318
651350d1
DH
319 _leave(" = %p", call);
320 return call;
321}
651350d1
DH
322EXPORT_SYMBOL(rxrpc_kernel_begin_call);
323
324/**
325 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
4de48af6 326 * @sock: The socket the call is on
651350d1
DH
327 * @call: The call to end
328 *
329 * Allow a kernel service to end a call it was using. The call must be
330 * complete before this is called (the call should be aborted if necessary).
331 */
4de48af6 332void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
651350d1
DH
333{
334 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
540b1c48
DH
335
336 mutex_lock(&call->user_mutex);
8d94aa38 337 rxrpc_release_call(rxrpc_sk(sock->sk), call);
540b1c48 338 mutex_unlock(&call->user_mutex);
cbd00891 339 rxrpc_put_call(call, rxrpc_call_put_kernel);
651350d1 340}
651350d1
DH
341EXPORT_SYMBOL(rxrpc_kernel_end_call);
342
f4d15fb6
DH
343/**
344 * rxrpc_kernel_check_life - Check to see whether a call is still alive
345 * @sock: The socket the call is on
346 * @call: The call to check
347 *
348 * Allow a kernel service to find out whether a call is still alive - ie. we're
349 * getting ACKs from the server. Returns a number representing the life state
350 * which can be compared to that returned by a previous call.
351 *
352 * If this is a client call, ping ACKs will be sent to the server to find out
353 * whether it's still responsive and whether the call is still alive on the
354 * server.
355 */
356u32 rxrpc_kernel_check_life(struct socket *sock, struct rxrpc_call *call)
357{
358 return call->acks_latest;
359}
360EXPORT_SYMBOL(rxrpc_kernel_check_life);
361
c038a58c
DH
362/**
363 * rxrpc_kernel_check_call - Check a call's state
364 * @sock: The socket the call is on
365 * @call: The call to check
366 * @_compl: Where to store the completion state
367 * @_abort_code: Where to store any abort code
368 *
369 * Allow a kernel service to query the state of a call and find out the manner
370 * of its termination if it has completed. Returns -EINPROGRESS if the call is
371 * still going, 0 if the call finished successfully, -ECONNABORTED if the call
372 * was aborted and an appropriate error if the call failed in some other way.
373 */
374int rxrpc_kernel_check_call(struct socket *sock, struct rxrpc_call *call,
375 enum rxrpc_call_completion *_compl, u32 *_abort_code)
376{
377 if (call->state != RXRPC_CALL_COMPLETE)
378 return -EINPROGRESS;
379 smp_rmb();
380 *_compl = call->completion;
381 *_abort_code = call->abort_code;
382 return call->error;
383}
384EXPORT_SYMBOL(rxrpc_kernel_check_call);
385
386/**
387 * rxrpc_kernel_retry_call - Allow a kernel service to retry a call
388 * @sock: The socket the call is on
389 * @call: The call to retry
390 * @srx: The address of the peer to contact
391 * @key: The security context to use (defaults to socket setting)
392 *
393 * Allow a kernel service to try resending a client call that failed due to a
394 * network error to a new address. The Tx queue is maintained intact, thereby
395 * relieving the need to re-encrypt any request data that has already been
396 * buffered.
397 */
398int rxrpc_kernel_retry_call(struct socket *sock, struct rxrpc_call *call,
399 struct sockaddr_rxrpc *srx, struct key *key)
400{
401 struct rxrpc_conn_parameters cp;
402 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
403 int ret;
404
405 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
406
407 if (!key)
408 key = rx->key;
409 if (key && !key->payload.data[0])
410 key = NULL; /* a no-security key */
411
412 memset(&cp, 0, sizeof(cp));
413 cp.local = rx->local;
414 cp.key = key;
415 cp.security_level = 0;
416 cp.exclusive = false;
417 cp.service_id = srx->srx_service;
418
419 mutex_lock(&call->user_mutex);
420
421 ret = rxrpc_prepare_call_for_retry(rx, call);
422 if (ret == 0)
423 ret = rxrpc_retry_client_call(rx, call, &cp, srx, GFP_KERNEL);
424
425 mutex_unlock(&call->user_mutex);
426 _leave(" = %d", ret);
427 return ret;
428}
429EXPORT_SYMBOL(rxrpc_kernel_retry_call);
430
651350d1 431/**
d001648e 432 * rxrpc_kernel_new_call_notification - Get notifications of new calls
651350d1 433 * @sock: The socket to intercept received messages on
d001648e 434 * @notify_new_call: Function to be called when new calls appear
00e90712 435 * @discard_new_call: Function to discard preallocated calls
651350d1 436 *
d001648e 437 * Allow a kernel service to be given notifications about new calls.
651350d1 438 */
d001648e
DH
439void rxrpc_kernel_new_call_notification(
440 struct socket *sock,
00e90712
DH
441 rxrpc_notify_new_call_t notify_new_call,
442 rxrpc_discard_new_call_t discard_new_call)
651350d1
DH
443{
444 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
445
d001648e 446 rx->notify_new_call = notify_new_call;
00e90712 447 rx->discard_new_call = discard_new_call;
651350d1 448}
d001648e 449EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
651350d1 450
17926a79
DH
451/*
452 * connect an RxRPC socket
453 * - this just targets it at a specific destination; no actual connection
454 * negotiation takes place
455 */
456static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
457 int addr_len, int flags)
458{
2341e077
DH
459 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
460 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
17926a79
DH
461 int ret;
462
463 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
464
465 ret = rxrpc_validate_address(rx, srx, addr_len);
466 if (ret < 0) {
467 _leave(" = %d [bad addr]", ret);
468 return ret;
469 }
470
471 lock_sock(&rx->sk);
472
2341e077
DH
473 ret = -EISCONN;
474 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
475 goto error;
476
17926a79 477 switch (rx->sk.sk_state) {
2341e077
DH
478 case RXRPC_UNBOUND:
479 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
480 case RXRPC_CLIENT_UNBOUND:
17926a79
DH
481 case RXRPC_CLIENT_BOUND:
482 break;
17926a79 483 default:
2341e077
DH
484 ret = -EBUSY;
485 goto error;
17926a79
DH
486 }
487
2341e077
DH
488 rx->connect_srx = *srx;
489 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
490 ret = 0;
17926a79 491
2341e077 492error:
17926a79 493 release_sock(&rx->sk);
2341e077 494 return ret;
17926a79
DH
495}
496
497/*
498 * send a message through an RxRPC socket
499 * - in a client this does a number of things:
500 * - finds/sets up a connection for the security specified (if any)
501 * - initiates a call (ID in control data)
502 * - ends the request phase of a call (if MSG_MORE is not set)
503 * - sends a call data packet
504 * - may send an abort (abort code in control data)
505 */
1b784140 506static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
17926a79 507{
2341e077 508 struct rxrpc_local *local;
17926a79
DH
509 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
510 int ret;
511
512 _enter(",{%d},,%zu", rx->sk.sk_state, len);
513
514 if (m->msg_flags & MSG_OOB)
515 return -EOPNOTSUPP;
516
517 if (m->msg_name) {
518 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
519 if (ret < 0) {
520 _leave(" = %d [bad addr]", ret);
521 return ret;
522 }
523 }
524
17926a79
DH
525 lock_sock(&rx->sk);
526
17926a79 527 switch (rx->sk.sk_state) {
2341e077 528 case RXRPC_UNBOUND:
cd5892c7
DH
529 rx->srx.srx_family = AF_RXRPC;
530 rx->srx.srx_service = 0;
531 rx->srx.transport_type = SOCK_DGRAM;
532 rx->srx.transport.family = rx->family;
533 switch (rx->family) {
534 case AF_INET:
535 rx->srx.transport_len = sizeof(struct sockaddr_in);
536 break;
d1912747 537#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
538 case AF_INET6:
539 rx->srx.transport_len = sizeof(struct sockaddr_in6);
540 break;
d1912747 541#endif
cd5892c7
DH
542 default:
543 ret = -EAFNOSUPPORT;
544 goto error_unlock;
545 }
2baec2c3 546 local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
2341e077
DH
547 if (IS_ERR(local)) {
548 ret = PTR_ERR(local);
549 goto error_unlock;
17926a79 550 }
2341e077
DH
551
552 rx->local = local;
553 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
554 /* Fall through */
555
556 case RXRPC_CLIENT_UNBOUND:
17926a79 557 case RXRPC_CLIENT_BOUND:
2341e077
DH
558 if (!m->msg_name &&
559 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
560 m->msg_name = &rx->connect_srx;
561 m->msg_namelen = sizeof(rx->connect_srx);
17926a79 562 }
2341e077
DH
563 case RXRPC_SERVER_BOUND:
564 case RXRPC_SERVER_LISTENING:
565 ret = rxrpc_do_sendmsg(rx, m, len);
540b1c48
DH
566 /* The socket has been unlocked */
567 goto out;
17926a79 568 default:
2341e077 569 ret = -EINVAL;
540b1c48 570 goto error_unlock;
17926a79
DH
571 }
572
2341e077 573error_unlock:
17926a79 574 release_sock(&rx->sk);
540b1c48 575out:
17926a79
DH
576 _leave(" = %d", ret);
577 return ret;
578}
579
580/*
581 * set RxRPC socket options
582 */
583static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
b7058842 584 char __user *optval, unsigned int optlen)
17926a79
DH
585{
586 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
95c96174 587 unsigned int min_sec_level;
4722974d 588 u16 service_upgrade[2];
17926a79
DH
589 int ret;
590
591 _enter(",%d,%d,,%d", level, optname, optlen);
592
593 lock_sock(&rx->sk);
594 ret = -EOPNOTSUPP;
595
596 if (level == SOL_RXRPC) {
597 switch (optname) {
598 case RXRPC_EXCLUSIVE_CONNECTION:
599 ret = -EINVAL;
600 if (optlen != 0)
601 goto error;
602 ret = -EISCONN;
2341e077 603 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79 604 goto error;
cc8feb8e 605 rx->exclusive = true;
17926a79
DH
606 goto success;
607
608 case RXRPC_SECURITY_KEY:
609 ret = -EINVAL;
610 if (rx->key)
611 goto error;
612 ret = -EISCONN;
2341e077 613 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79
DH
614 goto error;
615 ret = rxrpc_request_key(rx, optval, optlen);
616 goto error;
617
618 case RXRPC_SECURITY_KEYRING:
619 ret = -EINVAL;
620 if (rx->key)
621 goto error;
622 ret = -EISCONN;
2341e077 623 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79
DH
624 goto error;
625 ret = rxrpc_server_keyring(rx, optval, optlen);
626 goto error;
627
628 case RXRPC_MIN_SECURITY_LEVEL:
629 ret = -EINVAL;
95c96174 630 if (optlen != sizeof(unsigned int))
17926a79
DH
631 goto error;
632 ret = -EISCONN;
2341e077 633 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79
DH
634 goto error;
635 ret = get_user(min_sec_level,
95c96174 636 (unsigned int __user *) optval);
17926a79
DH
637 if (ret < 0)
638 goto error;
639 ret = -EINVAL;
640 if (min_sec_level > RXRPC_SECURITY_MAX)
641 goto error;
642 rx->min_sec_level = min_sec_level;
643 goto success;
644
4722974d
DH
645 case RXRPC_UPGRADEABLE_SERVICE:
646 ret = -EINVAL;
647 if (optlen != sizeof(service_upgrade) ||
648 rx->service_upgrade.from != 0)
649 goto error;
650 ret = -EISCONN;
651 if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
652 goto error;
653 ret = -EFAULT;
654 if (copy_from_user(service_upgrade, optval,
655 sizeof(service_upgrade)) != 0)
656 goto error;
657 ret = -EINVAL;
658 if ((service_upgrade[0] != rx->srx.srx_service ||
659 service_upgrade[1] != rx->second_service) &&
660 (service_upgrade[0] != rx->second_service ||
661 service_upgrade[1] != rx->srx.srx_service))
662 goto error;
663 rx->service_upgrade.from = service_upgrade[0];
664 rx->service_upgrade.to = service_upgrade[1];
665 goto success;
666
17926a79
DH
667 default:
668 break;
669 }
670 }
671
672success:
673 ret = 0;
674error:
675 release_sock(&rx->sk);
676 return ret;
677}
678
515559ca
DH
679/*
680 * Get socket options.
681 */
682static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
683 char __user *optval, int __user *_optlen)
684{
685 int optlen;
3ec0efde 686
515559ca
DH
687 if (level != SOL_RXRPC)
688 return -EOPNOTSUPP;
689
690 if (get_user(optlen, _optlen))
691 return -EFAULT;
3ec0efde 692
515559ca
DH
693 switch (optname) {
694 case RXRPC_SUPPORTED_CMSG:
695 if (optlen < sizeof(int))
696 return -ETOOSMALL;
697 if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
698 put_user(sizeof(int), _optlen))
699 return -EFAULT;
700 return 0;
3ec0efde 701
515559ca
DH
702 default:
703 return -EOPNOTSUPP;
704 }
705}
706
17926a79
DH
707/*
708 * permit an RxRPC socket to be polled
709 */
710static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
711 poll_table *wait)
712{
17926a79 713 struct sock *sk = sock->sk;
248f219c
DH
714 struct rxrpc_sock *rx = rxrpc_sk(sk);
715 unsigned int mask;
17926a79 716
aa395145 717 sock_poll_wait(file, sk_sleep(sk), wait);
17926a79
DH
718 mask = 0;
719
720 /* the socket is readable if there are any messages waiting on the Rx
721 * queue */
248f219c 722 if (!list_empty(&rx->recvmsg_q))
17926a79
DH
723 mask |= POLLIN | POLLRDNORM;
724
725 /* the socket is writable if there is space to add new data to the
726 * socket; there is no guarantee that any particular call in progress
727 * on the socket may have space in the Tx ACK window */
728 if (rxrpc_writable(sk))
729 mask |= POLLOUT | POLLWRNORM;
730
731 return mask;
732}
733
734/*
735 * create an RxRPC socket
736 */
3f378b68
EP
737static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
738 int kern)
17926a79
DH
739{
740 struct rxrpc_sock *rx;
741 struct sock *sk;
742
743 _enter("%p,%d", sock, protocol);
744
b4f1342f 745 /* we support transport protocol UDP/UDP6 only */
d1912747
DH
746 if (protocol != PF_INET &&
747 IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
17926a79
DH
748 return -EPROTONOSUPPORT;
749
750 if (sock->type != SOCK_DGRAM)
751 return -ESOCKTNOSUPPORT;
752
753 sock->ops = &rxrpc_rpc_ops;
754 sock->state = SS_UNCONNECTED;
755
11aa9c28 756 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
17926a79
DH
757 if (!sk)
758 return -ENOMEM;
759
760 sock_init_data(sock, sk);
8d94aa38 761 sock_set_flag(sk, SOCK_RCU_FREE);
2341e077 762 sk->sk_state = RXRPC_UNBOUND;
17926a79 763 sk->sk_write_space = rxrpc_write_space;
0e119b41 764 sk->sk_max_ack_backlog = 0;
17926a79
DH
765 sk->sk_destruct = rxrpc_sock_destructor;
766
767 rx = rxrpc_sk(sk);
19ffa01c 768 rx->family = protocol;
17926a79
DH
769 rx->calls = RB_ROOT;
770
248f219c
DH
771 spin_lock_init(&rx->incoming_lock);
772 INIT_LIST_HEAD(&rx->sock_calls);
773 INIT_LIST_HEAD(&rx->to_be_accepted);
774 INIT_LIST_HEAD(&rx->recvmsg_q);
775 rwlock_init(&rx->recvmsg_lock);
17926a79
DH
776 rwlock_init(&rx->call_lock);
777 memset(&rx->srx, 0, sizeof(rx->srx));
778
779 _leave(" = 0 [%p]", rx);
780 return 0;
781}
782
248f219c
DH
783/*
784 * Kill all the calls on a socket and shut it down.
785 */
786static int rxrpc_shutdown(struct socket *sock, int flags)
787{
788 struct sock *sk = sock->sk;
789 struct rxrpc_sock *rx = rxrpc_sk(sk);
790 int ret = 0;
791
792 _enter("%p,%d", sk, flags);
793
794 if (flags != SHUT_RDWR)
795 return -EOPNOTSUPP;
796 if (sk->sk_state == RXRPC_CLOSE)
797 return -ESHUTDOWN;
798
799 lock_sock(sk);
800
801 spin_lock_bh(&sk->sk_receive_queue.lock);
802 if (sk->sk_state < RXRPC_CLOSE) {
803 sk->sk_state = RXRPC_CLOSE;
804 sk->sk_shutdown = SHUTDOWN_MASK;
805 } else {
806 ret = -ESHUTDOWN;
807 }
808 spin_unlock_bh(&sk->sk_receive_queue.lock);
809
810 rxrpc_discard_prealloc(rx);
811
812 release_sock(sk);
813 return ret;
814}
815
17926a79
DH
816/*
817 * RxRPC socket destructor
818 */
819static void rxrpc_sock_destructor(struct sock *sk)
820{
821 _enter("%p", sk);
822
823 rxrpc_purge_queue(&sk->sk_receive_queue);
824
14afee4b 825 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
547b792c
IJ
826 WARN_ON(!sk_unhashed(sk));
827 WARN_ON(sk->sk_socket);
17926a79
DH
828
829 if (!sock_flag(sk, SOCK_DEAD)) {
830 printk("Attempt to release alive rxrpc socket: %p\n", sk);
831 return;
832 }
833}
834
835/*
836 * release an RxRPC socket
837 */
838static int rxrpc_release_sock(struct sock *sk)
839{
840 struct rxrpc_sock *rx = rxrpc_sk(sk);
841
41c6d650 842 _enter("%p{%d,%d}", sk, sk->sk_state, refcount_read(&sk->sk_refcnt));
17926a79
DH
843
844 /* declare the socket closed for business */
845 sock_orphan(sk);
846 sk->sk_shutdown = SHUTDOWN_MASK;
847
848 spin_lock_bh(&sk->sk_receive_queue.lock);
849 sk->sk_state = RXRPC_CLOSE;
850 spin_unlock_bh(&sk->sk_receive_queue.lock);
851
b63452c1 852 if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
248f219c 853 write_lock(&rx->local->services_lock);
b63452c1 854 rcu_assign_pointer(rx->local->service, NULL);
248f219c 855 write_unlock(&rx->local->services_lock);
17926a79
DH
856 }
857
858 /* try to flush out this socket */
00e90712 859 rxrpc_discard_prealloc(rx);
17926a79 860 rxrpc_release_calls_on_socket(rx);
651350d1 861 flush_workqueue(rxrpc_workqueue);
17926a79
DH
862 rxrpc_purge_queue(&sk->sk_receive_queue);
863
5627cc8b
DH
864 rxrpc_put_local(rx->local);
865 rx->local = NULL;
17926a79
DH
866 key_put(rx->key);
867 rx->key = NULL;
868 key_put(rx->securities);
869 rx->securities = NULL;
870 sock_put(sk);
871
872 _leave(" = 0");
873 return 0;
874}
875
876/*
877 * release an RxRPC BSD socket on close() or equivalent
878 */
879static int rxrpc_release(struct socket *sock)
880{
881 struct sock *sk = sock->sk;
882
883 _enter("%p{%p}", sock, sk);
884
885 if (!sk)
886 return 0;
887
888 sock->sk = NULL;
889
890 return rxrpc_release_sock(sk);
891}
892
893/*
894 * RxRPC network protocol
895 */
896static const struct proto_ops rxrpc_rpc_ops = {
e33b3d97 897 .family = PF_RXRPC,
17926a79
DH
898 .owner = THIS_MODULE,
899 .release = rxrpc_release,
900 .bind = rxrpc_bind,
901 .connect = rxrpc_connect,
902 .socketpair = sock_no_socketpair,
903 .accept = sock_no_accept,
904 .getname = sock_no_getname,
905 .poll = rxrpc_poll,
906 .ioctl = sock_no_ioctl,
907 .listen = rxrpc_listen,
248f219c 908 .shutdown = rxrpc_shutdown,
17926a79 909 .setsockopt = rxrpc_setsockopt,
515559ca 910 .getsockopt = rxrpc_getsockopt,
17926a79
DH
911 .sendmsg = rxrpc_sendmsg,
912 .recvmsg = rxrpc_recvmsg,
913 .mmap = sock_no_mmap,
914 .sendpage = sock_no_sendpage,
915};
916
917static struct proto rxrpc_proto = {
918 .name = "RXRPC",
919 .owner = THIS_MODULE,
920 .obj_size = sizeof(struct rxrpc_sock),
0d12f8a4 921 .max_header = sizeof(struct rxrpc_wire_header),
17926a79
DH
922};
923
ec1b4cf7 924static const struct net_proto_family rxrpc_family_ops = {
17926a79
DH
925 .family = PF_RXRPC,
926 .create = rxrpc_create,
927 .owner = THIS_MODULE,
928};
929
930/*
931 * initialise and register the RxRPC protocol
932 */
933static int __init af_rxrpc_init(void)
934{
17926a79 935 int ret = -1;
44430612 936 unsigned int tmp;
17926a79 937
ce6654cf 938 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
17926a79 939
44430612
MW
940 get_random_bytes(&tmp, sizeof(tmp));
941 tmp &= 0x3fffffff;
942 if (tmp == 0)
943 tmp = 1;
944 idr_set_cursor(&rxrpc_client_conn_ids, tmp);
17926a79 945
651350d1 946 ret = -ENOMEM;
17926a79
DH
947 rxrpc_call_jar = kmem_cache_create(
948 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
20c2df83 949 SLAB_HWCACHE_ALIGN, NULL);
17926a79 950 if (!rxrpc_call_jar) {
9b6d5398 951 pr_notice("Failed to allocate call jar\n");
17926a79
DH
952 goto error_call_jar;
953 }
954
e1fcc7e2 955 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
651350d1 956 if (!rxrpc_workqueue) {
9b6d5398 957 pr_notice("Failed to allocate work queue\n");
651350d1
DH
958 goto error_work_queue;
959 }
960
648af7fc
DH
961 ret = rxrpc_init_security();
962 if (ret < 0) {
9b6d5398 963 pr_crit("Cannot initialise security\n");
648af7fc
DH
964 goto error_security;
965 }
966
2baec2c3
DH
967 ret = register_pernet_subsys(&rxrpc_net_ops);
968 if (ret)
969 goto error_pernet;
970
17926a79 971 ret = proto_register(&rxrpc_proto, 1);
1c899641 972 if (ret < 0) {
9b6d5398 973 pr_crit("Cannot register protocol\n");
17926a79
DH
974 goto error_proto;
975 }
976
977 ret = sock_register(&rxrpc_family_ops);
978 if (ret < 0) {
9b6d5398 979 pr_crit("Cannot register socket family\n");
17926a79
DH
980 goto error_sock;
981 }
982
983 ret = register_key_type(&key_type_rxrpc);
984 if (ret < 0) {
9b6d5398 985 pr_crit("Cannot register client key type\n");
17926a79
DH
986 goto error_key_type;
987 }
988
989 ret = register_key_type(&key_type_rxrpc_s);
990 if (ret < 0) {
9b6d5398 991 pr_crit("Cannot register server key type\n");
17926a79
DH
992 goto error_key_type_s;
993 }
994
5873c083
DH
995 ret = rxrpc_sysctl_init();
996 if (ret < 0) {
9b6d5398 997 pr_crit("Cannot register sysctls\n");
5873c083
DH
998 goto error_sysctls;
999 }
1000
17926a79
DH
1001 return 0;
1002
5873c083
DH
1003error_sysctls:
1004 unregister_key_type(&key_type_rxrpc_s);
17926a79
DH
1005error_key_type_s:
1006 unregister_key_type(&key_type_rxrpc);
1007error_key_type:
1008 sock_unregister(PF_RXRPC);
1009error_sock:
1010 proto_unregister(&rxrpc_proto);
1011error_proto:
2baec2c3
DH
1012 unregister_pernet_subsys(&rxrpc_net_ops);
1013error_pernet:
648af7fc 1014 rxrpc_exit_security();
8addc044
WY
1015error_security:
1016 destroy_workqueue(rxrpc_workqueue);
651350d1 1017error_work_queue:
17926a79
DH
1018 kmem_cache_destroy(rxrpc_call_jar);
1019error_call_jar:
1020 return ret;
1021}
1022
1023/*
1024 * unregister the RxRPC protocol
1025 */
1026static void __exit af_rxrpc_exit(void)
1027{
1028 _enter("");
5873c083 1029 rxrpc_sysctl_exit();
17926a79
DH
1030 unregister_key_type(&key_type_rxrpc_s);
1031 unregister_key_type(&key_type_rxrpc);
1032 sock_unregister(PF_RXRPC);
1033 proto_unregister(&rxrpc_proto);
2baec2c3 1034 unregister_pernet_subsys(&rxrpc_net_ops);
71f3ca40
DH
1035 ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
1036 ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
4f95dd78 1037
2baec2c3
DH
1038 /* Make sure the local and peer records pinned by any dying connections
1039 * are released.
1040 */
1041 rcu_barrier();
1042 rxrpc_destroy_client_conn_ids();
1043
651350d1 1044 destroy_workqueue(rxrpc_workqueue);
648af7fc 1045 rxrpc_exit_security();
17926a79
DH
1046 kmem_cache_destroy(rxrpc_call_jar);
1047 _leave("");
1048}
1049
1050module_init(af_rxrpc_init);
1051module_exit(af_rxrpc_exit);