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