Blame view

net/rxrpc/input.c 32.9 KB
17926a793   David Howells   [AF_RXRPC]: Provi...
1
2
  /* RxRPC packet reception
   *
248f219cb   David Howells   rxrpc: Rewrite th...
3
   * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
17926a793   David Howells   [AF_RXRPC]: Provi...
4
5
6
7
8
9
10
   * Written by David Howells (dhowells@redhat.com)
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version
   * 2 of the License, or (at your option) any later version.
   */
9b6d53985   Joe Perches   rxrpc: Use pr_<le...
11
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17926a793   David Howells   [AF_RXRPC]: Provi...
12
13
14
15
16
17
18
19
  #include <linux/module.h>
  #include <linux/net.h>
  #include <linux/skbuff.h>
  #include <linux/errqueue.h>
  #include <linux/udp.h>
  #include <linux/in.h>
  #include <linux/in6.h>
  #include <linux/icmp.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
20
  #include <linux/gfp.h>
17926a793   David Howells   [AF_RXRPC]: Provi...
21
22
23
  #include <net/sock.h>
  #include <net/af_rxrpc.h>
  #include <net/ip.h>
1781f7f58   Herbert Xu   [UDP]: Restore mi...
24
  #include <net/udp.h>
0283328e2   Pavel Emelyanov   MIB: add struct n...
25
  #include <net/net_namespace.h>
17926a793   David Howells   [AF_RXRPC]: Provi...
26
  #include "ar-internal.h"
248f219cb   David Howells   rxrpc: Rewrite th...
27
28
29
  static void rxrpc_proto_abort(const char *why,
  			      struct rxrpc_call *call, rxrpc_seq_t seq)
  {
3a92789af   David Howells   rxrpc: Use negati...
30
  	if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG)) {
248f219cb   David Howells   rxrpc: Rewrite th...
31
32
33
34
  		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  		rxrpc_queue_call(call);
  	}
  }
17926a793   David Howells   [AF_RXRPC]: Provi...
35
  /*
57494343c   David Howells   rxrpc: Implement ...
36
37
38
39
   * Do TCP-style congestion management [RFC 5681].
   */
  static void rxrpc_congestion_management(struct rxrpc_call *call,
  					struct sk_buff *skb,
ed1e8679d   David Howells   rxrpc: Note seria...
40
41
  					struct rxrpc_ack_summary *summary,
  					rxrpc_serial_t acked_serial)
57494343c   David Howells   rxrpc: Implement ...
42
43
  {
  	enum rxrpc_congest_change change = rxrpc_cong_no_change;
57494343c   David Howells   rxrpc: Implement ...
44
45
46
47
48
49
50
51
52
53
54
55
  	unsigned int cumulative_acks = call->cong_cumul_acks;
  	unsigned int cwnd = call->cong_cwnd;
  	bool resend = false;
  
  	summary->flight_size =
  		(call->tx_top - call->tx_hard_ack) - summary->nr_acks;
  
  	if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
  		summary->retrans_timeo = true;
  		call->cong_ssthresh = max_t(unsigned int,
  					    summary->flight_size / 2, 2);
  		cwnd = 1;
8782def20   David Howells   rxrpc: Switch to ...
56
  		if (cwnd >= call->cong_ssthresh &&
57494343c   David Howells   rxrpc: Implement ...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  		    call->cong_mode == RXRPC_CALL_SLOW_START) {
  			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  			call->cong_tstamp = skb->tstamp;
  			cumulative_acks = 0;
  		}
  	}
  
  	cumulative_acks += summary->nr_new_acks;
  	cumulative_acks += summary->nr_rot_new_acks;
  	if (cumulative_acks > 255)
  		cumulative_acks = 255;
  
  	summary->mode = call->cong_mode;
  	summary->cwnd = call->cong_cwnd;
  	summary->ssthresh = call->cong_ssthresh;
  	summary->cumulative_acks = cumulative_acks;
  	summary->dup_acks = call->cong_dup_acks;
  
  	switch (call->cong_mode) {
  	case RXRPC_CALL_SLOW_START:
  		if (summary->nr_nacks > 0)
  			goto packet_loss_detected;
  		if (summary->cumulative_acks > 0)
  			cwnd += 1;
8782def20   David Howells   rxrpc: Switch to ...
81
  		if (cwnd >= call->cong_ssthresh) {
57494343c   David Howells   rxrpc: Implement ...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  			call->cong_tstamp = skb->tstamp;
  		}
  		goto out;
  
  	case RXRPC_CALL_CONGEST_AVOIDANCE:
  		if (summary->nr_nacks > 0)
  			goto packet_loss_detected;
  
  		/* We analyse the number of packets that get ACK'd per RTT
  		 * period and increase the window if we managed to fill it.
  		 */
  		if (call->peer->rtt_usage == 0)
  			goto out;
  		if (ktime_before(skb->tstamp,
  				 ktime_add_ns(call->cong_tstamp,
  					      call->peer->rtt)))
  			goto out_no_clear_ca;
  		change = rxrpc_cong_rtt_window_end;
  		call->cong_tstamp = skb->tstamp;
  		if (cumulative_acks >= cwnd)
  			cwnd++;
  		goto out;
  
  	case RXRPC_CALL_PACKET_LOSS:
  		if (summary->nr_nacks == 0)
  			goto resume_normality;
  
  		if (summary->new_low_nack) {
  			change = rxrpc_cong_new_low_nack;
  			call->cong_dup_acks = 1;
  			if (call->cong_extra > 1)
  				call->cong_extra = 1;
  			goto send_extra_data;
  		}
  
  		call->cong_dup_acks++;
  		if (call->cong_dup_acks < 3)
  			goto send_extra_data;
  
  		change = rxrpc_cong_begin_retransmission;
  		call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
  		call->cong_ssthresh = max_t(unsigned int,
  					    summary->flight_size / 2, 2);
  		cwnd = call->cong_ssthresh + 3;
  		call->cong_extra = 0;
  		call->cong_dup_acks = 0;
  		resend = true;
  		goto out;
  
  	case RXRPC_CALL_FAST_RETRANSMIT:
  		if (!summary->new_low_nack) {
  			if (summary->nr_new_acks == 0)
  				cwnd += 1;
  			call->cong_dup_acks++;
  			if (call->cong_dup_acks == 2) {
  				change = rxrpc_cong_retransmit_again;
  				call->cong_dup_acks = 0;
  				resend = true;
  			}
  		} else {
  			change = rxrpc_cong_progress;
  			cwnd = call->cong_ssthresh;
  			if (summary->nr_nacks == 0)
  				goto resume_normality;
  		}
  		goto out;
  
  	default:
  		BUG();
  		goto out;
  	}
  
  resume_normality:
  	change = rxrpc_cong_cleared_nacks;
  	call->cong_dup_acks = 0;
  	call->cong_extra = 0;
  	call->cong_tstamp = skb->tstamp;
8782def20   David Howells   rxrpc: Switch to ...
160
  	if (cwnd < call->cong_ssthresh)
57494343c   David Howells   rxrpc: Implement ...
161
162
163
164
165
166
167
168
169
170
  		call->cong_mode = RXRPC_CALL_SLOW_START;
  	else
  		call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  out:
  	cumulative_acks = 0;
  out_no_clear_ca:
  	if (cwnd >= RXRPC_RXTX_BUFF_SIZE - 1)
  		cwnd = RXRPC_RXTX_BUFF_SIZE - 1;
  	call->cong_cwnd = cwnd;
  	call->cong_cumul_acks = cumulative_acks;
ed1e8679d   David Howells   rxrpc: Note seria...
171
  	trace_rxrpc_congest(call, summary, acked_serial, change);
57494343c   David Howells   rxrpc: Implement ...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  	if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  		rxrpc_queue_call(call);
  	return;
  
  packet_loss_detected:
  	change = rxrpc_cong_saw_nack;
  	call->cong_mode = RXRPC_CALL_PACKET_LOSS;
  	call->cong_dup_acks = 0;
  	goto send_extra_data;
  
  send_extra_data:
  	/* Send some previously unsent DATA if we have some to advance the ACK
  	 * state.
  	 */
  	if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  	    RXRPC_TX_ANNO_LAST ||
  	    summary->nr_acks != call->tx_top - call->tx_hard_ack) {
  		call->cong_extra++;
  		wake_up(&call->waitq);
  	}
  	goto out_no_clear_ca;
  }
  
  /*
8e83134db   David Howells   rxrpc: Send pings...
196
197
198
199
200
201
202
   * Ping the other end to fill our RTT cache and to retrieve the rwind
   * and MTU parameters.
   */
  static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb,
  			    int skew)
  {
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
fc943f677   David Howells   rxrpc: Reduce the...
203
  	ktime_t now = skb->tstamp;
8e83134db   David Howells   rxrpc: Send pings...
204

fc943f677   David Howells   rxrpc: Reduce the...
205
206
207
  	if (call->peer->rtt_usage < 3 ||
  	    ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
  		rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
9c7ad4344   David Howells   rxrpc: Add tracep...
208
209
  				  true, true,
  				  rxrpc_propose_ack_ping_for_params);
8e83134db   David Howells   rxrpc: Send pings...
210
211
212
  }
  
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
213
   * Apply a hard ACK by advancing the Tx window.
