af_rxrpc.c 26.2 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
#include <linux/skbuff.h>
19
#include <linux/random.h>
20 21
#include <linux/poll.h>
#include <linux/proc_fs.h>
22
#include <linux/key-type.h>
23
#include <net/net_namespace.h>
24 25
#include <net/sock.h>
#include <net/af_rxrpc.h>
26
#define CREATE_TRACE_POINTS
27 28 29 30 31 32 33
#include "ar-internal.h"

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

34
unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
35
module_param_named(debug, rxrpc_debug, uint, 0644);
36
MODULE_PARM_DESC(debug, "RxRPC debugging mask");
37 38 39 40 41 42

static struct proto rxrpc_proto;
static const struct proto_ops rxrpc_rpc_ops;

/* current debugging ID */
atomic_t rxrpc_debug_id;
43
EXPORT_SYMBOL(rxrpc_debug_id);
44 45

/* count of skbs currently in use */
D
David Howells 已提交
46
atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
47

48 49
struct workqueue_struct *rxrpc_workqueue;

50 51 52 53 54 55 56
static void rxrpc_sock_destructor(struct sock *);

/*
 * see if an RxRPC socket is currently writable
 */
static inline int rxrpc_writable(struct sock *sk)
{
57
	return refcount_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
58 59 60 61 62 63 64 65
}

/*
 * 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;
107
		tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
108 109
		break;

110
#ifdef CONFIG_AF_RXRPC_IPV6
111
	case AF_INET6:
D
David Howells 已提交
112 113 114 115 116
		if (srx->transport_len < sizeof(struct sockaddr_in6))
			return -EINVAL;
		tail = offsetof(struct sockaddr_rxrpc, transport) +
			sizeof(struct sockaddr_in6);
		break;
117
#endif
D
David Howells 已提交
118

119 120 121 122
	default:
		return -EAFNOSUPPORT;
	}

123 124
	if (tail < len)
		memset((void *)srx + tail, 0, len - tail);
D
David Howells 已提交
125
	_debug("INET: %pISp", &srx->transport);
126 127 128 129 130 131 132 133
	return 0;
}

/*
 * bind a local address to an RxRPC socket
 */
static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
{
134
	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
135
	struct rxrpc_local *local;
136
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
137
	u16 service_id = srx->srx_service;
138 139 140 141 142 143 144 145 146 147
	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);

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	switch (rx->sk.sk_state) {
	case RXRPC_UNBOUND:
		rx->srx = *srx;
		local = rxrpc_lookup_local(sock_net(&rx->sk), &rx->srx);
		if (IS_ERR(local)) {
			ret = PTR_ERR(local);
			goto error_unlock;
		}

		if (service_id) {
			write_lock(&local->services_lock);
			if (rcu_access_pointer(local->service))
				goto service_in_use;
			rx->local = local;
			rcu_assign_pointer(local->service, rx);
			write_unlock(&local->services_lock);

			rx->sk.sk_state = RXRPC_SERVER_BOUND;
		} else {
			rx->local = local;
			rx->sk.sk_state = RXRPC_CLIENT_BOUND;
		}
		break;
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185
	case RXRPC_SERVER_BOUND:
		ret = -EINVAL;
		if (service_id == 0)
			goto error_unlock;
		ret = -EADDRINUSE;
		if (service_id == rx->srx.srx_service)
			goto error_unlock;
		ret = -EINVAL;
		srx->srx_service = rx->srx.srx_service;
		if (memcmp(srx, &rx->srx, sizeof(*srx)) != 0)
			goto error_unlock;
		rx->second_service = service_id;
		rx->sk.sk_state = RXRPC_SERVER_BOUND2;
		break;
186

187 188
	default:
		ret = -EINVAL;
189 190 191 192 193 194 195 196
		goto error_unlock;
	}

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

service_in_use:
197
	write_unlock(&local->services_lock);
198 199
	rxrpc_put_local(local);
	ret = -EADDRINUSE;
200 201 202 203 204 205 206 207 208 209 210 211 212 213
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);
214
	unsigned int max, old;
