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