17926a793   David Howells   [AF_RXRPC]: Provi...
214
   */
31a1b9895   David Howells   rxrpc: Generate a...
215
216
  static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
  				   struct rxrpc_ack_summary *summary)
17926a793   David Howells   [AF_RXRPC]: Provi...
217
  {
248f219cb   David Howells   rxrpc: Rewrite th...
218
219
  	struct sk_buff *skb, *list = NULL;
  	int ix;
70790dbe3   David Howells   rxrpc: Pass the l...
220
  	u8 annotation;
17926a793   David Howells   [AF_RXRPC]: Provi...
221

31a1b9895   David Howells   rxrpc: Generate a...
222
223
224
225
226
227
  	if (call->acks_lowest_nak == call->tx_hard_ack) {
  		call->acks_lowest_nak = to;
  	} else if (before_eq(call->acks_lowest_nak, to)) {
  		summary->new_low_nack = true;
  		call->acks_lowest_nak = to;
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
228
  	spin_lock(&call->lock);
17926a793   David Howells   [AF_RXRPC]: Provi...
229

248f219cb   David Howells   rxrpc: Rewrite th...
230
231
232
233
  	while (before(call->tx_hard_ack, to)) {
  		call->tx_hard_ack++;
  		ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK;
  		skb = call->rxtx_buffer[ix];
70790dbe3   David Howells   rxrpc: Pass the l...
234
  		annotation = call->rxtx_annotations[ix];
71f3ca408   David Howells   rxrpc: Improve sk...
235
  		rxrpc_see_skb(skb, rxrpc_skb_tx_rotated);
248f219cb   David Howells   rxrpc: Rewrite th...
236
237
238
239
  		call->rxtx_buffer[ix] = NULL;
  		call->rxtx_annotations[ix] = 0;
  		skb->next = list;
  		list = skb;
70790dbe3   David Howells   rxrpc: Pass the l...
240
241
242
  
  		if (annotation & RXRPC_TX_ANNO_LAST)
  			set_bit(RXRPC_CALL_TX_LAST, &call->flags);
31a1b9895   David Howells   rxrpc: Generate a...
243
244
  		if ((annotation & RXRPC_TX_ANNO_MASK) != RXRPC_TX_ANNO_ACK)
  			summary->nr_rot_new_acks++;
248f219cb   David Howells   rxrpc: Rewrite th...
245
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
246

248f219cb   David Howells   rxrpc: Rewrite th...
247
  	spin_unlock(&call->lock);
17926a793   David Howells   [AF_RXRPC]: Provi...
248

70790dbe3   David Howells   rxrpc: Pass the l...
249
250
251
  	trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ?
  				    rxrpc_transmit_rotate_last :
  				    rxrpc_transmit_rotate));
bc4abfcf5   David Howells   rxrpc: Add missin...
252
  	wake_up(&call->waitq);
248f219cb   David Howells   rxrpc: Rewrite th...
253
254
255
256
  	while (list) {
  		skb = list;
  		list = skb->next;
  		skb->next = NULL;
71f3ca408   David Howells   rxrpc: Improve sk...
257
  		rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
17926a793   David Howells   [AF_RXRPC]: Provi...
258
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
259
  }
17926a793   David Howells   [AF_RXRPC]: Provi...
260

248f219cb   David Howells   rxrpc: Rewrite th...
261
262
263
264
265
266
  /*
   * End the transmission phase of a call.
   *
   * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
   * or a final ACK packet.
   */
70790dbe3   David Howells   rxrpc: Pass the l...
267
268
  static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
  			       const char *abort_why)
248f219cb   David Howells   rxrpc: Rewrite th...
269
  {
17926a793   David Howells   [AF_RXRPC]: Provi...
270

70790dbe3   David Howells   rxrpc: Pass the l...
271
  	ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
17926a793   David Howells   [AF_RXRPC]: Provi...
272

248f219cb   David Howells   rxrpc: Rewrite th...
273
  	write_lock(&call->state_lock);
651350d10   David Howells   [AF_RXRPC]: Add a...
274

248f219cb   David Howells   rxrpc: Rewrite th...
275
  	switch (call->state) {
70790dbe3   David Howells   rxrpc: Pass the l...
276
  	case RXRPC_CALL_CLIENT_SEND_REQUEST:
248f219cb   David Howells   rxrpc: Rewrite th...
277
  	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
70790dbe3   David Howells   rxrpc: Pass the l...
278
279
280
281
  		if (reply_begun)
  			call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
  		else
  			call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
248f219cb   David Howells   rxrpc: Rewrite th...
282
  		break;
70790dbe3   David Howells   rxrpc: Pass the l...
283

248f219cb   David Howells   rxrpc: Rewrite th...
284
285
286
287
  	case RXRPC_CALL_SERVER_AWAIT_ACK:
  		__rxrpc_call_completed(call);
  		rxrpc_notify_socket(call);
  		break;
70790dbe3   David Howells   rxrpc: Pass the l...
288
289
290
  
  	default:
  		goto bad_state;
17926a793   David Howells   [AF_RXRPC]: Provi...
291
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
292

248f219cb   David Howells   rxrpc: Rewrite th...
293
  	write_unlock(&call->state_lock);
70790dbe3   David Howells   rxrpc: Pass the l...
294
  	if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) {
0d967960d   David Howells   rxrpc: Schedule a...
295
296
  		rxrpc_propose_ACK(call, RXRPC_ACK_IDLE, 0, 0, false, true,
  				  rxrpc_propose_ack_client_tx_end);
70790dbe3   David Howells   rxrpc: Pass the l...
297
298
299
300
  		trace_rxrpc_transmit(call, rxrpc_transmit_await_reply);
  	} else {
  		trace_rxrpc_transmit(call, rxrpc_transmit_end);
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
301
302
  	_leave(" = ok");
  	return true;
70790dbe3   David Howells   rxrpc: Pass the l...
303
304
305
306
307
308
309
310
311
312
313
314
315
  
  bad_state:
  	write_unlock(&call->state_lock);
  	kdebug("end_tx %s", rxrpc_call_states[call->state]);
  	rxrpc_proto_abort(abort_why, call, call->tx_top);
  	return false;
  }
  
  /*
   * Begin the reply reception phase of a call.
   */
  static bool rxrpc_receiving_reply(struct rxrpc_call *call)
  {
31a1b9895   David Howells   rxrpc: Generate a...
316
  	struct rxrpc_ack_summary summary = { 0 };
70790dbe3   David Howells   rxrpc: Pass the l...
317
  	rxrpc_seq_t top = READ_ONCE(call->tx_top);
dd7c1ee59   David Howells   rxrpc: Reinitiali...
318
319
320
321
322
323
  	if (call->ackr_reason) {
  		spin_lock_bh(&call->lock);
  		call->ackr_reason = 0;
  		call->resend_at = call->expire_at;
  		call->ack_at = call->expire_at;
  		spin_unlock_bh(&call->lock);
df0adc788   David Howells   rxrpc: Keep the c...
324
325
  		rxrpc_set_timer(call, rxrpc_timer_init_for_reply,
  				ktime_get_real());
dd7c1ee59   David Howells   rxrpc: Reinitiali...
326
  	}
70790dbe3   David Howells   rxrpc: Pass the l...
327
  	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags))
31a1b9895   David Howells   rxrpc: Generate a...
328
  		rxrpc_rotate_tx_window(call, top, &summary);
70790dbe3   David Howells   rxrpc: Pass the l...
329
330
331
332
333
334
335
336
  	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  		rxrpc_proto_abort("TXL", call, top);
  		return false;
  	}
  	if (!rxrpc_end_tx_phase(call, true, "ETD"))
  		return false;
  	call->tx_phase = false;
  	return true;
248f219cb   David Howells   rxrpc: Rewrite th...
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  }
  
  /*
   * Scan a jumbo packet to validate its structure and to work out how many
   * subpackets it contains.
   *
   * A jumbo packet is a collection of consecutive packets glued together with
   * little headers between that indicate how to change the initial header for
   * each subpacket.
   *
   * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but
   * the last are RXRPC_JUMBO_DATALEN in size.  The last subpacket may be of any
   * size.
   */
  static bool rxrpc_validate_jumbo(struct sk_buff *skb)
  {
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
775e5b71d   David Howells   rxrpc: The offset...
354
  	unsigned int offset = sizeof(struct rxrpc_wire_header);
89a80ed4c   David Howells   rxrpc: Use skb->l...
355
  	unsigned int len = skb->len;
248f219cb   David Howells   rxrpc: Rewrite th...
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  	int nr_jumbo = 1;
  	u8 flags = sp->hdr.flags;
  
  	do {
  		nr_jumbo++;
  		if (len - offset < RXRPC_JUMBO_SUBPKTLEN)
  			goto protocol_error;
  		if (flags & RXRPC_LAST_PACKET)
  			goto protocol_error;
  		offset += RXRPC_JUMBO_DATALEN;
  		if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  			goto protocol_error;
  		offset += sizeof(struct rxrpc_jumbo_header);
  	} while (flags & RXRPC_JUMBO_PACKET);
  
  	sp->nr_jumbo = nr_jumbo;
  	return true;
17926a793   David Howells   [AF_RXRPC]: Provi...
373

248f219cb   David Howells   rxrpc: Rewrite th...
374
375
  protocol_error:
  	return false;
17926a793   David Howells   [AF_RXRPC]: Provi...
376
377
378
  }
  
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
379
380
381
382
383
384
385
386
387
388
389
   * Handle reception of a duplicate packet.
   *
   * We have to take care to avoid an attack here whereby we're given a series of
   * jumbograms, each with a sequence number one before the preceding one and
   * filled up to maximum UDP size.  If they never send us the first packet in
   * the sequence, they can cause us to have to hold on to around 2MiB of kernel
   * space until the call times out.
   *
   * We limit the space usage by only accepting three duplicate jumbo packets per
   * call.  After that, we tell the other side we're no longer accepting jumbos
   * (that information is encoded in the ACK packet).
