ulpqueue.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /* SCTP kernel reference 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 Intel Corp.
  6. * Copyright (c) 2001 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This abstraction carries sctp events to the ULP (sockets).
  10. *
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Jon Grimm <jgrimm@us.ibm.com>
  37. * La Monte H.P. Yarroll <piggy@acm.org>
  38. * Sridhar Samudrala <sri@us.ibm.com>
  39. *
  40. * Any bugs reported given to us we will try to fix... any fixes shared will
  41. * be incorporated into the next SCTP release.
  42. */
  43. #include <linux/types.h>
  44. #include <linux/skbuff.h>
  45. #include <net/sock.h>
  46. #include <net/sctp/structs.h>
  47. #include <net/sctp/sctp.h>
  48. #include <net/sctp/sm.h>
  49. /* Forward declarations for internal helpers. */
  50. static struct sctp_ulpevent * sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  51. struct sctp_ulpevent *);
  52. static struct sctp_ulpevent * sctp_ulpq_order(struct sctp_ulpq *,
  53. struct sctp_ulpevent *);
  54. /* 1st Level Abstractions */
  55. /* Initialize a ULP queue from a block of memory. */
  56. struct sctp_ulpq *sctp_ulpq_init(struct sctp_ulpq *ulpq,
  57. struct sctp_association *asoc)
  58. {
  59. memset(ulpq, 0, sizeof(struct sctp_ulpq));
  60. ulpq->asoc = asoc;
  61. skb_queue_head_init(&ulpq->reasm);
  62. skb_queue_head_init(&ulpq->lobby);
  63. ulpq->pd_mode = 0;
  64. ulpq->malloced = 0;
  65. return ulpq;
  66. }
  67. /* Flush the reassembly and ordering queues. */
  68. static void sctp_ulpq_flush(struct sctp_ulpq *ulpq)
  69. {
  70. struct sk_buff *skb;
  71. struct sctp_ulpevent *event;
  72. while ((skb = __skb_dequeue(&ulpq->lobby)) != NULL) {
  73. event = sctp_skb2event(skb);
  74. sctp_ulpevent_free(event);
  75. }
  76. while ((skb = __skb_dequeue(&ulpq->reasm)) != NULL) {
  77. event = sctp_skb2event(skb);
  78. sctp_ulpevent_free(event);
  79. }
  80. }
  81. /* Dispose of a ulpqueue. */
  82. void sctp_ulpq_free(struct sctp_ulpq *ulpq)
  83. {
  84. sctp_ulpq_flush(ulpq);
  85. if (ulpq->malloced)
  86. kfree(ulpq);
  87. }
  88. /* Process an incoming DATA chunk. */
  89. int sctp_ulpq_tail_data(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  90. int gfp)
  91. {
  92. struct sk_buff_head temp;
  93. sctp_data_chunk_t *hdr;
  94. struct sctp_ulpevent *event;
  95. hdr = (sctp_data_chunk_t *) chunk->chunk_hdr;
  96. /* Create an event from the incoming chunk. */
  97. event = sctp_ulpevent_make_rcvmsg(chunk->asoc, chunk, gfp);
  98. if (!event)
  99. return -ENOMEM;
  100. /* Do reassembly if needed. */
  101. event = sctp_ulpq_reasm(ulpq, event);
  102. /* Do ordering if needed. */
  103. if ((event) && (event->msg_flags & MSG_EOR)){
  104. /* Create a temporary list to collect chunks on. */
  105. skb_queue_head_init(&temp);
  106. __skb_queue_tail(&temp, sctp_event2skb(event));
  107. event = sctp_ulpq_order(ulpq, event);
  108. }
  109. /* Send event to the ULP. */
  110. if (event)
  111. sctp_ulpq_tail_event(ulpq, event);
  112. return 0;
  113. }
  114. /* Add a new event for propagation to the ULP. */
  115. /* Clear the partial delivery mode for this socket. Note: This
  116. * assumes that no association is currently in partial delivery mode.
  117. */
  118. int sctp_clear_pd(struct sock *sk)
  119. {
  120. struct sctp_sock *sp = sctp_sk(sk);
  121. sp->pd_mode = 0;
  122. if (!skb_queue_empty(&sp->pd_lobby)) {
  123. struct list_head *list;
  124. sctp_skb_list_tail(&sp->pd_lobby, &sk->sk_receive_queue);
  125. list = (struct list_head *)&sctp_sk(sk)->pd_lobby;
  126. INIT_LIST_HEAD(list);
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. /* Clear the pd_mode and restart any pending messages waiting for delivery. */
  132. static int sctp_ulpq_clear_pd(struct sctp_ulpq *ulpq)
  133. {
  134. ulpq->pd_mode = 0;
  135. return sctp_clear_pd(ulpq->asoc->base.sk);
  136. }
  137. int sctp_ulpq_tail_event(struct sctp_ulpq *ulpq, struct sctp_ulpevent *event)
  138. {
  139. struct sock *sk = ulpq->asoc->base.sk;
  140. struct sk_buff_head *queue;
  141. int clear_pd = 0;
  142. /* If the socket is just going to throw this away, do not
  143. * even try to deliver it.
  144. */
  145. if (sock_flag(sk, SOCK_DEAD) || (sk->sk_shutdown & RCV_SHUTDOWN))
  146. goto out_free;
  147. /* Check if the user wishes to receive this event. */
  148. if (!sctp_ulpevent_is_enabled(event, &sctp_sk(sk)->subscribe))
  149. goto out_free;
  150. /* If we are in partial delivery mode, post to the lobby until
  151. * partial delivery is cleared, unless, of course _this_ is
  152. * the association the cause of the partial delivery.
  153. */
  154. if (!sctp_sk(sk)->pd_mode) {
  155. queue = &sk->sk_receive_queue;
  156. } else if (ulpq->pd_mode) {
  157. if (event->msg_flags & MSG_NOTIFICATION)
  158. queue = &sctp_sk(sk)->pd_lobby;
  159. else {
  160. clear_pd = event->msg_flags & MSG_EOR;
  161. queue = &sk->sk_receive_queue;
  162. }
  163. } else
  164. queue = &sctp_sk(sk)->pd_lobby;
  165. /* If we are harvesting multiple skbs they will be
  166. * collected on a list.
  167. */
  168. if (sctp_event2skb(event)->list)
  169. sctp_skb_list_tail(sctp_event2skb(event)->list, queue);
  170. else
  171. __skb_queue_tail(queue, sctp_event2skb(event));
  172. /* Did we just complete partial delivery and need to get
  173. * rolling again? Move pending data to the receive
  174. * queue.
  175. */
  176. if (clear_pd)
  177. sctp_ulpq_clear_pd(ulpq);
  178. if (queue == &sk->sk_receive_queue)
  179. sk->sk_data_ready(sk, 0);
  180. return 1;
  181. out_free:
  182. if (sctp_event2skb(event)->list)
  183. sctp_queue_purge_ulpevents(sctp_event2skb(event)->list);
  184. else
  185. sctp_ulpevent_free(event);
  186. return 0;
  187. }
  188. /* 2nd Level Abstractions */
  189. /* Helper function to store chunks that need to be reassembled. */
  190. static inline void sctp_ulpq_store_reasm(struct sctp_ulpq *ulpq,
  191. struct sctp_ulpevent *event)
  192. {
  193. struct sk_buff *pos;
  194. struct sctp_ulpevent *cevent;
  195. __u32 tsn, ctsn;
  196. tsn = event->tsn;
  197. /* See if it belongs at the end. */
  198. pos = skb_peek_tail(&ulpq->reasm);
  199. if (!pos) {
  200. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  201. return;
  202. }
  203. /* Short circuit just dropping it at the end. */
  204. cevent = sctp_skb2event(pos);
  205. ctsn = cevent->tsn;
  206. if (TSN_lt(ctsn, tsn)) {
  207. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  208. return;
  209. }
  210. /* Find the right place in this list. We store them by TSN. */
  211. skb_queue_walk(&ulpq->reasm, pos) {
  212. cevent = sctp_skb2event(pos);
  213. ctsn = cevent->tsn;
  214. if (TSN_lt(tsn, ctsn))
  215. break;
  216. }
  217. /* Insert before pos. */
  218. __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->reasm);
  219. }
  220. /* Helper function to return an event corresponding to the reassembled
  221. * datagram.
  222. * This routine creates a re-assembled skb given the first and last skb's
  223. * as stored in the reassembly queue. The skb's may be non-linear if the sctp
  224. * payload was fragmented on the way and ip had to reassemble them.
  225. * We add the rest of skb's to the first skb's fraglist.
  226. */
  227. static struct sctp_ulpevent *sctp_make_reassembled_event(struct sk_buff *f_frag, struct sk_buff *l_frag)
  228. {
  229. struct sk_buff *pos;
  230. struct sctp_ulpevent *event;
  231. struct sk_buff *pnext, *last;
  232. struct sk_buff *list = skb_shinfo(f_frag)->frag_list;
  233. /* Store the pointer to the 2nd skb */
  234. if (f_frag == l_frag)
  235. pos = NULL;
  236. else
  237. pos = f_frag->next;
  238. /* Get the last skb in the f_frag's frag_list if present. */
  239. for (last = list; list; last = list, list = list->next);
  240. /* Add the list of remaining fragments to the first fragments
  241. * frag_list.
  242. */
  243. if (last)
  244. last->next = pos;
  245. else
  246. skb_shinfo(f_frag)->frag_list = pos;
  247. /* Remove the first fragment from the reassembly queue. */
  248. __skb_unlink(f_frag, f_frag->list);
  249. while (pos) {
  250. pnext = pos->next;
  251. /* Update the len and data_len fields of the first fragment. */
  252. f_frag->len += pos->len;
  253. f_frag->data_len += pos->len;
  254. /* Remove the fragment from the reassembly queue. */
  255. __skb_unlink(pos, pos->list);
  256. /* Break if we have reached the last fragment. */
  257. if (pos == l_frag)
  258. break;
  259. pos->next = pnext;
  260. pos = pnext;
  261. };
  262. event = sctp_skb2event(f_frag);
  263. SCTP_INC_STATS(SCTP_MIB_REASMUSRMSGS);
  264. return event;
  265. }
  266. /* Helper function to check if an incoming chunk has filled up the last
  267. * missing fragment in a SCTP datagram and return the corresponding event.
  268. */
  269. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_reassembled(struct sctp_ulpq *ulpq)
  270. {
  271. struct sk_buff *pos;
  272. struct sctp_ulpevent *cevent;
  273. struct sk_buff *first_frag = NULL;
  274. __u32 ctsn, next_tsn;
  275. struct sctp_ulpevent *retval = NULL;
  276. /* Initialized to 0 just to avoid compiler warning message. Will
  277. * never be used with this value. It is referenced only after it
  278. * is set when we find the first fragment of a message.
  279. */
  280. next_tsn = 0;
  281. /* The chunks are held in the reasm queue sorted by TSN.
  282. * Walk through the queue sequentially and look for a sequence of
  283. * fragmented chunks that complete a datagram.
  284. * 'first_frag' and next_tsn are reset when we find a chunk which
  285. * is the first fragment of a datagram. Once these 2 fields are set
  286. * we expect to find the remaining middle fragments and the last
  287. * fragment in order. If not, first_frag is reset to NULL and we
  288. * start the next pass when we find another first fragment.
  289. */
  290. skb_queue_walk(&ulpq->reasm, pos) {
  291. cevent = sctp_skb2event(pos);
  292. ctsn = cevent->tsn;
  293. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  294. case SCTP_DATA_FIRST_FRAG:
  295. first_frag = pos;
  296. next_tsn = ctsn + 1;
  297. break;
  298. case SCTP_DATA_MIDDLE_FRAG:
  299. if ((first_frag) && (ctsn == next_tsn))
  300. next_tsn++;
  301. else
  302. first_frag = NULL;
  303. break;
  304. case SCTP_DATA_LAST_FRAG:
  305. if (first_frag && (ctsn == next_tsn))
  306. goto found;
  307. else
  308. first_frag = NULL;
  309. break;
  310. };
  311. }
  312. done:
  313. return retval;
  314. found:
  315. retval = sctp_make_reassembled_event(first_frag, pos);
  316. if (retval)
  317. retval->msg_flags |= MSG_EOR;
  318. goto done;
  319. }
  320. /* Retrieve the next set of fragments of a partial message. */
  321. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_partial(struct sctp_ulpq *ulpq)
  322. {
  323. struct sk_buff *pos, *last_frag, *first_frag;
  324. struct sctp_ulpevent *cevent;
  325. __u32 ctsn, next_tsn;
  326. int is_last;
  327. struct sctp_ulpevent *retval;
  328. /* The chunks are held in the reasm queue sorted by TSN.
  329. * Walk through the queue sequentially and look for the first
  330. * sequence of fragmented chunks.
  331. */
  332. if (skb_queue_empty(&ulpq->reasm))
  333. return NULL;
  334. last_frag = first_frag = NULL;
  335. retval = NULL;
  336. next_tsn = 0;
  337. is_last = 0;
  338. skb_queue_walk(&ulpq->reasm, pos) {
  339. cevent = sctp_skb2event(pos);
  340. ctsn = cevent->tsn;
  341. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  342. case SCTP_DATA_MIDDLE_FRAG:
  343. if (!first_frag) {
  344. first_frag = pos;
  345. next_tsn = ctsn + 1;
  346. last_frag = pos;
  347. } else if (next_tsn == ctsn)
  348. next_tsn++;
  349. else
  350. goto done;
  351. break;
  352. case SCTP_DATA_LAST_FRAG:
  353. if (!first_frag)
  354. first_frag = pos;
  355. else if (ctsn != next_tsn)
  356. goto done;
  357. last_frag = pos;
  358. is_last = 1;
  359. goto done;
  360. default:
  361. return NULL;
  362. };
  363. }
  364. /* We have the reassembled event. There is no need to look
  365. * further.
  366. */
  367. done:
  368. retval = sctp_make_reassembled_event(first_frag, last_frag);
  369. if (retval && is_last)
  370. retval->msg_flags |= MSG_EOR;
  371. return retval;
  372. }
  373. /* Helper function to reassemble chunks. Hold chunks on the reasm queue that
  374. * need reassembling.
  375. */
  376. static struct sctp_ulpevent *sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  377. struct sctp_ulpevent *event)
  378. {
  379. struct sctp_ulpevent *retval = NULL;
  380. /* Check if this is part of a fragmented message. */
  381. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  382. event->msg_flags |= MSG_EOR;
  383. return event;
  384. }
  385. sctp_ulpq_store_reasm(ulpq, event);
  386. if (!ulpq->pd_mode)
  387. retval = sctp_ulpq_retrieve_reassembled(ulpq);
  388. else {
  389. __u32 ctsn, ctsnap;
  390. /* Do not even bother unless this is the next tsn to
  391. * be delivered.
  392. */
  393. ctsn = event->tsn;
  394. ctsnap = sctp_tsnmap_get_ctsn(&ulpq->asoc->peer.tsn_map);
  395. if (TSN_lte(ctsn, ctsnap))
  396. retval = sctp_ulpq_retrieve_partial(ulpq);
  397. }
  398. return retval;
  399. }
  400. /* Retrieve the first part (sequential fragments) for partial delivery. */
  401. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_first(struct sctp_ulpq *ulpq)
  402. {
  403. struct sk_buff *pos, *last_frag, *first_frag;
  404. struct sctp_ulpevent *cevent;
  405. __u32 ctsn, next_tsn;
  406. struct sctp_ulpevent *retval;
  407. /* The chunks are held in the reasm queue sorted by TSN.
  408. * Walk through the queue sequentially and look for a sequence of
  409. * fragmented chunks that start a datagram.
  410. */
  411. if (skb_queue_empty(&ulpq->reasm))
  412. return NULL;
  413. last_frag = first_frag = NULL;
  414. retval = NULL;
  415. next_tsn = 0;
  416. skb_queue_walk(&ulpq->reasm, pos) {
  417. cevent = sctp_skb2event(pos);
  418. ctsn = cevent->tsn;
  419. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  420. case SCTP_DATA_FIRST_FRAG:
  421. if (!first_frag) {
  422. first_frag = pos;
  423. next_tsn = ctsn + 1;
  424. last_frag = pos;
  425. } else
  426. goto done;
  427. break;
  428. case SCTP_DATA_MIDDLE_FRAG:
  429. if (!first_frag)
  430. return NULL;
  431. if (ctsn == next_tsn) {
  432. next_tsn++;
  433. last_frag = pos;
  434. } else
  435. goto done;
  436. break;
  437. default:
  438. return NULL;
  439. };
  440. }
  441. /* We have the reassembled event. There is no need to look
  442. * further.
  443. */
  444. done:
  445. retval = sctp_make_reassembled_event(first_frag, last_frag);
  446. return retval;
  447. }
  448. /* Helper function to gather skbs that have possibly become
  449. * ordered by an an incoming chunk.
  450. */
  451. static inline void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
  452. struct sctp_ulpevent *event)
  453. {
  454. struct sk_buff *pos, *tmp;
  455. struct sctp_ulpevent *cevent;
  456. struct sctp_stream *in;
  457. __u16 sid, csid;
  458. __u16 ssn, cssn;
  459. sid = event->stream;
  460. ssn = event->ssn;
  461. in = &ulpq->asoc->ssnmap->in;
  462. /* We are holding the chunks by stream, by SSN. */
  463. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  464. cevent = (struct sctp_ulpevent *) pos->cb;
  465. csid = cevent->stream;
  466. cssn = cevent->ssn;
  467. /* Have we gone too far? */
  468. if (csid > sid)
  469. break;
  470. /* Have we not gone far enough? */
  471. if (csid < sid)
  472. continue;
  473. if (cssn != sctp_ssn_peek(in, sid))
  474. break;
  475. /* Found it, so mark in the ssnmap. */
  476. sctp_ssn_next(in, sid);
  477. __skb_unlink(pos, pos->list);
  478. /* Attach all gathered skbs to the event. */
  479. __skb_queue_tail(sctp_event2skb(event)->list, pos);
  480. }
  481. }
  482. /* Helper function to store chunks needing ordering. */
  483. static inline void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
  484. struct sctp_ulpevent *event)
  485. {
  486. struct sk_buff *pos;
  487. struct sctp_ulpevent *cevent;
  488. __u16 sid, csid;
  489. __u16 ssn, cssn;
  490. pos = skb_peek_tail(&ulpq->lobby);
  491. if (!pos) {
  492. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  493. return;
  494. }
  495. sid = event->stream;
  496. ssn = event->ssn;
  497. cevent = (struct sctp_ulpevent *) pos->cb;
  498. csid = cevent->stream;
  499. cssn = cevent->ssn;
  500. if (sid > csid) {
  501. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  502. return;
  503. }
  504. if ((sid == csid) && SSN_lt(cssn, ssn)) {
  505. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  506. return;
  507. }
  508. /* Find the right place in this list. We store them by
  509. * stream ID and then by SSN.
  510. */
  511. skb_queue_walk(&ulpq->lobby, pos) {
  512. cevent = (struct sctp_ulpevent *) pos->cb;
  513. csid = cevent->stream;
  514. cssn = cevent->ssn;
  515. if (csid > sid)
  516. break;
  517. if (csid == sid && SSN_lt(ssn, cssn))
  518. break;
  519. }
  520. /* Insert before pos. */
  521. __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->lobby);
  522. }
  523. static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
  524. struct sctp_ulpevent *event)
  525. {
  526. __u16 sid, ssn;
  527. struct sctp_stream *in;
  528. /* Check if this message needs ordering. */
  529. if (SCTP_DATA_UNORDERED & event->msg_flags)
  530. return event;
  531. /* Note: The stream ID must be verified before this routine. */
  532. sid = event->stream;
  533. ssn = event->ssn;
  534. in = &ulpq->asoc->ssnmap->in;
  535. /* Is this the expected SSN for this stream ID? */
  536. if (ssn != sctp_ssn_peek(in, sid)) {
  537. /* We've received something out of order, so find where it
  538. * needs to be placed. We order by stream and then by SSN.
  539. */
  540. sctp_ulpq_store_ordered(ulpq, event);
  541. return NULL;
  542. }
  543. /* Mark that the next chunk has been found. */
  544. sctp_ssn_next(in, sid);
  545. /* Go find any other chunks that were waiting for
  546. * ordering.
  547. */
  548. sctp_ulpq_retrieve_ordered(ulpq, event);
  549. return event;
  550. }
  551. /* Helper function to gather skbs that have possibly become
  552. * ordered by forward tsn skipping their dependencies.
  553. */
  554. static inline void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq)
  555. {
  556. struct sk_buff *pos, *tmp;
  557. struct sctp_ulpevent *cevent;
  558. struct sctp_ulpevent *event = NULL;
  559. struct sctp_stream *in;
  560. struct sk_buff_head temp;
  561. __u16 csid, cssn;
  562. in = &ulpq->asoc->ssnmap->in;
  563. /* We are holding the chunks by stream, by SSN. */
  564. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  565. cevent = (struct sctp_ulpevent *) pos->cb;
  566. csid = cevent->stream;
  567. cssn = cevent->ssn;
  568. if (cssn != sctp_ssn_peek(in, csid))
  569. break;
  570. /* Found it, so mark in the ssnmap. */
  571. sctp_ssn_next(in, csid);
  572. __skb_unlink(pos, pos->list);
  573. if (!event) {
  574. /* Create a temporary list to collect chunks on. */
  575. event = sctp_skb2event(pos);
  576. skb_queue_head_init(&temp);
  577. __skb_queue_tail(&temp, sctp_event2skb(event));
  578. } else {
  579. /* Attach all gathered skbs to the event. */
  580. __skb_queue_tail(sctp_event2skb(event)->list, pos);
  581. }
  582. }
  583. /* Send event to the ULP. */
  584. if (event)
  585. sctp_ulpq_tail_event(ulpq, event);
  586. }
  587. /* Skip over an SSN. */
  588. void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
  589. {
  590. struct sctp_stream *in;
  591. /* Note: The stream ID must be verified before this routine. */
  592. in = &ulpq->asoc->ssnmap->in;
  593. /* Is this an old SSN? If so ignore. */
  594. if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
  595. return;
  596. /* Mark that we are no longer expecting this SSN or lower. */
  597. sctp_ssn_skip(in, sid, ssn);
  598. /* Go find any other chunks that were waiting for
  599. * ordering and deliver them if needed.
  600. */
  601. sctp_ulpq_reap_ordered(ulpq);
  602. return;
  603. }
  604. /* Renege 'needed' bytes from the ordering queue. */
  605. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  606. {
  607. __u16 freed = 0;
  608. __u32 tsn;
  609. struct sk_buff *skb;
  610. struct sctp_ulpevent *event;
  611. struct sctp_tsnmap *tsnmap;
  612. tsnmap = &ulpq->asoc->peer.tsn_map;
  613. while ((skb = __skb_dequeue_tail(&ulpq->lobby)) != NULL) {
  614. freed += skb_headlen(skb);
  615. event = sctp_skb2event(skb);
  616. tsn = event->tsn;
  617. sctp_ulpevent_free(event);
  618. sctp_tsnmap_renege(tsnmap, tsn);
  619. if (freed >= needed)
  620. return freed;
  621. }
  622. return freed;
  623. }
  624. /* Renege 'needed' bytes from the reassembly queue. */
  625. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  626. {
  627. __u16 freed = 0;
  628. __u32 tsn;
  629. struct sk_buff *skb;
  630. struct sctp_ulpevent *event;
  631. struct sctp_tsnmap *tsnmap;
  632. tsnmap = &ulpq->asoc->peer.tsn_map;
  633. /* Walk backwards through the list, reneges the newest tsns. */
  634. while ((skb = __skb_dequeue_tail(&ulpq->reasm)) != NULL) {
  635. freed += skb_headlen(skb);
  636. event = sctp_skb2event(skb);
  637. tsn = event->tsn;
  638. sctp_ulpevent_free(event);
  639. sctp_tsnmap_renege(tsnmap, tsn);
  640. if (freed >= needed)
  641. return freed;
  642. }
  643. return freed;
  644. }
  645. /* Partial deliver the first message as there is pressure on rwnd. */
  646. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  647. struct sctp_chunk *chunk, int gfp)
  648. {
  649. struct sctp_ulpevent *event;
  650. struct sctp_association *asoc;
  651. asoc = ulpq->asoc;
  652. /* Are we already in partial delivery mode? */
  653. if (!sctp_sk(asoc->base.sk)->pd_mode) {
  654. /* Is partial delivery possible? */
  655. event = sctp_ulpq_retrieve_first(ulpq);
  656. /* Send event to the ULP. */
  657. if (event) {
  658. sctp_ulpq_tail_event(ulpq, event);
  659. sctp_sk(asoc->base.sk)->pd_mode = 1;
  660. ulpq->pd_mode = 1;
  661. return;
  662. }
  663. }
  664. }
  665. /* Renege some packets to make room for an incoming chunk. */
  666. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  667. int gfp)
  668. {
  669. struct sctp_association *asoc;
  670. __u16 needed, freed;
  671. asoc = ulpq->asoc;
  672. if (chunk) {
  673. needed = ntohs(chunk->chunk_hdr->length);
  674. needed -= sizeof(sctp_data_chunk_t);
  675. } else
  676. needed = SCTP_DEFAULT_MAXWINDOW;
  677. freed = 0;
  678. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  679. freed = sctp_ulpq_renege_order(ulpq, needed);
  680. if (freed < needed) {
  681. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  682. }
  683. }
  684. /* If able to free enough room, accept this chunk. */
  685. if (chunk && (freed >= needed)) {
  686. __u32 tsn;
  687. tsn = ntohl(chunk->subh.data_hdr->tsn);
  688. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  689. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  690. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  691. }
  692. return;
  693. }
  694. /* Notify the application if an association is aborted and in
  695. * partial delivery mode. Send up any pending received messages.
  696. */
  697. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, int gfp)
  698. {
  699. struct sctp_ulpevent *ev = NULL;
  700. struct sock *sk;
  701. if (!ulpq->pd_mode)
  702. return;
  703. sk = ulpq->asoc->base.sk;
  704. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  705. &sctp_sk(sk)->subscribe))
  706. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  707. SCTP_PARTIAL_DELIVERY_ABORTED,
  708. gfp);
  709. if (ev)
  710. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  711. /* If there is data waiting, send it up the socket now. */
  712. if (sctp_ulpq_clear_pd(ulpq) || ev)
  713. sk->sk_data_ready(sk, 0);
  714. }