af_rxrpc.c 20.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* AF_RXRPC implementation
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14
#include <linux/module.h>
15
#include <linux/kernel.h>
16
#include <linux/net.h>
17
#include <linux/slab.h>
18 19 20
#include <linux/skbuff.h>
#include <linux/poll.h>
#include <linux/proc_fs.h>
21
#include <linux/key-type.h>
22
#include <net/net_namespace.h>
23 24 25 26 27 28 29 30 31
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include "ar-internal.h"

MODULE_DESCRIPTION("RxRPC network protocol");
MODULE_AUTHOR("Red Hat, Inc.");
MODULE_LICENSE("GPL");
MODULE_ALIAS_NETPROTO(PF_RXRPC);

32
unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
33
module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
34
MODULE_PARM_DESC(debug, "RxRPC debugging mask");
35 36 37 38 39

static struct proto rxrpc_proto;
static const struct proto_ops rxrpc_rpc_ops;

/* local epoch for detecting local-end reset */
40
u32 rxrpc_epoch;
41 42 43 44 45 46 47

/* current debugging ID */
atomic_t rxrpc_debug_id;

/* count of skbs currently in use */
atomic_t rxrpc_n_skbs;

48 49
struct workqueue_struct *rxrpc_workqueue;

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static void rxrpc_sock_destructor(struct sock *);

/*
 * see if an RxRPC socket is currently writable
 */
static inline int rxrpc_writable(struct sock *sk)
{
	return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
}

/*
 * wait for write bufferage to become available
 */
static void rxrpc_write_space(struct sock *sk)
{
	_enter("%p", sk);
66
	rcu_read_lock();
67
	if (rxrpc_writable(sk)) {
68 69
		struct socket_wq *wq = rcu_dereference(sk->sk_wq);

H
Herbert Xu 已提交
70
		if (skwq_has_sleeper(wq))
71
			wake_up_interruptible(&wq->wait);
72
		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
73
	}
74
	rcu_read_unlock();
75 76 77 78 79 80 81 82 83
}

/*
 * validate an RxRPC address
 */
static int rxrpc_validate_address(struct rxrpc_sock *rx,
				  struct sockaddr_rxrpc *srx,
				  int len)
{
84
	unsigned int tail;
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if (len < sizeof(struct sockaddr_rxrpc))
		return -EINVAL;

	if (srx->srx_family != AF_RXRPC)
		return -EAFNOSUPPORT;

	if (srx->transport_type != SOCK_DGRAM)
		return -ESOCKTNOSUPPORT;

	len -= offsetof(struct sockaddr_rxrpc, transport);
	if (srx->transport_len < sizeof(sa_family_t) ||
	    srx->transport_len > len)
		return -EINVAL;

100
	if (srx->transport.family != rx->family)
101 102 103 104
		return -EAFNOSUPPORT;

	switch (srx->transport.family) {
	case AF_INET:
105 106
		if (srx->transport_len < sizeof(struct sockaddr_in))
			return -EINVAL;
H
Harvey Harrison 已提交
107
		_debug("INET: %x @ %pI4",
108
		       ntohs(srx->transport.sin.sin_port),
H
Harvey Harrison 已提交
109
		       &srx->transport.sin.sin_addr);
110
		tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
111 112 113 114 115 116 117
		break;

	case AF_INET6:
	default:
		return -EAFNOSUPPORT;
	}

118 119
	if (tail < len)
		memset((void *)srx + tail, 0, len - tail);
120 121 122 123 124 125 126 127
	return 0;
}

/*
 * bind a local address to an RxRPC socket
 */
