output.c 18.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* RxRPC packet transmission
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14
#include <linux/net.h>
15
#include <linux/gfp.h>
16 17
#include <linux/skbuff.h>
#include <linux/circ_buf.h>
18
#include <linux/export.h>
19 20 21 22
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include "ar-internal.h"

23 24 25
/*
 * Time till packet resend (in jiffies).
 */
26
unsigned int rxrpc_resend_timeout = 4 * HZ;
27

28
static int rxrpc_send_data(struct rxrpc_sock *rx,
29 30 31 32 33 34
			   struct rxrpc_call *call,
			   struct msghdr *msg, size_t len);

/*
 * extract control messages from the sendmsg() control buffer
 */
35
static int rxrpc_sendmsg_cmsg(struct msghdr *msg,
36 37
			      unsigned long *user_call_ID,
			      enum rxrpc_command *command,
38 39
			      u32 *abort_code,
			      bool *_exclusive)
40 41
{
	struct cmsghdr *cmsg;
42
	bool got_user_ID = false;
43 44 45 46 47 48 49
	int len;

	*command = RXRPC_CMD_SEND_DATA;

	if (msg->msg_controllen == 0)
		return -EINVAL;

50
	for_each_cmsghdr(cmsg, msg) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		if (!CMSG_OK(msg, cmsg))
			return -EINVAL;

		len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
		_debug("CMSG %d, %d, %d",
		       cmsg->cmsg_level, cmsg->cmsg_type, len);

		if (cmsg->cmsg_level != SOL_RXRPC)
			continue;

		switch (cmsg->cmsg_type) {
		case RXRPC_USER_CALL_ID:
			if (msg->msg_flags & MSG_CMSG_COMPAT) {
				if (len != sizeof(u32))
					return -EINVAL;
				*user_call_ID = *(u32 *) CMSG_DATA(cmsg);
			} else {
				if (len != sizeof(unsigned long))
					return -EINVAL;
				*user_call_ID = *(unsigned long *)
					CMSG_DATA(cmsg);
			}
			_debug("User Call ID %lx", *user_call_ID);
74
			got_user_ID = true;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			break;

		case RXRPC_ABORT:
			if (*command != RXRPC_CMD_SEND_DATA)
				return -EINVAL;
			*command = RXRPC_CMD_SEND_ABORT;
			if (len != sizeof(*abort_code))
				return -EINVAL;
			*abort_code = *(unsigned int *) CMSG_DATA(cmsg);
			_debug("Abort %x", *abort_code);
			if (*abort_code == 0)
				return -EINVAL;
			break;

		case RXRPC_ACCEPT:
			if (*command != RXRPC_CMD_SEND_DATA)
				return -EINVAL;
			*command = RXRPC_CMD_ACCEPT;
			if (len != 0)
				return -EINVAL;
			break;

97 98 99 100 101
		case RXRPC_EXCLUSIVE_CALL:
			*_exclusive = true;
			if (len != 0)
				return -EINVAL;
			break;
102 103 104 105 106
		default:
			return -EINVAL;
		}
	}

107 108
	if (!got_user_ID)
		return -EINVAL;
109 110 111 112 113 114 115 116 117 118 119 120 121
	_leave(" = 0");
	return 0;
}

/*
 * abort a call, sending an ABORT packet to the peer
 */
static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
{
	write_lock_bh(&call->state_lock);

	if (call->state <= RXRPC_CALL_COMPLETE) {
		call->state = RXRPC_CALL_LOCALLY_ABORTED;
122
		call->local_abort = abort_code;
123
		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
124 125
		del_timer_sync(&call->resend_timer);
		del_timer_sync(&call->ack_timer);
126 127
		clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
		clear_bit(RXRPC_CALL_EV_ACK, &call->events);
128
		clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
129
		rxrpc_queue_call(call);
130 131 132 133 134
	}

	write_unlock_bh(&call->state_lock);
}

135 136 137 138 139
/*
 * Create a new client call for sendmsg().
 */
static struct rxrpc_call *
rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
140
				  unsigned long user_call_ID, bool exclusive)
141
{
142
	struct rxrpc_conn_parameters cp;
143 144 145 146 147 148 149 150 151 152 153 154 155
	struct rxrpc_conn_bundle *bundle;
	struct rxrpc_transport *trans;
	struct rxrpc_call *call;
	struct key *key;
	long ret;

	DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);

	_enter("");

	if (!msg->msg_name)
		return ERR_PTR(-EDESTADDRREQ);

