outqueue.c 55.5 KB
Newer Older
1
/* SCTP kernel implementation
L
Linus Torvalds 已提交
2 3 4 5 6
 * (C) Copyright IBM Corp. 2001, 2004
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001-2003 Intel Corp.
 *
7
 * This file is part of the SCTP kernel implementation
L
Linus Torvalds 已提交
8 9 10 11
 *
 * These functions implement the sctp_outq class.   The outqueue handles
 * bundling and queueing of outgoing SCTP chunks.
 *
12
 * This SCTP implementation is free software;
L
Linus Torvalds 已提交
13 14 15 16 17
 * you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
18
 * This SCTP implementation is distributed in the hope that it
L
Linus Torvalds 已提交
19 20 21 22 23 24
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *                 ************************
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
25 26
 * along with GNU CC; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
L
Linus Torvalds 已提交
27 28 29
 *
 * Please send any bug reports or fixes you make to the
 * email address(es):
30
 *    lksctp developers <linux-sctp@vger.kernel.org>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41
 *
 * Written or modified by:
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson          <karl@athena.chicago.il.us>
 *    Perry Melange         <pmelange@null.cc.uic.edu>
 *    Xingang Guo           <xingang.guo@intel.com>
 *    Hui Huang 	    <hui.huang@nokia.com>
 *    Sridhar Samudrala     <sri@us.ibm.com>
 *    Jon Grimm             <jgrimm@us.ibm.com>
 */

42 43
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
44 45 46 47
#include <linux/types.h>
#include <linux/list.h>   /* For struct list_head */
#include <linux/socket.h>
#include <linux/ip.h>
48
#include <linux/slab.h>
L
Linus Torvalds 已提交
49 50 51 52
#include <net/sock.h>	  /* For skb_set_owner_w */

#include <net/sctp/sctp.h>
#include <net/sctp/sm.h>
53
#include <net/sctp/stream_sched.h>
L
Linus Torvalds 已提交
54 55 56 57 58 59

/* Declare internal functions here.  */
static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
static void sctp_check_transmitted(struct sctp_outq *q,
				   struct list_head *transmitted_queue,
				   struct sctp_transport *transport,
60
				   union sctp_addr *saddr,
L
Linus Torvalds 已提交
61
				   struct sctp_sackhdr *sack,
62
				   __u32 *highest_new_tsn);
L
Linus Torvalds 已提交
63 64 65 66 67 68 69

static void sctp_mark_missing(struct sctp_outq *q,
			      struct list_head *transmitted_queue,
			      struct sctp_transport *transport,
			      __u32 highest_new_tsn,
			      int count_of_newacks);

70
static void sctp_outq_flush(struct sctp_outq *q, int rtx_timeout, gfp_t gfp);
71

L
Linus Torvalds 已提交
72 73
/* Add data to the front of the queue. */
static inline void sctp_outq_head_data(struct sctp_outq *q,
74
				       struct sctp_chunk *ch)
L
Linus Torvalds 已提交
75
{
76 77 78
	struct sctp_stream_out_ext *oute;
	__u16 stream;

79
	list_add(&ch->list, &q->out_chunk_list);
L
Linus Torvalds 已提交
80
	q->out_qlen += ch->skb->len;
81 82

	stream = sctp_chunk_stream_no(ch);
83
	oute = SCTP_SO(&q->asoc->stream, stream)->ext;
84
	list_add(&ch->stream_list, &oute->outq);
L
Linus Torvalds 已提交
85 86 87 88 89
}

/* Take data from the front of the queue. */
static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
{
90
	return q->sched->dequeue(q);
L
Linus Torvalds 已提交
91
}
92

L
Linus Torvalds 已提交
93 94 95 96
/* Add data chunk to the end of the queue. */
static inline void sctp_outq_tail_data(struct sctp_outq *q,
				       struct sctp_chunk *ch)
{
97 98 99
	struct sctp_stream_out_ext *oute;
	__u16 stream;

100
	list_add_tail(&ch->list, &q->out_chunk_list);
L
Linus Torvalds 已提交
101
	q->out_qlen += ch->skb->len;
102 103

	stream = sctp_chunk_stream_no(ch);
104
	oute = SCTP_SO(&q->asoc->stream, stream)->ext;
105
	list_add_tail(&ch->stream_list, &oute->outq);
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116 117
}

/*
 * SFR-CACC algorithm:
 * D) If count_of_newacks is greater than or equal to 2
 * and t was not sent to the current primary then the
 * sender MUST NOT increment missing report count for t.
 */
static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
				       struct sctp_transport *transport,
				       int count_of_newacks)
{
118
	if (count_of_newacks >= 2 && transport != primary)
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
		return 1;
	return 0;
}

/*
 * SFR-CACC algorithm:
 * F) If count_of_newacks is less than 2, let d be the
 * destination to which t was sent. If cacc_saw_newack
 * is 0 for destination d, then the sender MUST NOT
 * increment missing report count for t.
 */
static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
				       int count_of_newacks)
{
133 134
	if (count_of_newacks < 2 &&
			(transport && !transport->cacc.cacc_saw_newack))
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		return 1;
	return 0;
}

/*
 * SFR-CACC algorithm:
 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
 * execute steps C, D, F.
 *
 * C has been implemented in sctp_outq_sack
 */
static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
				     struct sctp_transport *transport,
				     int count_of_newacks)
{
	if (!primary->cacc.cycling_changeover) {
		if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
			return 1;
		if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
			return 1;
		return 0;
	}
	return 0;
}

/*
 * SFR-CACC algorithm:
 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
 * than next_tsn_at_change of the current primary, then
 * the sender MUST NOT increment missing report count
 * for t.
 */
static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
{
	if (primary->cacc.cycling_changeover &&
	    TSN_lt(tsn, primary->cacc.next_tsn_at_change))
		return 1;
	return 0;
}

/*
 * SFR-CACC algorithm:
 * 3) If the missing report count for TSN t is to be
 * incremented according to [RFC2960] and
 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
L
Lucas De Marchi 已提交
180
 * then the sender MUST further execute steps 3.1 and
L
Linus Torvalds 已提交
181 182 183 184 185
 * 3.2 to determine if the missing report count for
 * TSN t SHOULD NOT be incremented.
 *
 * 3.3) If 3.1 and 3.2 do not dictate that the missing
 * report count for t should not be incremented, then
L
Lucas De Marchi 已提交
186
 * the sender SHOULD increment missing report count for
L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194
 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
 */
static inline int sctp_cacc_skip(struct sctp_transport *primary,
				 struct sctp_transport *transport,
				 int count_of_newacks,
				 __u32 tsn)
{
	if (primary->cacc.changeover_active &&
195 196
	    (sctp_cacc_skip_3_1(primary, transport, count_of_newacks) ||
	     sctp_cacc_skip_3_2(primary, tsn)))
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206
		return 1;
	return 0;
}

/* Initialize an existing sctp_outq.  This does the boring stuff.
 * You still need to define handlers if you really want to DO
 * something with this structure...
 */
void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
{
207 208
	memset(q, 0, sizeof(struct sctp_outq));

L
Linus Torvalds 已提交
209
	q->asoc = asoc;
210 211
	INIT_LIST_HEAD(&q->out_chunk_list);
	INIT_LIST_HEAD(&q->control_chunk_list);
L
Linus Torvalds 已提交
212 213 214
	INIT_LIST_HEAD(&q->retransmit);
	INIT_LIST_HEAD(&q->sacked);
	INIT_LIST_HEAD(&q->abandoned);
215
	sctp_sched_set_sched(asoc, SCTP_SS_FCFS);
L
Linus Torvalds 已提交
216 217 218 219
}

/* Free the outqueue structure and any related pending chunks.
 */
220
static void __sctp_outq_teardown(struct sctp_outq *q)
L
Linus Torvalds 已提交
221 222
{
	struct sctp_transport *transport;
223
	struct list_head *lchunk, *temp;
224
	struct sctp_chunk *chunk, *tmp;
L
Linus Torvalds 已提交
225 226

	/* Throw away unacknowledged chunks. */
227 228
	list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
			transports) {
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
			chunk = list_entry(lchunk, struct sctp_chunk,
					   transmitted_list);
			/* Mark as part of a failed message. */
			sctp_chunk_fail(chunk, q->error);
			sctp_chunk_free(chunk);
		}
	}

	/* Throw away chunks that have been gap ACKed.  */
	list_for_each_safe(lchunk, temp, &q->sacked) {
		list_del_init(lchunk);
		chunk = list_entry(lchunk, struct sctp_chunk,
				   transmitted_list);
		sctp_chunk_fail(chunk, q->error);
		sctp_chunk_free(chunk);
	}

	/* Throw away any chunks in the retransmit queue. */
	list_for_each_safe(lchunk, temp, &q->retransmit) {
		list_del_init(lchunk);
		chunk = list_entry(lchunk, struct sctp_chunk,
				   transmitted_list);
		sctp_chunk_fail(chunk, q->error);
		sctp_chunk_free(chunk);
	}

	/* Throw away any chunks that are in the abandoned queue. */
	list_for_each_safe(lchunk, temp, &q->abandoned) {
		list_del_init(lchunk);
		chunk = list_entry(lchunk, struct sctp_chunk,
				   transmitted_list);
		sctp_chunk_fail(chunk, q->error);
		sctp_chunk_free(chunk);
	}

	/* Throw away any leftover data chunks. */
	while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
267
		sctp_sched_dequeue_done(q, chunk);
