7ae7046f0b03c11464b6753e6a5bc9e0942ff849
[platform/kernel/linux-rpi.git] / net / rxrpc / input.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Processing of received RxRPC packets
3  *
4  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include "ar-internal.h"
11
12 static void rxrpc_proto_abort(const char *why,
13                               struct rxrpc_call *call, rxrpc_seq_t seq)
14 {
15         if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG))
16                 rxrpc_send_abort_packet(call);
17 }
18
19 /*
20  * Do TCP-style congestion management [RFC 5681].
21  */
22 static void rxrpc_congestion_management(struct rxrpc_call *call,
23                                         struct sk_buff *skb,
24                                         struct rxrpc_ack_summary *summary,
25                                         rxrpc_serial_t acked_serial)
26 {
27         enum rxrpc_congest_change change = rxrpc_cong_no_change;
28         unsigned int cumulative_acks = call->cong_cumul_acks;
29         unsigned int cwnd = call->cong_cwnd;
30         bool resend = false;
31
32         summary->flight_size =
33                 (call->tx_top - call->acks_hard_ack) - summary->nr_acks;
34
35         if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
36                 summary->retrans_timeo = true;
37                 call->cong_ssthresh = max_t(unsigned int,
38                                             summary->flight_size / 2, 2);
39                 cwnd = 1;
40                 if (cwnd >= call->cong_ssthresh &&
41                     call->cong_mode == RXRPC_CALL_SLOW_START) {
42                         call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
43                         call->cong_tstamp = skb->tstamp;
44                         cumulative_acks = 0;
45                 }
46         }
47
48         cumulative_acks += summary->nr_new_acks;
49         cumulative_acks += summary->nr_rot_new_acks;
50         if (cumulative_acks > 255)
51                 cumulative_acks = 255;
52
53         summary->mode = call->cong_mode;
54         summary->cwnd = call->cong_cwnd;
55         summary->ssthresh = call->cong_ssthresh;
56         summary->cumulative_acks = cumulative_acks;
57         summary->dup_acks = call->cong_dup_acks;
58
59         /* If we haven't transmitted anything for >1RTT, we should reset the
60          * congestion management state.
61          */
62         if ((call->cong_mode == RXRPC_CALL_SLOW_START ||
63              call->cong_mode == RXRPC_CALL_CONGEST_AVOIDANCE) &&
64             ktime_before(ktime_add_us(call->tx_last_sent,
65                                       call->peer->srtt_us >> 3),
66                          ktime_get_real())
67             ) {
68                 change = rxrpc_cong_idle_reset;
69                 summary->mode = RXRPC_CALL_SLOW_START;
70                 if (RXRPC_TX_SMSS > 2190)
71                         summary->cwnd = 2;
72                 else if (RXRPC_TX_SMSS > 1095)
73                         summary->cwnd = 3;
74                 else
75                         summary->cwnd = 4;
76         }
77
78         switch (call->cong_mode) {
79         case RXRPC_CALL_SLOW_START:
80                 if (summary->saw_nacks)
81                         goto packet_loss_detected;
82                 if (summary->cumulative_acks > 0)
83                         cwnd += 1;
84                 if (cwnd >= call->cong_ssthresh) {
85                         call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
86                         call->cong_tstamp = skb->tstamp;
87                 }
88                 goto out;
89
90         case RXRPC_CALL_CONGEST_AVOIDANCE:
91                 if (summary->saw_nacks)
92                         goto packet_loss_detected;
93
94                 /* We analyse the number of packets that get ACK'd per RTT
95                  * period and increase the window if we managed to fill it.
96                  */
97                 if (call->peer->rtt_count == 0)
98                         goto out;
99                 if (ktime_before(skb->tstamp,
100                                  ktime_add_us(call->cong_tstamp,
101                                               call->peer->srtt_us >> 3)))
102                         goto out_no_clear_ca;
103                 change = rxrpc_cong_rtt_window_end;
104                 call->cong_tstamp = skb->tstamp;
105                 if (cumulative_acks >= cwnd)
106                         cwnd++;
107                 goto out;
108
109         case RXRPC_CALL_PACKET_LOSS:
110                 if (!summary->saw_nacks)
111                         goto resume_normality;
112
113                 if (summary->new_low_nack) {
114                         change = rxrpc_cong_new_low_nack;
115                         call->cong_dup_acks = 1;
116                         if (call->cong_extra > 1)
117                                 call->cong_extra = 1;
118                         goto send_extra_data;
119                 }
120
121                 call->cong_dup_acks++;
122                 if (call->cong_dup_acks < 3)
123                         goto send_extra_data;
124
125                 change = rxrpc_cong_begin_retransmission;
126                 call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
127                 call->cong_ssthresh = max_t(unsigned int,
128                                             summary->flight_size / 2, 2);
129                 cwnd = call->cong_ssthresh + 3;
130                 call->cong_extra = 0;
131                 call->cong_dup_acks = 0;
132                 resend = true;
133                 goto out;
134
135         case RXRPC_CALL_FAST_RETRANSMIT:
136                 if (!summary->new_low_nack) {
137                         if (summary->nr_new_acks == 0)
138                                 cwnd += 1;
139                         call->cong_dup_acks++;
140                         if (call->cong_dup_acks == 2) {
141                                 change = rxrpc_cong_retransmit_again;
142                                 call->cong_dup_acks = 0;
143                                 resend = true;
144                         }
145                 } else {
146                         change = rxrpc_cong_progress;
147                         cwnd = call->cong_ssthresh;
148                         if (!summary->saw_nacks)
149                                 goto resume_normality;
150                 }
151                 goto out;
152
153         default:
154                 BUG();
155                 goto out;
156         }
157
158 resume_normality:
159         change = rxrpc_cong_cleared_nacks;
160         call->cong_dup_acks = 0;
161         call->cong_extra = 0;
162         call->cong_tstamp = skb->tstamp;
163         if (cwnd < call->cong_ssthresh)
164                 call->cong_mode = RXRPC_CALL_SLOW_START;
165         else
166                 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
167 out:
168         cumulative_acks = 0;
169 out_no_clear_ca:
170         if (cwnd >= RXRPC_TX_MAX_WINDOW)
171                 cwnd = RXRPC_TX_MAX_WINDOW;
172         call->cong_cwnd = cwnd;
173         call->cong_cumul_acks = cumulative_acks;
174         trace_rxrpc_congest(call, summary, acked_serial, change);
175         if (resend)
176                 rxrpc_resend(call, skb);
177         return;
178
179 packet_loss_detected:
180         change = rxrpc_cong_saw_nack;
181         call->cong_mode = RXRPC_CALL_PACKET_LOSS;
182         call->cong_dup_acks = 0;
183         goto send_extra_data;
184
185 send_extra_data:
186         /* Send some previously unsent DATA if we have some to advance the ACK
187          * state.
188          */
189         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
190             summary->nr_acks != call->tx_top - call->acks_hard_ack) {
191                 call->cong_extra++;
192                 wake_up(&call->waitq);
193         }
194         goto out_no_clear_ca;
195 }
196
197 /*
198  * Apply a hard ACK by advancing the Tx window.
199  */
200 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
201                                    struct rxrpc_ack_summary *summary)
202 {
203         struct rxrpc_txbuf *txb;
204         bool rot_last = false;
205
206         list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
207                 if (before_eq(txb->seq, call->acks_hard_ack))
208                         continue;
209                 summary->nr_rot_new_acks++;
210                 if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
211                         set_bit(RXRPC_CALL_TX_LAST, &call->flags);
212                         rot_last = true;
213                 }
214                 if (txb->seq == to)
215                         break;
216         }
217
218         if (rot_last)
219                 set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
220
221         _enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
222
223         if (call->acks_lowest_nak == call->acks_hard_ack) {
224                 call->acks_lowest_nak = to;
225         } else if (after(to, call->acks_lowest_nak)) {
226                 summary->new_low_nack = true;
227                 call->acks_lowest_nak = to;
228         }
229
230         smp_store_release(&call->acks_hard_ack, to);
231
232         trace_rxrpc_txqueue(call, (rot_last ?
233                                    rxrpc_txqueue_rotate_last :
234                                    rxrpc_txqueue_rotate));
235         wake_up(&call->waitq);
236         return rot_last;
237 }
238
239 /*
240  * End the transmission phase of a call.
241  *
242  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
243  * or a final ACK packet.
244  */
245 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
246                                const char *abort_why)
247 {
248         unsigned int state;
249
250         ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
251
252         write_lock(&call->state_lock);
253
254         state = call->state;
255         switch (state) {
256         case RXRPC_CALL_CLIENT_SEND_REQUEST:
257         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
258                 if (reply_begun)
259                         call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
260                 else
261                         call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
262                 break;
263
264         case RXRPC_CALL_SERVER_AWAIT_ACK:
265                 __rxrpc_call_completed(call);
266                 state = call->state;
267                 break;
268
269         default:
270                 goto bad_state;
271         }
272
273         write_unlock(&call->state_lock);
274         if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
275                 trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
276         else
277                 trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
278         _leave(" = ok");
279         return true;
280
281 bad_state:
282         write_unlock(&call->state_lock);
283         kdebug("end_tx %s", rxrpc_call_states[call->state]);
284         rxrpc_proto_abort(abort_why, call, call->tx_top);
285         return false;
286 }
287
288 /*
289  * Begin the reply reception phase of a call.
290  */
291 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
292 {
293         struct rxrpc_ack_summary summary = { 0 };
294         unsigned long now, timo;
295         rxrpc_seq_t top = READ_ONCE(call->tx_top);
296
297         if (call->ackr_reason) {
298                 now = jiffies;
299                 timo = now + MAX_JIFFY_OFFSET;
300                 WRITE_ONCE(call->resend_at, timo);
301                 WRITE_ONCE(call->delay_ack_at, timo);
302                 trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
303         }
304
305         if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
306                 if (!rxrpc_rotate_tx_window(call, top, &summary)) {
307                         rxrpc_proto_abort("TXL", call, top);
308                         return false;
309                 }
310         }
311         return rxrpc_end_tx_phase(call, true, "ETD");
312 }
313
314 static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
315                                           rxrpc_seq_t window, rxrpc_seq_t wtop)
316 {
317         atomic64_set_release(&call->ackr_window, ((u64)wtop) << 32 | window);
318 }
319
320 /*
321  * Push a DATA packet onto the Rx queue.
322  */
323 static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
324                                    rxrpc_seq_t window, rxrpc_seq_t wtop,
325                                    enum rxrpc_receive_trace why)
326 {
327         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
328         bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
329
330         __skb_queue_tail(&call->recvmsg_queue, skb);
331         rxrpc_input_update_ack_window(call, window, wtop);
332
333         trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
334 }
335
336 /*
337  * Process a DATA packet.
338  */
339 static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb,
340                                  bool *_notify)
341 {
342         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
343         struct sk_buff *oos;
344         rxrpc_serial_t serial = sp->hdr.serial;
345         u64 win = atomic64_read(&call->ackr_window);
346         rxrpc_seq_t window = lower_32_bits(win);
347         rxrpc_seq_t wtop = upper_32_bits(win);
348         rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
349         rxrpc_seq_t seq = sp->hdr.seq;
350         bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
351         int ack_reason = -1;
352
353         rxrpc_inc_stat(call->rxnet, stat_rx_data);
354         if (sp->hdr.flags & RXRPC_REQUEST_ACK)
355                 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
356         if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
357                 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
358
359         if (last) {
360                 if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
361                     seq + 1 != wtop) {
362                         rxrpc_proto_abort("LSN", call, seq);
363                         return;
364                 }
365         } else {
366                 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
367                     after_eq(seq, wtop)) {
368                         pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
369                                 call->debug_id, seq, window, wtop, wlimit);
370                         rxrpc_proto_abort("LSA", call, seq);
371                         return;
372                 }
373         }
374
375         if (after(seq, call->rx_highest_seq))
376                 call->rx_highest_seq = seq;
377
378         trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
379
380         if (before(seq, window)) {
381                 ack_reason = RXRPC_ACK_DUPLICATE;
382                 goto send_ack;
383         }
384         if (after(seq, wlimit)) {
385                 ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
386                 goto send_ack;
387         }
388
389         /* Queue the packet. */
390         if (seq == window) {
391                 rxrpc_seq_t reset_from;
392                 bool reset_sack = false;
393
394                 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
395                         ack_reason = RXRPC_ACK_REQUESTED;
396                 /* Send an immediate ACK if we fill in a hole */
397                 else if (!skb_queue_empty(&call->rx_oos_queue))
398                         ack_reason = RXRPC_ACK_DELAY;
399                 else
400                         atomic_inc_return(&call->ackr_nr_unacked);
401
402                 window++;
403                 if (after(window, wtop))
404                         wtop = window;
405
406                 rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg);
407
408                 spin_lock(&call->recvmsg_queue.lock);
409                 rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
410                 *_notify = true;
411
412                 while ((oos = skb_peek(&call->rx_oos_queue))) {
413                         struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
414
415                         if (after(osp->hdr.seq, window))
416                                 break;
417
418                         __skb_unlink(oos, &call->rx_oos_queue);
419                         last = osp->hdr.flags & RXRPC_LAST_PACKET;
420                         seq = osp->hdr.seq;
421                         if (!reset_sack) {
422                                 reset_from = seq;
423                                 reset_sack = true;
424                         }
425
426                         window++;
427                         rxrpc_input_queue_data(call, oos, window, wtop,
428                                                  rxrpc_receive_queue_oos);
429                 }
430
431                 spin_unlock(&call->recvmsg_queue.lock);
432
433                 if (reset_sack) {
434                         do {
435                                 call->ackr_sack_table[reset_from % RXRPC_SACK_SIZE] = 0;
436                         } while (reset_from++, before(reset_from, window));
437                 }
438         } else {
439                 bool keep = false;
440
441                 ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
442
443                 if (!call->ackr_sack_table[seq % RXRPC_SACK_SIZE]) {
444                         call->ackr_sack_table[seq % RXRPC_SACK_SIZE] = 1;
445                         keep = 1;
446                 }
447
448                 if (after(seq + 1, wtop)) {
449                         wtop = seq + 1;
450                         rxrpc_input_update_ack_window(call, window, wtop);
451                 }
452
453                 if (!keep) {
454                         ack_reason = RXRPC_ACK_DUPLICATE;
455                         goto send_ack;
456                 }
457
458                 skb_queue_walk(&call->rx_oos_queue, oos) {
459                         struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
460
461                         if (after(osp->hdr.seq, seq)) {
462                                 rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
463                                 __skb_queue_before(&call->rx_oos_queue, oos, skb);
464                                 goto oos_queued;
465                         }
466                 }
467
468                 rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
469                 __skb_queue_tail(&call->rx_oos_queue, skb);
470         oos_queued:
471                 trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
472                                     sp->hdr.serial, sp->hdr.seq);
473         }
474
475 send_ack:
476         if (ack_reason >= 0)
477                 rxrpc_send_ACK(call, ack_reason, serial,
478                                rxrpc_propose_ack_input_data);
479         else
480                 rxrpc_propose_delay_ACK(call, serial,
481                                         rxrpc_propose_ack_input_data);
482 }
483
484 /*
485  * Split a jumbo packet and file the bits separately.
486  */
487 static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
488 {
489         struct rxrpc_jumbo_header jhdr;
490         struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
491         struct sk_buff *jskb;
492         unsigned int offset = sizeof(struct rxrpc_wire_header);
493         unsigned int len = skb->len - offset;
494         bool notify = false;
495
496         while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
497                 if (len < RXRPC_JUMBO_SUBPKTLEN)
498                         goto protocol_error;
499                 if (sp->hdr.flags & RXRPC_LAST_PACKET)
500                         goto protocol_error;
501                 if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
502                                   &jhdr, sizeof(jhdr)) < 0)
503                         goto protocol_error;
504
505                 jskb = skb_clone(skb, GFP_NOFS);
506                 if (!jskb) {
507                         kdebug("couldn't clone");
508                         return false;
509                 }
510                 rxrpc_new_skb(jskb, rxrpc_skb_new_jumbo_subpacket);
511                 jsp = rxrpc_skb(jskb);
512                 jsp->offset = offset;
513                 jsp->len = RXRPC_JUMBO_DATALEN;
514                 rxrpc_input_data_one(call, jskb, &notify);
515                 rxrpc_free_skb(jskb, rxrpc_skb_put_jumbo_subpacket);
516
517                 sp->hdr.flags = jhdr.flags;
518                 sp->hdr._rsvd = ntohs(jhdr._rsvd);
519                 sp->hdr.seq++;
520                 sp->hdr.serial++;
521                 offset += RXRPC_JUMBO_SUBPKTLEN;
522                 len -= RXRPC_JUMBO_SUBPKTLEN;
523         }
524
525         sp->offset = offset;
526         sp->len    = len;
527         rxrpc_input_data_one(call, skb, &notify);
528         if (notify) {
529                 trace_rxrpc_notify_socket(call->debug_id, sp->hdr.serial);
530                 rxrpc_notify_socket(call);
531         }
532         return true;
533
534 protocol_error:
535         return false;
536 }
537
538 /*
539  * Process a DATA packet, adding the packet to the Rx ring.  The caller's
540  * packet ref must be passed on or discarded.
541  */
542 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
543 {
544         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
545         enum rxrpc_call_state state;
546         rxrpc_serial_t serial = sp->hdr.serial;
547         rxrpc_seq_t seq0 = sp->hdr.seq;
548
549         _enter("{%llx,%x},{%u,%x}",
550                atomic64_read(&call->ackr_window), call->rx_highest_seq,
551                skb->len, seq0);
552
553         state = READ_ONCE(call->state);
554         if (state >= RXRPC_CALL_COMPLETE)
555                 return;
556
557         if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
558                 unsigned long timo = READ_ONCE(call->next_req_timo);
559                 unsigned long now, expect_req_by;
560
561                 if (timo) {
562                         now = jiffies;
563                         expect_req_by = now + timo;
564                         WRITE_ONCE(call->expect_req_by, expect_req_by);
565                         rxrpc_reduce_call_timer(call, expect_req_by, now,
566                                                 rxrpc_timer_set_for_idle);
567                 }
568         }
569
570         /* Received data implicitly ACKs all of the request packets we sent
571          * when we're acting as a client.
572          */
573         if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
574              state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
575             !rxrpc_receiving_reply(call))
576                 goto out_notify;
577
578         if (!rxrpc_input_split_jumbo(call, skb)) {
579                 rxrpc_proto_abort("VLD", call, sp->hdr.seq);
580                 goto out_notify;
581         }
582         skb = NULL;
583
584 out_notify:
585         trace_rxrpc_notify_socket(call->debug_id, serial);
586         rxrpc_notify_socket(call);
587         _leave(" [queued]");
588 }
589
590 /*
591  * See if there's a cached RTT probe to complete.
592  */
593 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
594                                      ktime_t resp_time,
595                                      rxrpc_serial_t acked_serial,
596                                      rxrpc_serial_t ack_serial,
597                                      enum rxrpc_rtt_rx_trace type)
598 {
599         rxrpc_serial_t orig_serial;
600         unsigned long avail;
601         ktime_t sent_at;
602         bool matched = false;
603         int i;
604
605         avail = READ_ONCE(call->rtt_avail);
606         smp_rmb(); /* Read avail bits before accessing data. */
607
608         for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
609                 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
610                         continue;
611
612                 sent_at = call->rtt_sent_at[i];
613                 orig_serial = call->rtt_serial[i];
614
615                 if (orig_serial == acked_serial) {
616                         clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
617                         smp_mb(); /* Read data before setting avail bit */
618                         set_bit(i, &call->rtt_avail);
619                         if (type != rxrpc_rtt_rx_cancel)
620                                 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
621                                                    sent_at, resp_time);
622                         else
623                                 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
624                                                    orig_serial, acked_serial, 0, 0);
625                         matched = true;
626                 }
627
628                 /* If a later serial is being acked, then mark this slot as
629                  * being available.
630                  */
631                 if (after(acked_serial, orig_serial)) {
632                         trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
633                                            orig_serial, acked_serial, 0, 0);
634                         clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
635                         smp_wmb();
636                         set_bit(i, &call->rtt_avail);
637                 }
638         }
639
640         if (!matched)
641                 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
642 }
643
644 /*
645  * Process the extra information that may be appended to an ACK packet
646  */
647 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
648                                 struct rxrpc_ackinfo *ackinfo)
649 {
650         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
651         struct rxrpc_peer *peer;
652         unsigned int mtu;
653         bool wake = false;
654         u32 rwind = ntohl(ackinfo->rwind);
655
656         if (rwind > RXRPC_TX_MAX_WINDOW)
657                 rwind = RXRPC_TX_MAX_WINDOW;
658         if (call->tx_winsize != rwind) {
659                 if (rwind > call->tx_winsize)
660                         wake = true;
661                 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
662                 call->tx_winsize = rwind;
663         }
664
665         if (call->cong_ssthresh > rwind)
666                 call->cong_ssthresh = rwind;
667
668         mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
669
670         peer = call->peer;
671         if (mtu < peer->maxdata) {
672                 spin_lock_bh(&peer->lock);
673                 peer->maxdata = mtu;
674                 peer->mtu = mtu + peer->hdrsize;
675                 spin_unlock_bh(&peer->lock);
676         }
677
678         if (wake)
679                 wake_up(&call->waitq);
680 }
681
682 /*
683  * Process individual soft ACKs.
684  *
685  * Each ACK in the array corresponds to one packet and can be either an ACK or
686  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
687  * packets that lie beyond the end of the ACK list are scheduled for resend by
688  * the timer on the basis that the peer might just not have processed them at
689  * the time the ACK was sent.
690  */
691 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
692                                   rxrpc_seq_t seq, int nr_acks,
693                                   struct rxrpc_ack_summary *summary)
694 {
695         unsigned int i;
696
697         for (i = 0; i < nr_acks; i++) {
698                 if (acks[i] == RXRPC_ACK_TYPE_ACK) {
699                         summary->nr_acks++;
700                         summary->nr_new_acks++;
701                 } else {
702                         if (!summary->saw_nacks &&
703                             call->acks_lowest_nak != seq + i) {
704                                 call->acks_lowest_nak = seq + i;
705                                 summary->new_low_nack = true;
706                         }
707                         summary->saw_nacks = true;
708                 }
709         }
710 }
711
712 /*
713  * Return true if the ACK is valid - ie. it doesn't appear to have regressed
714  * with respect to the ack state conveyed by preceding ACKs.
715  */
716 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
717                                rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
718 {
719         rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
720
721         if (after(first_pkt, base))
722                 return true; /* The window advanced */
723
724         if (before(first_pkt, base))
725                 return false; /* firstPacket regressed */
726
727         if (after_eq(prev_pkt, call->acks_prev_seq))
728                 return true; /* previousPacket hasn't regressed. */
729
730         /* Some rx implementations put a serial number in previousPacket. */
731         if (after_eq(prev_pkt, base + call->tx_winsize))
732                 return false;
733         return true;
734 }
735
736 /*
737  * Process an ACK packet.
738  *
739  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
740  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
741  *
742  * A hard-ACK means that a packet has been processed and may be discarded; a
743  * soft-ACK means that the packet may be discarded and retransmission
744  * requested.  A phase is complete when all packets are hard-ACK'd.
745  */
746 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
747 {
748         struct rxrpc_ack_summary summary = { 0 };
749         struct rxrpc_ackpacket ack;
750         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
751         struct rxrpc_ackinfo info;
752         rxrpc_serial_t ack_serial, acked_serial;
753         rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
754         int nr_acks, offset, ioffset;
755
756         _enter("");
757
758         offset = sizeof(struct rxrpc_wire_header);
759         if (skb_copy_bits(skb, offset, &ack, sizeof(ack)) < 0)
760                 return rxrpc_proto_abort("XAK", call, 0);
761         offset += sizeof(ack);
762
763         ack_serial = sp->hdr.serial;
764         acked_serial = ntohl(ack.serial);
765         first_soft_ack = ntohl(ack.firstPacket);
766         prev_pkt = ntohl(ack.previousPacket);
767         hard_ack = first_soft_ack - 1;
768         nr_acks = ack.nAcks;
769         summary.ack_reason = (ack.reason < RXRPC_ACK__INVALID ?
770                               ack.reason : RXRPC_ACK__INVALID);
771
772         trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
773                            first_soft_ack, prev_pkt,
774                            summary.ack_reason, nr_acks);
775         rxrpc_inc_stat(call->rxnet, stat_rx_acks[ack.reason]);
776
777         switch (ack.reason) {
778         case RXRPC_ACK_PING_RESPONSE:
779                 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
780                                          rxrpc_rtt_rx_ping_response);
781                 break;
782         case RXRPC_ACK_REQUESTED:
783                 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
784                                          rxrpc_rtt_rx_requested_ack);
785                 break;
786         default:
787                 if (acked_serial != 0)
788                         rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
789                                                  rxrpc_rtt_rx_cancel);
790                 break;
791         }
792
793         if (ack.reason == RXRPC_ACK_PING) {
794                 rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
795                                rxrpc_propose_ack_respond_to_ping);
796         } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
797                 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
798                                rxrpc_propose_ack_respond_to_ack);
799         }
800
801         /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
802          * indicates that the client address changed due to NAT.  The server
803          * lost the call because it switched to a different peer.
804          */
805         if (unlikely(ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
806             first_soft_ack == 1 &&
807             prev_pkt == 0 &&
808             rxrpc_is_client_call(call)) {
809                 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
810                                           0, -ENETRESET);
811                 return;
812         }
813
814         /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
815          * indicate a change of address.  However, we can retransmit the call
816          * if we still have it buffered to the beginning.
817          */
818         if (unlikely(ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
819             first_soft_ack == 1 &&
820             prev_pkt == 0 &&
821             call->acks_hard_ack == 0 &&
822             rxrpc_is_client_call(call)) {
823                 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
824                                           0, -ENETRESET);
825                 return;
826         }
827
828         /* Discard any out-of-order or duplicate ACKs (outside lock). */
829         if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
830                 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
831                                            first_soft_ack, call->acks_first_seq,
832                                            prev_pkt, call->acks_prev_seq);
833                 return;
834         }
835
836         info.rxMTU = 0;
837         ioffset = offset + nr_acks + 3;
838         if (skb->len >= ioffset + sizeof(info) &&
839             skb_copy_bits(skb, ioffset, &info, sizeof(info)) < 0)
840                 return rxrpc_proto_abort("XAI", call, 0);
841
842         if (nr_acks > 0)
843                 skb_condense(skb);
844
845         call->acks_latest_ts = skb->tstamp;
846         call->acks_first_seq = first_soft_ack;
847         call->acks_prev_seq = prev_pkt;
848
849         switch (ack.reason) {
850         case RXRPC_ACK_PING:
851                 break;
852         default:
853                 if (after(acked_serial, call->acks_highest_serial))
854                         call->acks_highest_serial = acked_serial;
855                 break;
856         }
857
858         /* Parse rwind and mtu sizes if provided. */
859         if (info.rxMTU)
860                 rxrpc_input_ackinfo(call, skb, &info);
861
862         if (first_soft_ack == 0)
863                 return rxrpc_proto_abort("AK0", call, 0);
864
865         /* Ignore ACKs unless we are or have just been transmitting. */
866         switch (READ_ONCE(call->state)) {
867         case RXRPC_CALL_CLIENT_SEND_REQUEST:
868         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
869         case RXRPC_CALL_SERVER_SEND_REPLY:
870         case RXRPC_CALL_SERVER_AWAIT_ACK:
871                 break;
872         default:
873                 return;
874         }
875
876         if (before(hard_ack, call->acks_hard_ack) ||
877             after(hard_ack, call->tx_top))
878                 return rxrpc_proto_abort("AKW", call, 0);
879         if (nr_acks > call->tx_top - hard_ack)
880                 return rxrpc_proto_abort("AKN", call, 0);
881
882         if (after(hard_ack, call->acks_hard_ack)) {
883                 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
884                         rxrpc_end_tx_phase(call, false, "ETA");
885                         return;
886                 }
887         }
888
889         if (nr_acks > 0) {
890                 if (offset > (int)skb->len - nr_acks)
891                         return rxrpc_proto_abort("XSA", call, 0);
892                 rxrpc_input_soft_acks(call, skb->data + offset, first_soft_ack,
893                                       nr_acks, &summary);
894         }
895
896         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
897             summary.nr_acks == call->tx_top - hard_ack &&
898             rxrpc_is_client_call(call))
899                 rxrpc_propose_ping(call, ack_serial,
900                                    rxrpc_propose_ack_ping_for_lost_reply);
901
902         rxrpc_congestion_management(call, skb, &summary, acked_serial);
903 }
904
905 /*
906  * Process an ACKALL packet.
907  */
908 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
909 {
910         struct rxrpc_ack_summary summary = { 0 };
911
912         if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
913                 rxrpc_end_tx_phase(call, false, "ETL");
914 }
915
916 /*
917  * Process an ABORT packet directed at a call.
918  */
919 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
920 {
921         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
922
923         trace_rxrpc_rx_abort(call, sp->hdr.serial, skb->priority);
924
925         rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
926                                   skb->priority, -ECONNABORTED);
927 }
928
929 /*
930  * Process an incoming call packet.
931  */
932 void rxrpc_input_call_packet(struct rxrpc_call *call, struct sk_buff *skb)
933 {
934         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
935         unsigned long timo;
936
937         _enter("%p,%p", call, skb);
938
939         if (sp->hdr.serviceId != call->dest_srx.srx_service)
940                 call->dest_srx.srx_service = sp->hdr.serviceId;
941         if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
942                 call->rx_serial = sp->hdr.serial;
943         if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
944                 set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
945
946         timo = READ_ONCE(call->next_rx_timo);
947         if (timo) {
948                 unsigned long now = jiffies, expect_rx_by;
949
950                 expect_rx_by = now + timo;
951                 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
952                 rxrpc_reduce_call_timer(call, expect_rx_by, now,
953                                         rxrpc_timer_set_for_normal);
954         }
955
956         switch (sp->hdr.type) {
957         case RXRPC_PACKET_TYPE_DATA:
958                 rxrpc_input_data(call, skb);
959                 break;
960
961         case RXRPC_PACKET_TYPE_ACK:
962                 rxrpc_input_ack(call, skb);
963                 break;
964
965         case RXRPC_PACKET_TYPE_BUSY:
966                 /* Just ignore BUSY packets from the server; the retry and
967                  * lifespan timers will take care of business.  BUSY packets
968                  * from the client don't make sense.
969                  */
970                 break;
971
972         case RXRPC_PACKET_TYPE_ABORT:
973                 rxrpc_input_abort(call, skb);
974                 break;
975
976         case RXRPC_PACKET_TYPE_ACKALL:
977                 rxrpc_input_ackall(call, skb);
978                 break;
979
980         default:
981                 break;
982         }
983 }
984
985 /*
986  * Handle a new service call on a channel implicitly completing the preceding
987  * call on that channel.  This does not apply to client conns.
988  *
989  * TODO: If callNumber > call_id + 1, renegotiate security.
990  */
991 void rxrpc_implicit_end_call(struct rxrpc_call *call, struct sk_buff *skb)
992 {
993         struct rxrpc_connection *conn = call->conn;
994
995         switch (READ_ONCE(call->state)) {
996         case RXRPC_CALL_SERVER_AWAIT_ACK:
997                 rxrpc_call_completed(call);
998                 fallthrough;
999         case RXRPC_CALL_COMPLETE:
1000                 break;
1001         default:
1002                 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN))
1003                         rxrpc_send_abort_packet(call);
1004                 trace_rxrpc_improper_term(call);
1005                 break;
1006         }
1007
1008         rxrpc_input_call_event(call, skb);
1009
1010         spin_lock(&conn->bundle->channel_lock);
1011         __rxrpc_disconnect_call(conn, call);
1012         spin_unlock(&conn->bundle->channel_lock);
1013 }