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, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
29 * Please send any bug reports or fixes you make to the
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Perry Melange <pmelange@null.cc.uic.edu>
40 * Xingang Guo <xingang.guo@intel.com>
41 * Hui Huang <hui.huang@nokia.com>
42 * Sridhar Samudrala <sri@us.ibm.com>
43 * Jon Grimm <jgrimm@us.ibm.com>
45 * Any bugs reported given to us we will try to fix... any fixes shared will
46 * be incorporated into the next SCTP release.
49 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51 #include <linux/types.h>
52 #include <linux/list.h> /* For struct list_head */
53 #include <linux/socket.h>
55 #include <linux/slab.h>
56 #include <net/sock.h> /* For skb_set_owner_w */
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
61 /* Declare internal functions here. */
62 static int sctp_acked(struct sctp_sackhdr
*sack
, __u32 tsn
);
63 static void sctp_check_transmitted(struct sctp_outq
*q
,
64 struct list_head
*transmitted_queue
,
65 struct sctp_transport
*transport
,
66 union sctp_addr
*saddr
,
67 struct sctp_sackhdr
*sack
,
68 __u32
*highest_new_tsn
);
70 static void sctp_mark_missing(struct sctp_outq
*q
,
71 struct list_head
*transmitted_queue
,
72 struct sctp_transport
*transport
,
73 __u32 highest_new_tsn
,
74 int count_of_newacks
);
76 static void sctp_generate_fwdtsn(struct sctp_outq
*q
, __u32 sack_ctsn
);
78 static int sctp_outq_flush(struct sctp_outq
*q
, int rtx_timeout
);
80 /* Add data to the front of the queue. */
81 static inline void sctp_outq_head_data(struct sctp_outq
*q
,
82 struct sctp_chunk
*ch
)
84 list_add(&ch
->list
, &q
->out_chunk_list
);
85 q
->out_qlen
+= ch
->skb
->len
;
88 /* Take data from the front of the queue. */
89 static inline struct sctp_chunk
*sctp_outq_dequeue_data(struct sctp_outq
*q
)
91 struct sctp_chunk
*ch
= NULL
;
93 if (!list_empty(&q
->out_chunk_list
)) {
94 struct list_head
*entry
= q
->out_chunk_list
.next
;
96 ch
= list_entry(entry
, struct sctp_chunk
, list
);
98 q
->out_qlen
-= ch
->skb
->len
;
102 /* Add data chunk to the end of the queue. */
103 static inline void sctp_outq_tail_data(struct sctp_outq
*q
,
104 struct sctp_chunk
*ch
)
106 list_add_tail(&ch
->list
, &q
->out_chunk_list
);
107 q
->out_qlen
+= ch
->skb
->len
;
111 * SFR-CACC algorithm:
112 * D) If count_of_newacks is greater than or equal to 2
113 * and t was not sent to the current primary then the
114 * sender MUST NOT increment missing report count for t.
116 static inline int sctp_cacc_skip_3_1_d(struct sctp_transport
*primary
,
117 struct sctp_transport
*transport
,
118 int count_of_newacks
)
120 if (count_of_newacks
>=2 && transport
!= primary
)
126 * SFR-CACC algorithm:
127 * F) If count_of_newacks is less than 2, let d be the
128 * destination to which t was sent. If cacc_saw_newack
129 * is 0 for destination d, then the sender MUST NOT
130 * increment missing report count for t.
132 static inline int sctp_cacc_skip_3_1_f(struct sctp_transport
*transport
,
133 int count_of_newacks
)
135 if (count_of_newacks
< 2 &&
136 (transport
&& !transport
->cacc
.cacc_saw_newack
))
142 * SFR-CACC algorithm:
143 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
144 * execute steps C, D, F.
146 * C has been implemented in sctp_outq_sack
148 static inline int sctp_cacc_skip_3_1(struct sctp_transport
*primary
,
149 struct sctp_transport
*transport
,
150 int count_of_newacks
)
152 if (!primary
->cacc
.cycling_changeover
) {
153 if (sctp_cacc_skip_3_1_d(primary
, transport
, count_of_newacks
))
155 if (sctp_cacc_skip_3_1_f(transport
, count_of_newacks
))
163 * SFR-CACC algorithm:
164 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
165 * than next_tsn_at_change of the current primary, then
166 * the sender MUST NOT increment missing report count
169 static inline int sctp_cacc_skip_3_2(struct sctp_transport
*primary
, __u32 tsn
)
171 if (primary
->cacc
.cycling_changeover
&&
172 TSN_lt(tsn
, primary
->cacc
.next_tsn_at_change
))
178 * SFR-CACC algorithm:
179 * 3) If the missing report count for TSN t is to be
180 * incremented according to [RFC2960] and
181 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
182 * then the sender MUST further execute steps 3.1 and
183 * 3.2 to determine if the missing report count for
184 * TSN t SHOULD NOT be incremented.
186 * 3.3) If 3.1 and 3.2 do not dictate that the missing
187 * report count for t should not be incremented, then
188 * the sender SHOULD increment missing report count for
189 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
191 static inline int sctp_cacc_skip(struct sctp_transport
*primary
,
192 struct sctp_transport
*transport
,
193 int count_of_newacks
,
196 if (primary
->cacc
.changeover_active
&&
197 (sctp_cacc_skip_3_1(primary
, transport
, count_of_newacks
) ||
198 sctp_cacc_skip_3_2(primary
, tsn
)))
203 /* Initialize an existing sctp_outq. This does the boring stuff.
204 * You still need to define handlers if you really want to DO
205 * something with this structure...
207 void sctp_outq_init(struct sctp_association
*asoc
, struct sctp_outq
*q
)
209 memset(q
, 0, sizeof(struct sctp_outq
));
212 INIT_LIST_HEAD(&q
->out_chunk_list
);
213 INIT_LIST_HEAD(&q
->control_chunk_list
);
214 INIT_LIST_HEAD(&q
->retransmit
);
215 INIT_LIST_HEAD(&q
->sacked
);
216 INIT_LIST_HEAD(&q
->abandoned
);
221 /* Free the outqueue structure and any related pending chunks.
223 static void __sctp_outq_teardown(struct sctp_outq
*q
)
225 struct sctp_transport
*transport
;
226 struct list_head
*lchunk
, *temp
;
227 struct sctp_chunk
*chunk
, *tmp
;
229 /* Throw away unacknowledged chunks. */
230 list_for_each_entry(transport
, &q
->asoc
->peer
.transport_addr_list
,
232 while ((lchunk
= sctp_list_dequeue(&transport
->transmitted
)) != NULL
) {
233 chunk
= list_entry(lchunk
, struct sctp_chunk
,
235 /* Mark as part of a failed message. */
236 sctp_chunk_fail(chunk
, q
->error
);
237 sctp_chunk_free(chunk
);
241 /* Throw away chunks that have been gap ACKed. */
242 list_for_each_safe(lchunk
, temp
, &q
->sacked
) {
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 in the retransmit queue. */
251 list_for_each_safe(lchunk
, temp
, &q
->retransmit
) {
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 chunks that are in the abandoned queue. */
260 list_for_each_safe(lchunk
, temp
, &q
->abandoned
) {
261 list_del_init(lchunk
);
262 chunk
= list_entry(lchunk
, struct sctp_chunk
,
264 sctp_chunk_fail(chunk
, q
->error
);
265 sctp_chunk_free(chunk
);
268 /* Throw away any leftover data chunks. */
269 while ((chunk
= sctp_outq_dequeue_data(q
)) != NULL
) {
271 /* Mark as send failure. */
272 sctp_chunk_fail(chunk
, q
->error
);
273 sctp_chunk_free(chunk
);
276 /* Throw away any leftover control chunks. */
277 list_for_each_entry_safe(chunk
, tmp
, &q
->control_chunk_list
, list
) {
278 list_del_init(&chunk
->list
);
279 sctp_chunk_free(chunk
);
283 void sctp_outq_teardown(struct sctp_outq
*q
)
285 __sctp_outq_teardown(q
);
286 sctp_outq_init(q
->asoc
, q
);
289 /* Free the outqueue structure and any related pending chunks. */
290 void sctp_outq_free(struct sctp_outq
*q
)
292 /* Throw away leftover chunks. */
293 __sctp_outq_teardown(q
);
296 /* Put a new chunk in an sctp_outq. */
297 int sctp_outq_tail(struct sctp_outq
*q
, struct sctp_chunk
*chunk
)
299 struct net
*net
= sock_net(q
->asoc
->base
.sk
);
302 SCTP_DEBUG_PRINTK("sctp_outq_tail(%p, %p[%s])\n",
303 q
, chunk
, chunk
&& chunk
->chunk_hdr
?
304 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
))
307 /* If it is data, queue it up, otherwise, send it
310 if (sctp_chunk_is_data(chunk
)) {
311 /* Is it OK to queue data chunks? */
312 /* From 9. Termination of Association
314 * When either endpoint performs a shutdown, the
315 * association on each peer will stop accepting new
316 * data from its user and only deliver data in queue
317 * at the time of sending or receiving the SHUTDOWN
320 switch (q
->asoc
->state
) {
321 case SCTP_STATE_CLOSED
:
322 case SCTP_STATE_SHUTDOWN_PENDING
:
323 case SCTP_STATE_SHUTDOWN_SENT
:
324 case SCTP_STATE_SHUTDOWN_RECEIVED
:
325 case SCTP_STATE_SHUTDOWN_ACK_SENT
:
326 /* Cannot send after transport endpoint shutdown */
331 SCTP_DEBUG_PRINTK("outqueueing (%p, %p[%s])\n",
332 q
, chunk
, chunk
&& chunk
->chunk_hdr
?
333 sctp_cname(SCTP_ST_CHUNK(chunk
->chunk_hdr
->type
))
336 sctp_outq_tail_data(q
, chunk
);
337 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
338 SCTP_INC_STATS(net
, SCTP_MIB_OUTUNORDERCHUNKS
);
340 SCTP_INC_STATS(net
, SCTP_MIB_OUTORDERCHUNKS
);
345 list_add_tail(&chunk
->list
, &q
->control_chunk_list
);
346 SCTP_INC_STATS(net
, SCTP_MIB_OUTCTRLCHUNKS
);
353 error
= sctp_outq_flush(q
, 0);
358 /* Insert a chunk into the sorted list based on the TSNs. The retransmit list
359 * and the abandoned list are in ascending order.
361 static void sctp_insert_list(struct list_head
*head
, struct list_head
*new)
363 struct list_head
*pos
;
364 struct sctp_chunk
*nchunk
, *lchunk
;
368 nchunk
= list_entry(new, struct sctp_chunk
, transmitted_list
);
369 ntsn
= ntohl(nchunk
->subh
.data_hdr
->tsn
);
371 list_for_each(pos
, head
) {
372 lchunk
= list_entry(pos
, struct sctp_chunk
, transmitted_list
);
373 ltsn
= ntohl(lchunk
->subh
.data_hdr
->tsn
);
374 if (TSN_lt(ntsn
, ltsn
)) {
375 list_add(new, pos
->prev
);
381 list_add_tail(new, head
);
384 /* Mark all the eligible packets on a transport for retransmission. */
385 void sctp_retransmit_mark(struct sctp_outq
*q
,
386 struct sctp_transport
*transport
,
389 struct list_head
*lchunk
, *ltemp
;
390 struct sctp_chunk
*chunk
;
392 /* Walk through the specified transmitted queue. */
393 list_for_each_safe(lchunk
, ltemp
, &transport
->transmitted
) {
394 chunk
= list_entry(lchunk
, struct sctp_chunk
,
397 /* If the chunk is abandoned, move it to abandoned list. */
398 if (sctp_chunk_abandoned(chunk
)) {
399 list_del_init(lchunk
);
400 sctp_insert_list(&q
->abandoned
, lchunk
);
402 /* If this chunk has not been previousely acked,
403 * stop considering it 'outstanding'. Our peer
404 * will most likely never see it since it will
405 * not be retransmitted
407 if (!chunk
->tsn_gap_acked
) {
408 if (chunk
->transport
)
409 chunk
->transport
->flight_size
-=
410 sctp_data_size(chunk
);
411 q
->outstanding_bytes
-= sctp_data_size(chunk
);
412 q
->asoc
->peer
.rwnd
+= sctp_data_size(chunk
);
417 /* If we are doing retransmission due to a timeout or pmtu
418 * discovery, only the chunks that are not yet acked should
419 * be added to the retransmit queue.
421 if ((reason
== SCTP_RTXR_FAST_RTX
&&
422 (chunk
->fast_retransmit
== SCTP_NEED_FRTX
)) ||
423 (reason
!= SCTP_RTXR_FAST_RTX
&& !chunk
->tsn_gap_acked
)) {
424 /* RFC 2960 6.2.1 Processing a Received SACK
426 * C) Any time a DATA chunk is marked for
427 * retransmission (via either T3-rtx timer expiration
428 * (Section 6.3.3) or via fast retransmit
429 * (Section 7.2.4)), add the data size of those
430 * chunks to the rwnd.
432 q
->asoc
->peer
.rwnd
+= sctp_data_size(chunk
);
433 q
->outstanding_bytes
-= sctp_data_size(chunk
);
434 if (chunk
->transport
)
435 transport
->flight_size
-= sctp_data_size(chunk
);
437 /* sctpimpguide-05 Section 2.8.2
438 * M5) If a T3-rtx timer expires, the
439 * 'TSN.Missing.Report' of all affected TSNs is set
442 chunk
->tsn_missing_report
= 0;
444 /* If a chunk that is being used for RTT measurement
445 * has to be retransmitted, we cannot use this chunk
446 * anymore for RTT measurements. Reset rto_pending so
447 * that a new RTT measurement is started when a new
448 * data chunk is sent.
450 if (chunk
->rtt_in_progress
) {
451 chunk
->rtt_in_progress
= 0;
452 transport
->rto_pending
= 0;
455 /* Move the chunk to the retransmit queue. The chunks
456 * on the retransmit queue are always kept in order.
458 list_del_init(lchunk
);
459 sctp_insert_list(&q
->retransmit
, lchunk
);
463 SCTP_DEBUG_PRINTK("%s: transport: %p, reason: %d, "
464 "cwnd: %d, ssthresh: %d, flight_size: %d, "
465 "pba: %d\n", __func__
,
467 transport
->cwnd
, transport
->ssthresh
,
468 transport
->flight_size
,
469 transport
->partial_bytes_acked
);
473 /* Mark all the eligible packets on a transport for retransmission and force
476 void sctp_retransmit(struct sctp_outq
*q
, struct sctp_transport
*transport
,
477 sctp_retransmit_reason_t reason
)
479 struct net
*net
= sock_net(q
->asoc
->base
.sk
);
483 case SCTP_RTXR_T3_RTX
:
484 SCTP_INC_STATS(net
, SCTP_MIB_T3_RETRANSMITS
);
485 sctp_transport_lower_cwnd(transport
, SCTP_LOWER_CWND_T3_RTX
);
486 /* Update the retran path if the T3-rtx timer has expired for
487 * the current retran path.
489 if (transport
== transport
->asoc
->peer
.retran_path
)
490 sctp_assoc_update_retran_path(transport
->asoc
);
491 transport
->asoc
->rtx_data_chunks
+=
492 transport
->asoc
->unack_data
;
494 case SCTP_RTXR_FAST_RTX
:
495 SCTP_INC_STATS(net
, SCTP_MIB_FAST_RETRANSMITS
);
496 sctp_transport_lower_cwnd(transport
, SCTP_LOWER_CWND_FAST_RTX
);
499 case SCTP_RTXR_PMTUD
:
500 SCTP_INC_STATS(net
, SCTP_MIB_PMTUD_RETRANSMITS
);
502 case SCTP_RTXR_T1_RTX
:
503 SCTP_INC_STATS(net
, SCTP_MIB_T1_RETRANSMITS
);
504 transport
->asoc
->init_retries
++;
510 sctp_retransmit_mark(q
, transport
, reason
);
512 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
513 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
514 * following the procedures outlined in C1 - C5.
516 if (reason
== SCTP_RTXR_T3_RTX
)
517 sctp_generate_fwdtsn(q
, q
->asoc
->ctsn_ack_point
);
519 /* Flush the queues only on timeout, since fast_rtx is only
520 * triggered during sack processing and the queue
521 * will be flushed at the end.
523 if (reason
!= SCTP_RTXR_FAST_RTX
)
524 error
= sctp_outq_flush(q
, /* rtx_timeout */ 1);
527 q
->asoc
->base
.sk
->sk_err
= -error
;
531 * Transmit DATA chunks on the retransmit queue. Upon return from
532 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
533 * need to be transmitted by the caller.
534 * We assume that pkt->transport has already been set.
536 * The return value is a normal kernel error return value.
538 static int sctp_outq_flush_rtx(struct sctp_outq
*q
, struct sctp_packet
*pkt
,
539 int rtx_timeout
, int *start_timer
)
541 struct list_head
*lqueue
;
542 struct sctp_transport
*transport
= pkt
->transport
;
544 struct sctp_chunk
*chunk
, *chunk1
;
550 lqueue
= &q
->retransmit
;
551 fast_rtx
= q
->fast_rtx
;
553 /* This loop handles time-out retransmissions, fast retransmissions,
554 * and retransmissions due to opening of whindow.
556 * RFC 2960 6.3.3 Handle T3-rtx Expiration
558 * E3) Determine how many of the earliest (i.e., lowest TSN)
559 * outstanding DATA chunks for the address for which the
560 * T3-rtx has expired will fit into a single packet, subject
561 * to the MTU constraint for the path corresponding to the
562 * destination transport address to which the retransmission
563 * is being sent (this may be different from the address for
564 * which the timer expires [see Section 6.4]). Call this value
565 * K. Bundle and retransmit those K DATA chunks in a single
566 * packet to the destination endpoint.
568 * [Just to be painfully clear, if we are retransmitting
569 * because a timeout just happened, we should send only ONE
570 * packet of retransmitted data.]
572 * For fast retransmissions we also send only ONE packet. However,
573 * if we are just flushing the queue due to open window, we'll
574 * try to send as much as possible.
576 list_for_each_entry_safe(chunk
, chunk1
, lqueue
, transmitted_list
) {
577 /* If the chunk is abandoned, move it to abandoned list. */
578 if (sctp_chunk_abandoned(chunk
)) {
579 list_del_init(&chunk
->transmitted_list
);
580 sctp_insert_list(&q
->abandoned
,
581 &chunk
->transmitted_list
);
585 /* Make sure that Gap Acked TSNs are not retransmitted. A
586 * simple approach is just to move such TSNs out of the
587 * way and into a 'transmitted' queue and skip to the
590 if (chunk
->tsn_gap_acked
) {
591 list_move_tail(&chunk
->transmitted_list
,
592 &transport
->transmitted
);
596 /* If we are doing fast retransmit, ignore non-fast_rtransmit
599 if (fast_rtx
&& !chunk
->fast_retransmit
)
603 /* Attempt to append this chunk to the packet. */
604 status
= sctp_packet_append_chunk(pkt
, chunk
);
607 case SCTP_XMIT_PMTU_FULL
:
608 if (!pkt
->has_data
&& !pkt
->has_cookie_echo
) {
609 /* If this packet did not contain DATA then
610 * retransmission did not happen, so do it
611 * again. We'll ignore the error here since
612 * control chunks are already freed so there
613 * is nothing we can do.
615 sctp_packet_transmit(pkt
);
619 /* Send this packet. */
620 error
= sctp_packet_transmit(pkt
);
622 /* If we are retransmitting, we should only
623 * send a single packet.
624 * Otherwise, try appending this chunk again.
626 if (rtx_timeout
|| fast_rtx
)
631 /* Bundle next chunk in the next round. */
634 case SCTP_XMIT_RWND_FULL
:
635 /* Send this packet. */
636 error
= sctp_packet_transmit(pkt
);
638 /* Stop sending DATA as there is no more room
644 case SCTP_XMIT_NAGLE_DELAY
:
645 /* Send this packet. */
646 error
= sctp_packet_transmit(pkt
);
648 /* Stop sending DATA because of nagle delay. */
653 /* The append was successful, so add this chunk to
654 * the transmitted list.
656 list_move_tail(&chunk
->transmitted_list
,
657 &transport
->transmitted
);
659 /* Mark the chunk as ineligible for fast retransmit
660 * after it is retransmitted.
662 if (chunk
->fast_retransmit
== SCTP_NEED_FRTX
)
663 chunk
->fast_retransmit
= SCTP_DONT_FRTX
;
666 q
->asoc
->stats
.rtxchunks
++;
670 /* Set the timer if there were no errors */
671 if (!error
&& !timer
)
678 /* If we are here due to a retransmit timeout or a fast
679 * retransmit and if there are any chunks left in the retransmit
680 * queue that could not fit in the PMTU sized packet, they need
681 * to be marked as ineligible for a subsequent fast retransmit.
683 if (rtx_timeout
|| fast_rtx
) {
684 list_for_each_entry(chunk1
, lqueue
, transmitted_list
) {
685 if (chunk1
->fast_retransmit
== SCTP_NEED_FRTX
)
686 chunk1
->fast_retransmit
= SCTP_DONT_FRTX
;
690 *start_timer
= timer
;
692 /* Clear fast retransmit hint */
699 /* Cork the outqueue so queued chunks are really queued. */
700 int sctp_outq_uncork(struct sctp_outq
*q
)
705 return sctp_outq_flush(q
, 0);
710 * Try to flush an outqueue.
712 * Description: Send everything in q which we legally can, subject to
713 * congestion limitations.
714 * * Note: This function can be called from multiple contexts so appropriate
715 * locking concerns must be made. Today we use the sock lock to protect
718 static int sctp_outq_flush(struct sctp_outq
*q
, int rtx_timeout
)
720 struct sctp_packet
*packet
;
721 struct sctp_packet singleton
;
722 struct sctp_association
*asoc
= q
->asoc
;
723 __u16 sport
= asoc
->base
.bind_addr
.port
;
724 __u16 dport
= asoc
->peer
.port
;
725 __u32 vtag
= asoc
->peer
.i
.init_tag
;
726 struct sctp_transport
*transport
= NULL
;
727 struct sctp_transport
*new_transport
;
728 struct sctp_chunk
*chunk
, *tmp
;
734 /* These transports have chunks to send. */
735 struct list_head transport_list
;
736 struct list_head
*ltransport
;
738 INIT_LIST_HEAD(&transport_list
);
744 * When bundling control chunks with DATA chunks, an
745 * endpoint MUST place control chunks first in the outbound
746 * SCTP packet. The transmitter MUST transmit DATA chunks
747 * within a SCTP packet in increasing order of TSN.
751 list_for_each_entry_safe(chunk
, tmp
, &q
->control_chunk_list
, list
) {
753 * F1) This means that until such time as the ASCONF
754 * containing the add is acknowledged, the sender MUST
755 * NOT use the new IP address as a source for ANY SCTP
756 * packet except on carrying an ASCONF Chunk.
758 if (asoc
->src_out_of_asoc_ok
&&
759 chunk
->chunk_hdr
->type
!= SCTP_CID_ASCONF
)
762 list_del_init(&chunk
->list
);
764 /* Pick the right transport to use. */
765 new_transport
= chunk
->transport
;
767 if (!new_transport
) {
769 * If we have a prior transport pointer, see if
770 * the destination address of the chunk
771 * matches the destination address of the
772 * current transport. If not a match, then
773 * try to look up the transport with a given
774 * destination address. We do this because
775 * after processing ASCONFs, we may have new
776 * transports created.
779 sctp_cmp_addr_exact(&chunk
->dest
,
781 new_transport
= transport
;
783 new_transport
= sctp_assoc_lookup_paddr(asoc
,
786 /* if we still don't have a new transport, then
787 * use the current active path.
790 new_transport
= asoc
->peer
.active_path
;
791 } else if ((new_transport
->state
== SCTP_INACTIVE
) ||
792 (new_transport
->state
== SCTP_UNCONFIRMED
) ||
793 (new_transport
->state
== SCTP_PF
)) {
794 /* If the chunk is Heartbeat or Heartbeat Ack,
795 * send it to chunk->transport, even if it's
798 * 3.3.6 Heartbeat Acknowledgement:
800 * A HEARTBEAT ACK is always sent to the source IP
801 * address of the IP datagram containing the
802 * HEARTBEAT chunk to which this ack is responding.
805 * ASCONF_ACKs also must be sent to the source.
807 if (chunk
->chunk_hdr
->type
!= SCTP_CID_HEARTBEAT
&&
808 chunk
->chunk_hdr
->type
!= SCTP_CID_HEARTBEAT_ACK
&&
809 chunk
->chunk_hdr
->type
!= SCTP_CID_ASCONF_ACK
)
810 new_transport
= asoc
->peer
.active_path
;
813 /* Are we switching transports?
814 * Take care of transport locks.
816 if (new_transport
!= transport
) {
817 transport
= new_transport
;
818 if (list_empty(&transport
->send_ready
)) {
819 list_add_tail(&transport
->send_ready
,
822 packet
= &transport
->packet
;
823 sctp_packet_config(packet
, vtag
,
824 asoc
->peer
.ecn_capable
);
827 switch (chunk
->chunk_hdr
->type
) {
831 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
832 * COMPLETE with any other chunks. [Send them immediately.]
835 case SCTP_CID_INIT_ACK
:
836 case SCTP_CID_SHUTDOWN_COMPLETE
:
837 sctp_packet_init(&singleton
, transport
, sport
, dport
);
838 sctp_packet_config(&singleton
, vtag
, 0);
839 sctp_packet_append_chunk(&singleton
, chunk
);
840 error
= sctp_packet_transmit(&singleton
);
846 if (sctp_test_T_bit(chunk
)) {
847 packet
->vtag
= asoc
->c
.my_vtag
;
849 /* The following chunks are "response" chunks, i.e.
850 * they are generated in response to something we
851 * received. If we are sending these, then we can
852 * send only 1 packet containing these chunks.
854 case SCTP_CID_HEARTBEAT_ACK
:
855 case SCTP_CID_SHUTDOWN_ACK
:
856 case SCTP_CID_COOKIE_ACK
:
857 case SCTP_CID_COOKIE_ECHO
:
859 case SCTP_CID_ECN_CWR
:
860 case SCTP_CID_ASCONF_ACK
:
865 case SCTP_CID_HEARTBEAT
:
866 case SCTP_CID_SHUTDOWN
:
867 case SCTP_CID_ECN_ECNE
:
868 case SCTP_CID_ASCONF
:
869 case SCTP_CID_FWD_TSN
:
870 status
= sctp_packet_transmit_chunk(packet
, chunk
,
872 if (status
!= SCTP_XMIT_OK
) {
873 /* put the chunk back */
874 list_add(&chunk
->list
, &q
->control_chunk_list
);
876 asoc
->stats
.octrlchunks
++;
877 /* PR-SCTP C5) If a FORWARD TSN is sent, the
878 * sender MUST assure that at least one T3-rtx
881 if (chunk
->chunk_hdr
->type
== SCTP_CID_FWD_TSN
)
882 sctp_transport_reset_timers(transport
);
887 /* We built a chunk with an illegal type! */
892 if (q
->asoc
->src_out_of_asoc_ok
)
895 /* Is it OK to send data chunks? */
896 switch (asoc
->state
) {
897 case SCTP_STATE_COOKIE_ECHOED
:
898 /* Only allow bundling when this packet has a COOKIE-ECHO
901 if (!packet
|| !packet
->has_cookie_echo
)
905 case SCTP_STATE_ESTABLISHED
:
906 case SCTP_STATE_SHUTDOWN_PENDING
:
907 case SCTP_STATE_SHUTDOWN_RECEIVED
:
909 * RFC 2960 6.1 Transmission of DATA Chunks
911 * C) When the time comes for the sender to transmit,
912 * before sending new DATA chunks, the sender MUST
913 * first transmit any outstanding DATA chunks which
914 * are marked for retransmission (limited by the
917 if (!list_empty(&q
->retransmit
)) {
918 if (asoc
->peer
.retran_path
->state
== SCTP_UNCONFIRMED
)
920 if (transport
== asoc
->peer
.retran_path
)
923 /* Switch transports & prepare the packet. */
925 transport
= asoc
->peer
.retran_path
;
927 if (list_empty(&transport
->send_ready
)) {
928 list_add_tail(&transport
->send_ready
,
932 packet
= &transport
->packet
;
933 sctp_packet_config(packet
, vtag
,
934 asoc
->peer
.ecn_capable
);
936 error
= sctp_outq_flush_rtx(q
, packet
,
937 rtx_timeout
, &start_timer
);
940 sctp_transport_reset_timers(transport
);
942 /* This can happen on COOKIE-ECHO resend. Only
943 * one chunk can get bundled with a COOKIE-ECHO.
945 if (packet
->has_cookie_echo
)
948 /* Don't send new data if there is still data
949 * waiting to retransmit.
951 if (!list_empty(&q
->retransmit
))
955 /* Apply Max.Burst limitation to the current transport in
956 * case it will be used for new data. We are going to
957 * rest it before we return, but we want to apply the limit
958 * to the currently queued data.
961 sctp_transport_burst_limited(transport
);
963 /* Finally, transmit new packets. */
964 while ((chunk
= sctp_outq_dequeue_data(q
)) != NULL
) {
965 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
968 if (chunk
->sinfo
.sinfo_stream
>=
969 asoc
->c
.sinit_num_ostreams
) {
971 /* Mark as failed send. */
972 sctp_chunk_fail(chunk
, SCTP_ERROR_INV_STRM
);
973 sctp_chunk_free(chunk
);
977 /* Has this chunk expired? */
978 if (sctp_chunk_abandoned(chunk
)) {
979 sctp_chunk_fail(chunk
, 0);
980 sctp_chunk_free(chunk
);
984 /* If there is a specified transport, use it.
985 * Otherwise, we want to use the active path.
987 new_transport
= chunk
->transport
;
988 if (!new_transport
||
989 ((new_transport
->state
== SCTP_INACTIVE
) ||
990 (new_transport
->state
== SCTP_UNCONFIRMED
) ||
991 (new_transport
->state
== SCTP_PF
)))
992 new_transport
= asoc
->peer
.active_path
;
993 if (new_transport
->state
== SCTP_UNCONFIRMED
)
996 /* Change packets if necessary. */
997 if (new_transport
!= transport
) {
998 transport
= new_transport
;
1000 /* Schedule to have this transport's
1003 if (list_empty(&transport
->send_ready
)) {
1004 list_add_tail(&transport
->send_ready
,
1008 packet
= &transport
->packet
;
1009 sctp_packet_config(packet
, vtag
,
1010 asoc
->peer
.ecn_capable
);
1011 /* We've switched transports, so apply the
1012 * Burst limit to the new transport.
1014 sctp_transport_burst_limited(transport
);
1017 SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
1019 chunk
&& chunk
->chunk_hdr
?
1020 sctp_cname(SCTP_ST_CHUNK(
1021 chunk
->chunk_hdr
->type
))
1024 SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
1025 "%p skb->users %d.\n",
1026 ntohl(chunk
->subh
.data_hdr
->tsn
),
1027 chunk
->skb
?chunk
->skb
->head
: NULL
,
1029 atomic_read(&chunk
->skb
->users
) : -1);
1031 /* Add the chunk to the packet. */
1032 status
= sctp_packet_transmit_chunk(packet
, chunk
, 0);
1035 case SCTP_XMIT_PMTU_FULL
:
1036 case SCTP_XMIT_RWND_FULL
:
1037 case SCTP_XMIT_NAGLE_DELAY
:
1038 /* We could not append this chunk, so put
1039 * the chunk back on the output queue.
1041 SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
1042 "not transmit TSN: 0x%x, status: %d\n",
1043 ntohl(chunk
->subh
.data_hdr
->tsn
),
1045 sctp_outq_head_data(q
, chunk
);
1046 goto sctp_flush_out
;
1050 /* The sender is in the SHUTDOWN-PENDING state,
1051 * The sender MAY set the I-bit in the DATA
1054 if (asoc
->state
== SCTP_STATE_SHUTDOWN_PENDING
)
1055 chunk
->chunk_hdr
->flags
|= SCTP_DATA_SACK_IMM
;
1056 if (chunk
->chunk_hdr
->flags
& SCTP_DATA_UNORDERED
)
1057 asoc
->stats
.ouodchunks
++;
1059 asoc
->stats
.oodchunks
++;
1067 /* BUG: We assume that the sctp_packet_transmit()
1068 * call below will succeed all the time and add the
1069 * chunk to the transmitted list and restart the
1071 * It is possible that the call can fail under OOM
1074 * Is this really a problem? Won't this behave
1077 list_add_tail(&chunk
->transmitted_list
,
1078 &transport
->transmitted
);
1080 sctp_transport_reset_timers(transport
);
1084 /* Only let one DATA chunk get bundled with a
1085 * COOKIE-ECHO chunk.
1087 if (packet
->has_cookie_echo
)
1088 goto sctp_flush_out
;
1099 /* Before returning, examine all the transports touched in
1100 * this call. Right now, we bluntly force clear all the
1101 * transports. Things might change after we implement Nagle.
1102 * But such an examination is still required.
1106 while ((ltransport
= sctp_list_dequeue(&transport_list
)) != NULL
) {
1107 struct sctp_transport
*t
= list_entry(ltransport
,
1108 struct sctp_transport
,
1110 packet
= &t
->packet
;
1111 if (!sctp_packet_empty(packet
))
1112 error
= sctp_packet_transmit(packet
);
1114 /* Clear the burst limited state, if any */
1115 sctp_transport_burst_reset(t
);
1121 /* Update unack_data based on the incoming SACK chunk */
1122 static void sctp_sack_update_unack_data(struct sctp_association
*assoc
,
1123 struct sctp_sackhdr
*sack
)
1125 sctp_sack_variable_t
*frags
;
1129 unack_data
= assoc
->next_tsn
- assoc
->ctsn_ack_point
- 1;
1131 frags
= sack
->variable
;
1132 for (i
= 0; i
< ntohs(sack
->num_gap_ack_blocks
); i
++) {
1133 unack_data
-= ((ntohs(frags
[i
].gab
.end
) -
1134 ntohs(frags
[i
].gab
.start
) + 1));
1137 assoc
->unack_data
= unack_data
;
1140 /* This is where we REALLY process a SACK.
1142 * Process the SACK against the outqueue. Mostly, this just frees
1143 * things off the transmitted queue.
1145 int sctp_outq_sack(struct sctp_outq
*q
, struct sctp_chunk
*chunk
)
1147 struct sctp_association
*asoc
= q
->asoc
;
1148 struct sctp_sackhdr
*sack
= chunk
->subh
.sack_hdr
;
1149 struct sctp_transport
*transport
;
1150 struct sctp_chunk
*tchunk
= NULL
;
1151 struct list_head
*lchunk
, *transport_list
, *temp
;
1152 sctp_sack_variable_t
*frags
= sack
->variable
;
1153 __u32 sack_ctsn
, ctsn
, tsn
;
1154 __u32 highest_tsn
, highest_new_tsn
;
1156 unsigned int outstanding
;
1157 struct sctp_transport
*primary
= asoc
->peer
.primary_path
;
1158 int count_of_newacks
= 0;
1162 /* Grab the association's destination address list. */
1163 transport_list
= &asoc
->peer
.transport_addr_list
;
1165 sack_ctsn
= ntohl(sack
->cum_tsn_ack
);
1166 gap_ack_blocks
= ntohs(sack
->num_gap_ack_blocks
);
1167 asoc
->stats
.gapcnt
+= gap_ack_blocks
;
1169 * SFR-CACC algorithm:
1170 * On receipt of a SACK the sender SHOULD execute the
1171 * following statements.
1173 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1174 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1175 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1177 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1178 * is set the receiver of the SACK MUST take the following actions:
1180 * A) Initialize the cacc_saw_newack to 0 for all destination
1183 * Only bother if changeover_active is set. Otherwise, this is
1184 * totally suboptimal to do on every SACK.
1186 if (primary
->cacc
.changeover_active
) {
1187 u8 clear_cycling
= 0;
1189 if (TSN_lte(primary
->cacc
.next_tsn_at_change
, sack_ctsn
)) {
1190 primary
->cacc
.changeover_active
= 0;
1194 if (clear_cycling
|| gap_ack_blocks
) {
1195 list_for_each_entry(transport
, transport_list
,
1198 transport
->cacc
.cycling_changeover
= 0;
1200 transport
->cacc
.cacc_saw_newack
= 0;
1205 /* Get the highest TSN in the sack. */
1206 highest_tsn
= sack_ctsn
;
1208 highest_tsn
+= ntohs(frags
[gap_ack_blocks
- 1].gab
.end
);
1210 if (TSN_lt(asoc
->highest_sacked
, highest_tsn
))
1211 asoc
->highest_sacked
= highest_tsn
;
1213 highest_new_tsn
= sack_ctsn
;
1215 /* Run through the retransmit queue. Credit bytes received
1216 * and free those chunks that we can.
1218 sctp_check_transmitted(q
, &q
->retransmit
, NULL
, NULL
, sack
, &highest_new_tsn
);
1220 /* Run through the transmitted queue.
1221 * Credit bytes received and free those chunks which we can.
1223 * This is a MASSIVE candidate for optimization.
1225 list_for_each_entry(transport
, transport_list
, transports
) {
1226 sctp_check_transmitted(q
, &transport
->transmitted
,
1227 transport
, &chunk
->source
, sack
,
1230 * SFR-CACC algorithm:
1231 * C) Let count_of_newacks be the number of
1232 * destinations for which cacc_saw_newack is set.
1234 if (transport
->cacc
.cacc_saw_newack
)
1235 count_of_newacks
++;
1238 /* Move the Cumulative TSN Ack Point if appropriate. */
1239 if (TSN_lt(asoc
->ctsn_ack_point
, sack_ctsn
)) {
1240 asoc
->ctsn_ack_point
= sack_ctsn
;
1244 if (gap_ack_blocks
) {
1246 if (asoc
->fast_recovery
&& accum_moved
)
1247 highest_new_tsn
= highest_tsn
;
1249 list_for_each_entry(transport
, transport_list
, transports
)
1250 sctp_mark_missing(q
, &transport
->transmitted
, transport
,
1251 highest_new_tsn
, count_of_newacks
);
1254 /* Update unack_data field in the assoc. */
1255 sctp_sack_update_unack_data(asoc
, sack
);
1257 ctsn
= asoc
->ctsn_ack_point
;
1259 /* Throw away stuff rotting on the sack queue. */
1260 list_for_each_safe(lchunk
, temp
, &q
->sacked
) {
1261 tchunk
= list_entry(lchunk
, struct sctp_chunk
,
1263 tsn
= ntohl(tchunk
->subh
.data_hdr
->tsn
);
1264 if (TSN_lte(tsn
, ctsn
)) {
1265 list_del_init(&tchunk
->transmitted_list
);
1266 sctp_chunk_free(tchunk
);
1270 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1271 * number of bytes still outstanding after processing the
1272 * Cumulative TSN Ack and the Gap Ack Blocks.
1275 sack_a_rwnd
= ntohl(sack
->a_rwnd
);
1276 outstanding
= q
->outstanding_bytes
;
1278 if (outstanding
< sack_a_rwnd
)
1279 sack_a_rwnd
-= outstanding
;
1283 asoc
->peer
.rwnd
= sack_a_rwnd
;
1285 sctp_generate_fwdtsn(q
, sack_ctsn
);
1287 SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.\n",
1288 __func__
, sack_ctsn
);
1289 SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
1290 "%p is 0x%x. Adv peer ack point: 0x%x\n",
1291 __func__
, asoc
, ctsn
, asoc
->adv_peer_ack_point
);
1293 /* See if all chunks are acked.
1294 * Make sure the empty queue handler will get run later.
1296 q
->empty
= (list_empty(&q
->out_chunk_list
) &&
1297 list_empty(&q
->retransmit
));
1301 list_for_each_entry(transport
, transport_list
, transports
) {
1302 q
->empty
= q
->empty
&& list_empty(&transport
->transmitted
);
1307 SCTP_DEBUG_PRINTK("sack queue is empty.\n");
1312 /* Is the outqueue empty? */
1313 int sctp_outq_is_empty(const struct sctp_outq
*q
)
1318 /********************************************************************
1319 * 2nd Level Abstractions
1320 ********************************************************************/
1322 /* Go through a transport's transmitted list or the association's retransmit
1323 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1324 * The retransmit list will not have an associated transport.
1326 * I added coherent debug information output. --xguo
1328 * Instead of printing 'sacked' or 'kept' for each TSN on the
1329 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1330 * KEPT TSN6-TSN7, etc.
1332 static void sctp_check_transmitted(struct sctp_outq
*q
,
1333 struct list_head
*transmitted_queue
,
1334 struct sctp_transport
*transport
,
1335 union sctp_addr
*saddr
,
1336 struct sctp_sackhdr
*sack
,
1337 __u32
*highest_new_tsn_in_sack
)
1339 struct list_head
*lchunk
;
1340 struct sctp_chunk
*tchunk
;
1341 struct list_head tlist
;
1345 __u8 restart_timer
= 0;
1346 int bytes_acked
= 0;
1347 int migrate_bytes
= 0;
1349 /* These state variables are for coherent debug output. --xguo */
1352 __u32 dbg_ack_tsn
= 0; /* An ACKed TSN range starts here... */
1353 __u32 dbg_last_ack_tsn
= 0; /* ...and finishes here. */
1354 __u32 dbg_kept_tsn
= 0; /* An un-ACKed range starts here... */
1355 __u32 dbg_last_kept_tsn
= 0; /* ...and finishes here. */
1357 /* 0 : The last TSN was ACKed.
1358 * 1 : The last TSN was NOT ACKed (i.e. KEPT).
1359 * -1: We need to initialize.
1361 int dbg_prt_state
= -1;
1362 #endif /* SCTP_DEBUG */
1364 sack_ctsn
= ntohl(sack
->cum_tsn_ack
);
1366 INIT_LIST_HEAD(&tlist
);
1368 /* The while loop will skip empty transmitted queues. */
1369 while (NULL
!= (lchunk
= sctp_list_dequeue(transmitted_queue
))) {
1370 tchunk
= list_entry(lchunk
, struct sctp_chunk
,
1373 if (sctp_chunk_abandoned(tchunk
)) {
1374 /* Move the chunk to abandoned list. */
1375 sctp_insert_list(&q
->abandoned
, lchunk
);
1377 /* If this chunk has not been acked, stop
1378 * considering it as 'outstanding'.
1380 if (!tchunk
->tsn_gap_acked
) {
1381 if (tchunk
->transport
)
1382 tchunk
->transport
->flight_size
-=
1383 sctp_data_size(tchunk
);
1384 q
->outstanding_bytes
-= sctp_data_size(tchunk
);
1389 tsn
= ntohl(tchunk
->subh
.data_hdr
->tsn
);
1390 if (sctp_acked(sack
, tsn
)) {
1391 /* If this queue is the retransmit queue, the
1392 * retransmit timer has already reclaimed
1393 * the outstanding bytes for this chunk, so only
1394 * count bytes associated with a transport.
1397 /* If this chunk is being used for RTT
1398 * measurement, calculate the RTT and update
1399 * the RTO using this value.
1401 * 6.3.1 C5) Karn's algorithm: RTT measurements
1402 * MUST NOT be made using packets that were
1403 * retransmitted (and thus for which it is
1404 * ambiguous whether the reply was for the
1405 * first instance of the packet or a later
1408 if (!tchunk
->tsn_gap_acked
&&
1409 tchunk
->rtt_in_progress
) {
1410 tchunk
->rtt_in_progress
= 0;
1411 rtt
= jiffies
- tchunk
->sent_at
;
1412 sctp_transport_update_rto(transport
,
1417 /* If the chunk hasn't been marked as ACKED,
1418 * mark it and account bytes_acked if the
1419 * chunk had a valid transport (it will not
1420 * have a transport if ASCONF had deleted it
1421 * while DATA was outstanding).
1423 if (!tchunk
->tsn_gap_acked
) {
1424 tchunk
->tsn_gap_acked
= 1;
1425 *highest_new_tsn_in_sack
= tsn
;
1426 bytes_acked
+= sctp_data_size(tchunk
);
1427 if (!tchunk
->transport
)
1428 migrate_bytes
+= sctp_data_size(tchunk
);
1431 if (TSN_lte(tsn
, sack_ctsn
)) {
1432 /* RFC 2960 6.3.2 Retransmission Timer Rules
1434 * R3) Whenever a SACK is received
1435 * that acknowledges the DATA chunk
1436 * with the earliest outstanding TSN
1437 * for that address, restart T3-rtx
1438 * timer for that address with its
1443 if (!tchunk
->tsn_gap_acked
) {
1445 * SFR-CACC algorithm:
1446 * 2) If the SACK contains gap acks
1447 * and the flag CHANGEOVER_ACTIVE is
1448 * set the receiver of the SACK MUST
1449 * take the following action:
1451 * B) For each TSN t being acked that
1452 * has not been acked in any SACK so
1453 * far, set cacc_saw_newack to 1 for
1454 * the destination that the TSN was
1458 sack
->num_gap_ack_blocks
&&
1459 q
->asoc
->peer
.primary_path
->cacc
.
1461 transport
->cacc
.cacc_saw_newack
1465 list_add_tail(&tchunk
->transmitted_list
,
1468 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1469 * M2) Each time a SACK arrives reporting
1470 * 'Stray DATA chunk(s)' record the highest TSN
1471 * reported as newly acknowledged, call this
1472 * value 'HighestTSNinSack'. A newly
1473 * acknowledged DATA chunk is one not
1474 * previously acknowledged in a SACK.
1476 * When the SCTP sender of data receives a SACK
1477 * chunk that acknowledges, for the first time,
1478 * the receipt of a DATA chunk, all the still
1479 * unacknowledged DATA chunks whose TSN is
1480 * older than that newly acknowledged DATA
1481 * chunk, are qualified as 'Stray DATA chunks'.
1483 list_add_tail(lchunk
, &tlist
);
1487 switch (dbg_prt_state
) {
1488 case 0: /* last TSN was ACKed */
1489 if (dbg_last_ack_tsn
+ 1 == tsn
) {
1490 /* This TSN belongs to the
1491 * current ACK range.
1496 if (dbg_last_ack_tsn
!= dbg_ack_tsn
) {
1497 /* Display the end of the
1500 SCTP_DEBUG_PRINTK_CONT("-%08x",
1504 /* Start a new range. */
1505 SCTP_DEBUG_PRINTK_CONT(",%08x", tsn
);
1509 case 1: /* The last TSN was NOT ACKed. */
1510 if (dbg_last_kept_tsn
!= dbg_kept_tsn
) {
1511 /* Display the end of current range. */
1512 SCTP_DEBUG_PRINTK_CONT("-%08x",
1516 SCTP_DEBUG_PRINTK_CONT("\n");
1518 /* FALL THROUGH... */
1520 /* This is the first-ever TSN we examined. */
1521 /* Start a new range of ACK-ed TSNs. */
1522 SCTP_DEBUG_PRINTK("ACKed: %08x", tsn
);
1527 dbg_last_ack_tsn
= tsn
;
1528 #endif /* SCTP_DEBUG */
1531 if (tchunk
->tsn_gap_acked
) {
1532 SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
1536 tchunk
->tsn_gap_acked
= 0;
1538 if (tchunk
->transport
)
1539 bytes_acked
-= sctp_data_size(tchunk
);
1541 /* RFC 2960 6.3.2 Retransmission Timer Rules
1543 * R4) Whenever a SACK is received missing a
1544 * TSN that was previously acknowledged via a
1545 * Gap Ack Block, start T3-rtx for the
1546 * destination address to which the DATA
1547 * chunk was originally
1548 * transmitted if it is not already running.
1553 list_add_tail(lchunk
, &tlist
);
1556 /* See the above comments on ACK-ed TSNs. */
1557 switch (dbg_prt_state
) {
1559 if (dbg_last_kept_tsn
+ 1 == tsn
)
1562 if (dbg_last_kept_tsn
!= dbg_kept_tsn
)
1563 SCTP_DEBUG_PRINTK_CONT("-%08x",
1566 SCTP_DEBUG_PRINTK_CONT(",%08x", tsn
);
1571 if (dbg_last_ack_tsn
!= dbg_ack_tsn
)
1572 SCTP_DEBUG_PRINTK_CONT("-%08x",
1574 SCTP_DEBUG_PRINTK_CONT("\n");
1576 /* FALL THROUGH... */
1578 SCTP_DEBUG_PRINTK("KEPT: %08x",tsn
);
1583 dbg_last_kept_tsn
= tsn
;
1584 #endif /* SCTP_DEBUG */
1589 /* Finish off the last range, displaying its ending TSN. */
1590 switch (dbg_prt_state
) {
1592 if (dbg_last_ack_tsn
!= dbg_ack_tsn
) {
1593 SCTP_DEBUG_PRINTK_CONT("-%08x\n", dbg_last_ack_tsn
);
1595 SCTP_DEBUG_PRINTK_CONT("\n");
1600 if (dbg_last_kept_tsn
!= dbg_kept_tsn
) {
1601 SCTP_DEBUG_PRINTK_CONT("-%08x\n", dbg_last_kept_tsn
);
1603 SCTP_DEBUG_PRINTK_CONT("\n");
1606 #endif /* SCTP_DEBUG */
1609 struct sctp_association
*asoc
= transport
->asoc
;
1611 /* We may have counted DATA that was migrated
1612 * to this transport due to DEL-IP operation.
1613 * Subtract those bytes, since the were never
1614 * send on this transport and shouldn't be
1615 * credited to this transport.
1617 bytes_acked
-= migrate_bytes
;
1619 /* 8.2. When an outstanding TSN is acknowledged,
1620 * the endpoint shall clear the error counter of
1621 * the destination transport address to which the
1622 * DATA chunk was last sent.
1623 * The association's overall error counter is
1626 transport
->error_count
= 0;
1627 transport
->asoc
->overall_error_count
= 0;
1630 * While in SHUTDOWN PENDING, we may have started
1631 * the T5 shutdown guard timer after reaching the
1632 * retransmission limit. Stop that timer as soon
1633 * as the receiver acknowledged any data.
1635 if (asoc
->state
== SCTP_STATE_SHUTDOWN_PENDING
&&
1636 del_timer(&asoc
->timers
1637 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD
]))
1638 sctp_association_put(asoc
);
1640 /* Mark the destination transport address as
1641 * active if it is not so marked.
1643 if ((transport
->state
== SCTP_INACTIVE
||
1644 transport
->state
== SCTP_UNCONFIRMED
) &&
1645 sctp_cmp_addr_exact(&transport
->ipaddr
, saddr
)) {
1646 sctp_assoc_control_transport(
1650 SCTP_RECEIVED_SACK
);
1653 sctp_transport_raise_cwnd(transport
, sack_ctsn
,
1656 transport
->flight_size
-= bytes_acked
;
1657 if (transport
->flight_size
== 0)
1658 transport
->partial_bytes_acked
= 0;
1659 q
->outstanding_bytes
-= bytes_acked
+ migrate_bytes
;
1661 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1662 * When a sender is doing zero window probing, it
1663 * should not timeout the association if it continues
1664 * to receive new packets from the receiver. The
1665 * reason is that the receiver MAY keep its window
1666 * closed for an indefinite time.
1667 * A sender is doing zero window probing when the
1668 * receiver's advertised window is zero, and there is
1669 * only one data chunk in flight to the receiver.
1671 * Allow the association to timeout while in SHUTDOWN
1672 * PENDING or SHUTDOWN RECEIVED in case the receiver
1673 * stays in zero window mode forever.
1675 if (!q
->asoc
->peer
.rwnd
&&
1676 !list_empty(&tlist
) &&
1677 (sack_ctsn
+2 == q
->asoc
->next_tsn
) &&
1678 q
->asoc
->state
< SCTP_STATE_SHUTDOWN_PENDING
) {
1679 SCTP_DEBUG_PRINTK("%s: SACK received for zero "
1680 "window probe: %u\n",
1681 __func__
, sack_ctsn
);
1682 q
->asoc
->overall_error_count
= 0;
1683 transport
->error_count
= 0;
1687 /* RFC 2960 6.3.2 Retransmission Timer Rules
1689 * R2) Whenever all outstanding data sent to an address have
1690 * been acknowledged, turn off the T3-rtx timer of that
1693 if (!transport
->flight_size
) {
1694 if (del_timer(&transport
->T3_rtx_timer
))
1695 sctp_transport_put(transport
);
1696 } else if (restart_timer
) {
1697 if (!mod_timer(&transport
->T3_rtx_timer
,
1698 jiffies
+ transport
->rto
))
1699 sctp_transport_hold(transport
);
1703 list_splice(&tlist
, transmitted_queue
);
1706 /* Mark chunks as missing and consequently may get retransmitted. */
1707 static void sctp_mark_missing(struct sctp_outq
*q
,
1708 struct list_head
*transmitted_queue
,
1709 struct sctp_transport
*transport
,
1710 __u32 highest_new_tsn_in_sack
,
1711 int count_of_newacks
)
1713 struct sctp_chunk
*chunk
;
1715 char do_fast_retransmit
= 0;
1716 struct sctp_association
*asoc
= q
->asoc
;
1717 struct sctp_transport
*primary
= asoc
->peer
.primary_path
;
1719 list_for_each_entry(chunk
, transmitted_queue
, transmitted_list
) {
1721 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1723 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1724 * 'Unacknowledged TSN's', if the TSN number of an
1725 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1726 * value, increment the 'TSN.Missing.Report' count on that
1727 * chunk if it has NOT been fast retransmitted or marked for
1728 * fast retransmit already.
1730 if (chunk
->fast_retransmit
== SCTP_CAN_FRTX
&&
1731 !chunk
->tsn_gap_acked
&&
1732 TSN_lt(tsn
, highest_new_tsn_in_sack
)) {
1734 /* SFR-CACC may require us to skip marking
1735 * this chunk as missing.
1737 if (!transport
|| !sctp_cacc_skip(primary
,
1739 count_of_newacks
, tsn
)) {
1740 chunk
->tsn_missing_report
++;
1743 "%s: TSN 0x%x missing counter: %d\n",
1745 chunk
->tsn_missing_report
);
1749 * M4) If any DATA chunk is found to have a
1750 * 'TSN.Missing.Report'
1751 * value larger than or equal to 3, mark that chunk for
1752 * retransmission and start the fast retransmit procedure.
1755 if (chunk
->tsn_missing_report
>= 3) {
1756 chunk
->fast_retransmit
= SCTP_NEED_FRTX
;
1757 do_fast_retransmit
= 1;
1762 if (do_fast_retransmit
)
1763 sctp_retransmit(q
, transport
, SCTP_RTXR_FAST_RTX
);
1765 SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
1766 "ssthresh: %d, flight_size: %d, pba: %d\n",
1767 __func__
, transport
, transport
->cwnd
,
1768 transport
->ssthresh
, transport
->flight_size
,
1769 transport
->partial_bytes_acked
);
1773 /* Is the given TSN acked by this packet? */
1774 static int sctp_acked(struct sctp_sackhdr
*sack
, __u32 tsn
)
1777 sctp_sack_variable_t
*frags
;
1779 __u32 ctsn
= ntohl(sack
->cum_tsn_ack
);
1781 if (TSN_lte(tsn
, ctsn
))
1784 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1787 * These fields contain the Gap Ack Blocks. They are repeated
1788 * for each Gap Ack Block up to the number of Gap Ack Blocks
1789 * defined in the Number of Gap Ack Blocks field. All DATA
1790 * chunks with TSNs greater than or equal to (Cumulative TSN
1791 * Ack + Gap Ack Block Start) and less than or equal to
1792 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1793 * Block are assumed to have been received correctly.
1796 frags
= sack
->variable
;
1798 for (i
= 0; i
< ntohs(sack
->num_gap_ack_blocks
); ++i
) {
1799 if (TSN_lte(ntohs(frags
[i
].gab
.start
), gap
) &&
1800 TSN_lte(gap
, ntohs(frags
[i
].gab
.end
)))
1809 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip
*skiplist
,
1810 int nskips
, __be16 stream
)
1814 for (i
= 0; i
< nskips
; i
++) {
1815 if (skiplist
[i
].stream
== stream
)
1821 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1822 static void sctp_generate_fwdtsn(struct sctp_outq
*q
, __u32 ctsn
)
1824 struct sctp_association
*asoc
= q
->asoc
;
1825 struct sctp_chunk
*ftsn_chunk
= NULL
;
1826 struct sctp_fwdtsn_skip ftsn_skip_arr
[10];
1830 struct sctp_chunk
*chunk
;
1831 struct list_head
*lchunk
, *temp
;
1833 if (!asoc
->peer
.prsctp_capable
)
1836 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1839 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1840 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1842 if (TSN_lt(asoc
->adv_peer_ack_point
, ctsn
))
1843 asoc
->adv_peer_ack_point
= ctsn
;
1845 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1846 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1847 * the chunk next in the out-queue space is marked as "abandoned" as
1848 * shown in the following example:
1850 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1851 * and the Advanced.Peer.Ack.Point is updated to this value:
1853 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1854 * normal SACK processing local advancement
1856 * Adv.Ack.Pt-> 102 acked 102 acked
1857 * 103 abandoned 103 abandoned
1858 * 104 abandoned Adv.Ack.P-> 104 abandoned
1860 * 106 acked 106 acked
1863 * In this example, the data sender successfully advanced the
1864 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1866 list_for_each_safe(lchunk
, temp
, &q
->abandoned
) {
1867 chunk
= list_entry(lchunk
, struct sctp_chunk
,
1869 tsn
= ntohl(chunk
->subh
.data_hdr
->tsn
);
1871 /* Remove any chunks in the abandoned queue that are acked by
1874 if (TSN_lte(tsn
, ctsn
)) {
1875 list_del_init(lchunk
);
1876 sctp_chunk_free(chunk
);
1878 if (TSN_lte(tsn
, asoc
->adv_peer_ack_point
+1)) {
1879 asoc
->adv_peer_ack_point
= tsn
;
1880 if (chunk
->chunk_hdr
->flags
&
1881 SCTP_DATA_UNORDERED
)
1883 skip_pos
= sctp_get_skip_pos(&ftsn_skip_arr
[0],
1885 chunk
->subh
.data_hdr
->stream
);
1886 ftsn_skip_arr
[skip_pos
].stream
=
1887 chunk
->subh
.data_hdr
->stream
;
1888 ftsn_skip_arr
[skip_pos
].ssn
=
1889 chunk
->subh
.data_hdr
->ssn
;
1890 if (skip_pos
== nskips
)
1899 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1900 * is greater than the Cumulative TSN ACK carried in the received
1901 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1902 * chunk containing the latest value of the
1903 * "Advanced.Peer.Ack.Point".
1905 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1906 * list each stream and sequence number in the forwarded TSN. This
1907 * information will enable the receiver to easily find any
1908 * stranded TSN's waiting on stream reorder queues. Each stream
1909 * SHOULD only be reported once; this means that if multiple
1910 * abandoned messages occur in the same stream then only the
1911 * highest abandoned stream sequence number is reported. If the
1912 * total size of the FORWARD TSN does NOT fit in a single MTU then
1913 * the sender of the FORWARD TSN SHOULD lower the
1914 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1917 if (asoc
->adv_peer_ack_point
> ctsn
)
1918 ftsn_chunk
= sctp_make_fwdtsn(asoc
, asoc
->adv_peer_ack_point
,
1919 nskips
, &ftsn_skip_arr
[0]);
1922 list_add_tail(&ftsn_chunk
->list
, &q
->control_chunk_list
);
1923 SCTP_INC_STATS(sock_net(asoc
->base
.sk
), SCTP_MIB_OUTCTRLCHUNKS
);