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 * Queue a DATA packet for transmission, set the resend timeout and send the
107 static void rxrpc_queue_packet(struct rxrpc_call
*call
, struct sk_buff
*skb
,
110 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
111 rxrpc_seq_t seq
= sp
->hdr
.seq
;
113 u8 annotation
= RXRPC_TX_ANNO_UNACK
;
115 _net("queue skb %p [%d]", skb
, seq
);
117 ASSERTCMP(seq
, ==, call
->tx_top
+ 1);
120 annotation
|= RXRPC_TX_ANNO_LAST
;
122 /* We have to set the timestamp before queueing as the retransmit
123 * algorithm can see the packet as soon as we queue it.
125 skb
->tstamp
= ktime_get_real();
127 ix
= seq
& RXRPC_RXTX_BUFF_MASK
;
128 rxrpc_get_skb(skb
, rxrpc_skb_tx_got
);
129 call
->rxtx_annotations
[ix
] = annotation
;
131 call
->rxtx_buffer
[ix
] = skb
;
134 trace_rxrpc_transmit(call
, rxrpc_transmit_queue_last
);
136 trace_rxrpc_transmit(call
, rxrpc_transmit_queue
);
138 if (last
|| call
->state
== RXRPC_CALL_SERVER_ACK_REQUEST
) {
139 _debug("________awaiting reply/ACK__________");
140 write_lock_bh(&call
->state_lock
);
141 switch (call
->state
) {
142 case RXRPC_CALL_CLIENT_SEND_REQUEST
:
143 call
->state
= RXRPC_CALL_CLIENT_AWAIT_REPLY
;
145 case RXRPC_CALL_SERVER_ACK_REQUEST
:
146 call
->state
= RXRPC_CALL_SERVER_SEND_REPLY
;
147 call
->ack_at
= call
->expire_at
;
148 if (call
->ackr_reason
== RXRPC_ACK_DELAY
)
149 call
->ackr_reason
= 0;
150 __rxrpc_set_timer(call
, rxrpc_timer_init_for_send_reply
,
154 case RXRPC_CALL_SERVER_SEND_REPLY
:
155 call
->state
= RXRPC_CALL_SERVER_AWAIT_ACK
;
160 write_unlock_bh(&call
->state_lock
);
163 if (seq
== 1 && rxrpc_is_client_call(call
))
164 rxrpc_expose_client_call(call
);
166 ret
= rxrpc_send_data_packet(call
, skb
, false);
168 _debug("need instant resend %d", ret
);
169 rxrpc_instant_resend(call
, ix
);
171 ktime_t now
= ktime_get_real(), resend_at
;
173 resend_at
= ktime_add_ms(now
, rxrpc_resend_timeout
);
175 if (ktime_before(resend_at
, call
->resend_at
)) {
176 call
->resend_at
= resend_at
;
177 rxrpc_set_timer(call
, rxrpc_timer_set_for_send
, now
);
181 rxrpc_free_skb(skb
, rxrpc_skb_tx_freed
);
186 * send data through a socket
187 * - must be called in process context
188 * - The caller holds the call user access mutex, but not the socket lock.
190 static int rxrpc_send_data(struct rxrpc_sock
*rx
,
191 struct rxrpc_call
*call
,
192 struct msghdr
*msg
, size_t len
)
194 struct rxrpc_skb_priv
*sp
;
196 struct sock
*sk
= &rx
->sk
;
201 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
203 /* this should be in poll */
204 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
206 if (sk
->sk_err
|| (sk
->sk_shutdown
& SEND_SHUTDOWN
))
209 more
= msg
->msg_flags
& MSG_MORE
;
211 if (call
->tx_total_len
!= -1) {
212 if (len
> call
->tx_total_len
)
214 if (!more
&& len
!= call
->tx_total_len
)
218 skb
= call
->tx_pending
;
219 call
->tx_pending
= NULL
;
220 rxrpc_see_skb(skb
, rxrpc_skb_tx_seen
);
224 /* Check to see if there's a ping ACK to reply to. */
225 if (call
->ackr_reason
== RXRPC_ACK_PING_RESPONSE
)
226 rxrpc_send_ack_packet(call
, false);
229 size_t size
, chunk
, max
, space
;
233 if (call
->tx_top
- call
->tx_hard_ack
>=
234 min_t(unsigned int, call
->tx_winsize
,
235 call
->cong_cwnd
+ call
->cong_extra
)) {
237 if (msg
->msg_flags
& MSG_DONTWAIT
)
239 ret
= rxrpc_wait_for_tx_window(rx
, call
,
245 max
= RXRPC_JUMBO_DATALEN
;
246 max
-= call
->conn
->security_size
;
247 max
&= ~(call
->conn
->size_align
- 1UL);
250 if (chunk
> msg_data_left(msg
) && !more
)
251 chunk
= msg_data_left(msg
);
253 space
= chunk
+ call
->conn
->size_align
;
254 space
&= ~(call
->conn
->size_align
- 1UL);
256 size
= space
+ call
->conn
->security_size
;
258 _debug("SIZE: %zu/%zu/%zu", chunk
, space
, size
);
260 /* create a buffer that we can retain until it's ACK'd */
261 skb
= sock_alloc_send_skb(
262 sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
, &ret
);
266 rxrpc_new_skb(skb
, rxrpc_skb_tx_new
);
268 _debug("ALLOC SEND %p", skb
);
270 ASSERTCMP(skb
->mark
, ==, 0);
272 _debug("HS: %u", call
->conn
->security_size
);
273 skb_reserve(skb
, call
->conn
->security_size
);
274 skb
->len
+= call
->conn
->security_size
;
278 if (sp
->remain
> skb_tailroom(skb
))
279 sp
->remain
= skb_tailroom(skb
);
281 _net("skb: hr %d, tr %d, hl %d, rm %d",
287 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
293 /* append next segment of data to the current buffer */
294 if (msg_data_left(msg
) > 0) {
295 int copy
= skb_tailroom(skb
);
296 ASSERTCMP(copy
, >, 0);
297 if (copy
> msg_data_left(msg
))
298 copy
= msg_data_left(msg
);
299 if (copy
> sp
->remain
)
303 ret
= skb_add_data(skb
, &msg
->msg_iter
, copy
);
310 if (call
->tx_total_len
!= -1)
311 call
->tx_total_len
-= copy
;
314 /* check for the far side aborting the call or a network error
316 if (call
->state
== RXRPC_CALL_COMPLETE
)
317 goto call_terminated
;
319 /* add the packet to the send queue if it's now full */
320 if (sp
->remain
<= 0 ||
321 (msg_data_left(msg
) == 0 && !more
)) {
322 struct rxrpc_connection
*conn
= call
->conn
;
326 /* pad out if we're using security */
327 if (conn
->security_ix
) {
328 pad
= conn
->security_size
+ skb
->mark
;
329 pad
= conn
->size_align
- pad
;
330 pad
&= conn
->size_align
- 1;
331 _debug("pad %zu", pad
);
333 skb_put_zero(skb
, pad
);
336 seq
= call
->tx_top
+ 1;
340 sp
->hdr
.flags
= conn
->out_clientflag
;
342 if (msg_data_left(msg
) == 0 && !more
)
343 sp
->hdr
.flags
|= RXRPC_LAST_PACKET
;
344 else if (call
->tx_top
- call
->tx_hard_ack
<
346 sp
->hdr
.flags
|= RXRPC_MORE_PACKETS
;
348 ret
= conn
->security
->secure_packet(
349 call
, skb
, skb
->mark
, skb
->head
);
353 rxrpc_queue_packet(call
, skb
, !msg_data_left(msg
) && !more
);
356 } while (msg_data_left(msg
) > 0);
361 call
->tx_pending
= skb
;
362 _leave(" = %d", ret
);
366 rxrpc_free_skb(skb
, rxrpc_skb_tx_freed
);
367 _leave(" = %d", -call
->error
);
381 * extract control messages from the sendmsg() control buffer
383 static int rxrpc_sendmsg_cmsg(struct msghdr
*msg
, struct rxrpc_send_params
*p
)
385 struct cmsghdr
*cmsg
;
386 bool got_user_ID
= false;
389 if (msg
->msg_controllen
== 0)
392 for_each_cmsghdr(cmsg
, msg
) {
393 if (!CMSG_OK(msg
, cmsg
))
396 len
= cmsg
->cmsg_len
- sizeof(struct cmsghdr
);
397 _debug("CMSG %d, %d, %d",
398 cmsg
->cmsg_level
, cmsg
->cmsg_type
, len
);
400 if (cmsg
->cmsg_level
!= SOL_RXRPC
)
403 switch (cmsg
->cmsg_type
) {
404 case RXRPC_USER_CALL_ID
:
405 if (msg
->msg_flags
& MSG_CMSG_COMPAT
) {
406 if (len
!= sizeof(u32
))
408 p
->user_call_ID
= *(u32
*)CMSG_DATA(cmsg
);
410 if (len
!= sizeof(unsigned long))
412 p
->user_call_ID
= *(unsigned long *)
419 if (p
->command
!= RXRPC_CMD_SEND_DATA
)
421 p
->command
= RXRPC_CMD_SEND_ABORT
;
422 if (len
!= sizeof(p
->abort_code
))
424 p
->abort_code
= *(unsigned int *)CMSG_DATA(cmsg
);
425 if (p
->abort_code
== 0)
430 if (p
->command
!= RXRPC_CMD_SEND_DATA
)
432 p
->command
= RXRPC_CMD_ACCEPT
;
437 case RXRPC_EXCLUSIVE_CALL
:
443 case RXRPC_UPGRADE_SERVICE
:
449 case RXRPC_TX_LENGTH
:
450 if (p
->tx_total_len
!= -1 || len
!= sizeof(__s64
))
452 p
->tx_total_len
= *(__s64
*)CMSG_DATA(cmsg
);
453 if (p
->tx_total_len
< 0)
464 if (p
->tx_total_len
!= -1 && p
->command
!= RXRPC_CMD_SEND_DATA
)
471 * Create a new client call for sendmsg().
472 * - Called with the socket lock held, which it must release.
473 * - If it returns a call, the call's lock will need releasing by the caller.
475 static struct rxrpc_call
*
476 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock
*rx
, struct msghdr
*msg
,
477 struct rxrpc_send_params
*p
)
478 __releases(&rx
->sk
.sk_lock
.slock
)
480 struct rxrpc_conn_parameters cp
;
481 struct rxrpc_call
*call
;
484 DECLARE_SOCKADDR(struct sockaddr_rxrpc
*, srx
, msg
->msg_name
);
488 if (!msg
->msg_name
) {
489 release_sock(&rx
->sk
);
490 return ERR_PTR(-EDESTADDRREQ
);
494 if (key
&& !rx
->key
->payload
.data
[0])
497 memset(&cp
, 0, sizeof(cp
));
498 cp
.local
= rx
->local
;
500 cp
.security_level
= rx
->min_sec_level
;
501 cp
.exclusive
= rx
->exclusive
| p
->exclusive
;
502 cp
.upgrade
= p
->upgrade
;
503 cp
.service_id
= srx
->srx_service
;
504 call
= rxrpc_new_client_call(rx
, &cp
, srx
, p
->user_call_ID
,
505 p
->tx_total_len
, GFP_KERNEL
);
506 /* The socket is now unlocked */
508 _leave(" = %p\n", call
);
513 * send a message forming part of a client call through an RxRPC socket
514 * - caller holds the socket locked
515 * - the socket may be either a client socket or a server socket
517 int rxrpc_do_sendmsg(struct rxrpc_sock
*rx
, struct msghdr
*msg
, size_t len
)
518 __releases(&rx
->sk
.sk_lock
.slock
)
520 enum rxrpc_call_state state
;
521 struct rxrpc_call
*call
;
524 struct rxrpc_send_params p
= {
528 .command
= RXRPC_CMD_SEND_DATA
,
535 ret
= rxrpc_sendmsg_cmsg(msg
, &p
);
537 goto error_release_sock
;
539 if (p
.command
== RXRPC_CMD_ACCEPT
) {
541 if (rx
->sk
.sk_state
!= RXRPC_SERVER_LISTENING
)
542 goto error_release_sock
;
543 call
= rxrpc_accept_call(rx
, p
.user_call_ID
, NULL
);
544 /* The socket is now unlocked. */
546 return PTR_ERR(call
);
547 rxrpc_put_call(call
, rxrpc_call_put
);
551 call
= rxrpc_find_call_by_user_ID(rx
, p
.user_call_ID
);
554 if (p
.command
!= RXRPC_CMD_SEND_DATA
)
555 goto error_release_sock
;
556 call
= rxrpc_new_client_call_for_sendmsg(rx
, msg
, &p
);
557 /* The socket is now unlocked... */
559 return PTR_ERR(call
);
560 /* ... and we have the call lock. */
562 switch (READ_ONCE(call
->state
)) {
563 case RXRPC_CALL_UNINITIALISED
:
564 case RXRPC_CALL_CLIENT_AWAIT_CONN
:
565 case RXRPC_CALL_SERVER_PREALLOC
:
566 case RXRPC_CALL_SERVER_SECURING
:
567 case RXRPC_CALL_SERVER_ACCEPTING
:
569 goto error_release_sock
;
574 ret
= mutex_lock_interruptible(&call
->user_mutex
);
575 release_sock(&rx
->sk
);
581 if (p
.tx_total_len
!= -1) {
583 if (call
->tx_total_len
!= -1 ||
587 call
->tx_total_len
= p
.tx_total_len
;
591 state
= READ_ONCE(call
->state
);
592 _debug("CALL %d USR %lx ST %d on CONN %p",
593 call
->debug_id
, call
->user_call_ID
, state
, call
->conn
);
595 if (state
>= RXRPC_CALL_COMPLETE
) {
596 /* it's too late for this call */
598 } else if (p
.command
== RXRPC_CMD_SEND_ABORT
) {
600 if (rxrpc_abort_call("CMD", call
, 0, p
.abort_code
, -ECONNABORTED
))
601 ret
= rxrpc_send_abort_packet(call
);
602 } else if (p
.command
!= RXRPC_CMD_SEND_DATA
) {
604 } else if (rxrpc_is_client_call(call
) &&
605 state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
) {
606 /* request phase complete for this client call */
608 } else if (rxrpc_is_service_call(call
) &&
609 state
!= RXRPC_CALL_SERVER_ACK_REQUEST
&&
610 state
!= RXRPC_CALL_SERVER_SEND_REPLY
) {
611 /* Reply phase not begun or not complete for service call. */
614 ret
= rxrpc_send_data(rx
, call
, msg
, len
);
617 mutex_unlock(&call
->user_mutex
);
619 rxrpc_put_call(call
, rxrpc_call_put
);
620 _leave(" = %d", ret
);
624 release_sock(&rx
->sk
);
629 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
630 * @sock: The socket the call is on
631 * @call: The call to send data through
632 * @msg: The data to send
633 * @len: The amount of data to send
635 * Allow a kernel service to send data on a call. The call must be in an state
636 * appropriate to sending data. No control data should be supplied in @msg,
637 * nor should an address be supplied. MSG_MORE should be flagged if there's
638 * more data to come, otherwise this data will end the transmission phase.
640 int rxrpc_kernel_send_data(struct socket
*sock
, struct rxrpc_call
*call
,
641 struct msghdr
*msg
, size_t len
)
645 _enter("{%d,%s},", call
->debug_id
, rxrpc_call_states
[call
->state
]);
647 ASSERTCMP(msg
->msg_name
, ==, NULL
);
648 ASSERTCMP(msg
->msg_control
, ==, NULL
);
650 mutex_lock(&call
->user_mutex
);
652 _debug("CALL %d USR %lx ST %d on CONN %p",
653 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
655 switch (READ_ONCE(call
->state
)) {
656 case RXRPC_CALL_CLIENT_SEND_REQUEST
:
657 case RXRPC_CALL_SERVER_ACK_REQUEST
:
658 case RXRPC_CALL_SERVER_SEND_REPLY
:
659 ret
= rxrpc_send_data(rxrpc_sk(sock
->sk
), call
, msg
, len
);
661 case RXRPC_CALL_COMPLETE
:
662 read_lock_bh(&call
->state_lock
);
664 read_unlock_bh(&call
->state_lock
);
667 /* Request phase complete for this client call */
668 trace_rxrpc_rx_eproto(call
, 0, tracepoint_string("late_send"));
673 mutex_unlock(&call
->user_mutex
);
674 _leave(" = %d", ret
);
677 EXPORT_SYMBOL(rxrpc_kernel_send_data
);
680 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
681 * @sock: The socket the call is on
682 * @call: The call to be aborted
683 * @abort_code: The abort code to stick into the ABORT packet
684 * @error: Local error value
685 * @why: 3-char string indicating why.
687 * Allow a kernel service to abort a call, if it's still in an abortable state
688 * and return true if the call was aborted, false if it was already complete.
690 bool rxrpc_kernel_abort_call(struct socket
*sock
, struct rxrpc_call
*call
,
691 u32 abort_code
, int error
, const char *why
)
695 _enter("{%d},%d,%d,%s", call
->debug_id
, abort_code
, error
, why
);
697 mutex_lock(&call
->user_mutex
);
699 aborted
= rxrpc_abort_call(why
, call
, 0, abort_code
, error
);
701 rxrpc_send_abort_packet(call
);
703 mutex_unlock(&call
->user_mutex
);
706 EXPORT_SYMBOL(rxrpc_kernel_abort_call
);
709 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
710 * @sock: The socket the call is on
711 * @call: The call to be informed
712 * @tx_total_len: The amount of data to be transmitted for this call
714 * Allow a kernel service to set the total transmit length on a call. This
715 * allows buffer-to-packet encrypt-and-copy to be performed.
717 * This function is primarily for use for setting the reply length since the
718 * request length can be set when beginning the call.
720 void rxrpc_kernel_set_tx_length(struct socket
*sock
, struct rxrpc_call
*call
,
723 WARN_ON(call
->tx_total_len
!= -1);
724 call
->tx_total_len
= tx_total_len
;
726 EXPORT_SYMBOL(rxrpc_kernel_set_tx_length
);