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