blob: ea619535f0edad5b267922f7896134781532c4ab [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* 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
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
David Howells17926a72007-04-26 15:48:28 -070016#include <linux/skbuff.h>
17#include <linux/circ_buf.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040018#include <linux/export.h>
David Howells17926a72007-04-26 15:48:28 -070019#include <net/sock.h>
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
David Howells5873c082014-02-07 18:58:44 +000023/*
24 * Time till packet resend (in jiffies).
25 */
David Howellsdad8aff2016-03-09 23:22:56 +000026unsigned int rxrpc_resend_timeout = 4 * HZ;
David Howells17926a72007-04-26 15:48:28 -070027
Ying Xue1b784142015-03-02 15:37:48 +080028static int rxrpc_send_data(struct rxrpc_sock *rx,
David Howells17926a72007-04-26 15:48:28 -070029 struct rxrpc_call *call,
30 struct msghdr *msg, size_t len);
31
32/*
33 * extract control messages from the sendmsg() control buffer
34 */
35static int rxrpc_sendmsg_cmsg(struct rxrpc_sock *rx, struct msghdr *msg,
36 unsigned long *user_call_ID,
37 enum rxrpc_command *command,
38 u32 *abort_code,
39 bool server)
40{
41 struct cmsghdr *cmsg;
42 int len;
43
44 *command = RXRPC_CMD_SEND_DATA;
45
46 if (msg->msg_controllen == 0)
47 return -EINVAL;
48
Gu Zhengf95b4142014-12-11 11:22:04 +080049 for_each_cmsghdr(cmsg, msg) {
David Howells17926a72007-04-26 15:48:28 -070050 if (!CMSG_OK(msg, cmsg))
51 return -EINVAL;
52
53 len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
54 _debug("CMSG %d, %d, %d",
55 cmsg->cmsg_level, cmsg->cmsg_type, len);
56
57 if (cmsg->cmsg_level != SOL_RXRPC)
58 continue;
59
60 switch (cmsg->cmsg_type) {
61 case RXRPC_USER_CALL_ID:
62 if (msg->msg_flags & MSG_CMSG_COMPAT) {
63 if (len != sizeof(u32))
64 return -EINVAL;
65 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
66 } else {
67 if (len != sizeof(unsigned long))
68 return -EINVAL;
69 *user_call_ID = *(unsigned long *)
70 CMSG_DATA(cmsg);
71 }
72 _debug("User Call ID %lx", *user_call_ID);
73 break;
74
75 case RXRPC_ABORT:
76 if (*command != RXRPC_CMD_SEND_DATA)
77 return -EINVAL;
78 *command = RXRPC_CMD_SEND_ABORT;
79 if (len != sizeof(*abort_code))
80 return -EINVAL;
81 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
82 _debug("Abort %x", *abort_code);
83 if (*abort_code == 0)
84 return -EINVAL;
85 break;
86
87 case RXRPC_ACCEPT:
88 if (*command != RXRPC_CMD_SEND_DATA)
89 return -EINVAL;
90 *command = RXRPC_CMD_ACCEPT;
91 if (len != 0)
92 return -EINVAL;
93 if (!server)
94 return -EISCONN;
95 break;
96
97 default:
98 return -EINVAL;
99 }
100 }
101
102 _leave(" = 0");
103 return 0;
104}
105
106/*
107 * abort a call, sending an ABORT packet to the peer
108 */
109static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
110{
111 write_lock_bh(&call->state_lock);
112
113 if (call->state <= RXRPC_CALL_COMPLETE) {
114 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100115 call->local_abort = abort_code;
David Howells4c198ad2016-03-04 15:53:46 +0000116 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700117 del_timer_sync(&call->resend_timer);
118 del_timer_sync(&call->ack_timer);
David Howells4c198ad2016-03-04 15:53:46 +0000119 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
120 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700121 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells651350d2007-04-26 15:50:17 -0700122 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700123 }
124
125 write_unlock_bh(&call->state_lock);
126}
127
128/*
129 * send a message forming part of a client call through an RxRPC socket
130 * - caller holds the socket locked
131 * - the socket may be either a client socket or a server socket
132 */
Ying Xue1b784142015-03-02 15:37:48 +0800133int rxrpc_client_sendmsg(struct rxrpc_sock *rx, struct rxrpc_transport *trans,
134 struct msghdr *msg, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700135{
136 struct rxrpc_conn_bundle *bundle;
137 enum rxrpc_command cmd;
138 struct rxrpc_call *call;
139 unsigned long user_call_ID = 0;
140 struct key *key;
David Howells0d12f8a2016-03-04 15:53:46 +0000141 u16 service_id;
David Howells17926a72007-04-26 15:48:28 -0700142 u32 abort_code = 0;
143 int ret;
144
145 _enter("");
146
147 ASSERT(trans != NULL);
148
149 ret = rxrpc_sendmsg_cmsg(rx, msg, &user_call_ID, &cmd, &abort_code,
150 false);
151 if (ret < 0)
152 return ret;
153
154 bundle = NULL;
155 if (trans) {
David Howells0d12f8a2016-03-04 15:53:46 +0000156 service_id = rx->srx.srx_service;
David Howells17926a72007-04-26 15:48:28 -0700157 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100158 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx,
159 msg->msg_name);
David Howells0d12f8a2016-03-04 15:53:46 +0000160 service_id = srx->srx_service;
David Howells17926a72007-04-26 15:48:28 -0700161 }
162 key = rx->key;
David Howells146aa8b2015-10-21 14:04:48 +0100163 if (key && !rx->key->payload.data[0])
David Howells17926a72007-04-26 15:48:28 -0700164 key = NULL;
165 bundle = rxrpc_get_bundle(rx, trans, key, service_id,
166 GFP_KERNEL);
167 if (IS_ERR(bundle))
168 return PTR_ERR(bundle);
169 }
170
171 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID,
172 abort_code == 0, GFP_KERNEL);
173 if (trans)
174 rxrpc_put_bundle(trans, bundle);
175 if (IS_ERR(call)) {
176 _leave(" = %ld", PTR_ERR(call));
177 return PTR_ERR(call);
178 }
179
180 _debug("CALL %d USR %lx ST %d on CONN %p",
181 call->debug_id, call->user_call_ID, call->state, call->conn);
182
183 if (call->state >= RXRPC_CALL_COMPLETE) {
184 /* it's too late for this call */
185 ret = -ESHUTDOWN;
186 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
187 rxrpc_send_abort(call, abort_code);
188 } else if (cmd != RXRPC_CMD_SEND_DATA) {
189 ret = -EINVAL;
190 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
191 /* request phase complete for this client call */
192 ret = -EPROTO;
193 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800194 ret = rxrpc_send_data(rx, call, msg, len);
David Howells17926a72007-04-26 15:48:28 -0700195 }
196
197 rxrpc_put_call(call);
198 _leave(" = %d", ret);
199 return ret;
200}
201
David Howells651350d2007-04-26 15:50:17 -0700202/**
203 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
204 * @call: The call to send data through
205 * @msg: The data to send
206 * @len: The amount of data to send
207 *
208 * Allow a kernel service to send data on a call. The call must be in an state
209 * appropriate to sending data. No control data should be supplied in @msg,
210 * nor should an address be supplied. MSG_MORE should be flagged if there's
211 * more data to come, otherwise this data will end the transmission phase.
212 */
213int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
214 size_t len)
215{
216 int ret;
217
218 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
219
220 ASSERTCMP(msg->msg_name, ==, NULL);
221 ASSERTCMP(msg->msg_control, ==, NULL);
222
223 lock_sock(&call->socket->sk);
224
225 _debug("CALL %d USR %lx ST %d on CONN %p",
226 call->debug_id, call->user_call_ID, call->state, call->conn);
227
228 if (call->state >= RXRPC_CALL_COMPLETE) {
229 ret = -ESHUTDOWN; /* it's too late for this call */
230 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
231 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
232 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
233 ret = -EPROTO; /* request phase complete for this client call */
234 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800235 ret = rxrpc_send_data(call->socket, call, msg, len);
David Howells651350d2007-04-26 15:50:17 -0700236 }
237
238 release_sock(&call->socket->sk);
239 _leave(" = %d", ret);
240 return ret;
241}
242
243EXPORT_SYMBOL(rxrpc_kernel_send_data);
244
Ben Hutchings2c530402012-07-10 10:55:09 +0000245/**
David Howells651350d2007-04-26 15:50:17 -0700246 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
247 * @call: The call to be aborted
248 * @abort_code: The abort code to stick into the ABORT packet
249 *
250 * Allow a kernel service to abort a call, if it's still in an abortable state.
251 */
252void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
253{
254 _enter("{%d},%d", call->debug_id, abort_code);
255
256 lock_sock(&call->socket->sk);
257
258 _debug("CALL %d USR %lx ST %d on CONN %p",
259 call->debug_id, call->user_call_ID, call->state, call->conn);
260
261 if (call->state < RXRPC_CALL_COMPLETE)
262 rxrpc_send_abort(call, abort_code);
263
264 release_sock(&call->socket->sk);
265 _leave("");
266}
267
268EXPORT_SYMBOL(rxrpc_kernel_abort_call);
269
David Howells17926a72007-04-26 15:48:28 -0700270/*
271 * send a message through a server socket
272 * - caller holds the socket locked
273 */
Ying Xue1b784142015-03-02 15:37:48 +0800274int rxrpc_server_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700275{
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
David Howells651350d2007-04-26 15:50:17 -0700289 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 }
David Howells17926a72007-04-26 15:48:28 -0700296
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
Ying Xue1b784142015-03-02 15:37:48 +0800315 ret = rxrpc_send_data(rx, call, msg, len);
David Howells17926a72007-04-26 15:48:28 -0700316 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 */
334int 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 */
David Howells0d12f8a2016-03-04 15:53:46 +0000353 if (skb->len - sizeof(struct rxrpc_wire_header) < trans->peer->maxdata) {
David Howells17926a72007-04-26 15:48:28 -0700354 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
372send_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 */
398static 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",
David Howellsee72b9f2016-03-04 15:58:06 +0000406 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
407 call->acks_winsz),
David Howells17926a72007-04-26 15:48:28 -0700408 *timeo);
409
410 add_wait_queue(&call->tx_waitq, &myself);
411
412 for (;;) {
413 set_current_state(TASK_INTERRUPTIBLE);
414 ret = 0;
David Howellsee72b9f2016-03-04 15:58:06 +0000415 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700416 call->acks_winsz) > 0)
417 break;
418 if (signal_pending(current)) {
419 ret = sock_intr_errno(*timeo);
420 break;
421 }
422
423 release_sock(&rx->sk);
424 *timeo = schedule_timeout(*timeo);
425 lock_sock(&rx->sk);
426 }
427
428 remove_wait_queue(&call->tx_waitq, &myself);
429 set_current_state(TASK_RUNNING);
430 _leave(" = %d", ret);
431 return ret;
432}
433
434/*
435 * attempt to schedule an instant Tx resend
436 */
437static inline void rxrpc_instant_resend(struct rxrpc_call *call)
438{
439 read_lock_bh(&call->state_lock);
440 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
441 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
442 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000443 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700444 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700445 }
446 read_unlock_bh(&call->state_lock);
447}
448
449/*
450 * queue a packet for transmission, set the resend timer and attempt
451 * to send the packet immediately
452 */
453static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
454 bool last)
455{
456 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
457 int ret;
458
459 _net("queue skb %p [%d]", skb, call->acks_head);
460
461 ASSERT(call->acks_window != NULL);
462 call->acks_window[call->acks_head] = (unsigned long) skb;
463 smp_wmb();
464 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
465
466 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
467 _debug("________awaiting reply/ACK__________");
468 write_lock_bh(&call->state_lock);
469 switch (call->state) {
470 case RXRPC_CALL_CLIENT_SEND_REQUEST:
471 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
472 break;
473 case RXRPC_CALL_SERVER_ACK_REQUEST:
474 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
475 if (!last)
476 break;
477 case RXRPC_CALL_SERVER_SEND_REPLY:
478 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
479 break;
480 default:
481 break;
482 }
483 write_unlock_bh(&call->state_lock);
484 }
485
David Howells0d12f8a2016-03-04 15:53:46 +0000486 _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700487
Rusty Russell3db1cd52011-12-19 13:56:45 +0000488 sp->need_resend = false;
David Howells5873c082014-02-07 18:58:44 +0000489 sp->resend_at = jiffies + rxrpc_resend_timeout;
David Howells17926a72007-04-26 15:48:28 -0700490 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);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000510 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700511 rxrpc_instant_resend(call);
512 }
513
514 _leave("");
515}
516
517/*
David Howells0d12f8a2016-03-04 15:53:46 +0000518 * Convert a host-endian header into a network-endian header.
519 */
520static void rxrpc_insert_header(struct sk_buff *skb)
521{
522 struct rxrpc_wire_header whdr;
523 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
524
525 whdr.epoch = htonl(sp->hdr.epoch);
526 whdr.cid = htonl(sp->hdr.cid);
527 whdr.callNumber = htonl(sp->hdr.callNumber);
528 whdr.seq = htonl(sp->hdr.seq);
529 whdr.serial = htonl(sp->hdr.serial);
530 whdr.type = sp->hdr.type;
531 whdr.flags = sp->hdr.flags;
532 whdr.userStatus = sp->hdr.userStatus;
533 whdr.securityIndex = sp->hdr.securityIndex;
534 whdr._rsvd = htons(sp->hdr._rsvd);
535 whdr.serviceId = htons(sp->hdr.serviceId);
536
537 memcpy(skb->head, &whdr, sizeof(whdr));
538}
539
540/*
David Howells17926a72007-04-26 15:48:28 -0700541 * send data through a socket
542 * - must be called in process context
543 * - caller holds the socket locked
544 */
Ying Xue1b784142015-03-02 15:37:48 +0800545static int rxrpc_send_data(struct rxrpc_sock *rx,
David Howells17926a72007-04-26 15:48:28 -0700546 struct rxrpc_call *call,
547 struct msghdr *msg, size_t len)
548{
549 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700550 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700551 struct sock *sk = &rx->sk;
552 long timeo;
553 bool more;
Al Viroaf2b0402014-11-27 21:44:24 -0500554 int ret, copied;
David Howells17926a72007-04-26 15:48:28 -0700555
David Howells17926a72007-04-26 15:48:28 -0700556 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
557
558 /* this should be in poll */
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800559 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
David Howells17926a72007-04-26 15:48:28 -0700560
561 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
562 return -EPIPE;
563
David Howells17926a72007-04-26 15:48:28 -0700564 more = msg->msg_flags & MSG_MORE;
565
566 skb = call->tx_pending;
567 call->tx_pending = NULL;
568
569 copied = 0;
David Howells3af68782015-04-01 14:06:00 +0100570 do {
David Howells17926a72007-04-26 15:48:28 -0700571 if (!skb) {
572 size_t size, chunk, max, space;
573
574 _debug("alloc");
575
David Howellsee72b9f2016-03-04 15:58:06 +0000576 if (CIRC_SPACE(call->acks_head,
577 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700578 call->acks_winsz) <= 0) {
579 ret = -EAGAIN;
580 if (msg->msg_flags & MSG_DONTWAIT)
581 goto maybe_error;
582 ret = rxrpc_wait_for_tx_window(rx, call,
583 &timeo);
584 if (ret < 0)
585 goto maybe_error;
586 }
587
588 max = call->conn->trans->peer->maxdata;
589 max -= call->conn->security_size;
590 max &= ~(call->conn->size_align - 1UL);
591
592 chunk = max;
Al Viro01e97e62014-12-15 21:39:31 -0500593 if (chunk > msg_data_left(msg) && !more)
594 chunk = msg_data_left(msg);
David Howells17926a72007-04-26 15:48:28 -0700595
596 space = chunk + call->conn->size_align;
597 space &= ~(call->conn->size_align - 1UL);
598
599 size = space + call->conn->header_size;
600
601 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
602
603 /* create a buffer that we can retain until it's ACK'd */
604 skb = sock_alloc_send_skb(
605 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
606 if (!skb)
607 goto maybe_error;
608
609 rxrpc_new_skb(skb);
610
611 _debug("ALLOC SEND %p", skb);
612
613 ASSERTCMP(skb->mark, ==, 0);
614
615 _debug("HS: %u", call->conn->header_size);
616 skb_reserve(skb, call->conn->header_size);
617 skb->len += call->conn->header_size;
618
619 sp = rxrpc_skb(skb);
620 sp->remain = chunk;
621 if (sp->remain > skb_tailroom(skb))
622 sp->remain = skb_tailroom(skb);
623
624 _net("skb: hr %d, tr %d, hl %d, rm %d",
625 skb_headroom(skb),
626 skb_tailroom(skb),
627 skb_headlen(skb),
628 sp->remain);
629
630 skb->ip_summed = CHECKSUM_UNNECESSARY;
631 }
632
633 _debug("append");
634 sp = rxrpc_skb(skb);
635
636 /* append next segment of data to the current buffer */
Al Viro01e97e62014-12-15 21:39:31 -0500637 if (msg_data_left(msg) > 0) {
David Howellsaab94832015-04-01 15:48:00 +0100638 int copy = skb_tailroom(skb);
639 ASSERTCMP(copy, >, 0);
Al Viro01e97e62014-12-15 21:39:31 -0500640 if (copy > msg_data_left(msg))
641 copy = msg_data_left(msg);
David Howellsaab94832015-04-01 15:48:00 +0100642 if (copy > sp->remain)
643 copy = sp->remain;
David Howells17926a72007-04-26 15:48:28 -0700644
David Howellsaab94832015-04-01 15:48:00 +0100645 _debug("add");
646 ret = skb_add_data(skb, &msg->msg_iter, copy);
647 _debug("added");
648 if (ret < 0)
649 goto efault;
650 sp->remain -= copy;
651 skb->mark += copy;
652 copied += copy;
David Howellsaab94832015-04-01 15:48:00 +0100653 }
David Howells17926a72007-04-26 15:48:28 -0700654
655 /* check for the far side aborting the call or a network error
656 * occurring */
657 if (call->state > RXRPC_CALL_COMPLETE)
658 goto call_aborted;
659
660 /* add the packet to the send queue if it's now full */
David Howells382d7972015-04-01 15:43:26 +0100661 if (sp->remain <= 0 ||
Al Viro01e97e62014-12-15 21:39:31 -0500662 (msg_data_left(msg) == 0 && !more)) {
David Howells17926a72007-04-26 15:48:28 -0700663 struct rxrpc_connection *conn = call->conn;
David Howellse8388eb2014-02-14 20:05:32 +0000664 uint32_t seq;
David Howells17926a72007-04-26 15:48:28 -0700665 size_t pad;
666
667 /* pad out if we're using security */
David Howellse0e4d822016-04-07 17:23:58 +0100668 if (conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700669 pad = conn->security_size + skb->mark;
670 pad = conn->size_align - pad;
671 pad &= conn->size_align - 1;
672 _debug("pad %zu", pad);
673 if (pad)
674 memset(skb_put(skb, pad), 0, pad);
675 }
676
David Howellse8388eb2014-02-14 20:05:32 +0000677 seq = atomic_inc_return(&call->sequence);
678
David Howells0d12f8a2016-03-04 15:53:46 +0000679 sp->hdr.epoch = conn->epoch;
680 sp->hdr.cid = call->cid;
David Howells17926a72007-04-26 15:48:28 -0700681 sp->hdr.callNumber = call->call_id;
David Howells0d12f8a2016-03-04 15:53:46 +0000682 sp->hdr.seq = seq;
683 sp->hdr.serial = atomic_inc_return(&conn->serial);
684 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
David Howells17926a72007-04-26 15:48:28 -0700685 sp->hdr.userStatus = 0;
686 sp->hdr.securityIndex = conn->security_ix;
David Howells0d12f8a2016-03-04 15:53:46 +0000687 sp->hdr._rsvd = 0;
688 sp->hdr.serviceId = call->service_id;
David Howells17926a72007-04-26 15:48:28 -0700689
690 sp->hdr.flags = conn->out_clientflag;
Al Viro01e97e62014-12-15 21:39:31 -0500691 if (msg_data_left(msg) == 0 && !more)
David Howells17926a72007-04-26 15:48:28 -0700692 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howellsee72b9f2016-03-04 15:58:06 +0000693 else if (CIRC_SPACE(call->acks_head,
694 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700695 call->acks_winsz) > 1)
696 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howellse8388eb2014-02-14 20:05:32 +0000697 if (more && seq & 1)
698 sp->hdr.flags |= RXRPC_REQUEST_ACK;
David Howells17926a72007-04-26 15:48:28 -0700699
David Howellse0e4d822016-04-07 17:23:58 +0100700 ret = conn->security->secure_packet(
David Howells17926a72007-04-26 15:48:28 -0700701 call, skb, skb->mark,
David Howells0d12f8a2016-03-04 15:53:46 +0000702 skb->head + sizeof(struct rxrpc_wire_header));
David Howells17926a72007-04-26 15:48:28 -0700703 if (ret < 0)
704 goto out;
705
David Howells0d12f8a2016-03-04 15:53:46 +0000706 rxrpc_insert_header(skb);
Al Viro01e97e62014-12-15 21:39:31 -0500707 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
David Howells17926a72007-04-26 15:48:28 -0700708 skb = NULL;
709 }
Al Viro01e97e62014-12-15 21:39:31 -0500710 } while (msg_data_left(msg) > 0);
David Howells17926a72007-04-26 15:48:28 -0700711
David Howells19e64542007-06-18 23:30:41 -0700712success:
713 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700714out:
715 call->tx_pending = skb;
716 _leave(" = %d", ret);
717 return ret;
718
719call_aborted:
720 rxrpc_free_skb(skb);
721 if (call->state == RXRPC_CALL_NETWORK_ERROR)
722 ret = call->conn->trans->peer->net_error;
723 else
724 ret = -ECONNABORTED;
725 _leave(" = %d", ret);
726 return ret;
727
728maybe_error:
729 if (copied)
David Howells19e64542007-06-18 23:30:41 -0700730 goto success;
David Howells17926a72007-04-26 15:48:28 -0700731 goto out;
732
733efault:
734 ret = -EFAULT;
735 goto out;
736}