xref: /linux/net/rxrpc/af_rxrpc.c (revision a3a4a816b4b194c45d0217e8b9e08b2639802cda)
1 /* 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
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 #include <linux/poll.h>
21 #include <linux/proc_fs.h>
22 #include <linux/key-type.h>
23 #include <net/net_namespace.h>
24 #include <net/sock.h>
25 #include <net/af_rxrpc.h>
26 #define CREATE_TRACE_POINTS
27 #include "ar-internal.h"
28 
29 MODULE_DESCRIPTION("RxRPC network protocol");
30 MODULE_AUTHOR("Red Hat, Inc.");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS_NETPROTO(PF_RXRPC);
33 
34 unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
35 module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
36 MODULE_PARM_DESC(debug, "RxRPC debugging mask");
37 
38 static struct proto rxrpc_proto;
39 static const struct proto_ops rxrpc_rpc_ops;
40 
41 /* local epoch for detecting local-end reset */
42 u32 rxrpc_epoch;
43 
44 /* current debugging ID */
45 atomic_t rxrpc_debug_id;
46 
47 /* count of skbs currently in use */
48 atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
49 
50 struct workqueue_struct *rxrpc_workqueue;
51 
52 static void rxrpc_sock_destructor(struct sock *);
53 
54 /*
55  * see if an RxRPC socket is currently writable
56  */
57 static inline int rxrpc_writable(struct sock *sk)
58 {
59 	return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
60 }
61 
62 /*
63  * wait for write bufferage to become available
64  */
65 static void rxrpc_write_space(struct sock *sk)
66 {
67 	_enter("%p", sk);
68 	rcu_read_lock();
69 	if (rxrpc_writable(sk)) {
70 		struct socket_wq *wq = rcu_dereference(sk->sk_wq);
71 
72 		if (skwq_has_sleeper(wq))
73 			wake_up_interruptible(&wq->wait);
74 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
75 	}
76 	rcu_read_unlock();
77 }
78 
79 /*
80  * validate an RxRPC address
81  */
82 static int rxrpc_validate_address(struct rxrpc_sock *rx,
83 				  struct sockaddr_rxrpc *srx,
84 				  int len)
85 {
86 	unsigned int tail;
87 
88 	if (len < sizeof(struct sockaddr_rxrpc))
89 		return -EINVAL;
90 
91 	if (srx->srx_family != AF_RXRPC)
92 		return -EAFNOSUPPORT;
93 
94 	if (srx->transport_type != SOCK_DGRAM)
95 		return -ESOCKTNOSUPPORT;
96 
97 	len -= offsetof(struct sockaddr_rxrpc, transport);
98 	if (srx->transport_len < sizeof(sa_family_t) ||
99 	    srx->transport_len > len)
100 		return -EINVAL;
101 
102 	if (srx->transport.family != rx->family)
103 		return -EAFNOSUPPORT;
104 
105 	switch (srx->transport.family) {
106 	case AF_INET:
107 		if (srx->transport_len < sizeof(struct sockaddr_in))
108 			return -EINVAL;
109 		tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
110 		break;
111 
112 #ifdef CONFIG_AF_RXRPC_IPV6
113 	case AF_INET6:
114 		if (srx->transport_len < sizeof(struct sockaddr_in6))
115 			return -EINVAL;
116 		tail = offsetof(struct sockaddr_rxrpc, transport) +
117 			sizeof(struct sockaddr_in6);
118 		break;
119 #endif
120 
121 	default:
122 		return -EAFNOSUPPORT;
123 	}
124 
125 	if (tail < len)
126 		memset((void *)srx + tail, 0, len - tail);
127 	_debug("INET: %pISp", &srx->transport);
128 	return 0;
129 }
130 
131 /*
132  * bind a local address to an RxRPC socket
133  */
134 static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
135 {
136 	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
137 	struct sock *sk = sock->sk;
138 	struct rxrpc_local *local;
139 	struct rxrpc_sock *rx = rxrpc_sk(sk);
140 	u16 service_id = srx->srx_service;
141 	int ret;
142 
143 	_enter("%p,%p,%d", rx, saddr, len);
144 
145 	ret = rxrpc_validate_address(rx, srx, len);
146 	if (ret < 0)
147 		goto error;
148 
149 	lock_sock(&rx->sk);
150 
151 	if (rx->sk.sk_state != RXRPC_UNBOUND) {
152 		ret = -EINVAL;
153 		goto error_unlock;
154 	}
155 
156 	memcpy(&rx->srx, srx, sizeof(rx->srx));
157 
158 	local = rxrpc_lookup_local(&rx->srx);
159 	if (IS_ERR(local)) {
160 		ret = PTR_ERR(local);
161 		goto error_unlock;
162 	}
163 
164 	if (service_id) {
165 		write_lock(&local->services_lock);
166 		if (rcu_access_pointer(local->service))
167 			goto service_in_use;
168 		rx->local = local;
169 		rcu_assign_pointer(local->service, rx);
170 		write_unlock(&local->services_lock);
171 
172 		rx->sk.sk_state = RXRPC_SERVER_BOUND;
173 	} else {
174 		rx->local = local;
175 		rx->sk.sk_state = RXRPC_CLIENT_BOUND;
176 	}
177 
178 	release_sock(&rx->sk);
179 	_leave(" = 0");
180 	return 0;
181 
182 service_in_use:
183 	write_unlock(&local->services_lock);
184 	rxrpc_put_local(local);
185 	ret = -EADDRINUSE;
186 error_unlock:
187 	release_sock(&rx->sk);
188 error:
189 	_leave(" = %d", ret);
190 	return ret;
191 }
192 
193 /*
194  * set the number of pending calls permitted on a listening socket
195  */
196 static int rxrpc_listen(struct socket *sock, int backlog)
197 {
198 	struct sock *sk = sock->sk;
199 	struct rxrpc_sock *rx = rxrpc_sk(sk);
200 	unsigned int max, old;
201 	int ret;
202 
203 	_enter("%p,%d", rx, backlog);
204 
205 	lock_sock(&rx->sk);
206 
207 	switch (rx->sk.sk_state) {
208 	case RXRPC_UNBOUND:
209 		ret = -EADDRNOTAVAIL;
210 		break;
211 	case RXRPC_SERVER_BOUND:
212 		ASSERT(rx->local != NULL);
213 		max = READ_ONCE(rxrpc_max_backlog);
214 		ret = -EINVAL;
215 		if (backlog == INT_MAX)
216 			backlog = max;
217 		else if (backlog < 0 || backlog > max)
218 			break;
219 		old = sk->sk_max_ack_backlog;
220 		sk->sk_max_ack_backlog = backlog;
221 		ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
222 		if (ret == 0)
223 			rx->sk.sk_state = RXRPC_SERVER_LISTENING;
224 		else
225 			sk->sk_max_ack_backlog = old;
226 		break;
227 	case RXRPC_SERVER_LISTENING:
228 		if (backlog == 0) {
229 			rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
230 			sk->sk_max_ack_backlog = 0;
231 			rxrpc_discard_prealloc(rx);
232 			ret = 0;
233 			break;
234 		}
235 	default:
236 		ret = -EBUSY;
237 		break;
238 	}
239 
240 	release_sock(&rx->sk);
241 	_leave(" = %d", ret);
242 	return ret;
243 }
244 
245 /**
246  * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
247  * @sock: The socket on which to make the call
248  * @srx: The address of the peer to contact
249  * @key: The security context to use (defaults to socket setting)
250  * @user_call_ID: The ID to use
251  * @gfp: The allocation constraints
252  * @notify_rx: Where to send notifications instead of socket queue
253  *
254  * Allow a kernel service to begin a call on the nominated socket.  This just
255  * sets up all the internal tracking structures and allocates connection and
256  * call IDs as appropriate.  The call to be used is returned.
257  *
258  * The default socket destination address and security may be overridden by
259  * supplying @srx and @key.
260  */
261 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
262 					   struct sockaddr_rxrpc *srx,
263 					   struct key *key,
264 					   unsigned long user_call_ID,
265 					   gfp_t gfp,
266 					   rxrpc_notify_rx_t notify_rx)
267 {
268 	struct rxrpc_conn_parameters cp;
269 	struct rxrpc_call *call;
270 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
271 	int ret;
272 
273 	_enter(",,%x,%lx", key_serial(key), user_call_ID);
274 
275 	ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
276 	if (ret < 0)
277 		return ERR_PTR(ret);
278 
279 	lock_sock(&rx->sk);
280 
281 	if (!key)
282 		key = rx->key;
283 	if (key && !key->payload.data[0])
284 		key = NULL; /* a no-security key */
285 
286 	memset(&cp, 0, sizeof(cp));
287 	cp.local		= rx->local;
288 	cp.key			= key;
289 	cp.security_level	= 0;
290 	cp.exclusive		= false;
291 	cp.service_id		= srx->srx_service;
292 	call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp);
293 	/* The socket has been unlocked. */
294 	if (!IS_ERR(call))
295 		call->notify_rx = notify_rx;
296 
297 	mutex_unlock(&call->user_mutex);
298 	_leave(" = %p", call);
299 	return call;
300 }
301 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
302 
303 /**
304  * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
305  * @sock: The socket the call is on
306  * @call: The call to end
307  *
308  * Allow a kernel service to end a call it was using.  The call must be
309  * complete before this is called (the call should be aborted if necessary).
310  */
311 void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
312 {
313 	_enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
314 
315 	mutex_lock(&call->user_mutex);
316 	rxrpc_release_call(rxrpc_sk(sock->sk), call);
317 	mutex_unlock(&call->user_mutex);
318 	rxrpc_put_call(call, rxrpc_call_put_kernel);
319 }
320 EXPORT_SYMBOL(rxrpc_kernel_end_call);
321 
322 /**
323  * rxrpc_kernel_new_call_notification - Get notifications of new calls
324  * @sock: The socket to intercept received messages on
325  * @notify_new_call: Function to be called when new calls appear
326  * @discard_new_call: Function to discard preallocated calls
327  *
328  * Allow a kernel service to be given notifications about new calls.
329  */
330 void rxrpc_kernel_new_call_notification(
331 	struct socket *sock,
332 	rxrpc_notify_new_call_t notify_new_call,
333 	rxrpc_discard_new_call_t discard_new_call)
334 {
335 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
336 
337 	rx->notify_new_call = notify_new_call;
338 	rx->discard_new_call = discard_new_call;
339 }
340 EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
341 
342 /*
343  * connect an RxRPC socket
344  * - this just targets it at a specific destination; no actual connection
345  *   negotiation takes place
346  */
347 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
348 			 int addr_len, int flags)
349 {
350 	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
351 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
352 	int ret;
353 
354 	_enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
355 
356 	ret = rxrpc_validate_address(rx, srx, addr_len);
357 	if (ret < 0) {
358 		_leave(" = %d [bad addr]", ret);
359 		return ret;
360 	}
361 
362 	lock_sock(&rx->sk);
363 
364 	ret = -EISCONN;
365 	if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
366 		goto error;
367 
368 	switch (rx->sk.sk_state) {
369 	case RXRPC_UNBOUND:
370 		rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
371 	case RXRPC_CLIENT_UNBOUND:
372 	case RXRPC_CLIENT_BOUND:
373 		break;
374 	default:
375 		ret = -EBUSY;
376 		goto error;
377 	}
378 
379 	rx->connect_srx = *srx;
380 	set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
381 	ret = 0;
382 
383 error:
384 	release_sock(&rx->sk);
385 	return ret;
386 }
387 
388 /*
389  * send a message through an RxRPC socket
390  * - in a client this does a number of things:
391  *   - finds/sets up a connection for the security specified (if any)
392  *   - initiates a call (ID in control data)
393  *   - ends the request phase of a call (if MSG_MORE is not set)
394  *   - sends a call data packet
395  *   - may send an abort (abort code in control data)
396  */
397 static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
398 {
399 	struct rxrpc_local *local;
400 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
401 	int ret;
402 
403 	_enter(",{%d},,%zu", rx->sk.sk_state, len);
404 
405 	if (m->msg_flags & MSG_OOB)
406 		return -EOPNOTSUPP;
407 
408 	if (m->msg_name) {
409 		ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
410 		if (ret < 0) {
411 			_leave(" = %d [bad addr]", ret);
412 			return ret;
413 		}
414 	}
415 
416 	lock_sock(&rx->sk);
417 
418 	switch (rx->sk.sk_state) {
419 	case RXRPC_UNBOUND:
420 		rx->srx.srx_family = AF_RXRPC;
421 		rx->srx.srx_service = 0;
422 		rx->srx.transport_type = SOCK_DGRAM;
423 		rx->srx.transport.family = rx->family;
424 		switch (rx->family) {
425 		case AF_INET:
426 			rx->srx.transport_len = sizeof(struct sockaddr_in);
427 			break;
428 #ifdef CONFIG_AF_RXRPC_IPV6
429 		case AF_INET6:
430 			rx->srx.transport_len = sizeof(struct sockaddr_in6);
431 			break;
432 #endif
433 		default:
434 			ret = -EAFNOSUPPORT;
435 			goto error_unlock;
436 		}
437 		local = rxrpc_lookup_local(&rx->srx);
438 		if (IS_ERR(local)) {
439 			ret = PTR_ERR(local);
440 			goto error_unlock;
441 		}
442 
443 		rx->local = local;
444 		rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
445 		/* Fall through */
446 
447 	case RXRPC_CLIENT_UNBOUND:
448 	case RXRPC_CLIENT_BOUND:
449 		if (!m->msg_name &&
450 		    test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
451 			m->msg_name = &rx->connect_srx;
452 			m->msg_namelen = sizeof(rx->connect_srx);
453 		}
454 	case RXRPC_SERVER_BOUND:
455 	case RXRPC_SERVER_LISTENING:
456 		ret = rxrpc_do_sendmsg(rx, m, len);
457 		/* The socket has been unlocked */
458 		goto out;
459 	default:
460 		ret = -EINVAL;
461 		goto error_unlock;
462 	}
463 
464 error_unlock:
465 	release_sock(&rx->sk);
466 out:
467 	_leave(" = %d", ret);
468 	return ret;
469 }
470 
471 /*
472  * set RxRPC socket options
473  */
474 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
475 			    char __user *optval, unsigned int optlen)
476 {
477 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
478 	unsigned int min_sec_level;
479 	int ret;
480 
481 	_enter(",%d,%d,,%d", level, optname, optlen);
482 
483 	lock_sock(&rx->sk);
484 	ret = -EOPNOTSUPP;
485 
486 	if (level == SOL_RXRPC) {
487 		switch (optname) {
488 		case RXRPC_EXCLUSIVE_CONNECTION:
489 			ret = -EINVAL;
490 			if (optlen != 0)
491 				goto error;
492 			ret = -EISCONN;
493 			if (rx->sk.sk_state != RXRPC_UNBOUND)
494 				goto error;
495 			rx->exclusive = true;
496 			goto success;
497 
498 		case RXRPC_SECURITY_KEY:
499 			ret = -EINVAL;
500 			if (rx->key)
501 				goto error;
502 			ret = -EISCONN;
503 			if (rx->sk.sk_state != RXRPC_UNBOUND)
504 				goto error;
505 			ret = rxrpc_request_key(rx, optval, optlen);
506 			goto error;
507 
508 		case RXRPC_SECURITY_KEYRING:
509 			ret = -EINVAL;
510 			if (rx->key)
511 				goto error;
512 			ret = -EISCONN;
513 			if (rx->sk.sk_state != RXRPC_UNBOUND)
514 				goto error;
515 			ret = rxrpc_server_keyring(rx, optval, optlen);
516 			goto error;
517 
518 		case RXRPC_MIN_SECURITY_LEVEL:
519 			ret = -EINVAL;
520 			if (optlen != sizeof(unsigned int))
521 				goto error;
522 			ret = -EISCONN;
523 			if (rx->sk.sk_state != RXRPC_UNBOUND)
524 				goto error;
525 			ret = get_user(min_sec_level,
526 				       (unsigned int __user *) optval);
527 			if (ret < 0)
528 				goto error;
529 			ret = -EINVAL;
530 			if (min_sec_level > RXRPC_SECURITY_MAX)
531 				goto error;
532 			rx->min_sec_level = min_sec_level;
533 			goto success;
534 
535 		default:
536 			break;
537 		}
538 	}
539 
540 success:
541 	ret = 0;
542 error:
543 	release_sock(&rx->sk);
544 	return ret;
545 }
546 
547 /*
548  * permit an RxRPC socket to be polled
549  */
550 static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
551 			       poll_table *wait)
552 {
553 	struct sock *sk = sock->sk;
554 	struct rxrpc_sock *rx = rxrpc_sk(sk);
555 	unsigned int mask;
556 
557 	sock_poll_wait(file, sk_sleep(sk), wait);
558 	mask = 0;
559 
560 	/* the socket is readable if there are any messages waiting on the Rx
561 	 * queue */
562 	if (!list_empty(&rx->recvmsg_q))
563 		mask |= POLLIN | POLLRDNORM;
564 
565 	/* the socket is writable if there is space to add new data to the
566 	 * socket; there is no guarantee that any particular call in progress
567 	 * on the socket may have space in the Tx ACK window */
568 	if (rxrpc_writable(sk))
569 		mask |= POLLOUT | POLLWRNORM;
570 
571 	return mask;
572 }
573 
574 /*
575  * create an RxRPC socket
576  */
577 static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
578 			int kern)
579 {
580 	struct rxrpc_sock *rx;
581 	struct sock *sk;
582 
583 	_enter("%p,%d", sock, protocol);
584 
585 	if (!net_eq(net, &init_net))
586 		return -EAFNOSUPPORT;
587 
588 	/* we support transport protocol UDP/UDP6 only */
589 	if (protocol != PF_INET &&
590 	    IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
591 		return -EPROTONOSUPPORT;
592 
593 	if (sock->type != SOCK_DGRAM)
594 		return -ESOCKTNOSUPPORT;
595 
596 	sock->ops = &rxrpc_rpc_ops;
597 	sock->state = SS_UNCONNECTED;
598 
599 	sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
600 	if (!sk)
601 		return -ENOMEM;
602 
603 	sock_init_data(sock, sk);
604 	sock_set_flag(sk, SOCK_RCU_FREE);
605 	sk->sk_state		= RXRPC_UNBOUND;
606 	sk->sk_write_space	= rxrpc_write_space;
607 	sk->sk_max_ack_backlog	= 0;
608 	sk->sk_destruct		= rxrpc_sock_destructor;
609 
610 	rx = rxrpc_sk(sk);
611 	rx->family = protocol;
612 	rx->calls = RB_ROOT;
613 
614 	spin_lock_init(&rx->incoming_lock);
615 	INIT_LIST_HEAD(&rx->sock_calls);
616 	INIT_LIST_HEAD(&rx->to_be_accepted);
617 	INIT_LIST_HEAD(&rx->recvmsg_q);
618 	rwlock_init(&rx->recvmsg_lock);
619 	rwlock_init(&rx->call_lock);
620 	memset(&rx->srx, 0, sizeof(rx->srx));
621 
622 	_leave(" = 0 [%p]", rx);
623 	return 0;
624 }
625 
626 /*
627  * Kill all the calls on a socket and shut it down.
628  */
629 static int rxrpc_shutdown(struct socket *sock, int flags)
630 {
631 	struct sock *sk = sock->sk;
632 	struct rxrpc_sock *rx = rxrpc_sk(sk);
633 	int ret = 0;
634 
635 	_enter("%p,%d", sk, flags);
636 
637 	if (flags != SHUT_RDWR)
638 		return -EOPNOTSUPP;
639 	if (sk->sk_state == RXRPC_CLOSE)
640 		return -ESHUTDOWN;
641 
642 	lock_sock(sk);
643 
644 	spin_lock_bh(&sk->sk_receive_queue.lock);
645 	if (sk->sk_state < RXRPC_CLOSE) {
646 		sk->sk_state = RXRPC_CLOSE;
647 		sk->sk_shutdown = SHUTDOWN_MASK;
648 	} else {
649 		ret = -ESHUTDOWN;
650 	}
651 	spin_unlock_bh(&sk->sk_receive_queue.lock);
652 
653 	rxrpc_discard_prealloc(rx);
654 
655 	release_sock(sk);
656 	return ret;
657 }
658 
659 /*
660  * RxRPC socket destructor
661  */
662 static void rxrpc_sock_destructor(struct sock *sk)
663 {
664 	_enter("%p", sk);
665 
666 	rxrpc_purge_queue(&sk->sk_receive_queue);
667 
668 	WARN_ON(atomic_read(&sk->sk_wmem_alloc));
669 	WARN_ON(!sk_unhashed(sk));
670 	WARN_ON(sk->sk_socket);
671 
672 	if (!sock_flag(sk, SOCK_DEAD)) {
673 		printk("Attempt to release alive rxrpc socket: %p\n", sk);
674 		return;
675 	}
676 }
677 
678 /*
679  * release an RxRPC socket
680  */
681 static int rxrpc_release_sock(struct sock *sk)
682 {
683 	struct rxrpc_sock *rx = rxrpc_sk(sk);
684 
685 	_enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
686 
687 	/* declare the socket closed for business */
688 	sock_orphan(sk);
689 	sk->sk_shutdown = SHUTDOWN_MASK;
690 
691 	spin_lock_bh(&sk->sk_receive_queue.lock);
692 	sk->sk_state = RXRPC_CLOSE;
693 	spin_unlock_bh(&sk->sk_receive_queue.lock);
694 
695 	if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
696 		write_lock(&rx->local->services_lock);
697 		rcu_assign_pointer(rx->local->service, NULL);
698 		write_unlock(&rx->local->services_lock);
699 	}
700 
701 	/* try to flush out this socket */
702 	rxrpc_discard_prealloc(rx);
703 	rxrpc_release_calls_on_socket(rx);
704 	flush_workqueue(rxrpc_workqueue);
705 	rxrpc_purge_queue(&sk->sk_receive_queue);
706 
707 	rxrpc_put_local(rx->local);
708 	rx->local = NULL;
709 	key_put(rx->key);
710 	rx->key = NULL;
711 	key_put(rx->securities);
712 	rx->securities = NULL;
713 	sock_put(sk);
714 
715 	_leave(" = 0");
716 	return 0;
717 }
718 
719 /*
720  * release an RxRPC BSD socket on close() or equivalent
721  */
722 static int rxrpc_release(struct socket *sock)
723 {
724 	struct sock *sk = sock->sk;
725 
726 	_enter("%p{%p}", sock, sk);
727 
728 	if (!sk)
729 		return 0;
730 
731 	sock->sk = NULL;
732 
733 	return rxrpc_release_sock(sk);
734 }
735 
736 /*
737  * RxRPC network protocol
738  */
739 static const struct proto_ops rxrpc_rpc_ops = {
740 	.family		= PF_RXRPC,
741 	.owner		= THIS_MODULE,
742 	.release	= rxrpc_release,
743 	.bind		= rxrpc_bind,
744 	.connect	= rxrpc_connect,
745 	.socketpair	= sock_no_socketpair,
746 	.accept		= sock_no_accept,
747 	.getname	= sock_no_getname,
748 	.poll		= rxrpc_poll,
749 	.ioctl		= sock_no_ioctl,
750 	.listen		= rxrpc_listen,
751 	.shutdown	= rxrpc_shutdown,
752 	.setsockopt	= rxrpc_setsockopt,
753 	.getsockopt	= sock_no_getsockopt,
754 	.sendmsg	= rxrpc_sendmsg,
755 	.recvmsg	= rxrpc_recvmsg,
756 	.mmap		= sock_no_mmap,
757 	.sendpage	= sock_no_sendpage,
758 };
759 
760 static struct proto rxrpc_proto = {
761 	.name		= "RXRPC",
762 	.owner		= THIS_MODULE,
763 	.obj_size	= sizeof(struct rxrpc_sock),
764 	.max_header	= sizeof(struct rxrpc_wire_header),
765 };
766 
767 static const struct net_proto_family rxrpc_family_ops = {
768 	.family	= PF_RXRPC,
769 	.create = rxrpc_create,
770 	.owner	= THIS_MODULE,
771 };
772 
773 /*
774  * initialise and register the RxRPC protocol
775  */
776 static int __init af_rxrpc_init(void)
777 {
778 	int ret = -1;
779 	unsigned int tmp;
780 
781 	BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
782 
783 	get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch));
784 	rxrpc_epoch |= RXRPC_RANDOM_EPOCH;
785 	get_random_bytes(&tmp, sizeof(tmp));
786 	tmp &= 0x3fffffff;
787 	if (tmp == 0)
788 		tmp = 1;
789 	idr_set_cursor(&rxrpc_client_conn_ids, tmp);
790 
791 	ret = -ENOMEM;
792 	rxrpc_call_jar = kmem_cache_create(
793 		"rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
794 		SLAB_HWCACHE_ALIGN, NULL);
795 	if (!rxrpc_call_jar) {
796 		pr_notice("Failed to allocate call jar\n");
797 		goto error_call_jar;
798 	}
799 
800 	rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
801 	if (!rxrpc_workqueue) {
802 		pr_notice("Failed to allocate work queue\n");
803 		goto error_work_queue;
804 	}
805 
806 	ret = rxrpc_init_security();
807 	if (ret < 0) {
808 		pr_crit("Cannot initialise security\n");
809 		goto error_security;
810 	}
811 
812 	ret = proto_register(&rxrpc_proto, 1);
813 	if (ret < 0) {
814 		pr_crit("Cannot register protocol\n");
815 		goto error_proto;
816 	}
817 
818 	ret = sock_register(&rxrpc_family_ops);
819 	if (ret < 0) {
820 		pr_crit("Cannot register socket family\n");
821 		goto error_sock;
822 	}
823 
824 	ret = register_key_type(&key_type_rxrpc);
825 	if (ret < 0) {
826 		pr_crit("Cannot register client key type\n");
827 		goto error_key_type;
828 	}
829 
830 	ret = register_key_type(&key_type_rxrpc_s);
831 	if (ret < 0) {
832 		pr_crit("Cannot register server key type\n");
833 		goto error_key_type_s;
834 	}
835 
836 	ret = rxrpc_sysctl_init();
837 	if (ret < 0) {
838 		pr_crit("Cannot register sysctls\n");
839 		goto error_sysctls;
840 	}
841 
842 #ifdef CONFIG_PROC_FS
843 	proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
844 	proc_create("rxrpc_conns", 0, init_net.proc_net,
845 		    &rxrpc_connection_seq_fops);
846 #endif
847 	return 0;
848 
849 error_sysctls:
850 	unregister_key_type(&key_type_rxrpc_s);
851 error_key_type_s:
852 	unregister_key_type(&key_type_rxrpc);
853 error_key_type:
854 	sock_unregister(PF_RXRPC);
855 error_sock:
856 	proto_unregister(&rxrpc_proto);
857 error_proto:
858 	rxrpc_exit_security();
859 error_security:
860 	destroy_workqueue(rxrpc_workqueue);
861 error_work_queue:
862 	kmem_cache_destroy(rxrpc_call_jar);
863 error_call_jar:
864 	return ret;
865 }
866 
867 /*
868  * unregister the RxRPC protocol
869  */
870 static void __exit af_rxrpc_exit(void)
871 {
872 	_enter("");
873 	rxrpc_sysctl_exit();
874 	unregister_key_type(&key_type_rxrpc_s);
875 	unregister_key_type(&key_type_rxrpc);
876 	sock_unregister(PF_RXRPC);
877 	proto_unregister(&rxrpc_proto);
878 	rxrpc_destroy_all_calls();
879 	rxrpc_destroy_all_connections();
880 	ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
881 	ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
882 	rxrpc_destroy_all_locals();
883 
884 	remove_proc_entry("rxrpc_conns", init_net.proc_net);
885 	remove_proc_entry("rxrpc_calls", init_net.proc_net);
886 	destroy_workqueue(rxrpc_workqueue);
887 	rxrpc_exit_security();
888 	kmem_cache_destroy(rxrpc_call_jar);
889 	_leave("");
890 }
891 
892 module_init(af_rxrpc_init);
893 module_exit(af_rxrpc_exit);
894