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