L
Linus Torvalds 已提交
268 269 270 271 272 273 274

		/* Mark as send failure. */
		sctp_chunk_fail(chunk, q->error);
		sctp_chunk_free(chunk);
	}

	/* Throw away any leftover control chunks. */
275 276
	list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
		list_del_init(&chunk->list);
L
Linus Torvalds 已提交
277
		sctp_chunk_free(chunk);
278
	}
L
Linus Torvalds 已提交
279 280
}

281 282 283 284 285 286
void sctp_outq_teardown(struct sctp_outq *q)
{
	__sctp_outq_teardown(q);
	sctp_outq_init(q->asoc, q);
}

L
Linus Torvalds 已提交
287 288 289 290
/* Free the outqueue structure and any related pending chunks.  */
void sctp_outq_free(struct sctp_outq *q)
{
	/* Throw away leftover chunks. */
291
	__sctp_outq_teardown(q);
L
Linus Torvalds 已提交
292 293 294
}

/* Put a new chunk in an sctp_outq.  */
295
void sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk, gfp_t gfp)
L
Linus Torvalds 已提交
296
{
297
	struct net *net = sock_net(q->asoc->base.sk);
L
Linus Torvalds 已提交
298

299 300 301 302
	pr_debug("%s: outq:%p, chunk:%p[%s]\n", __func__, q, chunk,
		 chunk && chunk->chunk_hdr ?
		 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
		 "illegal chunk");
L
Linus Torvalds 已提交
303 304 305 306

	/* If it is data, queue it up, otherwise, send it
	 * immediately.
	 */
307
	if (sctp_chunk_is_data(chunk)) {
308 309 310 311 312 313
		pr_debug("%s: outqueueing: outq:%p, chunk:%p[%s])\n",
			 __func__, q, chunk, chunk && chunk->chunk_hdr ?
			 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
			 "illegal chunk");

		sctp_outq_tail_data(q, chunk);
314
		if (chunk->asoc->peer.prsctp_capable &&
315 316 317 318 319 320
		    SCTP_PR_PRIO_ENABLED(chunk->sinfo.sinfo_flags))
			chunk->asoc->sent_cnt_removable++;
		if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
			SCTP_INC_STATS(net, SCTP_MIB_OUTUNORDERCHUNKS);
		else
			SCTP_INC_STATS(net, SCTP_MIB_OUTORDERCHUNKS);
L
Linus Torvalds 已提交
321
	} else {
322
		list_add_tail(&chunk->list, &q->control_chunk_list);
323
		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
L
Linus Torvalds 已提交
324 325 326
	}

	if (!q->cork)
327
		sctp_outq_flush(q, 0, gfp);
L
Linus Torvalds 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
}

/* Insert a chunk into the sorted list based on the TSNs.  The retransmit list
 * and the abandoned list are in ascending order.
 */
static void sctp_insert_list(struct list_head *head, struct list_head *new)
{
	struct list_head *pos;
	struct sctp_chunk *nchunk, *lchunk;
	__u32 ntsn, ltsn;
	int done = 0;

	nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
	ntsn = ntohl(nchunk->subh.data_hdr->tsn);

	list_for_each(pos, head) {
		lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
		ltsn = ntohl(lchunk->subh.data_hdr->tsn);
		if (TSN_lt(ntsn, ltsn)) {
			list_add(new, pos->prev);
			done = 1;
			break;
		}
	}
	if (!done)
353
		list_add_tail(new, head);
L
Linus Torvalds 已提交
354 355
}

X
Xin Long 已提交
356 357 358 359 360 361 362
static int sctp_prsctp_prune_sent(struct sctp_association *asoc,
				  struct sctp_sndrcvinfo *sinfo,
				  struct list_head *queue, int msg_len)
{
	struct sctp_chunk *chk, *temp;

	list_for_each_entry_safe(chk, temp, queue, transmitted_list) {
363 364
		struct sctp_stream_out *streamout;

365 366 367
		if (!chk->msg->abandoned &&
		    (!SCTP_PR_PRIO_ENABLED(chk->sinfo.sinfo_flags) ||
		     chk->sinfo.sinfo_timetolive <= sinfo->sinfo_timetolive))
X
Xin Long 已提交
368 369
			continue;

370
		chk->msg->abandoned = 1;
X
Xin Long 已提交
371 372 373 374
		list_del_init(&chk->transmitted_list);
		sctp_insert_list(&asoc->outqueue.abandoned,
				 &chk->transmitted_list);

375
		streamout = SCTP_SO(&asoc->stream, chk->sinfo.sinfo_stream);
X
Xin Long 已提交
376 377
		asoc->sent_cnt_removable--;
		asoc->abandoned_sent[SCTP_PR_INDEX(PRIO)]++;
378
		streamout->ext->abandoned_sent[SCTP_PR_INDEX(PRIO)]++;
X
Xin Long 已提交
379

380 381
		if (queue != &asoc->outqueue.retransmit &&
		    !chk->tsn_gap_acked) {
X
Xin Long 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
			if (chk->transport)
				chk->transport->flight_size -=
						sctp_data_size(chk);
			asoc->outqueue.outstanding_bytes -= sctp_data_size(chk);
		}

		msg_len -= SCTP_DATA_SNDSIZE(chk) +
			   sizeof(struct sk_buff) +
			   sizeof(struct sctp_chunk);
		if (msg_len <= 0)
			break;
	}

	return msg_len;
}

static int sctp_prsctp_prune_unsent(struct sctp_association *asoc,
399
				    struct sctp_sndrcvinfo *sinfo, int msg_len)
X
Xin Long 已提交
400
{
401
	struct sctp_outq *q = &asoc->outqueue;
X
Xin Long 已提交
402 403
	struct sctp_chunk *chk, *temp;

404 405
	q->sched->unsched_all(&asoc->stream);

406
	list_for_each_entry_safe(chk, temp, &q->out_chunk_list, list) {
407
		if (!chk->msg->abandoned &&
408 409
		    (!(chk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG) ||
		     !SCTP_PR_PRIO_ENABLED(chk->sinfo.sinfo_flags) ||
410
		     chk->sinfo.sinfo_timetolive <= sinfo->sinfo_timetolive))
X
Xin Long 已提交
411 412
			continue;

413
		chk->msg->abandoned = 1;
414
		sctp_sched_dequeue_common(q, chk);
X
Xin Long 已提交
415 416
		asoc->sent_cnt_removable--;
		asoc->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
417
		if (chk->sinfo.sinfo_stream < asoc->stream.outcnt) {
418
			struct sctp_stream_out *streamout =
419
				SCTP_SO(&asoc->stream, chk->sinfo.sinfo_stream);
420

421
			streamout->ext->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
422
		}
X
Xin Long 已提交
423 424 425 426 427 428 429 430 431

		msg_len -= SCTP_DATA_SNDSIZE(chk) +
			   sizeof(struct sk_buff) +
			   sizeof(struct sctp_chunk);
		sctp_chunk_free(chk);
		if (msg_len <= 0)
			break;
	}

432 433
	q->sched->sched_all(&asoc->stream);

X
Xin Long 已提交
434 435 436 437 438 439 440 441 442
	return msg_len;
}

/* Abandon the chunks according their priorities */
void sctp_prsctp_prune(struct sctp_association *asoc,
		       struct sctp_sndrcvinfo *sinfo, int msg_len)
{
	struct sctp_transport *transport;

443
	if (!asoc->peer.prsctp_capable || !asoc->sent_cnt_removable)
X
Xin Long 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		return;

	msg_len = sctp_prsctp_prune_sent(asoc, sinfo,
					 &asoc->outqueue.retransmit,
					 msg_len);
	if (msg_len <= 0)
		return;

	list_for_each_entry(transport, &asoc->peer.transport_addr_list,
			    transports) {
		msg_len = sctp_prsctp_prune_sent(asoc, sinfo,
						 &transport->transmitted,
						 msg_len);
		if (msg_len <= 0)
			return;
	}

461
	sctp_prsctp_prune_unsent(asoc, sinfo, msg_len);
X
Xin Long 已提交
462 463
}

