1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* connection-level event handling
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/errqueue.h>
15 #include <net/af_rxrpc.h>
17 #include "ar-internal.h"
20 * Set the completion state on an aborted connection.
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)
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;
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);
40 spin_unlock(&conn->state_lock);
47 * Mark a socket buffer to indicate that the connection it's on should be aborted.
49 int rxrpc_abort_conn(struct rxrpc_connection *conn, struct sk_buff *skb,
50 s32 abort_code, int err, enum rxrpc_abort_reason why)
52 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
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);
64 * Mark a connection as being remotely aborted.
66 static bool rxrpc_input_conn_abort(struct rxrpc_connection *conn,
69 return rxrpc_set_conn_aborted(conn, skb, skb->priority, -ECONNABORTED,
70 RXRPC_CALL_REMOTELY_ABORTED);
74 * Retransmit terminal ACK or ABORT of the previous call.
76 void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
80 struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
81 struct rxrpc_channel *chan;
85 struct rxrpc_wire_header whdr;
88 struct rxrpc_ackpacket ack;
90 } __attribute__((packed)) pkt;
91 struct rxrpc_ackinfo ack_info;
94 u32 serial, mtu, call_id, padding;
96 _enter("%d", conn->debug_id);
98 chan = &conn->channels[channel];
100 /* If the last call got moved on whilst we were waiting to run, just
101 * ignore this packet.
103 call_id = chan->last_call;
104 if (skb && call_id != sp->hdr.callNumber)
107 msg.msg_name = &conn->peer->srx.transport;
108 msg.msg_namelen = conn->peer->srx.transport_len;
109 msg.msg_control = NULL;
110 msg.msg_controllen = 0;
113 iov[0].iov_base = &pkt;
114 iov[0].iov_len = sizeof(pkt.whdr);
115 iov[1].iov_base = &padding;
117 iov[2].iov_base = &ack_info;
118 iov[2].iov_len = sizeof(ack_info);
120 serial = atomic_inc_return(&conn->serial);
122 pkt.whdr.epoch = htonl(conn->proto.epoch);
123 pkt.whdr.cid = htonl(conn->proto.cid | channel);
124 pkt.whdr.callNumber = htonl(call_id);
125 pkt.whdr.serial = htonl(serial);
127 pkt.whdr.type = chan->last_type;
128 pkt.whdr.flags = conn->out_clientflag;
129 pkt.whdr.userStatus = 0;
130 pkt.whdr.securityIndex = conn->security_ix;
132 pkt.whdr.serviceId = htons(conn->service_id);
134 len = sizeof(pkt.whdr);
135 switch (chan->last_type) {
136 case RXRPC_PACKET_TYPE_ABORT:
137 pkt.abort_code = htonl(chan->last_abort);
138 iov[0].iov_len += sizeof(pkt.abort_code);
139 len += sizeof(pkt.abort_code);
143 case RXRPC_PACKET_TYPE_ACK:
144 mtu = conn->peer->if_mtu;
145 mtu -= conn->peer->hdrsize;
146 pkt.ack.bufferSpace = 0;
147 pkt.ack.maxSkew = htons(skb ? skb->priority : 0);
148 pkt.ack.firstPacket = htonl(chan->last_seq + 1);
149 pkt.ack.previousPacket = htonl(chan->last_seq);
150 pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0);
151 pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
153 ack_info.rxMTU = htonl(rxrpc_rx_mtu);
154 ack_info.maxMTU = htonl(mtu);
155 ack_info.rwind = htonl(rxrpc_rx_window_size);
156 ack_info.jumbo_max = htonl(rxrpc_rx_jumbo_max);
157 pkt.whdr.flags |= RXRPC_SLOW_START_OK;
159 iov[0].iov_len += sizeof(pkt.ack);
160 len += sizeof(pkt.ack) + 3 + sizeof(ack_info);
163 trace_rxrpc_tx_ack(chan->call_debug_id, serial,
164 ntohl(pkt.ack.firstPacket),
165 ntohl(pkt.ack.serial),
166 pkt.ack.reason, 0, rxrpc_rx_window_size);
173 ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
174 conn->peer->last_tx_at = ktime_get_seconds();
176 trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
177 rxrpc_tx_point_call_final_resend);
179 trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr,
180 rxrpc_tx_point_call_final_resend);
186 * pass a connection-level abort onto all calls on that connection
188 static void rxrpc_abort_calls(struct rxrpc_connection *conn)
190 struct rxrpc_call *call;
193 _enter("{%d},%x", conn->debug_id, conn->abort_code);
195 for (i = 0; i < RXRPC_MAXCALLS; i++) {
196 call = conn->channels[i].call;
198 rxrpc_set_call_completion(call,
208 * mark a call as being on a now-secured channel
209 * - must be called with BH's disabled.
211 static void rxrpc_call_is_secure(struct rxrpc_call *call)
213 if (call && __rxrpc_call_state(call) == RXRPC_CALL_SERVER_SECURING) {
214 rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST);
215 rxrpc_notify_socket(call);
220 * connection-level Rx packet processor
222 static int rxrpc_process_event(struct rxrpc_connection *conn,
225 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
228 if (conn->state == RXRPC_CONN_ABORTED)
229 return -ECONNABORTED;
231 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
233 switch (sp->hdr.type) {
234 case RXRPC_PACKET_TYPE_CHALLENGE:
235 return conn->security->respond_to_challenge(conn, skb);
237 case RXRPC_PACKET_TYPE_RESPONSE:
238 ret = conn->security->verify_response(conn, skb);
242 ret = conn->security->init_connection_security(
243 conn, conn->key->payload.data[0]);
247 spin_lock(&conn->state_lock);
248 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING)
249 conn->state = RXRPC_CONN_SERVICE;
250 spin_unlock(&conn->state_lock);
252 if (conn->state == RXRPC_CONN_SERVICE) {
253 /* Offload call state flipping to the I/O thread. As
254 * we've already received the packet, put it on the
255 * front of the queue.
257 skb->mark = RXRPC_SKB_MARK_SERVICE_CONN_SECURED;
258 rxrpc_get_skb(skb, rxrpc_skb_get_conn_secured);
259 skb_queue_head(&conn->local->rx_queue, skb);
260 rxrpc_wake_up_io_thread(conn->local);
271 * set up security and issue a challenge
273 static void rxrpc_secure_connection(struct rxrpc_connection *conn)
275 if (conn->security->issue_challenge(conn) < 0)
276 rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM,
281 * Process delayed final ACKs that we haven't subsumed into a subsequent call.
283 void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force)
285 unsigned long j = jiffies, next_j;
286 unsigned int channel;
290 next_j = j + LONG_MAX;
292 for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
293 struct rxrpc_channel *chan = &conn->channels[channel];
294 unsigned long ack_at;
296 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
299 ack_at = chan->final_ack_at;
300 if (time_before(j, ack_at) && !force) {
301 if (time_before(ack_at, next_j)) {
308 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
310 rxrpc_conn_retransmit_call(conn, NULL, channel);
314 if (time_before_eq(next_j, j))
317 rxrpc_reduce_conn_timer(conn, next_j);
321 * connection-level event processor
323 static void rxrpc_do_process_connection(struct rxrpc_connection *conn)
328 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
329 rxrpc_secure_connection(conn);
331 /* go through the conn-level event packets, releasing the ref on this
332 * connection that each one has when we've finished with it */
333 while ((skb = skb_dequeue(&conn->rx_queue))) {
334 rxrpc_see_skb(skb, rxrpc_skb_see_conn_work);
335 ret = rxrpc_process_event(conn, skb);
339 skb_queue_head(&conn->rx_queue, skb);
340 rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work);
343 rxrpc_free_skb(skb, rxrpc_skb_put_conn_work);
349 void rxrpc_process_connection(struct work_struct *work)
351 struct rxrpc_connection *conn =
352 container_of(work, struct rxrpc_connection, processor);
354 rxrpc_see_connection(conn, rxrpc_conn_see_work);
356 if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) {
357 rxrpc_do_process_connection(conn);
358 rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work);
363 * post connection-level events to the connection
364 * - this includes challenges, responses, some aborts and call terminal packet
367 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
370 _enter("%p,%p", conn, skb);
372 rxrpc_get_skb(skb, rxrpc_skb_get_conn_work);
373 skb_queue_tail(&conn->rx_queue, skb);
374 rxrpc_queue_conn(conn, rxrpc_conn_queue_rx_work);
378 * Input a connection-level packet.
380 bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
382 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
384 switch (sp->hdr.type) {
385 case RXRPC_PACKET_TYPE_BUSY:
386 /* Just ignore BUSY packets for now. */
389 case RXRPC_PACKET_TYPE_ABORT:
390 if (rxrpc_is_conn_aborted(conn))
392 rxrpc_input_conn_abort(conn, skb);
393 rxrpc_abort_calls(conn);
396 case RXRPC_PACKET_TYPE_CHALLENGE:
397 case RXRPC_PACKET_TYPE_RESPONSE:
398 if (rxrpc_is_conn_aborted(conn)) {
399 if (conn->completion == RXRPC_CALL_LOCALLY_ABORTED)
400 rxrpc_send_conn_abort(conn);
403 rxrpc_post_packet_to_conn(conn, skb);
413 * Input a connection event.
415 void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb)
419 if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events))
420 rxrpc_abort_calls(conn);
423 case RXRPC_SKB_MARK_SERVICE_CONN_SECURED:
424 if (conn->state != RXRPC_CONN_SERVICE)
427 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
428 rxrpc_call_is_secure(conn->channels[loop].call);
432 /* Process delayed ACKs whose time has come. */
433 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
434 rxrpc_process_delayed_final_acks(conn, false);