outqueue.c 55 KB

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