L
Linus Torvalds 已提交
464 465 466
/* Mark all the eligible packets on a transport for retransmission.  */
void sctp_retransmit_mark(struct sctp_outq *q,
			  struct sctp_transport *transport,
467
			  __u8 reason)
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480
{
	struct list_head *lchunk, *ltemp;
	struct sctp_chunk *chunk;

	/* Walk through the specified transmitted queue.  */
	list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
		chunk = list_entry(lchunk, struct sctp_chunk,
				   transmitted_list);

		/* If the chunk is abandoned, move it to abandoned list. */
		if (sctp_chunk_abandoned(chunk)) {
			list_del_init(lchunk);
			sctp_insert_list(&q->abandoned, lchunk);
481 482 483 484 485 486 487

			/* If this chunk has not been previousely acked,
			 * stop considering it 'outstanding'.  Our peer
			 * will most likely never see it since it will
			 * not be retransmitted
			 */
			if (!chunk->tsn_gap_acked) {
488 489 490
				if (chunk->transport)
					chunk->transport->flight_size -=
							sctp_data_size(chunk);
491
				q->outstanding_bytes -= sctp_data_size(chunk);
492
				q->asoc->peer.rwnd += sctp_data_size(chunk);
493
			}
L
Linus Torvalds 已提交
494 495 496
			continue;
		}

497 498 499
		/* If we are doing  retransmission due to a timeout or pmtu
		 * discovery, only the  chunks that are not yet acked should
		 * be added to the retransmit queue.
L
Linus Torvalds 已提交
500
		 */
501
		if ((reason == SCTP_RTXR_FAST_RTX  &&
502
			    (chunk->fast_retransmit == SCTP_NEED_FRTX)) ||
503
		    (reason != SCTP_RTXR_FAST_RTX  && !chunk->tsn_gap_acked)) {
L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511
			/* RFC 2960 6.2.1 Processing a Received SACK
			 *
			 * C) Any time a DATA chunk is marked for
			 * retransmission (via either T3-rtx timer expiration
			 * (Section 6.3.3) or via fast retransmit
			 * (Section 7.2.4)), add the data size of those
			 * chunks to the rwnd.
			 */
512
			q->asoc->peer.rwnd += sctp_data_size(chunk);
L
Linus Torvalds 已提交
513
			q->outstanding_bytes -= sctp_data_size(chunk);
514 515
			if (chunk->transport)
				transport->flight_size -= sctp_data_size(chunk);
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

			/* sctpimpguide-05 Section 2.8.2
			 * M5) If a T3-rtx timer expires, the
			 * 'TSN.Missing.Report' of all affected TSNs is set
			 * to 0.
			 */
			chunk->tsn_missing_report = 0;

			/* If a chunk that is being used for RTT measurement
			 * has to be retransmitted, we cannot use this chunk
			 * anymore for RTT measurements. Reset rto_pending so
			 * that a new RTT measurement is started when a new
			 * data chunk is sent.
			 */
			if (chunk->rtt_in_progress) {
				chunk->rtt_in_progress = 0;
				transport->rto_pending = 0;
			}

			/* Move the chunk to the retransmit queue. The chunks
			 * on the retransmit queue are always kept in order.
			 */
			list_del_init(lchunk);
			sctp_insert_list(&q->retransmit, lchunk);
		}
	}

543 544 545 546
	pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d, "
		 "flight_size:%d, pba:%d\n", __func__, transport, reason,
		 transport->cwnd, transport->ssthresh, transport->flight_size,
		 transport->partial_bytes_acked);
L
Linus Torvalds 已提交
547 548 549 550 551 552
}

/* Mark all the eligible packets on a transport for retransmission and force
 * one packet out.
 */
void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
553
		     enum sctp_retransmit_reason reason)
L
Linus Torvalds 已提交
554
{
555
	struct net *net = sock_net(q->asoc->base.sk);
L
Linus Torvalds 已提交
556

557
	switch (reason) {
L
Linus Torvalds 已提交
558
	case SCTP_RTXR_T3_RTX:
559
		SCTP_INC_STATS(net, SCTP_MIB_T3_RETRANSMITS);
L
Linus Torvalds 已提交
560 561 562 563 564 565
		sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
		/* Update the retran path if the T3-rtx timer has expired for
		 * the current retran path.
		 */
		if (transport == transport->asoc->peer.retran_path)
			sctp_assoc_update_retran_path(transport->asoc);
566 567
		transport->asoc->rtx_data_chunks +=
			transport->asoc->unack_data;
L
Linus Torvalds 已提交
568 569
		break;
	case SCTP_RTXR_FAST_RTX:
570
		SCTP_INC_STATS(net, SCTP_MIB_FAST_RETRANSMITS);
L
Linus Torvalds 已提交
571
		sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
572
		q->fast_rtx = 1;
L
Linus Torvalds 已提交
573 574
		break;
	case SCTP_RTXR_PMTUD:
575
		SCTP_INC_STATS(net, SCTP_MIB_PMTUD_RETRANSMITS);
L
Linus Torvalds 已提交
576
		break;
577
	case SCTP_RTXR_T1_RTX:
578
		SCTP_INC_STATS(net, SCTP_MIB_T1_RETRANSMITS);
579
		transport->asoc->init_retries++;
580
		break;
581 582
	default:
		BUG();
L
Linus Torvalds 已提交
583 584
	}

585
	sctp_retransmit_mark(q, transport, reason);
L
Linus Torvalds 已提交
586 587 588 589 590

	/* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
	 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
	 * following the procedures outlined in C1 - C5.
	 */
591
	if (reason == SCTP_RTXR_T3_RTX)
592
		q->asoc->stream.si->generate_ftsn(q, q->asoc->ctsn_ack_point);
L
Linus Torvalds 已提交
593

594 595 596 597 598
	/* Flush the queues only on timeout, since fast_rtx is only
	 * triggered during sack processing and the queue
	 * will be flushed at the end.
	 */
	if (reason != SCTP_RTXR_FAST_RTX)
599
		sctp_outq_flush(q, /* rtx_timeout */ 1, GFP_ATOMIC);
L
Linus Torvalds 已提交
600 601 602 603
}

/*
 * Transmit DATA chunks on the retransmit queue.  Upon return from
604
 * __sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
L
Linus Torvalds 已提交
605 606 607 608 609
 * need to be transmitted by the caller.
 * We assume that pkt->transport has already been set.
 *
 * The return value is a normal kernel error return value.
 */
610
static int __sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
611
				 int rtx_timeout, int *start_timer, gfp_t gfp)
L
Linus Torvalds 已提交
612 613 614
{
	struct sctp_transport *transport = pkt->transport;
	struct sctp_chunk *chunk, *chunk1;
X
Xin Long 已提交
615 616
	struct list_head *lqueue;
	enum sctp_xmit status;
L
Linus Torvalds 已提交
617
	int error = 0;
618
	int timer = 0;
619
	int done = 0;
X
Xin Long 已提交
620
	int fast_rtx;
L
Linus Torvalds 已提交
621 622

	lqueue = &q->retransmit;
623
	fast_rtx = q->fast_rtx;
L
Linus Torvalds 已提交
624

625 626 627 628
	/* This loop handles time-out retransmissions, fast retransmissions,
	 * and retransmissions due to opening of whindow.
	 *
	 * RFC 2960 6.3.3 Handle T3-rtx Expiration
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642
	 *
	 * E3) Determine how many of the earliest (i.e., lowest TSN)
	 * outstanding DATA chunks for the address for which the
	 * T3-rtx has expired will fit into a single packet, subject
	 * to the MTU constraint for the path corresponding to the
	 * destination transport address to which the retransmission
	 * is being sent (this may be different from the address for
	 * which the timer expires [see Section 6.4]). Call this value
	 * K. Bundle and retransmit those K DATA chunks in a single
	 * packet to the destination endpoint.
	 *
	 * [Just to be painfully clear, if we are retransmitting
	 * because a timeout just happened, we should send only ONE
	 * packet of retransmitted data.]
643 644 645 646
	 *
	 * For fast retransmissions we also send only ONE packet.  However,
	 * if we are just flushing the queue due to open window, we'll
	 * try to send as much as possible.
L
Linus Torvalds 已提交
647
	 */
648
	list_for_each_entry_safe(chunk, chunk1, lqueue, transmitted_list) {
649 650 651 652 653 654 655
		/* If the chunk is abandoned, move it to abandoned list. */
		if (sctp_chunk_abandoned(chunk)) {
			list_del_init(&chunk->transmitted_list);
			sctp_insert_list(&q->abandoned,
					 &chunk->transmitted_list);
			continue;
		}
L
Linus Torvalds 已提交
656 657 658 659 660 661 662

		/* Make sure that Gap Acked TSNs are not retransmitted.  A
		 * simple approach is just to move such TSNs out of the
		 * way and into a 'transmitted' queue and skip to the
		 * next chunk.
		 */
		if (chunk->tsn_gap_acked) {
663 664
			list_move_tail(&chunk->transmitted_list,
				       &transport->transmitted);
L
Linus Torvalds 已提交
665 666 667
			continue;
		}

668 669 670 671 672 673
		/* If we are doing fast retransmit, ignore non-fast_rtransmit
		 * chunks
		 */
		if (fast_rtx && !chunk->fast_retransmit)
			continue;

674
redo:
L
Linus Torvalds 已提交
675 676 677 678 679
		/* Attempt to append this chunk to the packet. */
		status = sctp_packet_append_chunk(pkt, chunk);

		switch (status) {
		case SCTP_XMIT_PMTU_FULL:
680 681 682 683 684 685 686
			if (!pkt->has_data && !pkt->has_cookie_echo) {
				/* If this packet did not contain DATA then
				 * retransmission did not happen, so do it
				 * again.  We'll ignore the error here since
				 * control chunks are already freed so there
				 * is nothing we can do.
				 */
687
				sctp_packet_transmit(pkt, gfp);
688 689 690
				goto redo;
			}

L
Linus Torvalds 已提交
691
			/* Send this packet.  */
692
			error = sctp_packet_transmit(pkt, gfp);
L
Linus Torvalds 已提交
693 694 695

			/* If we are retransmitting, we should only
			 * send a single packet.
696
			 * Otherwise, try appending this chunk again.
L
Linus Torvalds 已提交
697
			 */
698 699
			if (rtx_timeout || fast_rtx)
				done = 1;
700 701
			else
				goto redo;
L
Linus Torvalds 已提交
702

703
			/* Bundle next chunk in the next round.  */
L
Linus Torvalds 已提交
704 705 706
			break;

		case SCTP_XMIT_RWND_FULL:
707
			/* Send this packet. */
708
			error = sctp_packet_transmit(pkt, gfp);
L
Linus Torvalds 已提交
709 710 711 712

			/* Stop sending DATA as there is no more room
			 * at the receiver.
			 */
713
			done = 1;
L
Linus Torvalds 已提交
714 715
			break;

716
		case SCTP_XMIT_DELAY:
717
			/* Send this packet. */
718
			error = sctp_packet_transmit(pkt, gfp);
L
Linus Torvalds 已提交
719 720

			/* Stop sending DATA because of nagle delay. */
721
			done = 1;
L
Linus Torvalds 已提交
722 723 724 725 726 727
			break;

		default:
			/* The append was successful, so add this chunk to
			 * the transmitted list.
			 */
728 729
			list_move_tail(&chunk->transmitted_list,
				       &transport->transmitted);
L
Linus Torvalds 已提交
730

731
			/* Mark the chunk as ineligible for fast retransmit
L
Linus Torvalds 已提交
732 733
			 * after it is retransmitted.
			 */
734 735
			if (chunk->fast_retransmit == SCTP_NEED_FRTX)
				chunk->fast_retransmit = SCTP_DONT_FRTX;
L
Linus Torvalds 已提交
736

737
			q->asoc->stats.rtxchunks++;
L
Linus Torvalds 已提交
738
			break;
739
		}
L
Linus Torvalds 已提交
740

741 742 743 744
		/* Set the timer if there were no errors */
		if (!error && !timer)
			timer = 1;

745 746 747 748 749 750 751 752 753 754 755
		if (done)
			break;
	}

	/* If we are here due to a retransmit timeout or a fast
	 * retransmit and if there are any chunks left in the retransmit
	 * queue that could not fit in the PMTU sized packet, they need
	 * to be marked as ineligible for a subsequent fast retransmit.
	 */
	if (rtx_timeout || fast_rtx) {
		list_for_each_entry(chunk1, lqueue, transmitted_list) {
756 757
			if (chunk1->fast_retransmit == SCTP_NEED_FRTX)
				chunk1->fast_retransmit = SCTP_DONT_FRTX;
L
Linus Torvalds 已提交
758 759 760
		}
	}

761 762 763 764 765 766
	*start_timer = timer;

	/* Clear fast retransmit hint */
	if (fast_rtx)
		q->fast_rtx = 0;

L
Linus Torvalds 已提交
767 768 769 770
	return error;
}

