ulpqueue.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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 sk_buff *new = NULL;
  238. struct sctp_ulpevent *event;
  239. struct sk_buff *pnext, *last;
  240. struct sk_buff *list = skb_shinfo(f_frag)->frag_list;
  241. /* Store the pointer to the 2nd skb */
  242. if (f_frag == l_frag)
  243. pos = NULL;
  244. else
  245. pos = f_frag->next;
  246. /* Get the last skb in the f_frag's frag_list if present. */
  247. for (last = list; list; last = list, list = list->next);
  248. /* Add the list of remaining fragments to the first fragments
  249. * frag_list.
  250. */
  251. if (last)
  252. last->next = pos;
  253. else {
  254. if (skb_cloned(f_frag)) {
  255. /* This is a cloned skb, we can't just modify
  256. * the frag_list. We need a new skb to do that.
  257. * Instead of calling skb_unshare(), we'll do it
  258. * ourselves since we need to delay the free.
  259. */
  260. new = skb_copy(f_frag, GFP_ATOMIC);
  261. if (!new)
  262. return NULL; /* try again later */
  263. new->sk = f_frag->sk;
  264. skb_shinfo(new)->frag_list = pos;
  265. } else
  266. skb_shinfo(f_frag)->frag_list = pos;
  267. }
  268. /* Remove the first fragment from the reassembly queue. */
  269. __skb_unlink(f_frag, queue);
  270. /* if we did unshare, then free the old skb and re-assign */
  271. if (new) {
  272. kfree_skb(f_frag);
  273. f_frag = new;
  274. }
  275. while (pos) {
  276. pnext = pos->next;
  277. /* Update the len and data_len fields of the first fragment. */
  278. f_frag->len += pos->len;
  279. f_frag->data_len += pos->len;
  280. /* Remove the fragment from the reassembly queue. */
  281. __skb_unlink(pos, queue);
  282. /* Break if we have reached the last fragment. */
  283. if (pos == l_frag)
  284. break;
  285. pos->next = pnext;
  286. pos = pnext;
  287. };
  288. event = sctp_skb2event(f_frag);
  289. SCTP_INC_STATS(SCTP_MIB_REASMUSRMSGS);
  290. return event;
  291. }
  292. /* Helper function to check if an incoming chunk has filled up the last
  293. * missing fragment in a SCTP datagram and return the corresponding event.
  294. */
  295. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_reassembled(struct sctp_ulpq *ulpq)
  296. {
  297. struct sk_buff *pos;
  298. struct sctp_ulpevent *cevent;
  299. struct sk_buff *first_frag = NULL;
  300. __u32 ctsn, next_tsn;
  301. struct sctp_ulpevent *retval = NULL;
  302. /* Initialized to 0 just to avoid compiler warning message. Will
  303. * never be used with this value. It is referenced only after it
  304. * is set when we find the first fragment of a message.
  305. */
  306. next_tsn = 0;
  307. /* The chunks are held in the reasm queue sorted by TSN.
  308. * Walk through the queue sequentially and look for a sequence of
  309. * fragmented chunks that complete a datagram.
  310. * 'first_frag' and next_tsn are reset when we find a chunk which
  311. * is the first fragment of a datagram. Once these 2 fields are set
  312. * we expect to find the remaining middle fragments and the last
  313. * fragment in order. If not, first_frag is reset to NULL and we
  314. * start the next pass when we find another first fragment.
  315. */
  316. skb_queue_walk(&ulpq->reasm, pos) {
  317. cevent = sctp_skb2event(pos);
  318. ctsn = cevent->tsn;
  319. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  320. case SCTP_DATA_FIRST_FRAG:
  321. first_frag = pos;
  322. next_tsn = ctsn + 1;
  323. break;
  324. case SCTP_DATA_MIDDLE_FRAG:
  325. if ((first_frag) && (ctsn == next_tsn))
  326. next_tsn++;
  327. else
  328. first_frag = NULL;
  329. break;
  330. case SCTP_DATA_LAST_FRAG:
  331. if (first_frag && (ctsn == next_tsn))
  332. goto found;
  333. else
  334. first_frag = NULL;
  335. break;
  336. };
  337. }
  338. done:
  339. return retval;
  340. found:
  341. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, pos);
  342. if (retval)
  343. retval->msg_flags |= MSG_EOR;
  344. goto done;
  345. }
  346. /* Retrieve the next set of fragments of a partial message. */
  347. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_partial(struct sctp_ulpq *ulpq)
  348. {
  349. struct sk_buff *pos, *last_frag, *first_frag;
  350. struct sctp_ulpevent *cevent;
  351. __u32 ctsn, next_tsn;
  352. int is_last;
  353. struct sctp_ulpevent *retval;
  354. /* The chunks are held in the reasm queue sorted by TSN.
  355. * Walk through the queue sequentially and look for the first
  356. * sequence of fragmented chunks.
  357. */
  358. if (skb_queue_empty(&ulpq->reasm))
  359. return NULL;
  360. last_frag = first_frag = NULL;
  361. retval = NULL;
  362. next_tsn = 0;
  363. is_last = 0;
  364. skb_queue_walk(&ulpq->reasm, pos) {
  365. cevent = sctp_skb2event(pos);
  366. ctsn = cevent->tsn;
  367. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  368. case SCTP_DATA_MIDDLE_FRAG:
  369. if (!first_frag) {
  370. first_frag = pos;
  371. next_tsn = ctsn + 1;
  372. last_frag = pos;
  373. } else if (next_tsn == ctsn)
  374. next_tsn++;
  375. else
  376. goto done;
  377. break;
  378. case SCTP_DATA_LAST_FRAG:
  379. if (!first_frag)
  380. first_frag = pos;
  381. else if (ctsn != next_tsn)
  382. goto done;
  383. last_frag = pos;
  384. is_last = 1;
  385. goto done;
  386. default:
  387. return NULL;
  388. };
  389. }
  390. /* We have the reassembled event. There is no need to look
  391. * further.
  392. */
  393. done:
  394. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  395. if (retval && is_last)
  396. retval->msg_flags |= MSG_EOR;
  397. return retval;
  398. }
  399. /* Helper function to reassemble chunks. Hold chunks on the reasm queue that
  400. * need reassembling.
  401. */
  402. static struct sctp_ulpevent *sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  403. struct sctp_ulpevent *event)
  404. {
  405. struct sctp_ulpevent *retval = NULL;
  406. /* Check if this is part of a fragmented message. */
  407. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  408. event->msg_flags |= MSG_EOR;
  409. return event;
  410. }
  411. sctp_ulpq_store_reasm(ulpq, event);
  412. if (!ulpq->pd_mode)
  413. retval = sctp_ulpq_retrieve_reassembled(ulpq);
  414. else {
  415. __u32 ctsn, ctsnap;
  416. /* Do not even bother unless this is the next tsn to
  417. * be delivered.
  418. */
  419. ctsn = event->tsn;
  420. ctsnap = sctp_tsnmap_get_ctsn(&ulpq->asoc->peer.tsn_map);
  421. if (TSN_lte(ctsn, ctsnap))
  422. retval = sctp_ulpq_retrieve_partial(ulpq);
  423. }
  424. return retval;
  425. }
  426. /* Retrieve the first part (sequential fragments) for partial delivery. */
  427. static inline struct sctp_ulpevent *sctp_ulpq_retrieve_first(struct sctp_ulpq *ulpq)
  428. {
  429. struct sk_buff *pos, *last_frag, *first_frag;
  430. struct sctp_ulpevent *cevent;
  431. __u32 ctsn, next_tsn;
  432. struct sctp_ulpevent *retval;
  433. /* The chunks are held in the reasm queue sorted by TSN.
  434. * Walk through the queue sequentially and look for a sequence of
  435. * fragmented chunks that start a datagram.
  436. */
  437. if (skb_queue_empty(&ulpq->reasm))
  438. return NULL;
  439. last_frag = first_frag = NULL;
  440. retval = NULL;
  441. next_tsn = 0;
  442. skb_queue_walk(&ulpq->reasm, pos) {
  443. cevent = sctp_skb2event(pos);
  444. ctsn = cevent->tsn;
  445. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  446. case SCTP_DATA_FIRST_FRAG:
  447. if (!first_frag) {
  448. first_frag = pos;
  449. next_tsn = ctsn + 1;
  450. last_frag = pos;
  451. } else
  452. goto done;
  453. break;
  454. case SCTP_DATA_MIDDLE_FRAG:
  455. if (!first_frag)
  456. return NULL;
  457. if (ctsn == next_tsn) {
  458. next_tsn++;
  459. last_frag = pos;
  460. } else
  461. goto done;
  462. break;
  463. default:
  464. return NULL;
  465. };
  466. }
  467. /* We have the reassembled event. There is no need to look
  468. * further.
  469. */
  470. done:
  471. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  472. return retval;
  473. }
  474. /* Helper function to gather skbs that have possibly become
  475. * ordered by an an incoming chunk.
  476. */
  477. static inline void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
  478. struct sctp_ulpevent *event)
  479. {
  480. struct sk_buff_head *event_list;
  481. struct sk_buff *pos, *tmp;
  482. struct sctp_ulpevent *cevent;
  483. struct sctp_stream *in;
  484. __u16 sid, csid;
  485. __u16 ssn, cssn;
  486. sid = event->stream;
  487. ssn = event->ssn;
  488. in = &ulpq->asoc->ssnmap->in;
  489. event_list = (struct sk_buff_head *) sctp_event2skb(event)->prev;
  490. /* We are holding the chunks by stream, by SSN. */
  491. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  492. cevent = (struct sctp_ulpevent *) pos->cb;
  493. csid = cevent->stream;
  494. cssn = cevent->ssn;
  495. /* Have we gone too far? */
  496. if (csid > sid)
  497. break;
  498. /* Have we not gone far enough? */
  499. if (csid < sid)
  500. continue;
  501. if (cssn != sctp_ssn_peek(in, sid))
  502. break;
  503. /* Found it, so mark in the ssnmap. */
  504. sctp_ssn_next(in, sid);
  505. __skb_unlink(pos, &ulpq->lobby);
  506. /* Attach all gathered skbs to the event. */
  507. __skb_queue_tail(event_list, pos);
  508. }
  509. }
  510. /* Helper function to store chunks needing ordering. */
  511. static inline void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
  512. struct sctp_ulpevent *event)
  513. {
  514. struct sk_buff *pos;
  515. struct sctp_ulpevent *cevent;
  516. __u16 sid, csid;
  517. __u16 ssn, cssn;
  518. pos = skb_peek_tail(&ulpq->lobby);
  519. if (!pos) {
  520. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  521. return;
  522. }
  523. sid = event->stream;
  524. ssn = event->ssn;
  525. cevent = (struct sctp_ulpevent *) pos->cb;
  526. csid = cevent->stream;
  527. cssn = cevent->ssn;
  528. if (sid > csid) {
  529. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  530. return;
  531. }
  532. if ((sid == csid) && SSN_lt(cssn, ssn)) {
  533. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  534. return;
  535. }
  536. /* Find the right place in this list. We store them by
  537. * stream ID and then by SSN.
  538. */
  539. skb_queue_walk(&ulpq->lobby, pos) {
  540. cevent = (struct sctp_ulpevent *) pos->cb;
  541. csid = cevent->stream;
  542. cssn = cevent->ssn;
  543. if (csid > sid)
  544. break;
  545. if (csid == sid && SSN_lt(ssn, cssn))
  546. break;
  547. }
  548. /* Insert before pos. */
  549. __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->lobby);
  550. }
  551. static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
  552. struct sctp_ulpevent *event)
  553. {
  554. __u16 sid, ssn;
  555. struct sctp_stream *in;
  556. /* Check if this message needs ordering. */
  557. if (SCTP_DATA_UNORDERED & event->msg_flags)
  558. return event;
  559. /* Note: The stream ID must be verified before this routine. */
  560. sid = event->stream;
  561. ssn = event->ssn;
  562. in = &ulpq->asoc->ssnmap->in;
  563. /* Is this the expected SSN for this stream ID? */
  564. if (ssn != sctp_ssn_peek(in, sid)) {
  565. /* We've received something out of order, so find where it
  566. * needs to be placed. We order by stream and then by SSN.
  567. */
  568. sctp_ulpq_store_ordered(ulpq, event);
  569. return NULL;
  570. }
  571. /* Mark that the next chunk has been found. */
  572. sctp_ssn_next(in, sid);
  573. /* Go find any other chunks that were waiting for
  574. * ordering.
  575. */
  576. sctp_ulpq_retrieve_ordered(ulpq, event);
  577. return event;
  578. }
  579. /* Helper function to gather skbs that have possibly become
  580. * ordered by forward tsn skipping their dependencies.
  581. */
  582. static inline void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq)
  583. {
  584. struct sk_buff *pos, *tmp;
  585. struct sctp_ulpevent *cevent;
  586. struct sctp_ulpevent *event;
  587. struct sctp_stream *in;
  588. struct sk_buff_head temp;
  589. __u16 csid, cssn;
  590. in = &ulpq->asoc->ssnmap->in;
  591. /* We are holding the chunks by stream, by SSN. */
  592. skb_queue_head_init(&temp);
  593. event = NULL;
  594. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  595. cevent = (struct sctp_ulpevent *) pos->cb;
  596. csid = cevent->stream;
  597. cssn = cevent->ssn;
  598. if (cssn != sctp_ssn_peek(in, csid))
  599. break;
  600. /* Found it, so mark in the ssnmap. */
  601. sctp_ssn_next(in, csid);
  602. __skb_unlink(pos, &ulpq->lobby);
  603. if (!event) {
  604. /* Create a temporary list to collect chunks on. */
  605. event = sctp_skb2event(pos);
  606. __skb_queue_tail(&temp, sctp_event2skb(event));
  607. } else {
  608. /* Attach all gathered skbs to the event. */
  609. __skb_queue_tail(&temp, pos);
  610. }
  611. }
  612. /* Send event to the ULP. 'event' is the sctp_ulpevent for
  613. * very first SKB on the 'temp' list.
  614. */
  615. if (event)
  616. sctp_ulpq_tail_event(ulpq, event);
  617. }
  618. /* Skip over an SSN. */
  619. void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
  620. {
  621. struct sctp_stream *in;
  622. /* Note: The stream ID must be verified before this routine. */
  623. in = &ulpq->asoc->ssnmap->in;
  624. /* Is this an old SSN? If so ignore. */
  625. if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
  626. return;
  627. /* Mark that we are no longer expecting this SSN or lower. */
  628. sctp_ssn_skip(in, sid, ssn);
  629. /* Go find any other chunks that were waiting for
  630. * ordering and deliver them if needed.
  631. */
  632. sctp_ulpq_reap_ordered(ulpq);
  633. return;
  634. }
  635. /* Renege 'needed' bytes from the ordering queue. */
  636. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  637. {
  638. __u16 freed = 0;
  639. __u32 tsn;
  640. struct sk_buff *skb;
  641. struct sctp_ulpevent *event;
  642. struct sctp_tsnmap *tsnmap;
  643. tsnmap = &ulpq->asoc->peer.tsn_map;
  644. while ((skb = __skb_dequeue_tail(&ulpq->lobby)) != NULL) {
  645. freed += skb_headlen(skb);
  646. event = sctp_skb2event(skb);
  647. tsn = event->tsn;
  648. sctp_ulpevent_free(event);
  649. sctp_tsnmap_renege(tsnmap, tsn);
  650. if (freed >= needed)
  651. return freed;
  652. }
  653. return freed;
  654. }
  655. /* Renege 'needed' bytes from the reassembly queue. */
  656. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  657. {
  658. __u16 freed = 0;
  659. __u32 tsn;
  660. struct sk_buff *skb;
  661. struct sctp_ulpevent *event;
  662. struct sctp_tsnmap *tsnmap;
  663. tsnmap = &ulpq->asoc->peer.tsn_map;
  664. /* Walk backwards through the list, reneges the newest tsns. */
  665. while ((skb = __skb_dequeue_tail(&ulpq->reasm)) != NULL) {
  666. freed += skb_headlen(skb);
  667. event = sctp_skb2event(skb);
  668. tsn = event->tsn;
  669. sctp_ulpevent_free(event);
  670. sctp_tsnmap_renege(tsnmap, tsn);
  671. if (freed >= needed)
  672. return freed;
  673. }
  674. return freed;
  675. }
  676. /* Partial deliver the first message as there is pressure on rwnd. */
  677. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  678. struct sctp_chunk *chunk,
  679. gfp_t gfp)
  680. {
  681. struct sctp_ulpevent *event;
  682. struct sctp_association *asoc;
  683. asoc = ulpq->asoc;
  684. /* Are we already in partial delivery mode? */
  685. if (!sctp_sk(asoc->base.sk)->pd_mode) {
  686. /* Is partial delivery possible? */
  687. event = sctp_ulpq_retrieve_first(ulpq);
  688. /* Send event to the ULP. */
  689. if (event) {
  690. sctp_ulpq_tail_event(ulpq, event);
  691. sctp_sk(asoc->base.sk)->pd_mode = 1;
  692. ulpq->pd_mode = 1;
  693. return;
  694. }
  695. }
  696. }
  697. /* Renege some packets to make room for an incoming chunk. */
  698. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  699. gfp_t gfp)
  700. {
  701. struct sctp_association *asoc;
  702. __u16 needed, freed;
  703. asoc = ulpq->asoc;
  704. if (chunk) {
  705. needed = ntohs(chunk->chunk_hdr->length);
  706. needed -= sizeof(sctp_data_chunk_t);
  707. } else
  708. needed = SCTP_DEFAULT_MAXWINDOW;
  709. freed = 0;
  710. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  711. freed = sctp_ulpq_renege_order(ulpq, needed);
  712. if (freed < needed) {
  713. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  714. }
  715. }
  716. /* If able to free enough room, accept this chunk. */
  717. if (chunk && (freed >= needed)) {
  718. __u32 tsn;
  719. tsn = ntohl(chunk->subh.data_hdr->tsn);
  720. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  721. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  722. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  723. }
  724. return;
  725. }
  726. /* Notify the application if an association is aborted and in
  727. * partial delivery mode. Send up any pending received messages.
  728. */
  729. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  730. {
  731. struct sctp_ulpevent *ev = NULL;
  732. struct sock *sk;
  733. if (!ulpq->pd_mode)
  734. return;
  735. sk = ulpq->asoc->base.sk;
  736. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  737. &sctp_sk(sk)->subscribe))
  738. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  739. SCTP_PARTIAL_DELIVERY_ABORTED,
  740. gfp);
  741. if (ev)
  742. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  743. /* If there is data waiting, send it up the socket now. */
  744. if (sctp_ulpq_clear_pd(ulpq) || ev)
  745. sk->sk_data_ready(sk, 0);
  746. }