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