/* Cork the outqueue so queued chunks are really queued. */
771
void sctp_outq_uncork(struct sctp_outq *q, gfp_t gfp)
L
Linus Torvalds 已提交
772
{
773
	if (q->cork)
L
Linus Torvalds 已提交
774
		q->cork = 0;
775

776
	sctp_outq_flush(q, 0, gfp);
L
Linus Torvalds 已提交
777 778
}

779 780 781 782 783 784 785 786 787 788 789 790 791 792
static int sctp_packet_singleton(struct sctp_transport *transport,
				 struct sctp_chunk *chunk, gfp_t gfp)
{
	const struct sctp_association *asoc = transport->asoc;
	const __u16 sport = asoc->base.bind_addr.port;
	const __u16 dport = asoc->peer.port;
	const __u32 vtag = asoc->peer.i.init_tag;
	struct sctp_packet singleton;

	sctp_packet_init(&singleton, transport, sport, dport);
	sctp_packet_config(&singleton, vtag, 0);
	sctp_packet_append_chunk(&singleton, chunk);
	return sctp_packet_transmit(&singleton, gfp);
}
793

794 795 796 797 798 799 800
/* Struct to hold the context during sctp outq flush */
struct sctp_flush_ctx {
	struct sctp_outq *q;
	/* Current transport being used. It's NOT the same as curr active one */
	struct sctp_transport *transport;
	/* These transports have chunks to send. */
	struct list_head transport_list;
801 802 803
	struct sctp_association *asoc;
	/* Packet on the current transport above */
	struct sctp_packet *packet;
804 805 806 807
	gfp_t gfp;
};

/* transport: current transport */
808
static void sctp_outq_select_transport(struct sctp_flush_ctx *ctx,
809
				       struct sctp_chunk *chunk)
810 811 812 813 814
{
	struct sctp_transport *new_transport = chunk->transport;

	if (!new_transport) {
		if (!sctp_chunk_is_data(chunk)) {
815
			/* If we have a prior transport pointer, see if
816 817 818 819 820 821 822 823
			 * the destination address of the chunk
			 * matches the destination address of the
			 * current transport.  If not a match, then
			 * try to look up the transport with a given
			 * destination address.  We do this because
			 * after processing ASCONFs, we may have new
			 * transports created.
			 */
824 825 826
			if (ctx->transport && sctp_cmp_addr_exact(&chunk->dest,
							&ctx->transport->ipaddr))
				new_transport = ctx->transport;
827
			else
828
				new_transport = sctp_assoc_lookup_paddr(ctx->asoc,
829 830 831 832 833 834 835
								  &chunk->dest);
		}

		/* if we still don't have a new transport, then
		 * use the current active path.
		 */
		if (!new_transport)
836
			new_transport = ctx->asoc->peer.active_path;
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	} else {
		__u8 type;

		switch (new_transport->state) {
		case SCTP_INACTIVE:
		case SCTP_UNCONFIRMED:
		case SCTP_PF:
			/* If the chunk is Heartbeat or Heartbeat Ack,
			 * send it to chunk->transport, even if it's
			 * inactive.
			 *
			 * 3.3.6 Heartbeat Acknowledgement:
			 * ...
			 * A HEARTBEAT ACK is always sent to the source IP
			 * address of the IP datagram containing the
			 * HEARTBEAT chunk to which this ack is responding.
			 * ...
			 *
			 * ASCONF_ACKs also must be sent to the source.
			 */
			type = chunk->chunk_hdr->type;
			if (type != SCTP_CID_HEARTBEAT &&
			    type != SCTP_CID_HEARTBEAT_ACK &&
			    type != SCTP_CID_ASCONF_ACK)
861
				new_transport = ctx->asoc->peer.active_path;
862 863 864 865 866 867 868
			break;
		default:
			break;
		}
	}

	/* Are we switching transports? Take care of transport locks. */
869 870
	if (new_transport != ctx->transport) {
		ctx->transport = new_transport;
871 872
		ctx->packet = &ctx->transport->packet;

873 874 875
		if (list_empty(&ctx->transport->send_ready))
			list_add_tail(&ctx->transport->send_ready,
				      &ctx->transport_list);
876

877 878 879
		sctp_packet_config(ctx->packet,
				   ctx->asoc->peer.i.init_tag,
				   ctx->asoc->peer.ecn_capable);
880 881 882
		/* We've switched transports, so apply the
		 * Burst limit to the new transport.
		 */
883
		sctp_transport_burst_limited(ctx->transport);
884 885 886
	}
}

887
static void sctp_outq_flush_ctrl(struct sctp_flush_ctx *ctx)
L
Linus Torvalds 已提交
888
{
889
	struct sctp_chunk *chunk, *tmp;
X
Xin Long 已提交
890
	enum sctp_xmit status;
891
	int one_packet, error;
L
Linus Torvalds 已提交
892

893
	list_for_each_entry_safe(chunk, tmp, &ctx->q->control_chunk_list, list) {
894 895
		one_packet = 0;

896 897 898 899 900 901
		/* RFC 5061, 5.3
		 * F1) This means that until such time as the ASCONF
		 * containing the add is acknowledged, the sender MUST
		 * NOT use the new IP address as a source for ANY SCTP
		 * packet except on carrying an ASCONF Chunk.
		 */
902
		if (ctx->asoc->src_out_of_asoc_ok &&
903 904 905
		    chunk->chunk_hdr->type != SCTP_CID_ASCONF)
			continue;

906 907
		list_del_init(&chunk->list);

908 909
		/* Pick the right transport to use. Should always be true for
		 * the first chunk as we don't have a transport by then.
L
Linus Torvalds 已提交
910
		 */
911
		sctp_outq_select_transport(ctx, chunk);
L
Linus Torvalds 已提交
912 913

		switch (chunk->chunk_hdr->type) {
914
		/* 6.10 Bundling
L
Linus Torvalds 已提交
915 916 917 918 919 920 921
		 *   ...
		 *   An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
		 *   COMPLETE with any other chunks.  [Send them immediately.]
		 */
		case SCTP_CID_INIT:
		case SCTP_CID_INIT_ACK:
		case SCTP_CID_SHUTDOWN_COMPLETE:
922 923
			error = sctp_packet_singleton(ctx->transport, chunk,
						      ctx->gfp);
924
			if (error < 0) {
925
				ctx->asoc->base.sk->sk_err = -error;
926
				return;
927
			}
L
Linus Torvalds 已提交
928 929 930
			break;

		case SCTP_CID_ABORT:
931
			if (sctp_test_T_bit(chunk))
932
				ctx->packet->vtag = ctx->asoc->c.my_vtag;
933
			/* fallthru */
934

935 936 937 938 939
		/* The following chunks are "response" chunks, i.e.
		 * they are generated in response to something we
		 * received.  If we are sending these, then we can
		 * send only 1 packet containing these chunks.
		 */
L
Linus Torvalds 已提交
940 941 942
		case SCTP_CID_HEARTBEAT_ACK:
		case SCTP_CID_SHUTDOWN_ACK:
		case SCTP_CID_COOKIE_ACK:
943 944
		case SCTP_CID_COOKIE_ECHO:
		case SCTP_CID_ERROR:
L
Linus Torvalds 已提交
945 946
		case SCTP_CID_ECN_CWR:
		case SCTP_CID_ASCONF_ACK:
947
			one_packet = 1;
L
Lucas De Marchi 已提交
948
			/* Fall through */
949 950 951 952 953 954

		case SCTP_CID_SACK:
		case SCTP_CID_HEARTBEAT:
		case SCTP_CID_SHUTDOWN:
		case SCTP_CID_ECN_ECNE:
		case SCTP_CID_ASCONF:
L
Linus Torvalds 已提交
955
		case SCTP_CID_FWD_TSN:
956
		case SCTP_CID_I_FWD_TSN:
957
		case SCTP_CID_RECONF:
958
			status = sctp_packet_transmit_chunk(ctx->packet, chunk,
959
							    one_packet, ctx->gfp);
960
			if (status != SCTP_XMIT_OK) {
961
				/* put the chunk back */
962
				list_add(&chunk->list, &ctx->q->control_chunk_list);
963 964 965
				break;
			}

966
			ctx->asoc->stats.octrlchunks++;
967 968 969 970
			/* PR-SCTP C5) If a FORWARD TSN is sent, the
			 * sender MUST assure that at least one T3-rtx
			 * timer is running.
			 */
971 972
			if (chunk->chunk_hdr->type == SCTP_CID_FWD_TSN ||
			    chunk->chunk_hdr->type == SCTP_CID_I_FWD_TSN) {
973 974
				sctp_transport_reset_t3_rtx(ctx->transport);
				ctx->transport->last_time_sent = jiffies;
975
			}
976

977
			if (chunk == ctx->asoc->strreset_chunk)
978
				sctp_transport_reset_reconf_timer(ctx->transport);
979

L
Linus Torvalds 已提交
980 981 982 983 984
			break;

		default:
			/* We built a chunk with an illegal type! */
			BUG();
985
		}
L
Linus Torvalds 已提交
986
	}
987 988
}

