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