outqueue.c 55 KB

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