1451 lines · 37.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* RxRPC packet reception * * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #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>#include <linux/gfp.h>#include <net/sock.h>#include <net/af_rxrpc.h>#include <net/ip.h>#include <net/udp.h>#include <net/net_namespace.h>#include "ar-internal.h" 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);	}} /* * Do TCP-style congestion management [RFC 5681]. */static void rxrpc_congestion_management(struct rxrpc_call *call,					struct sk_buff *skb,					struct rxrpc_ack_summary *summary,					rxrpc_serial_t acked_serial){	enum rxrpc_congest_change change = rxrpc_cong_no_change;	unsigned int cumulative_acks = call->cong_cumul_acks;	unsigned int cwnd = call->cong_cwnd;	bool resend = false; 	summary->flight_size =		(call->tx_top - call->tx_hard_ack) - summary->nr_acks; 	if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {		summary->retrans_timeo = true;		call->cong_ssthresh = max_t(unsigned int,					    summary->flight_size / 2, 2);		cwnd = 1;		if (cwnd >= call->cong_ssthresh &&		    call->cong_mode == RXRPC_CALL_SLOW_START) {			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;			call->cong_tstamp = skb->tstamp;			cumulative_acks = 0;		}	} 	cumulative_acks += summary->nr_new_acks;	cumulative_acks += summary->nr_rot_new_acks;	if (cumulative_acks > 255)		cumulative_acks = 255; 	summary->mode = call->cong_mode;	summary->cwnd = call->cong_cwnd;	summary->ssthresh = call->cong_ssthresh;	summary->cumulative_acks = cumulative_acks;	summary->dup_acks = call->cong_dup_acks; 	switch (call->cong_mode) {	case RXRPC_CALL_SLOW_START:		if (summary->nr_nacks > 0)			goto packet_loss_detected;		if (summary->cumulative_acks > 0)			cwnd += 1;		if (cwnd >= call->cong_ssthresh) {			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;			call->cong_tstamp = skb->tstamp;		}		goto out; 	case RXRPC_CALL_CONGEST_AVOIDANCE:		if (summary->nr_nacks > 0)			goto packet_loss_detected; 		/* We analyse the number of packets that get ACK'd per RTT		 * period and increase the window if we managed to fill it.		 */		if (call->peer->rtt_count == 0)			goto out;		if (ktime_before(skb->tstamp,				 ktime_add_us(call->cong_tstamp,					      call->peer->srtt_us >> 3)))			goto out_no_clear_ca;		change = rxrpc_cong_rtt_window_end;		call->cong_tstamp = skb->tstamp;		if (cumulative_acks >= cwnd)			cwnd++;		goto out; 	case RXRPC_CALL_PACKET_LOSS:		if (summary->nr_nacks == 0)			goto resume_normality; 		if (summary->new_low_nack) {			change = rxrpc_cong_new_low_nack;			call->cong_dup_acks = 1;			if (call->cong_extra > 1)				call->cong_extra = 1;			goto send_extra_data;		} 		call->cong_dup_acks++;		if (call->cong_dup_acks < 3)			goto send_extra_data; 		change = rxrpc_cong_begin_retransmission;		call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;		call->cong_ssthresh = max_t(unsigned int,					    summary->flight_size / 2, 2);		cwnd = call->cong_ssthresh + 3;		call->cong_extra = 0;		call->cong_dup_acks = 0;		resend = true;		goto out; 	case RXRPC_CALL_FAST_RETRANSMIT:		if (!summary->new_low_nack) {			if (summary->nr_new_acks == 0)				cwnd += 1;			call->cong_dup_acks++;			if (call->cong_dup_acks == 2) {				change = rxrpc_cong_retransmit_again;				call->cong_dup_acks = 0;				resend = true;			}		} else {			change = rxrpc_cong_progress;			cwnd = call->cong_ssthresh;			if (summary->nr_nacks == 0)				goto resume_normality;		}		goto out; 	default:		BUG();		goto out;	} resume_normality:	change = rxrpc_cong_cleared_nacks;	call->cong_dup_acks = 0;	call->cong_extra = 0;	call->cong_tstamp = skb->tstamp;	if (cwnd < call->cong_ssthresh)		call->cong_mode = RXRPC_CALL_SLOW_START;	else		call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;out:	cumulative_acks = 0;out_no_clear_ca:	if (cwnd >= RXRPC_RXTX_BUFF_SIZE - 1)		cwnd = RXRPC_RXTX_BUFF_SIZE - 1;	call->cong_cwnd = cwnd;	call->cong_cumul_acks = cumulative_acks;	trace_rxrpc_congest(call, summary, acked_serial, change);	if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))		rxrpc_queue_call(call);	return; packet_loss_detected:	change = rxrpc_cong_saw_nack;	call->cong_mode = RXRPC_CALL_PACKET_LOSS;	call->cong_dup_acks = 0;	goto send_extra_data; send_extra_data:	/* Send some previously unsent DATA if we have some to advance the ACK	 * state.	 */	if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &	    RXRPC_TX_ANNO_LAST ||	    summary->nr_acks != call->tx_top - call->tx_hard_ack) {		call->cong_extra++;		wake_up(&call->waitq);	}	goto out_no_clear_ca;} /* * Apply a hard ACK by advancing the Tx window. */static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,				   struct rxrpc_ack_summary *summary){	struct sk_buff *skb, *list = NULL;	bool rot_last = false;	int ix;	u8 annotation; 	if (call->acks_lowest_nak == call->tx_hard_ack) {		call->acks_lowest_nak = to;	} else if (before_eq(call->acks_lowest_nak, to)) {		summary->new_low_nack = true;		call->acks_lowest_nak = to;	} 	spin_lock(&call->lock); 	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];		annotation = call->rxtx_annotations[ix];		rxrpc_see_skb(skb, rxrpc_skb_rotated);		call->rxtx_buffer[ix] = NULL;		call->rxtx_annotations[ix] = 0;		skb->next = list;		list = skb; 		if (annotation & RXRPC_TX_ANNO_LAST) {			set_bit(RXRPC_CALL_TX_LAST, &call->flags);			rot_last = true;		}		if ((annotation & RXRPC_TX_ANNO_MASK) != RXRPC_TX_ANNO_ACK)			summary->nr_rot_new_acks++;	} 	spin_unlock(&call->lock); 	trace_rxrpc_transmit(call, (rot_last ?				    rxrpc_transmit_rotate_last :				    rxrpc_transmit_rotate));	wake_up(&call->waitq); 	while (list) {		skb = list;		list = skb->next;		skb_mark_not_on_list(skb);		rxrpc_free_skb(skb, rxrpc_skb_freed);	} 	return rot_last;} /* * 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, bool reply_begun,			       const char *abort_why){	unsigned int state; 	ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags)); 	write_lock(&call->state_lock); 	state = call->state;	switch (state) {	case RXRPC_CALL_CLIENT_SEND_REQUEST:	case RXRPC_CALL_CLIENT_AWAIT_REPLY:		if (reply_begun)			call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;		else			call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;		break; 	case RXRPC_CALL_SERVER_AWAIT_ACK:		__rxrpc_call_completed(call);		state = call->state;		break; 	default:		goto bad_state;	} 	write_unlock(&call->state_lock);	if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)		trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);	else		trace_rxrpc_transmit(call, rxrpc_transmit_end);	_leave(" = ok");	return true; bad_state:	write_unlock(&call->state_lock);	kdebug("end_tx %s", rxrpc_call_states[call->state]);	rxrpc_proto_abort(abort_why, call, call->tx_top);	return false;} /* * Begin the reply reception phase of a call. */static bool rxrpc_receiving_reply(struct rxrpc_call *call){	struct rxrpc_ack_summary summary = { 0 };	unsigned long now, timo;	rxrpc_seq_t top = READ_ONCE(call->tx_top); 	if (call->ackr_reason) {		spin_lock_bh(&call->lock);		call->ackr_reason = 0;		spin_unlock_bh(&call->lock);		now = jiffies;		timo = now + MAX_JIFFY_OFFSET;		WRITE_ONCE(call->resend_at, timo);		WRITE_ONCE(call->ack_at, timo);		trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);	} 	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {		if (!rxrpc_rotate_tx_window(call, top, &summary)) {			rxrpc_proto_abort("TXL", call, top);			return false;		}	}	if (!rxrpc_end_tx_phase(call, true, "ETD"))		return false;	call->tx_phase = false;	return true;} /* * Scan a data 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_data(struct sk_buff *skb){	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);	unsigned int offset = sizeof(struct rxrpc_wire_header);	unsigned int len = skb->len;	u8 flags = sp->hdr.flags; 	for (;;) {		if (flags & RXRPC_REQUEST_ACK)			__set_bit(sp->nr_subpackets, sp->rx_req_ack);		sp->nr_subpackets++; 		if (!(flags & RXRPC_JUMBO_PACKET))			break; 		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);	} 	if (flags & RXRPC_LAST_PACKET)		sp->rx_flags |= RXRPC_SKB_INCL_LAST;	return true; protocol_error:	return false;} /* * 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). */static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,				 bool is_jumbo, bool *_jumbo_bad){	/* Discard normal packets that are duplicates. */	if (is_jumbo)		return; 	/* 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.	 */	if (!*_jumbo_bad) {		call->nr_jumbo_bad++;		*_jumbo_bad = true;	}} /* * Process a DATA packet, adding the packet to the Rx ring.  The caller's * packet ref must be passed on or discarded. */static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb){	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);	enum rxrpc_call_state state;	unsigned int j, nr_subpackets;	rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;	rxrpc_seq_t seq0 = sp->hdr.seq, hard_ack;	bool immediate_ack = false, jumbo_bad = false;	u8 ack = 0; 	_enter("{%u,%u},{%u,%u}",	       call->rx_hard_ack, call->rx_top, skb->len, seq0); 	_proto("Rx DATA %%%u { #%u f=%02x n=%u }",	       sp->hdr.serial, seq0, sp->hdr.flags, sp->nr_subpackets); 	state = READ_ONCE(call->state);	if (state >= RXRPC_CALL_COMPLETE) {		rxrpc_free_skb(skb, rxrpc_skb_freed);		return;	} 	if (call->state == RXRPC_CALL_SERVER_RECV_REQUEST) {		unsigned long timo = READ_ONCE(call->next_req_timo);		unsigned long now, expect_req_by; 		if (timo) {			now = jiffies;			expect_req_by = now + timo;			WRITE_ONCE(call->expect_req_by, expect_req_by);			rxrpc_reduce_call_timer(call, expect_req_by, now,						rxrpc_timer_set_for_idle);		}	} 	spin_lock(&call->input_lock); 	/* Received data implicitly ACKs all of the request packets we sent	 * when we're acting as a client.	 */	if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||	     state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&	    !rxrpc_receiving_reply(call))		goto unlock; 	call->ackr_prev_seq = seq0;	hard_ack = READ_ONCE(call->rx_hard_ack); 	nr_subpackets = sp->nr_subpackets;	if (nr_subpackets > 1) {		if (call->nr_jumbo_bad > 3) {			ack = RXRPC_ACK_NOSPACE;			ack_serial = serial;			goto ack;		}	} 	for (j = 0; j < nr_subpackets; j++) {		rxrpc_serial_t serial = sp->hdr.serial + j;		rxrpc_seq_t seq = seq0 + j;		unsigned int ix = seq & RXRPC_RXTX_BUFF_MASK;		bool terminal = (j == nr_subpackets - 1);		bool last = terminal && (sp->rx_flags & RXRPC_SKB_INCL_LAST);		u8 flags, annotation = j; 		_proto("Rx DATA+%u %%%u { #%x t=%u l=%u }",		     j, serial, seq, terminal, last); 		if (last) {			if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&			    seq != call->rx_top) {				rxrpc_proto_abort("LSN", call, seq);				goto unlock;			}		} else {			if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&			    after_eq(seq, call->rx_top)) {				rxrpc_proto_abort("LSA", call, seq);				goto unlock;			}		} 		flags = 0;		if (last)			flags |= RXRPC_LAST_PACKET;		if (!terminal)			flags |= RXRPC_JUMBO_PACKET;		if (test_bit(j, sp->rx_req_ack))			flags |= RXRPC_REQUEST_ACK;		trace_rxrpc_rx_data(call->debug_id, seq, serial, flags, annotation);

Lines 1–500 of 1451.