outqueue.c 55 KB

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