outqueue.c 55 KB

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