rxrpc: Delay the resend timer to allow for nsec->jiffies conv error
[linux-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,
9c7ad434
DH
52 true, true,
53 rxrpc_propose_ack_ping_for_params);
8e83134d
DH
54}
55
17926a79 56/*
248f219c 57 * Apply a hard ACK by advancing the Tx window.
17926a79 58 */
248f219c 59static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
17926a79 60{
248f219c
DH
61 struct sk_buff *skb, *list = NULL;
62 int ix;
70790dbe 63 u8 annotation;
17926a79 64
248f219c 65 spin_lock(&call->lock);
17926a79 66
248f219c
DH
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];
70790dbe 71 annotation = call->rxtx_annotations[ix];
71f3ca40 72 rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
248f219c
DH
73 call->rxtx_buffer[ix] = NULL;
74 call->rxtx_annotations[ix] = 0;
75 skb->next = list;
76 list = skb;
70790dbe
DH
77
78 if (annotation & RXRPC_TX_ANNO_LAST)
79 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
248f219c 80 }
17926a79 81
248f219c 82 spin_unlock(&call->lock);
17926a79 83
70790dbe
DH
84 trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
85 rxrpc_transmit_rotate_last :
86 rxrpc_transmit_rotate));
bc4abfcf
DH
87 wake_up(&call->waitq);
88
248f219c
DH
89 while (list) {
90 skb = list;
91 list = skb->next;
92 skb->next = NULL;
71f3ca40 93 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
17926a79 94 }
248f219c 95}
17926a79 96
248f219c
DH
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 */
70790dbe
DH
103static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
104 const char *abort_why)
248f219c 105{
17926a79 106
70790dbe 107 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
17926a79 108
248f219c 109 write_lock(&call->state_lock);
651350d1 110
248f219c 111 switch (call->state) {
70790dbe 112 case RXRPC_CALL_CLIENT_SEND_REQUEST:
248f219c 113 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
70790dbe
DH
114 if (reply_begun)
115 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
116 else
117 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
248f219c 118 break;
70790dbe 119
248f219c
DH
120 case RXRPC_CALL_SERVER_AWAIT_ACK:
121 __rxrpc_call_completed(call);
122 rxrpc_notify_socket(call);
123 break;
70790dbe
DH
124
125 default:
126 goto bad_state;
17926a79 127 }
17926a79 128
248f219c 129 write_unlock(&call->state_lock);
70790dbe
DH
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 }
248f219c
DH
135 _leave(" = ok");
136 return true;
70790dbe
DH
137
138bad_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 */
148static bool rxrpc_receiving_reply(struct rxrpc_call *call)
149{
150 rxrpc_seq_t top = READ_ONCE(call->tx_top);
151
dd7c1ee5
DH
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
70790dbe
DH
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;
248f219c
DH
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 */
185static bool rxrpc_validate_jumbo(struct sk_buff *skb)
186{
187 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
188 unsigned int offset = sp->offset;
89a80ed4 189 unsigned int len = skb->len;
248f219c
DH
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;
17926a79 207
248f219c
DH
208protocol_error:
209 return false;
17926a79
DH
210}
211
212/*
248f219c
DH
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).
17926a79 224 */
248f219c 225static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
75e42126 226 u8 annotation, bool *_jumbo_bad)
17926a79 227{
248f219c
DH
228 /* Discard normal packets that are duplicates. */
229 if (annotation == 0)
230 return;
17926a79 231
248f219c
DH
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 */
75e42126
DH
236 if (!*_jumbo_bad) {
237 call->nr_jumbo_bad++;
238 *_jumbo_bad = true;
248f219c
DH
239 }
240}
17926a79 241
248f219c
DH
242/*
243 * Process a DATA packet, adding the packet to the Rx ring.
244 */
245static 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;
75e42126 253 bool immediate_ack = false, jumbo_bad = false, queued;
248f219c
DH
254 u16 len;
255 u8 ack = 0, flags, annotation = 0;
17926a79 256
248f219c 257 _enter("{%u,%u},{%u,%u}",
89a80ed4 258 call->rx_hard_ack, call->rx_top, skb->len, seq);
17926a79 259
248f219c
DH
260 _proto("Rx DATA %%%u { #%u f=%02x }",
261 sp->hdr.serial, seq, sp->hdr.flags);
17926a79 262
248f219c
DH
263 if (call->state >= RXRPC_CALL_COMPLETE)
264 return;
17926a79 265
248f219c
DH
266 /* Received data implicitly ACKs all of the request packets we sent
267 * when we're acting as a client.
268 */
70790dbe
DH
269 if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
270 call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
271 !rxrpc_receiving_reply(call))
248f219c 272 return;
17926a79 273
248f219c 274 call->ackr_prev_seq = seq;
17926a79 275
248f219c
DH
276 hard_ack = READ_ONCE(call->rx_hard_ack);
277 if (after(seq, hard_ack + call->rx_winsize)) {
17926a79 278 ack = RXRPC_ACK_EXCEEDS_WINDOW;
248f219c
DH
279 ack_serial = serial;
280 goto ack;
17926a79
DH
281 }
282
248f219c
DH
283 flags = sp->hdr.flags;
284 if (flags & RXRPC_JUMBO_PACKET) {
75e42126 285 if (call->nr_jumbo_bad > 3) {
248f219c
DH
286 ack = RXRPC_ACK_NOSPACE;
287 ack_serial = serial;
288 goto ack;
17926a79 289 }
248f219c 290 annotation = 1;
17926a79
DH
291 }
292
248f219c
DH
293next_subpacket:
294 queued = false;
295 ix = seq & RXRPC_RXTX_BUFF_MASK;
89a80ed4 296 len = skb->len;
248f219c
DH
297 if (flags & RXRPC_JUMBO_PACKET)
298 len = RXRPC_JUMBO_DATALEN;
299
300 if (flags & RXRPC_LAST_PACKET) {
816c9fce 301 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
248f219c
DH
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);
17926a79
DH
308 }
309
248f219c
DH
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]) {
75e42126 322 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
248f219c
DH
323 if (ack != RXRPC_ACK_DUPLICATE) {
324 ack = RXRPC_ACK_DUPLICATE;
325 ack_serial = serial;
17926a79 326 }
248f219c
DH
327 immediate_ack = true;
328 goto skip;
17926a79
DH
329 }
330
248f219c
DH
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 */
71f3ca40 339 rxrpc_get_skb(skb, rxrpc_skb_rx_got);
248f219c
DH
340 call->rxtx_annotations[ix] = annotation;
341 smp_wmb();
342 call->rxtx_buffer[ix] = skb;
a7056c5b 343 if (after(seq, call->rx_top)) {
248f219c 344 smp_store_release(&call->rx_top, seq);
a7056c5b
DH
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 }
58dc63c9 353 if (flags & RXRPC_LAST_PACKET) {
816c9fce 354 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
58dc63c9
DH
355 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
356 } else {
357 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
358 }
248f219c
DH
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;
17926a79
DH
368 }
369
248f219c
DH
370skip:
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;
75e42126
DH
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 }
248f219c
DH
390
391 _proto("Rx DATA Jumbo %%%u", serial);
392 goto next_subpacket;
393 }
17926a79 394
248f219c
DH
395 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
396 ack = RXRPC_ACK_DELAY;
397 ack_serial = serial;
398 }
17926a79 399
248f219c
DH
400ack:
401 if (ack)
402 rxrpc_propose_ACK(call, ack, skew, ack_serial,
9c7ad434
DH
403 immediate_ack, true,
404 rxrpc_propose_ack_input_data);
17926a79 405
248f219c
DH
406 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
407 rxrpc_notify_socket(call);
408 _leave(" [queued]");
17926a79
DH
409}
410
50235c4b
DH
411/*
412 * Process a requested ACK.
413 */
414static 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
438found:
439 rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
440 orig_serial, ack_serial, sent_at, resp_time);
441}
442
8e83134d
DH
443/*
444 * Process a ping response.
445 */
446static 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
17926a79 469/*
248f219c 470 * Process the extra information that may be appended to an ACK packet
17926a79 471 */
248f219c
DH
472static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
473 struct rxrpc_ackinfo *ackinfo)
17926a79 474{
248f219c
DH
475 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
476 struct rxrpc_peer *peer;
477 unsigned int mtu;
01fd0742 478 u32 rwind = ntohl(ackinfo->rwind);
248f219c
DH
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),
01fd0742 483 rwind, ntohl(ackinfo->jumbo_max));
248f219c 484
01fd0742
DH
485 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
486 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
487 call->tx_winsize = rwind;
248f219c
DH
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}
17926a79 500
248f219c
DH
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 */
510static 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;
f07373ea 515 u8 annotation, anno_type;
248f219c
DH
516
517 for (; nr_acks > 0; nr_acks--, seq++) {
518 ix = seq & RXRPC_RXTX_BUFF_MASK;
f07373ea
DH
519 annotation = call->rxtx_annotations[ix];
520 anno_type = annotation & RXRPC_TX_ANNO_MASK;
521 annotation &= ~RXRPC_TX_ANNO_MASK;
d01dc4c3 522 switch (*acks++) {
248f219c 523 case RXRPC_ACK_TYPE_ACK:
f07373ea
DH
524 if (anno_type == RXRPC_TX_ANNO_ACK)
525 continue;
526 call->rxtx_annotations[ix] =
527 RXRPC_TX_ANNO_ACK | annotation;
248f219c
DH
528 break;
529 case RXRPC_ACK_TYPE_NACK:
f07373ea 530 if (anno_type == RXRPC_TX_ANNO_NAK)
248f219c 531 continue;
be8aa338
DH
532 if (anno_type == RXRPC_TX_ANNO_RETRANS)
533 continue;
f07373ea
DH
534 call->rxtx_annotations[ix] =
535 RXRPC_TX_ANNO_NAK | annotation;
248f219c
DH
536 resend = true;
537 break;
538 default:
539 return rxrpc_proto_abort("SFT", call, 0);
17926a79 540 }
17926a79 541 }
248f219c
DH
542
543 if (resend &&
544 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
545 rxrpc_queue_call(call);
17926a79
DH
546}
547
548/*
248f219c
DH
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.
17926a79 557 */
248f219c
DH
558static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
559 u16 skew)
17926a79 560{
9c7ad434 561 u8 ack_reason;
17926a79 562 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
248f219c
DH
563 union {
564 struct rxrpc_ackpacket ack;
565 struct rxrpc_ackinfo info;
566 u8 acks[RXRPC_MAXACKS];
567 } buf;
8e83134d 568 rxrpc_serial_t acked_serial;
248f219c
DH
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);
17926a79 577 }
248f219c
DH
578 sp->offset += sizeof(buf.ack);
579
8e83134d 580 acked_serial = ntohl(buf.ack.serial);
248f219c
DH
581 first_soft_ack = ntohl(buf.ack.firstPacket);
582 hard_ack = first_soft_ack - 1;
583 nr_acks = buf.ack.nAcks;
9c7ad434
DH
584 ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
585 buf.ack.reason : RXRPC_ACK__INVALID);
248f219c 586
9c7ad434 587 trace_rxrpc_rx_ack(call, first_soft_ack, ack_reason, nr_acks);
ec71eb9a 588
248f219c
DH
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),
8e83134d 594 acked_serial,
9c7ad434 595 rxrpc_ack_names[ack_reason],
248f219c
DH
596 buf.ack.nAcks);
597
8e83134d
DH
598 if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
599 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
600 sp->hdr.serial);
50235c4b
DH
601 if (buf.ack.reason == RXRPC_ACK_REQUESTED)
602 rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
603 sp->hdr.serial);
8e83134d 604
248f219c
DH
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,
9c7ad434
DH
608 skew, sp->hdr.serial, true, true,
609 rxrpc_propose_ack_respond_to_ping);
248f219c 610 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
563ea7d5 611 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
9c7ad434
DH
612 skew, sp->hdr.serial, true, true,
613 rxrpc_propose_ack_respond_to_ack);
17926a79
DH
614 }
615
248f219c 616 offset = sp->offset + nr_acks + 3;
89a80ed4 617 if (skb->len >= offset + sizeof(buf.info)) {
248f219c
DH
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 }
17926a79 622
248f219c
DH
623 if (first_soft_ack == 0)
624 return rxrpc_proto_abort("AK0", call, 0);
17926a79 625
248f219c
DH
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;
17926a79 633 default:
248f219c
DH
634 return;
635 }
17926a79 636
248f219c 637 /* Discard any out-of-order or duplicate ACKs. */
98dafac5 638 if (before_eq(sp->hdr.serial, call->acks_latest)) {
248f219c
DH
639 _debug("discard ACK %d <= %d",
640 sp->hdr.serial, call->acks_latest);
641 return;
642 }
643 call->acks_latest = sp->hdr.serial;
17926a79 644
248f219c
DH
645 if (before(hard_ack, call->tx_hard_ack) ||
646 after(hard_ack, call->tx_top))
647 return rxrpc_proto_abort("AKW", call, 0);
70790dbe
DH
648 if (nr_acks > call->tx_top - hard_ack)
649 return rxrpc_proto_abort("AKN", call, 0);
17926a79 650
248f219c
DH
651 if (after(hard_ack, call->tx_hard_ack))
652 rxrpc_rotate_tx_window(call, hard_ack);
17926a79 653
70790dbe
DH
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");
248f219c 662 return;
70790dbe 663 }
17926a79 664
17926a79
DH
665}
666
667/*
248f219c 668 * Process an ACKALL packet.
17926a79 669 */
248f219c 670static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
17926a79 671{
248f219c 672 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 673
248f219c 674 _proto("Rx ACKALL %%%u", sp->hdr.serial);
17926a79 675
70790dbe
DH
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");
248f219c 679}
17926a79 680
248f219c
DH
681/*
682 * Process an ABORT packet.
683 */
684static 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;
17926a79 689
248f219c 690 _enter("");
17926a79 691
248f219c
DH
692 if (skb->len >= 4 &&
693 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
694 abort_code = ntohl(wtmp);
17926a79 695
248f219c 696 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
17926a79 697
248f219c
DH
698 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
699 abort_code, ECONNABORTED))
700 rxrpc_notify_socket(call);
17926a79
DH
701}
702
703/*
248f219c 704 * Process an incoming call packet.
17926a79 705 */
248f219c
DH
706static void rxrpc_input_call_packet(struct rxrpc_call *call,
707 struct sk_buff *skb, u16 skew)
17926a79 708{
248f219c 709 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 710
7727640c 711 _enter("%p,%p", call, skb);
17926a79 712
248f219c
DH
713 switch (sp->hdr.type) {
714 case RXRPC_PACKET_TYPE_DATA:
715 rxrpc_input_data(call, skb, skew);
716 break;
f5c17aae 717
248f219c
DH
718 case RXRPC_PACKET_TYPE_ACK:
719 rxrpc_input_ack(call, skb, skew);
17926a79 720 break;
17926a79 721
248f219c
DH
722 case RXRPC_PACKET_TYPE_BUSY:
723 _proto("Rx BUSY %%%u", sp->hdr.serial);
17926a79 724
248f219c
DH
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;
17926a79 730
248f219c
DH
731 case RXRPC_PACKET_TYPE_ABORT:
732 rxrpc_input_abort(call, skb);
733 break;
17926a79 734
248f219c
DH
735 case RXRPC_PACKET_TYPE_ACKALL:
736 rxrpc_input_ackall(call, skb);
737 break;
f5c17aae 738
248f219c
DH
739 default:
740 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
741 break;
17926a79 742 }
248f219c 743
17926a79
DH
744 _leave("");
745}
746
747/*
748 * post connection-level events to the connection
18bfeba5
DH
749 * - this includes challenges, responses, some aborts and call terminal packet
750 * retransmission.
17926a79 751 */
2e7e9758 752static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
17926a79
DH
753 struct sk_buff *skb)
754{
755 _enter("%p,%p", conn, skb);
756
17926a79 757 skb_queue_tail(&conn->rx_queue, skb);
2e7e9758 758 rxrpc_queue_conn(conn);
17926a79
DH
759}
760
44ba0698
DH
761/*
762 * post endpoint-level events to the local endpoint
763 * - this includes debug and version messages
764 */
765static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
766 struct sk_buff *skb)
767{
768 _enter("%p,%p", local, skb);
769
44ba0698 770 skb_queue_tail(&local->event_queue, skb);
5acbee46 771 rxrpc_queue_local(local);
44ba0698
DH
772}
773
248f219c
DH
774/*
775 * put a packet up for transport-level abort
776 */
777static 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
0d12f8a4
DH
785/*
786 * Extract the wire header from a packet and translate the byte order.
787 */
788static noinline
789int 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 */
4d0fc73e 794 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
0d12f8a4 795 return -EBADMSG;
0d12f8a4
DH
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);
248f219c 809 sp->offset = sizeof(whdr);
0d12f8a4
DH
810 return 0;
811}
812
17926a79
DH
813/*
814 * handle data received on the local endpoint
815 * - may be called in interrupt context
4f95dd78
DH
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.
17926a79 820 */
248f219c 821void rxrpc_data_ready(struct sock *udp_sk)
17926a79 822{
8496af50 823 struct rxrpc_connection *conn;
248f219c
DH
824 struct rxrpc_channel *chan;
825 struct rxrpc_call *call;
17926a79 826 struct rxrpc_skb_priv *sp;
248f219c 827 struct rxrpc_local *local = udp_sk->sk_user_data;
17926a79 828 struct sk_buff *skb;
248f219c 829 unsigned int channel;
563ea7d5 830 int ret, skew;
17926a79 831
248f219c 832 _enter("%p", udp_sk);
17926a79
DH
833
834 ASSERT(!irqs_disabled());
835
248f219c 836 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
17926a79 837 if (!skb) {
17926a79
DH
838 if (ret == -EAGAIN)
839 return;
840 _debug("UDP socket error %d", ret);
841 return;
842 }
843
71f3ca40 844 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
17926a79
DH
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)) {
71f3ca40 850 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
02c22347 851 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
17926a79
DH
852 _leave(" [CSUM failed]");
853 return;
854 }
855
02c22347 856 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
1781f7f5 857
0d12f8a4
DH
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 */
17926a79
DH
861 skb_orphan(skb);
862 sp = rxrpc_skb(skb);
17926a79 863
89b475ab
DH
864 /* dig out the RxRPC connection details */
865 if (rxrpc_extract_header(sp, skb) < 0)
866 goto bad_message;
867
8a681c36
DH
868 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
869 static int lose;
870 if ((lose++ & 7) == 7) {
89b475ab 871 trace_rxrpc_rx_lose(sp);
8a681c36
DH
872 rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
873 return;
874 }
875 }
876
49e19ec7 877 trace_rxrpc_rx_packet(sp);
17926a79
DH
878
879 _net("Rx RxRPC %s ep=%x call=%x:%x",
880 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
0d12f8a4 881 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
17926a79 882
351c1e64
DH
883 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
884 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
17926a79
DH
885 _proto("Rx Bad Packet Type %u", sp->hdr.type);
886 goto bad_message;
887 }
888
248f219c
DH
889 switch (sp->hdr.type) {
890 case RXRPC_PACKET_TYPE_VERSION:
44ba0698
DH
891 rxrpc_post_packet_to_local(local, skb);
892 goto out;
bc6e1ea3 893
248f219c
DH
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 }
17926a79 906
8496af50
DH
907 rcu_read_lock();
908
8496af50 909 conn = rxrpc_find_connection_rcu(local, skb);
248f219c
DH
910 if (conn) {
911 if (sp->hdr.securityIndex != conn->security_ix)
912 goto wrong_security;
563ea7d5 913
248f219c
DH
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 }
17926a79 930
8496af50 931 /* Call-bound packets are routed by connection channel. */
248f219c
DH
932 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
933 chan = &conn->channels[channel];
18bfeba5
DH
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) {
248f219c
DH
940 /* For the previous service call, if completed successfully, we
941 * discard all further packets.
18bfeba5 942 */
2266ffde 943 if (rxrpc_conn_is_service(conn) &&
18bfeba5
DH
944 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
945 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
946 goto discard_unlock;
947
248f219c
DH
948 /* But otherwise we need to retransmit the final packet from
949 * data cached in the connection record.
18bfeba5
DH
950 */
951 rxrpc_post_packet_to_conn(conn, skb);
952 goto out_unlock;
953 }
0d12f8a4 954
18bfeba5 955 call = rcu_dereference(chan->call);
248f219c
DH
956 } else {
957 skew = 0;
958 call = NULL;
959 }
8496af50 960
248f219c
DH
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 }
8e83134d 973 rxrpc_send_ping(call, skb, skew);
7727640c 974 }
44ba0698 975
248f219c
DH
976 rxrpc_input_call_packet(call, skb, skew);
977 goto discard_unlock;
978
18bfeba5 979discard_unlock:
8496af50 980 rcu_read_unlock();
248f219c 981discard:
71f3ca40 982 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
44ba0698 983out:
49e19ec7 984 trace_rxrpc_rx_done(0, 0);
17926a79
DH
985 return;
986
248f219c 987out_unlock:
8496af50 988 rcu_read_unlock();
248f219c 989 goto out;
8496af50 990
248f219c
DH
991wrong_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;
17926a79 997
248f219c
DH
998bad_message_unlock:
999 rcu_read_unlock();
17926a79 1000bad_message:
248f219c
DH
1001 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1002 RX_PROTOCOL_ERROR, EBADMSG);
17926a79 1003 skb->priority = RX_PROTOCOL_ERROR;
248f219c
DH
1004post_abort:
1005 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
49e19ec7
DH
1006reject_packet:
1007 trace_rxrpc_rx_done(skb->mark, skb->priority);
17926a79 1008 rxrpc_reject_packet(local, skb);
17926a79
DH
1009 _leave(" [badmsg]");
1010}