blob: a7ba9e146dff9e3ba1c7de3a4cda1d74b06dc694 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 Intel Corp.
6 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05007 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * These functions implement the sctp_outq class. The outqueue handles
10 * bundling and queueing of outgoing SCTP chunks.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
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
49#include <linux/types.h>
50#include <linux/list.h> /* For struct list_head */
51#include <linux/socket.h>
52#include <linux/ip.h>
53#include <net/sock.h> /* For skb_set_owner_w */
54
55#include <net/sctp/sctp.h>
56#include <net/sctp/sm.h>
57
58/* Declare internal functions here. */
59static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
60static void sctp_check_transmitted(struct sctp_outq *q,
61 struct list_head *transmitted_queue,
62 struct sctp_transport *transport,
63 struct sctp_sackhdr *sack,
64 __u32 highest_new_tsn);
65
66static void sctp_mark_missing(struct sctp_outq *q,
67 struct list_head *transmitted_queue,
68 struct sctp_transport *transport,
69 __u32 highest_new_tsn,
70 int count_of_newacks);
71
72static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 sack_ctsn);
73
74/* Add data to the front of the queue. */
75static inline void sctp_outq_head_data(struct sctp_outq *q,
76 struct sctp_chunk *ch)
77{
David S. Miller79af02c2005-07-08 21:47:49 -070078 list_add(&ch->list, &q->out_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 q->out_qlen += ch->skb->len;
80 return;
81}
82
83/* Take data from the front of the queue. */
84static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
85{
David S. Miller79af02c2005-07-08 21:47:49 -070086 struct sctp_chunk *ch = NULL;
87
88 if (!list_empty(&q->out_chunk_list)) {
89 struct list_head *entry = q->out_chunk_list.next;
90
91 ch = list_entry(entry, struct sctp_chunk, list);
92 list_del_init(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 q->out_qlen -= ch->skb->len;
David S. Miller79af02c2005-07-08 21:47:49 -070094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return ch;
96}
97/* Add data chunk to the end of the queue. */
98static inline void sctp_outq_tail_data(struct sctp_outq *q,
99 struct sctp_chunk *ch)
100{
David S. Miller79af02c2005-07-08 21:47:49 -0700101 list_add_tail(&ch->list, &q->out_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 q->out_qlen += ch->skb->len;
103 return;
104}
105
106/*
107 * SFR-CACC algorithm:
108 * D) If count_of_newacks is greater than or equal to 2
109 * and t was not sent to the current primary then the
110 * sender MUST NOT increment missing report count for t.
111 */
112static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
113 struct sctp_transport *transport,
114 int count_of_newacks)
115{
116 if (count_of_newacks >=2 && transport != primary)
117 return 1;
118 return 0;
119}
120
121/*
122 * SFR-CACC algorithm:
123 * F) If count_of_newacks is less than 2, let d be the
124 * destination to which t was sent. If cacc_saw_newack
125 * is 0 for destination d, then the sender MUST NOT
126 * increment missing report count for t.
127 */
128static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
129 int count_of_newacks)
130{
131 if (count_of_newacks < 2 && !transport->cacc.cacc_saw_newack)
132 return 1;
133 return 0;
134}
135
136/*
137 * SFR-CACC algorithm:
138 * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
139 * execute steps C, D, F.
140 *
141 * C has been implemented in sctp_outq_sack
142 */
143static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
144 struct sctp_transport *transport,
145 int count_of_newacks)
146{
147 if (!primary->cacc.cycling_changeover) {
148 if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
149 return 1;
150 if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
151 return 1;
152 return 0;
153 }
154 return 0;
155}
156
157/*
158 * SFR-CACC algorithm:
159 * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
160 * than next_tsn_at_change of the current primary, then
161 * the sender MUST NOT increment missing report count
162 * for t.
163 */
164static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
165{
166 if (primary->cacc.cycling_changeover &&
167 TSN_lt(tsn, primary->cacc.next_tsn_at_change))
168 return 1;
169 return 0;
170}
171
172/*
173 * SFR-CACC algorithm:
174 * 3) If the missing report count for TSN t is to be
175 * incremented according to [RFC2960] and
176 * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
177 * then the sender MUST futher execute steps 3.1 and
178 * 3.2 to determine if the missing report count for
179 * TSN t SHOULD NOT be incremented.
180 *
181 * 3.3) If 3.1 and 3.2 do not dictate that the missing
182 * report count for t should not be incremented, then
183 * the sender SOULD increment missing report count for
184 * t (according to [RFC2960] and [SCTP_STEWART_2002]).
185 */
186static inline int sctp_cacc_skip(struct sctp_transport *primary,
187 struct sctp_transport *transport,
188 int count_of_newacks,
189 __u32 tsn)
190{
191 if (primary->cacc.changeover_active &&
192 (sctp_cacc_skip_3_1(primary, transport, count_of_newacks)
193 || sctp_cacc_skip_3_2(primary, tsn)))
194 return 1;
195 return 0;
196}
197
198/* Initialize an existing sctp_outq. This does the boring stuff.
199 * You still need to define handlers if you really want to DO
200 * something with this structure...
201 */
202void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
203{
204 q->asoc = asoc;
David S. Miller79af02c2005-07-08 21:47:49 -0700205 INIT_LIST_HEAD(&q->out_chunk_list);
206 INIT_LIST_HEAD(&q->control_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 INIT_LIST_HEAD(&q->retransmit);
208 INIT_LIST_HEAD(&q->sacked);
209 INIT_LIST_HEAD(&q->abandoned);
210
211 q->outstanding_bytes = 0;
212 q->empty = 1;
213 q->cork = 0;
214
215 q->malloced = 0;
216 q->out_qlen = 0;
217}
218
219/* Free the outqueue structure and any related pending chunks.
220 */
221void sctp_outq_teardown(struct sctp_outq *q)
222{
223 struct sctp_transport *transport;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700224 struct list_head *lchunk, *temp;
David S. Miller79af02c2005-07-08 21:47:49 -0700225 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* Throw away unacknowledged chunks. */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700228 list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
229 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
231 chunk = list_entry(lchunk, struct sctp_chunk,
232 transmitted_list);
233 /* Mark as part of a failed message. */
234 sctp_chunk_fail(chunk, q->error);
235 sctp_chunk_free(chunk);
236 }
237 }
238
239 /* Throw away chunks that have been gap ACKed. */
240 list_for_each_safe(lchunk, temp, &q->sacked) {
241 list_del_init(lchunk);
242 chunk = list_entry(lchunk, struct sctp_chunk,
243 transmitted_list);
244 sctp_chunk_fail(chunk, q->error);
245 sctp_chunk_free(chunk);
246 }
247
248 /* Throw away any chunks in the retransmit queue. */
249 list_for_each_safe(lchunk, temp, &q->retransmit) {
250 list_del_init(lchunk);
251 chunk = list_entry(lchunk, struct sctp_chunk,
252 transmitted_list);
253 sctp_chunk_fail(chunk, q->error);
254 sctp_chunk_free(chunk);
255 }
256
257 /* Throw away any chunks that are in the abandoned queue. */
258 list_for_each_safe(lchunk, temp, &q->abandoned) {
259 list_del_init(lchunk);
260 chunk = list_entry(lchunk, struct sctp_chunk,
261 transmitted_list);
262 sctp_chunk_fail(chunk, q->error);
263 sctp_chunk_free(chunk);
264 }
265
266 /* Throw away any leftover data chunks. */
267 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
268
269 /* Mark as send failure. */
270 sctp_chunk_fail(chunk, q->error);
271 sctp_chunk_free(chunk);
272 }
273
274 q->error = 0;
275
276 /* Throw away any leftover control chunks. */
David S. Miller79af02c2005-07-08 21:47:49 -0700277 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
278 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sctp_chunk_free(chunk);
David S. Miller79af02c2005-07-08 21:47:49 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/* Free the outqueue structure and any related pending chunks. */
284void sctp_outq_free(struct sctp_outq *q)
285{
286 /* Throw away leftover chunks. */
287 sctp_outq_teardown(q);
288
289 /* If we were kmalloc()'d, free the memory. */
290 if (q->malloced)
291 kfree(q);
292}
293
294/* Put a new chunk in an sctp_outq. */
295int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
296{
297 int error = 0;
298
299 SCTP_DEBUG_PRINTK("sctp_outq_tail(%p, %p[%s])\n",
300 q, chunk, chunk && chunk->chunk_hdr ?
301 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
302 : "Illegal Chunk");
303
304 /* If it is data, queue it up, otherwise, send it
305 * immediately.
306 */
307 if (SCTP_CID_DATA == chunk->chunk_hdr->type) {
308 /* Is it OK to queue data chunks? */
309 /* From 9. Termination of Association
310 *
311 * When either endpoint performs a shutdown, the
312 * association on each peer will stop accepting new
313 * data from its user and only deliver data in queue
314 * at the time of sending or receiving the SHUTDOWN
315 * chunk.
316 */
317 switch (q->asoc->state) {
318 case SCTP_STATE_EMPTY:
319 case SCTP_STATE_CLOSED:
320 case SCTP_STATE_SHUTDOWN_PENDING:
321 case SCTP_STATE_SHUTDOWN_SENT:
322 case SCTP_STATE_SHUTDOWN_RECEIVED:
323 case SCTP_STATE_SHUTDOWN_ACK_SENT:
324 /* Cannot send after transport endpoint shutdown */
325 error = -ESHUTDOWN;
326 break;
327
328 default:
329 SCTP_DEBUG_PRINTK("outqueueing (%p, %p[%s])\n",
330 q, chunk, chunk && chunk->chunk_hdr ?
331 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
332 : "Illegal Chunk");
333
334 sctp_outq_tail_data(q, chunk);
335 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
336 SCTP_INC_STATS(SCTP_MIB_OUTUNORDERCHUNKS);
337 else
338 SCTP_INC_STATS(SCTP_MIB_OUTORDERCHUNKS);
339 q->empty = 0;
340 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 } else {
David S. Miller79af02c2005-07-08 21:47:49 -0700343 list_add_tail(&chunk->list, &q->control_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
345 }
346
347 if (error < 0)
348 return error;
349
350 if (!q->cork)
351 error = sctp_outq_flush(q, 0);
352
353 return error;
354}
355
356/* Insert a chunk into the sorted list based on the TSNs. The retransmit list
357 * and the abandoned list are in ascending order.
358 */
359static void sctp_insert_list(struct list_head *head, struct list_head *new)
360{
361 struct list_head *pos;
362 struct sctp_chunk *nchunk, *lchunk;
363 __u32 ntsn, ltsn;
364 int done = 0;
365
366 nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
367 ntsn = ntohl(nchunk->subh.data_hdr->tsn);
368
369 list_for_each(pos, head) {
370 lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
371 ltsn = ntohl(lchunk->subh.data_hdr->tsn);
372 if (TSN_lt(ntsn, ltsn)) {
373 list_add(new, pos->prev);
374 done = 1;
375 break;
376 }
377 }
378 if (!done)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900379 list_add_tail(new, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/* Mark all the eligible packets on a transport for retransmission. */
383void sctp_retransmit_mark(struct sctp_outq *q,
384 struct sctp_transport *transport,
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400385 __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct list_head *lchunk, *ltemp;
388 struct sctp_chunk *chunk;
389
390 /* Walk through the specified transmitted queue. */
391 list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
392 chunk = list_entry(lchunk, struct sctp_chunk,
393 transmitted_list);
394
395 /* If the chunk is abandoned, move it to abandoned list. */
396 if (sctp_chunk_abandoned(chunk)) {
397 list_del_init(lchunk);
398 sctp_insert_list(&q->abandoned, lchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -0800399
400 /* If this chunk has not been previousely acked,
401 * stop considering it 'outstanding'. Our peer
402 * will most likely never see it since it will
403 * not be retransmitted
404 */
405 if (!chunk->tsn_gap_acked) {
406 chunk->transport->flight_size -=
407 sctp_data_size(chunk);
408 q->outstanding_bytes -= sctp_data_size(chunk);
409 q->asoc->peer.rwnd += (sctp_data_size(chunk) +
410 sizeof(struct sk_buff));
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 continue;
413 }
414
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400415 /* If we are doing retransmission due to a timeout or pmtu
416 * discovery, only the chunks that are not yet acked should
417 * be added to the retransmit queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400419 if ((reason == SCTP_RTXR_FAST_RTX &&
420 (chunk->fast_retransmit > 0)) ||
421 (reason != SCTP_RTXR_FAST_RTX && !chunk->tsn_gap_acked)) {
Vlad Yasevichd0ce9292007-08-24 19:37:46 +0900422 /* If this chunk was sent less then 1 rto ago, do not
423 * retransmit this chunk, but give the peer time
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400424 * to acknowlege it. Do this only when
425 * retransmitting due to T3 timeout.
Vlad Yasevichd0ce9292007-08-24 19:37:46 +0900426 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400427 if (reason == SCTP_RTXR_T3_RTX &&
428 (jiffies - chunk->sent_at) < transport->last_rto)
Vlad Yasevichd0ce9292007-08-24 19:37:46 +0900429 continue;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* RFC 2960 6.2.1 Processing a Received SACK
432 *
433 * C) Any time a DATA chunk is marked for
434 * retransmission (via either T3-rtx timer expiration
435 * (Section 6.3.3) or via fast retransmit
436 * (Section 7.2.4)), add the data size of those
437 * chunks to the rwnd.
438 */
Sridhar Samudralacd497882006-09-29 17:09:05 -0700439 q->asoc->peer.rwnd += (sctp_data_size(chunk) +
440 sizeof(struct sk_buff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 q->outstanding_bytes -= sctp_data_size(chunk);
442 transport->flight_size -= sctp_data_size(chunk);
443
444 /* sctpimpguide-05 Section 2.8.2
445 * M5) If a T3-rtx timer expires, the
446 * 'TSN.Missing.Report' of all affected TSNs is set
447 * to 0.
448 */
449 chunk->tsn_missing_report = 0;
450
451 /* If a chunk that is being used for RTT measurement
452 * has to be retransmitted, we cannot use this chunk
453 * anymore for RTT measurements. Reset rto_pending so
454 * that a new RTT measurement is started when a new
455 * data chunk is sent.
456 */
457 if (chunk->rtt_in_progress) {
458 chunk->rtt_in_progress = 0;
459 transport->rto_pending = 0;
460 }
461
462 /* Move the chunk to the retransmit queue. The chunks
463 * on the retransmit queue are always kept in order.
464 */
465 list_del_init(lchunk);
466 sctp_insert_list(&q->retransmit, lchunk);
467 }
468 }
469
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400470 SCTP_DEBUG_PRINTK("%s: transport: %p, reason: %d, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 "cwnd: %d, ssthresh: %d, flight_size: %d, "
Harvey Harrison0dc47872008-03-05 20:47:47 -0800472 "pba: %d\n", __func__,
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400473 transport, reason,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 transport->cwnd, transport->ssthresh,
475 transport->flight_size,
476 transport->partial_bytes_acked);
477
478}
479
480/* Mark all the eligible packets on a transport for retransmission and force
481 * one packet out.
482 */
483void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
484 sctp_retransmit_reason_t reason)
485{
486 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 switch(reason) {
489 case SCTP_RTXR_T3_RTX:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700490 SCTP_INC_STATS(SCTP_MIB_T3_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
492 /* Update the retran path if the T3-rtx timer has expired for
493 * the current retran path.
494 */
495 if (transport == transport->asoc->peer.retran_path)
496 sctp_assoc_update_retran_path(transport->asoc);
Neil Horman58fbbed2008-02-29 11:40:56 -0800497 transport->asoc->rtx_data_chunks +=
498 transport->asoc->unack_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
500 case SCTP_RTXR_FAST_RTX:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700501 SCTP_INC_STATS(SCTP_MIB_FAST_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 case SCTP_RTXR_PMTUD:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700505 SCTP_INC_STATS(SCTP_MIB_PMTUD_RETRANSMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400507 case SCTP_RTXR_T1_RTX:
508 SCTP_INC_STATS(SCTP_MIB_T1_RETRANSMITS);
Neil Horman58fbbed2008-02-29 11:40:56 -0800509 transport->asoc->init_retries++;
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400510 break;
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700511 default:
512 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Vlad Yasevichb6157d82007-10-24 15:59:16 -0400515 sctp_retransmit_mark(q, transport, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
518 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
519 * following the procedures outlined in C1 - C5.
520 */
521 sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
522
523 error = sctp_outq_flush(q, /* rtx_timeout */ 1);
524
525 if (error)
526 q->asoc->base.sk->sk_err = -error;
527}
528
529/*
530 * Transmit DATA chunks on the retransmit queue. Upon return from
531 * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
532 * need to be transmitted by the caller.
533 * We assume that pkt->transport has already been set.
534 *
535 * The return value is a normal kernel error return value.
536 */
537static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
538 int rtx_timeout, int *start_timer)
539{
540 struct list_head *lqueue;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700541 struct list_head *lchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct sctp_transport *transport = pkt->transport;
543 sctp_xmit_t status;
544 struct sctp_chunk *chunk, *chunk1;
545 struct sctp_association *asoc;
546 int error = 0;
547
548 asoc = q->asoc;
549 lqueue = &q->retransmit;
550
551 /* RFC 2960 6.3.3 Handle T3-rtx Expiration
552 *
553 * E3) Determine how many of the earliest (i.e., lowest TSN)
554 * outstanding DATA chunks for the address for which the
555 * T3-rtx has expired will fit into a single packet, subject
556 * to the MTU constraint for the path corresponding to the
557 * destination transport address to which the retransmission
558 * is being sent (this may be different from the address for
559 * which the timer expires [see Section 6.4]). Call this value
560 * K. Bundle and retransmit those K DATA chunks in a single
561 * packet to the destination endpoint.
562 *
563 * [Just to be painfully clear, if we are retransmitting
564 * because a timeout just happened, we should send only ONE
565 * packet of retransmitted data.]
566 */
567 lchunk = sctp_list_dequeue(lqueue);
568
569 while (lchunk) {
570 chunk = list_entry(lchunk, struct sctp_chunk,
571 transmitted_list);
572
573 /* Make sure that Gap Acked TSNs are not retransmitted. A
574 * simple approach is just to move such TSNs out of the
575 * way and into a 'transmitted' queue and skip to the
576 * next chunk.
577 */
578 if (chunk->tsn_gap_acked) {
579 list_add_tail(lchunk, &transport->transmitted);
580 lchunk = sctp_list_dequeue(lqueue);
581 continue;
582 }
583
584 /* Attempt to append this chunk to the packet. */
585 status = sctp_packet_append_chunk(pkt, chunk);
586
587 switch (status) {
588 case SCTP_XMIT_PMTU_FULL:
589 /* Send this packet. */
590 if ((error = sctp_packet_transmit(pkt)) == 0)
591 *start_timer = 1;
592
593 /* If we are retransmitting, we should only
594 * send a single packet.
595 */
596 if (rtx_timeout) {
597 list_add(lchunk, lqueue);
598 lchunk = NULL;
599 }
600
601 /* Bundle lchunk in the next round. */
602 break;
603
604 case SCTP_XMIT_RWND_FULL:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900605 /* Send this packet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if ((error = sctp_packet_transmit(pkt)) == 0)
607 *start_timer = 1;
608
609 /* Stop sending DATA as there is no more room
610 * at the receiver.
611 */
612 list_add(lchunk, lqueue);
613 lchunk = NULL;
614 break;
615
616 case SCTP_XMIT_NAGLE_DELAY:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900617 /* Send this packet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if ((error = sctp_packet_transmit(pkt)) == 0)
619 *start_timer = 1;
620
621 /* Stop sending DATA because of nagle delay. */
622 list_add(lchunk, lqueue);
623 lchunk = NULL;
624 break;
625
626 default:
627 /* The append was successful, so add this chunk to
628 * the transmitted list.
629 */
630 list_add_tail(lchunk, &transport->transmitted);
631
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900632 /* Mark the chunk as ineligible for fast retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * after it is retransmitted.
634 */
Vlad Yasevich27852c22006-02-02 16:57:31 -0800635 if (chunk->fast_retransmit > 0)
636 chunk->fast_retransmit = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 *start_timer = 1;
639 q->empty = 0;
640
641 /* Retrieve a new chunk to bundle. */
642 lchunk = sctp_list_dequeue(lqueue);
643 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* If we are here due to a retransmit timeout or a fast
647 * retransmit and if there are any chunks left in the retransmit
Wei Yongjun64b0812b2007-10-15 11:50:38 +0900648 * queue that could not fit in the PMTU sized packet, they need
649 * to be marked as ineligible for a subsequent fast retransmit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
651 if (rtx_timeout && !lchunk) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -0700652 list_for_each_entry(chunk1, lqueue, transmitted_list) {
Vlad Yasevich27852c22006-02-02 16:57:31 -0800653 if (chunk1->fast_retransmit > 0)
654 chunk1->fast_retransmit = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 }
657 }
658
659 return error;
660}
661
662/* Cork the outqueue so queued chunks are really queued. */
663int sctp_outq_uncork(struct sctp_outq *q)
664{
665 int error = 0;
Vlad Yasevich7d54dc62007-11-09 11:43:41 -0500666 if (q->cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 q->cork = 0;
Vlad Yasevich7d54dc62007-11-09 11:43:41 -0500668 error = sctp_outq_flush(q, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return error;
670}
671
672/*
673 * Try to flush an outqueue.
674 *
675 * Description: Send everything in q which we legally can, subject to
676 * congestion limitations.
677 * * Note: This function can be called from multiple contexts so appropriate
678 * locking concerns must be made. Today we use the sock lock to protect
679 * this function.
680 */
681int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
682{
683 struct sctp_packet *packet;
684 struct sctp_packet singleton;
685 struct sctp_association *asoc = q->asoc;
686 __u16 sport = asoc->base.bind_addr.port;
687 __u16 dport = asoc->peer.port;
688 __u32 vtag = asoc->peer.i.init_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct sctp_transport *transport = NULL;
690 struct sctp_transport *new_transport;
David S. Miller79af02c2005-07-08 21:47:49 -0700691 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 sctp_xmit_t status;
693 int error = 0;
694 int start_timer = 0;
695
696 /* These transports have chunks to send. */
697 struct list_head transport_list;
698 struct list_head *ltransport;
699
700 INIT_LIST_HEAD(&transport_list);
701 packet = NULL;
702
703 /*
704 * 6.10 Bundling
705 * ...
706 * When bundling control chunks with DATA chunks, an
707 * endpoint MUST place control chunks first in the outbound
708 * SCTP packet. The transmitter MUST transmit DATA chunks
709 * within a SCTP packet in increasing order of TSN.
710 * ...
711 */
712
David S. Miller79af02c2005-07-08 21:47:49 -0700713 list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
714 list_del_init(&chunk->list);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* Pick the right transport to use. */
717 new_transport = chunk->transport;
718
719 if (!new_transport) {
Vlad Yasevicha08de642007-12-20 14:11:47 -0800720 /*
721 * If we have a prior transport pointer, see if
722 * the destination address of the chunk
723 * matches the destination address of the
724 * current transport. If not a match, then
725 * try to look up the transport with a given
726 * destination address. We do this because
727 * after processing ASCONFs, we may have new
728 * transports created.
729 */
730 if (transport &&
731 sctp_cmp_addr_exact(&chunk->dest,
732 &transport->ipaddr))
733 new_transport = transport;
734 else
735 new_transport = sctp_assoc_lookup_paddr(asoc,
736 &chunk->dest);
737
738 /* if we still don't have a new transport, then
739 * use the current active path.
740 */
741 if (!new_transport)
742 new_transport = asoc->peer.active_path;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700743 } else if ((new_transport->state == SCTP_INACTIVE) ||
744 (new_transport->state == SCTP_UNCONFIRMED)) {
Frank Filz3f7a87d2005-06-20 13:14:57 -0700745 /* If the chunk is Heartbeat or Heartbeat Ack,
746 * send it to chunk->transport, even if it's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * inactive.
748 *
749 * 3.3.6 Heartbeat Acknowledgement:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900750 * ...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * A HEARTBEAT ACK is always sent to the source IP
752 * address of the IP datagram containing the
753 * HEARTBEAT chunk to which this ack is responding.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900754 * ...
Vlad Yasevicha08de642007-12-20 14:11:47 -0800755 *
756 * ASCONF_ACKs also must be sent to the source.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758 if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
Vlad Yasevicha08de642007-12-20 14:11:47 -0800759 chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK &&
760 chunk->chunk_hdr->type != SCTP_CID_ASCONF_ACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 new_transport = asoc->peer.active_path;
762 }
763
764 /* Are we switching transports?
765 * Take care of transport locks.
766 */
767 if (new_transport != transport) {
768 transport = new_transport;
769 if (list_empty(&transport->send_ready)) {
770 list_add_tail(&transport->send_ready,
771 &transport_list);
772 }
773 packet = &transport->packet;
774 sctp_packet_config(packet, vtag,
775 asoc->peer.ecn_capable);
776 }
777
778 switch (chunk->chunk_hdr->type) {
779 /*
780 * 6.10 Bundling
781 * ...
782 * An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
783 * COMPLETE with any other chunks. [Send them immediately.]
784 */
785 case SCTP_CID_INIT:
786 case SCTP_CID_INIT_ACK:
787 case SCTP_CID_SHUTDOWN_COMPLETE:
788 sctp_packet_init(&singleton, transport, sport, dport);
789 sctp_packet_config(&singleton, vtag, 0);
790 sctp_packet_append_chunk(&singleton, chunk);
791 error = sctp_packet_transmit(&singleton);
792 if (error < 0)
793 return error;
794 break;
795
796 case SCTP_CID_ABORT:
Gui Jianfengf4ad85c2008-04-12 18:39:34 -0700797 if (sctp_test_T_bit(chunk)) {
798 packet->vtag = asoc->c.my_vtag;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case SCTP_CID_SACK:
801 case SCTP_CID_HEARTBEAT:
802 case SCTP_CID_HEARTBEAT_ACK:
803 case SCTP_CID_SHUTDOWN:
804 case SCTP_CID_SHUTDOWN_ACK:
805 case SCTP_CID_ERROR:
806 case SCTP_CID_COOKIE_ECHO:
807 case SCTP_CID_COOKIE_ACK:
808 case SCTP_CID_ECN_ECNE:
809 case SCTP_CID_ECN_CWR:
810 case SCTP_CID_ASCONF:
811 case SCTP_CID_ASCONF_ACK:
812 case SCTP_CID_FWD_TSN:
813 sctp_packet_transmit_chunk(packet, chunk);
814 break;
815
816 default:
817 /* We built a chunk with an illegal type! */
818 BUG();
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
822 /* Is it OK to send data chunks? */
823 switch (asoc->state) {
824 case SCTP_STATE_COOKIE_ECHOED:
825 /* Only allow bundling when this packet has a COOKIE-ECHO
826 * chunk.
827 */
828 if (!packet || !packet->has_cookie_echo)
829 break;
830
831 /* fallthru */
832 case SCTP_STATE_ESTABLISHED:
833 case SCTP_STATE_SHUTDOWN_PENDING:
834 case SCTP_STATE_SHUTDOWN_RECEIVED:
835 /*
836 * RFC 2960 6.1 Transmission of DATA Chunks
837 *
838 * C) When the time comes for the sender to transmit,
839 * before sending new DATA chunks, the sender MUST
840 * first transmit any outstanding DATA chunks which
841 * are marked for retransmission (limited by the
842 * current cwnd).
843 */
844 if (!list_empty(&q->retransmit)) {
845 if (transport == asoc->peer.retran_path)
846 goto retran;
847
848 /* Switch transports & prepare the packet. */
849
850 transport = asoc->peer.retran_path;
851
852 if (list_empty(&transport->send_ready)) {
853 list_add_tail(&transport->send_ready,
854 &transport_list);
855 }
856
857 packet = &transport->packet;
858 sctp_packet_config(packet, vtag,
859 asoc->peer.ecn_capable);
860 retran:
861 error = sctp_outq_flush_rtx(q, packet,
862 rtx_timeout, &start_timer);
863
864 if (start_timer)
865 sctp_transport_reset_timers(transport);
866
867 /* This can happen on COOKIE-ECHO resend. Only
868 * one chunk can get bundled with a COOKIE-ECHO.
869 */
870 if (packet->has_cookie_echo)
871 goto sctp_flush_out;
872
873 /* Don't send new data if there is still data
874 * waiting to retransmit.
875 */
876 if (!list_empty(&q->retransmit))
877 goto sctp_flush_out;
878 }
879
880 /* Finally, transmit new packets. */
881 start_timer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
883 /* RFC 2960 6.5 Every DATA chunk MUST carry a valid
884 * stream identifier.
885 */
886 if (chunk->sinfo.sinfo_stream >=
887 asoc->c.sinit_num_ostreams) {
888
889 /* Mark as failed send. */
890 sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
891 sctp_chunk_free(chunk);
892 continue;
893 }
894
895 /* Has this chunk expired? */
896 if (sctp_chunk_abandoned(chunk)) {
897 sctp_chunk_fail(chunk, 0);
898 sctp_chunk_free(chunk);
899 continue;
900 }
901
902 /* If there is a specified transport, use it.
903 * Otherwise, we want to use the active path.
904 */
905 new_transport = chunk->transport;
Frank Filz3f7a87d2005-06-20 13:14:57 -0700906 if (!new_transport ||
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700907 ((new_transport->state == SCTP_INACTIVE) ||
908 (new_transport->state == SCTP_UNCONFIRMED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 new_transport = asoc->peer.active_path;
910
911 /* Change packets if necessary. */
912 if (new_transport != transport) {
913 transport = new_transport;
914
915 /* Schedule to have this transport's
916 * packet flushed.
917 */
918 if (list_empty(&transport->send_ready)) {
919 list_add_tail(&transport->send_ready,
920 &transport_list);
921 }
922
923 packet = &transport->packet;
924 sctp_packet_config(packet, vtag,
925 asoc->peer.ecn_capable);
926 }
927
928 SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
929 q, chunk,
930 chunk && chunk->chunk_hdr ?
931 sctp_cname(SCTP_ST_CHUNK(
932 chunk->chunk_hdr->type))
933 : "Illegal Chunk");
934
935 SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
936 "%p skb->users %d.\n",
937 ntohl(chunk->subh.data_hdr->tsn),
938 chunk->skb ?chunk->skb->head : NULL,
939 chunk->skb ?
940 atomic_read(&chunk->skb->users) : -1);
941
942 /* Add the chunk to the packet. */
943 status = sctp_packet_transmit_chunk(packet, chunk);
944
945 switch (status) {
946 case SCTP_XMIT_PMTU_FULL:
947 case SCTP_XMIT_RWND_FULL:
948 case SCTP_XMIT_NAGLE_DELAY:
949 /* We could not append this chunk, so put
950 * the chunk back on the output queue.
951 */
952 SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
953 "not transmit TSN: 0x%x, status: %d\n",
954 ntohl(chunk->subh.data_hdr->tsn),
955 status);
956 sctp_outq_head_data(q, chunk);
957 goto sctp_flush_out;
958 break;
959
960 case SCTP_XMIT_OK:
961 break;
962
963 default:
964 BUG();
965 }
966
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900967 /* BUG: We assume that the sctp_packet_transmit()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * call below will succeed all the time and add the
969 * chunk to the transmitted list and restart the
970 * timers.
971 * It is possible that the call can fail under OOM
972 * conditions.
973 *
974 * Is this really a problem? Won't this behave
975 * like a lost TSN?
976 */
977 list_add_tail(&chunk->transmitted_list,
978 &transport->transmitted);
979
980 sctp_transport_reset_timers(transport);
981
982 q->empty = 0;
983
984 /* Only let one DATA chunk get bundled with a
985 * COOKIE-ECHO chunk.
986 */
987 if (packet->has_cookie_echo)
988 goto sctp_flush_out;
989 }
990 break;
991
992 default:
993 /* Do nothing. */
994 break;
995 }
996
997sctp_flush_out:
998
999 /* Before returning, examine all the transports touched in
1000 * this call. Right now, we bluntly force clear all the
1001 * transports. Things might change after we implement Nagle.
1002 * But such an examination is still required.
1003 *
1004 * --xguo
1005 */
1006 while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL ) {
1007 struct sctp_transport *t = list_entry(ltransport,
1008 struct sctp_transport,
1009 send_ready);
1010 packet = &t->packet;
1011 if (!sctp_packet_empty(packet))
1012 error = sctp_packet_transmit(packet);
1013 }
1014
1015 return error;
1016}
1017
1018/* Update unack_data based on the incoming SACK chunk */
1019static void sctp_sack_update_unack_data(struct sctp_association *assoc,
1020 struct sctp_sackhdr *sack)
1021{
1022 sctp_sack_variable_t *frags;
1023 __u16 unack_data;
1024 int i;
1025
1026 unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
1027
1028 frags = sack->variable;
1029 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
1030 unack_data -= ((ntohs(frags[i].gab.end) -
1031 ntohs(frags[i].gab.start) + 1));
1032 }
1033
1034 assoc->unack_data = unack_data;
1035}
1036
1037/* Return the highest new tsn that is acknowledged by the given SACK chunk. */
1038static __u32 sctp_highest_new_tsn(struct sctp_sackhdr *sack,
1039 struct sctp_association *asoc)
1040{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct sctp_transport *transport;
1042 struct sctp_chunk *chunk;
1043 __u32 highest_new_tsn, tsn;
1044 struct list_head *transport_list = &asoc->peer.transport_addr_list;
1045
1046 highest_new_tsn = ntohl(sack->cum_tsn_ack);
1047
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001048 list_for_each_entry(transport, transport_list, transports) {
1049 list_for_each_entry(chunk, &transport->transmitted,
1050 transmitted_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 tsn = ntohl(chunk->subh.data_hdr->tsn);
1052
1053 if (!chunk->tsn_gap_acked &&
1054 TSN_lt(highest_new_tsn, tsn) &&
1055 sctp_acked(sack, tsn))
1056 highest_new_tsn = tsn;
1057 }
1058 }
1059
1060 return highest_new_tsn;
1061}
1062
1063/* This is where we REALLY process a SACK.
1064 *
1065 * Process the SACK against the outqueue. Mostly, this just frees
1066 * things off the transmitted queue.
1067 */
1068int sctp_outq_sack(struct sctp_outq *q, struct sctp_sackhdr *sack)
1069{
1070 struct sctp_association *asoc = q->asoc;
1071 struct sctp_transport *transport;
1072 struct sctp_chunk *tchunk = NULL;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001073 struct list_head *lchunk, *transport_list, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 sctp_sack_variable_t *frags = sack->variable;
1075 __u32 sack_ctsn, ctsn, tsn;
1076 __u32 highest_tsn, highest_new_tsn;
1077 __u32 sack_a_rwnd;
1078 unsigned outstanding;
1079 struct sctp_transport *primary = asoc->peer.primary_path;
1080 int count_of_newacks = 0;
1081
1082 /* Grab the association's destination address list. */
1083 transport_list = &asoc->peer.transport_addr_list;
1084
1085 sack_ctsn = ntohl(sack->cum_tsn_ack);
1086
1087 /*
1088 * SFR-CACC algorithm:
1089 * On receipt of a SACK the sender SHOULD execute the
1090 * following statements.
1091 *
1092 * 1) If the cumulative ack in the SACK passes next tsn_at_change
1093 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
1094 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
1095 * all destinations.
1096 */
1097 if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
1098 primary->cacc.changeover_active = 0;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001099 list_for_each_entry(transport, transport_list,
1100 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 transport->cacc.cycling_changeover = 0;
1102 }
1103 }
1104
1105 /*
1106 * SFR-CACC algorithm:
1107 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
1108 * is set the receiver of the SACK MUST take the following actions:
1109 *
1110 * A) Initialize the cacc_saw_newack to 0 for all destination
1111 * addresses.
1112 */
Al Viro34bcca22006-11-20 17:27:15 -08001113 if (sack->num_gap_ack_blocks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 primary->cacc.changeover_active) {
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001115 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 transport->cacc.cacc_saw_newack = 0;
1117 }
1118 }
1119
1120 /* Get the highest TSN in the sack. */
1121 highest_tsn = sack_ctsn;
1122 if (sack->num_gap_ack_blocks)
1123 highest_tsn +=
1124 ntohs(frags[ntohs(sack->num_gap_ack_blocks) - 1].gab.end);
1125
1126 if (TSN_lt(asoc->highest_sacked, highest_tsn)) {
1127 highest_new_tsn = highest_tsn;
1128 asoc->highest_sacked = highest_tsn;
1129 } else {
1130 highest_new_tsn = sctp_highest_new_tsn(sack, asoc);
1131 }
1132
1133 /* Run through the retransmit queue. Credit bytes received
1134 * and free those chunks that we can.
1135 */
1136 sctp_check_transmitted(q, &q->retransmit, NULL, sack, highest_new_tsn);
1137 sctp_mark_missing(q, &q->retransmit, NULL, highest_new_tsn, 0);
1138
1139 /* Run through the transmitted queue.
1140 * Credit bytes received and free those chunks which we can.
1141 *
1142 * This is a MASSIVE candidate for optimization.
1143 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001144 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sctp_check_transmitted(q, &transport->transmitted,
1146 transport, sack, highest_new_tsn);
1147 /*
1148 * SFR-CACC algorithm:
1149 * C) Let count_of_newacks be the number of
1150 * destinations for which cacc_saw_newack is set.
1151 */
1152 if (transport->cacc.cacc_saw_newack)
1153 count_of_newacks ++;
1154 }
1155
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001156 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 sctp_mark_missing(q, &transport->transmitted, transport,
1158 highest_new_tsn, count_of_newacks);
1159 }
1160
1161 /* Move the Cumulative TSN Ack Point if appropriate. */
1162 if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn))
1163 asoc->ctsn_ack_point = sack_ctsn;
1164
1165 /* Update unack_data field in the assoc. */
1166 sctp_sack_update_unack_data(asoc, sack);
1167
1168 ctsn = asoc->ctsn_ack_point;
1169
1170 /* Throw away stuff rotting on the sack queue. */
1171 list_for_each_safe(lchunk, temp, &q->sacked) {
1172 tchunk = list_entry(lchunk, struct sctp_chunk,
1173 transmitted_list);
1174 tsn = ntohl(tchunk->subh.data_hdr->tsn);
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05001175 if (TSN_lte(tsn, ctsn)) {
1176 list_del_init(&tchunk->transmitted_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 sctp_chunk_free(tchunk);
Vlad Yasevich5f9646c2008-02-05 14:23:44 -05001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
1181 /* ii) Set rwnd equal to the newly received a_rwnd minus the
1182 * number of bytes still outstanding after processing the
1183 * Cumulative TSN Ack and the Gap Ack Blocks.
1184 */
1185
1186 sack_a_rwnd = ntohl(sack->a_rwnd);
1187 outstanding = q->outstanding_bytes;
1188
1189 if (outstanding < sack_a_rwnd)
1190 sack_a_rwnd -= outstanding;
1191 else
1192 sack_a_rwnd = 0;
1193
1194 asoc->peer.rwnd = sack_a_rwnd;
1195
1196 sctp_generate_fwdtsn(q, sack_ctsn);
1197
1198 SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001199 __func__, sack_ctsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
1201 "%p is 0x%x. Adv peer ack point: 0x%x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001202 __func__, asoc, ctsn, asoc->adv_peer_ack_point);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* See if all chunks are acked.
1205 * Make sure the empty queue handler will get run later.
1206 */
David S. Miller79af02c2005-07-08 21:47:49 -07001207 q->empty = (list_empty(&q->out_chunk_list) &&
1208 list_empty(&q->control_chunk_list) &&
1209 list_empty(&q->retransmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!q->empty)
1211 goto finish;
1212
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001213 list_for_each_entry(transport, transport_list, transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 q->empty = q->empty && list_empty(&transport->transmitted);
1215 if (!q->empty)
1216 goto finish;
1217 }
1218
1219 SCTP_DEBUG_PRINTK("sack queue is empty.\n");
1220finish:
1221 return q->empty;
1222}
1223
1224/* Is the outqueue empty? */
1225int sctp_outq_is_empty(const struct sctp_outq *q)
1226{
1227 return q->empty;
1228}
1229
1230/********************************************************************
1231 * 2nd Level Abstractions
1232 ********************************************************************/
1233
1234/* Go through a transport's transmitted list or the association's retransmit
1235 * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
1236 * The retransmit list will not have an associated transport.
1237 *
1238 * I added coherent debug information output. --xguo
1239 *
1240 * Instead of printing 'sacked' or 'kept' for each TSN on the
1241 * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
1242 * KEPT TSN6-TSN7, etc.
1243 */
1244static void sctp_check_transmitted(struct sctp_outq *q,
1245 struct list_head *transmitted_queue,
1246 struct sctp_transport *transport,
1247 struct sctp_sackhdr *sack,
1248 __u32 highest_new_tsn_in_sack)
1249{
1250 struct list_head *lchunk;
1251 struct sctp_chunk *tchunk;
1252 struct list_head tlist;
1253 __u32 tsn;
1254 __u32 sack_ctsn;
1255 __u32 rtt;
1256 __u8 restart_timer = 0;
1257 int bytes_acked = 0;
1258
1259 /* These state variables are for coherent debug output. --xguo */
1260
1261#if SCTP_DEBUG
1262 __u32 dbg_ack_tsn = 0; /* An ACKed TSN range starts here... */
1263 __u32 dbg_last_ack_tsn = 0; /* ...and finishes here. */
1264 __u32 dbg_kept_tsn = 0; /* An un-ACKed range starts here... */
1265 __u32 dbg_last_kept_tsn = 0; /* ...and finishes here. */
1266
1267 /* 0 : The last TSN was ACKed.
1268 * 1 : The last TSN was NOT ACKed (i.e. KEPT).
1269 * -1: We need to initialize.
1270 */
1271 int dbg_prt_state = -1;
1272#endif /* SCTP_DEBUG */
1273
1274 sack_ctsn = ntohl(sack->cum_tsn_ack);
1275
1276 INIT_LIST_HEAD(&tlist);
1277
1278 /* The while loop will skip empty transmitted queues. */
1279 while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
1280 tchunk = list_entry(lchunk, struct sctp_chunk,
1281 transmitted_list);
1282
1283 if (sctp_chunk_abandoned(tchunk)) {
1284 /* Move the chunk to abandoned list. */
1285 sctp_insert_list(&q->abandoned, lchunk);
Vlad Yasevich8c4a2d42007-02-21 02:06:04 -08001286
1287 /* If this chunk has not been acked, stop
1288 * considering it as 'outstanding'.
1289 */
1290 if (!tchunk->tsn_gap_acked) {
1291 tchunk->transport->flight_size -=
1292 sctp_data_size(tchunk);
1293 q->outstanding_bytes -= sctp_data_size(tchunk);
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 continue;
1296 }
1297
1298 tsn = ntohl(tchunk->subh.data_hdr->tsn);
1299 if (sctp_acked(sack, tsn)) {
1300 /* If this queue is the retransmit queue, the
1301 * retransmit timer has already reclaimed
1302 * the outstanding bytes for this chunk, so only
1303 * count bytes associated with a transport.
1304 */
1305 if (transport) {
1306 /* If this chunk is being used for RTT
1307 * measurement, calculate the RTT and update
1308 * the RTO using this value.
1309 *
1310 * 6.3.1 C5) Karn's algorithm: RTT measurements
1311 * MUST NOT be made using packets that were
1312 * retransmitted (and thus for which it is
1313 * ambiguous whether the reply was for the
1314 * first instance of the packet or a later
1315 * instance).
1316 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001317 if (!tchunk->tsn_gap_acked &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 !tchunk->resent &&
1319 tchunk->rtt_in_progress) {
Vlad Yasevich4c9f5d52006-06-17 22:56:08 -07001320 tchunk->rtt_in_progress = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 rtt = jiffies - tchunk->sent_at;
1322 sctp_transport_update_rto(transport,
1323 rtt);
1324 }
1325 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001326 if (TSN_lte(tsn, sack_ctsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* RFC 2960 6.3.2 Retransmission Timer Rules
1328 *
1329 * R3) Whenever a SACK is received
1330 * that acknowledges the DATA chunk
1331 * with the earliest outstanding TSN
1332 * for that address, restart T3-rtx
1333 * timer for that address with its
1334 * current RTO.
1335 */
1336 restart_timer = 1;
1337
1338 if (!tchunk->tsn_gap_acked) {
1339 tchunk->tsn_gap_acked = 1;
1340 bytes_acked += sctp_data_size(tchunk);
1341 /*
1342 * SFR-CACC algorithm:
1343 * 2) If the SACK contains gap acks
1344 * and the flag CHANGEOVER_ACTIVE is
1345 * set the receiver of the SACK MUST
1346 * take the following action:
1347 *
1348 * B) For each TSN t being acked that
1349 * has not been acked in any SACK so
1350 * far, set cacc_saw_newack to 1 for
1351 * the destination that the TSN was
1352 * sent to.
1353 */
1354 if (transport &&
1355 sack->num_gap_ack_blocks &&
1356 q->asoc->peer.primary_path->cacc.
1357 changeover_active)
1358 transport->cacc.cacc_saw_newack
1359 = 1;
1360 }
1361
1362 list_add_tail(&tchunk->transmitted_list,
1363 &q->sacked);
1364 } else {
1365 /* RFC2960 7.2.4, sctpimpguide-05 2.8.2
1366 * M2) Each time a SACK arrives reporting
1367 * 'Stray DATA chunk(s)' record the highest TSN
1368 * reported as newly acknowledged, call this
1369 * value 'HighestTSNinSack'. A newly
1370 * acknowledged DATA chunk is one not
1371 * previously acknowledged in a SACK.
1372 *
1373 * When the SCTP sender of data receives a SACK
1374 * chunk that acknowledges, for the first time,
1375 * the receipt of a DATA chunk, all the still
1376 * unacknowledged DATA chunks whose TSN is
1377 * older than that newly acknowledged DATA
1378 * chunk, are qualified as 'Stray DATA chunks'.
1379 */
1380 if (!tchunk->tsn_gap_acked) {
1381 tchunk->tsn_gap_acked = 1;
1382 bytes_acked += sctp_data_size(tchunk);
1383 }
1384 list_add_tail(lchunk, &tlist);
1385 }
1386
1387#if SCTP_DEBUG
1388 switch (dbg_prt_state) {
1389 case 0: /* last TSN was ACKed */
1390 if (dbg_last_ack_tsn + 1 == tsn) {
1391 /* This TSN belongs to the
1392 * current ACK range.
1393 */
1394 break;
1395 }
1396
1397 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1398 /* Display the end of the
1399 * current range.
1400 */
1401 SCTP_DEBUG_PRINTK("-%08x",
1402 dbg_last_ack_tsn);
1403 }
1404
1405 /* Start a new range. */
1406 SCTP_DEBUG_PRINTK(",%08x", tsn);
1407 dbg_ack_tsn = tsn;
1408 break;
1409
1410 case 1: /* The last TSN was NOT ACKed. */
1411 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1412 /* Display the end of current range. */
1413 SCTP_DEBUG_PRINTK("-%08x",
1414 dbg_last_kept_tsn);
1415 }
1416
1417 SCTP_DEBUG_PRINTK("\n");
1418
1419 /* FALL THROUGH... */
1420 default:
1421 /* This is the first-ever TSN we examined. */
1422 /* Start a new range of ACK-ed TSNs. */
1423 SCTP_DEBUG_PRINTK("ACKed: %08x", tsn);
1424 dbg_prt_state = 0;
1425 dbg_ack_tsn = tsn;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 dbg_last_ack_tsn = tsn;
1429#endif /* SCTP_DEBUG */
1430
1431 } else {
1432 if (tchunk->tsn_gap_acked) {
1433 SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
1434 "data TSN: 0x%x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001435 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 tsn);
1437 tchunk->tsn_gap_acked = 0;
1438
1439 bytes_acked -= sctp_data_size(tchunk);
1440
1441 /* RFC 2960 6.3.2 Retransmission Timer Rules
1442 *
1443 * R4) Whenever a SACK is received missing a
1444 * TSN that was previously acknowledged via a
1445 * Gap Ack Block, start T3-rtx for the
1446 * destination address to which the DATA
1447 * chunk was originally
1448 * transmitted if it is not already running.
1449 */
1450 restart_timer = 1;
1451 }
1452
1453 list_add_tail(lchunk, &tlist);
1454
1455#if SCTP_DEBUG
1456 /* See the above comments on ACK-ed TSNs. */
1457 switch (dbg_prt_state) {
1458 case 1:
1459 if (dbg_last_kept_tsn + 1 == tsn)
1460 break;
1461
1462 if (dbg_last_kept_tsn != dbg_kept_tsn)
1463 SCTP_DEBUG_PRINTK("-%08x",
1464 dbg_last_kept_tsn);
1465
1466 SCTP_DEBUG_PRINTK(",%08x", tsn);
1467 dbg_kept_tsn = tsn;
1468 break;
1469
1470 case 0:
1471 if (dbg_last_ack_tsn != dbg_ack_tsn)
1472 SCTP_DEBUG_PRINTK("-%08x",
1473 dbg_last_ack_tsn);
1474 SCTP_DEBUG_PRINTK("\n");
1475
1476 /* FALL THROUGH... */
1477 default:
1478 SCTP_DEBUG_PRINTK("KEPT: %08x",tsn);
1479 dbg_prt_state = 1;
1480 dbg_kept_tsn = tsn;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 dbg_last_kept_tsn = tsn;
1484#endif /* SCTP_DEBUG */
1485 }
1486 }
1487
1488#if SCTP_DEBUG
1489 /* Finish off the last range, displaying its ending TSN. */
1490 switch (dbg_prt_state) {
1491 case 0:
1492 if (dbg_last_ack_tsn != dbg_ack_tsn) {
1493 SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_ack_tsn);
1494 } else {
1495 SCTP_DEBUG_PRINTK("\n");
1496 }
1497 break;
1498
1499 case 1:
1500 if (dbg_last_kept_tsn != dbg_kept_tsn) {
1501 SCTP_DEBUG_PRINTK("-%08x\n", dbg_last_kept_tsn);
1502 } else {
1503 SCTP_DEBUG_PRINTK("\n");
1504 }
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506#endif /* SCTP_DEBUG */
1507 if (transport) {
1508 if (bytes_acked) {
1509 /* 8.2. When an outstanding TSN is acknowledged,
1510 * the endpoint shall clear the error counter of
1511 * the destination transport address to which the
1512 * DATA chunk was last sent.
1513 * The association's overall error counter is
1514 * also cleared.
1515 */
1516 transport->error_count = 0;
1517 transport->asoc->overall_error_count = 0;
1518
1519 /* Mark the destination transport address as
1520 * active if it is not so marked.
1521 */
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001522 if ((transport->state == SCTP_INACTIVE) ||
1523 (transport->state == SCTP_UNCONFIRMED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 sctp_assoc_control_transport(
1525 transport->asoc,
1526 transport,
1527 SCTP_TRANSPORT_UP,
1528 SCTP_RECEIVED_SACK);
1529 }
1530
1531 sctp_transport_raise_cwnd(transport, sack_ctsn,
1532 bytes_acked);
1533
1534 transport->flight_size -= bytes_acked;
1535 q->outstanding_bytes -= bytes_acked;
1536 } else {
1537 /* RFC 2960 6.1, sctpimpguide-06 2.15.2
1538 * When a sender is doing zero window probing, it
1539 * should not timeout the association if it continues
1540 * to receive new packets from the receiver. The
1541 * reason is that the receiver MAY keep its window
1542 * closed for an indefinite time.
1543 * A sender is doing zero window probing when the
1544 * receiver's advertised window is zero, and there is
1545 * only one data chunk in flight to the receiver.
1546 */
1547 if (!q->asoc->peer.rwnd &&
1548 !list_empty(&tlist) &&
1549 (sack_ctsn+2 == q->asoc->next_tsn)) {
1550 SCTP_DEBUG_PRINTK("%s: SACK received for zero "
1551 "window probe: %u\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001552 __func__, sack_ctsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 q->asoc->overall_error_count = 0;
1554 transport->error_count = 0;
1555 }
1556 }
1557
1558 /* RFC 2960 6.3.2 Retransmission Timer Rules
1559 *
1560 * R2) Whenever all outstanding data sent to an address have
1561 * been acknowledged, turn off the T3-rtx timer of that
1562 * address.
1563 */
1564 if (!transport->flight_size) {
1565 if (timer_pending(&transport->T3_rtx_timer) &&
1566 del_timer(&transport->T3_rtx_timer)) {
1567 sctp_transport_put(transport);
1568 }
1569 } else if (restart_timer) {
1570 if (!mod_timer(&transport->T3_rtx_timer,
1571 jiffies + transport->rto))
1572 sctp_transport_hold(transport);
1573 }
1574 }
1575
1576 list_splice(&tlist, transmitted_queue);
1577}
1578
1579/* Mark chunks as missing and consequently may get retransmitted. */
1580static void sctp_mark_missing(struct sctp_outq *q,
1581 struct list_head *transmitted_queue,
1582 struct sctp_transport *transport,
1583 __u32 highest_new_tsn_in_sack,
1584 int count_of_newacks)
1585{
1586 struct sctp_chunk *chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 __u32 tsn;
1588 char do_fast_retransmit = 0;
1589 struct sctp_transport *primary = q->asoc->peer.primary_path;
1590
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001591 list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 tsn = ntohl(chunk->subh.data_hdr->tsn);
1594
1595 /* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
1596 * 'Unacknowledged TSN's', if the TSN number of an
1597 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
1598 * value, increment the 'TSN.Missing.Report' count on that
1599 * chunk if it has NOT been fast retransmitted or marked for
1600 * fast retransmit already.
1601 */
1602 if (!chunk->fast_retransmit &&
1603 !chunk->tsn_gap_acked &&
1604 TSN_lt(tsn, highest_new_tsn_in_sack)) {
1605
1606 /* SFR-CACC may require us to skip marking
1607 * this chunk as missing.
1608 */
1609 if (!transport || !sctp_cacc_skip(primary, transport,
1610 count_of_newacks, tsn)) {
1611 chunk->tsn_missing_report++;
1612
1613 SCTP_DEBUG_PRINTK(
1614 "%s: TSN 0x%x missing counter: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001615 __func__, tsn,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 chunk->tsn_missing_report);
1617 }
1618 }
1619 /*
1620 * M4) If any DATA chunk is found to have a
1621 * 'TSN.Missing.Report'
Vlad Yasevich27852c22006-02-02 16:57:31 -08001622 * value larger than or equal to 3, mark that chunk for
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * retransmission and start the fast retransmit procedure.
1624 */
1625
Vlad Yasevich27852c22006-02-02 16:57:31 -08001626 if (chunk->tsn_missing_report >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 chunk->fast_retransmit = 1;
1628 do_fast_retransmit = 1;
1629 }
1630 }
1631
1632 if (transport) {
1633 if (do_fast_retransmit)
1634 sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
1635
1636 SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
1637 "ssthresh: %d, flight_size: %d, pba: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001638 __func__, transport, transport->cwnd,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001639 transport->ssthresh, transport->flight_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 transport->partial_bytes_acked);
1641 }
1642}
1643
1644/* Is the given TSN acked by this packet? */
1645static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
1646{
1647 int i;
1648 sctp_sack_variable_t *frags;
1649 __u16 gap;
1650 __u32 ctsn = ntohl(sack->cum_tsn_ack);
1651
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001652 if (TSN_lte(tsn, ctsn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 goto pass;
1654
1655 /* 3.3.4 Selective Acknowledgement (SACK) (3):
1656 *
1657 * Gap Ack Blocks:
1658 * These fields contain the Gap Ack Blocks. They are repeated
1659 * for each Gap Ack Block up to the number of Gap Ack Blocks
1660 * defined in the Number of Gap Ack Blocks field. All DATA
1661 * chunks with TSNs greater than or equal to (Cumulative TSN
1662 * Ack + Gap Ack Block Start) and less than or equal to
1663 * (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
1664 * Block are assumed to have been received correctly.
1665 */
1666
1667 frags = sack->variable;
1668 gap = tsn - ctsn;
1669 for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
1670 if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
1671 TSN_lte(gap, ntohs(frags[i].gab.end)))
1672 goto pass;
1673 }
1674
1675 return 0;
1676pass:
1677 return 1;
1678}
1679
1680static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
Al Viro9f81bcd2006-11-20 17:26:34 -08001681 int nskips, __be16 stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 int i;
1684
1685 for (i = 0; i < nskips; i++) {
1686 if (skiplist[i].stream == stream)
1687 return i;
1688 }
1689 return i;
1690}
1691
1692/* Create and add a fwdtsn chunk to the outq's control queue if needed. */
1693static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
1694{
1695 struct sctp_association *asoc = q->asoc;
1696 struct sctp_chunk *ftsn_chunk = NULL;
1697 struct sctp_fwdtsn_skip ftsn_skip_arr[10];
1698 int nskips = 0;
1699 int skip_pos = 0;
1700 __u32 tsn;
1701 struct sctp_chunk *chunk;
1702 struct list_head *lchunk, *temp;
1703
1704 /* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
1705 * received SACK.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
1708 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
1709 */
1710 if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
1711 asoc->adv_peer_ack_point = ctsn;
1712
1713 /* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
1714 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
1715 * the chunk next in the out-queue space is marked as "abandoned" as
1716 * shown in the following example:
1717 *
1718 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
1719 * and the Advanced.Peer.Ack.Point is updated to this value:
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * out-queue at the end of ==> out-queue after Adv.Ack.Point
1722 * normal SACK processing local advancement
1723 * ... ...
1724 * Adv.Ack.Pt-> 102 acked 102 acked
1725 * 103 abandoned 103 abandoned
1726 * 104 abandoned Adv.Ack.P-> 104 abandoned
1727 * 105 105
1728 * 106 acked 106 acked
1729 * ... ...
1730 *
1731 * In this example, the data sender successfully advanced the
1732 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
1733 */
1734 list_for_each_safe(lchunk, temp, &q->abandoned) {
1735 chunk = list_entry(lchunk, struct sctp_chunk,
1736 transmitted_list);
1737 tsn = ntohl(chunk->subh.data_hdr->tsn);
1738
1739 /* Remove any chunks in the abandoned queue that are acked by
1740 * the ctsn.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (TSN_lte(tsn, ctsn)) {
1743 list_del_init(lchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 sctp_chunk_free(chunk);
1745 } else {
1746 if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
1747 asoc->adv_peer_ack_point = tsn;
1748 if (chunk->chunk_hdr->flags &
1749 SCTP_DATA_UNORDERED)
1750 continue;
1751 skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
1752 nskips,
1753 chunk->subh.data_hdr->stream);
1754 ftsn_skip_arr[skip_pos].stream =
1755 chunk->subh.data_hdr->stream;
1756 ftsn_skip_arr[skip_pos].ssn =
1757 chunk->subh.data_hdr->ssn;
1758 if (skip_pos == nskips)
1759 nskips++;
1760 if (nskips == 10)
1761 break;
1762 } else
1763 break;
1764 }
1765 }
1766
1767 /* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
1768 * is greater than the Cumulative TSN ACK carried in the received
1769 * SACK, the data sender MUST send the data receiver a FORWARD TSN
1770 * chunk containing the latest value of the
1771 * "Advanced.Peer.Ack.Point".
1772 *
1773 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
1774 * list each stream and sequence number in the forwarded TSN. This
1775 * information will enable the receiver to easily find any
1776 * stranded TSN's waiting on stream reorder queues. Each stream
1777 * SHOULD only be reported once; this means that if multiple
1778 * abandoned messages occur in the same stream then only the
1779 * highest abandoned stream sequence number is reported. If the
1780 * total size of the FORWARD TSN does NOT fit in a single MTU then
1781 * the sender of the FORWARD TSN SHOULD lower the
1782 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
1783 * single MTU.
1784 */
1785 if (asoc->adv_peer_ack_point > ctsn)
1786 ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001787 nskips, &ftsn_skip_arr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (ftsn_chunk) {
David S. Miller79af02c2005-07-08 21:47:49 -07001790 list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1792 }
1793}