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