rxrpc: Add a tracepoint to log injected Rx packet loss
[linux-2.6-block.git] / net / rxrpc / input.c
CommitLineData
17926a79
DH
1/* RxRPC packet reception
2 *
248f219c 3 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
17926a79
DH
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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
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>
5a0e3ad6 22#include <linux/gfp.h>
17926a79
DH
23#include <net/sock.h>
24#include <net/af_rxrpc.h>
25#include <net/ip.h>
1781f7f5 26#include <net/udp.h>
0283328e 27#include <net/net_namespace.h>
17926a79
DH
28#include "ar-internal.h"
29
248f219c
DH
30static 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
8e83134d
DH
39/*
40 * Ping the other end to fill our RTT cache and to retrieve the rwind
41 * and MTU parameters.
42 */
43static 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);
fc943f67 47 ktime_t now = skb->tstamp;
8e83134d 48
fc943f67
DH
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);
8e83134d
DH
53}
54
17926a79 55/*
248f219c 56 * Apply a hard ACK by advancing the Tx window.
17926a79 57 */
248f219c 58static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
17926a79 59{
248f219c
DH
60 struct sk_buff *skb, *list = NULL;
61 int ix;
70790dbe 62 u8 annotation;
17926a79 63
248f219c 64 spin_lock(&call->lock);
17926a79 65
248f219c
DH
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];
70790dbe 70 annotation = call->rxtx_annotations[ix];
71f3ca40 71 rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
248f219c
DH
72 call->rxtx_buffer[ix] = NULL;
73 call->rxtx_annotations[ix] = 0;
74 skb->next = list;
75 list = skb;
70790dbe
DH
76
77 if (annotation & RXRPC_TX_ANNO_LAST)
78 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
248f219c 79 }
17926a79 80
248f219c 81 spin_unlock(&call->lock);
17926a79 82
70790dbe
DH
83 trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
84 rxrpc_transmit_rotate_last :
85 rxrpc_transmit_rotate));
bc4abfcf
DH
86 wake_up(&call->waitq);
87
248f219c
DH
88 while (list) {
89 skb = list;
90 list = skb->next;
91 skb->next = NULL;
71f3ca40 92 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
17926a79 93 }
248f219c 94}
17926a79 95
248f219c
DH
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 */
70790dbe
DH
102static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
103 const char *abort_why)
248f219c 104{
17926a79 105
70790dbe 106 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
17926a79 107
248f219c 108 write_lock(&call->state_lock);
651350d1 109
248f219c 110 switch (call->state) {
70790dbe 111 case RXRPC_CALL_CLIENT_SEND_REQUEST:
248f219c 112 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
70790dbe
DH
113 if (reply_begun)
114 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
115 else
116 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
248f219c 117 break;
70790dbe 118
248f219c
DH
119 case RXRPC_CALL_SERVER_AWAIT_ACK:
120 __rxrpc_call_completed(call);
121 rxrpc_notify_socket(call);
122 break;
70790dbe
DH
123
124 default:
125 goto bad_state;
17926a79 126 }
17926a79 127
248f219c 128 write_unlock(&call->state_lock);
70790dbe
DH
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 }
248f219c
DH
134 _leave(" = ok");
135 return true;
70790dbe
DH
136
137bad_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 */
147static 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;
248f219c
DH
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 */
175static bool rxrpc_validate_jumbo(struct sk_buff *skb)
176{
177 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
178 unsigned int offset = sp->offset;
89a80ed4 179 unsigned int len = skb->len;
248f219c
DH
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;
17926a79 197
248f219c
DH
198protocol_error:
199 return false;
17926a79
DH
200}
201
202/*
248f219c
DH
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).
17926a79 214 */
248f219c 215static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
75e42126 216 u8 annotation, bool *_jumbo_bad)
17926a79 217{
248f219c
DH
218 /* Discard normal packets that are duplicates. */
219 if (annotation == 0)
220 return;
17926a79 221
248f219c
DH
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 */
75e42126
DH
226 if (!*_jumbo_bad) {
227 call->nr_jumbo_bad++;
228 *_jumbo_bad = true;
248f219c
DH
229 }
230}
17926a79 231
248f219c
DH
232/*
233 * Process a DATA packet, adding the packet to the Rx ring.
234 */
235static 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;
75e42126 243 bool immediate_ack = false, jumbo_bad = false, queued;
248f219c
DH
244 u16 len;
245 u8 ack = 0, flags, annotation = 0;
17926a79 246
248f219c 247 _enter("{%u,%u},{%u,%u}",
89a80ed4 248 call->rx_hard_ack, call->rx_top, skb->len, seq);
17926a79 249
248f219c
DH
250 _proto("Rx DATA %%%u { #%u f=%02x }",
251 sp->hdr.serial, seq, sp->hdr.flags);
17926a79 252
248f219c
DH
253 if (call->state >= RXRPC_CALL_COMPLETE)
254 return;
17926a79 255
248f219c
DH
256 /* Received data implicitly ACKs all of the request packets we sent
257 * when we're acting as a client.
258 */
70790dbe
DH
259 if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
260 call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
261 !rxrpc_receiving_reply(call))
248f219c 262 return;
17926a79 263
248f219c 264 call->ackr_prev_seq = seq;
17926a79 265
248f219c
DH
266 hard_ack = READ_ONCE(call->rx_hard_ack);
267 if (after(seq, hard_ack + call->rx_winsize)) {
17926a79 268 ack = RXRPC_ACK_EXCEEDS_WINDOW;
248f219c
DH
269 ack_serial = serial;
270 goto ack;
17926a79
DH
271 }
272
248f219c
DH
273 flags = sp->hdr.flags;
274 if (flags & RXRPC_JUMBO_PACKET) {
75e42126 275 if (call->nr_jumbo_bad > 3) {
248f219c
DH
276 ack = RXRPC_ACK_NOSPACE;
277 ack_serial = serial;
278 goto ack;
17926a79 279 }
248f219c 280 annotation = 1;
17926a79
DH
281 }
282
248f219c
DH
283next_subpacket:
284 queued = false;
285 ix = seq & RXRPC_RXTX_BUFF_MASK;
89a80ed4 286 len = skb->len;
248f219c
DH
287 if (flags & RXRPC_JUMBO_PACKET)
288 len = RXRPC_JUMBO_DATALEN;
289
290 if (flags & RXRPC_LAST_PACKET) {
816c9fce 291 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
248f219c
DH
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);
17926a79
DH
298 }
299
248f219c
DH
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]) {
75e42126 312 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
248f219c
DH
313 if (ack != RXRPC_ACK_DUPLICATE) {
314 ack = RXRPC_ACK_DUPLICATE;
315 ack_serial = serial;
17926a79 316 }
248f219c
DH
317 immediate_ack = true;
318 goto skip;
17926a79
DH
319 }
320
248f219c
DH
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 */
71f3ca40 329 rxrpc_get_skb(skb, rxrpc_skb_rx_got);
248f219c
DH
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);
58dc63c9 335 if (flags & RXRPC_LAST_PACKET) {
816c9fce 336 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
58dc63c9
DH
337 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
338 } else {
339 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
340 }
248f219c
DH
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;
17926a79
DH
350 }
351
248f219c
DH
352skip:
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;
75e42126
DH
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 }
248f219c
DH
372
373 _proto("Rx DATA Jumbo %%%u", serial);
374 goto next_subpacket;
375 }
17926a79 376
248f219c
DH
377 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
378 ack = RXRPC_ACK_DELAY;
379 ack_serial = serial;
380 }
17926a79 381
248f219c
DH
382ack:
383 if (ack)
384 rxrpc_propose_ACK(call, ack, skew, ack_serial,
385 immediate_ack, true);
17926a79 386
248f219c
DH
387 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
388 rxrpc_notify_socket(call);
389 _leave(" [queued]");
17926a79
DH
390}
391
50235c4b
DH
392/*
393 * Process a requested ACK.
394 */
395static 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
419found:
420 rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
421 orig_serial, ack_serial, sent_at, resp_time);
422}
423
8e83134d
DH
424/*
425 * Process a ping response.
426 */
427static 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
17926a79 450/*
248f219c 451 * Process the extra information that may be appended to an ACK packet
17926a79 452 */
248f219c
DH
453static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
454 struct rxrpc_ackinfo *ackinfo)
17926a79 455{
248f219c
DH
456 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
457 struct rxrpc_peer *peer;
458 unsigned int mtu;
01fd0742 459 u32 rwind = ntohl(ackinfo->rwind);
248f219c
DH
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),
01fd0742 464 rwind, ntohl(ackinfo->jumbo_max));
248f219c 465
01fd0742
DH
466 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
467 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
468 call->tx_winsize = rwind;
248f219c
DH
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}
17926a79 481
248f219c
DH
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 */
491static 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;
f07373ea 496 u8 annotation, anno_type;
248f219c
DH
497
498 for (; nr_acks > 0; nr_acks--, seq++) {
499 ix = seq & RXRPC_RXTX_BUFF_MASK;
f07373ea
DH
500 annotation = call->rxtx_annotations[ix];
501 anno_type = annotation & RXRPC_TX_ANNO_MASK;
502 annotation &= ~RXRPC_TX_ANNO_MASK;
d01dc4c3 503 switch (*acks++) {
248f219c 504 case RXRPC_ACK_TYPE_ACK:
f07373ea
DH
505 if (anno_type == RXRPC_TX_ANNO_ACK)
506 continue;
507 call->rxtx_annotations[ix] =
508 RXRPC_TX_ANNO_ACK | annotation;
248f219c
DH
509 break;
510 case RXRPC_ACK_TYPE_NACK:
f07373ea 511 if (anno_type == RXRPC_TX_ANNO_NAK)
248f219c 512 continue;
be8aa338
DH
513 if (anno_type == RXRPC_TX_ANNO_RETRANS)
514 continue;
f07373ea
DH
515 call->rxtx_annotations[ix] =
516 RXRPC_TX_ANNO_NAK | annotation;
248f219c
DH
517 resend = true;
518 break;
519 default:
520 return rxrpc_proto_abort("SFT", call, 0);
17926a79 521 }
17926a79 522 }
248f219c
DH
523
524 if (resend &&
525 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
526 rxrpc_queue_call(call);
17926a79
DH
527}
528
529/*
248f219c
DH
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.
17926a79 538 */
248f219c
DH
539static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
540 u16 skew)
17926a79
DH
541{
542 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
248f219c
DH
543 union {
544 struct rxrpc_ackpacket ack;
545 struct rxrpc_ackinfo info;
546 u8 acks[RXRPC_MAXACKS];
547 } buf;
8e83134d 548 rxrpc_serial_t acked_serial;
248f219c
DH
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);
17926a79 557 }
248f219c
DH
558 sp->offset += sizeof(buf.ack);
559
8e83134d 560 acked_serial = ntohl(buf.ack.serial);
248f219c
DH
561 first_soft_ack = ntohl(buf.ack.firstPacket);
562 hard_ack = first_soft_ack - 1;
563 nr_acks = buf.ack.nAcks;
564
ec71eb9a
DH
565 trace_rxrpc_rx_ack(call, first_soft_ack, buf.ack.reason, nr_acks);
566
248f219c
DH
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),
8e83134d 572 acked_serial,
248f219c
DH
573 rxrpc_acks(buf.ack.reason),
574 buf.ack.nAcks);
575
8e83134d
DH
576 if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
577 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
578 sp->hdr.serial);
50235c4b
DH
579 if (buf.ack.reason == RXRPC_ACK_REQUESTED)
580 rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
581 sp->hdr.serial);
8e83134d 582
248f219c
DH
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) {
563ea7d5 588 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
248f219c 589 skew, sp->hdr.serial, true, true);
17926a79
DH
590 }
591
248f219c 592 offset = sp->offset + nr_acks + 3;
89a80ed4 593 if (skb->len >= offset + sizeof(buf.info)) {
248f219c
DH
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 }
17926a79 598
248f219c
DH
599 if (first_soft_ack == 0)
600 return rxrpc_proto_abort("AK0", call, 0);
17926a79 601
248f219c
DH
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;
17926a79 609 default:
248f219c
DH
610 return;
611 }
17926a79 612
248f219c 613 /* Discard any out-of-order or duplicate ACKs. */
98dafac5 614 if (before_eq(sp->hdr.serial, call->acks_latest)) {
248f219c
DH
615 _debug("discard ACK %d <= %d",
616 sp->hdr.serial, call->acks_latest);
617 return;
618 }
619 call->acks_latest = sp->hdr.serial;
17926a79 620
248f219c
DH
621 if (before(hard_ack, call->tx_hard_ack) ||
622 after(hard_ack, call->tx_top))
623 return rxrpc_proto_abort("AKW", call, 0);
70790dbe
DH
624 if (nr_acks > call->tx_top - hard_ack)
625 return rxrpc_proto_abort("AKN", call, 0);
17926a79 626
248f219c
DH
627 if (after(hard_ack, call->tx_hard_ack))
628 rxrpc_rotate_tx_window(call, hard_ack);
17926a79 629
70790dbe
DH
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");
248f219c 638 return;
70790dbe 639 }
17926a79 640
17926a79
DH
641}
642
643/*
248f219c 644 * Process an ACKALL packet.
17926a79 645 */
248f219c 646static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
17926a79 647{
248f219c 648 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 649
248f219c 650 _proto("Rx ACKALL %%%u", sp->hdr.serial);
17926a79 651
70790dbe
DH
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");
248f219c 655}
17926a79 656
248f219c
DH
657/*
658 * Process an ABORT packet.
659 */
660static 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;
17926a79 665
248f219c 666 _enter("");
17926a79 667
248f219c
DH
668 if (skb->len >= 4 &&
669 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
670 abort_code = ntohl(wtmp);
17926a79 671
248f219c 672 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
17926a79 673
248f219c
DH
674 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
675 abort_code, ECONNABORTED))
676 rxrpc_notify_socket(call);
17926a79
DH
677}
678
679/*
248f219c 680 * Process an incoming call packet.
17926a79 681 */
248f219c
DH
682static void rxrpc_input_call_packet(struct rxrpc_call *call,
683 struct sk_buff *skb, u16 skew)
17926a79 684{
248f219c 685 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 686
7727640c 687 _enter("%p,%p", call, skb);
17926a79 688
248f219c
DH
689 switch (sp->hdr.type) {
690 case RXRPC_PACKET_TYPE_DATA:
691 rxrpc_input_data(call, skb, skew);
692 break;
f5c17aae 693
248f219c
DH
694 case RXRPC_PACKET_TYPE_ACK:
695 rxrpc_input_ack(call, skb, skew);
17926a79 696 break;
17926a79 697
248f219c
DH
698 case RXRPC_PACKET_TYPE_BUSY:
699 _proto("Rx BUSY %%%u", sp->hdr.serial);
17926a79 700
248f219c
DH
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;
17926a79 706
248f219c
DH
707 case RXRPC_PACKET_TYPE_ABORT:
708 rxrpc_input_abort(call, skb);
709 break;
17926a79 710
248f219c
DH
711 case RXRPC_PACKET_TYPE_ACKALL:
712 rxrpc_input_ackall(call, skb);
713 break;
f5c17aae 714
248f219c
DH
715 default:
716 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
717 break;
17926a79 718 }
248f219c 719
17926a79
DH
720 _leave("");
721}
722
723/*
724 * post connection-level events to the connection
18bfeba5
DH
725 * - this includes challenges, responses, some aborts and call terminal packet
726 * retransmission.
17926a79 727 */
2e7e9758 728static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
17926a79
DH
729 struct sk_buff *skb)
730{
731 _enter("%p,%p", conn, skb);
732
17926a79 733 skb_queue_tail(&conn->rx_queue, skb);
2e7e9758 734 rxrpc_queue_conn(conn);
17926a79
DH
735}
736
44ba0698
DH
737/*
738 * post endpoint-level events to the local endpoint
739 * - this includes debug and version messages
740 */
741static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
742 struct sk_buff *skb)
743{
744 _enter("%p,%p", local, skb);
745
44ba0698 746 skb_queue_tail(&local->event_queue, skb);
5acbee46 747 rxrpc_queue_local(local);
44ba0698
DH
748}
749
248f219c
DH
750/*
751 * put a packet up for transport-level abort
752 */
753static 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
0d12f8a4
DH
761/*
762 * Extract the wire header from a packet and translate the byte order.
763 */
764static noinline
765int 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 */
4d0fc73e 770 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
0d12f8a4 771 return -EBADMSG;
0d12f8a4
DH
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);
248f219c 785 sp->offset = sizeof(whdr);
0d12f8a4
DH
786 return 0;
787}
788
17926a79
DH
789/*
790 * handle data received on the local endpoint
791 * - may be called in interrupt context
4f95dd78
DH
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.
17926a79 796 */
248f219c 797void rxrpc_data_ready(struct sock *udp_sk)
17926a79 798{
8496af50 799 struct rxrpc_connection *conn;
248f219c
DH
800 struct rxrpc_channel *chan;
801 struct rxrpc_call *call;
17926a79 802 struct rxrpc_skb_priv *sp;
248f219c 803 struct rxrpc_local *local = udp_sk->sk_user_data;
17926a79 804 struct sk_buff *skb;
248f219c 805 unsigned int channel;
563ea7d5 806 int ret, skew;
17926a79 807
248f219c 808 _enter("%p", udp_sk);
17926a79
DH
809
810 ASSERT(!irqs_disabled());
811
248f219c 812 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
17926a79 813 if (!skb) {
17926a79
DH
814 if (ret == -EAGAIN)
815 return;
816 _debug("UDP socket error %d", ret);
817 return;
818 }
819
71f3ca40 820 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
17926a79
DH
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)) {
71f3ca40 826 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
02c22347 827 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
17926a79
DH
828 _leave(" [CSUM failed]");
829 return;
830 }
831
02c22347 832 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
1781f7f5 833
0d12f8a4
DH
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 */
17926a79
DH
837 skb_orphan(skb);
838 sp = rxrpc_skb(skb);
17926a79 839
89b475ab
DH
840 /* dig out the RxRPC connection details */
841 if (rxrpc_extract_header(sp, skb) < 0)
842 goto bad_message;
843
8a681c36
DH
844 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
845 static int lose;
846 if ((lose++ & 7) == 7) {
89b475ab 847 trace_rxrpc_rx_lose(sp);
8a681c36
DH
848 rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
849 return;
850 }
851 }
852
49e19ec7 853 trace_rxrpc_rx_packet(sp);
17926a79
DH
854
855 _net("Rx RxRPC %s ep=%x call=%x:%x",
856 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
0d12f8a4 857 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
17926a79 858
351c1e64
DH
859 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
860 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
17926a79
DH
861 _proto("Rx Bad Packet Type %u", sp->hdr.type);
862 goto bad_message;
863 }
864
248f219c
DH
865 switch (sp->hdr.type) {
866 case RXRPC_PACKET_TYPE_VERSION:
44ba0698
DH
867 rxrpc_post_packet_to_local(local, skb);
868 goto out;
bc6e1ea3 869
248f219c
DH
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 }
17926a79 882
8496af50
DH
883 rcu_read_lock();
884
8496af50 885 conn = rxrpc_find_connection_rcu(local, skb);
248f219c
DH
886 if (conn) {
887 if (sp->hdr.securityIndex != conn->security_ix)
888 goto wrong_security;
563ea7d5 889
248f219c
DH
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 }
17926a79 906
8496af50 907 /* Call-bound packets are routed by connection channel. */
248f219c
DH
908 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
909 chan = &conn->channels[channel];
18bfeba5
DH
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) {
248f219c
DH
916 /* For the previous service call, if completed successfully, we
917 * discard all further packets.
18bfeba5 918 */
2266ffde 919 if (rxrpc_conn_is_service(conn) &&
18bfeba5
DH
920 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
921 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
922 goto discard_unlock;
923
248f219c
DH
924 /* But otherwise we need to retransmit the final packet from
925 * data cached in the connection record.
18bfeba5
DH
926 */
927 rxrpc_post_packet_to_conn(conn, skb);
928 goto out_unlock;
929 }
0d12f8a4 930
18bfeba5 931 call = rcu_dereference(chan->call);
248f219c
DH
932 } else {
933 skew = 0;
934 call = NULL;
935 }
8496af50 936
248f219c
DH
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 }
8e83134d 949 rxrpc_send_ping(call, skb, skew);
7727640c 950 }
44ba0698 951
248f219c
DH
952 rxrpc_input_call_packet(call, skb, skew);
953 goto discard_unlock;
954
18bfeba5 955discard_unlock:
8496af50 956 rcu_read_unlock();
248f219c 957discard:
71f3ca40 958 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
44ba0698 959out:
49e19ec7 960 trace_rxrpc_rx_done(0, 0);
17926a79
DH
961 return;
962
248f219c 963out_unlock:
8496af50 964 rcu_read_unlock();
248f219c 965 goto out;
8496af50 966
248f219c
DH
967wrong_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;
17926a79 973
248f219c
DH
974bad_message_unlock:
975 rcu_read_unlock();
17926a79 976bad_message:
248f219c
DH
977 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
978 RX_PROTOCOL_ERROR, EBADMSG);
17926a79 979 skb->priority = RX_PROTOCOL_ERROR;
248f219c
DH
980post_abort:
981 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
49e19ec7
DH
982reject_packet:
983 trace_rxrpc_rx_done(skb->mark, skb->priority);
17926a79 984 rxrpc_reject_packet(local, skb);
17926a79
DH
985 _leave(" [badmsg]");
986}