Blame view

net/sctp/outqueue.c 54.7 KB
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
1
  /* SCTP kernel implementation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
   * (C) Copyright IBM Corp. 2001, 2004
   * Copyright (c) 1999-2000 Cisco, Inc.
   * Copyright (c) 1999-2001 Motorola, Inc.
   * Copyright (c) 2001-2003 Intel Corp.
   *
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
7
   * This file is part of the SCTP kernel implementation
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
10
11
   *
   * These functions implement the sctp_outq class.   The outqueue handles
   * bundling and queueing of outgoing SCTP chunks.
   *
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
12
   * This SCTP implementation is free software;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
   * you can redistribute it and/or modify it under the terms of
   * the GNU General Public License as published by
   * the Free Software Foundation; either version 2, or (at your option)
   * any later version.
   *
60c778b25   Vlad Yasevich   [SCTP]: Stop clai...
18
   * This SCTP implementation is distributed in the hope that it
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
   * will be useful, but WITHOUT ANY WARRANTY; without even the implied
   *                 ************************
   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with GNU CC; see the file COPYING.  If not, write to
   * the Free Software Foundation, 59 Temple Place - Suite 330,
   * Boston, MA 02111-1307, USA.
   *
   * Please send any bug reports or fixes you make to the
   * email address(es):
   *    lksctp developers <lksctp-developers@lists.sourceforge.net>
   *
   * Or submit a bug report through the following website:
   *    http://www.sf.net/projects/lksctp
   *
   * Written or modified by:
   *    La Monte H.P. Yarroll <piggy@acm.org>
   *    Karl Knutson          <karl@athena.chicago.il.us>
   *    Perry Melange         <pmelange@null.cc.uic.edu>
   *    Xingang Guo           <xingang.guo@intel.com>
   *    Hui Huang 	    <hui.huang@nokia.com>
   *    Sridhar Samudrala     <sri@us.ibm.com>
   *    Jon Grimm             <jgrimm@us.ibm.com>
   *
   * Any bugs reported given to us we will try to fix... any fixes shared will
   * be incorporated into the next SCTP release.
   */
145ce502e   Joe Perches   net/sctp: Use pr_...
48
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
  #include <linux/types.h>
  #include <linux/list.h>   /* For struct list_head */
  #include <linux/socket.h>
  #include <linux/ip.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
53
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
56
57
58
59
60
61
62
63
64
  #include <net/sock.h>	  /* For skb_set_owner_w */
  
  #include <net/sctp/sctp.h>
  #include <net/sctp/sm.h>
  
  /* Declare internal functions here.  */
  static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn);
  static void sctp_check_transmitted(struct sctp_outq *q,
  				   struct list_head *transmitted_queue,
  				   struct sctp_transport *transport,
  				   struct sctp_sackhdr *sack,
bfa0d9843   Vlad Yasevich   sctp: Optimize co...
65
  				   __u32 *highest_new_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
67
68
69
70
71
72
73
  
  static void sctp_mark_missing(struct sctp_outq *q,
  			      struct list_head *transmitted_queue,
  			      struct sctp_transport *transport,
  			      __u32 highest_new_tsn,
  			      int count_of_newacks);
  
  static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 sack_ctsn);
