outqueue.c 55 KB

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