blob: db3933cf6b97ded5d993101e1ce904028725ac7e [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 Howellscc8feb82016-04-04 14:00:37 +010038 u32 *abort_code,
39 bool *_exclusive)
David Howells17926a72007-04-26 15:48:28 -070040{
41 struct cmsghdr *cmsg;
David Howells2341e072016-06-09 23:02:51 +010042 bool got_user_ID = false;
David Howells17926a72007-04-26 15:48:28 -070043 int len;
44
45 *command = RXRPC_CMD_SEND_DATA;
46
47 if (msg->msg_controllen == 0)
48 return -EINVAL;
49
Gu Zhengf95b4142014-12-11 11:22:04 +080050 for_each_cmsghdr(cmsg, msg) {
David Howells17926a72007-04-26 15:48:28 -070051 if (!CMSG_OK(msg, cmsg))
52 return -EINVAL;
53
54 len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
55 _debug("CMSG %d, %d, %d",
56 cmsg->cmsg_level, cmsg->cmsg_type, len);
57
58 if (cmsg->cmsg_level != SOL_RXRPC)
59 continue;
60
61 switch (cmsg->cmsg_type) {
62 case RXRPC_USER_CALL_ID:
63 if (msg->msg_flags & MSG_CMSG_COMPAT) {
64 if (len != sizeof(u32))
65 return -EINVAL;
66 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
67 } else {
68 if (len != sizeof(unsigned long))
69 return -EINVAL;
70 *user_call_ID = *(unsigned long *)
71 CMSG_DATA(cmsg);
72 }
73 _debug("User Call ID %lx", *user_call_ID);
David Howells2341e072016-06-09 23:02:51 +010074 got_user_ID = true;
David Howells17926a72007-04-26 15:48:28 -070075 break;
76
77 case RXRPC_ABORT:
78 if (*command != RXRPC_CMD_SEND_DATA)
79 return -EINVAL;
80 *command = RXRPC_CMD_SEND_ABORT;
81 if (len != sizeof(*abort_code))
82 return -EINVAL;
83 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
84 _debug("Abort %x", *abort_code);
85 if (*abort_code == 0)
86 return -EINVAL;
87 break;
88
89 case RXRPC_ACCEPT:
90 if (*command != RXRPC_CMD_SEND_DATA)
91 return -EINVAL;
92 *command = RXRPC_CMD_ACCEPT;
93 if (len != 0)
94 return -EINVAL;
David Howells17926a72007-04-26 15:48:28 -070095 break;
96
David Howellscc8feb82016-04-04 14:00:37 +010097 case RXRPC_EXCLUSIVE_CALL:
98 *_exclusive = true;
99 if (len != 0)
100 return -EINVAL;
101 break;
David Howells17926a72007-04-26 15:48:28 -0700102 default:
103 return -EINVAL;
104 }
105 }
106
David Howells2341e072016-06-09 23:02:51 +0100107 if (!got_user_ID)
108 return -EINVAL;
David Howells17926a72007-04-26 15:48:28 -0700109 _leave(" = 0");
110 return 0;
111}
112
113/*
114 * abort a call, sending an ABORT packet to the peer
115 */
116static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
117{
118 write_lock_bh(&call->state_lock);
119
120 if (call->state <= RXRPC_CALL_COMPLETE) {
121 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100122 call->local_abort = abort_code;
David Howells4c198ad2016-03-04 15:53:46 +0000123 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700124 del_timer_sync(&call->resend_timer);
125 del_timer_sync(&call->ack_timer);
David Howells4c198ad2016-03-04 15:53:46 +0000126 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
127 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700128 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells651350d2007-04-26 15:50:17 -0700129 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700130 }
131
132 write_unlock_bh(&call->state_lock);
133}
134
135/*
David Howells2341e072016-06-09 23:02:51 +0100136 * Create a new client call for sendmsg().
137 */
138static struct rxrpc_call *
139rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
David Howellscc8feb82016-04-04 14:00:37 +0100140 unsigned long user_call_ID, bool exclusive)
David Howells2341e072016-06-09 23:02:51 +0100141{
David Howells19ffa012016-04-04 14:00:36 +0100142 struct rxrpc_conn_parameters cp;
David Howells2341e072016-06-09 23:02:51 +0100143 struct rxrpc_conn_bundle *bundle;
144 struct rxrpc_transport *trans;
145 struct rxrpc_call *call;
146 struct key *key;
147 long ret;
148
149 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
150
151 _enter("");
152
153 if (!msg->msg_name)
154 return ERR_PTR(-EDESTADDRREQ);
155
David Howells19ffa012016-04-04 14:00:36 +0100156 key = rx->key;
157 if (key && !rx->key->payload.data[0])
158 key = NULL;
159
160 memset(&cp, 0, sizeof(cp));
161 cp.local = rx->local;
162 cp.key = rx->key;
163 cp.security_level = rx->min_sec_level;
David Howellscc8feb82016-04-04 14:00:37 +0100164 cp.exclusive = rx->exclusive | exclusive;
David Howells19ffa012016-04-04 14:00:36 +0100165 cp.service_id = srx->srx_service;
166 trans = rxrpc_name_to_transport(&cp, msg->msg_name, msg->msg_namelen,
David Howells2341e072016-06-09 23:02:51 +0100167 GFP_KERNEL);
168 if (IS_ERR(trans)) {
169 ret = PTR_ERR(trans);
170 goto out;
171 }
David Howells19ffa012016-04-04 14:00:36 +0100172 cp.peer = trans->peer;
David Howells2341e072016-06-09 23:02:51 +0100173
David Howells19ffa012016-04-04 14:00:36 +0100174 bundle = rxrpc_get_bundle(rx, trans, cp.key, srx->srx_service,
175 GFP_KERNEL);
David Howells2341e072016-06-09 23:02:51 +0100176 if (IS_ERR(bundle)) {
177 ret = PTR_ERR(bundle);
178 goto out_trans;
179 }
180
David Howells19ffa012016-04-04 14:00:36 +0100181 call = rxrpc_new_client_call(rx, &cp, trans, bundle, user_call_ID,
David Howells2341e072016-06-09 23:02:51 +0100182 GFP_KERNEL);
183 rxrpc_put_bundle(trans, bundle);
184 rxrpc_put_transport(trans);
185 if (IS_ERR(call)) {
186 ret = PTR_ERR(call);
187 goto out_trans;
188 }
189
190 _leave(" = %p\n", call);
191 return call;
192
193out_trans:
194 rxrpc_put_transport(trans);
195out:
196 _leave(" = %ld", ret);
197 return ERR_PTR(ret);
198}
199
200/*
David Howells17926a72007-04-26 15:48:28 -0700201 * send a message forming part of a client call through an RxRPC socket
202 * - caller holds the socket locked
203 * - the socket may be either a client socket or a server socket
204 */
David Howells2341e072016-06-09 23:02:51 +0100205int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700206{
David Howells17926a72007-04-26 15:48:28 -0700207 enum rxrpc_command cmd;
208 struct rxrpc_call *call;
209 unsigned long user_call_ID = 0;
David Howellscc8feb82016-04-04 14:00:37 +0100210 bool exclusive = false;
David Howells17926a72007-04-26 15:48:28 -0700211 u32 abort_code = 0;
212 int ret;
213
214 _enter("");
215
David Howellscc8feb82016-04-04 14:00:37 +0100216 ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
217 &exclusive);
David Howells17926a72007-04-26 15:48:28 -0700218 if (ret < 0)
219 return ret;
220
David Howells2341e072016-06-09 23:02:51 +0100221 if (cmd == RXRPC_CMD_ACCEPT) {
222 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
223 return -EINVAL;
224 call = rxrpc_accept_call(rx, user_call_ID);
225 if (IS_ERR(call))
226 return PTR_ERR(call);
227 rxrpc_put_call(call);
228 return 0;
David Howells17926a72007-04-26 15:48:28 -0700229 }
230
David Howells2341e072016-06-09 23:02:51 +0100231 call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
232 if (!call) {
233 if (cmd != RXRPC_CMD_SEND_DATA)
234 return -EBADSLT;
David Howellscc8feb82016-04-04 14:00:37 +0100235 call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
236 exclusive);
David Howells2341e072016-06-09 23:02:51 +0100237 if (IS_ERR(call))
238 return PTR_ERR(call);
David Howells17926a72007-04-26 15:48:28 -0700239 }
240
241 _debug("CALL %d USR %lx ST %d on CONN %p",
242 call->debug_id, call->user_call_ID, call->state, call->conn);
243
244 if (call->state >= RXRPC_CALL_COMPLETE) {
245 /* it's too late for this call */
David Howells2341e072016-06-09 23:02:51 +0100246 ret = -ECONNRESET;
David Howells17926a72007-04-26 15:48:28 -0700247 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
248 rxrpc_send_abort(call, abort_code);
David Howells2341e072016-06-09 23:02:51 +0100249 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700250 } else if (cmd != RXRPC_CMD_SEND_DATA) {
251 ret = -EINVAL;
David Howells2341e072016-06-09 23:02:51 +0100252 } else if (!call->in_clientflag &&
253 call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
David Howells17926a72007-04-26 15:48:28 -0700254 /* request phase complete for this client call */
255 ret = -EPROTO;
David Howells2341e072016-06-09 23:02:51 +0100256 } else if (call->in_clientflag &&
257 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
258 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
259 /* Reply phase not begun or not complete for service call. */
260 ret = -EPROTO;
David Howells17926a72007-04-26 15:48:28 -0700261 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800262 ret = rxrpc_send_data(rx, call, msg, len);
David Howells17926a72007-04-26 15:48:28 -0700263 }
264
265 rxrpc_put_call(call);
266 _leave(" = %d", ret);
267 return ret;
268}
269
David Howells651350d2007-04-26 15:50:17 -0700270/**
271 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
272 * @call: The call to send data through
273 * @msg: The data to send
274 * @len: The amount of data to send
275 *
276 * Allow a kernel service to send data on a call. The call must be in an state
277 * appropriate to sending data. No control data should be supplied in @msg,
278 * nor should an address be supplied. MSG_MORE should be flagged if there's
279 * more data to come, otherwise this data will end the transmission phase.
280 */
281int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
282 size_t len)
283{
284 int ret;
285
286 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
287
288 ASSERTCMP(msg->msg_name, ==, NULL);
289 ASSERTCMP(msg->msg_control, ==, NULL);
290
291 lock_sock(&call->socket->sk);
292
293 _debug("CALL %d USR %lx ST %d on CONN %p",
294 call->debug_id, call->user_call_ID, call->state, call->conn);
295
296 if (call->state >= RXRPC_CALL_COMPLETE) {
297 ret = -ESHUTDOWN; /* it's too late for this call */
298 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
299 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
300 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
301 ret = -EPROTO; /* request phase complete for this client call */
302 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800303 ret = rxrpc_send_data(call->socket, call, msg, len);
David Howells651350d2007-04-26 15:50:17 -0700304 }
305
306 release_sock(&call->socket->sk);
307 _leave(" = %d", ret);
308 return ret;
309}
310
311EXPORT_SYMBOL(rxrpc_kernel_send_data);
312
Ben Hutchings2c530402012-07-10 10:55:09 +0000313/**
David Howells651350d2007-04-26 15:50:17 -0700314 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
315 * @call: The call to be aborted
316 * @abort_code: The abort code to stick into the ABORT packet
317 *
318 * Allow a kernel service to abort a call, if it's still in an abortable state.
319 */
320void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
321{
322 _enter("{%d},%d", call->debug_id, abort_code);
323
324 lock_sock(&call->socket->sk);
325
326 _debug("CALL %d USR %lx ST %d on CONN %p",
327 call->debug_id, call->user_call_ID, call->state, call->conn);
328
329 if (call->state < RXRPC_CALL_COMPLETE)
330 rxrpc_send_abort(call, abort_code);
331
332 release_sock(&call->socket->sk);
333 _leave("");
334}
335
336EXPORT_SYMBOL(rxrpc_kernel_abort_call);
337
David Howells17926a72007-04-26 15:48:28 -0700338/*
David Howells17926a72007-04-26 15:48:28 -0700339 * send a packet through the transport endpoint
340 */
David Howells985a5c82016-06-17 11:53:37 +0100341int rxrpc_send_data_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700342{
343 struct kvec iov[1];
344 struct msghdr msg;
345 int ret, opt;
346
347 _enter(",{%d}", skb->len);
348
349 iov[0].iov_base = skb->head;
350 iov[0].iov_len = skb->len;
351
David Howells985a5c82016-06-17 11:53:37 +0100352 msg.msg_name = &conn->params.peer->srx.transport;
353 msg.msg_namelen = conn->params.peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700354 msg.msg_control = NULL;
355 msg.msg_controllen = 0;
356 msg.msg_flags = 0;
357
358 /* send the packet with the don't fragment bit set if we currently
359 * think it's small enough */
David Howells985a5c82016-06-17 11:53:37 +0100360 if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) {
361 down_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700362 /* send the packet by UDP
363 * - returns -EMSGSIZE if UDP would have to fragment the packet
364 * to go out of the interface
365 * - in which case, we'll have processed the ICMP error
366 * message and update the peer record
367 */
David Howells985a5c82016-06-17 11:53:37 +0100368 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
David Howells17926a72007-04-26 15:48:28 -0700369 iov[0].iov_len);
370
David Howells985a5c82016-06-17 11:53:37 +0100371 up_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700372 if (ret == -EMSGSIZE)
373 goto send_fragmentable;
374
David Howells985a5c82016-06-17 11:53:37 +0100375 _leave(" = %d [%u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700376 return ret;
377 }
378
379send_fragmentable:
380 /* attempt to send this message with fragmentation enabled */
381 _debug("send fragment");
382
David Howells985a5c82016-06-17 11:53:37 +0100383 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700384
David Howells985a5c82016-06-17 11:53:37 +0100385 switch (conn->params.local->srx.transport.family) {
386 case AF_INET:
387 opt = IP_PMTUDISC_DONT;
388 ret = kernel_setsockopt(conn->params.local->socket,
389 SOL_IP, IP_MTU_DISCOVER,
390 (char *)&opt, sizeof(opt));
391 if (ret == 0) {
392 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
393 iov[0].iov_len);
394
395 opt = IP_PMTUDISC_DO;
396 kernel_setsockopt(conn->params.local->socket, SOL_IP,
397 IP_MTU_DISCOVER,
398 (char *)&opt, sizeof(opt));
399 }
400 break;
David Howells17926a72007-04-26 15:48:28 -0700401 }
402
David Howells985a5c82016-06-17 11:53:37 +0100403 up_write(&conn->params.local->defrag_sem);
404 _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700405 return ret;
406}
407
408/*
409 * wait for space to appear in the transmit/ACK window
410 * - caller holds the socket locked
411 */
412static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
413 struct rxrpc_call *call,
414 long *timeo)
415{
416 DECLARE_WAITQUEUE(myself, current);
417 int ret;
418
419 _enter(",{%d},%ld",
David Howellsee72b9f2016-03-04 15:58:06 +0000420 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
421 call->acks_winsz),
David Howells17926a72007-04-26 15:48:28 -0700422 *timeo);
423
424 add_wait_queue(&call->tx_waitq, &myself);
425
426 for (;;) {
427 set_current_state(TASK_INTERRUPTIBLE);
428 ret = 0;
David Howellsee72b9f2016-03-04 15:58:06 +0000429 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700430 call->acks_winsz) > 0)
431 break;
432 if (signal_pending(current)) {
433 ret = sock_intr_errno(*timeo);
434 break;
435 }
436
437 release_sock(&rx->sk);
438 *timeo = schedule_timeout(*timeo);
439 lock_sock(&rx->sk);
440 }
441
442 remove_wait_queue(&call->tx_waitq, &myself);
443 set_current_state(TASK_RUNNING);
444 _leave(" = %d", ret);
445 return ret;
446}
447
448/*
449 * attempt to schedule an instant Tx resend
450 */
451static inline void rxrpc_instant_resend(struct rxrpc_call *call)
452{
453 read_lock_bh(&call->state_lock);
454 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
455 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
456 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000457 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700458 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700459 }
460 read_unlock_bh(&call->state_lock);
461}
462
463/*
464 * queue a packet for transmission, set the resend timer and attempt
465 * to send the packet immediately
466 */
467static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
468 bool last)
469{
470 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
471 int ret;
472
473 _net("queue skb %p [%d]", skb, call->acks_head);
474
475 ASSERT(call->acks_window != NULL);
476 call->acks_window[call->acks_head] = (unsigned long) skb;
477 smp_wmb();
478 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
479
480 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
481 _debug("________awaiting reply/ACK__________");
482 write_lock_bh(&call->state_lock);
483 switch (call->state) {
484 case RXRPC_CALL_CLIENT_SEND_REQUEST:
485 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
486 break;
487 case RXRPC_CALL_SERVER_ACK_REQUEST:
488 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
489 if (!last)
490 break;
491 case RXRPC_CALL_SERVER_SEND_REPLY:
492 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
493 break;
494 default:
495 break;
496 }
497 write_unlock_bh(&call->state_lock);
498 }
499
David Howells0d12f8a2016-03-04 15:53:46 +0000500 _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700501
Rusty Russell3db1cd52011-12-19 13:56:45 +0000502 sp->need_resend = false;
David Howells5873c082014-02-07 18:58:44 +0000503 sp->resend_at = jiffies + rxrpc_resend_timeout;
David Howells17926a72007-04-26 15:48:28 -0700504 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
505 _debug("run timer");
506 call->resend_timer.expires = sp->resend_at;
507 add_timer(&call->resend_timer);
508 }
509
510 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
511 * we're ACK'ing the request phase of an incoming call */
512 ret = -EAGAIN;
513 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
514 /* the packet may be freed by rxrpc_process_call() before this
515 * returns */
David Howells985a5c82016-06-17 11:53:37 +0100516 ret = rxrpc_send_data_packet(call->conn, skb);
David Howells17926a72007-04-26 15:48:28 -0700517 _net("sent skb %p", skb);
518 } else {
519 _debug("failed to delete ACK timer");
520 }
521
522 if (ret < 0) {
523 _debug("need instant resend %d", ret);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000524 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700525 rxrpc_instant_resend(call);
526 }
527
528 _leave("");
529}
530
531/*
David Howells0d12f8a2016-03-04 15:53:46 +0000532 * Convert a host-endian header into a network-endian header.
533 */
534static void rxrpc_insert_header(struct sk_buff *skb)
535{
536 struct rxrpc_wire_header whdr;
537 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
538
539 whdr.epoch = htonl(sp->hdr.epoch);
540 whdr.cid = htonl(sp->hdr.cid);
541 whdr.callNumber = htonl(sp->hdr.callNumber);
542 whdr.seq = htonl(sp->hdr.seq);
543 whdr.serial = htonl(sp->hdr.serial);
544 whdr.type = sp->hdr.type;
545 whdr.flags = sp->hdr.flags;
546 whdr.userStatus = sp->hdr.userStatus;
547 whdr.securityIndex = sp->hdr.securityIndex;
548 whdr._rsvd = htons(sp->hdr._rsvd);
549 whdr.serviceId = htons(sp->hdr.serviceId);
550
551 memcpy(skb->head, &whdr, sizeof(whdr));
552}
553
554/*
David Howells17926a72007-04-26 15:48:28 -0700555 * send data through a socket
556 * - must be called in process context
557 * - caller holds the socket locked
558 */
Ying Xue1b784142015-03-02 15:37:48 +0800559static int rxrpc_send_data(struct rxrpc_sock *rx,
David Howells17926a72007-04-26 15:48:28 -0700560 struct rxrpc_call *call,
561 struct msghdr *msg, size_t len)
562{
563 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700564 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700565 struct sock *sk = &rx->sk;
566 long timeo;
567 bool more;
Al Viroaf2b0402014-11-27 21:44:24 -0500568 int ret, copied;
David Howells17926a72007-04-26 15:48:28 -0700569
David Howells17926a72007-04-26 15:48:28 -0700570 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
571
572 /* this should be in poll */
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800573 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
David Howells17926a72007-04-26 15:48:28 -0700574
575 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
576 return -EPIPE;
577
David Howells17926a72007-04-26 15:48:28 -0700578 more = msg->msg_flags & MSG_MORE;
579
580 skb = call->tx_pending;
581 call->tx_pending = NULL;
582
583 copied = 0;
David Howells3af68782015-04-01 14:06:00 +0100584 do {
David Howells17926a72007-04-26 15:48:28 -0700585 if (!skb) {
586 size_t size, chunk, max, space;
587
588 _debug("alloc");
589
David Howellsee72b9f2016-03-04 15:58:06 +0000590 if (CIRC_SPACE(call->acks_head,
591 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700592 call->acks_winsz) <= 0) {
593 ret = -EAGAIN;
594 if (msg->msg_flags & MSG_DONTWAIT)
595 goto maybe_error;
596 ret = rxrpc_wait_for_tx_window(rx, call,
597 &timeo);
598 if (ret < 0)
599 goto maybe_error;
600 }
601
David Howells85f32272016-04-04 14:00:36 +0100602 max = call->conn->params.peer->maxdata;
David Howells17926a72007-04-26 15:48:28 -0700603 max -= call->conn->security_size;
604 max &= ~(call->conn->size_align - 1UL);
605
606 chunk = max;
Al Viro01e97e62014-12-15 21:39:31 -0500607 if (chunk > msg_data_left(msg) && !more)
608 chunk = msg_data_left(msg);
David Howells17926a72007-04-26 15:48:28 -0700609
610 space = chunk + call->conn->size_align;
611 space &= ~(call->conn->size_align - 1UL);
612
613 size = space + call->conn->header_size;
614
615 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
616
617 /* create a buffer that we can retain until it's ACK'd */
618 skb = sock_alloc_send_skb(
619 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
620 if (!skb)
621 goto maybe_error;
622
623 rxrpc_new_skb(skb);
624
625 _debug("ALLOC SEND %p", skb);
626
627 ASSERTCMP(skb->mark, ==, 0);
628
629 _debug("HS: %u", call->conn->header_size);
630 skb_reserve(skb, call->conn->header_size);
631 skb->len += call->conn->header_size;
632
633 sp = rxrpc_skb(skb);
634 sp->remain = chunk;
635 if (sp->remain > skb_tailroom(skb))
636 sp->remain = skb_tailroom(skb);
637
638 _net("skb: hr %d, tr %d, hl %d, rm %d",
639 skb_headroom(skb),
640 skb_tailroom(skb),
641 skb_headlen(skb),
642 sp->remain);
643
644 skb->ip_summed = CHECKSUM_UNNECESSARY;
645 }
646
647 _debug("append");
648 sp = rxrpc_skb(skb);
649
650 /* append next segment of data to the current buffer */
Al Viro01e97e62014-12-15 21:39:31 -0500651 if (msg_data_left(msg) > 0) {
David Howellsaab94832015-04-01 15:48:00 +0100652 int copy = skb_tailroom(skb);
653 ASSERTCMP(copy, >, 0);
Al Viro01e97e62014-12-15 21:39:31 -0500654 if (copy > msg_data_left(msg))
655 copy = msg_data_left(msg);
David Howellsaab94832015-04-01 15:48:00 +0100656 if (copy > sp->remain)
657 copy = sp->remain;
David Howells17926a72007-04-26 15:48:28 -0700658
David Howellsaab94832015-04-01 15:48:00 +0100659 _debug("add");
660 ret = skb_add_data(skb, &msg->msg_iter, copy);
661 _debug("added");
662 if (ret < 0)
663 goto efault;
664 sp->remain -= copy;
665 skb->mark += copy;
666 copied += copy;
David Howellsaab94832015-04-01 15:48:00 +0100667 }
David Howells17926a72007-04-26 15:48:28 -0700668
669 /* check for the far side aborting the call or a network error
670 * occurring */
671 if (call->state > RXRPC_CALL_COMPLETE)
672 goto call_aborted;
673
674 /* add the packet to the send queue if it's now full */
David Howells382d7972015-04-01 15:43:26 +0100675 if (sp->remain <= 0 ||
Al Viro01e97e62014-12-15 21:39:31 -0500676 (msg_data_left(msg) == 0 && !more)) {
David Howells17926a72007-04-26 15:48:28 -0700677 struct rxrpc_connection *conn = call->conn;
David Howellse8388eb2014-02-14 20:05:32 +0000678 uint32_t seq;
David Howells17926a72007-04-26 15:48:28 -0700679 size_t pad;
680
681 /* pad out if we're using security */
David Howellse0e4d822016-04-07 17:23:58 +0100682 if (conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700683 pad = conn->security_size + skb->mark;
684 pad = conn->size_align - pad;
685 pad &= conn->size_align - 1;
686 _debug("pad %zu", pad);
687 if (pad)
688 memset(skb_put(skb, pad), 0, pad);
689 }
690
David Howellse8388eb2014-02-14 20:05:32 +0000691 seq = atomic_inc_return(&call->sequence);
692
David Howells19ffa012016-04-04 14:00:36 +0100693 sp->hdr.epoch = conn->proto.epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000694 sp->hdr.cid = call->cid;
David Howells17926a72007-04-26 15:48:28 -0700695 sp->hdr.callNumber = call->call_id;
David Howells0d12f8a2016-03-04 15:53:46 +0000696 sp->hdr.seq = seq;
697 sp->hdr.serial = atomic_inc_return(&conn->serial);
698 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
David Howells17926a72007-04-26 15:48:28 -0700699 sp->hdr.userStatus = 0;
700 sp->hdr.securityIndex = conn->security_ix;
David Howells0d12f8a2016-03-04 15:53:46 +0000701 sp->hdr._rsvd = 0;
702 sp->hdr.serviceId = call->service_id;
David Howells17926a72007-04-26 15:48:28 -0700703
704 sp->hdr.flags = conn->out_clientflag;
Al Viro01e97e62014-12-15 21:39:31 -0500705 if (msg_data_left(msg) == 0 && !more)
David Howells17926a72007-04-26 15:48:28 -0700706 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howellsee72b9f2016-03-04 15:58:06 +0000707 else if (CIRC_SPACE(call->acks_head,
708 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700709 call->acks_winsz) > 1)
710 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howellse8388eb2014-02-14 20:05:32 +0000711 if (more && seq & 1)
712 sp->hdr.flags |= RXRPC_REQUEST_ACK;
David Howells17926a72007-04-26 15:48:28 -0700713
David Howellse0e4d822016-04-07 17:23:58 +0100714 ret = conn->security->secure_packet(
David Howells17926a72007-04-26 15:48:28 -0700715 call, skb, skb->mark,
David Howells0d12f8a2016-03-04 15:53:46 +0000716 skb->head + sizeof(struct rxrpc_wire_header));
David Howells17926a72007-04-26 15:48:28 -0700717 if (ret < 0)
718 goto out;
719
David Howells0d12f8a2016-03-04 15:53:46 +0000720 rxrpc_insert_header(skb);
Al Viro01e97e62014-12-15 21:39:31 -0500721 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
David Howells17926a72007-04-26 15:48:28 -0700722 skb = NULL;
723 }
Al Viro01e97e62014-12-15 21:39:31 -0500724 } while (msg_data_left(msg) > 0);
David Howells17926a72007-04-26 15:48:28 -0700725
David Howells19e64542007-06-18 23:30:41 -0700726success:
727 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700728out:
729 call->tx_pending = skb;
730 _leave(" = %d", ret);
731 return ret;
732
733call_aborted:
734 rxrpc_free_skb(skb);
735 if (call->state == RXRPC_CALL_NETWORK_ERROR)
David Howellsf66d7492016-04-04 14:00:34 +0100736 ret = call->error_report < RXRPC_LOCAL_ERROR_OFFSET ?
737 call->error_report :
738 call->error_report - RXRPC_LOCAL_ERROR_OFFSET;
David Howells17926a72007-04-26 15:48:28 -0700739 else
740 ret = -ECONNABORTED;
741 _leave(" = %d", ret);
742 return ret;
743
744maybe_error:
745 if (copied)
David Howells19e64542007-06-18 23:30:41 -0700746 goto success;
David Howells17926a72007-04-26 15:48:28 -0700747 goto out;
748
749efault:
750 ret = -EFAULT;
751 goto out;
752}