input.c 19.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* RxRPC packet reception
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * 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 30 31 32 33 34 35 36 37 38 39 40 41
#include "ar-internal.h"

/*
 * queue a packet for recvmsg to pass to userspace
 * - the caller must hold a lock on call->lock
 * - must not be called with interrupts disabled (sk_filter() disables BH's)
 * - eats the packet whether successful or not
 * - there must be just one reference to the packet, which the caller passes to
 *   this function
 */
int rxrpc_queue_rcv_skb(struct rxrpc_call *call, struct sk_buff *skb,
			bool force, bool terminal)
{
	struct rxrpc_skb_priv *sp;
42
	struct rxrpc_sock *rx;
43
	struct sock *sk;
44
	int ret;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

	_enter(",,%d,%d", force, terminal);

	ASSERT(!irqs_disabled());

	sp = rxrpc_skb(skb);
	ASSERTCMP(sp->call, ==, call);

	/* if we've already posted the terminal message for a call, then we
	 * don't post any more */
	if (test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
		_debug("already terminated");
		ASSERTCMP(call->state, >=, RXRPC_CALL_COMPLETE);
		rxrpc_free_skb(skb);
		return 0;
	}

62 63 64 65 66 67
	/* The socket may go away under us */
	ret = 0;
	rcu_read_lock();
	rx = rcu_dereference(call->socket);
	if (!rx)
		goto out;
68
	sk = &rx->sk;
69 70
	if (sock_flag(sk, SOCK_DEAD))
		goto out;
71 72 73 74 75 76 77

	if (!force) {
		/* cast skb->rcvbuf to unsigned...  It's pointless, but
		 * reduces number of warnings when compiling with -W
		 * --ANK */
//		ret = -ENOBUFS;
//		if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
78
//		    (unsigned int) sk->sk_rcvbuf)
79 80 81 82 83 84 85 86 87 88
//			goto out;

		ret = sk_filter(sk, skb);
		if (ret < 0)
			goto out;
	}

	spin_lock_bh(&sk->sk_receive_queue.lock);
	if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags) &&
	    !test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
89
	    sk->sk_state != RXRPC_CLOSE) {
90 91 92 93 94 95 96 97 98 99
		skb->destructor = rxrpc_packet_destructor;
		skb->dev = NULL;
		skb->sk = sk;
		atomic_add(skb->truesize, &sk->sk_rmem_alloc);

		if (terminal) {
			_debug("<<<< TERMINAL MESSAGE >>>>");
			set_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags);
		}

100
		/* allow interception by a kernel service */
101 102
		if (skb->mark == RXRPC_SKB_MARK_NEW_CALL &&
		    rx->notify_new_call) {
103
			spin_unlock_bh(&sk->sk_receive_queue.lock);
104
			skb_queue_tail(&call->knlrecv_queue, skb);
105
			rx->notify_new_call(&rx->sk, NULL, 0);
106 107 108 109
		} else if (call->notify_rx) {
			spin_unlock_bh(&sk->sk_receive_queue.lock);
			skb_queue_tail(&call->knlrecv_queue, skb);
			call->notify_rx(&rx->sk, call, call->user_call_ID);
110 111 112 113 114
		} else {
			_net("post skb %p", skb);
			__skb_queue_tail(&sk->sk_receive_queue, skb);
			spin_unlock_bh(&sk->sk_receive_queue.lock);

115
			sk->sk_data_ready(sk);
116
		}
117 118 119 120 121 122 123
		skb = NULL;
	} else {
		spin_unlock_bh(&sk->sk_receive_queue.lock);
	}
	ret = 0;

out:
124
	rxrpc_free_skb(skb);
125
	rcu_read_unlock();
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

	_leave(" = %d", ret);
	return ret;
}

/*
 * process a DATA packet, posting the packet to the appropriate queue
 * - eats the packet if successful
 */
static int rxrpc_fast_process_data(struct rxrpc_call *call,
				   struct sk_buff *skb, u32 seq)
{
	struct rxrpc_skb_priv *sp;
	bool terminal;
	int ret, ackbit, ack;
141
	u32 serial;
142
	u16 skew;
143
	u8 flags;
144 145 146 147 148

