blob: d0eb98e1391c994baa29c6dd033c9b3e22e9b7f9 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * 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
12#include <linux/module.h>
13#include <linux/circ_buf.h>
14#include <linux/net.h>
15#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070017#include <linux/udp.h>
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
David Howells5873c082014-02-07 18:58:44 +000022/*
David Howells17926a72007-04-26 15:48:28 -070023 * propose an ACK be sent
24 */
David Howells4e36a952009-09-16 00:01:13 -070025void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells0d12f8a2016-03-04 15:53:46 +000026 u32 serial, bool immediate)
David Howells17926a72007-04-26 15:48:28 -070027{
28 unsigned long expiry;
29 s8 prior = rxrpc_ack_priority[ack_reason];
30
31 ASSERTCMP(prior, >, 0);
32
33 _enter("{%d},%s,%%%x,%u",
David Howells0d12f8a2016-03-04 15:53:46 +000034 call->debug_id, rxrpc_acks(ack_reason), serial, immediate);
David Howells17926a72007-04-26 15:48:28 -070035
36 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
37 if (immediate)
38 goto cancel_timer;
39 return;
40 }
41
42 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
43 * numbers */
44 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
45 if (prior <= 4)
46 call->ackr_serial = serial;
47 if (immediate)
48 goto cancel_timer;
49 return;
50 }
51
52 call->ackr_reason = ack_reason;
53 call->ackr_serial = serial;
54
55 switch (ack_reason) {
56 case RXRPC_ACK_DELAY:
57 _debug("run delay timer");
David Howells5873c082014-02-07 18:58:44 +000058 expiry = rxrpc_soft_ack_delay;
59 goto run_timer;
David Howells17926a72007-04-26 15:48:28 -070060
61 case RXRPC_ACK_IDLE:
62 if (!immediate) {
63 _debug("run defer timer");
David Howells5873c082014-02-07 18:58:44 +000064 expiry = rxrpc_idle_ack_delay;
David Howells17926a72007-04-26 15:48:28 -070065 goto run_timer;
66 }
67 goto cancel_timer;
68
69 case RXRPC_ACK_REQUESTED:
David Howells5873c082014-02-07 18:58:44 +000070 expiry = rxrpc_requested_ack_delay;
71 if (!expiry)
David Howells17926a72007-04-26 15:48:28 -070072 goto cancel_timer;
David Howells0d12f8a2016-03-04 15:53:46 +000073 if (!immediate || serial == 1) {
David Howells17926a72007-04-26 15:48:28 -070074 _debug("run defer timer");
David Howells17926a72007-04-26 15:48:28 -070075 goto run_timer;
76 }
77
78 default:
79 _debug("immediate ACK");
80 goto cancel_timer;
81 }
82
83run_timer:
84 expiry += jiffies;
85 if (!timer_pending(&call->ack_timer) ||
86 time_after(call->ack_timer.expires, expiry))
87 mod_timer(&call->ack_timer, expiry);
88 return;
89
90cancel_timer:
David Howells0d12f8a2016-03-04 15:53:46 +000091 _debug("cancel timer %%%u", serial);
David Howells17926a72007-04-26 15:48:28 -070092 try_to_del_timer_sync(&call->ack_timer);
93 read_lock_bh(&call->state_lock);
94 if (call->state <= RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +000095 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -070096 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -070097 read_unlock_bh(&call->state_lock);
98}
99
100/*
101 * propose an ACK be sent, locking the call structure
102 */
David Howells4e36a952009-09-16 00:01:13 -0700103void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells0d12f8a2016-03-04 15:53:46 +0000104 u32 serial, bool immediate)
David Howells17926a72007-04-26 15:48:28 -0700105{
106 s8 prior = rxrpc_ack_priority[ack_reason];
107
108 if (prior > rxrpc_ack_priority[call->ackr_reason]) {
109 spin_lock_bh(&call->lock);
110 __rxrpc_propose_ACK(call, ack_reason, serial, immediate);
111 spin_unlock_bh(&call->lock);
112 }
113}
114
115/*
116 * set the resend timer
117 */
118static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend,
119 unsigned long resend_at)
120{
121 read_lock_bh(&call->state_lock);
122 if (call->state >= RXRPC_CALL_COMPLETE)
123 resend = 0;
124
125 if (resend & 1) {
126 _debug("SET RESEND");
David Howells4c198ad2016-03-04 15:53:46 +0000127 set_bit(RXRPC_CALL_EV_RESEND, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700128 }
129
130 if (resend & 2) {
131 _debug("MODIFY RESEND TIMER");
132 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
133 mod_timer(&call->resend_timer, resend_at);
134 } else {
135 _debug("KILL RESEND TIMER");
136 del_timer_sync(&call->resend_timer);
David Howells4c198ad2016-03-04 15:53:46 +0000137 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700138 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
139 }
140 read_unlock_bh(&call->state_lock);
141}
142
143/*
144 * resend packets
145 */
146static void rxrpc_resend(struct rxrpc_call *call)
147{
David Howells0d12f8a2016-03-04 15:53:46 +0000148 struct rxrpc_wire_header *whdr;
David Howells17926a72007-04-26 15:48:28 -0700149 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700150 struct sk_buff *txb;
151 unsigned long *p_txb, resend_at;
Florian Westphalc03ae532015-02-28 11:51:36 +0100152 bool stop;
153 int loop;
David Howells17926a72007-04-26 15:48:28 -0700154 u8 resend;
155
156 _enter("{%d,%d,%d,%d},",
157 call->acks_hard, call->acks_unacked,
158 atomic_read(&call->sequence),
159 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
160
Florian Westphalc03ae532015-02-28 11:51:36 +0100161 stop = false;
David Howells17926a72007-04-26 15:48:28 -0700162 resend = 0;
163 resend_at = 0;
164
165 for (loop = call->acks_tail;
166 loop != call->acks_head || stop;
167 loop = (loop + 1) & (call->acks_winsz - 1)
168 ) {
169 p_txb = call->acks_window + loop;
170 smp_read_barrier_depends();
171 if (*p_txb & 1)
172 continue;
173
174 txb = (struct sk_buff *) *p_txb;
175 sp = rxrpc_skb(txb);
176
177 if (sp->need_resend) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000178 sp->need_resend = false;
David Howells17926a72007-04-26 15:48:28 -0700179
180 /* each Tx packet has a new serial number */
David Howells0d12f8a2016-03-04 15:53:46 +0000181 sp->hdr.serial = atomic_inc_return(&call->conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700182
David Howells0d12f8a2016-03-04 15:53:46 +0000183 whdr = (struct rxrpc_wire_header *)txb->head;
184 whdr->serial = htonl(sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700185
186 _proto("Tx DATA %%%u { #%d }",
David Howells0d12f8a2016-03-04 15:53:46 +0000187 sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700188 if (rxrpc_send_packet(call->conn->trans, txb) < 0) {
Florian Westphalc03ae532015-02-28 11:51:36 +0100189 stop = true;
David Howells17926a72007-04-26 15:48:28 -0700190 sp->resend_at = jiffies + 3;
191 } else {
192 sp->resend_at =
Florian Westphal765dd3b2015-02-28 11:51:37 +0100193 jiffies + rxrpc_resend_timeout;
David Howells17926a72007-04-26 15:48:28 -0700194 }
195 }
196
197 if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000198 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700199 resend |= 1;
200 } else if (resend & 2) {
201 if (time_before(sp->resend_at, resend_at))
202 resend_at = sp->resend_at;
203 } else {
204 resend_at = sp->resend_at;
205 resend |= 2;
206 }
207 }
208
209 rxrpc_set_resend(call, resend, resend_at);
210 _leave("");
211}
212
213/*
214 * handle resend timer expiry
215 */
216static void rxrpc_resend_timer(struct rxrpc_call *call)
217{
218 struct rxrpc_skb_priv *sp;
219 struct sk_buff *txb;
220 unsigned long *p_txb, resend_at;
221 int loop;
222 u8 resend;
223
224 _enter("%d,%d,%d",
225 call->acks_tail, call->acks_unacked, call->acks_head);
226
David Howells3b5bac22010-08-04 02:34:17 +0000227 if (call->state >= RXRPC_CALL_COMPLETE)
228 return;
229
David Howells17926a72007-04-26 15:48:28 -0700230 resend = 0;
231 resend_at = 0;
232
233 for (loop = call->acks_unacked;
234 loop != call->acks_head;
235 loop = (loop + 1) & (call->acks_winsz - 1)
236 ) {
237 p_txb = call->acks_window + loop;
238 smp_read_barrier_depends();
239 txb = (struct sk_buff *) (*p_txb & ~1);
240 sp = rxrpc_skb(txb);
241
242 ASSERT(!(*p_txb & 1));
243
244 if (sp->need_resend) {
245 ;
246 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000247 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700248 resend |= 1;
249 } else if (resend & 2) {
250 if (time_before(sp->resend_at, resend_at))
251 resend_at = sp->resend_at;
252 } else {
253 resend_at = sp->resend_at;
254 resend |= 2;
255 }
256 }
257
258 rxrpc_set_resend(call, resend, resend_at);
259 _leave("");
260}
261
262/*
263 * process soft ACKs of our transmitted packets
264 * - these indicate packets the peer has or has not received, but hasn't yet
265 * given to the consumer, and so can still be discarded and re-requested
266 */
267static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
268 struct rxrpc_ackpacket *ack,
269 struct sk_buff *skb)
270{
271 struct rxrpc_skb_priv *sp;
272 struct sk_buff *txb;
273 unsigned long *p_txb, resend_at;
274 int loop;
275 u8 sacks[RXRPC_MAXACKS], resend;
276
277 _enter("{%d,%d},{%d},",
278 call->acks_hard,
279 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
280 ack->nAcks);
281
282 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
283 goto protocol_error;
284
285 resend = 0;
286 resend_at = 0;
287 for (loop = 0; loop < ack->nAcks; loop++) {
288 p_txb = call->acks_window;
289 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
290 smp_read_barrier_depends();
291 txb = (struct sk_buff *) (*p_txb & ~1);
292 sp = rxrpc_skb(txb);
293
294 switch (sacks[loop]) {
295 case RXRPC_ACK_TYPE_ACK:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000296 sp->need_resend = false;
David Howells17926a72007-04-26 15:48:28 -0700297 *p_txb |= 1;
298 break;
299 case RXRPC_ACK_TYPE_NACK:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000300 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700301 *p_txb &= ~1;
302 resend = 1;
303 break;
304 default:
305 _debug("Unsupported ACK type %d", sacks[loop]);
306 goto protocol_error;
307 }
308 }
309
310 smp_mb();
311 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
312
313 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
314 * have been received or processed yet by the far end */
315 for (loop = call->acks_unacked;
316 loop != call->acks_head;
317 loop = (loop + 1) & (call->acks_winsz - 1)
318 ) {
319 p_txb = call->acks_window + loop;
320 smp_read_barrier_depends();
321 txb = (struct sk_buff *) (*p_txb & ~1);
322 sp = rxrpc_skb(txb);
323
324 if (*p_txb & 1) {
325 /* packet must have been discarded */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000326 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700327 *p_txb &= ~1;
328 resend |= 1;
329 } else if (sp->need_resend) {
330 ;
331 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000332 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700333 resend |= 1;
334 } else if (resend & 2) {
335 if (time_before(sp->resend_at, resend_at))
336 resend_at = sp->resend_at;
337 } else {
338 resend_at = sp->resend_at;
339 resend |= 2;
340 }
341 }
342
343 rxrpc_set_resend(call, resend, resend_at);
344 _leave(" = 0");
345 return 0;
346
347protocol_error:
348 _leave(" = -EPROTO");
349 return -EPROTO;
350}
351
352/*
353 * discard hard-ACK'd packets from the Tx window
354 */
355static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
356{
David Howells17926a72007-04-26 15:48:28 -0700357 unsigned long _skb;
358 int tail = call->acks_tail, old_tail;
359 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
360
David Howells8f7e6e72016-04-07 17:23:09 +0100361 _enter("{%u,%u},%u", call->acks_hard, win, hard);
David Howells17926a72007-04-26 15:48:28 -0700362
363 ASSERTCMP(hard - call->acks_hard, <=, win);
364
365 while (call->acks_hard < hard) {
366 smp_read_barrier_depends();
367 _skb = call->acks_window[tail] & ~1;
David Howells17926a72007-04-26 15:48:28 -0700368 rxrpc_free_skb((struct sk_buff *) _skb);
369 old_tail = tail;
370 tail = (tail + 1) & (call->acks_winsz - 1);
371 call->acks_tail = tail;
372 if (call->acks_unacked == old_tail)
373 call->acks_unacked = tail;
374 call->acks_hard++;
375 }
376
377 wake_up(&call->tx_waitq);
378}
379
380/*
381 * clear the Tx window in the event of a failure
382 */
383static void rxrpc_clear_tx_window(struct rxrpc_call *call)
384{
385 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
386}
387
388/*
389 * drain the out of sequence received packet queue into the packet Rx queue
390 */
391static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
392{
393 struct rxrpc_skb_priv *sp;
394 struct sk_buff *skb;
395 bool terminal;
396 int ret;
397
398 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
399
400 spin_lock_bh(&call->lock);
401
402 ret = -ECONNRESET;
403 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
404 goto socket_unavailable;
405
406 skb = skb_dequeue(&call->rx_oos_queue);
407 if (skb) {
408 sp = rxrpc_skb(skb);
409
410 _debug("drain OOS packet %d [%d]",
David Howells0d12f8a2016-03-04 15:53:46 +0000411 sp->hdr.seq, call->rx_first_oos);
David Howells17926a72007-04-26 15:48:28 -0700412
David Howells0d12f8a2016-03-04 15:53:46 +0000413 if (sp->hdr.seq != call->rx_first_oos) {
David Howells17926a72007-04-26 15:48:28 -0700414 skb_queue_head(&call->rx_oos_queue, skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000415 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
David Howells17926a72007-04-26 15:48:28 -0700416 _debug("requeue %p {%u}", skb, call->rx_first_oos);
417 } else {
418 skb->mark = RXRPC_SKB_MARK_DATA;
419 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
420 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
421 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
422 BUG_ON(ret < 0);
423 _debug("drain #%u", call->rx_data_post);
424 call->rx_data_post++;
425
426 /* find out what the next packet is */
427 skb = skb_peek(&call->rx_oos_queue);
428 if (skb)
David Howells0d12f8a2016-03-04 15:53:46 +0000429 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
David Howells17926a72007-04-26 15:48:28 -0700430 else
431 call->rx_first_oos = 0;
432 _debug("peek %p {%u}", skb, call->rx_first_oos);
433 }
434 }
435
436 ret = 0;
437socket_unavailable:
438 spin_unlock_bh(&call->lock);
439 _leave(" = %d", ret);
440 return ret;
441}
442
443/*
444 * insert an out of sequence packet into the buffer
445 */
446static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
447 struct sk_buff *skb)
448{
449 struct rxrpc_skb_priv *sp, *psp;
450 struct sk_buff *p;
451 u32 seq;
452
453 sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000454 seq = sp->hdr.seq;
David Howells17926a72007-04-26 15:48:28 -0700455 _enter(",,{%u}", seq);
456
457 skb->destructor = rxrpc_packet_destructor;
458 ASSERTCMP(sp->call, ==, NULL);
459 sp->call = call;
460 rxrpc_get_call(call);
461
462 /* insert into the buffer in sequence order */
463 spin_lock_bh(&call->lock);
464
465 skb_queue_walk(&call->rx_oos_queue, p) {
466 psp = rxrpc_skb(p);
David Howells0d12f8a2016-03-04 15:53:46 +0000467 if (psp->hdr.seq > seq) {
468 _debug("insert oos #%u before #%u", seq, psp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700469 skb_insert(p, skb, &call->rx_oos_queue);
470 goto inserted;
471 }
472 }
473
474 _debug("append oos #%u", seq);
475 skb_queue_tail(&call->rx_oos_queue, skb);
476inserted:
477
478 /* we might now have a new front to the queue */
479 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
480 call->rx_first_oos = seq;
481
482 read_lock(&call->state_lock);
483 if (call->state < RXRPC_CALL_COMPLETE &&
484 call->rx_data_post == call->rx_first_oos) {
485 _debug("drain rx oos now");
David Howells4c198ad2016-03-04 15:53:46 +0000486 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700487 }
488 read_unlock(&call->state_lock);
489
490 spin_unlock_bh(&call->lock);
491 _leave(" [stored #%u]", call->rx_first_oos);
492}
493
494/*
495 * clear the Tx window on final ACK reception
496 */
497static void rxrpc_zap_tx_window(struct rxrpc_call *call)
498{
499 struct rxrpc_skb_priv *sp;
500 struct sk_buff *skb;
501 unsigned long _skb, *acks_window;
David Howells4e36a952009-09-16 00:01:13 -0700502 u8 winsz = call->acks_winsz;
David Howells17926a72007-04-26 15:48:28 -0700503 int tail;
504
505 acks_window = call->acks_window;
506 call->acks_window = NULL;
507
508 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
509 tail = call->acks_tail;
510 smp_read_barrier_depends();
511 _skb = acks_window[tail] & ~1;
512 smp_mb();
513 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
514
515 skb = (struct sk_buff *) _skb;
516 sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000517 _debug("+++ clear Tx %u", sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700518 rxrpc_free_skb(skb);
519 }
520
521 kfree(acks_window);
522}
523
524/*
David Howells224711d2007-05-04 12:41:11 -0700525 * process the extra information that may be appended to an ACK packet
526 */
527static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
Eric Dumazet95c96172012-04-15 05:58:06 +0000528 unsigned int latest, int nAcks)
David Howells224711d2007-05-04 12:41:11 -0700529{
530 struct rxrpc_ackinfo ackinfo;
531 struct rxrpc_peer *peer;
Eric Dumazet95c96172012-04-15 05:58:06 +0000532 unsigned int mtu;
David Howells224711d2007-05-04 12:41:11 -0700533
534 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
535 _leave(" [no ackinfo]");
536 return;
537 }
538
539 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
540 latest,
541 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
542 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
543
544 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
545
546 peer = call->conn->trans->peer;
547 if (mtu < peer->maxdata) {
548 spin_lock_bh(&peer->lock);
549 peer->maxdata = mtu;
550 peer->mtu = mtu + peer->hdrsize;
551 spin_unlock_bh(&peer->lock);
552 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
553 }
554}
555
556/*
David Howells17926a72007-04-26 15:48:28 -0700557 * process packets in the reception queue
558 */
559static int rxrpc_process_rx_queue(struct rxrpc_call *call,
560 u32 *_abort_code)
561{
562 struct rxrpc_ackpacket ack;
563 struct rxrpc_skb_priv *sp;
564 struct sk_buff *skb;
565 bool post_ACK;
566 int latest;
567 u32 hard, tx;
568
569 _enter("");
570
571process_further:
572 skb = skb_dequeue(&call->rx_queue);
573 if (!skb)
574 return -EAGAIN;
575
576 _net("deferred skb %p", skb);
577
578 sp = rxrpc_skb(skb);
579
580 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
581
582 post_ACK = false;
583
584 switch (sp->hdr.type) {
585 /* data packets that wind up here have been received out of
586 * order, need security processing or are jumbo packets */
587 case RXRPC_PACKET_TYPE_DATA:
David Howells0d12f8a2016-03-04 15:53:46 +0000588 _proto("OOSQ DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700589
590 /* secured packets must be verified and possibly decrypted */
591 if (rxrpc_verify_packet(call, skb, _abort_code) < 0)
592 goto protocol_error;
593
594 rxrpc_insert_oos_packet(call, skb);
595 goto process_further;
596
597 /* partial ACK to process */
598 case RXRPC_PACKET_TYPE_ACK:
599 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
600 _debug("extraction failure");
601 goto protocol_error;
602 }
603 if (!skb_pull(skb, sizeof(ack)))
604 BUG();
605
David Howells0d12f8a2016-03-04 15:53:46 +0000606 latest = sp->hdr.serial;
David Howells17926a72007-04-26 15:48:28 -0700607 hard = ntohl(ack.firstPacket);
608 tx = atomic_read(&call->sequence);
609
610 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
611 latest,
612 ntohs(ack.maxSkew),
613 hard,
614 ntohl(ack.previousPacket),
615 ntohl(ack.serial),
Dan Carpenter08d4d212014-01-20 13:28:59 +0300616 rxrpc_acks(ack.reason),
David Howells17926a72007-04-26 15:48:28 -0700617 ack.nAcks);
618
David Howells224711d2007-05-04 12:41:11 -0700619 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
620
David Howells17926a72007-04-26 15:48:28 -0700621 if (ack.reason == RXRPC_ACK_PING) {
622 _proto("Rx ACK %%%u PING Request", latest);
623 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
624 sp->hdr.serial, true);
625 }
626
627 /* discard any out-of-order or duplicate ACKs */
628 if (latest - call->acks_latest <= 0) {
629 _debug("discard ACK %d <= %d",
630 latest, call->acks_latest);
631 goto discard;
632 }
633 call->acks_latest = latest;
634
635 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
636 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
637 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
638 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
639 goto discard;
640
641 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
642
643 if (hard > 0) {
644 if (hard - 1 > tx) {
645 _debug("hard-ACK'd packet %d not transmitted"
646 " (%d top)",
647 hard - 1, tx);
648 goto protocol_error;
649 }
650
651 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
652 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
David Howells33c40e22015-11-24 14:41:59 +0000653 hard > tx) {
654 call->acks_hard = tx;
David Howells17926a72007-04-26 15:48:28 -0700655 goto all_acked;
David Howells33c40e22015-11-24 14:41:59 +0000656 }
David Howells17926a72007-04-26 15:48:28 -0700657
658 smp_rmb();
659 rxrpc_rotate_tx_window(call, hard - 1);
660 }
661
662 if (ack.nAcks > 0) {
663 if (hard - 1 + ack.nAcks > tx) {
664 _debug("soft-ACK'd packet %d+%d not"
665 " transmitted (%d top)",
666 hard - 1, ack.nAcks, tx);
667 goto protocol_error;
668 }
669
670 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
671 goto protocol_error;
672 }
673 goto discard;
674
675 /* complete ACK to process */
676 case RXRPC_PACKET_TYPE_ACKALL:
677 goto all_acked;
678
679 /* abort and busy are handled elsewhere */
680 case RXRPC_PACKET_TYPE_BUSY:
681 case RXRPC_PACKET_TYPE_ABORT:
682 BUG();
683
684 /* connection level events - also handled elsewhere */
685 case RXRPC_PACKET_TYPE_CHALLENGE:
686 case RXRPC_PACKET_TYPE_RESPONSE:
687 case RXRPC_PACKET_TYPE_DEBUG:
688 BUG();
689 }
690
691 /* if we've had a hard ACK that covers all the packets we've sent, then
692 * that ends that phase of the operation */
693all_acked:
694 write_lock_bh(&call->state_lock);
695 _debug("ack all %d", call->state);
696
697 switch (call->state) {
698 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
699 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
700 break;
701 case RXRPC_CALL_SERVER_AWAIT_ACK:
702 _debug("srv complete");
703 call->state = RXRPC_CALL_COMPLETE;
704 post_ACK = true;
705 break;
706 case RXRPC_CALL_CLIENT_SEND_REQUEST:
707 case RXRPC_CALL_SERVER_RECV_REQUEST:
708 goto protocol_error_unlock; /* can't occur yet */
709 default:
710 write_unlock_bh(&call->state_lock);
711 goto discard; /* assume packet left over from earlier phase */
712 }
713
714 write_unlock_bh(&call->state_lock);
715
716 /* if all the packets we sent are hard-ACK'd, then we can discard
717 * whatever we've got left */
718 _debug("clear Tx %d",
719 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
720
721 del_timer_sync(&call->resend_timer);
722 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells4c198ad2016-03-04 15:53:46 +0000723 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700724
725 if (call->acks_window)
726 rxrpc_zap_tx_window(call);
727
728 if (post_ACK) {
729 /* post the final ACK message for userspace to pick up */
730 _debug("post ACK");
731 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
732 sp->call = call;
733 rxrpc_get_call(call);
734 spin_lock_bh(&call->lock);
735 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
736 BUG();
737 spin_unlock_bh(&call->lock);
738 goto process_further;
739 }
740
741discard:
742 rxrpc_free_skb(skb);
743 goto process_further;
744
745protocol_error_unlock:
746 write_unlock_bh(&call->state_lock);
747protocol_error:
748 rxrpc_free_skb(skb);
749 _leave(" = -EPROTO");
750 return -EPROTO;
751}
752
753/*
754 * post a message to the socket Rx queue for recvmsg() to pick up
755 */
756static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
757 bool fatal)
758{
759 struct rxrpc_skb_priv *sp;
760 struct sk_buff *skb;
761 int ret;
762
763 _enter("{%d,%lx},%u,%u,%d",
764 call->debug_id, call->flags, mark, error, fatal);
765
766 /* remove timers and things for fatal messages */
767 if (fatal) {
768 del_timer_sync(&call->resend_timer);
769 del_timer_sync(&call->ack_timer);
770 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
771 }
772
773 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
774 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
775 _leave("[no userid]");
776 return 0;
777 }
778
779 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
780 skb = alloc_skb(0, GFP_NOFS);
781 if (!skb)
782 return -ENOMEM;
783
784 rxrpc_new_skb(skb);
785
786 skb->mark = mark;
787
788 sp = rxrpc_skb(skb);
789 memset(sp, 0, sizeof(*sp));
790 sp->error = error;
791 sp->call = call;
792 rxrpc_get_call(call);
793
794 spin_lock_bh(&call->lock);
795 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
796 spin_unlock_bh(&call->lock);
Julia Lawall163e3cb2008-02-17 18:42:03 -0800797 BUG_ON(ret < 0);
David Howells17926a72007-04-26 15:48:28 -0700798 }
799
800 return 0;
801}
802
803/*
804 * handle background processing of incoming call packets and ACK / abort
805 * generation
806 */
807void rxrpc_process_call(struct work_struct *work)
808{
809 struct rxrpc_call *call =
810 container_of(work, struct rxrpc_call, processor);
David Howells0d12f8a2016-03-04 15:53:46 +0000811 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700812 struct rxrpc_ackpacket ack;
813 struct rxrpc_ackinfo ackinfo;
David Howells17926a72007-04-26 15:48:28 -0700814 struct msghdr msg;
815 struct kvec iov[5];
David Howells5b8848d2016-03-04 15:53:46 +0000816 enum rxrpc_call_event genbit;
David Howells17926a72007-04-26 15:48:28 -0700817 unsigned long bits;
David Howells224711d2007-05-04 12:41:11 -0700818 __be32 data, pad;
David Howells17926a72007-04-26 15:48:28 -0700819 size_t len;
David Howells5b8848d2016-03-04 15:53:46 +0000820 int loop, nbit, ioc, ret, mtu;
David Howells0d12f8a2016-03-04 15:53:46 +0000821 u32 serial, abort_code = RX_PROTOCOL_ERROR;
David Howells17926a72007-04-26 15:48:28 -0700822 u8 *acks = NULL;
823
824 //printk("\n--------------------\n");
825 _enter("{%d,%s,%lx} [%lu]",
826 call->debug_id, rxrpc_call_states[call->state], call->events,
827 (jiffies - call->creation_jif) / (HZ / 10));
828
829 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY, &call->flags)) {
830 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
831 return;
832 }
833
834 /* there's a good chance we're going to have to send a message, so set
835 * one up in advance */
836 msg.msg_name = &call->conn->trans->peer->srx.transport.sin;
837 msg.msg_namelen = sizeof(call->conn->trans->peer->srx.transport.sin);
838 msg.msg_control = NULL;
839 msg.msg_controllen = 0;
840 msg.msg_flags = 0;
841
David Howells0d12f8a2016-03-04 15:53:46 +0000842 whdr.epoch = htonl(call->conn->epoch);
843 whdr.cid = htonl(call->cid);
844 whdr.callNumber = htonl(call->call_id);
845 whdr.seq = 0;
846 whdr.type = RXRPC_PACKET_TYPE_ACK;
847 whdr.flags = call->conn->out_clientflag;
848 whdr.userStatus = 0;
849 whdr.securityIndex = call->conn->security_ix;
850 whdr._rsvd = 0;
851 whdr.serviceId = htons(call->service_id);
David Howells17926a72007-04-26 15:48:28 -0700852
853 memset(iov, 0, sizeof(iov));
David Howells0d12f8a2016-03-04 15:53:46 +0000854 iov[0].iov_base = &whdr;
855 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700856
857 /* deal with events of a final nature */
David Howells4c198ad2016-03-04 15:53:46 +0000858 if (test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700859 rxrpc_release_call(call);
David Howells4c198ad2016-03-04 15:53:46 +0000860 clear_bit(RXRPC_CALL_EV_RELEASE, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700861 }
862
David Howells4c198ad2016-03-04 15:53:46 +0000863 if (test_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700864 int error;
865
David Howells4c198ad2016-03-04 15:53:46 +0000866 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
867 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
868 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700869
870 error = call->conn->trans->peer->net_error;
871 _debug("post net error %d", error);
872
873 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NET_ERROR,
874 error, true) < 0)
875 goto no_mem;
David Howells4c198ad2016-03-04 15:53:46 +0000876 clear_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700877 goto kill_ACKs;
878 }
879
David Howells4c198ad2016-03-04 15:53:46 +0000880 if (test_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700881 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
882
David Howells4c198ad2016-03-04 15:53:46 +0000883 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
884 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700885
886 _debug("post conn abort");
887
888 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
889 call->conn->error, true) < 0)
890 goto no_mem;
David Howells4c198ad2016-03-04 15:53:46 +0000891 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700892 goto kill_ACKs;
893 }
894
David Howells4c198ad2016-03-04 15:53:46 +0000895 if (test_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events)) {
David Howells0d12f8a2016-03-04 15:53:46 +0000896 whdr.type = RXRPC_PACKET_TYPE_BUSY;
David Howells4c198ad2016-03-04 15:53:46 +0000897 genbit = RXRPC_CALL_EV_REJECT_BUSY;
David Howells17926a72007-04-26 15:48:28 -0700898 goto send_message;
899 }
900
David Howells4c198ad2016-03-04 15:53:46 +0000901 if (test_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700902 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
903
904 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
905 ECONNABORTED, true) < 0)
906 goto no_mem;
David Howells0d12f8a2016-03-04 15:53:46 +0000907 whdr.type = RXRPC_PACKET_TYPE_ABORT;
David Howellsdc44b3a2016-04-07 17:23:30 +0100908 data = htonl(call->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700909 iov[1].iov_base = &data;
910 iov[1].iov_len = sizeof(data);
David Howells4c198ad2016-03-04 15:53:46 +0000911 genbit = RXRPC_CALL_EV_ABORT;
David Howells17926a72007-04-26 15:48:28 -0700912 goto send_message;
913 }
914
David Howells4c198ad2016-03-04 15:53:46 +0000915 if (test_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events)) {
916 genbit = RXRPC_CALL_EV_ACK_FINAL;
David Howells224711d2007-05-04 12:41:11 -0700917
918 ack.bufferSpace = htons(8);
919 ack.maxSkew = 0;
920 ack.serial = 0;
921 ack.reason = RXRPC_ACK_IDLE;
922 ack.nAcks = 0;
923 call->ackr_reason = 0;
924
925 spin_lock_bh(&call->lock);
David Howells0d12f8a2016-03-04 15:53:46 +0000926 ack.serial = htonl(call->ackr_serial);
927 ack.previousPacket = htonl(call->ackr_prev_seq);
928 ack.firstPacket = htonl(call->rx_data_eaten + 1);
David Howells224711d2007-05-04 12:41:11 -0700929 spin_unlock_bh(&call->lock);
930
931 pad = 0;
932
933 iov[1].iov_base = &ack;
934 iov[1].iov_len = sizeof(ack);
935 iov[2].iov_base = &pad;
936 iov[2].iov_len = 3;
937 iov[3].iov_base = &ackinfo;
938 iov[3].iov_len = sizeof(ackinfo);
939 goto send_ACK;
David Howells17926a72007-04-26 15:48:28 -0700940 }
941
David Howells4c198ad2016-03-04 15:53:46 +0000942 if (call->events & ((1 << RXRPC_CALL_EV_RCVD_BUSY) |
943 (1 << RXRPC_CALL_EV_RCVD_ABORT))
David Howells17926a72007-04-26 15:48:28 -0700944 ) {
945 u32 mark;
946
David Howells4c198ad2016-03-04 15:53:46 +0000947 if (test_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700948 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
949 else
950 mark = RXRPC_SKB_MARK_BUSY;
951
952 _debug("post abort/busy");
953 rxrpc_clear_tx_window(call);
954 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
955 goto no_mem;
956
David Howells4c198ad2016-03-04 15:53:46 +0000957 clear_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
958 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700959 goto kill_ACKs;
960 }
961
David Howells4c198ad2016-03-04 15:53:46 +0000962 if (test_and_clear_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700963 _debug("do implicit ackall");
964 rxrpc_clear_tx_window(call);
965 }
966
David Howells4c198ad2016-03-04 15:53:46 +0000967 if (test_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700968 write_lock_bh(&call->state_lock);
969 if (call->state <= RXRPC_CALL_COMPLETE) {
970 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100971 call->local_abort = RX_CALL_TIMEOUT;
David Howells4c198ad2016-03-04 15:53:46 +0000972 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700973 }
974 write_unlock_bh(&call->state_lock);
975
976 _debug("post timeout");
977 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
978 ETIME, true) < 0)
979 goto no_mem;
980
David Howells4c198ad2016-03-04 15:53:46 +0000981 clear_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700982 goto kill_ACKs;
983 }
984
985 /* deal with assorted inbound messages */
986 if (!skb_queue_empty(&call->rx_queue)) {
987 switch (rxrpc_process_rx_queue(call, &abort_code)) {
988 case 0:
989 case -EAGAIN:
990 break;
991 case -ENOMEM:
992 goto no_mem;
993 case -EKEYEXPIRED:
994 case -EKEYREJECTED:
995 case -EPROTO:
996 rxrpc_abort_call(call, abort_code);
997 goto kill_ACKs;
998 }
999 }
1000
1001 /* handle resending */
David Howells4c198ad2016-03-04 15:53:46 +00001002 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001003 rxrpc_resend_timer(call);
David Howells4c198ad2016-03-04 15:53:46 +00001004 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001005 rxrpc_resend(call);
1006
1007 /* consider sending an ordinary ACK */
David Howells4c198ad2016-03-04 15:53:46 +00001008 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001009 _debug("send ACK: window: %d - %d { %lx }",
1010 call->rx_data_eaten, call->ackr_win_top,
1011 call->ackr_window[0]);
1012
1013 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1014 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1015 /* ACK by sending reply DATA packet in this state */
David Howells4c198ad2016-03-04 15:53:46 +00001016 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001017 goto maybe_reschedule;
1018 }
1019
David Howells4c198ad2016-03-04 15:53:46 +00001020 genbit = RXRPC_CALL_EV_ACK;
David Howells17926a72007-04-26 15:48:28 -07001021
1022 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1023 GFP_NOFS);
1024 if (!acks)
1025 goto no_mem;
1026
1027 //hdr.flags = RXRPC_SLOW_START_OK;
1028 ack.bufferSpace = htons(8);
1029 ack.maxSkew = 0;
David Howells17926a72007-04-26 15:48:28 -07001030
David Howells17926a72007-04-26 15:48:28 -07001031 spin_lock_bh(&call->lock);
David Howells0d12f8a2016-03-04 15:53:46 +00001032 ack.reason = call->ackr_reason;
1033 ack.serial = htonl(call->ackr_serial);
1034 ack.previousPacket = htonl(call->ackr_prev_seq);
David Howells17926a72007-04-26 15:48:28 -07001035 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1036
1037 ack.nAcks = 0;
1038 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1039 nbit = loop * BITS_PER_LONG;
1040 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1041 ) {
1042 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1043 if (bits & 1) {
1044 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1045 ack.nAcks = nbit + 1;
1046 }
1047 nbit++;
1048 }
1049 }
1050 call->ackr_reason = 0;
1051 spin_unlock_bh(&call->lock);
1052
1053 pad = 0;
1054
1055 iov[1].iov_base = &ack;
1056 iov[1].iov_len = sizeof(ack);
1057 iov[2].iov_base = acks;
1058 iov[2].iov_len = ack.nAcks;
1059 iov[3].iov_base = &pad;
1060 iov[3].iov_len = 3;
1061 iov[4].iov_base = &ackinfo;
1062 iov[4].iov_len = sizeof(ackinfo);
1063
1064 switch (ack.reason) {
1065 case RXRPC_ACK_REQUESTED:
1066 case RXRPC_ACK_DUPLICATE:
1067 case RXRPC_ACK_OUT_OF_SEQUENCE:
1068 case RXRPC_ACK_EXCEEDS_WINDOW:
1069 case RXRPC_ACK_NOSPACE:
1070 case RXRPC_ACK_PING:
1071 case RXRPC_ACK_PING_RESPONSE:
1072 goto send_ACK_with_skew;
1073 case RXRPC_ACK_DELAY:
1074 case RXRPC_ACK_IDLE:
1075 goto send_ACK;
1076 }
1077 }
1078
1079 /* handle completion of security negotiations on an incoming
1080 * connection */
David Howells4c198ad2016-03-04 15:53:46 +00001081 if (test_and_clear_bit(RXRPC_CALL_EV_SECURED, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001082 _debug("secured");
1083 spin_lock_bh(&call->lock);
1084
1085 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1086 _debug("securing");
1087 write_lock(&call->conn->lock);
1088 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +00001089 !test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001090 _debug("not released");
1091 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1092 list_move_tail(&call->accept_link,
1093 &call->socket->acceptq);
1094 }
1095 write_unlock(&call->conn->lock);
1096 read_lock(&call->state_lock);
1097 if (call->state < RXRPC_CALL_COMPLETE)
David Howells4c198ad2016-03-04 15:53:46 +00001098 set_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001099 read_unlock(&call->state_lock);
1100 }
1101
1102 spin_unlock_bh(&call->lock);
David Howells4c198ad2016-03-04 15:53:46 +00001103 if (!test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001104 goto maybe_reschedule;
1105 }
1106
1107 /* post a notification of an acceptable connection to the app */
David Howells4c198ad2016-03-04 15:53:46 +00001108 if (test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001109 _debug("post accept");
1110 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1111 0, false) < 0)
1112 goto no_mem;
David Howells4c198ad2016-03-04 15:53:46 +00001113 clear_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001114 goto maybe_reschedule;
1115 }
1116
1117 /* handle incoming call acceptance */
David Howells4c198ad2016-03-04 15:53:46 +00001118 if (test_and_clear_bit(RXRPC_CALL_EV_ACCEPTED, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001119 _debug("accepted");
1120 ASSERTCMP(call->rx_data_post, ==, 0);
1121 call->rx_data_post = 1;
1122 read_lock_bh(&call->state_lock);
1123 if (call->state < RXRPC_CALL_COMPLETE)
David Howells4c198ad2016-03-04 15:53:46 +00001124 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001125 read_unlock_bh(&call->state_lock);
1126 }
1127
1128 /* drain the out of sequence received packet queue into the packet Rx
1129 * queue */
David Howells4c198ad2016-03-04 15:53:46 +00001130 if (test_and_clear_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001131 while (call->rx_data_post == call->rx_first_oos)
1132 if (rxrpc_drain_rx_oos_queue(call) < 0)
1133 break;
1134 goto maybe_reschedule;
1135 }
1136
1137 /* other events may have been raised since we started checking */
1138 goto maybe_reschedule;
1139
1140send_ACK_with_skew:
1141 ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) -
1142 ntohl(ack.serial));
1143send_ACK:
David Howells224711d2007-05-04 12:41:11 -07001144 mtu = call->conn->trans->peer->if_mtu;
1145 mtu -= call->conn->trans->peer->hdrsize;
1146 ackinfo.maxMTU = htonl(mtu);
David Howells817913d2014-02-07 18:10:30 +00001147 ackinfo.rwind = htonl(rxrpc_rx_window_size);
David Howells224711d2007-05-04 12:41:11 -07001148
1149 /* permit the peer to send us jumbo packets if it wants to */
David Howells817913d2014-02-07 18:10:30 +00001150 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
1151 ackinfo.jumbo_max = htonl(rxrpc_rx_jumbo_max);
David Howells224711d2007-05-04 12:41:11 -07001152
David Howells0d12f8a2016-03-04 15:53:46 +00001153 serial = atomic_inc_return(&call->conn->serial);
1154 whdr.serial = htonl(serial);
David Howells17926a72007-04-26 15:48:28 -07001155 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
David Howells0d12f8a2016-03-04 15:53:46 +00001156 serial,
David Howells17926a72007-04-26 15:48:28 -07001157 ntohs(ack.maxSkew),
1158 ntohl(ack.firstPacket),
1159 ntohl(ack.previousPacket),
1160 ntohl(ack.serial),
Dan Carpenter08d4d212014-01-20 13:28:59 +03001161 rxrpc_acks(ack.reason),
David Howells17926a72007-04-26 15:48:28 -07001162 ack.nAcks);
1163
1164 del_timer_sync(&call->ack_timer);
1165 if (ack.nAcks > 0)
1166 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1167 goto send_message_2;
1168
1169send_message:
1170 _debug("send message");
1171
David Howells0d12f8a2016-03-04 15:53:46 +00001172 serial = atomic_inc_return(&call->conn->serial);
1173 whdr.serial = htonl(serial);
1174 _proto("Tx %s %%%u", rxrpc_pkts[whdr.type], serial);
David Howells17926a72007-04-26 15:48:28 -07001175send_message_2:
1176
1177 len = iov[0].iov_len;
1178 ioc = 1;
1179 if (iov[4].iov_len) {
1180 ioc = 5;
1181 len += iov[4].iov_len;
1182 len += iov[3].iov_len;
1183 len += iov[2].iov_len;
1184 len += iov[1].iov_len;
1185 } else if (iov[3].iov_len) {
1186 ioc = 4;
1187 len += iov[3].iov_len;
1188 len += iov[2].iov_len;
1189 len += iov[1].iov_len;
1190 } else if (iov[2].iov_len) {
1191 ioc = 3;
1192 len += iov[2].iov_len;
1193 len += iov[1].iov_len;
1194 } else if (iov[1].iov_len) {
1195 ioc = 2;
1196 len += iov[1].iov_len;
1197 }
1198
1199 ret = kernel_sendmsg(call->conn->trans->local->socket,
1200 &msg, iov, ioc, len);
1201 if (ret < 0) {
1202 _debug("sendmsg failed: %d", ret);
1203 read_lock_bh(&call->state_lock);
1204 if (call->state < RXRPC_CALL_DEAD)
David Howells651350d2007-04-26 15:50:17 -07001205 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001206 read_unlock_bh(&call->state_lock);
1207 goto error;
1208 }
1209
1210 switch (genbit) {
David Howells4c198ad2016-03-04 15:53:46 +00001211 case RXRPC_CALL_EV_ABORT:
David Howells17926a72007-04-26 15:48:28 -07001212 clear_bit(genbit, &call->events);
David Howells4c198ad2016-03-04 15:53:46 +00001213 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001214 goto kill_ACKs;
1215
David Howells4c198ad2016-03-04 15:53:46 +00001216 case RXRPC_CALL_EV_ACK_FINAL:
David Howells17926a72007-04-26 15:48:28 -07001217 write_lock_bh(&call->state_lock);
1218 if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK)
1219 call->state = RXRPC_CALL_COMPLETE;
1220 write_unlock_bh(&call->state_lock);
1221 goto kill_ACKs;
1222
1223 default:
1224 clear_bit(genbit, &call->events);
1225 switch (call->state) {
1226 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1227 case RXRPC_CALL_CLIENT_RECV_REPLY:
1228 case RXRPC_CALL_SERVER_RECV_REQUEST:
1229 case RXRPC_CALL_SERVER_ACK_REQUEST:
1230 _debug("start ACK timer");
1231 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
1232 call->ackr_serial, false);
1233 default:
1234 break;
1235 }
1236 goto maybe_reschedule;
1237 }
1238
1239kill_ACKs:
1240 del_timer_sync(&call->ack_timer);
David Howells4c198ad2016-03-04 15:53:46 +00001241 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001242 rxrpc_put_call(call);
David Howells4c198ad2016-03-04 15:53:46 +00001243 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001244
1245maybe_reschedule:
1246 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1247 read_lock_bh(&call->state_lock);
1248 if (call->state < RXRPC_CALL_DEAD)
David Howells651350d2007-04-26 15:50:17 -07001249 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001250 read_unlock_bh(&call->state_lock);
1251 }
1252
1253 /* don't leave aborted connections on the accept queue */
1254 if (call->state >= RXRPC_CALL_COMPLETE &&
1255 !list_empty(&call->accept_link)) {
1256 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
David Howells0d12f8a2016-03-04 15:53:46 +00001257 call, call->events, call->flags, call->conn->cid);
David Howells17926a72007-04-26 15:48:28 -07001258
1259 read_lock_bh(&call->state_lock);
1260 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +00001261 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells651350d2007-04-26 15:50:17 -07001262 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001263 read_unlock_bh(&call->state_lock);
1264 }
1265
1266error:
1267 clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags);
1268 kfree(acks);
1269
1270 /* because we don't want two CPUs both processing the work item for one
1271 * call at the same time, we use a flag to note when it's busy; however
1272 * this means there's a race between clearing the flag and setting the
1273 * work pending bit and the work item being processed again */
1274 if (call->events && !work_pending(&call->processor)) {
David Howells0d12f8a2016-03-04 15:53:46 +00001275 _debug("jumpstart %x", call->conn->cid);
David Howells651350d2007-04-26 15:50:17 -07001276 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001277 }
1278
1279 _leave("");
1280 return;
1281
1282no_mem:
1283 _debug("out of memory");
1284 goto maybe_reschedule;
1285}