ulpqueue.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. gfp_t 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. 'event' is the sctp_ulpevent for
  110. * very first SKB on the 'temp' list.
  111. */
  112. if (event)
  113. sctp_ulpq_tail_event(ulpq, event);
  114. return 0;
  115. }
  116. /* Add a new event for propagation to the ULP. */
  117. /* Clear the partial delivery mode for this socket. Note: This
  118. * assumes that no association is currently in partial delivery mode.
  119. */
  120. int sctp_clear_pd(struct sock *sk)
  121. {
  122. struct sctp_sock *sp = sctp_sk(sk);
  123. sp->pd_mode = 0;
  124. if (!skb_queue_empty(&sp->pd_lobby)) {
  125. struct list_head *list;
  126. sctp_skb_list_tail(&sp->pd_lobby, &sk->sk_receive_queue);
  127. list = (struct list_head *)&sctp_sk(sk)->pd_lobby;
  128. INIT_LIST_HEAD(list);
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. /* Clear the pd_mode and restart any pending messages waiting for delivery. */
  134. static int sctp_ulpq_clear_pd(struct sctp_ulpq *ulpq)
  135. {
  136. ulpq->pd_mode = 0;
  137. return sctp_clear_pd(ulpq->asoc->base.sk);
  138. }
  139. /* If the SKB of 'event' is on a list, it is the first such member
  140. * of that list.
  141. */
  142. int sctp_ulpq_tail_event(struct sctp_ulpq *ulpq, struct sctp_ulpevent *event)
  143. {
  144. struct sock *sk = ulpq->asoc->base.sk;
  145. struct sk_buff_head *queue, *skb_list;
  146. struct sk_buff *skb = sctp_event2skb(event);
  147. int clear_pd = 0;
  148. skb_list = (struct sk_buff_head *) skb->prev;
  149. /* If the socket is just going to throw this away, do not
  150. * even try to deliver it.
  151. */
  152. if (sock_flag(sk, SOCK_DEAD) || (sk->sk_shutdown & RCV_SHUTDOWN))
  153. goto out_free;
  154. /* Check if the user wishes to receive this event. */
  155. if (!sctp_ulpevent_is_enabled(event, &sctp_sk(sk)->subscribe))
  156. goto out_free;
  157. /* If we are in partial delivery mode, post to the lobby until
  158. * partial delivery is cleared, unless, of course _this_ is
  159. * the association the cause of the partial delivery.
  160. */
  161. if (!sctp_sk(sk)->pd_mode) {
  162. queue = &sk->sk_receive_queue;
  163. } else if (ulpq->pd_mode) {
  164. if (event->msg_flags & MSG_NOTIFICATION)
  165. queue = &sctp_sk(sk)->pd_lobby;
  166. else {
  167. clear_pd = event->msg_flags & MSG_EOR;
  168. queue = &sk->sk_receive_queue;
  169. }
  170. } else
  171. queue = &sctp_sk(sk)->pd_lobby;
  172. /* If we are harvesting multiple skbs they will be
  173. * collected on a list.
  174. */
  175. if (skb_list)
  176. sctp_skb_list_tail(skb_list, queue);
  177. else
  178. __skb_queue_tail(queue, skb);
  179. /* Did we just complete partial delivery and need to get
  180. * rolling again? Move pending data to the receive
  181. * queue.
  182. */
  183. if (clear_pd)
  184. sctp_ulpq_clear_pd(ulpq);
  185. if (queue == &sk->sk_receive_queue)
  186. sk->sk_data_ready(sk, 0);
  187. return 1;
  188. out_free:
  189. if (skb_list)
  190. sctp_queue_purge_ulpevents(skb_list);
  191. else
  192. sctp_ulpevent_free(event);
  193. return 0;
  194. }
  195. /* 2nd Level Abstractions */
  196. /* Helper function to store chunks that need to be reassembled. */
  197. static inline void sctp_ulpq_store_reasm(struct sctp_ulpq *ulpq,
  198. struct sctp_ulpevent *event)
  199. {
  200. struct sk_buff *pos;
  201. struct sctp_ulpevent *cevent;
  202. __u32 tsn, ctsn;
  203. tsn = event->tsn;
  204. /* See if it belongs at the end. */
  205. pos = skb_peek_tail(&ulpq->reasm);
  206. if (!pos) {
  207. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  208. return;
  209. }
  210. /* Short circuit just dropping it at the end. */
  211. cevent = sctp_skb2event(pos);
  212. ctsn = cevent->tsn;
  213. if (TSN_lt(ctsn, tsn)) {
  214. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  215. return;
  216. }
  217. /* Find the right place in this list. We store them by TSN. */
  218. skb_queue_walk(&ulpq->reasm, pos) {
  219. cevent = sctp_skb2event(pos);
  220. ctsn = cevent->tsn;
  221. if (TSN_lt(tsn, ctsn))
  222. break;
  223. }
  224. /* Insert before pos. */
  225. __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->reasm);
  226. }
  227. /* Helper function to return an event corresponding to the reassembled
  228. * datagram.
  229. * This routine creates a re-assembled skb given the first and last skb's
  230. * as stored in the reassembly queue. The skb's may be non-linear if the sctp
  231. * payload was fragmented on the way and ip had to reassemble them.
  232. * We add the rest of skb's to the first skb's fraglist.
  233. */
  234. static struct sctp_ulpevent *sctp_make_reassembled_event(struct sk_buff_head *queue, struct sk_buff *f_frag, struct sk_buff *l_frag)
  235. {
  236. struct sk_buff *pos;
  237. struct sctp_ulpevent *event;
  238. struct sk_buff *pnext, *last;
  239. struct sk_buff *list = skb_shinfo(f_frag)->frag_list;
  240. /* Store the pointer to the 2nd skb */
  241. if (f_frag == l_frag)
  242. pos = NULL;
  243. else
  244. pos = f_frag->next;
  245. /* Get the last skb in the f_frag's frag_list if present. */
  246. for (last = list; list; last = list, list = list->next);
  247. /* Add the list of remaining fragments to the first fragments
  248. * frag_list.
  249. */
  250. if (last)
  251. last->next = pos;
  252. else
  253. skb_shinfo(f_frag)->frag_list = pos;
  254. /* Remove the first fragment from the reassembly queue. */
  255. __skb_unlink(f_frag, queue);
  256. while (pos) {
  257. pnext = pos->next;
  258. /* Update the len and data_len fields of the first fragment. */
  259. f_frag->len += pos->len;
  260. f_frag->data_len += pos->len;
  261. /* Remove the fragment from the reassembly queue. */
  262. __skb_unlink(pos, queue);
  263. /* Break if we have reached the last fragment. */
  264. if (pos == l_frag)
  265. break;
  266. pos->next = pnext;
  267. pos = pnext;
  268. };
  269. event = sctp_skb2event(f_frag);
  270. SCTP_INC_STATS(SCTP_MIB_REASMUSRMSGS);
  271. return event;
  272. }
  273. /* Helper function to check if an incoming chunk has filled up the last
  274. * missing fragment in a SCTP datagram and return the corresponding event.
  275. */
  276. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_reassembled(struct sctp_ulpq *ulpq)
  277. {
  278. struct sk_buff *pos;
  279. struct sctp_ulpevent *cevent;
  280. struct sk_buff *first_frag = NULL;
  281. __u32 ctsn, next_tsn;
  282. struct sctp_ulpevent *retval = NULL;
  283. /* Initialized to 0 just to avoid compiler warning message. Will
  284. * never be used with this value. It is referenced only after it
  285. * is set when we find the first fragment of a message.
  286. */
  287. next_tsn = 0;
  288. /* The chunks are held in the reasm queue sorted by TSN.
  289. * Walk through the queue sequentially and look for a sequence of
  290. * fragmented chunks that complete a datagram.
  291. * 'first_frag' and next_tsn are reset when we find a chunk which
  292. * is the first fragment of a datagram. Once these 2 fields are set
  293. * we expect to find the remaining middle fragments and the last
  294. * fragment in order. If not, first_frag is reset to NULL and we
  295. * start the next pass when we find another first fragment.
  296. */
  297. skb_queue_walk(&ulpq->reasm, pos) {
  298. cevent = sctp_skb2event(pos);
  299. ctsn = cevent->tsn;
  300. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  301. case SCTP_DATA_FIRST_FRAG:
  302. first_frag = pos;
  303. next_tsn = ctsn + 1;
  304. break;
  305. case SCTP_DATA_MIDDLE_FRAG:
  306. if ((first_frag) && (ctsn == next_tsn))
  307. next_tsn++;
  308. else
  309. first_frag = NULL;
  310. break;
  311. case SCTP_DATA_LAST_FRAG:
  312. if (first_frag && (ctsn == next_tsn))
  313. goto found;
  314. else
  315. first_frag = NULL;
  316. break;
  317. };
  318. }
  319. done:
  320. return retval;
  321. found:
  322. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, pos);
  323. if (retval)
  324. retval->msg_flags |= MSG_EOR;
  325. goto done;
  326. }
  327. /* Retrieve the next set of fragments of a partial message. */
  328. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_partial(struct sctp_ulpq *ulpq)
  329. {
  330. struct sk_buff *pos, *last_frag, *first_frag;
  331. struct sctp_ulpevent *cevent;
  332. __u32 ctsn, next_tsn;
  333. int is_last;
  334. struct sctp_ulpevent *retval;
  335. /* The chunks are held in the reasm queue sorted by TSN.
  336. * Walk through the queue sequentially and look for the first
  337. * sequence of fragmented chunks.
  338. */
  339. if (skb_queue_empty(&ulpq->reasm))
  340. return NULL;
  341. last_frag = first_frag = NULL;
  342. retval = NULL;
  343. next_tsn = 0;
  344. is_last = 0;
  345. skb_queue_walk(&ulpq->reasm, pos) {
  346. cevent = sctp_skb2event(pos);
  347. ctsn = cevent->tsn;
  348. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  349. case SCTP_DATA_MIDDLE_FRAG:
  350. if (!first_frag) {
  351. first_frag = pos;
  352. next_tsn = ctsn + 1;
  353. last_frag = pos;
  354. } else if (next_tsn == ctsn)
  355. next_tsn++;
  356. else
  357. goto done;
  358. break;
  359. case SCTP_DATA_LAST_FRAG:
  360. if (!first_frag)
  361. first_frag = pos;
  362. else if (ctsn != next_tsn)
  363. goto done;
  364. last_frag = pos;
  365. is_last = 1;
  366. goto done;
  367. default:
  368. return NULL;
  369. };
  370. }
  371. /* We have the reassembled event. There is no need to look
  372. * further.
  373. */
  374. done:
  375. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  376. if (retval && is_last)
  377. retval->msg_flags |= MSG_EOR;
  378. return retval;
  379. }
  380. /* Helper function to reassemble chunks. Hold chunks on the reasm queue that
  381. * need reassembling.
  382. */
  383. static struct sctp_ulpevent *sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  384. struct sctp_ulpevent *event)
  385. {
  386. struct sctp_ulpevent *retval = NULL;
  387. /* Check if this is part of a fragmented message. */
  388. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  389. event->msg_flags |= MSG_EOR;
  390. return event;
  391. }
  392. sctp_ulpq_store_reasm(ulpq, event);
  393. if (!ulpq->pd_mode)
  394. retval = sctp_ulpq_retrieve_reassembled(ulpq);
  395. else {
  396. __u32 ctsn, ctsnap;
  397. /* Do not even bother unless this is the next tsn to
  398. * be delivered.
  399. */
  400. ctsn = event->tsn;
  401. ctsnap = sctp_tsnmap_get_ctsn(&ulpq->asoc->peer.tsn_map);
  402. if (TSN_lte(ctsn, ctsnap))
  403. retval = sctp_ulpq_retrieve_partial(ulpq);
  404. }
  405. return retval;
  406. }
  407. /* Retrieve the first part (sequential fragments) for partial delivery. */
  408. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_first(struct sctp_ulpq *ulpq)
  409. {
  410. struct sk_buff *pos, *last_frag, *first_frag;
  411. struct sctp_ulpevent *cevent;
  412. __u32 ctsn, next_tsn;
  413. struct sctp_ulpevent *retval;
  414. /* The chunks are held in the reasm queue sorted by TSN.
  415. * Walk through the queue sequentially and look for a sequence of
  416. * fragmented chunks that start a datagram.
  417. */
  418. if (skb_queue_empty(&ulpq->reasm))
  419. return NULL;
  420. last_frag = first_frag = NULL;
  421. retval = NULL;
  422. next_tsn = 0;
  423. skb_queue_walk(&ulpq->reasm, pos) {
  424. cevent = sctp_skb2event(pos);
  425. ctsn = cevent->tsn;
  426. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  427. case SCTP_DATA_FIRST_FRAG:
  428. if (!first_frag) {
  429. first_frag = pos;
  430. next_tsn = ctsn + 1;
  431. last_frag = pos;
  432. } else
  433. goto done;
  434. break;
  435. case SCTP_DATA_MIDDLE_FRAG:
  436. if (!first_frag)
  437. return NULL;
  438. if (ctsn == next_tsn) {
  439. next_tsn++;
  440. last_frag = pos;
  441. } else
  442. goto done;
  443. break;
  444. default:
  445. return NULL;
  446. };
  447. }
  448. /* We have the reassembled event. There is no need to look
  449. * further.
  450. */
  451. done:
  452. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  453. return retval;
  454. }
  455. /* Helper function to gather skbs that have possibly become
  456. * ordered by an an incoming chunk.
  457. */
  458. static inline void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
  459. struct sctp_ulpevent *event)
  460. {
  461. struct sk_buff_head *event_list;
  462. struct sk_buff *pos, *tmp;
  463. struct sctp_ulpevent *cevent;
  464. struct sctp_stream *in;
  465. __u16 sid, csid;
  466. __u16 ssn, cssn;
  467. sid = event->stream;
  468. ssn = event->ssn;
  469. in = &ulpq->asoc->ssnmap->in;
  470. event_list = (struct sk_buff_head *) sctp_event2skb(event)->prev;
  471. /* We are holding the chunks by stream, by SSN. */
  472. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  473. cevent = (struct sctp_ulpevent *) pos->cb;
  474. csid = cevent->stream;
  475. cssn = cevent->ssn;
  476. /* Have we gone too far? */
  477. if (csid > sid)
  478. break;
  479. /* Have we not gone far enough? */
  480. if (csid < sid)
  481. continue;
  482. if (cssn != sctp_ssn_peek(in, sid))
  483. break;
  484. /* Found it, so mark in the ssnmap. */
  485. sctp_ssn_next(in, sid);
  486. __skb_unlink(pos, &ulpq->lobby);
  487. /* Attach all gathered skbs to the event. */
  488. __skb_queue_tail(event_list, pos);
  489. }
  490. }
  491. /* Helper function to store chunks needing ordering. */
  492. static inline void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
  493. struct sctp_ulpevent *event)
  494. {
  495. struct sk_buff *pos;
  496. struct sctp_ulpevent *cevent;
  497. __u16 sid, csid;
  498. __u16 ssn, cssn;
  499. pos = skb_peek_tail(&ulpq->lobby);
  500. if (!pos) {
  501. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  502. return;
  503. }
  504. sid = event->stream;
  505. ssn = event->ssn;
  506. cevent = (struct sctp_ulpevent *) pos->cb;
  507. csid = cevent->stream;
  508. cssn = cevent->ssn;
  509. if (sid > csid) {
  510. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  511. return;
  512. }
  513. if ((sid == csid) && SSN_lt(cssn, ssn)) {
  514. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  515. return;
  516. }
  517. /* Find the right place in this list. We store them by
  518. * stream ID and then by SSN.
  519. */
  520. skb_queue_walk(&ulpq->lobby, pos) {
  521. cevent = (struct sctp_ulpevent *) pos->cb;
  522. csid = cevent->stream;
  523. cssn = cevent->ssn;
  524. if (csid > sid)
  525. break;
  526. if (csid == sid && SSN_lt(ssn, cssn))
  527. break;
  528. }
  529. /* Insert before pos. */
  530. __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->lobby);
  531. }
  532. static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
  533. struct sctp_ulpevent *event)
  534. {
  535. __u16 sid, ssn;
  536. struct sctp_stream *in;
  537. /* Check if this message needs ordering. */
  538. if (SCTP_DATA_UNORDERED & event->msg_flags)
  539. return event;
  540. /* Note: The stream ID must be verified before this routine. */
  541. sid = event->stream;
  542. ssn = event->ssn;
  543. in = &ulpq->asoc->ssnmap->in;
  544. /* Is this the expected SSN for this stream ID? */
  545. if (ssn != sctp_ssn_peek(in, sid)) {
  546. /* We've received something out of order, so find where it
  547. * needs to be placed. We order by stream and then by SSN.
  548. */
  549. sctp_ulpq_store_ordered(ulpq, event);
  550. return NULL;
  551. }
  552. /* Mark that the next chunk has been found. */
  553. sctp_ssn_next(in, sid);
  554. /* Go find any other chunks that were waiting for
  555. * ordering.
  556. */
  557. sctp_ulpq_retrieve_ordered(ulpq, event);
  558. return event;
  559. }
  560. /* Helper function to gather skbs that have possibly become
  561. * ordered by forward tsn skipping their dependencies.
  562. */
  563. static inline void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq)
  564. {
  565. struct sk_buff *pos, *tmp;
  566. struct sctp_ulpevent *cevent;
  567. struct sctp_ulpevent *event;
  568. struct sctp_stream *in;
  569. struct sk_buff_head temp;
  570. __u16 csid, cssn;
  571. in = &ulpq->asoc->ssnmap->in;
  572. /* We are holding the chunks by stream, by SSN. */
  573. skb_queue_head_init(&temp);
  574. event = NULL;
  575. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  576. cevent = (struct sctp_ulpevent *) pos->cb;
  577. csid = cevent->stream;
  578. cssn = cevent->ssn;
  579. if (cssn != sctp_ssn_peek(in, csid))
  580. break;
  581. /* Found it, so mark in the ssnmap. */
  582. sctp_ssn_next(in, csid);
  583. __skb_unlink(pos, &ulpq->lobby);
  584. if (!event) {
  585. /* Create a temporary list to collect chunks on. */
  586. event = sctp_skb2event(pos);
  587. __skb_queue_tail(&temp, sctp_event2skb(event));
  588. } else {
  589. /* Attach all gathered skbs to the event. */
  590. __skb_queue_tail(&temp, pos);
  591. }
  592. }
  593. /* Send event to the ULP. 'event' is the sctp_ulpevent for
  594. * very first SKB on the 'temp' list.
  595. */
  596. if (event)
  597. sctp_ulpq_tail_event(ulpq, event);
  598. }
  599. /* Skip over an SSN. */
  600. void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
  601. {
  602. struct sctp_stream *in;
  603. /* Note: The stream ID must be verified before this routine. */
  604. in = &ulpq->asoc->ssnmap->in;
  605. /* Is this an old SSN? If so ignore. */
  606. if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
  607. return;
  608. /* Mark that we are no longer expecting this SSN or lower. */
  609. sctp_ssn_skip(in, sid, ssn);
  610. /* Go find any other chunks that were waiting for
  611. * ordering and deliver them if needed.
  612. */
  613. sctp_ulpq_reap_ordered(ulpq);
  614. return;
  615. }
  616. /* Renege 'needed' bytes from the ordering queue. */
  617. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  618. {
  619. __u16 freed = 0;
  620. __u32 tsn;
  621. struct sk_buff *skb;
  622. struct sctp_ulpevent *event;
  623. struct sctp_tsnmap *tsnmap;
  624. tsnmap = &ulpq->asoc->peer.tsn_map;
  625. while ((skb = __skb_dequeue_tail(&ulpq->lobby)) != NULL) {
  626. freed += skb_headlen(skb);
  627. event = sctp_skb2event(skb);
  628. tsn = event->tsn;
  629. sctp_ulpevent_free(event);
  630. sctp_tsnmap_renege(tsnmap, tsn);
  631. if (freed >= needed)
  632. return freed;
  633. }
  634. return freed;
  635. }
  636. /* Renege 'needed' bytes from the reassembly queue. */
  637. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  638. {
  639. __u16 freed = 0;
  640. __u32 tsn;
  641. struct sk_buff *skb;
  642. struct sctp_ulpevent *event;
  643. struct sctp_tsnmap *tsnmap;
  644. tsnmap = &ulpq->asoc->peer.tsn_map;
  645. /* Walk backwards through the list, reneges the newest tsns. */
  646. while ((skb = __skb_dequeue_tail(&ulpq->reasm)) != NULL) {
  647. freed += skb_headlen(skb);
  648. event = sctp_skb2event(skb);
  649. tsn = event->tsn;
  650. sctp_ulpevent_free(event);
  651. sctp_tsnmap_renege(tsnmap, tsn);
  652. if (freed >= needed)
  653. return freed;
  654. }
  655. return freed;
  656. }
  657. /* Partial deliver the first message as there is pressure on rwnd. */
  658. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  659. struct sctp_chunk *chunk,
  660. gfp_t gfp)
  661. {
  662. struct sctp_ulpevent *event;
  663. struct sctp_association *asoc;
  664. asoc = ulpq->asoc;
  665. /* Are we already in partial delivery mode? */
  666. if (!sctp_sk(asoc->base.sk)->pd_mode) {
  667. /* Is partial delivery possible? */
  668. event = sctp_ulpq_retrieve_first(ulpq);
  669. /* Send event to the ULP. */
  670. if (event) {
  671. sctp_ulpq_tail_event(ulpq, event);
  672. sctp_sk(asoc->base.sk)->pd_mode = 1;
  673. ulpq->pd_mode = 1;
  674. return;
  675. }
  676. }
  677. }
  678. /* Renege some packets to make room for an incoming chunk. */
  679. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  680. gfp_t gfp)
  681. {
  682. struct sctp_association *asoc;
  683. __u16 needed, freed;
  684. asoc = ulpq->asoc;
  685. if (chunk) {
  686. needed = ntohs(chunk->chunk_hdr->length);
  687. needed -= sizeof(sctp_data_chunk_t);
  688. } else
  689. needed = SCTP_DEFAULT_MAXWINDOW;
  690. freed = 0;
  691. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  692. freed = sctp_ulpq_renege_order(ulpq, needed);
  693. if (freed < needed) {
  694. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  695. }
  696. }
  697. /* If able to free enough room, accept this chunk. */
  698. if (chunk && (freed >= needed)) {
  699. __u32 tsn;
  700. tsn = ntohl(chunk->subh.data_hdr->tsn);
  701. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  702. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  703. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  704. }
  705. return;
  706. }
  707. /* Notify the application if an association is aborted and in
  708. * partial delivery mode. Send up any pending received messages.
  709. */
  710. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  711. {
  712. struct sctp_ulpevent *ev = NULL;
  713. struct sock *sk;
  714. if (!ulpq->pd_mode)
  715. return;
  716. sk = ulpq->asoc->base.sk;
  717. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  718. &sctp_sk(sk)->subscribe))
  719. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  720. SCTP_PARTIAL_DELIVERY_ABORTED,
  721. gfp);
  722. if (ev)
  723. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  724. /* If there is data waiting, send it up the socket now. */
  725. if (sctp_ulpq_clear_pd(ulpq) || ev)
  726. sk->sk_data_ready(sk, 0);
  727. }