	_enter("{%u,%u},,{%u}", call->rx_data_post, call->rx_first_oos, seq);

	sp = rxrpc_skb(skb);
	ASSERTCMP(sp->call, ==, NULL);
149 150
	flags = sp->hdr.flags;
	serial = sp->hdr.serial;
151
	skew = skb->priority;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

	spin_lock(&call->lock);

	if (call->state > RXRPC_CALL_COMPLETE)
		goto discard;

	ASSERTCMP(call->rx_data_expect, >=, call->rx_data_post);
	ASSERTCMP(call->rx_data_post, >=, call->rx_data_recv);
	ASSERTCMP(call->rx_data_recv, >=, call->rx_data_eaten);

	if (seq < call->rx_data_post) {
		_debug("dup #%u [-%u]", seq, call->rx_data_post);
		ack = RXRPC_ACK_DUPLICATE;
		ret = -ENOBUFS;
		goto discard_and_ack;
	}

	/* we may already have the packet in the out of sequence queue */
	ackbit = seq - (call->rx_data_eaten + 1);
	ASSERTCMP(ackbit, >=, 0);
172
	if (__test_and_set_bit(ackbit, call->ackr_window)) {
173 174 175 176 177 178 179 180
		_debug("dup oos #%u [%u,%u]",
		       seq, call->rx_data_eaten, call->rx_data_post);
		ack = RXRPC_ACK_DUPLICATE;
		goto discard_and_ack;
	}

	if (seq >= call->ackr_win_top) {
		_debug("exceed #%u [%u]", seq, call->ackr_win_top);
181
		__clear_bit(ackbit, call->ackr_window);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		ack = RXRPC_ACK_EXCEEDS_WINDOW;
		goto discard_and_ack;
	}

	if (seq == call->rx_data_expect) {
		clear_bit(RXRPC_CALL_EXPECT_OOS, &call->flags);
		call->rx_data_expect++;
	} else if (seq > call->rx_data_expect) {
		_debug("oos #%u [%u]", seq, call->rx_data_expect);
		call->rx_data_expect = seq + 1;
		if (test_and_set_bit(RXRPC_CALL_EXPECT_OOS, &call->flags)) {
			ack = RXRPC_ACK_OUT_OF_SEQUENCE;
			goto enqueue_and_ack;
		}
		goto enqueue_packet;
	}

	if (seq != call->rx_data_post) {
		_debug("ahead #%u [%u]", seq, call->rx_data_post);
		goto enqueue_packet;
	}

	if (test_bit(RXRPC_CALL_RCVD_LAST, &call->flags))
		goto protocol_error;

	/* if the packet need security things doing to it, then it goes down
	 * the slow path */
209
	if (call->security_ix)
210 211 212
		goto enqueue_packet;

	sp->call = call;
D
David Howells 已提交
213
	rxrpc_get_call_for_skb(call, skb);
214 215
	terminal = ((flags & RXRPC_LAST_PACKET) &&
		    !(flags & RXRPC_CLIENT_INITIATED));
216 217 218
	ret = rxrpc_queue_rcv_skb(call, skb, false, terminal);
	if (ret < 0) {
		if (ret == -ENOMEM || ret == -ENOBUFS) {
219
			__clear_bit(ackbit, call->ackr_window);
220 221 222 223 224 225 226
			ack = RXRPC_ACK_NOSPACE;
			goto discard_and_ack;
		}
		goto out;
	}

	skb = NULL;
227
	sp = NULL;
228 229 230 231 232

	_debug("post #%u", seq);
	ASSERTCMP(call->rx_data_post, ==, seq);
	call->rx_data_post++;

233
	if (flags & RXRPC_LAST_PACKET)
234 235 236 237 238 239 240 241
		set_bit(RXRPC_CALL_RCVD_LAST, &call->flags);

	/* if we've reached an out of sequence packet then we need to drain
	 * that queue into the socket Rx queue now */
	if (call->rx_data_post == call->rx_first_oos) {
		_debug("drain rx oos now");
		read_lock(&call->state_lock);
		if (call->state < RXRPC_CALL_COMPLETE &&
242
		    !test_and_set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events))
243
			rxrpc_queue_call(call);
244 245 246 247 248
		read_unlock(&call->state_lock);
	}

	spin_unlock(&call->lock);
	atomic_inc(&call->ackr_not_idle);
