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