ulpqueue.c 29 KB

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