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 /* current debugging ID */
42 atomic_t rxrpc_debug_id;
44 /* count of skbs currently in use */
45 atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
47 struct workqueue_struct *rxrpc_workqueue;
49 static void rxrpc_sock_destructor(struct sock *);
52 * see if an RxRPC socket is currently writable
54 static inline int rxrpc_writable(struct sock *sk)
56 return refcount_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
60 * wait for write bufferage to become available
62 static void rxrpc_write_space(struct sock *sk)
66 if (rxrpc_writable(sk)) {
67 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
69 if (skwq_has_sleeper(wq))
70 wake_up_interruptible(&wq->wait);
71 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
77 * validate an RxRPC address
79 static int rxrpc_validate_address(struct rxrpc_sock *rx,
80 struct sockaddr_rxrpc *srx,
85 if (len < sizeof(struct sockaddr_rxrpc))
88 if (srx->srx_family != AF_RXRPC)
91 if (srx->transport_type != SOCK_DGRAM)
92 return -ESOCKTNOSUPPORT;
94 len -= offsetof(struct sockaddr_rxrpc, transport);
95 if (srx->transport_len < sizeof(sa_family_t) ||
96 srx->transport_len > len)
99 if (srx->transport.family != rx->family)
100 return -EAFNOSUPPORT;
102 switch (srx->transport.family) {
104 if (srx->transport_len < sizeof(struct sockaddr_in))
106 tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
109 #ifdef CONFIG_AF_RXRPC_IPV6
111 if (srx->transport_len < sizeof(struct sockaddr_in6))
113 tail = offsetof(struct sockaddr_rxrpc, transport) +
114 sizeof(struct sockaddr_in6);
119 return -EAFNOSUPPORT;
123 memset((void *)srx + tail, 0, len - tail);
124 _debug("INET: %pISp", &srx->transport);
129 * bind a local address to an RxRPC socket
131 static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
133 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
134 struct rxrpc_local *local;
135 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
136 u16 service_id = srx->srx_service;
139 _enter("%p,%p,%d", rx, saddr, len);
141 ret = rxrpc_validate_address(rx, srx, len);
147 switch (rx->sk.sk_state) {
150 local = rxrpc_lookup_local(sock_net(&rx->sk), &rx->srx);
152 ret = PTR_ERR(local);
157 write_lock(&local->services_lock);
158 if (rcu_access_pointer(local->service))
161 rcu_assign_pointer(local->service, rx);
162 write_unlock(&local->services_lock);
164 rx->sk.sk_state = RXRPC_SERVER_BOUND;
167 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
171 case RXRPC_SERVER_BOUND:
176 if (service_id == rx->srx.srx_service)
179 srx->srx_service = rx->srx.srx_service;
180 if (memcmp(srx, &rx->srx, sizeof(*srx)) != 0)
182 rx->second_service = service_id;
183 rx->sk.sk_state = RXRPC_SERVER_BOUND2;
191 release_sock(&rx->sk);
196 write_unlock(&local->services_lock);
197 rxrpc_put_local(local);
200 release_sock(&rx->sk);
202 _leave(" = %d", ret);
207 * set the number of pending calls permitted on a listening socket
209 static int rxrpc_listen(struct socket *sock, int backlog)
211 struct sock *sk = sock->sk;
212 struct rxrpc_sock *rx = rxrpc_sk(sk);
213 unsigned int max, old;
216 _enter("%p,%d", rx, backlog);
220 switch (rx->sk.sk_state) {
222 ret = -EADDRNOTAVAIL;
224 case RXRPC_SERVER_BOUND:
225 case RXRPC_SERVER_BOUND2:
226 ASSERT(rx->local != NULL);
227 max = READ_ONCE(rxrpc_max_backlog);
229 if (backlog == INT_MAX)
231 else if (backlog < 0 || backlog > max)
233 old = sk->sk_max_ack_backlog;
234 sk->sk_max_ack_backlog = backlog;
235 ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
237 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
239 sk->sk_max_ack_backlog = old;
241 case RXRPC_SERVER_LISTENING:
243 rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
244 sk->sk_max_ack_backlog = 0;
245 rxrpc_discard_prealloc(rx);
254 release_sock(&rx->sk);
255 _leave(" = %d", ret);
260 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
261 * @sock: The socket on which to make the call
262 * @srx: The address of the peer to contact
263 * @key: The security context to use (defaults to socket setting)
264 * @user_call_ID: The ID to use
265 * @tx_total_len: Total length of data to transmit during the call (or -1)
266 * @gfp: The allocation constraints
267 * @notify_rx: Where to send notifications instead of socket queue
269 * Allow a kernel service to begin a call on the nominated socket. This just
270 * sets up all the internal tracking structures and allocates connection and
271 * call IDs as appropriate. The call to be used is returned.
273 * The default socket destination address and security may be overridden by
274 * supplying @srx and @key.
276 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
277 struct sockaddr_rxrpc *srx,
279 unsigned long user_call_ID,
282 rxrpc_notify_rx_t notify_rx)
284 struct rxrpc_conn_parameters cp;
285 struct rxrpc_call *call;
286 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
289 _enter(",,%x,%lx", key_serial(key), user_call_ID);
291 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
299 if (key && !key->payload.data[0])
300 key = NULL; /* a no-security key */
302 memset(&cp, 0, sizeof(cp));
303 cp.local = rx->local;
305 cp.security_level = 0;
306 cp.exclusive = false;
307 cp.service_id = srx->srx_service;
308 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, tx_total_len,
310 /* The socket has been unlocked. */
312 call->notify_rx = notify_rx;
314 mutex_unlock(&call->user_mutex);
315 _leave(" = %p", call);
318 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
321 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
322 * @sock: The socket the call is on
323 * @call: The call to end
325 * Allow a kernel service to end a call it was using. The call must be
326 * complete before this is called (the call should be aborted if necessary).
328 void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
330 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
332 mutex_lock(&call->user_mutex);
333 rxrpc_release_call(rxrpc_sk(sock->sk), call);
334 mutex_unlock(&call->user_mutex);
335 rxrpc_put_call(call, rxrpc_call_put_kernel);
337 EXPORT_SYMBOL(rxrpc_kernel_end_call);
340 * rxrpc_kernel_new_call_notification - Get notifications of new calls
341 * @sock: The socket to intercept received messages on
342 * @notify_new_call: Function to be called when new calls appear
343 * @discard_new_call: Function to discard preallocated calls
345 * Allow a kernel service to be given notifications about new calls.
347 void rxrpc_kernel_new_call_notification(
349 rxrpc_notify_new_call_t notify_new_call,
350 rxrpc_discard_new_call_t discard_new_call)
352 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
354 rx->notify_new_call = notify_new_call;
355 rx->discard_new_call = discard_new_call;
357 EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
360 * connect an RxRPC socket
361 * - this just targets it at a specific destination; no actual connection
362 * negotiation takes place
364 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
365 int addr_len, int flags)
367 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
368 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
371 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
373 ret = rxrpc_validate_address(rx, srx, addr_len);
375 _leave(" = %d [bad addr]", ret);
382 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
385 switch (rx->sk.sk_state) {
387 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
388 case RXRPC_CLIENT_UNBOUND:
389 case RXRPC_CLIENT_BOUND:
396 rx->connect_srx = *srx;
397 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
401 release_sock(&rx->sk);
406 * send a message through an RxRPC socket
407 * - in a client this does a number of things:
408 * - finds/sets up a connection for the security specified (if any)
409 * - initiates a call (ID in control data)
410 * - ends the request phase of a call (if MSG_MORE is not set)
411 * - sends a call data packet
412 * - may send an abort (abort code in control data)
414 static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
416 struct rxrpc_local *local;
417 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
420 _enter(",{%d},,%zu", rx->sk.sk_state, len);
422 if (m->msg_flags & MSG_OOB)
426 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
428 _leave(" = %d [bad addr]", ret);
435 switch (rx->sk.sk_state) {
437 rx->srx.srx_family = AF_RXRPC;
438 rx->srx.srx_service = 0;
439 rx->srx.transport_type = SOCK_DGRAM;
440 rx->srx.transport.family = rx->family;
441 switch (rx->family) {
443 rx->srx.transport_len = sizeof(struct sockaddr_in);
445 #ifdef CONFIG_AF_RXRPC_IPV6
447 rx->srx.transport_len = sizeof(struct sockaddr_in6);
454 local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
456 ret = PTR_ERR(local);
461 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
464 case RXRPC_CLIENT_UNBOUND:
465 case RXRPC_CLIENT_BOUND:
467 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
468 m->msg_name = &rx->connect_srx;
469 m->msg_namelen = sizeof(rx->connect_srx);
471 case RXRPC_SERVER_BOUND:
472 case RXRPC_SERVER_LISTENING:
473 ret = rxrpc_do_sendmsg(rx, m, len);
474 /* The socket has been unlocked */
482 release_sock(&rx->sk);
484 _leave(" = %d", ret);
489 * set RxRPC socket options
491 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
492 char __user *optval, unsigned int optlen)
494 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
495 unsigned int min_sec_level;
496 u16 service_upgrade[2];
499 _enter(",%d,%d,,%d", level, optname, optlen);
504 if (level == SOL_RXRPC) {
506 case RXRPC_EXCLUSIVE_CONNECTION:
511 if (rx->sk.sk_state != RXRPC_UNBOUND)
513 rx->exclusive = true;
516 case RXRPC_SECURITY_KEY:
521 if (rx->sk.sk_state != RXRPC_UNBOUND)
523 ret = rxrpc_request_key(rx, optval, optlen);
526 case RXRPC_SECURITY_KEYRING:
531 if (rx->sk.sk_state != RXRPC_UNBOUND)
533 ret = rxrpc_server_keyring(rx, optval, optlen);
536 case RXRPC_MIN_SECURITY_LEVEL:
538 if (optlen != sizeof(unsigned int))
541 if (rx->sk.sk_state != RXRPC_UNBOUND)
543 ret = get_user(min_sec_level,
544 (unsigned int __user *) optval);
548 if (min_sec_level > RXRPC_SECURITY_MAX)
550 rx->min_sec_level = min_sec_level;
553 case RXRPC_UPGRADEABLE_SERVICE:
555 if (optlen != sizeof(service_upgrade) ||
556 rx->service_upgrade.from != 0)
559 if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
562 if (copy_from_user(service_upgrade, optval,
563 sizeof(service_upgrade)) != 0)
566 if ((service_upgrade[0] != rx->srx.srx_service ||
567 service_upgrade[1] != rx->second_service) &&
568 (service_upgrade[0] != rx->second_service ||
569 service_upgrade[1] != rx->srx.srx_service))
571 rx->service_upgrade.from = service_upgrade[0];
572 rx->service_upgrade.to = service_upgrade[1];
583 release_sock(&rx->sk);
588 * Get socket options.
590 static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
591 char __user *optval, int __user *_optlen)
595 if (level != SOL_RXRPC)
598 if (get_user(optlen, _optlen))
602 case RXRPC_SUPPORTED_CMSG:
603 if (optlen < sizeof(int))
605 if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
606 put_user(sizeof(int), _optlen))
616 * permit an RxRPC socket to be polled
618 static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
621 struct sock *sk = sock->sk;
622 struct rxrpc_sock *rx = rxrpc_sk(sk);
625 sock_poll_wait(file, sk_sleep(sk), wait);
628 /* the socket is readable if there are any messages waiting on the Rx
630 if (!list_empty(&rx->recvmsg_q))
631 mask |= POLLIN | POLLRDNORM;
633 /* the socket is writable if there is space to add new data to the
634 * socket; there is no guarantee that any particular call in progress
635 * on the socket may have space in the Tx ACK window */
636 if (rxrpc_writable(sk))
637 mask |= POLLOUT | POLLWRNORM;
643 * create an RxRPC socket
645 static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
648 struct rxrpc_sock *rx;
651 _enter("%p,%d", sock, protocol);
653 /* we support transport protocol UDP/UDP6 only */
654 if (protocol != PF_INET &&
655 IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
656 return -EPROTONOSUPPORT;
658 if (sock->type != SOCK_DGRAM)
659 return -ESOCKTNOSUPPORT;
661 sock->ops = &rxrpc_rpc_ops;
662 sock->state = SS_UNCONNECTED;
664 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
668 sock_init_data(sock, sk);
669 sock_set_flag(sk, SOCK_RCU_FREE);
670 sk->sk_state = RXRPC_UNBOUND;
671 sk->sk_write_space = rxrpc_write_space;
672 sk->sk_max_ack_backlog = 0;
673 sk->sk_destruct = rxrpc_sock_destructor;
676 rx->family = protocol;
679 spin_lock_init(&rx->incoming_lock);
680 INIT_LIST_HEAD(&rx->sock_calls);
681 INIT_LIST_HEAD(&rx->to_be_accepted);
682 INIT_LIST_HEAD(&rx->recvmsg_q);
683 rwlock_init(&rx->recvmsg_lock);
684 rwlock_init(&rx->call_lock);
685 memset(&rx->srx, 0, sizeof(rx->srx));
687 _leave(" = 0 [%p]", rx);
692 * Kill all the calls on a socket and shut it down.
694 static int rxrpc_shutdown(struct socket *sock, int flags)
696 struct sock *sk = sock->sk;
697 struct rxrpc_sock *rx = rxrpc_sk(sk);
700 _enter("%p,%d", sk, flags);
702 if (flags != SHUT_RDWR)
704 if (sk->sk_state == RXRPC_CLOSE)
709 spin_lock_bh(&sk->sk_receive_queue.lock);
710 if (sk->sk_state < RXRPC_CLOSE) {
711 sk->sk_state = RXRPC_CLOSE;
712 sk->sk_shutdown = SHUTDOWN_MASK;
716 spin_unlock_bh(&sk->sk_receive_queue.lock);
718 rxrpc_discard_prealloc(rx);
725 * RxRPC socket destructor
727 static void rxrpc_sock_destructor(struct sock *sk)
731 rxrpc_purge_queue(&sk->sk_receive_queue);
733 WARN_ON(refcount_read(&sk->sk_wmem_alloc));
734 WARN_ON(!sk_unhashed(sk));
735 WARN_ON(sk->sk_socket);
737 if (!sock_flag(sk, SOCK_DEAD)) {
738 printk("Attempt to release alive rxrpc socket: %p\n", sk);
744 * release an RxRPC socket
746 static int rxrpc_release_sock(struct sock *sk)
748 struct rxrpc_sock *rx = rxrpc_sk(sk);
750 _enter("%p{%d,%d}", sk, sk->sk_state, refcount_read(&sk->sk_refcnt));
752 /* declare the socket closed for business */
754 sk->sk_shutdown = SHUTDOWN_MASK;
756 spin_lock_bh(&sk->sk_receive_queue.lock);
757 sk->sk_state = RXRPC_CLOSE;
758 spin_unlock_bh(&sk->sk_receive_queue.lock);
760 if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
761 write_lock(&rx->local->services_lock);
762 rcu_assign_pointer(rx->local->service, NULL);
763 write_unlock(&rx->local->services_lock);
766 /* try to flush out this socket */
767 rxrpc_discard_prealloc(rx);
768 rxrpc_release_calls_on_socket(rx);
769 flush_workqueue(rxrpc_workqueue);
770 rxrpc_purge_queue(&sk->sk_receive_queue);
772 rxrpc_put_local(rx->local);
776 key_put(rx->securities);
777 rx->securities = NULL;
785 * release an RxRPC BSD socket on close() or equivalent
787 static int rxrpc_release(struct socket *sock)
789 struct sock *sk = sock->sk;
791 _enter("%p{%p}", sock, sk);
798 return rxrpc_release_sock(sk);
802 * RxRPC network protocol
804 static const struct proto_ops rxrpc_rpc_ops = {
806 .owner = THIS_MODULE,
807 .release = rxrpc_release,
809 .connect = rxrpc_connect,
810 .socketpair = sock_no_socketpair,
811 .accept = sock_no_accept,
812 .getname = sock_no_getname,
814 .ioctl = sock_no_ioctl,
815 .listen = rxrpc_listen,
816 .shutdown = rxrpc_shutdown,
817 .setsockopt = rxrpc_setsockopt,
818 .getsockopt = rxrpc_getsockopt,
819 .sendmsg = rxrpc_sendmsg,
820 .recvmsg = rxrpc_recvmsg,
821 .mmap = sock_no_mmap,
822 .sendpage = sock_no_sendpage,
825 static struct proto rxrpc_proto = {
827 .owner = THIS_MODULE,
828 .obj_size = sizeof(struct rxrpc_sock),
829 .max_header = sizeof(struct rxrpc_wire_header),
832 static const struct net_proto_family rxrpc_family_ops = {
834 .create = rxrpc_create,
835 .owner = THIS_MODULE,
839 * initialise and register the RxRPC protocol
841 static int __init af_rxrpc_init(void)
846 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
848 get_random_bytes(&tmp, sizeof(tmp));
852 idr_set_cursor(&rxrpc_client_conn_ids, tmp);
855 rxrpc_call_jar = kmem_cache_create(
856 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
857 SLAB_HWCACHE_ALIGN, NULL);
858 if (!rxrpc_call_jar) {
859 pr_notice("Failed to allocate call jar\n");
863 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
864 if (!rxrpc_workqueue) {
865 pr_notice("Failed to allocate work queue\n");
866 goto error_work_queue;
869 ret = rxrpc_init_security();
871 pr_crit("Cannot initialise security\n");
875 ret = register_pernet_subsys(&rxrpc_net_ops);
879 ret = proto_register(&rxrpc_proto, 1);
881 pr_crit("Cannot register protocol\n");
885 ret = sock_register(&rxrpc_family_ops);
887 pr_crit("Cannot register socket family\n");
891 ret = register_key_type(&key_type_rxrpc);
893 pr_crit("Cannot register client key type\n");
897 ret = register_key_type(&key_type_rxrpc_s);
899 pr_crit("Cannot register server key type\n");
900 goto error_key_type_s;
903 ret = rxrpc_sysctl_init();
905 pr_crit("Cannot register sysctls\n");
912 unregister_key_type(&key_type_rxrpc_s);
914 unregister_key_type(&key_type_rxrpc);
916 sock_unregister(PF_RXRPC);
918 proto_unregister(&rxrpc_proto);
920 unregister_pernet_subsys(&rxrpc_net_ops);
922 rxrpc_exit_security();
924 destroy_workqueue(rxrpc_workqueue);
926 kmem_cache_destroy(rxrpc_call_jar);
932 * unregister the RxRPC protocol
934 static void __exit af_rxrpc_exit(void)
938 unregister_key_type(&key_type_rxrpc_s);
939 unregister_key_type(&key_type_rxrpc);
940 sock_unregister(PF_RXRPC);
941 proto_unregister(&rxrpc_proto);
942 unregister_pernet_subsys(&rxrpc_net_ops);
943 ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
944 ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
946 /* Make sure the local and peer records pinned by any dying connections
950 rxrpc_destroy_client_conn_ids();
952 destroy_workqueue(rxrpc_workqueue);
953 rxrpc_exit_security();
954 kmem_cache_destroy(rxrpc_call_jar);
958 module_init(af_rxrpc_init);
959 module_exit(af_rxrpc_exit);