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 atomic_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 * @gfp: The allocation constraints
266 * @notify_rx: Where to send notifications instead of socket queue
268 * Allow a kernel service to begin a call on the nominated socket. This just
269 * sets up all the internal tracking structures and allocates connection and
270 * call IDs as appropriate. The call to be used is returned.
272 * The default socket destination address and security may be overridden by
273 * supplying @srx and @key.
275 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
276 struct sockaddr_rxrpc *srx,
278 unsigned long user_call_ID,
280 rxrpc_notify_rx_t notify_rx)
282 struct rxrpc_conn_parameters cp;
283 struct rxrpc_call *call;
284 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
287 _enter(",,%x,%lx", key_serial(key), user_call_ID);
289 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
297 if (key && !key->payload.data[0])
298 key = NULL; /* a no-security key */
300 memset(&cp, 0, sizeof(cp));
301 cp.local = rx->local;
303 cp.security_level = 0;
304 cp.exclusive = false;
305 cp.service_id = srx->srx_service;
306 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp);
307 /* The socket has been unlocked. */
309 call->notify_rx = notify_rx;
311 mutex_unlock(&call->user_mutex);
312 _leave(" = %p", call);
315 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
318 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
319 * @sock: The socket the call is on
320 * @call: The call to end
322 * Allow a kernel service to end a call it was using. The call must be
323 * complete before this is called (the call should be aborted if necessary).
325 void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
327 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
329 mutex_lock(&call->user_mutex);
330 rxrpc_release_call(rxrpc_sk(sock->sk), call);
331 mutex_unlock(&call->user_mutex);
332 rxrpc_put_call(call, rxrpc_call_put_kernel);
334 EXPORT_SYMBOL(rxrpc_kernel_end_call);
337 * rxrpc_kernel_new_call_notification - Get notifications of new calls
338 * @sock: The socket to intercept received messages on
339 * @notify_new_call: Function to be called when new calls appear
340 * @discard_new_call: Function to discard preallocated calls
342 * Allow a kernel service to be given notifications about new calls.
344 void rxrpc_kernel_new_call_notification(
346 rxrpc_notify_new_call_t notify_new_call,
347 rxrpc_discard_new_call_t discard_new_call)
349 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
351 rx->notify_new_call = notify_new_call;
352 rx->discard_new_call = discard_new_call;
354 EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
357 * connect an RxRPC socket
358 * - this just targets it at a specific destination; no actual connection
359 * negotiation takes place
361 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
362 int addr_len, int flags)
364 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
365 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
368 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
370 ret = rxrpc_validate_address(rx, srx, addr_len);
372 _leave(" = %d [bad addr]", ret);
379 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
382 switch (rx->sk.sk_state) {
384 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
385 case RXRPC_CLIENT_UNBOUND:
386 case RXRPC_CLIENT_BOUND:
393 rx->connect_srx = *srx;
394 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
398 release_sock(&rx->sk);
403 * send a message through an RxRPC socket
404 * - in a client this does a number of things:
405 * - finds/sets up a connection for the security specified (if any)
406 * - initiates a call (ID in control data)
407 * - ends the request phase of a call (if MSG_MORE is not set)
408 * - sends a call data packet
409 * - may send an abort (abort code in control data)
411 static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
413 struct rxrpc_local *local;
414 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
417 _enter(",{%d},,%zu", rx->sk.sk_state, len);
419 if (m->msg_flags & MSG_OOB)
423 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
425 _leave(" = %d [bad addr]", ret);
432 switch (rx->sk.sk_state) {
434 rx->srx.srx_family = AF_RXRPC;
435 rx->srx.srx_service = 0;
436 rx->srx.transport_type = SOCK_DGRAM;
437 rx->srx.transport.family = rx->family;
438 switch (rx->family) {
440 rx->srx.transport_len = sizeof(struct sockaddr_in);
442 #ifdef CONFIG_AF_RXRPC_IPV6
444 rx->srx.transport_len = sizeof(struct sockaddr_in6);
451 local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
453 ret = PTR_ERR(local);
458 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
461 case RXRPC_CLIENT_UNBOUND:
462 case RXRPC_CLIENT_BOUND:
464 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
465 m->msg_name = &rx->connect_srx;
466 m->msg_namelen = sizeof(rx->connect_srx);
468 case RXRPC_SERVER_BOUND:
469 case RXRPC_SERVER_LISTENING:
470 ret = rxrpc_do_sendmsg(rx, m, len);
471 /* The socket has been unlocked */
479 release_sock(&rx->sk);
481 _leave(" = %d", ret);
486 * set RxRPC socket options
488 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
489 char __user *optval, unsigned int optlen)
491 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
492 unsigned int min_sec_level;
493 u16 service_upgrade[2];
496 _enter(",%d,%d,,%d", level, optname, optlen);
501 if (level == SOL_RXRPC) {
503 case RXRPC_EXCLUSIVE_CONNECTION:
508 if (rx->sk.sk_state != RXRPC_UNBOUND)
510 rx->exclusive = true;
513 case RXRPC_SECURITY_KEY:
518 if (rx->sk.sk_state != RXRPC_UNBOUND)
520 ret = rxrpc_request_key(rx, optval, optlen);
523 case RXRPC_SECURITY_KEYRING:
528 if (rx->sk.sk_state != RXRPC_UNBOUND)
530 ret = rxrpc_server_keyring(rx, optval, optlen);
533 case RXRPC_MIN_SECURITY_LEVEL:
535 if (optlen != sizeof(unsigned int))
538 if (rx->sk.sk_state != RXRPC_UNBOUND)
540 ret = get_user(min_sec_level,
541 (unsigned int __user *) optval);
545 if (min_sec_level > RXRPC_SECURITY_MAX)
547 rx->min_sec_level = min_sec_level;
550 case RXRPC_UPGRADEABLE_SERVICE:
552 if (optlen != sizeof(service_upgrade) ||
553 rx->service_upgrade.from != 0)
556 if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
559 if (copy_from_user(service_upgrade, optval,
560 sizeof(service_upgrade)) != 0)
563 if ((service_upgrade[0] != rx->srx.srx_service ||
564 service_upgrade[1] != rx->second_service) &&
565 (service_upgrade[0] != rx->second_service ||
566 service_upgrade[1] != rx->srx.srx_service))
568 rx->service_upgrade.from = service_upgrade[0];
569 rx->service_upgrade.to = service_upgrade[1];
580 release_sock(&rx->sk);
585 * Get socket options.
587 static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
588 char __user *optval, int __user *_optlen)
592 if (level != SOL_RXRPC)
595 if (get_user(optlen, _optlen))
599 case RXRPC_SUPPORTED_CMSG:
600 if (optlen < sizeof(int))
602 if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
603 put_user(sizeof(int), _optlen))
613 * permit an RxRPC socket to be polled
615 static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
618 struct sock *sk = sock->sk;
619 struct rxrpc_sock *rx = rxrpc_sk(sk);
622 sock_poll_wait(file, sk_sleep(sk), wait);
625 /* the socket is readable if there are any messages waiting on the Rx
627 if (!list_empty(&rx->recvmsg_q))
628 mask |= POLLIN | POLLRDNORM;
630 /* the socket is writable if there is space to add new data to the
631 * socket; there is no guarantee that any particular call in progress
632 * on the socket may have space in the Tx ACK window */
633 if (rxrpc_writable(sk))
634 mask |= POLLOUT | POLLWRNORM;
640 * create an RxRPC socket
642 static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
645 struct rxrpc_sock *rx;
648 _enter("%p,%d", sock, protocol);
650 /* we support transport protocol UDP/UDP6 only */
651 if (protocol != PF_INET &&
652 IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
653 return -EPROTONOSUPPORT;
655 if (sock->type != SOCK_DGRAM)
656 return -ESOCKTNOSUPPORT;
658 sock->ops = &rxrpc_rpc_ops;
659 sock->state = SS_UNCONNECTED;
661 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
665 sock_init_data(sock, sk);
666 sock_set_flag(sk, SOCK_RCU_FREE);
667 sk->sk_state = RXRPC_UNBOUND;
668 sk->sk_write_space = rxrpc_write_space;
669 sk->sk_max_ack_backlog = 0;
670 sk->sk_destruct = rxrpc_sock_destructor;
673 rx->family = protocol;
676 spin_lock_init(&rx->incoming_lock);
677 INIT_LIST_HEAD(&rx->sock_calls);
678 INIT_LIST_HEAD(&rx->to_be_accepted);
679 INIT_LIST_HEAD(&rx->recvmsg_q);
680 rwlock_init(&rx->recvmsg_lock);
681 rwlock_init(&rx->call_lock);
682 memset(&rx->srx, 0, sizeof(rx->srx));
684 _leave(" = 0 [%p]", rx);
689 * Kill all the calls on a socket and shut it down.
691 static int rxrpc_shutdown(struct socket *sock, int flags)
693 struct sock *sk = sock->sk;
694 struct rxrpc_sock *rx = rxrpc_sk(sk);
697 _enter("%p,%d", sk, flags);
699 if (flags != SHUT_RDWR)
701 if (sk->sk_state == RXRPC_CLOSE)
706 spin_lock_bh(&sk->sk_receive_queue.lock);
707 if (sk->sk_state < RXRPC_CLOSE) {
708 sk->sk_state = RXRPC_CLOSE;
709 sk->sk_shutdown = SHUTDOWN_MASK;
713 spin_unlock_bh(&sk->sk_receive_queue.lock);
715 rxrpc_discard_prealloc(rx);
722 * RxRPC socket destructor
724 static void rxrpc_sock_destructor(struct sock *sk)
728 rxrpc_purge_queue(&sk->sk_receive_queue);
730 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
731 WARN_ON(!sk_unhashed(sk));
732 WARN_ON(sk->sk_socket);
734 if (!sock_flag(sk, SOCK_DEAD)) {
735 printk("Attempt to release alive rxrpc socket: %p\n", sk);
741 * release an RxRPC socket
743 static int rxrpc_release_sock(struct sock *sk)
745 struct rxrpc_sock *rx = rxrpc_sk(sk);
747 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
749 /* declare the socket closed for business */
751 sk->sk_shutdown = SHUTDOWN_MASK;
753 spin_lock_bh(&sk->sk_receive_queue.lock);
754 sk->sk_state = RXRPC_CLOSE;
755 spin_unlock_bh(&sk->sk_receive_queue.lock);
757 if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
758 write_lock(&rx->local->services_lock);
759 rcu_assign_pointer(rx->local->service, NULL);
760 write_unlock(&rx->local->services_lock);
763 /* try to flush out this socket */
764 rxrpc_discard_prealloc(rx);
765 rxrpc_release_calls_on_socket(rx);
766 flush_workqueue(rxrpc_workqueue);
767 rxrpc_purge_queue(&sk->sk_receive_queue);
769 rxrpc_put_local(rx->local);
773 key_put(rx->securities);
774 rx->securities = NULL;
782 * release an RxRPC BSD socket on close() or equivalent
784 static int rxrpc_release(struct socket *sock)
786 struct sock *sk = sock->sk;
788 _enter("%p{%p}", sock, sk);
795 return rxrpc_release_sock(sk);
799 * RxRPC network protocol
801 static const struct proto_ops rxrpc_rpc_ops = {
803 .owner = THIS_MODULE,
804 .release = rxrpc_release,
806 .connect = rxrpc_connect,
807 .socketpair = sock_no_socketpair,
808 .accept = sock_no_accept,
809 .getname = sock_no_getname,
811 .ioctl = sock_no_ioctl,
812 .listen = rxrpc_listen,
813 .shutdown = rxrpc_shutdown,
814 .setsockopt = rxrpc_setsockopt,
815 .getsockopt = rxrpc_getsockopt,
816 .sendmsg = rxrpc_sendmsg,
817 .recvmsg = rxrpc_recvmsg,
818 .mmap = sock_no_mmap,
819 .sendpage = sock_no_sendpage,
822 static struct proto rxrpc_proto = {
824 .owner = THIS_MODULE,
825 .obj_size = sizeof(struct rxrpc_sock),
826 .max_header = sizeof(struct rxrpc_wire_header),
829 static const struct net_proto_family rxrpc_family_ops = {
831 .create = rxrpc_create,
832 .owner = THIS_MODULE,
836 * initialise and register the RxRPC protocol
838 static int __init af_rxrpc_init(void)
843 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
845 get_random_bytes(&tmp, sizeof(tmp));
849 idr_set_cursor(&rxrpc_client_conn_ids, tmp);
852 rxrpc_call_jar = kmem_cache_create(
853 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
854 SLAB_HWCACHE_ALIGN, NULL);
855 if (!rxrpc_call_jar) {
856 pr_notice("Failed to allocate call jar\n");
860 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
861 if (!rxrpc_workqueue) {
862 pr_notice("Failed to allocate work queue\n");
863 goto error_work_queue;
866 ret = rxrpc_init_security();
868 pr_crit("Cannot initialise security\n");
872 ret = register_pernet_subsys(&rxrpc_net_ops);
876 ret = proto_register(&rxrpc_proto, 1);
878 pr_crit("Cannot register protocol\n");
882 ret = sock_register(&rxrpc_family_ops);
884 pr_crit("Cannot register socket family\n");
888 ret = register_key_type(&key_type_rxrpc);
890 pr_crit("Cannot register client key type\n");
894 ret = register_key_type(&key_type_rxrpc_s);
896 pr_crit("Cannot register server key type\n");
897 goto error_key_type_s;
900 ret = rxrpc_sysctl_init();
902 pr_crit("Cannot register sysctls\n");
909 unregister_key_type(&key_type_rxrpc_s);
911 unregister_key_type(&key_type_rxrpc);
913 sock_unregister(PF_RXRPC);
915 proto_unregister(&rxrpc_proto);
917 unregister_pernet_subsys(&rxrpc_net_ops);
919 rxrpc_exit_security();
921 destroy_workqueue(rxrpc_workqueue);
923 kmem_cache_destroy(rxrpc_call_jar);
929 * unregister the RxRPC protocol
931 static void __exit af_rxrpc_exit(void)
935 unregister_key_type(&key_type_rxrpc_s);
936 unregister_key_type(&key_type_rxrpc);
937 sock_unregister(PF_RXRPC);
938 proto_unregister(&rxrpc_proto);
939 unregister_pernet_subsys(&rxrpc_net_ops);
940 ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
941 ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
943 /* Make sure the local and peer records pinned by any dying connections
947 rxrpc_destroy_client_conn_ids();
949 destroy_workqueue(rxrpc_workqueue);
950 rxrpc_exit_security();
951 kmem_cache_destroy(rxrpc_call_jar);
955 module_init(af_rxrpc_init);
956 module_exit(af_rxrpc_exit);