Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
17926a79 DH |
2 | /* connection-level event handling |
3 | * | |
4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells (dhowells@redhat.com) | |
17926a79 DH |
6 | */ |
7 | ||
9b6d5398 JP |
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
9 | ||
17926a79 DH |
10 | #include <linux/module.h> |
11 | #include <linux/net.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/errqueue.h> | |
17926a79 DH |
14 | #include <net/sock.h> |
15 | #include <net/af_rxrpc.h> | |
16 | #include <net/ip.h> | |
17 | #include "ar-internal.h" | |
18 | ||
a00ce28b DH |
19 | /* |
20 | * Set the completion state on an aborted connection. | |
21 | */ | |
22 | static bool rxrpc_set_conn_aborted(struct rxrpc_connection *conn, struct sk_buff *skb, | |
23 | s32 abort_code, int err, | |
24 | enum rxrpc_call_completion compl) | |
25 | { | |
26 | bool aborted = false; | |
27 | ||
28 | if (conn->state != RXRPC_CONN_ABORTED) { | |
29 | spin_lock(&conn->state_lock); | |
30 | if (conn->state != RXRPC_CONN_ABORTED) { | |
31 | conn->abort_code = abort_code; | |
32 | conn->error = err; | |
33 | conn->completion = compl; | |
34 | /* Order the abort info before the state change. */ | |
35 | smp_store_release(&conn->state, RXRPC_CONN_ABORTED); | |
36 | set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); | |
37 | set_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events); | |
38 | aborted = true; | |
39 | } | |
40 | spin_unlock(&conn->state_lock); | |
41 | } | |
42 | ||
43 | return aborted; | |
44 | } | |
45 | ||
46 | /* | |
47 | * Mark a socket buffer to indicate that the connection it's on should be aborted. | |
48 | */ | |
49 | int rxrpc_abort_conn(struct rxrpc_connection *conn, struct sk_buff *skb, | |
57af281e | 50 | s32 abort_code, int err, enum rxrpc_abort_reason why) |
a00ce28b DH |
51 | { |
52 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); | |
53 | ||
54 | if (rxrpc_set_conn_aborted(conn, skb, abort_code, err, | |
55 | RXRPC_CALL_LOCALLY_ABORTED)) { | |
56 | trace_rxrpc_abort(0, why, sp->hdr.cid, sp->hdr.callNumber, | |
57 | sp->hdr.seq, abort_code, err); | |
58 | rxrpc_poke_conn(conn, rxrpc_conn_get_poke_abort); | |
59 | } | |
60 | return -EPROTO; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Mark a connection as being remotely aborted. | |
65 | */ | |
66 | static bool rxrpc_input_conn_abort(struct rxrpc_connection *conn, | |
67 | struct sk_buff *skb) | |
68 | { | |
69 | return rxrpc_set_conn_aborted(conn, skb, skb->priority, -ECONNABORTED, | |
70 | RXRPC_CALL_REMOTELY_ABORTED); | |
71 | } | |
72 | ||
18bfeba5 DH |
73 | /* |
74 | * Retransmit terminal ACK or ABORT of the previous call. | |
75 | */ | |
30df927b DH |
76 | void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn, |
77 | struct sk_buff *skb, | |
78 | unsigned int channel) | |
18bfeba5 | 79 | { |
3136ef49 | 80 | struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL; |
18bfeba5 DH |
81 | struct rxrpc_channel *chan; |
82 | struct msghdr msg; | |
5fc62f6a | 83 | struct kvec iov[3]; |
18bfeba5 DH |
84 | struct { |
85 | struct rxrpc_wire_header whdr; | |
86 | union { | |
5fc62f6a DH |
87 | __be32 abort_code; |
88 | struct rxrpc_ackpacket ack; | |
18bfeba5 DH |
89 | }; |
90 | } __attribute__((packed)) pkt; | |
17469ae0 | 91 | struct rxrpc_acktrailer trailer; |
18bfeba5 | 92 | size_t len; |
6b47fe1d | 93 | int ret, ioc; |
5fc62f6a | 94 | u32 serial, mtu, call_id, padding; |
18bfeba5 DH |
95 | |
96 | _enter("%d", conn->debug_id); | |
97 | ||
6f769f22 DH |
98 | if (sp && sp->hdr.type == RXRPC_PACKET_TYPE_ACK) { |
99 | if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header), | |
100 | &pkt.ack, sizeof(pkt.ack)) < 0) | |
101 | return; | |
102 | if (pkt.ack.reason == RXRPC_ACK_PING_RESPONSE) | |
103 | return; | |
104 | } | |
105 | ||
3136ef49 | 106 | chan = &conn->channels[channel]; |
18bfeba5 DH |
107 | |
108 | /* If the last call got moved on whilst we were waiting to run, just | |
109 | * ignore this packet. | |
110 | */ | |
9d35d880 | 111 | call_id = chan->last_call; |
3136ef49 | 112 | if (skb && call_id != sp->hdr.callNumber) |
18bfeba5 DH |
113 | return; |
114 | ||
2cc80086 DH |
115 | msg.msg_name = &conn->peer->srx.transport; |
116 | msg.msg_namelen = conn->peer->srx.transport_len; | |
18bfeba5 DH |
117 | msg.msg_control = NULL; |
118 | msg.msg_controllen = 0; | |
119 | msg.msg_flags = 0; | |
120 | ||
5fc62f6a DH |
121 | iov[0].iov_base = &pkt; |
122 | iov[0].iov_len = sizeof(pkt.whdr); | |
123 | iov[1].iov_base = &padding; | |
124 | iov[1].iov_len = 3; | |
17469ae0 DH |
125 | iov[2].iov_base = &trailer; |
126 | iov[2].iov_len = sizeof(trailer); | |
5fc62f6a | 127 | |
f3104141 | 128 | serial = rxrpc_get_next_serial(conn); |
9d35d880 | 129 | |
3136ef49 | 130 | pkt.whdr.epoch = htonl(conn->proto.epoch); |
fb1967a6 | 131 | pkt.whdr.cid = htonl(conn->proto.cid | channel); |
3136ef49 | 132 | pkt.whdr.callNumber = htonl(call_id); |
9d35d880 | 133 | pkt.whdr.serial = htonl(serial); |
18bfeba5 DH |
134 | pkt.whdr.seq = 0; |
135 | pkt.whdr.type = chan->last_type; | |
136 | pkt.whdr.flags = conn->out_clientflag; | |
137 | pkt.whdr.userStatus = 0; | |
138 | pkt.whdr.securityIndex = conn->security_ix; | |
139 | pkt.whdr._rsvd = 0; | |
68d6d1ae | 140 | pkt.whdr.serviceId = htons(conn->service_id); |
18bfeba5 DH |
141 | |
142 | len = sizeof(pkt.whdr); | |
143 | switch (chan->last_type) { | |
144 | case RXRPC_PACKET_TYPE_ABORT: | |
5fc62f6a DH |
145 | pkt.abort_code = htonl(chan->last_abort); |
146 | iov[0].iov_len += sizeof(pkt.abort_code); | |
147 | len += sizeof(pkt.abort_code); | |
148 | ioc = 1; | |
18bfeba5 DH |
149 | break; |
150 | ||
151 | case RXRPC_PACKET_TYPE_ACK: | |
2cc80086 DH |
152 | mtu = conn->peer->if_mtu; |
153 | mtu -= conn->peer->hdrsize; | |
18bfeba5 | 154 | pkt.ack.bufferSpace = 0; |
3136ef49 DH |
155 | pkt.ack.maxSkew = htons(skb ? skb->priority : 0); |
156 | pkt.ack.firstPacket = htonl(chan->last_seq + 1); | |
157 | pkt.ack.previousPacket = htonl(chan->last_seq); | |
158 | pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0); | |
159 | pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE; | |
18bfeba5 | 160 | pkt.ack.nAcks = 0; |
17469ae0 DH |
161 | trailer.maxMTU = htonl(rxrpc_rx_mtu); |
162 | trailer.ifMTU = htonl(mtu); | |
163 | trailer.rwind = htonl(rxrpc_rx_window_size); | |
164 | trailer.jumbo_max = htonl(rxrpc_rx_jumbo_max); | |
57494343 | 165 | pkt.whdr.flags |= RXRPC_SLOW_START_OK; |
5fc62f6a DH |
166 | padding = 0; |
167 | iov[0].iov_len += sizeof(pkt.ack); | |
17469ae0 | 168 | len += sizeof(pkt.ack) + 3 + sizeof(trailer); |
5fc62f6a | 169 | ioc = 3; |
18bfeba5 | 170 | |
4764c0da | 171 | trace_rxrpc_tx_ack(chan->call_debug_id, serial, |
f3f8337c DH |
172 | ntohl(pkt.ack.firstPacket), |
173 | ntohl(pkt.ack.serial), | |
f789bff2 | 174 | pkt.ack.reason, 0, rxrpc_rx_window_size); |
18bfeba5 | 175 | break; |
9d35d880 DH |
176 | |
177 | default: | |
178 | return; | |
18bfeba5 DH |
179 | } |
180 | ||
2cc80086 DH |
181 | ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len); |
182 | conn->peer->last_tx_at = ktime_get_seconds(); | |
6b47fe1d | 183 | if (ret < 0) |
4764c0da DH |
184 | trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret, |
185 | rxrpc_tx_point_call_final_resend); | |
186 | else | |
187 | trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr, | |
188 | rxrpc_tx_point_call_final_resend); | |
6b47fe1d | 189 | |
18bfeba5 | 190 | _leave(""); |
18bfeba5 DH |
191 | } |
192 | ||
17926a79 DH |
193 | /* |
194 | * pass a connection-level abort onto all calls on that connection | |
195 | */ | |
a00ce28b | 196 | static void rxrpc_abort_calls(struct rxrpc_connection *conn) |
17926a79 DH |
197 | { |
198 | struct rxrpc_call *call; | |
248f219c | 199 | int i; |
17926a79 | 200 | |
64753092 | 201 | _enter("{%d},%x", conn->debug_id, conn->abort_code); |
17926a79 | 202 | |
a1399f8b | 203 | for (i = 0; i < RXRPC_MAXCALLS; i++) { |
9d35d880 | 204 | call = conn->channels[i].call; |
a00ce28b DH |
205 | if (call) |
206 | rxrpc_set_call_completion(call, | |
207 | conn->completion, | |
5ac0d622 DH |
208 | conn->abort_code, |
209 | conn->error); | |
17926a79 DH |
210 | } |
211 | ||
17926a79 DH |
212 | _leave(""); |
213 | } | |
214 | ||
17926a79 DH |
215 | /* |
216 | * mark a call as being on a now-secured channel | |
248f219c | 217 | * - must be called with BH's disabled. |
17926a79 | 218 | */ |
5eaa65b2 | 219 | static void rxrpc_call_is_secure(struct rxrpc_call *call) |
17926a79 | 220 | { |
96b4059f DH |
221 | if (call && __rxrpc_call_state(call) == RXRPC_CALL_SERVER_SECURING) { |
222 | rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST); | |
223 | rxrpc_notify_socket(call); | |
17926a79 DH |
224 | } |
225 | } | |
226 | ||
227 | /* | |
228 | * connection-level Rx packet processor | |
229 | */ | |
230 | static int rxrpc_process_event(struct rxrpc_connection *conn, | |
a00ce28b | 231 | struct sk_buff *skb) |
17926a79 DH |
232 | { |
233 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); | |
2953d3b8 | 234 | int ret; |
17926a79 | 235 | |
a00ce28b | 236 | if (conn->state == RXRPC_CONN_ABORTED) |
17926a79 DH |
237 | return -ECONNABORTED; |
238 | ||
0d12f8a4 | 239 | _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial); |
519d2567 | 240 | |
17926a79 | 241 | switch (sp->hdr.type) { |
17926a79 | 242 | case RXRPC_PACKET_TYPE_CHALLENGE: |
a00ce28b | 243 | return conn->security->respond_to_challenge(conn, skb); |
17926a79 DH |
244 | |
245 | case RXRPC_PACKET_TYPE_RESPONSE: | |
a00ce28b | 246 | ret = conn->security->verify_response(conn, skb); |
17926a79 DH |
247 | if (ret < 0) |
248 | return ret; | |
249 | ||
41057ebd | 250 | ret = conn->security->init_connection_security( |
2cc80086 | 251 | conn, conn->key->payload.data[0]); |
17926a79 DH |
252 | if (ret < 0) |
253 | return ret; | |
254 | ||
3dd9c8b5 | 255 | spin_lock(&conn->state_lock); |
2953d3b8 | 256 | if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) |
bba304db | 257 | conn->state = RXRPC_CONN_SERVICE; |
2953d3b8 | 258 | spin_unlock(&conn->state_lock); |
17926a79 | 259 | |
2953d3b8 DH |
260 | if (conn->state == RXRPC_CONN_SERVICE) { |
261 | /* Offload call state flipping to the I/O thread. As | |
262 | * we've already received the packet, put it on the | |
263 | * front of the queue. | |
264 | */ | |
265 | skb->mark = RXRPC_SKB_MARK_SERVICE_CONN_SECURED; | |
266 | rxrpc_get_skb(skb, rxrpc_skb_get_conn_secured); | |
267 | skb_queue_head(&conn->local->rx_queue, skb); | |
268 | rxrpc_wake_up_io_thread(conn->local); | |
269 | } | |
17926a79 DH |
270 | return 0; |
271 | ||
272 | default: | |
57af281e | 273 | WARN_ON_ONCE(1); |
17926a79 DH |
274 | return -EPROTO; |
275 | } | |
276 | } | |
277 | ||
278 | /* | |
279 | * set up security and issue a challenge | |
280 | */ | |
281 | static void rxrpc_secure_connection(struct rxrpc_connection *conn) | |
282 | { | |
a00ce28b | 283 | if (conn->security->issue_challenge(conn) < 0) |
57af281e DH |
284 | rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM, |
285 | rxrpc_abort_nomem); | |
17926a79 DH |
286 | } |
287 | ||
3136ef49 DH |
288 | /* |
289 | * Process delayed final ACKs that we haven't subsumed into a subsequent call. | |
290 | */ | |
ddc7834a | 291 | void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force) |
3136ef49 DH |
292 | { |
293 | unsigned long j = jiffies, next_j; | |
294 | unsigned int channel; | |
295 | bool set; | |
296 | ||
297 | again: | |
298 | next_j = j + LONG_MAX; | |
299 | set = false; | |
300 | for (channel = 0; channel < RXRPC_MAXCALLS; channel++) { | |
301 | struct rxrpc_channel *chan = &conn->channels[channel]; | |
302 | unsigned long ack_at; | |
303 | ||
304 | if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags)) | |
305 | continue; | |
306 | ||
9d35d880 | 307 | ack_at = chan->final_ack_at; |
ddc7834a | 308 | if (time_before(j, ack_at) && !force) { |
3136ef49 DH |
309 | if (time_before(ack_at, next_j)) { |
310 | next_j = ack_at; | |
311 | set = true; | |
312 | } | |
313 | continue; | |
314 | } | |
315 | ||
316 | if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, | |
317 | &conn->flags)) | |
318 | rxrpc_conn_retransmit_call(conn, NULL, channel); | |
319 | } | |
320 | ||
321 | j = jiffies; | |
322 | if (time_before_eq(next_j, j)) | |
323 | goto again; | |
324 | if (set) | |
325 | rxrpc_reduce_conn_timer(conn, next_j); | |
326 | } | |
327 | ||
17926a79 DH |
328 | /* |
329 | * connection-level event processor | |
330 | */ | |
04d36d74 | 331 | static void rxrpc_do_process_connection(struct rxrpc_connection *conn) |
17926a79 | 332 | { |
17926a79 | 333 | struct sk_buff *skb; |
17926a79 DH |
334 | int ret; |
335 | ||
2c4579e4 | 336 | if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events)) |
17926a79 | 337 | rxrpc_secure_connection(conn); |
17926a79 DH |
338 | |
339 | /* go through the conn-level event packets, releasing the ref on this | |
340 | * connection that each one has when we've finished with it */ | |
341 | while ((skb = skb_dequeue(&conn->rx_queue))) { | |
9a36a6bc | 342 | rxrpc_see_skb(skb, rxrpc_skb_see_conn_work); |
a00ce28b | 343 | ret = rxrpc_process_event(conn, skb); |
17926a79 | 344 | switch (ret) { |
8c2f826d | 345 | case -ENOMEM: |
17926a79 | 346 | case -EAGAIN: |
a00ce28b DH |
347 | skb_queue_head(&conn->rx_queue, skb); |
348 | rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work); | |
349 | break; | |
17926a79 | 350 | default: |
9a36a6bc | 351 | rxrpc_free_skb(skb, rxrpc_skb_put_conn_work); |
17926a79 DH |
352 | break; |
353 | } | |
354 | } | |
04d36d74 DH |
355 | } |
356 | ||
357 | void rxrpc_process_connection(struct work_struct *work) | |
358 | { | |
359 | struct rxrpc_connection *conn = | |
360 | container_of(work, struct rxrpc_connection, processor); | |
361 | ||
7fa25105 | 362 | rxrpc_see_connection(conn, rxrpc_conn_see_work); |
04d36d74 | 363 | |
0fde882f | 364 | if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) { |
04d36d74 | 365 | rxrpc_do_process_connection(conn); |
0fde882f | 366 | rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work); |
04d36d74 | 367 | } |
17926a79 | 368 | } |
5e6ef4f1 DH |
369 | |
370 | /* | |
371 | * post connection-level events to the connection | |
372 | * - this includes challenges, responses, some aborts and call terminal packet | |
373 | * retransmission. | |
374 | */ | |
375 | static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn, | |
376 | struct sk_buff *skb) | |
377 | { | |
378 | _enter("%p,%p", conn, skb); | |
379 | ||
380 | rxrpc_get_skb(skb, rxrpc_skb_get_conn_work); | |
381 | skb_queue_tail(&conn->rx_queue, skb); | |
382 | rxrpc_queue_conn(conn, rxrpc_conn_queue_rx_work); | |
383 | } | |
384 | ||
385 | /* | |
386 | * Input a connection-level packet. | |
387 | */ | |
57af281e | 388 | bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb) |
5e6ef4f1 DH |
389 | { |
390 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); | |
391 | ||
5e6ef4f1 | 392 | switch (sp->hdr.type) { |
5e6ef4f1 DH |
393 | case RXRPC_PACKET_TYPE_BUSY: |
394 | /* Just ignore BUSY packets for now. */ | |
57af281e | 395 | return true; |
5e6ef4f1 DH |
396 | |
397 | case RXRPC_PACKET_TYPE_ABORT: | |
a00ce28b DH |
398 | if (rxrpc_is_conn_aborted(conn)) |
399 | return true; | |
400 | rxrpc_input_conn_abort(conn, skb); | |
401 | rxrpc_abort_calls(conn); | |
402 | return true; | |
5e6ef4f1 DH |
403 | |
404 | case RXRPC_PACKET_TYPE_CHALLENGE: | |
405 | case RXRPC_PACKET_TYPE_RESPONSE: | |
a00ce28b DH |
406 | if (rxrpc_is_conn_aborted(conn)) { |
407 | if (conn->completion == RXRPC_CALL_LOCALLY_ABORTED) | |
408 | rxrpc_send_conn_abort(conn); | |
409 | return true; | |
410 | } | |
5e6ef4f1 | 411 | rxrpc_post_packet_to_conn(conn, skb); |
57af281e | 412 | return true; |
5e6ef4f1 DH |
413 | |
414 | default: | |
57af281e DH |
415 | WARN_ON_ONCE(1); |
416 | return true; | |
5e6ef4f1 DH |
417 | } |
418 | } | |
f2cce89a DH |
419 | |
420 | /* | |
421 | * Input a connection event. | |
422 | */ | |
423 | void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb) | |
424 | { | |
2953d3b8 DH |
425 | unsigned int loop; |
426 | ||
a00ce28b DH |
427 | if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events)) |
428 | rxrpc_abort_calls(conn); | |
429 | ||
2953d3b8 DH |
430 | switch (skb->mark) { |
431 | case RXRPC_SKB_MARK_SERVICE_CONN_SECURED: | |
432 | if (conn->state != RXRPC_CONN_SERVICE) | |
433 | break; | |
434 | ||
2953d3b8 | 435 | for (loop = 0; loop < RXRPC_MAXCALLS; loop++) |
9d35d880 | 436 | rxrpc_call_is_secure(conn->channels[loop].call); |
2953d3b8 DH |
437 | break; |
438 | } | |
439 | ||
f2cce89a DH |
440 | /* Process delayed ACKs whose time has come. */ |
441 | if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK) | |
442 | rxrpc_process_delayed_final_acks(conn, false); | |
443 | } |