xref: /openbmc/linux/net/rxrpc/af_rxrpc.c (revision 27e45f2e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AF_RXRPC implementation
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/net.h>
13 #include <linux/slab.h>
14 #include <linux/skbuff.h>
15 #include <linux/random.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/key-type.h>
19 #include <net/net_namespace.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #define CREATE_TRACE_POINTS
23 #include "ar-internal.h"
24 
25 MODULE_DESCRIPTION("RxRPC network protocol");
26 MODULE_AUTHOR("Red Hat, Inc.");
27 MODULE_LICENSE("GPL");
28 MODULE_ALIAS_NETPROTO(PF_RXRPC);
29 
30 unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
31 module_param_named(debug, rxrpc_debug, uint, 0644);
32 MODULE_PARM_DESC(debug, "RxRPC debugging mask");
33 
34 static struct proto rxrpc_proto;
35 static const struct proto_ops rxrpc_rpc_ops;
36 
37 /* current debugging ID */
38 atomic_t rxrpc_debug_id;
39 EXPORT_SYMBOL(rxrpc_debug_id);
40 
41 /* count of skbs currently in use */
42 atomic_t rxrpc_n_rx_skbs;
43 
44 struct workqueue_struct *rxrpc_workqueue;
45 
46 static void rxrpc_sock_destructor(struct sock *);
47 
48 /*
49  * see if an RxRPC socket is currently writable
50  */
51 static inline int rxrpc_writable(struct sock *sk)
52 {
53 	return refcount_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
54 }
55 
56 /*
57  * wait for write bufferage to become available
58  */
59 static void rxrpc_write_space(struct sock *sk)
60 {
61 	_enter("%p", sk);
62 	rcu_read_lock();
63 	if (rxrpc_writable(sk)) {
64 		struct socket_wq *wq = rcu_dereference(sk->sk_wq);
65 
66 		if (skwq_has_sleeper(wq))
67 			wake_up_interruptible(&wq->wait);
68 		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
69 	}
70 	rcu_read_unlock();
71 }
72 
73 /*
74  * validate an RxRPC address
75  */
76 static int rxrpc_validate_address(struct rxrpc_sock *rx,
77 				  struct sockaddr_rxrpc *srx,
78 				  int len)
79 {
80 	unsigned int tail;
81 
82 	if (len < sizeof(struct sockaddr_rxrpc))
83 		return -EINVAL;
84 
85 	if (srx->srx_family != AF_RXRPC)
86 		return -EAFNOSUPPORT;
87 
88 	if (srx->transport_type != SOCK_DGRAM)
89 		return -ESOCKTNOSUPPORT;
90 
91 	len -= offsetof(struct sockaddr_rxrpc, transport);
92 	if (srx->transport_len < sizeof(sa_family_t) ||
93 	    srx->transport_len > len)
94 		return -EINVAL;
95 
96 	switch (srx->transport.family) {
97 	case AF_INET:
98 		if (rx->family != AF_INET &&
99 		    rx->family != AF_INET6)
100 			return -EAFNOSUPPORT;
101 		if (srx->transport_len < sizeof(struct sockaddr_in))
102 			return -EINVAL;
103 		tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
104 		break;
105 
106 #ifdef CONFIG_AF_RXRPC_IPV6
107 	case AF_INET6:
108 		if (rx->family != AF_INET6)
109 			return -EAFNOSUPPORT;
110 		if (srx->transport_len < sizeof(struct sockaddr_in6))
111 			return -EINVAL;
112 		tail = offsetof(struct sockaddr_rxrpc, transport) +
113 			sizeof(struct sockaddr_in6);
114 		break;
115 #endif
116 
117 	default:
118 		return -EAFNOSUPPORT;
119 	}
120 
121 	if (tail < len)
122 		memset((void *)srx + tail, 0, len - tail);
123 	_debug("INET: %pISp", &srx->transport);
124 	return 0;
125 }
126 
127 /*
128  * bind a local address to an RxRPC socket
129  */
130 static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
131 {
132 	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
133 	struct rxrpc_local *local;
134 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
135 	u16 service_id;
136 	int ret;
137 
138 	_enter("%p,%p,%d", rx, saddr, len);
139 
140 	ret = rxrpc_validate_address(rx, srx, len);
141 	if (ret < 0)
142 		goto error;
143 	service_id = srx->srx_service;
144 
145 	lock_sock(&rx->sk);
146 
147 	switch (rx->sk.sk_state) {
148 	case RXRPC_UNBOUND:
149 		rx->srx = *srx;
150 		local = rxrpc_lookup_local(sock_net(&rx->sk), &rx->srx);
151 		if (IS_ERR(local)) {
152 			ret = PTR_ERR(local);
153 			goto error_unlock;
154 		}
155 
156 		if (service_id) {
157 			write_lock(&local->services_lock);
158 			if (rcu_access_pointer(local->service))
159 				goto service_in_use;
160 			rx->local = local;
161 			rcu_assign_pointer(local->service, rx);
162 			write_unlock(&local->services_lock);
163 
164 			rx->sk.sk_state = RXRPC_SERVER_BOUND;
165 		} else {
166 			rx->local = local;
167 			rx->sk.sk_state = RXRPC_CLIENT_BOUND;
168 		}
169 		break;
170 
171 	case RXRPC_SERVER_BOUND:
172 		ret = -EINVAL;
173 		if (service_id == 0)
174 			goto error_unlock;
175 		ret = -EADDRINUSE;
176 		if (service_id == rx->srx.srx_service)
177 			goto error_unlock;
178 		ret = -EINVAL;
179 		srx->srx_service = rx->srx.srx_service;
180 		if (memcmp(srx, &rx->srx, sizeof(*srx)) != 0)
181 			goto error_unlock;
182 		rx->second_service = service_id;
183 		rx->sk.sk_state = RXRPC_SERVER_BOUND2;
184 		break;
185 
186 	default:
187 		ret = -EINVAL;
188 		goto error_unlock;
189 	}
190 
191 	release_sock(&rx->sk);
192 	_leave(" = 0");
193 	return 0;
194 
195 service_in_use:
196 	write_unlock(&local->services_lock);
197 	rxrpc_unuse_local(local, rxrpc_local_unuse_bind);
198 	rxrpc_put_local(local, rxrpc_local_put_bind);
199 	ret = -EADDRINUSE;
200 error_unlock:
201 	release_sock(&rx->sk);
202 error:
203 	_leave(" = %d", ret);
204 	return ret;
205 }
206 
207 /*
208  * set the number of pending calls permitted on a listening socket
209  */
210 static int rxrpc_listen(struct socket *sock, int backlog)
211 {
212 	struct sock *sk = sock->sk;
213 	struct rxrpc_sock *rx = rxrpc_sk(sk);
214 	unsigned int max, old;
215 	int ret;
216 
217 	_enter("%p,%d", rx, backlog);
218 
219 	lock_sock(&rx->sk);
220 
221 	switch (rx->sk.sk_state) {
222 	case RXRPC_UNBOUND:
223 		ret = -EADDRNOTAVAIL;
224 		break;
225 	case RXRPC_SERVER_BOUND:
226 	case RXRPC_SERVER_BOUND2:
227 		ASSERT(rx->local != NULL);
228 		max = READ_ONCE(rxrpc_max_backlog);
229 		ret = -EINVAL;
230 		if (backlog == INT_MAX)
231 			backlog = max;
232 		else if (backlog < 0 || backlog > max)
233 			break;
234 		old = sk->sk_max_ack_backlog;
235 		sk->sk_max_ack_backlog = backlog;
236 		ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
237 		if (ret == 0)
238 			rx->sk.sk_state = RXRPC_SERVER_LISTENING;
239 		else
240 			sk->sk_max_ack_backlog = old;
241 		break;
242 	case RXRPC_SERVER_LISTENING:
243 		if (backlog == 0) {
244 			rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
245 			sk->sk_max_ack_backlog = 0;
246 			rxrpc_discard_prealloc(rx);
247 			ret = 0;
248 			break;
249 		}
250 		fallthrough;
251 	default:
252 		ret = -EBUSY;
253 		break;
254 	}
255 
256 	release_sock(&rx->sk);
257 	_leave(" = %d", ret);
258 	return ret;
259 }
260 
261 /**
262  * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
263  * @sock: The socket on which to make the call
264  * @srx: The address of the peer to contact
265  * @key: The security context to use (defaults to socket setting)
266  * @user_call_ID: The ID to use
267  * @tx_total_len: Total length of data to transmit during the call (or -1)
268  * @gfp: The allocation constraints
269  * @notify_rx: Where to send notifications instead of socket queue
270  * @upgrade: Request service upgrade for call
271  * @interruptibility: The call is interruptible, or can be canceled.
272  * @debug_id: The debug ID for tracing to be assigned to the call
273  *
274  * Allow a kernel service to begin a call on the nominated socket.  This just
275  * sets up all the internal tracking structures and allocates connection and
276  * call IDs as appropriate.  The call to be used is returned.
277  *
278  * The default socket destination address and security may be overridden by
279  * supplying @srx and @key.
280  */
281 struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
282 					   struct sockaddr_rxrpc *srx,
283 					   struct key *key,
284 					   unsigned long user_call_ID,
285 					   s64 tx_total_len,
286 					   gfp_t gfp,
287 					   rxrpc_notify_rx_t notify_rx,
288 					   bool upgrade,
289 					   enum rxrpc_interruptibility interruptibility,
290 					   unsigned int debug_id)
291 {
292 	struct rxrpc_conn_parameters cp;
293 	struct rxrpc_call_params p;
294 	struct rxrpc_call *call;
295 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
296 	int ret;
297 
298 	_enter(",,%x,%lx", key_serial(key), user_call_ID);
299 
300 	ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
301 	if (ret < 0)
302 		return ERR_PTR(ret);
303 
304 	lock_sock(&rx->sk);
305 
306 	if (!key)
307 		key = rx->key;
308 	if (key && !key->payload.data[0])
309 		key = NULL; /* a no-security key */
310 
311 	memset(&p, 0, sizeof(p));
312 	p.user_call_ID		= user_call_ID;
313 	p.tx_total_len		= tx_total_len;
314 	p.interruptibility	= interruptibility;
315 	p.kernel		= true;
316 
317 	memset(&cp, 0, sizeof(cp));
318 	cp.local		= rx->local;
319 	cp.key			= key;
320 	cp.security_level	= rx->min_sec_level;
321 	cp.exclusive		= false;
322 	cp.upgrade		= upgrade;
323 	cp.service_id		= srx->srx_service;
324 	call = rxrpc_new_client_call(rx, &cp, srx, &p, gfp, debug_id);
325 	/* The socket has been unlocked. */
326 	if (!IS_ERR(call)) {
327 		call->notify_rx = notify_rx;
328 		mutex_unlock(&call->user_mutex);
329 	}
330 
331 	rxrpc_put_peer(cp.peer, rxrpc_peer_put_discard_tmp);
332 	_leave(" = %p", call);
333 	return call;
334 }
335 EXPORT_SYMBOL(rxrpc_kernel_begin_call);
336 
337 /*
338  * Dummy function used to stop the notifier talking to recvmsg().
339  */
340 static void rxrpc_dummy_notify_rx(struct sock *sk, struct rxrpc_call *rxcall,
341 				  unsigned long call_user_ID)
342 {
343 }
344 
345 /**
346  * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
347  * @sock: The socket the call is on
348  * @call: The call to end
349  *
350  * Allow a kernel service to end a call it was using.  The call must be
351  * complete before this is called (the call should be aborted if necessary).
352  */
353 void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
354 {
355 	_enter("%d{%d}", call->debug_id, refcount_read(&call->ref));
356 
357 	mutex_lock(&call->user_mutex);
358 	rxrpc_release_call(rxrpc_sk(sock->sk), call);
359 
360 	/* Make sure we're not going to call back into a kernel service */
361 	if (call->notify_rx) {
362 		spin_lock(&call->notify_lock);
363 		call->notify_rx = rxrpc_dummy_notify_rx;
364 		spin_unlock(&call->notify_lock);
365 	}
366 
367 	mutex_unlock(&call->user_mutex);
368 	rxrpc_put_call(call, rxrpc_call_put_kernel);
369 }
370 EXPORT_SYMBOL(rxrpc_kernel_end_call);
371 
372 /**
373  * rxrpc_kernel_check_life - Check to see whether a call is still alive
374  * @sock: The socket the call is on
375  * @call: The call to check
376  *
377  * Allow a kernel service to find out whether a call is still alive -
378  * ie. whether it has completed.
379  */
380 bool rxrpc_kernel_check_life(const struct socket *sock,
381 			     const struct rxrpc_call *call)
382 {
383 	return call->state != RXRPC_CALL_COMPLETE;
384 }
385 EXPORT_SYMBOL(rxrpc_kernel_check_life);
386 
387 /**
388  * rxrpc_kernel_get_epoch - Retrieve the epoch value from a call.
389  * @sock: The socket the call is on
390  * @call: The call to query
391  *
392  * Allow a kernel service to retrieve the epoch value from a service call to
393  * see if the client at the other end rebooted.
394  */
395 u32 rxrpc_kernel_get_epoch(struct socket *sock, struct rxrpc_call *call)
396 {
397 	return call->conn->proto.epoch;
398 }
399 EXPORT_SYMBOL(rxrpc_kernel_get_epoch);
400 
401 /**
402  * rxrpc_kernel_new_call_notification - Get notifications of new calls
403  * @sock: The socket to intercept received messages on
404  * @notify_new_call: Function to be called when new calls appear
405  * @discard_new_call: Function to discard preallocated calls
406  *
407  * Allow a kernel service to be given notifications about new calls.
408  */
409 void rxrpc_kernel_new_call_notification(
410 	struct socket *sock,
411 	rxrpc_notify_new_call_t notify_new_call,
412 	rxrpc_discard_new_call_t discard_new_call)
413 {
414 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
415 
416 	rx->notify_new_call = notify_new_call;
417 	rx->discard_new_call = discard_new_call;
418 }
419 EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
420 
421 /**
422  * rxrpc_kernel_set_max_life - Set maximum lifespan on a call
423  * @sock: The socket the call is on
424  * @call: The call to configure
425  * @hard_timeout: The maximum lifespan of the call in jiffies
426  *
427  * Set the maximum lifespan of a call.  The call will end with ETIME or
428  * ETIMEDOUT if it takes longer than this.
429  */
430 void rxrpc_kernel_set_max_life(struct socket *sock, struct rxrpc_call *call,
431 			       unsigned long hard_timeout)
432 {
433 	unsigned long now;
434 
435 	mutex_lock(&call->user_mutex);
436 
437 	now = jiffies;
438 	hard_timeout += now;
439 	WRITE_ONCE(call->expect_term_by, hard_timeout);
440 	rxrpc_reduce_call_timer(call, hard_timeout, now, rxrpc_timer_set_for_hard);
441 
442 	mutex_unlock(&call->user_mutex);
443 }
444 EXPORT_SYMBOL(rxrpc_kernel_set_max_life);
445 
446 /*
447  * connect an RxRPC socket
448  * - this just targets it at a specific destination; no actual connection
449  *   negotiation takes place
450  */
451 static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
452 			 int addr_len, int flags)
453 {
454 	struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
455 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
456 	int ret;
457 
458 	_enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
459 
460 	ret = rxrpc_validate_address(rx, srx, addr_len);
461 	if (ret < 0) {
462 		_leave(" = %d [bad addr]", ret);
463 		return ret;
464 	}
465 
466 	lock_sock(&rx->sk);
467 
468 	ret = -EISCONN;
469 	if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
470 		goto error;
471 
472 	switch (rx->sk.sk_state) {
473 	case RXRPC_UNBOUND:
474 		rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
475 		break;
476 	case RXRPC_CLIENT_UNBOUND:
477 	case RXRPC_CLIENT_BOUND:
478 		break;
479 	default:
480 		ret = -EBUSY;
481 		goto error;
482 	}
483 
484 	rx->connect_srx = *srx;
485 	set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
486 	ret = 0;
487 
488 error:
489 	release_sock(&rx->sk);
490 	return ret;
491 }
492 
493 /*
494  * send a message through an RxRPC socket
495  * - in a client this does a number of things:
496  *   - finds/sets up a connection for the security specified (if any)
497  *   - initiates a call (ID in control data)
498  *   - ends the request phase of a call (if MSG_MORE is not set)
499  *   - sends a call data packet
500  *   - may send an abort (abort code in control data)
501  */
502 static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
503 {
504 	struct rxrpc_local *local;
505 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
506 	int ret;
507 
508 	_enter(",{%d},,%zu", rx->sk.sk_state, len);
509 
510 	if (m->msg_flags & MSG_OOB)
511 		return -EOPNOTSUPP;
512 
513 	if (m->msg_name) {
514 		ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
515 		if (ret < 0) {
516 			_leave(" = %d [bad addr]", ret);
517 			return ret;
518 		}
519 	}
520 
521 	lock_sock(&rx->sk);
522 
523 	switch (rx->sk.sk_state) {
524 	case RXRPC_UNBOUND:
525 	case RXRPC_CLIENT_UNBOUND:
526 		rx->srx.srx_family = AF_RXRPC;
527 		rx->srx.srx_service = 0;
528 		rx->srx.transport_type = SOCK_DGRAM;
529 		rx->srx.transport.family = rx->family;
530 		switch (rx->family) {
531 		case AF_INET:
532 			rx->srx.transport_len = sizeof(struct sockaddr_in);
533 			break;
534 #ifdef CONFIG_AF_RXRPC_IPV6
535 		case AF_INET6:
536 			rx->srx.transport_len = sizeof(struct sockaddr_in6);
537 			break;
538 #endif
539 		default:
540 			ret = -EAFNOSUPPORT;
541 			goto error_unlock;
542 		}
543 		local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
544 		if (IS_ERR(local)) {
545 			ret = PTR_ERR(local);
546 			goto error_unlock;
547 		}
548 
549 		rx->local = local;
550 		rx->sk.sk_state = RXRPC_CLIENT_BOUND;
551 		fallthrough;
552 
553 	case RXRPC_CLIENT_BOUND:
554 		if (!m->msg_name &&
555 		    test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
556 			m->msg_name = &rx->connect_srx;
557 			m->msg_namelen = sizeof(rx->connect_srx);
558 		}
559 		fallthrough;
560 	case RXRPC_SERVER_BOUND:
561 	case RXRPC_SERVER_LISTENING:
562 		ret = rxrpc_do_sendmsg(rx, m, len);
563 		/* The socket has been unlocked */
564 		goto out;
565 	default:
566 		ret = -EINVAL;
567 		goto error_unlock;
568 	}
569 
570 error_unlock:
571 	release_sock(&rx->sk);
572 out:
573 	_leave(" = %d", ret);
574 	return ret;
575 }
576 
577 int rxrpc_sock_set_min_security_level(struct sock *sk, unsigned int val)
578 {
579 	if (sk->sk_state != RXRPC_UNBOUND)
580 		return -EISCONN;
581 	if (val > RXRPC_SECURITY_MAX)
582 		return -EINVAL;
583 	lock_sock(sk);
584 	rxrpc_sk(sk)->min_sec_level = val;
585 	release_sock(sk);
586 	return 0;
587 }
588 EXPORT_SYMBOL(rxrpc_sock_set_min_security_level);
589 
590 /*
591  * set RxRPC socket options
592  */
593 static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
594 			    sockptr_t optval, unsigned int optlen)
595 {
596 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
597 	unsigned int min_sec_level;
598 	u16 service_upgrade[2];
599 	int ret;
600 
601 	_enter(",%d,%d,,%d", level, optname, optlen);
602 
603 	lock_sock(&rx->sk);
604 	ret = -EOPNOTSUPP;
605 
606 	if (level == SOL_RXRPC) {
607 		switch (optname) {
608 		case RXRPC_EXCLUSIVE_CONNECTION:
609 			ret = -EINVAL;
610 			if (optlen != 0)
611 				goto error;
612 			ret = -EISCONN;
613 			if (rx->sk.sk_state != RXRPC_UNBOUND)
614 				goto error;
615 			rx->exclusive = true;
616 			goto success;
617 
618 		case RXRPC_SECURITY_KEY:
619 			ret = -EINVAL;
620 			if (rx->key)
621 				goto error;
622 			ret = -EISCONN;
623 			if (rx->sk.sk_state != RXRPC_UNBOUND)
624 				goto error;
625 			ret = rxrpc_request_key(rx, optval, optlen);
626 			goto error;
627 
628 		case RXRPC_SECURITY_KEYRING:
629 			ret = -EINVAL;
630 			if (rx->key)
631 				goto error;
632 			ret = -EISCONN;
633 			if (rx->sk.sk_state != RXRPC_UNBOUND)
634 				goto error;
635 			ret = rxrpc_server_keyring(rx, optval, optlen);
636 			goto error;
637 
638 		case RXRPC_MIN_SECURITY_LEVEL:
639 			ret = -EINVAL;
640 			if (optlen != sizeof(unsigned int))
641 				goto error;
642 			ret = -EISCONN;
643 			if (rx->sk.sk_state != RXRPC_UNBOUND)
644 				goto error;
645 			ret = copy_from_sockptr(&min_sec_level, optval,
646 				       sizeof(unsigned int));
647 			if (ret < 0)
648 				goto error;
649 			ret = -EINVAL;
650 			if (min_sec_level > RXRPC_SECURITY_MAX)
651 				goto error;
652 			rx->min_sec_level = min_sec_level;
653 			goto success;
654 
655 		case RXRPC_UPGRADEABLE_SERVICE:
656 			ret = -EINVAL;
657 			if (optlen != sizeof(service_upgrade) ||
658 			    rx->service_upgrade.from != 0)
659 				goto error;
660 			ret = -EISCONN;
661 			if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
662 				goto error;
663 			ret = -EFAULT;
664 			if (copy_from_sockptr(service_upgrade, optval,
665 					   sizeof(service_upgrade)) != 0)
666 				goto error;
667 			ret = -EINVAL;
668 			if ((service_upgrade[0] != rx->srx.srx_service ||
669 			     service_upgrade[1] != rx->second_service) &&
670 			    (service_upgrade[0] != rx->second_service ||
671 			     service_upgrade[1] != rx->srx.srx_service))
672 				goto error;
673 			rx->service_upgrade.from = service_upgrade[0];
674 			rx->service_upgrade.to = service_upgrade[1];
675 			goto success;
676 
677 		default:
678 			break;
679 		}
680 	}
681 
682 success:
683 	ret = 0;
684 error:
685 	release_sock(&rx->sk);
686 	return ret;
687 }
688 
689 /*
690  * Get socket options.
691  */
692 static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
693 			    char __user *optval, int __user *_optlen)
694 {
695 	int optlen;
696 
697 	if (level != SOL_RXRPC)
698 		return -EOPNOTSUPP;
699 
700 	if (get_user(optlen, _optlen))
701 		return -EFAULT;
702 
703 	switch (optname) {
704 	case RXRPC_SUPPORTED_CMSG:
705 		if (optlen < sizeof(int))
706 			return -ETOOSMALL;
707 		if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
708 		    put_user(sizeof(int), _optlen))
709 			return -EFAULT;
710 		return 0;
711 
712 	default:
713 		return -EOPNOTSUPP;
714 	}
715 }
716 
717 /*
718  * permit an RxRPC socket to be polled
719  */
720 static __poll_t rxrpc_poll(struct file *file, struct socket *sock,
721 			       poll_table *wait)
722 {
723 	struct sock *sk = sock->sk;
724 	struct rxrpc_sock *rx = rxrpc_sk(sk);
725 	__poll_t mask;
726 
727 	sock_poll_wait(file, sock, wait);
728 	mask = 0;
729 
730 	/* the socket is readable if there are any messages waiting on the Rx
731 	 * queue */
732 	if (!list_empty(&rx->recvmsg_q))
733 		mask |= EPOLLIN | EPOLLRDNORM;
734 
735 	/* the socket is writable if there is space to add new data to the
736 	 * socket; there is no guarantee that any particular call in progress
737 	 * on the socket may have space in the Tx ACK window */
738 	if (rxrpc_writable(sk))
739 		mask |= EPOLLOUT | EPOLLWRNORM;
740 
741 	return mask;
742 }
743 
744 /*
745  * create an RxRPC socket
746  */
747 static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
748 			int kern)
749 {
750 	struct rxrpc_net *rxnet;
751 	struct rxrpc_sock *rx;
752 	struct sock *sk;
753 
754 	_enter("%p,%d", sock, protocol);
755 
756 	/* we support transport protocol UDP/UDP6 only */
757 	if (protocol != PF_INET &&
758 	    IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
759 		return -EPROTONOSUPPORT;
760 
761 	if (sock->type != SOCK_DGRAM)
762 		return -ESOCKTNOSUPPORT;
763 
764 	sock->ops = &rxrpc_rpc_ops;
765 	sock->state = SS_UNCONNECTED;
766 
767 	sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
768 	if (!sk)
769 		return -ENOMEM;
770 
771 	sock_init_data(sock, sk);
772 	sock_set_flag(sk, SOCK_RCU_FREE);
773 	sk->sk_state		= RXRPC_UNBOUND;
774 	sk->sk_write_space	= rxrpc_write_space;
775 	sk->sk_max_ack_backlog	= 0;
776 	sk->sk_destruct		= rxrpc_sock_destructor;
777 
778 	rx = rxrpc_sk(sk);
779 	rx->family = protocol;
780 	rx->calls = RB_ROOT;
781 
782 	spin_lock_init(&rx->incoming_lock);
783 	INIT_LIST_HEAD(&rx->sock_calls);
784 	INIT_LIST_HEAD(&rx->to_be_accepted);
785 	INIT_LIST_HEAD(&rx->recvmsg_q);
786 	rwlock_init(&rx->recvmsg_lock);
787 	rwlock_init(&rx->call_lock);
788 	memset(&rx->srx, 0, sizeof(rx->srx));
789 
790 	rxnet = rxrpc_net(sock_net(&rx->sk));
791 	timer_reduce(&rxnet->peer_keepalive_timer, jiffies + 1);
792 
793 	_leave(" = 0 [%p]", rx);
794 	return 0;
795 }
796 
797 /*
798  * Kill all the calls on a socket and shut it down.
799  */
800 static int rxrpc_shutdown(struct socket *sock, int flags)
801 {
802 	struct sock *sk = sock->sk;
803 	struct rxrpc_sock *rx = rxrpc_sk(sk);
804 	int ret = 0;
805 
806 	_enter("%p,%d", sk, flags);
807 
808 	if (flags != SHUT_RDWR)
809 		return -EOPNOTSUPP;
810 	if (sk->sk_state == RXRPC_CLOSE)
811 		return -ESHUTDOWN;
812 
813 	lock_sock(sk);
814 
815 	if (sk->sk_state < RXRPC_CLOSE) {
816 		sk->sk_state = RXRPC_CLOSE;
817 		sk->sk_shutdown = SHUTDOWN_MASK;
818 	} else {
819 		ret = -ESHUTDOWN;
820 	}
821 
822 	rxrpc_discard_prealloc(rx);
823 
824 	release_sock(sk);
825 	return ret;
826 }
827 
828 /*
829  * RxRPC socket destructor
830  */
831 static void rxrpc_sock_destructor(struct sock *sk)
832 {
833 	_enter("%p", sk);
834 
835 	rxrpc_purge_queue(&sk->sk_receive_queue);
836 
837 	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
838 	WARN_ON(!sk_unhashed(sk));
839 	WARN_ON(sk->sk_socket);
840 
841 	if (!sock_flag(sk, SOCK_DEAD)) {
842 		printk("Attempt to release alive rxrpc socket: %p\n", sk);
843 		return;
844 	}
845 }
846 
847 /*
848  * release an RxRPC socket
849  */
850 static int rxrpc_release_sock(struct sock *sk)
851 {
852 	struct rxrpc_sock *rx = rxrpc_sk(sk);
853 
854 	_enter("%p{%d,%d}", sk, sk->sk_state, refcount_read(&sk->sk_refcnt));
855 
856 	/* declare the socket closed for business */
857 	sock_orphan(sk);
858 	sk->sk_shutdown = SHUTDOWN_MASK;
859 
860 	/* We want to kill off all connections from a service socket
861 	 * as fast as possible because we can't share these; client
862 	 * sockets, on the other hand, can share an endpoint.
863 	 */
864 	switch (sk->sk_state) {
865 	case RXRPC_SERVER_BOUND:
866 	case RXRPC_SERVER_BOUND2:
867 	case RXRPC_SERVER_LISTENING:
868 	case RXRPC_SERVER_LISTEN_DISABLED:
869 		rx->local->service_closed = true;
870 		break;
871 	}
872 
873 	sk->sk_state = RXRPC_CLOSE;
874 
875 	if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
876 		write_lock(&rx->local->services_lock);
877 		rcu_assign_pointer(rx->local->service, NULL);
878 		write_unlock(&rx->local->services_lock);
879 	}
880 
881 	/* try to flush out this socket */
882 	rxrpc_discard_prealloc(rx);
883 	rxrpc_release_calls_on_socket(rx);
884 	flush_workqueue(rxrpc_workqueue);
885 	rxrpc_purge_queue(&sk->sk_receive_queue);
886 
887 	rxrpc_unuse_local(rx->local, rxrpc_local_unuse_release_sock);
888 	rxrpc_put_local(rx->local, rxrpc_local_put_release_sock);
889 	rx->local = NULL;
890 	key_put(rx->key);
891 	rx->key = NULL;
892 	key_put(rx->securities);
893 	rx->securities = NULL;
894 	sock_put(sk);
895 
896 	_leave(" = 0");
897 	return 0;
898 }
899 
900 /*
901  * release an RxRPC BSD socket on close() or equivalent
902  */
903 static int rxrpc_release(struct socket *sock)
904 {
905 	struct sock *sk = sock->sk;
906 
907 	_enter("%p{%p}", sock, sk);
908 
909 	if (!sk)
910 		return 0;
911 
912 	sock->sk = NULL;
913 
914 	return rxrpc_release_sock(sk);
915 }
916 
917 /*
918  * RxRPC network protocol
919  */
920 static const struct proto_ops rxrpc_rpc_ops = {
921 	.family		= PF_RXRPC,
922 	.owner		= THIS_MODULE,
923 	.release	= rxrpc_release,
924 	.bind		= rxrpc_bind,
925 	.connect	= rxrpc_connect,
926 	.socketpair	= sock_no_socketpair,
927 	.accept		= sock_no_accept,
928 	.getname	= sock_no_getname,
929 	.poll		= rxrpc_poll,
930 	.ioctl		= sock_no_ioctl,
931 	.listen		= rxrpc_listen,
932 	.shutdown	= rxrpc_shutdown,
933 	.setsockopt	= rxrpc_setsockopt,
934 	.getsockopt	= rxrpc_getsockopt,
935 	.sendmsg	= rxrpc_sendmsg,
936 	.recvmsg	= rxrpc_recvmsg,
937 	.mmap		= sock_no_mmap,
938 	.sendpage	= sock_no_sendpage,
939 };
940 
941 static struct proto rxrpc_proto = {
942 	.name		= "RXRPC",
943 	.owner		= THIS_MODULE,
944 	.obj_size	= sizeof(struct rxrpc_sock),
945 	.max_header	= sizeof(struct rxrpc_wire_header),
946 };
947 
948 static const struct net_proto_family rxrpc_family_ops = {
949 	.family	= PF_RXRPC,
950 	.create = rxrpc_create,
951 	.owner	= THIS_MODULE,
952 };
953 
954 /*
955  * initialise and register the RxRPC protocol
956  */
957 static int __init af_rxrpc_init(void)
958 {
959 	int ret = -1;
960 	unsigned int tmp;
961 
962 	BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof_field(struct sk_buff, cb));
963 
964 	get_random_bytes(&tmp, sizeof(tmp));
965 	tmp &= 0x3fffffff;
966 	if (tmp == 0)
967 		tmp = 1;
968 	idr_set_cursor(&rxrpc_client_conn_ids, tmp);
969 
970 	ret = -ENOMEM;
971 	rxrpc_call_jar = kmem_cache_create(
972 		"rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
973 		SLAB_HWCACHE_ALIGN, NULL);
974 	if (!rxrpc_call_jar) {
975 		pr_notice("Failed to allocate call jar\n");
976 		goto error_call_jar;
977 	}
978 
979 	rxrpc_workqueue = alloc_workqueue("krxrpcd", WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
980 	if (!rxrpc_workqueue) {
981 		pr_notice("Failed to allocate work queue\n");
982 		goto error_work_queue;
983 	}
984 
985 	ret = rxrpc_init_security();
986 	if (ret < 0) {
987 		pr_crit("Cannot initialise security\n");
988 		goto error_security;
989 	}
990 
991 	ret = register_pernet_device(&rxrpc_net_ops);
992 	if (ret)
993 		goto error_pernet;
994 
995 	ret = proto_register(&rxrpc_proto, 1);
996 	if (ret < 0) {
997 		pr_crit("Cannot register protocol\n");
998 		goto error_proto;
999 	}
1000 
1001 	ret = sock_register(&rxrpc_family_ops);
1002 	if (ret < 0) {
1003 		pr_crit("Cannot register socket family\n");
1004 		goto error_sock;
1005 	}
1006 
1007 	ret = register_key_type(&key_type_rxrpc);
1008 	if (ret < 0) {
1009 		pr_crit("Cannot register client key type\n");
1010 		goto error_key_type;
1011 	}
1012 
1013 	ret = register_key_type(&key_type_rxrpc_s);
1014 	if (ret < 0) {
1015 		pr_crit("Cannot register server key type\n");
1016 		goto error_key_type_s;
1017 	}
1018 
1019 	ret = rxrpc_sysctl_init();
1020 	if (ret < 0) {
1021 		pr_crit("Cannot register sysctls\n");
1022 		goto error_sysctls;
1023 	}
1024 
1025 	return 0;
1026 
1027 error_sysctls:
1028 	unregister_key_type(&key_type_rxrpc_s);
1029 error_key_type_s:
1030 	unregister_key_type(&key_type_rxrpc);
1031 error_key_type:
1032 	sock_unregister(PF_RXRPC);
1033 error_sock:
1034 	proto_unregister(&rxrpc_proto);
1035 error_proto:
1036 	unregister_pernet_device(&rxrpc_net_ops);
1037 error_pernet:
1038 	rxrpc_exit_security();
1039 error_security:
1040 	destroy_workqueue(rxrpc_workqueue);
1041 error_work_queue:
1042 	kmem_cache_destroy(rxrpc_call_jar);
1043 error_call_jar:
1044 	return ret;
1045 }
1046 
1047 /*
1048  * unregister the RxRPC protocol
1049  */
1050 static void __exit af_rxrpc_exit(void)
1051 {
1052 	_enter("");
1053 	rxrpc_sysctl_exit();
1054 	unregister_key_type(&key_type_rxrpc_s);
1055 	unregister_key_type(&key_type_rxrpc);
1056 	sock_unregister(PF_RXRPC);
1057 	proto_unregister(&rxrpc_proto);
1058 	unregister_pernet_device(&rxrpc_net_ops);
1059 	ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
1060 
1061 	/* Make sure the local and peer records pinned by any dying connections
1062 	 * are released.
1063 	 */
1064 	rcu_barrier();
1065 	rxrpc_destroy_client_conn_ids();
1066 
1067 	destroy_workqueue(rxrpc_workqueue);
1068 	rxrpc_exit_security();
1069 	kmem_cache_destroy(rxrpc_call_jar);
1070 	_leave("");
1071 }
1072 
1073 module_init(af_rxrpc_init);
1074 module_exit(af_rxrpc_exit);
1075