outqueue.c 50 KB

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