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