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