outqueue.c 54 KB

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