ulpqueue.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /* SCTP kernel 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. * This SCTP 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. * This SCTP 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/slab.h>
  44. #include <linux/types.h>
  45. #include <linux/skbuff.h>
  46. #include <net/sock.h>
  47. #include <net/sctp/structs.h>
  48. #include <net/sctp/sctp.h>
  49. #include <net/sctp/sm.h>
  50. /* Forward declarations for internal helpers. */
  51. static struct sctp_ulpevent * sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  52. struct sctp_ulpevent *);
  53. static struct sctp_ulpevent * sctp_ulpq_order(struct sctp_ulpq *,
  54. struct sctp_ulpevent *);
  55. static void sctp_ulpq_reasm_drain(struct sctp_ulpq *ulpq);
  56. /* 1st Level Abstractions */
  57. /* Initialize a ULP queue from a block of memory. */
  58. struct sctp_ulpq *sctp_ulpq_init(struct sctp_ulpq *ulpq,
  59. struct sctp_association *asoc)
  60. {
  61. memset(ulpq, 0, sizeof(struct sctp_ulpq));
  62. ulpq->asoc = asoc;
  63. skb_queue_head_init(&ulpq->reasm);
  64. skb_queue_head_init(&ulpq->lobby);
  65. ulpq->pd_mode = 0;
  66. ulpq->malloced = 0;
  67. return ulpq;
  68. }
  69. /* Flush the reassembly and ordering queues. */
  70. void sctp_ulpq_flush(struct sctp_ulpq *ulpq)
  71. {
  72. struct sk_buff *skb;
  73. struct sctp_ulpevent *event;
  74. while ((skb = __skb_dequeue(&ulpq->lobby)) != NULL) {
  75. event = sctp_skb2event(skb);
  76. sctp_ulpevent_free(event);
  77. }
  78. while ((skb = __skb_dequeue(&ulpq->reasm)) != NULL) {
  79. event = sctp_skb2event(skb);
  80. sctp_ulpevent_free(event);
  81. }
  82. }
  83. /* Dispose of a ulpqueue. */
  84. void sctp_ulpq_free(struct sctp_ulpq *ulpq)
  85. {
  86. sctp_ulpq_flush(ulpq);
  87. if (ulpq->malloced)
  88. kfree(ulpq);
  89. }
  90. /* Process an incoming DATA chunk. */
  91. int sctp_ulpq_tail_data(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  92. gfp_t gfp)
  93. {
  94. struct sk_buff_head temp;
  95. struct sctp_ulpevent *event;
  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. /* Set the pd_mode on the socket and ulpq */
  156. static void sctp_ulpq_set_pd(struct sctp_ulpq *ulpq)
  157. {
  158. struct sctp_sock *sp = sctp_sk(ulpq->asoc->base.sk);
  159. atomic_inc(&sp->pd_mode);
  160. ulpq->pd_mode = 1;
  161. }
  162. /* Clear the pd_mode and restart any pending messages waiting for delivery. */
  163. static int sctp_ulpq_clear_pd(struct sctp_ulpq *ulpq)
  164. {
  165. ulpq->pd_mode = 0;
  166. sctp_ulpq_reasm_drain(ulpq);
  167. return sctp_clear_pd(ulpq->asoc->base.sk, ulpq->asoc);
  168. }
  169. /* If the SKB of 'event' is on a list, it is the first such member
  170. * of that list.
  171. */
  172. int sctp_ulpq_tail_event(struct sctp_ulpq *ulpq, struct sctp_ulpevent *event)
  173. {
  174. struct sock *sk = ulpq->asoc->base.sk;
  175. struct sk_buff_head *queue, *skb_list;
  176. struct sk_buff *skb = sctp_event2skb(event);
  177. int clear_pd = 0;
  178. skb_list = (struct sk_buff_head *) skb->prev;
  179. /* If the socket is just going to throw this away, do not
  180. * even try to deliver it.
  181. */
  182. if (sock_flag(sk, SOCK_DEAD) || (sk->sk_shutdown & RCV_SHUTDOWN))
  183. goto out_free;
  184. /* Check if the user wishes to receive this event. */
  185. if (!sctp_ulpevent_is_enabled(event, &sctp_sk(sk)->subscribe))
  186. goto out_free;
  187. /* If we are in partial delivery mode, post to the lobby until
  188. * partial delivery is cleared, unless, of course _this_ is
  189. * the association the cause of the partial delivery.
  190. */
  191. if (atomic_read(&sctp_sk(sk)->pd_mode) == 0) {
  192. queue = &sk->sk_receive_queue;
  193. } else {
  194. if (ulpq->pd_mode) {
  195. /* If the association is in partial delivery, we
  196. * need to finish delivering the partially processed
  197. * packet before passing any other data. This is
  198. * because we don't truly support stream interleaving.
  199. */
  200. if ((event->msg_flags & MSG_NOTIFICATION) ||
  201. (SCTP_DATA_NOT_FRAG ==
  202. (event->msg_flags & SCTP_DATA_FRAG_MASK)))
  203. queue = &sctp_sk(sk)->pd_lobby;
  204. else {
  205. clear_pd = event->msg_flags & MSG_EOR;
  206. queue = &sk->sk_receive_queue;
  207. }
  208. } else {
  209. /*
  210. * If fragment interleave is enabled, we
  211. * can queue this to the receive queue instead
  212. * of the lobby.
  213. */
  214. if (sctp_sk(sk)->frag_interleave)
  215. queue = &sk->sk_receive_queue;
  216. else
  217. queue = &sctp_sk(sk)->pd_lobby;
  218. }
  219. }
  220. /* If we are harvesting multiple skbs they will be
  221. * collected on a list.
  222. */
  223. if (skb_list)
  224. sctp_skb_list_tail(skb_list, queue);
  225. else
  226. __skb_queue_tail(queue, skb);
  227. /* Did we just complete partial delivery and need to get
  228. * rolling again? Move pending data to the receive
  229. * queue.
  230. */
  231. if (clear_pd)
  232. sctp_ulpq_clear_pd(ulpq);
  233. if (queue == &sk->sk_receive_queue)
  234. sk->sk_data_ready(sk, 0);
  235. return 1;
  236. out_free:
  237. if (skb_list)
  238. sctp_queue_purge_ulpevents(skb_list);
  239. else
  240. sctp_ulpevent_free(event);
  241. return 0;
  242. }
  243. /* 2nd Level Abstractions */
  244. /* Helper function to store chunks that need to be reassembled. */
  245. static void sctp_ulpq_store_reasm(struct sctp_ulpq *ulpq,
  246. struct sctp_ulpevent *event)
  247. {
  248. struct sk_buff *pos;
  249. struct sctp_ulpevent *cevent;
  250. __u32 tsn, ctsn;
  251. tsn = event->tsn;
  252. /* See if it belongs at the end. */
  253. pos = skb_peek_tail(&ulpq->reasm);
  254. if (!pos) {
  255. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  256. return;
  257. }
  258. /* Short circuit just dropping it at the end. */
  259. cevent = sctp_skb2event(pos);
  260. ctsn = cevent->tsn;
  261. if (TSN_lt(ctsn, tsn)) {
  262. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  263. return;
  264. }
  265. /* Find the right place in this list. We store them by TSN. */
  266. skb_queue_walk(&ulpq->reasm, pos) {
  267. cevent = sctp_skb2event(pos);
  268. ctsn = cevent->tsn;
  269. if (TSN_lt(tsn, ctsn))
  270. break;
  271. }
  272. /* Insert before pos. */
  273. __skb_queue_before(&ulpq->reasm, pos, sctp_event2skb(event));
  274. }
  275. /* Helper function to return an event corresponding to the reassembled
  276. * datagram.
  277. * This routine creates a re-assembled skb given the first and last skb's
  278. * as stored in the reassembly queue. The skb's may be non-linear if the sctp
  279. * payload was fragmented on the way and ip had to reassemble them.
  280. * We add the rest of skb's to the first skb's fraglist.
  281. */
  282. static struct sctp_ulpevent *sctp_make_reassembled_event(struct sk_buff_head *queue, struct sk_buff *f_frag, struct sk_buff *l_frag)
  283. {
  284. struct sk_buff *pos;
  285. struct sk_buff *new = NULL;
  286. struct sctp_ulpevent *event;
  287. struct sk_buff *pnext, *last;
  288. struct sk_buff *list = skb_shinfo(f_frag)->frag_list;
  289. /* Store the pointer to the 2nd skb */
  290. if (f_frag == l_frag)
  291. pos = NULL;
  292. else
  293. pos = f_frag->next;
  294. /* Get the last skb in the f_frag's frag_list if present. */
  295. for (last = list; list; last = list, list = list->next);
  296. /* Add the list of remaining fragments to the first fragments
  297. * frag_list.
  298. */
  299. if (last)
  300. last->next = pos;
  301. else {
  302. if (skb_cloned(f_frag)) {
  303. /* This is a cloned skb, we can't just modify
  304. * the frag_list. We need a new skb to do that.
  305. * Instead of calling skb_unshare(), we'll do it
  306. * ourselves since we need to delay the free.
  307. */
  308. new = skb_copy(f_frag, GFP_ATOMIC);
  309. if (!new)
  310. return NULL; /* try again later */
  311. sctp_skb_set_owner_r(new, f_frag->sk);
  312. skb_shinfo(new)->frag_list = pos;
  313. } else
  314. skb_shinfo(f_frag)->frag_list = pos;
  315. }
  316. /* Remove the first fragment from the reassembly queue. */
  317. __skb_unlink(f_frag, queue);
  318. /* if we did unshare, then free the old skb and re-assign */
  319. if (new) {
  320. kfree_skb(f_frag);
  321. f_frag = new;
  322. }
  323. while (pos) {
  324. pnext = pos->next;
  325. /* Update the len and data_len fields of the first fragment. */
  326. f_frag->len += pos->len;
  327. f_frag->data_len += pos->len;
  328. /* Remove the fragment from the reassembly queue. */
  329. __skb_unlink(pos, queue);
  330. /* Break if we have reached the last fragment. */
  331. if (pos == l_frag)
  332. break;
  333. pos->next = pnext;
  334. pos = pnext;
  335. }
  336. event = sctp_skb2event(f_frag);
  337. SCTP_INC_STATS(SCTP_MIB_REASMUSRMSGS);
  338. return event;
  339. }
  340. /* Helper function to check if an incoming chunk has filled up the last
  341. * missing fragment in a SCTP datagram and return the corresponding event.
  342. */
  343. static struct sctp_ulpevent *sctp_ulpq_retrieve_reassembled(struct sctp_ulpq *ulpq)
  344. {
  345. struct sk_buff *pos;
  346. struct sctp_ulpevent *cevent;
  347. struct sk_buff *first_frag = NULL;
  348. __u32 ctsn, next_tsn;
  349. struct sctp_ulpevent *retval = NULL;
  350. struct sk_buff *pd_first = NULL;
  351. struct sk_buff *pd_last = NULL;
  352. size_t pd_len = 0;
  353. struct sctp_association *asoc;
  354. u32 pd_point;
  355. /* Initialized to 0 just to avoid compiler warning message. Will
  356. * never be used with this value. It is referenced only after it
  357. * is set when we find the first fragment of a message.
  358. */
  359. next_tsn = 0;
  360. /* The chunks are held in the reasm queue sorted by TSN.
  361. * Walk through the queue sequentially and look for a sequence of
  362. * fragmented chunks that complete a datagram.
  363. * 'first_frag' and next_tsn are reset when we find a chunk which
  364. * is the first fragment of a datagram. Once these 2 fields are set
  365. * we expect to find the remaining middle fragments and the last
  366. * fragment in order. If not, first_frag is reset to NULL and we
  367. * start the next pass when we find another first fragment.
  368. *
  369. * There is a potential to do partial delivery if user sets
  370. * SCTP_PARTIAL_DELIVERY_POINT option. Lets count some things here
  371. * to see if can do PD.
  372. */
  373. skb_queue_walk(&ulpq->reasm, pos) {
  374. cevent = sctp_skb2event(pos);
  375. ctsn = cevent->tsn;
  376. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  377. case SCTP_DATA_FIRST_FRAG:
  378. /* If this "FIRST_FRAG" is the first
  379. * element in the queue, then count it towards
  380. * possible PD.
  381. */
  382. if (pos == ulpq->reasm.next) {
  383. pd_first = pos;
  384. pd_last = pos;
  385. pd_len = pos->len;
  386. } else {
  387. pd_first = NULL;
  388. pd_last = NULL;
  389. pd_len = 0;
  390. }
  391. first_frag = pos;
  392. next_tsn = ctsn + 1;
  393. break;
  394. case SCTP_DATA_MIDDLE_FRAG:
  395. if ((first_frag) && (ctsn == next_tsn)) {
  396. next_tsn++;
  397. if (pd_first) {
  398. pd_last = pos;
  399. pd_len += pos->len;
  400. }
  401. } else
  402. first_frag = NULL;
  403. break;
  404. case SCTP_DATA_LAST_FRAG:
  405. if (first_frag && (ctsn == next_tsn))
  406. goto found;
  407. else
  408. first_frag = NULL;
  409. break;
  410. }
  411. }
  412. asoc = ulpq->asoc;
  413. if (pd_first) {
  414. /* Make sure we can enter partial deliver.
  415. * We can trigger partial delivery only if framgent
  416. * interleave is set, or the socket is not already
  417. * in partial delivery.
  418. */
  419. if (!sctp_sk(asoc->base.sk)->frag_interleave &&
  420. atomic_read(&sctp_sk(asoc->base.sk)->pd_mode))
  421. goto done;
  422. cevent = sctp_skb2event(pd_first);
  423. pd_point = sctp_sk(asoc->base.sk)->pd_point;
  424. if (pd_point && pd_point <= pd_len) {
  425. retval = sctp_make_reassembled_event(&ulpq->reasm,
  426. pd_first,
  427. pd_last);
  428. if (retval)
  429. sctp_ulpq_set_pd(ulpq);
  430. }
  431. }
  432. done:
  433. return retval;
  434. found:
  435. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, pos);
  436. if (retval)
  437. retval->msg_flags |= MSG_EOR;
  438. goto done;
  439. }
  440. /* Retrieve the next set of fragments of a partial message. */
  441. static struct sctp_ulpevent *sctp_ulpq_retrieve_partial(struct sctp_ulpq *ulpq)
  442. {
  443. struct sk_buff *pos, *last_frag, *first_frag;
  444. struct sctp_ulpevent *cevent;
  445. __u32 ctsn, next_tsn;
  446. int is_last;
  447. struct sctp_ulpevent *retval;
  448. /* The chunks are held in the reasm queue sorted by TSN.
  449. * Walk through the queue sequentially and look for the first
  450. * sequence of fragmented chunks.
  451. */
  452. if (skb_queue_empty(&ulpq->reasm))
  453. return NULL;
  454. last_frag = first_frag = NULL;
  455. retval = NULL;
  456. next_tsn = 0;
  457. is_last = 0;
  458. skb_queue_walk(&ulpq->reasm, pos) {
  459. cevent = sctp_skb2event(pos);
  460. ctsn = cevent->tsn;
  461. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  462. case SCTP_DATA_MIDDLE_FRAG:
  463. if (!first_frag) {
  464. first_frag = pos;
  465. next_tsn = ctsn + 1;
  466. last_frag = pos;
  467. } else if (next_tsn == ctsn)
  468. next_tsn++;
  469. else
  470. goto done;
  471. break;
  472. case SCTP_DATA_LAST_FRAG:
  473. if (!first_frag)
  474. first_frag = pos;
  475. else if (ctsn != next_tsn)
  476. goto done;
  477. last_frag = pos;
  478. is_last = 1;
  479. goto done;
  480. default:
  481. return NULL;
  482. }
  483. }
  484. /* We have the reassembled event. There is no need to look
  485. * further.
  486. */
  487. done:
  488. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  489. if (retval && is_last)
  490. retval->msg_flags |= MSG_EOR;
  491. return retval;
  492. }
  493. /* Helper function to reassemble chunks. Hold chunks on the reasm queue that
  494. * need reassembling.
  495. */
  496. static struct sctp_ulpevent *sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  497. struct sctp_ulpevent *event)
  498. {
  499. struct sctp_ulpevent *retval = NULL;
  500. /* Check if this is part of a fragmented message. */
  501. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  502. event->msg_flags |= MSG_EOR;
  503. return event;
  504. }
  505. sctp_ulpq_store_reasm(ulpq, event);
  506. if (!ulpq->pd_mode)
  507. retval = sctp_ulpq_retrieve_reassembled(ulpq);
  508. else {
  509. __u32 ctsn, ctsnap;
  510. /* Do not even bother unless this is the next tsn to
  511. * be delivered.
  512. */
  513. ctsn = event->tsn;
  514. ctsnap = sctp_tsnmap_get_ctsn(&ulpq->asoc->peer.tsn_map);
  515. if (TSN_lte(ctsn, ctsnap))
  516. retval = sctp_ulpq_retrieve_partial(ulpq);
  517. }
  518. return retval;
  519. }
  520. /* Retrieve the first part (sequential fragments) for partial delivery. */
  521. static struct sctp_ulpevent *sctp_ulpq_retrieve_first(struct sctp_ulpq *ulpq)
  522. {
  523. struct sk_buff *pos, *last_frag, *first_frag;
  524. struct sctp_ulpevent *cevent;
  525. __u32 ctsn, next_tsn;
  526. struct sctp_ulpevent *retval;
  527. /* The chunks are held in the reasm queue sorted by TSN.
  528. * Walk through the queue sequentially and look for a sequence of
  529. * fragmented chunks that start a datagram.
  530. */
  531. if (skb_queue_empty(&ulpq->reasm))
  532. return NULL;
  533. last_frag = first_frag = NULL;
  534. retval = NULL;
  535. next_tsn = 0;
  536. skb_queue_walk(&ulpq->reasm, pos) {
  537. cevent = sctp_skb2event(pos);
  538. ctsn = cevent->tsn;
  539. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  540. case SCTP_DATA_FIRST_FRAG:
  541. if (!first_frag) {
  542. first_frag = pos;
  543. next_tsn = ctsn + 1;
  544. last_frag = pos;
  545. } else
  546. goto done;
  547. break;
  548. case SCTP_DATA_MIDDLE_FRAG:
  549. if (!first_frag)
  550. return NULL;
  551. if (ctsn == next_tsn) {
  552. next_tsn++;
  553. last_frag = pos;
  554. } else
  555. goto done;
  556. break;
  557. default:
  558. return NULL;
  559. }
  560. }
  561. /* We have the reassembled event. There is no need to look
  562. * further.
  563. */
  564. done:
  565. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  566. return retval;
  567. }
  568. /*
  569. * Flush out stale fragments from the reassembly queue when processing
  570. * a Forward TSN.
  571. *
  572. * RFC 3758, Section 3.6
  573. *
  574. * After receiving and processing a FORWARD TSN, the data receiver MUST
  575. * take cautions in updating its re-assembly queue. The receiver MUST
  576. * remove any partially reassembled message, which is still missing one
  577. * or more TSNs earlier than or equal to the new cumulative TSN point.
  578. * In the event that the receiver has invoked the partial delivery API,
  579. * a notification SHOULD also be generated to inform the upper layer API
  580. * that the message being partially delivered will NOT be completed.
  581. */
  582. void sctp_ulpq_reasm_flushtsn(struct sctp_ulpq *ulpq, __u32 fwd_tsn)
  583. {
  584. struct sk_buff *pos, *tmp;
  585. struct sctp_ulpevent *event;
  586. __u32 tsn;
  587. if (skb_queue_empty(&ulpq->reasm))
  588. return;
  589. skb_queue_walk_safe(&ulpq->reasm, pos, tmp) {
  590. event = sctp_skb2event(pos);
  591. tsn = event->tsn;
  592. /* Since the entire message must be abandoned by the
  593. * sender (item A3 in Section 3.5, RFC 3758), we can
  594. * free all fragments on the list that are less then
  595. * or equal to ctsn_point
  596. */
  597. if (TSN_lte(tsn, fwd_tsn)) {
  598. __skb_unlink(pos, &ulpq->reasm);
  599. sctp_ulpevent_free(event);
  600. } else
  601. break;
  602. }
  603. }
  604. /*
  605. * Drain the reassembly queue. If we just cleared parted delivery, it
  606. * is possible that the reassembly queue will contain already reassembled
  607. * messages. Retrieve any such messages and give them to the user.
  608. */
  609. static void sctp_ulpq_reasm_drain(struct sctp_ulpq *ulpq)
  610. {
  611. struct sctp_ulpevent *event = NULL;
  612. struct sk_buff_head temp;
  613. if (skb_queue_empty(&ulpq->reasm))
  614. return;
  615. while ((event = sctp_ulpq_retrieve_reassembled(ulpq)) != NULL) {
  616. /* Do ordering if needed. */
  617. if ((event) && (event->msg_flags & MSG_EOR)){
  618. skb_queue_head_init(&temp);
  619. __skb_queue_tail(&temp, sctp_event2skb(event));
  620. event = sctp_ulpq_order(ulpq, event);
  621. }
  622. /* Send event to the ULP. 'event' is the
  623. * sctp_ulpevent for very first SKB on the temp' list.
  624. */
  625. if (event)
  626. sctp_ulpq_tail_event(ulpq, event);
  627. }
  628. }
  629. /* Helper function to gather skbs that have possibly become
  630. * ordered by an an incoming chunk.
  631. */
  632. static void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
  633. struct sctp_ulpevent *event)
  634. {
  635. struct sk_buff_head *event_list;
  636. struct sk_buff *pos, *tmp;
  637. struct sctp_ulpevent *cevent;
  638. struct sctp_stream *in;
  639. __u16 sid, csid, cssn;
  640. sid = event->stream;
  641. in = &ulpq->asoc->ssnmap->in;
  642. event_list = (struct sk_buff_head *) sctp_event2skb(event)->prev;
  643. /* We are holding the chunks by stream, by SSN. */
  644. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  645. cevent = (struct sctp_ulpevent *) pos->cb;
  646. csid = cevent->stream;
  647. cssn = cevent->ssn;
  648. /* Have we gone too far? */
  649. if (csid > sid)
  650. break;
  651. /* Have we not gone far enough? */
  652. if (csid < sid)
  653. continue;
  654. if (cssn != sctp_ssn_peek(in, sid))
  655. break;
  656. /* Found it, so mark in the ssnmap. */
  657. sctp_ssn_next(in, sid);
  658. __skb_unlink(pos, &ulpq->lobby);
  659. /* Attach all gathered skbs to the event. */
  660. __skb_queue_tail(event_list, pos);
  661. }
  662. }
  663. /* Helper function to store chunks needing ordering. */
  664. static void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
  665. struct sctp_ulpevent *event)
  666. {
  667. struct sk_buff *pos;
  668. struct sctp_ulpevent *cevent;
  669. __u16 sid, csid;
  670. __u16 ssn, cssn;
  671. pos = skb_peek_tail(&ulpq->lobby);
  672. if (!pos) {
  673. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  674. return;
  675. }
  676. sid = event->stream;
  677. ssn = event->ssn;
  678. cevent = (struct sctp_ulpevent *) pos->cb;
  679. csid = cevent->stream;
  680. cssn = cevent->ssn;
  681. if (sid > csid) {
  682. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  683. return;
  684. }
  685. if ((sid == csid) && SSN_lt(cssn, ssn)) {
  686. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  687. return;
  688. }
  689. /* Find the right place in this list. We store them by
  690. * stream ID and then by SSN.
  691. */
  692. skb_queue_walk(&ulpq->lobby, pos) {
  693. cevent = (struct sctp_ulpevent *) pos->cb;
  694. csid = cevent->stream;
  695. cssn = cevent->ssn;
  696. if (csid > sid)
  697. break;
  698. if (csid == sid && SSN_lt(ssn, cssn))
  699. break;
  700. }
  701. /* Insert before pos. */
  702. __skb_queue_before(&ulpq->lobby, pos, sctp_event2skb(event));
  703. }
  704. static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
  705. struct sctp_ulpevent *event)
  706. {
  707. __u16 sid, ssn;
  708. struct sctp_stream *in;
  709. /* Check if this message needs ordering. */
  710. if (SCTP_DATA_UNORDERED & event->msg_flags)
  711. return event;
  712. /* Note: The stream ID must be verified before this routine. */
  713. sid = event->stream;
  714. ssn = event->ssn;
  715. in = &ulpq->asoc->ssnmap->in;
  716. /* Is this the expected SSN for this stream ID? */
  717. if (ssn != sctp_ssn_peek(in, sid)) {
  718. /* We've received something out of order, so find where it
  719. * needs to be placed. We order by stream and then by SSN.
  720. */
  721. sctp_ulpq_store_ordered(ulpq, event);
  722. return NULL;
  723. }
  724. /* Mark that the next chunk has been found. */
  725. sctp_ssn_next(in, sid);
  726. /* Go find any other chunks that were waiting for
  727. * ordering.
  728. */
  729. sctp_ulpq_retrieve_ordered(ulpq, event);
  730. return event;
  731. }
  732. /* Helper function to gather skbs that have possibly become
  733. * ordered by forward tsn skipping their dependencies.
  734. */
  735. static void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq, __u16 sid)
  736. {
  737. struct sk_buff *pos, *tmp;
  738. struct sctp_ulpevent *cevent;
  739. struct sctp_ulpevent *event;
  740. struct sctp_stream *in;
  741. struct sk_buff_head temp;
  742. struct sk_buff_head *lobby = &ulpq->lobby;
  743. __u16 csid, cssn;
  744. in = &ulpq->asoc->ssnmap->in;
  745. /* We are holding the chunks by stream, by SSN. */
  746. skb_queue_head_init(&temp);
  747. event = NULL;
  748. sctp_skb_for_each(pos, lobby, tmp) {
  749. cevent = (struct sctp_ulpevent *) pos->cb;
  750. csid = cevent->stream;
  751. cssn = cevent->ssn;
  752. /* Have we gone too far? */
  753. if (csid > sid)
  754. break;
  755. /* Have we not gone far enough? */
  756. if (csid < sid)
  757. continue;
  758. /* see if this ssn has been marked by skipping */
  759. if (!SSN_lt(cssn, sctp_ssn_peek(in, csid)))
  760. break;
  761. __skb_unlink(pos, lobby);
  762. if (!event)
  763. /* Create a temporary list to collect chunks on. */
  764. event = sctp_skb2event(pos);
  765. /* Attach all gathered skbs to the event. */
  766. __skb_queue_tail(&temp, pos);
  767. }
  768. /* If we didn't reap any data, see if the next expected SSN
  769. * is next on the queue and if so, use that.
  770. */
  771. if (event == NULL && pos != (struct sk_buff *)lobby) {
  772. cevent = (struct sctp_ulpevent *) pos->cb;
  773. csid = cevent->stream;
  774. cssn = cevent->ssn;
  775. if (csid == sid && cssn == sctp_ssn_peek(in, csid)) {
  776. sctp_ssn_next(in, csid);
  777. __skb_unlink(pos, lobby);
  778. __skb_queue_tail(&temp, pos);
  779. event = sctp_skb2event(pos);
  780. }
  781. }
  782. /* Send event to the ULP. 'event' is the sctp_ulpevent for
  783. * very first SKB on the 'temp' list.
  784. */
  785. if (event) {
  786. /* see if we have more ordered that we can deliver */
  787. sctp_ulpq_retrieve_ordered(ulpq, event);
  788. sctp_ulpq_tail_event(ulpq, event);
  789. }
  790. }
  791. /* Skip over an SSN. This is used during the processing of
  792. * Forwared TSN chunk to skip over the abandoned ordered data
  793. */
  794. void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
  795. {
  796. struct sctp_stream *in;
  797. /* Note: The stream ID must be verified before this routine. */
  798. in = &ulpq->asoc->ssnmap->in;
  799. /* Is this an old SSN? If so ignore. */
  800. if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
  801. return;
  802. /* Mark that we are no longer expecting this SSN or lower. */
  803. sctp_ssn_skip(in, sid, ssn);
  804. /* Go find any other chunks that were waiting for
  805. * ordering and deliver them if needed.
  806. */
  807. sctp_ulpq_reap_ordered(ulpq, sid);
  808. }
  809. static __u16 sctp_ulpq_renege_list(struct sctp_ulpq *ulpq,
  810. struct sk_buff_head *list, __u16 needed)
  811. {
  812. __u16 freed = 0;
  813. __u32 tsn;
  814. struct sk_buff *skb;
  815. struct sctp_ulpevent *event;
  816. struct sctp_tsnmap *tsnmap;
  817. tsnmap = &ulpq->asoc->peer.tsn_map;
  818. while ((skb = __skb_dequeue_tail(list)) != NULL) {
  819. freed += skb_headlen(skb);
  820. event = sctp_skb2event(skb);
  821. tsn = event->tsn;
  822. sctp_ulpevent_free(event);
  823. sctp_tsnmap_renege(tsnmap, tsn);
  824. if (freed >= needed)
  825. return freed;
  826. }
  827. return freed;
  828. }
  829. /* Renege 'needed' bytes from the ordering queue. */
  830. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  831. {
  832. return sctp_ulpq_renege_list(ulpq, &ulpq->lobby, needed);
  833. }
  834. /* Renege 'needed' bytes from the reassembly queue. */
  835. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  836. {
  837. return sctp_ulpq_renege_list(ulpq, &ulpq->reasm, needed);
  838. }
  839. /* Partial deliver the first message as there is pressure on rwnd. */
  840. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  841. struct sctp_chunk *chunk,
  842. gfp_t gfp)
  843. {
  844. struct sctp_ulpevent *event;
  845. struct sctp_association *asoc;
  846. struct sctp_sock *sp;
  847. asoc = ulpq->asoc;
  848. sp = sctp_sk(asoc->base.sk);
  849. /* If the association is already in Partial Delivery mode
  850. * we have noting to do.
  851. */
  852. if (ulpq->pd_mode)
  853. return;
  854. /* If the user enabled fragment interleave socket option,
  855. * multiple associations can enter partial delivery.
  856. * Otherwise, we can only enter partial delivery if the
  857. * socket is not in partial deliver mode.
  858. */
  859. if (sp->frag_interleave || atomic_read(&sp->pd_mode) == 0) {
  860. /* Is partial delivery possible? */
  861. event = sctp_ulpq_retrieve_first(ulpq);
  862. /* Send event to the ULP. */
  863. if (event) {
  864. sctp_ulpq_tail_event(ulpq, event);
  865. sctp_ulpq_set_pd(ulpq);
  866. return;
  867. }
  868. }
  869. }
  870. /* Renege some packets to make room for an incoming chunk. */
  871. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  872. gfp_t gfp)
  873. {
  874. struct sctp_association *asoc;
  875. __u16 needed, freed;
  876. asoc = ulpq->asoc;
  877. if (chunk) {
  878. needed = ntohs(chunk->chunk_hdr->length);
  879. needed -= sizeof(sctp_data_chunk_t);
  880. } else
  881. needed = SCTP_DEFAULT_MAXWINDOW;
  882. freed = 0;
  883. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  884. freed = sctp_ulpq_renege_order(ulpq, needed);
  885. if (freed < needed) {
  886. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  887. }
  888. }
  889. /* If able to free enough room, accept this chunk. */
  890. if (chunk && (freed >= needed)) {
  891. __u32 tsn;
  892. tsn = ntohl(chunk->subh.data_hdr->tsn);
  893. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  894. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  895. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  896. }
  897. sk_mem_reclaim(asoc->base.sk);
  898. }
  899. /* Notify the application if an association is aborted and in
  900. * partial delivery mode. Send up any pending received messages.
  901. */
  902. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  903. {
  904. struct sctp_ulpevent *ev = NULL;
  905. struct sock *sk;
  906. if (!ulpq->pd_mode)
  907. return;
  908. sk = ulpq->asoc->base.sk;
  909. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  910. &sctp_sk(sk)->subscribe))
  911. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  912. SCTP_PARTIAL_DELIVERY_ABORTED,
  913. gfp);
  914. if (ev)
  915. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  916. /* If there is data waiting, send it up the socket now. */
  917. if (sctp_ulpq_clear_pd(ulpq) || ev)
  918. sk->sk_data_ready(sk, 0);
  919. }