1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet reception
4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include "ar-internal.h"
12 static void rxrpc_proto_abort(const char *why,
13 struct rxrpc_call *call, rxrpc_seq_t seq)
15 if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG)) {
16 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
17 rxrpc_queue_call(call);
22 * Do TCP-style congestion management [RFC 5681].
24 static void rxrpc_congestion_management(struct rxrpc_call *call,
26 struct rxrpc_ack_summary *summary,
27 rxrpc_serial_t acked_serial)
29 enum rxrpc_congest_change change = rxrpc_cong_no_change;
30 unsigned int cumulative_acks = call->cong_cumul_acks;
31 unsigned int cwnd = call->cong_cwnd;
34 summary->flight_size =
35 (call->tx_top - call->tx_hard_ack) - summary->nr_acks;
37 if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
38 summary->retrans_timeo = true;
39 call->cong_ssthresh = max_t(unsigned int,
40 summary->flight_size / 2, 2);
42 if (cwnd >= call->cong_ssthresh &&
43 call->cong_mode == RXRPC_CALL_SLOW_START) {
44 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
45 call->cong_tstamp = skb->tstamp;
50 cumulative_acks += summary->nr_new_acks;
51 cumulative_acks += summary->nr_rot_new_acks;
52 if (cumulative_acks > 255)
53 cumulative_acks = 255;
55 summary->mode = call->cong_mode;
56 summary->cwnd = call->cong_cwnd;
57 summary->ssthresh = call->cong_ssthresh;
58 summary->cumulative_acks = cumulative_acks;
59 summary->dup_acks = call->cong_dup_acks;
61 switch (call->cong_mode) {
62 case RXRPC_CALL_SLOW_START:
63 if (summary->nr_nacks > 0)
64 goto packet_loss_detected;
65 if (summary->cumulative_acks > 0)
67 if (cwnd >= call->cong_ssthresh) {
68 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
69 call->cong_tstamp = skb->tstamp;
73 case RXRPC_CALL_CONGEST_AVOIDANCE:
74 if (summary->nr_nacks > 0)
75 goto packet_loss_detected;
77 /* We analyse the number of packets that get ACK'd per RTT
78 * period and increase the window if we managed to fill it.
80 if (call->peer->rtt_count == 0)
82 if (ktime_before(skb->tstamp,
83 ktime_add_us(call->cong_tstamp,
84 call->peer->srtt_us >> 3)))
86 change = rxrpc_cong_rtt_window_end;
87 call->cong_tstamp = skb->tstamp;
88 if (cumulative_acks >= cwnd)
92 case RXRPC_CALL_PACKET_LOSS:
93 if (summary->nr_nacks == 0)
94 goto resume_normality;
96 if (summary->new_low_nack) {
97 change = rxrpc_cong_new_low_nack;
98 call->cong_dup_acks = 1;
99 if (call->cong_extra > 1)
100 call->cong_extra = 1;
101 goto send_extra_data;
104 call->cong_dup_acks++;
105 if (call->cong_dup_acks < 3)
106 goto send_extra_data;
108 change = rxrpc_cong_begin_retransmission;
109 call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
110 call->cong_ssthresh = max_t(unsigned int,
111 summary->flight_size / 2, 2);
112 cwnd = call->cong_ssthresh + 3;
113 call->cong_extra = 0;
114 call->cong_dup_acks = 0;
118 case RXRPC_CALL_FAST_RETRANSMIT:
119 if (!summary->new_low_nack) {
120 if (summary->nr_new_acks == 0)
122 call->cong_dup_acks++;
123 if (call->cong_dup_acks == 2) {
124 change = rxrpc_cong_retransmit_again;
125 call->cong_dup_acks = 0;
129 change = rxrpc_cong_progress;
130 cwnd = call->cong_ssthresh;
131 if (summary->nr_nacks == 0)
132 goto resume_normality;
142 change = rxrpc_cong_cleared_nacks;
143 call->cong_dup_acks = 0;
144 call->cong_extra = 0;
145 call->cong_tstamp = skb->tstamp;
146 if (cwnd < call->cong_ssthresh)
147 call->cong_mode = RXRPC_CALL_SLOW_START;
149 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
153 if (cwnd >= RXRPC_RXTX_BUFF_SIZE - 1)
154 cwnd = RXRPC_RXTX_BUFF_SIZE - 1;
155 call->cong_cwnd = cwnd;
156 call->cong_cumul_acks = cumulative_acks;
157 trace_rxrpc_congest(call, summary, acked_serial, change);
158 if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
159 rxrpc_queue_call(call);
162 packet_loss_detected:
163 change = rxrpc_cong_saw_nack;
164 call->cong_mode = RXRPC_CALL_PACKET_LOSS;
165 call->cong_dup_acks = 0;
166 goto send_extra_data;
169 /* Send some previously unsent DATA if we have some to advance the ACK
172 if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
173 RXRPC_TX_ANNO_LAST ||
174 summary->nr_acks != call->tx_top - call->tx_hard_ack) {
176 wake_up(&call->waitq);
178 goto out_no_clear_ca;
182 * Apply a hard ACK by advancing the Tx window.
184 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
185 struct rxrpc_ack_summary *summary)
187 struct sk_buff *skb, *list = NULL;
188 bool rot_last = false;
192 if (call->acks_lowest_nak == call->tx_hard_ack) {
193 call->acks_lowest_nak = to;
194 } else if (before_eq(call->acks_lowest_nak, to)) {
195 summary->new_low_nack = true;
196 call->acks_lowest_nak = to;
199 spin_lock(&call->lock);
201 while (before(call->tx_hard_ack, to)) {
203 ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
204 skb = call->rxtx_buffer[ix];
205 annotation = call->rxtx_annotations[ix];
206 rxrpc_see_skb(skb, rxrpc_skb_rotated);
207 call->rxtx_buffer[ix] = NULL;
208 call->rxtx_annotations[ix] = 0;
212 if (annotation & RXRPC_TX_ANNO_LAST) {
213 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
216 if ((annotation & RXRPC_TX_ANNO_MASK) != RXRPC_TX_ANNO_ACK)
217 summary->nr_rot_new_acks++;
220 spin_unlock(&call->lock);
222 trace_rxrpc_transmit(call, (rot_last ?
223 rxrpc_transmit_rotate_last :
224 rxrpc_transmit_rotate));
225 wake_up(&call->waitq);
230 skb_mark_not_on_list(skb);
231 rxrpc_free_skb(skb, rxrpc_skb_freed);
238 * End the transmission phase of a call.
240 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
241 * or a final ACK packet.
243 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
244 const char *abort_why)
248 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
250 write_lock(&call->state_lock);
254 case RXRPC_CALL_CLIENT_SEND_REQUEST:
255 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
257 call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
259 call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
262 case RXRPC_CALL_SERVER_AWAIT_ACK:
263 __rxrpc_call_completed(call);
271 write_unlock(&call->state_lock);
272 if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
273 trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
275 trace_rxrpc_transmit(call, rxrpc_transmit_end);
280 write_unlock(&call->state_lock);
281 kdebug("end_tx %s", rxrpc_call_states[call->state]);
282 rxrpc_proto_abort(abort_why, call, call->tx_top);
287 * Begin the reply reception phase of a call.
289 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
291 struct rxrpc_ack_summary summary = { 0 };
292 unsigned long now, timo;
293 rxrpc_seq_t top = READ_ONCE(call->tx_top);
295 if (call->ackr_reason) {
296 spin_lock_bh(&call->lock);
297 call->ackr_reason = 0;
298 spin_unlock_bh(&call->lock);
300 timo = now + MAX_JIFFY_OFFSET;
301 WRITE_ONCE(call->resend_at, timo);
302 WRITE_ONCE(call->ack_at, timo);
303 trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
306 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
307 if (!rxrpc_rotate_tx_window(call, top, &summary)) {
308 rxrpc_proto_abort("TXL", call, top);
312 return rxrpc_end_tx_phase(call, true, "ETD");
316 * Scan a data packet to validate its structure and to work out how many
317 * subpackets it contains.
319 * A jumbo packet is a collection of consecutive packets glued together with
320 * little headers between that indicate how to change the initial header for
323 * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
324 * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
327 static bool rxrpc_validate_data(struct sk_buff *skb)
329 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
330 unsigned int offset = sizeof(struct rxrpc_wire_header);
331 unsigned int len = skb->len;
332 u8 flags = sp->hdr.flags;
335 if (flags & RXRPC_REQUEST_ACK)
336 __set_bit(sp->nr_subpackets, sp->rx_req_ack);
339 if (!(flags & RXRPC_JUMBO_PACKET))
342 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
344 if (flags & RXRPC_LAST_PACKET)
346 offset += RXRPC_JUMBO_DATALEN;
347 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
349 offset += sizeof(struct rxrpc_jumbo_header);
352 if (flags & RXRPC_LAST_PACKET)
353 sp->rx_flags |= RXRPC_SKB_INCL_LAST;
361 * Handle reception of a duplicate packet.
363 * We have to take care to avoid an attack here whereby we're given a series of
364 * jumbograms, each with a sequence number one before the preceding one and
365 * filled up to maximum UDP size. If they never send us the first packet in
366 * the sequence, they can cause us to have to hold on to around 2MiB of kernel
367 * space until the call times out.
369 * We limit the space usage by only accepting three duplicate jumbo packets per
370 * call. After that, we tell the other side we're no longer accepting jumbos
371 * (that information is encoded in the ACK packet).
373 static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
374 bool is_jumbo, bool *_jumbo_bad)
376 /* Discard normal packets that are duplicates. */
380 /* Skip jumbo subpackets that are duplicates. When we've had three or
381 * more partially duplicate jumbo packets, we refuse to take any more
382 * jumbos for this call.
385 call->nr_jumbo_bad++;
391 * Process a DATA packet, adding the packet to the Rx ring. The caller's
392 * packet ref must be passed on or discarded.
394 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
396 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
397 enum rxrpc_call_state state;
398 unsigned int j, nr_subpackets, nr_unacked = 0;
399 rxrpc_serial_t serial = sp->hdr.serial, ack_serial = serial;
400 rxrpc_seq_t seq0 = sp->hdr.seq, hard_ack;
401 bool jumbo_bad = false;
403 _enter("{%u,%u},{%u,%u}",
404 call->rx_hard_ack, call->rx_top, skb->len, seq0);
406 _proto("Rx DATA %%%u { #%u f=%02x n=%u }",
407 sp->hdr.serial, seq0, sp->hdr.flags, sp->nr_subpackets);
409 state = READ_ONCE(call->state);
410 if (state >= RXRPC_CALL_COMPLETE) {
411 rxrpc_free_skb(skb, rxrpc_skb_freed);
415 if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
416 unsigned long timo = READ_ONCE(call->next_req_timo);
417 unsigned long now, expect_req_by;
421 expect_req_by = now + timo;
422 WRITE_ONCE(call->expect_req_by, expect_req_by);
423 rxrpc_reduce_call_timer(call, expect_req_by, now,
424 rxrpc_timer_set_for_idle);
428 rxrpc_inc_stat(call->rxnet, stat_rx_data);
429 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
430 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
431 if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
432 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
434 spin_lock(&call->input_lock);
436 /* Received data implicitly ACKs all of the request packets we sent
437 * when we're acting as a client.
439 if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
440 state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
441 !rxrpc_receiving_reply(call))
444 hard_ack = READ_ONCE(call->rx_hard_ack);
446 nr_subpackets = sp->nr_subpackets;
447 if (nr_subpackets > 1) {
448 if (call->nr_jumbo_bad > 3) {
449 rxrpc_send_ACK(call, RXRPC_ACK_NOSPACE, serial,
450 rxrpc_propose_ack_input_data);
455 for (j = 0; j < nr_subpackets; j++) {
456 rxrpc_serial_t serial = sp->hdr.serial + j;
457 rxrpc_seq_t seq = seq0 + j;
458 unsigned int ix = seq & RXRPC_RXTX_BUFF_MASK;
459 bool terminal = (j == nr_subpackets - 1);
460 bool last = terminal && (sp->rx_flags & RXRPC_SKB_INCL_LAST);
462 u8 flags, annotation = j;
464 _proto("Rx DATA+%u %%%u { #%x t=%u l=%u }",
465 j, serial, seq, terminal, last);
468 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
469 seq != call->rx_top) {
470 rxrpc_proto_abort("LSN", call, seq);
474 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
475 after_eq(seq, call->rx_top)) {
476 rxrpc_proto_abort("LSA", call, seq);
483 flags |= RXRPC_LAST_PACKET;
485 flags |= RXRPC_JUMBO_PACKET;
486 if (test_bit(j, sp->rx_req_ack))
487 flags |= RXRPC_REQUEST_ACK;
488 trace_rxrpc_rx_data(call->debug_id, seq, serial, flags, annotation);
490 if (before_eq(seq, hard_ack)) {
491 rxrpc_send_ACK(call, RXRPC_ACK_DUPLICATE, serial,
492 rxrpc_propose_ack_input_data);
496 if (call->rxtx_buffer[ix]) {
497 rxrpc_input_dup_data(call, seq, nr_subpackets > 1,
499 rxrpc_send_ACK(call, RXRPC_ACK_DUPLICATE, serial,
500 rxrpc_propose_ack_input_data);
504 if (after(seq, hard_ack + call->rx_winsize)) {
505 rxrpc_send_ACK(call, RXRPC_ACK_EXCEEDS_WINDOW, serial,
506 rxrpc_propose_ack_input_data);
507 if (flags & RXRPC_JUMBO_PACKET) {
509 call->nr_jumbo_bad++;
517 if (flags & RXRPC_REQUEST_ACK) {
518 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, serial,
519 rxrpc_propose_ack_input_data);
523 if (after(seq0, call->ackr_highest_seq))
524 call->ackr_highest_seq = seq0;
526 /* Queue the packet. We use a couple of memory barriers here as need
527 * to make sure that rx_top is perceived to be set after the buffer
528 * pointer and that the buffer pointer is set after the annotation and
531 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
532 * and also rxrpc_fill_out_ack().
535 rxrpc_get_skb(skb, rxrpc_skb_got);
536 call->rxtx_annotations[ix] = annotation;
538 call->rxtx_buffer[ix] = skb;
539 if (after(seq, call->rx_top)) {
540 smp_store_release(&call->rx_top, seq);
541 } else if (before(seq, call->rx_top)) {
542 /* Send an immediate ACK if we fill in a hole */
544 rxrpc_send_ACK(call, RXRPC_ACK_DELAY, serial,
545 rxrpc_propose_ack_input_data);
551 /* From this point on, we're not allowed to touch the
552 * packet any longer as its ref now belongs to the Rx
560 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
561 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
563 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
566 if (after_eq(seq, call->rx_expect_next)) {
567 if (after(seq, call->rx_expect_next)) {
568 _net("OOS %u > %u", seq, call->rx_expect_next);
569 rxrpc_send_ACK(call, RXRPC_ACK_OUT_OF_SEQUENCE, serial,
570 rxrpc_propose_ack_input_data);
573 call->rx_expect_next = seq + 1;
583 if (atomic_add_return(nr_unacked, &call->ackr_nr_unacked) > 2)
584 rxrpc_send_ACK(call, RXRPC_ACK_IDLE, ack_serial,
585 rxrpc_propose_ack_input_data);
587 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, ack_serial,
588 rxrpc_propose_ack_input_data);
590 trace_rxrpc_notify_socket(call->debug_id, serial);
591 rxrpc_notify_socket(call);
593 spin_unlock(&call->input_lock);
594 rxrpc_free_skb(skb, rxrpc_skb_freed);
599 * See if there's a cached RTT probe to complete.
601 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
603 rxrpc_serial_t acked_serial,
604 rxrpc_serial_t ack_serial,
605 enum rxrpc_rtt_rx_trace type)
607 rxrpc_serial_t orig_serial;
610 bool matched = false;
613 avail = READ_ONCE(call->rtt_avail);
614 smp_rmb(); /* Read avail bits before accessing data. */
616 for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
617 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
620 sent_at = call->rtt_sent_at[i];
621 orig_serial = call->rtt_serial[i];
623 if (orig_serial == acked_serial) {
624 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
625 smp_mb(); /* Read data before setting avail bit */
626 set_bit(i, &call->rtt_avail);
627 if (type != rxrpc_rtt_rx_cancel)
628 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
631 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
632 orig_serial, acked_serial, 0, 0);
636 /* If a later serial is being acked, then mark this slot as
639 if (after(acked_serial, orig_serial)) {
640 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
641 orig_serial, acked_serial, 0, 0);
642 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
644 set_bit(i, &call->rtt_avail);
649 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
653 * Process the response to a ping that we sent to find out if we lost an ACK.
655 * If we got back a ping response that indicates a lower tx_top than what we
656 * had at the time of the ping transmission, we adjudge all the DATA packets
657 * sent between the response tx_top and the ping-time tx_top to have been lost.
659 static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
661 rxrpc_seq_t top, bottom, seq;
664 spin_lock_bh(&call->lock);
666 bottom = call->tx_hard_ack + 1;
667 top = call->acks_lost_top;
668 if (before(bottom, top)) {
669 for (seq = bottom; before_eq(seq, top); seq++) {
670 int ix = seq & RXRPC_RXTX_BUFF_MASK;
671 u8 annotation = call->rxtx_annotations[ix];
672 u8 anno_type = annotation & RXRPC_TX_ANNO_MASK;
674 if (anno_type != RXRPC_TX_ANNO_UNACK)
676 annotation &= ~RXRPC_TX_ANNO_MASK;
677 annotation |= RXRPC_TX_ANNO_RETRANS;
678 call->rxtx_annotations[ix] = annotation;
683 spin_unlock_bh(&call->lock);
685 if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
686 rxrpc_queue_call(call);
690 * Process a ping response.
692 static void rxrpc_input_ping_response(struct rxrpc_call *call,
694 rxrpc_serial_t acked_serial,
695 rxrpc_serial_t ack_serial)
697 if (acked_serial == call->acks_lost_ping)
698 rxrpc_input_check_for_lost_ack(call);
702 * Process the extra information that may be appended to an ACK packet
704 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
705 struct rxrpc_ackinfo *ackinfo)
707 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
708 struct rxrpc_peer *peer;
711 u32 rwind = ntohl(ackinfo->rwind);
713 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
715 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
716 rwind, ntohl(ackinfo->jumbo_max));
718 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
719 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
720 if (call->tx_winsize != rwind) {
721 if (rwind > call->tx_winsize)
723 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
724 call->tx_winsize = rwind;
727 if (call->cong_ssthresh > rwind)
728 call->cong_ssthresh = rwind;
730 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
733 if (mtu < peer->maxdata) {
734 spin_lock_bh(&peer->lock);
736 peer->mtu = mtu + peer->hdrsize;
737 spin_unlock_bh(&peer->lock);
738 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
742 wake_up(&call->waitq);
746 * Process individual soft ACKs.
748 * Each ACK in the array corresponds to one packet and can be either an ACK or
749 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
750 * packets that lie beyond the end of the ACK list are scheduled for resend by
751 * the timer on the basis that the peer might just not have processed them at
752 * the time the ACK was sent.
754 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
755 rxrpc_seq_t seq, int nr_acks,
756 struct rxrpc_ack_summary *summary)
759 u8 annotation, anno_type;
761 for (; nr_acks > 0; nr_acks--, seq++) {
762 ix = seq & RXRPC_RXTX_BUFF_MASK;
763 annotation = call->rxtx_annotations[ix];
764 anno_type = annotation & RXRPC_TX_ANNO_MASK;
765 annotation &= ~RXRPC_TX_ANNO_MASK;
767 case RXRPC_ACK_TYPE_ACK:
769 if (anno_type == RXRPC_TX_ANNO_ACK)
771 summary->nr_new_acks++;
772 call->rxtx_annotations[ix] =
773 RXRPC_TX_ANNO_ACK | annotation;
775 case RXRPC_ACK_TYPE_NACK:
776 if (!summary->nr_nacks &&
777 call->acks_lowest_nak != seq) {
778 call->acks_lowest_nak = seq;
779 summary->new_low_nack = true;
782 if (anno_type == RXRPC_TX_ANNO_NAK)
784 summary->nr_new_nacks++;
785 if (anno_type == RXRPC_TX_ANNO_RETRANS)
787 call->rxtx_annotations[ix] =
788 RXRPC_TX_ANNO_NAK | annotation;
791 return rxrpc_proto_abort("SFT", call, 0);
797 * Return true if the ACK is valid - ie. it doesn't appear to have regressed
798 * with respect to the ack state conveyed by preceding ACKs.
800 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
801 rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
803 rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
805 if (after(first_pkt, base))
806 return true; /* The window advanced */
808 if (before(first_pkt, base))
809 return false; /* firstPacket regressed */
811 if (after_eq(prev_pkt, call->acks_prev_seq))
812 return true; /* previousPacket hasn't regressed. */
814 /* Some rx implementations put a serial number in previousPacket. */
815 if (after_eq(prev_pkt, base + call->tx_winsize))
821 * Process an ACK packet.
823 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
824 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
826 * A hard-ACK means that a packet has been processed and may be discarded; a
827 * soft-ACK means that the packet may be discarded and retransmission
828 * requested. A phase is complete when all packets are hard-ACK'd.
830 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
832 struct rxrpc_ack_summary summary = { 0 };
833 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
835 struct rxrpc_ackpacket ack;
836 struct rxrpc_ackinfo info;
837 u8 acks[RXRPC_MAXACKS];
839 rxrpc_serial_t ack_serial, acked_serial;
840 rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
841 int nr_acks, offset, ioffset;
845 offset = sizeof(struct rxrpc_wire_header);
846 if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
847 _debug("extraction failure");
848 return rxrpc_proto_abort("XAK", call, 0);
850 offset += sizeof(buf.ack);
852 ack_serial = sp->hdr.serial;
853 acked_serial = ntohl(buf.ack.serial);
854 first_soft_ack = ntohl(buf.ack.firstPacket);
855 prev_pkt = ntohl(buf.ack.previousPacket);
856 hard_ack = first_soft_ack - 1;
857 nr_acks = buf.ack.nAcks;
858 summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
859 buf.ack.reason : RXRPC_ACK__INVALID);
861 trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
862 first_soft_ack, prev_pkt,
863 summary.ack_reason, nr_acks);
864 rxrpc_inc_stat(call->rxnet, stat_rx_acks[buf.ack.reason]);
866 switch (buf.ack.reason) {
867 case RXRPC_ACK_PING_RESPONSE:
868 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
870 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
871 rxrpc_rtt_rx_ping_response);
873 case RXRPC_ACK_REQUESTED:
874 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
875 rxrpc_rtt_rx_requested_ack);
878 if (acked_serial != 0)
879 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
880 rxrpc_rtt_rx_cancel);
884 if (buf.ack.reason == RXRPC_ACK_PING) {
885 _proto("Rx ACK %%%u PING Request", ack_serial);
886 rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
887 rxrpc_propose_ack_respond_to_ping);
888 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
889 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
890 rxrpc_propose_ack_respond_to_ack);
893 /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
894 * indicates that the client address changed due to NAT. The server
895 * lost the call because it switched to a different peer.
897 if (unlikely(buf.ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
898 first_soft_ack == 1 &&
900 rxrpc_is_client_call(call)) {
901 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
906 /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
907 * indicate a change of address. However, we can retransmit the call
908 * if we still have it buffered to the beginning.
910 if (unlikely(buf.ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
911 first_soft_ack == 1 &&
913 call->tx_hard_ack == 0 &&
914 rxrpc_is_client_call(call)) {
915 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
920 /* Discard any out-of-order or duplicate ACKs (outside lock). */
921 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
922 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
923 first_soft_ack, call->acks_first_seq,
924 prev_pkt, call->acks_prev_seq);
929 ioffset = offset + nr_acks + 3;
930 if (skb->len >= ioffset + sizeof(buf.info) &&
931 skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
932 return rxrpc_proto_abort("XAI", call, 0);
934 spin_lock(&call->input_lock);
936 /* Discard any out-of-order or duplicate ACKs (inside lock). */
937 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
938 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
939 first_soft_ack, call->acks_first_seq,
940 prev_pkt, call->acks_prev_seq);
943 call->acks_latest_ts = skb->tstamp;
945 call->acks_first_seq = first_soft_ack;
946 call->acks_prev_seq = prev_pkt;
948 if (buf.ack.reason != RXRPC_ACK_PING &&
949 after(acked_serial, call->acks_highest_serial))
950 call->acks_highest_serial = acked_serial;
952 /* Parse rwind and mtu sizes if provided. */
954 rxrpc_input_ackinfo(call, skb, &buf.info);
956 if (first_soft_ack == 0) {
957 rxrpc_proto_abort("AK0", call, 0);
961 /* Ignore ACKs unless we are or have just been transmitting. */
962 switch (READ_ONCE(call->state)) {
963 case RXRPC_CALL_CLIENT_SEND_REQUEST:
964 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
965 case RXRPC_CALL_SERVER_SEND_REPLY:
966 case RXRPC_CALL_SERVER_AWAIT_ACK:
972 if (before(hard_ack, call->tx_hard_ack) ||
973 after(hard_ack, call->tx_top)) {
974 rxrpc_proto_abort("AKW", call, 0);
977 if (nr_acks > call->tx_top - hard_ack) {
978 rxrpc_proto_abort("AKN", call, 0);
982 if (after(hard_ack, call->tx_hard_ack)) {
983 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
984 rxrpc_end_tx_phase(call, false, "ETA");
990 if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0) {
991 rxrpc_proto_abort("XSA", call, 0);
994 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
998 if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
999 RXRPC_TX_ANNO_LAST &&
1000 summary.nr_acks == call->tx_top - hard_ack &&
1001 rxrpc_is_client_call(call))
1002 rxrpc_propose_ping(call, ack_serial,
1003 rxrpc_propose_ack_ping_for_lost_reply);
1005 rxrpc_congestion_management(call, skb, &summary, acked_serial);
1007 spin_unlock(&call->input_lock);
1011 * Process an ACKALL packet.
1013 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1015 struct rxrpc_ack_summary summary = { 0 };
1016 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1018 _proto("Rx ACKALL %%%u", sp->hdr.serial);
1020 spin_lock(&call->input_lock);
1022 if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1023 rxrpc_end_tx_phase(call, false, "ETL");
1025 spin_unlock(&call->input_lock);
1029 * Process an ABORT packet directed at a call.
1031 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1033 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1035 u32 abort_code = RX_CALL_DEAD;
1039 if (skb->len >= 4 &&
1040 skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1041 &wtmp, sizeof(wtmp)) >= 0)
1042 abort_code = ntohl(wtmp);
1044 trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
1046 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
1048 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1049 abort_code, -ECONNABORTED);
1053 * Process an incoming call packet.
1055 static void rxrpc_input_call_packet(struct rxrpc_call *call,
1056 struct sk_buff *skb)
1058 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1061 _enter("%p,%p", call, skb);
1063 timo = READ_ONCE(call->next_rx_timo);
1065 unsigned long now = jiffies, expect_rx_by;
1067 expect_rx_by = now + timo;
1068 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1069 rxrpc_reduce_call_timer(call, expect_rx_by, now,
1070 rxrpc_timer_set_for_normal);
1073 switch (sp->hdr.type) {
1074 case RXRPC_PACKET_TYPE_DATA:
1075 rxrpc_input_data(call, skb);
1078 case RXRPC_PACKET_TYPE_ACK:
1079 rxrpc_input_ack(call, skb);
1082 case RXRPC_PACKET_TYPE_BUSY:
1083 _proto("Rx BUSY %%%u", sp->hdr.serial);
1085 /* Just ignore BUSY packets from the server; the retry and
1086 * lifespan timers will take care of business. BUSY packets
1087 * from the client don't make sense.
1091 case RXRPC_PACKET_TYPE_ABORT:
1092 rxrpc_input_abort(call, skb);
1095 case RXRPC_PACKET_TYPE_ACKALL:
1096 rxrpc_input_ackall(call, skb);
1103 rxrpc_free_skb(skb, rxrpc_skb_freed);
1109 * Handle a new service call on a channel implicitly completing the preceding
1110 * call on that channel. This does not apply to client conns.
1112 * TODO: If callNumber > call_id + 1, renegotiate security.
1114 static void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
1115 struct rxrpc_connection *conn,
1116 struct rxrpc_call *call)
1118 switch (READ_ONCE(call->state)) {
1119 case RXRPC_CALL_SERVER_AWAIT_ACK:
1120 rxrpc_call_completed(call);
1122 case RXRPC_CALL_COMPLETE:
1125 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
1126 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
1127 rxrpc_queue_call(call);
1129 trace_rxrpc_improper_term(call);
1133 spin_lock(&rx->incoming_lock);
1134 __rxrpc_disconnect_call(conn, call);
1135 spin_unlock(&rx->incoming_lock);
1139 * post connection-level events to the connection
1140 * - this includes challenges, responses, some aborts and call terminal packet
1143 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
1144 struct sk_buff *skb)
1146 _enter("%p,%p", conn, skb);
1148 skb_queue_tail(&conn->rx_queue, skb);
1149 rxrpc_queue_conn(conn);
1153 * post endpoint-level events to the local endpoint
1154 * - this includes debug and version messages
1156 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
1157 struct sk_buff *skb)
1159 _enter("%p,%p", local, skb);
1161 if (rxrpc_get_local_maybe(local)) {
1162 skb_queue_tail(&local->event_queue, skb);
1163 rxrpc_queue_local(local);
1165 rxrpc_free_skb(skb, rxrpc_skb_freed);
1170 * put a packet up for transport-level abort
1172 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
1174 if (rxrpc_get_local_maybe(local)) {
1175 skb_queue_tail(&local->reject_queue, skb);
1176 rxrpc_queue_local(local);
1178 rxrpc_free_skb(skb, rxrpc_skb_freed);
1183 * Extract the wire header from a packet and translate the byte order.
1186 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
1188 struct rxrpc_wire_header whdr;
1190 /* dig out the RxRPC connection details */
1191 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
1192 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
1193 tracepoint_string("bad_hdr"));
1197 memset(sp, 0, sizeof(*sp));
1198 sp->hdr.epoch = ntohl(whdr.epoch);
1199 sp->hdr.cid = ntohl(whdr.cid);
1200 sp->hdr.callNumber = ntohl(whdr.callNumber);
1201 sp->hdr.seq = ntohl(whdr.seq);
1202 sp->hdr.serial = ntohl(whdr.serial);
1203 sp->hdr.flags = whdr.flags;
1204 sp->hdr.type = whdr.type;
1205 sp->hdr.userStatus = whdr.userStatus;
1206 sp->hdr.securityIndex = whdr.securityIndex;
1207 sp->hdr._rsvd = ntohs(whdr._rsvd);
1208 sp->hdr.serviceId = ntohs(whdr.serviceId);
1213 * handle data received on the local endpoint
1214 * - may be called in interrupt context
1216 * [!] Note that as this is called from the encap_rcv hook, the socket is not
1217 * held locked by the caller and nothing prevents sk_user_data on the UDP from
1218 * being cleared in the middle of processing this function.
1220 * Called with the RCU read lock held from the IP layer via UDP.
1222 int rxrpc_input_packet(struct sock *udp_sk, struct sk_buff *skb)
1224 struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
1225 struct rxrpc_connection *conn;
1226 struct rxrpc_channel *chan;
1227 struct rxrpc_call *call = NULL;
1228 struct rxrpc_skb_priv *sp;
1229 struct rxrpc_peer *peer = NULL;
1230 struct rxrpc_sock *rx = NULL;
1231 unsigned int channel;
1233 _enter("%p", udp_sk);
1235 if (unlikely(!local)) {
1239 if (skb->tstamp == 0)
1240 skb->tstamp = ktime_get_real();
1242 rxrpc_new_skb(skb, rxrpc_skb_received);
1244 skb_pull(skb, sizeof(struct udphdr));
1246 /* The UDP protocol already released all skb resources;
1247 * we are free to add our own data there.
1249 sp = rxrpc_skb(skb);
1251 /* dig out the RxRPC connection details */
1252 if (rxrpc_extract_header(sp, skb) < 0)
1255 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
1257 if ((lose++ & 7) == 7) {
1258 trace_rxrpc_rx_lose(sp);
1259 rxrpc_free_skb(skb, rxrpc_skb_lost);
1264 if (skb->tstamp == 0)
1265 skb->tstamp = ktime_get_real();
1266 trace_rxrpc_rx_packet(sp);
1268 switch (sp->hdr.type) {
1269 case RXRPC_PACKET_TYPE_VERSION:
1270 if (rxrpc_to_client(sp))
1272 rxrpc_post_packet_to_local(local, skb);
1275 case RXRPC_PACKET_TYPE_BUSY:
1276 if (rxrpc_to_server(sp))
1279 case RXRPC_PACKET_TYPE_ACK:
1280 case RXRPC_PACKET_TYPE_ACKALL:
1281 if (sp->hdr.callNumber == 0)
1284 case RXRPC_PACKET_TYPE_ABORT:
1287 case RXRPC_PACKET_TYPE_DATA:
1288 if (sp->hdr.callNumber == 0 ||
1291 if (!rxrpc_validate_data(skb))
1294 /* Unshare the packet so that it can be modified for in-place
1297 if (sp->hdr.securityIndex != 0) {
1298 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
1300 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
1305 rxrpc_eaten_skb(skb, rxrpc_skb_received);
1307 rxrpc_new_skb(skb, rxrpc_skb_unshared);
1308 sp = rxrpc_skb(skb);
1313 case RXRPC_PACKET_TYPE_CHALLENGE:
1314 if (rxrpc_to_server(sp))
1317 case RXRPC_PACKET_TYPE_RESPONSE:
1318 if (rxrpc_to_client(sp))
1322 /* Packet types 9-11 should just be ignored. */
1323 case RXRPC_PACKET_TYPE_PARAMS:
1324 case RXRPC_PACKET_TYPE_10:
1325 case RXRPC_PACKET_TYPE_11:
1329 _proto("Rx Bad Packet Type %u", sp->hdr.type);
1333 if (sp->hdr.serviceId == 0)
1336 if (rxrpc_to_server(sp)) {
1337 /* Weed out packets to services we're not offering. Packets
1338 * that would begin a call are explicitly rejected and the rest
1339 * are just discarded.
1341 rx = rcu_dereference(local->service);
1342 if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
1343 sp->hdr.serviceId != rx->second_service)) {
1344 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
1346 goto unsupported_service;
1351 conn = rxrpc_find_connection_rcu(local, skb, &peer);
1353 if (sp->hdr.securityIndex != conn->security_ix)
1354 goto wrong_security;
1356 if (sp->hdr.serviceId != conn->service_id) {
1359 if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
1361 old_id = cmpxchg(&conn->service_id, conn->params.service_id,
1364 if (old_id != conn->params.service_id &&
1365 old_id != sp->hdr.serviceId)
1369 if (sp->hdr.callNumber == 0) {
1370 /* Connection-level packet */
1371 _debug("CONN %p {%d}", conn, conn->debug_id);
1372 rxrpc_post_packet_to_conn(conn, skb);
1376 if ((int)sp->hdr.serial - (int)conn->hi_serial > 0)
1377 conn->hi_serial = sp->hdr.serial;
1379 /* Call-bound packets are routed by connection channel. */
1380 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
1381 chan = &conn->channels[channel];
1383 /* Ignore really old calls */
1384 if (sp->hdr.callNumber < chan->last_call)
1387 if (sp->hdr.callNumber == chan->last_call) {
1389 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
1392 /* For the previous service call, if completed
1393 * successfully, we discard all further packets.
1395 if (rxrpc_conn_is_service(conn) &&
1396 chan->last_type == RXRPC_PACKET_TYPE_ACK)
1399 /* But otherwise we need to retransmit the final packet
1400 * from data cached in the connection record.
1402 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
1403 trace_rxrpc_rx_data(chan->call_debug_id,
1407 rxrpc_post_packet_to_conn(conn, skb);
1411 call = rcu_dereference(chan->call);
1413 if (sp->hdr.callNumber > chan->call_id) {
1414 if (rxrpc_to_client(sp))
1417 rxrpc_input_implicit_end_call(rx, conn, call);
1422 if (sp->hdr.serviceId != call->service_id)
1423 call->service_id = sp->hdr.serviceId;
1424 if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1425 call->rx_serial = sp->hdr.serial;
1426 if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1427 set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1431 if (!call || refcount_read(&call->ref) == 0) {
1432 if (rxrpc_to_client(sp) ||
1433 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
1435 if (sp->hdr.seq != 1)
1437 call = rxrpc_new_incoming_call(local, rx, skb);
1442 /* Process a call packet; this either discards or passes on the ref
1445 rxrpc_input_call_packet(call, skb);
1449 rxrpc_free_skb(skb, rxrpc_skb_freed);
1451 trace_rxrpc_rx_done(0, 0);
1455 trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1456 RXKADINCONSISTENCY, EBADMSG);
1457 skb->priority = RXKADINCONSISTENCY;
1460 unsupported_service:
1461 trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1462 RX_INVALID_OPERATION, EOPNOTSUPP);
1463 skb->priority = RX_INVALID_OPERATION;
1467 trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1468 RX_PROTOCOL_ERROR, EBADMSG);
1469 goto protocol_error;
1472 trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1473 RX_PROTOCOL_ERROR, EBADMSG);
1475 skb->priority = RX_PROTOCOL_ERROR;
1477 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
1479 trace_rxrpc_rx_done(skb->mark, skb->priority);
1480 rxrpc_reject_packet(local, skb);
1481 _leave(" [badmsg]");