ulpqueue.c 29 KB

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