conn_event.c 12.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7
/* connection-level event handling
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 */

8 9
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

10 11 12 13 14 15 16 17 18
#include <linux/module.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/errqueue.h>
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <net/ip.h>
#include "ar-internal.h"

19 20 21
/*
 * Retransmit terminal ACK or ABORT of the previous call.
 */
22
static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
23 24
				       struct sk_buff *skb,
				       unsigned int channel)
25
{
26
	struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
27 28
	struct rxrpc_channel *chan;
	struct msghdr msg;
29
	struct kvec iov[3];
30 31 32
	struct {
		struct rxrpc_wire_header whdr;
		union {
33 34
			__be32 abort_code;
			struct rxrpc_ackpacket ack;
35 36
		};
	} __attribute__((packed)) pkt;
37
	struct rxrpc_ackinfo ack_info;
38
	size_t len;
39
	int ret, ioc;
40
	u32 serial, mtu, call_id, padding;
41 42 43

	_enter("%d", conn->debug_id);

44
	chan = &conn->channels[channel];
45 46 47 48 49 50 51

	/* If the last call got moved on whilst we were waiting to run, just
	 * ignore this packet.
	 */
	call_id = READ_ONCE(chan->last_call);
	/* Sync with __rxrpc_disconnect_call() */
	smp_rmb();
52
	if (skb && call_id != sp->hdr.callNumber)
53 54 55 56 57 58 59 60
		return;

	msg.msg_name	= &conn->params.peer->srx.transport;
	msg.msg_namelen	= conn->params.peer->srx.transport_len;
	msg.msg_control	= NULL;
	msg.msg_controllen = 0;
	msg.msg_flags	= 0;

61 62 63 64 65 66 67
	iov[0].iov_base	= &pkt;
	iov[0].iov_len	= sizeof(pkt.whdr);
	iov[1].iov_base	= &padding;
	iov[1].iov_len	= 3;
	iov[2].iov_base	= &ack_info;
	iov[2].iov_len	= sizeof(ack_info);

68
	pkt.whdr.epoch		= htonl(conn->proto.epoch);
69
	pkt.whdr.cid		= htonl(conn->proto.cid | channel);
70
	pkt.whdr.callNumber	= htonl(call_id);
71 72 73 74 75 76
	pkt.whdr.seq		= 0;
	pkt.whdr.type		= chan->last_type;
	pkt.whdr.flags		= conn->out_clientflag;
	pkt.whdr.userStatus	= 0;
	pkt.whdr.securityIndex	= conn->security_ix;
	pkt.whdr._rsvd		= 0;
77
	pkt.whdr.serviceId	= htons(conn->service_id);
78 79 80 81

	len = sizeof(pkt.whdr);
	switch (chan->last_type) {
	case RXRPC_PACKET_TYPE_ABORT:
82 83 84 85
		pkt.abort_code	= htonl(chan->last_abort);
		iov[0].iov_len += sizeof(pkt.abort_code);
		len += sizeof(pkt.abort_code);
		ioc = 1;
86 87 88 89 90 91
		break;

	case RXRPC_PACKET_TYPE_ACK:
		mtu = conn->params.peer->if_mtu;
		mtu -= conn->params.peer->hdrsize;
		pkt.ack.bufferSpace	= 0;
92 93 94 95 96
		pkt.ack.maxSkew		= htons(skb ? skb->priority : 0);
		pkt.ack.firstPacket	= htonl(chan->last_seq + 1);
		pkt.ack.previousPacket	= htonl(chan->last_seq);
		pkt.ack.serial		= htonl(skb ? sp->hdr.serial : 0);
		pkt.ack.reason		= skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
97
		pkt.ack.nAcks		= 0;
98 99 100 101
		ack_info.rxMTU		= htonl(rxrpc_rx_mtu);
		ack_info.maxMTU		= htonl(mtu);
		ack_info.rwind		= htonl(rxrpc_rx_window_size);
		ack_info.jumbo_max	= htonl(rxrpc_rx_jumbo_max);
D
David Howells 已提交
102
		pkt.whdr.flags		|= RXRPC_SLOW_START_OK;
103 104 105 106
		padding			= 0;
		iov[0].iov_len += sizeof(pkt.ack);
		len += sizeof(pkt.ack) + 3 + sizeof(ack_info);
		ioc = 3;
107
		break;
108 109 110

	default:
		return;
111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}

	/* Resync with __rxrpc_disconnect_call() and check that the last call
	 * didn't get advanced whilst we were filling out the packets.
	 */
	smp_rmb();
	if (READ_ONCE(chan->last_call) != call_id)
		return;

	serial = atomic_inc_return(&conn->serial);
	pkt.whdr.serial = htonl(serial);

	switch (chan->last_type) {
	case RXRPC_PACKET_TYPE_ABORT:
125
		_proto("Tx ABORT %%%u { %d } [re]", serial, conn->abort_code);
126 127
		break;
	case RXRPC_PACKET_TYPE_ACK:
D
David Howells 已提交
128
		trace_rxrpc_tx_ack(chan->call_debug_id, serial,
129 130 131
				   ntohl(pkt.ack.firstPacket),
				   ntohl(pkt.ack.serial),
				   pkt.ack.reason, 0);
132 133 134 135
		_proto("Tx ACK %%%u [re]", serial);
		break;
	}

136
	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, ioc, len);
