outqueue.c 55 KB

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