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