137
	conn->params.peer->last_tx_at = ktime_get_seconds();
138
	if (ret < 0)
D
David Howells 已提交
139 140 141 142 143
		trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
				    rxrpc_tx_point_call_final_resend);
	else
		trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr,
				      rxrpc_tx_point_call_final_resend);
144

145 146 147
	_leave("");
}

148 149 150
/*
 * pass a connection-level abort onto all calls on that connection
 */
151
static void rxrpc_abort_calls(struct rxrpc_connection *conn,
152 153
			      enum rxrpc_call_completion compl,
			      rxrpc_serial_t serial)
154 155
{
	struct rxrpc_call *call;
156
	int i;
157

158
	_enter("{%d},%x", conn->debug_id, conn->abort_code);
159

160
	spin_lock(&conn->channel_lock);
161

162 163 164 165
	for (i = 0; i < RXRPC_MAXCALLS; i++) {
		call = rcu_dereference_protected(
			conn->channels[i].call,
			lockdep_is_held(&conn->channel_lock));
166
		if (call) {
167
			if (compl == RXRPC_CALL_LOCALLY_ABORTED)
168 169
				trace_rxrpc_abort(call->debug_id,
						  "CON", call->cid,
170
						  call->call_id, 0,
171 172
						  conn->abort_code,
						  conn->error);
173 174 175
			else
				trace_rxrpc_rx_abort(call, serial,
						     conn->abort_code);
176
			if (rxrpc_set_call_completion(call, compl,
177 178
						      conn->abort_code,
						      conn->error))
179
				rxrpc_notify_socket(call);
180 181 182
		}
	}

183
	spin_unlock(&conn->channel_lock);
184 185 186 187 188 189 190
	_leave("");
}

/*
 * generate a connection-level abort
 */
static int rxrpc_abort_connection(struct rxrpc_connection *conn,
191
				  int error, u32 abort_code)
192
{
193
	struct rxrpc_wire_header whdr;
194 195 196 197
	struct msghdr msg;
	struct kvec iov[2];
	__be32 word;
	size_t len;
198
	u32 serial;
199 200 201 202 203 204
	int ret;

	_enter("%d,,%u,%u", conn->debug_id, error, abort_code);

	/* generate a connection-level abort */
	spin_lock_bh(&conn->state_lock);
205
	if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
206 207 208 209 210
		spin_unlock_bh(&conn->state_lock);
		_leave(" = 0 [already dead]");
		return 0;
	}

211 212
	conn->error = error;
	conn->abort_code = abort_code;
213 214 215
	conn->state = RXRPC_CONN_LOCALLY_ABORTED;
	spin_unlock_bh(&conn->state_lock);

216 217
	msg.msg_name	= &conn->params.peer->srx.transport;
	msg.msg_namelen	= conn->params.peer->srx.transport_len;
218 219 220 221
	msg.msg_control	= NULL;
	msg.msg_controllen = 0;
	msg.msg_flags	= 0;

222 223
	whdr.epoch	= htonl(conn->proto.epoch);
	whdr.cid	= htonl(conn->proto.cid);
