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