215 216 217 218 219 220 221
	int ret;

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

	lock_sock(&rx->sk);

	switch (rx->sk.sk_state) {
222
	case RXRPC_UNBOUND:
223 224 225
		ret = -EADDRNOTAVAIL;
		break;
	case RXRPC_SERVER_BOUND:
226
	case RXRPC_SERVER_BOUND2:
227
		ASSERT(rx->local != NULL);
228 229 230 231 232 233
		max = READ_ONCE(rxrpc_max_backlog);
		ret = -EINVAL;
		if (backlog == INT_MAX)
			backlog = max;
		else if (backlog < 0 || backlog > max)
			break;
234
		old = sk->sk_max_ack_backlog;
235
		sk->sk_max_ack_backlog = backlog;
236 237 238 239 240
		ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
		if (ret == 0)
			rx->sk.sk_state = RXRPC_SERVER_LISTENING;
		else
			sk->sk_max_ack_backlog = old;
241
		break;
242 243 244 245 246 247 248 249
	case RXRPC_SERVER_LISTENING:
		if (backlog == 0) {
			rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
			sk->sk_max_ack_backlog = 0;
			rxrpc_discard_prealloc(rx);
			ret = 0;
			break;
		}
250
		/* Fall through */
251 252 253
	default:
		ret = -EBUSY;
		break;
254 255 256 257 258 259 260
	}

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

261 262 263
/**
 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
 * @sock: The socket on which to make the call
264
 * @srx: The address of the peer to contact
265 266
 * @key: The security context to use (defaults to socket setting)
 * @user_call_ID: The ID to use
267
 * @tx_total_len: Total length of data to transmit during the call (or -1)
268 269
 * @gfp: The allocation constraints
 * @notify_rx: Where to send notifications instead of socket queue
270
 * @upgrade: Request service upgrade for call
271
 * @debug_id: The debug ID for tracing to be assigned to the call
272 273 274 275 276 277 278 279 280 281 282 283
 *
 * 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,
284
					   s64 tx_total_len,
285
					   gfp_t gfp,
286
					   rxrpc_notify_rx_t notify_rx,
287 288
					   bool upgrade,
					   unsigned int debug_id)
289
{
290
	struct rxrpc_conn_parameters cp;
291
	struct rxrpc_call_params p;
292 293
	struct rxrpc_call *call;
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
294
	int ret;
295 296 297

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

298 299 300 301
	ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
	if (ret < 0)
		return ERR_PTR(ret);

302 303
	lock_sock(&rx->sk);

304 305 306 307 308
	if (!key)
		key = rx->key;
	if (key && !key->payload.data[0])
		key = NULL; /* a no-security key */

309 310 311 312
	memset(&p, 0, sizeof(p));
	p.user_call_ID = user_call_ID;
	p.tx_total_len = tx_total_len;

313 314 315 316 317
	memset(&cp, 0, sizeof(cp));
	cp.local		= rx->local;
	cp.key			= key;
	cp.security_level	= 0;
	cp.exclusive		= false;
318
	cp.upgrade		= upgrade;
319
	cp.service_id		= srx->srx_service;
320
	call = rxrpc_new_client_call(rx, &cp, srx, &p, gfp, debug_id);
321
	/* The socket has been unlocked. */
322
	if (!IS_ERR(call)) {
323
		call->notify_rx = notify_rx;
324 325
		mutex_unlock(&call->user_mutex);
	}
326

327
	rxrpc_put_peer(cp.peer);
328 329 330 331 332
	_leave(" = %p", call);
	return call;
}
EXPORT_SYMBOL(rxrpc_kernel_begin_call);

333 334 335 336 337 338 339 340
/*
 * Dummy function used to stop the notifier talking to recvmsg().
 */
static void rxrpc_dummy_notify_rx(struct sock *sk, struct rxrpc_call *rxcall,
				  unsigned long call_user_ID)
{
}

341 342
/**
 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
343
 * @sock: The socket the call is on
344 345 346 347 348
 * @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).
 */