static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
{
128
	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
129 130 131 132 133 134 135 136 137 138 139 140 141
	struct sock *sk = sock->sk;
	struct rxrpc_local *local;
	struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
	int ret;

	_enter("%p,%p,%d", rx, saddr, len);

	ret = rxrpc_validate_address(rx, srx, len);
	if (ret < 0)
		goto error;

	lock_sock(&rx->sk);

142
	if (rx->sk.sk_state != RXRPC_UNBOUND) {
143 144 145 146 147 148 149 150 151 152 153 154
		ret = -EINVAL;
		goto error_unlock;
	}

	memcpy(&rx->srx, srx, sizeof(rx->srx));

	local = rxrpc_lookup_local(&rx->srx);
	if (IS_ERR(local)) {
		ret = PTR_ERR(local);
		goto error_unlock;
	}

155
	if (rx->srx.srx_service) {
156 157
		write_lock_bh(&local->services_lock);
		list_for_each_entry(prx, &local->services, listen_link) {
158
			if (prx->srx.srx_service == rx->srx.srx_service)
159 160 161
				goto service_in_use;
		}

162
		rx->local = local;
163 164 165 166 167
		list_add_tail(&rx->listen_link, &local->services);
		write_unlock_bh(&local->services_lock);

		rx->sk.sk_state = RXRPC_SERVER_BOUND;
	} else {
168
		rx->local = local;
169 170 171 172 173 174 175 176 177
		rx->sk.sk_state = RXRPC_CLIENT_BOUND;
	}

	release_sock(&rx->sk);
	_leave(" = 0");
	return 0;

service_in_use:
	write_unlock_bh(&local->services_lock);
178 179
	rxrpc_put_local(local);
	ret = -EADDRINUSE;
180 181 182 183 184 185 186 187 188 189 190 191 192 193
error_unlock:
	release_sock(&rx->sk);
error:
	_leave(" = %d", ret);
	return ret;
}

/*
 * set the number of pending calls permitted on a listening socket
 */
static int rxrpc_listen(struct socket *sock, int backlog)
{
	struct sock *sk = sock->sk;
	struct rxrpc_sock *rx = rxrpc_sk(sk);
194
	unsigned int max;
195 196 197 198 199 200 201
	int ret;

	_enter("%p,%d", rx, backlog);

	lock_sock(&rx->sk);

	switch (rx->sk.sk_state) {
202
	case RXRPC_UNBOUND:
203 204 205 206
		ret = -EADDRNOTAVAIL;
		break;
	case RXRPC_SERVER_BOUND:
		ASSERT(rx->local != NULL);
207 208 209 210 211 212
		max = READ_ONCE(rxrpc_max_backlog);
		ret = -EINVAL;
		if (backlog == INT_MAX)
			backlog = max;
		else if (backlog < 0 || backlog > max)
			break;
213 214 215 216
		sk->sk_max_ack_backlog = backlog;
		rx->sk.sk_state = RXRPC_SERVER_LISTENING;
		ret = 0;
		break;
217 218 219
	default:
		ret = -EBUSY;
		break;
220 221 222 223 224 225 226 227 228 229
	}

	release_sock(&rx->sk);
	_leave(" = %d", ret);
	return ret;
}

/*
 * find a transport by address
 */
230 231 232 233 234
struct rxrpc_transport *
rxrpc_name_to_transport(struct rxrpc_conn_parameters *cp,
			struct sockaddr *addr,
			int addr_len,
			gfp_t gfp)
235 236 237 238
{
	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
	struct rxrpc_transport *trans;

239
	_enter("%p,%d", addr, addr_len);
240

241
	if (cp->local->srx.transport_type != srx->transport_type)
242
		return ERR_PTR(-ESOCKTNOSUPPORT);
243
	if (cp->local->srx.transport.family != srx->transport.family)
244 245 246
		return ERR_PTR(-EAFNOSUPPORT);

	/* find a remote transport endpoint from the local one */
247 248
	cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
	if (!cp->peer)
249
		return ERR_PTR(-ENOMEM);
250 251

	/* find a transport */
252 253
	trans = rxrpc_get_transport(cp->local, cp->peer, gfp);
	rxrpc_put_peer(cp->peer);
254 255 256 257
	_leave(" = %p", trans);
	return trans;
}

258 259 260
/**
 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
 * @sock: The socket on which to make the call
261
 * @srx: The address of the peer to contact
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
 * @key: The security context to use (defaults to socket setting)
 * @user_call_ID: The ID to use
 *
 * Allow a kernel service to begin a call on the nominated socket.  This just
 * sets up all the internal tracking structures and allocates connection and
 * call IDs as appropriate.  The call to be used is returned.
 *
 * The default socket destination address and security may be overridden by
 * supplying @srx and @key.
 */
struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
					   struct sockaddr_rxrpc *srx,
					   struct key *key,
					   unsigned long user_call_ID,
					   gfp_t gfp)
{
278
	struct rxrpc_conn_parameters cp;
279 280 281
	struct rxrpc_transport *trans;
	struct rxrpc_call *call;
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
282
	int ret;
283 284 285

	_enter(",,%x,%lx", key_serial(key), user_call_ID);

286 287 288 289
	ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
	if (ret < 0)
		return ERR_PTR(ret);

290 291
	lock_sock(&rx->sk);

292 293 294 295 296 297 298 299 300 301 302 303 304 305
	if (!key)
		key = rx->key;
	if (key && !key->payload.data[0])
		key = NULL; /* a no-security key */

	memset(&cp, 0, sizeof(cp));
	cp.local		= rx->local;
	cp.key			= key;
	cp.security_level	= 0;
	cp.exclusive		= false;
	cp.service_id		= srx->srx_service;

	trans = rxrpc_name_to_transport(&cp, (struct sockaddr *)srx,
					sizeof(*srx), gfp);
306 307 308 309
	if (IS_ERR(trans)) {
		call = ERR_CAST(trans);
		trans = NULL;
		goto out_notrans;
310
	}
311
	cp.peer = trans->peer;
312

313
	call = rxrpc_new_client_call(rx, &cp, trans, srx, user_call_ID, gfp);
314
	rxrpc_put_transport(trans);
315
out_notrans:
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	release_sock(&rx->sk);
	_leave(" = %p", call);
	return call;
}
EXPORT_SYMBOL(rxrpc_kernel_begin_call);

/**
 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
 * @call: The call to end
 *
 * Allow a kernel service to end a call it was using.  The call must be
 * complete before this is called (the call should be aborted if necessary).
 */
void rxrpc_kernel_end_call(struct rxrpc_call *call)
{
	_enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
	rxrpc_remove_user_ID(call->socket, call);
	rxrpc_put_call(call);
}
EXPORT_SYMBOL(rxrpc_kernel_end_call);

/**
 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
 * @sock: The socket to intercept received messages on
 * @interceptor: The function to pass the messages to
 *
 * Allow a kernel service to intercept messages heading for the Rx queue on an
 * RxRPC socket.  They get passed to the specified function instead.
 * @interceptor should free the socket buffers it is given.  @interceptor is
 * called with the socket receive queue spinlock held and softirqs disabled -
 * this ensures that the messages will be delivered in the right order.
 */
void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
					rxrpc_interceptor_t interceptor)
{
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);

	_enter("");
	rx->interceptor = interceptor;
}

EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);

359 360 361 362 363 364 365 366
/*
 * connect an RxRPC socket
 * - this just targets it at a specific destination; no actual connection
 *   negotiation takes place
 */
static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
			 int addr_len, int flags)
{
367 368
	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
369 370 371 372 373 374 375 376 377 378 379 380
	int ret;

	_enter("%p,%p,%d,%d", rx, addr, addr_len, flags);

	ret = rxrpc_validate_address(rx, srx, addr_len);
	if (ret < 0) {
		_leave(" = %d [bad addr]", ret);
		return ret;
	}

	lock_sock(&rx->sk);

381 382 383 384
	ret = -EISCONN;
	if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
		goto error;

385
	switch (rx->sk.sk_state) {
386 387 388
	case RXRPC_UNBOUND:
		rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
	case RXRPC_CLIENT_UNBOUND:
389 390 391
	case RXRPC_CLIENT_BOUND:
		break;
	default:
392 393
		ret = -EBUSY;
		goto error;
394 395
	}

396 397 398
	rx->connect_srx = *srx;
	set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
	ret = 0;
399

400
error:
401
	release_sock(&rx->sk);
402
	return ret;
403 404 405 406 407 408 409 410 411 412 413
}

/*
 * send a message through an RxRPC socket
 * - in a client this does a number of things:
 *   - finds/sets up a connection for the security specified (if any)
 *   - initiates a call (ID in control data)
 *   - ends the request phase of a call (if MSG_MORE is not set)
 *   - sends a call data packet
 *   - may send an abort (abort code in control data)
 */
414
static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
415
{
416
	struct rxrpc_local *local;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
	int ret;

	_enter(",{%d},,%zu", rx->sk.sk_state, len);

	if (m->msg_flags & MSG_OOB)
		return -EOPNOTSUPP;

	if (m->msg_name) {
		ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
		if (ret < 0) {
			_leave(" = %d [bad addr]", ret);
			return ret;
		}
	}

	lock_sock(&rx->sk);

	switch (rx->sk.sk_state) {
436 437 438 439 440
	case RXRPC_UNBOUND:
		local = rxrpc_lookup_local(&rx->srx);
		if (IS_ERR(local)) {
			ret = PTR_ERR(local);
			goto error_unlock;
441
		}
442 443 444 445 446 447

		rx->local = local;
		rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
		/* Fall through */

	case RXRPC_CLIENT_UNBOUND:
448
	case RXRPC_CLIENT_BOUND:
449 450 451 452
		if (!m->msg_name &&
		    test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
			m->msg_name = &rx->connect_srx;
			m->msg_namelen = sizeof(rx->connect_srx);
453
		}
454 455 456
	case RXRPC_SERVER_BOUND:
	case RXRPC_SERVER_LISTENING:
		ret = rxrpc_do_sendmsg(rx, m, len);
457 458
		break;
	default:
459
		ret = -EINVAL;
460 461 462
		break;
	}

463
error_unlock:
464 465 466 467 468 469 470 471 472
	release_sock(&rx->sk);
	_leave(" = %d", ret);
	return ret;
}

