2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $ */
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 uint32_t calc, calc_save;
65 * This is really set wrong with respect to a 1-2-m socket. Since
66 * the sb_cc is the count that everyone as put up. When we re-write
67 * sctp_soreceive then we will fix this so that ONLY this
68 * associations data is taken into account.
70 if (stcb->sctp_socket == NULL)
73 if (stcb->asoc.sb_cc == 0 &&
74 asoc->size_on_reasm_queue == 0 &&
75 asoc->size_on_all_streams == 0) {
76 /* Full rwnd granted */
77 asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
81 /* get actual space */
82 calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
85 * take out what has NOT been put on socket queue and we yet hold
88 calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
89 calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
96 /* what is the overhead of all these rwnd's */
98 calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
101 asoc->my_rwnd = calc;
102 if ((asoc->my_rwnd == 0) &&
103 (calc < stcb->asoc.my_rwnd_control_len)) {
105 * If our rwnd == 0 && the overhead is greater than the
106 * data onqueue, we clamp the rwnd to 1. This lets us
107 * still accept inbound segments, but hopefully will shut
108 * the sender down when he finally gets the message. This
109 * hopefully will gracefully avoid discarding packets.
114 (asoc->my_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
115 /* SWS engaged, tell peer none left */
120 /* Calculate what the rwnd would be */
122 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
124 uint32_t calc = 0, calc_save = 0, result = 0;
127 * This is really set wrong with respect to a 1-2-m socket. Since
128 * the sb_cc is the count that everyone as put up. When we re-write
129 * sctp_soreceive then we will fix this so that ONLY this
130 * associations data is taken into account.
132 if (stcb->sctp_socket == NULL)
135 if (stcb->asoc.sb_cc == 0 &&
136 asoc->size_on_reasm_queue == 0 &&
137 asoc->size_on_all_streams == 0) {
138 /* Full rwnd granted */
139 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
143 /* get actual space */
144 calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
147 * take out what has NOT been put on socket queue and we yet hold
150 calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
151 calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
157 /* what is the overhead of all these rwnd's */
158 calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
163 (calc < stcb->asoc.my_rwnd_control_len)) {
165 * If our rwnd == 0 && the overhead is greater than the
166 * data onqueue, we clamp the rwnd to 1. This lets us
167 * still accept inbound segments, but hopefully will shut
168 * the sender down when he finally gets the message. This
169 * hopefully will gracefully avoid discarding packets.
174 (result < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
175 /* SWS engaged, tell peer none left */
184 * Build out our readq entry based on the incoming packet.
186 struct sctp_queued_to_read *
187 sctp_build_readq_entry(struct sctp_tcb *stcb,
188 struct sctp_nets *net,
189 uint32_t tsn, uint32_t ppid,
190 uint32_t context, uint16_t stream_no,
191 uint16_t stream_seq, uint8_t flags,
194 struct sctp_queued_to_read *read_queue_e = NULL;
196 sctp_alloc_a_readq(stcb, read_queue_e);
197 if (read_queue_e == NULL) {
200 read_queue_e->sinfo_stream = stream_no;
201 read_queue_e->sinfo_ssn = stream_seq;
202 read_queue_e->sinfo_flags = (flags << 8);
203 read_queue_e->sinfo_ppid = ppid;
204 read_queue_e->sinfo_context = stcb->asoc.context;
205 read_queue_e->sinfo_timetolive = 0;
206 read_queue_e->sinfo_tsn = tsn;
207 read_queue_e->sinfo_cumtsn = tsn;
208 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
209 read_queue_e->whoFrom = net;
210 read_queue_e->length = 0;
211 atomic_add_int(&net->ref_count, 1);
212 read_queue_e->data = dm;
213 read_queue_e->spec_flags = 0;
214 read_queue_e->tail_mbuf = NULL;
215 read_queue_e->aux_data = NULL;
216 read_queue_e->stcb = stcb;
217 read_queue_e->port_from = stcb->rport;
218 read_queue_e->do_not_ref_stcb = 0;
219 read_queue_e->end_added = 0;
220 read_queue_e->some_taken = 0;
221 read_queue_e->pdapi_aborted = 0;
223 return (read_queue_e);
228 * Build out our readq entry based on the incoming packet.
230 static struct sctp_queued_to_read *
231 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
232 struct sctp_tmit_chunk *chk)
234 struct sctp_queued_to_read *read_queue_e = NULL;
236 sctp_alloc_a_readq(stcb, read_queue_e);
237 if (read_queue_e == NULL) {
240 read_queue_e->sinfo_stream = chk->rec.data.stream_number;
241 read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
242 read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
243 read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
244 read_queue_e->sinfo_context = stcb->asoc.context;
245 read_queue_e->sinfo_timetolive = 0;
246 read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
247 read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
248 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
249 read_queue_e->whoFrom = chk->whoTo;
250 read_queue_e->aux_data = NULL;
251 read_queue_e->length = 0;
252 atomic_add_int(&chk->whoTo->ref_count, 1);
253 read_queue_e->data = chk->data;
254 read_queue_e->tail_mbuf = NULL;
255 read_queue_e->stcb = stcb;
256 read_queue_e->port_from = stcb->rport;
257 read_queue_e->spec_flags = 0;
258 read_queue_e->do_not_ref_stcb = 0;
259 read_queue_e->end_added = 0;
260 read_queue_e->some_taken = 0;
261 read_queue_e->pdapi_aborted = 0;
263 return (read_queue_e);
268 sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
269 struct sctp_sndrcvinfo *sinfo)
271 struct sctp_sndrcvinfo *outinfo;
275 int use_extended = 0;
277 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
278 /* user does not want the sndrcv ctl */
281 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
283 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
285 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
289 ret = sctp_get_mbuf_for_msg(len,
290 0, M_DONTWAIT, 1, MT_DATA);
296 /* We need a CMSG header followed by the struct */
297 cmh = mtod(ret, struct cmsghdr *);
298 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
299 cmh->cmsg_level = IPPROTO_SCTP;
301 cmh->cmsg_type = SCTP_EXTRCV;
303 memcpy(outinfo, sinfo, len);
305 cmh->cmsg_type = SCTP_SNDRCV;
309 SCTP_BUF_LEN(ret) = cmh->cmsg_len;
315 sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
317 struct sctp_sndrcvinfo *sinfo)
319 struct sctp_sndrcvinfo *outinfo;
323 int use_extended = 0;
325 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
326 /* user does not want the sndrcv ctl */
329 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
331 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
333 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
335 SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
340 /* We need a CMSG header followed by the struct */
341 cmh = (struct cmsghdr *)buf;
342 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
343 cmh->cmsg_level = IPPROTO_SCTP;
345 cmh->cmsg_type = SCTP_EXTRCV;
347 memcpy(outinfo, sinfo, len);
349 cmh->cmsg_type = SCTP_SNDRCV;
359 * We are delivering currently from the reassembly queue. We must continue to
360 * deliver until we either: 1) run out of space. 2) run out of sequential
361 * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
364 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
366 struct sctp_tmit_chunk *chk;
371 struct sctp_queued_to_read *control, *ctl, *ctlat;
376 cntDel = stream_no = 0;
377 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
378 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
379 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
380 /* socket above is long gone or going.. */
382 asoc->fragmented_delivery_inprogress = 0;
383 chk = TAILQ_FIRST(&asoc->reasmqueue);
385 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
386 asoc->size_on_reasm_queue -= chk->send_size;
387 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
389 * Lose the data pointer, since its in the socket
393 sctp_m_freem(chk->data);
396 /* Now free the address and data */
397 sctp_free_a_chunk(stcb, chk);
398 /* sa_ignore FREED_MEMORY */
399 chk = TAILQ_FIRST(&asoc->reasmqueue);
403 SCTP_TCB_LOCK_ASSERT(stcb);
405 chk = TAILQ_FIRST(&asoc->reasmqueue);
409 if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
410 /* Can't deliver more :< */
413 stream_no = chk->rec.data.stream_number;
414 nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
415 if (nxt_todel != chk->rec.data.stream_seq &&
416 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
418 * Not the next sequence to deliver in its stream OR
423 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
425 control = sctp_build_readq_entry_chk(stcb, chk);
426 if (control == NULL) {
430 /* save it off for our future deliveries */
431 stcb->asoc.control_pdapi = control;
432 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
436 sctp_add_to_readq(stcb->sctp_ep,
437 stcb, control, &stcb->sctp_socket->so_rcv, end, SCTP_SO_NOT_LOCKED);
440 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
444 if (sctp_append_to_readq(stcb->sctp_ep, stcb,
445 stcb->asoc.control_pdapi,
446 chk->data, end, chk->rec.data.TSN_seq,
447 &stcb->sctp_socket->so_rcv)) {
449 * something is very wrong, either
450 * control_pdapi is NULL, or the tail_mbuf
451 * is corrupt, or there is a EOM already on
454 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
457 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
458 panic("This should not happen control_pdapi NULL?");
460 /* if we did not panic, it was a EOM */
461 panic("Bad chunking ??");
467 /* pull it we did it */
468 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
469 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
470 asoc->fragmented_delivery_inprogress = 0;
471 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
472 asoc->strmin[stream_no].last_sequence_delivered++;
474 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
475 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
477 } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
479 * turn the flag back on since we just delivered
482 asoc->fragmented_delivery_inprogress = 1;
484 asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
485 asoc->last_flags_delivered = chk->rec.data.rcv_flags;
486 asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
487 asoc->last_strm_no_delivered = chk->rec.data.stream_number;
489 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
490 asoc->size_on_reasm_queue -= chk->send_size;
491 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
492 /* free up the chk */
494 sctp_free_a_chunk(stcb, chk);
496 if (asoc->fragmented_delivery_inprogress == 0) {
498 * Now lets see if we can deliver the next one on
501 struct sctp_stream_in *strm;
503 strm = &asoc->strmin[stream_no];
504 nxt_todel = strm->last_sequence_delivered + 1;
505 ctl = TAILQ_FIRST(&strm->inqueue);
506 if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
507 while (ctl != NULL) {
508 /* Deliver more if we can. */
509 if (nxt_todel == ctl->sinfo_ssn) {
510 ctlat = TAILQ_NEXT(ctl, next);
511 TAILQ_REMOVE(&strm->inqueue, ctl, next);
512 asoc->size_on_all_streams -= ctl->length;
513 sctp_ucount_decr(asoc->cnt_on_all_streams);
514 strm->last_sequence_delivered++;
515 sctp_add_to_readq(stcb->sctp_ep, stcb,
517 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
522 nxt_todel = strm->last_sequence_delivered + 1;
527 /* sa_ignore FREED_MEMORY */
528 chk = TAILQ_FIRST(&asoc->reasmqueue);
533 * Queue the chunk either right into the socket buffer if it is the next one
534 * to go OR put it in the correct place in the delivery queue. If we do
535 * append to the so_buf, keep doing so until we are out of order. One big
536 * question still remains, what to do when the socket buffer is FULL??
539 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
540 struct sctp_queued_to_read *control, int *abort_flag)
543 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
544 * all the data in one stream this could happen quite rapidly. One
545 * could use the TSN to keep track of things, but this scheme breaks
546 * down in the other type of stream useage that could occur. Send a
547 * single msg to stream 0, send 4Billion messages to stream 1, now
548 * send a message to stream 0. You have a situation where the TSN
549 * has wrapped but not in the stream. Is this worth worrying about
550 * or should we just change our queue sort at the bottom to be by
553 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
554 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
555 * assignment this could happen... and I don't see how this would be
556 * a violation. So for now I am undecided an will leave the sort by
557 * SSN alone. Maybe a hybred approach is the answer
560 struct sctp_stream_in *strm;
561 struct sctp_queued_to_read *at;
567 asoc->size_on_all_streams += control->length;
568 sctp_ucount_incr(asoc->cnt_on_all_streams);
569 strm = &asoc->strmin[control->sinfo_stream];
570 nxt_todel = strm->last_sequence_delivered + 1;
571 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
572 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
574 SCTPDBG(SCTP_DEBUG_INDATA1,
575 "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
576 (uint32_t) control->sinfo_stream,
577 (uint32_t) strm->last_sequence_delivered,
578 (uint32_t) nxt_todel);
579 if (compare_with_wrap(strm->last_sequence_delivered,
580 control->sinfo_ssn, MAX_SEQ) ||
581 (strm->last_sequence_delivered == control->sinfo_ssn)) {
582 /* The incoming sseq is behind where we last delivered? */
583 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
584 control->sinfo_ssn, strm->last_sequence_delivered);
586 * throw it in the stream so it gets cleaned up in
587 * association destruction
589 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
590 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
591 0, M_DONTWAIT, 1, MT_DATA);
593 struct sctp_paramhdr *ph;
596 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
597 (sizeof(uint32_t) * 3);
598 ph = mtod(oper, struct sctp_paramhdr *);
599 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
600 ph->param_length = htons(SCTP_BUF_LEN(oper));
601 ippp = (uint32_t *) (ph + 1);
602 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
604 *ippp = control->sinfo_tsn;
606 *ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
608 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
609 sctp_abort_an_association(stcb->sctp_ep, stcb,
610 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
616 if (nxt_todel == control->sinfo_ssn) {
617 /* can be delivered right away? */
618 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
619 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
622 asoc->size_on_all_streams -= control->length;
623 sctp_ucount_decr(asoc->cnt_on_all_streams);
624 strm->last_sequence_delivered++;
625 sctp_add_to_readq(stcb->sctp_ep, stcb,
627 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
628 control = TAILQ_FIRST(&strm->inqueue);
629 while (control != NULL) {
631 nxt_todel = strm->last_sequence_delivered + 1;
632 if (nxt_todel == control->sinfo_ssn) {
633 at = TAILQ_NEXT(control, next);
634 TAILQ_REMOVE(&strm->inqueue, control, next);
635 asoc->size_on_all_streams -= control->length;
636 sctp_ucount_decr(asoc->cnt_on_all_streams);
637 strm->last_sequence_delivered++;
639 * We ignore the return of deliver_data here
640 * since we always can hold the chunk on the
641 * d-queue. And we have a finite number that
642 * can be delivered from the strq.
644 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
645 sctp_log_strm_del(control, NULL,
646 SCTP_STR_LOG_FROM_IMMED_DEL);
648 sctp_add_to_readq(stcb->sctp_ep, stcb,
650 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
659 * Ok, we did not deliver this guy, find the correct place
660 * to put it on the queue.
662 if (TAILQ_EMPTY(&strm->inqueue)) {
664 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
665 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
667 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
669 TAILQ_FOREACH(at, &strm->inqueue, next) {
670 if (compare_with_wrap(at->sinfo_ssn,
671 control->sinfo_ssn, MAX_SEQ)) {
673 * one in queue is bigger than the
674 * new one, insert before this one
676 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
677 sctp_log_strm_del(control, at,
678 SCTP_STR_LOG_FROM_INSERT_MD);
680 TAILQ_INSERT_BEFORE(at, control, next);
682 } else if (at->sinfo_ssn == control->sinfo_ssn) {
684 * Gak, He sent me a duplicate str
688 * foo bar, I guess I will just free
689 * this new guy, should we abort
690 * too? FIX ME MAYBE? Or it COULD be
691 * that the SSN's have wrapped.
692 * Maybe I should compare to TSN
693 * somehow... sigh for now just blow
698 sctp_m_freem(control->data);
699 control->data = NULL;
700 asoc->size_on_all_streams -= control->length;
701 sctp_ucount_decr(asoc->cnt_on_all_streams);
702 if (control->whoFrom)
703 sctp_free_remote_addr(control->whoFrom);
704 control->whoFrom = NULL;
705 sctp_free_a_readq(stcb, control);
708 if (TAILQ_NEXT(at, next) == NULL) {
710 * We are at the end, insert
713 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
714 sctp_log_strm_del(control, at,
715 SCTP_STR_LOG_FROM_INSERT_TL);
717 TAILQ_INSERT_AFTER(&strm->inqueue,
728 * Returns two things: You get the total size of the deliverable parts of the
729 * first fragmented message on the reassembly queue. And you get a 1 back if
730 * all of the message is ready or a 0 back if the message is still incomplete
733 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
735 struct sctp_tmit_chunk *chk;
739 chk = TAILQ_FIRST(&asoc->reasmqueue);
741 /* nothing on the queue */
744 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
745 /* Not a first on the queue */
748 tsn = chk->rec.data.TSN_seq;
750 if (tsn != chk->rec.data.TSN_seq) {
753 *t_size += chk->send_size;
754 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
758 chk = TAILQ_NEXT(chk, sctp_next);
764 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
766 struct sctp_tmit_chunk *chk;
771 chk = TAILQ_FIRST(&asoc->reasmqueue);
774 asoc->size_on_reasm_queue = 0;
775 asoc->cnt_on_reasm_queue = 0;
778 if (asoc->fragmented_delivery_inprogress == 0) {
780 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
781 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
782 (nxt_todel == chk->rec.data.stream_seq ||
783 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
785 * Yep the first one is here and its ok to deliver
788 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
789 (tsize >= stcb->sctp_ep->partial_delivery_point))) {
792 * Yes, we setup to start reception, by
793 * backing down the TSN just in case we
794 * can't deliver. If we
796 asoc->fragmented_delivery_inprogress = 1;
797 asoc->tsn_last_delivered =
798 chk->rec.data.TSN_seq - 1;
800 chk->rec.data.stream_number;
801 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
802 asoc->pdapi_ppid = chk->rec.data.payloadtype;
803 asoc->fragment_flags = chk->rec.data.rcv_flags;
804 sctp_service_reassembly(stcb, asoc);
809 * Service re-assembly will deliver stream data queued at
810 * the end of fragmented delivery.. but it wont know to go
811 * back and call itself again... we do that here with the
814 sctp_service_reassembly(stcb, asoc);
815 if (asoc->fragmented_delivery_inprogress == 0) {
817 * finished our Fragmented delivery, could be more
826 * Dump onto the re-assembly queue, in its proper place. After dumping on the
827 * queue, see if anthing can be delivered. If so pull it off (or as much as
828 * we can. If we run out of space then we must dump what we can and set the
829 * appropriate flag to say we queued what we could.
832 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
833 struct sctp_tmit_chunk *chk, int *abort_flag)
836 uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
838 struct sctp_tmit_chunk *at, *prev, *next;
841 cum_ackp1 = asoc->tsn_last_delivered + 1;
842 if (TAILQ_EMPTY(&asoc->reasmqueue)) {
843 /* This is the first one on the queue */
844 TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
846 * we do not check for delivery of anything when only one
849 asoc->size_on_reasm_queue = chk->send_size;
850 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
851 if (chk->rec.data.TSN_seq == cum_ackp1) {
852 if (asoc->fragmented_delivery_inprogress == 0 &&
853 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
854 SCTP_DATA_FIRST_FRAG) {
856 * An empty queue, no delivery inprogress,
857 * we hit the next one and it does NOT have
858 * a FIRST fragment mark.
860 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
861 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
862 0, M_DONTWAIT, 1, MT_DATA);
865 struct sctp_paramhdr *ph;
869 sizeof(struct sctp_paramhdr) +
870 (sizeof(uint32_t) * 3);
871 ph = mtod(oper, struct sctp_paramhdr *);
873 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
874 ph->param_length = htons(SCTP_BUF_LEN(oper));
875 ippp = (uint32_t *) (ph + 1);
876 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
878 *ippp = chk->rec.data.TSN_seq;
880 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
883 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
884 sctp_abort_an_association(stcb->sctp_ep, stcb,
885 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
887 } else if (asoc->fragmented_delivery_inprogress &&
888 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
890 * We are doing a partial delivery and the
891 * NEXT chunk MUST be either the LAST or
892 * MIDDLE fragment NOT a FIRST
894 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
895 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
896 0, M_DONTWAIT, 1, MT_DATA);
898 struct sctp_paramhdr *ph;
902 sizeof(struct sctp_paramhdr) +
903 (3 * sizeof(uint32_t));
904 ph = mtod(oper, struct sctp_paramhdr *);
906 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
907 ph->param_length = htons(SCTP_BUF_LEN(oper));
908 ippp = (uint32_t *) (ph + 1);
909 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
911 *ippp = chk->rec.data.TSN_seq;
913 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
915 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
916 sctp_abort_an_association(stcb->sctp_ep, stcb,
917 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
919 } else if (asoc->fragmented_delivery_inprogress) {
921 * Here we are ok with a MIDDLE or LAST
924 if (chk->rec.data.stream_number !=
925 asoc->str_of_pdapi) {
926 /* Got to be the right STR No */
927 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
928 chk->rec.data.stream_number,
930 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
931 0, M_DONTWAIT, 1, MT_DATA);
933 struct sctp_paramhdr *ph;
937 sizeof(struct sctp_paramhdr) +
938 (sizeof(uint32_t) * 3);
940 struct sctp_paramhdr *);
942 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
944 htons(SCTP_BUF_LEN(oper));
945 ippp = (uint32_t *) (ph + 1);
946 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
948 *ippp = chk->rec.data.TSN_seq;
950 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
952 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
953 sctp_abort_an_association(stcb->sctp_ep,
954 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
956 } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
957 SCTP_DATA_UNORDERED &&
958 chk->rec.data.stream_seq !=
959 asoc->ssn_of_pdapi) {
960 /* Got to be the right STR Seq */
961 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
962 chk->rec.data.stream_seq,
964 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
965 0, M_DONTWAIT, 1, MT_DATA);
967 struct sctp_paramhdr *ph;
971 sizeof(struct sctp_paramhdr) +
972 (3 * sizeof(uint32_t));
974 struct sctp_paramhdr *);
976 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
978 htons(SCTP_BUF_LEN(oper));
979 ippp = (uint32_t *) (ph + 1);
980 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
982 *ippp = chk->rec.data.TSN_seq;
984 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
987 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
988 sctp_abort_an_association(stcb->sctp_ep,
989 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
997 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
998 if (compare_with_wrap(at->rec.data.TSN_seq,
999 chk->rec.data.TSN_seq, MAX_TSN)) {
1001 * one in queue is bigger than the new one, insert
1005 asoc->size_on_reasm_queue += chk->send_size;
1006 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1008 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1010 } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1011 /* Gak, He sent me a duplicate str seq number */
1013 * foo bar, I guess I will just free this new guy,
1014 * should we abort too? FIX ME MAYBE? Or it COULD be
1015 * that the SSN's have wrapped. Maybe I should
1016 * compare to TSN somehow... sigh for now just blow
1020 sctp_m_freem(chk->data);
1023 sctp_free_a_chunk(stcb, chk);
1026 last_flags = at->rec.data.rcv_flags;
1027 last_tsn = at->rec.data.TSN_seq;
1029 if (TAILQ_NEXT(at, sctp_next) == NULL) {
1031 * We are at the end, insert it after this
1034 /* check it first */
1035 asoc->size_on_reasm_queue += chk->send_size;
1036 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1037 TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1042 /* Now the audits */
1044 prev_tsn = chk->rec.data.TSN_seq - 1;
1045 if (prev_tsn == prev->rec.data.TSN_seq) {
1047 * Ok the one I am dropping onto the end is the
1048 * NEXT. A bit of valdiation here.
1050 if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1051 SCTP_DATA_FIRST_FRAG ||
1052 (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1053 SCTP_DATA_MIDDLE_FRAG) {
1055 * Insert chk MUST be a MIDDLE or LAST
1058 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1059 SCTP_DATA_FIRST_FRAG) {
1060 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1061 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1062 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1063 0, M_DONTWAIT, 1, MT_DATA);
1065 struct sctp_paramhdr *ph;
1068 SCTP_BUF_LEN(oper) =
1069 sizeof(struct sctp_paramhdr) +
1070 (3 * sizeof(uint32_t));
1072 struct sctp_paramhdr *);
1074 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1076 htons(SCTP_BUF_LEN(oper));
1077 ippp = (uint32_t *) (ph + 1);
1078 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1080 *ippp = chk->rec.data.TSN_seq;
1082 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1085 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1086 sctp_abort_an_association(stcb->sctp_ep,
1087 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1091 if (chk->rec.data.stream_number !=
1092 prev->rec.data.stream_number) {
1094 * Huh, need the correct STR here,
1095 * they must be the same.
1097 SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1098 chk->rec.data.stream_number,
1099 prev->rec.data.stream_number);
1100 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1101 0, M_DONTWAIT, 1, MT_DATA);
1103 struct sctp_paramhdr *ph;
1106 SCTP_BUF_LEN(oper) =
1107 sizeof(struct sctp_paramhdr) +
1108 (3 * sizeof(uint32_t));
1110 struct sctp_paramhdr *);
1112 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1114 htons(SCTP_BUF_LEN(oper));
1115 ippp = (uint32_t *) (ph + 1);
1116 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1118 *ippp = chk->rec.data.TSN_seq;
1120 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1122 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1123 sctp_abort_an_association(stcb->sctp_ep,
1124 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1129 if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1130 chk->rec.data.stream_seq !=
1131 prev->rec.data.stream_seq) {
1133 * Huh, need the correct STR here,
1134 * they must be the same.
1136 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1137 chk->rec.data.stream_seq,
1138 prev->rec.data.stream_seq);
1139 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1140 0, M_DONTWAIT, 1, MT_DATA);
1142 struct sctp_paramhdr *ph;
1145 SCTP_BUF_LEN(oper) =
1146 sizeof(struct sctp_paramhdr) +
1147 (3 * sizeof(uint32_t));
1149 struct sctp_paramhdr *);
1151 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1153 htons(SCTP_BUF_LEN(oper));
1154 ippp = (uint32_t *) (ph + 1);
1155 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1157 *ippp = chk->rec.data.TSN_seq;
1159 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1161 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1162 sctp_abort_an_association(stcb->sctp_ep,
1163 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1168 } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1169 SCTP_DATA_LAST_FRAG) {
1170 /* Insert chk MUST be a FIRST */
1171 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1172 SCTP_DATA_FIRST_FRAG) {
1173 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1174 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1175 0, M_DONTWAIT, 1, MT_DATA);
1177 struct sctp_paramhdr *ph;
1180 SCTP_BUF_LEN(oper) =
1181 sizeof(struct sctp_paramhdr) +
1182 (3 * sizeof(uint32_t));
1184 struct sctp_paramhdr *);
1186 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1188 htons(SCTP_BUF_LEN(oper));
1189 ippp = (uint32_t *) (ph + 1);
1190 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1192 *ippp = chk->rec.data.TSN_seq;
1194 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1197 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1198 sctp_abort_an_association(stcb->sctp_ep,
1199 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1208 post_tsn = chk->rec.data.TSN_seq + 1;
1209 if (post_tsn == next->rec.data.TSN_seq) {
1211 * Ok the one I am inserting ahead of is my NEXT
1212 * one. A bit of valdiation here.
1214 if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1215 /* Insert chk MUST be a last fragment */
1216 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1217 != SCTP_DATA_LAST_FRAG) {
1218 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1219 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1220 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1221 0, M_DONTWAIT, 1, MT_DATA);
1223 struct sctp_paramhdr *ph;
1226 SCTP_BUF_LEN(oper) =
1227 sizeof(struct sctp_paramhdr) +
1228 (3 * sizeof(uint32_t));
1230 struct sctp_paramhdr *);
1232 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1234 htons(SCTP_BUF_LEN(oper));
1235 ippp = (uint32_t *) (ph + 1);
1236 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1238 *ippp = chk->rec.data.TSN_seq;
1240 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1242 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1243 sctp_abort_an_association(stcb->sctp_ep,
1244 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1249 } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1250 SCTP_DATA_MIDDLE_FRAG ||
1251 (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1252 SCTP_DATA_LAST_FRAG) {
1254 * Insert chk CAN be MIDDLE or FIRST NOT
1257 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1258 SCTP_DATA_LAST_FRAG) {
1259 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1260 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1261 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1262 0, M_DONTWAIT, 1, MT_DATA);
1264 struct sctp_paramhdr *ph;
1267 SCTP_BUF_LEN(oper) =
1268 sizeof(struct sctp_paramhdr) +
1269 (3 * sizeof(uint32_t));
1271 struct sctp_paramhdr *);
1273 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1275 htons(SCTP_BUF_LEN(oper));
1276 ippp = (uint32_t *) (ph + 1);
1277 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1279 *ippp = chk->rec.data.TSN_seq;
1281 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1284 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1285 sctp_abort_an_association(stcb->sctp_ep,
1286 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1291 if (chk->rec.data.stream_number !=
1292 next->rec.data.stream_number) {
1294 * Huh, need the correct STR here,
1295 * they must be the same.
1297 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1298 chk->rec.data.stream_number,
1299 next->rec.data.stream_number);
1300 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1301 0, M_DONTWAIT, 1, MT_DATA);
1303 struct sctp_paramhdr *ph;
1306 SCTP_BUF_LEN(oper) =
1307 sizeof(struct sctp_paramhdr) +
1308 (3 * sizeof(uint32_t));
1310 struct sctp_paramhdr *);
1312 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1314 htons(SCTP_BUF_LEN(oper));
1315 ippp = (uint32_t *) (ph + 1);
1316 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1318 *ippp = chk->rec.data.TSN_seq;
1320 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1323 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1324 sctp_abort_an_association(stcb->sctp_ep,
1325 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1330 if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1331 chk->rec.data.stream_seq !=
1332 next->rec.data.stream_seq) {
1334 * Huh, need the correct STR here,
1335 * they must be the same.
1337 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1338 chk->rec.data.stream_seq,
1339 next->rec.data.stream_seq);
1340 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1341 0, M_DONTWAIT, 1, MT_DATA);
1343 struct sctp_paramhdr *ph;
1346 SCTP_BUF_LEN(oper) =
1347 sizeof(struct sctp_paramhdr) +
1348 (3 * sizeof(uint32_t));
1350 struct sctp_paramhdr *);
1352 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1354 htons(SCTP_BUF_LEN(oper));
1355 ippp = (uint32_t *) (ph + 1);
1356 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1358 *ippp = chk->rec.data.TSN_seq;
1360 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1362 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1363 sctp_abort_an_association(stcb->sctp_ep,
1364 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1372 /* Do we need to do some delivery? check */
1373 sctp_deliver_reasm_check(stcb, asoc);
1377 * This is an unfortunate routine. It checks to make sure a evil guy is not
1378 * stuffing us full of bad packet fragments. A broken peer could also do this
1379 * but this is doubtful. It is to bad I must worry about evil crackers sigh
1383 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1386 struct sctp_tmit_chunk *at;
1389 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1390 if (compare_with_wrap(TSN_seq,
1391 at->rec.data.TSN_seq, MAX_TSN)) {
1392 /* is it one bigger? */
1393 tsn_est = at->rec.data.TSN_seq + 1;
1394 if (tsn_est == TSN_seq) {
1395 /* yep. It better be a last then */
1396 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1397 SCTP_DATA_LAST_FRAG) {
1399 * Ok this guy belongs next to a guy
1400 * that is NOT last, it should be a
1401 * middle/last, not a complete
1407 * This guy is ok since its a LAST
1408 * and the new chunk is a fully
1409 * self- contained one.
1414 } else if (TSN_seq == at->rec.data.TSN_seq) {
1415 /* Software error since I have a dup? */
1419 * Ok, 'at' is larger than new chunk but does it
1420 * need to be right before it.
1422 tsn_est = TSN_seq + 1;
1423 if (tsn_est == at->rec.data.TSN_seq) {
1424 /* Yep, It better be a first */
1425 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1426 SCTP_DATA_FIRST_FRAG) {
1439 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1440 struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1441 struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1442 int *break_flag, int last_chunk)
1444 /* Process a data chunk */
1445 /* struct sctp_tmit_chunk *chk; */
1446 struct sctp_tmit_chunk *chk;
1450 int need_reasm_check = 0;
1451 uint16_t strmno, strmseq;
1453 struct sctp_queued_to_read *control;
1455 uint32_t protocol_id;
1456 uint8_t chunk_flags;
1457 struct sctp_stream_reset_list *liste;
1460 tsn = ntohl(ch->dp.tsn);
1461 chunk_flags = ch->ch.chunk_flags;
1462 protocol_id = ch->dp.protocol_id;
1463 ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1464 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1465 sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1470 SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1471 if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1472 asoc->cumulative_tsn == tsn) {
1473 /* It is a duplicate */
1474 SCTP_STAT_INCR(sctps_recvdupdata);
1475 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1476 /* Record a dup for the next outbound sack */
1477 asoc->dup_tsns[asoc->numduptsns] = tsn;
1480 asoc->send_sack = 1;
1483 /* Calculate the number of TSN's between the base and this TSN */
1484 if (tsn >= asoc->mapping_array_base_tsn) {
1485 gap = tsn - asoc->mapping_array_base_tsn;
1487 gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1489 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1490 /* Can't hold the bit in the mapping at max array, toss it */
1493 if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1494 SCTP_TCB_LOCK_ASSERT(stcb);
1495 if (sctp_expand_mapping_array(asoc, gap)) {
1496 /* Can't expand, drop it */
1500 if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1503 /* See if we have received this one already */
1504 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1505 SCTP_STAT_INCR(sctps_recvdupdata);
1506 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1507 /* Record a dup for the next outbound sack */
1508 asoc->dup_tsns[asoc->numduptsns] = tsn;
1511 asoc->send_sack = 1;
1515 * Check to see about the GONE flag, duplicates would cause a sack
1516 * to be sent up above
1518 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1519 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1520 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1523 * wait a minute, this guy is gone, there is no longer a
1524 * receiver. Send peer an ABORT!
1526 struct mbuf *op_err;
1528 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1529 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1534 * Now before going further we see if there is room. If NOT then we
1535 * MAY let one through only IF this TSN is the one we are waiting
1536 * for on a partial delivery API.
1539 /* now do the tests */
1540 if (((asoc->cnt_on_all_streams +
1541 asoc->cnt_on_reasm_queue +
1542 asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
1543 (((int)asoc->my_rwnd) <= 0)) {
1545 * When we have NO room in the rwnd we check to make sure
1546 * the reader is doing its job...
1548 if (stcb->sctp_socket->so_rcv.sb_cc) {
1549 /* some to read, wake-up */
1550 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1553 so = SCTP_INP_SO(stcb->sctp_ep);
1554 atomic_add_int(&stcb->asoc.refcnt, 1);
1555 SCTP_TCB_UNLOCK(stcb);
1556 SCTP_SOCKET_LOCK(so, 1);
1557 SCTP_TCB_LOCK(stcb);
1558 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1559 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1560 /* assoc was freed while we were unlocked */
1561 SCTP_SOCKET_UNLOCK(so, 1);
1565 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1566 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1567 SCTP_SOCKET_UNLOCK(so, 1);
1570 /* now is it in the mapping array of what we have accepted? */
1571 if (compare_with_wrap(tsn,
1572 asoc->highest_tsn_inside_map, MAX_TSN)) {
1574 /* Nope not in the valid range dump it */
1575 sctp_set_rwnd(stcb, asoc);
1576 if ((asoc->cnt_on_all_streams +
1577 asoc->cnt_on_reasm_queue +
1578 asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
1579 SCTP_STAT_INCR(sctps_datadropchklmt);
1581 SCTP_STAT_INCR(sctps_datadroprwnd);
1588 strmno = ntohs(ch->dp.stream_id);
1589 if (strmno >= asoc->streamincnt) {
1590 struct sctp_paramhdr *phdr;
1593 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1594 0, M_DONTWAIT, 1, MT_DATA);
1596 /* add some space up front so prepend will work well */
1597 SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1598 phdr = mtod(mb, struct sctp_paramhdr *);
1600 * Error causes are just param's and this one has
1601 * two back to back phdr, one with the error type
1602 * and size, the other with the streamid and a rsvd
1604 SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1605 phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1606 phdr->param_length =
1607 htons(sizeof(struct sctp_paramhdr) * 2);
1609 /* We insert the stream in the type field */
1610 phdr->param_type = ch->dp.stream_id;
1611 /* And set the length to 0 for the rsvd field */
1612 phdr->param_length = 0;
1613 sctp_queue_op_err(stcb, mb);
1615 SCTP_STAT_INCR(sctps_badsid);
1616 SCTP_TCB_LOCK_ASSERT(stcb);
1617 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1618 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1619 /* we have a new high score */
1620 asoc->highest_tsn_inside_map = tsn;
1621 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1622 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1625 if (tsn == (asoc->cumulative_tsn + 1)) {
1626 /* Update cum-ack */
1627 asoc->cumulative_tsn = tsn;
1632 * Before we continue lets validate that we are not being fooled by
1633 * an evil attacker. We can only have 4k chunks based on our TSN
1634 * spread allowed by the mapping array 512 * 8 bits, so there is no
1635 * way our stream sequence numbers could have wrapped. We of course
1636 * only validate the FIRST fragment so the bit must be set.
1638 strmseq = ntohs(ch->dp.stream_sequence);
1639 #ifdef SCTP_ASOCLOG_OF_TSNS
1640 SCTP_TCB_LOCK_ASSERT(stcb);
1641 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1642 asoc->tsn_in_at = 0;
1643 asoc->tsn_in_wrapped = 1;
1645 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1646 asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1647 asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1648 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1649 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1650 asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1651 asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1652 asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1655 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1656 (TAILQ_EMPTY(&asoc->resetHead)) &&
1657 (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1658 (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1659 strmseq, MAX_SEQ) ||
1660 asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1661 /* The incoming sseq is behind where we last delivered? */
1662 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1663 strmseq, asoc->strmin[strmno].last_sequence_delivered);
1664 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1665 0, M_DONTWAIT, 1, MT_DATA);
1667 struct sctp_paramhdr *ph;
1670 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1671 (3 * sizeof(uint32_t));
1672 ph = mtod(oper, struct sctp_paramhdr *);
1673 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1674 ph->param_length = htons(SCTP_BUF_LEN(oper));
1675 ippp = (uint32_t *) (ph + 1);
1676 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1680 *ippp = ((strmno << 16) | strmseq);
1683 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1684 sctp_abort_an_association(stcb->sctp_ep, stcb,
1685 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1689 /************************************
1690 * From here down we may find ch-> invalid
1691 * so its a good idea NOT to use it.
1692 *************************************/
1694 the_len = (chk_length - sizeof(struct sctp_data_chunk));
1695 if (last_chunk == 0) {
1696 dmbuf = SCTP_M_COPYM(*m,
1697 (offset + sizeof(struct sctp_data_chunk)),
1698 the_len, M_DONTWAIT);
1699 #ifdef SCTP_MBUF_LOGGING
1700 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
1705 if (SCTP_BUF_IS_EXTENDED(mat)) {
1706 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1708 mat = SCTP_BUF_NEXT(mat);
1713 /* We can steal the last chunk */
1717 /* lop off the top part */
1718 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1719 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1720 l_len = SCTP_BUF_LEN(dmbuf);
1723 * need to count up the size hopefully does not hit
1731 l_len += SCTP_BUF_LEN(lat);
1732 lat = SCTP_BUF_NEXT(lat);
1735 if (l_len > the_len) {
1736 /* Trim the end round bytes off too */
1737 m_adj(dmbuf, -(l_len - the_len));
1740 if (dmbuf == NULL) {
1741 SCTP_STAT_INCR(sctps_nomem);
1744 if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1745 asoc->fragmented_delivery_inprogress == 0 &&
1746 TAILQ_EMPTY(&asoc->resetHead) &&
1748 ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1749 TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1750 /* Candidate for express delivery */
1752 * Its not fragmented, No PD-API is up, Nothing in the
1753 * delivery queue, Its un-ordered OR ordered and the next to
1754 * deliver AND nothing else is stuck on the stream queue,
1755 * And there is room for it in the socket buffer. Lets just
1756 * stuff it up the buffer....
1759 /* It would be nice to avoid this copy if we could :< */
1760 sctp_alloc_a_readq(stcb, control);
1761 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1767 if (control == NULL) {
1768 goto failed_express_del;
1770 sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1771 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1772 /* for ordered, bump what we delivered */
1773 asoc->strmin[strmno].last_sequence_delivered++;
1775 SCTP_STAT_INCR(sctps_recvexpress);
1776 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
1777 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1778 SCTP_STR_LOG_FROM_EXPRS_DEL);
1781 goto finish_express_del;
1784 /* If we reach here this is a new chunk */
1787 /* Express for fragmented delivery? */
1788 if ((asoc->fragmented_delivery_inprogress) &&
1789 (stcb->asoc.control_pdapi) &&
1790 (asoc->str_of_pdapi == strmno) &&
1791 (asoc->ssn_of_pdapi == strmseq)
1793 control = stcb->asoc.control_pdapi;
1794 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1795 /* Can't be another first? */
1796 goto failed_pdapi_express_del;
1798 if (tsn == (control->sinfo_tsn + 1)) {
1799 /* Yep, we can add it on */
1803 if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1806 cumack = asoc->cumulative_tsn;
1807 if ((cumack + 1) == tsn)
1810 if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1812 &stcb->sctp_socket->so_rcv)) {
1813 SCTP_PRINTF("Append fails end:%d\n", end);
1814 goto failed_pdapi_express_del;
1816 SCTP_STAT_INCR(sctps_recvexpressm);
1817 control->sinfo_tsn = tsn;
1818 asoc->tsn_last_delivered = tsn;
1819 asoc->fragment_flags = chunk_flags;
1820 asoc->tsn_of_pdapi_last_delivered = tsn;
1821 asoc->last_flags_delivered = chunk_flags;
1822 asoc->last_strm_seq_delivered = strmseq;
1823 asoc->last_strm_no_delivered = strmno;
1825 /* clean up the flags and such */
1826 asoc->fragmented_delivery_inprogress = 0;
1827 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1828 asoc->strmin[strmno].last_sequence_delivered++;
1830 stcb->asoc.control_pdapi = NULL;
1831 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1833 * There could be another message
1836 need_reasm_check = 1;
1840 goto finish_express_del;
1843 failed_pdapi_express_del:
1845 if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1846 sctp_alloc_a_chunk(stcb, chk);
1848 /* No memory so we drop the chunk */
1849 SCTP_STAT_INCR(sctps_nomem);
1850 if (last_chunk == 0) {
1851 /* we copied it, free the copy */
1852 sctp_m_freem(dmbuf);
1856 chk->rec.data.TSN_seq = tsn;
1857 chk->no_fr_allowed = 0;
1858 chk->rec.data.stream_seq = strmseq;
1859 chk->rec.data.stream_number = strmno;
1860 chk->rec.data.payloadtype = protocol_id;
1861 chk->rec.data.context = stcb->asoc.context;
1862 chk->rec.data.doing_fast_retransmit = 0;
1863 chk->rec.data.rcv_flags = chunk_flags;
1865 chk->send_size = the_len;
1867 atomic_add_int(&net->ref_count, 1);
1870 sctp_alloc_a_readq(stcb, control);
1871 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1877 if (control == NULL) {
1878 /* No memory so we drop the chunk */
1879 SCTP_STAT_INCR(sctps_nomem);
1880 if (last_chunk == 0) {
1881 /* we copied it, free the copy */
1882 sctp_m_freem(dmbuf);
1886 control->length = the_len;
1889 /* Mark it as received */
1890 /* Now queue it where it belongs */
1891 if (control != NULL) {
1892 /* First a sanity check */
1893 if (asoc->fragmented_delivery_inprogress) {
1895 * Ok, we have a fragmented delivery in progress if
1896 * this chunk is next to deliver OR belongs in our
1897 * view to the reassembly, the peer is evil or
1900 uint32_t estimate_tsn;
1902 estimate_tsn = asoc->tsn_last_delivered + 1;
1903 if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1904 (estimate_tsn == control->sinfo_tsn)) {
1905 /* Evil/Broke peer */
1906 sctp_m_freem(control->data);
1907 control->data = NULL;
1908 if (control->whoFrom) {
1909 sctp_free_remote_addr(control->whoFrom);
1910 control->whoFrom = NULL;
1912 sctp_free_a_readq(stcb, control);
1913 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1914 0, M_DONTWAIT, 1, MT_DATA);
1916 struct sctp_paramhdr *ph;
1919 SCTP_BUF_LEN(oper) =
1920 sizeof(struct sctp_paramhdr) +
1921 (3 * sizeof(uint32_t));
1922 ph = mtod(oper, struct sctp_paramhdr *);
1924 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1925 ph->param_length = htons(SCTP_BUF_LEN(oper));
1926 ippp = (uint32_t *) (ph + 1);
1927 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1931 *ippp = ((strmno << 16) | strmseq);
1933 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1934 sctp_abort_an_association(stcb->sctp_ep, stcb,
1935 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1940 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1941 sctp_m_freem(control->data);
1942 control->data = NULL;
1943 if (control->whoFrom) {
1944 sctp_free_remote_addr(control->whoFrom);
1945 control->whoFrom = NULL;
1947 sctp_free_a_readq(stcb, control);
1949 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1950 0, M_DONTWAIT, 1, MT_DATA);
1952 struct sctp_paramhdr *ph;
1955 SCTP_BUF_LEN(oper) =
1956 sizeof(struct sctp_paramhdr) +
1957 (3 * sizeof(uint32_t));
1959 struct sctp_paramhdr *);
1961 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1963 htons(SCTP_BUF_LEN(oper));
1964 ippp = (uint32_t *) (ph + 1);
1965 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1969 *ippp = ((strmno << 16) | strmseq);
1971 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1972 sctp_abort_an_association(stcb->sctp_ep,
1973 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1980 /* No PDAPI running */
1981 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1983 * Reassembly queue is NOT empty validate
1984 * that this tsn does not need to be in
1985 * reasembly queue. If it does then our peer
1986 * is broken or evil.
1988 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1989 sctp_m_freem(control->data);
1990 control->data = NULL;
1991 if (control->whoFrom) {
1992 sctp_free_remote_addr(control->whoFrom);
1993 control->whoFrom = NULL;
1995 sctp_free_a_readq(stcb, control);
1996 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1997 0, M_DONTWAIT, 1, MT_DATA);
1999 struct sctp_paramhdr *ph;
2002 SCTP_BUF_LEN(oper) =
2003 sizeof(struct sctp_paramhdr) +
2004 (3 * sizeof(uint32_t));
2006 struct sctp_paramhdr *);
2008 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2010 htons(SCTP_BUF_LEN(oper));
2011 ippp = (uint32_t *) (ph + 1);
2012 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2016 *ippp = ((strmno << 16) | strmseq);
2018 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2019 sctp_abort_an_association(stcb->sctp_ep,
2020 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2027 /* ok, if we reach here we have passed the sanity checks */
2028 if (chunk_flags & SCTP_DATA_UNORDERED) {
2029 /* queue directly into socket buffer */
2030 sctp_add_to_readq(stcb->sctp_ep, stcb,
2032 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
2035 * Special check for when streams are resetting. We
2036 * could be more smart about this and check the
2037 * actual stream to see if it is not being reset..
2038 * that way we would not create a HOLB when amongst
2039 * streams being reset and those not being reset.
2041 * We take complete messages that have a stream reset
2042 * intervening (aka the TSN is after where our
2043 * cum-ack needs to be) off and put them on a
2044 * pending_reply_queue. The reassembly ones we do
2045 * not have to worry about since they are all sorted
2046 * and proceessed by TSN order. It is only the
2047 * singletons I must worry about.
2049 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2050 ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2053 * yep its past where we need to reset... go
2054 * ahead and queue it.
2056 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2058 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2060 struct sctp_queued_to_read *ctlOn;
2061 unsigned char inserted = 0;
2063 ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2065 if (compare_with_wrap(control->sinfo_tsn,
2066 ctlOn->sinfo_tsn, MAX_TSN)) {
2067 ctlOn = TAILQ_NEXT(ctlOn, next);
2070 TAILQ_INSERT_BEFORE(ctlOn, control, next);
2075 if (inserted == 0) {
2077 * must be put at end, use
2078 * prevP (all setup from
2079 * loop) to setup nextP.
2081 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2085 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2092 /* Into the re-assembly queue */
2093 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2096 * the assoc is now gone and chk was put onto the
2097 * reasm queue, which has all been freed.
2104 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2105 /* we have a new high score */
2106 asoc->highest_tsn_inside_map = tsn;
2107 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2108 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2111 if (tsn == (asoc->cumulative_tsn + 1)) {
2112 /* Update cum-ack */
2113 asoc->cumulative_tsn = tsn;
2119 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2121 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2123 SCTP_STAT_INCR(sctps_recvdata);
2124 /* Set it present please */
2125 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
2126 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2128 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2129 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2130 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2132 SCTP_TCB_LOCK_ASSERT(stcb);
2133 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2134 /* check the special flag for stream resets */
2135 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2136 ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2137 (asoc->cumulative_tsn == liste->tsn))
2140 * we have finished working through the backlogged TSN's now
2141 * time to reset streams. 1: call reset function. 2: free
2142 * pending_reply space 3: distribute any chunks in
2143 * pending_reply_queue.
2145 struct sctp_queued_to_read *ctl;
2147 sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2148 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2149 SCTP_FREE(liste, SCTP_M_STRESET);
2150 /* sa_ignore FREED_MEMORY */
2151 liste = TAILQ_FIRST(&asoc->resetHead);
2152 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2153 if (ctl && (liste == NULL)) {
2154 /* All can be removed */
2156 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2157 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2161 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2164 /* more than one in queue */
2165 while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2167 * if ctl->sinfo_tsn is <= liste->tsn we can
2168 * process it which is the NOT of
2169 * ctl->sinfo_tsn > liste->tsn
2171 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2172 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2176 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2180 * Now service re-assembly to pick up anything that has been
2181 * held on reassembly queue?
2183 sctp_deliver_reasm_check(stcb, asoc);
2184 need_reasm_check = 0;
2186 if (need_reasm_check) {
2187 /* Another one waits ? */
2188 sctp_deliver_reasm_check(stcb, asoc);
2193 int8_t sctp_map_lookup_tab[256] = {
2194 -1, 0, -1, 1, -1, 0, -1, 2,
2195 -1, 0, -1, 1, -1, 0, -1, 3,
2196 -1, 0, -1, 1, -1, 0, -1, 2,
2197 -1, 0, -1, 1, -1, 0, -1, 4,
2198 -1, 0, -1, 1, -1, 0, -1, 2,
2199 -1, 0, -1, 1, -1, 0, -1, 3,
2200 -1, 0, -1, 1, -1, 0, -1, 2,
2201 -1, 0, -1, 1, -1, 0, -1, 5,
2202 -1, 0, -1, 1, -1, 0, -1, 2,
2203 -1, 0, -1, 1, -1, 0, -1, 3,
2204 -1, 0, -1, 1, -1, 0, -1, 2,
2205 -1, 0, -1, 1, -1, 0, -1, 4,
2206 -1, 0, -1, 1, -1, 0, -1, 2,
2207 -1, 0, -1, 1, -1, 0, -1, 3,
2208 -1, 0, -1, 1, -1, 0, -1, 2,
2209 -1, 0, -1, 1, -1, 0, -1, 6,
2210 -1, 0, -1, 1, -1, 0, -1, 2,
2211 -1, 0, -1, 1, -1, 0, -1, 3,
2212 -1, 0, -1, 1, -1, 0, -1, 2,
2213 -1, 0, -1, 1, -1, 0, -1, 4,
2214 -1, 0, -1, 1, -1, 0, -1, 2,
2215 -1, 0, -1, 1, -1, 0, -1, 3,
2216 -1, 0, -1, 1, -1, 0, -1, 2,
2217 -1, 0, -1, 1, -1, 0, -1, 5,
2218 -1, 0, -1, 1, -1, 0, -1, 2,
2219 -1, 0, -1, 1, -1, 0, -1, 3,
2220 -1, 0, -1, 1, -1, 0, -1, 2,
2221 -1, 0, -1, 1, -1, 0, -1, 4,
2222 -1, 0, -1, 1, -1, 0, -1, 2,
2223 -1, 0, -1, 1, -1, 0, -1, 3,
2224 -1, 0, -1, 1, -1, 0, -1, 2,
2225 -1, 0, -1, 1, -1, 0, -1, 7,
2230 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2233 * Now we also need to check the mapping array in a couple of ways.
2234 * 1) Did we move the cum-ack point?
2236 struct sctp_association *asoc;
2238 int last_all_ones = 0;
2239 int slide_from, slide_end, lgap, distance;
2240 uint32_t old_cumack, old_base, old_highest;
2241 unsigned char aux_array[64];
2247 old_cumack = asoc->cumulative_tsn;
2248 old_base = asoc->mapping_array_base_tsn;
2249 old_highest = asoc->highest_tsn_inside_map;
2250 if (asoc->mapping_array_size < 64)
2251 memcpy(aux_array, asoc->mapping_array,
2252 asoc->mapping_array_size);
2254 memcpy(aux_array, asoc->mapping_array, 64);
2257 * We could probably improve this a small bit by calculating the
2258 * offset of the current cum-ack as the starting point.
2261 for (i = 0; i < stcb->asoc.mapping_array_size; i++) {
2263 if (asoc->mapping_array[i] == 0xff) {
2267 /* there is a 0 bit */
2268 at += sctp_map_lookup_tab[asoc->mapping_array[i]];
2273 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2274 /* at is one off, since in the table a embedded -1 is present */
2277 if (compare_with_wrap(asoc->cumulative_tsn,
2278 asoc->highest_tsn_inside_map,
2281 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2282 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2284 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2285 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2286 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2289 if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2290 /* The complete array was completed by a single FR */
2291 /* higest becomes the cum-ack */
2294 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2295 /* clear the array */
2296 clr = (at >> 3) + 1;
2297 if (clr > asoc->mapping_array_size) {
2298 clr = asoc->mapping_array_size;
2300 memset(asoc->mapping_array, 0, clr);
2301 /* base becomes one ahead of the cum-ack */
2302 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2303 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2304 sctp_log_map(old_base, old_cumack, old_highest,
2305 SCTP_MAP_PREPARE_SLIDE);
2306 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2307 asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2309 } else if (at >= 8) {
2310 /* we can slide the mapping array down */
2311 /* Calculate the new byte postion we can move down */
2312 slide_from = at >> 3;
2314 * now calculate the ceiling of the move using our highest
2317 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2318 lgap = asoc->highest_tsn_inside_map -
2319 asoc->mapping_array_base_tsn;
2321 lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2322 asoc->highest_tsn_inside_map + 1;
2324 slide_end = lgap >> 3;
2325 if (slide_end < slide_from) {
2327 panic("impossible slide");
2329 printf("impossible slide?\n");
2333 distance = (slide_end - slide_from) + 1;
2334 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2335 sctp_log_map(old_base, old_cumack, old_highest,
2336 SCTP_MAP_PREPARE_SLIDE);
2337 sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2338 (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2340 if (distance + slide_from > asoc->mapping_array_size ||
2343 * Here we do NOT slide forward the array so that
2344 * hopefully when more data comes in to fill it up
2345 * we will be able to slide it forward. Really I
2346 * don't think this should happen :-0
2349 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2350 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2351 (uint32_t) asoc->mapping_array_size,
2352 SCTP_MAP_SLIDE_NONE);
2357 for (ii = 0; ii < distance; ii++) {
2358 asoc->mapping_array[ii] =
2359 asoc->mapping_array[slide_from + ii];
2361 for (ii = distance; ii <= slide_end; ii++) {
2362 asoc->mapping_array[ii] = 0;
2364 asoc->mapping_array_base_tsn += (slide_from << 3);
2365 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2366 sctp_log_map(asoc->mapping_array_base_tsn,
2367 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2368 SCTP_MAP_SLIDE_RESULT);
2373 * Now we need to see if we need to queue a sack or just start the
2374 * timer (if allowed).
2377 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2379 * Ok special case, in SHUTDOWN-SENT case. here we
2380 * maker sure SACK timer is off and instead send a
2381 * SHUTDOWN and a SACK
2383 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2384 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2385 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2387 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2388 sctp_send_sack(stcb);
2392 /* is there a gap now ? */
2393 is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2394 stcb->asoc.cumulative_tsn, MAX_TSN);
2397 * CMT DAC algorithm: increase number of packets
2398 * received since last ack
2400 stcb->asoc.cmt_dac_pkts_rcvd++;
2402 if ((stcb->asoc.send_sack == 1) || /* We need to send a
2404 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2406 (stcb->asoc.numduptsns) || /* we have dup's */
2407 (is_a_gap) || /* is still a gap */
2408 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2409 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2412 if ((sctp_cmt_on_off) && (sctp_cmt_use_dac) &&
2413 (stcb->asoc.send_sack == 0) &&
2414 (stcb->asoc.numduptsns == 0) &&
2415 (stcb->asoc.delayed_ack) &&
2416 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2419 * CMT DAC algorithm: With CMT,
2420 * delay acks even in the face of
2422 * reordering. Therefore, if acks that
2423 * do not have to be sent because of
2424 * the above reasons, will be
2425 * delayed. That is, acks that would
2426 * have been sent due to gap reports
2427 * will be delayed with DAC. Start
2428 * the delayed ack timer.
2430 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2431 stcb->sctp_ep, stcb, NULL);
2434 * Ok we must build a SACK since the
2435 * timer is pending, we got our
2436 * first packet OR there are gaps or
2439 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2440 sctp_send_sack(stcb);
2443 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2444 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2445 stcb->sctp_ep, stcb, NULL);
2453 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2455 struct sctp_tmit_chunk *chk;
2459 if (asoc->fragmented_delivery_inprogress) {
2460 sctp_service_reassembly(stcb, asoc);
2462 /* Can we proceed further, i.e. the PD-API is complete */
2463 if (asoc->fragmented_delivery_inprogress) {
2468 * Now is there some other chunk I can deliver from the reassembly
2472 chk = TAILQ_FIRST(&asoc->reasmqueue);
2474 asoc->size_on_reasm_queue = 0;
2475 asoc->cnt_on_reasm_queue = 0;
2478 nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2479 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2480 ((nxt_todel == chk->rec.data.stream_seq) ||
2481 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2483 * Yep the first one is here. We setup to start reception,
2484 * by backing down the TSN just in case we can't deliver.
2488 * Before we start though either all of the message should
2489 * be here or 1/4 the socket buffer max or nothing on the
2490 * delivery queue and something can be delivered.
2492 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2493 (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2494 asoc->fragmented_delivery_inprogress = 1;
2495 asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2496 asoc->str_of_pdapi = chk->rec.data.stream_number;
2497 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2498 asoc->pdapi_ppid = chk->rec.data.payloadtype;
2499 asoc->fragment_flags = chk->rec.data.rcv_flags;
2500 sctp_service_reassembly(stcb, asoc);
2501 if (asoc->fragmented_delivery_inprogress == 0) {
2509 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2510 struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2511 struct sctp_nets *net, uint32_t * high_tsn)
2513 struct sctp_data_chunk *ch, chunk_buf;
2514 struct sctp_association *asoc;
2515 int num_chunks = 0; /* number of control chunks processed */
2517 int chk_length, break_flag, last_chunk;
2518 int abort_flag = 0, was_a_gap = 0;
2522 sctp_set_rwnd(stcb, &stcb->asoc);
2525 SCTP_TCB_LOCK_ASSERT(stcb);
2527 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2528 stcb->asoc.cumulative_tsn, MAX_TSN)) {
2529 /* there was a gap before this data was processed */
2533 * setup where we got the last DATA packet from for any SACK that
2534 * may need to go out. Don't bump the net. This is done ONLY when a
2535 * chunk is assigned.
2537 asoc->last_data_chunk_from = net;
2540 * Now before we proceed we must figure out if this is a wasted
2541 * cluster... i.e. it is a small packet sent in and yet the driver
2542 * underneath allocated a full cluster for it. If so we must copy it
2543 * to a smaller mbuf and free up the cluster mbuf. This will help
2544 * with cluster starvation. Note for __Panda__ we don't do this
2545 * since it has clusters all the way down to 64 bytes.
2547 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2548 /* we only handle mbufs that are singletons.. not chains */
2549 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2551 /* ok lets see if we can copy the data up */
2554 /* get the pointers and copy */
2555 to = mtod(m, caddr_t *);
2556 from = mtod((*mm), caddr_t *);
2557 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2558 /* copy the length and free up the old */
2559 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2561 /* sucess, back copy */
2564 /* We are in trouble in the mbuf world .. yikes */
2568 /* get pointer to the first chunk header */
2569 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2570 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2575 * process all DATA chunks...
2577 *high_tsn = asoc->cumulative_tsn;
2579 asoc->data_pkts_seen++;
2580 while (stop_proc == 0) {
2581 /* validate chunk length */
2582 chk_length = ntohs(ch->ch.chunk_length);
2583 if (length - *offset < chk_length) {
2584 /* all done, mutulated chunk */
2588 if (ch->ch.chunk_type == SCTP_DATA) {
2589 if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2591 * Need to send an abort since we had a
2592 * invalid data chunk.
2594 struct mbuf *op_err;
2596 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2597 0, M_DONTWAIT, 1, MT_DATA);
2600 struct sctp_paramhdr *ph;
2603 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2604 (2 * sizeof(uint32_t));
2605 ph = mtod(op_err, struct sctp_paramhdr *);
2607 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2608 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2609 ippp = (uint32_t *) (ph + 1);
2610 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2612 *ippp = asoc->cumulative_tsn;
2615 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2616 sctp_abort_association(inp, stcb, m, iphlen, sh,
2620 #ifdef SCTP_AUDITING_ENABLED
2621 sctp_audit_log(0xB1, 0);
2623 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2628 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2629 chk_length, net, high_tsn, &abort_flag, &break_flag,
2638 * Set because of out of rwnd space and no
2639 * drop rep space left.
2645 /* not a data chunk in the data region */
2646 switch (ch->ch.chunk_type) {
2647 case SCTP_INITIATION:
2648 case SCTP_INITIATION_ACK:
2649 case SCTP_SELECTIVE_ACK:
2650 case SCTP_HEARTBEAT_REQUEST:
2651 case SCTP_HEARTBEAT_ACK:
2652 case SCTP_ABORT_ASSOCIATION:
2654 case SCTP_SHUTDOWN_ACK:
2655 case SCTP_OPERATION_ERROR:
2656 case SCTP_COOKIE_ECHO:
2657 case SCTP_COOKIE_ACK:
2660 case SCTP_SHUTDOWN_COMPLETE:
2661 case SCTP_AUTHENTICATION:
2662 case SCTP_ASCONF_ACK:
2663 case SCTP_PACKET_DROPPED:
2664 case SCTP_STREAM_RESET:
2665 case SCTP_FORWARD_CUM_TSN:
2668 * Now, what do we do with KNOWN chunks that
2669 * are NOT in the right place?
2671 * For now, I do nothing but ignore them. We
2672 * may later want to add sysctl stuff to
2673 * switch out and do either an ABORT() or
2674 * possibly process them.
2676 if (sctp_strict_data_order) {
2677 struct mbuf *op_err;
2679 op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2680 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0);
2685 /* unknown chunk type, use bit rules */
2686 if (ch->ch.chunk_type & 0x40) {
2687 /* Add a error report to the queue */
2689 struct sctp_paramhdr *phd;
2691 merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2693 phd = mtod(merr, struct sctp_paramhdr *);
2695 * We cheat and use param
2696 * type since we did not
2697 * bother to define a error
2698 * cause struct. They are
2699 * the same basic format
2700 * with different names.
2703 htons(SCTP_CAUSE_UNRECOG_CHUNK);
2705 htons(chk_length + sizeof(*phd));
2706 SCTP_BUF_LEN(merr) = sizeof(*phd);
2707 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2708 SCTP_SIZE32(chk_length),
2710 if (SCTP_BUF_NEXT(merr)) {
2711 sctp_queue_op_err(stcb, merr);
2717 if ((ch->ch.chunk_type & 0x80) == 0) {
2718 /* discard the rest of this packet */
2720 } /* else skip this bad chunk and
2723 }; /* switch of chunk type */
2725 *offset += SCTP_SIZE32(chk_length);
2726 if ((*offset >= length) || stop_proc) {
2727 /* no more data left in the mbuf chain */
2731 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2732 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2742 * we need to report rwnd overrun drops.
2744 sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2748 * Did we get data, if so update the time for auto-close and
2749 * give peer credit for being alive.
2751 SCTP_STAT_INCR(sctps_recvpktwithdata);
2752 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
2753 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2754 stcb->asoc.overall_error_count,
2756 SCTP_FROM_SCTP_INDATA,
2759 stcb->asoc.overall_error_count = 0;
2760 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2762 /* now service all of the reassm queue if needed */
2763 if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2764 sctp_service_queues(stcb, asoc);
2766 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2767 /* Assure that we ack right away */
2768 stcb->asoc.send_sack = 1;
2770 /* Start a sack timer or QUEUE a SACK for sending */
2771 if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
2772 (stcb->asoc.mapping_array[0] != 0xff)) {
2773 if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
2774 (stcb->asoc.delayed_ack == 0) ||
2775 (stcb->asoc.numduptsns) ||
2776 (stcb->asoc.send_sack == 1)) {
2777 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2778 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2780 sctp_send_sack(stcb);
2782 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2783 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2784 stcb->sctp_ep, stcb, NULL);
2788 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2797 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2798 struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2799 uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2800 int num_seg, int *ecn_seg_sums)
2802 /************************************************/
2803 /* process fragments and update sendqueue */
2804 /************************************************/
2805 struct sctp_sack *sack;
2806 struct sctp_gap_ack_block *frag, block;
2807 struct sctp_tmit_chunk *tp1;
2812 uint16_t frag_strt, frag_end, primary_flag_set;
2813 u_long last_frag_high;
2816 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
2818 if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
2819 primary_flag_set = 1;
2821 primary_flag_set = 0;
2825 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2826 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2827 *offset += sizeof(block);
2833 for (i = 0; i < num_seg; i++) {
2834 frag_strt = ntohs(frag->start);
2835 frag_end = ntohs(frag->end);
2836 /* some sanity checks on the fargment offsets */
2837 if (frag_strt > frag_end) {
2838 /* this one is malformed, skip */
2842 if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
2844 *biggest_tsn_acked = frag_end + last_tsn;
2846 /* mark acked dgs and find out the highestTSN being acked */
2848 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2850 /* save the locations of the last frags */
2851 last_frag_high = frag_end + last_tsn;
2854 * now lets see if we need to reset the queue due to
2855 * a out-of-order SACK fragment
2857 if (compare_with_wrap(frag_strt + last_tsn,
2858 last_frag_high, MAX_TSN)) {
2860 * if the new frag starts after the last TSN
2861 * frag covered, we are ok and this one is
2862 * beyond the last one
2867 * ok, they have reset us, so we need to
2868 * reset the queue this will cause extra
2869 * hunting but hey, they chose the
2870 * performance hit when they failed to order
2873 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2875 last_frag_high = frag_end + last_tsn;
2877 for (j = frag_strt + last_tsn; (compare_with_wrap((frag_end + last_tsn), j, MAX_TSN)); j++) {
2879 if (tp1->rec.data.doing_fast_retransmit)
2883 * CMT: CUCv2 algorithm. For each TSN being
2884 * processed from the sent queue, track the
2885 * next expected pseudo-cumack, or
2886 * rtx_pseudo_cumack, if required. Separate
2887 * cumack trackers for first transmissions,
2888 * and retransmissions.
2890 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2891 (tp1->snd_count == 1)) {
2892 tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2893 tp1->whoTo->find_pseudo_cumack = 0;
2895 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2896 (tp1->snd_count > 1)) {
2897 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2898 tp1->whoTo->find_rtx_pseudo_cumack = 0;
2900 if (tp1->rec.data.TSN_seq == j) {
2901 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2903 * must be held until
2907 * ECN Nonce: Add the nonce
2908 * value to the sender's
2911 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2913 * If it is less than RESEND, it is
2914 * now no-longer in flight.
2915 * Higher values may already be set
2916 * via previous Gap Ack Blocks...
2917 * i.e. ACKED or RESEND.
2919 if (compare_with_wrap(tp1->rec.data.TSN_seq,
2920 *biggest_newly_acked_tsn, MAX_TSN)) {
2921 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2930 * this_sack_highest_
2934 if (tp1->rec.data.chunk_was_revoked == 0)
2935 tp1->whoTo->saw_newack = 1;
2937 if (compare_with_wrap(tp1->rec.data.TSN_seq,
2938 tp1->whoTo->this_sack_highest_newack,
2940 tp1->whoTo->this_sack_highest_newack =
2941 tp1->rec.data.TSN_seq;
2946 * this_sack_lowest_n
2949 if (*this_sack_lowest_newack == 0) {
2950 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2951 sctp_log_sack(*this_sack_lowest_newack,
2953 tp1->rec.data.TSN_seq,
2956 SCTP_LOG_TSN_ACKED);
2958 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2963 * (rtx-)pseudo-cumac
2968 * (rtx-)pseudo-cumac
2970 * new_(rtx_)pseudo_c
2978 * (rtx-)pseudo-cumac
2986 if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2987 if (tp1->rec.data.chunk_was_revoked == 0) {
2988 tp1->whoTo->new_pseudo_cumack = 1;
2990 tp1->whoTo->find_pseudo_cumack = 1;
2992 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
2993 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2995 if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2996 if (tp1->rec.data.chunk_was_revoked == 0) {
2997 tp1->whoTo->new_pseudo_cumack = 1;
2999 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3001 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3002 sctp_log_sack(*biggest_newly_acked_tsn,
3004 tp1->rec.data.TSN_seq,
3007 SCTP_LOG_TSN_ACKED);
3009 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3010 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3011 tp1->whoTo->flight_size,
3013 (uintptr_t) tp1->whoTo,
3014 tp1->rec.data.TSN_seq);
3016 sctp_flight_size_decrease(tp1);
3017 sctp_total_flight_decrease(stcb, tp1);
3019 tp1->whoTo->net_ack += tp1->send_size;
3020 if (tp1->snd_count < 2) {
3026 tp1->whoTo->net_ack2 += tp1->send_size;
3033 sctp_calculate_rto(stcb,
3036 &tp1->sent_rcv_time,
3037 sctp_align_safe_nocopy);
3042 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3043 (*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3044 (*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3045 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3046 asoc->this_sack_highest_gap,
3048 asoc->this_sack_highest_gap =
3049 tp1->rec.data.TSN_seq;
3051 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3052 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3053 #ifdef SCTP_AUDITING_ENABLED
3054 sctp_audit_log(0xB2,
3055 (asoc->sent_queue_retran_cnt & 0x000000ff));
3060 * All chunks NOT UNSENT
3061 * fall through here and are
3064 tp1->sent = SCTP_DATAGRAM_MARKED;
3065 if (tp1->rec.data.chunk_was_revoked) {
3066 /* deflate the cwnd */
3067 tp1->whoTo->cwnd -= tp1->book_size;
3068 tp1->rec.data.chunk_was_revoked = 0;
3072 } /* if (tp1->TSN_seq == j) */
3073 if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
3077 tp1 = TAILQ_NEXT(tp1, sctp_next);
3078 } /* end while (tp1) */
3079 } /* end for (j = fragStart */
3080 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3081 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3082 *offset += sizeof(block);
3087 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3089 sctp_log_fr(*biggest_tsn_acked,
3090 *biggest_newly_acked_tsn,
3091 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3096 sctp_check_for_revoked(struct sctp_tcb *stcb,
3097 struct sctp_association *asoc, uint32_t cumack,
3098 u_long biggest_tsn_acked)
3100 struct sctp_tmit_chunk *tp1;
3101 int tot_revoked = 0;
3103 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3105 if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3108 * ok this guy is either ACK or MARKED. If it is
3109 * ACKED it has been previously acked but not this
3110 * time i.e. revoked. If it is MARKED it was ACK'ed
3113 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3118 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3119 /* it has been revoked */
3120 tp1->sent = SCTP_DATAGRAM_SENT;
3121 tp1->rec.data.chunk_was_revoked = 1;
3123 * We must add this stuff back in to assure
3124 * timers and such get started.
3126 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3127 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3128 tp1->whoTo->flight_size,
3130 (uintptr_t) tp1->whoTo,
3131 tp1->rec.data.TSN_seq);
3133 sctp_flight_size_increase(tp1);
3134 sctp_total_flight_increase(stcb, tp1);
3136 * We inflate the cwnd to compensate for our
3137 * artificial inflation of the flight_size.
3139 tp1->whoTo->cwnd += tp1->book_size;
3141 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3142 sctp_log_sack(asoc->last_acked_seq,
3144 tp1->rec.data.TSN_seq,
3147 SCTP_LOG_TSN_REVOKED);
3149 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3150 /* it has been re-acked in this SACK */
3151 tp1->sent = SCTP_DATAGRAM_ACKED;
3154 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3156 tp1 = TAILQ_NEXT(tp1, sctp_next);
3158 if (tot_revoked > 0) {
3160 * Setup the ecn nonce re-sync point. We do this since once
3161 * data is revoked we begin to retransmit things, which do
3162 * NOT have the ECN bits set. This means we are now out of
3163 * sync and must wait until we get back in sync with the
3164 * peer to check ECN bits.
3166 tp1 = TAILQ_FIRST(&asoc->send_queue);
3168 asoc->nonce_resync_tsn = asoc->sending_seq;
3170 asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3172 asoc->nonce_wait_for_ecne = 0;
3173 asoc->nonce_sum_check = 0;
3178 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3179 u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3181 struct sctp_tmit_chunk *tp1;
3182 int strike_flag = 0;
3184 int tot_retrans = 0;
3185 uint32_t sending_seq;
3186 struct sctp_nets *net;
3187 int num_dests_sacked = 0;
3190 * select the sending_seq, this is either the next thing ready to be
3191 * sent but not transmitted, OR, the next seq we assign.
3193 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3195 sending_seq = asoc->sending_seq;
3197 sending_seq = tp1->rec.data.TSN_seq;
3200 /* CMT DAC algo: finding out if SACK is a mixed SACK */
3201 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3202 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3203 if (net->saw_newack)
3207 if (stcb->asoc.peer_supports_prsctp) {
3208 (void)SCTP_GETTIME_TIMEVAL(&now);
3210 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3213 if (tp1->no_fr_allowed) {
3214 /* this one had a timeout or something */
3215 tp1 = TAILQ_NEXT(tp1, sctp_next);
3218 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3219 if (tp1->sent < SCTP_DATAGRAM_RESEND)
3220 sctp_log_fr(biggest_tsn_newly_acked,
3221 tp1->rec.data.TSN_seq,
3223 SCTP_FR_LOG_CHECK_STRIKE);
3225 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3227 tp1->sent == SCTP_DATAGRAM_UNSENT) {
3231 if (stcb->asoc.peer_supports_prsctp) {
3232 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3233 /* Is it expired? */
3235 (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3237 /* Yes so drop it */
3238 if (tp1->data != NULL) {
3239 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3240 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3241 &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3243 tp1 = TAILQ_NEXT(tp1, sctp_next);
3247 if ((PR_SCTP_RTX_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3248 /* Has it been retransmitted tv_sec times? */
3249 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3250 /* Yes, so drop it */
3251 if (tp1->data != NULL) {
3252 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3253 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3254 &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3256 tp1 = TAILQ_NEXT(tp1, sctp_next);
3261 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3262 asoc->this_sack_highest_gap, MAX_TSN)) {
3263 /* we are beyond the tsn in the sack */
3266 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3267 /* either a RESEND, ACKED, or MARKED */
3269 tp1 = TAILQ_NEXT(tp1, sctp_next);
3273 * CMT : SFR algo (covers part of DAC and HTNA as well)
3275 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3277 * No new acks were receieved for data sent to this
3278 * dest. Therefore, according to the SFR algo for
3279 * CMT, no data sent to this dest can be marked for
3280 * FR using this SACK.
3282 tp1 = TAILQ_NEXT(tp1, sctp_next);
3284 } else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3285 tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3287 * CMT: New acks were receieved for data sent to
3288 * this dest. But no new acks were seen for data
3289 * sent after tp1. Therefore, according to the SFR
3290 * algo for CMT, tp1 cannot be marked for FR using
3291 * this SACK. This step covers part of the DAC algo
3292 * and the HTNA algo as well.
3294 tp1 = TAILQ_NEXT(tp1, sctp_next);
3298 * Here we check to see if we were have already done a FR
3299 * and if so we see if the biggest TSN we saw in the sack is
3300 * smaller than the recovery point. If so we don't strike
3301 * the tsn... otherwise we CAN strike the TSN.
3304 * @@@ JRI: Check for CMT if (accum_moved &&
3305 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3308 if (accum_moved && asoc->fast_retran_loss_recovery) {
3310 * Strike the TSN if in fast-recovery and cum-ack
3313 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3314 sctp_log_fr(biggest_tsn_newly_acked,
3315 tp1->rec.data.TSN_seq,
3317 SCTP_FR_LOG_STRIKE_CHUNK);
3319 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3322 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3324 * CMT DAC algorithm: If SACK flag is set to
3325 * 0, then lowest_newack test will not pass
3326 * because it would have been set to the
3327 * cumack earlier. If not already to be
3328 * rtx'd, If not a mixed sack and if tp1 is
3329 * not between two sacked TSNs, then mark by
3330 * one more. NOTE that we are marking by one
3331 * additional time since the SACK DAC flag
3332 * indicates that two packets have been
3333 * received after this missing TSN.
3335 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3336 compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3337 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3338 sctp_log_fr(16 + num_dests_sacked,
3339 tp1->rec.data.TSN_seq,
3341 SCTP_FR_LOG_STRIKE_CHUNK);
3346 } else if (tp1->rec.data.doing_fast_retransmit) {
3348 * For those that have done a FR we must take
3349 * special consideration if we strike. I.e the
3350 * biggest_newly_acked must be higher than the
3351 * sending_seq at the time we did the FR.
3354 #ifdef SCTP_FR_TO_ALTERNATE
3356 * If FR's go to new networks, then we must only do
3357 * this for singly homed asoc's. However if the FR's
3358 * go to the same network (Armando's work) then its
3359 * ok to FR multiple times.
3367 if ((compare_with_wrap(biggest_tsn_newly_acked,
3368 tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3369 (biggest_tsn_newly_acked ==
3370 tp1->rec.data.fast_retran_tsn)) {
3372 * Strike the TSN, since this ack is
3373 * beyond where things were when we
3376 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3377 sctp_log_fr(biggest_tsn_newly_acked,
3378 tp1->rec.data.TSN_seq,
3380 SCTP_FR_LOG_STRIKE_CHUNK);
3382 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3386 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3388 * CMT DAC algorithm: If
3389 * SACK flag is set to 0,
3390 * then lowest_newack test
3391 * will not pass because it
3392 * would have been set to
3393 * the cumack earlier. If
3394 * not already to be rtx'd,
3395 * If not a mixed sack and
3396 * if tp1 is not between two
3397 * sacked TSNs, then mark by
3398 * one more. NOTE that we
3399 * are marking by one
3400 * additional time since the
3401 * SACK DAC flag indicates
3402 * that two packets have
3403 * been received after this
3406 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3407 (num_dests_sacked == 1) &&
3408 compare_with_wrap(this_sack_lowest_newack,
3409 tp1->rec.data.TSN_seq, MAX_TSN)) {
3410 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3411 sctp_log_fr(32 + num_dests_sacked,
3412 tp1->rec.data.TSN_seq,
3414 SCTP_FR_LOG_STRIKE_CHUNK);
3416 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3424 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3427 } else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3428 biggest_tsn_newly_acked, MAX_TSN)) {
3430 * We don't strike these: This is the HTNA
3431 * algorithm i.e. we don't strike If our TSN is
3432 * larger than the Highest TSN Newly Acked.
3436 /* Strike the TSN */
3437 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3438 sctp_log_fr(biggest_tsn_newly_acked,
3439 tp1->rec.data.TSN_seq,
3441 SCTP_FR_LOG_STRIKE_CHUNK);
3443 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3446 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3448 * CMT DAC algorithm: If SACK flag is set to
3449 * 0, then lowest_newack test will not pass
3450 * because it would have been set to the
3451 * cumack earlier. If not already to be
3452 * rtx'd, If not a mixed sack and if tp1 is
3453 * not between two sacked TSNs, then mark by
3454 * one more. NOTE that we are marking by one
3455 * additional time since the SACK DAC flag
3456 * indicates that two packets have been
3457 * received after this missing TSN.
3459 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3460 compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3461 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3462 sctp_log_fr(48 + num_dests_sacked,
3463 tp1->rec.data.TSN_seq,
3465 SCTP_FR_LOG_STRIKE_CHUNK);
3471 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3472 /* Increment the count to resend */
3473 struct sctp_nets *alt;
3475 /* printf("OK, we are now ready to FR this guy\n"); */
3476 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3477 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3481 /* This is a subsequent FR */
3482 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3484 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3485 if (sctp_cmt_on_off) {
3487 * CMT: Using RTX_SSTHRESH policy for CMT.
3488 * If CMT is being used, then pick dest with
3489 * largest ssthresh for any retransmission.
3491 tp1->no_fr_allowed = 1;
3493 /* sa_ignore NO_NULL_CHK */
3494 if (sctp_cmt_on_off && sctp_cmt_pf) {
3496 * JRS 5/18/07 - If CMT PF is on,
3497 * use the PF version of
3500 alt = sctp_find_alternate_net(stcb, alt, 2);
3503 * JRS 5/18/07 - If only CMT is on,
3504 * use the CMT version of
3507 /* sa_ignore NO_NULL_CHK */
3508 alt = sctp_find_alternate_net(stcb, alt, 1);
3514 * CUCv2: If a different dest is picked for
3515 * the retransmission, then new
3516 * (rtx-)pseudo_cumack needs to be tracked
3517 * for orig dest. Let CUCv2 track new (rtx-)
3518 * pseudo-cumack always.
3521 tp1->whoTo->find_pseudo_cumack = 1;
3522 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3524 } else {/* CMT is OFF */
3526 #ifdef SCTP_FR_TO_ALTERNATE
3527 /* Can we find an alternate? */
3528 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3531 * default behavior is to NOT retransmit
3532 * FR's to an alternate. Armando Caro's
3533 * paper details why.
3539 tp1->rec.data.doing_fast_retransmit = 1;
3541 /* mark the sending seq for possible subsequent FR's */
3543 * printf("Marking TSN for FR new value %x\n",
3544 * (uint32_t)tpi->rec.data.TSN_seq);
3546 if (TAILQ_EMPTY(&asoc->send_queue)) {
3548 * If the queue of send is empty then its
3549 * the next sequence number that will be
3550 * assigned so we subtract one from this to
3551 * get the one we last sent.
3553 tp1->rec.data.fast_retran_tsn = sending_seq;
3556 * If there are chunks on the send queue
3557 * (unsent data that has made it from the
3558 * stream queues but not out the door, we
3559 * take the first one (which will have the
3560 * lowest TSN) and subtract one to get the
3563 struct sctp_tmit_chunk *ttt;
3565 ttt = TAILQ_FIRST(&asoc->send_queue);
3566 tp1->rec.data.fast_retran_tsn =
3567 ttt->rec.data.TSN_seq;
3572 * this guy had a RTO calculation pending on
3577 /* fix counts and things */
3578 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3579 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3580 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3582 (uintptr_t) tp1->whoTo,
3583 tp1->rec.data.TSN_seq);
3586 tp1->whoTo->net_ack++;
3587 sctp_flight_size_decrease(tp1);
3589 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
3590 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3591 asoc->peers_rwnd, tp1->send_size, sctp_peer_chunk_oh);
3593 /* add back to the rwnd */
3594 asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
3596 /* remove from the total flight */
3597 sctp_total_flight_decrease(stcb, tp1);
3598 if (alt != tp1->whoTo) {
3599 /* yes, there is an alternate. */
3600 sctp_free_remote_addr(tp1->whoTo);
3601 /* sa_ignore FREED_MEMORY */
3603 atomic_add_int(&alt->ref_count, 1);
3606 tp1 = TAILQ_NEXT(tp1, sctp_next);
3609 if (tot_retrans > 0) {
3611 * Setup the ecn nonce re-sync point. We do this since once
3612 * we go to FR something we introduce a Karn's rule scenario
3613 * and won't know the totals for the ECN bits.
3615 asoc->nonce_resync_tsn = sending_seq;
3616 asoc->nonce_wait_for_ecne = 0;
3617 asoc->nonce_sum_check = 0;
3621 struct sctp_tmit_chunk *
3622 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3623 struct sctp_association *asoc)
3625 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3629 if (asoc->peer_supports_prsctp == 0) {
3632 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3634 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3635 tp1->sent != SCTP_DATAGRAM_RESEND) {
3636 /* no chance to advance, out of here */
3639 if (!PR_SCTP_ENABLED(tp1->flags)) {
3641 * We can't fwd-tsn past any that are reliable aka
3642 * retransmitted until the asoc fails.
3647 (void)SCTP_GETTIME_TIMEVAL(&now);
3650 tp2 = TAILQ_NEXT(tp1, sctp_next);
3652 * now we got a chunk which is marked for another
3653 * retransmission to a PR-stream but has run out its chances
3654 * already maybe OR has been marked to skip now. Can we skip
3655 * it if its a resend?
3657 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3658 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3660 * Now is this one marked for resend and its time is
3663 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3664 /* Yes so drop it */
3666 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3667 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3668 &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3672 * No, we are done when hit one for resend
3673 * whos time as not expired.
3679 * Ok now if this chunk is marked to drop it we can clean up
3680 * the chunk, advance our peer ack point and we can check
3683 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3684 /* advance PeerAckPoint goes forward */
3685 asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3688 * we don't want to de-queue it here. Just wait for
3689 * the next peer SACK to come with a new cumTSN and
3690 * then the chunk will be droped in the normal
3694 sctp_free_bufspace(stcb, asoc, tp1, 1);
3696 * Maybe there should be another
3699 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3700 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3701 tp1, SCTP_SO_NOT_LOCKED);
3702 sctp_m_freem(tp1->data);
3704 if (stcb->sctp_socket) {
3705 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3708 so = SCTP_INP_SO(stcb->sctp_ep);
3709 atomic_add_int(&stcb->asoc.refcnt, 1);
3710 SCTP_TCB_UNLOCK(stcb);
3711 SCTP_SOCKET_LOCK(so, 1);
3712 SCTP_TCB_LOCK(stcb);
3713 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3714 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3716 * assoc was freed while we
3719 SCTP_SOCKET_UNLOCK(so, 1);
3723 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
3724 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3725 SCTP_SOCKET_UNLOCK(so, 1);
3727 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
3728 sctp_wakeup_log(stcb, tp1->rec.data.TSN_seq, 1, SCTP_WAKESND_FROM_FWDTSN);
3734 * If it is still in RESEND we can advance no
3740 * If we hit here we just dumped tp1, move to next tsn on
3749 sctp_fs_audit(struct sctp_association *asoc)
3751 struct sctp_tmit_chunk *chk;
3752 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3754 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3755 if (chk->sent < SCTP_DATAGRAM_RESEND) {
3757 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3759 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3761 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3768 if ((inflight > 0) || (inbetween > 0)) {
3770 panic("Flight size-express incorrect? \n");
3772 SCTP_PRINTF("Flight size-express incorrect inflight:%d inbetween:%d\n",
3773 inflight, inbetween);
3780 sctp_window_probe_recovery(struct sctp_tcb *stcb,
3781 struct sctp_association *asoc,
3782 struct sctp_nets *net,
3783 struct sctp_tmit_chunk *tp1)
3785 struct sctp_tmit_chunk *chk;
3787 /* First setup this one and get it moved back */
3788 tp1->sent = SCTP_DATAGRAM_UNSENT;
3789 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3790 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3791 tp1->whoTo->flight_size,
3793 (uintptr_t) tp1->whoTo,
3794 tp1->rec.data.TSN_seq);
3796 sctp_flight_size_decrease(tp1);
3797 sctp_total_flight_decrease(stcb, tp1);
3798 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3799 TAILQ_INSERT_HEAD(&asoc->send_queue, tp1, sctp_next);
3800 asoc->sent_queue_cnt--;
3801 asoc->send_queue_cnt++;
3803 * Now all guys marked for RESEND on the sent_queue must be moved
3806 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3807 if (chk->sent == SCTP_DATAGRAM_RESEND) {
3808 /* Another chunk to move */
3809 chk->sent = SCTP_DATAGRAM_UNSENT;
3810 /* It should not be in flight */
3811 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3812 TAILQ_INSERT_AFTER(&asoc->send_queue, tp1, chk, sctp_next);
3813 asoc->sent_queue_cnt--;
3814 asoc->send_queue_cnt++;
3815 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3821 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3822 uint32_t rwnd, int nonce_sum_flag, int *abort_now)
3824 struct sctp_nets *net;
3825 struct sctp_association *asoc;
3826 struct sctp_tmit_chunk *tp1, *tp2;
3828 int win_probe_recovery = 0;
3829 int win_probe_recovered = 0;
3830 int j, done_once = 0;
3832 if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3833 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3834 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3836 SCTP_TCB_LOCK_ASSERT(stcb);
3837 #ifdef SCTP_ASOCLOG_OF_TSNS
3838 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3839 stcb->asoc.cumack_log_at++;
3840 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3841 stcb->asoc.cumack_log_at = 0;
3845 old_rwnd = asoc->peers_rwnd;
3846 if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
3849 } else if (asoc->last_acked_seq == cumack) {
3850 /* Window update sack */
3851 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3852 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * 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) {
3862 /* First setup for CC stuff */
3863 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3864 net->prev_cwnd = net->cwnd;
3869 * CMT: Reset CUC and Fast recovery algo variables before
3872 net->new_pseudo_cumack = 0;
3873 net->will_exit_fast_recovery = 0;
3875 if (sctp_strict_sacks) {
3878 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3879 tp1 = TAILQ_LAST(&asoc->sent_queue,
3880 sctpchunk_listhead);
3881 send_s = tp1->rec.data.TSN_seq + 1;
3883 send_s = asoc->sending_seq;
3885 if ((cumack == send_s) ||
3886 compare_with_wrap(cumack, send_s, MAX_TSN)) {
3892 panic("Impossible sack 1");
3896 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3897 0, M_DONTWAIT, 1, MT_DATA);
3899 struct sctp_paramhdr *ph;
3902 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3904 ph = mtod(oper, struct sctp_paramhdr *);
3905 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3906 ph->param_length = htons(SCTP_BUF_LEN(oper));
3907 ippp = (uint32_t *) (ph + 1);
3908 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3910 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3911 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3916 asoc->this_sack_highest_gap = cumack;
3917 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
3918 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3919 stcb->asoc.overall_error_count,
3921 SCTP_FROM_SCTP_INDATA,
3924 stcb->asoc.overall_error_count = 0;
3925 if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
3926 /* process the new consecutive TSN first */
3927 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3929 tp2 = TAILQ_NEXT(tp1, sctp_next);
3930 if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
3932 cumack == tp1->rec.data.TSN_seq) {
3933 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3934 printf("Warning, an unsent is now acked?\n");
3937 * ECN Nonce: Add the nonce to the sender's
3940 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
3941 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3943 * If it is less than ACKED, it is
3944 * now no-longer in flight. Higher
3945 * values may occur during marking
3947 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3948 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3949 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3950 tp1->whoTo->flight_size,
3952 (uintptr_t) tp1->whoTo,
3953 tp1->rec.data.TSN_seq);
3955 sctp_flight_size_decrease(tp1);
3956 /* sa_ignore NO_NULL_CHK */
3957 sctp_total_flight_decrease(stcb, tp1);
3959 tp1->whoTo->net_ack += tp1->send_size;
3960 if (tp1->snd_count < 2) {
3962 * True non-retransmited
3965 tp1->whoTo->net_ack2 +=
3968 /* update RTO too? */
3975 sctp_calculate_rto(stcb,
3977 &tp1->sent_rcv_time,
3978 sctp_align_safe_nocopy);
3983 * CMT: CUCv2 algorithm. From the
3984 * cumack'd TSNs, for each TSN being
3985 * acked for the first time, set the
3986 * following variables for the
3987 * corresp destination.
3988 * new_pseudo_cumack will trigger a
3990 * find_(rtx_)pseudo_cumack will
3991 * trigger search for the next
3992 * expected (rtx-)pseudo-cumack.
3994 tp1->whoTo->new_pseudo_cumack = 1;
3995 tp1->whoTo->find_pseudo_cumack = 1;
3996 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3998 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
3999 /* sa_ignore NO_NULL_CHK */
4000 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4003 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4004 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4006 if (tp1->rec.data.chunk_was_revoked) {
4007 /* deflate the cwnd */
4008 tp1->whoTo->cwnd -= tp1->book_size;
4009 tp1->rec.data.chunk_was_revoked = 0;
4011 tp1->sent = SCTP_DATAGRAM_ACKED;
4012 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4014 /* sa_ignore NO_NULL_CHK */
4015 sctp_free_bufspace(stcb, asoc, tp1, 1);
4016 sctp_m_freem(tp1->data);
4018 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4019 sctp_log_sack(asoc->last_acked_seq,
4021 tp1->rec.data.TSN_seq,
4024 SCTP_LOG_FREE_SENT);
4027 asoc->sent_queue_cnt--;
4028 sctp_free_a_chunk(stcb, tp1);
4036 /* sa_ignore NO_NULL_CHK */
4037 if (stcb->sctp_socket) {
4038 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4043 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4044 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4045 /* sa_ignore NO_NULL_CHK */
4046 sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4048 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4049 so = SCTP_INP_SO(stcb->sctp_ep);
4050 atomic_add_int(&stcb->asoc.refcnt, 1);
4051 SCTP_TCB_UNLOCK(stcb);
4052 SCTP_SOCKET_LOCK(so, 1);
4053 SCTP_TCB_LOCK(stcb);
4054 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4055 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4056 /* assoc was freed while we were unlocked */
4057 SCTP_SOCKET_UNLOCK(so, 1);
4061 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4062 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4063 SCTP_SOCKET_UNLOCK(so, 1);
4066 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4067 sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4071 /* JRS - Use the congestion control given in the CC module */
4072 if (asoc->last_acked_seq != cumack)
4073 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4075 asoc->last_acked_seq = cumack;
4077 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4078 /* nothing left in-flight */
4079 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4080 net->flight_size = 0;
4081 net->partial_bytes_acked = 0;
4083 asoc->total_flight = 0;
4084 asoc->total_flight_count = 0;
4086 /* Fix up the a-p-a-p for future PR-SCTP sends */
4087 if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4088 asoc->advanced_peer_ack_point = cumack;
4090 /* ECN Nonce updates */
4091 if (asoc->ecn_nonce_allowed) {
4092 if (asoc->nonce_sum_check) {
4093 if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4094 if (asoc->nonce_wait_for_ecne == 0) {
4095 struct sctp_tmit_chunk *lchk;
4097 lchk = TAILQ_FIRST(&asoc->send_queue);
4098 asoc->nonce_wait_for_ecne = 1;
4100 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4102 asoc->nonce_wait_tsn = asoc->sending_seq;
4105 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4106 (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4108 * Misbehaving peer. We need
4109 * to react to this guy
4111 asoc->ecn_allowed = 0;
4112 asoc->ecn_nonce_allowed = 0;
4117 /* See if Resynchronization Possible */
4118 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4119 asoc->nonce_sum_check = 1;
4121 * now we must calculate what the base is.
4122 * We do this based on two things, we know
4123 * the total's for all the segments
4124 * gap-acked in the SACK (none), We also
4125 * know the SACK's nonce sum, its in
4126 * nonce_sum_flag. So we can build a truth
4127 * table to back-calculate the new value of
4128 * asoc->nonce_sum_expect_base:
4130 * SACK-flag-Value Seg-Sums Base 0 0 0
4134 asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4139 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4140 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4141 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4142 /* SWS sender side engages */
4143 asoc->peers_rwnd = 0;
4145 if (asoc->peers_rwnd > old_rwnd) {
4146 win_probe_recovery = 1;
4148 /* Now assure a timer where data is queued at */
4151 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4152 if (win_probe_recovery && (net->window_probe)) {
4153 net->window_probe = 0;
4154 win_probe_recovered = 1;
4156 * Find first chunk that was used with window probe
4157 * and clear the sent
4159 /* sa_ignore FREED_MEMORY */
4160 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4161 if (tp1->window_probe) {
4162 /* move back to data send queue */
4163 sctp_window_probe_recovery(stcb, asoc, net, tp1);
4168 if (net->flight_size) {
4171 if (net->RTO == 0) {
4172 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4174 to_ticks = MSEC_TO_TICKS(net->RTO);
4177 (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4178 sctp_timeout_handler, &net->rxt_timer);
4180 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4181 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4183 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4185 if (sctp_early_fr) {
4186 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4187 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4188 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4189 SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4195 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4196 (asoc->sent_queue_retran_cnt == 0) &&
4197 (win_probe_recovered == 0) &&
4199 /* huh, this should not happen */
4200 sctp_fs_audit(asoc);
4201 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4202 net->flight_size = 0;
4204 asoc->total_flight = 0;
4205 asoc->total_flight_count = 0;
4206 asoc->sent_queue_retran_cnt = 0;
4207 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4208 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4209 sctp_flight_size_increase(tp1);
4210 sctp_total_flight_increase(stcb, tp1);
4211 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4212 asoc->sent_queue_retran_cnt++;
4218 /**********************************/
4219 /* Now what about shutdown issues */
4220 /**********************************/
4221 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4222 /* nothing left on sendqueue.. consider done */
4224 if ((asoc->stream_queue_cnt == 1) &&
4225 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4226 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4227 (asoc->locked_on_sending)
4229 struct sctp_stream_queue_pending *sp;
4232 * I may be in a state where we got all across.. but
4233 * cannot write more due to a shutdown... we abort
4234 * since the user did not indicate EOR in this case.
4235 * The sp will be cleaned during free of the asoc.
4237 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4239 if ((sp) && (sp->length == 0)) {
4240 /* Let cleanup code purge it */
4241 if (sp->msg_is_complete) {
4242 asoc->stream_queue_cnt--;
4244 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4245 asoc->locked_on_sending = NULL;
4246 asoc->stream_queue_cnt--;
4250 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4251 (asoc->stream_queue_cnt == 0)) {
4252 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4253 /* Need to abort here */
4259 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4260 0, M_DONTWAIT, 1, MT_DATA);
4262 struct sctp_paramhdr *ph;
4265 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4267 ph = mtod(oper, struct sctp_paramhdr *);
4268 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4269 ph->param_length = htons(SCTP_BUF_LEN(oper));
4270 ippp = (uint32_t *) (ph + 1);
4271 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4273 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4274 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4276 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4277 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4278 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4280 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4281 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4282 sctp_stop_timers_for_shutdown(stcb);
4283 sctp_send_shutdown(stcb,
4284 stcb->asoc.primary_destination);
4285 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4286 stcb->sctp_ep, stcb, asoc->primary_destination);
4287 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4288 stcb->sctp_ep, stcb, asoc->primary_destination);
4290 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4291 (asoc->stream_queue_cnt == 0)) {
4292 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4295 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4296 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4297 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4298 sctp_send_shutdown_ack(stcb,
4299 stcb->asoc.primary_destination);
4301 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4302 stcb->sctp_ep, stcb, asoc->primary_destination);
4305 if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
4306 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4308 stcb->asoc.peers_rwnd,
4309 stcb->asoc.total_flight,
4310 stcb->asoc.total_output_queue_size);
4315 sctp_handle_sack(struct mbuf *m, int offset,
4316 struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4317 struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4319 struct sctp_association *asoc;
4320 struct sctp_sack *sack;
4321 struct sctp_tmit_chunk *tp1, *tp2;
4322 uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4323 this_sack_lowest_newack;
4324 uint32_t sav_cum_ack;
4325 uint16_t num_seg, num_dup;
4326 uint16_t wake_him = 0;
4327 unsigned int sack_length;
4328 uint32_t send_s = 0;
4330 int accum_moved = 0;
4331 int will_exit_fast_recovery = 0;
4332 uint32_t a_rwnd, old_rwnd;
4333 int win_probe_recovery = 0;
4334 int win_probe_recovered = 0;
4335 struct sctp_nets *net = NULL;
4336 int nonce_sum_flag, ecn_seg_sums = 0;
4338 uint8_t reneged_all = 0;
4339 uint8_t cmt_dac_flag;
4342 * we take any chance we can to service our queues since we cannot
4343 * get awoken when the socket is read from :<
4346 * Now perform the actual SACK handling: 1) Verify that it is not an
4347 * old sack, if so discard. 2) If there is nothing left in the send
4348 * queue (cum-ack is equal to last acked) then you have a duplicate
4349 * too, update any rwnd change and verify no timers are running.
4350 * then return. 3) Process any new consequtive data i.e. cum-ack
4351 * moved process these first and note that it moved. 4) Process any
4352 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4353 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4354 * sync up flightsizes and things, stop all timers and also check
4355 * for shutdown_pending state. If so then go ahead and send off the
4356 * shutdown. If in shutdown recv, send off the shutdown-ack and
4357 * start that timer, Ret. 9) Strike any non-acked things and do FR
4358 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4359 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4360 * if in shutdown_recv state.
4362 SCTP_TCB_LOCK_ASSERT(stcb);
4365 this_sack_lowest_newack = 0;
4367 sack_length = (unsigned int)sack_len;
4369 SCTP_STAT_INCR(sctps_slowpath_sack);
4370 nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4371 cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4372 #ifdef SCTP_ASOCLOG_OF_TSNS
4373 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4374 stcb->asoc.cumack_log_at++;
4375 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4376 stcb->asoc.cumack_log_at = 0;
4379 num_seg = ntohs(sack->num_gap_ack_blks);
4382 if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4383 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4384 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4387 cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4388 num_dup = ntohs(sack->num_dup_tsns);
4390 old_rwnd = stcb->asoc.peers_rwnd;
4391 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
4392 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4393 stcb->asoc.overall_error_count,
4395 SCTP_FROM_SCTP_INDATA,
4398 stcb->asoc.overall_error_count = 0;
4400 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4401 sctp_log_sack(asoc->last_acked_seq,
4408 if ((num_dup) && (sctp_logging_level & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4409 int off_to_dup, iii;
4410 uint32_t *dupdata, dblock;
4412 off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4413 if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4414 dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4415 sizeof(uint32_t), (uint8_t *) & dblock);
4416 off_to_dup += sizeof(uint32_t);
4418 for (iii = 0; iii < num_dup; iii++) {
4419 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4420 dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4421 sizeof(uint32_t), (uint8_t *) & dblock);
4422 if (dupdata == NULL)
4424 off_to_dup += sizeof(uint32_t);
4428 SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4429 off_to_dup, num_dup, sack_length, num_seg);
4432 if (sctp_strict_sacks) {
4434 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4435 tp1 = TAILQ_LAST(&asoc->sent_queue,
4436 sctpchunk_listhead);
4437 send_s = tp1->rec.data.TSN_seq + 1;
4439 send_s = asoc->sending_seq;
4441 if (cum_ack == send_s ||
4442 compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4449 panic("Impossible sack 1");
4454 * no way, we have not even sent this TSN out yet.
4455 * Peer is hopelessly messed up with us.
4460 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4461 0, M_DONTWAIT, 1, MT_DATA);
4463 struct sctp_paramhdr *ph;
4466 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4468 ph = mtod(oper, struct sctp_paramhdr *);
4469 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4470 ph->param_length = htons(SCTP_BUF_LEN(oper));
4471 ippp = (uint32_t *) (ph + 1);
4472 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4474 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4475 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4480 /**********************/
4481 /* 1) check the range */
4482 /**********************/
4483 if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4484 /* acking something behind */
4487 sav_cum_ack = asoc->last_acked_seq;
4489 /* update the Rwnd of the peer */
4490 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4491 TAILQ_EMPTY(&asoc->send_queue) &&
4492 (asoc->stream_queue_cnt == 0)
4494 /* nothing left on send/sent and strmq */
4495 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4496 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4497 asoc->peers_rwnd, 0, 0, a_rwnd);
4499 asoc->peers_rwnd = a_rwnd;
4500 if (asoc->sent_queue_retran_cnt) {
4501 asoc->sent_queue_retran_cnt = 0;
4503 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4504 /* SWS sender side engages */
4505 asoc->peers_rwnd = 0;
4507 /* stop any timers */
4508 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4509 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4510 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4511 if (sctp_early_fr) {
4512 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4513 SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4514 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4515 SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4518 net->partial_bytes_acked = 0;
4519 net->flight_size = 0;
4521 asoc->total_flight = 0;
4522 asoc->total_flight_count = 0;
4526 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4527 * things. The total byte count acked is tracked in netAckSz AND
4528 * netAck2 is used to track the total bytes acked that are un-
4529 * amibguious and were never retransmitted. We track these on a per
4530 * destination address basis.
4532 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4533 net->prev_cwnd = net->cwnd;
4538 * CMT: Reset CUC and Fast recovery algo variables before
4541 net->new_pseudo_cumack = 0;
4542 net->will_exit_fast_recovery = 0;
4544 /* process the new consecutive TSN first */
4545 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4547 if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4549 last_tsn == tp1->rec.data.TSN_seq) {
4550 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4552 * ECN Nonce: Add the nonce to the sender's
4555 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4557 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4559 * If it is less than ACKED, it is
4560 * now no-longer in flight. Higher
4561 * values may occur during marking
4563 if ((tp1->whoTo->dest_state &
4564 SCTP_ADDR_UNCONFIRMED) &&
4565 (tp1->snd_count < 2)) {
4567 * If there was no retran
4568 * and the address is
4569 * un-confirmed and we sent
4571 * sacked.. its confirmed,
4574 tp1->whoTo->dest_state &=
4575 ~SCTP_ADDR_UNCONFIRMED;
4577 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4578 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4579 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4580 tp1->whoTo->flight_size,
4582 (uintptr_t) tp1->whoTo,
4583 tp1->rec.data.TSN_seq);
4585 sctp_flight_size_decrease(tp1);
4586 sctp_total_flight_decrease(stcb, tp1);
4588 tp1->whoTo->net_ack += tp1->send_size;
4590 /* CMT SFR and DAC algos */
4591 this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4592 tp1->whoTo->saw_newack = 1;
4594 if (tp1->snd_count < 2) {
4596 * True non-retransmited
4599 tp1->whoTo->net_ack2 +=
4602 /* update RTO too? */
4605 sctp_calculate_rto(stcb,
4607 &tp1->sent_rcv_time,
4608 sctp_align_safe_nocopy);
4613 * CMT: CUCv2 algorithm. From the
4614 * cumack'd TSNs, for each TSN being
4615 * acked for the first time, set the
4616 * following variables for the
4617 * corresp destination.
4618 * new_pseudo_cumack will trigger a
4620 * find_(rtx_)pseudo_cumack will
4621 * trigger search for the next
4622 * expected (rtx-)pseudo-cumack.
4624 tp1->whoTo->new_pseudo_cumack = 1;
4625 tp1->whoTo->find_pseudo_cumack = 1;
4626 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4629 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4630 sctp_log_sack(asoc->last_acked_seq,
4632 tp1->rec.data.TSN_seq,
4635 SCTP_LOG_TSN_ACKED);
4637 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4638 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4641 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4642 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4643 #ifdef SCTP_AUDITING_ENABLED
4644 sctp_audit_log(0xB3,
4645 (asoc->sent_queue_retran_cnt & 0x000000ff));
4648 if (tp1->rec.data.chunk_was_revoked) {
4649 /* deflate the cwnd */
4650 tp1->whoTo->cwnd -= tp1->book_size;
4651 tp1->rec.data.chunk_was_revoked = 0;
4653 tp1->sent = SCTP_DATAGRAM_ACKED;
4658 tp1 = TAILQ_NEXT(tp1, sctp_next);
4660 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4661 /* always set this up to cum-ack */
4662 asoc->this_sack_highest_gap = last_tsn;
4664 /* Move offset up to point to gaps/dups */
4665 offset += sizeof(struct sctp_sack_chunk);
4666 if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
4668 /* skip corrupt segments */
4674 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4675 * to be greater than the cumack. Also reset saw_newack to 0
4678 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4679 net->saw_newack = 0;
4680 net->this_sack_highest_newack = last_tsn;
4684 * thisSackHighestGap will increase while handling NEW
4685 * segments this_sack_highest_newack will increase while
4686 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4687 * used for CMT DAC algo. saw_newack will also change.
4689 sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
4690 &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4691 num_seg, &ecn_seg_sums);
4693 if (sctp_strict_sacks) {
4695 * validate the biggest_tsn_acked in the gap acks if
4696 * strict adherence is wanted.
4698 if ((biggest_tsn_acked == send_s) ||
4699 (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4701 * peer is either confused or we are under
4702 * attack. We must abort.
4709 /*******************************************/
4710 /* cancel ALL T3-send timer if accum moved */
4711 /*******************************************/
4712 if (sctp_cmt_on_off) {
4713 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4714 if (net->new_pseudo_cumack)
4715 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4717 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4722 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4723 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4724 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4728 /********************************************/
4729 /* drop the acked chunks from the sendqueue */
4730 /********************************************/
4731 asoc->last_acked_seq = cum_ack;
4733 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4737 if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
4741 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4742 /* no more sent on list */
4743 printf("Warning, tp1->sent == %d and its now acked?\n",
4746 tp2 = TAILQ_NEXT(tp1, sctp_next);
4747 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4748 if (tp1->pr_sctp_on) {
4749 if (asoc->pr_sctp_cnt != 0)
4750 asoc->pr_sctp_cnt--;
4752 if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
4753 (asoc->total_flight > 0)) {
4755 panic("Warning flight size is postive and should be 0");
4757 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4758 asoc->total_flight);
4760 asoc->total_flight = 0;
4763 /* sa_ignore NO_NULL_CHK */
4764 sctp_free_bufspace(stcb, asoc, tp1, 1);
4765 sctp_m_freem(tp1->data);
4766 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4767 asoc->sent_queue_cnt_removeable--;
4770 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4771 sctp_log_sack(asoc->last_acked_seq,
4773 tp1->rec.data.TSN_seq,
4776 SCTP_LOG_FREE_SENT);
4779 asoc->sent_queue_cnt--;
4780 sctp_free_a_chunk(stcb, tp1);
4783 } while (tp1 != NULL);
4786 /* sa_ignore NO_NULL_CHK */
4787 if ((wake_him) && (stcb->sctp_socket)) {
4788 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4792 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4793 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4794 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
4796 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4797 so = SCTP_INP_SO(stcb->sctp_ep);
4798 atomic_add_int(&stcb->asoc.refcnt, 1);
4799 SCTP_TCB_UNLOCK(stcb);
4800 SCTP_SOCKET_LOCK(so, 1);
4801 SCTP_TCB_LOCK(stcb);
4802 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4803 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4804 /* assoc was freed while we were unlocked */
4805 SCTP_SOCKET_UNLOCK(so, 1);
4809 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4810 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4811 SCTP_SOCKET_UNLOCK(so, 1);
4814 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4815 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
4819 if (asoc->fast_retran_loss_recovery && accum_moved) {
4820 if (compare_with_wrap(asoc->last_acked_seq,
4821 asoc->fast_recovery_tsn, MAX_TSN) ||
4822 asoc->last_acked_seq == asoc->fast_recovery_tsn) {
4823 /* Setup so we will exit RFC2582 fast recovery */
4824 will_exit_fast_recovery = 1;
4828 * Check for revoked fragments:
4830 * if Previous sack - Had no frags then we can't have any revoked if
4831 * Previous sack - Had frag's then - If we now have frags aka
4832 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4833 * some of them. else - The peer revoked all ACKED fragments, since
4834 * we had some before and now we have NONE.
4838 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4839 else if (asoc->saw_sack_with_frags) {
4840 int cnt_revoked = 0;
4842 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4844 /* Peer revoked all dg's marked or acked */
4845 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4846 if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
4847 (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
4848 tp1->sent = SCTP_DATAGRAM_SENT;
4849 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4850 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4851 tp1->whoTo->flight_size,
4853 (uintptr_t) tp1->whoTo,
4854 tp1->rec.data.TSN_seq);
4856 sctp_flight_size_increase(tp1);
4857 sctp_total_flight_increase(stcb, tp1);
4858 tp1->rec.data.chunk_was_revoked = 1;
4860 * To ensure that this increase in
4861 * flightsize, which is artificial,
4862 * does not throttle the sender, we
4863 * also increase the cwnd
4866 tp1->whoTo->cwnd += tp1->book_size;
4874 asoc->saw_sack_with_frags = 0;
4877 asoc->saw_sack_with_frags = 1;
4879 asoc->saw_sack_with_frags = 0;
4881 /* JRS - Use the congestion control given in the CC module */
4882 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4884 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4885 /* nothing left in-flight */
4886 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4887 /* stop all timers */
4888 if (sctp_early_fr) {
4889 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4890 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4891 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4892 SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
4895 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4896 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4897 net->flight_size = 0;
4898 net->partial_bytes_acked = 0;
4900 asoc->total_flight = 0;
4901 asoc->total_flight_count = 0;
4903 /**********************************/
4904 /* Now what about shutdown issues */
4905 /**********************************/
4906 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4907 /* nothing left on sendqueue.. consider done */
4908 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4909 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4910 asoc->peers_rwnd, 0, 0, a_rwnd);
4912 asoc->peers_rwnd = a_rwnd;
4913 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4914 /* SWS sender side engages */
4915 asoc->peers_rwnd = 0;
4918 if ((asoc->stream_queue_cnt == 1) &&
4919 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4920 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4921 (asoc->locked_on_sending)
4923 struct sctp_stream_queue_pending *sp;
4926 * I may be in a state where we got all across.. but
4927 * cannot write more due to a shutdown... we abort
4928 * since the user did not indicate EOR in this case.
4930 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4932 if ((sp) && (sp->length == 0)) {
4933 asoc->locked_on_sending = NULL;
4934 if (sp->msg_is_complete) {
4935 asoc->stream_queue_cnt--;
4937 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4938 asoc->stream_queue_cnt--;
4942 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4943 (asoc->stream_queue_cnt == 0)) {
4944 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4945 /* Need to abort here */
4951 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4952 0, M_DONTWAIT, 1, MT_DATA);
4954 struct sctp_paramhdr *ph;
4957 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4959 ph = mtod(oper, struct sctp_paramhdr *);
4960 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4961 ph->param_length = htons(SCTP_BUF_LEN(oper));
4962 ippp = (uint32_t *) (ph + 1);
4963 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4965 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4966 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4969 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4970 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4971 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4973 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4974 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4975 sctp_stop_timers_for_shutdown(stcb);
4976 sctp_send_shutdown(stcb,
4977 stcb->asoc.primary_destination);
4978 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4979 stcb->sctp_ep, stcb, asoc->primary_destination);
4980 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4981 stcb->sctp_ep, stcb, asoc->primary_destination);
4984 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4985 (asoc->stream_queue_cnt == 0)) {
4986 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4989 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4990 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4991 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4992 sctp_send_shutdown_ack(stcb,
4993 stcb->asoc.primary_destination);
4995 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4996 stcb->sctp_ep, stcb, asoc->primary_destination);
5001 * Now here we are going to recycle net_ack for a different use...
5004 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5009 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5010 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5011 * automatically ensure that.
5013 if (sctp_cmt_on_off && sctp_cmt_use_dac && (cmt_dac_flag == 0)) {
5014 this_sack_lowest_newack = cum_ack;
5017 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5018 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5020 /*********************************************/
5021 /* Here we perform PR-SCTP procedures */
5023 /*********************************************/
5024 /* C1. update advancedPeerAckPoint */
5025 if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5026 asoc->advanced_peer_ack_point = cum_ack;
5028 /* C2. try to further move advancedPeerAckPoint ahead */
5029 if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5030 struct sctp_tmit_chunk *lchk;
5032 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5033 /* C3. See if we need to send a Fwd-TSN */
5034 if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5037 * ISSUE with ECN, see FWD-TSN processing for notes
5038 * on issues that will occur when the ECN NONCE
5039 * stuff is put into SCTP for cross checking.
5041 send_forward_tsn(stcb, asoc);
5044 * ECN Nonce: Disable Nonce Sum check when FWD TSN
5045 * is sent and store resync tsn
5047 asoc->nonce_sum_check = 0;
5048 asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5050 /* Assure a timer is up */
5051 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5052 stcb->sctp_ep, stcb, lchk->whoTo);
5056 /* JRS - Use the congestion control given in the CC module */
5057 asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5059 /******************************************************************
5060 * Here we do the stuff with ECN Nonce checking.
5061 * We basically check to see if the nonce sum flag was incorrect
5062 * or if resynchronization needs to be done. Also if we catch a
5063 * misbehaving receiver we give him the kick.
5064 ******************************************************************/
5066 if (asoc->ecn_nonce_allowed) {
5067 if (asoc->nonce_sum_check) {
5068 if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5069 if (asoc->nonce_wait_for_ecne == 0) {
5070 struct sctp_tmit_chunk *lchk;
5072 lchk = TAILQ_FIRST(&asoc->send_queue);
5073 asoc->nonce_wait_for_ecne = 1;
5075 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5077 asoc->nonce_wait_tsn = asoc->sending_seq;
5080 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5081 (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5083 * Misbehaving peer. We need
5084 * to react to this guy
5086 asoc->ecn_allowed = 0;
5087 asoc->ecn_nonce_allowed = 0;
5092 /* See if Resynchronization Possible */
5093 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5094 asoc->nonce_sum_check = 1;
5096 * now we must calculate what the base is.
5097 * We do this based on two things, we know
5098 * the total's for all the segments
5099 * gap-acked in the SACK, its stored in
5100 * ecn_seg_sums. We also know the SACK's
5101 * nonce sum, its in nonce_sum_flag. So we
5102 * can build a truth table to back-calculate
5104 * asoc->nonce_sum_expect_base:
5106 * SACK-flag-Value Seg-Sums Base 0 0 0
5110 asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5114 /* Now are we exiting loss recovery ? */
5115 if (will_exit_fast_recovery) {
5116 /* Ok, we must exit fast recovery */
5117 asoc->fast_retran_loss_recovery = 0;
5119 if ((asoc->sat_t3_loss_recovery) &&
5120 ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5122 (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5123 /* end satellite t3 loss recovery */
5124 asoc->sat_t3_loss_recovery = 0;
5129 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5130 if (net->will_exit_fast_recovery) {
5131 /* Ok, we must exit fast recovery */
5132 net->fast_retran_loss_recovery = 0;
5136 /* Adjust and set the new rwnd value */
5137 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5138 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5139 asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
5141 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5142 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
5143 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5144 /* SWS sender side engages */
5145 asoc->peers_rwnd = 0;
5147 if (asoc->peers_rwnd > old_rwnd) {
5148 win_probe_recovery = 1;
5151 * Now we must setup so we have a timer up for anyone with
5157 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5158 if (win_probe_recovery && (net->window_probe)) {
5159 net->window_probe = 0;
5160 win_probe_recovered = 1;
5162 * Find first chunk that was used with
5163 * window probe and clear the event. Put
5164 * it back into the send queue as if has
5167 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5168 if (tp1->window_probe) {
5169 sctp_window_probe_recovery(stcb, asoc, net, tp1);
5174 if (net->flight_size) {
5176 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5177 stcb->sctp_ep, stcb, net);
5179 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5180 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5182 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5184 if (sctp_early_fr) {
5185 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5186 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5187 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5188 SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5194 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5195 (asoc->sent_queue_retran_cnt == 0) &&
5196 (win_probe_recovered == 0) &&
5198 /* huh, this should not happen */
5199 sctp_fs_audit(asoc);
5200 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5201 net->flight_size = 0;
5203 asoc->total_flight = 0;
5204 asoc->total_flight_count = 0;
5205 asoc->sent_queue_retran_cnt = 0;
5206 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5207 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5208 sctp_flight_size_increase(tp1);
5209 sctp_total_flight_increase(stcb, tp1);
5210 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5211 asoc->sent_queue_retran_cnt++;
5217 if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
5218 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5220 stcb->asoc.peers_rwnd,
5221 stcb->asoc.total_flight,
5222 stcb->asoc.total_output_queue_size);
5227 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5228 struct sctp_nets *netp, int *abort_flag)
5231 uint32_t cum_ack, a_rwnd;
5233 cum_ack = ntohl(cp->cumulative_tsn_ack);
5234 /* Arrange so a_rwnd does NOT change */
5235 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5237 /* Now call the express sack handling */
5238 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5242 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5243 struct sctp_stream_in *strmin)
5245 struct sctp_queued_to_read *ctl, *nctl;
5246 struct sctp_association *asoc;
5250 tt = strmin->last_sequence_delivered;
5252 * First deliver anything prior to and including the stream no that
5255 ctl = TAILQ_FIRST(&strmin->inqueue);
5257 nctl = TAILQ_NEXT(ctl, next);
5258 if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5259 (tt == ctl->sinfo_ssn)) {
5260 /* this is deliverable now */
5261 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5262 /* subtract pending on streams */
5263 asoc->size_on_all_streams -= ctl->length;
5264 sctp_ucount_decr(asoc->cnt_on_all_streams);
5265 /* deliver it to at least the delivery-q */
5266 if (stcb->sctp_socket) {
5267 sctp_add_to_readq(stcb->sctp_ep, stcb,
5269 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5272 /* no more delivery now. */
5278 * now we must deliver things in queue the normal way if any are
5281 tt = strmin->last_sequence_delivered + 1;
5282 ctl = TAILQ_FIRST(&strmin->inqueue);
5284 nctl = TAILQ_NEXT(ctl, next);
5285 if (tt == ctl->sinfo_ssn) {
5286 /* this is deliverable now */
5287 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5288 /* subtract pending on streams */
5289 asoc->size_on_all_streams -= ctl->length;
5290 sctp_ucount_decr(asoc->cnt_on_all_streams);
5291 /* deliver it to at least the delivery-q */
5292 strmin->last_sequence_delivered = ctl->sinfo_ssn;
5293 if (stcb->sctp_socket) {
5294 sctp_add_to_readq(stcb->sctp_ep, stcb,
5296 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5298 tt = strmin->last_sequence_delivered + 1;
5307 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5308 struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
5311 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5312 * forward TSN, when the SACK comes back that acknowledges the
5313 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5314 * get quite tricky since we may have sent more data interveneing
5315 * and must carefully account for what the SACK says on the nonce
5316 * and any gaps that are reported. This work will NOT be done here,
5317 * but I note it here since it is really related to PR-SCTP and
5321 /* The pr-sctp fwd tsn */
5323 * here we will perform all the data receiver side steps for
5324 * processing FwdTSN, as required in by pr-sctp draft:
5326 * Assume we get FwdTSN(x):
5328 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5329 * others we have 3) examine and update re-ordering queue on
5330 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5331 * report where we are.
5333 struct sctp_association *asoc;
5334 uint32_t new_cum_tsn, gap;
5335 unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
5336 struct sctp_stream_in *strm;
5337 struct sctp_tmit_chunk *chk, *at;
5339 cumack_set_flag = 0;
5342 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5343 SCTPDBG(SCTP_DEBUG_INDATA1,
5344 "Bad size too small/big fwd-tsn\n");
5347 m_size = (stcb->asoc.mapping_array_size << 3);
5348 /*************************************************************/
5349 /* 1. Here we update local cumTSN and shift the bitmap array */
5350 /*************************************************************/
5351 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5353 if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5354 asoc->cumulative_tsn == new_cum_tsn) {
5355 /* Already got there ... */
5358 if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5360 asoc->highest_tsn_inside_map = new_cum_tsn;
5361 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5362 sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5366 * now we know the new TSN is more advanced, let's find the actual
5369 if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
5371 (new_cum_tsn == asoc->mapping_array_base_tsn)) {
5372 gap = new_cum_tsn - asoc->mapping_array_base_tsn;
5374 /* try to prevent underflow here */
5375 gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5378 if (gap >= m_size) {
5379 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5380 sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5382 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5386 * out of range (of single byte chunks in the rwnd I
5387 * give out). This must be an attacker.
5390 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5391 0, M_DONTWAIT, 1, MT_DATA);
5393 struct sctp_paramhdr *ph;
5396 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5397 (sizeof(uint32_t) * 3);
5398 ph = mtod(oper, struct sctp_paramhdr *);
5399 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5400 ph->param_length = htons(SCTP_BUF_LEN(oper));
5401 ippp = (uint32_t *) (ph + 1);
5402 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5404 *ippp = asoc->highest_tsn_inside_map;
5406 *ippp = new_cum_tsn;
5408 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5409 sctp_abort_an_association(stcb->sctp_ep, stcb,
5410 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5413 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5415 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5416 cumack_set_flag = 1;
5417 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5418 asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
5420 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5421 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5423 asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
5425 SCTP_TCB_LOCK_ASSERT(stcb);
5426 if ((compare_with_wrap(((uint32_t) asoc->cumulative_tsn + gap), asoc->highest_tsn_inside_map, MAX_TSN)) ||
5427 (((uint32_t) asoc->cumulative_tsn + gap) == asoc->highest_tsn_inside_map)) {
5430 for (i = 0; i <= gap; i++) {
5431 SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
5435 * Now after marking all, slide thing forward but no sack
5438 sctp_sack_check(stcb, 0, 0, abort_flag);
5443 /*************************************************************/
5444 /* 2. Clear up re-assembly queue */
5445 /*************************************************************/
5447 * First service it if pd-api is up, just in case we can progress it
5450 if (asoc->fragmented_delivery_inprogress) {
5451 sctp_service_reassembly(stcb, asoc);
5453 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5454 /* For each one on here see if we need to toss it */
5456 * For now large messages held on the reasmqueue that are
5457 * complete will be tossed too. We could in theory do more
5458 * work to spin through and stop after dumping one msg aka
5459 * seeing the start of a new msg at the head, and call the
5460 * delivery function... to see if it can be delivered... But
5461 * for now we just dump everything on the queue.
5463 chk = TAILQ_FIRST(&asoc->reasmqueue);
5465 at = TAILQ_NEXT(chk, sctp_next);
5466 if (compare_with_wrap(asoc->cumulative_tsn,
5467 chk->rec.data.TSN_seq, MAX_TSN) ||
5468 asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
5469 /* It needs to be tossed */
5470 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5471 if (compare_with_wrap(chk->rec.data.TSN_seq,
5472 asoc->tsn_last_delivered, MAX_TSN)) {
5473 asoc->tsn_last_delivered =
5474 chk->rec.data.TSN_seq;
5475 asoc->str_of_pdapi =
5476 chk->rec.data.stream_number;
5477 asoc->ssn_of_pdapi =
5478 chk->rec.data.stream_seq;
5479 asoc->fragment_flags =
5480 chk->rec.data.rcv_flags;
5482 asoc->size_on_reasm_queue -= chk->send_size;
5483 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5486 /* Clear up any stream problem */
5487 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5488 SCTP_DATA_UNORDERED &&
5489 (compare_with_wrap(chk->rec.data.stream_seq,
5490 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5493 * We must dump forward this streams
5494 * sequence number if the chunk is
5495 * not unordered that is being
5496 * skipped. There is a chance that
5497 * if the peer does not include the
5498 * last fragment in its FWD-TSN we
5499 * WILL have a problem here since
5500 * you would have a partial chunk in
5501 * queue that may not be
5502 * deliverable. Also if a Partial
5503 * delivery API as started the user
5504 * may get a partial chunk. The next
5505 * read returning a new chunk...
5506 * really ugly but I see no way
5507 * around it! Maybe a notify??
5509 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5510 chk->rec.data.stream_seq;
5513 sctp_m_freem(chk->data);
5516 sctp_free_a_chunk(stcb, chk);
5519 * Ok we have gone beyond the end of the
5520 * fwd-tsn's mark. Some checks...
5522 if ((asoc->fragmented_delivery_inprogress) &&
5523 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5527 * Special case PD-API is up and
5528 * what we fwd-tsn' over includes
5529 * one that had the LAST_FRAG. We no
5530 * longer need to do the PD-API.
5532 asoc->fragmented_delivery_inprogress = 0;
5534 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5535 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5536 stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq, SCTP_SO_NOT_LOCKED);
5544 if (asoc->fragmented_delivery_inprogress) {
5546 * Ok we removed cnt_gone chunks in the PD-API queue that
5547 * were being delivered. So now we must turn off the flag.
5551 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5552 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5553 stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq, SCTP_SO_NOT_LOCKED);
5554 asoc->fragmented_delivery_inprogress = 0;
5556 /*************************************************************/
5557 /* 3. Update the PR-stream re-ordering queues */
5558 /*************************************************************/
5559 fwd_sz -= sizeof(*fwd);
5562 unsigned int num_str;
5563 struct sctp_strseq *stseq, strseqbuf;
5565 offset += sizeof(*fwd);
5567 num_str = fwd_sz / sizeof(struct sctp_strseq);
5568 for (i = 0; i < num_str; i++) {
5571 stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5572 sizeof(struct sctp_strseq),
5573 (uint8_t *) & strseqbuf);
5574 offset += sizeof(struct sctp_strseq);
5575 if (stseq == NULL) {
5579 st = ntohs(stseq->stream);
5581 st = ntohs(stseq->sequence);
5582 stseq->sequence = st;
5584 if (stseq->stream >= asoc->streamincnt) {
5585 /* screwed up streams, stop! */
5588 strm = &asoc->strmin[stseq->stream];
5589 if (compare_with_wrap(stseq->sequence,
5590 strm->last_sequence_delivered, MAX_SEQ)) {
5591 /* Update the sequence number */
5592 strm->last_sequence_delivered =
5595 /* now kick the stream the new way */
5596 /* sa_ignore NO_NULL_CHK */
5597 sctp_kick_prsctp_reorder_queue(stcb, strm);
5600 if (TAILQ_FIRST(&asoc->reasmqueue)) {
5601 /* now lets kick out and check for more fragmented delivery */
5602 /* sa_ignore NO_NULL_CHK */
5603 sctp_deliver_reasm_check(stcb, &stcb->asoc);