blob: f0d9115b9b7e10fb85ebcfe94c9b0dfc04add129 [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,
David Howells75e42122016-09-13 22:36:22 +0100167 u8 annotation, bool *_jumbo_bad)
David Howells248f2192016-09-08 11:10:12 +0100168{
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 */
David Howells75e42122016-09-13 22:36:22 +0100177 if (!*_jumbo_bad) {
178 call->nr_jumbo_bad++;
179 *_jumbo_bad = true;
David Howells248f2192016-09-08 11:10:12 +0100180 }
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;
David Howells75e42122016-09-13 22:36:22 +0100194 bool immediate_ack = false, jumbo_bad = false, queued;
David Howells248f2192016-09-08 11:10:12 +0100195 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) {
David Howells75e42122016-09-13 22:36:22 +0100225 if (call->nr_jumbo_bad > 3) {
David Howells248f2192016-09-08 11:10:12 +0100226 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) {
David Howells816c9fc2016-09-17 10:49:11 +0100241 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
David Howells248f2192016-09-08 11:10:12 +0100242 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]) {
David Howells75e42122016-09-13 22:36:22 +0100262 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
David Howells248f2192016-09-08 11:10:12 +0100263 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);
David Howells816c9fc2016-09-17 10:49:11 +0100285 if (flags & RXRPC_LAST_PACKET)
286 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100287 queued = true;
288
289 if (after_eq(seq, call->rx_expect_next)) {
290 if (after(seq, call->rx_expect_next)) {
291 _net("OOS %u > %u", seq, call->rx_expect_next);
292 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
293 ack_serial = serial;
294 }
295 call->rx_expect_next = seq + 1;
296 }
297
298skip:
299 offset += len;
300 if (flags & RXRPC_JUMBO_PACKET) {
301 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
302 return rxrpc_proto_abort("XJF", call, seq);
303 offset += sizeof(struct rxrpc_jumbo_header);
304 seq++;
305 serial++;
306 annotation++;
307 if (flags & RXRPC_JUMBO_PACKET)
308 annotation |= RXRPC_RX_ANNO_JLAST;
David Howells75e42122016-09-13 22:36:22 +0100309 if (after(seq, hard_ack + call->rx_winsize)) {
310 ack = RXRPC_ACK_EXCEEDS_WINDOW;
311 ack_serial = serial;
312 if (!jumbo_bad) {
313 call->nr_jumbo_bad++;
314 jumbo_bad = true;
315 }
316 goto ack;
317 }
David Howells248f2192016-09-08 11:10:12 +0100318
319 _proto("Rx DATA Jumbo %%%u", serial);
320 goto next_subpacket;
321 }
322
323 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
324 ack = RXRPC_ACK_DELAY;
325 ack_serial = serial;
326 }
327
328ack:
329 if (ack)
330 rxrpc_propose_ACK(call, ack, skew, ack_serial,
331 immediate_ack, true);
332
333 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
334 rxrpc_notify_socket(call);
335 _leave(" [queued]");
336}
337
338/*
339 * Process the extra information that may be appended to an ACK packet
340 */
341static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
342 struct rxrpc_ackinfo *ackinfo)
343{
344 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
345 struct rxrpc_peer *peer;
346 unsigned int mtu;
David Howells01fd0742016-09-13 10:23:01 +0100347 u32 rwind = ntohl(ackinfo->rwind);
David Howells248f2192016-09-08 11:10:12 +0100348
349 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
350 sp->hdr.serial,
351 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
David Howells01fd0742016-09-13 10:23:01 +0100352 rwind, ntohl(ackinfo->jumbo_max));
David Howells248f2192016-09-08 11:10:12 +0100353
David Howells01fd0742016-09-13 10:23:01 +0100354 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
355 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
356 call->tx_winsize = rwind;
David Howells248f2192016-09-08 11:10:12 +0100357
358 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
359
360 peer = call->peer;
361 if (mtu < peer->maxdata) {
362 spin_lock_bh(&peer->lock);
363 peer->maxdata = mtu;
364 peer->mtu = mtu + peer->hdrsize;
365 spin_unlock_bh(&peer->lock);
366 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
367 }
368}
369
370/*
371 * Process individual soft ACKs.
372 *
373 * Each ACK in the array corresponds to one packet and can be either an ACK or
374 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
375 * packets that lie beyond the end of the ACK list are scheduled for resend by
376 * the timer on the basis that the peer might just not have processed them at
377 * the time the ACK was sent.
378 */
379static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
380 rxrpc_seq_t seq, int nr_acks)
381{
382 bool resend = false;
383 int ix;
384
385 for (; nr_acks > 0; nr_acks--, seq++) {
386 ix = seq & RXRPC_RXTX_BUFF_MASK;
387 switch (*acks) {
388 case RXRPC_ACK_TYPE_ACK:
389 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_ACK;
390 break;
391 case RXRPC_ACK_TYPE_NACK:
392 if (call->rxtx_annotations[ix] == RXRPC_TX_ANNO_NAK)
393 continue;
394 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_NAK;
395 resend = true;
396 break;
397 default:
398 return rxrpc_proto_abort("SFT", call, 0);
399 }
400 }
401
402 if (resend &&
403 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
404 rxrpc_queue_call(call);
405}
406
407/*
408 * Process an ACK packet.
409 *
410 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
411 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
412 *
413 * A hard-ACK means that a packet has been processed and may be discarded; a
414 * soft-ACK means that the packet may be discarded and retransmission
415 * requested. A phase is complete when all packets are hard-ACK'd.
416 */
417static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
418 u16 skew)
419{
420 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
421 union {
422 struct rxrpc_ackpacket ack;
423 struct rxrpc_ackinfo info;
424 u8 acks[RXRPC_MAXACKS];
425 } buf;
426 rxrpc_seq_t first_soft_ack, hard_ack;
427 int nr_acks, offset;
428
429 _enter("");
430
431 if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
432 _debug("extraction failure");
433 return rxrpc_proto_abort("XAK", call, 0);
434 }
435 sp->offset += sizeof(buf.ack);
436
437 first_soft_ack = ntohl(buf.ack.firstPacket);
438 hard_ack = first_soft_ack - 1;
439 nr_acks = buf.ack.nAcks;
440
441 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
442 sp->hdr.serial,
443 ntohs(buf.ack.maxSkew),
444 first_soft_ack,
445 ntohl(buf.ack.previousPacket),
446 ntohl(buf.ack.serial),
447 rxrpc_acks(buf.ack.reason),
448 buf.ack.nAcks);
449
450 if (buf.ack.reason == RXRPC_ACK_PING) {
451 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
452 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
453 skew, sp->hdr.serial, true, true);
454 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
455 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
456 skew, sp->hdr.serial, true, true);
457 }
458
459 offset = sp->offset + nr_acks + 3;
David Howells89a80ed2016-09-13 22:36:22 +0100460 if (skb->len >= offset + sizeof(buf.info)) {
David Howells248f2192016-09-08 11:10:12 +0100461 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
462 return rxrpc_proto_abort("XAI", call, 0);
463 rxrpc_input_ackinfo(call, skb, &buf.info);
464 }
465
466 if (first_soft_ack == 0)
467 return rxrpc_proto_abort("AK0", call, 0);
468
469 /* Ignore ACKs unless we are or have just been transmitting. */
470 switch (call->state) {
471 case RXRPC_CALL_CLIENT_SEND_REQUEST:
472 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
473 case RXRPC_CALL_SERVER_SEND_REPLY:
474 case RXRPC_CALL_SERVER_AWAIT_ACK:
475 break;
476 default:
477 return;
478 }
479
480 /* Discard any out-of-order or duplicate ACKs. */
481 if ((int)sp->hdr.serial - (int)call->acks_latest <= 0) {
482 _debug("discard ACK %d <= %d",
483 sp->hdr.serial, call->acks_latest);
484 return;
485 }
486 call->acks_latest = sp->hdr.serial;
487
488 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
489 hard_ack == call->tx_top) {
490 rxrpc_end_tx_phase(call, "ETA");
491 return;
492 }
493
494 if (before(hard_ack, call->tx_hard_ack) ||
495 after(hard_ack, call->tx_top))
496 return rxrpc_proto_abort("AKW", call, 0);
497
498 if (after(hard_ack, call->tx_hard_ack))
499 rxrpc_rotate_tx_window(call, hard_ack);
500
501 if (after(first_soft_ack, call->tx_top))
502 return;
503
504 if (nr_acks > call->tx_top - first_soft_ack + 1)
505 nr_acks = first_soft_ack - call->tx_top + 1;
506 if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
507 return rxrpc_proto_abort("XSA", call, 0);
508 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
509}
510
511/*
512 * Process an ACKALL packet.
513 */
514static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
515{
516 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
517
518 _proto("Rx ACKALL %%%u", sp->hdr.serial);
519
520 rxrpc_end_tx_phase(call, "ETL");
521}
522
523/*
524 * Process an ABORT packet.
525 */
526static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700527{
528 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000529 __be32 wtmp;
David Howells248f2192016-09-08 11:10:12 +0100530 u32 abort_code = RX_CALL_DEAD;
531
532 _enter("");
533
534 if (skb->len >= 4 &&
535 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
536 abort_code = ntohl(wtmp);
537
538 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
539
540 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
541 abort_code, ECONNABORTED))
542 rxrpc_notify_socket(call);
543}
544
545/*
546 * Process an incoming call packet.
547 */
548static void rxrpc_input_call_packet(struct rxrpc_call *call,
549 struct sk_buff *skb, u16 skew)
550{
551 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700552
553 _enter("%p,%p", call, skb);
554
David Howells17926a72007-04-26 15:48:28 -0700555 switch (sp->hdr.type) {
David Howells248f2192016-09-08 11:10:12 +0100556 case RXRPC_PACKET_TYPE_DATA:
557 rxrpc_input_data(call, skb, skew);
558 break;
David Howells17926a72007-04-26 15:48:28 -0700559
David Howells248f2192016-09-08 11:10:12 +0100560 case RXRPC_PACKET_TYPE_ACK:
561 rxrpc_input_ack(call, skb, skew);
562 break;
David Howells17926a72007-04-26 15:48:28 -0700563
564 case RXRPC_PACKET_TYPE_BUSY:
David Howells0d12f8a2016-03-04 15:53:46 +0000565 _proto("Rx BUSY %%%u", sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700566
David Howells248f2192016-09-08 11:10:12 +0100567 /* Just ignore BUSY packets from the server; the retry and
568 * lifespan timers will take care of business. BUSY packets
569 * from the client don't make sense.
570 */
571 break;
David Howells17926a72007-04-26 15:48:28 -0700572
David Howells248f2192016-09-08 11:10:12 +0100573 case RXRPC_PACKET_TYPE_ABORT:
574 rxrpc_input_abort(call, skb);
575 break;
576
577 case RXRPC_PACKET_TYPE_ACKALL:
578 rxrpc_input_ackall(call, skb);
579 break;
David Howells17926a72007-04-26 15:48:28 -0700580
581 default:
David Howells0d12f8a2016-03-04 15:53:46 +0000582 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700583 break;
584 }
585
David Howells17926a72007-04-26 15:48:28 -0700586 _leave("");
587}
588
589/*
590 * post connection-level events to the connection
David Howells18bfeba2016-08-23 15:27:25 +0100591 * - this includes challenges, responses, some aborts and call terminal packet
592 * retransmission.
David Howells17926a72007-04-26 15:48:28 -0700593 */
David Howells2e7e9752016-08-09 10:11:48 +0100594static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
David Howells17926a72007-04-26 15:48:28 -0700595 struct sk_buff *skb)
596{
597 _enter("%p,%p", conn, skb);
598
David Howells17926a72007-04-26 15:48:28 -0700599 skb_queue_tail(&conn->rx_queue, skb);
David Howells2e7e9752016-08-09 10:11:48 +0100600 rxrpc_queue_conn(conn);
David Howells17926a72007-04-26 15:48:28 -0700601}
602
David Howells44ba0692015-04-01 16:31:26 +0100603/*
604 * post endpoint-level events to the local endpoint
605 * - this includes debug and version messages
606 */
607static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
608 struct sk_buff *skb)
609{
610 _enter("%p,%p", local, skb);
611
David Howells44ba0692015-04-01 16:31:26 +0100612 skb_queue_tail(&local->event_queue, skb);
David Howells5acbee42016-06-27 10:32:02 +0100613 rxrpc_queue_local(local);
David Howells44ba0692015-04-01 16:31:26 +0100614}
615
David Howells0d12f8a2016-03-04 15:53:46 +0000616/*
David Howells248f2192016-09-08 11:10:12 +0100617 * put a packet up for transport-level abort
618 */
619static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
620{
621 CHECK_SLAB_OKAY(&local->usage);
622
623 skb_queue_tail(&local->reject_queue, skb);
624 rxrpc_queue_local(local);
625}
626
627/*
David Howells0d12f8a2016-03-04 15:53:46 +0000628 * Extract the wire header from a packet and translate the byte order.
629 */
630static noinline
631int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
632{
633 struct rxrpc_wire_header whdr;
634
635 /* dig out the RxRPC connection details */
Willem de Bruijn4d0fc732016-04-07 11:44:59 -0400636 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
David Howells0d12f8a2016-03-04 15:53:46 +0000637 return -EBADMSG;
David Howells0d12f8a2016-03-04 15:53:46 +0000638
639 memset(sp, 0, sizeof(*sp));
640 sp->hdr.epoch = ntohl(whdr.epoch);
641 sp->hdr.cid = ntohl(whdr.cid);
642 sp->hdr.callNumber = ntohl(whdr.callNumber);
643 sp->hdr.seq = ntohl(whdr.seq);
644 sp->hdr.serial = ntohl(whdr.serial);
645 sp->hdr.flags = whdr.flags;
646 sp->hdr.type = whdr.type;
647 sp->hdr.userStatus = whdr.userStatus;
648 sp->hdr.securityIndex = whdr.securityIndex;
649 sp->hdr._rsvd = ntohs(whdr._rsvd);
650 sp->hdr.serviceId = ntohs(whdr.serviceId);
David Howells248f2192016-09-08 11:10:12 +0100651 sp->offset = sizeof(whdr);
David Howells0d12f8a2016-03-04 15:53:46 +0000652 return 0;
653}
654
David Howells17926a72007-04-26 15:48:28 -0700655/*
656 * handle data received on the local endpoint
657 * - may be called in interrupt context
David Howells4f95dd72016-04-04 14:00:35 +0100658 *
659 * The socket is locked by the caller and this prevents the socket from being
660 * shut down and the local endpoint from going away, thus sk_user_data will not
661 * be cleared until this function returns.
David Howells17926a72007-04-26 15:48:28 -0700662 */
David Howells248f2192016-09-08 11:10:12 +0100663void rxrpc_data_ready(struct sock *udp_sk)
David Howells17926a72007-04-26 15:48:28 -0700664{
David Howells8496af52016-07-01 07:51:50 +0100665 struct rxrpc_connection *conn;
David Howells248f2192016-09-08 11:10:12 +0100666 struct rxrpc_channel *chan;
667 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700668 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100669 struct rxrpc_local *local = udp_sk->sk_user_data;
David Howells17926a72007-04-26 15:48:28 -0700670 struct sk_buff *skb;
David Howells248f2192016-09-08 11:10:12 +0100671 unsigned int channel;
David Howells563ea7d2016-08-23 15:27:25 +0100672 int ret, skew;
David Howells17926a72007-04-26 15:48:28 -0700673
David Howells248f2192016-09-08 11:10:12 +0100674 _enter("%p", udp_sk);
David Howells17926a72007-04-26 15:48:28 -0700675
676 ASSERT(!irqs_disabled());
677
David Howells248f2192016-09-08 11:10:12 +0100678 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
David Howells17926a72007-04-26 15:48:28 -0700679 if (!skb) {
David Howells17926a72007-04-26 15:48:28 -0700680 if (ret == -EAGAIN)
681 return;
682 _debug("UDP socket error %d", ret);
683 return;
684 }
685
686 rxrpc_new_skb(skb);
687
688 _net("recv skb %p", skb);
689
690 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
691 if (skb_checksum_complete(skb)) {
692 rxrpc_free_skb(skb);
Eric Dumazet02c22342016-04-27 16:44:30 -0700693 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
David Howells17926a72007-04-26 15:48:28 -0700694 _leave(" [CSUM failed]");
695 return;
696 }
697
Eric Dumazet02c22342016-04-27 16:44:30 -0700698 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
Herbert Xu1781f7f2007-12-11 11:30:32 -0800699
David Howells0d12f8a2016-03-04 15:53:46 +0000700 /* The socket buffer we have is owned by UDP, with UDP's data all over
701 * it, but we really want our own data there.
702 */
David Howells17926a72007-04-26 15:48:28 -0700703 skb_orphan(skb);
704 sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700705
706 _net("Rx UDP packet from %08x:%04hu",
707 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
708
709 /* dig out the RxRPC connection details */
David Howells0d12f8a2016-03-04 15:53:46 +0000710 if (rxrpc_extract_header(sp, skb) < 0)
David Howells17926a72007-04-26 15:48:28 -0700711 goto bad_message;
David Howells49e19ec2016-09-08 11:10:12 +0100712 trace_rxrpc_rx_packet(sp);
David Howells17926a72007-04-26 15:48:28 -0700713
714 _net("Rx RxRPC %s ep=%x call=%x:%x",
715 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
David Howells0d12f8a2016-03-04 15:53:46 +0000716 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
David Howells17926a72007-04-26 15:48:28 -0700717
David Howells351c1e62016-03-04 15:56:06 +0000718 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
719 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
David Howells17926a72007-04-26 15:48:28 -0700720 _proto("Rx Bad Packet Type %u", sp->hdr.type);
721 goto bad_message;
722 }
723
David Howells248f2192016-09-08 11:10:12 +0100724 switch (sp->hdr.type) {
725 case RXRPC_PACKET_TYPE_VERSION:
David Howells44ba0692015-04-01 16:31:26 +0100726 rxrpc_post_packet_to_local(local, skb);
727 goto out;
David Howellsbc6e1ea2016-06-10 22:30:27 +0100728
David Howells248f2192016-09-08 11:10:12 +0100729 case RXRPC_PACKET_TYPE_BUSY:
730 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
731 goto discard;
732
733 case RXRPC_PACKET_TYPE_DATA:
734 if (sp->hdr.callNumber == 0)
735 goto bad_message;
736 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
737 !rxrpc_validate_jumbo(skb))
738 goto bad_message;
739 break;
740 }
David Howells17926a72007-04-26 15:48:28 -0700741
David Howells8496af52016-07-01 07:51:50 +0100742 rcu_read_lock();
743
David Howells8496af52016-07-01 07:51:50 +0100744 conn = rxrpc_find_connection_rcu(local, skb);
David Howells248f2192016-09-08 11:10:12 +0100745 if (conn) {
746 if (sp->hdr.securityIndex != conn->security_ix)
747 goto wrong_security;
David Howells563ea7d2016-08-23 15:27:25 +0100748
David Howells248f2192016-09-08 11:10:12 +0100749 if (sp->hdr.callNumber == 0) {
750 /* Connection-level packet */
751 _debug("CONN %p {%d}", conn, conn->debug_id);
752 rxrpc_post_packet_to_conn(conn, skb);
753 goto out_unlock;
754 }
David Howells8496af52016-07-01 07:51:50 +0100755
David Howells248f2192016-09-08 11:10:12 +0100756 /* Note the serial number skew here */
757 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
758 if (skew >= 0) {
759 if (skew > 0)
760 conn->hi_serial = sp->hdr.serial;
761 } else {
762 skew = -skew;
763 skew = min(skew, 65535);
764 }
765
David Howells8496af52016-07-01 07:51:50 +0100766 /* Call-bound packets are routed by connection channel. */
David Howells248f2192016-09-08 11:10:12 +0100767 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
768 chan = &conn->channels[channel];
Tim Smith77276402014-03-03 23:04:45 +0000769
David Howells18bfeba2016-08-23 15:27:25 +0100770 /* Ignore really old calls */
771 if (sp->hdr.callNumber < chan->last_call)
772 goto discard_unlock;
773
774 if (sp->hdr.callNumber == chan->last_call) {
David Howells248f2192016-09-08 11:10:12 +0100775 /* For the previous service call, if completed successfully, we
776 * discard all further packets.
David Howells18bfeba2016-08-23 15:27:25 +0100777 */
David Howells2266ffd2016-08-24 13:06:14 +0100778 if (rxrpc_conn_is_service(conn) &&
David Howells18bfeba2016-08-23 15:27:25 +0100779 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
780 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
781 goto discard_unlock;
782
David Howells248f2192016-09-08 11:10:12 +0100783 /* But otherwise we need to retransmit the final packet from
784 * data cached in the connection record.
David Howells18bfeba2016-08-23 15:27:25 +0100785 */
786 rxrpc_post_packet_to_conn(conn, skb);
787 goto out_unlock;
788 }
789
790 call = rcu_dereference(chan->call);
David Howells248f2192016-09-08 11:10:12 +0100791 } else {
792 skew = 0;
793 call = NULL;
Tim Smith77276402014-03-03 23:04:45 +0000794 }
David Howells44ba0692015-04-01 16:31:26 +0100795
David Howells248f2192016-09-08 11:10:12 +0100796 if (!call || atomic_read(&call->usage) == 0) {
797 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
798 sp->hdr.callNumber == 0 ||
799 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
800 goto bad_message_unlock;
801 if (sp->hdr.seq != 1)
802 goto discard_unlock;
803 call = rxrpc_new_incoming_call(local, conn, skb);
804 if (!call) {
805 rcu_read_unlock();
806 goto reject_packet;
807 }
808 }
809
810 rxrpc_input_call_packet(call, skb, skew);
811 goto discard_unlock;
812
David Howells18bfeba2016-08-23 15:27:25 +0100813discard_unlock:
David Howells8496af52016-07-01 07:51:50 +0100814 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +0100815discard:
816 rxrpc_free_skb(skb);
David Howells44ba0692015-04-01 16:31:26 +0100817out:
David Howells49e19ec2016-09-08 11:10:12 +0100818 trace_rxrpc_rx_done(0, 0);
David Howells17926a72007-04-26 15:48:28 -0700819 return;
820
David Howells248f2192016-09-08 11:10:12 +0100821out_unlock:
David Howells8496af52016-07-01 07:51:50 +0100822 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +0100823 goto out;
David Howells8496af52016-07-01 07:51:50 +0100824
David Howells248f2192016-09-08 11:10:12 +0100825wrong_security:
826 rcu_read_unlock();
827 trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
828 RXKADINCONSISTENCY, EBADMSG);
829 skb->priority = RXKADINCONSISTENCY;
830 goto post_abort;
David Howells17926a72007-04-26 15:48:28 -0700831
David Howells248f2192016-09-08 11:10:12 +0100832bad_message_unlock:
833 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700834bad_message:
David Howells248f2192016-09-08 11:10:12 +0100835 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
836 RX_PROTOCOL_ERROR, EBADMSG);
David Howells17926a72007-04-26 15:48:28 -0700837 skb->priority = RX_PROTOCOL_ERROR;
David Howells248f2192016-09-08 11:10:12 +0100838post_abort:
839 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
David Howells49e19ec2016-09-08 11:10:12 +0100840reject_packet:
841 trace_rxrpc_rx_done(skb->mark, skb->priority);
David Howells17926a72007-04-26 15:48:28 -0700842 rxrpc_reject_packet(local, skb);
David Howells17926a72007-04-26 15:48:28 -0700843 _leave(" [badmsg]");
844}