outqueue.c 55 KB

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