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
,
42 bool got_user_ID
= false;
45 *command
= RXRPC_CMD_SEND_DATA
;
47 if (msg
->msg_controllen
== 0)
50 for_each_cmsghdr(cmsg
, msg
) {
51 if (!CMSG_OK(msg
, cmsg
))
54 len
= cmsg
->cmsg_len
- CMSG_ALIGN(sizeof(struct cmsghdr
));
55 _debug("CMSG %d, %d, %d",
56 cmsg
->cmsg_level
, cmsg
->cmsg_type
, len
);
58 if (cmsg
->cmsg_level
!= SOL_RXRPC
)
61 switch (cmsg
->cmsg_type
) {
62 case RXRPC_USER_CALL_ID
:
63 if (msg
->msg_flags
& MSG_CMSG_COMPAT
) {
64 if (len
!= sizeof(u32
))
66 *user_call_ID
= *(u32
*) CMSG_DATA(cmsg
);
68 if (len
!= sizeof(unsigned long))
70 *user_call_ID
= *(unsigned long *)
73 _debug("User Call ID %lx", *user_call_ID
);
78 if (*command
!= RXRPC_CMD_SEND_DATA
)
80 *command
= RXRPC_CMD_SEND_ABORT
;
81 if (len
!= sizeof(*abort_code
))
83 *abort_code
= *(unsigned int *) CMSG_DATA(cmsg
);
84 _debug("Abort %x", *abort_code
);
90 if (*command
!= RXRPC_CMD_SEND_DATA
)
92 *command
= RXRPC_CMD_ACCEPT
;
97 case RXRPC_EXCLUSIVE_CALL
:
114 * abort a call, sending an ABORT packet to the peer
116 static void rxrpc_send_abort(struct rxrpc_call
*call
, u32 abort_code
)
118 write_lock_bh(&call
->state_lock
);
120 if (call
->state
<= RXRPC_CALL_COMPLETE
) {
121 call
->state
= RXRPC_CALL_LOCALLY_ABORTED
;
122 call
->local_abort
= abort_code
;
123 set_bit(RXRPC_CALL_EV_ABORT
, &call
->events
);
124 del_timer_sync(&call
->resend_timer
);
125 del_timer_sync(&call
->ack_timer
);
126 clear_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
);
127 clear_bit(RXRPC_CALL_EV_ACK
, &call
->events
);
128 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
129 rxrpc_queue_call(call
);
132 write_unlock_bh(&call
->state_lock
);
136 * Create a new client call for sendmsg().
138 static struct rxrpc_call
*
139 rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock
*rx
, struct msghdr
*msg
,
140 unsigned long user_call_ID
, bool exclusive
)
142 struct rxrpc_conn_parameters cp
;
143 struct rxrpc_call
*call
;
146 DECLARE_SOCKADDR(struct sockaddr_rxrpc
*, srx
, msg
->msg_name
);
151 return ERR_PTR(-EDESTADDRREQ
);
154 if (key
&& !rx
->key
->payload
.data
[0])
157 memset(&cp
, 0, sizeof(cp
));
158 cp
.local
= rx
->local
;
160 cp
.security_level
= rx
->min_sec_level
;
161 cp
.exclusive
= rx
->exclusive
| exclusive
;
162 cp
.service_id
= srx
->srx_service
;
163 call
= rxrpc_new_client_call(rx
, &cp
, srx
, user_call_ID
, GFP_KERNEL
);
165 _leave(" = %p\n", call
);
170 * send a message forming part of a client call through an RxRPC socket
171 * - caller holds the socket locked
172 * - the socket may be either a client socket or a server socket
174 int rxrpc_do_sendmsg(struct rxrpc_sock
*rx
, struct msghdr
*msg
, size_t len
)
176 enum rxrpc_command cmd
;
177 struct rxrpc_call
*call
;
178 unsigned long user_call_ID
= 0;
179 bool exclusive
= false;
185 ret
= rxrpc_sendmsg_cmsg(msg
, &user_call_ID
, &cmd
, &abort_code
,
190 if (cmd
== RXRPC_CMD_ACCEPT
) {
191 if (rx
->sk
.sk_state
!= RXRPC_SERVER_LISTENING
)
193 call
= rxrpc_accept_call(rx
, user_call_ID
);
195 return PTR_ERR(call
);
196 rxrpc_put_call(call
);
200 call
= rxrpc_find_call_by_user_ID(rx
, user_call_ID
);
202 if (cmd
!= RXRPC_CMD_SEND_DATA
)
204 call
= rxrpc_new_client_call_for_sendmsg(rx
, msg
, user_call_ID
,
207 return PTR_ERR(call
);
210 _debug("CALL %d USR %lx ST %d on CONN %p",
211 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
213 if (call
->state
>= RXRPC_CALL_COMPLETE
) {
214 /* it's too late for this call */
216 } else if (cmd
== RXRPC_CMD_SEND_ABORT
) {
217 rxrpc_send_abort(call
, abort_code
);
219 } else if (cmd
!= RXRPC_CMD_SEND_DATA
) {
221 } else if (!call
->in_clientflag
&&
222 call
->state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
) {
223 /* request phase complete for this client call */
225 } else if (call
->in_clientflag
&&
226 call
->state
!= RXRPC_CALL_SERVER_ACK_REQUEST
&&
227 call
->state
!= RXRPC_CALL_SERVER_SEND_REPLY
) {
228 /* Reply phase not begun or not complete for service call. */
231 ret
= rxrpc_send_data(rx
, call
, msg
, len
);
234 rxrpc_put_call(call
);
235 _leave(" = %d", ret
);
240 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
241 * @call: The call to send data through
242 * @msg: The data to send
243 * @len: The amount of data to send
245 * Allow a kernel service to send data on a call. The call must be in an state
246 * appropriate to sending data. No control data should be supplied in @msg,
247 * nor should an address be supplied. MSG_MORE should be flagged if there's
248 * more data to come, otherwise this data will end the transmission phase.
250 int rxrpc_kernel_send_data(struct rxrpc_call
*call
, struct msghdr
*msg
,
255 _enter("{%d,%s},", call
->debug_id
, rxrpc_call_states
[call
->state
]);
257 ASSERTCMP(msg
->msg_name
, ==, NULL
);
258 ASSERTCMP(msg
->msg_control
, ==, NULL
);
260 lock_sock(&call
->socket
->sk
);
262 _debug("CALL %d USR %lx ST %d on CONN %p",
263 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
265 if (call
->state
>= RXRPC_CALL_COMPLETE
) {
266 ret
= -ESHUTDOWN
; /* it's too late for this call */
267 } else if (call
->state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
&&
268 call
->state
!= RXRPC_CALL_SERVER_ACK_REQUEST
&&
269 call
->state
!= RXRPC_CALL_SERVER_SEND_REPLY
) {
270 ret
= -EPROTO
; /* request phase complete for this client call */
272 ret
= rxrpc_send_data(call
->socket
, call
, msg
, len
);
275 release_sock(&call
->socket
->sk
);
276 _leave(" = %d", ret
);
280 EXPORT_SYMBOL(rxrpc_kernel_send_data
);
283 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
284 * @call: The call to be aborted
285 * @abort_code: The abort code to stick into the ABORT packet
287 * Allow a kernel service to abort a call, if it's still in an abortable state.
289 void rxrpc_kernel_abort_call(struct rxrpc_call
*call
, u32 abort_code
)
291 _enter("{%d},%d", call
->debug_id
, abort_code
);
293 lock_sock(&call
->socket
->sk
);
295 _debug("CALL %d USR %lx ST %d on CONN %p",
296 call
->debug_id
, call
->user_call_ID
, call
->state
, call
->conn
);
298 if (call
->state
< RXRPC_CALL_COMPLETE
)
299 rxrpc_send_abort(call
, abort_code
);
301 release_sock(&call
->socket
->sk
);
305 EXPORT_SYMBOL(rxrpc_kernel_abort_call
);
308 * send a packet through the transport endpoint
310 int rxrpc_send_data_packet(struct rxrpc_connection
*conn
, struct sk_buff
*skb
)
316 _enter(",{%d}", skb
->len
);
318 iov
[0].iov_base
= skb
->head
;
319 iov
[0].iov_len
= skb
->len
;
321 msg
.msg_name
= &conn
->params
.peer
->srx
.transport
;
322 msg
.msg_namelen
= conn
->params
.peer
->srx
.transport_len
;
323 msg
.msg_control
= NULL
;
324 msg
.msg_controllen
= 0;
327 /* send the packet with the don't fragment bit set if we currently
328 * think it's small enough */
329 if (skb
->len
- sizeof(struct rxrpc_wire_header
) < conn
->params
.peer
->maxdata
) {
330 down_read(&conn
->params
.local
->defrag_sem
);
331 /* send the packet by UDP
332 * - returns -EMSGSIZE if UDP would have to fragment the packet
333 * to go out of the interface
334 * - in which case, we'll have processed the ICMP error
335 * message and update the peer record
337 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 1,
340 up_read(&conn
->params
.local
->defrag_sem
);
341 if (ret
== -EMSGSIZE
)
342 goto send_fragmentable
;
344 _leave(" = %d [%u]", ret
, conn
->params
.peer
->maxdata
);
349 /* attempt to send this message with fragmentation enabled */
350 _debug("send fragment");
352 down_write(&conn
->params
.local
->defrag_sem
);
354 switch (conn
->params
.local
->srx
.transport
.family
) {
356 opt
= IP_PMTUDISC_DONT
;
357 ret
= kernel_setsockopt(conn
->params
.local
->socket
,
358 SOL_IP
, IP_MTU_DISCOVER
,
359 (char *)&opt
, sizeof(opt
));
361 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 1,
364 opt
= IP_PMTUDISC_DO
;
365 kernel_setsockopt(conn
->params
.local
->socket
, SOL_IP
,
367 (char *)&opt
, sizeof(opt
));
372 up_write(&conn
->params
.local
->defrag_sem
);
373 _leave(" = %d [frag %u]", ret
, conn
->params
.peer
->maxdata
);
378 * wait for space to appear in the transmit/ACK window
379 * - caller holds the socket locked
381 static int rxrpc_wait_for_tx_window(struct rxrpc_sock
*rx
,
382 struct rxrpc_call
*call
,
385 DECLARE_WAITQUEUE(myself
, current
);
389 CIRC_SPACE(call
->acks_head
, ACCESS_ONCE(call
->acks_tail
),
393 add_wait_queue(&call
->tx_waitq
, &myself
);
396 set_current_state(TASK_INTERRUPTIBLE
);
398 if (CIRC_SPACE(call
->acks_head
, ACCESS_ONCE(call
->acks_tail
),
399 call
->acks_winsz
) > 0)
401 if (signal_pending(current
)) {
402 ret
= sock_intr_errno(*timeo
);
406 release_sock(&rx
->sk
);
407 *timeo
= schedule_timeout(*timeo
);
411 remove_wait_queue(&call
->tx_waitq
, &myself
);
412 set_current_state(TASK_RUNNING
);
413 _leave(" = %d", ret
);
418 * attempt to schedule an instant Tx resend
420 static inline void rxrpc_instant_resend(struct rxrpc_call
*call
)
422 read_lock_bh(&call
->state_lock
);
423 if (try_to_del_timer_sync(&call
->resend_timer
) >= 0) {
424 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
425 if (call
->state
< RXRPC_CALL_COMPLETE
&&
426 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
))
427 rxrpc_queue_call(call
);
429 read_unlock_bh(&call
->state_lock
);
433 * queue a packet for transmission, set the resend timer and attempt
434 * to send the packet immediately
436 static void rxrpc_queue_packet(struct rxrpc_call
*call
, struct sk_buff
*skb
,
439 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
442 _net("queue skb %p [%d]", skb
, call
->acks_head
);
444 ASSERT(call
->acks_window
!= NULL
);
445 call
->acks_window
[call
->acks_head
] = (unsigned long) skb
;
447 call
->acks_head
= (call
->acks_head
+ 1) & (call
->acks_winsz
- 1);
449 if (last
|| call
->state
== RXRPC_CALL_SERVER_ACK_REQUEST
) {
450 _debug("________awaiting reply/ACK__________");
451 write_lock_bh(&call
->state_lock
);
452 switch (call
->state
) {
453 case RXRPC_CALL_CLIENT_SEND_REQUEST
:
454 call
->state
= RXRPC_CALL_CLIENT_AWAIT_REPLY
;
456 case RXRPC_CALL_SERVER_ACK_REQUEST
:
457 call
->state
= RXRPC_CALL_SERVER_SEND_REPLY
;
460 case RXRPC_CALL_SERVER_SEND_REPLY
:
461 call
->state
= RXRPC_CALL_SERVER_AWAIT_ACK
;
466 write_unlock_bh(&call
->state_lock
);
469 _proto("Tx DATA %%%u { #%u }", sp
->hdr
.serial
, sp
->hdr
.seq
);
471 sp
->need_resend
= false;
472 sp
->resend_at
= jiffies
+ rxrpc_resend_timeout
;
473 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
)) {
475 call
->resend_timer
.expires
= sp
->resend_at
;
476 add_timer(&call
->resend_timer
);
479 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
480 * we're ACK'ing the request phase of an incoming call */
482 if (try_to_del_timer_sync(&call
->ack_timer
) >= 0) {
483 /* the packet may be freed by rxrpc_process_call() before this
485 ret
= rxrpc_send_data_packet(call
->conn
, skb
);
486 _net("sent skb %p", skb
);
488 _debug("failed to delete ACK timer");
492 _debug("need instant resend %d", ret
);
493 sp
->need_resend
= true;
494 rxrpc_instant_resend(call
);
501 * Convert a host-endian header into a network-endian header.
503 static void rxrpc_insert_header(struct sk_buff
*skb
)
505 struct rxrpc_wire_header whdr
;
506 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
508 whdr
.epoch
= htonl(sp
->hdr
.epoch
);
509 whdr
.cid
= htonl(sp
->hdr
.cid
);
510 whdr
.callNumber
= htonl(sp
->hdr
.callNumber
);
511 whdr
.seq
= htonl(sp
->hdr
.seq
);
512 whdr
.serial
= htonl(sp
->hdr
.serial
);
513 whdr
.type
= sp
->hdr
.type
;
514 whdr
.flags
= sp
->hdr
.flags
;
515 whdr
.userStatus
= sp
->hdr
.userStatus
;
516 whdr
.securityIndex
= sp
->hdr
.securityIndex
;
517 whdr
._rsvd
= htons(sp
->hdr
._rsvd
);
518 whdr
.serviceId
= htons(sp
->hdr
.serviceId
);
520 memcpy(skb
->head
, &whdr
, sizeof(whdr
));
524 * send data through a socket
525 * - must be called in process context
526 * - caller holds the socket locked
528 static int rxrpc_send_data(struct rxrpc_sock
*rx
,
529 struct rxrpc_call
*call
,
530 struct msghdr
*msg
, size_t len
)
532 struct rxrpc_skb_priv
*sp
;
534 struct sock
*sk
= &rx
->sk
;
539 timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
541 /* this should be in poll */
542 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
544 if (sk
->sk_err
|| (sk
->sk_shutdown
& SEND_SHUTDOWN
))
547 more
= msg
->msg_flags
& MSG_MORE
;
549 skb
= call
->tx_pending
;
550 call
->tx_pending
= NULL
;
555 size_t size
, chunk
, max
, space
;
559 if (CIRC_SPACE(call
->acks_head
,
560 ACCESS_ONCE(call
->acks_tail
),
561 call
->acks_winsz
) <= 0) {
563 if (msg
->msg_flags
& MSG_DONTWAIT
)
565 ret
= rxrpc_wait_for_tx_window(rx
, call
,
571 max
= call
->conn
->params
.peer
->maxdata
;
572 max
-= call
->conn
->security_size
;
573 max
&= ~(call
->conn
->size_align
- 1UL);
576 if (chunk
> msg_data_left(msg
) && !more
)
577 chunk
= msg_data_left(msg
);
579 space
= chunk
+ call
->conn
->size_align
;
580 space
&= ~(call
->conn
->size_align
- 1UL);
582 size
= space
+ call
->conn
->header_size
;
584 _debug("SIZE: %zu/%zu/%zu", chunk
, space
, size
);
586 /* create a buffer that we can retain until it's ACK'd */
587 skb
= sock_alloc_send_skb(
588 sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
, &ret
);
594 _debug("ALLOC SEND %p", skb
);
596 ASSERTCMP(skb
->mark
, ==, 0);
598 _debug("HS: %u", call
->conn
->header_size
);
599 skb_reserve(skb
, call
->conn
->header_size
);
600 skb
->len
+= call
->conn
->header_size
;
604 if (sp
->remain
> skb_tailroom(skb
))
605 sp
->remain
= skb_tailroom(skb
);
607 _net("skb: hr %d, tr %d, hl %d, rm %d",
613 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
619 /* append next segment of data to the current buffer */
620 if (msg_data_left(msg
) > 0) {
621 int copy
= skb_tailroom(skb
);
622 ASSERTCMP(copy
, >, 0);
623 if (copy
> msg_data_left(msg
))
624 copy
= msg_data_left(msg
);
625 if (copy
> sp
->remain
)
629 ret
= skb_add_data(skb
, &msg
->msg_iter
, copy
);
638 /* check for the far side aborting the call or a network error
640 if (call
->state
> RXRPC_CALL_COMPLETE
)
643 /* add the packet to the send queue if it's now full */
644 if (sp
->remain
<= 0 ||
645 (msg_data_left(msg
) == 0 && !more
)) {
646 struct rxrpc_connection
*conn
= call
->conn
;
650 /* pad out if we're using security */
651 if (conn
->security_ix
) {
652 pad
= conn
->security_size
+ skb
->mark
;
653 pad
= conn
->size_align
- pad
;
654 pad
&= conn
->size_align
- 1;
655 _debug("pad %zu", pad
);
657 memset(skb_put(skb
, pad
), 0, pad
);
660 seq
= atomic_inc_return(&call
->sequence
);
662 sp
->hdr
.epoch
= conn
->proto
.epoch
;
663 sp
->hdr
.cid
= call
->cid
;
664 sp
->hdr
.callNumber
= call
->call_id
;
666 sp
->hdr
.serial
= atomic_inc_return(&conn
->serial
);
667 sp
->hdr
.type
= RXRPC_PACKET_TYPE_DATA
;
668 sp
->hdr
.userStatus
= 0;
669 sp
->hdr
.securityIndex
= conn
->security_ix
;
671 sp
->hdr
.serviceId
= call
->service_id
;
673 sp
->hdr
.flags
= conn
->out_clientflag
;
674 if (msg_data_left(msg
) == 0 && !more
)
675 sp
->hdr
.flags
|= RXRPC_LAST_PACKET
;
676 else if (CIRC_SPACE(call
->acks_head
,
677 ACCESS_ONCE(call
->acks_tail
),
678 call
->acks_winsz
) > 1)
679 sp
->hdr
.flags
|= RXRPC_MORE_PACKETS
;
681 sp
->hdr
.flags
|= RXRPC_REQUEST_ACK
;
683 ret
= conn
->security
->secure_packet(
684 call
, skb
, skb
->mark
,
685 skb
->head
+ sizeof(struct rxrpc_wire_header
));
689 rxrpc_insert_header(skb
);
690 rxrpc_queue_packet(call
, skb
, !msg_data_left(msg
) && !more
);
693 } while (msg_data_left(msg
) > 0);
698 call
->tx_pending
= skb
;
699 _leave(" = %d", ret
);
704 if (call
->state
== RXRPC_CALL_NETWORK_ERROR
)
705 ret
= call
->error_report
< RXRPC_LOCAL_ERROR_OFFSET
?
707 call
->error_report
- RXRPC_LOCAL_ERROR_OFFSET
;
710 _leave(" = %d", ret
);