outqueue.c 55 KB

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