outqueue.c 55 KB

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