ulpqueue.c 27 KB

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