17926a793   David Howells   [AF_RXRPC]: Provi...
390
   */
248f219cb   David Howells   rxrpc: Rewrite th...
391
  static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq,
75e421263   David Howells   rxrpc: Correctly ...
392
  				 u8 annotation, bool *_jumbo_bad)
17926a793   David Howells   [AF_RXRPC]: Provi...
393
  {
248f219cb   David Howells   rxrpc: Rewrite th...
394
395
396
  	/* Discard normal packets that are duplicates. */
  	if (annotation == 0)
  		return;
17926a793   David Howells   [AF_RXRPC]: Provi...
397

248f219cb   David Howells   rxrpc: Rewrite th...
398
399
400
401
  	/* Skip jumbo subpackets that are duplicates.  When we've had three or
  	 * more partially duplicate jumbo packets, we refuse to take any more
  	 * jumbos for this call.
  	 */
75e421263   David Howells   rxrpc: Correctly ...
402
403
404
  	if (!*_jumbo_bad) {
  		call->nr_jumbo_bad++;
  		*_jumbo_bad = true;
248f219cb   David Howells   rxrpc: Rewrite th...
405
406
  	}
  }
17926a793   David Howells   [AF_RXRPC]: Provi...
407

248f219cb   David Howells   rxrpc: Rewrite th...
408
409
410
411
412
413
414
  /*
   * Process a DATA packet, adding the packet to the Rx ring.
   */
  static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb,
  			     u16 skew)
  {
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
146d8fef9   David Howells   rxrpc: Call state...
415
  	enum rxrpc_call_state state;
775e5b71d   David Howells   rxrpc: The offset...
416
  	unsigned int offset = sizeof(struct rxrpc_wire_header);
248f219cb   David Howells   rxrpc: Rewrite th...
417
418
419
  	unsigned int ix;
  	rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0;
  	rxrpc_seq_t seq = sp->hdr.seq, hard_ack;
75e421263   David Howells   rxrpc: Correctly ...
420
  	bool immediate_ack = false, jumbo_bad = false, queued;
248f219cb   David Howells   rxrpc: Rewrite th...
421
422
  	u16 len;
  	u8 ack = 0, flags, annotation = 0;
17926a793   David Howells   [AF_RXRPC]: Provi...
423

248f219cb   David Howells   rxrpc: Rewrite th...
424
  	_enter("{%u,%u},{%u,%u}",
89a80ed4c   David Howells   rxrpc: Use skb->l...
425
  	       call->rx_hard_ack, call->rx_top, skb->len, seq);
17926a793   David Howells   [AF_RXRPC]: Provi...
426

248f219cb   David Howells   rxrpc: Rewrite th...
427
428
  	_proto("Rx DATA %%%u { #%u f=%02x }",
  	       sp->hdr.serial, seq, sp->hdr.flags);
17926a793   David Howells   [AF_RXRPC]: Provi...
429

146d8fef9   David Howells   rxrpc: Call state...
430
431
  	state = READ_ONCE(call->state);
  	if (state >= RXRPC_CALL_COMPLETE)
248f219cb   David Howells   rxrpc: Rewrite th...
432
  		return;
17926a793   David Howells   [AF_RXRPC]: Provi...
433

248f219cb   David Howells   rxrpc: Rewrite th...
434
435
436
  	/* Received data implicitly ACKs all of the request packets we sent
  	 * when we're acting as a client.
  	 */
146d8fef9   David Howells   rxrpc: Call state...
437
438
  	if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
  	     state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
70790dbe3   David Howells   rxrpc: Pass the l...
439
  	    !rxrpc_receiving_reply(call))
248f219cb   David Howells   rxrpc: Rewrite th...
440
  		return;
17926a793   David Howells   [AF_RXRPC]: Provi...
441

248f219cb   David Howells   rxrpc: Rewrite th...
442
  	call->ackr_prev_seq = seq;
17926a793   David Howells   [AF_RXRPC]: Provi...
443

248f219cb   David Howells   rxrpc: Rewrite th...
444
445
  	hard_ack = READ_ONCE(call->rx_hard_ack);
  	if (after(seq, hard_ack + call->rx_winsize)) {
17926a793   David Howells   [AF_RXRPC]: Provi...
446
  		ack = RXRPC_ACK_EXCEEDS_WINDOW;
248f219cb   David Howells   rxrpc: Rewrite th...
447
448
  		ack_serial = serial;
  		goto ack;
17926a793   David Howells   [AF_RXRPC]: Provi...
449
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
450
451
  	flags = sp->hdr.flags;
  	if (flags & RXRPC_JUMBO_PACKET) {
75e421263   David Howells   rxrpc: Correctly ...
452
  		if (call->nr_jumbo_bad > 3) {
248f219cb   David Howells   rxrpc: Rewrite th...
453
454
455
  			ack = RXRPC_ACK_NOSPACE;
  			ack_serial = serial;
  			goto ack;
17926a793   David Howells   [AF_RXRPC]: Provi...
456
  		}
248f219cb   David Howells   rxrpc: Rewrite th...
457
  		annotation = 1;
17926a793   David Howells   [AF_RXRPC]: Provi...
458
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
459
460
461
  next_subpacket:
  	queued = false;
  	ix = seq & RXRPC_RXTX_BUFF_MASK;
89a80ed4c   David Howells   rxrpc: Use skb->l...
462
  	len = skb->len;
248f219cb   David Howells   rxrpc: Rewrite th...
463
464
465
466
  	if (flags & RXRPC_JUMBO_PACKET)
  		len = RXRPC_JUMBO_DATALEN;
  
  	if (flags & RXRPC_LAST_PACKET) {
816c9fce1   David Howells   rxrpc: Fix handli...
467
  		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
248f219cb   David Howells   rxrpc: Rewrite th...
468
469
470
471
472
473
  		    seq != call->rx_top)
  			return rxrpc_proto_abort("LSN", call, seq);
  	} else {
  		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
  		    after_eq(seq, call->rx_top))
  			return rxrpc_proto_abort("LSA", call, seq);
17926a793   David Howells   [AF_RXRPC]: Provi...
474
  	}
b1d9f7fde   David Howells   rxrpc: Add some m...
475
  	trace_rxrpc_rx_data(call, seq, serial, flags, annotation);
248f219cb   David Howells   rxrpc: Rewrite th...
476
477
478
479
480
481
482
483
484
485
486
487
  	if (before_eq(seq, hard_ack)) {
  		ack = RXRPC_ACK_DUPLICATE;
  		ack_serial = serial;
  		goto skip;
  	}
  
  	if (flags & RXRPC_REQUEST_ACK && !ack) {
  		ack = RXRPC_ACK_REQUESTED;
  		ack_serial = serial;
  	}
  
  	if (call->rxtx_buffer[ix]) {
75e421263   David Howells   rxrpc: Correctly ...
488
  		rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad);
248f219cb   David Howells   rxrpc: Rewrite th...
489
490
491
  		if (ack != RXRPC_ACK_DUPLICATE) {
  			ack = RXRPC_ACK_DUPLICATE;
  			ack_serial = serial;
17926a793   David Howells   [AF_RXRPC]: Provi...
492
  		}
248f219cb   David Howells   rxrpc: Rewrite th...
493
494
  		immediate_ack = true;
  		goto skip;
17926a793   David Howells   [AF_RXRPC]: Provi...
495
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
496
497
498
499
500
501
502
503
  	/* Queue the packet.  We use a couple of memory barriers here as need
  	 * to make sure that rx_top is perceived to be set after the buffer
  	 * pointer and that the buffer pointer is set after the annotation and
  	 * the skb data.
  	 *
  	 * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window()
  	 * and also rxrpc_fill_out_ack().
  	 */
71f3ca408   David Howells   rxrpc: Improve sk...
504
  	rxrpc_get_skb(skb, rxrpc_skb_rx_got);
248f219cb   David Howells   rxrpc: Rewrite th...
505
506
507
  	call->rxtx_annotations[ix] = annotation;
  	smp_wmb();
  	call->rxtx_buffer[ix] = skb;
