outqueue.c 56 KB

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