input.c 23.3 KB
Newer Older
1 2
/* RxRPC packet reception
 *
3
 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4 5 6 7 8 9 10 11
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14 15 16 17 18 19 20 21
#include <linux/module.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/errqueue.h>
#include <linux/udp.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/icmp.h>
22
#include <linux/gfp.h>
23 24 25
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <net/ip.h>
26
#include <net/udp.h>
27
#include <net/net_namespace.h>
28 29
#include "ar-internal.h"

30 31 32 33 34 35 36 37 38
static void rxrpc_proto_abort(const char *why,
			      struct rxrpc_call *call, rxrpc_seq_t seq)
{
	if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, EBADMSG)) {
		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
		rxrpc_queue_call(call);
	}
}

39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * Ping the other end to fill our RTT cache and to retrieve the rwind
 * and MTU parameters.
 */
static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
			    int skew)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);

	rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
			  true, true);
}

52
/*
53
 * Apply a hard ACK by advancing the Tx window.
54
 */
55
static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
56
{
57 58
	struct sk_buff *skb, *list = NULL;
	int ix;
59

60
	spin_lock(&call->lock);
61

62 63 64 65
	while (before(call->tx_hard_ack, to)) {
		call->tx_hard_ack++;
		ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
		skb = call->rxtx_buffer[ix];
D
David Howells 已提交
66
		rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
67 68 69 70 71
		call->rxtx_buffer[ix] = NULL;
		call->rxtx_annotations[ix] = 0;
		skb->next = list;
		list = skb;
	}
72

73
	spin_unlock(&call->lock);
74

75
	trace_rxrpc_transmit(call, rxrpc_transmit_rotate);
76 77
	wake_up(&call->waitq);

78 79 80 81
	while (list) {
		skb = list;
		list = skb->next;
		skb->next = NULL;
D
David Howells 已提交
82
		rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
83
	}
84
}
85

86 87 88 89 90 91 92 93 94
/*
 * End the transmission phase of a call.
 *
 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
 * or a final ACK packet.
 */
static bool rxrpc_end_tx_phase(struct rxrpc_call *call, const char *abort_why)
{
	_enter("");
95

96 97 98 99 100 101 102 103 104
	switch (call->state) {
	case RXRPC_CALL_CLIENT_RECV_REPLY:
		return true;
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
	case RXRPC_CALL_SERVER_AWAIT_ACK:
		break;
	default:
		rxrpc_proto_abort(abort_why, call, call->tx_top);
		return false;
105 106
	}

107
	rxrpc_rotate_tx_window(call, call->tx_top);
108

109
	write_lock(&call->state_lock);
110

111 112 113 114
	switch (call->state) {
	default:
		break;
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
D
David Howells 已提交
115
		call->tx_phase = false;
116 117 118 119 120 121
		call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
		break;
	case RXRPC_CALL_SERVER_AWAIT_ACK:
		__rxrpc_call_completed(call);
		rxrpc_notify_socket(call);
		break;
122 123
	}

124
	write_unlock(&call->state_lock);
125
	trace_rxrpc_transmit(call, rxrpc_transmit_end);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	_leave(" = ok");
	return true;
}

/*
 * Scan a jumbo packet to validate its structure and to work out how many
 * subpackets it contains.
 *
 * A jumbo packet is a collection of consecutive packets glued together with
 * little headers between that indicate how to change the initial header for
 * each subpacket.
 *
 * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
 * the last are RXRPC_JUMBO_DATALEN in size.  The last subpacket may be of any
 * size.
 */
static bool rxrpc_validate_jumbo(struct sk_buff *skb)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	unsigned int offset = sp->offset;
146
	unsigned int len = skb->len;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	int nr_jumbo = 1;
	u8 flags = sp->hdr.flags;

	do {
		nr_jumbo++;
		if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
			goto protocol_error;
		if (flags & RXRPC_LAST_PACKET)
			goto protocol_error;
		offset += RXRPC_JUMBO_DATALEN;
		if (skb_copy_bits(skb, offset, &flags, 1) < 0)
			goto protocol_error;
		offset += sizeof(struct rxrpc_jumbo_header);
	} while (flags & RXRPC_JUMBO_PACKET);

	sp->nr_jumbo = nr_jumbo;
	return true;
