blob: 5958ef8ba2a0fd6be67f50105ba47f78352b3ada [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC packet reception
2 *
David Howells248f2192016-09-08 11:10:12 +01003 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
David Howells17926a72007-04-26 15:48:28 -07004 * 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
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
David Howells17926a72007-04-26 15:48:28 -070023#include <net/sock.h>
24#include <net/af_rxrpc.h>
25#include <net/ip.h>
Herbert Xu1781f7f2007-12-11 11:30:32 -080026#include <net/udp.h>
Pavel Emelyanov02833282008-07-05 21:18:48 -070027#include <net/net_namespace.h>
David Howells17926a72007-04-26 15:48:28 -070028#include "ar-internal.h"
29
David Howells248f2192016-09-08 11:10:12 +010030static void rxrpc_proto_abort(const char *why,
31 struct rxrpc_call *call, rxrpc_seq_t seq)
David Howells17926a72007-04-26 15:48:28 -070032{
David Howells248f2192016-09-08 11:10:12 +010033 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);
David Howells17926a72007-04-26 15:48:28 -070036 }
David Howells17926a72007-04-26 15:48:28 -070037}
38
39/*
David Howells248f2192016-09-08 11:10:12 +010040 * Apply a hard ACK by advancing the Tx window.
David Howells17926a72007-04-26 15:48:28 -070041 */
David Howells248f2192016-09-08 11:10:12 +010042static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to)
David Howells17926a72007-04-26 15:48:28 -070043{
David Howells248f2192016-09-08 11:10:12 +010044 struct sk_buff *skb, *list = NULL;
45 int ix;
David Howells17926a72007-04-26 15:48:28 -070046
47 spin_lock(&call->lock);
48
David Howells248f2192016-09-08 11:10:12 +010049 while (before(call->tx_hard_ack, to)) {
50 call->tx_hard_ack++;
51 ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
52 skb = call->rxtx_buffer[ix];
53 rxrpc_see_skb(skb);
54 call->rxtx_buffer[ix] = NULL;
55 call->rxtx_annotations[ix] = 0;
56 skb->next = list;
57 list = skb;
David Howells17926a72007-04-26 15:48:28 -070058 }
59
60 spin_unlock(&call->lock);
David Howells17926a72007-04-26 15:48:28 -070061
David Howellsbc4abfc2016-09-13 22:36:21 +010062 wake_up(&call->waitq);
63
David Howells248f2192016-09-08 11:10:12 +010064 while (list) {
65 skb = list;
66 list = skb->next;
67 skb->next = NULL;
68 rxrpc_free_skb(skb);
69 }
David Howells17926a72007-04-26 15:48:28 -070070}
71
72/*
David Howells248f2192016-09-08 11:10:12 +010073 * End the transmission phase of a call.
74 *
75 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
76 * or a final ACK packet.
David Howells17926a72007-04-26 15:48:28 -070077 */
David Howells248f2192016-09-08 11:10:12 +010078static bool rxrpc_end_tx_phase(struct rxrpc_call *call, const char *abort_why)
David Howells17926a72007-04-26 15:48:28 -070079{
David Howells248f2192016-09-08 11:10:12 +010080 _enter("");
David Howells17926a72007-04-26 15:48:28 -070081
82 switch (call->state) {
David Howells248f2192016-09-08 11:10:12 +010083 case RXRPC_CALL_CLIENT_RECV_REPLY:
84 return true;
85 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
86 case RXRPC_CALL_SERVER_AWAIT_ACK:
87 break;
88 default:
89 rxrpc_proto_abort(abort_why, call, call->tx_top);
90 return false;
91 }
92
93 rxrpc_rotate_tx_window(call, call->tx_top);
94
95 write_lock(&call->state_lock);
96
97 switch (call->state) {
98 default:
99 break;
David Howells17926a72007-04-26 15:48:28 -0700100 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
101 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
David Howells17926a72007-04-26 15:48:28 -0700102 break;
David Howells248f2192016-09-08 11:10:12 +0100103 case RXRPC_CALL_SERVER_AWAIT_ACK:
104 __rxrpc_call_completed(call);
105 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700106 break;
107 }
David Howells248f2192016-09-08 11:10:12 +0100108
109 write_unlock(&call->state_lock);
110 _leave(" = ok");
111 return true;
112}
113
114/*
115 * Scan a jumbo packet to validate its structure and to work out how many
116 * subpackets it contains.
117 *
118 * A jumbo packet is a collection of consecutive packets glued together with
119 * little headers between that indicate how to change the initial header for
120 * each subpacket.
121 *
122 * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
123 * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
124 * size.
125 */
126static bool rxrpc_validate_jumbo(struct sk_buff *skb)
127{
128 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
129 unsigned int offset = sp->offset;
David Howells89a80ed2016-09-13 22:36:22 +0100130 unsigned int len = skb->len;
David Howells248f2192016-09-08 11:10:12 +0100131 int nr_jumbo = 1;
132 u8 flags = sp->hdr.flags;
133
134 do {
135 nr_jumbo++;
136 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
137 goto protocol_error;
138 if (flags & RXRPC_LAST_PACKET)
139 goto protocol_error;
140 offset += RXRPC_JUMBO_DATALEN;
141 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
142 goto protocol_error;
143 offset += sizeof(struct rxrpc_jumbo_header);
144 } while (flags & RXRPC_JUMBO_PACKET);
145
146 sp->nr_jumbo = nr_jumbo;
147 return true;
148
149protocol_error:
150 return false;
151}
152
153/*
154 * Handle reception of a duplicate packet.
155 *
156 * We have to take care to avoid an attack here whereby we're given a series of
157 * jumbograms, each with a sequence number one before the preceding one and
158 * filled up to maximum UDP size. If they never send us the first packet in
159 * the sequence, they can cause us to have to hold on to around 2MiB of kernel
160 * space until the call times out.
161 *
162 * We limit the space usage by only accepting three duplicate jumbo packets per
163 * call. After that, we tell the other side we're no longer accepting jumbos
164 * (that information is encoded in the ACK packet).
165 */
166static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
167 u8 annotation, bool *_jumbo_dup)
168{
169 /* Discard normal packets that are duplicates. */
170 if (annotation == 0)
171 return;
172
173 /* Skip jumbo subpackets that are duplicates. When we've had three or
174 * more partially duplicate jumbo packets, we refuse to take any more
175 * jumbos for this call.
176 */
177 if (!*_jumbo_dup) {
178 call->nr_jumbo_dup++;
179 *_jumbo_dup = true;
180 }
David Howells17926a72007-04-26 15:48:28 -0700181}
182
183/*
David Howells248f2192016-09-08 11:10:12 +0100184 * Process a DATA packet, adding the packet to the Rx ring.
David Howells17926a72007-04-26 15:48:28 -0700185 */
David Howells248f2192016-09-08 11:10:12 +0100186static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
187 u16 skew)
188{
189 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
190 unsigned int offset = sp->offset;
191 unsigned int ix;
192 rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
193 rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
194 bool immediate_ack = false, jumbo_dup = false, queued;
195 u16 len;
196 u8 ack = 0, flags, annotation = 0;
197
198 _enter("{%u,%u},{%u,%u}",
David Howells89a80ed2016-09-13 22:36:22 +0100199 call->rx_hard_ack, call->rx_top, skb->len, seq);
David Howells248f2192016-09-08 11:10:12 +0100200
201 _proto("Rx DATA %%%u { #%u f=%02x }",
202 sp->hdr.serial, seq, sp->hdr.flags);
203
204 if (call->state >= RXRPC_CALL_COMPLETE)
205 return;
206
207 /* Received data implicitly ACKs all of the request packets we sent
208 * when we're acting as a client.
209 */
210 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY &&
211 !rxrpc_end_tx_phase(call, "ETD"))
212 return;
213
214 call->ackr_prev_seq = seq;
215
216 hard_ack = READ_ONCE(call->rx_hard_ack);
217 if (after(seq, hard_ack + call->rx_winsize)) {
218 ack = RXRPC_ACK_EXCEEDS_WINDOW;
219 ack_serial = serial;
220 goto ack;
221 }
222
223 flags = sp->hdr.flags;
224 if (flags & RXRPC_JUMBO_PACKET) {
225 if (call->nr_jumbo_dup > 3) {
226 ack = RXRPC_ACK_NOSPACE;
227 ack_serial = serial;
228 goto ack;
229 }
230 annotation = 1;
231 }
232
233next_subpacket:
234 queued = false;
235 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells89a80ed2016-09-13 22:36:22 +0100236 len = skb->len;
David Howells248f2192016-09-08 11:10:12 +0100237 if (flags & RXRPC_JUMBO_PACKET)
238 len = RXRPC_JUMBO_DATALEN;
239
240 if (flags & RXRPC_LAST_PACKET) {
241 if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
242 seq != call->rx_top)
243 return rxrpc_proto_abort("LSN", call, seq);
244 } else {
245 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
246 after_eq(seq, call->rx_top))
247 return rxrpc_proto_abort("LSA", call, seq);
248 }
249
250 if (before_eq(seq, hard_ack)) {
251 ack = RXRPC_ACK_DUPLICATE;
252 ack_serial = serial;
253 goto skip;
254 }
255
256 if (flags & RXRPC_REQUEST_ACK && !ack) {
257 ack = RXRPC_ACK_REQUESTED;
258 ack_serial = serial;
259 }
260
261 if (call->rxtx_buffer[ix]) {
262 rxrpc_input_dup_data(call, seq, annotation, &jumbo_dup);
263 if (ack != RXRPC_ACK_DUPLICATE) {
264 ack = RXRPC_ACK_DUPLICATE;
265 ack_serial = serial;
266 }
267 immediate_ack = true;
268 goto skip;
269 }
270
271 /* Queue the packet. We use a couple of memory barriers here as need
272 * to make sure that rx_top is perceived to be set after the buffer
273 * pointer and that the buffer pointer is set after the annotation and
274 * the skb data.
275 *
276 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
277 * and also rxrpc_fill_out_ack().
278 */
279 rxrpc_get_skb(skb);
280 call->rxtx_annotations[ix] = annotation;
281 smp_wmb();
282 call->rxtx_buffer[ix] = skb;
283 if (after(seq, call->rx_top))
284 smp_store_release(&call->rx_top, seq);
285 queued = true;
286
287 if (after_eq(seq, call->rx_expect_next)) {
288 if (after(seq, call->rx_expect_next)) {
289 _net("OOS %u > %u", seq, call->rx_expect_next);
290 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
291 ack_serial = serial;
292 }
293 call->rx_expect_next = seq + 1;
294 }
295
296skip:
297 offset += len;
298 if (flags & RXRPC_JUMBO_PACKET) {
299 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
300 return rxrpc_proto_abort("XJF", call, seq);
301 offset += sizeof(struct rxrpc_jumbo_header);
302 seq++;
303 serial++;
304 annotation++;
305 if (flags & RXRPC_JUMBO_PACKET)
306 annotation |= RXRPC_RX_ANNO_JLAST;
307
308 _proto("Rx DATA Jumbo %%%u", serial);
309 goto next_subpacket;
310 }
311
312 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
313 ack = RXRPC_ACK_DELAY;
314 ack_serial = serial;
315 }
316
317ack:
318 if (ack)
319 rxrpc_propose_ACK(call, ack, skew, ack_serial,
320 immediate_ack, true);
321
322 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
323 rxrpc_notify_socket(call);
324 _leave(" [queued]");
325}
326
327/*
328 * Process the extra information that may be appended to an ACK packet
329 */
330static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
331 struct rxrpc_ackinfo *ackinfo)
332{
333 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
334 struct rxrpc_peer *peer;
335 unsigned int mtu;
336
337 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
338 sp->hdr.serial,
339 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
340 ntohl(ackinfo->rwind), ntohl(ackinfo->jumbo_max));
341
342 if (call->tx_winsize > ntohl(ackinfo->rwind))
343 call->tx_winsize = ntohl(ackinfo->rwind);
344
345 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
346
347 peer = call->peer;
348 if (mtu < peer->maxdata) {
349 spin_lock_bh(&peer->lock);
350 peer->maxdata = mtu;
351 peer->mtu = mtu + peer->hdrsize;
352 spin_unlock_bh(&peer->lock);
353 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
354 }
355}
356
357/*
358 * Process individual soft ACKs.
359 *
360 * Each ACK in the array corresponds to one packet and can be either an ACK or
361 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
362 * packets that lie beyond the end of the ACK list are scheduled for resend by
363 * the timer on the basis that the peer might just not have processed them at
364 * the time the ACK was sent.
365 */
366static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
367 rxrpc_seq_t seq, int nr_acks)
368{
369 bool resend = false;
370 int ix;
371
372 for (; nr_acks > 0; nr_acks--, seq++) {
373 ix = seq & RXRPC_RXTX_BUFF_MASK;
374 switch (*acks) {
375 case RXRPC_ACK_TYPE_ACK:
376 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_ACK;
377 break;
378 case RXRPC_ACK_TYPE_NACK:
379 if (call->rxtx_annotations[ix] == RXRPC_TX_ANNO_NAK)
380 continue;
381 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_NAK;
382 resend = true;
383 break;
384 default:
385 return rxrpc_proto_abort("SFT", call, 0);
386 }
387 }
388
389 if (resend &&
390 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
391 rxrpc_queue_call(call);
392}
393
394/*
395 * Process an ACK packet.
396 *
397 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
398 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
399 *
400 * A hard-ACK means that a packet has been processed and may be discarded; a
401 * soft-ACK means that the packet may be discarded and retransmission
402 * requested. A phase is complete when all packets are hard-ACK'd.
403 */
404static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
405 u16 skew)
406{
407 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
408 union {
409 struct rxrpc_ackpacket ack;
410 struct rxrpc_ackinfo info;
411 u8 acks[RXRPC_MAXACKS];
412 } buf;
413 rxrpc_seq_t first_soft_ack, hard_ack;
414 int nr_acks, offset;
415
416 _enter("");
417
418 if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
419 _debug("extraction failure");
420 return rxrpc_proto_abort("XAK", call, 0);
421 }
422 sp->offset += sizeof(buf.ack);
423
424 first_soft_ack = ntohl(buf.ack.firstPacket);
425 hard_ack = first_soft_ack - 1;
426 nr_acks = buf.ack.nAcks;
427
428 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
429 sp->hdr.serial,
430 ntohs(buf.ack.maxSkew),
431 first_soft_ack,
432 ntohl(buf.ack.previousPacket),
433 ntohl(buf.ack.serial),
434 rxrpc_acks(buf.ack.reason),
435 buf.ack.nAcks);
436
437 if (buf.ack.reason == RXRPC_ACK_PING) {
438 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
439 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
440 skew, sp->hdr.serial, true, true);
441 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
442 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
443 skew, sp->hdr.serial, true, true);
444 }
445
446 offset = sp->offset + nr_acks + 3;
David Howells89a80ed2016-09-13 22:36:22 +0100447 if (skb->len >= offset + sizeof(buf.info)) {
David Howells248f2192016-09-08 11:10:12 +0100448 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
449 return rxrpc_proto_abort("XAI", call, 0);
450 rxrpc_input_ackinfo(call, skb, &buf.info);
451 }
452
453 if (first_soft_ack == 0)
454 return rxrpc_proto_abort("AK0", call, 0);
455
456 /* Ignore ACKs unless we are or have just been transmitting. */
457 switch (call->state) {
458 case RXRPC_CALL_CLIENT_SEND_REQUEST:
459 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
460 case RXRPC_CALL_SERVER_SEND_REPLY:
461 case RXRPC_CALL_SERVER_AWAIT_ACK:
462 break;
463 default:
464 return;
465 }
466
467 /* Discard any out-of-order or duplicate ACKs. */
468 if ((int)sp->hdr.serial - (int)call->acks_latest <= 0) {
469 _debug("discard ACK %d <= %d",
470 sp->hdr.serial, call->acks_latest);
471 return;
472 }
473 call->acks_latest = sp->hdr.serial;
474
475 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
476 hard_ack == call->tx_top) {
477 rxrpc_end_tx_phase(call, "ETA");
478 return;
479 }
480
481 if (before(hard_ack, call->tx_hard_ack) ||
482 after(hard_ack, call->tx_top))
483 return rxrpc_proto_abort("AKW", call, 0);
484
485 if (after(hard_ack, call->tx_hard_ack))
486 rxrpc_rotate_tx_window(call, hard_ack);
487
488 if (after(first_soft_ack, call->tx_top))
489 return;
490
491 if (nr_acks > call->tx_top - first_soft_ack + 1)
492 nr_acks = first_soft_ack - call->tx_top + 1;
493 if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
494 return rxrpc_proto_abort("XSA", call, 0);
495 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
496}
497
498/*
499 * Process an ACKALL packet.
500 */
501static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
502{
503 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
504
505 _proto("Rx ACKALL %%%u", sp->hdr.serial);
506
507 rxrpc_end_tx_phase(call, "ETL");
508}
509
510/*
511 * Process an ABORT packet.
512 */
513static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700514{
515 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000516 __be32 wtmp;
David Howells248f2192016-09-08 11:10:12 +0100517 u32 abort_code = RX_CALL_DEAD;
518
519 _enter("");
520
521 if (skb->len >= 4 &&
522 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
523 abort_code = ntohl(wtmp);
524
525 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
526
527 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
528 abort_code, ECONNABORTED))
529 rxrpc_notify_socket(call);
530}
531
532/*
533 * Process an incoming call packet.
534 */
535static void rxrpc_input_call_packet(struct rxrpc_call *call,
536 struct sk_buff *skb, u16 skew)
537{
538 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700539
540 _enter("%p,%p", call, skb);
541
David Howells17926a72007-04-26 15:48:28 -0700542 switch (sp->hdr.type) {
David Howells248f2192016-09-08 11:10:12 +0100543 case RXRPC_PACKET_TYPE_DATA:
544 rxrpc_input_data(call, skb, skew);
545 break;
David Howells17926a72007-04-26 15:48:28 -0700546
David Howells248f2192016-09-08 11:10:12 +0100547 case RXRPC_PACKET_TYPE_ACK:
548 rxrpc_input_ack(call, skb, skew);
549 break;
David Howells17926a72007-04-26 15:48:28 -0700550
551 case RXRPC_PACKET_TYPE_BUSY:
David Howells0d12f8a2016-03-04 15:53:46 +0000552 _proto("Rx BUSY %%%u", sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700553
David Howells248f2192016-09-08 11:10:12 +0100554 /* Just ignore BUSY packets from the server; the retry and
555 * lifespan timers will take care of business. BUSY packets
556 * from the client don't make sense.
557 */
558 break;
David Howells17926a72007-04-26 15:48:28 -0700559
David Howells248f2192016-09-08 11:10:12 +0100560 case RXRPC_PACKET_TYPE_ABORT:
561 rxrpc_input_abort(call, skb);
562 break;
563
564 case RXRPC_PACKET_TYPE_ACKALL:
565 rxrpc_input_ackall(call, skb);
566 break;
David Howells17926a72007-04-26 15:48:28 -0700567
568 default:
David Howells0d12f8a2016-03-04 15:53:46 +0000569 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700570 break;
571 }
572
David Howells17926a72007-04-26 15:48:28 -0700573 _leave("");
574}
575
576/*
577 * post connection-level events to the connection
David Howells18bfeba2016-08-23 15:27:25 +0100578 * - this includes challenges, responses, some aborts and call terminal packet
579 * retransmission.
David Howells17926a72007-04-26 15:48:28 -0700580 */
David Howells2e7e9752016-08-09 10:11:48 +0100581static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
David Howells17926a72007-04-26 15:48:28 -0700582 struct sk_buff *skb)
583{
584 _enter("%p,%p", conn, skb);
585
David Howells17926a72007-04-26 15:48:28 -0700586 skb_queue_tail(&conn->rx_queue, skb);
David Howells2e7e9752016-08-09 10:11:48 +0100587 rxrpc_queue_conn(conn);
David Howells17926a72007-04-26 15:48:28 -0700588}
589
David Howells44ba0692015-04-01 16:31:26 +0100590/*
591 * post endpoint-level events to the local endpoint
592 * - this includes debug and version messages
593 */
594static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
595 struct sk_buff *skb)
596{
597 _enter("%p,%p", local, skb);
598
David Howells44ba0692015-04-01 16:31:26 +0100599 skb_queue_tail(&local->event_queue, skb);
David Howells5acbee42016-06-27 10:32:02 +0100600 rxrpc_queue_local(local);
David Howells44ba0692015-04-01 16:31:26 +0100601}
602
David Howells0d12f8a2016-03-04 15:53:46 +0000603/*
David Howells248f2192016-09-08 11:10:12 +0100604 * put a packet up for transport-level abort
605 */
606static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
607{
608 CHECK_SLAB_OKAY(&local->usage);
609
610 skb_queue_tail(&local->reject_queue, skb);
611 rxrpc_queue_local(local);
612}
613
614/*
David Howells0d12f8a2016-03-04 15:53:46 +0000615 * Extract the wire header from a packet and translate the byte order.
616 */
617static noinline
618int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
619{
620 struct rxrpc_wire_header whdr;
621
622 /* dig out the RxRPC connection details */
Willem de Bruijn4d0fc732016-04-07 11:44:59 -0400623 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
David Howells0d12f8a2016-03-04 15:53:46 +0000624 return -EBADMSG;
David Howells0d12f8a2016-03-04 15:53:46 +0000625
626 memset(sp, 0, sizeof(*sp));
627 sp->hdr.epoch = ntohl(whdr.epoch);
628 sp->hdr.cid = ntohl(whdr.cid);
629 sp->hdr.callNumber = ntohl(whdr.callNumber);
630 sp->hdr.seq = ntohl(whdr.seq);
631 sp->hdr.serial = ntohl(whdr.serial);
632 sp->hdr.flags = whdr.flags;
633 sp->hdr.type = whdr.type;
634 sp->hdr.userStatus = whdr.userStatus;
635 sp->hdr.securityIndex = whdr.securityIndex;
636 sp->hdr._rsvd = ntohs(whdr._rsvd);
637 sp->hdr.serviceId = ntohs(whdr.serviceId);
David Howells248f2192016-09-08 11:10:12 +0100638 sp->offset = sizeof(whdr);
David Howells0d12f8a2016-03-04 15:53:46 +0000639 return 0;
640}
641
David Howells17926a72007-04-26 15:48:28 -0700642/*
643 * handle data received on the local endpoint
644 * - may be called in interrupt context
David Howells4f95dd72016-04-04 14:00:35 +0100645 *
646 * The socket is locked by the caller and this prevents the socket from being
647 * shut down and the local endpoint from going away, thus sk_user_data will not
648 * be cleared until this function returns.
David Howells17926a72007-04-26 15:48:28 -0700649 */
David Howells248f2192016-09-08 11:10:12 +0100650void rxrpc_data_ready(struct sock *udp_sk)
David Howells17926a72007-04-26 15:48:28 -0700651{
David Howells8496af52016-07-01 07:51:50 +0100652 struct rxrpc_connection *conn;
David Howells248f2192016-09-08 11:10:12 +0100653 struct rxrpc_channel *chan;
654 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700655 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100656 struct rxrpc_local *local = udp_sk->sk_user_data;
David Howells17926a72007-04-26 15:48:28 -0700657 struct sk_buff *skb;
David Howells248f2192016-09-08 11:10:12 +0100658 unsigned int channel;
David Howells563ea7d2016-08-23 15:27:25 +0100659 int ret, skew;
David Howells17926a72007-04-26 15:48:28 -0700660
David Howells248f2192016-09-08 11:10:12 +0100661 _enter("%p", udp_sk);
David Howells17926a72007-04-26 15:48:28 -0700662
663 ASSERT(!irqs_disabled());
664
David Howells248f2192016-09-08 11:10:12 +0100665 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
David Howells17926a72007-04-26 15:48:28 -0700666 if (!skb) {
David Howells17926a72007-04-26 15:48:28 -0700667 if (ret == -EAGAIN)
668 return;
669 _debug("UDP socket error %d", ret);
670 return;
671 }
672
673 rxrpc_new_skb(skb);
674
675 _net("recv skb %p", skb);
676
677 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
678 if (skb_checksum_complete(skb)) {
679 rxrpc_free_skb(skb);
Eric Dumazet02c22342016-04-27 16:44:30 -0700680 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
David Howells17926a72007-04-26 15:48:28 -0700681 _leave(" [CSUM failed]");
682 return;
683 }
684
Eric Dumazet02c22342016-04-27 16:44:30 -0700685 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
Herbert Xu1781f7f2007-12-11 11:30:32 -0800686
David Howells0d12f8a2016-03-04 15:53:46 +0000687 /* The socket buffer we have is owned by UDP, with UDP's data all over
688 * it, but we really want our own data there.
689 */
David Howells17926a72007-04-26 15:48:28 -0700690 skb_orphan(skb);
691 sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700692
693 _net("Rx UDP packet from %08x:%04hu",
694 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
695
696 /* dig out the RxRPC connection details */
David Howells0d12f8a2016-03-04 15:53:46 +0000697 if (rxrpc_extract_header(sp, skb) < 0)
David Howells17926a72007-04-26 15:48:28 -0700698 goto bad_message;
David Howells49e19ec2016-09-08 11:10:12 +0100699 trace_rxrpc_rx_packet(sp);
David Howells17926a72007-04-26 15:48:28 -0700700
701 _net("Rx RxRPC %s ep=%x call=%x:%x",
702 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
David Howells0d12f8a2016-03-04 15:53:46 +0000703 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
David Howells17926a72007-04-26 15:48:28 -0700704
David Howells351c1e62016-03-04 15:56:06 +0000705 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
706 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
David Howells17926a72007-04-26 15:48:28 -0700707 _proto("Rx Bad Packet Type %u", sp->hdr.type);
708 goto bad_message;
709 }
710
David Howells248f2192016-09-08 11:10:12 +0100711 switch (sp->hdr.type) {
712 case RXRPC_PACKET_TYPE_VERSION:
David Howells44ba0692015-04-01 16:31:26 +0100713 rxrpc_post_packet_to_local(local, skb);
714 goto out;
David Howellsbc6e1ea2016-06-10 22:30:27 +0100715
David Howells248f2192016-09-08 11:10:12 +0100716 case RXRPC_PACKET_TYPE_BUSY:
717 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
718 goto discard;
719
720 case RXRPC_PACKET_TYPE_DATA:
721 if (sp->hdr.callNumber == 0)
722 goto bad_message;
723 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
724 !rxrpc_validate_jumbo(skb))
725 goto bad_message;
726 break;
727 }
David Howells17926a72007-04-26 15:48:28 -0700728
David Howells8496af52016-07-01 07:51:50 +0100729 rcu_read_lock();
730
David Howells8496af52016-07-01 07:51:50 +0100731 conn = rxrpc_find_connection_rcu(local, skb);
David Howells248f2192016-09-08 11:10:12 +0100732 if (conn) {
733 if (sp->hdr.securityIndex != conn->security_ix)
734 goto wrong_security;
David Howells563ea7d2016-08-23 15:27:25 +0100735
David Howells248f2192016-09-08 11:10:12 +0100736 if (sp->hdr.callNumber == 0) {
737 /* Connection-level packet */
738 _debug("CONN %p {%d}", conn, conn->debug_id);
739 rxrpc_post_packet_to_conn(conn, skb);
740 goto out_unlock;
741 }
David Howells8496af52016-07-01 07:51:50 +0100742
David Howells248f2192016-09-08 11:10:12 +0100743 /* Note the serial number skew here */
744 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
745 if (skew >= 0) {
746 if (skew > 0)
747 conn->hi_serial = sp->hdr.serial;
748 } else {
749 skew = -skew;
750 skew = min(skew, 65535);
751 }
752
David Howells8496af52016-07-01 07:51:50 +0100753 /* Call-bound packets are routed by connection channel. */
David Howells248f2192016-09-08 11:10:12 +0100754 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
755 chan = &conn->channels[channel];
Tim Smith77276402014-03-03 23:04:45 +0000756
David Howells18bfeba2016-08-23 15:27:25 +0100757 /* Ignore really old calls */
758 if (sp->hdr.callNumber < chan->last_call)
759 goto discard_unlock;
760
761 if (sp->hdr.callNumber == chan->last_call) {
David Howells248f2192016-09-08 11:10:12 +0100762 /* For the previous service call, if completed successfully, we
763 * discard all further packets.
David Howells18bfeba2016-08-23 15:27:25 +0100764 */
David Howells2266ffd2016-08-24 13:06:14 +0100765 if (rxrpc_conn_is_service(conn) &&
David Howells18bfeba2016-08-23 15:27:25 +0100766 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
767 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
768 goto discard_unlock;
769
David Howells248f2192016-09-08 11:10:12 +0100770 /* But otherwise we need to retransmit the final packet from
771 * data cached in the connection record.
David Howells18bfeba2016-08-23 15:27:25 +0100772 */
773 rxrpc_post_packet_to_conn(conn, skb);
774 goto out_unlock;
775 }
776
777 call = rcu_dereference(chan->call);
David Howells248f2192016-09-08 11:10:12 +0100778 } else {
779 skew = 0;
780 call = NULL;
Tim Smith77276402014-03-03 23:04:45 +0000781 }
David Howells44ba0692015-04-01 16:31:26 +0100782
David Howells248f2192016-09-08 11:10:12 +0100783 if (!call || atomic_read(&call->usage) == 0) {
784 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
785 sp->hdr.callNumber == 0 ||
786 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
787 goto bad_message_unlock;
788 if (sp->hdr.seq != 1)
789 goto discard_unlock;
790 call = rxrpc_new_incoming_call(local, conn, skb);
791 if (!call) {
792 rcu_read_unlock();
793 goto reject_packet;
794 }
795 }
796
797 rxrpc_input_call_packet(call, skb, skew);
798 goto discard_unlock;
799
David Howells18bfeba2016-08-23 15:27:25 +0100800discard_unlock:
David Howells8496af52016-07-01 07:51:50 +0100801 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +0100802discard:
803 rxrpc_free_skb(skb);
David Howells44ba0692015-04-01 16:31:26 +0100804out:
David Howells49e19ec2016-09-08 11:10:12 +0100805 trace_rxrpc_rx_done(0, 0);
David Howells17926a72007-04-26 15:48:28 -0700806 return;
807
David Howells248f2192016-09-08 11:10:12 +0100808out_unlock:
David Howells8496af52016-07-01 07:51:50 +0100809 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +0100810 goto out;
David Howells8496af52016-07-01 07:51:50 +0100811
David Howells248f2192016-09-08 11:10:12 +0100812wrong_security:
813 rcu_read_unlock();
814 trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
815 RXKADINCONSISTENCY, EBADMSG);
816 skb->priority = RXKADINCONSISTENCY;
817 goto post_abort;
David Howells17926a72007-04-26 15:48:28 -0700818
David Howells248f2192016-09-08 11:10:12 +0100819bad_message_unlock:
820 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700821bad_message:
David Howells248f2192016-09-08 11:10:12 +0100822 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
823 RX_PROTOCOL_ERROR, EBADMSG);
David Howells17926a72007-04-26 15:48:28 -0700824 skb->priority = RX_PROTOCOL_ERROR;
David Howells248f2192016-09-08 11:10:12 +0100825post_abort:
826 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
David Howells49e19ec2016-09-08 11:10:12 +0100827reject_packet:
828 trace_rxrpc_rx_done(skb->mark, skb->priority);
David Howells17926a72007-04-26 15:48:28 -0700829 rxrpc_reject_packet(local, skb);
David Howells17926a72007-04-26 15:48:28 -0700830 _leave(" [badmsg]");
831}