xref: /openbmc/linux/net/rxrpc/conn_event.c (revision 93696d8f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* connection-level event handling
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/errqueue.h>
14 #include <net/sock.h>
15 #include <net/af_rxrpc.h>
16 #include <net/ip.h>
17 #include "ar-internal.h"
18 
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,
50 		     s32 abort_code, int err, enum rxrpc_abort_reason why)
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 
73 /*
74  * Retransmit terminal ACK or ABORT of the previous call.
75  */
76 void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
77 				struct sk_buff *skb,
78 				unsigned int channel)
79 {
80 	struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
81 	struct rxrpc_channel *chan;
82 	struct msghdr msg;
83 	struct kvec iov[3];
84 	struct {
85 		struct rxrpc_wire_header whdr;
86 		union {
87 			__be32 abort_code;
88 			struct rxrpc_ackpacket ack;
89 		};
90 	} __attribute__((packed)) pkt;
91 	struct rxrpc_acktrailer trailer;
92 	size_t len;
93 	int ret, ioc;
94 	u32 serial, mtu, call_id, padding;
95 
96 	_enter("%d", conn->debug_id);
97 
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 
106 	chan = &conn->channels[channel];
107 
108 	/* If the last call got moved on whilst we were waiting to run, just
109 	 * ignore this packet.
110 	 */
111 	call_id = chan->last_call;
112 	if (skb && call_id != sp->hdr.callNumber)
113 		return;
114 
115 	msg.msg_name	= &conn->peer->srx.transport;
116 	msg.msg_namelen	= conn->peer->srx.transport_len;
117 	msg.msg_control	= NULL;
118 	msg.msg_controllen = 0;
119 	msg.msg_flags	= 0;
120 
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;
125 	iov[2].iov_base	= &trailer;
126 	iov[2].iov_len	= sizeof(trailer);
127 
128 	serial = rxrpc_get_next_serial(conn);
129 
130 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
131 	pkt.whdr.cid		= htonl(conn->proto.cid | channel);
132 	pkt.whdr.callNumber	= htonl(call_id);
133 	pkt.whdr.serial		= htonl(serial);
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;
140 	pkt.whdr.serviceId	= htons(conn->service_id);
141 
142 	len = sizeof(pkt.whdr);
143 	switch (chan->last_type) {
144 	case RXRPC_PACKET_TYPE_ABORT:
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;
149 		break;
150 
151 	case RXRPC_PACKET_TYPE_ACK:
152 		mtu = conn->peer->if_mtu;
153 		mtu -= conn->peer->hdrsize;
154 		pkt.ack.bufferSpace	= 0;
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;
160 		pkt.ack.nAcks		= 0;
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);
165 		pkt.whdr.flags		|= RXRPC_SLOW_START_OK;
166 		padding			= 0;
167 		iov[0].iov_len += sizeof(pkt.ack);
168 		len += sizeof(pkt.ack) + 3 + sizeof(trailer);
169 		ioc = 3;
170 
171 		trace_rxrpc_tx_ack(chan->call_debug_id, serial,
172 				   ntohl(pkt.ack.firstPacket),
173 				   ntohl(pkt.ack.serial),
174 				   pkt.ack.reason, 0, rxrpc_rx_window_size);
175 		break;
176 
177 	default:
178 		return;
179 	}
180 
181 	ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
182 	conn->peer->last_tx_at = ktime_get_seconds();
183 	if (ret < 0)
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);
189 
190 	_leave("");
191 }
192 
193 /*
194  * pass a connection-level abort onto all calls on that connection
195  */
196 static void rxrpc_abort_calls(struct rxrpc_connection *conn)
197 {
198 	struct rxrpc_call *call;
199 	int i;
200 
201 	_enter("{%d},%x", conn->debug_id, conn->abort_code);
202 
203 	for (i = 0; i < RXRPC_MAXCALLS; i++) {
204 		call = conn->channels[i].call;
205 		if (call)
206 			rxrpc_set_call_completion(call,
207 						  conn->completion,
208 						  conn->abort_code,
209 						  conn->error);
210 	}
211 
212 	_leave("");
213 }
214 
215 /*
216  * mark a call as being on a now-secured channel
217  * - must be called with BH's disabled.
218  */
219 static void rxrpc_call_is_secure(struct rxrpc_call *call)
220 {
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);
224 	}
225 }
226 
227 /*
228  * connection-level Rx packet processor
229  */
230 static int rxrpc_process_event(struct rxrpc_connection *conn,
231 			       struct sk_buff *skb)
232 {
233 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
234 	int ret;
235 
236 	if (conn->state == RXRPC_CONN_ABORTED)
237 		return -ECONNABORTED;
238 
239 	_enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
240 
241 	switch (sp->hdr.type) {
242 	case RXRPC_PACKET_TYPE_CHALLENGE:
243 		return conn->security->respond_to_challenge(conn, skb);
244 
245 	case RXRPC_PACKET_TYPE_RESPONSE:
246 		ret = conn->security->verify_response(conn, skb);
247 		if (ret < 0)
248 			return ret;
249 
250 		ret = conn->security->init_connection_security(
251 			conn, conn->key->payload.data[0]);
252 		if (ret < 0)
253 			return ret;
254 
255 		spin_lock(&conn->state_lock);
256 		if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING)
257 			conn->state = RXRPC_CONN_SERVICE;
258 		spin_unlock(&conn->state_lock);
259 
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 		}
270 		return 0;
271 
272 	default:
273 		WARN_ON_ONCE(1);
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 {
283 	if (conn->security->issue_challenge(conn) < 0)
284 		rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM,
285 				 rxrpc_abort_nomem);
286 }
287 
288 /*
289  * Process delayed final ACKs that we haven't subsumed into a subsequent call.
290  */
291 void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force)
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 
307 		ack_at = chan->final_ack_at;
308 		if (time_before(j, ack_at) && !force) {
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 
328 /*
329  * connection-level event processor
330  */
331 static void rxrpc_do_process_connection(struct rxrpc_connection *conn)
332 {
333 	struct sk_buff *skb;
334 	int ret;
335 
336 	if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
337 		rxrpc_secure_connection(conn);
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))) {
342 		rxrpc_see_skb(skb, rxrpc_skb_see_conn_work);
343 		ret = rxrpc_process_event(conn, skb);
344 		switch (ret) {
345 		case -ENOMEM:
346 		case -EAGAIN:
347 			skb_queue_head(&conn->rx_queue, skb);
348 			rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work);
349 			break;
350 		default:
351 			rxrpc_free_skb(skb, rxrpc_skb_put_conn_work);
352 			break;
353 		}
354 	}
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 
362 	rxrpc_see_connection(conn, rxrpc_conn_see_work);
363 
364 	if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) {
365 		rxrpc_do_process_connection(conn);
366 		rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work);
367 	}
368 }
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  */
388 bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
389 {
390 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
391 
392 	switch (sp->hdr.type) {
393 	case RXRPC_PACKET_TYPE_BUSY:
394 		/* Just ignore BUSY packets for now. */
395 		return true;
396 
397 	case RXRPC_PACKET_TYPE_ABORT:
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;
403 
404 	case RXRPC_PACKET_TYPE_CHALLENGE:
405 	case RXRPC_PACKET_TYPE_RESPONSE:
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 		}
411 		rxrpc_post_packet_to_conn(conn, skb);
412 		return true;
413 
414 	default:
415 		WARN_ON_ONCE(1);
416 		return true;
417 	}
418 }
419 
420 /*
421  * Input a connection event.
422  */
423 void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb)
424 {
425 	unsigned int loop;
426 
427 	if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events))
428 		rxrpc_abort_calls(conn);
429 
430 	switch (skb->mark) {
431 	case RXRPC_SKB_MARK_SERVICE_CONN_SECURED:
432 		if (conn->state != RXRPC_CONN_SERVICE)
433 			break;
434 
435 		for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
436 			rxrpc_call_is_secure(conn->channels[loop].call);
437 		break;
438 	}
439 
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 }
444