ulpqueue.c 24 KB

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