outqueue.c 55 KB

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