ulpqueue.c 28 KB

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