ulpqueue.c 29 KB

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