]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/sendmsg.c
rxrpc: Split sendmsg from packet transmission code
[mirror_ubuntu-bionic-kernel.git] / net / rxrpc / sendmsg.c
CommitLineData
0b58b8a1
DH
1/* 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>
18#include <linux/circ_buf.h>
19#include <net/sock.h>
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
23static int rxrpc_send_data(struct rxrpc_sock *rx,
24 struct rxrpc_call *call,
25 struct msghdr *msg, size_t len);
26
27/*
28 * extract control messages from the sendmsg() control buffer
29 */
30static int rxrpc_sendmsg_cmsg(struct msghdr *msg,
31 unsigned long *user_call_ID,
32 enum rxrpc_command *command,
33 u32 *abort_code,
34 bool *_exclusive)
35{
36 struct cmsghdr *cmsg;
37 bool got_user_ID = false;
38 int len;
39
40 *command = RXRPC_CMD_SEND_DATA;
41
42 if (msg->msg_controllen == 0)
43 return -EINVAL;
44
45 for_each_cmsghdr(cmsg, msg) {
46 if (!CMSG_OK(msg, cmsg))
47 return -EINVAL;
48
49 len = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
50 _debug("CMSG %d, %d, %d",
51 cmsg->cmsg_level, cmsg->cmsg_type, len);
52
53 if (cmsg->cmsg_level != SOL_RXRPC)
54 continue;
55
56 switch (cmsg->cmsg_type) {
57 case RXRPC_USER_CALL_ID:
58 if (msg->msg_flags & MSG_CMSG_COMPAT) {
59 if (len != sizeof(u32))
60 return -EINVAL;
61 *user_call_ID = *(u32 *) CMSG_DATA(cmsg);
62 } else {
63 if (len != sizeof(unsigned long))
64 return -EINVAL;
65 *user_call_ID = *(unsigned long *)
66 CMSG_DATA(cmsg);
67 }
68 _debug("User Call ID %lx", *user_call_ID);
69 got_user_ID = true;
70 break;
71
72 case RXRPC_ABORT:
73 if (*command != RXRPC_CMD_SEND_DATA)
74 return -EINVAL;
75 *command = RXRPC_CMD_SEND_ABORT;
76 if (len != sizeof(*abort_code))
77 return -EINVAL;
78 *abort_code = *(unsigned int *) CMSG_DATA(cmsg);
79 _debug("Abort %x", *abort_code);
80 if (*abort_code == 0)
81 return -EINVAL;
82 break;
83
84 case RXRPC_ACCEPT:
85 if (*command != RXRPC_CMD_SEND_DATA)
86 return -EINVAL;
87 *command = RXRPC_CMD_ACCEPT;
88 if (len != 0)
89 return -EINVAL;
90 break;
91
92 case RXRPC_EXCLUSIVE_CALL:
93 *_exclusive = true;
94 if (len != 0)
95 return -EINVAL;
96 break;
97 default:
98 return -EINVAL;
99 }
100 }
101
102 if (!got_user_ID)
103 return -EINVAL;
104 _leave(" = 0");
105 return 0;
106}
107
108/*
109 * abort a call, sending an ABORT packet to the peer
110 */
111static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
112{
113 if (call->state >= RXRPC_CALL_COMPLETE)
114 return;
115
116 write_lock_bh(&call->state_lock);
117
118 if (__rxrpc_abort_call(call, abort_code, ECONNABORTED)) {
119 del_timer_sync(&call->resend_timer);
120 del_timer_sync(&call->ack_timer);
121 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
122 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
123 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
124 rxrpc_queue_call(call);
125 }
126
127 write_unlock_bh(&call->state_lock);
128}
129
130/*
131 * Create a new client call for sendmsg().
132 */
133static struct rxrpc_call *
134rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg,
135 unsigned long user_call_ID, bool exclusive)
136{
137 struct rxrpc_conn_parameters cp;
138 struct rxrpc_call *call;
139 struct key *key;
140
141 DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name);
142
143 _enter("");
144
145 if (!msg->msg_name)
146 return ERR_PTR(-EDESTADDRREQ);
147
148 key = rx->key;
149 if (key && !rx->key->payload.data[0])
150 key = NULL;
151
152 memset(&cp, 0, sizeof(cp));
153 cp.local = rx->local;
154 cp.key = rx->key;
155 cp.security_level = rx->min_sec_level;
156 cp.exclusive = rx->exclusive | exclusive;
157 cp.service_id = srx->srx_service;
158 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, GFP_KERNEL);
159
160 _leave(" = %p\n", call);
161 return call;
162}
163
164/*
165 * send a message forming part of a client call through an RxRPC socket
166 * - caller holds the socket locked
167 * - the socket may be either a client socket or a server socket
168 */
169int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
170{
171 enum rxrpc_command cmd;
172 struct rxrpc_call *call;
173 unsigned long user_call_ID = 0;
174 bool exclusive = false;
175 u32 abort_code = 0;
176 int ret;
177
178 _enter("");
179
180 ret = rxrpc_sendmsg_cmsg(msg, &user_call_ID, &cmd, &abort_code,
181 &exclusive);
182 if (ret < 0)
183 return ret;
184
185 if (cmd == RXRPC_CMD_ACCEPT) {
186 if (rx->sk.sk_state != RXRPC_SERVER_LISTENING)
187 return -EINVAL;
188 call = rxrpc_accept_call(rx, user_call_ID, NULL);
189 if (IS_ERR(call))
190 return PTR_ERR(call);
191 rxrpc_put_call(call);
192 return 0;
193 }
194
195 call = rxrpc_find_call_by_user_ID(rx, user_call_ID);
196 if (!call) {
197 if (cmd != RXRPC_CMD_SEND_DATA)
198 return -EBADSLT;
199 call = rxrpc_new_client_call_for_sendmsg(rx, msg, user_call_ID,
200 exclusive);
201 if (IS_ERR(call))
202 return PTR_ERR(call);
203 }
204
205 rxrpc_see_call(call);
206 _debug("CALL %d USR %lx ST %d on CONN %p",
207 call->debug_id, call->user_call_ID, call->state, call->conn);
208
209 if (call->state >= RXRPC_CALL_COMPLETE) {
210 /* it's too late for this call */
211 ret = -ESHUTDOWN;
212 } else if (cmd == RXRPC_CMD_SEND_ABORT) {
213 rxrpc_send_abort(call, abort_code);
214 ret = 0;
215 } else if (cmd != RXRPC_CMD_SEND_DATA) {
216 ret = -EINVAL;
217 } else if (rxrpc_is_client_call(call) &&
218 call->state != RXRPC_CALL_CLIENT_SEND_REQUEST) {
219 /* request phase complete for this client call */
220 ret = -EPROTO;
221 } else if (rxrpc_is_service_call(call) &&
222 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
223 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
224 /* Reply phase not begun or not complete for service call. */
225 ret = -EPROTO;
226 } else {
227 ret = rxrpc_send_data(rx, call, msg, len);
228 }
229
230 rxrpc_put_call(call);
231 _leave(" = %d", ret);
232 return ret;
233}
234
235/**
236 * rxrpc_kernel_send_data - Allow a kernel service to send data on a call
237 * @sock: The socket the call is on
238 * @call: The call to send data through
239 * @msg: The data to send
240 * @len: The amount of data to send
241 *
242 * Allow a kernel service to send data on a call. The call must be in an state
243 * appropriate to sending data. No control data should be supplied in @msg,
244 * nor should an address be supplied. MSG_MORE should be flagged if there's
245 * more data to come, otherwise this data will end the transmission phase.
246 */
247int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call,
248 struct msghdr *msg, size_t len)
249{
250 int ret;
251
252 _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]);
253
254 ASSERTCMP(msg->msg_name, ==, NULL);
255 ASSERTCMP(msg->msg_control, ==, NULL);
256
257 lock_sock(sock->sk);
258
259 _debug("CALL %d USR %lx ST %d on CONN %p",
260 call->debug_id, call->user_call_ID, call->state, call->conn);
261
262 if (call->state >= RXRPC_CALL_COMPLETE) {
263 ret = -ESHUTDOWN; /* it's too late for this call */
264 } else if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
265 call->state != RXRPC_CALL_SERVER_ACK_REQUEST &&
266 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
267 ret = -EPROTO; /* request phase complete for this client call */
268 } else {
269 ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len);
270 }
271
272 release_sock(sock->sk);
273 _leave(" = %d", ret);
274 return ret;
275}
276EXPORT_SYMBOL(rxrpc_kernel_send_data);
277
278/**
279 * rxrpc_kernel_abort_call - Allow a kernel service to abort a call
280 * @sock: The socket the call is on
281 * @call: The call to be aborted
282 * @abort_code: The abort code to stick into the ABORT packet
283 *
284 * Allow a kernel service to abort a call, if it's still in an abortable state.
285 */
286void rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call,
287 u32 abort_code)
288{
289 _enter("{%d},%d", call->debug_id, abort_code);
290
291 lock_sock(sock->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 rxrpc_send_abort(call, abort_code);
297
298 release_sock(sock->sk);
299 _leave("");
300}
301
302EXPORT_SYMBOL(rxrpc_kernel_abort_call);
303
304/*
305 * wait for space to appear in the transmit/ACK window
306 * - caller holds the socket locked
307 */
308static int rxrpc_wait_for_tx_window(struct rxrpc_sock *rx,
309 struct rxrpc_call *call,
310 long *timeo)
311{
312 DECLARE_WAITQUEUE(myself, current);
313 int ret;
314
315 _enter(",{%d},%ld",
316 CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
317 call->acks_winsz),
318 *timeo);
319
320 add_wait_queue(&call->waitq, &myself);
321
322 for (;;) {
323 set_current_state(TASK_INTERRUPTIBLE);
324 ret = 0;
325 if (CIRC_SPACE(call->acks_head, ACCESS_ONCE(call->acks_tail),
326 call->acks_winsz) > 0)
327 break;
328 if (signal_pending(current)) {
329 ret = sock_intr_errno(*timeo);
330 break;
331 }
332
333 release_sock(&rx->sk);
334 *timeo = schedule_timeout(*timeo);
335 lock_sock(&rx->sk);
336 }
337
338 remove_wait_queue(&call->waitq, &myself);
339 set_current_state(TASK_RUNNING);
340 _leave(" = %d", ret);
341 return ret;
342}
343
344/*
345 * attempt to schedule an instant Tx resend
346 */
347static inline void rxrpc_instant_resend(struct rxrpc_call *call)
348{
349 read_lock_bh(&call->state_lock);
350 if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
351 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
352 if (call->state < RXRPC_CALL_COMPLETE &&
353 !test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
354 rxrpc_queue_call(call);
355 }
356 read_unlock_bh(&call->state_lock);
357}
358
359/*
360 * queue a packet for transmission, set the resend timer and attempt
361 * to send the packet immediately
362 */
363static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
364 bool last)
365{
366 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
367 int ret;
368
369 _net("queue skb %p [%d]", skb, call->acks_head);
370
371 ASSERT(call->acks_window != NULL);
372 call->acks_window[call->acks_head] = (unsigned long) skb;
373 smp_wmb();
374 call->acks_head = (call->acks_head + 1) & (call->acks_winsz - 1);
375
376 if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) {
377 _debug("________awaiting reply/ACK__________");
378 write_lock_bh(&call->state_lock);
379 switch (call->state) {
380 case RXRPC_CALL_CLIENT_SEND_REQUEST:
381 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
382 break;
383 case RXRPC_CALL_SERVER_ACK_REQUEST:
384 call->state = RXRPC_CALL_SERVER_SEND_REPLY;
385 if (!last)
386 break;
387 case RXRPC_CALL_SERVER_SEND_REPLY:
388 call->state = RXRPC_CALL_SERVER_AWAIT_ACK;
389 break;
390 default:
391 break;
392 }
393 write_unlock_bh(&call->state_lock);
394 }
395
396 _proto("Tx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
397
398 sp->need_resend = false;
399 sp->resend_at = jiffies + rxrpc_resend_timeout;
400 if (!test_and_set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags)) {
401 _debug("run timer");
402 call->resend_timer.expires = sp->resend_at;
403 add_timer(&call->resend_timer);
404 }
405
406 /* attempt to cancel the rx-ACK timer, deferring reply transmission if
407 * we're ACK'ing the request phase of an incoming call */
408 ret = -EAGAIN;
409 if (try_to_del_timer_sync(&call->ack_timer) >= 0) {
410 /* the packet may be freed by rxrpc_process_call() before this
411 * returns */
412 if (rxrpc_is_client_call(call))
413 rxrpc_expose_client_call(call);
414 ret = rxrpc_send_data_packet(call->conn, skb);
415 _net("sent skb %p", skb);
416 } else {
417 _debug("failed to delete ACK timer");
418 }
419
420 if (ret < 0) {
421 _debug("need instant resend %d", ret);
422 sp->need_resend = true;
423 rxrpc_instant_resend(call);
424 }
425
426 _leave("");
427}
428
429/*
430 * Convert a host-endian header into a network-endian header.
431 */
432static void rxrpc_insert_header(struct sk_buff *skb)
433{
434 struct rxrpc_wire_header whdr;
435 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
436
437 whdr.epoch = htonl(sp->hdr.epoch);
438 whdr.cid = htonl(sp->hdr.cid);
439 whdr.callNumber = htonl(sp->hdr.callNumber);
440 whdr.seq = htonl(sp->hdr.seq);
441 whdr.serial = htonl(sp->hdr.serial);
442 whdr.type = sp->hdr.type;
443 whdr.flags = sp->hdr.flags;
444 whdr.userStatus = sp->hdr.userStatus;
445 whdr.securityIndex = sp->hdr.securityIndex;
446 whdr._rsvd = htons(sp->hdr._rsvd);
447 whdr.serviceId = htons(sp->hdr.serviceId);
448
449 memcpy(skb->head, &whdr, sizeof(whdr));
450}
451
452/*
453 * send data through a socket
454 * - must be called in process context
455 * - caller holds the socket locked
456 */
457static int rxrpc_send_data(struct rxrpc_sock *rx,
458 struct rxrpc_call *call,
459 struct msghdr *msg, size_t len)
460{
461 struct rxrpc_skb_priv *sp;
462 struct sk_buff *skb;
463 struct sock *sk = &rx->sk;
464 long timeo;
465 bool more;
466 int ret, copied;
467
468 timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
469
470 /* this should be in poll */
471 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
472
473 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
474 return -EPIPE;
475
476 more = msg->msg_flags & MSG_MORE;
477
478 skb = call->tx_pending;
479 call->tx_pending = NULL;
480 rxrpc_see_skb(skb);
481
482 copied = 0;
483 do {
484 if (!skb) {
485 size_t size, chunk, max, space;
486
487 _debug("alloc");
488
489 if (CIRC_SPACE(call->acks_head,
490 ACCESS_ONCE(call->acks_tail),
491 call->acks_winsz) <= 0) {
492 ret = -EAGAIN;
493 if (msg->msg_flags & MSG_DONTWAIT)
494 goto maybe_error;
495 ret = rxrpc_wait_for_tx_window(rx, call,
496 &timeo);
497 if (ret < 0)
498 goto maybe_error;
499 }
500
501 max = call->conn->params.peer->maxdata;
502 max -= call->conn->security_size;
503 max &= ~(call->conn->size_align - 1UL);
504
505 chunk = max;
506 if (chunk > msg_data_left(msg) && !more)
507 chunk = msg_data_left(msg);
508
509 space = chunk + call->conn->size_align;
510 space &= ~(call->conn->size_align - 1UL);
511
512 size = space + call->conn->header_size;
513
514 _debug("SIZE: %zu/%zu/%zu", chunk, space, size);
515
516 /* create a buffer that we can retain until it's ACK'd */
517 skb = sock_alloc_send_skb(
518 sk, size, msg->msg_flags & MSG_DONTWAIT, &ret);
519 if (!skb)
520 goto maybe_error;
521
522 rxrpc_new_skb(skb);
523
524 _debug("ALLOC SEND %p", skb);
525
526 ASSERTCMP(skb->mark, ==, 0);
527
528 _debug("HS: %u", call->conn->header_size);
529 skb_reserve(skb, call->conn->header_size);
530 skb->len += call->conn->header_size;
531
532 sp = rxrpc_skb(skb);
533 sp->remain = chunk;
534 if (sp->remain > skb_tailroom(skb))
535 sp->remain = skb_tailroom(skb);
536
537 _net("skb: hr %d, tr %d, hl %d, rm %d",
538 skb_headroom(skb),
539 skb_tailroom(skb),
540 skb_headlen(skb),
541 sp->remain);
542
543 skb->ip_summed = CHECKSUM_UNNECESSARY;
544 }
545
546 _debug("append");
547 sp = rxrpc_skb(skb);
548
549 /* append next segment of data to the current buffer */
550 if (msg_data_left(msg) > 0) {
551 int copy = skb_tailroom(skb);
552 ASSERTCMP(copy, >, 0);
553 if (copy > msg_data_left(msg))
554 copy = msg_data_left(msg);
555 if (copy > sp->remain)
556 copy = sp->remain;
557
558 _debug("add");
559 ret = skb_add_data(skb, &msg->msg_iter, copy);
560 _debug("added");
561 if (ret < 0)
562 goto efault;
563 sp->remain -= copy;
564 skb->mark += copy;
565 copied += copy;
566 }
567
568 /* check for the far side aborting the call or a network error
569 * occurring */
570 if (call->state == RXRPC_CALL_COMPLETE)
571 goto call_terminated;
572
573 /* add the packet to the send queue if it's now full */
574 if (sp->remain <= 0 ||
575 (msg_data_left(msg) == 0 && !more)) {
576 struct rxrpc_connection *conn = call->conn;
577 uint32_t seq;
578 size_t pad;
579
580 /* pad out if we're using security */
581 if (conn->security_ix) {
582 pad = conn->security_size + skb->mark;
583 pad = conn->size_align - pad;
584 pad &= conn->size_align - 1;
585 _debug("pad %zu", pad);
586 if (pad)
587 memset(skb_put(skb, pad), 0, pad);
588 }
589
590 seq = atomic_inc_return(&call->sequence);
591
592 sp->hdr.epoch = conn->proto.epoch;
593 sp->hdr.cid = call->cid;
594 sp->hdr.callNumber = call->call_id;
595 sp->hdr.seq = seq;
596 sp->hdr.serial = atomic_inc_return(&conn->serial);
597 sp->hdr.type = RXRPC_PACKET_TYPE_DATA;
598 sp->hdr.userStatus = 0;
599 sp->hdr.securityIndex = conn->security_ix;
600 sp->hdr._rsvd = 0;
601 sp->hdr.serviceId = call->service_id;
602
603 sp->hdr.flags = conn->out_clientflag;
604 if (msg_data_left(msg) == 0 && !more)
605 sp->hdr.flags |= RXRPC_LAST_PACKET;
606 else if (CIRC_SPACE(call->acks_head,
607 ACCESS_ONCE(call->acks_tail),
608 call->acks_winsz) > 1)
609 sp->hdr.flags |= RXRPC_MORE_PACKETS;
610 if (more && seq & 1)
611 sp->hdr.flags |= RXRPC_REQUEST_ACK;
612
613 ret = conn->security->secure_packet(
614 call, skb, skb->mark,
615 skb->head + sizeof(struct rxrpc_wire_header));
616 if (ret < 0)
617 goto out;
618
619 rxrpc_insert_header(skb);
620 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
621 skb = NULL;
622 }
623 } while (msg_data_left(msg) > 0);
624
625success:
626 ret = copied;
627out:
628 call->tx_pending = skb;
629 _leave(" = %d", ret);
630 return ret;
631
632call_terminated:
633 rxrpc_free_skb(skb);
634 _leave(" = %d", -call->error);
635 return ret;
636
637maybe_error:
638 if (copied)
639 goto success;
640 goto out;
641
642efault:
643 ret = -EFAULT;
644 goto out;
645}