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 int 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 int sctp_outq_tail(struct sctp_outq
*q
, struct sctp_chunk
*chunk
, gfp_t gfp
)
290 struct net
*net
= sock_net(q
->asoc
->base
.sk
);
293 pr_debug("%s: outq:%p, chunk:%p[%s]\n", __func__
, q
, chunk
,
294 chunk
&& chunk
->chunk_hdr
?
295 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
)) :
298 /* If it is data, queue it up, otherwise, send it
301 if (sctp_chunk_is_data(chunk
)) {
302 /* Is it OK to queue data chunks? */
303 /* From 9. Termination of Association
305 * When either endpoint performs a shutdown, the
306 * association on each peer will stop accepting new
307 * data from its user and only deliver data in queue
308 * at the time of sending or receiving the SHUTDOWN
311 switch (q
->asoc
->state
) {
312 case SCTP_STATE_CLOSED
:
313 case SCTP_STATE_SHUTDOWN_PENDING
:
314 case SCTP_STATE_SHUTDOWN_SENT
:
315 case SCTP_STATE_SHUTDOWN_RECEIVED
:
316 case SCTP_STATE_SHUTDOWN_ACK_SENT
:
317 /* Cannot send after transport endpoint shutdown */
322 pr_debug("%s: outqueueing: outq:%p, chunk:%p[%s])\n",
323 __func__
, q
, chunk
, chunk
&& chunk
->chunk_hdr
?
324 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
)) :
327 sctp_chunk_hold(chunk
);
328 sctp_outq_tail_data(q
, chunk
);
329 if (chunk
->asoc
->prsctp_enable
&&
330 SCTP_PR_PRIO_ENABLED(chunk
->sinfo
.sinfo_flags
))
331 chunk
->asoc
->sent_cnt_removable
++;
332 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
333 SCTP_INC_STATS(net
, SCTP_MIB_OUTUNORDERCHUNKS
);
335 SCTP_INC_STATS(net
, SCTP_MIB_OUTORDERCHUNKS
);
339 list_add_tail(&chunk
->list
, &q
->control_chunk_list
);
340 SCTP_INC_STATS(net
, SCTP_MIB_OUTCTRLCHUNKS
);
347 error
= sctp_outq_flush(q
, 0, gfp
);
352 /* Insert a chunk into the sorted list based on the TSNs. The retransmit list
353 * and the abandoned list are in ascending order.
355 static void sctp_insert_list(struct list_head
*head
, struct list_head
*new)
357 struct list_head
*pos
;
358 struct sctp_chunk
*nchunk
, *lchunk
;
362 nchunk
= list_entry(new, struct sctp_chunk
, transmitted_list
);
363 ntsn
= ntohl(nchunk
->subh
.data_hdr
->tsn
);
365 list_for_each(pos
, head
) {
366 lchunk
= list_entry(pos
, struct sctp_chunk
, transmitted_list
);
367 ltsn
= ntohl(lchunk
->subh
.data_hdr
->tsn
);
368 if (TSN_lt(ntsn
, ltsn
)) {
369 list_add(new, pos
->prev
);
375 list_add_tail(new, head
);
378 static int sctp_prsctp_prune_sent(struct sctp_association
*asoc
,
379 struct sctp_sndrcvinfo
*sinfo
,
380 struct list_head
*queue
, int msg_len
)
382 struct sctp_chunk
*chk
, *temp
;
384 list_for_each_entry_safe(chk
, temp
, queue
, transmitted_list
) {
385 if (!SCTP_PR_PRIO_ENABLED(chk
->sinfo
.sinfo_flags
) ||
386 chk
->prsctp_param
<= sinfo
->sinfo_timetolive
)
389 list_del_init(&chk
->transmitted_list
);
390 sctp_insert_list(&asoc
->outqueue
.abandoned
,
391 &chk
->transmitted_list
);
393 asoc
->sent_cnt_removable
--;
394 asoc
->abandoned_sent
[SCTP_PR_INDEX(PRIO
)]++;
396 if (!chk
->tsn_gap_acked
) {
398 chk
->transport
->flight_size
-=
400 asoc
->outqueue
.outstanding_bytes
-= sctp_data_size(chk
);
403 msg_len
-= SCTP_DATA_SNDSIZE(chk
) +
404 sizeof(struct sk_buff
) +
405 sizeof(struct sctp_chunk
);
413 static int sctp_prsctp_prune_unsent(struct sctp_association
*asoc
,
414 struct sctp_sndrcvinfo
*sinfo
,
415 struct list_head
*queue
, int msg_len
)
417 struct sctp_chunk
*chk
, *temp
;
419 list_for_each_entry_safe(chk
, temp
, queue
, list
) {
420 if (!SCTP_PR_PRIO_ENABLED(chk
->sinfo
.sinfo_flags
) ||
421 chk
->prsctp_param
<= sinfo
->sinfo_timetolive
)
424 list_del_init(&chk
->list
);
425 asoc
->sent_cnt_removable
--;
426 asoc
->abandoned_unsent
[SCTP_PR_INDEX(PRIO
)]++;
428 msg_len
-= SCTP_DATA_SNDSIZE(chk
) +
429 sizeof(struct sk_buff
) +
430 sizeof(struct sctp_chunk
);
431 sctp_chunk_free(chk
);
439 /* Abandon the chunks according their priorities */
440 void sctp_prsctp_prune(struct sctp_association
*asoc
,
441 struct sctp_sndrcvinfo
*sinfo
, int msg_len
)
443 struct sctp_transport
*transport
;
445 if (!asoc
->prsctp_enable
|| !asoc
->sent_cnt_removable
)
448 msg_len
= sctp_prsctp_prune_sent(asoc
, sinfo
,
449 &asoc
->outqueue
.retransmit
,
454 list_for_each_entry(transport
, &asoc
->peer
.transport_addr_list
,
456 msg_len
= sctp_prsctp_prune_sent(asoc
, sinfo
,
457 &transport
->transmitted
,
463 sctp_prsctp_prune_unsent(asoc
, sinfo
,
464 &asoc
->outqueue
.out_chunk_list
,
468 /* Mark all the eligible packets on a transport for retransmission. */
469 void sctp_retransmit_mark(struct sctp_outq
*q
,
470 struct sctp_transport
*transport
,
473 struct list_head
*lchunk
, *ltemp
;
474 struct sctp_chunk
*chunk
;
476 /* Walk through the specified transmitted queue. */
477 list_for_each_safe(lchunk
, ltemp
, &transport
->transmitted
) {
478 chunk
= list_entry(lchunk
, struct sctp_chunk
,
481 /* If the chunk is abandoned, move it to abandoned list. */
482 if (sctp_chunk_abandoned(chunk
)) {
483 list_del_init(lchunk
);
484 sctp_insert_list(&q
->abandoned
, lchunk
);
486 /* If this chunk has not been previousely acked,
487 * stop considering it 'outstanding'. Our peer
488 * will most likely never see it since it will
489 * not be retransmitted
491 if (!chunk
->tsn_gap_acked
) {
492 if (chunk
->transport
)
493 chunk
->transport
->flight_size
-=
494 sctp_data_size(chunk
);
495 q
->outstanding_bytes
-= sctp_data_size(chunk
);
496 q
->asoc
->peer
.rwnd
+= sctp_data_size(chunk
);
501 /* If we are doing retransmission due to a timeout or pmtu
502 * discovery, only the chunks that are not yet acked should
503 * be added to the retransmit queue.
505 if ((reason
== SCTP_RTXR_FAST_RTX
&&
506 (chunk
->fast_retransmit
== SCTP_NEED_FRTX
)) ||
507 (reason
!= SCTP_RTXR_FAST_RTX
&& !chunk
->tsn_gap_acked
)) {
508 /* RFC 2960 6.2.1 Processing a Received SACK
510 * C) Any time a DATA chunk is marked for
511 * retransmission (via either T3-rtx timer expiration
512 * (Section 6.3.3) or via fast retransmit
513 * (Section 7.2.4)), add the data size of those
514 * chunks to the rwnd.
516 q
->asoc
->peer
.rwnd
+= sctp_data_size(chunk
);
517 q
->outstanding_bytes
-= sctp_data_size(chunk
);
518 if (chunk
->transport
)
519 transport
->flight_size
-= sctp_data_size(chunk
);
521 /* sctpimpguide-05 Section 2.8.2
522 * M5) If a T3-rtx timer expires, the
523 * 'TSN.Missing.Report' of all affected TSNs is set
526 chunk
->tsn_missing_report
= 0;
528 /* If a chunk that is being used for RTT measurement
529 * has to be retransmitted, we cannot use this chunk
530 * anymore for RTT measurements. Reset rto_pending so
531 * that a new RTT measurement is started when a new
532 * data chunk is sent.
534 if (chunk
->rtt_in_progress
) {
535 chunk
->rtt_in_progress
= 0;
536 transport
->rto_pending
= 0;
541 /* Move the chunk to the retransmit queue. The chunks
542 * on the retransmit queue are always kept in order.
544 list_del_init(lchunk
);
545 sctp_insert_list(&q
->retransmit
, lchunk
);
549 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d, "
550 "flight_size:%d, pba:%d\n", __func__
, transport
, reason
,
551 transport
->cwnd
, transport
->ssthresh
, transport
->flight_size
,
552 transport
->partial_bytes_acked
);
555 /* Mark all the eligible packets on a transport for retransmission and force
558 void sctp_retransmit(struct sctp_outq
*q
, struct sctp_transport
*transport
,
559 sctp_retransmit_reason_t reason
)
561 struct net
*net
= sock_net(q
->asoc
->base
.sk
);
565 case SCTP_RTXR_T3_RTX
:
566 SCTP_INC_STATS(net
, SCTP_MIB_T3_RETRANSMITS
);
567 sctp_transport_lower_cwnd(transport
, SCTP_LOWER_CWND_T3_RTX
);
568 /* Update the retran path if the T3-rtx timer has expired for
569 * the current retran path.
571 if (transport
== transport
->asoc
->peer
.retran_path
)
572 sctp_assoc_update_retran_path(transport
->asoc
);
573 transport
->asoc
->rtx_data_chunks
+=
574 transport
->asoc
->unack_data
;
576 case SCTP_RTXR_FAST_RTX
:
577 SCTP_INC_STATS(net
, SCTP_MIB_FAST_RETRANSMITS
);
578 sctp_transport_lower_cwnd(transport
, SCTP_LOWER_CWND_FAST_RTX
);
581 case SCTP_RTXR_PMTUD
:
582 SCTP_INC_STATS(net
, SCTP_MIB_PMTUD_RETRANSMITS
);
584 case SCTP_RTXR_T1_RTX
:
585 SCTP_INC_STATS(net
, SCTP_MIB_T1_RETRANSMITS
);
586 transport
->asoc
->init_retries
++;
592 sctp_retransmit_mark(q
, transport
, reason
);
594 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
595 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
596 * following the procedures outlined in C1 - C5.
598 if (reason
== SCTP_RTXR_T3_RTX
)
599 sctp_generate_fwdtsn(q
, q
->asoc
->ctsn_ack_point
);
601 /* Flush the queues only on timeout, since fast_rtx is only
602 * triggered during sack processing and the queue
603 * will be flushed at the end.
605 if (reason
!= SCTP_RTXR_FAST_RTX
)
606 error
= sctp_outq_flush(q
, /* rtx_timeout */ 1, GFP_ATOMIC
);
609 q
->asoc
->base
.sk
->sk_err
= -error
;
613 * Transmit DATA chunks on the retransmit queue. Upon return from
614 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
615 * need to be transmitted by the caller.
616 * We assume that pkt->transport has already been set.
618 * The return value is a normal kernel error return value.
620 static int sctp_outq_flush_rtx(struct sctp_outq
*q
, struct sctp_packet
*pkt
,
621 int rtx_timeout
, int *start_timer
)
623 struct list_head
*lqueue
;
624 struct sctp_transport
*transport
= pkt
->transport
;
626 struct sctp_chunk
*chunk
, *chunk1
;
632 lqueue
= &q
->retransmit
;
633 fast_rtx
= q
->fast_rtx
;
635 /* This loop handles time-out retransmissions, fast retransmissions,
636 * and retransmissions due to opening of whindow.
638 * RFC 2960 6.3.3 Handle T3-rtx Expiration
640 * E3) Determine how many of the earliest (i.e., lowest TSN)
641 * outstanding DATA chunks for the address for which the
642 * T3-rtx has expired will fit into a single packet, subject
643 * to the MTU constraint for the path corresponding to the
644 * destination transport address to which the retransmission
645 * is being sent (this may be different from the address for
646 * which the timer expires [see Section 6.4]). Call this value
647 * K. Bundle and retransmit those K DATA chunks in a single
648 * packet to the destination endpoint.
650 * [Just to be painfully clear, if we are retransmitting
651 * because a timeout just happened, we should send only ONE
652 * packet of retransmitted data.]
654 * For fast retransmissions we also send only ONE packet. However,
655 * if we are just flushing the queue due to open window, we'll
656 * try to send as much as possible.
658 list_for_each_entry_safe(chunk
, chunk1
, lqueue
, transmitted_list
) {
659 /* If the chunk is abandoned, move it to abandoned list. */
660 if (sctp_chunk_abandoned(chunk
)) {
661 list_del_init(&chunk
->transmitted_list
);
662 sctp_insert_list(&q
->abandoned
,
663 &chunk
->transmitted_list
);
667 /* Make sure that Gap Acked TSNs are not retransmitted. A
668 * simple approach is just to move such TSNs out of the
669 * way and into a 'transmitted' queue and skip to the
672 if (chunk
->tsn_gap_acked
) {
673 list_move_tail(&chunk
->transmitted_list
,
674 &transport
->transmitted
);
678 /* If we are doing fast retransmit, ignore non-fast_rtransmit
681 if (fast_rtx
&& !chunk
->fast_retransmit
)
685 /* Attempt to append this chunk to the packet. */
686 status
= sctp_packet_append_chunk(pkt
, chunk
);
689 case SCTP_XMIT_PMTU_FULL
:
690 if (!pkt
->has_data
&& !pkt
->has_cookie_echo
) {
691 /* If this packet did not contain DATA then
692 * retransmission did not happen, so do it
693 * again. We'll ignore the error here since
694 * control chunks are already freed so there
695 * is nothing we can do.
697 sctp_packet_transmit(pkt
, GFP_ATOMIC
);
701 /* Send this packet. */
702 error
= sctp_packet_transmit(pkt
, GFP_ATOMIC
);
704 /* If we are retransmitting, we should only
705 * send a single packet.
706 * Otherwise, try appending this chunk again.
708 if (rtx_timeout
|| fast_rtx
)
713 /* Bundle next chunk in the next round. */
716 case SCTP_XMIT_RWND_FULL
:
717 /* Send this packet. */
718 error
= sctp_packet_transmit(pkt
, GFP_ATOMIC
);
720 /* Stop sending DATA as there is no more room
726 case SCTP_XMIT_DELAY
:
727 /* Send this packet. */
728 error
= sctp_packet_transmit(pkt
, GFP_ATOMIC
);
730 /* Stop sending DATA because of nagle delay. */
735 /* The append was successful, so add this chunk to
736 * the transmitted list.
738 list_move_tail(&chunk
->transmitted_list
,
739 &transport
->transmitted
);
741 /* Mark the chunk as ineligible for fast retransmit
742 * after it is retransmitted.
744 if (chunk
->fast_retransmit
== SCTP_NEED_FRTX
)
745 chunk
->fast_retransmit
= SCTP_DONT_FRTX
;
747 q
->asoc
->stats
.rtxchunks
++;
751 /* Set the timer if there were no errors */
752 if (!error
&& !timer
)
759 /* If we are here due to a retransmit timeout or a fast
760 * retransmit and if there are any chunks left in the retransmit
761 * queue that could not fit in the PMTU sized packet, they need
762 * to be marked as ineligible for a subsequent fast retransmit.
764 if (rtx_timeout
|| fast_rtx
) {
765 list_for_each_entry(chunk1
, lqueue
, transmitted_list
) {
766 if (chunk1
->fast_retransmit
== SCTP_NEED_FRTX
)
767 chunk1
->fast_retransmit
= SCTP_DONT_FRTX
;
771 *start_timer
= timer
;
773 /* Clear fast retransmit hint */
780 /* Cork the outqueue so queued chunks are really queued. */
781 int sctp_outq_uncork(struct sctp_outq
*q
, gfp_t gfp
)
786 return sctp_outq_flush(q
, 0, gfp
);
791 * Try to flush an outqueue.
793 * Description: Send everything in q which we legally can, subject to
794 * congestion limitations.
795 * * Note: This function can be called from multiple contexts so appropriate
796 * locking concerns must be made. Today we use the sock lock to protect
799 static int sctp_outq_flush(struct sctp_outq
*q
, int rtx_timeout
, gfp_t gfp
)
801 struct sctp_packet
*packet
;
802 struct sctp_packet singleton
;
803 struct sctp_association
*asoc
= q
->asoc
;
804 __u16 sport
= asoc
->base
.bind_addr
.port
;
805 __u16 dport
= asoc
->peer
.port
;
806 __u32 vtag
= asoc
->peer
.i
.init_tag
;
807 struct sctp_transport
*transport
= NULL
;
808 struct sctp_transport
*new_transport
;
809 struct sctp_chunk
*chunk
, *tmp
;
815 /* These transports have chunks to send. */
816 struct list_head transport_list
;
817 struct list_head
*ltransport
;
819 INIT_LIST_HEAD(&transport_list
);
825 * When bundling control chunks with DATA chunks, an
826 * endpoint MUST place control chunks first in the outbound
827 * SCTP packet. The transmitter MUST transmit DATA chunks
828 * within a SCTP packet in increasing order of TSN.
832 list_for_each_entry_safe(chunk
, tmp
, &q
->control_chunk_list
, list
) {
834 * F1) This means that until such time as the ASCONF
835 * containing the add is acknowledged, the sender MUST
836 * NOT use the new IP address as a source for ANY SCTP
837 * packet except on carrying an ASCONF Chunk.
839 if (asoc
->src_out_of_asoc_ok
&&
840 chunk
->chunk_hdr
->type
!= SCTP_CID_ASCONF
)
843 list_del_init(&chunk
->list
);
845 /* Pick the right transport to use. */
846 new_transport
= chunk
->transport
;
848 if (!new_transport
) {
850 * If we have a prior transport pointer, see if
851 * the destination address of the chunk
852 * matches the destination address of the
853 * current transport. If not a match, then
854 * try to look up the transport with a given
855 * destination address. We do this because
856 * after processing ASCONFs, we may have new
857 * transports created.
860 sctp_cmp_addr_exact(&chunk
->dest
,
862 new_transport
= transport
;
864 new_transport
= sctp_assoc_lookup_paddr(asoc
,
867 /* if we still don't have a new transport, then
868 * use the current active path.
871 new_transport
= asoc
->peer
.active_path
;
872 } else if ((new_transport
->state
== SCTP_INACTIVE
) ||
873 (new_transport
->state
== SCTP_UNCONFIRMED
) ||
874 (new_transport
->state
== SCTP_PF
)) {
875 /* If the chunk is Heartbeat or Heartbeat Ack,
876 * send it to chunk->transport, even if it's
879 * 3.3.6 Heartbeat Acknowledgement:
881 * A HEARTBEAT ACK is always sent to the source IP
882 * address of the IP datagram containing the
883 * HEARTBEAT chunk to which this ack is responding.
886 * ASCONF_ACKs also must be sent to the source.
888 if (chunk
->chunk_hdr
->type
!= SCTP_CID_HEARTBEAT
&&
889 chunk
->chunk_hdr
->type
!= SCTP_CID_HEARTBEAT_ACK
&&
890 chunk
->chunk_hdr
->type
!= SCTP_CID_ASCONF_ACK
)
891 new_transport
= asoc
->peer
.active_path
;
894 /* Are we switching transports?
895 * Take care of transport locks.
897 if (new_transport
!= transport
) {
898 transport
= new_transport
;
899 if (list_empty(&transport
->send_ready
)) {
900 list_add_tail(&transport
->send_ready
,
903 packet
= &transport
->packet
;
904 sctp_packet_config(packet
, vtag
,
905 asoc
->peer
.ecn_capable
);
908 switch (chunk
->chunk_hdr
->type
) {
912 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
913 * COMPLETE with any other chunks. [Send them immediately.]
916 case SCTP_CID_INIT_ACK
:
917 case SCTP_CID_SHUTDOWN_COMPLETE
:
918 sctp_packet_init(&singleton
, transport
, sport
, dport
);
919 sctp_packet_config(&singleton
, vtag
, 0);
920 sctp_packet_append_chunk(&singleton
, chunk
);
921 error
= sctp_packet_transmit(&singleton
, gfp
);
927 if (sctp_test_T_bit(chunk
)) {
928 packet
->vtag
= asoc
->c
.my_vtag
;
930 /* The following chunks are "response" chunks, i.e.
931 * they are generated in response to something we
932 * received. If we are sending these, then we can
933 * send only 1 packet containing these chunks.
935 case SCTP_CID_HEARTBEAT_ACK
:
936 case SCTP_CID_SHUTDOWN_ACK
:
937 case SCTP_CID_COOKIE_ACK
:
938 case SCTP_CID_COOKIE_ECHO
:
940 case SCTP_CID_ECN_CWR
:
941 case SCTP_CID_ASCONF_ACK
:
946 case SCTP_CID_HEARTBEAT
:
947 case SCTP_CID_SHUTDOWN
:
948 case SCTP_CID_ECN_ECNE
:
949 case SCTP_CID_ASCONF
:
950 case SCTP_CID_FWD_TSN
:
951 status
= sctp_packet_transmit_chunk(packet
, chunk
,
953 if (status
!= SCTP_XMIT_OK
) {
954 /* put the chunk back */
955 list_add(&chunk
->list
, &q
->control_chunk_list
);
957 asoc
->stats
.octrlchunks
++;
958 /* PR-SCTP C5) If a FORWARD TSN is sent, the
959 * sender MUST assure that at least one T3-rtx
962 if (chunk
->chunk_hdr
->type
== SCTP_CID_FWD_TSN
) {
963 sctp_transport_reset_t3_rtx(transport
);
964 transport
->last_time_sent
= jiffies
;
970 /* We built a chunk with an illegal type! */
975 if (q
->asoc
->src_out_of_asoc_ok
)
978 /* Is it OK to send data chunks? */
979 switch (asoc
->state
) {
980 case SCTP_STATE_COOKIE_ECHOED
:
981 /* Only allow bundling when this packet has a COOKIE-ECHO
984 if (!packet
|| !packet
->has_cookie_echo
)
988 case SCTP_STATE_ESTABLISHED
:
989 case SCTP_STATE_SHUTDOWN_PENDING
:
990 case SCTP_STATE_SHUTDOWN_RECEIVED
:
992 * RFC 2960 6.1 Transmission of DATA Chunks
994 * C) When the time comes for the sender to transmit,
995 * before sending new DATA chunks, the sender MUST
996 * first transmit any outstanding DATA chunks which
997 * are marked for retransmission (limited by the
1000 if (!list_empty(&q
->retransmit
)) {
1001 if (asoc
->peer
.retran_path
->state
== SCTP_UNCONFIRMED
)
1002 goto sctp_flush_out
;
1003 if (transport
== asoc
->peer
.retran_path
)
1006 /* Switch transports & prepare the packet. */
1008 transport
= asoc
->peer
.retran_path
;
1010 if (list_empty(&transport
->send_ready
)) {
1011 list_add_tail(&transport
->send_ready
,
1015 packet
= &transport
->packet
;
1016 sctp_packet_config(packet
, vtag
,
1017 asoc
->peer
.ecn_capable
);
1019 error
= sctp_outq_flush_rtx(q
, packet
,
1020 rtx_timeout
, &start_timer
);
1023 sctp_transport_reset_t3_rtx(transport
);
1024 transport
->last_time_sent
= jiffies
;
1027 /* This can happen on COOKIE-ECHO resend. Only
1028 * one chunk can get bundled with a COOKIE-ECHO.
1030 if (packet
->has_cookie_echo
)
1031 goto sctp_flush_out
;
1033 /* Don't send new data if there is still data
1034 * waiting to retransmit.
1036 if (!list_empty(&q
->retransmit
))
1037 goto sctp_flush_out
;
1040 /* Apply Max.Burst limitation to the current transport in
1041 * case it will be used for new data. We are going to
1042 * rest it before we return, but we want to apply the limit
1043 * to the currently queued data.
1046 sctp_transport_burst_limited(transport
);
1048 /* Finally, transmit new packets. */
1049 while ((chunk
= sctp_outq_dequeue_data(q
)) != NULL
) {
1050 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
1051 * stream identifier.
1053 if (chunk
->sinfo
.sinfo_stream
>=
1054 asoc
->c
.sinit_num_ostreams
) {
1056 /* Mark as failed send. */
1057 sctp_chunk_fail(chunk
, SCTP_ERROR_INV_STRM
);
1058 if (asoc
->prsctp_enable
&&
1059 SCTP_PR_PRIO_ENABLED(chunk
->sinfo
.sinfo_flags
))
1060 asoc
->sent_cnt_removable
--;
1061 sctp_chunk_free(chunk
);
1065 /* Has this chunk expired? */
1066 if (sctp_chunk_abandoned(chunk
)) {
1067 sctp_chunk_fail(chunk
, 0);
1068 sctp_chunk_free(chunk
);
1072 /* If there is a specified transport, use it.
1073 * Otherwise, we want to use the active path.
1075 new_transport
= chunk
->transport
;
1076 if (!new_transport
||
1077 ((new_transport
->state
== SCTP_INACTIVE
) ||
1078 (new_transport
->state
== SCTP_UNCONFIRMED
) ||
1079 (new_transport
->state
== SCTP_PF
)))
1080 new_transport
= asoc
->peer
.active_path
;
1081 if (new_transport
->state
== SCTP_UNCONFIRMED
) {
1082 WARN_ONCE(1, "Atempt to send packet on unconfirmed path.");
1083 sctp_chunk_fail(chunk
, 0);
1084 sctp_chunk_free(chunk
);
1088 /* Change packets if necessary. */
1089 if (new_transport
!= transport
) {
1090 transport
= new_transport
;
1092 /* Schedule to have this transport's
1095 if (list_empty(&transport
->send_ready
)) {
1096 list_add_tail(&transport
->send_ready
,
1100 packet
= &transport
->packet
;
1101 sctp_packet_config(packet
, vtag
,
1102 asoc
->peer
.ecn_capable
);
1103 /* We've switched transports, so apply the
1104 * Burst limit to the new transport.
1106 sctp_transport_burst_limited(transport
);
1109 pr_debug("%s: outq:%p, chunk:%p[%s], tx-tsn:0x%x skb->head:%p "
1111 __func__
, q
, chunk
, chunk
&& chunk
->chunk_hdr
?
1112 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
)) :
1113 "illegal chunk", ntohl(chunk
->subh
.data_hdr
->tsn
),
1114 chunk
->skb
? chunk
->skb
->head
: NULL
, chunk
->skb
?
1115 atomic_read(&chunk
->skb
->users
) : -1);
1117 /* Add the chunk to the packet. */
1118 status
= sctp_packet_transmit_chunk(packet
, chunk
, 0, gfp
);
1121 case SCTP_XMIT_PMTU_FULL
:
1122 case SCTP_XMIT_RWND_FULL
:
1123 case SCTP_XMIT_DELAY
:
1124 /* We could not append this chunk, so put
1125 * the chunk back on the output queue.
1127 pr_debug("%s: could not transmit tsn:0x%x, status:%d\n",
1128 __func__
, ntohl(chunk
->subh
.data_hdr
->tsn
),
1131 sctp_outq_head_data(q
, chunk
);
1132 goto sctp_flush_out
;
1135 /* The sender is in the SHUTDOWN-PENDING state,
1136 * The sender MAY set the I-bit in the DATA
1139 if (asoc
->state
== SCTP_STATE_SHUTDOWN_PENDING
)
1140 chunk
->chunk_hdr
->flags
|= SCTP_DATA_SACK_IMM
;
1141 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
1142 asoc
->stats
.ouodchunks
++;
1144 asoc
->stats
.oodchunks
++;
1152 /* BUG: We assume that the sctp_packet_transmit()
1153 * call below will succeed all the time and add the
1154 * chunk to the transmitted list and restart the
1156 * It is possible that the call can fail under OOM
1159 * Is this really a problem? Won't this behave
1162 list_add_tail(&chunk
->transmitted_list
,
1163 &transport
->transmitted
);
1165 sctp_transport_reset_t3_rtx(transport
);
1166 transport
->last_time_sent
= jiffies
;
1168 /* Only let one DATA chunk get bundled with a
1169 * COOKIE-ECHO chunk.
1171 if (packet
->has_cookie_echo
)
1172 goto sctp_flush_out
;
1183 /* Before returning, examine all the transports touched in
1184 * this call. Right now, we bluntly force clear all the
1185 * transports. Things might change after we implement Nagle.
1186 * But such an examination is still required.
1190 while ((ltransport
= sctp_list_dequeue(&transport_list
)) != NULL
) {
1191 struct sctp_transport
*t
= list_entry(ltransport
,
1192 struct sctp_transport
,
1194 packet
= &t
->packet
;
1195 if (!sctp_packet_empty(packet
))
1196 error
= sctp_packet_transmit(packet
, gfp
);
1198 /* Clear the burst limited state, if any */
1199 sctp_transport_burst_reset(t
);
1205 /* Update unack_data based on the incoming SACK chunk */
1206 static void sctp_sack_update_unack_data(struct sctp_association
*assoc
,
1207 struct sctp_sackhdr
*sack
)
1209 sctp_sack_variable_t
*frags
;
1213 unack_data
= assoc
->next_tsn
- assoc
->ctsn_ack_point
- 1;
1215 frags
= sack
->variable
;
1216 for (i
= 0; i
< ntohs(sack
->num_gap_ack_blocks
); i
++) {
1217 unack_data
-= ((ntohs(frags
[i
].gab
.end
) -
1218 ntohs(frags
[i
].gab
.start
) + 1));
1221 assoc
->unack_data
= unack_data
;
1224 /* This is where we REALLY process a SACK.
1226 * Process the SACK against the outqueue. Mostly, this just frees
1227 * things off the transmitted queue.
1229 int sctp_outq_sack(struct sctp_outq
*q
, struct sctp_chunk
*chunk
)
1231 struct sctp_association
*asoc
= q
->asoc
;
1232 struct sctp_sackhdr
*sack
= chunk
->subh
.sack_hdr
;
1233 struct sctp_transport
*transport
;
1234 struct sctp_chunk
*tchunk
= NULL
;
1235 struct list_head
*lchunk
, *transport_list
, *temp
;
1236 sctp_sack_variable_t
*frags
= sack
->variable
;
1237 __u32 sack_ctsn
, ctsn
, tsn
;
1238 __u32 highest_tsn
, highest_new_tsn
;
1240 unsigned int outstanding
;
1241 struct sctp_transport
*primary
= asoc
->peer
.primary_path
;
1242 int count_of_newacks
= 0;
1246 /* Grab the association's destination address list. */
1247 transport_list
= &asoc
->peer
.transport_addr_list
;
1249 sack_ctsn
= ntohl(sack
->cum_tsn_ack
);
1250 gap_ack_blocks
= ntohs(sack
->num_gap_ack_blocks
);
1251 asoc
->stats
.gapcnt
+= gap_ack_blocks
;
1253 * SFR-CACC algorithm:
1254 * On receipt of a SACK the sender SHOULD execute the
1255 * following statements.
1257 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1258 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1259 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1261 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1262 * is set the receiver of the SACK MUST take the following actions:
1264 * A) Initialize the cacc_saw_newack to 0 for all destination
1267 * Only bother if changeover_active is set. Otherwise, this is
1268 * totally suboptimal to do on every SACK.
1270 if (primary
->cacc
.changeover_active
) {
1271 u8 clear_cycling
= 0;
1273 if (TSN_lte(primary
->cacc
.next_tsn_at_change
, sack_ctsn
)) {
1274 primary
->cacc
.changeover_active
= 0;
1278 if (clear_cycling
|| gap_ack_blocks
) {
1279 list_for_each_entry(transport
, transport_list
,
1282 transport
->cacc
.cycling_changeover
= 0;
1284 transport
->cacc
.cacc_saw_newack
= 0;
1289 /* Get the highest TSN in the sack. */
1290 highest_tsn
= sack_ctsn
;
1292 highest_tsn
+= ntohs(frags
[gap_ack_blocks
- 1].gab
.end
);
1294 if (TSN_lt(asoc
->highest_sacked
, highest_tsn
))
1295 asoc
->highest_sacked
= highest_tsn
;
1297 highest_new_tsn
= sack_ctsn
;
1299 /* Run through the retransmit queue. Credit bytes received
1300 * and free those chunks that we can.
1302 sctp_check_transmitted(q
, &q
->retransmit
, NULL
, NULL
, sack
, &highest_new_tsn
);
1304 /* Run through the transmitted queue.
1305 * Credit bytes received and free those chunks which we can.
1307 * This is a MASSIVE candidate for optimization.
1309 list_for_each_entry(transport
, transport_list
, transports
) {
1310 sctp_check_transmitted(q
, &transport
->transmitted
,
1311 transport
, &chunk
->source
, sack
,
1314 * SFR-CACC algorithm:
1315 * C) Let count_of_newacks be the number of
1316 * destinations for which cacc_saw_newack is set.
1318 if (transport
->cacc
.cacc_saw_newack
)
1322 /* Move the Cumulative TSN Ack Point if appropriate. */
1323 if (TSN_lt(asoc
->ctsn_ack_point
, sack_ctsn
)) {
1324 asoc
->ctsn_ack_point
= sack_ctsn
;
1328 if (gap_ack_blocks
) {
1330 if (asoc
->fast_recovery
&& accum_moved
)
1331 highest_new_tsn
= highest_tsn
;
1333 list_for_each_entry(transport
, transport_list
, transports
)
1334 sctp_mark_missing(q
, &transport
->transmitted
, transport
,
1335 highest_new_tsn
, count_of_newacks
);
1338 /* Update unack_data field in the assoc. */
1339 sctp_sack_update_unack_data(asoc
, sack
);
1341 ctsn
= asoc
->ctsn_ack_point
;
1343 /* Throw away stuff rotting on the sack queue. */
1344 list_for_each_safe(lchunk
, temp
, &q
->sacked
) {
1345 tchunk
= list_entry(lchunk
, struct sctp_chunk
,
1347 tsn
= ntohl(tchunk
->subh
.data_hdr
->tsn
);
1348 if (TSN_lte(tsn
, ctsn
)) {
1349 list_del_init(&tchunk
->transmitted_list
);
1350 if (asoc
->prsctp_enable
&&
1351 SCTP_PR_PRIO_ENABLED(chunk
->sinfo
.sinfo_flags
))
1352 asoc
->sent_cnt_removable
--;
1353 sctp_chunk_free(tchunk
);
1357 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1358 * number of bytes still outstanding after processing the
1359 * Cumulative TSN Ack and the Gap Ack Blocks.
1362 sack_a_rwnd
= ntohl(sack
->a_rwnd
);
1363 asoc
->peer
.zero_window_announced
= !sack_a_rwnd
;
1364 outstanding
= q
->outstanding_bytes
;
1366 if (outstanding
< sack_a_rwnd
)
1367 sack_a_rwnd
-= outstanding
;
1371 asoc
->peer
.rwnd
= sack_a_rwnd
;
1373 sctp_generate_fwdtsn(q
, sack_ctsn
);
1375 pr_debug("%s: sack cumulative tsn ack:0x%x\n", __func__
, sack_ctsn
);
1376 pr_debug("%s: cumulative tsn ack of assoc:%p is 0x%x, "
1377 "advertised peer ack point:0x%x\n", __func__
, asoc
, ctsn
,
1378 asoc
->adv_peer_ack_point
);
1380 return sctp_outq_is_empty(q
);
1383 /* Is the outqueue empty?
1384 * The queue is empty when we have not pending data, no in-flight data
1385 * and nothing pending retransmissions.
1387 int sctp_outq_is_empty(const struct sctp_outq
*q
)
1389 return q
->out_qlen
== 0 && q
->outstanding_bytes
== 0 &&
1390 list_empty(&q
->retransmit
);
1393 /********************************************************************
1394 * 2nd Level Abstractions
1395 ********************************************************************/
1397 /* Go through a transport's transmitted list or the association's retransmit
1398 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1399 * The retransmit list will not have an associated transport.
1401 * I added coherent debug information output. --xguo
1403 * Instead of printing 'sacked' or 'kept' for each TSN on the
1404 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1405 * KEPT TSN6-TSN7, etc.
1407 static void sctp_check_transmitted(struct sctp_outq
*q
,
1408 struct list_head
*transmitted_queue
,
1409 struct sctp_transport
*transport
,
1410 union sctp_addr
*saddr
,
1411 struct sctp_sackhdr
*sack
,
1412 __u32
*highest_new_tsn_in_sack
)
1414 struct list_head
*lchunk
;
1415 struct sctp_chunk
*tchunk
;
1416 struct list_head tlist
;
1420 __u8 restart_timer
= 0;
1421 int bytes_acked
= 0;
1422 int migrate_bytes
= 0;
1423 bool forward_progress
= false;
1425 sack_ctsn
= ntohl(sack
->cum_tsn_ack
);
1427 INIT_LIST_HEAD(&tlist
);
1429 /* The while loop will skip empty transmitted queues. */
1430 while (NULL
!= (lchunk
= sctp_list_dequeue(transmitted_queue
))) {
1431 tchunk
= list_entry(lchunk
, struct sctp_chunk
,
1434 if (sctp_chunk_abandoned(tchunk
)) {
1435 /* Move the chunk to abandoned list. */
1436 sctp_insert_list(&q
->abandoned
, lchunk
);
1438 /* If this chunk has not been acked, stop
1439 * considering it as 'outstanding'.
1441 if (!tchunk
->tsn_gap_acked
) {
1442 if (tchunk
->transport
)
1443 tchunk
->transport
->flight_size
-=
1444 sctp_data_size(tchunk
);
1445 q
->outstanding_bytes
-= sctp_data_size(tchunk
);
1450 tsn
= ntohl(tchunk
->subh
.data_hdr
->tsn
);
1451 if (sctp_acked(sack
, tsn
)) {
1452 /* If this queue is the retransmit queue, the
1453 * retransmit timer has already reclaimed
1454 * the outstanding bytes for this chunk, so only
1455 * count bytes associated with a transport.
1458 /* If this chunk is being used for RTT
1459 * measurement, calculate the RTT and update
1460 * the RTO using this value.
1462 * 6.3.1 C5) Karn's algorithm: RTT measurements
1463 * MUST NOT be made using packets that were
1464 * retransmitted (and thus for which it is
1465 * ambiguous whether the reply was for the
1466 * first instance of the packet or a later
1469 if (!tchunk
->tsn_gap_acked
&&
1471 tchunk
->rtt_in_progress
) {
1472 tchunk
->rtt_in_progress
= 0;
1473 rtt
= jiffies
- tchunk
->sent_at
;
1474 sctp_transport_update_rto(transport
,
1479 /* If the chunk hasn't been marked as ACKED,
1480 * mark it and account bytes_acked if the
1481 * chunk had a valid transport (it will not
1482 * have a transport if ASCONF had deleted it
1483 * while DATA was outstanding).
1485 if (!tchunk
->tsn_gap_acked
) {
1486 tchunk
->tsn_gap_acked
= 1;
1487 if (TSN_lt(*highest_new_tsn_in_sack
, tsn
))
1488 *highest_new_tsn_in_sack
= tsn
;
1489 bytes_acked
+= sctp_data_size(tchunk
);
1490 if (!tchunk
->transport
)
1491 migrate_bytes
+= sctp_data_size(tchunk
);
1492 forward_progress
= true;
1495 if (TSN_lte(tsn
, sack_ctsn
)) {
1496 /* RFC 2960 6.3.2 Retransmission Timer Rules
1498 * R3) Whenever a SACK is received
1499 * that acknowledges the DATA chunk
1500 * with the earliest outstanding TSN
1501 * for that address, restart T3-rtx
1502 * timer for that address with its
1506 forward_progress
= true;
1508 if (!tchunk
->tsn_gap_acked
) {
1510 * SFR-CACC algorithm:
1511 * 2) If the SACK contains gap acks
1512 * and the flag CHANGEOVER_ACTIVE is
1513 * set the receiver of the SACK MUST
1514 * take the following action:
1516 * B) For each TSN t being acked that
1517 * has not been acked in any SACK so
1518 * far, set cacc_saw_newack to 1 for
1519 * the destination that the TSN was
1523 sack
->num_gap_ack_blocks
&&
1524 q
->asoc
->peer
.primary_path
->cacc
.
1526 transport
->cacc
.cacc_saw_newack
1530 list_add_tail(&tchunk
->transmitted_list
,
1533 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1534 * M2) Each time a SACK arrives reporting
1535 * 'Stray DATA chunk(s)' record the highest TSN
1536 * reported as newly acknowledged, call this
1537 * value 'HighestTSNinSack'. A newly
1538 * acknowledged DATA chunk is one not
1539 * previously acknowledged in a SACK.
1541 * When the SCTP sender of data receives a SACK
1542 * chunk that acknowledges, for the first time,
1543 * the receipt of a DATA chunk, all the still
1544 * unacknowledged DATA chunks whose TSN is
1545 * older than that newly acknowledged DATA
1546 * chunk, are qualified as 'Stray DATA chunks'.
1548 list_add_tail(lchunk
, &tlist
);
1551 if (tchunk
->tsn_gap_acked
) {
1552 pr_debug("%s: receiver reneged on data TSN:0x%x\n",
1555 tchunk
->tsn_gap_acked
= 0;
1557 if (tchunk
->transport
)
1558 bytes_acked
-= sctp_data_size(tchunk
);
1560 /* RFC 2960 6.3.2 Retransmission Timer Rules
1562 * R4) Whenever a SACK is received missing a
1563 * TSN that was previously acknowledged via a
1564 * Gap Ack Block, start T3-rtx for the
1565 * destination address to which the DATA
1566 * chunk was originally
1567 * transmitted if it is not already running.
1572 list_add_tail(lchunk
, &tlist
);
1578 struct sctp_association
*asoc
= transport
->asoc
;
1580 /* We may have counted DATA that was migrated
1581 * to this transport due to DEL-IP operation.
1582 * Subtract those bytes, since the were never
1583 * send on this transport and shouldn't be
1584 * credited to this transport.
1586 bytes_acked
-= migrate_bytes
;
1588 /* 8.2. When an outstanding TSN is acknowledged,
1589 * the endpoint shall clear the error counter of
1590 * the destination transport address to which the
1591 * DATA chunk was last sent.
1592 * The association's overall error counter is
1595 transport
->error_count
= 0;
1596 transport
->asoc
->overall_error_count
= 0;
1597 forward_progress
= true;
1600 * While in SHUTDOWN PENDING, we may have started
1601 * the T5 shutdown guard timer after reaching the
1602 * retransmission limit. Stop that timer as soon
1603 * as the receiver acknowledged any data.
1605 if (asoc
->state
== SCTP_STATE_SHUTDOWN_PENDING
&&
1606 del_timer(&asoc
->timers
1607 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD
]))
1608 sctp_association_put(asoc
);
1610 /* Mark the destination transport address as
1611 * active if it is not so marked.
1613 if ((transport
->state
== SCTP_INACTIVE
||
1614 transport
->state
== SCTP_UNCONFIRMED
) &&
1615 sctp_cmp_addr_exact(&transport
->ipaddr
, saddr
)) {
1616 sctp_assoc_control_transport(
1620 SCTP_RECEIVED_SACK
);
1623 sctp_transport_raise_cwnd(transport
, sack_ctsn
,
1626 transport
->flight_size
-= bytes_acked
;
1627 if (transport
->flight_size
== 0)
1628 transport
->partial_bytes_acked
= 0;
1629 q
->outstanding_bytes
-= bytes_acked
+ migrate_bytes
;
1631 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1632 * When a sender is doing zero window probing, it
1633 * should not timeout the association if it continues
1634 * to receive new packets from the receiver. The
1635 * reason is that the receiver MAY keep its window
1636 * closed for an indefinite time.
1637 * A sender is doing zero window probing when the
1638 * receiver's advertised window is zero, and there is
1639 * only one data chunk in flight to the receiver.
1641 * Allow the association to timeout while in SHUTDOWN
1642 * PENDING or SHUTDOWN RECEIVED in case the receiver
1643 * stays in zero window mode forever.
1645 if (!q
->asoc
->peer
.rwnd
&&
1646 !list_empty(&tlist
) &&
1647 (sack_ctsn
+2 == q
->asoc
->next_tsn
) &&
1648 q
->asoc
->state
< SCTP_STATE_SHUTDOWN_PENDING
) {
1649 pr_debug("%s: sack received for zero window "
1650 "probe:%u\n", __func__
, sack_ctsn
);
1652 q
->asoc
->overall_error_count
= 0;
1653 transport
->error_count
= 0;
1657 /* RFC 2960 6.3.2 Retransmission Timer Rules
1659 * R2) Whenever all outstanding data sent to an address have
1660 * been acknowledged, turn off the T3-rtx timer of that
1663 if (!transport
->flight_size
) {
1664 if (del_timer(&transport
->T3_rtx_timer
))
1665 sctp_transport_put(transport
);
1666 } else if (restart_timer
) {
1667 if (!mod_timer(&transport
->T3_rtx_timer
,
1668 jiffies
+ transport
->rto
))
1669 sctp_transport_hold(transport
);
1672 if (forward_progress
) {
1674 dst_confirm(transport
->dst
);
1678 list_splice(&tlist
, transmitted_queue
);
1681 /* Mark chunks as missing and consequently may get retransmitted. */
1682 static void sctp_mark_missing(struct sctp_outq
*q
,
1683 struct list_head
*transmitted_queue
,
1684 struct sctp_transport
*transport
,
1685 __u32 highest_new_tsn_in_sack
,
1686 int count_of_newacks
)
1688 struct sctp_chunk
*chunk
;
1690 char do_fast_retransmit
= 0;
1691 struct sctp_association
*asoc
= q
->asoc
;
1692 struct sctp_transport
*primary
= asoc
->peer
.primary_path
;
1694 list_for_each_entry(chunk
, transmitted_queue
, transmitted_list
) {
1696 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1698 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1699 * 'Unacknowledged TSN's', if the TSN number of an
1700 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1701 * value, increment the 'TSN.Missing.Report' count on that
1702 * chunk if it has NOT been fast retransmitted or marked for
1703 * fast retransmit already.
1705 if (chunk
->fast_retransmit
== SCTP_CAN_FRTX
&&
1706 !chunk
->tsn_gap_acked
&&
1707 TSN_lt(tsn
, highest_new_tsn_in_sack
)) {
1709 /* SFR-CACC may require us to skip marking
1710 * this chunk as missing.
1712 if (!transport
|| !sctp_cacc_skip(primary
,
1714 count_of_newacks
, tsn
)) {
1715 chunk
->tsn_missing_report
++;
1717 pr_debug("%s: tsn:0x%x missing counter:%d\n",
1718 __func__
, tsn
, chunk
->tsn_missing_report
);
1722 * M4) If any DATA chunk is found to have a
1723 * 'TSN.Missing.Report'
1724 * value larger than or equal to 3, mark that chunk for
1725 * retransmission and start the fast retransmit procedure.
1728 if (chunk
->tsn_missing_report
>= 3) {
1729 chunk
->fast_retransmit
= SCTP_NEED_FRTX
;
1730 do_fast_retransmit
= 1;
1735 if (do_fast_retransmit
)
1736 sctp_retransmit(q
, transport
, SCTP_RTXR_FAST_RTX
);
1738 pr_debug("%s: transport:%p, cwnd:%d, ssthresh:%d, "
1739 "flight_size:%d, pba:%d\n", __func__
, transport
,
1740 transport
->cwnd
, transport
->ssthresh
,
1741 transport
->flight_size
, transport
->partial_bytes_acked
);
1745 /* Is the given TSN acked by this packet? */
1746 static int sctp_acked(struct sctp_sackhdr
*sack
, __u32 tsn
)
1749 sctp_sack_variable_t
*frags
;
1751 __u32 ctsn
= ntohl(sack
->cum_tsn_ack
);
1753 if (TSN_lte(tsn
, ctsn
))
1756 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1759 * These fields contain the Gap Ack Blocks. They are repeated
1760 * for each Gap Ack Block up to the number of Gap Ack Blocks
1761 * defined in the Number of Gap Ack Blocks field. All DATA
1762 * chunks with TSNs greater than or equal to (Cumulative TSN
1763 * Ack + Gap Ack Block Start) and less than or equal to
1764 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1765 * Block are assumed to have been received correctly.
1768 frags
= sack
->variable
;
1770 for (i
= 0; i
< ntohs(sack
->num_gap_ack_blocks
); ++i
) {
1771 if (TSN_lte(ntohs(frags
[i
].gab
.start
), gap
) &&
1772 TSN_lte(gap
, ntohs(frags
[i
].gab
.end
)))
1781 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip
*skiplist
,
1782 int nskips
, __be16 stream
)
1786 for (i
= 0; i
< nskips
; i
++) {
1787 if (skiplist
[i
].stream
== stream
)
1793 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1794 static void sctp_generate_fwdtsn(struct sctp_outq
*q
, __u32 ctsn
)
1796 struct sctp_association
*asoc
= q
->asoc
;
1797 struct sctp_chunk
*ftsn_chunk
= NULL
;
1798 struct sctp_fwdtsn_skip ftsn_skip_arr
[10];
1802 struct sctp_chunk
*chunk
;
1803 struct list_head
*lchunk
, *temp
;
1805 if (!asoc
->peer
.prsctp_capable
)
1808 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1811 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1812 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1814 if (TSN_lt(asoc
->adv_peer_ack_point
, ctsn
))
1815 asoc
->adv_peer_ack_point
= ctsn
;
1817 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1818 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1819 * the chunk next in the out-queue space is marked as "abandoned" as
1820 * shown in the following example:
1822 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1823 * and the Advanced.Peer.Ack.Point is updated to this value:
1825 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1826 * normal SACK processing local advancement
1828 * Adv.Ack.Pt-> 102 acked 102 acked
1829 * 103 abandoned 103 abandoned
1830 * 104 abandoned Adv.Ack.P-> 104 abandoned
1832 * 106 acked 106 acked
1835 * In this example, the data sender successfully advanced the
1836 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1838 list_for_each_safe(lchunk
, temp
, &q
->abandoned
) {
1839 chunk
= list_entry(lchunk
, struct sctp_chunk
,
1841 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1843 /* Remove any chunks in the abandoned queue that are acked by
1846 if (TSN_lte(tsn
, ctsn
)) {
1847 list_del_init(lchunk
);
1848 sctp_chunk_free(chunk
);
1850 if (TSN_lte(tsn
, asoc
->adv_peer_ack_point
+1)) {
1851 asoc
->adv_peer_ack_point
= tsn
;
1852 if (chunk
->chunk_hdr
->flags
&
1853 SCTP_DATA_UNORDERED
)
1855 skip_pos
= sctp_get_skip_pos(&ftsn_skip_arr
[0],
1857 chunk
->subh
.data_hdr
->stream
);
1858 ftsn_skip_arr
[skip_pos
].stream
=
1859 chunk
->subh
.data_hdr
->stream
;
1860 ftsn_skip_arr
[skip_pos
].ssn
=
1861 chunk
->subh
.data_hdr
->ssn
;
1862 if (skip_pos
== nskips
)
1871 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1872 * is greater than the Cumulative TSN ACK carried in the received
1873 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1874 * chunk containing the latest value of the
1875 * "Advanced.Peer.Ack.Point".
1877 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1878 * list each stream and sequence number in the forwarded TSN. This
1879 * information will enable the receiver to easily find any
1880 * stranded TSN's waiting on stream reorder queues. Each stream
1881 * SHOULD only be reported once; this means that if multiple
1882 * abandoned messages occur in the same stream then only the
1883 * highest abandoned stream sequence number is reported. If the
1884 * total size of the FORWARD TSN does NOT fit in a single MTU then
1885 * the sender of the FORWARD TSN SHOULD lower the
1886 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1889 if (asoc
->adv_peer_ack_point
> ctsn
)
1890 ftsn_chunk
= sctp_make_fwdtsn(asoc
, asoc
->adv_peer_ack_point
,
1891 nskips
, &ftsn_skip_arr
[0]);
1894 list_add_tail(&ftsn_chunk
->list
, &q
->control_chunk_list
);
1895 SCTP_INC_STATS(sock_net(asoc
->base
.sk
), SCTP_MIB_OUTCTRLCHUNKS
);