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