249
	rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, skew, serial, false);
250 251 252 253 254 255 256 257 258 259 260 261
	_leave(" = 0 [posted]");
	return 0;

protocol_error:
	ret = -EBADMSG;
out:
	spin_unlock(&call->lock);
	_leave(" = %d", ret);
	return ret;

discard_and_ack:
	_debug("discard and ACK packet %p", skb);
262
	__rxrpc_propose_ACK(call, ack, skew, serial, true);
263 264 265 266 267 268 269
discard:
	spin_unlock(&call->lock);
	rxrpc_free_skb(skb);
	_leave(" = 0 [discarded]");
	return 0;

enqueue_and_ack:
270
	__rxrpc_propose_ACK(call, ack, skew, serial, true);
271 272 273 274 275 276
enqueue_packet:
	_net("defer skb %p", skb);
	spin_unlock(&call->lock);
	skb_queue_tail(&call->rx_queue, skb);
	atomic_inc(&call->ackr_not_idle);
	read_lock(&call->state_lock);
277
	if (call->state < RXRPC_CALL_COMPLETE)
278
		rxrpc_queue_call(call);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	read_unlock(&call->state_lock);
	_leave(" = 0 [queued]");
	return 0;
}

/*
 * assume an implicit ACKALL of the transmission phase of a client socket upon
 * reception of the first reply packet
 */
static void rxrpc_assume_implicit_ackall(struct rxrpc_call *call, u32 serial)
{
	write_lock_bh(&call->state_lock);

	switch (call->state) {
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
		call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
		call->acks_latest = serial;

		_debug("implicit ACKALL %%%u", call->acks_latest);
298
		set_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events);
299 300 301
		write_unlock_bh(&call->state_lock);

		if (try_to_del_timer_sync(&call->resend_timer) >= 0) {
302 303
			clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
			clear_bit(RXRPC_CALL_EV_RESEND, &call->events);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
			clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
		}
		break;

	default:
		write_unlock_bh(&call->state_lock);
		break;
	}
}

/*
 * post an incoming packet to the nominated call to deal with
 * - must get rid of the sk_buff, either by freeing it or by queuing it
 */
void rxrpc_fast_process_packet(struct rxrpc_call *call, struct sk_buff *skb)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
321
	__be32 wtmp;
322
	u32 abort_code;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

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

	ASSERT(!irqs_disabled());

#if 0 // INJECT RX ERROR
	if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
		static int skip = 0;
		if (++skip == 3) {
			printk("DROPPED 3RD PACKET!!!!!!!!!!!!!\n");
			skip = 0;
			goto free_packet;
		}
	}
#endif

	/* request ACK generation for any ACK or DATA packet that requests
	 * it */
	if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
342
		_proto("ACK Requested on %%%u", sp->hdr.serial);
343 344
		rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
				  skb->priority, sp->hdr.serial, false);