164

165 166
protocol_error:
	return false;
167 168 169
}

/*
170 171 172 173 174 175 176 177 178 179 180
 * Handle reception of a duplicate packet.
 *
 * We have to take care to avoid an attack here whereby we're given a series of
 * jumbograms, each with a sequence number one before the preceding one and
 * filled up to maximum UDP size.  If they never send us the first packet in
 * the sequence, they can cause us to have to hold on to around 2MiB of kernel
 * space until the call times out.
 *
 * We limit the space usage by only accepting three duplicate jumbo packets per
 * call.  After that, we tell the other side we're no longer accepting jumbos
 * (that information is encoded in the ACK packet).
181
 */
182
static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
183
				 u8 annotation, bool *_jumbo_bad)
184
{
185 186 187
	/* Discard normal packets that are duplicates. */
	if (annotation == 0)
		return;
188

189 190 191 192
	/* Skip jumbo subpackets that are duplicates.  When we've had three or
	 * more partially duplicate jumbo packets, we refuse to take any more
	 * jumbos for this call.
	 */
193 194 195
	if (!*_jumbo_bad) {
		call->nr_jumbo_bad++;
		*_jumbo_bad = true;
196 197
	}
}
198

199 200 201 202 203 204 205 206 207 208 209
/*
 * Process a DATA packet, adding the packet to the Rx ring.
 */
static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
			     u16 skew)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	unsigned int offset = sp->offset;
	unsigned int ix;
	rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
	rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
210
	bool immediate_ack = false, jumbo_bad = false, queued;
211 212
	u16 len;
	u8 ack = 0, flags, annotation = 0;
213

214
	_enter("{%u,%u},{%u,%u}",
215
	       call->rx_hard_ack, call->rx_top, skb->len, seq);
216

217 218
	_proto("Rx DATA %%%u { #%u f=%02x }",
	       sp->hdr.serial, seq, sp->hdr.flags);
219

220 221
	if (call->state >= RXRPC_CALL_COMPLETE)
		return;
222

223 224 225 226 227 228
	/* Received data implicitly ACKs all of the request packets we sent
	 * when we're acting as a client.
	 */
	if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY &&
	    !rxrpc_end_tx_phase(call, "ETD"))
		return;
229

230
	call->ackr_prev_seq = seq;
231

232 233
	hard_ack = READ_ONCE(call->rx_hard_ack);
	if (after(seq, hard_ack + call->rx_winsize)) {
234
		ack = RXRPC_ACK_EXCEEDS_WINDOW;
235 236
		ack_serial = serial;
		goto ack;
237 238
	}

239 240
	flags = sp->hdr.flags;
	if (flags & RXRPC_JUMBO_PACKET) {
241
		if (call->nr_jumbo_bad > 3) {
242 243 244
			ack = RXRPC_ACK_NOSPACE;
			ack_serial = serial;
			goto ack;
245
		}
246
		annotation = 1;
247 248
	}

249 250 251
next_subpacket:
	queued = false;
	ix = seq & RXRPC_RXTX_BUFF_MASK;
252
	len = skb->len;
253 254 255 256
	if (flags & RXRPC_JUMBO_PACKET)
		len = RXRPC_JUMBO_DATALEN;

	if (flags & RXRPC_LAST_PACKET) {
257
		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
258 259 260 261 262 263
		    seq != call->rx_top)
			return rxrpc_proto_abort("LSN", call, seq);
	} else {
		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
		    after_eq(seq, call->rx_top))
			return rxrpc_proto_abort("LSA", call, seq);
264 265
	}

266 267 268 269 270 271 272 273 274 275 276 277
	if (before_eq(seq, hard_ack)) {
		ack = RXRPC_ACK_DUPLICATE;
		ack_serial = serial;
		goto skip;
	}

	if (flags & RXRPC_REQUEST_ACK && !ack) {
		ack = RXRPC_ACK_REQUESTED;
		ack_serial = serial;
	}

	if (call->rxtx_buffer[ix]) {
278
		rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
279 280 281
		if (ack != RXRPC_ACK_DUPLICATE) {
			ack = RXRPC_ACK_DUPLICATE;
			ack_serial = serial;
282
		}
283 284
		immediate_ack = true;
		goto skip;
285 286
	}