989
/* Returns false if new data shouldn't be sent */
990 991
static bool sctp_outq_flush_rtx(struct sctp_flush_ctx *ctx,
				int rtx_timeout)
992 993 994
{
	int error, start_timer = 0;

995
	if (ctx->asoc->peer.retran_path->state == SCTP_UNCONFIRMED)
996 997
		return false;

998
	if (ctx->transport != ctx->asoc->peer.retran_path) {
999
		/* Switch transports & prepare the packet.  */
1000 1001
		ctx->transport = ctx->asoc->peer.retran_path;
		ctx->packet = &ctx->transport->packet;
1002

1003 1004 1005
		if (list_empty(&ctx->transport->send_ready))
			list_add_tail(&ctx->transport->send_ready,
				      &ctx->transport_list);
1006

1007 1008
		sctp_packet_config(ctx->packet, ctx->asoc->peer.i.init_tag,
				   ctx->asoc->peer.ecn_capable);
1009 1010
	}

1011 1012
	error = __sctp_outq_flush_rtx(ctx->q, ctx->packet, rtx_timeout,
				      &start_timer, ctx->gfp);
1013
	if (error < 0)
1014
		ctx->asoc->base.sk->sk_err = -error;
1015 1016

	if (start_timer) {
1017 1018
		sctp_transport_reset_t3_rtx(ctx->transport);
		ctx->transport->last_time_sent = jiffies;
1019 1020 1021 1022 1023
	}

	/* This can happen on COOKIE-ECHO resend.  Only
	 * one chunk can get bundled with a COOKIE-ECHO.
	 */
1024
	if (ctx->packet->has_cookie_echo)
1025 1026 1027 1028 1029
		return false;

	/* Don't send new data if there is still data
	 * waiting to retransmit.
	 */
1030
	if (!list_empty(&ctx->q->retransmit))
1031 1032 1033 1034
		return false;

	return true;
}
1035

1036 1037
static void sctp_outq_flush_data(struct sctp_flush_ctx *ctx,
				 int rtx_timeout)
1038 1039 1040
{
	struct sctp_chunk *chunk;
	enum sctp_xmit status;
1041

L
Linus Torvalds 已提交
1042
	/* Is it OK to send data chunks?  */
1043
	switch (ctx->asoc->state) {
L
Linus Torvalds 已提交
1044 1045 1046 1047
	case SCTP_STATE_COOKIE_ECHOED:
		/* Only allow bundling when this packet has a COOKIE-ECHO
		 * chunk.
		 */
1048
		if (!ctx->packet || !ctx->packet->has_cookie_echo)
1049
			return;
L
Linus Torvalds 已提交
1050

1051
		/* fall through */
L
Linus Torvalds 已提交
1052 1053 1054
	case SCTP_STATE_ESTABLISHED:
	case SCTP_STATE_SHUTDOWN_PENDING:
	case SCTP_STATE_SHUTDOWN_RECEIVED:
1055
		break;
L
Linus Torvalds 已提交
1056

1057 1058 1059 1060
	default:
		/* Do nothing. */
		return;
	}
L
Linus Torvalds 已提交
1061

1062
	/* RFC 2960 6.1  Transmission of DATA Chunks
1063 1064 1065 1066 1067 1068 1069
	 *
	 * C) When the time comes for the sender to transmit,
	 * before sending new DATA chunks, the sender MUST
	 * first transmit any outstanding DATA chunks which
	 * are marked for retransmission (limited by the
	 * current cwnd).
	 */
1070 1071 1072
	if (!list_empty(&ctx->q->retransmit) &&
	    !sctp_outq_flush_rtx(ctx, rtx_timeout))
		return;
1073

1074 1075 1076 1077 1078
	/* Apply Max.Burst limitation to the current transport in
	 * case it will be used for new data.  We are going to
	 * rest it before we return, but we want to apply the limit
	 * to the currently queued data.
	 */
1079 1080
	if (ctx->transport)
		sctp_transport_burst_limited(ctx->transport);
L
Linus Torvalds 已提交
1081

1082
	/* Finally, transmit new packets.  */
1083
	while ((chunk = sctp_outq_dequeue_data(ctx->q)) != NULL) {
1084
		__u32 sid = ntohs(chunk->subh.data_hdr->stream);
1085
		__u8 stream_state = SCTP_SO(&ctx->asoc->stream, sid)->state;
1086

1087 1088
		/* Has this chunk expired? */
		if (sctp_chunk_abandoned(chunk)) {
1089
			sctp_sched_dequeue_done(ctx->q, chunk);
1090 1091 1092 1093
			sctp_chunk_fail(chunk, 0);
			sctp_chunk_free(chunk);
			continue;
		}
1094

1095
		if (stream_state == SCTP_STREAM_CLOSED) {
1096
			sctp_outq_head_data(ctx->q, chunk);
1097 1098
			break;
		}
L
Linus Torvalds 已提交
1099

1100
		sctp_outq_select_transport(ctx, chunk);
1101

1102
		pr_debug("%s: outq:%p, chunk:%p[%s], tx-tsn:0x%x skb->head:%p skb->users:%d\n",
1103
			 __func__, ctx->q, chunk, chunk && chunk->chunk_hdr ?
1104 1105 1106 1107 1108 1109
			 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
			 "illegal chunk", ntohl(chunk->subh.data_hdr->tsn),
			 chunk->skb ? chunk->skb->head : NULL, chunk->skb ?
			 refcount_read(&chunk->skb->users) : -1);

		/* Add the chunk to the packet.  */
1110 1111
		status = sctp_packet_transmit_chunk(ctx->packet, chunk, 0,
						    ctx->gfp);
1112 1113 1114
		if (status != SCTP_XMIT_OK) {
			/* We could not append this chunk, so put
			 * the chunk back on the output queue.
1115
			 */
1116 1117 1118 1119
			pr_debug("%s: could not transmit tsn:0x%x, status:%d\n",
				 __func__, ntohl(chunk->subh.data_hdr->tsn),
				 status);

1120
			sctp_outq_head_data(ctx->q, chunk);
1121 1122
			break;
		}
1123

1124 1125 1126 1127
		/* The sender is in the SHUTDOWN-PENDING state,
		 * The sender MAY set the I-bit in the DATA
		 * chunk header.
		 */
1128
		if (ctx->asoc->state == SCTP_STATE_SHUTDOWN_PENDING)
1129 1130
			chunk->chunk_hdr->flags |= SCTP_DATA_SACK_IMM;
		if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
1131
			ctx->asoc->stats.ouodchunks++;
1132
		else
1133
			ctx->asoc->stats.oodchunks++;
L
Linus Torvalds 已提交
1134

1135 1136 1137
		/* Only now it's safe to consider this
		 * chunk as sent, sched-wise.
		 */
1138
		sctp_sched_dequeue_done(ctx->q, chunk);
L
Linus Torvalds 已提交
1139

1140
		list_add_tail(&chunk->transmitted_list,
1141
			      &ctx->transport->transmitted);
L
Linus Torvalds 已提交
1142

1143 1144
		sctp_transport_reset_t3_rtx(ctx->transport);
		ctx->transport->last_time_sent = jiffies;
1145 1146 1147 1148

		/* Only let one DATA chunk get bundled with a
		 * COOKIE-ECHO chunk.
		 */
1149
		if (ctx->packet->has_cookie_echo)
1150
			break;
L
Linus Torvalds 已提交
1151
	}
1152 1153
}

1154
static void sctp_outq_flush_transports(struct sctp_flush_ctx *ctx)
1155 1156 1157 1158 1159 1160
{
	struct list_head *ltransport;
	struct sctp_packet *packet;
	struct sctp_transport *t;
	int error = 0;

1161
	while ((ltransport = sctp_list_dequeue(&ctx->transport_list)) != NULL) {
1162 1163 1164
		t = list_entry(ltransport, struct sctp_transport, send_ready);
		packet = &t->packet;
		if (!sctp_packet_empty(packet)) {
1165
			error = sctp_packet_transmit(packet, ctx->gfp);
1166
			if (error < 0)
1167
				ctx->q->asoc->base.sk->sk_err = -error;
1168 1169 1170 1171 1172 1173 1174
		}

		/* Clear the burst limited state, if any */
		sctp_transport_burst_reset(t);
	}
}

