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