]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/rxrpc/call_event.c
rxrpc: Calculate serial skew on packet reception
[mirror_ubuntu-artful-kernel.git] / net / rxrpc / call_event.c
1 /* 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/circ_buf.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include <linux/slab.h>
19 #include <linux/udp.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
23
24 /*
25 * propose an ACK be sent
26 */
27 void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
28 u16 skew, u32 serial, bool immediate)
29 {
30 unsigned long expiry;
31 s8 prior = rxrpc_ack_priority[ack_reason];
32
33 ASSERTCMP(prior, >, 0);
34
35 _enter("{%d},%s,%%%x,%u",
36 call->debug_id, rxrpc_acks(ack_reason), serial, immediate);
37
38 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
39 if (immediate)
40 goto cancel_timer;
41 return;
42 }
43
44 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
45 * numbers */
46 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
47 if (prior <= 4) {
48 call->ackr_skew = skew;
49 call->ackr_serial = serial;
50 }
51 if (immediate)
52 goto cancel_timer;
53 return;
54 }
55
56 call->ackr_reason = ack_reason;
57 call->ackr_serial = serial;
58
59 switch (ack_reason) {
60 case RXRPC_ACK_DELAY:
61 _debug("run delay timer");
62 expiry = rxrpc_soft_ack_delay;
63 goto run_timer;
64
65 case RXRPC_ACK_IDLE:
66 if (!immediate) {
67 _debug("run defer timer");
68 expiry = rxrpc_idle_ack_delay;
69 goto run_timer;
70 }
71 goto cancel_timer;
72
73 case RXRPC_ACK_REQUESTED:
74 expiry = rxrpc_requested_ack_delay;
75 if (!expiry)
76 goto cancel_timer;
77 if (!immediate || serial == 1) {
78 _debug("run defer timer");
79 goto run_timer;
80 }
81
82 default:
83 _debug("immediate ACK");
84 goto cancel_timer;
85 }
86
87 run_timer:
88 expiry += jiffies;
89 if (!timer_pending(&call->ack_timer) ||
90 time_after(call->ack_timer.expires, expiry))
91 mod_timer(&call->ack_timer, expiry);
92 return;
93
94 cancel_timer:
95 _debug("cancel timer %%%u", serial);
96 try_to_del_timer_sync(&call->ack_timer);
97 read_lock_bh(&call->state_lock);
98 if (call->state <= RXRPC_CALL_COMPLETE &&
99 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
100 rxrpc_queue_call(call);
101 read_unlock_bh(&call->state_lock);
102 }
103
104 /*
105 * propose an ACK be sent, locking the call structure
106 */
107 void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
108 u16 skew, u32 serial, bool immediate)
109 {
110 s8 prior = rxrpc_ack_priority[ack_reason];
111
112 if (prior > rxrpc_ack_priority[call->ackr_reason]) {
113 spin_lock_bh(&call->lock);
114 __rxrpc_propose_ACK(call, ack_reason, skew, serial, immediate);
115 spin_unlock_bh(&call->lock);
116 }
117 }
118
119 /*
120 * set the resend timer
121 */
122 static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend,
123 unsigned long resend_at)
124 {
125 read_lock_bh(&call->state_lock);
126 if (call->state >= RXRPC_CALL_COMPLETE)
127 resend = 0;
128
129 if (resend & 1) {
130 _debug("SET RESEND");
131 set_bit(RXRPC_CALL_EV_RESEND, &call->events);
132 }
133
134 if (resend & 2) {
135 _debug("MODIFY RESEND TIMER");
136 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
137 mod_timer(&call->resend_timer, resend_at);
138 } else {
139 _debug("KILL RESEND TIMER");
140 del_timer_sync(&call->resend_timer);
141 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
142 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
143 }
144 read_unlock_bh(&call->state_lock);
145 }
146
147 /*
148 * resend packets
149 */
150 static void rxrpc_resend(struct rxrpc_call *call)
151 {
152 struct rxrpc_wire_header *whdr;
153 struct rxrpc_skb_priv *sp;
154 struct sk_buff *txb;
155 unsigned long *p_txb, resend_at;
156 bool stop;
157 int loop;
158 u8 resend;
159
160 _enter("{%d,%d,%d,%d},",
161 call->acks_hard, call->acks_unacked,
162 atomic_read(&call->sequence),
163 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
164
165 stop = false;
166 resend = 0;
167 resend_at = 0;
168
169 for (loop = call->acks_tail;
170 loop != call->acks_head || stop;
171 loop = (loop + 1) & (call->acks_winsz - 1)
172 ) {
173 p_txb = call->acks_window + loop;
174 smp_read_barrier_depends();
175 if (*p_txb & 1)
176 continue;
177
178 txb = (struct sk_buff *) *p_txb;
179 sp = rxrpc_skb(txb);
180
181 if (sp->need_resend) {
182 sp->need_resend = false;
183
184 /* each Tx packet has a new serial number */
185 sp->hdr.serial = atomic_inc_return(&call->conn->serial);
186
187 whdr = (struct rxrpc_wire_header *)txb->head;
188 whdr->serial = htonl(sp->hdr.serial);
189
190 _proto("Tx DATA %%%u { #%d }",
191 sp->hdr.serial, sp->hdr.seq);
192 if (rxrpc_send_data_packet(call->conn, txb) < 0) {
193 stop = true;
194 sp->resend_at = jiffies + 3;
195 } else {
196 sp->resend_at =
197 jiffies + rxrpc_resend_timeout;
198 }
199 }
200
201 if (time_after_eq(jiffies + 1, sp->resend_at)) {
202 sp->need_resend = true;
203 resend |= 1;
204 } else if (resend & 2) {
205 if (time_before(sp->resend_at, resend_at))
206 resend_at = sp->resend_at;
207 } else {
208 resend_at = sp->resend_at;
209 resend |= 2;
210 }
211 }
212
213 rxrpc_set_resend(call, resend, resend_at);
214 _leave("");
215 }
216
217 /*
218 * handle resend timer expiry
219 */
220 static void rxrpc_resend_timer(struct rxrpc_call *call)
221 {
222 struct rxrpc_skb_priv *sp;
223 struct sk_buff *txb;
224 unsigned long *p_txb, resend_at;
225 int loop;
226 u8 resend;
227
228 _enter("%d,%d,%d",
229 call->acks_tail, call->acks_unacked, call->acks_head);
230
231 if (call->state >= RXRPC_CALL_COMPLETE)
232 return;
233
234 resend = 0;
235 resend_at = 0;
236
237 for (loop = call->acks_unacked;
238 loop != call->acks_head;
239 loop = (loop + 1) & (call->acks_winsz - 1)
240 ) {
241 p_txb = call->acks_window + loop;
242 smp_read_barrier_depends();
243 txb = (struct sk_buff *) (*p_txb & ~1);
244 sp = rxrpc_skb(txb);
245
246 ASSERT(!(*p_txb & 1));
247
248 if (sp->need_resend) {
249 ;
250 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
251 sp->need_resend = true;
252 resend |= 1;
253 } else if (resend & 2) {
254 if (time_before(sp->resend_at, resend_at))
255 resend_at = sp->resend_at;
256 } else {
257 resend_at = sp->resend_at;
258 resend |= 2;
259 }
260 }
261
262 rxrpc_set_resend(call, resend, resend_at);
263 _leave("");
264 }
265
266 /*
267 * process soft ACKs of our transmitted packets
268 * - these indicate packets the peer has or has not received, but hasn't yet
269 * given to the consumer, and so can still be discarded and re-requested
270 */
271 static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
272 struct rxrpc_ackpacket *ack,
273 struct sk_buff *skb)
274 {
275 struct rxrpc_skb_priv *sp;
276 struct sk_buff *txb;
277 unsigned long *p_txb, resend_at;
278 int loop;
279 u8 sacks[RXRPC_MAXACKS], resend;
280
281 _enter("{%d,%d},{%d},",
282 call->acks_hard,
283 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
284 ack->nAcks);
285
286 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
287 goto protocol_error;
288
289 resend = 0;
290 resend_at = 0;
291 for (loop = 0; loop < ack->nAcks; loop++) {
292 p_txb = call->acks_window;
293 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
294 smp_read_barrier_depends();
295 txb = (struct sk_buff *) (*p_txb & ~1);
296 sp = rxrpc_skb(txb);
297
298 switch (sacks[loop]) {
299 case RXRPC_ACK_TYPE_ACK:
300 sp->need_resend = false;
301 *p_txb |= 1;
302 break;
303 case RXRPC_ACK_TYPE_NACK:
304 sp->need_resend = true;
305 *p_txb &= ~1;
306 resend = 1;
307 break;
308 default:
309 _debug("Unsupported ACK type %d", sacks[loop]);
310 goto protocol_error;
311 }
312 }
313
314 smp_mb();
315 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
316
317 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
318 * have been received or processed yet by the far end */
319 for (loop = call->acks_unacked;
320 loop != call->acks_head;
321 loop = (loop + 1) & (call->acks_winsz - 1)
322 ) {
323 p_txb = call->acks_window + loop;
324 smp_read_barrier_depends();
325 txb = (struct sk_buff *) (*p_txb & ~1);
326 sp = rxrpc_skb(txb);
327
328 if (*p_txb & 1) {
329 /* packet must have been discarded */
330 sp->need_resend = true;
331 *p_txb &= ~1;
332 resend |= 1;
333 } else if (sp->need_resend) {
334 ;
335 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
336 sp->need_resend = true;
337 resend |= 1;
338 } else if (resend & 2) {
339 if (time_before(sp->resend_at, resend_at))
340 resend_at = sp->resend_at;
341 } else {
342 resend_at = sp->resend_at;
343 resend |= 2;
344 }
345 }
346
347 rxrpc_set_resend(call, resend, resend_at);
348 _leave(" = 0");
349 return 0;
350
351 protocol_error:
352 _leave(" = -EPROTO");
353 return -EPROTO;
354 }
355
356 /*
357 * discard hard-ACK'd packets from the Tx window
358 */
359 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
360 {
361 unsigned long _skb;
362 int tail = call->acks_tail, old_tail;
363 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
364
365 _enter("{%u,%u},%u", call->acks_hard, win, hard);
366
367 ASSERTCMP(hard - call->acks_hard, <=, win);
368
369 while (call->acks_hard < hard) {
370 smp_read_barrier_depends();
371 _skb = call->acks_window[tail] & ~1;
372 rxrpc_free_skb((struct sk_buff *) _skb);
373 old_tail = tail;
374 tail = (tail + 1) & (call->acks_winsz - 1);
375 call->acks_tail = tail;
376 if (call->acks_unacked == old_tail)
377 call->acks_unacked = tail;
378 call->acks_hard++;
379 }
380
381 wake_up(&call->tx_waitq);
382 }
383
384 /*
385 * clear the Tx window in the event of a failure
386 */
387 static void rxrpc_clear_tx_window(struct rxrpc_call *call)
388 {
389 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
390 }
391
392 /*
393 * drain the out of sequence received packet queue into the packet Rx queue
394 */
395 static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
396 {
397 struct rxrpc_skb_priv *sp;
398 struct sk_buff *skb;
399 bool terminal;
400 int ret;
401
402 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
403
404 spin_lock_bh(&call->lock);
405
406 ret = -ECONNRESET;
407 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
408 goto socket_unavailable;
409
410 skb = skb_dequeue(&call->rx_oos_queue);
411 if (skb) {
412 rxrpc_see_skb(skb);
413 sp = rxrpc_skb(skb);
414
415 _debug("drain OOS packet %d [%d]",
416 sp->hdr.seq, call->rx_first_oos);
417
418 if (sp->hdr.seq != call->rx_first_oos) {
419 skb_queue_head(&call->rx_oos_queue, skb);
420 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
421 _debug("requeue %p {%u}", skb, call->rx_first_oos);
422 } else {
423 skb->mark = RXRPC_SKB_MARK_DATA;
424 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
425 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
426 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
427 BUG_ON(ret < 0);
428 _debug("drain #%u", call->rx_data_post);
429 call->rx_data_post++;
430
431 /* find out what the next packet is */
432 skb = skb_peek(&call->rx_oos_queue);
433 rxrpc_see_skb(skb);
434 if (skb)
435 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
436 else
437 call->rx_first_oos = 0;
438 _debug("peek %p {%u}", skb, call->rx_first_oos);
439 }
440 }
441
442 ret = 0;
443 socket_unavailable:
444 spin_unlock_bh(&call->lock);
445 _leave(" = %d", ret);
446 return ret;
447 }
448
449 /*
450 * insert an out of sequence packet into the buffer
451 */
452 static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
453 struct sk_buff *skb)
454 {
455 struct rxrpc_skb_priv *sp, *psp;
456 struct sk_buff *p;
457 u32 seq;
458
459 sp = rxrpc_skb(skb);
460 seq = sp->hdr.seq;
461 _enter(",,{%u}", seq);
462
463 skb->destructor = rxrpc_packet_destructor;
464 ASSERTCMP(sp->call, ==, NULL);
465 sp->call = call;
466 rxrpc_get_call(call);
467 atomic_inc(&call->skb_count);
468
469 /* insert into the buffer in sequence order */
470 spin_lock_bh(&call->lock);
471
472 skb_queue_walk(&call->rx_oos_queue, p) {
473 psp = rxrpc_skb(p);
474 if (psp->hdr.seq > seq) {
475 _debug("insert oos #%u before #%u", seq, psp->hdr.seq);
476 skb_insert(p, skb, &call->rx_oos_queue);
477 goto inserted;
478 }
479 }
480
481 _debug("append oos #%u", seq);
482 skb_queue_tail(&call->rx_oos_queue, skb);
483 inserted:
484
485 /* we might now have a new front to the queue */
486 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
487 call->rx_first_oos = seq;
488
489 read_lock(&call->state_lock);
490 if (call->state < RXRPC_CALL_COMPLETE &&
491 call->rx_data_post == call->rx_first_oos) {
492 _debug("drain rx oos now");
493 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
494 }
495 read_unlock(&call->state_lock);
496
497 spin_unlock_bh(&call->lock);
498 _leave(" [stored #%u]", call->rx_first_oos);
499 }
500
501 /*
502 * clear the Tx window on final ACK reception
503 */
504 static void rxrpc_zap_tx_window(struct rxrpc_call *call)
505 {
506 struct rxrpc_skb_priv *sp;
507 struct sk_buff *skb;
508 unsigned long _skb, *acks_window;
509 u8 winsz = call->acks_winsz;
510 int tail;
511
512 acks_window = call->acks_window;
513 call->acks_window = NULL;
514
515 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
516 tail = call->acks_tail;
517 smp_read_barrier_depends();
518 _skb = acks_window[tail] & ~1;
519 smp_mb();
520 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
521
522 skb = (struct sk_buff *) _skb;
523 sp = rxrpc_skb(skb);
524 _debug("+++ clear Tx %u", sp->hdr.seq);
525 rxrpc_free_skb(skb);
526 }
527
528 kfree(acks_window);
529 }
530
531 /*
532 * process the extra information that may be appended to an ACK packet
533 */
534 static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
535 unsigned int latest, int nAcks)
536 {
537 struct rxrpc_ackinfo ackinfo;
538 struct rxrpc_peer *peer;
539 unsigned int mtu;
540
541 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
542 _leave(" [no ackinfo]");
543 return;
544 }
545
546 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
547 latest,
548 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
549 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
550
551 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
552
553 peer = call->conn->params.peer;
554 if (mtu < peer->maxdata) {
555 spin_lock_bh(&peer->lock);
556 peer->maxdata = mtu;
557 peer->mtu = mtu + peer->hdrsize;
558 spin_unlock_bh(&peer->lock);
559 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
560 }
561 }
562
563 /*
564 * process packets in the reception queue
565 */
566 static int rxrpc_process_rx_queue(struct rxrpc_call *call,
567 u32 *_abort_code)
568 {
569 struct rxrpc_ackpacket ack;
570 struct rxrpc_skb_priv *sp;
571 struct sk_buff *skb;
572 bool post_ACK;
573 int latest;
574 u32 hard, tx;
575
576 _enter("");
577
578 process_further:
579 skb = skb_dequeue(&call->rx_queue);
580 if (!skb)
581 return -EAGAIN;
582
583 rxrpc_see_skb(skb);
584 _net("deferred skb %p", skb);
585
586 sp = rxrpc_skb(skb);
587
588 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
589
590 post_ACK = false;
591
592 switch (sp->hdr.type) {
593 /* data packets that wind up here have been received out of
594 * order, need security processing or are jumbo packets */
595 case RXRPC_PACKET_TYPE_DATA:
596 _proto("OOSQ DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
597
598 /* secured packets must be verified and possibly decrypted */
599 if (call->conn->security->verify_packet(call, skb,
600 _abort_code) < 0)
601 goto protocol_error;
602
603 rxrpc_insert_oos_packet(call, skb);
604 goto process_further;
605
606 /* partial ACK to process */
607 case RXRPC_PACKET_TYPE_ACK:
608 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
609 _debug("extraction failure");
610 goto protocol_error;
611 }
612 if (!skb_pull(skb, sizeof(ack)))
613 BUG();
614
615 latest = sp->hdr.serial;
616 hard = ntohl(ack.firstPacket);
617 tx = atomic_read(&call->sequence);
618
619 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
620 latest,
621 ntohs(ack.maxSkew),
622 hard,
623 ntohl(ack.previousPacket),
624 ntohl(ack.serial),
625 rxrpc_acks(ack.reason),
626 ack.nAcks);
627
628 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
629
630 if (ack.reason == RXRPC_ACK_PING) {
631 _proto("Rx ACK %%%u PING Request", latest);
632 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
633 skb->priority, sp->hdr.serial, true);
634 }
635
636 /* discard any out-of-order or duplicate ACKs */
637 if (latest - call->acks_latest <= 0) {
638 _debug("discard ACK %d <= %d",
639 latest, call->acks_latest);
640 goto discard;
641 }
642 call->acks_latest = latest;
643
644 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
645 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
646 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
647 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
648 goto discard;
649
650 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
651
652 if (hard > 0) {
653 if (hard - 1 > tx) {
654 _debug("hard-ACK'd packet %d not transmitted"
655 " (%d top)",
656 hard - 1, tx);
657 goto protocol_error;
658 }
659
660 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
661 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
662 hard > tx) {
663 call->acks_hard = tx;
664 goto all_acked;
665 }
666
667 smp_rmb();
668 rxrpc_rotate_tx_window(call, hard - 1);
669 }
670
671 if (ack.nAcks > 0) {
672 if (hard - 1 + ack.nAcks > tx) {
673 _debug("soft-ACK'd packet %d+%d not"
674 " transmitted (%d top)",
675 hard - 1, ack.nAcks, tx);
676 goto protocol_error;
677 }
678
679 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
680 goto protocol_error;
681 }
682 goto discard;
683
684 /* complete ACK to process */
685 case RXRPC_PACKET_TYPE_ACKALL:
686 goto all_acked;
687
688 /* abort and busy are handled elsewhere */
689 case RXRPC_PACKET_TYPE_BUSY:
690 case RXRPC_PACKET_TYPE_ABORT:
691 BUG();
692
693 /* connection level events - also handled elsewhere */
694 case RXRPC_PACKET_TYPE_CHALLENGE:
695 case RXRPC_PACKET_TYPE_RESPONSE:
696 case RXRPC_PACKET_TYPE_DEBUG:
697 BUG();
698 }
699
700 /* if we've had a hard ACK that covers all the packets we've sent, then
701 * that ends that phase of the operation */
702 all_acked:
703 write_lock_bh(&call->state_lock);
704 _debug("ack all %d", call->state);
705
706 switch (call->state) {
707 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
708 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
709 break;
710 case RXRPC_CALL_SERVER_AWAIT_ACK:
711 _debug("srv complete");
712 call->state = RXRPC_CALL_COMPLETE;
713 post_ACK = true;
714 break;
715 case RXRPC_CALL_CLIENT_SEND_REQUEST:
716 case RXRPC_CALL_SERVER_RECV_REQUEST:
717 goto protocol_error_unlock; /* can't occur yet */
718 default:
719 write_unlock_bh(&call->state_lock);
720 goto discard; /* assume packet left over from earlier phase */
721 }
722
723 write_unlock_bh(&call->state_lock);
724
725 /* if all the packets we sent are hard-ACK'd, then we can discard
726 * whatever we've got left */
727 _debug("clear Tx %d",
728 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
729
730 del_timer_sync(&call->resend_timer);
731 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
732 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
733
734 if (call->acks_window)
735 rxrpc_zap_tx_window(call);
736
737 if (post_ACK) {
738 /* post the final ACK message for userspace to pick up */
739 _debug("post ACK");
740 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
741 sp->call = call;
742 rxrpc_get_call(call);
743 atomic_inc(&call->skb_count);
744 spin_lock_bh(&call->lock);
745 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
746 BUG();
747 spin_unlock_bh(&call->lock);
748 goto process_further;
749 }
750
751 discard:
752 rxrpc_free_skb(skb);
753 goto process_further;
754
755 protocol_error_unlock:
756 write_unlock_bh(&call->state_lock);
757 protocol_error:
758 rxrpc_free_skb(skb);
759 _leave(" = -EPROTO");
760 return -EPROTO;
761 }
762
763 /*
764 * post a message to the socket Rx queue for recvmsg() to pick up
765 */
766 static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
767 bool fatal)
768 {
769 struct rxrpc_skb_priv *sp;
770 struct sk_buff *skb;
771 int ret;
772
773 _enter("{%d,%lx},%u,%u,%d",
774 call->debug_id, call->flags, mark, error, fatal);
775
776 /* remove timers and things for fatal messages */
777 if (fatal) {
778 del_timer_sync(&call->resend_timer);
779 del_timer_sync(&call->ack_timer);
780 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
781 }
782
783 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
784 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
785 _leave("[no userid]");
786 return 0;
787 }
788
789 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
790 skb = alloc_skb(0, GFP_NOFS);
791 if (!skb)
792 return -ENOMEM;
793
794 rxrpc_new_skb(skb);
795
796 skb->mark = mark;
797
798 sp = rxrpc_skb(skb);
799 memset(sp, 0, sizeof(*sp));
800 sp->error = error;
801 sp->call = call;
802 rxrpc_get_call(call);
803 atomic_inc(&call->skb_count);
804
805 spin_lock_bh(&call->lock);
806 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
807 spin_unlock_bh(&call->lock);
808 BUG_ON(ret < 0);
809 }
810
811 return 0;
812 }
813
814 /*
815 * handle background processing of incoming call packets and ACK / abort
816 * generation
817 */
818 void rxrpc_process_call(struct work_struct *work)
819 {
820 struct rxrpc_call *call =
821 container_of(work, struct rxrpc_call, processor);
822 struct rxrpc_wire_header whdr;
823 struct rxrpc_ackpacket ack;
824 struct rxrpc_ackinfo ackinfo;
825 struct msghdr msg;
826 struct kvec iov[5];
827 enum rxrpc_call_event genbit;
828 unsigned long bits;
829 __be32 data, pad;
830 size_t len;
831 int loop, nbit, ioc, ret, mtu;
832 u32 serial, abort_code = RX_PROTOCOL_ERROR;
833 u8 *acks = NULL;
834
835 //printk("\n--------------------\n");
836 _enter("{%d,%s,%lx} [%lu]",
837 call->debug_id, rxrpc_call_states[call->state], call->events,
838 (jiffies - call->creation_jif) / (HZ / 10));
839
840 if (!call->conn)
841 goto skip_msg_init;
842
843 /* there's a good chance we're going to have to send a message, so set
844 * one up in advance */
845 msg.msg_name = &call->conn->params.peer->srx.transport;
846 msg.msg_namelen = call->conn->params.peer->srx.transport_len;
847 msg.msg_control = NULL;
848 msg.msg_controllen = 0;
849 msg.msg_flags = 0;
850
851 whdr.epoch = htonl(call->conn->proto.epoch);
852 whdr.cid = htonl(call->cid);
853 whdr.callNumber = htonl(call->call_id);
854 whdr.seq = 0;
855 whdr.type = RXRPC_PACKET_TYPE_ACK;
856 whdr.flags = call->conn->out_clientflag;
857 whdr.userStatus = 0;
858 whdr.securityIndex = call->conn->security_ix;
859 whdr._rsvd = 0;
860 whdr.serviceId = htons(call->service_id);
861
862 memset(iov, 0, sizeof(iov));
863 iov[0].iov_base = &whdr;
864 iov[0].iov_len = sizeof(whdr);
865 skip_msg_init:
866
867 /* deal with events of a final nature */
868 if (test_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events)) {
869 enum rxrpc_skb_mark mark;
870 int error;
871
872 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
873 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
874 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
875
876 error = call->error_report;
877 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
878 mark = RXRPC_SKB_MARK_NET_ERROR;
879 _debug("post net error %d", error);
880 } else {
881 mark = RXRPC_SKB_MARK_LOCAL_ERROR;
882 error -= RXRPC_LOCAL_ERROR_OFFSET;
883 _debug("post net local error %d", error);
884 }
885
886 if (rxrpc_post_message(call, mark, error, true) < 0)
887 goto no_mem;
888 clear_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events);
889 goto kill_ACKs;
890 }
891
892 if (test_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events)) {
893 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
894
895 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
896 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
897
898 _debug("post conn abort");
899
900 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
901 call->conn->error, true) < 0)
902 goto no_mem;
903 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
904 goto kill_ACKs;
905 }
906
907 if (test_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events)) {
908 whdr.type = RXRPC_PACKET_TYPE_BUSY;
909 genbit = RXRPC_CALL_EV_REJECT_BUSY;
910 goto send_message;
911 }
912
913 if (test_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
914 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
915
916 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
917 ECONNABORTED, true) < 0)
918 goto no_mem;
919 whdr.type = RXRPC_PACKET_TYPE_ABORT;
920 data = htonl(call->local_abort);
921 iov[1].iov_base = &data;
922 iov[1].iov_len = sizeof(data);
923 genbit = RXRPC_CALL_EV_ABORT;
924 goto send_message;
925 }
926
927 if (test_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events)) {
928 genbit = RXRPC_CALL_EV_ACK_FINAL;
929
930 ack.bufferSpace = htons(8);
931 ack.maxSkew = 0;
932 ack.serial = 0;
933 ack.reason = RXRPC_ACK_IDLE;
934 ack.nAcks = 0;
935 call->ackr_reason = 0;
936
937 spin_lock_bh(&call->lock);
938 ack.serial = htonl(call->ackr_serial);
939 ack.previousPacket = htonl(call->ackr_prev_seq);
940 ack.firstPacket = htonl(call->rx_data_eaten + 1);
941 spin_unlock_bh(&call->lock);
942
943 pad = 0;
944
945 iov[1].iov_base = &ack;
946 iov[1].iov_len = sizeof(ack);
947 iov[2].iov_base = &pad;
948 iov[2].iov_len = 3;
949 iov[3].iov_base = &ackinfo;
950 iov[3].iov_len = sizeof(ackinfo);
951 goto send_ACK;
952 }
953
954 if (call->events & ((1 << RXRPC_CALL_EV_RCVD_BUSY) |
955 (1 << RXRPC_CALL_EV_RCVD_ABORT))
956 ) {
957 u32 mark;
958
959 if (test_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events))
960 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
961 else
962 mark = RXRPC_SKB_MARK_BUSY;
963
964 _debug("post abort/busy");
965 rxrpc_clear_tx_window(call);
966 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
967 goto no_mem;
968
969 clear_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
970 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
971 goto kill_ACKs;
972 }
973
974 if (test_and_clear_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events)) {
975 _debug("do implicit ackall");
976 rxrpc_clear_tx_window(call);
977 }
978
979 if (test_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events)) {
980 write_lock_bh(&call->state_lock);
981 if (call->state <= RXRPC_CALL_COMPLETE) {
982 call->state = RXRPC_CALL_LOCALLY_ABORTED;
983 call->local_abort = RX_CALL_TIMEOUT;
984 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
985 }
986 write_unlock_bh(&call->state_lock);
987
988 _debug("post timeout");
989 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
990 ETIME, true) < 0)
991 goto no_mem;
992
993 clear_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
994 goto kill_ACKs;
995 }
996
997 /* deal with assorted inbound messages */
998 if (!skb_queue_empty(&call->rx_queue)) {
999 switch (rxrpc_process_rx_queue(call, &abort_code)) {
1000 case 0:
1001 case -EAGAIN:
1002 break;
1003 case -ENOMEM:
1004 goto no_mem;
1005 case -EKEYEXPIRED:
1006 case -EKEYREJECTED:
1007 case -EPROTO:
1008 rxrpc_abort_call(call, abort_code);
1009 goto kill_ACKs;
1010 }
1011 }
1012
1013 /* handle resending */
1014 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
1015 rxrpc_resend_timer(call);
1016 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events))
1017 rxrpc_resend(call);
1018
1019 /* consider sending an ordinary ACK */
1020 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
1021 _debug("send ACK: window: %d - %d { %lx }",
1022 call->rx_data_eaten, call->ackr_win_top,
1023 call->ackr_window[0]);
1024
1025 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1026 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1027 /* ACK by sending reply DATA packet in this state */
1028 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
1029 goto maybe_reschedule;
1030 }
1031
1032 genbit = RXRPC_CALL_EV_ACK;
1033
1034 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1035 GFP_NOFS);
1036 if (!acks)
1037 goto no_mem;
1038
1039 //hdr.flags = RXRPC_SLOW_START_OK;
1040 ack.bufferSpace = htons(8);
1041 ack.maxSkew = 0;
1042
1043 spin_lock_bh(&call->lock);
1044 ack.reason = call->ackr_reason;
1045 ack.serial = htonl(call->ackr_serial);
1046 ack.previousPacket = htonl(call->ackr_prev_seq);
1047 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1048
1049 ack.nAcks = 0;
1050 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1051 nbit = loop * BITS_PER_LONG;
1052 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1053 ) {
1054 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1055 if (bits & 1) {
1056 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1057 ack.nAcks = nbit + 1;
1058 }
1059 nbit++;
1060 }
1061 }
1062 call->ackr_reason = 0;
1063 spin_unlock_bh(&call->lock);
1064
1065 pad = 0;
1066
1067 iov[1].iov_base = &ack;
1068 iov[1].iov_len = sizeof(ack);
1069 iov[2].iov_base = acks;
1070 iov[2].iov_len = ack.nAcks;
1071 iov[3].iov_base = &pad;
1072 iov[3].iov_len = 3;
1073 iov[4].iov_base = &ackinfo;
1074 iov[4].iov_len = sizeof(ackinfo);
1075
1076 switch (ack.reason) {
1077 case RXRPC_ACK_REQUESTED:
1078 case RXRPC_ACK_DUPLICATE:
1079 case RXRPC_ACK_OUT_OF_SEQUENCE:
1080 case RXRPC_ACK_EXCEEDS_WINDOW:
1081 case RXRPC_ACK_NOSPACE:
1082 case RXRPC_ACK_PING:
1083 case RXRPC_ACK_PING_RESPONSE:
1084 goto send_ACK_with_skew;
1085 case RXRPC_ACK_DELAY:
1086 case RXRPC_ACK_IDLE:
1087 goto send_ACK;
1088 }
1089 }
1090
1091 /* handle completion of security negotiations on an incoming
1092 * connection */
1093 if (test_and_clear_bit(RXRPC_CALL_EV_SECURED, &call->events)) {
1094 _debug("secured");
1095 spin_lock_bh(&call->lock);
1096
1097 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1098 _debug("securing");
1099 write_lock(&call->socket->call_lock);
1100 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1101 !test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
1102 _debug("not released");
1103 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1104 list_move_tail(&call->accept_link,
1105 &call->socket->acceptq);
1106 }
1107 write_unlock(&call->socket->call_lock);
1108 read_lock(&call->state_lock);
1109 if (call->state < RXRPC_CALL_COMPLETE)
1110 set_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
1111 read_unlock(&call->state_lock);
1112 }
1113
1114 spin_unlock_bh(&call->lock);
1115 if (!test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events))
1116 goto maybe_reschedule;
1117 }
1118
1119 /* post a notification of an acceptable connection to the app */
1120 if (test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events)) {
1121 _debug("post accept");
1122 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1123 0, false) < 0)
1124 goto no_mem;
1125 clear_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
1126 goto maybe_reschedule;
1127 }
1128
1129 /* handle incoming call acceptance */
1130 if (test_and_clear_bit(RXRPC_CALL_EV_ACCEPTED, &call->events)) {
1131 _debug("accepted");
1132 ASSERTCMP(call->rx_data_post, ==, 0);
1133 call->rx_data_post = 1;
1134 read_lock_bh(&call->state_lock);
1135 if (call->state < RXRPC_CALL_COMPLETE)
1136 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
1137 read_unlock_bh(&call->state_lock);
1138 }
1139
1140 /* drain the out of sequence received packet queue into the packet Rx
1141 * queue */
1142 if (test_and_clear_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events)) {
1143 while (call->rx_data_post == call->rx_first_oos)
1144 if (rxrpc_drain_rx_oos_queue(call) < 0)
1145 break;
1146 goto maybe_reschedule;
1147 }
1148
1149 if (test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
1150 rxrpc_release_call(call);
1151 clear_bit(RXRPC_CALL_EV_RELEASE, &call->events);
1152 }
1153
1154 /* other events may have been raised since we started checking */
1155 goto maybe_reschedule;
1156
1157 send_ACK_with_skew:
1158 ack.maxSkew = htons(call->ackr_skew);
1159 send_ACK:
1160 mtu = call->conn->params.peer->if_mtu;
1161 mtu -= call->conn->params.peer->hdrsize;
1162 ackinfo.maxMTU = htonl(mtu);
1163 ackinfo.rwind = htonl(rxrpc_rx_window_size);
1164
1165 /* permit the peer to send us jumbo packets if it wants to */
1166 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
1167 ackinfo.jumbo_max = htonl(rxrpc_rx_jumbo_max);
1168
1169 serial = atomic_inc_return(&call->conn->serial);
1170 whdr.serial = htonl(serial);
1171 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1172 serial,
1173 ntohs(ack.maxSkew),
1174 ntohl(ack.firstPacket),
1175 ntohl(ack.previousPacket),
1176 ntohl(ack.serial),
1177 rxrpc_acks(ack.reason),
1178 ack.nAcks);
1179
1180 del_timer_sync(&call->ack_timer);
1181 if (ack.nAcks > 0)
1182 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1183 goto send_message_2;
1184
1185 send_message:
1186 _debug("send message");
1187
1188 serial = atomic_inc_return(&call->conn->serial);
1189 whdr.serial = htonl(serial);
1190 _proto("Tx %s %%%u", rxrpc_pkts[whdr.type], serial);
1191 send_message_2:
1192
1193 len = iov[0].iov_len;
1194 ioc = 1;
1195 if (iov[4].iov_len) {
1196 ioc = 5;
1197 len += iov[4].iov_len;
1198 len += iov[3].iov_len;
1199 len += iov[2].iov_len;
1200 len += iov[1].iov_len;
1201 } else if (iov[3].iov_len) {
1202 ioc = 4;
1203 len += iov[3].iov_len;
1204 len += iov[2].iov_len;
1205 len += iov[1].iov_len;
1206 } else if (iov[2].iov_len) {
1207 ioc = 3;
1208 len += iov[2].iov_len;
1209 len += iov[1].iov_len;
1210 } else if (iov[1].iov_len) {
1211 ioc = 2;
1212 len += iov[1].iov_len;
1213 }
1214
1215 ret = kernel_sendmsg(call->conn->params.local->socket,
1216 &msg, iov, ioc, len);
1217 if (ret < 0) {
1218 _debug("sendmsg failed: %d", ret);
1219 read_lock_bh(&call->state_lock);
1220 if (call->state < RXRPC_CALL_DEAD)
1221 rxrpc_queue_call(call);
1222 read_unlock_bh(&call->state_lock);
1223 goto error;
1224 }
1225
1226 switch (genbit) {
1227 case RXRPC_CALL_EV_ABORT:
1228 clear_bit(genbit, &call->events);
1229 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
1230 goto kill_ACKs;
1231
1232 case RXRPC_CALL_EV_ACK_FINAL:
1233 write_lock_bh(&call->state_lock);
1234 if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK)
1235 call->state = RXRPC_CALL_COMPLETE;
1236 write_unlock_bh(&call->state_lock);
1237 goto kill_ACKs;
1238
1239 default:
1240 clear_bit(genbit, &call->events);
1241 switch (call->state) {
1242 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1243 case RXRPC_CALL_CLIENT_RECV_REPLY:
1244 case RXRPC_CALL_SERVER_RECV_REQUEST:
1245 case RXRPC_CALL_SERVER_ACK_REQUEST:
1246 _debug("start ACK timer");
1247 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
1248 call->ackr_skew, call->ackr_serial,
1249 false);
1250 default:
1251 break;
1252 }
1253 goto maybe_reschedule;
1254 }
1255
1256 kill_ACKs:
1257 del_timer_sync(&call->ack_timer);
1258 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events))
1259 rxrpc_put_call(call);
1260 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
1261
1262 maybe_reschedule:
1263 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1264 read_lock_bh(&call->state_lock);
1265 if (call->state < RXRPC_CALL_DEAD)
1266 rxrpc_queue_call(call);
1267 read_unlock_bh(&call->state_lock);
1268 }
1269
1270 /* don't leave aborted connections on the accept queue */
1271 if (call->state >= RXRPC_CALL_COMPLETE &&
1272 !list_empty(&call->accept_link)) {
1273 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1274 call, call->events, call->flags, call->conn->proto.cid);
1275
1276 read_lock_bh(&call->state_lock);
1277 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1278 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
1279 rxrpc_queue_call(call);
1280 read_unlock_bh(&call->state_lock);
1281 }
1282
1283 error:
1284 kfree(acks);
1285
1286 /* because we don't want two CPUs both processing the work item for one
1287 * call at the same time, we use a flag to note when it's busy; however
1288 * this means there's a race between clearing the flag and setting the
1289 * work pending bit and the work item being processed again */
1290 if (call->events && !work_pending(&call->processor)) {
1291 _debug("jumpstart %x", call->conn->proto.cid);
1292 rxrpc_queue_call(call);
1293 }
1294
1295 _leave("");
1296 return;
1297
1298 no_mem:
1299 _debug("out of memory");
1300 goto maybe_reschedule;
1301 }