1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
7 * This file is part of the SCTP kernel implementation
9 * These functions implement the sctp_outq class. The outqueue handles
10 * bundling and queueing of outgoing SCTP chunks.
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <linux-sctp@vger.kernel.org>
32 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Perry Melange <pmelange@null.cc.uic.edu>
36 * Xingang Guo <xingang.guo@intel.com>
37 * Hui Huang <hui.huang@nokia.com>
38 * Sridhar Samudrala <sri@us.ibm.com>
39 * Jon Grimm <jgrimm@us.ibm.com>
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 #include <linux/types.h>
45 #include <linux/list.h> /* For struct list_head */
46 #include <linux/socket.h>
48 #include <linux/slab.h>
49 #include <net/sock.h> /* For skb_set_owner_w */
51 #include <net/sctp/sctp.h>
52 #include <net/sctp/sm.h>
54 /* Declare internal functions here. */
55 static int sctp_acked(struct sctp_sackhdr
*sack
, __u32 tsn
);
56 static void sctp_check_transmitted(struct sctp_outq
*q
,
57 struct list_head
*transmitted_queue
,
58 struct sctp_transport
*transport
,
59 union sctp_addr
*saddr
,
60 struct sctp_sackhdr
*sack
,
61 __u32
*highest_new_tsn
);
63 static void sctp_mark_missing(struct sctp_outq
*q
,
64 struct list_head
*transmitted_queue
,
65 struct sctp_transport
*transport
,
66 __u32 highest_new_tsn
,
67 int count_of_newacks
);
69 static void sctp_generate_fwdtsn(struct sctp_outq
*q
, __u32 sack_ctsn
);
71 static void sctp_outq_flush(struct sctp_outq
*q
, int rtx_timeout
, gfp_t gfp
);
73 /* Add data to the front of the queue. */
74 static inline void sctp_outq_head_data(struct sctp_outq
*q
,
75 struct sctp_chunk
*ch
)
77 list_add(&ch
->list
, &q
->out_chunk_list
);
78 q
->out_qlen
+= ch
->skb
->len
;
81 /* Take data from the front of the queue. */
82 static inline struct sctp_chunk
*sctp_outq_dequeue_data(struct sctp_outq
*q
)
84 struct sctp_chunk
*ch
= NULL
;
86 if (!list_empty(&q
->out_chunk_list
)) {
87 struct list_head
*entry
= q
->out_chunk_list
.next
;
89 ch
= list_entry(entry
, struct sctp_chunk
, list
);
91 q
->out_qlen
-= ch
->skb
->len
;
95 /* Add data chunk to the end of the queue. */
96 static inline void sctp_outq_tail_data(struct sctp_outq
*q
,
97 struct sctp_chunk
*ch
)
99 list_add_tail(&ch
->list
, &q
->out_chunk_list
);
100 q
->out_qlen
+= ch
->skb
->len
;
104 * SFR-CACC algorithm:
105 * D) If count_of_newacks is greater than or equal to 2
106 * and t was not sent to the current primary then the
107 * sender MUST NOT increment missing report count for t.
109 static inline int sctp_cacc_skip_3_1_d(struct sctp_transport
*primary
,
110 struct sctp_transport
*transport
,
111 int count_of_newacks
)
113 if (count_of_newacks
>= 2 && transport
!= primary
)
119 * SFR-CACC algorithm:
120 * F) If count_of_newacks is less than 2, let d be the
121 * destination to which t was sent. If cacc_saw_newack
122 * is 0 for destination d, then the sender MUST NOT
123 * increment missing report count for t.
125 static inline int sctp_cacc_skip_3_1_f(struct sctp_transport
*transport
,
126 int count_of_newacks
)
128 if (count_of_newacks
< 2 &&
129 (transport
&& !transport
->cacc
.cacc_saw_newack
))
135 * SFR-CACC algorithm:
136 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
137 * execute steps C, D, F.
139 * C has been implemented in sctp_outq_sack
141 static inline int sctp_cacc_skip_3_1(struct sctp_transport
*primary
,
142 struct sctp_transport
*transport
,
143 int count_of_newacks
)
145 if (!primary
->cacc
.cycling_changeover
) {
146 if (sctp_cacc_skip_3_1_d(primary
, transport
, count_of_newacks
))
148 if (sctp_cacc_skip_3_1_f(transport
, count_of_newacks
))
156 * SFR-CACC algorithm:
157 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
158 * than next_tsn_at_change of the current primary, then
159 * the sender MUST NOT increment missing report count
162 static inline int sctp_cacc_skip_3_2(struct sctp_transport
*primary
, __u32 tsn
)
164 if (primary
->cacc
.cycling_changeover
&&
165 TSN_lt(tsn
, primary
->cacc
.next_tsn_at_change
))
171 * SFR-CACC algorithm:
172 * 3) If the missing report count for TSN t is to be
173 * incremented according to [RFC2960] and
174 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
175 * then the sender MUST further execute steps 3.1 and
176 * 3.2 to determine if the missing report count for
177 * TSN t SHOULD NOT be incremented.
179 * 3.3) If 3.1 and 3.2 do not dictate that the missing
180 * report count for t should not be incremented, then
181 * the sender SHOULD increment missing report count for
182 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
184 static inline int sctp_cacc_skip(struct sctp_transport
*primary
,
185 struct sctp_transport
*transport
,
186 int count_of_newacks
,
189 if (primary
->cacc
.changeover_active
&&
190 (sctp_cacc_skip_3_1(primary
, transport
, count_of_newacks
) ||
191 sctp_cacc_skip_3_2(primary
, tsn
)))
196 /* Initialize an existing sctp_outq. This does the boring stuff.
197 * You still need to define handlers if you really want to DO
198 * something with this structure...
200 void sctp_outq_init(struct sctp_association
*asoc
, struct sctp_outq
*q
)
202 memset(q
, 0, sizeof(struct sctp_outq
));
205 INIT_LIST_HEAD(&q
->out_chunk_list
);
206 INIT_LIST_HEAD(&q
->control_chunk_list
);
207 INIT_LIST_HEAD(&q
->retransmit
);
208 INIT_LIST_HEAD(&q
->sacked
);
209 INIT_LIST_HEAD(&q
->abandoned
);
212 /* Free the outqueue structure and any related pending chunks.
214 static void __sctp_outq_teardown(struct sctp_outq
*q
)
216 struct sctp_transport
*transport
;
217 struct list_head
*lchunk
, *temp
;
218 struct sctp_chunk
*chunk
, *tmp
;
220 /* Throw away unacknowledged chunks. */
221 list_for_each_entry(transport
, &q
->asoc
->peer
.transport_addr_list
,
223 while ((lchunk
= sctp_list_dequeue(&transport
->transmitted
)) != NULL
) {
224 chunk
= list_entry(lchunk
, struct sctp_chunk
,
226 /* Mark as part of a failed message. */
227 sctp_chunk_fail(chunk
, q
->error
);
228 sctp_chunk_free(chunk
);
232 /* Throw away chunks that have been gap ACKed. */
233 list_for_each_safe(lchunk
, temp
, &q
->sacked
) {
234 list_del_init(lchunk
);
235 chunk
= list_entry(lchunk
, struct sctp_chunk
,
237 sctp_chunk_fail(chunk
, q
->error
);
238 sctp_chunk_free(chunk
);
241 /* Throw away any chunks in the retransmit queue. */
242 list_for_each_safe(lchunk
, temp
, &q
->retransmit
) {
243 list_del_init(lchunk
);
244 chunk
= list_entry(lchunk
, struct sctp_chunk
,
246 sctp_chunk_fail(chunk
, q
->error
);
247 sctp_chunk_free(chunk
);
250 /* Throw away any chunks that are in the abandoned queue. */
251 list_for_each_safe(lchunk
, temp
, &q
->abandoned
) {
252 list_del_init(lchunk
);
253 chunk
= list_entry(lchunk
, struct sctp_chunk
,
255 sctp_chunk_fail(chunk
, q
->error
);
256 sctp_chunk_free(chunk
);
259 /* Throw away any leftover data chunks. */
260 while ((chunk
= sctp_outq_dequeue_data(q
)) != NULL
) {
262 /* Mark as send failure. */
263 sctp_chunk_fail(chunk
, q
->error
);
264 sctp_chunk_free(chunk
);
267 /* Throw away any leftover control chunks. */
268 list_for_each_entry_safe(chunk
, tmp
, &q
->control_chunk_list
, list
) {
269 list_del_init(&chunk
->list
);
270 sctp_chunk_free(chunk
);
274 void sctp_outq_teardown(struct sctp_outq
*q
)
276 __sctp_outq_teardown(q
);
277 sctp_outq_init(q
->asoc
, q
);
280 /* Free the outqueue structure and any related pending chunks. */
281 void sctp_outq_free(struct sctp_outq
*q
)
283 /* Throw away leftover chunks. */
284 __sctp_outq_teardown(q
);
287 /* Put a new chunk in an sctp_outq. */
288 void sctp_outq_tail(struct sctp_outq
*q
, struct sctp_chunk
*chunk
, gfp_t gfp
)
290 struct net
*net
= sock_net(q
->asoc
->base
.sk
);
292 pr_debug("%s: outq:%p, chunk:%p[%s]\n", __func__
, q
, chunk
,
293 chunk
&& chunk
->chunk_hdr
?
294 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
)) :
297 /* If it is data, queue it up, otherwise, send it
300 if (sctp_chunk_is_data(chunk
)) {
301 pr_debug("%s: outqueueing: outq:%p, chunk:%p[%s])\n",
302 __func__
, q
, chunk
, chunk
&& chunk
->chunk_hdr
?
303 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
)) :
306 sctp_outq_tail_data(q
, chunk
);
307 if (chunk
->asoc
->peer
.prsctp_capable
&&
308 SCTP_PR_PRIO_ENABLED(chunk
->sinfo
.sinfo_flags
))
309 chunk
->asoc
->sent_cnt_removable
++;
310 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
311 SCTP_INC_STATS(net
, SCTP_MIB_OUTUNORDERCHUNKS
);
313 SCTP_INC_STATS(net
, SCTP_MIB_OUTORDERCHUNKS
);
315 list_add_tail(&chunk
->list
, &q
->control_chunk_list
);
316 SCTP_INC_STATS(net
, SCTP_MIB_OUTCTRLCHUNKS
);
320 sctp_outq_flush(q
, 0, gfp
);
323 /* Insert a chunk into the sorted list based on the TSNs. The retransmit list
324 * and the abandoned list are in ascending order.
326 static void sctp_insert_list(struct list_head
*head
, struct list_head
*new)
328 struct list_head
*pos
;
329 struct sctp_chunk
*nchunk
, *lchunk
;
333 nchunk
= list_entry(new, struct sctp_chunk
, transmitted_list
);
334 ntsn
= ntohl(nchunk
->subh
.data_hdr
->tsn
);
336 list_for_each(pos
, head
) {
337 lchunk
= list_entry(pos
, struct sctp_chunk
, transmitted_list
);
338 ltsn
= ntohl(lchunk
->subh
.data_hdr
->tsn
);
339 if (TSN_lt(ntsn
, ltsn
)) {
340 list_add(new, pos
->prev
);
346 list_add_tail(new, head
);
349 static int sctp_prsctp_prune_sent(struct sctp_association
*asoc
,
350 struct sctp_sndrcvinfo
*sinfo
,
351 struct list_head
*queue
, int msg_len
)
353 struct sctp_chunk
*chk
, *temp
;
355 list_for_each_entry_safe(chk
, temp
, queue
, transmitted_list
) {
356 struct sctp_stream_out
*streamout
;
358 if (!SCTP_PR_PRIO_ENABLED(chk
->sinfo
.sinfo_flags
) ||
359 chk
->sinfo
.sinfo_timetolive
<= sinfo
->sinfo_timetolive
)
362 list_del_init(&chk
->transmitted_list
);
363 sctp_insert_list(&asoc
->outqueue
.abandoned
,
364 &chk
->transmitted_list
);
366 streamout
= &asoc
->stream
.out
[chk
->sinfo
.sinfo_stream
];
367 asoc
->sent_cnt_removable
--;
368 asoc
->abandoned_sent
[SCTP_PR_INDEX(PRIO
)]++;
369 streamout
->abandoned_sent
[SCTP_PR_INDEX(PRIO
)]++;
371 if (queue
!= &asoc
->outqueue
.retransmit
&&
372 !chk
->tsn_gap_acked
) {
374 chk
->transport
->flight_size
-=
376 asoc
->outqueue
.outstanding_bytes
-= sctp_data_size(chk
);
379 msg_len
-= SCTP_DATA_SNDSIZE(chk
) +
380 sizeof(struct sk_buff
) +
381 sizeof(struct sctp_chunk
);
389 static int sctp_prsctp_prune_unsent(struct sctp_association
*asoc
,
390 struct sctp_sndrcvinfo
*sinfo
, int msg_len
)
392 struct sctp_outq
*q
= &asoc
->outqueue
;
393 struct sctp_chunk
*chk
, *temp
;
395 list_for_each_entry_safe(chk
, temp
, &q
->out_chunk_list
, list
) {
396 if (!SCTP_PR_PRIO_ENABLED(chk
->sinfo
.sinfo_flags
) ||
397 chk
->sinfo
.sinfo_timetolive
<= sinfo
->sinfo_timetolive
)
400 list_del_init(&chk
->list
);
401 q
->out_qlen
-= chk
->skb
->len
;
402 asoc
->sent_cnt_removable
--;
403 asoc
->abandoned_unsent
[SCTP_PR_INDEX(PRIO
)]++;
404 if (chk
->sinfo
.sinfo_stream
< asoc
->stream
.outcnt
) {
405 struct sctp_stream_out
*streamout
=
406 &asoc
->stream
.out
[chk
->sinfo
.sinfo_stream
];
408 streamout
->abandoned_unsent
[SCTP_PR_INDEX(PRIO
)]++;
411 msg_len
-= SCTP_DATA_SNDSIZE(chk
) +
412 sizeof(struct sk_buff
) +
413 sizeof(struct sctp_chunk
);
414 sctp_chunk_free(chk
);
422 /* Abandon the chunks according their priorities */
423 void sctp_prsctp_prune(struct sctp_association
*asoc
,
424 struct sctp_sndrcvinfo
*sinfo
, int msg_len
)
426 struct sctp_transport
*transport
;
428 if (!asoc
->peer
.prsctp_capable
|| !asoc
->sent_cnt_removable
)
431 msg_len
= sctp_prsctp_prune_sent(asoc
, sinfo
,
432 &asoc
->outqueue
.retransmit
,
437 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
439 msg_len
= sctp_prsctp_prune_sent(asoc
, sinfo
,
440 &transport
->transmitted
,
446 sctp_prsctp_prune_unsent(asoc
, sinfo
, msg_len
);
449 /* Mark all the eligible packets on a transport for retransmission. */
450 void sctp_retransmit_mark(struct sctp_outq
*q
,
451 struct sctp_transport
*transport
,
454 struct list_head
*lchunk
, *ltemp
;
455 struct sctp_chunk
*chunk
;
457 /* Walk through the specified transmitted queue. */
458 list_for_each_safe(lchunk
, ltemp
, &transport
->transmitted
) {
459 chunk
= list_entry(lchunk
, struct sctp_chunk
,
462 /* If the chunk is abandoned, move it to abandoned list. */
463 if (sctp_chunk_abandoned(chunk
)) {
464 list_del_init(lchunk
);
465 sctp_insert_list(&q
->abandoned
, lchunk
);
467 /* If this chunk has not been previousely acked,
468 * stop considering it 'outstanding'. Our peer
469 * will most likely never see it since it will
470 * not be retransmitted
472 if (!chunk
->tsn_gap_acked
) {
473 if (chunk
->transport
)
474 chunk
->transport
->flight_size
-=
475 sctp_data_size(chunk
);
476 q
->outstanding_bytes
-= sctp_data_size(chunk
);
477 q
->asoc
->peer
.rwnd
+= sctp_data_size(chunk
);
482 /* If we are doing retransmission due to a timeout or pmtu
483 * discovery, only the chunks that are not yet acked should
484 * be added to the retransmit queue.
486 if ((reason
== SCTP_RTXR_FAST_RTX
&&
487 (chunk
->fast_retransmit
== SCTP_NEED_FRTX
)) ||
488 (reason
!= SCTP_RTXR_FAST_RTX
&& !chunk
->tsn_gap_acked
)) {
489 /* RFC 2960 6.2.1 Processing a Received SACK
491 * C) Any time a DATA chunk is marked for
492 * retransmission (via either T3-rtx timer expiration
493 * (Section 6.3.3) or via fast retransmit
494 * (Section 7.2.4)), add the data size of those
495 * chunks to the rwnd.
497 q
->asoc
->peer
.rwnd
+= sctp_data_size(chunk
);
498 q
->outstanding_bytes
-= sctp_data_size(chunk
);
499 if (chunk
->transport
)
500 transport
->flight_size
-= sctp_data_size(chunk
);
502 /* sctpimpguide-05 Section 2.8.2
503 * M5) If a T3-rtx timer expires, the
504 * 'TSN.Missing.Report' of all affected TSNs is set
507 chunk
->tsn_missing_report
= 0;
509 /* If a chunk that is being used for RTT measurement
510 * has to be retransmitted, we cannot use this chunk
511 * anymore for RTT measurements. Reset rto_pending so
512 * that a new RTT measurement is started when a new
513 * data chunk is sent.
515 if (chunk
->rtt_in_progress
) {
516 chunk
->rtt_in_progress
= 0;
517 transport
->rto_pending
= 0;
520 /* Move the chunk to the retransmit queue. The chunks
521 * on the retransmit queue are always kept in order.
523 list_del_init(lchunk
);
524 sctp_insert_list(&q
->retransmit
, lchunk
);
528 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d, "
529 "flight_size:%d, pba:%d\n", __func__
, transport
, reason
,
530 transport
->cwnd
, transport
->ssthresh
, transport
->flight_size
,
531 transport
->partial_bytes_acked
);
534 /* Mark all the eligible packets on a transport for retransmission and force
537 void sctp_retransmit(struct sctp_outq
*q
, struct sctp_transport
*transport
,
538 enum sctp_retransmit_reason reason
)
540 struct net
*net
= sock_net(q
->asoc
->base
.sk
);
543 case SCTP_RTXR_T3_RTX
:
544 SCTP_INC_STATS(net
, SCTP_MIB_T3_RETRANSMITS
);
545 sctp_transport_lower_cwnd(transport
, SCTP_LOWER_CWND_T3_RTX
);
546 /* Update the retran path if the T3-rtx timer has expired for
547 * the current retran path.
549 if (transport
== transport
->asoc
->peer
.retran_path
)
550 sctp_assoc_update_retran_path(transport
->asoc
);
551 transport
->asoc
->rtx_data_chunks
+=
552 transport
->asoc
->unack_data
;
554 case SCTP_RTXR_FAST_RTX
:
555 SCTP_INC_STATS(net
, SCTP_MIB_FAST_RETRANSMITS
);
556 sctp_transport_lower_cwnd(transport
, SCTP_LOWER_CWND_FAST_RTX
);
559 case SCTP_RTXR_PMTUD
:
560 SCTP_INC_STATS(net
, SCTP_MIB_PMTUD_RETRANSMITS
);
562 case SCTP_RTXR_T1_RTX
:
563 SCTP_INC_STATS(net
, SCTP_MIB_T1_RETRANSMITS
);
564 transport
->asoc
->init_retries
++;
570 sctp_retransmit_mark(q
, transport
, reason
);
572 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
573 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
574 * following the procedures outlined in C1 - C5.
576 if (reason
== SCTP_RTXR_T3_RTX
)
577 sctp_generate_fwdtsn(q
, q
->asoc
->ctsn_ack_point
);
579 /* Flush the queues only on timeout, since fast_rtx is only
580 * triggered during sack processing and the queue
581 * will be flushed at the end.
583 if (reason
!= SCTP_RTXR_FAST_RTX
)
584 sctp_outq_flush(q
, /* rtx_timeout */ 1, GFP_ATOMIC
);
588 * Transmit DATA chunks on the retransmit queue. Upon return from
589 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
590 * need to be transmitted by the caller.
591 * We assume that pkt->transport has already been set.
593 * The return value is a normal kernel error return value.
595 static int sctp_outq_flush_rtx(struct sctp_outq
*q
, struct sctp_packet
*pkt
,
596 int rtx_timeout
, int *start_timer
)
598 struct sctp_transport
*transport
= pkt
->transport
;
599 struct sctp_chunk
*chunk
, *chunk1
;
600 struct list_head
*lqueue
;
601 enum sctp_xmit status
;
607 lqueue
= &q
->retransmit
;
608 fast_rtx
= q
->fast_rtx
;
610 /* This loop handles time-out retransmissions, fast retransmissions,
611 * and retransmissions due to opening of whindow.
613 * RFC 2960 6.3.3 Handle T3-rtx Expiration
615 * E3) Determine how many of the earliest (i.e., lowest TSN)
616 * outstanding DATA chunks for the address for which the
617 * T3-rtx has expired will fit into a single packet, subject
618 * to the MTU constraint for the path corresponding to the
619 * destination transport address to which the retransmission
620 * is being sent (this may be different from the address for
621 * which the timer expires [see Section 6.4]). Call this value
622 * K. Bundle and retransmit those K DATA chunks in a single
623 * packet to the destination endpoint.
625 * [Just to be painfully clear, if we are retransmitting
626 * because a timeout just happened, we should send only ONE
627 * packet of retransmitted data.]
629 * For fast retransmissions we also send only ONE packet. However,
630 * if we are just flushing the queue due to open window, we'll
631 * try to send as much as possible.
633 list_for_each_entry_safe(chunk
, chunk1
, lqueue
, transmitted_list
) {
634 /* If the chunk is abandoned, move it to abandoned list. */
635 if (sctp_chunk_abandoned(chunk
)) {
636 list_del_init(&chunk
->transmitted_list
);
637 sctp_insert_list(&q
->abandoned
,
638 &chunk
->transmitted_list
);
642 /* Make sure that Gap Acked TSNs are not retransmitted. A
643 * simple approach is just to move such TSNs out of the
644 * way and into a 'transmitted' queue and skip to the
647 if (chunk
->tsn_gap_acked
) {
648 list_move_tail(&chunk
->transmitted_list
,
649 &transport
->transmitted
);
653 /* If we are doing fast retransmit, ignore non-fast_rtransmit
656 if (fast_rtx
&& !chunk
->fast_retransmit
)
660 /* Attempt to append this chunk to the packet. */
661 status
= sctp_packet_append_chunk(pkt
, chunk
);
664 case SCTP_XMIT_PMTU_FULL
:
665 if (!pkt
->has_data
&& !pkt
->has_cookie_echo
) {
666 /* If this packet did not contain DATA then
667 * retransmission did not happen, so do it
668 * again. We'll ignore the error here since
669 * control chunks are already freed so there
670 * is nothing we can do.
672 sctp_packet_transmit(pkt
, GFP_ATOMIC
);
676 /* Send this packet. */
677 error
= sctp_packet_transmit(pkt
, GFP_ATOMIC
);
679 /* If we are retransmitting, we should only
680 * send a single packet.
681 * Otherwise, try appending this chunk again.
683 if (rtx_timeout
|| fast_rtx
)
688 /* Bundle next chunk in the next round. */
691 case SCTP_XMIT_RWND_FULL
:
692 /* Send this packet. */
693 error
= sctp_packet_transmit(pkt
, GFP_ATOMIC
);
695 /* Stop sending DATA as there is no more room
701 case SCTP_XMIT_DELAY
:
702 /* Send this packet. */
703 error
= sctp_packet_transmit(pkt
, GFP_ATOMIC
);
705 /* Stop sending DATA because of nagle delay. */
710 /* The append was successful, so add this chunk to
711 * the transmitted list.
713 list_move_tail(&chunk
->transmitted_list
,
714 &transport
->transmitted
);
716 /* Mark the chunk as ineligible for fast retransmit
717 * after it is retransmitted.
719 if (chunk
->fast_retransmit
== SCTP_NEED_FRTX
)
720 chunk
->fast_retransmit
= SCTP_DONT_FRTX
;
722 q
->asoc
->stats
.rtxchunks
++;
726 /* Set the timer if there were no errors */
727 if (!error
&& !timer
)
734 /* If we are here due to a retransmit timeout or a fast
735 * retransmit and if there are any chunks left in the retransmit
736 * queue that could not fit in the PMTU sized packet, they need
737 * to be marked as ineligible for a subsequent fast retransmit.
739 if (rtx_timeout
|| fast_rtx
) {
740 list_for_each_entry(chunk1
, lqueue
, transmitted_list
) {
741 if (chunk1
->fast_retransmit
== SCTP_NEED_FRTX
)
742 chunk1
->fast_retransmit
= SCTP_DONT_FRTX
;
746 *start_timer
= timer
;
748 /* Clear fast retransmit hint */
755 /* Cork the outqueue so queued chunks are really queued. */
756 void sctp_outq_uncork(struct sctp_outq
*q
, gfp_t gfp
)
761 sctp_outq_flush(q
, 0, gfp
);
766 * Try to flush an outqueue.
768 * Description: Send everything in q which we legally can, subject to
769 * congestion limitations.
770 * * Note: This function can be called from multiple contexts so appropriate
771 * locking concerns must be made. Today we use the sock lock to protect
774 static void sctp_outq_flush(struct sctp_outq
*q
, int rtx_timeout
, gfp_t gfp
)
776 struct sctp_packet
*packet
;
777 struct sctp_packet singleton
;
778 struct sctp_association
*asoc
= q
->asoc
;
779 __u16 sport
= asoc
->base
.bind_addr
.port
;
780 __u16 dport
= asoc
->peer
.port
;
781 __u32 vtag
= asoc
->peer
.i
.init_tag
;
782 struct sctp_transport
*transport
= NULL
;
783 struct sctp_transport
*new_transport
;
784 struct sctp_chunk
*chunk
, *tmp
;
785 enum sctp_xmit status
;
790 /* These transports have chunks to send. */
791 struct list_head transport_list
;
792 struct list_head
*ltransport
;
794 INIT_LIST_HEAD(&transport_list
);
800 * When bundling control chunks with DATA chunks, an
801 * endpoint MUST place control chunks first in the outbound
802 * SCTP packet. The transmitter MUST transmit DATA chunks
803 * within a SCTP packet in increasing order of TSN.
807 list_for_each_entry_safe(chunk
, tmp
, &q
->control_chunk_list
, list
) {
809 * F1) This means that until such time as the ASCONF
810 * containing the add is acknowledged, the sender MUST
811 * NOT use the new IP address as a source for ANY SCTP
812 * packet except on carrying an ASCONF Chunk.
814 if (asoc
->src_out_of_asoc_ok
&&
815 chunk
->chunk_hdr
->type
!= SCTP_CID_ASCONF
)
818 list_del_init(&chunk
->list
);
820 /* Pick the right transport to use. */
821 new_transport
= chunk
->transport
;
823 if (!new_transport
) {
825 * If we have a prior transport pointer, see if
826 * the destination address of the chunk
827 * matches the destination address of the
828 * current transport. If not a match, then
829 * try to look up the transport with a given
830 * destination address. We do this because
831 * after processing ASCONFs, we may have new
832 * transports created.
835 sctp_cmp_addr_exact(&chunk
->dest
,
837 new_transport
= transport
;
839 new_transport
= sctp_assoc_lookup_paddr(asoc
,
842 /* if we still don't have a new transport, then
843 * use the current active path.
846 new_transport
= asoc
->peer
.active_path
;
847 } else if ((new_transport
->state
== SCTP_INACTIVE
) ||
848 (new_transport
->state
== SCTP_UNCONFIRMED
) ||
849 (new_transport
->state
== SCTP_PF
)) {
850 /* If the chunk is Heartbeat or Heartbeat Ack,
851 * send it to chunk->transport, even if it's
854 * 3.3.6 Heartbeat Acknowledgement:
856 * A HEARTBEAT ACK is always sent to the source IP
857 * address of the IP datagram containing the
858 * HEARTBEAT chunk to which this ack is responding.
861 * ASCONF_ACKs also must be sent to the source.
863 if (chunk
->chunk_hdr
->type
!= SCTP_CID_HEARTBEAT
&&
864 chunk
->chunk_hdr
->type
!= SCTP_CID_HEARTBEAT_ACK
&&
865 chunk
->chunk_hdr
->type
!= SCTP_CID_ASCONF_ACK
)
866 new_transport
= asoc
->peer
.active_path
;
869 /* Are we switching transports?
870 * Take care of transport locks.
872 if (new_transport
!= transport
) {
873 transport
= new_transport
;
874 if (list_empty(&transport
->send_ready
)) {
875 list_add_tail(&transport
->send_ready
,
878 packet
= &transport
->packet
;
879 sctp_packet_config(packet
, vtag
,
880 asoc
->peer
.ecn_capable
);
883 switch (chunk
->chunk_hdr
->type
) {
887 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
888 * COMPLETE with any other chunks. [Send them immediately.]
891 case SCTP_CID_INIT_ACK
:
892 case SCTP_CID_SHUTDOWN_COMPLETE
:
893 sctp_packet_init(&singleton
, transport
, sport
, dport
);
894 sctp_packet_config(&singleton
, vtag
, 0);
895 sctp_packet_append_chunk(&singleton
, chunk
);
896 error
= sctp_packet_transmit(&singleton
, gfp
);
898 asoc
->base
.sk
->sk_err
= -error
;
904 if (sctp_test_T_bit(chunk
)) {
905 packet
->vtag
= asoc
->c
.my_vtag
;
907 /* The following chunks are "response" chunks, i.e.
908 * they are generated in response to something we
909 * received. If we are sending these, then we can
910 * send only 1 packet containing these chunks.
912 case SCTP_CID_HEARTBEAT_ACK
:
913 case SCTP_CID_SHUTDOWN_ACK
:
914 case SCTP_CID_COOKIE_ACK
:
915 case SCTP_CID_COOKIE_ECHO
:
917 case SCTP_CID_ECN_CWR
:
918 case SCTP_CID_ASCONF_ACK
:
923 case SCTP_CID_HEARTBEAT
:
924 case SCTP_CID_SHUTDOWN
:
925 case SCTP_CID_ECN_ECNE
:
926 case SCTP_CID_ASCONF
:
927 case SCTP_CID_FWD_TSN
:
928 case SCTP_CID_RECONF
:
929 status
= sctp_packet_transmit_chunk(packet
, chunk
,
931 if (status
!= SCTP_XMIT_OK
) {
932 /* put the chunk back */
933 list_add(&chunk
->list
, &q
->control_chunk_list
);
937 asoc
->stats
.octrlchunks
++;
938 /* PR-SCTP C5) If a FORWARD TSN is sent, the
939 * sender MUST assure that at least one T3-rtx
942 if (chunk
->chunk_hdr
->type
== SCTP_CID_FWD_TSN
) {
943 sctp_transport_reset_t3_rtx(transport
);
944 transport
->last_time_sent
= jiffies
;
947 if (chunk
== asoc
->strreset_chunk
)
948 sctp_transport_reset_reconf_timer(transport
);
953 /* We built a chunk with an illegal type! */
958 if (q
->asoc
->src_out_of_asoc_ok
)
961 /* Is it OK to send data chunks? */
962 switch (asoc
->state
) {
963 case SCTP_STATE_COOKIE_ECHOED
:
964 /* Only allow bundling when this packet has a COOKIE-ECHO
967 if (!packet
|| !packet
->has_cookie_echo
)
971 case SCTP_STATE_ESTABLISHED
:
972 case SCTP_STATE_SHUTDOWN_PENDING
:
973 case SCTP_STATE_SHUTDOWN_RECEIVED
:
975 * RFC 2960 6.1 Transmission of DATA Chunks
977 * C) When the time comes for the sender to transmit,
978 * before sending new DATA chunks, the sender MUST
979 * first transmit any outstanding DATA chunks which
980 * are marked for retransmission (limited by the
983 if (!list_empty(&q
->retransmit
)) {
984 if (asoc
->peer
.retran_path
->state
== SCTP_UNCONFIRMED
)
986 if (transport
== asoc
->peer
.retran_path
)
989 /* Switch transports & prepare the packet. */
991 transport
= asoc
->peer
.retran_path
;
993 if (list_empty(&transport
->send_ready
)) {
994 list_add_tail(&transport
->send_ready
,
998 packet
= &transport
->packet
;
999 sctp_packet_config(packet
, vtag
,
1000 asoc
->peer
.ecn_capable
);
1002 error
= sctp_outq_flush_rtx(q
, packet
,
1003 rtx_timeout
, &start_timer
);
1005 asoc
->base
.sk
->sk_err
= -error
;
1008 sctp_transport_reset_t3_rtx(transport
);
1009 transport
->last_time_sent
= jiffies
;
1012 /* This can happen on COOKIE-ECHO resend. Only
1013 * one chunk can get bundled with a COOKIE-ECHO.
1015 if (packet
->has_cookie_echo
)
1016 goto sctp_flush_out
;
1018 /* Don't send new data if there is still data
1019 * waiting to retransmit.
1021 if (!list_empty(&q
->retransmit
))
1022 goto sctp_flush_out
;
1025 /* Apply Max.Burst limitation to the current transport in
1026 * case it will be used for new data. We are going to
1027 * rest it before we return, but we want to apply the limit
1028 * to the currently queued data.
1031 sctp_transport_burst_limited(transport
);
1033 /* Finally, transmit new packets. */
1034 while ((chunk
= sctp_outq_dequeue_data(q
)) != NULL
) {
1035 __u32 sid
= ntohs(chunk
->subh
.data_hdr
->stream
);
1037 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
1038 * stream identifier.
1040 if (chunk
->sinfo
.sinfo_stream
>= asoc
->stream
.outcnt
) {
1042 /* Mark as failed send. */
1043 sctp_chunk_fail(chunk
, SCTP_ERROR_INV_STRM
);
1044 if (asoc
->peer
.prsctp_capable
&&
1045 SCTP_PR_PRIO_ENABLED(chunk
->sinfo
.sinfo_flags
))
1046 asoc
->sent_cnt_removable
--;
1047 sctp_chunk_free(chunk
);
1051 /* Has this chunk expired? */
1052 if (sctp_chunk_abandoned(chunk
)) {
1053 sctp_chunk_fail(chunk
, 0);
1054 sctp_chunk_free(chunk
);
1058 if (asoc
->stream
.out
[sid
].state
== SCTP_STREAM_CLOSED
) {
1059 sctp_outq_head_data(q
, chunk
);
1060 goto sctp_flush_out
;
1063 /* If there is a specified transport, use it.
1064 * Otherwise, we want to use the active path.
1066 new_transport
= chunk
->transport
;
1067 if (!new_transport
||
1068 ((new_transport
->state
== SCTP_INACTIVE
) ||
1069 (new_transport
->state
== SCTP_UNCONFIRMED
) ||
1070 (new_transport
->state
== SCTP_PF
)))
1071 new_transport
= asoc
->peer
.active_path
;
1072 if (new_transport
->state
== SCTP_UNCONFIRMED
) {
1073 WARN_ONCE(1, "Attempt to send packet on unconfirmed path.");
1074 sctp_chunk_fail(chunk
, 0);
1075 sctp_chunk_free(chunk
);
1079 /* Change packets if necessary. */
1080 if (new_transport
!= transport
) {
1081 transport
= new_transport
;
1083 /* Schedule to have this transport's
1086 if (list_empty(&transport
->send_ready
)) {
1087 list_add_tail(&transport
->send_ready
,
1091 packet
= &transport
->packet
;
1092 sctp_packet_config(packet
, vtag
,
1093 asoc
->peer
.ecn_capable
);
1094 /* We've switched transports, so apply the
1095 * Burst limit to the new transport.
1097 sctp_transport_burst_limited(transport
);
1100 pr_debug("%s: outq:%p, chunk:%p[%s], tx-tsn:0x%x skb->head:%p "
1102 __func__
, q
, chunk
, chunk
&& chunk
->chunk_hdr
?
1103 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
)) :
1104 "illegal chunk", ntohl(chunk
->subh
.data_hdr
->tsn
),
1105 chunk
->skb
? chunk
->skb
->head
: NULL
, chunk
->skb
?
1106 refcount_read(&chunk
->skb
->users
) : -1);
1108 /* Add the chunk to the packet. */
1109 status
= sctp_packet_transmit_chunk(packet
, chunk
, 0, gfp
);
1112 case SCTP_XMIT_PMTU_FULL
:
1113 case SCTP_XMIT_RWND_FULL
:
1114 case SCTP_XMIT_DELAY
:
1115 /* We could not append this chunk, so put
1116 * the chunk back on the output queue.
1118 pr_debug("%s: could not transmit tsn:0x%x, status:%d\n",
1119 __func__
, ntohl(chunk
->subh
.data_hdr
->tsn
),
1122 sctp_outq_head_data(q
, chunk
);
1123 goto sctp_flush_out
;
1126 /* The sender is in the SHUTDOWN-PENDING state,
1127 * The sender MAY set the I-bit in the DATA
1130 if (asoc
->state
== SCTP_STATE_SHUTDOWN_PENDING
)
1131 chunk
->chunk_hdr
->flags
|= SCTP_DATA_SACK_IMM
;
1132 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
1133 asoc
->stats
.ouodchunks
++;
1135 asoc
->stats
.oodchunks
++;
1143 /* BUG: We assume that the sctp_packet_transmit()
1144 * call below will succeed all the time and add the
1145 * chunk to the transmitted list and restart the
1147 * It is possible that the call can fail under OOM
1150 * Is this really a problem? Won't this behave
1153 list_add_tail(&chunk
->transmitted_list
,
1154 &transport
->transmitted
);
1156 sctp_transport_reset_t3_rtx(transport
);
1157 transport
->last_time_sent
= jiffies
;
1159 /* Only let one DATA chunk get bundled with a
1160 * COOKIE-ECHO chunk.
1162 if (packet
->has_cookie_echo
)
1163 goto sctp_flush_out
;
1174 /* Before returning, examine all the transports touched in
1175 * this call. Right now, we bluntly force clear all the
1176 * transports. Things might change after we implement Nagle.
1177 * But such an examination is still required.
1181 while ((ltransport
= sctp_list_dequeue(&transport_list
)) != NULL
) {
1182 struct sctp_transport
*t
= list_entry(ltransport
,
1183 struct sctp_transport
,
1185 packet
= &t
->packet
;
1186 if (!sctp_packet_empty(packet
)) {
1187 error
= sctp_packet_transmit(packet
, gfp
);
1189 asoc
->base
.sk
->sk_err
= -error
;
1192 /* Clear the burst limited state, if any */
1193 sctp_transport_burst_reset(t
);
1197 /* Update unack_data based on the incoming SACK chunk */
1198 static void sctp_sack_update_unack_data(struct sctp_association
*assoc
,
1199 struct sctp_sackhdr
*sack
)
1201 union sctp_sack_variable
*frags
;
1205 unack_data
= assoc
->next_tsn
- assoc
->ctsn_ack_point
- 1;
1207 frags
= sack
->variable
;
1208 for (i
= 0; i
< ntohs(sack
->num_gap_ack_blocks
); i
++) {
1209 unack_data
-= ((ntohs(frags
[i
].gab
.end
) -
1210 ntohs(frags
[i
].gab
.start
) + 1));
1213 assoc
->unack_data
= unack_data
;
1216 /* This is where we REALLY process a SACK.
1218 * Process the SACK against the outqueue. Mostly, this just frees
1219 * things off the transmitted queue.
1221 int sctp_outq_sack(struct sctp_outq
*q
, struct sctp_chunk
*chunk
)
1223 struct sctp_association
*asoc
= q
->asoc
;
1224 struct sctp_sackhdr
*sack
= chunk
->subh
.sack_hdr
;
1225 struct sctp_transport
*transport
;
1226 struct sctp_chunk
*tchunk
= NULL
;
1227 struct list_head
*lchunk
, *transport_list
, *temp
;
1228 union sctp_sack_variable
*frags
= sack
->variable
;
1229 __u32 sack_ctsn
, ctsn
, tsn
;
1230 __u32 highest_tsn
, highest_new_tsn
;
1232 unsigned int outstanding
;
1233 struct sctp_transport
*primary
= asoc
->peer
.primary_path
;
1234 int count_of_newacks
= 0;
1238 /* Grab the association's destination address list. */
1239 transport_list
= &asoc
->peer
.transport_addr_list
;
1241 sack_ctsn
= ntohl(sack
->cum_tsn_ack
);
1242 gap_ack_blocks
= ntohs(sack
->num_gap_ack_blocks
);
1243 asoc
->stats
.gapcnt
+= gap_ack_blocks
;
1245 * SFR-CACC algorithm:
1246 * On receipt of a SACK the sender SHOULD execute the
1247 * following statements.
1249 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1250 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1251 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1253 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1254 * is set the receiver of the SACK MUST take the following actions:
1256 * A) Initialize the cacc_saw_newack to 0 for all destination
1259 * Only bother if changeover_active is set. Otherwise, this is
1260 * totally suboptimal to do on every SACK.
1262 if (primary
->cacc
.changeover_active
) {
1263 u8 clear_cycling
= 0;
1265 if (TSN_lte(primary
->cacc
.next_tsn_at_change
, sack_ctsn
)) {
1266 primary
->cacc
.changeover_active
= 0;
1270 if (clear_cycling
|| gap_ack_blocks
) {
1271 list_for_each_entry(transport
, transport_list
,
1274 transport
->cacc
.cycling_changeover
= 0;
1276 transport
->cacc
.cacc_saw_newack
= 0;
1281 /* Get the highest TSN in the sack. */
1282 highest_tsn
= sack_ctsn
;
1284 highest_tsn
+= ntohs(frags
[gap_ack_blocks
- 1].gab
.end
);
1286 if (TSN_lt(asoc
->highest_sacked
, highest_tsn
))
1287 asoc
->highest_sacked
= highest_tsn
;
1289 highest_new_tsn
= sack_ctsn
;
1291 /* Run through the retransmit queue. Credit bytes received
1292 * and free those chunks that we can.
1294 sctp_check_transmitted(q
, &q
->retransmit
, NULL
, NULL
, sack
, &highest_new_tsn
);
1296 /* Run through the transmitted queue.
1297 * Credit bytes received and free those chunks which we can.
1299 * This is a MASSIVE candidate for optimization.
1301 list_for_each_entry(transport
, transport_list
, transports
) {
1302 sctp_check_transmitted(q
, &transport
->transmitted
,
1303 transport
, &chunk
->source
, sack
,
1306 * SFR-CACC algorithm:
1307 * C) Let count_of_newacks be the number of
1308 * destinations for which cacc_saw_newack is set.
1310 if (transport
->cacc
.cacc_saw_newack
)
1314 /* Move the Cumulative TSN Ack Point if appropriate. */
1315 if (TSN_lt(asoc
->ctsn_ack_point
, sack_ctsn
)) {
1316 asoc
->ctsn_ack_point
= sack_ctsn
;
1320 if (gap_ack_blocks
) {
1322 if (asoc
->fast_recovery
&& accum_moved
)
1323 highest_new_tsn
= highest_tsn
;
1325 list_for_each_entry(transport
, transport_list
, transports
)
1326 sctp_mark_missing(q
, &transport
->transmitted
, transport
,
1327 highest_new_tsn
, count_of_newacks
);
1330 /* Update unack_data field in the assoc. */
1331 sctp_sack_update_unack_data(asoc
, sack
);
1333 ctsn
= asoc
->ctsn_ack_point
;
1335 /* Throw away stuff rotting on the sack queue. */
1336 list_for_each_safe(lchunk
, temp
, &q
->sacked
) {
1337 tchunk
= list_entry(lchunk
, struct sctp_chunk
,
1339 tsn
= ntohl(tchunk
->subh
.data_hdr
->tsn
);
1340 if (TSN_lte(tsn
, ctsn
)) {
1341 list_del_init(&tchunk
->transmitted_list
);
1342 if (asoc
->peer
.prsctp_capable
&&
1343 SCTP_PR_PRIO_ENABLED(chunk
->sinfo
.sinfo_flags
))
1344 asoc
->sent_cnt_removable
--;
1345 sctp_chunk_free(tchunk
);
1349 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1350 * number of bytes still outstanding after processing the
1351 * Cumulative TSN Ack and the Gap Ack Blocks.
1354 sack_a_rwnd
= ntohl(sack
->a_rwnd
);
1355 asoc
->peer
.zero_window_announced
= !sack_a_rwnd
;
1356 outstanding
= q
->outstanding_bytes
;
1358 if (outstanding
< sack_a_rwnd
)
1359 sack_a_rwnd
-= outstanding
;
1363 asoc
->peer
.rwnd
= sack_a_rwnd
;
1365 sctp_generate_fwdtsn(q
, sack_ctsn
);
1367 pr_debug("%s: sack cumulative tsn ack:0x%x\n", __func__
, sack_ctsn
);
1368 pr_debug("%s: cumulative tsn ack of assoc:%p is 0x%x, "
1369 "advertised peer ack point:0x%x\n", __func__
, asoc
, ctsn
,
1370 asoc
->adv_peer_ack_point
);
1372 return sctp_outq_is_empty(q
);
1375 /* Is the outqueue empty?
1376 * The queue is empty when we have not pending data, no in-flight data
1377 * and nothing pending retransmissions.
1379 int sctp_outq_is_empty(const struct sctp_outq
*q
)
1381 return q
->out_qlen
== 0 && q
->outstanding_bytes
== 0 &&
1382 list_empty(&q
->retransmit
);
1385 /********************************************************************
1386 * 2nd Level Abstractions
1387 ********************************************************************/
1389 /* Go through a transport's transmitted list or the association's retransmit
1390 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1391 * The retransmit list will not have an associated transport.
1393 * I added coherent debug information output. --xguo
1395 * Instead of printing 'sacked' or 'kept' for each TSN on the
1396 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1397 * KEPT TSN6-TSN7, etc.
1399 static void sctp_check_transmitted(struct sctp_outq
*q
,
1400 struct list_head
*transmitted_queue
,
1401 struct sctp_transport
*transport
,
1402 union sctp_addr
*saddr
,
1403 struct sctp_sackhdr
*sack
,
1404 __u32
*highest_new_tsn_in_sack
)
1406 struct list_head
*lchunk
;
1407 struct sctp_chunk
*tchunk
;
1408 struct list_head tlist
;
1412 __u8 restart_timer
= 0;
1413 int bytes_acked
= 0;
1414 int migrate_bytes
= 0;
1415 bool forward_progress
= false;
1417 sack_ctsn
= ntohl(sack
->cum_tsn_ack
);
1419 INIT_LIST_HEAD(&tlist
);
1421 /* The while loop will skip empty transmitted queues. */
1422 while (NULL
!= (lchunk
= sctp_list_dequeue(transmitted_queue
))) {
1423 tchunk
= list_entry(lchunk
, struct sctp_chunk
,
1426 if (sctp_chunk_abandoned(tchunk
)) {
1427 /* Move the chunk to abandoned list. */
1428 sctp_insert_list(&q
->abandoned
, lchunk
);
1430 /* If this chunk has not been acked, stop
1431 * considering it as 'outstanding'.
1433 if (transmitted_queue
!= &q
->retransmit
&&
1434 !tchunk
->tsn_gap_acked
) {
1435 if (tchunk
->transport
)
1436 tchunk
->transport
->flight_size
-=
1437 sctp_data_size(tchunk
);
1438 q
->outstanding_bytes
-= sctp_data_size(tchunk
);
1443 tsn
= ntohl(tchunk
->subh
.data_hdr
->tsn
);
1444 if (sctp_acked(sack
, tsn
)) {
1445 /* If this queue is the retransmit queue, the
1446 * retransmit timer has already reclaimed
1447 * the outstanding bytes for this chunk, so only
1448 * count bytes associated with a transport.
1451 /* If this chunk is being used for RTT
1452 * measurement, calculate the RTT and update
1453 * the RTO using this value.
1455 * 6.3.1 C5) Karn's algorithm: RTT measurements
1456 * MUST NOT be made using packets that were
1457 * retransmitted (and thus for which it is
1458 * ambiguous whether the reply was for the
1459 * first instance of the packet or a later
1462 if (!tchunk
->tsn_gap_acked
&&
1463 !sctp_chunk_retransmitted(tchunk
) &&
1464 tchunk
->rtt_in_progress
) {
1465 tchunk
->rtt_in_progress
= 0;
1466 rtt
= jiffies
- tchunk
->sent_at
;
1467 sctp_transport_update_rto(transport
,
1472 /* If the chunk hasn't been marked as ACKED,
1473 * mark it and account bytes_acked if the
1474 * chunk had a valid transport (it will not
1475 * have a transport if ASCONF had deleted it
1476 * while DATA was outstanding).
1478 if (!tchunk
->tsn_gap_acked
) {
1479 tchunk
->tsn_gap_acked
= 1;
1480 if (TSN_lt(*highest_new_tsn_in_sack
, tsn
))
1481 *highest_new_tsn_in_sack
= tsn
;
1482 bytes_acked
+= sctp_data_size(tchunk
);
1483 if (!tchunk
->transport
)
1484 migrate_bytes
+= sctp_data_size(tchunk
);
1485 forward_progress
= true;
1488 if (TSN_lte(tsn
, sack_ctsn
)) {
1489 /* RFC 2960 6.3.2 Retransmission Timer Rules
1491 * R3) Whenever a SACK is received
1492 * that acknowledges the DATA chunk
1493 * with the earliest outstanding TSN
1494 * for that address, restart T3-rtx
1495 * timer for that address with its
1499 forward_progress
= true;
1501 if (!tchunk
->tsn_gap_acked
) {
1503 * SFR-CACC algorithm:
1504 * 2) If the SACK contains gap acks
1505 * and the flag CHANGEOVER_ACTIVE is
1506 * set the receiver of the SACK MUST
1507 * take the following action:
1509 * B) For each TSN t being acked that
1510 * has not been acked in any SACK so
1511 * far, set cacc_saw_newack to 1 for
1512 * the destination that the TSN was
1516 sack
->num_gap_ack_blocks
&&
1517 q
->asoc
->peer
.primary_path
->cacc
.
1519 transport
->cacc
.cacc_saw_newack
1523 list_add_tail(&tchunk
->transmitted_list
,
1526 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1527 * M2) Each time a SACK arrives reporting
1528 * 'Stray DATA chunk(s)' record the highest TSN
1529 * reported as newly acknowledged, call this
1530 * value 'HighestTSNinSack'. A newly
1531 * acknowledged DATA chunk is one not
1532 * previously acknowledged in a SACK.
1534 * When the SCTP sender of data receives a SACK
1535 * chunk that acknowledges, for the first time,
1536 * the receipt of a DATA chunk, all the still
1537 * unacknowledged DATA chunks whose TSN is
1538 * older than that newly acknowledged DATA
1539 * chunk, are qualified as 'Stray DATA chunks'.
1541 list_add_tail(lchunk
, &tlist
);
1544 if (tchunk
->tsn_gap_acked
) {
1545 pr_debug("%s: receiver reneged on data TSN:0x%x\n",
1548 tchunk
->tsn_gap_acked
= 0;
1550 if (tchunk
->transport
)
1551 bytes_acked
-= sctp_data_size(tchunk
);
1553 /* RFC 2960 6.3.2 Retransmission Timer Rules
1555 * R4) Whenever a SACK is received missing a
1556 * TSN that was previously acknowledged via a
1557 * Gap Ack Block, start T3-rtx for the
1558 * destination address to which the DATA
1559 * chunk was originally
1560 * transmitted if it is not already running.
1565 list_add_tail(lchunk
, &tlist
);
1571 struct sctp_association
*asoc
= transport
->asoc
;
1573 /* We may have counted DATA that was migrated
1574 * to this transport due to DEL-IP operation.
1575 * Subtract those bytes, since the were never
1576 * send on this transport and shouldn't be
1577 * credited to this transport.
1579 bytes_acked
-= migrate_bytes
;
1581 /* 8.2. When an outstanding TSN is acknowledged,
1582 * the endpoint shall clear the error counter of
1583 * the destination transport address to which the
1584 * DATA chunk was last sent.
1585 * The association's overall error counter is
1588 transport
->error_count
= 0;
1589 transport
->asoc
->overall_error_count
= 0;
1590 forward_progress
= true;
1593 * While in SHUTDOWN PENDING, we may have started
1594 * the T5 shutdown guard timer after reaching the
1595 * retransmission limit. Stop that timer as soon
1596 * as the receiver acknowledged any data.
1598 if (asoc
->state
== SCTP_STATE_SHUTDOWN_PENDING
&&
1599 del_timer(&asoc
->timers
1600 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD
]))
1601 sctp_association_put(asoc
);
1603 /* Mark the destination transport address as
1604 * active if it is not so marked.
1606 if ((transport
->state
== SCTP_INACTIVE
||
1607 transport
->state
== SCTP_UNCONFIRMED
) &&
1608 sctp_cmp_addr_exact(&transport
->ipaddr
, saddr
)) {
1609 sctp_assoc_control_transport(
1613 SCTP_RECEIVED_SACK
);
1616 sctp_transport_raise_cwnd(transport
, sack_ctsn
,
1619 transport
->flight_size
-= bytes_acked
;
1620 if (transport
->flight_size
== 0)
1621 transport
->partial_bytes_acked
= 0;
1622 q
->outstanding_bytes
-= bytes_acked
+ migrate_bytes
;
1624 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1625 * When a sender is doing zero window probing, it
1626 * should not timeout the association if it continues
1627 * to receive new packets from the receiver. The
1628 * reason is that the receiver MAY keep its window
1629 * closed for an indefinite time.
1630 * A sender is doing zero window probing when the
1631 * receiver's advertised window is zero, and there is
1632 * only one data chunk in flight to the receiver.
1634 * Allow the association to timeout while in SHUTDOWN
1635 * PENDING or SHUTDOWN RECEIVED in case the receiver
1636 * stays in zero window mode forever.
1638 if (!q
->asoc
->peer
.rwnd
&&
1639 !list_empty(&tlist
) &&
1640 (sack_ctsn
+2 == q
->asoc
->next_tsn
) &&
1641 q
->asoc
->state
< SCTP_STATE_SHUTDOWN_PENDING
) {
1642 pr_debug("%s: sack received for zero window "
1643 "probe:%u\n", __func__
, sack_ctsn
);
1645 q
->asoc
->overall_error_count
= 0;
1646 transport
->error_count
= 0;
1650 /* RFC 2960 6.3.2 Retransmission Timer Rules
1652 * R2) Whenever all outstanding data sent to an address have
1653 * been acknowledged, turn off the T3-rtx timer of that
1656 if (!transport
->flight_size
) {
1657 if (del_timer(&transport
->T3_rtx_timer
))
1658 sctp_transport_put(transport
);
1659 } else if (restart_timer
) {
1660 if (!mod_timer(&transport
->T3_rtx_timer
,
1661 jiffies
+ transport
->rto
))
1662 sctp_transport_hold(transport
);
1665 if (forward_progress
) {
1667 sctp_transport_dst_confirm(transport
);
1671 list_splice(&tlist
, transmitted_queue
);
1674 /* Mark chunks as missing and consequently may get retransmitted. */
1675 static void sctp_mark_missing(struct sctp_outq
*q
,
1676 struct list_head
*transmitted_queue
,
1677 struct sctp_transport
*transport
,
1678 __u32 highest_new_tsn_in_sack
,
1679 int count_of_newacks
)
1681 struct sctp_chunk
*chunk
;
1683 char do_fast_retransmit
= 0;
1684 struct sctp_association
*asoc
= q
->asoc
;
1685 struct sctp_transport
*primary
= asoc
->peer
.primary_path
;
1687 list_for_each_entry(chunk
, transmitted_queue
, transmitted_list
) {
1689 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1691 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1692 * 'Unacknowledged TSN's', if the TSN number of an
1693 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1694 * value, increment the 'TSN.Missing.Report' count on that
1695 * chunk if it has NOT been fast retransmitted or marked for
1696 * fast retransmit already.
1698 if (chunk
->fast_retransmit
== SCTP_CAN_FRTX
&&
1699 !chunk
->tsn_gap_acked
&&
1700 TSN_lt(tsn
, highest_new_tsn_in_sack
)) {
1702 /* SFR-CACC may require us to skip marking
1703 * this chunk as missing.
1705 if (!transport
|| !sctp_cacc_skip(primary
,
1707 count_of_newacks
, tsn
)) {
1708 chunk
->tsn_missing_report
++;
1710 pr_debug("%s: tsn:0x%x missing counter:%d\n",
1711 __func__
, tsn
, chunk
->tsn_missing_report
);
1715 * M4) If any DATA chunk is found to have a
1716 * 'TSN.Missing.Report'
1717 * value larger than or equal to 3, mark that chunk for
1718 * retransmission and start the fast retransmit procedure.
1721 if (chunk
->tsn_missing_report
>= 3) {
1722 chunk
->fast_retransmit
= SCTP_NEED_FRTX
;
1723 do_fast_retransmit
= 1;
1728 if (do_fast_retransmit
)
1729 sctp_retransmit(q
, transport
, SCTP_RTXR_FAST_RTX
);
1731 pr_debug("%s: transport:%p, cwnd:%d, ssthresh:%d, "
1732 "flight_size:%d, pba:%d\n", __func__
, transport
,
1733 transport
->cwnd
, transport
->ssthresh
,
1734 transport
->flight_size
, transport
->partial_bytes_acked
);
1738 /* Is the given TSN acked by this packet? */
1739 static int sctp_acked(struct sctp_sackhdr
*sack
, __u32 tsn
)
1741 __u32 ctsn
= ntohl(sack
->cum_tsn_ack
);
1742 union sctp_sack_variable
*frags
;
1743 __u16 tsn_offset
, blocks
;
1746 if (TSN_lte(tsn
, ctsn
))
1749 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1752 * These fields contain the Gap Ack Blocks. They are repeated
1753 * for each Gap Ack Block up to the number of Gap Ack Blocks
1754 * defined in the Number of Gap Ack Blocks field. All DATA
1755 * chunks with TSNs greater than or equal to (Cumulative TSN
1756 * Ack + Gap Ack Block Start) and less than or equal to
1757 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1758 * Block are assumed to have been received correctly.
1761 frags
= sack
->variable
;
1762 blocks
= ntohs(sack
->num_gap_ack_blocks
);
1763 tsn_offset
= tsn
- ctsn
;
1764 for (i
= 0; i
< blocks
; ++i
) {
1765 if (tsn_offset
>= ntohs(frags
[i
].gab
.start
) &&
1766 tsn_offset
<= ntohs(frags
[i
].gab
.end
))
1775 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip
*skiplist
,
1776 int nskips
, __be16 stream
)
1780 for (i
= 0; i
< nskips
; i
++) {
1781 if (skiplist
[i
].stream
== stream
)
1787 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1788 static void sctp_generate_fwdtsn(struct sctp_outq
*q
, __u32 ctsn
)
1790 struct sctp_association
*asoc
= q
->asoc
;
1791 struct sctp_chunk
*ftsn_chunk
= NULL
;
1792 struct sctp_fwdtsn_skip ftsn_skip_arr
[10];
1796 struct sctp_chunk
*chunk
;
1797 struct list_head
*lchunk
, *temp
;
1799 if (!asoc
->peer
.prsctp_capable
)
1802 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1805 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1806 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1808 if (TSN_lt(asoc
->adv_peer_ack_point
, ctsn
))
1809 asoc
->adv_peer_ack_point
= ctsn
;
1811 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1812 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1813 * the chunk next in the out-queue space is marked as "abandoned" as
1814 * shown in the following example:
1816 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1817 * and the Advanced.Peer.Ack.Point is updated to this value:
1819 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1820 * normal SACK processing local advancement
1822 * Adv.Ack.Pt-> 102 acked 102 acked
1823 * 103 abandoned 103 abandoned
1824 * 104 abandoned Adv.Ack.P-> 104 abandoned
1826 * 106 acked 106 acked
1829 * In this example, the data sender successfully advanced the
1830 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1832 list_for_each_safe(lchunk
, temp
, &q
->abandoned
) {
1833 chunk
= list_entry(lchunk
, struct sctp_chunk
,
1835 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1837 /* Remove any chunks in the abandoned queue that are acked by
1840 if (TSN_lte(tsn
, ctsn
)) {
1841 list_del_init(lchunk
);
1842 sctp_chunk_free(chunk
);
1844 if (TSN_lte(tsn
, asoc
->adv_peer_ack_point
+1)) {
1845 asoc
->adv_peer_ack_point
= tsn
;
1846 if (chunk
->chunk_hdr
->flags
&
1847 SCTP_DATA_UNORDERED
)
1849 skip_pos
= sctp_get_skip_pos(&ftsn_skip_arr
[0],
1851 chunk
->subh
.data_hdr
->stream
);
1852 ftsn_skip_arr
[skip_pos
].stream
=
1853 chunk
->subh
.data_hdr
->stream
;
1854 ftsn_skip_arr
[skip_pos
].ssn
=
1855 chunk
->subh
.data_hdr
->ssn
;
1856 if (skip_pos
== nskips
)
1865 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1866 * is greater than the Cumulative TSN ACK carried in the received
1867 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1868 * chunk containing the latest value of the
1869 * "Advanced.Peer.Ack.Point".
1871 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1872 * list each stream and sequence number in the forwarded TSN. This
1873 * information will enable the receiver to easily find any
1874 * stranded TSN's waiting on stream reorder queues. Each stream
1875 * SHOULD only be reported once; this means that if multiple
1876 * abandoned messages occur in the same stream then only the
1877 * highest abandoned stream sequence number is reported. If the
1878 * total size of the FORWARD TSN does NOT fit in a single MTU then
1879 * the sender of the FORWARD TSN SHOULD lower the
1880 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1883 if (asoc
->adv_peer_ack_point
> ctsn
)
1884 ftsn_chunk
= sctp_make_fwdtsn(asoc
, asoc
->adv_peer_ack_point
,
1885 nskips
, &ftsn_skip_arr
[0]);
1888 list_add_tail(&ftsn_chunk
->list
, &q
->control_chunk_list
);
1889 SCTP_INC_STATS(sock_net(asoc
->base
.sk
), SCTP_MIB_OUTCTRLCHUNKS
);