David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 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> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 18 | #include <linux/sched/signal.h> |
| 19 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 20 | #include <net/sock.h> |
| 21 | #include <net/af_rxrpc.h> |
| 22 | #include "ar-internal.h" |
| 23 | |
David Howells | 3dc20f0 | 2016-09-04 13:25:21 +0100 | [diff] [blame] | 24 | enum 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 Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 31 | struct rxrpc_send_params { |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 32 | s64 tx_total_len; /* Total Tx data length (if send data) */ |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 33 | 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 Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 40 | /* |
| 41 | * wait for space to appear in the transmit/ACK window |
| 42 | * - caller holds the socket locked |
| 43 | */ |
| 44 | static 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 Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 51 | _enter(",{%u,%u,%u}", |
| 52 | call->tx_hard_ack, call->tx_top, call->tx_winsize); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 53 | |
| 54 | add_wait_queue(&call->waitq, &myself); |
| 55 | |
| 56 | for (;;) { |
| 57 | set_current_state(TASK_INTERRUPTIBLE); |
| 58 | ret = 0; |
David Howells | 5749434 | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 59 | if (call->tx_top - call->tx_hard_ack < |
| 60 | min_t(unsigned int, call->tx_winsize, |
| 61 | call->cong_cwnd + call->cong_extra)) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 62 | break; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 63 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 64 | ret = -call->error; |
| 65 | break; |
| 66 | } |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 67 | if (signal_pending(current)) { |
| 68 | ret = sock_intr_errno(*timeo); |
| 69 | break; |
| 70 | } |
| 71 | |
David Howells | a124fe3 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 72 | trace_rxrpc_transmit(call, rxrpc_transmit_wait); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 73 | mutex_unlock(&call->user_mutex); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 74 | *timeo = schedule_timeout(*timeo); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 75 | if (mutex_lock_interruptible(&call->user_mutex) < 0) { |
| 76 | ret = sock_intr_errno(*timeo); |
| 77 | break; |
| 78 | } |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 79 | } |
| 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 Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 88 | * Schedule an instant Tx resend. |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 89 | */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 90 | static inline void rxrpc_instant_resend(struct rxrpc_call *call, int ix) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 91 | { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 92 | 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 Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 97 | rxrpc_queue_call(call); |
| 98 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 99 | |
| 100 | spin_unlock_bh(&call->lock); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 104 | * Queue a DATA packet for transmission, set the resend timeout and send the |
| 105 | * packet immediately |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 106 | */ |
| 107 | static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb, |
| 108 | bool last) |
| 109 | { |
| 110 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 111 | rxrpc_seq_t seq = sp->hdr.seq; |
| 112 | int ret, ix; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 113 | u8 annotation = RXRPC_TX_ANNO_UNACK; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 114 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 115 | _net("queue skb %p [%d]", skb, seq); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 116 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 117 | ASSERTCMP(seq, ==, call->tx_top + 1); |
| 118 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 119 | if (last) |
| 120 | annotation |= RXRPC_TX_ANNO_LAST; |
| 121 | |
David Howells | b24d289 | 2016-09-23 13:17:33 +0100 | [diff] [blame] | 122 | /* We have to set the timestamp before queueing as the retransmit |
| 123 | * algorithm can see the packet as soon as we queue it. |
| 124 | */ |
| 125 | skb->tstamp = ktime_get_real(); |
| 126 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 127 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 128 | rxrpc_get_skb(skb, rxrpc_skb_tx_got); |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 129 | call->rxtx_annotations[ix] = annotation; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 130 | smp_wmb(); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 131 | call->rxtx_buffer[ix] = skb; |
| 132 | call->tx_top = seq; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 133 | if (last) |
David Howells | a124fe3 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 134 | trace_rxrpc_transmit(call, rxrpc_transmit_queue_last); |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 135 | else |
David Howells | a124fe3 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 136 | trace_rxrpc_transmit(call, rxrpc_transmit_queue); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 137 | |
| 138 | if (last || call->state == RXRPC_CALL_SERVER_ACK_REQUEST) { |
| 139 | _debug("________awaiting reply/ACK__________"); |
| 140 | write_lock_bh(&call->state_lock); |
| 141 | switch (call->state) { |
| 142 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
| 143 | call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY; |
| 144 | break; |
| 145 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 146 | call->state = RXRPC_CALL_SERVER_SEND_REPLY; |
David Howells | 9749fd2 | 2016-10-06 08:11:50 +0100 | [diff] [blame] | 147 | call->ack_at = call->expire_at; |
| 148 | if (call->ackr_reason == RXRPC_ACK_DELAY) |
| 149 | call->ackr_reason = 0; |
| 150 | __rxrpc_set_timer(call, rxrpc_timer_init_for_send_reply, |
| 151 | ktime_get_real()); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 152 | if (!last) |
| 153 | break; |
| 154 | case RXRPC_CALL_SERVER_SEND_REPLY: |
| 155 | call->state = RXRPC_CALL_SERVER_AWAIT_ACK; |
| 156 | break; |
| 157 | default: |
| 158 | break; |
| 159 | } |
| 160 | write_unlock_bh(&call->state_lock); |
| 161 | } |
| 162 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 163 | if (seq == 1 && rxrpc_is_client_call(call)) |
| 164 | rxrpc_expose_client_call(call); |
| 165 | |
David Howells | a176707 | 2016-09-29 22:37:15 +0100 | [diff] [blame] | 166 | ret = rxrpc_send_data_packet(call, skb, false); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 167 | if (ret < 0) { |
| 168 | _debug("need instant resend %d", ret); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 169 | rxrpc_instant_resend(call, ix); |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 170 | } else { |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 171 | ktime_t now = ktime_get_real(), resend_at; |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 172 | |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 173 | resend_at = ktime_add_ms(now, rxrpc_resend_timeout); |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 174 | |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 175 | if (ktime_before(resend_at, call->resend_at)) { |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 176 | call->resend_at = resend_at; |
David Howells | df0adc7 | 2016-09-26 22:12:49 +0100 | [diff] [blame] | 177 | rxrpc_set_timer(call, rxrpc_timer_set_for_send, now); |
David Howells | dfc3da4 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 178 | } |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 179 | } |
| 180 | |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 181 | rxrpc_free_skb(skb, rxrpc_skb_tx_freed); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 182 | _leave(""); |
| 183 | } |
| 184 | |
| 185 | /* |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 186 | * send data through a socket |
| 187 | * - must be called in process context |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 188 | * - The caller holds the call user access mutex, but not the socket lock. |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 189 | */ |
| 190 | static int rxrpc_send_data(struct rxrpc_sock *rx, |
| 191 | struct rxrpc_call *call, |
| 192 | struct msghdr *msg, size_t len) |
| 193 | { |
| 194 | struct rxrpc_skb_priv *sp; |
| 195 | struct sk_buff *skb; |
| 196 | struct sock *sk = &rx->sk; |
| 197 | long timeo; |
| 198 | bool more; |
| 199 | int ret, copied; |
| 200 | |
| 201 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 202 | |
| 203 | /* this should be in poll */ |
| 204 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 205 | |
| 206 | if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) |
| 207 | return -EPIPE; |
| 208 | |
| 209 | more = msg->msg_flags & MSG_MORE; |
| 210 | |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 211 | if (call->tx_total_len != -1) { |
| 212 | if (len > call->tx_total_len) |
| 213 | return -EMSGSIZE; |
| 214 | if (!more && len != call->tx_total_len) |
| 215 | return -EMSGSIZE; |
| 216 | } |
| 217 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 218 | skb = call->tx_pending; |
| 219 | call->tx_pending = NULL; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 220 | rxrpc_see_skb(skb, rxrpc_skb_tx_seen); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 221 | |
| 222 | copied = 0; |
| 223 | do { |
David Howells | 7aa51da | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 224 | /* Check to see if there's a ping ACK to reply to. */ |
| 225 | if (call->ackr_reason == RXRPC_ACK_PING_RESPONSE) |
David Howells | a5af7e1 | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 226 | rxrpc_send_ack_packet(call, false); |
David Howells | 7aa51da | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 227 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 228 | if (!skb) { |
| 229 | size_t size, chunk, max, space; |
| 230 | |
| 231 | _debug("alloc"); |
| 232 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 233 | if (call->tx_top - call->tx_hard_ack >= |
David Howells | 5749434 | 2016-09-24 18:05:27 +0100 | [diff] [blame] | 234 | min_t(unsigned int, call->tx_winsize, |
| 235 | call->cong_cwnd + call->cong_extra)) { |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 236 | ret = -EAGAIN; |
| 237 | if (msg->msg_flags & MSG_DONTWAIT) |
| 238 | goto maybe_error; |
| 239 | ret = rxrpc_wait_for_tx_window(rx, call, |
| 240 | &timeo); |
| 241 | if (ret < 0) |
| 242 | goto maybe_error; |
| 243 | } |
| 244 | |
David Howells | 182f505 | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 245 | max = RXRPC_JUMBO_DATALEN; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 246 | max -= call->conn->security_size; |
| 247 | max &= ~(call->conn->size_align - 1UL); |
| 248 | |
| 249 | chunk = max; |
| 250 | if (chunk > msg_data_left(msg) && !more) |
| 251 | chunk = msg_data_left(msg); |
| 252 | |
| 253 | space = chunk + call->conn->size_align; |
| 254 | space &= ~(call->conn->size_align - 1UL); |
| 255 | |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 256 | size = space + call->conn->security_size; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 257 | |
| 258 | _debug("SIZE: %zu/%zu/%zu", chunk, space, size); |
| 259 | |
| 260 | /* create a buffer that we can retain until it's ACK'd */ |
| 261 | skb = sock_alloc_send_skb( |
| 262 | sk, size, msg->msg_flags & MSG_DONTWAIT, &ret); |
| 263 | if (!skb) |
| 264 | goto maybe_error; |
| 265 | |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 266 | rxrpc_new_skb(skb, rxrpc_skb_tx_new); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 267 | |
| 268 | _debug("ALLOC SEND %p", skb); |
| 269 | |
| 270 | ASSERTCMP(skb->mark, ==, 0); |
| 271 | |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 272 | _debug("HS: %u", call->conn->security_size); |
| 273 | skb_reserve(skb, call->conn->security_size); |
| 274 | skb->len += call->conn->security_size; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 275 | |
| 276 | sp = rxrpc_skb(skb); |
| 277 | sp->remain = chunk; |
| 278 | if (sp->remain > skb_tailroom(skb)) |
| 279 | sp->remain = skb_tailroom(skb); |
| 280 | |
| 281 | _net("skb: hr %d, tr %d, hl %d, rm %d", |
| 282 | skb_headroom(skb), |
| 283 | skb_tailroom(skb), |
| 284 | skb_headlen(skb), |
| 285 | sp->remain); |
| 286 | |
| 287 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 288 | } |
| 289 | |
| 290 | _debug("append"); |
| 291 | sp = rxrpc_skb(skb); |
| 292 | |
| 293 | /* append next segment of data to the current buffer */ |
| 294 | if (msg_data_left(msg) > 0) { |
| 295 | int copy = skb_tailroom(skb); |
| 296 | ASSERTCMP(copy, >, 0); |
| 297 | if (copy > msg_data_left(msg)) |
| 298 | copy = msg_data_left(msg); |
| 299 | if (copy > sp->remain) |
| 300 | copy = sp->remain; |
| 301 | |
| 302 | _debug("add"); |
| 303 | ret = skb_add_data(skb, &msg->msg_iter, copy); |
| 304 | _debug("added"); |
| 305 | if (ret < 0) |
| 306 | goto efault; |
| 307 | sp->remain -= copy; |
| 308 | skb->mark += copy; |
| 309 | copied += copy; |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 310 | if (call->tx_total_len != -1) |
| 311 | call->tx_total_len -= copy; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* check for the far side aborting the call or a network error |
| 315 | * occurring */ |
| 316 | if (call->state == RXRPC_CALL_COMPLETE) |
| 317 | goto call_terminated; |
| 318 | |
| 319 | /* add the packet to the send queue if it's now full */ |
| 320 | if (sp->remain <= 0 || |
| 321 | (msg_data_left(msg) == 0 && !more)) { |
| 322 | struct rxrpc_connection *conn = call->conn; |
| 323 | uint32_t seq; |
| 324 | size_t pad; |
| 325 | |
| 326 | /* pad out if we're using security */ |
| 327 | if (conn->security_ix) { |
| 328 | pad = conn->security_size + skb->mark; |
| 329 | pad = conn->size_align - pad; |
| 330 | pad &= conn->size_align - 1; |
| 331 | _debug("pad %zu", pad); |
| 332 | if (pad) |
Johannes Berg | b080db5 | 2017-06-16 14:29:19 +0200 | [diff] [blame] | 333 | skb_put_zero(skb, pad); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 334 | } |
| 335 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 336 | seq = call->tx_top + 1; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 337 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 338 | sp->hdr.seq = seq; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 339 | sp->hdr._rsvd = 0; |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 340 | sp->hdr.flags = conn->out_clientflag; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 341 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 342 | if (msg_data_left(msg) == 0 && !more) |
| 343 | sp->hdr.flags |= RXRPC_LAST_PACKET; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 344 | else if (call->tx_top - call->tx_hard_ack < |
| 345 | call->tx_winsize) |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 346 | sp->hdr.flags |= RXRPC_MORE_PACKETS; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 347 | |
| 348 | ret = conn->security->secure_packet( |
David Howells | 5a924b8 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 349 | call, skb, skb->mark, skb->head); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 350 | if (ret < 0) |
| 351 | goto out; |
| 352 | |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 353 | rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more); |
| 354 | skb = NULL; |
| 355 | } |
| 356 | } while (msg_data_left(msg) > 0); |
| 357 | |
| 358 | success: |
| 359 | ret = copied; |
| 360 | out: |
| 361 | call->tx_pending = skb; |
| 362 | _leave(" = %d", ret); |
| 363 | return ret; |
| 364 | |
| 365 | call_terminated: |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 366 | rxrpc_free_skb(skb, rxrpc_skb_tx_freed); |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 367 | _leave(" = %d", -call->error); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 368 | return -call->error; |
David Howells | 0b58b8a | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 369 | |
| 370 | maybe_error: |
| 371 | if (copied) |
| 372 | goto success; |
| 373 | goto out; |
| 374 | |
| 375 | efault: |
| 376 | ret = -EFAULT; |
| 377 | goto out; |
| 378 | } |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | * extract control messages from the sendmsg() control buffer |
| 382 | */ |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 383 | static int rxrpc_sendmsg_cmsg(struct msghdr *msg, struct rxrpc_send_params *p) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 384 | { |
| 385 | struct cmsghdr *cmsg; |
| 386 | bool got_user_ID = false; |
| 387 | int len; |
| 388 | |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 389 | if (msg->msg_controllen == 0) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | for_each_cmsghdr(cmsg, msg) { |
| 393 | if (!CMSG_OK(msg, cmsg)) |
| 394 | return -EINVAL; |
| 395 | |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 396 | len = cmsg->cmsg_len - sizeof(struct cmsghdr); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 397 | _debug("CMSG %d, %d, %d", |
| 398 | cmsg->cmsg_level, cmsg->cmsg_type, len); |
| 399 | |
| 400 | if (cmsg->cmsg_level != SOL_RXRPC) |
| 401 | continue; |
| 402 | |
| 403 | switch (cmsg->cmsg_type) { |
| 404 | case RXRPC_USER_CALL_ID: |
| 405 | if (msg->msg_flags & MSG_CMSG_COMPAT) { |
| 406 | if (len != sizeof(u32)) |
| 407 | return -EINVAL; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 408 | p->user_call_ID = *(u32 *)CMSG_DATA(cmsg); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 409 | } else { |
| 410 | if (len != sizeof(unsigned long)) |
| 411 | return -EINVAL; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 412 | p->user_call_ID = *(unsigned long *) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 413 | CMSG_DATA(cmsg); |
| 414 | } |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 415 | got_user_ID = true; |
| 416 | break; |
| 417 | |
| 418 | case RXRPC_ABORT: |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 419 | if (p->command != RXRPC_CMD_SEND_DATA) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 420 | return -EINVAL; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 421 | p->command = RXRPC_CMD_SEND_ABORT; |
| 422 | if (len != sizeof(p->abort_code)) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 423 | return -EINVAL; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 424 | p->abort_code = *(unsigned int *)CMSG_DATA(cmsg); |
| 425 | if (p->abort_code == 0) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 426 | return -EINVAL; |
| 427 | break; |
| 428 | |
| 429 | case RXRPC_ACCEPT: |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 430 | if (p->command != RXRPC_CMD_SEND_DATA) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 431 | return -EINVAL; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 432 | p->command = RXRPC_CMD_ACCEPT; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 433 | if (len != 0) |
| 434 | return -EINVAL; |
| 435 | break; |
| 436 | |
| 437 | case RXRPC_EXCLUSIVE_CALL: |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 438 | p->exclusive = true; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 439 | if (len != 0) |
| 440 | return -EINVAL; |
| 441 | break; |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 442 | |
| 443 | case RXRPC_UPGRADE_SERVICE: |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 444 | p->upgrade = true; |
David Howells | 4e25572 | 2017-06-05 14:30:49 +0100 | [diff] [blame] | 445 | if (len != 0) |
| 446 | return -EINVAL; |
| 447 | break; |
| 448 | |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 449 | case RXRPC_TX_LENGTH: |
| 450 | if (p->tx_total_len != -1 || len != sizeof(__s64)) |
| 451 | return -EINVAL; |
| 452 | p->tx_total_len = *(__s64 *)CMSG_DATA(cmsg); |
| 453 | if (p->tx_total_len < 0) |
| 454 | return -EINVAL; |
| 455 | break; |
| 456 | |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 457 | default: |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (!got_user_ID) |
| 463 | return -EINVAL; |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 464 | if (p->tx_total_len != -1 && p->command != RXRPC_CMD_SEND_DATA) |
| 465 | return -EINVAL; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 466 | _leave(" = 0"); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 471 | * Create a new client call for sendmsg(). |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 472 | * - Called with the socket lock held, which it must release. |
| 473 | * - If it returns a call, the call's lock will need releasing by the caller. |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 474 | */ |
| 475 | static struct rxrpc_call * |
| 476 | rxrpc_new_client_call_for_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 477 | struct rxrpc_send_params *p) |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 478 | __releases(&rx->sk.sk_lock.slock) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 479 | { |
| 480 | struct rxrpc_conn_parameters cp; |
| 481 | struct rxrpc_call *call; |
| 482 | struct key *key; |
| 483 | |
| 484 | DECLARE_SOCKADDR(struct sockaddr_rxrpc *, srx, msg->msg_name); |
| 485 | |
| 486 | _enter(""); |
| 487 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 488 | if (!msg->msg_name) { |
| 489 | release_sock(&rx->sk); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 490 | return ERR_PTR(-EDESTADDRREQ); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 491 | } |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 492 | |
| 493 | key = rx->key; |
| 494 | if (key && !rx->key->payload.data[0]) |
| 495 | key = NULL; |
| 496 | |
| 497 | memset(&cp, 0, sizeof(cp)); |
| 498 | cp.local = rx->local; |
| 499 | cp.key = rx->key; |
| 500 | cp.security_level = rx->min_sec_level; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 501 | cp.exclusive = rx->exclusive | p->exclusive; |
| 502 | cp.upgrade = p->upgrade; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 503 | cp.service_id = srx->srx_service; |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 504 | call = rxrpc_new_client_call(rx, &cp, srx, p->user_call_ID, |
| 505 | p->tx_total_len, GFP_KERNEL); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 506 | /* The socket is now unlocked */ |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 507 | |
| 508 | _leave(" = %p\n", call); |
| 509 | return call; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * send a message forming part of a client call through an RxRPC socket |
| 514 | * - caller holds the socket locked |
| 515 | * - the socket may be either a client socket or a server socket |
| 516 | */ |
| 517 | int rxrpc_do_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len) |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 518 | __releases(&rx->sk.sk_lock.slock) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 519 | { |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 520 | enum rxrpc_call_state state; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 521 | struct rxrpc_call *call; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 522 | int ret; |
| 523 | |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 524 | struct rxrpc_send_params p = { |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 525 | .tx_total_len = -1, |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 526 | .user_call_ID = 0, |
| 527 | .abort_code = 0, |
| 528 | .command = RXRPC_CMD_SEND_DATA, |
| 529 | .exclusive = false, |
| 530 | .upgrade = true, |
| 531 | }; |
| 532 | |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 533 | _enter(""); |
| 534 | |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 535 | ret = rxrpc_sendmsg_cmsg(msg, &p); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 536 | if (ret < 0) |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 537 | goto error_release_sock; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 538 | |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 539 | if (p.command == RXRPC_CMD_ACCEPT) { |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 540 | ret = -EINVAL; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 541 | if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 542 | goto error_release_sock; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 543 | call = rxrpc_accept_call(rx, p.user_call_ID, NULL); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 544 | /* The socket is now unlocked. */ |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 545 | if (IS_ERR(call)) |
| 546 | return PTR_ERR(call); |
David Howells | fff7242 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 547 | rxrpc_put_call(call, rxrpc_call_put); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 551 | call = rxrpc_find_call_by_user_ID(rx, p.user_call_ID); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 552 | if (!call) { |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 553 | ret = -EBADSLT; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 554 | if (p.command != RXRPC_CMD_SEND_DATA) |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 555 | goto error_release_sock; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 556 | call = rxrpc_new_client_call_for_sendmsg(rx, msg, &p); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 557 | /* The socket is now unlocked... */ |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 558 | if (IS_ERR(call)) |
| 559 | return PTR_ERR(call); |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 560 | /* ... and we have the call lock. */ |
| 561 | } else { |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 562 | switch (READ_ONCE(call->state)) { |
| 563 | case RXRPC_CALL_UNINITIALISED: |
| 564 | case RXRPC_CALL_CLIENT_AWAIT_CONN: |
| 565 | case RXRPC_CALL_SERVER_PREALLOC: |
| 566 | case RXRPC_CALL_SERVER_SECURING: |
| 567 | case RXRPC_CALL_SERVER_ACCEPTING: |
| 568 | ret = -EBUSY; |
David Howells | 37411ca | 2017-03-02 23:48:52 +0000 | [diff] [blame] | 569 | goto error_release_sock; |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 570 | default: |
| 571 | break; |
| 572 | } |
David Howells | 37411ca | 2017-03-02 23:48:52 +0000 | [diff] [blame] | 573 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 574 | ret = mutex_lock_interruptible(&call->user_mutex); |
| 575 | release_sock(&rx->sk); |
| 576 | if (ret < 0) { |
| 577 | ret = -ERESTARTSYS; |
| 578 | goto error_put; |
| 579 | } |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 580 | |
| 581 | if (p.tx_total_len != -1) { |
| 582 | ret = -EINVAL; |
| 583 | if (call->tx_total_len != -1 || |
| 584 | call->tx_pending || |
| 585 | call->tx_top != 0) |
| 586 | goto error_put; |
| 587 | call->tx_total_len = p.tx_total_len; |
| 588 | } |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 589 | } |
| 590 | |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 591 | state = READ_ONCE(call->state); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 592 | _debug("CALL %d USR %lx ST %d on CONN %p", |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 593 | call->debug_id, call->user_call_ID, state, call->conn); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 594 | |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 595 | if (state >= RXRPC_CALL_COMPLETE) { |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 596 | /* it's too late for this call */ |
| 597 | ret = -ESHUTDOWN; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 598 | } else if (p.command == RXRPC_CMD_SEND_ABORT) { |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 599 | ret = 0; |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 600 | if (rxrpc_abort_call("CMD", call, 0, p.abort_code, -ECONNABORTED)) |
David Howells | 26cb02a | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 601 | ret = rxrpc_send_abort_packet(call); |
David Howells | 3ab26a6 | 2017-06-07 14:41:52 +0100 | [diff] [blame] | 602 | } else if (p.command != RXRPC_CMD_SEND_DATA) { |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 603 | ret = -EINVAL; |
| 604 | } else if (rxrpc_is_client_call(call) && |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 605 | state != RXRPC_CALL_CLIENT_SEND_REQUEST) { |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 606 | /* request phase complete for this client call */ |
| 607 | ret = -EPROTO; |
| 608 | } else if (rxrpc_is_service_call(call) && |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 609 | state != RXRPC_CALL_SERVER_ACK_REQUEST && |
| 610 | state != RXRPC_CALL_SERVER_SEND_REPLY) { |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 611 | /* Reply phase not begun or not complete for service call. */ |
| 612 | ret = -EPROTO; |
| 613 | } else { |
| 614 | ret = rxrpc_send_data(rx, call, msg, len); |
| 615 | } |
| 616 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 617 | mutex_unlock(&call->user_mutex); |
| 618 | error_put: |
David Howells | fff7242 | 2016-09-07 14:34:21 +0100 | [diff] [blame] | 619 | rxrpc_put_call(call, rxrpc_call_put); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 620 | _leave(" = %d", ret); |
| 621 | return ret; |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 622 | |
| 623 | error_release_sock: |
| 624 | release_sock(&rx->sk); |
| 625 | return ret; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
| 629 | * rxrpc_kernel_send_data - Allow a kernel service to send data on a call |
| 630 | * @sock: The socket the call is on |
| 631 | * @call: The call to send data through |
| 632 | * @msg: The data to send |
| 633 | * @len: The amount of data to send |
| 634 | * |
| 635 | * Allow a kernel service to send data on a call. The call must be in an state |
| 636 | * appropriate to sending data. No control data should be supplied in @msg, |
| 637 | * nor should an address be supplied. MSG_MORE should be flagged if there's |
| 638 | * more data to come, otherwise this data will end the transmission phase. |
| 639 | */ |
| 640 | int rxrpc_kernel_send_data(struct socket *sock, struct rxrpc_call *call, |
| 641 | struct msghdr *msg, size_t len) |
| 642 | { |
| 643 | int ret; |
| 644 | |
| 645 | _enter("{%d,%s},", call->debug_id, rxrpc_call_states[call->state]); |
| 646 | |
| 647 | ASSERTCMP(msg->msg_name, ==, NULL); |
| 648 | ASSERTCMP(msg->msg_control, ==, NULL); |
| 649 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 650 | mutex_lock(&call->user_mutex); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 651 | |
| 652 | _debug("CALL %d USR %lx ST %d on CONN %p", |
| 653 | call->debug_id, call->user_call_ID, call->state, call->conn); |
| 654 | |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 655 | switch (READ_ONCE(call->state)) { |
| 656 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
| 657 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 658 | case RXRPC_CALL_SERVER_SEND_REPLY: |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 659 | ret = rxrpc_send_data(rxrpc_sk(sock->sk), call, msg, len); |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 660 | break; |
| 661 | case RXRPC_CALL_COMPLETE: |
David Howells | 6fc166d | 2017-03-09 08:10:32 +0000 | [diff] [blame] | 662 | read_lock_bh(&call->state_lock); |
| 663 | ret = -call->error; |
| 664 | read_unlock_bh(&call->state_lock); |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 665 | break; |
| 666 | default: |
David Howells | fb46f6e | 2017-04-06 10:12:00 +0100 | [diff] [blame] | 667 | /* Request phase complete for this client call */ |
| 668 | trace_rxrpc_rx_eproto(call, 0, tracepoint_string("late_send")); |
David Howells | 146d8fe | 2017-03-04 00:01:41 +0000 | [diff] [blame] | 669 | ret = -EPROTO; |
| 670 | break; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 671 | } |
| 672 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 673 | mutex_unlock(&call->user_mutex); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 674 | _leave(" = %d", ret); |
| 675 | return ret; |
| 676 | } |
| 677 | EXPORT_SYMBOL(rxrpc_kernel_send_data); |
| 678 | |
| 679 | /** |
| 680 | * rxrpc_kernel_abort_call - Allow a kernel service to abort a call |
| 681 | * @sock: The socket the call is on |
| 682 | * @call: The call to be aborted |
| 683 | * @abort_code: The abort code to stick into the ABORT packet |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 684 | * @error: Local error value |
| 685 | * @why: 3-char string indicating why. |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 686 | * |
David Howells | 84a4c09 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 687 | * Allow a kernel service to abort a call, if it's still in an abortable state |
| 688 | * and return true if the call was aborted, false if it was already complete. |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 689 | */ |
David Howells | 84a4c09 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 690 | bool rxrpc_kernel_abort_call(struct socket *sock, struct rxrpc_call *call, |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 691 | u32 abort_code, int error, const char *why) |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 692 | { |
David Howells | 84a4c09 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 693 | bool aborted; |
| 694 | |
David Howells | 5a42976 | 2016-09-06 22:19:51 +0100 | [diff] [blame] | 695 | _enter("{%d},%d,%d,%s", call->debug_id, abort_code, error, why); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 696 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 697 | mutex_lock(&call->user_mutex); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 698 | |
David Howells | 84a4c09 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 699 | aborted = rxrpc_abort_call(why, call, 0, abort_code, error); |
| 700 | if (aborted) |
David Howells | 26cb02a | 2016-10-06 08:11:49 +0100 | [diff] [blame] | 701 | rxrpc_send_abort_packet(call); |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 702 | |
David Howells | 540b1c4 | 2017-02-27 15:43:06 +0000 | [diff] [blame] | 703 | mutex_unlock(&call->user_mutex); |
David Howells | 84a4c09 | 2017-04-06 10:11:59 +0100 | [diff] [blame] | 704 | return aborted; |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 705 | } |
David Howells | df423a4 | 2016-09-02 22:39:45 +0100 | [diff] [blame] | 706 | EXPORT_SYMBOL(rxrpc_kernel_abort_call); |
David Howells | e754eba | 2017-06-07 12:40:03 +0100 | [diff] [blame] | 707 | |
| 708 | /** |
| 709 | * rxrpc_kernel_set_tx_length - Set the total Tx length on a call |
| 710 | * @sock: The socket the call is on |
| 711 | * @call: The call to be informed |
| 712 | * @tx_total_len: The amount of data to be transmitted for this call |
| 713 | * |
| 714 | * Allow a kernel service to set the total transmit length on a call. This |
| 715 | * allows buffer-to-packet encrypt-and-copy to be performed. |
| 716 | * |
| 717 | * This function is primarily for use for setting the reply length since the |
| 718 | * request length can be set when beginning the call. |
| 719 | */ |
| 720 | void rxrpc_kernel_set_tx_length(struct socket *sock, struct rxrpc_call *call, |
| 721 | s64 tx_total_len) |
| 722 | { |
| 723 | WARN_ON(call->tx_total_len != -1); |
| 724 | call->tx_total_len = tx_total_len; |
| 725 | } |
| 726 | EXPORT_SYMBOL(rxrpc_kernel_set_tx_length); |