ulpqueue.c 29 KB

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