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