1 /* AF_RXRPC sendmsg() implementation.
3 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/net.h>
15 #include <linux/gfp.h>
16 #include <linux/skbuff.h>
17 #include <linux/export.h>
18 #include <linux/sched/signal.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
25 RXRPC_CMD_SEND_DATA, /* send data message */
26 RXRPC_CMD_SEND_ABORT, /* request abort generation */
27 RXRPC_CMD_ACCEPT, /* [server] accept incoming call */
28 RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */
31 struct rxrpc_send_params {
32 s64 tx_total_len; /* Total Tx data length (if send data) */
33 unsigned long user_call_ID; /* User's call ID */
34 u32 abort_code; /* Abort code to Tx (if abort) */
35 enum rxrpc_command command : 8; /* The command to implement */
36 bool exclusive; /* Shared or exclusive call */
37 bool upgrade; /* If the connection is upgradeable */
41 * wait for space to appear in the transmit/ACK window
42 * - caller holds the socket locked
44 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
45 struct rxrpc_call *call,
48 DECLARE_WAITQUEUE(myself, current);
52 call->tx_hard_ack, call->tx_top, call->tx_winsize);
54 add_wait_queue(&call->waitq, &myself);
57 set_current_state(TASK_INTERRUPTIBLE);
59 if (call->tx_top - call->tx_hard_ack <
60 min_t(unsigned int, call->tx_winsize,
61 call->cong_cwnd + call->cong_extra))
63 if (call->state >= RXRPC_CALL_COMPLETE) {
67 if (signal_pending(current)) {
68 ret = sock_intr_errno(*timeo);
72 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
73 mutex_unlock(&call->user_mutex);
74 *timeo = schedule_timeout(*timeo);
75 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
76 ret = sock_intr_errno(*timeo);
81 remove_wait_queue(&call->waitq, &myself);
82 set_current_state(TASK_RUNNING);
88 * Schedule an instant Tx resend.
90 static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
92 spin_lock_bh(&call->lock);
94 if (call->state < RXRPC_CALL_COMPLETE) {
95 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
96 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
97 rxrpc_queue_call(call);
100 spin_unlock_bh(&call->lock);
104 * Notify the owner of the call that the transmit phase is ended and the last
105 * packet has been queued.
107 static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
108 rxrpc_notify_end_tx_t notify_end_tx)
111 notify_end_tx(&rx->sk, call, call->user_call_ID);
115 * Queue a DATA packet for transmission, set the resend timeout and send the
118 static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
119 struct sk_buff *skb, bool last,
120 rxrpc_notify_end_tx_t notify_end_tx)
122 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
123 rxrpc_seq_t seq = sp->hdr.seq;
125 u8 annotation = RXRPC_TX_ANNO_UNACK;
127 _net("queue skb %p [%d]", skb, seq);
129 ASSERTCMP(seq, ==, call->tx_top + 1);
132 annotation |= RXRPC_TX_ANNO_LAST;
134 /* We have to set the timestamp before queueing as the retransmit
135 * algorithm can see the packet as soon as we queue it.
137 skb->tstamp = ktime_get_real();
139 ix = seq & RXRPC_RXTX_BUFF_MASK;
140 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
141 call->rxtx_annotations[ix] = annotation;
143 call->rxtx_buffer[ix] = skb;
146 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
148 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
150 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
151 _debug("________awaiting reply/ACK__________");
152 write_lock_bh(&call->state_lock);
153 switch (call->state) {
154 case RXRPC_CALL_CLIENT_SEND_REQUEST:
155 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
156 rxrpc_notify_end_tx(rx, call, notify_end_tx);
158 case RXRPC_CALL_SERVER_ACK_REQUEST:
159 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
160 call->ack_at = call->expire_at;
161 if (call->ackr_reason == RXRPC_ACK_DELAY)
162 call->ackr_reason = 0;
163 __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
167 case RXRPC_CALL_SERVER_SEND_REPLY:
168 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
169 rxrpc_notify_end_tx(rx, call, notify_end_tx);
174 write_unlock_bh(&call->state_lock);
177 if (seq == 1 && rxrpc_is_client_call(call))
178 rxrpc_expose_client_call(call);
180 ret = rxrpc_send_data_packet(call, skb, false);
182 _debug("need instant resend %d", ret);
183 rxrpc_instant_resend(call, ix);
185 ktime_t now = ktime_get_real(), resend_at;
187 resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
189 if (ktime_before(resend_at, call->resend_at)) {
190 call->resend_at = resend_at;
191 rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
195 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
200 * send data through a socket
201 * - must be called in process context
202 * - The caller holds the call user access mutex, but not the socket lock.
204 static int rxrpc_send_data(struct rxrpc_sock *rx,
205 struct rxrpc_call *call,
206 struct msghdr *msg, size_t len,
207 rxrpc_notify_end_tx_t notify_end_tx)
209 struct rxrpc_skb_priv *sp;
211 struct sock *sk = &rx->sk;
216 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
218 /* this should be in poll */
219 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
221 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
224 more = msg->msg_flags & MSG_MORE;
226 if (call->tx_total_len != -1) {
227 if (len > call->tx_total_len)
229 if (!more && len != call->tx_total_len)
233 skb = call->tx_pending;
234 call->tx_pending = NULL;
235 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
239 /* Check to see if there's a ping ACK to reply to. */
240 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
241 rxrpc_send_ack_packet(call, false);
244 size_t size, chunk, max, space;
248 if (call->tx_top - call->tx_hard_ack >=
249 min_t(unsigned int, call->tx_winsize,
250 call->cong_cwnd + call->cong_extra)) {
252 if (msg->msg_flags & MSG_DONTWAIT)
254 ret = rxrpc_wait_for_tx_window(rx, call,
260 max = RXRPC_JUMBO_DATALEN;
261 max -= call->conn->security_size;
262 max &= ~(call->conn->size_align - 1UL);
265 if (chunk > msg_data_left(msg) && !more)
266 chunk = msg_data_left(msg);
268 space = chunk + call->conn->size_align;
269 space &= ~(call->conn->size_align - 1UL);
271 size = space + call->conn->security_size;
273 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
275 /* create a buffer that we can retain until it's ACK'd */
276 skb = sock_alloc_send_skb(
277 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
281 rxrpc_new_skb(skb, rxrpc_skb_tx_new);
283 _debug("ALLOC SEND %p", skb);
285 ASSERTCMP(skb->mark, ==, 0);
287 _debug("HS: %u", call->conn->security_size);
288 skb_reserve(skb, call->conn->security_size);
289 skb->len += call->conn->security_size;
293 if (sp->remain > skb_tailroom(skb))
294 sp->remain = skb_tailroom(skb);
296 _net("skb: hr %d, tr %d, hl %d, rm %d",
302 skb->ip_summed = CHECKSUM_UNNECESSARY;
308 /* append next segment of data to the current buffer */
309 if (msg_data_left(msg) > 0) {
310 int copy = skb_tailroom(skb);
311 ASSERTCMP(copy, >, 0);
312 if (copy > msg_data_left(msg))
313 copy = msg_data_left(msg);
314 if (copy > sp->remain)
318 ret = skb_add_data(skb, &msg->msg_iter, copy);
325 if (call->tx_total_len != -1)
326 call->tx_total_len -= copy;
329 /* check for the far side aborting the call or a network error
331 if (call->state == RXRPC_CALL_COMPLETE)
332 goto call_terminated;
334 /* add the packet to the send queue if it's now full */
335 if (sp->remain <= 0 ||
336 (msg_data_left(msg) == 0 && !more)) {
337 struct rxrpc_connection *conn = call->conn;
341 /* pad out if we're using security */
342 if (conn->security_ix) {
343 pad = conn->security_size + skb->mark;
344 pad = conn->size_align - pad;
345 pad &= conn->size_align - 1;
346 _debug("pad %zu", pad);
348 skb_put_zero(skb, pad);
351 seq = call->tx_top + 1;
355 sp->hdr.flags = conn->out_clientflag;
357 if (msg_data_left(msg) == 0 && !more)
358 sp->hdr.flags |= RXRPC_LAST_PACKET;
359 else if (call->tx_top - call->tx_hard_ack <
361 sp->hdr.flags |= RXRPC_MORE_PACKETS;
363 ret = conn->security->secure_packet(
364 call, skb, skb->mark, skb->head);
368 rxrpc_queue_packet(rx, call, skb,
369 !msg_data_left(msg) && !more,
373 } while (msg_data_left(msg) > 0);
378 call->tx_pending = skb;
379 _leave(" = %d", ret);
383 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
384 _leave(" = %d", call->error);
398 * extract control messages from the sendmsg() control buffer
400 static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
402 struct cmsghdr *cmsg;
403 bool got_user_ID = false;
406 if (msg->msg_controllen == 0)
409 for_each_cmsghdr(cmsg, msg) {
410 if (!CMSG_OK(msg, cmsg))
413 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
414 _debug("CMSG %d, %d, %d",
415 cmsg->cmsg_level, cmsg->cmsg_type, len);
417 if (cmsg->cmsg_level != SOL_RXRPC)
420 switch (cmsg->cmsg_type) {
421 case RXRPC_USER_CALL_ID:
422 if (msg->msg_flags & MSG_CMSG_COMPAT) {
423 if (len != sizeof(u32))
425 p->user_call_ID = *(u32 *)CMSG_DATA(cmsg);
427 if (len != sizeof(unsigned long))
429 p->user_call_ID = *(unsigned long *)
436 if (p->command != RXRPC_CMD_SEND_DATA)
438 p->command = RXRPC_CMD_SEND_ABORT;
439 if (len != sizeof(p->abort_code))
441 p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
442 if (p->abort_code == 0)
447 if (p->command != RXRPC_CMD_SEND_DATA)
449 p->command = RXRPC_CMD_ACCEPT;
454 case RXRPC_EXCLUSIVE_CALL:
460 case RXRPC_UPGRADE_SERVICE:
466 case RXRPC_TX_LENGTH:
467 if (p->tx_total_len != -1 || len != sizeof(__s64))
469 p->tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
470 if (p->tx_total_len < 0)
481 if (p->tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
488 * Create a new client call for sendmsg().
489 * - Called with the socket lock held, which it must release.
490 * - If it returns a call, the call's lock will need releasing by the caller.
492 static struct rxrpc_call *
493 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
494 struct rxrpc_send_params *p)
495 __releases(&rx->sk.sk_lock.slock)
497 struct rxrpc_conn_parameters cp;
498 struct rxrpc_call *call;
501 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
505 if (!msg->msg_name) {
506 release_sock(&rx->sk);
507 return ERR_PTR(-EDESTADDRREQ);
511 if (key && !rx->key->payload.data[0])
514 memset(&cp, 0, sizeof(cp));
515 cp.local = rx->local;
517 cp.security_level = rx->min_sec_level;
518 cp.exclusive = rx->exclusive | p->exclusive;
519 cp.upgrade = p->upgrade;
520 cp.service_id = srx->srx_service;
521 call = rxrpc_new_client_call(rx, &cp, srx, p->user_call_ID,
522 p->tx_total_len, GFP_KERNEL);
523 /* The socket is now unlocked */
525 _leave(" = %p\n", call);
530 * send a message forming part of a client call through an RxRPC socket
531 * - caller holds the socket locked
532 * - the socket may be either a client socket or a server socket
534 int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
535 __releases(&rx->sk.sk_lock.slock)
537 enum rxrpc_call_state state;
538 struct rxrpc_call *call;
541 struct rxrpc_send_params p = {
545 .command = RXRPC_CMD_SEND_DATA,
552 ret = rxrpc_sendmsg_cmsg(msg, &p);
554 goto error_release_sock;
556 if (p.command == RXRPC_CMD_ACCEPT) {
558 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
559 goto error_release_sock;
560 call = rxrpc_accept_call(rx, p.user_call_ID, NULL);
561 /* The socket is now unlocked. */
563 return PTR_ERR(call);
564 rxrpc_put_call(call, rxrpc_call_put);
568 call = rxrpc_find_call_by_user_ID(rx, p.user_call_ID);
571 if (p.command != RXRPC_CMD_SEND_DATA)
572 goto error_release_sock;
573 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
574 /* The socket is now unlocked... */
576 return PTR_ERR(call);
577 /* ... and we have the call lock. */
579 switch (READ_ONCE(call->state)) {
580 case RXRPC_CALL_UNINITIALISED:
581 case RXRPC_CALL_CLIENT_AWAIT_CONN:
582 case RXRPC_CALL_SERVER_PREALLOC:
583 case RXRPC_CALL_SERVER_SECURING:
584 case RXRPC_CALL_SERVER_ACCEPTING:
586 goto error_release_sock;
591 ret = mutex_lock_interruptible(&call->user_mutex);
592 release_sock(&rx->sk);
598 if (p.tx_total_len != -1) {
600 if (call->tx_total_len != -1 ||
604 call->tx_total_len = p.tx_total_len;
608 state = READ_ONCE(call->state);
609 _debug("CALL %d USR %lx ST %d on CONN %p",
610 call->debug_id, call->user_call_ID, state, call->conn);
612 if (state >= RXRPC_CALL_COMPLETE) {
613 /* it's too late for this call */
615 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
617 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
618 ret = rxrpc_send_abort_packet(call);
619 } else if (p.command != RXRPC_CMD_SEND_DATA) {
621 } else if (rxrpc_is_client_call(call) &&
622 state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
623 /* request phase complete for this client call */
625 } else if (rxrpc_is_service_call(call) &&
626 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
627 state != RXRPC_CALL_SERVER_SEND_REPLY) {
628 /* Reply phase not begun or not complete for service call. */
631 ret = rxrpc_send_data(rx, call, msg, len, NULL);
634 mutex_unlock(&call->user_mutex);
636 rxrpc_put_call(call, rxrpc_call_put);
637 _leave(" = %d", ret);
641 release_sock(&rx->sk);
646 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
647 * @sock: The socket the call is on
648 * @call: The call to send data through
649 * @msg: The data to send
650 * @len: The amount of data to send
651 * @notify_end_tx: Notification that the last packet is queued.
653 * Allow a kernel service to send data on a call. The call must be in an state
654 * appropriate to sending data. No control data should be supplied in @msg,
655 * nor should an address be supplied. MSG_MORE should be flagged if there's
656 * more data to come, otherwise this data will end the transmission phase.
658 int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
659 struct msghdr *msg, size_t len,
660 rxrpc_notify_end_tx_t notify_end_tx)
664 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
666 ASSERTCMP(msg->msg_name, ==, NULL);
667 ASSERTCMP(msg->msg_control, ==, NULL);
669 mutex_lock(&call->user_mutex);
671 _debug("CALL %d USR %lx ST %d on CONN %p",
672 call->debug_id, call->user_call_ID, call->state, call->conn);
674 switch (READ_ONCE(call->state)) {
675 case RXRPC_CALL_CLIENT_SEND_REQUEST:
676 case RXRPC_CALL_SERVER_ACK_REQUEST:
677 case RXRPC_CALL_SERVER_SEND_REPLY:
678 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
681 case RXRPC_CALL_COMPLETE:
682 read_lock_bh(&call->state_lock);
684 read_unlock_bh(&call->state_lock);
687 /* Request phase complete for this client call */
688 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
693 mutex_unlock(&call->user_mutex);
694 _leave(" = %d", ret);
697 EXPORT_SYMBOL(rxrpc_kernel_send_data);
700 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
701 * @sock: The socket the call is on
702 * @call: The call to be aborted
703 * @abort_code: The abort code to stick into the ABORT packet
704 * @error: Local error value
705 * @why: 3-char string indicating why.
707 * Allow a kernel service to abort a call, if it's still in an abortable state
708 * and return true if the call was aborted, false if it was already complete.
710 bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
711 u32 abort_code, int error, const char *why)
715 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
717 mutex_lock(&call->user_mutex);
719 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
721 rxrpc_send_abort_packet(call);
723 mutex_unlock(&call->user_mutex);
726 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
729 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
730 * @sock: The socket the call is on
731 * @call: The call to be informed
732 * @tx_total_len: The amount of data to be transmitted for this call
734 * Allow a kernel service to set the total transmit length on a call. This
735 * allows buffer-to-packet encrypt-and-copy to be performed.
737 * This function is primarily for use for setting the reply length since the
738 * request length can be set when beginning the call.
740 void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
743 WARN_ON(call->tx_total_len != -1);
744 call->tx_total_len = tx_total_len;
746 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);