1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AF_RXRPC sendmsg() implementation.
4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
21 * Return true if there's sufficient Tx queue space.
23 static bool rxrpc_check_tx_space(struct rxrpc_call *call, rxrpc_seq_t *_tx_win)
25 unsigned int win_size;
26 rxrpc_seq_t tx_win = smp_load_acquire(&call->acks_hard_ack);
28 /* If we haven't transmitted anything for >1RTT, we should reset the
29 * congestion management state.
31 if (ktime_before(ktime_add_us(call->tx_last_sent,
32 call->peer->srtt_us >> 3),
34 if (RXRPC_TX_SMSS > 2190)
36 else if (RXRPC_TX_SMSS > 1095)
40 win_size += call->cong_extra;
42 win_size = min_t(unsigned int, call->tx_winsize,
43 call->cong_cwnd + call->cong_extra);
48 return call->tx_top - tx_win < win_size;
52 * Wait for space to appear in the Tx queue or a signal to occur.
54 static int rxrpc_wait_for_tx_window_intr(struct rxrpc_sock *rx,
55 struct rxrpc_call *call,
59 set_current_state(TASK_INTERRUPTIBLE);
60 if (rxrpc_check_tx_space(call, NULL))
63 if (call->state >= RXRPC_CALL_COMPLETE)
66 if (signal_pending(current))
67 return sock_intr_errno(*timeo);
69 if (READ_ONCE(call->acks_hard_ack) != call->tx_bottom) {
70 rxrpc_shrink_call_tx_buffer(call);
74 trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
75 *timeo = schedule_timeout(*timeo);
80 * Wait for space to appear in the Tx queue uninterruptibly, but with
81 * a timeout of 2*RTT if no progress was made and a signal occurred.
83 static int rxrpc_wait_for_tx_window_waitall(struct rxrpc_sock *rx,
84 struct rxrpc_call *call)
86 rxrpc_seq_t tx_start, tx_win;
87 signed long rtt, timeout;
89 rtt = READ_ONCE(call->peer->srtt_us) >> 3;
90 rtt = usecs_to_jiffies(rtt) * 2;
95 tx_start = smp_load_acquire(&call->acks_hard_ack);
98 set_current_state(TASK_UNINTERRUPTIBLE);
100 if (rxrpc_check_tx_space(call, &tx_win))
103 if (call->state >= RXRPC_CALL_COMPLETE)
107 tx_win == tx_start && signal_pending(current))
110 if (READ_ONCE(call->acks_hard_ack) != call->tx_bottom) {
111 rxrpc_shrink_call_tx_buffer(call);
115 if (tx_win != tx_start) {
120 trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
121 timeout = schedule_timeout(timeout);
126 * Wait for space to appear in the Tx queue uninterruptibly.
128 static int rxrpc_wait_for_tx_window_nonintr(struct rxrpc_sock *rx,
129 struct rxrpc_call *call,
133 set_current_state(TASK_UNINTERRUPTIBLE);
134 if (rxrpc_check_tx_space(call, NULL))
137 if (call->state >= RXRPC_CALL_COMPLETE)
140 if (READ_ONCE(call->acks_hard_ack) != call->tx_bottom) {
141 rxrpc_shrink_call_tx_buffer(call);
145 trace_rxrpc_txqueue(call, rxrpc_txqueue_wait);
146 *timeo = schedule_timeout(*timeo);
151 * wait for space to appear in the transmit/ACK window
152 * - caller holds the socket locked
154 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
155 struct rxrpc_call *call,
159 DECLARE_WAITQUEUE(myself, current);
162 _enter(",{%u,%u,%u,%u}",
163 call->tx_bottom, call->acks_hard_ack, call->tx_top, call->tx_winsize);
165 add_wait_queue(&call->waitq, &myself);
167 switch (call->interruptibility) {
168 case RXRPC_INTERRUPTIBLE:
170 ret = rxrpc_wait_for_tx_window_waitall(rx, call);
172 ret = rxrpc_wait_for_tx_window_intr(rx, call, timeo);
174 case RXRPC_PREINTERRUPTIBLE:
175 case RXRPC_UNINTERRUPTIBLE:
177 ret = rxrpc_wait_for_tx_window_nonintr(rx, call, timeo);
181 remove_wait_queue(&call->waitq, &myself);
182 set_current_state(TASK_RUNNING);
183 _leave(" = %d", ret);
188 * Notify the owner of the call that the transmit phase is ended and the last
189 * packet has been queued.
191 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
192 rxrpc_notify_end_tx_t notify_end_tx)
195 notify_end_tx(&rx->sk, call, call->user_call_ID);
199 * Queue a DATA packet for transmission, set the resend timeout and send
200 * the packet immediately. Returns the error from rxrpc_send_data_packet()
201 * in case the caller wants to do something with it.
203 static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
204 struct rxrpc_txbuf *txb,
205 rxrpc_notify_end_tx_t notify_end_tx)
208 rxrpc_seq_t seq = txb->seq;
209 bool last = test_bit(RXRPC_TXBUF_LAST, &txb->flags);
212 rxrpc_inc_stat(call->rxnet, stat_tx_data);
214 ASSERTCMP(seq, ==, call->tx_top + 1);
216 /* We have to set the timestamp before queueing as the retransmit
217 * algorithm can see the packet as soon as we queue it.
219 txb->last_sent = ktime_get_real();
221 /* Add the packet to the call's output buffer */
222 rxrpc_get_txbuf(txb, rxrpc_txbuf_get_buffer);
223 spin_lock(&call->tx_lock);
224 list_add_tail(&txb->call_link, &call->tx_buffer);
226 spin_unlock(&call->tx_lock);
229 trace_rxrpc_txqueue(call, rxrpc_txqueue_queue_last);
231 trace_rxrpc_txqueue(call, rxrpc_txqueue_queue);
233 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
234 _debug("________awaiting reply/ACK__________");
235 write_lock_bh(&call->state_lock);
236 switch (call->state) {
237 case RXRPC_CALL_CLIENT_SEND_REQUEST:
238 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
239 rxrpc_notify_end_tx(rx, call, notify_end_tx);
241 case RXRPC_CALL_SERVER_ACK_REQUEST:
242 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
244 WRITE_ONCE(call->delay_ack_at, now + MAX_JIFFY_OFFSET);
245 if (call->ackr_reason == RXRPC_ACK_DELAY)
246 call->ackr_reason = 0;
247 trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
251 case RXRPC_CALL_SERVER_SEND_REPLY:
252 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
253 rxrpc_notify_end_tx(rx, call, notify_end_tx);
258 write_unlock_bh(&call->state_lock);
261 if (seq == 1 && rxrpc_is_client_call(call))
262 rxrpc_expose_client_call(call);
264 ret = rxrpc_send_data_packet(call, txb);
270 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
275 unsigned long now = jiffies;
276 unsigned long resend_at = now + call->peer->rto_j;
278 WRITE_ONCE(call->resend_at, resend_at);
279 rxrpc_reduce_call_timer(call, resend_at, now,
280 rxrpc_timer_set_for_send);
284 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_trans);
288 * send data through a socket
289 * - must be called in process context
290 * - The caller holds the call user access mutex, but not the socket lock.
292 static int rxrpc_send_data(struct rxrpc_sock *rx,
293 struct rxrpc_call *call,
294 struct msghdr *msg, size_t len,
295 rxrpc_notify_end_tx_t notify_end_tx,
298 struct rxrpc_txbuf *txb;
299 struct sock *sk = &rx->sk;
300 enum rxrpc_call_state state;
302 bool more = msg->msg_flags & MSG_MORE;
305 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
307 /* this should be in poll */
308 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
312 if (sk->sk_shutdown & SEND_SHUTDOWN)
314 state = READ_ONCE(call->state);
316 if (state >= RXRPC_CALL_COMPLETE)
319 if (state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
320 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
321 state != RXRPC_CALL_SERVER_SEND_REPLY)
325 if (call->tx_total_len != -1) {
326 if (len - copied > call->tx_total_len)
328 if (!more && len - copied != call->tx_total_len)
332 txb = call->tx_pending;
333 call->tx_pending = NULL;
335 rxrpc_see_txbuf(txb, rxrpc_txbuf_see_send_more);
338 rxrpc_transmit_ack_packets(call->peer->local);
341 size_t remain, bufsize, chunk, offset;
345 if (!rxrpc_check_tx_space(call, NULL))
348 /* Work out the maximum size of a packet. Assume that
349 * the security header is going to be in the padded
350 * region (enc blocksize), but the trailer is not.
352 remain = more ? INT_MAX : msg_data_left(msg);
353 ret = call->conn->security->how_much_data(call, remain,
354 &bufsize, &chunk, &offset);
358 _debug("SIZE: %zu/%zu @%zu", chunk, bufsize, offset);
360 /* create a buffer that we can retain until it's ACK'd */
362 txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_DATA,
367 txb->offset = offset;
368 txb->space -= offset;
369 txb->space = min_t(size_t, chunk, txb->space);
374 /* append next segment of data to the current buffer */
375 if (msg_data_left(msg) > 0) {
376 size_t copy = min_t(size_t, txb->space, msg_data_left(msg));
378 _debug("add %zu", copy);
379 if (!copy_from_iter_full(txb->data + txb->offset, copy,
387 if (call->tx_total_len != -1)
388 call->tx_total_len -= copy;
391 /* check for the far side aborting the call or a network error
393 if (call->state == RXRPC_CALL_COMPLETE)
394 goto call_terminated;
396 /* add the packet to the send queue if it's now full */
398 (msg_data_left(msg) == 0 && !more)) {
399 if (msg_data_left(msg) == 0 && !more) {
400 txb->wire.flags |= RXRPC_LAST_PACKET;
401 __set_bit(RXRPC_TXBUF_LAST, &txb->flags);
403 else if (call->tx_top - call->acks_hard_ack <
405 txb->wire.flags |= RXRPC_MORE_PACKETS;
407 ret = call->security->secure_packet(call, txb);
411 rxrpc_queue_packet(rx, call, txb, notify_end_tx);
414 } while (msg_data_left(msg) > 0);
418 if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE) {
419 read_lock_bh(&call->state_lock);
422 read_unlock_bh(&call->state_lock);
425 call->tx_pending = txb;
426 _leave(" = %d", ret);
430 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_send_aborted);
431 _leave(" = %d", call->error);
445 if (msg->msg_flags & MSG_DONTWAIT)
447 mutex_unlock(&call->user_mutex);
448 *_dropped_lock = true;
449 ret = rxrpc_wait_for_tx_window(rx, call, &timeo,
450 msg->msg_flags & MSG_WAITALL);
453 if (call->interruptibility == RXRPC_INTERRUPTIBLE) {
454 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
455 ret = sock_intr_errno(timeo);
459 mutex_lock(&call->user_mutex);
461 *_dropped_lock = false;
466 * extract control messages from the sendmsg() control buffer
468 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
470 struct cmsghdr *cmsg;
471 bool got_user_ID = false;
474 if (msg->msg_controllen == 0)
477 for_each_cmsghdr(cmsg, msg) {
478 if (!CMSG_OK(msg, cmsg))
481 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
482 _debug("CMSG %d, %d, %d",
483 cmsg->cmsg_level, cmsg->cmsg_type, len);
485 if (cmsg->cmsg_level != SOL_RXRPC)
488 switch (cmsg->cmsg_type) {
489 case RXRPC_USER_CALL_ID:
490 if (msg->msg_flags & MSG_CMSG_COMPAT) {
491 if (len != sizeof(u32))
493 p->call.user_call_ID = *(u32 *)CMSG_DATA(cmsg);
495 if (len != sizeof(unsigned long))
497 p->call.user_call_ID = *(unsigned long *)
504 if (p->command != RXRPC_CMD_SEND_DATA)
506 p->command = RXRPC_CMD_SEND_ABORT;
507 if (len != sizeof(p->abort_code))
509 p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
510 if (p->abort_code == 0)
514 case RXRPC_CHARGE_ACCEPT:
515 if (p->command != RXRPC_CMD_SEND_DATA)
517 p->command = RXRPC_CMD_CHARGE_ACCEPT;
522 case RXRPC_EXCLUSIVE_CALL:
528 case RXRPC_UPGRADE_SERVICE:
534 case RXRPC_TX_LENGTH:
535 if (p->call.tx_total_len != -1 || len != sizeof(__s64))
537 p->call.tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
538 if (p->call.tx_total_len < 0)
542 case RXRPC_SET_CALL_TIMEOUT:
543 if (len & 3 || len < 4 || len > 12)
545 memcpy(&p->call.timeouts, CMSG_DATA(cmsg), len);
546 p->call.nr_timeouts = len / 4;
547 if (p->call.timeouts.hard > INT_MAX / HZ)
549 if (p->call.nr_timeouts >= 2 && p->call.timeouts.idle > 60 * 60 * 1000)
551 if (p->call.nr_timeouts >= 3 && p->call.timeouts.normal > 60 * 60 * 1000)
562 if (p->call.tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
569 * Create a new client call for sendmsg().
570 * - Called with the socket lock held, which it must release.
571 * - If it returns a call, the call's lock will need releasing by the caller.
573 static struct rxrpc_call *
574 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
575 struct rxrpc_send_params *p)
576 __releases(&rx->sk.sk_lock.slock)
577 __acquires(&call->user_mutex)
579 struct rxrpc_conn_parameters cp;
580 struct rxrpc_call *call;
583 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
587 if (!msg->msg_name) {
588 release_sock(&rx->sk);
589 return ERR_PTR(-EDESTADDRREQ);
593 if (key && !rx->key->payload.data[0])
596 memset(&cp, 0, sizeof(cp));
597 cp.local = rx->local;
599 cp.security_level = rx->min_sec_level;
600 cp.exclusive = rx->exclusive | p->exclusive;
601 cp.upgrade = p->upgrade;
602 cp.service_id = srx->srx_service;
603 call = rxrpc_new_client_call(rx, &cp, srx, &p->call, GFP_KERNEL,
604 atomic_inc_return(&rxrpc_debug_id));
605 /* The socket is now unlocked */
607 rxrpc_put_peer(cp.peer, rxrpc_peer_put_discard_tmp);
608 _leave(" = %p\n", call);
613 * send a message forming part of a client call through an RxRPC socket
614 * - caller holds the socket locked
615 * - the socket may be either a client socket or a server socket
617 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
618 __releases(&rx->sk.sk_lock.slock)
620 enum rxrpc_call_state state;
621 struct rxrpc_call *call;
622 unsigned long now, j;
623 bool dropped_lock = false;
626 struct rxrpc_send_params p = {
627 .call.tx_total_len = -1,
628 .call.user_call_ID = 0,
629 .call.nr_timeouts = 0,
630 .call.interruptibility = RXRPC_INTERRUPTIBLE,
632 .command = RXRPC_CMD_SEND_DATA,
639 ret = rxrpc_sendmsg_cmsg(msg, &p);
641 goto error_release_sock;
643 if (p.command == RXRPC_CMD_CHARGE_ACCEPT) {
645 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
646 goto error_release_sock;
647 ret = rxrpc_user_charge_accept(rx, p.call.user_call_ID);
648 goto error_release_sock;
651 call = rxrpc_find_call_by_user_ID(rx, p.call.user_call_ID);
654 if (p.command != RXRPC_CMD_SEND_DATA)
655 goto error_release_sock;
656 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
657 /* The socket is now unlocked... */
659 return PTR_ERR(call);
660 /* ... and we have the call lock. */
662 if (READ_ONCE(call->state) == RXRPC_CALL_COMPLETE)
665 switch (READ_ONCE(call->state)) {
666 case RXRPC_CALL_UNINITIALISED:
667 case RXRPC_CALL_CLIENT_AWAIT_CONN:
668 case RXRPC_CALL_SERVER_PREALLOC:
669 case RXRPC_CALL_SERVER_SECURING:
670 rxrpc_put_call(call, rxrpc_call_put_sendmsg);
672 goto error_release_sock;
677 ret = mutex_lock_interruptible(&call->user_mutex);
678 release_sock(&rx->sk);
684 if (p.call.tx_total_len != -1) {
686 if (call->tx_total_len != -1 ||
690 call->tx_total_len = p.call.tx_total_len;
694 switch (p.call.nr_timeouts) {
696 j = msecs_to_jiffies(p.call.timeouts.normal);
697 if (p.call.timeouts.normal > 0 && j == 0)
699 WRITE_ONCE(call->next_rx_timo, j);
702 j = msecs_to_jiffies(p.call.timeouts.idle);
703 if (p.call.timeouts.idle > 0 && j == 0)
705 WRITE_ONCE(call->next_req_timo, j);
708 if (p.call.timeouts.hard > 0) {
709 j = msecs_to_jiffies(p.call.timeouts.hard);
712 WRITE_ONCE(call->expect_term_by, j);
713 rxrpc_reduce_call_timer(call, j, now,
714 rxrpc_timer_set_for_hard);
719 state = READ_ONCE(call->state);
720 _debug("CALL %d USR %lx ST %d on CONN %p",
721 call->debug_id, call->user_call_ID, state, call->conn);
723 if (state >= RXRPC_CALL_COMPLETE) {
724 /* it's too late for this call */
726 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
728 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
729 ret = rxrpc_send_abort_packet(call);
730 } else if (p.command != RXRPC_CMD_SEND_DATA) {
733 ret = rxrpc_send_data(rx, call, msg, len, NULL, &dropped_lock);
738 mutex_unlock(&call->user_mutex);
740 rxrpc_put_call(call, rxrpc_call_put_sendmsg);
741 _leave(" = %d", ret);
745 release_sock(&rx->sk);
750 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
751 * @sock: The socket the call is on
752 * @call: The call to send data through
753 * @msg: The data to send
754 * @len: The amount of data to send
755 * @notify_end_tx: Notification that the last packet is queued.
757 * Allow a kernel service to send data on a call. The call must be in an state
758 * appropriate to sending data. No control data should be supplied in @msg,
759 * nor should an address be supplied. MSG_MORE should be flagged if there's
760 * more data to come, otherwise this data will end the transmission phase.
762 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
763 struct msghdr *msg, size_t len,
764 rxrpc_notify_end_tx_t notify_end_tx)
766 bool dropped_lock = false;
769 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
771 ASSERTCMP(msg->msg_name, ==, NULL);
772 ASSERTCMP(msg->msg_control, ==, NULL);
774 mutex_lock(&call->user_mutex);
776 _debug("CALL %d USR %lx ST %d on CONN %p",
777 call->debug_id, call->user_call_ID, call->state, call->conn);
779 switch (READ_ONCE(call->state)) {
780 case RXRPC_CALL_CLIENT_SEND_REQUEST:
781 case RXRPC_CALL_SERVER_ACK_REQUEST:
782 case RXRPC_CALL_SERVER_SEND_REPLY:
783 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
784 notify_end_tx, &dropped_lock);
786 case RXRPC_CALL_COMPLETE:
787 read_lock_bh(&call->state_lock);
789 read_unlock_bh(&call->state_lock);
792 /* Request phase complete for this client call */
793 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
799 mutex_unlock(&call->user_mutex);
800 _leave(" = %d", ret);
803 EXPORT_SYMBOL(rxrpc_kernel_send_data);
806 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
807 * @sock: The socket the call is on
808 * @call: The call to be aborted
809 * @abort_code: The abort code to stick into the ABORT packet
810 * @error: Local error value
811 * @why: 3-char string indicating why.
813 * Allow a kernel service to abort a call, if it's still in an abortable state
814 * and return true if the call was aborted, false if it was already complete.
816 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
817 u32 abort_code, int error, const char *why)
821 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
823 mutex_lock(&call->user_mutex);
825 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
827 rxrpc_send_abort_packet(call);
829 mutex_unlock(&call->user_mutex);
832 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
835 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
836 * @sock: The socket the call is on
837 * @call: The call to be informed
838 * @tx_total_len: The amount of data to be transmitted for this call
840 * Allow a kernel service to set the total transmit length on a call. This
841 * allows buffer-to-packet encrypt-and-copy to be performed.
843 * This function is primarily for use for setting the reply length since the
844 * request length can be set when beginning the call.
846 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
849 WARN_ON(call->tx_total_len != -1);
850 call->tx_total_len = tx_total_len;
852 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);