blob: 8a9917cba6fe7ea77c723cce40da919de1c33598 [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_call *call;
144 struct key *key;
David Howells2341e072016-06-09 23:02:51 +0100145
146 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
147
148 _enter("");
149
150 if (!msg->msg_name)
151 return ERR_PTR(-EDESTADDRREQ);
152
David Howells19ffa012016-04-04 14:00:36 +0100153 key = rx->key;
154 if (key && !rx->key->payload.data[0])
155 key = NULL;
156
157 memset(&cp, 0, sizeof(cp));
158 cp.local = rx->local;
159 cp.key = rx->key;
160 cp.security_level = rx->min_sec_level;
David Howellscc8feb82016-04-04 14:00:37 +0100161 cp.exclusive = rx->exclusive | exclusive;
David Howells19ffa012016-04-04 14:00:36 +0100162 cp.service_id = srx->srx_service;
David Howellsaa390bb2016-06-17 10:06:56 +0100163 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, GFP_KERNEL);
David Howells2341e072016-06-09 23:02:51 +0100164
165 _leave(" = %p\n", call);
166 return call;
David Howells2341e072016-06-09 23:02:51 +0100167}
168
169/*
David Howells17926a72007-04-26 15:48:28 -0700170 * send a message forming part of a client call through an RxRPC socket
171 * - caller holds the socket locked
172 * - the socket may be either a client socket or a server socket
173 */
David Howells2341e072016-06-09 23:02:51 +0100174int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700175{
David Howells17926a72007-04-26 15:48:28 -0700176 enum rxrpc_command cmd;
177 struct rxrpc_call *call;
178 unsigned long user_call_ID = 0;
David Howellscc8feb82016-04-04 14:00:37 +0100179 bool exclusive = false;
David Howells17926a72007-04-26 15:48:28 -0700180 u32 abort_code = 0;
181 int ret;
182
183 _enter("");
184
David Howellscc8feb82016-04-04 14:00:37 +0100185 ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
186 &exclusive);
David Howells17926a72007-04-26 15:48:28 -0700187 if (ret < 0)
188 return ret;
189
David Howells2341e072016-06-09 23:02:51 +0100190 if (cmd == RXRPC_CMD_ACCEPT) {
191 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
192 return -EINVAL;
193 call = rxrpc_accept_call(rx, user_call_ID);
194 if (IS_ERR(call))
195 return PTR_ERR(call);
196 rxrpc_put_call(call);
197 return 0;
David Howells17926a72007-04-26 15:48:28 -0700198 }
199
David Howells2341e072016-06-09 23:02:51 +0100200 call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
201 if (!call) {
202 if (cmd != RXRPC_CMD_SEND_DATA)
203 return -EBADSLT;
David Howellscc8feb82016-04-04 14:00:37 +0100204 call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
205 exclusive);
David Howells2341e072016-06-09 23:02:51 +0100206 if (IS_ERR(call))
207 return PTR_ERR(call);
David Howells17926a72007-04-26 15:48:28 -0700208 }
209
210 _debug("CALL %d USR %lx ST %d on CONN %p",
211 call->debug_id, call->user_call_ID, call->state, call->conn);
212
213 if (call->state >= RXRPC_CALL_COMPLETE) {
214 /* it's too late for this call */
David Howells2341e072016-06-09 23:02:51 +0100215 ret = -ECONNRESET;
David Howells17926a72007-04-26 15:48:28 -0700216 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
217 rxrpc_send_abort(call, abort_code);
David Howells2341e072016-06-09 23:02:51 +0100218 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700219 } else if (cmd != RXRPC_CMD_SEND_DATA) {
220 ret = -EINVAL;
David Howellsdabe5a72016-08-23 15:27:24 +0100221 } else if (rxrpc_is_client_call(call) &&
David Howells2341e072016-06-09 23:02:51 +0100222 call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
David Howells17926a72007-04-26 15:48:28 -0700223 /* request phase complete for this client call */
224 ret = -EPROTO;
David Howellsdabe5a72016-08-23 15:27:24 +0100225 } else if (rxrpc_is_service_call(call) &&
David Howells2341e072016-06-09 23:02:51 +0100226 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
227 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
228 /* Reply phase not begun or not complete for service call. */
229 ret = -EPROTO;
David Howells17926a72007-04-26 15:48:28 -0700230 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800231 ret = rxrpc_send_data(rx, call, msg, len);
David Howells17926a72007-04-26 15:48:28 -0700232 }
233
234 rxrpc_put_call(call);
235 _leave(" = %d", ret);
236 return ret;
237}
238
David Howells651350d2007-04-26 15:50:17 -0700239/**
240 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
241 * @call: The call to send data through
242 * @msg: The data to send
243 * @len: The amount of data to send
244 *
245 * Allow a kernel service to send data on a call. The call must be in an state
246 * appropriate to sending data. No control data should be supplied in @msg,
247 * nor should an address be supplied. MSG_MORE should be flagged if there's
248 * more data to come, otherwise this data will end the transmission phase.
249 */
250int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
251 size_t len)
252{
253 int ret;
254
255 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
256
257 ASSERTCMP(msg->msg_name, ==, NULL);
258 ASSERTCMP(msg->msg_control, ==, NULL);
259
260 lock_sock(&call->socket->sk);
261
262 _debug("CALL %d USR %lx ST %d on CONN %p",
263 call->debug_id, call->user_call_ID, call->state, call->conn);
264
265 if (call->state >= RXRPC_CALL_COMPLETE) {
266 ret = -ESHUTDOWN; /* it's too late for this call */
267 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
268 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
269 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
270 ret = -EPROTO; /* request phase complete for this client call */
271 } else {
Ying Xue1b784142015-03-02 15:37:48 +0800272 ret = rxrpc_send_data(call->socket, call, msg, len);
David Howells651350d2007-04-26 15:50:17 -0700273 }
274
275 release_sock(&call->socket->sk);
276 _leave(" = %d", ret);
277 return ret;
278}
279
280EXPORT_SYMBOL(rxrpc_kernel_send_data);
281
Ben Hutchings2c530402012-07-10 10:55:09 +0000282/**
David Howells651350d2007-04-26 15:50:17 -0700283 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
284 * @call: The call to be aborted
285 * @abort_code: The abort code to stick into the ABORT packet
286 *
287 * Allow a kernel service to abort a call, if it's still in an abortable state.
288 */
289void rxrpc_kernel_abort_call(struct rxrpc_call *call, u32 abort_code)
290{
291 _enter("{%d},%d", call->debug_id, abort_code);
292
293 lock_sock(&call->socket->sk);
294
295 _debug("CALL %d USR %lx ST %d on CONN %p",
296 call->debug_id, call->user_call_ID, call->state, call->conn);
297
298 if (call->state < RXRPC_CALL_COMPLETE)
299 rxrpc_send_abort(call, abort_code);
300
301 release_sock(&call->socket->sk);
302 _leave("");
303}
304
305EXPORT_SYMBOL(rxrpc_kernel_abort_call);
306
David Howells17926a72007-04-26 15:48:28 -0700307/*
David Howells17926a72007-04-26 15:48:28 -0700308 * send a packet through the transport endpoint
309 */
David Howells985a5c82016-06-17 11:53:37 +0100310int rxrpc_send_data_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700311{
312 struct kvec iov[1];
313 struct msghdr msg;
314 int ret, opt;
315
316 _enter(",{%d}", skb->len);
317
318 iov[0].iov_base = skb->head;
319 iov[0].iov_len = skb->len;
320
David Howells985a5c82016-06-17 11:53:37 +0100321 msg.msg_name = &conn->params.peer->srx.transport;
322 msg.msg_namelen = conn->params.peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700323 msg.msg_control = NULL;
324 msg.msg_controllen = 0;
325 msg.msg_flags = 0;
326
327 /* send the packet with the don't fragment bit set if we currently
328 * think it's small enough */
David Howells985a5c82016-06-17 11:53:37 +0100329 if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) {
330 down_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700331 /* send the packet by UDP
332 * - returns -EMSGSIZE if UDP would have to fragment the packet
333 * to go out of the interface
334 * - in which case, we'll have processed the ICMP error
335 * message and update the peer record
336 */
David Howells985a5c82016-06-17 11:53:37 +0100337 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
David Howells17926a72007-04-26 15:48:28 -0700338 iov[0].iov_len);
339
David Howells985a5c82016-06-17 11:53:37 +0100340 up_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700341 if (ret == -EMSGSIZE)
342 goto send_fragmentable;
343
David Howells985a5c82016-06-17 11:53:37 +0100344 _leave(" = %d [%u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700345 return ret;
346 }
347
348send_fragmentable:
349 /* attempt to send this message with fragmentation enabled */
350 _debug("send fragment");
351
David Howells985a5c82016-06-17 11:53:37 +0100352 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700353
David Howells985a5c82016-06-17 11:53:37 +0100354 switch (conn->params.local->srx.transport.family) {
355 case AF_INET:
356 opt = IP_PMTUDISC_DONT;
357 ret = kernel_setsockopt(conn->params.local->socket,
358 SOL_IP, IP_MTU_DISCOVER,
359 (char *)&opt, sizeof(opt));
360 if (ret == 0) {
361 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
362 iov[0].iov_len);
363
364 opt = IP_PMTUDISC_DO;
365 kernel_setsockopt(conn->params.local->socket, SOL_IP,
366 IP_MTU_DISCOVER,
367 (char *)&opt, sizeof(opt));
368 }
369 break;
David Howells17926a72007-04-26 15:48:28 -0700370 }
371
David Howells985a5c82016-06-17 11:53:37 +0100372 up_write(&conn->params.local->defrag_sem);
373 _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700374 return ret;
375}
376
377/*
378 * wait for space to appear in the transmit/ACK window
379 * - caller holds the socket locked
380 */
381static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
382 struct rxrpc_call *call,
383 long *timeo)
384{
385 DECLARE_WAITQUEUE(myself, current);
386 int ret;
387
388 _enter(",{%d},%ld",
David Howellsee72b9f2016-03-04 15:58:06 +0000389 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
390 call->acks_winsz),
David Howells17926a72007-04-26 15:48:28 -0700391 *timeo);
392
David Howells45025bc2016-08-24 07:30:52 +0100393 add_wait_queue(&call->waitq, &myself);
David Howells17926a72007-04-26 15:48:28 -0700394
395 for (;;) {
396 set_current_state(TASK_INTERRUPTIBLE);
397 ret = 0;
David Howellsee72b9f2016-03-04 15:58:06 +0000398 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700399 call->acks_winsz) > 0)
400 break;
401 if (signal_pending(current)) {
402 ret = sock_intr_errno(*timeo);
403 break;
404 }
405
406 release_sock(&rx->sk);
407 *timeo = schedule_timeout(*timeo);
408 lock_sock(&rx->sk);
409 }
410
David Howells45025bc2016-08-24 07:30:52 +0100411 remove_wait_queue(&call->waitq, &myself);
David Howells17926a72007-04-26 15:48:28 -0700412 set_current_state(TASK_RUNNING);
413 _leave(" = %d", ret);
414 return ret;
415}
416
417/*
418 * attempt to schedule an instant Tx resend
419 */
420static inline void rxrpc_instant_resend(struct rxrpc_call *call)
421{
422 read_lock_bh(&call->state_lock);
423 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
424 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
425 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000426 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700427 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700428 }
429 read_unlock_bh(&call->state_lock);
430}
431
432/*
433 * queue a packet for transmission, set the resend timer and attempt
434 * to send the packet immediately
435 */
436static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
437 bool last)
438{
439 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
440 int ret;
441
442 _net("queue skb %p [%d]", skb, call->acks_head);
443
444 ASSERT(call->acks_window != NULL);
445 call->acks_window[call->acks_head] = (unsigned long) skb;
446 smp_wmb();
447 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
448
449 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
450 _debug("________awaiting reply/ACK__________");
451 write_lock_bh(&call->state_lock);
452 switch (call->state) {
453 case RXRPC_CALL_CLIENT_SEND_REQUEST:
454 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
455 break;
456 case RXRPC_CALL_SERVER_ACK_REQUEST:
457 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
458 if (!last)
459 break;
460 case RXRPC_CALL_SERVER_SEND_REPLY:
461 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
462 break;
463 default:
464 break;
465 }
466 write_unlock_bh(&call->state_lock);
467 }
468
David Howells0d12f8a2016-03-04 15:53:46 +0000469 _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700470
Rusty Russell3db1cd52011-12-19 13:56:45 +0000471 sp->need_resend = false;
David Howells5873c082014-02-07 18:58:44 +0000472 sp->resend_at = jiffies + rxrpc_resend_timeout;
David Howells17926a72007-04-26 15:48:28 -0700473 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
474 _debug("run timer");
475 call->resend_timer.expires = sp->resend_at;
476 add_timer(&call->resend_timer);
477 }
478
479 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
480 * we're ACK'ing the request phase of an incoming call */
481 ret = -EAGAIN;
482 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
483 /* the packet may be freed by rxrpc_process_call() before this
484 * returns */
David Howells45025bc2016-08-24 07:30:52 +0100485 if (rxrpc_is_client_call(call))
486 rxrpc_expose_client_call(call);
David Howells985a5c82016-06-17 11:53:37 +0100487 ret = rxrpc_send_data_packet(call->conn, skb);
David Howells17926a72007-04-26 15:48:28 -0700488 _net("sent skb %p", skb);
489 } else {
490 _debug("failed to delete ACK timer");
491 }
492
493 if (ret < 0) {
494 _debug("need instant resend %d", ret);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000495 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700496 rxrpc_instant_resend(call);
497 }
498
499 _leave("");
500}
501
502/*
David Howells0d12f8a2016-03-04 15:53:46 +0000503 * Convert a host-endian header into a network-endian header.
504 */
505static void rxrpc_insert_header(struct sk_buff *skb)
506{
507 struct rxrpc_wire_header whdr;
508 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
509
510 whdr.epoch = htonl(sp->hdr.epoch);
511 whdr.cid = htonl(sp->hdr.cid);
512 whdr.callNumber = htonl(sp->hdr.callNumber);
513 whdr.seq = htonl(sp->hdr.seq);
514 whdr.serial = htonl(sp->hdr.serial);
515 whdr.type = sp->hdr.type;
516 whdr.flags = sp->hdr.flags;
517 whdr.userStatus = sp->hdr.userStatus;
518 whdr.securityIndex = sp->hdr.securityIndex;
519 whdr._rsvd = htons(sp->hdr._rsvd);
520 whdr.serviceId = htons(sp->hdr.serviceId);
521
522 memcpy(skb->head, &whdr, sizeof(whdr));
523}
524
525/*
David Howells17926a72007-04-26 15:48:28 -0700526 * send data through a socket
527 * - must be called in process context
528 * - caller holds the socket locked
529 */
Ying Xue1b784142015-03-02 15:37:48 +0800530static int rxrpc_send_data(struct rxrpc_sock *rx,
David Howells17926a72007-04-26 15:48:28 -0700531 struct rxrpc_call *call,
532 struct msghdr *msg, size_t len)
533{
534 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700535 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700536 struct sock *sk = &rx->sk;
537 long timeo;
538 bool more;
Al Viroaf2b0402014-11-27 21:44:24 -0500539 int ret, copied;
David Howells17926a72007-04-26 15:48:28 -0700540
David Howells17926a72007-04-26 15:48:28 -0700541 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
542
543 /* this should be in poll */
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800544 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
David Howells17926a72007-04-26 15:48:28 -0700545
546 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
547 return -EPIPE;
548
David Howells17926a72007-04-26 15:48:28 -0700549 more = msg->msg_flags & MSG_MORE;
550
551 skb = call->tx_pending;
552 call->tx_pending = NULL;
David Howellsdf844fd2016-08-23 15:27:24 +0100553 rxrpc_see_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700554
555 copied = 0;
David Howells3af68782015-04-01 14:06:00 +0100556 do {
David Howells17926a72007-04-26 15:48:28 -0700557 if (!skb) {
558 size_t size, chunk, max, space;
559
560 _debug("alloc");
561
David Howellsee72b9f2016-03-04 15:58:06 +0000562 if (CIRC_SPACE(call->acks_head,
563 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700564 call->acks_winsz) <= 0) {
565 ret = -EAGAIN;
566 if (msg->msg_flags & MSG_DONTWAIT)
567 goto maybe_error;
568 ret = rxrpc_wait_for_tx_window(rx, call,
569 &timeo);
570 if (ret < 0)
571 goto maybe_error;
572 }
573
David Howells85f32272016-04-04 14:00:36 +0100574 max = call->conn->params.peer->maxdata;
David Howells17926a72007-04-26 15:48:28 -0700575 max -= call->conn->security_size;
576 max &= ~(call->conn->size_align - 1UL);
577
578 chunk = max;
Al Viro01e97e62014-12-15 21:39:31 -0500579 if (chunk > msg_data_left(msg) && !more)
580 chunk = msg_data_left(msg);
David Howells17926a72007-04-26 15:48:28 -0700581
582 space = chunk + call->conn->size_align;
583 space &= ~(call->conn->size_align - 1UL);
584
585 size = space + call->conn->header_size;
586
587 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
588
589 /* create a buffer that we can retain until it's ACK'd */
590 skb = sock_alloc_send_skb(
591 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
592 if (!skb)
593 goto maybe_error;
594
595 rxrpc_new_skb(skb);
596
597 _debug("ALLOC SEND %p", skb);
598
599 ASSERTCMP(skb->mark, ==, 0);
600
601 _debug("HS: %u", call->conn->header_size);
602 skb_reserve(skb, call->conn->header_size);
603 skb->len += call->conn->header_size;
604
605 sp = rxrpc_skb(skb);
606 sp->remain = chunk;
607 if (sp->remain > skb_tailroom(skb))
608 sp->remain = skb_tailroom(skb);
609
610 _net("skb: hr %d, tr %d, hl %d, rm %d",
611 skb_headroom(skb),
612 skb_tailroom(skb),
613 skb_headlen(skb),
614 sp->remain);
615
616 skb->ip_summed = CHECKSUM_UNNECESSARY;
617 }
618
619 _debug("append");
620 sp = rxrpc_skb(skb);
621
622 /* append next segment of data to the current buffer */
Al Viro01e97e62014-12-15 21:39:31 -0500623 if (msg_data_left(msg) > 0) {
David Howellsaab94832015-04-01 15:48:00 +0100624 int copy = skb_tailroom(skb);
625 ASSERTCMP(copy, >, 0);
Al Viro01e97e62014-12-15 21:39:31 -0500626 if (copy > msg_data_left(msg))
627 copy = msg_data_left(msg);
David Howellsaab94832015-04-01 15:48:00 +0100628 if (copy > sp->remain)
629 copy = sp->remain;
David Howells17926a72007-04-26 15:48:28 -0700630
David Howellsaab94832015-04-01 15:48:00 +0100631 _debug("add");
632 ret = skb_add_data(skb, &msg->msg_iter, copy);
633 _debug("added");
634 if (ret < 0)
635 goto efault;
636 sp->remain -= copy;
637 skb->mark += copy;
638 copied += copy;
David Howellsaab94832015-04-01 15:48:00 +0100639 }
David Howells17926a72007-04-26 15:48:28 -0700640
641 /* check for the far side aborting the call or a network error
642 * occurring */
643 if (call->state > RXRPC_CALL_COMPLETE)
644 goto call_aborted;
645
646 /* add the packet to the send queue if it's now full */
David Howells382d7972015-04-01 15:43:26 +0100647 if (sp->remain <= 0 ||
Al Viro01e97e62014-12-15 21:39:31 -0500648 (msg_data_left(msg) == 0 && !more)) {
David Howells17926a72007-04-26 15:48:28 -0700649 struct rxrpc_connection *conn = call->conn;
David Howellse8388eb2014-02-14 20:05:32 +0000650 uint32_t seq;
David Howells17926a72007-04-26 15:48:28 -0700651 size_t pad;
652
653 /* pad out if we're using security */
David Howellse0e4d822016-04-07 17:23:58 +0100654 if (conn->security_ix) {
David Howells17926a72007-04-26 15:48:28 -0700655 pad = conn->security_size + skb->mark;
656 pad = conn->size_align - pad;
657 pad &= conn->size_align - 1;
658 _debug("pad %zu", pad);
659 if (pad)
660 memset(skb_put(skb, pad), 0, pad);
661 }
662
David Howellse8388eb2014-02-14 20:05:32 +0000663 seq = atomic_inc_return(&call->sequence);
664
David Howells19ffa012016-04-04 14:00:36 +0100665 sp->hdr.epoch = conn->proto.epoch;
David Howells0d12f8a2016-03-04 15:53:46 +0000666 sp->hdr.cid = call->cid;
David Howells17926a72007-04-26 15:48:28 -0700667 sp->hdr.callNumber = call->call_id;
David Howells0d12f8a2016-03-04 15:53:46 +0000668 sp->hdr.seq = seq;
669 sp->hdr.serial = atomic_inc_return(&conn->serial);
670 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
David Howells17926a72007-04-26 15:48:28 -0700671 sp->hdr.userStatus = 0;
672 sp->hdr.securityIndex = conn->security_ix;
David Howells0d12f8a2016-03-04 15:53:46 +0000673 sp->hdr._rsvd = 0;
674 sp->hdr.serviceId = call->service_id;
David Howells17926a72007-04-26 15:48:28 -0700675
676 sp->hdr.flags = conn->out_clientflag;
Al Viro01e97e62014-12-15 21:39:31 -0500677 if (msg_data_left(msg) == 0 && !more)
David Howells17926a72007-04-26 15:48:28 -0700678 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howellsee72b9f2016-03-04 15:58:06 +0000679 else if (CIRC_SPACE(call->acks_head,
680 ACCESS_ONCE(call->acks_tail),
David Howells17926a72007-04-26 15:48:28 -0700681 call->acks_winsz) > 1)
682 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howellse8388eb2014-02-14 20:05:32 +0000683 if (more && seq & 1)
684 sp->hdr.flags |= RXRPC_REQUEST_ACK;
David Howells17926a72007-04-26 15:48:28 -0700685
David Howellse0e4d822016-04-07 17:23:58 +0100686 ret = conn->security->secure_packet(
David Howells17926a72007-04-26 15:48:28 -0700687 call, skb, skb->mark,
David Howells0d12f8a2016-03-04 15:53:46 +0000688 skb->head + sizeof(struct rxrpc_wire_header));
David Howells17926a72007-04-26 15:48:28 -0700689 if (ret < 0)
690 goto out;
691
David Howells0d12f8a2016-03-04 15:53:46 +0000692 rxrpc_insert_header(skb);
Al Viro01e97e62014-12-15 21:39:31 -0500693 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
David Howells17926a72007-04-26 15:48:28 -0700694 skb = NULL;
695 }
Al Viro01e97e62014-12-15 21:39:31 -0500696 } while (msg_data_left(msg) > 0);
David Howells17926a72007-04-26 15:48:28 -0700697
David Howells19e64542007-06-18 23:30:41 -0700698success:
699 ret = copied;
David Howells17926a72007-04-26 15:48:28 -0700700out:
701 call->tx_pending = skb;
702 _leave(" = %d", ret);
703 return ret;
704
705call_aborted:
706 rxrpc_free_skb(skb);
707 if (call->state == RXRPC_CALL_NETWORK_ERROR)
David Howellsf66d7492016-04-04 14:00:34 +0100708 ret = call->error_report < RXRPC_LOCAL_ERROR_OFFSET ?
709 call->error_report :
710 call->error_report - RXRPC_LOCAL_ERROR_OFFSET;
David Howells17926a72007-04-26 15:48:28 -0700711 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}