345 346 347 348 349 350
	}

	switch (sp->hdr.type) {
	case RXRPC_PACKET_TYPE_ABORT:
		_debug("abort");

351
		if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
352 353
			goto protocol_error;

354 355
		abort_code = ntohl(wtmp);
		_proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
356

357 358 359
		if (__rxrpc_set_call_completion(call,
						RXRPC_CALL_REMOTELY_ABORTED,
						abort_code, ECONNABORTED)) {
360
			set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
361
			rxrpc_queue_call(call);
362
		}
363
		goto free_packet;
364 365

	case RXRPC_PACKET_TYPE_BUSY:
366
		_proto("Rx BUSY %%%u", sp->hdr.serial);
367

368
		if (rxrpc_is_service_call(call))
369 370 371 372 373
			goto protocol_error;

		write_lock_bh(&call->state_lock);
		switch (call->state) {
		case RXRPC_CALL_CLIENT_SEND_REQUEST:
374 375 376
			__rxrpc_set_call_completion(call,
						    RXRPC_CALL_SERVER_BUSY,
						    0, EBUSY);
377
			set_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
378
			rxrpc_queue_call(call);
379 380 381 382 383 384 385
		case RXRPC_CALL_SERVER_BUSY:
			goto free_packet_unlock;
		default:
			goto protocol_error_locked;
		}

	default:
386
		_proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
387 388 389
		goto protocol_error;

	case RXRPC_PACKET_TYPE_DATA:
390
		_proto("Rx DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
391

392
		if (sp->hdr.seq == 0)
393 394 395 396 397 398 399
			goto protocol_error;

		call->ackr_prev_seq = sp->hdr.seq;

		/* 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)
400
			rxrpc_assume_implicit_ackall(call, sp->hdr.serial);
401

402
		switch (rxrpc_fast_process_data(call, skb, sp->hdr.seq)) {
403 404 405 406 407 408 409 410 411 412 413 414
		case 0:
			skb = NULL;
			goto done;

		default:
			BUG();

			/* data packet received beyond the last packet */
		case -EBADMSG:
			goto protocol_error;
		}

415
	case RXRPC_PACKET_TYPE_ACKALL:
416 417 418
	case RXRPC_PACKET_TYPE_ACK:
		/* ACK processing is done in process context */
		read_lock_bh(&call->state_lock);
419
		if (call->state < RXRPC_CALL_COMPLETE) {
420
			skb_queue_tail(&call->rx_queue, skb);
421
			rxrpc_queue_call(call);
422 423 424 425 426 427 428 429 430 431
			skb = NULL;
		}
		read_unlock_bh(&call->state_lock);
		goto free_packet;
	}

protocol_error:
	_debug("protocol error");
	write_lock_bh(&call->state_lock);
protocol_error_locked:
432
	if (__rxrpc_abort_call("FPR", call, 0, RX_PROTOCOL_ERROR, EPROTO))
433
		rxrpc_queue_call(call);
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
free_packet_unlock:
	write_unlock_bh(&call->state_lock);
free_packet:
	rxrpc_free_skb(skb);
done:
	_leave("");
}

/*
 * split up a jumbo data packet
 */
static void rxrpc_process_jumbo_packet(struct rxrpc_call *call,
				       struct sk_buff *jumbo)
{
	struct rxrpc_jumbo_header jhdr;
	struct rxrpc_skb_priv *sp;
	struct sk_buff *part;

	_enter(",{%u,%u}", jumbo->data_len, jumbo->len);

	sp = rxrpc_skb(jumbo);

	do {
		sp->hdr.flags &= ~RXRPC_JUMBO_PACKET;

		/* make a clone to represent the first subpacket in what's left
		 * of the jumbo packet */
		part = skb_clone(jumbo, GFP_ATOMIC);
		if (!part) {
			/* simply ditch the tail in the event of ENOMEM */
			pskb_trim(jumbo, RXRPC_JUMBO_DATALEN);
			break;
		}
		rxrpc_new_skb(part);

		pskb_trim(part, RXRPC_JUMBO_DATALEN);

		if (!pskb_pull(jumbo, RXRPC_JUMBO_DATALEN))
			goto protocol_error;

		if (skb_copy_bits(jumbo, 0, &jhdr, sizeof(jhdr)) < 0)
			goto protocol_error;
		if (!pskb_pull(jumbo, sizeof(jhdr)))
			BUG();

479 480
		sp->hdr.seq	+= 1;
		sp->hdr.serial	+= 1;
481
		sp->hdr.flags	= jhdr.flags;
482
		sp->hdr._rsvd	= ntohs(jhdr._rsvd);
483

484
		_proto("Rx DATA Jumbo %%%u", sp->hdr.serial - 1);
485 486 487 488 489 490 491 492 493 494 495 496 497

		rxrpc_fast_process_packet(call, part);
		part = NULL;

	} while (sp->hdr.flags & RXRPC_JUMBO_PACKET);

	rxrpc_fast_process_packet(call, jumbo);
	_leave("");
	return;

protocol_error:
	_debug("protocol error");
	rxrpc_free_skb(part);
498 499
	if (rxrpc_abort_call("PJP", call, sp->hdr.seq,
			     RX_PROTOCOL_ERROR, EPROTO))
500
		rxrpc_queue_call(call);
501
	rxrpc_free_skb(jumbo);
502 503 504 505 506 507 508
	_leave("");
}