abd0b198e   Adrian Bunk   sctp: make sctp_o...
74
  static int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
  /* Add data to the front of the queue. */
  static inline void sctp_outq_head_data(struct sctp_outq *q,
  					struct sctp_chunk *ch)
  {
79af02c25   David S. Miller   [SCTP]: Use struc...
79
  	list_add(&ch->list, &q->out_chunk_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
  	q->out_qlen += ch->skb->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
  }
  
  /* Take data from the front of the queue. */
  static inline struct sctp_chunk *sctp_outq_dequeue_data(struct sctp_outq *q)
  {
79af02c25   David S. Miller   [SCTP]: Use struc...
86
87
88
89
90
91
92
  	struct sctp_chunk *ch = NULL;
  
  	if (!list_empty(&q->out_chunk_list)) {
  		struct list_head *entry = q->out_chunk_list.next;
  
  		ch = list_entry(entry, struct sctp_chunk, list);
  		list_del_init(entry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  		q->out_qlen -= ch->skb->len;
79af02c25   David S. Miller   [SCTP]: Use struc...
94
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
  	return ch;
  }
  /* Add data chunk to the end of the queue. */
  static inline void sctp_outq_tail_data(struct sctp_outq *q,
  				       struct sctp_chunk *ch)
  {
79af02c25   David S. Miller   [SCTP]: Use struc...
101
  	list_add_tail(&ch->list, &q->out_chunk_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  	q->out_qlen += ch->skb->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  }
  
  /*
   * SFR-CACC algorithm:
   * D) If count_of_newacks is greater than or equal to 2
   * and t was not sent to the current primary then the
   * sender MUST NOT increment missing report count for t.
   */
  static inline int sctp_cacc_skip_3_1_d(struct sctp_transport *primary,
  				       struct sctp_transport *transport,
  				       int count_of_newacks)
  {
  	if (count_of_newacks >=2 && transport != primary)
  		return 1;
  	return 0;
  }
  
  /*
   * SFR-CACC algorithm:
   * F) If count_of_newacks is less than 2, let d be the
   * destination to which t was sent. If cacc_saw_newack
   * is 0 for destination d, then the sender MUST NOT
   * increment missing report count for t.
   */
  static inline int sctp_cacc_skip_3_1_f(struct sctp_transport *transport,
  				       int count_of_newacks)
  {
f246a7b7c   Vlad Yasevich   sctp: teach CACC ...
130
131
  	if (count_of_newacks < 2 &&
  			(transport && !transport->cacc.cacc_saw_newack))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  		return 1;
  	return 0;
  }
  
  /*
   * SFR-CACC algorithm:
   * 3.1) If CYCLING_CHANGEOVER is 0, the sender SHOULD
   * execute steps C, D, F.
   *
   * C has been implemented in sctp_outq_sack
   */
  static inline int sctp_cacc_skip_3_1(struct sctp_transport *primary,
  				     struct sctp_transport *transport,
  				     int count_of_newacks)
  {
  	if (!primary->cacc.cycling_changeover) {
  		if (sctp_cacc_skip_3_1_d(primary, transport, count_of_newacks))
  			return 1;
  		if (sctp_cacc_skip_3_1_f(transport, count_of_newacks))
  			return 1;
  		return 0;
  	}
  	return 0;
  }
  
  /*
   * SFR-CACC algorithm:
   * 3.2) Else if CYCLING_CHANGEOVER is 1, and t is less
   * than next_tsn_at_change of the current primary, then
   * the sender MUST NOT increment missing report count
   * for t.
   */
  static inline int sctp_cacc_skip_3_2(struct sctp_transport *primary, __u32 tsn)
  {
  	if (primary->cacc.cycling_changeover &&
  	    TSN_lt(tsn, primary->cacc.next_tsn_at_change))
  		return 1;
  	return 0;
  }
  
  /*
   * SFR-CACC algorithm:
   * 3) If the missing report count for TSN t is to be
   * incremented according to [RFC2960] and
   * [SCTP_STEWART-2002], and CHANGEOVER_ACTIVE is set,
25985edce   Lucas De Marchi   Fix common misspe...
177
   * then the sender MUST further execute steps 3.1 and
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
181
182
   * 3.2 to determine if the missing report count for
   * TSN t SHOULD NOT be incremented.
   *
   * 3.3) If 3.1 and 3.2 do not dictate that the missing
   * report count for t should not be incremented, then
25985edce   Lucas De Marchi   Fix common misspe...
183
   * the sender SHOULD increment missing report count for
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
188
189
190
191
   * t (according to [RFC2960] and [SCTP_STEWART_2002]).
   */
  static inline int sctp_cacc_skip(struct sctp_transport *primary,
  				 struct sctp_transport *transport,
  				 int count_of_newacks,
  				 __u32 tsn)
  {
  	if (primary->cacc.changeover_active &&
f64f9e719   Joe Perches   net: Move && and ...
192
193
  	    (sctp_cacc_skip_3_1(primary, transport, count_of_newacks) ||
  	     sctp_cacc_skip_3_2(primary, tsn)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
198
199
200
201
202
203
204
  		return 1;
  	return 0;
  }
  
  /* Initialize an existing sctp_outq.  This does the boring stuff.
   * You still need to define handlers if you really want to DO
   * something with this structure...
   */
  void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
  {
  	q->asoc = asoc;
79af02c25   David S. Miller   [SCTP]: Use struc...
205
206
  	INIT_LIST_HEAD(&q->out_chunk_list);
  	INIT_LIST_HEAD(&q->control_chunk_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
  	INIT_LIST_HEAD(&q->retransmit);
  	INIT_LIST_HEAD(&q->sacked);
  	INIT_LIST_HEAD(&q->abandoned);
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
210
  	q->fast_rtx = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
213
214
215
216
217
218
219
220
221
222
223
  	q->outstanding_bytes = 0;
  	q->empty = 1;
  	q->cork  = 0;
  
  	q->malloced = 0;
  	q->out_qlen = 0;
  }
  
  /* Free the outqueue structure and any related pending chunks.
   */
  void sctp_outq_teardown(struct sctp_outq *q)
  {
  	struct sctp_transport *transport;
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
224
  	struct list_head *lchunk, *temp;
79af02c25   David S. Miller   [SCTP]: Use struc...
225
  	struct sctp_chunk *chunk, *tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
  
  	/* Throw away unacknowledged chunks. */
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
228
229
  	list_for_each_entry(transport, &q->asoc->peer.transport_addr_list,
  			transports) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  		while ((lchunk = sctp_list_dequeue(&transport->transmitted)) != NULL) {
  			chunk = list_entry(lchunk, struct sctp_chunk,
  					   transmitted_list);
  			/* Mark as part of a failed message. */
  			sctp_chunk_fail(chunk, q->error);
  			sctp_chunk_free(chunk);
  		}
  	}
  
  	/* Throw away chunks that have been gap ACKed.  */
  	list_for_each_safe(lchunk, temp, &q->sacked) {
  		list_del_init(lchunk);
  		chunk = list_entry(lchunk, struct sctp_chunk,
  				   transmitted_list);
  		sctp_chunk_fail(chunk, q->error);
  		sctp_chunk_free(chunk);
  	}
  
  	/* Throw away any chunks in the retransmit queue. */
  	list_for_each_safe(lchunk, temp, &q->retransmit) {
  		list_del_init(lchunk);
  		chunk = list_entry(lchunk, struct sctp_chunk,
  				   transmitted_list);
  		sctp_chunk_fail(chunk, q->error);
  		sctp_chunk_free(chunk);
  	}
  
  	/* Throw away any chunks that are in the abandoned queue. */
  	list_for_each_safe(lchunk, temp, &q->abandoned) {
  		list_del_init(lchunk);
  		chunk = list_entry(lchunk, struct sctp_chunk,
  				   transmitted_list);
  		sctp_chunk_fail(chunk, q->error);
  		sctp_chunk_free(chunk);
  	}
  
  	/* Throw away any leftover data chunks. */
  	while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
  
  		/* Mark as send failure. */
  		sctp_chunk_fail(chunk, q->error);
  		sctp_chunk_free(chunk);
  	}
  
  	q->error = 0;
  
  	/* Throw away any leftover control chunks. */
79af02c25   David S. Miller   [SCTP]: Use struc...
277
278
  	list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
  		list_del_init(&chunk->list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
  		sctp_chunk_free(chunk);
79af02c25   David S. Miller   [SCTP]: Use struc...
280
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  }
  
  /* Free the outqueue structure and any related pending chunks.  */
  void sctp_outq_free(struct sctp_outq *q)
  {
  	/* Throw away leftover chunks. */
  	sctp_outq_teardown(q);
  
  	/* If we were kmalloc()'d, free the memory.  */
  	if (q->malloced)
  		kfree(q);
  }
  
  /* Put a new chunk in an sctp_outq.  */
  int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
  {
  	int error = 0;
  
  	SCTP_DEBUG_PRINTK("sctp_outq_tail(%p, %p[%s])
  ",
  			  q, chunk, chunk && chunk->chunk_hdr ?
  			  sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
  			  : "Illegal Chunk");
  
  	/* If it is data, queue it up, otherwise, send it
  	 * immediately.
  	 */
ec7b95195   Shan Wei   sctp: use sctp_ch...
308
  	if (sctp_chunk_is_data(chunk)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
310
311
312
313
314
315
316
317
318
  		/* Is it OK to queue data chunks?  */
  		/* From 9. Termination of Association
  		 *
  		 * When either endpoint performs a shutdown, the
  		 * association on each peer will stop accepting new
  		 * data from its user and only deliver data in queue
  		 * at the time of sending or receiving the SHUTDOWN
  		 * chunk.
  		 */
  		switch (q->asoc->state) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
  		case SCTP_STATE_CLOSED:
  		case SCTP_STATE_SHUTDOWN_PENDING:
  		case SCTP_STATE_SHUTDOWN_SENT:
  		case SCTP_STATE_SHUTDOWN_RECEIVED:
  		case SCTP_STATE_SHUTDOWN_ACK_SENT:
  			/* Cannot send after transport endpoint shutdown */
  			error = -ESHUTDOWN;
  			break;
  
  		default:
  			SCTP_DEBUG_PRINTK("outqueueing (%p, %p[%s])
  ",
  			  q, chunk, chunk && chunk->chunk_hdr ?
  			  sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type))
  			  : "Illegal Chunk");
  
  			sctp_outq_tail_data(q, chunk);
  			if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
  				SCTP_INC_STATS(SCTP_MIB_OUTUNORDERCHUNKS);
  			else
  				SCTP_INC_STATS(SCTP_MIB_OUTORDERCHUNKS);
  			q->empty = 0;
  			break;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
342
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
  	} else {
79af02c25   David S. Miller   [SCTP]: Use struc...
344
  		list_add_tail(&chunk->list, &q->control_chunk_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  		SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  	}
  
  	if (error < 0)
  		return error;
  
  	if (!q->cork)
  		error = sctp_outq_flush(q, 0);
  
  	return error;
  }
  
  /* Insert a chunk into the sorted list based on the TSNs.  The retransmit list
   * and the abandoned list are in ascending order.
   */
  static void sctp_insert_list(struct list_head *head, struct list_head *new)
  {
  	struct list_head *pos;
  	struct sctp_chunk *nchunk, *lchunk;
  	__u32 ntsn, ltsn;
  	int done = 0;
  
  	nchunk = list_entry(new, struct sctp_chunk, transmitted_list);
  	ntsn = ntohl(nchunk->subh.data_hdr->tsn);
  
  	list_for_each(pos, head) {
  		lchunk = list_entry(pos, struct sctp_chunk, transmitted_list);
  		ltsn = ntohl(lchunk->subh.data_hdr->tsn);
  		if (TSN_lt(ntsn, ltsn)) {
  			list_add(new, pos->prev);
  			done = 1;
  			break;
  		}
  	}
  	if (!done)
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
380
  		list_add_tail(new, head);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
  }
  
  /* Mark all the eligible packets on a transport for retransmission.  */
  void sctp_retransmit_mark(struct sctp_outq *q,
  			  struct sctp_transport *transport,
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
386
  			  __u8 reason)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
390
391
392
393
394
395
396
397
398
399
  {
  	struct list_head *lchunk, *ltemp;
  	struct sctp_chunk *chunk;
  
  	/* Walk through the specified transmitted queue.  */
  	list_for_each_safe(lchunk, ltemp, &transport->transmitted) {
  		chunk = list_entry(lchunk, struct sctp_chunk,
  				   transmitted_list);
  
  		/* If the chunk is abandoned, move it to abandoned list. */
  		if (sctp_chunk_abandoned(chunk)) {
  			list_del_init(lchunk);
  			sctp_insert_list(&q->abandoned, lchunk);
8c4a2d41a   Vlad Yasevich   [SCTP]: Fix conne...
400
401
402
403
404
405
406
  
  			/* If this chunk has not been previousely acked,
  			 * stop considering it 'outstanding'.  Our peer
  			 * will most likely never see it since it will
  			 * not be retransmitted
  			 */
  			if (!chunk->tsn_gap_acked) {
31b02e154   Vlad Yasevich   sctp: Failover tr...
407
408
409
  				if (chunk->transport)
  					chunk->transport->flight_size -=
  							sctp_data_size(chunk);
8c4a2d41a   Vlad Yasevich   [SCTP]: Fix conne...
410
  				q->outstanding_bytes -= sctp_data_size(chunk);
a76c0adf6   Thomas Graf   sctp: Do not acco...
411
  				q->asoc->peer.rwnd += sctp_data_size(chunk);
8c4a2d41a   Vlad Yasevich   [SCTP]: Fix conne...
412
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
  			continue;
  		}
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
415
416
417
  		/* If we are doing  retransmission due to a timeout or pmtu
  		 * discovery, only the  chunks that are not yet acked should
  		 * be added to the retransmit queue.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
  		 */
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
419
  		if ((reason == SCTP_RTXR_FAST_RTX  &&
c226ef9b8   Neil Horman   sctp: reduce memo...
420
  			    (chunk->fast_retransmit == SCTP_NEED_FRTX)) ||
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
421
  		    (reason != SCTP_RTXR_FAST_RTX  && !chunk->tsn_gap_acked)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
424
425
426
427
428
429
  			/* RFC 2960 6.2.1 Processing a Received SACK
  			 *
  			 * C) Any time a DATA chunk is marked for
  			 * retransmission (via either T3-rtx timer expiration
  			 * (Section 6.3.3) or via fast retransmit
  			 * (Section 7.2.4)), add the data size of those
  			 * chunks to the rwnd.
  			 */
a76c0adf6   Thomas Graf   sctp: Do not acco...
430
  			q->asoc->peer.rwnd += sctp_data_size(chunk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  			q->outstanding_bytes -= sctp_data_size(chunk);
31b02e154   Vlad Yasevich   sctp: Failover tr...
432
433
  			if (chunk->transport)
  				transport->flight_size -= sctp_data_size(chunk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  
  			/* sctpimpguide-05 Section 2.8.2
  			 * M5) If a T3-rtx timer expires, the
  			 * 'TSN.Missing.Report' of all affected TSNs is set
  			 * to 0.
  			 */
  			chunk->tsn_missing_report = 0;
  
  			/* If a chunk that is being used for RTT measurement
  			 * has to be retransmitted, we cannot use this chunk
  			 * anymore for RTT measurements. Reset rto_pending so
  			 * that a new RTT measurement is started when a new
  			 * data chunk is sent.
  			 */
  			if (chunk->rtt_in_progress) {
  				chunk->rtt_in_progress = 0;
  				transport->rto_pending = 0;
  			}
  
  			/* Move the chunk to the retransmit queue. The chunks
  			 * on the retransmit queue are always kept in order.
  			 */
  			list_del_init(lchunk);
  			sctp_insert_list(&q->retransmit, lchunk);
  		}
  	}
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
460
  	SCTP_DEBUG_PRINTK("%s: transport: %p, reason: %d, "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
  			  "cwnd: %d, ssthresh: %d, flight_size: %d, "
0dc47877a   Harvey Harrison   net: replace rema...
462
463
  			  "pba: %d
  ", __func__,
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
464
  			  transport, reason,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
466
467
468
469
470
471
472
473
474
475
476
477
  			  transport->cwnd, transport->ssthresh,
  			  transport->flight_size,
  			  transport->partial_bytes_acked);
  
  }
  
  /* Mark all the eligible packets on a transport for retransmission and force
   * one packet out.
   */
  void sctp_retransmit(struct sctp_outq *q, struct sctp_transport *transport,
  		     sctp_retransmit_reason_t reason)
  {
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
  
  	switch(reason) {
  	case SCTP_RTXR_T3_RTX:
ac0b04627   Sridhar Samudrala   [SCTP]: Extend /p...
481
  		SCTP_INC_STATS(SCTP_MIB_T3_RETRANSMITS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
483
484
485
486
487
  		sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_T3_RTX);
  		/* Update the retran path if the T3-rtx timer has expired for
  		 * the current retran path.
  		 */
  		if (transport == transport->asoc->peer.retran_path)
  			sctp_assoc_update_retran_path(transport->asoc);
58fbbed4f   Neil Horman   [SCTP]: extend ex...
488
489
  		transport->asoc->rtx_data_chunks +=
  			transport->asoc->unack_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
  		break;
  	case SCTP_RTXR_FAST_RTX:
ac0b04627   Sridhar Samudrala   [SCTP]: Extend /p...
492
  		SCTP_INC_STATS(SCTP_MIB_FAST_RETRANSMITS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
  		sctp_transport_lower_cwnd(transport, SCTP_LOWER_CWND_FAST_RTX);
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
494
  		q->fast_rtx = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
496
  		break;
  	case SCTP_RTXR_PMTUD:
ac0b04627   Sridhar Samudrala   [SCTP]: Extend /p...
497
  		SCTP_INC_STATS(SCTP_MIB_PMTUD_RETRANSMITS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498
  		break;
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
499
500
  	case SCTP_RTXR_T1_RTX:
  		SCTP_INC_STATS(SCTP_MIB_T1_RETRANSMITS);
58fbbed4f   Neil Horman   [SCTP]: extend ex...
501
  		transport->asoc->init_retries++;
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
502
  		break;
ac0b04627   Sridhar Samudrala   [SCTP]: Extend /p...
503
504
  	default:
  		BUG();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
  	}
b6157d8e0   Vlad Yasevich   SCTP: Fix differe...
506
  	sctp_retransmit_mark(q, transport, reason);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
  
  	/* PR-SCTP A5) Any time the T3-rtx timer expires, on any destination,
  	 * the sender SHOULD try to advance the "Advanced.Peer.Ack.Point" by
  	 * following the procedures outlined in C1 - C5.
  	 */
8b750ce54   Vlad Yasevich   sctp: Flush the q...
512
513
  	if (reason == SCTP_RTXR_T3_RTX)
  		sctp_generate_fwdtsn(q, q->asoc->ctsn_ack_point);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514

8b750ce54   Vlad Yasevich   sctp: Flush the q...
515
516
517
518
519
520
  	/* Flush the queues only on timeout, since fast_rtx is only
  	 * triggered during sack processing and the queue
  	 * will be flushed at the end.
  	 */
  	if (reason != SCTP_RTXR_FAST_RTX)
  		error = sctp_outq_flush(q, /* rtx_timeout */ 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  
  	if (error)
  		q->asoc->base.sk->sk_err = -error;
  }
  
  /*
   * Transmit DATA chunks on the retransmit queue.  Upon return from
   * sctp_outq_flush_rtx() the packet 'pkt' may contain chunks which
   * need to be transmitted by the caller.
   * We assume that pkt->transport has already been set.
   *
   * The return value is a normal kernel error return value.
   */
  static int sctp_outq_flush_rtx(struct sctp_outq *q, struct sctp_packet *pkt,
  			       int rtx_timeout, int *start_timer)
  {
  	struct list_head *lqueue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
  	struct sctp_transport *transport = pkt->transport;
  	sctp_xmit_t status;
  	struct sctp_chunk *chunk, *chunk1;
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
541
  	int fast_rtx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
542
  	int error = 0;
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
543
  	int timer = 0;
8b750ce54   Vlad Yasevich   sctp: Flush the q...
544
  	int done = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
546
  	lqueue = &q->retransmit;
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
547
  	fast_rtx = q->fast_rtx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548

8b750ce54   Vlad Yasevich   sctp: Flush the q...
549
550
551
552
  	/* This loop handles time-out retransmissions, fast retransmissions,
  	 * and retransmissions due to opening of whindow.
  	 *
  	 * RFC 2960 6.3.3 Handle T3-rtx Expiration
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
553
554
555
556
557
558
559
560
561
562
563
564
565
566
  	 *
  	 * E3) Determine how many of the earliest (i.e., lowest TSN)
  	 * outstanding DATA chunks for the address for which the
  	 * T3-rtx has expired will fit into a single packet, subject
  	 * to the MTU constraint for the path corresponding to the
  	 * destination transport address to which the retransmission
  	 * is being sent (this may be different from the address for
  	 * which the timer expires [see Section 6.4]). Call this value
  	 * K. Bundle and retransmit those K DATA chunks in a single
  	 * packet to the destination endpoint.
  	 *
  	 * [Just to be painfully clear, if we are retransmitting
  	 * because a timeout just happened, we should send only ONE
  	 * packet of retransmitted data.]
8b750ce54   Vlad Yasevich   sctp: Flush the q...
567
568
569
570
  	 *
  	 * For fast retransmissions we also send only ONE packet.  However,
  	 * if we are just flushing the queue due to open window, we'll
  	 * try to send as much as possible.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571
  	 */
8b750ce54   Vlad Yasevich   sctp: Flush the q...
572
  	list_for_each_entry_safe(chunk, chunk1, lqueue, transmitted_list) {
4c6a6f421   Wei Yongjun   sctp: move chunk ...
573
574
575
576
577
578
579
  		/* If the chunk is abandoned, move it to abandoned list. */
  		if (sctp_chunk_abandoned(chunk)) {
  			list_del_init(&chunk->transmitted_list);
  			sctp_insert_list(&q->abandoned,
  					 &chunk->transmitted_list);
  			continue;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
581
582
583
584
585
586
  
  		/* Make sure that Gap Acked TSNs are not retransmitted.  A
  		 * simple approach is just to move such TSNs out of the
  		 * way and into a 'transmitted' queue and skip to the
  		 * next chunk.
  		 */
  		if (chunk->tsn_gap_acked) {
8b750ce54   Vlad Yasevich   sctp: Flush the q...
587
588
589
  			list_del(&chunk->transmitted_list);
  			list_add_tail(&chunk->transmitted_list,
  					&transport->transmitted);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
591
  			continue;
  		}
8b750ce54   Vlad Yasevich   sctp: Flush the q...
592
593
594
595
596
  		/* If we are doing fast retransmit, ignore non-fast_rtransmit
  		 * chunks
  		 */
  		if (fast_rtx && !chunk->fast_retransmit)
  			continue;
bc4f841a0   Wei Yongjun   sctp: fix to retr...
597
  redo:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
598
599
600
601
602
  		/* Attempt to append this chunk to the packet. */
  		status = sctp_packet_append_chunk(pkt, chunk);
  
  		switch (status) {
  		case SCTP_XMIT_PMTU_FULL:
bc4f841a0   Wei Yongjun   sctp: fix to retr...
603
604
605
606
607
608
609
610
611
612
  			if (!pkt->has_data && !pkt->has_cookie_echo) {
  				/* If this packet did not contain DATA then
  				 * retransmission did not happen, so do it
  				 * again.  We'll ignore the error here since
  				 * control chunks are already freed so there
  				 * is nothing we can do.
  				 */
  				sctp_packet_transmit(pkt);
  				goto redo;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
613
  			/* Send this packet.  */
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
614
  			error = sctp_packet_transmit(pkt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
615
616
617
  
  			/* If we are retransmitting, we should only
  			 * send a single packet.
f246a7b7c   Vlad Yasevich   sctp: teach CACC ...
618
  			 * Otherwise, try appending this chunk again.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
  			 */
8b750ce54   Vlad Yasevich   sctp: Flush the q...
620
621
  			if (rtx_timeout || fast_rtx)
  				done = 1;
f246a7b7c   Vlad Yasevich   sctp: teach CACC ...
622
623
  			else
  				goto redo;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624

8b750ce54   Vlad Yasevich   sctp: Flush the q...
625
  			/* Bundle next chunk in the next round.  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
628
  			break;
  
  		case SCTP_XMIT_RWND_FULL:
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
629
  			/* Send this packet. */
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
630
  			error = sctp_packet_transmit(pkt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
631
632
633
634
  
  			/* Stop sending DATA as there is no more room
  			 * at the receiver.
  			 */
8b750ce54   Vlad Yasevich   sctp: Flush the q...
635
  			done = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
636
637
638
  			break;
  
  		case SCTP_XMIT_NAGLE_DELAY:
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
639
  			/* Send this packet. */
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
640
  			error = sctp_packet_transmit(pkt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
  
  			/* Stop sending DATA because of nagle delay. */
8b750ce54   Vlad Yasevich   sctp: Flush the q...
643
  			done = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
645
646
647
648
649
  			break;
  
  		default:
  			/* The append was successful, so add this chunk to
  			 * the transmitted list.
  			 */
8b750ce54   Vlad Yasevich   sctp: Flush the q...
650
651
652
  			list_del(&chunk->transmitted_list);
  			list_add_tail(&chunk->transmitted_list,
  					&transport->transmitted);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653

d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
654
  			/* Mark the chunk as ineligible for fast retransmit
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
656
  			 * after it is retransmitted.
  			 */
c226ef9b8   Neil Horman   sctp: reduce memo...
657
658
  			if (chunk->fast_retransmit == SCTP_NEED_FRTX)
  				chunk->fast_retransmit = SCTP_DONT_FRTX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
659

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  			q->empty = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
  			break;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
662
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663

62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
664
665
666
  		/* Set the timer if there were no errors */
  		if (!error && !timer)
  			timer = 1;
8b750ce54   Vlad Yasevich   sctp: Flush the q...
667
668
669
670
671
672
673
674
675
676
677
  		if (done)
  			break;
  	}
  
  	/* If we are here due to a retransmit timeout or a fast
  	 * retransmit and if there are any chunks left in the retransmit
  	 * queue that could not fit in the PMTU sized packet, they need
  	 * to be marked as ineligible for a subsequent fast retransmit.
  	 */
  	if (rtx_timeout || fast_rtx) {
  		list_for_each_entry(chunk1, lqueue, transmitted_list) {
c226ef9b8   Neil Horman   sctp: reduce memo...
678
679
  			if (chunk1->fast_retransmit == SCTP_NEED_FRTX)
  				chunk1->fast_retransmit = SCTP_DONT_FRTX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
681
  		}
  	}
62aeaff5c   Vlad Yasevich   sctp: Start T3-RT...
682
683
684
685
686
  	*start_timer = timer;
  
  	/* Clear fast retransmit hint */
  	if (fast_rtx)
  		q->fast_rtx = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
689
690
691
692
693
  	return error;
  }
  
  /* Cork the outqueue so queued chunks are really queued. */
  int sctp_outq_uncork(struct sctp_outq *q)
  {
  	int error = 0;
7d54dc687   Vlad Yasevich   SCTP: Always flus...
694
  	if (q->cork)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
695
  		q->cork = 0;
7d54dc687   Vlad Yasevich   SCTP: Always flus...
696
  	error = sctp_outq_flush(q, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
698
  	return error;
  }
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
699

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
700
701
702
703
704
705
706
707
708
  /*
   * Try to flush an outqueue.
   *
   * Description: Send everything in q which we legally can, subject to
   * congestion limitations.
   * * Note: This function can be called from multiple contexts so appropriate
   * locking concerns must be made.  Today we use the sock lock to protect
   * this function.
   */
abd0b198e   Adrian Bunk   sctp: make sctp_o...
709
  static int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
711
712
713
714
715
716
  {
  	struct sctp_packet *packet;
  	struct sctp_packet singleton;
  	struct sctp_association *asoc = q->asoc;
  	__u16 sport = asoc->base.bind_addr.port;
  	__u16 dport = asoc->peer.port;
  	__u32 vtag = asoc->peer.i.init_tag;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
  	struct sctp_transport *transport = NULL;
  	struct sctp_transport *new_transport;
79af02c25   David S. Miller   [SCTP]: Use struc...
719
  	struct sctp_chunk *chunk, *tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
721
722
  	sctp_xmit_t status;
  	int error = 0;
  	int start_timer = 0;
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
723
  	int one_packet = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
  
  	/* These transports have chunks to send. */
  	struct list_head transport_list;
  	struct list_head *ltransport;
  
  	INIT_LIST_HEAD(&transport_list);
  	packet = NULL;
  
  	/*
  	 * 6.10 Bundling
  	 *   ...
  	 *   When bundling control chunks with DATA chunks, an
  	 *   endpoint MUST place control chunks first in the outbound
  	 *   SCTP packet.  The transmitter MUST transmit DATA chunks
  	 *   within a SCTP packet in increasing order of TSN.
  	 *   ...
  	 */
79af02c25   David S. Miller   [SCTP]: Use struc...
741
  	list_for_each_entry_safe(chunk, tmp, &q->control_chunk_list, list) {
8a07eb0a5   Michio Honda   sctp: Add ASCONF ...
742
743
744
745
746
747
748
749
750
  		/* RFC 5061, 5.3
  		 * F1) This means that until such time as the ASCONF
  		 * containing the add is acknowledged, the sender MUST
  		 * NOT use the new IP address as a source for ANY SCTP
  		 * packet except on carrying an ASCONF Chunk.
  		 */
  		if (asoc->src_out_of_asoc_ok &&
  		    chunk->chunk_hdr->type != SCTP_CID_ASCONF)
  			continue;
79af02c25   David S. Miller   [SCTP]: Use struc...
751
  		list_del_init(&chunk->list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
752
753
754
755
  		/* Pick the right transport to use. */
  		new_transport = chunk->transport;
  
  		if (!new_transport) {
a08de64d0   Vlad Yasevich   [SCTP]: Update AS...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  			/*
  			 * If we have a prior transport pointer, see if
  			 * the destination address of the chunk
  			 * matches the destination address of the
  			 * current transport.  If not a match, then
  			 * try to look up the transport with a given
  			 * destination address.  We do this because
  			 * after processing ASCONFs, we may have new
  			 * transports created.
  			 */
  			if (transport &&
  			    sctp_cmp_addr_exact(&chunk->dest,
  						&transport->ipaddr))
  					new_transport = transport;
  			else
  				new_transport = sctp_assoc_lookup_paddr(asoc,
  								&chunk->dest);
  
  			/* if we still don't have a new transport, then
  			 * use the current active path.
  			 */
  			if (!new_transport)
  				new_transport = asoc->peer.active_path;
ad8fec172   Sridhar Samudrala   [SCTP]: Verify al...
779
780
  		} else if ((new_transport->state == SCTP_INACTIVE) ||
  			   (new_transport->state == SCTP_UNCONFIRMED)) {
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
781
782
  			/* If the chunk is Heartbeat or Heartbeat Ack,
  			 * send it to chunk->transport, even if it's
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
784
785
  			 * inactive.
  			 *
  			 * 3.3.6 Heartbeat Acknowledgement:
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
786
  			 * ...
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
788
789
  			 * A HEARTBEAT ACK is always sent to the source IP
  			 * address of the IP datagram containing the
  			 * HEARTBEAT chunk to which this ack is responding.
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
790
  			 * ...
a08de64d0   Vlad Yasevich   [SCTP]: Update AS...
791
792
  			 *
  			 * ASCONF_ACKs also must be sent to the source.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
794
  			 */
  			if (chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT &&
a08de64d0   Vlad Yasevich   [SCTP]: Update AS...
795
796
  			    chunk->chunk_hdr->type != SCTP_CID_HEARTBEAT_ACK &&
  			    chunk->chunk_hdr->type != SCTP_CID_ASCONF_ACK)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  				new_transport = asoc->peer.active_path;
  		}
  
  		/* Are we switching transports?
  		 * Take care of transport locks.
  		 */
  		if (new_transport != transport) {
  			transport = new_transport;
  			if (list_empty(&transport->send_ready)) {
  				list_add_tail(&transport->send_ready,
  					      &transport_list);
  			}
  			packet = &transport->packet;
  			sctp_packet_config(packet, vtag,
  					   asoc->peer.ecn_capable);
  		}
  
  		switch (chunk->chunk_hdr->type) {
  		/*
  		 * 6.10 Bundling
  		 *   ...
  		 *   An endpoint MUST NOT bundle INIT, INIT ACK or SHUTDOWN
  		 *   COMPLETE with any other chunks.  [Send them immediately.]
  		 */
  		case SCTP_CID_INIT:
  		case SCTP_CID_INIT_ACK:
  		case SCTP_CID_SHUTDOWN_COMPLETE:
  			sctp_packet_init(&singleton, transport, sport, dport);
  			sctp_packet_config(&singleton, vtag, 0);
  			sctp_packet_append_chunk(&singleton, chunk);
  			error = sctp_packet_transmit(&singleton);
  			if (error < 0)
  				return error;
  			break;
  
  		case SCTP_CID_ABORT:
f4ad85ca3   Gui Jianfeng   [SCTP]: Fix proto...
833
834
835
  			if (sctp_test_T_bit(chunk)) {
  				packet->vtag = asoc->c.my_vtag;
  			}
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
836
837
838
839
840
  		/* The following chunks are "response" chunks, i.e.
  		 * they are generated in response to something we
  		 * received.  If we are sending these, then we can
  		 * send only 1 packet containing these chunks.
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
  		case SCTP_CID_HEARTBEAT_ACK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
  		case SCTP_CID_SHUTDOWN_ACK:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
  		case SCTP_CID_COOKIE_ACK:
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
844
845
  		case SCTP_CID_COOKIE_ECHO:
  		case SCTP_CID_ERROR:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
  		case SCTP_CID_ECN_CWR:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
  		case SCTP_CID_ASCONF_ACK:
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
848
  			one_packet = 1;
25985edce   Lucas De Marchi   Fix common misspe...
849
  			/* Fall through */
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
850
851
852
853
854
855
  
  		case SCTP_CID_SACK:
  		case SCTP_CID_HEARTBEAT:
  		case SCTP_CID_SHUTDOWN:
  		case SCTP_CID_ECN_ECNE:
  		case SCTP_CID_ASCONF:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
856
  		case SCTP_CID_FWD_TSN:
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
857
858
859
860
861
  			status = sctp_packet_transmit_chunk(packet, chunk,
  							    one_packet);
  			if (status  != SCTP_XMIT_OK) {
  				/* put the chunk back */
  				list_add(&chunk->list, &q->control_chunk_list);
bd69b981a   Wei Yongjun   sctp: assure at l...
862
863
864
865
866
  			} else if (chunk->chunk_hdr->type == SCTP_CID_FWD_TSN) {
  				/* PR-SCTP C5) If a FORWARD TSN is sent, the
  				 * sender MUST assure that at least one T3-rtx
  				 * timer is running.
  				 */
d9efc2231   Vlad Yasevich   sctp: Do not forc...
867
  				sctp_transport_reset_timers(transport);
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
868
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
870
871
872
873
  			break;
  
  		default:
  			/* We built a chunk with an illegal type! */
  			BUG();
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
874
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
875
  	}
8a07eb0a5   Michio Honda   sctp: Add ASCONF ...
876
877
  	if (q->asoc->src_out_of_asoc_ok)
  		goto sctp_flush_out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
  	/* Is it OK to send data chunks?  */
  	switch (asoc->state) {
  	case SCTP_STATE_COOKIE_ECHOED:
  		/* Only allow bundling when this packet has a COOKIE-ECHO
  		 * chunk.
  		 */
  		if (!packet || !packet->has_cookie_echo)
  			break;
  
  		/* fallthru */
  	case SCTP_STATE_ESTABLISHED:
  	case SCTP_STATE_SHUTDOWN_PENDING:
  	case SCTP_STATE_SHUTDOWN_RECEIVED:
  		/*
  		 * RFC 2960 6.1  Transmission of DATA Chunks
  		 *
  		 * C) When the time comes for the sender to transmit,
  		 * before sending new DATA chunks, the sender MUST
  		 * first transmit any outstanding DATA chunks which
  		 * are marked for retransmission (limited by the
  		 * current cwnd).
  		 */
  		if (!list_empty(&q->retransmit)) {
f207c050f   Michio Honda   sctp: HEARTBEAT n...
901
902
  			if (asoc->peer.retran_path->state == SCTP_UNCONFIRMED)
  				goto sctp_flush_out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  			if (transport == asoc->peer.retran_path)
  				goto retran;
  
  			/* Switch transports & prepare the packet.  */
  
  			transport = asoc->peer.retran_path;
  
  			if (list_empty(&transport->send_ready)) {
  				list_add_tail(&transport->send_ready,
  					      &transport_list);
  			}
  
  			packet = &transport->packet;
  			sctp_packet_config(packet, vtag,
  					   asoc->peer.ecn_capable);
  		retran:
  			error = sctp_outq_flush_rtx(q, packet,
  						    rtx_timeout, &start_timer);
  
  			if (start_timer)
d9efc2231   Vlad Yasevich   sctp: Do not forc...
923
  				sctp_transport_reset_timers(transport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
926
927
928
929
930
931
932
933
934
935
936
  
  			/* This can happen on COOKIE-ECHO resend.  Only
  			 * one chunk can get bundled with a COOKIE-ECHO.
  			 */
  			if (packet->has_cookie_echo)
  				goto sctp_flush_out;
  
  			/* Don't send new data if there is still data
  			 * waiting to retransmit.
  			 */
  			if (!list_empty(&q->retransmit))
  				goto sctp_flush_out;
  		}
46d5a8085   Vlad Yasevich   sctp: Update max....
937
938
939
940
941
942
943
  		/* Apply Max.Burst limitation to the current transport in
  		 * case it will be used for new data.  We are going to
  		 * rest it before we return, but we want to apply the limit
  		 * to the currently queued data.
  		 */
  		if (transport)
  			sctp_transport_burst_limited(transport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
944
  		/* Finally, transmit new packets.  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
  		while ((chunk = sctp_outq_dequeue_data(q)) != NULL) {
  			/* RFC 2960 6.5 Every DATA chunk MUST carry a valid
  			 * stream identifier.
  			 */
  			if (chunk->sinfo.sinfo_stream >=
  			    asoc->c.sinit_num_ostreams) {
  
  				/* Mark as failed send. */
  				sctp_chunk_fail(chunk, SCTP_ERROR_INV_STRM);
  				sctp_chunk_free(chunk);
  				continue;
  			}
  
  			/* Has this chunk expired? */
  			if (sctp_chunk_abandoned(chunk)) {
  				sctp_chunk_fail(chunk, 0);
  				sctp_chunk_free(chunk);
  				continue;
  			}
  
  			/* If there is a specified transport, use it.
  			 * Otherwise, we want to use the active path.
  			 */
  			new_transport = chunk->transport;
3f7a87d2f   Frank Filz   [SCTP] sctp_conne...
969
  			if (!new_transport ||
ad8fec172   Sridhar Samudrala   [SCTP]: Verify al...
970
971
  			    ((new_transport->state == SCTP_INACTIVE) ||
  			     (new_transport->state == SCTP_UNCONFIRMED)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
  				new_transport = asoc->peer.active_path;
f207c050f   Michio Honda   sctp: HEARTBEAT n...
973
974
  			if (new_transport->state == SCTP_UNCONFIRMED)
  				continue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
  
  			/* Change packets if necessary.  */
  			if (new_transport != transport) {
  				transport = new_transport;
  
  				/* Schedule to have this transport's
  				 * packet flushed.
  				 */
  				if (list_empty(&transport->send_ready)) {
  					list_add_tail(&transport->send_ready,
  						      &transport_list);
  				}
  
  				packet = &transport->packet;
  				sctp_packet_config(packet, vtag,
  						   asoc->peer.ecn_capable);
46d5a8085   Vlad Yasevich   sctp: Update max....
991
992
993
994
  				/* We've switched transports, so apply the
  				 * Burst limit to the new transport.
  				 */
  				sctp_transport_burst_limited(transport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
  			}
  
  			SCTP_DEBUG_PRINTK("sctp_outq_flush(%p, %p[%s]), ",
  					  q, chunk,
  					  chunk && chunk->chunk_hdr ?
  					  sctp_cname(SCTP_ST_CHUNK(
  						  chunk->chunk_hdr->type))
  					  : "Illegal Chunk");
  
  			SCTP_DEBUG_PRINTK("TX TSN 0x%x skb->head "
  					"%p skb->users %d.
  ",
  					ntohl(chunk->subh.data_hdr->tsn),
  					chunk->skb ?chunk->skb->head : NULL,
  					chunk->skb ?
  					atomic_read(&chunk->skb->users) : -1);
  
  			/* Add the chunk to the packet.  */
2e3216cd5   Vlad Yasevich   sctp: Follow secu...
1013
  			status = sctp_packet_transmit_chunk(packet, chunk, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  
  			switch (status) {
  			case SCTP_XMIT_PMTU_FULL:
  			case SCTP_XMIT_RWND_FULL:
  			case SCTP_XMIT_NAGLE_DELAY:
  				/* We could not append this chunk, so put
  				 * the chunk back on the output queue.
  				 */
  				SCTP_DEBUG_PRINTK("sctp_outq_flush: could "
  					"not transmit TSN: 0x%x, status: %d
  ",
  					ntohl(chunk->subh.data_hdr->tsn),
  					status);
  				sctp_outq_head_data(q, chunk);
  				goto sctp_flush_out;
  				break;
  
  			case SCTP_XMIT_OK:
b93d64717   Wei Yongjun   sctp: implement t...
1032
1033
1034
1035
1036
1037
  				/* The sender is in the SHUTDOWN-PENDING state,
  				 * The sender MAY set the I-bit in the DATA
  				 * chunk header.
  				 */
  				if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING)
  					chunk->chunk_hdr->flags |= SCTP_DATA_SACK_IMM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1038
1039
1040
1041
1042
  				break;
  
  			default:
  				BUG();
  			}
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1043
  			/* BUG: We assume that the sctp_packet_transmit()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  			 * call below will succeed all the time and add the
  			 * chunk to the transmitted list and restart the
  			 * timers.
  			 * It is possible that the call can fail under OOM
  			 * conditions.
  			 *
  			 * Is this really a problem?  Won't this behave
  			 * like a lost TSN?
  			 */
  			list_add_tail(&chunk->transmitted_list,
  				      &transport->transmitted);
d9efc2231   Vlad Yasevich   sctp: Do not forc...
1055
  			sctp_transport_reset_timers(transport);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
  
  			q->empty = 0;
  
  			/* Only let one DATA chunk get bundled with a
  			 * COOKIE-ECHO chunk.
  			 */
  			if (packet->has_cookie_echo)
  				goto sctp_flush_out;
  		}
  		break;
  
  	default:
  		/* Do nothing.  */
  		break;
  	}
  
  sctp_flush_out:
  
  	/* Before returning, examine all the transports touched in
  	 * this call.  Right now, we bluntly force clear all the
  	 * transports.  Things might change after we implement Nagle.
  	 * But such an examination is still required.
  	 *
  	 * --xguo
  	 */
  	while ((ltransport = sctp_list_dequeue(&transport_list)) != NULL ) {
  		struct sctp_transport *t = list_entry(ltransport,
  						      struct sctp_transport,
  						      send_ready);
  		packet = &t->packet;
  		if (!sctp_packet_empty(packet))
  			error = sctp_packet_transmit(packet);
46d5a8085   Vlad Yasevich   sctp: Update max....
1088
1089
1090
  
  		/* Clear the burst limited state, if any */
  		sctp_transport_burst_reset(t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
  	}
  
  	return error;
  }
  
  /* Update unack_data based on the incoming SACK chunk */
  static void sctp_sack_update_unack_data(struct sctp_association *assoc,
  					struct sctp_sackhdr *sack)
  {
  	sctp_sack_variable_t *frags;
  	__u16 unack_data;
  	int i;
  
  	unack_data = assoc->next_tsn - assoc->ctsn_ack_point - 1;
  
  	frags = sack->variable;
  	for (i = 0; i < ntohs(sack->num_gap_ack_blocks); i++) {
  		unack_data -= ((ntohs(frags[i].gab.end) -
  				ntohs(frags[i].gab.start) + 1));
  	}
  
  	assoc->unack_data = unack_data;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
  /* This is where we REALLY process a SACK.
   *
   * Process the SACK against the outqueue.  Mostly, this just frees
   * things off the transmitted queue.
   */
  int sctp_outq_sack(struct sctp_outq *q, struct sctp_sackhdr *sack)
  {
  	struct sctp_association *asoc = q->asoc;
  	struct sctp_transport *transport;
  	struct sctp_chunk *tchunk = NULL;
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
1124
  	struct list_head *lchunk, *transport_list, *temp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1125
1126
1127
1128
1129
1130
1131
  	sctp_sack_variable_t *frags = sack->variable;
  	__u32 sack_ctsn, ctsn, tsn;
  	__u32 highest_tsn, highest_new_tsn;
  	__u32 sack_a_rwnd;
  	unsigned outstanding;
  	struct sctp_transport *primary = asoc->peer.primary_path;
  	int count_of_newacks = 0;
2cd9b822b   Vlad Yasevich   sctp: Only mark c...
1132
  	int gap_ack_blocks;
ea862c8d1   Vlad Yasevich   sctp: correctly m...
1133
  	u8 accum_moved = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
1135
1136
1137
1138
  
  	/* Grab the association's destination address list. */
  	transport_list = &asoc->peer.transport_addr_list;
  
  	sack_ctsn = ntohl(sack->cum_tsn_ack);
2cd9b822b   Vlad Yasevich   sctp: Only mark c...
1139
  	gap_ack_blocks = ntohs(sack->num_gap_ack_blocks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1140
1141
1142
1143
1144
1145
1146
1147
1148
  	/*
  	 * SFR-CACC algorithm:
  	 * On receipt of a SACK the sender SHOULD execute the
  	 * following statements.
  	 *
  	 * 1) If the cumulative ack in the SACK passes next tsn_at_change
  	 * on the current primary, the CHANGEOVER_ACTIVE flag SHOULD be
  	 * cleared. The CYCLING_CHANGEOVER flag SHOULD also be cleared for
  	 * all destinations.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1149
1150
1151
1152
1153
  	 * 2) If the SACK contains gap acks and the flag CHANGEOVER_ACTIVE
  	 * is set the receiver of the SACK MUST take the following actions:
  	 *
  	 * A) Initialize the cacc_saw_newack to 0 for all destination
  	 * addresses.
ab5216a5b   Vlad Yasevich   sctp: Optimize SF...
1154
1155
1156
  	 *
  	 * Only bother if changeover_active is set. Otherwise, this is
  	 * totally suboptimal to do on every SACK.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
  	 */
ab5216a5b   Vlad Yasevich   sctp: Optimize SF...
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
  	if (primary->cacc.changeover_active) {
  		u8 clear_cycling = 0;
  
  		if (TSN_lte(primary->cacc.next_tsn_at_change, sack_ctsn)) {
  			primary->cacc.changeover_active = 0;
  			clear_cycling = 1;
  		}
  
  		if (clear_cycling || gap_ack_blocks) {
  			list_for_each_entry(transport, transport_list,
  					transports) {
  				if (clear_cycling)
  					transport->cacc.cycling_changeover = 0;
  				if (gap_ack_blocks)
  					transport->cacc.cacc_saw_newack = 0;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1174
1175
1176
1177
1178
  		}
  	}
  
  	/* Get the highest TSN in the sack. */
  	highest_tsn = sack_ctsn;
2cd9b822b   Vlad Yasevich   sctp: Only mark c...
1179
1180
  	if (gap_ack_blocks)
  		highest_tsn += ntohs(frags[gap_ack_blocks - 1].gab.end);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181

bfa0d9843   Vlad Yasevich   sctp: Optimize co...
1182
  	if (TSN_lt(asoc->highest_sacked, highest_tsn))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1183
  		asoc->highest_sacked = highest_tsn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1184

bfa0d9843   Vlad Yasevich   sctp: Optimize co...
1185
  	highest_new_tsn = sack_ctsn;
2cd9b822b   Vlad Yasevich   sctp: Only mark c...
1186

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1187
1188
1189
  	/* Run through the retransmit queue.  Credit bytes received
  	 * and free those chunks that we can.
  	 */
bfa0d9843   Vlad Yasevich   sctp: Optimize co...
1190
  	sctp_check_transmitted(q, &q->retransmit, NULL, sack, &highest_new_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1191
1192
1193
1194
1195
1196
  
  	/* Run through the transmitted queue.
  	 * Credit bytes received and free those chunks which we can.
  	 *
  	 * This is a MASSIVE candidate for optimization.
  	 */
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
1197
  	list_for_each_entry(transport, transport_list, transports) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1198
  		sctp_check_transmitted(q, &transport->transmitted,
bfa0d9843   Vlad Yasevich   sctp: Optimize co...
1199
  				       transport, sack, &highest_new_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1200
1201
1202
1203
1204
1205
1206
1207
  		/*
  		 * SFR-CACC algorithm:
  		 * C) Let count_of_newacks be the number of
  		 * destinations for which cacc_saw_newack is set.
  		 */
  		if (transport->cacc.cacc_saw_newack)
  			count_of_newacks ++;
  	}
ea862c8d1   Vlad Yasevich   sctp: correctly m...
1208
1209
1210
1211
1212
  	/* Move the Cumulative TSN Ack Point if appropriate.  */
  	if (TSN_lt(asoc->ctsn_ack_point, sack_ctsn)) {
  		asoc->ctsn_ack_point = sack_ctsn;
  		accum_moved = 1;
  	}
2cd9b822b   Vlad Yasevich   sctp: Only mark c...
1213
  	if (gap_ack_blocks) {
ea862c8d1   Vlad Yasevich   sctp: correctly m...
1214
1215
1216
  
  		if (asoc->fast_recovery && accum_moved)
  			highest_new_tsn = highest_tsn;
2cd9b822b   Vlad Yasevich   sctp: Only mark c...
1217
1218
1219
  		list_for_each_entry(transport, transport_list, transports)
  			sctp_mark_missing(q, &transport->transmitted, transport,
  					  highest_new_tsn, count_of_newacks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1220
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
  	/* Update unack_data field in the assoc. */
  	sctp_sack_update_unack_data(asoc, sack);
  
  	ctsn = asoc->ctsn_ack_point;
  
  	/* Throw away stuff rotting on the sack queue.  */
  	list_for_each_safe(lchunk, temp, &q->sacked) {
  		tchunk = list_entry(lchunk, struct sctp_chunk,
  				    transmitted_list);
  		tsn = ntohl(tchunk->subh.data_hdr->tsn);
5f9646c3d   Vlad Yasevich   [SCTP]: Make sure...
1231
1232
  		if (TSN_lte(tsn, ctsn)) {
  			list_del_init(&tchunk->transmitted_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1233
  			sctp_chunk_free(tchunk);
5f9646c3d   Vlad Yasevich   [SCTP]: Make sure...
1234
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
  	}
  
  	/* ii) Set rwnd equal to the newly received a_rwnd minus the
  	 *     number of bytes still outstanding after processing the
  	 *     Cumulative TSN Ack and the Gap Ack Blocks.
  	 */
  
  	sack_a_rwnd = ntohl(sack->a_rwnd);
  	outstanding = q->outstanding_bytes;
  
  	if (outstanding < sack_a_rwnd)
  		sack_a_rwnd -= outstanding;
  	else
  		sack_a_rwnd = 0;
  
  	asoc->peer.rwnd = sack_a_rwnd;
  
  	sctp_generate_fwdtsn(q, sack_ctsn);
  
  	SCTP_DEBUG_PRINTK("%s: sack Cumulative TSN Ack is 0x%x.
  ",
0dc47877a   Harvey Harrison   net: replace rema...
1256
  			  __func__, sack_ctsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1257
1258
1259
  	SCTP_DEBUG_PRINTK("%s: Cumulative TSN Ack of association, "
  			  "%p is 0x%x. Adv peer ack point: 0x%x
  ",
0dc47877a   Harvey Harrison   net: replace rema...
1260
  			  __func__, asoc, ctsn, asoc->adv_peer_ack_point);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1261
1262
1263
1264
  
  	/* See if all chunks are acked.
  	 * Make sure the empty queue handler will get run later.
  	 */
79af02c25   David S. Miller   [SCTP]: Use struc...
1265
  	q->empty = (list_empty(&q->out_chunk_list) &&
79af02c25   David S. Miller   [SCTP]: Use struc...
1266
  		    list_empty(&q->retransmit));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1267
1268
  	if (!q->empty)
  		goto finish;
9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
1269
  	list_for_each_entry(transport, transport_list, transports) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
  		q->empty = q->empty && list_empty(&transport->transmitted);
  		if (!q->empty)
  			goto finish;
  	}
  
  	SCTP_DEBUG_PRINTK("sack queue is empty.
  ");
  finish:
  	return q->empty;
  }
  
  /* Is the outqueue empty?  */
  int sctp_outq_is_empty(const struct sctp_outq *q)
  {
  	return q->empty;
  }
  
  /********************************************************************
   * 2nd Level Abstractions
   ********************************************************************/
  
  /* Go through a transport's transmitted list or the association's retransmit
   * list and move chunks that are acked by the Cumulative TSN Ack to q->sacked.
   * The retransmit list will not have an associated transport.
   *
   * I added coherent debug information output.	--xguo
   *
   * Instead of printing 'sacked' or 'kept' for each TSN on the
   * transmitted_queue, we print a range: SACKED: TSN1-TSN2, TSN3, TSN4-TSN5.
   * KEPT TSN6-TSN7, etc.
   */
  static void sctp_check_transmitted(struct sctp_outq *q,
  				   struct list_head *transmitted_queue,
  				   struct sctp_transport *transport,
  				   struct sctp_sackhdr *sack,
bfa0d9843   Vlad Yasevich   sctp: Optimize co...
1305
  				   __u32 *highest_new_tsn_in_sack)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1306
1307
1308
1309
1310
1311
1312
1313
1314
  {
  	struct list_head *lchunk;
  	struct sctp_chunk *tchunk;
  	struct list_head tlist;
  	__u32 tsn;
  	__u32 sack_ctsn;
  	__u32 rtt;
  	__u8 restart_timer = 0;
  	int bytes_acked = 0;
31b02e154   Vlad Yasevich   sctp: Failover tr...
1315
  	int migrate_bytes = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
  
  	/* These state variables are for coherent debug output. --xguo */
  
  #if SCTP_DEBUG
  	__u32 dbg_ack_tsn = 0;	/* An ACKed TSN range starts here... */
  	__u32 dbg_last_ack_tsn = 0;  /* ...and finishes here.	     */
  	__u32 dbg_kept_tsn = 0;	/* An un-ACKed range starts here...  */
  	__u32 dbg_last_kept_tsn = 0; /* ...and finishes here.	     */
  
  	/* 0 : The last TSN was ACKed.
  	 * 1 : The last TSN was NOT ACKed (i.e. KEPT).
  	 * -1: We need to initialize.
  	 */
  	int dbg_prt_state = -1;
  #endif /* SCTP_DEBUG */
  
  	sack_ctsn = ntohl(sack->cum_tsn_ack);
  
  	INIT_LIST_HEAD(&tlist);
  
  	/* The while loop will skip empty transmitted queues. */
  	while (NULL != (lchunk = sctp_list_dequeue(transmitted_queue))) {
  		tchunk = list_entry(lchunk, struct sctp_chunk,
  				    transmitted_list);
  
  		if (sctp_chunk_abandoned(tchunk)) {
  			/* Move the chunk to abandoned list. */
  			sctp_insert_list(&q->abandoned, lchunk);
8c4a2d41a   Vlad Yasevich   [SCTP]: Fix conne...
1344
1345
1346
1347
1348
  
  			/* If this chunk has not been acked, stop
  			 * considering it as 'outstanding'.
  			 */
  			if (!tchunk->tsn_gap_acked) {
31b02e154   Vlad Yasevich   sctp: Failover tr...
1349
1350
1351
  				if (tchunk->transport)
  					tchunk->transport->flight_size -=
  							sctp_data_size(tchunk);
8c4a2d41a   Vlad Yasevich   [SCTP]: Fix conne...
1352
1353
  				q->outstanding_bytes -= sctp_data_size(tchunk);
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
  			continue;
  		}
  
  		tsn = ntohl(tchunk->subh.data_hdr->tsn);
  		if (sctp_acked(sack, tsn)) {
  			/* If this queue is the retransmit queue, the
  			 * retransmit timer has already reclaimed
  			 * the outstanding bytes for this chunk, so only
  			 * count bytes associated with a transport.
  			 */
  			if (transport) {
  				/* If this chunk is being used for RTT
  				 * measurement, calculate the RTT and update
  				 * the RTO using this value.
  				 *
  				 * 6.3.1 C5) Karn's algorithm: RTT measurements
  				 * MUST NOT be made using packets that were
  				 * retransmitted (and thus for which it is
  				 * ambiguous whether the reply was for the
  				 * first instance of the packet or a later
  				 * instance).
  				 */
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1376
  				if (!tchunk->tsn_gap_acked &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1377
  				    tchunk->rtt_in_progress) {
4c9f5d530   Vlad Yasevich   [SCTP] Reset rtt_...
1378
  					tchunk->rtt_in_progress = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1379
1380
1381
1382
1383
  					rtt = jiffies - tchunk->sent_at;
  					sctp_transport_update_rto(transport,
  								  rtt);
  				}
  			}
31b02e154   Vlad Yasevich   sctp: Failover tr...
1384
1385
1386
1387
1388
1389
1390
1391
1392
  
  			/* If the chunk hasn't been marked as ACKED,
  			 * mark it and account bytes_acked if the
  			 * chunk had a valid transport (it will not
  			 * have a transport if ASCONF had deleted it
  			 * while DATA was outstanding).
  			 */
  			if (!tchunk->tsn_gap_acked) {
  				tchunk->tsn_gap_acked = 1;
bfa0d9843   Vlad Yasevich   sctp: Optimize co...
1393
  				*highest_new_tsn_in_sack = tsn;
31b02e154   Vlad Yasevich   sctp: Failover tr...
1394
1395
1396
1397
  				bytes_acked += sctp_data_size(tchunk);
  				if (!tchunk->transport)
  					migrate_bytes += sctp_data_size(tchunk);
  			}
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1398
  			if (TSN_lte(tsn, sack_ctsn)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
  				/* RFC 2960  6.3.2 Retransmission Timer Rules
  				 *
  				 * R3) Whenever a SACK is received
  				 * that acknowledges the DATA chunk
  				 * with the earliest outstanding TSN
  				 * for that address, restart T3-rtx
  				 * timer for that address with its
  				 * current RTO.
  				 */
  				restart_timer = 1;
  
  				if (!tchunk->tsn_gap_acked) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
  					/*
  					 * SFR-CACC algorithm:
  					 * 2) If the SACK contains gap acks
  					 * and the flag CHANGEOVER_ACTIVE is
  					 * set the receiver of the SACK MUST
  					 * take the following action:
  					 *
  					 * B) For each TSN t being acked that
  					 * has not been acked in any SACK so
  					 * far, set cacc_saw_newack to 1 for
  					 * the destination that the TSN was
  					 * sent to.
  					 */
  					if (transport &&
  					    sack->num_gap_ack_blocks &&
  					    q->asoc->peer.primary_path->cacc.
  					    changeover_active)
  						transport->cacc.cacc_saw_newack
  							= 1;
  				}
  
  				list_add_tail(&tchunk->transmitted_list,
  					      &q->sacked);
  			} else {
  				/* RFC2960 7.2.4, sctpimpguide-05 2.8.2
  				 * M2) Each time a SACK arrives reporting
  				 * 'Stray DATA chunk(s)' record the highest TSN
  				 * reported as newly acknowledged, call this
  				 * value 'HighestTSNinSack'. A newly
  				 * acknowledged DATA chunk is one not
  				 * previously acknowledged in a SACK.
  				 *
  				 * When the SCTP sender of data receives a SACK
  				 * chunk that acknowledges, for the first time,
  				 * the receipt of a DATA chunk, all the still
  				 * unacknowledged DATA chunks whose TSN is
  				 * older than that newly acknowledged DATA
  				 * chunk, are qualified as 'Stray DATA chunks'.
  				 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
  				list_add_tail(lchunk, &tlist);
  			}
  
  #if SCTP_DEBUG
  			switch (dbg_prt_state) {
  			case 0:	/* last TSN was ACKed */
  				if (dbg_last_ack_tsn + 1 == tsn) {
  					/* This TSN belongs to the
  					 * current ACK range.
  					 */
  					break;
  				}
  
  				if (dbg_last_ack_tsn != dbg_ack_tsn) {
  					/* Display the end of the
  					 * current range.
  					 */
145ce502e   Joe Perches   net/sctp: Use pr_...
1467
1468
  					SCTP_DEBUG_PRINTK_CONT("-%08x",
  							       dbg_last_ack_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1469
1470
1471
  				}
  
  				/* Start a new range.  */
145ce502e   Joe Perches   net/sctp: Use pr_...
1472
  				SCTP_DEBUG_PRINTK_CONT(",%08x", tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1473
1474
1475
1476
1477
1478
  				dbg_ack_tsn = tsn;
  				break;
  
  			case 1:	/* The last TSN was NOT ACKed. */
  				if (dbg_last_kept_tsn != dbg_kept_tsn) {
  					/* Display the end of current range. */
145ce502e   Joe Perches   net/sctp: Use pr_...
1479
1480
  					SCTP_DEBUG_PRINTK_CONT("-%08x",
  							       dbg_last_kept_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1481
  				}
145ce502e   Joe Perches   net/sctp: Use pr_...
1482
1483
  				SCTP_DEBUG_PRINTK_CONT("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1484
1485
1486
1487
1488
1489
1490
1491
  
  				/* FALL THROUGH... */
  			default:
  				/* This is the first-ever TSN we examined.  */
  				/* Start a new range of ACK-ed TSNs.  */
  				SCTP_DEBUG_PRINTK("ACKed: %08x", tsn);
  				dbg_prt_state = 0;
  				dbg_ack_tsn = tsn;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
1492
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1493
1494
1495
1496
1497
1498
1499
1500
1501
  
  			dbg_last_ack_tsn = tsn;
  #endif /* SCTP_DEBUG */
  
  		} else {
  			if (tchunk->tsn_gap_acked) {
  				SCTP_DEBUG_PRINTK("%s: Receiver reneged on "
  						  "data TSN: 0x%x
  ",
0dc47877a   Harvey Harrison   net: replace rema...
1502
  						  __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
1504
  						  tsn);
  				tchunk->tsn_gap_acked = 0;
31b02e154   Vlad Yasevich   sctp: Failover tr...
1505
1506
  				if (tchunk->transport)
  					bytes_acked -= sctp_data_size(tchunk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
  
  				/* RFC 2960 6.3.2 Retransmission Timer Rules
  				 *
  				 * R4) Whenever a SACK is received missing a
  				 * TSN that was previously acknowledged via a
  				 * Gap Ack Block, start T3-rtx for the
  				 * destination address to which the DATA
  				 * chunk was originally
  				 * transmitted if it is not already running.
  				 */
  				restart_timer = 1;
  			}
  
  			list_add_tail(lchunk, &tlist);
  
  #if SCTP_DEBUG
  			/* See the above comments on ACK-ed TSNs. */
  			switch (dbg_prt_state) {
  			case 1:
  				if (dbg_last_kept_tsn + 1 == tsn)
  					break;
  
  				if (dbg_last_kept_tsn != dbg_kept_tsn)
145ce502e   Joe Perches   net/sctp: Use pr_...
1530
1531
  					SCTP_DEBUG_PRINTK_CONT("-%08x",
  							       dbg_last_kept_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1532

145ce502e   Joe Perches   net/sctp: Use pr_...
1533
  				SCTP_DEBUG_PRINTK_CONT(",%08x", tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1534
1535
1536
1537
1538
  				dbg_kept_tsn = tsn;
  				break;
  
  			case 0:
  				if (dbg_last_ack_tsn != dbg_ack_tsn)
145ce502e   Joe Perches   net/sctp: Use pr_...
1539
1540
1541
1542
  					SCTP_DEBUG_PRINTK_CONT("-%08x",
  							       dbg_last_ack_tsn);
  				SCTP_DEBUG_PRINTK_CONT("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1543
1544
1545
1546
1547
1548
  
  				/* FALL THROUGH... */
  			default:
  				SCTP_DEBUG_PRINTK("KEPT: %08x",tsn);
  				dbg_prt_state = 1;
  				dbg_kept_tsn = tsn;
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
1549
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
  
  			dbg_last_kept_tsn = tsn;
  #endif /* SCTP_DEBUG */
  		}
  	}
  
  #if SCTP_DEBUG
  	/* Finish off the last range, displaying its ending TSN.  */
  	switch (dbg_prt_state) {
  	case 0:
  		if (dbg_last_ack_tsn != dbg_ack_tsn) {
145ce502e   Joe Perches   net/sctp: Use pr_...
1561
1562
  			SCTP_DEBUG_PRINTK_CONT("-%08x
  ", dbg_last_ack_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1563
  		} else {
145ce502e   Joe Perches   net/sctp: Use pr_...
1564
1565
  			SCTP_DEBUG_PRINTK_CONT("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1566
1567
1568
1569
1570
  		}
  	break;
  
  	case 1:
  		if (dbg_last_kept_tsn != dbg_kept_tsn) {
145ce502e   Joe Perches   net/sctp: Use pr_...
1571
1572
  			SCTP_DEBUG_PRINTK_CONT("-%08x
  ", dbg_last_kept_tsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1573
  		} else {
145ce502e   Joe Perches   net/sctp: Use pr_...
1574
1575
  			SCTP_DEBUG_PRINTK_CONT("
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1576
  		}
3ff50b799   Stephen Hemminger   [NET]: cleanup ex...
1577
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1578
1579
1580
  #endif /* SCTP_DEBUG */
  	if (transport) {
  		if (bytes_acked) {
f8d960524   Thomas Graf   sctp: Enforce ret...
1581
  			struct sctp_association *asoc = transport->asoc;
31b02e154   Vlad Yasevich   sctp: Failover tr...
1582
1583
1584
1585
1586
1587
1588
  			/* We may have counted DATA that was migrated
  			 * to this transport due to DEL-IP operation.
  			 * Subtract those bytes, since the were never
  			 * send on this transport and shouldn't be
  			 * credited to this transport.
  			 */
  			bytes_acked -= migrate_bytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1589
1590
1591
1592
1593
1594
1595
1596
1597
  			/* 8.2. When an outstanding TSN is acknowledged,
  			 * the endpoint shall clear the error counter of
  			 * the destination transport address to which the
  			 * DATA chunk was last sent.
  			 * The association's overall error counter is
  			 * also cleared.
  			 */
  			transport->error_count = 0;
  			transport->asoc->overall_error_count = 0;
f8d960524   Thomas Graf   sctp: Enforce ret...
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
  			/*
  			 * While in SHUTDOWN PENDING, we may have started
  			 * the T5 shutdown guard timer after reaching the
  			 * retransmission limit. Stop that timer as soon
  			 * as the receiver acknowledged any data.
  			 */
  			if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING &&
  			    del_timer(&asoc->timers
  				[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]))
  					sctp_association_put(asoc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1608
1609
1610
  			/* Mark the destination transport address as
  			 * active if it is not so marked.
  			 */
ad8fec172   Sridhar Samudrala   [SCTP]: Verify al...
1611
1612
  			if ((transport->state == SCTP_INACTIVE) ||
  			    (transport->state == SCTP_UNCONFIRMED)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
  				sctp_assoc_control_transport(
  					transport->asoc,
  					transport,
  					SCTP_TRANSPORT_UP,
  					SCTP_RECEIVED_SACK);
  			}
  
  			sctp_transport_raise_cwnd(transport, sack_ctsn,
  						  bytes_acked);
  
  			transport->flight_size -= bytes_acked;
8b73a07c8   Gui Jianfeng   SCTP: Initialize ...
1624
1625
  			if (transport->flight_size == 0)
  				transport->partial_bytes_acked = 0;
31b02e154   Vlad Yasevich   sctp: Failover tr...
1626
  			q->outstanding_bytes -= bytes_acked + migrate_bytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
  		} else {
  			/* RFC 2960 6.1, sctpimpguide-06 2.15.2
  			 * When a sender is doing zero window probing, it
  			 * should not timeout the association if it continues
  			 * to receive new packets from the receiver. The
  			 * reason is that the receiver MAY keep its window
  			 * closed for an indefinite time.
  			 * A sender is doing zero window probing when the
  			 * receiver's advertised window is zero, and there is
  			 * only one data chunk in flight to the receiver.
f8d960524   Thomas Graf   sctp: Enforce ret...
1637
1638
1639
1640
  			 *
  			 * Allow the association to timeout while in SHUTDOWN
  			 * PENDING or SHUTDOWN RECEIVED in case the receiver
  			 * stays in zero window mode forever.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1641
1642
1643
  			 */
  			if (!q->asoc->peer.rwnd &&
  			    !list_empty(&tlist) &&
f8d960524   Thomas Graf   sctp: Enforce ret...
1644
1645
  			    (sack_ctsn+2 == q->asoc->next_tsn) &&
  			    q->asoc->state < SCTP_STATE_SHUTDOWN_PENDING) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646
1647
1648
  				SCTP_DEBUG_PRINTK("%s: SACK received for zero "
  						  "window probe: %u
  ",
0dc47877a   Harvey Harrison   net: replace rema...
1649
  						  __func__, sack_ctsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
  				q->asoc->overall_error_count = 0;
  				transport->error_count = 0;
  			}
  		}
  
  		/* RFC 2960 6.3.2 Retransmission Timer Rules
  		 *
  		 * R2) Whenever all outstanding data sent to an address have
  		 * been acknowledged, turn off the T3-rtx timer of that
  		 * address.
  		 */
  		if (!transport->flight_size) {
  			if (timer_pending(&transport->T3_rtx_timer) &&
  			    del_timer(&transport->T3_rtx_timer)) {
  				sctp_transport_put(transport);
  			}
  		} else if (restart_timer) {
  			if (!mod_timer(&transport->T3_rtx_timer,
  				       jiffies + transport->rto))
  				sctp_transport_hold(transport);
  		}
  	}
  
  	list_splice(&tlist, transmitted_queue);
  }
  
  /* Mark chunks as missing and consequently may get retransmitted. */
  static void sctp_mark_missing(struct sctp_outq *q,
  			      struct list_head *transmitted_queue,
  			      struct sctp_transport *transport,
  			      __u32 highest_new_tsn_in_sack,
  			      int count_of_newacks)
  {
  	struct sctp_chunk *chunk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1684
1685
  	__u32 tsn;
  	char do_fast_retransmit = 0;
ea862c8d1   Vlad Yasevich   sctp: correctly m...
1686
1687
  	struct sctp_association *asoc = q->asoc;
  	struct sctp_transport *primary = asoc->peer.primary_path;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1688

9dbc15f05   Robert P. J. Day   [SCTP]: "list_for...
1689
  	list_for_each_entry(chunk, transmitted_queue, transmitted_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1690

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1691
1692
1693
1694
1695
1696
1697
1698
1699
  		tsn = ntohl(chunk->subh.data_hdr->tsn);
  
  		/* RFC 2960 7.2.4, sctpimpguide-05 2.8.2 M3) Examine all
  		 * 'Unacknowledged TSN's', if the TSN number of an
  		 * 'Unacknowledged TSN' is smaller than the 'HighestTSNinSack'
  		 * value, increment the 'TSN.Missing.Report' count on that
  		 * chunk if it has NOT been fast retransmitted or marked for
  		 * fast retransmit already.
  		 */
c226ef9b8   Neil Horman   sctp: reduce memo...
1700
  		if (chunk->fast_retransmit == SCTP_CAN_FRTX &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1701
1702
1703
1704
1705
1706
  		    !chunk->tsn_gap_acked &&
  		    TSN_lt(tsn, highest_new_tsn_in_sack)) {
  
  			/* SFR-CACC may require us to skip marking
  			 * this chunk as missing.
  			 */
f246a7b7c   Vlad Yasevich   sctp: teach CACC ...
1707
1708
1709
  			if (!transport || !sctp_cacc_skip(primary,
  						chunk->transport,
  						count_of_newacks, tsn)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1710
1711
1712
1713
1714
  				chunk->tsn_missing_report++;
  
  				SCTP_DEBUG_PRINTK(
  					"%s: TSN 0x%x missing counter: %d
  ",
0dc47877a   Harvey Harrison   net: replace rema...
1715
  					__func__, tsn,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1716
1717
1718
1719
1720
1721
  					chunk->tsn_missing_report);
  			}
  		}
  		/*
  		 * M4) If any DATA chunk is found to have a
  		 * 'TSN.Missing.Report'
27852c26b   Vlad Yasevich   [SCTP]: Fix 'fast...
1722
  		 * value larger than or equal to 3, mark that chunk for
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1723
1724
  		 * retransmission and start the fast retransmit procedure.
  		 */
27852c26b   Vlad Yasevich   [SCTP]: Fix 'fast...
1725
  		if (chunk->tsn_missing_report >= 3) {
c226ef9b8   Neil Horman   sctp: reduce memo...
1726
  			chunk->fast_retransmit = SCTP_NEED_FRTX;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
  			do_fast_retransmit = 1;
  		}
  	}
  
  	if (transport) {
  		if (do_fast_retransmit)
  			sctp_retransmit(q, transport, SCTP_RTXR_FAST_RTX);
  
  		SCTP_DEBUG_PRINTK("%s: transport: %p, cwnd: %d, "
  				  "ssthresh: %d, flight_size: %d, pba: %d
  ",
0dc47877a   Harvey Harrison   net: replace rema...
1738
  				  __func__, transport, transport->cwnd,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1739
  				  transport->ssthresh, transport->flight_size,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
  				  transport->partial_bytes_acked);
  	}
  }
  
  /* Is the given TSN acked by this packet?  */
  static int sctp_acked(struct sctp_sackhdr *sack, __u32 tsn)
  {
  	int i;
  	sctp_sack_variable_t *frags;
  	__u16 gap;
  	__u32 ctsn = ntohl(sack->cum_tsn_ack);
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1751
  	if (TSN_lte(tsn, ctsn))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
  		goto pass;
  
  	/* 3.3.4 Selective Acknowledgement (SACK) (3):
  	 *
  	 * Gap Ack Blocks:
  	 *  These fields contain the Gap Ack Blocks. They are repeated
  	 *  for each Gap Ack Block up to the number of Gap Ack Blocks
  	 *  defined in the Number of Gap Ack Blocks field. All DATA
  	 *  chunks with TSNs greater than or equal to (Cumulative TSN
  	 *  Ack + Gap Ack Block Start) and less than or equal to
  	 *  (Cumulative TSN Ack + Gap Ack Block End) of each Gap Ack
  	 *  Block are assumed to have been received correctly.
  	 */
  
  	frags = sack->variable;
  	gap = tsn - ctsn;
  	for (i = 0; i < ntohs(sack->num_gap_ack_blocks); ++i) {
  		if (TSN_lte(ntohs(frags[i].gab.start), gap) &&
  		    TSN_lte(gap, ntohs(frags[i].gab.end)))
  			goto pass;
  	}
  
  	return 0;
  pass:
  	return 1;
  }
  
  static inline int sctp_get_skip_pos(struct sctp_fwdtsn_skip *skiplist,
9f81bcd94   Al Viro   [SCTP]: More triv...
1780
  				    int nskips, __be16 stream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
  {
  	int i;
  
  	for (i = 0; i < nskips; i++) {
  		if (skiplist[i].stream == stream)
  			return i;
  	}
  	return i;
  }
  
  /* Create and add a fwdtsn chunk to the outq's control queue if needed. */
  static void sctp_generate_fwdtsn(struct sctp_outq *q, __u32 ctsn)
  {
  	struct sctp_association *asoc = q->asoc;
  	struct sctp_chunk *ftsn_chunk = NULL;
  	struct sctp_fwdtsn_skip ftsn_skip_arr[10];
  	int nskips = 0;
  	int skip_pos = 0;
  	__u32 tsn;
  	struct sctp_chunk *chunk;
  	struct list_head *lchunk, *temp;
76595024f   Wei Yongjun   sctp: fix to send...
1802
1803
  	if (!asoc->peer.prsctp_capable)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1804
1805
  	/* PR-SCTP C1) Let SackCumAck be the Cumulative TSN ACK carried in the
  	 * received SACK.
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1806
  	 *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
  	 * If (Advanced.Peer.Ack.Point < SackCumAck), then update
  	 * Advanced.Peer.Ack.Point to be equal to SackCumAck.
  	 */
  	if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
  		asoc->adv_peer_ack_point = ctsn;
  
  	/* PR-SCTP C2) Try to further advance the "Advanced.Peer.Ack.Point"
  	 * locally, that is, to move "Advanced.Peer.Ack.Point" up as long as
  	 * the chunk next in the out-queue space is marked as "abandoned" as
  	 * shown in the following example:
  	 *
  	 * Assuming that a SACK arrived with the Cumulative TSN ACK 102
  	 * and the Advanced.Peer.Ack.Point is updated to this value:
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1820
  	 *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
  	 *   out-queue at the end of  ==>   out-queue after Adv.Ack.Point
  	 *   normal SACK processing           local advancement
  	 *                ...                           ...
  	 *   Adv.Ack.Pt-> 102 acked                     102 acked
  	 *                103 abandoned                 103 abandoned
  	 *                104 abandoned     Adv.Ack.P-> 104 abandoned
  	 *                105                           105
  	 *                106 acked                     106 acked
  	 *                ...                           ...
  	 *
  	 * In this example, the data sender successfully advanced the
  	 * "Advanced.Peer.Ack.Point" from 102 to 104 locally.
  	 */
  	list_for_each_safe(lchunk, temp, &q->abandoned) {
  		chunk = list_entry(lchunk, struct sctp_chunk,
  					transmitted_list);
  		tsn = ntohl(chunk->subh.data_hdr->tsn);
  
  		/* Remove any chunks in the abandoned queue that are acked by
  		 * the ctsn.
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1841
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1842
1843
  		if (TSN_lte(tsn, ctsn)) {
  			list_del_init(lchunk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
  			sctp_chunk_free(chunk);
  		} else {
  			if (TSN_lte(tsn, asoc->adv_peer_ack_point+1)) {
  				asoc->adv_peer_ack_point = tsn;
  				if (chunk->chunk_hdr->flags &
  					 SCTP_DATA_UNORDERED)
  					continue;
  				skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0],
  						nskips,
  						chunk->subh.data_hdr->stream);
  				ftsn_skip_arr[skip_pos].stream =
  					chunk->subh.data_hdr->stream;
  				ftsn_skip_arr[skip_pos].ssn =
  					 chunk->subh.data_hdr->ssn;
  				if (skip_pos == nskips)
  					nskips++;
  				if (nskips == 10)
  					break;
  			} else
  				break;
  		}
  	}
  
  	/* PR-SCTP C3) If, after step C1 and C2, the "Advanced.Peer.Ack.Point"
  	 * is greater than the Cumulative TSN ACK carried in the received
  	 * SACK, the data sender MUST send the data receiver a FORWARD TSN
  	 * chunk containing the latest value of the
  	 * "Advanced.Peer.Ack.Point".
  	 *
  	 * C4) For each "abandoned" TSN the sender of the FORWARD TSN SHOULD
  	 * list each stream and sequence number in the forwarded TSN. This
  	 * information will enable the receiver to easily find any
  	 * stranded TSN's waiting on stream reorder queues. Each stream
  	 * SHOULD only be reported once; this means that if multiple
  	 * abandoned messages occur in the same stream then only the
  	 * highest abandoned stream sequence number is reported. If the
  	 * total size of the FORWARD TSN does NOT fit in a single MTU then
  	 * the sender of the FORWARD TSN SHOULD lower the
  	 * Advanced.Peer.Ack.Point to the last TSN that will fit in a
  	 * single MTU.
  	 */
  	if (asoc->adv_peer_ack_point > ctsn)
  		ftsn_chunk = sctp_make_fwdtsn(asoc, asoc->adv_peer_ack_point,
d808ad9ab   YOSHIFUJI Hideaki   [NET] SCTP: Fix w...
1887
  					      nskips, &ftsn_skip_arr[0]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1888
1889
  
  	if (ftsn_chunk) {
79af02c25   David S. Miller   [SCTP]: Use struc...
1890
  		list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1891
1892
1893
  		SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
  	}
  }