xref: /linux/net/rxrpc/sendmsg.c (revision 56d95b0adfa224bb1c67733dbcad30dd8debd39e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AF_RXRPC sendmsg() implementation.
3  *
4  * Copyright (C) 2007, 2016 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/net.h>
11 #include <linux/gfp.h>
12 #include <linux/skbuff.h>
13 #include <linux/export.h>
14 #include <linux/sched/signal.h>
15 
16 #include <net/sock.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
19 
20 /*
21  * Propose an abort to be made in the I/O thread.
22  */
23 bool rxrpc_propose_abort(struct rxrpc_call *call, s32 abort_code, int error,
24 			 enum rxrpc_abort_reason why)
25 {
26 	_enter("{%d},%d,%d,%u", call->debug_id, abort_code, error, why);
27 
28 	if (!call->send_abort && !rxrpc_call_is_complete(call)) {
29 		call->send_abort_why = why;
30 		call->send_abort_err = error;
31 		call->send_abort_seq = 0;
32 		trace_rxrpc_abort_call(call, abort_code);
33 		/* Request abort locklessly vs rxrpc_input_call_event(). */
34 		smp_store_release(&call->send_abort, abort_code);
35 		rxrpc_poke_call(call, rxrpc_call_poke_abort);
36 		return true;
37 	}
38 
39 	return false;
40 }
41 
42 /*
43  * Wait for a call to become connected.  Interruption here doesn't cause the
44  * call to be aborted.
45  */
46 static int rxrpc_wait_to_be_connected(struct rxrpc_call *call, long *timeo)
47 {
48 	DECLARE_WAITQUEUE(myself, current);
49 	int ret = 0;
50 
51 	_enter("%d", call->debug_id);
52 
53 	if (rxrpc_call_state(call) != RXRPC_CALL_CLIENT_AWAIT_CONN)
54 		goto no_wait;
55 
56 	add_wait_queue_exclusive(&call->waitq, &myself);
57 
58 	for (;;) {
59 		switch (call->interruptibility) {
60 		case RXRPC_INTERRUPTIBLE:
61 		case RXRPC_PREINTERRUPTIBLE:
62 			set_current_state(TASK_INTERRUPTIBLE);
63 			break;
64 		case RXRPC_UNINTERRUPTIBLE:
65 		default:
66 			set_current_state(TASK_UNINTERRUPTIBLE);
67 			break;
68 		}
69 
70 		if (rxrpc_call_state(call) != RXRPC_CALL_CLIENT_AWAIT_CONN)
71 			break;
72 		if ((call->interruptibility == RXRPC_INTERRUPTIBLE ||
73 		     call->interruptibility == RXRPC_PREINTERRUPTIBLE) &&
74 		    signal_pending(current)) {
75 			ret = sock_intr_errno(*timeo);
76 			break;
77 		}
78 		*timeo = schedule_timeout(*timeo);
79 	}
80 
81 	remove_wait_queue(&call->waitq, &myself);
82 	__set_current_state(TASK_RUNNING);
83 
84 no_wait:
85 	if (ret == 0 && rxrpc_call_is_complete(call))
86 		ret = call->error;
87 
88 	_leave(" = %d", ret);
89 	return ret;
90 }
91 
92 /*
93  * Return true if there's sufficient Tx queue space.
94  */
95 static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win)
96 {
97 	rxrpc_seq_t tx_bottom = READ_ONCE(call->tx_bottom);
98 
99 	if (_tx_win)
100 		*_tx_win = tx_bottom;
101 	return call->send_top - tx_bottom < 256;
102 }
103 
104 /*
105  * Wait for space to appear in the Tx queue or a signal to occur.
106  */
107 static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
108 					 struct rxrpc_call *call,
109 					 long *timeo)
110 {
111 	for (;;) {
112 		set_current_state(TASK_INTERRUPTIBLE);
113 		if (rxrpc_check_tx_space(call, NULL))
114 			return 0;
115 
116 		if (rxrpc_call_is_complete(call))
117 			return call->error;
118 
119 		if (signal_pending(current))
120 			return sock_intr_errno(*timeo);
121 
122 		trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
123 		*timeo = schedule_timeout(*timeo);
124 	}
125 }
126 
127 /*
128  * Wait for space to appear in the Tx queue uninterruptibly, but with
129  * a timeout of 2*RTT if no progress was made and a signal occurred.
130  */
131 static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
132 					    struct rxrpc_call *call)
133 {
134 	rxrpc_seq_t tx_start, tx_win;
135 	signed long rtt, timeout;
136 
137 	rtt = READ_ONCE(call->srtt_us) >> 3;
138 	rtt = usecs_to_jiffies(rtt) * 2;
139 	if (rtt < 2)
140 		rtt = 2;
141 
142 	timeout = rtt;
143 	tx_start = READ_ONCE(call->tx_bottom);
144 
145 	for (;;) {
146 		set_current_state(TASK_UNINTERRUPTIBLE);
147 
148 		if (rxrpc_check_tx_space(call, &tx_win))
149 			return 0;
150 
151 		if (rxrpc_call_is_complete(call))
152 			return call->error;
153 
154 		if (timeout == 0 &&
155 		    tx_win == tx_start && signal_pending(current))
156 			return -EINTR;
157 
158 		if (tx_win != tx_start) {
159 			timeout = rtt;
160 			tx_start = tx_win;
161 		}
162 
163 		trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
164 		timeout = schedule_timeout(timeout);
165 	}
166 }
167 
168 /*
169  * Wait for space to appear in the Tx queue uninterruptibly.
170  */
171 static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
172 					    struct rxrpc_call *call,
173 					    long *timeo)
174 {
175 	for (;;) {
176 		set_current_state(TASK_UNINTERRUPTIBLE);
177 		if (rxrpc_check_tx_space(call, NULL))
178 			return 0;
179 
180 		if (rxrpc_call_is_complete(call))
181 			return call->error;
182 
183 		trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
184 		*timeo = schedule_timeout(*timeo);
185 	}
186 }
187 
188 /*
189  * wait for space to appear in the transmit/ACK window
190  * - caller holds the socket locked
191  */
192 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
193 				    struct rxrpc_call *call,
194 				    long *timeo,
195 				    bool waitall)
196 {
197 	DECLARE_WAITQUEUE(myself, current);
198 	int ret;
199 
200 	_enter(",{%u,%u,%u}",
201 	       call->tx_bottom, call->tx_top, call->tx_winsize);
202 
203 	add_wait_queue(&call->waitq, &myself);
204 
205 	switch (call->interruptibility) {
206 	case RXRPC_INTERRUPTIBLE:
207 		if (waitall)
208 			ret = rxrpc_wait_for_tx_window_waitall(rx, call);
209 		else
210 			ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
211 		break;
212 	case RXRPC_PREINTERRUPTIBLE:
213 	case RXRPC_UNINTERRUPTIBLE:
214 	default:
215 		ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
216 		break;
217 	}
218 
219 	remove_wait_queue(&call->waitq, &myself);
220 	set_current_state(TASK_RUNNING);
221 	_leave(" = %d", ret);
222 	return ret;
223 }
224 
225 /*
226  * Notify the owner of the call that the transmit phase is ended and the last
227  * packet has been queued.
228  */
229 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
230 				rxrpc_notify_end_tx_t notify_end_tx)
231 {
232 	if (notify_end_tx)
233 		notify_end_tx(&rx->sk, call, call->user_call_ID);
234 }
235 
236 /*
237  * Queue a DATA packet for transmission, set the resend timeout and send
238  * the packet immediately.  Returns the error from rxrpc_send_data_packet()
239  * in case the caller wants to do something with it.
240  */
241 static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
242 			       struct rxrpc_txbuf *txb,
243 			       rxrpc_notify_end_tx_t notify_end_tx)
244 {
245 	struct rxrpc_txqueue *sq = call->send_queue;
246 	rxrpc_seq_t seq = txb->seq;
247 	bool poke, last = txb->flags & RXRPC_LAST_PACKET;
248 	int ix = seq & RXRPC_TXQ_MASK;
249 	rxrpc_inc_stat(call->rxnet, stat_tx_data);
250 
251 	ASSERTCMP(txb->seq, ==, call->send_top + 1);
252 
253 	if (last)
254 		trace_rxrpc_txqueue(call, rxrpc_txqueue_queue_last);
255 	else
256 		trace_rxrpc_txqueue(call, rxrpc_txqueue_queue);
257 
258 	if (WARN_ON_ONCE(sq->bufs[ix]))
259 		trace_rxrpc_tq(call, sq, seq, rxrpc_tq_queue_dup);
260 	else
261 		trace_rxrpc_tq(call, sq, seq, rxrpc_tq_queue);
262 
263 	/* Add the packet to the call's output buffer */
264 	poke = (READ_ONCE(call->tx_bottom) == call->send_top);
265 	sq->bufs[ix] = txb;
266 	/* Order send_top after the queue->next pointer and txb content. */
267 	smp_store_release(&call->send_top, seq);
268 	if (last) {
269 		rxrpc_notify_end_tx(rx, call, notify_end_tx);
270 		call->send_queue = NULL;
271 	}
272 
273 	if (poke)
274 		rxrpc_poke_call(call, rxrpc_call_poke_start);
275 }
276 
277 /*
278  * Allocate a new txqueue unit and add it to the transmission queue.
279  */
280 static int rxrpc_alloc_txqueue(struct sock *sk, struct rxrpc_call *call)
281 {
282 	struct rxrpc_txqueue *tq;
283 
284 	tq = kzalloc(sizeof(*tq), sk->sk_allocation);
285 	if (!tq)
286 		return -ENOMEM;
287 
288 	tq->xmit_ts_base = KTIME_MIN;
289 	for (int i = 0; i < RXRPC_NR_TXQUEUE; i++)
290 		tq->segment_xmit_ts[i] = UINT_MAX;
291 
292 	if (call->send_queue) {
293 		tq->qbase = call->send_top + 1;
294 		call->send_queue->next = tq;
295 		call->send_queue = tq;
296 	} else if (WARN_ON(call->tx_queue)) {
297 		kfree(tq);
298 		return -ENOMEM;
299 	} else {
300 		/* We start at seq 1, so pretend seq 0 is hard-acked. */
301 		tq->nr_reported_acks = 1;
302 		tq->segment_acked = 1UL;
303 		tq->qbase = 0;
304 		call->tx_qbase = 0;
305 		call->send_queue = tq;
306 		call->tx_qtail = tq;
307 		call->tx_queue = tq;
308 	}
309 
310 	trace_rxrpc_tq(call, tq, call->send_top, rxrpc_tq_alloc);
311 	return 0;
312 }
313 
314 /*
315  * send data through a socket
316  * - must be called in process context
317  * - The caller holds the call user access mutex, but not the socket lock.
318  */
319 static int rxrpc_send_data(struct rxrpc_sock *rx,
320 			   struct rxrpc_call *call,
321 			   struct msghdr *msg, size_t len,
322 			   rxrpc_notify_end_tx_t notify_end_tx,
323 			   bool *_dropped_lock)
324 {
325 	struct rxrpc_txbuf *txb;
326 	struct sock *sk = &rx->sk;
327 	enum rxrpc_call_state state;
328 	long timeo;
329 	bool more = msg->msg_flags & MSG_MORE;
330 	int ret, copied = 0;
331 
332 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
333 
334 	ret = rxrpc_wait_to_be_connected(call, &timeo);
335 	if (ret < 0)
336 		return ret;
337 
338 	if (call->conn->state == RXRPC_CONN_CLIENT_UNSECURED) {
339 		ret = rxrpc_init_client_conn_security(call->conn);
340 		if (ret < 0)
341 			return ret;
342 	}
343 
344 	/* this should be in poll */
345 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
346 
347 reload:
348 	txb = call->tx_pending;
349 	call->tx_pending = NULL;
350 	if (txb)
351 		rxrpc_see_txbuf(txb, rxrpc_txbuf_see_send_more);
352 
353 	ret = -EPIPE;
354 	if (sk->sk_shutdown & SEND_SHUTDOWN)
355 		goto maybe_error;
356 	state = rxrpc_call_state(call);
357 	ret = -ESHUTDOWN;
358 	if (state >= RXRPC_CALL_COMPLETE)
359 		goto maybe_error;
360 	ret = -EPROTO;
361 	if (state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
362 	    state != RXRPC_CALL_SERVER_ACK_REQUEST &&
363 	    state != RXRPC_CALL_SERVER_SEND_REPLY) {
364 		/* Request phase complete for this client call */
365 		trace_rxrpc_abort(call->debug_id, rxrpc_sendmsg_late_send,
366 				  call->cid, call->call_id, call->rx_consumed,
367 				  0, -EPROTO);
368 		goto maybe_error;
369 	}
370 
371 	ret = -EMSGSIZE;
372 	if (call->tx_total_len != -1) {
373 		if (len - copied > call->tx_total_len)
374 			goto maybe_error;
375 		if (!more && len - copied != call->tx_total_len)
376 			goto maybe_error;
377 	}
378 
379 	do {
380 		if (!txb) {
381 			size_t remain;
382 
383 			_debug("alloc");
384 
385 			if (!rxrpc_check_tx_space(call, NULL))
386 				goto wait_for_space;
387 
388 			/* See if we need to begin/extend the Tx queue. */
389 			if (!call->send_queue || !((call->send_top + 1) & RXRPC_TXQ_MASK)) {
390 				ret = rxrpc_alloc_txqueue(sk, call);
391 				if (ret < 0)
392 					goto maybe_error;
393 			}
394 
395 			/* Work out the maximum size of a packet.  Assume that
396 			 * the security header is going to be in the padded
397 			 * region (enc blocksize), but the trailer is not.
398 			 */
399 			remain = more ? INT_MAX : msg_data_left(msg);
400 			txb = call->conn->security->alloc_txbuf(call, remain, sk->sk_allocation);
401 			if (!txb) {
402 				ret = -ENOMEM;
403 				goto maybe_error;
404 			}
405 		}
406 
407 		_debug("append");
408 
409 		/* append next segment of data to the current buffer */
410 		if (msg_data_left(msg) > 0) {
411 			size_t copy = umin(txb->space, msg_data_left(msg));
412 
413 			_debug("add %zu", copy);
414 			if (!copy_from_iter_full(txb->kvec[0].iov_base + txb->offset,
415 						 copy, &msg->msg_iter))
416 				goto efault;
417 			_debug("added");
418 			txb->space -= copy;
419 			txb->len += copy;
420 			txb->offset += copy;
421 			copied += copy;
422 			if (call->tx_total_len != -1)
423 				call->tx_total_len -= copy;
424 		}
425 
426 		/* check for the far side aborting the call or a network error
427 		 * occurring */
428 		if (rxrpc_call_is_complete(call))
429 			goto call_terminated;
430 
431 		/* add the packet to the send queue if it's now full */
432 		if (!txb->space ||
433 		    (msg_data_left(msg) == 0 && !more)) {
434 			if (msg_data_left(msg) == 0 && !more)
435 				txb->flags |= RXRPC_LAST_PACKET;
436 
437 			ret = call->security->secure_packet(call, txb);
438 			if (ret < 0)
439 				goto out;
440 
441 			txb->kvec[0].iov_len += txb->len;
442 			rxrpc_queue_packet(rx, call, txb, notify_end_tx);
443 			txb = NULL;
444 		}
445 	} while (msg_data_left(msg) > 0);
446 
447 success:
448 	ret = copied;
449 	if (rxrpc_call_is_complete(call) &&
450 	    call->error < 0)
451 		ret = call->error;
452 out:
453 	call->tx_pending = txb;
454 	_leave(" = %d", ret);
455 	return ret;
456 
457 call_terminated:
458 	rxrpc_put_txbuf(txb, rxrpc_txbuf_put_send_aborted);
459 	_leave(" = %d", call->error);
460 	return call->error;
461 
462 maybe_error:
463 	if (copied)
464 		goto success;
465 	goto out;
466 
467 efault:
468 	ret = -EFAULT;
469 	goto out;
470 
471 wait_for_space:
472 	ret = -EAGAIN;
473 	if (msg->msg_flags & MSG_DONTWAIT)
474 		goto maybe_error;
475 	mutex_unlock(&call->user_mutex);
476 	*_dropped_lock = true;
477 	ret = rxrpc_wait_for_tx_window(rx, call, &timeo,
478 				       msg->msg_flags & MSG_WAITALL);
479 	if (ret < 0)
480 		goto maybe_error;
481 	if (call->interruptibility == RXRPC_INTERRUPTIBLE) {
482 		if (mutex_lock_interruptible(&call->user_mutex) < 0) {
483 			ret = sock_intr_errno(timeo);
484 			goto maybe_error;
485 		}
486 	} else {
487 		mutex_lock(&call->user_mutex);
488 	}
489 	*_dropped_lock = false;
490 	goto reload;
491 }
492 
493 /*
494  * extract control messages from the sendmsg() control buffer
495  */
496 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
497 {
498 	struct cmsghdr *cmsg;
499 	bool got_user_ID = false;
500 	int len;
501 
502 	if (msg->msg_controllen == 0)
503 		return -EINVAL;
504 
505 	for_each_cmsghdr(cmsg, msg) {
506 		if (!CMSG_OK(msg, cmsg))
507 			return -EINVAL;
508 
509 		len = cmsg->cmsg_len - sizeof(struct cmsghdr);
510 		_debug("CMSG %d, %d, %d",
511 		       cmsg->cmsg_level, cmsg->cmsg_type, len);
512 
513 		if (cmsg->cmsg_level != SOL_RXRPC)
514 			continue;
515 
516 		switch (cmsg->cmsg_type) {
517 		case RXRPC_USER_CALL_ID:
518 			if (msg->msg_flags & MSG_CMSG_COMPAT) {
519 				if (len != sizeof(u32))
520 					return -EINVAL;
521 				p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
522 			} else {
523 				if (len != sizeof(unsigned long))
524 					return -EINVAL;
525 				p->call.user_call_ID = *(unsigned long *)
526 					CMSG_DATA(cmsg);
527 			}
528 			got_user_ID = true;
529 			break;
530 
531 		case RXRPC_ABORT:
532 			if (p->command != RXRPC_CMD_SEND_DATA)
533 				return -EINVAL;
534 			p->command = RXRPC_CMD_SEND_ABORT;
535 			if (len != sizeof(p->abort_code))
536 				return -EINVAL;
537 			p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
538 			if (p->abort_code == 0)
539 				return -EINVAL;
540 			break;
541 
542 		case RXRPC_CHARGE_ACCEPT:
543 			if (p->command != RXRPC_CMD_SEND_DATA)
544 				return -EINVAL;
545 			p->command = RXRPC_CMD_CHARGE_ACCEPT;
546 			if (len != 0)
547 				return -EINVAL;
548 			break;
549 
550 		case RXRPC_EXCLUSIVE_CALL:
551 			p->exclusive = true;
552 			if (len != 0)
553 				return -EINVAL;
554 			break;
555 
556 		case RXRPC_UPGRADE_SERVICE:
557 			p->upgrade = true;
558 			if (len != 0)
559 				return -EINVAL;
560 			break;
561 
562 		case RXRPC_TX_LENGTH:
563 			if (p->call.tx_total_len != -1 || len != sizeof(__s64))
564 				return -EINVAL;
565 			p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
566 			if (p->call.tx_total_len < 0)
567 				return -EINVAL;
568 			break;
569 
570 		case RXRPC_SET_CALL_TIMEOUT:
571 			if (len & 3 || len < 4 || len > 12)
572 				return -EINVAL;
573 			memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
574 			p->call.nr_timeouts = len / 4;
575 			if (p->call.timeouts.hard > INT_MAX / HZ)
576 				return -ERANGE;
577 			if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
578 				return -ERANGE;
579 			if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
580 				return -ERANGE;
581 			break;
582 
583 		default:
584 			return -EINVAL;
585 		}
586 	}
587 
588 	if (!got_user_ID)
589 		return -EINVAL;
590 	if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
591 		return -EINVAL;
592 	_leave(" = 0");
593 	return 0;
594 }
595 
596 /*
597  * Create a new client call for sendmsg().
598  * - Called with the socket lock held, which it must release.
599  * - If it returns a call, the call's lock will need releasing by the caller.
600  */
601 static struct rxrpc_call *
602 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
603 				  struct rxrpc_send_params *p)
604 	__releases(&rx->sk.sk_lock.slock)
605 	__acquires(&call->user_mutex)
606 {
607 	struct rxrpc_conn_parameters cp;
608 	struct rxrpc_peer *peer;
609 	struct rxrpc_call *call;
610 	struct key *key;
611 
612 	DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
613 
614 	_enter("");
615 
616 	if (!msg->msg_name) {
617 		release_sock(&rx->sk);
618 		return ERR_PTR(-EDESTADDRREQ);
619 	}
620 
621 	peer = rxrpc_lookup_peer(rx->local, srx, GFP_KERNEL);
622 	if (!peer) {
623 		release_sock(&rx->sk);
624 		return ERR_PTR(-ENOMEM);
625 	}
626 
627 	key = rx->key;
628 	if (key && !rx->key->payload.data[0])
629 		key = NULL;
630 
631 	memset(&cp, 0, sizeof(cp));
632 	cp.local		= rx->local;
633 	cp.peer			= peer;
634 	cp.key			= rx->key;
635 	cp.security_level	= rx->min_sec_level;
636 	cp.exclusive		= rx->exclusive | p->exclusive;
637 	cp.upgrade		= p->upgrade;
638 	cp.service_id		= srx->srx_service;
639 	call = rxrpc_new_client_call(rx, &cp, &p->call, GFP_KERNEL,
640 				     atomic_inc_return(&rxrpc_debug_id));
641 	/* The socket is now unlocked */
642 
643 	rxrpc_put_peer(peer, rxrpc_peer_put_application);
644 	_leave(" = %p\n", call);
645 	return call;
646 }
647 
648 /*
649  * send a message forming part of a client call through an RxRPC socket
650  * - caller holds the socket locked
651  * - the socket may be either a client socket or a server socket
652  */
653 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
654 	__releases(&rx->sk.sk_lock.slock)
655 {
656 	struct rxrpc_call *call;
657 	bool dropped_lock = false;
658 	int ret;
659 
660 	struct rxrpc_send_params p = {
661 		.call.tx_total_len	= -1,
662 		.call.user_call_ID	= 0,
663 		.call.nr_timeouts	= 0,
664 		.call.interruptibility	= RXRPC_INTERRUPTIBLE,
665 		.abort_code		= 0,
666 		.command		= RXRPC_CMD_SEND_DATA,
667 		.exclusive		= false,
668 		.upgrade		= false,
669 	};
670 
671 	_enter("");
672 
673 	ret = rxrpc_sendmsg_cmsg(msg, &p);
674 	if (ret < 0)
675 		goto error_release_sock;
676 
677 	if (p.command == RXRPC_CMD_CHARGE_ACCEPT) {
678 		ret = -EINVAL;
679 		if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
680 			goto error_release_sock;
681 		ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID);
682 		goto error_release_sock;
683 	}
684 
685 	call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
686 	if (!call) {
687 		ret = -EBADSLT;
688 		if (p.command != RXRPC_CMD_SEND_DATA)
689 			goto error_release_sock;
690 		call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
691 		/* The socket is now unlocked... */
692 		if (IS_ERR(call))
693 			return PTR_ERR(call);
694 		/* ... and we have the call lock. */
695 		p.call.nr_timeouts = 0;
696 		ret = 0;
697 		if (rxrpc_call_is_complete(call))
698 			goto out_put_unlock;
699 	} else {
700 		switch (rxrpc_call_state(call)) {
701 		case RXRPC_CALL_CLIENT_AWAIT_CONN:
702 		case RXRPC_CALL_SERVER_SECURING:
703 			if (p.command == RXRPC_CMD_SEND_ABORT)
704 				break;
705 			fallthrough;
706 		case RXRPC_CALL_UNINITIALISED:
707 		case RXRPC_CALL_SERVER_PREALLOC:
708 			rxrpc_put_call(call, rxrpc_call_put_sendmsg);
709 			ret = -EBUSY;
710 			goto error_release_sock;
711 		default:
712 			break;
713 		}
714 
715 		ret = mutex_lock_interruptible(&call->user_mutex);
716 		release_sock(&rx->sk);
717 		if (ret < 0) {
718 			ret = -ERESTARTSYS;
719 			goto error_put;
720 		}
721 
722 		if (p.call.tx_total_len != -1) {
723 			ret = -EINVAL;
724 			if (call->tx_total_len != -1 ||
725 			    call->tx_pending ||
726 			    call->tx_top != 0)
727 				goto out_put_unlock;
728 			call->tx_total_len = p.call.tx_total_len;
729 		}
730 	}
731 
732 	switch (p.call.nr_timeouts) {
733 	case 3:
734 		WRITE_ONCE(call->next_rx_timo, p.call.timeouts.normal);
735 		fallthrough;
736 	case 2:
737 		WRITE_ONCE(call->next_req_timo, p.call.timeouts.idle);
738 		fallthrough;
739 	case 1:
740 		if (p.call.timeouts.hard > 0) {
741 			ktime_t delay = ms_to_ktime(p.call.timeouts.hard * MSEC_PER_SEC);
742 
743 			WRITE_ONCE(call->expect_term_by,
744 				   ktime_add(p.call.timeouts.hard,
745 					     ktime_get_real()));
746 			trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_hard);
747 			rxrpc_poke_call(call, rxrpc_call_poke_set_timeout);
748 
749 		}
750 		break;
751 	}
752 
753 	if (rxrpc_call_is_complete(call)) {
754 		/* it's too late for this call */
755 		ret = -ESHUTDOWN;
756 	} else if (p.command == RXRPC_CMD_SEND_ABORT) {
757 		rxrpc_propose_abort(call, p.abort_code, -ECONNABORTED,
758 				    rxrpc_abort_call_sendmsg);
759 		ret = 0;
760 	} else if (p.command != RXRPC_CMD_SEND_DATA) {
761 		ret = -EINVAL;
762 	} else {
763 		ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock);
764 	}
765 
766 out_put_unlock:
767 	if (!dropped_lock)
768 		mutex_unlock(&call->user_mutex);
769 error_put:
770 	rxrpc_put_call(call, rxrpc_call_put_sendmsg);
771 	_leave(" = %d", ret);
772 	return ret;
773 
774 error_release_sock:
775 	release_sock(&rx->sk);
776 	return ret;
777 }
778 
779 /**
780  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
781  * @sock: The socket the call is on
782  * @call: The call to send data through
783  * @msg: The data to send
784  * @len: The amount of data to send
785  * @notify_end_tx: Notification that the last packet is queued.
786  *
787  * Allow a kernel service to send data on a call.  The call must be in an state
788  * appropriate to sending data.  No control data should be supplied in @msg,
789  * nor should an address be supplied.  MSG_MORE should be flagged if there's
790  * more data to come, otherwise this data will end the transmission phase.
791  */
792 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
793 			   struct msghdr *msg, size_t len,
794 			   rxrpc_notify_end_tx_t notify_end_tx)
795 {
796 	bool dropped_lock = false;
797 	int ret;
798 
799 	_enter("{%d},", call->debug_id);
800 
801 	ASSERTCMP(msg->msg_name, ==, NULL);
802 	ASSERTCMP(msg->msg_control, ==, NULL);
803 
804 	mutex_lock(&call->user_mutex);
805 
806 	ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
807 			      notify_end_tx, &dropped_lock);
808 	if (ret == -ESHUTDOWN)
809 		ret = call->error;
810 
811 	if (!dropped_lock)
812 		mutex_unlock(&call->user_mutex);
813 	_leave(" = %d", ret);
814 	return ret;
815 }
816 EXPORT_SYMBOL(rxrpc_kernel_send_data);
817 
818 /**
819  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
820  * @sock: The socket the call is on
821  * @call: The call to be aborted
822  * @abort_code: The abort code to stick into the ABORT packet
823  * @error: Local error value
824  * @why: Indication as to why.
825  *
826  * Allow a kernel service to abort a call, if it's still in an abortable state
827  * and return true if the call was aborted, false if it was already complete.
828  */
829 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
830 			     u32 abort_code, int error, enum rxrpc_abort_reason why)
831 {
832 	bool aborted;
833 
834 	_enter("{%d},%d,%d,%u", call->debug_id, abort_code, error, why);
835 
836 	mutex_lock(&call->user_mutex);
837 	aborted = rxrpc_propose_abort(call, abort_code, error, why);
838 	mutex_unlock(&call->user_mutex);
839 	return aborted;
840 }
841 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
842 
843 /**
844  * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
845  * @sock: The socket the call is on
846  * @call: The call to be informed
847  * @tx_total_len: The amount of data to be transmitted for this call
848  *
849  * Allow a kernel service to set the total transmit length on a call.  This
850  * allows buffer-to-packet encrypt-and-copy to be performed.
851  *
852  * This function is primarily for use for setting the reply length since the
853  * request length can be set when beginning the call.
854  */
855 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
856 				s64 tx_total_len)
857 {
858 	WARN_ON(call->tx_total_len != -1);
859 	call->tx_total_len = tx_total_len;
860 }
861 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);
862