a7056c5ba   David Howells   rxrpc: Send an im...
508
  	if (after(seq, call->rx_top)) {
248f219cb   David Howells   rxrpc: Rewrite th...
509
  		smp_store_release(&call->rx_top, seq);
a7056c5ba   David Howells   rxrpc: Send an im...
510
511
512
513
514
515
516
517
  	} else if (before(seq, call->rx_top)) {
  		/* Send an immediate ACK if we fill in a hole */
  		if (!ack) {
  			ack = RXRPC_ACK_DELAY;
  			ack_serial = serial;
  		}
  		immediate_ack = true;
  	}
58dc63c99   David Howells   rxrpc: Add a trac...
518
  	if (flags & RXRPC_LAST_PACKET) {
816c9fce1   David Howells   rxrpc: Fix handli...
519
  		set_bit(RXRPC_CALL_RX_LAST, &call->flags);
58dc63c99   David Howells   rxrpc: Add a trac...
520
521
522
523
  		trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq);
  	} else {
  		trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq);
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
524
525
526
527
528
529
530
531
532
  	queued = true;
  
  	if (after_eq(seq, call->rx_expect_next)) {
  		if (after(seq, call->rx_expect_next)) {
  			_net("OOS %u > %u", seq, call->rx_expect_next);
  			ack = RXRPC_ACK_OUT_OF_SEQUENCE;
  			ack_serial = serial;
  		}
  		call->rx_expect_next = seq + 1;
17926a793   David Howells   [AF_RXRPC]: Provi...
533
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
534
535
536
537
538
539
540
541
542
543
544
  skip:
  	offset += len;
  	if (flags & RXRPC_JUMBO_PACKET) {
  		if (skb_copy_bits(skb, offset, &flags, 1) < 0)
  			return rxrpc_proto_abort("XJF", call, seq);
  		offset += sizeof(struct rxrpc_jumbo_header);
  		seq++;
  		serial++;
  		annotation++;
  		if (flags & RXRPC_JUMBO_PACKET)
  			annotation |= RXRPC_RX_ANNO_JLAST;
75e421263   David Howells   rxrpc: Correctly ...
545
546
547
548
549
550
551
552
553
  		if (after(seq, hard_ack + call->rx_winsize)) {
  			ack = RXRPC_ACK_EXCEEDS_WINDOW;
  			ack_serial = serial;
  			if (!jumbo_bad) {
  				call->nr_jumbo_bad++;
  				jumbo_bad = true;
  			}
  			goto ack;
  		}
248f219cb   David Howells   rxrpc: Rewrite th...
554
555
556
557
  
  		_proto("Rx DATA Jumbo %%%u", serial);
  		goto next_subpacket;
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
558

248f219cb   David Howells   rxrpc: Rewrite th...
559
560
561
562
  	if (queued && flags & RXRPC_LAST_PACKET && !ack) {
  		ack = RXRPC_ACK_DELAY;
  		ack_serial = serial;
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
563

248f219cb   David Howells   rxrpc: Rewrite th...
564
565
566
  ack:
  	if (ack)
  		rxrpc_propose_ACK(call, ack, skew, ack_serial,
9c7ad4344   David Howells   rxrpc: Add tracep...
567
568
  				  immediate_ack, true,
  				  rxrpc_propose_ack_input_data);
17926a793   David Howells   [AF_RXRPC]: Provi...
569

248f219cb   David Howells   rxrpc: Rewrite th...
570
571
572
  	if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
  		rxrpc_notify_socket(call);
  	_leave(" [queued]");
17926a793   David Howells   [AF_RXRPC]: Provi...
573
574
575
  }
  
  /*
50235c4b5   David Howells   rxrpc: Obtain RTT...
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
   * Process a requested ACK.
   */
  static void rxrpc_input_requested_ack(struct rxrpc_call *call,
  				      ktime_t resp_time,
  				      rxrpc_serial_t orig_serial,
  				      rxrpc_serial_t ack_serial)
  {
  	struct rxrpc_skb_priv *sp;
  	struct sk_buff *skb;
  	ktime_t sent_at;
  	int ix;
  
  	for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) {
  		skb = call->rxtx_buffer[ix];
  		if (!skb)
  			continue;
  
  		sp = rxrpc_skb(skb);
  		if (sp->hdr.serial != orig_serial)
  			continue;
  		smp_rmb();
  		sent_at = skb->tstamp;
  		goto found;
  	}
  	return;
  
  found:
  	rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack,
  			   orig_serial, ack_serial, sent_at, resp_time);
  }
  
  /*
8e83134db   David Howells   rxrpc: Send pings...
608
609
610
611
612
613
614
615
616
   * Process a ping response.
   */
  static void rxrpc_input_ping_response(struct rxrpc_call *call,
  				      ktime_t resp_time,
  				      rxrpc_serial_t orig_serial,
  				      rxrpc_serial_t ack_serial)
  {
  	rxrpc_serial_t ping_serial;
  	ktime_t ping_time;
a5af7e1fc   David Howells   rxrpc: Fix loss o...
617
  	ping_time = call->ping_time;
8e83134db   David Howells   rxrpc: Send pings...
618
  	smp_rmb();
a5af7e1fc   David Howells   rxrpc: Fix loss o...
619
  	ping_serial = call->ping_serial;
8e83134db   David Howells   rxrpc: Send pings...
620
621
622
623
624
625
626
627
628
629
630
631
632
  
  	if (!test_bit(RXRPC_CALL_PINGING, &call->flags) ||
  	    before(orig_serial, ping_serial))
  		return;
  	clear_bit(RXRPC_CALL_PINGING, &call->flags);
  	if (after(orig_serial, ping_serial))
  		return;
  
  	rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response,
  			   orig_serial, ack_serial, ping_time, resp_time);
  }
  
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
633
   * Process the extra information that may be appended to an ACK packet
17926a793   David Howells   [AF_RXRPC]: Provi...
634
   */
248f219cb   David Howells   rxrpc: Rewrite th...
635
636
  static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
  				struct rxrpc_ackinfo *ackinfo)
17926a793   David Howells   [AF_RXRPC]: Provi...
637
  {
248f219cb   David Howells   rxrpc: Rewrite th...
638
639
640
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  	struct rxrpc_peer *peer;
  	unsigned int mtu;
702f2ac87   David Howells   rxrpc: Wake up th...
641
  	bool wake = false;
01fd07422   David Howells   rxrpc: Allow tx_w...
642
  	u32 rwind = ntohl(ackinfo->rwind);
248f219cb   David Howells   rxrpc: Rewrite th...
643
644
645
646
  
  	_proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
  	       sp->hdr.serial,
  	       ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
01fd07422   David Howells   rxrpc: Allow tx_w...
647
  	       rwind, ntohl(ackinfo->jumbo_max));
248f219cb   David Howells   rxrpc: Rewrite th...
648

702f2ac87   David Howells   rxrpc: Wake up th...
649
650
651
652
653
  	if (call->tx_winsize != rwind) {
  		if (rwind > RXRPC_RXTX_BUFF_SIZE - 1)
  			rwind = RXRPC_RXTX_BUFF_SIZE - 1;
  		if (rwind > call->tx_winsize)
  			wake = true;
740586d29   David Howells   rxrpc: Trace chan...
654
655
  		trace_rxrpc_rx_rwind_change(call, sp->hdr.serial,
  					    ntohl(ackinfo->rwind), wake);
702f2ac87   David Howells   rxrpc: Wake up th...
656
657
  		call->tx_winsize = rwind;
  	}
085111509   David Howells   rxrpc: Reduce sst...
658
659
  	if (call->cong_ssthresh > rwind)
  		call->cong_ssthresh = rwind;
248f219cb   David Howells   rxrpc: Rewrite th...
660
661
662
663
664
665
666
667
668
669
670
  
  	mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
  
  	peer = call->peer;
  	if (mtu < peer->maxdata) {
  		spin_lock_bh(&peer->lock);
  		peer->maxdata = mtu;
  		peer->mtu = mtu + peer->hdrsize;
  		spin_unlock_bh(&peer->lock);
  		_net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
  	}
702f2ac87   David Howells   rxrpc: Wake up th...
671
672
673
  
  	if (wake)
  		wake_up(&call->waitq);
248f219cb   David Howells   rxrpc: Rewrite th...
674
  }
17926a793   David Howells   [AF_RXRPC]: Provi...
675

248f219cb   David Howells   rxrpc: Rewrite th...
676
677
678
679
680
681
682
683
684
685
  /*
   * Process individual soft ACKs.
   *
   * Each ACK in the array corresponds to one packet and can be either an ACK or
   * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
   * packets that lie beyond the end of the ACK list are scheduled for resend by
   * the timer on the basis that the peer might just not have processed them at
   * the time the ACK was sent.
   */
  static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
31a1b9895   David Howells   rxrpc: Generate a...
686
687
  				  rxrpc_seq_t seq, int nr_acks,
  				  struct rxrpc_ack_summary *summary)
