]> Pileus Git - ~andy/linux/blob - net/sctp/outqueue.c
sctp: Start T3-RTX timer when fast retransmitting lowest TSN
[~andy/linux] / net / sctp / outqueue.c
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.
6  *
7  * This file is part of the SCTP kernel implementation
8  *
9  * These functions implement the sctp_outq class.   The outqueue handles
10  * bundling and queueing of outgoing SCTP chunks.
11  *
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)
16  * any later version.
17  *
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.
23  *
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.
28  *
29  * Please send any bug reports or fixes you make to the
30  * email address(es):
31  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
32  *
33  * Or submit a bug report through the following website:
34  *    http://www.sf.net/projects/lksctp
35  *
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>
44  *
45  * Any bugs reported given to us we will try to fix... any fixes shared will
46  * be incorporated into the next SCTP release.
47  */
48
49 #include <linux/types.h>
50 #include <linux/list.h>   /* For struct list_head */
51 #include <linux/socket.h>
52 #include <linux/ip.h>
53 #include <net/sock.h>     /* For skb_set_owner_w */
54
55 #include <net/sctp/sctp.h>
56 #include <net/sctp/sm.h>
57
58 /* Declare internal functions here.  */
59 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
60 static void sctp_check_transmitted(struct sctp_outq *q,
61                                    struct list_head *transmitted_queue,
62                                    struct sctp_transport *transport,
63                                    struct sctp_sackhdr *sack,
64                                    __u32 highest_new_tsn);
65
66 static void sctp_mark_missing(struct sctp_outq *q,
67                               struct list_head *transmitted_queue,
68                               struct sctp_transport *transport,
69                               __u32 highest_new_tsn,
70                               int count_of_newacks);
71
72 static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 sack_ctsn);
73
74 /* Add data to the front of the queue. */
75 static inline void sctp_outq_head_data(struct sctp_outq *q,
76                                         struct sctp_chunk *ch)
77 {
78         list_add(&ch->list, &q->out_chunk_list);
79         q->out_qlen += ch->skb->len;
80         return;
81 }
82
83 /* Take data from the front of the queue. */
84 static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
85 {
86         struct sctp_chunk *ch = NULL;
87
88         if (!list_empty(&q->out_chunk_list)) {
89                 struct list_head *entry = q->out_chunk_list.next;
90
91                 ch = list_entry(entry, struct sctp_chunk, list);
92                 list_del_init(entry);
93                 q->out_qlen -= ch->skb->len;
94         }
95         return ch;
96 }
97 /* Add data chunk to the end of the queue. */
98 static inline void sctp_outq_tail_data(struct sctp_outq *q,
99                                        struct sctp_chunk *ch)
100 {
101         list_add_tail(&ch->list, &q->out_chunk_list);
102         q->out_qlen += ch->skb->len;
103         return;
104 }
105
106 /*
107  * SFR-CACC algorithm:
108  * D) If count_of_newacks is greater than or equal to 2
109  * and t was not sent to the current primary then the
110  * sender MUST NOT increment missing report count for t.
111  */
112 static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
113                                        struct sctp_transport *transport,
114                                        int count_of_newacks)
115 {
116         if (count_of_newacks >=2 && transport != primary)
117                 return 1;
118         return 0;
119 }
120
121 /*
122  * SFR-CACC algorithm:
123  * F) If count_of_newacks is less than 2, let d be the
124  * destination to which t was sent. If cacc_saw_newack
125  * is 0 for destination d, then the sender MUST NOT
126  * increment missing report count for t.
127  */
128 static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
129                                        int count_of_newacks)
130 {
131         if (count_of_newacks < 2 && !transport->cacc.cacc_saw_newack)
132                 return 1;
133         return 0;
134 }
135
136 /*
137  * SFR-CACC algorithm:
138  * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
139  * execute steps C, D, F.
140  *
141  * C has been implemented in sctp_outq_sack
142  */
143 static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
144                                      struct sctp_transport *transport,
145                                      int count_of_newacks)
146 {
147         if (!primary->cacc.cycling_changeover) {
148                 if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
149                         return 1;
150                 if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
151                         return 1;
152                 return 0;
153         }
154         return 0;
155 }
156
157 /*
158  * SFR-CACC algorithm:
159  * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
160  * than next_tsn_at_change of the current primary, then
161  * the sender MUST NOT increment missing report count
162  * for t.
163  */
164 static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
165 {
166         if (primary->cacc.cycling_changeover &&
167             TSN_lt(tsn, primary->cacc.next_tsn_at_change))
168                 return 1;
169         return 0;
170 }
171
172 /*
173  * SFR-CACC algorithm:
174  * 3) If the missing report count for TSN t is to be
175  * incremented according to [RFC2960] and
176  * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
177  * then the sender MUST futher execute steps 3.1 and
178  * 3.2 to determine if the missing report count for
179  * TSN t SHOULD NOT be incremented.
180  *
181  * 3.3) If 3.1 and 3.2 do not dictate that the missing
182  * report count for t should not be incremented, then
183  * the sender SOULD increment missing report count for
184  * t (according to [RFC2960] and [SCTP_STEWART_2002]).
185  */
186 static inline int sctp_cacc_skip(struct sctp_transport *primary,
187                                  struct sctp_transport *transport,
188                                  int count_of_newacks,
189                                  __u32 tsn)
190 {
191         if (primary->cacc.changeover_active &&
192             (sctp_cacc_skip_3_1(primary, transport, count_of_newacks)
193              || sctp_cacc_skip_3_2(primary, tsn)))
194                 return 1;
195         return 0;
196 }
197
198 /* Initialize an existing sctp_outq.  This does the boring stuff.
199  * You still need to define handlers if you really want to DO
200  * something with this structure...
201  */
202 void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
203 {
204         q->asoc = asoc;
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);
210
211         q->fast_rtx = 0;
212         q->outstanding_bytes = 0;
213         q->empty = 1;
214         q->cork  = 0;
215
216         q->malloced = 0;
217         q->out_qlen = 0;
218 }
219
220 /* Free the outqueue structure and any related pending chunks.
221  */
222 void sctp_outq_teardown(struct sctp_outq *q)
223 {
224         struct sctp_transport *transport;
225         struct list_head *lchunk, *temp;
226         struct sctp_chunk *chunk, *tmp;
227
228         /* Throw away unacknowledged chunks. */
229         list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
230                         transports) {
231                 while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
232                         chunk = list_entry(lchunk, struct sctp_chunk,
233                                            transmitted_list);
234                         /* Mark as part of a failed message. */
235                         sctp_chunk_fail(chunk, q->error);
236                         sctp_chunk_free(chunk);
237                 }
238         }
239
240         /* Throw away chunks that have been gap ACKed.  */
241         list_for_each_safe(lchunk, temp, &q->sacked) {
242                 list_del_init(lchunk);
243                 chunk = list_entry(lchunk, struct sctp_chunk,
244                                    transmitted_list);
245                 sctp_chunk_fail(chunk, q->error);
246                 sctp_chunk_free(chunk);
247         }
248
249         /* Throw away any chunks in the retransmit queue. */
250         list_for_each_safe(lchunk, temp, &q->retransmit) {
251                 list_del_init(lchunk);
252                 chunk = list_entry(lchunk, struct sctp_chunk,
253                                    transmitted_list);
254                 sctp_chunk_fail(chunk, q->error);
255                 sctp_chunk_free(chunk);
256         }
257
258         /* Throw away any chunks that are in the abandoned queue. */
259         list_for_each_safe(lchunk, temp, &q->abandoned) {
260                 list_del_init(lchunk);
261                 chunk = list_entry(lchunk, struct sctp_chunk,
262                                    transmitted_list);
263                 sctp_chunk_fail(chunk, q->error);
264                 sctp_chunk_free(chunk);
265         }
266
267         /* Throw away any leftover data chunks. */
268         while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
269
270                 /* Mark as send failure. */
271                 sctp_chunk_fail(chunk, q->error);
272                 sctp_chunk_free(chunk);
273         }
274
275         q->error = 0;
276
277         /* Throw away any leftover control chunks. */
278         list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
279                 list_del_init(&chunk->list);
280                 sctp_chunk_free(chunk);
281         }
282 }
283
284 /* Free the outqueue structure and any related pending chunks.  */
285 void sctp_outq_free(struct sctp_outq *q)
286 {
287         /* Throw away leftover chunks. */
288         sctp_outq_teardown(q);
289
290         /* If we were kmalloc()'d, free the memory.  */
291         if (q->malloced)
292                 kfree(q);
293 }
294
295 /* Put a new chunk in an sctp_outq.  */
296 int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
297 {
298         int error = 0;
299
300         SCTP_DEBUG_PRINTK("sctp_outq_tail(%p, %p[%s])\n",
301                           q, chunk, chunk && chunk->chunk_hdr ?
302                           sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
303                           : "Illegal Chunk");
304
305         /* If it is data, queue it up, otherwise, send it
306          * immediately.
307          */
308         if (SCTP_CID_DATA == chunk->chunk_hdr->type) {
309                 /* Is it OK to queue data chunks?  */
310                 /* From 9. Termination of Association
311                  *
312                  * When either endpoint performs a shutdown, the
313                  * association on each peer will stop accepting new
314                  * data from its user and only deliver data in queue
315                  * at the time of sending or receiving the SHUTDOWN
316                  * chunk.
317                  */
318                 switch (q->asoc->state) {
319                 case SCTP_STATE_EMPTY:
320                 case SCTP_STATE_CLOSED:
321                 case SCTP_STATE_SHUTDOWN_PENDING:
322                 case SCTP_STATE_SHUTDOWN_SENT:
323                 case SCTP_STATE_SHUTDOWN_RECEIVED:
324                 case SCTP_STATE_SHUTDOWN_ACK_SENT:
325                         /* Cannot send after transport endpoint shutdown */
326                         error = -ESHUTDOWN;
327                         break;
328
329                 default:
330                         SCTP_DEBUG_PRINTK("outqueueing (%p, %p[%s])\n",
331                           q, chunk, chunk && chunk->chunk_hdr ?
332                           sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
333                           : "Illegal Chunk");
334
335                         sctp_outq_tail_data(q, chunk);
336                         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
337                                 SCTP_INC_STATS(SCTP_MIB_OUTUNORDERCHUNKS);
338                         else
339                                 SCTP_INC_STATS(SCTP_MIB_OUTORDERCHUNKS);
340                         q->empty = 0;
341                         break;
342                 }
343         } else {
344                 list_add_tail(&chunk->list, &q->control_chunk_list);
345                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
346         }
347
348         if (error < 0)
349                 return error;
350
351         if (!q->cork)
352                 error = sctp_outq_flush(q, 0);
353
354         return error;
355 }
356
357 /* Insert a chunk into the sorted list based on the TSNs.  The retransmit list
358  * and the abandoned list are in ascending order.
359  */
360 static void sctp_insert_list(struct list_head *head, struct list_head *new)
361 {
362         struct list_head *pos;
363         struct sctp_chunk *nchunk, *lchunk;
364         __u32 ntsn, ltsn;
365         int done = 0;
366
367         nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
368         ntsn = ntohl(nchunk->subh.data_hdr->tsn);
369
370         list_for_each(pos, head) {
371                 lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
372                 ltsn = ntohl(lchunk->subh.data_hdr->tsn);
373                 if (TSN_lt(ntsn, ltsn)) {
374                         list_add(new, pos->prev);
375                         done = 1;
376                         break;
377                 }
378         }
379         if (!done)
380                 list_add_tail(new, head);
381 }
382
383 /* Mark all the eligible packets on a transport for retransmission.  */
384 void sctp_retransmit_mark(struct sctp_outq *q,
385                           struct sctp_transport *transport,
386                           __u8 reason)
387 {
388         struct list_head *lchunk, *ltemp;
389         struct sctp_chunk *chunk;
390
391         /* Walk through the specified transmitted queue.  */
392         list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
393                 chunk = list_entry(lchunk, struct sctp_chunk,
394                                    transmitted_list);
395
396                 /* If the chunk is abandoned, move it to abandoned list. */
397                 if (sctp_chunk_abandoned(chunk)) {
398                         list_del_init(lchunk);
399                         sctp_insert_list(&q->abandoned, lchunk);
400
401                         /* If this chunk has not been previousely acked,
402                          * stop considering it 'outstanding'.  Our peer
403                          * will most likely never see it since it will
404                          * not be retransmitted
405                          */
406                         if (!chunk->tsn_gap_acked) {
407                                 chunk->transport->flight_size -=
408                                                 sctp_data_size(chunk);
409                                 q->outstanding_bytes -= sctp_data_size(chunk);
410                                 q->asoc->peer.rwnd += (sctp_data_size(chunk) +
411                                                         sizeof(struct sk_buff));
412                         }
413                         continue;
414                 }
415
416                 /* If we are doing  retransmission due to a timeout or pmtu
417                  * discovery, only the  chunks that are not yet acked should
418                  * be added to the retransmit queue.
419                  */
420                 if ((reason == SCTP_RTXR_FAST_RTX  &&
421                             (chunk->fast_retransmit > 0)) ||
422                     (reason != SCTP_RTXR_FAST_RTX  && !chunk->tsn_gap_acked)) {
423                         /* If this chunk was sent less then 1 rto ago, do not
424                          * retransmit this chunk, but give the peer time
425                          * to acknowlege it.  Do this only when
426                          * retransmitting due to T3 timeout.
427                          */
428                         if (reason == SCTP_RTXR_T3_RTX &&
429                             (jiffies - chunk->sent_at) < transport->last_rto)
430                                 continue;
431
432                         /* RFC 2960 6.2.1 Processing a Received SACK
433                          *
434                          * C) Any time a DATA chunk is marked for
435                          * retransmission (via either T3-rtx timer expiration
436                          * (Section 6.3.3) or via fast retransmit
437                          * (Section 7.2.4)), add the data size of those
438                          * chunks to the rwnd.
439                          */
440                         q->asoc->peer.rwnd += (sctp_data_size(chunk) +
441                                                 sizeof(struct sk_buff));
442                         q->outstanding_bytes -= sctp_data_size(chunk);
443                         transport->flight_size -= sctp_data_size(chunk);
444
445                         /* sctpimpguide-05 Section 2.8.2
446                          * M5) If a T3-rtx timer expires, the
447                          * 'TSN.Missing.Report' of all affected TSNs is set
448                          * to 0.
449                          */
450                         chunk->tsn_missing_report = 0;
451
452                         /* If a chunk that is being used for RTT measurement
453                          * has to be retransmitted, we cannot use this chunk
454                          * anymore for RTT measurements. Reset rto_pending so
455                          * that a new RTT measurement is started when a new
456                          * data chunk is sent.
457                          */
458                         if (chunk->rtt_in_progress) {
459                                 chunk->rtt_in_progress = 0;
460                                 transport->rto_pending = 0;
461                         }
462
463                         /* Move the chunk to the retransmit queue. The chunks
464                          * on the retransmit queue are always kept in order.
465                          */
466                         list_del_init(lchunk);
467                         sctp_insert_list(&q->retransmit, lchunk);
468                 }
469         }
470
471         SCTP_DEBUG_PRINTK("%s: transport: %p, reason: %d, "
472                           "cwnd: %d, ssthresh: %d, flight_size: %d, "
473                           "pba: %d\n", __func__,
474                           transport, reason,
475                           transport->cwnd, transport->ssthresh,
476                           transport->flight_size,
477                           transport->partial_bytes_acked);
478
479 }
480
481 /* Mark all the eligible packets on a transport for retransmission and force
482  * one packet out.
483  */
484 void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
485                      sctp_retransmit_reason_t reason)
486 {
487         int error = 0;
488
489         switch(reason) {
490         case SCTP_RTXR_T3_RTX:
491                 SCTP_INC_STATS(SCTP_MIB_T3_RETRANSMITS);
492                 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
493                 /* Update the retran path if the T3-rtx timer has expired for
494                  * the current retran path.
495                  */
496                 if (transport == transport->asoc->peer.retran_path)
497                         sctp_assoc_update_retran_path(transport->asoc);
498                 transport->asoc->rtx_data_chunks +=
499                         transport->asoc->unack_data;
500                 break;
501         case SCTP_RTXR_FAST_RTX:
502                 SCTP_INC_STATS(SCTP_MIB_FAST_RETRANSMITS);
503                 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
504                 q->fast_rtx = 1;
505                 break;
506         case SCTP_RTXR_PMTUD:
507                 SCTP_INC_STATS(SCTP_MIB_PMTUD_RETRANSMITS);
508                 break;
509         case SCTP_RTXR_T1_RTX:
510                 SCTP_INC_STATS(SCTP_MIB_T1_RETRANSMITS);
511                 transport->asoc->init_retries++;
512                 break;
513         default:
514                 BUG();
515         }
516
517         sctp_retransmit_mark(q, transport, reason);
518
519         /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
520          * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
521          * following the procedures outlined in C1 - C5.
522          */
523         sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
524
525         error = sctp_outq_flush(q, /* rtx_timeout */ 1);
526
527         if (error)
528                 q->asoc->base.sk->sk_err = -error;
529 }
530
531 /*
532  * Transmit DATA chunks on the retransmit queue.  Upon return from
533  * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
534  * need to be transmitted by the caller.
535  * We assume that pkt->transport has already been set.
536  *
537  * The return value is a normal kernel error return value.
538  */
539 static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
540                                int rtx_timeout, int *start_timer)
541 {
542         struct list_head *lqueue;
543         struct list_head *lchunk;
544         struct sctp_transport *transport = pkt->transport;
545         sctp_xmit_t status;
546         struct sctp_chunk *chunk, *chunk1;
547         struct sctp_association *asoc;
548         int fast_rtx;
549         int error = 0;
550         int timer = 0;
551
552         asoc = q->asoc;
553         lqueue = &q->retransmit;
554         fast_rtx = q->fast_rtx;
555
556         /* RFC 2960 6.3.3 Handle T3-rtx Expiration
557          *
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.
567          *
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.]
571          */
572         lchunk = sctp_list_dequeue(lqueue);
573
574         while (lchunk) {
575                 chunk = list_entry(lchunk, struct sctp_chunk,
576                                    transmitted_list);
577
578                 /* Make sure that Gap Acked TSNs are not retransmitted.  A
579                  * simple approach is just to move such TSNs out of the
580                  * way and into a 'transmitted' queue and skip to the
581                  * next chunk.
582                  */
583                 if (chunk->tsn_gap_acked) {
584                         list_add_tail(lchunk, &transport->transmitted);
585                         lchunk = sctp_list_dequeue(lqueue);
586                         continue;
587                 }
588
589                 /* Attempt to append this chunk to the packet. */
590                 status = sctp_packet_append_chunk(pkt, chunk);
591
592                 switch (status) {
593                 case SCTP_XMIT_PMTU_FULL:
594                         /* Send this packet.  */
595                         error = sctp_packet_transmit(pkt);
596
597                         /* If we are retransmitting, we should only
598                          * send a single packet.
599                          */
600                         if (rtx_timeout || fast_rtx) {
601                                 list_add(lchunk, lqueue);
602                                 lchunk = NULL;
603                         }
604
605                         /* Bundle lchunk in the next round.  */
606                         break;
607
608                 case SCTP_XMIT_RWND_FULL:
609                         /* Send this packet. */
610                         error = sctp_packet_transmit(pkt);
611
612                         /* Stop sending DATA as there is no more room
613                          * at the receiver.
614                          */
615                         list_add(lchunk, lqueue);
616                         lchunk = NULL;
617                         break;
618
619                 case SCTP_XMIT_NAGLE_DELAY:
620                         /* Send this packet. */
621                         error = sctp_packet_transmit(pkt);
622
623                         /* Stop sending DATA because of nagle delay. */
624                         list_add(lchunk, lqueue);
625                         lchunk = NULL;
626                         break;
627
628                 default:
629                         /* The append was successful, so add this chunk to
630                          * the transmitted list.
631                          */
632                         list_add_tail(lchunk, &transport->transmitted);
633
634                         /* Mark the chunk as ineligible for fast retransmit
635                          * after it is retransmitted.
636                          */
637                         if (chunk->fast_retransmit > 0)
638                                 chunk->fast_retransmit = -1;
639
640                         /* Force start T3-rtx timer when fast retransmitting
641                          * the earliest outstanding TSN
642                          */
643                         if (!timer && fast_rtx &&
644                             ntohl(chunk->subh.data_hdr->tsn) ==
645                                              asoc->ctsn_ack_point + 1)
646                                 timer = 2;
647
648                         q->empty = 0;
649
650                         /* Retrieve a new chunk to bundle. */
651                         lchunk = sctp_list_dequeue(lqueue);
652                         break;
653                 }
654
655                 /* Set the timer if there were no errors */
656                 if (!error && !timer)
657                         timer = 1;
658
659                 /* If we are here due to a retransmit timeout or a fast
660                  * retransmit and if there are any chunks left in the retransmit
661                  * queue that could not fit in the PMTU sized packet, they need
662                  * to be marked as ineligible for a subsequent fast retransmit.
663                  */
664                 if (rtx_timeout && fast_rtx) {
665                         list_for_each_entry(chunk1, lqueue, transmitted_list) {
666                                 if (chunk1->fast_retransmit > 0)
667                                         chunk1->fast_retransmit = -1;
668                         }
669                 }
670         }
671
672         *start_timer = timer;
673
674         /* Clear fast retransmit hint */
675         if (fast_rtx)
676                 q->fast_rtx = 0;
677
678         return error;
679 }
680
681 /* Cork the outqueue so queued chunks are really queued. */
682 int sctp_outq_uncork(struct sctp_outq *q)
683 {
684         int error = 0;
685         if (q->cork)
686                 q->cork = 0;
687         error = sctp_outq_flush(q, 0);
688         return error;
689 }
690
691 /*
692  * Try to flush an outqueue.
693  *
694  * Description: Send everything in q which we legally can, subject to
695  * congestion limitations.
696  * * Note: This function can be called from multiple contexts so appropriate
697  * locking concerns must be made.  Today we use the sock lock to protect
698  * this function.
699  */
700 int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
701 {
702         struct sctp_packet *packet;
703         struct sctp_packet singleton;
704         struct sctp_association *asoc = q->asoc;
705         __u16 sport = asoc->base.bind_addr.port;
706         __u16 dport = asoc->peer.port;
707         __u32 vtag = asoc->peer.i.init_tag;
708         struct sctp_transport *transport = NULL;
709         struct sctp_transport *new_transport;
710         struct sctp_chunk *chunk, *tmp;
711         sctp_xmit_t status;
712         int error = 0;
713         int start_timer = 0;
714
715         /* These transports have chunks to send. */
716         struct list_head transport_list;
717         struct list_head *ltransport;
718
719         INIT_LIST_HEAD(&transport_list);
720         packet = NULL;
721
722         /*
723          * 6.10 Bundling
724          *   ...
725          *   When bundling control chunks with DATA chunks, an
726          *   endpoint MUST place control chunks first in the outbound
727          *   SCTP packet.  The transmitter MUST transmit DATA chunks
728          *   within a SCTP packet in increasing order of TSN.
729          *   ...
730          */
731
732         list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
733                 list_del_init(&chunk->list);
734
735                 /* Pick the right transport to use. */
736                 new_transport = chunk->transport;
737
738                 if (!new_transport) {
739                         /*
740                          * If we have a prior transport pointer, see if
741                          * the destination address of the chunk
742                          * matches the destination address of the
743                          * current transport.  If not a match, then
744                          * try to look up the transport with a given
745                          * destination address.  We do this because
746                          * after processing ASCONFs, we may have new
747                          * transports created.
748                          */
749                         if (transport &&
750                             sctp_cmp_addr_exact(&chunk->dest,
751                                                 &transport->ipaddr))
752                                         new_transport = transport;
753                         else
754                                 new_transport = sctp_assoc_lookup_paddr(asoc,
755                                                                 &chunk->dest);
756
757                         /* if we still don't have a new transport, then
758                          * use the current active path.
759                          */
760                         if (!new_transport)
761                                 new_transport = asoc->peer.active_path;
762                 } else if ((new_transport->state == SCTP_INACTIVE) ||
763                            (new_transport->state == SCTP_UNCONFIRMED)) {
764                         /* If the chunk is Heartbeat or Heartbeat Ack,
765                          * send it to chunk->transport, even if it's
766                          * inactive.
767                          *
768                          * 3.3.6 Heartbeat Acknowledgement:
769                          * ...
770                          * A HEARTBEAT ACK is always sent to the source IP
771                          * address of the IP datagram containing the
772                          * HEARTBEAT chunk to which this ack is responding.
773                          * ...
774                          *
775                          * ASCONF_ACKs also must be sent to the source.
776                          */
777                         if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
778                             chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK &&
779                             chunk->chunk_hdr->type != SCTP_CID_ASCONF_ACK)
780                                 new_transport = asoc->peer.active_path;
781                 }
782
783                 /* Are we switching transports?
784                  * Take care of transport locks.
785                  */
786                 if (new_transport != transport) {
787                         transport = new_transport;
788                         if (list_empty(&transport->send_ready)) {
789                                 list_add_tail(&transport->send_ready,
790                                               &transport_list);
791                         }
792                         packet = &transport->packet;
793                         sctp_packet_config(packet, vtag,
794                                            asoc->peer.ecn_capable);
795                 }
796
797                 switch (chunk->chunk_hdr->type) {
798                 /*
799                  * 6.10 Bundling
800                  *   ...
801                  *   An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
802                  *   COMPLETE with any other chunks.  [Send them immediately.]
803                  */
804                 case SCTP_CID_INIT:
805                 case SCTP_CID_INIT_ACK:
806                 case SCTP_CID_SHUTDOWN_COMPLETE:
807                         sctp_packet_init(&singleton, transport, sport, dport);
808                         sctp_packet_config(&singleton, vtag, 0);
809                         sctp_packet_append_chunk(&singleton, chunk);
810                         error = sctp_packet_transmit(&singleton);
811                         if (error < 0)
812                                 return error;
813                         break;
814
815                 case SCTP_CID_ABORT:
816                         if (sctp_test_T_bit(chunk)) {
817                                 packet->vtag = asoc->c.my_vtag;
818                         }
819                 case SCTP_CID_SACK:
820                 case SCTP_CID_HEARTBEAT:
821                 case SCTP_CID_HEARTBEAT_ACK:
822                 case SCTP_CID_SHUTDOWN:
823                 case SCTP_CID_SHUTDOWN_ACK:
824                 case SCTP_CID_ERROR:
825                 case SCTP_CID_COOKIE_ECHO:
826                 case SCTP_CID_COOKIE_ACK:
827                 case SCTP_CID_ECN_ECNE:
828                 case SCTP_CID_ECN_CWR:
829                 case SCTP_CID_ASCONF:
830                 case SCTP_CID_ASCONF_ACK:
831                 case SCTP_CID_FWD_TSN:
832                         sctp_packet_transmit_chunk(packet, chunk);
833                         break;
834
835                 default:
836                         /* We built a chunk with an illegal type! */
837                         BUG();
838                 }
839         }
840
841         /* Is it OK to send data chunks?  */
842         switch (asoc->state) {
843         case SCTP_STATE_COOKIE_ECHOED:
844                 /* Only allow bundling when this packet has a COOKIE-ECHO
845                  * chunk.
846                  */
847                 if (!packet || !packet->has_cookie_echo)
848                         break;
849
850                 /* fallthru */
851         case SCTP_STATE_ESTABLISHED:
852         case SCTP_STATE_SHUTDOWN_PENDING:
853         case SCTP_STATE_SHUTDOWN_RECEIVED:
854                 /*
855                  * RFC 2960 6.1  Transmission of DATA Chunks
856                  *
857                  * C) When the time comes for the sender to transmit,
858                  * before sending new DATA chunks, the sender MUST
859                  * first transmit any outstanding DATA chunks which
860                  * are marked for retransmission (limited by the
861                  * current cwnd).
862                  */
863                 if (!list_empty(&q->retransmit)) {
864                         if (transport == asoc->peer.retran_path)
865                                 goto retran;
866
867                         /* Switch transports & prepare the packet.  */
868
869                         transport = asoc->peer.retran_path;
870
871                         if (list_empty(&transport->send_ready)) {
872                                 list_add_tail(&transport->send_ready,
873                                               &transport_list);
874                         }
875
876                         packet = &transport->packet;
877                         sctp_packet_config(packet, vtag,
878                                            asoc->peer.ecn_capable);
879                 retran:
880                         error = sctp_outq_flush_rtx(q, packet,
881                                                     rtx_timeout, &start_timer);
882
883                         if (start_timer)
884                                 sctp_transport_reset_timers(transport,
885                                                             start_timer-1);
886
887                         /* This can happen on COOKIE-ECHO resend.  Only
888                          * one chunk can get bundled with a COOKIE-ECHO.
889                          */
890                         if (packet->has_cookie_echo)
891                                 goto sctp_flush_out;
892
893                         /* Don't send new data if there is still data
894                          * waiting to retransmit.
895                          */
896                         if (!list_empty(&q->retransmit))
897                                 goto sctp_flush_out;
898                 }
899
900                 /* Finally, transmit new packets.  */
901                 start_timer = 0;
902                 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
903                         /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
904                          * stream identifier.
905                          */
906                         if (chunk->sinfo.sinfo_stream >=
907                             asoc->c.sinit_num_ostreams) {
908
909                                 /* Mark as failed send. */
910                                 sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
911                                 sctp_chunk_free(chunk);
912                                 continue;
913                         }
914
915                         /* Has this chunk expired? */
916                         if (sctp_chunk_abandoned(chunk)) {
917                                 sctp_chunk_fail(chunk, 0);
918                                 sctp_chunk_free(chunk);
919                                 continue;
920                         }
921
922                         /* If there is a specified transport, use it.
923                          * Otherwise, we want to use the active path.
924                          */
925                         new_transport = chunk->transport;
926                         if (!new_transport ||
927                             ((new_transport->state == SCTP_INACTIVE) ||
928                              (new_transport->state == SCTP_UNCONFIRMED)))
929                                 new_transport = asoc->peer.active_path;
930
931                         /* Change packets if necessary.  */
932                         if (new_transport != transport) {
933                                 transport = new_transport;
934
935                                 /* Schedule to have this transport's
936                                  * packet flushed.
937                                  */
938                                 if (list_empty(&transport->send_ready)) {
939                                         list_add_tail(&transport->send_ready,
940                                                       &transport_list);
941                                 }
942
943                                 packet = &transport->packet;
944                                 sctp_packet_config(packet, vtag,
945                                                    asoc->peer.ecn_capable);
946                         }
947
948                         SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
949                                           q, chunk,
950                                           chunk && chunk->chunk_hdr ?
951                                           sctp_cname(SCTP_ST_CHUNK(
952                                                   chunk->chunk_hdr->type))
953                                           : "Illegal Chunk");
954
955                         SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
956                                         "%p skb->users %d.\n",
957                                         ntohl(chunk->subh.data_hdr->tsn),
958                                         chunk->skb ?chunk->skb->head : NULL,
959                                         chunk->skb ?
960                                         atomic_read(&chunk->skb->users) : -1);
961
962                         /* Add the chunk to the packet.  */
963                         status = sctp_packet_transmit_chunk(packet, chunk);
964
965                         switch (status) {
966                         case SCTP_XMIT_PMTU_FULL:
967                         case SCTP_XMIT_RWND_FULL:
968                         case SCTP_XMIT_NAGLE_DELAY:
969                                 /* We could not append this chunk, so put
970                                  * the chunk back on the output queue.
971                                  */
972                                 SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
973                                         "not transmit TSN: 0x%x, status: %d\n",
974                                         ntohl(chunk->subh.data_hdr->tsn),
975                                         status);
976                                 sctp_outq_head_data(q, chunk);
977                                 goto sctp_flush_out;
978                                 break;
979
980                         case SCTP_XMIT_OK:
981                                 break;
982
983                         default:
984                                 BUG();
985                         }
986
987                         /* BUG: We assume that the sctp_packet_transmit()
988                          * call below will succeed all the time and add the
989                          * chunk to the transmitted list and restart the
990                          * timers.
991                          * It is possible that the call can fail under OOM
992                          * conditions.
993                          *
994                          * Is this really a problem?  Won't this behave
995                          * like a lost TSN?
996                          */
997                         list_add_tail(&chunk->transmitted_list,
998                                       &transport->transmitted);
999
1000                         sctp_transport_reset_timers(transport, start_timer-1);
1001
1002                         q->empty = 0;
1003
1004                         /* Only let one DATA chunk get bundled with a
1005                          * COOKIE-ECHO chunk.
1006                          */
1007                         if (packet->has_cookie_echo)
1008                                 goto sctp_flush_out;
1009                 }
1010                 break;
1011
1012         default:
1013                 /* Do nothing.  */
1014                 break;
1015         }
1016
1017 sctp_flush_out:
1018
1019         /* Before returning, examine all the transports touched in
1020          * this call.  Right now, we bluntly force clear all the
1021          * transports.  Things might change after we implement Nagle.
1022          * But such an examination is still required.
1023          *
1024          * --xguo
1025          */
1026         while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL ) {
1027                 struct sctp_transport *t = list_entry(ltransport,
1028                                                       struct sctp_transport,
1029                                                       send_ready);
1030                 packet = &t->packet;
1031                 if (!sctp_packet_empty(packet))
1032                         error = sctp_packet_transmit(packet);
1033         }
1034
1035         return error;
1036 }
1037
1038 /* Update unack_data based on the incoming SACK chunk */
1039 static void sctp_sack_update_unack_data(struct sctp_association *assoc,
1040                                         struct sctp_sackhdr *sack)
1041 {
1042         sctp_sack_variable_t *frags;
1043         __u16 unack_data;
1044         int i;
1045
1046         unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
1047
1048         frags = sack->variable;
1049         for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
1050                 unack_data -= ((ntohs(frags[i].gab.end) -
1051                                 ntohs(frags[i].gab.start) + 1));
1052         }
1053
1054         assoc->unack_data = unack_data;
1055 }
1056
1057 /* Return the highest new tsn that is acknowledged by the given SACK chunk. */
1058 static __u32 sctp_highest_new_tsn(struct sctp_sackhdr *sack,
1059                                   struct sctp_association *asoc)
1060 {
1061         struct sctp_transport *transport;
1062         struct sctp_chunk *chunk;
1063         __u32 highest_new_tsn, tsn;
1064         struct list_head *transport_list = &asoc->peer.transport_addr_list;
1065
1066         highest_new_tsn = ntohl(sack->cum_tsn_ack);
1067
1068         list_for_each_entry(transport, transport_list, transports) {
1069                 list_for_each_entry(chunk, &transport->transmitted,
1070                                 transmitted_list) {
1071                         tsn = ntohl(chunk->subh.data_hdr->tsn);
1072
1073                         if (!chunk->tsn_gap_acked &&
1074                             TSN_lt(highest_new_tsn, tsn) &&
1075                             sctp_acked(sack, tsn))
1076                                 highest_new_tsn = tsn;
1077                 }
1078         }
1079
1080         return highest_new_tsn;
1081 }
1082
1083 /* This is where we REALLY process a SACK.
1084  *
1085  * Process the SACK against the outqueue.  Mostly, this just frees
1086  * things off the transmitted queue.
1087  */
1088 int sctp_outq_sack(struct sctp_outq *q, struct sctp_sackhdr *sack)
1089 {
1090         struct sctp_association *asoc = q->asoc;
1091         struct sctp_transport *transport;
1092         struct sctp_chunk *tchunk = NULL;
1093         struct list_head *lchunk, *transport_list, *temp;
1094         sctp_sack_variable_t *frags = sack->variable;
1095         __u32 sack_ctsn, ctsn, tsn;
1096         __u32 highest_tsn, highest_new_tsn;
1097         __u32 sack_a_rwnd;
1098         unsigned outstanding;
1099         struct sctp_transport *primary = asoc->peer.primary_path;
1100         int count_of_newacks = 0;
1101
1102         /* Grab the association's destination address list. */
1103         transport_list = &asoc->peer.transport_addr_list;
1104
1105         sack_ctsn = ntohl(sack->cum_tsn_ack);
1106
1107         /*
1108          * SFR-CACC algorithm:
1109          * On receipt of a SACK the sender SHOULD execute the
1110          * following statements.
1111          *
1112          * 1) If the cumulative ack in the SACK passes next tsn_at_change
1113          * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1114          * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1115          * all destinations.
1116          */
1117         if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1118                 primary->cacc.changeover_active = 0;
1119                 list_for_each_entry(transport, transport_list,
1120                                 transports) {
1121                         transport->cacc.cycling_changeover = 0;
1122                 }
1123         }
1124
1125         /*
1126          * SFR-CACC algorithm:
1127          * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1128          * is set the receiver of the SACK MUST take the following actions:
1129          *
1130          * A) Initialize the cacc_saw_newack to 0 for all destination
1131          * addresses.
1132          */
1133         if (sack->num_gap_ack_blocks &&
1134             primary->cacc.changeover_active) {
1135                 list_for_each_entry(transport, transport_list, transports) {
1136                         transport->cacc.cacc_saw_newack = 0;
1137                 }
1138         }
1139
1140         /* Get the highest TSN in the sack. */
1141         highest_tsn = sack_ctsn;
1142         if (sack->num_gap_ack_blocks)
1143                 highest_tsn +=
1144                     ntohs(frags[ntohs(sack->num_gap_ack_blocks) - 1].gab.end);
1145
1146         if (TSN_lt(asoc->highest_sacked, highest_tsn)) {
1147                 highest_new_tsn = highest_tsn;
1148                 asoc->highest_sacked = highest_tsn;
1149         } else {
1150                 highest_new_tsn = sctp_highest_new_tsn(sack, asoc);
1151         }
1152
1153         /* Run through the retransmit queue.  Credit bytes received
1154          * and free those chunks that we can.
1155          */
1156         sctp_check_transmitted(q, &q->retransmit, NULL, sack, highest_new_tsn);
1157         sctp_mark_missing(q, &q->retransmit, NULL, highest_new_tsn, 0);
1158
1159         /* Run through the transmitted queue.
1160          * Credit bytes received and free those chunks which we can.
1161          *
1162          * This is a MASSIVE candidate for optimization.
1163          */
1164         list_for_each_entry(transport, transport_list, transports) {
1165                 sctp_check_transmitted(q, &transport->transmitted,
1166                                        transport, sack, highest_new_tsn);
1167                 /*
1168                  * SFR-CACC algorithm:
1169                  * C) Let count_of_newacks be the number of
1170                  * destinations for which cacc_saw_newack is set.
1171                  */
1172                 if (transport->cacc.cacc_saw_newack)
1173                         count_of_newacks ++;
1174         }
1175
1176         list_for_each_entry(transport, transport_list, transports) {
1177                 sctp_mark_missing(q, &transport->transmitted, transport,
1178                                   highest_new_tsn, count_of_newacks);
1179         }
1180
1181         /* Move the Cumulative TSN Ack Point if appropriate.  */
1182         if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn))
1183                 asoc->ctsn_ack_point = sack_ctsn;
1184
1185         /* Update unack_data field in the assoc. */
1186         sctp_sack_update_unack_data(asoc, sack);
1187
1188         ctsn = asoc->ctsn_ack_point;
1189
1190         /* Throw away stuff rotting on the sack queue.  */
1191         list_for_each_safe(lchunk, temp, &q->sacked) {
1192                 tchunk = list_entry(lchunk, struct sctp_chunk,
1193                                     transmitted_list);
1194                 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1195                 if (TSN_lte(tsn, ctsn)) {
1196                         list_del_init(&tchunk->transmitted_list);
1197                         sctp_chunk_free(tchunk);
1198                 }
1199         }
1200
1201         /* ii) Set rwnd equal to the newly received a_rwnd minus the
1202          *     number of bytes still outstanding after processing the
1203          *     Cumulative TSN Ack and the Gap Ack Blocks.
1204          */
1205
1206         sack_a_rwnd = ntohl(sack->a_rwnd);
1207         outstanding = q->outstanding_bytes;
1208
1209         if (outstanding < sack_a_rwnd)
1210                 sack_a_rwnd -= outstanding;
1211         else
1212                 sack_a_rwnd = 0;
1213
1214         asoc->peer.rwnd = sack_a_rwnd;
1215
1216         sctp_generate_fwdtsn(q, sack_ctsn);
1217
1218         SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.\n",
1219                           __func__, sack_ctsn);
1220         SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
1221                           "%p is 0x%x. Adv peer ack point: 0x%x\n",
1222                           __func__, asoc, ctsn, asoc->adv_peer_ack_point);
1223
1224         /* See if all chunks are acked.
1225          * Make sure the empty queue handler will get run later.
1226          */
1227         q->empty = (list_empty(&q->out_chunk_list) &&
1228                     list_empty(&q->control_chunk_list) &&
1229                     list_empty(&q->retransmit));
1230         if (!q->empty)
1231                 goto finish;
1232
1233         list_for_each_entry(transport, transport_list, transports) {
1234                 q->empty = q->empty && list_empty(&transport->transmitted);
1235                 if (!q->empty)
1236                         goto finish;
1237         }
1238
1239         SCTP_DEBUG_PRINTK("sack queue is empty.\n");
1240 finish:
1241         return q->empty;
1242 }
1243
1244 /* Is the outqueue empty?  */
1245 int sctp_outq_is_empty(const struct sctp_outq *q)
1246 {
1247         return q->empty;
1248 }
1249
1250 /********************************************************************
1251  * 2nd Level Abstractions
1252  ********************************************************************/
1253
1254 /* Go through a transport's transmitted list or the association's retransmit
1255  * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1256  * The retransmit list will not have an associated transport.
1257  *
1258  * I added coherent debug information output.   --xguo
1259  *
1260  * Instead of printing 'sacked' or 'kept' for each TSN on the
1261  * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1262  * KEPT TSN6-TSN7, etc.
1263  */
1264 static void sctp_check_transmitted(struct sctp_outq *q,
1265                                    struct list_head *transmitted_queue,
1266                                    struct sctp_transport *transport,
1267                                    struct sctp_sackhdr *sack,
1268                                    __u32 highest_new_tsn_in_sack)
1269 {
1270         struct list_head *lchunk;
1271         struct sctp_chunk *tchunk;
1272         struct list_head tlist;
1273         __u32 tsn;
1274         __u32 sack_ctsn;
1275         __u32 rtt;
1276         __u8 restart_timer = 0;
1277         int bytes_acked = 0;
1278
1279         /* These state variables are for coherent debug output. --xguo */
1280
1281 #if SCTP_DEBUG
1282         __u32 dbg_ack_tsn = 0;  /* An ACKed TSN range starts here... */
1283         __u32 dbg_last_ack_tsn = 0;  /* ...and finishes here.        */
1284         __u32 dbg_kept_tsn = 0; /* An un-ACKed range starts here...  */
1285         __u32 dbg_last_kept_tsn = 0; /* ...and finishes here.        */
1286
1287         /* 0 : The last TSN was ACKed.
1288          * 1 : The last TSN was NOT ACKed (i.e. KEPT).
1289          * -1: We need to initialize.
1290          */
1291         int dbg_prt_state = -1;
1292 #endif /* SCTP_DEBUG */
1293
1294         sack_ctsn = ntohl(sack->cum_tsn_ack);
1295
1296         INIT_LIST_HEAD(&tlist);
1297
1298         /* The while loop will skip empty transmitted queues. */
1299         while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1300                 tchunk = list_entry(lchunk, struct sctp_chunk,
1301                                     transmitted_list);
1302
1303                 if (sctp_chunk_abandoned(tchunk)) {
1304                         /* Move the chunk to abandoned list. */
1305                         sctp_insert_list(&q->abandoned, lchunk);
1306
1307                         /* If this chunk has not been acked, stop
1308                          * considering it as 'outstanding'.
1309                          */
1310                         if (!tchunk->tsn_gap_acked) {
1311                                 tchunk->transport->flight_size -=
1312                                                 sctp_data_size(tchunk);
1313                                 q->outstanding_bytes -= sctp_data_size(tchunk);
1314                         }
1315                         continue;
1316                 }
1317
1318                 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1319                 if (sctp_acked(sack, tsn)) {
1320                         /* If this queue is the retransmit queue, the
1321                          * retransmit timer has already reclaimed
1322                          * the outstanding bytes for this chunk, so only
1323                          * count bytes associated with a transport.
1324                          */
1325                         if (transport) {
1326                                 /* If this chunk is being used for RTT
1327                                  * measurement, calculate the RTT and update
1328                                  * the RTO using this value.
1329                                  *
1330                                  * 6.3.1 C5) Karn's algorithm: RTT measurements
1331                                  * MUST NOT be made using packets that were
1332                                  * retransmitted (and thus for which it is
1333                                  * ambiguous whether the reply was for the
1334                                  * first instance of the packet or a later
1335                                  * instance).
1336                                  */
1337                                 if (!tchunk->tsn_gap_acked &&
1338                                     !tchunk->resent &&
1339                                     tchunk->rtt_in_progress) {
1340                                         tchunk->rtt_in_progress = 0;
1341                                         rtt = jiffies - tchunk->sent_at;
1342                                         sctp_transport_update_rto(transport,
1343                                                                   rtt);
1344                                 }
1345                         }
1346                         if (TSN_lte(tsn, sack_ctsn)) {
1347                                 /* RFC 2960  6.3.2 Retransmission Timer Rules
1348                                  *
1349                                  * R3) Whenever a SACK is received
1350                                  * that acknowledges the DATA chunk
1351                                  * with the earliest outstanding TSN
1352                                  * for that address, restart T3-rtx
1353                                  * timer for that address with its
1354                                  * current RTO.
1355                                  */
1356                                 restart_timer = 1;
1357
1358                                 if (!tchunk->tsn_gap_acked) {
1359                                         tchunk->tsn_gap_acked = 1;
1360                                         bytes_acked += sctp_data_size(tchunk);
1361                                         /*
1362                                          * SFR-CACC algorithm:
1363                                          * 2) If the SACK contains gap acks
1364                                          * and the flag CHANGEOVER_ACTIVE is
1365                                          * set the receiver of the SACK MUST
1366                                          * take the following action:
1367                                          *
1368                                          * B) For each TSN t being acked that
1369                                          * has not been acked in any SACK so
1370                                          * far, set cacc_saw_newack to 1 for
1371                                          * the destination that the TSN was
1372                                          * sent to.
1373                                          */
1374                                         if (transport &&
1375                                             sack->num_gap_ack_blocks &&
1376                                             q->asoc->peer.primary_path->cacc.
1377                                             changeover_active)
1378                                                 transport->cacc.cacc_saw_newack
1379                                                         = 1;
1380                                 }
1381
1382                                 list_add_tail(&tchunk->transmitted_list,
1383                                               &q->sacked);
1384                         } else {
1385                                 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1386                                  * M2) Each time a SACK arrives reporting
1387                                  * 'Stray DATA chunk(s)' record the highest TSN
1388                                  * reported as newly acknowledged, call this
1389                                  * value 'HighestTSNinSack'. A newly
1390                                  * acknowledged DATA chunk is one not
1391                                  * previously acknowledged in a SACK.
1392                                  *
1393                                  * When the SCTP sender of data receives a SACK
1394                                  * chunk that acknowledges, for the first time,
1395                                  * the receipt of a DATA chunk, all the still
1396                                  * unacknowledged DATA chunks whose TSN is
1397                                  * older than that newly acknowledged DATA
1398                                  * chunk, are qualified as 'Stray DATA chunks'.
1399                                  */
1400                                 if (!tchunk->tsn_gap_acked) {
1401                                         tchunk->tsn_gap_acked = 1;
1402                                         bytes_acked += sctp_data_size(tchunk);
1403                                 }
1404                                 list_add_tail(lchunk, &tlist);
1405                         }
1406
1407 #if SCTP_DEBUG
1408                         switch (dbg_prt_state) {
1409                         case 0: /* last TSN was ACKed */
1410                                 if (dbg_last_ack_tsn + 1 == tsn) {
1411                                         /* This TSN belongs to the
1412                                          * current ACK range.
1413                                          */
1414                                         break;
1415                                 }
1416
1417                                 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1418                                         /* Display the end of the
1419                                          * current range.
1420                                          */
1421                                         SCTP_DEBUG_PRINTK("-%08x",
1422                                                           dbg_last_ack_tsn);
1423                                 }
1424
1425                                 /* Start a new range.  */
1426                                 SCTP_DEBUG_PRINTK(",%08x", tsn);
1427                                 dbg_ack_tsn = tsn;
1428                                 break;
1429
1430                         case 1: /* The last TSN was NOT ACKed. */
1431                                 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1432                                         /* Display the end of current range. */
1433                                         SCTP_DEBUG_PRINTK("-%08x",
1434                                                           dbg_last_kept_tsn);
1435                                 }
1436
1437                                 SCTP_DEBUG_PRINTK("\n");
1438
1439                                 /* FALL THROUGH... */
1440                         default:
1441                                 /* This is the first-ever TSN we examined.  */
1442                                 /* Start a new range of ACK-ed TSNs.  */
1443                                 SCTP_DEBUG_PRINTK("ACKed: %08x", tsn);
1444                                 dbg_prt_state = 0;
1445                                 dbg_ack_tsn = tsn;
1446                         }
1447
1448                         dbg_last_ack_tsn = tsn;
1449 #endif /* SCTP_DEBUG */
1450
1451                 } else {
1452                         if (tchunk->tsn_gap_acked) {
1453                                 SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
1454                                                   "data TSN: 0x%x\n",
1455                                                   __func__,
1456                                                   tsn);
1457                                 tchunk->tsn_gap_acked = 0;
1458
1459                                 bytes_acked -= sctp_data_size(tchunk);
1460
1461                                 /* RFC 2960 6.3.2 Retransmission Timer Rules
1462                                  *
1463                                  * R4) Whenever a SACK is received missing a
1464                                  * TSN that was previously acknowledged via a
1465                                  * Gap Ack Block, start T3-rtx for the
1466                                  * destination address to which the DATA
1467                                  * chunk was originally
1468                                  * transmitted if it is not already running.
1469                                  */
1470                                 restart_timer = 1;
1471                         }
1472
1473                         list_add_tail(lchunk, &tlist);
1474
1475 #if SCTP_DEBUG
1476                         /* See the above comments on ACK-ed TSNs. */
1477                         switch (dbg_prt_state) {
1478                         case 1:
1479                                 if (dbg_last_kept_tsn + 1 == tsn)
1480                                         break;
1481
1482                                 if (dbg_last_kept_tsn != dbg_kept_tsn)
1483                                         SCTP_DEBUG_PRINTK("-%08x",
1484                                                           dbg_last_kept_tsn);
1485
1486                                 SCTP_DEBUG_PRINTK(",%08x", tsn);
1487                                 dbg_kept_tsn = tsn;
1488                                 break;
1489
1490                         case 0:
1491                                 if (dbg_last_ack_tsn != dbg_ack_tsn)
1492                                         SCTP_DEBUG_PRINTK("-%08x",
1493                                                           dbg_last_ack_tsn);
1494                                 SCTP_DEBUG_PRINTK("\n");
1495
1496                                 /* FALL THROUGH... */
1497                         default:
1498                                 SCTP_DEBUG_PRINTK("KEPT: %08x",tsn);
1499                                 dbg_prt_state = 1;
1500                                 dbg_kept_tsn = tsn;
1501                         }
1502
1503                         dbg_last_kept_tsn = tsn;
1504 #endif /* SCTP_DEBUG */
1505                 }
1506         }
1507
1508 #if SCTP_DEBUG
1509         /* Finish off the last range, displaying its ending TSN.  */
1510         switch (dbg_prt_state) {
1511         case 0:
1512                 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1513                         SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_ack_tsn);
1514                 } else {
1515                         SCTP_DEBUG_PRINTK("\n");
1516                 }
1517         break;
1518
1519         case 1:
1520                 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1521                         SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_kept_tsn);
1522                 } else {
1523                         SCTP_DEBUG_PRINTK("\n");
1524                 }
1525         }
1526 #endif /* SCTP_DEBUG */
1527         if (transport) {
1528                 if (bytes_acked) {
1529                         /* 8.2. When an outstanding TSN is acknowledged,
1530                          * the endpoint shall clear the error counter of
1531                          * the destination transport address to which the
1532                          * DATA chunk was last sent.
1533                          * The association's overall error counter is
1534                          * also cleared.
1535                          */
1536                         transport->error_count = 0;
1537                         transport->asoc->overall_error_count = 0;
1538
1539                         /* Mark the destination transport address as
1540                          * active if it is not so marked.
1541                          */
1542                         if ((transport->state == SCTP_INACTIVE) ||
1543                             (transport->state == SCTP_UNCONFIRMED)) {
1544                                 sctp_assoc_control_transport(
1545                                         transport->asoc,
1546                                         transport,
1547                                         SCTP_TRANSPORT_UP,
1548                                         SCTP_RECEIVED_SACK);
1549                         }
1550
1551                         sctp_transport_raise_cwnd(transport, sack_ctsn,
1552                                                   bytes_acked);
1553
1554                         transport->flight_size -= bytes_acked;
1555                         if (transport->flight_size == 0)
1556                                 transport->partial_bytes_acked = 0;
1557                         q->outstanding_bytes -= bytes_acked;
1558                 } else {
1559                         /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1560                          * When a sender is doing zero window probing, it
1561                          * should not timeout the association if it continues
1562                          * to receive new packets from the receiver. The
1563                          * reason is that the receiver MAY keep its window
1564                          * closed for an indefinite time.
1565                          * A sender is doing zero window probing when the
1566                          * receiver's advertised window is zero, and there is
1567                          * only one data chunk in flight to the receiver.
1568                          */
1569                         if (!q->asoc->peer.rwnd &&
1570                             !list_empty(&tlist) &&
1571                             (sack_ctsn+2 == q->asoc->next_tsn)) {
1572                                 SCTP_DEBUG_PRINTK("%s: SACK received for zero "
1573                                                   "window probe: %u\n",
1574                                                   __func__, sack_ctsn);
1575                                 q->asoc->overall_error_count = 0;
1576                                 transport->error_count = 0;
1577                         }
1578                 }
1579
1580                 /* RFC 2960 6.3.2 Retransmission Timer Rules
1581                  *
1582                  * R2) Whenever all outstanding data sent to an address have
1583                  * been acknowledged, turn off the T3-rtx timer of that
1584                  * address.
1585                  */
1586                 if (!transport->flight_size) {
1587                         if (timer_pending(&transport->T3_rtx_timer) &&
1588                             del_timer(&transport->T3_rtx_timer)) {
1589                                 sctp_transport_put(transport);
1590                         }
1591                 } else if (restart_timer) {
1592                         if (!mod_timer(&transport->T3_rtx_timer,
1593                                        jiffies + transport->rto))
1594                                 sctp_transport_hold(transport);
1595                 }
1596         }
1597
1598         list_splice(&tlist, transmitted_queue);
1599 }
1600
1601 /* Mark chunks as missing and consequently may get retransmitted. */
1602 static void sctp_mark_missing(struct sctp_outq *q,
1603                               struct list_head *transmitted_queue,
1604                               struct sctp_transport *transport,
1605                               __u32 highest_new_tsn_in_sack,
1606                               int count_of_newacks)
1607 {
1608         struct sctp_chunk *chunk;
1609         __u32 tsn;
1610         char do_fast_retransmit = 0;
1611         struct sctp_transport *primary = q->asoc->peer.primary_path;
1612
1613         list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
1614
1615                 tsn = ntohl(chunk->subh.data_hdr->tsn);
1616
1617                 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1618                  * 'Unacknowledged TSN's', if the TSN number of an
1619                  * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1620                  * value, increment the 'TSN.Missing.Report' count on that
1621                  * chunk if it has NOT been fast retransmitted or marked for
1622                  * fast retransmit already.
1623                  */
1624                 if (!chunk->fast_retransmit &&
1625                     !chunk->tsn_gap_acked &&
1626                     TSN_lt(tsn, highest_new_tsn_in_sack)) {
1627
1628                         /* SFR-CACC may require us to skip marking
1629                          * this chunk as missing.
1630                          */
1631                         if (!transport || !sctp_cacc_skip(primary, transport,
1632                                             count_of_newacks, tsn)) {
1633                                 chunk->tsn_missing_report++;
1634
1635                                 SCTP_DEBUG_PRINTK(
1636                                         "%s: TSN 0x%x missing counter: %d\n",
1637                                         __func__, tsn,
1638                                         chunk->tsn_missing_report);
1639                         }
1640                 }
1641                 /*
1642                  * M4) If any DATA chunk is found to have a
1643                  * 'TSN.Missing.Report'
1644                  * value larger than or equal to 3, mark that chunk for
1645                  * retransmission and start the fast retransmit procedure.
1646                  */
1647
1648                 if (chunk->tsn_missing_report >= 3) {
1649                         chunk->fast_retransmit = 1;
1650                         do_fast_retransmit = 1;
1651                 }
1652         }
1653
1654         if (transport) {
1655                 if (do_fast_retransmit)
1656                         sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1657
1658                 SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
1659                                   "ssthresh: %d, flight_size: %d, pba: %d\n",
1660                                   __func__, transport, transport->cwnd,
1661                                   transport->ssthresh, transport->flight_size,
1662                                   transport->partial_bytes_acked);
1663         }
1664 }
1665
1666 /* Is the given TSN acked by this packet?  */
1667 static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1668 {
1669         int i;
1670         sctp_sack_variable_t *frags;
1671         __u16 gap;
1672         __u32 ctsn = ntohl(sack->cum_tsn_ack);
1673
1674         if (TSN_lte(tsn, ctsn))
1675                 goto pass;
1676
1677         /* 3.3.4 Selective Acknowledgement (SACK) (3):
1678          *
1679          * Gap Ack Blocks:
1680          *  These fields contain the Gap Ack Blocks. They are repeated
1681          *  for each Gap Ack Block up to the number of Gap Ack Blocks
1682          *  defined in the Number of Gap Ack Blocks field. All DATA
1683          *  chunks with TSNs greater than or equal to (Cumulative TSN
1684          *  Ack + Gap Ack Block Start) and less than or equal to
1685          *  (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1686          *  Block are assumed to have been received correctly.
1687          */
1688
1689         frags = sack->variable;
1690         gap = tsn - ctsn;
1691         for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
1692                 if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
1693                     TSN_lte(gap, ntohs(frags[i].gab.end)))
1694                         goto pass;
1695         }
1696
1697         return 0;
1698 pass:
1699         return 1;
1700 }
1701
1702 static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
1703                                     int nskips, __be16 stream)
1704 {
1705         int i;
1706
1707         for (i = 0; i < nskips; i++) {
1708                 if (skiplist[i].stream == stream)
1709                         return i;
1710         }
1711         return i;
1712 }
1713
1714 /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1715 static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
1716 {
1717         struct sctp_association *asoc = q->asoc;
1718         struct sctp_chunk *ftsn_chunk = NULL;
1719         struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1720         int nskips = 0;
1721         int skip_pos = 0;
1722         __u32 tsn;
1723         struct sctp_chunk *chunk;
1724         struct list_head *lchunk, *temp;
1725
1726         /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1727          * received SACK.
1728          *
1729          * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1730          * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1731          */
1732         if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1733                 asoc->adv_peer_ack_point = ctsn;
1734
1735         /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1736          * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1737          * the chunk next in the out-queue space is marked as "abandoned" as
1738          * shown in the following example:
1739          *
1740          * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1741          * and the Advanced.Peer.Ack.Point is updated to this value:
1742          *
1743          *   out-queue at the end of  ==>   out-queue after Adv.Ack.Point
1744          *   normal SACK processing           local advancement
1745          *                ...                           ...
1746          *   Adv.Ack.Pt-> 102 acked                     102 acked
1747          *                103 abandoned                 103 abandoned
1748          *                104 abandoned     Adv.Ack.P-> 104 abandoned
1749          *                105                           105
1750          *                106 acked                     106 acked
1751          *                ...                           ...
1752          *
1753          * In this example, the data sender successfully advanced the
1754          * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1755          */
1756         list_for_each_safe(lchunk, temp, &q->abandoned) {
1757                 chunk = list_entry(lchunk, struct sctp_chunk,
1758                                         transmitted_list);
1759                 tsn = ntohl(chunk->subh.data_hdr->tsn);
1760
1761                 /* Remove any chunks in the abandoned queue that are acked by
1762                  * the ctsn.
1763                  */
1764                 if (TSN_lte(tsn, ctsn)) {
1765                         list_del_init(lchunk);
1766                         sctp_chunk_free(chunk);
1767                 } else {
1768                         if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1769                                 asoc->adv_peer_ack_point = tsn;
1770                                 if (chunk->chunk_hdr->flags &
1771                                          SCTP_DATA_UNORDERED)
1772                                         continue;
1773                                 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1774                                                 nskips,
1775                                                 chunk->subh.data_hdr->stream);
1776                                 ftsn_skip_arr[skip_pos].stream =
1777                                         chunk->subh.data_hdr->stream;
1778                                 ftsn_skip_arr[skip_pos].ssn =
1779                                          chunk->subh.data_hdr->ssn;
1780                                 if (skip_pos == nskips)
1781                                         nskips++;
1782                                 if (nskips == 10)
1783                                         break;
1784                         } else
1785                                 break;
1786                 }
1787         }
1788
1789         /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1790          * is greater than the Cumulative TSN ACK carried in the received
1791          * SACK, the data sender MUST send the data receiver a FORWARD TSN
1792          * chunk containing the latest value of the
1793          * "Advanced.Peer.Ack.Point".
1794          *
1795          * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1796          * list each stream and sequence number in the forwarded TSN. This
1797          * information will enable the receiver to easily find any
1798          * stranded TSN's waiting on stream reorder queues. Each stream
1799          * SHOULD only be reported once; this means that if multiple
1800          * abandoned messages occur in the same stream then only the
1801          * highest abandoned stream sequence number is reported. If the
1802          * total size of the FORWARD TSN does NOT fit in a single MTU then
1803          * the sender of the FORWARD TSN SHOULD lower the
1804          * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1805          * single MTU.
1806          */
1807         if (asoc->adv_peer_ack_point > ctsn)
1808                 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
1809                                               nskips, &ftsn_skip_arr[0]);
1810
1811         if (ftsn_chunk) {
1812                 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1813                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1814         }
1815 }