blob: 9ea6f972767e7c902c4fbc04a1390905800f3ad6 [file] [log] [blame]
David Howells0b58b8a2016-09-02 22:39:45 +01001/* AF_RXRPC sendmsg() implementation.
2 *
3 * Copyright (C) 2007, 2016 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/net.h>
15#include <linux/gfp.h>
16#include <linux/skbuff.h>
17#include <linux/export.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010018#include <linux/sched/signal.h>
19
David Howells0b58b8a2016-09-02 22:39:45 +010020#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include "ar-internal.h"
23
David Howells3dc20f02016-09-04 13:25:21 +010024enum rxrpc_command {
25 RXRPC_CMD_SEND_DATA, /* send data message */
26 RXRPC_CMD_SEND_ABORT, /* request abort generation */
27 RXRPC_CMD_ACCEPT, /* [server] accept incoming call */
28 RXRPC_CMD_REJECT_BUSY, /* [server] reject a call as busy */
29};
30
David Howells3ab26a62017-06-07 14:41:52 +010031struct rxrpc_send_params {
David Howellse754eba2017-06-07 12:40:03 +010032 s64 tx_total_len; /* Total Tx data length (if send data) */
David Howells3ab26a62017-06-07 14:41:52 +010033 unsigned long user_call_ID; /* User's call ID */
34 u32 abort_code; /* Abort code to Tx (if abort) */
35 enum rxrpc_command command : 8; /* The command to implement */
36 bool exclusive; /* Shared or exclusive call */
37 bool upgrade; /* If the connection is upgradeable */
38};
39
David Howells0b58b8a2016-09-02 22:39:45 +010040/*
41 * wait for space to appear in the transmit/ACK window
42 * - caller holds the socket locked
43 */
44static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
45 struct rxrpc_call *call,
46 long *timeo)
47{
48 DECLARE_WAITQUEUE(myself, current);
49 int ret;
50
David Howells248f2192016-09-08 11:10:12 +010051 _enter(",{%u,%u,%u}",
52 call->tx_hard_ack, call->tx_top, call->tx_winsize);
David Howells0b58b8a2016-09-02 22:39:45 +010053
54 add_wait_queue(&call->waitq, &myself);
55
56 for (;;) {
57 set_current_state(TASK_INTERRUPTIBLE);
58 ret = 0;
David Howells57494342016-09-24 18:05:27 +010059 if (call->tx_top - call->tx_hard_ack <
60 min_t(unsigned int, call->tx_winsize,
61 call->cong_cwnd + call->cong_extra))
David Howells0b58b8a2016-09-02 22:39:45 +010062 break;
David Howells248f2192016-09-08 11:10:12 +010063 if (call->state >= RXRPC_CALL_COMPLETE) {
David Howellsbd2db2d2017-08-29 10:18:43 +010064 ret = call->error;
David Howells248f2192016-09-08 11:10:12 +010065 break;
66 }
David Howells0b58b8a2016-09-02 22:39:45 +010067 if (signal_pending(current)) {
68 ret = sock_intr_errno(*timeo);
69 break;
70 }
71
David Howellsa124fe32016-09-17 10:49:13 +010072 trace_rxrpc_transmit(call, rxrpc_transmit_wait);
David Howells540b1c42017-02-27 15:43:06 +000073 mutex_unlock(&call->user_mutex);
David Howells0b58b8a2016-09-02 22:39:45 +010074 *timeo = schedule_timeout(*timeo);
David Howells540b1c42017-02-27 15:43:06 +000075 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
76 ret = sock_intr_errno(*timeo);
77 break;
78 }
David Howells0b58b8a2016-09-02 22:39:45 +010079 }
80
81 remove_wait_queue(&call->waitq, &myself);
82 set_current_state(TASK_RUNNING);
83 _leave(" = %d", ret);
84 return ret;
85}
86
87/*
David Howells248f2192016-09-08 11:10:12 +010088 * Schedule an instant Tx resend.
David Howells0b58b8a2016-09-02 22:39:45 +010089 */
David Howells248f2192016-09-08 11:10:12 +010090static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix)
David Howells0b58b8a2016-09-02 22:39:45 +010091{
David Howells248f2192016-09-08 11:10:12 +010092 spin_lock_bh(&call->lock);
93
94 if (call->state < RXRPC_CALL_COMPLETE) {
95 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
96 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells0b58b8a2016-09-02 22:39:45 +010097 rxrpc_queue_call(call);
98 }
David Howells248f2192016-09-08 11:10:12 +010099
100 spin_unlock_bh(&call->lock);
David Howells0b58b8a2016-09-02 22:39:45 +0100101}
102
103/*
David Howellse8332512017-08-29 10:18:56 +0100104 * Notify the owner of the call that the transmit phase is ended and the last
105 * packet has been queued.
106 */
107static void rxrpc_notify_end_tx(struct rxrpc_sock *rx, struct rxrpc_call *call,
108 rxrpc_notify_end_tx_t notify_end_tx)
109{
110 if (notify_end_tx)
111 notify_end_tx(&rx->sk, call, call->user_call_ID);
112}
113
114/*
David Howells248f2192016-09-08 11:10:12 +0100115 * Queue a DATA packet for transmission, set the resend timeout and send the
116 * packet immediately
David Howells0b58b8a2016-09-02 22:39:45 +0100117 */
David Howellse8332512017-08-29 10:18:56 +0100118static void rxrpc_queue_packet(struct rxrpc_sock *rx, struct rxrpc_call *call,
119 struct sk_buff *skb, bool last,
120 rxrpc_notify_end_tx_t notify_end_tx)
David Howells0b58b8a2016-09-02 22:39:45 +0100121{
122 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells248f2192016-09-08 11:10:12 +0100123 rxrpc_seq_t seq = sp->hdr.seq;
124 int ret, ix;
David Howells70790db2016-09-23 12:39:22 +0100125 u8 annotation = RXRPC_TX_ANNO_UNACK;
David Howells0b58b8a2016-09-02 22:39:45 +0100126
David Howells248f2192016-09-08 11:10:12 +0100127 _net("queue skb %p [%d]", skb, seq);
David Howells0b58b8a2016-09-02 22:39:45 +0100128
David Howells248f2192016-09-08 11:10:12 +0100129 ASSERTCMP(seq, ==, call->tx_top + 1);
130
David Howellsc038a582017-08-29 10:19:01 +0100131 if (last) {
David Howells70790db2016-09-23 12:39:22 +0100132 annotation |= RXRPC_TX_ANNO_LAST;
David Howellsc038a582017-08-29 10:19:01 +0100133 set_bit(RXRPC_CALL_TX_LASTQ, &call->flags);
134 }
David Howells70790db2016-09-23 12:39:22 +0100135
David Howellsb24d2892016-09-23 13:17:33 +0100136 /* We have to set the timestamp before queueing as the retransmit
137 * algorithm can see the packet as soon as we queue it.
138 */
139 skb->tstamp = ktime_get_real();
140
David Howells248f2192016-09-08 11:10:12 +0100141 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells71f3ca42016-09-17 10:49:14 +0100142 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
David Howells70790db2016-09-23 12:39:22 +0100143 call->rxtx_annotations[ix] = annotation;
David Howells0b58b8a2016-09-02 22:39:45 +0100144 smp_wmb();
David Howells248f2192016-09-08 11:10:12 +0100145 call->rxtx_buffer[ix] = skb;
146 call->tx_top = seq;
David Howells70790db2016-09-23 12:39:22 +0100147 if (last)
David Howellsa124fe32016-09-17 10:49:13 +0100148 trace_rxrpc_transmit(call, rxrpc_transmit_queue_last);
David Howells70790db2016-09-23 12:39:22 +0100149 else
David Howellsa124fe32016-09-17 10:49:13 +0100150 trace_rxrpc_transmit(call, rxrpc_transmit_queue);
David Howells0b58b8a2016-09-02 22:39:45 +0100151
152 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
153 _debug("________awaiting reply/ACK__________");
154 write_lock_bh(&call->state_lock);
155 switch (call->state) {
156 case RXRPC_CALL_CLIENT_SEND_REQUEST:
157 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
David Howellse8332512017-08-29 10:18:56 +0100158 rxrpc_notify_end_tx(rx, call, notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100159 break;
160 case RXRPC_CALL_SERVER_ACK_REQUEST:
161 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
David Howells9749fd22016-10-06 08:11:50 +0100162 call->ack_at = call->expire_at;
163 if (call->ackr_reason == RXRPC_ACK_DELAY)
164 call->ackr_reason = 0;
165 __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply,
166 ktime_get_real());
David Howells0b58b8a2016-09-02 22:39:45 +0100167 if (!last)
168 break;
169 case RXRPC_CALL_SERVER_SEND_REPLY:
170 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
David Howellse8332512017-08-29 10:18:56 +0100171 rxrpc_notify_end_tx(rx, call, notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100172 break;
173 default:
174 break;
175 }
176 write_unlock_bh(&call->state_lock);
177 }
178
David Howells248f2192016-09-08 11:10:12 +0100179 if (seq == 1 && rxrpc_is_client_call(call))
180 rxrpc_expose_client_call(call);
181
David Howellsa1767072016-09-29 22:37:15 +0100182 ret = rxrpc_send_data_packet(call, skb, false);
David Howells0b58b8a2016-09-02 22:39:45 +0100183 if (ret < 0) {
184 _debug("need instant resend %d", ret);
David Howells248f2192016-09-08 11:10:12 +0100185 rxrpc_instant_resend(call, ix);
David Howellsdfc3da42016-09-23 12:39:23 +0100186 } else {
David Howellsdf0adc72016-09-26 22:12:49 +0100187 ktime_t now = ktime_get_real(), resend_at;
David Howellsdfc3da42016-09-23 12:39:23 +0100188
David Howellsdf0adc72016-09-26 22:12:49 +0100189 resend_at = ktime_add_ms(now, rxrpc_resend_timeout);
David Howellsdfc3da42016-09-23 12:39:23 +0100190
David Howellsdf0adc72016-09-26 22:12:49 +0100191 if (ktime_before(resend_at, call->resend_at)) {
David Howellsdfc3da42016-09-23 12:39:23 +0100192 call->resend_at = resend_at;
David Howellsdf0adc72016-09-26 22:12:49 +0100193 rxrpc_set_timer(call, rxrpc_timer_set_for_send, now);
David Howellsdfc3da42016-09-23 12:39:23 +0100194 }
David Howells0b58b8a2016-09-02 22:39:45 +0100195 }
196
David Howells71f3ca42016-09-17 10:49:14 +0100197 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells0b58b8a2016-09-02 22:39:45 +0100198 _leave("");
199}
200
201/*
David Howells0b58b8a2016-09-02 22:39:45 +0100202 * send data through a socket
203 * - must be called in process context
David Howells540b1c42017-02-27 15:43:06 +0000204 * - The caller holds the call user access mutex, but not the socket lock.
David Howells0b58b8a2016-09-02 22:39:45 +0100205 */
206static int rxrpc_send_data(struct rxrpc_sock *rx,
207 struct rxrpc_call *call,
David Howellse8332512017-08-29 10:18:56 +0100208 struct msghdr *msg, size_t len,
209 rxrpc_notify_end_tx_t notify_end_tx)
David Howells0b58b8a2016-09-02 22:39:45 +0100210{
211 struct rxrpc_skb_priv *sp;
212 struct sk_buff *skb;
213 struct sock *sk = &rx->sk;
214 long timeo;
215 bool more;
216 int ret, copied;
217
218 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
219
220 /* this should be in poll */
221 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
222
223 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
224 return -EPIPE;
225
226 more = msg->msg_flags & MSG_MORE;
227
David Howellse754eba2017-06-07 12:40:03 +0100228 if (call->tx_total_len != -1) {
229 if (len > call->tx_total_len)
230 return -EMSGSIZE;
231 if (!more && len != call->tx_total_len)
232 return -EMSGSIZE;
233 }
234
David Howells0b58b8a2016-09-02 22:39:45 +0100235 skb = call->tx_pending;
236 call->tx_pending = NULL;
David Howells71f3ca42016-09-17 10:49:14 +0100237 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
David Howells0b58b8a2016-09-02 22:39:45 +0100238
239 copied = 0;
240 do {
David Howells7aa51da2016-09-22 00:29:31 +0100241 /* Check to see if there's a ping ACK to reply to. */
242 if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE)
David Howellsa5af7e12016-10-06 08:11:49 +0100243 rxrpc_send_ack_packet(call, false);
David Howells7aa51da2016-09-22 00:29:31 +0100244
David Howells0b58b8a2016-09-02 22:39:45 +0100245 if (!skb) {
246 size_t size, chunk, max, space;
247
248 _debug("alloc");
249
David Howells248f2192016-09-08 11:10:12 +0100250 if (call->tx_top - call->tx_hard_ack >=
David Howells57494342016-09-24 18:05:27 +0100251 min_t(unsigned int, call->tx_winsize,
252 call->cong_cwnd + call->cong_extra)) {
David Howells0b58b8a2016-09-02 22:39:45 +0100253 ret = -EAGAIN;
254 if (msg->msg_flags & MSG_DONTWAIT)
255 goto maybe_error;
256 ret = rxrpc_wait_for_tx_window(rx, call,
257 &timeo);
258 if (ret < 0)
259 goto maybe_error;
260 }
261
David Howells182f5052016-09-17 10:49:12 +0100262 max = RXRPC_JUMBO_DATALEN;
David Howells0b58b8a2016-09-02 22:39:45 +0100263 max -= call->conn->security_size;
264 max &= ~(call->conn->size_align - 1UL);
265
266 chunk = max;
267 if (chunk > msg_data_left(msg) && !more)
268 chunk = msg_data_left(msg);
269
270 space = chunk + call->conn->size_align;
271 space &= ~(call->conn->size_align - 1UL);
272
David Howells5a924b82016-09-22 00:29:31 +0100273 size = space + call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100274
275 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
276
277 /* create a buffer that we can retain until it's ACK'd */
278 skb = sock_alloc_send_skb(
279 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
280 if (!skb)
281 goto maybe_error;
282
David Howells71f3ca42016-09-17 10:49:14 +0100283 rxrpc_new_skb(skb, rxrpc_skb_tx_new);
David Howells0b58b8a2016-09-02 22:39:45 +0100284
285 _debug("ALLOC SEND %p", skb);
286
287 ASSERTCMP(skb->mark, ==, 0);
288
David Howells5a924b82016-09-22 00:29:31 +0100289 _debug("HS: %u", call->conn->security_size);
290 skb_reserve(skb, call->conn->security_size);
291 skb->len += call->conn->security_size;
David Howells0b58b8a2016-09-02 22:39:45 +0100292
293 sp = rxrpc_skb(skb);
294 sp->remain = chunk;
295 if (sp->remain > skb_tailroom(skb))
296 sp->remain = skb_tailroom(skb);
297
298 _net("skb: hr %d, tr %d, hl %d, rm %d",
299 skb_headroom(skb),
300 skb_tailroom(skb),
301 skb_headlen(skb),
302 sp->remain);
303
304 skb->ip_summed = CHECKSUM_UNNECESSARY;
305 }
306
307 _debug("append");
308 sp = rxrpc_skb(skb);
309
310 /* append next segment of data to the current buffer */
311 if (msg_data_left(msg) > 0) {
312 int copy = skb_tailroom(skb);
313 ASSERTCMP(copy, >, 0);
314 if (copy > msg_data_left(msg))
315 copy = msg_data_left(msg);
316 if (copy > sp->remain)
317 copy = sp->remain;
318
319 _debug("add");
320 ret = skb_add_data(skb, &msg->msg_iter, copy);
321 _debug("added");
322 if (ret < 0)
323 goto efault;
324 sp->remain -= copy;
325 skb->mark += copy;
326 copied += copy;
David Howellse754eba2017-06-07 12:40:03 +0100327 if (call->tx_total_len != -1)
328 call->tx_total_len -= copy;
David Howells0b58b8a2016-09-02 22:39:45 +0100329 }
330
David Howells0b58b8a2016-09-02 22:39:45 +0100331 /* add the packet to the send queue if it's now full */
332 if (sp->remain <= 0 ||
333 (msg_data_left(msg) == 0 && !more)) {
334 struct rxrpc_connection *conn = call->conn;
335 uint32_t seq;
336 size_t pad;
337
338 /* pad out if we're using security */
339 if (conn->security_ix) {
340 pad = conn->security_size + skb->mark;
341 pad = conn->size_align - pad;
342 pad &= conn->size_align - 1;
343 _debug("pad %zu", pad);
344 if (pad)
Johannes Bergb080db52017-06-16 14:29:19 +0200345 skb_put_zero(skb, pad);
David Howells0b58b8a2016-09-02 22:39:45 +0100346 }
347
David Howells248f2192016-09-08 11:10:12 +0100348 seq = call->tx_top + 1;
David Howells0b58b8a2016-09-02 22:39:45 +0100349
David Howells0b58b8a2016-09-02 22:39:45 +0100350 sp->hdr.seq = seq;
David Howells0b58b8a2016-09-02 22:39:45 +0100351 sp->hdr._rsvd = 0;
David Howells5a924b82016-09-22 00:29:31 +0100352 sp->hdr.flags = conn->out_clientflag;
David Howells0b58b8a2016-09-02 22:39:45 +0100353
David Howells0b58b8a2016-09-02 22:39:45 +0100354 if (msg_data_left(msg) == 0 && !more)
355 sp->hdr.flags |= RXRPC_LAST_PACKET;
David Howells248f2192016-09-08 11:10:12 +0100356 else if (call->tx_top - call->tx_hard_ack <
357 call->tx_winsize)
David Howells0b58b8a2016-09-02 22:39:45 +0100358 sp->hdr.flags |= RXRPC_MORE_PACKETS;
David Howells0b58b8a2016-09-02 22:39:45 +0100359
360 ret = conn->security->secure_packet(
David Howells5a924b82016-09-22 00:29:31 +0100361 call, skb, skb->mark, skb->head);
David Howells0b58b8a2016-09-02 22:39:45 +0100362 if (ret < 0)
363 goto out;
364
David Howellse8332512017-08-29 10:18:56 +0100365 rxrpc_queue_packet(rx, call, skb,
366 !msg_data_left(msg) && !more,
367 notify_end_tx);
David Howells0b58b8a2016-09-02 22:39:45 +0100368 skb = NULL;
369 }
David Howellsc038a582017-08-29 10:19:01 +0100370
371 /* Check for the far side aborting the call or a network error
372 * occurring. If this happens, save any packet that was under
373 * construction so that in the case of a network error, the
374 * call can be retried or redirected.
375 */
376 if (call->state == RXRPC_CALL_COMPLETE) {
377 ret = call->error;
378 goto out;
379 }
David Howells0b58b8a2016-09-02 22:39:45 +0100380 } while (msg_data_left(msg) > 0);
381
382success:
383 ret = copied;
384out:
385 call->tx_pending = skb;
386 _leave(" = %d", ret);
387 return ret;
388
David Howells0b58b8a2016-09-02 22:39:45 +0100389maybe_error:
390 if (copied)
391 goto success;
392 goto out;
393
394efault:
395 ret = -EFAULT;
396 goto out;
397}
David Howellsdf423a42016-09-02 22:39:45 +0100398
399/*
400 * extract control messages from the sendmsg() control buffer
401 */
David Howells3ab26a62017-06-07 14:41:52 +0100402static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p)
David Howellsdf423a42016-09-02 22:39:45 +0100403{
404 struct cmsghdr *cmsg;
405 bool got_user_ID = false;
406 int len;
407
David Howellsdf423a42016-09-02 22:39:45 +0100408 if (msg->msg_controllen == 0)
409 return -EINVAL;
410
411 for_each_cmsghdr(cmsg, msg) {
412 if (!CMSG_OK(msg, cmsg))
413 return -EINVAL;
414
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800415 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
David Howellsdf423a42016-09-02 22:39:45 +0100416 _debug("CMSG %d, %d, %d",
417 cmsg->cmsg_level, cmsg->cmsg_type, len);
418
419 if (cmsg->cmsg_level != SOL_RXRPC)
420 continue;
421
422 switch (cmsg->cmsg_type) {
423 case RXRPC_USER_CALL_ID:
424 if (msg->msg_flags & MSG_CMSG_COMPAT) {
425 if (len != sizeof(u32))
426 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100427 p->user_call_ID = *(u32 *)CMSG_DATA(cmsg);
David Howellsdf423a42016-09-02 22:39:45 +0100428 } else {
429 if (len != sizeof(unsigned long))
430 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100431 p->user_call_ID = *(unsigned long *)
David Howellsdf423a42016-09-02 22:39:45 +0100432 CMSG_DATA(cmsg);
433 }
David Howellsdf423a42016-09-02 22:39:45 +0100434 got_user_ID = true;
435 break;
436
437 case RXRPC_ABORT:
David Howells3ab26a62017-06-07 14:41:52 +0100438 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100439 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100440 p->command = RXRPC_CMD_SEND_ABORT;
441 if (len != sizeof(p->abort_code))
David Howellsdf423a42016-09-02 22:39:45 +0100442 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100443 p->abort_code = *(unsigned int *)CMSG_DATA(cmsg);
444 if (p->abort_code == 0)
David Howellsdf423a42016-09-02 22:39:45 +0100445 return -EINVAL;
446 break;
447
448 case RXRPC_ACCEPT:
David Howells3ab26a62017-06-07 14:41:52 +0100449 if (p->command != RXRPC_CMD_SEND_DATA)
David Howellsdf423a42016-09-02 22:39:45 +0100450 return -EINVAL;
David Howells3ab26a62017-06-07 14:41:52 +0100451 p->command = RXRPC_CMD_ACCEPT;
David Howellsdf423a42016-09-02 22:39:45 +0100452 if (len != 0)
453 return -EINVAL;
454 break;
455
456 case RXRPC_EXCLUSIVE_CALL:
David Howells3ab26a62017-06-07 14:41:52 +0100457 p->exclusive = true;
David Howellsdf423a42016-09-02 22:39:45 +0100458 if (len != 0)
459 return -EINVAL;
460 break;
David Howells4e255722017-06-05 14:30:49 +0100461
462 case RXRPC_UPGRADE_SERVICE:
David Howells3ab26a62017-06-07 14:41:52 +0100463 p->upgrade = true;
David Howells4e255722017-06-05 14:30:49 +0100464 if (len != 0)
465 return -EINVAL;
466 break;
467
David Howellse754eba2017-06-07 12:40:03 +0100468 case RXRPC_TX_LENGTH:
469 if (p->tx_total_len != -1 || len != sizeof(__s64))
470 return -EINVAL;
471 p->tx_total_len = *(__s64 *)CMSG_DATA(cmsg);
472 if (p->tx_total_len < 0)
473 return -EINVAL;
474 break;
475
David Howellsdf423a42016-09-02 22:39:45 +0100476 default:
477 return -EINVAL;
478 }
479 }
480
481 if (!got_user_ID)
482 return -EINVAL;
David Howellse754eba2017-06-07 12:40:03 +0100483 if (p->tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA)
484 return -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100485 _leave(" = 0");
486 return 0;
487}
488
489/*
David Howellsdf423a42016-09-02 22:39:45 +0100490 * Create a new client call for sendmsg().
David Howells540b1c42017-02-27 15:43:06 +0000491 * - Called with the socket lock held, which it must release.
492 * - If it returns a call, the call's lock will need releasing by the caller.
David Howellsdf423a42016-09-02 22:39:45 +0100493 */
494static struct rxrpc_call *
495rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
David Howells3ab26a62017-06-07 14:41:52 +0100496 struct rxrpc_send_params *p)
David Howells540b1c42017-02-27 15:43:06 +0000497 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100498{
499 struct rxrpc_conn_parameters cp;
500 struct rxrpc_call *call;
501 struct key *key;
502
503 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
504
505 _enter("");
506
David Howells540b1c42017-02-27 15:43:06 +0000507 if (!msg->msg_name) {
508 release_sock(&rx->sk);
David Howellsdf423a42016-09-02 22:39:45 +0100509 return ERR_PTR(-EDESTADDRREQ);
David Howells540b1c42017-02-27 15:43:06 +0000510 }
David Howellsdf423a42016-09-02 22:39:45 +0100511
512 key = rx->key;
513 if (key && !rx->key->payload.data[0])
514 key = NULL;
515
516 memset(&cp, 0, sizeof(cp));
517 cp.local = rx->local;
518 cp.key = rx->key;
519 cp.security_level = rx->min_sec_level;
David Howells3ab26a62017-06-07 14:41:52 +0100520 cp.exclusive = rx->exclusive | p->exclusive;
521 cp.upgrade = p->upgrade;
David Howellsdf423a42016-09-02 22:39:45 +0100522 cp.service_id = srx->srx_service;
David Howellse754eba2017-06-07 12:40:03 +0100523 call = rxrpc_new_client_call(rx, &cp, srx, p->user_call_ID,
524 p->tx_total_len, GFP_KERNEL);
David Howells540b1c42017-02-27 15:43:06 +0000525 /* The socket is now unlocked */
David Howellsdf423a42016-09-02 22:39:45 +0100526
527 _leave(" = %p\n", call);
528 return call;
529}
530
531/*
532 * send a message forming part of a client call through an RxRPC socket
533 * - caller holds the socket locked
534 * - the socket may be either a client socket or a server socket
535 */
536int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
David Howells540b1c42017-02-27 15:43:06 +0000537 __releases(&rx->sk.sk_lock.slock)
David Howellsdf423a42016-09-02 22:39:45 +0100538{
David Howells146d8fe2017-03-04 00:01:41 +0000539 enum rxrpc_call_state state;
David Howellsdf423a42016-09-02 22:39:45 +0100540 struct rxrpc_call *call;
David Howellsdf423a42016-09-02 22:39:45 +0100541 int ret;
542
David Howells3ab26a62017-06-07 14:41:52 +0100543 struct rxrpc_send_params p = {
David Howellse754eba2017-06-07 12:40:03 +0100544 .tx_total_len = -1,
David Howells3ab26a62017-06-07 14:41:52 +0100545 .user_call_ID = 0,
546 .abort_code = 0,
547 .command = RXRPC_CMD_SEND_DATA,
548 .exclusive = false,
549 .upgrade = true,
550 };
551
David Howellsdf423a42016-09-02 22:39:45 +0100552 _enter("");
553
David Howells3ab26a62017-06-07 14:41:52 +0100554 ret = rxrpc_sendmsg_cmsg(msg, &p);
David Howellsdf423a42016-09-02 22:39:45 +0100555 if (ret < 0)
David Howells540b1c42017-02-27 15:43:06 +0000556 goto error_release_sock;
David Howellsdf423a42016-09-02 22:39:45 +0100557
David Howells3ab26a62017-06-07 14:41:52 +0100558 if (p.command == RXRPC_CMD_ACCEPT) {
David Howells540b1c42017-02-27 15:43:06 +0000559 ret = -EINVAL;
David Howellsdf423a42016-09-02 22:39:45 +0100560 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
David Howells540b1c42017-02-27 15:43:06 +0000561 goto error_release_sock;
David Howells3ab26a62017-06-07 14:41:52 +0100562 call = rxrpc_accept_call(rx, p.user_call_ID, NULL);
David Howells540b1c42017-02-27 15:43:06 +0000563 /* The socket is now unlocked. */
David Howellsdf423a42016-09-02 22:39:45 +0100564 if (IS_ERR(call))
565 return PTR_ERR(call);
David Howellsfff72422016-09-07 14:34:21 +0100566 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100567 return 0;
568 }
569
David Howells3ab26a62017-06-07 14:41:52 +0100570 call = rxrpc_find_call_by_user_ID(rx, p.user_call_ID);
David Howellsdf423a42016-09-02 22:39:45 +0100571 if (!call) {
David Howells540b1c42017-02-27 15:43:06 +0000572 ret = -EBADSLT;
David Howells3ab26a62017-06-07 14:41:52 +0100573 if (p.command != RXRPC_CMD_SEND_DATA)
David Howells540b1c42017-02-27 15:43:06 +0000574 goto error_release_sock;
David Howells3ab26a62017-06-07 14:41:52 +0100575 call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p);
David Howells540b1c42017-02-27 15:43:06 +0000576 /* The socket is now unlocked... */
David Howellsdf423a42016-09-02 22:39:45 +0100577 if (IS_ERR(call))
578 return PTR_ERR(call);
David Howells540b1c42017-02-27 15:43:06 +0000579 /* ... and we have the call lock. */
580 } else {
David Howells146d8fe2017-03-04 00:01:41 +0000581 switch (READ_ONCE(call->state)) {
582 case RXRPC_CALL_UNINITIALISED:
583 case RXRPC_CALL_CLIENT_AWAIT_CONN:
584 case RXRPC_CALL_SERVER_PREALLOC:
585 case RXRPC_CALL_SERVER_SECURING:
586 case RXRPC_CALL_SERVER_ACCEPTING:
587 ret = -EBUSY;
David Howells37411ca2017-03-02 23:48:52 +0000588 goto error_release_sock;
David Howells146d8fe2017-03-04 00:01:41 +0000589 default:
590 break;
591 }
David Howells37411ca2017-03-02 23:48:52 +0000592
David Howells540b1c42017-02-27 15:43:06 +0000593 ret = mutex_lock_interruptible(&call->user_mutex);
594 release_sock(&rx->sk);
595 if (ret < 0) {
596 ret = -ERESTARTSYS;
597 goto error_put;
598 }
David Howellse754eba2017-06-07 12:40:03 +0100599
600 if (p.tx_total_len != -1) {
601 ret = -EINVAL;
602 if (call->tx_total_len != -1 ||
603 call->tx_pending ||
604 call->tx_top != 0)
605 goto error_put;
606 call->tx_total_len = p.tx_total_len;
607 }
David Howellsdf423a42016-09-02 22:39:45 +0100608 }
609
David Howells146d8fe2017-03-04 00:01:41 +0000610 state = READ_ONCE(call->state);
David Howellsdf423a42016-09-02 22:39:45 +0100611 _debug("CALL %d USR %lx ST %d on CONN %p",
David Howells146d8fe2017-03-04 00:01:41 +0000612 call->debug_id, call->user_call_ID, state, call->conn);
David Howellsdf423a42016-09-02 22:39:45 +0100613
David Howells146d8fe2017-03-04 00:01:41 +0000614 if (state >= RXRPC_CALL_COMPLETE) {
David Howellsdf423a42016-09-02 22:39:45 +0100615 /* it's too late for this call */
616 ret = -ESHUTDOWN;
David Howells3ab26a62017-06-07 14:41:52 +0100617 } else if (p.command == RXRPC_CMD_SEND_ABORT) {
David Howellsdf423a42016-09-02 22:39:45 +0100618 ret = 0;
David Howells3ab26a62017-06-07 14:41:52 +0100619 if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED))
David Howells26cb02a2016-10-06 08:11:49 +0100620 ret = rxrpc_send_abort_packet(call);
David Howells3ab26a62017-06-07 14:41:52 +0100621 } else if (p.command != RXRPC_CMD_SEND_DATA) {
David Howellsdf423a42016-09-02 22:39:45 +0100622 ret = -EINVAL;
623 } else if (rxrpc_is_client_call(call) &&
David Howells146d8fe2017-03-04 00:01:41 +0000624 state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
David Howellsdf423a42016-09-02 22:39:45 +0100625 /* request phase complete for this client call */
626 ret = -EPROTO;
627 } else if (rxrpc_is_service_call(call) &&
David Howells146d8fe2017-03-04 00:01:41 +0000628 state != RXRPC_CALL_SERVER_ACK_REQUEST &&
629 state != RXRPC_CALL_SERVER_SEND_REPLY) {
David Howellsdf423a42016-09-02 22:39:45 +0100630 /* Reply phase not begun or not complete for service call. */
631 ret = -EPROTO;
632 } else {
David Howellse8332512017-08-29 10:18:56 +0100633 ret = rxrpc_send_data(rx, call, msg, len, NULL);
David Howellsdf423a42016-09-02 22:39:45 +0100634 }
635
David Howells540b1c42017-02-27 15:43:06 +0000636 mutex_unlock(&call->user_mutex);
637error_put:
David Howellsfff72422016-09-07 14:34:21 +0100638 rxrpc_put_call(call, rxrpc_call_put);
David Howellsdf423a42016-09-02 22:39:45 +0100639 _leave(" = %d", ret);
640 return ret;
David Howells540b1c42017-02-27 15:43:06 +0000641
642error_release_sock:
643 release_sock(&rx->sk);
644 return ret;
David Howellsdf423a42016-09-02 22:39:45 +0100645}
646
647/**
648 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
649 * @sock: The socket the call is on
650 * @call: The call to send data through
651 * @msg: The data to send
652 * @len: The amount of data to send
David Howellse8332512017-08-29 10:18:56 +0100653 * @notify_end_tx: Notification that the last packet is queued.
David Howellsdf423a42016-09-02 22:39:45 +0100654 *
655 * Allow a kernel service to send data on a call. The call must be in an state
656 * appropriate to sending data. No control data should be supplied in @msg,
657 * nor should an address be supplied. MSG_MORE should be flagged if there's
658 * more data to come, otherwise this data will end the transmission phase.
659 */
660int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
David Howellse8332512017-08-29 10:18:56 +0100661 struct msghdr *msg, size_t len,
662 rxrpc_notify_end_tx_t notify_end_tx)
David Howellsdf423a42016-09-02 22:39:45 +0100663{
664 int ret;
665
666 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
667
668 ASSERTCMP(msg->msg_name, ==, NULL);
669 ASSERTCMP(msg->msg_control, ==, NULL);
670
David Howells540b1c42017-02-27 15:43:06 +0000671 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100672
673 _debug("CALL %d USR %lx ST %d on CONN %p",
674 call->debug_id, call->user_call_ID, call->state, call->conn);
675
David Howells146d8fe2017-03-04 00:01:41 +0000676 switch (READ_ONCE(call->state)) {
677 case RXRPC_CALL_CLIENT_SEND_REQUEST:
678 case RXRPC_CALL_SERVER_ACK_REQUEST:
679 case RXRPC_CALL_SERVER_SEND_REPLY:
David Howellse8332512017-08-29 10:18:56 +0100680 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len,
681 notify_end_tx);
David Howells146d8fe2017-03-04 00:01:41 +0000682 break;
683 case RXRPC_CALL_COMPLETE:
David Howells6fc166d2017-03-09 08:10:32 +0000684 read_lock_bh(&call->state_lock);
David Howellsbd2db2d2017-08-29 10:18:43 +0100685 ret = call->error;
David Howells6fc166d2017-03-09 08:10:32 +0000686 read_unlock_bh(&call->state_lock);
David Howells146d8fe2017-03-04 00:01:41 +0000687 break;
688 default:
David Howellsfb46f6e2017-04-06 10:12:00 +0100689 /* Request phase complete for this client call */
690 trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send"));
David Howells146d8fe2017-03-04 00:01:41 +0000691 ret = -EPROTO;
692 break;
David Howellsdf423a42016-09-02 22:39:45 +0100693 }
694
David Howells540b1c42017-02-27 15:43:06 +0000695 mutex_unlock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100696 _leave(" = %d", ret);
697 return ret;
698}
699EXPORT_SYMBOL(rxrpc_kernel_send_data);
700
701/**
702 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
703 * @sock: The socket the call is on
704 * @call: The call to be aborted
705 * @abort_code: The abort code to stick into the ABORT packet
David Howells5a429762016-09-06 22:19:51 +0100706 * @error: Local error value
707 * @why: 3-char string indicating why.
David Howellsdf423a42016-09-02 22:39:45 +0100708 *
David Howells84a4c092017-04-06 10:11:59 +0100709 * Allow a kernel service to abort a call, if it's still in an abortable state
710 * and return true if the call was aborted, false if it was already complete.
David Howellsdf423a42016-09-02 22:39:45 +0100711 */
David Howells84a4c092017-04-06 10:11:59 +0100712bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
David Howells5a429762016-09-06 22:19:51 +0100713 u32 abort_code, int error, const char *why)
David Howellsdf423a42016-09-02 22:39:45 +0100714{
David Howells84a4c092017-04-06 10:11:59 +0100715 bool aborted;
716
David Howells5a429762016-09-06 22:19:51 +0100717 _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why);
David Howellsdf423a42016-09-02 22:39:45 +0100718
David Howells540b1c42017-02-27 15:43:06 +0000719 mutex_lock(&call->user_mutex);
David Howellsdf423a42016-09-02 22:39:45 +0100720
David Howells84a4c092017-04-06 10:11:59 +0100721 aborted = rxrpc_abort_call(why, call, 0, abort_code, error);
722 if (aborted)
David Howells26cb02a2016-10-06 08:11:49 +0100723 rxrpc_send_abort_packet(call);
David Howellsdf423a42016-09-02 22:39:45 +0100724
David Howells540b1c42017-02-27 15:43:06 +0000725 mutex_unlock(&call->user_mutex);
David Howells84a4c092017-04-06 10:11:59 +0100726 return aborted;
David Howellsdf423a42016-09-02 22:39:45 +0100727}
David Howellsdf423a42016-09-02 22:39:45 +0100728EXPORT_SYMBOL(rxrpc_kernel_abort_call);
David Howellse754eba2017-06-07 12:40:03 +0100729
730/**
731 * rxrpc_kernel_set_tx_length - Set the total Tx length on a call
732 * @sock: The socket the call is on
733 * @call: The call to be informed
734 * @tx_total_len: The amount of data to be transmitted for this call
735 *
736 * Allow a kernel service to set the total transmit length on a call. This
737 * allows buffer-to-packet encrypt-and-copy to be performed.
738 *
739 * This function is primarily for use for setting the reply length since the
740 * request length can be set when beginning the call.
741 */
742void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call,
743 s64 tx_total_len)
744{
745 WARN_ON(call->tx_total_len != -1);
746 call->tx_total_len = tx_total_len;
747}
748EXPORT_SYMBOL(rxrpc_kernel_set_tx_length);