blob: 7ac1edf3aac70f0da2c0ab9745e77371e476e1eb [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];
David Howells71f3ca42016-09-17 10:49:14 +010053 rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
David Howells248f2192016-09-08 11:10:12 +010054 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 Howellsa124fe32016-09-17 10:49:13 +010062 trace_rxrpc_transmit(call, rxrpc_transmit_rotate);
David Howellsbc4abfc2016-09-13 22:36:21 +010063 wake_up(&call->waitq);
64
David Howells248f2192016-09-08 11:10:12 +010065 while (list) {
66 skb = list;
67 list = skb->next;
68 skb->next = NULL;
David Howells71f3ca42016-09-17 10:49:14 +010069 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells248f2192016-09-08 11:10:12 +010070 }
David Howells17926a72007-04-26 15:48:28 -070071}
72
73/*
David Howells248f2192016-09-08 11:10:12 +010074 * End the transmission phase of a call.
75 *
76 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
77 * or a final ACK packet.
David Howells17926a72007-04-26 15:48:28 -070078 */
David Howells248f2192016-09-08 11:10:12 +010079static bool rxrpc_end_tx_phase(struct rxrpc_call *call, const char *abort_why)
David Howells17926a72007-04-26 15:48:28 -070080{
David Howells248f2192016-09-08 11:10:12 +010081 _enter("");
David Howells17926a72007-04-26 15:48:28 -070082
83 switch (call->state) {
David Howells248f2192016-09-08 11:10:12 +010084 case RXRPC_CALL_CLIENT_RECV_REPLY:
85 return true;
86 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
87 case RXRPC_CALL_SERVER_AWAIT_ACK:
88 break;
89 default:
90 rxrpc_proto_abort(abort_why, call, call->tx_top);
91 return false;
92 }
93
94 rxrpc_rotate_tx_window(call, call->tx_top);
95
96 write_lock(&call->state_lock);
97
98 switch (call->state) {
99 default:
100 break;
David Howells17926a72007-04-26 15:48:28 -0700101 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
David Howells71f3ca42016-09-17 10:49:14 +0100102 call->tx_phase = false;
David Howells17926a72007-04-26 15:48:28 -0700103 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
David Howells17926a72007-04-26 15:48:28 -0700104 break;
David Howells248f2192016-09-08 11:10:12 +0100105 case RXRPC_CALL_SERVER_AWAIT_ACK:
106 __rxrpc_call_completed(call);
107 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700108 break;
109 }
David Howells248f2192016-09-08 11:10:12 +0100110
111 write_unlock(&call->state_lock);
David Howellsa124fe32016-09-17 10:49:13 +0100112 trace_rxrpc_transmit(call, rxrpc_transmit_end);
David Howells248f2192016-09-08 11:10:12 +0100113 _leave(" = ok");
114 return true;
115}
116
117/*
118 * Scan a jumbo packet to validate its structure and to work out how many
119 * subpackets it contains.
120 *
121 * A jumbo packet is a collection of consecutive packets glued together with
122 * little headers between that indicate how to change the initial header for
123 * each subpacket.
124 *
125 * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
126 * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
127 * size.
128 */
129static bool rxrpc_validate_jumbo(struct sk_buff *skb)
130{
131 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
132 unsigned int offset = sp->offset;
David Howells89a80ed2016-09-13 22:36:22 +0100133 unsigned int len = skb->len;
David Howells248f2192016-09-08 11:10:12 +0100134 int nr_jumbo = 1;
135 u8 flags = sp->hdr.flags;
136
137 do {
138 nr_jumbo++;
139 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
140 goto protocol_error;
141 if (flags & RXRPC_LAST_PACKET)
142 goto protocol_error;
143 offset += RXRPC_JUMBO_DATALEN;
144 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
145 goto protocol_error;
146 offset += sizeof(struct rxrpc_jumbo_header);
147 } while (flags & RXRPC_JUMBO_PACKET);
148
149 sp->nr_jumbo = nr_jumbo;
150 return true;
151
152protocol_error:
153 return false;
154}
155
156/*
157 * Handle reception of a duplicate packet.
158 *
159 * We have to take care to avoid an attack here whereby we're given a series of
160 * jumbograms, each with a sequence number one before the preceding one and
161 * filled up to maximum UDP size. If they never send us the first packet in
162 * the sequence, they can cause us to have to hold on to around 2MiB of kernel
163 * space until the call times out.
164 *
165 * We limit the space usage by only accepting three duplicate jumbo packets per
166 * call. After that, we tell the other side we're no longer accepting jumbos
167 * (that information is encoded in the ACK packet).
168 */
169static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
David Howells75e42122016-09-13 22:36:22 +0100170 u8 annotation, bool *_jumbo_bad)
David Howells248f2192016-09-08 11:10:12 +0100171{
172 /* Discard normal packets that are duplicates. */
173 if (annotation == 0)
174 return;
175
176 /* Skip jumbo subpackets that are duplicates. When we've had three or
177 * more partially duplicate jumbo packets, we refuse to take any more
178 * jumbos for this call.
179 */
David Howells75e42122016-09-13 22:36:22 +0100180 if (!*_jumbo_bad) {
181 call->nr_jumbo_bad++;
182 *_jumbo_bad = true;
David Howells248f2192016-09-08 11:10:12 +0100183 }
David Howells17926a72007-04-26 15:48:28 -0700184}
185
186/*
David Howells248f2192016-09-08 11:10:12 +0100187 * Process a DATA packet, adding the packet to the Rx ring.
David Howells17926a72007-04-26 15:48:28 -0700188 */
David Howells248f2192016-09-08 11:10:12 +0100189static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
190 u16 skew)
191{
192 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
193 unsigned int offset = sp->offset;
194 unsigned int ix;
195 rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
196 rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
David Howells75e42122016-09-13 22:36:22 +0100197 bool immediate_ack = false, jumbo_bad = false, queued;
David Howells248f2192016-09-08 11:10:12 +0100198 u16 len;
199 u8 ack = 0, flags, annotation = 0;
200
201 _enter("{%u,%u},{%u,%u}",
David Howells89a80ed2016-09-13 22:36:22 +0100202 call->rx_hard_ack, call->rx_top, skb->len, seq);
David Howells248f2192016-09-08 11:10:12 +0100203
204 _proto("Rx DATA %%%u { #%u f=%02x }",
205 sp->hdr.serial, seq, sp->hdr.flags);
206
207 if (call->state >= RXRPC_CALL_COMPLETE)
208 return;
209
210 /* Received data implicitly ACKs all of the request packets we sent
211 * when we're acting as a client.
212 */
213 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY &&
214 !rxrpc_end_tx_phase(call, "ETD"))
215 return;
216
217 call->ackr_prev_seq = seq;
218
219 hard_ack = READ_ONCE(call->rx_hard_ack);
220 if (after(seq, hard_ack + call->rx_winsize)) {
221 ack = RXRPC_ACK_EXCEEDS_WINDOW;
222 ack_serial = serial;
223 goto ack;
224 }
225
226 flags = sp->hdr.flags;
227 if (flags & RXRPC_JUMBO_PACKET) {
David Howells75e42122016-09-13 22:36:22 +0100228 if (call->nr_jumbo_bad > 3) {
David Howells248f2192016-09-08 11:10:12 +0100229 ack = RXRPC_ACK_NOSPACE;
230 ack_serial = serial;
231 goto ack;
232 }
233 annotation = 1;
234 }
235
236next_subpacket:
237 queued = false;
238 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells89a80ed2016-09-13 22:36:22 +0100239 len = skb->len;
David Howells248f2192016-09-08 11:10:12 +0100240 if (flags & RXRPC_JUMBO_PACKET)
241 len = RXRPC_JUMBO_DATALEN;
242
243 if (flags & RXRPC_LAST_PACKET) {
David Howells816c9fc2016-09-17 10:49:11 +0100244 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
David Howells248f2192016-09-08 11:10:12 +0100245 seq != call->rx_top)
246 return rxrpc_proto_abort("LSN", call, seq);
247 } else {
248 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
249 after_eq(seq, call->rx_top))
250 return rxrpc_proto_abort("LSA", call, seq);
251 }
252
253 if (before_eq(seq, hard_ack)) {
254 ack = RXRPC_ACK_DUPLICATE;
255 ack_serial = serial;
256 goto skip;
257 }
258
259 if (flags & RXRPC_REQUEST_ACK && !ack) {
260 ack = RXRPC_ACK_REQUESTED;
261 ack_serial = serial;
262 }
263
264 if (call->rxtx_buffer[ix]) {
David Howells75e42122016-09-13 22:36:22 +0100265 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
David Howells248f2192016-09-08 11:10:12 +0100266 if (ack != RXRPC_ACK_DUPLICATE) {
267 ack = RXRPC_ACK_DUPLICATE;
268 ack_serial = serial;
269 }
270 immediate_ack = true;
271 goto skip;
272 }
273
274 /* Queue the packet. We use a couple of memory barriers here as need
275 * to make sure that rx_top is perceived to be set after the buffer
276 * pointer and that the buffer pointer is set after the annotation and
277 * the skb data.
278 *
279 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
280 * and also rxrpc_fill_out_ack().
281 */
David Howells71f3ca42016-09-17 10:49:14 +0100282 rxrpc_get_skb(skb, rxrpc_skb_rx_got);
David Howells248f2192016-09-08 11:10:12 +0100283 call->rxtx_annotations[ix] = annotation;
284 smp_wmb();
285 call->rxtx_buffer[ix] = skb;
286 if (after(seq, call->rx_top))
287 smp_store_release(&call->rx_top, seq);
David Howells58dc63c2016-09-17 10:49:13 +0100288 if (flags & RXRPC_LAST_PACKET) {
David Howells816c9fc2016-09-17 10:49:11 +0100289 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
David Howells58dc63c2016-09-17 10:49:13 +0100290 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
291 } else {
292 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
293 }
David Howells248f2192016-09-08 11:10:12 +0100294 queued = true;
295
296 if (after_eq(seq, call->rx_expect_next)) {
297 if (after(seq, call->rx_expect_next)) {
298 _net("OOS %u > %u", seq, call->rx_expect_next);
299 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
300 ack_serial = serial;
301 }
302 call->rx_expect_next = seq + 1;
303 }
304
305skip:
306 offset += len;
307 if (flags & RXRPC_JUMBO_PACKET) {
308 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
309 return rxrpc_proto_abort("XJF", call, seq);
310 offset += sizeof(struct rxrpc_jumbo_header);
311 seq++;
312 serial++;
313 annotation++;
314 if (flags & RXRPC_JUMBO_PACKET)
315 annotation |= RXRPC_RX_ANNO_JLAST;
David Howells75e42122016-09-13 22:36:22 +0100316 if (after(seq, hard_ack + call->rx_winsize)) {
317 ack = RXRPC_ACK_EXCEEDS_WINDOW;
318 ack_serial = serial;
319 if (!jumbo_bad) {
320 call->nr_jumbo_bad++;
321 jumbo_bad = true;
322 }
323 goto ack;
324 }
David Howells248f2192016-09-08 11:10:12 +0100325
326 _proto("Rx DATA Jumbo %%%u", serial);
327 goto next_subpacket;
328 }
329
330 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
331 ack = RXRPC_ACK_DELAY;
332 ack_serial = serial;
333 }
334
335ack:
336 if (ack)
337 rxrpc_propose_ACK(call, ack, skew, ack_serial,
338 immediate_ack, true);
339
340 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
341 rxrpc_notify_socket(call);
342 _leave(" [queued]");
343}
344
345/*
346 * Process the extra information that may be appended to an ACK packet
347 */
348static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
349 struct rxrpc_ackinfo *ackinfo)
350{
351 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
352 struct rxrpc_peer *peer;
353 unsigned int mtu;
David Howells01fd0742016-09-13 10:23:01 +0100354 u32 rwind = ntohl(ackinfo->rwind);
David Howells248f2192016-09-08 11:10:12 +0100355
356 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
357 sp->hdr.serial,
358 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
David Howells01fd0742016-09-13 10:23:01 +0100359 rwind, ntohl(ackinfo->jumbo_max));
David Howells248f2192016-09-08 11:10:12 +0100360
David Howells01fd0742016-09-13 10:23:01 +0100361 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
362 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
363 call->tx_winsize = rwind;
David Howells248f2192016-09-08 11:10:12 +0100364
365 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
366
367 peer = call->peer;
368 if (mtu < peer->maxdata) {
369 spin_lock_bh(&peer->lock);
370 peer->maxdata = mtu;
371 peer->mtu = mtu + peer->hdrsize;
372 spin_unlock_bh(&peer->lock);
373 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
374 }
375}
376
377/*
378 * Process individual soft ACKs.
379 *
380 * Each ACK in the array corresponds to one packet and can be either an ACK or
381 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
382 * packets that lie beyond the end of the ACK list are scheduled for resend by
383 * the timer on the basis that the peer might just not have processed them at
384 * the time the ACK was sent.
385 */
386static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
387 rxrpc_seq_t seq, int nr_acks)
388{
389 bool resend = false;
390 int ix;
391
392 for (; nr_acks > 0; nr_acks--, seq++) {
393 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howellsd01dc4c2016-09-17 10:49:12 +0100394 switch (*acks++) {
David Howells248f2192016-09-08 11:10:12 +0100395 case RXRPC_ACK_TYPE_ACK:
396 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_ACK;
397 break;
398 case RXRPC_ACK_TYPE_NACK:
399 if (call->rxtx_annotations[ix] == RXRPC_TX_ANNO_NAK)
400 continue;
401 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_NAK;
402 resend = true;
403 break;
404 default:
405 return rxrpc_proto_abort("SFT", call, 0);
406 }
407 }
408
409 if (resend &&
410 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
411 rxrpc_queue_call(call);
412}
413
414/*
415 * Process an ACK packet.
416 *
417 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
418 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
419 *
420 * A hard-ACK means that a packet has been processed and may be discarded; a
421 * soft-ACK means that the packet may be discarded and retransmission
422 * requested. A phase is complete when all packets are hard-ACK'd.
423 */
424static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
425 u16 skew)
426{
427 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
428 union {
429 struct rxrpc_ackpacket ack;
430 struct rxrpc_ackinfo info;
431 u8 acks[RXRPC_MAXACKS];
432 } buf;
433 rxrpc_seq_t first_soft_ack, hard_ack;
434 int nr_acks, offset;
435
436 _enter("");
437
438 if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
439 _debug("extraction failure");
440 return rxrpc_proto_abort("XAK", call, 0);
441 }
442 sp->offset += sizeof(buf.ack);
443
444 first_soft_ack = ntohl(buf.ack.firstPacket);
445 hard_ack = first_soft_ack - 1;
446 nr_acks = buf.ack.nAcks;
447
David Howellsec71eb92016-09-17 10:49:13 +0100448 trace_rxrpc_rx_ack(call, first_soft_ack, buf.ack.reason, nr_acks);
449
David Howells248f2192016-09-08 11:10:12 +0100450 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
451 sp->hdr.serial,
452 ntohs(buf.ack.maxSkew),
453 first_soft_ack,
454 ntohl(buf.ack.previousPacket),
455 ntohl(buf.ack.serial),
456 rxrpc_acks(buf.ack.reason),
457 buf.ack.nAcks);
458
459 if (buf.ack.reason == RXRPC_ACK_PING) {
460 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
461 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
462 skew, sp->hdr.serial, true, true);
463 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
464 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
465 skew, sp->hdr.serial, true, true);
466 }
467
468 offset = sp->offset + nr_acks + 3;
David Howells89a80ed2016-09-13 22:36:22 +0100469 if (skb->len >= offset + sizeof(buf.info)) {
David Howells248f2192016-09-08 11:10:12 +0100470 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
471 return rxrpc_proto_abort("XAI", call, 0);
472 rxrpc_input_ackinfo(call, skb, &buf.info);
473 }
474
475 if (first_soft_ack == 0)
476 return rxrpc_proto_abort("AK0", call, 0);
477
478 /* Ignore ACKs unless we are or have just been transmitting. */
479 switch (call->state) {
480 case RXRPC_CALL_CLIENT_SEND_REQUEST:
481 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
482 case RXRPC_CALL_SERVER_SEND_REPLY:
483 case RXRPC_CALL_SERVER_AWAIT_ACK:
484 break;
485 default:
486 return;
487 }
488
489 /* Discard any out-of-order or duplicate ACKs. */
490 if ((int)sp->hdr.serial - (int)call->acks_latest <= 0) {
491 _debug("discard ACK %d <= %d",
492 sp->hdr.serial, call->acks_latest);
493 return;
494 }
495 call->acks_latest = sp->hdr.serial;
496
497 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
498 hard_ack == call->tx_top) {
499 rxrpc_end_tx_phase(call, "ETA");
500 return;
501 }
502
503 if (before(hard_ack, call->tx_hard_ack) ||
504 after(hard_ack, call->tx_top))
505 return rxrpc_proto_abort("AKW", call, 0);
506
507 if (after(hard_ack, call->tx_hard_ack))
508 rxrpc_rotate_tx_window(call, hard_ack);
509
510 if (after(first_soft_ack, call->tx_top))
511 return;
512
513 if (nr_acks > call->tx_top - first_soft_ack + 1)
514 nr_acks = first_soft_ack - call->tx_top + 1;
515 if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
516 return rxrpc_proto_abort("XSA", call, 0);
517 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks);
518}
519
520/*
521 * Process an ACKALL packet.
522 */
523static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
524{
525 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
526
527 _proto("Rx ACKALL %%%u", sp->hdr.serial);
528
529 rxrpc_end_tx_phase(call, "ETL");
530}
531
532/*
533 * Process an ABORT packet.
534 */
535static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700536{
537 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000538 __be32 wtmp;
David Howells248f2192016-09-08 11:10:12 +0100539 u32 abort_code = RX_CALL_DEAD;
540
541 _enter("");
542
543 if (skb->len >= 4 &&
544 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
545 abort_code = ntohl(wtmp);
546
547 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
548
549 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
550 abort_code, ECONNABORTED))
551 rxrpc_notify_socket(call);
552}
553
554/*
555 * Process an incoming call packet.
556 */
557static void rxrpc_input_call_packet(struct rxrpc_call *call,
558 struct sk_buff *skb, u16 skew)
559{
560 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700561
562 _enter("%p,%p", call, skb);
563
David Howells17926a72007-04-26 15:48:28 -0700564 switch (sp->hdr.type) {
David Howells248f2192016-09-08 11:10:12 +0100565 case RXRPC_PACKET_TYPE_DATA:
566 rxrpc_input_data(call, skb, skew);
567 break;
David Howells17926a72007-04-26 15:48:28 -0700568
David Howells248f2192016-09-08 11:10:12 +0100569 case RXRPC_PACKET_TYPE_ACK:
570 rxrpc_input_ack(call, skb, skew);
571 break;
David Howells17926a72007-04-26 15:48:28 -0700572
573 case RXRPC_PACKET_TYPE_BUSY:
David Howells0d12f8a2016-03-04 15:53:46 +0000574 _proto("Rx BUSY %%%u", sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700575
David Howells248f2192016-09-08 11:10:12 +0100576 /* Just ignore BUSY packets from the server; the retry and
577 * lifespan timers will take care of business. BUSY packets
578 * from the client don't make sense.
579 */
580 break;
David Howells17926a72007-04-26 15:48:28 -0700581
David Howells248f2192016-09-08 11:10:12 +0100582 case RXRPC_PACKET_TYPE_ABORT:
583 rxrpc_input_abort(call, skb);
584 break;
585
586 case RXRPC_PACKET_TYPE_ACKALL:
587 rxrpc_input_ackall(call, skb);
588 break;
David Howells17926a72007-04-26 15:48:28 -0700589
590 default:
David Howells0d12f8a2016-03-04 15:53:46 +0000591 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700592 break;
593 }
594
David Howells17926a72007-04-26 15:48:28 -0700595 _leave("");
596}
597
598/*
599 * post connection-level events to the connection
David Howells18bfeba2016-08-23 15:27:25 +0100600 * - this includes challenges, responses, some aborts and call terminal packet
601 * retransmission.
David Howells17926a72007-04-26 15:48:28 -0700602 */
David Howells2e7e9752016-08-09 10:11:48 +0100603static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
David Howells17926a72007-04-26 15:48:28 -0700604 struct sk_buff *skb)
605{
606 _enter("%p,%p", conn, skb);
607
David Howells17926a72007-04-26 15:48:28 -0700608 skb_queue_tail(&conn->rx_queue, skb);
David Howells2e7e9752016-08-09 10:11:48 +0100609 rxrpc_queue_conn(conn);
David Howells17926a72007-04-26 15:48:28 -0700610}
611
David Howells44ba0692015-04-01 16:31:26 +0100612/*
613 * post endpoint-level events to the local endpoint
614 * - this includes debug and version messages
615 */
616static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
617 struct sk_buff *skb)
618{
619 _enter("%p,%p", local, skb);
620
David Howells44ba0692015-04-01 16:31:26 +0100621 skb_queue_tail(&local->event_queue, skb);
David Howells5acbee42016-06-27 10:32:02 +0100622 rxrpc_queue_local(local);
David Howells44ba0692015-04-01 16:31:26 +0100623}
624
David Howells0d12f8a2016-03-04 15:53:46 +0000625/*
David Howells248f2192016-09-08 11:10:12 +0100626 * put a packet up for transport-level abort
627 */
628static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
629{
630 CHECK_SLAB_OKAY(&local->usage);
631
632 skb_queue_tail(&local->reject_queue, skb);
633 rxrpc_queue_local(local);
634}
635
636/*
David Howells0d12f8a2016-03-04 15:53:46 +0000637 * Extract the wire header from a packet and translate the byte order.
638 */
639static noinline
640int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
641{
642 struct rxrpc_wire_header whdr;
643
644 /* dig out the RxRPC connection details */
Willem de Bruijn4d0fc732016-04-07 11:44:59 -0400645 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
David Howells0d12f8a2016-03-04 15:53:46 +0000646 return -EBADMSG;
David Howells0d12f8a2016-03-04 15:53:46 +0000647
648 memset(sp, 0, sizeof(*sp));
649 sp->hdr.epoch = ntohl(whdr.epoch);
650 sp->hdr.cid = ntohl(whdr.cid);
651 sp->hdr.callNumber = ntohl(whdr.callNumber);
652 sp->hdr.seq = ntohl(whdr.seq);
653 sp->hdr.serial = ntohl(whdr.serial);
654 sp->hdr.flags = whdr.flags;
655 sp->hdr.type = whdr.type;
656 sp->hdr.userStatus = whdr.userStatus;
657 sp->hdr.securityIndex = whdr.securityIndex;
658 sp->hdr._rsvd = ntohs(whdr._rsvd);
659 sp->hdr.serviceId = ntohs(whdr.serviceId);
David Howells248f2192016-09-08 11:10:12 +0100660 sp->offset = sizeof(whdr);
David Howells0d12f8a2016-03-04 15:53:46 +0000661 return 0;
662}
663
David Howells17926a72007-04-26 15:48:28 -0700664/*
665 * handle data received on the local endpoint
666 * - may be called in interrupt context
David Howells4f95dd72016-04-04 14:00:35 +0100667 *
668 * The socket is locked by the caller and this prevents the socket from being
669 * shut down and the local endpoint from going away, thus sk_user_data will not
670 * be cleared until this function returns.
David Howells17926a72007-04-26 15:48:28 -0700671 */
David Howells248f2192016-09-08 11:10:12 +0100672void rxrpc_data_ready(struct sock *udp_sk)
David Howells17926a72007-04-26 15:48:28 -0700673{
David Howells8496af52016-07-01 07:51:50 +0100674 struct rxrpc_connection *conn;
David Howells248f2192016-09-08 11:10:12 +0100675 struct rxrpc_channel *chan;
676 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700677 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100678 struct rxrpc_local *local = udp_sk->sk_user_data;
David Howells17926a72007-04-26 15:48:28 -0700679 struct sk_buff *skb;
David Howells248f2192016-09-08 11:10:12 +0100680 unsigned int channel;
David Howells563ea7d2016-08-23 15:27:25 +0100681 int ret, skew;
David Howells17926a72007-04-26 15:48:28 -0700682
David Howells248f2192016-09-08 11:10:12 +0100683 _enter("%p", udp_sk);
David Howells17926a72007-04-26 15:48:28 -0700684
685 ASSERT(!irqs_disabled());
686
David Howells248f2192016-09-08 11:10:12 +0100687 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
David Howells17926a72007-04-26 15:48:28 -0700688 if (!skb) {
David Howells17926a72007-04-26 15:48:28 -0700689 if (ret == -EAGAIN)
690 return;
691 _debug("UDP socket error %d", ret);
692 return;
693 }
694
David Howells71f3ca42016-09-17 10:49:14 +0100695 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
David Howells17926a72007-04-26 15:48:28 -0700696
697 _net("recv skb %p", skb);
698
699 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
700 if (skb_checksum_complete(skb)) {
David Howells71f3ca42016-09-17 10:49:14 +0100701 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
Eric Dumazet02c22342016-04-27 16:44:30 -0700702 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
David Howells17926a72007-04-26 15:48:28 -0700703 _leave(" [CSUM failed]");
704 return;
705 }
706
Eric Dumazet02c22342016-04-27 16:44:30 -0700707 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
Herbert Xu1781f7f2007-12-11 11:30:32 -0800708
David Howells0d12f8a2016-03-04 15:53:46 +0000709 /* The socket buffer we have is owned by UDP, with UDP's data all over
710 * it, but we really want our own data there.
711 */
David Howells17926a72007-04-26 15:48:28 -0700712 skb_orphan(skb);
713 sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700714
David Howells8a681c362016-09-17 10:49:15 +0100715 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
716 static int lose;
717 if ((lose++ & 7) == 7) {
718 rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
719 return;
720 }
721 }
722
David Howells17926a72007-04-26 15:48:28 -0700723 _net("Rx UDP packet from %08x:%04hu",
724 ntohl(ip_hdr(skb)->saddr), ntohs(udp_hdr(skb)->source));
725
726 /* dig out the RxRPC connection details */
David Howells0d12f8a2016-03-04 15:53:46 +0000727 if (rxrpc_extract_header(sp, skb) < 0)
David Howells17926a72007-04-26 15:48:28 -0700728 goto bad_message;
David Howells49e19ec2016-09-08 11:10:12 +0100729 trace_rxrpc_rx_packet(sp);
David Howells17926a72007-04-26 15:48:28 -0700730
731 _net("Rx RxRPC %s ep=%x call=%x:%x",
732 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
David Howells0d12f8a2016-03-04 15:53:46 +0000733 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
David Howells17926a72007-04-26 15:48:28 -0700734
David Howells351c1e62016-03-04 15:56:06 +0000735 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
736 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
David Howells17926a72007-04-26 15:48:28 -0700737 _proto("Rx Bad Packet Type %u", sp->hdr.type);
738 goto bad_message;
739 }
740
David Howells248f2192016-09-08 11:10:12 +0100741 switch (sp->hdr.type) {
742 case RXRPC_PACKET_TYPE_VERSION:
David Howells44ba0692015-04-01 16:31:26 +0100743 rxrpc_post_packet_to_local(local, skb);
744 goto out;
David Howellsbc6e1ea2016-06-10 22:30:27 +0100745
David Howells248f2192016-09-08 11:10:12 +0100746 case RXRPC_PACKET_TYPE_BUSY:
747 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
748 goto discard;
749
750 case RXRPC_PACKET_TYPE_DATA:
751 if (sp->hdr.callNumber == 0)
752 goto bad_message;
753 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
754 !rxrpc_validate_jumbo(skb))
755 goto bad_message;
756 break;
757 }
David Howells17926a72007-04-26 15:48:28 -0700758
David Howells8496af52016-07-01 07:51:50 +0100759 rcu_read_lock();
760
David Howells8496af52016-07-01 07:51:50 +0100761 conn = rxrpc_find_connection_rcu(local, skb);
David Howells248f2192016-09-08 11:10:12 +0100762 if (conn) {
763 if (sp->hdr.securityIndex != conn->security_ix)
764 goto wrong_security;
David Howells563ea7d2016-08-23 15:27:25 +0100765
David Howells248f2192016-09-08 11:10:12 +0100766 if (sp->hdr.callNumber == 0) {
767 /* Connection-level packet */
768 _debug("CONN %p {%d}", conn, conn->debug_id);
769 rxrpc_post_packet_to_conn(conn, skb);
770 goto out_unlock;
771 }
David Howells8496af52016-07-01 07:51:50 +0100772
David Howells248f2192016-09-08 11:10:12 +0100773 /* Note the serial number skew here */
774 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
775 if (skew >= 0) {
776 if (skew > 0)
777 conn->hi_serial = sp->hdr.serial;
778 } else {
779 skew = -skew;
780 skew = min(skew, 65535);
781 }
782
David Howells8496af52016-07-01 07:51:50 +0100783 /* Call-bound packets are routed by connection channel. */
David Howells248f2192016-09-08 11:10:12 +0100784 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
785 chan = &conn->channels[channel];
Tim Smith77276402014-03-03 23:04:45 +0000786
David Howells18bfeba2016-08-23 15:27:25 +0100787 /* Ignore really old calls */
788 if (sp->hdr.callNumber < chan->last_call)
789 goto discard_unlock;
790
791 if (sp->hdr.callNumber == chan->last_call) {
David Howells248f2192016-09-08 11:10:12 +0100792 /* For the previous service call, if completed successfully, we
793 * discard all further packets.
David Howells18bfeba2016-08-23 15:27:25 +0100794 */
David Howells2266ffd2016-08-24 13:06:14 +0100795 if (rxrpc_conn_is_service(conn) &&
David Howells18bfeba2016-08-23 15:27:25 +0100796 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
797 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
798 goto discard_unlock;
799
David Howells248f2192016-09-08 11:10:12 +0100800 /* But otherwise we need to retransmit the final packet from
801 * data cached in the connection record.
David Howells18bfeba2016-08-23 15:27:25 +0100802 */
803 rxrpc_post_packet_to_conn(conn, skb);
804 goto out_unlock;
805 }
806
807 call = rcu_dereference(chan->call);
David Howells248f2192016-09-08 11:10:12 +0100808 } else {
809 skew = 0;
810 call = NULL;
Tim Smith77276402014-03-03 23:04:45 +0000811 }
David Howells44ba0692015-04-01 16:31:26 +0100812
David Howells248f2192016-09-08 11:10:12 +0100813 if (!call || atomic_read(&call->usage) == 0) {
814 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
815 sp->hdr.callNumber == 0 ||
816 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
817 goto bad_message_unlock;
818 if (sp->hdr.seq != 1)
819 goto discard_unlock;
820 call = rxrpc_new_incoming_call(local, conn, skb);
821 if (!call) {
822 rcu_read_unlock();
823 goto reject_packet;
824 }
825 }
826
827 rxrpc_input_call_packet(call, skb, skew);
828 goto discard_unlock;
829
David Howells18bfeba2016-08-23 15:27:25 +0100830discard_unlock:
David Howells8496af52016-07-01 07:51:50 +0100831 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +0100832discard:
David Howells71f3ca42016-09-17 10:49:14 +0100833 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells44ba0692015-04-01 16:31:26 +0100834out:
David Howells49e19ec2016-09-08 11:10:12 +0100835 trace_rxrpc_rx_done(0, 0);
David Howells17926a72007-04-26 15:48:28 -0700836 return;
837
David Howells248f2192016-09-08 11:10:12 +0100838out_unlock:
David Howells8496af52016-07-01 07:51:50 +0100839 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +0100840 goto out;
David Howells8496af52016-07-01 07:51:50 +0100841
David Howells248f2192016-09-08 11:10:12 +0100842wrong_security:
843 rcu_read_unlock();
844 trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
845 RXKADINCONSISTENCY, EBADMSG);
846 skb->priority = RXKADINCONSISTENCY;
847 goto post_abort;
David Howells17926a72007-04-26 15:48:28 -0700848
David Howells248f2192016-09-08 11:10:12 +0100849bad_message_unlock:
850 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -0700851bad_message:
David Howells248f2192016-09-08 11:10:12 +0100852 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
853 RX_PROTOCOL_ERROR, EBADMSG);
David Howells17926a72007-04-26 15:48:28 -0700854 skb->priority = RX_PROTOCOL_ERROR;
David Howells248f2192016-09-08 11:10:12 +0100855post_abort:
856 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
David Howells49e19ec2016-09-08 11:10:12 +0100857reject_packet:
858 trace_rxrpc_rx_done(skb->mark, skb->priority);
David Howells17926a72007-04-26 15:48:28 -0700859 rxrpc_reject_packet(local, skb);
David Howells17926a72007-04-26 15:48:28 -0700860 _leave(" [badmsg]");
861}