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 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/poll.h>
16 #include <linux/proc_fs.h>
18 #include <net/af_rxrpc.h>
19 #include "ar-internal.h"
21 MODULE_DESCRIPTION("RxRPC network protocol");
22 MODULE_AUTHOR("Red Hat, Inc.");
23 MODULE_LICENSE("GPL");
24 MODULE_ALIAS_NETPROTO(PF_RXRPC);
26 unsigned rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
27 module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
28 MODULE_PARM_DESC(rxrpc_debug, "RxRPC debugging mask");
30 static int sysctl_rxrpc_max_qlen __read_mostly = 10;
32 static struct proto rxrpc_proto;
33 static const struct proto_ops rxrpc_rpc_ops;
35 /* local epoch for detecting local-end reset */
38 /* current debugging ID */
39 atomic_t rxrpc_debug_id;
41 /* count of skbs currently in use */
42 atomic_t rxrpc_n_skbs;
44 struct workqueue_struct *rxrpc_workqueue;
46 static void rxrpc_sock_destructor(struct sock *);
49 * see if an RxRPC socket is currently writable
51 static inline int rxrpc_writable(struct sock *sk)
53 return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
57 * wait for write bufferage to become available
59 static void rxrpc_write_space(struct sock *sk)
62 read_lock(&sk->sk_callback_lock);
63 if (rxrpc_writable(sk)) {
64 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
65 wake_up_interruptible(sk->sk_sleep);
66 sk_wake_async(sk, 2, POLL_OUT);
68 read_unlock(&sk->sk_callback_lock);
72 * validate an RxRPC address
74 static int rxrpc_validate_address(struct rxrpc_sock *rx,
75 struct sockaddr_rxrpc *srx,
78 if (len < sizeof(struct sockaddr_rxrpc))
81 if (srx->srx_family != AF_RXRPC)
84 if (srx->transport_type != SOCK_DGRAM)
85 return -ESOCKTNOSUPPORT;
87 len -= offsetof(struct sockaddr_rxrpc, transport);
88 if (srx->transport_len < sizeof(sa_family_t) ||
89 srx->transport_len > len)
92 if (srx->transport.family != rx->proto)
95 switch (srx->transport.family) {
97 _debug("INET: %x @ %u.%u.%u.%u",
98 ntohs(srx->transport.sin.sin_port),
99 NIPQUAD(srx->transport.sin.sin_addr));
100 if (srx->transport_len > 8)
101 memset((void *)&srx->transport + 8, 0,
102 srx->transport_len - 8);
107 return -EAFNOSUPPORT;
114 * bind a local address to an RxRPC socket
116 static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
118 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) saddr;
119 struct sock *sk = sock->sk;
120 struct rxrpc_local *local;
121 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
125 _enter("%p,%p,%d", rx, saddr, len);
127 ret = rxrpc_validate_address(rx, srx, len);
133 if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
138 memcpy(&rx->srx, srx, sizeof(rx->srx));
140 /* find a local transport endpoint if we don't have one already */
141 local = rxrpc_lookup_local(&rx->srx);
143 ret = PTR_ERR(local);
148 if (srx->srx_service) {
149 service_id = htons(srx->srx_service);
150 write_lock_bh(&local->services_lock);
151 list_for_each_entry(prx, &local->services, listen_link) {
152 if (prx->service_id == service_id)
156 rx->service_id = service_id;
157 list_add_tail(&rx->listen_link, &local->services);
158 write_unlock_bh(&local->services_lock);
160 rx->sk.sk_state = RXRPC_SERVER_BOUND;
162 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
165 release_sock(&rx->sk);
171 write_unlock_bh(&local->services_lock);
173 release_sock(&rx->sk);
175 _leave(" = %d", ret);
180 * set the number of pending calls permitted on a listening socket
182 static int rxrpc_listen(struct socket *sock, int backlog)
184 struct sock *sk = sock->sk;
185 struct rxrpc_sock *rx = rxrpc_sk(sk);
188 _enter("%p,%d", rx, backlog);
192 switch (rx->sk.sk_state) {
193 case RXRPC_UNCONNECTED:
194 ret = -EADDRNOTAVAIL;
196 case RXRPC_CLIENT_BOUND:
197 case RXRPC_CLIENT_CONNECTED:
201 case RXRPC_SERVER_BOUND:
202 ASSERT(rx->local != NULL);
203 sk->sk_max_ack_backlog = backlog;
204 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
209 release_sock(&rx->sk);
210 _leave(" = %d", ret);
215 * find a transport by address
217 static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
218 struct sockaddr *addr,
219 int addr_len, int flags,
222 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
223 struct rxrpc_transport *trans;
224 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
225 struct rxrpc_peer *peer;
227 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
229 ASSERT(rx->local != NULL);
230 ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
232 if (rx->srx.transport_type != srx->transport_type)
233 return ERR_PTR(-ESOCKTNOSUPPORT);
234 if (rx->srx.transport.family != srx->transport.family)
235 return ERR_PTR(-EAFNOSUPPORT);
237 /* find a remote transport endpoint from the local one */
238 peer = rxrpc_get_peer(srx, gfp);
240 return ERR_PTR(PTR_ERR(peer));
242 /* find a transport */
243 trans = rxrpc_get_transport(rx->local, peer, gfp);
244 rxrpc_put_peer(peer);
245 _leave(" = %p", trans);
250 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
251 * @sock: The socket on which to make the call
252 * @srx: The address of the peer to contact (defaults to socket setting)
253 * @key: The security context to use (defaults to socket setting)
254 * @user_call_ID: The ID to use
256 * Allow a kernel service to begin a call on the nominated socket. This just
257 * sets up all the internal tracking structures and allocates connection and
258 * call IDs as appropriate. The call to be used is returned.
260 * The default socket destination address and security may be overridden by
261 * supplying @srx and @key.
263 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
264 struct sockaddr_rxrpc *srx,
266 unsigned long user_call_ID,
269 struct rxrpc_conn_bundle *bundle;
270 struct rxrpc_transport *trans;
271 struct rxrpc_call *call;
272 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
275 _enter(",,%x,%lx", key_serial(key), user_call_ID);
280 trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
281 sizeof(*srx), 0, gfp);
283 call = ERR_PTR(PTR_ERR(trans));
290 call = ERR_PTR(-ENOTCONN);
293 atomic_inc(&trans->usage);
296 service_id = rx->service_id;
298 service_id = htons(srx->srx_service);
302 if (key && !key->payload.data)
303 key = NULL; /* a no-security key */
305 bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp);
306 if (IS_ERR(bundle)) {
307 call = ERR_PTR(PTR_ERR(bundle));
311 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
313 rxrpc_put_bundle(trans, bundle);
315 rxrpc_put_transport(trans);
316 release_sock(&rx->sk);
317 _leave(" = %p", call);
321 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
324 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
325 * @call: The call to end
327 * Allow a kernel service to end a call it was using. The call must be
328 * complete before this is called (the call should be aborted if necessary).
330 void rxrpc_kernel_end_call(struct rxrpc_call *call)
332 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
333 rxrpc_remove_user_ID(call->socket, call);
334 rxrpc_put_call(call);
337 EXPORT_SYMBOL(rxrpc_kernel_end_call);
340 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
341 * @sock: The socket to intercept received messages on
342 * @interceptor: The function to pass the messages to
344 * Allow a kernel service to intercept messages heading for the Rx queue on an
345 * RxRPC socket. They get passed to the specified function instead.
346 * @interceptor should free the socket buffers it is given. @interceptor is
347 * called with the socket receive queue spinlock held and softirqs disabled -
348 * this ensures that the messages will be delivered in the right order.
350 void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
351 rxrpc_interceptor_t interceptor)
353 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
356 rx->interceptor = interceptor;
359 EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
362 * connect an RxRPC socket
363 * - this just targets it at a specific destination; no actual connection
364 * negotiation takes place
366 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
367 int addr_len, int flags)
369 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
370 struct sock *sk = sock->sk;
371 struct rxrpc_transport *trans;
372 struct rxrpc_local *local;
373 struct rxrpc_sock *rx = rxrpc_sk(sk);
376 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
378 ret = rxrpc_validate_address(rx, srx, addr_len);
380 _leave(" = %d [bad addr]", ret);
386 switch (rx->sk.sk_state) {
387 case RXRPC_UNCONNECTED:
388 /* find a local transport endpoint if we don't have one already */
389 ASSERTCMP(rx->local, ==, NULL);
390 rx->srx.srx_family = AF_RXRPC;
391 rx->srx.srx_service = 0;
392 rx->srx.transport_type = srx->transport_type;
393 rx->srx.transport_len = sizeof(sa_family_t);
394 rx->srx.transport.family = srx->transport.family;
395 local = rxrpc_lookup_local(&rx->srx);
397 release_sock(&rx->sk);
398 return PTR_ERR(local);
401 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
402 case RXRPC_CLIENT_BOUND:
404 case RXRPC_CLIENT_CONNECTED:
405 release_sock(&rx->sk);
408 release_sock(&rx->sk);
409 return -EBUSY; /* server sockets can't connect as well */
412 trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
415 release_sock(&rx->sk);
416 _leave(" = %ld", PTR_ERR(trans));
417 return PTR_ERR(trans);
421 rx->service_id = htons(srx->srx_service);
422 rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
424 release_sock(&rx->sk);
429 * send a message through an RxRPC socket
430 * - in a client this does a number of things:
431 * - finds/sets up a connection for the security specified (if any)
432 * - initiates a call (ID in control data)
433 * - ends the request phase of a call (if MSG_MORE is not set)
434 * - sends a call data packet
435 * - may send an abort (abort code in control data)
437 static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
438 struct msghdr *m, size_t len)
440 struct rxrpc_transport *trans;
441 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
444 _enter(",{%d},,%zu", rx->sk.sk_state, len);
446 if (m->msg_flags & MSG_OOB)
450 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
452 _leave(" = %d [bad addr]", ret);
462 trans = rxrpc_name_to_transport(sock, m->msg_name,
463 m->msg_namelen, 0, GFP_KERNEL);
465 ret = PTR_ERR(trans);
472 atomic_inc(&trans->usage);
475 switch (rx->sk.sk_state) {
476 case RXRPC_SERVER_LISTENING:
478 ret = rxrpc_server_sendmsg(iocb, rx, m, len);
481 case RXRPC_SERVER_BOUND:
482 case RXRPC_CLIENT_BOUND:
487 case RXRPC_CLIENT_CONNECTED:
488 ret = rxrpc_client_sendmsg(iocb, rx, trans, m, len);
496 release_sock(&rx->sk);
498 rxrpc_put_transport(trans);
499 _leave(" = %d", ret);
504 * set RxRPC socket options
506 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
507 char __user *optval, int optlen)
509 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
510 unsigned min_sec_level;
513 _enter(",%d,%d,,%d", level, optname, optlen);
518 if (level == SOL_RXRPC) {
520 case RXRPC_EXCLUSIVE_CONNECTION:
525 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
527 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
530 case RXRPC_SECURITY_KEY:
535 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
537 ret = rxrpc_request_key(rx, optval, optlen);
540 case RXRPC_SECURITY_KEYRING:
545 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
547 ret = rxrpc_server_keyring(rx, optval, optlen);
550 case RXRPC_MIN_SECURITY_LEVEL:
552 if (optlen != sizeof(unsigned))
555 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
557 ret = get_user(min_sec_level,
558 (unsigned __user *) optval);
562 if (min_sec_level > RXRPC_SECURITY_MAX)
564 rx->min_sec_level = min_sec_level;
575 release_sock(&rx->sk);
580 * permit an RxRPC socket to be polled
582 static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
586 struct sock *sk = sock->sk;
588 poll_wait(file, sk->sk_sleep, wait);
591 /* the socket is readable if there are any messages waiting on the Rx
593 if (!skb_queue_empty(&sk->sk_receive_queue))
594 mask |= POLLIN | POLLRDNORM;
596 /* the socket is writable if there is space to add new data to the
597 * socket; there is no guarantee that any particular call in progress
598 * on the socket may have space in the Tx ACK window */
599 if (rxrpc_writable(sk))
600 mask |= POLLOUT | POLLWRNORM;
606 * create an RxRPC socket
608 static int rxrpc_create(struct socket *sock, int protocol)
610 struct rxrpc_sock *rx;
613 _enter("%p,%d", sock, protocol);
615 /* we support transport protocol UDP only */
616 if (protocol != PF_INET)
617 return -EPROTONOSUPPORT;
619 if (sock->type != SOCK_DGRAM)
620 return -ESOCKTNOSUPPORT;
622 sock->ops = &rxrpc_rpc_ops;
623 sock->state = SS_UNCONNECTED;
625 sk = sk_alloc(PF_RXRPC, GFP_KERNEL, &rxrpc_proto, 1);
629 sock_init_data(sock, sk);
630 sk->sk_state = RXRPC_UNCONNECTED;
631 sk->sk_write_space = rxrpc_write_space;
632 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
633 sk->sk_destruct = rxrpc_sock_destructor;
636 rx->proto = protocol;
639 INIT_LIST_HEAD(&rx->listen_link);
640 INIT_LIST_HEAD(&rx->secureq);
641 INIT_LIST_HEAD(&rx->acceptq);
642 rwlock_init(&rx->call_lock);
643 memset(&rx->srx, 0, sizeof(rx->srx));
645 _leave(" = 0 [%p]", rx);
650 * RxRPC socket destructor
652 static void rxrpc_sock_destructor(struct sock *sk)
656 rxrpc_purge_queue(&sk->sk_receive_queue);
658 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
659 BUG_TRAP(sk_unhashed(sk));
660 BUG_TRAP(!sk->sk_socket);
662 if (!sock_flag(sk, SOCK_DEAD)) {
663 printk("Attempt to release alive rxrpc socket: %p\n", sk);
669 * release an RxRPC socket
671 static int rxrpc_release_sock(struct sock *sk)
673 struct rxrpc_sock *rx = rxrpc_sk(sk);
675 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
677 /* declare the socket closed for business */
679 sk->sk_shutdown = SHUTDOWN_MASK;
681 spin_lock_bh(&sk->sk_receive_queue.lock);
682 sk->sk_state = RXRPC_CLOSE;
683 spin_unlock_bh(&sk->sk_receive_queue.lock);
685 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
687 if (!list_empty(&rx->listen_link)) {
688 write_lock_bh(&rx->local->services_lock);
689 list_del(&rx->listen_link);
690 write_unlock_bh(&rx->local->services_lock);
693 /* try to flush out this socket */
694 rxrpc_release_calls_on_socket(rx);
695 flush_workqueue(rxrpc_workqueue);
696 rxrpc_purge_queue(&sk->sk_receive_queue);
699 rxrpc_put_connection(rx->conn);
704 rxrpc_put_bundle(rx->trans, rx->bundle);
708 rxrpc_put_transport(rx->trans);
712 rxrpc_put_local(rx->local);
718 key_put(rx->securities);
719 rx->securities = NULL;
727 * release an RxRPC BSD socket on close() or equivalent
729 static int rxrpc_release(struct socket *sock)
731 struct sock *sk = sock->sk;
733 _enter("%p{%p}", sock, sk);
740 return rxrpc_release_sock(sk);
744 * RxRPC network protocol
746 static const struct proto_ops rxrpc_rpc_ops = {
748 .owner = THIS_MODULE,
749 .release = rxrpc_release,
751 .connect = rxrpc_connect,
752 .socketpair = sock_no_socketpair,
753 .accept = sock_no_accept,
754 .getname = sock_no_getname,
756 .ioctl = sock_no_ioctl,
757 .listen = rxrpc_listen,
758 .shutdown = sock_no_shutdown,
759 .setsockopt = rxrpc_setsockopt,
760 .getsockopt = sock_no_getsockopt,
761 .sendmsg = rxrpc_sendmsg,
762 .recvmsg = rxrpc_recvmsg,
763 .mmap = sock_no_mmap,
764 .sendpage = sock_no_sendpage,
767 static struct proto rxrpc_proto = {
769 .owner = THIS_MODULE,
770 .obj_size = sizeof(struct rxrpc_sock),
771 .max_header = sizeof(struct rxrpc_header),
774 static struct net_proto_family rxrpc_family_ops = {
776 .create = rxrpc_create,
777 .owner = THIS_MODULE,
781 * initialise and register the RxRPC protocol
783 static int __init af_rxrpc_init(void)
785 struct sk_buff *dummy_skb;
788 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof(dummy_skb->cb));
790 rxrpc_epoch = htonl(xtime.tv_sec);
793 rxrpc_call_jar = kmem_cache_create(
794 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
795 SLAB_HWCACHE_ALIGN, NULL);
796 if (!rxrpc_call_jar) {
797 printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n");
801 rxrpc_workqueue = create_workqueue("krxrpcd");
802 if (!rxrpc_workqueue) {
803 printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n");
804 goto error_work_queue;
807 ret = proto_register(&rxrpc_proto, 1);
809 printk(KERN_CRIT "RxRPC: Cannot register protocol\n");
813 ret = sock_register(&rxrpc_family_ops);
815 printk(KERN_CRIT "RxRPC: Cannot register socket family\n");
819 ret = register_key_type(&key_type_rxrpc);
821 printk(KERN_CRIT "RxRPC: Cannot register client key type\n");
825 ret = register_key_type(&key_type_rxrpc_s);
827 printk(KERN_CRIT "RxRPC: Cannot register server key type\n");
828 goto error_key_type_s;
831 #ifdef CONFIG_PROC_FS
832 proc_net_fops_create("rxrpc_calls", 0, &rxrpc_call_seq_fops);
833 proc_net_fops_create("rxrpc_conns", 0, &rxrpc_connection_seq_fops);
838 unregister_key_type(&key_type_rxrpc);
840 sock_unregister(PF_RXRPC);
842 proto_unregister(&rxrpc_proto);
844 destroy_workqueue(rxrpc_workqueue);
846 kmem_cache_destroy(rxrpc_call_jar);
852 * unregister the RxRPC protocol
854 static void __exit af_rxrpc_exit(void)
857 unregister_key_type(&key_type_rxrpc_s);
858 unregister_key_type(&key_type_rxrpc);
859 sock_unregister(PF_RXRPC);
860 proto_unregister(&rxrpc_proto);
861 rxrpc_destroy_all_calls();
862 rxrpc_destroy_all_connections();
863 rxrpc_destroy_all_transports();
864 rxrpc_destroy_all_peers();
865 rxrpc_destroy_all_locals();
867 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
869 _debug("flush scheduled work");
870 flush_workqueue(rxrpc_workqueue);
871 proc_net_remove("rxrpc_conns");
872 proc_net_remove("rxrpc_calls");
873 destroy_workqueue(rxrpc_workqueue);
874 kmem_cache_destroy(rxrpc_call_jar);
878 module_init(af_rxrpc_init);
879 module_exit(af_rxrpc_exit);