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