ulpqueue.c 29 KB

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