1175
/* Try to flush an outqueue.
1176 1177 1178 1179 1180 1181 1182
 *
 * Description: Send everything in q which we legally can, subject to
 * congestion limitations.
 * * Note: This function can be called from multiple contexts so appropriate
 * locking concerns must be made.  Today we use the sock lock to protect
 * this function.
 */
1183

1184 1185
static void sctp_outq_flush(struct sctp_outq *q, int rtx_timeout, gfp_t gfp)
{
1186 1187 1188 1189
	struct sctp_flush_ctx ctx = {
		.q = q,
		.transport = NULL,
		.transport_list = LIST_HEAD_INIT(ctx.transport_list),
1190 1191
		.asoc = q->asoc,
		.packet = NULL,
1192 1193
		.gfp = gfp,
	};
1194

1195
	/* 6.10 Bundling
1196 1197 1198 1199 1200 1201 1202 1203
	 *   ...
	 *   When bundling control chunks with DATA chunks, an
	 *   endpoint MUST place control chunks first in the outbound
	 *   SCTP packet.  The transmitter MUST transmit DATA chunks
	 *   within a SCTP packet in increasing order of TSN.
	 *   ...
	 */

1204
	sctp_outq_flush_ctrl(&ctx);
1205 1206 1207 1208

	if (q->asoc->src_out_of_asoc_ok)
		goto sctp_flush_out;

1209
	sctp_outq_flush_data(&ctx, rtx_timeout);
L
Linus Torvalds 已提交
1210 1211 1212

sctp_flush_out:

1213
	sctp_outq_flush_transports(&ctx);
L
Linus Torvalds 已提交
1214 1215 1216 1217 1218 1219
}

/* Update unack_data based on the incoming SACK chunk */
static void sctp_sack_update_unack_data(struct sctp_association *assoc,
					struct sctp_sackhdr *sack)
{
1220
	union sctp_sack_variable *frags;
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	__u16 unack_data;
	int i;

	unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;

	frags = sack->variable;
	for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
		unack_data -= ((ntohs(frags[i].gab.end) -
				ntohs(frags[i].gab.start) + 1));
	}

	assoc->unack_data = unack_data;
}

/* This is where we REALLY process a SACK.
 *
 * Process the SACK against the outqueue.  Mostly, this just frees
 * things off the transmitted queue.
 */
1240
int sctp_outq_sack(struct sctp_outq *q, struct sctp_chunk *chunk)
L
Linus Torvalds 已提交
1241 1242
{
	struct sctp_association *asoc = q->asoc;
1243
	struct sctp_sackhdr *sack = chunk->subh.sack_hdr;
L
Linus Torvalds 已提交
1244 1245
	struct sctp_transport *transport;
	struct sctp_chunk *tchunk = NULL;
1246
	struct list_head *lchunk, *transport_list, *temp;
1247
	union sctp_sack_variable *frags = sack->variable;
L
Linus Torvalds 已提交
1248 1249 1250
	__u32 sack_ctsn, ctsn, tsn;
	__u32 highest_tsn, highest_new_tsn;
	__u32 sack_a_rwnd;
1251
	unsigned int outstanding;
L
Linus Torvalds 已提交
1252 1253
	struct sctp_transport *primary = asoc->peer.primary_path;
	int count_of_newacks = 0;
1254
	int gap_ack_blocks;
1255
	u8 accum_moved = 0;
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260

	/* Grab the association's destination address list. */
	transport_list = &asoc->peer.transport_addr_list;

	sack_ctsn = ntohl(sack->cum_tsn_ack);
1261
	gap_ack_blocks = ntohs(sack->num_gap_ack_blocks);
1262
	asoc->stats.gapcnt += gap_ack_blocks;
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
	/*
	 * SFR-CACC algorithm:
	 * On receipt of a SACK the sender SHOULD execute the
	 * following statements.
	 *
	 * 1) If the cumulative ack in the SACK passes next tsn_at_change
	 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
	 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
	 * all destinations.
	 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
	 * is set the receiver of the SACK MUST take the following actions:
	 *
	 * A) Initialize the cacc_saw_newack to 0 for all destination
	 * addresses.
1277 1278 1279
	 *
	 * Only bother if changeover_active is set. Otherwise, this is
	 * totally suboptimal to do on every SACK.
L
Linus Torvalds 已提交
1280
	 */
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
	if (primary->cacc.changeover_active) {
		u8 clear_cycling = 0;

		if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
			primary->cacc.changeover_active = 0;
			clear_cycling = 1;
		}

		if (clear_cycling || gap_ack_blocks) {
			list_for_each_entry(transport, transport_list,
					transports) {
				if (clear_cycling)
					transport->cacc.cycling_changeover = 0;
				if (gap_ack_blocks)
					transport->cacc.cacc_saw_newack = 0;
			}
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301
		}
	}

	/* Get the highest TSN in the sack. */
	highest_tsn = sack_ctsn;
1302 1303
	if (gap_ack_blocks)
		highest_tsn += ntohs(frags[gap_ack_blocks - 1].gab.end);
L
Linus Torvalds 已提交
1304

1305
	if (TSN_lt(asoc->highest_sacked, highest_tsn))
L
Linus Torvalds 已提交
1306 1307
		asoc->highest_sacked = highest_tsn;

1308
	highest_new_tsn = sack_ctsn;
1309

L
Linus Torvalds 已提交
1310 1311 1312
	/* Run through the retransmit queue.  Credit bytes received
	 * and free those chunks that we can.
	 */
1313
	sctp_check_transmitted(q, &q->retransmit, NULL, NULL, sack, &highest_new_tsn);
L
Linus Torvalds 已提交
1314 1315 1316 1317 1318 1319

	/* Run through the transmitted queue.
	 * Credit bytes received and free those chunks which we can.
	 *
	 * This is a MASSIVE candidate for optimization.
	 */
1320
	list_for_each_entry(transport, transport_list, transports) {
L
Linus Torvalds 已提交
1321
		sctp_check_transmitted(q, &transport->transmitted,
1322 1323
				       transport, &chunk->source, sack,
				       &highest_new_tsn);
L
Linus Torvalds 已提交
1324 1325 1326 1327 1328 1329
		/*
		 * SFR-CACC algorithm:
		 * C) Let count_of_newacks be the number of
		 * destinations for which cacc_saw_newack is set.
		 */
		if (transport->cacc.cacc_saw_newack)
1330
			count_of_newacks++;
L
Linus Torvalds 已提交
1331 1332
	}

1333 1334 1335 1336 1337 1338
	/* Move the Cumulative TSN Ack Point if appropriate.  */
	if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn)) {
		asoc->ctsn_ack_point = sack_ctsn;
		accum_moved = 1;
	}

1339
	if (gap_ack_blocks) {
1340 1341 1342 1343

		if (asoc->fast_recovery && accum_moved)
			highest_new_tsn = highest_tsn;

1344 1345 1346
		list_for_each_entry(transport, transport_list, transports)
			sctp_mark_missing(q, &transport->transmitted, transport,
					  highest_new_tsn, count_of_newacks);
L
Linus Torvalds 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	}

	/* Update unack_data field in the assoc. */
	sctp_sack_update_unack_data(asoc, sack);

	ctsn = asoc->ctsn_ack_point;

	/* Throw away stuff rotting on the sack queue.  */
	list_for_each_safe(lchunk, temp, &q->sacked) {
		tchunk = list_entry(lchunk, struct sctp_chunk,
				    transmitted_list);
		tsn = ntohl(tchunk->subh.data_hdr->tsn);
1359 1360
		if (TSN_lte(tsn, ctsn)) {
			list_del_init(&tchunk->transmitted_list);
1361
			if (asoc->peer.prsctp_capable &&
X
Xin Long 已提交
1362 1363
			    SCTP_PR_PRIO_ENABLED(chunk->sinfo.sinfo_flags))
				asoc->sent_cnt_removable--;
L
Linus Torvalds 已提交
1364
			sctp_chunk_free(tchunk);
1365
		}
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371 1372 1373
	}

	/* ii) Set rwnd equal to the newly received a_rwnd minus the
	 *     number of bytes still outstanding after processing the
	 *     Cumulative TSN Ack and the Gap Ack Blocks.
	 */

	sack_a_rwnd = ntohl(sack->a_rwnd);
1374
	asoc->peer.zero_window_announced = !sack_a_rwnd;
L
Linus Torvalds 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383
	outstanding = q->outstanding_bytes;

	if (outstanding < sack_a_rwnd)
		sack_a_rwnd -= outstanding;
	else
		sack_a_rwnd = 0;

	asoc->peer.rwnd = sack_a_rwnd;

1384
	asoc->stream.si->generate_ftsn(q, sack_ctsn);
L
Linus Torvalds 已提交
1385

1386 1387 1388 1389
	pr_debug("%s: sack cumulative tsn ack:0x%x\n", __func__, sack_ctsn);
	pr_debug("%s: cumulative tsn ack of assoc:%p is 0x%x, "
		 "advertised peer ack point:0x%x\n", __func__, asoc, ctsn,
		 asoc->adv_peer_ack_point);
L
Linus Torvalds 已提交
1390

1391
	return sctp_outq_is_empty(q);
L
Linus Torvalds 已提交
1392 1393
}

1394 1395 1396 1397
/* Is the outqueue empty?
 * The queue is empty when we have not pending data, no in-flight data
 * and nothing pending retransmissions.
 */
