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