/*
 * set RxRPC socket options
 */
static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
473
			    char __user *optval, unsigned int optlen)
474 475
{
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
476
	unsigned int min_sec_level;
477 478 479 480 481 482 483 484 485 486 487 488 489 490
	int ret;

	_enter(",%d,%d,,%d", level, optname, optlen);

	lock_sock(&rx->sk);
	ret = -EOPNOTSUPP;

	if (level == SOL_RXRPC) {
		switch (optname) {
		case RXRPC_EXCLUSIVE_CONNECTION:
			ret = -EINVAL;
			if (optlen != 0)
				goto error;
			ret = -EISCONN;
491
			if (rx->sk.sk_state != RXRPC_UNBOUND)
492
				goto error;
493
			rx->exclusive = true;
494 495 496 497 498 499 500
			goto success;

		case RXRPC_SECURITY_KEY:
			ret = -EINVAL;
			if (rx->key)
				goto error;
			ret = -EISCONN;
501
			if (rx->sk.sk_state != RXRPC_UNBOUND)
502 503 504 505 506 507 508 509 510
				goto error;
			ret = rxrpc_request_key(rx, optval, optlen);
			goto error;

		case RXRPC_SECURITY_KEYRING:
			ret = -EINVAL;
			if (rx->key)
				goto error;
			ret = -EISCONN;
511
			if (rx->sk.sk_state != RXRPC_UNBOUND)
512 513 514 515 516 517
				goto error;
			ret = rxrpc_server_keyring(rx, optval, optlen);
			goto error;

		case RXRPC_MIN_SECURITY_LEVEL:
			ret = -EINVAL;
518
			if (optlen != sizeof(unsigned int))
519 520
				goto error;
			ret = -EISCONN;
521
			if (rx->sk.sk_state != RXRPC_UNBOUND)
522 523
				goto error;
			ret = get_user(min_sec_level,
524
				       (unsigned int __user *) optval);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
			if (ret < 0)
				goto error;
			ret = -EINVAL;
			if (min_sec_level > RXRPC_SECURITY_MAX)
				goto error;
			rx->min_sec_level = min_sec_level;
			goto success;

		default:
			break;
		}
	}

success:
	ret = 0;
error:
	release_sock(&rx->sk);
	return ret;
}

/*
 * permit an RxRPC socket to be polled
 */
static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
			       poll_table *wait)
{
	unsigned int mask;
	struct sock *sk = sock->sk;

E
Eric Dumazet 已提交
554
	sock_poll_wait(file, sk_sleep(sk), wait);
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	mask = 0;

	/* the socket is readable if there are any messages waiting on the Rx
	 * queue */
	if (!skb_queue_empty(&sk->sk_receive_queue))
		mask |= POLLIN | POLLRDNORM;

	/* the socket is writable if there is space to add new data to the
	 * socket; there is no guarantee that any particular call in progress
	 * on the socket may have space in the Tx ACK window */
	if (rxrpc_writable(sk))
		mask |= POLLOUT | POLLWRNORM;

	return mask;
}

/*
 * create an RxRPC socket
 */
574 575
static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
			int kern)
