stream_interleave.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright Red Hat Inc. 2017
  4. *
  5. * This file is part of the SCTP kernel implementation
  6. *
  7. * These functions implement sctp stream message interleaving, mostly
  8. * including I-DATA and I-FORWARD-TSN chunks process.
  9. *
  10. * Please send any bug reports or fixes you make to the
  11. * email addresched(es):
  12. * lksctp developers <[email protected]>
  13. *
  14. * Written or modified by:
  15. * Xin Long <[email protected]>
  16. */
  17. #include <net/busy_poll.h>
  18. #include <net/sctp/sctp.h>
  19. #include <net/sctp/sm.h>
  20. #include <net/sctp/ulpevent.h>
  21. #include <linux/sctp.h>
  22. static struct sctp_chunk *sctp_make_idatafrag_empty(
  23. const struct sctp_association *asoc,
  24. const struct sctp_sndrcvinfo *sinfo,
  25. int len, __u8 flags, gfp_t gfp)
  26. {
  27. struct sctp_chunk *retval;
  28. struct sctp_idatahdr dp;
  29. memset(&dp, 0, sizeof(dp));
  30. dp.stream = htons(sinfo->sinfo_stream);
  31. if (sinfo->sinfo_flags & SCTP_UNORDERED)
  32. flags |= SCTP_DATA_UNORDERED;
  33. retval = sctp_make_idata(asoc, flags, sizeof(dp) + len, gfp);
  34. if (!retval)
  35. return NULL;
  36. retval->subh.idata_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
  37. memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
  38. return retval;
  39. }
  40. static void sctp_chunk_assign_mid(struct sctp_chunk *chunk)
  41. {
  42. struct sctp_stream *stream;
  43. struct sctp_chunk *lchunk;
  44. __u32 cfsn = 0;
  45. __u16 sid;
  46. if (chunk->has_mid)
  47. return;
  48. sid = sctp_chunk_stream_no(chunk);
  49. stream = &chunk->asoc->stream;
  50. list_for_each_entry(lchunk, &chunk->msg->chunks, frag_list) {
  51. struct sctp_idatahdr *hdr;
  52. __u32 mid;
  53. lchunk->has_mid = 1;
  54. hdr = lchunk->subh.idata_hdr;
  55. if (lchunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG)
  56. hdr->ppid = lchunk->sinfo.sinfo_ppid;
  57. else
  58. hdr->fsn = htonl(cfsn++);
  59. if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
  60. mid = lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG ?
  61. sctp_mid_uo_next(stream, out, sid) :
  62. sctp_mid_uo_peek(stream, out, sid);
  63. } else {
  64. mid = lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG ?
  65. sctp_mid_next(stream, out, sid) :
  66. sctp_mid_peek(stream, out, sid);
  67. }
  68. hdr->mid = htonl(mid);
  69. }
  70. }
  71. static bool sctp_validate_data(struct sctp_chunk *chunk)
  72. {
  73. struct sctp_stream *stream;
  74. __u16 sid, ssn;
  75. if (chunk->chunk_hdr->type != SCTP_CID_DATA)
  76. return false;
  77. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
  78. return true;
  79. stream = &chunk->asoc->stream;
  80. sid = sctp_chunk_stream_no(chunk);
  81. ssn = ntohs(chunk->subh.data_hdr->ssn);
  82. return !SSN_lt(ssn, sctp_ssn_peek(stream, in, sid));
  83. }
  84. static bool sctp_validate_idata(struct sctp_chunk *chunk)
  85. {
  86. struct sctp_stream *stream;
  87. __u32 mid;
  88. __u16 sid;
  89. if (chunk->chunk_hdr->type != SCTP_CID_I_DATA)
  90. return false;
  91. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
  92. return true;
  93. stream = &chunk->asoc->stream;
  94. sid = sctp_chunk_stream_no(chunk);
  95. mid = ntohl(chunk->subh.idata_hdr->mid);
  96. return !MID_lt(mid, sctp_mid_peek(stream, in, sid));
  97. }
  98. static void sctp_intl_store_reasm(struct sctp_ulpq *ulpq,
  99. struct sctp_ulpevent *event)
  100. {
  101. struct sctp_ulpevent *cevent;
  102. struct sk_buff *pos, *loc;
  103. pos = skb_peek_tail(&ulpq->reasm);
  104. if (!pos) {
  105. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  106. return;
  107. }
  108. cevent = sctp_skb2event(pos);
  109. if (event->stream == cevent->stream &&
  110. event->mid == cevent->mid &&
  111. (cevent->msg_flags & SCTP_DATA_FIRST_FRAG ||
  112. (!(event->msg_flags & SCTP_DATA_FIRST_FRAG) &&
  113. event->fsn > cevent->fsn))) {
  114. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  115. return;
  116. }
  117. if ((event->stream == cevent->stream &&
  118. MID_lt(cevent->mid, event->mid)) ||
  119. event->stream > cevent->stream) {
  120. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  121. return;
  122. }
  123. loc = NULL;
  124. skb_queue_walk(&ulpq->reasm, pos) {
  125. cevent = sctp_skb2event(pos);
  126. if (event->stream < cevent->stream ||
  127. (event->stream == cevent->stream &&
  128. MID_lt(event->mid, cevent->mid))) {
  129. loc = pos;
  130. break;
  131. }
  132. if (event->stream == cevent->stream &&
  133. event->mid == cevent->mid &&
  134. !(cevent->msg_flags & SCTP_DATA_FIRST_FRAG) &&
  135. (event->msg_flags & SCTP_DATA_FIRST_FRAG ||
  136. event->fsn < cevent->fsn)) {
  137. loc = pos;
  138. break;
  139. }
  140. }
  141. if (!loc)
  142. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  143. else
  144. __skb_queue_before(&ulpq->reasm, loc, sctp_event2skb(event));
  145. }
  146. static struct sctp_ulpevent *sctp_intl_retrieve_partial(
  147. struct sctp_ulpq *ulpq,
  148. struct sctp_ulpevent *event)
  149. {
  150. struct sk_buff *first_frag = NULL;
  151. struct sk_buff *last_frag = NULL;
  152. struct sctp_ulpevent *retval;
  153. struct sctp_stream_in *sin;
  154. struct sk_buff *pos;
  155. __u32 next_fsn = 0;
  156. int is_last = 0;
  157. sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
  158. skb_queue_walk(&ulpq->reasm, pos) {
  159. struct sctp_ulpevent *cevent = sctp_skb2event(pos);
  160. if (cevent->stream < event->stream)
  161. continue;
  162. if (cevent->stream > event->stream ||
  163. cevent->mid != sin->mid)
  164. break;
  165. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  166. case SCTP_DATA_FIRST_FRAG:
  167. goto out;
  168. case SCTP_DATA_MIDDLE_FRAG:
  169. if (!first_frag) {
  170. if (cevent->fsn == sin->fsn) {
  171. first_frag = pos;
  172. last_frag = pos;
  173. next_fsn = cevent->fsn + 1;
  174. }
  175. } else if (cevent->fsn == next_fsn) {
  176. last_frag = pos;
  177. next_fsn++;
  178. } else {
  179. goto out;
  180. }
  181. break;
  182. case SCTP_DATA_LAST_FRAG:
  183. if (!first_frag) {
  184. if (cevent->fsn == sin->fsn) {
  185. first_frag = pos;
  186. last_frag = pos;
  187. next_fsn = 0;
  188. is_last = 1;
  189. }
  190. } else if (cevent->fsn == next_fsn) {
  191. last_frag = pos;
  192. next_fsn = 0;
  193. is_last = 1;
  194. }
  195. goto out;
  196. default:
  197. goto out;
  198. }
  199. }
  200. out:
  201. if (!first_frag)
  202. return NULL;
  203. retval = sctp_make_reassembled_event(ulpq->asoc->base.net, &ulpq->reasm,
  204. first_frag, last_frag);
  205. if (retval) {
  206. sin->fsn = next_fsn;
  207. if (is_last) {
  208. retval->msg_flags |= MSG_EOR;
  209. sin->pd_mode = 0;
  210. }
  211. }
  212. return retval;
  213. }
  214. static struct sctp_ulpevent *sctp_intl_retrieve_reassembled(
  215. struct sctp_ulpq *ulpq,
  216. struct sctp_ulpevent *event)
  217. {
  218. struct sctp_association *asoc = ulpq->asoc;
  219. struct sk_buff *pos, *first_frag = NULL;
  220. struct sctp_ulpevent *retval = NULL;
  221. struct sk_buff *pd_first = NULL;
  222. struct sk_buff *pd_last = NULL;
  223. struct sctp_stream_in *sin;
  224. __u32 next_fsn = 0;
  225. __u32 pd_point = 0;
  226. __u32 pd_len = 0;
  227. __u32 mid = 0;
  228. sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
  229. skb_queue_walk(&ulpq->reasm, pos) {
  230. struct sctp_ulpevent *cevent = sctp_skb2event(pos);
  231. if (cevent->stream < event->stream)
  232. continue;
  233. if (cevent->stream > event->stream)
  234. break;
  235. if (MID_lt(cevent->mid, event->mid))
  236. continue;
  237. if (MID_lt(event->mid, cevent->mid))
  238. break;
  239. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  240. case SCTP_DATA_FIRST_FRAG:
  241. if (cevent->mid == sin->mid) {
  242. pd_first = pos;
  243. pd_last = pos;
  244. pd_len = pos->len;
  245. }
  246. first_frag = pos;
  247. next_fsn = 0;
  248. mid = cevent->mid;
  249. break;
  250. case SCTP_DATA_MIDDLE_FRAG:
  251. if (first_frag && cevent->mid == mid &&
  252. cevent->fsn == next_fsn) {
  253. next_fsn++;
  254. if (pd_first) {
  255. pd_last = pos;
  256. pd_len += pos->len;
  257. }
  258. } else {
  259. first_frag = NULL;
  260. }
  261. break;
  262. case SCTP_DATA_LAST_FRAG:
  263. if (first_frag && cevent->mid == mid &&
  264. cevent->fsn == next_fsn)
  265. goto found;
  266. else
  267. first_frag = NULL;
  268. break;
  269. }
  270. }
  271. if (!pd_first)
  272. goto out;
  273. pd_point = sctp_sk(asoc->base.sk)->pd_point;
  274. if (pd_point && pd_point <= pd_len) {
  275. retval = sctp_make_reassembled_event(asoc->base.net,
  276. &ulpq->reasm,
  277. pd_first, pd_last);
  278. if (retval) {
  279. sin->fsn = next_fsn;
  280. sin->pd_mode = 1;
  281. }
  282. }
  283. goto out;
  284. found:
  285. retval = sctp_make_reassembled_event(asoc->base.net, &ulpq->reasm,
  286. first_frag, pos);
  287. if (retval)
  288. retval->msg_flags |= MSG_EOR;
  289. out:
  290. return retval;
  291. }
  292. static struct sctp_ulpevent *sctp_intl_reasm(struct sctp_ulpq *ulpq,
  293. struct sctp_ulpevent *event)
  294. {
  295. struct sctp_ulpevent *retval = NULL;
  296. struct sctp_stream_in *sin;
  297. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  298. event->msg_flags |= MSG_EOR;
  299. return event;
  300. }
  301. sctp_intl_store_reasm(ulpq, event);
  302. sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
  303. if (sin->pd_mode && event->mid == sin->mid &&
  304. event->fsn == sin->fsn)
  305. retval = sctp_intl_retrieve_partial(ulpq, event);
  306. if (!retval)
  307. retval = sctp_intl_retrieve_reassembled(ulpq, event);
  308. return retval;
  309. }
  310. static void sctp_intl_store_ordered(struct sctp_ulpq *ulpq,
  311. struct sctp_ulpevent *event)
  312. {
  313. struct sctp_ulpevent *cevent;
  314. struct sk_buff *pos, *loc;
  315. pos = skb_peek_tail(&ulpq->lobby);
  316. if (!pos) {
  317. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  318. return;
  319. }
  320. cevent = (struct sctp_ulpevent *)pos->cb;
  321. if (event->stream == cevent->stream &&
  322. MID_lt(cevent->mid, event->mid)) {
  323. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  324. return;
  325. }
  326. if (event->stream > cevent->stream) {
  327. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  328. return;
  329. }
  330. loc = NULL;
  331. skb_queue_walk(&ulpq->lobby, pos) {
  332. cevent = (struct sctp_ulpevent *)pos->cb;
  333. if (cevent->stream > event->stream) {
  334. loc = pos;
  335. break;
  336. }
  337. if (cevent->stream == event->stream &&
  338. MID_lt(event->mid, cevent->mid)) {
  339. loc = pos;
  340. break;
  341. }
  342. }
  343. if (!loc)
  344. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  345. else
  346. __skb_queue_before(&ulpq->lobby, loc, sctp_event2skb(event));
  347. }
  348. static void sctp_intl_retrieve_ordered(struct sctp_ulpq *ulpq,
  349. struct sctp_ulpevent *event)
  350. {
  351. struct sk_buff_head *event_list;
  352. struct sctp_stream *stream;
  353. struct sk_buff *pos, *tmp;
  354. __u16 sid = event->stream;
  355. stream = &ulpq->asoc->stream;
  356. event_list = (struct sk_buff_head *)sctp_event2skb(event)->prev;
  357. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  358. struct sctp_ulpevent *cevent = (struct sctp_ulpevent *)pos->cb;
  359. if (cevent->stream > sid)
  360. break;
  361. if (cevent->stream < sid)
  362. continue;
  363. if (cevent->mid != sctp_mid_peek(stream, in, sid))
  364. break;
  365. sctp_mid_next(stream, in, sid);
  366. __skb_unlink(pos, &ulpq->lobby);
  367. __skb_queue_tail(event_list, pos);
  368. }
  369. }
  370. static struct sctp_ulpevent *sctp_intl_order(struct sctp_ulpq *ulpq,
  371. struct sctp_ulpevent *event)
  372. {
  373. struct sctp_stream *stream;
  374. __u16 sid;
  375. stream = &ulpq->asoc->stream;
  376. sid = event->stream;
  377. if (event->mid != sctp_mid_peek(stream, in, sid)) {
  378. sctp_intl_store_ordered(ulpq, event);
  379. return NULL;
  380. }
  381. sctp_mid_next(stream, in, sid);
  382. sctp_intl_retrieve_ordered(ulpq, event);
  383. return event;
  384. }
  385. static int sctp_enqueue_event(struct sctp_ulpq *ulpq,
  386. struct sk_buff_head *skb_list)
  387. {
  388. struct sock *sk = ulpq->asoc->base.sk;
  389. struct sctp_sock *sp = sctp_sk(sk);
  390. struct sctp_ulpevent *event;
  391. struct sk_buff *skb;
  392. skb = __skb_peek(skb_list);
  393. event = sctp_skb2event(skb);
  394. if (sk->sk_shutdown & RCV_SHUTDOWN &&
  395. (sk->sk_shutdown & SEND_SHUTDOWN ||
  396. !sctp_ulpevent_is_notification(event)))
  397. goto out_free;
  398. if (!sctp_ulpevent_is_notification(event)) {
  399. sk_mark_napi_id(sk, skb);
  400. sk_incoming_cpu_update(sk);
  401. }
  402. if (!sctp_ulpevent_is_enabled(event, ulpq->asoc->subscribe))
  403. goto out_free;
  404. if (skb_list)
  405. skb_queue_splice_tail_init(skb_list,
  406. &sk->sk_receive_queue);
  407. else
  408. __skb_queue_tail(&sk->sk_receive_queue, skb);
  409. if (!sp->data_ready_signalled) {
  410. sp->data_ready_signalled = 1;
  411. sk->sk_data_ready(sk);
  412. }
  413. return 1;
  414. out_free:
  415. if (skb_list)
  416. sctp_queue_purge_ulpevents(skb_list);
  417. else
  418. sctp_ulpevent_free(event);
  419. return 0;
  420. }
  421. static void sctp_intl_store_reasm_uo(struct sctp_ulpq *ulpq,
  422. struct sctp_ulpevent *event)
  423. {
  424. struct sctp_ulpevent *cevent;
  425. struct sk_buff *pos;
  426. pos = skb_peek_tail(&ulpq->reasm_uo);
  427. if (!pos) {
  428. __skb_queue_tail(&ulpq->reasm_uo, sctp_event2skb(event));
  429. return;
  430. }
  431. cevent = sctp_skb2event(pos);
  432. if (event->stream == cevent->stream &&
  433. event->mid == cevent->mid &&
  434. (cevent->msg_flags & SCTP_DATA_FIRST_FRAG ||
  435. (!(event->msg_flags & SCTP_DATA_FIRST_FRAG) &&
  436. event->fsn > cevent->fsn))) {
  437. __skb_queue_tail(&ulpq->reasm_uo, sctp_event2skb(event));
  438. return;
  439. }
  440. if ((event->stream == cevent->stream &&
  441. MID_lt(cevent->mid, event->mid)) ||
  442. event->stream > cevent->stream) {
  443. __skb_queue_tail(&ulpq->reasm_uo, sctp_event2skb(event));
  444. return;
  445. }
  446. skb_queue_walk(&ulpq->reasm_uo, pos) {
  447. cevent = sctp_skb2event(pos);
  448. if (event->stream < cevent->stream ||
  449. (event->stream == cevent->stream &&
  450. MID_lt(event->mid, cevent->mid)))
  451. break;
  452. if (event->stream == cevent->stream &&
  453. event->mid == cevent->mid &&
  454. !(cevent->msg_flags & SCTP_DATA_FIRST_FRAG) &&
  455. (event->msg_flags & SCTP_DATA_FIRST_FRAG ||
  456. event->fsn < cevent->fsn))
  457. break;
  458. }
  459. __skb_queue_before(&ulpq->reasm_uo, pos, sctp_event2skb(event));
  460. }
  461. static struct sctp_ulpevent *sctp_intl_retrieve_partial_uo(
  462. struct sctp_ulpq *ulpq,
  463. struct sctp_ulpevent *event)
  464. {
  465. struct sk_buff *first_frag = NULL;
  466. struct sk_buff *last_frag = NULL;
  467. struct sctp_ulpevent *retval;
  468. struct sctp_stream_in *sin;
  469. struct sk_buff *pos;
  470. __u32 next_fsn = 0;
  471. int is_last = 0;
  472. sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
  473. skb_queue_walk(&ulpq->reasm_uo, pos) {
  474. struct sctp_ulpevent *cevent = sctp_skb2event(pos);
  475. if (cevent->stream < event->stream)
  476. continue;
  477. if (cevent->stream > event->stream)
  478. break;
  479. if (MID_lt(cevent->mid, sin->mid_uo))
  480. continue;
  481. if (MID_lt(sin->mid_uo, cevent->mid))
  482. break;
  483. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  484. case SCTP_DATA_FIRST_FRAG:
  485. goto out;
  486. case SCTP_DATA_MIDDLE_FRAG:
  487. if (!first_frag) {
  488. if (cevent->fsn == sin->fsn_uo) {
  489. first_frag = pos;
  490. last_frag = pos;
  491. next_fsn = cevent->fsn + 1;
  492. }
  493. } else if (cevent->fsn == next_fsn) {
  494. last_frag = pos;
  495. next_fsn++;
  496. } else {
  497. goto out;
  498. }
  499. break;
  500. case SCTP_DATA_LAST_FRAG:
  501. if (!first_frag) {
  502. if (cevent->fsn == sin->fsn_uo) {
  503. first_frag = pos;
  504. last_frag = pos;
  505. next_fsn = 0;
  506. is_last = 1;
  507. }
  508. } else if (cevent->fsn == next_fsn) {
  509. last_frag = pos;
  510. next_fsn = 0;
  511. is_last = 1;
  512. }
  513. goto out;
  514. default:
  515. goto out;
  516. }
  517. }
  518. out:
  519. if (!first_frag)
  520. return NULL;
  521. retval = sctp_make_reassembled_event(ulpq->asoc->base.net,
  522. &ulpq->reasm_uo, first_frag,
  523. last_frag);
  524. if (retval) {
  525. sin->fsn_uo = next_fsn;
  526. if (is_last) {
  527. retval->msg_flags |= MSG_EOR;
  528. sin->pd_mode_uo = 0;
  529. }
  530. }
  531. return retval;
  532. }
  533. static struct sctp_ulpevent *sctp_intl_retrieve_reassembled_uo(
  534. struct sctp_ulpq *ulpq,
  535. struct sctp_ulpevent *event)
  536. {
  537. struct sctp_association *asoc = ulpq->asoc;
  538. struct sk_buff *pos, *first_frag = NULL;
  539. struct sctp_ulpevent *retval = NULL;
  540. struct sk_buff *pd_first = NULL;
  541. struct sk_buff *pd_last = NULL;
  542. struct sctp_stream_in *sin;
  543. __u32 next_fsn = 0;
  544. __u32 pd_point = 0;
  545. __u32 pd_len = 0;
  546. __u32 mid = 0;
  547. sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
  548. skb_queue_walk(&ulpq->reasm_uo, pos) {
  549. struct sctp_ulpevent *cevent = sctp_skb2event(pos);
  550. if (cevent->stream < event->stream)
  551. continue;
  552. if (cevent->stream > event->stream)
  553. break;
  554. if (MID_lt(cevent->mid, event->mid))
  555. continue;
  556. if (MID_lt(event->mid, cevent->mid))
  557. break;
  558. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  559. case SCTP_DATA_FIRST_FRAG:
  560. if (!sin->pd_mode_uo) {
  561. sin->mid_uo = cevent->mid;
  562. pd_first = pos;
  563. pd_last = pos;
  564. pd_len = pos->len;
  565. }
  566. first_frag = pos;
  567. next_fsn = 0;
  568. mid = cevent->mid;
  569. break;
  570. case SCTP_DATA_MIDDLE_FRAG:
  571. if (first_frag && cevent->mid == mid &&
  572. cevent->fsn == next_fsn) {
  573. next_fsn++;
  574. if (pd_first) {
  575. pd_last = pos;
  576. pd_len += pos->len;
  577. }
  578. } else {
  579. first_frag = NULL;
  580. }
  581. break;
  582. case SCTP_DATA_LAST_FRAG:
  583. if (first_frag && cevent->mid == mid &&
  584. cevent->fsn == next_fsn)
  585. goto found;
  586. else
  587. first_frag = NULL;
  588. break;
  589. }
  590. }
  591. if (!pd_first)
  592. goto out;
  593. pd_point = sctp_sk(asoc->base.sk)->pd_point;
  594. if (pd_point && pd_point <= pd_len) {
  595. retval = sctp_make_reassembled_event(asoc->base.net,
  596. &ulpq->reasm_uo,
  597. pd_first, pd_last);
  598. if (retval) {
  599. sin->fsn_uo = next_fsn;
  600. sin->pd_mode_uo = 1;
  601. }
  602. }
  603. goto out;
  604. found:
  605. retval = sctp_make_reassembled_event(asoc->base.net, &ulpq->reasm_uo,
  606. first_frag, pos);
  607. if (retval)
  608. retval->msg_flags |= MSG_EOR;
  609. out:
  610. return retval;
  611. }
  612. static struct sctp_ulpevent *sctp_intl_reasm_uo(struct sctp_ulpq *ulpq,
  613. struct sctp_ulpevent *event)
  614. {
  615. struct sctp_ulpevent *retval = NULL;
  616. struct sctp_stream_in *sin;
  617. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  618. event->msg_flags |= MSG_EOR;
  619. return event;
  620. }
  621. sctp_intl_store_reasm_uo(ulpq, event);
  622. sin = sctp_stream_in(&ulpq->asoc->stream, event->stream);
  623. if (sin->pd_mode_uo && event->mid == sin->mid_uo &&
  624. event->fsn == sin->fsn_uo)
  625. retval = sctp_intl_retrieve_partial_uo(ulpq, event);
  626. if (!retval)
  627. retval = sctp_intl_retrieve_reassembled_uo(ulpq, event);
  628. return retval;
  629. }
  630. static struct sctp_ulpevent *sctp_intl_retrieve_first_uo(struct sctp_ulpq *ulpq)
  631. {
  632. struct sctp_stream_in *csin, *sin = NULL;
  633. struct sk_buff *first_frag = NULL;
  634. struct sk_buff *last_frag = NULL;
  635. struct sctp_ulpevent *retval;
  636. struct sk_buff *pos;
  637. __u32 next_fsn = 0;
  638. __u16 sid = 0;
  639. skb_queue_walk(&ulpq->reasm_uo, pos) {
  640. struct sctp_ulpevent *cevent = sctp_skb2event(pos);
  641. csin = sctp_stream_in(&ulpq->asoc->stream, cevent->stream);
  642. if (csin->pd_mode_uo)
  643. continue;
  644. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  645. case SCTP_DATA_FIRST_FRAG:
  646. if (first_frag)
  647. goto out;
  648. first_frag = pos;
  649. last_frag = pos;
  650. next_fsn = 0;
  651. sin = csin;
  652. sid = cevent->stream;
  653. sin->mid_uo = cevent->mid;
  654. break;
  655. case SCTP_DATA_MIDDLE_FRAG:
  656. if (!first_frag)
  657. break;
  658. if (cevent->stream == sid &&
  659. cevent->mid == sin->mid_uo &&
  660. cevent->fsn == next_fsn) {
  661. next_fsn++;
  662. last_frag = pos;
  663. } else {
  664. goto out;
  665. }
  666. break;
  667. case SCTP_DATA_LAST_FRAG:
  668. if (first_frag)
  669. goto out;
  670. break;
  671. default:
  672. break;
  673. }
  674. }
  675. if (!first_frag)
  676. return NULL;
  677. out:
  678. retval = sctp_make_reassembled_event(ulpq->asoc->base.net,
  679. &ulpq->reasm_uo, first_frag,
  680. last_frag);
  681. if (retval) {
  682. sin->fsn_uo = next_fsn;
  683. sin->pd_mode_uo = 1;
  684. }
  685. return retval;
  686. }
  687. static int sctp_ulpevent_idata(struct sctp_ulpq *ulpq,
  688. struct sctp_chunk *chunk, gfp_t gfp)
  689. {
  690. struct sctp_ulpevent *event;
  691. struct sk_buff_head temp;
  692. int event_eor = 0;
  693. event = sctp_ulpevent_make_rcvmsg(chunk->asoc, chunk, gfp);
  694. if (!event)
  695. return -ENOMEM;
  696. event->mid = ntohl(chunk->subh.idata_hdr->mid);
  697. if (event->msg_flags & SCTP_DATA_FIRST_FRAG)
  698. event->ppid = chunk->subh.idata_hdr->ppid;
  699. else
  700. event->fsn = ntohl(chunk->subh.idata_hdr->fsn);
  701. if (!(event->msg_flags & SCTP_DATA_UNORDERED)) {
  702. event = sctp_intl_reasm(ulpq, event);
  703. if (event) {
  704. skb_queue_head_init(&temp);
  705. __skb_queue_tail(&temp, sctp_event2skb(event));
  706. if (event->msg_flags & MSG_EOR)
  707. event = sctp_intl_order(ulpq, event);
  708. }
  709. } else {
  710. event = sctp_intl_reasm_uo(ulpq, event);
  711. if (event) {
  712. skb_queue_head_init(&temp);
  713. __skb_queue_tail(&temp, sctp_event2skb(event));
  714. }
  715. }
  716. if (event) {
  717. event_eor = (event->msg_flags & MSG_EOR) ? 1 : 0;
  718. sctp_enqueue_event(ulpq, &temp);
  719. }
  720. return event_eor;
  721. }
  722. static struct sctp_ulpevent *sctp_intl_retrieve_first(struct sctp_ulpq *ulpq)
  723. {
  724. struct sctp_stream_in *csin, *sin = NULL;
  725. struct sk_buff *first_frag = NULL;
  726. struct sk_buff *last_frag = NULL;
  727. struct sctp_ulpevent *retval;
  728. struct sk_buff *pos;
  729. __u32 next_fsn = 0;
  730. __u16 sid = 0;
  731. skb_queue_walk(&ulpq->reasm, pos) {
  732. struct sctp_ulpevent *cevent = sctp_skb2event(pos);
  733. csin = sctp_stream_in(&ulpq->asoc->stream, cevent->stream);
  734. if (csin->pd_mode)
  735. continue;
  736. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  737. case SCTP_DATA_FIRST_FRAG:
  738. if (first_frag)
  739. goto out;
  740. if (cevent->mid == csin->mid) {
  741. first_frag = pos;
  742. last_frag = pos;
  743. next_fsn = 0;
  744. sin = csin;
  745. sid = cevent->stream;
  746. }
  747. break;
  748. case SCTP_DATA_MIDDLE_FRAG:
  749. if (!first_frag)
  750. break;
  751. if (cevent->stream == sid &&
  752. cevent->mid == sin->mid &&
  753. cevent->fsn == next_fsn) {
  754. next_fsn++;
  755. last_frag = pos;
  756. } else {
  757. goto out;
  758. }
  759. break;
  760. case SCTP_DATA_LAST_FRAG:
  761. if (first_frag)
  762. goto out;
  763. break;
  764. default:
  765. break;
  766. }
  767. }
  768. if (!first_frag)
  769. return NULL;
  770. out:
  771. retval = sctp_make_reassembled_event(ulpq->asoc->base.net,
  772. &ulpq->reasm, first_frag,
  773. last_frag);
  774. if (retval) {
  775. sin->fsn = next_fsn;
  776. sin->pd_mode = 1;
  777. }
  778. return retval;
  779. }
  780. static void sctp_intl_start_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  781. {
  782. struct sctp_ulpevent *event;
  783. struct sk_buff_head temp;
  784. if (!skb_queue_empty(&ulpq->reasm)) {
  785. do {
  786. event = sctp_intl_retrieve_first(ulpq);
  787. if (event) {
  788. skb_queue_head_init(&temp);
  789. __skb_queue_tail(&temp, sctp_event2skb(event));
  790. sctp_enqueue_event(ulpq, &temp);
  791. }
  792. } while (event);
  793. }
  794. if (!skb_queue_empty(&ulpq->reasm_uo)) {
  795. do {
  796. event = sctp_intl_retrieve_first_uo(ulpq);
  797. if (event) {
  798. skb_queue_head_init(&temp);
  799. __skb_queue_tail(&temp, sctp_event2skb(event));
  800. sctp_enqueue_event(ulpq, &temp);
  801. }
  802. } while (event);
  803. }
  804. }
  805. static void sctp_renege_events(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  806. gfp_t gfp)
  807. {
  808. struct sctp_association *asoc = ulpq->asoc;
  809. __u32 freed = 0;
  810. __u16 needed;
  811. needed = ntohs(chunk->chunk_hdr->length) -
  812. sizeof(struct sctp_idata_chunk);
  813. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  814. freed = sctp_ulpq_renege_list(ulpq, &ulpq->lobby, needed);
  815. if (freed < needed)
  816. freed += sctp_ulpq_renege_list(ulpq, &ulpq->reasm,
  817. needed);
  818. if (freed < needed)
  819. freed += sctp_ulpq_renege_list(ulpq, &ulpq->reasm_uo,
  820. needed);
  821. }
  822. if (freed >= needed && sctp_ulpevent_idata(ulpq, chunk, gfp) <= 0)
  823. sctp_intl_start_pd(ulpq, gfp);
  824. }
  825. static void sctp_intl_stream_abort_pd(struct sctp_ulpq *ulpq, __u16 sid,
  826. __u32 mid, __u16 flags, gfp_t gfp)
  827. {
  828. struct sock *sk = ulpq->asoc->base.sk;
  829. struct sctp_ulpevent *ev = NULL;
  830. if (!sctp_ulpevent_type_enabled(ulpq->asoc->subscribe,
  831. SCTP_PARTIAL_DELIVERY_EVENT))
  832. return;
  833. ev = sctp_ulpevent_make_pdapi(ulpq->asoc, SCTP_PARTIAL_DELIVERY_ABORTED,
  834. sid, mid, flags, gfp);
  835. if (ev) {
  836. struct sctp_sock *sp = sctp_sk(sk);
  837. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  838. if (!sp->data_ready_signalled) {
  839. sp->data_ready_signalled = 1;
  840. sk->sk_data_ready(sk);
  841. }
  842. }
  843. }
  844. static void sctp_intl_reap_ordered(struct sctp_ulpq *ulpq, __u16 sid)
  845. {
  846. struct sctp_stream *stream = &ulpq->asoc->stream;
  847. struct sctp_ulpevent *cevent, *event = NULL;
  848. struct sk_buff_head *lobby = &ulpq->lobby;
  849. struct sk_buff *pos, *tmp;
  850. struct sk_buff_head temp;
  851. __u16 csid;
  852. __u32 cmid;
  853. skb_queue_head_init(&temp);
  854. sctp_skb_for_each(pos, lobby, tmp) {
  855. cevent = (struct sctp_ulpevent *)pos->cb;
  856. csid = cevent->stream;
  857. cmid = cevent->mid;
  858. if (csid > sid)
  859. break;
  860. if (csid < sid)
  861. continue;
  862. if (!MID_lt(cmid, sctp_mid_peek(stream, in, csid)))
  863. break;
  864. __skb_unlink(pos, lobby);
  865. if (!event)
  866. event = sctp_skb2event(pos);
  867. __skb_queue_tail(&temp, pos);
  868. }
  869. if (!event && pos != (struct sk_buff *)lobby) {
  870. cevent = (struct sctp_ulpevent *)pos->cb;
  871. csid = cevent->stream;
  872. cmid = cevent->mid;
  873. if (csid == sid && cmid == sctp_mid_peek(stream, in, csid)) {
  874. sctp_mid_next(stream, in, csid);
  875. __skb_unlink(pos, lobby);
  876. __skb_queue_tail(&temp, pos);
  877. event = sctp_skb2event(pos);
  878. }
  879. }
  880. if (event) {
  881. sctp_intl_retrieve_ordered(ulpq, event);
  882. sctp_enqueue_event(ulpq, &temp);
  883. }
  884. }
  885. static void sctp_intl_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  886. {
  887. struct sctp_stream *stream = &ulpq->asoc->stream;
  888. __u16 sid;
  889. for (sid = 0; sid < stream->incnt; sid++) {
  890. struct sctp_stream_in *sin = SCTP_SI(stream, sid);
  891. __u32 mid;
  892. if (sin->pd_mode_uo) {
  893. sin->pd_mode_uo = 0;
  894. mid = sin->mid_uo;
  895. sctp_intl_stream_abort_pd(ulpq, sid, mid, 0x1, gfp);
  896. }
  897. if (sin->pd_mode) {
  898. sin->pd_mode = 0;
  899. mid = sin->mid;
  900. sctp_intl_stream_abort_pd(ulpq, sid, mid, 0, gfp);
  901. sctp_mid_skip(stream, in, sid, mid);
  902. sctp_intl_reap_ordered(ulpq, sid);
  903. }
  904. }
  905. /* intl abort pd happens only when all data needs to be cleaned */
  906. sctp_ulpq_flush(ulpq);
  907. }
  908. static inline int sctp_get_skip_pos(struct sctp_ifwdtsn_skip *skiplist,
  909. int nskips, __be16 stream, __u8 flags)
  910. {
  911. int i;
  912. for (i = 0; i < nskips; i++)
  913. if (skiplist[i].stream == stream &&
  914. skiplist[i].flags == flags)
  915. return i;
  916. return i;
  917. }
  918. #define SCTP_FTSN_U_BIT 0x1
  919. static void sctp_generate_iftsn(struct sctp_outq *q, __u32 ctsn)
  920. {
  921. struct sctp_ifwdtsn_skip ftsn_skip_arr[10];
  922. struct sctp_association *asoc = q->asoc;
  923. struct sctp_chunk *ftsn_chunk = NULL;
  924. struct list_head *lchunk, *temp;
  925. int nskips = 0, skip_pos;
  926. struct sctp_chunk *chunk;
  927. __u32 tsn;
  928. if (!asoc->peer.prsctp_capable)
  929. return;
  930. if (TSN_lt(asoc->adv_peer_ack_point, ctsn))
  931. asoc->adv_peer_ack_point = ctsn;
  932. list_for_each_safe(lchunk, temp, &q->abandoned) {
  933. chunk = list_entry(lchunk, struct sctp_chunk, transmitted_list);
  934. tsn = ntohl(chunk->subh.data_hdr->tsn);
  935. if (TSN_lte(tsn, ctsn)) {
  936. list_del_init(lchunk);
  937. sctp_chunk_free(chunk);
  938. } else if (TSN_lte(tsn, asoc->adv_peer_ack_point + 1)) {
  939. __be16 sid = chunk->subh.idata_hdr->stream;
  940. __be32 mid = chunk->subh.idata_hdr->mid;
  941. __u8 flags = 0;
  942. if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
  943. flags |= SCTP_FTSN_U_BIT;
  944. asoc->adv_peer_ack_point = tsn;
  945. skip_pos = sctp_get_skip_pos(&ftsn_skip_arr[0], nskips,
  946. sid, flags);
  947. ftsn_skip_arr[skip_pos].stream = sid;
  948. ftsn_skip_arr[skip_pos].reserved = 0;
  949. ftsn_skip_arr[skip_pos].flags = flags;
  950. ftsn_skip_arr[skip_pos].mid = mid;
  951. if (skip_pos == nskips)
  952. nskips++;
  953. if (nskips == 10)
  954. break;
  955. } else {
  956. break;
  957. }
  958. }
  959. if (asoc->adv_peer_ack_point > ctsn)
  960. ftsn_chunk = sctp_make_ifwdtsn(asoc, asoc->adv_peer_ack_point,
  961. nskips, &ftsn_skip_arr[0]);
  962. if (ftsn_chunk) {
  963. list_add_tail(&ftsn_chunk->list, &q->control_chunk_list);
  964. SCTP_INC_STATS(asoc->base.net, SCTP_MIB_OUTCTRLCHUNKS);
  965. }
  966. }
  967. #define _sctp_walk_ifwdtsn(pos, chunk, end) \
  968. for (pos = chunk->subh.ifwdtsn_hdr->skip; \
  969. (void *)pos <= (void *)chunk->subh.ifwdtsn_hdr->skip + (end) - \
  970. sizeof(struct sctp_ifwdtsn_skip); pos++)
  971. #define sctp_walk_ifwdtsn(pos, ch) \
  972. _sctp_walk_ifwdtsn((pos), (ch), ntohs((ch)->chunk_hdr->length) - \
  973. sizeof(struct sctp_ifwdtsn_chunk))
  974. static bool sctp_validate_fwdtsn(struct sctp_chunk *chunk)
  975. {
  976. struct sctp_fwdtsn_skip *skip;
  977. __u16 incnt;
  978. if (chunk->chunk_hdr->type != SCTP_CID_FWD_TSN)
  979. return false;
  980. incnt = chunk->asoc->stream.incnt;
  981. sctp_walk_fwdtsn(skip, chunk)
  982. if (ntohs(skip->stream) >= incnt)
  983. return false;
  984. return true;
  985. }
  986. static bool sctp_validate_iftsn(struct sctp_chunk *chunk)
  987. {
  988. struct sctp_ifwdtsn_skip *skip;
  989. __u16 incnt;
  990. if (chunk->chunk_hdr->type != SCTP_CID_I_FWD_TSN)
  991. return false;
  992. incnt = chunk->asoc->stream.incnt;
  993. sctp_walk_ifwdtsn(skip, chunk)
  994. if (ntohs(skip->stream) >= incnt)
  995. return false;
  996. return true;
  997. }
  998. static void sctp_report_fwdtsn(struct sctp_ulpq *ulpq, __u32 ftsn)
  999. {
  1000. /* Move the Cumulattive TSN Ack ahead. */
  1001. sctp_tsnmap_skip(&ulpq->asoc->peer.tsn_map, ftsn);
  1002. /* purge the fragmentation queue */
  1003. sctp_ulpq_reasm_flushtsn(ulpq, ftsn);
  1004. /* Abort any in progress partial delivery. */
  1005. sctp_ulpq_abort_pd(ulpq, GFP_ATOMIC);
  1006. }
  1007. static void sctp_intl_reasm_flushtsn(struct sctp_ulpq *ulpq, __u32 ftsn)
  1008. {
  1009. struct sk_buff *pos, *tmp;
  1010. skb_queue_walk_safe(&ulpq->reasm, pos, tmp) {
  1011. struct sctp_ulpevent *event = sctp_skb2event(pos);
  1012. __u32 tsn = event->tsn;
  1013. if (TSN_lte(tsn, ftsn)) {
  1014. __skb_unlink(pos, &ulpq->reasm);
  1015. sctp_ulpevent_free(event);
  1016. }
  1017. }
  1018. skb_queue_walk_safe(&ulpq->reasm_uo, pos, tmp) {
  1019. struct sctp_ulpevent *event = sctp_skb2event(pos);
  1020. __u32 tsn = event->tsn;
  1021. if (TSN_lte(tsn, ftsn)) {
  1022. __skb_unlink(pos, &ulpq->reasm_uo);
  1023. sctp_ulpevent_free(event);
  1024. }
  1025. }
  1026. }
  1027. static void sctp_report_iftsn(struct sctp_ulpq *ulpq, __u32 ftsn)
  1028. {
  1029. /* Move the Cumulattive TSN Ack ahead. */
  1030. sctp_tsnmap_skip(&ulpq->asoc->peer.tsn_map, ftsn);
  1031. /* purge the fragmentation queue */
  1032. sctp_intl_reasm_flushtsn(ulpq, ftsn);
  1033. /* abort only when it's for all data */
  1034. if (ftsn == sctp_tsnmap_get_max_tsn_seen(&ulpq->asoc->peer.tsn_map))
  1035. sctp_intl_abort_pd(ulpq, GFP_ATOMIC);
  1036. }
  1037. static void sctp_handle_fwdtsn(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk)
  1038. {
  1039. struct sctp_fwdtsn_skip *skip;
  1040. /* Walk through all the skipped SSNs */
  1041. sctp_walk_fwdtsn(skip, chunk)
  1042. sctp_ulpq_skip(ulpq, ntohs(skip->stream), ntohs(skip->ssn));
  1043. }
  1044. static void sctp_intl_skip(struct sctp_ulpq *ulpq, __u16 sid, __u32 mid,
  1045. __u8 flags)
  1046. {
  1047. struct sctp_stream_in *sin = sctp_stream_in(&ulpq->asoc->stream, sid);
  1048. struct sctp_stream *stream = &ulpq->asoc->stream;
  1049. if (flags & SCTP_FTSN_U_BIT) {
  1050. if (sin->pd_mode_uo && MID_lt(sin->mid_uo, mid)) {
  1051. sin->pd_mode_uo = 0;
  1052. sctp_intl_stream_abort_pd(ulpq, sid, mid, 0x1,
  1053. GFP_ATOMIC);
  1054. }
  1055. return;
  1056. }
  1057. if (MID_lt(mid, sctp_mid_peek(stream, in, sid)))
  1058. return;
  1059. if (sin->pd_mode) {
  1060. sin->pd_mode = 0;
  1061. sctp_intl_stream_abort_pd(ulpq, sid, mid, 0x0, GFP_ATOMIC);
  1062. }
  1063. sctp_mid_skip(stream, in, sid, mid);
  1064. sctp_intl_reap_ordered(ulpq, sid);
  1065. }
  1066. static void sctp_handle_iftsn(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk)
  1067. {
  1068. struct sctp_ifwdtsn_skip *skip;
  1069. /* Walk through all the skipped MIDs and abort stream pd if possible */
  1070. sctp_walk_ifwdtsn(skip, chunk)
  1071. sctp_intl_skip(ulpq, ntohs(skip->stream),
  1072. ntohl(skip->mid), skip->flags);
  1073. }
  1074. static int do_ulpq_tail_event(struct sctp_ulpq *ulpq, struct sctp_ulpevent *event)
  1075. {
  1076. struct sk_buff_head temp;
  1077. skb_queue_head_init(&temp);
  1078. __skb_queue_tail(&temp, sctp_event2skb(event));
  1079. return sctp_ulpq_tail_event(ulpq, &temp);
  1080. }
  1081. static struct sctp_stream_interleave sctp_stream_interleave_0 = {
  1082. .data_chunk_len = sizeof(struct sctp_data_chunk),
  1083. .ftsn_chunk_len = sizeof(struct sctp_fwdtsn_chunk),
  1084. /* DATA process functions */
  1085. .make_datafrag = sctp_make_datafrag_empty,
  1086. .assign_number = sctp_chunk_assign_ssn,
  1087. .validate_data = sctp_validate_data,
  1088. .ulpevent_data = sctp_ulpq_tail_data,
  1089. .enqueue_event = do_ulpq_tail_event,
  1090. .renege_events = sctp_ulpq_renege,
  1091. .start_pd = sctp_ulpq_partial_delivery,
  1092. .abort_pd = sctp_ulpq_abort_pd,
  1093. /* FORWARD-TSN process functions */
  1094. .generate_ftsn = sctp_generate_fwdtsn,
  1095. .validate_ftsn = sctp_validate_fwdtsn,
  1096. .report_ftsn = sctp_report_fwdtsn,
  1097. .handle_ftsn = sctp_handle_fwdtsn,
  1098. };
  1099. static int do_sctp_enqueue_event(struct sctp_ulpq *ulpq,
  1100. struct sctp_ulpevent *event)
  1101. {
  1102. struct sk_buff_head temp;
  1103. skb_queue_head_init(&temp);
  1104. __skb_queue_tail(&temp, sctp_event2skb(event));
  1105. return sctp_enqueue_event(ulpq, &temp);
  1106. }
  1107. static struct sctp_stream_interleave sctp_stream_interleave_1 = {
  1108. .data_chunk_len = sizeof(struct sctp_idata_chunk),
  1109. .ftsn_chunk_len = sizeof(struct sctp_ifwdtsn_chunk),
  1110. /* I-DATA process functions */
  1111. .make_datafrag = sctp_make_idatafrag_empty,
  1112. .assign_number = sctp_chunk_assign_mid,
  1113. .validate_data = sctp_validate_idata,
  1114. .ulpevent_data = sctp_ulpevent_idata,
  1115. .enqueue_event = do_sctp_enqueue_event,
  1116. .renege_events = sctp_renege_events,
  1117. .start_pd = sctp_intl_start_pd,
  1118. .abort_pd = sctp_intl_abort_pd,
  1119. /* I-FORWARD-TSN process functions */
  1120. .generate_ftsn = sctp_generate_iftsn,
  1121. .validate_ftsn = sctp_validate_iftsn,
  1122. .report_ftsn = sctp_report_iftsn,
  1123. .handle_ftsn = sctp_handle_iftsn,
  1124. };
  1125. void sctp_stream_interleave_init(struct sctp_stream *stream)
  1126. {
  1127. struct sctp_association *asoc;
  1128. asoc = container_of(stream, struct sctp_association, stream);
  1129. stream->si = asoc->peer.intl_capable ? &sctp_stream_interleave_1
  1130. : &sctp_stream_interleave_0;
  1131. }