outqueue.c 55 KB

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