576 577 578 579 580 581
{
	struct rxrpc_sock *rx;
	struct sock *sk;

	_enter("%p,%d", sock, protocol);

O
Octavian Purdila 已提交
582
	if (!net_eq(net, &init_net))
583 584
		return -EAFNOSUPPORT;

585
	/* we support transport protocol UDP/UDP6 only */
586 587 588 589 590 591 592 593 594
	if (protocol != PF_INET)
		return -EPROTONOSUPPORT;

	if (sock->type != SOCK_DGRAM)
		return -ESOCKTNOSUPPORT;

	sock->ops = &rxrpc_rpc_ops;
	sock->state = SS_UNCONNECTED;

595
	sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
596 597 598 599
	if (!sk)
		return -ENOMEM;

	sock_init_data(sock, sk);
600
	sk->sk_state		= RXRPC_UNBOUND;
601
	sk->sk_write_space	= rxrpc_write_space;
602
	sk->sk_max_ack_backlog	= 0;
603 604 605
	sk->sk_destruct		= rxrpc_sock_destructor;

	rx = rxrpc_sk(sk);
606
	rx->family = protocol;
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	rx->calls = RB_ROOT;

	INIT_LIST_HEAD(&rx->listen_link);
	INIT_LIST_HEAD(&rx->secureq);
	INIT_LIST_HEAD(&rx->acceptq);
	rwlock_init(&rx->call_lock);
	memset(&rx->srx, 0, sizeof(rx->srx));

	_leave(" = 0 [%p]", rx);
	return 0;
}

/*
 * RxRPC socket destructor
 */
static void rxrpc_sock_destructor(struct sock *sk)
{
	_enter("%p", sk);

	rxrpc_purge_queue(&sk->sk_receive_queue);

628 629 630
	WARN_ON(atomic_read(&sk->sk_wmem_alloc));
	WARN_ON(!sk_unhashed(sk));
	WARN_ON(sk->sk_socket);
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

	if (!sock_flag(sk, SOCK_DEAD)) {
		printk("Attempt to release alive rxrpc socket: %p\n", sk);
		return;
	}
}

/*
 * release an RxRPC socket
 */
static int rxrpc_release_sock(struct sock *sk)
{
	struct rxrpc_sock *rx = rxrpc_sk(sk);

	_enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));

	/* declare the socket closed for business */
	sock_orphan(sk);
	sk->sk_shutdown = SHUTDOWN_MASK;

	spin_lock_bh(&sk->sk_receive_queue.lock);
	sk->sk_state = RXRPC_CLOSE;
	spin_unlock_bh(&sk->sk_receive_queue.lock);

	ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);

	if (!list_empty(&rx->listen_link)) {
		write_lock_bh(&rx->local->services_lock);
		list_del(&rx->listen_link);
		write_unlock_bh(&rx->local->services_lock);
	}

	/* try to flush out this socket */
	rxrpc_release_calls_on_socket(rx);
665
	flush_workqueue(rxrpc_workqueue);
666 667
	rxrpc_purge_queue(&sk->sk_receive_queue);

668 669
	rxrpc_put_local(rx->local);
	rx->local = NULL;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	key_put(rx->key);
	rx->key = NULL;
	key_put(rx->securities);
	rx->securities = NULL;
	sock_put(sk);

	_leave(" = 0");
	return 0;
}

/*
 * release an RxRPC BSD socket on close() or equivalent
 */
static int rxrpc_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	_enter("%p{%p}", sock, sk);

	if (!sk)
		return 0;

	sock->sk = NULL;

	return rxrpc_release_sock(sk);
}

/*
 * RxRPC network protocol
 */
static const struct proto_ops rxrpc_rpc_ops = {
701
	.family		= PF_RXRPC,
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	.owner		= THIS_MODULE,
	.release	= rxrpc_release,
	.bind		= rxrpc_bind,
	.connect	= rxrpc_connect,
	.socketpair	= sock_no_socketpair,
	.accept		= sock_no_accept,
	.getname	= sock_no_getname,
	.poll		= rxrpc_poll,
	.ioctl		= sock_no_ioctl,
	.listen		= rxrpc_listen,
	.shutdown	= sock_no_shutdown,
	.setsockopt	= rxrpc_setsockopt,
	.getsockopt	= sock_no_getsockopt,
	.sendmsg	= rxrpc_sendmsg,
	.recvmsg	= rxrpc_recvmsg,
	.mmap		= sock_no_mmap,
	.sendpage	= sock_no_sendpage,
};

static struct proto rxrpc_proto = {
	.name		= "RXRPC",
	.owner		= THIS_MODULE,
	.obj_size	= sizeof(struct rxrpc_sock),
725
	.max_header	= sizeof(struct rxrpc_wire_header),
726 727
};

728
static const struct net_proto_family rxrpc_family_ops = {
729 730 731 732 733 734 735 736 737 738 739 740
	.family	= PF_RXRPC,
	.create = rxrpc_create,
	.owner	= THIS_MODULE,
};

/*
 * initialise and register the RxRPC protocol
 */
