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