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