ulpqueue.c 29 KB

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