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