349
void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
350 351
{
	_enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
352 353

	mutex_lock(&call->user_mutex);
354
	rxrpc_release_call(rxrpc_sk(sock->sk), call);
355 356 357 358 359 360 361 362

	/* Make sure we're not going to call back into a kernel service */
	if (call->notify_rx) {
		spin_lock_bh(&call->notify_lock);
		call->notify_rx = rxrpc_dummy_notify_rx;
		spin_unlock_bh(&call->notify_lock);
	}

363
	mutex_unlock(&call->user_mutex);
364
	rxrpc_put_call(call, rxrpc_call_put_kernel);
365 366 367
}
EXPORT_SYMBOL(rxrpc_kernel_end_call);

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/**
 * rxrpc_kernel_check_life - Check to see whether a call is still alive
 * @sock: The socket the call is on
 * @call: The call to check
 *
 * Allow a kernel service to find out whether a call is still alive - ie. we're
 * getting ACKs from the server.  Returns a number representing the life state
 * which can be compared to that returned by a previous call.
 *
 * If this is a client call, ping ACKs will be sent to the server to find out
 * whether it's still responsive and whether the call is still alive on the
 * server.
 */
u32 rxrpc_kernel_check_life(struct socket *sock, struct rxrpc_call *call)
{
	return call->acks_latest;
}
EXPORT_SYMBOL(rxrpc_kernel_check_life);

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
/**
 * rxrpc_kernel_check_call - Check a call's state
 * @sock: The socket the call is on
 * @call: The call to check
 * @_compl: Where to store the completion state
 * @_abort_code: Where to store any abort code
 *
 * Allow a kernel service to query the state of a call and find out the manner
 * of its termination if it has completed.  Returns -EINPROGRESS if the call is
 * still going, 0 if the call finished successfully, -ECONNABORTED if the call
 * was aborted and an appropriate error if the call failed in some other way.
 */
int rxrpc_kernel_check_call(struct socket *sock, struct rxrpc_call *call,
			    enum rxrpc_call_completion *_compl, u32 *_abort_code)
{
	if (call->state != RXRPC_CALL_COMPLETE)
		return -EINPROGRESS;
	smp_rmb();
	*_compl = call->completion;
	*_abort_code = call->abort_code;
	return call->error;
}
EXPORT_SYMBOL(rxrpc_kernel_check_call);

/**
 * rxrpc_kernel_retry_call - Allow a kernel service to retry a call
 * @sock: The socket the call is on
 * @call: The call to retry
 * @srx: The address of the peer to contact
 * @key: The security context to use (defaults to socket setting)
 *
 * Allow a kernel service to try resending a client call that failed due to a
 * network error to a new address.  The Tx queue is maintained intact, thereby
 * relieving the need to re-encrypt any request data that has already been
 * buffered.
 */
int rxrpc_kernel_retry_call(struct socket *sock, struct rxrpc_call *call,
			    struct sockaddr_rxrpc *srx, struct key *key)
{
	struct rxrpc_conn_parameters cp;
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
	int ret;

	_enter("%d{%d}", call->debug_id, atomic_read(&call->usage));

	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;

	mutex_lock(&call->user_mutex);

	ret = rxrpc_prepare_call_for_retry(rx, call);
	if (ret == 0)
		ret = rxrpc_retry_client_call(rx, call, &cp, srx, GFP_KERNEL);

	mutex_unlock(&call->user_mutex);
451
	rxrpc_put_peer(cp.peer);
452 453 454 455 456
	_leave(" = %d", ret);
	return ret;
}
EXPORT_SYMBOL(rxrpc_kernel_retry_call);

457
/**
458
 * rxrpc_kernel_new_call_notification - Get notifications of new calls
459
 * @sock: The socket to intercept received messages on
460
 * @notify_new_call: Function to be called when new calls appear
461
 * @discard_new_call: Function to discard preallocated calls
462
 *
463
 * Allow a kernel service to be given notifications about new calls.
464
 */
465 466
void rxrpc_kernel_new_call_notification(
	struct socket *sock,
467 468
	rxrpc_notify_new_call_t notify_new_call,
	rxrpc_discard_new_call_t discard_new_call)
