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 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/circ_buf.h>
16 #include <net/af_rxrpc.h>
17 #include "ar-internal.h"
19 int rxrpc_resend_timeout = 4;
21 static int rxrpc_send_data(struct kiocb *iocb,
22 struct rxrpc_sock *rx,
23 struct rxrpc_call *call,
24 struct msghdr *msg, size_t len);
27 * extract control messages from the sendmsg() control buffer
29 static int rxrpc_sendmsg_cmsg(struct rxrpc_sock *rx, struct msghdr *msg,
30 unsigned long *user_call_ID,
31 enum rxrpc_command *command,
38 *command = RXRPC_CMD_SEND_DATA;
40 if (msg->msg_controllen == 0)
43 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
44 if (!CMSG_OK(msg, cmsg))
47 len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
48 _debug("CMSG %d, %d, %d",
49 cmsg->cmsg_level, cmsg->cmsg_type, len);
51 if (cmsg->cmsg_level != SOL_RXRPC)
54 switch (cmsg->cmsg_type) {
55 case RXRPC_USER_CALL_ID:
56 if (msg->msg_flags & MSG_CMSG_COMPAT) {
57 if (len != sizeof(u32))
59 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
61 if (len != sizeof(unsigned long))
63 *user_call_ID = *(unsigned long *)
66 _debug("User Call ID %lx", *user_call_ID);
70 if (*command != RXRPC_CMD_SEND_DATA)
72 *command = RXRPC_CMD_SEND_ABORT;
73 if (len != sizeof(*abort_code))
75 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
76 _debug("Abort %x", *abort_code);
82 if (*command != RXRPC_CMD_SEND_DATA)
84 *command = RXRPC_CMD_ACCEPT;
101 * abort a call, sending an ABORT packet to the peer
103 static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
105 write_lock_bh(&call->state_lock);
107 if (call->state <= RXRPC_CALL_COMPLETE) {
108 call->state = RXRPC_CALL_LOCALLY_ABORTED;
109 call->abort_code = abort_code;
110 set_bit(RXRPC_CALL_ABORT, &call->events);
111 del_timer_sync(&call->resend_timer);
112 del_timer_sync(&call->ack_timer);
113 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
114 clear_bit(RXRPC_CALL_ACK, &call->events);
115 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
116 rxrpc_queue_call(call);
119 write_unlock_bh(&call->state_lock);
123 * send a message forming part of a client call through an RxRPC socket
124 * - caller holds the socket locked
125 * - the socket may be either a client socket or a server socket
127 int rxrpc_client_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
128 struct rxrpc_transport *trans, struct msghdr *msg,
131 struct rxrpc_conn_bundle *bundle;
132 enum rxrpc_command cmd;
133 struct rxrpc_call *call;
134 unsigned long user_call_ID = 0;
142 ASSERT(trans != NULL);
144 ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
151 service_id = rx->service_id;
153 struct sockaddr_rxrpc *srx =
154 (struct sockaddr_rxrpc *) msg->msg_name;
155 service_id = htons(srx->srx_service);
158 if (key && !rx->key->payload.data)
160 bundle = rxrpc_get_bundle(rx, trans, key, service_id,
163 return PTR_ERR(bundle);
166 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID,
167 abort_code == 0, GFP_KERNEL);
169 rxrpc_put_bundle(trans, bundle);
171 _leave(" = %ld", PTR_ERR(call));
172 return PTR_ERR(call);
175 _debug("CALL %d USR %lx ST %d on CONN %p",
176 call->debug_id, call->user_call_ID, call->state, call->conn);
178 if (call->state >= RXRPC_CALL_COMPLETE) {
179 /* it's too late for this call */
181 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
182 rxrpc_send_abort(call, abort_code);
183 } else if (cmd != RXRPC_CMD_SEND_DATA) {
185 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
186 /* request phase complete for this client call */
189 ret = rxrpc_send_data(iocb, rx, call, msg, len);
192 rxrpc_put_call(call);
193 _leave(" = %d", ret);
198 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
199 * @call: The call to send data through
200 * @msg: The data to send
201 * @len: The amount of data to send
203 * Allow a kernel service to send data on a call. The call must be in an state
204 * appropriate to sending data. No control data should be supplied in @msg,
205 * nor should an address be supplied. MSG_MORE should be flagged if there's
206 * more data to come, otherwise this data will end the transmission phase.
208 int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
213 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
215 ASSERTCMP(msg->msg_name, ==, NULL);
216 ASSERTCMP(msg->msg_control, ==, NULL);
218 lock_sock(&call->socket->sk);
220 _debug("CALL %d USR %lx ST %d on CONN %p",
221 call->debug_id, call->user_call_ID, call->state, call->conn);
223 if (call->state >= RXRPC_CALL_COMPLETE) {
224 ret = -ESHUTDOWN; /* it's too late for this call */
225 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
226 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
227 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
228 ret = -EPROTO; /* request phase complete for this client call */
230 mm_segment_t oldfs = get_fs();
232 ret = rxrpc_send_data(NULL, call->socket, call, msg, len);
236 release_sock(&call->socket->sk);
237 _leave(" = %d", ret);
241 EXPORT_SYMBOL(rxrpc_kernel_send_data);
244 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
245 * @call: The call to be aborted
246 * @abort_code: The abort code to stick into the ABORT packet
248 * Allow a kernel service to abort a call, if it's still in an abortable state.
250 void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
252 _enter("{%d},%d", call->debug_id, abort_code);
254 lock_sock(&call->socket->sk);
256 _debug("CALL %d USR %lx ST %d on CONN %p",
257 call->debug_id, call->user_call_ID, call->state, call->conn);
259 if (call->state < RXRPC_CALL_COMPLETE)
260 rxrpc_send_abort(call, abort_code);
262 release_sock(&call->socket->sk);
266 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
269 * send a message through a server socket
270 * - caller holds the socket locked
272 int rxrpc_server_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
273 struct msghdr *msg, size_t len)
275 enum rxrpc_command cmd;
276 struct rxrpc_call *call;
277 unsigned long user_call_ID = 0;
283 ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
288 if (cmd == RXRPC_CMD_ACCEPT) {
289 call = rxrpc_accept_call(rx, user_call_ID);
291 return PTR_ERR(call);
292 rxrpc_put_call(call);
296 call = rxrpc_find_server_call(rx, user_call_ID);
299 if (call->state >= RXRPC_CALL_COMPLETE) {
305 case RXRPC_CMD_SEND_DATA:
306 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
307 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
308 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
309 /* Tx phase not yet begun for this call */
314 ret = rxrpc_send_data(iocb, rx, call, msg, len);
317 case RXRPC_CMD_SEND_ABORT:
318 rxrpc_send_abort(call, abort_code);
325 rxrpc_put_call(call);
326 _leave(" = %d", ret);
331 * send a packet through the transport endpoint
333 int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
339 _enter(",{%d}", skb->len);
341 iov[0].iov_base = skb->head;
342 iov[0].iov_len = skb->len;
344 msg.msg_name = &trans->peer->srx.transport.sin;
345 msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
346 msg.msg_control = NULL;
347 msg.msg_controllen = 0;
350 /* send the packet with the don't fragment bit set if we currently
351 * think it's small enough */
352 if (skb->len - sizeof(struct rxrpc_header) < trans->peer->maxdata) {
353 down_read(&trans->local->defrag_sem);
354 /* send the packet by UDP
355 * - returns -EMSGSIZE if UDP would have to fragment the packet
356 * to go out of the interface
357 * - in which case, we'll have processed the ICMP error
358 * message and update the peer record
360 ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
363 up_read(&trans->local->defrag_sem);
364 if (ret == -EMSGSIZE)
365 goto send_fragmentable;
367 _leave(" = %d [%u]", ret, trans->peer->maxdata);
372 /* attempt to send this message with fragmentation enabled */
373 _debug("send fragment");
375 down_write(&trans->local->defrag_sem);
376 opt = IP_PMTUDISC_DONT;
377 ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
378 (char *) &opt, sizeof(opt));
380 ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
383 opt = IP_PMTUDISC_DO;
384 kernel_setsockopt(trans->local->socket, SOL_IP,
385 IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
388 up_write(&trans->local->defrag_sem);
389 _leave(" = %d [frag %u]", ret, trans->peer->maxdata);
394 * wait for space to appear in the transmit/ACK window
395 * - caller holds the socket locked
397 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
398 struct rxrpc_call *call,
401 DECLARE_WAITQUEUE(myself, current);
405 CIRC_SPACE(call->acks_head, call->acks_tail, call->acks_winsz),
408 add_wait_queue(&call->tx_waitq, &myself);
411 set_current_state(TASK_INTERRUPTIBLE);
413 if (CIRC_SPACE(call->acks_head, call->acks_tail,
414 call->acks_winsz) > 0)
416 if (signal_pending(current)) {
417 ret = sock_intr_errno(*timeo);
421 release_sock(&rx->sk);
422 *timeo = schedule_timeout(*timeo);
426 remove_wait_queue(&call->tx_waitq, &myself);
427 set_current_state(TASK_RUNNING);
428 _leave(" = %d", ret);
433 * attempt to schedule an instant Tx resend
435 static inline void rxrpc_instant_resend(struct rxrpc_call *call)
437 read_lock_bh(&call->state_lock);
438 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
439 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
440 if (call->state < RXRPC_CALL_COMPLETE &&
441 !test_and_set_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
442 rxrpc_queue_call(call);
444 read_unlock_bh(&call->state_lock);
448 * queue a packet for transmission, set the resend timer and attempt
449 * to send the packet immediately
451 static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
454 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
457 _net("queue skb %p [%d]", skb, call->acks_head);
459 ASSERT(call->acks_window != NULL);
460 call->acks_window[call->acks_head] = (unsigned long) skb;
462 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
464 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
465 _debug("________awaiting reply/ACK__________");
466 write_lock_bh(&call->state_lock);
467 switch (call->state) {
468 case RXRPC_CALL_CLIENT_SEND_REQUEST:
469 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
471 case RXRPC_CALL_SERVER_ACK_REQUEST:
472 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
475 case RXRPC_CALL_SERVER_SEND_REPLY:
476 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
481 write_unlock_bh(&call->state_lock);
484 _proto("Tx DATA %%%u { #%u }",
485 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
488 sp->resend_at = jiffies + rxrpc_resend_timeout * HZ;
489 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
491 call->resend_timer.expires = sp->resend_at;
492 add_timer(&call->resend_timer);
495 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
496 * we're ACK'ing the request phase of an incoming call */
498 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
499 /* the packet may be freed by rxrpc_process_call() before this
501 ret = rxrpc_send_packet(call->conn->trans, skb);
502 _net("sent skb %p", skb);
504 _debug("failed to delete ACK timer");
508 _debug("need instant resend %d", ret);
510 rxrpc_instant_resend(call);
517 * send data through a socket
518 * - must be called in process context
519 * - caller holds the socket locked
521 static int rxrpc_send_data(struct kiocb *iocb,
522 struct rxrpc_sock *rx,
523 struct rxrpc_call *call,
524 struct msghdr *msg, size_t len)
526 struct rxrpc_skb_priv *sp;
527 unsigned char __user *from;
530 struct sock *sk = &rx->sk;
533 int ret, ioc, segment, copied;
535 _enter(",,,{%zu},%zu", msg->msg_iovlen, len);
537 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
539 /* this should be in poll */
540 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
542 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
546 ioc = msg->msg_iovlen - 1;
547 from = iov->iov_base;
548 segment = iov->iov_len;
550 more = msg->msg_flags & MSG_MORE;
552 skb = call->tx_pending;
553 call->tx_pending = NULL;
562 _debug("SEGMENT %d @%p", segment, from);
565 size_t size, chunk, max, space;
569 if (CIRC_SPACE(call->acks_head, call->acks_tail,
570 call->acks_winsz) <= 0) {
572 if (msg->msg_flags & MSG_DONTWAIT)
574 ret = rxrpc_wait_for_tx_window(rx, call,
580 max = call->conn->trans->peer->maxdata;
581 max -= call->conn->security_size;
582 max &= ~(call->conn->size_align - 1UL);
585 if (chunk > len && !more)
588 space = chunk + call->conn->size_align;
589 space &= ~(call->conn->size_align - 1UL);
591 size = space + call->conn->header_size;
593 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
595 /* create a buffer that we can retain until it's ACK'd */
596 skb = sock_alloc_send_skb(
597 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
603 _debug("ALLOC SEND %p", skb);
605 ASSERTCMP(skb->mark, ==, 0);
607 _debug("HS: %u", call->conn->header_size);
608 skb_reserve(skb, call->conn->header_size);
609 skb->len += call->conn->header_size;
613 if (sp->remain > skb_tailroom(skb))
614 sp->remain = skb_tailroom(skb);
616 _net("skb: hr %d, tr %d, hl %d, rm %d",
622 skb->ip_summed = CHECKSUM_UNNECESSARY;
628 /* append next segment of data to the current buffer */
629 copy = skb_tailroom(skb);
630 ASSERTCMP(copy, >, 0);
633 if (copy > sp->remain)
637 ret = skb_add_data(skb, from, copy);
648 while (segment == 0 && ioc > 0) {
649 from = iov->iov_base;
650 segment = iov->iov_len;
659 /* check for the far side aborting the call or a network error
661 if (call->state > RXRPC_CALL_COMPLETE)
664 /* add the packet to the send queue if it's now full */
665 if (sp->remain <= 0 || (segment == 0 && !more)) {
666 struct rxrpc_connection *conn = call->conn;
669 /* pad out if we're using security */
670 if (conn->security) {
671 pad = conn->security_size + skb->mark;
672 pad = conn->size_align - pad;
673 pad &= conn->size_align - 1;
674 _debug("pad %zu", pad);
676 memset(skb_put(skb, pad), 0, pad);
679 sp->hdr.epoch = conn->epoch;
680 sp->hdr.cid = call->cid;
681 sp->hdr.callNumber = call->call_id;
683 htonl(atomic_inc_return(&call->sequence));
685 htonl(atomic_inc_return(&conn->serial));
686 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
687 sp->hdr.userStatus = 0;
688 sp->hdr.securityIndex = conn->security_ix;
690 sp->hdr.serviceId = conn->service_id;
692 sp->hdr.flags = conn->out_clientflag;
693 if (len == 0 && !more)
694 sp->hdr.flags |= RXRPC_LAST_PACKET;
695 else if (CIRC_SPACE(call->acks_head, call->acks_tail,
696 call->acks_winsz) > 1)
697 sp->hdr.flags |= RXRPC_MORE_PACKETS;
699 ret = rxrpc_secure_packet(
700 call, skb, skb->mark,
701 skb->head + sizeof(struct rxrpc_header));
705 memcpy(skb->head, &sp->hdr,
706 sizeof(struct rxrpc_header));
707 rxrpc_queue_packet(call, skb, segment == 0 && !more);
711 } while (segment > 0);
716 call->tx_pending = skb;
717 _leave(" = %d", ret);
722 if (call->state == RXRPC_CALL_NETWORK_ERROR)
723 ret = call->conn->trans->peer->net_error;
726 _leave(" = %d", ret);