224 225 226 227 228 229 230
	whdr.callNumber	= 0;
	whdr.seq	= 0;
	whdr.type	= RXRPC_PACKET_TYPE_ABORT;
	whdr.flags	= conn->out_clientflag;
	whdr.userStatus	= 0;
	whdr.securityIndex = conn->security_ix;
	whdr._rsvd	= 0;
231
	whdr.serviceId	= htons(conn->service_id);
232

233
	word		= htonl(conn->abort_code);
234

235 236
	iov[0].iov_base	= &whdr;
	iov[0].iov_len	= sizeof(whdr);
237 238 239 240 241
	iov[1].iov_base	= &word;
	iov[1].iov_len	= sizeof(word);

	len = iov[0].iov_len + iov[1].iov_len;

242
	serial = atomic_inc_return(&conn->serial);
243
	rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, serial);
244
	whdr.serial = htonl(serial);
245
	_proto("Tx CONN ABORT %%%u { %d }", serial, conn->abort_code);
246

247
	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
248
	if (ret < 0) {
249
		trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
D
David Howells 已提交
250
				    rxrpc_tx_point_conn_abort);
251 252 253 254
		_debug("sendmsg failed: %d", ret);
		return -EAGAIN;
	}

D
David Howells 已提交
255 256
	trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);

257
	conn->params.peer->last_tx_at = ktime_get_seconds();
258

259 260 261 262 263 264
	_leave(" = 0");
	return 0;
}

/*
 * mark a call as being on a now-secured channel
265
 * - must be called with BH's disabled.
266
 */
R
Roel Kluin 已提交
267
static void rxrpc_call_is_secure(struct rxrpc_call *call)
268 269 270
{
	_enter("%p", call);
	if (call) {
271 272 273 274 275 276
		write_lock_bh(&call->state_lock);
		if (call->state == RXRPC_CALL_SERVER_SECURING) {
			call->state = RXRPC_CALL_SERVER_ACCEPTING;
			rxrpc_notify_socket(call);
		}
		write_unlock_bh(&call->state_lock);
277 278 279 280 281 282 283 284 285 286 287
	}
}

/*
 * connection-level Rx packet processor
 */
static int rxrpc_process_event(struct rxrpc_connection *conn,
			       struct sk_buff *skb,
			       u32 *_abort_code)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
288 289
	__be32 wtmp;
	u32 abort_code;
290 291
	int loop, ret;

292
	if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
293
		_leave(" = -ECONNABORTED [%u]", conn->state);
294
		return -ECONNABORTED;
295
	}
296

297
	_enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
298

299
	switch (sp->hdr.type) {
300 301
	case RXRPC_PACKET_TYPE_DATA:
	case RXRPC_PACKET_TYPE_ACK:
302 303
		rxrpc_conn_retransmit_call(conn, skb,
					   sp->hdr.cid & RXRPC_CHANNELMASK);
304 305
		return 0;

306 307 308 309
	case RXRPC_PACKET_TYPE_BUSY:
		/* Just ignore BUSY packets for now. */
		return 0;

310
	case RXRPC_PACKET_TYPE_ABORT:
311
		if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
312 313 314
				  &wtmp, sizeof(wtmp)) < 0) {
			trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
					      tracepoint_string("bad_abort"));
315
			return -EPROTO;
316
		}
317 318
		abort_code = ntohl(wtmp);
		_proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
319

320 321
		conn->error = -ECONNABORTED;
		conn->abort_code = abort_code;
322
		conn->state = RXRPC_CONN_REMOTELY_ABORTED;
323
		rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED, sp->hdr.serial);
324 325 326
		return -ECONNABORTED;

	case RXRPC_PACKET_TYPE_CHALLENGE:
327 328
		return conn->security->respond_to_challenge(conn, skb,
							    _abort_code);
329 330 331 332 333 334 335 336 337 338

	case RXRPC_PACKET_TYPE_RESPONSE:
		ret = conn->security->verify_response(conn, skb, _abort_code);
		if (ret < 0)
			return ret;

		ret = conn->security->init_connection_security(conn);
		if (ret < 0)
			return ret;

339 340 341 342
		ret = conn->security->prime_packet_security(conn);
		if (ret < 0)
			return ret;

343
		spin_lock(&conn->channel_lock);
