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