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