1 /* RxRPC packet transmission
3 * Copyright (C) 2007 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 License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, 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/circ_buf.h>
18 #include <linux/export.h>
20 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
24 * Time till packet resend (in jiffies).
26 unsigned int rxrpc_resend_timeout
= 4 * HZ
;
28 static int rxrpc_send_data(struct rxrpc_sock
*rx
,
29 struct rxrpc_call
*call
,
30 struct msghdr
*msg
, size_t len
);
33 * extract control messages from the sendmsg() control buffer
35 static int rxrpc_sendmsg_cmsg(struct msghdr
*msg
,
36 unsigned long *user_call_ID
,
37 enum rxrpc_command
*command
,
41 bool got_user_ID
= false;
44 *command
= RXRPC_CMD_SEND_DATA
;
46 if (msg
->msg_controllen
== 0)
49 for_each_cmsghdr(cmsg
, msg
) {
50 if (!CMSG_OK(msg
, cmsg
))
53 len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
54 _debug("CMSG %d, %d, %d",
55 cmsg
->cmsg_level
, cmsg
->cmsg_type
, len
);
57 if (cmsg
->cmsg_level
!= SOL_RXRPC
)
60 switch (cmsg
->cmsg_type
) {
61 case RXRPC_USER_CALL_ID
:
62 if (msg
->msg_flags
& MSG_CMSG_COMPAT
) {
63 if (len
!= sizeof(u32
))
65 *user_call_ID
= *(u32
*) CMSG_DATA(cmsg
);
67 if (len
!= sizeof(unsigned long))
69 *user_call_ID
= *(unsigned long *)
72 _debug("User Call ID %lx", *user_call_ID
);
77 if (*command
!= RXRPC_CMD_SEND_DATA
)
79 *command
= RXRPC_CMD_SEND_ABORT
;
80 if (len
!= sizeof(*abort_code
))
82 *abort_code
= *(unsigned int *) CMSG_DATA(cmsg
);
83 _debug("Abort %x", *abort_code
);
89 if (*command
!= RXRPC_CMD_SEND_DATA
)
91 *command
= RXRPC_CMD_ACCEPT
;
108 * abort a call, sending an ABORT packet to the peer
110 static void rxrpc_send_abort(struct rxrpc_call
*call
, u32 abort_code
)
112 write_lock_bh(&call
->state_lock
);
114 if (call
->state
<= RXRPC_CALL_COMPLETE
) {
115 call
->state
= RXRPC_CALL_LOCALLY_ABORTED
;
116 call
->local_abort
= abort_code
;
117 set_bit(RXRPC_CALL_EV_ABORT
, &call
->events
);
118 del_timer_sync(&call
->resend_timer
);
119 del_timer_sync(&call
->ack_timer
);
120 clear_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
);
121 clear_bit(RXRPC_CALL_EV_ACK
, &call
->events
);
122 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
123 rxrpc_queue_call(call
);
126 write_unlock_bh(&call
->state_lock
);
130 * Create a new client call for sendmsg().
132 static struct rxrpc_call
*
133 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock
*rx
, struct msghdr
*msg
,
134 unsigned long user_call_ID
)
136 struct rxrpc_conn_bundle
*bundle
;
137 struct rxrpc_transport
*trans
;
138 struct rxrpc_call
*call
;
142 DECLARE_SOCKADDR(struct sockaddr_rxrpc
*, srx
, msg
->msg_name
);
147 return ERR_PTR(-EDESTADDRREQ
);
149 trans
= rxrpc_name_to_transport(rx
, msg
->msg_name
, msg
->msg_namelen
, 0,
152 ret
= PTR_ERR(trans
);
157 if (key
&& !rx
->key
->payload
.data
[0])
159 bundle
= rxrpc_get_bundle(rx
, trans
, key
, srx
->srx_service
, GFP_KERNEL
);
160 if (IS_ERR(bundle
)) {
161 ret
= PTR_ERR(bundle
);
165 call
= rxrpc_new_client_call(rx
, trans
, bundle
, user_call_ID
,
167 rxrpc_put_bundle(trans
, bundle
);
168 rxrpc_put_transport(trans
);
174 _leave(" = %p\n", call
);
178 rxrpc_put_transport(trans
);
180 _leave(" = %ld", ret
);
185 * send a message forming part of a client call through an RxRPC socket
186 * - caller holds the socket locked
187 * - the socket may be either a client socket or a server socket
189 int rxrpc_do_sendmsg(struct rxrpc_sock
*rx
, struct msghdr
*msg
, size_t len
)
191 enum rxrpc_command cmd
;
192 struct rxrpc_call
*call
;
193 unsigned long user_call_ID
= 0;
199 ret
= rxrpc_sendmsg_cmsg(msg
, &user_call_ID
, &cmd
, &abort_code
);
203 if (cmd
== RXRPC_CMD_ACCEPT
) {
204 if (rx
->sk
.sk_state
!= RXRPC_SERVER_LISTENING
)
206 call
= rxrpc_accept_call(rx
, user_call_ID
);
208 return PTR_ERR(call
);
209 rxrpc_put_call(call
);
213 call
= rxrpc_find_call_by_user_ID(rx
, user_call_ID
);
215 if (cmd
!= RXRPC_CMD_SEND_DATA
)
217 call
= rxrpc_new_client_call_for_sendmsg(rx
, msg
, user_call_ID
);
219 return PTR_ERR(call
);
222 _debug("CALL %d USR %lx ST %d on CONN %p",
223 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
225 if (call
->state
>= RXRPC_CALL_COMPLETE
) {
226 /* it's too late for this call */
228 } else if (cmd
== RXRPC_CMD_SEND_ABORT
) {
229 rxrpc_send_abort(call
, abort_code
);
231 } else if (cmd
!= RXRPC_CMD_SEND_DATA
) {
233 } else if (!call
->in_clientflag
&&
234 call
->state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
) {
235 /* request phase complete for this client call */
237 } else if (call
->in_clientflag
&&
238 call
->state
!= RXRPC_CALL_SERVER_ACK_REQUEST
&&
239 call
->state
!= RXRPC_CALL_SERVER_SEND_REPLY
) {
240 /* Reply phase not begun or not complete for service call. */
243 ret
= rxrpc_send_data(rx
, call
, msg
, len
);
246 rxrpc_put_call(call
);
247 _leave(" = %d", ret
);
252 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
253 * @call: The call to send data through
254 * @msg: The data to send
255 * @len: The amount of data to send
257 * Allow a kernel service to send data on a call. The call must be in an state
258 * appropriate to sending data. No control data should be supplied in @msg,
259 * nor should an address be supplied. MSG_MORE should be flagged if there's
260 * more data to come, otherwise this data will end the transmission phase.
262 int rxrpc_kernel_send_data(struct rxrpc_call
*call
, struct msghdr
*msg
,
267 _enter("{%d,%s},", call
->debug_id
, rxrpc_call_states
[call
->state
]);
269 ASSERTCMP(msg
->msg_name
, ==, NULL
);
270 ASSERTCMP(msg
->msg_control
, ==, NULL
);
272 lock_sock(&call
->socket
->sk
);
274 _debug("CALL %d USR %lx ST %d on CONN %p",
275 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
277 if (call
->state
>= RXRPC_CALL_COMPLETE
) {
278 ret
= -ESHUTDOWN
; /* it's too late for this call */
279 } else if (call
->state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
&&
280 call
->state
!= RXRPC_CALL_SERVER_ACK_REQUEST
&&
281 call
->state
!= RXRPC_CALL_SERVER_SEND_REPLY
) {
282 ret
= -EPROTO
; /* request phase complete for this client call */
284 ret
= rxrpc_send_data(call
->socket
, call
, msg
, len
);
287 release_sock(&call
->socket
->sk
);
288 _leave(" = %d", ret
);
292 EXPORT_SYMBOL(rxrpc_kernel_send_data
);
295 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
296 * @call: The call to be aborted
297 * @abort_code: The abort code to stick into the ABORT packet
299 * Allow a kernel service to abort a call, if it's still in an abortable state.
301 void rxrpc_kernel_abort_call(struct rxrpc_call
*call
, u32 abort_code
)
303 _enter("{%d},%d", call
->debug_id
, abort_code
);
305 lock_sock(&call
->socket
->sk
);
307 _debug("CALL %d USR %lx ST %d on CONN %p",
308 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
310 if (call
->state
< RXRPC_CALL_COMPLETE
)
311 rxrpc_send_abort(call
, abort_code
);
313 release_sock(&call
->socket
->sk
);
317 EXPORT_SYMBOL(rxrpc_kernel_abort_call
);
320 * send a packet through the transport endpoint
322 int rxrpc_send_packet(struct rxrpc_transport
*trans
, struct sk_buff
*skb
)
328 _enter(",{%d}", skb
->len
);
330 iov
[0].iov_base
= skb
->head
;
331 iov
[0].iov_len
= skb
->len
;
333 msg
.msg_name
= &trans
->peer
->srx
.transport
.sin
;
334 msg
.msg_namelen
= sizeof(trans
->peer
->srx
.transport
.sin
);
335 msg
.msg_control
= NULL
;
336 msg
.msg_controllen
= 0;
339 /* send the packet with the don't fragment bit set if we currently
340 * think it's small enough */
341 if (skb
->len
- sizeof(struct rxrpc_wire_header
) < trans
->peer
->maxdata
) {
342 down_read(&trans
->local
->defrag_sem
);
343 /* send the packet by UDP
344 * - returns -EMSGSIZE if UDP would have to fragment the packet
345 * to go out of the interface
346 * - in which case, we'll have processed the ICMP error
347 * message and update the peer record
349 ret
= kernel_sendmsg(trans
->local
->socket
, &msg
, iov
, 1,
352 up_read(&trans
->local
->defrag_sem
);
353 if (ret
== -EMSGSIZE
)
354 goto send_fragmentable
;
356 _leave(" = %d [%u]", ret
, trans
->peer
->maxdata
);
361 /* attempt to send this message with fragmentation enabled */
362 _debug("send fragment");
364 down_write(&trans
->local
->defrag_sem
);
365 opt
= IP_PMTUDISC_DONT
;
366 ret
= kernel_setsockopt(trans
->local
->socket
, SOL_IP
, IP_MTU_DISCOVER
,
367 (char *) &opt
, sizeof(opt
));
369 ret
= kernel_sendmsg(trans
->local
->socket
, &msg
, iov
, 1,
372 opt
= IP_PMTUDISC_DO
;
373 kernel_setsockopt(trans
->local
->socket
, SOL_IP
,
374 IP_MTU_DISCOVER
, (char *) &opt
, sizeof(opt
));
377 up_write(&trans
->local
->defrag_sem
);
378 _leave(" = %d [frag %u]", ret
, trans
->peer
->maxdata
);
383 * wait for space to appear in the transmit/ACK window
384 * - caller holds the socket locked
386 static int rxrpc_wait_for_tx_window(struct rxrpc_sock
*rx
,
387 struct rxrpc_call
*call
,
390 DECLARE_WAITQUEUE(myself
, current
);
394 CIRC_SPACE(call
->acks_head
, ACCESS_ONCE(call
->acks_tail
),
398 add_wait_queue(&call
->tx_waitq
, &myself
);
401 set_current_state(TASK_INTERRUPTIBLE
);
403 if (CIRC_SPACE(call
->acks_head
, ACCESS_ONCE(call
->acks_tail
),
404 call
->acks_winsz
) > 0)
406 if (signal_pending(current
)) {
407 ret
= sock_intr_errno(*timeo
);
411 release_sock(&rx
->sk
);
412 *timeo
= schedule_timeout(*timeo
);
416 remove_wait_queue(&call
->tx_waitq
, &myself
);
417 set_current_state(TASK_RUNNING
);
418 _leave(" = %d", ret
);
423 * attempt to schedule an instant Tx resend
425 static inline void rxrpc_instant_resend(struct rxrpc_call
*call
)
427 read_lock_bh(&call
->state_lock
);
428 if (try_to_del_timer_sync(&call
->resend_timer
) >= 0) {
429 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
430 if (call
->state
< RXRPC_CALL_COMPLETE
&&
431 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
))
432 rxrpc_queue_call(call
);
434 read_unlock_bh(&call
->state_lock
);
438 * queue a packet for transmission, set the resend timer and attempt
439 * to send the packet immediately
441 static void rxrpc_queue_packet(struct rxrpc_call
*call
, struct sk_buff
*skb
,
444 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
447 _net("queue skb %p [%d]", skb
, call
->acks_head
);
449 ASSERT(call
->acks_window
!= NULL
);
450 call
->acks_window
[call
->acks_head
] = (unsigned long) skb
;
452 call
->acks_head
= (call
->acks_head
+ 1) & (call
->acks_winsz
- 1);
454 if (last
|| call
->state
== RXRPC_CALL_SERVER_ACK_REQUEST
) {
455 _debug("________awaiting reply/ACK__________");
456 write_lock_bh(&call
->state_lock
);
457 switch (call
->state
) {
458 case RXRPC_CALL_CLIENT_SEND_REQUEST
:
459 call
->state
= RXRPC_CALL_CLIENT_AWAIT_REPLY
;
461 case RXRPC_CALL_SERVER_ACK_REQUEST
:
462 call
->state
= RXRPC_CALL_SERVER_SEND_REPLY
;
465 case RXRPC_CALL_SERVER_SEND_REPLY
:
466 call
->state
= RXRPC_CALL_SERVER_AWAIT_ACK
;
471 write_unlock_bh(&call
->state_lock
);
474 _proto("Tx DATA %%%u { #%u }", sp
->hdr
.serial
, sp
->hdr
.seq
);
476 sp
->need_resend
= false;
477 sp
->resend_at
= jiffies
+ rxrpc_resend_timeout
;
478 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
)) {
480 call
->resend_timer
.expires
= sp
->resend_at
;
481 add_timer(&call
->resend_timer
);
484 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
485 * we're ACK'ing the request phase of an incoming call */
487 if (try_to_del_timer_sync(&call
->ack_timer
) >= 0) {
488 /* the packet may be freed by rxrpc_process_call() before this
490 ret
= rxrpc_send_packet(call
->conn
->trans
, skb
);
491 _net("sent skb %p", skb
);
493 _debug("failed to delete ACK timer");
497 _debug("need instant resend %d", ret
);
498 sp
->need_resend
= true;
499 rxrpc_instant_resend(call
);
506 * Convert a host-endian header into a network-endian header.
508 static void rxrpc_insert_header(struct sk_buff
*skb
)
510 struct rxrpc_wire_header whdr
;
511 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
513 whdr
.epoch
= htonl(sp
->hdr
.epoch
);
514 whdr
.cid
= htonl(sp
->hdr
.cid
);
515 whdr
.callNumber
= htonl(sp
->hdr
.callNumber
);
516 whdr
.seq
= htonl(sp
->hdr
.seq
);
517 whdr
.serial
= htonl(sp
->hdr
.serial
);
518 whdr
.type
= sp
->hdr
.type
;
519 whdr
.flags
= sp
->hdr
.flags
;
520 whdr
.userStatus
= sp
->hdr
.userStatus
;
521 whdr
.securityIndex
= sp
->hdr
.securityIndex
;
522 whdr
._rsvd
= htons(sp
->hdr
._rsvd
);
523 whdr
.serviceId
= htons(sp
->hdr
.serviceId
);
525 memcpy(skb
->head
, &whdr
, sizeof(whdr
));
529 * send data through a socket
530 * - must be called in process context
531 * - caller holds the socket locked
533 static int rxrpc_send_data(struct rxrpc_sock
*rx
,
534 struct rxrpc_call
*call
,
535 struct msghdr
*msg
, size_t len
)
537 struct rxrpc_skb_priv
*sp
;
539 struct sock
*sk
= &rx
->sk
;
544 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
546 /* this should be in poll */
547 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
549 if (sk
->sk_err
|| (sk
->sk_shutdown
& SEND_SHUTDOWN
))
552 more
= msg
->msg_flags
& MSG_MORE
;
554 skb
= call
->tx_pending
;
555 call
->tx_pending
= NULL
;
560 size_t size
, chunk
, max
, space
;
564 if (CIRC_SPACE(call
->acks_head
,
565 ACCESS_ONCE(call
->acks_tail
),
566 call
->acks_winsz
) <= 0) {
568 if (msg
->msg_flags
& MSG_DONTWAIT
)
570 ret
= rxrpc_wait_for_tx_window(rx
, call
,
576 max
= call
->conn
->trans
->peer
->maxdata
;
577 max
-= call
->conn
->security_size
;
578 max
&= ~(call
->conn
->size_align
- 1UL);
581 if (chunk
> msg_data_left(msg
) && !more
)
582 chunk
= msg_data_left(msg
);
584 space
= chunk
+ call
->conn
->size_align
;
585 space
&= ~(call
->conn
->size_align
- 1UL);
587 size
= space
+ call
->conn
->header_size
;
589 _debug("SIZE: %zu/%zu/%zu", chunk
, space
, size
);
591 /* create a buffer that we can retain until it's ACK'd */
592 skb
= sock_alloc_send_skb(
593 sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
, &ret
);
599 _debug("ALLOC SEND %p", skb
);
601 ASSERTCMP(skb
->mark
, ==, 0);
603 _debug("HS: %u", call
->conn
->header_size
);
604 skb_reserve(skb
, call
->conn
->header_size
);
605 skb
->len
+= call
->conn
->header_size
;
609 if (sp
->remain
> skb_tailroom(skb
))
610 sp
->remain
= skb_tailroom(skb
);
612 _net("skb: hr %d, tr %d, hl %d, rm %d",
618 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
624 /* append next segment of data to the current buffer */
625 if (msg_data_left(msg
) > 0) {
626 int copy
= skb_tailroom(skb
);
627 ASSERTCMP(copy
, >, 0);
628 if (copy
> msg_data_left(msg
))
629 copy
= msg_data_left(msg
);
630 if (copy
> sp
->remain
)
634 ret
= skb_add_data(skb
, &msg
->msg_iter
, copy
);
643 /* check for the far side aborting the call or a network error
645 if (call
->state
> RXRPC_CALL_COMPLETE
)
648 /* add the packet to the send queue if it's now full */
649 if (sp
->remain
<= 0 ||
650 (msg_data_left(msg
) == 0 && !more
)) {
651 struct rxrpc_connection
*conn
= call
->conn
;
655 /* pad out if we're using security */
656 if (conn
->security_ix
) {
657 pad
= conn
->security_size
+ skb
->mark
;
658 pad
= conn
->size_align
- pad
;
659 pad
&= conn
->size_align
- 1;
660 _debug("pad %zu", pad
);
662 memset(skb_put(skb
, pad
), 0, pad
);
665 seq
= atomic_inc_return(&call
->sequence
);
667 sp
->hdr
.epoch
= conn
->epoch
;
668 sp
->hdr
.cid
= call
->cid
;
669 sp
->hdr
.callNumber
= call
->call_id
;
671 sp
->hdr
.serial
= atomic_inc_return(&conn
->serial
);
672 sp
->hdr
.type
= RXRPC_PACKET_TYPE_DATA
;
673 sp
->hdr
.userStatus
= 0;
674 sp
->hdr
.securityIndex
= conn
->security_ix
;
676 sp
->hdr
.serviceId
= call
->service_id
;
678 sp
->hdr
.flags
= conn
->out_clientflag
;
679 if (msg_data_left(msg
) == 0 && !more
)
680 sp
->hdr
.flags
|= RXRPC_LAST_PACKET
;
681 else if (CIRC_SPACE(call
->acks_head
,
682 ACCESS_ONCE(call
->acks_tail
),
683 call
->acks_winsz
) > 1)
684 sp
->hdr
.flags
|= RXRPC_MORE_PACKETS
;
686 sp
->hdr
.flags
|= RXRPC_REQUEST_ACK
;
688 ret
= conn
->security
->secure_packet(
689 call
, skb
, skb
->mark
,
690 skb
->head
+ sizeof(struct rxrpc_wire_header
));
694 rxrpc_insert_header(skb
);
695 rxrpc_queue_packet(call
, skb
, !msg_data_left(msg
) && !more
);
698 } while (msg_data_left(msg
) > 0);
703 call
->tx_pending
= skb
;
704 _leave(" = %d", ret
);
709 if (call
->state
== RXRPC_CALL_NETWORK_ERROR
)
710 ret
= call
->conn
->trans
->peer
->net_error
;
713 _leave(" = %d", ret
);