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