287 288 289 290 291 292 293 294
	/* Queue the packet.  We use a couple of memory barriers here as need
	 * to make sure that rx_top is perceived to be set after the buffer
	 * pointer and that the buffer pointer is set after the annotation and
	 * the skb data.
	 *
	 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
	 * and also rxrpc_fill_out_ack().
	 */
D
David Howells 已提交
295
	rxrpc_get_skb(skb, rxrpc_skb_rx_got);
296 297 298 299 300
	call->rxtx_annotations[ix] = annotation;
	smp_wmb();
	call->rxtx_buffer[ix] = skb;
	if (after(seq, call->rx_top))
		smp_store_release(&call->rx_top, seq);
301
	if (flags & RXRPC_LAST_PACKET) {
302
		set_bit(RXRPC_CALL_RX_LAST, &call->flags);
303 304 305 306
		trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
	} else {
		trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
	}
307 308 309 310 311 312 313 314 315
	queued = true;

	if (after_eq(seq, call->rx_expect_next)) {
		if (after(seq, call->rx_expect_next)) {
			_net("OOS %u > %u", seq, call->rx_expect_next);
			ack = RXRPC_ACK_OUT_OF_SEQUENCE;
			ack_serial = serial;
		}
		call->rx_expect_next = seq + 1;
316 317
	}

318 319 320 321 322 323 324 325 326 327 328
skip:
	offset += len;
	if (flags & RXRPC_JUMBO_PACKET) {
		if (skb_copy_bits(skb, offset, &flags, 1) < 0)
			return rxrpc_proto_abort("XJF", call, seq);
		offset += sizeof(struct rxrpc_jumbo_header);
		seq++;
		serial++;
		annotation++;
		if (flags & RXRPC_JUMBO_PACKET)
			annotation |= RXRPC_RX_ANNO_JLAST;
329 330 331 332 333 334 335 336 337
		if (after(seq, hard_ack + call->rx_winsize)) {
			ack = RXRPC_ACK_EXCEEDS_WINDOW;
			ack_serial = serial;
			if (!jumbo_bad) {
				call->nr_jumbo_bad++;
				jumbo_bad = true;
			}
			goto ack;
		}
338 339 340 341

		_proto("Rx DATA Jumbo %%%u", serial);
		goto next_subpacket;
	}
342

343 344 345 346
	if (queued && flags & RXRPC_LAST_PACKET && !ack) {
		ack = RXRPC_ACK_DELAY;
		ack_serial = serial;
	}
347

348 349 350 351
ack:
	if (ack)
		rxrpc_propose_ACK(call, ack, skew, ack_serial,
				  immediate_ack, true);
352

353 354 355
	if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
		rxrpc_notify_socket(call);
	_leave(" [queued]");
356 357
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
/*
 * Process a ping response.
 */
static void rxrpc_input_ping_response(struct rxrpc_call *call,
				      ktime_t resp_time,
				      rxrpc_serial_t orig_serial,
				      rxrpc_serial_t ack_serial)
{
	rxrpc_serial_t ping_serial;
	ktime_t ping_time;

	ping_time = call->ackr_ping_time;
	smp_rmb();
	ping_serial = call->ackr_ping;

	if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
	    before(orig_serial, ping_serial))
		return;
	clear_bit(RXRPC_CALL_PINGING, &call->flags);
	if (after(orig_serial, ping_serial))
		return;

	rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
			   orig_serial, ack_serial, ping_time, resp_time);
}

384
/*
385
 * Process the extra information that may be appended to an ACK packet
386
 */
387 388
static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
				struct rxrpc_ackinfo *ackinfo)
389
{
390 391 392
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	struct rxrpc_peer *peer;
	unsigned int mtu;
393
	u32 rwind = ntohl(ackinfo->rwind);
394 395 396 397

	_proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
	       sp->hdr.serial,
	       ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
398
	       rwind, ntohl(ackinfo->jumbo_max));
399

400 401 402
	if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
		rwind = RXRPC_RXTX_BUFF_SIZE - 1;
	call->tx_winsize = rwind;
