blob: 111516c3d34cceb1e511f55df542860ccbb099ea [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05007 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * These functions implement the sctp_outq class. The outqueue handles
10 * bundling and queueing of outgoing SCTP chunks.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * 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
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080025 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020030 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
Joe Perches145ce502010-08-24 13:21:08 +000042#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/types.h>
45#include <linux/list.h> /* For struct list_head */
46#include <linux/socket.h>
47#include <linux/ip.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/sock.h> /* For skb_set_owner_w */
50
51#include <net/sctp/sctp.h>
52#include <net/sctp/sm.h>
53
54/* Declare internal functions here. */
55static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
56static void sctp_check_transmitted(struct sctp_outq *q,
57 struct list_head *transmitted_queue,
58 struct sctp_transport *transport,
Nicolas Dichteledfee032012-10-03 05:43:22 +000059 union sctp_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct sctp_sackhdr *sack,
Vlad Yasevichbfa0d982010-04-30 22:41:10 -040061 __u32 *highest_new_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63static void sctp_mark_missing(struct sctp_outq *q,
64 struct list_head *transmitted_queue,
65 struct sctp_transport *transport,
66 __u32 highest_new_tsn,
67 int count_of_newacks);
68
69static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 sack_ctsn);
70
Adrian Bunkabd0b1982008-07-22 14:20:45 -070071static int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Add data to the front of the queue. */
74static inline void sctp_outq_head_data(struct sctp_outq *q,
75 struct sctp_chunk *ch)
76{
David S. Miller79af02c2005-07-08 21:47:49 -070077 list_add(&ch->list, &q->out_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 q->out_qlen += ch->skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81/* Take data from the front of the queue. */
82static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
83{
David S. Miller79af02c2005-07-08 21:47:49 -070084 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 q->out_qlen -= ch->skb->len;
David S. Miller79af02c2005-07-08 21:47:49 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return ch;
94}
95/* Add data chunk to the end of the queue. */
96static inline void sctp_outq_tail_data(struct sctp_outq *q,
97 struct sctp_chunk *ch)
98{
David S. Miller79af02c2005-07-08 21:47:49 -070099 list_add_tail(&ch->list, &q->out_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 q->out_qlen += ch->skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/*
104 * SFR-CACC algorithm:
105 * D) If count_of_newacks is greater than or equal to 2
106 * and t was not sent to the current primary then the
107 * sender MUST NOT increment missing report count for t.
108 */
109static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
110 struct sctp_transport *transport,
111 int count_of_newacks)
112{
wangweidongcb3f8372013-12-23 12:16:50 +0800113 if (count_of_newacks >= 2 && transport != primary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 1;
115 return 0;
116}
117
118/*
119 * SFR-CACC algorithm:
120 * F) If count_of_newacks is less than 2, let d be the
121 * destination to which t was sent. If cacc_saw_newack
122 * is 0 for destination d, then the sender MUST NOT
123 * increment missing report count for t.
124 */
125static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
126 int count_of_newacks)
127{
Vlad Yasevichf246a7b2011-04-18 19:13:56 +0000128 if (count_of_newacks < 2 &&
129 (transport && !transport->cacc.cacc_saw_newack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 1;
131 return 0;
132}
133
134/*
135 * SFR-CACC algorithm:
136 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
137 * execute steps C, D, F.
138 *
139 * C has been implemented in sctp_outq_sack
140 */
141static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
142 struct sctp_transport *transport,
143 int count_of_newacks)
144{
145 if (!primary->cacc.cycling_changeover) {
146 if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
147 return 1;
148 if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
149 return 1;
150 return 0;
151 }
152 return 0;
153}
154
155/*
156 * SFR-CACC algorithm:
157 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
158 * than next_tsn_at_change of the current primary, then
159 * the sender MUST NOT increment missing report count
160 * for t.
161 */
162static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
163{
164 if (primary->cacc.cycling_changeover &&
165 TSN_lt(tsn, primary->cacc.next_tsn_at_change))
166 return 1;
167 return 0;
168}
169
170/*
171 * SFR-CACC algorithm:
172 * 3) If the missing report count for TSN t is to be
173 * incremented according to [RFC2960] and
174 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300175 * then the sender MUST further execute steps 3.1 and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300181 * the sender SHOULD increment missing report count for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
183 */
184static inline int sctp_cacc_skip(struct sctp_transport *primary,
185 struct sctp_transport *transport,
186 int count_of_newacks,
187 __u32 tsn)
188{
189 if (primary->cacc.changeover_active &&
Joe Perchesf64f9e72009-11-29 16:55:45 -0800190 (sctp_cacc_skip_3_1(primary, transport, count_of_newacks) ||
191 sctp_cacc_skip_3_2(primary, tsn)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 1;
193 return 0;
194}
195
196/* Initialize an existing sctp_outq. This does the boring stuff.
197 * You still need to define handlers if you really want to DO
198 * something with this structure...
199 */
200void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
201{
Neil Hormanc5c77742013-06-12 14:26:44 -0400202 memset(q, 0, sizeof(struct sctp_outq));
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 q->asoc = asoc;
David S. Miller79af02c2005-07-08 21:47:49 -0700205 INIT_LIST_HEAD(&q->out_chunk_list);
206 INIT_LIST_HEAD(&q->control_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 INIT_LIST_HEAD(&q->retransmit);
208 INIT_LIST_HEAD(&q->sacked);
209 INIT_LIST_HEAD(&q->abandoned);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 q->empty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/* Free the outqueue structure and any related pending chunks.
215 */
Neil Horman2f94aab2013-01-17 11:15:08 +0000216static void __sctp_outq_teardown(struct sctp_outq *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct sctp_transport *transport;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700219 struct list_head *lchunk, *temp;
David S. Miller79af02c2005-07-08 21:47:49 -0700220 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Throw away unacknowledged chunks. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700223 list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
224 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
226 chunk = list_entry(lchunk, struct sctp_chunk,
227 transmitted_list);
228 /* Mark as part of a failed message. */
229 sctp_chunk_fail(chunk, q->error);
230 sctp_chunk_free(chunk);
231 }
232 }
233
234 /* Throw away chunks that have been gap ACKed. */
235 list_for_each_safe(lchunk, temp, &q->sacked) {
236 list_del_init(lchunk);
237 chunk = list_entry(lchunk, struct sctp_chunk,
238 transmitted_list);
239 sctp_chunk_fail(chunk, q->error);
240 sctp_chunk_free(chunk);
241 }
242
243 /* Throw away any chunks in the retransmit queue. */
244 list_for_each_safe(lchunk, temp, &q->retransmit) {
245 list_del_init(lchunk);
246 chunk = list_entry(lchunk, struct sctp_chunk,
247 transmitted_list);
248 sctp_chunk_fail(chunk, q->error);
249 sctp_chunk_free(chunk);
250 }
251
252 /* Throw away any chunks that are in the abandoned queue. */
253 list_for_each_safe(lchunk, temp, &q->abandoned) {
254 list_del_init(lchunk);
255 chunk = list_entry(lchunk, struct sctp_chunk,
256 transmitted_list);
257 sctp_chunk_fail(chunk, q->error);
258 sctp_chunk_free(chunk);
259 }
260
261 /* Throw away any leftover data chunks. */
262 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
263
264 /* Mark as send failure. */
265 sctp_chunk_fail(chunk, q->error);
266 sctp_chunk_free(chunk);
267 }
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* Throw away any leftover control chunks. */
David S. Miller79af02c2005-07-08 21:47:49 -0700270 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
271 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sctp_chunk_free(chunk);
David S. Miller79af02c2005-07-08 21:47:49 -0700273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Neil Horman2f94aab2013-01-17 11:15:08 +0000276void sctp_outq_teardown(struct sctp_outq *q)
277{
278 __sctp_outq_teardown(q);
279 sctp_outq_init(q->asoc, q);
280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/* Free the outqueue structure and any related pending chunks. */
283void sctp_outq_free(struct sctp_outq *q)
284{
285 /* Throw away leftover chunks. */
Neil Horman2f94aab2013-01-17 11:15:08 +0000286 __sctp_outq_teardown(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/* Put a new chunk in an sctp_outq. */
290int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
291{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000292 struct net *net = sock_net(q->asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int error = 0;
294
Daniel Borkmannbb333812013-06-28 19:49:40 +0200295 pr_debug("%s: outq:%p, chunk:%p[%s]\n", __func__, q, chunk,
296 chunk && chunk->chunk_hdr ?
297 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
298 "illegal chunk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* If it is data, queue it up, otherwise, send it
301 * immediately.
302 */
Shan Weiec7b9512010-04-30 22:41:09 -0400303 if (sctp_chunk_is_data(chunk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Is it OK to queue data chunks? */
305 /* From 9. Termination of Association
306 *
307 * When either endpoint performs a shutdown, the
308 * association on each peer will stop accepting new
309 * data from its user and only deliver data in queue
310 * at the time of sending or receiving the SHUTDOWN
311 * chunk.
312 */
313 switch (q->asoc->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case SCTP_STATE_CLOSED:
315 case SCTP_STATE_SHUTDOWN_PENDING:
316 case SCTP_STATE_SHUTDOWN_SENT:
317 case SCTP_STATE_SHUTDOWN_RECEIVED:
318 case SCTP_STATE_SHUTDOWN_ACK_SENT:
319 /* Cannot send after transport endpoint shutdown */
320 error = -ESHUTDOWN;
321 break;
322
323 default:
Daniel Borkmannbb333812013-06-28 19:49:40 +0200324 pr_debug("%s: outqueueing: outq:%p, chunk:%p[%s])\n",
325 __func__, q, chunk, chunk && chunk->chunk_hdr ?
326 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
327 "illegal chunk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 sctp_outq_tail_data(q, chunk);
330 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000331 SCTP_INC_STATS(net, SCTP_MIB_OUTUNORDERCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 else
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000333 SCTP_INC_STATS(net, SCTP_MIB_OUTORDERCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 q->empty = 0;
335 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 } else {
David S. Miller79af02c2005-07-08 21:47:49 -0700338 list_add_tail(&chunk->list, &q->control_chunk_list);
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000339 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 if (error < 0)
343 return error;
344
345 if (!q->cork)
346 error = sctp_outq_flush(q, 0);
347
348 return error;
349}
350
351/* Insert a chunk into the sorted list based on the TSNs. The retransmit list
352 * and the abandoned list are in ascending order.
353 */
354static void sctp_insert_list(struct list_head *head, struct list_head *new)
355{
356 struct list_head *pos;
357 struct sctp_chunk *nchunk, *lchunk;
358 __u32 ntsn, ltsn;
359 int done = 0;
360
361 nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
362 ntsn = ntohl(nchunk->subh.data_hdr->tsn);
363
364 list_for_each(pos, head) {
365 lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
366 ltsn = ntohl(lchunk->subh.data_hdr->tsn);
367 if (TSN_lt(ntsn, ltsn)) {
368 list_add(new, pos->prev);
369 done = 1;
370 break;
371 }
372 }
373 if (!done)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900374 list_add_tail(new, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/* Mark all the eligible packets on a transport for retransmission. */
378void sctp_retransmit_mark(struct sctp_outq *q,
379 struct sctp_transport *transport,
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400380 __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct list_head *lchunk, *ltemp;
383 struct sctp_chunk *chunk;
384
385 /* Walk through the specified transmitted queue. */
386 list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
387 chunk = list_entry(lchunk, struct sctp_chunk,
388 transmitted_list);
389
390 /* If the chunk is abandoned, move it to abandoned list. */
391 if (sctp_chunk_abandoned(chunk)) {
392 list_del_init(lchunk);
393 sctp_insert_list(&q->abandoned, lchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800394
395 /* If this chunk has not been previousely acked,
396 * stop considering it 'outstanding'. Our peer
397 * will most likely never see it since it will
398 * not be retransmitted
399 */
400 if (!chunk->tsn_gap_acked) {
Vlad Yasevich31b02e12009-09-04 18:21:00 -0400401 if (chunk->transport)
402 chunk->transport->flight_size -=
403 sctp_data_size(chunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800404 q->outstanding_bytes -= sctp_data_size(chunk);
Thomas Grafa76c0ad2011-12-19 04:11:40 +0000405 q->asoc->peer.rwnd += sctp_data_size(chunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 continue;
408 }
409
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400410 /* If we are doing retransmission due to a timeout or pmtu
411 * discovery, only the chunks that are not yet acked should
412 * be added to the retransmit queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400414 if ((reason == SCTP_RTXR_FAST_RTX &&
Neil Hormanc226ef92008-07-25 12:44:09 -0400415 (chunk->fast_retransmit == SCTP_NEED_FRTX)) ||
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400416 (reason != SCTP_RTXR_FAST_RTX && !chunk->tsn_gap_acked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* RFC 2960 6.2.1 Processing a Received SACK
418 *
419 * C) Any time a DATA chunk is marked for
420 * retransmission (via either T3-rtx timer expiration
421 * (Section 6.3.3) or via fast retransmit
422 * (Section 7.2.4)), add the data size of those
423 * chunks to the rwnd.
424 */
Thomas Grafa76c0ad2011-12-19 04:11:40 +0000425 q->asoc->peer.rwnd += sctp_data_size(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 q->outstanding_bytes -= sctp_data_size(chunk);
Vlad Yasevich31b02e12009-09-04 18:21:00 -0400427 if (chunk->transport)
428 transport->flight_size -= sctp_data_size(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* sctpimpguide-05 Section 2.8.2
431 * M5) If a T3-rtx timer expires, the
432 * 'TSN.Missing.Report' of all affected TSNs is set
433 * to 0.
434 */
435 chunk->tsn_missing_report = 0;
436
437 /* If a chunk that is being used for RTT measurement
438 * has to be retransmitted, we cannot use this chunk
439 * anymore for RTT measurements. Reset rto_pending so
440 * that a new RTT measurement is started when a new
441 * data chunk is sent.
442 */
443 if (chunk->rtt_in_progress) {
444 chunk->rtt_in_progress = 0;
445 transport->rto_pending = 0;
446 }
447
Xufeng Zhang6eabca52013-11-25 11:26:57 +0800448 chunk->resent = 1;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Move the chunk to the retransmit queue. The chunks
451 * on the retransmit queue are always kept in order.
452 */
453 list_del_init(lchunk);
454 sctp_insert_list(&q->retransmit, lchunk);
455 }
456 }
457
Daniel Borkmannbb333812013-06-28 19:49:40 +0200458 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d, "
459 "flight_size:%d, pba:%d\n", __func__, transport, reason,
460 transport->cwnd, transport->ssthresh, transport->flight_size,
461 transport->partial_bytes_acked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/* Mark all the eligible packets on a transport for retransmission and force
465 * one packet out.
466 */
467void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
468 sctp_retransmit_reason_t reason)
469{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000470 struct net *net = sock_net(q->asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
wangweidongcb3f8372013-12-23 12:16:50 +0800473 switch (reason) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 case SCTP_RTXR_T3_RTX:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000475 SCTP_INC_STATS(net, SCTP_MIB_T3_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
477 /* Update the retran path if the T3-rtx timer has expired for
478 * the current retran path.
479 */
480 if (transport == transport->asoc->peer.retran_path)
481 sctp_assoc_update_retran_path(transport->asoc);
Neil Horman58fbbed2008-02-29 11:40:56 -0800482 transport->asoc->rtx_data_chunks +=
483 transport->asoc->unack_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case SCTP_RTXR_FAST_RTX:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000486 SCTP_INC_STATS(net, SCTP_MIB_FAST_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700488 q->fast_rtx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case SCTP_RTXR_PMTUD:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000491 SCTP_INC_STATS(net, SCTP_MIB_PMTUD_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400493 case SCTP_RTXR_T1_RTX:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000494 SCTP_INC_STATS(net, SCTP_MIB_T1_RETRANSMITS);
Neil Horman58fbbed2008-02-29 11:40:56 -0800495 transport->asoc->init_retries++;
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400496 break;
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700497 default:
498 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400501 sctp_retransmit_mark(q, transport, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
504 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
505 * following the procedures outlined in C1 - C5.
506 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700507 if (reason == SCTP_RTXR_T3_RTX)
508 sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700510 /* Flush the queues only on timeout, since fast_rtx is only
511 * triggered during sack processing and the queue
512 * will be flushed at the end.
513 */
514 if (reason != SCTP_RTXR_FAST_RTX)
515 error = sctp_outq_flush(q, /* rtx_timeout */ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (error)
518 q->asoc->base.sk->sk_err = -error;
519}
520
521/*
522 * Transmit DATA chunks on the retransmit queue. Upon return from
523 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
524 * need to be transmitted by the caller.
525 * We assume that pkt->transport has already been set.
526 *
527 * The return value is a normal kernel error return value.
528 */
529static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
530 int rtx_timeout, int *start_timer)
531{
532 struct list_head *lqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct sctp_transport *transport = pkt->transport;
534 sctp_xmit_t status;
535 struct sctp_chunk *chunk, *chunk1;
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700536 int fast_rtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int error = 0;
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700538 int timer = 0;
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700539 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 lqueue = &q->retransmit;
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700542 fast_rtx = q->fast_rtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700544 /* This loop handles time-out retransmissions, fast retransmissions,
545 * and retransmissions due to opening of whindow.
546 *
547 * RFC 2960 6.3.3 Handle T3-rtx Expiration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *
549 * E3) Determine how many of the earliest (i.e., lowest TSN)
550 * outstanding DATA chunks for the address for which the
551 * T3-rtx has expired will fit into a single packet, subject
552 * to the MTU constraint for the path corresponding to the
553 * destination transport address to which the retransmission
554 * is being sent (this may be different from the address for
555 * which the timer expires [see Section 6.4]). Call this value
556 * K. Bundle and retransmit those K DATA chunks in a single
557 * packet to the destination endpoint.
558 *
559 * [Just to be painfully clear, if we are retransmitting
560 * because a timeout just happened, we should send only ONE
561 * packet of retransmitted data.]
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700562 *
563 * For fast retransmissions we also send only ONE packet. However,
564 * if we are just flushing the queue due to open window, we'll
565 * try to send as much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700567 list_for_each_entry_safe(chunk, chunk1, lqueue, transmitted_list) {
Wei Yongjun4c6a6f422011-04-19 21:32:28 +0000568 /* If the chunk is abandoned, move it to abandoned list. */
569 if (sctp_chunk_abandoned(chunk)) {
570 list_del_init(&chunk->transmitted_list);
571 sctp_insert_list(&q->abandoned,
572 &chunk->transmitted_list);
573 continue;
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* Make sure that Gap Acked TSNs are not retransmitted. A
577 * simple approach is just to move such TSNs out of the
578 * way and into a 'transmitted' queue and skip to the
579 * next chunk.
580 */
581 if (chunk->tsn_gap_acked) {
Wei Yongjun54a27922012-09-03 23:58:16 +0000582 list_move_tail(&chunk->transmitted_list,
583 &transport->transmitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 continue;
585 }
586
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700587 /* If we are doing fast retransmit, ignore non-fast_rtransmit
588 * chunks
589 */
590 if (fast_rtx && !chunk->fast_retransmit)
591 continue;
592
Wei Yongjunbc4f8412010-04-30 22:38:53 -0400593redo:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* Attempt to append this chunk to the packet. */
595 status = sctp_packet_append_chunk(pkt, chunk);
596
597 switch (status) {
598 case SCTP_XMIT_PMTU_FULL:
Wei Yongjunbc4f8412010-04-30 22:38:53 -0400599 if (!pkt->has_data && !pkt->has_cookie_echo) {
600 /* If this packet did not contain DATA then
601 * retransmission did not happen, so do it
602 * again. We'll ignore the error here since
603 * control chunks are already freed so there
604 * is nothing we can do.
605 */
606 sctp_packet_transmit(pkt);
607 goto redo;
608 }
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Send this packet. */
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700611 error = sctp_packet_transmit(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* If we are retransmitting, we should only
614 * send a single packet.
Vlad Yasevichf246a7b2011-04-18 19:13:56 +0000615 * Otherwise, try appending this chunk again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700617 if (rtx_timeout || fast_rtx)
618 done = 1;
Vlad Yasevichf246a7b2011-04-18 19:13:56 +0000619 else
620 goto redo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700622 /* Bundle next chunk in the next round. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624
625 case SCTP_XMIT_RWND_FULL:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900626 /* Send this packet. */
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700627 error = sctp_packet_transmit(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* Stop sending DATA as there is no more room
630 * at the receiver.
631 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700632 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634
635 case SCTP_XMIT_NAGLE_DELAY:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900636 /* Send this packet. */
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700637 error = sctp_packet_transmit(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* Stop sending DATA because of nagle delay. */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700640 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642
643 default:
644 /* The append was successful, so add this chunk to
645 * the transmitted list.
646 */
Wei Yongjun54a27922012-09-03 23:58:16 +0000647 list_move_tail(&chunk->transmitted_list,
648 &transport->transmitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900650 /* Mark the chunk as ineligible for fast retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * after it is retransmitted.
652 */
Neil Hormanc226ef92008-07-25 12:44:09 -0400653 if (chunk->fast_retransmit == SCTP_NEED_FRTX)
654 chunk->fast_retransmit = SCTP_DONT_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 q->empty = 0;
Michele Baldessari196d6752012-12-01 04:49:42 +0000657 q->asoc->stats.rtxchunks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700661 /* Set the timer if there were no errors */
662 if (!error && !timer)
663 timer = 1;
664
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700665 if (done)
666 break;
667 }
668
669 /* If we are here due to a retransmit timeout or a fast
670 * retransmit and if there are any chunks left in the retransmit
671 * queue that could not fit in the PMTU sized packet, they need
672 * to be marked as ineligible for a subsequent fast retransmit.
673 */
674 if (rtx_timeout || fast_rtx) {
675 list_for_each_entry(chunk1, lqueue, transmitted_list) {
Neil Hormanc226ef92008-07-25 12:44:09 -0400676 if (chunk1->fast_retransmit == SCTP_NEED_FRTX)
677 chunk1->fast_retransmit = SCTP_DONT_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 }
680
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700681 *start_timer = timer;
682
683 /* Clear fast retransmit hint */
684 if (fast_rtx)
685 q->fast_rtx = 0;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return error;
688}
689
690/* Cork the outqueue so queued chunks are really queued. */
691int sctp_outq_uncork(struct sctp_outq *q)
692{
Vlad Yasevich7d54dc62007-11-09 11:43:41 -0500693 if (q->cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 q->cork = 0;
Daniel Borkmanndacda322013-04-16 11:07:14 +0000695
696 return sctp_outq_flush(q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700/*
701 * Try to flush an outqueue.
702 *
703 * Description: Send everything in q which we legally can, subject to
704 * congestion limitations.
705 * * Note: This function can be called from multiple contexts so appropriate
706 * locking concerns must be made. Today we use the sock lock to protect
707 * this function.
708 */
Adrian Bunkabd0b1982008-07-22 14:20:45 -0700709static int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct sctp_packet *packet;
712 struct sctp_packet singleton;
713 struct sctp_association *asoc = q->asoc;
714 __u16 sport = asoc->base.bind_addr.port;
715 __u16 dport = asoc->peer.port;
716 __u32 vtag = asoc->peer.i.init_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct sctp_transport *transport = NULL;
718 struct sctp_transport *new_transport;
David S. Miller79af02c2005-07-08 21:47:49 -0700719 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sctp_xmit_t status;
721 int error = 0;
722 int start_timer = 0;
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700723 int one_packet = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* These transports have chunks to send. */
726 struct list_head transport_list;
727 struct list_head *ltransport;
728
729 INIT_LIST_HEAD(&transport_list);
730 packet = NULL;
731
732 /*
733 * 6.10 Bundling
734 * ...
735 * When bundling control chunks with DATA chunks, an
736 * endpoint MUST place control chunks first in the outbound
737 * SCTP packet. The transmitter MUST transmit DATA chunks
738 * within a SCTP packet in increasing order of TSN.
739 * ...
740 */
741
David S. Miller79af02c2005-07-08 21:47:49 -0700742 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
Michio Honda8a07eb02011-04-26 20:19:36 +0900743 /* RFC 5061, 5.3
744 * F1) This means that until such time as the ASCONF
745 * containing the add is acknowledged, the sender MUST
746 * NOT use the new IP address as a source for ANY SCTP
747 * packet except on carrying an ASCONF Chunk.
748 */
749 if (asoc->src_out_of_asoc_ok &&
750 chunk->chunk_hdr->type != SCTP_CID_ASCONF)
751 continue;
752
David S. Miller79af02c2005-07-08 21:47:49 -0700753 list_del_init(&chunk->list);
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* Pick the right transport to use. */
756 new_transport = chunk->transport;
757
758 if (!new_transport) {
Vlad Yasevicha08de642007-12-20 14:11:47 -0800759 /*
760 * If we have a prior transport pointer, see if
761 * the destination address of the chunk
762 * matches the destination address of the
763 * current transport. If not a match, then
764 * try to look up the transport with a given
765 * destination address. We do this because
766 * after processing ASCONFs, we may have new
767 * transports created.
768 */
769 if (transport &&
770 sctp_cmp_addr_exact(&chunk->dest,
771 &transport->ipaddr))
772 new_transport = transport;
773 else
774 new_transport = sctp_assoc_lookup_paddr(asoc,
775 &chunk->dest);
776
777 /* if we still don't have a new transport, then
778 * use the current active path.
779 */
780 if (!new_transport)
781 new_transport = asoc->peer.active_path;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700782 } else if ((new_transport->state == SCTP_INACTIVE) ||
Neil Horman5aa93bc2012-07-21 07:56:07 +0000783 (new_transport->state == SCTP_UNCONFIRMED) ||
784 (new_transport->state == SCTP_PF)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -0700785 /* If the chunk is Heartbeat or Heartbeat Ack,
786 * send it to chunk->transport, even if it's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * inactive.
788 *
789 * 3.3.6 Heartbeat Acknowledgement:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900790 * ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * A HEARTBEAT ACK is always sent to the source IP
792 * address of the IP datagram containing the
793 * HEARTBEAT chunk to which this ack is responding.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900794 * ...
Vlad Yasevicha08de642007-12-20 14:11:47 -0800795 *
796 * ASCONF_ACKs also must be sent to the source.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
798 if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
Vlad Yasevicha08de642007-12-20 14:11:47 -0800799 chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK &&
800 chunk->chunk_hdr->type != SCTP_CID_ASCONF_ACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 new_transport = asoc->peer.active_path;
802 }
803
804 /* Are we switching transports?
805 * Take care of transport locks.
806 */
807 if (new_transport != transport) {
808 transport = new_transport;
809 if (list_empty(&transport->send_ready)) {
810 list_add_tail(&transport->send_ready,
811 &transport_list);
812 }
813 packet = &transport->packet;
814 sctp_packet_config(packet, vtag,
815 asoc->peer.ecn_capable);
816 }
817
818 switch (chunk->chunk_hdr->type) {
819 /*
820 * 6.10 Bundling
821 * ...
822 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
823 * COMPLETE with any other chunks. [Send them immediately.]
824 */
825 case SCTP_CID_INIT:
826 case SCTP_CID_INIT_ACK:
827 case SCTP_CID_SHUTDOWN_COMPLETE:
828 sctp_packet_init(&singleton, transport, sport, dport);
829 sctp_packet_config(&singleton, vtag, 0);
830 sctp_packet_append_chunk(&singleton, chunk);
831 error = sctp_packet_transmit(&singleton);
832 if (error < 0)
833 return error;
834 break;
835
836 case SCTP_CID_ABORT:
Gui Jianfengf4ad85c2008-04-12 18:39:34 -0700837 if (sctp_test_T_bit(chunk)) {
838 packet->vtag = asoc->c.my_vtag;
839 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700840 /* The following chunks are "response" chunks, i.e.
841 * they are generated in response to something we
842 * received. If we are sending these, then we can
843 * send only 1 packet containing these chunks.
844 */
845 case SCTP_CID_HEARTBEAT_ACK:
846 case SCTP_CID_SHUTDOWN_ACK:
847 case SCTP_CID_COOKIE_ACK:
848 case SCTP_CID_COOKIE_ECHO:
849 case SCTP_CID_ERROR:
850 case SCTP_CID_ECN_CWR:
851 case SCTP_CID_ASCONF_ACK:
852 one_packet = 1;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300853 /* Fall through */
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 case SCTP_CID_SACK:
856 case SCTP_CID_HEARTBEAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 case SCTP_CID_SHUTDOWN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case SCTP_CID_ECN_ECNE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 case SCTP_CID_ASCONF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 case SCTP_CID_FWD_TSN:
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700861 status = sctp_packet_transmit_chunk(packet, chunk,
862 one_packet);
863 if (status != SCTP_XMIT_OK) {
864 /* put the chunk back */
865 list_add(&chunk->list, &q->control_chunk_list);
Michele Baldessari196d6752012-12-01 04:49:42 +0000866 } else {
867 asoc->stats.octrlchunks++;
Wei Yongjunbd69b982010-04-30 21:42:43 -0400868 /* PR-SCTP C5) If a FORWARD TSN is sent, the
869 * sender MUST assure that at least one T3-rtx
870 * timer is running.
871 */
Michele Baldessari196d6752012-12-01 04:49:42 +0000872 if (chunk->chunk_hdr->type == SCTP_CID_FWD_TSN)
873 sctp_transport_reset_timers(transport);
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876
877 default:
878 /* We built a chunk with an illegal type! */
879 BUG();
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
Michio Honda8a07eb02011-04-26 20:19:36 +0900883 if (q->asoc->src_out_of_asoc_ok)
884 goto sctp_flush_out;
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* Is it OK to send data chunks? */
887 switch (asoc->state) {
888 case SCTP_STATE_COOKIE_ECHOED:
889 /* Only allow bundling when this packet has a COOKIE-ECHO
890 * chunk.
891 */
892 if (!packet || !packet->has_cookie_echo)
893 break;
894
895 /* fallthru */
896 case SCTP_STATE_ESTABLISHED:
897 case SCTP_STATE_SHUTDOWN_PENDING:
898 case SCTP_STATE_SHUTDOWN_RECEIVED:
899 /*
900 * RFC 2960 6.1 Transmission of DATA Chunks
901 *
902 * C) When the time comes for the sender to transmit,
903 * before sending new DATA chunks, the sender MUST
904 * first transmit any outstanding DATA chunks which
905 * are marked for retransmission (limited by the
906 * current cwnd).
907 */
908 if (!list_empty(&q->retransmit)) {
Michio Hondaf207c052011-06-16 10:54:23 +0900909 if (asoc->peer.retran_path->state == SCTP_UNCONFIRMED)
910 goto sctp_flush_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (transport == asoc->peer.retran_path)
912 goto retran;
913
914 /* Switch transports & prepare the packet. */
915
916 transport = asoc->peer.retran_path;
917
918 if (list_empty(&transport->send_ready)) {
919 list_add_tail(&transport->send_ready,
920 &transport_list);
921 }
922
923 packet = &transport->packet;
924 sctp_packet_config(packet, vtag,
925 asoc->peer.ecn_capable);
926 retran:
927 error = sctp_outq_flush_rtx(q, packet,
928 rtx_timeout, &start_timer);
929
930 if (start_timer)
Vlad Yasevichd9efc222010-04-30 22:41:09 -0400931 sctp_transport_reset_timers(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* This can happen on COOKIE-ECHO resend. Only
934 * one chunk can get bundled with a COOKIE-ECHO.
935 */
936 if (packet->has_cookie_echo)
937 goto sctp_flush_out;
938
939 /* Don't send new data if there is still data
940 * waiting to retransmit.
941 */
942 if (!list_empty(&q->retransmit))
943 goto sctp_flush_out;
944 }
945
Vlad Yasevich46d5a802009-11-23 15:54:00 -0500946 /* Apply Max.Burst limitation to the current transport in
947 * case it will be used for new data. We are going to
948 * rest it before we return, but we want to apply the limit
949 * to the currently queued data.
950 */
951 if (transport)
952 sctp_transport_burst_limited(transport);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Finally, transmit new packets. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
956 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
957 * stream identifier.
958 */
959 if (chunk->sinfo.sinfo_stream >=
960 asoc->c.sinit_num_ostreams) {
961
962 /* Mark as failed send. */
963 sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
964 sctp_chunk_free(chunk);
965 continue;
966 }
967
968 /* Has this chunk expired? */
969 if (sctp_chunk_abandoned(chunk)) {
970 sctp_chunk_fail(chunk, 0);
971 sctp_chunk_free(chunk);
972 continue;
973 }
974
975 /* If there is a specified transport, use it.
976 * Otherwise, we want to use the active path.
977 */
978 new_transport = chunk->transport;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700979 if (!new_transport ||
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700980 ((new_transport->state == SCTP_INACTIVE) ||
Neil Horman5aa93bc2012-07-21 07:56:07 +0000981 (new_transport->state == SCTP_UNCONFIRMED) ||
982 (new_transport->state == SCTP_PF)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 new_transport = asoc->peer.active_path;
Michio Hondaf207c052011-06-16 10:54:23 +0900984 if (new_transport->state == SCTP_UNCONFIRMED)
985 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Change packets if necessary. */
988 if (new_transport != transport) {
989 transport = new_transport;
990
991 /* Schedule to have this transport's
992 * packet flushed.
993 */
994 if (list_empty(&transport->send_ready)) {
995 list_add_tail(&transport->send_ready,
996 &transport_list);
997 }
998
999 packet = &transport->packet;
1000 sctp_packet_config(packet, vtag,
1001 asoc->peer.ecn_capable);
Vlad Yasevich46d5a802009-11-23 15:54:00 -05001002 /* We've switched transports, so apply the
1003 * Burst limit to the new transport.
1004 */
1005 sctp_transport_burst_limited(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Daniel Borkmannbb333812013-06-28 19:49:40 +02001008 pr_debug("%s: outq:%p, chunk:%p[%s], tx-tsn:0x%x skb->head:%p "
1009 "skb->users:%d\n",
1010 __func__, q, chunk, chunk && chunk->chunk_hdr ?
1011 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
1012 "illegal chunk", ntohl(chunk->subh.data_hdr->tsn),
1013 chunk->skb ? chunk->skb->head : NULL, chunk->skb ?
1014 atomic_read(&chunk->skb->users) : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /* Add the chunk to the packet. */
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001017 status = sctp_packet_transmit_chunk(packet, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 switch (status) {
1020 case SCTP_XMIT_PMTU_FULL:
1021 case SCTP_XMIT_RWND_FULL:
1022 case SCTP_XMIT_NAGLE_DELAY:
1023 /* We could not append this chunk, so put
1024 * the chunk back on the output queue.
1025 */
Daniel Borkmannbb333812013-06-28 19:49:40 +02001026 pr_debug("%s: could not transmit tsn:0x%x, status:%d\n",
1027 __func__, ntohl(chunk->subh.data_hdr->tsn),
1028 status);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 sctp_outq_head_data(q, chunk);
1031 goto sctp_flush_out;
1032 break;
1033
1034 case SCTP_XMIT_OK:
Wei Yongjunb93d6472009-11-23 15:53:56 -05001035 /* The sender is in the SHUTDOWN-PENDING state,
1036 * The sender MAY set the I-bit in the DATA
1037 * chunk header.
1038 */
1039 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING)
1040 chunk->chunk_hdr->flags |= SCTP_DATA_SACK_IMM;
Michele Baldessari196d6752012-12-01 04:49:42 +00001041 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
1042 asoc->stats.ouodchunks++;
1043 else
1044 asoc->stats.oodchunks++;
Wei Yongjunb93d6472009-11-23 15:53:56 -05001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047
1048 default:
1049 BUG();
1050 }
1051
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001052 /* BUG: We assume that the sctp_packet_transmit()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 * call below will succeed all the time and add the
1054 * chunk to the transmitted list and restart the
1055 * timers.
1056 * It is possible that the call can fail under OOM
1057 * conditions.
1058 *
1059 * Is this really a problem? Won't this behave
1060 * like a lost TSN?
1061 */
1062 list_add_tail(&chunk->transmitted_list,
1063 &transport->transmitted);
1064
Vlad Yasevichd9efc222010-04-30 22:41:09 -04001065 sctp_transport_reset_timers(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 q->empty = 0;
1068
1069 /* Only let one DATA chunk get bundled with a
1070 * COOKIE-ECHO chunk.
1071 */
1072 if (packet->has_cookie_echo)
1073 goto sctp_flush_out;
1074 }
1075 break;
1076
1077 default:
1078 /* Do nothing. */
1079 break;
1080 }
1081
1082sctp_flush_out:
1083
1084 /* Before returning, examine all the transports touched in
1085 * this call. Right now, we bluntly force clear all the
1086 * transports. Things might change after we implement Nagle.
1087 * But such an examination is still required.
1088 *
1089 * --xguo
1090 */
wangweidongcb3f8372013-12-23 12:16:50 +08001091 while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct sctp_transport *t = list_entry(ltransport,
1093 struct sctp_transport,
1094 send_ready);
1095 packet = &t->packet;
1096 if (!sctp_packet_empty(packet))
1097 error = sctp_packet_transmit(packet);
Vlad Yasevich46d5a802009-11-23 15:54:00 -05001098
1099 /* Clear the burst limited state, if any */
1100 sctp_transport_burst_reset(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
1103 return error;
1104}
1105
1106/* Update unack_data based on the incoming SACK chunk */
1107static void sctp_sack_update_unack_data(struct sctp_association *assoc,
1108 struct sctp_sackhdr *sack)
1109{
1110 sctp_sack_variable_t *frags;
1111 __u16 unack_data;
1112 int i;
1113
1114 unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
1115
1116 frags = sack->variable;
1117 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
1118 unack_data -= ((ntohs(frags[i].gab.end) -
1119 ntohs(frags[i].gab.start) + 1));
1120 }
1121
1122 assoc->unack_data = unack_data;
1123}
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/* This is where we REALLY process a SACK.
1126 *
1127 * Process the SACK against the outqueue. Mostly, this just frees
1128 * things off the transmitted queue.
1129 */
Nicolas Dichteledfee032012-10-03 05:43:22 +00001130int sctp_outq_sack(struct sctp_outq *q, struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct sctp_association *asoc = q->asoc;
Nicolas Dichteledfee032012-10-03 05:43:22 +00001133 struct sctp_sackhdr *sack = chunk->subh.sack_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct sctp_transport *transport;
1135 struct sctp_chunk *tchunk = NULL;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001136 struct list_head *lchunk, *transport_list, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 sctp_sack_variable_t *frags = sack->variable;
1138 __u32 sack_ctsn, ctsn, tsn;
1139 __u32 highest_tsn, highest_new_tsn;
1140 __u32 sack_a_rwnd;
Eric Dumazet95c96172012-04-15 05:58:06 +00001141 unsigned int outstanding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct sctp_transport *primary = asoc->peer.primary_path;
1143 int count_of_newacks = 0;
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001144 int gap_ack_blocks;
Vlad Yasevichea862c82010-04-30 22:41:10 -04001145 u8 accum_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* Grab the association's destination address list. */
1148 transport_list = &asoc->peer.transport_addr_list;
1149
1150 sack_ctsn = ntohl(sack->cum_tsn_ack);
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001151 gap_ack_blocks = ntohs(sack->num_gap_ack_blocks);
Michele Baldessari196d6752012-12-01 04:49:42 +00001152 asoc->stats.gapcnt += gap_ack_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * SFR-CACC algorithm:
1155 * On receipt of a SACK the sender SHOULD execute the
1156 * following statements.
1157 *
1158 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1159 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1160 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1161 * all destinations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1163 * is set the receiver of the SACK MUST take the following actions:
1164 *
1165 * A) Initialize the cacc_saw_newack to 0 for all destination
1166 * addresses.
Vlad Yasevichab5216a2008-06-19 18:17:24 -04001167 *
1168 * Only bother if changeover_active is set. Otherwise, this is
1169 * totally suboptimal to do on every SACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Vlad Yasevichab5216a2008-06-19 18:17:24 -04001171 if (primary->cacc.changeover_active) {
1172 u8 clear_cycling = 0;
1173
1174 if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1175 primary->cacc.changeover_active = 0;
1176 clear_cycling = 1;
1177 }
1178
1179 if (clear_cycling || gap_ack_blocks) {
1180 list_for_each_entry(transport, transport_list,
1181 transports) {
1182 if (clear_cycling)
1183 transport->cacc.cycling_changeover = 0;
1184 if (gap_ack_blocks)
1185 transport->cacc.cacc_saw_newack = 0;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 }
1189
1190 /* Get the highest TSN in the sack. */
1191 highest_tsn = sack_ctsn;
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001192 if (gap_ack_blocks)
1193 highest_tsn += ntohs(frags[gap_ack_blocks - 1].gab.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Vlad Yasevichbfa0d982010-04-30 22:41:10 -04001195 if (TSN_lt(asoc->highest_sacked, highest_tsn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 asoc->highest_sacked = highest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Vlad Yasevichbfa0d982010-04-30 22:41:10 -04001198 highest_new_tsn = sack_ctsn;
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* Run through the retransmit queue. Credit bytes received
1201 * and free those chunks that we can.
1202 */
Nicolas Dichteledfee032012-10-03 05:43:22 +00001203 sctp_check_transmitted(q, &q->retransmit, NULL, NULL, sack, &highest_new_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* Run through the transmitted queue.
1206 * Credit bytes received and free those chunks which we can.
1207 *
1208 * This is a MASSIVE candidate for optimization.
1209 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001210 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 sctp_check_transmitted(q, &transport->transmitted,
Nicolas Dichteledfee032012-10-03 05:43:22 +00001212 transport, &chunk->source, sack,
1213 &highest_new_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /*
1215 * SFR-CACC algorithm:
1216 * C) Let count_of_newacks be the number of
1217 * destinations for which cacc_saw_newack is set.
1218 */
1219 if (transport->cacc.cacc_saw_newack)
wangweidongcb3f8372013-12-23 12:16:50 +08001220 count_of_newacks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
Vlad Yasevichea862c82010-04-30 22:41:10 -04001223 /* Move the Cumulative TSN Ack Point if appropriate. */
1224 if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn)) {
1225 asoc->ctsn_ack_point = sack_ctsn;
1226 accum_moved = 1;
1227 }
1228
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001229 if (gap_ack_blocks) {
Vlad Yasevichea862c82010-04-30 22:41:10 -04001230
1231 if (asoc->fast_recovery && accum_moved)
1232 highest_new_tsn = highest_tsn;
1233
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001234 list_for_each_entry(transport, transport_list, transports)
1235 sctp_mark_missing(q, &transport->transmitted, transport,
1236 highest_new_tsn, count_of_newacks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* Update unack_data field in the assoc. */
1240 sctp_sack_update_unack_data(asoc, sack);
1241
1242 ctsn = asoc->ctsn_ack_point;
1243
1244 /* Throw away stuff rotting on the sack queue. */
1245 list_for_each_safe(lchunk, temp, &q->sacked) {
1246 tchunk = list_entry(lchunk, struct sctp_chunk,
1247 transmitted_list);
1248 tsn = ntohl(tchunk->subh.data_hdr->tsn);
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05001249 if (TSN_lte(tsn, ctsn)) {
1250 list_del_init(&tchunk->transmitted_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 sctp_chunk_free(tchunk);
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
1255 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1256 * number of bytes still outstanding after processing the
1257 * Cumulative TSN Ack and the Gap Ack Blocks.
1258 */
1259
1260 sack_a_rwnd = ntohl(sack->a_rwnd);
1261 outstanding = q->outstanding_bytes;
1262
1263 if (outstanding < sack_a_rwnd)
1264 sack_a_rwnd -= outstanding;
1265 else
1266 sack_a_rwnd = 0;
1267
1268 asoc->peer.rwnd = sack_a_rwnd;
1269
1270 sctp_generate_fwdtsn(q, sack_ctsn);
1271
Daniel Borkmannbb333812013-06-28 19:49:40 +02001272 pr_debug("%s: sack cumulative tsn ack:0x%x\n", __func__, sack_ctsn);
1273 pr_debug("%s: cumulative tsn ack of assoc:%p is 0x%x, "
1274 "advertised peer ack point:0x%x\n", __func__, asoc, ctsn,
1275 asoc->adv_peer_ack_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /* See if all chunks are acked.
1278 * Make sure the empty queue handler will get run later.
1279 */
David S. Miller79af02c2005-07-08 21:47:49 -07001280 q->empty = (list_empty(&q->out_chunk_list) &&
David S. Miller79af02c2005-07-08 21:47:49 -07001281 list_empty(&q->retransmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!q->empty)
1283 goto finish;
1284
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001285 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 q->empty = q->empty && list_empty(&transport->transmitted);
1287 if (!q->empty)
1288 goto finish;
1289 }
1290
Daniel Borkmannbb333812013-06-28 19:49:40 +02001291 pr_debug("%s: sack queue is empty\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292finish:
1293 return q->empty;
1294}
1295
1296/* Is the outqueue empty? */
1297int sctp_outq_is_empty(const struct sctp_outq *q)
1298{
1299 return q->empty;
1300}
1301
1302/********************************************************************
1303 * 2nd Level Abstractions
1304 ********************************************************************/
1305
1306/* Go through a transport's transmitted list or the association's retransmit
1307 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1308 * The retransmit list will not have an associated transport.
1309 *
1310 * I added coherent debug information output. --xguo
1311 *
1312 * Instead of printing 'sacked' or 'kept' for each TSN on the
1313 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1314 * KEPT TSN6-TSN7, etc.
1315 */
1316static void sctp_check_transmitted(struct sctp_outq *q,
1317 struct list_head *transmitted_queue,
1318 struct sctp_transport *transport,
Nicolas Dichteledfee032012-10-03 05:43:22 +00001319 union sctp_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct sctp_sackhdr *sack,
Vlad Yasevichbfa0d982010-04-30 22:41:10 -04001321 __u32 *highest_new_tsn_in_sack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct list_head *lchunk;
1324 struct sctp_chunk *tchunk;
1325 struct list_head tlist;
1326 __u32 tsn;
1327 __u32 sack_ctsn;
1328 __u32 rtt;
1329 __u8 restart_timer = 0;
1330 int bytes_acked = 0;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001331 int migrate_bytes = 0;
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001332 bool forward_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 sack_ctsn = ntohl(sack->cum_tsn_ack);
1335
1336 INIT_LIST_HEAD(&tlist);
1337
1338 /* The while loop will skip empty transmitted queues. */
1339 while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1340 tchunk = list_entry(lchunk, struct sctp_chunk,
1341 transmitted_list);
1342
1343 if (sctp_chunk_abandoned(tchunk)) {
1344 /* Move the chunk to abandoned list. */
1345 sctp_insert_list(&q->abandoned, lchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -08001346
1347 /* If this chunk has not been acked, stop
1348 * considering it as 'outstanding'.
1349 */
1350 if (!tchunk->tsn_gap_acked) {
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001351 if (tchunk->transport)
1352 tchunk->transport->flight_size -=
1353 sctp_data_size(tchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -08001354 q->outstanding_bytes -= sctp_data_size(tchunk);
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 continue;
1357 }
1358
1359 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1360 if (sctp_acked(sack, tsn)) {
1361 /* If this queue is the retransmit queue, the
1362 * retransmit timer has already reclaimed
1363 * the outstanding bytes for this chunk, so only
1364 * count bytes associated with a transport.
1365 */
1366 if (transport) {
1367 /* If this chunk is being used for RTT
1368 * measurement, calculate the RTT and update
1369 * the RTO using this value.
1370 *
1371 * 6.3.1 C5) Karn's algorithm: RTT measurements
1372 * MUST NOT be made using packets that were
1373 * retransmitted (and thus for which it is
1374 * ambiguous whether the reply was for the
1375 * first instance of the packet or a later
1376 * instance).
1377 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001378 if (!tchunk->tsn_gap_acked &&
Xufeng Zhang6eabca52013-11-25 11:26:57 +08001379 !tchunk->resent &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 tchunk->rtt_in_progress) {
Vlad Yasevich4c9f5d52006-06-17 22:56:08 -07001381 tchunk->rtt_in_progress = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 rtt = jiffies - tchunk->sent_at;
1383 sctp_transport_update_rto(transport,
1384 rtt);
1385 }
1386 }
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001387
1388 /* If the chunk hasn't been marked as ACKED,
1389 * mark it and account bytes_acked if the
1390 * chunk had a valid transport (it will not
1391 * have a transport if ASCONF had deleted it
1392 * while DATA was outstanding).
1393 */
1394 if (!tchunk->tsn_gap_acked) {
1395 tchunk->tsn_gap_acked = 1;
Chang Xiangzhongd6c41612013-11-21 22:56:28 +01001396 if (TSN_lt(*highest_new_tsn_in_sack, tsn))
1397 *highest_new_tsn_in_sack = tsn;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001398 bytes_acked += sctp_data_size(tchunk);
1399 if (!tchunk->transport)
1400 migrate_bytes += sctp_data_size(tchunk);
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001401 forward_progress = true;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001402 }
1403
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001404 if (TSN_lte(tsn, sack_ctsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* RFC 2960 6.3.2 Retransmission Timer Rules
1406 *
1407 * R3) Whenever a SACK is received
1408 * that acknowledges the DATA chunk
1409 * with the earliest outstanding TSN
1410 * for that address, restart T3-rtx
1411 * timer for that address with its
1412 * current RTO.
1413 */
1414 restart_timer = 1;
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001415 forward_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (!tchunk->tsn_gap_acked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * SFR-CACC algorithm:
1420 * 2) If the SACK contains gap acks
1421 * and the flag CHANGEOVER_ACTIVE is
1422 * set the receiver of the SACK MUST
1423 * take the following action:
1424 *
1425 * B) For each TSN t being acked that
1426 * has not been acked in any SACK so
1427 * far, set cacc_saw_newack to 1 for
1428 * the destination that the TSN was
1429 * sent to.
1430 */
1431 if (transport &&
1432 sack->num_gap_ack_blocks &&
1433 q->asoc->peer.primary_path->cacc.
1434 changeover_active)
1435 transport->cacc.cacc_saw_newack
1436 = 1;
1437 }
1438
1439 list_add_tail(&tchunk->transmitted_list,
1440 &q->sacked);
1441 } else {
1442 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1443 * M2) Each time a SACK arrives reporting
1444 * 'Stray DATA chunk(s)' record the highest TSN
1445 * reported as newly acknowledged, call this
1446 * value 'HighestTSNinSack'. A newly
1447 * acknowledged DATA chunk is one not
1448 * previously acknowledged in a SACK.
1449 *
1450 * When the SCTP sender of data receives a SACK
1451 * chunk that acknowledges, for the first time,
1452 * the receipt of a DATA chunk, all the still
1453 * unacknowledged DATA chunks whose TSN is
1454 * older than that newly acknowledged DATA
1455 * chunk, are qualified as 'Stray DATA chunks'.
1456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 list_add_tail(lchunk, &tlist);
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 } else {
1460 if (tchunk->tsn_gap_acked) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001461 pr_debug("%s: receiver reneged on data TSN:0x%x\n",
1462 __func__, tsn);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 tchunk->tsn_gap_acked = 0;
1465
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001466 if (tchunk->transport)
1467 bytes_acked -= sctp_data_size(tchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /* RFC 2960 6.3.2 Retransmission Timer Rules
1470 *
1471 * R4) Whenever a SACK is received missing a
1472 * TSN that was previously acknowledged via a
1473 * Gap Ack Block, start T3-rtx for the
1474 * destination address to which the DATA
1475 * chunk was originally
1476 * transmitted if it is not already running.
1477 */
1478 restart_timer = 1;
1479 }
1480
1481 list_add_tail(lchunk, &tlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483 }
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (transport) {
1486 if (bytes_acked) {
Thomas Graff8d96052011-07-07 00:28:35 +00001487 struct sctp_association *asoc = transport->asoc;
1488
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001489 /* We may have counted DATA that was migrated
1490 * to this transport due to DEL-IP operation.
1491 * Subtract those bytes, since the were never
1492 * send on this transport and shouldn't be
1493 * credited to this transport.
1494 */
1495 bytes_acked -= migrate_bytes;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* 8.2. When an outstanding TSN is acknowledged,
1498 * the endpoint shall clear the error counter of
1499 * the destination transport address to which the
1500 * DATA chunk was last sent.
1501 * The association's overall error counter is
1502 * also cleared.
1503 */
1504 transport->error_count = 0;
1505 transport->asoc->overall_error_count = 0;
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001506 forward_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Thomas Graff8d96052011-07-07 00:28:35 +00001508 /*
1509 * While in SHUTDOWN PENDING, we may have started
1510 * the T5 shutdown guard timer after reaching the
1511 * retransmission limit. Stop that timer as soon
1512 * as the receiver acknowledged any data.
1513 */
1514 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING &&
1515 del_timer(&asoc->timers
1516 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]))
1517 sctp_association_put(asoc);
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /* Mark the destination transport address as
1520 * active if it is not so marked.
1521 */
Nicolas Dichteledfee032012-10-03 05:43:22 +00001522 if ((transport->state == SCTP_INACTIVE ||
1523 transport->state == SCTP_UNCONFIRMED) &&
1524 sctp_cmp_addr_exact(&transport->ipaddr, saddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 sctp_assoc_control_transport(
1526 transport->asoc,
1527 transport,
1528 SCTP_TRANSPORT_UP,
1529 SCTP_RECEIVED_SACK);
1530 }
1531
1532 sctp_transport_raise_cwnd(transport, sack_ctsn,
1533 bytes_acked);
1534
1535 transport->flight_size -= bytes_acked;
Gui Jianfeng8b73a072008-04-17 14:22:18 -07001536 if (transport->flight_size == 0)
1537 transport->partial_bytes_acked = 0;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001538 q->outstanding_bytes -= bytes_acked + migrate_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 } else {
1540 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1541 * When a sender is doing zero window probing, it
1542 * should not timeout the association if it continues
1543 * to receive new packets from the receiver. The
1544 * reason is that the receiver MAY keep its window
1545 * closed for an indefinite time.
1546 * A sender is doing zero window probing when the
1547 * receiver's advertised window is zero, and there is
1548 * only one data chunk in flight to the receiver.
Thomas Graff8d96052011-07-07 00:28:35 +00001549 *
1550 * Allow the association to timeout while in SHUTDOWN
1551 * PENDING or SHUTDOWN RECEIVED in case the receiver
1552 * stays in zero window mode forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 */
1554 if (!q->asoc->peer.rwnd &&
1555 !list_empty(&tlist) &&
Thomas Graff8d96052011-07-07 00:28:35 +00001556 (sack_ctsn+2 == q->asoc->next_tsn) &&
1557 q->asoc->state < SCTP_STATE_SHUTDOWN_PENDING) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001558 pr_debug("%s: sack received for zero window "
1559 "probe:%u\n", __func__, sack_ctsn);
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 q->asoc->overall_error_count = 0;
1562 transport->error_count = 0;
1563 }
1564 }
1565
1566 /* RFC 2960 6.3.2 Retransmission Timer Rules
1567 *
1568 * R2) Whenever all outstanding data sent to an address have
1569 * been acknowledged, turn off the T3-rtx timer of that
1570 * address.
1571 */
1572 if (!transport->flight_size) {
Ying Xue25cc4ae2013-02-03 20:32:57 +00001573 if (del_timer(&transport->T3_rtx_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 sctp_transport_put(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 } else if (restart_timer) {
1576 if (!mod_timer(&transport->T3_rtx_timer,
1577 jiffies + transport->rto))
1578 sctp_transport_hold(transport);
1579 }
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001580
1581 if (forward_progress) {
1582 if (transport->dst)
1583 dst_confirm(transport->dst);
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
1587 list_splice(&tlist, transmitted_queue);
1588}
1589
1590/* Mark chunks as missing and consequently may get retransmitted. */
1591static void sctp_mark_missing(struct sctp_outq *q,
1592 struct list_head *transmitted_queue,
1593 struct sctp_transport *transport,
1594 __u32 highest_new_tsn_in_sack,
1595 int count_of_newacks)
1596{
1597 struct sctp_chunk *chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 __u32 tsn;
1599 char do_fast_retransmit = 0;
Vlad Yasevichea862c82010-04-30 22:41:10 -04001600 struct sctp_association *asoc = q->asoc;
1601 struct sctp_transport *primary = asoc->peer.primary_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001603 list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 tsn = ntohl(chunk->subh.data_hdr->tsn);
1606
1607 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1608 * 'Unacknowledged TSN's', if the TSN number of an
1609 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1610 * value, increment the 'TSN.Missing.Report' count on that
1611 * chunk if it has NOT been fast retransmitted or marked for
1612 * fast retransmit already.
1613 */
Neil Hormanc226ef92008-07-25 12:44:09 -04001614 if (chunk->fast_retransmit == SCTP_CAN_FRTX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 !chunk->tsn_gap_acked &&
1616 TSN_lt(tsn, highest_new_tsn_in_sack)) {
1617
1618 /* SFR-CACC may require us to skip marking
1619 * this chunk as missing.
1620 */
Vlad Yasevichf246a7b2011-04-18 19:13:56 +00001621 if (!transport || !sctp_cacc_skip(primary,
1622 chunk->transport,
1623 count_of_newacks, tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 chunk->tsn_missing_report++;
1625
Daniel Borkmannbb333812013-06-28 19:49:40 +02001626 pr_debug("%s: tsn:0x%x missing counter:%d\n",
1627 __func__, tsn, chunk->tsn_missing_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
1629 }
1630 /*
1631 * M4) If any DATA chunk is found to have a
1632 * 'TSN.Missing.Report'
Vlad Yasevich27852c22006-02-02 16:57:31 -08001633 * value larger than or equal to 3, mark that chunk for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * retransmission and start the fast retransmit procedure.
1635 */
1636
Vlad Yasevich27852c22006-02-02 16:57:31 -08001637 if (chunk->tsn_missing_report >= 3) {
Neil Hormanc226ef92008-07-25 12:44:09 -04001638 chunk->fast_retransmit = SCTP_NEED_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 do_fast_retransmit = 1;
1640 }
1641 }
1642
1643 if (transport) {
1644 if (do_fast_retransmit)
1645 sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1646
Daniel Borkmannbb333812013-06-28 19:49:40 +02001647 pr_debug("%s: transport:%p, cwnd:%d, ssthresh:%d, "
1648 "flight_size:%d, pba:%d\n", __func__, transport,
1649 transport->cwnd, transport->ssthresh,
1650 transport->flight_size, transport->partial_bytes_acked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652}
1653
1654/* Is the given TSN acked by this packet? */
1655static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1656{
1657 int i;
1658 sctp_sack_variable_t *frags;
1659 __u16 gap;
1660 __u32 ctsn = ntohl(sack->cum_tsn_ack);
1661
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001662 if (TSN_lte(tsn, ctsn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto pass;
1664
1665 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1666 *
1667 * Gap Ack Blocks:
1668 * These fields contain the Gap Ack Blocks. They are repeated
1669 * for each Gap Ack Block up to the number of Gap Ack Blocks
1670 * defined in the Number of Gap Ack Blocks field. All DATA
1671 * chunks with TSNs greater than or equal to (Cumulative TSN
1672 * Ack + Gap Ack Block Start) and less than or equal to
1673 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1674 * Block are assumed to have been received correctly.
1675 */
1676
1677 frags = sack->variable;
1678 gap = tsn - ctsn;
1679 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
1680 if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
1681 TSN_lte(gap, ntohs(frags[i].gab.end)))
1682 goto pass;
1683 }
1684
1685 return 0;
1686pass:
1687 return 1;
1688}
1689
1690static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
Al Viro9f81bcd2006-11-20 17:26:34 -08001691 int nskips, __be16 stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 int i;
1694
1695 for (i = 0; i < nskips; i++) {
1696 if (skiplist[i].stream == stream)
1697 return i;
1698 }
1699 return i;
1700}
1701
1702/* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1703static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
1704{
1705 struct sctp_association *asoc = q->asoc;
1706 struct sctp_chunk *ftsn_chunk = NULL;
1707 struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1708 int nskips = 0;
1709 int skip_pos = 0;
1710 __u32 tsn;
1711 struct sctp_chunk *chunk;
1712 struct list_head *lchunk, *temp;
1713
Wei Yongjun76595022009-03-12 09:49:19 +00001714 if (!asoc->peer.prsctp_capable)
1715 return;
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1718 * received SACK.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1721 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1722 */
1723 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1724 asoc->adv_peer_ack_point = ctsn;
1725
1726 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1727 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1728 * the chunk next in the out-queue space is marked as "abandoned" as
1729 * shown in the following example:
1730 *
1731 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1732 * and the Advanced.Peer.Ack.Point is updated to this value:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1735 * normal SACK processing local advancement
1736 * ... ...
1737 * Adv.Ack.Pt-> 102 acked 102 acked
1738 * 103 abandoned 103 abandoned
1739 * 104 abandoned Adv.Ack.P-> 104 abandoned
1740 * 105 105
1741 * 106 acked 106 acked
1742 * ... ...
1743 *
1744 * In this example, the data sender successfully advanced the
1745 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1746 */
1747 list_for_each_safe(lchunk, temp, &q->abandoned) {
1748 chunk = list_entry(lchunk, struct sctp_chunk,
1749 transmitted_list);
1750 tsn = ntohl(chunk->subh.data_hdr->tsn);
1751
1752 /* Remove any chunks in the abandoned queue that are acked by
1753 * the ctsn.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (TSN_lte(tsn, ctsn)) {
1756 list_del_init(lchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 sctp_chunk_free(chunk);
1758 } else {
1759 if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1760 asoc->adv_peer_ack_point = tsn;
1761 if (chunk->chunk_hdr->flags &
1762 SCTP_DATA_UNORDERED)
1763 continue;
1764 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1765 nskips,
1766 chunk->subh.data_hdr->stream);
1767 ftsn_skip_arr[skip_pos].stream =
1768 chunk->subh.data_hdr->stream;
1769 ftsn_skip_arr[skip_pos].ssn =
1770 chunk->subh.data_hdr->ssn;
1771 if (skip_pos == nskips)
1772 nskips++;
1773 if (nskips == 10)
1774 break;
1775 } else
1776 break;
1777 }
1778 }
1779
1780 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1781 * is greater than the Cumulative TSN ACK carried in the received
1782 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1783 * chunk containing the latest value of the
1784 * "Advanced.Peer.Ack.Point".
1785 *
1786 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1787 * list each stream and sequence number in the forwarded TSN. This
1788 * information will enable the receiver to easily find any
1789 * stranded TSN's waiting on stream reorder queues. Each stream
1790 * SHOULD only be reported once; this means that if multiple
1791 * abandoned messages occur in the same stream then only the
1792 * highest abandoned stream sequence number is reported. If the
1793 * total size of the FORWARD TSN does NOT fit in a single MTU then
1794 * the sender of the FORWARD TSN SHOULD lower the
1795 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1796 * single MTU.
1797 */
1798 if (asoc->adv_peer_ack_point > ctsn)
1799 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001800 nskips, &ftsn_skip_arr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 if (ftsn_chunk) {
David S. Miller79af02c2005-07-08 21:47:49 -07001803 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001804 SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806}