/*
 * post an incoming packet to the appropriate call/socket to deal with
 * - must get rid of the sk_buff, either by freeing it or by queuing it
 */
509 510
static void rxrpc_post_packet_to_call(struct rxrpc_connection *conn,
				      struct rxrpc_call *call,
511 512 513 514
				      struct sk_buff *skb)
{
	struct rxrpc_skb_priv *sp;

515
	_enter("%p,%p", call, skb);
516 517 518 519 520 521 522

	sp = rxrpc_skb(skb);

	_debug("extant call [%d]", call->state);

	read_lock(&call->state_lock);
	switch (call->state) {
523
	case RXRPC_CALL_COMPLETE:
524 525 526 527 528 529 530 531
		switch (call->completion) {
		case RXRPC_CALL_LOCALLY_ABORTED:
			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT,
					      &call->events)) {
				rxrpc_queue_call(call);
				goto free_unlock;
			}
		default:
532
			goto dead_call;
533
		case RXRPC_CALL_SUCCEEDED:
534
			if (rxrpc_is_service_call(call))
535 536 537 538 539 540 541
				goto dead_call;
			goto resend_final_ack;
		}

	case RXRPC_CALL_CLIENT_FINAL_ACK:
		goto resend_final_ack;

542 543 544 545 546 547 548 549 550 551 552 553 554 555
	default:
		break;
	}

	read_unlock(&call->state_lock);

	if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
	    sp->hdr.flags & RXRPC_JUMBO_PACKET)
		rxrpc_process_jumbo_packet(call, skb);
	else
		rxrpc_fast_process_packet(call, skb);

	goto done;

556 557 558 559 560 561
resend_final_ack:
	_debug("final ack again");
	set_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events);
	rxrpc_queue_call(call);
	goto free_unlock;

562
dead_call:
563 564
	if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
		skb->priority = RX_CALL_DEAD;
565
		rxrpc_reject_packet(conn->params.local, skb);
566
		goto unlock;
567 568 569
	}
free_unlock:
	rxrpc_free_skb(skb);
570 571
unlock:
	read_unlock(&call->state_lock);
572 573 574 575 576 577
done:
	_leave("");
}

/*
 * post connection-level events to the connection
578 579
 * - this includes challenges, responses, some aborts and call terminal packet
 *   retransmission.
580
 */
581
static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
582 583 584 585 586
				      struct sk_buff *skb)
{
	_enter("%p,%p", conn, skb);

	skb_queue_tail(&conn->rx_queue, skb);
587
	rxrpc_queue_conn(conn);
588 589
}

590 591 592 593 594 595 596 597 598 599
/*
 * 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);
600
	rxrpc_queue_local(local);
601 602
}

603 604 605 606 607 608 609 610 611
/*
 * 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 */
612
	if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
613
		return -EBADMSG;
614
	if (!pskb_pull(skb, sizeof(whdr)))
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
		BUG();

	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);
	return 0;
}

632 633 634
/*
 * handle data received on the local endpoint
 * - may be called in interrupt context
635 636 637 638
 *
 * 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.
639
 */
640
void rxrpc_data_ready(struct sock *sk)
641
{
642
	struct rxrpc_connection *conn;
643
	struct rxrpc_skb_priv *sp;
644
	struct rxrpc_local *local = sk->sk_user_data;
645
	struct sk_buff *skb;
646
	int ret, skew;
647

648
	_enter("%p", sk);
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

	ASSERT(!irqs_disabled());

	skb = skb_recv_datagram(sk, 0, 1, &ret);
	if (!skb) {
		if (ret == -EAGAIN)
			return;
		_debug("UDP socket error %d", ret);
		return;
	}

	rxrpc_new_skb(skb);

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

	/* we'll probably need to checksum it (didn't call sock_recvmsg) */
	if (skb_checksum_complete(skb)) {
		rxrpc_free_skb(skb);
667
		__UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
668 669 670 671
		_leave(" [CSUM failed]");
		return;
	}

672
	__UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
673

674 675 676
	/* The socket buffer we have is owned by UDP, with UDP's data all over
	 * it, but we really want our own data there.
	 */
677 678 679 680 681 682 683
	skb_orphan(skb);
	sp = rxrpc_skb(skb);

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

	/* dig out the RxRPC connection details */
684
	if (rxrpc_extract_header(sp, skb) < 0)
685
		goto bad_message;
686
	trace_rxrpc_rx_packet(sp);
687 688 689

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

692 693
	if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
	    !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
694 695 696 697
		_proto("Rx Bad Packet Type %u", sp->hdr.type);
		goto bad_message;
	}

