blob: 0344f4494eb739c99959cd2449817090db801b61 [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 Howells8e831342016-09-22 00:29:31 +010040 * Ping the other end to fill our RTT cache and to retrieve the rwind
41 * and MTU parameters.
42 */
43static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
44 int skew)
45{
46 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howellsfc943f62016-09-22 00:29:32 +010047 ktime_t now = skb->tstamp;
David Howells8e831342016-09-22 00:29:31 +010048
David Howellsfc943f62016-09-22 00:29:32 +010049 if (call->peer->rtt_usage < 3 ||
50 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
51 rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
David Howells9c7ad432016-09-23 13:50:40 +010052 true, true,
53 rxrpc_propose_ack_ping_for_params);
David Howells8e831342016-09-22 00:29:31 +010054}
55
56/*
David Howells248f2192016-09-08 11:10:12 +010057 * Apply a hard ACK by advancing the Tx window.
David Howells17926a72007-04-26 15:48:28 -070058 */
David Howells31a1b982016-09-24 18:05:26 +010059static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
60 struct rxrpc_ack_summary *summary)
David Howells17926a72007-04-26 15:48:28 -070061{
David Howells248f2192016-09-08 11:10:12 +010062 struct sk_buff *skb, *list = NULL;
63 int ix;
David Howells70790db2016-09-23 12:39:22 +010064 u8 annotation;
David Howells17926a72007-04-26 15:48:28 -070065
David Howells31a1b982016-09-24 18:05:26 +010066 if (call->acks_lowest_nak == call->tx_hard_ack) {
67 call->acks_lowest_nak = to;
68 } else if (before_eq(call->acks_lowest_nak, to)) {
69 summary->new_low_nack = true;
70 call->acks_lowest_nak = to;
71 }
72
David Howells17926a72007-04-26 15:48:28 -070073 spin_lock(&call->lock);
74
David Howells248f2192016-09-08 11:10:12 +010075 while (before(call->tx_hard_ack, to)) {
76 call->tx_hard_ack++;
77 ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
78 skb = call->rxtx_buffer[ix];
David Howells70790db2016-09-23 12:39:22 +010079 annotation = call->rxtx_annotations[ix];
David Howells71f3ca42016-09-17 10:49:14 +010080 rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
David Howells248f2192016-09-08 11:10:12 +010081 call->rxtx_buffer[ix] = NULL;
82 call->rxtx_annotations[ix] = 0;
83 skb->next = list;
84 list = skb;
David Howells70790db2016-09-23 12:39:22 +010085
86 if (annotation & RXRPC_TX_ANNO_LAST)
87 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
David Howells31a1b982016-09-24 18:05:26 +010088 if ((annotation & RXRPC_TX_ANNO_MASK) != RXRPC_TX_ANNO_ACK)
89 summary->nr_rot_new_acks++;
David Howells17926a72007-04-26 15:48:28 -070090 }
91
92 spin_unlock(&call->lock);
David Howells17926a72007-04-26 15:48:28 -070093
David Howells70790db2016-09-23 12:39:22 +010094 trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
95 rxrpc_transmit_rotate_last :
96 rxrpc_transmit_rotate));
David Howellsbc4abfc2016-09-13 22:36:21 +010097 wake_up(&call->waitq);
98
David Howells248f2192016-09-08 11:10:12 +010099 while (list) {
100 skb = list;
101 list = skb->next;
102 skb->next = NULL;
David Howells71f3ca42016-09-17 10:49:14 +0100103 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells248f2192016-09-08 11:10:12 +0100104 }
David Howells17926a72007-04-26 15:48:28 -0700105}
106
107/*
David Howells248f2192016-09-08 11:10:12 +0100108 * End the transmission phase of a call.
109 *
110 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
111 * or a final ACK packet.
David Howells17926a72007-04-26 15:48:28 -0700112 */
David Howells70790db2016-09-23 12:39:22 +0100113static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
114 const char *abort_why)
David Howells17926a72007-04-26 15:48:28 -0700115{
David Howells17926a72007-04-26 15:48:28 -0700116
David Howells70790db2016-09-23 12:39:22 +0100117 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
David Howells248f2192016-09-08 11:10:12 +0100118
119 write_lock(&call->state_lock);
120
121 switch (call->state) {
David Howells70790db2016-09-23 12:39:22 +0100122 case RXRPC_CALL_CLIENT_SEND_REQUEST:
David Howells17926a72007-04-26 15:48:28 -0700123 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
David Howells70790db2016-09-23 12:39:22 +0100124 if (reply_begun)
125 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
126 else
127 call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
David Howells17926a72007-04-26 15:48:28 -0700128 break;
David Howells70790db2016-09-23 12:39:22 +0100129
David Howells248f2192016-09-08 11:10:12 +0100130 case RXRPC_CALL_SERVER_AWAIT_ACK:
131 __rxrpc_call_completed(call);
132 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700133 break;
David Howells70790db2016-09-23 12:39:22 +0100134
135 default:
136 goto bad_state;
David Howells17926a72007-04-26 15:48:28 -0700137 }
David Howells248f2192016-09-08 11:10:12 +0100138
139 write_unlock(&call->state_lock);
David Howells70790db2016-09-23 12:39:22 +0100140 if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) {
David Howells0d967962016-09-24 18:05:27 +0100141 rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, true,
142 rxrpc_propose_ack_client_tx_end);
David Howells70790db2016-09-23 12:39:22 +0100143 trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
144 } else {
145 trace_rxrpc_transmit(call, rxrpc_transmit_end);
146 }
David Howells248f2192016-09-08 11:10:12 +0100147 _leave(" = ok");
148 return true;
David Howells70790db2016-09-23 12:39:22 +0100149
150bad_state:
151 write_unlock(&call->state_lock);
152 kdebug("end_tx %s", rxrpc_call_states[call->state]);
153 rxrpc_proto_abort(abort_why, call, call->tx_top);
154 return false;
155}
156
157/*
158 * Begin the reply reception phase of a call.
159 */
160static bool rxrpc_receiving_reply(struct rxrpc_call *call)
161{
David Howells31a1b982016-09-24 18:05:26 +0100162 struct rxrpc_ack_summary summary = { 0 };
David Howells70790db2016-09-23 12:39:22 +0100163 rxrpc_seq_t top = READ_ONCE(call->tx_top);
164
David Howellsdd7c1ee2016-09-24 18:05:27 +0100165 if (call->ackr_reason) {
166 spin_lock_bh(&call->lock);
167 call->ackr_reason = 0;
168 call->resend_at = call->expire_at;
169 call->ack_at = call->expire_at;
170 spin_unlock_bh(&call->lock);
171 rxrpc_set_timer(call, rxrpc_timer_init_for_reply);
172 }
173
David Howells70790db2016-09-23 12:39:22 +0100174 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags))
David Howells31a1b982016-09-24 18:05:26 +0100175 rxrpc_rotate_tx_window(call, top, &summary);
David Howells70790db2016-09-23 12:39:22 +0100176 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
177 rxrpc_proto_abort("TXL", call, top);
178 return false;
179 }
180 if (!rxrpc_end_tx_phase(call, true, "ETD"))
181 return false;
182 call->tx_phase = false;
183 return true;
David Howells248f2192016-09-08 11:10:12 +0100184}
185
186/*
187 * Scan a jumbo packet to validate its structure and to work out how many
188 * subpackets it contains.
189 *
190 * A jumbo packet is a collection of consecutive packets glued together with
191 * little headers between that indicate how to change the initial header for
192 * each subpacket.
193 *
194 * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
195 * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any
196 * size.
197 */
198static bool rxrpc_validate_jumbo(struct sk_buff *skb)
199{
200 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
201 unsigned int offset = sp->offset;
David Howells89a80ed2016-09-13 22:36:22 +0100202 unsigned int len = skb->len;
David Howells248f2192016-09-08 11:10:12 +0100203 int nr_jumbo = 1;
204 u8 flags = sp->hdr.flags;
205
206 do {
207 nr_jumbo++;
208 if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
209 goto protocol_error;
210 if (flags & RXRPC_LAST_PACKET)
211 goto protocol_error;
212 offset += RXRPC_JUMBO_DATALEN;
213 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
214 goto protocol_error;
215 offset += sizeof(struct rxrpc_jumbo_header);
216 } while (flags & RXRPC_JUMBO_PACKET);
217
218 sp->nr_jumbo = nr_jumbo;
219 return true;
220
221protocol_error:
222 return false;
223}
224
225/*
226 * Handle reception of a duplicate packet.
227 *
228 * We have to take care to avoid an attack here whereby we're given a series of
229 * jumbograms, each with a sequence number one before the preceding one and
230 * filled up to maximum UDP size. If they never send us the first packet in
231 * the sequence, they can cause us to have to hold on to around 2MiB of kernel
232 * space until the call times out.
233 *
234 * We limit the space usage by only accepting three duplicate jumbo packets per
235 * call. After that, we tell the other side we're no longer accepting jumbos
236 * (that information is encoded in the ACK packet).
237 */
238static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
David Howells75e42122016-09-13 22:36:22 +0100239 u8 annotation, bool *_jumbo_bad)
David Howells248f2192016-09-08 11:10:12 +0100240{
241 /* Discard normal packets that are duplicates. */
242 if (annotation == 0)
243 return;
244
245 /* Skip jumbo subpackets that are duplicates. When we've had three or
246 * more partially duplicate jumbo packets, we refuse to take any more
247 * jumbos for this call.
248 */
David Howells75e42122016-09-13 22:36:22 +0100249 if (!*_jumbo_bad) {
250 call->nr_jumbo_bad++;
251 *_jumbo_bad = true;
David Howells248f2192016-09-08 11:10:12 +0100252 }
David Howells17926a72007-04-26 15:48:28 -0700253}
254
255/*
David Howells248f2192016-09-08 11:10:12 +0100256 * Process a DATA packet, adding the packet to the Rx ring.
David Howells17926a72007-04-26 15:48:28 -0700257 */
David Howells248f2192016-09-08 11:10:12 +0100258static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
259 u16 skew)
260{
261 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
262 unsigned int offset = sp->offset;
263 unsigned int ix;
264 rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
265 rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
David Howells75e42122016-09-13 22:36:22 +0100266 bool immediate_ack = false, jumbo_bad = false, queued;
David Howells248f2192016-09-08 11:10:12 +0100267 u16 len;
268 u8 ack = 0, flags, annotation = 0;
269
270 _enter("{%u,%u},{%u,%u}",
David Howells89a80ed2016-09-13 22:36:22 +0100271 call->rx_hard_ack, call->rx_top, skb->len, seq);
David Howells248f2192016-09-08 11:10:12 +0100272
273 _proto("Rx DATA %%%u { #%u f=%02x }",
274 sp->hdr.serial, seq, sp->hdr.flags);
275
276 if (call->state >= RXRPC_CALL_COMPLETE)
277 return;
278
279 /* Received data implicitly ACKs all of the request packets we sent
280 * when we're acting as a client.
281 */
David Howells70790db2016-09-23 12:39:22 +0100282 if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
283 call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
284 !rxrpc_receiving_reply(call))
David Howells248f2192016-09-08 11:10:12 +0100285 return;
286
287 call->ackr_prev_seq = seq;
288
289 hard_ack = READ_ONCE(call->rx_hard_ack);
290 if (after(seq, hard_ack + call->rx_winsize)) {
291 ack = RXRPC_ACK_EXCEEDS_WINDOW;
292 ack_serial = serial;
293 goto ack;
294 }
295
296 flags = sp->hdr.flags;
297 if (flags & RXRPC_JUMBO_PACKET) {
David Howells75e42122016-09-13 22:36:22 +0100298 if (call->nr_jumbo_bad > 3) {
David Howells248f2192016-09-08 11:10:12 +0100299 ack = RXRPC_ACK_NOSPACE;
300 ack_serial = serial;
301 goto ack;
302 }
303 annotation = 1;
304 }
305
306next_subpacket:
307 queued = false;
308 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howells89a80ed2016-09-13 22:36:22 +0100309 len = skb->len;
David Howells248f2192016-09-08 11:10:12 +0100310 if (flags & RXRPC_JUMBO_PACKET)
311 len = RXRPC_JUMBO_DATALEN;
312
313 if (flags & RXRPC_LAST_PACKET) {
David Howells816c9fc2016-09-17 10:49:11 +0100314 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
David Howells248f2192016-09-08 11:10:12 +0100315 seq != call->rx_top)
316 return rxrpc_proto_abort("LSN", call, seq);
317 } else {
318 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
319 after_eq(seq, call->rx_top))
320 return rxrpc_proto_abort("LSA", call, seq);
321 }
322
323 if (before_eq(seq, hard_ack)) {
324 ack = RXRPC_ACK_DUPLICATE;
325 ack_serial = serial;
326 goto skip;
327 }
328
329 if (flags & RXRPC_REQUEST_ACK && !ack) {
330 ack = RXRPC_ACK_REQUESTED;
331 ack_serial = serial;
332 }
333
334 if (call->rxtx_buffer[ix]) {
David Howells75e42122016-09-13 22:36:22 +0100335 rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
David Howells248f2192016-09-08 11:10:12 +0100336 if (ack != RXRPC_ACK_DUPLICATE) {
337 ack = RXRPC_ACK_DUPLICATE;
338 ack_serial = serial;
339 }
340 immediate_ack = true;
341 goto skip;
342 }
343
344 /* Queue the packet. We use a couple of memory barriers here as need
345 * to make sure that rx_top is perceived to be set after the buffer
346 * pointer and that the buffer pointer is set after the annotation and
347 * the skb data.
348 *
349 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
350 * and also rxrpc_fill_out_ack().
351 */
David Howells71f3ca42016-09-17 10:49:14 +0100352 rxrpc_get_skb(skb, rxrpc_skb_rx_got);
David Howells248f2192016-09-08 11:10:12 +0100353 call->rxtx_annotations[ix] = annotation;
354 smp_wmb();
355 call->rxtx_buffer[ix] = skb;
David Howellsa7056c52016-09-24 18:05:27 +0100356 if (after(seq, call->rx_top)) {
David Howells248f2192016-09-08 11:10:12 +0100357 smp_store_release(&call->rx_top, seq);
David Howellsa7056c52016-09-24 18:05:27 +0100358 } else if (before(seq, call->rx_top)) {
359 /* Send an immediate ACK if we fill in a hole */
360 if (!ack) {
361 ack = RXRPC_ACK_DELAY;
362 ack_serial = serial;
363 }
364 immediate_ack = true;
365 }
David Howells58dc63c2016-09-17 10:49:13 +0100366 if (flags & RXRPC_LAST_PACKET) {
David Howells816c9fc2016-09-17 10:49:11 +0100367 set_bit(RXRPC_CALL_RX_LAST, &call->flags);
David Howells58dc63c2016-09-17 10:49:13 +0100368 trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
369 } else {
370 trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
371 }
David Howells248f2192016-09-08 11:10:12 +0100372 queued = true;
373
374 if (after_eq(seq, call->rx_expect_next)) {
375 if (after(seq, call->rx_expect_next)) {
376 _net("OOS %u > %u", seq, call->rx_expect_next);
377 ack = RXRPC_ACK_OUT_OF_SEQUENCE;
378 ack_serial = serial;
379 }
380 call->rx_expect_next = seq + 1;
381 }
382
383skip:
384 offset += len;
385 if (flags & RXRPC_JUMBO_PACKET) {
386 if (skb_copy_bits(skb, offset, &flags, 1) < 0)
387 return rxrpc_proto_abort("XJF", call, seq);
388 offset += sizeof(struct rxrpc_jumbo_header);
389 seq++;
390 serial++;
391 annotation++;
392 if (flags & RXRPC_JUMBO_PACKET)
393 annotation |= RXRPC_RX_ANNO_JLAST;
David Howells75e42122016-09-13 22:36:22 +0100394 if (after(seq, hard_ack + call->rx_winsize)) {
395 ack = RXRPC_ACK_EXCEEDS_WINDOW;
396 ack_serial = serial;
397 if (!jumbo_bad) {
398 call->nr_jumbo_bad++;
399 jumbo_bad = true;
400 }
401 goto ack;
402 }
David Howells248f2192016-09-08 11:10:12 +0100403
404 _proto("Rx DATA Jumbo %%%u", serial);
405 goto next_subpacket;
406 }
407
408 if (queued && flags & RXRPC_LAST_PACKET && !ack) {
409 ack = RXRPC_ACK_DELAY;
410 ack_serial = serial;
411 }
412
413ack:
414 if (ack)
415 rxrpc_propose_ACK(call, ack, skew, ack_serial,
David Howells9c7ad432016-09-23 13:50:40 +0100416 immediate_ack, true,
417 rxrpc_propose_ack_input_data);
David Howells248f2192016-09-08 11:10:12 +0100418
419 if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
420 rxrpc_notify_socket(call);
421 _leave(" [queued]");
422}
423
424/*
David Howells50235c42016-09-22 00:29:31 +0100425 * Process a requested ACK.
426 */
427static void rxrpc_input_requested_ack(struct rxrpc_call *call,
428 ktime_t resp_time,
429 rxrpc_serial_t orig_serial,
430 rxrpc_serial_t ack_serial)
431{
432 struct rxrpc_skb_priv *sp;
433 struct sk_buff *skb;
434 ktime_t sent_at;
435 int ix;
436
437 for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
438 skb = call->rxtx_buffer[ix];
439 if (!skb)
440 continue;
441
442 sp = rxrpc_skb(skb);
443 if (sp->hdr.serial != orig_serial)
444 continue;
445 smp_rmb();
446 sent_at = skb->tstamp;
447 goto found;
448 }
449 return;
450
451found:
452 rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
453 orig_serial, ack_serial, sent_at, resp_time);
454}
455
456/*
David Howells8e831342016-09-22 00:29:31 +0100457 * Process a ping response.
458 */
459static void rxrpc_input_ping_response(struct rxrpc_call *call,
460 ktime_t resp_time,
461 rxrpc_serial_t orig_serial,
462 rxrpc_serial_t ack_serial)
463{
464 rxrpc_serial_t ping_serial;
465 ktime_t ping_time;
466
467 ping_time = call->ackr_ping_time;
468 smp_rmb();
469 ping_serial = call->ackr_ping;
470
471 if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
472 before(orig_serial, ping_serial))
473 return;
474 clear_bit(RXRPC_CALL_PINGING, &call->flags);
475 if (after(orig_serial, ping_serial))
476 return;
477
478 rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
479 orig_serial, ack_serial, ping_time, resp_time);
480}
481
482/*
David Howells248f2192016-09-08 11:10:12 +0100483 * Process the extra information that may be appended to an ACK packet
484 */
485static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
486 struct rxrpc_ackinfo *ackinfo)
487{
488 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
489 struct rxrpc_peer *peer;
490 unsigned int mtu;
David Howells01fd0742016-09-13 10:23:01 +0100491 u32 rwind = ntohl(ackinfo->rwind);
David Howells248f2192016-09-08 11:10:12 +0100492
493 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
494 sp->hdr.serial,
495 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
David Howells01fd0742016-09-13 10:23:01 +0100496 rwind, ntohl(ackinfo->jumbo_max));
David Howells248f2192016-09-08 11:10:12 +0100497
David Howells01fd0742016-09-13 10:23:01 +0100498 if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
499 rwind = RXRPC_RXTX_BUFF_SIZE - 1;
500 call->tx_winsize = rwind;
David Howells248f2192016-09-08 11:10:12 +0100501
502 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
503
504 peer = call->peer;
505 if (mtu < peer->maxdata) {
506 spin_lock_bh(&peer->lock);
507 peer->maxdata = mtu;
508 peer->mtu = mtu + peer->hdrsize;
509 spin_unlock_bh(&peer->lock);
510 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
511 }
512}
513
514/*
515 * Process individual soft ACKs.
516 *
517 * Each ACK in the array corresponds to one packet and can be either an ACK or
518 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
519 * packets that lie beyond the end of the ACK list are scheduled for resend by
520 * the timer on the basis that the peer might just not have processed them at
521 * the time the ACK was sent.
522 */
523static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
David Howells31a1b982016-09-24 18:05:26 +0100524 rxrpc_seq_t seq, int nr_acks,
525 struct rxrpc_ack_summary *summary)
David Howells248f2192016-09-08 11:10:12 +0100526{
527 bool resend = false;
528 int ix;
David Howellsf07373e2016-09-22 00:29:32 +0100529 u8 annotation, anno_type;
David Howells248f2192016-09-08 11:10:12 +0100530
531 for (; nr_acks > 0; nr_acks--, seq++) {
532 ix = seq & RXRPC_RXTX_BUFF_MASK;
David Howellsf07373e2016-09-22 00:29:32 +0100533 annotation = call->rxtx_annotations[ix];
534 anno_type = annotation & RXRPC_TX_ANNO_MASK;
535 annotation &= ~RXRPC_TX_ANNO_MASK;
David Howellsd01dc4c2016-09-17 10:49:12 +0100536 switch (*acks++) {
David Howells248f2192016-09-08 11:10:12 +0100537 case RXRPC_ACK_TYPE_ACK:
David Howells31a1b982016-09-24 18:05:26 +0100538 summary->nr_acks++;
David Howellsf07373e2016-09-22 00:29:32 +0100539 if (anno_type == RXRPC_TX_ANNO_ACK)
540 continue;
David Howells31a1b982016-09-24 18:05:26 +0100541 summary->nr_new_acks++;
David Howellsf07373e2016-09-22 00:29:32 +0100542 call->rxtx_annotations[ix] =
543 RXRPC_TX_ANNO_ACK | annotation;
David Howells248f2192016-09-08 11:10:12 +0100544 break;
545 case RXRPC_ACK_TYPE_NACK:
David Howells31a1b982016-09-24 18:05:26 +0100546 if (!summary->nr_nacks &&
547 call->acks_lowest_nak != seq) {
548 call->acks_lowest_nak = seq;
549 summary->new_low_nack = true;
550 }
551 summary->nr_nacks++;
David Howellsf07373e2016-09-22 00:29:32 +0100552 if (anno_type == RXRPC_TX_ANNO_NAK)
David Howells248f2192016-09-08 11:10:12 +0100553 continue;
David Howells31a1b982016-09-24 18:05:26 +0100554 summary->nr_new_nacks++;
David Howellsbe8aa332016-09-23 12:39:23 +0100555 if (anno_type == RXRPC_TX_ANNO_RETRANS)
556 continue;
David Howellsf07373e2016-09-22 00:29:32 +0100557 call->rxtx_annotations[ix] =
558 RXRPC_TX_ANNO_NAK | annotation;
David Howells248f2192016-09-08 11:10:12 +0100559 resend = true;
560 break;
561 default:
562 return rxrpc_proto_abort("SFT", call, 0);
563 }
564 }
565
566 if (resend &&
567 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
568 rxrpc_queue_call(call);
569}
570
571/*
572 * Process an ACK packet.
573 *
574 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
575 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
576 *
577 * A hard-ACK means that a packet has been processed and may be discarded; a
578 * soft-ACK means that the packet may be discarded and retransmission
579 * requested. A phase is complete when all packets are hard-ACK'd.
580 */
581static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
582 u16 skew)
583{
David Howells31a1b982016-09-24 18:05:26 +0100584 struct rxrpc_ack_summary summary = { 0 };
David Howells248f2192016-09-08 11:10:12 +0100585 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
586 union {
587 struct rxrpc_ackpacket ack;
588 struct rxrpc_ackinfo info;
589 u8 acks[RXRPC_MAXACKS];
590 } buf;
David Howells8e831342016-09-22 00:29:31 +0100591 rxrpc_serial_t acked_serial;
David Howells248f2192016-09-08 11:10:12 +0100592 rxrpc_seq_t first_soft_ack, hard_ack;
593 int nr_acks, offset;
594
595 _enter("");
596
597 if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) {
598 _debug("extraction failure");
599 return rxrpc_proto_abort("XAK", call, 0);
600 }
601 sp->offset += sizeof(buf.ack);
602
David Howells8e831342016-09-22 00:29:31 +0100603 acked_serial = ntohl(buf.ack.serial);
David Howells248f2192016-09-08 11:10:12 +0100604 first_soft_ack = ntohl(buf.ack.firstPacket);
605 hard_ack = first_soft_ack - 1;
606 nr_acks = buf.ack.nAcks;
David Howells31a1b982016-09-24 18:05:26 +0100607 summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
608 buf.ack.reason : RXRPC_ACK__INVALID);
David Howells248f2192016-09-08 11:10:12 +0100609
David Howells31a1b982016-09-24 18:05:26 +0100610 trace_rxrpc_rx_ack(call, first_soft_ack, summary.ack_reason, nr_acks);
David Howellsec71eb92016-09-17 10:49:13 +0100611
David Howells248f2192016-09-08 11:10:12 +0100612 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
613 sp->hdr.serial,
614 ntohs(buf.ack.maxSkew),
615 first_soft_ack,
616 ntohl(buf.ack.previousPacket),
David Howells8e831342016-09-22 00:29:31 +0100617 acked_serial,
David Howells31a1b982016-09-24 18:05:26 +0100618 rxrpc_ack_names[summary.ack_reason],
David Howells248f2192016-09-08 11:10:12 +0100619 buf.ack.nAcks);
620
David Howells8e831342016-09-22 00:29:31 +0100621 if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
622 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
623 sp->hdr.serial);
David Howells50235c42016-09-22 00:29:31 +0100624 if (buf.ack.reason == RXRPC_ACK_REQUESTED)
625 rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
626 sp->hdr.serial);
David Howells8e831342016-09-22 00:29:31 +0100627
David Howells248f2192016-09-08 11:10:12 +0100628 if (buf.ack.reason == RXRPC_ACK_PING) {
629 _proto("Rx ACK %%%u PING Request", sp->hdr.serial);
630 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
David Howells9c7ad432016-09-23 13:50:40 +0100631 skew, sp->hdr.serial, true, true,
632 rxrpc_propose_ack_respond_to_ping);
David Howells248f2192016-09-08 11:10:12 +0100633 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
634 rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
David Howells9c7ad432016-09-23 13:50:40 +0100635 skew, sp->hdr.serial, true, true,
636 rxrpc_propose_ack_respond_to_ack);
David Howells248f2192016-09-08 11:10:12 +0100637 }
638
639 offset = sp->offset + nr_acks + 3;
David Howells89a80ed2016-09-13 22:36:22 +0100640 if (skb->len >= offset + sizeof(buf.info)) {
David Howells248f2192016-09-08 11:10:12 +0100641 if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0)
642 return rxrpc_proto_abort("XAI", call, 0);
643 rxrpc_input_ackinfo(call, skb, &buf.info);
644 }
645
646 if (first_soft_ack == 0)
647 return rxrpc_proto_abort("AK0", call, 0);
648
649 /* Ignore ACKs unless we are or have just been transmitting. */
650 switch (call->state) {
651 case RXRPC_CALL_CLIENT_SEND_REQUEST:
652 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
653 case RXRPC_CALL_SERVER_SEND_REPLY:
654 case RXRPC_CALL_SERVER_AWAIT_ACK:
655 break;
656 default:
657 return;
658 }
659
660 /* Discard any out-of-order or duplicate ACKs. */
David Howells98dafac2016-09-23 14:04:38 +0100661 if (before_eq(sp->hdr.serial, call->acks_latest)) {
David Howells248f2192016-09-08 11:10:12 +0100662 _debug("discard ACK %d <= %d",
663 sp->hdr.serial, call->acks_latest);
664 return;
665 }
666 call->acks_latest = sp->hdr.serial;
667
David Howells248f2192016-09-08 11:10:12 +0100668 if (before(hard_ack, call->tx_hard_ack) ||
669 after(hard_ack, call->tx_top))
670 return rxrpc_proto_abort("AKW", call, 0);
David Howells70790db2016-09-23 12:39:22 +0100671 if (nr_acks > call->tx_top - hard_ack)
672 return rxrpc_proto_abort("AKN", call, 0);
David Howells248f2192016-09-08 11:10:12 +0100673
674 if (after(hard_ack, call->tx_hard_ack))
David Howells31a1b982016-09-24 18:05:26 +0100675 rxrpc_rotate_tx_window(call, hard_ack, &summary);
David Howells248f2192016-09-08 11:10:12 +0100676
David Howells70790db2016-09-23 12:39:22 +0100677 if (nr_acks > 0) {
678 if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0)
679 return rxrpc_proto_abort("XSA", call, 0);
David Howells31a1b982016-09-24 18:05:26 +0100680 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
681 &summary);
David Howells70790db2016-09-23 12:39:22 +0100682 }
David Howells248f2192016-09-08 11:10:12 +0100683
David Howells70790db2016-09-23 12:39:22 +0100684 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
685 rxrpc_end_tx_phase(call, false, "ETA");
686 return;
687 }
688
David Howells0d967962016-09-24 18:05:27 +0100689 if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
690 RXRPC_TX_ANNO_LAST &&
691 summary.nr_acks == call->tx_top - hard_ack)
692 rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
693 false, true,
694 rxrpc_propose_ack_ping_for_lost_reply);
David Howells248f2192016-09-08 11:10:12 +0100695}
696
697/*
698 * Process an ACKALL packet.
699 */
700static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
701{
David Howells31a1b982016-09-24 18:05:26 +0100702 struct rxrpc_ack_summary summary = { 0 };
David Howells248f2192016-09-08 11:10:12 +0100703 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
704
705 _proto("Rx ACKALL %%%u", sp->hdr.serial);
706
David Howells31a1b982016-09-24 18:05:26 +0100707 rxrpc_rotate_tx_window(call, call->tx_top, &summary);
David Howells70790db2016-09-23 12:39:22 +0100708 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
709 rxrpc_end_tx_phase(call, false, "ETL");
David Howells248f2192016-09-08 11:10:12 +0100710}
711
712/*
713 * Process an ABORT packet.
714 */
715static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700716{
717 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000718 __be32 wtmp;
David Howells248f2192016-09-08 11:10:12 +0100719 u32 abort_code = RX_CALL_DEAD;
720
721 _enter("");
722
723 if (skb->len >= 4 &&
724 skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0)
725 abort_code = ntohl(wtmp);
726
727 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
728
729 if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
730 abort_code, ECONNABORTED))
731 rxrpc_notify_socket(call);
732}
733
734/*
735 * Process an incoming call packet.
736 */
737static void rxrpc_input_call_packet(struct rxrpc_call *call,
738 struct sk_buff *skb, u16 skew)
739{
740 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700741
742 _enter("%p,%p", call, skb);
743
David Howells17926a72007-04-26 15:48:28 -0700744 switch (sp->hdr.type) {
David Howells248f2192016-09-08 11:10:12 +0100745 case RXRPC_PACKET_TYPE_DATA:
746 rxrpc_input_data(call, skb, skew);
747 break;
David Howells17926a72007-04-26 15:48:28 -0700748
David Howells248f2192016-09-08 11:10:12 +0100749 case RXRPC_PACKET_TYPE_ACK:
750 rxrpc_input_ack(call, skb, skew);
751 break;
David Howells17926a72007-04-26 15:48:28 -0700752
753 case RXRPC_PACKET_TYPE_BUSY:
David Howells0d12f8a2016-03-04 15:53:46 +0000754 _proto("Rx BUSY %%%u", sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700755
David Howells248f2192016-09-08 11:10:12 +0100756 /* Just ignore BUSY packets from the server; the retry and
757 * lifespan timers will take care of business. BUSY packets
758 * from the client don't make sense.
759 */
760 break;
David Howells17926a72007-04-26 15:48:28 -0700761
David Howells248f2192016-09-08 11:10:12 +0100762 case RXRPC_PACKET_TYPE_ABORT:
763 rxrpc_input_abort(call, skb);
764 break;
765
766 case RXRPC_PACKET_TYPE_ACKALL:
767 rxrpc_input_ackall(call, skb);
768 break;
David Howells17926a72007-04-26 15:48:28 -0700769
770 default:
David Howells0d12f8a2016-03-04 15:53:46 +0000771 _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700772 break;
773 }
774
David Howells17926a72007-04-26 15:48:28 -0700775 _leave("");
776}
777
778/*
779 * post connection-level events to the connection
David Howells18bfeba2016-08-23 15:27:25 +0100780 * - this includes challenges, responses, some aborts and call terminal packet
781 * retransmission.
David Howells17926a72007-04-26 15:48:28 -0700782 */
David Howells2e7e9752016-08-09 10:11:48 +0100783static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
David Howells17926a72007-04-26 15:48:28 -0700784 struct sk_buff *skb)
785{
786 _enter("%p,%p", conn, skb);
787
David Howells17926a72007-04-26 15:48:28 -0700788 skb_queue_tail(&conn->rx_queue, skb);
David Howells2e7e9752016-08-09 10:11:48 +0100789 rxrpc_queue_conn(conn);
David Howells17926a72007-04-26 15:48:28 -0700790}
791
David Howells44ba0692015-04-01 16:31:26 +0100792/*
793 * post endpoint-level events to the local endpoint
794 * - this includes debug and version messages
795 */
796static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
797 struct sk_buff *skb)
798{
799 _enter("%p,%p", local, skb);
800
David Howells44ba0692015-04-01 16:31:26 +0100801 skb_queue_tail(&local->event_queue, skb);
David Howells5acbee42016-06-27 10:32:02 +0100802 rxrpc_queue_local(local);
David Howells44ba0692015-04-01 16:31:26 +0100803}
804
David Howells0d12f8a2016-03-04 15:53:46 +0000805/*
David Howells248f2192016-09-08 11:10:12 +0100806 * put a packet up for transport-level abort
807 */
808static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
809{
810 CHECK_SLAB_OKAY(&local->usage);
811
812 skb_queue_tail(&local->reject_queue, skb);
813 rxrpc_queue_local(local);
814}
815
816/*
David Howells0d12f8a2016-03-04 15:53:46 +0000817 * Extract the wire header from a packet and translate the byte order.
818 */
819static noinline
820int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
821{
822 struct rxrpc_wire_header whdr;
823
824 /* dig out the RxRPC connection details */
Willem de Bruijn4d0fc732016-04-07 11:44:59 -0400825 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0)
David Howells0d12f8a2016-03-04 15:53:46 +0000826 return -EBADMSG;
David Howells0d12f8a2016-03-04 15:53:46 +0000827
828 memset(sp, 0, sizeof(*sp));
829 sp->hdr.epoch = ntohl(whdr.epoch);
830 sp->hdr.cid = ntohl(whdr.cid);
831 sp->hdr.callNumber = ntohl(whdr.callNumber);
832 sp->hdr.seq = ntohl(whdr.seq);
833 sp->hdr.serial = ntohl(whdr.serial);
834 sp->hdr.flags = whdr.flags;
835 sp->hdr.type = whdr.type;
836 sp->hdr.userStatus = whdr.userStatus;
837 sp->hdr.securityIndex = whdr.securityIndex;
838 sp->hdr._rsvd = ntohs(whdr._rsvd);
839 sp->hdr.serviceId = ntohs(whdr.serviceId);
David Howells248f2192016-09-08 11:10:12 +0100840 sp->offset = sizeof(whdr);
David Howells0d12f8a2016-03-04 15:53:46 +0000841 return 0;
842}
843
David Howells17926a72007-04-26 15:48:28 -0700844/*
845 * handle data received on the local endpoint
846 * - may be called in interrupt context
David Howells4f95dd72016-04-04 14:00:35 +0100847 *
848 * The socket is locked by the caller and this prevents the socket from being
849 * shut down and the local endpoint from going away, thus sk_user_data will not
850 * be cleared until this function returns.
David Howells17926a72007-04-26 15:48:28 -0700851 */
David Howells248f2192016-09-08 11:10:12 +0100852void rxrpc_data_ready(struct sock *udp_sk)
David Howells17926a72007-04-26 15:48:28 -0700853{
David Howells8496af52016-07-01 07:51:50 +0100854 struct rxrpc_connection *conn;
David Howells248f2192016-09-08 11:10:12 +0100855 struct rxrpc_channel *chan;
856 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700857 struct rxrpc_skb_priv *sp;
David Howells248f2192016-09-08 11:10:12 +0100858 struct rxrpc_local *local = udp_sk->sk_user_data;
David Howells17926a72007-04-26 15:48:28 -0700859 struct sk_buff *skb;
David Howells248f2192016-09-08 11:10:12 +0100860 unsigned int channel;
David Howells563ea7d2016-08-23 15:27:25 +0100861 int ret, skew;
David Howells17926a72007-04-26 15:48:28 -0700862
David Howells248f2192016-09-08 11:10:12 +0100863 _enter("%p", udp_sk);
David Howells17926a72007-04-26 15:48:28 -0700864
865 ASSERT(!irqs_disabled());
866
David Howells248f2192016-09-08 11:10:12 +0100867 skb = skb_recv_datagram(udp_sk, 0, 1, &ret);
David Howells17926a72007-04-26 15:48:28 -0700868 if (!skb) {
David Howells17926a72007-04-26 15:48:28 -0700869 if (ret == -EAGAIN)
870 return;
871 _debug("UDP socket error %d", ret);
872 return;
873 }
874
David Howells71f3ca42016-09-17 10:49:14 +0100875 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
David Howells17926a72007-04-26 15:48:28 -0700876
877 _net("recv skb %p", skb);
878
879 /* we'll probably need to checksum it (didn't call sock_recvmsg) */
880 if (skb_checksum_complete(skb)) {
David Howells71f3ca42016-09-17 10:49:14 +0100881 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
Eric Dumazet02c22342016-04-27 16:44:30 -0700882 __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
David Howells17926a72007-04-26 15:48:28 -0700883 _leave(" [CSUM failed]");
884 return;
885 }
886
Eric Dumazet02c22342016-04-27 16:44:30 -0700887 __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
Herbert Xu1781f7f2007-12-11 11:30:32 -0800888
David Howells0d12f8a2016-03-04 15:53:46 +0000889 /* The socket buffer we have is owned by UDP, with UDP's data all over
890 * it, but we really want our own data there.
891 */
David Howells17926a72007-04-26 15:48:28 -0700892 skb_orphan(skb);
893 sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700894
David Howells89b475a2016-09-23 12:39:22 +0100895 /* dig out the RxRPC connection details */
896 if (rxrpc_extract_header(sp, skb) < 0)
897 goto bad_message;
898
David Howells8a681c362016-09-17 10:49:15 +0100899 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
900 static int lose;
901 if ((lose++ & 7) == 7) {
David Howells89b475a2016-09-23 12:39:22 +0100902 trace_rxrpc_rx_lose(sp);
David Howells8a681c362016-09-17 10:49:15 +0100903 rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
904 return;
905 }
906 }
907
David Howells49e19ec2016-09-08 11:10:12 +0100908 trace_rxrpc_rx_packet(sp);
David Howells17926a72007-04-26 15:48:28 -0700909
910 _net("Rx RxRPC %s ep=%x call=%x:%x",
911 sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
David Howells0d12f8a2016-03-04 15:53:46 +0000912 sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
David Howells17926a72007-04-26 15:48:28 -0700913
David Howells351c1e62016-03-04 15:56:06 +0000914 if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
915 !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
David Howells17926a72007-04-26 15:48:28 -0700916 _proto("Rx Bad Packet Type %u", sp->hdr.type);
917 goto bad_message;
918 }
919
David Howells248f2192016-09-08 11:10:12 +0100920 switch (sp->hdr.type) {
921 case RXRPC_PACKET_TYPE_VERSION:
David Howells44ba0692015-04-01 16:31:26 +0100922 rxrpc_post_packet_to_local(local, skb);
923 goto out;
David Howellsbc6e1ea2016-06-10 22:30:27 +0100924
David Howells248f2192016-09-08 11:10:12 +0100925 case RXRPC_PACKET_TYPE_BUSY:
926 if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
927 goto discard;
928
929 case RXRPC_PACKET_TYPE_DATA:
930 if (sp->hdr.callNumber == 0)
931 goto bad_message;
932 if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
933 !rxrpc_validate_jumbo(skb))
934 goto bad_message;
935 break;
936 }
David Howells17926a72007-04-26 15:48:28 -0700937
David Howells8496af52016-07-01 07:51:50 +0100938 rcu_read_lock();
939
David Howells8496af52016-07-01 07:51:50 +0100940 conn = rxrpc_find_connection_rcu(local, skb);
David Howells248f2192016-09-08 11:10:12 +0100941 if (conn) {
942 if (sp->hdr.securityIndex != conn->security_ix)
943 goto wrong_security;
David Howells563ea7d2016-08-23 15:27:25 +0100944
David Howells248f2192016-09-08 11:10:12 +0100945 if (sp->hdr.callNumber == 0) {
946 /* Connection-level packet */
947 _debug("CONN %p {%d}", conn, conn->debug_id);
948 rxrpc_post_packet_to_conn(conn, skb);
949 goto out_unlock;
950 }
David Howells8496af52016-07-01 07:51:50 +0100951
David Howells248f2192016-09-08 11:10:12 +0100952 /* Note the serial number skew here */
953 skew = (int)sp->hdr.serial - (int)conn->hi_serial;
954 if (skew >= 0) {
955 if (skew > 0)
956 conn->hi_serial = sp->hdr.serial;
957 } else {
958 skew = -skew;
959 skew = min(skew, 65535);
960 }
961
David Howells8496af52016-07-01 07:51:50 +0100962 /* Call-bound packets are routed by connection channel. */
David Howells248f2192016-09-08 11:10:12 +0100963 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
964 chan = &conn->channels[channel];
Tim Smith77276402014-03-03 23:04:45 +0000965
David Howells18bfeba2016-08-23 15:27:25 +0100966 /* Ignore really old calls */
967 if (sp->hdr.callNumber < chan->last_call)
968 goto discard_unlock;
969
970 if (sp->hdr.callNumber == chan->last_call) {
David Howells248f2192016-09-08 11:10:12 +0100971 /* For the previous service call, if completed successfully, we
972 * discard all further packets.
David Howells18bfeba2016-08-23 15:27:25 +0100973 */
David Howells2266ffd2016-08-24 13:06:14 +0100974 if (rxrpc_conn_is_service(conn) &&
David Howells18bfeba2016-08-23 15:27:25 +0100975 (chan->last_type == RXRPC_PACKET_TYPE_ACK ||
976 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT))
977 goto discard_unlock;
978
David Howells248f2192016-09-08 11:10:12 +0100979 /* But otherwise we need to retransmit the final packet from
980 * data cached in the connection record.
David Howells18bfeba2016-08-23 15:27:25 +0100981 */
982 rxrpc_post_packet_to_conn(conn, skb);
983 goto out_unlock;
984 }
985
986 call = rcu_dereference(chan->call);
David Howells248f2192016-09-08 11:10:12 +0100987 } else {
988 skew = 0;
989 call = NULL;
Tim Smith77276402014-03-03 23:04:45 +0000990 }
David Howells44ba0692015-04-01 16:31:26 +0100991
David Howells248f2192016-09-08 11:10:12 +0100992 if (!call || atomic_read(&call->usage) == 0) {
993 if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
994 sp->hdr.callNumber == 0 ||
995 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
996 goto bad_message_unlock;
997 if (sp->hdr.seq != 1)
998 goto discard_unlock;
999 call = rxrpc_new_incoming_call(local, conn, skb);
1000 if (!call) {
1001 rcu_read_unlock();
1002 goto reject_packet;
1003 }
David Howells8e831342016-09-22 00:29:31 +01001004 rxrpc_send_ping(call, skb, skew);
David Howells248f2192016-09-08 11:10:12 +01001005 }
1006
1007 rxrpc_input_call_packet(call, skb, skew);
1008 goto discard_unlock;
1009
David Howells18bfeba2016-08-23 15:27:25 +01001010discard_unlock:
David Howells8496af52016-07-01 07:51:50 +01001011 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +01001012discard:
David Howells71f3ca42016-09-17 10:49:14 +01001013 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells44ba0692015-04-01 16:31:26 +01001014out:
David Howells49e19ec2016-09-08 11:10:12 +01001015 trace_rxrpc_rx_done(0, 0);
David Howells17926a72007-04-26 15:48:28 -07001016 return;
1017
David Howells248f2192016-09-08 11:10:12 +01001018out_unlock:
David Howells8496af52016-07-01 07:51:50 +01001019 rcu_read_unlock();
David Howells248f2192016-09-08 11:10:12 +01001020 goto out;
David Howells8496af52016-07-01 07:51:50 +01001021
David Howells248f2192016-09-08 11:10:12 +01001022wrong_security:
1023 rcu_read_unlock();
1024 trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1025 RXKADINCONSISTENCY, EBADMSG);
1026 skb->priority = RXKADINCONSISTENCY;
1027 goto post_abort;
David Howells17926a72007-04-26 15:48:28 -07001028
David Howells248f2192016-09-08 11:10:12 +01001029bad_message_unlock:
1030 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -07001031bad_message:
David Howells248f2192016-09-08 11:10:12 +01001032 trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1033 RX_PROTOCOL_ERROR, EBADMSG);
David Howells17926a72007-04-26 15:48:28 -07001034 skb->priority = RX_PROTOCOL_ERROR;
David Howells248f2192016-09-08 11:10:12 +01001035post_abort:
1036 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
David Howells49e19ec2016-09-08 11:10:12 +01001037reject_packet:
1038 trace_rxrpc_rx_done(skb->mark, skb->priority);
David Howells17926a72007-04-26 15:48:28 -07001039 rxrpc_reject_packet(local, skb);
David Howells17926a72007-04-26 15:48:28 -07001040 _leave(" [badmsg]");
1041}