ulpqueue.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. /* Helper function to gather skbs that have possibly become
  568. * ordered by an an incoming chunk.
  569. */
  570. static inline void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
  571. struct sctp_ulpevent *event)
  572. {
  573. struct sk_buff_head *event_list;
  574. struct sk_buff *pos, *tmp;
  575. struct sctp_ulpevent *cevent;
  576. struct sctp_stream *in;
  577. __u16 sid, csid;
  578. __u16 ssn, cssn;
  579. sid = event->stream;
  580. ssn = event->ssn;
  581. in = &ulpq->asoc->ssnmap->in;
  582. event_list = (struct sk_buff_head *) sctp_event2skb(event)->prev;
  583. /* We are holding the chunks by stream, by SSN. */
  584. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  585. cevent = (struct sctp_ulpevent *) pos->cb;
  586. csid = cevent->stream;
  587. cssn = cevent->ssn;
  588. /* Have we gone too far? */
  589. if (csid > sid)
  590. break;
  591. /* Have we not gone far enough? */
  592. if (csid < sid)
  593. continue;
  594. if (cssn != sctp_ssn_peek(in, sid))
  595. break;
  596. /* Found it, so mark in the ssnmap. */
  597. sctp_ssn_next(in, sid);
  598. __skb_unlink(pos, &ulpq->lobby);
  599. /* Attach all gathered skbs to the event. */
  600. __skb_queue_tail(event_list, pos);
  601. }
  602. }
  603. /* Helper function to store chunks needing ordering. */
  604. static inline void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
  605. struct sctp_ulpevent *event)
  606. {
  607. struct sk_buff *pos;
  608. struct sctp_ulpevent *cevent;
  609. __u16 sid, csid;
  610. __u16 ssn, cssn;
  611. pos = skb_peek_tail(&ulpq->lobby);
  612. if (!pos) {
  613. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  614. return;
  615. }
  616. sid = event->stream;
  617. ssn = event->ssn;
  618. cevent = (struct sctp_ulpevent *) pos->cb;
  619. csid = cevent->stream;
  620. cssn = cevent->ssn;
  621. if (sid > csid) {
  622. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  623. return;
  624. }
  625. if ((sid == csid) && SSN_lt(cssn, ssn)) {
  626. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  627. return;
  628. }
  629. /* Find the right place in this list. We store them by
  630. * stream ID and then by SSN.
  631. */
  632. skb_queue_walk(&ulpq->lobby, pos) {
  633. cevent = (struct sctp_ulpevent *) pos->cb;
  634. csid = cevent->stream;
  635. cssn = cevent->ssn;
  636. if (csid > sid)
  637. break;
  638. if (csid == sid && SSN_lt(ssn, cssn))
  639. break;
  640. }
  641. /* Insert before pos. */
  642. __skb_insert(sctp_event2skb(event), pos->prev, pos, &ulpq->lobby);
  643. }
  644. static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
  645. struct sctp_ulpevent *event)
  646. {
  647. __u16 sid, ssn;
  648. struct sctp_stream *in;
  649. /* Check if this message needs ordering. */
  650. if (SCTP_DATA_UNORDERED & event->msg_flags)
  651. return event;
  652. /* Note: The stream ID must be verified before this routine. */
  653. sid = event->stream;
  654. ssn = event->ssn;
  655. in = &ulpq->asoc->ssnmap->in;
  656. /* Is this the expected SSN for this stream ID? */
  657. if (ssn != sctp_ssn_peek(in, sid)) {
  658. /* We've received something out of order, so find where it
  659. * needs to be placed. We order by stream and then by SSN.
  660. */
  661. sctp_ulpq_store_ordered(ulpq, event);
  662. return NULL;
  663. }
  664. /* Mark that the next chunk has been found. */
  665. sctp_ssn_next(in, sid);
  666. /* Go find any other chunks that were waiting for
  667. * ordering.
  668. */
  669. sctp_ulpq_retrieve_ordered(ulpq, event);
  670. return event;
  671. }
  672. /* Helper function to gather skbs that have possibly become
  673. * ordered by forward tsn skipping their dependencies.
  674. */
  675. static inline void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq)
  676. {
  677. struct sk_buff *pos, *tmp;
  678. struct sctp_ulpevent *cevent;
  679. struct sctp_ulpevent *event;
  680. struct sctp_stream *in;
  681. struct sk_buff_head temp;
  682. __u16 csid, cssn;
  683. in = &ulpq->asoc->ssnmap->in;
  684. /* We are holding the chunks by stream, by SSN. */
  685. skb_queue_head_init(&temp);
  686. event = NULL;
  687. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  688. cevent = (struct sctp_ulpevent *) pos->cb;
  689. csid = cevent->stream;
  690. cssn = cevent->ssn;
  691. if (cssn != sctp_ssn_peek(in, csid))
  692. break;
  693. /* Found it, so mark in the ssnmap. */
  694. sctp_ssn_next(in, csid);
  695. __skb_unlink(pos, &ulpq->lobby);
  696. if (!event) {
  697. /* Create a temporary list to collect chunks on. */
  698. event = sctp_skb2event(pos);
  699. __skb_queue_tail(&temp, sctp_event2skb(event));
  700. } else {
  701. /* Attach all gathered skbs to the event. */
  702. __skb_queue_tail(&temp, pos);
  703. }
  704. }
  705. /* Send event to the ULP. 'event' is the sctp_ulpevent for
  706. * very first SKB on the 'temp' list.
  707. */
  708. if (event)
  709. sctp_ulpq_tail_event(ulpq, event);
  710. }
  711. /* Skip over an SSN. */
  712. void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
  713. {
  714. struct sctp_stream *in;
  715. /* Note: The stream ID must be verified before this routine. */
  716. in = &ulpq->asoc->ssnmap->in;
  717. /* Is this an old SSN? If so ignore. */
  718. if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
  719. return;
  720. /* Mark that we are no longer expecting this SSN or lower. */
  721. sctp_ssn_skip(in, sid, ssn);
  722. /* Go find any other chunks that were waiting for
  723. * ordering and deliver them if needed.
  724. */
  725. sctp_ulpq_reap_ordered(ulpq);
  726. return;
  727. }
  728. /* Renege 'needed' bytes from the ordering queue. */
  729. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  730. {
  731. __u16 freed = 0;
  732. __u32 tsn;
  733. struct sk_buff *skb;
  734. struct sctp_ulpevent *event;
  735. struct sctp_tsnmap *tsnmap;
  736. tsnmap = &ulpq->asoc->peer.tsn_map;
  737. while ((skb = __skb_dequeue_tail(&ulpq->lobby)) != NULL) {
  738. freed += skb_headlen(skb);
  739. event = sctp_skb2event(skb);
  740. tsn = event->tsn;
  741. sctp_ulpevent_free(event);
  742. sctp_tsnmap_renege(tsnmap, tsn);
  743. if (freed >= needed)
  744. return freed;
  745. }
  746. return freed;
  747. }
  748. /* Renege 'needed' bytes from the reassembly queue. */
  749. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  750. {
  751. __u16 freed = 0;
  752. __u32 tsn;
  753. struct sk_buff *skb;
  754. struct sctp_ulpevent *event;
  755. struct sctp_tsnmap *tsnmap;
  756. tsnmap = &ulpq->asoc->peer.tsn_map;
  757. /* Walk backwards through the list, reneges the newest tsns. */
  758. while ((skb = __skb_dequeue_tail(&ulpq->reasm)) != NULL) {
  759. freed += skb_headlen(skb);
  760. event = sctp_skb2event(skb);
  761. tsn = event->tsn;
  762. sctp_ulpevent_free(event);
  763. sctp_tsnmap_renege(tsnmap, tsn);
  764. if (freed >= needed)
  765. return freed;
  766. }
  767. return freed;
  768. }
  769. /* Partial deliver the first message as there is pressure on rwnd. */
  770. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  771. struct sctp_chunk *chunk,
  772. gfp_t gfp)
  773. {
  774. struct sctp_ulpevent *event;
  775. struct sctp_association *asoc;
  776. struct sctp_sock *sp;
  777. asoc = ulpq->asoc;
  778. sp = sctp_sk(asoc->base.sk);
  779. /* If the association is already in Partial Delivery mode
  780. * we have noting to do.
  781. */
  782. if (ulpq->pd_mode)
  783. return;
  784. /* If the user enabled fragment interleave socket option,
  785. * multiple associations can enter partial delivery.
  786. * Otherwise, we can only enter partial delivery if the
  787. * socket is not in partial deliver mode.
  788. */
  789. if (sp->frag_interleave || atomic_read(&sp->pd_mode) == 0) {
  790. /* Is partial delivery possible? */
  791. event = sctp_ulpq_retrieve_first(ulpq);
  792. /* Send event to the ULP. */
  793. if (event) {
  794. sctp_ulpq_tail_event(ulpq, event);
  795. sctp_ulpq_set_pd(ulpq);
  796. return;
  797. }
  798. }
  799. }
  800. /* Renege some packets to make room for an incoming chunk. */
  801. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  802. gfp_t gfp)
  803. {
  804. struct sctp_association *asoc;
  805. __u16 needed, freed;
  806. asoc = ulpq->asoc;
  807. if (chunk) {
  808. needed = ntohs(chunk->chunk_hdr->length);
  809. needed -= sizeof(sctp_data_chunk_t);
  810. } else
  811. needed = SCTP_DEFAULT_MAXWINDOW;
  812. freed = 0;
  813. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  814. freed = sctp_ulpq_renege_order(ulpq, needed);
  815. if (freed < needed) {
  816. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  817. }
  818. }
  819. /* If able to free enough room, accept this chunk. */
  820. if (chunk && (freed >= needed)) {
  821. __u32 tsn;
  822. tsn = ntohl(chunk->subh.data_hdr->tsn);
  823. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  824. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  825. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  826. }
  827. return;
  828. }
  829. /* Notify the application if an association is aborted and in
  830. * partial delivery mode. Send up any pending received messages.
  831. */
  832. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  833. {
  834. struct sctp_ulpevent *ev = NULL;
  835. struct sock *sk;
  836. if (!ulpq->pd_mode)
  837. return;
  838. sk = ulpq->asoc->base.sk;
  839. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  840. &sctp_sk(sk)->subscribe))
  841. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  842. SCTP_PARTIAL_DELIVERY_ABORTED,
  843. gfp);
  844. if (ev)
  845. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  846. /* If there is data waiting, send it up the socket now. */
  847. if (sctp_ulpq_clear_pd(ulpq) || ev)
  848. sk->sk_data_ready(sk, 0);
  849. }