156 157 158 159 160 161 162 163
	key = rx->key;
	if (key && !rx->key->payload.data[0])
		key = NULL;

	memset(&cp, 0, sizeof(cp));
	cp.local		= rx->local;
	cp.key			= rx->key;
	cp.security_level	= rx->min_sec_level;
164
	cp.exclusive		= rx->exclusive | exclusive;
165 166
	cp.service_id		= srx->srx_service;
	trans = rxrpc_name_to_transport(&cp, msg->msg_name, msg->msg_namelen,
167 168 169 170 171
					GFP_KERNEL);
	if (IS_ERR(trans)) {
		ret = PTR_ERR(trans);
		goto out;
	}
172
	cp.peer = trans->peer;
173

174 175
	bundle = rxrpc_get_bundle(rx, trans, cp.key, srx->srx_service,
				  GFP_KERNEL);
176 177 178 179 180
	if (IS_ERR(bundle)) {
		ret = PTR_ERR(bundle);
		goto out_trans;
	}

181
	call = rxrpc_new_client_call(rx, &cp, trans, bundle, user_call_ID,
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
				     GFP_KERNEL);
	rxrpc_put_bundle(trans, bundle);
	rxrpc_put_transport(trans);
	if (IS_ERR(call)) {
		ret = PTR_ERR(call);
		goto out_trans;
	}

	_leave(" = %p\n", call);
	return call;

out_trans:
	rxrpc_put_transport(trans);
out:
	_leave(" = %ld", ret);
	return ERR_PTR(ret);
}

200 201 202 203 204
/*
 * send a message forming part of a client call through an RxRPC socket
 * - caller holds the socket locked
 * - the socket may be either a client socket or a server socket
 */
205
int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
206 207 208 209
{
	enum rxrpc_command cmd;
	struct rxrpc_call *call;
	unsigned long user_call_ID = 0;
210
	bool exclusive = false;
211 212 213 214 215
	u32 abort_code = 0;
	int ret;

	_enter("");

216 217
	ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
				 &exclusive);
218 219 220
	if (ret < 0)
		return ret;

221 222 223 224 225 226 227 228
	if (cmd == RXRPC_CMD_ACCEPT) {
		if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
			return -EINVAL;
		call = rxrpc_accept_call(rx, user_call_ID);
		if (IS_ERR(call))
			return PTR_ERR(call);
		rxrpc_put_call(call);
		return 0;
229 230
	}

231 232 233 234
	call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
	if (!call) {
		if (cmd != RXRPC_CMD_SEND_DATA)
			return -EBADSLT;
235 236
		call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
							 exclusive);
237 238
		if (IS_ERR(call))
			return PTR_ERR(call);
239 240 241 242 243 244 245
	}

	_debug("CALL %d USR %lx ST %d on CONN %p",
	       call->debug_id, call->user_call_ID, call->state, call->conn);

	if (call->state >= RXRPC_CALL_COMPLETE) {
		/* it's too late for this call */
246
		ret = -ECONNRESET;
247 248
	} else if (cmd == RXRPC_CMD_SEND_ABORT) {
		rxrpc_send_abort(call, abort_code);
249
		ret = 0;
250 251
	} else if (cmd != RXRPC_CMD_SEND_DATA) {
		ret = -EINVAL;
252 253
	} else if (!call->in_clientflag &&
		   call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
254 255
		/* request phase complete for this client call */
		ret = -EPROTO;
256 257 258 259 260
	} else if (call->in_clientflag &&
		   call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
		   call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
		/* Reply phase not begun or not complete for service call. */
		ret = -EPROTO;
261
	} else {
262
		ret = rxrpc_send_data(rx, call, msg, len);
263 264 265 266 267 268 269
	}

	rxrpc_put_call(call);
	_leave(" = %d", ret);
	return ret;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/**
 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
 * @call: The call to send data through
 * @msg: The data to send
 * @len: The amount of data to send
 *
 * Allow a kernel service to send data on a call.  The call must be in an state
 * appropriate to sending data.  No control data should be supplied in @msg,
 * nor should an address be supplied.  MSG_MORE should be flagged if there's
 * more data to come, otherwise this data will end the transmission phase.
 */
int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
			   size_t len)
{
	int ret;

	_enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);

	ASSERTCMP(msg->msg_name, ==, NULL);
	ASSERTCMP(msg->msg_control, ==, NULL);

	lock_sock(&call->socket->sk);

	_debug("CALL %d USR %lx ST %d on CONN %p",
	       call->debug_id, call->user_call_ID, call->state, call->conn);

	if (call->state >= RXRPC_CALL_COMPLETE) {
		ret = -ESHUTDOWN; /* it's too late for this call */
	} else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
		   call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
		   call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
		ret = -EPROTO; /* request phase complete for this client call */
	} else {
303
		ret = rxrpc_send_data(call->socket, call, msg, len);
304 305 306 307 308 309 310 311 312
	}

	release_sock(&call->socket->sk);
	_leave(" = %d", ret);
	return ret;
}

