outqueue.c 50 KB

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