rxrpc: Remove the rxtx ring
[linux-block.git] / net / rxrpc / input.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
17926a79
DH
2/* RxRPC packet reception
3 *
248f219c 4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
17926a79 5 * Written by David Howells (dhowells@redhat.com)
17926a79
DH
6 */
7
9b6d5398
JP
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
17926a79
DH
10#include "ar-internal.h"
11
248f219c
DH
12static void rxrpc_proto_abort(const char *why,
13 struct rxrpc_call *call, rxrpc_seq_t seq)
14{
3a92789a 15 if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG)) {
248f219c
DH
16 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
17 rxrpc_queue_call(call);
18 }
19}
20
57494343
DH
21/*
22 * Do TCP-style congestion management [RFC 5681].
23 */
24static void rxrpc_congestion_management(struct rxrpc_call *call,
25 struct sk_buff *skb,
ed1e8679
DH
26 struct rxrpc_ack_summary *summary,
27 rxrpc_serial_t acked_serial)
57494343
DH
28{
29 enum rxrpc_congest_change change = rxrpc_cong_no_change;
57494343
DH
30 unsigned int cumulative_acks = call->cong_cumul_acks;
31 unsigned int cwnd = call->cong_cwnd;
32 bool resend = false;
33
34 summary->flight_size =
a4ea4c47 35 (call->tx_top - call->acks_hard_ack) - summary->nr_acks;
57494343
DH
36
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);
41 cwnd = 1;
8782def2 42 if (cwnd >= call->cong_ssthresh &&
57494343
DH
43 call->cong_mode == RXRPC_CALL_SLOW_START) {
44 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
45 call->cong_tstamp = skb->tstamp;
46 cumulative_acks = 0;
47 }
48 }
49
50 cumulative_acks += summary->nr_new_acks;
51 cumulative_acks += summary->nr_rot_new_acks;
52 if (cumulative_acks > 255)
53 cumulative_acks = 255;
54
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;
60
61 switch (call->cong_mode) {
62 case RXRPC_CALL_SLOW_START:
d57a3a15 63 if (summary->saw_nacks)
57494343
DH
64 goto packet_loss_detected;
65 if (summary->cumulative_acks > 0)
66 cwnd += 1;
8782def2 67 if (cwnd >= call->cong_ssthresh) {
57494343
DH
68 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
69 call->cong_tstamp = skb->tstamp;
70 }
71 goto out;
72
73 case RXRPC_CALL_CONGEST_AVOIDANCE:
d57a3a15 74 if (summary->saw_nacks)
57494343
DH
75 goto packet_loss_detected;
76
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.
79 */
c410bf01 80 if (call->peer->rtt_count == 0)
57494343
DH
81 goto out;
82 if (ktime_before(skb->tstamp,
c410bf01
DH
83 ktime_add_us(call->cong_tstamp,
84 call->peer->srtt_us >> 3)))
57494343
DH
85 goto out_no_clear_ca;
86 change = rxrpc_cong_rtt_window_end;
87 call->cong_tstamp = skb->tstamp;
88 if (cumulative_acks >= cwnd)
89 cwnd++;
90 goto out;
91
92 case RXRPC_CALL_PACKET_LOSS:
d57a3a15 93 if (!summary->saw_nacks)
57494343
DH
94 goto resume_normality;
95
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;
102 }
103
104 call->cong_dup_acks++;
105 if (call->cong_dup_acks < 3)
106 goto send_extra_data;
107
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;
115 resend = true;
116 goto out;
117
118 case RXRPC_CALL_FAST_RETRANSMIT:
119 if (!summary->new_low_nack) {
120 if (summary->nr_new_acks == 0)
121 cwnd += 1;
122 call->cong_dup_acks++;
123 if (call->cong_dup_acks == 2) {
124 change = rxrpc_cong_retransmit_again;
125 call->cong_dup_acks = 0;
126 resend = true;
127 }
128 } else {
129 change = rxrpc_cong_progress;
130 cwnd = call->cong_ssthresh;
d57a3a15 131 if (!summary->saw_nacks)
57494343
DH
132 goto resume_normality;
133 }
134 goto out;
135
136 default:
137 BUG();
138 goto out;
139 }
140
141resume_normality:
142 change = rxrpc_cong_cleared_nacks;
143 call->cong_dup_acks = 0;
144 call->cong_extra = 0;
145 call->cong_tstamp = skb->tstamp;
8782def2 146 if (cwnd < call->cong_ssthresh)
57494343
DH
147 call->cong_mode = RXRPC_CALL_SLOW_START;
148 else
149 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
150out:
151 cumulative_acks = 0;
152out_no_clear_ca:
a4ea4c47
DH
153 if (cwnd >= RXRPC_TX_MAX_WINDOW)
154 cwnd = RXRPC_TX_MAX_WINDOW;
57494343
DH
155 call->cong_cwnd = cwnd;
156 call->cong_cumul_acks = cumulative_acks;
ed1e8679 157 trace_rxrpc_congest(call, summary, acked_serial, change);
57494343
DH
158 if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
159 rxrpc_queue_call(call);
160 return;
161
162packet_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;
167
168send_extra_data:
169 /* Send some previously unsent DATA if we have some to advance the ACK
170 * state.
171 */
a4ea4c47
DH
172 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
173 summary->nr_acks != call->tx_top - call->acks_hard_ack) {
57494343
DH
174 call->cong_extra++;
175 wake_up(&call->waitq);
176 }
177 goto out_no_clear_ca;
178}
179
17926a79 180/*
248f219c 181 * Apply a hard ACK by advancing the Tx window.
17926a79 182 */
c479d5f2 183static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
31a1b989 184 struct rxrpc_ack_summary *summary)
17926a79 185{
a4ea4c47 186 struct rxrpc_txbuf *txb;
c479d5f2 187 bool rot_last = false;
17926a79 188
a4ea4c47
DH
189 list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
190 if (before_eq(txb->seq, call->acks_hard_ack))
191 continue;
d57a3a15 192 summary->nr_rot_new_acks++;
a4ea4c47 193 if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
70790dbe 194 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
c479d5f2
DH
195 rot_last = true;
196 }
a4ea4c47
DH
197 if (txb->seq == to)
198 break;
248f219c 199 }
17926a79 200
a4ea4c47
DH
201 if (rot_last)
202 set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
17926a79 203
a4ea4c47 204 _enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
bc4abfcf 205
a4ea4c47
DH
206 if (call->acks_lowest_nak == call->acks_hard_ack) {
207 call->acks_lowest_nak = to;
208 } else if (before_eq(call->acks_lowest_nak, to)) {
209 summary->new_low_nack = true;
210 call->acks_lowest_nak = to;
17926a79 211 }
c479d5f2 212
a4ea4c47
DH
213 smp_store_release(&call->acks_hard_ack, to);
214
215 trace_rxrpc_txqueue(call, (rot_last ?
216 rxrpc_txqueue_rotate_last :
217 rxrpc_txqueue_rotate));
218 wake_up(&call->waitq);
c479d5f2 219 return rot_last;
248f219c 220}
17926a79 221
248f219c
DH
222/*
223 * End the transmission phase of a call.
224 *
225 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
226 * or a final ACK packet.
227 */
70790dbe
DH
228static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
229 const char *abort_why)
248f219c 230{
dfe99522 231 unsigned int state;
17926a79 232
70790dbe 233 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
17926a79 234
248f219c 235 write_lock(&call->state_lock);
651350d1 236
dfe99522
DH
237 state = call->state;
238 switch (state) {
70790dbe 239 case RXRPC_CALL_CLIENT_SEND_REQUEST:
248f219c 240 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
70790dbe 241 if (reply_begun)
dfe99522 242 call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
70790dbe 243 else
dfe99522 244 call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
248f219c 245 break;
70790dbe 246
248f219c
DH
247 case RXRPC_CALL_SERVER_AWAIT_ACK:
248 __rxrpc_call_completed(call);
dfe99522 249 state = call->state;
248f219c 250 break;
70790dbe
DH
251
252 default:
253 goto bad_state;
17926a79 254 }
17926a79 255
248f219c 256 write_unlock(&call->state_lock);
dfe99522 257 if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
a4ea4c47 258 trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
dfe99522 259 else
a4ea4c47 260 trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
248f219c
DH
261 _leave(" = ok");
262 return true;
70790dbe
DH
263
264bad_state:
265 write_unlock(&call->state_lock);
266 kdebug("end_tx %s", rxrpc_call_states[call->state]);
267 rxrpc_proto_abort(abort_why, call, call->tx_top);
268 return false;
269}
270
271/*
272 * Begin the reply reception phase of a call.
273 */
274static bool rxrpc_receiving_reply(struct rxrpc_call *call)
275{
31a1b989 276 struct rxrpc_ack_summary summary = { 0 };
a158bdd3 277 unsigned long now, timo;
70790dbe
DH
278 rxrpc_seq_t top = READ_ONCE(call->tx_top);
279
dd7c1ee5 280 if (call->ackr_reason) {
a158bdd3
DH
281 now = jiffies;
282 timo = now + MAX_JIFFY_OFFSET;
283 WRITE_ONCE(call->resend_at, timo);
530403d9 284 WRITE_ONCE(call->delay_ack_at, timo);
a158bdd3 285 trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
dd7c1ee5
DH
286 }
287
70790dbe 288 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
c479d5f2
DH
289 if (!rxrpc_rotate_tx_window(call, top, &summary)) {
290 rxrpc_proto_abort("TXL", call, top);
291 return false;
292 }
70790dbe 293 }
a11e6ff9 294 return rxrpc_end_tx_phase(call, true, "ETD");
248f219c
DH
295}
296
5d7edbc9
DH
297static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
298 rxrpc_seq_t window, rxrpc_seq_t wtop)
299{
300 atomic64_set_release(&call->ackr_window, ((u64)wtop) << 32 | window);
301}
302
248f219c 303/*
5d7edbc9
DH
304 * Push a DATA packet onto the Rx queue.
305 */
306static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
307 rxrpc_seq_t window, rxrpc_seq_t wtop,
308 enum rxrpc_receive_trace why)
309{
310 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
311 bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
312
313 __skb_queue_tail(&call->recvmsg_queue, skb);
314 rxrpc_input_update_ack_window(call, window, wtop);
315
316 trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
317}
318
319/*
320 * Process a DATA packet.
248f219c 321 */
d4d02d8b 322static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb)
248f219c
DH
323{
324 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
5d7edbc9 325 struct sk_buff *oos;
d4d02d8b 326 rxrpc_serial_t serial = sp->hdr.serial;
5d7edbc9
DH
327 u64 win = atomic64_read(&call->ackr_window);
328 rxrpc_seq_t window = lower_32_bits(win);
329 rxrpc_seq_t wtop = upper_32_bits(win);
330 rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
331 rxrpc_seq_t seq = sp->hdr.seq;
d4d02d8b 332 bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
5d7edbc9 333 int ack_reason = -1;
248f219c 334
d4d02d8b
DH
335 rxrpc_inc_stat(call->rxnet, stat_rx_data);
336 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
337 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
338 if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
339 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
c3c9e3df 340
d4d02d8b 341 if (last) {
5d7edbc9
DH
342 if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
343 seq + 1 != wtop) {
d4d02d8b 344 rxrpc_proto_abort("LSN", call, seq);
5d7edbc9 345 goto err_free;
d4d02d8b
DH
346 }
347 } else {
348 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
5d7edbc9
DH
349 after_eq(seq, wtop)) {
350 pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
351 call->debug_id, seq, window, wtop, wlimit);
d4d02d8b 352 rxrpc_proto_abort("LSA", call, seq);
5d7edbc9 353 goto err_free;
d4d02d8b 354 }
c3c9e3df 355 }
248f219c 356
5d7edbc9
DH
357 if (after(seq, call->rx_highest_seq))
358 call->rx_highest_seq = seq;
359
d4d02d8b 360 trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
17926a79 361
5d7edbc9
DH
362 if (before(seq, window)) {
363 ack_reason = RXRPC_ACK_DUPLICATE;
364 goto send_ack;
d4d02d8b 365 }
5d7edbc9
DH
366 if (after(seq, wlimit)) {
367 ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
368 goto send_ack;
d4d02d8b
DH
369 }
370
5d7edbc9
DH
371 /* Queue the packet. */
372 if (seq == window) {
373 rxrpc_seq_t reset_from;
374 bool reset_sack = false;
d4d02d8b 375
5d7edbc9
DH
376 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
377 ack_reason = RXRPC_ACK_REQUESTED;
378 /* Send an immediate ACK if we fill in a hole */
379 else if (!skb_queue_empty(&call->rx_oos_queue))
380 ack_reason = RXRPC_ACK_DELAY;
d4d02d8b 381
5d7edbc9
DH
382 window++;
383 if (after(window, wtop))
384 wtop = window;
d4d02d8b 385
5d7edbc9
DH
386 spin_lock(&call->recvmsg_queue.lock);
387 rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
388 skb = NULL;
389
390 while ((oos = skb_peek(&call->rx_oos_queue))) {
391 struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
392
393 if (after(osp->hdr.seq, window))
394 break;
395
396 __skb_unlink(oos, &call->rx_oos_queue);
397 last = osp->hdr.flags & RXRPC_LAST_PACKET;
398 seq = osp->hdr.seq;
399 if (!reset_sack) {
400 reset_from = seq;
401 reset_sack = true;
402 }
403
404 window++;
405 rxrpc_input_queue_data(call, oos, window, wtop,
406 rxrpc_receive_queue_oos);
d4d02d8b 407 }
d4d02d8b 408
5d7edbc9 409 spin_unlock(&call->recvmsg_queue.lock);
d4d02d8b 410
5d7edbc9
DH
411 if (reset_sack) {
412 do {
413 call->ackr_sack_table[reset_from % RXRPC_SACK_SIZE] = 0;
414 } while (reset_from++, before(reset_from, window));
415 }
d4d02d8b 416 } else {
5d7edbc9 417 bool keep = false;
d4d02d8b 418
5d7edbc9
DH
419 ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
420
421 if (!call->ackr_sack_table[seq % RXRPC_SACK_SIZE]) {
422 call->ackr_sack_table[seq % RXRPC_SACK_SIZE] = 1;
423 keep = 1;
424 }
425
426 if (after(seq + 1, wtop)) {
427 wtop = seq + 1;
428 rxrpc_input_update_ack_window(call, window, wtop);
429 }
430
431 if (!keep) {
432 ack_reason = RXRPC_ACK_DUPLICATE;
433 goto send_ack;
434 }
435
436 skb_queue_walk(&call->rx_oos_queue, oos) {
437 struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
438
439 if (after(osp->hdr.seq, seq)) {
440 __skb_queue_before(&call->rx_oos_queue, oos, skb);
441 goto oos_queued;
442 }
d4d02d8b 443 }
5d7edbc9
DH
444
445 __skb_queue_tail(&call->rx_oos_queue, skb);
446 oos_queued:
447 trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
448 sp->hdr.serial, sp->hdr.seq);
449 skb = NULL;
d4d02d8b
DH
450 }
451
5d7edbc9
DH
452send_ack:
453 if (ack_reason < 0 &&
454 atomic_inc_return(&call->ackr_nr_unacked) > 2 &&
455 test_and_set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags)) {
456 ack_reason = RXRPC_ACK_IDLE;
457 } else if (ack_reason >= 0) {
458 set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
459 }
460
461 if (ack_reason >= 0)
462 rxrpc_send_ACK(call, ack_reason, serial,
d4d02d8b
DH
463 rxrpc_propose_ack_input_data);
464 else
465 rxrpc_propose_delay_ACK(call, serial,
466 rxrpc_propose_ack_input_data);
467
5d7edbc9 468err_free:
d4d02d8b 469 rxrpc_free_skb(skb, rxrpc_skb_freed);
17926a79
DH
470}
471
472/*
d4d02d8b 473 * Split a jumbo packet and file the bits separately.
17926a79 474 */
d4d02d8b 475static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
17926a79 476{
d4d02d8b
DH
477 struct rxrpc_jumbo_header jhdr;
478 struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
479 struct sk_buff *jskb;
480 unsigned int offset = sizeof(struct rxrpc_wire_header);
481 unsigned int len = skb->len - offset;
17926a79 482
d4d02d8b
DH
483 while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
484 if (len < RXRPC_JUMBO_SUBPKTLEN)
485 goto protocol_error;
486 if (sp->hdr.flags & RXRPC_LAST_PACKET)
487 goto protocol_error;
488 if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
489 &jhdr, sizeof(jhdr)) < 0)
490 goto protocol_error;
491
492 jskb = skb_clone(skb, GFP_ATOMIC);
493 if (!jskb) {
494 kdebug("couldn't clone");
495 return false;
496 }
497 rxrpc_new_skb(jskb, rxrpc_skb_cloned_jumbo);
498 jsp = rxrpc_skb(jskb);
499 jsp->offset = offset;
500 jsp->len = RXRPC_JUMBO_DATALEN;
501 rxrpc_input_data_one(call, jskb);
502
503 sp->hdr.flags = jhdr.flags;
504 sp->hdr._rsvd = ntohs(jhdr._rsvd);
505 sp->hdr.seq++;
506 sp->hdr.serial++;
507 offset += RXRPC_JUMBO_SUBPKTLEN;
508 len -= RXRPC_JUMBO_SUBPKTLEN;
248f219c 509 }
d4d02d8b
DH
510
511 sp->offset = offset;
512 sp->len = len;
513 rxrpc_input_data_one(call, skb);
514 return true;
515
516protocol_error:
517 return false;
248f219c 518}
17926a79 519
248f219c 520/*
4858e403
DH
521 * Process a DATA packet, adding the packet to the Rx ring. The caller's
522 * packet ref must be passed on or discarded.
248f219c 523 */
e8c3af6b 524static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
248f219c
DH
525{
526 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
146d8fef 527 enum rxrpc_call_state state;
d4d02d8b
DH
528 rxrpc_serial_t serial = sp->hdr.serial;
529 rxrpc_seq_t seq0 = sp->hdr.seq;
17926a79 530
5d7edbc9
DH
531 _enter("{%llx,%x},{%u,%x}",
532 atomic64_read(&call->ackr_window), call->rx_highest_seq,
533 skb->len, seq0);
17926a79 534
d4d02d8b
DH
535 _proto("Rx DATA %%%u { #%u f=%02x }",
536 sp->hdr.serial, seq0, sp->hdr.flags);
17926a79 537
146d8fef 538 state = READ_ONCE(call->state);
4858e403 539 if (state >= RXRPC_CALL_COMPLETE) {
987db9f7 540 rxrpc_free_skb(skb, rxrpc_skb_freed);
248f219c 541 return;
4858e403 542 }
17926a79 543
d4d02d8b
DH
544 /* Unshare the packet so that it can be modified for in-place
545 * decryption.
546 */
547 if (sp->hdr.securityIndex != 0) {
548 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
549 if (!nskb) {
550 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
551 return;
552 }
553
554 if (nskb != skb) {
555 rxrpc_eaten_skb(skb, rxrpc_skb_received);
556 skb = nskb;
557 rxrpc_new_skb(skb, rxrpc_skb_unshared);
558 sp = rxrpc_skb(skb);
559 }
560 }
561
a95d25dd 562 if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
a158bdd3
DH
563 unsigned long timo = READ_ONCE(call->next_req_timo);
564 unsigned long now, expect_req_by;
565
566 if (timo) {
567 now = jiffies;
568 expect_req_by = now + timo;
569 WRITE_ONCE(call->expect_req_by, expect_req_by);
570 rxrpc_reduce_call_timer(call, expect_req_by, now,
571 rxrpc_timer_set_for_idle);
572 }
573 }
574
c1e15b49
DH
575 spin_lock(&call->input_lock);
576
248f219c
DH
577 /* Received data implicitly ACKs all of the request packets we sent
578 * when we're acting as a client.
579 */
146d8fef
DH
580 if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
581 state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
70790dbe 582 !rxrpc_receiving_reply(call))
d4d02d8b 583 goto out;
72f0c6fb 584
d4d02d8b
DH
585 if (!rxrpc_input_split_jumbo(call, skb)) {
586 rxrpc_proto_abort("VLD", call, sp->hdr.seq);
587 goto out;
248f219c 588 }
d4d02d8b 589 skb = NULL;
17926a79 590
d4d02d8b 591out:
f71dbf2f
DH
592 trace_rxrpc_notify_socket(call->debug_id, serial);
593 rxrpc_notify_socket(call);
c1e15b49 594
c1e15b49 595 spin_unlock(&call->input_lock);
987db9f7 596 rxrpc_free_skb(skb, rxrpc_skb_freed);
248f219c 597 _leave(" [queued]");
17926a79
DH
598}
599
50235c4b 600/*
4700c4d8 601 * See if there's a cached RTT probe to complete.
50235c4b 602 */
4700c4d8
DH
603static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
604 ktime_t resp_time,
605 rxrpc_serial_t acked_serial,
606 rxrpc_serial_t ack_serial,
607 enum rxrpc_rtt_rx_trace type)
50235c4b 608{
4700c4d8
DH
609 rxrpc_serial_t orig_serial;
610 unsigned long avail;
50235c4b 611 ktime_t sent_at;
4700c4d8
DH
612 bool matched = false;
613 int i;
50235c4b 614
4700c4d8
DH
615 avail = READ_ONCE(call->rtt_avail);
616 smp_rmb(); /* Read avail bits before accessing data. */
50235c4b 617
4700c4d8
DH
618 for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
619 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
50235c4b 620 continue;
b604dd98 621
4700c4d8
DH
622 sent_at = call->rtt_sent_at[i];
623 orig_serial = call->rtt_serial[i];
624
625 if (orig_serial == acked_serial) {
626 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
627 smp_mb(); /* Read data before setting avail bit */
628 set_bit(i, &call->rtt_avail);
629 if (type != rxrpc_rtt_rx_cancel)
630 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
631 sent_at, resp_time);
632 else
633 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
634 orig_serial, acked_serial, 0, 0);
635 matched = true;
636 }
637
638 /* If a later serial is being acked, then mark this slot as
639 * being available.
640 */
641 if (after(acked_serial, orig_serial)) {
642 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
643 orig_serial, acked_serial, 0, 0);
644 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
645 smp_wmb();
646 set_bit(i, &call->rtt_avail);
647 }
648 }
50235c4b 649
4700c4d8
DH
650 if (!matched)
651 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
50235c4b
DH
652}
653
bd1fdf8c
DH
654/*
655 * Process the response to a ping that we sent to find out if we lost an ACK.
656 *
657 * If we got back a ping response that indicates a lower tx_top than what we
658 * had at the time of the ping transmission, we adjudge all the DATA packets
659 * sent between the response tx_top and the ping-time tx_top to have been lost.
660 */
661static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
662{
d57a3a15
DH
663 if (after(call->acks_lost_top, call->acks_prev_seq) &&
664 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
bd1fdf8c
DH
665 rxrpc_queue_call(call);
666}
667
8e83134d
DH
668/*
669 * Process a ping response.
670 */
671static void rxrpc_input_ping_response(struct rxrpc_call *call,
672 ktime_t resp_time,
4700c4d8 673 rxrpc_serial_t acked_serial,
8e83134d
DH
674 rxrpc_serial_t ack_serial)
675{
4700c4d8 676 if (acked_serial == call->acks_lost_ping)
bd1fdf8c 677 rxrpc_input_check_for_lost_ack(call);
8e83134d
DH
678}
679
17926a79 680/*
248f219c 681 * Process the extra information that may be appended to an ACK packet
17926a79 682 */
248f219c
DH
683static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
684 struct rxrpc_ackinfo *ackinfo)
17926a79 685{
248f219c
DH
686 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
687 struct rxrpc_peer *peer;
688 unsigned int mtu;
702f2ac8 689 bool wake = false;
01fd0742 690 u32 rwind = ntohl(ackinfo->rwind);
248f219c
DH
691
692 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
693 sp->hdr.serial,
694 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
01fd0742 695 rwind, ntohl(ackinfo->jumbo_max));
248f219c 696
a4ea4c47
DH
697 if (rwind > RXRPC_TX_MAX_WINDOW)
698 rwind = RXRPC_TX_MAX_WINDOW;
702f2ac8 699 if (call->tx_winsize != rwind) {
702f2ac8
DH
700 if (rwind > call->tx_winsize)
701 wake = true;
a2ad7c21 702 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
702f2ac8
DH
703 call->tx_winsize = rwind;
704 }
705
08511150
DH
706 if (call->cong_ssthresh > rwind)
707 call->cong_ssthresh = rwind;
248f219c
DH
708
709 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
710
711 peer = call->peer;
712 if (mtu < peer->maxdata) {
713 spin_lock_bh(&peer->lock);
714 peer->maxdata = mtu;
715 peer->mtu = mtu + peer->hdrsize;
716 spin_unlock_bh(&peer->lock);
717 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
718 }
702f2ac8
DH
719
720 if (wake)
721 wake_up(&call->waitq);
248f219c 722}
17926a79 723
248f219c
DH
724/*
725 * Process individual soft ACKs.
726 *
727 * Each ACK in the array corresponds to one packet and can be either an ACK or
728 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
729 * packets that lie beyond the end of the ACK list are scheduled for resend by
730 * the timer on the basis that the peer might just not have processed them at
731 * the time the ACK was sent.
732 */
733static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
31a1b989
DH
734 rxrpc_seq_t seq, int nr_acks,
735 struct rxrpc_ack_summary *summary)
248f219c 736{
d57a3a15 737 unsigned int i;
a4ea4c47 738
d57a3a15
DH
739 for (i = 0; i < nr_acks; i++) {
740 if (acks[i] == RXRPC_ACK_TYPE_ACK) {
31a1b989 741 summary->nr_acks++;
31a1b989 742 summary->nr_new_acks++;
d57a3a15
DH
743 } else {
744 if (!summary->saw_nacks &&
745 call->acks_lowest_nak != seq + i) {
746 call->acks_lowest_nak = seq + i;
31a1b989
DH
747 summary->new_low_nack = true;
748 }
d57a3a15 749 summary->saw_nacks = true;
17926a79 750 }
17926a79
DH
751 }
752}
753
441fdee1
DH
754/*
755 * Return true if the ACK is valid - ie. it doesn't appear to have regressed
756 * with respect to the ack state conveyed by preceding ACKs.
757 */
758static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
759 rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
760{
8940ba3c 761 rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
441fdee1
DH
762
763 if (after(first_pkt, base))
764 return true; /* The window advanced */
765
766 if (before(first_pkt, base))
767 return false; /* firstPacket regressed */
768
8940ba3c 769 if (after_eq(prev_pkt, call->acks_prev_seq))
441fdee1
DH
770 return true; /* previousPacket hasn't regressed. */
771
772 /* Some rx implementations put a serial number in previousPacket. */
773 if (after_eq(prev_pkt, base + call->tx_winsize))
774 return false;
775 return true;
776}
777
17926a79 778/*
248f219c
DH
779 * Process an ACK packet.
780 *
781 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
782 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
783 *
784 * A hard-ACK means that a packet has been processed and may be discarded; a
785 * soft-ACK means that the packet may be discarded and retransmission
786 * requested. A phase is complete when all packets are hard-ACK'd.
17926a79 787 */
e8c3af6b 788static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
17926a79 789{
31a1b989 790 struct rxrpc_ack_summary summary = { 0 };
d57a3a15 791 struct rxrpc_ackpacket ack;
17926a79 792 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
d57a3a15
DH
793 struct rxrpc_ackinfo info;
794 struct sk_buff *skb_old = NULL, *skb_put = skb;
68528d93 795 rxrpc_serial_t ack_serial, acked_serial;
1a2391c3 796 rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
775e5b71 797 int nr_acks, offset, ioffset;
248f219c
DH
798
799 _enter("");
800
775e5b71 801 offset = sizeof(struct rxrpc_wire_header);
d57a3a15
DH
802 if (skb_copy_bits(skb, offset, &ack, sizeof(ack)) < 0) {
803 rxrpc_proto_abort("XAK", call, 0);
804 goto out_not_locked;
17926a79 805 }
d57a3a15 806 offset += sizeof(ack);
248f219c 807
68528d93 808 ack_serial = sp->hdr.serial;
d57a3a15
DH
809 acked_serial = ntohl(ack.serial);
810 first_soft_ack = ntohl(ack.firstPacket);
811 prev_pkt = ntohl(ack.previousPacket);
248f219c 812 hard_ack = first_soft_ack - 1;
d57a3a15
DH
813 nr_acks = ack.nAcks;
814 summary.ack_reason = (ack.reason < RXRPC_ACK__INVALID ?
815 ack.reason : RXRPC_ACK__INVALID);
248f219c 816
68528d93 817 trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
1a2391c3 818 first_soft_ack, prev_pkt,
b1d9f7fd 819 summary.ack_reason, nr_acks);
d57a3a15 820 rxrpc_inc_stat(call->rxnet, stat_rx_acks[ack.reason]);
ec71eb9a 821
d57a3a15 822 switch (ack.reason) {
4700c4d8 823 case RXRPC_ACK_PING_RESPONSE:
4700c4d8
DH
824 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
825 rxrpc_rtt_rx_ping_response);
826 break;
827 case RXRPC_ACK_REQUESTED:
828 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
829 rxrpc_rtt_rx_requested_ack);
830 break;
831 default:
832 if (acked_serial != 0)
833 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
834 rxrpc_rtt_rx_cancel);
835 break;
836 }
8e83134d 837
d57a3a15 838 if (ack.reason == RXRPC_ACK_PING) {
68528d93 839 _proto("Rx ACK %%%u PING Request", ack_serial);
72f0c6fb
DH
840 rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
841 rxrpc_propose_ack_respond_to_ping);
248f219c 842 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
72f0c6fb
DH
843 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
844 rxrpc_propose_ack_respond_to_ack);
17926a79
DH
845 }
846
adc9613f
DH
847 /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
848 * indicates that the client address changed due to NAT. The server
849 * lost the call because it switched to a different peer.
850 */
d57a3a15 851 if (unlikely(ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
adc9613f
DH
852 first_soft_ack == 1 &&
853 prev_pkt == 0 &&
854 rxrpc_is_client_call(call)) {
855 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
856 0, -ENETRESET);
857 return;
858 }
859
860 /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
861 * indicate a change of address. However, we can retransmit the call
862 * if we still have it buffered to the beginning.
863 */
d57a3a15 864 if (unlikely(ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
adc9613f
DH
865 first_soft_ack == 1 &&
866 prev_pkt == 0 &&
a4ea4c47 867 call->acks_hard_ack == 0 &&
adc9613f
DH
868 rxrpc_is_client_call(call)) {
869 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
870 0, -ENETRESET);
871 return;
872 }
873
1a2391c3 874 /* Discard any out-of-order or duplicate ACKs (outside lock). */
441fdee1 875 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
68528d93 876 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
8940ba3c
DH
877 first_soft_ack, call->acks_first_seq,
878 prev_pkt, call->acks_prev_seq);
d57a3a15 879 goto out_not_locked;
d1f12947 880 }
c1e15b49 881
d57a3a15 882 info.rxMTU = 0;
775e5b71 883 ioffset = offset + nr_acks + 3;
d57a3a15
DH
884 if (skb->len >= ioffset + sizeof(info) &&
885 skb_copy_bits(skb, ioffset, &info, sizeof(info)) < 0) {
886 rxrpc_proto_abort("XAI", call, 0);
887 goto out_not_locked;
888 }
889
890 if (nr_acks > 0)
891 skb_condense(skb);
c1e15b49
DH
892
893 spin_lock(&call->input_lock);
894
1a2391c3 895 /* Discard any out-of-order or duplicate ACKs (inside lock). */
441fdee1 896 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
68528d93 897 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
8940ba3c
DH
898 first_soft_ack, call->acks_first_seq,
899 prev_pkt, call->acks_prev_seq);
c1e15b49 900 goto out;
d1f12947 901 }
298bc15b 902 call->acks_latest_ts = skb->tstamp;
298bc15b 903
8940ba3c
DH
904 call->acks_first_seq = first_soft_ack;
905 call->acks_prev_seq = prev_pkt;
1a2391c3 906
d57a3a15
DH
907 switch (ack.reason) {
908 case RXRPC_ACK_PING:
909 break;
910 case RXRPC_ACK_PING_RESPONSE:
911 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
912 ack_serial);
913 fallthrough;
914 default:
915 if (after(acked_serial, call->acks_highest_serial))
916 call->acks_highest_serial = acked_serial;
917 break;
918 }
589a0c1e 919
298bc15b 920 /* Parse rwind and mtu sizes if provided. */
d57a3a15
DH
921 if (info.rxMTU)
922 rxrpc_input_ackinfo(call, skb, &info);
17926a79 923
c1e15b49
DH
924 if (first_soft_ack == 0) {
925 rxrpc_proto_abort("AK0", call, 0);
926 goto out;
927 }
17926a79 928
248f219c 929 /* Ignore ACKs unless we are or have just been transmitting. */
146d8fef 930 switch (READ_ONCE(call->state)) {
248f219c
DH
931 case RXRPC_CALL_CLIENT_SEND_REQUEST:
932 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
933 case RXRPC_CALL_SERVER_SEND_REPLY:
934 case RXRPC_CALL_SERVER_AWAIT_ACK:
935 break;
17926a79 936 default:
c1e15b49 937 goto out;
248f219c 938 }
17926a79 939
a4ea4c47 940 if (before(hard_ack, call->acks_hard_ack) ||
c1e15b49
DH
941 after(hard_ack, call->tx_top)) {
942 rxrpc_proto_abort("AKW", call, 0);
943 goto out;
944 }
945 if (nr_acks > call->tx_top - hard_ack) {
946 rxrpc_proto_abort("AKN", call, 0);
947 goto out;
948 }
17926a79 949
a4ea4c47 950 if (after(hard_ack, call->acks_hard_ack)) {
c479d5f2
DH
951 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
952 rxrpc_end_tx_phase(call, false, "ETA");
c1e15b49 953 goto out;
c479d5f2
DH
954 }
955 }
17926a79 956
70790dbe 957 if (nr_acks > 0) {
d57a3a15 958 if (offset > (int)skb->len - nr_acks) {
c1e15b49
DH
959 rxrpc_proto_abort("XSA", call, 0);
960 goto out;
961 }
d57a3a15
DH
962
963 spin_lock(&call->acks_ack_lock);
964 skb_old = call->acks_soft_tbl;
965 call->acks_soft_tbl = skb;
966 spin_unlock(&call->acks_ack_lock);
967
968 rxrpc_input_soft_acks(call, skb->data + offset, first_soft_ack,
969 nr_acks, &summary);
970 skb_put = NULL;
971 } else if (call->acks_soft_tbl) {
972 spin_lock(&call->acks_ack_lock);
973 skb_old = call->acks_soft_tbl;
974 call->acks_soft_tbl = NULL;
975 spin_unlock(&call->acks_ack_lock);
70790dbe
DH
976 }
977
a4ea4c47 978 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
a9f312d9
DH
979 summary.nr_acks == call->tx_top - hard_ack &&
980 rxrpc_is_client_call(call))
72f0c6fb
DH
981 rxrpc_propose_ping(call, ack_serial,
982 rxrpc_propose_ack_ping_for_lost_reply);
57494343 983
c1e15b49
DH
984 rxrpc_congestion_management(call, skb, &summary, acked_serial);
985out:
986 spin_unlock(&call->input_lock);
d57a3a15
DH
987out_not_locked:
988 rxrpc_free_skb(skb_put, rxrpc_skb_freed);
989 rxrpc_free_skb(skb_old, rxrpc_skb_freed);
17926a79
DH
990}
991
992/*
248f219c 993 * Process an ACKALL packet.
17926a79 994 */
248f219c 995static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
17926a79 996{
31a1b989 997 struct rxrpc_ack_summary summary = { 0 };
248f219c 998 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 999
248f219c 1000 _proto("Rx ACKALL %%%u", sp->hdr.serial);
17926a79 1001
c1e15b49
DH
1002 spin_lock(&call->input_lock);
1003
c479d5f2 1004 if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
70790dbe 1005 rxrpc_end_tx_phase(call, false, "ETL");
c1e15b49
DH
1006
1007 spin_unlock(&call->input_lock);
248f219c 1008}
17926a79 1009
248f219c 1010/*
005ede28 1011 * Process an ABORT packet directed at a call.
248f219c
DH
1012 */
1013static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1014{
1015 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1016 __be32 wtmp;
1017 u32 abort_code = RX_CALL_DEAD;
17926a79 1018
248f219c 1019 _enter("");
17926a79 1020
248f219c 1021 if (skb->len >= 4 &&
775e5b71
DH
1022 skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1023 &wtmp, sizeof(wtmp)) >= 0)
248f219c 1024 abort_code = ntohl(wtmp);
17926a79 1025
005ede28
DH
1026 trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
1027
248f219c 1028 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
17926a79 1029
5ac0d622
DH
1030 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1031 abort_code, -ECONNABORTED);
17926a79
DH
1032}
1033
1034/*
248f219c 1035 * Process an incoming call packet.
17926a79 1036 */
248f219c 1037static void rxrpc_input_call_packet(struct rxrpc_call *call,
e8c3af6b 1038 struct sk_buff *skb)
17926a79 1039{
248f219c 1040 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
a158bdd3 1041 unsigned long timo;
17926a79 1042
7727640c 1043 _enter("%p,%p", call, skb);
17926a79 1044
a158bdd3
DH
1045 timo = READ_ONCE(call->next_rx_timo);
1046 if (timo) {
1047 unsigned long now = jiffies, expect_rx_by;
1048
c54e43d7 1049 expect_rx_by = now + timo;
a158bdd3
DH
1050 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1051 rxrpc_reduce_call_timer(call, expect_rx_by, now,
1052 rxrpc_timer_set_for_normal);
1053 }
3d7682af 1054
248f219c
DH
1055 switch (sp->hdr.type) {
1056 case RXRPC_PACKET_TYPE_DATA:
e8c3af6b 1057 rxrpc_input_data(call, skb);
4858e403 1058 goto no_free;
f5c17aae 1059
248f219c 1060 case RXRPC_PACKET_TYPE_ACK:
e8c3af6b 1061 rxrpc_input_ack(call, skb);
d57a3a15 1062 goto no_free;
17926a79 1063
248f219c
DH
1064 case RXRPC_PACKET_TYPE_BUSY:
1065 _proto("Rx BUSY %%%u", sp->hdr.serial);
17926a79 1066
248f219c
DH
1067 /* Just ignore BUSY packets from the server; the retry and
1068 * lifespan timers will take care of business. BUSY packets
1069 * from the client don't make sense.
1070 */
1071 break;
17926a79 1072
248f219c
DH
1073 case RXRPC_PACKET_TYPE_ABORT:
1074 rxrpc_input_abort(call, skb);
1075 break;
17926a79 1076
248f219c
DH
1077 case RXRPC_PACKET_TYPE_ACKALL:
1078 rxrpc_input_ackall(call, skb);
1079 break;
f5c17aae 1080
248f219c 1081 default:
248f219c 1082 break;
17926a79 1083 }
248f219c 1084
987db9f7 1085 rxrpc_free_skb(skb, rxrpc_skb_freed);
4858e403 1086no_free:
17926a79
DH
1087 _leave("");
1088}
1089
b3156274 1090/*
c1e15b49
DH
1091 * Handle a new service call on a channel implicitly completing the preceding
1092 * call on that channel. This does not apply to client conns.
b3156274
DH
1093 *
1094 * TODO: If callNumber > call_id + 1, renegotiate security.
1095 */
c1e15b49
DH
1096static void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
1097 struct rxrpc_connection *conn,
b3156274
DH
1098 struct rxrpc_call *call)
1099{
146d8fef 1100 switch (READ_ONCE(call->state)) {
b3156274
DH
1101 case RXRPC_CALL_SERVER_AWAIT_ACK:
1102 rxrpc_call_completed(call);
df561f66 1103 fallthrough;
b3156274
DH
1104 case RXRPC_CALL_COMPLETE:
1105 break;
1106 default:
3a92789a 1107 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
b3156274
DH
1108 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
1109 rxrpc_queue_call(call);
1110 }
c1e15b49 1111 trace_rxrpc_improper_term(call);
b3156274
DH
1112 break;
1113 }
1114
c1e15b49 1115 spin_lock(&rx->incoming_lock);
b3156274 1116 __rxrpc_disconnect_call(conn, call);
c1e15b49 1117 spin_unlock(&rx->incoming_lock);
b3156274
DH
1118}
1119
17926a79
DH
1120/*
1121 * post connection-level events to the connection
18bfeba5
DH
1122 * - this includes challenges, responses, some aborts and call terminal packet
1123 * retransmission.
17926a79 1124 */
2e7e9758 1125static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
17926a79
DH
1126 struct sk_buff *skb)
1127{
1128 _enter("%p,%p", conn, skb);
1129
17926a79 1130 skb_queue_tail(&conn->rx_queue, skb);
2e7e9758 1131 rxrpc_queue_conn(conn);
17926a79
DH
1132}
1133
44ba0698
DH
1134/*
1135 * post endpoint-level events to the local endpoint
1136 * - this includes debug and version messages
1137 */
1138static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
1139 struct sk_buff *skb)
1140{
1141 _enter("%p,%p", local, skb);
1142
730c5fd4
DH
1143 if (rxrpc_get_local_maybe(local)) {
1144 skb_queue_tail(&local->event_queue, skb);
1145 rxrpc_queue_local(local);
1146 } else {
987db9f7 1147 rxrpc_free_skb(skb, rxrpc_skb_freed);
730c5fd4 1148 }
44ba0698
DH
1149}
1150
248f219c
DH
1151/*
1152 * put a packet up for transport-level abort
1153 */
1154static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
1155{
730c5fd4
DH
1156 if (rxrpc_get_local_maybe(local)) {
1157 skb_queue_tail(&local->reject_queue, skb);
1158 rxrpc_queue_local(local);
1159 } else {
987db9f7 1160 rxrpc_free_skb(skb, rxrpc_skb_freed);
730c5fd4 1161 }
248f219c
DH
1162}
1163
0d12f8a4
DH
1164/*
1165 * Extract the wire header from a packet and translate the byte order.
1166 */
1167static noinline
1168int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
1169{
1170 struct rxrpc_wire_header whdr;
1171
1172 /* dig out the RxRPC connection details */
fb46f6ee
DH
1173 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
1174 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
1175 tracepoint_string("bad_hdr"));
0d12f8a4 1176 return -EBADMSG;
fb46f6ee 1177 }
0d12f8a4
DH
1178
1179 memset(sp, 0, sizeof(*sp));
1180 sp->hdr.epoch = ntohl(whdr.epoch);
1181 sp->hdr.cid = ntohl(whdr.cid);
1182 sp->hdr.callNumber = ntohl(whdr.callNumber);
1183 sp->hdr.seq = ntohl(whdr.seq);
1184 sp->hdr.serial = ntohl(whdr.serial);
1185 sp->hdr.flags = whdr.flags;
1186 sp->hdr.type = whdr.type;
1187 sp->hdr.userStatus = whdr.userStatus;
1188 sp->hdr.securityIndex = whdr.securityIndex;
1189 sp->hdr._rsvd = ntohs(whdr._rsvd);
1190 sp->hdr.serviceId = ntohs(whdr.serviceId);
1191 return 0;
1192}
1193
17926a79
DH
1194/*
1195 * handle data received on the local endpoint
1196 * - may be called in interrupt context
4f95dd78 1197 *
032be5f1
ED
1198 * [!] Note that as this is called from the encap_rcv hook, the socket is not
1199 * held locked by the caller and nothing prevents sk_user_data on the UDP from
1200 * being cleared in the middle of processing this function.
bfd28211
DH
1201 *
1202 * Called with the RCU read lock held from the IP layer via UDP.
17926a79 1203 */
5271953c 1204int rxrpc_input_packet(struct sock *udp_sk, struct sk_buff *skb)
17926a79 1205{
032be5f1 1206 struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
8496af50 1207 struct rxrpc_connection *conn;
248f219c 1208 struct rxrpc_channel *chan;
403fc2a1 1209 struct rxrpc_call *call = NULL;
17926a79 1210 struct rxrpc_skb_priv *sp;
0099dc58
DH
1211 struct rxrpc_peer *peer = NULL;
1212 struct rxrpc_sock *rx = NULL;
248f219c 1213 unsigned int channel;
17926a79 1214
248f219c 1215 _enter("%p", udp_sk);
17926a79 1216
032be5f1
ED
1217 if (unlikely(!local)) {
1218 kfree_skb(skb);
1219 return 0;
1220 }
b604dd98
DH
1221 if (skb->tstamp == 0)
1222 skb->tstamp = ktime_get_real();
1223
987db9f7 1224 rxrpc_new_skb(skb, rxrpc_skb_received);
17926a79 1225
5271953c 1226 skb_pull(skb, sizeof(struct udphdr));
1781f7f5 1227
7c13f97f
PA
1228 /* The UDP protocol already released all skb resources;
1229 * we are free to add our own data there.
0d12f8a4 1230 */
17926a79 1231 sp = rxrpc_skb(skb);
17926a79 1232
89b475ab
DH
1233 /* dig out the RxRPC connection details */
1234 if (rxrpc_extract_header(sp, skb) < 0)
1235 goto bad_message;
1236
8a681c36
DH
1237 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
1238 static int lose;
1239 if ((lose++ & 7) == 7) {
89b475ab 1240 trace_rxrpc_rx_lose(sp);
987db9f7 1241 rxrpc_free_skb(skb, rxrpc_skb_lost);
5271953c 1242 return 0;
8a681c36
DH
1243 }
1244 }
1245
2cfa2271
DH
1246 if (skb->tstamp == 0)
1247 skb->tstamp = ktime_get_real();
49e19ec7 1248 trace_rxrpc_rx_packet(sp);
17926a79 1249
248f219c
DH
1250 switch (sp->hdr.type) {
1251 case RXRPC_PACKET_TYPE_VERSION:
dc71db34 1252 if (rxrpc_to_client(sp))
ace45bec 1253 goto discard;
44ba0698
DH
1254 rxrpc_post_packet_to_local(local, skb);
1255 goto out;
bc6e1ea3 1256
248f219c 1257 case RXRPC_PACKET_TYPE_BUSY:
dc71db34 1258 if (rxrpc_to_server(sp))
248f219c 1259 goto discard;
df561f66 1260 fallthrough;
403fc2a1
DH
1261 case RXRPC_PACKET_TYPE_ACK:
1262 case RXRPC_PACKET_TYPE_ACKALL:
1263 if (sp->hdr.callNumber == 0)
1264 goto bad_message;
df561f66 1265 fallthrough;
403fc2a1
DH
1266 case RXRPC_PACKET_TYPE_ABORT:
1267 break;
248f219c
DH
1268
1269 case RXRPC_PACKET_TYPE_DATA:
403fc2a1
DH
1270 if (sp->hdr.callNumber == 0 ||
1271 sp->hdr.seq == 0)
248f219c 1272 goto bad_message;
d0d5c0cd
DH
1273
1274 /* Unshare the packet so that it can be modified for in-place
1275 * decryption.
1276 */
1277 if (sp->hdr.securityIndex != 0) {
1278 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
1279 if (!nskb) {
1280 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
1281 goto out;
1282 }
1283
1284 if (nskb != skb) {
1285 rxrpc_eaten_skb(skb, rxrpc_skb_received);
d0d5c0cd 1286 skb = nskb;
59132894 1287 rxrpc_new_skb(skb, rxrpc_skb_unshared);
d0d5c0cd
DH
1288 sp = rxrpc_skb(skb);
1289 }
1290 }
248f219c 1291 break;
b41d7cfe 1292
403fc2a1
DH
1293 case RXRPC_PACKET_TYPE_CHALLENGE:
1294 if (rxrpc_to_server(sp))
1295 goto discard;
1296 break;
1297 case RXRPC_PACKET_TYPE_RESPONSE:
1298 if (rxrpc_to_client(sp))
1299 goto discard;
1300 break;
1301
b41d7cfe
DH
1302 /* Packet types 9-11 should just be ignored. */
1303 case RXRPC_PACKET_TYPE_PARAMS:
1304 case RXRPC_PACKET_TYPE_10:
1305 case RXRPC_PACKET_TYPE_11:
1306 goto discard;
403fc2a1
DH
1307
1308 default:
1309 _proto("Rx Bad Packet Type %u", sp->hdr.type);
1310 goto bad_message;
248f219c 1311 }
17926a79 1312
403fc2a1
DH
1313 if (sp->hdr.serviceId == 0)
1314 goto bad_message;
1315
403fc2a1
DH
1316 if (rxrpc_to_server(sp)) {
1317 /* Weed out packets to services we're not offering. Packets
1318 * that would begin a call are explicitly rejected and the rest
1319 * are just discarded.
1320 */
1321 rx = rcu_dereference(local->service);
1322 if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
1323 sp->hdr.serviceId != rx->second_service)) {
1324 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
1325 sp->hdr.seq == 1)
1326 goto unsupported_service;
bfd28211 1327 goto discard;
403fc2a1
DH
1328 }
1329 }
1330
0099dc58 1331 conn = rxrpc_find_connection_rcu(local, skb, &peer);
248f219c
DH
1332 if (conn) {
1333 if (sp->hdr.securityIndex != conn->security_ix)
1334 goto wrong_security;
563ea7d5 1335
4e255721 1336 if (sp->hdr.serviceId != conn->service_id) {
c1e15b49
DH
1337 int old_id;
1338
1339 if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
1340 goto reupgrade;
1341 old_id = cmpxchg(&conn->service_id, conn->params.service_id,
1342 sp->hdr.serviceId);
1343
1344 if (old_id != conn->params.service_id &&
1345 old_id != sp->hdr.serviceId)
4e255721 1346 goto reupgrade;
4e255721 1347 }
3d7682af 1348
248f219c
DH
1349 if (sp->hdr.callNumber == 0) {
1350 /* Connection-level packet */
1351 _debug("CONN %p {%d}", conn, conn->debug_id);
1352 rxrpc_post_packet_to_conn(conn, skb);
bfd28211 1353 goto out;
248f219c
DH
1354 }
1355
e8c3af6b
DH
1356 if ((int)sp->hdr.serial - (int)conn->hi_serial > 0)
1357 conn->hi_serial = sp->hdr.serial;
17926a79 1358
8496af50 1359 /* Call-bound packets are routed by connection channel. */
248f219c
DH
1360 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
1361 chan = &conn->channels[channel];
18bfeba5
DH
1362
1363 /* Ignore really old calls */
1364 if (sp->hdr.callNumber < chan->last_call)
bfd28211 1365 goto discard;
18bfeba5
DH
1366
1367 if (sp->hdr.callNumber == chan->last_call) {
57b0c9d4
DH
1368 if (chan->call ||
1369 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
bfd28211 1370 goto discard;
57b0c9d4
DH
1371
1372 /* For the previous service call, if completed
1373 * successfully, we discard all further packets.
18bfeba5 1374 */
2266ffde 1375 if (rxrpc_conn_is_service(conn) &&
57b0c9d4 1376 chan->last_type == RXRPC_PACKET_TYPE_ACK)
bfd28211 1377 goto discard;
18bfeba5 1378
57b0c9d4
DH
1379 /* But otherwise we need to retransmit the final packet
1380 * from data cached in the connection record.
18bfeba5 1381 */
4764c0da
DH
1382 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
1383 trace_rxrpc_rx_data(chan->call_debug_id,
1384 sp->hdr.seq,
1385 sp->hdr.serial,
d4d02d8b 1386 sp->hdr.flags);
18bfeba5 1387 rxrpc_post_packet_to_conn(conn, skb);
bfd28211 1388 goto out;
18bfeba5 1389 }
0d12f8a4 1390
18bfeba5 1391 call = rcu_dereference(chan->call);
b3156274
DH
1392
1393 if (sp->hdr.callNumber > chan->call_id) {
bfd28211 1394 if (rxrpc_to_client(sp))
b3156274 1395 goto reject_packet;
b3156274 1396 if (call)
c1e15b49 1397 rxrpc_input_implicit_end_call(rx, conn, call);
b3156274
DH
1398 call = NULL;
1399 }
4e255721 1400
1a025028
DH
1401 if (call) {
1402 if (sp->hdr.serviceId != call->service_id)
1403 call->service_id = sp->hdr.serviceId;
1404 if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1405 call->rx_serial = sp->hdr.serial;
1406 if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1407 set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1408 }
248f219c 1409 }
8496af50 1410
a0575429 1411 if (!call || refcount_read(&call->ref) == 0) {
dc71db34 1412 if (rxrpc_to_client(sp) ||
248f219c 1413 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
bfd28211 1414 goto bad_message;
248f219c 1415 if (sp->hdr.seq != 1)
bfd28211 1416 goto discard;
c1e15b49 1417 call = rxrpc_new_incoming_call(local, rx, skb);
bfd28211 1418 if (!call)
248f219c 1419 goto reject_packet;
7727640c 1420 }
44ba0698 1421
4858e403
DH
1422 /* Process a call packet; this either discards or passes on the ref
1423 * elsewhere.
1424 */
e8c3af6b 1425 rxrpc_input_call_packet(call, skb);
4858e403 1426 goto out;
248f219c 1427
248f219c 1428discard:
987db9f7 1429 rxrpc_free_skb(skb, rxrpc_skb_freed);
44ba0698 1430out:
49e19ec7 1431 trace_rxrpc_rx_done(0, 0);
5271953c 1432 return 0;
8496af50 1433
248f219c 1434wrong_security:
a25e21f0 1435 trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
248f219c
DH
1436 RXKADINCONSISTENCY, EBADMSG);
1437 skb->priority = RXKADINCONSISTENCY;
1438 goto post_abort;
17926a79 1439
403fc2a1 1440unsupported_service:
403fc2a1
DH
1441 trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1442 RX_INVALID_OPERATION, EOPNOTSUPP);
1443 skb->priority = RX_INVALID_OPERATION;
1444 goto post_abort;
1445
4e255721 1446reupgrade:
a25e21f0 1447 trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
4e255721
DH
1448 RX_PROTOCOL_ERROR, EBADMSG);
1449 goto protocol_error;
1450
17926a79 1451bad_message:
a25e21f0 1452 trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
248f219c 1453 RX_PROTOCOL_ERROR, EBADMSG);
4e255721 1454protocol_error:
17926a79 1455 skb->priority = RX_PROTOCOL_ERROR;
248f219c 1456post_abort:
ece64fec 1457 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
49e19ec7
DH
1458reject_packet:
1459 trace_rxrpc_rx_done(skb->mark, skb->priority);
17926a79 1460 rxrpc_reject_packet(local, skb);
17926a79 1461 _leave(" [badmsg]");
5271953c 1462 return 0;
17926a79 1463}