EXPORT_SYMBOL(rxrpc_kernel_send_data);

313
/**
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
 * @call: The call to be aborted
 * @abort_code: The abort code to stick into the ABORT packet
 *
 * Allow a kernel service to abort a call, if it's still in an abortable state.
 */
void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
{
	_enter("{%d},%d", call->debug_id, abort_code);

	lock_sock(&call->socket->sk);

	_debug("CALL %d USR %lx ST %d on CONN %p",
	       call->debug_id, call->user_call_ID, call->state, call->conn);

	if (call->state < RXRPC_CALL_COMPLETE)
		rxrpc_send_abort(call, abort_code);

	release_sock(&call->socket->sk);
	_leave("");
}

EXPORT_SYMBOL(rxrpc_kernel_abort_call);

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/*
 * send a packet through the transport endpoint
 */
int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
{
	struct kvec iov[1];
	struct msghdr msg;
	int ret, opt;

	_enter(",{%d}", skb->len);

	iov[0].iov_base = skb->head;
	iov[0].iov_len = skb->len;

	msg.msg_name = &trans->peer->srx.transport.sin;
	msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = 0;

	/* send the packet with the don't fragment bit set if we currently
	 * think it's small enough */
360
	if (skb->len - sizeof(struct rxrpc_wire_header) < trans->peer->maxdata) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		down_read(&trans->local->defrag_sem);
		/* send the packet by UDP
		 * - returns -EMSGSIZE if UDP would have to fragment the packet
		 *   to go out of the interface
		 *   - in which case, we'll have processed the ICMP error
		 *     message and update the peer record
		 */
		ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
				     iov[0].iov_len);

		up_read(&trans->local->defrag_sem);
		if (ret == -EMSGSIZE)
			goto send_fragmentable;

		_leave(" = %d [%u]", ret, trans->peer->maxdata);
		return ret;
	}

send_fragmentable:
	/* attempt to send this message with fragmentation enabled */
	_debug("send fragment");

	down_write(&trans->local->defrag_sem);
	opt = IP_PMTUDISC_DONT;
	ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
				(char *) &opt, sizeof(opt));
	if (ret == 0) {
		ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
				     iov[0].iov_len);

		opt = IP_PMTUDISC_DO;
		kernel_setsockopt(trans->local->socket, SOL_IP,
				  IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
	}

	up_write(&trans->local->defrag_sem);
	_leave(" = %d [frag %u]", ret, trans->peer->maxdata);
	return ret;
}

/*
 * wait for space to appear in the transmit/ACK window
 * - caller holds the socket locked
 */
static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
				    struct rxrpc_call *call,
				    long *timeo)
{
	DECLARE_WAITQUEUE(myself, current);
	int ret;

	_enter(",{%d},%ld",
413 414
	       CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
			  call->acks_winsz),
415 416 417 418 419 420 421
	       *timeo);

	add_wait_queue(&call->tx_waitq, &myself);

	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		ret = 0;
422
		if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
			       call->acks_winsz) > 0)
			break;
		if (signal_pending(current)) {
			ret = sock_intr_errno(*timeo);
			break;
		}

		release_sock(&rx->sk);
		*timeo = schedule_timeout(*timeo);
		lock_sock(&rx->sk);
	}

	remove_wait_queue(&call->tx_waitq, &myself);
	set_current_state(TASK_RUNNING);
	_leave(" = %d", ret);
	return ret;
}

/*
 * attempt to schedule an instant Tx resend
 */
static inline void rxrpc_instant_resend(struct rxrpc_call *call)
{
	read_lock_bh(&call->state_lock);
	if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
		clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
		if (call->state < RXRPC_CALL_COMPLETE &&
450
		    !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
451
			rxrpc_queue_call(call);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	}
	read_unlock_bh(&call->state_lock);
}

/*
 * queue a packet for transmission, set the resend timer and attempt
 * to send the packet immediately
 */
