outqueue.c 55 KB

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