L
Linus Torvalds 已提交
1398 1399
int sctp_outq_is_empty(const struct sctp_outq *q)
{
1400 1401
	return q->out_qlen == 0 && q->outstanding_bytes == 0 &&
	       list_empty(&q->retransmit);
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
}

/********************************************************************
 * 2nd Level Abstractions
 ********************************************************************/

/* Go through a transport's transmitted list or the association's retransmit
 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
 * The retransmit list will not have an associated transport.
 *
 * I added coherent debug information output.	--xguo
 *
 * Instead of printing 'sacked' or 'kept' for each TSN on the
 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
 * KEPT TSN6-TSN7, etc.
 */
static void sctp_check_transmitted(struct sctp_outq *q,
				   struct list_head *transmitted_queue,
				   struct sctp_transport *transport,
1421
				   union sctp_addr *saddr,
L
Linus Torvalds 已提交
1422
				   struct sctp_sackhdr *sack,
1423
				   __u32 *highest_new_tsn_in_sack)
L
Linus Torvalds 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432
{
	struct list_head *lchunk;
	struct sctp_chunk *tchunk;
	struct list_head tlist;
	__u32 tsn;
	__u32 sack_ctsn;
	__u32 rtt;
	__u8 restart_timer = 0;
	int bytes_acked = 0;
1433
	int migrate_bytes = 0;
1434
	bool forward_progress = false;
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

	sack_ctsn = ntohl(sack->cum_tsn_ack);

	INIT_LIST_HEAD(&tlist);

	/* The while loop will skip empty transmitted queues. */
	while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
		tchunk = list_entry(lchunk, struct sctp_chunk,
				    transmitted_list);

		if (sctp_chunk_abandoned(tchunk)) {
			/* Move the chunk to abandoned list. */
			sctp_insert_list(&q->abandoned, lchunk);
1448 1449 1450 1451

			/* If this chunk has not been acked, stop
			 * considering it as 'outstanding'.
			 */
1452 1453
			if (transmitted_queue != &q->retransmit &&
			    !tchunk->tsn_gap_acked) {
1454 1455 1456
				if (tchunk->transport)
					tchunk->transport->flight_size -=
							sctp_data_size(tchunk);
1457 1458
				q->outstanding_bytes -= sctp_data_size(tchunk);
			}
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
			continue;
		}

		tsn = ntohl(tchunk->subh.data_hdr->tsn);
		if (sctp_acked(sack, tsn)) {
			/* If this queue is the retransmit queue, the
			 * retransmit timer has already reclaimed
			 * the outstanding bytes for this chunk, so only
			 * count bytes associated with a transport.
			 */
1469
			if (transport && !tchunk->tsn_gap_acked) {
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
				/* If this chunk is being used for RTT
				 * measurement, calculate the RTT and update
				 * the RTO using this value.
				 *
				 * 6.3.1 C5) Karn's algorithm: RTT measurements
				 * MUST NOT be made using packets that were
				 * retransmitted (and thus for which it is
				 * ambiguous whether the reply was for the
				 * first instance of the packet or a later
				 * instance).
				 */
1481
				if (!sctp_chunk_retransmitted(tchunk) &&
L
Linus Torvalds 已提交
1482
				    tchunk->rtt_in_progress) {
1483
					tchunk->rtt_in_progress = 0;
L
Linus Torvalds 已提交
1484 1485 1486 1487
					rtt = jiffies - tchunk->sent_at;
					sctp_transport_update_rto(transport,
								  rtt);
				}
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

				if (TSN_lte(tsn, sack_ctsn)) {
					/*
					 * SFR-CACC algorithm:
					 * 2) If the SACK contains gap acks
					 * and the flag CHANGEOVER_ACTIVE is
					 * set the receiver of the SACK MUST
					 * take the following action:
					 *
					 * B) For each TSN t being acked that
					 * has not been acked in any SACK so
					 * far, set cacc_saw_newack to 1 for
					 * the destination that the TSN was
					 * sent to.
					 */
					if (sack->num_gap_ack_blocks &&
					    q->asoc->peer.primary_path->cacc.
					    changeover_active)
						transport->cacc.cacc_saw_newack
							= 1;
				}
L
Linus Torvalds 已提交
1509
			}
1510 1511 1512 1513 1514 1515 1516 1517 1518

			/* If the chunk hasn't been marked as ACKED,
			 * mark it and account bytes_acked if the
			 * chunk had a valid transport (it will not
			 * have a transport if ASCONF had deleted it
			 * while DATA was outstanding).
			 */
			if (!tchunk->tsn_gap_acked) {
				tchunk->tsn_gap_acked = 1;
1519 1520
				if (TSN_lt(*highest_new_tsn_in_sack, tsn))
					*highest_new_tsn_in_sack = tsn;
1521 1522 1523
				bytes_acked += sctp_data_size(tchunk);
				if (!tchunk->transport)
					migrate_bytes += sctp_data_size(tchunk);
1524
				forward_progress = true;
1525 1526
			}

1527
			if (TSN_lte(tsn, sack_ctsn)) {
L
Linus Torvalds 已提交
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
				/* RFC 2960  6.3.2 Retransmission Timer Rules
				 *
				 * R3) Whenever a SACK is received
				 * that acknowledges the DATA chunk
				 * with the earliest outstanding TSN
				 * for that address, restart T3-rtx
				 * timer for that address with its
				 * current RTO.
				 */
				restart_timer = 1;
1538
				forward_progress = true;
L
Linus Torvalds 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

				list_add_tail(&tchunk->transmitted_list,
					      &q->sacked);
			} else {
				/* RFC2960 7.2.4, sctpimpguide-05 2.8.2
				 * M2) Each time a SACK arrives reporting
				 * 'Stray DATA chunk(s)' record the highest TSN
				 * reported as newly acknowledged, call this
				 * value 'HighestTSNinSack'. A newly
				 * acknowledged DATA chunk is one not
				 * previously acknowledged in a SACK.
				 *
				 * When the SCTP sender of data receives a SACK
				 * chunk that acknowledges, for the first time,
				 * the receipt of a DATA chunk, all the still
				 * unacknowledged DATA chunks whose TSN is
				 * older than that newly acknowledged DATA
				 * chunk, are qualified as 'Stray DATA chunks'.
				 */
				list_add_tail(lchunk, &tlist);
			}
		} else {
			if (tchunk->tsn_gap_acked) {
1562 1563 1564
				pr_debug("%s: receiver reneged on data TSN:0x%x\n",
					 __func__, tsn);

L
Linus Torvalds 已提交
1565 1566
				tchunk->tsn_gap_acked = 0;

1567 1568
				if (tchunk->transport)
					bytes_acked -= sctp_data_size(tchunk);
L
Linus Torvalds 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587

				/* RFC 2960 6.3.2 Retransmission Timer Rules
				 *
				 * R4) Whenever a SACK is received missing a
				 * TSN that was previously acknowledged via a
				 * Gap Ack Block, start T3-rtx for the
				 * destination address to which the DATA
				 * chunk was originally
				 * transmitted if it is not already running.
				 */
				restart_timer = 1;
			}

			list_add_tail(lchunk, &tlist);
		}
	}

	if (transport) {
		if (bytes_acked) {
1588 1589
			struct sctp_association *asoc = transport->asoc;

1590 1591 1592 1593 1594 1595 1596 1597
			/* We may have counted DATA that was migrated
			 * to this transport due to DEL-IP operation.
			 * Subtract those bytes, since the were never
			 * send on this transport and shouldn't be
			 * credited to this transport.
			 */
			bytes_acked -= migrate_bytes;

L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606
			/* 8.2. When an outstanding TSN is acknowledged,
			 * the endpoint shall clear the error counter of
			 * the destination transport address to which the
			 * DATA chunk was last sent.
			 * The association's overall error counter is
			 * also cleared.
			 */
			transport->error_count = 0;
			transport->asoc->overall_error_count = 0;
1607
			forward_progress = true;
L
Linus Torvalds 已提交
1608

1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
			/*
			 * While in SHUTDOWN PENDING, we may have started
			 * the T5 shutdown guard timer after reaching the
			 * retransmission limit. Stop that timer as soon
			 * as the receiver acknowledged any data.
			 */
			if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING &&
			    del_timer(&asoc->timers
				[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]))
					sctp_association_put(asoc);

L
Linus Torvalds 已提交
1620 1621 1622
			/* Mark the destination transport address as
			 * active if it is not so marked.
			 */
1623 1624 1625
			if ((transport->state == SCTP_INACTIVE ||
			     transport->state == SCTP_UNCONFIRMED) &&
			    sctp_cmp_addr_exact(&transport->ipaddr, saddr)) {
L
Linus Torvalds 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
				sctp_assoc_control_transport(
					transport->asoc,
					transport,
					SCTP_TRANSPORT_UP,
					SCTP_RECEIVED_SACK);
			}

			sctp_transport_raise_cwnd(transport, sack_ctsn,
						  bytes_acked);

			transport->flight_size -= bytes_acked;
1637 1638
			if (transport->flight_size == 0)
				transport->partial_bytes_acked = 0;
1639
			q->outstanding_bytes -= bytes_acked + migrate_bytes;
L
Linus Torvalds 已提交
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
		} else {
			/* RFC 2960 6.1, sctpimpguide-06 2.15.2
			 * When a sender is doing zero window probing, it
			 * should not timeout the association if it continues
			 * to receive new packets from the receiver. The
			 * reason is that the receiver MAY keep its window
			 * closed for an indefinite time.
			 * A sender is doing zero window probing when the
			 * receiver's advertised window is zero, and there is
			 * only one data chunk in flight to the receiver.
1650 1651 1652 1653
			 *
			 * Allow the association to timeout while in SHUTDOWN
			 * PENDING or SHUTDOWN RECEIVED in case the receiver
			 * stays in zero window mode forever.
L
Linus Torvalds 已提交
1654 1655 1656
			 */
			if (!q->asoc->peer.rwnd &&
			    !list_empty(&tlist) &&
1657 1658
			    (sack_ctsn+2 == q->asoc->next_tsn) &&
			    q->asoc->state < SCTP_STATE_SHUTDOWN_PENDING) {
1659 1660 1661
				pr_debug("%s: sack received for zero window "
					 "probe:%u\n", __func__, sack_ctsn);

L
Linus Torvalds 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
				q->asoc->overall_error_count = 0;
				transport->error_count = 0;
			}
		}

		/* RFC 2960 6.3.2 Retransmission Timer Rules
		 *
		 * R2) Whenever all outstanding data sent to an address have
		 * been acknowledged, turn off the T3-rtx timer of that
		 * address.
		 */
		if (!transport->flight_size) {
1674
			if (del_timer(&transport->T3_rtx_timer))
L
Linus Torvalds 已提交
1675 1676 1677 1678 1679 1680
				sctp_transport_put(transport);
		} else if (restart_timer) {
			if (!mod_timer(&transport->T3_rtx_timer,
				       jiffies + transport->rto))
				sctp_transport_hold(transport);
		}
