1 /* connection-level event handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/errqueue.h>
19 #include <net/af_rxrpc.h>
21 #include "ar-internal.h"
24 * Retransmit terminal ACK or ABORT of the previous call.
26 static void rxrpc_conn_retransmit_call(struct rxrpc_connection
*conn
,
30 struct rxrpc_skb_priv
*sp
= skb
? rxrpc_skb(skb
) : NULL
;
31 struct rxrpc_channel
*chan
;
35 struct rxrpc_wire_header whdr
;
38 struct rxrpc_ackpacket ack
;
40 } __attribute__((packed
)) pkt
;
41 struct rxrpc_ackinfo ack_info
;
44 u32 serial
, mtu
, call_id
, padding
;
46 _enter("%d", conn
->debug_id
);
48 chan
= &conn
->channels
[channel
];
50 /* If the last call got moved on whilst we were waiting to run, just
53 call_id
= READ_ONCE(chan
->last_call
);
54 /* Sync with __rxrpc_disconnect_call() */
56 if (skb
&& call_id
!= sp
->hdr
.callNumber
)
59 msg
.msg_name
= &conn
->params
.peer
->srx
.transport
;
60 msg
.msg_namelen
= conn
->params
.peer
->srx
.transport_len
;
61 msg
.msg_control
= NULL
;
62 msg
.msg_controllen
= 0;
65 iov
[0].iov_base
= &pkt
;
66 iov
[0].iov_len
= sizeof(pkt
.whdr
);
67 iov
[1].iov_base
= &padding
;
69 iov
[2].iov_base
= &ack_info
;
70 iov
[2].iov_len
= sizeof(ack_info
);
72 pkt
.whdr
.epoch
= htonl(conn
->proto
.epoch
);
73 pkt
.whdr
.cid
= htonl(conn
->proto
.cid
| channel
);
74 pkt
.whdr
.callNumber
= htonl(call_id
);
76 pkt
.whdr
.type
= chan
->last_type
;
77 pkt
.whdr
.flags
= conn
->out_clientflag
;
78 pkt
.whdr
.userStatus
= 0;
79 pkt
.whdr
.securityIndex
= conn
->security_ix
;
81 pkt
.whdr
.serviceId
= htons(conn
->service_id
);
83 len
= sizeof(pkt
.whdr
);
84 switch (chan
->last_type
) {
85 case RXRPC_PACKET_TYPE_ABORT
:
86 pkt
.abort_code
= htonl(chan
->last_abort
);
87 iov
[0].iov_len
+= sizeof(pkt
.abort_code
);
88 len
+= sizeof(pkt
.abort_code
);
92 case RXRPC_PACKET_TYPE_ACK
:
93 mtu
= conn
->params
.peer
->if_mtu
;
94 mtu
-= conn
->params
.peer
->hdrsize
;
95 pkt
.ack
.bufferSpace
= 0;
96 pkt
.ack
.maxSkew
= htons(skb
? skb
->priority
: 0);
97 pkt
.ack
.firstPacket
= htonl(chan
->last_seq
+ 1);
98 pkt
.ack
.previousPacket
= htonl(chan
->last_seq
);
99 pkt
.ack
.serial
= htonl(skb
? sp
->hdr
.serial
: 0);
100 pkt
.ack
.reason
= skb
? RXRPC_ACK_DUPLICATE
: RXRPC_ACK_IDLE
;
102 ack_info
.rxMTU
= htonl(rxrpc_rx_mtu
);
103 ack_info
.maxMTU
= htonl(mtu
);
104 ack_info
.rwind
= htonl(rxrpc_rx_window_size
);
105 ack_info
.jumbo_max
= htonl(rxrpc_rx_jumbo_max
);
106 pkt
.whdr
.flags
|= RXRPC_SLOW_START_OK
;
108 iov
[0].iov_len
+= sizeof(pkt
.ack
);
109 len
+= sizeof(pkt
.ack
) + 3 + sizeof(ack_info
);
117 /* Resync with __rxrpc_disconnect_call() and check that the last call
118 * didn't get advanced whilst we were filling out the packets.
121 if (READ_ONCE(chan
->last_call
) != call_id
)
124 serial
= atomic_inc_return(&conn
->serial
);
125 pkt
.whdr
.serial
= htonl(serial
);
127 switch (chan
->last_type
) {
128 case RXRPC_PACKET_TYPE_ABORT
:
129 _proto("Tx ABORT %%%u { %d } [re]", serial
, conn
->abort_code
);
131 case RXRPC_PACKET_TYPE_ACK
:
132 trace_rxrpc_tx_ack(NULL
, serial
, chan
->last_seq
, 0,
133 RXRPC_ACK_DUPLICATE
, 0);
134 _proto("Tx ACK %%%u [re]", serial
);
138 kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, ioc
, len
);
144 * pass a connection-level abort onto all calls on that connection
146 static void rxrpc_abort_calls(struct rxrpc_connection
*conn
,
147 enum rxrpc_call_completion
compl)
149 struct rxrpc_call
*call
;
152 _enter("{%d},%x", conn
->debug_id
, conn
->abort_code
);
154 spin_lock(&conn
->channel_lock
);
156 for (i
= 0; i
< RXRPC_MAXCALLS
; i
++) {
157 call
= rcu_dereference_protected(
158 conn
->channels
[i
].call
,
159 lockdep_is_held(&conn
->channel_lock
));
161 if (compl == RXRPC_CALL_LOCALLY_ABORTED
)
162 trace_rxrpc_abort("CON", call
->cid
,
166 if (rxrpc_set_call_completion(call
, compl,
169 rxrpc_notify_socket(call
);
173 spin_unlock(&conn
->channel_lock
);
178 * generate a connection-level abort
180 static int rxrpc_abort_connection(struct rxrpc_connection
*conn
,
181 int error
, u32 abort_code
)
183 struct rxrpc_wire_header whdr
;
191 _enter("%d,,%u,%u", conn
->debug_id
, error
, abort_code
);
193 /* generate a connection-level abort */
194 spin_lock_bh(&conn
->state_lock
);
195 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
196 spin_unlock_bh(&conn
->state_lock
);
197 _leave(" = 0 [already dead]");
202 conn
->abort_code
= abort_code
;
203 conn
->state
= RXRPC_CONN_LOCALLY_ABORTED
;
204 spin_unlock_bh(&conn
->state_lock
);
206 rxrpc_abort_calls(conn
, RXRPC_CALL_LOCALLY_ABORTED
);
208 msg
.msg_name
= &conn
->params
.peer
->srx
.transport
;
209 msg
.msg_namelen
= conn
->params
.peer
->srx
.transport_len
;
210 msg
.msg_control
= NULL
;
211 msg
.msg_controllen
= 0;
214 whdr
.epoch
= htonl(conn
->proto
.epoch
);
215 whdr
.cid
= htonl(conn
->proto
.cid
);
218 whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
219 whdr
.flags
= conn
->out_clientflag
;
221 whdr
.securityIndex
= conn
->security_ix
;
223 whdr
.serviceId
= htons(conn
->service_id
);
225 word
= htonl(conn
->abort_code
);
227 iov
[0].iov_base
= &whdr
;
228 iov
[0].iov_len
= sizeof(whdr
);
229 iov
[1].iov_base
= &word
;
230 iov
[1].iov_len
= sizeof(word
);
232 len
= iov
[0].iov_len
+ iov
[1].iov_len
;
234 serial
= atomic_inc_return(&conn
->serial
);
235 whdr
.serial
= htonl(serial
);
236 _proto("Tx CONN ABORT %%%u { %d }", serial
, conn
->abort_code
);
238 ret
= kernel_sendmsg(conn
->params
.local
->socket
, &msg
, iov
, 2, len
);
240 _debug("sendmsg failed: %d", ret
);
249 * mark a call as being on a now-secured channel
250 * - must be called with BH's disabled.
252 static void rxrpc_call_is_secure(struct rxrpc_call
*call
)
256 write_lock_bh(&call
->state_lock
);
257 if (call
->state
== RXRPC_CALL_SERVER_SECURING
) {
258 call
->state
= RXRPC_CALL_SERVER_ACCEPTING
;
259 rxrpc_notify_socket(call
);
261 write_unlock_bh(&call
->state_lock
);
266 * connection-level Rx packet processor
268 static int rxrpc_process_event(struct rxrpc_connection
*conn
,
272 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
277 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
278 _leave(" = -ECONNABORTED [%u]", conn
->state
);
279 return -ECONNABORTED
;
282 _enter("{%d},{%u,%%%u},", conn
->debug_id
, sp
->hdr
.type
, sp
->hdr
.serial
);
284 switch (sp
->hdr
.type
) {
285 case RXRPC_PACKET_TYPE_DATA
:
286 case RXRPC_PACKET_TYPE_ACK
:
287 rxrpc_conn_retransmit_call(conn
, skb
,
288 sp
->hdr
.cid
& RXRPC_CHANNELMASK
);
291 case RXRPC_PACKET_TYPE_BUSY
:
292 /* Just ignore BUSY packets for now. */
295 case RXRPC_PACKET_TYPE_ABORT
:
296 if (skb_copy_bits(skb
, sizeof(struct rxrpc_wire_header
),
297 &wtmp
, sizeof(wtmp
)) < 0) {
298 trace_rxrpc_rx_eproto(NULL
, sp
->hdr
.serial
,
299 tracepoint_string("bad_abort"));
302 abort_code
= ntohl(wtmp
);
303 _proto("Rx ABORT %%%u { ac=%d }", sp
->hdr
.serial
, abort_code
);
305 conn
->error
= -ECONNABORTED
;
306 conn
->abort_code
= abort_code
;
307 conn
->state
= RXRPC_CONN_REMOTELY_ABORTED
;
308 rxrpc_abort_calls(conn
, RXRPC_CALL_REMOTELY_ABORTED
);
309 return -ECONNABORTED
;
311 case RXRPC_PACKET_TYPE_CHALLENGE
:
312 return conn
->security
->respond_to_challenge(conn
, skb
,
315 case RXRPC_PACKET_TYPE_RESPONSE
:
316 ret
= conn
->security
->verify_response(conn
, skb
, _abort_code
);
320 ret
= conn
->security
->init_connection_security(conn
);
324 ret
= conn
->security
->prime_packet_security(conn
);
328 spin_lock(&conn
->channel_lock
);
329 spin_lock(&conn
->state_lock
);
331 if (conn
->state
== RXRPC_CONN_SERVICE_CHALLENGING
) {
332 conn
->state
= RXRPC_CONN_SERVICE
;
333 spin_unlock(&conn
->state_lock
);
334 for (loop
= 0; loop
< RXRPC_MAXCALLS
; loop
++)
335 rxrpc_call_is_secure(
336 rcu_dereference_protected(
337 conn
->channels
[loop
].call
,
338 lockdep_is_held(&conn
->channel_lock
)));
340 spin_unlock(&conn
->state_lock
);
343 spin_unlock(&conn
->channel_lock
);
347 trace_rxrpc_rx_eproto(NULL
, sp
->hdr
.serial
,
348 tracepoint_string("bad_conn_pkt"));
354 * set up security and issue a challenge
356 static void rxrpc_secure_connection(struct rxrpc_connection
*conn
)
361 _enter("{%d}", conn
->debug_id
);
363 ASSERT(conn
->security_ix
!= 0);
365 if (!conn
->params
.key
) {
366 _debug("set up security");
367 ret
= rxrpc_init_server_conn_security(conn
);
372 abort_code
= RX_CALL_DEAD
;
375 abort_code
= RXKADNOAUTH
;
380 if (conn
->security
->issue_challenge(conn
) < 0) {
381 abort_code
= RX_CALL_DEAD
;
390 _debug("abort %d, %d", ret
, abort_code
);
391 rxrpc_abort_connection(conn
, ret
, abort_code
);
392 _leave(" [aborted]");
396 * Process delayed final ACKs that we haven't subsumed into a subsequent call.
398 static void rxrpc_process_delayed_final_acks(struct rxrpc_connection
*conn
)
400 unsigned long j
= jiffies
, next_j
;
401 unsigned int channel
;
405 next_j
= j
+ LONG_MAX
;
407 for (channel
= 0; channel
< RXRPC_MAXCALLS
; channel
++) {
408 struct rxrpc_channel
*chan
= &conn
->channels
[channel
];
409 unsigned long ack_at
;
411 if (!test_bit(RXRPC_CONN_FINAL_ACK_0
+ channel
, &conn
->flags
))
414 smp_rmb(); /* vs rxrpc_disconnect_client_call */
415 ack_at
= READ_ONCE(chan
->final_ack_at
);
417 if (time_before(j
, ack_at
)) {
418 if (time_before(ack_at
, next_j
)) {
425 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0
+ channel
,
427 rxrpc_conn_retransmit_call(conn
, NULL
, channel
);
431 if (time_before_eq(next_j
, j
))
434 rxrpc_reduce_conn_timer(conn
, next_j
);
438 * connection-level event processor
440 void rxrpc_process_connection(struct work_struct
*work
)
442 struct rxrpc_connection
*conn
=
443 container_of(work
, struct rxrpc_connection
, processor
);
445 u32 abort_code
= RX_PROTOCOL_ERROR
;
448 rxrpc_see_connection(conn
);
450 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE
, &conn
->events
))
451 rxrpc_secure_connection(conn
);
453 /* Process delayed ACKs whose time has come. */
454 if (conn
->flags
& RXRPC_CONN_FINAL_ACK_MASK
)
455 rxrpc_process_delayed_final_acks(conn
);
457 /* go through the conn-level event packets, releasing the ref on this
458 * connection that each one has when we've finished with it */
459 while ((skb
= skb_dequeue(&conn
->rx_queue
))) {
460 rxrpc_see_skb(skb
, rxrpc_skb_rx_seen
);
461 ret
= rxrpc_process_event(conn
, skb
, &abort_code
);
469 goto requeue_and_leave
;
472 rxrpc_free_skb(skb
, rxrpc_skb_rx_freed
);
478 rxrpc_put_connection(conn
);
483 skb_queue_head(&conn
->rx_queue
, skb
);
487 if (rxrpc_abort_connection(conn
, ret
, abort_code
) < 0)
488 goto requeue_and_leave
;
489 rxrpc_free_skb(skb
, rxrpc_skb_rx_freed
);