403 404 405 406 407 408 409 410 411 412 413 414

	mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));

	peer = call->peer;
	if (mtu < peer->maxdata) {
		spin_lock_bh(&peer->lock);
		peer->maxdata = mtu;
		peer->mtu = mtu + peer->hdrsize;
		spin_unlock_bh(&peer->lock);
		_net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
	}
}
415

416 417 418 419 420 421 422 423 424 425 426 427 428 429
/*
 * Process individual soft ACKs.
 *
 * Each ACK in the array corresponds to one packet and can be either an ACK or
 * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
 * packets that lie beyond the end of the ACK list are scheduled for resend by
 * the timer on the basis that the peer might just not have processed them at
 * the time the ACK was sent.
 */
static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
				  rxrpc_seq_t seq, int nr_acks)
{
	bool resend = false;
	int ix;
D
David Howells 已提交
430
	u8 annotation, anno_type;
431 432 433

	for (; nr_acks > 0; nr_acks--, seq++) {
		ix = seq & RXRPC_RXTX_BUFF_MASK;
D
David Howells 已提交
434 435 436
		annotation = call->rxtx_annotations[ix];
		anno_type = annotation & RXRPC_TX_ANNO_MASK;
		annotation &= ~RXRPC_TX_ANNO_MASK;
437
		switch (*acks++) {
438
		case RXRPC_ACK_TYPE_ACK:
D
David Howells 已提交
439 440 441 442
			if (anno_type == RXRPC_TX_ANNO_ACK)
				continue;
			call->rxtx_annotations[ix] =
				RXRPC_TX_ANNO_ACK | annotation;
443 444
			break;
		case RXRPC_ACK_TYPE_NACK:
D
David Howells 已提交
445
			if (anno_type == RXRPC_TX_ANNO_NAK)
446
				continue;
D
David Howells 已提交
447 448
			call->rxtx_annotations[ix] =
				RXRPC_TX_ANNO_NAK | annotation;
449 450 451 452
			resend = true;
			break;
		default:
			return rxrpc_proto_abort("SFT", call, 0);
453 454
		}
	}
455 456 457 458

	if (resend &&
	    !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
		rxrpc_queue_call(call);
459 460 461
}

/*
462 463 464 465 466 467 468 469
 * Process an ACK packet.
 *
 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
 * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
 *
 * A hard-ACK means that a packet has been processed and may be discarded; a
 * soft-ACK means that the packet may be discarded and retransmission
 * requested.  A phase is complete when all packets are hard-ACK'd.
470
 */
471 472
static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
			    u16 skew)
473 474
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
475 476 477 478 479
	union {
		struct rxrpc_ackpacket ack;
		struct rxrpc_ackinfo info;
		u8 acks[RXRPC_MAXACKS];
	} buf;
480
	rxrpc_serial_t acked_serial;
481 482 483 484 485 486 487 488
	rxrpc_seq_t first_soft_ack, hard_ack;
	int nr_acks, offset;

	_enter("");

	if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
		_debug("extraction failure");
		return rxrpc_proto_abort("XAK", call, 0);
489
	}
490 491
	sp->offset += sizeof(buf.ack);

492
	acked_serial = ntohl(buf.ack.serial);
493 494 495 496
	first_soft_ack = ntohl(buf.ack.firstPacket);
	hard_ack = first_soft_ack - 1;
	nr_acks = buf.ack.nAcks;

497 498
	trace_rxrpc_rx_ack(call, first_soft_ack, buf.ack.reason, nr_acks);

499 500 501 502 503
	_proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
	       sp->hdr.serial,
	       ntohs(buf.ack.maxSkew),
	       first_soft_ack,
	       ntohl(buf.ack.previousPacket),
504
	       acked_serial,
505 506 507
	       rxrpc_acks(buf.ack.reason),
	       buf.ack.nAcks);

508 509 510 511
	if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
		rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
					  sp->hdr.serial);

512 513 514 515 516
	if (buf.ack.reason == RXRPC_ACK_PING) {
		_proto("Rx ACK %%%u PING Request", sp->hdr.serial);
		rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
				  skew, sp->hdr.serial, true, true);
	} else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
517
		rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
518
				  skew, sp->hdr.serial, true, true);
519 520
	}

