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