344 345
		spin_lock(&conn->state_lock);

346 347
		if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
			conn->state = RXRPC_CONN_SERVICE;
348
			spin_unlock(&conn->state_lock);
349
			for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
350 351
				rxrpc_call_is_secure(
					rcu_dereference_protected(
352 353
						conn->channels[loop].call,
						lockdep_is_held(&conn->channel_lock)));
354 355
		} else {
			spin_unlock(&conn->state_lock);
356 357
		}

358
		spin_unlock(&conn->channel_lock);
359 360 361
		return 0;

	default:
362 363
		trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
				      tracepoint_string("bad_conn_pkt"));
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		return -EPROTO;
	}
}

/*
 * set up security and issue a challenge
 */
static void rxrpc_secure_connection(struct rxrpc_connection *conn)
{
	u32 abort_code;
	int ret;

	_enter("{%d}", conn->debug_id);

	ASSERT(conn->security_ix != 0);
379
	ASSERT(conn->server_key);
380 381 382 383 384 385 386 387 388 389 390 391

	if (conn->security->issue_challenge(conn) < 0) {
		abort_code = RX_CALL_DEAD;
		ret = -ENOMEM;
		goto abort;
	}

	_leave("");
	return;

abort:
	_debug("abort %d, %d", ret, abort_code);
392
	rxrpc_abort_connection(conn, ret, abort_code);
393 394 395
	_leave(" [aborted]");
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/*
 * Process delayed final ACKs that we haven't subsumed into a subsequent call.
 */
static void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn)
{
	unsigned long j = jiffies, next_j;
	unsigned int channel;
	bool set;

again:
	next_j = j + LONG_MAX;
	set = false;
	for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
		struct rxrpc_channel *chan = &conn->channels[channel];
		unsigned long ack_at;

		if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
			continue;

		smp_rmb(); /* vs rxrpc_disconnect_client_call */
		ack_at = READ_ONCE(chan->final_ack_at);

		if (time_before(j, ack_at)) {
			if (time_before(ack_at, next_j)) {
				next_j = ack_at;
				set = true;
			}
			continue;
		}

		if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
				       &conn->flags))
			rxrpc_conn_retransmit_call(conn, NULL, channel);
	}

	j = jiffies;
	if (time_before_eq(next_j, j))
		goto again;
	if (set)
		rxrpc_reduce_conn_timer(conn, next_j);
}

438 439 440 441 442 443 444 445 446 447 448
/*
 * connection-level event processor
 */
void rxrpc_process_connection(struct work_struct *work)
{
	struct rxrpc_connection *conn =
		container_of(work, struct rxrpc_connection, processor);
	struct sk_buff *skb;
	u32 abort_code = RX_PROTOCOL_ERROR;
	int ret;

449
	rxrpc_see_connection(conn);
450

451
	if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
452 453
		rxrpc_secure_connection(conn);

454 455 456 457
	/* Process delayed ACKs whose time has come. */
	if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
		rxrpc_process_delayed_final_acks(conn);

458 459 460
	/* go through the conn-level event packets, releasing the ref on this
	 * connection that each one has when we've finished with it */
	while ((skb = skb_dequeue(&conn->rx_queue))) {
461
		rxrpc_see_skb(skb, rxrpc_skb_seen);
462 463 464 465 466 467
		ret = rxrpc_process_event(conn, skb, &abort_code);
		switch (ret) {
		case -EPROTO:
		case -EKEYEXPIRED:
		case -EKEYREJECTED:
			goto protocol_error;
468
		case -ENOMEM:
469 470 471 472
		case -EAGAIN:
			goto requeue_and_leave;
		case -ECONNABORTED:
		default:
473
			rxrpc_free_skb(skb, rxrpc_skb_freed);
474 475 476 477 478 479 480 481 482 483 484 485 486 487
			break;
		}
	}

out:
	rxrpc_put_connection(conn);
	_leave("");
	return;

requeue_and_leave:
	skb_queue_head(&conn->rx_queue, skb);
	goto out;

protocol_error:
488
	if (rxrpc_abort_connection(conn, ret, abort_code) < 0)
489
		goto requeue_and_leave;
490
	rxrpc_free_skb(skb, rxrpc_skb_freed);
491 492
	goto out;
}