0cb23ba79e3b0ba0414c8d3f175c6147f3ff3f00
[platform/kernel/linux-rpi.git] / net / rxrpc / input.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet reception
3  *
4  * Copyright (C) 2007, 2016 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                 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
17                 rxrpc_queue_call(call);
18         }
19 }
20
21 /*
22  * Do TCP-style congestion management [RFC 5681].
23  */
24 static void rxrpc_congestion_management(struct rxrpc_call *call,
25                                         struct sk_buff *skb,
26                                         struct rxrpc_ack_summary *summary,
27                                         rxrpc_serial_t acked_serial)
28 {
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;
32         bool resend = false;
33
34         summary->flight_size =
35                 (call->tx_top - call->tx_hard_ack) - summary->nr_acks;
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;
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;
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:
63                 if (summary->nr_nacks > 0)
64                         goto packet_loss_detected;
65                 if (summary->cumulative_acks > 0)
66                         cwnd += 1;
67                 if (cwnd >= call->cong_ssthresh) {
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:
74                 if (summary->nr_nacks > 0)
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                  */
80                 if (call->peer->rtt_count == 0)
81                         goto out;
82                 if (ktime_before(skb->tstamp,
83                                  ktime_add_us(call->cong_tstamp,
84                                               call->peer->srtt_us >> 3)))
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:
93                 if (summary->nr_nacks == 0)
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;
131                         if (summary->nr_nacks == 0)
132                                 goto resume_normality;
133                 }
134                 goto out;
135
136         default:
137                 BUG();
138                 goto out;
139         }
140
141 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;
148         else
149                 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
150 out:
151         cumulative_acks = 0;
152 out_no_clear_ca:
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);
160         return;
161
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;
167
168 send_extra_data:
169         /* Send some previously unsent DATA if we have some to advance the ACK
170          * state.
171          */
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) {
175                 call->cong_extra++;
176                 wake_up(&call->waitq);
177         }
178         goto out_no_clear_ca;
179 }
180
181 /*
182  * Apply a hard ACK by advancing the Tx window.
183  */
184 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
185                                    struct rxrpc_ack_summary *summary)
186 {
187         struct sk_buff *skb, *list = NULL;
188         bool rot_last = false;
189         int ix;
190         u8 annotation;
191
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;
197         }
198
199         spin_lock(&call->lock);
200
201         while (before(call->tx_hard_ack, to)) {
202                 call->tx_hard_ack++;
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;
209                 skb->next = list;
210                 list = skb;
211
212                 if (annotation & RXRPC_TX_ANNO_LAST) {
213                         set_bit(RXRPC_CALL_TX_LAST, &call->flags);
214                         rot_last = true;
215                 }
216                 if ((annotation & RXRPC_TX_ANNO_MASK) != RXRPC_TX_ANNO_ACK)
217                         summary->nr_rot_new_acks++;
218         }
219
220         spin_unlock(&call->lock);
221
222         trace_rxrpc_transmit(call, (rot_last ?
223                                     rxrpc_transmit_rotate_last :
224                                     rxrpc_transmit_rotate));
225         wake_up(&call->waitq);
226
227         while (list) {
228                 skb = list;
229                 list = skb->next;
230                 skb_mark_not_on_list(skb);
231                 rxrpc_free_skb(skb, rxrpc_skb_freed);
232         }
233
234         return rot_last;
235 }
236
237 /*
238  * End the transmission phase of a call.
239  *
240  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
241  * or a final ACK packet.
242  */
243 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
244                                const char *abort_why)
245 {
246         unsigned int state;
247
248         ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
249
250         write_lock(&call->state_lock);
251
252         state = call->state;
253         switch (state) {
254         case RXRPC_CALL_CLIENT_SEND_REQUEST:
255         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
256                 if (reply_begun)
257                         call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
258                 else
259                         call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
260                 break;
261
262         case RXRPC_CALL_SERVER_AWAIT_ACK:
263                 __rxrpc_call_completed(call);
264                 state = call->state;
265                 break;
266
267         default:
268                 goto bad_state;
269         }
270
271         write_unlock(&call->state_lock);
272         if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
273                 trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
274         else
275                 trace_rxrpc_transmit(call, rxrpc_transmit_end);
276         _leave(" = ok");
277         return true;
278
279 bad_state:
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);
283         return false;
284 }
285
286 /*
287  * Begin the reply reception phase of a call.
288  */
289 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
290 {
291         struct rxrpc_ack_summary summary = { 0 };
292         unsigned long now, timo;
293         rxrpc_seq_t top = READ_ONCE(call->tx_top);
294
295         if (call->ackr_reason) {
296                 spin_lock_bh(&call->lock);
297                 call->ackr_reason = 0;
298                 spin_unlock_bh(&call->lock);
299                 now = jiffies;
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);
304         }
305
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);
309                         return false;
310                 }
311         }
312         return rxrpc_end_tx_phase(call, true, "ETD");
313 }
314
315 /*
316  * Scan a data packet to validate its structure and to work out how many
317  * subpackets it contains.
318  *
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
321  * each subpacket.
322  *
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
325  * size.
326  */
327 static bool rxrpc_validate_data(struct sk_buff *skb)
328 {
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;
333
334         for (;;) {
335                 if (flags & RXRPC_REQUEST_ACK)
336                         __set_bit(sp->nr_subpackets, sp->rx_req_ack);
337                 sp->nr_subpackets++;
338
339                 if (!(flags & RXRPC_JUMBO_PACKET))
340                         break;
341
342                 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
343                         goto protocol_error;
344                 if (flags & RXRPC_LAST_PACKET)
345                         goto protocol_error;
346                 offset += RXRPC_JUMBO_DATALEN;
347                 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
348                         goto protocol_error;
349                 offset += sizeof(struct rxrpc_jumbo_header);
350         }
351
352         if (flags & RXRPC_LAST_PACKET)
353                 sp->rx_flags |= RXRPC_SKB_INCL_LAST;
354         return true;
355
356 protocol_error:
357         return false;
358 }
359
360 /*
361  * Handle reception of a duplicate packet.
362  *
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.
368  *
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).
372  */
373 static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
374                                  bool is_jumbo, bool *_jumbo_bad)
375 {
376         /* Discard normal packets that are duplicates. */
377         if (is_jumbo)
378                 return;
379
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.
383          */
384         if (!*_jumbo_bad) {
385                 call->nr_jumbo_bad++;
386                 *_jumbo_bad = true;
387         }
388 }
389
390 /*
391  * Process a DATA packet, adding the packet to the Rx ring.  The caller's
392  * packet ref must be passed on or discarded.
393  */
394 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
395 {
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 immediate_ack = false, jumbo_bad = false;
402         u8 ack = 0;
403
404         _enter("{%u,%u},{%u,%u}",
405                call->rx_hard_ack, call->rx_top, skb->len, seq0);
406
407         _proto("Rx DATA %%%u { #%u f=%02x n=%u }",
408                sp->hdr.serial, seq0, sp->hdr.flags, sp->nr_subpackets);
409
410         state = READ_ONCE(call->state);
411         if (state >= RXRPC_CALL_COMPLETE) {
412                 rxrpc_free_skb(skb, rxrpc_skb_freed);
413                 return;
414         }
415
416         if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
417                 unsigned long timo = READ_ONCE(call->next_req_timo);
418                 unsigned long now, expect_req_by;
419
420                 if (timo) {
421                         now = jiffies;
422                         expect_req_by = now + timo;
423                         WRITE_ONCE(call->expect_req_by, expect_req_by);
424                         rxrpc_reduce_call_timer(call, expect_req_by, now,
425                                                 rxrpc_timer_set_for_idle);
426                 }
427         }
428
429         rxrpc_inc_stat(call->rxnet, stat_rx_data);
430         if (sp->hdr.flags & RXRPC_REQUEST_ACK)
431                 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
432         if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
433                 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
434
435         spin_lock(&call->input_lock);
436
437         /* Received data implicitly ACKs all of the request packets we sent
438          * when we're acting as a client.
439          */
440         if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
441              state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
442             !rxrpc_receiving_reply(call))
443                 goto unlock;
444
445         hard_ack = READ_ONCE(call->rx_hard_ack);
446
447         nr_subpackets = sp->nr_subpackets;
448         if (nr_subpackets > 1) {
449                 if (call->nr_jumbo_bad > 3) {
450                         ack = RXRPC_ACK_NOSPACE;
451                         ack_serial = serial;
452                         goto ack;
453                 }
454         }
455
456         for (j = 0; j < nr_subpackets; j++) {
457                 rxrpc_serial_t serial = sp->hdr.serial + j;
458                 rxrpc_seq_t seq = seq0 + j;
459                 unsigned int ix = seq & RXRPC_RXTX_BUFF_MASK;
460                 bool terminal = (j == nr_subpackets - 1);
461                 bool last = terminal && (sp->rx_flags & RXRPC_SKB_INCL_LAST);
462                 u8 flags, annotation = j;
463
464                 _proto("Rx DATA+%u %%%u { #%x t=%u l=%u }",
465                      j, serial, seq, terminal, last);
466
467                 if (last) {
468                         if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
469                             seq != call->rx_top) {
470                                 rxrpc_proto_abort("LSN", call, seq);
471                                 goto unlock;
472                         }
473                 } else {
474                         if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
475                             after_eq(seq, call->rx_top)) {
476                                 rxrpc_proto_abort("LSA", call, seq);
477                                 goto unlock;
478                         }
479                 }
480
481                 flags = 0;
482                 if (last)
483                         flags |= RXRPC_LAST_PACKET;
484                 if (!terminal)
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);
489
490                 if (before_eq(seq, hard_ack)) {
491                         ack = RXRPC_ACK_DUPLICATE;
492                         ack_serial = serial;
493                         continue;
494                 }
495
496                 if (call->rxtx_buffer[ix]) {
497                         rxrpc_input_dup_data(call, seq, nr_subpackets > 1,
498                                              &jumbo_bad);
499                         if (ack != RXRPC_ACK_DUPLICATE) {
500                                 ack = RXRPC_ACK_DUPLICATE;
501                                 ack_serial = serial;
502                         }
503                         immediate_ack = true;
504                         continue;
505                 }
506
507                 if (after(seq, hard_ack + call->rx_winsize)) {
508                         ack = RXRPC_ACK_EXCEEDS_WINDOW;
509                         ack_serial = serial;
510                         if (flags & RXRPC_JUMBO_PACKET) {
511                                 if (!jumbo_bad) {
512                                         call->nr_jumbo_bad++;
513                                         jumbo_bad = true;
514                                 }
515                         }
516
517                         goto ack;
518                 }
519
520                 if (flags & RXRPC_REQUEST_ACK && !ack) {
521                         ack = RXRPC_ACK_REQUESTED;
522                         ack_serial = serial;
523                 }
524
525                 if (after(seq0, call->ackr_highest_seq))
526                         call->ackr_highest_seq = seq0;
527
528                 /* Queue the packet.  We use a couple of memory barriers here as need
529                  * to make sure that rx_top is perceived to be set after the buffer
530                  * pointer and that the buffer pointer is set after the annotation and
531                  * the skb data.
532                  *
533                  * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
534                  * and also rxrpc_fill_out_ack().
535                  */
536                 if (!terminal)
537                         rxrpc_get_skb(skb, rxrpc_skb_got);
538                 call->rxtx_annotations[ix] = annotation;
539                 smp_wmb();
540                 call->rxtx_buffer[ix] = skb;
541                 if (after(seq, call->rx_top)) {
542                         smp_store_release(&call->rx_top, seq);
543                 } else if (before(seq, call->rx_top)) {
544                         /* Send an immediate ACK if we fill in a hole */
545                         if (!ack) {
546                                 ack = RXRPC_ACK_DELAY;
547                                 ack_serial = serial;
548                         }
549                         immediate_ack = true;
550                 }
551
552                 if (terminal) {
553                         /* From this point on, we're not allowed to touch the
554                          * packet any longer as its ref now belongs to the Rx
555                          * ring.
556                          */
557                         skb = NULL;
558                         sp = NULL;
559                 }
560
561                 nr_unacked++;
562
563                 if (last) {
564                         set_bit(RXRPC_CALL_RX_LAST, &call->flags);
565                         if (!ack) {
566                                 ack = RXRPC_ACK_DELAY;
567                                 ack_serial = serial;
568                         }
569                         trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
570                 } else {
571                         trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
572                 }
573
574                 if (after_eq(seq, call->rx_expect_next)) {
575                         if (after(seq, call->rx_expect_next)) {
576                                 _net("OOS %u > %u", seq, call->rx_expect_next);
577                                 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
578                                 ack_serial = serial;
579                         }
580                         call->rx_expect_next = seq + 1;
581                 }
582                 if (!ack)
583                         ack_serial = serial;
584         }
585
586 ack:
587         if (atomic_add_return(nr_unacked, &call->ackr_nr_unacked) > 2 && !ack)
588                 ack = RXRPC_ACK_IDLE;
589
590         if (ack)
591                 rxrpc_propose_ACK(call, ack, ack_serial,
592                                   immediate_ack, true,
593                                   rxrpc_propose_ack_input_data);
594         else
595                 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, serial,
596                                   false, true,
597                                   rxrpc_propose_ack_input_data);
598
599         trace_rxrpc_notify_socket(call->debug_id, serial);
600         rxrpc_notify_socket(call);
601
602 unlock:
603         spin_unlock(&call->input_lock);
604         rxrpc_free_skb(skb, rxrpc_skb_freed);
605         _leave(" [queued]");
606 }
607
608 /*
609  * See if there's a cached RTT probe to complete.
610  */
611 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
612                                      ktime_t resp_time,
613                                      rxrpc_serial_t acked_serial,
614                                      rxrpc_serial_t ack_serial,
615                                      enum rxrpc_rtt_rx_trace type)
616 {
617         rxrpc_serial_t orig_serial;
618         unsigned long avail;
619         ktime_t sent_at;
620         bool matched = false;
621         int i;
622
623         avail = READ_ONCE(call->rtt_avail);
624         smp_rmb(); /* Read avail bits before accessing data. */
625
626         for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
627                 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
628                         continue;
629
630                 sent_at = call->rtt_sent_at[i];
631                 orig_serial = call->rtt_serial[i];
632
633                 if (orig_serial == acked_serial) {
634                         clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
635                         smp_mb(); /* Read data before setting avail bit */
636                         set_bit(i, &call->rtt_avail);
637                         if (type != rxrpc_rtt_rx_cancel)
638                                 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
639                                                    sent_at, resp_time);
640                         else
641                                 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
642                                                    orig_serial, acked_serial, 0, 0);
643                         matched = true;
644                 }
645
646                 /* If a later serial is being acked, then mark this slot as
647                  * being available.
648                  */
649                 if (after(acked_serial, orig_serial)) {
650                         trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
651                                            orig_serial, acked_serial, 0, 0);
652                         clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
653                         smp_wmb();
654                         set_bit(i, &call->rtt_avail);
655                 }
656         }
657
658         if (!matched)
659                 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
660 }
661
662 /*
663  * Process the response to a ping that we sent to find out if we lost an ACK.
664  *
665  * If we got back a ping response that indicates a lower tx_top than what we
666  * had at the time of the ping transmission, we adjudge all the DATA packets
667  * sent between the response tx_top and the ping-time tx_top to have been lost.
668  */
669 static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
670 {
671         rxrpc_seq_t top, bottom, seq;
672         bool resend = false;
673
674         spin_lock_bh(&call->lock);
675
676         bottom = call->tx_hard_ack + 1;
677         top = call->acks_lost_top;
678         if (before(bottom, top)) {
679                 for (seq = bottom; before_eq(seq, top); seq++) {
680                         int ix = seq & RXRPC_RXTX_BUFF_MASK;
681                         u8 annotation = call->rxtx_annotations[ix];
682                         u8 anno_type = annotation & RXRPC_TX_ANNO_MASK;
683
684                         if (anno_type != RXRPC_TX_ANNO_UNACK)
685                                 continue;
686                         annotation &= ~RXRPC_TX_ANNO_MASK;
687                         annotation |= RXRPC_TX_ANNO_RETRANS;
688                         call->rxtx_annotations[ix] = annotation;
689                         resend = true;
690                 }
691         }
692
693         spin_unlock_bh(&call->lock);
694
695         if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
696                 rxrpc_queue_call(call);
697 }
698
699 /*
700  * Process a ping response.
701  */
702 static void rxrpc_input_ping_response(struct rxrpc_call *call,
703                                       ktime_t resp_time,
704                                       rxrpc_serial_t acked_serial,
705                                       rxrpc_serial_t ack_serial)
706 {
707         if (acked_serial == call->acks_lost_ping)
708                 rxrpc_input_check_for_lost_ack(call);
709 }
710
711 /*
712  * Process the extra information that may be appended to an ACK packet
713  */
714 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
715                                 struct rxrpc_ackinfo *ackinfo)
716 {
717         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
718         struct rxrpc_peer *peer;
719         unsigned int mtu;
720         bool wake = false;
721         u32 rwind = ntohl(ackinfo->rwind);
722
723         _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
724                sp->hdr.serial,
725                ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
726                rwind, ntohl(ackinfo->jumbo_max));
727
728         if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
729                 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
730         if (call->tx_winsize != rwind) {
731                 if (rwind > call->tx_winsize)
732                         wake = true;
733                 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
734                 call->tx_winsize = rwind;
735         }
736
737         if (call->cong_ssthresh > rwind)
738                 call->cong_ssthresh = rwind;
739
740         mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
741
742         peer = call->peer;
743         if (mtu < peer->maxdata) {
744                 spin_lock_bh(&peer->lock);
745                 peer->maxdata = mtu;
746                 peer->mtu = mtu + peer->hdrsize;
747                 spin_unlock_bh(&peer->lock);
748                 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
749         }
750
751         if (wake)
752                 wake_up(&call->waitq);
753 }
754
755 /*
756  * Process individual soft ACKs.
757  *
758  * Each ACK in the array corresponds to one packet and can be either an ACK or
759  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
760  * packets that lie beyond the end of the ACK list are scheduled for resend by
761  * the timer on the basis that the peer might just not have processed them at
762  * the time the ACK was sent.
763  */
764 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
765                                   rxrpc_seq_t seq, int nr_acks,
766                                   struct rxrpc_ack_summary *summary)
767 {
768         int ix;
769         u8 annotation, anno_type;
770
771         for (; nr_acks > 0; nr_acks--, seq++) {
772                 ix = seq & RXRPC_RXTX_BUFF_MASK;
773                 annotation = call->rxtx_annotations[ix];
774                 anno_type = annotation & RXRPC_TX_ANNO_MASK;
775                 annotation &= ~RXRPC_TX_ANNO_MASK;
776                 switch (*acks++) {
777                 case RXRPC_ACK_TYPE_ACK:
778                         summary->nr_acks++;
779                         if (anno_type == RXRPC_TX_ANNO_ACK)
780                                 continue;
781                         summary->nr_new_acks++;
782                         call->rxtx_annotations[ix] =
783                                 RXRPC_TX_ANNO_ACK | annotation;
784                         break;
785                 case RXRPC_ACK_TYPE_NACK:
786                         if (!summary->nr_nacks &&
787                             call->acks_lowest_nak != seq) {
788                                 call->acks_lowest_nak = seq;
789                                 summary->new_low_nack = true;
790                         }
791                         summary->nr_nacks++;
792                         if (anno_type == RXRPC_TX_ANNO_NAK)
793                                 continue;
794                         summary->nr_new_nacks++;
795                         if (anno_type == RXRPC_TX_ANNO_RETRANS)
796                                 continue;
797                         call->rxtx_annotations[ix] =
798                                 RXRPC_TX_ANNO_NAK | annotation;
799                         break;
800                 default:
801                         return rxrpc_proto_abort("SFT", call, 0);
802                 }
803         }
804 }
805
806 /*
807  * Return true if the ACK is valid - ie. it doesn't appear to have regressed
808  * with respect to the ack state conveyed by preceding ACKs.
809  */
810 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
811                                rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
812 {
813         rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
814
815         if (after(first_pkt, base))
816                 return true; /* The window advanced */
817
818         if (before(first_pkt, base))
819                 return false; /* firstPacket regressed */
820
821         if (after_eq(prev_pkt, call->acks_prev_seq))
822                 return true; /* previousPacket hasn't regressed. */
823
824         /* Some rx implementations put a serial number in previousPacket. */
825         if (after_eq(prev_pkt, base + call->tx_winsize))
826                 return false;
827         return true;
828 }
829
830 /*
831  * Process an ACK packet.
832  *
833  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
834  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
835  *
836  * A hard-ACK means that a packet has been processed and may be discarded; a
837  * soft-ACK means that the packet may be discarded and retransmission
838  * requested.  A phase is complete when all packets are hard-ACK'd.
839  */
840 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
841 {
842         struct rxrpc_ack_summary summary = { 0 };
843         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
844         union {
845                 struct rxrpc_ackpacket ack;
846                 struct rxrpc_ackinfo info;
847                 u8 acks[RXRPC_MAXACKS];
848         } buf;
849         rxrpc_serial_t ack_serial, acked_serial;
850         rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
851         int nr_acks, offset, ioffset;
852
853         _enter("");
854
855         offset = sizeof(struct rxrpc_wire_header);
856         if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
857                 _debug("extraction failure");
858                 return rxrpc_proto_abort("XAK", call, 0);
859         }
860         offset += sizeof(buf.ack);
861
862         ack_serial = sp->hdr.serial;
863         acked_serial = ntohl(buf.ack.serial);
864         first_soft_ack = ntohl(buf.ack.firstPacket);
865         prev_pkt = ntohl(buf.ack.previousPacket);
866         hard_ack = first_soft_ack - 1;
867         nr_acks = buf.ack.nAcks;
868         summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
869                               buf.ack.reason : RXRPC_ACK__INVALID);
870
871         trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
872                            first_soft_ack, prev_pkt,
873                            summary.ack_reason, nr_acks);
874         rxrpc_inc_stat(call->rxnet, stat_rx_acks[buf.ack.reason]);
875
876         switch (buf.ack.reason) {
877         case RXRPC_ACK_PING_RESPONSE:
878                 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
879                                           ack_serial);
880                 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
881                                          rxrpc_rtt_rx_ping_response);
882                 break;
883         case RXRPC_ACK_REQUESTED:
884                 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
885                                          rxrpc_rtt_rx_requested_ack);
886                 break;
887         default:
888                 if (acked_serial != 0)
889                         rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
890                                                  rxrpc_rtt_rx_cancel);
891                 break;
892         }
893
894         if (buf.ack.reason == RXRPC_ACK_PING) {
895                 _proto("Rx ACK %%%u PING Request", ack_serial);
896                 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
897                                   ack_serial, true, true,
898                                   rxrpc_propose_ack_respond_to_ping);
899         } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
900                 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
901                                   ack_serial, true, true,
902                                   rxrpc_propose_ack_respond_to_ack);
903         }
904
905         /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
906          * indicates that the client address changed due to NAT.  The server
907          * lost the call because it switched to a different peer.
908          */
909         if (unlikely(buf.ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
910             first_soft_ack == 1 &&
911             prev_pkt == 0 &&
912             rxrpc_is_client_call(call)) {
913                 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
914                                           0, -ENETRESET);
915                 return;
916         }
917
918         /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
919          * indicate a change of address.  However, we can retransmit the call
920          * if we still have it buffered to the beginning.
921          */
922         if (unlikely(buf.ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
923             first_soft_ack == 1 &&
924             prev_pkt == 0 &&
925             call->tx_hard_ack == 0 &&
926             rxrpc_is_client_call(call)) {
927                 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
928                                           0, -ENETRESET);
929                 return;
930         }
931
932         /* Discard any out-of-order or duplicate ACKs (outside lock). */
933         if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
934                 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
935                                            first_soft_ack, call->acks_first_seq,
936                                            prev_pkt, call->acks_prev_seq);
937                 return;
938         }
939
940         buf.info.rxMTU = 0;
941         ioffset = offset + nr_acks + 3;
942         if (skb->len >= ioffset + sizeof(buf.info) &&
943             skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
944                 return rxrpc_proto_abort("XAI", call, 0);
945
946         spin_lock(&call->input_lock);
947
948         /* Discard any out-of-order or duplicate ACKs (inside lock). */
949         if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
950                 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
951                                            first_soft_ack, call->acks_first_seq,
952                                            prev_pkt, call->acks_prev_seq);
953                 goto out;
954         }
955         call->acks_latest_ts = skb->tstamp;
956
957         call->acks_first_seq = first_soft_ack;
958         call->acks_prev_seq = prev_pkt;
959
960         if (buf.ack.reason != RXRPC_ACK_PING &&
961             after(acked_serial, call->acks_highest_serial))
962                 call->acks_highest_serial = acked_serial;
963
964         /* Parse rwind and mtu sizes if provided. */
965         if (buf.info.rxMTU)
966                 rxrpc_input_ackinfo(call, skb, &buf.info);
967
968         if (first_soft_ack == 0) {
969                 rxrpc_proto_abort("AK0", call, 0);
970                 goto out;
971         }
972
973         /* Ignore ACKs unless we are or have just been transmitting. */
974         switch (READ_ONCE(call->state)) {
975         case RXRPC_CALL_CLIENT_SEND_REQUEST:
976         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
977         case RXRPC_CALL_SERVER_SEND_REPLY:
978         case RXRPC_CALL_SERVER_AWAIT_ACK:
979                 break;
980         default:
981                 goto out;
982         }
983
984         if (before(hard_ack, call->tx_hard_ack) ||
985             after(hard_ack, call->tx_top)) {
986                 rxrpc_proto_abort("AKW", call, 0);
987                 goto out;
988         }
989         if (nr_acks > call->tx_top - hard_ack) {
990                 rxrpc_proto_abort("AKN", call, 0);
991                 goto out;
992         }
993
994         if (after(hard_ack, call->tx_hard_ack)) {
995                 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
996                         rxrpc_end_tx_phase(call, false, "ETA");
997                         goto out;
998                 }
999         }
1000
1001         if (nr_acks > 0) {
1002                 if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0) {
1003                         rxrpc_proto_abort("XSA", call, 0);
1004                         goto out;
1005                 }
1006                 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
1007                                       &summary);
1008         }
1009
1010         if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
1011             RXRPC_TX_ANNO_LAST &&
1012             summary.nr_acks == call->tx_top - hard_ack &&
1013             rxrpc_is_client_call(call))
1014                 rxrpc_propose_ACK(call, RXRPC_ACK_PING, ack_serial,
1015                                   false, true,
1016                                   rxrpc_propose_ack_ping_for_lost_reply);
1017
1018         rxrpc_congestion_management(call, skb, &summary, acked_serial);
1019 out:
1020         spin_unlock(&call->input_lock);
1021 }
1022
1023 /*
1024  * Process an ACKALL packet.
1025  */
1026 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1027 {
1028         struct rxrpc_ack_summary summary = { 0 };
1029         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1030
1031         _proto("Rx ACKALL %%%u", sp->hdr.serial);
1032
1033         spin_lock(&call->input_lock);
1034
1035         if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1036                 rxrpc_end_tx_phase(call, false, "ETL");
1037
1038         spin_unlock(&call->input_lock);
1039 }
1040
1041 /*
1042  * Process an ABORT packet directed at a call.
1043  */
1044 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1045 {
1046         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1047         __be32 wtmp;
1048         u32 abort_code = RX_CALL_DEAD;
1049
1050         _enter("");
1051
1052         if (skb->len >= 4 &&
1053             skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1054                           &wtmp, sizeof(wtmp)) >= 0)
1055                 abort_code = ntohl(wtmp);
1056
1057         trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
1058
1059         _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
1060
1061         rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1062                                   abort_code, -ECONNABORTED);
1063 }
1064
1065 /*
1066  * Process an incoming call packet.
1067  */
1068 static void rxrpc_input_call_packet(struct rxrpc_call *call,
1069                                     struct sk_buff *skb)
1070 {
1071         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1072         unsigned long timo;
1073
1074         _enter("%p,%p", call, skb);
1075
1076         timo = READ_ONCE(call->next_rx_timo);
1077         if (timo) {
1078                 unsigned long now = jiffies, expect_rx_by;
1079
1080                 expect_rx_by = now + timo;
1081                 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1082                 rxrpc_reduce_call_timer(call, expect_rx_by, now,
1083                                         rxrpc_timer_set_for_normal);
1084         }
1085
1086         switch (sp->hdr.type) {
1087         case RXRPC_PACKET_TYPE_DATA:
1088                 rxrpc_input_data(call, skb);
1089                 goto no_free;
1090
1091         case RXRPC_PACKET_TYPE_ACK:
1092                 rxrpc_input_ack(call, skb);
1093                 break;
1094
1095         case RXRPC_PACKET_TYPE_BUSY:
1096                 _proto("Rx BUSY %%%u", sp->hdr.serial);
1097
1098                 /* Just ignore BUSY packets from the server; the retry and
1099                  * lifespan timers will take care of business.  BUSY packets
1100                  * from the client don't make sense.
1101                  */
1102                 break;
1103
1104         case RXRPC_PACKET_TYPE_ABORT:
1105                 rxrpc_input_abort(call, skb);
1106                 break;
1107
1108         case RXRPC_PACKET_TYPE_ACKALL:
1109                 rxrpc_input_ackall(call, skb);
1110                 break;
1111
1112         default:
1113                 break;
1114         }
1115
1116         rxrpc_free_skb(skb, rxrpc_skb_freed);
1117 no_free:
1118         _leave("");
1119 }
1120
1121 /*
1122  * Handle a new service call on a channel implicitly completing the preceding
1123  * call on that channel.  This does not apply to client conns.
1124  *
1125  * TODO: If callNumber > call_id + 1, renegotiate security.
1126  */
1127 static void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
1128                                           struct rxrpc_connection *conn,
1129                                           struct rxrpc_call *call)
1130 {
1131         switch (READ_ONCE(call->state)) {
1132         case RXRPC_CALL_SERVER_AWAIT_ACK:
1133                 rxrpc_call_completed(call);
1134                 fallthrough;
1135         case RXRPC_CALL_COMPLETE:
1136                 break;
1137         default:
1138                 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
1139                         set_bit(RXRPC_CALL_EV_ABORT, &call->events);
1140                         rxrpc_queue_call(call);
1141                 }
1142                 trace_rxrpc_improper_term(call);
1143                 break;
1144         }
1145
1146         spin_lock(&rx->incoming_lock);
1147         __rxrpc_disconnect_call(conn, call);
1148         spin_unlock(&rx->incoming_lock);
1149 }
1150
1151 /*
1152  * post connection-level events to the connection
1153  * - this includes challenges, responses, some aborts and call terminal packet
1154  *   retransmission.
1155  */
1156 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
1157                                       struct sk_buff *skb)
1158 {
1159         _enter("%p,%p", conn, skb);
1160
1161         skb_queue_tail(&conn->rx_queue, skb);
1162         rxrpc_queue_conn(conn);
1163 }
1164
1165 /*
1166  * post endpoint-level events to the local endpoint
1167  * - this includes debug and version messages
1168  */
1169 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
1170                                        struct sk_buff *skb)
1171 {
1172         _enter("%p,%p", local, skb);
1173
1174         if (rxrpc_get_local_maybe(local)) {
1175                 skb_queue_tail(&local->event_queue, skb);
1176                 rxrpc_queue_local(local);
1177         } else {
1178                 rxrpc_free_skb(skb, rxrpc_skb_freed);
1179         }
1180 }
1181
1182 /*
1183  * put a packet up for transport-level abort
1184  */
1185 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
1186 {
1187         if (rxrpc_get_local_maybe(local)) {
1188                 skb_queue_tail(&local->reject_queue, skb);
1189                 rxrpc_queue_local(local);
1190         } else {
1191                 rxrpc_free_skb(skb, rxrpc_skb_freed);
1192         }
1193 }
1194
1195 /*
1196  * Extract the wire header from a packet and translate the byte order.
1197  */
1198 static noinline
1199 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
1200 {
1201         struct rxrpc_wire_header whdr;
1202
1203         /* dig out the RxRPC connection details */
1204         if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
1205                 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
1206                                       tracepoint_string("bad_hdr"));
1207                 return -EBADMSG;
1208         }
1209
1210         memset(sp, 0, sizeof(*sp));
1211         sp->hdr.epoch           = ntohl(whdr.epoch);
1212         sp->hdr.cid             = ntohl(whdr.cid);
1213         sp->hdr.callNumber      = ntohl(whdr.callNumber);
1214         sp->hdr.seq             = ntohl(whdr.seq);
1215         sp->hdr.serial          = ntohl(whdr.serial);
1216         sp->hdr.flags           = whdr.flags;
1217         sp->hdr.type            = whdr.type;
1218         sp->hdr.userStatus      = whdr.userStatus;
1219         sp->hdr.securityIndex   = whdr.securityIndex;
1220         sp->hdr._rsvd           = ntohs(whdr._rsvd);
1221         sp->hdr.serviceId       = ntohs(whdr.serviceId);
1222         return 0;
1223 }
1224
1225 /*
1226  * handle data received on the local endpoint
1227  * - may be called in interrupt context
1228  *
1229  * [!] Note that as this is called from the encap_rcv hook, the socket is not
1230  * held locked by the caller and nothing prevents sk_user_data on the UDP from
1231  * being cleared in the middle of processing this function.
1232  *
1233  * Called with the RCU read lock held from the IP layer via UDP.
1234  */
1235 int rxrpc_input_packet(struct sock *udp_sk, struct sk_buff *skb)
1236 {
1237         struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
1238         struct rxrpc_connection *conn;
1239         struct rxrpc_channel *chan;
1240         struct rxrpc_call *call = NULL;
1241         struct rxrpc_skb_priv *sp;
1242         struct rxrpc_peer *peer = NULL;
1243         struct rxrpc_sock *rx = NULL;
1244         unsigned int channel;
1245
1246         _enter("%p", udp_sk);
1247
1248         if (unlikely(!local)) {
1249                 kfree_skb(skb);
1250                 return 0;
1251         }
1252         if (skb->tstamp == 0)
1253                 skb->tstamp = ktime_get_real();
1254
1255         rxrpc_new_skb(skb, rxrpc_skb_received);
1256
1257         skb_pull(skb, sizeof(struct udphdr));
1258
1259         /* The UDP protocol already released all skb resources;
1260          * we are free to add our own data there.
1261          */
1262         sp = rxrpc_skb(skb);
1263
1264         /* dig out the RxRPC connection details */
1265         if (rxrpc_extract_header(sp, skb) < 0)
1266                 goto bad_message;
1267
1268         if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
1269                 static int lose;
1270                 if ((lose++ & 7) == 7) {
1271                         trace_rxrpc_rx_lose(sp);
1272                         rxrpc_free_skb(skb, rxrpc_skb_lost);
1273                         return 0;
1274                 }
1275         }
1276
1277         if (skb->tstamp == 0)
1278                 skb->tstamp = ktime_get_real();
1279         trace_rxrpc_rx_packet(sp);
1280
1281         switch (sp->hdr.type) {
1282         case RXRPC_PACKET_TYPE_VERSION:
1283                 if (rxrpc_to_client(sp))
1284                         goto discard;
1285                 rxrpc_post_packet_to_local(local, skb);
1286                 goto out;
1287
1288         case RXRPC_PACKET_TYPE_BUSY:
1289                 if (rxrpc_to_server(sp))
1290                         goto discard;
1291                 fallthrough;
1292         case RXRPC_PACKET_TYPE_ACK:
1293         case RXRPC_PACKET_TYPE_ACKALL:
1294                 if (sp->hdr.callNumber == 0)
1295                         goto bad_message;
1296                 fallthrough;
1297         case RXRPC_PACKET_TYPE_ABORT:
1298                 break;
1299
1300         case RXRPC_PACKET_TYPE_DATA:
1301                 if (sp->hdr.callNumber == 0 ||
1302                     sp->hdr.seq == 0)
1303                         goto bad_message;
1304                 if (!rxrpc_validate_data(skb))
1305                         goto bad_message;
1306
1307                 /* Unshare the packet so that it can be modified for in-place
1308                  * decryption.
1309                  */
1310                 if (sp->hdr.securityIndex != 0) {
1311                         struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
1312                         if (!nskb) {
1313                                 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
1314                                 goto out;
1315                         }
1316
1317                         if (nskb != skb) {
1318                                 rxrpc_eaten_skb(skb, rxrpc_skb_received);
1319                                 skb = nskb;
1320                                 rxrpc_new_skb(skb, rxrpc_skb_unshared);
1321                                 sp = rxrpc_skb(skb);
1322                         }
1323                 }
1324                 break;
1325
1326         case RXRPC_PACKET_TYPE_CHALLENGE:
1327                 if (rxrpc_to_server(sp))
1328                         goto discard;
1329                 break;
1330         case RXRPC_PACKET_TYPE_RESPONSE:
1331                 if (rxrpc_to_client(sp))
1332                         goto discard;
1333                 break;
1334
1335                 /* Packet types 9-11 should just be ignored. */
1336         case RXRPC_PACKET_TYPE_PARAMS:
1337         case RXRPC_PACKET_TYPE_10:
1338         case RXRPC_PACKET_TYPE_11:
1339                 goto discard;
1340
1341         default:
1342                 _proto("Rx Bad Packet Type %u", sp->hdr.type);
1343                 goto bad_message;
1344         }
1345
1346         if (sp->hdr.serviceId == 0)
1347                 goto bad_message;
1348
1349         if (rxrpc_to_server(sp)) {
1350                 /* Weed out packets to services we're not offering.  Packets
1351                  * that would begin a call are explicitly rejected and the rest
1352                  * are just discarded.
1353                  */
1354                 rx = rcu_dereference(local->service);
1355                 if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
1356                             sp->hdr.serviceId != rx->second_service)) {
1357                         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
1358                             sp->hdr.seq == 1)
1359                                 goto unsupported_service;
1360                         goto discard;
1361                 }
1362         }
1363
1364         conn = rxrpc_find_connection_rcu(local, skb, &peer);
1365         if (conn) {
1366                 if (sp->hdr.securityIndex != conn->security_ix)
1367                         goto wrong_security;
1368
1369                 if (sp->hdr.serviceId != conn->service_id) {
1370                         int old_id;
1371
1372                         if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
1373                                 goto reupgrade;
1374                         old_id = cmpxchg(&conn->service_id, conn->params.service_id,
1375                                          sp->hdr.serviceId);
1376
1377                         if (old_id != conn->params.service_id &&
1378                             old_id != sp->hdr.serviceId)
1379                                 goto reupgrade;
1380                 }
1381
1382                 if (sp->hdr.callNumber == 0) {
1383                         /* Connection-level packet */
1384                         _debug("CONN %p {%d}", conn, conn->debug_id);
1385                         rxrpc_post_packet_to_conn(conn, skb);
1386                         goto out;
1387                 }
1388
1389                 if ((int)sp->hdr.serial - (int)conn->hi_serial > 0)
1390                         conn->hi_serial = sp->hdr.serial;
1391
1392                 /* Call-bound packets are routed by connection channel. */
1393                 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
1394                 chan = &conn->channels[channel];
1395
1396                 /* Ignore really old calls */
1397                 if (sp->hdr.callNumber < chan->last_call)
1398                         goto discard;
1399
1400                 if (sp->hdr.callNumber == chan->last_call) {
1401                         if (chan->call ||
1402                             sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
1403                                 goto discard;
1404
1405                         /* For the previous service call, if completed
1406                          * successfully, we discard all further packets.
1407                          */
1408                         if (rxrpc_conn_is_service(conn) &&
1409                             chan->last_type == RXRPC_PACKET_TYPE_ACK)
1410                                 goto discard;
1411
1412                         /* But otherwise we need to retransmit the final packet
1413                          * from data cached in the connection record.
1414                          */
1415                         if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
1416                                 trace_rxrpc_rx_data(chan->call_debug_id,
1417                                                     sp->hdr.seq,
1418                                                     sp->hdr.serial,
1419                                                     sp->hdr.flags, 0);
1420                         rxrpc_post_packet_to_conn(conn, skb);
1421                         goto out;
1422                 }
1423
1424                 call = rcu_dereference(chan->call);
1425
1426                 if (sp->hdr.callNumber > chan->call_id) {
1427                         if (rxrpc_to_client(sp))
1428                                 goto reject_packet;
1429                         if (call)
1430                                 rxrpc_input_implicit_end_call(rx, conn, call);
1431                         call = NULL;
1432                 }
1433
1434                 if (call) {
1435                         if (sp->hdr.serviceId != call->service_id)
1436                                 call->service_id = sp->hdr.serviceId;
1437                         if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1438                                 call->rx_serial = sp->hdr.serial;
1439                         if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1440                                 set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1441                 }
1442         }
1443
1444         if (!call || refcount_read(&call->ref) == 0) {
1445                 if (rxrpc_to_client(sp) ||
1446                     sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
1447                         goto bad_message;
1448                 if (sp->hdr.seq != 1)
1449                         goto discard;
1450                 call = rxrpc_new_incoming_call(local, rx, skb);
1451                 if (!call)
1452                         goto reject_packet;
1453         }
1454
1455         /* Process a call packet; this either discards or passes on the ref
1456          * elsewhere.
1457          */
1458         rxrpc_input_call_packet(call, skb);
1459         goto out;
1460
1461 discard:
1462         rxrpc_free_skb(skb, rxrpc_skb_freed);
1463 out:
1464         trace_rxrpc_rx_done(0, 0);
1465         return 0;
1466
1467 wrong_security:
1468         trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1469                           RXKADINCONSISTENCY, EBADMSG);
1470         skb->priority = RXKADINCONSISTENCY;
1471         goto post_abort;
1472
1473 unsupported_service:
1474         trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1475                           RX_INVALID_OPERATION, EOPNOTSUPP);
1476         skb->priority = RX_INVALID_OPERATION;
1477         goto post_abort;
1478
1479 reupgrade:
1480         trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1481                           RX_PROTOCOL_ERROR, EBADMSG);
1482         goto protocol_error;
1483
1484 bad_message:
1485         trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1486                           RX_PROTOCOL_ERROR, EBADMSG);
1487 protocol_error:
1488         skb->priority = RX_PROTOCOL_ERROR;
1489 post_abort:
1490         skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
1491 reject_packet:
1492         trace_rxrpc_rx_done(skb->mark, skb->priority);
1493         rxrpc_reject_packet(local, skb);
1494         _leave(" [badmsg]");
1495         return 0;
1496 }