xref: /linux-6.15/net/rxrpc/sendmsg.c (revision b341a026)
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 ([email protected])
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->peer->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->acks_hard_ack);
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,%u}",
201 	       call->tx_bottom, call->acks_hard_ack, 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 	spin_lock(&call->tx_lock);
265 	poke = (READ_ONCE(call->tx_bottom) == call->send_top);
266 	sq->bufs[ix] = txb;
267 	/* Order send_top after the queue->next pointer and txb content. */
268 	smp_store_release(&call->send_top, seq);
269 	if (last) {
270 		rxrpc_notify_end_tx(rx, call, notify_end_tx);
271 		call->send_queue = NULL;
272 	}
273 	spin_unlock(&call->tx_lock);
274 
275 	if (poke)
276 		rxrpc_poke_call(call, rxrpc_call_poke_start);
277 }
278 
279 /*
280  * Allocate a new txqueue unit and add it to the transmission queue.
281  */
282 static int rxrpc_alloc_txqueue(struct sock *sk, struct rxrpc_call *call)
283 {
284 	struct rxrpc_txqueue *tq;
285 
286 	tq = kzalloc(sizeof(*tq), sk->sk_allocation);
287 	if (!tq)
288 		return -ENOMEM;
289 
290 	tq->xmit_ts_base = KTIME_MIN;
291 	for (int i = 0; i < RXRPC_NR_TXQUEUE; i++)
292 		tq->segment_xmit_ts[i] = UINT_MAX;
293 
294 	if (call->send_queue) {
295 		tq->qbase = call->send_top + 1;
296 		call->send_queue->next = tq;
297 		call->send_queue = tq;
298 	} else if (WARN_ON(call->tx_queue)) {
299 		kfree(tq);
300 		return -ENOMEM;
301 	} else {
302 		tq->qbase = 0;
303 		call->tx_qbase = 0;
304 		call->send_queue = tq;
305 		call->tx_qtail = tq;
306 		call->tx_queue = tq;
307 	}
308 
309 	trace_rxrpc_tq(call, tq, call->send_top, rxrpc_tq_alloc);
310 	return 0;
311 }
312 
313 /*
314  * send data through a socket
315  * - must be called in process context
316  * - The caller holds the call user access mutex, but not the socket lock.
317  */
318 static int rxrpc_send_data(struct rxrpc_sock *rx,
319 			   struct rxrpc_call *call,
320 			   struct msghdr *msg, size_t len,
321 			   rxrpc_notify_end_tx_t notify_end_tx,
322 			   bool *_dropped_lock)
323 {
324 	struct rxrpc_txbuf *txb;
325 	struct sock *sk = &rx->sk;
326 	enum rxrpc_call_state state;
327 	long timeo;
328 	bool more = msg->msg_flags & MSG_MORE;
329 	int ret, copied = 0;
330 
331 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
332 
333 	ret = rxrpc_wait_to_be_connected(call, &timeo);
334 	if (ret < 0)
335 		return ret;
336 
337 	if (call->conn->state == RXRPC_CONN_CLIENT_UNSECURED) {
338 		ret = rxrpc_init_client_conn_security(call->conn);
339 		if (ret < 0)
340 			return ret;
341 	}
342 
343 	/* this should be in poll */
344 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
345 
346 reload:
347 	txb = call->tx_pending;
348 	call->tx_pending = NULL;
349 	if (txb)
350 		rxrpc_see_txbuf(txb, rxrpc_txbuf_see_send_more);
351 
352 	ret = -EPIPE;
353 	if (sk->sk_shutdown & SEND_SHUTDOWN)
354 		goto maybe_error;
355 	state = rxrpc_call_state(call);
356 	ret = -ESHUTDOWN;
357 	if (state >= RXRPC_CALL_COMPLETE)
358 		goto maybe_error;
359 	ret = -EPROTO;
360 	if (state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
361 	    state != RXRPC_CALL_SERVER_ACK_REQUEST &&
362 	    state != RXRPC_CALL_SERVER_SEND_REPLY) {
363 		/* Request phase complete for this client call */
364 		trace_rxrpc_abort(call->debug_id, rxrpc_sendmsg_late_send,
365 				  call->cid, call->call_id, call->rx_consumed,
366 				  0, -EPROTO);
367 		goto maybe_error;
368 	}
369 
370 	ret = -EMSGSIZE;
371 	if (call->tx_total_len != -1) {
372 		if (len - copied > call->tx_total_len)
373 			goto maybe_error;
374 		if (!more && len - copied != call->tx_total_len)
375 			goto maybe_error;
376 	}
377 
378 	do {
379 		if (!txb) {
380 			size_t remain;
381 
382 			_debug("alloc");
383 
384 			if (!rxrpc_check_tx_space(call, NULL))
385 				goto wait_for_space;
386 
387 			/* See if we need to begin/extend the Tx queue. */
388 			if (!call->send_queue || !((call->send_top + 1) & RXRPC_TXQ_MASK)) {
389 				ret = rxrpc_alloc_txqueue(sk, call);
390 				if (ret < 0)
391 					goto maybe_error;
392 			}
393 
394 			/* Work out the maximum size of a packet.  Assume that
395 			 * the security header is going to be in the padded
396 			 * region (enc blocksize), but the trailer is not.
397 			 */
398 			remain = more ? INT_MAX : msg_data_left(msg);
399 			txb = call->conn->security->alloc_txbuf(call, remain, sk->sk_allocation);
400 			if (!txb) {
401 				ret = -ENOMEM;
402 				goto maybe_error;
403 			}
404 		}
405 
406 		_debug("append");
407 
408 		/* append next segment of data to the current buffer */
409 		if (msg_data_left(msg) > 0) {
410 			size_t copy = umin(txb->space, msg_data_left(msg));
411 
412 			_debug("add %zu", copy);
413 			if (!copy_from_iter_full(txb->kvec[0].iov_base + txb->offset,
414 						 copy, &msg->msg_iter))
415 				goto efault;
416 			_debug("added");
417 			txb->space -= copy;
418 			txb->len += copy;
419 			txb->offset += copy;
420 			copied += copy;
421 			if (call->tx_total_len != -1)
422 				call->tx_total_len -= copy;
423 		}
424 
425 		/* check for the far side aborting the call or a network error
426 		 * occurring */
427 		if (rxrpc_call_is_complete(call))
428 			goto call_terminated;
429 
430 		/* add the packet to the send queue if it's now full */
431 		if (!txb->space ||
432 		    (msg_data_left(msg) == 0 && !more)) {
433 			if (msg_data_left(msg) == 0 && !more)
434 				txb->flags |= RXRPC_LAST_PACKET;
435 
436 			ret = call->security->secure_packet(call, txb);
437 			if (ret < 0)
438 				goto out;
439 
440 			txb->kvec[0].iov_len += txb->len;
441 			rxrpc_queue_packet(rx, call, txb, notify_end_tx);
442 			txb = NULL;
443 		}
444 	} while (msg_data_left(msg) > 0);
445 
446 success:
447 	ret = copied;
448 	if (rxrpc_call_is_complete(call) &&
449 	    call->error < 0)
450 		ret = call->error;
451 out:
452 	call->tx_pending = txb;
453 	_leave(" = %d", ret);
454 	return ret;
455 
456 call_terminated:
457 	rxrpc_put_txbuf(txb, rxrpc_txbuf_put_send_aborted);
458 	_leave(" = %d", call->error);
459 	return call->error;
460 
461 maybe_error:
462 	if (copied)
463 		goto success;
464 	goto out;
465 
466 efault:
467 	ret = -EFAULT;
468 	goto out;
469 
470 wait_for_space:
471 	ret = -EAGAIN;
472 	if (msg->msg_flags & MSG_DONTWAIT)
473 		goto maybe_error;
474 	mutex_unlock(&call->user_mutex);
475 	*_dropped_lock = true;
476 	ret = rxrpc_wait_for_tx_window(rx, call, &timeo,
477 				       msg->msg_flags & MSG_WAITALL);
478 	if (ret < 0)
479 		goto maybe_error;
480 	if (call->interruptibility == RXRPC_INTERRUPTIBLE) {
481 		if (mutex_lock_interruptible(&call->user_mutex) < 0) {
482 			ret = sock_intr_errno(timeo);
483 			goto maybe_error;
484 		}
485 	} else {
486 		mutex_lock(&call->user_mutex);
487 	}
488 	*_dropped_lock = false;
489 	goto reload;
490 }
491 
492 /*
493  * extract control messages from the sendmsg() control buffer
494  */
495 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
496 {
497 	struct cmsghdr *cmsg;
498 	bool got_user_ID = false;
499 	int len;
500 
501 	if (msg->msg_controllen == 0)
502 		return -EINVAL;
503 
504 	for_each_cmsghdr(cmsg, msg) {
505 		if (!CMSG_OK(msg, cmsg))
506 			return -EINVAL;
507 
508 		len = cmsg->cmsg_len - sizeof(struct cmsghdr);
509 		_debug("CMSG %d, %d, %d",
510 		       cmsg->cmsg_level, cmsg->cmsg_type, len);
511 
512 		if (cmsg->cmsg_level != SOL_RXRPC)
513 			continue;
514 
515 		switch (cmsg->cmsg_type) {
516 		case RXRPC_USER_CALL_ID:
517 			if (msg->msg_flags & MSG_CMSG_COMPAT) {
518 				if (len != sizeof(u32))
519 					return -EINVAL;
520 				p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
521 			} else {
522 				if (len != sizeof(unsigned long))
523 					return -EINVAL;
524 				p->call.user_call_ID = *(unsigned long *)
525 					CMSG_DATA(cmsg);
526 			}
527 			got_user_ID = true;
528 			break;
529 
530 		case RXRPC_ABORT:
531 			if (p->command != RXRPC_CMD_SEND_DATA)
532 				return -EINVAL;
533 			p->command = RXRPC_CMD_SEND_ABORT;
534 			if (len != sizeof(p->abort_code))
535 				return -EINVAL;
536 			p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
537 			if (p->abort_code == 0)
538 				return -EINVAL;
539 			break;
540 
541 		case RXRPC_CHARGE_ACCEPT:
542 			if (p->command != RXRPC_CMD_SEND_DATA)
543 				return -EINVAL;
544 			p->command = RXRPC_CMD_CHARGE_ACCEPT;
545 			if (len != 0)
546 				return -EINVAL;
547 			break;
548 
549 		case RXRPC_EXCLUSIVE_CALL:
550 			p->exclusive = true;
551 			if (len != 0)
552 				return -EINVAL;
553 			break;
554 
555 		case RXRPC_UPGRADE_SERVICE:
556 			p->upgrade = true;
557 			if (len != 0)
558 				return -EINVAL;
559 			break;
560 
561 		case RXRPC_TX_LENGTH:
562 			if (p->call.tx_total_len != -1 || len != sizeof(__s64))
563 				return -EINVAL;
564 			p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
565 			if (p->call.tx_total_len < 0)
566 				return -EINVAL;
567 			break;
568 
569 		case RXRPC_SET_CALL_TIMEOUT:
570 			if (len & 3 || len < 4 || len > 12)
571 				return -EINVAL;
572 			memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
573 			p->call.nr_timeouts = len / 4;
574 			if (p->call.timeouts.hard > INT_MAX / HZ)
575 				return -ERANGE;
576 			if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
577 				return -ERANGE;
578 			if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
579 				return -ERANGE;
580 			break;
581 
582 		default:
583 			return -EINVAL;
584 		}
585 	}
586 
587 	if (!got_user_ID)
588 		return -EINVAL;
589 	if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
590 		return -EINVAL;
591 	_leave(" = 0");
592 	return 0;
593 }
594 
595 /*
596  * Create a new client call for sendmsg().
597  * - Called with the socket lock held, which it must release.
598  * - If it returns a call, the call's lock will need releasing by the caller.
599  */
600 static struct rxrpc_call *
601 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
602 				  struct rxrpc_send_params *p)
603 	__releases(&rx->sk.sk_lock.slock)
604 	__acquires(&call->user_mutex)
605 {
606 	struct rxrpc_conn_parameters cp;
607 	struct rxrpc_peer *peer;
608 	struct rxrpc_call *call;
609 	struct key *key;
610 
611 	DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
612 
613 	_enter("");
614 
615 	if (!msg->msg_name) {
616 		release_sock(&rx->sk);
617 		return ERR_PTR(-EDESTADDRREQ);
618 	}
619 
620 	peer = rxrpc_lookup_peer(rx->local, srx, GFP_KERNEL);
621 	if (!peer) {
622 		release_sock(&rx->sk);
623 		return ERR_PTR(-ENOMEM);
624 	}
625 
626 	key = rx->key;
627 	if (key && !rx->key->payload.data[0])
628 		key = NULL;
629 
630 	memset(&cp, 0, sizeof(cp));
631 	cp.local		= rx->local;
632 	cp.peer			= peer;
633 	cp.key			= rx->key;
634 	cp.security_level	= rx->min_sec_level;
635 	cp.exclusive		= rx->exclusive | p->exclusive;
636 	cp.upgrade		= p->upgrade;
637 	cp.service_id		= srx->srx_service;
638 	call = rxrpc_new_client_call(rx, &cp, &p->call, GFP_KERNEL,
639 				     atomic_inc_return(&rxrpc_debug_id));
640 	/* The socket is now unlocked */
641 
642 	rxrpc_put_peer(peer, rxrpc_peer_put_application);
643 	_leave(" = %p\n", call);
644 	return call;
645 }
646 
647 /*
648  * send a message forming part of a client call through an RxRPC socket
649  * - caller holds the socket locked
650  * - the socket may be either a client socket or a server socket
651  */
652 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
653 	__releases(&rx->sk.sk_lock.slock)
654 {
655 	struct rxrpc_call *call;
656 	bool dropped_lock = false;
657 	int ret;
658 
659 	struct rxrpc_send_params p = {
660 		.call.tx_total_len	= -1,
661 		.call.user_call_ID	= 0,
662 		.call.nr_timeouts	= 0,
663 		.call.interruptibility	= RXRPC_INTERRUPTIBLE,
664 		.abort_code		= 0,
665 		.command		= RXRPC_CMD_SEND_DATA,
666 		.exclusive		= false,
667 		.upgrade		= false,
668 	};
669 
670 	_enter("");
671 
672 	ret = rxrpc_sendmsg_cmsg(msg, &p);
673 	if (ret < 0)
674 		goto error_release_sock;
675 
676 	if (p.command == RXRPC_CMD_CHARGE_ACCEPT) {
677 		ret = -EINVAL;
678 		if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
679 			goto error_release_sock;
680 		ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID);
681 		goto error_release_sock;
682 	}
683 
684 	call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
685 	if (!call) {
686 		ret = -EBADSLT;
687 		if (p.command != RXRPC_CMD_SEND_DATA)
688 			goto error_release_sock;
689 		call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
690 		/* The socket is now unlocked... */
691 		if (IS_ERR(call))
692 			return PTR_ERR(call);
693 		/* ... and we have the call lock. */
694 		p.call.nr_timeouts = 0;
695 		ret = 0;
696 		if (rxrpc_call_is_complete(call))
697 			goto out_put_unlock;
698 	} else {
699 		switch (rxrpc_call_state(call)) {
700 		case RXRPC_CALL_CLIENT_AWAIT_CONN:
701 		case RXRPC_CALL_SERVER_SECURING:
702 			if (p.command == RXRPC_CMD_SEND_ABORT)
703 				break;
704 			fallthrough;
705 		case RXRPC_CALL_UNINITIALISED:
706 		case RXRPC_CALL_SERVER_PREALLOC:
707 			rxrpc_put_call(call, rxrpc_call_put_sendmsg);
708 			ret = -EBUSY;
709 			goto error_release_sock;
710 		default:
711 			break;
712 		}
713 
714 		ret = mutex_lock_interruptible(&call->user_mutex);
715 		release_sock(&rx->sk);
716 		if (ret < 0) {
717 			ret = -ERESTARTSYS;
718 			goto error_put;
719 		}
720 
721 		if (p.call.tx_total_len != -1) {
722 			ret = -EINVAL;
723 			if (call->tx_total_len != -1 ||
724 			    call->tx_pending ||
725 			    call->tx_top != 0)
726 				goto out_put_unlock;
727 			call->tx_total_len = p.call.tx_total_len;
728 		}
729 	}
730 
731 	switch (p.call.nr_timeouts) {
732 	case 3:
733 		WRITE_ONCE(call->next_rx_timo, p.call.timeouts.normal);
734 		fallthrough;
735 	case 2:
736 		WRITE_ONCE(call->next_req_timo, p.call.timeouts.idle);
737 		fallthrough;
738 	case 1:
739 		if (p.call.timeouts.hard > 0) {
740 			ktime_t delay = ms_to_ktime(p.call.timeouts.hard * MSEC_PER_SEC);
741 
742 			WRITE_ONCE(call->expect_term_by,
743 				   ktime_add(p.call.timeouts.hard,
744 					     ktime_get_real()));
745 			trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_hard);
746 			rxrpc_poke_call(call, rxrpc_call_poke_set_timeout);
747 
748 		}
749 		break;
750 	}
751 
752 	if (rxrpc_call_is_complete(call)) {
753 		/* it's too late for this call */
754 		ret = -ESHUTDOWN;
755 	} else if (p.command == RXRPC_CMD_SEND_ABORT) {
756 		rxrpc_propose_abort(call, p.abort_code, -ECONNABORTED,
757 				    rxrpc_abort_call_sendmsg);
758 		ret = 0;
759 	} else if (p.command != RXRPC_CMD_SEND_DATA) {
760 		ret = -EINVAL;
761 	} else {
762 		ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock);
763 	}
764 
765 out_put_unlock:
766 	if (!dropped_lock)
767 		mutex_unlock(&call->user_mutex);
768 error_put:
769 	rxrpc_put_call(call, rxrpc_call_put_sendmsg);
770 	_leave(" = %d", ret);
771 	return ret;
772 
773 error_release_sock:
774 	release_sock(&rx->sk);
775 	return ret;
776 }
777 
778 /**
779  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
780  * @sock: The socket the call is on
781  * @call: The call to send data through
782  * @msg: The data to send
783  * @len: The amount of data to send
784  * @notify_end_tx: Notification that the last packet is queued.
785  *
786  * Allow a kernel service to send data on a call.  The call must be in an state
787  * appropriate to sending data.  No control data should be supplied in @msg,
788  * nor should an address be supplied.  MSG_MORE should be flagged if there's
789  * more data to come, otherwise this data will end the transmission phase.
790  */
791 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
792 			   struct msghdr *msg, size_t len,
793 			   rxrpc_notify_end_tx_t notify_end_tx)
794 {
795 	bool dropped_lock = false;
796 	int ret;
797 
798 	_enter("{%d},", call->debug_id);
799 
800 	ASSERTCMP(msg->msg_name, ==, NULL);
801 	ASSERTCMP(msg->msg_control, ==, NULL);
802 
803 	mutex_lock(&call->user_mutex);
804 
805 	ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
806 			      notify_end_tx, &dropped_lock);
807 	if (ret == -ESHUTDOWN)
808 		ret = call->error;
809 
810 	if (!dropped_lock)
811 		mutex_unlock(&call->user_mutex);
812 	_leave(" = %d", ret);
813 	return ret;
814 }
815 EXPORT_SYMBOL(rxrpc_kernel_send_data);
816 
817 /**
818  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
819  * @sock: The socket the call is on
820  * @call: The call to be aborted
821  * @abort_code: The abort code to stick into the ABORT packet
822  * @error: Local error value
823  * @why: Indication as to why.
824  *
825  * Allow a kernel service to abort a call, if it's still in an abortable state
826  * and return true if the call was aborted, false if it was already complete.
827  */
828 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
829 			     u32 abort_code, int error, enum rxrpc_abort_reason why)
830 {
831 	bool aborted;
832 
833 	_enter("{%d},%d,%d,%u", call->debug_id, abort_code, error, why);
834 
835 	mutex_lock(&call->user_mutex);
836 	aborted = rxrpc_propose_abort(call, abort_code, error, why);
837 	mutex_unlock(&call->user_mutex);
838 	return aborted;
839 }
840 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
841 
842 /**
843  * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
844  * @sock: The socket the call is on
845  * @call: The call to be informed
846  * @tx_total_len: The amount of data to be transmitted for this call
847  *
848  * Allow a kernel service to set the total transmit length on a call.  This
849  * allows buffer-to-packet encrypt-and-copy to be performed.
850  *
851  * This function is primarily for use for setting the reply length since the
852  * request length can be set when beginning the call.
853  */
854 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
855 				s64 tx_total_len)
856 {
857 	WARN_ON(call->tx_total_len != -1);
858 	call->tx_total_len = tx_total_len;
859 }
860 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);
861