outqueue.c 55 KB

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