ulpqueue.c 29 KB

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