blob: 9cb854b05342e57a6743ee1fd7e91cab7c09bbd2 [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>
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030053#include <net/sctp/stream_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/* Declare internal functions here. */
56static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
57static void sctp_check_transmitted(struct sctp_outq *q,
58 struct list_head *transmitted_queue,
59 struct sctp_transport *transport,
Nicolas Dichteledfee032012-10-03 05:43:22 +000060 union sctp_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct sctp_sackhdr *sack,
Vlad Yasevichbfa0d982010-04-30 22:41:10 -040062 __u32 *highest_new_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static void sctp_mark_missing(struct sctp_outq *q,
65 struct list_head *transmitted_queue,
66 struct sctp_transport *transport,
67 __u32 highest_new_tsn,
68 int count_of_newacks);
69
Xin Long83dbc3d2016-09-14 02:04:22 +080070static void sctp_outq_flush(struct sctp_outq *q, int rtx_timeout, gfp_t gfp);
Adrian Bunkabd0b1982008-07-22 14:20:45 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Add data to the front of the queue. */
73static inline void sctp_outq_head_data(struct sctp_outq *q,
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030074 struct sctp_chunk *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030076 struct sctp_stream_out_ext *oute;
77 __u16 stream;
78
David S. Miller79af02c2005-07-08 21:47:49 -070079 list_add(&ch->list, &q->out_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 q->out_qlen += ch->skb->len;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030081
82 stream = sctp_chunk_stream_no(ch);
Konstantin Khorenko05364ca2018-08-10 20:11:42 +030083 oute = SCTP_SO(&q->asoc->stream, stream)->ext;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030084 list_add(&ch->stream_list, &oute->outq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/* Take data from the front of the queue. */
88static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
89{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030090 return q->sched->dequeue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Add data chunk to the end of the queue. */
94static inline void sctp_outq_tail_data(struct sctp_outq *q,
95 struct sctp_chunk *ch)
96{
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -030097 struct sctp_stream_out_ext *oute;
98 __u16 stream;
99
David S. Miller79af02c2005-07-08 21:47:49 -0700100 list_add_tail(&ch->list, &q->out_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 q->out_qlen += ch->skb->len;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300102
103 stream = sctp_chunk_stream_no(ch);
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300104 oute = SCTP_SO(&q->asoc->stream, stream)->ext;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300105 list_add_tail(&ch->stream_list, &oute->outq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
109 * SFR-CACC algorithm:
110 * D) If count_of_newacks is greater than or equal to 2
111 * and t was not sent to the current primary then the
112 * sender MUST NOT increment missing report count for t.
113 */
114static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
115 struct sctp_transport *transport,
116 int count_of_newacks)
117{
wangweidongcb3f8372013-12-23 12:16:50 +0800118 if (count_of_newacks >= 2 && transport != primary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 1;
120 return 0;
121}
122
123/*
124 * SFR-CACC algorithm:
125 * F) If count_of_newacks is less than 2, let d be the
126 * destination to which t was sent. If cacc_saw_newack
127 * is 0 for destination d, then the sender MUST NOT
128 * increment missing report count for t.
129 */
130static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
131 int count_of_newacks)
132{
Vlad Yasevichf246a7b2011-04-18 19:13:56 +0000133 if (count_of_newacks < 2 &&
134 (transport && !transport->cacc.cacc_saw_newack))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 1;
136 return 0;
137}
138
139/*
140 * SFR-CACC algorithm:
141 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
142 * execute steps C, D, F.
143 *
144 * C has been implemented in sctp_outq_sack
145 */
146static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
147 struct sctp_transport *transport,
148 int count_of_newacks)
149{
150 if (!primary->cacc.cycling_changeover) {
151 if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
152 return 1;
153 if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
154 return 1;
155 return 0;
156 }
157 return 0;
158}
159
160/*
161 * SFR-CACC algorithm:
162 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
163 * than next_tsn_at_change of the current primary, then
164 * the sender MUST NOT increment missing report count
165 * for t.
166 */
167static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
168{
169 if (primary->cacc.cycling_changeover &&
170 TSN_lt(tsn, primary->cacc.next_tsn_at_change))
171 return 1;
172 return 0;
173}
174
175/*
176 * SFR-CACC algorithm:
177 * 3) If the missing report count for TSN t is to be
178 * incremented according to [RFC2960] and
179 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300180 * then the sender MUST further execute steps 3.1 and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * 3.2 to determine if the missing report count for
182 * TSN t SHOULD NOT be incremented.
183 *
184 * 3.3) If 3.1 and 3.2 do not dictate that the missing
185 * report count for t should not be incremented, then
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300186 * the sender SHOULD increment missing report count for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
188 */
189static inline int sctp_cacc_skip(struct sctp_transport *primary,
190 struct sctp_transport *transport,
191 int count_of_newacks,
192 __u32 tsn)
193{
194 if (primary->cacc.changeover_active &&
Joe Perchesf64f9e72009-11-29 16:55:45 -0800195 (sctp_cacc_skip_3_1(primary, transport, count_of_newacks) ||
196 sctp_cacc_skip_3_2(primary, tsn)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return 1;
198 return 0;
199}
200
201/* Initialize an existing sctp_outq. This does the boring stuff.
202 * You still need to define handlers if you really want to DO
203 * something with this structure...
204 */
205void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
206{
Neil Hormanc5c77742013-06-12 14:26:44 -0400207 memset(q, 0, sizeof(struct sctp_outq));
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 q->asoc = asoc;
David S. Miller79af02c2005-07-08 21:47:49 -0700210 INIT_LIST_HEAD(&q->out_chunk_list);
211 INIT_LIST_HEAD(&q->control_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 INIT_LIST_HEAD(&q->retransmit);
213 INIT_LIST_HEAD(&q->sacked);
214 INIT_LIST_HEAD(&q->abandoned);
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300215 sctp_sched_set_sched(asoc, SCTP_SS_FCFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/* Free the outqueue structure and any related pending chunks.
219 */
Neil Horman2f94aab2013-01-17 11:15:08 +0000220static void __sctp_outq_teardown(struct sctp_outq *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct sctp_transport *transport;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700223 struct list_head *lchunk, *temp;
David S. Miller79af02c2005-07-08 21:47:49 -0700224 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* Throw away unacknowledged chunks. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700227 list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
228 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
230 chunk = list_entry(lchunk, struct sctp_chunk,
231 transmitted_list);
232 /* Mark as part of a failed message. */
233 sctp_chunk_fail(chunk, q->error);
234 sctp_chunk_free(chunk);
235 }
236 }
237
238 /* Throw away chunks that have been gap ACKed. */
239 list_for_each_safe(lchunk, temp, &q->sacked) {
240 list_del_init(lchunk);
241 chunk = list_entry(lchunk, struct sctp_chunk,
242 transmitted_list);
243 sctp_chunk_fail(chunk, q->error);
244 sctp_chunk_free(chunk);
245 }
246
247 /* Throw away any chunks in the retransmit queue. */
248 list_for_each_safe(lchunk, temp, &q->retransmit) {
249 list_del_init(lchunk);
250 chunk = list_entry(lchunk, struct sctp_chunk,
251 transmitted_list);
252 sctp_chunk_fail(chunk, q->error);
253 sctp_chunk_free(chunk);
254 }
255
256 /* Throw away any chunks that are in the abandoned queue. */
257 list_for_each_safe(lchunk, temp, &q->abandoned) {
258 list_del_init(lchunk);
259 chunk = list_entry(lchunk, struct sctp_chunk,
260 transmitted_list);
261 sctp_chunk_fail(chunk, q->error);
262 sctp_chunk_free(chunk);
263 }
264
265 /* Throw away any leftover data chunks. */
266 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300267 sctp_sched_dequeue_done(q, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* Mark as send failure. */
270 sctp_chunk_fail(chunk, q->error);
271 sctp_chunk_free(chunk);
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Throw away any leftover control chunks. */
David S. Miller79af02c2005-07-08 21:47:49 -0700275 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
276 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 sctp_chunk_free(chunk);
David S. Miller79af02c2005-07-08 21:47:49 -0700278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Neil Horman2f94aab2013-01-17 11:15:08 +0000281void sctp_outq_teardown(struct sctp_outq *q)
282{
283 __sctp_outq_teardown(q);
284 sctp_outq_init(q->asoc, q);
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* Free the outqueue structure and any related pending chunks. */
288void sctp_outq_free(struct sctp_outq *q)
289{
290 /* Throw away leftover chunks. */
Neil Horman2f94aab2013-01-17 11:15:08 +0000291 __sctp_outq_teardown(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/* Put a new chunk in an sctp_outq. */
Xin Long83dbc3d2016-09-14 02:04:22 +0800295void sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000297 struct net *net = sock_net(q->asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Daniel Borkmannbb333812013-06-28 19:49:40 +0200299 pr_debug("%s: outq:%p, chunk:%p[%s]\n", __func__, q, chunk,
300 chunk && chunk->chunk_hdr ?
301 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
302 "illegal chunk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* If it is data, queue it up, otherwise, send it
305 * immediately.
306 */
Shan Weiec7b9512010-04-30 22:41:09 -0400307 if (sctp_chunk_is_data(chunk)) {
Xin Long2c897912016-09-14 02:04:18 +0800308 pr_debug("%s: outqueueing: outq:%p, chunk:%p[%s])\n",
309 __func__, q, chunk, chunk && chunk->chunk_hdr ?
310 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
311 "illegal chunk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Xin Long2c897912016-09-14 02:04:18 +0800313 sctp_outq_tail_data(q, chunk);
David S. Millerb50afd22016-10-02 21:17:07 -0400314 if (chunk->asoc->peer.prsctp_capable &&
Xin Long2c897912016-09-14 02:04:18 +0800315 SCTP_PR_PRIO_ENABLED(chunk->sinfo.sinfo_flags))
316 chunk->asoc->sent_cnt_removable++;
317 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
318 SCTP_INC_STATS(net, SCTP_MIB_OUTUNORDERCHUNKS);
319 else
320 SCTP_INC_STATS(net, SCTP_MIB_OUTORDERCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 } else {
David S. Miller79af02c2005-07-08 21:47:49 -0700322 list_add_tail(&chunk->list, &q->control_chunk_list);
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000323 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!q->cork)
Xin Long83dbc3d2016-09-14 02:04:22 +0800327 sctp_outq_flush(q, 0, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/* Insert a chunk into the sorted list based on the TSNs. The retransmit list
331 * and the abandoned list are in ascending order.
332 */
333static void sctp_insert_list(struct list_head *head, struct list_head *new)
334{
335 struct list_head *pos;
336 struct sctp_chunk *nchunk, *lchunk;
337 __u32 ntsn, ltsn;
338 int done = 0;
339
340 nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
341 ntsn = ntohl(nchunk->subh.data_hdr->tsn);
342
343 list_for_each(pos, head) {
344 lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
345 ltsn = ntohl(lchunk->subh.data_hdr->tsn);
346 if (TSN_lt(ntsn, ltsn)) {
347 list_add(new, pos->prev);
348 done = 1;
349 break;
350 }
351 }
352 if (!done)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900353 list_add_tail(new, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Xin Long8dbdf1f2016-07-09 19:47:45 +0800356static int sctp_prsctp_prune_sent(struct sctp_association *asoc,
357 struct sctp_sndrcvinfo *sinfo,
358 struct list_head *queue, int msg_len)
359{
360 struct sctp_chunk *chk, *temp;
361
362 list_for_each_entry_safe(chk, temp, queue, transmitted_list) {
Xin Longd229d482017-04-01 17:07:46 +0800363 struct sctp_stream_out *streamout;
364
Xin Longe5f61292017-11-25 21:18:35 +0800365 if (!chk->msg->abandoned &&
366 (!SCTP_PR_PRIO_ENABLED(chk->sinfo.sinfo_flags) ||
367 chk->sinfo.sinfo_timetolive <= sinfo->sinfo_timetolive))
Xin Long8dbdf1f2016-07-09 19:47:45 +0800368 continue;
369
Xin Longe5f61292017-11-25 21:18:35 +0800370 chk->msg->abandoned = 1;
Xin Long8dbdf1f2016-07-09 19:47:45 +0800371 list_del_init(&chk->transmitted_list);
372 sctp_insert_list(&asoc->outqueue.abandoned,
373 &chk->transmitted_list);
374
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300375 streamout = SCTP_SO(&asoc->stream, chk->sinfo.sinfo_stream);
Xin Long8dbdf1f2016-07-09 19:47:45 +0800376 asoc->sent_cnt_removable--;
377 asoc->abandoned_sent[SCTP_PR_INDEX(PRIO)]++;
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300378 streamout->ext->abandoned_sent[SCTP_PR_INDEX(PRIO)]++;
Xin Long8dbdf1f2016-07-09 19:47:45 +0800379
Xin Longd30fc512017-11-25 21:18:34 +0800380 if (queue != &asoc->outqueue.retransmit &&
381 !chk->tsn_gap_acked) {
Xin Long8dbdf1f2016-07-09 19:47:45 +0800382 if (chk->transport)
383 chk->transport->flight_size -=
384 sctp_data_size(chk);
385 asoc->outqueue.outstanding_bytes -= sctp_data_size(chk);
386 }
387
Xin Long605c0ac2018-10-17 03:07:50 +0800388 msg_len -= chk->skb->truesize + sizeof(struct sctp_chunk);
Xin Long8dbdf1f2016-07-09 19:47:45 +0800389 if (msg_len <= 0)
390 break;
391 }
392
393 return msg_len;
394}
395
396static int sctp_prsctp_prune_unsent(struct sctp_association *asoc,
Xin Long23bb09c2017-03-18 20:03:59 +0800397 struct sctp_sndrcvinfo *sinfo, int msg_len)
Xin Long8dbdf1f2016-07-09 19:47:45 +0800398{
Xin Long23bb09c2017-03-18 20:03:59 +0800399 struct sctp_outq *q = &asoc->outqueue;
Xin Long8dbdf1f2016-07-09 19:47:45 +0800400 struct sctp_chunk *chk, *temp;
401
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300402 q->sched->unsched_all(&asoc->stream);
403
Xin Long23bb09c2017-03-18 20:03:59 +0800404 list_for_each_entry_safe(chk, temp, &q->out_chunk_list, list) {
Xin Longe5f61292017-11-25 21:18:35 +0800405 if (!chk->msg->abandoned &&
Xin Long779edd72017-11-25 21:18:36 +0800406 (!(chk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG) ||
407 !SCTP_PR_PRIO_ENABLED(chk->sinfo.sinfo_flags) ||
Xin Longe5f61292017-11-25 21:18:35 +0800408 chk->sinfo.sinfo_timetolive <= sinfo->sinfo_timetolive))
Xin Long8dbdf1f2016-07-09 19:47:45 +0800409 continue;
410
Xin Longe5f61292017-11-25 21:18:35 +0800411 chk->msg->abandoned = 1;
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300412 sctp_sched_dequeue_common(q, chk);
Xin Long8dbdf1f2016-07-09 19:47:45 +0800413 asoc->sent_cnt_removable--;
414 asoc->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
Xin Longcee360a2017-05-31 16:36:31 +0800415 if (chk->sinfo.sinfo_stream < asoc->stream.outcnt) {
Xin Longd229d482017-04-01 17:07:46 +0800416 struct sctp_stream_out *streamout =
Konstantin Khorenko05364ca2018-08-10 20:11:42 +0300417 SCTP_SO(&asoc->stream, chk->sinfo.sinfo_stream);
Xin Longd229d482017-04-01 17:07:46 +0800418
Marcelo Ricardo Leitnerf952be72017-10-03 19:20:11 -0300419 streamout->ext->abandoned_unsent[SCTP_PR_INDEX(PRIO)]++;
Xin Longd229d482017-04-01 17:07:46 +0800420 }
Xin Long8dbdf1f2016-07-09 19:47:45 +0800421
Xin Long605c0ac2018-10-17 03:07:50 +0800422 msg_len -= chk->skb->truesize + sizeof(struct sctp_chunk);
Xin Long8dbdf1f2016-07-09 19:47:45 +0800423 sctp_chunk_free(chk);
424 if (msg_len <= 0)
425 break;
426 }
427
Marcelo Ricardo Leitner5bbbbe32017-10-03 19:20:13 -0300428 q->sched->sched_all(&asoc->stream);
429
Xin Long8dbdf1f2016-07-09 19:47:45 +0800430 return msg_len;
431}
432
433/* Abandon the chunks according their priorities */
434void sctp_prsctp_prune(struct sctp_association *asoc,
435 struct sctp_sndrcvinfo *sinfo, int msg_len)
436{
437 struct sctp_transport *transport;
438
Xin Longbe4947b2016-09-29 02:37:28 +0800439 if (!asoc->peer.prsctp_capable || !asoc->sent_cnt_removable)
Xin Long8dbdf1f2016-07-09 19:47:45 +0800440 return;
441
442 msg_len = sctp_prsctp_prune_sent(asoc, sinfo,
443 &asoc->outqueue.retransmit,
444 msg_len);
445 if (msg_len <= 0)
446 return;
447
448 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
449 transports) {
450 msg_len = sctp_prsctp_prune_sent(asoc, sinfo,
451 &transport->transmitted,
452 msg_len);
453 if (msg_len <= 0)
454 return;
455 }
456
Xin Long23bb09c2017-03-18 20:03:59 +0800457 sctp_prsctp_prune_unsent(asoc, sinfo, msg_len);
Xin Long8dbdf1f2016-07-09 19:47:45 +0800458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* Mark all the eligible packets on a transport for retransmission. */
461void sctp_retransmit_mark(struct sctp_outq *q,
462 struct sctp_transport *transport,
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400463 __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct list_head *lchunk, *ltemp;
466 struct sctp_chunk *chunk;
467
468 /* Walk through the specified transmitted queue. */
469 list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
470 chunk = list_entry(lchunk, struct sctp_chunk,
471 transmitted_list);
472
473 /* If the chunk is abandoned, move it to abandoned list. */
474 if (sctp_chunk_abandoned(chunk)) {
475 list_del_init(lchunk);
476 sctp_insert_list(&q->abandoned, lchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800477
478 /* If this chunk has not been previousely acked,
479 * stop considering it 'outstanding'. Our peer
480 * will most likely never see it since it will
481 * not be retransmitted
482 */
483 if (!chunk->tsn_gap_acked) {
Vlad Yasevich31b02e12009-09-04 18:21:00 -0400484 if (chunk->transport)
485 chunk->transport->flight_size -=
486 sctp_data_size(chunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800487 q->outstanding_bytes -= sctp_data_size(chunk);
Thomas Grafa76c0ad2011-12-19 04:11:40 +0000488 q->asoc->peer.rwnd += sctp_data_size(chunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 continue;
491 }
492
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400493 /* If we are doing retransmission due to a timeout or pmtu
494 * discovery, only the chunks that are not yet acked should
495 * be added to the retransmit queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400497 if ((reason == SCTP_RTXR_FAST_RTX &&
Neil Hormanc226ef92008-07-25 12:44:09 -0400498 (chunk->fast_retransmit == SCTP_NEED_FRTX)) ||
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400499 (reason != SCTP_RTXR_FAST_RTX && !chunk->tsn_gap_acked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* RFC 2960 6.2.1 Processing a Received SACK
501 *
502 * C) Any time a DATA chunk is marked for
503 * retransmission (via either T3-rtx timer expiration
504 * (Section 6.3.3) or via fast retransmit
505 * (Section 7.2.4)), add the data size of those
506 * chunks to the rwnd.
507 */
Thomas Grafa76c0ad2011-12-19 04:11:40 +0000508 q->asoc->peer.rwnd += sctp_data_size(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 q->outstanding_bytes -= sctp_data_size(chunk);
Vlad Yasevich31b02e12009-09-04 18:21:00 -0400510 if (chunk->transport)
511 transport->flight_size -= sctp_data_size(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* sctpimpguide-05 Section 2.8.2
514 * M5) If a T3-rtx timer expires, the
515 * 'TSN.Missing.Report' of all affected TSNs is set
516 * to 0.
517 */
518 chunk->tsn_missing_report = 0;
519
520 /* If a chunk that is being used for RTT measurement
521 * has to be retransmitted, we cannot use this chunk
522 * anymore for RTT measurements. Reset rto_pending so
523 * that a new RTT measurement is started when a new
524 * data chunk is sent.
525 */
526 if (chunk->rtt_in_progress) {
527 chunk->rtt_in_progress = 0;
528 transport->rto_pending = 0;
529 }
530
531 /* Move the chunk to the retransmit queue. The chunks
532 * on the retransmit queue are always kept in order.
533 */
534 list_del_init(lchunk);
535 sctp_insert_list(&q->retransmit, lchunk);
536 }
537 }
538
Daniel Borkmannbb333812013-06-28 19:49:40 +0200539 pr_debug("%s: transport:%p, reason:%d, cwnd:%d, ssthresh:%d, "
540 "flight_size:%d, pba:%d\n", __func__, transport, reason,
541 transport->cwnd, transport->ssthresh, transport->flight_size,
542 transport->partial_bytes_acked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/* Mark all the eligible packets on a transport for retransmission and force
546 * one packet out.
547 */
548void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
Xin Long125c2982017-08-05 19:59:52 +0800549 enum sctp_retransmit_reason reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000551 struct net *net = sock_net(q->asoc->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
wangweidongcb3f8372013-12-23 12:16:50 +0800553 switch (reason) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 case SCTP_RTXR_T3_RTX:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000555 SCTP_INC_STATS(net, SCTP_MIB_T3_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
557 /* Update the retran path if the T3-rtx timer has expired for
558 * the current retran path.
559 */
560 if (transport == transport->asoc->peer.retran_path)
561 sctp_assoc_update_retran_path(transport->asoc);
Neil Horman58fbbed2008-02-29 11:40:56 -0800562 transport->asoc->rtx_data_chunks +=
563 transport->asoc->unack_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case SCTP_RTXR_FAST_RTX:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000566 SCTP_INC_STATS(net, SCTP_MIB_FAST_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700568 q->fast_rtx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570 case SCTP_RTXR_PMTUD:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000571 SCTP_INC_STATS(net, SCTP_MIB_PMTUD_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400573 case SCTP_RTXR_T1_RTX:
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000574 SCTP_INC_STATS(net, SCTP_MIB_T1_RETRANSMITS);
Neil Horman58fbbed2008-02-29 11:40:56 -0800575 transport->asoc->init_retries++;
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400576 break;
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700577 default:
578 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400581 sctp_retransmit_mark(q, transport, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
584 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
585 * following the procedures outlined in C1 - C5.
586 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700587 if (reason == SCTP_RTXR_T3_RTX)
Xin Long8e0c3b72017-12-15 00:41:26 +0800588 q->asoc->stream.si->generate_ftsn(q, q->asoc->ctsn_ack_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700590 /* Flush the queues only on timeout, since fast_rtx is only
591 * triggered during sack processing and the queue
592 * will be flushed at the end.
593 */
594 if (reason != SCTP_RTXR_FAST_RTX)
Xin Long64519442016-09-14 02:04:21 +0800595 sctp_outq_flush(q, /* rtx_timeout */ 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598/*
599 * Transmit DATA chunks on the retransmit queue. Upon return from
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300600 * __sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * need to be transmitted by the caller.
602 * We assume that pkt->transport has already been set.
603 *
604 * The return value is a normal kernel error return value.
605 */
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300606static int __sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
Marcelo Ricardo Leitner6605f692018-05-14 14:34:42 -0300607 int rtx_timeout, int *start_timer, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct sctp_transport *transport = pkt->transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct sctp_chunk *chunk, *chunk1;
Xin Long86b36f22017-08-05 19:59:57 +0800611 struct list_head *lqueue;
612 enum sctp_xmit status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int error = 0;
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700614 int timer = 0;
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700615 int done = 0;
Xin Long86b36f22017-08-05 19:59:57 +0800616 int fast_rtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 lqueue = &q->retransmit;
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700619 fast_rtx = q->fast_rtx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700621 /* This loop handles time-out retransmissions, fast retransmissions,
622 * and retransmissions due to opening of whindow.
623 *
624 * RFC 2960 6.3.3 Handle T3-rtx Expiration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
626 * E3) Determine how many of the earliest (i.e., lowest TSN)
627 * outstanding DATA chunks for the address for which the
628 * T3-rtx has expired will fit into a single packet, subject
629 * to the MTU constraint for the path corresponding to the
630 * destination transport address to which the retransmission
631 * is being sent (this may be different from the address for
632 * which the timer expires [see Section 6.4]). Call this value
633 * K. Bundle and retransmit those K DATA chunks in a single
634 * packet to the destination endpoint.
635 *
636 * [Just to be painfully clear, if we are retransmitting
637 * because a timeout just happened, we should send only ONE
638 * packet of retransmitted data.]
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700639 *
640 * For fast retransmissions we also send only ONE packet. However,
641 * if we are just flushing the queue due to open window, we'll
642 * try to send as much as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700644 list_for_each_entry_safe(chunk, chunk1, lqueue, transmitted_list) {
Wei Yongjun4c6a6f422011-04-19 21:32:28 +0000645 /* If the chunk is abandoned, move it to abandoned list. */
646 if (sctp_chunk_abandoned(chunk)) {
647 list_del_init(&chunk->transmitted_list);
648 sctp_insert_list(&q->abandoned,
649 &chunk->transmitted_list);
650 continue;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* Make sure that Gap Acked TSNs are not retransmitted. A
654 * simple approach is just to move such TSNs out of the
655 * way and into a 'transmitted' queue and skip to the
656 * next chunk.
657 */
658 if (chunk->tsn_gap_acked) {
Wei Yongjun54a27922012-09-03 23:58:16 +0000659 list_move_tail(&chunk->transmitted_list,
660 &transport->transmitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 continue;
662 }
663
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700664 /* If we are doing fast retransmit, ignore non-fast_rtransmit
665 * chunks
666 */
667 if (fast_rtx && !chunk->fast_retransmit)
668 continue;
669
Wei Yongjunbc4f8412010-04-30 22:38:53 -0400670redo:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* Attempt to append this chunk to the packet. */
672 status = sctp_packet_append_chunk(pkt, chunk);
673
674 switch (status) {
675 case SCTP_XMIT_PMTU_FULL:
Wei Yongjunbc4f8412010-04-30 22:38:53 -0400676 if (!pkt->has_data && !pkt->has_cookie_echo) {
677 /* If this packet did not contain DATA then
678 * retransmission did not happen, so do it
679 * again. We'll ignore the error here since
680 * control chunks are already freed so there
681 * is nothing we can do.
682 */
Marcelo Ricardo Leitner6605f692018-05-14 14:34:42 -0300683 sctp_packet_transmit(pkt, gfp);
Wei Yongjunbc4f8412010-04-30 22:38:53 -0400684 goto redo;
685 }
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Send this packet. */
Marcelo Ricardo Leitner6605f692018-05-14 14:34:42 -0300688 error = sctp_packet_transmit(pkt, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* If we are retransmitting, we should only
691 * send a single packet.
Vlad Yasevichf246a7b2011-04-18 19:13:56 +0000692 * Otherwise, try appending this chunk again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700694 if (rtx_timeout || fast_rtx)
695 done = 1;
Vlad Yasevichf246a7b2011-04-18 19:13:56 +0000696 else
697 goto redo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700699 /* Bundle next chunk in the next round. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
701
702 case SCTP_XMIT_RWND_FULL:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900703 /* Send this packet. */
Marcelo Ricardo Leitner6605f692018-05-14 14:34:42 -0300704 error = sctp_packet_transmit(pkt, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 /* Stop sending DATA as there is no more room
707 * at the receiver.
708 */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700709 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711
David Laight526cbef2014-07-22 08:59:14 +0000712 case SCTP_XMIT_DELAY:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900713 /* Send this packet. */
Marcelo Ricardo Leitner6605f692018-05-14 14:34:42 -0300714 error = sctp_packet_transmit(pkt, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Stop sending DATA because of nagle delay. */
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700717 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719
720 default:
721 /* The append was successful, so add this chunk to
722 * the transmitted list.
723 */
Wei Yongjun54a27922012-09-03 23:58:16 +0000724 list_move_tail(&chunk->transmitted_list,
725 &transport->transmitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900727 /* Mark the chunk as ineligible for fast retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * after it is retransmitted.
729 */
Neil Hormanc226ef92008-07-25 12:44:09 -0400730 if (chunk->fast_retransmit == SCTP_NEED_FRTX)
731 chunk->fast_retransmit = SCTP_DONT_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Michele Baldessari196d6752012-12-01 04:49:42 +0000733 q->asoc->stats.rtxchunks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700737 /* Set the timer if there were no errors */
738 if (!error && !timer)
739 timer = 1;
740
Vlad Yasevich8b750ce2008-06-04 12:39:36 -0700741 if (done)
742 break;
743 }
744
745 /* If we are here due to a retransmit timeout or a fast
746 * retransmit and if there are any chunks left in the retransmit
747 * queue that could not fit in the PMTU sized packet, they need
748 * to be marked as ineligible for a subsequent fast retransmit.
749 */
750 if (rtx_timeout || fast_rtx) {
751 list_for_each_entry(chunk1, lqueue, transmitted_list) {
Neil Hormanc226ef92008-07-25 12:44:09 -0400752 if (chunk1->fast_retransmit == SCTP_NEED_FRTX)
753 chunk1->fast_retransmit = SCTP_DONT_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 }
756
Vlad Yasevich62aeaff2008-06-04 12:39:11 -0700757 *start_timer = timer;
758
759 /* Clear fast retransmit hint */
760 if (fast_rtx)
761 q->fast_rtx = 0;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return error;
764}
765
766/* Cork the outqueue so queued chunks are really queued. */
Xin Long83dbc3d2016-09-14 02:04:22 +0800767void sctp_outq_uncork(struct sctp_outq *q, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Vlad Yasevich7d54dc62007-11-09 11:43:41 -0500769 if (q->cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 q->cork = 0;
Daniel Borkmanndacda322013-04-16 11:07:14 +0000771
Xin Long83dbc3d2016-09-14 02:04:22 +0800772 sctp_outq_flush(q, 0, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Marcelo Ricardo Leitnerb9fd6832018-05-14 14:34:36 -0300775static int sctp_packet_singleton(struct sctp_transport *transport,
776 struct sctp_chunk *chunk, gfp_t gfp)
777{
778 const struct sctp_association *asoc = transport->asoc;
779 const __u16 sport = asoc->base.bind_addr.port;
780 const __u16 dport = asoc->peer.port;
781 const __u32 vtag = asoc->peer.i.init_tag;
782 struct sctp_packet singleton;
783
784 sctp_packet_init(&singleton, transport, sport, dport);
785 sctp_packet_config(&singleton, vtag, 0);
786 sctp_packet_append_chunk(&singleton, chunk);
787 return sctp_packet_transmit(&singleton, gfp);
788}
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700789
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300790/* Struct to hold the context during sctp outq flush */
791struct sctp_flush_ctx {
792 struct sctp_outq *q;
793 /* Current transport being used. It's NOT the same as curr active one */
794 struct sctp_transport *transport;
795 /* These transports have chunks to send. */
796 struct list_head transport_list;
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300797 struct sctp_association *asoc;
798 /* Packet on the current transport above */
799 struct sctp_packet *packet;
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300800 gfp_t gfp;
801};
802
803/* transport: current transport */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300804static void sctp_outq_select_transport(struct sctp_flush_ctx *ctx,
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300805 struct sctp_chunk *chunk)
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300806{
807 struct sctp_transport *new_transport = chunk->transport;
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300808
809 if (!new_transport) {
810 if (!sctp_chunk_is_data(chunk)) {
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -0300811 /* If we have a prior transport pointer, see if
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300812 * the destination address of the chunk
813 * matches the destination address of the
814 * current transport. If not a match, then
815 * try to look up the transport with a given
816 * destination address. We do this because
817 * after processing ASCONFs, we may have new
818 * transports created.
819 */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300820 if (ctx->transport && sctp_cmp_addr_exact(&chunk->dest,
821 &ctx->transport->ipaddr))
822 new_transport = ctx->transport;
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300823 else
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300824 new_transport = sctp_assoc_lookup_paddr(ctx->asoc,
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300825 &chunk->dest);
826 }
827
828 /* if we still don't have a new transport, then
829 * use the current active path.
830 */
831 if (!new_transport)
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300832 new_transport = ctx->asoc->peer.active_path;
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300833 } else {
834 __u8 type;
835
836 switch (new_transport->state) {
837 case SCTP_INACTIVE:
838 case SCTP_UNCONFIRMED:
839 case SCTP_PF:
840 /* If the chunk is Heartbeat or Heartbeat Ack,
841 * send it to chunk->transport, even if it's
842 * inactive.
843 *
844 * 3.3.6 Heartbeat Acknowledgement:
845 * ...
846 * A HEARTBEAT ACK is always sent to the source IP
847 * address of the IP datagram containing the
848 * HEARTBEAT chunk to which this ack is responding.
849 * ...
850 *
851 * ASCONF_ACKs also must be sent to the source.
852 */
853 type = chunk->chunk_hdr->type;
854 if (type != SCTP_CID_HEARTBEAT &&
855 type != SCTP_CID_HEARTBEAT_ACK &&
856 type != SCTP_CID_ASCONF_ACK)
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300857 new_transport = ctx->asoc->peer.active_path;
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300858 break;
859 default:
860 break;
861 }
862 }
863
864 /* Are we switching transports? Take care of transport locks. */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300865 if (new_transport != ctx->transport) {
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300866 ctx->transport = new_transport;
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300867 ctx->packet = &ctx->transport->packet;
868
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300869 if (list_empty(&ctx->transport->send_ready))
870 list_add_tail(&ctx->transport->send_ready,
871 &ctx->transport_list);
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300872
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300873 sctp_packet_config(ctx->packet,
874 ctx->asoc->peer.i.init_tag,
875 ctx->asoc->peer.ecn_capable);
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300876 /* We've switched transports, so apply the
877 * Burst limit to the new transport.
878 */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300879 sctp_transport_burst_limited(ctx->transport);
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300880 }
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300881}
882
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300883static void sctp_outq_flush_ctrl(struct sctp_flush_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
David S. Miller79af02c2005-07-08 21:47:49 -0700885 struct sctp_chunk *chunk, *tmp;
Xin Long86b36f22017-08-05 19:59:57 +0800886 enum sctp_xmit status;
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -0300887 int one_packet, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300889 list_for_each_entry_safe(chunk, tmp, &ctx->q->control_chunk_list, list) {
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -0300890 one_packet = 0;
891
Michio Honda8a07eb02011-04-26 20:19:36 +0900892 /* RFC 5061, 5.3
893 * F1) This means that until such time as the ASCONF
894 * containing the add is acknowledged, the sender MUST
895 * NOT use the new IP address as a source for ANY SCTP
896 * packet except on carrying an ASCONF Chunk.
897 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300898 if (ctx->asoc->src_out_of_asoc_ok &&
Michio Honda8a07eb02011-04-26 20:19:36 +0900899 chunk->chunk_hdr->type != SCTP_CID_ASCONF)
900 continue;
901
David S. Miller79af02c2005-07-08 21:47:49 -0700902 list_del_init(&chunk->list);
903
Marcelo Ricardo Leitner0d634b02018-05-14 14:34:37 -0300904 /* Pick the right transport to use. Should always be true for
905 * the first chunk as we don't have a transport by then.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300907 sctp_outq_select_transport(ctx, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 switch (chunk->chunk_hdr->type) {
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -0300910 /* 6.10 Bundling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * ...
912 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
913 * COMPLETE with any other chunks. [Send them immediately.]
914 */
915 case SCTP_CID_INIT:
916 case SCTP_CID_INIT_ACK:
917 case SCTP_CID_SHUTDOWN_COMPLETE:
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300918 error = sctp_packet_singleton(ctx->transport, chunk,
919 ctx->gfp);
Xin Long64519442016-09-14 02:04:21 +0800920 if (error < 0) {
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300921 ctx->asoc->base.sk->sk_err = -error;
Xin Long83dbc3d2016-09-14 02:04:22 +0800922 return;
Xin Long64519442016-09-14 02:04:21 +0800923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925
926 case SCTP_CID_ABORT:
Marcelo Ricardo Leitner37f47bc2018-01-11 14:22:06 -0200927 if (sctp_test_T_bit(chunk))
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300928 ctx->packet->vtag = ctx->asoc->c.my_vtag;
Marcelo Ricardo Leitner37f47bc2018-01-11 14:22:06 -0200929 /* fallthru */
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -0300930
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700931 /* The following chunks are "response" chunks, i.e.
932 * they are generated in response to something we
933 * received. If we are sending these, then we can
934 * send only 1 packet containing these chunks.
935 */
936 case SCTP_CID_HEARTBEAT_ACK:
937 case SCTP_CID_SHUTDOWN_ACK:
938 case SCTP_CID_COOKIE_ACK:
939 case SCTP_CID_COOKIE_ECHO:
940 case SCTP_CID_ERROR:
941 case SCTP_CID_ECN_CWR:
942 case SCTP_CID_ASCONF_ACK:
943 one_packet = 1;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300944 /* Fall through */
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case SCTP_CID_SACK:
947 case SCTP_CID_HEARTBEAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 case SCTP_CID_SHUTDOWN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case SCTP_CID_ECN_ECNE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case SCTP_CID_ASCONF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 case SCTP_CID_FWD_TSN:
Xin Long8e0c3b72017-12-15 00:41:26 +0800952 case SCTP_CID_I_FWD_TSN:
Xin Long7f9d68a2017-01-18 00:44:47 +0800953 case SCTP_CID_RECONF:
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300954 status = sctp_packet_transmit_chunk(ctx->packet, chunk,
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300955 one_packet, ctx->gfp);
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -0300956 if (status != SCTP_XMIT_OK) {
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700957 /* put the chunk back */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300958 list_add(&chunk->list, &ctx->q->control_chunk_list);
Xin Long7f9d68a2017-01-18 00:44:47 +0800959 break;
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700960 }
Xin Long7f9d68a2017-01-18 00:44:47 +0800961
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300962 ctx->asoc->stats.octrlchunks++;
Xin Long7f9d68a2017-01-18 00:44:47 +0800963 /* PR-SCTP C5) If a FORWARD TSN is sent, the
964 * sender MUST assure that at least one T3-rtx
965 * timer is running.
966 */
Xin Long8e0c3b72017-12-15 00:41:26 +0800967 if (chunk->chunk_hdr->type == SCTP_CID_FWD_TSN ||
968 chunk->chunk_hdr->type == SCTP_CID_I_FWD_TSN) {
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300969 sctp_transport_reset_t3_rtx(ctx->transport);
970 ctx->transport->last_time_sent = jiffies;
Xin Long7f9d68a2017-01-18 00:44:47 +0800971 }
972
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300973 if (chunk == ctx->asoc->strreset_chunk)
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300974 sctp_transport_reset_reconf_timer(ctx->transport);
Xin Long7f9d68a2017-01-18 00:44:47 +0800975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
977
978 default:
979 /* We built a chunk with an illegal type! */
980 BUG();
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -0300983}
984
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300985/* Returns false if new data shouldn't be sent */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300986static bool sctp_outq_flush_rtx(struct sctp_flush_ctx *ctx,
987 int rtx_timeout)
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300988{
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300989 int error, start_timer = 0;
990
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300991 if (ctx->asoc->peer.retran_path->state == SCTP_UNCONFIRMED)
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300992 return false;
993
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300994 if (ctx->transport != ctx->asoc->peer.retran_path) {
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300995 /* Switch transports & prepare the packet. */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -0300996 ctx->transport = ctx->asoc->peer.retran_path;
997 ctx->packet = &ctx->transport->packet;
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -0300998
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -0300999 if (list_empty(&ctx->transport->send_ready))
1000 list_add_tail(&ctx->transport->send_ready,
1001 &ctx->transport_list);
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001002
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001003 sctp_packet_config(ctx->packet, ctx->asoc->peer.i.init_tag,
1004 ctx->asoc->peer.ecn_capable);
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001005 }
1006
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001007 error = __sctp_outq_flush_rtx(ctx->q, ctx->packet, rtx_timeout,
1008 &start_timer, ctx->gfp);
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001009 if (error < 0)
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001010 ctx->asoc->base.sk->sk_err = -error;
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001011
1012 if (start_timer) {
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001013 sctp_transport_reset_t3_rtx(ctx->transport);
1014 ctx->transport->last_time_sent = jiffies;
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001015 }
1016
1017 /* This can happen on COOKIE-ECHO resend. Only
1018 * one chunk can get bundled with a COOKIE-ECHO.
1019 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001020 if (ctx->packet->has_cookie_echo)
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001021 return false;
1022
1023 /* Don't send new data if there is still data
1024 * waiting to retransmit.
1025 */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001026 if (!list_empty(&ctx->q->retransmit))
Marcelo Ricardo Leitner96e04182018-05-14 14:34:39 -03001027 return false;
1028
1029 return true;
1030}
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001031
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001032static void sctp_outq_flush_data(struct sctp_flush_ctx *ctx,
1033 int rtx_timeout)
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -03001034{
Marcelo Ricardo Leitner7a0b9df2018-05-14 14:34:38 -03001035 struct sctp_chunk *chunk;
1036 enum sctp_xmit status;
Michio Honda8a07eb02011-04-26 20:19:36 +09001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /* Is it OK to send data chunks? */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001039 switch (ctx->asoc->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 case SCTP_STATE_COOKIE_ECHOED:
1041 /* Only allow bundling when this packet has a COOKIE-ECHO
1042 * chunk.
1043 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001044 if (!ctx->packet || !ctx->packet->has_cookie_echo)
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Gustavo A. R. Silva2cc543f2018-10-03 12:45:56 +02001047 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 case SCTP_STATE_ESTABLISHED:
1049 case SCTP_STATE_SHUTDOWN_PENDING:
1050 case SCTP_STATE_SHUTDOWN_RECEIVED:
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001051 break;
1052
1053 default:
1054 /* Do nothing. */
1055 return;
1056 }
1057
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -03001058 /* RFC 2960 6.1 Transmission of DATA Chunks
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001059 *
1060 * C) When the time comes for the sender to transmit,
1061 * before sending new DATA chunks, the sender MUST
1062 * first transmit any outstanding DATA chunks which
1063 * are marked for retransmission (limited by the
1064 * current cwnd).
1065 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001066 if (!list_empty(&ctx->q->retransmit) &&
1067 !sctp_outq_flush_rtx(ctx, rtx_timeout))
1068 return;
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001069
1070 /* Apply Max.Burst limitation to the current transport in
1071 * case it will be used for new data. We are going to
1072 * rest it before we return, but we want to apply the limit
1073 * to the currently queued data.
1074 */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001075 if (ctx->transport)
1076 sctp_transport_burst_limited(ctx->transport);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001077
1078 /* Finally, transmit new packets. */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001079 while ((chunk = sctp_outq_dequeue_data(ctx->q)) != NULL) {
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001080 __u32 sid = ntohs(chunk->subh.data_hdr->stream);
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001081 __u8 stream_state = SCTP_SO(&ctx->asoc->stream, sid)->state;
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001082
1083 /* Has this chunk expired? */
1084 if (sctp_chunk_abandoned(chunk)) {
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001085 sctp_sched_dequeue_done(ctx->q, chunk);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001086 sctp_chunk_fail(chunk, 0);
1087 sctp_chunk_free(chunk);
1088 continue;
1089 }
1090
Konstantin Khorenko05364ca2018-08-10 20:11:42 +03001091 if (stream_state == SCTP_STREAM_CLOSED) {
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001092 sctp_outq_head_data(ctx->q, chunk);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001093 break;
1094 }
1095
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001096 sctp_outq_select_transport(ctx, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -03001098 pr_debug("%s: outq:%p, chunk:%p[%s], tx-tsn:0x%x skb->head:%p skb->users:%d\n",
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001099 __func__, ctx->q, chunk, chunk && chunk->chunk_hdr ?
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001100 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)) :
1101 "illegal chunk", ntohl(chunk->subh.data_hdr->tsn),
1102 chunk->skb ? chunk->skb->head : NULL, chunk->skb ?
1103 refcount_read(&chunk->skb->users) : -1);
Vlad Yasevich46d5a802009-11-23 15:54:00 -05001104
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001105 /* Add the chunk to the packet. */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001106 status = sctp_packet_transmit_chunk(ctx->packet, chunk, 0,
1107 ctx->gfp);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001108 if (status != SCTP_XMIT_OK) {
1109 /* We could not append this chunk, so put
1110 * the chunk back on the output queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001112 pr_debug("%s: could not transmit tsn:0x%x, status:%d\n",
1113 __func__, ntohl(chunk->subh.data_hdr->tsn),
1114 status);
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001115
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001116 sctp_outq_head_data(ctx->q, chunk);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001120 /* The sender is in the SHUTDOWN-PENDING state,
1121 * The sender MAY set the I-bit in the DATA
1122 * chunk header.
1123 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001124 if (ctx->asoc->state == SCTP_STATE_SHUTDOWN_PENDING)
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001125 chunk->chunk_hdr->flags |= SCTP_DATA_SACK_IMM;
1126 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001127 ctx->asoc->stats.ouodchunks++;
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001128 else
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001129 ctx->asoc->stats.oodchunks++;
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001130
1131 /* Only now it's safe to consider this
1132 * chunk as sent, sched-wise.
1133 */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001134 sctp_sched_dequeue_done(ctx->q, chunk);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001135
1136 list_add_tail(&chunk->transmitted_list,
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001137 &ctx->transport->transmitted);
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001138
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001139 sctp_transport_reset_t3_rtx(ctx->transport);
1140 ctx->transport->last_time_sent = jiffies;
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001141
1142 /* Only let one DATA chunk get bundled with a
1143 * COOKIE-ECHO chunk.
1144 */
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001145 if (ctx->packet->has_cookie_echo)
Marcelo Ricardo Leitner4fdbb0e2018-05-14 14:34:43 -03001146 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001148}
1149
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001150static void sctp_outq_flush_transports(struct sctp_flush_ctx *ctx)
Marcelo Ricardo Leitner4bf21b62018-05-14 14:34:41 -03001151{
1152 struct list_head *ltransport;
1153 struct sctp_packet *packet;
1154 struct sctp_transport *t;
1155 int error = 0;
1156
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001157 while ((ltransport = sctp_list_dequeue(&ctx->transport_list)) != NULL) {
Marcelo Ricardo Leitner4bf21b62018-05-14 14:34:41 -03001158 t = list_entry(ltransport, struct sctp_transport, send_ready);
1159 packet = &t->packet;
1160 if (!sctp_packet_empty(packet)) {
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001161 error = sctp_packet_transmit(packet, ctx->gfp);
Marcelo Ricardo Leitner4bf21b62018-05-14 14:34:41 -03001162 if (error < 0)
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001163 ctx->q->asoc->base.sk->sk_err = -error;
Marcelo Ricardo Leitner4bf21b62018-05-14 14:34:41 -03001164 }
1165
1166 /* Clear the burst limited state, if any */
1167 sctp_transport_burst_reset(t);
1168 }
1169}
1170
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -03001171/* Try to flush an outqueue.
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001172 *
1173 * Description: Send everything in q which we legally can, subject to
1174 * congestion limitations.
1175 * * Note: This function can be called from multiple contexts so appropriate
1176 * locking concerns must be made. Today we use the sock lock to protect
1177 * this function.
1178 */
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001179
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001180static void sctp_outq_flush(struct sctp_outq *q, int rtx_timeout, gfp_t gfp)
1181{
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001182 struct sctp_flush_ctx ctx = {
1183 .q = q,
1184 .transport = NULL,
1185 .transport_list = LIST_HEAD_INIT(ctx.transport_list),
Marcelo Ricardo Leitnere136e962018-05-14 14:35:19 -03001186 .asoc = q->asoc,
1187 .packet = NULL,
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001188 .gfp = gfp,
1189 };
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001190
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -03001191 /* 6.10 Bundling
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001192 * ...
1193 * When bundling control chunks with DATA chunks, an
1194 * endpoint MUST place control chunks first in the outbound
1195 * SCTP packet. The transmitter MUST transmit DATA chunks
1196 * within a SCTP packet in increasing order of TSN.
1197 * ...
1198 */
1199
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001200 sctp_outq_flush_ctrl(&ctx);
Marcelo Ricardo Leitnercb93cc52018-05-14 14:34:40 -03001201
1202 if (q->asoc->src_out_of_asoc_ok)
1203 goto sctp_flush_out;
1204
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001205 sctp_outq_flush_data(&ctx, rtx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207sctp_flush_out:
1208
Marcelo Ricardo Leitnerbb543842018-05-14 14:35:18 -03001209 sctp_outq_flush_transports(&ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
1212/* Update unack_data based on the incoming SACK chunk */
1213static void sctp_sack_update_unack_data(struct sctp_association *assoc,
1214 struct sctp_sackhdr *sack)
1215{
Xin Longafd93b72017-07-23 09:34:31 +08001216 union sctp_sack_variable *frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 __u16 unack_data;
1218 int i;
1219
1220 unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
1221
1222 frags = sack->variable;
1223 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
1224 unack_data -= ((ntohs(frags[i].gab.end) -
1225 ntohs(frags[i].gab.start) + 1));
1226 }
1227
1228 assoc->unack_data = unack_data;
1229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/* This is where we REALLY process a SACK.
1232 *
1233 * Process the SACK against the outqueue. Mostly, this just frees
1234 * things off the transmitted queue.
1235 */
Nicolas Dichteledfee032012-10-03 05:43:22 +00001236int sctp_outq_sack(struct sctp_outq *q, struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct sctp_association *asoc = q->asoc;
Nicolas Dichteledfee032012-10-03 05:43:22 +00001239 struct sctp_sackhdr *sack = chunk->subh.sack_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct sctp_transport *transport;
1241 struct sctp_chunk *tchunk = NULL;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001242 struct list_head *lchunk, *transport_list, *temp;
Xin Longafd93b72017-07-23 09:34:31 +08001243 union sctp_sack_variable *frags = sack->variable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 __u32 sack_ctsn, ctsn, tsn;
1245 __u32 highest_tsn, highest_new_tsn;
1246 __u32 sack_a_rwnd;
Eric Dumazet95c96172012-04-15 05:58:06 +00001247 unsigned int outstanding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 struct sctp_transport *primary = asoc->peer.primary_path;
1249 int count_of_newacks = 0;
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001250 int gap_ack_blocks;
Vlad Yasevichea862c82010-04-30 22:41:10 -04001251 u8 accum_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /* Grab the association's destination address list. */
1254 transport_list = &asoc->peer.transport_addr_list;
1255
1256 sack_ctsn = ntohl(sack->cum_tsn_ack);
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001257 gap_ack_blocks = ntohs(sack->num_gap_ack_blocks);
Michele Baldessari196d6752012-12-01 04:49:42 +00001258 asoc->stats.gapcnt += gap_ack_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * SFR-CACC algorithm:
1261 * On receipt of a SACK the sender SHOULD execute the
1262 * following statements.
1263 *
1264 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1265 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1266 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1267 * all destinations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1269 * is set the receiver of the SACK MUST take the following actions:
1270 *
1271 * A) Initialize the cacc_saw_newack to 0 for all destination
1272 * addresses.
Vlad Yasevichab5216a2008-06-19 18:17:24 -04001273 *
1274 * Only bother if changeover_active is set. Otherwise, this is
1275 * totally suboptimal to do on every SACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Vlad Yasevichab5216a2008-06-19 18:17:24 -04001277 if (primary->cacc.changeover_active) {
1278 u8 clear_cycling = 0;
1279
1280 if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1281 primary->cacc.changeover_active = 0;
1282 clear_cycling = 1;
1283 }
1284
1285 if (clear_cycling || gap_ack_blocks) {
1286 list_for_each_entry(transport, transport_list,
1287 transports) {
1288 if (clear_cycling)
1289 transport->cacc.cycling_changeover = 0;
1290 if (gap_ack_blocks)
1291 transport->cacc.cacc_saw_newack = 0;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 }
1295
1296 /* Get the highest TSN in the sack. */
1297 highest_tsn = sack_ctsn;
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001298 if (gap_ack_blocks)
1299 highest_tsn += ntohs(frags[gap_ack_blocks - 1].gab.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Vlad Yasevichbfa0d982010-04-30 22:41:10 -04001301 if (TSN_lt(asoc->highest_sacked, highest_tsn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 asoc->highest_sacked = highest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Vlad Yasevichbfa0d982010-04-30 22:41:10 -04001304 highest_new_tsn = sack_ctsn;
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* Run through the retransmit queue. Credit bytes received
1307 * and free those chunks that we can.
1308 */
Nicolas Dichteledfee032012-10-03 05:43:22 +00001309 sctp_check_transmitted(q, &q->retransmit, NULL, NULL, sack, &highest_new_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 /* Run through the transmitted queue.
1312 * Credit bytes received and free those chunks which we can.
1313 *
1314 * This is a MASSIVE candidate for optimization.
1315 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001316 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 sctp_check_transmitted(q, &transport->transmitted,
Nicolas Dichteledfee032012-10-03 05:43:22 +00001318 transport, &chunk->source, sack,
1319 &highest_new_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * SFR-CACC algorithm:
1322 * C) Let count_of_newacks be the number of
1323 * destinations for which cacc_saw_newack is set.
1324 */
1325 if (transport->cacc.cacc_saw_newack)
wangweidongcb3f8372013-12-23 12:16:50 +08001326 count_of_newacks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Vlad Yasevichea862c82010-04-30 22:41:10 -04001329 /* Move the Cumulative TSN Ack Point if appropriate. */
1330 if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn)) {
1331 asoc->ctsn_ack_point = sack_ctsn;
1332 accum_moved = 1;
1333 }
1334
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001335 if (gap_ack_blocks) {
Vlad Yasevichea862c82010-04-30 22:41:10 -04001336
1337 if (asoc->fast_recovery && accum_moved)
1338 highest_new_tsn = highest_tsn;
1339
Vlad Yasevich2cd9b822008-06-19 17:59:13 -04001340 list_for_each_entry(transport, transport_list, transports)
1341 sctp_mark_missing(q, &transport->transmitted, transport,
1342 highest_new_tsn, count_of_newacks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Update unack_data field in the assoc. */
1346 sctp_sack_update_unack_data(asoc, sack);
1347
1348 ctsn = asoc->ctsn_ack_point;
1349
1350 /* Throw away stuff rotting on the sack queue. */
1351 list_for_each_safe(lchunk, temp, &q->sacked) {
1352 tchunk = list_entry(lchunk, struct sctp_chunk,
1353 transmitted_list);
1354 tsn = ntohl(tchunk->subh.data_hdr->tsn);
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05001355 if (TSN_lte(tsn, ctsn)) {
1356 list_del_init(&tchunk->transmitted_list);
Xin Longbe4947b2016-09-29 02:37:28 +08001357 if (asoc->peer.prsctp_capable &&
Xin Long8dbdf1f2016-07-09 19:47:45 +08001358 SCTP_PR_PRIO_ENABLED(chunk->sinfo.sinfo_flags))
1359 asoc->sent_cnt_removable--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 sctp_chunk_free(tchunk);
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1365 * number of bytes still outstanding after processing the
1366 * Cumulative TSN Ack and the Gap Ack Blocks.
1367 */
1368
1369 sack_a_rwnd = ntohl(sack->a_rwnd);
lucien8a0d19c2015-12-05 15:35:36 +08001370 asoc->peer.zero_window_announced = !sack_a_rwnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 outstanding = q->outstanding_bytes;
1372
1373 if (outstanding < sack_a_rwnd)
1374 sack_a_rwnd -= outstanding;
1375 else
1376 sack_a_rwnd = 0;
1377
1378 asoc->peer.rwnd = sack_a_rwnd;
1379
Xin Long8e0c3b72017-12-15 00:41:26 +08001380 asoc->stream.si->generate_ftsn(q, sack_ctsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Daniel Borkmannbb333812013-06-28 19:49:40 +02001382 pr_debug("%s: sack cumulative tsn ack:0x%x\n", __func__, sack_ctsn);
1383 pr_debug("%s: cumulative tsn ack of assoc:%p is 0x%x, "
1384 "advertised peer ack point:0x%x\n", __func__, asoc, ctsn,
1385 asoc->adv_peer_ack_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Vlad Yasevich619a60e2014-01-02 14:39:44 -05001387 return sctp_outq_is_empty(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Vlad Yasevich619a60e2014-01-02 14:39:44 -05001390/* Is the outqueue empty?
1391 * The queue is empty when we have not pending data, no in-flight data
1392 * and nothing pending retransmissions.
1393 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394int sctp_outq_is_empty(const struct sctp_outq *q)
1395{
Vlad Yasevich619a60e2014-01-02 14:39:44 -05001396 return q->out_qlen == 0 && q->outstanding_bytes == 0 &&
1397 list_empty(&q->retransmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400/********************************************************************
1401 * 2nd Level Abstractions
1402 ********************************************************************/
1403
1404/* Go through a transport's transmitted list or the association's retransmit
1405 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1406 * The retransmit list will not have an associated transport.
1407 *
1408 * I added coherent debug information output. --xguo
1409 *
1410 * Instead of printing 'sacked' or 'kept' for each TSN on the
1411 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1412 * KEPT TSN6-TSN7, etc.
1413 */
1414static void sctp_check_transmitted(struct sctp_outq *q,
1415 struct list_head *transmitted_queue,
1416 struct sctp_transport *transport,
Nicolas Dichteledfee032012-10-03 05:43:22 +00001417 union sctp_addr *saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct sctp_sackhdr *sack,
Vlad Yasevichbfa0d982010-04-30 22:41:10 -04001419 __u32 *highest_new_tsn_in_sack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 struct list_head *lchunk;
1422 struct sctp_chunk *tchunk;
1423 struct list_head tlist;
1424 __u32 tsn;
1425 __u32 sack_ctsn;
1426 __u32 rtt;
1427 __u8 restart_timer = 0;
1428 int bytes_acked = 0;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001429 int migrate_bytes = 0;
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001430 bool forward_progress = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 sack_ctsn = ntohl(sack->cum_tsn_ack);
1433
1434 INIT_LIST_HEAD(&tlist);
1435
1436 /* The while loop will skip empty transmitted queues. */
1437 while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1438 tchunk = list_entry(lchunk, struct sctp_chunk,
1439 transmitted_list);
1440
1441 if (sctp_chunk_abandoned(tchunk)) {
1442 /* Move the chunk to abandoned list. */
1443 sctp_insert_list(&q->abandoned, lchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -08001444
1445 /* If this chunk has not been acked, stop
1446 * considering it as 'outstanding'.
1447 */
Xin Longd30fc512017-11-25 21:18:34 +08001448 if (transmitted_queue != &q->retransmit &&
1449 !tchunk->tsn_gap_acked) {
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001450 if (tchunk->transport)
1451 tchunk->transport->flight_size -=
1452 sctp_data_size(tchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -08001453 q->outstanding_bytes -= sctp_data_size(tchunk);
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 continue;
1456 }
1457
1458 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1459 if (sctp_acked(sack, tsn)) {
1460 /* If this queue is the retransmit queue, the
1461 * retransmit timer has already reclaimed
1462 * the outstanding bytes for this chunk, so only
1463 * count bytes associated with a transport.
1464 */
Marcelo Ricardo Leitner51446782018-04-24 18:17:35 -03001465 if (transport && !tchunk->tsn_gap_acked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* If this chunk is being used for RTT
1467 * measurement, calculate the RTT and update
1468 * the RTO using this value.
1469 *
1470 * 6.3.1 C5) Karn's algorithm: RTT measurements
1471 * MUST NOT be made using packets that were
1472 * retransmitted (and thus for which it is
1473 * ambiguous whether the reply was for the
1474 * first instance of the packet or a later
1475 * instance).
1476 */
Marcelo Ricardo Leitner51446782018-04-24 18:17:35 -03001477 if (!sctp_chunk_retransmitted(tchunk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 tchunk->rtt_in_progress) {
Vlad Yasevich4c9f5d52006-06-17 22:56:08 -07001479 tchunk->rtt_in_progress = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 rtt = jiffies - tchunk->sent_at;
1481 sctp_transport_update_rto(transport,
1482 rtt);
1483 }
Marcelo Ricardo Leitner51446782018-04-24 18:17:35 -03001484
1485 if (TSN_lte(tsn, sack_ctsn)) {
1486 /*
1487 * SFR-CACC algorithm:
1488 * 2) If the SACK contains gap acks
1489 * and the flag CHANGEOVER_ACTIVE is
1490 * set the receiver of the SACK MUST
1491 * take the following action:
1492 *
1493 * B) For each TSN t being acked that
1494 * has not been acked in any SACK so
1495 * far, set cacc_saw_newack to 1 for
1496 * the destination that the TSN was
1497 * sent to.
1498 */
1499 if (sack->num_gap_ack_blocks &&
1500 q->asoc->peer.primary_path->cacc.
1501 changeover_active)
1502 transport->cacc.cacc_saw_newack
1503 = 1;
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001506
1507 /* If the chunk hasn't been marked as ACKED,
1508 * mark it and account bytes_acked if the
1509 * chunk had a valid transport (it will not
1510 * have a transport if ASCONF had deleted it
1511 * while DATA was outstanding).
1512 */
1513 if (!tchunk->tsn_gap_acked) {
1514 tchunk->tsn_gap_acked = 1;
Chang Xiangzhongd6c41612013-11-21 22:56:28 +01001515 if (TSN_lt(*highest_new_tsn_in_sack, tsn))
1516 *highest_new_tsn_in_sack = tsn;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001517 bytes_acked += sctp_data_size(tchunk);
1518 if (!tchunk->transport)
1519 migrate_bytes += sctp_data_size(tchunk);
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001520 forward_progress = true;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001521 }
1522
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001523 if (TSN_lte(tsn, sack_ctsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* RFC 2960 6.3.2 Retransmission Timer Rules
1525 *
1526 * R3) Whenever a SACK is received
1527 * that acknowledges the DATA chunk
1528 * with the earliest outstanding TSN
1529 * for that address, restart T3-rtx
1530 * timer for that address with its
1531 * current RTO.
1532 */
1533 restart_timer = 1;
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001534 forward_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 list_add_tail(&tchunk->transmitted_list,
1537 &q->sacked);
1538 } else {
1539 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1540 * M2) Each time a SACK arrives reporting
1541 * 'Stray DATA chunk(s)' record the highest TSN
1542 * reported as newly acknowledged, call this
1543 * value 'HighestTSNinSack'. A newly
1544 * acknowledged DATA chunk is one not
1545 * previously acknowledged in a SACK.
1546 *
1547 * When the SCTP sender of data receives a SACK
1548 * chunk that acknowledges, for the first time,
1549 * the receipt of a DATA chunk, all the still
1550 * unacknowledged DATA chunks whose TSN is
1551 * older than that newly acknowledged DATA
1552 * chunk, are qualified as 'Stray DATA chunks'.
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 list_add_tail(lchunk, &tlist);
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else {
1557 if (tchunk->tsn_gap_acked) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001558 pr_debug("%s: receiver reneged on data TSN:0x%x\n",
1559 __func__, tsn);
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 tchunk->tsn_gap_acked = 0;
1562
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001563 if (tchunk->transport)
1564 bytes_acked -= sctp_data_size(tchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* RFC 2960 6.3.2 Retransmission Timer Rules
1567 *
1568 * R4) Whenever a SACK is received missing a
1569 * TSN that was previously acknowledged via a
1570 * Gap Ack Block, start T3-rtx for the
1571 * destination address to which the DATA
1572 * chunk was originally
1573 * transmitted if it is not already running.
1574 */
1575 restart_timer = 1;
1576 }
1577
1578 list_add_tail(lchunk, &tlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 }
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (transport) {
1583 if (bytes_acked) {
Thomas Graff8d96052011-07-07 00:28:35 +00001584 struct sctp_association *asoc = transport->asoc;
1585
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001586 /* We may have counted DATA that was migrated
1587 * to this transport due to DEL-IP operation.
1588 * Subtract those bytes, since the were never
1589 * send on this transport and shouldn't be
1590 * credited to this transport.
1591 */
1592 bytes_acked -= migrate_bytes;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* 8.2. When an outstanding TSN is acknowledged,
1595 * the endpoint shall clear the error counter of
1596 * the destination transport address to which the
1597 * DATA chunk was last sent.
1598 * The association's overall error counter is
1599 * also cleared.
1600 */
1601 transport->error_count = 0;
1602 transport->asoc->overall_error_count = 0;
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001603 forward_progress = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Thomas Graff8d96052011-07-07 00:28:35 +00001605 /*
1606 * While in SHUTDOWN PENDING, we may have started
1607 * the T5 shutdown guard timer after reaching the
1608 * retransmission limit. Stop that timer as soon
1609 * as the receiver acknowledged any data.
1610 */
1611 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING &&
1612 del_timer(&asoc->timers
1613 [SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]))
1614 sctp_association_put(asoc);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* Mark the destination transport address as
1617 * active if it is not so marked.
1618 */
Nicolas Dichteledfee032012-10-03 05:43:22 +00001619 if ((transport->state == SCTP_INACTIVE ||
1620 transport->state == SCTP_UNCONFIRMED) &&
1621 sctp_cmp_addr_exact(&transport->ipaddr, saddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 sctp_assoc_control_transport(
1623 transport->asoc,
1624 transport,
1625 SCTP_TRANSPORT_UP,
1626 SCTP_RECEIVED_SACK);
1627 }
1628
1629 sctp_transport_raise_cwnd(transport, sack_ctsn,
1630 bytes_acked);
1631
1632 transport->flight_size -= bytes_acked;
Gui Jianfeng8b73a072008-04-17 14:22:18 -07001633 if (transport->flight_size == 0)
1634 transport->partial_bytes_acked = 0;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04001635 q->outstanding_bytes -= bytes_acked + migrate_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 } else {
1637 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1638 * When a sender is doing zero window probing, it
1639 * should not timeout the association if it continues
1640 * to receive new packets from the receiver. The
1641 * reason is that the receiver MAY keep its window
1642 * closed for an indefinite time.
1643 * A sender is doing zero window probing when the
1644 * receiver's advertised window is zero, and there is
1645 * only one data chunk in flight to the receiver.
Thomas Graff8d96052011-07-07 00:28:35 +00001646 *
1647 * Allow the association to timeout while in SHUTDOWN
1648 * PENDING or SHUTDOWN RECEIVED in case the receiver
1649 * stays in zero window mode forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651 if (!q->asoc->peer.rwnd &&
1652 !list_empty(&tlist) &&
Thomas Graff8d96052011-07-07 00:28:35 +00001653 (sack_ctsn+2 == q->asoc->next_tsn) &&
1654 q->asoc->state < SCTP_STATE_SHUTDOWN_PENDING) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001655 pr_debug("%s: sack received for zero window "
1656 "probe:%u\n", __func__, sack_ctsn);
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 q->asoc->overall_error_count = 0;
1659 transport->error_count = 0;
1660 }
1661 }
1662
1663 /* RFC 2960 6.3.2 Retransmission Timer Rules
1664 *
1665 * R2) Whenever all outstanding data sent to an address have
1666 * been acknowledged, turn off the T3-rtx timer of that
1667 * address.
1668 */
1669 if (!transport->flight_size) {
Ying Xue25cc4ae2013-02-03 20:32:57 +00001670 if (del_timer(&transport->T3_rtx_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 sctp_transport_put(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 } else if (restart_timer) {
1673 if (!mod_timer(&transport->T3_rtx_timer,
1674 jiffies + transport->rto))
1675 sctp_transport_hold(transport);
1676 }
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001677
1678 if (forward_progress) {
1679 if (transport->dst)
Julian Anastasovc86a7732017-02-06 23:14:13 +02001680 sctp_transport_dst_confirm(transport);
Daniel Borkmann8c2f4142013-07-09 16:17:04 +02001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683
1684 list_splice(&tlist, transmitted_queue);
1685}
1686
1687/* Mark chunks as missing and consequently may get retransmitted. */
1688static void sctp_mark_missing(struct sctp_outq *q,
1689 struct list_head *transmitted_queue,
1690 struct sctp_transport *transport,
1691 __u32 highest_new_tsn_in_sack,
1692 int count_of_newacks)
1693{
1694 struct sctp_chunk *chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 __u32 tsn;
1696 char do_fast_retransmit = 0;
Vlad Yasevichea862c82010-04-30 22:41:10 -04001697 struct sctp_association *asoc = q->asoc;
1698 struct sctp_transport *primary = asoc->peer.primary_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001700 list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 tsn = ntohl(chunk->subh.data_hdr->tsn);
1703
1704 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1705 * 'Unacknowledged TSN's', if the TSN number of an
1706 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1707 * value, increment the 'TSN.Missing.Report' count on that
1708 * chunk if it has NOT been fast retransmitted or marked for
1709 * fast retransmit already.
1710 */
Neil Hormanc226ef92008-07-25 12:44:09 -04001711 if (chunk->fast_retransmit == SCTP_CAN_FRTX &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 !chunk->tsn_gap_acked &&
1713 TSN_lt(tsn, highest_new_tsn_in_sack)) {
1714
1715 /* SFR-CACC may require us to skip marking
1716 * this chunk as missing.
1717 */
Vlad Yasevichf246a7b2011-04-18 19:13:56 +00001718 if (!transport || !sctp_cacc_skip(primary,
1719 chunk->transport,
1720 count_of_newacks, tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 chunk->tsn_missing_report++;
1722
Daniel Borkmannbb333812013-06-28 19:49:40 +02001723 pr_debug("%s: tsn:0x%x missing counter:%d\n",
1724 __func__, tsn, chunk->tsn_missing_report);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 }
1727 /*
1728 * M4) If any DATA chunk is found to have a
1729 * 'TSN.Missing.Report'
Vlad Yasevich27852c22006-02-02 16:57:31 -08001730 * value larger than or equal to 3, mark that chunk for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * retransmission and start the fast retransmit procedure.
1732 */
1733
Vlad Yasevich27852c22006-02-02 16:57:31 -08001734 if (chunk->tsn_missing_report >= 3) {
Neil Hormanc226ef92008-07-25 12:44:09 -04001735 chunk->fast_retransmit = SCTP_NEED_FRTX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 do_fast_retransmit = 1;
1737 }
1738 }
1739
1740 if (transport) {
1741 if (do_fast_retransmit)
1742 sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1743
Daniel Borkmannbb333812013-06-28 19:49:40 +02001744 pr_debug("%s: transport:%p, cwnd:%d, ssthresh:%d, "
1745 "flight_size:%d, pba:%d\n", __func__, transport,
1746 transport->cwnd, transport->ssthresh,
1747 transport->flight_size, transport->partial_bytes_acked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749}
1750
1751/* Is the given TSN acked by this packet? */
1752static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 __u32 ctsn = ntohl(sack->cum_tsn_ack);
Xin Longafd93b72017-07-23 09:34:31 +08001755 union sctp_sack_variable *frags;
1756 __u16 tsn_offset, blocks;
1757 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001759 if (TSN_lte(tsn, ctsn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto pass;
1761
Marcelo Ricardo Leitner5884f352018-05-14 14:35:20 -03001762 /* 3.3.4 Selective Acknowledgment (SACK) (3):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 *
1764 * Gap Ack Blocks:
1765 * These fields contain the Gap Ack Blocks. They are repeated
1766 * for each Gap Ack Block up to the number of Gap Ack Blocks
1767 * defined in the Number of Gap Ack Blocks field. All DATA
1768 * chunks with TSNs greater than or equal to (Cumulative TSN
1769 * Ack + Gap Ack Block Start) and less than or equal to
1770 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1771 * Block are assumed to have been received correctly.
1772 */
1773
1774 frags = sack->variable;
Marcelo Ricardo Leitnera3007442016-09-20 18:19:13 -03001775 blocks = ntohs(sack->num_gap_ack_blocks);
1776 tsn_offset = tsn - ctsn;
1777 for (i = 0; i < blocks; ++i) {
1778 if (tsn_offset >= ntohs(frags[i].gab.start) &&
1779 tsn_offset <= ntohs(frags[i].gab.end))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto pass;
1781 }
1782
1783 return 0;
1784pass:
1785 return 1;
1786}
1787
1788static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
Al Viro9f81bcd2006-11-20 17:26:34 -08001789 int nskips, __be16 stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 int i;
1792
1793 for (i = 0; i < nskips; i++) {
1794 if (skiplist[i].stream == stream)
1795 return i;
1796 }
1797 return i;
1798}
1799
1800/* Create and add a fwdtsn chunk to the outq's control queue if needed. */
Xin Long8e0c3b72017-12-15 00:41:26 +08001801void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 struct sctp_association *asoc = q->asoc;
1804 struct sctp_chunk *ftsn_chunk = NULL;
1805 struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1806 int nskips = 0;
1807 int skip_pos = 0;
1808 __u32 tsn;
1809 struct sctp_chunk *chunk;
1810 struct list_head *lchunk, *temp;
1811
Wei Yongjun76595022009-03-12 09:49:19 +00001812 if (!asoc->peer.prsctp_capable)
1813 return;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1816 * received SACK.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001817 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1819 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1820 */
1821 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1822 asoc->adv_peer_ack_point = ctsn;
1823
1824 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1825 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1826 * the chunk next in the out-queue space is marked as "abandoned" as
1827 * shown in the following example:
1828 *
1829 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1830 * and the Advanced.Peer.Ack.Point is updated to this value:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001831 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1833 * normal SACK processing local advancement
1834 * ... ...
1835 * Adv.Ack.Pt-> 102 acked 102 acked
1836 * 103 abandoned 103 abandoned
1837 * 104 abandoned Adv.Ack.P-> 104 abandoned
1838 * 105 105
1839 * 106 acked 106 acked
1840 * ... ...
1841 *
1842 * In this example, the data sender successfully advanced the
1843 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1844 */
1845 list_for_each_safe(lchunk, temp, &q->abandoned) {
1846 chunk = list_entry(lchunk, struct sctp_chunk,
1847 transmitted_list);
1848 tsn = ntohl(chunk->subh.data_hdr->tsn);
1849
1850 /* Remove any chunks in the abandoned queue that are acked by
1851 * the ctsn.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (TSN_lte(tsn, ctsn)) {
1854 list_del_init(lchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 sctp_chunk_free(chunk);
1856 } else {
1857 if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1858 asoc->adv_peer_ack_point = tsn;
1859 if (chunk->chunk_hdr->flags &
1860 SCTP_DATA_UNORDERED)
1861 continue;
1862 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1863 nskips,
1864 chunk->subh.data_hdr->stream);
1865 ftsn_skip_arr[skip_pos].stream =
1866 chunk->subh.data_hdr->stream;
1867 ftsn_skip_arr[skip_pos].ssn =
1868 chunk->subh.data_hdr->ssn;
1869 if (skip_pos == nskips)
1870 nskips++;
1871 if (nskips == 10)
1872 break;
1873 } else
1874 break;
1875 }
1876 }
1877
1878 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1879 * is greater than the Cumulative TSN ACK carried in the received
1880 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1881 * chunk containing the latest value of the
1882 * "Advanced.Peer.Ack.Point".
1883 *
1884 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1885 * list each stream and sequence number in the forwarded TSN. This
1886 * information will enable the receiver to easily find any
1887 * stranded TSN's waiting on stream reorder queues. Each stream
1888 * SHOULD only be reported once; this means that if multiple
1889 * abandoned messages occur in the same stream then only the
1890 * highest abandoned stream sequence number is reported. If the
1891 * total size of the FORWARD TSN does NOT fit in a single MTU then
1892 * the sender of the FORWARD TSN SHOULD lower the
1893 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1894 * single MTU.
1895 */
1896 if (asoc->adv_peer_ack_point > ctsn)
1897 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001898 nskips, &ftsn_skip_arr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if (ftsn_chunk) {
David S. Miller79af02c2005-07-08 21:47:49 -07001901 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001902 SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904}