2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_indata.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_timer.h>
50 * NOTES: On the outbound side of things I need to check the sack timer to
51 * see if I should generate a sack into the chunk queue (if I have data to
52 * send that is and will be sending it .. for bundling.
54 * The callback in sctp_usrreq.c will get called when the socket is read from.
55 * This will cause sctp_service_queues() to get called on the top entry in
60 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
62 asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
65 /* Calculate what the rwnd would be */
67 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
72 * This is really set wrong with respect to a 1-2-m socket. Since
73 * the sb_cc is the count that everyone as put up. When we re-write
74 * sctp_soreceive then we will fix this so that ONLY this
75 * associations data is taken into account.
77 if (stcb->sctp_socket == NULL)
80 if (stcb->asoc.sb_cc == 0 &&
81 asoc->size_on_reasm_queue == 0 &&
82 asoc->size_on_all_streams == 0) {
83 /* Full rwnd granted */
84 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
87 /* get actual space */
88 calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
91 * take out what has NOT been put on socket queue and we yet hold
94 calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
95 asoc->cnt_on_reasm_queue * MSIZE));
96 calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
97 asoc->cnt_on_all_streams * MSIZE));
103 /* what is the overhead of all these rwnd's */
104 calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
106 * If the window gets too small due to ctrl-stuff, reduce it to 1,
107 * even it is 0. SWS engaged
109 if (calc < stcb->asoc.my_rwnd_control_len) {
118 * Build out our readq entry based on the incoming packet.
120 struct sctp_queued_to_read *
121 sctp_build_readq_entry(struct sctp_tcb *stcb,
122 struct sctp_nets *net,
123 uint32_t tsn, uint32_t ppid,
124 uint32_t context, uint16_t stream_no,
125 uint16_t stream_seq, uint8_t flags,
128 struct sctp_queued_to_read *read_queue_e = NULL;
130 sctp_alloc_a_readq(stcb, read_queue_e);
131 if (read_queue_e == NULL) {
134 read_queue_e->sinfo_stream = stream_no;
135 read_queue_e->sinfo_ssn = stream_seq;
136 read_queue_e->sinfo_flags = (flags << 8);
137 read_queue_e->sinfo_ppid = ppid;
138 read_queue_e->sinfo_context = context;
139 read_queue_e->sinfo_timetolive = 0;
140 read_queue_e->sinfo_tsn = tsn;
141 read_queue_e->sinfo_cumtsn = tsn;
142 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
143 read_queue_e->whoFrom = net;
144 read_queue_e->length = 0;
145 atomic_add_int(&net->ref_count, 1);
146 read_queue_e->data = dm;
147 read_queue_e->spec_flags = 0;
148 read_queue_e->tail_mbuf = NULL;
149 read_queue_e->aux_data = NULL;
150 read_queue_e->stcb = stcb;
151 read_queue_e->port_from = stcb->rport;
152 read_queue_e->do_not_ref_stcb = 0;
153 read_queue_e->end_added = 0;
154 read_queue_e->some_taken = 0;
155 read_queue_e->pdapi_aborted = 0;
157 return (read_queue_e);
162 * Build out our readq entry based on the incoming packet.
164 static struct sctp_queued_to_read *
165 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
166 struct sctp_tmit_chunk *chk)
168 struct sctp_queued_to_read *read_queue_e = NULL;
170 sctp_alloc_a_readq(stcb, read_queue_e);
171 if (read_queue_e == NULL) {
174 read_queue_e->sinfo_stream = chk->rec.data.stream_number;
175 read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
176 read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
177 read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
178 read_queue_e->sinfo_context = stcb->asoc.context;
179 read_queue_e->sinfo_timetolive = 0;
180 read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
181 read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
182 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
183 read_queue_e->whoFrom = chk->whoTo;
184 read_queue_e->aux_data = NULL;
185 read_queue_e->length = 0;
186 atomic_add_int(&chk->whoTo->ref_count, 1);
187 read_queue_e->data = chk->data;
188 read_queue_e->tail_mbuf = NULL;
189 read_queue_e->stcb = stcb;
190 read_queue_e->port_from = stcb->rport;
191 read_queue_e->spec_flags = 0;
192 read_queue_e->do_not_ref_stcb = 0;
193 read_queue_e->end_added = 0;
194 read_queue_e->some_taken = 0;
195 read_queue_e->pdapi_aborted = 0;
197 return (read_queue_e);
202 sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
204 struct sctp_extrcvinfo *seinfo;
205 struct sctp_sndrcvinfo *outinfo;
206 struct sctp_rcvinfo *rcvinfo;
207 struct sctp_nxtinfo *nxtinfo;
214 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
215 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
216 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
217 /* user does not want any ancillary data */
221 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
222 len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
224 seinfo = (struct sctp_extrcvinfo *)sinfo;
225 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
226 (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
228 len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
232 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
233 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
235 len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
238 len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
244 ret = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
249 SCTP_BUF_LEN(ret) = 0;
251 /* We need a CMSG header followed by the struct */
252 cmh = mtod(ret, struct cmsghdr *);
254 * Make sure that there is no un-initialized padding between the
255 * cmsg header and cmsg data and after the cmsg data.
258 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
259 cmh->cmsg_level = IPPROTO_SCTP;
260 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
261 cmh->cmsg_type = SCTP_RCVINFO;
262 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
263 rcvinfo->rcv_sid = sinfo->sinfo_stream;
264 rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
265 rcvinfo->rcv_flags = sinfo->sinfo_flags;
266 rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
267 rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
268 rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
269 rcvinfo->rcv_context = sinfo->sinfo_context;
270 rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
271 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
272 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
275 cmh->cmsg_level = IPPROTO_SCTP;
276 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
277 cmh->cmsg_type = SCTP_NXTINFO;
278 nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
279 nxtinfo->nxt_sid = seinfo->sreinfo_next_stream;
280 nxtinfo->nxt_flags = 0;
281 if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
282 nxtinfo->nxt_flags |= SCTP_UNORDERED;
284 if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
285 nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
287 if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
288 nxtinfo->nxt_flags |= SCTP_COMPLETE;
290 nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid;
291 nxtinfo->nxt_length = seinfo->sreinfo_next_length;
292 nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid;
293 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
294 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
296 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
297 cmh->cmsg_level = IPPROTO_SCTP;
298 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
300 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
301 cmh->cmsg_type = SCTP_EXTRCV;
302 memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
303 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
305 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
306 cmh->cmsg_type = SCTP_SNDRCV;
308 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
316 sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
318 uint32_t gap, i, cumackp1;
321 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
324 cumackp1 = asoc->cumulative_tsn + 1;
325 if (SCTP_TSN_GT(cumackp1, tsn)) {
327 * this tsn is behind the cum ack and thus we don't need to
328 * worry about it being moved from one to the other.
332 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
333 if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
334 SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
335 sctp_print_mapping_array(asoc);
337 panic("Things are really messed up now!!");
340 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
341 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
342 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
343 asoc->highest_tsn_inside_nr_map = tsn;
345 if (tsn == asoc->highest_tsn_inside_map) {
346 /* We must back down to see what the new highest is */
347 for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
348 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
349 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
350 asoc->highest_tsn_inside_map = i;
356 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
363 * We are delivering currently from the reassembly queue. We must continue to
364 * deliver until we either: 1) run out of space. 2) run out of sequential
365 * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
368 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
370 struct sctp_tmit_chunk *chk, *nchk;
375 struct sctp_queued_to_read *control, *ctl, *nctl;
380 cntDel = stream_no = 0;
381 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
382 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
383 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
384 /* socket above is long gone or going.. */
386 asoc->fragmented_delivery_inprogress = 0;
387 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
388 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
389 asoc->size_on_reasm_queue -= chk->send_size;
390 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
392 * Lose the data pointer, since its in the socket
396 sctp_m_freem(chk->data);
399 /* Now free the address and data */
400 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
401 /* sa_ignore FREED_MEMORY */
405 SCTP_TCB_LOCK_ASSERT(stcb);
406 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
407 if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
408 /* Can't deliver more :< */
411 stream_no = chk->rec.data.stream_number;
412 nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
413 if (nxt_todel != chk->rec.data.stream_seq &&
414 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
416 * Not the next sequence to deliver in its stream OR
421 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
423 control = sctp_build_readq_entry_chk(stcb, chk);
424 if (control == NULL) {
428 /* save it off for our future deliveries */
429 stcb->asoc.control_pdapi = control;
430 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
434 sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
435 sctp_add_to_readq(stcb->sctp_ep,
436 stcb, control, &stcb->sctp_socket->so_rcv, end,
437 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
440 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
444 sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
445 if (sctp_append_to_readq(stcb->sctp_ep, stcb,
446 stcb->asoc.control_pdapi,
447 chk->data, end, chk->rec.data.TSN_seq,
448 &stcb->sctp_socket->so_rcv)) {
450 * something is very wrong, either
451 * control_pdapi is NULL, or the tail_mbuf
452 * is corrupt, or there is a EOM already on
455 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
459 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
460 panic("This should not happen control_pdapi NULL?");
462 /* if we did not panic, it was a EOM */
463 panic("Bad chunking ??");
465 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
466 SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
468 SCTP_PRINTF("Bad chunking ??\n");
469 SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
477 /* pull it we did it */
478 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
479 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
480 asoc->fragmented_delivery_inprogress = 0;
481 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
482 asoc->strmin[stream_no].last_sequence_delivered++;
484 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
485 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
487 } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
489 * turn the flag back on since we just delivered
492 asoc->fragmented_delivery_inprogress = 1;
494 asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
495 asoc->last_flags_delivered = chk->rec.data.rcv_flags;
496 asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
497 asoc->last_strm_no_delivered = chk->rec.data.stream_number;
499 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
500 asoc->size_on_reasm_queue -= chk->send_size;
501 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
502 /* free up the chk */
504 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
506 if (asoc->fragmented_delivery_inprogress == 0) {
508 * Now lets see if we can deliver the next one on
511 struct sctp_stream_in *strm;
513 strm = &asoc->strmin[stream_no];
514 nxt_todel = strm->last_sequence_delivered + 1;
515 TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
516 /* Deliver more if we can. */
517 if (nxt_todel == ctl->sinfo_ssn) {
518 TAILQ_REMOVE(&strm->inqueue, ctl, next);
519 asoc->size_on_all_streams -= ctl->length;
520 sctp_ucount_decr(asoc->cnt_on_all_streams);
521 strm->last_sequence_delivered++;
522 sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
523 sctp_add_to_readq(stcb->sctp_ep, stcb,
525 &stcb->sctp_socket->so_rcv, 1,
526 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
530 nxt_todel = strm->last_sequence_delivered + 1;
538 * Queue the chunk either right into the socket buffer if it is the next one
539 * to go OR put it in the correct place in the delivery queue. If we do
540 * append to the so_buf, keep doing so until we are out of order. One big
541 * question still remains, what to do when the socket buffer is FULL??
544 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
545 struct sctp_queued_to_read *control, int *abort_flag)
548 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
549 * all the data in one stream this could happen quite rapidly. One
550 * could use the TSN to keep track of things, but this scheme breaks
551 * down in the other type of stream useage that could occur. Send a
552 * single msg to stream 0, send 4Billion messages to stream 1, now
553 * send a message to stream 0. You have a situation where the TSN
554 * has wrapped but not in the stream. Is this worth worrying about
555 * or should we just change our queue sort at the bottom to be by
558 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
559 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
560 * assignment this could happen... and I don't see how this would be
561 * a violation. So for now I am undecided an will leave the sort by
562 * SSN alone. Maybe a hybred approach is the answer
565 struct sctp_stream_in *strm;
566 struct sctp_queued_to_read *at;
570 char msg[SCTP_DIAG_INFO_LEN];
573 asoc->size_on_all_streams += control->length;
574 sctp_ucount_incr(asoc->cnt_on_all_streams);
575 strm = &asoc->strmin[control->sinfo_stream];
576 nxt_todel = strm->last_sequence_delivered + 1;
577 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
578 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
580 SCTPDBG(SCTP_DEBUG_INDATA1,
581 "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
582 (uint32_t) control->sinfo_stream,
583 (uint32_t) strm->last_sequence_delivered,
584 (uint32_t) nxt_todel);
585 if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
586 /* The incoming sseq is behind where we last delivered? */
587 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
588 control->sinfo_ssn, strm->last_sequence_delivered);
591 * throw it in the stream so it gets cleaned up in
592 * association destruction
594 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
595 snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
596 strm->last_sequence_delivered, control->sinfo_tsn,
597 control->sinfo_stream, control->sinfo_ssn);
598 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
599 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
600 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
605 if (nxt_todel == control->sinfo_ssn) {
606 /* can be delivered right away? */
607 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
608 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
610 /* EY it wont be queued if it could be delivered directly */
612 asoc->size_on_all_streams -= control->length;
613 sctp_ucount_decr(asoc->cnt_on_all_streams);
614 strm->last_sequence_delivered++;
616 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
617 sctp_add_to_readq(stcb->sctp_ep, stcb,
619 &stcb->sctp_socket->so_rcv, 1,
620 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
621 TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
623 nxt_todel = strm->last_sequence_delivered + 1;
624 if (nxt_todel == control->sinfo_ssn) {
625 TAILQ_REMOVE(&strm->inqueue, control, next);
626 asoc->size_on_all_streams -= control->length;
627 sctp_ucount_decr(asoc->cnt_on_all_streams);
628 strm->last_sequence_delivered++;
630 * We ignore the return of deliver_data here
631 * since we always can hold the chunk on the
632 * d-queue. And we have a finite number that
633 * can be delivered from the strq.
635 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
636 sctp_log_strm_del(control, NULL,
637 SCTP_STR_LOG_FROM_IMMED_DEL);
639 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
640 sctp_add_to_readq(stcb->sctp_ep, stcb,
642 &stcb->sctp_socket->so_rcv, 1,
643 SCTP_READ_LOCK_NOT_HELD,
652 * Ok, we did not deliver this guy, find the correct place
653 * to put it on the queue.
655 if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
658 if (TAILQ_EMPTY(&strm->inqueue)) {
660 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
661 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
663 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
665 TAILQ_FOREACH(at, &strm->inqueue, next) {
666 if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
668 * one in queue is bigger than the
669 * new one, insert before this one
671 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
672 sctp_log_strm_del(control, at,
673 SCTP_STR_LOG_FROM_INSERT_MD);
675 TAILQ_INSERT_BEFORE(at, control, next);
677 } else if (at->sinfo_ssn == control->sinfo_ssn) {
679 * Gak, He sent me a duplicate str
683 * foo bar, I guess I will just free
684 * this new guy, should we abort
685 * too? FIX ME MAYBE? Or it COULD be
686 * that the SSN's have wrapped.
687 * Maybe I should compare to TSN
688 * somehow... sigh for now just blow
693 sctp_m_freem(control->data);
694 control->data = NULL;
695 asoc->size_on_all_streams -= control->length;
696 sctp_ucount_decr(asoc->cnt_on_all_streams);
697 if (control->whoFrom) {
698 sctp_free_remote_addr(control->whoFrom);
699 control->whoFrom = NULL;
701 sctp_free_a_readq(stcb, control);
704 if (TAILQ_NEXT(at, next) == NULL) {
706 * We are at the end, insert
709 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
710 sctp_log_strm_del(control, at,
711 SCTP_STR_LOG_FROM_INSERT_TL);
713 TAILQ_INSERT_AFTER(&strm->inqueue,
724 * Returns two things: You get the total size of the deliverable parts of the
725 * first fragmented message on the reassembly queue. And you get a 1 back if
726 * all of the message is ready or a 0 back if the message is still incomplete
729 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
731 struct sctp_tmit_chunk *chk;
735 chk = TAILQ_FIRST(&asoc->reasmqueue);
737 /* nothing on the queue */
740 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
741 /* Not a first on the queue */
744 tsn = chk->rec.data.TSN_seq;
745 TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
746 if (tsn != chk->rec.data.TSN_seq) {
749 *t_size += chk->send_size;
750 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
759 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
761 struct sctp_tmit_chunk *chk;
763 uint32_t tsize, pd_point;
766 chk = TAILQ_FIRST(&asoc->reasmqueue);
769 asoc->size_on_reasm_queue = 0;
770 asoc->cnt_on_reasm_queue = 0;
773 if (asoc->fragmented_delivery_inprogress == 0) {
775 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
776 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
777 (nxt_todel == chk->rec.data.stream_seq ||
778 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
780 * Yep the first one is here and its ok to deliver
783 if (stcb->sctp_socket) {
784 pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
785 stcb->sctp_ep->partial_delivery_point);
787 pd_point = stcb->sctp_ep->partial_delivery_point;
789 if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
791 * Yes, we setup to start reception, by
792 * backing down the TSN just in case we
793 * can't deliver. If we
795 asoc->fragmented_delivery_inprogress = 1;
796 asoc->tsn_last_delivered =
797 chk->rec.data.TSN_seq - 1;
799 chk->rec.data.stream_number;
800 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
801 asoc->pdapi_ppid = chk->rec.data.payloadtype;
802 asoc->fragment_flags = chk->rec.data.rcv_flags;
803 sctp_service_reassembly(stcb, asoc);
808 * Service re-assembly will deliver stream data queued at
809 * the end of fragmented delivery.. but it wont know to go
810 * back and call itself again... we do that here with the
813 sctp_service_reassembly(stcb, asoc);
814 if (asoc->fragmented_delivery_inprogress == 0) {
816 * finished our Fragmented delivery, could be more
825 * Dump onto the re-assembly queue, in its proper place. After dumping on the
826 * queue, see if anthing can be delivered. If so pull it off (or as much as
827 * we can. If we run out of space then we must dump what we can and set the
828 * appropriate flag to say we queued what we could.
831 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
832 struct sctp_tmit_chunk *chk, int *abort_flag)
835 char msg[SCTP_DIAG_INFO_LEN];
836 uint32_t cum_ackp1, prev_tsn, post_tsn;
837 struct sctp_tmit_chunk *at, *prev, *next;
840 cum_ackp1 = asoc->tsn_last_delivered + 1;
841 if (TAILQ_EMPTY(&asoc->reasmqueue)) {
842 /* This is the first one on the queue */
843 TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
845 * we do not check for delivery of anything when only one
848 asoc->size_on_reasm_queue = chk->send_size;
849 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
850 if (chk->rec.data.TSN_seq == cum_ackp1) {
851 if (asoc->fragmented_delivery_inprogress == 0 &&
852 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
853 SCTP_DATA_FIRST_FRAG) {
855 * An empty queue, no delivery inprogress,
856 * we hit the next one and it does NOT have
857 * a FIRST fragment mark.
859 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
860 snprintf(msg, sizeof(msg),
861 "Expected B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
862 chk->rec.data.TSN_seq,
863 chk->rec.data.stream_number,
864 chk->rec.data.stream_seq);
865 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
866 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
867 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
869 } else if (asoc->fragmented_delivery_inprogress &&
870 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
872 * We are doing a partial delivery and the
873 * NEXT chunk MUST be either the LAST or
874 * MIDDLE fragment NOT a FIRST
876 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
877 snprintf(msg, sizeof(msg),
878 "Didn't expect B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
879 chk->rec.data.TSN_seq,
880 chk->rec.data.stream_number,
881 chk->rec.data.stream_seq);
882 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
883 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
884 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
886 } else if (asoc->fragmented_delivery_inprogress) {
888 * Here we are ok with a MIDDLE or LAST
891 if (chk->rec.data.stream_number !=
892 asoc->str_of_pdapi) {
893 /* Got to be the right STR No */
894 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
895 chk->rec.data.stream_number,
897 snprintf(msg, sizeof(msg),
898 "Expected SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
900 chk->rec.data.TSN_seq,
901 chk->rec.data.stream_number,
902 chk->rec.data.stream_seq);
903 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
904 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
905 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
907 } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
908 SCTP_DATA_UNORDERED &&
909 chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
910 /* Got to be the right STR Seq */
911 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
912 chk->rec.data.stream_seq,
914 snprintf(msg, sizeof(msg),
915 "Expected SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
917 chk->rec.data.TSN_seq,
918 chk->rec.data.stream_number,
919 chk->rec.data.stream_seq);
920 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
921 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
922 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
930 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
931 if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
933 * one in queue is bigger than the new one, insert
937 asoc->size_on_reasm_queue += chk->send_size;
938 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
940 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
942 } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
943 /* Gak, He sent me a duplicate str seq number */
945 * foo bar, I guess I will just free this new guy,
946 * should we abort too? FIX ME MAYBE? Or it COULD be
947 * that the SSN's have wrapped. Maybe I should
948 * compare to TSN somehow... sigh for now just blow
952 sctp_m_freem(chk->data);
955 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
959 if (TAILQ_NEXT(at, sctp_next) == NULL) {
961 * We are at the end, insert it after this
965 asoc->size_on_reasm_queue += chk->send_size;
966 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
967 TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
974 prev_tsn = chk->rec.data.TSN_seq - 1;
975 if (prev_tsn == prev->rec.data.TSN_seq) {
977 * Ok the one I am dropping onto the end is the
978 * NEXT. A bit of valdiation here.
980 if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
981 SCTP_DATA_FIRST_FRAG ||
982 (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
983 SCTP_DATA_MIDDLE_FRAG) {
985 * Insert chk MUST be a MIDDLE or LAST
988 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
989 SCTP_DATA_FIRST_FRAG) {
990 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
991 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
992 snprintf(msg, sizeof(msg),
993 "Can't handle B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
994 chk->rec.data.TSN_seq,
995 chk->rec.data.stream_number,
996 chk->rec.data.stream_seq);
997 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
998 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
999 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1003 if (chk->rec.data.stream_number !=
1004 prev->rec.data.stream_number) {
1006 * Huh, need the correct STR here,
1007 * they must be the same.
1009 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sid:%d not the same as at:%d\n",
1010 chk->rec.data.stream_number,
1011 prev->rec.data.stream_number);
1012 snprintf(msg, sizeof(msg),
1013 "Expect SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1014 prev->rec.data.stream_number,
1015 chk->rec.data.TSN_seq,
1016 chk->rec.data.stream_number,
1017 chk->rec.data.stream_seq);
1018 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1019 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1020 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1024 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
1025 (prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
1027 * Huh, need the same ordering here,
1028 * they must be the same.
1030 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, U-bit not constant\n");
1031 snprintf(msg, sizeof(msg),
1032 "Expect U-bit=%d for TSN=%8.8x, got U-bit=%d",
1033 (prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0,
1034 chk->rec.data.TSN_seq,
1035 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0);
1036 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1037 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1038 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1042 if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1043 chk->rec.data.stream_seq !=
1044 prev->rec.data.stream_seq) {
1046 * Huh, need the correct STR here,
1047 * they must be the same.
1049 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1050 chk->rec.data.stream_seq,
1051 prev->rec.data.stream_seq);
1052 snprintf(msg, sizeof(msg),
1053 "Expect SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1054 prev->rec.data.stream_seq,
1055 chk->rec.data.TSN_seq,
1056 chk->rec.data.stream_number,
1057 chk->rec.data.stream_seq);
1058 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1059 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1060 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1064 } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1065 SCTP_DATA_LAST_FRAG) {
1066 /* Insert chk MUST be a FIRST */
1067 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1068 SCTP_DATA_FIRST_FRAG) {
1069 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1070 snprintf(msg, sizeof(msg),
1071 "Expect B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1072 chk->rec.data.TSN_seq,
1073 chk->rec.data.stream_number,
1074 chk->rec.data.stream_seq);
1075 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1076 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1077 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1085 post_tsn = chk->rec.data.TSN_seq + 1;
1086 if (post_tsn == next->rec.data.TSN_seq) {
1088 * Ok the one I am inserting ahead of is my NEXT
1089 * one. A bit of valdiation here.
1091 if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1092 /* Insert chk MUST be a last fragment */
1093 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1094 != SCTP_DATA_LAST_FRAG) {
1095 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1096 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1097 snprintf(msg, sizeof(msg),
1098 "Expect only E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1099 chk->rec.data.TSN_seq,
1100 chk->rec.data.stream_number,
1101 chk->rec.data.stream_seq);
1102 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1103 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1104 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1108 } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1109 SCTP_DATA_MIDDLE_FRAG ||
1110 (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1111 SCTP_DATA_LAST_FRAG) {
1113 * Insert chk CAN be MIDDLE or FIRST NOT
1116 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1117 SCTP_DATA_LAST_FRAG) {
1118 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1119 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1120 snprintf(msg, sizeof(msg),
1121 "Didn't expect E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1122 chk->rec.data.TSN_seq,
1123 chk->rec.data.stream_number,
1124 chk->rec.data.stream_seq);
1125 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1126 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1127 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1131 if (chk->rec.data.stream_number !=
1132 next->rec.data.stream_number) {
1134 * Huh, need the correct STR here,
1135 * they must be the same.
1137 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1138 chk->rec.data.stream_number,
1139 next->rec.data.stream_number);
1140 snprintf(msg, sizeof(msg),
1141 "Required SID %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1142 next->rec.data.stream_number,
1143 chk->rec.data.TSN_seq,
1144 chk->rec.data.stream_number,
1145 chk->rec.data.stream_seq);
1146 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1147 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1148 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1152 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
1153 (next->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
1155 * Huh, need the same ordering here,
1156 * they must be the same.
1158 SCTPDBG(SCTP_DEBUG_INDATA1, "Next check - Gak, Evil plot, U-bit not constant\n");
1159 snprintf(msg, sizeof(msg),
1160 "Expect U-bit=%d for TSN=%8.8x, got U-bit=%d",
1161 (next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0,
1162 chk->rec.data.TSN_seq,
1163 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0);
1164 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1165 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1166 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1170 if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1171 chk->rec.data.stream_seq !=
1172 next->rec.data.stream_seq) {
1174 * Huh, need the correct STR here,
1175 * they must be the same.
1177 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1178 chk->rec.data.stream_seq,
1179 next->rec.data.stream_seq);
1180 snprintf(msg, sizeof(msg),
1181 "Required SSN %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1182 next->rec.data.stream_seq,
1183 chk->rec.data.TSN_seq,
1184 chk->rec.data.stream_number,
1185 chk->rec.data.stream_seq);
1186 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1187 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1188 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1195 /* Do we need to do some delivery? check */
1196 sctp_deliver_reasm_check(stcb, asoc);
1200 * This is an unfortunate routine. It checks to make sure a evil guy is not
1201 * stuffing us full of bad packet fragments. A broken peer could also do this
1202 * but this is doubtful. It is to bad I must worry about evil crackers sigh
1206 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1209 struct sctp_tmit_chunk *at;
1212 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1213 if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1214 /* is it one bigger? */
1215 tsn_est = at->rec.data.TSN_seq + 1;
1216 if (tsn_est == TSN_seq) {
1217 /* yep. It better be a last then */
1218 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1219 SCTP_DATA_LAST_FRAG) {
1221 * Ok this guy belongs next to a guy
1222 * that is NOT last, it should be a
1223 * middle/last, not a complete
1229 * This guy is ok since its a LAST
1230 * and the new chunk is a fully
1231 * self- contained one.
1236 } else if (TSN_seq == at->rec.data.TSN_seq) {
1237 /* Software error since I have a dup? */
1241 * Ok, 'at' is larger than new chunk but does it
1242 * need to be right before it.
1244 tsn_est = TSN_seq + 1;
1245 if (tsn_est == at->rec.data.TSN_seq) {
1246 /* Yep, It better be a first */
1247 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1248 SCTP_DATA_FIRST_FRAG) {
1260 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1261 struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1262 struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1263 int *break_flag, int last_chunk)
1265 /* Process a data chunk */
1266 /* struct sctp_tmit_chunk *chk; */
1267 struct sctp_tmit_chunk *chk;
1271 int need_reasm_check = 0;
1272 uint16_t strmno, strmseq;
1273 struct mbuf *op_err;
1274 char msg[SCTP_DIAG_INFO_LEN];
1275 struct sctp_queued_to_read *control;
1277 uint32_t protocol_id;
1278 uint8_t chunk_flags;
1279 struct sctp_stream_reset_list *liste;
1282 tsn = ntohl(ch->dp.tsn);
1283 chunk_flags = ch->ch.chunk_flags;
1284 if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1285 asoc->send_sack = 1;
1287 protocol_id = ch->dp.protocol_id;
1288 ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1289 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1290 sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1295 SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1296 if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1297 /* It is a duplicate */
1298 SCTP_STAT_INCR(sctps_recvdupdata);
1299 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1300 /* Record a dup for the next outbound sack */
1301 asoc->dup_tsns[asoc->numduptsns] = tsn;
1304 asoc->send_sack = 1;
1307 /* Calculate the number of TSN's between the base and this TSN */
1308 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1309 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1310 /* Can't hold the bit in the mapping at max array, toss it */
1313 if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1314 SCTP_TCB_LOCK_ASSERT(stcb);
1315 if (sctp_expand_mapping_array(asoc, gap)) {
1316 /* Can't expand, drop it */
1320 if (SCTP_TSN_GT(tsn, *high_tsn)) {
1323 /* See if we have received this one already */
1324 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
1325 SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1326 SCTP_STAT_INCR(sctps_recvdupdata);
1327 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1328 /* Record a dup for the next outbound sack */
1329 asoc->dup_tsns[asoc->numduptsns] = tsn;
1332 asoc->send_sack = 1;
1336 * Check to see about the GONE flag, duplicates would cause a sack
1337 * to be sent up above
1339 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1340 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1341 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1343 * wait a minute, this guy is gone, there is no longer a
1344 * receiver. Send peer an ABORT!
1346 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1347 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1352 * Now before going further we see if there is room. If NOT then we
1353 * MAY let one through only IF this TSN is the one we are waiting
1354 * for on a partial delivery API.
1357 /* now do the tests */
1358 if (((asoc->cnt_on_all_streams +
1359 asoc->cnt_on_reasm_queue +
1360 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1361 (((int)asoc->my_rwnd) <= 0)) {
1363 * When we have NO room in the rwnd we check to make sure
1364 * the reader is doing its job...
1366 if (stcb->sctp_socket->so_rcv.sb_cc) {
1367 /* some to read, wake-up */
1368 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1371 so = SCTP_INP_SO(stcb->sctp_ep);
1372 atomic_add_int(&stcb->asoc.refcnt, 1);
1373 SCTP_TCB_UNLOCK(stcb);
1374 SCTP_SOCKET_LOCK(so, 1);
1375 SCTP_TCB_LOCK(stcb);
1376 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1377 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1378 /* assoc was freed while we were unlocked */
1379 SCTP_SOCKET_UNLOCK(so, 1);
1383 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1384 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1385 SCTP_SOCKET_UNLOCK(so, 1);
1388 /* now is it in the mapping array of what we have accepted? */
1389 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
1390 SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1391 /* Nope not in the valid range dump it */
1392 sctp_set_rwnd(stcb, asoc);
1393 if ((asoc->cnt_on_all_streams +
1394 asoc->cnt_on_reasm_queue +
1395 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1396 SCTP_STAT_INCR(sctps_datadropchklmt);
1398 SCTP_STAT_INCR(sctps_datadroprwnd);
1404 strmno = ntohs(ch->dp.stream_id);
1405 if (strmno >= asoc->streamincnt) {
1406 struct sctp_paramhdr *phdr;
1409 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1410 0, M_NOWAIT, 1, MT_DATA);
1412 /* add some space up front so prepend will work well */
1413 SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1414 phdr = mtod(mb, struct sctp_paramhdr *);
1416 * Error causes are just param's and this one has
1417 * two back to back phdr, one with the error type
1418 * and size, the other with the streamid and a rsvd
1420 SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1421 phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1422 phdr->param_length =
1423 htons(sizeof(struct sctp_paramhdr) * 2);
1425 /* We insert the stream in the type field */
1426 phdr->param_type = ch->dp.stream_id;
1427 /* And set the length to 0 for the rsvd field */
1428 phdr->param_length = 0;
1429 sctp_queue_op_err(stcb, mb);
1431 SCTP_STAT_INCR(sctps_badsid);
1432 SCTP_TCB_LOCK_ASSERT(stcb);
1433 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1434 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1435 asoc->highest_tsn_inside_nr_map = tsn;
1437 if (tsn == (asoc->cumulative_tsn + 1)) {
1438 /* Update cum-ack */
1439 asoc->cumulative_tsn = tsn;
1444 * Before we continue lets validate that we are not being fooled by
1445 * an evil attacker. We can only have 4k chunks based on our TSN
1446 * spread allowed by the mapping array 512 * 8 bits, so there is no
1447 * way our stream sequence numbers could have wrapped. We of course
1448 * only validate the FIRST fragment so the bit must be set.
1450 strmseq = ntohs(ch->dp.stream_sequence);
1451 #ifdef SCTP_ASOCLOG_OF_TSNS
1452 SCTP_TCB_LOCK_ASSERT(stcb);
1453 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1454 asoc->tsn_in_at = 0;
1455 asoc->tsn_in_wrapped = 1;
1457 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1458 asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1459 asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1460 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1461 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1462 asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1463 asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1464 asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1467 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1468 (TAILQ_EMPTY(&asoc->resetHead)) &&
1469 (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1470 SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1471 /* The incoming sseq is behind where we last delivered? */
1472 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1473 strmseq, asoc->strmin[strmno].last_sequence_delivered);
1475 snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1476 asoc->strmin[strmno].last_sequence_delivered,
1477 tsn, strmno, strmseq);
1478 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1479 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1480 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1484 /************************************
1485 * From here down we may find ch-> invalid
1486 * so its a good idea NOT to use it.
1487 *************************************/
1489 the_len = (chk_length - sizeof(struct sctp_data_chunk));
1490 if (last_chunk == 0) {
1491 dmbuf = SCTP_M_COPYM(*m,
1492 (offset + sizeof(struct sctp_data_chunk)),
1494 #ifdef SCTP_MBUF_LOGGING
1495 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1496 sctp_log_mbc(dmbuf, SCTP_MBUF_ICOPY);
1500 /* We can steal the last chunk */
1504 /* lop off the top part */
1505 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1506 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1507 l_len = SCTP_BUF_LEN(dmbuf);
1510 * need to count up the size hopefully does not hit
1516 for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1517 l_len += SCTP_BUF_LEN(lat);
1520 if (l_len > the_len) {
1521 /* Trim the end round bytes off too */
1522 m_adj(dmbuf, -(l_len - the_len));
1525 if (dmbuf == NULL) {
1526 SCTP_STAT_INCR(sctps_nomem);
1529 if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1530 asoc->fragmented_delivery_inprogress == 0 &&
1531 TAILQ_EMPTY(&asoc->resetHead) &&
1533 ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1534 TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1535 /* Candidate for express delivery */
1537 * Its not fragmented, No PD-API is up, Nothing in the
1538 * delivery queue, Its un-ordered OR ordered and the next to
1539 * deliver AND nothing else is stuck on the stream queue,
1540 * And there is room for it in the socket buffer. Lets just
1541 * stuff it up the buffer....
1544 /* It would be nice to avoid this copy if we could :< */
1545 sctp_alloc_a_readq(stcb, control);
1546 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1551 if (control == NULL) {
1552 goto failed_express_del;
1554 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1555 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1556 asoc->highest_tsn_inside_nr_map = tsn;
1558 sctp_add_to_readq(stcb->sctp_ep, stcb,
1559 control, &stcb->sctp_socket->so_rcv,
1560 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1562 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1563 /* for ordered, bump what we delivered */
1564 asoc->strmin[strmno].last_sequence_delivered++;
1566 SCTP_STAT_INCR(sctps_recvexpress);
1567 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
1568 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1569 SCTP_STR_LOG_FROM_EXPRS_DEL);
1573 goto finish_express_del;
1576 /* If we reach here this is a new chunk */
1579 /* Express for fragmented delivery? */
1580 if ((asoc->fragmented_delivery_inprogress) &&
1581 (stcb->asoc.control_pdapi) &&
1582 (asoc->str_of_pdapi == strmno) &&
1583 (asoc->ssn_of_pdapi == strmseq)
1585 control = stcb->asoc.control_pdapi;
1586 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1587 /* Can't be another first? */
1588 goto failed_pdapi_express_del;
1590 if (tsn == (control->sinfo_tsn + 1)) {
1591 /* Yep, we can add it on */
1594 if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1597 if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1599 &stcb->sctp_socket->so_rcv)) {
1600 SCTP_PRINTF("Append fails end:%d\n", end);
1601 goto failed_pdapi_express_del;
1603 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1604 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1605 asoc->highest_tsn_inside_nr_map = tsn;
1607 SCTP_STAT_INCR(sctps_recvexpressm);
1608 asoc->tsn_last_delivered = tsn;
1609 asoc->fragment_flags = chunk_flags;
1610 asoc->tsn_of_pdapi_last_delivered = tsn;
1611 asoc->last_flags_delivered = chunk_flags;
1612 asoc->last_strm_seq_delivered = strmseq;
1613 asoc->last_strm_no_delivered = strmno;
1615 /* clean up the flags and such */
1616 asoc->fragmented_delivery_inprogress = 0;
1617 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1618 asoc->strmin[strmno].last_sequence_delivered++;
1620 stcb->asoc.control_pdapi = NULL;
1621 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1623 * There could be another message
1626 need_reasm_check = 1;
1630 goto finish_express_del;
1633 failed_pdapi_express_del:
1635 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
1636 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1637 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1638 asoc->highest_tsn_inside_nr_map = tsn;
1641 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1642 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
1643 asoc->highest_tsn_inside_map = tsn;
1646 if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1647 sctp_alloc_a_chunk(stcb, chk);
1649 /* No memory so we drop the chunk */
1650 SCTP_STAT_INCR(sctps_nomem);
1651 if (last_chunk == 0) {
1652 /* we copied it, free the copy */
1653 sctp_m_freem(dmbuf);
1657 chk->rec.data.TSN_seq = tsn;
1658 chk->no_fr_allowed = 0;
1659 chk->rec.data.stream_seq = strmseq;
1660 chk->rec.data.stream_number = strmno;
1661 chk->rec.data.payloadtype = protocol_id;
1662 chk->rec.data.context = stcb->asoc.context;
1663 chk->rec.data.doing_fast_retransmit = 0;
1664 chk->rec.data.rcv_flags = chunk_flags;
1666 chk->send_size = the_len;
1668 atomic_add_int(&net->ref_count, 1);
1671 sctp_alloc_a_readq(stcb, control);
1672 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1677 if (control == NULL) {
1678 /* No memory so we drop the chunk */
1679 SCTP_STAT_INCR(sctps_nomem);
1680 if (last_chunk == 0) {
1681 /* we copied it, free the copy */
1682 sctp_m_freem(dmbuf);
1686 control->length = the_len;
1689 /* Mark it as received */
1690 /* Now queue it where it belongs */
1691 if (control != NULL) {
1692 /* First a sanity check */
1693 if (asoc->fragmented_delivery_inprogress) {
1695 * Ok, we have a fragmented delivery in progress if
1696 * this chunk is next to deliver OR belongs in our
1697 * view to the reassembly, the peer is evil or
1700 uint32_t estimate_tsn;
1702 estimate_tsn = asoc->tsn_last_delivered + 1;
1703 if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1704 (estimate_tsn == control->sinfo_tsn)) {
1705 /* Evil/Broke peer */
1706 sctp_m_freem(control->data);
1707 control->data = NULL;
1708 if (control->whoFrom) {
1709 sctp_free_remote_addr(control->whoFrom);
1710 control->whoFrom = NULL;
1712 sctp_free_a_readq(stcb, control);
1713 snprintf(msg, sizeof(msg), "Reas. queue emtpy, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1714 tsn, strmno, strmseq);
1715 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1716 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1717 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1724 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1725 sctp_m_freem(control->data);
1726 control->data = NULL;
1727 if (control->whoFrom) {
1728 sctp_free_remote_addr(control->whoFrom);
1729 control->whoFrom = NULL;
1731 sctp_free_a_readq(stcb, control);
1732 snprintf(msg, sizeof(msg), "PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1733 tsn, strmno, strmseq);
1734 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1735 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1736 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1745 /* No PDAPI running */
1746 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1748 * Reassembly queue is NOT empty validate
1749 * that this tsn does not need to be in
1750 * reasembly queue. If it does then our peer
1751 * is broken or evil.
1753 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1754 sctp_m_freem(control->data);
1755 control->data = NULL;
1756 if (control->whoFrom) {
1757 sctp_free_remote_addr(control->whoFrom);
1758 control->whoFrom = NULL;
1760 sctp_free_a_readq(stcb, control);
1761 snprintf(msg, sizeof(msg), "No PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1762 tsn, strmno, strmseq);
1763 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1764 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1765 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1774 /* ok, if we reach here we have passed the sanity checks */
1775 if (chunk_flags & SCTP_DATA_UNORDERED) {
1776 /* queue directly into socket buffer */
1777 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
1778 sctp_add_to_readq(stcb->sctp_ep, stcb,
1780 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1783 * Special check for when streams are resetting. We
1784 * could be more smart about this and check the
1785 * actual stream to see if it is not being reset..
1786 * that way we would not create a HOLB when amongst
1787 * streams being reset and those not being reset.
1789 * We take complete messages that have a stream reset
1790 * intervening (aka the TSN is after where our
1791 * cum-ack needs to be) off and put them on a
1792 * pending_reply_queue. The reassembly ones we do
1793 * not have to worry about since they are all sorted
1794 * and proceessed by TSN order. It is only the
1795 * singletons I must worry about.
1797 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
1798 SCTP_TSN_GT(tsn, liste->tsn)) {
1800 * yep its past where we need to reset... go
1801 * ahead and queue it.
1803 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
1805 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
1807 struct sctp_queued_to_read *ctlOn,
1809 unsigned char inserted = 0;
1811 TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
1812 if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
1816 TAILQ_INSERT_BEFORE(ctlOn, control, next);
1821 if (inserted == 0) {
1823 * must be put at end, use
1824 * prevP (all setup from
1825 * loop) to setup nextP.
1827 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
1831 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
1841 /* Into the re-assembly queue */
1842 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
1845 * the assoc is now gone and chk was put onto the
1846 * reasm queue, which has all been freed.
1855 if (tsn == (asoc->cumulative_tsn + 1)) {
1856 /* Update cum-ack */
1857 asoc->cumulative_tsn = tsn;
1863 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
1865 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
1867 SCTP_STAT_INCR(sctps_recvdata);
1868 /* Set it present please */
1869 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
1870 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
1872 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1873 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
1874 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
1876 /* check the special flag for stream resets */
1877 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
1878 SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
1880 * we have finished working through the backlogged TSN's now
1881 * time to reset streams. 1: call reset function. 2: free
1882 * pending_reply space 3: distribute any chunks in
1883 * pending_reply_queue.
1885 struct sctp_queued_to_read *ctl, *nctl;
1887 sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
1888 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
1889 SCTP_FREE(liste, SCTP_M_STRESET);
1890 /* sa_ignore FREED_MEMORY */
1891 liste = TAILQ_FIRST(&asoc->resetHead);
1892 if (TAILQ_EMPTY(&asoc->resetHead)) {
1893 /* All can be removed */
1894 TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
1895 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
1896 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
1902 TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
1903 if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
1907 * if ctl->sinfo_tsn is <= liste->tsn we can
1908 * process it which is the NOT of
1909 * ctl->sinfo_tsn > liste->tsn
1911 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
1912 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
1919 * Now service re-assembly to pick up anything that has been
1920 * held on reassembly queue?
1922 sctp_deliver_reasm_check(stcb, asoc);
1923 need_reasm_check = 0;
1925 if (need_reasm_check) {
1926 /* Another one waits ? */
1927 sctp_deliver_reasm_check(stcb, asoc);
1932 int8_t sctp_map_lookup_tab[256] = {
1933 0, 1, 0, 2, 0, 1, 0, 3,
1934 0, 1, 0, 2, 0, 1, 0, 4,
1935 0, 1, 0, 2, 0, 1, 0, 3,
1936 0, 1, 0, 2, 0, 1, 0, 5,
1937 0, 1, 0, 2, 0, 1, 0, 3,
1938 0, 1, 0, 2, 0, 1, 0, 4,
1939 0, 1, 0, 2, 0, 1, 0, 3,
1940 0, 1, 0, 2, 0, 1, 0, 6,
1941 0, 1, 0, 2, 0, 1, 0, 3,
1942 0, 1, 0, 2, 0, 1, 0, 4,
1943 0, 1, 0, 2, 0, 1, 0, 3,
1944 0, 1, 0, 2, 0, 1, 0, 5,
1945 0, 1, 0, 2, 0, 1, 0, 3,
1946 0, 1, 0, 2, 0, 1, 0, 4,
1947 0, 1, 0, 2, 0, 1, 0, 3,
1948 0, 1, 0, 2, 0, 1, 0, 7,
1949 0, 1, 0, 2, 0, 1, 0, 3,
1950 0, 1, 0, 2, 0, 1, 0, 4,
1951 0, 1, 0, 2, 0, 1, 0, 3,
1952 0, 1, 0, 2, 0, 1, 0, 5,
1953 0, 1, 0, 2, 0, 1, 0, 3,
1954 0, 1, 0, 2, 0, 1, 0, 4,
1955 0, 1, 0, 2, 0, 1, 0, 3,
1956 0, 1, 0, 2, 0, 1, 0, 6,
1957 0, 1, 0, 2, 0, 1, 0, 3,
1958 0, 1, 0, 2, 0, 1, 0, 4,
1959 0, 1, 0, 2, 0, 1, 0, 3,
1960 0, 1, 0, 2, 0, 1, 0, 5,
1961 0, 1, 0, 2, 0, 1, 0, 3,
1962 0, 1, 0, 2, 0, 1, 0, 4,
1963 0, 1, 0, 2, 0, 1, 0, 3,
1964 0, 1, 0, 2, 0, 1, 0, 8
1969 sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
1972 * Now we also need to check the mapping array in a couple of ways.
1973 * 1) Did we move the cum-ack point?
1975 * When you first glance at this you might think that all entries that
1976 * make up the postion of the cum-ack would be in the nr-mapping
1977 * array only.. i.e. things up to the cum-ack are always
1978 * deliverable. Thats true with one exception, when its a fragmented
1979 * message we may not deliver the data until some threshold (or all
1980 * of it) is in place. So we must OR the nr_mapping_array and
1981 * mapping_array to get a true picture of the cum-ack.
1983 struct sctp_association *asoc;
1986 int slide_from, slide_end, lgap, distance;
1987 uint32_t old_cumack, old_base, old_highest, highest_tsn;
1991 old_cumack = asoc->cumulative_tsn;
1992 old_base = asoc->mapping_array_base_tsn;
1993 old_highest = asoc->highest_tsn_inside_map;
1995 * We could probably improve this a small bit by calculating the
1996 * offset of the current cum-ack as the starting point.
1999 for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2000 val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
2004 /* there is a 0 bit */
2005 at += sctp_map_lookup_tab[val];
2009 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2011 if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
2012 SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2014 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2015 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2017 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2018 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2019 sctp_print_mapping_array(asoc);
2020 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2021 sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2023 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2024 asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2027 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2028 highest_tsn = asoc->highest_tsn_inside_nr_map;
2030 highest_tsn = asoc->highest_tsn_inside_map;
2032 if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2033 /* The complete array was completed by a single FR */
2034 /* highest becomes the cum-ack */
2042 /* clear the array */
2043 clr = ((at + 7) >> 3);
2044 if (clr > asoc->mapping_array_size) {
2045 clr = asoc->mapping_array_size;
2047 memset(asoc->mapping_array, 0, clr);
2048 memset(asoc->nr_mapping_array, 0, clr);
2050 for (i = 0; i < asoc->mapping_array_size; i++) {
2051 if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2052 SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2053 sctp_print_mapping_array(asoc);
2057 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2058 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2059 } else if (at >= 8) {
2060 /* we can slide the mapping array down */
2061 /* slide_from holds where we hit the first NON 0xff byte */
2064 * now calculate the ceiling of the move using our highest
2067 SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
2068 slide_end = (lgap >> 3);
2069 if (slide_end < slide_from) {
2070 sctp_print_mapping_array(asoc);
2072 panic("impossible slide");
2074 SCTP_PRINTF("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
2075 lgap, slide_end, slide_from, at);
2079 if (slide_end > asoc->mapping_array_size) {
2081 panic("would overrun buffer");
2083 SCTP_PRINTF("Gak, would have overrun map end:%d slide_end:%d\n",
2084 asoc->mapping_array_size, slide_end);
2085 slide_end = asoc->mapping_array_size;
2088 distance = (slide_end - slide_from) + 1;
2089 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2090 sctp_log_map(old_base, old_cumack, old_highest,
2091 SCTP_MAP_PREPARE_SLIDE);
2092 sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2093 (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2095 if (distance + slide_from > asoc->mapping_array_size ||
2098 * Here we do NOT slide forward the array so that
2099 * hopefully when more data comes in to fill it up
2100 * we will be able to slide it forward. Really I
2101 * don't think this should happen :-0
2104 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2105 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2106 (uint32_t) asoc->mapping_array_size,
2107 SCTP_MAP_SLIDE_NONE);
2112 for (ii = 0; ii < distance; ii++) {
2113 asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
2114 asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
2117 for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2118 asoc->mapping_array[ii] = 0;
2119 asoc->nr_mapping_array[ii] = 0;
2121 if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2122 asoc->highest_tsn_inside_map += (slide_from << 3);
2124 if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2125 asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2127 asoc->mapping_array_base_tsn += (slide_from << 3);
2128 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2129 sctp_log_map(asoc->mapping_array_base_tsn,
2130 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2131 SCTP_MAP_SLIDE_RESULT);
2138 sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2140 struct sctp_association *asoc;
2141 uint32_t highest_tsn;
2144 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2145 highest_tsn = asoc->highest_tsn_inside_nr_map;
2147 highest_tsn = asoc->highest_tsn_inside_map;
2151 * Now we need to see if we need to queue a sack or just start the
2152 * timer (if allowed).
2154 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2156 * Ok special case, in SHUTDOWN-SENT case. here we maker
2157 * sure SACK timer is off and instead send a SHUTDOWN and a
2160 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2161 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2162 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2164 sctp_send_shutdown(stcb,
2165 ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2166 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2170 /* is there a gap now ? */
2171 is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2174 * CMT DAC algorithm: increase number of packets received
2177 stcb->asoc.cmt_dac_pkts_rcvd++;
2179 if ((stcb->asoc.send_sack == 1) || /* We need to send a
2181 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2183 (stcb->asoc.numduptsns) || /* we have dup's */
2184 (is_a_gap) || /* is still a gap */
2185 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2186 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2189 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2190 (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
2191 (stcb->asoc.send_sack == 0) &&
2192 (stcb->asoc.numduptsns == 0) &&
2193 (stcb->asoc.delayed_ack) &&
2194 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2197 * CMT DAC algorithm: With CMT, delay acks
2198 * even in the face of
2200 * reordering. Therefore, if acks that do not
2201 * have to be sent because of the above
2202 * reasons, will be delayed. That is, acks
2203 * that would have been sent due to gap
2204 * reports will be delayed with DAC. Start
2205 * the delayed ack timer.
2207 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2208 stcb->sctp_ep, stcb, NULL);
2211 * Ok we must build a SACK since the timer
2212 * is pending, we got our first packet OR
2213 * there are gaps or duplicates.
2215 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2216 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2219 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2220 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2221 stcb->sctp_ep, stcb, NULL);
2228 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2230 struct sctp_tmit_chunk *chk;
2231 uint32_t tsize, pd_point;
2234 if (asoc->fragmented_delivery_inprogress) {
2235 sctp_service_reassembly(stcb, asoc);
2237 /* Can we proceed further, i.e. the PD-API is complete */
2238 if (asoc->fragmented_delivery_inprogress) {
2243 * Now is there some other chunk I can deliver from the reassembly
2247 chk = TAILQ_FIRST(&asoc->reasmqueue);
2249 asoc->size_on_reasm_queue = 0;
2250 asoc->cnt_on_reasm_queue = 0;
2253 nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2254 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2255 ((nxt_todel == chk->rec.data.stream_seq) ||
2256 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2258 * Yep the first one is here. We setup to start reception,
2259 * by backing down the TSN just in case we can't deliver.
2263 * Before we start though either all of the message should
2264 * be here or the socket buffer max or nothing on the
2265 * delivery queue and something can be delivered.
2267 if (stcb->sctp_socket) {
2268 pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
2269 stcb->sctp_ep->partial_delivery_point);
2271 pd_point = stcb->sctp_ep->partial_delivery_point;
2273 if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2274 asoc->fragmented_delivery_inprogress = 1;
2275 asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2276 asoc->str_of_pdapi = chk->rec.data.stream_number;
2277 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2278 asoc->pdapi_ppid = chk->rec.data.payloadtype;
2279 asoc->fragment_flags = chk->rec.data.rcv_flags;
2280 sctp_service_reassembly(stcb, asoc);
2281 if (asoc->fragmented_delivery_inprogress == 0) {
2289 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2290 struct sockaddr *src, struct sockaddr *dst,
2291 struct sctphdr *sh, struct sctp_inpcb *inp,
2292 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t * high_tsn,
2293 uint8_t mflowtype, uint32_t mflowid,
2294 uint32_t vrf_id, uint16_t port)
2296 struct sctp_data_chunk *ch, chunk_buf;
2297 struct sctp_association *asoc;
2298 int num_chunks = 0; /* number of control chunks processed */
2300 int chk_length, break_flag, last_chunk;
2301 int abort_flag = 0, was_a_gap;
2303 uint32_t highest_tsn;
2306 sctp_set_rwnd(stcb, &stcb->asoc);
2309 SCTP_TCB_LOCK_ASSERT(stcb);
2311 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2312 highest_tsn = asoc->highest_tsn_inside_nr_map;
2314 highest_tsn = asoc->highest_tsn_inside_map;
2316 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2318 * setup where we got the last DATA packet from for any SACK that
2319 * may need to go out. Don't bump the net. This is done ONLY when a
2320 * chunk is assigned.
2322 asoc->last_data_chunk_from = net;
2325 * Now before we proceed we must figure out if this is a wasted
2326 * cluster... i.e. it is a small packet sent in and yet the driver
2327 * underneath allocated a full cluster for it. If so we must copy it
2328 * to a smaller mbuf and free up the cluster mbuf. This will help
2329 * with cluster starvation. Note for __Panda__ we don't do this
2330 * since it has clusters all the way down to 64 bytes.
2332 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2333 /* we only handle mbufs that are singletons.. not chains */
2334 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2336 /* ok lets see if we can copy the data up */
2339 /* get the pointers and copy */
2340 to = mtod(m, caddr_t *);
2341 from = mtod((*mm), caddr_t *);
2342 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2343 /* copy the length and free up the old */
2344 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2346 /* sucess, back copy */
2349 /* We are in trouble in the mbuf world .. yikes */
2353 /* get pointer to the first chunk header */
2354 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2355 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2360 * process all DATA chunks...
2362 *high_tsn = asoc->cumulative_tsn;
2364 asoc->data_pkts_seen++;
2365 while (stop_proc == 0) {
2366 /* validate chunk length */
2367 chk_length = ntohs(ch->ch.chunk_length);
2368 if (length - *offset < chk_length) {
2369 /* all done, mutulated chunk */
2373 if (ch->ch.chunk_type == SCTP_DATA) {
2374 if ((size_t)chk_length < sizeof(struct sctp_data_chunk)) {
2376 * Need to send an abort since we had a
2377 * invalid data chunk.
2379 struct mbuf *op_err;
2380 char msg[SCTP_DIAG_INFO_LEN];
2382 snprintf(msg, sizeof(msg), "DATA chunk of length %d",
2384 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2385 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2386 sctp_abort_association(inp, stcb, m, iphlen,
2387 src, dst, sh, op_err,
2392 if ((size_t)chk_length == sizeof(struct sctp_data_chunk)) {
2394 * Need to send an abort since we had an
2397 struct mbuf *op_err;
2399 op_err = sctp_generate_no_user_data_cause(ch->dp.tsn);
2400 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2401 sctp_abort_association(inp, stcb, m, iphlen,
2402 src, dst, sh, op_err,
2407 #ifdef SCTP_AUDITING_ENABLED
2408 sctp_audit_log(0xB1, 0);
2410 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2415 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2416 chk_length, net, high_tsn, &abort_flag, &break_flag,
2425 * Set because of out of rwnd space and no
2426 * drop rep space left.
2432 /* not a data chunk in the data region */
2433 switch (ch->ch.chunk_type) {
2434 case SCTP_INITIATION:
2435 case SCTP_INITIATION_ACK:
2436 case SCTP_SELECTIVE_ACK:
2437 case SCTP_NR_SELECTIVE_ACK:
2438 case SCTP_HEARTBEAT_REQUEST:
2439 case SCTP_HEARTBEAT_ACK:
2440 case SCTP_ABORT_ASSOCIATION:
2442 case SCTP_SHUTDOWN_ACK:
2443 case SCTP_OPERATION_ERROR:
2444 case SCTP_COOKIE_ECHO:
2445 case SCTP_COOKIE_ACK:
2448 case SCTP_SHUTDOWN_COMPLETE:
2449 case SCTP_AUTHENTICATION:
2450 case SCTP_ASCONF_ACK:
2451 case SCTP_PACKET_DROPPED:
2452 case SCTP_STREAM_RESET:
2453 case SCTP_FORWARD_CUM_TSN:
2456 * Now, what do we do with KNOWN chunks that
2457 * are NOT in the right place?
2459 * For now, I do nothing but ignore them. We
2460 * may later want to add sysctl stuff to
2461 * switch out and do either an ABORT() or
2462 * possibly process them.
2464 if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2465 struct mbuf *op_err;
2467 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
2468 sctp_abort_association(inp, stcb,
2478 /* unknown chunk type, use bit rules */
2479 if (ch->ch.chunk_type & 0x40) {
2480 /* Add a error report to the queue */
2482 struct sctp_paramhdr *phd;
2484 merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_NOWAIT, 1, MT_DATA);
2486 phd = mtod(merr, struct sctp_paramhdr *);
2488 * We cheat and use param
2489 * type since we did not
2490 * bother to define a error
2491 * cause struct. They are
2492 * the same basic format
2493 * with different names.
2496 htons(SCTP_CAUSE_UNRECOG_CHUNK);
2498 htons(chk_length + sizeof(*phd));
2499 SCTP_BUF_LEN(merr) = sizeof(*phd);
2500 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
2501 if (SCTP_BUF_NEXT(merr)) {
2502 if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL) == NULL) {
2505 sctp_queue_op_err(stcb, merr);
2512 if ((ch->ch.chunk_type & 0x80) == 0) {
2513 /* discard the rest of this packet */
2515 } /* else skip this bad chunk and
2518 } /* switch of chunk type */
2520 *offset += SCTP_SIZE32(chk_length);
2521 if ((*offset >= length) || stop_proc) {
2522 /* no more data left in the mbuf chain */
2526 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2527 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2536 * we need to report rwnd overrun drops.
2538 sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2542 * Did we get data, if so update the time for auto-close and
2543 * give peer credit for being alive.
2545 SCTP_STAT_INCR(sctps_recvpktwithdata);
2546 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2547 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2548 stcb->asoc.overall_error_count,
2550 SCTP_FROM_SCTP_INDATA,
2553 stcb->asoc.overall_error_count = 0;
2554 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2556 /* now service all of the reassm queue if needed */
2557 if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2558 sctp_service_queues(stcb, asoc);
2560 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2561 /* Assure that we ack right away */
2562 stcb->asoc.send_sack = 1;
2564 /* Start a sack timer or QUEUE a SACK for sending */
2565 sctp_sack_check(stcb, was_a_gap);
2570 sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
2571 uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
2573 uint32_t * biggest_newly_acked_tsn,
2574 uint32_t * this_sack_lowest_newack,
2577 struct sctp_tmit_chunk *tp1;
2578 unsigned int theTSN;
2579 int j, wake_him = 0, circled = 0;
2581 /* Recover the tp1 we last saw */
2584 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2586 for (j = frag_strt; j <= frag_end; j++) {
2587 theTSN = j + last_tsn;
2589 if (tp1->rec.data.doing_fast_retransmit)
2593 * CMT: CUCv2 algorithm. For each TSN being
2594 * processed from the sent queue, track the
2595 * next expected pseudo-cumack, or
2596 * rtx_pseudo_cumack, if required. Separate
2597 * cumack trackers for first transmissions,
2598 * and retransmissions.
2600 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
2601 (tp1->whoTo->find_pseudo_cumack == 1) &&
2602 (tp1->snd_count == 1)) {
2603 tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2604 tp1->whoTo->find_pseudo_cumack = 0;
2606 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
2607 (tp1->whoTo->find_rtx_pseudo_cumack == 1) &&
2608 (tp1->snd_count > 1)) {
2609 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2610 tp1->whoTo->find_rtx_pseudo_cumack = 0;
2612 if (tp1->rec.data.TSN_seq == theTSN) {
2613 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2615 * must be held until
2618 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2620 * If it is less than RESEND, it is
2621 * now no-longer in flight.
2622 * Higher values may already be set
2623 * via previous Gap Ack Blocks...
2624 * i.e. ACKED or RESEND.
2626 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
2627 *biggest_newly_acked_tsn)) {
2628 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2631 * CMT: SFR algo (and HTNA) - set
2632 * saw_newack to 1 for dest being
2633 * newly acked. update
2634 * this_sack_highest_newack if
2637 if (tp1->rec.data.chunk_was_revoked == 0)
2638 tp1->whoTo->saw_newack = 1;
2640 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
2641 tp1->whoTo->this_sack_highest_newack)) {
2642 tp1->whoTo->this_sack_highest_newack =
2643 tp1->rec.data.TSN_seq;
2646 * CMT DAC algo: also update
2647 * this_sack_lowest_newack
2649 if (*this_sack_lowest_newack == 0) {
2650 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2651 sctp_log_sack(*this_sack_lowest_newack,
2653 tp1->rec.data.TSN_seq,
2656 SCTP_LOG_TSN_ACKED);
2658 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2661 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
2662 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
2663 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
2664 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
2665 * Separate pseudo_cumack trackers for first transmissions and
2668 if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2669 if (tp1->rec.data.chunk_was_revoked == 0) {
2670 tp1->whoTo->new_pseudo_cumack = 1;
2672 tp1->whoTo->find_pseudo_cumack = 1;
2674 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2675 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2677 if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2678 if (tp1->rec.data.chunk_was_revoked == 0) {
2679 tp1->whoTo->new_pseudo_cumack = 1;
2681 tp1->whoTo->find_rtx_pseudo_cumack = 1;
2683 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2684 sctp_log_sack(*biggest_newly_acked_tsn,
2686 tp1->rec.data.TSN_seq,
2689 SCTP_LOG_TSN_ACKED);
2691 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2692 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
2693 tp1->whoTo->flight_size,
2695 (uintptr_t) tp1->whoTo,
2696 tp1->rec.data.TSN_seq);
2698 sctp_flight_size_decrease(tp1);
2699 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2700 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2703 sctp_total_flight_decrease(stcb, tp1);
2705 tp1->whoTo->net_ack += tp1->send_size;
2706 if (tp1->snd_count < 2) {
2708 * True non-retransmited chunk
2710 tp1->whoTo->net_ack2 += tp1->send_size;
2718 sctp_calculate_rto(stcb,
2721 &tp1->sent_rcv_time,
2722 sctp_align_safe_nocopy,
2723 SCTP_RTT_FROM_DATA);
2726 if (tp1->whoTo->rto_needed == 0) {
2727 tp1->whoTo->rto_needed = 1;
2733 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
2734 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
2735 stcb->asoc.this_sack_highest_gap)) {
2736 stcb->asoc.this_sack_highest_gap =
2737 tp1->rec.data.TSN_seq;
2739 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
2740 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
2741 #ifdef SCTP_AUDITING_ENABLED
2742 sctp_audit_log(0xB2,
2743 (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
2748 * All chunks NOT UNSENT fall through here and are marked
2749 * (leave PR-SCTP ones that are to skip alone though)
2751 if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
2752 (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2753 tp1->sent = SCTP_DATAGRAM_MARKED;
2755 if (tp1->rec.data.chunk_was_revoked) {
2756 /* deflate the cwnd */
2757 tp1->whoTo->cwnd -= tp1->book_size;
2758 tp1->rec.data.chunk_was_revoked = 0;
2760 /* NR Sack code here */
2762 (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2763 if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
2764 stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--;
2767 panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
2770 tp1->sent = SCTP_DATAGRAM_NR_ACKED;
2776 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
2777 sctp_m_freem(tp1->data);
2784 } /* if (tp1->TSN_seq == theTSN) */
2785 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
2788 tp1 = TAILQ_NEXT(tp1, sctp_next);
2789 if ((tp1 == NULL) && (circled == 0)) {
2791 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2793 } /* end while (tp1) */
2796 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2798 /* In case the fragments were not in order we must reset */
2799 } /* end for (j = fragStart */
2801 return (wake_him); /* Return value only used for nr-sack */
2806 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2807 uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2808 uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2809 int num_seg, int num_nr_seg, int *rto_ok)
2811 struct sctp_gap_ack_block *frag, block;
2812 struct sctp_tmit_chunk *tp1;
2817 uint16_t frag_strt, frag_end, prev_frag_end;
2819 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2823 for (i = 0; i < (num_seg + num_nr_seg); i++) {
2826 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2828 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2829 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2830 *offset += sizeof(block);
2832 return (chunk_freed);
2834 frag_strt = ntohs(frag->start);
2835 frag_end = ntohs(frag->end);
2837 if (frag_strt > frag_end) {
2838 /* This gap report is malformed, skip it. */
2841 if (frag_strt <= prev_frag_end) {
2842 /* This gap report is not in order, so restart. */
2843 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2845 if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
2846 *biggest_tsn_acked = last_tsn + frag_end;
2853 if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
2854 non_revocable, &num_frs, biggest_newly_acked_tsn,
2855 this_sack_lowest_newack, rto_ok)) {
2858 prev_frag_end = frag_end;
2860 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2862 sctp_log_fr(*biggest_tsn_acked,
2863 *biggest_newly_acked_tsn,
2864 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
2866 return (chunk_freed);
2870 sctp_check_for_revoked(struct sctp_tcb *stcb,
2871 struct sctp_association *asoc, uint32_t cumack,
2872 uint32_t biggest_tsn_acked)
2874 struct sctp_tmit_chunk *tp1;
2876 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
2877 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
2879 * ok this guy is either ACK or MARKED. If it is
2880 * ACKED it has been previously acked but not this
2881 * time i.e. revoked. If it is MARKED it was ACK'ed
2884 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
2887 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
2888 /* it has been revoked */
2889 tp1->sent = SCTP_DATAGRAM_SENT;
2890 tp1->rec.data.chunk_was_revoked = 1;
2892 * We must add this stuff back in to assure
2893 * timers and such get started.
2895 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2896 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
2897 tp1->whoTo->flight_size,
2899 (uintptr_t) tp1->whoTo,
2900 tp1->rec.data.TSN_seq);
2902 sctp_flight_size_increase(tp1);
2903 sctp_total_flight_increase(stcb, tp1);
2905 * We inflate the cwnd to compensate for our
2906 * artificial inflation of the flight_size.
2908 tp1->whoTo->cwnd += tp1->book_size;
2909 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2910 sctp_log_sack(asoc->last_acked_seq,
2912 tp1->rec.data.TSN_seq,
2915 SCTP_LOG_TSN_REVOKED);
2917 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
2918 /* it has been re-acked in this SACK */
2919 tp1->sent = SCTP_DATAGRAM_ACKED;
2922 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
2929 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
2930 uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
2932 struct sctp_tmit_chunk *tp1;
2933 int strike_flag = 0;
2935 int tot_retrans = 0;
2936 uint32_t sending_seq;
2937 struct sctp_nets *net;
2938 int num_dests_sacked = 0;
2941 * select the sending_seq, this is either the next thing ready to be
2942 * sent but not transmitted, OR, the next seq we assign.
2944 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
2946 sending_seq = asoc->sending_seq;
2948 sending_seq = tp1->rec.data.TSN_seq;
2951 /* CMT DAC algo: finding out if SACK is a mixed SACK */
2952 if ((asoc->sctp_cmt_on_off > 0) &&
2953 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
2954 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2955 if (net->saw_newack)
2959 if (stcb->asoc.prsctp_supported) {
2960 (void)SCTP_GETTIME_TIMEVAL(&now);
2962 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
2964 if (tp1->no_fr_allowed) {
2965 /* this one had a timeout or something */
2968 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2969 if (tp1->sent < SCTP_DATAGRAM_RESEND)
2970 sctp_log_fr(biggest_tsn_newly_acked,
2971 tp1->rec.data.TSN_seq,
2973 SCTP_FR_LOG_CHECK_STRIKE);
2975 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
2976 tp1->sent == SCTP_DATAGRAM_UNSENT) {
2980 if (stcb->asoc.prsctp_supported) {
2981 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
2982 /* Is it expired? */
2983 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
2984 /* Yes so drop it */
2985 if (tp1->data != NULL) {
2986 (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
2987 SCTP_SO_NOT_LOCKED);
2993 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
2994 /* we are beyond the tsn in the sack */
2997 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
2998 /* either a RESEND, ACKED, or MARKED */
3000 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3001 /* Continue strikin FWD-TSN chunks */
3002 tp1->rec.data.fwd_tsn_cnt++;
3007 * CMT : SFR algo (covers part of DAC and HTNA as well)
3009 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3011 * No new acks were receieved for data sent to this
3012 * dest. Therefore, according to the SFR algo for
3013 * CMT, no data sent to this dest can be marked for
3014 * FR using this SACK.
3017 } else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
3018 tp1->whoTo->this_sack_highest_newack)) {
3020 * CMT: New acks were receieved for data sent to
3021 * this dest. But no new acks were seen for data
3022 * sent after tp1. Therefore, according to the SFR
3023 * algo for CMT, tp1 cannot be marked for FR using
3024 * this SACK. This step covers part of the DAC algo
3025 * and the HTNA algo as well.
3030 * Here we check to see if we were have already done a FR
3031 * and if so we see if the biggest TSN we saw in the sack is
3032 * smaller than the recovery point. If so we don't strike
3033 * the tsn... otherwise we CAN strike the TSN.
3036 * @@@ JRI: Check for CMT if (accum_moved &&
3037 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3040 if (accum_moved && asoc->fast_retran_loss_recovery) {
3042 * Strike the TSN if in fast-recovery and cum-ack
3045 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3046 sctp_log_fr(biggest_tsn_newly_acked,
3047 tp1->rec.data.TSN_seq,
3049 SCTP_FR_LOG_STRIKE_CHUNK);
3051 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3054 if ((asoc->sctp_cmt_on_off > 0) &&
3055 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3057 * CMT DAC algorithm: If SACK flag is set to
3058 * 0, then lowest_newack test will not pass
3059 * because it would have been set to the
3060 * cumack earlier. If not already to be
3061 * rtx'd, If not a mixed sack and if tp1 is
3062 * not between two sacked TSNs, then mark by
3063 * one more. NOTE that we are marking by one
3064 * additional time since the SACK DAC flag
3065 * indicates that two packets have been
3066 * received after this missing TSN.
3068 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3069 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3070 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3071 sctp_log_fr(16 + num_dests_sacked,
3072 tp1->rec.data.TSN_seq,
3074 SCTP_FR_LOG_STRIKE_CHUNK);
3079 } else if ((tp1->rec.data.doing_fast_retransmit) &&
3080 (asoc->sctp_cmt_on_off == 0)) {
3082 * For those that have done a FR we must take
3083 * special consideration if we strike. I.e the
3084 * biggest_newly_acked must be higher than the
3085 * sending_seq at the time we did the FR.
3088 #ifdef SCTP_FR_TO_ALTERNATE
3090 * If FR's go to new networks, then we must only do
3091 * this for singly homed asoc's. However if the FR's
3092 * go to the same network (Armando's work) then its
3093 * ok to FR multiple times.
3101 if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3102 tp1->rec.data.fast_retran_tsn)) {
3104 * Strike the TSN, since this ack is
3105 * beyond where things were when we
3108 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3109 sctp_log_fr(biggest_tsn_newly_acked,
3110 tp1->rec.data.TSN_seq,
3112 SCTP_FR_LOG_STRIKE_CHUNK);
3114 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3118 if ((asoc->sctp_cmt_on_off > 0) &&
3119 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3121 * CMT DAC algorithm: If
3122 * SACK flag is set to 0,
3123 * then lowest_newack test
3124 * will not pass because it
3125 * would have been set to
3126 * the cumack earlier. If
3127 * not already to be rtx'd,
3128 * If not a mixed sack and
3129 * if tp1 is not between two
3130 * sacked TSNs, then mark by
3131 * one more. NOTE that we
3132 * are marking by one
3133 * additional time since the
3134 * SACK DAC flag indicates
3135 * that two packets have
3136 * been received after this
3139 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3140 (num_dests_sacked == 1) &&
3141 SCTP_TSN_GT(this_sack_lowest_newack,
3142 tp1->rec.data.TSN_seq)) {
3143 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3144 sctp_log_fr(32 + num_dests_sacked,
3145 tp1->rec.data.TSN_seq,
3147 SCTP_FR_LOG_STRIKE_CHUNK);
3149 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3157 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3160 } else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
3161 biggest_tsn_newly_acked)) {
3163 * We don't strike these: This is the HTNA
3164 * algorithm i.e. we don't strike If our TSN is
3165 * larger than the Highest TSN Newly Acked.
3169 /* Strike the TSN */
3170 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3171 sctp_log_fr(biggest_tsn_newly_acked,
3172 tp1->rec.data.TSN_seq,
3174 SCTP_FR_LOG_STRIKE_CHUNK);
3176 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3179 if ((asoc->sctp_cmt_on_off > 0) &&
3180 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3182 * CMT DAC algorithm: If SACK flag is set to
3183 * 0, then lowest_newack test will not pass
3184 * because it would have been set to the
3185 * cumack earlier. If not already to be
3186 * rtx'd, If not a mixed sack and if tp1 is
3187 * not between two sacked TSNs, then mark by
3188 * one more. NOTE that we are marking by one
3189 * additional time since the SACK DAC flag
3190 * indicates that two packets have been
3191 * received after this missing TSN.
3193 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3194 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3195 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3196 sctp_log_fr(48 + num_dests_sacked,
3197 tp1->rec.data.TSN_seq,
3199 SCTP_FR_LOG_STRIKE_CHUNK);
3205 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3206 struct sctp_nets *alt;
3208 /* fix counts and things */
3209 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3210 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3211 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3213 (uintptr_t) tp1->whoTo,
3214 tp1->rec.data.TSN_seq);
3217 tp1->whoTo->net_ack++;
3218 sctp_flight_size_decrease(tp1);
3219 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3220 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3224 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3225 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3226 asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3228 /* add back to the rwnd */
3229 asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3231 /* remove from the total flight */
3232 sctp_total_flight_decrease(stcb, tp1);
3234 if ((stcb->asoc.prsctp_supported) &&
3235 (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3237 * Has it been retransmitted tv_sec times? -
3238 * we store the retran count there.
3240 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3241 /* Yes, so drop it */
3242 if (tp1->data != NULL) {
3243 (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3244 SCTP_SO_NOT_LOCKED);
3246 /* Make sure to flag we had a FR */
3247 tp1->whoTo->net_ack++;
3252 * SCTP_PRINTF("OK, we are now ready to FR this
3255 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3256 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3260 /* This is a subsequent FR */
3261 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3263 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3264 if (asoc->sctp_cmt_on_off > 0) {
3266 * CMT: Using RTX_SSTHRESH policy for CMT.
3267 * If CMT is being used, then pick dest with
3268 * largest ssthresh for any retransmission.
3270 tp1->no_fr_allowed = 1;
3272 /* sa_ignore NO_NULL_CHK */
3273 if (asoc->sctp_cmt_pf > 0) {
3275 * JRS 5/18/07 - If CMT PF is on,
3276 * use the PF version of
3279 alt = sctp_find_alternate_net(stcb, alt, 2);
3282 * JRS 5/18/07 - If only CMT is on,
3283 * use the CMT version of
3286 /* sa_ignore NO_NULL_CHK */
3287 alt = sctp_find_alternate_net(stcb, alt, 1);
3293 * CUCv2: If a different dest is picked for
3294 * the retransmission, then new
3295 * (rtx-)pseudo_cumack needs to be tracked
3296 * for orig dest. Let CUCv2 track new (rtx-)
3297 * pseudo-cumack always.
3300 tp1->whoTo->find_pseudo_cumack = 1;
3301 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3303 } else {/* CMT is OFF */
3305 #ifdef SCTP_FR_TO_ALTERNATE
3306 /* Can we find an alternate? */
3307 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3310 * default behavior is to NOT retransmit
3311 * FR's to an alternate. Armando Caro's
3312 * paper details why.
3318 tp1->rec.data.doing_fast_retransmit = 1;
3320 /* mark the sending seq for possible subsequent FR's */
3322 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3323 * (uint32_t)tpi->rec.data.TSN_seq);
3325 if (TAILQ_EMPTY(&asoc->send_queue)) {
3327 * If the queue of send is empty then its
3328 * the next sequence number that will be
3329 * assigned so we subtract one from this to
3330 * get the one we last sent.
3332 tp1->rec.data.fast_retran_tsn = sending_seq;
3335 * If there are chunks on the send queue
3336 * (unsent data that has made it from the
3337 * stream queues but not out the door, we
3338 * take the first one (which will have the
3339 * lowest TSN) and subtract one to get the
3342 struct sctp_tmit_chunk *ttt;
3344 ttt = TAILQ_FIRST(&asoc->send_queue);
3345 tp1->rec.data.fast_retran_tsn =
3346 ttt->rec.data.TSN_seq;
3351 * this guy had a RTO calculation pending on
3354 if ((tp1->whoTo != NULL) &&
3355 (tp1->whoTo->rto_needed == 0)) {
3356 tp1->whoTo->rto_needed = 1;
3360 if (alt != tp1->whoTo) {
3361 /* yes, there is an alternate. */
3362 sctp_free_remote_addr(tp1->whoTo);
3363 /* sa_ignore FREED_MEMORY */
3365 atomic_add_int(&alt->ref_count, 1);
3371 struct sctp_tmit_chunk *
3372 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3373 struct sctp_association *asoc)
3375 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3379 if (asoc->prsctp_supported == 0) {
3382 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3383 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3384 tp1->sent != SCTP_DATAGRAM_RESEND &&
3385 tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3386 /* no chance to advance, out of here */
3389 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
3390 if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3391 (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3392 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
3393 asoc->advanced_peer_ack_point,
3394 tp1->rec.data.TSN_seq, 0, 0);
3397 if (!PR_SCTP_ENABLED(tp1->flags)) {
3399 * We can't fwd-tsn past any that are reliable aka
3400 * retransmitted until the asoc fails.
3405 (void)SCTP_GETTIME_TIMEVAL(&now);
3409 * now we got a chunk which is marked for another
3410 * retransmission to a PR-stream but has run out its chances
3411 * already maybe OR has been marked to skip now. Can we skip
3412 * it if its a resend?
3414 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3415 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3417 * Now is this one marked for resend and its time is
3420 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3421 /* Yes so drop it */
3423 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3424 1, SCTP_SO_NOT_LOCKED);
3428 * No, we are done when hit one for resend
3429 * whos time as not expired.
3435 * Ok now if this chunk is marked to drop it we can clean up
3436 * the chunk, advance our peer ack point and we can check
3439 if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3440 (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3441 /* advance PeerAckPoint goes forward */
3442 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3443 asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3445 } else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
3446 /* No update but we do save the chk */
3451 * If it is still in RESEND we can advance no
3461 sctp_fs_audit(struct sctp_association *asoc)
3463 struct sctp_tmit_chunk *chk;
3464 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3465 int entry_flight, entry_cnt, ret;
3467 entry_flight = asoc->total_flight;
3468 entry_cnt = asoc->total_flight_count;
3471 if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
3474 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3475 if (chk->sent < SCTP_DATAGRAM_RESEND) {
3476 SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n",
3477 chk->rec.data.TSN_seq,
3481 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3483 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3485 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3492 if ((inflight > 0) || (inbetween > 0)) {
3494 panic("Flight size-express incorrect? \n");
3496 SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n",
3497 entry_flight, entry_cnt);
3499 SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
3500 inflight, inbetween, resend, above, acked);
3509 sctp_window_probe_recovery(struct sctp_tcb *stcb,
3510 struct sctp_association *asoc,
3511 struct sctp_tmit_chunk *tp1)
3513 tp1->window_probe = 0;
3514 if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3515 /* TSN's skipped we do NOT move back. */
3516 sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3517 tp1->whoTo ? tp1->whoTo->flight_size : 0,
3519 (uintptr_t) tp1->whoTo,
3520 tp1->rec.data.TSN_seq);
3523 /* First setup this by shrinking flight */
3524 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3525 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3528 sctp_flight_size_decrease(tp1);
3529 sctp_total_flight_decrease(stcb, tp1);
3530 /* Now mark for resend */
3531 tp1->sent = SCTP_DATAGRAM_RESEND;
3532 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3534 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3535 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3536 tp1->whoTo->flight_size,
3538 (uintptr_t) tp1->whoTo,
3539 tp1->rec.data.TSN_seq);
3544 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3545 uint32_t rwnd, int *abort_now, int ecne_seen)
3547 struct sctp_nets *net;
3548 struct sctp_association *asoc;
3549 struct sctp_tmit_chunk *tp1, *tp2;
3551 int win_probe_recovery = 0;
3552 int win_probe_recovered = 0;
3553 int j, done_once = 0;
3556 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3557 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3558 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3560 SCTP_TCB_LOCK_ASSERT(stcb);
3561 #ifdef SCTP_ASOCLOG_OF_TSNS
3562 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3563 stcb->asoc.cumack_log_at++;
3564 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3565 stcb->asoc.cumack_log_at = 0;
3569 old_rwnd = asoc->peers_rwnd;
3570 if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
3573 } else if (asoc->last_acked_seq == cumack) {
3574 /* Window update sack */
3575 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3576 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3577 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3578 /* SWS sender side engages */
3579 asoc->peers_rwnd = 0;
3581 if (asoc->peers_rwnd > old_rwnd) {
3586 /* First setup for CC stuff */
3587 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3588 if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3589 /* Drag along the window_tsn for cwr's */
3590 net->cwr_window_tsn = cumack;
3592 net->prev_cwnd = net->cwnd;
3597 * CMT: Reset CUC and Fast recovery algo variables before
3600 net->new_pseudo_cumack = 0;
3601 net->will_exit_fast_recovery = 0;
3602 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3603 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3606 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3609 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3610 tp1 = TAILQ_LAST(&asoc->sent_queue,
3611 sctpchunk_listhead);
3612 send_s = tp1->rec.data.TSN_seq + 1;
3614 send_s = asoc->sending_seq;
3616 if (SCTP_TSN_GE(cumack, send_s)) {
3617 struct mbuf *op_err;
3618 char msg[SCTP_DIAG_INFO_LEN];
3622 snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x",
3624 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
3625 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3626 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3630 asoc->this_sack_highest_gap = cumack;
3631 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3632 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3633 stcb->asoc.overall_error_count,
3635 SCTP_FROM_SCTP_INDATA,
3638 stcb->asoc.overall_error_count = 0;
3639 if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3640 /* process the new consecutive TSN first */
3641 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3642 if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
3643 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3644 SCTP_PRINTF("Warning, an unsent is now acked?\n");
3646 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3648 * If it is less than ACKED, it is
3649 * now no-longer in flight. Higher
3650 * values may occur during marking
3652 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3653 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3654 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3655 tp1->whoTo->flight_size,
3657 (uintptr_t) tp1->whoTo,
3658 tp1->rec.data.TSN_seq);
3660 sctp_flight_size_decrease(tp1);
3661 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3662 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3665 /* sa_ignore NO_NULL_CHK */
3666 sctp_total_flight_decrease(stcb, tp1);
3668 tp1->whoTo->net_ack += tp1->send_size;
3669 if (tp1->snd_count < 2) {
3671 * True non-retransmited
3674 tp1->whoTo->net_ack2 +=
3677 /* update RTO too? */
3686 sctp_calculate_rto(stcb,
3688 &tp1->sent_rcv_time,
3689 sctp_align_safe_nocopy,
3690 SCTP_RTT_FROM_DATA);
3693 if (tp1->whoTo->rto_needed == 0) {
3694 tp1->whoTo->rto_needed = 1;
3700 * CMT: CUCv2 algorithm. From the
3701 * cumack'd TSNs, for each TSN being
3702 * acked for the first time, set the
3703 * following variables for the
3704 * corresp destination.
3705 * new_pseudo_cumack will trigger a
3707 * find_(rtx_)pseudo_cumack will
3708 * trigger search for the next
3709 * expected (rtx-)pseudo-cumack.
3711 tp1->whoTo->new_pseudo_cumack = 1;
3712 tp1->whoTo->find_pseudo_cumack = 1;
3713 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3715 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3716 /* sa_ignore NO_NULL_CHK */
3717 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
3720 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3721 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3723 if (tp1->rec.data.chunk_was_revoked) {
3724 /* deflate the cwnd */
3725 tp1->whoTo->cwnd -= tp1->book_size;
3726 tp1->rec.data.chunk_was_revoked = 0;
3728 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3729 if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
3730 asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
3733 panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
3737 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3739 /* sa_ignore NO_NULL_CHK */
3740 sctp_free_bufspace(stcb, asoc, tp1, 1);
3741 sctp_m_freem(tp1->data);
3744 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3745 sctp_log_sack(asoc->last_acked_seq,
3747 tp1->rec.data.TSN_seq,
3750 SCTP_LOG_FREE_SENT);
3752 asoc->sent_queue_cnt--;
3753 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
3760 /* sa_ignore NO_NULL_CHK */
3761 if (stcb->sctp_socket) {
3762 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3766 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3767 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
3768 /* sa_ignore NO_NULL_CHK */
3769 sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
3771 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3772 so = SCTP_INP_SO(stcb->sctp_ep);
3773 atomic_add_int(&stcb->asoc.refcnt, 1);
3774 SCTP_TCB_UNLOCK(stcb);
3775 SCTP_SOCKET_LOCK(so, 1);
3776 SCTP_TCB_LOCK(stcb);
3777 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3778 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3779 /* assoc was freed while we were unlocked */
3780 SCTP_SOCKET_UNLOCK(so, 1);
3784 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
3785 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3786 SCTP_SOCKET_UNLOCK(so, 1);
3789 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
3790 sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
3794 /* JRS - Use the congestion control given in the CC module */
3795 if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
3796 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3797 if (net->net_ack2 > 0) {
3799 * Karn's rule applies to clearing error
3800 * count, this is optional.
3802 net->error_count = 0;
3803 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
3804 /* addr came good */
3805 net->dest_state |= SCTP_ADDR_REACHABLE;
3806 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
3807 0, (void *)net, SCTP_SO_NOT_LOCKED);
3809 if (net == stcb->asoc.primary_destination) {
3810 if (stcb->asoc.alternate) {
3812 * release the alternate,
3815 sctp_free_remote_addr(stcb->asoc.alternate);
3816 stcb->asoc.alternate = NULL;
3819 if (net->dest_state & SCTP_ADDR_PF) {
3820 net->dest_state &= ~SCTP_ADDR_PF;
3821 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
3822 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
3823 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
3824 /* Done with this net */
3827 /* restore any doubled timers */
3828 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3829 if (net->RTO < stcb->asoc.minrto) {
3830 net->RTO = stcb->asoc.minrto;
3832 if (net->RTO > stcb->asoc.maxrto) {
3833 net->RTO = stcb->asoc.maxrto;
3837 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
3839 asoc->last_acked_seq = cumack;
3841 if (TAILQ_EMPTY(&asoc->sent_queue)) {
3842 /* nothing left in-flight */
3843 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3844 net->flight_size = 0;
3845 net->partial_bytes_acked = 0;
3847 asoc->total_flight = 0;
3848 asoc->total_flight_count = 0;
3851 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3852 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3853 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3854 /* SWS sender side engages */
3855 asoc->peers_rwnd = 0;
3857 if (asoc->peers_rwnd > old_rwnd) {
3858 win_probe_recovery = 1;
3860 /* Now assure a timer where data is queued at */
3863 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3866 if (win_probe_recovery && (net->window_probe)) {
3867 win_probe_recovered = 1;
3869 * Find first chunk that was used with window probe
3870 * and clear the sent
3872 /* sa_ignore FREED_MEMORY */
3873 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3874 if (tp1->window_probe) {
3875 /* move back to data send queue */
3876 sctp_window_probe_recovery(stcb, asoc, tp1);
3881 if (net->RTO == 0) {
3882 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
3884 to_ticks = MSEC_TO_TICKS(net->RTO);
3886 if (net->flight_size) {
3888 (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
3889 sctp_timeout_handler, &net->rxt_timer);
3890 if (net->window_probe) {
3891 net->window_probe = 0;
3894 if (net->window_probe) {
3896 * In window probes we must assure a timer
3897 * is still running there
3899 net->window_probe = 0;
3900 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3901 SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
3902 sctp_timeout_handler, &net->rxt_timer);
3904 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3905 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3907 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
3912 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
3913 (asoc->sent_queue_retran_cnt == 0) &&
3914 (win_probe_recovered == 0) &&
3917 * huh, this should not happen unless all packets are
3918 * PR-SCTP and marked to skip of course.
3920 if (sctp_fs_audit(asoc)) {
3921 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3922 net->flight_size = 0;
3924 asoc->total_flight = 0;
3925 asoc->total_flight_count = 0;
3926 asoc->sent_queue_retran_cnt = 0;
3927 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3928 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3929 sctp_flight_size_increase(tp1);
3930 sctp_total_flight_increase(stcb, tp1);
3931 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3932 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3939 /**********************************/
3940 /* Now what about shutdown issues */
3941 /**********************************/
3942 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
3943 /* nothing left on sendqueue.. consider done */
3945 if ((asoc->stream_queue_cnt == 1) &&
3946 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
3947 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
3948 (asoc->locked_on_sending)
3950 struct sctp_stream_queue_pending *sp;
3953 * I may be in a state where we got all across.. but
3954 * cannot write more due to a shutdown... we abort
3955 * since the user did not indicate EOR in this case.
3956 * The sp will be cleaned during free of the asoc.
3958 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
3960 if ((sp) && (sp->length == 0)) {
3961 /* Let cleanup code purge it */
3962 if (sp->msg_is_complete) {
3963 asoc->stream_queue_cnt--;
3965 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3966 asoc->locked_on_sending = NULL;
3967 asoc->stream_queue_cnt--;
3971 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
3972 (asoc->stream_queue_cnt == 0)) {
3973 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
3974 /* Need to abort here */
3975 struct mbuf *op_err;
3980 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3981 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
3982 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3984 struct sctp_nets *netp;
3986 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3987 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3988 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3990 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
3991 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
3992 sctp_stop_timers_for_shutdown(stcb);
3993 if (asoc->alternate) {
3994 netp = asoc->alternate;
3996 netp = asoc->primary_destination;
3998 sctp_send_shutdown(stcb, netp);
3999 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4000 stcb->sctp_ep, stcb, netp);
4001 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4002 stcb->sctp_ep, stcb, netp);
4004 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4005 (asoc->stream_queue_cnt == 0)) {
4006 struct sctp_nets *netp;
4008 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4011 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4012 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4013 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4014 sctp_stop_timers_for_shutdown(stcb);
4015 if (asoc->alternate) {
4016 netp = asoc->alternate;
4018 netp = asoc->primary_destination;
4020 sctp_send_shutdown_ack(stcb, netp);
4021 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4022 stcb->sctp_ep, stcb, netp);
4025 /*********************************************/
4026 /* Here we perform PR-SCTP procedures */
4028 /*********************************************/
4029 /* C1. update advancedPeerAckPoint */
4030 if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4031 asoc->advanced_peer_ack_point = cumack;
4033 /* PR-Sctp issues need to be addressed too */
4034 if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
4035 struct sctp_tmit_chunk *lchk;
4036 uint32_t old_adv_peer_ack_point;
4038 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4039 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4040 /* C3. See if we need to send a Fwd-TSN */
4041 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4043 * ISSUE with ECN, see FWD-TSN processing.
4045 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4046 send_forward_tsn(stcb, asoc);
4048 /* try to FR fwd-tsn's that get lost too */
4049 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
4050 send_forward_tsn(stcb, asoc);
4055 /* Assure a timer is up */
4056 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4057 stcb->sctp_ep, stcb, lchk->whoTo);
4060 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4061 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4063 stcb->asoc.peers_rwnd,
4064 stcb->asoc.total_flight,
4065 stcb->asoc.total_output_queue_size);
4070 sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
4071 struct sctp_tcb *stcb,
4072 uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4073 int *abort_now, uint8_t flags,
4074 uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4076 struct sctp_association *asoc;
4077 struct sctp_tmit_chunk *tp1, *tp2;
4078 uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4079 uint16_t wake_him = 0;
4080 uint32_t send_s = 0;
4082 int accum_moved = 0;
4083 int will_exit_fast_recovery = 0;
4084 uint32_t a_rwnd, old_rwnd;
4085 int win_probe_recovery = 0;
4086 int win_probe_recovered = 0;
4087 struct sctp_nets *net = NULL;
4090 uint8_t reneged_all = 0;
4091 uint8_t cmt_dac_flag;
4094 * we take any chance we can to service our queues since we cannot
4095 * get awoken when the socket is read from :<
4098 * Now perform the actual SACK handling: 1) Verify that it is not an
4099 * old sack, if so discard. 2) If there is nothing left in the send
4100 * queue (cum-ack is equal to last acked) then you have a duplicate
4101 * too, update any rwnd change and verify no timers are running.
4102 * then return. 3) Process any new consequtive data i.e. cum-ack
4103 * moved process these first and note that it moved. 4) Process any
4104 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4105 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4106 * sync up flightsizes and things, stop all timers and also check
4107 * for shutdown_pending state. If so then go ahead and send off the
4108 * shutdown. If in shutdown recv, send off the shutdown-ack and
4109 * start that timer, Ret. 9) Strike any non-acked things and do FR
4110 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4111 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4112 * if in shutdown_recv state.
4114 SCTP_TCB_LOCK_ASSERT(stcb);
4116 this_sack_lowest_newack = 0;
4117 SCTP_STAT_INCR(sctps_slowpath_sack);
4119 cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
4120 #ifdef SCTP_ASOCLOG_OF_TSNS
4121 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4122 stcb->asoc.cumack_log_at++;
4123 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4124 stcb->asoc.cumack_log_at = 0;
4129 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4130 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4131 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4133 old_rwnd = stcb->asoc.peers_rwnd;
4134 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4135 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4136 stcb->asoc.overall_error_count,
4138 SCTP_FROM_SCTP_INDATA,
4141 stcb->asoc.overall_error_count = 0;
4143 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4144 sctp_log_sack(asoc->last_acked_seq,
4151 if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4153 uint32_t *dupdata, dblock;
4155 for (i = 0; i < num_dup; i++) {
4156 dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4157 sizeof(uint32_t), (uint8_t *) & dblock);
4158 if (dupdata == NULL) {
4161 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4164 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4166 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4167 tp1 = TAILQ_LAST(&asoc->sent_queue,
4168 sctpchunk_listhead);
4169 send_s = tp1->rec.data.TSN_seq + 1;
4172 send_s = asoc->sending_seq;
4174 if (SCTP_TSN_GE(cum_ack, send_s)) {
4175 struct mbuf *op_err;
4176 char msg[SCTP_DIAG_INFO_LEN];
4179 * no way, we have not even sent this TSN out yet.
4180 * Peer is hopelessly messed up with us.
4182 SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4185 SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n",
4186 tp1->rec.data.TSN_seq, (void *)tp1);
4191 snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x",
4193 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4194 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4195 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4199 /**********************/
4200 /* 1) check the range */
4201 /**********************/
4202 if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4203 /* acking something behind */
4206 /* update the Rwnd of the peer */
4207 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4208 TAILQ_EMPTY(&asoc->send_queue) &&
4209 (asoc->stream_queue_cnt == 0)) {
4210 /* nothing left on send/sent and strmq */
4211 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4212 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4213 asoc->peers_rwnd, 0, 0, a_rwnd);
4215 asoc->peers_rwnd = a_rwnd;
4216 if (asoc->sent_queue_retran_cnt) {
4217 asoc->sent_queue_retran_cnt = 0;
4219 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4220 /* SWS sender side engages */
4221 asoc->peers_rwnd = 0;
4223 /* stop any timers */
4224 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4225 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4226 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4227 net->partial_bytes_acked = 0;
4228 net->flight_size = 0;
4230 asoc->total_flight = 0;
4231 asoc->total_flight_count = 0;
4235 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4236 * things. The total byte count acked is tracked in netAckSz AND
4237 * netAck2 is used to track the total bytes acked that are un-
4238 * amibguious and were never retransmitted. We track these on a per
4239 * destination address basis.
4241 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4242 if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4243 /* Drag along the window_tsn for cwr's */
4244 net->cwr_window_tsn = cum_ack;
4246 net->prev_cwnd = net->cwnd;
4251 * CMT: Reset CUC and Fast recovery algo variables before
4254 net->new_pseudo_cumack = 0;
4255 net->will_exit_fast_recovery = 0;
4256 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4257 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4260 /* process the new consecutive TSN first */
4261 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4262 if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4263 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4265 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4267 * If it is less than ACKED, it is
4268 * now no-longer in flight. Higher
4269 * values may occur during marking
4271 if ((tp1->whoTo->dest_state &
4272 SCTP_ADDR_UNCONFIRMED) &&
4273 (tp1->snd_count < 2)) {
4275 * If there was no retran
4276 * and the address is
4277 * un-confirmed and we sent
4279 * sacked.. its confirmed,
4282 tp1->whoTo->dest_state &=
4283 ~SCTP_ADDR_UNCONFIRMED;
4285 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4286 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4287 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4288 tp1->whoTo->flight_size,
4290 (uintptr_t) tp1->whoTo,
4291 tp1->rec.data.TSN_seq);
4293 sctp_flight_size_decrease(tp1);
4294 sctp_total_flight_decrease(stcb, tp1);
4295 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4296 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4300 tp1->whoTo->net_ack += tp1->send_size;
4302 /* CMT SFR and DAC algos */
4303 this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4304 tp1->whoTo->saw_newack = 1;
4306 if (tp1->snd_count < 2) {
4308 * True non-retransmited
4311 tp1->whoTo->net_ack2 +=
4314 /* update RTO too? */
4318 sctp_calculate_rto(stcb,
4320 &tp1->sent_rcv_time,
4321 sctp_align_safe_nocopy,
4322 SCTP_RTT_FROM_DATA);
4325 if (tp1->whoTo->rto_needed == 0) {
4326 tp1->whoTo->rto_needed = 1;
4332 * CMT: CUCv2 algorithm. From the
4333 * cumack'd TSNs, for each TSN being
4334 * acked for the first time, set the
4335 * following variables for the
4336 * corresp destination.
4337 * new_pseudo_cumack will trigger a
4339 * find_(rtx_)pseudo_cumack will
4340 * trigger search for the next
4341 * expected (rtx-)pseudo-cumack.
4343 tp1->whoTo->new_pseudo_cumack = 1;
4344 tp1->whoTo->find_pseudo_cumack = 1;
4345 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4348 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4349 sctp_log_sack(asoc->last_acked_seq,
4351 tp1->rec.data.TSN_seq,
4354 SCTP_LOG_TSN_ACKED);
4356 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4357 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4360 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4361 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4362 #ifdef SCTP_AUDITING_ENABLED
4363 sctp_audit_log(0xB3,
4364 (asoc->sent_queue_retran_cnt & 0x000000ff));
4367 if (tp1->rec.data.chunk_was_revoked) {
4368 /* deflate the cwnd */
4369 tp1->whoTo->cwnd -= tp1->book_size;
4370 tp1->rec.data.chunk_was_revoked = 0;
4372 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4373 tp1->sent = SCTP_DATAGRAM_ACKED;
4380 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4381 /* always set this up to cum-ack */
4382 asoc->this_sack_highest_gap = last_tsn;
4384 if ((num_seg > 0) || (num_nr_seg > 0)) {
4387 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4388 * to be greater than the cumack. Also reset saw_newack to 0
4391 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4392 net->saw_newack = 0;
4393 net->this_sack_highest_newack = last_tsn;
4397 * thisSackHighestGap will increase while handling NEW
4398 * segments this_sack_highest_newack will increase while
4399 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4400 * used for CMT DAC algo. saw_newack will also change.
4402 if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4403 &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4404 num_seg, num_nr_seg, &rto_ok)) {
4407 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4409 * validate the biggest_tsn_acked in the gap acks if
4410 * strict adherence is wanted.
4412 if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4414 * peer is either confused or we are under
4415 * attack. We must abort.
4417 SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4418 biggest_tsn_acked, send_s);
4423 /*******************************************/
4424 /* cancel ALL T3-send timer if accum moved */
4425 /*******************************************/
4426 if (asoc->sctp_cmt_on_off > 0) {
4427 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4428 if (net->new_pseudo_cumack)
4429 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4431 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4436 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4437 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4438 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4442 /********************************************/
4443 /* drop the acked chunks from the sentqueue */
4444 /********************************************/
4445 asoc->last_acked_seq = cum_ack;
4447 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
4448 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4451 if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4452 if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
4453 asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
4456 panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
4460 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4461 if (PR_SCTP_ENABLED(tp1->flags)) {
4462 if (asoc->pr_sctp_cnt != 0)
4463 asoc->pr_sctp_cnt--;
4465 asoc->sent_queue_cnt--;
4467 /* sa_ignore NO_NULL_CHK */
4468 sctp_free_bufspace(stcb, asoc, tp1, 1);
4469 sctp_m_freem(tp1->data);
4471 if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4472 asoc->sent_queue_cnt_removeable--;
4475 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4476 sctp_log_sack(asoc->last_acked_seq,
4478 tp1->rec.data.TSN_seq,
4481 SCTP_LOG_FREE_SENT);
4483 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4486 if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
4488 panic("Warning flight size is postive and should be 0");
4490 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4491 asoc->total_flight);
4493 asoc->total_flight = 0;
4495 /* sa_ignore NO_NULL_CHK */
4496 if ((wake_him) && (stcb->sctp_socket)) {
4497 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4501 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4502 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4503 sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
4505 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4506 so = SCTP_INP_SO(stcb->sctp_ep);
4507 atomic_add_int(&stcb->asoc.refcnt, 1);
4508 SCTP_TCB_UNLOCK(stcb);
4509 SCTP_SOCKET_LOCK(so, 1);
4510 SCTP_TCB_LOCK(stcb);
4511 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4512 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4513 /* assoc was freed while we were unlocked */
4514 SCTP_SOCKET_UNLOCK(so, 1);
4518 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4519 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4520 SCTP_SOCKET_UNLOCK(so, 1);
4523 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4524 sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
4528 if (asoc->fast_retran_loss_recovery && accum_moved) {
4529 if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4530 /* Setup so we will exit RFC2582 fast recovery */
4531 will_exit_fast_recovery = 1;
4535 * Check for revoked fragments:
4537 * if Previous sack - Had no frags then we can't have any revoked if
4538 * Previous sack - Had frag's then - If we now have frags aka
4539 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4540 * some of them. else - The peer revoked all ACKED fragments, since
4541 * we had some before and now we have NONE.
4545 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4546 asoc->saw_sack_with_frags = 1;
4547 } else if (asoc->saw_sack_with_frags) {
4548 int cnt_revoked = 0;
4550 /* Peer revoked all dg's marked or acked */
4551 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4552 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4553 tp1->sent = SCTP_DATAGRAM_SENT;
4554 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4555 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4556 tp1->whoTo->flight_size,
4558 (uintptr_t) tp1->whoTo,
4559 tp1->rec.data.TSN_seq);
4561 sctp_flight_size_increase(tp1);
4562 sctp_total_flight_increase(stcb, tp1);
4563 tp1->rec.data.chunk_was_revoked = 1;
4565 * To ensure that this increase in
4566 * flightsize, which is artificial, does not
4567 * throttle the sender, we also increase the
4568 * cwnd artificially.
4570 tp1->whoTo->cwnd += tp1->book_size;
4577 asoc->saw_sack_with_frags = 0;
4580 asoc->saw_sack_with_nr_frags = 1;
4582 asoc->saw_sack_with_nr_frags = 0;
4584 /* JRS - Use the congestion control given in the CC module */
4585 if (ecne_seen == 0) {
4586 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4587 if (net->net_ack2 > 0) {
4589 * Karn's rule applies to clearing error
4590 * count, this is optional.
4592 net->error_count = 0;
4593 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4594 /* addr came good */
4595 net->dest_state |= SCTP_ADDR_REACHABLE;
4596 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4597 0, (void *)net, SCTP_SO_NOT_LOCKED);
4599 if (net == stcb->asoc.primary_destination) {
4600 if (stcb->asoc.alternate) {
4602 * release the alternate,
4605 sctp_free_remote_addr(stcb->asoc.alternate);
4606 stcb->asoc.alternate = NULL;
4609 if (net->dest_state & SCTP_ADDR_PF) {
4610 net->dest_state &= ~SCTP_ADDR_PF;
4611 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4612 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4613 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4614 /* Done with this net */
4617 /* restore any doubled timers */
4618 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4619 if (net->RTO < stcb->asoc.minrto) {
4620 net->RTO = stcb->asoc.minrto;
4622 if (net->RTO > stcb->asoc.maxrto) {
4623 net->RTO = stcb->asoc.maxrto;
4627 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4629 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4630 /* nothing left in-flight */
4631 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4632 /* stop all timers */
4633 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4634 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4635 net->flight_size = 0;
4636 net->partial_bytes_acked = 0;
4638 asoc->total_flight = 0;
4639 asoc->total_flight_count = 0;
4641 /**********************************/
4642 /* Now what about shutdown issues */
4643 /**********************************/
4644 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4645 /* nothing left on sendqueue.. consider done */
4646 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4647 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4648 asoc->peers_rwnd, 0, 0, a_rwnd);
4650 asoc->peers_rwnd = a_rwnd;
4651 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4652 /* SWS sender side engages */
4653 asoc->peers_rwnd = 0;
4656 if ((asoc->stream_queue_cnt == 1) &&
4657 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4658 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4659 (asoc->locked_on_sending)
4661 struct sctp_stream_queue_pending *sp;
4664 * I may be in a state where we got all across.. but
4665 * cannot write more due to a shutdown... we abort
4666 * since the user did not indicate EOR in this case.
4668 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4670 if ((sp) && (sp->length == 0)) {
4671 asoc->locked_on_sending = NULL;
4672 if (sp->msg_is_complete) {
4673 asoc->stream_queue_cnt--;
4675 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4676 asoc->stream_queue_cnt--;
4680 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4681 (asoc->stream_queue_cnt == 0)) {
4682 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4683 /* Need to abort here */
4684 struct mbuf *op_err;
4689 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4690 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4691 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4694 struct sctp_nets *netp;
4696 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4697 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4698 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4700 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4701 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4702 sctp_stop_timers_for_shutdown(stcb);
4703 if (asoc->alternate) {
4704 netp = asoc->alternate;
4706 netp = asoc->primary_destination;
4708 sctp_send_shutdown(stcb, netp);
4709 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4710 stcb->sctp_ep, stcb, netp);
4711 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4712 stcb->sctp_ep, stcb, netp);
4715 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4716 (asoc->stream_queue_cnt == 0)) {
4717 struct sctp_nets *netp;
4719 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4722 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4723 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4724 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4725 sctp_stop_timers_for_shutdown(stcb);
4726 if (asoc->alternate) {
4727 netp = asoc->alternate;
4729 netp = asoc->primary_destination;
4731 sctp_send_shutdown_ack(stcb, netp);
4732 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4733 stcb->sctp_ep, stcb, netp);
4738 * Now here we are going to recycle net_ack for a different use...
4741 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4746 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4747 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4748 * automatically ensure that.
4750 if ((asoc->sctp_cmt_on_off > 0) &&
4751 SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
4752 (cmt_dac_flag == 0)) {
4753 this_sack_lowest_newack = cum_ack;
4755 if ((num_seg > 0) || (num_nr_seg > 0)) {
4756 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4757 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4759 /* JRS - Use the congestion control given in the CC module */
4760 asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4762 /* Now are we exiting loss recovery ? */
4763 if (will_exit_fast_recovery) {
4764 /* Ok, we must exit fast recovery */
4765 asoc->fast_retran_loss_recovery = 0;
4767 if ((asoc->sat_t3_loss_recovery) &&
4768 SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4769 /* end satellite t3 loss recovery */
4770 asoc->sat_t3_loss_recovery = 0;
4775 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4776 if (net->will_exit_fast_recovery) {
4777 /* Ok, we must exit fast recovery */
4778 net->fast_retran_loss_recovery = 0;
4782 /* Adjust and set the new rwnd value */
4783 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4784 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4785 asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
4787 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
4788 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4789 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4790 /* SWS sender side engages */
4791 asoc->peers_rwnd = 0;
4793 if (asoc->peers_rwnd > old_rwnd) {
4794 win_probe_recovery = 1;
4797 * Now we must setup so we have a timer up for anyone with
4803 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4804 if (win_probe_recovery && (net->window_probe)) {
4805 win_probe_recovered = 1;
4807 * Find first chunk that was used with
4808 * window probe and clear the event. Put
4809 * it back into the send queue as if has
4812 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4813 if (tp1->window_probe) {
4814 sctp_window_probe_recovery(stcb, asoc, tp1);
4819 if (net->flight_size) {
4821 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4822 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4823 stcb->sctp_ep, stcb, net);
4825 if (net->window_probe) {
4826 net->window_probe = 0;
4829 if (net->window_probe) {
4831 * In window probes we must assure a timer
4832 * is still running there
4834 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4835 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4836 stcb->sctp_ep, stcb, net);
4839 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4840 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4842 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4847 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4848 (asoc->sent_queue_retran_cnt == 0) &&
4849 (win_probe_recovered == 0) &&
4852 * huh, this should not happen unless all packets are
4853 * PR-SCTP and marked to skip of course.
4855 if (sctp_fs_audit(asoc)) {
4856 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4857 net->flight_size = 0;
4859 asoc->total_flight = 0;
4860 asoc->total_flight_count = 0;
4861 asoc->sent_queue_retran_cnt = 0;
4862 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4863 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4864 sctp_flight_size_increase(tp1);
4865 sctp_total_flight_increase(stcb, tp1);
4866 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4867 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4874 /*********************************************/
4875 /* Here we perform PR-SCTP procedures */
4877 /*********************************************/
4878 /* C1. update advancedPeerAckPoint */
4879 if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
4880 asoc->advanced_peer_ack_point = cum_ack;
4882 /* C2. try to further move advancedPeerAckPoint ahead */
4883 if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
4884 struct sctp_tmit_chunk *lchk;
4885 uint32_t old_adv_peer_ack_point;
4887 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4888 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4889 /* C3. See if we need to send a Fwd-TSN */
4890 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
4892 * ISSUE with ECN, see FWD-TSN processing.
4894 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
4895 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
4896 0xee, cum_ack, asoc->advanced_peer_ack_point,
4897 old_adv_peer_ack_point);
4899 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4900 send_forward_tsn(stcb, asoc);
4902 /* try to FR fwd-tsn's that get lost too */
4903 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
4904 send_forward_tsn(stcb, asoc);
4909 /* Assure a timer is up */
4910 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4911 stcb->sctp_ep, stcb, lchk->whoTo);
4914 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4915 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4917 stcb->asoc.peers_rwnd,
4918 stcb->asoc.total_flight,
4919 stcb->asoc.total_output_queue_size);
4924 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
4927 uint32_t cum_ack, a_rwnd;
4929 cum_ack = ntohl(cp->cumulative_tsn_ack);
4930 /* Arrange so a_rwnd does NOT change */
4931 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
4933 /* Now call the express sack handling */
4934 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
4938 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
4939 struct sctp_stream_in *strmin)
4941 struct sctp_queued_to_read *ctl, *nctl;
4942 struct sctp_association *asoc;
4946 tt = strmin->last_sequence_delivered;
4948 * First deliver anything prior to and including the stream no that
4951 TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
4952 if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
4953 /* this is deliverable now */
4954 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4955 /* subtract pending on streams */
4956 asoc->size_on_all_streams -= ctl->length;
4957 sctp_ucount_decr(asoc->cnt_on_all_streams);
4958 /* deliver it to at least the delivery-q */
4959 if (stcb->sctp_socket) {
4960 sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4961 sctp_add_to_readq(stcb->sctp_ep, stcb,
4963 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
4966 /* no more delivery now. */
4971 * now we must deliver things in queue the normal way if any are
4974 tt = strmin->last_sequence_delivered + 1;
4975 TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
4976 if (tt == ctl->sinfo_ssn) {
4977 /* this is deliverable now */
4978 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4979 /* subtract pending on streams */
4980 asoc->size_on_all_streams -= ctl->length;
4981 sctp_ucount_decr(asoc->cnt_on_all_streams);
4982 /* deliver it to at least the delivery-q */
4983 strmin->last_sequence_delivered = ctl->sinfo_ssn;
4984 if (stcb->sctp_socket) {
4985 sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4986 sctp_add_to_readq(stcb->sctp_ep, stcb,
4988 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
4991 tt = strmin->last_sequence_delivered + 1;
4999 sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
5000 struct sctp_association *asoc,
5001 uint16_t stream, uint16_t seq)
5003 struct sctp_tmit_chunk *chk, *nchk;
5005 /* For each one on here see if we need to toss it */
5007 * For now large messages held on the reasmqueue that are complete
5008 * will be tossed too. We could in theory do more work to spin
5009 * through and stop after dumping one msg aka seeing the start of a
5010 * new msg at the head, and call the delivery function... to see if
5011 * it can be delivered... But for now we just dump everything on the
5014 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5016 * Do not toss it if on a different stream or marked for
5017 * unordered delivery in which case the stream sequence
5018 * number has no meaning.
5020 if ((chk->rec.data.stream_number != stream) ||
5021 ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
5024 if (chk->rec.data.stream_seq == seq) {
5025 /* It needs to be tossed */
5026 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5027 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
5028 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
5029 asoc->str_of_pdapi = chk->rec.data.stream_number;
5030 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
5031 asoc->fragment_flags = chk->rec.data.rcv_flags;
5033 asoc->size_on_reasm_queue -= chk->send_size;
5034 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5036 /* Clear up any stream problem */
5037 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
5038 SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5040 * We must dump forward this streams
5041 * sequence number if the chunk is not
5042 * unordered that is being skipped. There is
5043 * a chance that if the peer does not
5044 * include the last fragment in its FWD-TSN
5045 * we WILL have a problem here since you
5046 * would have a partial chunk in queue that
5047 * may not be deliverable. Also if a Partial
5048 * delivery API as started the user may get
5049 * a partial chunk. The next read returning
5050 * a new chunk... really ugly but I see no
5051 * way around it! Maybe a notify??
5053 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5056 sctp_m_freem(chk->data);
5059 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5060 } else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
5062 * If the stream_seq is > than the purging one, we
5072 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5073 struct sctp_forward_tsn_chunk *fwd,
5074 int *abort_flag, struct mbuf *m, int offset)
5076 /* The pr-sctp fwd tsn */
5078 * here we will perform all the data receiver side steps for
5079 * processing FwdTSN, as required in by pr-sctp draft:
5081 * Assume we get FwdTSN(x):
5083 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5084 * others we have 3) examine and update re-ordering queue on
5085 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5086 * report where we are.
5088 struct sctp_association *asoc;
5089 uint32_t new_cum_tsn, gap;
5090 unsigned int i, fwd_sz, m_size;
5092 struct sctp_stream_in *strm;
5093 struct sctp_tmit_chunk *chk, *nchk;
5094 struct sctp_queued_to_read *ctl, *sv;
5097 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5098 SCTPDBG(SCTP_DEBUG_INDATA1,
5099 "Bad size too small/big fwd-tsn\n");
5102 m_size = (stcb->asoc.mapping_array_size << 3);
5103 /*************************************************************/
5104 /* 1. Here we update local cumTSN and shift the bitmap array */
5105 /*************************************************************/
5106 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5108 if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5109 /* Already got there ... */
5113 * now we know the new TSN is more advanced, let's find the actual
5116 SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
5117 asoc->cumulative_tsn = new_cum_tsn;
5118 if (gap >= m_size) {
5119 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5120 struct mbuf *op_err;
5121 char msg[SCTP_DIAG_INFO_LEN];
5124 * out of range (of single byte chunks in the rwnd I
5125 * give out). This must be an attacker.
5128 snprintf(msg, sizeof(msg),
5129 "New cum ack %8.8x too high, highest TSN %8.8x",
5130 new_cum_tsn, asoc->highest_tsn_inside_map);
5131 op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
5132 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5133 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5136 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5138 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5139 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5140 asoc->highest_tsn_inside_map = new_cum_tsn;
5142 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5143 asoc->highest_tsn_inside_nr_map = new_cum_tsn;
5145 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5146 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5149 SCTP_TCB_LOCK_ASSERT(stcb);
5150 for (i = 0; i <= gap; i++) {
5151 if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
5152 !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
5153 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
5154 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
5155 asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5160 /*************************************************************/
5161 /* 2. Clear up re-assembly queue */
5162 /*************************************************************/
5164 * First service it if pd-api is up, just in case we can progress it
5167 if (asoc->fragmented_delivery_inprogress) {
5168 sctp_service_reassembly(stcb, asoc);
5170 /* For each one on here see if we need to toss it */
5172 * For now large messages held on the reasmqueue that are complete
5173 * will be tossed too. We could in theory do more work to spin
5174 * through and stop after dumping one msg aka seeing the start of a
5175 * new msg at the head, and call the delivery function... to see if
5176 * it can be delivered... But for now we just dump everything on the
5179 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5180 if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5181 /* It needs to be tossed */
5182 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5183 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
5184 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
5185 asoc->str_of_pdapi = chk->rec.data.stream_number;
5186 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
5187 asoc->fragment_flags = chk->rec.data.rcv_flags;
5189 asoc->size_on_reasm_queue -= chk->send_size;
5190 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5192 /* Clear up any stream problem */
5193 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
5194 SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5196 * We must dump forward this streams
5197 * sequence number if the chunk is not
5198 * unordered that is being skipped. There is
5199 * a chance that if the peer does not
5200 * include the last fragment in its FWD-TSN
5201 * we WILL have a problem here since you
5202 * would have a partial chunk in queue that
5203 * may not be deliverable. Also if a Partial
5204 * delivery API as started the user may get
5205 * a partial chunk. The next read returning
5206 * a new chunk... really ugly but I see no
5207 * way around it! Maybe a notify??
5209 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5212 sctp_m_freem(chk->data);
5215 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5218 * Ok we have gone beyond the end of the fwd-tsn's
5224 /*******************************************************/
5225 /* 3. Update the PR-stream re-ordering queues and fix */
5226 /* delivery issues as needed. */
5227 /*******************************************************/
5228 fwd_sz -= sizeof(*fwd);
5231 unsigned int num_str;
5232 struct sctp_strseq *stseq, strseqbuf;
5234 offset += sizeof(*fwd);
5236 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5237 num_str = fwd_sz / sizeof(struct sctp_strseq);
5238 for (i = 0; i < num_str; i++) {
5241 stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5242 sizeof(struct sctp_strseq),
5243 (uint8_t *) & strseqbuf);
5244 offset += sizeof(struct sctp_strseq);
5245 if (stseq == NULL) {
5249 st = ntohs(stseq->stream);
5251 st = ntohs(stseq->sequence);
5252 stseq->sequence = st;
5257 * Ok we now look for the stream/seq on the read
5258 * queue where its not all delivered. If we find it
5259 * we transmute the read entry into a PDI_ABORTED.
5261 if (stseq->stream >= asoc->streamincnt) {
5262 /* screwed up streams, stop! */
5265 if ((asoc->str_of_pdapi == stseq->stream) &&
5266 (asoc->ssn_of_pdapi == stseq->sequence)) {
5268 * If this is the one we were partially
5269 * delivering now then we no longer are.
5270 * Note this will change with the reassembly
5273 asoc->fragmented_delivery_inprogress = 0;
5275 sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
5276 TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
5277 if ((ctl->sinfo_stream == stseq->stream) &&
5278 (ctl->sinfo_ssn == stseq->sequence)) {
5279 str_seq = (stseq->stream << 16) | stseq->sequence;
5281 ctl->pdapi_aborted = 1;
5282 sv = stcb->asoc.control_pdapi;
5283 stcb->asoc.control_pdapi = ctl;
5284 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5286 SCTP_PARTIAL_DELIVERY_ABORTED,
5288 SCTP_SO_NOT_LOCKED);
5289 stcb->asoc.control_pdapi = sv;
5291 } else if ((ctl->sinfo_stream == stseq->stream) &&
5292 SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
5293 /* We are past our victim SSN */
5297 strm = &asoc->strmin[stseq->stream];
5298 if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5299 /* Update the sequence number */
5300 strm->last_sequence_delivered = stseq->sequence;
5302 /* now kick the stream the new way */
5303 /* sa_ignore NO_NULL_CHK */
5304 sctp_kick_prsctp_reorder_queue(stcb, strm);
5306 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5309 * Now slide thing forward.
5311 sctp_slide_mapping_arrays(stcb);
5313 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5314 /* now lets kick out and check for more fragmented delivery */
5315 /* sa_ignore NO_NULL_CHK */
5316 sctp_deliver_reasm_check(stcb, &stcb->asoc);