blob: 122d55d992e121ab2f8b24bc0d2e1668f55aa59e [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* AF_RXRPC implementation
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020017#include <net/net_namespace.h>
David Howells17926a72007-04-26 15:48:28 -070018#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
22MODULE_DESCRIPTION("RxRPC network protocol");
23MODULE_AUTHOR("Red Hat, Inc.");
24MODULE_LICENSE("GPL");
25MODULE_ALIAS_NETPROTO(PF_RXRPC);
26
27unsigned rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
28module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
29MODULE_PARM_DESC(rxrpc_debug, "RxRPC debugging mask");
30
31static int sysctl_rxrpc_max_qlen __read_mostly = 10;
32
33static struct proto rxrpc_proto;
34static const struct proto_ops rxrpc_rpc_ops;
35
36/* local epoch for detecting local-end reset */
37__be32 rxrpc_epoch;
38
39/* current debugging ID */
40atomic_t rxrpc_debug_id;
41
42/* count of skbs currently in use */
43atomic_t rxrpc_n_skbs;
44
David Howells651350d2007-04-26 15:50:17 -070045struct workqueue_struct *rxrpc_workqueue;
46
David Howells17926a72007-04-26 15:48:28 -070047static void rxrpc_sock_destructor(struct sock *);
48
49/*
50 * see if an RxRPC socket is currently writable
51 */
52static inline int rxrpc_writable(struct sock *sk)
53{
54 return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
55}
56
57/*
58 * wait for write bufferage to become available
59 */
60static void rxrpc_write_space(struct sock *sk)
61{
62 _enter("%p", 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);
68 }
69 read_unlock(&sk->sk_callback_lock);
70}
71
72/*
73 * validate an RxRPC address
74 */
75static int rxrpc_validate_address(struct rxrpc_sock *rx,
76 struct sockaddr_rxrpc *srx,
77 int len)
78{
79 if (len < sizeof(struct sockaddr_rxrpc))
80 return -EINVAL;
81
82 if (srx->srx_family != AF_RXRPC)
83 return -EAFNOSUPPORT;
84
85 if (srx->transport_type != SOCK_DGRAM)
86 return -ESOCKTNOSUPPORT;
87
88 len -= offsetof(struct sockaddr_rxrpc, transport);
89 if (srx->transport_len < sizeof(sa_family_t) ||
90 srx->transport_len > len)
91 return -EINVAL;
92
93 if (srx->transport.family != rx->proto)
94 return -EAFNOSUPPORT;
95
96 switch (srx->transport.family) {
97 case AF_INET:
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);
104 break;
105
106 case AF_INET6:
107 default:
108 return -EAFNOSUPPORT;
109 }
110
111 return 0;
112}
113
114/*
115 * bind a local address to an RxRPC socket
116 */
117static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
118{
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;
123 __be16 service_id;
124 int ret;
125
126 _enter("%p,%p,%d", rx, saddr, len);
127
128 ret = rxrpc_validate_address(rx, srx, len);
129 if (ret < 0)
130 goto error;
131
132 lock_sock(&rx->sk);
133
134 if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
135 ret = -EINVAL;
136 goto error_unlock;
137 }
138
139 memcpy(&rx->srx, srx, sizeof(rx->srx));
140
141 /* find a local transport endpoint if we don't have one already */
142 local = rxrpc_lookup_local(&rx->srx);
143 if (IS_ERR(local)) {
144 ret = PTR_ERR(local);
145 goto error_unlock;
146 }
147
148 rx->local = 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)
154 goto service_in_use;
155 }
156
157 rx->service_id = service_id;
158 list_add_tail(&rx->listen_link, &local->services);
159 write_unlock_bh(&local->services_lock);
160
161 rx->sk.sk_state = RXRPC_SERVER_BOUND;
162 } else {
163 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
164 }
165
166 release_sock(&rx->sk);
167 _leave(" = 0");
168 return 0;
169
170service_in_use:
171 ret = -EADDRINUSE;
172 write_unlock_bh(&local->services_lock);
173error_unlock:
174 release_sock(&rx->sk);
175error:
176 _leave(" = %d", ret);
177 return ret;
178}
179
180/*
181 * set the number of pending calls permitted on a listening socket
182 */
183static int rxrpc_listen(struct socket *sock, int backlog)
184{
185 struct sock *sk = sock->sk;
186 struct rxrpc_sock *rx = rxrpc_sk(sk);
187 int ret;
188
189 _enter("%p,%d", rx, backlog);
190
191 lock_sock(&rx->sk);
192
193 switch (rx->sk.sk_state) {
194 case RXRPC_UNCONNECTED:
195 ret = -EADDRNOTAVAIL;
196 break;
197 case RXRPC_CLIENT_BOUND:
198 case RXRPC_CLIENT_CONNECTED:
199 default:
200 ret = -EBUSY;
201 break;
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;
206 ret = 0;
207 break;
208 }
209
210 release_sock(&rx->sk);
211 _leave(" = %d", ret);
212 return ret;
213}
214
215/*
216 * find a transport by address
217 */
218static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
219 struct sockaddr *addr,
David Howells651350d2007-04-26 15:50:17 -0700220 int addr_len, int flags,
221 gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700222{
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;
227
228 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
229
230 ASSERT(rx->local != NULL);
231 ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
232
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);
237
238 /* find a remote transport endpoint from the local one */
David Howells651350d2007-04-26 15:50:17 -0700239 peer = rxrpc_get_peer(srx, gfp);
David Howells17926a72007-04-26 15:48:28 -0700240 if (IS_ERR(peer))
241 return ERR_PTR(PTR_ERR(peer));
242
243 /* find a transport */
David Howells651350d2007-04-26 15:50:17 -0700244 trans = rxrpc_get_transport(rx->local, peer, gfp);
David Howells17926a72007-04-26 15:48:28 -0700245 rxrpc_put_peer(peer);
246 _leave(" = %p", trans);
247 return trans;
248}
249
David Howells651350d2007-04-26 15:50:17 -0700250/**
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
256 *
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.
260 *
261 * The default socket destination address and security may be overridden by
262 * supplying @srx and @key.
263 */
264struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
265 struct sockaddr_rxrpc *srx,
266 struct key *key,
267 unsigned long user_call_ID,
268 gfp_t gfp)
269{
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);
274 __be16 service_id;
275
276 _enter(",,%x,%lx", key_serial(key), user_call_ID);
277
278 lock_sock(&rx->sk);
279
280 if (srx) {
281 trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
282 sizeof(*srx), 0, gfp);
283 if (IS_ERR(trans)) {
284 call = ERR_PTR(PTR_ERR(trans));
285 trans = NULL;
286 goto out;
287 }
288 } else {
289 trans = rx->trans;
290 if (!trans) {
291 call = ERR_PTR(-ENOTCONN);
292 goto out;
293 }
294 atomic_inc(&trans->usage);
295 }
296
297 service_id = rx->service_id;
298 if (srx)
299 service_id = htons(srx->srx_service);
300
301 if (!key)
302 key = rx->key;
303 if (key && !key->payload.data)
304 key = NULL; /* a no-security key */
305
306 bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp);
307 if (IS_ERR(bundle)) {
308 call = ERR_PTR(PTR_ERR(bundle));
309 goto out;
310 }
311
312 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
313 gfp);
314 rxrpc_put_bundle(trans, bundle);
315out:
316 rxrpc_put_transport(trans);
317 release_sock(&rx->sk);
318 _leave(" = %p", call);
319 return call;
320}
321
322EXPORT_SYMBOL(rxrpc_kernel_begin_call);
323
324/**
325 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
326 * @call: The call to end
327 *
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).
330 */
331void rxrpc_kernel_end_call(struct rxrpc_call *call)
332{
333 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
334 rxrpc_remove_user_ID(call->socket, call);
335 rxrpc_put_call(call);
336}
337
338EXPORT_SYMBOL(rxrpc_kernel_end_call);
339
340/**
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
344 *
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.
350 */
351void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
352 rxrpc_interceptor_t interceptor)
353{
354 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
355
356 _enter("");
357 rx->interceptor = interceptor;
358}
359
360EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
361
David Howells17926a72007-04-26 15:48:28 -0700362/*
363 * connect an RxRPC socket
364 * - this just targets it at a specific destination; no actual connection
365 * negotiation takes place
366 */
367static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
368 int addr_len, int flags)
369{
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);
375 int ret;
376
377 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
378
379 ret = rxrpc_validate_address(rx, srx, addr_len);
380 if (ret < 0) {
381 _leave(" = %d [bad addr]", ret);
382 return ret;
383 }
384
385 lock_sock(&rx->sk);
386
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);
397 if (IS_ERR(local)) {
398 release_sock(&rx->sk);
399 return PTR_ERR(local);
400 }
401 rx->local = local;
402 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
403 case RXRPC_CLIENT_BOUND:
404 break;
405 case RXRPC_CLIENT_CONNECTED:
406 release_sock(&rx->sk);
407 return -EISCONN;
408 default:
409 release_sock(&rx->sk);
410 return -EBUSY; /* server sockets can't connect as well */
411 }
412
David Howells651350d2007-04-26 15:50:17 -0700413 trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
414 GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700415 if (IS_ERR(trans)) {
416 release_sock(&rx->sk);
417 _leave(" = %ld", PTR_ERR(trans));
418 return PTR_ERR(trans);
419 }
420
421 rx->trans = trans;
422 rx->service_id = htons(srx->srx_service);
423 rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
424
425 release_sock(&rx->sk);
426 return 0;
427}
428
429/*
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)
437 */
438static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
439 struct msghdr *m, size_t len)
440{
441 struct rxrpc_transport *trans;
442 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
443 int ret;
444
445 _enter(",{%d},,%zu", rx->sk.sk_state, len);
446
447 if (m->msg_flags & MSG_OOB)
448 return -EOPNOTSUPP;
449
450 if (m->msg_name) {
451 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
452 if (ret < 0) {
453 _leave(" = %d [bad addr]", ret);
454 return ret;
455 }
456 }
457
458 trans = NULL;
459 lock_sock(&rx->sk);
460
461 if (m->msg_name) {
462 ret = -EISCONN;
463 trans = rxrpc_name_to_transport(sock, m->msg_name,
David Howells651350d2007-04-26 15:50:17 -0700464 m->msg_namelen, 0, GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 trans = NULL;
468 goto out;
469 }
470 } else {
471 trans = rx->trans;
472 if (trans)
473 atomic_inc(&trans->usage);
474 }
475
476 switch (rx->sk.sk_state) {
477 case RXRPC_SERVER_LISTENING:
478 if (!m->msg_name) {
479 ret = rxrpc_server_sendmsg(iocb, rx, m, len);
480 break;
481 }
482 case RXRPC_SERVER_BOUND:
483 case RXRPC_CLIENT_BOUND:
484 if (!m->msg_name) {
485 ret = -ENOTCONN;
486 break;
487 }
488 case RXRPC_CLIENT_CONNECTED:
489 ret = rxrpc_client_sendmsg(iocb, rx, trans, m, len);
490 break;
491 default:
492 ret = -ENOTCONN;
493 break;
494 }
495
496out:
497 release_sock(&rx->sk);
498 if (trans)
499 rxrpc_put_transport(trans);
500 _leave(" = %d", ret);
501 return ret;
502}
503
504/*
505 * set RxRPC socket options
506 */
507static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
508 char __user *optval, int optlen)
509{
510 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
511 unsigned min_sec_level;
512 int ret;
513
514 _enter(",%d,%d,,%d", level, optname, optlen);
515
516 lock_sock(&rx->sk);
517 ret = -EOPNOTSUPP;
518
519 if (level == SOL_RXRPC) {
520 switch (optname) {
521 case RXRPC_EXCLUSIVE_CONNECTION:
522 ret = -EINVAL;
523 if (optlen != 0)
524 goto error;
525 ret = -EISCONN;
526 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
527 goto error;
528 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
529 goto success;
530
531 case RXRPC_SECURITY_KEY:
532 ret = -EINVAL;
533 if (rx->key)
534 goto error;
535 ret = -EISCONN;
536 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
537 goto error;
538 ret = rxrpc_request_key(rx, optval, optlen);
539 goto error;
540
541 case RXRPC_SECURITY_KEYRING:
542 ret = -EINVAL;
543 if (rx->key)
544 goto error;
545 ret = -EISCONN;
546 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
547 goto error;
548 ret = rxrpc_server_keyring(rx, optval, optlen);
549 goto error;
550
551 case RXRPC_MIN_SECURITY_LEVEL:
552 ret = -EINVAL;
553 if (optlen != sizeof(unsigned))
554 goto error;
555 ret = -EISCONN;
556 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
557 goto error;
558 ret = get_user(min_sec_level,
559 (unsigned __user *) optval);
560 if (ret < 0)
561 goto error;
562 ret = -EINVAL;
563 if (min_sec_level > RXRPC_SECURITY_MAX)
564 goto error;
565 rx->min_sec_level = min_sec_level;
566 goto success;
567
568 default:
569 break;
570 }
571 }
572
573success:
574 ret = 0;
575error:
576 release_sock(&rx->sk);
577 return ret;
578}
579
580/*
581 * permit an RxRPC socket to be polled
582 */
583static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
584 poll_table *wait)
585{
586 unsigned int mask;
587 struct sock *sk = sock->sk;
588
589 poll_wait(file, sk->sk_sleep, wait);
590 mask = 0;
591
592 /* the socket is readable if there are any messages waiting on the Rx
593 * queue */
594 if (!skb_queue_empty(&sk->sk_receive_queue))
595 mask |= POLLIN | POLLRDNORM;
596
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;
602
603 return mask;
604}
605
606/*
607 * create an RxRPC socket
608 */
609static int rxrpc_create(struct socket *sock, int protocol)
610{
611 struct rxrpc_sock *rx;
612 struct sock *sk;
613
614 _enter("%p,%d", sock, protocol);
615
616 /* we support transport protocol UDP only */
617 if (protocol != PF_INET)
618 return -EPROTONOSUPPORT;
619
620 if (sock->type != SOCK_DGRAM)
621 return -ESOCKTNOSUPPORT;
622
623 sock->ops = &rxrpc_rpc_ops;
624 sock->state = SS_UNCONNECTED;
625
626 sk = sk_alloc(PF_RXRPC, GFP_KERNEL, &rxrpc_proto, 1);
627 if (!sk)
628 return -ENOMEM;
629
630 sock_init_data(sock, sk);
631 sk->sk_state = RXRPC_UNCONNECTED;
632 sk->sk_write_space = rxrpc_write_space;
633 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
634 sk->sk_destruct = rxrpc_sock_destructor;
635
636 rx = rxrpc_sk(sk);
637 rx->proto = protocol;
638 rx->calls = RB_ROOT;
639
640 INIT_LIST_HEAD(&rx->listen_link);
641 INIT_LIST_HEAD(&rx->secureq);
642 INIT_LIST_HEAD(&rx->acceptq);
643 rwlock_init(&rx->call_lock);
644 memset(&rx->srx, 0, sizeof(rx->srx));
645
646 _leave(" = 0 [%p]", rx);
647 return 0;
648}
649
650/*
651 * RxRPC socket destructor
652 */
653static void rxrpc_sock_destructor(struct sock *sk)
654{
655 _enter("%p", sk);
656
657 rxrpc_purge_queue(&sk->sk_receive_queue);
658
659 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
660 BUG_TRAP(sk_unhashed(sk));
661 BUG_TRAP(!sk->sk_socket);
662
663 if (!sock_flag(sk, SOCK_DEAD)) {
664 printk("Attempt to release alive rxrpc socket: %p\n", sk);
665 return;
666 }
667}
668
669/*
670 * release an RxRPC socket
671 */
672static int rxrpc_release_sock(struct sock *sk)
673{
674 struct rxrpc_sock *rx = rxrpc_sk(sk);
675
676 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
677
678 /* declare the socket closed for business */
679 sock_orphan(sk);
680 sk->sk_shutdown = SHUTDOWN_MASK;
681
682 spin_lock_bh(&sk->sk_receive_queue.lock);
683 sk->sk_state = RXRPC_CLOSE;
684 spin_unlock_bh(&sk->sk_receive_queue.lock);
685
686 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
687
688 if (!list_empty(&rx->listen_link)) {
689 write_lock_bh(&rx->local->services_lock);
690 list_del(&rx->listen_link);
691 write_unlock_bh(&rx->local->services_lock);
692 }
693
694 /* try to flush out this socket */
695 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700696 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700697 rxrpc_purge_queue(&sk->sk_receive_queue);
698
699 if (rx->conn) {
700 rxrpc_put_connection(rx->conn);
701 rx->conn = NULL;
702 }
703
704 if (rx->bundle) {
705 rxrpc_put_bundle(rx->trans, rx->bundle);
706 rx->bundle = NULL;
707 }
708 if (rx->trans) {
709 rxrpc_put_transport(rx->trans);
710 rx->trans = NULL;
711 }
712 if (rx->local) {
713 rxrpc_put_local(rx->local);
714 rx->local = NULL;
715 }
716
717 key_put(rx->key);
718 rx->key = NULL;
719 key_put(rx->securities);
720 rx->securities = NULL;
721 sock_put(sk);
722
723 _leave(" = 0");
724 return 0;
725}
726
727/*
728 * release an RxRPC BSD socket on close() or equivalent
729 */
730static int rxrpc_release(struct socket *sock)
731{
732 struct sock *sk = sock->sk;
733
734 _enter("%p{%p}", sock, sk);
735
736 if (!sk)
737 return 0;
738
739 sock->sk = NULL;
740
741 return rxrpc_release_sock(sk);
742}
743
744/*
745 * RxRPC network protocol
746 */
747static const struct proto_ops rxrpc_rpc_ops = {
748 .family = PF_UNIX,
749 .owner = THIS_MODULE,
750 .release = rxrpc_release,
751 .bind = rxrpc_bind,
752 .connect = rxrpc_connect,
753 .socketpair = sock_no_socketpair,
754 .accept = sock_no_accept,
755 .getname = sock_no_getname,
756 .poll = rxrpc_poll,
757 .ioctl = sock_no_ioctl,
758 .listen = rxrpc_listen,
759 .shutdown = sock_no_shutdown,
760 .setsockopt = rxrpc_setsockopt,
761 .getsockopt = sock_no_getsockopt,
762 .sendmsg = rxrpc_sendmsg,
763 .recvmsg = rxrpc_recvmsg,
764 .mmap = sock_no_mmap,
765 .sendpage = sock_no_sendpage,
766};
767
768static struct proto rxrpc_proto = {
769 .name = "RXRPC",
770 .owner = THIS_MODULE,
771 .obj_size = sizeof(struct rxrpc_sock),
772 .max_header = sizeof(struct rxrpc_header),
773};
774
775static struct net_proto_family rxrpc_family_ops = {
776 .family = PF_RXRPC,
777 .create = rxrpc_create,
778 .owner = THIS_MODULE,
779};
780
781/*
782 * initialise and register the RxRPC protocol
783 */
784static int __init af_rxrpc_init(void)
785{
786 struct sk_buff *dummy_skb;
787 int ret = -1;
788
789 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof(dummy_skb->cb));
790
john stultz2c6b47d2007-07-24 17:47:43 -0700791 rxrpc_epoch = htonl(get_seconds());
David Howells17926a72007-04-26 15:48:28 -0700792
David Howells651350d2007-04-26 15:50:17 -0700793 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700794 rxrpc_call_jar = kmem_cache_create(
795 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900796 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700797 if (!rxrpc_call_jar) {
798 printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700799 goto error_call_jar;
800 }
801
David Howells651350d2007-04-26 15:50:17 -0700802 rxrpc_workqueue = create_workqueue("krxrpcd");
803 if (!rxrpc_workqueue) {
804 printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n");
805 goto error_work_queue;
806 }
807
David Howells17926a72007-04-26 15:48:28 -0700808 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900809 if (ret < 0) {
810 printk(KERN_CRIT "RxRPC: Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700811 goto error_proto;
812 }
813
814 ret = sock_register(&rxrpc_family_ops);
815 if (ret < 0) {
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900816 printk(KERN_CRIT "RxRPC: Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700817 goto error_sock;
818 }
819
820 ret = register_key_type(&key_type_rxrpc);
821 if (ret < 0) {
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900822 printk(KERN_CRIT "RxRPC: Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700823 goto error_key_type;
824 }
825
826 ret = register_key_type(&key_type_rxrpc_s);
827 if (ret < 0) {
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900828 printk(KERN_CRIT "RxRPC: Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700829 goto error_key_type_s;
830 }
831
832#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200833 proc_net_fops_create(&init_net, "rxrpc_calls", 0, &rxrpc_call_seq_fops);
834 proc_net_fops_create(&init_net, "rxrpc_conns", 0, &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700835#endif
836 return 0;
837
838error_key_type_s:
839 unregister_key_type(&key_type_rxrpc);
840error_key_type:
841 sock_unregister(PF_RXRPC);
842error_sock:
843 proto_unregister(&rxrpc_proto);
844error_proto:
David Howells651350d2007-04-26 15:50:17 -0700845 destroy_workqueue(rxrpc_workqueue);
846error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700847 kmem_cache_destroy(rxrpc_call_jar);
848error_call_jar:
849 return ret;
850}
851
852/*
853 * unregister the RxRPC protocol
854 */
855static void __exit af_rxrpc_exit(void)
856{
857 _enter("");
858 unregister_key_type(&key_type_rxrpc_s);
859 unregister_key_type(&key_type_rxrpc);
860 sock_unregister(PF_RXRPC);
861 proto_unregister(&rxrpc_proto);
862 rxrpc_destroy_all_calls();
863 rxrpc_destroy_all_connections();
864 rxrpc_destroy_all_transports();
865 rxrpc_destroy_all_peers();
866 rxrpc_destroy_all_locals();
867
868 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
869
870 _debug("flush scheduled work");
David Howells651350d2007-04-26 15:50:17 -0700871 flush_workqueue(rxrpc_workqueue);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200872 proc_net_remove(&init_net, "rxrpc_conns");
873 proc_net_remove(&init_net, "rxrpc_calls");
David Howells651350d2007-04-26 15:50:17 -0700874 destroy_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700875 kmem_cache_destroy(rxrpc_call_jar);
876 _leave("");
877}
878
879module_init(af_rxrpc_init);
880module_exit(af_rxrpc_exit);