outqueue.c 55 KB

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