469 470 471
{
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);

472
	rx->notify_new_call = notify_new_call;
473
	rx->discard_new_call = discard_new_call;
474
}
475
EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
476

477 478 479 480 481 482 483 484
/*
 * 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)
{
485 486
	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
487 488 489 490 491 492 493 494 495 496 497 498
	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);

499 500 501 502
	ret = -EISCONN;
	if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
		goto error;

503
	switch (rx->sk.sk_state) {
504 505 506
	case RXRPC_UNBOUND:
		rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
	case RXRPC_CLIENT_UNBOUND:
507 508 509
	case RXRPC_CLIENT_BOUND:
		break;
	default:
510 511
		ret = -EBUSY;
		goto error;
512 513
	}

514 515 516
	rx->connect_srx = *srx;
	set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
	ret = 0;
517

518
error:
519
	release_sock(&rx->sk);
520
	return ret;
521 522 523 524 525 526 527 528 529 530 531
}

/*
 * 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)
 */
532
static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
533
{
534
	struct rxrpc_local *local;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	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) {
554
	case RXRPC_UNBOUND:
555 556 557 558 559 560 561 562
		rx->srx.srx_family = AF_RXRPC;
		rx->srx.srx_service = 0;
		rx->srx.transport_type = SOCK_DGRAM;
		rx->srx.transport.family = rx->family;
		switch (rx->family) {
		case AF_INET:
			rx->srx.transport_len = sizeof(struct sockaddr_in);
			break;
563
#ifdef CONFIG_AF_RXRPC_IPV6
D
David Howells 已提交
564 565 566
		case AF_INET6:
			rx->srx.transport_len = sizeof(struct sockaddr_in6);
			break;
567
#endif
568 569 570 571
		default:
			ret = -EAFNOSUPPORT;
			goto error_unlock;
		}
572
		local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
573 574 575
		if (IS_ERR(local)) {
			ret = PTR_ERR(local);
			goto error_unlock;
576
		}
577 578 579 580 581 582

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

	case RXRPC_CLIENT_UNBOUND:
583
	case RXRPC_CLIENT_BOUND:
584 585 586 587
		if (!m->msg_name &&
		    test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
			m->msg_name = &rx->connect_srx;
			m->msg_namelen = sizeof(rx->connect_srx);
588
		}
589
		/* Fall through */
590 591 592
	case RXRPC_SERVER_BOUND:
	case RXRPC_SERVER_LISTENING:
		ret = rxrpc_do_sendmsg(rx, m, len);
593 594
		/* The socket has been unlocked */
		goto out;
595
	default:
596
		ret = -EINVAL;
597
		goto error_unlock;
598 599
	}

600
error_unlock:
601
	release_sock(&rx->sk);
602
out:
603 604 605 606 607 608 609 610
	_leave(" = %d", ret);
	return ret;
}

/*
 * set RxRPC socket options
 */
static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
611
			    char __user *optval, unsigned int optlen)
