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