blob: 2e3c4064e29c22bed0ec2b5b7c472c20c712cd54 [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 */
David Howells2341e072016-06-09 23:02:51 +010035static int rxrpc_sendmsg_cmsg(struct msghdr *msg,
David Howells17926a72007-04-26 15:48:28 -070036 unsigned long *user_call_ID,
37 enum rxrpc_command *command,
David Howells2341e072016-06-09 23:02:51 +010038 u32 *abort_code)
David Howells17926a72007-04-26 15:48:28 -070039{
40 struct cmsghdr *cmsg;
David Howells2341e072016-06-09 23:02:51 +010041 bool got_user_ID = false;
David Howells17926a72007-04-26 15:48:28 -070042 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);
David Howells2341e072016-06-09 23:02:51 +010073 got_user_ID = true;
David Howells17926a72007-04-26 15:48:28 -070074 break;
75
76 case RXRPC_ABORT:
77 if (*command != RXRPC_CMD_SEND_DATA)
78 return -EINVAL;
79 *command = RXRPC_CMD_SEND_ABORT;
80 if (len != sizeof(*abort_code))
81 return -EINVAL;
82 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
83 _debug("Abort %x", *abort_code);
84 if (*abort_code == 0)
85 return -EINVAL;
86 break;
87
88 case RXRPC_ACCEPT:
89 if (*command != RXRPC_CMD_SEND_DATA)
90 return -EINVAL;
91 *command = RXRPC_CMD_ACCEPT;
92 if (len != 0)
93 return -EINVAL;
David Howells17926a72007-04-26 15:48:28 -070094 break;
95
96 default:
97 return -EINVAL;
98 }
99 }
100
David Howells2341e072016-06-09 23:02:51 +0100101 if (!got_user_ID)
102 return -EINVAL;
David Howells17926a72007-04-26 15:48:28 -0700103 _leave(" = 0");
104 return 0;
105}
106
107/*
108 * abort a call, sending an ABORT packet to the peer
109 */
110static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
111{
112 write_lock_bh(&call->state_lock);
113
114 if (call->state <= RXRPC_CALL_COMPLETE) {
115 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100116 call->local_abort = abort_code;
David Howells4c198ad2016-03-04 15:53:46 +0000117 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700118 del_timer_sync(&call->resend_timer);
119 del_timer_sync(&call->ack_timer);
David Howells4c198ad2016-03-04 15:53:46 +0000120 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
121 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700122 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells651350d2007-04-26 15:50:17 -0700123 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700124 }
125
126 write_unlock_bh(&call->state_lock);
127}
128
129/*
David Howells2341e072016-06-09 23:02:51 +0100130 * Create a new client call for sendmsg().
131 */
132static struct rxrpc_call *
133rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
134 unsigned long user_call_ID)
135{
136 struct rxrpc_conn_bundle *bundle;
137 struct rxrpc_transport *trans;
138 struct rxrpc_call *call;
139 struct key *key;
140 long ret;
141
142 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
143
144 _enter("");
145
146 if (!msg->msg_name)
147 return ERR_PTR(-EDESTADDRREQ);
148
149 trans = rxrpc_name_to_transport(rx, msg->msg_name, msg->msg_namelen, 0,
150 GFP_KERNEL);
151 if (IS_ERR(trans)) {
152 ret = PTR_ERR(trans);
153 goto out;
154 }
155
156 key = rx->key;
157 if (key && !rx->key->payload.data[0])
158 key = NULL;
159 bundle = rxrpc_get_bundle(rx, trans, key, srx->srx_service, GFP_KERNEL);
160 if (IS_ERR(bundle)) {
161 ret = PTR_ERR(bundle);
162 goto out_trans;
163 }
164
165 call = rxrpc_new_client_call(rx, trans, bundle, user_call_ID,
166 GFP_KERNEL);
167 rxrpc_put_bundle(trans, bundle);
168 rxrpc_put_transport(trans);
169 if (IS_ERR(call)) {
170 ret = PTR_ERR(call);
171 goto out_trans;
172 }
173
174 _leave(" = %p\n", call);
175 return call;
176
177out_trans:
178 rxrpc_put_transport(trans);
179out:
180 _leave(" = %ld", ret);
181 return ERR_PTR(ret);
182}
183
184/*
David Howells17926a72007-04-26 15:48:28 -0700185 * send a message forming part of a client call through an RxRPC socket
186 * - caller holds the socket locked
187 * - the socket may be either a client socket or a server socket
188 */
David Howells2341e072016-06-09 23:02:51 +0100189int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700190{
David Howells17926a72007-04-26 15:48:28 -0700191 enum rxrpc_command cmd;
192 struct rxrpc_call *call;
193 unsigned long user_call_ID = 0;
David Howells17926a72007-04-26 15:48:28 -0700194 u32 abort_code = 0;
195 int ret;
196
197 _enter("");
198
David Howells2341e072016-06-09 23:02:51 +0100199 ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code);
David Howells17926a72007-04-26 15:48:28 -0700200 if (ret < 0)
201 return ret;
202
David Howells2341e072016-06-09 23:02:51 +0100203 if (cmd == RXRPC_CMD_ACCEPT) {
204 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
205 return -EINVAL;
206 call = rxrpc_accept_call(rx, user_call_ID);
207 if (IS_ERR(call))
208 return PTR_ERR(call);
209 rxrpc_put_call(call);
210 return 0;
David Howells17926a72007-04-26 15:48:28 -0700211 }
212
David Howells2341e072016-06-09 23:02:51 +0100213 call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
214 if (!call) {
215 if (cmd != RXRPC_CMD_SEND_DATA)
216 return -EBADSLT;
217 call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID);
218 if (IS_ERR(call))
219 return PTR_ERR(call);
David Howells17926a72007-04-26 15:48:28 -0700220 }
221
222 _debug("CALL %d USR %lx ST %d on CONN %p",
223 call->debug_id, call->user_call_ID, call->state, call->conn);
224
225 if (call->state >= RXRPC_CALL_COMPLETE) {
226 /* it's too late for this call */
David Howells2341e072016-06-09 23:02:51 +0100227 ret = -ECONNRESET;
David Howells17926a72007-04-26 15:48:28 -0700228 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
229 rxrpc_send_abort(call, abort_code);
David Howells2341e072016-06-09 23:02:51 +0100230 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700231 } else if (cmd != RXRPC_CMD_SEND_DATA) {
232 ret = -EINVAL;
David Howells2341e072016-06-09 23:02:51 +0100233 } else if (!call->in_clientflag &&
234 call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
David Howells17926a72007-04-26 15:48:28 -0700235 /* request phase complete for this client call */
236 ret = -EPROTO;
David Howells2341e072016-06-09 23:02:51 +0100237 } else if (call->in_clientflag &&
238 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
239 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
240 /* Reply phase not begun or not complete for service call. */
241 ret = -EPROTO;
David Howells17926a72007-04-26 15:48:28 -0700242 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800243 ret = rxrpc_send_data(rx, call, msg, len);
David Howells17926a72007-04-26 15:48:28 -0700244 }
245
246 rxrpc_put_call(call);
247 _leave(" = %d", ret);
248 return ret;
249}
250
David Howells651350d2007-04-26 15:50:17 -0700251/**
252 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
253 * @call: The call to send data through
254 * @msg: The data to send
255 * @len: The amount of data to send
256 *
257 * Allow a kernel service to send data on a call. The call must be in an state
258 * appropriate to sending data. No control data should be supplied in @msg,
259 * nor should an address be supplied. MSG_MORE should be flagged if there's
260 * more data to come, otherwise this data will end the transmission phase.
261 */
262int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
263 size_t len)
264{
265 int ret;
266
267 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
268
269 ASSERTCMP(msg->msg_name, ==, NULL);
270 ASSERTCMP(msg->msg_control, ==, NULL);
271
272 lock_sock(&call->socket->sk);
273
274 _debug("CALL %d USR %lx ST %d on CONN %p",
275 call->debug_id, call->user_call_ID, call->state, call->conn);
276
277 if (call->state >= RXRPC_CALL_COMPLETE) {
278 ret = -ESHUTDOWN; /* it's too late for this call */
279 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
280 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
281 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
282 ret = -EPROTO; /* request phase complete for this client call */
283 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800284 ret = rxrpc_send_data(call->socket, call, msg, len);
David Howells651350d2007-04-26 15:50:17 -0700285 }
286
287 release_sock(&call->socket->sk);
288 _leave(" = %d", ret);
289 return ret;
290}
291
292EXPORT_SYMBOL(rxrpc_kernel_send_data);
293
Ben Hutchings2c530402012-07-10 10:55:09 +0000294/**
David Howells651350d2007-04-26 15:50:17 -0700295 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
296 * @call: The call to be aborted
297 * @abort_code: The abort code to stick into the ABORT packet
298 *
299 * Allow a kernel service to abort a call, if it's still in an abortable state.
300 */
301void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
302{
303 _enter("{%d},%d", call->debug_id, abort_code);
304
305 lock_sock(&call->socket->sk);
306
307 _debug("CALL %d USR %lx ST %d on CONN %p",
308 call->debug_id, call->user_call_ID, call->state, call->conn);
309
310 if (call->state < RXRPC_CALL_COMPLETE)
311 rxrpc_send_abort(call, abort_code);
312
313 release_sock(&call->socket->sk);
314 _leave("");
315}
316
317EXPORT_SYMBOL(rxrpc_kernel_abort_call);
318
David Howells17926a72007-04-26 15:48:28 -0700319/*
David Howells17926a72007-04-26 15:48:28 -0700320 * send a packet through the transport endpoint
321 */
322int rxrpc_send_packet(struct rxrpc_transport *trans, struct sk_buff *skb)
323{
324 struct kvec iov[1];
325 struct msghdr msg;
326 int ret, opt;
327
328 _enter(",{%d}", skb->len);
329
330 iov[0].iov_base = skb->head;
331 iov[0].iov_len = skb->len;
332
333 msg.msg_name = &trans->peer->srx.transport.sin;
334 msg.msg_namelen = sizeof(trans->peer->srx.transport.sin);
335 msg.msg_control = NULL;
336 msg.msg_controllen = 0;
337 msg.msg_flags = 0;
338
339 /* send the packet with the don't fragment bit set if we currently
340 * think it's small enough */
David Howells0d12f8a2016-03-04 15:53:46 +0000341 if (skb->len - sizeof(struct rxrpc_wire_header) < trans->peer->maxdata) {
David Howells17926a72007-04-26 15:48:28 -0700342 down_read(&trans->local->defrag_sem);
343 /* send the packet by UDP
344 * - returns -EMSGSIZE if UDP would have to fragment the packet
345 * to go out of the interface
346 * - in which case, we'll have processed the ICMP error
347 * message and update the peer record
348 */
349 ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
350 iov[0].iov_len);
351
352 up_read(&trans->local->defrag_sem);
353 if (ret == -EMSGSIZE)
354 goto send_fragmentable;
355
356 _leave(" = %d [%u]", ret, trans->peer->maxdata);
357 return ret;
358 }
359
360send_fragmentable:
361 /* attempt to send this message with fragmentation enabled */
362 _debug("send fragment");
363
364 down_write(&trans->local->defrag_sem);
365 opt = IP_PMTUDISC_DONT;
366 ret = kernel_setsockopt(trans->local->socket, SOL_IP, IP_MTU_DISCOVER,
367 (char *) &opt, sizeof(opt));
368 if (ret == 0) {
369 ret = kernel_sendmsg(trans->local->socket, &msg, iov, 1,
370 iov[0].iov_len);
371
372 opt = IP_PMTUDISC_DO;
373 kernel_setsockopt(trans->local->socket, SOL_IP,
374 IP_MTU_DISCOVER, (char *) &opt, sizeof(opt));
375 }
376
377 up_write(&trans->local->defrag_sem);
378 _leave(" = %d [frag %u]", ret, trans->peer->maxdata);
379 return ret;
380}
381
382/*
383 * wait for space to appear in the transmit/ACK window
384 * - caller holds the socket locked
385 */
386static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
387 struct rxrpc_call *call,
388 long *timeo)
389{
390 DECLARE_WAITQUEUE(myself, current);
391 int ret;
392
393 _enter(",{%d},%ld",
David Howellsee72b9f2016-03-04 15:58:06 +0000394 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
395 call->acks_winsz),
David Howells17926a72007-04-26 15:48:28 -0700396 *timeo);
397
398 add_wait_queue(&call->tx_waitq, &myself);
399
400 for (;;) {
401 set_current_state(TASK_INTERRUPTIBLE);
402 ret = 0;
David Howellsee72b9f2016-03-04 15:58:06 +0000403 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700404 call->acks_winsz) > 0)
405 break;
406 if (signal_pending(current)) {
407 ret = sock_intr_errno(*timeo);
408 break;
409 }
410
411 release_sock(&rx->sk);
412 *timeo = schedule_timeout(*timeo);
413 lock_sock(&rx->sk);
414 }
415
416 remove_wait_queue(&call->tx_waitq, &myself);
417 set_current_state(TASK_RUNNING);
418 _leave(" = %d", ret);
419 return ret;
420}
421
422/*
423 * attempt to schedule an instant Tx resend
424 */
425static inline void rxrpc_instant_resend(struct rxrpc_call *call)
426{
427 read_lock_bh(&call->state_lock);
428 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
429 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
430 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000431 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700432 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700433 }
434 read_unlock_bh(&call->state_lock);
435}
436
437/*
438 * queue a packet for transmission, set the resend timer and attempt
439 * to send the packet immediately
440 */
441static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
442 bool last)
443{
444 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
445 int ret;
446
447 _net("queue skb %p [%d]", skb, call->acks_head);
448
449 ASSERT(call->acks_window != NULL);
450 call->acks_window[call->acks_head] = (unsigned long) skb;
451 smp_wmb();
452 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
453
454 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
455 _debug("________awaiting reply/ACK__________");
456 write_lock_bh(&call->state_lock);
457 switch (call->state) {
458 case RXRPC_CALL_CLIENT_SEND_REQUEST:
459 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
460 break;
461 case RXRPC_CALL_SERVER_ACK_REQUEST:
462 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
463 if (!last)
464 break;
465 case RXRPC_CALL_SERVER_SEND_REPLY:
466 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
467 break;
468 default:
469 break;
470 }
471 write_unlock_bh(&call->state_lock);
472 }
473
David Howells0d12f8a2016-03-04 15:53:46 +0000474 _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700475
Rusty Russell3db1cd52011-12-19 13:56:45 +0000476 sp->need_resend = false;
David Howells5873c082014-02-07 18:58:44 +0000477 sp->resend_at = jiffies + rxrpc_resend_timeout;
David Howells17926a72007-04-26 15:48:28 -0700478 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
479 _debug("run timer");
480 call->resend_timer.expires = sp->resend_at;
481 add_timer(&call->resend_timer);
482 }
483
484 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
485 * we're ACK'ing the request phase of an incoming call */
486 ret = -EAGAIN;
487 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
488 /* the packet may be freed by rxrpc_process_call() before this
489 * returns */
490 ret = rxrpc_send_packet(call->conn->trans, skb);
491 _net("sent skb %p", skb);
492 } else {
493 _debug("failed to delete ACK timer");
494 }
495
496 if (ret < 0) {
497 _debug("need instant resend %d", ret);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000498 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700499 rxrpc_instant_resend(call);
500 }
501
502 _leave("");
503}
504
505/*
David Howells0d12f8a2016-03-04 15:53:46 +0000506 * Convert a host-endian header into a network-endian header.
507 */
508static void rxrpc_insert_header(struct sk_buff *skb)
509{
510 struct rxrpc_wire_header whdr;
511 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
512
513 whdr.epoch = htonl(sp->hdr.epoch);
514 whdr.cid = htonl(sp->hdr.cid);
515 whdr.callNumber = htonl(sp->hdr.callNumber);
516 whdr.seq = htonl(sp->hdr.seq);
517 whdr.serial = htonl(sp->hdr.serial);
518 whdr.type = sp->hdr.type;
519 whdr.flags = sp->hdr.flags;
520 whdr.userStatus = sp->hdr.userStatus;
521 whdr.securityIndex = sp->hdr.securityIndex;
522 whdr._rsvd = htons(sp->hdr._rsvd);
523 whdr.serviceId = htons(sp->hdr.serviceId);
524
525 memcpy(skb->head, &whdr, sizeof(whdr));
526}
527
528/*
David Howells17926a72007-04-26 15:48:28 -0700529 * send data through a socket
530 * - must be called in process context
531 * - caller holds the socket locked
532 */
Ying Xue1b784142015-03-02 15:37:48 +0800533static int rxrpc_send_data(struct rxrpc_sock *rx,
David Howells17926a72007-04-26 15:48:28 -0700534 struct rxrpc_call *call,
535 struct msghdr *msg, size_t len)
536{
537 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700538 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700539 struct sock *sk = &rx->sk;
540 long timeo;
541 bool more;
Al Viroaf2b0402014-11-27 21:44:24 -0500542 int ret, copied;
David Howells17926a72007-04-26 15:48:28 -0700543
David Howells17926a72007-04-26 15:48:28 -0700544 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
545
546 /* this should be in poll */
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800547 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
David Howells17926a72007-04-26 15:48:28 -0700548
549 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
550 return -EPIPE;
551
David Howells17926a72007-04-26 15:48:28 -0700552 more = msg->msg_flags & MSG_MORE;
553
554 skb = call->tx_pending;
555 call->tx_pending = NULL;
556
557 copied = 0;
David Howells3af68782015-04-01 14:06:00 +0100558 do {
David Howells17926a72007-04-26 15:48:28 -0700559 if (!skb) {
560 size_t size, chunk, max, space;
561
562 _debug("alloc");
563
David Howellsee72b9f2016-03-04 15:58:06 +0000564 if (CIRC_SPACE(call->acks_head,
565 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700566 call->acks_winsz) <= 0) {
567 ret = -EAGAIN;
568 if (msg->msg_flags & MSG_DONTWAIT)
569 goto maybe_error;
570 ret = rxrpc_wait_for_tx_window(rx, call,
571 &timeo);
572 if (ret < 0)
573 goto maybe_error;
574 }
575
576 max = call->conn->trans->peer->maxdata;
577 max -= call->conn->security_size;
578 max &= ~(call->conn->size_align - 1UL);
579
580 chunk = max;
Al Viro01e97e62014-12-15 21:39:31 -0500581 if (chunk > msg_data_left(msg) && !more)
582 chunk = msg_data_left(msg);
David Howells17926a72007-04-26 15:48:28 -0700583
584 space = chunk + call->conn->size_align;
585 space &= ~(call->conn->size_align - 1UL);
586
587 size = space + call->conn->header_size;
588
589 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
590
591 /* create a buffer that we can retain until it's ACK'd */
592 skb = sock_alloc_send_skb(
593 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
594 if (!skb)
595 goto maybe_error;
596
597 rxrpc_new_skb(skb);
598
599 _debug("ALLOC SEND %p", skb);
600
601 ASSERTCMP(skb->mark, ==, 0);
602
603 _debug("HS: %u", call->conn->header_size);
604 skb_reserve(skb, call->conn->header_size);
605 skb->len += call->conn->header_size;
606
607 sp = rxrpc_skb(skb);
608 sp->remain = chunk;
609 if (sp->remain > skb_tailroom(skb))
610 sp->remain = skb_tailroom(skb);
611
612 _net("skb: hr %d, tr %d, hl %d, rm %d",
613 skb_headroom(skb),
614 skb_tailroom(skb),
615 skb_headlen(skb),
616 sp->remain);
617
618 skb->ip_summed = CHECKSUM_UNNECESSARY;
619 }
620
621 _debug("append");
622 sp = rxrpc_skb(skb);
623
624 /* append next segment of data to the current buffer */
Al Viro01e97e62014-12-15 21:39:31 -0500625 if (msg_data_left(msg) > 0) {
David Howellsaab94832015-04-01 15:48:00 +0100626 int copy = skb_tailroom(skb);
627 ASSERTCMP(copy, >, 0);
Al Viro01e97e62014-12-15 21:39:31 -0500628 if (copy > msg_data_left(msg))
629 copy = msg_data_left(msg);
David Howellsaab94832015-04-01 15:48:00 +0100630 if (copy > sp->remain)
631 copy = sp->remain;
David Howells17926a72007-04-26 15:48:28 -0700632
David Howellsaab94832015-04-01 15:48:00 +0100633 _debug("add");
634 ret = skb_add_data(skb, &msg->msg_iter, copy);
635 _debug("added");
636 if (ret < 0)
637 goto efault;
638 sp->remain -= copy;
639 skb->mark += copy;
640 copied += copy;
David Howellsaab94832015-04-01 15:48:00 +0100641 }
David Howells17926a72007-04-26 15:48:28 -0700642
643 /* check for the far side aborting the call or a network error
644 * occurring */
645 if (call->state > RXRPC_CALL_COMPLETE)
646 goto call_aborted;
647
648 /* add the packet to the send queue if it's now full */
David Howells382d7972015-04-01 15:43:26 +0100649 if (sp->remain <= 0 ||
Al Viro01e97e62014-12-15 21:39:31 -0500650 (msg_data_left(msg) == 0 && !more)) {
David Howells17926a72007-04-26 15:48:28 -0700651 struct rxrpc_connection *conn = call->conn;
David Howellse8388eb2014-02-14 20:05:32 +0000652 uint32_t seq;
David Howells17926a72007-04-26 15:48:28 -0700653 size_t pad;
654
655 /* pad out if we're using security */
David Howellse0e4d822016-04-07 17:23:58 +0100656 if (conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700657 pad = conn->security_size + skb->mark;
658 pad = conn->size_align - pad;
659 pad &= conn->size_align - 1;
660 _debug("pad %zu", pad);
661 if (pad)
662 memset(skb_put(skb, pad), 0, pad);
663 }
664
David Howellse8388eb2014-02-14 20:05:32 +0000665 seq = atomic_inc_return(&call->sequence);
666
David Howells0d12f8a2016-03-04 15:53:46 +0000667 sp->hdr.epoch = conn->epoch;
668 sp->hdr.cid = call->cid;
David Howells17926a72007-04-26 15:48:28 -0700669 sp->hdr.callNumber = call->call_id;
David Howells0d12f8a2016-03-04 15:53:46 +0000670 sp->hdr.seq = seq;
671 sp->hdr.serial = atomic_inc_return(&conn->serial);
672 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
David Howells17926a72007-04-26 15:48:28 -0700673 sp->hdr.userStatus = 0;
674 sp->hdr.securityIndex = conn->security_ix;
David Howells0d12f8a2016-03-04 15:53:46 +0000675 sp->hdr._rsvd = 0;
676 sp->hdr.serviceId = call->service_id;
David Howells17926a72007-04-26 15:48:28 -0700677
678 sp->hdr.flags = conn->out_clientflag;
Al Viro01e97e62014-12-15 21:39:31 -0500679 if (msg_data_left(msg) == 0 && !more)
David Howells17926a72007-04-26 15:48:28 -0700680 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howellsee72b9f2016-03-04 15:58:06 +0000681 else if (CIRC_SPACE(call->acks_head,
682 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700683 call->acks_winsz) > 1)
684 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howellse8388eb2014-02-14 20:05:32 +0000685 if (more && seq & 1)
686 sp->hdr.flags |= RXRPC_REQUEST_ACK;
David Howells17926a72007-04-26 15:48:28 -0700687
David Howellse0e4d822016-04-07 17:23:58 +0100688 ret = conn->security->secure_packet(
David Howells17926a72007-04-26 15:48:28 -0700689 call, skb, skb->mark,
David Howells0d12f8a2016-03-04 15:53:46 +0000690 skb->head + sizeof(struct rxrpc_wire_header));
David Howells17926a72007-04-26 15:48:28 -0700691 if (ret < 0)
692 goto out;
693
David Howells0d12f8a2016-03-04 15:53:46 +0000694 rxrpc_insert_header(skb);
Al Viro01e97e62014-12-15 21:39:31 -0500695 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
David Howells17926a72007-04-26 15:48:28 -0700696 skb = NULL;
697 }
Al Viro01e97e62014-12-15 21:39:31 -0500698 } while (msg_data_left(msg) > 0);
David Howells17926a72007-04-26 15:48:28 -0700699
David Howells19e64542007-06-18 23:30:41 -0700700success:
701 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700702out:
703 call->tx_pending = skb;
704 _leave(" = %d", ret);
705 return ret;
706
707call_aborted:
708 rxrpc_free_skb(skb);
709 if (call->state == RXRPC_CALL_NETWORK_ERROR)
710 ret = call->conn->trans->peer->net_error;
711 else
712 ret = -ECONNABORTED;
713 _leave(" = %d", ret);
714 return ret;
715
716maybe_error:
717 if (copied)
David Howells19e64542007-06-18 23:30:41 -0700718 goto success;
David Howells17926a72007-04-26 15:48:28 -0700719 goto out;
720
721efault:
722 ret = -EFAULT;
723 goto out;
724}