1 /* RxRPC packet transmission
2  *
3  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11 
12 #include <linux/net.h>
13 #include <linux/gfp.h>
14 #include <linux/skbuff.h>
15 #include <linux/circ_buf.h>
16 #include <net/sock.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
19 
20 int rxrpc_resend_timeout = 4;
21 
22 static int rxrpc_send_data(struct kiocb *iocb,
23 			   struct rxrpc_sock *rx,
24 			   struct rxrpc_call *call,
25 			   struct msghdr *msg, size_t len);
26 
27 /*
28  * extract control messages from the sendmsg() control buffer
29  */
rxrpc_sendmsg_cmsg(struct rxrpc_sock * rx,struct msghdr * msg,unsigned long * user_call_ID,enum rxrpc_command * command,u32 * abort_code,bool server)30 static int rxrpc_sendmsg_cmsg(struct rxrpc_sock *rx, struct msghdr *msg,
31 			      unsigned long *user_call_ID,
32 			      enum rxrpc_command *command,
33 			      u32 *abort_code,
34 			      bool server)
35 {
36 	struct cmsghdr *cmsg;
37 	int len;
38 
39 	*command = RXRPC_CMD_SEND_DATA;
40 
41 	if (msg->msg_controllen == 0)
42 		return -EINVAL;
43 
44 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
45 		if (!CMSG_OK(msg, cmsg))
46 			return -EINVAL;
47 
48 		len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
49 		_debug("CMSG %d, %d, %d",
50 		       cmsg->cmsg_level, cmsg->cmsg_type, len);
51 
52 		if (cmsg->cmsg_level != SOL_RXRPC)
53 			continue;
54 
55 		switch (cmsg->cmsg_type) {
56 		case RXRPC_USER_CALL_ID:
57 			if (msg->msg_flags & MSG_CMSG_COMPAT) {
58 				if (len != sizeof(u32))
59 					return -EINVAL;
60 				*user_call_ID = *(u32 *) CMSG_DATA(cmsg);
61 			} else {
62 				if (len != sizeof(unsigned long))
63 					return -EINVAL;
64 				*user_call_ID = *(unsigned long *)
65 					CMSG_DATA(cmsg);
66 			}
67 			_debug("User Call ID %lx", *user_call_ID);
68 			break;
69 
70 		case RXRPC_ABORT:
71 			if (*command != RXRPC_CMD_SEND_DATA)
72 				return -EINVAL;
73 			*command = RXRPC_CMD_SEND_ABORT;
74 			if (len != sizeof(*abort_code))
75 				return -EINVAL;
76 			*abort_code = *(unsigned int *) CMSG_DATA(cmsg);
77 			_debug("Abort %x", *abort_code);
78 			if (*abort_code == 0)
79 				return -EINVAL;
80 			break;
81 
82 		case RXRPC_ACCEPT:
83 			if (*command != RXRPC_CMD_SEND_DATA)
84 				return -EINVAL;
85 			*command = RXRPC_CMD_ACCEPT;
86 			if (len != 0)
87 				return -EINVAL;
88 			if (!server)
89 				return -EISCONN;
90 			break;
91 
92 		default:
93 			return -EINVAL;
94 		}
95 	}
96 
97 	_leave(" = 0");
98 	return 0;
99 }
100 
101 /*
102  * abort a call, sending an ABORT packet to the peer
103  */
rxrpc_send_abort(struct rxrpc_call * call,u32 abort_code)104 static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
105 {
106 	write_lock_bh(&call->state_lock);
107 
108 	if (call->state <= RXRPC_CALL_COMPLETE) {
109 		call->state = RXRPC_CALL_LOCALLY_ABORTED;
110 		call->abort_code = abort_code;
111 		set_bit(RXRPC_CALL_ABORT, &call->events);
112 		del_timer_sync(&call->resend_timer);
113 		del_timer_sync(&call->ack_timer);
114 		clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
115 		clear_bit(RXRPC_CALL_ACK, &call->events);
116 		clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
117 		rxrpc_queue_call(call);
118 	}
119 
120 	write_unlock_bh(&call->state_lock);
121 }
122 
123 /*
124  * send a message forming part of a client call through an RxRPC socket
125  * - caller holds the socket locked
126  * - the socket may be either a client socket or a server socket
127  */
rxrpc_client_sendmsg(struct kiocb * iocb,struct rxrpc_sock * rx,struct rxrpc_transport * trans,struct msghdr * msg,size_t len)128 int rxrpc_client_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
129 			 struct rxrpc_transport *trans, struct msghdr *msg,
130 			 size_t len)
131 {
132 	struct rxrpc_conn_bundle *bundle;
133 	enum rxrpc_command cmd;
134 	struct rxrpc_call *call;
135 	unsigned long user_call_ID = 0;
136 	struct key *key;
137 	__be16 service_id;
138 	u32 abort_code = 0;
139 	int ret;
140 
141 	_enter("");
142 
143 	ASSERT(trans != NULL);
144 
145 	ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
146 				 false);
147 	if (ret < 0)
148 		return ret;
149 
150 	bundle = NULL;
151 	if (trans) {
152 		service_id = rx->service_id;
153 		if (msg->msg_name) {
154 			struct sockaddr_rxrpc *srx =
155 				(struct sockaddr_rxrpc *) msg->msg_name;
156 			service_id = htons(srx->srx_service);
157 		}
158 		key = rx->key;
159 		if (key && !rx->key->payload.data)
160 			key = NULL;
161 		bundle = rxrpc_get_bundle(rx, trans, key, service_id,
162 					  GFP_KERNEL);
163 		if (IS_ERR(bundle))
164 			return PTR_ERR(bundle);
165 	}
166 
167 	call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID,
168 				     abort_code == 0, GFP_KERNEL);
169 	if (trans)
170 		rxrpc_put_bundle(trans, bundle);
171 	if (IS_ERR(call)) {
172 		_leave(" = %ld", PTR_ERR(call));
173 		return PTR_ERR(call);
174 	}
175 
176 	_debug("CALL %d USR %lx ST %d on CONN %p",
177 	       call->debug_id, call->user_call_ID, call->state, call->conn);
178 
179 	if (call->state >= RXRPC_CALL_COMPLETE) {
180 		/* it's too late for this call */
181 		ret = -ESHUTDOWN;
182 	} else if (cmd == RXRPC_CMD_SEND_ABORT) {
183 		rxrpc_send_abort(call, abort_code);
184 	} else if (cmd != RXRPC_CMD_SEND_DATA) {
185 		ret = -EINVAL;
186 	} else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
187 		/* request phase complete for this client call */
188 		ret = -EPROTO;
189 	} else {
190 		ret = rxrpc_send_data(iocb, rx, call, msg, len);
191 	}
192 
193 	rxrpc_put_call(call);
194 	_leave(" = %d", ret);
195 	return ret;
196 }
197 
198 /**
199  * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
200  * @call: The call to send data through
201  * @msg: The data to send
202  * @len: The amount of data to send
203  *
204  * Allow a kernel service to send data on a call.  The call must be in an state
205  * appropriate to sending data.  No control data should be supplied in @msg,
206  * nor should an address be supplied.  MSG_MORE should be flagged if there's
207  * more data to come, otherwise this data will end the transmission phase.
208  */
rxrpc_kernel_send_data(struct rxrpc_call * call,struct msghdr * msg,size_t len)209 int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
210 			   size_t len)
211 {
212 	int ret;
213 
214 	_enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
215 
216 	ASSERTCMP(msg->msg_name, ==, NULL);
217 	ASSERTCMP(msg->msg_control, ==, NULL);
218 
219 	lock_sock(&call->socket->sk);
220 
221 	_debug("CALL %d USR %lx ST %d on CONN %p",
222 	       call->debug_id, call->user_call_ID, call->state, call->conn);
223 
224 	if (call->state >= RXRPC_CALL_COMPLETE) {
225 		ret = -ESHUTDOWN; /* it's too late for this call */
226 	} else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
227 		   call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
228 		   call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
229 		ret = -EPROTO; /* request phase complete for this client call */
230 	} else {
231 		mm_segment_t oldfs = get_fs();
232 		set_fs(KERNEL_DS);
233 		ret = rxrpc_send_data(NULL, call->socket, call, msg, len);
234 		set_fs(oldfs);
235 	}
236 
237 	release_sock(&call->socket->sk);
238 	_leave(" = %d", ret);
239 	return ret;
240 }
241 
242 EXPORT_SYMBOL(rxrpc_kernel_send_data);
243 
244 /*
245  * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
246  * @call: The call to be aborted
247  * @abort_code: The abort code to stick into the ABORT packet
248  *
249  * Allow a kernel service to abort a call, if it's still in an abortable state.
250  */
rxrpc_kernel_abort_call(struct rxrpc_call * call,u32 abort_code)251 void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
252 {
253 	_enter("{%d},%d", call->debug_id, abort_code);
254 
255 	lock_sock(&call->socket->sk);
256 
257 	_debug("CALL %d USR %lx ST %d on CONN %p",
258 	       call->debug_id, call->user_call_ID, call->state, call->conn);
259 
260 	if (call->state < RXRPC_CALL_COMPLETE)
261 		rxrpc_send_abort(call, abort_code);
262 
263 	release_sock(&call->socket->sk);
264 	_leave("");
265 }
266 
267 EXPORT_SYMBOL(rxrpc_kernel_abort_call);
268 
269 /*
270  * send a message through a server socket
271  * - caller holds the socket locked
272  */
rxrpc_server_sendmsg(struct kiocb * iocb,struct rxrpc_sock * rx,struct msghdr * msg,size_t len)273 int rxrpc_server_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
274 			 struct msghdr *msg, size_t len)
275 {
276 	enum rxrpc_command cmd;
277 	struct rxrpc_call *call;
278 	unsigned long user_call_ID = 0;
279 	u32 abort_code = 0;
280 	int ret;
281 
282 	_enter("");
283 
284 	ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
285 				 true);
286 	if (ret < 0)
287 		return ret;
288 
289 	if (cmd == RXRPC_CMD_ACCEPT) {
290 		call = rxrpc_accept_call(rx, user_call_ID);
291 		if (IS_ERR(call))
292 			return PTR_ERR(call);
293 		rxrpc_put_call(call);
294 		return 0;
295 	}
296 
297 	call = rxrpc_find_server_call(rx, user_call_ID);
298 	if (!call)
299 		return -EBADSLT;
300 	if (call->state >= RXRPC_CALL_COMPLETE) {
301 		ret = -ESHUTDOWN;
302 		goto out;
303 	}
304 
305 	switch (cmd) {
306 	case RXRPC_CMD_SEND_DATA:
307 		if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
308 		    call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
309 		    call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
310 			/* Tx phase not yet begun for this call */
311 			ret = -EPROTO;
312 			break;
313 		}
314 
315 		ret = rxrpc_send_data(iocb, rx, call, msg, len);
316 		break;
317 
318 	case RXRPC_CMD_SEND_ABORT:
319 		rxrpc_send_abort(call, abort_code);
320 		break;
321 	default:
322 		BUG();
323 	}
324 
325 	out:
326 	rxrpc_put_call(call);
327 	_leave(" = %d", ret);
328 	return ret;
329 }
330 
331 /*
332  * send a packet through the transport endpoint
333  */
rxrpc_send_packet(struct rxrpc_transport * trans,struct sk_buff * skb)334 int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
335 {
336 	struct kvec iov[1];
337 	struct msghdr msg;
338 	int ret, opt;
339 
340 	_enter(",{%d}", skb->len);
341 
342 	iov[0].iov_base = skb->head;
343 	iov[0].iov_len = skb->len;
344 
345 	msg.msg_name = &trans->peer->srx.transport.sin;
346 	msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
347 	msg.msg_control = NULL;
348 	msg.msg_controllen = 0;
349 	msg.msg_flags = 0;
350 
351 	/* send the packet with the don't fragment bit set if we currently
352 	 * think it's small enough */
353 	if (skb->len - sizeof(struct rxrpc_header) < trans->peer->maxdata) {
354 		down_read(&trans->local->defrag_sem);
355 		/* send the packet by UDP
356 		 * - returns -EMSGSIZE if UDP would have to fragment the packet
357 		 *   to go out of the interface
358 		 *   - in which case, we'll have processed the ICMP error
359 		 *     message and update the peer record
360 		 */
361 		ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
362 				     iov[0].iov_len);
363 
364 		up_read(&trans->local->defrag_sem);
365 		if (ret == -EMSGSIZE)
366 			goto send_fragmentable;
367 
368 		_leave(" = %d [%u]", ret, trans->peer->maxdata);
369 		return ret;
370 	}
371 
372 send_fragmentable:
373 	/* attempt to send this message with fragmentation enabled */
374 	_debug("send fragment");
375 
376 	down_write(&trans->local->defrag_sem);
377 	opt = IP_PMTUDISC_DONT;
378 	ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
379 				(char *) &opt, sizeof(opt));
380 	if (ret == 0) {
381 		ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
382 				     iov[0].iov_len);
383 
384 		opt = IP_PMTUDISC_DO;
385 		kernel_setsockopt(trans->local->socket, SOL_IP,
386 				  IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
387 	}
388 
389 	up_write(&trans->local->defrag_sem);
390 	_leave(" = %d [frag %u]", ret, trans->peer->maxdata);
391 	return ret;
392 }
393 
394 /*
395  * wait for space to appear in the transmit/ACK window
396  * - caller holds the socket locked
397  */
rxrpc_wait_for_tx_window(struct rxrpc_sock * rx,struct rxrpc_call * call,long * timeo)398 static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
399 				    struct rxrpc_call *call,
400 				    long *timeo)
401 {
402 	DECLARE_WAITQUEUE(myself, current);
403 	int ret;
404 
405 	_enter(",{%d},%ld",
406 	       CIRC_SPACE(call->acks_head, call->acks_tail, call->acks_winsz),
407 	       *timeo);
408 
409 	add_wait_queue(&call->tx_waitq, &myself);
410 
411 	for (;;) {
412 		set_current_state(TASK_INTERRUPTIBLE);
413 		ret = 0;
414 		if (CIRC_SPACE(call->acks_head, call->acks_tail,
415 			       call->acks_winsz) > 0)
416 			break;
417 		if (signal_pending(current)) {
418 			ret = sock_intr_errno(*timeo);
419 			break;
420 		}
421 
422 		release_sock(&rx->sk);
423 		*timeo = schedule_timeout(*timeo);
424 		lock_sock(&rx->sk);
425 	}
426 
427 	remove_wait_queue(&call->tx_waitq, &myself);
428 	set_current_state(TASK_RUNNING);
429 	_leave(" = %d", ret);
430 	return ret;
431 }
432 
433 /*
434  * attempt to schedule an instant Tx resend
435  */
rxrpc_instant_resend(struct rxrpc_call * call)436 static inline void rxrpc_instant_resend(struct rxrpc_call *call)
437 {
438 	read_lock_bh(&call->state_lock);
439 	if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
440 		clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
441 		if (call->state < RXRPC_CALL_COMPLETE &&
442 		    !test_and_set_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
443 			rxrpc_queue_call(call);
444 	}
445 	read_unlock_bh(&call->state_lock);
446 }
447 
448 /*
449  * queue a packet for transmission, set the resend timer and attempt
450  * to send the packet immediately
451  */
rxrpc_queue_packet(struct rxrpc_call * call,struct sk_buff * skb,bool last)452 static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
453 			       bool last)
454 {
455 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
456 	int ret;
457 
458 	_net("queue skb %p [%d]", skb, call->acks_head);
459 
460 	ASSERT(call->acks_window != NULL);
461 	call->acks_window[call->acks_head] = (unsigned long) skb;
462 	smp_wmb();
463 	call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
464 
465 	if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
466 		_debug("________awaiting reply/ACK__________");
467 		write_lock_bh(&call->state_lock);
468 		switch (call->state) {
469 		case RXRPC_CALL_CLIENT_SEND_REQUEST:
470 			call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
471 			break;
472 		case RXRPC_CALL_SERVER_ACK_REQUEST:
473 			call->state = RXRPC_CALL_SERVER_SEND_REPLY;
474 			if (!last)
475 				break;
476 		case RXRPC_CALL_SERVER_SEND_REPLY:
477 			call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
478 			break;
479 		default:
480 			break;
481 		}
482 		write_unlock_bh(&call->state_lock);
483 	}
484 
485 	_proto("Tx DATA %%%u { #%u }",
486 	       ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
487 
488 	sp->need_resend = 0;
489 	sp->resend_at = jiffies + rxrpc_resend_timeout * HZ;
490 	if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
491 		_debug("run timer");
492 		call->resend_timer.expires = sp->resend_at;
493 		add_timer(&call->resend_timer);
494 	}
495 
496 	/* attempt to cancel the rx-ACK timer, deferring reply transmission if
497 	 * we're ACK'ing the request phase of an incoming call */
498 	ret = -EAGAIN;
499 	if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
500 		/* the packet may be freed by rxrpc_process_call() before this
501 		 * returns */
502 		ret = rxrpc_send_packet(call->conn->trans, skb);
503 		_net("sent skb %p", skb);
504 	} else {
505 		_debug("failed to delete ACK timer");
506 	}
507 
508 	if (ret < 0) {
509 		_debug("need instant resend %d", ret);
510 		sp->need_resend = 1;
511 		rxrpc_instant_resend(call);
512 	}
513 
514 	_leave("");
515 }
516 
517 /*
518  * send data through a socket
519  * - must be called in process context
520  * - caller holds the socket locked
521  */
rxrpc_send_data(struct kiocb * iocb,struct rxrpc_sock * rx,struct rxrpc_call * call,struct msghdr * msg,size_t len)522 static int rxrpc_send_data(struct kiocb *iocb,
523 			   struct rxrpc_sock *rx,
524 			   struct rxrpc_call *call,
525 			   struct msghdr *msg, size_t len)
526 {
527 	struct rxrpc_skb_priv *sp;
528 	unsigned char __user *from;
529 	struct sk_buff *skb;
530 	struct iovec *iov;
531 	struct sock *sk = &rx->sk;
532 	long timeo;
533 	bool more;
534 	int ret, ioc, segment, copied;
535 
536 	_enter(",,,{%zu},%zu", msg->msg_iovlen, len);
537 
538 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
539 
540 	/* this should be in poll */
541 	clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
542 
543 	if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
544 		return -EPIPE;
545 
546 	iov = msg->msg_iov;
547 	ioc = msg->msg_iovlen - 1;
548 	from = iov->iov_base;
549 	segment = iov->iov_len;
550 	iov++;
551 	more = msg->msg_flags & MSG_MORE;
552 
553 	skb = call->tx_pending;
554 	call->tx_pending = NULL;
555 
556 	copied = 0;
557 	do {
558 		int copy;
559 
560 		if (segment > len)
561 			segment = len;
562 
563 		_debug("SEGMENT %d @%p", segment, from);
564 
565 		if (!skb) {
566 			size_t size, chunk, max, space;
567 
568 			_debug("alloc");
569 
570 			if (CIRC_SPACE(call->acks_head, call->acks_tail,
571 				       call->acks_winsz) <= 0) {
572 				ret = -EAGAIN;
573 				if (msg->msg_flags & MSG_DONTWAIT)
574 					goto maybe_error;
575 				ret = rxrpc_wait_for_tx_window(rx, call,
576 							       &timeo);
577 				if (ret < 0)
578 					goto maybe_error;
579 			}
580 
581 			max = call->conn->trans->peer->maxdata;
582 			max -= call->conn->security_size;
583 			max &= ~(call->conn->size_align - 1UL);
584 
585 			chunk = max;
586 			if (chunk > len && !more)
587 				chunk = len;
588 
589 			space = chunk + call->conn->size_align;
590 			space &= ~(call->conn->size_align - 1UL);
591 
592 			size = space + call->conn->header_size;
593 
594 			_debug("SIZE: %zu/%zu/%zu", chunk, space, size);
595 
596 			/* create a buffer that we can retain until it's ACK'd */
597 			skb = sock_alloc_send_skb(
598 				sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
599 			if (!skb)
600 				goto maybe_error;
601 
602 			rxrpc_new_skb(skb);
603 
604 			_debug("ALLOC SEND %p", skb);
605 
606 			ASSERTCMP(skb->mark, ==, 0);
607 
608 			_debug("HS: %u", call->conn->header_size);
609 			skb_reserve(skb, call->conn->header_size);
610 			skb->len += call->conn->header_size;
611 
612 			sp = rxrpc_skb(skb);
613 			sp->remain = chunk;
614 			if (sp->remain > skb_tailroom(skb))
615 				sp->remain = skb_tailroom(skb);
616 
617 			_net("skb: hr %d, tr %d, hl %d, rm %d",
618 			       skb_headroom(skb),
619 			       skb_tailroom(skb),
620 			       skb_headlen(skb),
621 			       sp->remain);
622 
623 			skb->ip_summed = CHECKSUM_UNNECESSARY;
624 		}
625 
626 		_debug("append");
627 		sp = rxrpc_skb(skb);
628 
629 		/* append next segment of data to the current buffer */
630 		copy = skb_tailroom(skb);
631 		ASSERTCMP(copy, >, 0);
632 		if (copy > segment)
633 			copy = segment;
634 		if (copy > sp->remain)
635 			copy = sp->remain;
636 
637 		_debug("add");
638 		ret = skb_add_data(skb, from, copy);
639 		_debug("added");
640 		if (ret < 0)
641 			goto efault;
642 		sp->remain -= copy;
643 		skb->mark += copy;
644 		copied += copy;
645 
646 		len -= copy;
647 		segment -= copy;
648 		from += copy;
649 		while (segment == 0 && ioc > 0) {
650 			from = iov->iov_base;
651 			segment = iov->iov_len;
652 			iov++;
653 			ioc--;
654 		}
655 		if (len == 0) {
656 			segment = 0;
657 			ioc = 0;
658 		}
659 
660 		/* check for the far side aborting the call or a network error
661 		 * occurring */
662 		if (call->state > RXRPC_CALL_COMPLETE)
663 			goto call_aborted;
664 
665 		/* add the packet to the send queue if it's now full */
666 		if (sp->remain <= 0 || (segment == 0 && !more)) {
667 			struct rxrpc_connection *conn = call->conn;
668 			size_t pad;
669 
670 			/* pad out if we're using security */
671 			if (conn->security) {
672 				pad = conn->security_size + skb->mark;
673 				pad = conn->size_align - pad;
674 				pad &= conn->size_align - 1;
675 				_debug("pad %zu", pad);
676 				if (pad)
677 					memset(skb_put(skb, pad), 0, pad);
678 			}
679 
680 			sp->hdr.epoch = conn->epoch;
681 			sp->hdr.cid = call->cid;
682 			sp->hdr.callNumber = call->call_id;
683 			sp->hdr.seq =
684 				htonl(atomic_inc_return(&call->sequence));
685 			sp->hdr.serial =
686 				htonl(atomic_inc_return(&conn->serial));
687 			sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
688 			sp->hdr.userStatus = 0;
689 			sp->hdr.securityIndex = conn->security_ix;
690 			sp->hdr._rsvd = 0;
691 			sp->hdr.serviceId = conn->service_id;
692 
693 			sp->hdr.flags = conn->out_clientflag;
694 			if (len == 0 && !more)
695 				sp->hdr.flags |= RXRPC_LAST_PACKET;
696 			else if (CIRC_SPACE(call->acks_head, call->acks_tail,
697 					    call->acks_winsz) > 1)
698 				sp->hdr.flags |= RXRPC_MORE_PACKETS;
699 
700 			ret = rxrpc_secure_packet(
701 				call, skb, skb->mark,
702 				skb->head + sizeof(struct rxrpc_header));
703 			if (ret < 0)
704 				goto out;
705 
706 			memcpy(skb->head, &sp->hdr,
707 			       sizeof(struct rxrpc_header));
708 			rxrpc_queue_packet(call, skb, segment == 0 && !more);
709 			skb = NULL;
710 		}
711 
712 	} while (segment > 0);
713 
714 success:
715 	ret = copied;
716 out:
717 	call->tx_pending = skb;
718 	_leave(" = %d", ret);
719 	return ret;
720 
721 call_aborted:
722 	rxrpc_free_skb(skb);
723 	if (call->state == RXRPC_CALL_NETWORK_ERROR)
724 		ret = call->conn->trans->peer->net_error;
725 	else
726 		ret = -ECONNABORTED;
727 	_leave(" = %d", ret);
728 	return ret;
729 
730 maybe_error:
731 	if (copied)
732 		goto success;
733 	goto out;
734 
735 efault:
736 	ret = -EFAULT;
737 	goto out;
738 }
739