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 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_pcb.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctp_auth.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_asconf.h>
50 #include <netinet/sctp_indata.h>
51 #include <netinet/sctp_bsd_addr.h>
52 #include <netinet/sctp_input.h>
53 #include <netinet/sctp_crc32.h>
54 #include <netinet/sctp_lock_bsd.h>
56 * NOTES: On the outbound side of things I need to check the sack timer to
57 * see if I should generate a sack into the chunk queue (if I have data to
58 * send that is and will be sending it .. for bundling.
60 * The callback in sctp_usrreq.c will get called when the socket is read from.
61 * This will cause sctp_service_queues() to get called on the top entry in
65 sctp_add_chk_to_control(struct sctp_queued_to_read *control,
66 struct sctp_stream_in *strm,
67 struct sctp_tcb *stcb,
68 struct sctp_association *asoc,
69 struct sctp_tmit_chunk *chk, int hold_rlock);
72 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
74 asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
77 /* Calculate what the rwnd would be */
79 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
84 * This is really set wrong with respect to a 1-2-m socket. Since
85 * the sb_cc is the count that everyone as put up. When we re-write
86 * sctp_soreceive then we will fix this so that ONLY this
87 * associations data is taken into account.
89 if (stcb->sctp_socket == NULL) {
93 KASSERT(asoc->cnt_on_reasm_queue > 0 || asoc->size_on_reasm_queue == 0,
94 ("size_on_reasm_queue is %u", asoc->size_on_reasm_queue));
95 KASSERT(asoc->cnt_on_all_streams > 0 || asoc->size_on_all_streams == 0,
96 ("size_on_all_streams is %u", asoc->size_on_all_streams));
97 if (stcb->asoc.sb_cc == 0 &&
98 asoc->cnt_on_reasm_queue == 0 &&
99 asoc->cnt_on_all_streams == 0) {
100 /* Full rwnd granted */
101 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
104 /* get actual space */
105 calc = (uint32_t)sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
107 * take out what has NOT been put on socket queue and we yet hold
110 calc = sctp_sbspace_sub(calc, (uint32_t)(asoc->size_on_reasm_queue +
111 asoc->cnt_on_reasm_queue * MSIZE));
112 calc = sctp_sbspace_sub(calc, (uint32_t)(asoc->size_on_all_streams +
113 asoc->cnt_on_all_streams * MSIZE));
119 /* what is the overhead of all these rwnd's */
120 calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
122 * If the window gets too small due to ctrl-stuff, reduce it to 1,
123 * even it is 0. SWS engaged
125 if (calc < stcb->asoc.my_rwnd_control_len) {
132 * Build out our readq entry based on the incoming packet.
134 struct sctp_queued_to_read *
135 sctp_build_readq_entry(struct sctp_tcb *stcb,
136 struct sctp_nets *net,
137 uint32_t tsn, uint32_t ppid,
138 uint32_t context, uint16_t sid,
139 uint32_t mid, uint8_t flags,
142 struct sctp_queued_to_read *read_queue_e = NULL;
144 sctp_alloc_a_readq(stcb, read_queue_e);
145 if (read_queue_e == NULL) {
148 memset(read_queue_e, 0, sizeof(struct sctp_queued_to_read));
149 read_queue_e->sinfo_stream = sid;
150 read_queue_e->sinfo_flags = (flags << 8);
151 read_queue_e->sinfo_ppid = ppid;
152 read_queue_e->sinfo_context = context;
153 read_queue_e->sinfo_tsn = tsn;
154 read_queue_e->sinfo_cumtsn = tsn;
155 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
156 read_queue_e->mid = mid;
157 read_queue_e->top_fsn = read_queue_e->fsn_included = 0xffffffff;
158 TAILQ_INIT(&read_queue_e->reasm);
159 read_queue_e->whoFrom = net;
160 atomic_add_int(&net->ref_count, 1);
161 read_queue_e->data = dm;
162 read_queue_e->stcb = stcb;
163 read_queue_e->port_from = stcb->rport;
164 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
165 read_queue_e->do_not_ref_stcb = 1;
168 return (read_queue_e);
172 sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
174 struct sctp_extrcvinfo *seinfo;
175 struct sctp_sndrcvinfo *outinfo;
176 struct sctp_rcvinfo *rcvinfo;
177 struct sctp_nxtinfo *nxtinfo;
184 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
185 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
186 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
187 /* user does not want any ancillary data */
192 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
193 len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
195 seinfo = (struct sctp_extrcvinfo *)sinfo;
196 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
197 (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
199 len += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
203 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
204 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
206 len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
209 len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
215 ret = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
220 SCTP_BUF_LEN(ret) = 0;
222 /* We need a CMSG header followed by the struct */
223 cmh = mtod(ret, struct cmsghdr *);
225 * Make sure that there is no un-initialized padding between the
226 * cmsg header and cmsg data and after the cmsg data.
229 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
230 cmh->cmsg_level = IPPROTO_SCTP;
231 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
232 cmh->cmsg_type = SCTP_RCVINFO;
233 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
234 rcvinfo->rcv_sid = sinfo->sinfo_stream;
235 rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
236 rcvinfo->rcv_flags = sinfo->sinfo_flags;
237 rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
238 rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
239 rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
240 rcvinfo->rcv_context = sinfo->sinfo_context;
241 rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
242 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
243 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
246 cmh->cmsg_level = IPPROTO_SCTP;
247 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
248 cmh->cmsg_type = SCTP_NXTINFO;
249 nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
250 nxtinfo->nxt_sid = seinfo->serinfo_next_stream;
251 nxtinfo->nxt_flags = 0;
252 if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
253 nxtinfo->nxt_flags |= SCTP_UNORDERED;
255 if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
256 nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
258 if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
259 nxtinfo->nxt_flags |= SCTP_COMPLETE;
261 nxtinfo->nxt_ppid = seinfo->serinfo_next_ppid;
262 nxtinfo->nxt_length = seinfo->serinfo_next_length;
263 nxtinfo->nxt_assoc_id = seinfo->serinfo_next_aid;
264 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
265 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
267 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
268 cmh->cmsg_level = IPPROTO_SCTP;
269 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
271 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
272 cmh->cmsg_type = SCTP_EXTRCV;
273 memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
274 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
276 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
277 cmh->cmsg_type = SCTP_SNDRCV;
279 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
286 sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
291 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
294 if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
296 * This tsn is behind the cum ack and thus we don't need to
297 * worry about it being moved from one to the other.
301 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
302 in_r = SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap);
303 in_nr = SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap);
304 KASSERT(in_r || in_nr, ("%s: Things are really messed up now", __func__));
306 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
307 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
308 asoc->highest_tsn_inside_nr_map = tsn;
312 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
313 if (tsn == asoc->highest_tsn_inside_map) {
314 /* We must back down to see what the new highest is. */
315 for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
316 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
317 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
318 asoc->highest_tsn_inside_map = i;
322 if (!SCTP_TSN_GE(i, asoc->mapping_array_base_tsn)) {
323 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
330 sctp_place_control_in_stream(struct sctp_stream_in *strm,
331 struct sctp_association *asoc,
332 struct sctp_queued_to_read *control)
334 struct sctp_queued_to_read *at;
335 struct sctp_readhead *q;
336 uint8_t flags, unordered;
338 flags = (control->sinfo_flags >> 8);
339 unordered = flags & SCTP_DATA_UNORDERED;
341 q = &strm->uno_inqueue;
342 if (asoc->idata_supported == 0) {
343 if (!TAILQ_EMPTY(q)) {
345 * Only one stream can be here in old style
350 TAILQ_INSERT_TAIL(q, control, next_instrm);
351 control->on_strm_q = SCTP_ON_UNORDERED;
357 if ((flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
358 control->end_added = 1;
359 control->first_frag_seen = 1;
360 control->last_frag_seen = 1;
362 if (TAILQ_EMPTY(q)) {
364 TAILQ_INSERT_HEAD(q, control, next_instrm);
366 control->on_strm_q = SCTP_ON_UNORDERED;
368 control->on_strm_q = SCTP_ON_ORDERED;
372 TAILQ_FOREACH(at, q, next_instrm) {
373 if (SCTP_MID_GT(asoc->idata_supported, at->mid, control->mid)) {
375 * one in queue is bigger than the new one,
376 * insert before this one
378 TAILQ_INSERT_BEFORE(at, control, next_instrm);
380 control->on_strm_q = SCTP_ON_UNORDERED;
382 control->on_strm_q = SCTP_ON_ORDERED;
385 } else if (SCTP_MID_EQ(asoc->idata_supported, at->mid, control->mid)) {
387 * Gak, He sent me a duplicate msg id
388 * number?? return -1 to abort.
392 if (TAILQ_NEXT(at, next_instrm) == NULL) {
394 * We are at the end, insert it
397 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
398 sctp_log_strm_del(control, at,
399 SCTP_STR_LOG_FROM_INSERT_TL);
401 TAILQ_INSERT_AFTER(q, at, control, next_instrm);
403 control->on_strm_q = SCTP_ON_UNORDERED;
405 control->on_strm_q = SCTP_ON_ORDERED;
416 sctp_abort_in_reasm(struct sctp_tcb *stcb,
417 struct sctp_queued_to_read *control,
418 struct sctp_tmit_chunk *chk,
419 int *abort_flag, int opspot)
421 char msg[SCTP_DIAG_INFO_LEN];
424 if (stcb->asoc.idata_supported) {
425 SCTP_SNPRINTF(msg, sizeof(msg),
426 "Reass %x,CF:%x,TSN=%8.8x,SID=%4.4x,FSN=%8.8x,MID:%8.8x",
428 control->fsn_included,
431 chk->rec.data.fsn, chk->rec.data.mid);
433 SCTP_SNPRINTF(msg, sizeof(msg),
434 "Reass %x,CI:%x,TSN=%8.8x,SID=%4.4x,FSN=%4.4x,SSN:%4.4x",
436 control->fsn_included,
440 (uint16_t)chk->rec.data.mid);
442 oper = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
443 sctp_m_freem(chk->data);
445 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
446 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
447 sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
452 sctp_clean_up_control(struct sctp_tcb *stcb, struct sctp_queued_to_read *control)
455 * The control could not be placed and must be cleaned.
457 struct sctp_tmit_chunk *chk, *nchk;
459 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
460 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
462 sctp_m_freem(chk->data);
464 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
466 sctp_free_remote_addr(control->whoFrom);
468 sctp_m_freem(control->data);
469 control->data = NULL;
471 sctp_free_a_readq(stcb, control);
475 * Queue the chunk either right into the socket buffer if it is the next one
476 * to go OR put it in the correct place in the delivery queue. If we do
477 * append to the so_buf, keep doing so until we are out of order as
478 * long as the control's entered are non-fragmented.
481 sctp_queue_data_to_stream(struct sctp_tcb *stcb,
482 struct sctp_association *asoc,
483 struct sctp_queued_to_read *control, int *abort_flag, int *need_reasm)
486 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
487 * all the data in one stream this could happen quite rapidly. One
488 * could use the TSN to keep track of things, but this scheme breaks
489 * down in the other type of stream usage that could occur. Send a
490 * single msg to stream 0, send 4Billion messages to stream 1, now
491 * send a message to stream 0. You have a situation where the TSN
492 * has wrapped but not in the stream. Is this worth worrying about
493 * or should we just change our queue sort at the bottom to be by
496 * Could it also be legal for a peer to send ssn 1 with TSN 2 and
497 * ssn 2 with TSN 1? If the peer is doing some sort of funky TSN/SSN
498 * assignment this could happen... and I don't see how this would be
499 * a violation. So for now I am undecided an will leave the sort by
500 * SSN alone. Maybe a hybred approach is the answer
503 struct sctp_queued_to_read *at;
507 struct sctp_stream_in *strm;
508 char msg[SCTP_DIAG_INFO_LEN];
510 strm = &asoc->strmin[control->sinfo_stream];
511 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
512 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
514 if (SCTP_MID_GT((asoc->idata_supported), strm->last_mid_delivered, control->mid)) {
515 /* The incoming sseq is behind where we last delivered? */
516 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ: %u delivered: %u from peer, Abort association\n",
517 strm->last_mid_delivered, control->mid);
519 * throw it in the stream so it gets cleaned up in
520 * association destruction
522 TAILQ_INSERT_HEAD(&strm->inqueue, control, next_instrm);
523 if (asoc->idata_supported) {
524 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered MID=%8.8x, got TSN=%8.8x, SID=%4.4x, MID=%8.8x",
525 strm->last_mid_delivered, control->sinfo_tsn,
526 control->sinfo_stream, control->mid);
528 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
529 (uint16_t)strm->last_mid_delivered,
531 control->sinfo_stream,
532 (uint16_t)control->mid);
534 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
535 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
536 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
541 asoc->size_on_all_streams += control->length;
542 sctp_ucount_incr(asoc->cnt_on_all_streams);
543 nxt_todel = strm->last_mid_delivered + 1;
544 if (SCTP_MID_EQ(asoc->idata_supported, nxt_todel, control->mid)) {
545 /* can be delivered right away? */
546 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
547 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
549 /* EY it wont be queued if it could be delivered directly */
551 if (asoc->size_on_all_streams >= control->length) {
552 asoc->size_on_all_streams -= control->length;
555 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
557 asoc->size_on_all_streams = 0;
560 sctp_ucount_decr(asoc->cnt_on_all_streams);
561 strm->last_mid_delivered++;
562 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
563 sctp_add_to_readq(stcb->sctp_ep, stcb,
565 &stcb->sctp_socket->so_rcv, 1,
566 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_LOCKED);
567 TAILQ_FOREACH_SAFE(control, &strm->inqueue, next_instrm, at) {
569 nxt_todel = strm->last_mid_delivered + 1;
570 if (SCTP_MID_EQ(asoc->idata_supported, nxt_todel, control->mid) &&
571 (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG)) {
572 if (control->on_strm_q == SCTP_ON_ORDERED) {
573 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
574 if (asoc->size_on_all_streams >= control->length) {
575 asoc->size_on_all_streams -= control->length;
578 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
580 asoc->size_on_all_streams = 0;
583 sctp_ucount_decr(asoc->cnt_on_all_streams);
586 panic("Huh control: %p is on_strm_q: %d",
587 control, control->on_strm_q);
590 control->on_strm_q = 0;
591 strm->last_mid_delivered++;
593 * We ignore the return of deliver_data here
594 * since we always can hold the chunk on the
595 * d-queue. And we have a finite number that
596 * can be delivered from the strq.
598 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
599 sctp_log_strm_del(control, NULL,
600 SCTP_STR_LOG_FROM_IMMED_DEL);
602 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
603 sctp_add_to_readq(stcb->sctp_ep, stcb,
605 &stcb->sctp_socket->so_rcv, 1,
606 SCTP_READ_LOCK_NOT_HELD,
609 } else if (SCTP_MID_EQ(asoc->idata_supported, nxt_todel, control->mid)) {
617 * Ok, we did not deliver this guy, find the correct place
618 * to put it on the queue.
620 if (sctp_place_control_in_stream(strm, asoc, control)) {
621 SCTP_SNPRINTF(msg, sizeof(msg),
622 "Queue to str MID: %u duplicate", control->mid);
623 sctp_clean_up_control(stcb, control);
624 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
625 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
626 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
633 sctp_setup_tail_pointer(struct sctp_queued_to_read *control)
635 struct mbuf *m, *prev = NULL;
636 struct sctp_tcb *stcb;
638 stcb = control->stcb;
639 control->held_length = 0;
643 if (SCTP_BUF_LEN(m) == 0) {
644 /* Skip mbufs with NO length */
647 control->data = sctp_m_free(m);
650 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
651 m = SCTP_BUF_NEXT(prev);
654 control->tail_mbuf = prev;
659 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
660 if (control->on_read_q) {
662 * On read queue so we must increment the SB stuff,
663 * we assume caller has done any locks of SB.
665 sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
667 m = SCTP_BUF_NEXT(m);
670 control->tail_mbuf = prev;
675 sctp_add_to_tail_pointer(struct sctp_queued_to_read *control, struct mbuf *m, uint32_t *added)
677 struct mbuf *prev = NULL;
678 struct sctp_tcb *stcb;
680 stcb = control->stcb;
683 panic("Control broken");
688 if (control->tail_mbuf == NULL) {
690 sctp_m_freem(control->data);
692 sctp_setup_tail_pointer(control);
695 control->tail_mbuf->m_next = m;
697 if (SCTP_BUF_LEN(m) == 0) {
698 /* Skip mbufs with NO length */
701 control->tail_mbuf->m_next = sctp_m_free(m);
702 m = control->tail_mbuf->m_next;
704 SCTP_BUF_NEXT(prev) = sctp_m_free(m);
705 m = SCTP_BUF_NEXT(prev);
708 control->tail_mbuf = prev;
713 if (control->on_read_q) {
715 * On read queue so we must increment the SB stuff,
716 * we assume caller has done any locks of SB.
718 sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
720 *added += SCTP_BUF_LEN(m);
721 atomic_add_int(&control->length, SCTP_BUF_LEN(m));
722 m = SCTP_BUF_NEXT(m);
725 control->tail_mbuf = prev;
730 sctp_build_readq_entry_from_ctl(struct sctp_queued_to_read *nc, struct sctp_queued_to_read *control)
732 memset(nc, 0, sizeof(struct sctp_queued_to_read));
733 nc->sinfo_stream = control->sinfo_stream;
734 nc->mid = control->mid;
735 TAILQ_INIT(&nc->reasm);
736 nc->top_fsn = control->top_fsn;
737 nc->mid = control->mid;
738 nc->sinfo_flags = control->sinfo_flags;
739 nc->sinfo_ppid = control->sinfo_ppid;
740 nc->sinfo_context = control->sinfo_context;
741 nc->fsn_included = 0xffffffff;
742 nc->sinfo_tsn = control->sinfo_tsn;
743 nc->sinfo_cumtsn = control->sinfo_cumtsn;
744 nc->sinfo_assoc_id = control->sinfo_assoc_id;
745 nc->whoFrom = control->whoFrom;
746 atomic_add_int(&nc->whoFrom->ref_count, 1);
747 nc->stcb = control->stcb;
748 nc->port_from = control->port_from;
749 nc->do_not_ref_stcb = control->do_not_ref_stcb;
753 sctp_reset_a_control(struct sctp_queued_to_read *control,
754 struct sctp_inpcb *inp, uint32_t tsn)
756 control->fsn_included = tsn;
757 if (control->on_read_q) {
759 * We have to purge it from there, hopefully this will work
762 TAILQ_REMOVE(&inp->read_queue, control, next);
763 control->on_read_q = 0;
768 sctp_handle_old_unordered_data(struct sctp_tcb *stcb,
769 struct sctp_association *asoc,
770 struct sctp_stream_in *strm,
771 struct sctp_queued_to_read *control,
773 int inp_read_lock_held)
776 * Special handling for the old un-ordered data chunk. All the
777 * chunks/TSN's go to mid 0. So we have to do the old style watching
778 * to see if we have it all. If you return one, no other control
779 * entries on the un-ordered queue will be looked at. In theory
780 * there should be no others entries in reality, unless the guy is
781 * sending both unordered NDATA and unordered DATA...
783 struct sctp_tmit_chunk *chk, *lchk, *tchk;
785 struct sctp_queued_to_read *nc;
788 if (control->first_frag_seen == 0) {
789 /* Nothing we can do, we have not seen the first piece yet */
792 /* Collapse any we can */
795 fsn = control->fsn_included + 1;
796 /* Now what can we add? */
797 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, lchk) {
798 if (chk->rec.data.fsn == fsn) {
800 sctp_alloc_a_readq(stcb, nc);
804 memset(nc, 0, sizeof(struct sctp_queued_to_read));
805 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
806 sctp_add_chk_to_control(control, strm, stcb, asoc, chk, SCTP_READ_LOCK_NOT_HELD);
810 if (control->end_added) {
812 if (!TAILQ_EMPTY(&control->reasm)) {
814 * Ok we have to move anything left
815 * on the control queue to a new
818 sctp_build_readq_entry_from_ctl(nc, control);
819 tchk = TAILQ_FIRST(&control->reasm);
820 if (tchk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
821 TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
822 if (asoc->size_on_reasm_queue >= tchk->send_size) {
823 asoc->size_on_reasm_queue -= tchk->send_size;
826 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, tchk->send_size);
828 asoc->size_on_reasm_queue = 0;
831 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
832 nc->first_frag_seen = 1;
833 nc->fsn_included = tchk->rec.data.fsn;
834 nc->data = tchk->data;
835 nc->sinfo_ppid = tchk->rec.data.ppid;
836 nc->sinfo_tsn = tchk->rec.data.tsn;
837 sctp_mark_non_revokable(asoc, tchk->rec.data.tsn);
839 sctp_free_a_chunk(stcb, tchk, SCTP_SO_NOT_LOCKED);
840 sctp_setup_tail_pointer(nc);
841 tchk = TAILQ_FIRST(&control->reasm);
843 /* Spin the rest onto the queue */
845 TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
846 TAILQ_INSERT_TAIL(&nc->reasm, tchk, sctp_next);
847 tchk = TAILQ_FIRST(&control->reasm);
850 * Now lets add it to the queue
851 * after removing control
853 TAILQ_INSERT_TAIL(&strm->uno_inqueue, nc, next_instrm);
854 nc->on_strm_q = SCTP_ON_UNORDERED;
855 if (control->on_strm_q) {
856 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
857 control->on_strm_q = 0;
860 if (control->pdapi_started) {
861 strm->pd_api_started = 0;
862 control->pdapi_started = 0;
864 if (control->on_strm_q) {
865 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
866 control->on_strm_q = 0;
867 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
869 if (control->on_read_q == 0) {
870 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
871 &stcb->sctp_socket->so_rcv, control->end_added,
872 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
874 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
875 if ((nc->first_frag_seen) && !TAILQ_EMPTY(&nc->reasm)) {
877 * Switch to the new guy and
883 if (nc->on_strm_q == 0) {
884 sctp_free_a_readq(stcb, nc);
889 sctp_free_a_readq(stcb, nc);
896 if (cnt_added && strm->pd_api_started) {
897 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
899 if ((control->length > pd_point) && (strm->pd_api_started == 0)) {
900 strm->pd_api_started = 1;
901 control->pdapi_started = 1;
902 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
903 &stcb->sctp_socket->so_rcv, control->end_added,
904 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
905 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
913 sctp_inject_old_unordered_data(struct sctp_tcb *stcb,
914 struct sctp_association *asoc,
915 struct sctp_queued_to_read *control,
916 struct sctp_tmit_chunk *chk,
919 struct sctp_tmit_chunk *at;
923 * Here we need to place the chunk into the control structure sorted
924 * in the correct order.
926 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
927 /* Its the very first one. */
928 SCTPDBG(SCTP_DEBUG_XXX,
929 "chunk is a first fsn: %u becomes fsn_included\n",
931 at = TAILQ_FIRST(&control->reasm);
932 if (at && SCTP_TSN_GT(chk->rec.data.fsn, at->rec.data.fsn)) {
934 * The first chunk in the reassembly is a smaller
935 * TSN than this one, even though this has a first,
936 * it must be from a subsequent msg.
940 if (control->first_frag_seen) {
942 * In old un-ordered we can reassembly on one
943 * control multiple messages. As long as the next
944 * FIRST is greater then the old first (TSN i.e. FSN
950 if (SCTP_TSN_GT(chk->rec.data.fsn, control->fsn_included)) {
952 * Easy way the start of a new guy beyond
957 if ((chk->rec.data.fsn == control->fsn_included) ||
958 (control->pdapi_started)) {
960 * Ok this should not happen, if it does we
961 * started the pd-api on the higher TSN
962 * (since the equals part is a TSN failure
965 * We are completly hosed in that case since
966 * I have no way to recover. This really
967 * will only happen if we can get more TSN's
968 * higher before the pd-api-point.
970 sctp_abort_in_reasm(stcb, control, chk,
972 SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
977 * Ok we have two firsts and the one we just got is
978 * smaller than the one we previously placed.. yuck!
979 * We must swap them out.
982 tdata = control->data;
983 control->data = chk->data;
985 /* Save the lengths */
986 chk->send_size = control->length;
987 /* Recompute length of control and tail pointer */
988 sctp_setup_tail_pointer(control);
989 /* Fix the FSN included */
990 tmp = control->fsn_included;
991 control->fsn_included = chk->rec.data.fsn;
992 chk->rec.data.fsn = tmp;
993 /* Fix the TSN included */
994 tmp = control->sinfo_tsn;
995 control->sinfo_tsn = chk->rec.data.tsn;
996 chk->rec.data.tsn = tmp;
997 /* Fix the PPID included */
998 tmp = control->sinfo_ppid;
999 control->sinfo_ppid = chk->rec.data.ppid;
1000 chk->rec.data.ppid = tmp;
1001 /* Fix tail pointer */
1004 control->first_frag_seen = 1;
1005 control->fsn_included = chk->rec.data.fsn;
1006 control->top_fsn = chk->rec.data.fsn;
1007 control->sinfo_tsn = chk->rec.data.tsn;
1008 control->sinfo_ppid = chk->rec.data.ppid;
1009 control->data = chk->data;
1010 sctp_mark_non_revokable(asoc, chk->rec.data.tsn);
1012 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1013 sctp_setup_tail_pointer(control);
1018 TAILQ_FOREACH(at, &control->reasm, sctp_next) {
1019 if (SCTP_TSN_GT(at->rec.data.fsn, chk->rec.data.fsn)) {
1021 * This one in queue is bigger than the new one,
1022 * insert the new one before at.
1024 asoc->size_on_reasm_queue += chk->send_size;
1025 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1027 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1029 } else if (at->rec.data.fsn == chk->rec.data.fsn) {
1031 * They sent a duplicate fsn number. This really
1032 * should not happen since the FSN is a TSN and it
1033 * should have been dropped earlier.
1035 sctp_abort_in_reasm(stcb, control, chk,
1037 SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1041 if (inserted == 0) {
1042 /* Its at the end */
1043 asoc->size_on_reasm_queue += chk->send_size;
1044 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1045 control->top_fsn = chk->rec.data.fsn;
1046 TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
1051 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc,
1052 struct sctp_stream_in *strm, int inp_read_lock_held)
1055 * Given a stream, strm, see if any of the SSN's on it that are
1056 * fragmented are ready to deliver. If so go ahead and place them on
1057 * the read queue. In so placing if we have hit the end, then we
1058 * need to remove them from the stream's queue.
1060 struct sctp_queued_to_read *control, *nctl = NULL;
1061 uint32_t next_to_del;
1065 if (stcb->sctp_socket) {
1066 pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
1067 stcb->sctp_ep->partial_delivery_point);
1069 pd_point = stcb->sctp_ep->partial_delivery_point;
1071 control = TAILQ_FIRST(&strm->uno_inqueue);
1073 if ((control != NULL) &&
1074 (asoc->idata_supported == 0)) {
1075 /* Special handling needed for "old" data format */
1076 if (sctp_handle_old_unordered_data(stcb, asoc, strm, control, pd_point, inp_read_lock_held)) {
1080 if (strm->pd_api_started) {
1081 /* Can't add more */
1085 SCTPDBG(SCTP_DEBUG_XXX, "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u -uo\n",
1086 control, control->end_added, control->mid, control->top_fsn, control->fsn_included);
1087 nctl = TAILQ_NEXT(control, next_instrm);
1088 if (control->end_added) {
1089 /* We just put the last bit on */
1090 if (control->on_strm_q) {
1092 if (control->on_strm_q != SCTP_ON_UNORDERED) {
1093 panic("Huh control: %p on_q: %d -- not unordered?",
1094 control, control->on_strm_q);
1097 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
1098 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
1099 if (asoc->size_on_all_streams >= control->length) {
1100 asoc->size_on_all_streams -= control->length;
1103 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1105 asoc->size_on_all_streams = 0;
1108 sctp_ucount_decr(asoc->cnt_on_all_streams);
1109 control->on_strm_q = 0;
1111 if (control->on_read_q == 0) {
1112 sctp_add_to_readq(stcb->sctp_ep, stcb,
1114 &stcb->sctp_socket->so_rcv, control->end_added,
1115 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1118 /* Can we do a PD-API for this un-ordered guy? */
1119 if ((control->length >= pd_point) && (strm->pd_api_started == 0)) {
1120 strm->pd_api_started = 1;
1121 control->pdapi_started = 1;
1122 sctp_add_to_readq(stcb->sctp_ep, stcb,
1124 &stcb->sctp_socket->so_rcv, control->end_added,
1125 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1133 control = TAILQ_FIRST(&strm->inqueue);
1134 if (strm->pd_api_started) {
1135 /* Can't add more */
1138 if (control == NULL) {
1141 if (SCTP_MID_EQ(asoc->idata_supported, strm->last_mid_delivered, control->mid)) {
1143 * Ok the guy at the top was being partially delivered
1144 * completed, so we remove it. Note the pd_api flag was
1145 * taken off when the chunk was merged on in
1146 * sctp_queue_data_for_reasm below.
1148 nctl = TAILQ_NEXT(control, next_instrm);
1149 SCTPDBG(SCTP_DEBUG_XXX,
1150 "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (lastdel: %u)- o\n",
1151 control, control->end_added, control->mid,
1152 control->top_fsn, control->fsn_included,
1153 strm->last_mid_delivered);
1154 if (control->end_added) {
1155 if (control->on_strm_q) {
1157 if (control->on_strm_q != SCTP_ON_ORDERED) {
1158 panic("Huh control: %p on_q: %d -- not ordered?",
1159 control, control->on_strm_q);
1162 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
1163 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
1164 if (asoc->size_on_all_streams >= control->length) {
1165 asoc->size_on_all_streams -= control->length;
1168 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1170 asoc->size_on_all_streams = 0;
1173 sctp_ucount_decr(asoc->cnt_on_all_streams);
1174 control->on_strm_q = 0;
1176 if (strm->pd_api_started && control->pdapi_started) {
1177 control->pdapi_started = 0;
1178 strm->pd_api_started = 0;
1180 if (control->on_read_q == 0) {
1181 sctp_add_to_readq(stcb->sctp_ep, stcb,
1183 &stcb->sctp_socket->so_rcv, control->end_added,
1184 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1189 if (strm->pd_api_started) {
1191 * Can't add more must have gotten an un-ordered above being
1192 * partially delivered.
1197 next_to_del = strm->last_mid_delivered + 1;
1199 SCTPDBG(SCTP_DEBUG_XXX,
1200 "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (nxtdel: %u)- o\n",
1201 control, control->end_added, control->mid, control->top_fsn, control->fsn_included,
1203 nctl = TAILQ_NEXT(control, next_instrm);
1204 if (SCTP_MID_EQ(asoc->idata_supported, control->mid, next_to_del) &&
1205 (control->first_frag_seen)) {
1208 /* Ok we can deliver it onto the stream. */
1209 if (control->end_added) {
1210 /* We are done with it afterwards */
1211 if (control->on_strm_q) {
1213 if (control->on_strm_q != SCTP_ON_ORDERED) {
1214 panic("Huh control: %p on_q: %d -- not ordered?",
1215 control, control->on_strm_q);
1218 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
1219 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
1220 if (asoc->size_on_all_streams >= control->length) {
1221 asoc->size_on_all_streams -= control->length;
1224 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1226 asoc->size_on_all_streams = 0;
1229 sctp_ucount_decr(asoc->cnt_on_all_streams);
1230 control->on_strm_q = 0;
1234 if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
1236 * A singleton now slipping through - mark
1237 * it non-revokable too
1239 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
1240 } else if (control->end_added == 0) {
1242 * Check if we can defer adding until its
1245 if ((control->length < pd_point) || (strm->pd_api_started)) {
1247 * Don't need it or cannot add more
1248 * (one being delivered that way)
1253 done = (control->end_added) && (control->last_frag_seen);
1254 if (control->on_read_q == 0) {
1256 if (asoc->size_on_all_streams >= control->length) {
1257 asoc->size_on_all_streams -= control->length;
1260 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
1262 asoc->size_on_all_streams = 0;
1265 strm->pd_api_started = 1;
1266 control->pdapi_started = 1;
1268 sctp_add_to_readq(stcb->sctp_ep, stcb,
1270 &stcb->sctp_socket->so_rcv, control->end_added,
1271 inp_read_lock_held, SCTP_SO_NOT_LOCKED);
1273 strm->last_mid_delivered = next_to_del;
1285 sctp_add_chk_to_control(struct sctp_queued_to_read *control,
1286 struct sctp_stream_in *strm,
1287 struct sctp_tcb *stcb, struct sctp_association *asoc,
1288 struct sctp_tmit_chunk *chk, int hold_rlock)
1291 * Given a control and a chunk, merge the data from the chk onto the
1292 * control and free up the chunk resources.
1297 if (control->on_read_q && (hold_rlock == 0)) {
1299 * Its being pd-api'd so we must do some locks.
1301 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1304 if (control->data == NULL) {
1305 control->data = chk->data;
1306 sctp_setup_tail_pointer(control);
1308 sctp_add_to_tail_pointer(control, chk->data, &added);
1310 control->fsn_included = chk->rec.data.fsn;
1311 asoc->size_on_reasm_queue -= chk->send_size;
1312 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
1313 sctp_mark_non_revokable(asoc, chk->rec.data.tsn);
1315 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1316 control->first_frag_seen = 1;
1317 control->sinfo_tsn = chk->rec.data.tsn;
1318 control->sinfo_ppid = chk->rec.data.ppid;
1320 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1322 if ((control->on_strm_q) && (control->on_read_q)) {
1323 if (control->pdapi_started) {
1324 control->pdapi_started = 0;
1325 strm->pd_api_started = 0;
1327 if (control->on_strm_q == SCTP_ON_UNORDERED) {
1329 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
1330 control->on_strm_q = 0;
1331 } else if (control->on_strm_q == SCTP_ON_ORDERED) {
1333 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
1335 * Don't need to decrement
1336 * size_on_all_streams, since control is on
1339 sctp_ucount_decr(asoc->cnt_on_all_streams);
1340 control->on_strm_q = 0;
1342 } else if (control->on_strm_q) {
1343 panic("Unknown state on ctrl: %p on_strm_q: %d", control,
1344 control->on_strm_q);
1348 control->end_added = 1;
1349 control->last_frag_seen = 1;
1352 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1354 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1359 * Dump onto the re-assembly queue, in its proper place. After dumping on the
1360 * queue, see if anthing can be delivered. If so pull it off (or as much as
1361 * we can. If we run out of space then we must dump what we can and set the
1362 * appropriate flag to say we queued what we could.
1365 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
1366 struct sctp_queued_to_read *control,
1367 struct sctp_tmit_chunk *chk,
1368 int created_control,
1369 int *abort_flag, uint32_t tsn)
1372 struct sctp_tmit_chunk *at, *nat;
1373 struct sctp_stream_in *strm;
1374 int do_wakeup, unordered;
1377 strm = &asoc->strmin[control->sinfo_stream];
1379 * For old un-ordered data chunks.
1381 if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
1386 /* Must be added to the stream-in queue */
1387 if (created_control) {
1388 if ((unordered == 0) || (asoc->idata_supported)) {
1389 sctp_ucount_incr(asoc->cnt_on_all_streams);
1391 if (sctp_place_control_in_stream(strm, asoc, control)) {
1392 /* Duplicate SSN? */
1393 sctp_abort_in_reasm(stcb, control, chk,
1395 SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1396 sctp_clean_up_control(stcb, control);
1399 if ((tsn == (asoc->cumulative_tsn + 1) && (asoc->idata_supported == 0))) {
1401 * Ok we created this control and now lets validate
1402 * that its legal i.e. there is a B bit set, if not
1403 * and we have up to the cum-ack then its invalid.
1405 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
1406 sctp_abort_in_reasm(stcb, control, chk,
1408 SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1413 if ((asoc->idata_supported == 0) && (unordered == 1)) {
1414 sctp_inject_old_unordered_data(stcb, asoc, control, chk, abort_flag);
1418 * Ok we must queue the chunk into the reasembly portion: o if its
1419 * the first it goes to the control mbuf. o if its not first but the
1420 * next in sequence it goes to the control, and each succeeding one
1421 * in order also goes. o if its not in order we place it on the list
1424 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1425 /* Its the very first one. */
1426 SCTPDBG(SCTP_DEBUG_XXX,
1427 "chunk is a first fsn: %u becomes fsn_included\n",
1429 if (control->first_frag_seen) {
1431 * Error on senders part, they either sent us two
1432 * data chunks with FIRST, or they sent two
1433 * un-ordered chunks that were fragmented at the
1434 * same time in the same stream.
1436 sctp_abort_in_reasm(stcb, control, chk,
1438 SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1441 control->first_frag_seen = 1;
1442 control->sinfo_ppid = chk->rec.data.ppid;
1443 control->sinfo_tsn = chk->rec.data.tsn;
1444 control->fsn_included = chk->rec.data.fsn;
1445 control->data = chk->data;
1446 sctp_mark_non_revokable(asoc, chk->rec.data.tsn);
1448 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1449 sctp_setup_tail_pointer(control);
1450 asoc->size_on_all_streams += control->length;
1452 /* Place the chunk in our list */
1455 if (control->last_frag_seen == 0) {
1456 /* Still willing to raise highest FSN seen */
1457 if (SCTP_TSN_GT(chk->rec.data.fsn, control->top_fsn)) {
1458 SCTPDBG(SCTP_DEBUG_XXX,
1459 "We have a new top_fsn: %u\n",
1461 control->top_fsn = chk->rec.data.fsn;
1463 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1464 SCTPDBG(SCTP_DEBUG_XXX,
1465 "The last fsn is now in place fsn: %u\n",
1467 control->last_frag_seen = 1;
1468 if (SCTP_TSN_GT(control->top_fsn, chk->rec.data.fsn)) {
1469 SCTPDBG(SCTP_DEBUG_XXX,
1470 "New fsn: %u is not at top_fsn: %u -- abort\n",
1473 sctp_abort_in_reasm(stcb, control, chk,
1475 SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1479 if (asoc->idata_supported || control->first_frag_seen) {
1481 * For IDATA we always check since we know
1482 * that the first fragment is 0. For old
1483 * DATA we have to receive the first before
1484 * we know the first FSN (which is the TSN).
1486 if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn)) {
1488 * We have already delivered up to
1491 sctp_abort_in_reasm(stcb, control, chk,
1493 SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1498 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1499 /* Second last? huh? */
1500 SCTPDBG(SCTP_DEBUG_XXX,
1501 "Duplicate last fsn: %u (top: %u) -- abort\n",
1502 chk->rec.data.fsn, control->top_fsn);
1503 sctp_abort_in_reasm(stcb, control,
1505 SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1508 if (asoc->idata_supported || control->first_frag_seen) {
1510 * For IDATA we always check since we know
1511 * that the first fragment is 0. For old
1512 * DATA we have to receive the first before
1513 * we know the first FSN (which is the TSN).
1516 if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn)) {
1518 * We have already delivered up to
1521 SCTPDBG(SCTP_DEBUG_XXX,
1522 "New fsn: %u is already seen in included_fsn: %u -- abort\n",
1523 chk->rec.data.fsn, control->fsn_included);
1524 sctp_abort_in_reasm(stcb, control, chk,
1526 SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1531 * validate not beyond top FSN if we have seen last
1534 if (SCTP_TSN_GT(chk->rec.data.fsn, control->top_fsn)) {
1535 SCTPDBG(SCTP_DEBUG_XXX,
1536 "New fsn: %u is beyond or at top_fsn: %u -- abort\n",
1539 sctp_abort_in_reasm(stcb, control, chk,
1541 SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1546 * If we reach here, we need to place the new chunk in the
1547 * reassembly for this control.
1549 SCTPDBG(SCTP_DEBUG_XXX,
1550 "chunk is a not first fsn: %u needs to be inserted\n",
1552 TAILQ_FOREACH(at, &control->reasm, sctp_next) {
1553 if (SCTP_TSN_GT(at->rec.data.fsn, chk->rec.data.fsn)) {
1554 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
1555 /* Last not at the end? huh? */
1556 SCTPDBG(SCTP_DEBUG_XXX,
1557 "Last fragment not last in list: -- abort\n");
1558 sctp_abort_in_reasm(stcb, control,
1560 SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1564 * This one in queue is bigger than the new
1565 * one, insert the new one before at.
1567 SCTPDBG(SCTP_DEBUG_XXX,
1568 "Insert it before fsn: %u\n",
1570 asoc->size_on_reasm_queue += chk->send_size;
1571 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1572 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1575 } else if (at->rec.data.fsn == chk->rec.data.fsn) {
1577 * Gak, He sent me a duplicate str seq
1581 * foo bar, I guess I will just free this
1582 * new guy, should we abort too? FIX ME
1583 * MAYBE? Or it COULD be that the SSN's have
1584 * wrapped. Maybe I should compare to TSN
1585 * somehow... sigh for now just blow away
1588 SCTPDBG(SCTP_DEBUG_XXX,
1589 "Duplicate to fsn: %u -- abort\n",
1591 sctp_abort_in_reasm(stcb, control,
1593 SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1597 if (inserted == 0) {
1598 /* Goes on the end */
1599 SCTPDBG(SCTP_DEBUG_XXX, "Inserting at tail of list fsn: %u\n",
1601 asoc->size_on_reasm_queue += chk->send_size;
1602 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1603 TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
1607 * Ok lets see if we can suck any up into the control structure that
1608 * are in seq if it makes sense.
1612 * If the first fragment has not been seen there is no sense in
1615 if (control->first_frag_seen) {
1616 next_fsn = control->fsn_included + 1;
1617 TAILQ_FOREACH_SAFE(at, &control->reasm, sctp_next, nat) {
1618 if (at->rec.data.fsn == next_fsn) {
1619 /* We can add this one now to the control */
1620 SCTPDBG(SCTP_DEBUG_XXX,
1621 "Adding more to control: %p at: %p fsn: %u next_fsn: %u included: %u\n",
1624 next_fsn, control->fsn_included);
1625 TAILQ_REMOVE(&control->reasm, at, sctp_next);
1626 lenadded = sctp_add_chk_to_control(control, strm, stcb, asoc, at, SCTP_READ_LOCK_NOT_HELD);
1627 if (control->on_read_q) {
1631 * We only add to the
1632 * size-on-all-streams if its not on
1633 * the read q. The read q flag will
1634 * cause a sballoc so its accounted
1637 asoc->size_on_all_streams += lenadded;
1640 if (control->end_added && control->pdapi_started) {
1641 if (strm->pd_api_started) {
1642 strm->pd_api_started = 0;
1643 control->pdapi_started = 0;
1645 if (control->on_read_q == 0) {
1646 sctp_add_to_readq(stcb->sctp_ep, stcb,
1648 &stcb->sctp_socket->so_rcv, control->end_added,
1649 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1659 /* Need to wakeup the reader */
1660 sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1664 static struct sctp_queued_to_read *
1665 sctp_find_reasm_entry(struct sctp_stream_in *strm, uint32_t mid, int ordered, int idata_supported)
1667 struct sctp_queued_to_read *control;
1670 TAILQ_FOREACH(control, &strm->inqueue, next_instrm) {
1671 if (SCTP_MID_EQ(idata_supported, control->mid, mid)) {
1676 if (idata_supported) {
1677 TAILQ_FOREACH(control, &strm->uno_inqueue, next_instrm) {
1678 if (SCTP_MID_EQ(idata_supported, control->mid, mid)) {
1683 control = TAILQ_FIRST(&strm->uno_inqueue);
1690 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1691 struct mbuf **m, int offset, int chk_length,
1692 struct sctp_nets *net, uint32_t *high_tsn, int *abort_flag,
1693 int *break_flag, int last_chunk, uint8_t chk_type)
1695 struct sctp_tmit_chunk *chk = NULL; /* make gcc happy */
1696 struct sctp_stream_in *strm;
1697 uint32_t tsn, fsn, gap, mid;
1700 int need_reasm_check = 0;
1702 struct mbuf *op_err;
1703 char msg[SCTP_DIAG_INFO_LEN];
1704 struct sctp_queued_to_read *control, *ncontrol;
1707 struct sctp_stream_reset_list *liste;
1710 int created_control = 0;
1712 if (chk_type == SCTP_IDATA) {
1713 struct sctp_idata_chunk *chunk, chunk_buf;
1715 chunk = (struct sctp_idata_chunk *)sctp_m_getptr(*m, offset,
1716 sizeof(struct sctp_idata_chunk), (uint8_t *)&chunk_buf);
1717 chk_flags = chunk->ch.chunk_flags;
1718 clen = sizeof(struct sctp_idata_chunk);
1719 tsn = ntohl(chunk->dp.tsn);
1720 sid = ntohs(chunk->dp.sid);
1721 mid = ntohl(chunk->dp.mid);
1722 if (chk_flags & SCTP_DATA_FIRST_FRAG) {
1724 ppid = chunk->dp.ppid_fsn.ppid;
1726 fsn = ntohl(chunk->dp.ppid_fsn.fsn);
1727 ppid = 0xffffffff; /* Use as an invalid value. */
1730 struct sctp_data_chunk *chunk, chunk_buf;
1732 chunk = (struct sctp_data_chunk *)sctp_m_getptr(*m, offset,
1733 sizeof(struct sctp_data_chunk), (uint8_t *)&chunk_buf);
1734 chk_flags = chunk->ch.chunk_flags;
1735 clen = sizeof(struct sctp_data_chunk);
1736 tsn = ntohl(chunk->dp.tsn);
1737 sid = ntohs(chunk->dp.sid);
1738 mid = (uint32_t)(ntohs(chunk->dp.ssn));
1740 ppid = chunk->dp.ppid;
1742 if ((size_t)chk_length == clen) {
1744 * Need to send an abort since we had a empty data chunk.
1746 op_err = sctp_generate_no_user_data_cause(tsn);
1747 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1748 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1752 if ((chk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1753 asoc->send_sack = 1;
1755 ordered = ((chk_flags & SCTP_DATA_UNORDERED) == 0);
1756 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1757 sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1762 SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, chk_type, tsn);
1763 if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1764 /* It is a duplicate */
1765 SCTP_STAT_INCR(sctps_recvdupdata);
1766 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1767 /* Record a dup for the next outbound sack */
1768 asoc->dup_tsns[asoc->numduptsns] = tsn;
1771 asoc->send_sack = 1;
1774 /* Calculate the number of TSN's between the base and this TSN */
1775 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1776 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1777 /* Can't hold the bit in the mapping at max array, toss it */
1780 if (gap >= (uint32_t)(asoc->mapping_array_size << 3)) {
1781 SCTP_TCB_LOCK_ASSERT(stcb);
1782 if (sctp_expand_mapping_array(asoc, gap)) {
1783 /* Can't expand, drop it */
1787 if (SCTP_TSN_GT(tsn, *high_tsn)) {
1790 /* See if we have received this one already */
1791 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
1792 SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1793 SCTP_STAT_INCR(sctps_recvdupdata);
1794 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1795 /* Record a dup for the next outbound sack */
1796 asoc->dup_tsns[asoc->numduptsns] = tsn;
1799 asoc->send_sack = 1;
1803 * Check to see about the GONE flag, duplicates would cause a sack
1804 * to be sent up above
1806 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1807 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1808 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1810 * wait a minute, this guy is gone, there is no longer a
1811 * receiver. Send peer an ABORT!
1813 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1814 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1819 * Now before going further we see if there is room. If NOT then we
1820 * MAY let one through only IF this TSN is the one we are waiting
1821 * for on a partial delivery API.
1824 /* Is the stream valid? */
1825 if (sid >= asoc->streamincnt) {
1826 struct sctp_error_invalid_stream *cause;
1828 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_invalid_stream),
1829 0, M_NOWAIT, 1, MT_DATA);
1830 if (op_err != NULL) {
1831 /* add some space up front so prepend will work well */
1832 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1833 cause = mtod(op_err, struct sctp_error_invalid_stream *);
1835 * Error causes are just param's and this one has
1836 * two back to back phdr, one with the error type
1837 * and size, the other with the streamid and a rsvd
1839 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_invalid_stream);
1840 cause->cause.code = htons(SCTP_CAUSE_INVALID_STREAM);
1841 cause->cause.length = htons(sizeof(struct sctp_error_invalid_stream));
1842 cause->stream_id = htons(sid);
1843 cause->reserved = htons(0);
1844 sctp_queue_op_err(stcb, op_err);
1846 SCTP_STAT_INCR(sctps_badsid);
1847 SCTP_TCB_LOCK_ASSERT(stcb);
1848 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1849 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1850 asoc->highest_tsn_inside_nr_map = tsn;
1852 if (tsn == (asoc->cumulative_tsn + 1)) {
1853 /* Update cum-ack */
1854 asoc->cumulative_tsn = tsn;
1859 * If its a fragmented message, lets see if we can find the control
1860 * on the reassembly queues.
1862 if ((chk_type == SCTP_IDATA) &&
1863 ((chk_flags & SCTP_DATA_FIRST_FRAG) == 0) &&
1866 * The first *must* be fsn 0, and other (middle/end) pieces
1867 * can *not* be fsn 0. XXX: This can happen in case of a
1868 * wrap around. Ignore is for now.
1870 SCTP_SNPRINTF(msg, sizeof(msg), "FSN zero for MID=%8.8x, but flags=%2.2x", mid, chk_flags);
1873 control = sctp_find_reasm_entry(&asoc->strmin[sid], mid, ordered, asoc->idata_supported);
1874 SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags:0x%x look for control on queues %p\n",
1875 chk_flags, control);
1876 if ((chk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1877 /* See if we can find the re-assembly entity */
1878 if (control != NULL) {
1879 /* We found something, does it belong? */
1880 if (ordered && (mid != control->mid)) {
1881 SCTP_SNPRINTF(msg, sizeof(msg), "Reassembly problem (MID=%8.8x)", mid);
1883 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1884 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1885 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1889 if (ordered && ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED)) {
1891 * We can't have a switched order with an
1894 SCTP_SNPRINTF(msg, sizeof(msg),
1895 "All fragments of a user message must be ordered or unordered (TSN=%8.8x)",
1899 if (!ordered && (((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) == 0)) {
1901 * We can't have a switched unordered with a
1904 SCTP_SNPRINTF(msg, sizeof(msg),
1905 "All fragments of a user message must be ordered or unordered (TSN=%8.8x)",
1912 * Its a complete segment. Lets validate we don't have a
1913 * re-assembly going on with the same Stream/Seq (for
1914 * ordered) or in the same Stream for unordered.
1916 if (control != NULL) {
1917 if (ordered || asoc->idata_supported) {
1918 SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x dup detected on MID: %u\n",
1920 SCTP_SNPRINTF(msg, sizeof(msg), "Duplicate MID=%8.8x detected.", mid);
1923 if ((tsn == control->fsn_included + 1) &&
1924 (control->end_added == 0)) {
1925 SCTP_SNPRINTF(msg, sizeof(msg),
1926 "Illegal message sequence, missing end for MID: %8.8x",
1927 control->fsn_included);
1935 /* now do the tests */
1936 if (((asoc->cnt_on_all_streams +
1937 asoc->cnt_on_reasm_queue +
1938 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1939 (((int)asoc->my_rwnd) <= 0)) {
1941 * When we have NO room in the rwnd we check to make sure
1942 * the reader is doing its job...
1944 if (stcb->sctp_socket->so_rcv.sb_cc) {
1945 /* some to read, wake-up */
1946 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1948 /* now is it in the mapping array of what we have accepted? */
1949 if (chk_type == SCTP_DATA) {
1950 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
1951 SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1952 /* Nope not in the valid range dump it */
1954 sctp_set_rwnd(stcb, asoc);
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 SCTP_STAT_INCR(sctps_datadropchklmt);
1960 SCTP_STAT_INCR(sctps_datadroprwnd);
1966 if (control == NULL) {
1969 if (SCTP_TSN_GT(fsn, control->top_fsn)) {
1974 #ifdef SCTP_ASOCLOG_OF_TSNS
1975 SCTP_TCB_LOCK_ASSERT(stcb);
1976 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1977 asoc->tsn_in_at = 0;
1978 asoc->tsn_in_wrapped = 1;
1980 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1981 asoc->in_tsnlog[asoc->tsn_in_at].strm = sid;
1982 asoc->in_tsnlog[asoc->tsn_in_at].seq = mid;
1983 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1984 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1985 asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1986 asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1987 asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1991 * Before we continue lets validate that we are not being fooled by
1992 * an evil attacker. We can only have Nk chunks based on our TSN
1993 * spread allowed by the mapping array N * 8 bits, so there is no
1994 * way our stream sequence numbers could have wrapped. We of course
1995 * only validate the FIRST fragment so the bit must be set.
1997 if ((chk_flags & SCTP_DATA_FIRST_FRAG) &&
1998 (TAILQ_EMPTY(&asoc->resetHead)) &&
1999 (chk_flags & SCTP_DATA_UNORDERED) == 0 &&
2000 SCTP_MID_GE(asoc->idata_supported, asoc->strmin[sid].last_mid_delivered, mid)) {
2001 /* The incoming sseq is behind where we last delivered? */
2002 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ: %u delivered: %u from peer, Abort!\n",
2003 mid, asoc->strmin[sid].last_mid_delivered);
2005 if (asoc->idata_supported) {
2006 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered MID=%8.8x, got TSN=%8.8x, SID=%4.4x, MID=%8.8x",
2007 asoc->strmin[sid].last_mid_delivered,
2012 SCTP_SNPRINTF(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
2013 (uint16_t)asoc->strmin[sid].last_mid_delivered,
2018 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2019 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_18;
2020 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
2024 if (chk_type == SCTP_IDATA) {
2025 the_len = (chk_length - sizeof(struct sctp_idata_chunk));
2027 the_len = (chk_length - sizeof(struct sctp_data_chunk));
2029 if (last_chunk == 0) {
2030 if (chk_type == SCTP_IDATA) {
2031 dmbuf = SCTP_M_COPYM(*m,
2032 (offset + sizeof(struct sctp_idata_chunk)),
2035 dmbuf = SCTP_M_COPYM(*m,
2036 (offset + sizeof(struct sctp_data_chunk)),
2039 #ifdef SCTP_MBUF_LOGGING
2040 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2041 sctp_log_mbc(dmbuf, SCTP_MBUF_ICOPY);
2045 /* We can steal the last chunk */
2049 /* lop off the top part */
2050 if (chk_type == SCTP_IDATA) {
2051 m_adj(dmbuf, (offset + sizeof(struct sctp_idata_chunk)));
2053 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
2055 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
2056 l_len = SCTP_BUF_LEN(dmbuf);
2059 * need to count up the size hopefully does not hit
2065 for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
2066 l_len += SCTP_BUF_LEN(lat);
2069 if (l_len > the_len) {
2070 /* Trim the end round bytes off too */
2071 m_adj(dmbuf, -(l_len - the_len));
2074 if (dmbuf == NULL) {
2075 SCTP_STAT_INCR(sctps_nomem);
2079 * Now no matter what, we need a control, get one if we don't have
2080 * one (we may have gotten it above when we found the message was
2083 if (control == NULL) {
2084 sctp_alloc_a_readq(stcb, control);
2085 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2090 if (control == NULL) {
2091 SCTP_STAT_INCR(sctps_nomem);
2094 if ((chk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
2097 control->data = dmbuf;
2098 control->tail_mbuf = NULL;
2099 for (mm = control->data; mm; mm = mm->m_next) {
2100 control->length += SCTP_BUF_LEN(mm);
2101 if (SCTP_BUF_NEXT(mm) == NULL) {
2102 control->tail_mbuf = mm;
2105 control->end_added = 1;
2106 control->last_frag_seen = 1;
2107 control->first_frag_seen = 1;
2108 control->fsn_included = fsn;
2109 control->top_fsn = fsn;
2111 created_control = 1;
2113 SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x ordered: %d MID: %u control: %p\n",
2114 chk_flags, ordered, mid, control);
2115 if ((chk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
2116 TAILQ_EMPTY(&asoc->resetHead) &&
2118 (SCTP_MID_EQ(asoc->idata_supported, asoc->strmin[sid].last_mid_delivered + 1, mid) &&
2119 TAILQ_EMPTY(&asoc->strmin[sid].inqueue)))) {
2120 /* Candidate for express delivery */
2122 * Its not fragmented, No PD-API is up, Nothing in the
2123 * delivery queue, Its un-ordered OR ordered and the next to
2124 * deliver AND nothing else is stuck on the stream queue,
2125 * And there is room for it in the socket buffer. Lets just
2126 * stuff it up the buffer....
2128 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2129 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
2130 asoc->highest_tsn_inside_nr_map = tsn;
2132 SCTPDBG(SCTP_DEBUG_XXX, "Injecting control: %p to be read (MID: %u)\n",
2135 sctp_add_to_readq(stcb->sctp_ep, stcb,
2136 control, &stcb->sctp_socket->so_rcv,
2137 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2139 if ((chk_flags & SCTP_DATA_UNORDERED) == 0) {
2140 /* for ordered, bump what we delivered */
2141 asoc->strmin[sid].last_mid_delivered++;
2143 SCTP_STAT_INCR(sctps_recvexpress);
2144 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2145 sctp_log_strm_del_alt(stcb, tsn, mid, sid,
2146 SCTP_STR_LOG_FROM_EXPRS_DEL);
2149 goto finish_express_del;
2152 /* Now will we need a chunk too? */
2153 if ((chk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2154 sctp_alloc_a_chunk(stcb, chk);
2156 /* No memory so we drop the chunk */
2157 SCTP_STAT_INCR(sctps_nomem);
2158 if (last_chunk == 0) {
2159 /* we copied it, free the copy */
2160 sctp_m_freem(dmbuf);
2164 chk->rec.data.tsn = tsn;
2165 chk->no_fr_allowed = 0;
2166 chk->rec.data.fsn = fsn;
2167 chk->rec.data.mid = mid;
2168 chk->rec.data.sid = sid;
2169 chk->rec.data.ppid = ppid;
2170 chk->rec.data.context = stcb->asoc.context;
2171 chk->rec.data.doing_fast_retransmit = 0;
2172 chk->rec.data.rcv_flags = chk_flags;
2174 chk->send_size = the_len;
2176 SCTPDBG(SCTP_DEBUG_XXX, "Building ck: %p for control: %p to be read (MID: %u)\n",
2179 atomic_add_int(&net->ref_count, 1);
2182 /* Set the appropriate TSN mark */
2183 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
2184 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2185 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
2186 asoc->highest_tsn_inside_nr_map = tsn;
2189 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2190 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
2191 asoc->highest_tsn_inside_map = tsn;
2194 /* Now is it complete (i.e. not fragmented)? */
2195 if ((chk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
2197 * Special check for when streams are resetting. We could be
2198 * more smart about this and check the actual stream to see
2199 * if it is not being reset.. that way we would not create a
2200 * HOLB when amongst streams being reset and those not being
2204 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2205 SCTP_TSN_GT(tsn, liste->tsn)) {
2207 * yep its past where we need to reset... go ahead
2210 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2212 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2214 struct sctp_queued_to_read *lcontrol, *nlcontrol;
2215 unsigned char inserted = 0;
2217 TAILQ_FOREACH_SAFE(lcontrol, &asoc->pending_reply_queue, next, nlcontrol) {
2218 if (SCTP_TSN_GT(control->sinfo_tsn, lcontrol->sinfo_tsn)) {
2222 TAILQ_INSERT_BEFORE(lcontrol, control, next);
2227 if (inserted == 0) {
2229 * must be put at end, use prevP
2230 * (all setup from loop) to setup
2233 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2236 goto finish_express_del;
2238 if (chk_flags & SCTP_DATA_UNORDERED) {
2239 /* queue directly into socket buffer */
2240 SCTPDBG(SCTP_DEBUG_XXX, "Unordered data to be read control: %p MID: %u\n",
2242 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2243 sctp_add_to_readq(stcb->sctp_ep, stcb,
2245 &stcb->sctp_socket->so_rcv, 1,
2246 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2249 SCTPDBG(SCTP_DEBUG_XXX, "Queue control: %p for reordering MID: %u\n", control,
2251 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag, &need_reasm_check);
2259 goto finish_express_del;
2261 /* If we reach here its a reassembly */
2262 need_reasm_check = 1;
2263 SCTPDBG(SCTP_DEBUG_XXX,
2264 "Queue data to stream for reasm control: %p MID: %u\n",
2266 sctp_queue_data_for_reasm(stcb, asoc, control, chk, created_control, abort_flag, tsn);
2269 * the assoc is now gone and chk was put onto the reasm
2270 * queue, which has all been freed.
2278 /* Here we tidy up things */
2279 if (tsn == (asoc->cumulative_tsn + 1)) {
2280 /* Update cum-ack */
2281 asoc->cumulative_tsn = tsn;
2287 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2289 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2291 SCTP_STAT_INCR(sctps_recvdata);
2292 /* Set it present please */
2293 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2294 sctp_log_strm_del_alt(stcb, tsn, mid, sid, SCTP_STR_LOG_FROM_MARK_TSN);
2296 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2297 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2298 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2300 if (need_reasm_check) {
2301 (void)sctp_deliver_reasm_check(stcb, asoc, &asoc->strmin[sid], SCTP_READ_LOCK_NOT_HELD);
2302 need_reasm_check = 0;
2304 /* check the special flag for stream resets */
2305 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2306 SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
2308 * we have finished working through the backlogged TSN's now
2309 * time to reset streams. 1: call reset function. 2: free
2310 * pending_reply space 3: distribute any chunks in
2311 * pending_reply_queue.
2313 sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
2314 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2315 sctp_send_deferred_reset_response(stcb, liste, SCTP_STREAM_RESET_RESULT_PERFORMED);
2316 SCTP_FREE(liste, SCTP_M_STRESET);
2317 /* sa_ignore FREED_MEMORY */
2318 liste = TAILQ_FIRST(&asoc->resetHead);
2319 if (TAILQ_EMPTY(&asoc->resetHead)) {
2320 /* All can be removed */
2321 TAILQ_FOREACH_SAFE(control, &asoc->pending_reply_queue, next, ncontrol) {
2322 TAILQ_REMOVE(&asoc->pending_reply_queue, control, next);
2323 strm = &asoc->strmin[control->sinfo_stream];
2324 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag, &need_reasm_check);
2328 if (need_reasm_check) {
2329 (void)sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_NOT_HELD);
2330 need_reasm_check = 0;
2334 TAILQ_FOREACH_SAFE(control, &asoc->pending_reply_queue, next, ncontrol) {
2335 if (SCTP_TSN_GT(control->sinfo_tsn, liste->tsn)) {
2339 * if control->sinfo_tsn is <= liste->tsn we
2340 * can process it which is the NOT of
2341 * control->sinfo_tsn > liste->tsn
2343 TAILQ_REMOVE(&asoc->pending_reply_queue, control, next);
2344 strm = &asoc->strmin[control->sinfo_stream];
2345 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag, &need_reasm_check);
2349 if (need_reasm_check) {
2350 (void)sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_NOT_HELD);
2351 need_reasm_check = 0;
2359 static const int8_t sctp_map_lookup_tab[256] = {
2360 0, 1, 0, 2, 0, 1, 0, 3,
2361 0, 1, 0, 2, 0, 1, 0, 4,
2362 0, 1, 0, 2, 0, 1, 0, 3,
2363 0, 1, 0, 2, 0, 1, 0, 5,
2364 0, 1, 0, 2, 0, 1, 0, 3,
2365 0, 1, 0, 2, 0, 1, 0, 4,
2366 0, 1, 0, 2, 0, 1, 0, 3,
2367 0, 1, 0, 2, 0, 1, 0, 6,
2368 0, 1, 0, 2, 0, 1, 0, 3,
2369 0, 1, 0, 2, 0, 1, 0, 4,
2370 0, 1, 0, 2, 0, 1, 0, 3,
2371 0, 1, 0, 2, 0, 1, 0, 5,
2372 0, 1, 0, 2, 0, 1, 0, 3,
2373 0, 1, 0, 2, 0, 1, 0, 4,
2374 0, 1, 0, 2, 0, 1, 0, 3,
2375 0, 1, 0, 2, 0, 1, 0, 7,
2376 0, 1, 0, 2, 0, 1, 0, 3,
2377 0, 1, 0, 2, 0, 1, 0, 4,
2378 0, 1, 0, 2, 0, 1, 0, 3,
2379 0, 1, 0, 2, 0, 1, 0, 5,
2380 0, 1, 0, 2, 0, 1, 0, 3,
2381 0, 1, 0, 2, 0, 1, 0, 4,
2382 0, 1, 0, 2, 0, 1, 0, 3,
2383 0, 1, 0, 2, 0, 1, 0, 6,
2384 0, 1, 0, 2, 0, 1, 0, 3,
2385 0, 1, 0, 2, 0, 1, 0, 4,
2386 0, 1, 0, 2, 0, 1, 0, 3,
2387 0, 1, 0, 2, 0, 1, 0, 5,
2388 0, 1, 0, 2, 0, 1, 0, 3,
2389 0, 1, 0, 2, 0, 1, 0, 4,
2390 0, 1, 0, 2, 0, 1, 0, 3,
2391 0, 1, 0, 2, 0, 1, 0, 8
2395 sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2398 * Now we also need to check the mapping array in a couple of ways.
2399 * 1) Did we move the cum-ack point?
2401 * When you first glance at this you might think that all entries
2402 * that make up the position of the cum-ack would be in the
2403 * nr-mapping array only.. i.e. things up to the cum-ack are always
2404 * deliverable. Thats true with one exception, when its a fragmented
2405 * message we may not deliver the data until some threshold (or all
2406 * of it) is in place. So we must OR the nr_mapping_array and
2407 * mapping_array to get a true picture of the cum-ack.
2409 struct sctp_association *asoc;
2412 int slide_from, slide_end, lgap, distance;
2413 uint32_t old_cumack, old_base, old_highest, highest_tsn;
2417 old_cumack = asoc->cumulative_tsn;
2418 old_base = asoc->mapping_array_base_tsn;
2419 old_highest = asoc->highest_tsn_inside_map;
2421 * We could probably improve this a small bit by calculating the
2422 * offset of the current cum-ack as the starting point.
2425 for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2426 val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
2430 /* there is a 0 bit */
2431 at += sctp_map_lookup_tab[val];
2435 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2437 if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
2438 SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2440 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2441 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2443 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2444 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2445 sctp_print_mapping_array(asoc);
2446 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2447 sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2449 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2450 asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2453 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2454 highest_tsn = asoc->highest_tsn_inside_nr_map;
2456 highest_tsn = asoc->highest_tsn_inside_map;
2458 if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2459 /* The complete array was completed by a single FR */
2460 /* highest becomes the cum-ack */
2466 /* clear the array */
2467 clr = ((at + 7) >> 3);
2468 if (clr > asoc->mapping_array_size) {
2469 clr = asoc->mapping_array_size;
2471 memset(asoc->mapping_array, 0, clr);
2472 memset(asoc->nr_mapping_array, 0, clr);
2474 for (i = 0; i < asoc->mapping_array_size; i++) {
2475 if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2476 SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2477 sctp_print_mapping_array(asoc);
2481 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2482 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2483 } else if (at >= 8) {
2484 /* we can slide the mapping array down */
2485 /* slide_from holds where we hit the first NON 0xff byte */
2488 * now calculate the ceiling of the move using our highest
2491 SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
2492 slide_end = (lgap >> 3);
2493 if (slide_end < slide_from) {
2494 sctp_print_mapping_array(asoc);
2496 panic("impossible slide");
2498 SCTP_PRINTF("impossible slide lgap: %x slide_end: %x slide_from: %x? at: %d\n",
2499 lgap, slide_end, slide_from, at);
2503 if (slide_end > asoc->mapping_array_size) {
2505 panic("would overrun buffer");
2507 SCTP_PRINTF("Gak, would have overrun map end: %d slide_end: %d\n",
2508 asoc->mapping_array_size, slide_end);
2509 slide_end = asoc->mapping_array_size;
2512 distance = (slide_end - slide_from) + 1;
2513 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2514 sctp_log_map(old_base, old_cumack, old_highest,
2515 SCTP_MAP_PREPARE_SLIDE);
2516 sctp_log_map((uint32_t)slide_from, (uint32_t)slide_end,
2517 (uint32_t)lgap, SCTP_MAP_SLIDE_FROM);
2519 if (distance + slide_from > asoc->mapping_array_size ||
2522 * Here we do NOT slide forward the array so that
2523 * hopefully when more data comes in to fill it up
2524 * we will be able to slide it forward. Really I
2525 * don't think this should happen :-0
2527 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2528 sctp_log_map((uint32_t)distance, (uint32_t)slide_from,
2529 (uint32_t)asoc->mapping_array_size,
2530 SCTP_MAP_SLIDE_NONE);
2535 for (ii = 0; ii < distance; ii++) {
2536 asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
2537 asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
2539 for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2540 asoc->mapping_array[ii] = 0;
2541 asoc->nr_mapping_array[ii] = 0;
2543 if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2544 asoc->highest_tsn_inside_map += (slide_from << 3);
2546 if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2547 asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2549 asoc->mapping_array_base_tsn += (slide_from << 3);
2550 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2551 sctp_log_map(asoc->mapping_array_base_tsn,
2552 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2553 SCTP_MAP_SLIDE_RESULT);
2560 sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2562 struct sctp_association *asoc;
2563 uint32_t highest_tsn;
2566 sctp_slide_mapping_arrays(stcb);
2568 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2569 highest_tsn = asoc->highest_tsn_inside_nr_map;
2571 highest_tsn = asoc->highest_tsn_inside_map;
2573 /* Is there a gap now? */
2574 is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2577 * Now we need to see if we need to queue a sack or just start the
2578 * timer (if allowed).
2580 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
2582 * Ok special case, in SHUTDOWN-SENT case. here we maker
2583 * sure SACK timer is off and instead send a SHUTDOWN and a
2586 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2587 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2588 stcb->sctp_ep, stcb, NULL,
2589 SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2591 sctp_send_shutdown(stcb,
2592 ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2594 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2598 * CMT DAC algorithm: increase number of packets received
2601 stcb->asoc.cmt_dac_pkts_rcvd++;
2603 if ((stcb->asoc.send_sack == 1) || /* We need to send a
2605 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2607 (stcb->asoc.numduptsns) || /* we have dup's */
2608 (is_a_gap) || /* is still a gap */
2609 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2610 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)) { /* hit limit of pkts */
2611 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2612 (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
2613 (stcb->asoc.send_sack == 0) &&
2614 (stcb->asoc.numduptsns == 0) &&
2615 (stcb->asoc.delayed_ack) &&
2616 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2618 * CMT DAC algorithm: With CMT, delay acks
2619 * even in the face of reordering.
2620 * Therefore, if acks that do not have to be
2621 * sent because of the above reasons, will
2622 * be delayed. That is, acks that would have
2623 * been sent due to gap reports will be
2624 * delayed with DAC. Start the delayed ack
2627 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2628 stcb->sctp_ep, stcb, NULL);
2631 * Ok we must build a SACK since the timer
2632 * is pending, we got our first packet OR
2633 * there are gaps or duplicates.
2635 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
2636 SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
2637 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2640 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2641 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2642 stcb->sctp_ep, stcb, NULL);
2649 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2650 struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2651 struct sctp_nets *net, uint32_t *high_tsn)
2653 struct sctp_chunkhdr *ch, chunk_buf;
2654 struct sctp_association *asoc;
2655 int num_chunks = 0; /* number of control chunks processed */
2657 int break_flag, last_chunk;
2658 int abort_flag = 0, was_a_gap;
2660 uint32_t highest_tsn;
2661 uint16_t chk_length;
2664 sctp_set_rwnd(stcb, &stcb->asoc);
2667 SCTP_TCB_LOCK_ASSERT(stcb);
2669 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2670 highest_tsn = asoc->highest_tsn_inside_nr_map;
2672 highest_tsn = asoc->highest_tsn_inside_map;
2674 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2676 * setup where we got the last DATA packet from for any SACK that
2677 * may need to go out. Don't bump the net. This is done ONLY when a
2678 * chunk is assigned.
2680 asoc->last_data_chunk_from = net;
2683 * Now before we proceed we must figure out if this is a wasted
2684 * cluster... i.e. it is a small packet sent in and yet the driver
2685 * underneath allocated a full cluster for it. If so we must copy it
2686 * to a smaller mbuf and free up the cluster mbuf. This will help
2687 * with cluster starvation.
2689 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2690 /* we only handle mbufs that are singletons.. not chains */
2691 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2693 /* ok lets see if we can copy the data up */
2696 /* get the pointers and copy */
2697 to = mtod(m, caddr_t *);
2698 from = mtod((*mm), caddr_t *);
2699 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2700 /* copy the length and free up the old */
2701 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2703 /* success, back copy */
2706 /* We are in trouble in the mbuf world .. yikes */
2710 /* get pointer to the first chunk header */
2711 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
2712 sizeof(struct sctp_chunkhdr),
2713 (uint8_t *)&chunk_buf);
2718 * process all DATA chunks...
2720 *high_tsn = asoc->cumulative_tsn;
2722 asoc->data_pkts_seen++;
2723 while (stop_proc == 0) {
2724 /* validate chunk length */
2725 chk_length = ntohs(ch->chunk_length);
2726 if (length - *offset < chk_length) {
2727 /* all done, mutulated chunk */
2731 if ((asoc->idata_supported == 1) &&
2732 (ch->chunk_type == SCTP_DATA)) {
2733 struct mbuf *op_err;
2734 char msg[SCTP_DIAG_INFO_LEN];
2736 SCTP_SNPRINTF(msg, sizeof(msg), "%s", "DATA chunk received when I-DATA was negotiated");
2737 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2738 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_21;
2739 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2742 if ((asoc->idata_supported == 0) &&
2743 (ch->chunk_type == SCTP_IDATA)) {
2744 struct mbuf *op_err;
2745 char msg[SCTP_DIAG_INFO_LEN];
2747 SCTP_SNPRINTF(msg, sizeof(msg), "%s", "I-DATA chunk received when DATA was negotiated");
2748 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2749 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_22;
2750 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2753 if ((ch->chunk_type == SCTP_DATA) ||
2754 (ch->chunk_type == SCTP_IDATA)) {
2757 if (ch->chunk_type == SCTP_DATA) {
2758 clen = sizeof(struct sctp_data_chunk);
2760 clen = sizeof(struct sctp_idata_chunk);
2762 if (chk_length < clen) {
2764 * Need to send an abort since we had a
2765 * invalid data chunk.
2767 struct mbuf *op_err;
2768 char msg[SCTP_DIAG_INFO_LEN];
2770 SCTP_SNPRINTF(msg, sizeof(msg), "%s chunk of length %u",
2771 ch->chunk_type == SCTP_DATA ? "DATA" : "I-DATA",
2773 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2774 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_23;
2775 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2778 #ifdef SCTP_AUDITING_ENABLED
2779 sctp_audit_log(0xB1, 0);
2781 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2786 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset,
2787 chk_length, net, high_tsn, &abort_flag, &break_flag,
2788 last_chunk, ch->chunk_type)) {
2796 * Set because of out of rwnd space and no
2797 * drop rep space left.
2803 /* not a data chunk in the data region */
2804 switch (ch->chunk_type) {
2805 case SCTP_INITIATION:
2806 case SCTP_INITIATION_ACK:
2807 case SCTP_SELECTIVE_ACK:
2808 case SCTP_NR_SELECTIVE_ACK:
2809 case SCTP_HEARTBEAT_REQUEST:
2810 case SCTP_HEARTBEAT_ACK:
2811 case SCTP_ABORT_ASSOCIATION:
2813 case SCTP_SHUTDOWN_ACK:
2814 case SCTP_OPERATION_ERROR:
2815 case SCTP_COOKIE_ECHO:
2816 case SCTP_COOKIE_ACK:
2819 case SCTP_SHUTDOWN_COMPLETE:
2820 case SCTP_AUTHENTICATION:
2821 case SCTP_ASCONF_ACK:
2822 case SCTP_PACKET_DROPPED:
2823 case SCTP_STREAM_RESET:
2824 case SCTP_FORWARD_CUM_TSN:
2828 * Now, what do we do with KNOWN
2829 * chunks that are NOT in the right
2832 * For now, I do nothing but ignore
2833 * them. We may later want to add
2834 * sysctl stuff to switch out and do
2835 * either an ABORT() or possibly
2838 struct mbuf *op_err;
2839 char msg[SCTP_DIAG_INFO_LEN];
2841 SCTP_SNPRINTF(msg, sizeof(msg), "DATA chunk followed by chunk of type %2.2x",
2843 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2844 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2849 * Unknown chunk type: use bit rules after
2852 if (chk_length < sizeof(struct sctp_chunkhdr)) {
2854 * Need to send an abort since we
2855 * had a invalid chunk.
2857 struct mbuf *op_err;
2858 char msg[SCTP_DIAG_INFO_LEN];
2860 SCTP_SNPRINTF(msg, sizeof(msg), "Chunk of length %u", chk_length);
2861 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2862 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
2863 sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2866 if (ch->chunk_type & 0x40) {
2867 /* Add a error report to the queue */
2868 struct mbuf *op_err;
2869 struct sctp_gen_error_cause *cause;
2871 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
2872 0, M_NOWAIT, 1, MT_DATA);
2873 if (op_err != NULL) {
2874 cause = mtod(op_err, struct sctp_gen_error_cause *);
2875 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
2876 cause->length = htons((uint16_t)(chk_length + sizeof(struct sctp_gen_error_cause)));
2877 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
2878 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
2879 if (SCTP_BUF_NEXT(op_err) != NULL) {
2880 sctp_queue_op_err(stcb, op_err);
2882 sctp_m_freem(op_err);
2886 if ((ch->chunk_type & 0x80) == 0) {
2887 /* discard the rest of this packet */
2889 } /* else skip this bad chunk and
2892 } /* switch of chunk type */
2894 *offset += SCTP_SIZE32(chk_length);
2895 if ((*offset >= length) || stop_proc) {
2896 /* no more data left in the mbuf chain */
2900 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
2901 sizeof(struct sctp_chunkhdr),
2902 (uint8_t *)&chunk_buf);
2911 * we need to report rwnd overrun drops.
2913 sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2917 * Did we get data, if so update the time for auto-close and
2918 * give peer credit for being alive.
2920 SCTP_STAT_INCR(sctps_recvpktwithdata);
2921 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2922 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2923 stcb->asoc.overall_error_count,
2925 SCTP_FROM_SCTP_INDATA,
2928 stcb->asoc.overall_error_count = 0;
2929 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2931 /* now service all of the reassm queue if needed */
2932 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
2933 /* Assure that we ack right away */
2934 stcb->asoc.send_sack = 1;
2936 /* Start a sack timer or QUEUE a SACK for sending */
2937 sctp_sack_check(stcb, was_a_gap);
2942 sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
2943 uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
2945 uint32_t *biggest_newly_acked_tsn,
2946 uint32_t *this_sack_lowest_newack,
2949 struct sctp_tmit_chunk *tp1;
2950 unsigned int theTSN;
2951 int j, wake_him = 0, circled = 0;
2953 /* Recover the tp1 we last saw */
2956 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2958 for (j = frag_strt; j <= frag_end; j++) {
2959 theTSN = j + last_tsn;
2961 if (tp1->rec.data.doing_fast_retransmit)
2965 * CMT: CUCv2 algorithm. For each TSN being
2966 * processed from the sent queue, track the
2967 * next expected pseudo-cumack, or
2968 * rtx_pseudo_cumack, if required. Separate
2969 * cumack trackers for first transmissions,
2970 * and retransmissions.
2972 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
2973 (tp1->whoTo->find_pseudo_cumack == 1) &&
2974 (tp1->snd_count == 1)) {
2975 tp1->whoTo->pseudo_cumack = tp1->rec.data.tsn;
2976 tp1->whoTo->find_pseudo_cumack = 0;
2978 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
2979 (tp1->whoTo->find_rtx_pseudo_cumack == 1) &&
2980 (tp1->snd_count > 1)) {
2981 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.tsn;
2982 tp1->whoTo->find_rtx_pseudo_cumack = 0;
2984 if (tp1->rec.data.tsn == theTSN) {
2985 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2987 * must be held until
2990 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2992 * If it is less than RESEND, it is
2993 * now no-longer in flight.
2994 * Higher values may already be set
2995 * via previous Gap Ack Blocks...
2996 * i.e. ACKED or RESEND.
2998 if (SCTP_TSN_GT(tp1->rec.data.tsn,
2999 *biggest_newly_acked_tsn)) {
3000 *biggest_newly_acked_tsn = tp1->rec.data.tsn;
3003 * CMT: SFR algo (and HTNA) - set
3004 * saw_newack to 1 for dest being
3005 * newly acked. update
3006 * this_sack_highest_newack if
3009 if (tp1->rec.data.chunk_was_revoked == 0)
3010 tp1->whoTo->saw_newack = 1;
3012 if (SCTP_TSN_GT(tp1->rec.data.tsn,
3013 tp1->whoTo->this_sack_highest_newack)) {
3014 tp1->whoTo->this_sack_highest_newack =
3018 * CMT DAC algo: also update
3019 * this_sack_lowest_newack
3021 if (*this_sack_lowest_newack == 0) {
3022 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3023 sctp_log_sack(*this_sack_lowest_newack,
3028 SCTP_LOG_TSN_ACKED);
3030 *this_sack_lowest_newack = tp1->rec.data.tsn;
3033 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
3034 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
3035 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
3036 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
3037 * Separate pseudo_cumack trackers for first transmissions and
3040 if (tp1->rec.data.tsn == tp1->whoTo->pseudo_cumack) {
3041 if (tp1->rec.data.chunk_was_revoked == 0) {
3042 tp1->whoTo->new_pseudo_cumack = 1;
3044 tp1->whoTo->find_pseudo_cumack = 1;
3046 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3047 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.tsn, SCTP_CWND_LOG_FROM_SACK);
3049 if (tp1->rec.data.tsn == tp1->whoTo->rtx_pseudo_cumack) {
3050 if (tp1->rec.data.chunk_was_revoked == 0) {
3051 tp1->whoTo->new_pseudo_cumack = 1;
3053 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3055 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3056 sctp_log_sack(*biggest_newly_acked_tsn,
3061 SCTP_LOG_TSN_ACKED);
3063 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3064 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3065 tp1->whoTo->flight_size,
3067 (uint32_t)(uintptr_t)tp1->whoTo,
3070 sctp_flight_size_decrease(tp1);
3071 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3072 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3075 sctp_total_flight_decrease(stcb, tp1);
3077 tp1->whoTo->net_ack += tp1->send_size;
3078 if (tp1->snd_count < 2) {
3080 * True non-retransmitted chunk
3082 tp1->whoTo->net_ack2 += tp1->send_size;
3089 sctp_calculate_rto(stcb,
3092 &tp1->sent_rcv_time,
3093 SCTP_RTT_FROM_DATA)) {
3096 if (tp1->whoTo->rto_needed == 0) {
3097 tp1->whoTo->rto_needed = 1;
3103 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3104 if (SCTP_TSN_GT(tp1->rec.data.tsn,
3105 stcb->asoc.this_sack_highest_gap)) {
3106 stcb->asoc.this_sack_highest_gap =
3109 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3110 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
3111 #ifdef SCTP_AUDITING_ENABLED
3112 sctp_audit_log(0xB2,
3113 (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
3118 * All chunks NOT UNSENT fall through here and are marked
3119 * (leave PR-SCTP ones that are to skip alone though)
3121 if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
3122 (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
3123 tp1->sent = SCTP_DATAGRAM_MARKED;
3125 if (tp1->rec.data.chunk_was_revoked) {
3126 /* deflate the cwnd */
3127 tp1->whoTo->cwnd -= tp1->book_size;
3128 tp1->rec.data.chunk_was_revoked = 0;
3130 /* NR Sack code here */
3132 (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
3133 if (stcb->asoc.strmout[tp1->rec.data.sid].chunks_on_queues > 0) {
3134 stcb->asoc.strmout[tp1->rec.data.sid].chunks_on_queues--;
3137 panic("No chunks on the queues for sid %u.", tp1->rec.data.sid);
3140 if ((stcb->asoc.strmout[tp1->rec.data.sid].chunks_on_queues == 0) &&
3141 (stcb->asoc.strmout[tp1->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
3142 TAILQ_EMPTY(&stcb->asoc.strmout[tp1->rec.data.sid].outqueue)) {
3143 stcb->asoc.trigger_reset = 1;
3145 tp1->sent = SCTP_DATAGRAM_NR_ACKED;
3151 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
3152 sctp_m_freem(tp1->data);
3159 } /* if (tp1->tsn == theTSN) */
3160 if (SCTP_TSN_GT(tp1->rec.data.tsn, theTSN)) {
3163 tp1 = TAILQ_NEXT(tp1, sctp_next);
3164 if ((tp1 == NULL) && (circled == 0)) {
3166 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3168 } /* end while (tp1) */
3171 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3173 /* In case the fragments were not in order we must reset */
3174 } /* end for (j = fragStart */
3176 return (wake_him); /* Return value only used for nr-sack */
3180 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3181 uint32_t last_tsn, uint32_t *biggest_tsn_acked,
3182 uint32_t *biggest_newly_acked_tsn, uint32_t *this_sack_lowest_newack,
3183 int num_seg, int num_nr_seg, int *rto_ok)
3185 struct sctp_gap_ack_block *frag, block;
3186 struct sctp_tmit_chunk *tp1;
3191 uint16_t frag_strt, frag_end, prev_frag_end;
3193 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3197 for (i = 0; i < (num_seg + num_nr_seg); i++) {
3200 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3202 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3203 sizeof(struct sctp_gap_ack_block), (uint8_t *)&block);
3204 *offset += sizeof(block);
3206 return (chunk_freed);
3208 frag_strt = ntohs(frag->start);
3209 frag_end = ntohs(frag->end);
3211 if (frag_strt > frag_end) {
3212 /* This gap report is malformed, skip it. */
3215 if (frag_strt <= prev_frag_end) {
3216 /* This gap report is not in order, so restart. */
3217 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3219 if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3220 *biggest_tsn_acked = last_tsn + frag_end;
3227 if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3228 non_revocable, &num_frs, biggest_newly_acked_tsn,
3229 this_sack_lowest_newack, rto_ok)) {
3232 prev_frag_end = frag_end;
3234 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3236 sctp_log_fr(*biggest_tsn_acked,
3237 *biggest_newly_acked_tsn,
3238 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3240 return (chunk_freed);
3244 sctp_check_for_revoked(struct sctp_tcb *stcb,
3245 struct sctp_association *asoc, uint32_t cumack,
3246 uint32_t biggest_tsn_acked)
3248 struct sctp_tmit_chunk *tp1;
3250 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3251 if (SCTP_TSN_GT(tp1->rec.data.tsn, cumack)) {
3253 * ok this guy is either ACK or MARKED. If it is
3254 * ACKED it has been previously acked but not this
3255 * time i.e. revoked. If it is MARKED it was ACK'ed
3258 if (SCTP_TSN_GT(tp1->rec.data.tsn, biggest_tsn_acked)) {
3261 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3262 /* it has been revoked */
3263 tp1->sent = SCTP_DATAGRAM_SENT;
3264 tp1->rec.data.chunk_was_revoked = 1;
3266 * We must add this stuff back in to assure
3267 * timers and such get started.
3269 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3270 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3271 tp1->whoTo->flight_size,
3273 (uint32_t)(uintptr_t)tp1->whoTo,
3276 sctp_flight_size_increase(tp1);
3277 sctp_total_flight_increase(stcb, tp1);
3279 * We inflate the cwnd to compensate for our
3280 * artificial inflation of the flight_size.
3282 tp1->whoTo->cwnd += tp1->book_size;
3283 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3284 sctp_log_sack(asoc->last_acked_seq,
3289 SCTP_LOG_TSN_REVOKED);
3291 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3292 /* it has been re-acked in this SACK */
3293 tp1->sent = SCTP_DATAGRAM_ACKED;
3296 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3302 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3303 uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3305 struct sctp_tmit_chunk *tp1;
3306 int strike_flag = 0;
3308 int tot_retrans = 0;
3309 uint32_t sending_seq;
3310 struct sctp_nets *net;
3311 int num_dests_sacked = 0;
3314 * select the sending_seq, this is either the next thing ready to be
3315 * sent but not transmitted, OR, the next seq we assign.
3317 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3319 sending_seq = asoc->sending_seq;
3321 sending_seq = tp1->rec.data.tsn;
3324 /* CMT DAC algo: finding out if SACK is a mixed SACK */
3325 if ((asoc->sctp_cmt_on_off > 0) &&
3326 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3327 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3328 if (net->saw_newack)
3332 if (stcb->asoc.prsctp_supported) {
3333 (void)SCTP_GETTIME_TIMEVAL(&now);
3335 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3337 if (tp1->no_fr_allowed) {
3338 /* this one had a timeout or something */
3341 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3342 if (tp1->sent < SCTP_DATAGRAM_RESEND)
3343 sctp_log_fr(biggest_tsn_newly_acked,
3346 SCTP_FR_LOG_CHECK_STRIKE);
3348 if (SCTP_TSN_GT(tp1->rec.data.tsn, biggest_tsn_acked) ||
3349 tp1->sent == SCTP_DATAGRAM_UNSENT) {
3353 if (stcb->asoc.prsctp_supported) {
3354 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3355 /* Is it expired? */
3356 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3357 /* Yes so drop it */
3358 if (tp1->data != NULL) {
3359 (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3360 SCTP_SO_NOT_LOCKED);
3366 if (SCTP_TSN_GT(tp1->rec.data.tsn, asoc->this_sack_highest_gap) &&
3367 !(accum_moved && asoc->fast_retran_loss_recovery)) {
3368 /* we are beyond the tsn in the sack */
3371 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3372 /* either a RESEND, ACKED, or MARKED */
3374 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3375 /* Continue strikin FWD-TSN chunks */
3376 tp1->rec.data.fwd_tsn_cnt++;
3381 * CMT : SFR algo (covers part of DAC and HTNA as well)
3383 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3385 * No new acks were receieved for data sent to this
3386 * dest. Therefore, according to the SFR algo for
3387 * CMT, no data sent to this dest can be marked for
3388 * FR using this SACK.
3391 } else if (tp1->whoTo &&
3392 SCTP_TSN_GT(tp1->rec.data.tsn,
3393 tp1->whoTo->this_sack_highest_newack) &&
3394 !(accum_moved && asoc->fast_retran_loss_recovery)) {
3396 * CMT: New acks were receieved for data sent to
3397 * this dest. But no new acks were seen for data
3398 * sent after tp1. Therefore, according to the SFR
3399 * algo for CMT, tp1 cannot be marked for FR using
3400 * this SACK. This step covers part of the DAC algo
3401 * and the HTNA algo as well.
3406 * Here we check to see if we were have already done a FR
3407 * and if so we see if the biggest TSN we saw in the sack is
3408 * smaller than the recovery point. If so we don't strike
3409 * the tsn... otherwise we CAN strike the TSN.
3412 * @@@ JRI: Check for CMT if (accum_moved &&
3413 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3416 if (accum_moved && asoc->fast_retran_loss_recovery) {
3418 * Strike the TSN if in fast-recovery and cum-ack
3421 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3422 sctp_log_fr(biggest_tsn_newly_acked,
3425 SCTP_FR_LOG_STRIKE_CHUNK);
3427 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3430 if ((asoc->sctp_cmt_on_off > 0) &&
3431 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3433 * CMT DAC algorithm: If SACK flag is set to
3434 * 0, then lowest_newack test will not pass
3435 * because it would have been set to the
3436 * cumack earlier. If not already to be
3437 * rtx'd, If not a mixed sack and if tp1 is
3438 * not between two sacked TSNs, then mark by
3439 * one more. NOTE that we are marking by one
3440 * additional time since the SACK DAC flag
3441 * indicates that two packets have been
3442 * received after this missing TSN.
3444 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3445 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.tsn)) {
3446 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3447 sctp_log_fr(16 + num_dests_sacked,
3450 SCTP_FR_LOG_STRIKE_CHUNK);
3455 } else if ((tp1->rec.data.doing_fast_retransmit) &&
3456 (asoc->sctp_cmt_on_off == 0)) {
3458 * For those that have done a FR we must take
3459 * special consideration if we strike. I.e the
3460 * biggest_newly_acked must be higher than the
3461 * sending_seq at the time we did the FR.
3464 #ifdef SCTP_FR_TO_ALTERNATE
3466 * If FR's go to new networks, then we must only do
3467 * this for singly homed asoc's. However if the FR's
3468 * go to the same network (Armando's work) then its
3469 * ok to FR multiple times.
3476 if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3477 tp1->rec.data.fast_retran_tsn)) {
3479 * Strike the TSN, since this ack is
3480 * beyond where things were when we
3483 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3484 sctp_log_fr(biggest_tsn_newly_acked,
3487 SCTP_FR_LOG_STRIKE_CHUNK);
3489 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3493 if ((asoc->sctp_cmt_on_off > 0) &&
3494 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3496 * CMT DAC algorithm: If
3497 * SACK flag is set to 0,
3498 * then lowest_newack test
3499 * will not pass because it
3500 * would have been set to
3501 * the cumack earlier. If
3502 * not already to be rtx'd,
3503 * If not a mixed sack and
3504 * if tp1 is not between two
3505 * sacked TSNs, then mark by
3506 * one more. NOTE that we
3507 * are marking by one
3508 * additional time since the
3509 * SACK DAC flag indicates
3510 * that two packets have
3511 * been received after this
3514 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3515 (num_dests_sacked == 1) &&
3516 SCTP_TSN_GT(this_sack_lowest_newack,
3517 tp1->rec.data.tsn)) {
3518 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3519 sctp_log_fr(32 + num_dests_sacked,
3522 SCTP_FR_LOG_STRIKE_CHUNK);
3524 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3532 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3535 } else if (SCTP_TSN_GT(tp1->rec.data.tsn,
3536 biggest_tsn_newly_acked)) {
3538 * We don't strike these: This is the HTNA
3539 * algorithm i.e. we don't strike If our TSN is
3540 * larger than the Highest TSN Newly Acked.
3544 /* Strike the TSN */
3545 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3546 sctp_log_fr(biggest_tsn_newly_acked,
3549 SCTP_FR_LOG_STRIKE_CHUNK);
3551 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3554 if ((asoc->sctp_cmt_on_off > 0) &&
3555 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3557 * CMT DAC algorithm: If SACK flag is set to
3558 * 0, then lowest_newack test will not pass
3559 * because it would have been set to the
3560 * cumack earlier. If not already to be
3561 * rtx'd, If not a mixed sack and if tp1 is
3562 * not between two sacked TSNs, then mark by
3563 * one more. NOTE that we are marking by one
3564 * additional time since the SACK DAC flag
3565 * indicates that two packets have been
3566 * received after this missing TSN.
3568 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3569 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.tsn)) {
3570 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3571 sctp_log_fr(48 + num_dests_sacked,
3574 SCTP_FR_LOG_STRIKE_CHUNK);
3580 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3581 struct sctp_nets *alt;
3583 /* fix counts and things */
3584 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3585 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3586 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3588 (uint32_t)(uintptr_t)tp1->whoTo,
3592 tp1->whoTo->net_ack++;
3593 sctp_flight_size_decrease(tp1);
3594 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3595 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3600 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3601 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3602 asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3604 /* add back to the rwnd */
3605 asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3607 /* remove from the total flight */
3608 sctp_total_flight_decrease(stcb, tp1);
3610 if ((stcb->asoc.prsctp_supported) &&
3611 (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3613 * Has it been retransmitted tv_sec times? -
3614 * we store the retran count there.
3616 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3617 /* Yes, so drop it */
3618 if (tp1->data != NULL) {
3619 (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3620 SCTP_SO_NOT_LOCKED);
3622 /* Make sure to flag we had a FR */
3623 if (tp1->whoTo != NULL) {
3624 tp1->whoTo->net_ack++;
3630 * SCTP_PRINTF("OK, we are now ready to FR this
3633 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3634 sctp_log_fr(tp1->rec.data.tsn, tp1->snd_count,
3638 /* This is a subsequent FR */
3639 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3641 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3642 if (asoc->sctp_cmt_on_off > 0) {
3644 * CMT: Using RTX_SSTHRESH policy for CMT.
3645 * If CMT is being used, then pick dest with
3646 * largest ssthresh for any retransmission.
3648 tp1->no_fr_allowed = 1;
3650 /* sa_ignore NO_NULL_CHK */
3651 if (asoc->sctp_cmt_pf > 0) {
3653 * JRS 5/18/07 - If CMT PF is on,
3654 * use the PF version of
3657 alt = sctp_find_alternate_net(stcb, alt, 2);
3660 * JRS 5/18/07 - If only CMT is on,
3661 * use the CMT version of
3664 /* sa_ignore NO_NULL_CHK */
3665 alt = sctp_find_alternate_net(stcb, alt, 1);
3671 * CUCv2: If a different dest is picked for
3672 * the retransmission, then new
3673 * (rtx-)pseudo_cumack needs to be tracked
3674 * for orig dest. Let CUCv2 track new (rtx-)
3675 * pseudo-cumack always.
3678 tp1->whoTo->find_pseudo_cumack = 1;
3679 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3681 } else { /* CMT is OFF */
3682 #ifdef SCTP_FR_TO_ALTERNATE
3683 /* Can we find an alternate? */
3684 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3687 * default behavior is to NOT retransmit
3688 * FR's to an alternate. Armando Caro's
3689 * paper details why.
3695 tp1->rec.data.doing_fast_retransmit = 1;
3697 /* mark the sending seq for possible subsequent FR's */
3699 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3700 * (uint32_t)tpi->rec.data.tsn);
3702 if (TAILQ_EMPTY(&asoc->send_queue)) {
3704 * If the queue of send is empty then its
3705 * the next sequence number that will be
3706 * assigned so we subtract one from this to
3707 * get the one we last sent.
3709 tp1->rec.data.fast_retran_tsn = sending_seq;
3712 * If there are chunks on the send queue
3713 * (unsent data that has made it from the
3714 * stream queues but not out the door, we
3715 * take the first one (which will have the
3716 * lowest TSN) and subtract one to get the
3719 struct sctp_tmit_chunk *ttt;
3721 ttt = TAILQ_FIRST(&asoc->send_queue);
3722 tp1->rec.data.fast_retran_tsn =
3728 * this guy had a RTO calculation pending on
3731 if ((tp1->whoTo != NULL) &&
3732 (tp1->whoTo->rto_needed == 0)) {
3733 tp1->whoTo->rto_needed = 1;
3737 if (alt != tp1->whoTo) {
3738 /* yes, there is an alternate. */
3739 sctp_free_remote_addr(tp1->whoTo);
3740 /* sa_ignore FREED_MEMORY */
3742 atomic_add_int(&alt->ref_count, 1);
3748 struct sctp_tmit_chunk *
3749 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3750 struct sctp_association *asoc)
3752 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3756 if (asoc->prsctp_supported == 0) {
3759 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3760 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3761 tp1->sent != SCTP_DATAGRAM_RESEND &&
3762 tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3763 /* no chance to advance, out of here */
3766 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
3767 if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3768 (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3769 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
3770 asoc->advanced_peer_ack_point,
3771 tp1->rec.data.tsn, 0, 0);
3774 if (!PR_SCTP_ENABLED(tp1->flags)) {
3776 * We can't fwd-tsn past any that are reliable aka
3777 * retransmitted until the asoc fails.
3782 (void)SCTP_GETTIME_TIMEVAL(&now);
3786 * now we got a chunk which is marked for another
3787 * retransmission to a PR-stream but has run out its chances
3788 * already maybe OR has been marked to skip now. Can we skip
3789 * it if its a resend?
3791 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3792 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3794 * Now is this one marked for resend and its time is
3797 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3798 /* Yes so drop it */
3800 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3801 1, SCTP_SO_NOT_LOCKED);
3805 * No, we are done when hit one for resend
3806 * whos time as not expired.
3812 * Ok now if this chunk is marked to drop it we can clean up
3813 * the chunk, advance our peer ack point and we can check
3816 if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3817 (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3818 /* advance PeerAckPoint goes forward */
3819 if (SCTP_TSN_GT(tp1->rec.data.tsn, asoc->advanced_peer_ack_point)) {
3820 asoc->advanced_peer_ack_point = tp1->rec.data.tsn;
3822 } else if (tp1->rec.data.tsn == asoc->advanced_peer_ack_point) {
3823 /* No update but we do save the chk */
3828 * If it is still in RESEND we can advance no
3838 sctp_fs_audit(struct sctp_association *asoc)
3840 struct sctp_tmit_chunk *chk;
3841 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3844 int entry_flight, entry_cnt;
3849 entry_flight = asoc->total_flight;
3850 entry_cnt = asoc->total_flight_count;
3852 if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
3855 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3856 if (chk->sent < SCTP_DATAGRAM_RESEND) {
3857 SCTP_PRINTF("Chk TSN: %u size: %d inflight cnt: %d\n",
3862 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3864 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3866 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3873 if ((inflight > 0) || (inbetween > 0)) {
3875 panic("Flight size-express incorrect? \n");
3877 SCTP_PRINTF("asoc->total_flight: %d cnt: %d\n",
3878 entry_flight, entry_cnt);
3880 SCTP_PRINTF("Flight size-express incorrect F: %d I: %d R: %d Ab: %d ACK: %d\n",
3881 inflight, inbetween, resend, above, acked);
3889 sctp_window_probe_recovery(struct sctp_tcb *stcb,
3890 struct sctp_association *asoc,
3891 struct sctp_tmit_chunk *tp1)
3893 tp1->window_probe = 0;
3894 if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3895 /* TSN's skipped we do NOT move back. */
3896 sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3897 tp1->whoTo ? tp1->whoTo->flight_size : 0,
3899 (uint32_t)(uintptr_t)tp1->whoTo,
3903 /* First setup this by shrinking flight */
3904 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3905 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3908 sctp_flight_size_decrease(tp1);
3909 sctp_total_flight_decrease(stcb, tp1);
3910 /* Now mark for resend */
3911 tp1->sent = SCTP_DATAGRAM_RESEND;
3912 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3914 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3915 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3916 tp1->whoTo->flight_size,
3918 (uint32_t)(uintptr_t)tp1->whoTo,
3924 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3925 uint32_t rwnd, int *abort_now, int ecne_seen)
3927 struct sctp_nets *net;
3928 struct sctp_association *asoc;
3929 struct sctp_tmit_chunk *tp1, *tp2;
3931 int win_probe_recovery = 0;
3932 int win_probe_recovered = 0;
3933 int j, done_once = 0;
3937 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3938 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3939 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3941 SCTP_TCB_LOCK_ASSERT(stcb);
3942 #ifdef SCTP_ASOCLOG_OF_TSNS
3943 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3944 stcb->asoc.cumack_log_at++;
3945 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3946 stcb->asoc.cumack_log_at = 0;
3950 old_rwnd = asoc->peers_rwnd;
3951 if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
3954 } else if (asoc->last_acked_seq == cumack) {
3955 /* Window update sack */
3956 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3957 (uint32_t)(asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3958 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3959 /* SWS sender side engages */
3960 asoc->peers_rwnd = 0;
3962 if (asoc->peers_rwnd > old_rwnd) {
3968 /* First setup for CC stuff */
3969 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3970 if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3971 /* Drag along the window_tsn for cwr's */
3972 net->cwr_window_tsn = cumack;
3974 net->prev_cwnd = net->cwnd;
3979 * CMT: Reset CUC and Fast recovery algo variables before
3982 net->new_pseudo_cumack = 0;
3983 net->will_exit_fast_recovery = 0;
3984 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3985 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3988 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3989 tp1 = TAILQ_LAST(&asoc->sent_queue,
3990 sctpchunk_listhead);
3991 send_s = tp1->rec.data.tsn + 1;
3993 send_s = asoc->sending_seq;
3995 if (SCTP_TSN_GE(cumack, send_s)) {
3996 struct mbuf *op_err;
3997 char msg[SCTP_DIAG_INFO_LEN];
4001 SCTP_SNPRINTF(msg, sizeof(msg),
4002 "Cum ack %8.8x greater or equal than TSN %8.8x",
4004 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4005 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4006 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4009 asoc->this_sack_highest_gap = cumack;
4010 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4011 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4012 stcb->asoc.overall_error_count,
4014 SCTP_FROM_SCTP_INDATA,
4017 stcb->asoc.overall_error_count = 0;
4018 if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
4019 /* process the new consecutive TSN first */
4020 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
4021 if (SCTP_TSN_GE(cumack, tp1->rec.data.tsn)) {
4022 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4023 SCTP_PRINTF("Warning, an unsent is now acked?\n");
4025 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4027 * If it is less than ACKED, it is
4028 * now no-longer in flight. Higher
4029 * values may occur during marking
4031 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4032 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4033 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4034 tp1->whoTo->flight_size,
4036 (uint32_t)(uintptr_t)tp1->whoTo,
4039 sctp_flight_size_decrease(tp1);
4040 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4041 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4044 /* sa_ignore NO_NULL_CHK */
4045 sctp_total_flight_decrease(stcb, tp1);
4047 tp1->whoTo->net_ack += tp1->send_size;
4048 if (tp1->snd_count < 2) {
4050 * True non-retransmitted
4053 tp1->whoTo->net_ack2 +=
4056 /* update RTO too? */
4059 sctp_calculate_rto(stcb,
4062 &tp1->sent_rcv_time,
4063 SCTP_RTT_FROM_DATA)) {
4066 if (tp1->whoTo->rto_needed == 0) {
4067 tp1->whoTo->rto_needed = 1;
4073 * CMT: CUCv2 algorithm. From the
4074 * cumack'd TSNs, for each TSN being
4075 * acked for the first time, set the
4076 * following variables for the
4077 * corresp destination.
4078 * new_pseudo_cumack will trigger a
4080 * find_(rtx_)pseudo_cumack will
4081 * trigger search for the next
4082 * expected (rtx-)pseudo-cumack.
4084 tp1->whoTo->new_pseudo_cumack = 1;
4085 tp1->whoTo->find_pseudo_cumack = 1;
4086 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4087 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4088 /* sa_ignore NO_NULL_CHK */
4089 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.tsn, SCTP_CWND_LOG_FROM_SACK);
4092 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4093 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4095 if (tp1->rec.data.chunk_was_revoked) {
4096 /* deflate the cwnd */
4097 tp1->whoTo->cwnd -= tp1->book_size;
4098 tp1->rec.data.chunk_was_revoked = 0;
4100 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4101 if (asoc->strmout[tp1->rec.data.sid].chunks_on_queues > 0) {
4102 asoc->strmout[tp1->rec.data.sid].chunks_on_queues--;
4105 panic("No chunks on the queues for sid %u.", tp1->rec.data.sid);
4109 if ((asoc->strmout[tp1->rec.data.sid].chunks_on_queues == 0) &&
4110 (asoc->strmout[tp1->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
4111 TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.sid].outqueue)) {
4112 asoc->trigger_reset = 1;
4114 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4116 /* sa_ignore NO_NULL_CHK */
4117 sctp_free_bufspace(stcb, asoc, tp1, 1);
4118 sctp_m_freem(tp1->data);
4121 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4122 sctp_log_sack(asoc->last_acked_seq,
4127 SCTP_LOG_FREE_SENT);
4129 asoc->sent_queue_cnt--;
4130 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4136 /* sa_ignore NO_NULL_CHK */
4137 if (stcb->sctp_socket) {
4138 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4139 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4140 /* sa_ignore NO_NULL_CHK */
4141 sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
4143 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4145 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4146 sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
4150 /* JRS - Use the congestion control given in the CC module */
4151 if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4152 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4153 if (net->net_ack2 > 0) {
4155 * Karn's rule applies to clearing error
4156 * count, this is optional.
4158 net->error_count = 0;
4159 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4160 /* addr came good */
4161 net->dest_state |= SCTP_ADDR_REACHABLE;
4162 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4163 0, (void *)net, SCTP_SO_NOT_LOCKED);
4165 if (net == stcb->asoc.primary_destination) {
4166 if (stcb->asoc.alternate) {
4168 * release the alternate,
4171 sctp_free_remote_addr(stcb->asoc.alternate);
4172 stcb->asoc.alternate = NULL;
4175 if (net->dest_state & SCTP_ADDR_PF) {
4176 net->dest_state &= ~SCTP_ADDR_PF;
4177 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4178 stcb->sctp_ep, stcb, net,
4179 SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4180 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4181 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4182 /* Done with this net */
4185 /* restore any doubled timers */
4186 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4187 if (net->RTO < stcb->asoc.minrto) {
4188 net->RTO = stcb->asoc.minrto;
4190 if (net->RTO > stcb->asoc.maxrto) {
4191 net->RTO = stcb->asoc.maxrto;
4195 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4197 asoc->last_acked_seq = cumack;
4199 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4200 /* nothing left in-flight */
4201 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4202 net->flight_size = 0;
4203 net->partial_bytes_acked = 0;
4205 asoc->total_flight = 0;
4206 asoc->total_flight_count = 0;
4210 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4211 (uint32_t)(asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4212 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4213 /* SWS sender side engages */
4214 asoc->peers_rwnd = 0;
4216 if (asoc->peers_rwnd > old_rwnd) {
4217 win_probe_recovery = 1;
4219 /* Now assure a timer where data is queued at */
4222 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4223 if (win_probe_recovery && (net->window_probe)) {
4224 win_probe_recovered = 1;
4226 * Find first chunk that was used with window probe
4227 * and clear the sent
4229 /* sa_ignore FREED_MEMORY */
4230 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4231 if (tp1->window_probe) {
4232 /* move back to data send queue */
4233 sctp_window_probe_recovery(stcb, asoc, tp1);
4238 if (net->flight_size) {
4240 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net);
4241 if (net->window_probe) {
4242 net->window_probe = 0;
4245 if (net->window_probe) {
4247 * In window probes we must assure a timer
4248 * is still running there
4250 net->window_probe = 0;
4251 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4252 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, net);
4254 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4255 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4257 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4262 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4263 (asoc->sent_queue_retran_cnt == 0) &&
4264 (win_probe_recovered == 0) &&
4267 * huh, this should not happen unless all packets are
4268 * PR-SCTP and marked to skip of course.
4270 if (sctp_fs_audit(asoc)) {
4271 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4272 net->flight_size = 0;
4274 asoc->total_flight = 0;
4275 asoc->total_flight_count = 0;
4276 asoc->sent_queue_retran_cnt = 0;
4277 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4278 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4279 sctp_flight_size_increase(tp1);
4280 sctp_total_flight_increase(stcb, tp1);
4281 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4282 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4289 /**********************************/
4290 /* Now what about shutdown issues */
4291 /**********************************/
4292 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4293 /* nothing left on sendqueue.. consider done */
4295 if ((asoc->stream_queue_cnt == 1) &&
4296 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4297 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4298 ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc))) {
4299 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
4301 if (((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4302 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4303 (asoc->stream_queue_cnt == 1) &&
4304 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4305 struct mbuf *op_err;
4309 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4310 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_28;
4311 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4314 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4315 (asoc->stream_queue_cnt == 0)) {
4316 struct sctp_nets *netp;
4318 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4319 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4320 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4322 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
4323 sctp_stop_timers_for_shutdown(stcb);
4324 if (asoc->alternate) {
4325 netp = asoc->alternate;
4327 netp = asoc->primary_destination;
4329 sctp_send_shutdown(stcb, netp);
4330 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4331 stcb->sctp_ep, stcb, netp);
4332 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4333 stcb->sctp_ep, stcb, NULL);
4334 } else if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4335 (asoc->stream_queue_cnt == 0)) {
4336 struct sctp_nets *netp;
4338 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4339 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
4340 sctp_stop_timers_for_shutdown(stcb);
4341 if (asoc->alternate) {
4342 netp = asoc->alternate;
4344 netp = asoc->primary_destination;
4346 sctp_send_shutdown_ack(stcb, netp);
4347 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4348 stcb->sctp_ep, stcb, netp);
4351 /*********************************************/
4352 /* Here we perform PR-SCTP procedures */
4354 /*********************************************/
4355 /* C1. update advancedPeerAckPoint */
4356 if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4357 asoc->advanced_peer_ack_point = cumack;
4359 /* PR-Sctp issues need to be addressed too */
4360 if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
4361 struct sctp_tmit_chunk *lchk;
4362 uint32_t old_adv_peer_ack_point;
4364 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4365 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4366 /* C3. See if we need to send a Fwd-TSN */
4367 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4369 * ISSUE with ECN, see FWD-TSN processing.
4371 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4372 send_forward_tsn(stcb, asoc);
4374 /* try to FR fwd-tsn's that get lost too */
4375 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
4376 send_forward_tsn(stcb, asoc);
4380 for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
4381 if (lchk->whoTo != NULL) {
4386 /* Assure a timer is up */
4387 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4388 stcb->sctp_ep, stcb, lchk->whoTo);
4391 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4392 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4394 stcb->asoc.peers_rwnd,
4395 stcb->asoc.total_flight,
4396 stcb->asoc.total_output_queue_size);
4401 sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
4402 struct sctp_tcb *stcb,
4403 uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4404 int *abort_now, uint8_t flags,
4405 uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4407 struct sctp_association *asoc;
4408 struct sctp_tmit_chunk *tp1, *tp2;
4409 uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4410 uint16_t wake_him = 0;
4411 uint32_t send_s = 0;
4413 int accum_moved = 0;
4414 int will_exit_fast_recovery = 0;
4415 uint32_t a_rwnd, old_rwnd;
4416 int win_probe_recovery = 0;
4417 int win_probe_recovered = 0;
4418 struct sctp_nets *net = NULL;
4421 uint8_t reneged_all = 0;
4422 uint8_t cmt_dac_flag;
4425 * we take any chance we can to service our queues since we cannot
4426 * get awoken when the socket is read from :<
4429 * Now perform the actual SACK handling: 1) Verify that it is not an
4430 * old sack, if so discard. 2) If there is nothing left in the send
4431 * queue (cum-ack is equal to last acked) then you have a duplicate
4432 * too, update any rwnd change and verify no timers are running.
4433 * then return. 3) Process any new consequtive data i.e. cum-ack
4434 * moved process these first and note that it moved. 4) Process any
4435 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4436 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4437 * sync up flightsizes and things, stop all timers and also check
4438 * for shutdown_pending state. If so then go ahead and send off the
4439 * shutdown. If in shutdown recv, send off the shutdown-ack and
4440 * start that timer, Ret. 9) Strike any non-acked things and do FR
4441 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4442 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4443 * if in shutdown_recv state.
4445 SCTP_TCB_LOCK_ASSERT(stcb);
4447 this_sack_lowest_newack = 0;
4448 SCTP_STAT_INCR(sctps_slowpath_sack);
4450 cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
4451 #ifdef SCTP_ASOCLOG_OF_TSNS
4452 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4453 stcb->asoc.cumack_log_at++;
4454 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4455 stcb->asoc.cumack_log_at = 0;
4460 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4461 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4462 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4465 old_rwnd = stcb->asoc.peers_rwnd;
4466 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4467 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4468 stcb->asoc.overall_error_count,
4470 SCTP_FROM_SCTP_INDATA,
4473 stcb->asoc.overall_error_count = 0;
4475 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4476 sctp_log_sack(asoc->last_acked_seq,
4483 if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4485 uint32_t *dupdata, dblock;
4487 for (i = 0; i < num_dup; i++) {
4488 dupdata = (uint32_t *)sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4489 sizeof(uint32_t), (uint8_t *)&dblock);
4490 if (dupdata == NULL) {
4493 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4497 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4498 tp1 = TAILQ_LAST(&asoc->sent_queue,
4499 sctpchunk_listhead);
4500 send_s = tp1->rec.data.tsn + 1;
4503 send_s = asoc->sending_seq;
4505 if (SCTP_TSN_GE(cum_ack, send_s)) {
4506 struct mbuf *op_err;
4507 char msg[SCTP_DIAG_INFO_LEN];
4510 * no way, we have not even sent this TSN out yet. Peer is
4511 * hopelessly messed up with us.
4513 SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4516 SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1: %p\n",
4517 tp1->rec.data.tsn, (void *)tp1);
4522 SCTP_SNPRINTF(msg, sizeof(msg),
4523 "Cum ack %8.8x greater or equal than TSN %8.8x",
4525 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4526 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_29;
4527 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4530 /**********************/
4531 /* 1) check the range */
4532 /**********************/
4533 if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4534 /* acking something behind */
4538 /* update the Rwnd of the peer */
4539 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4540 TAILQ_EMPTY(&asoc->send_queue) &&
4541 (asoc->stream_queue_cnt == 0)) {
4542 /* nothing left on send/sent and strmq */
4543 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4544 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4545 asoc->peers_rwnd, 0, 0, a_rwnd);
4547 asoc->peers_rwnd = a_rwnd;
4548 if (asoc->sent_queue_retran_cnt) {
4549 asoc->sent_queue_retran_cnt = 0;
4551 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4552 /* SWS sender side engages */
4553 asoc->peers_rwnd = 0;
4555 /* stop any timers */
4556 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4557 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4558 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4559 net->partial_bytes_acked = 0;
4560 net->flight_size = 0;
4562 asoc->total_flight = 0;
4563 asoc->total_flight_count = 0;
4567 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4568 * things. The total byte count acked is tracked in netAckSz AND
4569 * netAck2 is used to track the total bytes acked that are un-
4570 * amibguious and were never retransmitted. We track these on a per
4571 * destination address basis.
4573 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4574 if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4575 /* Drag along the window_tsn for cwr's */
4576 net->cwr_window_tsn = cum_ack;
4578 net->prev_cwnd = net->cwnd;
4583 * CMT: Reset CUC and Fast recovery algo variables before
4586 net->new_pseudo_cumack = 0;
4587 net->will_exit_fast_recovery = 0;
4588 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4589 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4593 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4594 * to be greater than the cumack. Also reset saw_newack to 0
4597 net->saw_newack = 0;
4598 net->this_sack_highest_newack = last_tsn;
4600 /* process the new consecutive TSN first */
4601 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4602 if (SCTP_TSN_GE(last_tsn, tp1->rec.data.tsn)) {
4603 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4605 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4607 * If it is less than ACKED, it is
4608 * now no-longer in flight. Higher
4609 * values may occur during marking
4611 if ((tp1->whoTo->dest_state &
4612 SCTP_ADDR_UNCONFIRMED) &&
4613 (tp1->snd_count < 2)) {
4615 * If there was no retran
4616 * and the address is
4617 * un-confirmed and we sent
4619 * sacked.. its confirmed,
4622 tp1->whoTo->dest_state &=
4623 ~SCTP_ADDR_UNCONFIRMED;
4625 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4626 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4627 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4628 tp1->whoTo->flight_size,
4630 (uint32_t)(uintptr_t)tp1->whoTo,
4633 sctp_flight_size_decrease(tp1);
4634 sctp_total_flight_decrease(stcb, tp1);
4635 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4636 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4640 tp1->whoTo->net_ack += tp1->send_size;
4642 /* CMT SFR and DAC algos */
4643 this_sack_lowest_newack = tp1->rec.data.tsn;
4644 tp1->whoTo->saw_newack = 1;
4646 if (tp1->snd_count < 2) {
4648 * True non-retransmitted
4651 tp1->whoTo->net_ack2 +=
4654 /* update RTO too? */
4657 sctp_calculate_rto(stcb,
4660 &tp1->sent_rcv_time,
4661 SCTP_RTT_FROM_DATA)) {
4664 if (tp1->whoTo->rto_needed == 0) {
4665 tp1->whoTo->rto_needed = 1;
4671 * CMT: CUCv2 algorithm. From the
4672 * cumack'd TSNs, for each TSN being
4673 * acked for the first time, set the
4674 * following variables for the
4675 * corresp destination.
4676 * new_pseudo_cumack will trigger a
4678 * find_(rtx_)pseudo_cumack will
4679 * trigger search for the next
4680 * expected (rtx-)pseudo-cumack.
4682 tp1->whoTo->new_pseudo_cumack = 1;
4683 tp1->whoTo->find_pseudo_cumack = 1;
4684 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4685 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4686 sctp_log_sack(asoc->last_acked_seq,
4691 SCTP_LOG_TSN_ACKED);
4693 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4694 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.tsn, SCTP_CWND_LOG_FROM_SACK);
4697 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4698 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4699 #ifdef SCTP_AUDITING_ENABLED
4700 sctp_audit_log(0xB3,
4701 (asoc->sent_queue_retran_cnt & 0x000000ff));
4704 if (tp1->rec.data.chunk_was_revoked) {
4705 /* deflate the cwnd */
4706 tp1->whoTo->cwnd -= tp1->book_size;
4707 tp1->rec.data.chunk_was_revoked = 0;
4709 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4710 tp1->sent = SCTP_DATAGRAM_ACKED;
4717 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4718 /* always set this up to cum-ack */
4719 asoc->this_sack_highest_gap = last_tsn;
4721 if ((num_seg > 0) || (num_nr_seg > 0)) {
4723 * thisSackHighestGap will increase while handling NEW
4724 * segments this_sack_highest_newack will increase while
4725 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4726 * used for CMT DAC algo. saw_newack will also change.
4728 if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4729 &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4730 num_seg, num_nr_seg, &rto_ok)) {
4734 * validate the biggest_tsn_acked in the gap acks if strict
4735 * adherence is wanted.
4737 if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4739 * peer is either confused or we are under attack.
4742 SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4743 biggest_tsn_acked, send_s);
4747 /*******************************************/
4748 /* cancel ALL T3-send timer if accum moved */
4749 /*******************************************/
4750 if (asoc->sctp_cmt_on_off > 0) {
4751 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4752 if (net->new_pseudo_cumack)
4753 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4755 SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4759 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4760 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4761 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
4765 /********************************************/
4766 /* drop the acked chunks from the sentqueue */
4767 /********************************************/
4768 asoc->last_acked_seq = cum_ack;
4770 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
4771 if (SCTP_TSN_GT(tp1->rec.data.tsn, cum_ack)) {
4774 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4775 if (asoc->strmout[tp1->rec.data.sid].chunks_on_queues > 0) {
4776 asoc->strmout[tp1->rec.data.sid].chunks_on_queues--;
4779 panic("No chunks on the queues for sid %u.", tp1->rec.data.sid);
4783 if ((asoc->strmout[tp1->rec.data.sid].chunks_on_queues == 0) &&
4784 (asoc->strmout[tp1->rec.data.sid].state == SCTP_STREAM_RESET_PENDING) &&
4785 TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.sid].outqueue)) {
4786 asoc->trigger_reset = 1;
4788 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4789 if (PR_SCTP_ENABLED(tp1->flags)) {
4790 if (asoc->pr_sctp_cnt != 0)
4791 asoc->pr_sctp_cnt--;
4793 asoc->sent_queue_cnt--;
4795 /* sa_ignore NO_NULL_CHK */
4796 sctp_free_bufspace(stcb, asoc, tp1, 1);
4797 sctp_m_freem(tp1->data);
4799 if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4800 asoc->sent_queue_cnt_removeable--;
4803 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4804 sctp_log_sack(asoc->last_acked_seq,
4809 SCTP_LOG_FREE_SENT);
4811 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4814 if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
4816 panic("Warning flight size is positive and should be 0");
4818 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4819 asoc->total_flight);
4821 asoc->total_flight = 0;
4824 /* sa_ignore NO_NULL_CHK */
4825 if ((wake_him) && (stcb->sctp_socket)) {
4826 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4827 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4828 sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
4830 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4832 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4833 sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
4837 if (asoc->fast_retran_loss_recovery && accum_moved) {
4838 if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4839 /* Setup so we will exit RFC2582 fast recovery */
4840 will_exit_fast_recovery = 1;
4844 * Check for revoked fragments:
4846 * if Previous sack - Had no frags then we can't have any revoked if
4847 * Previous sack - Had frag's then - If we now have frags aka
4848 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4849 * some of them. else - The peer revoked all ACKED fragments, since
4850 * we had some before and now we have NONE.
4854 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4855 asoc->saw_sack_with_frags = 1;
4856 } else if (asoc->saw_sack_with_frags) {
4857 int cnt_revoked = 0;
4859 /* Peer revoked all dg's marked or acked */
4860 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4861 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4862 tp1->sent = SCTP_DATAGRAM_SENT;
4863 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4864 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4865 tp1->whoTo->flight_size,
4867 (uint32_t)(uintptr_t)tp1->whoTo,
4870 sctp_flight_size_increase(tp1);
4871 sctp_total_flight_increase(stcb, tp1);
4872 tp1->rec.data.chunk_was_revoked = 1;
4874 * To ensure that this increase in
4875 * flightsize, which is artificial, does not
4876 * throttle the sender, we also increase the
4877 * cwnd artificially.
4879 tp1->whoTo->cwnd += tp1->book_size;
4886 asoc->saw_sack_with_frags = 0;
4889 asoc->saw_sack_with_nr_frags = 1;
4891 asoc->saw_sack_with_nr_frags = 0;
4893 /* JRS - Use the congestion control given in the CC module */
4894 if (ecne_seen == 0) {
4895 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4896 if (net->net_ack2 > 0) {
4898 * Karn's rule applies to clearing error
4899 * count, this is optional.
4901 net->error_count = 0;
4902 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4903 /* addr came good */
4904 net->dest_state |= SCTP_ADDR_REACHABLE;
4905 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4906 0, (void *)net, SCTP_SO_NOT_LOCKED);
4909 if (net == stcb->asoc.primary_destination) {
4910 if (stcb->asoc.alternate) {
4912 * release the alternate,
4915 sctp_free_remote_addr(stcb->asoc.alternate);
4916 stcb->asoc.alternate = NULL;
4920 if (net->dest_state & SCTP_ADDR_PF) {
4921 net->dest_state &= ~SCTP_ADDR_PF;
4922 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4923 stcb->sctp_ep, stcb, net,
4924 SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
4925 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4926 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4927 /* Done with this net */
4930 /* restore any doubled timers */
4931 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4932 if (net->RTO < stcb->asoc.minrto) {
4933 net->RTO = stcb->asoc.minrto;
4935 if (net->RTO > stcb->asoc.maxrto) {
4936 net->RTO = stcb->asoc.maxrto;
4940 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4943 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4944 /* nothing left in-flight */
4945 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4946 /* stop all timers */
4947 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4949 SCTP_FROM_SCTP_INDATA + SCTP_LOC_34);
4950 net->flight_size = 0;
4951 net->partial_bytes_acked = 0;
4953 asoc->total_flight = 0;
4954 asoc->total_flight_count = 0;
4957 /**********************************/
4958 /* Now what about shutdown issues */
4959 /**********************************/
4960 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4961 /* nothing left on sendqueue.. consider done */
4962 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4963 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4964 asoc->peers_rwnd, 0, 0, a_rwnd);
4966 asoc->peers_rwnd = a_rwnd;
4967 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4968 /* SWS sender side engages */
4969 asoc->peers_rwnd = 0;
4972 if ((asoc->stream_queue_cnt == 1) &&
4973 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4974 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4975 ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc))) {
4976 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
4978 if (((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4979 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4980 (asoc->stream_queue_cnt == 1) &&
4981 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4982 struct mbuf *op_err;
4986 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4987 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_35;
4988 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4991 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4992 (asoc->stream_queue_cnt == 0)) {
4993 struct sctp_nets *netp;
4995 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4996 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4997 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4999 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
5000 sctp_stop_timers_for_shutdown(stcb);
5001 if (asoc->alternate) {
5002 netp = asoc->alternate;
5004 netp = asoc->primary_destination;
5006 sctp_send_shutdown(stcb, netp);
5007 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5008 stcb->sctp_ep, stcb, netp);
5009 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5010 stcb->sctp_ep, stcb, NULL);
5012 } else if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5013 (asoc->stream_queue_cnt == 0)) {
5014 struct sctp_nets *netp;
5016 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5017 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
5018 sctp_stop_timers_for_shutdown(stcb);
5019 if (asoc->alternate) {
5020 netp = asoc->alternate;
5022 netp = asoc->primary_destination;
5024 sctp_send_shutdown_ack(stcb, netp);
5025 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5026 stcb->sctp_ep, stcb, netp);
5031 * Now here we are going to recycle net_ack for a different use...
5034 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5039 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5040 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5041 * automatically ensure that.
5043 if ((asoc->sctp_cmt_on_off > 0) &&
5044 SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
5045 (cmt_dac_flag == 0)) {
5046 this_sack_lowest_newack = cum_ack;
5048 if ((num_seg > 0) || (num_nr_seg > 0)) {
5049 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5050 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5052 /* JRS - Use the congestion control given in the CC module */
5053 asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5055 /* Now are we exiting loss recovery ? */
5056 if (will_exit_fast_recovery) {
5057 /* Ok, we must exit fast recovery */
5058 asoc->fast_retran_loss_recovery = 0;
5060 if ((asoc->sat_t3_loss_recovery) &&
5061 SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
5062 /* end satellite t3 loss recovery */
5063 asoc->sat_t3_loss_recovery = 0;
5068 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5069 if (net->will_exit_fast_recovery) {
5070 /* Ok, we must exit fast recovery */
5071 net->fast_retran_loss_recovery = 0;
5075 /* Adjust and set the new rwnd value */
5076 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5077 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5078 asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
5080 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5081 (uint32_t)(asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5082 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5083 /* SWS sender side engages */
5084 asoc->peers_rwnd = 0;
5086 if (asoc->peers_rwnd > old_rwnd) {
5087 win_probe_recovery = 1;
5091 * Now we must setup so we have a timer up for anyone with
5097 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5098 if (win_probe_recovery && (net->window_probe)) {
5099 win_probe_recovered = 1;
5101 * Find first chunk that was used with
5102 * window probe and clear the event. Put
5103 * it back into the send queue as if has
5106 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5107 if (tp1->window_probe) {
5108 sctp_window_probe_recovery(stcb, asoc, tp1);
5113 if (net->flight_size) {
5115 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5116 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5117 stcb->sctp_ep, stcb, net);
5119 if (net->window_probe) {
5120 net->window_probe = 0;
5123 if (net->window_probe) {
5125 * In window probes we must assure a timer
5126 * is still running there
5128 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5129 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5130 stcb->sctp_ep, stcb, net);
5132 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5133 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5135 SCTP_FROM_SCTP_INDATA + SCTP_LOC_36);
5140 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5141 (asoc->sent_queue_retran_cnt == 0) &&
5142 (win_probe_recovered == 0) &&
5145 * huh, this should not happen unless all packets are
5146 * PR-SCTP and marked to skip of course.
5148 if (sctp_fs_audit(asoc)) {
5149 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5150 net->flight_size = 0;
5152 asoc->total_flight = 0;
5153 asoc->total_flight_count = 0;
5154 asoc->sent_queue_retran_cnt = 0;
5155 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5156 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5157 sctp_flight_size_increase(tp1);
5158 sctp_total_flight_increase(stcb, tp1);
5159 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5160 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5167 /*********************************************/
5168 /* Here we perform PR-SCTP procedures */
5170 /*********************************************/
5171 /* C1. update advancedPeerAckPoint */
5172 if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5173 asoc->advanced_peer_ack_point = cum_ack;
5175 /* C2. try to further move advancedPeerAckPoint ahead */
5176 if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
5177 struct sctp_tmit_chunk *lchk;
5178 uint32_t old_adv_peer_ack_point;
5180 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5181 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5182 /* C3. See if we need to send a Fwd-TSN */
5183 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5185 * ISSUE with ECN, see FWD-TSN processing.
5187 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
5188 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
5189 0xee, cum_ack, asoc->advanced_peer_ack_point,
5190 old_adv_peer_ack_point);
5192 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5193 send_forward_tsn(stcb, asoc);
5195 /* try to FR fwd-tsn's that get lost too */
5196 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
5197 send_forward_tsn(stcb, asoc);
5201 for (; lchk != NULL; lchk = TAILQ_NEXT(lchk, sctp_next)) {
5202 if (lchk->whoTo != NULL) {
5207 /* Assure a timer is up */
5208 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5209 stcb->sctp_ep, stcb, lchk->whoTo);
5212 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5213 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5215 stcb->asoc.peers_rwnd,
5216 stcb->asoc.total_flight,
5217 stcb->asoc.total_output_queue_size);
5222 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5225 uint32_t cum_ack, a_rwnd;
5227 cum_ack = ntohl(cp->cumulative_tsn_ack);
5228 /* Arrange so a_rwnd does NOT change */
5229 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5231 /* Now call the express sack handling */
5232 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5236 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5237 struct sctp_stream_in *strmin)
5239 struct sctp_queued_to_read *control, *ncontrol;
5240 struct sctp_association *asoc;
5242 int need_reasm_check = 0;
5245 mid = strmin->last_mid_delivered;
5247 * First deliver anything prior to and including the stream no that
5250 TAILQ_FOREACH_SAFE(control, &strmin->inqueue, next_instrm, ncontrol) {
5251 if (SCTP_MID_GE(asoc->idata_supported, mid, control->mid)) {
5252 /* this is deliverable now */
5253 if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
5254 if (control->on_strm_q) {
5255 if (control->on_strm_q == SCTP_ON_ORDERED) {
5256 TAILQ_REMOVE(&strmin->inqueue, control, next_instrm);
5257 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5258 TAILQ_REMOVE(&strmin->uno_inqueue, control, next_instrm);
5261 panic("strmin: %p ctl: %p unknown %d",
5262 strmin, control, control->on_strm_q);
5265 control->on_strm_q = 0;
5267 /* subtract pending on streams */
5268 if (asoc->size_on_all_streams >= control->length) {
5269 asoc->size_on_all_streams -= control->length;
5272 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5274 asoc->size_on_all_streams = 0;
5277 sctp_ucount_decr(asoc->cnt_on_all_streams);
5278 /* deliver it to at least the delivery-q */
5279 if (stcb->sctp_socket) {
5280 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
5281 sctp_add_to_readq(stcb->sctp_ep, stcb,
5283 &stcb->sctp_socket->so_rcv,
5284 1, SCTP_READ_LOCK_HELD,
5285 SCTP_SO_NOT_LOCKED);
5288 /* Its a fragmented message */
5289 if (control->first_frag_seen) {
5291 * Make it so this is next to
5292 * deliver, we restore later
5294 strmin->last_mid_delivered = control->mid - 1;
5295 need_reasm_check = 1;
5300 /* no more delivery now. */
5304 if (need_reasm_check) {
5307 ret = sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin, SCTP_READ_LOCK_HELD);
5308 if (SCTP_MID_GT(asoc->idata_supported, mid, strmin->last_mid_delivered)) {
5309 /* Restore the next to deliver unless we are ahead */
5310 strmin->last_mid_delivered = mid;
5313 /* Left the front Partial one on */
5316 need_reasm_check = 0;
5319 * now we must deliver things in queue the normal way if any are
5322 mid = strmin->last_mid_delivered + 1;
5323 TAILQ_FOREACH_SAFE(control, &strmin->inqueue, next_instrm, ncontrol) {
5324 if (SCTP_MID_EQ(asoc->idata_supported, mid, control->mid)) {
5325 if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
5326 /* this is deliverable now */
5327 if (control->on_strm_q) {
5328 if (control->on_strm_q == SCTP_ON_ORDERED) {
5329 TAILQ_REMOVE(&strmin->inqueue, control, next_instrm);
5330 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5331 TAILQ_REMOVE(&strmin->uno_inqueue, control, next_instrm);
5334 panic("strmin: %p ctl: %p unknown %d",
5335 strmin, control, control->on_strm_q);
5338 control->on_strm_q = 0;
5340 /* subtract pending on streams */
5341 if (asoc->size_on_all_streams >= control->length) {
5342 asoc->size_on_all_streams -= control->length;
5345 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5347 asoc->size_on_all_streams = 0;
5350 sctp_ucount_decr(asoc->cnt_on_all_streams);
5351 /* deliver it to at least the delivery-q */
5352 strmin->last_mid_delivered = control->mid;
5353 if (stcb->sctp_socket) {
5354 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
5355 sctp_add_to_readq(stcb->sctp_ep, stcb,
5357 &stcb->sctp_socket->so_rcv, 1,
5358 SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5360 mid = strmin->last_mid_delivered + 1;
5362 /* Its a fragmented message */
5363 if (control->first_frag_seen) {
5365 * Make it so this is next to
5368 strmin->last_mid_delivered = control->mid - 1;
5369 need_reasm_check = 1;
5377 if (need_reasm_check) {
5378 (void)sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin, SCTP_READ_LOCK_HELD);
5383 sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
5384 struct sctp_association *asoc, struct sctp_stream_in *strm,
5385 struct sctp_queued_to_read *control, int ordered, uint32_t cumtsn)
5387 struct sctp_tmit_chunk *chk, *nchk;
5390 * For now large messages held on the stream reasm that are complete
5391 * will be tossed too. We could in theory do more work to spin
5392 * through and stop after dumping one msg aka seeing the start of a
5393 * new msg at the head, and call the delivery function... to see if
5394 * it can be delivered... But for now we just dump everything on the
5397 if (!asoc->idata_supported && !ordered &&
5398 control->first_frag_seen &&
5399 SCTP_TSN_GT(control->fsn_included, cumtsn)) {
5402 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5403 /* Purge hanging chunks */
5404 if (!asoc->idata_supported && !ordered) {
5405 if (SCTP_TSN_GT(chk->rec.data.tsn, cumtsn)) {
5409 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5410 if (asoc->size_on_reasm_queue >= chk->send_size) {
5411 asoc->size_on_reasm_queue -= chk->send_size;
5414 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
5416 asoc->size_on_reasm_queue = 0;
5419 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5421 sctp_m_freem(chk->data);
5424 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5426 if (!TAILQ_EMPTY(&control->reasm)) {
5427 /* This has to be old data, unordered */
5428 if (control->data) {
5429 sctp_m_freem(control->data);
5430 control->data = NULL;
5432 sctp_reset_a_control(control, stcb->sctp_ep, cumtsn);
5433 chk = TAILQ_FIRST(&control->reasm);
5434 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
5435 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5436 sctp_add_chk_to_control(control, strm, stcb, asoc,
5437 chk, SCTP_READ_LOCK_HELD);
5439 sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_HELD);
5442 if (control->on_strm_q == SCTP_ON_ORDERED) {
5443 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
5444 if (asoc->size_on_all_streams >= control->length) {
5445 asoc->size_on_all_streams -= control->length;
5448 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5450 asoc->size_on_all_streams = 0;
5453 sctp_ucount_decr(asoc->cnt_on_all_streams);
5454 control->on_strm_q = 0;
5455 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5456 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
5457 control->on_strm_q = 0;
5459 } else if (control->on_strm_q) {
5460 panic("strm: %p ctl: %p unknown %d",
5461 strm, control, control->on_strm_q);
5464 control->on_strm_q = 0;
5465 if (control->on_read_q == 0) {
5466 sctp_free_remote_addr(control->whoFrom);
5467 if (control->data) {
5468 sctp_m_freem(control->data);
5469 control->data = NULL;
5471 sctp_free_a_readq(stcb, control);
5476 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5477 struct sctp_forward_tsn_chunk *fwd,
5478 int *abort_flag, struct mbuf *m, int offset)
5480 /* The pr-sctp fwd tsn */
5482 * here we will perform all the data receiver side steps for
5483 * processing FwdTSN, as required in by pr-sctp draft:
5485 * Assume we get FwdTSN(x):
5487 * 1) update local cumTSN to x 2) try to further advance cumTSN to x
5488 * + others we have 3) examine and update re-ordering queue on
5489 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5490 * report where we are.
5492 struct sctp_association *asoc;
5493 uint32_t new_cum_tsn, gap;
5494 unsigned int i, fwd_sz, m_size;
5496 struct sctp_stream_in *strm;
5497 struct sctp_queued_to_read *control, *sv;
5500 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5501 SCTPDBG(SCTP_DEBUG_INDATA1,
5502 "Bad size too small/big fwd-tsn\n");
5505 m_size = (stcb->asoc.mapping_array_size << 3);
5506 /*************************************************************/
5507 /* 1. Here we update local cumTSN and shift the bitmap array */
5508 /*************************************************************/
5509 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5511 if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5512 /* Already got there ... */
5516 * now we know the new TSN is more advanced, let's find the actual
5519 SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
5520 asoc->cumulative_tsn = new_cum_tsn;
5521 if (gap >= m_size) {
5522 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5523 struct mbuf *op_err;
5524 char msg[SCTP_DIAG_INFO_LEN];
5527 * out of range (of single byte chunks in the rwnd I
5528 * give out). This must be an attacker.
5531 SCTP_SNPRINTF(msg, sizeof(msg),
5532 "New cum ack %8.8x too high, highest TSN %8.8x",
5533 new_cum_tsn, asoc->highest_tsn_inside_map);
5534 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5535 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_37;
5536 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5539 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5541 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5542 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5543 asoc->highest_tsn_inside_map = new_cum_tsn;
5545 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5546 asoc->highest_tsn_inside_nr_map = new_cum_tsn;
5548 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5549 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5552 SCTP_TCB_LOCK_ASSERT(stcb);
5553 for (i = 0; i <= gap; i++) {
5554 if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
5555 !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
5556 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
5557 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
5558 asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5563 /*************************************************************/
5564 /* 2. Clear up re-assembly queue */
5565 /*************************************************************/
5567 /* This is now done as part of clearing up the stream/seq */
5568 if (asoc->idata_supported == 0) {
5571 /* Flush all the un-ordered data based on cum-tsn */
5572 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5573 for (sid = 0; sid < asoc->streamincnt; sid++) {
5574 strm = &asoc->strmin[sid];
5575 if (!TAILQ_EMPTY(&strm->uno_inqueue)) {
5576 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, TAILQ_FIRST(&strm->uno_inqueue), 0, new_cum_tsn);
5579 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5581 /*******************************************************/
5582 /* 3. Update the PR-stream re-ordering queues and fix */
5583 /* delivery issues as needed. */
5584 /*******************************************************/
5585 fwd_sz -= sizeof(*fwd);
5588 unsigned int num_str;
5591 uint16_t ordered, flags;
5592 struct sctp_strseq *stseq, strseqbuf;
5593 struct sctp_strseq_mid *stseq_m, strseqbuf_m;
5595 offset += sizeof(*fwd);
5597 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5598 if (asoc->idata_supported) {
5599 num_str = fwd_sz / sizeof(struct sctp_strseq_mid);
5601 num_str = fwd_sz / sizeof(struct sctp_strseq);
5603 for (i = 0; i < num_str; i++) {
5604 if (asoc->idata_supported) {
5605 stseq_m = (struct sctp_strseq_mid *)sctp_m_getptr(m, offset,
5606 sizeof(struct sctp_strseq_mid),
5607 (uint8_t *)&strseqbuf_m);
5608 offset += sizeof(struct sctp_strseq_mid);
5609 if (stseq_m == NULL) {
5612 sid = ntohs(stseq_m->sid);
5613 mid = ntohl(stseq_m->mid);
5614 flags = ntohs(stseq_m->flags);
5615 if (flags & PR_SCTP_UNORDERED_FLAG) {
5621 stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5622 sizeof(struct sctp_strseq),
5623 (uint8_t *)&strseqbuf);
5624 offset += sizeof(struct sctp_strseq);
5625 if (stseq == NULL) {
5628 sid = ntohs(stseq->sid);
5629 mid = (uint32_t)ntohs(stseq->ssn);
5637 * Ok we now look for the stream/seq on the read
5638 * queue where its not all delivered. If we find it
5639 * we transmute the read entry into a PDI_ABORTED.
5641 if (sid >= asoc->streamincnt) {
5642 /* screwed up streams, stop! */
5645 if ((asoc->str_of_pdapi == sid) &&
5646 (asoc->ssn_of_pdapi == mid)) {
5648 * If this is the one we were partially
5649 * delivering now then we no longer are.
5650 * Note this will change with the reassembly
5653 asoc->fragmented_delivery_inprogress = 0;
5655 strm = &asoc->strmin[sid];
5657 TAILQ_FOREACH(control, &strm->inqueue, next_instrm) {
5658 if (SCTP_MID_GE(asoc->idata_supported, mid, control->mid)) {
5659 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, control, ordered, new_cum_tsn);
5663 if (asoc->idata_supported) {
5664 TAILQ_FOREACH(control, &strm->uno_inqueue, next_instrm) {
5665 if (SCTP_MID_GE(asoc->idata_supported, mid, control->mid)) {
5666 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, control, ordered, new_cum_tsn);
5670 if (!TAILQ_EMPTY(&strm->uno_inqueue)) {
5671 sctp_flush_reassm_for_str_seq(stcb, asoc, strm, TAILQ_FIRST(&strm->uno_inqueue), ordered, new_cum_tsn);
5675 TAILQ_FOREACH(control, &stcb->sctp_ep->read_queue, next) {
5676 if ((control->sinfo_stream == sid) &&
5677 (SCTP_MID_EQ(asoc->idata_supported, control->mid, mid))) {
5678 str_seq = (sid << 16) | (0x0000ffff & mid);
5679 control->pdapi_aborted = 1;
5680 sv = stcb->asoc.control_pdapi;
5681 control->end_added = 1;
5682 if (control->on_strm_q == SCTP_ON_ORDERED) {
5683 TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
5684 if (asoc->size_on_all_streams >= control->length) {
5685 asoc->size_on_all_streams -= control->length;
5688 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
5690 asoc->size_on_all_streams = 0;
5693 sctp_ucount_decr(asoc->cnt_on_all_streams);
5694 } else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5695 TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
5697 } else if (control->on_strm_q) {
5698 panic("strm: %p ctl: %p unknown %d",
5699 strm, control, control->on_strm_q);
5702 control->on_strm_q = 0;
5703 stcb->asoc.control_pdapi = control;
5704 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5706 SCTP_PARTIAL_DELIVERY_ABORTED,
5708 SCTP_SO_NOT_LOCKED);
5709 stcb->asoc.control_pdapi = sv;
5711 } else if ((control->sinfo_stream == sid) &&
5712 SCTP_MID_GT(asoc->idata_supported, control->mid, mid)) {
5713 /* We are past our victim SSN */
5717 if (SCTP_MID_GT(asoc->idata_supported, mid, strm->last_mid_delivered)) {
5718 /* Update the sequence number */
5719 strm->last_mid_delivered = mid;
5721 /* now kick the stream the new way */
5722 /* sa_ignore NO_NULL_CHK */
5723 sctp_kick_prsctp_reorder_queue(stcb, strm);
5725 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5728 * Now slide thing forward.
5730 sctp_slide_mapping_arrays(stcb);