248f219cb   David Howells   rxrpc: Rewrite th...
688
  {
248f219cb   David Howells   rxrpc: Rewrite th...
689
  	int ix;
f07373ead   David Howells   rxrpc: Add re-sen...
690
  	u8 annotation, anno_type;
248f219cb   David Howells   rxrpc: Rewrite th...
691
692
693
  
  	for (; nr_acks > 0; nr_acks--, seq++) {
  		ix = seq & RXRPC_RXTX_BUFF_MASK;
f07373ead   David Howells   rxrpc: Add re-sen...
694
695
696
  		annotation = call->rxtx_annotations[ix];
  		anno_type = annotation & RXRPC_TX_ANNO_MASK;
  		annotation &= ~RXRPC_TX_ANNO_MASK;
d01dc4c3c   David Howells   rxrpc: Fix the pa...
697
  		switch (*acks++) {
248f219cb   David Howells   rxrpc: Rewrite th...
698
  		case RXRPC_ACK_TYPE_ACK:
31a1b9895   David Howells   rxrpc: Generate a...
699
  			summary->nr_acks++;
f07373ead   David Howells   rxrpc: Add re-sen...
700
701
  			if (anno_type == RXRPC_TX_ANNO_ACK)
  				continue;
31a1b9895   David Howells   rxrpc: Generate a...
702
  			summary->nr_new_acks++;
f07373ead   David Howells   rxrpc: Add re-sen...
703
704
  			call->rxtx_annotations[ix] =
  				RXRPC_TX_ANNO_ACK | annotation;
248f219cb   David Howells   rxrpc: Rewrite th...
705
706
  			break;
  		case RXRPC_ACK_TYPE_NACK:
31a1b9895   David Howells   rxrpc: Generate a...
707
708
709
710
711
712
  			if (!summary->nr_nacks &&
  			    call->acks_lowest_nak != seq) {
  				call->acks_lowest_nak = seq;
  				summary->new_low_nack = true;
  			}
  			summary->nr_nacks++;
f07373ead   David Howells   rxrpc: Add re-sen...
713
  			if (anno_type == RXRPC_TX_ANNO_NAK)
248f219cb   David Howells   rxrpc: Rewrite th...
714
  				continue;
31a1b9895   David Howells   rxrpc: Generate a...
715
  			summary->nr_new_nacks++;
be8aa3380   David Howells   rxrpc: Fix accide...
716
717
  			if (anno_type == RXRPC_TX_ANNO_RETRANS)
  				continue;
f07373ead   David Howells   rxrpc: Add re-sen...
718
719
  			call->rxtx_annotations[ix] =
  				RXRPC_TX_ANNO_NAK | annotation;
248f219cb   David Howells   rxrpc: Rewrite th...
720
721
722
  			break;
  		default:
  			return rxrpc_proto_abort("SFT", call, 0);
17926a793   David Howells   [AF_RXRPC]: Provi...
723
  		}
17926a793   David Howells   [AF_RXRPC]: Provi...
724
725
726
727
  	}
  }
  
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
728
729
730
731
732
733
734
735
   * Process an ACK packet.
   *
   * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
   * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
   *
   * A hard-ACK means that a packet has been processed and may be discarded; a
   * soft-ACK means that the packet may be discarded and retransmission
   * requested.  A phase is complete when all packets are hard-ACK'd.
17926a793   David Howells   [AF_RXRPC]: Provi...
736
   */
248f219cb   David Howells   rxrpc: Rewrite th...
737
738
  static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb,
  			    u16 skew)
17926a793   David Howells   [AF_RXRPC]: Provi...
739
  {
31a1b9895   David Howells   rxrpc: Generate a...
740
  	struct rxrpc_ack_summary summary = { 0 };
17926a793   David Howells   [AF_RXRPC]: Provi...
741
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
248f219cb   David Howells   rxrpc: Rewrite th...
742
743
744
745
746
  	union {
  		struct rxrpc_ackpacket ack;
  		struct rxrpc_ackinfo info;
  		u8 acks[RXRPC_MAXACKS];
  	} buf;
8e83134db   David Howells   rxrpc: Send pings...
747
  	rxrpc_serial_t acked_serial;
248f219cb   David Howells   rxrpc: Rewrite th...
748
  	rxrpc_seq_t first_soft_ack, hard_ack;
775e5b71d   David Howells   rxrpc: The offset...
749
  	int nr_acks, offset, ioffset;
248f219cb   David Howells   rxrpc: Rewrite th...
750
751
  
  	_enter("");
775e5b71d   David Howells   rxrpc: The offset...
752
753
  	offset = sizeof(struct rxrpc_wire_header);
  	if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
248f219cb   David Howells   rxrpc: Rewrite th...
754
755
  		_debug("extraction failure");
  		return rxrpc_proto_abort("XAK", call, 0);
17926a793   David Howells   [AF_RXRPC]: Provi...
756
  	}
775e5b71d   David Howells   rxrpc: The offset...
757
  	offset += sizeof(buf.ack);
248f219cb   David Howells   rxrpc: Rewrite th...
758

8e83134db   David Howells   rxrpc: Send pings...
759
  	acked_serial = ntohl(buf.ack.serial);
248f219cb   David Howells   rxrpc: Rewrite th...
760
761
762
  	first_soft_ack = ntohl(buf.ack.firstPacket);
  	hard_ack = first_soft_ack - 1;
  	nr_acks = buf.ack.nAcks;
31a1b9895   David Howells   rxrpc: Generate a...
763
764
  	summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
  			      buf.ack.reason : RXRPC_ACK__INVALID);
248f219cb   David Howells   rxrpc: Rewrite th...
765

b1d9f7fde   David Howells   rxrpc: Add some m...
766
767
768
  	trace_rxrpc_rx_ack(call, sp->hdr.serial, acked_serial,
  			   first_soft_ack, ntohl(buf.ack.previousPacket),
  			   summary.ack_reason, nr_acks);
ec71eb9ad   David Howells   rxrpc: Add a trac...
769

8e83134db   David Howells   rxrpc: Send pings...
770
771
772
  	if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE)
  		rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
  					  sp->hdr.serial);
50235c4b5   David Howells   rxrpc: Obtain RTT...
773
774
775
  	if (buf.ack.reason == RXRPC_ACK_REQUESTED)
  		rxrpc_input_requested_ack(call, skb->tstamp, acked_serial,
  					  sp->hdr.serial);
8e83134db   David Howells   rxrpc: Send pings...
776

248f219cb   David Howells   rxrpc: Rewrite th...
777
778
779
  	if (buf.ack.reason == RXRPC_ACK_PING) {
  		_proto("Rx ACK %%%u PING Request", sp->hdr.serial);
  		rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
9c7ad4344   David Howells   rxrpc: Add tracep...
780
781
  				  skew, sp->hdr.serial, true, true,
  				  rxrpc_propose_ack_respond_to_ping);
248f219cb   David Howells   rxrpc: Rewrite th...
782
  	} else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
563ea7d5d   David Howells   rxrpc: Calculate ...
783
  		rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED,
9c7ad4344   David Howells   rxrpc: Add tracep...
784
785
  				  skew, sp->hdr.serial, true, true,
  				  rxrpc_propose_ack_respond_to_ack);
17926a793   David Howells   [AF_RXRPC]: Provi...
786
  	}