521
	offset = sp->offset + nr_acks + 3;
522
	if (skb->len >= offset + sizeof(buf.info)) {
523 524 525 526
		if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
			return rxrpc_proto_abort("XAI", call, 0);
		rxrpc_input_ackinfo(call, skb, &buf.info);
	}
527

528 529
	if (first_soft_ack == 0)
		return rxrpc_proto_abort("AK0", call, 0);
530

531 532 533 534 535 536 537
	/* Ignore ACKs unless we are or have just been transmitting. */
	switch (call->state) {
	case RXRPC_CALL_CLIENT_SEND_REQUEST:
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
	case RXRPC_CALL_SERVER_SEND_REPLY:
	case RXRPC_CALL_SERVER_AWAIT_ACK:
		break;
538
	default:
539 540
		return;
	}
541

542 543 544 545 546 547 548
	/* Discard any out-of-order or duplicate ACKs. */
	if ((int)sp->hdr.serial - (int)call->acks_latest <= 0) {
		_debug("discard ACK %d <= %d",
		       sp->hdr.serial, call->acks_latest);
		return;
	}
	call->acks_latest = sp->hdr.serial;
549

550 551 552 553 554
	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
	    hard_ack == call->tx_top) {
		rxrpc_end_tx_phase(call, "ETA");
		return;
	}
555

556 557 558
	if (before(hard_ack, call->tx_hard_ack) ||
	    after(hard_ack, call->tx_top))
		return rxrpc_proto_abort("AKW", call, 0);
559

560 561
	if (after(hard_ack, call->tx_hard_ack))
		rxrpc_rotate_tx_window(call, hard_ack);
562

563 564
	if (after(first_soft_ack, call->tx_top))
		return;
565

566 567 568 569 570
	if (nr_acks > call->tx_top - first_soft_ack + 1)
		nr_acks = first_soft_ack - call->tx_top + 1;
	if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
		return rxrpc_proto_abort("XSA", call, 0);
	rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
571 572 573
}

/*
574
 * Process an ACKALL packet.
575
 */
576
static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
577
{
578
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
579

580
	_proto("Rx ACKALL %%%u", sp->hdr.serial);
581

582 583
	rxrpc_end_tx_phase(call, "ETL");
}
584

585 586 587 588 589 590 591 592
/*
 * Process an ABORT packet.
 */
static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	__be32 wtmp;
	u32 abort_code = RX_CALL_DEAD;
593

594
	_enter("");
595

596 597 598
	if (skb->len >= 4 &&
	    skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
		abort_code = ntohl(wtmp);
599

600
	_proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
601

602 603 604
	if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
				      abort_code, ECONNABORTED))
		rxrpc_notify_socket(call);
605 606 607
}

/*
608
 * Process an incoming call packet.
609
 */
610 611
static void rxrpc_input_call_packet(struct rxrpc_call *call,
				    struct sk_buff *skb, u16 skew)
612
{
613
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
614

615
	_enter("%p,%p", call, skb);
616

617 618 619 620
	switch (sp->hdr.type) {
	case RXRPC_PACKET_TYPE_DATA:
		rxrpc_input_data(call, skb, skew);
		break;
621

622 623
	case RXRPC_PACKET_TYPE_ACK:
		rxrpc_input_ack(call, skb, skew);
624 625
		break;

626 627
	case RXRPC_PACKET_TYPE_BUSY:
		_proto("Rx BUSY %%%u", sp->hdr.serial);
628

629 630 631 632 633
		/* Just ignore BUSY packets from the server; the retry and
		 * lifespan timers will take care of business.  BUSY packets
		 * from the client don't make sense.
		 */
		break;
634

635 636 637
	case RXRPC_PACKET_TYPE_ABORT:
		rxrpc_input_abort(call, skb);
		break;
638

639 640 641
	case RXRPC_PACKET_TYPE_ACKALL:
		rxrpc_input_ackall(call, skb);
		break;
642

643 644 645
	default:
		_proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
		break;
646
	}
647

648 649 650 651 652
	_leave("");
}

/*
 * post connection-level events to the connection
653 654
 * - this includes challenges, responses, some aborts and call terminal packet
 *   retransmission.
655
 */