1681 1682 1683

		if (forward_progress) {
			if (transport->dst)
1684
				sctp_transport_dst_confirm(transport);
1685
		}
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
	}

	list_splice(&tlist, transmitted_queue);
}

/* Mark chunks as missing and consequently may get retransmitted. */
static void sctp_mark_missing(struct sctp_outq *q,
			      struct list_head *transmitted_queue,
			      struct sctp_transport *transport,
			      __u32 highest_new_tsn_in_sack,
			      int count_of_newacks)
{
	struct sctp_chunk *chunk;
	__u32 tsn;
	char do_fast_retransmit = 0;
1701 1702
	struct sctp_association *asoc = q->asoc;
	struct sctp_transport *primary = asoc->peer.primary_path;
L
Linus Torvalds 已提交
1703

1704
	list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
L
Linus Torvalds 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

		tsn = ntohl(chunk->subh.data_hdr->tsn);

		/* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
		 * 'Unacknowledged TSN's', if the TSN number of an
		 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
		 * value, increment the 'TSN.Missing.Report' count on that
		 * chunk if it has NOT been fast retransmitted or marked for
		 * fast retransmit already.
		 */
1715
		if (chunk->fast_retransmit == SCTP_CAN_FRTX &&
L
Linus Torvalds 已提交
1716 1717 1718 1719 1720 1721
		    !chunk->tsn_gap_acked &&
		    TSN_lt(tsn, highest_new_tsn_in_sack)) {

			/* SFR-CACC may require us to skip marking
			 * this chunk as missing.
			 */
1722 1723 1724
			if (!transport || !sctp_cacc_skip(primary,
						chunk->transport,
						count_of_newacks, tsn)) {
L
Linus Torvalds 已提交
1725 1726
				chunk->tsn_missing_report++;

1727 1728
				pr_debug("%s: tsn:0x%x missing counter:%d\n",
					 __func__, tsn, chunk->tsn_missing_report);
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733
			}
		}
		/*
		 * M4) If any DATA chunk is found to have a
		 * 'TSN.Missing.Report'
1734
		 * value larger than or equal to 3, mark that chunk for
L
Linus Torvalds 已提交
1735 1736 1737
		 * retransmission and start the fast retransmit procedure.
		 */

1738
		if (chunk->tsn_missing_report >= 3) {
1739
			chunk->fast_retransmit = SCTP_NEED_FRTX;
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746 1747
			do_fast_retransmit = 1;
		}
	}

	if (transport) {
		if (do_fast_retransmit)
			sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);

1748 1749 1750 1751
		pr_debug("%s: transport:%p, cwnd:%d, ssthresh:%d, "
			 "flight_size:%d, pba:%d\n",  __func__, transport,
			 transport->cwnd, transport->ssthresh,
			 transport->flight_size, transport->partial_bytes_acked);
L
Linus Torvalds 已提交
1752 1753 1754 1755 1756 1757 1758
	}
}

/* Is the given TSN acked by this packet?  */
static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
{
	__u32 ctsn = ntohl(sack->cum_tsn_ack);
1759 1760 1761
	union sctp_sack_variable *frags;
	__u16 tsn_offset, blocks;
	int i;
L
Linus Torvalds 已提交
1762

1763
	if (TSN_lte(tsn, ctsn))
L
Linus Torvalds 已提交
1764 1765
		goto pass;

1766
	/* 3.3.4 Selective Acknowledgment (SACK) (3):
L
Linus Torvalds 已提交
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
	 *
	 * Gap Ack Blocks:
	 *  These fields contain the Gap Ack Blocks. They are repeated
	 *  for each Gap Ack Block up to the number of Gap Ack Blocks
	 *  defined in the Number of Gap Ack Blocks field. All DATA
	 *  chunks with TSNs greater than or equal to (Cumulative TSN
	 *  Ack + Gap Ack Block Start) and less than or equal to
	 *  (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
	 *  Block are assumed to have been received correctly.
	 */

	frags = sack->variable;
1779 1780 1781 1782 1783
	blocks = ntohs(sack->num_gap_ack_blocks);
	tsn_offset = tsn - ctsn;
	for (i = 0; i < blocks; ++i) {
		if (tsn_offset >= ntohs(frags[i].gab.start) &&
		    tsn_offset <= ntohs(frags[i].gab.end))
L
Linus Torvalds 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792
			goto pass;
	}

	return 0;
pass:
	return 1;
}

static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
A
Al Viro 已提交
1793
				    int nskips, __be16 stream)
L
Linus Torvalds 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
{
	int i;

	for (i = 0; i < nskips; i++) {
		if (skiplist[i].stream == stream)
			return i;
	}
	return i;
}

/* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1805
void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
L
Linus Torvalds 已提交
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
{
	struct sctp_association *asoc = q->asoc;
	struct sctp_chunk *ftsn_chunk = NULL;
	struct sctp_fwdtsn_skip ftsn_skip_arr[10];
	int nskips = 0;
	int skip_pos = 0;
	__u32 tsn;
	struct sctp_chunk *chunk;
	struct list_head *lchunk, *temp;

1816 1817 1818
	if (!asoc->peer.prsctp_capable)
		return;

L
Linus Torvalds 已提交
1819 1820
	/* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
	 * received SACK.
1821
	 *
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
	 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
	 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
	 */
	if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
		asoc->adv_peer_ack_point = ctsn;

	/* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
	 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
	 * the chunk next in the out-queue space is marked as "abandoned" as
	 * shown in the following example:
	 *
	 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
	 * and the Advanced.Peer.Ack.Point is updated to this value:
1835
	 *
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
	 *   out-queue at the end of  ==>   out-queue after Adv.Ack.Point
	 *   normal SACK processing           local advancement
	 *                ...                           ...
	 *   Adv.Ack.Pt-> 102 acked                     102 acked
	 *                103 abandoned                 103 abandoned
	 *                104 abandoned     Adv.Ack.P-> 104 abandoned
	 *                105                           105
	 *                106 acked                     106 acked
	 *                ...                           ...
	 *
	 * In this example, the data sender successfully advanced the
	 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
	 */
	list_for_each_safe(lchunk, temp, &q->abandoned) {
		chunk = list_entry(lchunk, struct sctp_chunk,
					transmitted_list);
		tsn = ntohl(chunk->subh.data_hdr->tsn);

		/* Remove any chunks in the abandoned queue that are acked by
		 * the ctsn.
1856
		 */
L
Linus Torvalds 已提交
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
		if (TSN_lte(tsn, ctsn)) {
			list_del_init(lchunk);
			sctp_chunk_free(chunk);
		} else {
			if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
				asoc->adv_peer_ack_point = tsn;
				if (chunk->chunk_hdr->flags &
					 SCTP_DATA_UNORDERED)
					continue;
				skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
						nskips,
						chunk->subh.data_hdr->stream);
				ftsn_skip_arr[skip_pos].stream =
					chunk->subh.data_hdr->stream;
				ftsn_skip_arr[skip_pos].ssn =
					 chunk->subh.data_hdr->ssn;
				if (skip_pos == nskips)
					nskips++;
				if (nskips == 10)
					break;
			} else
				break;
		}
	}

	/* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
	 * is greater than the Cumulative TSN ACK carried in the received
	 * SACK, the data sender MUST send the data receiver a FORWARD TSN
	 * chunk containing the latest value of the
	 * "Advanced.Peer.Ack.Point".
	 *
	 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
	 * list each stream and sequence number in the forwarded TSN. This
	 * information will enable the receiver to easily find any
	 * stranded TSN's waiting on stream reorder queues. Each stream
	 * SHOULD only be reported once; this means that if multiple
	 * abandoned messages occur in the same stream then only the
	 * highest abandoned stream sequence number is reported. If the
	 * total size of the FORWARD TSN does NOT fit in a single MTU then
	 * the sender of the FORWARD TSN SHOULD lower the
	 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
	 * single MTU.
	 */
	if (asoc->adv_peer_ack_point > ctsn)
		ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
1902
					      nskips, &ftsn_skip_arr[0]);
L
Linus Torvalds 已提交
1903 1904

	if (ftsn_chunk) {
1905
		list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1906
		SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_OUTCTRLCHUNKS);
L
Linus Torvalds 已提交
1907 1908
	}
}