xref: /linux/net/rxrpc/conn_event.c (revision a35d00d5512accd337510fa4de756b743d331a87)
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_irq(&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_irq(&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 void rxrpc_input_conn_abort(struct rxrpc_connection *conn,
67 				   struct sk_buff *skb)
68 {
69 	trace_rxrpc_rx_conn_abort(conn, skb);
70 	rxrpc_set_conn_aborted(conn, skb, skb->priority, -ECONNABORTED,
71 			       RXRPC_CALL_REMOTELY_ABORTED);
72 }
73 
74 /*
75  * Retransmit terminal ACK or ABORT of the previous call.
76  */
77 void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
78 				struct sk_buff *skb,
79 				unsigned int channel)
80 {
81 	struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
82 	struct rxrpc_channel *chan;
83 	struct msghdr msg;
84 	struct kvec iov[3];
85 	struct {
86 		struct rxrpc_wire_header whdr;
87 		union {
88 			__be32 abort_code;
89 			struct rxrpc_ackpacket ack;
90 		};
91 	} __attribute__((packed)) pkt;
92 	struct rxrpc_acktrailer trailer;
93 	size_t len;
94 	int ret, ioc;
95 	u32 serial, max_mtu, if_mtu, call_id, padding;
96 
97 	_enter("%d", conn->debug_id);
98 
99 	if (sp && sp->hdr.type == RXRPC_PACKET_TYPE_ACK) {
100 		if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
101 				  &pkt.ack, sizeof(pkt.ack)) < 0)
102 			return;
103 		if (pkt.ack.reason == RXRPC_ACK_PING_RESPONSE)
104 			return;
105 	}
106 
107 	chan = &conn->channels[channel];
108 
109 	/* If the last call got moved on whilst we were waiting to run, just
110 	 * ignore this packet.
111 	 */
112 	call_id = chan->last_call;
113 	if (skb && call_id != sp->hdr.callNumber)
114 		return;
115 
116 	msg.msg_name	= &conn->peer->srx.transport;
117 	msg.msg_namelen	= conn->peer->srx.transport_len;
118 	msg.msg_control	= NULL;
119 	msg.msg_controllen = 0;
120 	msg.msg_flags	= 0;
121 
122 	iov[0].iov_base	= &pkt;
123 	iov[0].iov_len	= sizeof(pkt.whdr);
124 	iov[1].iov_base	= &padding;
125 	iov[1].iov_len	= 3;
126 	iov[2].iov_base	= &trailer;
127 	iov[2].iov_len	= sizeof(trailer);
128 
129 	serial = rxrpc_get_next_serial(conn);
130 
131 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
132 	pkt.whdr.cid		= htonl(conn->proto.cid | channel);
133 	pkt.whdr.callNumber	= htonl(call_id);
134 	pkt.whdr.serial		= htonl(serial);
135 	pkt.whdr.seq		= 0;
136 	pkt.whdr.type		= chan->last_type;
137 	pkt.whdr.flags		= conn->out_clientflag;
138 	pkt.whdr.userStatus	= 0;
139 	pkt.whdr.securityIndex	= conn->security_ix;
140 	pkt.whdr._rsvd		= 0;
141 	pkt.whdr.serviceId	= htons(conn->service_id);
142 
143 	len = sizeof(pkt.whdr);
144 	switch (chan->last_type) {
145 	case RXRPC_PACKET_TYPE_ABORT:
146 		pkt.abort_code	= htonl(chan->last_abort);
147 		iov[0].iov_len += sizeof(pkt.abort_code);
148 		len += sizeof(pkt.abort_code);
149 		ioc = 1;
150 		break;
151 
152 	case RXRPC_PACKET_TYPE_ACK:
153 		if_mtu = conn->peer->if_mtu - conn->peer->hdrsize;
154 		if (conn->peer->ackr_adv_pmtud) {
155 			max_mtu = umax(conn->peer->max_data, rxrpc_rx_mtu);
156 		} else {
157 			if_mtu = umin(1444, if_mtu);
158 			max_mtu = if_mtu;
159 		}
160 		pkt.ack.bufferSpace	= 0;
161 		pkt.ack.maxSkew		= htons(skb ? skb->priority : 0);
162 		pkt.ack.firstPacket	= htonl(chan->last_seq + 1);
163 		pkt.ack.previousPacket	= htonl(chan->last_seq);
164 		pkt.ack.serial		= htonl(skb ? sp->hdr.serial : 0);
165 		pkt.ack.reason		= skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
166 		pkt.ack.nAcks		= 0;
167 		trailer.maxMTU		= htonl(max_mtu);
168 		trailer.ifMTU		= htonl(if_mtu);
169 		trailer.rwind		= htonl(rxrpc_rx_window_size);
170 		trailer.jumbo_max	= 0;
171 		pkt.whdr.flags		|= RXRPC_SLOW_START_OK;
172 		padding			= 0;
173 		iov[0].iov_len += sizeof(pkt.ack);
174 		len += sizeof(pkt.ack) + 3 + sizeof(trailer);
175 		ioc = 3;
176 
177 		trace_rxrpc_tx_ack(chan->call_debug_id, serial,
178 				   ntohl(pkt.ack.firstPacket),
179 				   ntohl(pkt.ack.serial),
180 				   pkt.ack.reason, 0, rxrpc_rx_window_size,
181 				   rxrpc_propose_ack_retransmit);
182 		break;
183 
184 	default:
185 		return;
186 	}
187 
188 	ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
189 	conn->peer->last_tx_at = ktime_get_seconds();
190 	if (ret < 0)
191 		trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
192 				    rxrpc_tx_point_call_final_resend);
193 	else
194 		trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr,
195 				      rxrpc_tx_point_call_final_resend);
196 
197 	_leave("");
198 }
199 
200 /*
201  * pass a connection-level abort onto all calls on that connection
202  */
203 static void rxrpc_abort_calls(struct rxrpc_connection *conn)
204 {
205 	struct rxrpc_call *call;
206 	int i;
207 
208 	_enter("{%d},%x", conn->debug_id, conn->abort_code);
209 
210 	for (i = 0; i < RXRPC_MAXCALLS; i++) {
211 		call = conn->channels[i].call;
212 		if (call) {
213 			rxrpc_see_call(call, rxrpc_call_see_conn_abort);
214 			rxrpc_set_call_completion(call,
215 						  conn->completion,
216 						  conn->abort_code,
217 						  conn->error);
218 			rxrpc_poke_call(call, rxrpc_call_poke_conn_abort);
219 		}
220 	}
221 
222 	_leave("");
223 }
224 
225 /*
226  * mark a call as being on a now-secured channel
227  * - must be called with BH's disabled.
228  */
229 static void rxrpc_call_is_secure(struct rxrpc_call *call)
230 {
231 	if (call && __rxrpc_call_state(call) == RXRPC_CALL_SERVER_SECURING) {
232 		rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST);
233 		rxrpc_notify_socket(call);
234 	}
235 }
236 
237 /*
238  * connection-level Rx packet processor
239  */
240 static int rxrpc_process_event(struct rxrpc_connection *conn,
241 			       struct sk_buff *skb)
242 {
243 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
244 	int ret;
245 
246 	if (conn->state == RXRPC_CONN_ABORTED)
247 		return -ECONNABORTED;
248 
249 	_enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
250 
251 	switch (sp->hdr.type) {
252 	case RXRPC_PACKET_TYPE_CHALLENGE:
253 		return conn->security->respond_to_challenge(conn, skb);
254 
255 	case RXRPC_PACKET_TYPE_RESPONSE:
256 		ret = conn->security->verify_response(conn, skb);
257 		if (ret < 0)
258 			return ret;
259 
260 		ret = conn->security->init_connection_security(
261 			conn, conn->key->payload.data[0]);
262 		if (ret < 0)
263 			return ret;
264 
265 		spin_lock_irq(&conn->state_lock);
266 		if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING)
267 			conn->state = RXRPC_CONN_SERVICE;
268 		spin_unlock_irq(&conn->state_lock);
269 
270 		if (conn->state == RXRPC_CONN_SERVICE) {
271 			/* Offload call state flipping to the I/O thread.  As
272 			 * we've already received the packet, put it on the
273 			 * front of the queue.
274 			 */
275 			skb->mark = RXRPC_SKB_MARK_SERVICE_CONN_SECURED;
276 			rxrpc_get_skb(skb, rxrpc_skb_get_conn_secured);
277 			skb_queue_head(&conn->local->rx_queue, skb);
278 			rxrpc_wake_up_io_thread(conn->local);
279 		}
280 		return 0;
281 
282 	default:
283 		WARN_ON_ONCE(1);
284 		return -EPROTO;
285 	}
286 }
287 
288 /*
289  * set up security and issue a challenge
290  */
291 static void rxrpc_secure_connection(struct rxrpc_connection *conn)
292 {
293 	if (conn->security->issue_challenge(conn) < 0)
294 		rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM,
295 				 rxrpc_abort_nomem);
296 }
297 
298 /*
299  * Process delayed final ACKs that we haven't subsumed into a subsequent call.
300  */
301 void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force)
302 {
303 	unsigned long j = jiffies, next_j;
304 	unsigned int channel;
305 	bool set;
306 
307 again:
308 	next_j = j + LONG_MAX;
309 	set = false;
310 	for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
311 		struct rxrpc_channel *chan = &conn->channels[channel];
312 		unsigned long ack_at;
313 
314 		if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
315 			continue;
316 
317 		ack_at = chan->final_ack_at;
318 		if (time_before(j, ack_at) && !force) {
319 			if (time_before(ack_at, next_j)) {
320 				next_j = ack_at;
321 				set = true;
322 			}
323 			continue;
324 		}
325 
326 		if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
327 				       &conn->flags))
328 			rxrpc_conn_retransmit_call(conn, NULL, channel);
329 	}
330 
331 	j = jiffies;
332 	if (time_before_eq(next_j, j))
333 		goto again;
334 	if (set)
335 		rxrpc_reduce_conn_timer(conn, next_j);
336 }
337 
338 /*
339  * connection-level event processor
340  */
341 static void rxrpc_do_process_connection(struct rxrpc_connection *conn)
342 {
343 	struct sk_buff *skb;
344 	int ret;
345 
346 	if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
347 		rxrpc_secure_connection(conn);
348 
349 	/* go through the conn-level event packets, releasing the ref on this
350 	 * connection that each one has when we've finished with it */
351 	while ((skb = skb_dequeue(&conn->rx_queue))) {
352 		rxrpc_see_skb(skb, rxrpc_skb_see_conn_work);
353 		ret = rxrpc_process_event(conn, skb);
354 		switch (ret) {
355 		case -ENOMEM:
356 		case -EAGAIN:
357 			skb_queue_head(&conn->rx_queue, skb);
358 			rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work);
359 			break;
360 		default:
361 			rxrpc_free_skb(skb, rxrpc_skb_put_conn_work);
362 			break;
363 		}
364 	}
365 }
366 
367 void rxrpc_process_connection(struct work_struct *work)
368 {
369 	struct rxrpc_connection *conn =
370 		container_of(work, struct rxrpc_connection, processor);
371 
372 	rxrpc_see_connection(conn, rxrpc_conn_see_work);
373 
374 	if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) {
375 		rxrpc_do_process_connection(conn);
376 		rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work);
377 	}
378 }
379 
380 /*
381  * post connection-level events to the connection
382  * - this includes challenges, responses, some aborts and call terminal packet
383  *   retransmission.
384  */
385 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
386 				      struct sk_buff *skb)
387 {
388 	_enter("%p,%p", conn, skb);
389 
390 	rxrpc_get_skb(skb, rxrpc_skb_get_conn_work);
391 	skb_queue_tail(&conn->rx_queue, skb);
392 	rxrpc_queue_conn(conn, rxrpc_conn_queue_rx_work);
393 }
394 
395 /*
396  * Input a connection-level packet.
397  */
398 bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
399 {
400 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
401 
402 	switch (sp->hdr.type) {
403 	case RXRPC_PACKET_TYPE_BUSY:
404 		/* Just ignore BUSY packets for now. */
405 		return true;
406 
407 	case RXRPC_PACKET_TYPE_ABORT:
408 		if (rxrpc_is_conn_aborted(conn))
409 			return true;
410 		rxrpc_input_conn_abort(conn, skb);
411 		rxrpc_abort_calls(conn);
412 		return true;
413 
414 	case RXRPC_PACKET_TYPE_CHALLENGE:
415 	case RXRPC_PACKET_TYPE_RESPONSE:
416 		if (rxrpc_is_conn_aborted(conn)) {
417 			if (conn->completion == RXRPC_CALL_LOCALLY_ABORTED)
418 				rxrpc_send_conn_abort(conn);
419 			return true;
420 		}
421 		rxrpc_post_packet_to_conn(conn, skb);
422 		return true;
423 
424 	default:
425 		WARN_ON_ONCE(1);
426 		return true;
427 	}
428 }
429 
430 /*
431  * Input a connection event.
432  */
433 void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb)
434 {
435 	unsigned int loop;
436 
437 	if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events))
438 		rxrpc_abort_calls(conn);
439 
440 	switch (skb->mark) {
441 	case RXRPC_SKB_MARK_SERVICE_CONN_SECURED:
442 		if (conn->state != RXRPC_CONN_SERVICE)
443 			break;
444 
445 		for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
446 			rxrpc_call_is_secure(conn->channels[loop].call);
447 		break;
448 	}
449 
450 	/* Process delayed ACKs whose time has come. */
451 	if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
452 		rxrpc_process_delayed_final_acks(conn, false);
453 }
454