blob: 2d0c29c837f72d3308242d4d30fd0f038ca4af57 [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>
David Howells76181c12007-10-16 23:29:46 -070017#include <linux/key-type.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020018#include <net/net_namespace.h>
David Howells17926a72007-04-26 15:48:28 -070019#include <net/sock.h>
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
23MODULE_DESCRIPTION("RxRPC network protocol");
24MODULE_AUTHOR("Red Hat, Inc.");
25MODULE_LICENSE("GPL");
26MODULE_ALIAS_NETPROTO(PF_RXRPC);
27
28unsigned rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
29module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
30MODULE_PARM_DESC(rxrpc_debug, "RxRPC debugging mask");
31
32static int sysctl_rxrpc_max_qlen __read_mostly = 10;
33
34static struct proto rxrpc_proto;
35static const struct proto_ops rxrpc_rpc_ops;
36
37/* local epoch for detecting local-end reset */
38__be32 rxrpc_epoch;
39
40/* current debugging ID */
41atomic_t rxrpc_debug_id;
42
43/* count of skbs currently in use */
44atomic_t rxrpc_n_skbs;
45
David Howells651350d2007-04-26 15:50:17 -070046struct workqueue_struct *rxrpc_workqueue;
47
David Howells17926a72007-04-26 15:48:28 -070048static void rxrpc_sock_destructor(struct sock *);
49
50/*
51 * see if an RxRPC socket is currently writable
52 */
53static inline int rxrpc_writable(struct sock *sk)
54{
55 return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
56}
57
58/*
59 * wait for write bufferage to become available
60 */
61static void rxrpc_write_space(struct sock *sk)
62{
63 _enter("%p", sk);
64 read_lock(&sk->sk_callback_lock);
65 if (rxrpc_writable(sk)) {
66 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
67 wake_up_interruptible(sk->sk_sleep);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +080068 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
David Howells17926a72007-04-26 15:48:28 -070069 }
70 read_unlock(&sk->sk_callback_lock);
71}
72
73/*
74 * validate an RxRPC address
75 */
76static int rxrpc_validate_address(struct rxrpc_sock *rx,
77 struct sockaddr_rxrpc *srx,
78 int len)
79{
80 if (len < sizeof(struct sockaddr_rxrpc))
81 return -EINVAL;
82
83 if (srx->srx_family != AF_RXRPC)
84 return -EAFNOSUPPORT;
85
86 if (srx->transport_type != SOCK_DGRAM)
87 return -ESOCKTNOSUPPORT;
88
89 len -= offsetof(struct sockaddr_rxrpc, transport);
90 if (srx->transport_len < sizeof(sa_family_t) ||
91 srx->transport_len > len)
92 return -EINVAL;
93
94 if (srx->transport.family != rx->proto)
95 return -EAFNOSUPPORT;
96
97 switch (srx->transport.family) {
98 case AF_INET:
99 _debug("INET: %x @ %u.%u.%u.%u",
100 ntohs(srx->transport.sin.sin_port),
101 NIPQUAD(srx->transport.sin.sin_addr));
102 if (srx->transport_len > 8)
103 memset((void *)&srx->transport + 8, 0,
104 srx->transport_len - 8);
105 break;
106
107 case AF_INET6:
108 default:
109 return -EAFNOSUPPORT;
110 }
111
112 return 0;
113}
114
115/*
116 * bind a local address to an RxRPC socket
117 */
118static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
119{
120 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) saddr;
121 struct sock *sk = sock->sk;
122 struct rxrpc_local *local;
123 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
124 __be16 service_id;
125 int ret;
126
127 _enter("%p,%p,%d", rx, saddr, len);
128
129 ret = rxrpc_validate_address(rx, srx, len);
130 if (ret < 0)
131 goto error;
132
133 lock_sock(&rx->sk);
134
135 if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
136 ret = -EINVAL;
137 goto error_unlock;
138 }
139
140 memcpy(&rx->srx, srx, sizeof(rx->srx));
141
142 /* find a local transport endpoint if we don't have one already */
143 local = rxrpc_lookup_local(&rx->srx);
144 if (IS_ERR(local)) {
145 ret = PTR_ERR(local);
146 goto error_unlock;
147 }
148
149 rx->local = local;
150 if (srx->srx_service) {
151 service_id = htons(srx->srx_service);
152 write_lock_bh(&local->services_lock);
153 list_for_each_entry(prx, &local->services, listen_link) {
154 if (prx->service_id == service_id)
155 goto service_in_use;
156 }
157
158 rx->service_id = service_id;
159 list_add_tail(&rx->listen_link, &local->services);
160 write_unlock_bh(&local->services_lock);
161
162 rx->sk.sk_state = RXRPC_SERVER_BOUND;
163 } else {
164 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
165 }
166
167 release_sock(&rx->sk);
168 _leave(" = 0");
169 return 0;
170
171service_in_use:
172 ret = -EADDRINUSE;
173 write_unlock_bh(&local->services_lock);
174error_unlock:
175 release_sock(&rx->sk);
176error:
177 _leave(" = %d", ret);
178 return ret;
179}
180
181/*
182 * set the number of pending calls permitted on a listening socket
183 */
184static int rxrpc_listen(struct socket *sock, int backlog)
185{
186 struct sock *sk = sock->sk;
187 struct rxrpc_sock *rx = rxrpc_sk(sk);
188 int ret;
189
190 _enter("%p,%d", rx, backlog);
191
192 lock_sock(&rx->sk);
193
194 switch (rx->sk.sk_state) {
195 case RXRPC_UNCONNECTED:
196 ret = -EADDRNOTAVAIL;
197 break;
198 case RXRPC_CLIENT_BOUND:
199 case RXRPC_CLIENT_CONNECTED:
200 default:
201 ret = -EBUSY;
202 break;
203 case RXRPC_SERVER_BOUND:
204 ASSERT(rx->local != NULL);
205 sk->sk_max_ack_backlog = backlog;
206 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
207 ret = 0;
208 break;
209 }
210
211 release_sock(&rx->sk);
212 _leave(" = %d", ret);
213 return ret;
214}
215
216/*
217 * find a transport by address
218 */
219static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
220 struct sockaddr *addr,
David Howells651350d2007-04-26 15:50:17 -0700221 int addr_len, int flags,
222 gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700223{
224 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
225 struct rxrpc_transport *trans;
226 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
227 struct rxrpc_peer *peer;
228
229 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
230
231 ASSERT(rx->local != NULL);
232 ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
233
234 if (rx->srx.transport_type != srx->transport_type)
235 return ERR_PTR(-ESOCKTNOSUPPORT);
236 if (rx->srx.transport.family != srx->transport.family)
237 return ERR_PTR(-EAFNOSUPPORT);
238
239 /* find a remote transport endpoint from the local one */
David Howells651350d2007-04-26 15:50:17 -0700240 peer = rxrpc_get_peer(srx, gfp);
David Howells17926a72007-04-26 15:48:28 -0700241 if (IS_ERR(peer))
David Howellse231c2e2008-02-07 00:15:26 -0800242 return ERR_CAST(peer);
David Howells17926a72007-04-26 15:48:28 -0700243
244 /* find a transport */
David Howells651350d2007-04-26 15:50:17 -0700245 trans = rxrpc_get_transport(rx->local, peer, gfp);
David Howells17926a72007-04-26 15:48:28 -0700246 rxrpc_put_peer(peer);
247 _leave(" = %p", trans);
248 return trans;
249}
250
David Howells651350d2007-04-26 15:50:17 -0700251/**
252 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
253 * @sock: The socket on which to make the call
254 * @srx: The address of the peer to contact (defaults to socket setting)
255 * @key: The security context to use (defaults to socket setting)
256 * @user_call_ID: The ID to use
257 *
258 * Allow a kernel service to begin a call on the nominated socket. This just
259 * sets up all the internal tracking structures and allocates connection and
260 * call IDs as appropriate. The call to be used is returned.
261 *
262 * The default socket destination address and security may be overridden by
263 * supplying @srx and @key.
264 */
265struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
266 struct sockaddr_rxrpc *srx,
267 struct key *key,
268 unsigned long user_call_ID,
269 gfp_t gfp)
270{
271 struct rxrpc_conn_bundle *bundle;
272 struct rxrpc_transport *trans;
273 struct rxrpc_call *call;
274 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
275 __be16 service_id;
276
277 _enter(",,%x,%lx", key_serial(key), user_call_ID);
278
279 lock_sock(&rx->sk);
280
281 if (srx) {
282 trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
283 sizeof(*srx), 0, gfp);
284 if (IS_ERR(trans)) {
David Howellse231c2e2008-02-07 00:15:26 -0800285 call = ERR_CAST(trans);
David Howells651350d2007-04-26 15:50:17 -0700286 trans = NULL;
287 goto out;
288 }
289 } else {
290 trans = rx->trans;
291 if (!trans) {
292 call = ERR_PTR(-ENOTCONN);
293 goto out;
294 }
295 atomic_inc(&trans->usage);
296 }
297
298 service_id = rx->service_id;
299 if (srx)
300 service_id = htons(srx->srx_service);
301
302 if (!key)
303 key = rx->key;
304 if (key && !key->payload.data)
305 key = NULL; /* a no-security key */
306
307 bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp);
308 if (IS_ERR(bundle)) {
David Howellse231c2e2008-02-07 00:15:26 -0800309 call = ERR_CAST(bundle);
David Howells651350d2007-04-26 15:50:17 -0700310 goto out;
311 }
312
313 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
314 gfp);
315 rxrpc_put_bundle(trans, bundle);
316out:
317 rxrpc_put_transport(trans);
318 release_sock(&rx->sk);
319 _leave(" = %p", call);
320 return call;
321}
322
323EXPORT_SYMBOL(rxrpc_kernel_begin_call);
324
325/**
326 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
327 * @call: The call to end
328 *
329 * Allow a kernel service to end a call it was using. The call must be
330 * complete before this is called (the call should be aborted if necessary).
331 */
332void rxrpc_kernel_end_call(struct rxrpc_call *call)
333{
334 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
335 rxrpc_remove_user_ID(call->socket, call);
336 rxrpc_put_call(call);
337}
338
339EXPORT_SYMBOL(rxrpc_kernel_end_call);
340
341/**
342 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
343 * @sock: The socket to intercept received messages on
344 * @interceptor: The function to pass the messages to
345 *
346 * Allow a kernel service to intercept messages heading for the Rx queue on an
347 * RxRPC socket. They get passed to the specified function instead.
348 * @interceptor should free the socket buffers it is given. @interceptor is
349 * called with the socket receive queue spinlock held and softirqs disabled -
350 * this ensures that the messages will be delivered in the right order.
351 */
352void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
353 rxrpc_interceptor_t interceptor)
354{
355 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
356
357 _enter("");
358 rx->interceptor = interceptor;
359}
360
361EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
362
David Howells17926a72007-04-26 15:48:28 -0700363/*
364 * connect an RxRPC socket
365 * - this just targets it at a specific destination; no actual connection
366 * negotiation takes place
367 */
368static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
369 int addr_len, int flags)
370{
371 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
372 struct sock *sk = sock->sk;
373 struct rxrpc_transport *trans;
374 struct rxrpc_local *local;
375 struct rxrpc_sock *rx = rxrpc_sk(sk);
376 int ret;
377
378 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
379
380 ret = rxrpc_validate_address(rx, srx, addr_len);
381 if (ret < 0) {
382 _leave(" = %d [bad addr]", ret);
383 return ret;
384 }
385
386 lock_sock(&rx->sk);
387
388 switch (rx->sk.sk_state) {
389 case RXRPC_UNCONNECTED:
390 /* find a local transport endpoint if we don't have one already */
391 ASSERTCMP(rx->local, ==, NULL);
392 rx->srx.srx_family = AF_RXRPC;
393 rx->srx.srx_service = 0;
394 rx->srx.transport_type = srx->transport_type;
395 rx->srx.transport_len = sizeof(sa_family_t);
396 rx->srx.transport.family = srx->transport.family;
397 local = rxrpc_lookup_local(&rx->srx);
398 if (IS_ERR(local)) {
399 release_sock(&rx->sk);
400 return PTR_ERR(local);
401 }
402 rx->local = local;
403 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
404 case RXRPC_CLIENT_BOUND:
405 break;
406 case RXRPC_CLIENT_CONNECTED:
407 release_sock(&rx->sk);
408 return -EISCONN;
409 default:
410 release_sock(&rx->sk);
411 return -EBUSY; /* server sockets can't connect as well */
412 }
413
David Howells651350d2007-04-26 15:50:17 -0700414 trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
415 GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700416 if (IS_ERR(trans)) {
417 release_sock(&rx->sk);
418 _leave(" = %ld", PTR_ERR(trans));
419 return PTR_ERR(trans);
420 }
421
422 rx->trans = trans;
423 rx->service_id = htons(srx->srx_service);
424 rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
425
426 release_sock(&rx->sk);
427 return 0;
428}
429
430/*
431 * send a message through an RxRPC socket
432 * - in a client this does a number of things:
433 * - finds/sets up a connection for the security specified (if any)
434 * - initiates a call (ID in control data)
435 * - ends the request phase of a call (if MSG_MORE is not set)
436 * - sends a call data packet
437 * - may send an abort (abort code in control data)
438 */
439static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
440 struct msghdr *m, size_t len)
441{
442 struct rxrpc_transport *trans;
443 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
444 int ret;
445
446 _enter(",{%d},,%zu", rx->sk.sk_state, len);
447
448 if (m->msg_flags & MSG_OOB)
449 return -EOPNOTSUPP;
450
451 if (m->msg_name) {
452 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
453 if (ret < 0) {
454 _leave(" = %d [bad addr]", ret);
455 return ret;
456 }
457 }
458
459 trans = NULL;
460 lock_sock(&rx->sk);
461
462 if (m->msg_name) {
463 ret = -EISCONN;
464 trans = rxrpc_name_to_transport(sock, m->msg_name,
David Howells651350d2007-04-26 15:50:17 -0700465 m->msg_namelen, 0, GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700466 if (IS_ERR(trans)) {
467 ret = PTR_ERR(trans);
468 trans = NULL;
469 goto out;
470 }
471 } else {
472 trans = rx->trans;
473 if (trans)
474 atomic_inc(&trans->usage);
475 }
476
477 switch (rx->sk.sk_state) {
478 case RXRPC_SERVER_LISTENING:
479 if (!m->msg_name) {
480 ret = rxrpc_server_sendmsg(iocb, rx, m, len);
481 break;
482 }
483 case RXRPC_SERVER_BOUND:
484 case RXRPC_CLIENT_BOUND:
485 if (!m->msg_name) {
486 ret = -ENOTCONN;
487 break;
488 }
489 case RXRPC_CLIENT_CONNECTED:
490 ret = rxrpc_client_sendmsg(iocb, rx, trans, m, len);
491 break;
492 default:
493 ret = -ENOTCONN;
494 break;
495 }
496
497out:
498 release_sock(&rx->sk);
499 if (trans)
500 rxrpc_put_transport(trans);
501 _leave(" = %d", ret);
502 return ret;
503}
504
505/*
506 * set RxRPC socket options
507 */
508static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
509 char __user *optval, int optlen)
510{
511 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
512 unsigned min_sec_level;
513 int ret;
514
515 _enter(",%d,%d,,%d", level, optname, optlen);
516
517 lock_sock(&rx->sk);
518 ret = -EOPNOTSUPP;
519
520 if (level == SOL_RXRPC) {
521 switch (optname) {
522 case RXRPC_EXCLUSIVE_CONNECTION:
523 ret = -EINVAL;
524 if (optlen != 0)
525 goto error;
526 ret = -EISCONN;
527 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
528 goto error;
529 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
530 goto success;
531
532 case RXRPC_SECURITY_KEY:
533 ret = -EINVAL;
534 if (rx->key)
535 goto error;
536 ret = -EISCONN;
537 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
538 goto error;
539 ret = rxrpc_request_key(rx, optval, optlen);
540 goto error;
541
542 case RXRPC_SECURITY_KEYRING:
543 ret = -EINVAL;
544 if (rx->key)
545 goto error;
546 ret = -EISCONN;
547 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
548 goto error;
549 ret = rxrpc_server_keyring(rx, optval, optlen);
550 goto error;
551
552 case RXRPC_MIN_SECURITY_LEVEL:
553 ret = -EINVAL;
554 if (optlen != sizeof(unsigned))
555 goto error;
556 ret = -EISCONN;
557 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
558 goto error;
559 ret = get_user(min_sec_level,
560 (unsigned __user *) optval);
561 if (ret < 0)
562 goto error;
563 ret = -EINVAL;
564 if (min_sec_level > RXRPC_SECURITY_MAX)
565 goto error;
566 rx->min_sec_level = min_sec_level;
567 goto success;
568
569 default:
570 break;
571 }
572 }
573
574success:
575 ret = 0;
576error:
577 release_sock(&rx->sk);
578 return ret;
579}
580
581/*
582 * permit an RxRPC socket to be polled
583 */
584static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
585 poll_table *wait)
586{
587 unsigned int mask;
588 struct sock *sk = sock->sk;
589
590 poll_wait(file, sk->sk_sleep, wait);
591 mask = 0;
592
593 /* the socket is readable if there are any messages waiting on the Rx
594 * queue */
595 if (!skb_queue_empty(&sk->sk_receive_queue))
596 mask |= POLLIN | POLLRDNORM;
597
598 /* the socket is writable if there is space to add new data to the
599 * socket; there is no guarantee that any particular call in progress
600 * on the socket may have space in the Tx ACK window */
601 if (rxrpc_writable(sk))
602 mask |= POLLOUT | POLLWRNORM;
603
604 return mask;
605}
606
607/*
608 * create an RxRPC socket
609 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700610static int rxrpc_create(struct net *net, struct socket *sock, int protocol)
David Howells17926a72007-04-26 15:48:28 -0700611{
612 struct rxrpc_sock *rx;
613 struct sock *sk;
614
615 _enter("%p,%d", sock, protocol);
616
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700617 if (net != &init_net)
618 return -EAFNOSUPPORT;
619
David Howells17926a72007-04-26 15:48:28 -0700620 /* we support transport protocol UDP only */
621 if (protocol != PF_INET)
622 return -EPROTONOSUPPORT;
623
624 if (sock->type != SOCK_DGRAM)
625 return -ESOCKTNOSUPPORT;
626
627 sock->ops = &rxrpc_rpc_ops;
628 sock->state = SS_UNCONNECTED;
629
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700630 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto);
David Howells17926a72007-04-26 15:48:28 -0700631 if (!sk)
632 return -ENOMEM;
633
634 sock_init_data(sock, sk);
635 sk->sk_state = RXRPC_UNCONNECTED;
636 sk->sk_write_space = rxrpc_write_space;
637 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
638 sk->sk_destruct = rxrpc_sock_destructor;
639
640 rx = rxrpc_sk(sk);
641 rx->proto = protocol;
642 rx->calls = RB_ROOT;
643
644 INIT_LIST_HEAD(&rx->listen_link);
645 INIT_LIST_HEAD(&rx->secureq);
646 INIT_LIST_HEAD(&rx->acceptq);
647 rwlock_init(&rx->call_lock);
648 memset(&rx->srx, 0, sizeof(rx->srx));
649
650 _leave(" = 0 [%p]", rx);
651 return 0;
652}
653
654/*
655 * RxRPC socket destructor
656 */
657static void rxrpc_sock_destructor(struct sock *sk)
658{
659 _enter("%p", sk);
660
661 rxrpc_purge_queue(&sk->sk_receive_queue);
662
663 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
664 BUG_TRAP(sk_unhashed(sk));
665 BUG_TRAP(!sk->sk_socket);
666
667 if (!sock_flag(sk, SOCK_DEAD)) {
668 printk("Attempt to release alive rxrpc socket: %p\n", sk);
669 return;
670 }
671}
672
673/*
674 * release an RxRPC socket
675 */
676static int rxrpc_release_sock(struct sock *sk)
677{
678 struct rxrpc_sock *rx = rxrpc_sk(sk);
679
680 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
681
682 /* declare the socket closed for business */
683 sock_orphan(sk);
684 sk->sk_shutdown = SHUTDOWN_MASK;
685
686 spin_lock_bh(&sk->sk_receive_queue.lock);
687 sk->sk_state = RXRPC_CLOSE;
688 spin_unlock_bh(&sk->sk_receive_queue.lock);
689
690 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
691
692 if (!list_empty(&rx->listen_link)) {
693 write_lock_bh(&rx->local->services_lock);
694 list_del(&rx->listen_link);
695 write_unlock_bh(&rx->local->services_lock);
696 }
697
698 /* try to flush out this socket */
699 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700700 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700701 rxrpc_purge_queue(&sk->sk_receive_queue);
702
703 if (rx->conn) {
704 rxrpc_put_connection(rx->conn);
705 rx->conn = NULL;
706 }
707
708 if (rx->bundle) {
709 rxrpc_put_bundle(rx->trans, rx->bundle);
710 rx->bundle = NULL;
711 }
712 if (rx->trans) {
713 rxrpc_put_transport(rx->trans);
714 rx->trans = NULL;
715 }
716 if (rx->local) {
717 rxrpc_put_local(rx->local);
718 rx->local = NULL;
719 }
720
721 key_put(rx->key);
722 rx->key = NULL;
723 key_put(rx->securities);
724 rx->securities = NULL;
725 sock_put(sk);
726
727 _leave(" = 0");
728 return 0;
729}
730
731/*
732 * release an RxRPC BSD socket on close() or equivalent
733 */
734static int rxrpc_release(struct socket *sock)
735{
736 struct sock *sk = sock->sk;
737
738 _enter("%p{%p}", sock, sk);
739
740 if (!sk)
741 return 0;
742
743 sock->sk = NULL;
744
745 return rxrpc_release_sock(sk);
746}
747
748/*
749 * RxRPC network protocol
750 */
751static const struct proto_ops rxrpc_rpc_ops = {
752 .family = PF_UNIX,
753 .owner = THIS_MODULE,
754 .release = rxrpc_release,
755 .bind = rxrpc_bind,
756 .connect = rxrpc_connect,
757 .socketpair = sock_no_socketpair,
758 .accept = sock_no_accept,
759 .getname = sock_no_getname,
760 .poll = rxrpc_poll,
761 .ioctl = sock_no_ioctl,
762 .listen = rxrpc_listen,
763 .shutdown = sock_no_shutdown,
764 .setsockopt = rxrpc_setsockopt,
765 .getsockopt = sock_no_getsockopt,
766 .sendmsg = rxrpc_sendmsg,
767 .recvmsg = rxrpc_recvmsg,
768 .mmap = sock_no_mmap,
769 .sendpage = sock_no_sendpage,
770};
771
772static struct proto rxrpc_proto = {
773 .name = "RXRPC",
774 .owner = THIS_MODULE,
775 .obj_size = sizeof(struct rxrpc_sock),
776 .max_header = sizeof(struct rxrpc_header),
777};
778
779static struct net_proto_family rxrpc_family_ops = {
780 .family = PF_RXRPC,
781 .create = rxrpc_create,
782 .owner = THIS_MODULE,
783};
784
785/*
786 * initialise and register the RxRPC protocol
787 */
788static int __init af_rxrpc_init(void)
789{
790 struct sk_buff *dummy_skb;
791 int ret = -1;
792
793 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof(dummy_skb->cb));
794
john stultz2c6b47d2007-07-24 17:47:43 -0700795 rxrpc_epoch = htonl(get_seconds());
David Howells17926a72007-04-26 15:48:28 -0700796
David Howells651350d2007-04-26 15:50:17 -0700797 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700798 rxrpc_call_jar = kmem_cache_create(
799 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900800 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700801 if (!rxrpc_call_jar) {
802 printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700803 goto error_call_jar;
804 }
805
David Howells651350d2007-04-26 15:50:17 -0700806 rxrpc_workqueue = create_workqueue("krxrpcd");
807 if (!rxrpc_workqueue) {
808 printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n");
809 goto error_work_queue;
810 }
811
David Howells17926a72007-04-26 15:48:28 -0700812 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900813 if (ret < 0) {
814 printk(KERN_CRIT "RxRPC: Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700815 goto error_proto;
816 }
817
818 ret = sock_register(&rxrpc_family_ops);
819 if (ret < 0) {
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900820 printk(KERN_CRIT "RxRPC: Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700821 goto error_sock;
822 }
823
824 ret = register_key_type(&key_type_rxrpc);
825 if (ret < 0) {
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900826 printk(KERN_CRIT "RxRPC: Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700827 goto error_key_type;
828 }
829
830 ret = register_key_type(&key_type_rxrpc_s);
831 if (ret < 0) {
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900832 printk(KERN_CRIT "RxRPC: Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700833 goto error_key_type_s;
834 }
835
836#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200837 proc_net_fops_create(&init_net, "rxrpc_calls", 0, &rxrpc_call_seq_fops);
838 proc_net_fops_create(&init_net, "rxrpc_conns", 0, &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700839#endif
840 return 0;
841
842error_key_type_s:
843 unregister_key_type(&key_type_rxrpc);
844error_key_type:
845 sock_unregister(PF_RXRPC);
846error_sock:
847 proto_unregister(&rxrpc_proto);
848error_proto:
David Howells651350d2007-04-26 15:50:17 -0700849 destroy_workqueue(rxrpc_workqueue);
850error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700851 kmem_cache_destroy(rxrpc_call_jar);
852error_call_jar:
853 return ret;
854}
855
856/*
857 * unregister the RxRPC protocol
858 */
859static void __exit af_rxrpc_exit(void)
860{
861 _enter("");
862 unregister_key_type(&key_type_rxrpc_s);
863 unregister_key_type(&key_type_rxrpc);
864 sock_unregister(PF_RXRPC);
865 proto_unregister(&rxrpc_proto);
866 rxrpc_destroy_all_calls();
867 rxrpc_destroy_all_connections();
868 rxrpc_destroy_all_transports();
869 rxrpc_destroy_all_peers();
870 rxrpc_destroy_all_locals();
871
872 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
873
874 _debug("flush scheduled work");
David Howells651350d2007-04-26 15:50:17 -0700875 flush_workqueue(rxrpc_workqueue);
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200876 proc_net_remove(&init_net, "rxrpc_conns");
877 proc_net_remove(&init_net, "rxrpc_calls");
David Howells651350d2007-04-26 15:50:17 -0700878 destroy_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700879 kmem_cache_destroy(rxrpc_call_jar);
880 _leave("");
881}
882
883module_init(af_rxrpc_init);
884module_exit(af_rxrpc_exit);