698 699 700 701
	if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
		rxrpc_post_packet_to_local(local, skb);
		goto out;
	}
702

703 704 705 706
	if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
	    (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
		goto bad_message;

707 708 709
	rcu_read_lock();

	conn = rxrpc_find_connection_rcu(local, skb);
710 711
	if (!conn) {
		skb->priority = 0;
712
		goto cant_route_call;
713 714 715 716 717 718 719 720 721 722 723 724
	}

	/* 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;
		skb->priority = 0;
	} else {
		skew = -skew;
		skb->priority = min(skew, 65535);
	}
725

726 727
	if (sp->hdr.callNumber == 0) {
		/* Connection-level packet */
728
		_debug("CONN %p {%d}", conn, conn->debug_id);
729
		rxrpc_post_packet_to_conn(conn, skb);
730
		goto out_unlock;
731
	} else {
732 733 734
		/* Call-bound packets are routed by connection channel. */
		unsigned int channel = sp->hdr.cid & RXRPC_CHANNELMASK;
		struct rxrpc_channel *chan = &conn->channels[channel];
735 736 737 738 739 740 741 742 743 744
		struct rxrpc_call *call;

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

		if (sp->hdr.callNumber == chan->last_call) {
			/* For the previous service call, if completed
			 * successfully, we discard all further packets.
			 */
D
David Howells 已提交
745
			if (rxrpc_conn_is_service(conn) &&
746 747 748 749 750 751 752 753 754 755
			    (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
			     sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
				goto discard_unlock;

			/* But otherwise we need to retransmit the final packet
			 * from data cached in the connection record.
			 */
			rxrpc_post_packet_to_conn(conn, skb);
			goto out_unlock;
		}
756

757
		call = rcu_dereference(chan->call);
758
		if (!call || atomic_read(&call->usage) == 0)
759
			goto cant_route_call;
760

D
David Howells 已提交
761
		rxrpc_see_call(call);
762
		rxrpc_post_packet_to_call(conn, call, skb);
763
		goto out_unlock;
764
	}
765

766 767 768
discard_unlock:
	rxrpc_free_skb(skb);
out_unlock:
769
	rcu_read_unlock();
770
out:
771
	trace_rxrpc_rx_done(0, 0);
772 773 774
	return;

cant_route_call:
775 776
	rcu_read_unlock();

777 778 779
	_debug("can't route call");
	if (sp->hdr.flags & RXRPC_CLIENT_INITIATED &&
	    sp->hdr.type == RXRPC_PACKET_TYPE_DATA) {
780
		if (sp->hdr.seq == 1) {
781 782
			_debug("first packet");
			skb_queue_tail(&local->accept_queue, skb);
783
			rxrpc_queue_work(&local->processor);
784
			_leave(" [incoming]");
785
			goto out;
786 787 788 789 790 791
		}
		skb->priority = RX_INVALID_OPERATION;
	} else {
		skb->priority = RX_CALL_DEAD;
	}

792 793
	if (sp->hdr.type != RXRPC_PACKET_TYPE_ABORT) {
		_debug("reject type %d",sp->hdr.type);
794
		goto reject_packet;
795 796
	} else {
		rxrpc_free_skb(skb);
797
	}
798 799 800 801 802
	_leave(" [no call]");
	return;

bad_message:
	skb->priority = RX_PROTOCOL_ERROR;
803 804
reject_packet:
	trace_rxrpc_rx_done(skb->mark, skb->priority);
805 806 807
	rxrpc_reject_packet(local, skb);
	_leave(" [badmsg]");
}