775e5b71d   David Howells   rxrpc: The offset...
787
788
789
  	ioffset = offset + nr_acks + 3;
  	if (skb->len >= ioffset + sizeof(buf.info)) {
  		if (skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
248f219cb   David Howells   rxrpc: Rewrite th...
790
791
792
  			return rxrpc_proto_abort("XAI", call, 0);
  		rxrpc_input_ackinfo(call, skb, &buf.info);
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
793

248f219cb   David Howells   rxrpc: Rewrite th...
794
795
  	if (first_soft_ack == 0)
  		return rxrpc_proto_abort("AK0", call, 0);
17926a793   David Howells   [AF_RXRPC]: Provi...
796

248f219cb   David Howells   rxrpc: Rewrite th...
797
  	/* Ignore ACKs unless we are or have just been transmitting. */
146d8fef9   David Howells   rxrpc: Call state...
798
  	switch (READ_ONCE(call->state)) {
248f219cb   David Howells   rxrpc: Rewrite th...
799
800
801
802
803
  	case RXRPC_CALL_CLIENT_SEND_REQUEST:
  	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
  	case RXRPC_CALL_SERVER_SEND_REPLY:
  	case RXRPC_CALL_SERVER_AWAIT_ACK:
  		break;
17926a793   David Howells   [AF_RXRPC]: Provi...
804
  	default:
248f219cb   David Howells   rxrpc: Rewrite th...
805
806
  		return;
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
807

248f219cb   David Howells   rxrpc: Rewrite th...
808
  	/* Discard any out-of-order or duplicate ACKs. */
98dafac56   David Howells   rxrpc: Use before...
809
  	if (before_eq(sp->hdr.serial, call->acks_latest)) {
248f219cb   David Howells   rxrpc: Rewrite th...
810
811
812
813
  		_debug("discard ACK %d <= %d",
  		       sp->hdr.serial, call->acks_latest);
  		return;
  	}
57494343c   David Howells   rxrpc: Implement ...
814
  	call->acks_latest_ts = skb->tstamp;
248f219cb   David Howells   rxrpc: Rewrite th...
815
  	call->acks_latest = sp->hdr.serial;
17926a793   David Howells   [AF_RXRPC]: Provi...
816

248f219cb   David Howells   rxrpc: Rewrite th...
817
818
819
  	if (before(hard_ack, call->tx_hard_ack) ||
  	    after(hard_ack, call->tx_top))
  		return rxrpc_proto_abort("AKW", call, 0);
70790dbe3   David Howells   rxrpc: Pass the l...
820
821
  	if (nr_acks > call->tx_top - hard_ack)
  		return rxrpc_proto_abort("AKN", call, 0);
17926a793   David Howells   [AF_RXRPC]: Provi...
822

248f219cb   David Howells   rxrpc: Rewrite th...
823
  	if (after(hard_ack, call->tx_hard_ack))
31a1b9895   David Howells   rxrpc: Generate a...
824
  		rxrpc_rotate_tx_window(call, hard_ack, &summary);
17926a793   David Howells   [AF_RXRPC]: Provi...
825

70790dbe3   David Howells   rxrpc: Pass the l...
826
  	if (nr_acks > 0) {
775e5b71d   David Howells   rxrpc: The offset...
827
  		if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0)
70790dbe3   David Howells   rxrpc: Pass the l...
828
  			return rxrpc_proto_abort("XSA", call, 0);
31a1b9895   David Howells   rxrpc: Generate a...
829
830
  		rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
  				      &summary);
70790dbe3   David Howells   rxrpc: Pass the l...
831
832
833
834
  	}
  
  	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
  		rxrpc_end_tx_phase(call, false, "ETA");
248f219cb   David Howells   rxrpc: Rewrite th...
835
  		return;
70790dbe3   David Howells   rxrpc: Pass the l...
836
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
837

0d967960d   David Howells   rxrpc: Schedule a...
838
839
  	if (call->rxtx_annotations[call->tx_top & RXRPC_RXTX_BUFF_MASK] &
  	    RXRPC_TX_ANNO_LAST &&
a9f312d98   David Howells   rxrpc: Only ping ...
840
841
  	    summary.nr_acks == call->tx_top - hard_ack &&
  	    rxrpc_is_client_call(call))
0d967960d   David Howells   rxrpc: Schedule a...
842
843
844
  		rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial,
  				  false, true,
  				  rxrpc_propose_ack_ping_for_lost_reply);
57494343c   David Howells   rxrpc: Implement ...
845

ed1e8679d   David Howells   rxrpc: Note seria...
846
  	return rxrpc_congestion_management(call, skb, &summary, acked_serial);
17926a793   David Howells   [AF_RXRPC]: Provi...
847
848
849
  }
  
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
850
   * Process an ACKALL packet.
17926a793   David Howells   [AF_RXRPC]: Provi...
851
   */
248f219cb   David Howells   rxrpc: Rewrite th...
852
  static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
17926a793   David Howells   [AF_RXRPC]: Provi...
853
  {
31a1b9895   David Howells   rxrpc: Generate a...
854
  	struct rxrpc_ack_summary summary = { 0 };
248f219cb   David Howells   rxrpc: Rewrite th...
855
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a793   David Howells   [AF_RXRPC]: Provi...
856

248f219cb   David Howells   rxrpc: Rewrite th...
857
  	_proto("Rx ACKALL %%%u", sp->hdr.serial);
17926a793   David Howells   [AF_RXRPC]: Provi...
858

31a1b9895   David Howells   rxrpc: Generate a...
859
  	rxrpc_rotate_tx_window(call, call->tx_top, &summary);
70790dbe3   David Howells   rxrpc: Pass the l...
860
861
  	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags))
  		rxrpc_end_tx_phase(call, false, "ETL");
248f219cb   David Howells   rxrpc: Rewrite th...
862
  }
17926a793   David Howells   [AF_RXRPC]: Provi...
863

248f219cb   David Howells   rxrpc: Rewrite th...
864
  /*
005ede286   David Howells   rxrpc: Trace rece...
865
   * Process an ABORT packet directed at a call.
248f219cb   David Howells   rxrpc: Rewrite th...
866
867
868
869
870
871
   */
  static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
  {
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  	__be32 wtmp;
  	u32 abort_code = RX_CALL_DEAD;
17926a793   David Howells   [AF_RXRPC]: Provi...
872

248f219cb   David Howells   rxrpc: Rewrite th...
873
  	_enter("");
17926a793   David Howells   [AF_RXRPC]: Provi...
874

248f219cb   David Howells   rxrpc: Rewrite th...
875
  	if (skb->len >= 4 &&
775e5b71d   David Howells   rxrpc: The offset...
876
877
  	    skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  			  &wtmp, sizeof(wtmp)) >= 0)
248f219cb   David Howells   rxrpc: Rewrite th...
878
  		abort_code = ntohl(wtmp);
17926a793   David Howells   [AF_RXRPC]: Provi...
879

005ede286   David Howells   rxrpc: Trace rece...
880
  	trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
248f219cb   David Howells   rxrpc: Rewrite th...
881
  	_proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
17926a793   David Howells   [AF_RXRPC]: Provi...
882

248f219cb   David Howells   rxrpc: Rewrite th...
883
  	if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
3a92789af   David Howells   rxrpc: Use negati...
884
  				      abort_code, -ECONNABORTED))
248f219cb   David Howells   rxrpc: Rewrite th...
885
  		rxrpc_notify_socket(call);
17926a793   David Howells   [AF_RXRPC]: Provi...
886
887
888
  }
  
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
889
   * Process an incoming call packet.
17926a793   David Howells   [AF_RXRPC]: Provi...
890
   */
248f219cb   David Howells   rxrpc: Rewrite th...
891
892
  static void rxrpc_input_call_packet(struct rxrpc_call *call,
  				    struct sk_buff *skb, u16 skew)
17926a793   David Howells   [AF_RXRPC]: Provi...
893
  {
248f219cb   David Howells   rxrpc: Rewrite th...
894
  	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a793   David Howells   [AF_RXRPC]: Provi...
895

7727640cc   Tim Smith   af_rxrpc: Keep rx...
896
  	_enter("%p,%p", call, skb);
17926a793   David Howells   [AF_RXRPC]: Provi...
897

248f219cb   David Howells   rxrpc: Rewrite th...
898
899
900
901
  	switch (sp->hdr.type) {
  	case RXRPC_PACKET_TYPE_DATA:
  		rxrpc_input_data(call, skb, skew);
  		break;
f5c17aaeb   David Howells   rxrpc: Calls shou...
902

248f219cb   David Howells   rxrpc: Rewrite th...
903
904
  	case RXRPC_PACKET_TYPE_ACK:
  		rxrpc_input_ack(call, skb, skew);
17926a793   David Howells   [AF_RXRPC]: Provi...
905
  		break;
17926a793   David Howells   [AF_RXRPC]: Provi...
906

248f219cb   David Howells   rxrpc: Rewrite th...
907
908
  	case RXRPC_PACKET_TYPE_BUSY:
  		_proto("Rx BUSY %%%u", sp->hdr.serial);
17926a793   David Howells   [AF_RXRPC]: Provi...
909

248f219cb   David Howells   rxrpc: Rewrite th...
910
911
912
913
914
  		/* Just ignore BUSY packets from the server; the retry and
  		 * lifespan timers will take care of business.  BUSY packets
  		 * from the client don't make sense.
  		 */
  		break;
17926a793   David Howells   [AF_RXRPC]: Provi...
915

248f219cb   David Howells   rxrpc: Rewrite th...
916
917
918
  	case RXRPC_PACKET_TYPE_ABORT:
  		rxrpc_input_abort(call, skb);
  		break;
17926a793   David Howells   [AF_RXRPC]: Provi...
919

248f219cb   David Howells   rxrpc: Rewrite th...
920
921
922
  	case RXRPC_PACKET_TYPE_ACKALL:
  		rxrpc_input_ackall(call, skb);
  		break;
f5c17aaeb   David Howells   rxrpc: Calls shou...
923

248f219cb   David Howells   rxrpc: Rewrite th...
924
  	default:
248f219cb   David Howells   rxrpc: Rewrite th...
925
  		break;
17926a793   David Howells   [AF_RXRPC]: Provi...
926
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
927

17926a793   David Howells   [AF_RXRPC]: Provi...
928
929
930
931
  	_leave("");
  }
  
  /*
b3156274c   David Howells   rxrpc: Partially ...
932
933
934
935
936
937
938
939
   * Handle a new call on a channel implicitly completing the preceding call on
   * that channel.
   *
   * TODO: If callNumber > call_id + 1, renegotiate security.
   */
  static void rxrpc_input_implicit_end_call(struct rxrpc_connection *conn,
  					  struct rxrpc_call *call)
  {
146d8fef9   David Howells   rxrpc: Call state...
940
  	switch (READ_ONCE(call->state)) {
b3156274c   David Howells   rxrpc: Partially ...
941
942
943
944
945
946
  	case RXRPC_CALL_SERVER_AWAIT_ACK:
  		rxrpc_call_completed(call);
  		break;
  	case RXRPC_CALL_COMPLETE:
  		break;
  	default:
3a92789af   David Howells   rxrpc: Use negati...
947
  		if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
b3156274c   David Howells   rxrpc: Partially ...
948
949
950
951
952
  			set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  			rxrpc_queue_call(call);
  		}
  		break;
  	}
b1d9f7fde   David Howells   rxrpc: Add some m...
953
  	trace_rxrpc_improper_term(call);
b3156274c   David Howells   rxrpc: Partially ...
954
955
956
957
958
  	__rxrpc_disconnect_call(conn, call);
  	rxrpc_notify_socket(call);
  }
  
  /*
17926a793   David Howells   [AF_RXRPC]: Provi...
959
   * post connection-level events to the connection
18bfeba50   David Howells   rxrpc: Perform te...
960
961
   * - this includes challenges, responses, some aborts and call terminal packet
   *   retransmission.
17926a793   David Howells   [AF_RXRPC]: Provi...
962
   */
