xref: /openbmc/linux/net/rxrpc/sendmsg.c (revision 165f2d28)
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  * Return true if there's sufficient Tx queue space.
22  */
23 static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win)
24 {
25 	unsigned int win_size =
26 		min_t(unsigned int, call->tx_winsize,
27 		      call->cong_cwnd + call->cong_extra);
28 	rxrpc_seq_t tx_win = READ_ONCE(call->tx_hard_ack);
29 
30 	if (_tx_win)
31 		*_tx_win = tx_win;
32 	return call->tx_top - tx_win < win_size;
33 }
34 
35 /*
36  * Wait for space to appear in the Tx queue or a signal to occur.
37  */
38 static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
39 					 struct rxrpc_call *call,
40 					 long *timeo)
41 {
42 	for (;;) {
43 		set_current_state(TASK_INTERRUPTIBLE);
44 		if (rxrpc_check_tx_space(call, NULL))
45 			return 0;
46 
47 		if (call->state >= RXRPC_CALL_COMPLETE)
48 			return call->error;
49 
50 		if (signal_pending(current))
51 			return sock_intr_errno(*timeo);
52 
53 		trace_rxrpc_transmit(call, rxrpc_transmit_wait);
54 		mutex_unlock(&call->user_mutex);
55 		*timeo = schedule_timeout(*timeo);
56 		if (mutex_lock_interruptible(&call->user_mutex) < 0)
57 			return sock_intr_errno(*timeo);
58 	}
59 }
60 
61 /*
62  * Wait for space to appear in the Tx queue uninterruptibly, but with
63  * a timeout of 2*RTT if no progress was made and a signal occurred.
64  */
65 static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
66 					    struct rxrpc_call *call)
67 {
68 	rxrpc_seq_t tx_start, tx_win;
69 	signed long rtt2, timeout;
70 	u64 rtt;
71 
72 	rtt = READ_ONCE(call->peer->rtt);
73 	rtt2 = nsecs_to_jiffies64(rtt) * 2;
74 	if (rtt2 < 2)
75 		rtt2 = 2;
76 
77 	timeout = rtt2;
78 	tx_start = READ_ONCE(call->tx_hard_ack);
79 
80 	for (;;) {
81 		set_current_state(TASK_UNINTERRUPTIBLE);
82 
83 		tx_win = READ_ONCE(call->tx_hard_ack);
84 		if (rxrpc_check_tx_space(call, &tx_win))
85 			return 0;
86 
87 		if (call->state >= RXRPC_CALL_COMPLETE)
88 			return call->error;
89 
90 		if (timeout == 0 &&
91 		    tx_win == tx_start && signal_pending(current))
92 			return -EINTR;
93 
94 		if (tx_win != tx_start) {
95 			timeout = rtt2;
96 			tx_start = tx_win;
97 		}
98 
99 		trace_rxrpc_transmit(call, rxrpc_transmit_wait);
100 		timeout = schedule_timeout(timeout);
101 	}
102 }
103 
104 /*
105  * Wait for space to appear in the Tx queue uninterruptibly.
106  */
107 static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
108 					    struct rxrpc_call *call,
109 					    long *timeo)
110 {
111 	for (;;) {
112 		set_current_state(TASK_UNINTERRUPTIBLE);
113 		if (rxrpc_check_tx_space(call, NULL))
114 			return 0;
115 
116 		if (call->state >= RXRPC_CALL_COMPLETE)
117 			return call->error;
118 
119 		trace_rxrpc_transmit(call, rxrpc_transmit_wait);
120 		*timeo = schedule_timeout(*timeo);
121 	}
122 }
123 
124 /*
125  * wait for space to appear in the transmit/ACK window
126  * - caller holds the socket locked
127  */
128 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
129 				    struct rxrpc_call *call,
130 				    long *timeo,
131 				    bool waitall)
132 {
133 	DECLARE_WAITQUEUE(myself, current);
134 	int ret;
135 
136 	_enter(",{%u,%u,%u}",
137 	       call->tx_hard_ack, call->tx_top, call->tx_winsize);
138 
139 	add_wait_queue(&call->waitq, &myself);
140 
141 	switch (call->interruptibility) {
142 	case RXRPC_INTERRUPTIBLE:
143 		if (waitall)
144 			ret = rxrpc_wait_for_tx_window_waitall(rx, call);
145 		else
146 			ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
147 		break;
148 	case RXRPC_PREINTERRUPTIBLE:
149 	case RXRPC_UNINTERRUPTIBLE:
150 	default:
151 		ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
152 		break;
153 	}
154 
155 	remove_wait_queue(&call->waitq, &myself);
156 	set_current_state(TASK_RUNNING);
157 	_leave(" = %d", ret);
158 	return ret;
159 }
160 
161 /*
162  * Schedule an instant Tx resend.
163  */
164 static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
165 {
166 	spin_lock_bh(&call->lock);
167 
168 	if (call->state < RXRPC_CALL_COMPLETE) {
169 		call->rxtx_annotations[ix] =
170 			(call->rxtx_annotations[ix] & RXRPC_TX_ANNO_LAST) |
171 			RXRPC_TX_ANNO_RETRANS;
172 		if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
173 			rxrpc_queue_call(call);
174 	}
175 
176 	spin_unlock_bh(&call->lock);
177 }
178 
179 /*
180  * Notify the owner of the call that the transmit phase is ended and the last
181  * packet has been queued.
182  */
183 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
184 				rxrpc_notify_end_tx_t notify_end_tx)
185 {
186 	if (notify_end_tx)
187 		notify_end_tx(&rx->sk, call, call->user_call_ID);
188 }
189 
190 /*
191  * Queue a DATA packet for transmission, set the resend timeout and send
192  * the packet immediately.  Returns the error from rxrpc_send_data_packet()
193  * in case the caller wants to do something with it.
194  */
195 static int rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
196 			      struct sk_buff *skb, bool last,
197 			      rxrpc_notify_end_tx_t notify_end_tx)
198 {
199 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
200 	unsigned long now;
201 	rxrpc_seq_t seq = sp->hdr.seq;
202 	int ret, ix;
203 	u8 annotation = RXRPC_TX_ANNO_UNACK;
204 
205 	_net("queue skb %p [%d]", skb, seq);
206 
207 	ASSERTCMP(seq, ==, call->tx_top + 1);
208 
209 	if (last)
210 		annotation |= RXRPC_TX_ANNO_LAST;
211 
212 	/* We have to set the timestamp before queueing as the retransmit
213 	 * algorithm can see the packet as soon as we queue it.
214 	 */
215 	skb->tstamp = ktime_get_real();
216 
217 	ix = seq & RXRPC_RXTX_BUFF_MASK;
218 	rxrpc_get_skb(skb, rxrpc_skb_got);
219 	call->rxtx_annotations[ix] = annotation;
220 	smp_wmb();
221 	call->rxtx_buffer[ix] = skb;
222 	call->tx_top = seq;
223 	if (last)
224 		trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
225 	else
226 		trace_rxrpc_transmit(call, rxrpc_transmit_queue);
227 
228 	if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
229 		_debug("________awaiting reply/ACK__________");
230 		write_lock_bh(&call->state_lock);
231 		switch (call->state) {
232 		case RXRPC_CALL_CLIENT_SEND_REQUEST:
233 			call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
234 			rxrpc_notify_end_tx(rx, call, notify_end_tx);
235 			break;
236 		case RXRPC_CALL_SERVER_ACK_REQUEST:
237 			call->state = RXRPC_CALL_SERVER_SEND_REPLY;
238 			now = jiffies;
239 			WRITE_ONCE(call->ack_at, now + MAX_JIFFY_OFFSET);
240 			if (call->ackr_reason == RXRPC_ACK_DELAY)
241 				call->ackr_reason = 0;
242 			trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
243 			if (!last)
244 				break;
245 			/* Fall through */
246 		case RXRPC_CALL_SERVER_SEND_REPLY:
247 			call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
248 			rxrpc_notify_end_tx(rx, call, notify_end_tx);
249 			break;
250 		default:
251 			break;
252 		}
253 		write_unlock_bh(&call->state_lock);
254 	}
255 
256 	if (seq == 1 && rxrpc_is_client_call(call))
257 		rxrpc_expose_client_call(call);
258 
259 	ret = rxrpc_send_data_packet(call, skb, false);
260 	if (ret < 0) {
261 		switch (ret) {
262 		case -ENETUNREACH:
263 		case -EHOSTUNREACH:
264 		case -ECONNREFUSED:
265 			rxrpc_set_call_completion(call,
266 						  RXRPC_CALL_LOCAL_ERROR,
267 						  0, ret);
268 			rxrpc_notify_socket(call);
269 			goto out;
270 		}
271 		_debug("need instant resend %d", ret);
272 		rxrpc_instant_resend(call, ix);
273 	} else {
274 		unsigned long now = jiffies, resend_at;
275 
276 		if (call->peer->rtt_usage > 1)
277 			resend_at = nsecs_to_jiffies(call->peer->rtt * 3 / 2);
278 		else
279 			resend_at = rxrpc_resend_timeout;
280 		if (resend_at < 1)
281 			resend_at = 1;
282 
283 		resend_at += now;
284 		WRITE_ONCE(call->resend_at, resend_at);
285 		rxrpc_reduce_call_timer(call, resend_at, now,
286 					rxrpc_timer_set_for_send);
287 	}
288 
289 out:
290 	rxrpc_free_skb(skb, rxrpc_skb_freed);
291 	_leave(" = %d", ret);
292 	return ret;
293 }
294 
295 /*
296  * send data through a socket
297  * - must be called in process context
298  * - The caller holds the call user access mutex, but not the socket lock.
299  */
300 static int rxrpc_send_data(struct rxrpc_sock *rx,
301 			   struct rxrpc_call *call,
302 			   struct msghdr *msg, size_t len,
303 			   rxrpc_notify_end_tx_t notify_end_tx)
304 {
305 	struct rxrpc_skb_priv *sp;
306 	struct sk_buff *skb;
307 	struct sock *sk = &rx->sk;
308 	long timeo;
309 	bool more;
310 	int ret, copied;
311 
312 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
313 
314 	/* this should be in poll */
315 	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
316 
317 	if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
318 		return -EPIPE;
319 
320 	more = msg->msg_flags & MSG_MORE;
321 
322 	if (call->tx_total_len != -1) {
323 		if (len > call->tx_total_len)
324 			return -EMSGSIZE;
325 		if (!more && len != call->tx_total_len)
326 			return -EMSGSIZE;
327 	}
328 
329 	skb = call->tx_pending;
330 	call->tx_pending = NULL;
331 	rxrpc_see_skb(skb, rxrpc_skb_seen);
332 
333 	copied = 0;
334 	do {
335 		/* Check to see if there's a ping ACK to reply to. */
336 		if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
337 			rxrpc_send_ack_packet(call, false, NULL);
338 
339 		if (!skb) {
340 			size_t size, chunk, max, space;
341 
342 			_debug("alloc");
343 
344 			if (!rxrpc_check_tx_space(call, NULL)) {
345 				ret = -EAGAIN;
346 				if (msg->msg_flags & MSG_DONTWAIT)
347 					goto maybe_error;
348 				ret = rxrpc_wait_for_tx_window(rx, call,
349 							       &timeo,
350 							       msg->msg_flags & MSG_WAITALL);
351 				if (ret < 0)
352 					goto maybe_error;
353 			}
354 
355 			max = RXRPC_JUMBO_DATALEN;
356 			max -= call->conn->security_size;
357 			max &= ~(call->conn->size_align - 1UL);
358 
359 			chunk = max;
360 			if (chunk > msg_data_left(msg) && !more)
361 				chunk = msg_data_left(msg);
362 
363 			space = chunk + call->conn->size_align;
364 			space &= ~(call->conn->size_align - 1UL);
365 
366 			size = space + call->conn->security_size;
367 
368 			_debug("SIZE: %zu/%zu/%zu", chunk, space, size);
369 
370 			/* create a buffer that we can retain until it's ACK'd */
371 			skb = sock_alloc_send_skb(
372 				sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
373 			if (!skb)
374 				goto maybe_error;
375 
376 			sp = rxrpc_skb(skb);
377 			sp->rx_flags |= RXRPC_SKB_TX_BUFFER;
378 			rxrpc_new_skb(skb, rxrpc_skb_new);
379 
380 			_debug("ALLOC SEND %p", skb);
381 
382 			ASSERTCMP(skb->mark, ==, 0);
383 
384 			_debug("HS: %u", call->conn->security_size);
385 			skb_reserve(skb, call->conn->security_size);
386 			skb->len += call->conn->security_size;
387 
388 			sp->remain = chunk;
389 			if (sp->remain > skb_tailroom(skb))
390 				sp->remain = skb_tailroom(skb);
391 
392 			_net("skb: hr %d, tr %d, hl %d, rm %d",
393 			       skb_headroom(skb),
394 			       skb_tailroom(skb),
395 			       skb_headlen(skb),
396 			       sp->remain);
397 
398 			skb->ip_summed = CHECKSUM_UNNECESSARY;
399 		}
400 
401 		_debug("append");
402 		sp = rxrpc_skb(skb);
403 
404 		/* append next segment of data to the current buffer */
405 		if (msg_data_left(msg) > 0) {
406 			int copy = skb_tailroom(skb);
407 			ASSERTCMP(copy, >, 0);
408 			if (copy > msg_data_left(msg))
409 				copy = msg_data_left(msg);
410 			if (copy > sp->remain)
411 				copy = sp->remain;
412 
413 			_debug("add");
414 			ret = skb_add_data(skb, &msg->msg_iter, copy);
415 			_debug("added");
416 			if (ret < 0)
417 				goto efault;
418 			sp->remain -= copy;
419 			skb->mark += 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 (call->state == RXRPC_CALL_COMPLETE)
428 			goto call_terminated;
429 
430 		/* add the packet to the send queue if it's now full */
431 		if (sp->remain <= 0 ||
432 		    (msg_data_left(msg) == 0 && !more)) {
433 			struct rxrpc_connection *conn = call->conn;
434 			uint32_t seq;
435 			size_t pad;
436 
437 			/* pad out if we're using security */
438 			if (conn->security_ix) {
439 				pad = conn->security_size + skb->mark;
440 				pad = conn->size_align - pad;
441 				pad &= conn->size_align - 1;
442 				_debug("pad %zu", pad);
443 				if (pad)
444 					skb_put_zero(skb, pad);
445 			}
446 
447 			seq = call->tx_top + 1;
448 
449 			sp->hdr.seq	= seq;
450 			sp->hdr._rsvd	= 0;
451 			sp->hdr.flags	= conn->out_clientflag;
452 
453 			if (msg_data_left(msg) == 0 && !more)
454 				sp->hdr.flags |= RXRPC_LAST_PACKET;
455 			else if (call->tx_top - call->tx_hard_ack <
456 				 call->tx_winsize)
457 				sp->hdr.flags |= RXRPC_MORE_PACKETS;
458 
459 			ret = call->security->secure_packet(
460 				call, skb, skb->mark, skb->head);
461 			if (ret < 0)
462 				goto out;
463 
464 			ret = rxrpc_queue_packet(rx, call, skb,
465 						 !msg_data_left(msg) && !more,
466 						 notify_end_tx);
467 			/* Should check for failure here */
468 			skb = NULL;
469 		}
470 	} while (msg_data_left(msg) > 0);
471 
472 success:
473 	ret = copied;
474 out:
475 	call->tx_pending = skb;
476 	_leave(" = %d", ret);
477 	return ret;
478 
479 call_terminated:
480 	rxrpc_free_skb(skb, rxrpc_skb_freed);
481 	_leave(" = %d", call->error);
482 	return call->error;
483 
484 maybe_error:
485 	if (copied)
486 		goto success;
487 	goto out;
488 
489 efault:
490 	ret = -EFAULT;
491 	goto out;
492 }
493 
494 /*
495  * extract control messages from the sendmsg() control buffer
496  */
497 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
498 {
499 	struct cmsghdr *cmsg;
500 	bool got_user_ID = false;
501 	int len;
502 
503 	if (msg->msg_controllen == 0)
504 		return -EINVAL;
505 
506 	for_each_cmsghdr(cmsg, msg) {
507 		if (!CMSG_OK(msg, cmsg))
508 			return -EINVAL;
509 
510 		len = cmsg->cmsg_len - sizeof(struct cmsghdr);
511 		_debug("CMSG %d, %d, %d",
512 		       cmsg->cmsg_level, cmsg->cmsg_type, len);
513 
514 		if (cmsg->cmsg_level != SOL_RXRPC)
515 			continue;
516 
517 		switch (cmsg->cmsg_type) {
518 		case RXRPC_USER_CALL_ID:
519 			if (msg->msg_flags & MSG_CMSG_COMPAT) {
520 				if (len != sizeof(u32))
521 					return -EINVAL;
522 				p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
523 			} else {
524 				if (len != sizeof(unsigned long))
525 					return -EINVAL;
526 				p->call.user_call_ID = *(unsigned long *)
527 					CMSG_DATA(cmsg);
528 			}
529 			got_user_ID = true;
530 			break;
531 
532 		case RXRPC_ABORT:
533 			if (p->command != RXRPC_CMD_SEND_DATA)
534 				return -EINVAL;
535 			p->command = RXRPC_CMD_SEND_ABORT;
536 			if (len != sizeof(p->abort_code))
537 				return -EINVAL;
538 			p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
539 			if (p->abort_code == 0)
540 				return -EINVAL;
541 			break;
542 
543 		case RXRPC_ACCEPT:
544 			if (p->command != RXRPC_CMD_SEND_DATA)
545 				return -EINVAL;
546 			p->command = RXRPC_CMD_ACCEPT;
547 			if (len != 0)
548 				return -EINVAL;
549 			break;
550 
551 		case RXRPC_EXCLUSIVE_CALL:
552 			p->exclusive = true;
553 			if (len != 0)
554 				return -EINVAL;
555 			break;
556 
557 		case RXRPC_UPGRADE_SERVICE:
558 			p->upgrade = true;
559 			if (len != 0)
560 				return -EINVAL;
561 			break;
562 
563 		case RXRPC_TX_LENGTH:
564 			if (p->call.tx_total_len != -1 || len != sizeof(__s64))
565 				return -EINVAL;
566 			p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
567 			if (p->call.tx_total_len < 0)
568 				return -EINVAL;
569 			break;
570 
571 		case RXRPC_SET_CALL_TIMEOUT:
572 			if (len & 3 || len < 4 || len > 12)
573 				return -EINVAL;
574 			memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
575 			p->call.nr_timeouts = len / 4;
576 			if (p->call.timeouts.hard > INT_MAX / HZ)
577 				return -ERANGE;
578 			if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
579 				return -ERANGE;
580 			if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
581 				return -ERANGE;
582 			break;
583 
584 		default:
585 			return -EINVAL;
586 		}
587 	}
588 
589 	if (!got_user_ID)
590 		return -EINVAL;
591 	if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
592 		return -EINVAL;
593 	_leave(" = 0");
594 	return 0;
595 }
596 
597 /*
598  * Create a new client call for sendmsg().
599  * - Called with the socket lock held, which it must release.
600  * - If it returns a call, the call's lock will need releasing by the caller.
601  */
602 static struct rxrpc_call *
603 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
604 				  struct rxrpc_send_params *p)
605 	__releases(&rx->sk.sk_lock.slock)
606 	__acquires(&call->user_mutex)
607 {
608 	struct rxrpc_conn_parameters cp;
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 	key = rx->key;
622 	if (key && !rx->key->payload.data[0])
623 		key = NULL;
624 
625 	memset(&cp, 0, sizeof(cp));
626 	cp.local		= rx->local;
627 	cp.key			= rx->key;
628 	cp.security_level	= rx->min_sec_level;
629 	cp.exclusive		= rx->exclusive | p->exclusive;
630 	cp.upgrade		= p->upgrade;
631 	cp.service_id		= srx->srx_service;
632 	call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
633 				     atomic_inc_return(&rxrpc_debug_id));
634 	/* The socket is now unlocked */
635 
636 	rxrpc_put_peer(cp.peer);
637 	_leave(" = %p\n", call);
638 	return call;
639 }
640 
641 /*
642  * send a message forming part of a client call through an RxRPC socket
643  * - caller holds the socket locked
644  * - the socket may be either a client socket or a server socket
645  */
646 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
647 	__releases(&rx->sk.sk_lock.slock)
648 	__releases(&call->user_mutex)
649 {
650 	enum rxrpc_call_state state;
651 	struct rxrpc_call *call;
652 	unsigned long now, j;
653 	int ret;
654 
655 	struct rxrpc_send_params p = {
656 		.call.tx_total_len	= -1,
657 		.call.user_call_ID	= 0,
658 		.call.nr_timeouts	= 0,
659 		.call.interruptibility	= RXRPC_INTERRUPTIBLE,
660 		.abort_code		= 0,
661 		.command		= RXRPC_CMD_SEND_DATA,
662 		.exclusive		= false,
663 		.upgrade		= false,
664 	};
665 
666 	_enter("");
667 
668 	ret = rxrpc_sendmsg_cmsg(msg, &p);
669 	if (ret < 0)
670 		goto error_release_sock;
671 
672 	if (p.command == RXRPC_CMD_ACCEPT) {
673 		ret = -EINVAL;
674 		if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
675 			goto error_release_sock;
676 		call = rxrpc_accept_call(rx, p.call.user_call_ID, NULL);
677 		/* The socket is now unlocked. */
678 		if (IS_ERR(call))
679 			return PTR_ERR(call);
680 		ret = 0;
681 		goto out_put_unlock;
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 	} else {
695 		switch (READ_ONCE(call->state)) {
696 		case RXRPC_CALL_UNINITIALISED:
697 		case RXRPC_CALL_CLIENT_AWAIT_CONN:
698 		case RXRPC_CALL_SERVER_PREALLOC:
699 		case RXRPC_CALL_SERVER_SECURING:
700 		case RXRPC_CALL_SERVER_ACCEPTING:
701 			rxrpc_put_call(call, rxrpc_call_put);
702 			ret = -EBUSY;
703 			goto error_release_sock;
704 		default:
705 			break;
706 		}
707 
708 		ret = mutex_lock_interruptible(&call->user_mutex);
709 		release_sock(&rx->sk);
710 		if (ret < 0) {
711 			ret = -ERESTARTSYS;
712 			goto error_put;
713 		}
714 
715 		if (p.call.tx_total_len != -1) {
716 			ret = -EINVAL;
717 			if (call->tx_total_len != -1 ||
718 			    call->tx_pending ||
719 			    call->tx_top != 0)
720 				goto error_put;
721 			call->tx_total_len = p.call.tx_total_len;
722 		}
723 	}
724 
725 	switch (p.call.nr_timeouts) {
726 	case 3:
727 		j = msecs_to_jiffies(p.call.timeouts.normal);
728 		if (p.call.timeouts.normal > 0 && j == 0)
729 			j = 1;
730 		WRITE_ONCE(call->next_rx_timo, j);
731 		/* Fall through */
732 	case 2:
733 		j = msecs_to_jiffies(p.call.timeouts.idle);
734 		if (p.call.timeouts.idle > 0 && j == 0)
735 			j = 1;
736 		WRITE_ONCE(call->next_req_timo, j);
737 		/* Fall through */
738 	case 1:
739 		if (p.call.timeouts.hard > 0) {
740 			j = msecs_to_jiffies(p.call.timeouts.hard);
741 			now = jiffies;
742 			j += now;
743 			WRITE_ONCE(call->expect_term_by, j);
744 			rxrpc_reduce_call_timer(call, j, now,
745 						rxrpc_timer_set_for_hard);
746 		}
747 		break;
748 	}
749 
750 	state = READ_ONCE(call->state);
751 	_debug("CALL %d USR %lx ST %d on CONN %p",
752 	       call->debug_id, call->user_call_ID, state, call->conn);
753 
754 	if (state >= RXRPC_CALL_COMPLETE) {
755 		/* it's too late for this call */
756 		ret = -ESHUTDOWN;
757 	} else if (p.command == RXRPC_CMD_SEND_ABORT) {
758 		ret = 0;
759 		if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
760 			ret = rxrpc_send_abort_packet(call);
761 	} else if (p.command != RXRPC_CMD_SEND_DATA) {
762 		ret = -EINVAL;
763 	} else if (rxrpc_is_client_call(call) &&
764 		   state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
765 		/* request phase complete for this client call */
766 		ret = -EPROTO;
767 	} else if (rxrpc_is_service_call(call) &&
768 		   state != RXRPC_CALL_SERVER_ACK_REQUEST &&
769 		   state != RXRPC_CALL_SERVER_SEND_REPLY) {
770 		/* Reply phase not begun or not complete for service call. */
771 		ret = -EPROTO;
772 	} else {
773 		ret = rxrpc_send_data(rx, call, msg, len, NULL);
774 	}
775 
776 out_put_unlock:
777 	mutex_unlock(&call->user_mutex);
778 error_put:
779 	rxrpc_put_call(call, rxrpc_call_put);
780 	_leave(" = %d", ret);
781 	return ret;
782 
783 error_release_sock:
784 	release_sock(&rx->sk);
785 	return ret;
786 }
787 
788 /**
789  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
790  * @sock: The socket the call is on
791  * @call: The call to send data through
792  * @msg: The data to send
793  * @len: The amount of data to send
794  * @notify_end_tx: Notification that the last packet is queued.
795  *
796  * Allow a kernel service to send data on a call.  The call must be in an state
797  * appropriate to sending data.  No control data should be supplied in @msg,
798  * nor should an address be supplied.  MSG_MORE should be flagged if there's
799  * more data to come, otherwise this data will end the transmission phase.
800  */
801 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
802 			   struct msghdr *msg, size_t len,
803 			   rxrpc_notify_end_tx_t notify_end_tx)
804 {
805 	int ret;
806 
807 	_enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
808 
809 	ASSERTCMP(msg->msg_name, ==, NULL);
810 	ASSERTCMP(msg->msg_control, ==, NULL);
811 
812 	mutex_lock(&call->user_mutex);
813 
814 	_debug("CALL %d USR %lx ST %d on CONN %p",
815 	       call->debug_id, call->user_call_ID, call->state, call->conn);
816 
817 	switch (READ_ONCE(call->state)) {
818 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
819 	case RXRPC_CALL_SERVER_ACK_REQUEST:
820 	case RXRPC_CALL_SERVER_SEND_REPLY:
821 		ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
822 				      notify_end_tx);
823 		break;
824 	case RXRPC_CALL_COMPLETE:
825 		read_lock_bh(&call->state_lock);
826 		ret = call->error;
827 		read_unlock_bh(&call->state_lock);
828 		break;
829 	default:
830 		/* Request phase complete for this client call */
831 		trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
832 		ret = -EPROTO;
833 		break;
834 	}
835 
836 	mutex_unlock(&call->user_mutex);
837 	_leave(" = %d", ret);
838 	return ret;
839 }
840 EXPORT_SYMBOL(rxrpc_kernel_send_data);
841 
842 /**
843  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
844  * @sock: The socket the call is on
845  * @call: The call to be aborted
846  * @abort_code: The abort code to stick into the ABORT packet
847  * @error: Local error value
848  * @why: 3-char string indicating why.
849  *
850  * Allow a kernel service to abort a call, if it's still in an abortable state
851  * and return true if the call was aborted, false if it was already complete.
852  */
853 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
854 			     u32 abort_code, int error, const char *why)
855 {
856 	bool aborted;
857 
858 	_enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
859 
860 	mutex_lock(&call->user_mutex);
861 
862 	aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
863 	if (aborted)
864 		rxrpc_send_abort_packet(call);
865 
866 	mutex_unlock(&call->user_mutex);
867 	return aborted;
868 }
869 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
870 
871 /**
872  * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
873  * @sock: The socket the call is on
874  * @call: The call to be informed
875  * @tx_total_len: The amount of data to be transmitted for this call
876  *
877  * Allow a kernel service to set the total transmit length on a call.  This
878  * allows buffer-to-packet encrypt-and-copy to be performed.
879  *
880  * This function is primarily for use for setting the reply length since the
881  * request length can be set when beginning the call.
882  */
883 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
884 				s64 tx_total_len)
885 {
886 	WARN_ON(call->tx_total_len != -1);
887 	call->tx_total_len = tx_total_len;
888 }
889 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);
890