ulpqueue.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001 Intel Corp.
  6. * Copyright (c) 2001 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This abstraction carries sctp events to the ULP (sockets).
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Jon Grimm <jgrimm@us.ibm.com>
  37. * La Monte H.P. Yarroll <piggy@acm.org>
  38. * Sridhar Samudrala <sri@us.ibm.com>
  39. *
  40. * Any bugs reported given to us we will try to fix... any fixes shared will
  41. * be incorporated into the next SCTP release.
  42. */
  43. #include <linux/types.h>
  44. #include <linux/skbuff.h>
  45. #include <net/sock.h>
  46. #include <net/sctp/structs.h>
  47. #include <net/sctp/sctp.h>
  48. #include <net/sctp/sm.h>
  49. /* Forward declarations for internal helpers. */
  50. static struct sctp_ulpevent * sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  51. struct sctp_ulpevent *);
  52. static struct sctp_ulpevent * sctp_ulpq_order(struct sctp_ulpq *,
  53. struct sctp_ulpevent *);
  54. static void sctp_ulpq_reasm_drain(struct sctp_ulpq *ulpq);
  55. /* 1st Level Abstractions */
  56. /* Initialize a ULP queue from a block of memory. */
  57. struct sctp_ulpq *sctp_ulpq_init(struct sctp_ulpq *ulpq,
  58. struct sctp_association *asoc)
  59. {
  60. memset(ulpq, 0, sizeof(struct sctp_ulpq));
  61. ulpq->asoc = asoc;
  62. skb_queue_head_init(&ulpq->reasm);
  63. skb_queue_head_init(&ulpq->lobby);
  64. ulpq->pd_mode = 0;
  65. ulpq->malloced = 0;
  66. return ulpq;
  67. }
  68. /* Flush the reassembly and ordering queues. */
  69. void sctp_ulpq_flush(struct sctp_ulpq *ulpq)
  70. {
  71. struct sk_buff *skb;
  72. struct sctp_ulpevent *event;
  73. while ((skb = __skb_dequeue(&ulpq->lobby)) != NULL) {
  74. event = sctp_skb2event(skb);
  75. sctp_ulpevent_free(event);
  76. }
  77. while ((skb = __skb_dequeue(&ulpq->reasm)) != NULL) {
  78. event = sctp_skb2event(skb);
  79. sctp_ulpevent_free(event);
  80. }
  81. }
  82. /* Dispose of a ulpqueue. */
  83. void sctp_ulpq_free(struct sctp_ulpq *ulpq)
  84. {
  85. sctp_ulpq_flush(ulpq);
  86. if (ulpq->malloced)
  87. kfree(ulpq);
  88. }
  89. /* Process an incoming DATA chunk. */
  90. int sctp_ulpq_tail_data(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  91. gfp_t gfp)
  92. {
  93. struct sk_buff_head temp;
  94. sctp_data_chunk_t *hdr;
  95. struct sctp_ulpevent *event;
  96. hdr = (sctp_data_chunk_t *) chunk->chunk_hdr;
  97. /* Create an event from the incoming chunk. */
  98. event = sctp_ulpevent_make_rcvmsg(chunk->asoc, chunk, gfp);
  99. if (!event)
  100. return -ENOMEM;
  101. /* Do reassembly if needed. */
  102. event = sctp_ulpq_reasm(ulpq, event);
  103. /* Do ordering if needed. */
  104. if ((event) && (event->msg_flags & MSG_EOR)){
  105. /* Create a temporary list to collect chunks on. */
  106. skb_queue_head_init(&temp);
  107. __skb_queue_tail(&temp, sctp_event2skb(event));
  108. event = sctp_ulpq_order(ulpq, event);
  109. }
  110. /* Send event to the ULP. 'event' is the sctp_ulpevent for
  111. * very first SKB on the 'temp' list.
  112. */
  113. if (event)
  114. sctp_ulpq_tail_event(ulpq, event);
  115. return 0;
  116. }
  117. /* Add a new event for propagation to the ULP. */
  118. /* Clear the partial delivery mode for this socket. Note: This
  119. * assumes that no association is currently in partial delivery mode.
  120. */
  121. int sctp_clear_pd(struct sock *sk, struct sctp_association *asoc)
  122. {
  123. struct sctp_sock *sp = sctp_sk(sk);
  124. if (atomic_dec_and_test(&sp->pd_mode)) {
  125. /* This means there are no other associations in PD, so
  126. * we can go ahead and clear out the lobby in one shot
  127. */
  128. if (!skb_queue_empty(&sp->pd_lobby)) {
  129. struct list_head *list;
  130. sctp_skb_list_tail(&sp->pd_lobby, &sk->sk_receive_queue);
  131. list = (struct list_head *)&sctp_sk(sk)->pd_lobby;
  132. INIT_LIST_HEAD(list);
  133. return 1;
  134. }
  135. } else {
  136. /* There are other associations in PD, so we only need to
  137. * pull stuff out of the lobby that belongs to the
  138. * associations that is exiting PD (all of its notifications
  139. * are posted here).
  140. */
  141. if (!skb_queue_empty(&sp->pd_lobby) && asoc) {
  142. struct sk_buff *skb, *tmp;
  143. struct sctp_ulpevent *event;
  144. sctp_skb_for_each(skb, &sp->pd_lobby, tmp) {
  145. event = sctp_skb2event(skb);
  146. if (event->asoc == asoc) {
  147. __skb_unlink(skb, &sp->pd_lobby);
  148. __skb_queue_tail(&sk->sk_receive_queue,
  149. skb);
  150. }
  151. }
  152. }
  153. }
  154. return 0;
  155. }
  156. /* Set the pd_mode on the socket and ulpq */
  157. static void sctp_ulpq_set_pd(struct sctp_ulpq *ulpq)
  158. {
  159. struct sctp_sock *sp = sctp_sk(ulpq->asoc->base.sk);
  160. atomic_inc(&sp->pd_mode);
  161. ulpq->pd_mode = 1;
  162. }
  163. /* Clear the pd_mode and restart any pending messages waiting for delivery. */
  164. static int sctp_ulpq_clear_pd(struct sctp_ulpq *ulpq)
  165. {
  166. ulpq->pd_mode = 0;
  167. sctp_ulpq_reasm_drain(ulpq);
  168. return sctp_clear_pd(ulpq->asoc->base.sk, ulpq->asoc);
  169. }
  170. /* If the SKB of 'event' is on a list, it is the first such member
  171. * of that list.
  172. */
  173. int sctp_ulpq_tail_event(struct sctp_ulpq *ulpq, struct sctp_ulpevent *event)
  174. {
  175. struct sock *sk = ulpq->asoc->base.sk;
  176. struct sk_buff_head *queue, *skb_list;
  177. struct sk_buff *skb = sctp_event2skb(event);
  178. int clear_pd = 0;
  179. skb_list = (struct sk_buff_head *) skb->prev;
  180. /* If the socket is just going to throw this away, do not
  181. * even try to deliver it.
  182. */
  183. if (sock_flag(sk, SOCK_DEAD) || (sk->sk_shutdown & RCV_SHUTDOWN))
  184. goto out_free;
  185. /* Check if the user wishes to receive this event. */
  186. if (!sctp_ulpevent_is_enabled(event, &sctp_sk(sk)->subscribe))
  187. goto out_free;
  188. /* If we are in partial delivery mode, post to the lobby until
  189. * partial delivery is cleared, unless, of course _this_ is
  190. * the association the cause of the partial delivery.
  191. */
  192. if (atomic_read(&sctp_sk(sk)->pd_mode) == 0) {
  193. queue = &sk->sk_receive_queue;
  194. } else {
  195. if (ulpq->pd_mode) {
  196. /* If the association is in partial delivery, we
  197. * need to finish delivering the partially processed
  198. * packet before passing any other data. This is
  199. * because we don't truly support stream interleaving.
  200. */
  201. if ((event->msg_flags & MSG_NOTIFICATION) ||
  202. (SCTP_DATA_NOT_FRAG ==
  203. (event->msg_flags & SCTP_DATA_FRAG_MASK)))
  204. queue = &sctp_sk(sk)->pd_lobby;
  205. else {
  206. clear_pd = event->msg_flags & MSG_EOR;
  207. queue = &sk->sk_receive_queue;
  208. }
  209. } else {
  210. /*
  211. * If fragment interleave is enabled, we
  212. * can queue this to the recieve queue instead
  213. * of the lobby.
  214. */
  215. if (sctp_sk(sk)->frag_interleave)
  216. queue = &sk->sk_receive_queue;
  217. else
  218. queue = &sctp_sk(sk)->pd_lobby;
  219. }
  220. }
  221. /* If we are harvesting multiple skbs they will be
  222. * collected on a list.
  223. */
  224. if (skb_list)
  225. sctp_skb_list_tail(skb_list, queue);
  226. else
  227. __skb_queue_tail(queue, skb);
  228. /* Did we just complete partial delivery and need to get
  229. * rolling again? Move pending data to the receive
  230. * queue.
  231. */
  232. if (clear_pd)
  233. sctp_ulpq_clear_pd(ulpq);
  234. if (queue == &sk->sk_receive_queue)
  235. sk->sk_data_ready(sk, 0);
  236. return 1;
  237. out_free:
  238. if (skb_list)
  239. sctp_queue_purge_ulpevents(skb_list);
  240. else
  241. sctp_ulpevent_free(event);
  242. return 0;
  243. }
  244. /* 2nd Level Abstractions */
  245. /* Helper function to store chunks that need to be reassembled. */
  246. static void sctp_ulpq_store_reasm(struct sctp_ulpq *ulpq,
  247. struct sctp_ulpevent *event)
  248. {
  249. struct sk_buff *pos;
  250. struct sctp_ulpevent *cevent;
  251. __u32 tsn, ctsn;
  252. tsn = event->tsn;
  253. /* See if it belongs at the end. */
  254. pos = skb_peek_tail(&ulpq->reasm);
  255. if (!pos) {
  256. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  257. return;
  258. }
  259. /* Short circuit just dropping it at the end. */
  260. cevent = sctp_skb2event(pos);
  261. ctsn = cevent->tsn;
  262. if (TSN_lt(ctsn, tsn)) {
  263. __skb_queue_tail(&ulpq->reasm, sctp_event2skb(event));
  264. return;
  265. }
  266. /* Find the right place in this list. We store them by TSN. */
  267. skb_queue_walk(&ulpq->reasm, pos) {
  268. cevent = sctp_skb2event(pos);
  269. ctsn = cevent->tsn;
  270. if (TSN_lt(tsn, ctsn))
  271. break;
  272. }
  273. /* Insert before pos. */
  274. __skb_queue_before(&ulpq->reasm, pos, sctp_event2skb(event));
  275. }
  276. /* Helper function to return an event corresponding to the reassembled
  277. * datagram.
  278. * This routine creates a re-assembled skb given the first and last skb's
  279. * as stored in the reassembly queue. The skb's may be non-linear if the sctp
  280. * payload was fragmented on the way and ip had to reassemble them.
  281. * We add the rest of skb's to the first skb's fraglist.
  282. */
  283. static struct sctp_ulpevent *sctp_make_reassembled_event(struct sk_buff_head *queue, struct sk_buff *f_frag, struct sk_buff *l_frag)
  284. {
  285. struct sk_buff *pos;
  286. struct sk_buff *new = NULL;
  287. struct sctp_ulpevent *event;
  288. struct sk_buff *pnext, *last;
  289. struct sk_buff *list = skb_shinfo(f_frag)->frag_list;
  290. /* Store the pointer to the 2nd skb */
  291. if (f_frag == l_frag)
  292. pos = NULL;
  293. else
  294. pos = f_frag->next;
  295. /* Get the last skb in the f_frag's frag_list if present. */
  296. for (last = list; list; last = list, list = list->next);
  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(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(&ulpq->reasm,
  427. pd_first,
  428. pd_last);
  429. if (retval)
  430. sctp_ulpq_set_pd(ulpq);
  431. }
  432. }
  433. done:
  434. return retval;
  435. found:
  436. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, pos);
  437. if (retval)
  438. retval->msg_flags |= MSG_EOR;
  439. goto done;
  440. }
  441. /* Retrieve the next set of fragments of a partial message. */
  442. static struct sctp_ulpevent *sctp_ulpq_retrieve_partial(struct sctp_ulpq *ulpq)
  443. {
  444. struct sk_buff *pos, *last_frag, *first_frag;
  445. struct sctp_ulpevent *cevent;
  446. __u32 ctsn, next_tsn;
  447. int is_last;
  448. struct sctp_ulpevent *retval;
  449. /* The chunks are held in the reasm queue sorted by TSN.
  450. * Walk through the queue sequentially and look for the first
  451. * sequence of fragmented chunks.
  452. */
  453. if (skb_queue_empty(&ulpq->reasm))
  454. return NULL;
  455. last_frag = first_frag = NULL;
  456. retval = NULL;
  457. next_tsn = 0;
  458. is_last = 0;
  459. skb_queue_walk(&ulpq->reasm, pos) {
  460. cevent = sctp_skb2event(pos);
  461. ctsn = cevent->tsn;
  462. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  463. case SCTP_DATA_MIDDLE_FRAG:
  464. if (!first_frag) {
  465. first_frag = pos;
  466. next_tsn = ctsn + 1;
  467. last_frag = pos;
  468. } else if (next_tsn == ctsn)
  469. next_tsn++;
  470. else
  471. goto done;
  472. break;
  473. case SCTP_DATA_LAST_FRAG:
  474. if (!first_frag)
  475. first_frag = pos;
  476. else if (ctsn != next_tsn)
  477. goto done;
  478. last_frag = pos;
  479. is_last = 1;
  480. goto done;
  481. default:
  482. return NULL;
  483. }
  484. }
  485. /* We have the reassembled event. There is no need to look
  486. * further.
  487. */
  488. done:
  489. retval = sctp_make_reassembled_event(&ulpq->reasm, first_frag, last_frag);
  490. if (retval && is_last)
  491. retval->msg_flags |= MSG_EOR;
  492. return retval;
  493. }
  494. /* Helper function to reassemble chunks. Hold chunks on the reasm queue that
  495. * need reassembling.
  496. */
  497. static struct sctp_ulpevent *sctp_ulpq_reasm(struct sctp_ulpq *ulpq,
  498. struct sctp_ulpevent *event)
  499. {
  500. struct sctp_ulpevent *retval = NULL;
  501. /* Check if this is part of a fragmented message. */
  502. if (SCTP_DATA_NOT_FRAG == (event->msg_flags & SCTP_DATA_FRAG_MASK)) {
  503. event->msg_flags |= MSG_EOR;
  504. return event;
  505. }
  506. sctp_ulpq_store_reasm(ulpq, event);
  507. if (!ulpq->pd_mode)
  508. retval = sctp_ulpq_retrieve_reassembled(ulpq);
  509. else {
  510. __u32 ctsn, ctsnap;
  511. /* Do not even bother unless this is the next tsn to
  512. * be delivered.
  513. */
  514. ctsn = event->tsn;
  515. ctsnap = sctp_tsnmap_get_ctsn(&ulpq->asoc->peer.tsn_map);
  516. if (TSN_lte(ctsn, ctsnap))
  517. retval = sctp_ulpq_retrieve_partial(ulpq);
  518. }
  519. return retval;
  520. }
  521. /* Retrieve the first part (sequential fragments) for partial delivery. */
  522. static struct sctp_ulpevent *sctp_ulpq_retrieve_first(struct sctp_ulpq *ulpq)
  523. {
  524. struct sk_buff *pos, *last_frag, *first_frag;
  525. struct sctp_ulpevent *cevent;
  526. __u32 ctsn, next_tsn;
  527. struct sctp_ulpevent *retval;
  528. /* The chunks are held in the reasm queue sorted by TSN.
  529. * Walk through the queue sequentially and look for a sequence of
  530. * fragmented chunks that start a datagram.
  531. */
  532. if (skb_queue_empty(&ulpq->reasm))
  533. return NULL;
  534. last_frag = first_frag = NULL;
  535. retval = NULL;
  536. next_tsn = 0;
  537. skb_queue_walk(&ulpq->reasm, pos) {
  538. cevent = sctp_skb2event(pos);
  539. ctsn = cevent->tsn;
  540. switch (cevent->msg_flags & SCTP_DATA_FRAG_MASK) {
  541. case SCTP_DATA_FIRST_FRAG:
  542. if (!first_frag) {
  543. first_frag = pos;
  544. next_tsn = ctsn + 1;
  545. last_frag = pos;
  546. } else
  547. goto done;
  548. break;
  549. case SCTP_DATA_MIDDLE_FRAG:
  550. if (!first_frag)
  551. return NULL;
  552. if (ctsn == next_tsn) {
  553. next_tsn++;
  554. last_frag = pos;
  555. } else
  556. goto done;
  557. break;
  558. default:
  559. return NULL;
  560. }
  561. }
  562. /* We have the reassembled event. There is no need to look
  563. * further.
  564. */
  565. done:
  566. retval = sctp_make_reassembled_event(&ulpq->reasm, 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. struct sk_buff_head temp;
  614. if (skb_queue_empty(&ulpq->reasm))
  615. return;
  616. while ((event = sctp_ulpq_retrieve_reassembled(ulpq)) != NULL) {
  617. /* Do ordering if needed. */
  618. if ((event) && (event->msg_flags & MSG_EOR)){
  619. skb_queue_head_init(&temp);
  620. __skb_queue_tail(&temp, sctp_event2skb(event));
  621. event = sctp_ulpq_order(ulpq, event);
  622. }
  623. /* Send event to the ULP. 'event' is the
  624. * sctp_ulpevent for very first SKB on the temp' list.
  625. */
  626. if (event)
  627. sctp_ulpq_tail_event(ulpq, event);
  628. }
  629. }
  630. /* Helper function to gather skbs that have possibly become
  631. * ordered by an an incoming chunk.
  632. */
  633. static void sctp_ulpq_retrieve_ordered(struct sctp_ulpq *ulpq,
  634. struct sctp_ulpevent *event)
  635. {
  636. struct sk_buff_head *event_list;
  637. struct sk_buff *pos, *tmp;
  638. struct sctp_ulpevent *cevent;
  639. struct sctp_stream *in;
  640. __u16 sid, csid;
  641. __u16 ssn, cssn;
  642. sid = event->stream;
  643. ssn = event->ssn;
  644. in = &ulpq->asoc->ssnmap->in;
  645. event_list = (struct sk_buff_head *) sctp_event2skb(event)->prev;
  646. /* We are holding the chunks by stream, by SSN. */
  647. sctp_skb_for_each(pos, &ulpq->lobby, tmp) {
  648. cevent = (struct sctp_ulpevent *) pos->cb;
  649. csid = cevent->stream;
  650. cssn = cevent->ssn;
  651. /* Have we gone too far? */
  652. if (csid > sid)
  653. break;
  654. /* Have we not gone far enough? */
  655. if (csid < sid)
  656. continue;
  657. if (cssn != sctp_ssn_peek(in, sid))
  658. break;
  659. /* Found it, so mark in the ssnmap. */
  660. sctp_ssn_next(in, sid);
  661. __skb_unlink(pos, &ulpq->lobby);
  662. /* Attach all gathered skbs to the event. */
  663. __skb_queue_tail(event_list, pos);
  664. }
  665. }
  666. /* Helper function to store chunks needing ordering. */
  667. static void sctp_ulpq_store_ordered(struct sctp_ulpq *ulpq,
  668. struct sctp_ulpevent *event)
  669. {
  670. struct sk_buff *pos;
  671. struct sctp_ulpevent *cevent;
  672. __u16 sid, csid;
  673. __u16 ssn, cssn;
  674. pos = skb_peek_tail(&ulpq->lobby);
  675. if (!pos) {
  676. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  677. return;
  678. }
  679. sid = event->stream;
  680. ssn = event->ssn;
  681. cevent = (struct sctp_ulpevent *) pos->cb;
  682. csid = cevent->stream;
  683. cssn = cevent->ssn;
  684. if (sid > csid) {
  685. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  686. return;
  687. }
  688. if ((sid == csid) && SSN_lt(cssn, ssn)) {
  689. __skb_queue_tail(&ulpq->lobby, sctp_event2skb(event));
  690. return;
  691. }
  692. /* Find the right place in this list. We store them by
  693. * stream ID and then by SSN.
  694. */
  695. skb_queue_walk(&ulpq->lobby, pos) {
  696. cevent = (struct sctp_ulpevent *) pos->cb;
  697. csid = cevent->stream;
  698. cssn = cevent->ssn;
  699. if (csid > sid)
  700. break;
  701. if (csid == sid && SSN_lt(ssn, cssn))
  702. break;
  703. }
  704. /* Insert before pos. */
  705. __skb_queue_before(&ulpq->lobby, pos, sctp_event2skb(event));
  706. }
  707. static struct sctp_ulpevent *sctp_ulpq_order(struct sctp_ulpq *ulpq,
  708. struct sctp_ulpevent *event)
  709. {
  710. __u16 sid, ssn;
  711. struct sctp_stream *in;
  712. /* Check if this message needs ordering. */
  713. if (SCTP_DATA_UNORDERED & event->msg_flags)
  714. return event;
  715. /* Note: The stream ID must be verified before this routine. */
  716. sid = event->stream;
  717. ssn = event->ssn;
  718. in = &ulpq->asoc->ssnmap->in;
  719. /* Is this the expected SSN for this stream ID? */
  720. if (ssn != sctp_ssn_peek(in, sid)) {
  721. /* We've received something out of order, so find where it
  722. * needs to be placed. We order by stream and then by SSN.
  723. */
  724. sctp_ulpq_store_ordered(ulpq, event);
  725. return NULL;
  726. }
  727. /* Mark that the next chunk has been found. */
  728. sctp_ssn_next(in, sid);
  729. /* Go find any other chunks that were waiting for
  730. * ordering.
  731. */
  732. sctp_ulpq_retrieve_ordered(ulpq, event);
  733. return event;
  734. }
  735. /* Helper function to gather skbs that have possibly become
  736. * ordered by forward tsn skipping their dependencies.
  737. */
  738. static void sctp_ulpq_reap_ordered(struct sctp_ulpq *ulpq, __u16 sid)
  739. {
  740. struct sk_buff *pos, *tmp;
  741. struct sctp_ulpevent *cevent;
  742. struct sctp_ulpevent *event;
  743. struct sctp_stream *in;
  744. struct sk_buff_head temp;
  745. struct sk_buff_head *lobby = &ulpq->lobby;
  746. __u16 csid, cssn;
  747. in = &ulpq->asoc->ssnmap->in;
  748. /* We are holding the chunks by stream, by SSN. */
  749. skb_queue_head_init(&temp);
  750. event = NULL;
  751. sctp_skb_for_each(pos, lobby, tmp) {
  752. cevent = (struct sctp_ulpevent *) pos->cb;
  753. csid = cevent->stream;
  754. cssn = cevent->ssn;
  755. /* Have we gone too far? */
  756. if (csid > sid)
  757. break;
  758. /* Have we not gone far enough? */
  759. if (csid < sid)
  760. continue;
  761. /* see if this ssn has been marked by skipping */
  762. if (!SSN_lt(cssn, sctp_ssn_peek(in, csid)))
  763. break;
  764. __skb_unlink(pos, lobby);
  765. if (!event)
  766. /* Create a temporary list to collect chunks on. */
  767. event = sctp_skb2event(pos);
  768. /* Attach all gathered skbs to the event. */
  769. __skb_queue_tail(&temp, pos);
  770. }
  771. /* If we didn't reap any data, see if the next expected SSN
  772. * is next on the queue and if so, use that.
  773. */
  774. if (event == NULL && pos != (struct sk_buff *)lobby) {
  775. cevent = (struct sctp_ulpevent *) pos->cb;
  776. csid = cevent->stream;
  777. cssn = cevent->ssn;
  778. if (csid == sid && cssn == sctp_ssn_peek(in, csid)) {
  779. sctp_ssn_next(in, csid);
  780. __skb_unlink(pos, lobby);
  781. __skb_queue_tail(&temp, pos);
  782. event = sctp_skb2event(pos);
  783. }
  784. }
  785. /* Send event to the ULP. 'event' is the sctp_ulpevent for
  786. * very first SKB on the 'temp' list.
  787. */
  788. if (event) {
  789. /* see if we have more ordered that we can deliver */
  790. sctp_ulpq_retrieve_ordered(ulpq, event);
  791. sctp_ulpq_tail_event(ulpq, event);
  792. }
  793. }
  794. /* Skip over an SSN. This is used during the processing of
  795. * Forwared TSN chunk to skip over the abandoned ordered data
  796. */
  797. void sctp_ulpq_skip(struct sctp_ulpq *ulpq, __u16 sid, __u16 ssn)
  798. {
  799. struct sctp_stream *in;
  800. /* Note: The stream ID must be verified before this routine. */
  801. in = &ulpq->asoc->ssnmap->in;
  802. /* Is this an old SSN? If so ignore. */
  803. if (SSN_lt(ssn, sctp_ssn_peek(in, sid)))
  804. return;
  805. /* Mark that we are no longer expecting this SSN or lower. */
  806. sctp_ssn_skip(in, sid, ssn);
  807. /* Go find any other chunks that were waiting for
  808. * ordering and deliver them if needed.
  809. */
  810. sctp_ulpq_reap_ordered(ulpq, sid);
  811. return;
  812. }
  813. static __u16 sctp_ulpq_renege_list(struct sctp_ulpq *ulpq,
  814. struct sk_buff_head *list, __u16 needed)
  815. {
  816. __u16 freed = 0;
  817. __u32 tsn;
  818. struct sk_buff *skb;
  819. struct sctp_ulpevent *event;
  820. struct sctp_tsnmap *tsnmap;
  821. tsnmap = &ulpq->asoc->peer.tsn_map;
  822. while ((skb = __skb_dequeue_tail(list)) != NULL) {
  823. freed += skb_headlen(skb);
  824. event = sctp_skb2event(skb);
  825. tsn = event->tsn;
  826. sctp_ulpevent_free(event);
  827. sctp_tsnmap_renege(tsnmap, tsn);
  828. if (freed >= needed)
  829. return freed;
  830. }
  831. return freed;
  832. }
  833. /* Renege 'needed' bytes from the ordering queue. */
  834. static __u16 sctp_ulpq_renege_order(struct sctp_ulpq *ulpq, __u16 needed)
  835. {
  836. return sctp_ulpq_renege_list(ulpq, &ulpq->lobby, needed);
  837. }
  838. /* Renege 'needed' bytes from the reassembly queue. */
  839. static __u16 sctp_ulpq_renege_frags(struct sctp_ulpq *ulpq, __u16 needed)
  840. {
  841. return sctp_ulpq_renege_list(ulpq, &ulpq->reasm, needed);
  842. }
  843. /* Partial deliver the first message as there is pressure on rwnd. */
  844. void sctp_ulpq_partial_delivery(struct sctp_ulpq *ulpq,
  845. struct sctp_chunk *chunk,
  846. gfp_t gfp)
  847. {
  848. struct sctp_ulpevent *event;
  849. struct sctp_association *asoc;
  850. struct sctp_sock *sp;
  851. asoc = ulpq->asoc;
  852. sp = sctp_sk(asoc->base.sk);
  853. /* If the association is already in Partial Delivery mode
  854. * we have noting to do.
  855. */
  856. if (ulpq->pd_mode)
  857. return;
  858. /* If the user enabled fragment interleave socket option,
  859. * multiple associations can enter partial delivery.
  860. * Otherwise, we can only enter partial delivery if the
  861. * socket is not in partial deliver mode.
  862. */
  863. if (sp->frag_interleave || atomic_read(&sp->pd_mode) == 0) {
  864. /* Is partial delivery possible? */
  865. event = sctp_ulpq_retrieve_first(ulpq);
  866. /* Send event to the ULP. */
  867. if (event) {
  868. sctp_ulpq_tail_event(ulpq, event);
  869. sctp_ulpq_set_pd(ulpq);
  870. return;
  871. }
  872. }
  873. }
  874. /* Renege some packets to make room for an incoming chunk. */
  875. void sctp_ulpq_renege(struct sctp_ulpq *ulpq, struct sctp_chunk *chunk,
  876. gfp_t gfp)
  877. {
  878. struct sctp_association *asoc;
  879. __u16 needed, freed;
  880. asoc = ulpq->asoc;
  881. if (chunk) {
  882. needed = ntohs(chunk->chunk_hdr->length);
  883. needed -= sizeof(sctp_data_chunk_t);
  884. } else
  885. needed = SCTP_DEFAULT_MAXWINDOW;
  886. freed = 0;
  887. if (skb_queue_empty(&asoc->base.sk->sk_receive_queue)) {
  888. freed = sctp_ulpq_renege_order(ulpq, needed);
  889. if (freed < needed) {
  890. freed += sctp_ulpq_renege_frags(ulpq, needed - freed);
  891. }
  892. }
  893. /* If able to free enough room, accept this chunk. */
  894. if (chunk && (freed >= needed)) {
  895. __u32 tsn;
  896. tsn = ntohl(chunk->subh.data_hdr->tsn);
  897. sctp_tsnmap_mark(&asoc->peer.tsn_map, tsn);
  898. sctp_ulpq_tail_data(ulpq, chunk, gfp);
  899. sctp_ulpq_partial_delivery(ulpq, chunk, gfp);
  900. }
  901. sk_mem_reclaim(asoc->base.sk);
  902. return;
  903. }
  904. /* Notify the application if an association is aborted and in
  905. * partial delivery mode. Send up any pending received messages.
  906. */
  907. void sctp_ulpq_abort_pd(struct sctp_ulpq *ulpq, gfp_t gfp)
  908. {
  909. struct sctp_ulpevent *ev = NULL;
  910. struct sock *sk;
  911. if (!ulpq->pd_mode)
  912. return;
  913. sk = ulpq->asoc->base.sk;
  914. if (sctp_ulpevent_type_enabled(SCTP_PARTIAL_DELIVERY_EVENT,
  915. &sctp_sk(sk)->subscribe))
  916. ev = sctp_ulpevent_make_pdapi(ulpq->asoc,
  917. SCTP_PARTIAL_DELIVERY_ABORTED,
  918. gfp);
  919. if (ev)
  920. __skb_queue_tail(&sk->sk_receive_queue, sctp_event2skb(ev));
  921. /* If there is data waiting, send it up the socket now. */
  922. if (sctp_ulpq_clear_pd(ulpq) || ev)
  923. sk->sk_data_ready(sk, 0);
  924. }