rxrpc: Reinitialise the call ACK and timer state for client reply phase
[linux-block.git] / net / rxrpc / input.c
1 /* RxRPC packet reception
2  *
3  * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/errqueue.h>
18 #include <linux/udp.h>
19 #include <linux/in.h>
20 #include <linux/in6.h>
21 #include <linux/icmp.h>
22 #include <linux/gfp.h>
23 #include <net/sock.h>
24 #include <net/af_rxrpc.h>
25 #include <net/ip.h>
26 #include <net/udp.h>
27 #include <net/net_namespace.h>
28 #include "ar-internal.h"
29
30 static void rxrpc_proto_abort(const char *why,
31                               struct rxrpc_call *call, rxrpc_seq_t seq)
32 {
33         if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, EBADMSG)) {
34                 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
35                 rxrpc_queue_call(call);
36         }
37 }
38
39 /*
40  * Ping the other end to fill our RTT cache and to retrieve the rwind
41  * and MTU parameters.
42  */
43 static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
44                             int skew)
45 {
46         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
47         ktime_t now = skb->tstamp;
48
49         if (call->peer->rtt_usage < 3 ||
50             ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
51                 rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
52                                   true, true,
53                                   rxrpc_propose_ack_ping_for_params);
54 }
55
56 /*
57  * Apply a hard ACK by advancing the Tx window.
58  */
59 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
60 {
61         struct sk_buff *skb, *list = NULL;
62         int ix;
63         u8 annotation;
64
65         spin_lock(&call->lock);
66
67         while (before(call->tx_hard_ack, to)) {
68                 call->tx_hard_ack++;
69                 ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
70                 skb = call->rxtx_buffer[ix];
71                 annotation = call->rxtx_annotations[ix];
72                 rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
73                 call->rxtx_buffer[ix] = NULL;
74                 call->rxtx_annotations[ix] = 0;
75                 skb->next = list;
76                 list = skb;
77
78                 if (annotation & RXRPC_TX_ANNO_LAST)
79                         set_bit(RXRPC_CALL_TX_LAST, &call->flags);
80         }
81
82         spin_unlock(&call->lock);
83
84         trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
85                                     rxrpc_transmit_rotate_last :
86                                     rxrpc_transmit_rotate));
87         wake_up(&call->waitq);
88
89         while (list) {
90                 skb = list;
91                 list = skb->next;
92                 skb->next = NULL;
93                 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
94         }
95 }
96
97 /*
98  * End the transmission phase of a call.
99  *
100  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
101  * or a final ACK packet.
102  */
103 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
104                                const char *abort_why)
105 {
106
107         ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
108
109         write_lock(&call->state_lock);
110
111         switch (call->state) {
112         case RXRPC_CALL_CLIENT_SEND_REQUEST:
113         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
114                 if (reply_begun)
115                         call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
116                 else
117                         call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
118                 break;
119
120         case RXRPC_CALL_SERVER_AWAIT_ACK:
121                 __rxrpc_call_completed(call);
122                 rxrpc_notify_socket(call);
123                 break;
124
125         default:
126                 goto bad_state;
127         }
128
129         write_unlock(&call->state_lock);
130         if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) {
131                 trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
132         } else {
133                 trace_rxrpc_transmit(call, rxrpc_transmit_end);
134         }
135         _leave(" = ok");
136         return true;
137
138 bad_state:
139         write_unlock(&call->state_lock);
140         kdebug("end_tx %s", rxrpc_call_states[call->state]);
141         rxrpc_proto_abort(abort_why, call, call->tx_top);
142         return false;
143 }
144
145 /*
146  * Begin the reply reception phase of a call.
147  */
148 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
149 {
150         rxrpc_seq_t top = READ_ONCE(call->tx_top);
151
152         if (call->ackr_reason) {
153                 spin_lock_bh(&call->lock);
154                 call->ackr_reason = 0;
155                 call->resend_at = call->expire_at;
156                 call->ack_at = call->expire_at;
157                 spin_unlock_bh(&call->lock);
158                 rxrpc_set_timer(call, rxrpc_timer_init_for_reply);
159         }
160
161         if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags))
162                 rxrpc_rotate_tx_window(call, top);
163         if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
164                 rxrpc_proto_abort("TXL", call, top);
165                 return false;
166         }
167         if (!rxrpc_end_tx_phase(call, true, "ETD"))
168                 return false;
169         call->tx_phase = false;
170         return true;
171 }
172
173 /*
174  * Scan a jumbo packet to validate its structure and to work out how many
175  * subpackets it contains.
176  *
177  * A jumbo packet is a collection of consecutive packets glued together with
178  * little headers between that indicate how to change the initial header for
179  * each subpacket.
180  *
181  * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
182  * the last are RXRPC_JUMBO_DATALEN in size.  The last subpacket may be of any
183  * size.
184  */
185 static bool rxrpc_validate_jumbo(struct sk_buff *skb)
186 {
187         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
188         unsigned int offset = sp->offset;
189         unsigned int len = skb->len;
190         int nr_jumbo = 1;
191         u8 flags = sp->hdr.flags;
192
193         do {
194                 nr_jumbo++;
195                 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
196                         goto protocol_error;
197                 if (flags & RXRPC_LAST_PACKET)
198                         goto protocol_error;
199                 offset += RXRPC_JUMBO_DATALEN;
200                 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
201                         goto protocol_error;
202                 offset += sizeof(struct rxrpc_jumbo_header);
203         } while (flags & RXRPC_JUMBO_PACKET);
204
205         sp->nr_jumbo = nr_jumbo;
206         return true;
207
208 protocol_error:
209         return false;
210 }
211
212 /*
213  * Handle reception of a duplicate packet.
214  *
215  * We have to take care to avoid an attack here whereby we're given a series of
216  * jumbograms, each with a sequence number one before the preceding one and
217  * filled up to maximum UDP size.  If they never send us the first packet in
218  * the sequence, they can cause us to have to hold on to around 2MiB of kernel
219  * space until the call times out.
220  *
221  * We limit the space usage by only accepting three duplicate jumbo packets per
222  * call.  After that, we tell the other side we're no longer accepting jumbos
223  * (that information is encoded in the ACK packet).
224  */
225 static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
226                                  u8 annotation, bool *_jumbo_bad)
227 {
228         /* Discard normal packets that are duplicates. */
229         if (annotation == 0)
230                 return;
231
232         /* Skip jumbo subpackets that are duplicates.  When we've had three or
233          * more partially duplicate jumbo packets, we refuse to take any more
234          * jumbos for this call.
235          */
236         if (!*_jumbo_bad) {
237                 call->nr_jumbo_bad++;
238                 *_jumbo_bad = true;
239         }
240 }
241
242 /*
243  * Process a DATA packet, adding the packet to the Rx ring.
244  */
245 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
246                              u16 skew)
247 {
248         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
249         unsigned int offset = sp->offset;
250         unsigned int ix;
251         rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
252         rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
253         bool immediate_ack = false, jumbo_bad = false, queued;
254         u16 len;
255         u8 ack = 0, flags, annotation = 0;
256
257         _enter("{%u,%u},{%u,%u}",
258                call->rx_hard_ack, call->rx_top, skb->len, seq);
259
260         _proto("Rx DATA %%%u { #%u f=%02x }",
261                sp->hdr.serial, seq, sp->hdr.flags);
262
263         if (call->state >= RXRPC_CALL_COMPLETE)
264                 return;
265
266         /* Received data implicitly ACKs all of the request packets we sent
267          * when we're acting as a client.
268          */
269         if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
270              call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
271             !rxrpc_receiving_reply(call))
272                 return;
273
274         call->ackr_prev_seq = seq;
275
276         hard_ack = READ_ONCE(call->rx_hard_ack);
277         if (after(seq, hard_ack + call->rx_winsize)) {
278                 ack = RXRPC_ACK_EXCEEDS_WINDOW;
279                 ack_serial = serial;
280                 goto ack;
281         }
282
283         flags = sp->hdr.flags;
284         if (flags & RXRPC_JUMBO_PACKET) {
285                 if (call->nr_jumbo_bad > 3) {
286                         ack = RXRPC_ACK_NOSPACE;
287                         ack_serial = serial;
288                         goto ack;
289                 }
290                 annotation = 1;
291         }
292
293 next_subpacket:
294         queued = false;
295         ix = seq & RXRPC_RXTX_BUFF_MASK;
296         len = skb->len;
297         if (flags & RXRPC_JUMBO_PACKET)
298                 len = RXRPC_JUMBO_DATALEN;
299
300         if (flags & RXRPC_LAST_PACKET) {
301                 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
302                     seq != call->rx_top)
303                         return rxrpc_proto_abort("LSN", call, seq);
304         } else {
305                 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
306                     after_eq(seq, call->rx_top))
307                         return rxrpc_proto_abort("LSA", call, seq);
308         }
309
310         if (before_eq(seq, hard_ack)) {
311                 ack = RXRPC_ACK_DUPLICATE;
312                 ack_serial = serial;
313                 goto skip;
314         }
315
316         if (flags & RXRPC_REQUEST_ACK && !ack) {
317                 ack = RXRPC_ACK_REQUESTED;
318                 ack_serial = serial;
319         }
320
321         if (call->rxtx_buffer[ix]) {
322                 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
323                 if (ack != RXRPC_ACK_DUPLICATE) {
324                         ack = RXRPC_ACK_DUPLICATE;
325                         ack_serial = serial;
326                 }
327                 immediate_ack = true;
328                 goto skip;
329         }
330
331         /* Queue the packet.  We use a couple of memory barriers here as need
332          * to make sure that rx_top is perceived to be set after the buffer
333          * pointer and that the buffer pointer is set after the annotation and
334          * the skb data.
335          *
336          * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
337          * and also rxrpc_fill_out_ack().
338          */
339         rxrpc_get_skb(skb, rxrpc_skb_rx_got);
340         call->rxtx_annotations[ix] = annotation;
341         smp_wmb();
342         call->rxtx_buffer[ix] = skb;
343         if (after(seq, call->rx_top)) {
344                 smp_store_release(&call->rx_top, seq);
345         } else if (before(seq, call->rx_top)) {
346                 /* Send an immediate ACK if we fill in a hole */
347                 if (!ack) {
348                         ack = RXRPC_ACK_DELAY;
349                         ack_serial = serial;
350                 }
351                 immediate_ack = true;
352         }
353         if (flags & RXRPC_LAST_PACKET) {
354                 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
355                 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
356         } else {
357                 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
358         }
359         queued = true;
360
361         if (after_eq(seq, call->rx_expect_next)) {
362                 if (after(seq, call->rx_expect_next)) {
363                         _net("OOS %u > %u", seq, call->rx_expect_next);
364                         ack = RXRPC_ACK_OUT_OF_SEQUENCE;
365                         ack_serial = serial;
366                 }
367                 call->rx_expect_next = seq + 1;
368         }
369
370 skip:
371         offset += len;
372         if (flags & RXRPC_JUMBO_PACKET) {
373                 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
374                         return rxrpc_proto_abort("XJF", call, seq);
375                 offset += sizeof(struct rxrpc_jumbo_header);
376                 seq++;
377                 serial++;
378                 annotation++;
379                 if (flags & RXRPC_JUMBO_PACKET)
380                         annotation |= RXRPC_RX_ANNO_JLAST;
381                 if (after(seq, hard_ack + call->rx_winsize)) {
382                         ack = RXRPC_ACK_EXCEEDS_WINDOW;
383                         ack_serial = serial;
384                         if (!jumbo_bad) {
385                                 call->nr_jumbo_bad++;
386                                 jumbo_bad = true;
387                         }
388                         goto ack;
389                 }
390
391                 _proto("Rx DATA Jumbo %%%u", serial);
392                 goto next_subpacket;
393         }
394
395         if (queued && flags & RXRPC_LAST_PACKET && !ack) {
396                 ack = RXRPC_ACK_DELAY;
397                 ack_serial = serial;
398         }
399
400 ack:
401         if (ack)
402                 rxrpc_propose_ACK(call, ack, skew, ack_serial,
403                                   immediate_ack, true,
404                                   rxrpc_propose_ack_input_data);
405
406         if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
407                 rxrpc_notify_socket(call);
408         _leave(" [queued]");
409 }
410
411 /*
412  * Process a requested ACK.
413  */
414 static void rxrpc_input_requested_ack(struct rxrpc_call *call,
415                                       ktime_t resp_time,
416                                       rxrpc_serial_t orig_serial,
417                                       rxrpc_serial_t ack_serial)
418 {
419         struct rxrpc_skb_priv *sp;
420         struct sk_buff *skb;
421         ktime_t sent_at;
422         int ix;
423
424         for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
425                 skb = call->rxtx_buffer[ix];
426                 if (!skb)
427                         continue;
428
429                 sp = rxrpc_skb(skb);
430                 if (sp->hdr.serial != orig_serial)
431                         continue;
432                 smp_rmb();
433                 sent_at = skb->tstamp;
434                 goto found;
435         }
436         return;
437
438 found:
439         rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
440                            orig_serial, ack_serial, sent_at, resp_time);
441 }
442
443 /*
444  * Process a ping response.
445  */
446 static void rxrpc_input_ping_response(struct rxrpc_call *call,
447                                       ktime_t resp_time,
448                                       rxrpc_serial_t orig_serial,
449                                       rxrpc_serial_t ack_serial)
450 {
451         rxrpc_serial_t ping_serial;
452         ktime_t ping_time;
453
454         ping_time = call->ackr_ping_time;
455         smp_rmb();
456         ping_serial = call->ackr_ping;
457
458         if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
459             before(orig_serial, ping_serial))
460                 return;
461         clear_bit(RXRPC_CALL_PINGING, &call->flags);
462         if (after(orig_serial, ping_serial))
463                 return;
464
465         rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
466                            orig_serial, ack_serial, ping_time, resp_time);
467 }
468
469 /*
470  * Process the extra information that may be appended to an ACK packet
471  */
472 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
473                                 struct rxrpc_ackinfo *ackinfo)
474 {
475         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
476         struct rxrpc_peer *peer;
477         unsigned int mtu;
478         u32 rwind = ntohl(ackinfo->rwind);
479
480         _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
481                sp->hdr.serial,
482                ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
483                rwind, ntohl(ackinfo->jumbo_max));
484
485         if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
486                 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
487         call->tx_winsize = rwind;
488
489         mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
490
491         peer = call->peer;
492         if (mtu < peer->maxdata) {
493                 spin_lock_bh(&peer->lock);
494                 peer->maxdata = mtu;
495                 peer->mtu = mtu + peer->hdrsize;
496                 spin_unlock_bh(&peer->lock);
497                 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
498         }
499 }
500
501 /*
502  * Process individual soft ACKs.
503  *
504  * Each ACK in the array corresponds to one packet and can be either an ACK or
505  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
506  * packets that lie beyond the end of the ACK list are scheduled for resend by
507  * the timer on the basis that the peer might just not have processed them at
508  * the time the ACK was sent.
509  */
510 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
511                                   rxrpc_seq_t seq, int nr_acks)
512 {
513         bool resend = false;
514         int ix;
515         u8 annotation, anno_type;
516
517         for (; nr_acks > 0; nr_acks--, seq++) {
518                 ix = seq & RXRPC_RXTX_BUFF_MASK;
519                 annotation = call->rxtx_annotations[ix];
520                 anno_type = annotation & RXRPC_TX_ANNO_MASK;
521                 annotation &= ~RXRPC_TX_ANNO_MASK;
522                 switch (*acks++) {
523                 case RXRPC_ACK_TYPE_ACK:
524                         if (anno_type == RXRPC_TX_ANNO_ACK)
525                                 continue;
526                         call->rxtx_annotations[ix] =
527                                 RXRPC_TX_ANNO_ACK | annotation;
528                         break;
529                 case RXRPC_ACK_TYPE_NACK:
530                         if (anno_type == RXRPC_TX_ANNO_NAK)
531                                 continue;
532                         if (anno_type == RXRPC_TX_ANNO_RETRANS)
533                                 continue;
534                         call->rxtx_annotations[ix] =
535                                 RXRPC_TX_ANNO_NAK | annotation;
536                         resend = true;
537                         break;
538                 default:
539                         return rxrpc_proto_abort("SFT", call, 0);
540                 }
541         }
542
543         if (resend &&
544             !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
545                 rxrpc_queue_call(call);
546 }
547
548 /*
549  * Process an ACK packet.
550  *
551  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
552  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
553  *
554  * A hard-ACK means that a packet has been processed and may be discarded; a
555  * soft-ACK means that the packet may be discarded and retransmission
556  * requested.  A phase is complete when all packets are hard-ACK'd.
557  */
558 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
559                             u16 skew)
560 {
561         u8 ack_reason;
562         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
563         union {
564                 struct rxrpc_ackpacket ack;
565                 struct rxrpc_ackinfo info;
566                 u8 acks[RXRPC_MAXACKS];
567         } buf;
568         rxrpc_serial_t acked_serial;
569         rxrpc_seq_t first_soft_ack, hard_ack;
570         int nr_acks, offset;
571
572         _enter("");
573
574         if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
575                 _debug("extraction failure");
576                 return rxrpc_proto_abort("XAK", call, 0);
577         }
578         sp->offset += sizeof(buf.ack);
579
580         acked_serial = ntohl(buf.ack.serial);
581         first_soft_ack = ntohl(buf.ack.firstPacket);
582         hard_ack = first_soft_ack - 1;
583         nr_acks = buf.ack.nAcks;
584         ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
585                       buf.ack.reason : RXRPC_ACK__INVALID);
586
587         trace_rxrpc_rx_ack(call, first_soft_ack, ack_reason, nr_acks);
588
589         _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
590                sp->hdr.serial,
591                ntohs(buf.ack.maxSkew),
592                first_soft_ack,
593                ntohl(buf.ack.previousPacket),
594                acked_serial,
595                rxrpc_ack_names[ack_reason],
596                buf.ack.nAcks);
597
598         if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
599                 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
600                                           sp->hdr.serial);
601         if (buf.ack.reason == RXRPC_ACK_REQUESTED)
602                 rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
603                                           sp->hdr.serial);
604
605         if (buf.ack.reason == RXRPC_ACK_PING) {
606                 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
607                 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
608                                   skew, sp->hdr.serial, true, true,
609                                   rxrpc_propose_ack_respond_to_ping);
610         } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
611                 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
612                                   skew, sp->hdr.serial, true, true,
613                                   rxrpc_propose_ack_respond_to_ack);
614         }
615
616         offset = sp->offset + nr_acks + 3;
617         if (skb->len >= offset + sizeof(buf.info)) {
618                 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
619                         return rxrpc_proto_abort("XAI", call, 0);
620                 rxrpc_input_ackinfo(call, skb, &buf.info);
621         }
622
623         if (first_soft_ack == 0)
624                 return rxrpc_proto_abort("AK0", call, 0);
625
626         /* Ignore ACKs unless we are or have just been transmitting. */
627         switch (call->state) {
628         case RXRPC_CALL_CLIENT_SEND_REQUEST:
629         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
630         case RXRPC_CALL_SERVER_SEND_REPLY:
631         case RXRPC_CALL_SERVER_AWAIT_ACK:
632                 break;
633         default:
634                 return;
635         }
636
637         /* Discard any out-of-order or duplicate ACKs. */
638         if (before_eq(sp->hdr.serial, call->acks_latest)) {
639                 _debug("discard ACK %d <= %d",
640                        sp->hdr.serial, call->acks_latest);
641                 return;
642         }
643         call->acks_latest = sp->hdr.serial;
644
645         if (before(hard_ack, call->tx_hard_ack) ||
646             after(hard_ack, call->tx_top))
647                 return rxrpc_proto_abort("AKW", call, 0);
648         if (nr_acks > call->tx_top - hard_ack)
649                 return rxrpc_proto_abort("AKN", call, 0);
650
651         if (after(hard_ack, call->tx_hard_ack))
652                 rxrpc_rotate_tx_window(call, hard_ack);
653
654         if (nr_acks > 0) {
655                 if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
656                         return rxrpc_proto_abort("XSA", call, 0);
657                 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
658         }
659
660         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
661                 rxrpc_end_tx_phase(call, false, "ETA");
662                 return;
663         }
664
665 }
666
667 /*
668  * Process an ACKALL packet.
669  */
670 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
671 {
672         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
673
674         _proto("Rx ACKALL %%%u", sp->hdr.serial);
675
676         rxrpc_rotate_tx_window(call, call->tx_top);
677         if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
678                 rxrpc_end_tx_phase(call, false, "ETL");
679 }
680
681 /*
682  * Process an ABORT packet.
683  */
684 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
685 {
686         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
687         __be32 wtmp;
688         u32 abort_code = RX_CALL_DEAD;
689
690         _enter("");
691
692         if (skb->len >= 4 &&
693             skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
694                 abort_code = ntohl(wtmp);
695
696         _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
697
698         if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
699                                       abort_code, ECONNABORTED))
700                 rxrpc_notify_socket(call);
701 }
702
703 /*
704  * Process an incoming call packet.
705  */
706 static void rxrpc_input_call_packet(struct rxrpc_call *call,
707                                     struct sk_buff *skb, u16 skew)
708 {
709         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
710
711         _enter("%p,%p", call, skb);
712
713         switch (sp->hdr.type) {
714         case RXRPC_PACKET_TYPE_DATA:
715                 rxrpc_input_data(call, skb, skew);
716                 break;
717
718         case RXRPC_PACKET_TYPE_ACK:
719                 rxrpc_input_ack(call, skb, skew);
720                 break;
721
722         case RXRPC_PACKET_TYPE_BUSY:
723                 _proto("Rx BUSY %%%u", sp->hdr.serial);
724
725                 /* Just ignore BUSY packets from the server; the retry and
726                  * lifespan timers will take care of business.  BUSY packets
727                  * from the client don't make sense.
728                  */
729                 break;
730
731         case RXRPC_PACKET_TYPE_ABORT:
732                 rxrpc_input_abort(call, skb);
733                 break;
734
735         case RXRPC_PACKET_TYPE_ACKALL:
736                 rxrpc_input_ackall(call, skb);
737                 break;
738
739         default:
740                 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
741                 break;
742         }
743
744         _leave("");
745 }
746
747 /*
748  * post connection-level events to the connection
749  * - this includes challenges, responses, some aborts and call terminal packet
750  *   retransmission.
751  */
752 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
753                                       struct sk_buff *skb)
754 {
755         _enter("%p,%p", conn, skb);
756
757         skb_queue_tail(&conn->rx_queue, skb);
758         rxrpc_queue_conn(conn);
759 }
760
761 /*
762  * post endpoint-level events to the local endpoint
763  * - this includes debug and version messages
764  */
765 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
766                                        struct sk_buff *skb)
767 {
768         _enter("%p,%p", local, skb);
769
770         skb_queue_tail(&local->event_queue, skb);
771         rxrpc_queue_local(local);
772 }
773
774 /*
775  * put a packet up for transport-level abort
776  */
777 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
778 {
779         CHECK_SLAB_OKAY(&local->usage);
780
781         skb_queue_tail(&local->reject_queue, skb);
782         rxrpc_queue_local(local);
783 }
784
785 /*
786  * Extract the wire header from a packet and translate the byte order.
787  */
788 static noinline
789 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
790 {
791         struct rxrpc_wire_header whdr;
792
793         /* dig out the RxRPC connection details */
794         if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
795                 return -EBADMSG;
796
797         memset(sp, 0, sizeof(*sp));
798         sp->hdr.epoch           = ntohl(whdr.epoch);
799         sp->hdr.cid             = ntohl(whdr.cid);
800         sp->hdr.callNumber      = ntohl(whdr.callNumber);
801         sp->hdr.seq             = ntohl(whdr.seq);
802         sp->hdr.serial          = ntohl(whdr.serial);
803         sp->hdr.flags           = whdr.flags;
804         sp->hdr.type            = whdr.type;
805         sp->hdr.userStatus      = whdr.userStatus;
806         sp->hdr.securityIndex   = whdr.securityIndex;
807         sp->hdr._rsvd           = ntohs(whdr._rsvd);
808         sp->hdr.serviceId       = ntohs(whdr.serviceId);
809         sp->offset = sizeof(whdr);
810         return 0;
811 }
812
813 /*
814  * handle data received on the local endpoint
815  * - may be called in interrupt context
816  *
817  * The socket is locked by the caller and this prevents the socket from being
818  * shut down and the local endpoint from going away, thus sk_user_data will not
819  * be cleared until this function returns.
820  */
821 void rxrpc_data_ready(struct sock *udp_sk)
822 {
823         struct rxrpc_connection *conn;
824         struct rxrpc_channel *chan;
825         struct rxrpc_call *call;
826         struct rxrpc_skb_priv *sp;
827         struct rxrpc_local *local = udp_sk->sk_user_data;
828         struct sk_buff *skb;
829         unsigned int channel;
830         int ret, skew;
831
832         _enter("%p", udp_sk);
833
834         ASSERT(!irqs_disabled());
835
836         skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
837         if (!skb) {
838                 if (ret == -EAGAIN)
839                         return;
840                 _debug("UDP socket error %d", ret);
841                 return;
842         }
843
844         rxrpc_new_skb(skb, rxrpc_skb_rx_received);
845
846         _net("recv skb %p", skb);
847
848         /* we'll probably need to checksum it (didn't call sock_recvmsg) */
849         if (skb_checksum_complete(skb)) {
850                 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
851                 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
852                 _leave(" [CSUM failed]");
853                 return;
854         }
855
856         __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
857
858         /* The socket buffer we have is owned by UDP, with UDP's data all over
859          * it, but we really want our own data there.
860          */
861         skb_orphan(skb);
862         sp = rxrpc_skb(skb);
863
864         /* dig out the RxRPC connection details */
865         if (rxrpc_extract_header(sp, skb) < 0)
866                 goto bad_message;
867
868         if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
869                 static int lose;
870                 if ((lose++ & 7) == 7) {
871                         trace_rxrpc_rx_lose(sp);
872                         rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
873                         return;
874                 }
875         }
876
877         trace_rxrpc_rx_packet(sp);
878
879         _net("Rx RxRPC %s ep=%x call=%x:%x",
880              sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
881              sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
882
883         if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
884             !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
885                 _proto("Rx Bad Packet Type %u", sp->hdr.type);
886                 goto bad_message;
887         }
888
889         switch (sp->hdr.type) {
890         case RXRPC_PACKET_TYPE_VERSION:
891                 rxrpc_post_packet_to_local(local, skb);
892                 goto out;
893
894         case RXRPC_PACKET_TYPE_BUSY:
895                 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
896                         goto discard;
897
898         case RXRPC_PACKET_TYPE_DATA:
899                 if (sp->hdr.callNumber == 0)
900                         goto bad_message;
901                 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
902                     !rxrpc_validate_jumbo(skb))
903                         goto bad_message;
904                 break;
905         }
906
907         rcu_read_lock();
908
909         conn = rxrpc_find_connection_rcu(local, skb);
910         if (conn) {
911                 if (sp->hdr.securityIndex != conn->security_ix)
912                         goto wrong_security;
913
914                 if (sp->hdr.callNumber == 0) {
915                         /* Connection-level packet */
916                         _debug("CONN %p {%d}", conn, conn->debug_id);
917                         rxrpc_post_packet_to_conn(conn, skb);
918                         goto out_unlock;
919                 }
920
921                 /* Note the serial number skew here */
922                 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
923                 if (skew >= 0) {
924                         if (skew > 0)
925                                 conn->hi_serial = sp->hdr.serial;
926                 } else {
927                         skew = -skew;
928                         skew = min(skew, 65535);
929                 }
930
931                 /* Call-bound packets are routed by connection channel. */
932                 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
933                 chan = &conn->channels[channel];
934
935                 /* Ignore really old calls */
936                 if (sp->hdr.callNumber < chan->last_call)
937                         goto discard_unlock;
938
939                 if (sp->hdr.callNumber == chan->last_call) {
940                         /* For the previous service call, if completed successfully, we
941                          * discard all further packets.
942                          */
943                         if (rxrpc_conn_is_service(conn) &&
944                             (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
945                              sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
946                                 goto discard_unlock;
947
948                         /* But otherwise we need to retransmit the final packet from
949                          * data cached in the connection record.
950                          */
951                         rxrpc_post_packet_to_conn(conn, skb);
952                         goto out_unlock;
953                 }
954
955                 call = rcu_dereference(chan->call);
956         } else {
957                 skew = 0;
958                 call = NULL;
959         }
960
961         if (!call || atomic_read(&call->usage) == 0) {
962                 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
963                     sp->hdr.callNumber == 0 ||
964                     sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
965                         goto bad_message_unlock;
966                 if (sp->hdr.seq != 1)
967                         goto discard_unlock;
968                 call = rxrpc_new_incoming_call(local, conn, skb);
969                 if (!call) {
970                         rcu_read_unlock();
971                         goto reject_packet;
972                 }
973                 rxrpc_send_ping(call, skb, skew);
974         }
975
976         rxrpc_input_call_packet(call, skb, skew);
977         goto discard_unlock;
978
979 discard_unlock:
980         rcu_read_unlock();
981 discard:
982         rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
983 out:
984         trace_rxrpc_rx_done(0, 0);
985         return;
986
987 out_unlock:
988         rcu_read_unlock();
989         goto out;
990
991 wrong_security:
992         rcu_read_unlock();
993         trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
994                           RXKADINCONSISTENCY, EBADMSG);
995         skb->priority = RXKADINCONSISTENCY;
996         goto post_abort;
997
998 bad_message_unlock:
999         rcu_read_unlock();
1000 bad_message:
1001         trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1002                           RX_PROTOCOL_ERROR, EBADMSG);
1003         skb->priority = RX_PROTOCOL_ERROR;
1004 post_abort:
1005         skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
1006 reject_packet:
1007         trace_rxrpc_rx_done(skb->mark, skb->priority);
1008         rxrpc_reject_packet(local, skb);
1009         _leave(" [badmsg]");
1010 }