2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_indata.c 363440 2020-07-23 01:35:24Z tuexen $");
40 #include <netinet/sctp_os.h>
41 #if defined(__FreeBSD__) && !defined(__Userspace__)
44 #include <netinet/sctp_var.h>
45 #include <netinet/sctp_sysctl.h>
46 #include <netinet/sctp_header.h>
47 #include <netinet/sctp_pcb.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_output.h>
50 #include <netinet/sctp_uio.h>
51 #include <netinet/sctp_auth.h>
52 #include <netinet/sctp_timer.h>
53 #include <netinet/sctp_asconf.h>
54 #include <netinet/sctp_indata.h>
55 #include <netinet/sctp_bsd_addr.h>
56 #include <netinet/sctp_input.h>
57 #include <netinet/sctp_crc32.h>
58 #if defined(__FreeBSD__) && !defined(__Userspace__)
59 #include <netinet/sctp_lock_bsd.h>
62 * NOTES: On the outbound side of things I need to check the sack timer to
63 * see if I should generate a sack into the chunk queue (if I have data to
64 * send that is and will be sending it .. for bundling.
66 * The callback in sctp_usrreq.c will get called when the socket is read from.
67 * This will cause sctp_service_queues() to get called on the top entry in
71 sctp_add_chk_to_control(struct sctp_queued_to_read *control,
72 struct sctp_stream_in *strm,
73 struct sctp_tcb *stcb,
74 struct sctp_association *asoc,
75 struct sctp_tmit_chunk *chk, int hold_rlock);
79 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
81 asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
84 /* Calculate what the rwnd would be */
86 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
91 * This is really set wrong with respect to a 1-2-m socket. Since
92 * the sb_cc is the count that everyone as put up. When we re-write
93 * sctp_soreceive then we will fix this so that ONLY this
94 * associations data is taken into account.
96 if (stcb->sctp_socket == NULL) {
100 KASSERT(asoc->cnt_on_reasm_queue > 0 || asoc->size_on_reasm_queue == 0,
101 ("size_on_reasm_queue is %u", asoc->size_on_reasm_queue));
102 KASSERT(asoc->cnt_on_all_streams > 0 || asoc->size_on_all_streams == 0,
103 ("size_on_all_streams is %u", asoc->size_on_all_streams));
104 if (stcb->asoc.sb_cc == 0 &&
105 asoc->cnt_on_reasm_queue == 0 &&
106 asoc->cnt_on_all_streams == 0) {
107 /* Full rwnd granted */
108 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
111 /* get actual space */
112 calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
114 * take out what has NOT been put on socket queue and we yet hold
117 calc = sctp_sbspace_sub(calc, (uint32_t)(asoc->size_on_reasm_queue +
118 asoc->cnt_on_reasm_queue * MSIZE));
119 calc = sctp_sbspace_sub(calc, (uint32_t)(asoc->size_on_all_streams +
120 asoc->cnt_on_all_streams * MSIZE));
126 /* what is the overhead of all these rwnd's */
127 calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
128 /* If the window gets too small due to ctrl-stuff, reduce it
129 * to 1, even it is 0. SWS engaged
131 if (calc < stcb->asoc.my_rwnd_control_len) {
140 * Build out our readq entry based on the incoming packet.
142 struct sctp_queued_to_read *
143 sctp_build_readq_entry(struct sctp_tcb *stcb,
144 struct sctp_nets *net,
145 uint32_t tsn, uint32_t ppid,
146 uint32_t context, uint16_t sid,
147 uint32_t mid, uint8_t flags,
150 struct sctp_queued_to_read *read_queue_e = NULL;
152 sctp_alloc_a_readq(stcb, read_queue_e);
153 if (read_queue_e == NULL) {
156 memset(read_queue_e, 0, sizeof(struct sctp_queued_to_read));
157 read_queue_e->sinfo_stream = sid;
158 read_queue_e->sinfo_flags = (flags << 8);
159 read_queue_e->sinfo_ppid = ppid;
160 read_queue_e->sinfo_context = context;
161 read_queue_e->sinfo_tsn = tsn;
162 read_queue_e->sinfo_cumtsn = tsn;
163 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
164 read_queue_e->mid = mid;
165 read_queue_e->top_fsn = read_queue_e->fsn_included = 0xffffffff;
166 TAILQ_INIT(&read_queue_e->reasm);
167 read_queue_e->whoFrom = net;
168 atomic_add_int(&net->ref_count, 1);
169 read_queue_e->data = dm;
170 read_queue_e->stcb = stcb;
171 read_queue_e->port_from = stcb->rport;
172 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
173 read_queue_e->do_not_ref_stcb = 1;
176 return (read_queue_e);
180 sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
182 struct sctp_extrcvinfo *seinfo;
183 struct sctp_sndrcvinfo *outinfo;
184 struct sctp_rcvinfo *rcvinfo;
185 struct sctp_nxtinfo *nxtinfo;
196 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
197 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
198 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
199 /* user does not want any ancillary data */
204 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
205 len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
207 seinfo = (struct sctp_extrcvinfo *)sinfo;
208 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
209 (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
211 len += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
215 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
216 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
218 len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
221 len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
227 ret = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
232 SCTP_BUF_LEN(ret) = 0;
234 /* We need a CMSG header followed by the struct */
236 cmh = mtod(ret, WSACMSGHDR *);
238 cmh = mtod(ret, struct cmsghdr *);
241 * Make sure that there is no un-initialized padding between
242 * the cmsg header and cmsg data and after the cmsg data.
245 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
246 cmh->cmsg_level = IPPROTO_SCTP;
247 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
248 cmh->cmsg_type = SCTP_RCVINFO;
249 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
250 rcvinfo->rcv_sid = sinfo->sinfo_stream;
251 rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
252 rcvinfo->rcv_flags = sinfo->sinfo_flags;
253 rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
254 rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
255 rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
256 rcvinfo->rcv_context = sinfo->sinfo_context;
257 rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
259 cmh = (WSACMSGHDR *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
261 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
263 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
266 cmh->cmsg_level = IPPROTO_SCTP;
267 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
268 cmh->cmsg_type = SCTP_NXTINFO;
269 nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
270 nxtinfo->nxt_sid = seinfo->serinfo_next_stream;
271 nxtinfo->nxt_flags = 0;
272 if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
273 nxtinfo->nxt_flags |= SCTP_UNORDERED;
275 if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
276 nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
278 if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
279 nxtinfo->nxt_flags |= SCTP_COMPLETE;
281 nxtinfo->nxt_ppid = seinfo->serinfo_next_ppid;
282 nxtinfo->nxt_length = seinfo->serinfo_next_length;
283 nxtinfo->nxt_assoc_id = seinfo->serinfo_next_aid;
285 cmh = (WSACMSGHDR *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
287 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
289 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
291 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
292 cmh->cmsg_level = IPPROTO_SCTP;
293 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
295 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
296 cmh->cmsg_type = SCTP_EXTRCV;
297 memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
298 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
300 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
301 cmh->cmsg_type = SCTP_SNDRCV;
303 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
311 sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
313 uint32_t gap, i, cumackp1;
316 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
319 cumackp1 = asoc->cumulative_tsn + 1;
320 if (SCTP_TSN_GT(cumackp1, tsn)) {
321 /* this tsn is behind the cum ack and thus we don't
322 * need to worry about it being moved from one to the other.
326 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
327 in_r = SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap);
328 in_nr = SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap);
329 if ((in_r == 0) && (in_nr == 0)) {
331 panic("Things are really messed up now");
333 SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
334 sctp_print_mapping_array(asoc);
338 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
340 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
341 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
342 asoc->highest_tsn_inside_nr_map = tsn;
344 if (tsn == asoc->highest_tsn_inside_map) {
345 /* We must back down to see what the new highest is */
346 for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
347 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
348 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
349 asoc->highest_tsn_inside_map = i;
355 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
361 sctp_place_control_in_stream(struct sctp_stream_in *strm,
362 struct sctp_association *asoc,
363 struct sctp_queued_to_read *control)
365 struct sctp_queued_to_read *at;
366 struct sctp_readhead *q;
367 uint8_t flags, unordered;
369 flags = (control->sinfo_flags >> 8);
370 unordered = flags & SCTP_DATA_UNORDERED;
372 q = &strm->uno_inqueue;
373 if (asoc->idata_supported == 0) {
374 if (!TAILQ_EMPTY(q)) {
375 /* Only one stream can be here in old style -- abort */
378 TAILQ_INSERT_TAIL(q, control, next_instrm);
379 control->on_strm_q = SCTP_ON_UNORDERED;
385 if ((flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
386 control->end_added = 1;
387 control->first_frag_seen = 1;
388 control->last_frag_seen = 1;
390 if (TAILQ_EMPTY(q)) {
392 TAILQ_INSERT_HEAD(q, control, next_instrm);
394 control->on_strm_q = SCTP_ON_UNORDERED;
396 control->on_strm_q = SCTP_ON_ORDERED;
400 TAILQ_FOREACH(at, q, next_instrm) {
401 if (SCTP_MID_GT(asoc->idata_supported, at->mid, control->mid)) {
403 * one in queue is bigger than the
404 * new one, insert before this one
406 TAILQ_INSERT_BEFORE(at, control, next_instrm);
408 control->on_strm_q = SCTP_ON_UNORDERED;
410 control->on_strm_q = SCTP_ON_ORDERED ;
413 } else if (SCTP_MID_EQ(asoc->idata_supported, at->mid, control->mid)) {
415 * Gak, He sent me a duplicate msg
416 * id number?? return -1 to abort.
420 if (TAILQ_NEXT(at, next_instrm) == NULL) {
422 * We are at the end, insert
425 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
426 sctp_log_strm_del(control, at,
427 SCTP_STR_LOG_FROM_INSERT_TL);
429 TAILQ_INSERT_AFTER(q, at, control, next_instrm);
431 control->on_strm_q = SCTP_ON_UNORDERED ;
433 control->on_strm_q = SCTP_ON_ORDERED ;
444 sctp_abort_in_reasm(struct sctp_tcb *stcb,
445 struct sctp_queued_to_read *control,
446 struct sctp_tmit_chunk *chk,
447 int *abort_flag, int opspot)
449 char msg[SCTP_DIAG_INFO_LEN];
452 if (stcb->asoc.idata_supported) {
453 SCTP_SNPRINTF(msg, sizeof(msg),
454 "Reass %x,CF:%x,TSN=%8.8x,SID=%4.4x,FSN=%8.8x,MID:%8.8x",
456 control->fsn_included,
459 chk->rec.data.fsn, chk->rec.data.mid);
461 SCTP_SNPRINTF(msg, sizeof(msg),
462 "Reass %x,CI:%x,TSN=%8.8x,SID=%4.4x,FSN=%4.4x,SSN:%4.4x",
464 control->fsn_included,
468 (uint16_t)chk->rec.data.mid);
470 oper = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
471 sctp_m_freem(chk->data);
473 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
474 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
475 sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
480 sctp_clean_up_control(struct sctp_tcb *stcb, struct sctp_queued_to_read *control)
483 * The control could not be placed and must be cleaned.
485 struct sctp_tmit_chunk *chk, *nchk;
486 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
487 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
489 sctp_m_freem(chk->data);
491 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
493 sctp_free_remote_addr(control->whoFrom);
495 sctp_m_freem(control->data);
496 control->data = NULL;
498 sctp_free_a_readq(stcb, control);
502 * Queue the chunk either right into the socket buffer if it is the next one
503 * to go OR put it in the correct place in the delivery queue. If we do
504 * append to the so_buf, keep doing so until we are out of order as
505 * long as the control's entered are non-fragmented.
508 sctp_queue_data_to_stream(struct sctp_tcb *stcb,
509 struct sctp_association *asoc,
510 struct sctp_queued_to_read *control, int *abort_flag, int *need_reasm)
513 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
514 * all the data in one stream this could happen quite rapidly. One
515 * could use the TSN to keep track of things, but this scheme breaks
516 * down in the other type of stream usage that could occur. Send a
517 * single msg to stream 0, send 4Billion messages to stream 1, now
518 * send a message to stream 0. You have a situation where the TSN
519 * has wrapped but not in the stream. Is this worth worrying about
520 * or should we just change our queue sort at the bottom to be by
523 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
524 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
525 * assignment this could happen... and I don't see how this would be
526 * a violation. So for now I am undecided an will leave the sort by
527 * SSN alone. Maybe a hybred approach is the answer
530 struct sctp_queued_to_read *at;
534 struct sctp_stream_in *strm;
535 char msg[SCTP_DIAG_INFO_LEN];
537 strm = &asoc->strmin[control->sinfo_stream];
538 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
539 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
541 if (SCTP_MID_GT((asoc->idata_supported), strm->last_mid_delivered, control->mid)) {
542 /* The incoming sseq is behind where we last delivered? */
543 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ: %u delivered: %u from peer, Abort association\n",
544 strm->last_mid_delivered, control->mid);
546 * throw it in the stream so it gets cleaned up in
547 * association destruction
549 TAILQ_INSERT_HEAD(&strm->inqueue, control, next_instrm);
550 if (asoc->idata_supported) {
551 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered MID=%8.8x, got TSN=%8.8x, SID=%4.4x, MID=%8.8x",
552 strm->last_mid_delivered, control->sinfo_tsn,
553 control->sinfo_stream, control->mid);
555 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
556 (uint16_t)strm->last_mid_delivered,
558 control->sinfo_stream,
559 (uint16_t)control->mid);
561 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
562 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
563 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
569 asoc->size_on_all_streams += control->length;
570 sctp_ucount_incr(asoc->cnt_on_all_streams);
571 nxt_todel = strm->last_mid_delivered + 1;
572 if (SCTP_MID_EQ(asoc->idata_supported, nxt_todel, control->mid)) {
573 #if defined(__APPLE__) && !defined(__Userspace__)
576 so = SCTP_INP_SO(stcb->sctp_ep);
577 atomic_add_int(&stcb->asoc.refcnt, 1);
578 SCTP_TCB_UNLOCK(stcb);
579 SCTP_SOCKET_LOCK(so, 1);
581 atomic_subtract_int(&stcb->asoc.refcnt, 1);
582 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
583 SCTP_SOCKET_UNLOCK(so, 1);
587 /* can be delivered right away? */
588 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
589 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
591 /* EY it wont be queued if it could be delivered directly */
593 if (asoc->size_on_all_streams >= control->length) {
594 asoc->size_on_all_streams -= control->length;
597 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
599 asoc->size_on_all_streams = 0;
602 sctp_ucount_decr(asoc->cnt_on_all_streams);
603 strm->last_mid_delivered++;
604 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
605 sctp_add_to_readq(stcb->sctp_ep, stcb,
607 &stcb->sctp_socket->so_rcv, 1,
608 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_LOCKED);
609 TAILQ_FOREACH_SAFE(control, &strm->inqueue, next_instrm, at) {
611 nxt_todel = strm->last_mid_delivered + 1;
612 if (SCTP_MID_EQ(asoc->idata_supported, nxt_todel, control->mid) &&
613 (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG)) {
614 if (control->on_strm_q == SCTP_ON_ORDERED) {
615 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
616 if (asoc->size_on_all_streams >= control->length) {
617 asoc->size_on_all_streams -= control->length;
620 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
622 asoc->size_on_all_streams = 0;
625 sctp_ucount_decr(asoc->cnt_on_all_streams);
628 panic("Huh control: %p is on_strm_q: %d",
629 control, control->on_strm_q);
632 control->on_strm_q = 0;
633 strm->last_mid_delivered++;
635 * We ignore the return of deliver_data here
636 * since we always can hold the chunk on the
637 * d-queue. And we have a finite number that
638 * can be delivered from the strq.
640 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
641 sctp_log_strm_del(control, NULL,
642 SCTP_STR_LOG_FROM_IMMED_DEL);
644 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
645 sctp_add_to_readq(stcb->sctp_ep, stcb,
647 &stcb->sctp_socket->so_rcv, 1,
648 SCTP_READ_LOCK_NOT_HELD,
651 } else if (SCTP_MID_EQ(asoc->idata_supported, nxt_todel, control->mid)) {
656 #if defined(__APPLE__) && !defined(__Userspace__)
657 SCTP_SOCKET_UNLOCK(so, 1);
662 * Ok, we did not deliver this guy, find the correct place
663 * to put it on the queue.
665 if (sctp_place_control_in_stream(strm, asoc, control)) {
666 SCTP_SNPRINTF(msg, sizeof(msg),
667 "Queue to str MID: %u duplicate", control->mid);
668 sctp_clean_up_control(stcb, control);
669 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
670 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
671 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
679 sctp_setup_tail_pointer(struct sctp_queued_to_read *control)
681 struct mbuf *m, *prev = NULL;
682 struct sctp_tcb *stcb;
684 stcb = control->stcb;
685 control->held_length = 0;
689 if (SCTP_BUF_LEN(m) == 0) {
690 /* Skip mbufs with NO length */
693 control->data = sctp_m_free(m);
696 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
697 m = SCTP_BUF_NEXT(prev);
700 control->tail_mbuf = prev;
705 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
706 if (control->on_read_q) {
708 * On read queue so we must increment the
709 * SB stuff, we assume caller has done any locks of SB.
711 sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
713 m = SCTP_BUF_NEXT(m);
716 control->tail_mbuf = prev;
721 sctp_add_to_tail_pointer(struct sctp_queued_to_read *control, struct mbuf *m, uint32_t *added)
723 struct mbuf *prev=NULL;
724 struct sctp_tcb *stcb;
726 stcb = control->stcb;
729 panic("Control broken");
734 if (control->tail_mbuf == NULL) {
736 sctp_m_freem(control->data);
738 sctp_setup_tail_pointer(control);
741 control->tail_mbuf->m_next = m;
743 if (SCTP_BUF_LEN(m) == 0) {
744 /* Skip mbufs with NO length */
747 control->tail_mbuf->m_next = sctp_m_free(m);
748 m = control->tail_mbuf->m_next;
750 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
751 m = SCTP_BUF_NEXT(prev);
754 control->tail_mbuf = prev;
759 if (control->on_read_q) {
761 * On read queue so we must increment the
762 * SB stuff, we assume caller has done any locks of SB.
764 sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
766 *added += SCTP_BUF_LEN(m);
767 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
768 m = SCTP_BUF_NEXT(m);
771 control->tail_mbuf = prev;
776 sctp_build_readq_entry_from_ctl(struct sctp_queued_to_read *nc, struct sctp_queued_to_read *control)
778 memset(nc, 0, sizeof(struct sctp_queued_to_read));
779 nc->sinfo_stream = control->sinfo_stream;
780 nc->mid = control->mid;
781 TAILQ_INIT(&nc->reasm);
782 nc->top_fsn = control->top_fsn;
783 nc->mid = control->mid;
784 nc->sinfo_flags = control->sinfo_flags;
785 nc->sinfo_ppid = control->sinfo_ppid;
786 nc->sinfo_context = control->sinfo_context;
787 nc->fsn_included = 0xffffffff;
788 nc->sinfo_tsn = control->sinfo_tsn;
789 nc->sinfo_cumtsn = control->sinfo_cumtsn;
790 nc->sinfo_assoc_id = control->sinfo_assoc_id;
791 nc->whoFrom = control->whoFrom;
792 atomic_add_int(&nc->whoFrom->ref_count, 1);
793 nc->stcb = control->stcb;
794 nc->port_from = control->port_from;
795 nc->do_not_ref_stcb = control->do_not_ref_stcb;
799 sctp_reset_a_control(struct sctp_queued_to_read *control,
800 struct sctp_inpcb *inp, uint32_t tsn)
802 control->fsn_included = tsn;
803 if (control->on_read_q) {
805 * We have to purge it from there,
806 * hopefully this will work :-)
808 TAILQ_REMOVE(&inp->read_queue, control, next);
809 control->on_read_q = 0;
814 sctp_handle_old_unordered_data(struct sctp_tcb *stcb,
815 struct sctp_association *asoc,
816 struct sctp_stream_in *strm,
817 struct sctp_queued_to_read *control,
819 int inp_read_lock_held)
821 /* Special handling for the old un-ordered data chunk.
822 * All the chunks/TSN's go to mid 0. So
823 * we have to do the old style watching to see
824 * if we have it all. If you return one, no other
825 * control entries on the un-ordered queue will
826 * be looked at. In theory there should be no others
827 * entries in reality, unless the guy is sending both
828 * unordered NDATA and unordered DATA...
830 struct sctp_tmit_chunk *chk, *lchk, *tchk;
832 struct sctp_queued_to_read *nc;
835 if (control->first_frag_seen == 0) {
836 /* Nothing we can do, we have not seen the first piece yet */
839 /* Collapse any we can */
842 fsn = control->fsn_included + 1;
843 /* Now what can we add? */
844 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, lchk) {
845 if (chk->rec.data.fsn == fsn) {
847 sctp_alloc_a_readq(stcb, nc);
851 memset(nc, 0, sizeof(struct sctp_queued_to_read));
852 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
853 sctp_add_chk_to_control(control, strm, stcb, asoc, chk, SCTP_READ_LOCK_NOT_HELD);
857 if (control->end_added) {
859 if (!TAILQ_EMPTY(&control->reasm)) {
861 * Ok we have to move anything left on
862 * the control queue to a new control.
864 sctp_build_readq_entry_from_ctl(nc, control);
865 tchk = TAILQ_FIRST(&control->reasm);
866 if (tchk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
867 TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
868 if (asoc->size_on_reasm_queue >= tchk->send_size) {
869 asoc->size_on_reasm_queue -= tchk->send_size;
872 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, tchk->send_size);
874 asoc->size_on_reasm_queue = 0;
877 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
878 nc->first_frag_seen = 1;
879 nc->fsn_included = tchk->rec.data.fsn;
880 nc->data = tchk->data;
881 nc->sinfo_ppid = tchk->rec.data.ppid;
882 nc->sinfo_tsn = tchk->rec.data.tsn;
883 sctp_mark_non_revokable(asoc, tchk->rec.data.tsn);
885 sctp_free_a_chunk(stcb, tchk, SCTP_SO_NOT_LOCKED);
886 sctp_setup_tail_pointer(nc);
887 tchk = TAILQ_FIRST(&control->reasm);
889 /* Spin the rest onto the queue */
891 TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
892 TAILQ_INSERT_TAIL(&nc->reasm, tchk, sctp_next);
893 tchk = TAILQ_FIRST(&control->reasm);
895 /* Now lets add it to the queue after removing control */
896 TAILQ_INSERT_TAIL(&strm->uno_inqueue, nc, next_instrm);
897 nc->on_strm_q = SCTP_ON_UNORDERED;
898 if (control->on_strm_q) {
899 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
900 control->on_strm_q = 0;
903 if (control->pdapi_started) {
904 strm->pd_api_started = 0;
905 control->pdapi_started = 0;
907 if (control->on_strm_q) {
908 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
909 control->on_strm_q = 0;
910 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
912 if (control->on_read_q == 0) {
913 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
914 &stcb->sctp_socket->so_rcv, control->end_added,
915 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
916 #if defined(__Userspace__)
918 sctp_invoke_recv_callback(stcb->sctp_ep, stcb, control, inp_read_lock_held);
921 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
922 if ((nc->first_frag_seen) && !TAILQ_EMPTY(&nc->reasm)) {
923 /* Switch to the new guy and continue */
927 if (nc->on_strm_q == 0) {
928 sctp_free_a_readq(stcb, nc);
933 sctp_free_a_readq(stcb, nc);
940 if (cnt_added && strm->pd_api_started) {
941 #if defined(__Userspace__)
942 sctp_invoke_recv_callback(stcb->sctp_ep, stcb, control, SCTP_READ_LOCK_NOT_HELD);
944 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
946 if ((control->length > pd_point) && (strm->pd_api_started == 0)) {
947 strm->pd_api_started = 1;
948 control->pdapi_started = 1;
949 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
950 &stcb->sctp_socket->so_rcv, control->end_added,
951 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
952 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
960 sctp_inject_old_unordered_data(struct sctp_tcb *stcb,
961 struct sctp_association *asoc,
962 struct sctp_queued_to_read *control,
963 struct sctp_tmit_chunk *chk,
966 struct sctp_tmit_chunk *at;
969 * Here we need to place the chunk into the control structure
970 * sorted in the correct order.
972 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
973 /* Its the very first one. */
974 SCTPDBG(SCTP_DEBUG_XXX,
975 "chunk is a first fsn: %u becomes fsn_included\n",
977 at = TAILQ_FIRST(&control->reasm);
978 if (at && SCTP_TSN_GT(chk->rec.data.fsn, at->rec.data.fsn)) {
980 * The first chunk in the reassembly is
981 * a smaller TSN than this one, even though
982 * this has a first, it must be from a subsequent
987 if (control->first_frag_seen) {
989 * In old un-ordered we can reassembly on
990 * one control multiple messages. As long
991 * as the next FIRST is greater then the old
992 * first (TSN i.e. FSN wise)
997 if (SCTP_TSN_GT(chk->rec.data.fsn, control->fsn_included)) {
998 /* Easy way the start of a new guy beyond the lowest */
1001 if ((chk->rec.data.fsn == control->fsn_included) ||
1002 (control->pdapi_started)) {
1004 * Ok this should not happen, if it does
1005 * we started the pd-api on the higher TSN (since
1006 * the equals part is a TSN failure it must be that).
1008 * We are completly hosed in that case since I have
1009 * no way to recover. This really will only happen
1010 * if we can get more TSN's higher before the pd-api-point.
1012 sctp_abort_in_reasm(stcb, control, chk,
1014 SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1019 * Ok we have two firsts and the one we just got
1020 * is smaller than the one we previously placed.. yuck!
1021 * We must swap them out.
1023 /* swap the mbufs */
1024 tdata = control->data;
1025 control->data = chk->data;
1027 /* Save the lengths */
1028 chk->send_size = control->length;
1029 /* Recompute length of control and tail pointer */
1030 sctp_setup_tail_pointer(control);
1031 /* Fix the FSN included */
1032 tmp = control->fsn_included;
1033 control->fsn_included = chk->rec.data.fsn;
1034 chk->rec.data.fsn = tmp;
1035 /* Fix the TSN included */
1036 tmp = control->sinfo_tsn;
1037 control->sinfo_tsn = chk->rec.data.tsn;
1038 chk->rec.data.tsn = tmp;
1039 /* Fix the PPID included */
1040 tmp = control->sinfo_ppid;
1041 control->sinfo_ppid = chk->rec.data.ppid;
1042 chk->rec.data.ppid = tmp;
1043 /* Fix tail pointer */
1046 control->first_frag_seen = 1;
1047 control->fsn_included = chk->rec.data.fsn;
1048 control->top_fsn = chk->rec.data.fsn;
1049 control->sinfo_tsn = chk->rec.data.tsn;
1050 control->sinfo_ppid = chk->rec.data.ppid;
1051 control->data = chk->data;
1052 sctp_mark_non_revokable(asoc, chk->rec.data.tsn);
1054 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1055 sctp_setup_tail_pointer(control);
1060 TAILQ_FOREACH(at, &control->reasm, sctp_next) {
1061 if (SCTP_TSN_GT(at->rec.data.fsn, chk->rec.data.fsn)) {
1063 * This one in queue is bigger than the new one, insert
1064 * the new one before at.
1066 asoc->size_on_reasm_queue += chk->send_size;
1067 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1069 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1071 } else if (at->rec.data.fsn == chk->rec.data.fsn) {
1073 * They sent a duplicate fsn number. This
1074 * really should not happen since the FSN is
1075 * a TSN and it should have been dropped earlier.
1077 sctp_abort_in_reasm(stcb, control, chk,
1079 SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1084 if (inserted == 0) {
1085 /* Its at the end */
1086 asoc->size_on_reasm_queue += chk->send_size;
1087 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1088 control->top_fsn = chk->rec.data.fsn;
1089 TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
1094 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc,
1095 struct sctp_stream_in *strm, int inp_read_lock_held)
1098 * Given a stream, strm, see if any of
1099 * the SSN's on it that are fragmented
1100 * are ready to deliver. If so go ahead
1101 * and place them on the read queue. In
1102 * so placing if we have hit the end, then
1103 * we need to remove them from the stream's queue.
1105 struct sctp_queued_to_read *control, *nctl = NULL;
1106 uint32_t next_to_del;
1110 if (stcb->sctp_socket) {
1111 pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
1112 stcb->sctp_ep->partial_delivery_point);
1114 pd_point = stcb->sctp_ep->partial_delivery_point;
1116 control = TAILQ_FIRST(&strm->uno_inqueue);
1118 if ((control != NULL) &&
1119 (asoc->idata_supported == 0)) {
1120 /* Special handling needed for "old" data format */
1121 if (sctp_handle_old_unordered_data(stcb, asoc, strm, control, pd_point, inp_read_lock_held)) {
1125 if (strm->pd_api_started) {
1126 /* Can't add more */
1130 SCTPDBG(SCTP_DEBUG_XXX, "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u -uo\n",
1131 control, control->end_added, control->mid, control->top_fsn, control->fsn_included);
1132 nctl = TAILQ_NEXT(control, next_instrm);
1133 if (control->end_added) {
1134 /* We just put the last bit on */
1135 if (control->on_strm_q) {
1137 if (control->on_strm_q != SCTP_ON_UNORDERED ) {
1138 panic("Huh control: %p on_q: %d -- not unordered?",
1139 control, control->on_strm_q);
1142 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
1143 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
1144 if (asoc->size_on_all_streams >= control->length) {
1145 asoc->size_on_all_streams -= control->length;
1148 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1150 asoc->size_on_all_streams = 0;
1153 sctp_ucount_decr(asoc->cnt_on_all_streams);
1154 control->on_strm_q = 0;
1156 if (control->on_read_q == 0) {
1157 sctp_add_to_readq(stcb->sctp_ep, stcb,
1159 &stcb->sctp_socket->so_rcv, control->end_added,
1160 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1163 /* Can we do a PD-API for this un-ordered guy? */
1164 if ((control->length >= pd_point) && (strm->pd_api_started == 0)) {
1165 strm->pd_api_started = 1;
1166 control->pdapi_started = 1;
1167 sctp_add_to_readq(stcb->sctp_ep, stcb,
1169 &stcb->sctp_socket->so_rcv, control->end_added,
1170 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1178 control = TAILQ_FIRST(&strm->inqueue);
1179 if (strm->pd_api_started) {
1180 /* Can't add more */
1183 if (control == NULL) {
1186 if (SCTP_MID_EQ(asoc->idata_supported, strm->last_mid_delivered, control->mid)) {
1187 /* Ok the guy at the top was being partially delivered
1188 * completed, so we remove it. Note
1189 * the pd_api flag was taken off when the
1190 * chunk was merged on in sctp_queue_data_for_reasm below.
1192 nctl = TAILQ_NEXT(control, next_instrm);
1193 SCTPDBG(SCTP_DEBUG_XXX,
1194 "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (lastdel: %u)- o\n",
1195 control, control->end_added, control->mid,
1196 control->top_fsn, control->fsn_included,
1197 strm->last_mid_delivered);
1198 if (control->end_added) {
1199 if (control->on_strm_q) {
1201 if (control->on_strm_q != SCTP_ON_ORDERED ) {
1202 panic("Huh control: %p on_q: %d -- not ordered?",
1203 control, control->on_strm_q);
1206 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
1207 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
1208 if (asoc->size_on_all_streams >= control->length) {
1209 asoc->size_on_all_streams -= control->length;
1212 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1214 asoc->size_on_all_streams = 0;
1217 sctp_ucount_decr(asoc->cnt_on_all_streams);
1218 control->on_strm_q = 0;
1220 if (strm->pd_api_started && control->pdapi_started) {
1221 control->pdapi_started = 0;
1222 strm->pd_api_started = 0;
1224 if (control->on_read_q == 0) {
1225 sctp_add_to_readq(stcb->sctp_ep, stcb,
1227 &stcb->sctp_socket->so_rcv, control->end_added,
1228 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1233 if (strm->pd_api_started) {
1234 /* Can't add more must have gotten an un-ordered above being partially delivered. */
1238 next_to_del = strm->last_mid_delivered + 1;
1240 SCTPDBG(SCTP_DEBUG_XXX,
1241 "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (nxtdel: %u)- o\n",
1242 control, control->end_added, control->mid, control->top_fsn, control->fsn_included,
1244 nctl = TAILQ_NEXT(control, next_instrm);
1245 if (SCTP_MID_EQ(asoc->idata_supported, control->mid, next_to_del) &&
1246 (control->first_frag_seen)) {
1249 /* Ok we can deliver it onto the stream. */
1250 if (control->end_added) {
1251 /* We are done with it afterwards */
1252 if (control->on_strm_q) {
1254 if (control->on_strm_q != SCTP_ON_ORDERED ) {
1255 panic("Huh control: %p on_q: %d -- not ordered?",
1256 control, control->on_strm_q);
1259 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
1260 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
1261 if (asoc->size_on_all_streams >= control->length) {
1262 asoc->size_on_all_streams -= control->length;
1265 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1267 asoc->size_on_all_streams = 0;
1270 sctp_ucount_decr(asoc->cnt_on_all_streams);
1271 control->on_strm_q = 0;
1275 if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
1276 /* A singleton now slipping through - mark it non-revokable too */
1277 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
1278 } else if (control->end_added == 0) {
1279 /* Check if we can defer adding until its all there */
1280 if ((control->length < pd_point) || (strm->pd_api_started)) {
1281 /* Don't need it or cannot add more (one being delivered that way) */
1285 done = (control->end_added) && (control->last_frag_seen);
1286 if (control->on_read_q == 0) {
1288 if (asoc->size_on_all_streams >= control->length) {
1289 asoc->size_on_all_streams -= control->length;
1292 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1294 asoc->size_on_all_streams = 0;
1297 strm->pd_api_started = 1;
1298 control->pdapi_started = 1;
1300 sctp_add_to_readq(stcb->sctp_ep, stcb,
1302 &stcb->sctp_socket->so_rcv, control->end_added,
1303 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1305 strm->last_mid_delivered = next_to_del;
1318 sctp_add_chk_to_control(struct sctp_queued_to_read *control,
1319 struct sctp_stream_in *strm,
1320 struct sctp_tcb *stcb, struct sctp_association *asoc,
1321 struct sctp_tmit_chunk *chk, int hold_rlock)
1324 * Given a control and a chunk, merge the
1325 * data from the chk onto the control and free
1326 * up the chunk resources.
1331 if (control->on_read_q && (hold_rlock == 0)) {
1333 * Its being pd-api'd so we must
1336 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1339 if (control->data == NULL) {
1340 control->data = chk->data;
1341 sctp_setup_tail_pointer(control);
1343 sctp_add_to_tail_pointer(control, chk->data, &added);
1345 control->fsn_included = chk->rec.data.fsn;
1346 asoc->size_on_reasm_queue -= chk->send_size;
1347 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
1348 sctp_mark_non_revokable(asoc, chk->rec.data.tsn);
1350 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1351 control->first_frag_seen = 1;
1352 control->sinfo_tsn = chk->rec.data.tsn;
1353 control->sinfo_ppid = chk->rec.data.ppid;
1355 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1357 if ((control->on_strm_q) && (control->on_read_q)) {
1358 if (control->pdapi_started) {
1359 control->pdapi_started = 0;
1360 strm->pd_api_started = 0;
1362 if (control->on_strm_q == SCTP_ON_UNORDERED) {
1364 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
1365 control->on_strm_q = 0;
1366 } else if (control->on_strm_q == SCTP_ON_ORDERED) {
1368 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
1370 * Don't need to decrement size_on_all_streams,
1371 * since control is on the read queue.
1373 sctp_ucount_decr(asoc->cnt_on_all_streams);
1374 control->on_strm_q = 0;
1376 } else if (control->on_strm_q) {
1377 panic("Unknown state on ctrl: %p on_strm_q: %d", control,
1378 control->on_strm_q);
1382 control->end_added = 1;
1383 control->last_frag_seen = 1;
1386 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1388 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1393 * Dump onto the re-assembly queue, in its proper place. After dumping on the
1394 * queue, see if anthing can be delivered. If so pull it off (or as much as
1395 * we can. If we run out of space then we must dump what we can and set the
1396 * appropriate flag to say we queued what we could.
1399 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
1400 struct sctp_queued_to_read *control,
1401 struct sctp_tmit_chunk *chk,
1402 int created_control,
1403 int *abort_flag, uint32_t tsn)
1406 struct sctp_tmit_chunk *at, *nat;
1407 struct sctp_stream_in *strm;
1408 int do_wakeup, unordered;
1411 strm = &asoc->strmin[control->sinfo_stream];
1413 * For old un-ordered data chunks.
1415 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
1420 /* Must be added to the stream-in queue */
1421 if (created_control) {
1422 if ((unordered == 0) || (asoc->idata_supported)) {
1423 sctp_ucount_incr(asoc->cnt_on_all_streams);
1425 if (sctp_place_control_in_stream(strm, asoc, control)) {
1426 /* Duplicate SSN? */
1427 sctp_abort_in_reasm(stcb, control, chk,
1429 SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1430 sctp_clean_up_control(stcb, control);
1433 if ((tsn == (asoc->cumulative_tsn + 1) && (asoc->idata_supported == 0))) {
1434 /* Ok we created this control and now
1435 * lets validate that its legal i.e. there
1436 * is a B bit set, if not and we have
1437 * up to the cum-ack then its invalid.
1439 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
1440 sctp_abort_in_reasm(stcb, control, chk,
1442 SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1447 if ((asoc->idata_supported == 0) && (unordered == 1)) {
1448 sctp_inject_old_unordered_data(stcb, asoc, control, chk, abort_flag);
1452 * Ok we must queue the chunk into the reasembly portion:
1453 * o if its the first it goes to the control mbuf.
1454 * o if its not first but the next in sequence it goes to the control,
1455 * and each succeeding one in order also goes.
1456 * o if its not in order we place it on the list in its place.
1458 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1459 /* Its the very first one. */
1460 SCTPDBG(SCTP_DEBUG_XXX,
1461 "chunk is a first fsn: %u becomes fsn_included\n",
1463 if (control->first_frag_seen) {
1465 * Error on senders part, they either
1466 * sent us two data chunks with FIRST,
1467 * or they sent two un-ordered chunks that
1468 * were fragmented at the same time in the same stream.
1470 sctp_abort_in_reasm(stcb, control, chk,
1472 SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1475 control->first_frag_seen = 1;
1476 control->sinfo_ppid = chk->rec.data.ppid;
1477 control->sinfo_tsn = chk->rec.data.tsn;
1478 control->fsn_included = chk->rec.data.fsn;
1479 control->data = chk->data;
1480 sctp_mark_non_revokable(asoc, chk->rec.data.tsn);
1482 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1483 sctp_setup_tail_pointer(control);
1484 asoc->size_on_all_streams += control->length;
1486 /* Place the chunk in our list */
1488 if (control->last_frag_seen == 0) {
1489 /* Still willing to raise highest FSN seen */
1490 if (SCTP_TSN_GT(chk->rec.data.fsn, control->top_fsn)) {
1491 SCTPDBG(SCTP_DEBUG_XXX,
1492 "We have a new top_fsn: %u\n",
1494 control->top_fsn = chk->rec.data.fsn;
1496 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1497 SCTPDBG(SCTP_DEBUG_XXX,
1498 "The last fsn is now in place fsn: %u\n",
1500 control->last_frag_seen = 1;
1501 if (SCTP_TSN_GT(control->top_fsn, chk->rec.data.fsn)) {
1502 SCTPDBG(SCTP_DEBUG_XXX,
1503 "New fsn: %u is not at top_fsn: %u -- abort\n",
1506 sctp_abort_in_reasm(stcb, control, chk,
1508 SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1512 if (asoc->idata_supported || control->first_frag_seen) {
1514 * For IDATA we always check since we know that
1515 * the first fragment is 0. For old DATA we have
1516 * to receive the first before we know the first FSN
1517 * (which is the TSN).
1519 if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn)) {
1520 /* We have already delivered up to this so its a dup */
1521 sctp_abort_in_reasm(stcb, control, chk,
1523 SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1528 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1529 /* Second last? huh? */
1530 SCTPDBG(SCTP_DEBUG_XXX,
1531 "Duplicate last fsn: %u (top: %u) -- abort\n",
1532 chk->rec.data.fsn, control->top_fsn);
1533 sctp_abort_in_reasm(stcb, control,
1535 SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1538 if (asoc->idata_supported || control->first_frag_seen) {
1540 * For IDATA we always check since we know that
1541 * the first fragment is 0. For old DATA we have
1542 * to receive the first before we know the first FSN
1543 * (which is the TSN).
1546 if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn)) {
1547 /* We have already delivered up to this so its a dup */
1548 SCTPDBG(SCTP_DEBUG_XXX,
1549 "New fsn: %u is already seen in included_fsn: %u -- abort\n",
1550 chk->rec.data.fsn, control->fsn_included);
1551 sctp_abort_in_reasm(stcb, control, chk,
1553 SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1557 /* validate not beyond top FSN if we have seen last one */
1558 if (SCTP_TSN_GT(chk->rec.data.fsn, control->top_fsn)) {
1559 SCTPDBG(SCTP_DEBUG_XXX,
1560 "New fsn: %u is beyond or at top_fsn: %u -- abort\n",
1563 sctp_abort_in_reasm(stcb, control, chk,
1565 SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1570 * If we reach here, we need to place the
1571 * new chunk in the reassembly for this
1574 SCTPDBG(SCTP_DEBUG_XXX,
1575 "chunk is a not first fsn: %u needs to be inserted\n",
1577 TAILQ_FOREACH(at, &control->reasm, sctp_next) {
1578 if (SCTP_TSN_GT(at->rec.data.fsn, chk->rec.data.fsn)) {
1579 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1580 /* Last not at the end? huh? */
1581 SCTPDBG(SCTP_DEBUG_XXX,
1582 "Last fragment not last in list: -- abort\n");
1583 sctp_abort_in_reasm(stcb, control,
1585 SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1589 * This one in queue is bigger than the new one, insert
1590 * the new one before at.
1592 SCTPDBG(SCTP_DEBUG_XXX,
1593 "Insert it before fsn: %u\n",
1595 asoc->size_on_reasm_queue += chk->send_size;
1596 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1597 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1600 } else if (at->rec.data.fsn == chk->rec.data.fsn) {
1601 /* Gak, He sent me a duplicate str seq number */
1603 * foo bar, I guess I will just free this new guy,
1604 * should we abort too? FIX ME MAYBE? Or it COULD be
1605 * that the SSN's have wrapped. Maybe I should
1606 * compare to TSN somehow... sigh for now just blow
1609 SCTPDBG(SCTP_DEBUG_XXX,
1610 "Duplicate to fsn: %u -- abort\n",
1612 sctp_abort_in_reasm(stcb, control,
1614 SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1618 if (inserted == 0) {
1619 /* Goes on the end */
1620 SCTPDBG(SCTP_DEBUG_XXX, "Inserting at tail of list fsn: %u\n",
1622 asoc->size_on_reasm_queue += chk->send_size;
1623 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1624 TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
1628 * Ok lets see if we can suck any up into the control
1629 * structure that are in seq if it makes sense.
1633 * If the first fragment has not been
1634 * seen there is no sense in looking.
1636 if (control->first_frag_seen) {
1637 next_fsn = control->fsn_included + 1;
1638 TAILQ_FOREACH_SAFE(at, &control->reasm, sctp_next, nat) {
1639 if (at->rec.data.fsn == next_fsn) {
1640 /* We can add this one now to the control */
1641 SCTPDBG(SCTP_DEBUG_XXX,
1642 "Adding more to control: %p at: %p fsn: %u next_fsn: %u included: %u\n",
1645 next_fsn, control->fsn_included);
1646 TAILQ_REMOVE(&control->reasm, at, sctp_next);
1647 lenadded = sctp_add_chk_to_control(control, strm, stcb, asoc, at, SCTP_READ_LOCK_NOT_HELD);
1648 if (control->on_read_q) {
1652 * We only add to the size-on-all-streams
1653 * if its not on the read q. The read q
1654 * flag will cause a sballoc so its accounted
1657 asoc->size_on_all_streams += lenadded;
1660 if (control->end_added && control->pdapi_started) {
1661 if (strm->pd_api_started) {
1662 strm->pd_api_started = 0;
1663 control->pdapi_started = 0;
1665 if (control->on_read_q == 0) {
1666 sctp_add_to_readq(stcb->sctp_ep, stcb,
1668 &stcb->sctp_socket->so_rcv, control->end_added,
1669 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1679 #if defined(__Userspace__)
1680 sctp_invoke_recv_callback(stcb->sctp_ep, stcb, control, SCTP_READ_LOCK_NOT_HELD);
1682 /* Need to wakeup the reader */
1683 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1687 static struct sctp_queued_to_read *
1688 sctp_find_reasm_entry(struct sctp_stream_in *strm, uint32_t mid, int ordered, int idata_supported)
1690 struct sctp_queued_to_read *control;
1693 TAILQ_FOREACH(control, &strm->inqueue, next_instrm) {
1694 if (SCTP_MID_EQ(idata_supported, control->mid, mid)) {
1699 if (idata_supported) {
1700 TAILQ_FOREACH(control, &strm->uno_inqueue, next_instrm) {
1701 if (SCTP_MID_EQ(idata_supported, control->mid, mid)) {
1706 control = TAILQ_FIRST(&strm->uno_inqueue);
1713 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1714 struct mbuf **m, int offset, int chk_length,
1715 struct sctp_nets *net, uint32_t *high_tsn, int *abort_flag,
1716 int *break_flag, int last_chunk, uint8_t chk_type)
1718 struct sctp_tmit_chunk *chk = NULL; /* make gcc happy */
1719 struct sctp_stream_in *strm;
1720 uint32_t tsn, fsn, gap, mid;
1723 int need_reasm_check = 0;
1725 struct mbuf *op_err;
1726 char msg[SCTP_DIAG_INFO_LEN];
1727 struct sctp_queued_to_read *control, *ncontrol;
1730 struct sctp_stream_reset_list *liste;
1733 int created_control = 0;
1735 if (chk_type == SCTP_IDATA) {
1736 struct sctp_idata_chunk *chunk, chunk_buf;
1738 chunk = (struct sctp_idata_chunk *)sctp_m_getptr(*m, offset,
1739 sizeof(struct sctp_idata_chunk), (uint8_t *)&chunk_buf);
1740 chk_flags = chunk->ch.chunk_flags;
1741 clen = sizeof(struct sctp_idata_chunk);
1742 tsn = ntohl(chunk->dp.tsn);
1743 sid = ntohs(chunk->dp.sid);
1744 mid = ntohl(chunk->dp.mid);
1745 if (chk_flags & SCTP_DATA_FIRST_FRAG) {
1747 ppid = chunk->dp.ppid_fsn.ppid;
1749 fsn = ntohl(chunk->dp.ppid_fsn.fsn);
1750 ppid = 0xffffffff; /* Use as an invalid value. */
1753 struct sctp_data_chunk *chunk, chunk_buf;
1755 chunk = (struct sctp_data_chunk *)sctp_m_getptr(*m, offset,
1756 sizeof(struct sctp_data_chunk), (uint8_t *)&chunk_buf);
1757 chk_flags = chunk->ch.chunk_flags;
1758 clen = sizeof(struct sctp_data_chunk);
1759 tsn = ntohl(chunk->dp.tsn);
1760 sid = ntohs(chunk->dp.sid);
1761 mid = (uint32_t)(ntohs(chunk->dp.ssn));
1763 ppid = chunk->dp.ppid;
1765 if ((size_t)chk_length == clen) {
1767 * Need to send an abort since we had a
1770 op_err = sctp_generate_no_user_data_cause(tsn);
1771 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1772 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1776 if ((chk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1777 asoc->send_sack = 1;
1779 ordered = ((chk_flags & SCTP_DATA_UNORDERED) == 0);
1780 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1781 sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1786 SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, chk_type, tsn);
1787 if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1788 /* It is a duplicate */
1789 SCTP_STAT_INCR(sctps_recvdupdata);
1790 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1791 /* Record a dup for the next outbound sack */
1792 asoc->dup_tsns[asoc->numduptsns] = tsn;
1795 asoc->send_sack = 1;
1798 /* Calculate the number of TSN's between the base and this TSN */
1799 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1800 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1801 /* Can't hold the bit in the mapping at max array, toss it */
1804 if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1805 SCTP_TCB_LOCK_ASSERT(stcb);
1806 if (sctp_expand_mapping_array(asoc, gap)) {
1807 /* Can't expand, drop it */
1811 if (SCTP_TSN_GT(tsn, *high_tsn)) {
1814 /* See if we have received this one already */
1815 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
1816 SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1817 SCTP_STAT_INCR(sctps_recvdupdata);
1818 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1819 /* Record a dup for the next outbound sack */
1820 asoc->dup_tsns[asoc->numduptsns] = tsn;
1823 asoc->send_sack = 1;
1827 * Check to see about the GONE flag, duplicates would cause a sack
1828 * to be sent up above
1830 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1831 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1832 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1834 * wait a minute, this guy is gone, there is no longer a
1835 * receiver. Send peer an ABORT!
1837 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1838 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1843 * Now before going further we see if there is room. If NOT then we
1844 * MAY let one through only IF this TSN is the one we are waiting
1845 * for on a partial delivery API.
1848 /* Is the stream valid? */
1849 if (sid >= asoc->streamincnt) {
1850 struct sctp_error_invalid_stream *cause;
1852 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_invalid_stream),
1853 0, M_NOWAIT, 1, MT_DATA);
1854 if (op_err != NULL) {
1855 /* add some space up front so prepend will work well */
1856 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1857 cause = mtod(op_err, struct sctp_error_invalid_stream *);
1859 * Error causes are just param's and this one has
1860 * two back to back phdr, one with the error type
1861 * and size, the other with the streamid and a rsvd
1863 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_invalid_stream);
1864 cause->cause.code = htons(SCTP_CAUSE_INVALID_STREAM);
1865 cause->cause.length = htons(sizeof(struct sctp_error_invalid_stream));
1866 cause->stream_id = htons(sid);
1867 cause->reserved = htons(0);
1868 sctp_queue_op_err(stcb, op_err);
1870 SCTP_STAT_INCR(sctps_badsid);
1871 SCTP_TCB_LOCK_ASSERT(stcb);
1872 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1873 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1874 asoc->highest_tsn_inside_nr_map = tsn;
1876 if (tsn == (asoc->cumulative_tsn + 1)) {
1877 /* Update cum-ack */
1878 asoc->cumulative_tsn = tsn;
1883 * If its a fragmented message, lets see if we can
1884 * find the control on the reassembly queues.
1886 if ((chk_type == SCTP_IDATA) &&
1887 ((chk_flags & SCTP_DATA_FIRST_FRAG) == 0) &&
1890 * The first *must* be fsn 0, and other
1891 * (middle/end) pieces can *not* be fsn 0.
1892 * XXX: This can happen in case of a wrap around.
1893 * Ignore is for now.
1895 SCTP_SNPRINTF(msg, sizeof(msg), "FSN zero for MID=%8.8x, but flags=%2.2x", mid, chk_flags);
1898 control = sctp_find_reasm_entry(&asoc->strmin[sid], mid, ordered, asoc->idata_supported);
1899 SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags:0x%x look for control on queues %p\n",
1900 chk_flags, control);
1901 if ((chk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1902 /* See if we can find the re-assembly entity */
1903 if (control != NULL) {
1904 /* We found something, does it belong? */
1905 if (ordered && (mid != control->mid)) {
1906 SCTP_SNPRINTF(msg, sizeof(msg), "Reassembly problem (MID=%8.8x)", mid);
1908 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1909 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1910 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1914 if (ordered && ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED)) {
1915 /* We can't have a switched order with an unordered chunk */
1916 SCTP_SNPRINTF(msg, sizeof(msg),
1917 "All fragments of a user message must be ordered or unordered (TSN=%8.8x)",
1921 if (!ordered && (((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) == 0)) {
1922 /* We can't have a switched unordered with a ordered chunk */
1923 SCTP_SNPRINTF(msg, sizeof(msg),
1924 "All fragments of a user message must be ordered or unordered (TSN=%8.8x)",
1930 /* Its a complete segment. Lets validate we
1931 * don't have a re-assembly going on with
1932 * the same Stream/Seq (for ordered) or in
1933 * the same Stream for unordered.
1935 if (control != NULL) {
1936 if (ordered || asoc->idata_supported) {
1937 SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x dup detected on MID: %u\n",
1939 SCTP_SNPRINTF(msg, sizeof(msg), "Duplicate MID=%8.8x detected.", mid);
1942 if ((tsn == control->fsn_included + 1) &&
1943 (control->end_added == 0)) {
1944 SCTP_SNPRINTF(msg, sizeof(msg),
1945 "Illegal message sequence, missing end for MID: %8.8x",
1946 control->fsn_included);
1954 /* now do the tests */
1955 if (((asoc->cnt_on_all_streams +
1956 asoc->cnt_on_reasm_queue +
1957 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1958 (((int)asoc->my_rwnd) <= 0)) {
1960 * When we have NO room in the rwnd we check to make sure
1961 * the reader is doing its job...
1963 if (stcb->sctp_socket->so_rcv.sb_cc) {
1964 /* some to read, wake-up */
1965 #if defined(__APPLE__) && !defined(__Userspace__)
1968 so = SCTP_INP_SO(stcb->sctp_ep);
1969 atomic_add_int(&stcb->asoc.refcnt, 1);
1970 SCTP_TCB_UNLOCK(stcb);
1971 SCTP_SOCKET_LOCK(so, 1);
1972 SCTP_TCB_LOCK(stcb);
1973 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1974 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1975 /* assoc was freed while we were unlocked */
1976 SCTP_SOCKET_UNLOCK(so, 1);
1980 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1981 #if defined(__APPLE__) && !defined(__Userspace__)
1982 SCTP_SOCKET_UNLOCK(so, 1);
1985 /* now is it in the mapping array of what we have accepted? */
1986 if (chk_type == SCTP_DATA) {
1987 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
1988 SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1989 /* Nope not in the valid range dump it */
1991 sctp_set_rwnd(stcb, asoc);
1992 if ((asoc->cnt_on_all_streams +
1993 asoc->cnt_on_reasm_queue +
1994 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1995 SCTP_STAT_INCR(sctps_datadropchklmt);
1997 SCTP_STAT_INCR(sctps_datadroprwnd);
2003 if (control == NULL) {
2006 if (SCTP_TSN_GT(fsn, control->top_fsn)) {
2011 #ifdef SCTP_ASOCLOG_OF_TSNS
2012 SCTP_TCB_LOCK_ASSERT(stcb);
2013 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
2014 asoc->tsn_in_at = 0;
2015 asoc->tsn_in_wrapped = 1;
2017 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
2018 asoc->in_tsnlog[asoc->tsn_in_at].strm = sid;
2019 asoc->in_tsnlog[asoc->tsn_in_at].seq = mid;
2020 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
2021 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
2022 asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
2023 asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
2024 asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
2028 * Before we continue lets validate that we are not being fooled by
2029 * an evil attacker. We can only have Nk chunks based on our TSN
2030 * spread allowed by the mapping array N * 8 bits, so there is no
2031 * way our stream sequence numbers could have wrapped. We of course
2032 * only validate the FIRST fragment so the bit must be set.
2034 if ((chk_flags & SCTP_DATA_FIRST_FRAG) &&
2035 (TAILQ_EMPTY(&asoc->resetHead)) &&
2036 (chk_flags & SCTP_DATA_UNORDERED) == 0 &&
2037 SCTP_MID_GE(asoc->idata_supported, asoc->strmin[sid].last_mid_delivered, mid)) {
2038 /* The incoming sseq is behind where we last delivered? */
2039 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ: %u delivered: %u from peer, Abort!\n",
2040 mid, asoc->strmin[sid].last_mid_delivered);
2042 if (asoc->idata_supported) {
2043 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered MID=%8.8x, got TSN=%8.8x, SID=%4.4x, MID=%8.8x",
2044 asoc->strmin[sid].last_mid_delivered,
2049 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
2050 (uint16_t)asoc->strmin[sid].last_mid_delivered,
2055 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2056 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_18;
2057 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
2061 if (chk_type == SCTP_IDATA) {
2062 the_len = (chk_length - sizeof(struct sctp_idata_chunk));
2064 the_len = (chk_length - sizeof(struct sctp_data_chunk));
2066 if (last_chunk == 0) {
2067 if (chk_type == SCTP_IDATA) {
2068 dmbuf = SCTP_M_COPYM(*m,
2069 (offset + sizeof(struct sctp_idata_chunk)),
2072 dmbuf = SCTP_M_COPYM(*m,
2073 (offset + sizeof(struct sctp_data_chunk)),
2076 #ifdef SCTP_MBUF_LOGGING
2077 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2078 sctp_log_mbc(dmbuf, SCTP_MBUF_ICOPY);
2082 /* We can steal the last chunk */
2085 /* lop off the top part */
2086 if (chk_type == SCTP_IDATA) {
2087 m_adj(dmbuf, (offset + sizeof(struct sctp_idata_chunk)));
2089 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
2091 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
2092 l_len = SCTP_BUF_LEN(dmbuf);
2094 /* need to count up the size hopefully
2095 * does not hit this to often :-0
2100 for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
2101 l_len += SCTP_BUF_LEN(lat);
2104 if (l_len > the_len) {
2105 /* Trim the end round bytes off too */
2106 m_adj(dmbuf, -(l_len - the_len));
2109 if (dmbuf == NULL) {
2110 SCTP_STAT_INCR(sctps_nomem);
2114 * Now no matter what, we need a control, get one
2115 * if we don't have one (we may have gotten it
2116 * above when we found the message was fragmented
2118 if (control == NULL) {
2119 sctp_alloc_a_readq(stcb, control);
2120 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2125 if (control == NULL) {
2126 SCTP_STAT_INCR(sctps_nomem);
2129 if ((chk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
2132 control->data = dmbuf;
2133 control->tail_mbuf = NULL;
2134 for (mm = control->data; mm; mm = mm->m_next) {
2135 control->length += SCTP_BUF_LEN(mm);
2136 if (SCTP_BUF_NEXT(mm) == NULL) {
2137 control->tail_mbuf = mm;
2140 control->end_added = 1;
2141 control->last_frag_seen = 1;
2142 control->first_frag_seen = 1;
2143 control->fsn_included = fsn;
2144 control->top_fsn = fsn;
2146 created_control = 1;
2148 SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x ordered: %d MID: %u control: %p\n",
2149 chk_flags, ordered, mid, control);
2150 if ((chk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
2151 TAILQ_EMPTY(&asoc->resetHead) &&
2153 (SCTP_MID_EQ(asoc->idata_supported, asoc->strmin[sid].last_mid_delivered + 1, mid) &&
2154 TAILQ_EMPTY(&asoc->strmin[sid].inqueue)))) {
2155 /* Candidate for express delivery */
2157 * Its not fragmented, No PD-API is up, Nothing in the
2158 * delivery queue, Its un-ordered OR ordered and the next to
2159 * deliver AND nothing else is stuck on the stream queue,
2160 * And there is room for it in the socket buffer. Lets just
2161 * stuff it up the buffer....
2163 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2164 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
2165 asoc->highest_tsn_inside_nr_map = tsn;
2167 SCTPDBG(SCTP_DEBUG_XXX, "Injecting control: %p to be read (MID: %u)\n",
2170 sctp_add_to_readq(stcb->sctp_ep, stcb,
2171 control, &stcb->sctp_socket->so_rcv,
2172 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2174 if ((chk_flags & SCTP_DATA_UNORDERED) == 0) {
2175 /* for ordered, bump what we delivered */
2176 asoc->strmin[sid].last_mid_delivered++;
2178 SCTP_STAT_INCR(sctps_recvexpress);
2179 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2180 sctp_log_strm_del_alt(stcb, tsn, mid, sid,
2181 SCTP_STR_LOG_FROM_EXPRS_DEL);
2184 goto finish_express_del;
2187 /* Now will we need a chunk too? */
2188 if ((chk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2189 sctp_alloc_a_chunk(stcb, chk);
2191 /* No memory so we drop the chunk */
2192 SCTP_STAT_INCR(sctps_nomem);
2193 if (last_chunk == 0) {
2194 /* we copied it, free the copy */
2195 sctp_m_freem(dmbuf);
2199 chk->rec.data.tsn = tsn;
2200 chk->no_fr_allowed = 0;
2201 chk->rec.data.fsn = fsn;
2202 chk->rec.data.mid = mid;
2203 chk->rec.data.sid = sid;
2204 chk->rec.data.ppid = ppid;
2205 chk->rec.data.context = stcb->asoc.context;
2206 chk->rec.data.doing_fast_retransmit = 0;
2207 chk->rec.data.rcv_flags = chk_flags;
2209 chk->send_size = the_len;
2211 SCTPDBG(SCTP_DEBUG_XXX, "Building ck: %p for control: %p to be read (MID: %u)\n",
2214 atomic_add_int(&net->ref_count, 1);
2217 /* Set the appropriate TSN mark */
2218 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
2219 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2220 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
2221 asoc->highest_tsn_inside_nr_map = tsn;
2224 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2225 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
2226 asoc->highest_tsn_inside_map = tsn;
2229 /* Now is it complete (i.e. not fragmented)? */
2230 if ((chk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
2232 * Special check for when streams are resetting. We
2233 * could be more smart about this and check the
2234 * actual stream to see if it is not being reset..
2235 * that way we would not create a HOLB when amongst
2236 * streams being reset and those not being reset.
2239 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2240 SCTP_TSN_GT(tsn, liste->tsn)) {
2242 * yep its past where we need to reset... go
2243 * ahead and queue it.
2245 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2247 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2249 struct sctp_queued_to_read *lcontrol, *nlcontrol;
2250 unsigned char inserted = 0;
2251 TAILQ_FOREACH_SAFE(lcontrol, &asoc->pending_reply_queue, next, nlcontrol) {
2252 if (SCTP_TSN_GT(control->sinfo_tsn, lcontrol->sinfo_tsn)) {
2257 TAILQ_INSERT_BEFORE(lcontrol, control, next);
2262 if (inserted == 0) {
2264 * must be put at end, use
2265 * prevP (all setup from
2266 * loop) to setup nextP.
2268 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2271 goto finish_express_del;
2273 if (chk_flags & SCTP_DATA_UNORDERED) {
2274 /* queue directly into socket buffer */
2275 SCTPDBG(SCTP_DEBUG_XXX, "Unordered data to be read control: %p MID: %u\n",
2277 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2278 sctp_add_to_readq(stcb->sctp_ep, stcb,
2280 &stcb->sctp_socket->so_rcv, 1,
2281 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2284 SCTPDBG(SCTP_DEBUG_XXX, "Queue control: %p for reordering MID: %u\n", control,
2286 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag, &need_reasm_check);
2294 goto finish_express_del;
2296 /* If we reach here its a reassembly */
2297 need_reasm_check = 1;
2298 SCTPDBG(SCTP_DEBUG_XXX,
2299 "Queue data to stream for reasm control: %p MID: %u\n",
2301 sctp_queue_data_for_reasm(stcb, asoc, control, chk, created_control, abort_flag, tsn);
2304 * the assoc is now gone and chk was put onto the
2305 * reasm queue, which has all been freed.
2313 /* Here we tidy up things */
2314 if (tsn == (asoc->cumulative_tsn + 1)) {
2315 /* Update cum-ack */
2316 asoc->cumulative_tsn = tsn;
2322 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2324 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2326 SCTP_STAT_INCR(sctps_recvdata);
2327 /* Set it present please */
2328 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2329 sctp_log_strm_del_alt(stcb, tsn, mid, sid, SCTP_STR_LOG_FROM_MARK_TSN);
2331 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2332 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2333 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2335 if (need_reasm_check) {
2336 (void)sctp_deliver_reasm_check(stcb, asoc, &asoc->strmin[sid], SCTP_READ_LOCK_NOT_HELD);
2337 need_reasm_check = 0;
2339 /* check the special flag for stream resets */
2340 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2341 SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
2343 * we have finished working through the backlogged TSN's now
2344 * time to reset streams. 1: call reset function. 2: free
2345 * pending_reply space 3: distribute any chunks in
2346 * pending_reply_queue.
2348 sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
2349 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2350 sctp_send_deferred_reset_response(stcb, liste, SCTP_STREAM_RESET_RESULT_PERFORMED);
2351 SCTP_FREE(liste, SCTP_M_STRESET);
2352 /*sa_ignore FREED_MEMORY*/
2353 liste = TAILQ_FIRST(&asoc->resetHead);
2354 if (TAILQ_EMPTY(&asoc->resetHead)) {
2355 /* All can be removed */
2356 TAILQ_FOREACH_SAFE(control, &asoc->pending_reply_queue, next, ncontrol) {
2357 TAILQ_REMOVE(&asoc->pending_reply_queue, control, next);
2358 strm = &asoc->strmin[control->sinfo_stream];
2359 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag, &need_reasm_check);
2363 if (need_reasm_check) {
2364 (void)sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_NOT_HELD);
2365 need_reasm_check = 0;
2369 TAILQ_FOREACH_SAFE(control, &asoc->pending_reply_queue, next, ncontrol) {
2370 if (SCTP_TSN_GT(control->sinfo_tsn, liste->tsn)) {
2374 * if control->sinfo_tsn is <= liste->tsn we can
2375 * process it which is the NOT of
2376 * control->sinfo_tsn > liste->tsn
2378 TAILQ_REMOVE(&asoc->pending_reply_queue, control, next);
2379 strm = &asoc->strmin[control->sinfo_stream];
2380 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag, &need_reasm_check);
2384 if (need_reasm_check) {
2385 (void)sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_NOT_HELD);
2386 need_reasm_check = 0;
2394 static const int8_t sctp_map_lookup_tab[256] = {
2395 0, 1, 0, 2, 0, 1, 0, 3,
2396 0, 1, 0, 2, 0, 1, 0, 4,
2397 0, 1, 0, 2, 0, 1, 0, 3,
2398 0, 1, 0, 2, 0, 1, 0, 5,
2399 0, 1, 0, 2, 0, 1, 0, 3,
2400 0, 1, 0, 2, 0, 1, 0, 4,
2401 0, 1, 0, 2, 0, 1, 0, 3,
2402 0, 1, 0, 2, 0, 1, 0, 6,
2403 0, 1, 0, 2, 0, 1, 0, 3,
2404 0, 1, 0, 2, 0, 1, 0, 4,
2405 0, 1, 0, 2, 0, 1, 0, 3,
2406 0, 1, 0, 2, 0, 1, 0, 5,
2407 0, 1, 0, 2, 0, 1, 0, 3,
2408 0, 1, 0, 2, 0, 1, 0, 4,
2409 0, 1, 0, 2, 0, 1, 0, 3,
2410 0, 1, 0, 2, 0, 1, 0, 7,
2411 0, 1, 0, 2, 0, 1, 0, 3,
2412 0, 1, 0, 2, 0, 1, 0, 4,
2413 0, 1, 0, 2, 0, 1, 0, 3,
2414 0, 1, 0, 2, 0, 1, 0, 5,
2415 0, 1, 0, 2, 0, 1, 0, 3,
2416 0, 1, 0, 2, 0, 1, 0, 4,
2417 0, 1, 0, 2, 0, 1, 0, 3,
2418 0, 1, 0, 2, 0, 1, 0, 6,
2419 0, 1, 0, 2, 0, 1, 0, 3,
2420 0, 1, 0, 2, 0, 1, 0, 4,
2421 0, 1, 0, 2, 0, 1, 0, 3,
2422 0, 1, 0, 2, 0, 1, 0, 5,
2423 0, 1, 0, 2, 0, 1, 0, 3,
2424 0, 1, 0, 2, 0, 1, 0, 4,
2425 0, 1, 0, 2, 0, 1, 0, 3,
2426 0, 1, 0, 2, 0, 1, 0, 8
2431 sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2434 * Now we also need to check the mapping array in a couple of ways.
2435 * 1) Did we move the cum-ack point?
2437 * When you first glance at this you might think
2438 * that all entries that make up the position
2439 * of the cum-ack would be in the nr-mapping array
2440 * only.. i.e. things up to the cum-ack are always
2441 * deliverable. Thats true with one exception, when
2442 * its a fragmented message we may not deliver the data
2443 * until some threshold (or all of it) is in place. So
2444 * we must OR the nr_mapping_array and mapping_array to
2445 * get a true picture of the cum-ack.
2447 struct sctp_association *asoc;
2450 int slide_from, slide_end, lgap, distance;
2451 uint32_t old_cumack, old_base, old_highest, highest_tsn;
2455 old_cumack = asoc->cumulative_tsn;
2456 old_base = asoc->mapping_array_base_tsn;
2457 old_highest = asoc->highest_tsn_inside_map;
2459 * We could probably improve this a small bit by calculating the
2460 * offset of the current cum-ack as the starting point.
2463 for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2464 val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
2468 /* there is a 0 bit */
2469 at += sctp_map_lookup_tab[val];
2473 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at-1);
2475 if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
2476 SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2478 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2479 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2481 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2482 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2483 sctp_print_mapping_array(asoc);
2484 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2485 sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2487 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2488 asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2491 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2492 highest_tsn = asoc->highest_tsn_inside_nr_map;
2494 highest_tsn = asoc->highest_tsn_inside_map;
2496 if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2497 /* The complete array was completed by a single FR */
2498 /* highest becomes the cum-ack */
2504 /* clear the array */
2505 clr = ((at+7) >> 3);
2506 if (clr > asoc->mapping_array_size) {
2507 clr = asoc->mapping_array_size;
2509 memset(asoc->mapping_array, 0, clr);
2510 memset(asoc->nr_mapping_array, 0, clr);
2512 for (i = 0; i < asoc->mapping_array_size; i++) {
2513 if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2514 SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2515 sctp_print_mapping_array(asoc);
2519 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2520 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2521 } else if (at >= 8) {
2522 /* we can slide the mapping array down */
2523 /* slide_from holds where we hit the first NON 0xff byte */
2526 * now calculate the ceiling of the move using our highest
2529 SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
2530 slide_end = (lgap >> 3);
2531 if (slide_end < slide_from) {
2532 sctp_print_mapping_array(asoc);
2534 panic("impossible slide");
2536 SCTP_PRINTF("impossible slide lgap: %x slide_end: %x slide_from: %x? at: %d\n",
2537 lgap, slide_end, slide_from, at);
2541 if (slide_end > asoc->mapping_array_size) {
2543 panic("would overrun buffer");
2545 SCTP_PRINTF("Gak, would have overrun map end: %d slide_end: %d\n",
2546 asoc->mapping_array_size, slide_end);
2547 slide_end = asoc->mapping_array_size;
2550 distance = (slide_end - slide_from) + 1;
2551 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2552 sctp_log_map(old_base, old_cumack, old_highest,
2553 SCTP_MAP_PREPARE_SLIDE);
2554 sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2555 (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2557 if (distance + slide_from > asoc->mapping_array_size ||
2560 * Here we do NOT slide forward the array so that
2561 * hopefully when more data comes in to fill it up
2562 * we will be able to slide it forward. Really I
2563 * don't think this should happen :-0
2566 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2567 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2568 (uint32_t) asoc->mapping_array_size,
2569 SCTP_MAP_SLIDE_NONE);
2574 for (ii = 0; ii < distance; ii++) {
2575 asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
2576 asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
2579 for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2580 asoc->mapping_array[ii] = 0;
2581 asoc->nr_mapping_array[ii] = 0;
2583 if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2584 asoc->highest_tsn_inside_map += (slide_from << 3);
2586 if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2587 asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2589 asoc->mapping_array_base_tsn += (slide_from << 3);
2590 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2591 sctp_log_map(asoc->mapping_array_base_tsn,
2592 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2593 SCTP_MAP_SLIDE_RESULT);
2600 sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2602 struct sctp_association *asoc;
2603 uint32_t highest_tsn;
2606 sctp_slide_mapping_arrays(stcb);
2608 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2609 highest_tsn = asoc->highest_tsn_inside_nr_map;
2611 highest_tsn = asoc->highest_tsn_inside_map;
2613 /* Is there a gap now? */
2614 is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2617 * Now we need to see if we need to queue a sack or just start the
2618 * timer (if allowed).
2620 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
2622 * Ok special case, in SHUTDOWN-SENT case. here we
2623 * maker sure SACK timer is off and instead send a
2624 * SHUTDOWN and a SACK
2626 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2627 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2628 stcb->sctp_ep, stcb, NULL,
2629 SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2631 sctp_send_shutdown(stcb,
2632 ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2634 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2638 * CMT DAC algorithm: increase number of packets
2639 * received since last ack
2641 stcb->asoc.cmt_dac_pkts_rcvd++;
2643 if ((stcb->asoc.send_sack == 1) || /* We need to send a SACK */
2644 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2646 (stcb->asoc.numduptsns) || /* we have dup's */
2647 (is_a_gap) || /* is still a gap */
2648 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2649 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2652 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2653 (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
2654 (stcb->asoc.send_sack == 0) &&
2655 (stcb->asoc.numduptsns == 0) &&
2656 (stcb->asoc.delayed_ack) &&
2657 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2660 * CMT DAC algorithm: With CMT,
2661 * delay acks even in the face of
2663 * reordering. Therefore, if acks
2664 * that do not have to be sent
2665 * because of the above reasons,
2666 * will be delayed. That is, acks
2667 * that would have been sent due to
2668 * gap reports will be delayed with
2669 * DAC. Start the delayed ack timer.
2671 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2672 stcb->sctp_ep, stcb, NULL);
2675 * Ok we must build a SACK since the
2676 * timer is pending, we got our
2677 * first packet OR there are gaps or
2680 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
2681 SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
2682 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2685 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2686 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2687 stcb->sctp_ep, stcb, NULL);
2694 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2695 struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2696 struct sctp_nets *net, uint32_t *high_tsn)
2698 struct sctp_chunkhdr *ch, chunk_buf;
2699 struct sctp_association *asoc;
2700 int num_chunks = 0; /* number of control chunks processed */
2702 int break_flag, last_chunk;
2703 int abort_flag = 0, was_a_gap;
2705 uint32_t highest_tsn;
2706 uint16_t chk_length;
2709 sctp_set_rwnd(stcb, &stcb->asoc);
2712 SCTP_TCB_LOCK_ASSERT(stcb);
2714 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2715 highest_tsn = asoc->highest_tsn_inside_nr_map;
2717 highest_tsn = asoc->highest_tsn_inside_map;
2719 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2721 * setup where we got the last DATA packet from for any SACK that
2722 * may need to go out. Don't bump the net. This is done ONLY when a
2723 * chunk is assigned.
2725 asoc->last_data_chunk_from = net;
2728 * Now before we proceed we must figure out if this is a wasted
2729 * cluster... i.e. it is a small packet sent in and yet the driver
2730 * underneath allocated a full cluster for it. If so we must copy it
2731 * to a smaller mbuf and free up the cluster mbuf. This will help
2732 * with cluster starvation.
2734 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2735 /* we only handle mbufs that are singletons.. not chains */
2736 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2738 /* ok lets see if we can copy the data up */
2740 /* get the pointers and copy */
2741 to = mtod(m, caddr_t *);
2742 from = mtod((*mm), caddr_t *);
2743 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2744 /* copy the length and free up the old */
2745 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2747 /* success, back copy */
2750 /* We are in trouble in the mbuf world .. yikes */
2754 /* get pointer to the first chunk header */
2755 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
2756 sizeof(struct sctp_chunkhdr),
2757 (uint8_t *)&chunk_buf);
2762 * process all DATA chunks...
2764 *high_tsn = asoc->cumulative_tsn;
2766 asoc->data_pkts_seen++;
2767 while (stop_proc == 0) {
2768 /* validate chunk length */
2769 chk_length = ntohs(ch->chunk_length);
2770 if (length - *offset < chk_length) {
2771 /* all done, mutulated chunk */
2775 if ((asoc->idata_supported == 1) &&
2776 (ch->chunk_type == SCTP_DATA)) {
2777 struct mbuf *op_err;
2778 char msg[SCTP_DIAG_INFO_LEN];
2780 SCTP_SNPRINTF(msg, sizeof(msg), "%s", "DATA chunk received when I-DATA was negotiated");
2781 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2782 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_21;
2783 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2786 if ((asoc->idata_supported == 0) &&
2787 (ch->chunk_type == SCTP_IDATA)) {
2788 struct mbuf *op_err;
2789 char msg[SCTP_DIAG_INFO_LEN];
2791 SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-DATA chunk received when DATA was negotiated");
2792 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2793 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_22;
2794 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2797 if ((ch->chunk_type == SCTP_DATA) ||
2798 (ch->chunk_type == SCTP_IDATA)) {
2801 if (ch->chunk_type == SCTP_DATA) {
2802 clen = sizeof(struct sctp_data_chunk);
2804 clen = sizeof(struct sctp_idata_chunk);
2806 if (chk_length < clen) {
2808 * Need to send an abort since we had a
2809 * invalid data chunk.
2811 struct mbuf *op_err;
2812 char msg[SCTP_DIAG_INFO_LEN];
2814 SCTP_SNPRINTF(msg, sizeof(msg), "%s chunk of length %u",
2815 ch->chunk_type == SCTP_DATA ? "DATA" : "I-DATA",
2817 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2818 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_23;
2819 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2822 #ifdef SCTP_AUDITING_ENABLED
2823 sctp_audit_log(0xB1, 0);
2825 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2830 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset,
2831 chk_length, net, high_tsn, &abort_flag, &break_flag,
2832 last_chunk, ch->chunk_type)) {
2840 * Set because of out of rwnd space and no
2841 * drop rep space left.
2847 /* not a data chunk in the data region */
2848 switch (ch->chunk_type) {
2849 case SCTP_INITIATION:
2850 case SCTP_INITIATION_ACK:
2851 case SCTP_SELECTIVE_ACK:
2852 case SCTP_NR_SELECTIVE_ACK:
2853 case SCTP_HEARTBEAT_REQUEST:
2854 case SCTP_HEARTBEAT_ACK:
2855 case SCTP_ABORT_ASSOCIATION:
2857 case SCTP_SHUTDOWN_ACK:
2858 case SCTP_OPERATION_ERROR:
2859 case SCTP_COOKIE_ECHO:
2860 case SCTP_COOKIE_ACK:
2863 case SCTP_SHUTDOWN_COMPLETE:
2864 case SCTP_AUTHENTICATION:
2865 case SCTP_ASCONF_ACK:
2866 case SCTP_PACKET_DROPPED:
2867 case SCTP_STREAM_RESET:
2868 case SCTP_FORWARD_CUM_TSN:
2872 * Now, what do we do with KNOWN chunks that
2873 * are NOT in the right place?
2875 * For now, I do nothing but ignore them. We
2876 * may later want to add sysctl stuff to
2877 * switch out and do either an ABORT() or
2878 * possibly process them.
2880 struct mbuf *op_err;
2881 char msg[SCTP_DIAG_INFO_LEN];
2883 SCTP_SNPRINTF(msg, sizeof(msg), "DATA chunk followed by chunk of type %2.2x",
2885 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2886 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2891 * Unknown chunk type: use bit rules after
2894 if (chk_length < sizeof(struct sctp_chunkhdr)) {
2896 * Need to send an abort since we had a
2899 struct mbuf *op_err;
2900 char msg[SCTP_DIAG_INFO_LEN];
2902 SCTP_SNPRINTF(msg, sizeof(msg), "Chunk of length %u", chk_length);
2903 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2904 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
2905 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2908 if (ch->chunk_type & 0x40) {
2909 /* Add a error report to the queue */
2910 struct mbuf *op_err;
2911 struct sctp_gen_error_cause *cause;
2913 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
2914 0, M_NOWAIT, 1, MT_DATA);
2915 if (op_err != NULL) {
2916 cause = mtod(op_err, struct sctp_gen_error_cause *);
2917 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
2918 cause->length = htons((uint16_t)(chk_length + sizeof(struct sctp_gen_error_cause)));
2919 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
2920 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
2921 if (SCTP_BUF_NEXT(op_err) != NULL) {
2922 sctp_queue_op_err(stcb, op_err);
2924 sctp_m_freem(op_err);
2928 if ((ch->chunk_type & 0x80) == 0) {
2929 /* discard the rest of this packet */
2931 } /* else skip this bad chunk and
2934 } /* switch of chunk type */
2936 *offset += SCTP_SIZE32(chk_length);
2937 if ((*offset >= length) || stop_proc) {
2938 /* no more data left in the mbuf chain */
2942 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
2943 sizeof(struct sctp_chunkhdr),
2944 (uint8_t *)&chunk_buf);
2953 * we need to report rwnd overrun drops.
2955 sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2959 * Did we get data, if so update the time for auto-close and
2960 * give peer credit for being alive.
2962 SCTP_STAT_INCR(sctps_recvpktwithdata);
2963 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2964 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2965 stcb->asoc.overall_error_count,
2967 SCTP_FROM_SCTP_INDATA,
2970 stcb->asoc.overall_error_count = 0;
2971 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2973 /* now service all of the reassm queue if needed */
2974 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
2975 /* Assure that we ack right away */
2976 stcb->asoc.send_sack = 1;
2978 /* Start a sack timer or QUEUE a SACK for sending */
2979 sctp_sack_check(stcb, was_a_gap);
2984 sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
2985 uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
2987 uint32_t *biggest_newly_acked_tsn,
2988 uint32_t *this_sack_lowest_newack,
2991 struct sctp_tmit_chunk *tp1;
2992 unsigned int theTSN;
2993 int j, wake_him = 0, circled = 0;
2995 /* Recover the tp1 we last saw */
2998 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3000 for (j = frag_strt; j <= frag_end; j++) {
3001 theTSN = j + last_tsn;
3003 if (tp1->rec.data.doing_fast_retransmit)
3007 * CMT: CUCv2 algorithm. For each TSN being
3008 * processed from the sent queue, track the
3009 * next expected pseudo-cumack, or
3010 * rtx_pseudo_cumack, if required. Separate
3011 * cumack trackers for first transmissions,
3012 * and retransmissions.
3014 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3015 (tp1->whoTo->find_pseudo_cumack == 1) &&
3016 (tp1->snd_count == 1)) {
3017 tp1->whoTo->pseudo_cumack = tp1->rec.data.tsn;
3018 tp1->whoTo->find_pseudo_cumack = 0;
3020 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3021 (tp1->whoTo->find_rtx_pseudo_cumack == 1) &&
3022 (tp1->snd_count > 1)) {
3023 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.tsn;
3024 tp1->whoTo->find_rtx_pseudo_cumack = 0;
3026 if (tp1->rec.data.tsn == theTSN) {
3027 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
3029 * must be held until
3032 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3034 * If it is less than RESEND, it is
3035 * now no-longer in flight.
3036 * Higher values may already be set
3037 * via previous Gap Ack Blocks...
3038 * i.e. ACKED or RESEND.
3040 if (SCTP_TSN_GT(tp1->rec.data.tsn,
3041 *biggest_newly_acked_tsn)) {
3042 *biggest_newly_acked_tsn = tp1->rec.data.tsn;
3045 * CMT: SFR algo (and HTNA) - set
3046 * saw_newack to 1 for dest being
3047 * newly acked. update
3048 * this_sack_highest_newack if
3051 if (tp1->rec.data.chunk_was_revoked == 0)
3052 tp1->whoTo->saw_newack = 1;
3054 if (SCTP_TSN_GT(tp1->rec.data.tsn,
3055 tp1->whoTo->this_sack_highest_newack)) {
3056 tp1->whoTo->this_sack_highest_newack =
3060 * CMT DAC algo: also update
3061 * this_sack_lowest_newack
3063 if (*this_sack_lowest_newack == 0) {
3064 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3065 sctp_log_sack(*this_sack_lowest_newack,
3070 SCTP_LOG_TSN_ACKED);
3072 *this_sack_lowest_newack = tp1->rec.data.tsn;
3075 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
3076 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
3077 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
3078 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
3079 * Separate pseudo_cumack trackers for first transmissions and
3082 if (tp1->rec.data.tsn == tp1->whoTo->pseudo_cumack) {
3083 if (tp1->rec.data.chunk_was_revoked == 0) {
3084 tp1->whoTo->new_pseudo_cumack = 1;
3086 tp1->whoTo->find_pseudo_cumack = 1;
3088 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3089 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.tsn, SCTP_CWND_LOG_FROM_SACK);
3091 if (tp1->rec.data.tsn == tp1->whoTo->rtx_pseudo_cumack) {
3092 if (tp1->rec.data.chunk_was_revoked == 0) {
3093 tp1->whoTo->new_pseudo_cumack = 1;
3095 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3097 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3098 sctp_log_sack(*biggest_newly_acked_tsn,
3103 SCTP_LOG_TSN_ACKED);
3105 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3106 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3107 tp1->whoTo->flight_size,
3109 (uint32_t)(uintptr_t)tp1->whoTo,
3112 sctp_flight_size_decrease(tp1);
3113 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3114 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged)(tp1->whoTo,
3117 sctp_total_flight_decrease(stcb, tp1);
3119 tp1->whoTo->net_ack += tp1->send_size;
3120 if (tp1->snd_count < 2) {
3122 * True non-retransmitted chunk
3124 tp1->whoTo->net_ack2 += tp1->send_size;
3131 sctp_calculate_rto(stcb,
3134 &tp1->sent_rcv_time,
3135 SCTP_RTT_FROM_DATA)) {
3138 if (tp1->whoTo->rto_needed == 0) {
3139 tp1->whoTo->rto_needed = 1;
3146 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3147 if (SCTP_TSN_GT(tp1->rec.data.tsn,
3148 stcb->asoc.this_sack_highest_gap)) {
3149 stcb->asoc.this_sack_highest_gap =
3152 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3153 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
3154 #ifdef SCTP_AUDITING_ENABLED
3155 sctp_audit_log(0xB2,
3156 (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
3161 * All chunks NOT UNSENT fall through here and are marked
3162 * (leave PR-SCTP ones that are to skip alone though)
3164 if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
3165 (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
3166 tp1->sent = SCTP_DATAGRAM_MARKED;
3168 if (tp1->rec.data.chunk_was_revoked) {
3169 /* deflate the cwnd */
3170 tp1->whoTo->cwnd -= tp1->book_size;
3171 tp1->rec.data.chunk_was_revoked = 0;
3173 /* NR Sack code here */
3175 (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
3176 if (stcb->asoc.strmout[tp1->rec.data.sid].chunks_on_queues > 0) {
3177 stcb->asoc.strmout[tp1->rec.data.sid].chunks_on_queues--;
3180 panic("No chunks on the queues for sid %u.", tp1->rec.data.sid);
3183 if ((stcb->asoc.strmout[tp1->rec.data.sid].chunks_on_queues == 0) &&
3184 (stcb->asoc.strmout[tp1->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
3185 TAILQ_EMPTY(&stcb->asoc.strmout[tp1->rec.data.sid].outqueue)) {
3186 stcb->asoc.trigger_reset = 1;
3188 tp1->sent = SCTP_DATAGRAM_NR_ACKED;
3190 /* sa_ignore NO_NULL_CHK */
3191 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
3192 sctp_m_freem(tp1->data);
3199 } /* if (tp1->tsn == theTSN) */
3200 if (SCTP_TSN_GT(tp1->rec.data.tsn, theTSN)) {
3203 tp1 = TAILQ_NEXT(tp1, sctp_next);
3204 if ((tp1 == NULL) && (circled == 0)) {
3206 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3208 } /* end while (tp1) */
3211 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3213 /* In case the fragments were not in order we must reset */
3214 } /* end for (j = fragStart */
3216 return (wake_him); /* Return value only used for nr-sack */
3221 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3222 uint32_t last_tsn, uint32_t *biggest_tsn_acked,
3223 uint32_t *biggest_newly_acked_tsn, uint32_t *this_sack_lowest_newack,
3224 int num_seg, int num_nr_seg, int *rto_ok)
3226 struct sctp_gap_ack_block *frag, block;
3227 struct sctp_tmit_chunk *tp1;
3232 uint16_t frag_strt, frag_end, prev_frag_end;
3234 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3238 for (i = 0; i < (num_seg + num_nr_seg); i++) {
3241 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3243 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3244 sizeof(struct sctp_gap_ack_block), (uint8_t *) &block);
3245 *offset += sizeof(block);
3247 return (chunk_freed);
3249 frag_strt = ntohs(frag->start);
3250 frag_end = ntohs(frag->end);
3252 if (frag_strt > frag_end) {
3253 /* This gap report is malformed, skip it. */
3256 if (frag_strt <= prev_frag_end) {
3257 /* This gap report is not in order, so restart. */
3258 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3260 if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3261 *biggest_tsn_acked = last_tsn + frag_end;
3268 if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3269 non_revocable, &num_frs, biggest_newly_acked_tsn,
3270 this_sack_lowest_newack, rto_ok)) {
3273 prev_frag_end = frag_end;
3275 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3277 sctp_log_fr(*biggest_tsn_acked,
3278 *biggest_newly_acked_tsn,
3279 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3281 return (chunk_freed);
3285 sctp_check_for_revoked(struct sctp_tcb *stcb,
3286 struct sctp_association *asoc, uint32_t cumack,
3287 uint32_t biggest_tsn_acked)
3289 struct sctp_tmit_chunk *tp1;
3291 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3292 if (SCTP_TSN_GT(tp1->rec.data.tsn, cumack)) {
3294 * ok this guy is either ACK or MARKED. If it is
3295 * ACKED it has been previously acked but not this
3296 * time i.e. revoked. If it is MARKED it was ACK'ed
3299 if (SCTP_TSN_GT(tp1->rec.data.tsn, biggest_tsn_acked)) {
3302 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3303 /* it has been revoked */
3304 tp1->sent = SCTP_DATAGRAM_SENT;
3305 tp1->rec.data.chunk_was_revoked = 1;
3306 /* We must add this stuff back in to
3307 * assure timers and such get started.
3309 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3310 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3311 tp1->whoTo->flight_size,
3313 (uint32_t)(uintptr_t)tp1->whoTo,
3316 sctp_flight_size_increase(tp1);
3317 sctp_total_flight_increase(stcb, tp1);
3318 /* We inflate the cwnd to compensate for our
3319 * artificial inflation of the flight_size.
3321 tp1->whoTo->cwnd += tp1->book_size;
3322 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3323 sctp_log_sack(asoc->last_acked_seq,
3328 SCTP_LOG_TSN_REVOKED);
3330 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3331 /* it has been re-acked in this SACK */
3332 tp1->sent = SCTP_DATAGRAM_ACKED;
3335 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3342 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3343 uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3345 struct sctp_tmit_chunk *tp1;
3346 int strike_flag = 0;
3348 int tot_retrans = 0;
3349 uint32_t sending_seq;
3350 struct sctp_nets *net;
3351 int num_dests_sacked = 0;
3354 * select the sending_seq, this is either the next thing ready to be
3355 * sent but not transmitted, OR, the next seq we assign.
3357 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3359 sending_seq = asoc->sending_seq;
3361 sending_seq = tp1->rec.data.tsn;
3364 /* CMT DAC algo: finding out if SACK is a mixed SACK */
3365 if ((asoc->sctp_cmt_on_off > 0) &&
3366 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3367 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3368 if (net->saw_newack)
3372 if (stcb->asoc.prsctp_supported) {
3373 (void)SCTP_GETTIME_TIMEVAL(&now);
3375 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3377 if (tp1->no_fr_allowed) {
3378 /* this one had a timeout or something */
3381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3382 if (tp1->sent < SCTP_DATAGRAM_RESEND)
3383 sctp_log_fr(biggest_tsn_newly_acked,
3386 SCTP_FR_LOG_CHECK_STRIKE);
3388 if (SCTP_TSN_GT(tp1->rec.data.tsn, biggest_tsn_acked) ||
3389 tp1->sent == SCTP_DATAGRAM_UNSENT) {
3393 if (stcb->asoc.prsctp_supported) {
3394 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3395 /* Is it expired? */
3396 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
3397 if (timercmp(&now, &tp1->rec.data.timetodrop, >)) {
3399 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3401 /* Yes so drop it */
3402 if (tp1->data != NULL) {
3403 (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3404 SCTP_SO_NOT_LOCKED);
3411 if (SCTP_TSN_GT(tp1->rec.data.tsn, asoc->this_sack_highest_gap) &&
3412 !(accum_moved && asoc->fast_retran_loss_recovery)) {
3413 /* we are beyond the tsn in the sack */
3416 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3417 /* either a RESEND, ACKED, or MARKED */
3419 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3420 /* Continue strikin FWD-TSN chunks */
3421 tp1->rec.data.fwd_tsn_cnt++;
3426 * CMT : SFR algo (covers part of DAC and HTNA as well)
3428 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3430 * No new acks were receieved for data sent to this
3431 * dest. Therefore, according to the SFR algo for
3432 * CMT, no data sent to this dest can be marked for
3433 * FR using this SACK.
3436 } else if (tp1->whoTo &&
3437 SCTP_TSN_GT(tp1->rec.data.tsn,
3438 tp1->whoTo->this_sack_highest_newack) &&
3439 !(accum_moved && asoc->fast_retran_loss_recovery)) {
3441 * CMT: New acks were receieved for data sent to
3442 * this dest. But no new acks were seen for data
3443 * sent after tp1. Therefore, according to the SFR
3444 * algo for CMT, tp1 cannot be marked for FR using
3445 * this SACK. This step covers part of the DAC algo
3446 * and the HTNA algo as well.
3451 * Here we check to see if we were have already done a FR
3452 * and if so we see if the biggest TSN we saw in the sack is
3453 * smaller than the recovery point. If so we don't strike
3454 * the tsn... otherwise we CAN strike the TSN.
3457 * @@@ JRI: Check for CMT
3458 * if (accum_moved && asoc->fast_retran_loss_recovery && (sctp_cmt_on_off == 0)) {
3460 if (accum_moved && asoc->fast_retran_loss_recovery) {
3462 * Strike the TSN if in fast-recovery and cum-ack
3465 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3466 sctp_log_fr(biggest_tsn_newly_acked,
3469 SCTP_FR_LOG_STRIKE_CHUNK);
3471 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3474 if ((asoc->sctp_cmt_on_off > 0) &&
3475 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3477 * CMT DAC algorithm: If SACK flag is set to
3478 * 0, then lowest_newack test will not pass
3479 * because it would have been set to the
3480 * cumack earlier. If not already to be
3481 * rtx'd, If not a mixed sack and if tp1 is
3482 * not between two sacked TSNs, then mark by
3484 * NOTE that we are marking by one additional time since the SACK DAC flag indicates that
3485 * two packets have been received after this missing TSN.
3487 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3488 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.tsn)) {
3489 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3490 sctp_log_fr(16 + num_dests_sacked,
3493 SCTP_FR_LOG_STRIKE_CHUNK);
3498 } else if ((tp1->rec.data.doing_fast_retransmit) &&
3499 (asoc->sctp_cmt_on_off == 0)) {
3501 * For those that have done a FR we must take
3502 * special consideration if we strike. I.e the
3503 * biggest_newly_acked must be higher than the
3504 * sending_seq at the time we did the FR.
3507 #ifdef SCTP_FR_TO_ALTERNATE
3509 * If FR's go to new networks, then we must only do
3510 * this for singly homed asoc's. However if the FR's
3511 * go to the same network (Armando's work) then its
3512 * ok to FR multiple times.
3520 if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3521 tp1->rec.data.fast_retran_tsn)) {
3523 * Strike the TSN, since this ack is
3524 * beyond where things were when we
3527 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3528 sctp_log_fr(biggest_tsn_newly_acked,
3531 SCTP_FR_LOG_STRIKE_CHUNK);
3533 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3537 if ((asoc->sctp_cmt_on_off > 0) &&
3538 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3540 * CMT DAC algorithm: If
3541 * SACK flag is set to 0,
3542 * then lowest_newack test
3543 * will not pass because it
3544 * would have been set to
3545 * the cumack earlier. If
3546 * not already to be rtx'd,
3547 * If not a mixed sack and
3548 * if tp1 is not between two
3549 * sacked TSNs, then mark by
3551 * NOTE that we are marking by one additional time since the SACK DAC flag indicates that
3552 * two packets have been received after this missing TSN.
3554 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3555 (num_dests_sacked == 1) &&
3556 SCTP_TSN_GT(this_sack_lowest_newack,
3557 tp1->rec.data.tsn)) {
3558 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3559 sctp_log_fr(32 + num_dests_sacked,
3562 SCTP_FR_LOG_STRIKE_CHUNK);
3564 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3572 * JRI: TODO: remove code for HTNA algo. CMT's
3573 * SFR algo covers HTNA.
3575 } else if (SCTP_TSN_GT(tp1->rec.data.tsn,
3576 biggest_tsn_newly_acked)) {
3578 * We don't strike these: This is the HTNA
3579 * algorithm i.e. we don't strike If our TSN is
3580 * larger than the Highest TSN Newly Acked.
3584 /* Strike the TSN */
3585 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3586 sctp_log_fr(biggest_tsn_newly_acked,
3589 SCTP_FR_LOG_STRIKE_CHUNK);
3591 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3594 if ((asoc->sctp_cmt_on_off > 0) &&
3595 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3597 * CMT DAC algorithm: If SACK flag is set to
3598 * 0, then lowest_newack test will not pass
3599 * because it would have been set to the
3600 * cumack earlier. If not already to be
3601 * rtx'd, If not a mixed sack and if tp1 is
3602 * not between two sacked TSNs, then mark by
3604 * NOTE that we are marking by one additional time since the SACK DAC flag indicates that
3605 * two packets have been received after this missing TSN.
3607 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3608 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.tsn)) {
3609 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3610 sctp_log_fr(48 + num_dests_sacked,
3613 SCTP_FR_LOG_STRIKE_CHUNK);
3619 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3620 struct sctp_nets *alt;
3622 /* fix counts and things */
3623 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3624 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3625 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3627 (uint32_t)(uintptr_t)tp1->whoTo,
3631 tp1->whoTo->net_ack++;
3632 sctp_flight_size_decrease(tp1);
3633 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3634 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged)(tp1->whoTo,
3639 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3640 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3641 asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3643 /* add back to the rwnd */
3644 asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3646 /* remove from the total flight */
3647 sctp_total_flight_decrease(stcb, tp1);
3649 if ((stcb->asoc.prsctp_supported) &&
3650 (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3651 /* Has it been retransmitted tv_sec times? - we store the retran count there. */
3652 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3653 /* Yes, so drop it */
3654 if (tp1->data != NULL) {
3655 (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3656 SCTP_SO_NOT_LOCKED);
3658 /* Make sure to flag we had a FR */
3659 if (tp1->whoTo != NULL) {
3660 tp1->whoTo->net_ack++;
3665 /* SCTP_PRINTF("OK, we are now ready to FR this guy\n"); */
3666 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3667 sctp_log_fr(tp1->rec.data.tsn, tp1->snd_count,
3671 /* This is a subsequent FR */
3672 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3674 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3675 if (asoc->sctp_cmt_on_off > 0) {
3677 * CMT: Using RTX_SSTHRESH policy for CMT.
3678 * If CMT is being used, then pick dest with
3679 * largest ssthresh for any retransmission.
3681 tp1->no_fr_allowed = 1;
3683 /*sa_ignore NO_NULL_CHK*/
3684 if (asoc->sctp_cmt_pf > 0) {
3685 /* JRS 5/18/07 - If CMT PF is on, use the PF version of find_alt_net() */
3686 alt = sctp_find_alternate_net(stcb, alt, 2);
3688 /* JRS 5/18/07 - If only CMT is on, use the CMT version of find_alt_net() */
3689 /*sa_ignore NO_NULL_CHK*/
3690 alt = sctp_find_alternate_net(stcb, alt, 1);
3696 * CUCv2: If a different dest is picked for
3697 * the retransmission, then new
3698 * (rtx-)pseudo_cumack needs to be tracked
3699 * for orig dest. Let CUCv2 track new (rtx-)
3700 * pseudo-cumack always.
3703 tp1->whoTo->find_pseudo_cumack = 1;
3704 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3707 } else {/* CMT is OFF */
3709 #ifdef SCTP_FR_TO_ALTERNATE
3710 /* Can we find an alternate? */
3711 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3714 * default behavior is to NOT retransmit
3715 * FR's to an alternate. Armando Caro's
3716 * paper details why.
3722 tp1->rec.data.doing_fast_retransmit = 1;
3724 /* mark the sending seq for possible subsequent FR's */
3726 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3727 * (uint32_t)tpi->rec.data.tsn);
3729 if (TAILQ_EMPTY(&asoc->send_queue)) {
3731 * If the queue of send is empty then its
3732 * the next sequence number that will be
3733 * assigned so we subtract one from this to
3734 * get the one we last sent.
3736 tp1->rec.data.fast_retran_tsn = sending_seq;
3739 * If there are chunks on the send queue
3740 * (unsent data that has made it from the
3741 * stream queues but not out the door, we
3742 * take the first one (which will have the
3743 * lowest TSN) and subtract one to get the
3746 struct sctp_tmit_chunk *ttt;
3748 ttt = TAILQ_FIRST(&asoc->send_queue);
3749 tp1->rec.data.fast_retran_tsn =
3755 * this guy had a RTO calculation pending on
3758 if ((tp1->whoTo != NULL) &&
3759 (tp1->whoTo->rto_needed == 0)) {
3760 tp1->whoTo->rto_needed = 1;
3764 if (alt != tp1->whoTo) {
3765 /* yes, there is an alternate. */
3766 sctp_free_remote_addr(tp1->whoTo);
3767 /*sa_ignore FREED_MEMORY*/
3769 atomic_add_int(&alt->ref_count, 1);
3775 struct sctp_tmit_chunk *
3776 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3777 struct sctp_association *asoc)
3779 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3783 if (asoc->prsctp_supported == 0) {
3786 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3787 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3788 tp1->sent != SCTP_DATAGRAM_RESEND &&
3789 tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3790 /* no chance to advance, out of here */
3793 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
3794 if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3795 (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3796 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
3797 asoc->advanced_peer_ack_point,
3798 tp1->rec.data.tsn, 0, 0);
3801 if (!PR_SCTP_ENABLED(tp1->flags)) {
3803 * We can't fwd-tsn past any that are reliable aka
3804 * retransmitted until the asoc fails.
3809 (void)SCTP_GETTIME_TIMEVAL(&now);
3813 * now we got a chunk which is marked for another
3814 * retransmission to a PR-stream but has run out its chances
3815 * already maybe OR has been marked to skip now. Can we skip
3816 * it if its a resend?
3818 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3819 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3821 * Now is this one marked for resend and its time is
3824 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
3825 if (timercmp(&now, &tp1->rec.data.timetodrop, >)) {
3827 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3829 /* Yes so drop it */
3831 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3832 1, SCTP_SO_NOT_LOCKED);
3836 * No, we are done when hit one for resend
3837 * whos time as not expired.
3843 * Ok now if this chunk is marked to drop it we can clean up
3844 * the chunk, advance our peer ack point and we can check
3847 if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3848 (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3849 /* advance PeerAckPoint goes forward */
3850 if (SCTP_TSN_GT(tp1->rec.data.tsn, asoc->advanced_peer_ack_point)) {
3851 asoc->advanced_peer_ack_point = tp1->rec.data.tsn;
3853 } else if (tp1->rec.data.tsn == asoc->advanced_peer_ack_point) {
3854 /* No update but we do save the chk */
3859 * If it is still in RESEND we can advance no
3869 sctp_fs_audit(struct sctp_association *asoc)
3871 struct sctp_tmit_chunk *chk;
3872 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3875 int entry_flight, entry_cnt;
3880 entry_flight = asoc->total_flight;
3881 entry_cnt = asoc->total_flight_count;
3883 if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
3886 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3887 if (chk->sent < SCTP_DATAGRAM_RESEND) {
3888 SCTP_PRINTF("Chk TSN: %u size: %d inflight cnt: %d\n",
3893 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3895 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3897 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3904 if ((inflight > 0) || (inbetween > 0)) {
3906 panic("Flight size-express incorrect? \n");
3908 SCTP_PRINTF("asoc->total_flight: %d cnt: %d\n",
3909 entry_flight, entry_cnt);
3911 SCTP_PRINTF("Flight size-express incorrect F: %d I: %d R: %d Ab: %d ACK: %d\n",
3912 inflight, inbetween, resend, above, acked);
3921 sctp_window_probe_recovery(struct sctp_tcb *stcb,
3922 struct sctp_association *asoc,
3923 struct sctp_tmit_chunk *tp1)
3925 tp1->window_probe = 0;
3926 if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3927 /* TSN's skipped we do NOT move back. */
3928 sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3929 tp1->whoTo ? tp1->whoTo->flight_size : 0,
3931 (uint32_t)(uintptr_t)tp1->whoTo,
3935 /* First setup this by shrinking flight */
3936 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3937 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged)(tp1->whoTo,
3940 sctp_flight_size_decrease(tp1);
3941 sctp_total_flight_decrease(stcb, tp1);
3942 /* Now mark for resend */
3943 tp1->sent = SCTP_DATAGRAM_RESEND;
3944 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3946 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3947 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3948 tp1->whoTo->flight_size,
3950 (uint32_t)(uintptr_t)tp1->whoTo,
3956 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3957 uint32_t rwnd, int *abort_now, int ecne_seen)
3959 struct sctp_nets *net;
3960 struct sctp_association *asoc;
3961 struct sctp_tmit_chunk *tp1, *tp2;
3963 int win_probe_recovery = 0;
3964 int win_probe_recovered = 0;
3965 int j, done_once = 0;
3969 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3970 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3971 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3973 SCTP_TCB_LOCK_ASSERT(stcb);
3974 #ifdef SCTP_ASOCLOG_OF_TSNS
3975 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3976 stcb->asoc.cumack_log_at++;
3977 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3978 stcb->asoc.cumack_log_at = 0;
3982 old_rwnd = asoc->peers_rwnd;
3983 if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
3986 } else if (asoc->last_acked_seq == cumack) {
3987 /* Window update sack */
3988 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3989 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3990 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3991 /* SWS sender side engages */
3992 asoc->peers_rwnd = 0;
3994 if (asoc->peers_rwnd > old_rwnd) {
4000 /* First setup for CC stuff */
4001 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4002 if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
4003 /* Drag along the window_tsn for cwr's */
4004 net->cwr_window_tsn = cumack;
4006 net->prev_cwnd = net->cwnd;
4011 * CMT: Reset CUC and Fast recovery algo variables before
4014 net->new_pseudo_cumack = 0;
4015 net->will_exit_fast_recovery = 0;
4016 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4017 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack)(stcb, net);
4020 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4021 tp1 = TAILQ_LAST(&asoc->sent_queue,
4022 sctpchunk_listhead);
4023 send_s = tp1->rec.data.tsn + 1;
4025 send_s = asoc->sending_seq;
4027 if (SCTP_TSN_GE(cumack, send_s)) {
4028 struct mbuf *op_err;
4029 char msg[SCTP_DIAG_INFO_LEN];
4033 SCTP_SNPRINTF(msg, sizeof(msg),
4034 "Cum ack %8.8x greater or equal than TSN %8.8x",
4036 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4037 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4038 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4041 asoc->this_sack_highest_gap = cumack;
4042 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4043 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4044 stcb->asoc.overall_error_count,
4046 SCTP_FROM_SCTP_INDATA,
4049 stcb->asoc.overall_error_count = 0;
4050 if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
4051 /* process the new consecutive TSN first */
4052 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
4053 if (SCTP_TSN_GE(cumack, tp1->rec.data.tsn)) {
4054 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4055 SCTP_PRINTF("Warning, an unsent is now acked?\n");
4057 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4059 * If it is less than ACKED, it is
4060 * now no-longer in flight. Higher
4061 * values may occur during marking
4063 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4064 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4065 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4066 tp1->whoTo->flight_size,
4068 (uint32_t)(uintptr_t)tp1->whoTo,
4071 sctp_flight_size_decrease(tp1);
4072 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4073 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged)(tp1->whoTo,
4076 /* sa_ignore NO_NULL_CHK */
4077 sctp_total_flight_decrease(stcb, tp1);
4079 tp1->whoTo->net_ack += tp1->send_size;
4080 if (tp1->snd_count < 2) {
4082 * True non-retransmitted
4085 tp1->whoTo->net_ack2 +=
4088 /* update RTO too? */
4091 sctp_calculate_rto(stcb,
4094 &tp1->sent_rcv_time,
4095 SCTP_RTT_FROM_DATA)) {
4098 if (tp1->whoTo->rto_needed == 0) {
4099 tp1->whoTo->rto_needed = 1;
4105 * CMT: CUCv2 algorithm. From the
4106 * cumack'd TSNs, for each TSN being
4107 * acked for the first time, set the
4108 * following variables for the
4109 * corresp destination.
4110 * new_pseudo_cumack will trigger a
4112 * find_(rtx_)pseudo_cumack will
4113 * trigger search for the next
4114 * expected (rtx-)pseudo-cumack.
4116 tp1->whoTo->new_pseudo_cumack = 1;
4117 tp1->whoTo->find_pseudo_cumack = 1;
4118 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4120 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4121 /* sa_ignore NO_NULL_CHK */
4122 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.tsn, SCTP_CWND_LOG_FROM_SACK);
4125 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4126 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4128 if (tp1->rec.data.chunk_was_revoked) {
4129 /* deflate the cwnd */
4130 tp1->whoTo->cwnd -= tp1->book_size;
4131 tp1->rec.data.chunk_was_revoked = 0;
4133 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4134 if (asoc->strmout[tp1->rec.data.sid].chunks_on_queues > 0) {
4135 asoc->strmout[tp1->rec.data.sid].chunks_on_queues--;
4138 panic("No chunks on the queues for sid %u.", tp1->rec.data.sid);
4142 if ((asoc->strmout[tp1->rec.data.sid].chunks_on_queues == 0) &&
4143 (asoc->strmout[tp1->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
4144 TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.sid].outqueue)) {
4145 asoc->trigger_reset = 1;
4147 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4149 /* sa_ignore NO_NULL_CHK */
4150 sctp_free_bufspace(stcb, asoc, tp1, 1);
4151 sctp_m_freem(tp1->data);
4154 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4155 sctp_log_sack(asoc->last_acked_seq,
4160 SCTP_LOG_FREE_SENT);
4162 asoc->sent_queue_cnt--;
4163 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4170 #if defined(__Userspace__)
4171 if (stcb->sctp_ep->recv_callback) {
4172 if (stcb->sctp_socket) {
4173 uint32_t inqueue_bytes, sb_free_now;
4174 struct sctp_inpcb *inp;
4176 inp = stcb->sctp_ep;
4177 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
4178 sb_free_now = SCTP_SB_LIMIT_SND(stcb->sctp_socket) - (inqueue_bytes + stcb->asoc.sb_send_resv);
4180 /* check if the amount free in the send socket buffer crossed the threshold */
4181 if (inp->send_callback &&
4182 (((inp->send_sb_threshold > 0) &&
4183 (sb_free_now >= inp->send_sb_threshold) &&
4184 (stcb->asoc.chunks_on_out_queue <= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) ||
4185 (inp->send_sb_threshold == 0))) {
4186 atomic_add_int(&stcb->asoc.refcnt, 1);
4187 SCTP_TCB_UNLOCK(stcb);
4188 inp->send_callback(stcb->sctp_socket, sb_free_now);
4189 SCTP_TCB_LOCK(stcb);
4190 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4193 } else if (stcb->sctp_socket) {
4195 /* sa_ignore NO_NULL_CHK */
4196 if (stcb->sctp_socket) {
4198 #if defined(__APPLE__) && !defined(__Userspace__)
4202 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4203 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4204 /* sa_ignore NO_NULL_CHK */
4205 sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
4207 #if defined(__APPLE__) && !defined(__Userspace__)
4208 so = SCTP_INP_SO(stcb->sctp_ep);
4209 atomic_add_int(&stcb->asoc.refcnt, 1);
4210 SCTP_TCB_UNLOCK(stcb);
4211 SCTP_SOCKET_LOCK(so, 1);
4212 SCTP_TCB_LOCK(stcb);
4213 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4214 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4215 /* assoc was freed while we were unlocked */
4216 SCTP_SOCKET_UNLOCK(so, 1);
4220 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4221 #if defined(__APPLE__) && !defined(__Userspace__)
4222 SCTP_SOCKET_UNLOCK(so, 1);
4225 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4226 sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
4230 /* JRS - Use the congestion control given in the CC module */
4231 if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4232 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4233 if (net->net_ack2 > 0) {
4235 * Karn's rule applies to clearing error count, this
4238 net->error_count = 0;
4239 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4240 /* addr came good */
4241 net->dest_state |= SCTP_ADDR_REACHABLE;
4242 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4243 0, (void *)net, SCTP_SO_NOT_LOCKED);
4245 if (net == stcb->asoc.primary_destination) {
4246 if (stcb->asoc.alternate) {
4247 /* release the alternate, primary is good */
4248 sctp_free_remote_addr(stcb->asoc.alternate);
4249 stcb->asoc.alternate = NULL;
4252 if (net->dest_state & SCTP_ADDR_PF) {
4253 net->dest_state &= ~SCTP_ADDR_PF;
4254 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4255 stcb->sctp_ep, stcb, net,
4256 SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4257 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4258 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4259 /* Done with this net */
4262 /* restore any doubled timers */
4263 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4264 if (net->RTO < stcb->asoc.minrto) {
4265 net->RTO = stcb->asoc.minrto;
4267 if (net->RTO > stcb->asoc.maxrto) {
4268 net->RTO = stcb->asoc.maxrto;
4272 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4274 asoc->last_acked_seq = cumack;
4276 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4277 /* nothing left in-flight */
4278 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4279 net->flight_size = 0;
4280 net->partial_bytes_acked = 0;
4282 asoc->total_flight = 0;
4283 asoc->total_flight_count = 0;
4287 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4288 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4289 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4290 /* SWS sender side engages */
4291 asoc->peers_rwnd = 0;
4293 if (asoc->peers_rwnd > old_rwnd) {
4294 win_probe_recovery = 1;
4296 /* Now assure a timer where data is queued at */
4299 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4300 if (win_probe_recovery && (net->window_probe)) {
4301 win_probe_recovered = 1;
4303 * Find first chunk that was used with window probe
4304 * and clear the sent
4306 /* sa_ignore FREED_MEMORY */
4307 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4308 if (tp1->window_probe) {
4309 /* move back to data send queue */
4310 sctp_window_probe_recovery(stcb, asoc, tp1);
4315 if (net->flight_size) {
4317 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net);
4318 if (net->window_probe) {
4319 net->window_probe = 0;
4322 if (net->window_probe) {
4323 /* In window probes we must assure a timer is still running there */
4324 net->window_probe = 0;
4325 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4326 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net);
4328 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4329 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4331 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4336 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4337 (asoc->sent_queue_retran_cnt == 0) &&
4338 (win_probe_recovered == 0) &&
4340 /* huh, this should not happen unless all packets
4341 * are PR-SCTP and marked to skip of course.
4343 if (sctp_fs_audit(asoc)) {
4344 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4345 net->flight_size = 0;
4347 asoc->total_flight = 0;
4348 asoc->total_flight_count = 0;
4349 asoc->sent_queue_retran_cnt = 0;
4350 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4351 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4352 sctp_flight_size_increase(tp1);
4353 sctp_total_flight_increase(stcb, tp1);
4354 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4355 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4362 /**********************************/
4363 /* Now what about shutdown issues */
4364 /**********************************/
4365 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4366 /* nothing left on sendqueue.. consider done */
4368 if ((asoc->stream_queue_cnt == 1) &&
4369 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4370 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4371 ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc))) {
4372 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
4374 if (((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4375 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4376 (asoc->stream_queue_cnt == 1) &&
4377 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4378 struct mbuf *op_err;
4382 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4383 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_28;
4384 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4387 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4388 (asoc->stream_queue_cnt == 0)) {
4389 struct sctp_nets *netp;
4391 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4392 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4393 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4395 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
4396 sctp_stop_timers_for_shutdown(stcb);
4397 if (asoc->alternate) {
4398 netp = asoc->alternate;
4400 netp = asoc->primary_destination;
4402 sctp_send_shutdown(stcb, netp);
4403 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4404 stcb->sctp_ep, stcb, netp);
4405 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4406 stcb->sctp_ep, stcb, NULL);
4407 } else if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4408 (asoc->stream_queue_cnt == 0)) {
4409 struct sctp_nets *netp;
4411 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4412 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
4413 sctp_stop_timers_for_shutdown(stcb);
4414 if (asoc->alternate) {
4415 netp = asoc->alternate;
4417 netp = asoc->primary_destination;
4419 sctp_send_shutdown_ack(stcb, netp);
4420 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4421 stcb->sctp_ep, stcb, netp);
4424 /*********************************************/
4425 /* Here we perform PR-SCTP procedures */
4427 /*********************************************/
4428 /* C1. update advancedPeerAckPoint */
4429 if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4430 asoc->advanced_peer_ack_point = cumack;
4432 /* PR-Sctp issues need to be addressed too */
4433 if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
4434 struct sctp_tmit_chunk *lchk;
4435 uint32_t old_adv_peer_ack_point;
4437 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4438 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4439 /* C3. See if we need to send a Fwd-TSN */
4440 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4442 * ISSUE with ECN, see FWD-TSN processing.
4444 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4445 send_forward_tsn(stcb, asoc);
4447 /* try to FR fwd-tsn's that get lost too */
4448 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
4449 send_forward_tsn(stcb, asoc);
4453 for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
4454 if (lchk->whoTo != NULL) {
4459 /* Assure a timer is up */
4460 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4461 stcb->sctp_ep, stcb, lchk->whoTo);
4464 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4465 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4467 stcb->asoc.peers_rwnd,
4468 stcb->asoc.total_flight,
4469 stcb->asoc.total_output_queue_size);
4474 sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
4475 struct sctp_tcb *stcb,
4476 uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4477 int *abort_now, uint8_t flags,
4478 uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4480 struct sctp_association *asoc;
4481 struct sctp_tmit_chunk *tp1, *tp2;
4482 uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4483 uint16_t wake_him = 0;
4484 uint32_t send_s = 0;
4486 int accum_moved = 0;
4487 int will_exit_fast_recovery = 0;
4488 uint32_t a_rwnd, old_rwnd;
4489 int win_probe_recovery = 0;
4490 int win_probe_recovered = 0;
4491 struct sctp_nets *net = NULL;
4494 uint8_t reneged_all = 0;
4495 uint8_t cmt_dac_flag;
4497 * we take any chance we can to service our queues since we cannot
4498 * get awoken when the socket is read from :<
4501 * Now perform the actual SACK handling: 1) Verify that it is not an
4502 * old sack, if so discard. 2) If there is nothing left in the send
4503 * queue (cum-ack is equal to last acked) then you have a duplicate
4504 * too, update any rwnd change and verify no timers are running.
4505 * then return. 3) Process any new consequtive data i.e. cum-ack
4506 * moved process these first and note that it moved. 4) Process any
4507 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4508 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4509 * sync up flightsizes and things, stop all timers and also check
4510 * for shutdown_pending state. If so then go ahead and send off the
4511 * shutdown. If in shutdown recv, send off the shutdown-ack and
4512 * start that timer, Ret. 9) Strike any non-acked things and do FR
4513 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4514 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4515 * if in shutdown_recv state.
4517 SCTP_TCB_LOCK_ASSERT(stcb);
4519 this_sack_lowest_newack = 0;
4520 SCTP_STAT_INCR(sctps_slowpath_sack);
4522 cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
4523 #ifdef SCTP_ASOCLOG_OF_TSNS
4524 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4525 stcb->asoc.cumack_log_at++;
4526 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4527 stcb->asoc.cumack_log_at = 0;
4532 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4533 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4534 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4537 old_rwnd = stcb->asoc.peers_rwnd;
4538 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4539 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4540 stcb->asoc.overall_error_count,
4542 SCTP_FROM_SCTP_INDATA,
4545 stcb->asoc.overall_error_count = 0;
4547 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4548 sctp_log_sack(asoc->last_acked_seq,
4555 if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4557 uint32_t *dupdata, dblock;
4559 for (i = 0; i < num_dup; i++) {
4560 dupdata = (uint32_t *)sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4561 sizeof(uint32_t), (uint8_t *)&dblock);
4562 if (dupdata == NULL) {
4565 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4569 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4570 tp1 = TAILQ_LAST(&asoc->sent_queue,
4571 sctpchunk_listhead);
4572 send_s = tp1->rec.data.tsn + 1;
4575 send_s = asoc->sending_seq;
4577 if (SCTP_TSN_GE(cum_ack, send_s)) {
4578 struct mbuf *op_err;
4579 char msg[SCTP_DIAG_INFO_LEN];
4582 * no way, we have not even sent this TSN out yet.
4583 * Peer is hopelessly messed up with us.
4585 SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4588 SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1: %p\n",
4589 tp1->rec.data.tsn, (void *)tp1);
4594 SCTP_SNPRINTF(msg, sizeof(msg),
4595 "Cum ack %8.8x greater or equal than TSN %8.8x",
4597 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4598 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_29;
4599 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4602 /**********************/
4603 /* 1) check the range */
4604 /**********************/
4605 if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4606 /* acking something behind */
4610 /* update the Rwnd of the peer */
4611 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4612 TAILQ_EMPTY(&asoc->send_queue) &&
4613 (asoc->stream_queue_cnt == 0)) {
4614 /* nothing left on send/sent and strmq */
4615 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4616 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4617 asoc->peers_rwnd, 0, 0, a_rwnd);
4619 asoc->peers_rwnd = a_rwnd;
4620 if (asoc->sent_queue_retran_cnt) {
4621 asoc->sent_queue_retran_cnt = 0;
4623 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4624 /* SWS sender side engages */
4625 asoc->peers_rwnd = 0;
4627 /* stop any timers */
4628 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4629 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4630 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4631 net->partial_bytes_acked = 0;
4632 net->flight_size = 0;
4634 asoc->total_flight = 0;
4635 asoc->total_flight_count = 0;
4639 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4640 * things. The total byte count acked is tracked in netAckSz AND
4641 * netAck2 is used to track the total bytes acked that are un-
4642 * amibguious and were never retransmitted. We track these on a per
4643 * destination address basis.
4645 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4646 if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4647 /* Drag along the window_tsn for cwr's */
4648 net->cwr_window_tsn = cum_ack;
4650 net->prev_cwnd = net->cwnd;
4655 * CMT: Reset CUC and Fast recovery algo variables before
4658 net->new_pseudo_cumack = 0;
4659 net->will_exit_fast_recovery = 0;
4660 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4661 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack)(stcb, net);
4665 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4666 * to be greater than the cumack. Also reset saw_newack to 0
4669 net->saw_newack = 0;
4670 net->this_sack_highest_newack = last_tsn;
4672 /* process the new consecutive TSN first */
4673 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4674 if (SCTP_TSN_GE(last_tsn, tp1->rec.data.tsn)) {
4675 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4677 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4679 * If it is less than ACKED, it is
4680 * now no-longer in flight. Higher
4681 * values may occur during marking
4683 if ((tp1->whoTo->dest_state &
4684 SCTP_ADDR_UNCONFIRMED) &&
4685 (tp1->snd_count < 2)) {
4687 * If there was no retran
4688 * and the address is
4689 * un-confirmed and we sent
4691 * sacked.. its confirmed,
4694 tp1->whoTo->dest_state &=
4695 ~SCTP_ADDR_UNCONFIRMED;
4697 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4698 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4699 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4700 tp1->whoTo->flight_size,
4702 (uint32_t)(uintptr_t)tp1->whoTo,
4705 sctp_flight_size_decrease(tp1);
4706 sctp_total_flight_decrease(stcb, tp1);
4707 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4708 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged)(tp1->whoTo,
4712 tp1->whoTo->net_ack += tp1->send_size;
4714 /* CMT SFR and DAC algos */
4715 this_sack_lowest_newack = tp1->rec.data.tsn;
4716 tp1->whoTo->saw_newack = 1;
4718 if (tp1->snd_count < 2) {
4720 * True non-retransmitted
4723 tp1->whoTo->net_ack2 +=
4726 /* update RTO too? */
4729 sctp_calculate_rto(stcb,
4732 &tp1->sent_rcv_time,
4733 SCTP_RTT_FROM_DATA)) {
4736 if (tp1->whoTo->rto_needed == 0) {
4737 tp1->whoTo->rto_needed = 1;
4743 * CMT: CUCv2 algorithm. From the
4744 * cumack'd TSNs, for each TSN being
4745 * acked for the first time, set the
4746 * following variables for the
4747 * corresp destination.
4748 * new_pseudo_cumack will trigger a
4750 * find_(rtx_)pseudo_cumack will
4751 * trigger search for the next
4752 * expected (rtx-)pseudo-cumack.
4754 tp1->whoTo->new_pseudo_cumack = 1;
4755 tp1->whoTo->find_pseudo_cumack = 1;
4756 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4759 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4760 sctp_log_sack(asoc->last_acked_seq,
4765 SCTP_LOG_TSN_ACKED);
4767 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4768 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.tsn, SCTP_CWND_LOG_FROM_SACK);
4771 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4772 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4773 #ifdef SCTP_AUDITING_ENABLED
4774 sctp_audit_log(0xB3,
4775 (asoc->sent_queue_retran_cnt & 0x000000ff));
4778 if (tp1->rec.data.chunk_was_revoked) {
4779 /* deflate the cwnd */
4780 tp1->whoTo->cwnd -= tp1->book_size;
4781 tp1->rec.data.chunk_was_revoked = 0;
4783 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4784 tp1->sent = SCTP_DATAGRAM_ACKED;
4791 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4792 /* always set this up to cum-ack */
4793 asoc->this_sack_highest_gap = last_tsn;
4795 if ((num_seg > 0) || (num_nr_seg > 0)) {
4798 * thisSackHighestGap will increase while handling NEW
4799 * segments this_sack_highest_newack will increase while
4800 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4801 * used for CMT DAC algo. saw_newack will also change.
4803 if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4804 &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4805 num_seg, num_nr_seg, &rto_ok)) {
4809 * validate the biggest_tsn_acked in the gap acks if
4810 * strict adherence is wanted.
4812 if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4814 * peer is either confused or we are under
4815 * attack. We must abort.
4817 SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4818 biggest_tsn_acked, send_s);
4822 /*******************************************/
4823 /* cancel ALL T3-send timer if accum moved */
4824 /*******************************************/
4825 if (asoc->sctp_cmt_on_off > 0) {
4826 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4827 if (net->new_pseudo_cumack)
4828 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4830 SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4835 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4836 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4837 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
4841 /********************************************/
4842 /* drop the acked chunks from the sentqueue */
4843 /********************************************/
4844 asoc->last_acked_seq = cum_ack;
4846 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
4847 if (SCTP_TSN_GT(tp1->rec.data.tsn, cum_ack)) {
4850 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4851 if (asoc->strmout[tp1->rec.data.sid].chunks_on_queues > 0) {
4852 asoc->strmout[tp1->rec.data.sid].chunks_on_queues--;
4855 panic("No chunks on the queues for sid %u.", tp1->rec.data.sid);
4859 if ((asoc->strmout[tp1->rec.data.sid].chunks_on_queues == 0) &&
4860 (asoc->strmout[tp1->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
4861 TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.sid].outqueue)) {
4862 asoc->trigger_reset = 1;
4864 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4865 if (PR_SCTP_ENABLED(tp1->flags)) {
4866 if (asoc->pr_sctp_cnt != 0)
4867 asoc->pr_sctp_cnt--;
4869 asoc->sent_queue_cnt--;
4871 /* sa_ignore NO_NULL_CHK */
4872 sctp_free_bufspace(stcb, asoc, tp1, 1);
4873 sctp_m_freem(tp1->data);
4875 if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4876 asoc->sent_queue_cnt_removeable--;
4879 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4880 sctp_log_sack(asoc->last_acked_seq,
4885 SCTP_LOG_FREE_SENT);
4887 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4890 if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
4892 panic("Warning flight size is positive and should be 0");
4894 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4895 asoc->total_flight);
4897 asoc->total_flight = 0;
4900 #if defined(__Userspace__)
4901 if (stcb->sctp_ep->recv_callback) {
4902 if (stcb->sctp_socket) {
4903 uint32_t inqueue_bytes, sb_free_now;
4904 struct sctp_inpcb *inp;
4906 inp = stcb->sctp_ep;
4907 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
4908 sb_free_now = SCTP_SB_LIMIT_SND(stcb->sctp_socket) - (inqueue_bytes + stcb->asoc.sb_send_resv);
4910 /* check if the amount free in the send socket buffer crossed the threshold */
4911 if (inp->send_callback &&
4912 (((inp->send_sb_threshold > 0) && (sb_free_now >= inp->send_sb_threshold)) ||
4913 (inp->send_sb_threshold == 0))) {
4914 atomic_add_int(&stcb->asoc.refcnt, 1);
4915 SCTP_TCB_UNLOCK(stcb);
4916 inp->send_callback(stcb->sctp_socket, sb_free_now);
4917 SCTP_TCB_LOCK(stcb);
4918 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4921 } else if ((wake_him) && (stcb->sctp_socket)) {
4923 /* sa_ignore NO_NULL_CHK */
4924 if ((wake_him) && (stcb->sctp_socket)) {
4926 #if defined(__APPLE__) && !defined(__Userspace__)
4930 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4931 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4932 sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
4934 #if defined(__APPLE__) && !defined(__Userspace__)
4935 so = SCTP_INP_SO(stcb->sctp_ep);
4936 atomic_add_int(&stcb->asoc.refcnt, 1);
4937 SCTP_TCB_UNLOCK(stcb);
4938 SCTP_SOCKET_LOCK(so, 1);
4939 SCTP_TCB_LOCK(stcb);
4940 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4941 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4942 /* assoc was freed while we were unlocked */
4943 SCTP_SOCKET_UNLOCK(so, 1);
4947 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4948 #if defined(__APPLE__) && !defined(__Userspace__)
4949 SCTP_SOCKET_UNLOCK(so, 1);
4952 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4953 sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
4957 if (asoc->fast_retran_loss_recovery && accum_moved) {
4958 if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4959 /* Setup so we will exit RFC2582 fast recovery */
4960 will_exit_fast_recovery = 1;
4964 * Check for revoked fragments:
4966 * if Previous sack - Had no frags then we can't have any revoked if
4967 * Previous sack - Had frag's then - If we now have frags aka
4968 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4969 * some of them. else - The peer revoked all ACKED fragments, since
4970 * we had some before and now we have NONE.
4974 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4975 asoc->saw_sack_with_frags = 1;
4976 } else if (asoc->saw_sack_with_frags) {
4977 int cnt_revoked = 0;
4979 /* Peer revoked all dg's marked or acked */
4980 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4981 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4982 tp1->sent = SCTP_DATAGRAM_SENT;
4983 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4984 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4985 tp1->whoTo->flight_size,
4987 (uint32_t)(uintptr_t)tp1->whoTo,
4990 sctp_flight_size_increase(tp1);
4991 sctp_total_flight_increase(stcb, tp1);
4992 tp1->rec.data.chunk_was_revoked = 1;
4994 * To ensure that this increase in
4995 * flightsize, which is artificial,
4996 * does not throttle the sender, we
4997 * also increase the cwnd
5000 tp1->whoTo->cwnd += tp1->book_size;
5007 asoc->saw_sack_with_frags = 0;
5010 asoc->saw_sack_with_nr_frags = 1;
5012 asoc->saw_sack_with_nr_frags = 0;
5014 /* JRS - Use the congestion control given in the CC module */
5015 if (ecne_seen == 0) {
5016 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5017 if (net->net_ack2 > 0) {
5019 * Karn's rule applies to clearing error count, this
5022 net->error_count = 0;
5023 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
5024 /* addr came good */
5025 net->dest_state |= SCTP_ADDR_REACHABLE;
5026 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
5027 0, (void *)net, SCTP_SO_NOT_LOCKED);
5030 if (net == stcb->asoc.primary_destination) {
5031 if (stcb->asoc.alternate) {
5032 /* release the alternate, primary is good */
5033 sctp_free_remote_addr(stcb->asoc.alternate);
5034 stcb->asoc.alternate = NULL;
5038 if (net->dest_state & SCTP_ADDR_PF) {
5039 net->dest_state &= ~SCTP_ADDR_PF;
5040 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
5041 stcb->sctp_ep, stcb, net,
5042 SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5043 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
5044 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
5045 /* Done with this net */
5048 /* restore any doubled timers */
5049 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
5050 if (net->RTO < stcb->asoc.minrto) {
5051 net->RTO = stcb->asoc.minrto;
5053 if (net->RTO > stcb->asoc.maxrto) {
5054 net->RTO = stcb->asoc.maxrto;
5058 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5061 if (TAILQ_EMPTY(&asoc->sent_queue)) {
5062 /* nothing left in-flight */
5063 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5064 /* stop all timers */
5065 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5067 SCTP_FROM_SCTP_INDATA + SCTP_LOC_34);
5068 net->flight_size = 0;
5069 net->partial_bytes_acked = 0;
5071 asoc->total_flight = 0;
5072 asoc->total_flight_count = 0;
5075 /**********************************/
5076 /* Now what about shutdown issues */
5077 /**********************************/
5078 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5079 /* nothing left on sendqueue.. consider done */
5080 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5081 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5082 asoc->peers_rwnd, 0, 0, a_rwnd);
5084 asoc->peers_rwnd = a_rwnd;
5085 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5086 /* SWS sender side engages */
5087 asoc->peers_rwnd = 0;
5090 if ((asoc->stream_queue_cnt == 1) &&
5091 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5092 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5093 ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc))) {
5094 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
5096 if (((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5097 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5098 (asoc->stream_queue_cnt == 1) &&
5099 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
5100 struct mbuf *op_err;
5104 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
5105 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_35;
5106 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5109 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5110 (asoc->stream_queue_cnt == 0)) {
5111 struct sctp_nets *netp;
5113 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
5114 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5115 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5117 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
5118 sctp_stop_timers_for_shutdown(stcb);
5119 if (asoc->alternate) {
5120 netp = asoc->alternate;
5122 netp = asoc->primary_destination;
5124 sctp_send_shutdown(stcb, netp);
5125 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5126 stcb->sctp_ep, stcb, netp);
5127 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5128 stcb->sctp_ep, stcb, NULL);
5130 } else if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5131 (asoc->stream_queue_cnt == 0)) {
5132 struct sctp_nets *netp;
5134 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5135 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
5136 sctp_stop_timers_for_shutdown(stcb);
5137 if (asoc->alternate) {
5138 netp = asoc->alternate;
5140 netp = asoc->primary_destination;
5142 sctp_send_shutdown_ack(stcb, netp);
5143 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5144 stcb->sctp_ep, stcb, netp);
5149 * Now here we are going to recycle net_ack for a different use...
5152 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5157 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5158 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5159 * automatically ensure that.
5161 if ((asoc->sctp_cmt_on_off > 0) &&
5162 SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
5163 (cmt_dac_flag == 0)) {
5164 this_sack_lowest_newack = cum_ack;
5166 if ((num_seg > 0) || (num_nr_seg > 0)) {
5167 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5168 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5170 /* JRS - Use the congestion control given in the CC module */
5171 asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5173 /* Now are we exiting loss recovery ? */
5174 if (will_exit_fast_recovery) {
5175 /* Ok, we must exit fast recovery */
5176 asoc->fast_retran_loss_recovery = 0;
5178 if ((asoc->sat_t3_loss_recovery) &&
5179 SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
5180 /* end satellite t3 loss recovery */
5181 asoc->sat_t3_loss_recovery = 0;
5186 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5187 if (net->will_exit_fast_recovery) {
5188 /* Ok, we must exit fast recovery */
5189 net->fast_retran_loss_recovery = 0;
5193 /* Adjust and set the new rwnd value */
5194 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5195 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5196 asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
5198 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5199 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5200 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5201 /* SWS sender side engages */
5202 asoc->peers_rwnd = 0;
5204 if (asoc->peers_rwnd > old_rwnd) {
5205 win_probe_recovery = 1;
5209 * Now we must setup so we have a timer up for anyone with
5215 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5216 if (win_probe_recovery && (net->window_probe)) {
5217 win_probe_recovered = 1;
5219 * Find first chunk that was used with
5220 * window probe and clear the event. Put
5221 * it back into the send queue as if has
5224 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5225 if (tp1->window_probe) {
5226 sctp_window_probe_recovery(stcb, asoc, tp1);
5231 if (net->flight_size) {
5233 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5234 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5235 stcb->sctp_ep, stcb, net);
5237 if (net->window_probe) {
5238 net->window_probe = 0;
5241 if (net->window_probe) {
5242 /* In window probes we must assure a timer is still running there */
5243 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5244 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5245 stcb->sctp_ep, stcb, net);
5248 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5249 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5251 SCTP_FROM_SCTP_INDATA + SCTP_LOC_36);
5256 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5257 (asoc->sent_queue_retran_cnt == 0) &&
5258 (win_probe_recovered == 0) &&
5260 /* huh, this should not happen unless all packets
5261 * are PR-SCTP and marked to skip of course.
5263 if (sctp_fs_audit(asoc)) {
5264 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5265 net->flight_size = 0;
5267 asoc->total_flight = 0;
5268 asoc->total_flight_count = 0;
5269 asoc->sent_queue_retran_cnt = 0;
5270 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5271 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5272 sctp_flight_size_increase(tp1);
5273 sctp_total_flight_increase(stcb, tp1);
5274 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5275 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5282 /*********************************************/
5283 /* Here we perform PR-SCTP procedures */
5285 /*********************************************/
5286 /* C1. update advancedPeerAckPoint */
5287 if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5288 asoc->advanced_peer_ack_point = cum_ack;
5290 /* C2. try to further move advancedPeerAckPoint ahead */
5291 if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
5292 struct sctp_tmit_chunk *lchk;
5293 uint32_t old_adv_peer_ack_point;
5295 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5296 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5297 /* C3. See if we need to send a Fwd-TSN */
5298 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5300 * ISSUE with ECN, see FWD-TSN processing.
5302 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
5303 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
5304 0xee, cum_ack, asoc->advanced_peer_ack_point,
5305 old_adv_peer_ack_point);
5307 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5308 send_forward_tsn(stcb, asoc);
5310 /* try to FR fwd-tsn's that get lost too */
5311 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
5312 send_forward_tsn(stcb, asoc);
5316 for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
5317 if (lchk->whoTo != NULL) {
5322 /* Assure a timer is up */
5323 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5324 stcb->sctp_ep, stcb, lchk->whoTo);
5327 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5328 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5330 stcb->asoc.peers_rwnd,
5331 stcb->asoc.total_flight,
5332 stcb->asoc.total_output_queue_size);
5337 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5340 uint32_t cum_ack, a_rwnd;
5342 cum_ack = ntohl(cp->cumulative_tsn_ack);
5343 /* Arrange so a_rwnd does NOT change */
5344 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5346 /* Now call the express sack handling */
5347 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5351 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5352 struct sctp_stream_in *strmin)
5354 struct sctp_queued_to_read *control, *ncontrol;
5355 struct sctp_association *asoc;
5357 int need_reasm_check = 0;
5360 mid = strmin->last_mid_delivered;
5362 * First deliver anything prior to and including the stream no that
5365 TAILQ_FOREACH_SAFE(control, &strmin->inqueue, next_instrm, ncontrol) {
5366 if (SCTP_MID_GE(asoc->idata_supported, mid, control->mid)) {
5367 /* this is deliverable now */
5368 if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
5369 if (control->on_strm_q) {
5370 if (control->on_strm_q == SCTP_ON_ORDERED) {
5371 TAILQ_REMOVE(&strmin->inqueue, control, next_instrm);
5372 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5373 TAILQ_REMOVE(&strmin->uno_inqueue, control, next_instrm);
5376 panic("strmin: %p ctl: %p unknown %d",
5377 strmin, control, control->on_strm_q);
5380 control->on_strm_q = 0;
5382 /* subtract pending on streams */
5383 if (asoc->size_on_all_streams >= control->length) {
5384 asoc->size_on_all_streams -= control->length;
5387 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5389 asoc->size_on_all_streams = 0;
5392 sctp_ucount_decr(asoc->cnt_on_all_streams);
5393 /* deliver it to at least the delivery-q */
5394 if (stcb->sctp_socket) {
5395 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
5396 sctp_add_to_readq(stcb->sctp_ep, stcb,
5398 &stcb->sctp_socket->so_rcv,
5399 1, SCTP_READ_LOCK_HELD,
5400 SCTP_SO_NOT_LOCKED);
5403 /* Its a fragmented message */
5404 if (control->first_frag_seen) {
5405 /* Make it so this is next to deliver, we restore later */
5406 strmin->last_mid_delivered = control->mid - 1;
5407 need_reasm_check = 1;
5412 /* no more delivery now. */
5416 if (need_reasm_check) {
5418 ret = sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin, SCTP_READ_LOCK_HELD);
5419 if (SCTP_MID_GT(asoc->idata_supported, mid, strmin->last_mid_delivered)) {
5420 /* Restore the next to deliver unless we are ahead */
5421 strmin->last_mid_delivered = mid;
5424 /* Left the front Partial one on */
5427 need_reasm_check = 0;
5430 * now we must deliver things in queue the normal way if any are
5433 mid = strmin->last_mid_delivered + 1;
5434 TAILQ_FOREACH_SAFE(control, &strmin->inqueue, next_instrm, ncontrol) {
5435 if (SCTP_MID_EQ(asoc->idata_supported, mid, control->mid)) {
5436 if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
5437 /* this is deliverable now */
5438 if (control->on_strm_q) {
5439 if (control->on_strm_q == SCTP_ON_ORDERED) {
5440 TAILQ_REMOVE(&strmin->inqueue, control, next_instrm);
5441 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5442 TAILQ_REMOVE(&strmin->uno_inqueue, control, next_instrm);
5445 panic("strmin: %p ctl: %p unknown %d",
5446 strmin, control, control->on_strm_q);
5449 control->on_strm_q = 0;
5451 /* subtract pending on streams */
5452 if (asoc->size_on_all_streams >= control->length) {
5453 asoc->size_on_all_streams -= control->length;
5456 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5458 asoc->size_on_all_streams = 0;
5461 sctp_ucount_decr(asoc->cnt_on_all_streams);
5462 /* deliver it to at least the delivery-q */
5463 strmin->last_mid_delivered = control->mid;
5464 if (stcb->sctp_socket) {
5465 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
5466 sctp_add_to_readq(stcb->sctp_ep, stcb,
5468 &stcb->sctp_socket->so_rcv, 1,
5469 SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5472 mid = strmin->last_mid_delivered + 1;
5474 /* Its a fragmented message */
5475 if (control->first_frag_seen) {
5476 /* Make it so this is next to deliver */
5477 strmin->last_mid_delivered = control->mid - 1;
5478 need_reasm_check = 1;
5486 if (need_reasm_check) {
5487 (void)sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin, SCTP_READ_LOCK_HELD);
5494 sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
5495 struct sctp_association *asoc, struct sctp_stream_in *strm,
5496 struct sctp_queued_to_read *control, int ordered, uint32_t cumtsn)
5498 struct sctp_tmit_chunk *chk, *nchk;
5499 int cnt_removed = 0;
5502 * For now large messages held on the stream reasm that are
5503 * complete will be tossed too. We could in theory do more
5504 * work to spin through and stop after dumping one msg aka
5505 * seeing the start of a new msg at the head, and call the
5506 * delivery function... to see if it can be delivered... But
5507 * for now we just dump everything on the queue.
5509 if (!asoc->idata_supported && !ordered && SCTP_TSN_GT(control->fsn_included, cumtsn)) {
5512 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5513 /* Purge hanging chunks */
5514 if (!asoc->idata_supported && (ordered == 0)) {
5515 if (SCTP_TSN_GT(chk->rec.data.tsn, cumtsn)) {
5520 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5521 if (asoc->size_on_reasm_queue >= chk->send_size) {
5522 asoc->size_on_reasm_queue -= chk->send_size;
5525 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
5527 asoc->size_on_reasm_queue = 0;
5530 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5532 sctp_m_freem(chk->data);
5535 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5537 if (!TAILQ_EMPTY(&control->reasm)) {
5538 /* This has to be old data, unordered */
5539 if (control->data) {
5540 sctp_m_freem(control->data);
5541 control->data = NULL;
5543 sctp_reset_a_control(control, stcb->sctp_ep, cumtsn);
5544 chk = TAILQ_FIRST(&control->reasm);
5545 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
5546 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5547 sctp_add_chk_to_control(control, strm, stcb, asoc,
5548 chk, SCTP_READ_LOCK_HELD);
5550 sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_HELD);
5553 if (control->on_strm_q == SCTP_ON_ORDERED) {
5554 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
5555 if (asoc->size_on_all_streams >= control->length) {
5556 asoc->size_on_all_streams -= control->length;
5559 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5561 asoc->size_on_all_streams = 0;
5564 sctp_ucount_decr(asoc->cnt_on_all_streams);
5565 control->on_strm_q = 0;
5566 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5567 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
5568 control->on_strm_q = 0;
5570 } else if (control->on_strm_q) {
5571 panic("strm: %p ctl: %p unknown %d",
5572 strm, control, control->on_strm_q);
5575 control->on_strm_q = 0;
5576 if (control->on_read_q == 0) {
5577 sctp_free_remote_addr(control->whoFrom);
5578 if (control->data) {
5579 sctp_m_freem(control->data);
5580 control->data = NULL;
5582 sctp_free_a_readq(stcb, control);
5587 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5588 struct sctp_forward_tsn_chunk *fwd,
5589 int *abort_flag, struct mbuf *m , int offset)
5591 /* The pr-sctp fwd tsn */
5593 * here we will perform all the data receiver side steps for
5594 * processing FwdTSN, as required in by pr-sctp draft:
5596 * Assume we get FwdTSN(x):
5598 * 1) update local cumTSN to x
5599 * 2) try to further advance cumTSN to x + others we have
5600 * 3) examine and update re-ordering queue on pr-in-streams
5601 * 4) clean up re-assembly queue
5602 * 5) Send a sack to report where we are.
5604 struct sctp_association *asoc;
5605 uint32_t new_cum_tsn, gap;
5606 unsigned int i, fwd_sz, m_size;
5608 struct sctp_stream_in *strm;
5609 struct sctp_queued_to_read *control, *sv;
5612 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5613 SCTPDBG(SCTP_DEBUG_INDATA1,
5614 "Bad size too small/big fwd-tsn\n");
5617 m_size = (stcb->asoc.mapping_array_size << 3);
5618 /*************************************************************/
5619 /* 1. Here we update local cumTSN and shift the bitmap array */
5620 /*************************************************************/
5621 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5623 if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5624 /* Already got there ... */
5628 * now we know the new TSN is more advanced, let's find the actual
5631 SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
5632 asoc->cumulative_tsn = new_cum_tsn;
5633 if (gap >= m_size) {
5634 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5635 struct mbuf *op_err;
5636 char msg[SCTP_DIAG_INFO_LEN];
5639 * out of range (of single byte chunks in the rwnd I
5640 * give out). This must be an attacker.
5643 SCTP_SNPRINTF(msg, sizeof(msg),
5644 "New cum ack %8.8x too high, highest TSN %8.8x",
5645 new_cum_tsn, asoc->highest_tsn_inside_map);
5646 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5647 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_37;
5648 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5651 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5653 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5654 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5655 asoc->highest_tsn_inside_map = new_cum_tsn;
5657 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5658 asoc->highest_tsn_inside_nr_map = new_cum_tsn;
5660 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5661 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5664 SCTP_TCB_LOCK_ASSERT(stcb);
5665 for (i = 0; i <= gap; i++) {
5666 if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
5667 !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
5668 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
5669 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
5670 asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5675 /*************************************************************/
5676 /* 2. Clear up re-assembly queue */
5677 /*************************************************************/
5679 /* This is now done as part of clearing up the stream/seq */
5680 if (asoc->idata_supported == 0) {
5683 /* Flush all the un-ordered data based on cum-tsn */
5684 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5685 for (sid = 0 ; sid < asoc->streamincnt; sid++) {
5686 strm = &asoc->strmin[sid];
5687 if (!TAILQ_EMPTY(&strm->uno_inqueue)) {
5688 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, TAILQ_FIRST(&strm->uno_inqueue), 0, new_cum_tsn);
5691 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5693 /*******************************************************/
5694 /* 3. Update the PR-stream re-ordering queues and fix */
5695 /* delivery issues as needed. */
5696 /*******************************************************/
5697 fwd_sz -= sizeof(*fwd);
5700 unsigned int num_str;
5703 uint16_t ordered, flags;
5704 struct sctp_strseq *stseq, strseqbuf;
5705 struct sctp_strseq_mid *stseq_m, strseqbuf_m;
5706 offset += sizeof(*fwd);
5708 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5709 if (asoc->idata_supported) {
5710 num_str = fwd_sz / sizeof(struct sctp_strseq_mid);
5712 num_str = fwd_sz / sizeof(struct sctp_strseq);
5714 for (i = 0; i < num_str; i++) {
5715 if (asoc->idata_supported) {
5716 stseq_m = (struct sctp_strseq_mid *)sctp_m_getptr(m, offset,
5717 sizeof(struct sctp_strseq_mid),
5718 (uint8_t *)&strseqbuf_m);
5719 offset += sizeof(struct sctp_strseq_mid);
5720 if (stseq_m == NULL) {
5723 sid = ntohs(stseq_m->sid);
5724 mid = ntohl(stseq_m->mid);
5725 flags = ntohs(stseq_m->flags);
5726 if (flags & PR_SCTP_UNORDERED_FLAG) {
5732 stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5733 sizeof(struct sctp_strseq),
5734 (uint8_t *)&strseqbuf);
5735 offset += sizeof(struct sctp_strseq);
5736 if (stseq == NULL) {
5739 sid = ntohs(stseq->sid);
5740 mid = (uint32_t)ntohs(stseq->ssn);
5748 * Ok we now look for the stream/seq on the read queue
5749 * where its not all delivered. If we find it we transmute the
5750 * read entry into a PDI_ABORTED.
5752 if (sid >= asoc->streamincnt) {
5753 /* screwed up streams, stop! */
5756 if ((asoc->str_of_pdapi == sid) &&
5757 (asoc->ssn_of_pdapi == mid)) {
5758 /* If this is the one we were partially delivering
5759 * now then we no longer are. Note this will change
5760 * with the reassembly re-write.
5762 asoc->fragmented_delivery_inprogress = 0;
5764 strm = &asoc->strmin[sid];
5766 TAILQ_FOREACH(control, &strm->inqueue, next_instrm) {
5767 if (SCTP_MID_GE(asoc->idata_supported, mid, control->mid)) {
5768 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, control, ordered, new_cum_tsn);
5772 if (asoc->idata_supported) {
5773 TAILQ_FOREACH(control, &strm->uno_inqueue, next_instrm) {
5774 if (SCTP_MID_GE(asoc->idata_supported, mid, control->mid)) {
5775 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, control, ordered, new_cum_tsn);
5779 if (!TAILQ_EMPTY(&strm->uno_inqueue)) {
5780 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, TAILQ_FIRST(&strm->uno_inqueue), ordered, new_cum_tsn);
5784 TAILQ_FOREACH(control, &stcb->sctp_ep->read_queue, next) {
5785 if ((control->sinfo_stream == sid) &&
5786 (SCTP_MID_EQ(asoc->idata_supported, control->mid, mid))) {
5787 str_seq = (sid << 16) | (0x0000ffff & mid);
5788 control->pdapi_aborted = 1;
5789 sv = stcb->asoc.control_pdapi;
5790 control->end_added = 1;
5791 if (control->on_strm_q == SCTP_ON_ORDERED) {
5792 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
5793 if (asoc->size_on_all_streams >= control->length) {
5794 asoc->size_on_all_streams -= control->length;
5797 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5799 asoc->size_on_all_streams = 0;
5802 sctp_ucount_decr(asoc->cnt_on_all_streams);
5803 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5804 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
5806 } else if (control->on_strm_q) {
5807 panic("strm: %p ctl: %p unknown %d",
5808 strm, control, control->on_strm_q);
5811 control->on_strm_q = 0;
5812 stcb->asoc.control_pdapi = control;
5813 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5815 SCTP_PARTIAL_DELIVERY_ABORTED,
5817 SCTP_SO_NOT_LOCKED);
5818 stcb->asoc.control_pdapi = sv;
5820 } else if ((control->sinfo_stream == sid) &&
5821 SCTP_MID_GT(asoc->idata_supported, control->mid, mid)) {
5822 /* We are past our victim SSN */
5826 if (SCTP_MID_GT(asoc->idata_supported, mid, strm->last_mid_delivered)) {
5827 /* Update the sequence number */
5828 strm->last_mid_delivered = mid;
5830 /* now kick the stream the new way */
5831 /*sa_ignore NO_NULL_CHK*/
5832 sctp_kick_prsctp_reorder_queue(stcb, strm);
5834 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5837 * Now slide thing forward.
5839 sctp_slide_mapping_arrays(stcb);