2e7e9758b   David Howells   rxrpc: Once packe...
963
  static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
17926a793   David Howells   [AF_RXRPC]: Provi...
964
965
966
  				      struct sk_buff *skb)
  {
  	_enter("%p,%p", conn, skb);
17926a793   David Howells   [AF_RXRPC]: Provi...
967
  	skb_queue_tail(&conn->rx_queue, skb);
2e7e9758b   David Howells   rxrpc: Once packe...
968
  	rxrpc_queue_conn(conn);
17926a793   David Howells   [AF_RXRPC]: Provi...
969
  }
44ba06987   David Howells   RxRPC: Handle VER...
970
971
972
973
974
975
976
977
  /*
   * post endpoint-level events to the local endpoint
   * - this includes debug and version messages
   */
  static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
  				       struct sk_buff *skb)
  {
  	_enter("%p,%p", local, skb);
44ba06987   David Howells   RxRPC: Handle VER...
978
  	skb_queue_tail(&local->event_queue, skb);
5acbee464   David Howells   rxrpc: Provide qu...
979
  	rxrpc_queue_local(local);
44ba06987   David Howells   RxRPC: Handle VER...
980
  }
0d12f8a40   David Howells   rxrpc: Keep the s...
981
  /*
248f219cb   David Howells   rxrpc: Rewrite th...
982
983
984
985
986
987
988
989
990
991
992
   * put a packet up for transport-level abort
   */
  static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
  {
  	CHECK_SLAB_OKAY(&local->usage);
  
  	skb_queue_tail(&local->reject_queue, skb);
  	rxrpc_queue_local(local);
  }
  
  /*
0d12f8a40   David Howells   rxrpc: Keep the s...
993
994
995
996
997
998
999
1000
   * Extract the wire header from a packet and translate the byte order.
   */
  static noinline
  int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
  {
  	struct rxrpc_wire_header whdr;
  
  	/* dig out the RxRPC connection details */
fb46f6ee1   David Howells   rxrpc: Trace prot...
1001
1002
1003
  	if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
  		trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
  				      tracepoint_string("bad_hdr"));
0d12f8a40   David Howells   rxrpc: Keep the s...
1004
  		return -EBADMSG;
fb46f6ee1   David Howells   rxrpc: Trace prot...
1005
  	}
0d12f8a40   David Howells   rxrpc: Keep the s...
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
  
  	memset(sp, 0, sizeof(*sp));
  	sp->hdr.epoch		= ntohl(whdr.epoch);
  	sp->hdr.cid		= ntohl(whdr.cid);
  	sp->hdr.callNumber	= ntohl(whdr.callNumber);
  	sp->hdr.seq		= ntohl(whdr.seq);
  	sp->hdr.serial		= ntohl(whdr.serial);
  	sp->hdr.flags		= whdr.flags;
  	sp->hdr.type		= whdr.type;
  	sp->hdr.userStatus	= whdr.userStatus;
  	sp->hdr.securityIndex	= whdr.securityIndex;
  	sp->hdr._rsvd		= ntohs(whdr._rsvd);
  	sp->hdr.serviceId	= ntohs(whdr.serviceId);
  	return 0;
  }
17926a793   David Howells   [AF_RXRPC]: Provi...
1021
1022
1023
  /*
   * handle data received on the local endpoint
   * - may be called in interrupt context
4f95dd78a   David Howells   rxrpc: Rework loc...
1024
1025
1026
1027
   *
   * The socket is locked by the caller and this prevents the socket from being
   * shut down and the local endpoint from going away, thus sk_user_data will not
   * be cleared until this function returns.
17926a793   David Howells   [AF_RXRPC]: Provi...
1028
   */
248f219cb   David Howells   rxrpc: Rewrite th...
1029
  void rxrpc_data_ready(struct sock *udp_sk)
17926a793   David Howells   [AF_RXRPC]: Provi...
1030
  {
8496af50e   David Howells   rxrpc: Use RCU to...
1031
  	struct rxrpc_connection *conn;
248f219cb   David Howells   rxrpc: Rewrite th...
1032
1033
  	struct rxrpc_channel *chan;
  	struct rxrpc_call *call;
17926a793   David Howells   [AF_RXRPC]: Provi...
1034
  	struct rxrpc_skb_priv *sp;
248f219cb   David Howells   rxrpc: Rewrite th...
1035
  	struct rxrpc_local *local = udp_sk->sk_user_data;
17926a793   David Howells   [AF_RXRPC]: Provi...
1036
  	struct sk_buff *skb;
248f219cb   David Howells   rxrpc: Rewrite th...
1037
  	unsigned int channel;
563ea7d5d   David Howells   rxrpc: Calculate ...
1038
  	int ret, skew;
17926a793   David Howells   [AF_RXRPC]: Provi...
1039

248f219cb   David Howells   rxrpc: Rewrite th...
1040
  	_enter("%p", udp_sk);
17926a793   David Howells   [AF_RXRPC]: Provi...
1041
1042
  
  	ASSERT(!irqs_disabled());
7c13f97ff   Paolo Abeni   udp: do fwd memor...
1043
  	skb = skb_recv_udp(udp_sk, 0, 1, &ret);
17926a793   David Howells   [AF_RXRPC]: Provi...
1044
  	if (!skb) {
17926a793   David Howells   [AF_RXRPC]: Provi...
1045
1046
1047
1048
1049
  		if (ret == -EAGAIN)
  			return;
  		_debug("UDP socket error %d", ret);
  		return;
  	}
71f3ca408   David Howells   rxrpc: Improve sk...
1050
  	rxrpc_new_skb(skb, rxrpc_skb_rx_received);
17926a793   David Howells   [AF_RXRPC]: Provi...
1051
1052
1053
1054
1055
  
  	_net("recv skb %p", skb);
  
  	/* we'll probably need to checksum it (didn't call sock_recvmsg) */
  	if (skb_checksum_complete(skb)) {
71f3ca408   David Howells   rxrpc: Improve sk...
1056
  		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
02c223470   Eric Dumazet   net: udp: rename ...
1057
  		__UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0);
17926a793   David Howells   [AF_RXRPC]: Provi...
1058
1059
1060
  		_leave(" [CSUM failed]");
  		return;
  	}
02c223470   Eric Dumazet   net: udp: rename ...
1061
  	__UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0);
1781f7f58   Herbert Xu   [UDP]: Restore mi...
1062

7c13f97ff   Paolo Abeni   udp: do fwd memor...
1063
1064
  	/* The UDP protocol already released all skb resources;
  	 * we are free to add our own data there.
0d12f8a40   David Howells   rxrpc: Keep the s...
1065
  	 */
17926a793   David Howells   [AF_RXRPC]: Provi...
1066
  	sp = rxrpc_skb(skb);
17926a793   David Howells   [AF_RXRPC]: Provi...
1067

89b475abd   David Howells   rxrpc: Add a trac...
1068
1069
1070
  	/* dig out the RxRPC connection details */
  	if (rxrpc_extract_header(sp, skb) < 0)
  		goto bad_message;
8a681c360   David Howells   rxrpc: Add config...
1071
1072
1073
  	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
  		static int lose;
  		if ((lose++ & 7) == 7) {
89b475abd   David Howells   rxrpc: Add a trac...
1074
  			trace_rxrpc_rx_lose(sp);
8a681c360   David Howells   rxrpc: Add config...
1075
1076
1077
1078
  			rxrpc_lose_skb(skb, rxrpc_skb_rx_lost);
  			return;
  		}
  	}
49e19ec7d   David Howells   rxrpc: Add tracep...
1079
  	trace_rxrpc_rx_packet(sp);
17926a793   David Howells   [AF_RXRPC]: Provi...
1080
1081
1082
  
  	_net("Rx RxRPC %s ep=%x call=%x:%x",
  	     sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient",
0d12f8a40   David Howells   rxrpc: Keep the s...
1083
  	     sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber);
17926a793   David Howells   [AF_RXRPC]: Provi...
1084

351c1e648   David Howells   rxrpc: Be more se...
1085
1086
  	if (sp->hdr.type >= RXRPC_N_PACKET_TYPES ||
  	    !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) {
17926a793   David Howells   [AF_RXRPC]: Provi...
1087
1088
1089
  		_proto("Rx Bad Packet Type %u", sp->hdr.type);
  		goto bad_message;
  	}
