ulpqueue.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. }
  813. static __u16 sctp_ulpq_renege_list(struct sctp_ulpq *ulpq,
  814. struct sk_buff_head *list, __u16 needed)
  815. {
  816. __u16 freed = 0;
  817. __u32 tsn;
  818. struct sk_buff *skb;
  819. struct sctp_ulpevent *event;
  820. struct sctp_tsnmap *tsnmap;
  821. tsnmap = &ulpq->asoc->peer.tsn_map;
  822. while ((skb = __skb_dequeue_tail(list)) != NULL) {
  823. freed += skb_headlen(skb);
  824. event = sctp_skb2event(skb);
  825. tsn = event->tsn;
  826. sctp_ulpevent_free(event);
  827. sctp_tsnmap_renege(tsnmap, tsn);
  828. if (freed >= needed)
  829. return freed;
  830. }
  831. return freed;
  832. }
  833. /* Renege 'needed' bytes from the ordering queue. */
  834. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  835. {
  836. return sctp_ulpq_renege_list(ulpq, &ulpq->lobby, needed);
  837. }
  838. /* Renege 'needed' bytes from the reassembly queue. */
  839. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  840. {
  841. return sctp_ulpq_renege_list(ulpq, &ulpq->reasm, needed);
  842. }
  843. /* Partial deliver the first message as there is pressure on rwnd. */
  844. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  845. struct sctp_chunk *chunk,
  846. gfp_t gfp)
  847. {
  848. struct sctp_ulpevent *event;
  849. struct sctp_association *asoc;
  850. struct sctp_sock *sp;
  851. asoc = ulpq->asoc;
  852. sp = sctp_sk(asoc->base.sk);
  853. /* If the association is already in Partial Delivery mode
  854. * we have noting to do.
  855. */
  856. if (ulpq->pd_mode)
  857. return;
  858. /* If the user enabled fragment interleave socket option,
  859. * multiple associations can enter partial delivery.
  860. * Otherwise, we can only enter partial delivery if the
  861. * socket is not in partial deliver mode.
  862. */
  863. if (sp->frag_interleave || atomic_read(&sp->pd_mode) == 0) {
  864. /* Is partial delivery possible? */
  865. event = sctp_ulpq_retrieve_first(ulpq);
  866. /* Send event to the ULP. */
  867. if (event) {
  868. sctp_ulpq_tail_event(ulpq, event);
  869. sctp_ulpq_set_pd(ulpq);
  870. return;
  871. }
  872. }
  873. }
  874. /* Renege some packets to make room for an incoming chunk. */
  875. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  876. gfp_t gfp)
  877. {
  878. struct sctp_association *asoc;
  879. __u16 needed, freed;
  880. asoc = ulpq->asoc;
  881. if (chunk) {
  882. needed = ntohs(chunk->chunk_hdr->length);
  883. needed -= sizeof(sctp_data_chunk_t);
  884. } else
  885. needed = SCTP_DEFAULT_MAXWINDOW;
  886. freed = 0;
  887. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  888. freed = sctp_ulpq_renege_order(ulpq, needed);
  889. if (freed < needed) {
  890. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  891. }
  892. }
  893. /* If able to free enough room, accept this chunk. */
  894. if (chunk && (freed >= needed)) {
  895. __u32 tsn;
  896. tsn = ntohl(chunk->subh.data_hdr->tsn);
  897. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  898. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  899. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  900. }
  901. sk_mem_reclaim(asoc->base.sk);
  902. }
  903. /* Notify the application if an association is aborted and in
  904. * partial delivery mode. Send up any pending received messages.
  905. */
  906. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  907. {
  908. struct sctp_ulpevent *ev = NULL;
  909. struct sock *sk;
  910. if (!ulpq->pd_mode)
  911. return;
  912. sk = ulpq->asoc->base.sk;
  913. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  914. &sctp_sk(sk)->subscribe))
  915. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  916. SCTP_PARTIAL_DELIVERY_ABORTED,
  917. gfp);
  918. if (ev)
  919. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  920. /* If there is data waiting, send it up the socket now. */
  921. if (sctp_ulpq_clear_pd(ulpq) || ev)
  922. sk->sk_data_ready(sk, 0);
  923. }