1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001-2003 Intel Corp.
8 * This file is part of the SCTP kernel implementation
10 * These functions implement the sctp_outq class. The outqueue handles
11 * bundling and queueing of outgoing SCTP chunks.
13 * Please send any bug reports or fixes you make to the
15 * lksctp developers <linux-sctp@vger.kernel.org>
17 * Written or modified by:
18 * La Monte H.P. Yarroll <piggy@acm.org>
19 * Karl Knutson <karl@athena.chicago.il.us>
20 * Perry Melange <pmelange@null.cc.uic.edu>
21 * Xingang Guo <xingang.guo@intel.com>
22 * Hui Huang <hui.huang@nokia.com>
23 * Sridhar Samudrala <sri@us.ibm.com>
24 * Jon Grimm <jgrimm@us.ibm.com>
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/types.h>
30 #include <linux/list.h> /* For struct list_head */
31 #include <linux/socket.h>
33 #include <linux/slab.h>
34 #include <net/sock.h> /* For skb_set_owner_w */
36 #include <net/sctp/sctp.h>
37 #include <net/sctp/sm.h>
38 #include <net/sctp/stream_sched.h>
39 #include <trace/events/sctp.h>
41 /* Declare internal functions here. */
42 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
43 static void sctp_check_transmitted(struct sctp_outq *q,
44 struct list_head *transmitted_queue,
45 struct sctp_transport *transport,
46 union sctp_addr *saddr,
47 struct sctp_sackhdr *sack,
48 __u32 *highest_new_tsn);
50 static void sctp_mark_missing(struct sctp_outq *q,
51 struct list_head *transmitted_queue,
52 struct sctp_transport *transport,
53 __u32 highest_new_tsn,
54 int count_of_newacks);
56 static void sctp_outq_flush(struct sctp_outq *q, int rtx_timeout, gfp_t gfp);
58 /* Add data to the front of the queue. */
59 static inline void sctp_outq_head_data(struct sctp_outq *q,
60 struct sctp_chunk *ch)
62 struct sctp_stream_out_ext *oute;
65 list_add(&ch->list, &q->out_chunk_list);
66 q->out_qlen += ch->skb->len;
68 stream = sctp_chunk_stream_no(ch);
69 oute = SCTP_SO(&q->asoc->stream, stream)->ext;
70 list_add(&ch->stream_list, &oute->outq);
73 /* Take data from the front of the queue. */
74 static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
76 return q->sched->dequeue(q);
79 /* Add data chunk to the end of the queue. */
80 static inline void sctp_outq_tail_data(struct sctp_outq *q,
81 struct sctp_chunk *ch)
83 struct sctp_stream_out_ext *oute;
86 list_add_tail(&ch->list, &q->out_chunk_list);
87 q->out_qlen += ch->skb->len;
89 stream = sctp_chunk_stream_no(ch);
90 oute = SCTP_SO(&q->asoc->stream, stream)->ext;
91 list_add_tail(&ch->stream_list, &oute->outq);
96 * D) If count_of_newacks is greater than or equal to 2
97 * and t was not sent to the current primary then the
98 * sender MUST NOT increment missing report count for t.
100 static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
101 struct sctp_transport *transport,
102 int count_of_newacks)
104 if (count_of_newacks >= 2 && transport != primary)
110 * SFR-CACC algorithm:
111 * F) If count_of_newacks is less than 2, let d be the
112 * destination to which t was sent. If cacc_saw_newack
113 * is 0 for destination d, then the sender MUST NOT
114 * increment missing report count for t.
116 static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
117 int count_of_newacks)
119 if (count_of_newacks < 2 &&
120 (transport && !transport->cacc.cacc_saw_newack))
126 * SFR-CACC algorithm:
127 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
128 * execute steps C, D, F.
130 * C has been implemented in sctp_outq_sack
132 static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
133 struct sctp_transport *transport,
134 int count_of_newacks)
136 if (!primary->cacc.cycling_changeover) {
137 if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
139 if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
147 * SFR-CACC algorithm:
148 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
149 * than next_tsn_at_change of the current primary, then
150 * the sender MUST NOT increment missing report count
153 static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
155 if (primary->cacc.cycling_changeover &&
156 TSN_lt(tsn, primary->cacc.next_tsn_at_change))
162 * SFR-CACC algorithm:
163 * 3) If the missing report count for TSN t is to be
164 * incremented according to [RFC2960] and
165 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
166 * then the sender MUST further execute steps 3.1 and
167 * 3.2 to determine if the missing report count for
168 * TSN t SHOULD NOT be incremented.
170 * 3.3) If 3.1 and 3.2 do not dictate that the missing
171 * report count for t should not be incremented, then
172 * the sender SHOULD increment missing report count for
173 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
175 static inline int sctp_cacc_skip(struct sctp_transport *primary,
176 struct sctp_transport *transport,
177 int count_of_newacks,
180 if (primary->cacc.changeover_active &&
181 (sctp_cacc_skip_3_1(primary, transport, count_of_newacks) ||
182 sctp_cacc_skip_3_2(primary, tsn)))
187 /* Initialize an existing sctp_outq. This does the boring stuff.
188 * You still need to define handlers if you really want to DO
189 * something with this structure...
191 void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
193 memset(q, 0, sizeof(struct sctp_outq));
196 INIT_LIST_HEAD(&q->out_chunk_list);
197 INIT_LIST_HEAD(&q->control_chunk_list);
198 INIT_LIST_HEAD(&q->retransmit);
199 INIT_LIST_HEAD(&q->sacked);
200 INIT_LIST_HEAD(&q->abandoned);
201 sctp_sched_set_sched(asoc, sctp_sk(asoc->base.sk)->default_ss);
204 /* Free the outqueue structure and any related pending chunks.
206 static void __sctp_outq_teardown(struct sctp_outq *q)
208 struct sctp_transport *transport;
209 struct list_head *lchunk, *temp;
210 struct sctp_chunk *chunk, *tmp;
212 /* Throw away unacknowledged chunks. */
213 list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
215 while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
216 chunk = list_entry(lchunk, struct sctp_chunk,
218 /* Mark as part of a failed message. */
219 sctp_chunk_fail(chunk, q->error);
220 sctp_chunk_free(chunk);
224 /* Throw away chunks that have been gap ACKed. */
225 list_for_each_safe(lchunk, temp, &q->sacked) {
226 list_del_init(lchunk);
227 chunk = list_entry(lchunk, struct sctp_chunk,
229 sctp_chunk_fail(chunk, q->error);
230 sctp_chunk_free(chunk);
233 /* Throw away any chunks in the retransmit queue. */
234 list_for_each_safe(lchunk, temp, &q->retransmit) {
235 list_del_init(lchunk);
236 chunk = list_entry(lchunk, struct sctp_chunk,
238 sctp_chunk_fail(chunk, q->error);
239 sctp_chunk_free(chunk);
242 /* Throw away any chunks that are in the abandoned queue. */
243 list_for_each_safe(lchunk, temp, &q->abandoned) {
244 list_del_init(lchunk);
245 chunk = list_entry(lchunk, struct sctp_chunk,
247 sctp_chunk_fail(chunk, q->error);
248 sctp_chunk_free(chunk);
251 /* Throw away any leftover data chunks. */
252 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
253 sctp_sched_dequeue_done(q, chunk);
255 /* Mark as send failure. */
256 sctp_chunk_fail(chunk, q->error);
257 sctp_chunk_free(chunk);
260 /* Throw away any leftover control chunks. */
261 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
262 list_del_init(&chunk->list);
263 sctp_chunk_free(chunk);
267 void sctp_outq_teardown(struct sctp_outq *q)
269 __sctp_outq_teardown(q);
270 sctp_outq_init(q->asoc, q);
273 /* Free the outqueue structure and any related pending chunks. */
274 void sctp_outq_free(struct sctp_outq *q)
276 /* Throw away leftover chunks. */
277 __sctp_outq_teardown(q);
280 /* Put a new chunk in an sctp_outq. */
281 void sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk, gfp_t gfp)
283 struct net *net = q->asoc->base.net;
285 pr_debug("%s: outq:%p, chunk:%p[%s]\n", __func__, q, chunk,
286 chunk && chunk->chunk_hdr ?
287 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
290 /* If it is data, queue it up, otherwise, send it
293 if (sctp_chunk_is_data(chunk)) {
294 pr_debug("%s: outqueueing: outq:%p, chunk:%p[%s])\n",
295 __func__, q, chunk, chunk && chunk->chunk_hdr ?
296 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
299 sctp_outq_tail_data(q, chunk);
300 if (chunk->asoc->peer.prsctp_capable &&
301 SCTP_PR_PRIO_ENABLED(chunk->sinfo.sinfo_flags))
302 chunk->asoc->sent_cnt_removable++;
303 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
304 SCTP_INC_STATS(net, SCTP_MIB_OUTUNORDERCHUNKS);
306 SCTP_INC_STATS(net, SCTP_MIB_OUTORDERCHUNKS);
308 list_add_tail(&chunk->list, &q->control_chunk_list);
309 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
313 sctp_outq_flush(q, 0, gfp);
316 /* Insert a chunk into the sorted list based on the TSNs. The retransmit list
317 * and the abandoned list are in ascending order.
319 static void sctp_insert_list(struct list_head *head, struct list_head *new)
321 struct list_head *pos;
322 struct sctp_chunk *nchunk, *lchunk;
326 nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
327 ntsn = ntohl(nchunk->subh.data_hdr->tsn);
329 list_for_each(pos, head) {
330 lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
331 ltsn = ntohl(lchunk->subh.data_hdr->tsn);
332 if (TSN_lt(ntsn, ltsn)) {
333 list_add(new, pos->prev);
339 list_add_tail(new, head);
342 static int sctp_prsctp_prune_sent(struct sctp_association *asoc,
343 struct sctp_sndrcvinfo *sinfo,
344 struct list_head *queue, int msg_len)
346 struct sctp_chunk *chk, *temp;
348 list_for_each_entry_safe(chk, temp, queue, transmitted_list) {
349 struct sctp_stream_out *streamout;
351 if (!chk->msg->abandoned &&
352 (!SCTP_PR_PRIO_ENABLED(chk->sinfo.sinfo_flags) ||
353 chk->sinfo.sinfo_timetolive <= sinfo->sinfo_timetolive))
356 chk->msg->abandoned = 1;
357 list_del_init(&chk->transmitted_list);
358 sctp_insert_list(&asoc->outqueue.abandoned,
359 &chk->transmitted_list);
361 streamout = SCTP_SO(&asoc->stream, chk->sinfo.sinfo_stream);
362 asoc->sent_cnt_removable--;
363 asoc->abandoned_sent[SCTP_PR_INDEX(PRIO)]++;
364 streamout->ext->abandoned_sent[SCTP_PR_INDEX(PRIO)]++;
366 if (queue != &asoc->outqueue.retransmit &&
367 !chk->tsn_gap_acked) {
369 chk->transport->flight_size -=
371 asoc->outqueue.outstanding_bytes -= sctp_data_size(chk);
374 msg_len -= chk->skb->truesize + sizeof(struct sctp_chunk);
382 static int sctp_prsctp_prune_unsent(struct sctp_association *asoc,
383 struct sctp_sndrcvinfo *sinfo, int msg_len)
385 struct sctp_outq *q = &asoc->outqueue;
386 struct sctp_chunk *chk, *temp;
387 struct sctp_stream_out *sout;
389 q->sched->unsched_all(&asoc->stream);
391 list_for_each_entry_safe(chk, temp, &q->out_chunk_list, list) {
392 if (!chk->msg->abandoned &&
393 (!(chk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG) ||
394 !SCTP_PR_PRIO_ENABLED(chk->sinfo.sinfo_flags) ||
395 chk->sinfo.sinfo_timetolive <= sinfo->sinfo_timetolive))
398 chk->msg->abandoned = 1;
399 sctp_sched_dequeue_common(q, chk);
400 asoc->sent_cnt_removable--;
401 asoc->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
403 sout = SCTP_SO(&asoc->stream, chk->sinfo.sinfo_stream);
404 sout->ext->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
406 msg_len -= chk->skb->truesize + sizeof(struct sctp_chunk);
407 sctp_chunk_free(chk);
412 q->sched->sched_all(&asoc->stream);
417 /* Abandon the chunks according their priorities */
418 void sctp_prsctp_prune(struct sctp_association *asoc,
419 struct sctp_sndrcvinfo *sinfo, int msg_len)
421 struct sctp_transport *transport;
423 if (!asoc->peer.prsctp_capable || !asoc->sent_cnt_removable)
426 msg_len = sctp_prsctp_prune_sent(asoc, sinfo,
427 &asoc->outqueue.retransmit,
432 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
434 msg_len = sctp_prsctp_prune_sent(asoc, sinfo,
435 &transport->transmitted,
441 sctp_prsctp_prune_unsent(asoc, sinfo, msg_len);
444 /* Mark all the eligible packets on a transport for retransmission. */
445 void sctp_retransmit_mark(struct sctp_outq *q,
446 struct sctp_transport *transport,
449 struct list_head *lchunk, *ltemp;
450 struct sctp_chunk *chunk;
452 /* Walk through the specified transmitted queue. */
453 list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
454 chunk = list_entry(lchunk, struct sctp_chunk,
457 /* If the chunk is abandoned, move it to abandoned list. */
458 if (sctp_chunk_abandoned(chunk)) {
459 list_del_init(lchunk);
460 sctp_insert_list(&q->abandoned, lchunk);
462 /* If this chunk has not been previousely acked,
463 * stop considering it 'outstanding'. Our peer
464 * will most likely never see it since it will
465 * not be retransmitted
467 if (!chunk->tsn_gap_acked) {
468 if (chunk->transport)
469 chunk->transport->flight_size -=
470 sctp_data_size(chunk);
471 q->outstanding_bytes -= sctp_data_size(chunk);
472 q->asoc->peer.rwnd += sctp_data_size(chunk);
477 /* If we are doing retransmission due to a timeout or pmtu
478 * discovery, only the chunks that are not yet acked should
479 * be added to the retransmit queue.
481 if ((reason == SCTP_RTXR_FAST_RTX &&
482 (chunk->fast_retransmit == SCTP_NEED_FRTX)) ||
483 (reason != SCTP_RTXR_FAST_RTX && !chunk->tsn_gap_acked)) {
484 /* RFC 2960 6.2.1 Processing a Received SACK
486 * C) Any time a DATA chunk is marked for
487 * retransmission (via either T3-rtx timer expiration
488 * (Section 6.3.3) or via fast retransmit
489 * (Section 7.2.4)), add the data size of those
490 * chunks to the rwnd.
492 q->asoc->peer.rwnd += sctp_data_size(chunk);
493 q->outstanding_bytes -= sctp_data_size(chunk);
494 if (chunk->transport)
495 transport->flight_size -= sctp_data_size(chunk);
497 /* sctpimpguide-05 Section 2.8.2
498 * M5) If a T3-rtx timer expires, the
499 * 'TSN.Missing.Report' of all affected TSNs is set
502 chunk->tsn_missing_report = 0;
504 /* If a chunk that is being used for RTT measurement
505 * has to be retransmitted, we cannot use this chunk
506 * anymore for RTT measurements. Reset rto_pending so
507 * that a new RTT measurement is started when a new
508 * data chunk is sent.
510 if (chunk->rtt_in_progress) {
511 chunk->rtt_in_progress = 0;
512 transport->rto_pending = 0;
515 /* Move the chunk to the retransmit queue. The chunks
516 * on the retransmit queue are always kept in order.
518 list_del_init(lchunk);
519 sctp_insert_list(&q->retransmit, lchunk);
523 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d, "
524 "flight_size:%d, pba:%d\n", __func__, transport, reason,
525 transport->cwnd, transport->ssthresh, transport->flight_size,
526 transport->partial_bytes_acked);
529 /* Mark all the eligible packets on a transport for retransmission and force
532 void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
533 enum sctp_retransmit_reason reason)
535 struct net *net = q->asoc->base.net;
538 case SCTP_RTXR_T3_RTX:
539 SCTP_INC_STATS(net, SCTP_MIB_T3_RETRANSMITS);
540 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
541 /* Update the retran path if the T3-rtx timer has expired for
542 * the current retran path.
544 if (transport == transport->asoc->peer.retran_path)
545 sctp_assoc_update_retran_path(transport->asoc);
546 transport->asoc->rtx_data_chunks +=
547 transport->asoc->unack_data;
548 if (transport->pl.state == SCTP_PL_COMPLETE &&
549 transport->asoc->unack_data)
550 sctp_transport_reset_probe_timer(transport);
552 case SCTP_RTXR_FAST_RTX:
553 SCTP_INC_STATS(net, SCTP_MIB_FAST_RETRANSMITS);
554 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
557 case SCTP_RTXR_PMTUD:
558 SCTP_INC_STATS(net, SCTP_MIB_PMTUD_RETRANSMITS);
560 case SCTP_RTXR_T1_RTX:
561 SCTP_INC_STATS(net, SCTP_MIB_T1_RETRANSMITS);
562 transport->asoc->init_retries++;
568 sctp_retransmit_mark(q, transport, reason);
570 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
571 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
572 * following the procedures outlined in C1 - C5.
574 if (reason == SCTP_RTXR_T3_RTX)
575 q->asoc->stream.si->generate_ftsn(q, q->asoc->ctsn_ack_point);
577 /* Flush the queues only on timeout, since fast_rtx is only
578 * triggered during sack processing and the queue
579 * will be flushed at the end.
581 if (reason != SCTP_RTXR_FAST_RTX)
582 sctp_outq_flush(q, /* rtx_timeout */ 1, GFP_ATOMIC);
586 * Transmit DATA chunks on the retransmit queue. Upon return from
587 * __sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
588 * need to be transmitted by the caller.
589 * We assume that pkt->transport has already been set.
591 * The return value is a normal kernel error return value.
593 static int __sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
594 int rtx_timeout, int *start_timer, gfp_t gfp)
596 struct sctp_transport *transport = pkt->transport;
597 struct sctp_chunk *chunk, *chunk1;
598 struct list_head *lqueue;
599 enum sctp_xmit status;
605 lqueue = &q->retransmit;
606 fast_rtx = q->fast_rtx;
608 /* This loop handles time-out retransmissions, fast retransmissions,
609 * and retransmissions due to opening of whindow.
611 * RFC 2960 6.3.3 Handle T3-rtx Expiration
613 * E3) Determine how many of the earliest (i.e., lowest TSN)
614 * outstanding DATA chunks for the address for which the
615 * T3-rtx has expired will fit into a single packet, subject
616 * to the MTU constraint for the path corresponding to the
617 * destination transport address to which the retransmission
618 * is being sent (this may be different from the address for
619 * which the timer expires [see Section 6.4]). Call this value
620 * K. Bundle and retransmit those K DATA chunks in a single
621 * packet to the destination endpoint.
623 * [Just to be painfully clear, if we are retransmitting
624 * because a timeout just happened, we should send only ONE
625 * packet of retransmitted data.]
627 * For fast retransmissions we also send only ONE packet. However,
628 * if we are just flushing the queue due to open window, we'll
629 * try to send as much as possible.
631 list_for_each_entry_safe(chunk, chunk1, lqueue, transmitted_list) {
632 /* If the chunk is abandoned, move it to abandoned list. */
633 if (sctp_chunk_abandoned(chunk)) {
634 list_del_init(&chunk->transmitted_list);
635 sctp_insert_list(&q->abandoned,
636 &chunk->transmitted_list);
640 /* Make sure that Gap Acked TSNs are not retransmitted. A
641 * simple approach is just to move such TSNs out of the
642 * way and into a 'transmitted' queue and skip to the
645 if (chunk->tsn_gap_acked) {
646 list_move_tail(&chunk->transmitted_list,
647 &transport->transmitted);
651 /* If we are doing fast retransmit, ignore non-fast_rtransmit
654 if (fast_rtx && !chunk->fast_retransmit)
658 /* Attempt to append this chunk to the packet. */
659 status = sctp_packet_append_chunk(pkt, chunk);
662 case SCTP_XMIT_PMTU_FULL:
663 if (!pkt->has_data && !pkt->has_cookie_echo) {
664 /* If this packet did not contain DATA then
665 * retransmission did not happen, so do it
666 * again. We'll ignore the error here since
667 * control chunks are already freed so there
668 * is nothing we can do.
670 sctp_packet_transmit(pkt, gfp);
674 /* Send this packet. */
675 error = sctp_packet_transmit(pkt, gfp);
677 /* If we are retransmitting, we should only
678 * send a single packet.
679 * Otherwise, try appending this chunk again.
681 if (rtx_timeout || fast_rtx)
686 /* Bundle next chunk in the next round. */
689 case SCTP_XMIT_RWND_FULL:
690 /* Send this packet. */
691 error = sctp_packet_transmit(pkt, gfp);
693 /* Stop sending DATA as there is no more room
699 case SCTP_XMIT_DELAY:
700 /* Send this packet. */
701 error = sctp_packet_transmit(pkt, gfp);
703 /* Stop sending DATA because of nagle delay. */
708 /* The append was successful, so add this chunk to
709 * the transmitted list.
711 list_move_tail(&chunk->transmitted_list,
712 &transport->transmitted);
714 /* Mark the chunk as ineligible for fast retransmit
715 * after it is retransmitted.
717 if (chunk->fast_retransmit == SCTP_NEED_FRTX)
718 chunk->fast_retransmit = SCTP_DONT_FRTX;
720 q->asoc->stats.rtxchunks++;
724 /* Set the timer if there were no errors */
725 if (!error && !timer)
732 /* If we are here due to a retransmit timeout or a fast
733 * retransmit and if there are any chunks left in the retransmit
734 * queue that could not fit in the PMTU sized packet, they need
735 * to be marked as ineligible for a subsequent fast retransmit.
737 if (rtx_timeout || fast_rtx) {
738 list_for_each_entry(chunk1, lqueue, transmitted_list) {
739 if (chunk1->fast_retransmit == SCTP_NEED_FRTX)
740 chunk1->fast_retransmit = SCTP_DONT_FRTX;
744 *start_timer = timer;
746 /* Clear fast retransmit hint */
753 /* Cork the outqueue so queued chunks are really queued. */
754 void sctp_outq_uncork(struct sctp_outq *q, gfp_t gfp)
759 sctp_outq_flush(q, 0, gfp);
762 static int sctp_packet_singleton(struct sctp_transport *transport,
763 struct sctp_chunk *chunk, gfp_t gfp)
765 const struct sctp_association *asoc = transport->asoc;
766 const __u16 sport = asoc->base.bind_addr.port;
767 const __u16 dport = asoc->peer.port;
768 const __u32 vtag = asoc->peer.i.init_tag;
769 struct sctp_packet singleton;
771 sctp_packet_init(&singleton, transport, sport, dport);
772 sctp_packet_config(&singleton, vtag, 0);
773 if (sctp_packet_append_chunk(&singleton, chunk) != SCTP_XMIT_OK) {
774 list_del_init(&chunk->list);
775 sctp_chunk_free(chunk);
778 return sctp_packet_transmit(&singleton, gfp);
781 /* Struct to hold the context during sctp outq flush */
782 struct sctp_flush_ctx {
784 /* Current transport being used. It's NOT the same as curr active one */
785 struct sctp_transport *transport;
786 /* These transports have chunks to send. */
787 struct list_head transport_list;
788 struct sctp_association *asoc;
789 /* Packet on the current transport above */
790 struct sctp_packet *packet;
794 /* transport: current transport */
795 static void sctp_outq_select_transport(struct sctp_flush_ctx *ctx,
796 struct sctp_chunk *chunk)
798 struct sctp_transport *new_transport = chunk->transport;
800 if (!new_transport) {
801 if (!sctp_chunk_is_data(chunk)) {
802 /* If we have a prior transport pointer, see if
803 * the destination address of the chunk
804 * matches the destination address of the
805 * current transport. If not a match, then
806 * try to look up the transport with a given
807 * destination address. We do this because
808 * after processing ASCONFs, we may have new
809 * transports created.
811 if (ctx->transport && sctp_cmp_addr_exact(&chunk->dest,
812 &ctx->transport->ipaddr))
813 new_transport = ctx->transport;
815 new_transport = sctp_assoc_lookup_paddr(ctx->asoc,
819 /* if we still don't have a new transport, then
820 * use the current active path.
823 new_transport = ctx->asoc->peer.active_path;
827 switch (new_transport->state) {
829 case SCTP_UNCONFIRMED:
831 /* If the chunk is Heartbeat or Heartbeat Ack,
832 * send it to chunk->transport, even if it's
835 * 3.3.6 Heartbeat Acknowledgement:
837 * A HEARTBEAT ACK is always sent to the source IP
838 * address of the IP datagram containing the
839 * HEARTBEAT chunk to which this ack is responding.
842 * ASCONF_ACKs also must be sent to the source.
844 type = chunk->chunk_hdr->type;
845 if (type != SCTP_CID_HEARTBEAT &&
846 type != SCTP_CID_HEARTBEAT_ACK &&
847 type != SCTP_CID_ASCONF_ACK)
848 new_transport = ctx->asoc->peer.active_path;
855 /* Are we switching transports? Take care of transport locks. */
856 if (new_transport != ctx->transport) {
857 ctx->transport = new_transport;
858 ctx->packet = &ctx->transport->packet;
860 if (list_empty(&ctx->transport->send_ready))
861 list_add_tail(&ctx->transport->send_ready,
862 &ctx->transport_list);
864 sctp_packet_config(ctx->packet,
865 ctx->asoc->peer.i.init_tag,
866 ctx->asoc->peer.ecn_capable);
867 /* We've switched transports, so apply the
868 * Burst limit to the new transport.
870 sctp_transport_burst_limited(ctx->transport);
874 static void sctp_outq_flush_ctrl(struct sctp_flush_ctx *ctx)
876 struct sctp_chunk *chunk, *tmp;
877 enum sctp_xmit status;
878 int one_packet, error;
880 list_for_each_entry_safe(chunk, tmp, &ctx->q->control_chunk_list, list) {
884 * F1) This means that until such time as the ASCONF
885 * containing the add is acknowledged, the sender MUST
886 * NOT use the new IP address as a source for ANY SCTP
887 * packet except on carrying an ASCONF Chunk.
889 if (ctx->asoc->src_out_of_asoc_ok &&
890 chunk->chunk_hdr->type != SCTP_CID_ASCONF)
893 list_del_init(&chunk->list);
895 /* Pick the right transport to use. Should always be true for
896 * the first chunk as we don't have a transport by then.
898 sctp_outq_select_transport(ctx, chunk);
900 switch (chunk->chunk_hdr->type) {
903 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
904 * COMPLETE with any other chunks. [Send them immediately.]
907 case SCTP_CID_INIT_ACK:
908 case SCTP_CID_SHUTDOWN_COMPLETE:
909 error = sctp_packet_singleton(ctx->transport, chunk,
912 ctx->asoc->base.sk->sk_err = -error;
915 ctx->asoc->stats.octrlchunks++;
919 if (sctp_test_T_bit(chunk))
920 ctx->packet->vtag = ctx->asoc->c.my_vtag;
923 /* The following chunks are "response" chunks, i.e.
924 * they are generated in response to something we
925 * received. If we are sending these, then we can
926 * send only 1 packet containing these chunks.
928 case SCTP_CID_HEARTBEAT_ACK:
929 case SCTP_CID_SHUTDOWN_ACK:
930 case SCTP_CID_COOKIE_ACK:
931 case SCTP_CID_COOKIE_ECHO:
933 case SCTP_CID_ECN_CWR:
934 case SCTP_CID_ASCONF_ACK:
938 case SCTP_CID_HEARTBEAT:
939 if (chunk->pmtu_probe) {
940 error = sctp_packet_singleton(ctx->transport,
943 ctx->asoc->stats.octrlchunks++;
948 case SCTP_CID_SHUTDOWN:
949 case SCTP_CID_ECN_ECNE:
950 case SCTP_CID_ASCONF:
951 case SCTP_CID_FWD_TSN:
952 case SCTP_CID_I_FWD_TSN:
953 case SCTP_CID_RECONF:
954 status = sctp_packet_transmit_chunk(ctx->packet, chunk,
955 one_packet, ctx->gfp);
956 if (status != SCTP_XMIT_OK) {
957 /* put the chunk back */
958 list_add(&chunk->list, &ctx->q->control_chunk_list);
962 ctx->asoc->stats.octrlchunks++;
963 /* PR-SCTP C5) If a FORWARD TSN is sent, the
964 * sender MUST assure that at least one T3-rtx
967 if (chunk->chunk_hdr->type == SCTP_CID_FWD_TSN ||
968 chunk->chunk_hdr->type == SCTP_CID_I_FWD_TSN) {
969 sctp_transport_reset_t3_rtx(ctx->transport);
970 ctx->transport->last_time_sent = jiffies;
973 if (chunk == ctx->asoc->strreset_chunk)
974 sctp_transport_reset_reconf_timer(ctx->transport);
979 /* We built a chunk with an illegal type! */
985 /* Returns false if new data shouldn't be sent */
986 static bool sctp_outq_flush_rtx(struct sctp_flush_ctx *ctx,
989 int error, start_timer = 0;
991 if (ctx->asoc->peer.retran_path->state == SCTP_UNCONFIRMED)
994 if (ctx->transport != ctx->asoc->peer.retran_path) {
995 /* Switch transports & prepare the packet. */
996 ctx->transport = ctx->asoc->peer.retran_path;
997 ctx->packet = &ctx->transport->packet;
999 if (list_empty(&ctx->transport->send_ready))
1000 list_add_tail(&ctx->transport->send_ready,
1001 &ctx->transport_list);
1003 sctp_packet_config(ctx->packet, ctx->asoc->peer.i.init_tag,
1004 ctx->asoc->peer.ecn_capable);
1007 error = __sctp_outq_flush_rtx(ctx->q, ctx->packet, rtx_timeout,
1008 &start_timer, ctx->gfp);
1010 ctx->asoc->base.sk->sk_err = -error;
1013 sctp_transport_reset_t3_rtx(ctx->transport);
1014 ctx->transport->last_time_sent = jiffies;
1017 /* This can happen on COOKIE-ECHO resend. Only
1018 * one chunk can get bundled with a COOKIE-ECHO.
1020 if (ctx->packet->has_cookie_echo)
1023 /* Don't send new data if there is still data
1024 * waiting to retransmit.
1026 if (!list_empty(&ctx->q->retransmit))
1032 static void sctp_outq_flush_data(struct sctp_flush_ctx *ctx,
1035 struct sctp_chunk *chunk;
1036 enum sctp_xmit status;
1038 /* Is it OK to send data chunks? */
1039 switch (ctx->asoc->state) {
1040 case SCTP_STATE_COOKIE_ECHOED:
1041 /* Only allow bundling when this packet has a COOKIE-ECHO
1044 if (!ctx->packet || !ctx->packet->has_cookie_echo)
1048 case SCTP_STATE_ESTABLISHED:
1049 case SCTP_STATE_SHUTDOWN_PENDING:
1050 case SCTP_STATE_SHUTDOWN_RECEIVED:
1058 /* RFC 2960 6.1 Transmission of DATA Chunks
1060 * C) When the time comes for the sender to transmit,
1061 * before sending new DATA chunks, the sender MUST
1062 * first transmit any outstanding DATA chunks which
1063 * are marked for retransmission (limited by the
1066 if (!list_empty(&ctx->q->retransmit) &&
1067 !sctp_outq_flush_rtx(ctx, rtx_timeout))
1070 /* Apply Max.Burst limitation to the current transport in
1071 * case it will be used for new data. We are going to
1072 * rest it before we return, but we want to apply the limit
1073 * to the currently queued data.
1076 sctp_transport_burst_limited(ctx->transport);
1078 /* Finally, transmit new packets. */
1079 while ((chunk = sctp_outq_dequeue_data(ctx->q)) != NULL) {
1080 __u32 sid = ntohs(chunk->subh.data_hdr->stream);
1081 __u8 stream_state = SCTP_SO(&ctx->asoc->stream, sid)->state;
1083 /* Has this chunk expired? */
1084 if (sctp_chunk_abandoned(chunk)) {
1085 sctp_sched_dequeue_done(ctx->q, chunk);
1086 sctp_chunk_fail(chunk, 0);
1087 sctp_chunk_free(chunk);
1091 if (stream_state == SCTP_STREAM_CLOSED) {
1092 sctp_outq_head_data(ctx->q, chunk);
1096 sctp_outq_select_transport(ctx, chunk);
1098 pr_debug("%s: outq:%p, chunk:%p[%s], tx-tsn:0x%x skb->head:%p skb->users:%d\n",
1099 __func__, ctx->q, chunk, chunk && chunk->chunk_hdr ?
1100 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
1101 "illegal chunk", ntohl(chunk->subh.data_hdr->tsn),
1102 chunk->skb ? chunk->skb->head : NULL, chunk->skb ?
1103 refcount_read(&chunk->skb->users) : -1);
1105 /* Add the chunk to the packet. */
1106 status = sctp_packet_transmit_chunk(ctx->packet, chunk, 0,
1108 if (status != SCTP_XMIT_OK) {
1109 /* We could not append this chunk, so put
1110 * the chunk back on the output queue.
1112 pr_debug("%s: could not transmit tsn:0x%x, status:%d\n",
1113 __func__, ntohl(chunk->subh.data_hdr->tsn),
1116 sctp_outq_head_data(ctx->q, chunk);
1120 /* The sender is in the SHUTDOWN-PENDING state,
1121 * The sender MAY set the I-bit in the DATA
1124 if (ctx->asoc->state == SCTP_STATE_SHUTDOWN_PENDING)
1125 chunk->chunk_hdr->flags |= SCTP_DATA_SACK_IMM;
1126 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
1127 ctx->asoc->stats.ouodchunks++;
1129 ctx->asoc->stats.oodchunks++;
1131 /* Only now it's safe to consider this
1132 * chunk as sent, sched-wise.
1134 sctp_sched_dequeue_done(ctx->q, chunk);
1136 list_add_tail(&chunk->transmitted_list,
1137 &ctx->transport->transmitted);
1139 sctp_transport_reset_t3_rtx(ctx->transport);
1140 ctx->transport->last_time_sent = jiffies;
1142 /* Only let one DATA chunk get bundled with a
1143 * COOKIE-ECHO chunk.
1145 if (ctx->packet->has_cookie_echo)
1150 static void sctp_outq_flush_transports(struct sctp_flush_ctx *ctx)
1152 struct sock *sk = ctx->asoc->base.sk;
1153 struct list_head *ltransport;
1154 struct sctp_packet *packet;
1155 struct sctp_transport *t;
1158 while ((ltransport = sctp_list_dequeue(&ctx->transport_list)) != NULL) {
1159 t = list_entry(ltransport, struct sctp_transport, send_ready);
1160 packet = &t->packet;
1161 if (!sctp_packet_empty(packet)) {
1163 if (t->dst && __sk_dst_get(sk) != t->dst) {
1165 sk_setup_caps(sk, t->dst);
1168 error = sctp_packet_transmit(packet, ctx->gfp);
1170 ctx->q->asoc->base.sk->sk_err = -error;
1173 /* Clear the burst limited state, if any */
1174 sctp_transport_burst_reset(t);
1178 /* Try to flush an outqueue.
1180 * Description: Send everything in q which we legally can, subject to
1181 * congestion limitations.
1182 * * Note: This function can be called from multiple contexts so appropriate
1183 * locking concerns must be made. Today we use the sock lock to protect
1187 static void sctp_outq_flush(struct sctp_outq *q, int rtx_timeout, gfp_t gfp)
1189 struct sctp_flush_ctx ctx = {
1192 .transport_list = LIST_HEAD_INIT(ctx.transport_list),
1200 * When bundling control chunks with DATA chunks, an
1201 * endpoint MUST place control chunks first in the outbound
1202 * SCTP packet. The transmitter MUST transmit DATA chunks
1203 * within a SCTP packet in increasing order of TSN.
1207 sctp_outq_flush_ctrl(&ctx);
1209 if (q->asoc->src_out_of_asoc_ok)
1210 goto sctp_flush_out;
1212 sctp_outq_flush_data(&ctx, rtx_timeout);
1216 sctp_outq_flush_transports(&ctx);
1219 /* Update unack_data based on the incoming SACK chunk */
1220 static void sctp_sack_update_unack_data(struct sctp_association *assoc,
1221 struct sctp_sackhdr *sack)
1223 union sctp_sack_variable *frags;
1227 unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
1229 frags = sack->variable;
1230 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
1231 unack_data -= ((ntohs(frags[i].gab.end) -
1232 ntohs(frags[i].gab.start) + 1));
1235 assoc->unack_data = unack_data;
1238 /* This is where we REALLY process a SACK.
1240 * Process the SACK against the outqueue. Mostly, this just frees
1241 * things off the transmitted queue.
1243 int sctp_outq_sack(struct sctp_outq *q, struct sctp_chunk *chunk)
1245 struct sctp_association *asoc = q->asoc;
1246 struct sctp_sackhdr *sack = chunk->subh.sack_hdr;
1247 struct sctp_transport *transport;
1248 struct sctp_chunk *tchunk = NULL;
1249 struct list_head *lchunk, *transport_list, *temp;
1250 union sctp_sack_variable *frags = sack->variable;
1251 __u32 sack_ctsn, ctsn, tsn;
1252 __u32 highest_tsn, highest_new_tsn;
1254 unsigned int outstanding;
1255 struct sctp_transport *primary = asoc->peer.primary_path;
1256 int count_of_newacks = 0;
1260 /* Grab the association's destination address list. */
1261 transport_list = &asoc->peer.transport_addr_list;
1263 /* SCTP path tracepoint for congestion control debugging. */
1264 if (trace_sctp_probe_path_enabled()) {
1265 list_for_each_entry(transport, transport_list, transports)
1266 trace_sctp_probe_path(transport, asoc);
1269 sack_ctsn = ntohl(sack->cum_tsn_ack);
1270 gap_ack_blocks = ntohs(sack->num_gap_ack_blocks);
1271 asoc->stats.gapcnt += gap_ack_blocks;
1273 * SFR-CACC algorithm:
1274 * On receipt of a SACK the sender SHOULD execute the
1275 * following statements.
1277 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1278 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1279 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1281 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1282 * is set the receiver of the SACK MUST take the following actions:
1284 * A) Initialize the cacc_saw_newack to 0 for all destination
1287 * Only bother if changeover_active is set. Otherwise, this is
1288 * totally suboptimal to do on every SACK.
1290 if (primary->cacc.changeover_active) {
1291 u8 clear_cycling = 0;
1293 if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1294 primary->cacc.changeover_active = 0;
1298 if (clear_cycling || gap_ack_blocks) {
1299 list_for_each_entry(transport, transport_list,
1302 transport->cacc.cycling_changeover = 0;
1304 transport->cacc.cacc_saw_newack = 0;
1309 /* Get the highest TSN in the sack. */
1310 highest_tsn = sack_ctsn;
1312 highest_tsn += ntohs(frags[gap_ack_blocks - 1].gab.end);
1314 if (TSN_lt(asoc->highest_sacked, highest_tsn))
1315 asoc->highest_sacked = highest_tsn;
1317 highest_new_tsn = sack_ctsn;
1319 /* Run through the retransmit queue. Credit bytes received
1320 * and free those chunks that we can.
1322 sctp_check_transmitted(q, &q->retransmit, NULL, NULL, sack, &highest_new_tsn);
1324 /* Run through the transmitted queue.
1325 * Credit bytes received and free those chunks which we can.
1327 * This is a MASSIVE candidate for optimization.
1329 list_for_each_entry(transport, transport_list, transports) {
1330 sctp_check_transmitted(q, &transport->transmitted,
1331 transport, &chunk->source, sack,
1334 * SFR-CACC algorithm:
1335 * C) Let count_of_newacks be the number of
1336 * destinations for which cacc_saw_newack is set.
1338 if (transport->cacc.cacc_saw_newack)
1342 /* Move the Cumulative TSN Ack Point if appropriate. */
1343 if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn)) {
1344 asoc->ctsn_ack_point = sack_ctsn;
1348 if (gap_ack_blocks) {
1350 if (asoc->fast_recovery && accum_moved)
1351 highest_new_tsn = highest_tsn;
1353 list_for_each_entry(transport, transport_list, transports)
1354 sctp_mark_missing(q, &transport->transmitted, transport,
1355 highest_new_tsn, count_of_newacks);
1358 /* Update unack_data field in the assoc. */
1359 sctp_sack_update_unack_data(asoc, sack);
1361 ctsn = asoc->ctsn_ack_point;
1363 /* Throw away stuff rotting on the sack queue. */
1364 list_for_each_safe(lchunk, temp, &q->sacked) {
1365 tchunk = list_entry(lchunk, struct sctp_chunk,
1367 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1368 if (TSN_lte(tsn, ctsn)) {
1369 list_del_init(&tchunk->transmitted_list);
1370 if (asoc->peer.prsctp_capable &&
1371 SCTP_PR_PRIO_ENABLED(chunk->sinfo.sinfo_flags))
1372 asoc->sent_cnt_removable--;
1373 sctp_chunk_free(tchunk);
1377 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1378 * number of bytes still outstanding after processing the
1379 * Cumulative TSN Ack and the Gap Ack Blocks.
1382 sack_a_rwnd = ntohl(sack->a_rwnd);
1383 asoc->peer.zero_window_announced = !sack_a_rwnd;
1384 outstanding = q->outstanding_bytes;
1386 if (outstanding < sack_a_rwnd)
1387 sack_a_rwnd -= outstanding;
1391 asoc->peer.rwnd = sack_a_rwnd;
1393 asoc->stream.si->generate_ftsn(q, sack_ctsn);
1395 pr_debug("%s: sack cumulative tsn ack:0x%x\n", __func__, sack_ctsn);
1396 pr_debug("%s: cumulative tsn ack of assoc:%p is 0x%x, "
1397 "advertised peer ack point:0x%x\n", __func__, asoc, ctsn,
1398 asoc->adv_peer_ack_point);
1400 return sctp_outq_is_empty(q);
1403 /* Is the outqueue empty?
1404 * The queue is empty when we have not pending data, no in-flight data
1405 * and nothing pending retransmissions.
1407 int sctp_outq_is_empty(const struct sctp_outq *q)
1409 return q->out_qlen == 0 && q->outstanding_bytes == 0 &&
1410 list_empty(&q->retransmit);
1413 /********************************************************************
1414 * 2nd Level Abstractions
1415 ********************************************************************/
1417 /* Go through a transport's transmitted list or the association's retransmit
1418 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1419 * The retransmit list will not have an associated transport.
1421 * I added coherent debug information output. --xguo
1423 * Instead of printing 'sacked' or 'kept' for each TSN on the
1424 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1425 * KEPT TSN6-TSN7, etc.
1427 static void sctp_check_transmitted(struct sctp_outq *q,
1428 struct list_head *transmitted_queue,
1429 struct sctp_transport *transport,
1430 union sctp_addr *saddr,
1431 struct sctp_sackhdr *sack,
1432 __u32 *highest_new_tsn_in_sack)
1434 struct list_head *lchunk;
1435 struct sctp_chunk *tchunk;
1436 struct list_head tlist;
1440 __u8 restart_timer = 0;
1441 int bytes_acked = 0;
1442 int migrate_bytes = 0;
1443 bool forward_progress = false;
1445 sack_ctsn = ntohl(sack->cum_tsn_ack);
1447 INIT_LIST_HEAD(&tlist);
1449 /* The while loop will skip empty transmitted queues. */
1450 while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1451 tchunk = list_entry(lchunk, struct sctp_chunk,
1454 if (sctp_chunk_abandoned(tchunk)) {
1455 /* Move the chunk to abandoned list. */
1456 sctp_insert_list(&q->abandoned, lchunk);
1458 /* If this chunk has not been acked, stop
1459 * considering it as 'outstanding'.
1461 if (transmitted_queue != &q->retransmit &&
1462 !tchunk->tsn_gap_acked) {
1463 if (tchunk->transport)
1464 tchunk->transport->flight_size -=
1465 sctp_data_size(tchunk);
1466 q->outstanding_bytes -= sctp_data_size(tchunk);
1471 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1472 if (sctp_acked(sack, tsn)) {
1473 /* If this queue is the retransmit queue, the
1474 * retransmit timer has already reclaimed
1475 * the outstanding bytes for this chunk, so only
1476 * count bytes associated with a transport.
1478 if (transport && !tchunk->tsn_gap_acked) {
1479 /* If this chunk is being used for RTT
1480 * measurement, calculate the RTT and update
1481 * the RTO using this value.
1483 * 6.3.1 C5) Karn's algorithm: RTT measurements
1484 * MUST NOT be made using packets that were
1485 * retransmitted (and thus for which it is
1486 * ambiguous whether the reply was for the
1487 * first instance of the packet or a later
1490 if (!sctp_chunk_retransmitted(tchunk) &&
1491 tchunk->rtt_in_progress) {
1492 tchunk->rtt_in_progress = 0;
1493 rtt = jiffies - tchunk->sent_at;
1494 sctp_transport_update_rto(transport,
1498 if (TSN_lte(tsn, sack_ctsn)) {
1500 * SFR-CACC algorithm:
1501 * 2) If the SACK contains gap acks
1502 * and the flag CHANGEOVER_ACTIVE is
1503 * set the receiver of the SACK MUST
1504 * take the following action:
1506 * B) For each TSN t being acked that
1507 * has not been acked in any SACK so
1508 * far, set cacc_saw_newack to 1 for
1509 * the destination that the TSN was
1512 if (sack->num_gap_ack_blocks &&
1513 q->asoc->peer.primary_path->cacc.
1515 transport->cacc.cacc_saw_newack
1520 /* If the chunk hasn't been marked as ACKED,
1521 * mark it and account bytes_acked if the
1522 * chunk had a valid transport (it will not
1523 * have a transport if ASCONF had deleted it
1524 * while DATA was outstanding).
1526 if (!tchunk->tsn_gap_acked) {
1527 tchunk->tsn_gap_acked = 1;
1528 if (TSN_lt(*highest_new_tsn_in_sack, tsn))
1529 *highest_new_tsn_in_sack = tsn;
1530 bytes_acked += sctp_data_size(tchunk);
1531 if (!tchunk->transport)
1532 migrate_bytes += sctp_data_size(tchunk);
1533 forward_progress = true;
1536 if (TSN_lte(tsn, sack_ctsn)) {
1537 /* RFC 2960 6.3.2 Retransmission Timer Rules
1539 * R3) Whenever a SACK is received
1540 * that acknowledges the DATA chunk
1541 * with the earliest outstanding TSN
1542 * for that address, restart T3-rtx
1543 * timer for that address with its
1547 forward_progress = true;
1549 list_add_tail(&tchunk->transmitted_list,
1552 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1553 * M2) Each time a SACK arrives reporting
1554 * 'Stray DATA chunk(s)' record the highest TSN
1555 * reported as newly acknowledged, call this
1556 * value 'HighestTSNinSack'. A newly
1557 * acknowledged DATA chunk is one not
1558 * previously acknowledged in a SACK.
1560 * When the SCTP sender of data receives a SACK
1561 * chunk that acknowledges, for the first time,
1562 * the receipt of a DATA chunk, all the still
1563 * unacknowledged DATA chunks whose TSN is
1564 * older than that newly acknowledged DATA
1565 * chunk, are qualified as 'Stray DATA chunks'.
1567 list_add_tail(lchunk, &tlist);
1570 if (tchunk->tsn_gap_acked) {
1571 pr_debug("%s: receiver reneged on data TSN:0x%x\n",
1574 tchunk->tsn_gap_acked = 0;
1576 if (tchunk->transport)
1577 bytes_acked -= sctp_data_size(tchunk);
1579 /* RFC 2960 6.3.2 Retransmission Timer Rules
1581 * R4) Whenever a SACK is received missing a
1582 * TSN that was previously acknowledged via a
1583 * Gap Ack Block, start T3-rtx for the
1584 * destination address to which the DATA
1585 * chunk was originally
1586 * transmitted if it is not already running.
1591 list_add_tail(lchunk, &tlist);
1597 struct sctp_association *asoc = transport->asoc;
1599 /* We may have counted DATA that was migrated
1600 * to this transport due to DEL-IP operation.
1601 * Subtract those bytes, since the were never
1602 * send on this transport and shouldn't be
1603 * credited to this transport.
1605 bytes_acked -= migrate_bytes;
1607 /* 8.2. When an outstanding TSN is acknowledged,
1608 * the endpoint shall clear the error counter of
1609 * the destination transport address to which the
1610 * DATA chunk was last sent.
1611 * The association's overall error counter is
1614 transport->error_count = 0;
1615 transport->asoc->overall_error_count = 0;
1616 forward_progress = true;
1619 * While in SHUTDOWN PENDING, we may have started
1620 * the T5 shutdown guard timer after reaching the
1621 * retransmission limit. Stop that timer as soon
1622 * as the receiver acknowledged any data.
1624 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING &&
1625 del_timer(&asoc->timers
1626 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]))
1627 sctp_association_put(asoc);
1629 /* Mark the destination transport address as
1630 * active if it is not so marked.
1632 if ((transport->state == SCTP_INACTIVE ||
1633 transport->state == SCTP_UNCONFIRMED) &&
1634 sctp_cmp_addr_exact(&transport->ipaddr, saddr)) {
1635 sctp_assoc_control_transport(
1639 SCTP_RECEIVED_SACK);
1642 sctp_transport_raise_cwnd(transport, sack_ctsn,
1645 transport->flight_size -= bytes_acked;
1646 if (transport->flight_size == 0)
1647 transport->partial_bytes_acked = 0;
1648 q->outstanding_bytes -= bytes_acked + migrate_bytes;
1650 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1651 * When a sender is doing zero window probing, it
1652 * should not timeout the association if it continues
1653 * to receive new packets from the receiver. The
1654 * reason is that the receiver MAY keep its window
1655 * closed for an indefinite time.
1656 * A sender is doing zero window probing when the
1657 * receiver's advertised window is zero, and there is
1658 * only one data chunk in flight to the receiver.
1660 * Allow the association to timeout while in SHUTDOWN
1661 * PENDING or SHUTDOWN RECEIVED in case the receiver
1662 * stays in zero window mode forever.
1664 if (!q->asoc->peer.rwnd &&
1665 !list_empty(&tlist) &&
1666 (sack_ctsn+2 == q->asoc->next_tsn) &&
1667 q->asoc->state < SCTP_STATE_SHUTDOWN_PENDING) {
1668 pr_debug("%s: sack received for zero window "
1669 "probe:%u\n", __func__, sack_ctsn);
1671 q->asoc->overall_error_count = 0;
1672 transport->error_count = 0;
1676 /* RFC 2960 6.3.2 Retransmission Timer Rules
1678 * R2) Whenever all outstanding data sent to an address have
1679 * been acknowledged, turn off the T3-rtx timer of that
1682 if (!transport->flight_size) {
1683 if (del_timer(&transport->T3_rtx_timer))
1684 sctp_transport_put(transport);
1685 } else if (restart_timer) {
1686 if (!mod_timer(&transport->T3_rtx_timer,
1687 jiffies + transport->rto))
1688 sctp_transport_hold(transport);
1691 if (forward_progress) {
1693 sctp_transport_dst_confirm(transport);
1697 list_splice(&tlist, transmitted_queue);
1700 /* Mark chunks as missing and consequently may get retransmitted. */
1701 static void sctp_mark_missing(struct sctp_outq *q,
1702 struct list_head *transmitted_queue,
1703 struct sctp_transport *transport,
1704 __u32 highest_new_tsn_in_sack,
1705 int count_of_newacks)
1707 struct sctp_chunk *chunk;
1709 char do_fast_retransmit = 0;
1710 struct sctp_association *asoc = q->asoc;
1711 struct sctp_transport *primary = asoc->peer.primary_path;
1713 list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
1715 tsn = ntohl(chunk->subh.data_hdr->tsn);
1717 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1718 * 'Unacknowledged TSN's', if the TSN number of an
1719 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1720 * value, increment the 'TSN.Missing.Report' count on that
1721 * chunk if it has NOT been fast retransmitted or marked for
1722 * fast retransmit already.
1724 if (chunk->fast_retransmit == SCTP_CAN_FRTX &&
1725 !chunk->tsn_gap_acked &&
1726 TSN_lt(tsn, highest_new_tsn_in_sack)) {
1728 /* SFR-CACC may require us to skip marking
1729 * this chunk as missing.
1731 if (!transport || !sctp_cacc_skip(primary,
1733 count_of_newacks, tsn)) {
1734 chunk->tsn_missing_report++;
1736 pr_debug("%s: tsn:0x%x missing counter:%d\n",
1737 __func__, tsn, chunk->tsn_missing_report);
1741 * M4) If any DATA chunk is found to have a
1742 * 'TSN.Missing.Report'
1743 * value larger than or equal to 3, mark that chunk for
1744 * retransmission and start the fast retransmit procedure.
1747 if (chunk->tsn_missing_report >= 3) {
1748 chunk->fast_retransmit = SCTP_NEED_FRTX;
1749 do_fast_retransmit = 1;
1754 if (do_fast_retransmit)
1755 sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1757 pr_debug("%s: transport:%p, cwnd:%d, ssthresh:%d, "
1758 "flight_size:%d, pba:%d\n", __func__, transport,
1759 transport->cwnd, transport->ssthresh,
1760 transport->flight_size, transport->partial_bytes_acked);
1764 /* Is the given TSN acked by this packet? */
1765 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1767 __u32 ctsn = ntohl(sack->cum_tsn_ack);
1768 union sctp_sack_variable *frags;
1769 __u16 tsn_offset, blocks;
1772 if (TSN_lte(tsn, ctsn))
1775 /* 3.3.4 Selective Acknowledgment (SACK) (3):
1778 * These fields contain the Gap Ack Blocks. They are repeated
1779 * for each Gap Ack Block up to the number of Gap Ack Blocks
1780 * defined in the Number of Gap Ack Blocks field. All DATA
1781 * chunks with TSNs greater than or equal to (Cumulative TSN
1782 * Ack + Gap Ack Block Start) and less than or equal to
1783 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1784 * Block are assumed to have been received correctly.
1787 frags = sack->variable;
1788 blocks = ntohs(sack->num_gap_ack_blocks);
1789 tsn_offset = tsn - ctsn;
1790 for (i = 0; i < blocks; ++i) {
1791 if (tsn_offset >= ntohs(frags[i].gab.start) &&
1792 tsn_offset <= ntohs(frags[i].gab.end))
1801 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
1802 int nskips, __be16 stream)
1806 for (i = 0; i < nskips; i++) {
1807 if (skiplist[i].stream == stream)
1813 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1814 void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
1816 struct sctp_association *asoc = q->asoc;
1817 struct sctp_chunk *ftsn_chunk = NULL;
1818 struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1822 struct sctp_chunk *chunk;
1823 struct list_head *lchunk, *temp;
1825 if (!asoc->peer.prsctp_capable)
1828 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1831 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1832 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1834 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1835 asoc->adv_peer_ack_point = ctsn;
1837 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1838 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1839 * the chunk next in the out-queue space is marked as "abandoned" as
1840 * shown in the following example:
1842 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1843 * and the Advanced.Peer.Ack.Point is updated to this value:
1845 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1846 * normal SACK processing local advancement
1848 * Adv.Ack.Pt-> 102 acked 102 acked
1849 * 103 abandoned 103 abandoned
1850 * 104 abandoned Adv.Ack.P-> 104 abandoned
1852 * 106 acked 106 acked
1855 * In this example, the data sender successfully advanced the
1856 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1858 list_for_each_safe(lchunk, temp, &q->abandoned) {
1859 chunk = list_entry(lchunk, struct sctp_chunk,
1861 tsn = ntohl(chunk->subh.data_hdr->tsn);
1863 /* Remove any chunks in the abandoned queue that are acked by
1866 if (TSN_lte(tsn, ctsn)) {
1867 list_del_init(lchunk);
1868 sctp_chunk_free(chunk);
1870 if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1871 asoc->adv_peer_ack_point = tsn;
1872 if (chunk->chunk_hdr->flags &
1873 SCTP_DATA_UNORDERED)
1875 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1877 chunk->subh.data_hdr->stream);
1878 ftsn_skip_arr[skip_pos].stream =
1879 chunk->subh.data_hdr->stream;
1880 ftsn_skip_arr[skip_pos].ssn =
1881 chunk->subh.data_hdr->ssn;
1882 if (skip_pos == nskips)
1891 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1892 * is greater than the Cumulative TSN ACK carried in the received
1893 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1894 * chunk containing the latest value of the
1895 * "Advanced.Peer.Ack.Point".
1897 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1898 * list each stream and sequence number in the forwarded TSN. This
1899 * information will enable the receiver to easily find any
1900 * stranded TSN's waiting on stream reorder queues. Each stream
1901 * SHOULD only be reported once; this means that if multiple
1902 * abandoned messages occur in the same stream then only the
1903 * highest abandoned stream sequence number is reported. If the
1904 * total size of the FORWARD TSN does NOT fit in a single MTU then
1905 * the sender of the FORWARD TSN SHOULD lower the
1906 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1909 if (asoc->adv_peer_ack_point > ctsn)
1910 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
1911 nskips, &ftsn_skip_arr[0]);
1914 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1915 SCTP_INC_STATS(asoc->base.net, SCTP_MIB_OUTCTRLCHUNKS);