248f219cb   David Howells   rxrpc: Rewrite th...
1090
1091
  	switch (sp->hdr.type) {
  	case RXRPC_PACKET_TYPE_VERSION:
44ba06987   David Howells   RxRPC: Handle VER...
1092
1093
  		rxrpc_post_packet_to_local(local, skb);
  		goto out;
bc6e1ea32   David Howells   rxrpc: Trim line-...
1094

248f219cb   David Howells   rxrpc: Rewrite th...
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
  	case RXRPC_PACKET_TYPE_BUSY:
  		if (sp->hdr.flags & RXRPC_CLIENT_INITIATED)
  			goto discard;
  
  	case RXRPC_PACKET_TYPE_DATA:
  		if (sp->hdr.callNumber == 0)
  			goto bad_message;
  		if (sp->hdr.flags & RXRPC_JUMBO_PACKET &&
  		    !rxrpc_validate_jumbo(skb))
  			goto bad_message;
  		break;
  	}
17926a793   David Howells   [AF_RXRPC]: Provi...
1107

8496af50e   David Howells   rxrpc: Use RCU to...
1108
  	rcu_read_lock();
8496af50e   David Howells   rxrpc: Use RCU to...
1109
  	conn = rxrpc_find_connection_rcu(local, skb);
248f219cb   David Howells   rxrpc: Rewrite th...
1110
1111
1112
  	if (conn) {
  		if (sp->hdr.securityIndex != conn->security_ix)
  			goto wrong_security;
563ea7d5d   David Howells   rxrpc: Calculate ...
1113

4e255721d   David Howells   rxrpc: Add servic...
1114
1115
1116
1117
1118
1119
1120
  		if (sp->hdr.serviceId != conn->service_id) {
  			if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) ||
  			    conn->service_id != conn->params.service_id)
  				goto reupgrade;
  			conn->service_id = sp->hdr.serviceId;
  		}
  		
248f219cb   David Howells   rxrpc: Rewrite th...
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
  		if (sp->hdr.callNumber == 0) {
  			/* Connection-level packet */
  			_debug("CONN %p {%d}", conn, conn->debug_id);
  			rxrpc_post_packet_to_conn(conn, skb);
  			goto out_unlock;
  		}
  
  		/* Note the serial number skew here */
  		skew = (int)sp->hdr.serial - (int)conn->hi_serial;
  		if (skew >= 0) {
  			if (skew > 0)
  				conn->hi_serial = sp->hdr.serial;
  		} else {
  			skew = -skew;
  			skew = min(skew, 65535);
  		}
17926a793   David Howells   [AF_RXRPC]: Provi...
1137

8496af50e   David Howells   rxrpc: Use RCU to...
1138
  		/* Call-bound packets are routed by connection channel. */
248f219cb   David Howells   rxrpc: Rewrite th...
1139
1140
  		channel = sp->hdr.cid & RXRPC_CHANNELMASK;
  		chan = &conn->channels[channel];
18bfeba50   David Howells   rxrpc: Perform te...
1141
1142
1143
1144
1145
1146
  
  		/* Ignore really old calls */
  		if (sp->hdr.callNumber < chan->last_call)
  			goto discard_unlock;
  
  		if (sp->hdr.callNumber == chan->last_call) {
637b9b187   David Howells   rxrpc: Don't trea...
1147
1148
1149
1150
1151
1152
  			if (chan->call ||
  			    sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
  				goto discard_unlock;
  
  			/* For the previous service call, if completed
  			 * successfully, we discard all further packets.
18bfeba50   David Howells   rxrpc: Perform te...
1153
  			 */
2266ffdef   David Howells   rxrpc: Fix conn-b...
1154
  			if (rxrpc_conn_is_service(conn) &&
637b9b187   David Howells   rxrpc: Don't trea...
1155
  			    chan->last_type == RXRPC_PACKET_TYPE_ACK)
18bfeba50   David Howells   rxrpc: Perform te...
1156
  				goto discard_unlock;
637b9b187   David Howells   rxrpc: Don't trea...
1157
1158
  			/* But otherwise we need to retransmit the final packet
  			 * from data cached in the connection record.
18bfeba50   David Howells   rxrpc: Perform te...
1159
1160
1161
1162
  			 */
  			rxrpc_post_packet_to_conn(conn, skb);
  			goto out_unlock;
  		}
0d12f8a40   David Howells   rxrpc: Keep the s...
1163

18bfeba50   David Howells   rxrpc: Perform te...
1164
  		call = rcu_dereference(chan->call);
b3156274c   David Howells   rxrpc: Partially ...
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
  
  		if (sp->hdr.callNumber > chan->call_id) {
  			if (!(sp->hdr.flags & RXRPC_CLIENT_INITIATED)) {
  				rcu_read_unlock();
  				goto reject_packet;
  			}
  			if (call)
  				rxrpc_input_implicit_end_call(conn, call);
  			call = NULL;
  		}
4e255721d   David Howells   rxrpc: Add servic...
1175
1176
1177
  
  		if (call && sp->hdr.serviceId != call->service_id)
  			call->service_id = sp->hdr.serviceId;
248f219cb   David Howells   rxrpc: Rewrite th...
1178
1179
1180
1181
  	} else {
  		skew = 0;
  		call = NULL;
  	}
8496af50e   David Howells   rxrpc: Use RCU to...
1182

248f219cb   David Howells   rxrpc: Rewrite th...
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  	if (!call || atomic_read(&call->usage) == 0) {
  		if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) ||
  		    sp->hdr.callNumber == 0 ||
  		    sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
  			goto bad_message_unlock;
  		if (sp->hdr.seq != 1)
  			goto discard_unlock;
  		call = rxrpc_new_incoming_call(local, conn, skb);
  		if (!call) {
  			rcu_read_unlock();
  			goto reject_packet;
  		}
8e83134db   David Howells   rxrpc: Send pings...
1195
  		rxrpc_send_ping(call, skb, skew);
540b1c48c   David Howells   rxrpc: Fix deadlo...
1196
  		mutex_unlock(&call->user_mutex);
7727640cc   Tim Smith   af_rxrpc: Keep rx...
1197
  	}
44ba06987   David Howells   RxRPC: Handle VER...
1198

248f219cb   David Howells   rxrpc: Rewrite th...
1199
1200
  	rxrpc_input_call_packet(call, skb, skew);
  	goto discard_unlock;
18bfeba50   David Howells   rxrpc: Perform te...
1201
  discard_unlock:
8496af50e   David Howells   rxrpc: Use RCU to...
1202
  	rcu_read_unlock();
248f219cb   David Howells   rxrpc: Rewrite th...
1203
  discard:
71f3ca408   David Howells   rxrpc: Improve sk...
1204
  	rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
44ba06987   David Howells   RxRPC: Handle VER...
1205
  out:
49e19ec7d   David Howells   rxrpc: Add tracep...
1206
  	trace_rxrpc_rx_done(0, 0);
17926a793   David Howells   [AF_RXRPC]: Provi...
1207
  	return;
248f219cb   David Howells   rxrpc: Rewrite th...
1208
  out_unlock:
8496af50e   David Howells   rxrpc: Use RCU to...
1209
  	rcu_read_unlock();
248f219cb   David Howells   rxrpc: Rewrite th...
1210
  	goto out;
8496af50e   David Howells   rxrpc: Use RCU to...
1211

248f219cb   David Howells   rxrpc: Rewrite th...
1212
1213
1214
1215
1216
1217
  wrong_security:
  	rcu_read_unlock();
  	trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  			  RXKADINCONSISTENCY, EBADMSG);
  	skb->priority = RXKADINCONSISTENCY;
  	goto post_abort;
17926a793   David Howells   [AF_RXRPC]: Provi...
1218

4e255721d   David Howells   rxrpc: Add servic...
1219
1220
1221
1222
1223
  reupgrade:
  	rcu_read_unlock();
  	trace_rxrpc_abort("UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  			  RX_PROTOCOL_ERROR, EBADMSG);
  	goto protocol_error;
248f219cb   David Howells   rxrpc: Rewrite th...
1224
1225
  bad_message_unlock:
  	rcu_read_unlock();
17926a793   David Howells   [AF_RXRPC]: Provi...
1226
  bad_message:
248f219cb   David Howells   rxrpc: Rewrite th...
1227
1228
  	trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  			  RX_PROTOCOL_ERROR, EBADMSG);
4e255721d   David Howells   rxrpc: Add servic...
1229
  protocol_error:
17926a793   David Howells   [AF_RXRPC]: Provi...
1230
  	skb->priority = RX_PROTOCOL_ERROR;
248f219cb   David Howells   rxrpc: Rewrite th...
1231
1232
  post_abort:
  	skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
49e19ec7d   David Howells   rxrpc: Add tracep...
1233
1234
  reject_packet:
  	trace_rxrpc_rx_done(skb->mark, skb->priority);
17926a793   David Howells   [AF_RXRPC]: Provi...
1235
  	rxrpc_reject_packet(local, skb);
17926a793   David Howells   [AF_RXRPC]: Provi...
1236
1237
  	_leave(" [badmsg]");
  }