656
static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
657 658 659 660 661
				      struct sk_buff *skb)
{
	_enter("%p,%p", conn, skb);

	skb_queue_tail(&conn->rx_queue, skb);
662
	rxrpc_queue_conn(conn);
663 664
}

665 666 667 668 669 670 671 672 673 674
/*
 * post endpoint-level events to the local endpoint
 * - this includes debug and version messages
 */
static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
				       struct sk_buff *skb)
{
	_enter("%p,%p", local, skb);

	skb_queue_tail(&local->event_queue, skb);
675
	rxrpc_queue_local(local);
676 677
}

678 679 680 681 682 683 684 685 686 687 688
/*
 * put a packet up for transport-level abort
 */
static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
{
	CHECK_SLAB_OKAY(&local->usage);

	skb_queue_tail(&local->reject_queue, skb);
	rxrpc_queue_local(local);
}

689 690 691 692 693 694 695 696 697
/*
 * Extract the wire header from a packet and translate the byte order.
 */
static noinline
int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
{
	struct rxrpc_wire_header whdr;

	/* dig out the RxRPC connection details */
698
	if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
699 700 701 702 703 704 705 706 707 708 709 710 711 712
		return -EBADMSG;

	memset(sp, 0, sizeof(*sp));
	sp->hdr.epoch		= ntohl(whdr.epoch);
	sp->hdr.cid		= ntohl(whdr.cid);
	sp->hdr.callNumber	= ntohl(whdr.callNumber);
	sp->hdr.seq		= ntohl(whdr.seq);
	sp->hdr.serial		= ntohl(whdr.serial);
	sp->hdr.flags		= whdr.flags;
	sp->hdr.type		= whdr.type;
	sp->hdr.userStatus	= whdr.userStatus;
	sp->hdr.securityIndex	= whdr.securityIndex;
	sp->hdr._rsvd		= ntohs(whdr._rsvd);
	sp->hdr.serviceId	= ntohs(whdr.serviceId);
713
	sp->offset = sizeof(whdr);
714 715 716
	return 0;
}

717 718 719
/*
 * handle data received on the local endpoint
 * - may be called in interrupt context
720 721 722 723
 *
 * The socket is locked by the caller and this prevents the socket from being
 * shut down and the local endpoint from going away, thus sk_user_data will not
 * be cleared until this function returns.
724
 */
725
void rxrpc_data_ready(struct sock *udp_sk)
726
{
727
	struct rxrpc_connection *conn;
728 729
	struct rxrpc_channel *chan;
	struct rxrpc_call *call;
730
	struct rxrpc_skb_priv *sp;
731
	struct rxrpc_local *local = udp_sk->sk_user_data;
732
	struct sk_buff *skb;
733
	unsigned int channel;
734
	int ret, skew;
735

736
	_enter("%p", udp_sk);
737 738 739

	ASSERT(!irqs_disabled());

740
	skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
741 742 743 744 745 746 747
	if (!skb) {
		if (ret == -EAGAIN)
			return;
		_debug("UDP socket error %d", ret);
		return;
	}

D
David Howells 已提交
748
	rxrpc_new_skb(skb, rxrpc_skb_rx_received);
749 750 751 752 753

	_net("recv skb %p", skb);

	/* we'll probably need to checksum it (didn't call sock_recvmsg) */
	if (skb_checksum_complete(skb)) {
D
David Howells 已提交
754
		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
755
		__UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
756 757 758 759
		_leave(" [CSUM failed]");
		return;
	}

760
	__UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
761

762 763 764
	/* The socket buffer we have is owned by UDP, with UDP's data all over
	 * it, but we really want our own data there.
	 */
765 766 767
	skb_orphan(skb);
	sp = rxrpc_skb(skb);

768 769 770 771 772 773 774 775
	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
		static int lose;
		if ((lose++ & 7) == 7) {
			rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
			return;
		}
	}

776 777 778 779
	_net("Rx UDP packet from %08x:%04hu",
	     ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));

	/* dig out the RxRPC connection details */
780
	if (rxrpc_extract_header(sp, skb) < 0)
781
		goto bad_message;
782
	trace_rxrpc_rx_packet(sp);