612 613
{
	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
614
	unsigned int min_sec_level;
D
David Howells 已提交
615
	u16 service_upgrade[2];
616 617 618 619 620 621 622 623 624 625 626 627 628 629
	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;
630
			if (rx->sk.sk_state != RXRPC_UNBOUND)
631
				goto error;
632
			rx->exclusive = true;
633 634 635 636 637 638 639
			goto success;

		case RXRPC_SECURITY_KEY:
			ret = -EINVAL;
			if (rx->key)
				goto error;
			ret = -EISCONN;
640
			if (rx->sk.sk_state != RXRPC_UNBOUND)
641 642 643 644 645 646 647 648 649
				goto error;
			ret = rxrpc_request_key(rx, optval, optlen);
			goto error;

		case RXRPC_SECURITY_KEYRING:
			ret = -EINVAL;
			if (rx->key)
				goto error;
			ret = -EISCONN;
650
			if (rx->sk.sk_state != RXRPC_UNBOUND)
651 652 653 654 655 656
				goto error;
			ret = rxrpc_server_keyring(rx, optval, optlen);
			goto error;

		case RXRPC_MIN_SECURITY_LEVEL:
			ret = -EINVAL;
657
			if (optlen != sizeof(unsigned int))
658 659
				goto error;
			ret = -EISCONN;
660
			if (rx->sk.sk_state != RXRPC_UNBOUND)
661 662
				goto error;
			ret = get_user(min_sec_level,
663
				       (unsigned int __user *) optval);
664 665 666 667 668 669 670 671
			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;

D
David Howells 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		case RXRPC_UPGRADEABLE_SERVICE:
			ret = -EINVAL;
			if (optlen != sizeof(service_upgrade) ||
			    rx->service_upgrade.from != 0)
				goto error;
			ret = -EISCONN;
			if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
				goto error;
			ret = -EFAULT;
			if (copy_from_user(service_upgrade, optval,
					   sizeof(service_upgrade)) != 0)
				goto error;
			ret = -EINVAL;
			if ((service_upgrade[0] != rx->srx.srx_service ||
			     service_upgrade[1] != rx->second_service) &&
			    (service_upgrade[0] != rx->second_service ||
			     service_upgrade[1] != rx->srx.srx_service))
				goto error;
			rx->service_upgrade.from = service_upgrade[0];
			rx->service_upgrade.to = service_upgrade[1];
			goto success;

694 695 696 697 698 699 700 701 702 703 704 705
		default:
			break;
		}
	}

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

706 707 708 709 710 711 712
/*
 * Get socket options.
 */
static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
			    char __user *optval, int __user *_optlen)
{
	int optlen;
713

714 715 716 717 718
	if (level != SOL_RXRPC)
		return -EOPNOTSUPP;

	if (get_user(optlen, _optlen))
		return -EFAULT;
719

720 721 722 723 724 725 726 727
	switch (optname) {
	case RXRPC_SUPPORTED_CMSG:
		if (optlen < sizeof(int))
			return -ETOOSMALL;
		if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
		    put_user(sizeof(int), _optlen))
			return -EFAULT;
		return 0;
728

729 730 731 732 733
	default:
		return -EOPNOTSUPP;
	}
}

734 735 736
/*
 * permit an RxRPC socket to be polled
 */
A
Al Viro 已提交
737
static __poll_t rxrpc_poll(struct file *file, struct socket *sock,
738 739 740
			       poll_table *wait)
{
	struct sock *sk = sock->sk;
741
	struct rxrpc_sock *rx = rxrpc_sk(sk);
A
Al Viro 已提交
742
	__poll_t mask;
743

E
Eric Dumazet 已提交
744
	sock_poll_wait(file, sk_sleep(sk), wait);
745 746 747 748
	mask = 0;

	/* the socket is readable if there are any messages waiting on the Rx
	 * queue */
749
	if (!list_empty(&rx->recvmsg_q))
750
		mask |= EPOLLIN | EPOLLRDNORM;
751 752 753 754 755

	/* 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))
756
		mask |= EPOLLOUT | EPOLLWRNORM;
757 758 759 760 761 762 763

	return mask;
}

/*
 * create an RxRPC socket
 */
764 765
static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
			int kern)
