]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/rxrpc/af_rxrpc.c
rxrpc: Rewrite the data and ack handling code
[mirror_ubuntu-artful-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
41/* local epoch for detecting local-end reset */
0d12f8a4 42u32 rxrpc_epoch;
17926a79
DH
43
44/* current debugging ID */
45atomic_t rxrpc_debug_id;
46
47/* count of skbs currently in use */
48atomic_t rxrpc_n_skbs;
49
651350d1
DH
50struct workqueue_struct *rxrpc_workqueue;
51
17926a79
DH
52static 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);
43815482 68 rcu_read_lock();
17926a79 69 if (rxrpc_writable(sk)) {
43815482
ED
70 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
71
1ce0bf50 72 if (skwq_has_sleeper(wq))
43815482 73 wake_up_interruptible(&wq->wait);
8d8ad9d7 74 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
17926a79 75 }
43815482 76 rcu_read_unlock();
17926a79
DH
77}
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{
dad8aff7 86 unsigned int tail;
ab802ee0 87
17926a79
DH
88 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
19ffa01c 102 if (srx->transport.family != rx->family)
17926a79
DH
103 return -EAFNOSUPPORT;
104
105 switch (srx->transport.family) {
106 case AF_INET:
4f95dd78
DH
107 if (srx->transport_len < sizeof(struct sockaddr_in))
108 return -EINVAL;
21454aaa 109 _debug("INET: %x @ %pI4",
17926a79 110 ntohs(srx->transport.sin.sin_port),
21454aaa 111 &srx->transport.sin.sin_addr);
ab802ee0 112 tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
17926a79
DH
113 break;
114
115 case AF_INET6:
116 default:
117 return -EAFNOSUPPORT;
118 }
119
ab802ee0
DH
120 if (tail < len)
121 memset((void *)srx + tail, 0, len - tail);
17926a79
DH
122 return 0;
123}
124
125/*
126 * bind a local address to an RxRPC socket
127 */
128static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
129{
b4f1342f 130 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
17926a79
DH
131 struct sock *sk = sock->sk;
132 struct rxrpc_local *local;
133 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
17926a79
DH
134 int ret;
135
136 _enter("%p,%p,%d", rx, saddr, len);
137
138 ret = rxrpc_validate_address(rx, srx, len);
139 if (ret < 0)
140 goto error;
141
142 lock_sock(&rx->sk);
143
2341e077 144 if (rx->sk.sk_state != RXRPC_UNBOUND) {
17926a79
DH
145 ret = -EINVAL;
146 goto error_unlock;
147 }
148
149 memcpy(&rx->srx, srx, sizeof(rx->srx));
150
17926a79
DH
151 local = rxrpc_lookup_local(&rx->srx);
152 if (IS_ERR(local)) {
153 ret = PTR_ERR(local);
154 goto error_unlock;
155 }
156
2341e077 157 if (rx->srx.srx_service) {
248f219c 158 write_lock(&local->services_lock);
de8d6c74 159 hlist_for_each_entry(prx, &local->services, listen_link) {
2341e077 160 if (prx->srx.srx_service == rx->srx.srx_service)
17926a79
DH
161 goto service_in_use;
162 }
163
2341e077 164 rx->local = local;
de8d6c74 165 hlist_add_head_rcu(&rx->listen_link, &local->services);
248f219c 166 write_unlock(&local->services_lock);
17926a79
DH
167
168 rx->sk.sk_state = RXRPC_SERVER_BOUND;
169 } else {
2341e077 170 rx->local = local;
17926a79
DH
171 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
172 }
173
174 release_sock(&rx->sk);
175 _leave(" = 0");
176 return 0;
177
178service_in_use:
248f219c 179 write_unlock(&local->services_lock);
2341e077
DH
180 rxrpc_put_local(local);
181 ret = -EADDRINUSE;
17926a79
DH
182error_unlock:
183 release_sock(&rx->sk);
184error:
185 _leave(" = %d", ret);
186 return ret;
187}
188
189/*
190 * set the number of pending calls permitted on a listening socket
191 */
192static int rxrpc_listen(struct socket *sock, int backlog)
193{
194 struct sock *sk = sock->sk;
195 struct rxrpc_sock *rx = rxrpc_sk(sk);
00e90712 196 unsigned int max, old;
17926a79
DH
197 int ret;
198
199 _enter("%p,%d", rx, backlog);
200
201 lock_sock(&rx->sk);
202
203 switch (rx->sk.sk_state) {
2341e077 204 case RXRPC_UNBOUND:
17926a79
DH
205 ret = -EADDRNOTAVAIL;
206 break;
17926a79
DH
207 case RXRPC_SERVER_BOUND:
208 ASSERT(rx->local != NULL);
0e119b41
DH
209 max = READ_ONCE(rxrpc_max_backlog);
210 ret = -EINVAL;
211 if (backlog == INT_MAX)
212 backlog = max;
213 else if (backlog < 0 || backlog > max)
214 break;
00e90712 215 old = sk->sk_max_ack_backlog;
17926a79 216 sk->sk_max_ack_backlog = backlog;
00e90712
DH
217 ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
218 if (ret == 0)
219 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
220 else
221 sk->sk_max_ack_backlog = old;
17926a79 222 break;
0e119b41
DH
223 default:
224 ret = -EBUSY;
225 break;
17926a79
DH
226 }
227
228 release_sock(&rx->sk);
229 _leave(" = %d", ret);
230 return ret;
231}
232
651350d1
DH
233/**
234 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
235 * @sock: The socket on which to make the call
2341e077 236 * @srx: The address of the peer to contact
651350d1
DH
237 * @key: The security context to use (defaults to socket setting)
238 * @user_call_ID: The ID to use
d001648e
DH
239 * @gfp: The allocation constraints
240 * @notify_rx: Where to send notifications instead of socket queue
651350d1
DH
241 *
242 * Allow a kernel service to begin a call on the nominated socket. This just
243 * sets up all the internal tracking structures and allocates connection and
244 * call IDs as appropriate. The call to be used is returned.
245 *
246 * The default socket destination address and security may be overridden by
247 * supplying @srx and @key.
248 */
249struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
250 struct sockaddr_rxrpc *srx,
251 struct key *key,
252 unsigned long user_call_ID,
d001648e
DH
253 gfp_t gfp,
254 rxrpc_notify_rx_t notify_rx)
651350d1 255{
19ffa01c 256 struct rxrpc_conn_parameters cp;
651350d1
DH
257 struct rxrpc_call *call;
258 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
f4552c2d 259 int ret;
651350d1
DH
260
261 _enter(",,%x,%lx", key_serial(key), user_call_ID);
262
f4552c2d
DH
263 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
264 if (ret < 0)
265 return ERR_PTR(ret);
266
651350d1
DH
267 lock_sock(&rx->sk);
268
19ffa01c
DH
269 if (!key)
270 key = rx->key;
271 if (key && !key->payload.data[0])
272 key = NULL; /* a no-security key */
273
274 memset(&cp, 0, sizeof(cp));
275 cp.local = rx->local;
276 cp.key = key;
277 cp.security_level = 0;
278 cp.exclusive = false;
279 cp.service_id = srx->srx_service;
aa390bbe 280 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp);
d001648e
DH
281 if (!IS_ERR(call))
282 call->notify_rx = notify_rx;
19ffa01c 283
651350d1
DH
284 release_sock(&rx->sk);
285 _leave(" = %p", call);
286 return call;
287}
651350d1
DH
288EXPORT_SYMBOL(rxrpc_kernel_begin_call);
289
290/**
291 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
4de48af6 292 * @sock: The socket the call is on
651350d1
DH
293 * @call: The call to end
294 *
295 * Allow a kernel service to end a call it was using. The call must be
296 * complete before this is called (the call should be aborted if necessary).
297 */
4de48af6 298void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
651350d1
DH
299{
300 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
8d94aa38 301 rxrpc_release_call(rxrpc_sk(sock->sk), call);
fff72429 302 rxrpc_put_call(call, rxrpc_call_put);
651350d1 303}
651350d1
DH
304EXPORT_SYMBOL(rxrpc_kernel_end_call);
305
306/**
d001648e 307 * rxrpc_kernel_new_call_notification - Get notifications of new calls
651350d1 308 * @sock: The socket to intercept received messages on
d001648e 309 * @notify_new_call: Function to be called when new calls appear
00e90712 310 * @discard_new_call: Function to discard preallocated calls
651350d1 311 *
d001648e 312 * Allow a kernel service to be given notifications about new calls.
651350d1 313 */
d001648e
DH
314void rxrpc_kernel_new_call_notification(
315 struct socket *sock,
00e90712
DH
316 rxrpc_notify_new_call_t notify_new_call,
317 rxrpc_discard_new_call_t discard_new_call)
651350d1
DH
318{
319 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
320
d001648e 321 rx->notify_new_call = notify_new_call;
00e90712 322 rx->discard_new_call = discard_new_call;
651350d1 323}
d001648e 324EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
651350d1 325
17926a79
DH
326/*
327 * connect an RxRPC socket
328 * - this just targets it at a specific destination; no actual connection
329 * negotiation takes place
330 */
331static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
332 int addr_len, int flags)
333{
2341e077
DH
334 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
335 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
17926a79
DH
336 int ret;
337
338 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
339
340 ret = rxrpc_validate_address(rx, srx, addr_len);
341 if (ret < 0) {
342 _leave(" = %d [bad addr]", ret);
343 return ret;
344 }
345
346 lock_sock(&rx->sk);
347
2341e077
DH
348 ret = -EISCONN;
349 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
350 goto error;
351
17926a79 352 switch (rx->sk.sk_state) {
2341e077
DH
353 case RXRPC_UNBOUND:
354 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
355 case RXRPC_CLIENT_UNBOUND:
17926a79
DH
356 case RXRPC_CLIENT_BOUND:
357 break;
17926a79 358 default:
2341e077
DH
359 ret = -EBUSY;
360 goto error;
17926a79
DH
361 }
362
2341e077
DH
363 rx->connect_srx = *srx;
364 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
365 ret = 0;
17926a79 366
2341e077 367error:
17926a79 368 release_sock(&rx->sk);
2341e077 369 return ret;
17926a79
DH
370}
371
372/*
373 * send a message through an RxRPC socket
374 * - in a client this does a number of things:
375 * - finds/sets up a connection for the security specified (if any)
376 * - initiates a call (ID in control data)
377 * - ends the request phase of a call (if MSG_MORE is not set)
378 * - sends a call data packet
379 * - may send an abort (abort code in control data)
380 */
1b784140 381static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
17926a79 382{
2341e077 383 struct rxrpc_local *local;
17926a79
DH
384 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
385 int ret;
386
387 _enter(",{%d},,%zu", rx->sk.sk_state, len);
388
389 if (m->msg_flags & MSG_OOB)
390 return -EOPNOTSUPP;
391
392 if (m->msg_name) {
393 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
394 if (ret < 0) {
395 _leave(" = %d [bad addr]", ret);
396 return ret;
397 }
398 }
399
17926a79
DH
400 lock_sock(&rx->sk);
401
17926a79 402 switch (rx->sk.sk_state) {
2341e077
DH
403 case RXRPC_UNBOUND:
404 local = rxrpc_lookup_local(&rx->srx);
405 if (IS_ERR(local)) {
406 ret = PTR_ERR(local);
407 goto error_unlock;
17926a79 408 }
2341e077
DH
409
410 rx->local = local;
411 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
412 /* Fall through */
413
414 case RXRPC_CLIENT_UNBOUND:
17926a79 415 case RXRPC_CLIENT_BOUND:
2341e077
DH
416 if (!m->msg_name &&
417 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
418 m->msg_name = &rx->connect_srx;
419 m->msg_namelen = sizeof(rx->connect_srx);
17926a79 420 }
2341e077
DH
421 case RXRPC_SERVER_BOUND:
422 case RXRPC_SERVER_LISTENING:
423 ret = rxrpc_do_sendmsg(rx, m, len);
17926a79
DH
424 break;
425 default:
2341e077 426 ret = -EINVAL;
17926a79
DH
427 break;
428 }
429
2341e077 430error_unlock:
17926a79 431 release_sock(&rx->sk);
17926a79
DH
432 _leave(" = %d", ret);
433 return ret;
434}
435
436/*
437 * set RxRPC socket options
438 */
439static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
b7058842 440 char __user *optval, unsigned int optlen)
17926a79
DH
441{
442 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
95c96174 443 unsigned int min_sec_level;
17926a79
DH
444 int ret;
445
446 _enter(",%d,%d,,%d", level, optname, optlen);
447
448 lock_sock(&rx->sk);
449 ret = -EOPNOTSUPP;
450
451 if (level == SOL_RXRPC) {
452 switch (optname) {
453 case RXRPC_EXCLUSIVE_CONNECTION:
454 ret = -EINVAL;
455 if (optlen != 0)
456 goto error;
457 ret = -EISCONN;
2341e077 458 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79 459 goto error;
cc8feb8e 460 rx->exclusive = true;
17926a79
DH
461 goto success;
462
463 case RXRPC_SECURITY_KEY:
464 ret = -EINVAL;
465 if (rx->key)
466 goto error;
467 ret = -EISCONN;
2341e077 468 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79
DH
469 goto error;
470 ret = rxrpc_request_key(rx, optval, optlen);
471 goto error;
472
473 case RXRPC_SECURITY_KEYRING:
474 ret = -EINVAL;
475 if (rx->key)
476 goto error;
477 ret = -EISCONN;
2341e077 478 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79
DH
479 goto error;
480 ret = rxrpc_server_keyring(rx, optval, optlen);
481 goto error;
482
483 case RXRPC_MIN_SECURITY_LEVEL:
484 ret = -EINVAL;
95c96174 485 if (optlen != sizeof(unsigned int))
17926a79
DH
486 goto error;
487 ret = -EISCONN;
2341e077 488 if (rx->sk.sk_state != RXRPC_UNBOUND)
17926a79
DH
489 goto error;
490 ret = get_user(min_sec_level,
95c96174 491 (unsigned int __user *) optval);
17926a79
DH
492 if (ret < 0)
493 goto error;
494 ret = -EINVAL;
495 if (min_sec_level > RXRPC_SECURITY_MAX)
496 goto error;
497 rx->min_sec_level = min_sec_level;
498 goto success;
499
500 default:
501 break;
502 }
503 }
504
505success:
506 ret = 0;
507error:
508 release_sock(&rx->sk);
509 return ret;
510}
511
512/*
513 * permit an RxRPC socket to be polled
514 */
515static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
516 poll_table *wait)
517{
17926a79 518 struct sock *sk = sock->sk;
248f219c
DH
519 struct rxrpc_sock *rx = rxrpc_sk(sk);
520 unsigned int mask;
17926a79 521
aa395145 522 sock_poll_wait(file, sk_sleep(sk), wait);
17926a79
DH
523 mask = 0;
524
525 /* the socket is readable if there are any messages waiting on the Rx
526 * queue */
248f219c 527 if (!list_empty(&rx->recvmsg_q))
17926a79
DH
528 mask |= POLLIN | POLLRDNORM;
529
530 /* the socket is writable if there is space to add new data to the
531 * socket; there is no guarantee that any particular call in progress
532 * on the socket may have space in the Tx ACK window */
533 if (rxrpc_writable(sk))
534 mask |= POLLOUT | POLLWRNORM;
535
536 return mask;
537}
538
539/*
540 * create an RxRPC socket
541 */
3f378b68
EP
542static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
543 int kern)
17926a79
DH
544{
545 struct rxrpc_sock *rx;
546 struct sock *sk;
547
548 _enter("%p,%d", sock, protocol);
549
09ad9bc7 550 if (!net_eq(net, &init_net))
1b8d7ae4
EB
551 return -EAFNOSUPPORT;
552
b4f1342f 553 /* we support transport protocol UDP/UDP6 only */
17926a79
DH
554 if (protocol != PF_INET)
555 return -EPROTONOSUPPORT;
556
557 if (sock->type != SOCK_DGRAM)
558 return -ESOCKTNOSUPPORT;
559
560 sock->ops = &rxrpc_rpc_ops;
561 sock->state = SS_UNCONNECTED;
562
11aa9c28 563 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
17926a79
DH
564 if (!sk)
565 return -ENOMEM;
566
567 sock_init_data(sock, sk);
8d94aa38 568 sock_set_flag(sk, SOCK_RCU_FREE);
2341e077 569 sk->sk_state = RXRPC_UNBOUND;
17926a79 570 sk->sk_write_space = rxrpc_write_space;
0e119b41 571 sk->sk_max_ack_backlog = 0;
17926a79
DH
572 sk->sk_destruct = rxrpc_sock_destructor;
573
574 rx = rxrpc_sk(sk);
19ffa01c 575 rx->family = protocol;
17926a79
DH
576 rx->calls = RB_ROOT;
577
de8d6c74 578 INIT_HLIST_NODE(&rx->listen_link);
248f219c
DH
579 spin_lock_init(&rx->incoming_lock);
580 INIT_LIST_HEAD(&rx->sock_calls);
581 INIT_LIST_HEAD(&rx->to_be_accepted);
582 INIT_LIST_HEAD(&rx->recvmsg_q);
583 rwlock_init(&rx->recvmsg_lock);
17926a79
DH
584 rwlock_init(&rx->call_lock);
585 memset(&rx->srx, 0, sizeof(rx->srx));
586
587 _leave(" = 0 [%p]", rx);
588 return 0;
589}
590
248f219c
DH
591/*
592 * Kill all the calls on a socket and shut it down.
593 */
594static int rxrpc_shutdown(struct socket *sock, int flags)
595{
596 struct sock *sk = sock->sk;
597 struct rxrpc_sock *rx = rxrpc_sk(sk);
598 int ret = 0;
599
600 _enter("%p,%d", sk, flags);
601
602 if (flags != SHUT_RDWR)
603 return -EOPNOTSUPP;
604 if (sk->sk_state == RXRPC_CLOSE)
605 return -ESHUTDOWN;
606
607 lock_sock(sk);
608
609 spin_lock_bh(&sk->sk_receive_queue.lock);
610 if (sk->sk_state < RXRPC_CLOSE) {
611 sk->sk_state = RXRPC_CLOSE;
612 sk->sk_shutdown = SHUTDOWN_MASK;
613 } else {
614 ret = -ESHUTDOWN;
615 }
616 spin_unlock_bh(&sk->sk_receive_queue.lock);
617
618 rxrpc_discard_prealloc(rx);
619
620 release_sock(sk);
621 return ret;
622}
623
17926a79
DH
624/*
625 * RxRPC socket destructor
626 */
627static void rxrpc_sock_destructor(struct sock *sk)
628{
629 _enter("%p", sk);
630
631 rxrpc_purge_queue(&sk->sk_receive_queue);
632
547b792c
IJ
633 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
634 WARN_ON(!sk_unhashed(sk));
635 WARN_ON(sk->sk_socket);
17926a79
DH
636
637 if (!sock_flag(sk, SOCK_DEAD)) {
638 printk("Attempt to release alive rxrpc socket: %p\n", sk);
639 return;
640 }
641}
642
643/*
644 * release an RxRPC socket
645 */
646static int rxrpc_release_sock(struct sock *sk)
647{
648 struct rxrpc_sock *rx = rxrpc_sk(sk);
649
650 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
651
652 /* declare the socket closed for business */
653 sock_orphan(sk);
654 sk->sk_shutdown = SHUTDOWN_MASK;
655
656 spin_lock_bh(&sk->sk_receive_queue.lock);
657 sk->sk_state = RXRPC_CLOSE;
658 spin_unlock_bh(&sk->sk_receive_queue.lock);
659
660 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
661
de8d6c74 662 if (!hlist_unhashed(&rx->listen_link)) {
248f219c 663 write_lock(&rx->local->services_lock);
de8d6c74 664 hlist_del_rcu(&rx->listen_link);
248f219c 665 write_unlock(&rx->local->services_lock);
17926a79
DH
666 }
667
668 /* try to flush out this socket */
00e90712 669 rxrpc_discard_prealloc(rx);
17926a79 670 rxrpc_release_calls_on_socket(rx);
651350d1 671 flush_workqueue(rxrpc_workqueue);
17926a79
DH
672 rxrpc_purge_queue(&sk->sk_receive_queue);
673
5627cc8b
DH
674 rxrpc_put_local(rx->local);
675 rx->local = NULL;
17926a79
DH
676 key_put(rx->key);
677 rx->key = NULL;
678 key_put(rx->securities);
679 rx->securities = NULL;
680 sock_put(sk);
681
682 _leave(" = 0");
683 return 0;
684}
685
686/*
687 * release an RxRPC BSD socket on close() or equivalent
688 */
689static int rxrpc_release(struct socket *sock)
690{
691 struct sock *sk = sock->sk;
692
693 _enter("%p{%p}", sock, sk);
694
695 if (!sk)
696 return 0;
697
698 sock->sk = NULL;
699
700 return rxrpc_release_sock(sk);
701}
702
703/*
704 * RxRPC network protocol
705 */
706static const struct proto_ops rxrpc_rpc_ops = {
e33b3d97 707 .family = PF_RXRPC,
17926a79
DH
708 .owner = THIS_MODULE,
709 .release = rxrpc_release,
710 .bind = rxrpc_bind,
711 .connect = rxrpc_connect,
712 .socketpair = sock_no_socketpair,
713 .accept = sock_no_accept,
714 .getname = sock_no_getname,
715 .poll = rxrpc_poll,
716 .ioctl = sock_no_ioctl,
717 .listen = rxrpc_listen,
248f219c 718 .shutdown = rxrpc_shutdown,
17926a79
DH
719 .setsockopt = rxrpc_setsockopt,
720 .getsockopt = sock_no_getsockopt,
721 .sendmsg = rxrpc_sendmsg,
722 .recvmsg = rxrpc_recvmsg,
723 .mmap = sock_no_mmap,
724 .sendpage = sock_no_sendpage,
725};
726
727static struct proto rxrpc_proto = {
728 .name = "RXRPC",
729 .owner = THIS_MODULE,
730 .obj_size = sizeof(struct rxrpc_sock),
0d12f8a4 731 .max_header = sizeof(struct rxrpc_wire_header),
17926a79
DH
732};
733
ec1b4cf7 734static const struct net_proto_family rxrpc_family_ops = {
17926a79
DH
735 .family = PF_RXRPC,
736 .create = rxrpc_create,
737 .owner = THIS_MODULE,
738};
739
740/*
741 * initialise and register the RxRPC protocol
742 */
743static int __init af_rxrpc_init(void)
744{
17926a79
DH
745 int ret = -1;
746
ce6654cf 747 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
17926a79 748
5f2d9c44
DH
749 get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch));
750 rxrpc_epoch |= RXRPC_RANDOM_EPOCH;
751 get_random_bytes(&rxrpc_client_conn_ids.cur,
752 sizeof(rxrpc_client_conn_ids.cur));
753 rxrpc_client_conn_ids.cur &= 0x3fffffff;
754 if (rxrpc_client_conn_ids.cur == 0)
755 rxrpc_client_conn_ids.cur = 1;
17926a79 756
651350d1 757 ret = -ENOMEM;
17926a79
DH
758 rxrpc_call_jar = kmem_cache_create(
759 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
20c2df83 760 SLAB_HWCACHE_ALIGN, NULL);
17926a79 761 if (!rxrpc_call_jar) {
9b6d5398 762 pr_notice("Failed to allocate call jar\n");
17926a79
DH
763 goto error_call_jar;
764 }
765
e1fcc7e2 766 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
651350d1 767 if (!rxrpc_workqueue) {
9b6d5398 768 pr_notice("Failed to allocate work queue\n");
651350d1
DH
769 goto error_work_queue;
770 }
771
648af7fc
DH
772 ret = rxrpc_init_security();
773 if (ret < 0) {
9b6d5398 774 pr_crit("Cannot initialise security\n");
648af7fc
DH
775 goto error_security;
776 }
777
17926a79 778 ret = proto_register(&rxrpc_proto, 1);
1c899641 779 if (ret < 0) {
9b6d5398 780 pr_crit("Cannot register protocol\n");
17926a79
DH
781 goto error_proto;
782 }
783
784 ret = sock_register(&rxrpc_family_ops);
785 if (ret < 0) {
9b6d5398 786 pr_crit("Cannot register socket family\n");
17926a79
DH
787 goto error_sock;
788 }
789
790 ret = register_key_type(&key_type_rxrpc);
791 if (ret < 0) {
9b6d5398 792 pr_crit("Cannot register client key type\n");
17926a79
DH
793 goto error_key_type;
794 }
795
796 ret = register_key_type(&key_type_rxrpc_s);
797 if (ret < 0) {
9b6d5398 798 pr_crit("Cannot register server key type\n");
17926a79
DH
799 goto error_key_type_s;
800 }
801
5873c083
DH
802 ret = rxrpc_sysctl_init();
803 if (ret < 0) {
9b6d5398 804 pr_crit("Cannot register sysctls\n");
5873c083
DH
805 goto error_sysctls;
806 }
807
17926a79 808#ifdef CONFIG_PROC_FS
d4beaa66
G
809 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
810 proc_create("rxrpc_conns", 0, init_net.proc_net,
811 &rxrpc_connection_seq_fops);
17926a79
DH
812#endif
813 return 0;
814
5873c083
DH
815error_sysctls:
816 unregister_key_type(&key_type_rxrpc_s);
17926a79
DH
817error_key_type_s:
818 unregister_key_type(&key_type_rxrpc);
819error_key_type:
820 sock_unregister(PF_RXRPC);
821error_sock:
822 proto_unregister(&rxrpc_proto);
823error_proto:
648af7fc 824 rxrpc_exit_security();
8addc044
WY
825error_security:
826 destroy_workqueue(rxrpc_workqueue);
651350d1 827error_work_queue:
17926a79
DH
828 kmem_cache_destroy(rxrpc_call_jar);
829error_call_jar:
830 return ret;
831}
832
833/*
834 * unregister the RxRPC protocol
835 */
836static void __exit af_rxrpc_exit(void)
837{
838 _enter("");
5873c083 839 rxrpc_sysctl_exit();
17926a79
DH
840 unregister_key_type(&key_type_rxrpc_s);
841 unregister_key_type(&key_type_rxrpc);
842 sock_unregister(PF_RXRPC);
843 proto_unregister(&rxrpc_proto);
844 rxrpc_destroy_all_calls();
845 rxrpc_destroy_all_connections();
17926a79 846 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
4f95dd78
DH
847 rxrpc_destroy_all_locals();
848
ece31ffd
G
849 remove_proc_entry("rxrpc_conns", init_net.proc_net);
850 remove_proc_entry("rxrpc_calls", init_net.proc_net);
651350d1 851 destroy_workqueue(rxrpc_workqueue);
648af7fc 852 rxrpc_exit_security();
17926a79
DH
853 kmem_cache_destroy(rxrpc_call_jar);
854 _leave("");
855}
856
857module_init(af_rxrpc_init);
858module_exit(af_rxrpc_exit);