static int __init af_rxrpc_init(void)
{
	int ret = -1;

741
	BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
742

743
	rxrpc_epoch = get_seconds();
744

745
	ret = -ENOMEM;
746 747
	rxrpc_call_jar = kmem_cache_create(
		"rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
748
		SLAB_HWCACHE_ALIGN, NULL);
749
	if (!rxrpc_call_jar) {
750
		pr_notice("Failed to allocate call jar\n");
751 752 753
		goto error_call_jar;
	}

754
	rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
755
	if (!rxrpc_workqueue) {
756
		pr_notice("Failed to allocate work queue\n");
757 758 759
		goto error_work_queue;
	}

760 761
	ret = rxrpc_init_security();
	if (ret < 0) {
762
		pr_crit("Cannot initialise security\n");
763 764 765
		goto error_security;
	}

766
	ret = proto_register(&rxrpc_proto, 1);
767
	if (ret < 0) {
768
		pr_crit("Cannot register protocol\n");
769 770 771 772 773
		goto error_proto;
	}

	ret = sock_register(&rxrpc_family_ops);
	if (ret < 0) {
774
		pr_crit("Cannot register socket family\n");
775 776 777 778 779
		goto error_sock;
	}

	ret = register_key_type(&key_type_rxrpc);
	if (ret < 0) {
780
		pr_crit("Cannot register client key type\n");
781 782 783 784 785
		goto error_key_type;
	}

	ret = register_key_type(&key_type_rxrpc_s);
	if (ret < 0) {
786
		pr_crit("Cannot register server key type\n");
787 788 789
		goto error_key_type_s;
	}

790 791
	ret = rxrpc_sysctl_init();
	if (ret < 0) {
792
		pr_crit("Cannot register sysctls\n");
793 794 795
		goto error_sysctls;
	}

796
#ifdef CONFIG_PROC_FS
797 798 799
	proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
	proc_create("rxrpc_conns", 0, init_net.proc_net,
		    &rxrpc_connection_seq_fops);
800 801 802
#endif
	return 0;

803 804
error_sysctls:
	unregister_key_type(&key_type_rxrpc_s);
805 806 807 808 809 810 811
error_key_type_s:
	unregister_key_type(&key_type_rxrpc);
error_key_type:
	sock_unregister(PF_RXRPC);
error_sock:
	proto_unregister(&rxrpc_proto);
error_proto:
812
	destroy_workqueue(rxrpc_workqueue);
813 814
error_security:
	rxrpc_exit_security();
815
error_work_queue:
816 817 818 819 820 821 822 823 824 825 826
	kmem_cache_destroy(rxrpc_call_jar);
error_call_jar:
	return ret;
}

/*
 * unregister the RxRPC protocol
 */
static void __exit af_rxrpc_exit(void)
{
	_enter("");
827
	rxrpc_sysctl_exit();
828 829 830 831 832 833 834 835 836 837
	unregister_key_type(&key_type_rxrpc_s);
	unregister_key_type(&key_type_rxrpc);
	sock_unregister(PF_RXRPC);
	proto_unregister(&rxrpc_proto);
	rxrpc_destroy_all_calls();
	rxrpc_destroy_all_connections();
	rxrpc_destroy_all_transports();

	ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);

838 839 840 841 842 843 844 845 846
	/* We need to flush the scheduled work twice because the local endpoint
	 * records involve a work item in their destruction as they can only be
	 * destroyed from process context.  However, a connection may have a
	 * work item outstanding - and this will pin the local endpoint record
	 * until the connection goes away.
	 *
	 * Peers don't pin locals and calls pin sockets - which prevents the
	 * module from being unloaded - so we should only need two flushes.
	 */
847
	_debug("flush scheduled work");
848
	flush_workqueue(rxrpc_workqueue);
849 850 851 852 853
	_debug("flush scheduled work 2");
	flush_workqueue(rxrpc_workqueue);
	_debug("synchronise RCU");
	rcu_barrier();
	_debug("destroy locals");
854 855
	ASSERT(idr_is_empty(&rxrpc_client_conn_ids));
	idr_destroy(&rxrpc_client_conn_ids);
856 857
	rxrpc_destroy_all_locals();

858 859
	remove_proc_entry("rxrpc_conns", init_net.proc_net);
	remove_proc_entry("rxrpc_calls", init_net.proc_net);
860
	destroy_workqueue(rxrpc_workqueue);
861
	rxrpc_exit_security();
862 863 864 865 866 867
	kmem_cache_destroy(rxrpc_call_jar);
	_leave("");
}

module_init(af_rxrpc_init);
module_exit(af_rxrpc_exit);