766
{
767
	struct rxrpc_net *rxnet;
768 769 770 771 772
	struct rxrpc_sock *rx;
	struct sock *sk;

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

773
	/* we support transport protocol UDP/UDP6 only */
774 775
	if (protocol != PF_INET &&
	    IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
776 777 778 779 780 781 782 783
		return -EPROTONOSUPPORT;

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

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

784
	sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
785 786 787 788
	if (!sk)
		return -ENOMEM;

	sock_init_data(sock, sk);
789
	sock_set_flag(sk, SOCK_RCU_FREE);
790
	sk->sk_state		= RXRPC_UNBOUND;
791
	sk->sk_write_space	= rxrpc_write_space;
792
	sk->sk_max_ack_backlog	= 0;
793 794 795
	sk->sk_destruct		= rxrpc_sock_destructor;

	rx = rxrpc_sk(sk);
796
	rx->family = protocol;
797 798
	rx->calls = RB_ROOT;

799 800 801 802 803
	spin_lock_init(&rx->incoming_lock);
	INIT_LIST_HEAD(&rx->sock_calls);
	INIT_LIST_HEAD(&rx->to_be_accepted);
	INIT_LIST_HEAD(&rx->recvmsg_q);
	rwlock_init(&rx->recvmsg_lock);
804 805 806
	rwlock_init(&rx->call_lock);
	memset(&rx->srx, 0, sizeof(rx->srx));

807 808 809
	rxnet = rxrpc_net(sock_net(&rx->sk));
	timer_reduce(&rxnet->peer_keepalive_timer, jiffies + 1);

810 811 812 813
	_leave(" = 0 [%p]", rx);
	return 0;
}

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
/*
 * Kill all the calls on a socket and shut it down.
 */
static int rxrpc_shutdown(struct socket *sock, int flags)
{
	struct sock *sk = sock->sk;
	struct rxrpc_sock *rx = rxrpc_sk(sk);
	int ret = 0;

	_enter("%p,%d", sk, flags);

	if (flags != SHUT_RDWR)
		return -EOPNOTSUPP;
	if (sk->sk_state == RXRPC_CLOSE)
		return -ESHUTDOWN;

	lock_sock(sk);

	spin_lock_bh(&sk->sk_receive_queue.lock);
	if (sk->sk_state < RXRPC_CLOSE) {
		sk->sk_state = RXRPC_CLOSE;
		sk->sk_shutdown = SHUTDOWN_MASK;
	} else {
		ret = -ESHUTDOWN;
	}
	spin_unlock_bh(&sk->sk_receive_queue.lock);

	rxrpc_discard_prealloc(rx);

	release_sock(sk);
	return ret;
}

847 848 849 850 851 852 853 854 855
/*
 * RxRPC socket destructor
 */
static void rxrpc_sock_destructor(struct sock *sk)
{
	_enter("%p", sk);

	rxrpc_purge_queue(&sk->sk_receive_queue);

856
	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
857 858
	WARN_ON(!sk_unhashed(sk));
	WARN_ON(sk->sk_socket);
859 860 861 862 863 864 865 866 867 868 869 870 871

	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);
872
	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
873

874
	_enter("%p{%d,%d}", sk, sk->sk_state, refcount_read(&sk->sk_refcnt));
875 876 877 878 879

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

D
David Howells 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892
	/* We want to kill off all connections from a service socket
	 * as fast as possible because we can't share these; client
	 * sockets, on the other hand, can share an endpoint.
	 */
	switch (sk->sk_state) {
	case RXRPC_SERVER_BOUND:
	case RXRPC_SERVER_BOUND2:
	case RXRPC_SERVER_LISTENING:
	case RXRPC_SERVER_LISTEN_DISABLED:
		rx->local->service_closed = true;
		break;
	}

893 894 895 896
	spin_lock_bh(&sk->sk_receive_queue.lock);
	sk->sk_state = RXRPC_CLOSE;
	spin_unlock_bh(&sk->sk_receive_queue.lock);

897
	if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
898
		write_lock(&rx->local->services_lock);
899
		rcu_assign_pointer(rx->local->service, NULL);
900
		write_unlock(&rx->local->services_lock);
901 902 903
	}

	/* try to flush out this socket */
904
	rxrpc_discard_prealloc(rx);
905
	rxrpc_release_calls_on_socket(rx);
906
	flush_workqueue(rxrpc_workqueue);
907
	rxrpc_purge_queue(&sk->sk_receive_queue);
908 909
	rxrpc_queue_work(&rxnet->service_conn_reaper);
	rxrpc_queue_work(&rxnet->client_conn_reaper);
910

911 912
	rxrpc_put_local(rx->local);
	rx->local = NULL;
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
	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 = {
944
	.family		= PF_RXRPC,
945 946 947 948 949 950 951 952 953 954
	.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,
955
	.shutdown	= rxrpc_shutdown,
956
	.setsockopt	= rxrpc_setsockopt,
957
	.getsockopt	= rxrpc_getsockopt,
958 959 960 961 962 963 964 965 966 967
	.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),
