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 = READ_ONCE(chan->last_call);
104 /* Sync with __rxrpc_disconnect_call() */
106 if (skb && call_id != sp->hdr.callNumber)
109 msg.msg_name = &conn->peer->srx.transport;
110 msg.msg_namelen = conn->peer->srx.transport_len;
111 msg.msg_control = NULL;
112 msg.msg_controllen = 0;
115 iov[0].iov_base = &pkt;
116 iov[0].iov_len = sizeof(pkt.whdr);
117 iov[1].iov_base = &padding;
119 iov[2].iov_base = &ack_info;
120 iov[2].iov_len = sizeof(ack_info);
122 pkt.whdr.epoch = htonl(conn->proto.epoch);
123 pkt.whdr.cid = htonl(conn->proto.cid | channel);
124 pkt.whdr.callNumber = htonl(call_id);
126 pkt.whdr.type = chan->last_type;
127 pkt.whdr.flags = conn->out_clientflag;
128 pkt.whdr.userStatus = 0;
129 pkt.whdr.securityIndex = conn->security_ix;
131 pkt.whdr.serviceId = htons(conn->service_id);
133 len = sizeof(pkt.whdr);
134 switch (chan->last_type) {
135 case RXRPC_PACKET_TYPE_ABORT:
136 pkt.abort_code = htonl(chan->last_abort);
137 iov[0].iov_len += sizeof(pkt.abort_code);
138 len += sizeof(pkt.abort_code);
142 case RXRPC_PACKET_TYPE_ACK:
143 mtu = conn->peer->if_mtu;
144 mtu -= conn->peer->hdrsize;
145 pkt.ack.bufferSpace = 0;
146 pkt.ack.maxSkew = htons(skb ? skb->priority : 0);
147 pkt.ack.firstPacket = htonl(chan->last_seq + 1);
148 pkt.ack.previousPacket = htonl(chan->last_seq);
149 pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0);
150 pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
152 ack_info.rxMTU = htonl(rxrpc_rx_mtu);
153 ack_info.maxMTU = htonl(mtu);
154 ack_info.rwind = htonl(rxrpc_rx_window_size);
155 ack_info.jumbo_max = htonl(rxrpc_rx_jumbo_max);
156 pkt.whdr.flags |= RXRPC_SLOW_START_OK;
158 iov[0].iov_len += sizeof(pkt.ack);
159 len += sizeof(pkt.ack) + 3 + sizeof(ack_info);
167 /* Resync with __rxrpc_disconnect_call() and check that the last call
168 * didn't get advanced whilst we were filling out the packets.
171 if (READ_ONCE(chan->last_call) != call_id)
174 serial = atomic_inc_return(&conn->serial);
175 pkt.whdr.serial = htonl(serial);
177 switch (chan->last_type) {
178 case RXRPC_PACKET_TYPE_ABORT:
180 case RXRPC_PACKET_TYPE_ACK:
181 trace_rxrpc_tx_ack(chan->call_debug_id, serial,
182 ntohl(pkt.ack.firstPacket),
183 ntohl(pkt.ack.serial),
188 ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
189 conn->peer->last_tx_at = ktime_get_seconds();
191 trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
192 rxrpc_tx_point_call_final_resend);
194 trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr,
195 rxrpc_tx_point_call_final_resend);
201 * pass a connection-level abort onto all calls on that connection
203 static void rxrpc_abort_calls(struct rxrpc_connection *conn)
205 struct rxrpc_call *call;
208 _enter("{%d},%x", conn->debug_id, conn->abort_code);
210 spin_lock(&conn->bundle->channel_lock);
212 for (i = 0; i < RXRPC_MAXCALLS; i++) {
213 call = rcu_dereference_protected(
214 conn->channels[i].call,
215 lockdep_is_held(&conn->bundle->channel_lock));
217 rxrpc_set_call_completion(call,
223 spin_unlock(&conn->bundle->channel_lock);
228 * mark a call as being on a now-secured channel
229 * - must be called with BH's disabled.
231 static void rxrpc_call_is_secure(struct rxrpc_call *call)
235 write_lock(&call->state_lock);
236 if (call->state == RXRPC_CALL_SERVER_SECURING) {
237 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
238 rxrpc_notify_socket(call);
240 write_unlock(&call->state_lock);
245 * connection-level Rx packet processor
247 static int rxrpc_process_event(struct rxrpc_connection *conn,
250 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
253 if (conn->state == RXRPC_CONN_ABORTED)
254 return -ECONNABORTED;
256 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
258 switch (sp->hdr.type) {
259 case RXRPC_PACKET_TYPE_CHALLENGE:
260 return conn->security->respond_to_challenge(conn, skb);
262 case RXRPC_PACKET_TYPE_RESPONSE:
263 ret = conn->security->verify_response(conn, skb);
267 ret = conn->security->init_connection_security(
268 conn, conn->key->payload.data[0]);
272 spin_lock(&conn->bundle->channel_lock);
273 spin_lock(&conn->state_lock);
275 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
276 conn->state = RXRPC_CONN_SERVICE;
277 spin_unlock(&conn->state_lock);
278 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
279 rxrpc_call_is_secure(
280 rcu_dereference_protected(
281 conn->channels[loop].call,
282 lockdep_is_held(&conn->bundle->channel_lock)));
284 spin_unlock(&conn->state_lock);
287 spin_unlock(&conn->bundle->channel_lock);
297 * set up security and issue a challenge
299 static void rxrpc_secure_connection(struct rxrpc_connection *conn)
301 if (conn->security->issue_challenge(conn) < 0)
302 rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM,
307 * Process delayed final ACKs that we haven't subsumed into a subsequent call.
309 void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force)
311 unsigned long j = jiffies, next_j;
312 unsigned int channel;
316 next_j = j + LONG_MAX;
318 for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
319 struct rxrpc_channel *chan = &conn->channels[channel];
320 unsigned long ack_at;
322 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
325 smp_rmb(); /* vs rxrpc_disconnect_client_call */
326 ack_at = READ_ONCE(chan->final_ack_at);
328 if (time_before(j, ack_at) && !force) {
329 if (time_before(ack_at, next_j)) {
336 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
338 rxrpc_conn_retransmit_call(conn, NULL, channel);
342 if (time_before_eq(next_j, j))
345 rxrpc_reduce_conn_timer(conn, next_j);
349 * connection-level event processor
351 static void rxrpc_do_process_connection(struct rxrpc_connection *conn)
356 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
357 rxrpc_secure_connection(conn);
359 /* go through the conn-level event packets, releasing the ref on this
360 * connection that each one has when we've finished with it */
361 while ((skb = skb_dequeue(&conn->rx_queue))) {
362 rxrpc_see_skb(skb, rxrpc_skb_see_conn_work);
363 ret = rxrpc_process_event(conn, skb);
367 skb_queue_head(&conn->rx_queue, skb);
368 rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work);
371 rxrpc_free_skb(skb, rxrpc_skb_put_conn_work);
377 void rxrpc_process_connection(struct work_struct *work)
379 struct rxrpc_connection *conn =
380 container_of(work, struct rxrpc_connection, processor);
382 rxrpc_see_connection(conn, rxrpc_conn_see_work);
384 if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) {
385 rxrpc_do_process_connection(conn);
386 rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work);
391 * post connection-level events to the connection
392 * - this includes challenges, responses, some aborts and call terminal packet
395 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
398 _enter("%p,%p", conn, skb);
400 rxrpc_get_skb(skb, rxrpc_skb_get_conn_work);
401 skb_queue_tail(&conn->rx_queue, skb);
402 rxrpc_queue_conn(conn, rxrpc_conn_queue_rx_work);
406 * Input a connection-level packet.
408 bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
410 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
412 switch (sp->hdr.type) {
413 case RXRPC_PACKET_TYPE_BUSY:
414 /* Just ignore BUSY packets for now. */
417 case RXRPC_PACKET_TYPE_ABORT:
418 if (rxrpc_is_conn_aborted(conn))
420 rxrpc_input_conn_abort(conn, skb);
421 rxrpc_abort_calls(conn);
424 case RXRPC_PACKET_TYPE_CHALLENGE:
425 case RXRPC_PACKET_TYPE_RESPONSE:
426 if (rxrpc_is_conn_aborted(conn)) {
427 if (conn->completion == RXRPC_CALL_LOCALLY_ABORTED)
428 rxrpc_send_conn_abort(conn);
431 rxrpc_post_packet_to_conn(conn, skb);
441 * Input a connection event.
443 void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb)
445 if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events))
446 rxrpc_abort_calls(conn);
448 /* Process delayed ACKs whose time has come. */
449 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
450 rxrpc_process_delayed_final_acks(conn, false);