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>
17 #include <net/net_namespace.h>
19 #include <net/af_rxrpc.h>
20 #include "ar-internal.h"
22 MODULE_DESCRIPTION("RxRPC network protocol");
23 MODULE_AUTHOR("Red Hat, Inc.");
24 MODULE_LICENSE("GPL");
25 MODULE_ALIAS_NETPROTO(PF_RXRPC);
27 unsigned rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
28 module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
29 MODULE_PARM_DESC(rxrpc_debug, "RxRPC debugging mask");
31 static int sysctl_rxrpc_max_qlen __read_mostly = 10;
33 static struct proto rxrpc_proto;
34 static const struct proto_ops rxrpc_rpc_ops;
36 /* local epoch for detecting local-end reset */
39 /* current debugging ID */
40 atomic_t rxrpc_debug_id;
42 /* count of skbs currently in use */
43 atomic_t rxrpc_n_skbs;
45 struct workqueue_struct *rxrpc_workqueue;
47 static void rxrpc_sock_destructor(struct sock *);
50 * see if an RxRPC socket is currently writable
52 static inline int rxrpc_writable(struct sock *sk)
54 return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
58 * wait for write bufferage to become available
60 static void rxrpc_write_space(struct sock *sk)
63 read_lock(&sk->sk_callback_lock);
64 if (rxrpc_writable(sk)) {
65 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
66 wake_up_interruptible(sk->sk_sleep);
67 sk_wake_async(sk, 2, POLL_OUT);
69 read_unlock(&sk->sk_callback_lock);
73 * validate an RxRPC address
75 static int rxrpc_validate_address(struct rxrpc_sock *rx,
76 struct sockaddr_rxrpc *srx,
79 if (len < sizeof(struct sockaddr_rxrpc))
82 if (srx->srx_family != AF_RXRPC)
85 if (srx->transport_type != SOCK_DGRAM)
86 return -ESOCKTNOSUPPORT;
88 len -= offsetof(struct sockaddr_rxrpc, transport);
89 if (srx->transport_len < sizeof(sa_family_t) ||
90 srx->transport_len > len)
93 if (srx->transport.family != rx->proto)
96 switch (srx->transport.family) {
98 _debug("INET: %x @ %u.%u.%u.%u",
99 ntohs(srx->transport.sin.sin_port),
100 NIPQUAD(srx->transport.sin.sin_addr));
101 if (srx->transport_len > 8)
102 memset((void *)&srx->transport + 8, 0,
103 srx->transport_len - 8);
108 return -EAFNOSUPPORT;
115 * bind a local address to an RxRPC socket
117 static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
119 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) saddr;
120 struct sock *sk = sock->sk;
121 struct rxrpc_local *local;
122 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
126 _enter("%p,%p,%d", rx, saddr, len);
128 ret = rxrpc_validate_address(rx, srx, len);
134 if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
139 memcpy(&rx->srx, srx, sizeof(rx->srx));
141 /* find a local transport endpoint if we don't have one already */
142 local = rxrpc_lookup_local(&rx->srx);
144 ret = PTR_ERR(local);
149 if (srx->srx_service) {
150 service_id = htons(srx->srx_service);
151 write_lock_bh(&local->services_lock);
152 list_for_each_entry(prx, &local->services, listen_link) {
153 if (prx->service_id == service_id)
157 rx->service_id = service_id;
158 list_add_tail(&rx->listen_link, &local->services);
159 write_unlock_bh(&local->services_lock);
161 rx->sk.sk_state = RXRPC_SERVER_BOUND;
163 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
166 release_sock(&rx->sk);
172 write_unlock_bh(&local->services_lock);
174 release_sock(&rx->sk);
176 _leave(" = %d", ret);
181 * set the number of pending calls permitted on a listening socket
183 static int rxrpc_listen(struct socket *sock, int backlog)
185 struct sock *sk = sock->sk;
186 struct rxrpc_sock *rx = rxrpc_sk(sk);
189 _enter("%p,%d", rx, backlog);
193 switch (rx->sk.sk_state) {
194 case RXRPC_UNCONNECTED:
195 ret = -EADDRNOTAVAIL;
197 case RXRPC_CLIENT_BOUND:
198 case RXRPC_CLIENT_CONNECTED:
202 case RXRPC_SERVER_BOUND:
203 ASSERT(rx->local != NULL);
204 sk->sk_max_ack_backlog = backlog;
205 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
210 release_sock(&rx->sk);
211 _leave(" = %d", ret);
216 * find a transport by address
218 static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
219 struct sockaddr *addr,
220 int addr_len, int flags,
223 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
224 struct rxrpc_transport *trans;
225 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
226 struct rxrpc_peer *peer;
228 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
230 ASSERT(rx->local != NULL);
231 ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
233 if (rx->srx.transport_type != srx->transport_type)
234 return ERR_PTR(-ESOCKTNOSUPPORT);
235 if (rx->srx.transport.family != srx->transport.family)
236 return ERR_PTR(-EAFNOSUPPORT);
238 /* find a remote transport endpoint from the local one */
239 peer = rxrpc_get_peer(srx, gfp);
241 return ERR_PTR(PTR_ERR(peer));
243 /* find a transport */
244 trans = rxrpc_get_transport(rx->local, peer, gfp);
245 rxrpc_put_peer(peer);
246 _leave(" = %p", trans);
251 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
252 * @sock: The socket on which to make the call
253 * @srx: The address of the peer to contact (defaults to socket setting)
254 * @key: The security context to use (defaults to socket setting)
255 * @user_call_ID: The ID to use
257 * Allow a kernel service to begin a call on the nominated socket. This just
258 * sets up all the internal tracking structures and allocates connection and
259 * call IDs as appropriate. The call to be used is returned.
261 * The default socket destination address and security may be overridden by
262 * supplying @srx and @key.
264 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
265 struct sockaddr_rxrpc *srx,
267 unsigned long user_call_ID,
270 struct rxrpc_conn_bundle *bundle;
271 struct rxrpc_transport *trans;
272 struct rxrpc_call *call;
273 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
276 _enter(",,%x,%lx", key_serial(key), user_call_ID);
281 trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
282 sizeof(*srx), 0, gfp);
284 call = ERR_PTR(PTR_ERR(trans));
291 call = ERR_PTR(-ENOTCONN);
294 atomic_inc(&trans->usage);
297 service_id = rx->service_id;
299 service_id = htons(srx->srx_service);
303 if (key && !key->payload.data)
304 key = NULL; /* a no-security key */
306 bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp);
307 if (IS_ERR(bundle)) {
308 call = ERR_PTR(PTR_ERR(bundle));
312 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
314 rxrpc_put_bundle(trans, bundle);
316 rxrpc_put_transport(trans);
317 release_sock(&rx->sk);
318 _leave(" = %p", call);
322 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
325 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
326 * @call: The call to end
328 * Allow a kernel service to end a call it was using. The call must be
329 * complete before this is called (the call should be aborted if necessary).
331 void rxrpc_kernel_end_call(struct rxrpc_call *call)
333 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
334 rxrpc_remove_user_ID(call->socket, call);
335 rxrpc_put_call(call);
338 EXPORT_SYMBOL(rxrpc_kernel_end_call);
341 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
342 * @sock: The socket to intercept received messages on
343 * @interceptor: The function to pass the messages to
345 * Allow a kernel service to intercept messages heading for the Rx queue on an
346 * RxRPC socket. They get passed to the specified function instead.
347 * @interceptor should free the socket buffers it is given. @interceptor is
348 * called with the socket receive queue spinlock held and softirqs disabled -
349 * this ensures that the messages will be delivered in the right order.
351 void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
352 rxrpc_interceptor_t interceptor)
354 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
357 rx->interceptor = interceptor;
360 EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
363 * connect an RxRPC socket
364 * - this just targets it at a specific destination; no actual connection
365 * negotiation takes place
367 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
368 int addr_len, int flags)
370 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
371 struct sock *sk = sock->sk;
372 struct rxrpc_transport *trans;
373 struct rxrpc_local *local;
374 struct rxrpc_sock *rx = rxrpc_sk(sk);
377 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
379 ret = rxrpc_validate_address(rx, srx, addr_len);
381 _leave(" = %d [bad addr]", ret);
387 switch (rx->sk.sk_state) {
388 case RXRPC_UNCONNECTED:
389 /* find a local transport endpoint if we don't have one already */
390 ASSERTCMP(rx->local, ==, NULL);
391 rx->srx.srx_family = AF_RXRPC;
392 rx->srx.srx_service = 0;
393 rx->srx.transport_type = srx->transport_type;
394 rx->srx.transport_len = sizeof(sa_family_t);
395 rx->srx.transport.family = srx->transport.family;
396 local = rxrpc_lookup_local(&rx->srx);
398 release_sock(&rx->sk);
399 return PTR_ERR(local);
402 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
403 case RXRPC_CLIENT_BOUND:
405 case RXRPC_CLIENT_CONNECTED:
406 release_sock(&rx->sk);
409 release_sock(&rx->sk);
410 return -EBUSY; /* server sockets can't connect as well */
413 trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
416 release_sock(&rx->sk);
417 _leave(" = %ld", PTR_ERR(trans));
418 return PTR_ERR(trans);
422 rx->service_id = htons(srx->srx_service);
423 rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
425 release_sock(&rx->sk);
430 * send a message through an RxRPC socket
431 * - in a client this does a number of things:
432 * - finds/sets up a connection for the security specified (if any)
433 * - initiates a call (ID in control data)
434 * - ends the request phase of a call (if MSG_MORE is not set)
435 * - sends a call data packet
436 * - may send an abort (abort code in control data)
438 static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
439 struct msghdr *m, size_t len)
441 struct rxrpc_transport *trans;
442 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
445 _enter(",{%d},,%zu", rx->sk.sk_state, len);
447 if (m->msg_flags & MSG_OOB)
451 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
453 _leave(" = %d [bad addr]", ret);
463 trans = rxrpc_name_to_transport(sock, m->msg_name,
464 m->msg_namelen, 0, GFP_KERNEL);
466 ret = PTR_ERR(trans);
473 atomic_inc(&trans->usage);
476 switch (rx->sk.sk_state) {
477 case RXRPC_SERVER_LISTENING:
479 ret = rxrpc_server_sendmsg(iocb, rx, m, len);
482 case RXRPC_SERVER_BOUND:
483 case RXRPC_CLIENT_BOUND:
488 case RXRPC_CLIENT_CONNECTED:
489 ret = rxrpc_client_sendmsg(iocb, rx, trans, m, len);
497 release_sock(&rx->sk);
499 rxrpc_put_transport(trans);
500 _leave(" = %d", ret);
505 * set RxRPC socket options
507 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
508 char __user *optval, int optlen)
510 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
511 unsigned min_sec_level;
514 _enter(",%d,%d,,%d", level, optname, optlen);
519 if (level == SOL_RXRPC) {
521 case RXRPC_EXCLUSIVE_CONNECTION:
526 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
528 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
531 case RXRPC_SECURITY_KEY:
536 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
538 ret = rxrpc_request_key(rx, optval, optlen);
541 case RXRPC_SECURITY_KEYRING:
546 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
548 ret = rxrpc_server_keyring(rx, optval, optlen);
551 case RXRPC_MIN_SECURITY_LEVEL:
553 if (optlen != sizeof(unsigned))
556 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
558 ret = get_user(min_sec_level,
559 (unsigned __user *) optval);
563 if (min_sec_level > RXRPC_SECURITY_MAX)
565 rx->min_sec_level = min_sec_level;
576 release_sock(&rx->sk);
581 * permit an RxRPC socket to be polled
583 static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
587 struct sock *sk = sock->sk;
589 poll_wait(file, sk->sk_sleep, wait);
592 /* the socket is readable if there are any messages waiting on the Rx
594 if (!skb_queue_empty(&sk->sk_receive_queue))
595 mask |= POLLIN | POLLRDNORM;
597 /* the socket is writable if there is space to add new data to the
598 * socket; there is no guarantee that any particular call in progress
599 * on the socket may have space in the Tx ACK window */
600 if (rxrpc_writable(sk))
601 mask |= POLLOUT | POLLWRNORM;
607 * create an RxRPC socket
609 static int rxrpc_create(struct net *net, struct socket *sock, int protocol)
611 struct rxrpc_sock *rx;
614 _enter("%p,%d", sock, protocol);
616 if (net != &init_net)
617 return -EAFNOSUPPORT;
619 /* we support transport protocol UDP only */
620 if (protocol != PF_INET)
621 return -EPROTONOSUPPORT;
623 if (sock->type != SOCK_DGRAM)
624 return -ESOCKTNOSUPPORT;
626 sock->ops = &rxrpc_rpc_ops;
627 sock->state = SS_UNCONNECTED;
629 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, 1);
633 sock_init_data(sock, sk);
634 sk->sk_state = RXRPC_UNCONNECTED;
635 sk->sk_write_space = rxrpc_write_space;
636 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
637 sk->sk_destruct = rxrpc_sock_destructor;
640 rx->proto = protocol;
643 INIT_LIST_HEAD(&rx->listen_link);
644 INIT_LIST_HEAD(&rx->secureq);
645 INIT_LIST_HEAD(&rx->acceptq);
646 rwlock_init(&rx->call_lock);
647 memset(&rx->srx, 0, sizeof(rx->srx));
649 _leave(" = 0 [%p]", rx);
654 * RxRPC socket destructor
656 static void rxrpc_sock_destructor(struct sock *sk)
660 rxrpc_purge_queue(&sk->sk_receive_queue);
662 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
663 BUG_TRAP(sk_unhashed(sk));
664 BUG_TRAP(!sk->sk_socket);
666 if (!sock_flag(sk, SOCK_DEAD)) {
667 printk("Attempt to release alive rxrpc socket: %p\n", sk);
673 * release an RxRPC socket
675 static int rxrpc_release_sock(struct sock *sk)
677 struct rxrpc_sock *rx = rxrpc_sk(sk);
679 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
681 /* declare the socket closed for business */
683 sk->sk_shutdown = SHUTDOWN_MASK;
685 spin_lock_bh(&sk->sk_receive_queue.lock);
686 sk->sk_state = RXRPC_CLOSE;
687 spin_unlock_bh(&sk->sk_receive_queue.lock);
689 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
691 if (!list_empty(&rx->listen_link)) {
692 write_lock_bh(&rx->local->services_lock);
693 list_del(&rx->listen_link);
694 write_unlock_bh(&rx->local->services_lock);
697 /* try to flush out this socket */
698 rxrpc_release_calls_on_socket(rx);
699 flush_workqueue(rxrpc_workqueue);
700 rxrpc_purge_queue(&sk->sk_receive_queue);
703 rxrpc_put_connection(rx->conn);
708 rxrpc_put_bundle(rx->trans, rx->bundle);
712 rxrpc_put_transport(rx->trans);
716 rxrpc_put_local(rx->local);
722 key_put(rx->securities);
723 rx->securities = NULL;
731 * release an RxRPC BSD socket on close() or equivalent
733 static int rxrpc_release(struct socket *sock)
735 struct sock *sk = sock->sk;
737 _enter("%p{%p}", sock, sk);
744 return rxrpc_release_sock(sk);
748 * RxRPC network protocol
750 static const struct proto_ops rxrpc_rpc_ops = {
752 .owner = THIS_MODULE,
753 .release = rxrpc_release,
755 .connect = rxrpc_connect,
756 .socketpair = sock_no_socketpair,
757 .accept = sock_no_accept,
758 .getname = sock_no_getname,
760 .ioctl = sock_no_ioctl,
761 .listen = rxrpc_listen,
762 .shutdown = sock_no_shutdown,
763 .setsockopt = rxrpc_setsockopt,
764 .getsockopt = sock_no_getsockopt,
765 .sendmsg = rxrpc_sendmsg,
766 .recvmsg = rxrpc_recvmsg,
767 .mmap = sock_no_mmap,
768 .sendpage = sock_no_sendpage,
771 static struct proto rxrpc_proto = {
773 .owner = THIS_MODULE,
774 .obj_size = sizeof(struct rxrpc_sock),
775 .max_header = sizeof(struct rxrpc_header),
778 static struct net_proto_family rxrpc_family_ops = {
780 .create = rxrpc_create,
781 .owner = THIS_MODULE,
785 * initialise and register the RxRPC protocol
787 static int __init af_rxrpc_init(void)
789 struct sk_buff *dummy_skb;
792 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof(dummy_skb->cb));
794 rxrpc_epoch = htonl(get_seconds());
797 rxrpc_call_jar = kmem_cache_create(
798 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
799 SLAB_HWCACHE_ALIGN, NULL);
800 if (!rxrpc_call_jar) {
801 printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n");
805 rxrpc_workqueue = create_workqueue("krxrpcd");
806 if (!rxrpc_workqueue) {
807 printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n");
808 goto error_work_queue;
811 ret = proto_register(&rxrpc_proto, 1);
813 printk(KERN_CRIT "RxRPC: Cannot register protocol\n");
817 ret = sock_register(&rxrpc_family_ops);
819 printk(KERN_CRIT "RxRPC: Cannot register socket family\n");
823 ret = register_key_type(&key_type_rxrpc);
825 printk(KERN_CRIT "RxRPC: Cannot register client key type\n");
829 ret = register_key_type(&key_type_rxrpc_s);
831 printk(KERN_CRIT "RxRPC: Cannot register server key type\n");
832 goto error_key_type_s;
835 #ifdef CONFIG_PROC_FS
836 proc_net_fops_create(&init_net, "rxrpc_calls", 0, &rxrpc_call_seq_fops);
837 proc_net_fops_create(&init_net, "rxrpc_conns", 0, &rxrpc_connection_seq_fops);
842 unregister_key_type(&key_type_rxrpc);
844 sock_unregister(PF_RXRPC);
846 proto_unregister(&rxrpc_proto);
848 destroy_workqueue(rxrpc_workqueue);
850 kmem_cache_destroy(rxrpc_call_jar);
856 * unregister the RxRPC protocol
858 static void __exit af_rxrpc_exit(void)
861 unregister_key_type(&key_type_rxrpc_s);
862 unregister_key_type(&key_type_rxrpc);
863 sock_unregister(PF_RXRPC);
864 proto_unregister(&rxrpc_proto);
865 rxrpc_destroy_all_calls();
866 rxrpc_destroy_all_connections();
867 rxrpc_destroy_all_transports();
868 rxrpc_destroy_all_peers();
869 rxrpc_destroy_all_locals();
871 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
873 _debug("flush scheduled work");
874 flush_workqueue(rxrpc_workqueue);
875 proc_net_remove(&init_net, "rxrpc_conns");
876 proc_net_remove(&init_net, "rxrpc_calls");
877 destroy_workqueue(rxrpc_workqueue);
878 kmem_cache_destroy(rxrpc_call_jar);
882 module_init(af_rxrpc_init);
883 module_exit(af_rxrpc_exit);