968
	.max_header	= sizeof(struct rxrpc_wire_header),
969 970
};

971
static const struct net_proto_family rxrpc_family_ops = {
972 973 974 975 976 977 978 979 980 981 982
	.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;
983
	unsigned int tmp;
984

985
	BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
986

987 988 989 990 991
	get_random_bytes(&tmp, sizeof(tmp));
	tmp &= 0x3fffffff;
	if (tmp == 0)
		tmp = 1;
	idr_set_cursor(&rxrpc_client_conn_ids, tmp);
992

993
	ret = -ENOMEM;
994 995
	rxrpc_call_jar = kmem_cache_create(
		"rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
996
		SLAB_HWCACHE_ALIGN, NULL);
997
	if (!rxrpc_call_jar) {
998
		pr_notice("Failed to allocate call jar\n");
999 1000 1001
		goto error_call_jar;
	}

1002
	rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
1003
	if (!rxrpc_workqueue) {
1004
		pr_notice("Failed to allocate work queue\n");
1005 1006 1007
		goto error_work_queue;
	}

1008 1009
	ret = rxrpc_init_security();
	if (ret < 0) {
1010
		pr_crit("Cannot initialise security\n");
1011 1012 1013
		goto error_security;
	}

1014 1015 1016 1017
	ret = register_pernet_subsys(&rxrpc_net_ops);
	if (ret)
		goto error_pernet;

1018
	ret = proto_register(&rxrpc_proto, 1);
1019
	if (ret < 0) {
1020
		pr_crit("Cannot register protocol\n");
1021 1022 1023 1024 1025
		goto error_proto;
	}

	ret = sock_register(&rxrpc_family_ops);
	if (ret < 0) {
1026
		pr_crit("Cannot register socket family\n");
1027 1028 1029 1030 1031
		goto error_sock;
	}

	ret = register_key_type(&key_type_rxrpc);
	if (ret < 0) {
1032
		pr_crit("Cannot register client key type\n");
1033 1034 1035 1036 1037
		goto error_key_type;
	}

	ret = register_key_type(&key_type_rxrpc_s);
	if (ret < 0) {
1038
		pr_crit("Cannot register server key type\n");
1039 1040 1041
		goto error_key_type_s;
	}

1042 1043
	ret = rxrpc_sysctl_init();
	if (ret < 0) {
1044
		pr_crit("Cannot register sysctls\n");
1045 1046 1047
		goto error_sysctls;
	}

1048 1049
	return 0;

1050 1051
error_sysctls:
	unregister_key_type(&key_type_rxrpc_s);
1052 1053 1054 1055 1056 1057 1058
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:
1059 1060
	unregister_pernet_subsys(&rxrpc_net_ops);
error_pernet:
1061
	rxrpc_exit_security();
1062 1063
error_security:
	destroy_workqueue(rxrpc_workqueue);
1064
error_work_queue:
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	kmem_cache_destroy(rxrpc_call_jar);
error_call_jar:
	return ret;
}

/*
 * unregister the RxRPC protocol
 */
static void __exit af_rxrpc_exit(void)
{
	_enter("");
1076
	rxrpc_sysctl_exit();
1077 1078 1079 1080
	unregister_key_type(&key_type_rxrpc_s);
	unregister_key_type(&key_type_rxrpc);
	sock_unregister(PF_RXRPC);
	proto_unregister(&rxrpc_proto);
1081
	unregister_pernet_subsys(&rxrpc_net_ops);
D
David Howells 已提交
1082 1083
	ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
	ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
1084

1085 1086 1087 1088 1089 1090
	/* Make sure the local and peer records pinned by any dying connections
	 * are released.
	 */
	rcu_barrier();
	rxrpc_destroy_client_conn_ids();

1091
	destroy_workqueue(rxrpc_workqueue);
1092
	rxrpc_exit_security();
1093 1094 1095 1096 1097 1098
	kmem_cache_destroy(rxrpc_call_jar);
	_leave("");
}

module_init(af_rxrpc_init);
module_exit(af_rxrpc_exit);