outqueue.c 56 KB

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