static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
			       bool last)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	int ret;

	_net("queue skb %p [%d]", skb, call->acks_head);

	ASSERT(call->acks_window != NULL);
	call->acks_window[call->acks_head] = (unsigned long) skb;
	smp_wmb();
	call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);

	if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
		_debug("________awaiting reply/ACK__________");
		write_lock_bh(&call->state_lock);
		switch (call->state) {
		case RXRPC_CALL_CLIENT_SEND_REQUEST:
			call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
			break;
		case RXRPC_CALL_SERVER_ACK_REQUEST:
			call->state = RXRPC_CALL_SERVER_SEND_REPLY;
			if (!last)
				break;
		case RXRPC_CALL_SERVER_SEND_REPLY:
			call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
			break;
		default:
			break;
		}
		write_unlock_bh(&call->state_lock);
	}

493
	_proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
494

495
	sp->need_resend = false;
496
	sp->resend_at = jiffies + rxrpc_resend_timeout;
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
		_debug("run timer");
		call->resend_timer.expires = sp->resend_at;
		add_timer(&call->resend_timer);
	}

	/* attempt to cancel the rx-ACK timer, deferring reply transmission if
	 * we're ACK'ing the request phase of an incoming call */
	ret = -EAGAIN;
	if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
		/* the packet may be freed by rxrpc_process_call() before this
		 * returns */
		ret = rxrpc_send_packet(call->conn->trans, skb);
		_net("sent skb %p", skb);
	} else {
		_debug("failed to delete ACK timer");
	}

	if (ret < 0) {
		_debug("need instant resend %d", ret);
517
		sp->need_resend = true;
518 519 520 521 522 523
		rxrpc_instant_resend(call);
	}

	_leave("");
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
/*
 * Convert a host-endian header into a network-endian header.
 */
static void rxrpc_insert_header(struct sk_buff *skb)
{
	struct rxrpc_wire_header whdr;
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);

	whdr.epoch	= htonl(sp->hdr.epoch);
	whdr.cid	= htonl(sp->hdr.cid);
	whdr.callNumber	= htonl(sp->hdr.callNumber);
	whdr.seq	= htonl(sp->hdr.seq);
	whdr.serial	= htonl(sp->hdr.serial);
	whdr.type	= sp->hdr.type;
	whdr.flags	= sp->hdr.flags;
	whdr.userStatus	= sp->hdr.userStatus;
	whdr.securityIndex = sp->hdr.securityIndex;
	whdr._rsvd	= htons(sp->hdr._rsvd);
	whdr.serviceId	= htons(sp->hdr.serviceId);

	memcpy(skb->head, &whdr, sizeof(whdr));
}

547 548 549 550 551
/*
 * send data through a socket
 * - must be called in process context
 * - caller holds the socket locked
 */
552
static int rxrpc_send_data(struct rxrpc_sock *rx,
553 554 555 556 557 558 559 560
			   struct rxrpc_call *call,
			   struct msghdr *msg, size_t len)
{
	struct rxrpc_skb_priv *sp;
	struct sk_buff *skb;
	struct sock *sk = &rx->sk;
	long timeo;
	bool more;
561
	int ret, copied;
562 563 564 565

	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);

	/* this should be in poll */
566
	sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
567 568 569 570 571 572 573 574 575 576

	if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
		return -EPIPE;

	more = msg->msg_flags & MSG_MORE;

	skb = call->tx_pending;
	call->tx_pending = NULL;

	copied = 0;
