1 /* AF_RXRPC implementation
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/net.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/random.h>
20 #include <linux/poll.h>
21 #include <linux/proc_fs.h>
22 #include <linux/key-type.h>
23 #include <net/net_namespace.h>
25 #include <net/af_rxrpc.h>
26 #define CREATE_TRACE_POINTS
27 #include "ar-internal.h"
29 MODULE_DESCRIPTION("RxRPC network protocol");
30 MODULE_AUTHOR("Red Hat, Inc.");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS_NETPROTO(PF_RXRPC
);
34 unsigned int rxrpc_debug
; // = RXRPC_DEBUG_KPROTO;
35 module_param_named(debug
, rxrpc_debug
, uint
, S_IWUSR
| S_IRUGO
);
36 MODULE_PARM_DESC(debug
, "RxRPC debugging mask");
38 static struct proto rxrpc_proto
;
39 static const struct proto_ops rxrpc_rpc_ops
;
41 /* local epoch for detecting local-end reset */
44 /* current debugging ID */
45 atomic_t rxrpc_debug_id
;
47 /* count of skbs currently in use */
48 atomic_t rxrpc_n_tx_skbs
, rxrpc_n_rx_skbs
;
50 struct workqueue_struct
*rxrpc_workqueue
;
52 static void rxrpc_sock_destructor(struct sock
*);
55 * see if an RxRPC socket is currently writable
57 static inline int rxrpc_writable(struct sock
*sk
)
59 return atomic_read(&sk
->sk_wmem_alloc
) < (size_t) sk
->sk_sndbuf
;
63 * wait for write bufferage to become available
65 static void rxrpc_write_space(struct sock
*sk
)
69 if (rxrpc_writable(sk
)) {
70 struct socket_wq
*wq
= rcu_dereference(sk
->sk_wq
);
72 if (skwq_has_sleeper(wq
))
73 wake_up_interruptible(&wq
->wait
);
74 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
80 * validate an RxRPC address
82 static int rxrpc_validate_address(struct rxrpc_sock
*rx
,
83 struct sockaddr_rxrpc
*srx
,
88 if (len
< sizeof(struct sockaddr_rxrpc
))
91 if (srx
->srx_family
!= AF_RXRPC
)
94 if (srx
->transport_type
!= SOCK_DGRAM
)
95 return -ESOCKTNOSUPPORT
;
97 len
-= offsetof(struct sockaddr_rxrpc
, transport
);
98 if (srx
->transport_len
< sizeof(sa_family_t
) ||
99 srx
->transport_len
> len
)
102 if (srx
->transport
.family
!= rx
->family
)
103 return -EAFNOSUPPORT
;
105 switch (srx
->transport
.family
) {
107 if (srx
->transport_len
< sizeof(struct sockaddr_in
))
109 tail
= offsetof(struct sockaddr_rxrpc
, transport
.sin
.__pad
);
112 #ifdef CONFIG_AF_RXRPC_IPV6
114 if (srx
->transport_len
< sizeof(struct sockaddr_in6
))
116 tail
= offsetof(struct sockaddr_rxrpc
, transport
) +
117 sizeof(struct sockaddr_in6
);
122 return -EAFNOSUPPORT
;
126 memset((void *)srx
+ tail
, 0, len
- tail
);
127 _debug("INET: %pISp", &srx
->transport
);
132 * bind a local address to an RxRPC socket
134 static int rxrpc_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
136 struct sockaddr_rxrpc
*srx
= (struct sockaddr_rxrpc
*)saddr
;
137 struct sock
*sk
= sock
->sk
;
138 struct rxrpc_local
*local
;
139 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
140 u16 service_id
= srx
->srx_service
;
143 _enter("%p,%p,%d", rx
, saddr
, len
);
145 ret
= rxrpc_validate_address(rx
, srx
, len
);
151 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
) {
156 memcpy(&rx
->srx
, srx
, sizeof(rx
->srx
));
158 local
= rxrpc_lookup_local(&rx
->srx
);
160 ret
= PTR_ERR(local
);
165 write_lock(&local
->services_lock
);
166 if (rcu_access_pointer(local
->service
))
169 rcu_assign_pointer(local
->service
, rx
);
170 write_unlock(&local
->services_lock
);
172 rx
->sk
.sk_state
= RXRPC_SERVER_BOUND
;
175 rx
->sk
.sk_state
= RXRPC_CLIENT_BOUND
;
178 release_sock(&rx
->sk
);
183 write_unlock(&local
->services_lock
);
184 rxrpc_put_local(local
);
187 release_sock(&rx
->sk
);
189 _leave(" = %d", ret
);
194 * set the number of pending calls permitted on a listening socket
196 static int rxrpc_listen(struct socket
*sock
, int backlog
)
198 struct sock
*sk
= sock
->sk
;
199 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
200 unsigned int max
, old
;
203 _enter("%p,%d", rx
, backlog
);
207 switch (rx
->sk
.sk_state
) {
209 ret
= -EADDRNOTAVAIL
;
211 case RXRPC_SERVER_BOUND
:
212 ASSERT(rx
->local
!= NULL
);
213 max
= READ_ONCE(rxrpc_max_backlog
);
215 if (backlog
== INT_MAX
)
217 else if (backlog
< 0 || backlog
> max
)
219 old
= sk
->sk_max_ack_backlog
;
220 sk
->sk_max_ack_backlog
= backlog
;
221 ret
= rxrpc_service_prealloc(rx
, GFP_KERNEL
);
223 rx
->sk
.sk_state
= RXRPC_SERVER_LISTENING
;
225 sk
->sk_max_ack_backlog
= old
;
232 release_sock(&rx
->sk
);
233 _leave(" = %d", ret
);
238 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
239 * @sock: The socket on which to make the call
240 * @srx: The address of the peer to contact
241 * @key: The security context to use (defaults to socket setting)
242 * @user_call_ID: The ID to use
243 * @gfp: The allocation constraints
244 * @notify_rx: Where to send notifications instead of socket queue
246 * Allow a kernel service to begin a call on the nominated socket. This just
247 * sets up all the internal tracking structures and allocates connection and
248 * call IDs as appropriate. The call to be used is returned.
250 * The default socket destination address and security may be overridden by
251 * supplying @srx and @key.
253 struct rxrpc_call
*rxrpc_kernel_begin_call(struct socket
*sock
,
254 struct sockaddr_rxrpc
*srx
,
256 unsigned long user_call_ID
,
258 rxrpc_notify_rx_t notify_rx
)
260 struct rxrpc_conn_parameters cp
;
261 struct rxrpc_call
*call
;
262 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
265 _enter(",,%x,%lx", key_serial(key
), user_call_ID
);
267 ret
= rxrpc_validate_address(rx
, srx
, sizeof(*srx
));
275 if (key
&& !key
->payload
.data
[0])
276 key
= NULL
; /* a no-security key */
278 memset(&cp
, 0, sizeof(cp
));
279 cp
.local
= rx
->local
;
281 cp
.security_level
= 0;
282 cp
.exclusive
= false;
283 cp
.service_id
= srx
->srx_service
;
284 call
= rxrpc_new_client_call(rx
, &cp
, srx
, user_call_ID
, gfp
);
286 call
->notify_rx
= notify_rx
;
288 release_sock(&rx
->sk
);
289 _leave(" = %p", call
);
292 EXPORT_SYMBOL(rxrpc_kernel_begin_call
);
295 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
296 * @sock: The socket the call is on
297 * @call: The call to end
299 * Allow a kernel service to end a call it was using. The call must be
300 * complete before this is called (the call should be aborted if necessary).
302 void rxrpc_kernel_end_call(struct socket
*sock
, struct rxrpc_call
*call
)
304 _enter("%d{%d}", call
->debug_id
, atomic_read(&call
->usage
));
305 rxrpc_release_call(rxrpc_sk(sock
->sk
), call
);
306 rxrpc_put_call(call
, rxrpc_call_put_kernel
);
308 EXPORT_SYMBOL(rxrpc_kernel_end_call
);
311 * rxrpc_kernel_new_call_notification - Get notifications of new calls
312 * @sock: The socket to intercept received messages on
313 * @notify_new_call: Function to be called when new calls appear
314 * @discard_new_call: Function to discard preallocated calls
316 * Allow a kernel service to be given notifications about new calls.
318 void rxrpc_kernel_new_call_notification(
320 rxrpc_notify_new_call_t notify_new_call
,
321 rxrpc_discard_new_call_t discard_new_call
)
323 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
325 rx
->notify_new_call
= notify_new_call
;
326 rx
->discard_new_call
= discard_new_call
;
328 EXPORT_SYMBOL(rxrpc_kernel_new_call_notification
);
331 * connect an RxRPC socket
332 * - this just targets it at a specific destination; no actual connection
333 * negotiation takes place
335 static int rxrpc_connect(struct socket
*sock
, struct sockaddr
*addr
,
336 int addr_len
, int flags
)
338 struct sockaddr_rxrpc
*srx
= (struct sockaddr_rxrpc
*)addr
;
339 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
342 _enter("%p,%p,%d,%d", rx
, addr
, addr_len
, flags
);
344 ret
= rxrpc_validate_address(rx
, srx
, addr_len
);
346 _leave(" = %d [bad addr]", ret
);
353 if (test_bit(RXRPC_SOCK_CONNECTED
, &rx
->flags
))
356 switch (rx
->sk
.sk_state
) {
358 rx
->sk
.sk_state
= RXRPC_CLIENT_UNBOUND
;
359 case RXRPC_CLIENT_UNBOUND
:
360 case RXRPC_CLIENT_BOUND
:
367 rx
->connect_srx
= *srx
;
368 set_bit(RXRPC_SOCK_CONNECTED
, &rx
->flags
);
372 release_sock(&rx
->sk
);
377 * send a message through an RxRPC socket
378 * - in a client this does a number of things:
379 * - finds/sets up a connection for the security specified (if any)
380 * - initiates a call (ID in control data)
381 * - ends the request phase of a call (if MSG_MORE is not set)
382 * - sends a call data packet
383 * - may send an abort (abort code in control data)
385 static int rxrpc_sendmsg(struct socket
*sock
, struct msghdr
*m
, size_t len
)
387 struct rxrpc_local
*local
;
388 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
391 _enter(",{%d},,%zu", rx
->sk
.sk_state
, len
);
393 if (m
->msg_flags
& MSG_OOB
)
397 ret
= rxrpc_validate_address(rx
, m
->msg_name
, m
->msg_namelen
);
399 _leave(" = %d [bad addr]", ret
);
406 switch (rx
->sk
.sk_state
) {
408 rx
->srx
.srx_family
= AF_RXRPC
;
409 rx
->srx
.srx_service
= 0;
410 rx
->srx
.transport_type
= SOCK_DGRAM
;
411 rx
->srx
.transport
.family
= rx
->family
;
412 switch (rx
->family
) {
414 rx
->srx
.transport_len
= sizeof(struct sockaddr_in
);
416 #ifdef CONFIG_AF_RXRPC_IPV6
418 rx
->srx
.transport_len
= sizeof(struct sockaddr_in6
);
425 local
= rxrpc_lookup_local(&rx
->srx
);
427 ret
= PTR_ERR(local
);
432 rx
->sk
.sk_state
= RXRPC_CLIENT_UNBOUND
;
435 case RXRPC_CLIENT_UNBOUND
:
436 case RXRPC_CLIENT_BOUND
:
438 test_bit(RXRPC_SOCK_CONNECTED
, &rx
->flags
)) {
439 m
->msg_name
= &rx
->connect_srx
;
440 m
->msg_namelen
= sizeof(rx
->connect_srx
);
442 case RXRPC_SERVER_BOUND
:
443 case RXRPC_SERVER_LISTENING
:
444 ret
= rxrpc_do_sendmsg(rx
, m
, len
);
452 release_sock(&rx
->sk
);
453 _leave(" = %d", ret
);
458 * set RxRPC socket options
460 static int rxrpc_setsockopt(struct socket
*sock
, int level
, int optname
,
461 char __user
*optval
, unsigned int optlen
)
463 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
464 unsigned int min_sec_level
;
467 _enter(",%d,%d,,%d", level
, optname
, optlen
);
472 if (level
== SOL_RXRPC
) {
474 case RXRPC_EXCLUSIVE_CONNECTION
:
479 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
481 rx
->exclusive
= true;
484 case RXRPC_SECURITY_KEY
:
489 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
491 ret
= rxrpc_request_key(rx
, optval
, optlen
);
494 case RXRPC_SECURITY_KEYRING
:
499 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
501 ret
= rxrpc_server_keyring(rx
, optval
, optlen
);
504 case RXRPC_MIN_SECURITY_LEVEL
:
506 if (optlen
!= sizeof(unsigned int))
509 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
511 ret
= get_user(min_sec_level
,
512 (unsigned int __user
*) optval
);
516 if (min_sec_level
> RXRPC_SECURITY_MAX
)
518 rx
->min_sec_level
= min_sec_level
;
529 release_sock(&rx
->sk
);
534 * permit an RxRPC socket to be polled
536 static unsigned int rxrpc_poll(struct file
*file
, struct socket
*sock
,
539 struct sock
*sk
= sock
->sk
;
540 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
543 sock_poll_wait(file
, sk_sleep(sk
), wait
);
546 /* the socket is readable if there are any messages waiting on the Rx
548 if (!list_empty(&rx
->recvmsg_q
))
549 mask
|= POLLIN
| POLLRDNORM
;
551 /* the socket is writable if there is space to add new data to the
552 * socket; there is no guarantee that any particular call in progress
553 * on the socket may have space in the Tx ACK window */
554 if (rxrpc_writable(sk
))
555 mask
|= POLLOUT
| POLLWRNORM
;
561 * create an RxRPC socket
563 static int rxrpc_create(struct net
*net
, struct socket
*sock
, int protocol
,
566 struct rxrpc_sock
*rx
;
569 _enter("%p,%d", sock
, protocol
);
571 if (!net_eq(net
, &init_net
))
572 return -EAFNOSUPPORT
;
574 /* we support transport protocol UDP/UDP6 only */
575 if (protocol
!= PF_INET
&&
576 IS_ENABLED(CONFIG_AF_RXRPC_IPV6
) && protocol
!= PF_INET6
)
577 return -EPROTONOSUPPORT
;
579 if (sock
->type
!= SOCK_DGRAM
)
580 return -ESOCKTNOSUPPORT
;
582 sock
->ops
= &rxrpc_rpc_ops
;
583 sock
->state
= SS_UNCONNECTED
;
585 sk
= sk_alloc(net
, PF_RXRPC
, GFP_KERNEL
, &rxrpc_proto
, kern
);
589 sock_init_data(sock
, sk
);
590 sock_set_flag(sk
, SOCK_RCU_FREE
);
591 sk
->sk_state
= RXRPC_UNBOUND
;
592 sk
->sk_write_space
= rxrpc_write_space
;
593 sk
->sk_max_ack_backlog
= 0;
594 sk
->sk_destruct
= rxrpc_sock_destructor
;
597 rx
->family
= protocol
;
600 spin_lock_init(&rx
->incoming_lock
);
601 INIT_LIST_HEAD(&rx
->sock_calls
);
602 INIT_LIST_HEAD(&rx
->to_be_accepted
);
603 INIT_LIST_HEAD(&rx
->recvmsg_q
);
604 rwlock_init(&rx
->recvmsg_lock
);
605 rwlock_init(&rx
->call_lock
);
606 memset(&rx
->srx
, 0, sizeof(rx
->srx
));
608 _leave(" = 0 [%p]", rx
);
613 * Kill all the calls on a socket and shut it down.
615 static int rxrpc_shutdown(struct socket
*sock
, int flags
)
617 struct sock
*sk
= sock
->sk
;
618 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
621 _enter("%p,%d", sk
, flags
);
623 if (flags
!= SHUT_RDWR
)
625 if (sk
->sk_state
== RXRPC_CLOSE
)
630 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
631 if (sk
->sk_state
< RXRPC_CLOSE
) {
632 sk
->sk_state
= RXRPC_CLOSE
;
633 sk
->sk_shutdown
= SHUTDOWN_MASK
;
637 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
639 rxrpc_discard_prealloc(rx
);
646 * RxRPC socket destructor
648 static void rxrpc_sock_destructor(struct sock
*sk
)
652 rxrpc_purge_queue(&sk
->sk_receive_queue
);
654 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
655 WARN_ON(!sk_unhashed(sk
));
656 WARN_ON(sk
->sk_socket
);
658 if (!sock_flag(sk
, SOCK_DEAD
)) {
659 printk("Attempt to release alive rxrpc socket: %p\n", sk
);
665 * release an RxRPC socket
667 static int rxrpc_release_sock(struct sock
*sk
)
669 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
671 _enter("%p{%d,%d}", sk
, sk
->sk_state
, atomic_read(&sk
->sk_refcnt
));
673 /* declare the socket closed for business */
675 sk
->sk_shutdown
= SHUTDOWN_MASK
;
677 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
678 sk
->sk_state
= RXRPC_CLOSE
;
679 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
681 if (rx
->local
&& rcu_access_pointer(rx
->local
->service
) == rx
) {
682 write_lock(&rx
->local
->services_lock
);
683 rcu_assign_pointer(rx
->local
->service
, NULL
);
684 write_unlock(&rx
->local
->services_lock
);
687 /* try to flush out this socket */
688 rxrpc_discard_prealloc(rx
);
689 rxrpc_release_calls_on_socket(rx
);
690 flush_workqueue(rxrpc_workqueue
);
691 rxrpc_purge_queue(&sk
->sk_receive_queue
);
693 rxrpc_put_local(rx
->local
);
697 key_put(rx
->securities
);
698 rx
->securities
= NULL
;
706 * release an RxRPC BSD socket on close() or equivalent
708 static int rxrpc_release(struct socket
*sock
)
710 struct sock
*sk
= sock
->sk
;
712 _enter("%p{%p}", sock
, sk
);
719 return rxrpc_release_sock(sk
);
723 * RxRPC network protocol
725 static const struct proto_ops rxrpc_rpc_ops
= {
727 .owner
= THIS_MODULE
,
728 .release
= rxrpc_release
,
730 .connect
= rxrpc_connect
,
731 .socketpair
= sock_no_socketpair
,
732 .accept
= sock_no_accept
,
733 .getname
= sock_no_getname
,
735 .ioctl
= sock_no_ioctl
,
736 .listen
= rxrpc_listen
,
737 .shutdown
= rxrpc_shutdown
,
738 .setsockopt
= rxrpc_setsockopt
,
739 .getsockopt
= sock_no_getsockopt
,
740 .sendmsg
= rxrpc_sendmsg
,
741 .recvmsg
= rxrpc_recvmsg
,
742 .mmap
= sock_no_mmap
,
743 .sendpage
= sock_no_sendpage
,
746 static struct proto rxrpc_proto
= {
748 .owner
= THIS_MODULE
,
749 .obj_size
= sizeof(struct rxrpc_sock
),
750 .max_header
= sizeof(struct rxrpc_wire_header
),
753 static const struct net_proto_family rxrpc_family_ops
= {
755 .create
= rxrpc_create
,
756 .owner
= THIS_MODULE
,
760 * initialise and register the RxRPC protocol
762 static int __init
af_rxrpc_init(void)
767 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv
) > FIELD_SIZEOF(struct sk_buff
, cb
));
769 get_random_bytes(&rxrpc_epoch
, sizeof(rxrpc_epoch
));
770 rxrpc_epoch
|= RXRPC_RANDOM_EPOCH
;
771 get_random_bytes(&tmp
, sizeof(tmp
));
775 idr_set_cursor(&rxrpc_client_conn_ids
, tmp
);
778 rxrpc_call_jar
= kmem_cache_create(
779 "rxrpc_call_jar", sizeof(struct rxrpc_call
), 0,
780 SLAB_HWCACHE_ALIGN
, NULL
);
781 if (!rxrpc_call_jar
) {
782 pr_notice("Failed to allocate call jar\n");
786 rxrpc_workqueue
= alloc_workqueue("krxrpcd", 0, 1);
787 if (!rxrpc_workqueue
) {
788 pr_notice("Failed to allocate work queue\n");
789 goto error_work_queue
;
792 ret
= rxrpc_init_security();
794 pr_crit("Cannot initialise security\n");
798 ret
= proto_register(&rxrpc_proto
, 1);
800 pr_crit("Cannot register protocol\n");
804 ret
= sock_register(&rxrpc_family_ops
);
806 pr_crit("Cannot register socket family\n");
810 ret
= register_key_type(&key_type_rxrpc
);
812 pr_crit("Cannot register client key type\n");
816 ret
= register_key_type(&key_type_rxrpc_s
);
818 pr_crit("Cannot register server key type\n");
819 goto error_key_type_s
;
822 ret
= rxrpc_sysctl_init();
824 pr_crit("Cannot register sysctls\n");
828 #ifdef CONFIG_PROC_FS
829 proc_create("rxrpc_calls", 0, init_net
.proc_net
, &rxrpc_call_seq_fops
);
830 proc_create("rxrpc_conns", 0, init_net
.proc_net
,
831 &rxrpc_connection_seq_fops
);
836 unregister_key_type(&key_type_rxrpc_s
);
838 unregister_key_type(&key_type_rxrpc
);
840 sock_unregister(PF_RXRPC
);
842 proto_unregister(&rxrpc_proto
);
844 rxrpc_exit_security();
846 destroy_workqueue(rxrpc_workqueue
);
848 kmem_cache_destroy(rxrpc_call_jar
);
854 * unregister the RxRPC protocol
856 static void __exit
af_rxrpc_exit(void)
860 unregister_key_type(&key_type_rxrpc_s
);
861 unregister_key_type(&key_type_rxrpc
);
862 sock_unregister(PF_RXRPC
);
863 proto_unregister(&rxrpc_proto
);
864 rxrpc_destroy_all_calls();
865 rxrpc_destroy_all_connections();
866 ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs
), ==, 0);
867 ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs
), ==, 0);
868 rxrpc_destroy_all_locals();
870 remove_proc_entry("rxrpc_conns", init_net
.proc_net
);
871 remove_proc_entry("rxrpc_calls", init_net
.proc_net
);
872 destroy_workqueue(rxrpc_workqueue
);
873 rxrpc_exit_security();
874 kmem_cache_destroy(rxrpc_call_jar
);
878 module_init(af_rxrpc_init
);
879 module_exit(af_rxrpc_exit
);