powerpc/powernv: Enable LEDS support
[linux/fpc-iii.git] / net / rxrpc / af_rxrpc.c
blob25d60ed15284a56b1a2f6b9bc8c680f0c9f567c0
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/kernel.h>
14 #include <linux/net.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/poll.h>
18 #include <linux/proc_fs.h>
19 #include <linux/key-type.h>
20 #include <net/net_namespace.h>
21 #include <net/sock.h>
22 #include <net/af_rxrpc.h>
23 #include "ar-internal.h"
25 MODULE_DESCRIPTION("RxRPC network protocol");
26 MODULE_AUTHOR("Red Hat, Inc.");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS_NETPROTO(PF_RXRPC);
30 unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
31 module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
32 MODULE_PARM_DESC(debug, "RxRPC debugging mask");
34 static int sysctl_rxrpc_max_qlen __read_mostly = 10;
36 static struct proto rxrpc_proto;
37 static const struct proto_ops rxrpc_rpc_ops;
39 /* local epoch for detecting local-end reset */
40 __be32 rxrpc_epoch;
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)
65 _enter("%p", sk);
66 rcu_read_lock();
67 if (rxrpc_writable(sk)) {
68 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
70 if (wq_has_sleeper(wq))
71 wake_up_interruptible(&wq->wait);
72 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
74 rcu_read_unlock();
78 * validate an RxRPC address
80 static int rxrpc_validate_address(struct rxrpc_sock *rx,
81 struct sockaddr_rxrpc *srx,
82 int len)
84 if (len < sizeof(struct sockaddr_rxrpc))
85 return -EINVAL;
87 if (srx->srx_family != AF_RXRPC)
88 return -EAFNOSUPPORT;
90 if (srx->transport_type != SOCK_DGRAM)
91 return -ESOCKTNOSUPPORT;
93 len -= offsetof(struct sockaddr_rxrpc, transport);
94 if (srx->transport_len < sizeof(sa_family_t) ||
95 srx->transport_len > len)
96 return -EINVAL;
98 if (srx->transport.family != rx->proto)
99 return -EAFNOSUPPORT;
101 switch (srx->transport.family) {
102 case AF_INET:
103 _debug("INET: %x @ %pI4",
104 ntohs(srx->transport.sin.sin_port),
105 &srx->transport.sin.sin_addr);
106 if (srx->transport_len > 8)
107 memset((void *)&srx->transport + 8, 0,
108 srx->transport_len - 8);
109 break;
111 case AF_INET6:
112 default:
113 return -EAFNOSUPPORT;
116 return 0;
120 * bind a local address to an RxRPC socket
122 static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
124 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) saddr;
125 struct sock *sk = sock->sk;
126 struct rxrpc_local *local;
127 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
128 __be16 service_id;
129 int ret;
131 _enter("%p,%p,%d", rx, saddr, len);
133 ret = rxrpc_validate_address(rx, srx, len);
134 if (ret < 0)
135 goto error;
137 lock_sock(&rx->sk);
139 if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
140 ret = -EINVAL;
141 goto error_unlock;
144 memcpy(&rx->srx, srx, sizeof(rx->srx));
146 /* find a local transport endpoint if we don't have one already */
147 local = rxrpc_lookup_local(&rx->srx);
148 if (IS_ERR(local)) {
149 ret = PTR_ERR(local);
150 goto error_unlock;
153 rx->local = local;
154 if (srx->srx_service) {
155 service_id = htons(srx->srx_service);
156 write_lock_bh(&local->services_lock);
157 list_for_each_entry(prx, &local->services, listen_link) {
158 if (prx->service_id == service_id)
159 goto service_in_use;
162 rx->service_id = service_id;
163 list_add_tail(&rx->listen_link, &local->services);
164 write_unlock_bh(&local->services_lock);
166 rx->sk.sk_state = RXRPC_SERVER_BOUND;
167 } else {
168 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
171 release_sock(&rx->sk);
172 _leave(" = 0");
173 return 0;
175 service_in_use:
176 ret = -EADDRINUSE;
177 write_unlock_bh(&local->services_lock);
178 error_unlock:
179 release_sock(&rx->sk);
180 error:
181 _leave(" = %d", ret);
182 return ret;
186 * set the number of pending calls permitted on a listening socket
188 static int rxrpc_listen(struct socket *sock, int backlog)
190 struct sock *sk = sock->sk;
191 struct rxrpc_sock *rx = rxrpc_sk(sk);
192 int ret;
194 _enter("%p,%d", rx, backlog);
196 lock_sock(&rx->sk);
198 switch (rx->sk.sk_state) {
199 case RXRPC_UNCONNECTED:
200 ret = -EADDRNOTAVAIL;
201 break;
202 case RXRPC_CLIENT_BOUND:
203 case RXRPC_CLIENT_CONNECTED:
204 default:
205 ret = -EBUSY;
206 break;
207 case RXRPC_SERVER_BOUND:
208 ASSERT(rx->local != NULL);
209 sk->sk_max_ack_backlog = backlog;
210 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
211 ret = 0;
212 break;
215 release_sock(&rx->sk);
216 _leave(" = %d", ret);
217 return ret;
221 * find a transport by address
223 static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
224 struct sockaddr *addr,
225 int addr_len, int flags,
226 gfp_t gfp)
228 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
229 struct rxrpc_transport *trans;
230 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
231 struct rxrpc_peer *peer;
233 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
235 ASSERT(rx->local != NULL);
236 ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
238 if (rx->srx.transport_type != srx->transport_type)
239 return ERR_PTR(-ESOCKTNOSUPPORT);
240 if (rx->srx.transport.family != srx->transport.family)
241 return ERR_PTR(-EAFNOSUPPORT);
243 /* find a remote transport endpoint from the local one */
244 peer = rxrpc_get_peer(srx, gfp);
245 if (IS_ERR(peer))
246 return ERR_CAST(peer);
248 /* find a transport */
249 trans = rxrpc_get_transport(rx->local, peer, gfp);
250 rxrpc_put_peer(peer);
251 _leave(" = %p", trans);
252 return trans;
256 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
257 * @sock: The socket on which to make the call
258 * @srx: The address of the peer to contact (defaults to socket setting)
259 * @key: The security context to use (defaults to socket setting)
260 * @user_call_ID: The ID to use
262 * Allow a kernel service to begin a call on the nominated socket. This just
263 * sets up all the internal tracking structures and allocates connection and
264 * call IDs as appropriate. The call to be used is returned.
266 * The default socket destination address and security may be overridden by
267 * supplying @srx and @key.
269 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
270 struct sockaddr_rxrpc *srx,
271 struct key *key,
272 unsigned long user_call_ID,
273 gfp_t gfp)
275 struct rxrpc_conn_bundle *bundle;
276 struct rxrpc_transport *trans;
277 struct rxrpc_call *call;
278 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
279 __be16 service_id;
281 _enter(",,%x,%lx", key_serial(key), user_call_ID);
283 lock_sock(&rx->sk);
285 if (srx) {
286 trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
287 sizeof(*srx), 0, gfp);
288 if (IS_ERR(trans)) {
289 call = ERR_CAST(trans);
290 trans = NULL;
291 goto out_notrans;
293 } else {
294 trans = rx->trans;
295 if (!trans) {
296 call = ERR_PTR(-ENOTCONN);
297 goto out_notrans;
299 atomic_inc(&trans->usage);
302 service_id = rx->service_id;
303 if (srx)
304 service_id = htons(srx->srx_service);
306 if (!key)
307 key = rx->key;
308 if (key && !key->payload.data)
309 key = NULL; /* a no-security key */
311 bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp);
312 if (IS_ERR(bundle)) {
313 call = ERR_CAST(bundle);
314 goto out;
317 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
318 gfp);
319 rxrpc_put_bundle(trans, bundle);
320 out:
321 rxrpc_put_transport(trans);
322 out_notrans:
323 release_sock(&rx->sk);
324 _leave(" = %p", call);
325 return call;
328 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
331 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
332 * @call: The call to end
334 * Allow a kernel service to end a call it was using. The call must be
335 * complete before this is called (the call should be aborted if necessary).
337 void rxrpc_kernel_end_call(struct rxrpc_call *call)
339 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
340 rxrpc_remove_user_ID(call->socket, call);
341 rxrpc_put_call(call);
344 EXPORT_SYMBOL(rxrpc_kernel_end_call);
347 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
348 * @sock: The socket to intercept received messages on
349 * @interceptor: The function to pass the messages to
351 * Allow a kernel service to intercept messages heading for the Rx queue on an
352 * RxRPC socket. They get passed to the specified function instead.
353 * @interceptor should free the socket buffers it is given. @interceptor is
354 * called with the socket receive queue spinlock held and softirqs disabled -
355 * this ensures that the messages will be delivered in the right order.
357 void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
358 rxrpc_interceptor_t interceptor)
360 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
362 _enter("");
363 rx->interceptor = interceptor;
366 EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
369 * connect an RxRPC socket
370 * - this just targets it at a specific destination; no actual connection
371 * negotiation takes place
373 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
374 int addr_len, int flags)
376 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
377 struct sock *sk = sock->sk;
378 struct rxrpc_transport *trans;
379 struct rxrpc_local *local;
380 struct rxrpc_sock *rx = rxrpc_sk(sk);
381 int ret;
383 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
385 ret = rxrpc_validate_address(rx, srx, addr_len);
386 if (ret < 0) {
387 _leave(" = %d [bad addr]", ret);
388 return ret;
391 lock_sock(&rx->sk);
393 switch (rx->sk.sk_state) {
394 case RXRPC_UNCONNECTED:
395 /* find a local transport endpoint if we don't have one already */
396 ASSERTCMP(rx->local, ==, NULL);
397 rx->srx.srx_family = AF_RXRPC;
398 rx->srx.srx_service = 0;
399 rx->srx.transport_type = srx->transport_type;
400 rx->srx.transport_len = sizeof(sa_family_t);
401 rx->srx.transport.family = srx->transport.family;
402 local = rxrpc_lookup_local(&rx->srx);
403 if (IS_ERR(local)) {
404 release_sock(&rx->sk);
405 return PTR_ERR(local);
407 rx->local = local;
408 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
409 case RXRPC_CLIENT_BOUND:
410 break;
411 case RXRPC_CLIENT_CONNECTED:
412 release_sock(&rx->sk);
413 return -EISCONN;
414 default:
415 release_sock(&rx->sk);
416 return -EBUSY; /* server sockets can't connect as well */
419 trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
420 GFP_KERNEL);
421 if (IS_ERR(trans)) {
422 release_sock(&rx->sk);
423 _leave(" = %ld", PTR_ERR(trans));
424 return PTR_ERR(trans);
427 rx->trans = trans;
428 rx->service_id = htons(srx->srx_service);
429 rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
431 release_sock(&rx->sk);
432 return 0;
436 * send a message through an RxRPC socket
437 * - in a client this does a number of things:
438 * - finds/sets up a connection for the security specified (if any)
439 * - initiates a call (ID in control data)
440 * - ends the request phase of a call (if MSG_MORE is not set)
441 * - sends a call data packet
442 * - may send an abort (abort code in control data)
444 static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
446 struct rxrpc_transport *trans;
447 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
448 int ret;
450 _enter(",{%d},,%zu", rx->sk.sk_state, len);
452 if (m->msg_flags & MSG_OOB)
453 return -EOPNOTSUPP;
455 if (m->msg_name) {
456 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
457 if (ret < 0) {
458 _leave(" = %d [bad addr]", ret);
459 return ret;
463 trans = NULL;
464 lock_sock(&rx->sk);
466 if (m->msg_name) {
467 ret = -EISCONN;
468 trans = rxrpc_name_to_transport(sock, m->msg_name,
469 m->msg_namelen, 0, GFP_KERNEL);
470 if (IS_ERR(trans)) {
471 ret = PTR_ERR(trans);
472 trans = NULL;
473 goto out;
475 } else {
476 trans = rx->trans;
477 if (trans)
478 atomic_inc(&trans->usage);
481 switch (rx->sk.sk_state) {
482 case RXRPC_SERVER_LISTENING:
483 if (!m->msg_name) {
484 ret = rxrpc_server_sendmsg(rx, m, len);
485 break;
487 case RXRPC_SERVER_BOUND:
488 case RXRPC_CLIENT_BOUND:
489 if (!m->msg_name) {
490 ret = -ENOTCONN;
491 break;
493 case RXRPC_CLIENT_CONNECTED:
494 ret = rxrpc_client_sendmsg(rx, trans, m, len);
495 break;
496 default:
497 ret = -ENOTCONN;
498 break;
501 out:
502 release_sock(&rx->sk);
503 if (trans)
504 rxrpc_put_transport(trans);
505 _leave(" = %d", ret);
506 return ret;
510 * set RxRPC socket options
512 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
513 char __user *optval, unsigned int optlen)
515 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
516 unsigned int min_sec_level;
517 int ret;
519 _enter(",%d,%d,,%d", level, optname, optlen);
521 lock_sock(&rx->sk);
522 ret = -EOPNOTSUPP;
524 if (level == SOL_RXRPC) {
525 switch (optname) {
526 case RXRPC_EXCLUSIVE_CONNECTION:
527 ret = -EINVAL;
528 if (optlen != 0)
529 goto error;
530 ret = -EISCONN;
531 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
532 goto error;
533 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
534 goto success;
536 case RXRPC_SECURITY_KEY:
537 ret = -EINVAL;
538 if (rx->key)
539 goto error;
540 ret = -EISCONN;
541 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
542 goto error;
543 ret = rxrpc_request_key(rx, optval, optlen);
544 goto error;
546 case RXRPC_SECURITY_KEYRING:
547 ret = -EINVAL;
548 if (rx->key)
549 goto error;
550 ret = -EISCONN;
551 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
552 goto error;
553 ret = rxrpc_server_keyring(rx, optval, optlen);
554 goto error;
556 case RXRPC_MIN_SECURITY_LEVEL:
557 ret = -EINVAL;
558 if (optlen != sizeof(unsigned int))
559 goto error;
560 ret = -EISCONN;
561 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
562 goto error;
563 ret = get_user(min_sec_level,
564 (unsigned int __user *) optval);
565 if (ret < 0)
566 goto error;
567 ret = -EINVAL;
568 if (min_sec_level > RXRPC_SECURITY_MAX)
569 goto error;
570 rx->min_sec_level = min_sec_level;
571 goto success;
573 default:
574 break;
578 success:
579 ret = 0;
580 error:
581 release_sock(&rx->sk);
582 return ret;
586 * permit an RxRPC socket to be polled
588 static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
589 poll_table *wait)
591 unsigned int mask;
592 struct sock *sk = sock->sk;
594 sock_poll_wait(file, sk_sleep(sk), wait);
595 mask = 0;
597 /* the socket is readable if there are any messages waiting on the Rx
598 * queue */
599 if (!skb_queue_empty(&sk->sk_receive_queue))
600 mask |= POLLIN | POLLRDNORM;
602 /* the socket is writable if there is space to add new data to the
603 * socket; there is no guarantee that any particular call in progress
604 * on the socket may have space in the Tx ACK window */
605 if (rxrpc_writable(sk))
606 mask |= POLLOUT | POLLWRNORM;
608 return mask;
612 * create an RxRPC socket
614 static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
615 int kern)
617 struct rxrpc_sock *rx;
618 struct sock *sk;
620 _enter("%p,%d", sock, protocol);
622 if (!net_eq(net, &init_net))
623 return -EAFNOSUPPORT;
625 /* we support transport protocol UDP only */
626 if (protocol != PF_INET)
627 return -EPROTONOSUPPORT;
629 if (sock->type != SOCK_DGRAM)
630 return -ESOCKTNOSUPPORT;
632 sock->ops = &rxrpc_rpc_ops;
633 sock->state = SS_UNCONNECTED;
635 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
636 if (!sk)
637 return -ENOMEM;
639 sock_init_data(sock, sk);
640 sk->sk_state = RXRPC_UNCONNECTED;
641 sk->sk_write_space = rxrpc_write_space;
642 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
643 sk->sk_destruct = rxrpc_sock_destructor;
645 rx = rxrpc_sk(sk);
646 rx->proto = protocol;
647 rx->calls = RB_ROOT;
649 INIT_LIST_HEAD(&rx->listen_link);
650 INIT_LIST_HEAD(&rx->secureq);
651 INIT_LIST_HEAD(&rx->acceptq);
652 rwlock_init(&rx->call_lock);
653 memset(&rx->srx, 0, sizeof(rx->srx));
655 _leave(" = 0 [%p]", rx);
656 return 0;
660 * RxRPC socket destructor
662 static void rxrpc_sock_destructor(struct sock *sk)
664 _enter("%p", sk);
666 rxrpc_purge_queue(&sk->sk_receive_queue);
668 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
669 WARN_ON(!sk_unhashed(sk));
670 WARN_ON(sk->sk_socket);
672 if (!sock_flag(sk, SOCK_DEAD)) {
673 printk("Attempt to release alive rxrpc socket: %p\n", sk);
674 return;
679 * release an RxRPC socket
681 static int rxrpc_release_sock(struct sock *sk)
683 struct rxrpc_sock *rx = rxrpc_sk(sk);
685 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
687 /* declare the socket closed for business */
688 sock_orphan(sk);
689 sk->sk_shutdown = SHUTDOWN_MASK;
691 spin_lock_bh(&sk->sk_receive_queue.lock);
692 sk->sk_state = RXRPC_CLOSE;
693 spin_unlock_bh(&sk->sk_receive_queue.lock);
695 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
697 if (!list_empty(&rx->listen_link)) {
698 write_lock_bh(&rx->local->services_lock);
699 list_del(&rx->listen_link);
700 write_unlock_bh(&rx->local->services_lock);
703 /* try to flush out this socket */
704 rxrpc_release_calls_on_socket(rx);
705 flush_workqueue(rxrpc_workqueue);
706 rxrpc_purge_queue(&sk->sk_receive_queue);
708 if (rx->conn) {
709 rxrpc_put_connection(rx->conn);
710 rx->conn = NULL;
713 if (rx->bundle) {
714 rxrpc_put_bundle(rx->trans, rx->bundle);
715 rx->bundle = NULL;
717 if (rx->trans) {
718 rxrpc_put_transport(rx->trans);
719 rx->trans = NULL;
721 if (rx->local) {
722 rxrpc_put_local(rx->local);
723 rx->local = NULL;
726 key_put(rx->key);
727 rx->key = NULL;
728 key_put(rx->securities);
729 rx->securities = NULL;
730 sock_put(sk);
732 _leave(" = 0");
733 return 0;
737 * release an RxRPC BSD socket on close() or equivalent
739 static int rxrpc_release(struct socket *sock)
741 struct sock *sk = sock->sk;
743 _enter("%p{%p}", sock, sk);
745 if (!sk)
746 return 0;
748 sock->sk = NULL;
750 return rxrpc_release_sock(sk);
754 * RxRPC network protocol
756 static const struct proto_ops rxrpc_rpc_ops = {
757 .family = PF_UNIX,
758 .owner = THIS_MODULE,
759 .release = rxrpc_release,
760 .bind = rxrpc_bind,
761 .connect = rxrpc_connect,
762 .socketpair = sock_no_socketpair,
763 .accept = sock_no_accept,
764 .getname = sock_no_getname,
765 .poll = rxrpc_poll,
766 .ioctl = sock_no_ioctl,
767 .listen = rxrpc_listen,
768 .shutdown = sock_no_shutdown,
769 .setsockopt = rxrpc_setsockopt,
770 .getsockopt = sock_no_getsockopt,
771 .sendmsg = rxrpc_sendmsg,
772 .recvmsg = rxrpc_recvmsg,
773 .mmap = sock_no_mmap,
774 .sendpage = sock_no_sendpage,
777 static struct proto rxrpc_proto = {
778 .name = "RXRPC",
779 .owner = THIS_MODULE,
780 .obj_size = sizeof(struct rxrpc_sock),
781 .max_header = sizeof(struct rxrpc_header),
784 static const struct net_proto_family rxrpc_family_ops = {
785 .family = PF_RXRPC,
786 .create = rxrpc_create,
787 .owner = THIS_MODULE,
791 * initialise and register the RxRPC protocol
793 static int __init af_rxrpc_init(void)
795 int ret = -1;
797 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
799 rxrpc_epoch = htonl(get_seconds());
801 ret = -ENOMEM;
802 rxrpc_call_jar = kmem_cache_create(
803 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
804 SLAB_HWCACHE_ALIGN, NULL);
805 if (!rxrpc_call_jar) {
806 printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n");
807 goto error_call_jar;
810 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
811 if (!rxrpc_workqueue) {
812 printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n");
813 goto error_work_queue;
816 ret = proto_register(&rxrpc_proto, 1);
817 if (ret < 0) {
818 printk(KERN_CRIT "RxRPC: Cannot register protocol\n");
819 goto error_proto;
822 ret = sock_register(&rxrpc_family_ops);
823 if (ret < 0) {
824 printk(KERN_CRIT "RxRPC: Cannot register socket family\n");
825 goto error_sock;
828 ret = register_key_type(&key_type_rxrpc);
829 if (ret < 0) {
830 printk(KERN_CRIT "RxRPC: Cannot register client key type\n");
831 goto error_key_type;
834 ret = register_key_type(&key_type_rxrpc_s);
835 if (ret < 0) {
836 printk(KERN_CRIT "RxRPC: Cannot register server key type\n");
837 goto error_key_type_s;
840 ret = rxrpc_sysctl_init();
841 if (ret < 0) {
842 printk(KERN_CRIT "RxRPC: Cannot register sysctls\n");
843 goto error_sysctls;
846 #ifdef CONFIG_PROC_FS
847 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
848 proc_create("rxrpc_conns", 0, init_net.proc_net,
849 &rxrpc_connection_seq_fops);
850 #endif
851 return 0;
853 error_sysctls:
854 unregister_key_type(&key_type_rxrpc_s);
855 error_key_type_s:
856 unregister_key_type(&key_type_rxrpc);
857 error_key_type:
858 sock_unregister(PF_RXRPC);
859 error_sock:
860 proto_unregister(&rxrpc_proto);
861 error_proto:
862 destroy_workqueue(rxrpc_workqueue);
863 error_work_queue:
864 kmem_cache_destroy(rxrpc_call_jar);
865 error_call_jar:
866 return ret;
870 * unregister the RxRPC protocol
872 static void __exit af_rxrpc_exit(void)
874 _enter("");
875 rxrpc_sysctl_exit();
876 unregister_key_type(&key_type_rxrpc_s);
877 unregister_key_type(&key_type_rxrpc);
878 sock_unregister(PF_RXRPC);
879 proto_unregister(&rxrpc_proto);
880 rxrpc_destroy_all_calls();
881 rxrpc_destroy_all_connections();
882 rxrpc_destroy_all_transports();
883 rxrpc_destroy_all_peers();
884 rxrpc_destroy_all_locals();
886 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
888 _debug("flush scheduled work");
889 flush_workqueue(rxrpc_workqueue);
890 remove_proc_entry("rxrpc_conns", init_net.proc_net);
891 remove_proc_entry("rxrpc_calls", init_net.proc_net);
892 destroy_workqueue(rxrpc_workqueue);
893 kmem_cache_destroy(rxrpc_call_jar);
894 _leave("");
897 module_init(af_rxrpc_init);
898 module_exit(af_rxrpc_exit);