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/poll.h>
20 #include <linux/proc_fs.h>
21 #include <linux/key-type.h>
22 #include <net/net_namespace.h>
24 #include <net/af_rxrpc.h>
25 #include "ar-internal.h"
27 MODULE_DESCRIPTION("RxRPC network protocol");
28 MODULE_AUTHOR("Red Hat, Inc.");
29 MODULE_LICENSE("GPL");
30 MODULE_ALIAS_NETPROTO(PF_RXRPC
);
32 unsigned int rxrpc_debug
; // = RXRPC_DEBUG_KPROTO;
33 module_param_named(debug
, rxrpc_debug
, uint
, S_IWUSR
| S_IRUGO
);
34 MODULE_PARM_DESC(debug
, "RxRPC debugging mask");
36 static struct proto rxrpc_proto
;
37 static const struct proto_ops rxrpc_rpc_ops
;
39 /* local epoch for detecting local-end reset */
42 /* current debugging ID */
43 atomic_t rxrpc_debug_id
;
45 /* count of skbs currently in use */
46 atomic_t rxrpc_n_skbs
;
48 struct workqueue_struct
*rxrpc_workqueue
;
50 static void rxrpc_sock_destructor(struct sock
*);
53 * see if an RxRPC socket is currently writable
55 static inline int rxrpc_writable(struct sock
*sk
)
57 return atomic_read(&sk
->sk_wmem_alloc
) < (size_t) sk
->sk_sndbuf
;
61 * wait for write bufferage to become available
63 static void rxrpc_write_space(struct sock
*sk
)
67 if (rxrpc_writable(sk
)) {
68 struct socket_wq
*wq
= rcu_dereference(sk
->sk_wq
);
70 if (skwq_has_sleeper(wq
))
71 wake_up_interruptible(&wq
->wait
);
72 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
78 * validate an RxRPC address
80 static int rxrpc_validate_address(struct rxrpc_sock
*rx
,
81 struct sockaddr_rxrpc
*srx
,
86 if (len
< sizeof(struct sockaddr_rxrpc
))
89 if (srx
->srx_family
!= AF_RXRPC
)
92 if (srx
->transport_type
!= SOCK_DGRAM
)
93 return -ESOCKTNOSUPPORT
;
95 len
-= offsetof(struct sockaddr_rxrpc
, transport
);
96 if (srx
->transport_len
< sizeof(sa_family_t
) ||
97 srx
->transport_len
> len
)
100 if (srx
->transport
.family
!= rx
->family
)
101 return -EAFNOSUPPORT
;
103 switch (srx
->transport
.family
) {
105 if (srx
->transport_len
< sizeof(struct sockaddr_in
))
107 _debug("INET: %x @ %pI4",
108 ntohs(srx
->transport
.sin
.sin_port
),
109 &srx
->transport
.sin
.sin_addr
);
110 tail
= offsetof(struct sockaddr_rxrpc
, transport
.sin
.__pad
);
115 return -EAFNOSUPPORT
;
119 memset((void *)srx
+ tail
, 0, len
- tail
);
124 * bind a local address to an RxRPC socket
126 static int rxrpc_bind(struct socket
*sock
, struct sockaddr
*saddr
, int len
)
128 struct sockaddr_rxrpc
*srx
= (struct sockaddr_rxrpc
*)saddr
;
129 struct sock
*sk
= sock
->sk
;
130 struct rxrpc_local
*local
;
131 struct rxrpc_sock
*rx
= rxrpc_sk(sk
), *prx
;
134 _enter("%p,%p,%d", rx
, saddr
, len
);
136 ret
= rxrpc_validate_address(rx
, srx
, len
);
142 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
) {
147 memcpy(&rx
->srx
, srx
, sizeof(rx
->srx
));
149 local
= rxrpc_lookup_local(&rx
->srx
);
151 ret
= PTR_ERR(local
);
155 if (rx
->srx
.srx_service
) {
156 write_lock_bh(&local
->services_lock
);
157 list_for_each_entry(prx
, &local
->services
, listen_link
) {
158 if (prx
->srx
.srx_service
== rx
->srx
.srx_service
)
163 list_add_tail(&rx
->listen_link
, &local
->services
);
164 write_unlock_bh(&local
->services_lock
);
166 rx
->sk
.sk_state
= RXRPC_SERVER_BOUND
;
169 rx
->sk
.sk_state
= RXRPC_CLIENT_BOUND
;
172 release_sock(&rx
->sk
);
177 write_unlock_bh(&local
->services_lock
);
178 rxrpc_put_local(local
);
181 release_sock(&rx
->sk
);
183 _leave(" = %d", ret
);
188 * set the number of pending calls permitted on a listening socket
190 static int rxrpc_listen(struct socket
*sock
, int backlog
)
192 struct sock
*sk
= sock
->sk
;
193 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
197 _enter("%p,%d", rx
, backlog
);
201 switch (rx
->sk
.sk_state
) {
203 ret
= -EADDRNOTAVAIL
;
205 case RXRPC_SERVER_BOUND
:
206 ASSERT(rx
->local
!= NULL
);
207 max
= READ_ONCE(rxrpc_max_backlog
);
209 if (backlog
== INT_MAX
)
211 else if (backlog
< 0 || backlog
> max
)
213 sk
->sk_max_ack_backlog
= backlog
;
214 rx
->sk
.sk_state
= RXRPC_SERVER_LISTENING
;
222 release_sock(&rx
->sk
);
223 _leave(" = %d", ret
);
228 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
229 * @sock: The socket on which to make the call
230 * @srx: The address of the peer to contact
231 * @key: The security context to use (defaults to socket setting)
232 * @user_call_ID: The ID to use
234 * Allow a kernel service to begin a call on the nominated socket. This just
235 * sets up all the internal tracking structures and allocates connection and
236 * call IDs as appropriate. The call to be used is returned.
238 * The default socket destination address and security may be overridden by
239 * supplying @srx and @key.
241 struct rxrpc_call
*rxrpc_kernel_begin_call(struct socket
*sock
,
242 struct sockaddr_rxrpc
*srx
,
244 unsigned long user_call_ID
,
247 struct rxrpc_conn_parameters cp
;
248 struct rxrpc_call
*call
;
249 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
252 _enter(",,%x,%lx", key_serial(key
), user_call_ID
);
254 ret
= rxrpc_validate_address(rx
, srx
, sizeof(*srx
));
262 if (key
&& !key
->payload
.data
[0])
263 key
= NULL
; /* a no-security key */
265 memset(&cp
, 0, sizeof(cp
));
266 cp
.local
= rx
->local
;
268 cp
.security_level
= 0;
269 cp
.exclusive
= false;
270 cp
.service_id
= srx
->srx_service
;
271 call
= rxrpc_new_client_call(rx
, &cp
, srx
, user_call_ID
, gfp
);
273 release_sock(&rx
->sk
);
274 _leave(" = %p", call
);
277 EXPORT_SYMBOL(rxrpc_kernel_begin_call
);
280 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
281 * @call: The call to end
283 * Allow a kernel service to end a call it was using. The call must be
284 * complete before this is called (the call should be aborted if necessary).
286 void rxrpc_kernel_end_call(struct rxrpc_call
*call
)
288 _enter("%d{%d}", call
->debug_id
, atomic_read(&call
->usage
));
289 rxrpc_remove_user_ID(call
->socket
, call
);
290 rxrpc_put_call(call
);
292 EXPORT_SYMBOL(rxrpc_kernel_end_call
);
295 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
296 * @sock: The socket to intercept received messages on
297 * @interceptor: The function to pass the messages to
299 * Allow a kernel service to intercept messages heading for the Rx queue on an
300 * RxRPC socket. They get passed to the specified function instead.
301 * @interceptor should free the socket buffers it is given. @interceptor is
302 * called with the socket receive queue spinlock held and softirqs disabled -
303 * this ensures that the messages will be delivered in the right order.
305 void rxrpc_kernel_intercept_rx_messages(struct socket
*sock
,
306 rxrpc_interceptor_t interceptor
)
308 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
311 rx
->interceptor
= interceptor
;
314 EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages
);
317 * connect an RxRPC socket
318 * - this just targets it at a specific destination; no actual connection
319 * negotiation takes place
321 static int rxrpc_connect(struct socket
*sock
, struct sockaddr
*addr
,
322 int addr_len
, int flags
)
324 struct sockaddr_rxrpc
*srx
= (struct sockaddr_rxrpc
*)addr
;
325 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
328 _enter("%p,%p,%d,%d", rx
, addr
, addr_len
, flags
);
330 ret
= rxrpc_validate_address(rx
, srx
, addr_len
);
332 _leave(" = %d [bad addr]", ret
);
339 if (test_bit(RXRPC_SOCK_CONNECTED
, &rx
->flags
))
342 switch (rx
->sk
.sk_state
) {
344 rx
->sk
.sk_state
= RXRPC_CLIENT_UNBOUND
;
345 case RXRPC_CLIENT_UNBOUND
:
346 case RXRPC_CLIENT_BOUND
:
353 rx
->connect_srx
= *srx
;
354 set_bit(RXRPC_SOCK_CONNECTED
, &rx
->flags
);
358 release_sock(&rx
->sk
);
363 * send a message through an RxRPC socket
364 * - in a client this does a number of things:
365 * - finds/sets up a connection for the security specified (if any)
366 * - initiates a call (ID in control data)
367 * - ends the request phase of a call (if MSG_MORE is not set)
368 * - sends a call data packet
369 * - may send an abort (abort code in control data)
371 static int rxrpc_sendmsg(struct socket
*sock
, struct msghdr
*m
, size_t len
)
373 struct rxrpc_local
*local
;
374 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
377 _enter(",{%d},,%zu", rx
->sk
.sk_state
, len
);
379 if (m
->msg_flags
& MSG_OOB
)
383 ret
= rxrpc_validate_address(rx
, m
->msg_name
, m
->msg_namelen
);
385 _leave(" = %d [bad addr]", ret
);
392 switch (rx
->sk
.sk_state
) {
394 local
= rxrpc_lookup_local(&rx
->srx
);
396 ret
= PTR_ERR(local
);
401 rx
->sk
.sk_state
= RXRPC_CLIENT_UNBOUND
;
404 case RXRPC_CLIENT_UNBOUND
:
405 case RXRPC_CLIENT_BOUND
:
407 test_bit(RXRPC_SOCK_CONNECTED
, &rx
->flags
)) {
408 m
->msg_name
= &rx
->connect_srx
;
409 m
->msg_namelen
= sizeof(rx
->connect_srx
);
411 case RXRPC_SERVER_BOUND
:
412 case RXRPC_SERVER_LISTENING
:
413 ret
= rxrpc_do_sendmsg(rx
, m
, len
);
421 release_sock(&rx
->sk
);
422 _leave(" = %d", ret
);
427 * set RxRPC socket options
429 static int rxrpc_setsockopt(struct socket
*sock
, int level
, int optname
,
430 char __user
*optval
, unsigned int optlen
)
432 struct rxrpc_sock
*rx
= rxrpc_sk(sock
->sk
);
433 unsigned int min_sec_level
;
436 _enter(",%d,%d,,%d", level
, optname
, optlen
);
441 if (level
== SOL_RXRPC
) {
443 case RXRPC_EXCLUSIVE_CONNECTION
:
448 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
450 rx
->exclusive
= true;
453 case RXRPC_SECURITY_KEY
:
458 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
460 ret
= rxrpc_request_key(rx
, optval
, optlen
);
463 case RXRPC_SECURITY_KEYRING
:
468 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
470 ret
= rxrpc_server_keyring(rx
, optval
, optlen
);
473 case RXRPC_MIN_SECURITY_LEVEL
:
475 if (optlen
!= sizeof(unsigned int))
478 if (rx
->sk
.sk_state
!= RXRPC_UNBOUND
)
480 ret
= get_user(min_sec_level
,
481 (unsigned int __user
*) optval
);
485 if (min_sec_level
> RXRPC_SECURITY_MAX
)
487 rx
->min_sec_level
= min_sec_level
;
498 release_sock(&rx
->sk
);
503 * permit an RxRPC socket to be polled
505 static unsigned int rxrpc_poll(struct file
*file
, struct socket
*sock
,
509 struct sock
*sk
= sock
->sk
;
511 sock_poll_wait(file
, sk_sleep(sk
), wait
);
514 /* the socket is readable if there are any messages waiting on the Rx
516 if (!skb_queue_empty(&sk
->sk_receive_queue
))
517 mask
|= POLLIN
| POLLRDNORM
;
519 /* the socket is writable if there is space to add new data to the
520 * socket; there is no guarantee that any particular call in progress
521 * on the socket may have space in the Tx ACK window */
522 if (rxrpc_writable(sk
))
523 mask
|= POLLOUT
| POLLWRNORM
;
529 * create an RxRPC socket
531 static int rxrpc_create(struct net
*net
, struct socket
*sock
, int protocol
,
534 struct rxrpc_sock
*rx
;
537 _enter("%p,%d", sock
, protocol
);
539 if (!net_eq(net
, &init_net
))
540 return -EAFNOSUPPORT
;
542 /* we support transport protocol UDP/UDP6 only */
543 if (protocol
!= PF_INET
)
544 return -EPROTONOSUPPORT
;
546 if (sock
->type
!= SOCK_DGRAM
)
547 return -ESOCKTNOSUPPORT
;
549 sock
->ops
= &rxrpc_rpc_ops
;
550 sock
->state
= SS_UNCONNECTED
;
552 sk
= sk_alloc(net
, PF_RXRPC
, GFP_KERNEL
, &rxrpc_proto
, kern
);
556 sock_init_data(sock
, sk
);
557 sk
->sk_state
= RXRPC_UNBOUND
;
558 sk
->sk_write_space
= rxrpc_write_space
;
559 sk
->sk_max_ack_backlog
= 0;
560 sk
->sk_destruct
= rxrpc_sock_destructor
;
563 rx
->family
= protocol
;
566 INIT_LIST_HEAD(&rx
->listen_link
);
567 INIT_LIST_HEAD(&rx
->secureq
);
568 INIT_LIST_HEAD(&rx
->acceptq
);
569 rwlock_init(&rx
->call_lock
);
570 memset(&rx
->srx
, 0, sizeof(rx
->srx
));
572 _leave(" = 0 [%p]", rx
);
577 * RxRPC socket destructor
579 static void rxrpc_sock_destructor(struct sock
*sk
)
583 rxrpc_purge_queue(&sk
->sk_receive_queue
);
585 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
586 WARN_ON(!sk_unhashed(sk
));
587 WARN_ON(sk
->sk_socket
);
589 if (!sock_flag(sk
, SOCK_DEAD
)) {
590 printk("Attempt to release alive rxrpc socket: %p\n", sk
);
596 * release an RxRPC socket
598 static int rxrpc_release_sock(struct sock
*sk
)
600 struct rxrpc_sock
*rx
= rxrpc_sk(sk
);
602 _enter("%p{%d,%d}", sk
, sk
->sk_state
, atomic_read(&sk
->sk_refcnt
));
604 /* declare the socket closed for business */
606 sk
->sk_shutdown
= SHUTDOWN_MASK
;
608 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
609 sk
->sk_state
= RXRPC_CLOSE
;
610 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
612 ASSERTCMP(rx
->listen_link
.next
, !=, LIST_POISON1
);
614 if (!list_empty(&rx
->listen_link
)) {
615 write_lock_bh(&rx
->local
->services_lock
);
616 list_del(&rx
->listen_link
);
617 write_unlock_bh(&rx
->local
->services_lock
);
620 /* try to flush out this socket */
621 rxrpc_release_calls_on_socket(rx
);
622 flush_workqueue(rxrpc_workqueue
);
623 rxrpc_purge_queue(&sk
->sk_receive_queue
);
625 rxrpc_put_local(rx
->local
);
629 key_put(rx
->securities
);
630 rx
->securities
= NULL
;
638 * release an RxRPC BSD socket on close() or equivalent
640 static int rxrpc_release(struct socket
*sock
)
642 struct sock
*sk
= sock
->sk
;
644 _enter("%p{%p}", sock
, sk
);
651 return rxrpc_release_sock(sk
);
655 * RxRPC network protocol
657 static const struct proto_ops rxrpc_rpc_ops
= {
659 .owner
= THIS_MODULE
,
660 .release
= rxrpc_release
,
662 .connect
= rxrpc_connect
,
663 .socketpair
= sock_no_socketpair
,
664 .accept
= sock_no_accept
,
665 .getname
= sock_no_getname
,
667 .ioctl
= sock_no_ioctl
,
668 .listen
= rxrpc_listen
,
669 .shutdown
= sock_no_shutdown
,
670 .setsockopt
= rxrpc_setsockopt
,
671 .getsockopt
= sock_no_getsockopt
,
672 .sendmsg
= rxrpc_sendmsg
,
673 .recvmsg
= rxrpc_recvmsg
,
674 .mmap
= sock_no_mmap
,
675 .sendpage
= sock_no_sendpage
,
678 static struct proto rxrpc_proto
= {
680 .owner
= THIS_MODULE
,
681 .obj_size
= sizeof(struct rxrpc_sock
),
682 .max_header
= sizeof(struct rxrpc_wire_header
),
685 static const struct net_proto_family rxrpc_family_ops
= {
687 .create
= rxrpc_create
,
688 .owner
= THIS_MODULE
,
692 * initialise and register the RxRPC protocol
694 static int __init
af_rxrpc_init(void)
698 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv
) > FIELD_SIZEOF(struct sk_buff
, cb
));
700 rxrpc_epoch
= get_seconds();
703 rxrpc_call_jar
= kmem_cache_create(
704 "rxrpc_call_jar", sizeof(struct rxrpc_call
), 0,
705 SLAB_HWCACHE_ALIGN
, NULL
);
706 if (!rxrpc_call_jar
) {
707 pr_notice("Failed to allocate call jar\n");
711 rxrpc_workqueue
= alloc_workqueue("krxrpcd", 0, 1);
712 if (!rxrpc_workqueue
) {
713 pr_notice("Failed to allocate work queue\n");
714 goto error_work_queue
;
717 ret
= rxrpc_init_security();
719 pr_crit("Cannot initialise security\n");
723 ret
= proto_register(&rxrpc_proto
, 1);
725 pr_crit("Cannot register protocol\n");
729 ret
= sock_register(&rxrpc_family_ops
);
731 pr_crit("Cannot register socket family\n");
735 ret
= register_key_type(&key_type_rxrpc
);
737 pr_crit("Cannot register client key type\n");
741 ret
= register_key_type(&key_type_rxrpc_s
);
743 pr_crit("Cannot register server key type\n");
744 goto error_key_type_s
;
747 ret
= rxrpc_sysctl_init();
749 pr_crit("Cannot register sysctls\n");
753 #ifdef CONFIG_PROC_FS
754 proc_create("rxrpc_calls", 0, init_net
.proc_net
, &rxrpc_call_seq_fops
);
755 proc_create("rxrpc_conns", 0, init_net
.proc_net
,
756 &rxrpc_connection_seq_fops
);
761 unregister_key_type(&key_type_rxrpc_s
);
763 unregister_key_type(&key_type_rxrpc
);
765 sock_unregister(PF_RXRPC
);
767 proto_unregister(&rxrpc_proto
);
769 rxrpc_exit_security();
771 destroy_workqueue(rxrpc_workqueue
);
773 kmem_cache_destroy(rxrpc_call_jar
);
779 * unregister the RxRPC protocol
781 static void __exit
af_rxrpc_exit(void)
785 unregister_key_type(&key_type_rxrpc_s
);
786 unregister_key_type(&key_type_rxrpc
);
787 sock_unregister(PF_RXRPC
);
788 proto_unregister(&rxrpc_proto
);
789 rxrpc_destroy_all_calls();
790 rxrpc_destroy_all_connections();
791 ASSERTCMP(atomic_read(&rxrpc_n_skbs
), ==, 0);
792 rxrpc_destroy_all_locals();
794 remove_proc_entry("rxrpc_conns", init_net
.proc_net
);
795 remove_proc_entry("rxrpc_calls", init_net
.proc_net
);
796 destroy_workqueue(rxrpc_workqueue
);
797 rxrpc_exit_security();
798 kmem_cache_destroy(rxrpc_call_jar
);
802 module_init(af_rxrpc_init
);
803 module_exit(af_rxrpc_exit
);