783 784 785

	_net("Rx RxRPC %s ep=%x call=%x:%x",
	     sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
786
	     sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
787

788 789
	if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
	    !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
790 791 792 793
		_proto("Rx Bad Packet Type %u", sp->hdr.type);
		goto bad_message;
	}

794 795
	switch (sp->hdr.type) {
	case RXRPC_PACKET_TYPE_VERSION:
796 797
		rxrpc_post_packet_to_local(local, skb);
		goto out;
798

799 800 801 802 803 804 805 806 807 808 809 810
	case RXRPC_PACKET_TYPE_BUSY:
		if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
			goto discard;

	case RXRPC_PACKET_TYPE_DATA:
		if (sp->hdr.callNumber == 0)
			goto bad_message;
		if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
		    !rxrpc_validate_jumbo(skb))
			goto bad_message;
		break;
	}
811

812 813 814
	rcu_read_lock();

	conn = rxrpc_find_connection_rcu(local, skb);
815 816 817
	if (conn) {
		if (sp->hdr.securityIndex != conn->security_ix)
			goto wrong_security;
818

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
		if (sp->hdr.callNumber == 0) {
			/* Connection-level packet */
			_debug("CONN %p {%d}", conn, conn->debug_id);
			rxrpc_post_packet_to_conn(conn, skb);
			goto out_unlock;
		}

		/* Note the serial number skew here */
		skew = (int)sp->hdr.serial - (int)conn->hi_serial;
		if (skew >= 0) {
			if (skew > 0)
				conn->hi_serial = sp->hdr.serial;
		} else {
			skew = -skew;
			skew = min(skew, 65535);
		}
835

836
		/* Call-bound packets are routed by connection channel. */
837 838
		channel = sp->hdr.cid & RXRPC_CHANNELMASK;
		chan = &conn->channels[channel];
839 840 841 842 843 844

		/* Ignore really old calls */
		if (sp->hdr.callNumber < chan->last_call)
			goto discard_unlock;

		if (sp->hdr.callNumber == chan->last_call) {
845 846
			/* For the previous service call, if completed successfully, we
			 * discard all further packets.
847
			 */
D
David Howells 已提交
848
			if (rxrpc_conn_is_service(conn) &&
849 850 851 852
			    (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
			     sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
				goto discard_unlock;

853 854
			/* But otherwise we need to retransmit the final packet from
			 * data cached in the connection record.
855 856 857 858
			 */
			rxrpc_post_packet_to_conn(conn, skb);
			goto out_unlock;
		}
859

860
		call = rcu_dereference(chan->call);
861 862 863 864
	} else {
		skew = 0;
		call = NULL;
	}
865

866 867 868 869 870 871 872 873 874 875 876 877
	if (!call || atomic_read(&call->usage) == 0) {
		if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
		    sp->hdr.callNumber == 0 ||
		    sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
			goto bad_message_unlock;
		if (sp->hdr.seq != 1)
			goto discard_unlock;
		call = rxrpc_new_incoming_call(local, conn, skb);
		if (!call) {
			rcu_read_unlock();
			goto reject_packet;
		}
878
		rxrpc_send_ping(call, skb, skew);
879
	}
880

881 882 883
	rxrpc_input_call_packet(call, skb, skew);
	goto discard_unlock;

884
discard_unlock:
885
	rcu_read_unlock();
886
discard:
D
David Howells 已提交
887
	rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
888
out:
889
	trace_rxrpc_rx_done(0, 0);
890 891
	return;

892
out_unlock:
893
	rcu_read_unlock();
894
	goto out;
895

896 897 898 899 900 901
wrong_security:
	rcu_read_unlock();
	trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
			  RXKADINCONSISTENCY, EBADMSG);
	skb->priority = RXKADINCONSISTENCY;
	goto post_abort;
902

903 904
bad_message_unlock:
	rcu_read_unlock();
905
bad_message:
906 907
	trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
			  RX_PROTOCOL_ERROR, EBADMSG);
908
	skb->priority = RX_PROTOCOL_ERROR;
909 910
post_abort:
	skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
911 912
reject_packet:
	trace_rxrpc_rx_done(skb->mark, skb->priority);
913 914 915
	rxrpc_reject_packet(local, skb);
	_leave(" [badmsg]");
}