577
	do {
578 579 580 581 582
		if (!skb) {
			size_t size, chunk, max, space;

			_debug("alloc");

583 584
			if (CIRC_SPACE(call->acks_head,
				       ACCESS_ONCE(call->acks_tail),
585 586 587 588 589 590 591 592 593 594
				       call->acks_winsz) <= 0) {
				ret = -EAGAIN;
				if (msg->msg_flags & MSG_DONTWAIT)
					goto maybe_error;
				ret = rxrpc_wait_for_tx_window(rx, call,
							       &timeo);
				if (ret < 0)
					goto maybe_error;
			}

595
			max = call->conn->params.peer->maxdata;
596 597 598 599
			max -= call->conn->security_size;
			max &= ~(call->conn->size_align - 1UL);

			chunk = max;
A
Al Viro 已提交
600 601
			if (chunk > msg_data_left(msg) && !more)
				chunk = msg_data_left(msg);
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

			space = chunk + call->conn->size_align;
			space &= ~(call->conn->size_align - 1UL);

			size = space + call->conn->header_size;

			_debug("SIZE: %zu/%zu/%zu", chunk, space, size);

			/* create a buffer that we can retain until it's ACK'd */
			skb = sock_alloc_send_skb(
				sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
			if (!skb)
				goto maybe_error;

			rxrpc_new_skb(skb);

			_debug("ALLOC SEND %p", skb);

			ASSERTCMP(skb->mark, ==, 0);

			_debug("HS: %u", call->conn->header_size);
			skb_reserve(skb, call->conn->header_size);
			skb->len += call->conn->header_size;

			sp = rxrpc_skb(skb);
			sp->remain = chunk;
			if (sp->remain > skb_tailroom(skb))
				sp->remain = skb_tailroom(skb);

			_net("skb: hr %d, tr %d, hl %d, rm %d",
			       skb_headroom(skb),
			       skb_tailroom(skb),
			       skb_headlen(skb),
			       sp->remain);

			skb->ip_summed = CHECKSUM_UNNECESSARY;
		}

		_debug("append");
		sp = rxrpc_skb(skb);

		/* append next segment of data to the current buffer */
A
Al Viro 已提交
644
		if (msg_data_left(msg) > 0) {
645 646
			int copy = skb_tailroom(skb);
			ASSERTCMP(copy, >, 0);
A
Al Viro 已提交
647 648
			if (copy > msg_data_left(msg))
				copy = msg_data_left(msg);
649 650 651 652 653 654 655 656 657 658 659 660
			if (copy > sp->remain)
				copy = sp->remain;

			_debug("add");
			ret = skb_add_data(skb, &msg->msg_iter, copy);
			_debug("added");
			if (ret < 0)
				goto efault;
			sp->remain -= copy;
			skb->mark += copy;
			copied += copy;
		}
661 662 663 664 665 666 667

		/* check for the far side aborting the call or a network error
		 * occurring */
		if (call->state > RXRPC_CALL_COMPLETE)
			goto call_aborted;

		/* add the packet to the send queue if it's now full */
668
		if (sp->remain <= 0 ||
A
Al Viro 已提交
669
		    (msg_data_left(msg) == 0 && !more)) {
670
			struct rxrpc_connection *conn = call->conn;
671
			uint32_t seq;
672 673 674
			size_t pad;

			/* pad out if we're using security */
675
			if (conn->security_ix) {
676 677 678 679 680 681 682 683
				pad = conn->security_size + skb->mark;
				pad = conn->size_align - pad;
				pad &= conn->size_align - 1;
				_debug("pad %zu", pad);
				if (pad)
					memset(skb_put(skb, pad), 0, pad);
			}

684 685
			seq = atomic_inc_return(&call->sequence);

686
			sp->hdr.epoch	= conn->proto.epoch;
687
			sp->hdr.cid	= call->cid;
688
			sp->hdr.callNumber = call->call_id;
689 690 691
			sp->hdr.seq	= seq;
			sp->hdr.serial	= atomic_inc_return(&conn->serial);
			sp->hdr.type	= RXRPC_PACKET_TYPE_DATA;
692 693
			sp->hdr.userStatus = 0;
			sp->hdr.securityIndex = conn->security_ix;
694 695
			sp->hdr._rsvd	= 0;
			sp->hdr.serviceId = call->service_id;
696 697

			sp->hdr.flags = conn->out_clientflag;
A
Al Viro 已提交
698
			if (msg_data_left(msg) == 0 && !more)
699
				sp->hdr.flags |= RXRPC_LAST_PACKET;
700 701
			else if (CIRC_SPACE(call->acks_head,
					    ACCESS_ONCE(call->acks_tail),
702 703
					    call->acks_winsz) > 1)
				sp->hdr.flags |= RXRPC_MORE_PACKETS;
704 705
			if (more && seq & 1)
				sp->hdr.flags |= RXRPC_REQUEST_ACK;
706

707
			ret = conn->security->secure_packet(
708
				call, skb, skb->mark,
709
				skb->head + sizeof(struct rxrpc_wire_header));
710 711 712
			if (ret < 0)
				goto out;

713
			rxrpc_insert_header(skb);
A
Al Viro 已提交
714
			rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
715 716
			skb = NULL;
		}
A
Al Viro 已提交
717
	} while (msg_data_left(msg) > 0);
718

719 720
success:
	ret = copied;
721 722 723 724 725 726 727 728
out:
	call->tx_pending = skb;
	_leave(" = %d", ret);
	return ret;

call_aborted:
	rxrpc_free_skb(skb);
	if (call->state == RXRPC_CALL_NETWORK_ERROR)
729 730 731
		ret = call->error_report < RXRPC_LOCAL_ERROR_OFFSET ?
			call->error_report :
			call->error_report - RXRPC_LOCAL_ERROR_OFFSET;
732 733 734 735 736 737 738
	else
		ret = -ECONNABORTED;
	_leave(" = %d", ret);
	return ret;

maybe_error:
	if (copied)
739
		goto success;
740 741 742 743 744 745
	goto out;

efault:
	ret = -EFAULT;
	goto out;
}