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 (asoc->my_rwnd < 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;
1482 /* Calculate the number of TSN's between the base and this TSN */
1483 if (tsn >= asoc->mapping_array_base_tsn) {
1484 gap = tsn - asoc->mapping_array_base_tsn;
1486 gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1488 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1489 /* Can't hold the bit in the mapping at max array, toss it */
1492 if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1493 SCTP_TCB_LOCK_ASSERT(stcb);
1494 if (sctp_expand_mapping_array(asoc, gap)) {
1495 /* Can't expand, drop it */
1499 if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1502 /* See if we have received this one already */
1503 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1504 SCTP_STAT_INCR(sctps_recvdupdata);
1505 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1506 /* Record a dup for the next outbound sack */
1507 asoc->dup_tsns[asoc->numduptsns] = tsn;
1510 asoc->send_sack = 1;
1514 * Check to see about the GONE flag, duplicates would cause a sack
1515 * to be sent up above
1517 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1518 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1519 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1522 * wait a minute, this guy is gone, there is no longer a
1523 * receiver. Send peer an ABORT!
1525 struct mbuf *op_err;
1527 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1528 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1533 * Now before going further we see if there is room. If NOT then we
1534 * MAY let one through only IF this TSN is the one we are waiting
1535 * for on a partial delivery API.
1538 /* now do the tests */
1539 if (((asoc->cnt_on_all_streams +
1540 asoc->cnt_on_reasm_queue +
1541 asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
1542 (((int)asoc->my_rwnd) <= 0)) {
1544 * When we have NO room in the rwnd we check to make sure
1545 * the reader is doing its job...
1547 if (stcb->sctp_socket->so_rcv.sb_cc) {
1548 /* some to read, wake-up */
1549 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1552 so = SCTP_INP_SO(stcb->sctp_ep);
1553 atomic_add_int(&stcb->asoc.refcnt, 1);
1554 SCTP_TCB_UNLOCK(stcb);
1555 SCTP_SOCKET_LOCK(so, 1);
1556 SCTP_TCB_LOCK(stcb);
1557 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1558 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1559 /* assoc was freed while we were unlocked */
1560 SCTP_SOCKET_UNLOCK(so, 1);
1564 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1565 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1566 SCTP_SOCKET_UNLOCK(so, 1);
1569 /* now is it in the mapping array of what we have accepted? */
1570 if (compare_with_wrap(tsn,
1571 asoc->highest_tsn_inside_map, MAX_TSN)) {
1573 /* Nope not in the valid range dump it */
1574 SCTPDBG(SCTP_DEBUG_INDATA1, "My rwnd overrun1:tsn:%x rwnd %x sbspace:%x\n",
1576 sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv));
1577 sctp_set_rwnd(stcb, asoc);
1578 if ((asoc->cnt_on_all_streams +
1579 asoc->cnt_on_reasm_queue +
1580 asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
1581 SCTP_STAT_INCR(sctps_datadropchklmt);
1583 SCTP_STAT_INCR(sctps_datadroprwnd);
1590 strmno = ntohs(ch->dp.stream_id);
1591 if (strmno >= asoc->streamincnt) {
1592 struct sctp_paramhdr *phdr;
1595 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1596 0, M_DONTWAIT, 1, MT_DATA);
1598 /* add some space up front so prepend will work well */
1599 SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1600 phdr = mtod(mb, struct sctp_paramhdr *);
1602 * Error causes are just param's and this one has
1603 * two back to back phdr, one with the error type
1604 * and size, the other with the streamid and a rsvd
1606 SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1607 phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1608 phdr->param_length =
1609 htons(sizeof(struct sctp_paramhdr) * 2);
1611 /* We insert the stream in the type field */
1612 phdr->param_type = ch->dp.stream_id;
1613 /* And set the length to 0 for the rsvd field */
1614 phdr->param_length = 0;
1615 sctp_queue_op_err(stcb, mb);
1617 SCTP_STAT_INCR(sctps_badsid);
1618 SCTP_TCB_LOCK_ASSERT(stcb);
1619 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1620 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1621 /* we have a new high score */
1622 asoc->highest_tsn_inside_map = tsn;
1623 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1624 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1627 if (tsn == (asoc->cumulative_tsn + 1)) {
1628 /* Update cum-ack */
1629 asoc->cumulative_tsn = tsn;
1634 * Before we continue lets validate that we are not being fooled by
1635 * an evil attacker. We can only have 4k chunks based on our TSN
1636 * spread allowed by the mapping array 512 * 8 bits, so there is no
1637 * way our stream sequence numbers could have wrapped. We of course
1638 * only validate the FIRST fragment so the bit must be set.
1640 strmseq = ntohs(ch->dp.stream_sequence);
1641 #ifdef SCTP_ASOCLOG_OF_TSNS
1642 SCTP_TCB_LOCK_ASSERT(stcb);
1643 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1644 asoc->tsn_in_at = 0;
1645 asoc->tsn_in_wrapped = 1;
1647 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1648 asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1649 asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1650 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1651 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1652 asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1653 asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1654 asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1657 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1658 (TAILQ_EMPTY(&asoc->resetHead)) &&
1659 (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1660 (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1661 strmseq, MAX_SEQ) ||
1662 asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1663 /* The incoming sseq is behind where we last delivered? */
1664 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1665 strmseq, asoc->strmin[strmno].last_sequence_delivered);
1666 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1667 0, M_DONTWAIT, 1, MT_DATA);
1669 struct sctp_paramhdr *ph;
1672 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1673 (3 * sizeof(uint32_t));
1674 ph = mtod(oper, struct sctp_paramhdr *);
1675 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1676 ph->param_length = htons(SCTP_BUF_LEN(oper));
1677 ippp = (uint32_t *) (ph + 1);
1678 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1682 *ippp = ((strmno << 16) | strmseq);
1685 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1686 sctp_abort_an_association(stcb->sctp_ep, stcb,
1687 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1691 /************************************
1692 * From here down we may find ch-> invalid
1693 * so its a good idea NOT to use it.
1694 *************************************/
1696 the_len = (chk_length - sizeof(struct sctp_data_chunk));
1697 if (last_chunk == 0) {
1698 dmbuf = SCTP_M_COPYM(*m,
1699 (offset + sizeof(struct sctp_data_chunk)),
1700 the_len, M_DONTWAIT);
1701 #ifdef SCTP_MBUF_LOGGING
1702 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
1707 if (SCTP_BUF_IS_EXTENDED(mat)) {
1708 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1710 mat = SCTP_BUF_NEXT(mat);
1715 /* We can steal the last chunk */
1719 /* lop off the top part */
1720 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1721 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1722 l_len = SCTP_BUF_LEN(dmbuf);
1725 * need to count up the size hopefully does not hit
1733 l_len += SCTP_BUF_LEN(lat);
1734 lat = SCTP_BUF_NEXT(lat);
1737 if (l_len > the_len) {
1738 /* Trim the end round bytes off too */
1739 m_adj(dmbuf, -(l_len - the_len));
1742 if (dmbuf == NULL) {
1743 SCTP_STAT_INCR(sctps_nomem);
1746 if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1747 asoc->fragmented_delivery_inprogress == 0 &&
1748 TAILQ_EMPTY(&asoc->resetHead) &&
1750 ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1751 TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1752 /* Candidate for express delivery */
1754 * Its not fragmented, No PD-API is up, Nothing in the
1755 * delivery queue, Its un-ordered OR ordered and the next to
1756 * deliver AND nothing else is stuck on the stream queue,
1757 * And there is room for it in the socket buffer. Lets just
1758 * stuff it up the buffer....
1761 /* It would be nice to avoid this copy if we could :< */
1762 sctp_alloc_a_readq(stcb, control);
1763 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1769 if (control == NULL) {
1770 goto failed_express_del;
1772 sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1773 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1774 /* for ordered, bump what we delivered */
1775 asoc->strmin[strmno].last_sequence_delivered++;
1777 SCTP_STAT_INCR(sctps_recvexpress);
1778 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
1779 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1780 SCTP_STR_LOG_FROM_EXPRS_DEL);
1783 goto finish_express_del;
1786 /* If we reach here this is a new chunk */
1789 /* Express for fragmented delivery? */
1790 if ((asoc->fragmented_delivery_inprogress) &&
1791 (stcb->asoc.control_pdapi) &&
1792 (asoc->str_of_pdapi == strmno) &&
1793 (asoc->ssn_of_pdapi == strmseq)
1795 control = stcb->asoc.control_pdapi;
1796 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1797 /* Can't be another first? */
1798 goto failed_pdapi_express_del;
1800 if (tsn == (control->sinfo_tsn + 1)) {
1801 /* Yep, we can add it on */
1805 if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1808 cumack = asoc->cumulative_tsn;
1809 if ((cumack + 1) == tsn)
1812 if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1814 &stcb->sctp_socket->so_rcv)) {
1815 SCTP_PRINTF("Append fails end:%d\n", end);
1816 goto failed_pdapi_express_del;
1818 SCTP_STAT_INCR(sctps_recvexpressm);
1819 control->sinfo_tsn = tsn;
1820 asoc->tsn_last_delivered = tsn;
1821 asoc->fragment_flags = chunk_flags;
1822 asoc->tsn_of_pdapi_last_delivered = tsn;
1823 asoc->last_flags_delivered = chunk_flags;
1824 asoc->last_strm_seq_delivered = strmseq;
1825 asoc->last_strm_no_delivered = strmno;
1827 /* clean up the flags and such */
1828 asoc->fragmented_delivery_inprogress = 0;
1829 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1830 asoc->strmin[strmno].last_sequence_delivered++;
1832 stcb->asoc.control_pdapi = NULL;
1833 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1835 * There could be another message
1838 need_reasm_check = 1;
1842 goto finish_express_del;
1845 failed_pdapi_express_del:
1847 if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1848 sctp_alloc_a_chunk(stcb, chk);
1850 /* No memory so we drop the chunk */
1851 SCTP_STAT_INCR(sctps_nomem);
1852 if (last_chunk == 0) {
1853 /* we copied it, free the copy */
1854 sctp_m_freem(dmbuf);
1858 chk->rec.data.TSN_seq = tsn;
1859 chk->no_fr_allowed = 0;
1860 chk->rec.data.stream_seq = strmseq;
1861 chk->rec.data.stream_number = strmno;
1862 chk->rec.data.payloadtype = protocol_id;
1863 chk->rec.data.context = stcb->asoc.context;
1864 chk->rec.data.doing_fast_retransmit = 0;
1865 chk->rec.data.rcv_flags = chunk_flags;
1867 chk->send_size = the_len;
1869 atomic_add_int(&net->ref_count, 1);
1872 sctp_alloc_a_readq(stcb, control);
1873 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1879 if (control == NULL) {
1880 /* No memory so we drop the chunk */
1881 SCTP_STAT_INCR(sctps_nomem);
1882 if (last_chunk == 0) {
1883 /* we copied it, free the copy */
1884 sctp_m_freem(dmbuf);
1888 control->length = the_len;
1891 /* Mark it as received */
1892 /* Now queue it where it belongs */
1893 if (control != NULL) {
1894 /* First a sanity check */
1895 if (asoc->fragmented_delivery_inprogress) {
1897 * Ok, we have a fragmented delivery in progress if
1898 * this chunk is next to deliver OR belongs in our
1899 * view to the reassembly, the peer is evil or
1902 uint32_t estimate_tsn;
1904 estimate_tsn = asoc->tsn_last_delivered + 1;
1905 if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1906 (estimate_tsn == control->sinfo_tsn)) {
1907 /* Evil/Broke peer */
1908 sctp_m_freem(control->data);
1909 control->data = NULL;
1910 if (control->whoFrom) {
1911 sctp_free_remote_addr(control->whoFrom);
1912 control->whoFrom = NULL;
1914 sctp_free_a_readq(stcb, control);
1915 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1916 0, M_DONTWAIT, 1, MT_DATA);
1918 struct sctp_paramhdr *ph;
1921 SCTP_BUF_LEN(oper) =
1922 sizeof(struct sctp_paramhdr) +
1923 (3 * sizeof(uint32_t));
1924 ph = mtod(oper, struct sctp_paramhdr *);
1926 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1927 ph->param_length = htons(SCTP_BUF_LEN(oper));
1928 ippp = (uint32_t *) (ph + 1);
1929 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1933 *ippp = ((strmno << 16) | strmseq);
1935 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1936 sctp_abort_an_association(stcb->sctp_ep, stcb,
1937 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1942 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1943 sctp_m_freem(control->data);
1944 control->data = NULL;
1945 if (control->whoFrom) {
1946 sctp_free_remote_addr(control->whoFrom);
1947 control->whoFrom = NULL;
1949 sctp_free_a_readq(stcb, control);
1951 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1952 0, M_DONTWAIT, 1, MT_DATA);
1954 struct sctp_paramhdr *ph;
1957 SCTP_BUF_LEN(oper) =
1958 sizeof(struct sctp_paramhdr) +
1959 (3 * sizeof(uint32_t));
1961 struct sctp_paramhdr *);
1963 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1965 htons(SCTP_BUF_LEN(oper));
1966 ippp = (uint32_t *) (ph + 1);
1967 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1971 *ippp = ((strmno << 16) | strmseq);
1973 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1974 sctp_abort_an_association(stcb->sctp_ep,
1975 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1982 /* No PDAPI running */
1983 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1985 * Reassembly queue is NOT empty validate
1986 * that this tsn does not need to be in
1987 * reasembly queue. If it does then our peer
1988 * is broken or evil.
1990 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1991 sctp_m_freem(control->data);
1992 control->data = NULL;
1993 if (control->whoFrom) {
1994 sctp_free_remote_addr(control->whoFrom);
1995 control->whoFrom = NULL;
1997 sctp_free_a_readq(stcb, control);
1998 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1999 0, M_DONTWAIT, 1, MT_DATA);
2001 struct sctp_paramhdr *ph;
2004 SCTP_BUF_LEN(oper) =
2005 sizeof(struct sctp_paramhdr) +
2006 (3 * sizeof(uint32_t));
2008 struct sctp_paramhdr *);
2010 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2012 htons(SCTP_BUF_LEN(oper));
2013 ippp = (uint32_t *) (ph + 1);
2014 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2018 *ippp = ((strmno << 16) | strmseq);
2020 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2021 sctp_abort_an_association(stcb->sctp_ep,
2022 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2029 /* ok, if we reach here we have passed the sanity checks */
2030 if (chunk_flags & SCTP_DATA_UNORDERED) {
2031 /* queue directly into socket buffer */
2032 sctp_add_to_readq(stcb->sctp_ep, stcb,
2034 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
2037 * Special check for when streams are resetting. We
2038 * could be more smart about this and check the
2039 * actual stream to see if it is not being reset..
2040 * that way we would not create a HOLB when amongst
2041 * streams being reset and those not being reset.
2043 * We take complete messages that have a stream reset
2044 * intervening (aka the TSN is after where our
2045 * cum-ack needs to be) off and put them on a
2046 * pending_reply_queue. The reassembly ones we do
2047 * not have to worry about since they are all sorted
2048 * and proceessed by TSN order. It is only the
2049 * singletons I must worry about.
2051 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2052 ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2055 * yep its past where we need to reset... go
2056 * ahead and queue it.
2058 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2060 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2062 struct sctp_queued_to_read *ctlOn;
2063 unsigned char inserted = 0;
2065 ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2067 if (compare_with_wrap(control->sinfo_tsn,
2068 ctlOn->sinfo_tsn, MAX_TSN)) {
2069 ctlOn = TAILQ_NEXT(ctlOn, next);
2072 TAILQ_INSERT_BEFORE(ctlOn, control, next);
2077 if (inserted == 0) {
2079 * must be put at end, use
2080 * prevP (all setup from
2081 * loop) to setup nextP.
2083 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2087 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2094 /* Into the re-assembly queue */
2095 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2098 * the assoc is now gone and chk was put onto the
2099 * reasm queue, which has all been freed.
2106 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2107 /* we have a new high score */
2108 asoc->highest_tsn_inside_map = tsn;
2109 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2110 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2113 if (tsn == (asoc->cumulative_tsn + 1)) {
2114 /* Update cum-ack */
2115 asoc->cumulative_tsn = tsn;
2121 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2123 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2125 SCTP_STAT_INCR(sctps_recvdata);
2126 /* Set it present please */
2127 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
2128 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2130 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2131 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2132 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2134 SCTP_TCB_LOCK_ASSERT(stcb);
2135 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2136 /* check the special flag for stream resets */
2137 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2138 ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2139 (asoc->cumulative_tsn == liste->tsn))
2142 * we have finished working through the backlogged TSN's now
2143 * time to reset streams. 1: call reset function. 2: free
2144 * pending_reply space 3: distribute any chunks in
2145 * pending_reply_queue.
2147 struct sctp_queued_to_read *ctl;
2149 sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2150 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2151 SCTP_FREE(liste, SCTP_M_STRESET);
2152 /* sa_ignore FREED_MEMORY */
2153 liste = TAILQ_FIRST(&asoc->resetHead);
2154 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2155 if (ctl && (liste == NULL)) {
2156 /* All can be removed */
2158 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2159 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2163 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2166 /* more than one in queue */
2167 while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2169 * if ctl->sinfo_tsn is <= liste->tsn we can
2170 * process it which is the NOT of
2171 * ctl->sinfo_tsn > liste->tsn
2173 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2174 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2178 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2182 * Now service re-assembly to pick up anything that has been
2183 * held on reassembly queue?
2185 sctp_deliver_reasm_check(stcb, asoc);
2186 need_reasm_check = 0;
2188 if (need_reasm_check) {
2189 /* Another one waits ? */
2190 sctp_deliver_reasm_check(stcb, asoc);
2195 int8_t sctp_map_lookup_tab[256] = {
2196 -1, 0, -1, 1, -1, 0, -1, 2,
2197 -1, 0, -1, 1, -1, 0, -1, 3,
2198 -1, 0, -1, 1, -1, 0, -1, 2,
2199 -1, 0, -1, 1, -1, 0, -1, 4,
2200 -1, 0, -1, 1, -1, 0, -1, 2,
2201 -1, 0, -1, 1, -1, 0, -1, 3,
2202 -1, 0, -1, 1, -1, 0, -1, 2,
2203 -1, 0, -1, 1, -1, 0, -1, 5,
2204 -1, 0, -1, 1, -1, 0, -1, 2,
2205 -1, 0, -1, 1, -1, 0, -1, 3,
2206 -1, 0, -1, 1, -1, 0, -1, 2,
2207 -1, 0, -1, 1, -1, 0, -1, 4,
2208 -1, 0, -1, 1, -1, 0, -1, 2,
2209 -1, 0, -1, 1, -1, 0, -1, 3,
2210 -1, 0, -1, 1, -1, 0, -1, 2,
2211 -1, 0, -1, 1, -1, 0, -1, 6,
2212 -1, 0, -1, 1, -1, 0, -1, 2,
2213 -1, 0, -1, 1, -1, 0, -1, 3,
2214 -1, 0, -1, 1, -1, 0, -1, 2,
2215 -1, 0, -1, 1, -1, 0, -1, 4,
2216 -1, 0, -1, 1, -1, 0, -1, 2,
2217 -1, 0, -1, 1, -1, 0, -1, 3,
2218 -1, 0, -1, 1, -1, 0, -1, 2,
2219 -1, 0, -1, 1, -1, 0, -1, 5,
2220 -1, 0, -1, 1, -1, 0, -1, 2,
2221 -1, 0, -1, 1, -1, 0, -1, 3,
2222 -1, 0, -1, 1, -1, 0, -1, 2,
2223 -1, 0, -1, 1, -1, 0, -1, 4,
2224 -1, 0, -1, 1, -1, 0, -1, 2,
2225 -1, 0, -1, 1, -1, 0, -1, 3,
2226 -1, 0, -1, 1, -1, 0, -1, 2,
2227 -1, 0, -1, 1, -1, 0, -1, 7,
2232 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2235 * Now we also need to check the mapping array in a couple of ways.
2236 * 1) Did we move the cum-ack point?
2238 struct sctp_association *asoc;
2240 int last_all_ones = 0;
2241 int slide_from, slide_end, lgap, distance;
2242 uint32_t old_cumack, old_base, old_highest;
2243 unsigned char aux_array[64];
2249 old_cumack = asoc->cumulative_tsn;
2250 old_base = asoc->mapping_array_base_tsn;
2251 old_highest = asoc->highest_tsn_inside_map;
2252 if (asoc->mapping_array_size < 64)
2253 memcpy(aux_array, asoc->mapping_array,
2254 asoc->mapping_array_size);
2256 memcpy(aux_array, asoc->mapping_array, 64);
2259 * We could probably improve this a small bit by calculating the
2260 * offset of the current cum-ack as the starting point.
2263 for (i = 0; i < stcb->asoc.mapping_array_size; i++) {
2265 if (asoc->mapping_array[i] == 0xff) {
2269 /* there is a 0 bit */
2270 at += sctp_map_lookup_tab[asoc->mapping_array[i]];
2275 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2276 /* at is one off, since in the table a embedded -1 is present */
2279 if (compare_with_wrap(asoc->cumulative_tsn,
2280 asoc->highest_tsn_inside_map,
2283 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2284 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2286 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2287 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2288 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2291 if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2292 /* The complete array was completed by a single FR */
2293 /* higest becomes the cum-ack */
2296 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2297 /* clear the array */
2298 clr = (at >> 3) + 1;
2299 if (clr > asoc->mapping_array_size) {
2300 clr = asoc->mapping_array_size;
2302 memset(asoc->mapping_array, 0, clr);
2303 /* base becomes one ahead of the cum-ack */
2304 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2305 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2306 sctp_log_map(old_base, old_cumack, old_highest,
2307 SCTP_MAP_PREPARE_SLIDE);
2308 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2309 asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2311 } else if (at >= 8) {
2312 /* we can slide the mapping array down */
2313 /* Calculate the new byte postion we can move down */
2314 slide_from = at >> 3;
2316 * now calculate the ceiling of the move using our highest
2319 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2320 lgap = asoc->highest_tsn_inside_map -
2321 asoc->mapping_array_base_tsn;
2323 lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2324 asoc->highest_tsn_inside_map + 1;
2326 slide_end = lgap >> 3;
2327 if (slide_end < slide_from) {
2329 panic("impossible slide");
2331 printf("impossible slide?\n");
2335 distance = (slide_end - slide_from) + 1;
2336 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2337 sctp_log_map(old_base, old_cumack, old_highest,
2338 SCTP_MAP_PREPARE_SLIDE);
2339 sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2340 (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2342 if (distance + slide_from > asoc->mapping_array_size ||
2345 * Here we do NOT slide forward the array so that
2346 * hopefully when more data comes in to fill it up
2347 * we will be able to slide it forward. Really I
2348 * don't think this should happen :-0
2351 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2352 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2353 (uint32_t) asoc->mapping_array_size,
2354 SCTP_MAP_SLIDE_NONE);
2359 for (ii = 0; ii < distance; ii++) {
2360 asoc->mapping_array[ii] =
2361 asoc->mapping_array[slide_from + ii];
2363 for (ii = distance; ii <= slide_end; ii++) {
2364 asoc->mapping_array[ii] = 0;
2366 asoc->mapping_array_base_tsn += (slide_from << 3);
2367 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2368 sctp_log_map(asoc->mapping_array_base_tsn,
2369 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2370 SCTP_MAP_SLIDE_RESULT);
2375 * Now we need to see if we need to queue a sack or just start the
2376 * timer (if allowed).
2379 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2381 * Ok special case, in SHUTDOWN-SENT case. here we
2382 * maker sure SACK timer is off and instead send a
2383 * SHUTDOWN and a SACK
2385 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2386 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2387 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2389 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2390 sctp_send_sack(stcb);
2394 /* is there a gap now ? */
2395 is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2396 stcb->asoc.cumulative_tsn, MAX_TSN);
2399 * CMT DAC algorithm: increase number of packets
2400 * received since last ack
2402 stcb->asoc.cmt_dac_pkts_rcvd++;
2404 if ((stcb->asoc.send_sack == 1) || /* We need to send a
2406 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2408 (stcb->asoc.numduptsns) || /* we have dup's */
2409 (is_a_gap) || /* is still a gap */
2410 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2411 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2414 if ((sctp_cmt_on_off) && (sctp_cmt_use_dac) &&
2415 (stcb->asoc.send_sack == 0) &&
2416 (stcb->asoc.numduptsns == 0) &&
2417 (stcb->asoc.delayed_ack) &&
2418 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2421 * CMT DAC algorithm: With CMT,
2422 * delay acks even in the face of
2424 * reordering. Therefore, if acks that
2425 * do not have to be sent because of
2426 * the above reasons, will be
2427 * delayed. That is, acks that would
2428 * have been sent due to gap reports
2429 * will be delayed with DAC. Start
2430 * the delayed ack timer.
2432 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2433 stcb->sctp_ep, stcb, NULL);
2436 * Ok we must build a SACK since the
2437 * timer is pending, we got our
2438 * first packet OR there are gaps or
2441 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2442 sctp_send_sack(stcb);
2445 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2446 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2447 stcb->sctp_ep, stcb, NULL);
2455 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2457 struct sctp_tmit_chunk *chk;
2461 if (asoc->fragmented_delivery_inprogress) {
2462 sctp_service_reassembly(stcb, asoc);
2464 /* Can we proceed further, i.e. the PD-API is complete */
2465 if (asoc->fragmented_delivery_inprogress) {
2470 * Now is there some other chunk I can deliver from the reassembly
2474 chk = TAILQ_FIRST(&asoc->reasmqueue);
2476 asoc->size_on_reasm_queue = 0;
2477 asoc->cnt_on_reasm_queue = 0;
2480 nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2481 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2482 ((nxt_todel == chk->rec.data.stream_seq) ||
2483 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2485 * Yep the first one is here. We setup to start reception,
2486 * by backing down the TSN just in case we can't deliver.
2490 * Before we start though either all of the message should
2491 * be here or 1/4 the socket buffer max or nothing on the
2492 * delivery queue and something can be delivered.
2494 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2495 (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2496 asoc->fragmented_delivery_inprogress = 1;
2497 asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2498 asoc->str_of_pdapi = chk->rec.data.stream_number;
2499 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2500 asoc->pdapi_ppid = chk->rec.data.payloadtype;
2501 asoc->fragment_flags = chk->rec.data.rcv_flags;
2502 sctp_service_reassembly(stcb, asoc);
2503 if (asoc->fragmented_delivery_inprogress == 0) {
2511 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2512 struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2513 struct sctp_nets *net, uint32_t * high_tsn)
2515 struct sctp_data_chunk *ch, chunk_buf;
2516 struct sctp_association *asoc;
2517 int num_chunks = 0; /* number of control chunks processed */
2519 int chk_length, break_flag, last_chunk;
2520 int abort_flag = 0, was_a_gap = 0;
2524 sctp_set_rwnd(stcb, &stcb->asoc);
2527 SCTP_TCB_LOCK_ASSERT(stcb);
2529 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2530 stcb->asoc.cumulative_tsn, MAX_TSN)) {
2531 /* there was a gap before this data was processed */
2535 * setup where we got the last DATA packet from for any SACK that
2536 * may need to go out. Don't bump the net. This is done ONLY when a
2537 * chunk is assigned.
2539 asoc->last_data_chunk_from = net;
2542 * Now before we proceed we must figure out if this is a wasted
2543 * cluster... i.e. it is a small packet sent in and yet the driver
2544 * underneath allocated a full cluster for it. If so we must copy it
2545 * to a smaller mbuf and free up the cluster mbuf. This will help
2546 * with cluster starvation. Note for __Panda__ we don't do this
2547 * since it has clusters all the way down to 64 bytes.
2549 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2550 /* we only handle mbufs that are singletons.. not chains */
2551 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2553 /* ok lets see if we can copy the data up */
2556 /* get the pointers and copy */
2557 to = mtod(m, caddr_t *);
2558 from = mtod((*mm), caddr_t *);
2559 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2560 /* copy the length and free up the old */
2561 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2563 /* sucess, back copy */
2566 /* We are in trouble in the mbuf world .. yikes */
2570 /* get pointer to the first chunk header */
2571 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2572 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2577 * process all DATA chunks...
2579 *high_tsn = asoc->cumulative_tsn;
2581 asoc->data_pkts_seen++;
2582 while (stop_proc == 0) {
2583 /* validate chunk length */
2584 chk_length = ntohs(ch->ch.chunk_length);
2585 if (length - *offset < chk_length) {
2586 /* all done, mutulated chunk */
2590 if (ch->ch.chunk_type == SCTP_DATA) {
2591 if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2593 * Need to send an abort since we had a
2594 * invalid data chunk.
2596 struct mbuf *op_err;
2598 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2599 0, M_DONTWAIT, 1, MT_DATA);
2602 struct sctp_paramhdr *ph;
2605 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2606 (2 * sizeof(uint32_t));
2607 ph = mtod(op_err, struct sctp_paramhdr *);
2609 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2610 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2611 ippp = (uint32_t *) (ph + 1);
2612 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2614 *ippp = asoc->cumulative_tsn;
2617 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2618 sctp_abort_association(inp, stcb, m, iphlen, sh,
2622 #ifdef SCTP_AUDITING_ENABLED
2623 sctp_audit_log(0xB1, 0);
2625 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2630 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2631 chk_length, net, high_tsn, &abort_flag, &break_flag,
2640 * Set because of out of rwnd space and no
2641 * drop rep space left.
2647 /* not a data chunk in the data region */
2648 switch (ch->ch.chunk_type) {
2649 case SCTP_INITIATION:
2650 case SCTP_INITIATION_ACK:
2651 case SCTP_SELECTIVE_ACK:
2652 case SCTP_HEARTBEAT_REQUEST:
2653 case SCTP_HEARTBEAT_ACK:
2654 case SCTP_ABORT_ASSOCIATION:
2656 case SCTP_SHUTDOWN_ACK:
2657 case SCTP_OPERATION_ERROR:
2658 case SCTP_COOKIE_ECHO:
2659 case SCTP_COOKIE_ACK:
2662 case SCTP_SHUTDOWN_COMPLETE:
2663 case SCTP_AUTHENTICATION:
2664 case SCTP_ASCONF_ACK:
2665 case SCTP_PACKET_DROPPED:
2666 case SCTP_STREAM_RESET:
2667 case SCTP_FORWARD_CUM_TSN:
2670 * Now, what do we do with KNOWN chunks that
2671 * are NOT in the right place?
2673 * For now, I do nothing but ignore them. We
2674 * may later want to add sysctl stuff to
2675 * switch out and do either an ABORT() or
2676 * possibly process them.
2678 if (sctp_strict_data_order) {
2679 struct mbuf *op_err;
2681 op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2682 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0);
2687 /* unknown chunk type, use bit rules */
2688 if (ch->ch.chunk_type & 0x40) {
2689 /* Add a error report to the queue */
2691 struct sctp_paramhdr *phd;
2693 merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2695 phd = mtod(merr, struct sctp_paramhdr *);
2697 * We cheat and use param
2698 * type since we did not
2699 * bother to define a error
2700 * cause struct. They are
2701 * the same basic format
2702 * with different names.
2705 htons(SCTP_CAUSE_UNRECOG_CHUNK);
2707 htons(chk_length + sizeof(*phd));
2708 SCTP_BUF_LEN(merr) = sizeof(*phd);
2709 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2710 SCTP_SIZE32(chk_length),
2712 if (SCTP_BUF_NEXT(merr)) {
2713 sctp_queue_op_err(stcb, merr);
2719 if ((ch->ch.chunk_type & 0x80) == 0) {
2720 /* discard the rest of this packet */
2722 } /* else skip this bad chunk and
2725 }; /* switch of chunk type */
2727 *offset += SCTP_SIZE32(chk_length);
2728 if ((*offset >= length) || stop_proc) {
2729 /* no more data left in the mbuf chain */
2733 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2734 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2744 * we need to report rwnd overrun drops.
2746 sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2750 * Did we get data, if so update the time for auto-close and
2751 * give peer credit for being alive.
2753 SCTP_STAT_INCR(sctps_recvpktwithdata);
2754 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
2755 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2756 stcb->asoc.overall_error_count,
2758 SCTP_FROM_SCTP_INDATA,
2761 stcb->asoc.overall_error_count = 0;
2762 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2764 /* now service all of the reassm queue if needed */
2765 if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2766 sctp_service_queues(stcb, asoc);
2768 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2769 /* Assure that we ack right away */
2770 stcb->asoc.send_sack = 1;
2772 /* Start a sack timer or QUEUE a SACK for sending */
2773 if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
2774 (stcb->asoc.mapping_array[0] != 0xff)) {
2775 if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
2776 (stcb->asoc.delayed_ack == 0) ||
2777 (stcb->asoc.send_sack == 1)) {
2778 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2779 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2781 sctp_send_sack(stcb);
2783 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2784 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2785 stcb->sctp_ep, stcb, NULL);
2789 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2798 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2799 struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2800 uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2801 int num_seg, int *ecn_seg_sums)
2803 /************************************************/
2804 /* process fragments and update sendqueue */
2805 /************************************************/
2806 struct sctp_sack *sack;
2807 struct sctp_gap_ack_block *frag, block;
2808 struct sctp_tmit_chunk *tp1;
2813 uint16_t frag_strt, frag_end, primary_flag_set;
2814 u_long last_frag_high;
2817 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
2819 if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
2820 primary_flag_set = 1;
2822 primary_flag_set = 0;
2826 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2827 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2828 *offset += sizeof(block);
2834 for (i = 0; i < num_seg; i++) {
2835 frag_strt = ntohs(frag->start);
2836 frag_end = ntohs(frag->end);
2837 /* some sanity checks on the fargment offsets */
2838 if (frag_strt > frag_end) {
2839 /* this one is malformed, skip */
2843 if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
2845 *biggest_tsn_acked = frag_end + last_tsn;
2847 /* mark acked dgs and find out the highestTSN being acked */
2849 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2851 /* save the locations of the last frags */
2852 last_frag_high = frag_end + last_tsn;
2855 * now lets see if we need to reset the queue due to
2856 * a out-of-order SACK fragment
2858 if (compare_with_wrap(frag_strt + last_tsn,
2859 last_frag_high, MAX_TSN)) {
2861 * if the new frag starts after the last TSN
2862 * frag covered, we are ok and this one is
2863 * beyond the last one
2868 * ok, they have reset us, so we need to
2869 * reset the queue this will cause extra
2870 * hunting but hey, they chose the
2871 * performance hit when they failed to order
2874 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2876 last_frag_high = frag_end + last_tsn;
2878 for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
2880 if (tp1->rec.data.doing_fast_retransmit)
2884 * CMT: CUCv2 algorithm. For each TSN being
2885 * processed from the sent queue, track the
2886 * next expected pseudo-cumack, or
2887 * rtx_pseudo_cumack, if required. Separate
2888 * cumack trackers for first transmissions,
2889 * and retransmissions.
2891 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2892 (tp1->snd_count == 1)) {
2893 tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2894 tp1->whoTo->find_pseudo_cumack = 0;
2896 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2897 (tp1->snd_count > 1)) {
2898 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2899 tp1->whoTo->find_rtx_pseudo_cumack = 0;
2901 if (tp1->rec.data.TSN_seq == j) {
2902 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2904 * must be held until
2908 * ECN Nonce: Add the nonce
2909 * value to the sender's
2912 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2914 * If it is less than RESEND, it is
2915 * now no-longer in flight.
2916 * Higher values may already be set
2917 * via previous Gap Ack Blocks...
2918 * i.e. ACKED or RESEND.
2920 if (compare_with_wrap(tp1->rec.data.TSN_seq,
2921 *biggest_newly_acked_tsn, MAX_TSN)) {
2922 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2931 * this_sack_highest_
2935 if (tp1->rec.data.chunk_was_revoked == 0)
2936 tp1->whoTo->saw_newack = 1;
2938 if (compare_with_wrap(tp1->rec.data.TSN_seq,
2939 tp1->whoTo->this_sack_highest_newack,
2941 tp1->whoTo->this_sack_highest_newack =
2942 tp1->rec.data.TSN_seq;
2947 * this_sack_lowest_n
2950 if (*this_sack_lowest_newack == 0) {
2951 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2952 sctp_log_sack(*this_sack_lowest_newack,
2954 tp1->rec.data.TSN_seq,
2957 SCTP_LOG_TSN_ACKED);
2959 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2964 * (rtx-)pseudo-cumac
2969 * (rtx-)pseudo-cumac
2971 * new_(rtx_)pseudo_c
2979 * (rtx-)pseudo-cumac
2987 if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2988 if (tp1->rec.data.chunk_was_revoked == 0) {
2989 tp1->whoTo->new_pseudo_cumack = 1;
2991 tp1->whoTo->find_pseudo_cumack = 1;
2993 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
2994 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2996 if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2997 if (tp1->rec.data.chunk_was_revoked == 0) {
2998 tp1->whoTo->new_pseudo_cumack = 1;
3000 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3002 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3003 sctp_log_sack(*biggest_newly_acked_tsn,
3005 tp1->rec.data.TSN_seq,
3008 SCTP_LOG_TSN_ACKED);
3010 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3011 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3012 tp1->whoTo->flight_size,
3014 (uintptr_t) tp1->whoTo,
3015 tp1->rec.data.TSN_seq);
3017 sctp_flight_size_decrease(tp1);
3018 sctp_total_flight_decrease(stcb, tp1);
3020 tp1->whoTo->net_ack += tp1->send_size;
3021 if (tp1->snd_count < 2) {
3027 tp1->whoTo->net_ack2 += tp1->send_size;
3034 sctp_calculate_rto(stcb,
3037 &tp1->sent_rcv_time,
3038 sctp_align_safe_nocopy);
3043 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3044 (*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3045 (*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3046 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3047 asoc->this_sack_highest_gap,
3049 asoc->this_sack_highest_gap =
3050 tp1->rec.data.TSN_seq;
3052 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3053 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3054 #ifdef SCTP_AUDITING_ENABLED
3055 sctp_audit_log(0xB2,
3056 (asoc->sent_queue_retran_cnt & 0x000000ff));
3061 * All chunks NOT UNSENT
3062 * fall through here and are
3065 tp1->sent = SCTP_DATAGRAM_MARKED;
3066 if (tp1->rec.data.chunk_was_revoked) {
3067 /* deflate the cwnd */
3068 tp1->whoTo->cwnd -= tp1->book_size;
3069 tp1->rec.data.chunk_was_revoked = 0;
3073 } /* if (tp1->TSN_seq == j) */
3074 if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
3078 tp1 = TAILQ_NEXT(tp1, sctp_next);
3079 } /* end while (tp1) */
3080 } /* end for (j = fragStart */
3081 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3082 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3083 *offset += sizeof(block);
3088 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3090 sctp_log_fr(*biggest_tsn_acked,
3091 *biggest_newly_acked_tsn,
3092 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3097 sctp_check_for_revoked(struct sctp_tcb *stcb,
3098 struct sctp_association *asoc, uint32_t cumack,
3099 u_long biggest_tsn_acked)
3101 struct sctp_tmit_chunk *tp1;
3102 int tot_revoked = 0;
3104 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3106 if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3109 * ok this guy is either ACK or MARKED. If it is
3110 * ACKED it has been previously acked but not this
3111 * time i.e. revoked. If it is MARKED it was ACK'ed
3114 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3119 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3120 /* it has been revoked */
3121 tp1->sent = SCTP_DATAGRAM_SENT;
3122 tp1->rec.data.chunk_was_revoked = 1;
3124 * We must add this stuff back in to assure
3125 * timers and such get started.
3127 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3128 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3129 tp1->whoTo->flight_size,
3131 (uintptr_t) tp1->whoTo,
3132 tp1->rec.data.TSN_seq);
3134 sctp_flight_size_increase(tp1);
3135 sctp_total_flight_increase(stcb, tp1);
3137 * We inflate the cwnd to compensate for our
3138 * artificial inflation of the flight_size.
3140 tp1->whoTo->cwnd += tp1->book_size;
3142 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3143 sctp_log_sack(asoc->last_acked_seq,
3145 tp1->rec.data.TSN_seq,
3148 SCTP_LOG_TSN_REVOKED);
3150 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3151 /* it has been re-acked in this SACK */
3152 tp1->sent = SCTP_DATAGRAM_ACKED;
3155 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3157 tp1 = TAILQ_NEXT(tp1, sctp_next);
3159 if (tot_revoked > 0) {
3161 * Setup the ecn nonce re-sync point. We do this since once
3162 * data is revoked we begin to retransmit things, which do
3163 * NOT have the ECN bits set. This means we are now out of
3164 * sync and must wait until we get back in sync with the
3165 * peer to check ECN bits.
3167 tp1 = TAILQ_FIRST(&asoc->send_queue);
3169 asoc->nonce_resync_tsn = asoc->sending_seq;
3171 asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3173 asoc->nonce_wait_for_ecne = 0;
3174 asoc->nonce_sum_check = 0;
3179 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3180 u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3182 struct sctp_tmit_chunk *tp1;
3183 int strike_flag = 0;
3185 int tot_retrans = 0;
3186 uint32_t sending_seq;
3187 struct sctp_nets *net;
3188 int num_dests_sacked = 0;
3191 * select the sending_seq, this is either the next thing ready to be
3192 * sent but not transmitted, OR, the next seq we assign.
3194 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3196 sending_seq = asoc->sending_seq;
3198 sending_seq = tp1->rec.data.TSN_seq;
3201 /* CMT DAC algo: finding out if SACK is a mixed SACK */
3202 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3203 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3204 if (net->saw_newack)
3208 if (stcb->asoc.peer_supports_prsctp) {
3209 (void)SCTP_GETTIME_TIMEVAL(&now);
3211 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3214 if (tp1->no_fr_allowed) {
3215 /* this one had a timeout or something */
3216 tp1 = TAILQ_NEXT(tp1, sctp_next);
3219 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3220 if (tp1->sent < SCTP_DATAGRAM_RESEND)
3221 sctp_log_fr(biggest_tsn_newly_acked,
3222 tp1->rec.data.TSN_seq,
3224 SCTP_FR_LOG_CHECK_STRIKE);
3226 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3228 tp1->sent == SCTP_DATAGRAM_UNSENT) {
3232 if (stcb->asoc.peer_supports_prsctp) {
3233 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3234 /* Is it expired? */
3236 (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3238 /* Yes so drop it */
3239 if (tp1->data != NULL) {
3240 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3241 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3242 &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3244 tp1 = TAILQ_NEXT(tp1, sctp_next);
3248 if ((PR_SCTP_RTX_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3249 /* Has it been retransmitted tv_sec times? */
3250 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3251 /* Yes, so drop it */
3252 if (tp1->data != NULL) {
3253 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3254 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3255 &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3257 tp1 = TAILQ_NEXT(tp1, sctp_next);
3262 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3263 asoc->this_sack_highest_gap, MAX_TSN)) {
3264 /* we are beyond the tsn in the sack */
3267 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3268 /* either a RESEND, ACKED, or MARKED */
3270 tp1 = TAILQ_NEXT(tp1, sctp_next);
3274 * CMT : SFR algo (covers part of DAC and HTNA as well)
3276 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3278 * No new acks were receieved for data sent to this
3279 * dest. Therefore, according to the SFR algo for
3280 * CMT, no data sent to this dest can be marked for
3281 * FR using this SACK.
3283 tp1 = TAILQ_NEXT(tp1, sctp_next);
3285 } else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3286 tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3288 * CMT: New acks were receieved for data sent to
3289 * this dest. But no new acks were seen for data
3290 * sent after tp1. Therefore, according to the SFR
3291 * algo for CMT, tp1 cannot be marked for FR using
3292 * this SACK. This step covers part of the DAC algo
3293 * and the HTNA algo as well.
3295 tp1 = TAILQ_NEXT(tp1, sctp_next);
3299 * Here we check to see if we were have already done a FR
3300 * and if so we see if the biggest TSN we saw in the sack is
3301 * smaller than the recovery point. If so we don't strike
3302 * the tsn... otherwise we CAN strike the TSN.
3305 * @@@ JRI: Check for CMT if (accum_moved &&
3306 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3309 if (accum_moved && asoc->fast_retran_loss_recovery) {
3311 * Strike the TSN if in fast-recovery and cum-ack
3314 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3315 sctp_log_fr(biggest_tsn_newly_acked,
3316 tp1->rec.data.TSN_seq,
3318 SCTP_FR_LOG_STRIKE_CHUNK);
3320 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3323 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3325 * CMT DAC algorithm: If SACK flag is set to
3326 * 0, then lowest_newack test will not pass
3327 * because it would have been set to the
3328 * cumack earlier. If not already to be
3329 * rtx'd, If not a mixed sack and if tp1 is
3330 * not between two sacked TSNs, then mark by
3331 * one more. NOTE that we are marking by one
3332 * additional time since the SACK DAC flag
3333 * indicates that two packets have been
3334 * received after this missing TSN.
3336 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3337 compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3338 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3339 sctp_log_fr(16 + num_dests_sacked,
3340 tp1->rec.data.TSN_seq,
3342 SCTP_FR_LOG_STRIKE_CHUNK);
3347 } else if (tp1->rec.data.doing_fast_retransmit) {
3349 * For those that have done a FR we must take
3350 * special consideration if we strike. I.e the
3351 * biggest_newly_acked must be higher than the
3352 * sending_seq at the time we did the FR.
3355 #ifdef SCTP_FR_TO_ALTERNATE
3357 * If FR's go to new networks, then we must only do
3358 * this for singly homed asoc's. However if the FR's
3359 * go to the same network (Armando's work) then its
3360 * ok to FR multiple times.
3368 if ((compare_with_wrap(biggest_tsn_newly_acked,
3369 tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3370 (biggest_tsn_newly_acked ==
3371 tp1->rec.data.fast_retran_tsn)) {
3373 * Strike the TSN, since this ack is
3374 * beyond where things were when we
3377 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3378 sctp_log_fr(biggest_tsn_newly_acked,
3379 tp1->rec.data.TSN_seq,
3381 SCTP_FR_LOG_STRIKE_CHUNK);
3383 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3387 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3389 * CMT DAC algorithm: If
3390 * SACK flag is set to 0,
3391 * then lowest_newack test
3392 * will not pass because it
3393 * would have been set to
3394 * the cumack earlier. If
3395 * not already to be rtx'd,
3396 * If not a mixed sack and
3397 * if tp1 is not between two
3398 * sacked TSNs, then mark by
3399 * one more. NOTE that we
3400 * are marking by one
3401 * additional time since the
3402 * SACK DAC flag indicates
3403 * that two packets have
3404 * been received after this
3407 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3408 (num_dests_sacked == 1) &&
3409 compare_with_wrap(this_sack_lowest_newack,
3410 tp1->rec.data.TSN_seq, MAX_TSN)) {
3411 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3412 sctp_log_fr(32 + num_dests_sacked,
3413 tp1->rec.data.TSN_seq,
3415 SCTP_FR_LOG_STRIKE_CHUNK);
3417 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3425 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3428 } else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3429 biggest_tsn_newly_acked, MAX_TSN)) {
3431 * We don't strike these: This is the HTNA
3432 * algorithm i.e. we don't strike If our TSN is
3433 * larger than the Highest TSN Newly Acked.
3437 /* Strike the TSN */
3438 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3439 sctp_log_fr(biggest_tsn_newly_acked,
3440 tp1->rec.data.TSN_seq,
3442 SCTP_FR_LOG_STRIKE_CHUNK);
3444 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3447 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3449 * CMT DAC algorithm: If SACK flag is set to
3450 * 0, then lowest_newack test will not pass
3451 * because it would have been set to the
3452 * cumack earlier. If not already to be
3453 * rtx'd, If not a mixed sack and if tp1 is
3454 * not between two sacked TSNs, then mark by
3455 * one more. NOTE that we are marking by one
3456 * additional time since the SACK DAC flag
3457 * indicates that two packets have been
3458 * received after this missing TSN.
3460 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3461 compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3462 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3463 sctp_log_fr(48 + num_dests_sacked,
3464 tp1->rec.data.TSN_seq,
3466 SCTP_FR_LOG_STRIKE_CHUNK);
3472 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3473 /* Increment the count to resend */
3474 struct sctp_nets *alt;
3476 /* printf("OK, we are now ready to FR this guy\n"); */
3477 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3478 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3482 /* This is a subsequent FR */
3483 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3485 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3486 if (sctp_cmt_on_off) {
3488 * CMT: Using RTX_SSTHRESH policy for CMT.
3489 * If CMT is being used, then pick dest with
3490 * largest ssthresh for any retransmission.
3492 tp1->no_fr_allowed = 1;
3494 /* sa_ignore NO_NULL_CHK */
3495 if (sctp_cmt_on_off && sctp_cmt_pf) {
3497 * JRS 5/18/07 - If CMT PF is on,
3498 * use the PF version of
3501 alt = sctp_find_alternate_net(stcb, alt, 2);
3504 * JRS 5/18/07 - If only CMT is on,
3505 * use the CMT version of
3508 /* sa_ignore NO_NULL_CHK */
3509 alt = sctp_find_alternate_net(stcb, alt, 1);
3515 * CUCv2: If a different dest is picked for
3516 * the retransmission, then new
3517 * (rtx-)pseudo_cumack needs to be tracked
3518 * for orig dest. Let CUCv2 track new (rtx-)
3519 * pseudo-cumack always.
3522 tp1->whoTo->find_pseudo_cumack = 1;
3523 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3525 } else {/* CMT is OFF */
3527 #ifdef SCTP_FR_TO_ALTERNATE
3528 /* Can we find an alternate? */
3529 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3532 * default behavior is to NOT retransmit
3533 * FR's to an alternate. Armando Caro's
3534 * paper details why.
3540 tp1->rec.data.doing_fast_retransmit = 1;
3542 /* mark the sending seq for possible subsequent FR's */
3544 * printf("Marking TSN for FR new value %x\n",
3545 * (uint32_t)tpi->rec.data.TSN_seq);
3547 if (TAILQ_EMPTY(&asoc->send_queue)) {
3549 * If the queue of send is empty then its
3550 * the next sequence number that will be
3551 * assigned so we subtract one from this to
3552 * get the one we last sent.
3554 tp1->rec.data.fast_retran_tsn = sending_seq;
3557 * If there are chunks on the send queue
3558 * (unsent data that has made it from the
3559 * stream queues but not out the door, we
3560 * take the first one (which will have the
3561 * lowest TSN) and subtract one to get the
3564 struct sctp_tmit_chunk *ttt;
3566 ttt = TAILQ_FIRST(&asoc->send_queue);
3567 tp1->rec.data.fast_retran_tsn =
3568 ttt->rec.data.TSN_seq;
3573 * this guy had a RTO calculation pending on
3578 /* fix counts and things */
3579 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3580 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3581 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3583 (uintptr_t) tp1->whoTo,
3584 tp1->rec.data.TSN_seq);
3587 tp1->whoTo->net_ack++;
3588 sctp_flight_size_decrease(tp1);
3590 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
3591 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3592 asoc->peers_rwnd, tp1->send_size, sctp_peer_chunk_oh);
3594 /* add back to the rwnd */
3595 asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
3597 /* remove from the total flight */
3598 sctp_total_flight_decrease(stcb, tp1);
3599 if (alt != tp1->whoTo) {
3600 /* yes, there is an alternate. */
3601 sctp_free_remote_addr(tp1->whoTo);
3602 /* sa_ignore FREED_MEMORY */
3604 atomic_add_int(&alt->ref_count, 1);
3607 tp1 = TAILQ_NEXT(tp1, sctp_next);
3610 if (tot_retrans > 0) {
3612 * Setup the ecn nonce re-sync point. We do this since once
3613 * we go to FR something we introduce a Karn's rule scenario
3614 * and won't know the totals for the ECN bits.
3616 asoc->nonce_resync_tsn = sending_seq;
3617 asoc->nonce_wait_for_ecne = 0;
3618 asoc->nonce_sum_check = 0;
3622 struct sctp_tmit_chunk *
3623 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3624 struct sctp_association *asoc)
3626 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3630 if (asoc->peer_supports_prsctp == 0) {
3633 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3635 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3636 tp1->sent != SCTP_DATAGRAM_RESEND) {
3637 /* no chance to advance, out of here */
3640 if (!PR_SCTP_ENABLED(tp1->flags)) {
3642 * We can't fwd-tsn past any that are reliable aka
3643 * retransmitted until the asoc fails.
3648 (void)SCTP_GETTIME_TIMEVAL(&now);
3651 tp2 = TAILQ_NEXT(tp1, sctp_next);
3653 * now we got a chunk which is marked for another
3654 * retransmission to a PR-stream but has run out its chances
3655 * already maybe OR has been marked to skip now. Can we skip
3656 * it if its a resend?
3658 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3659 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3661 * Now is this one marked for resend and its time is
3664 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3665 /* Yes so drop it */
3667 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3668 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3669 &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3673 * No, we are done when hit one for resend
3674 * whos time as not expired.
3680 * Ok now if this chunk is marked to drop it we can clean up
3681 * the chunk, advance our peer ack point and we can check
3684 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3685 /* advance PeerAckPoint goes forward */
3686 asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3689 * we don't want to de-queue it here. Just wait for
3690 * the next peer SACK to come with a new cumTSN and
3691 * then the chunk will be droped in the normal
3695 sctp_free_bufspace(stcb, asoc, tp1, 1);
3697 * Maybe there should be another
3700 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3701 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3702 tp1, SCTP_SO_NOT_LOCKED);
3703 sctp_m_freem(tp1->data);
3705 if (stcb->sctp_socket) {
3706 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3709 so = SCTP_INP_SO(stcb->sctp_ep);
3710 atomic_add_int(&stcb->asoc.refcnt, 1);
3711 SCTP_TCB_UNLOCK(stcb);
3712 SCTP_SOCKET_LOCK(so, 1);
3713 SCTP_TCB_LOCK(stcb);
3714 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3715 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3717 * assoc was freed while we
3720 SCTP_SOCKET_UNLOCK(so, 1);
3724 sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
3725 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3726 SCTP_SOCKET_UNLOCK(so, 1);
3728 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
3729 sctp_wakeup_log(stcb, tp1->rec.data.TSN_seq, 1, SCTP_WAKESND_FROM_FWDTSN);
3735 * If it is still in RESEND we can advance no
3741 * If we hit here we just dumped tp1, move to next tsn on
3750 sctp_fs_audit(struct sctp_association *asoc)
3752 struct sctp_tmit_chunk *chk;
3753 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3755 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3756 if (chk->sent < SCTP_DATAGRAM_RESEND) {
3758 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3760 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3762 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3769 if ((inflight > 0) || (inbetween > 0)) {
3771 panic("Flight size-express incorrect? \n");
3773 SCTP_PRINTF("Flight size-express incorrect inflight:%d inbetween:%d\n",
3774 inflight, inbetween);
3781 sctp_window_probe_recovery(struct sctp_tcb *stcb,
3782 struct sctp_association *asoc,
3783 struct sctp_nets *net,
3784 struct sctp_tmit_chunk *tp1)
3786 struct sctp_tmit_chunk *chk;
3788 /* First setup this one and get it moved back */
3789 tp1->sent = SCTP_DATAGRAM_UNSENT;
3790 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3791 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3792 tp1->whoTo->flight_size,
3794 (uintptr_t) tp1->whoTo,
3795 tp1->rec.data.TSN_seq);
3797 sctp_flight_size_decrease(tp1);
3798 sctp_total_flight_decrease(stcb, tp1);
3799 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3800 TAILQ_INSERT_HEAD(&asoc->send_queue, tp1, sctp_next);
3801 asoc->sent_queue_cnt--;
3802 asoc->send_queue_cnt++;
3804 * Now all guys marked for RESEND on the sent_queue must be moved
3807 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3808 if (chk->sent == SCTP_DATAGRAM_RESEND) {
3809 /* Another chunk to move */
3810 chk->sent = SCTP_DATAGRAM_UNSENT;
3811 /* It should not be in flight */
3812 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3813 TAILQ_INSERT_AFTER(&asoc->send_queue, tp1, chk, sctp_next);
3814 asoc->sent_queue_cnt--;
3815 asoc->send_queue_cnt++;
3816 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3822 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3823 uint32_t rwnd, int nonce_sum_flag, int *abort_now)
3825 struct sctp_nets *net;
3826 struct sctp_association *asoc;
3827 struct sctp_tmit_chunk *tp1, *tp2;
3829 int win_probe_recovery = 0;
3830 int win_probe_recovered = 0;
3831 int j, done_once = 0;
3833 if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3834 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3835 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3837 SCTP_TCB_LOCK_ASSERT(stcb);
3838 #ifdef SCTP_ASOCLOG_OF_TSNS
3839 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3840 stcb->asoc.cumack_log_at++;
3841 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3842 stcb->asoc.cumack_log_at = 0;
3846 old_rwnd = asoc->peers_rwnd;
3847 if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
3850 } else if (asoc->last_acked_seq == cumack) {
3851 /* Window update sack */
3852 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3853 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
3854 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3855 /* SWS sender side engages */
3856 asoc->peers_rwnd = 0;
3858 if (asoc->peers_rwnd > old_rwnd) {
3863 /* First setup for CC stuff */
3864 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3865 net->prev_cwnd = net->cwnd;
3870 * CMT: Reset CUC and Fast recovery algo variables before
3873 net->new_pseudo_cumack = 0;
3874 net->will_exit_fast_recovery = 0;
3876 if (sctp_strict_sacks) {
3879 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3880 tp1 = TAILQ_LAST(&asoc->sent_queue,
3881 sctpchunk_listhead);
3882 send_s = tp1->rec.data.TSN_seq + 1;
3884 send_s = asoc->sending_seq;
3886 if ((cumack == send_s) ||
3887 compare_with_wrap(cumack, send_s, MAX_TSN)) {
3893 panic("Impossible sack 1");
3897 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3898 0, M_DONTWAIT, 1, MT_DATA);
3900 struct sctp_paramhdr *ph;
3903 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3905 ph = mtod(oper, struct sctp_paramhdr *);
3906 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3907 ph->param_length = htons(SCTP_BUF_LEN(oper));
3908 ippp = (uint32_t *) (ph + 1);
3909 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3911 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3912 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3917 asoc->this_sack_highest_gap = cumack;
3918 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
3919 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3920 stcb->asoc.overall_error_count,
3922 SCTP_FROM_SCTP_INDATA,
3925 stcb->asoc.overall_error_count = 0;
3926 if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
3927 /* process the new consecutive TSN first */
3928 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3930 tp2 = TAILQ_NEXT(tp1, sctp_next);
3931 if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
3933 cumack == tp1->rec.data.TSN_seq) {
3934 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3935 printf("Warning, an unsent is now acked?\n");
3938 * ECN Nonce: Add the nonce to the sender's
3941 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
3942 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3944 * If it is less than ACKED, it is
3945 * now no-longer in flight. Higher
3946 * values may occur during marking
3948 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3949 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3950 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3951 tp1->whoTo->flight_size,
3953 (uintptr_t) tp1->whoTo,
3954 tp1->rec.data.TSN_seq);
3956 sctp_flight_size_decrease(tp1);
3957 /* sa_ignore NO_NULL_CHK */
3958 sctp_total_flight_decrease(stcb, tp1);
3960 tp1->whoTo->net_ack += tp1->send_size;
3961 if (tp1->snd_count < 2) {
3963 * True non-retransmited
3966 tp1->whoTo->net_ack2 +=
3969 /* update RTO too? */
3976 sctp_calculate_rto(stcb,
3978 &tp1->sent_rcv_time,
3979 sctp_align_safe_nocopy);
3984 * CMT: CUCv2 algorithm. From the
3985 * cumack'd TSNs, for each TSN being
3986 * acked for the first time, set the
3987 * following variables for the
3988 * corresp destination.
3989 * new_pseudo_cumack will trigger a
3991 * find_(rtx_)pseudo_cumack will
3992 * trigger search for the next
3993 * expected (rtx-)pseudo-cumack.
3995 tp1->whoTo->new_pseudo_cumack = 1;
3996 tp1->whoTo->find_pseudo_cumack = 1;
3997 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3999 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4000 /* sa_ignore NO_NULL_CHK */
4001 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4004 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4005 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4007 if (tp1->rec.data.chunk_was_revoked) {
4008 /* deflate the cwnd */
4009 tp1->whoTo->cwnd -= tp1->book_size;
4010 tp1->rec.data.chunk_was_revoked = 0;
4012 tp1->sent = SCTP_DATAGRAM_ACKED;
4013 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4015 /* sa_ignore NO_NULL_CHK */
4016 sctp_free_bufspace(stcb, asoc, tp1, 1);
4017 sctp_m_freem(tp1->data);
4019 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4020 sctp_log_sack(asoc->last_acked_seq,
4022 tp1->rec.data.TSN_seq,
4025 SCTP_LOG_FREE_SENT);
4028 asoc->sent_queue_cnt--;
4029 sctp_free_a_chunk(stcb, tp1);
4037 /* sa_ignore NO_NULL_CHK */
4038 if (stcb->sctp_socket) {
4039 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4044 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4045 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4046 /* sa_ignore NO_NULL_CHK */
4047 sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4049 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4050 so = SCTP_INP_SO(stcb->sctp_ep);
4051 atomic_add_int(&stcb->asoc.refcnt, 1);
4052 SCTP_TCB_UNLOCK(stcb);
4053 SCTP_SOCKET_LOCK(so, 1);
4054 SCTP_TCB_LOCK(stcb);
4055 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4056 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4057 /* assoc was freed while we were unlocked */
4058 SCTP_SOCKET_UNLOCK(so, 1);
4062 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4063 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4064 SCTP_SOCKET_UNLOCK(so, 1);
4067 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4068 sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4072 /* JRS - Use the congestion control given in the CC module */
4073 if (asoc->last_acked_seq != cumack)
4074 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4076 asoc->last_acked_seq = cumack;
4078 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4079 /* nothing left in-flight */
4080 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4081 net->flight_size = 0;
4082 net->partial_bytes_acked = 0;
4084 asoc->total_flight = 0;
4085 asoc->total_flight_count = 0;
4087 /* Fix up the a-p-a-p for future PR-SCTP sends */
4088 if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4089 asoc->advanced_peer_ack_point = cumack;
4091 /* ECN Nonce updates */
4092 if (asoc->ecn_nonce_allowed) {
4093 if (asoc->nonce_sum_check) {
4094 if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4095 if (asoc->nonce_wait_for_ecne == 0) {
4096 struct sctp_tmit_chunk *lchk;
4098 lchk = TAILQ_FIRST(&asoc->send_queue);
4099 asoc->nonce_wait_for_ecne = 1;
4101 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4103 asoc->nonce_wait_tsn = asoc->sending_seq;
4106 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4107 (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4109 * Misbehaving peer. We need
4110 * to react to this guy
4112 asoc->ecn_allowed = 0;
4113 asoc->ecn_nonce_allowed = 0;
4118 /* See if Resynchronization Possible */
4119 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4120 asoc->nonce_sum_check = 1;
4122 * now we must calculate what the base is.
4123 * We do this based on two things, we know
4124 * the total's for all the segments
4125 * gap-acked in the SACK (none), We also
4126 * know the SACK's nonce sum, its in
4127 * nonce_sum_flag. So we can build a truth
4128 * table to back-calculate the new value of
4129 * asoc->nonce_sum_expect_base:
4131 * SACK-flag-Value Seg-Sums Base 0 0 0
4135 asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4140 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4141 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4142 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4143 /* SWS sender side engages */
4144 asoc->peers_rwnd = 0;
4146 if (asoc->peers_rwnd > old_rwnd) {
4147 win_probe_recovery = 1;
4149 /* Now assure a timer where data is queued at */
4152 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4153 if (win_probe_recovery && (net->window_probe)) {
4154 net->window_probe = 0;
4155 win_probe_recovered = 1;
4157 * Find first chunk that was used with window probe
4158 * and clear the sent
4160 /* sa_ignore FREED_MEMORY */
4161 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4162 if (tp1->window_probe) {
4163 /* move back to data send queue */
4164 sctp_window_probe_recovery(stcb, asoc, net, tp1);
4169 if (net->flight_size) {
4172 if (net->RTO == 0) {
4173 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4175 to_ticks = MSEC_TO_TICKS(net->RTO);
4178 (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4179 sctp_timeout_handler, &net->rxt_timer);
4181 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4182 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4184 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4186 if (sctp_early_fr) {
4187 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4188 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4189 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4190 SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4196 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4197 (asoc->sent_queue_retran_cnt == 0) &&
4198 (win_probe_recovered == 0) &&
4200 /* huh, this should not happen */
4201 sctp_fs_audit(asoc);
4202 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4203 net->flight_size = 0;
4205 asoc->total_flight = 0;
4206 asoc->total_flight_count = 0;
4207 asoc->sent_queue_retran_cnt = 0;
4208 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4209 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4210 sctp_flight_size_increase(tp1);
4211 sctp_total_flight_increase(stcb, tp1);
4212 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4213 asoc->sent_queue_retran_cnt++;
4219 /**********************************/
4220 /* Now what about shutdown issues */
4221 /**********************************/
4222 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4223 /* nothing left on sendqueue.. consider done */
4225 if ((asoc->stream_queue_cnt == 1) &&
4226 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4227 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4228 (asoc->locked_on_sending)
4230 struct sctp_stream_queue_pending *sp;
4233 * I may be in a state where we got all across.. but
4234 * cannot write more due to a shutdown... we abort
4235 * since the user did not indicate EOR in this case.
4236 * The sp will be cleaned during free of the asoc.
4238 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4240 if ((sp) && (sp->length == 0)) {
4241 /* Let cleanup code purge it */
4242 if (sp->msg_is_complete) {
4243 asoc->stream_queue_cnt--;
4245 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4246 asoc->locked_on_sending = NULL;
4247 asoc->stream_queue_cnt--;
4251 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4252 (asoc->stream_queue_cnt == 0)) {
4253 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4254 /* Need to abort here */
4260 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4261 0, M_DONTWAIT, 1, MT_DATA);
4263 struct sctp_paramhdr *ph;
4266 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4268 ph = mtod(oper, struct sctp_paramhdr *);
4269 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4270 ph->param_length = htons(SCTP_BUF_LEN(oper));
4271 ippp = (uint32_t *) (ph + 1);
4272 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4274 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4275 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4277 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4278 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4279 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4281 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
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_send_shutdown_ack(stcb,
4298 stcb->asoc.primary_destination);
4300 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4301 stcb->sctp_ep, stcb, asoc->primary_destination);
4304 if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
4305 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4307 stcb->asoc.peers_rwnd,
4308 stcb->asoc.total_flight,
4309 stcb->asoc.total_output_queue_size);
4314 sctp_handle_sack(struct mbuf *m, int offset,
4315 struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4316 struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4318 struct sctp_association *asoc;
4319 struct sctp_sack *sack;
4320 struct sctp_tmit_chunk *tp1, *tp2;
4321 uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4322 this_sack_lowest_newack;
4323 uint32_t sav_cum_ack;
4324 uint16_t num_seg, num_dup;
4325 uint16_t wake_him = 0;
4326 unsigned int sack_length;
4327 uint32_t send_s = 0;
4329 int accum_moved = 0;
4330 int will_exit_fast_recovery = 0;
4331 uint32_t a_rwnd, old_rwnd;
4332 int win_probe_recovery = 0;
4333 int win_probe_recovered = 0;
4334 struct sctp_nets *net = NULL;
4335 int nonce_sum_flag, ecn_seg_sums = 0;
4337 uint8_t reneged_all = 0;
4338 uint8_t cmt_dac_flag;
4341 * we take any chance we can to service our queues since we cannot
4342 * get awoken when the socket is read from :<
4345 * Now perform the actual SACK handling: 1) Verify that it is not an
4346 * old sack, if so discard. 2) If there is nothing left in the send
4347 * queue (cum-ack is equal to last acked) then you have a duplicate
4348 * too, update any rwnd change and verify no timers are running.
4349 * then return. 3) Process any new consequtive data i.e. cum-ack
4350 * moved process these first and note that it moved. 4) Process any
4351 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4352 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4353 * sync up flightsizes and things, stop all timers and also check
4354 * for shutdown_pending state. If so then go ahead and send off the
4355 * shutdown. If in shutdown recv, send off the shutdown-ack and
4356 * start that timer, Ret. 9) Strike any non-acked things and do FR
4357 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4358 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4359 * if in shutdown_recv state.
4361 SCTP_TCB_LOCK_ASSERT(stcb);
4364 this_sack_lowest_newack = 0;
4366 sack_length = (unsigned int)sack_len;
4368 SCTP_STAT_INCR(sctps_slowpath_sack);
4369 nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4370 cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4371 #ifdef SCTP_ASOCLOG_OF_TSNS
4372 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4373 stcb->asoc.cumack_log_at++;
4374 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4375 stcb->asoc.cumack_log_at = 0;
4378 num_seg = ntohs(sack->num_gap_ack_blks);
4381 if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4382 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4383 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4386 cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4387 num_dup = ntohs(sack->num_dup_tsns);
4389 old_rwnd = stcb->asoc.peers_rwnd;
4390 if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
4391 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4392 stcb->asoc.overall_error_count,
4394 SCTP_FROM_SCTP_INDATA,
4397 stcb->asoc.overall_error_count = 0;
4399 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4400 sctp_log_sack(asoc->last_acked_seq,
4407 if ((num_dup) && (sctp_logging_level & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4408 int off_to_dup, iii;
4409 uint32_t *dupdata, dblock;
4411 off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4412 if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4413 dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4414 sizeof(uint32_t), (uint8_t *) & dblock);
4415 off_to_dup += sizeof(uint32_t);
4417 for (iii = 0; iii < num_dup; iii++) {
4418 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4419 dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4420 sizeof(uint32_t), (uint8_t *) & dblock);
4421 if (dupdata == NULL)
4423 off_to_dup += sizeof(uint32_t);
4427 SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4428 off_to_dup, num_dup, sack_length, num_seg);
4431 if (sctp_strict_sacks) {
4433 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4434 tp1 = TAILQ_LAST(&asoc->sent_queue,
4435 sctpchunk_listhead);
4436 send_s = tp1->rec.data.TSN_seq + 1;
4438 send_s = asoc->sending_seq;
4440 if (cum_ack == send_s ||
4441 compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4448 panic("Impossible sack 1");
4453 * no way, we have not even sent this TSN out yet.
4454 * Peer is hopelessly messed up with us.
4459 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4460 0, M_DONTWAIT, 1, MT_DATA);
4462 struct sctp_paramhdr *ph;
4465 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4467 ph = mtod(oper, struct sctp_paramhdr *);
4468 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4469 ph->param_length = htons(SCTP_BUF_LEN(oper));
4470 ippp = (uint32_t *) (ph + 1);
4471 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4473 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4474 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4479 /**********************/
4480 /* 1) check the range */
4481 /**********************/
4482 if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4483 /* acking something behind */
4486 sav_cum_ack = asoc->last_acked_seq;
4488 /* update the Rwnd of the peer */
4489 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4490 TAILQ_EMPTY(&asoc->send_queue) &&
4491 (asoc->stream_queue_cnt == 0)
4493 /* nothing left on send/sent and strmq */
4494 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4495 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4496 asoc->peers_rwnd, 0, 0, a_rwnd);
4498 asoc->peers_rwnd = a_rwnd;
4499 if (asoc->sent_queue_retran_cnt) {
4500 asoc->sent_queue_retran_cnt = 0;
4502 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4503 /* SWS sender side engages */
4504 asoc->peers_rwnd = 0;
4506 /* stop any timers */
4507 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4508 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4509 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4510 if (sctp_early_fr) {
4511 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4512 SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4513 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4514 SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4517 net->partial_bytes_acked = 0;
4518 net->flight_size = 0;
4520 asoc->total_flight = 0;
4521 asoc->total_flight_count = 0;
4525 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4526 * things. The total byte count acked is tracked in netAckSz AND
4527 * netAck2 is used to track the total bytes acked that are un-
4528 * amibguious and were never retransmitted. We track these on a per
4529 * destination address basis.
4531 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4532 net->prev_cwnd = net->cwnd;
4537 * CMT: Reset CUC and Fast recovery algo variables before
4540 net->new_pseudo_cumack = 0;
4541 net->will_exit_fast_recovery = 0;
4543 /* process the new consecutive TSN first */
4544 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4546 if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4548 last_tsn == tp1->rec.data.TSN_seq) {
4549 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4551 * ECN Nonce: Add the nonce to the sender's
4554 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4556 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4558 * If it is less than ACKED, it is
4559 * now no-longer in flight. Higher
4560 * values may occur during marking
4562 if ((tp1->whoTo->dest_state &
4563 SCTP_ADDR_UNCONFIRMED) &&
4564 (tp1->snd_count < 2)) {
4566 * If there was no retran
4567 * and the address is
4568 * un-confirmed and we sent
4570 * sacked.. its confirmed,
4573 tp1->whoTo->dest_state &=
4574 ~SCTP_ADDR_UNCONFIRMED;
4576 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4577 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4578 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4579 tp1->whoTo->flight_size,
4581 (uintptr_t) tp1->whoTo,
4582 tp1->rec.data.TSN_seq);
4584 sctp_flight_size_decrease(tp1);
4585 sctp_total_flight_decrease(stcb, tp1);
4587 tp1->whoTo->net_ack += tp1->send_size;
4589 /* CMT SFR and DAC algos */
4590 this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4591 tp1->whoTo->saw_newack = 1;
4593 if (tp1->snd_count < 2) {
4595 * True non-retransmited
4598 tp1->whoTo->net_ack2 +=
4601 /* update RTO too? */
4604 sctp_calculate_rto(stcb,
4606 &tp1->sent_rcv_time,
4607 sctp_align_safe_nocopy);
4612 * CMT: CUCv2 algorithm. From the
4613 * cumack'd TSNs, for each TSN being
4614 * acked for the first time, set the
4615 * following variables for the
4616 * corresp destination.
4617 * new_pseudo_cumack will trigger a
4619 * find_(rtx_)pseudo_cumack will
4620 * trigger search for the next
4621 * expected (rtx-)pseudo-cumack.
4623 tp1->whoTo->new_pseudo_cumack = 1;
4624 tp1->whoTo->find_pseudo_cumack = 1;
4625 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4628 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4629 sctp_log_sack(asoc->last_acked_seq,
4631 tp1->rec.data.TSN_seq,
4634 SCTP_LOG_TSN_ACKED);
4636 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4637 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4640 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4641 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4642 #ifdef SCTP_AUDITING_ENABLED
4643 sctp_audit_log(0xB3,
4644 (asoc->sent_queue_retran_cnt & 0x000000ff));
4647 if (tp1->rec.data.chunk_was_revoked) {
4648 /* deflate the cwnd */
4649 tp1->whoTo->cwnd -= tp1->book_size;
4650 tp1->rec.data.chunk_was_revoked = 0;
4652 tp1->sent = SCTP_DATAGRAM_ACKED;
4657 tp1 = TAILQ_NEXT(tp1, sctp_next);
4659 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4660 /* always set this up to cum-ack */
4661 asoc->this_sack_highest_gap = last_tsn;
4663 /* Move offset up to point to gaps/dups */
4664 offset += sizeof(struct sctp_sack_chunk);
4665 if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
4667 /* skip corrupt segments */
4673 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4674 * to be greater than the cumack. Also reset saw_newack to 0
4677 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4678 net->saw_newack = 0;
4679 net->this_sack_highest_newack = last_tsn;
4683 * thisSackHighestGap will increase while handling NEW
4684 * segments this_sack_highest_newack will increase while
4685 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4686 * used for CMT DAC algo. saw_newack will also change.
4688 sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
4689 &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4690 num_seg, &ecn_seg_sums);
4692 if (sctp_strict_sacks) {
4694 * validate the biggest_tsn_acked in the gap acks if
4695 * strict adherence is wanted.
4697 if ((biggest_tsn_acked == send_s) ||
4698 (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4700 * peer is either confused or we are under
4701 * attack. We must abort.
4708 /*******************************************/
4709 /* cancel ALL T3-send timer if accum moved */
4710 /*******************************************/
4711 if (sctp_cmt_on_off) {
4712 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4713 if (net->new_pseudo_cumack)
4714 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4716 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4721 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4722 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4723 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4727 /********************************************/
4728 /* drop the acked chunks from the sendqueue */
4729 /********************************************/
4730 asoc->last_acked_seq = cum_ack;
4732 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4736 if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
4740 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4741 /* no more sent on list */
4742 printf("Warning, tp1->sent == %d and its now acked?\n",
4745 tp2 = TAILQ_NEXT(tp1, sctp_next);
4746 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4747 if (tp1->pr_sctp_on) {
4748 if (asoc->pr_sctp_cnt != 0)
4749 asoc->pr_sctp_cnt--;
4751 if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
4752 (asoc->total_flight > 0)) {
4754 panic("Warning flight size is postive and should be 0");
4756 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4757 asoc->total_flight);
4759 asoc->total_flight = 0;
4762 /* sa_ignore NO_NULL_CHK */
4763 sctp_free_bufspace(stcb, asoc, tp1, 1);
4764 sctp_m_freem(tp1->data);
4765 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4766 asoc->sent_queue_cnt_removeable--;
4769 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4770 sctp_log_sack(asoc->last_acked_seq,
4772 tp1->rec.data.TSN_seq,
4775 SCTP_LOG_FREE_SENT);
4778 asoc->sent_queue_cnt--;
4779 sctp_free_a_chunk(stcb, tp1);
4782 } while (tp1 != NULL);
4785 /* sa_ignore NO_NULL_CHK */
4786 if ((wake_him) && (stcb->sctp_socket)) {
4787 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4791 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4792 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4793 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
4795 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4796 so = SCTP_INP_SO(stcb->sctp_ep);
4797 atomic_add_int(&stcb->asoc.refcnt, 1);
4798 SCTP_TCB_UNLOCK(stcb);
4799 SCTP_SOCKET_LOCK(so, 1);
4800 SCTP_TCB_LOCK(stcb);
4801 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4802 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4803 /* assoc was freed while we were unlocked */
4804 SCTP_SOCKET_UNLOCK(so, 1);
4808 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4809 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4810 SCTP_SOCKET_UNLOCK(so, 1);
4813 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4814 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
4818 if (asoc->fast_retran_loss_recovery && accum_moved) {
4819 if (compare_with_wrap(asoc->last_acked_seq,
4820 asoc->fast_recovery_tsn, MAX_TSN) ||
4821 asoc->last_acked_seq == asoc->fast_recovery_tsn) {
4822 /* Setup so we will exit RFC2582 fast recovery */
4823 will_exit_fast_recovery = 1;
4827 * Check for revoked fragments:
4829 * if Previous sack - Had no frags then we can't have any revoked if
4830 * Previous sack - Had frag's then - If we now have frags aka
4831 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4832 * some of them. else - The peer revoked all ACKED fragments, since
4833 * we had some before and now we have NONE.
4837 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4838 else if (asoc->saw_sack_with_frags) {
4839 int cnt_revoked = 0;
4841 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4843 /* Peer revoked all dg's marked or acked */
4844 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4845 if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
4846 (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
4847 tp1->sent = SCTP_DATAGRAM_SENT;
4848 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4849 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4850 tp1->whoTo->flight_size,
4852 (uintptr_t) tp1->whoTo,
4853 tp1->rec.data.TSN_seq);
4855 sctp_flight_size_increase(tp1);
4856 sctp_total_flight_increase(stcb, tp1);
4857 tp1->rec.data.chunk_was_revoked = 1;
4859 * To ensure that this increase in
4860 * flightsize, which is artificial,
4861 * does not throttle the sender, we
4862 * also increase the cwnd
4865 tp1->whoTo->cwnd += tp1->book_size;
4873 asoc->saw_sack_with_frags = 0;
4876 asoc->saw_sack_with_frags = 1;
4878 asoc->saw_sack_with_frags = 0;
4880 /* JRS - Use the congestion control given in the CC module */
4881 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4883 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4884 /* nothing left in-flight */
4885 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4886 /* stop all timers */
4887 if (sctp_early_fr) {
4888 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4889 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4890 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4891 SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
4894 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4895 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4896 net->flight_size = 0;
4897 net->partial_bytes_acked = 0;
4899 asoc->total_flight = 0;
4900 asoc->total_flight_count = 0;
4902 /**********************************/
4903 /* Now what about shutdown issues */
4904 /**********************************/
4905 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4906 /* nothing left on sendqueue.. consider done */
4907 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4908 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4909 asoc->peers_rwnd, 0, 0, a_rwnd);
4911 asoc->peers_rwnd = a_rwnd;
4912 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4913 /* SWS sender side engages */
4914 asoc->peers_rwnd = 0;
4917 if ((asoc->stream_queue_cnt == 1) &&
4918 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4919 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4920 (asoc->locked_on_sending)
4922 struct sctp_stream_queue_pending *sp;
4925 * I may be in a state where we got all across.. but
4926 * cannot write more due to a shutdown... we abort
4927 * since the user did not indicate EOR in this case.
4929 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4931 if ((sp) && (sp->length == 0)) {
4932 asoc->locked_on_sending = NULL;
4933 if (sp->msg_is_complete) {
4934 asoc->stream_queue_cnt--;
4936 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4937 asoc->stream_queue_cnt--;
4941 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4942 (asoc->stream_queue_cnt == 0)) {
4943 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4944 /* Need to abort here */
4950 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4951 0, M_DONTWAIT, 1, MT_DATA);
4953 struct sctp_paramhdr *ph;
4956 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4958 ph = mtod(oper, struct sctp_paramhdr *);
4959 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4960 ph->param_length = htons(SCTP_BUF_LEN(oper));
4961 ippp = (uint32_t *) (ph + 1);
4962 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4964 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4965 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4968 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4969 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4970 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4972 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4973 sctp_stop_timers_for_shutdown(stcb);
4974 sctp_send_shutdown(stcb,
4975 stcb->asoc.primary_destination);
4976 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4977 stcb->sctp_ep, stcb, asoc->primary_destination);
4978 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4979 stcb->sctp_ep, stcb, asoc->primary_destination);
4982 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4983 (asoc->stream_queue_cnt == 0)) {
4984 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4987 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4988 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4989 sctp_send_shutdown_ack(stcb,
4990 stcb->asoc.primary_destination);
4992 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4993 stcb->sctp_ep, stcb, asoc->primary_destination);
4998 * Now here we are going to recycle net_ack for a different use...
5001 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5006 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5007 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5008 * automatically ensure that.
5010 if (sctp_cmt_on_off && sctp_cmt_use_dac && (cmt_dac_flag == 0)) {
5011 this_sack_lowest_newack = cum_ack;
5014 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5015 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5017 /*********************************************/
5018 /* Here we perform PR-SCTP procedures */
5020 /*********************************************/
5021 /* C1. update advancedPeerAckPoint */
5022 if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5023 asoc->advanced_peer_ack_point = cum_ack;
5025 /* C2. try to further move advancedPeerAckPoint ahead */
5026 if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5027 struct sctp_tmit_chunk *lchk;
5029 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5030 /* C3. See if we need to send a Fwd-TSN */
5031 if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5034 * ISSUE with ECN, see FWD-TSN processing for notes
5035 * on issues that will occur when the ECN NONCE
5036 * stuff is put into SCTP for cross checking.
5038 send_forward_tsn(stcb, asoc);
5041 * ECN Nonce: Disable Nonce Sum check when FWD TSN
5042 * is sent and store resync tsn
5044 asoc->nonce_sum_check = 0;
5045 asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5047 /* Assure a timer is up */
5048 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5049 stcb->sctp_ep, stcb, lchk->whoTo);
5053 /* JRS - Use the congestion control given in the CC module */
5054 asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5056 /******************************************************************
5057 * Here we do the stuff with ECN Nonce checking.
5058 * We basically check to see if the nonce sum flag was incorrect
5059 * or if resynchronization needs to be done. Also if we catch a
5060 * misbehaving receiver we give him the kick.
5061 ******************************************************************/
5063 if (asoc->ecn_nonce_allowed) {
5064 if (asoc->nonce_sum_check) {
5065 if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5066 if (asoc->nonce_wait_for_ecne == 0) {
5067 struct sctp_tmit_chunk *lchk;
5069 lchk = TAILQ_FIRST(&asoc->send_queue);
5070 asoc->nonce_wait_for_ecne = 1;
5072 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5074 asoc->nonce_wait_tsn = asoc->sending_seq;
5077 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5078 (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5080 * Misbehaving peer. We need
5081 * to react to this guy
5083 asoc->ecn_allowed = 0;
5084 asoc->ecn_nonce_allowed = 0;
5089 /* See if Resynchronization Possible */
5090 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5091 asoc->nonce_sum_check = 1;
5093 * now we must calculate what the base is.
5094 * We do this based on two things, we know
5095 * the total's for all the segments
5096 * gap-acked in the SACK, its stored in
5097 * ecn_seg_sums. We also know the SACK's
5098 * nonce sum, its in nonce_sum_flag. So we
5099 * can build a truth table to back-calculate
5101 * asoc->nonce_sum_expect_base:
5103 * SACK-flag-Value Seg-Sums Base 0 0 0
5107 asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5111 /* Now are we exiting loss recovery ? */
5112 if (will_exit_fast_recovery) {
5113 /* Ok, we must exit fast recovery */
5114 asoc->fast_retran_loss_recovery = 0;
5116 if ((asoc->sat_t3_loss_recovery) &&
5117 ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5119 (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5120 /* end satellite t3 loss recovery */
5121 asoc->sat_t3_loss_recovery = 0;
5126 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5127 if (net->will_exit_fast_recovery) {
5128 /* Ok, we must exit fast recovery */
5129 net->fast_retran_loss_recovery = 0;
5133 /* Adjust and set the new rwnd value */
5134 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5135 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5136 asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
5138 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5139 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
5140 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5141 /* SWS sender side engages */
5142 asoc->peers_rwnd = 0;
5144 if (asoc->peers_rwnd > old_rwnd) {
5145 win_probe_recovery = 1;
5148 * Now we must setup so we have a timer up for anyone with
5154 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5155 if (win_probe_recovery && (net->window_probe)) {
5156 net->window_probe = 0;
5157 win_probe_recovered = 1;
5159 * Find first chunk that was used with
5160 * window probe and clear the event. Put
5161 * it back into the send queue as if has
5164 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5165 if (tp1->window_probe) {
5166 sctp_window_probe_recovery(stcb, asoc, net, tp1);
5171 if (net->flight_size) {
5173 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5174 stcb->sctp_ep, stcb, net);
5176 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5177 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5179 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5181 if (sctp_early_fr) {
5182 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5183 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5184 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5185 SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5191 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5192 (asoc->sent_queue_retran_cnt == 0) &&
5193 (win_probe_recovered == 0) &&
5195 /* huh, this should not happen */
5196 sctp_fs_audit(asoc);
5197 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5198 net->flight_size = 0;
5200 asoc->total_flight = 0;
5201 asoc->total_flight_count = 0;
5202 asoc->sent_queue_retran_cnt = 0;
5203 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5204 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5205 sctp_flight_size_increase(tp1);
5206 sctp_total_flight_increase(stcb, tp1);
5207 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5208 asoc->sent_queue_retran_cnt++;
5214 if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
5215 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5217 stcb->asoc.peers_rwnd,
5218 stcb->asoc.total_flight,
5219 stcb->asoc.total_output_queue_size);
5224 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5225 struct sctp_nets *netp, int *abort_flag)
5228 uint32_t cum_ack, a_rwnd;
5230 cum_ack = ntohl(cp->cumulative_tsn_ack);
5231 /* Arrange so a_rwnd does NOT change */
5232 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5234 /* Now call the express sack handling */
5235 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5239 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5240 struct sctp_stream_in *strmin)
5242 struct sctp_queued_to_read *ctl, *nctl;
5243 struct sctp_association *asoc;
5247 tt = strmin->last_sequence_delivered;
5249 * First deliver anything prior to and including the stream no that
5252 ctl = TAILQ_FIRST(&strmin->inqueue);
5254 nctl = TAILQ_NEXT(ctl, next);
5255 if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5256 (tt == ctl->sinfo_ssn)) {
5257 /* this is deliverable now */
5258 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5259 /* subtract pending on streams */
5260 asoc->size_on_all_streams -= ctl->length;
5261 sctp_ucount_decr(asoc->cnt_on_all_streams);
5262 /* deliver it to at least the delivery-q */
5263 if (stcb->sctp_socket) {
5264 sctp_add_to_readq(stcb->sctp_ep, stcb,
5266 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5269 /* no more delivery now. */
5275 * now we must deliver things in queue the normal way if any are
5278 tt = strmin->last_sequence_delivered + 1;
5279 ctl = TAILQ_FIRST(&strmin->inqueue);
5281 nctl = TAILQ_NEXT(ctl, next);
5282 if (tt == ctl->sinfo_ssn) {
5283 /* this is deliverable now */
5284 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5285 /* subtract pending on streams */
5286 asoc->size_on_all_streams -= ctl->length;
5287 sctp_ucount_decr(asoc->cnt_on_all_streams);
5288 /* deliver it to at least the delivery-q */
5289 strmin->last_sequence_delivered = ctl->sinfo_ssn;
5290 if (stcb->sctp_socket) {
5291 sctp_add_to_readq(stcb->sctp_ep, stcb,
5293 &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5295 tt = strmin->last_sequence_delivered + 1;
5304 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5305 struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
5308 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5309 * forward TSN, when the SACK comes back that acknowledges the
5310 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5311 * get quite tricky since we may have sent more data interveneing
5312 * and must carefully account for what the SACK says on the nonce
5313 * and any gaps that are reported. This work will NOT be done here,
5314 * but I note it here since it is really related to PR-SCTP and
5318 /* The pr-sctp fwd tsn */
5320 * here we will perform all the data receiver side steps for
5321 * processing FwdTSN, as required in by pr-sctp draft:
5323 * Assume we get FwdTSN(x):
5325 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5326 * others we have 3) examine and update re-ordering queue on
5327 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5328 * report where we are.
5330 struct sctp_association *asoc;
5331 uint32_t new_cum_tsn, gap;
5332 unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
5333 struct sctp_stream_in *strm;
5334 struct sctp_tmit_chunk *chk, *at;
5336 cumack_set_flag = 0;
5339 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5340 SCTPDBG(SCTP_DEBUG_INDATA1,
5341 "Bad size too small/big fwd-tsn\n");
5344 m_size = (stcb->asoc.mapping_array_size << 3);
5345 /*************************************************************/
5346 /* 1. Here we update local cumTSN and shift the bitmap array */
5347 /*************************************************************/
5348 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5350 if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5351 asoc->cumulative_tsn == new_cum_tsn) {
5352 /* Already got there ... */
5355 if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5357 asoc->highest_tsn_inside_map = new_cum_tsn;
5358 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5359 sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5363 * now we know the new TSN is more advanced, let's find the actual
5366 if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
5368 (new_cum_tsn == asoc->mapping_array_base_tsn)) {
5369 gap = new_cum_tsn - asoc->mapping_array_base_tsn;
5371 /* try to prevent underflow here */
5372 gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5375 if (gap >= m_size) {
5376 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5377 sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5379 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5383 * out of range (of single byte chunks in the rwnd I
5384 * give out). This must be an attacker.
5387 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5388 0, M_DONTWAIT, 1, MT_DATA);
5390 struct sctp_paramhdr *ph;
5393 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5394 (sizeof(uint32_t) * 3);
5395 ph = mtod(oper, struct sctp_paramhdr *);
5396 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5397 ph->param_length = htons(SCTP_BUF_LEN(oper));
5398 ippp = (uint32_t *) (ph + 1);
5399 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5401 *ippp = asoc->highest_tsn_inside_map;
5403 *ippp = new_cum_tsn;
5405 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5406 sctp_abort_an_association(stcb->sctp_ep, stcb,
5407 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5410 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5412 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5413 cumack_set_flag = 1;
5414 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5415 asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
5417 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5418 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5420 asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
5422 SCTP_TCB_LOCK_ASSERT(stcb);
5423 if ((compare_with_wrap(((uint32_t) asoc->cumulative_tsn + gap), asoc->highest_tsn_inside_map, MAX_TSN)) ||
5424 (((uint32_t) asoc->cumulative_tsn + gap) == asoc->highest_tsn_inside_map)) {
5427 for (i = 0; i <= gap; i++) {
5428 SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
5432 * Now after marking all, slide thing forward but no sack
5435 sctp_sack_check(stcb, 0, 0, abort_flag);
5440 /*************************************************************/
5441 /* 2. Clear up re-assembly queue */
5442 /*************************************************************/
5444 * First service it if pd-api is up, just in case we can progress it
5447 if (asoc->fragmented_delivery_inprogress) {
5448 sctp_service_reassembly(stcb, asoc);
5450 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5451 /* For each one on here see if we need to toss it */
5453 * For now large messages held on the reasmqueue that are
5454 * complete will be tossed too. We could in theory do more
5455 * work to spin through and stop after dumping one msg aka
5456 * seeing the start of a new msg at the head, and call the
5457 * delivery function... to see if it can be delivered... But
5458 * for now we just dump everything on the queue.
5460 chk = TAILQ_FIRST(&asoc->reasmqueue);
5462 at = TAILQ_NEXT(chk, sctp_next);
5463 if (compare_with_wrap(asoc->cumulative_tsn,
5464 chk->rec.data.TSN_seq, MAX_TSN) ||
5465 asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
5466 /* It needs to be tossed */
5467 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5468 if (compare_with_wrap(chk->rec.data.TSN_seq,
5469 asoc->tsn_last_delivered, MAX_TSN)) {
5470 asoc->tsn_last_delivered =
5471 chk->rec.data.TSN_seq;
5472 asoc->str_of_pdapi =
5473 chk->rec.data.stream_number;
5474 asoc->ssn_of_pdapi =
5475 chk->rec.data.stream_seq;
5476 asoc->fragment_flags =
5477 chk->rec.data.rcv_flags;
5479 asoc->size_on_reasm_queue -= chk->send_size;
5480 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5483 /* Clear up any stream problem */
5484 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5485 SCTP_DATA_UNORDERED &&
5486 (compare_with_wrap(chk->rec.data.stream_seq,
5487 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5490 * We must dump forward this streams
5491 * sequence number if the chunk is
5492 * not unordered that is being
5493 * skipped. There is a chance that
5494 * if the peer does not include the
5495 * last fragment in its FWD-TSN we
5496 * WILL have a problem here since
5497 * you would have a partial chunk in
5498 * queue that may not be
5499 * deliverable. Also if a Partial
5500 * delivery API as started the user
5501 * may get a partial chunk. The next
5502 * read returning a new chunk...
5503 * really ugly but I see no way
5504 * around it! Maybe a notify??
5506 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5507 chk->rec.data.stream_seq;
5510 sctp_m_freem(chk->data);
5513 sctp_free_a_chunk(stcb, chk);
5516 * Ok we have gone beyond the end of the
5517 * fwd-tsn's mark. Some checks...
5519 if ((asoc->fragmented_delivery_inprogress) &&
5520 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5524 * Special case PD-API is up and
5525 * what we fwd-tsn' over includes
5526 * one that had the LAST_FRAG. We no
5527 * longer need to do the PD-API.
5529 asoc->fragmented_delivery_inprogress = 0;
5531 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5532 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5533 stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq, SCTP_SO_NOT_LOCKED);
5541 if (asoc->fragmented_delivery_inprogress) {
5543 * Ok we removed cnt_gone chunks in the PD-API queue that
5544 * were being delivered. So now we must turn off the flag.
5548 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5549 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5550 stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq, SCTP_SO_NOT_LOCKED);
5551 asoc->fragmented_delivery_inprogress = 0;
5553 /*************************************************************/
5554 /* 3. Update the PR-stream re-ordering queues */
5555 /*************************************************************/
5556 fwd_sz -= sizeof(*fwd);
5559 unsigned int num_str;
5560 struct sctp_strseq *stseq, strseqbuf;
5562 offset += sizeof(*fwd);
5564 num_str = fwd_sz / sizeof(struct sctp_strseq);
5565 for (i = 0; i < num_str; i++) {
5568 stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5569 sizeof(struct sctp_strseq),
5570 (uint8_t *) & strseqbuf);
5571 offset += sizeof(struct sctp_strseq);
5572 if (stseq == NULL) {
5576 st = ntohs(stseq->stream);
5578 st = ntohs(stseq->sequence);
5579 stseq->sequence = st;
5581 if (stseq->stream >= asoc->streamincnt) {
5582 /* screwed up streams, stop! */
5585 strm = &asoc->strmin[stseq->stream];
5586 if (compare_with_wrap(stseq->sequence,
5587 strm->last_sequence_delivered, MAX_SEQ)) {
5588 /* Update the sequence number */
5589 strm->last_sequence_delivered =
5592 /* now kick the stream the new way */
5593 /* sa_ignore NO_NULL_CHK */
5594 sctp_kick_prsctp_reorder_queue(stcb, strm);
5597 if (TAILQ_FIRST(&asoc->reasmqueue)) {
5598 /* now lets kick out and check for more fragmented delivery */
5599 /* sa_ignore NO_NULL_CHK */
5600 sctp_deliver_reasm_check(stcb, &stcb->asoc);