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