1 /* RxRPC packet transmission
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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/gfp.h>
14 #include <linux/skbuff.h>
15 #include <linux/circ_buf.h>
16 #include <linux/export.h>
18 #include <net/af_rxrpc.h>
19 #include "ar-internal.h"
22 * Time till packet resend (in jiffies).
24 unsigned int rxrpc_resend_timeout = 4 * HZ;
26 static int rxrpc_send_data(struct rxrpc_sock *rx,
27 struct rxrpc_call *call,
28 struct msghdr *msg, size_t len);
31 * extract control messages from the sendmsg() control buffer
33 static int rxrpc_sendmsg_cmsg(struct rxrpc_sock *rx, struct msghdr *msg,
34 unsigned long *user_call_ID,
35 enum rxrpc_command *command,
42 *command = RXRPC_CMD_SEND_DATA;
44 if (msg->msg_controllen == 0)
47 for_each_cmsghdr(cmsg, msg) {
48 if (!CMSG_OK(msg, cmsg))
51 len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
52 _debug("CMSG %d, %d, %d",
53 cmsg->cmsg_level, cmsg->cmsg_type, len);
55 if (cmsg->cmsg_level != SOL_RXRPC)
58 switch (cmsg->cmsg_type) {
59 case RXRPC_USER_CALL_ID:
60 if (msg->msg_flags & MSG_CMSG_COMPAT) {
61 if (len != sizeof(u32))
63 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
65 if (len != sizeof(unsigned long))
67 *user_call_ID = *(unsigned long *)
70 _debug("User Call ID %lx", *user_call_ID);
74 if (*command != RXRPC_CMD_SEND_DATA)
76 *command = RXRPC_CMD_SEND_ABORT;
77 if (len != sizeof(*abort_code))
79 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
80 _debug("Abort %x", *abort_code);
86 if (*command != RXRPC_CMD_SEND_DATA)
88 *command = RXRPC_CMD_ACCEPT;
105 * abort a call, sending an ABORT packet to the peer
107 static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
109 write_lock_bh(&call->state_lock);
111 if (call->state <= RXRPC_CALL_COMPLETE) {
112 call->state = RXRPC_CALL_LOCALLY_ABORTED;
113 call->abort_code = abort_code;
114 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
115 del_timer_sync(&call->resend_timer);
116 del_timer_sync(&call->ack_timer);
117 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
118 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
119 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
120 rxrpc_queue_call(call);
123 write_unlock_bh(&call->state_lock);
127 * send a message forming part of a client call through an RxRPC socket
128 * - caller holds the socket locked
129 * - the socket may be either a client socket or a server socket
131 int rxrpc_client_sendmsg(struct rxrpc_sock *rx, struct rxrpc_transport *trans,
132 struct msghdr *msg, size_t len)
134 struct rxrpc_conn_bundle *bundle;
135 enum rxrpc_command cmd;
136 struct rxrpc_call *call;
137 unsigned long user_call_ID = 0;
145 ASSERT(trans != NULL);
147 ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
154 service_id = rx->srx.srx_service;
156 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx,
158 service_id = srx->srx_service;
161 if (key && !rx->key->payload.data[0])
163 bundle = rxrpc_get_bundle(rx, trans, key, service_id,
166 return PTR_ERR(bundle);
169 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID,
170 abort_code == 0, GFP_KERNEL);
172 rxrpc_put_bundle(trans, bundle);
174 _leave(" = %ld", PTR_ERR(call));
175 return PTR_ERR(call);
178 _debug("CALL %d USR %lx ST %d on CONN %p",
179 call->debug_id, call->user_call_ID, call->state, call->conn);
181 if (call->state >= RXRPC_CALL_COMPLETE) {
182 /* it's too late for this call */
184 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
185 rxrpc_send_abort(call, abort_code);
186 } else if (cmd != RXRPC_CMD_SEND_DATA) {
188 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
189 /* request phase complete for this client call */
192 ret = rxrpc_send_data(rx, call, msg, len);
195 rxrpc_put_call(call);
196 _leave(" = %d", ret);
201 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
202 * @call: The call to send data through
203 * @msg: The data to send
204 * @len: The amount of data to send
206 * Allow a kernel service to send data on a call. The call must be in an state
207 * appropriate to sending data. No control data should be supplied in @msg,
208 * nor should an address be supplied. MSG_MORE should be flagged if there's
209 * more data to come, otherwise this data will end the transmission phase.
211 int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
216 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
218 ASSERTCMP(msg->msg_name, ==, NULL);
219 ASSERTCMP(msg->msg_control, ==, NULL);
221 lock_sock(&call->socket->sk);
223 _debug("CALL %d USR %lx ST %d on CONN %p",
224 call->debug_id, call->user_call_ID, call->state, call->conn);
226 if (call->state >= RXRPC_CALL_COMPLETE) {
227 ret = -ESHUTDOWN; /* it's too late for this call */
228 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
229 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
230 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
231 ret = -EPROTO; /* request phase complete for this client call */
233 ret = rxrpc_send_data(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 rxrpc_sock *rx, struct msghdr *msg, size_t len)
274 enum rxrpc_command cmd;
275 struct rxrpc_call *call;
276 unsigned long user_call_ID = 0;
282 ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
287 if (cmd == RXRPC_CMD_ACCEPT) {
288 call = rxrpc_accept_call(rx, user_call_ID);
290 return PTR_ERR(call);
291 rxrpc_put_call(call);
295 call = rxrpc_find_server_call(rx, user_call_ID);
298 if (call->state >= RXRPC_CALL_COMPLETE) {
304 case RXRPC_CMD_SEND_DATA:
305 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
306 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
307 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
308 /* Tx phase not yet begun for this call */
313 ret = rxrpc_send_data(rx, call, msg, len);
316 case RXRPC_CMD_SEND_ABORT:
317 rxrpc_send_abort(call, abort_code);
324 rxrpc_put_call(call);
325 _leave(" = %d", ret);
330 * send a packet through the transport endpoint
332 int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
338 _enter(",{%d}", skb->len);
340 iov[0].iov_base = skb->head;
341 iov[0].iov_len = skb->len;
343 msg.msg_name = &trans->peer->srx.transport.sin;
344 msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
345 msg.msg_control = NULL;
346 msg.msg_controllen = 0;
349 /* send the packet with the don't fragment bit set if we currently
350 * think it's small enough */
351 if (skb->len - sizeof(struct rxrpc_wire_header) < trans->peer->maxdata) {
352 down_read(&trans->local->defrag_sem);
353 /* send the packet by UDP
354 * - returns -EMSGSIZE if UDP would have to fragment the packet
355 * to go out of the interface
356 * - in which case, we'll have processed the ICMP error
357 * message and update the peer record
359 ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
362 up_read(&trans->local->defrag_sem);
363 if (ret == -EMSGSIZE)
364 goto send_fragmentable;
366 _leave(" = %d [%u]", ret, trans->peer->maxdata);
371 /* attempt to send this message with fragmentation enabled */
372 _debug("send fragment");
374 down_write(&trans->local->defrag_sem);
375 opt = IP_PMTUDISC_DONT;
376 ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
377 (char *) &opt, sizeof(opt));
379 ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
382 opt = IP_PMTUDISC_DO;
383 kernel_setsockopt(trans->local->socket, SOL_IP,
384 IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
387 up_write(&trans->local->defrag_sem);
388 _leave(" = %d [frag %u]", ret, trans->peer->maxdata);
393 * wait for space to appear in the transmit/ACK window
394 * - caller holds the socket locked
396 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
397 struct rxrpc_call *call,
400 DECLARE_WAITQUEUE(myself, current);
404 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
408 add_wait_queue(&call->tx_waitq, &myself);
411 set_current_state(TASK_INTERRUPTIBLE);
413 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(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_EV_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 }", sp->hdr.serial, sp->hdr.seq);
486 sp->need_resend = false;
487 sp->resend_at = jiffies + rxrpc_resend_timeout;
488 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
490 call->resend_timer.expires = sp->resend_at;
491 add_timer(&call->resend_timer);
494 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
495 * we're ACK'ing the request phase of an incoming call */
497 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
498 /* the packet may be freed by rxrpc_process_call() before this
500 ret = rxrpc_send_packet(call->conn->trans, skb);
501 _net("sent skb %p", skb);
503 _debug("failed to delete ACK timer");
507 _debug("need instant resend %d", ret);
508 sp->need_resend = true;
509 rxrpc_instant_resend(call);
516 * Convert a host-endian header into a network-endian header.
518 static void rxrpc_insert_header(struct sk_buff *skb)
520 struct rxrpc_wire_header whdr;
521 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
523 whdr.epoch = htonl(sp->hdr.epoch);
524 whdr.cid = htonl(sp->hdr.cid);
525 whdr.callNumber = htonl(sp->hdr.callNumber);
526 whdr.seq = htonl(sp->hdr.seq);
527 whdr.serial = htonl(sp->hdr.serial);
528 whdr.type = sp->hdr.type;
529 whdr.flags = sp->hdr.flags;
530 whdr.userStatus = sp->hdr.userStatus;
531 whdr.securityIndex = sp->hdr.securityIndex;
532 whdr._rsvd = htons(sp->hdr._rsvd);
533 whdr.serviceId = htons(sp->hdr.serviceId);
535 memcpy(skb->head, &whdr, sizeof(whdr));
539 * send data through a socket
540 * - must be called in process context
541 * - caller holds the socket locked
543 static int rxrpc_send_data(struct rxrpc_sock *rx,
544 struct rxrpc_call *call,
545 struct msghdr *msg, size_t len)
547 struct rxrpc_skb_priv *sp;
549 struct sock *sk = &rx->sk;
554 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
556 /* this should be in poll */
557 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
559 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
562 more = msg->msg_flags & MSG_MORE;
564 skb = call->tx_pending;
565 call->tx_pending = NULL;
570 size_t size, chunk, max, space;
574 if (CIRC_SPACE(call->acks_head,
575 ACCESS_ONCE(call->acks_tail),
576 call->acks_winsz) <= 0) {
578 if (msg->msg_flags & MSG_DONTWAIT)
580 ret = rxrpc_wait_for_tx_window(rx, call,
586 max = call->conn->trans->peer->maxdata;
587 max -= call->conn->security_size;
588 max &= ~(call->conn->size_align - 1UL);
591 if (chunk > msg_data_left(msg) && !more)
592 chunk = msg_data_left(msg);
594 space = chunk + call->conn->size_align;
595 space &= ~(call->conn->size_align - 1UL);
597 size = space + call->conn->header_size;
599 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
601 /* create a buffer that we can retain until it's ACK'd */
602 skb = sock_alloc_send_skb(
603 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
609 _debug("ALLOC SEND %p", skb);
611 ASSERTCMP(skb->mark, ==, 0);
613 _debug("HS: %u", call->conn->header_size);
614 skb_reserve(skb, call->conn->header_size);
615 skb->len += call->conn->header_size;
619 if (sp->remain > skb_tailroom(skb))
620 sp->remain = skb_tailroom(skb);
622 _net("skb: hr %d, tr %d, hl %d, rm %d",
628 skb->ip_summed = CHECKSUM_UNNECESSARY;
634 /* append next segment of data to the current buffer */
635 if (msg_data_left(msg) > 0) {
636 int copy = skb_tailroom(skb);
637 ASSERTCMP(copy, >, 0);
638 if (copy > msg_data_left(msg))
639 copy = msg_data_left(msg);
640 if (copy > sp->remain)
644 ret = skb_add_data(skb, &msg->msg_iter, copy);
653 /* check for the far side aborting the call or a network error
655 if (call->state > RXRPC_CALL_COMPLETE)
658 /* add the packet to the send queue if it's now full */
659 if (sp->remain <= 0 ||
660 (msg_data_left(msg) == 0 && !more)) {
661 struct rxrpc_connection *conn = call->conn;
665 /* pad out if we're using security */
666 if (conn->security) {
667 pad = conn->security_size + skb->mark;
668 pad = conn->size_align - pad;
669 pad &= conn->size_align - 1;
670 _debug("pad %zu", pad);
672 memset(skb_put(skb, pad), 0, pad);
675 seq = atomic_inc_return(&call->sequence);
677 sp->hdr.epoch = conn->epoch;
678 sp->hdr.cid = call->cid;
679 sp->hdr.callNumber = call->call_id;
681 sp->hdr.serial = atomic_inc_return(&conn->serial);
682 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
683 sp->hdr.userStatus = 0;
684 sp->hdr.securityIndex = conn->security_ix;
686 sp->hdr.serviceId = call->service_id;
688 sp->hdr.flags = conn->out_clientflag;
689 if (msg_data_left(msg) == 0 && !more)
690 sp->hdr.flags |= RXRPC_LAST_PACKET;
691 else if (CIRC_SPACE(call->acks_head,
692 ACCESS_ONCE(call->acks_tail),
693 call->acks_winsz) > 1)
694 sp->hdr.flags |= RXRPC_MORE_PACKETS;
696 sp->hdr.flags |= RXRPC_REQUEST_ACK;
698 ret = rxrpc_secure_packet(
699 call, skb, skb->mark,
700 skb->head + sizeof(struct rxrpc_wire_header));
704 rxrpc_insert_header(skb);
705 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
708 } while (msg_data_left(msg) > 0);
713 call->tx_pending = skb;
714 _leave(" = %d", ret);
719 if (call->state == RXRPC_CALL_NETWORK_ERROR)
720 ret = call->conn->trans->peer->net_error;
723 _leave(" = %d", ret);