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_w_oh;
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 */
97 calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
101 * If our overhead is greater than the advertised rwnd, we
102 * clamp the rwnd to 1. This lets us still accept inbound
103 * segments, but hopefully will shut the sender down when he
104 * finally gets the message.
110 (asoc->my_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
111 /* SWS engaged, tell peer none left */
117 /* Calculate what the rwnd would be */
120 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
122 uint32_t calc = 0, calc_w_oh;
125 * This is really set wrong with respect to a 1-2-m socket. Since
126 * the sb_cc is the count that everyone as put up. When we re-write
127 * sctp_soreceive then we will fix this so that ONLY this
128 * associations data is taken into account.
130 if (stcb->sctp_socket == NULL)
133 if (stcb->asoc.sb_cc == 0 &&
134 asoc->size_on_reasm_queue == 0 &&
135 asoc->size_on_all_streams == 0) {
136 /* Full rwnd granted */
137 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
141 /* get actual space */
142 calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
145 * take out what has NOT been put on socket queue and we yet hold
148 calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
149 calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
155 /* what is the overhead of all these rwnd's */
156 calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
157 if (calc_w_oh == 0) {
159 * If our overhead is greater than the advertised rwnd, we
160 * clamp the rwnd to 1. This lets us still accept inbound
161 * segments, but hopefully will shut the sender down when he
162 * finally gets the message.
168 (calc < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
169 /* SWS engaged, tell peer none left */
179 * Build out our readq entry based on the incoming packet.
181 struct sctp_queued_to_read *
182 sctp_build_readq_entry(struct sctp_tcb *stcb,
183 struct sctp_nets *net,
184 uint32_t tsn, uint32_t ppid,
185 uint32_t context, uint16_t stream_no,
186 uint16_t stream_seq, uint8_t flags,
189 struct sctp_queued_to_read *read_queue_e = NULL;
191 sctp_alloc_a_readq(stcb, read_queue_e);
192 if (read_queue_e == NULL) {
195 read_queue_e->sinfo_stream = stream_no;
196 read_queue_e->sinfo_ssn = stream_seq;
197 read_queue_e->sinfo_flags = (flags << 8);
198 read_queue_e->sinfo_ppid = ppid;
199 read_queue_e->sinfo_context = stcb->asoc.context;
200 read_queue_e->sinfo_timetolive = 0;
201 read_queue_e->sinfo_tsn = tsn;
202 read_queue_e->sinfo_cumtsn = tsn;
203 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
204 read_queue_e->whoFrom = net;
205 read_queue_e->length = 0;
206 atomic_add_int(&net->ref_count, 1);
207 read_queue_e->data = dm;
208 read_queue_e->spec_flags = 0;
209 read_queue_e->tail_mbuf = NULL;
210 read_queue_e->aux_data = NULL;
211 read_queue_e->stcb = stcb;
212 read_queue_e->port_from = stcb->rport;
213 read_queue_e->do_not_ref_stcb = 0;
214 read_queue_e->end_added = 0;
215 read_queue_e->some_taken = 0;
216 read_queue_e->pdapi_aborted = 0;
218 return (read_queue_e);
223 * Build out our readq entry based on the incoming packet.
225 static struct sctp_queued_to_read *
226 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
227 struct sctp_tmit_chunk *chk)
229 struct sctp_queued_to_read *read_queue_e = NULL;
231 sctp_alloc_a_readq(stcb, read_queue_e);
232 if (read_queue_e == NULL) {
235 read_queue_e->sinfo_stream = chk->rec.data.stream_number;
236 read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
237 read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
238 read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
239 read_queue_e->sinfo_context = stcb->asoc.context;
240 read_queue_e->sinfo_timetolive = 0;
241 read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
242 read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
243 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
244 read_queue_e->whoFrom = chk->whoTo;
245 read_queue_e->aux_data = NULL;
246 read_queue_e->length = 0;
247 atomic_add_int(&chk->whoTo->ref_count, 1);
248 read_queue_e->data = chk->data;
249 read_queue_e->tail_mbuf = NULL;
250 read_queue_e->stcb = stcb;
251 read_queue_e->port_from = stcb->rport;
252 read_queue_e->spec_flags = 0;
253 read_queue_e->do_not_ref_stcb = 0;
254 read_queue_e->end_added = 0;
255 read_queue_e->some_taken = 0;
256 read_queue_e->pdapi_aborted = 0;
258 return (read_queue_e);
263 sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
264 struct sctp_sndrcvinfo *sinfo)
266 struct sctp_sndrcvinfo *outinfo;
270 int use_extended = 0;
272 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
273 /* user does not want the sndrcv ctl */
276 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
278 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
280 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
284 ret = sctp_get_mbuf_for_msg(len,
285 0, M_DONTWAIT, 1, MT_DATA);
291 /* We need a CMSG header followed by the struct */
292 cmh = mtod(ret, struct cmsghdr *);
293 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
294 cmh->cmsg_level = IPPROTO_SCTP;
296 cmh->cmsg_type = SCTP_EXTRCV;
298 memcpy(outinfo, sinfo, len);
300 cmh->cmsg_type = SCTP_SNDRCV;
304 SCTP_BUF_LEN(ret) = cmh->cmsg_len;
310 sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
312 struct sctp_sndrcvinfo *sinfo)
314 struct sctp_sndrcvinfo *outinfo;
318 int use_extended = 0;
320 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
321 /* user does not want the sndrcv ctl */
324 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
326 len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
328 len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
330 SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
335 /* We need a CMSG header followed by the struct */
336 cmh = (struct cmsghdr *)buf;
337 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
338 cmh->cmsg_level = IPPROTO_SCTP;
340 cmh->cmsg_type = SCTP_EXTRCV;
342 memcpy(outinfo, sinfo, len);
344 cmh->cmsg_type = SCTP_SNDRCV;
354 * We are delivering currently from the reassembly queue. We must continue to
355 * deliver until we either: 1) run out of space. 2) run out of sequential
356 * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
359 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
361 struct sctp_tmit_chunk *chk;
366 struct sctp_queued_to_read *control, *ctl, *ctlat;
371 cntDel = stream_no = 0;
372 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
373 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
374 /* socket above is long gone */
375 asoc->fragmented_delivery_inprogress = 0;
376 chk = TAILQ_FIRST(&asoc->reasmqueue);
378 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
379 asoc->size_on_reasm_queue -= chk->send_size;
380 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
382 * Lose the data pointer, since its in the socket
386 sctp_m_freem(chk->data);
389 /* Now free the address and data */
390 sctp_free_remote_addr(chk->whoTo);
391 sctp_free_a_chunk(stcb, chk);
392 /* sa_ignore FREED_MEMORY */
393 chk = TAILQ_FIRST(&asoc->reasmqueue);
397 SCTP_TCB_LOCK_ASSERT(stcb);
399 chk = TAILQ_FIRST(&asoc->reasmqueue);
403 if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
404 /* Can't deliver more :< */
407 stream_no = chk->rec.data.stream_number;
408 nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
409 if (nxt_todel != chk->rec.data.stream_seq &&
410 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
412 * Not the next sequence to deliver in its stream OR
417 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
419 control = sctp_build_readq_entry_chk(stcb, chk);
420 if (control == NULL) {
424 /* save it off for our future deliveries */
425 stcb->asoc.control_pdapi = control;
426 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
430 sctp_add_to_readq(stcb->sctp_ep,
431 stcb, control, &stcb->sctp_socket->so_rcv, end);
434 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
438 if (sctp_append_to_readq(stcb->sctp_ep, stcb,
439 stcb->asoc.control_pdapi,
440 chk->data, end, chk->rec.data.TSN_seq,
441 &stcb->sctp_socket->so_rcv)) {
443 * something is very wrong, either
444 * control_pdapi is NULL, or the tail_mbuf
445 * is corrupt, or there is a EOM already on
448 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
449 panic("This should not happen control_pdapi NULL?");
451 /* if we did not panic, it was a EOM */
452 panic("Bad chunking ??");
457 /* pull it we did it */
458 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
459 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
460 asoc->fragmented_delivery_inprogress = 0;
461 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
462 asoc->strmin[stream_no].last_sequence_delivered++;
464 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
465 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
467 } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
469 * turn the flag back on since we just delivered
472 asoc->fragmented_delivery_inprogress = 1;
474 asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
475 asoc->last_flags_delivered = chk->rec.data.rcv_flags;
476 asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
477 asoc->last_strm_no_delivered = chk->rec.data.stream_number;
479 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
480 asoc->size_on_reasm_queue -= chk->send_size;
481 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
482 /* free up the chk */
484 sctp_free_remote_addr(chk->whoTo);
485 sctp_free_a_chunk(stcb, chk);
487 if (asoc->fragmented_delivery_inprogress == 0) {
489 * Now lets see if we can deliver the next one on
492 struct sctp_stream_in *strm;
494 strm = &asoc->strmin[stream_no];
495 nxt_todel = strm->last_sequence_delivered + 1;
496 ctl = TAILQ_FIRST(&strm->inqueue);
497 if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
498 while (ctl != NULL) {
499 /* Deliver more if we can. */
500 if (nxt_todel == ctl->sinfo_ssn) {
501 ctlat = TAILQ_NEXT(ctl, next);
502 TAILQ_REMOVE(&strm->inqueue, ctl, next);
503 asoc->size_on_all_streams -= ctl->length;
504 sctp_ucount_decr(asoc->cnt_on_all_streams);
505 strm->last_sequence_delivered++;
506 sctp_add_to_readq(stcb->sctp_ep, stcb,
508 &stcb->sctp_socket->so_rcv, 1);
513 nxt_todel = strm->last_sequence_delivered + 1;
518 /* sa_ignore FREED_MEMORY */
519 chk = TAILQ_FIRST(&asoc->reasmqueue);
524 * Queue the chunk either right into the socket buffer if it is the next one
525 * to go OR put it in the correct place in the delivery queue. If we do
526 * append to the so_buf, keep doing so until we are out of order. One big
527 * question still remains, what to do when the socket buffer is FULL??
530 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
531 struct sctp_queued_to_read *control, int *abort_flag)
534 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
535 * all the data in one stream this could happen quite rapidly. One
536 * could use the TSN to keep track of things, but this scheme breaks
537 * down in the other type of stream useage that could occur. Send a
538 * single msg to stream 0, send 4Billion messages to stream 1, now
539 * send a message to stream 0. You have a situation where the TSN
540 * has wrapped but not in the stream. Is this worth worrying about
541 * or should we just change our queue sort at the bottom to be by
544 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
545 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
546 * assignment this could happen... and I don't see how this would be
547 * a violation. So for now I am undecided an will leave the sort by
548 * SSN alone. Maybe a hybred approach is the answer
551 struct sctp_stream_in *strm;
552 struct sctp_queued_to_read *at;
558 asoc->size_on_all_streams += control->length;
559 sctp_ucount_incr(asoc->cnt_on_all_streams);
560 strm = &asoc->strmin[control->sinfo_stream];
561 nxt_todel = strm->last_sequence_delivered + 1;
562 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
563 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
565 SCTPDBG(SCTP_DEBUG_INDATA1,
566 "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
567 (uint32_t) control->sinfo_stream,
568 (uint32_t) strm->last_sequence_delivered,
569 (uint32_t) nxt_todel);
570 if (compare_with_wrap(strm->last_sequence_delivered,
571 control->sinfo_ssn, MAX_SEQ) ||
572 (strm->last_sequence_delivered == control->sinfo_ssn)) {
573 /* The incoming sseq is behind where we last delivered? */
574 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
575 control->sinfo_ssn, strm->last_sequence_delivered);
577 * throw it in the stream so it gets cleaned up in
578 * association destruction
580 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
581 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
582 0, M_DONTWAIT, 1, MT_DATA);
584 struct sctp_paramhdr *ph;
587 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
588 (sizeof(uint32_t) * 3);
589 ph = mtod(oper, struct sctp_paramhdr *);
590 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
591 ph->param_length = htons(SCTP_BUF_LEN(oper));
592 ippp = (uint32_t *) (ph + 1);
593 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
595 *ippp = control->sinfo_tsn;
597 *ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
599 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
600 sctp_abort_an_association(stcb->sctp_ep, stcb,
601 SCTP_PEER_FAULTY, oper);
607 if (nxt_todel == control->sinfo_ssn) {
608 /* can be delivered right away? */
609 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
610 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
613 asoc->size_on_all_streams -= control->length;
614 sctp_ucount_decr(asoc->cnt_on_all_streams);
615 strm->last_sequence_delivered++;
616 sctp_add_to_readq(stcb->sctp_ep, stcb,
618 &stcb->sctp_socket->so_rcv, 1);
619 control = TAILQ_FIRST(&strm->inqueue);
620 while (control != NULL) {
622 nxt_todel = strm->last_sequence_delivered + 1;
623 if (nxt_todel == control->sinfo_ssn) {
624 at = TAILQ_NEXT(control, next);
625 TAILQ_REMOVE(&strm->inqueue, control, next);
626 asoc->size_on_all_streams -= control->length;
627 sctp_ucount_decr(asoc->cnt_on_all_streams);
628 strm->last_sequence_delivered++;
630 * We ignore the return of deliver_data here
631 * since we always can hold the chunk on the
632 * d-queue. And we have a finite number that
633 * can be delivered from the strq.
635 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
636 sctp_log_strm_del(control, NULL,
637 SCTP_STR_LOG_FROM_IMMED_DEL);
639 sctp_add_to_readq(stcb->sctp_ep, stcb,
641 &stcb->sctp_socket->so_rcv, 1);
650 * Ok, we did not deliver this guy, find the correct place
651 * to put it on the queue.
653 if (TAILQ_EMPTY(&strm->inqueue)) {
655 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
656 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
658 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
660 TAILQ_FOREACH(at, &strm->inqueue, next) {
661 if (compare_with_wrap(at->sinfo_ssn,
662 control->sinfo_ssn, MAX_SEQ)) {
664 * one in queue is bigger than the
665 * new one, insert before this one
667 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
668 sctp_log_strm_del(control, at,
669 SCTP_STR_LOG_FROM_INSERT_MD);
671 TAILQ_INSERT_BEFORE(at, control, next);
673 } else if (at->sinfo_ssn == control->sinfo_ssn) {
675 * Gak, He sent me a duplicate str
679 * foo bar, I guess I will just free
680 * this new guy, should we abort
681 * too? FIX ME MAYBE? Or it COULD be
682 * that the SSN's have wrapped.
683 * Maybe I should compare to TSN
684 * somehow... sigh for now just blow
689 sctp_m_freem(control->data);
690 control->data = NULL;
691 asoc->size_on_all_streams -= control->length;
692 sctp_ucount_decr(asoc->cnt_on_all_streams);
693 sctp_free_remote_addr(control->whoFrom);
694 sctp_free_a_readq(stcb, control);
697 if (TAILQ_NEXT(at, next) == NULL) {
699 * We are at the end, insert
702 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
703 sctp_log_strm_del(control, at,
704 SCTP_STR_LOG_FROM_INSERT_TL);
706 TAILQ_INSERT_AFTER(&strm->inqueue,
717 * Returns two things: You get the total size of the deliverable parts of the
718 * first fragmented message on the reassembly queue. And you get a 1 back if
719 * all of the message is ready or a 0 back if the message is still incomplete
722 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
724 struct sctp_tmit_chunk *chk;
728 chk = TAILQ_FIRST(&asoc->reasmqueue);
730 /* nothing on the queue */
733 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
734 /* Not a first on the queue */
737 tsn = chk->rec.data.TSN_seq;
739 if (tsn != chk->rec.data.TSN_seq) {
742 *t_size += chk->send_size;
743 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
747 chk = TAILQ_NEXT(chk, sctp_next);
753 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
755 struct sctp_tmit_chunk *chk;
760 chk = TAILQ_FIRST(&asoc->reasmqueue);
763 asoc->size_on_reasm_queue = 0;
764 asoc->cnt_on_reasm_queue = 0;
767 if (asoc->fragmented_delivery_inprogress == 0) {
769 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
770 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
771 (nxt_todel == chk->rec.data.stream_seq ||
772 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
774 * Yep the first one is here and its ok to deliver
777 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
778 (tsize > stcb->sctp_ep->partial_delivery_point))) {
781 * Yes, we setup to start reception, by
782 * backing down the TSN just in case we
783 * can't deliver. If we
785 asoc->fragmented_delivery_inprogress = 1;
786 asoc->tsn_last_delivered =
787 chk->rec.data.TSN_seq - 1;
789 chk->rec.data.stream_number;
790 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
791 asoc->pdapi_ppid = chk->rec.data.payloadtype;
792 asoc->fragment_flags = chk->rec.data.rcv_flags;
793 sctp_service_reassembly(stcb, asoc);
798 * Service re-assembly will deliver stream data queued at
799 * the end of fragmented delivery.. but it wont know to go
800 * back and call itself again... we do that here with the
803 sctp_service_reassembly(stcb, asoc);
804 if (asoc->fragmented_delivery_inprogress == 0) {
806 * finished our Fragmented delivery, could be more
815 * Dump onto the re-assembly queue, in its proper place. After dumping on the
816 * queue, see if anthing can be delivered. If so pull it off (or as much as
817 * we can. If we run out of space then we must dump what we can and set the
818 * appropriate flag to say we queued what we could.
821 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
822 struct sctp_tmit_chunk *chk, int *abort_flag)
825 uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
827 struct sctp_tmit_chunk *at, *prev, *next;
830 cum_ackp1 = asoc->tsn_last_delivered + 1;
831 if (TAILQ_EMPTY(&asoc->reasmqueue)) {
832 /* This is the first one on the queue */
833 TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
835 * we do not check for delivery of anything when only one
838 asoc->size_on_reasm_queue = chk->send_size;
839 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
840 if (chk->rec.data.TSN_seq == cum_ackp1) {
841 if (asoc->fragmented_delivery_inprogress == 0 &&
842 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
843 SCTP_DATA_FIRST_FRAG) {
845 * An empty queue, no delivery inprogress,
846 * we hit the next one and it does NOT have
847 * a FIRST fragment mark.
849 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
850 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
851 0, M_DONTWAIT, 1, MT_DATA);
854 struct sctp_paramhdr *ph;
858 sizeof(struct sctp_paramhdr) +
859 (sizeof(uint32_t) * 3);
860 ph = mtod(oper, struct sctp_paramhdr *);
862 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
863 ph->param_length = htons(SCTP_BUF_LEN(oper));
864 ippp = (uint32_t *) (ph + 1);
865 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
867 *ippp = chk->rec.data.TSN_seq;
869 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
872 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
873 sctp_abort_an_association(stcb->sctp_ep, stcb,
874 SCTP_PEER_FAULTY, oper);
876 } else if (asoc->fragmented_delivery_inprogress &&
877 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
879 * We are doing a partial delivery and the
880 * NEXT chunk MUST be either the LAST or
881 * MIDDLE fragment NOT a FIRST
883 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
884 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
885 0, M_DONTWAIT, 1, MT_DATA);
887 struct sctp_paramhdr *ph;
891 sizeof(struct sctp_paramhdr) +
892 (3 * sizeof(uint32_t));
893 ph = mtod(oper, struct sctp_paramhdr *);
895 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
896 ph->param_length = htons(SCTP_BUF_LEN(oper));
897 ippp = (uint32_t *) (ph + 1);
898 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
900 *ippp = chk->rec.data.TSN_seq;
902 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
904 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
905 sctp_abort_an_association(stcb->sctp_ep, stcb,
906 SCTP_PEER_FAULTY, oper);
908 } else if (asoc->fragmented_delivery_inprogress) {
910 * Here we are ok with a MIDDLE or LAST
913 if (chk->rec.data.stream_number !=
914 asoc->str_of_pdapi) {
915 /* Got to be the right STR No */
916 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
917 chk->rec.data.stream_number,
919 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
920 0, M_DONTWAIT, 1, MT_DATA);
922 struct sctp_paramhdr *ph;
926 sizeof(struct sctp_paramhdr) +
927 (sizeof(uint32_t) * 3);
929 struct sctp_paramhdr *);
931 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
933 htons(SCTP_BUF_LEN(oper));
934 ippp = (uint32_t *) (ph + 1);
935 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
937 *ippp = chk->rec.data.TSN_seq;
939 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
941 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
942 sctp_abort_an_association(stcb->sctp_ep,
943 stcb, SCTP_PEER_FAULTY, oper);
945 } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
946 SCTP_DATA_UNORDERED &&
947 chk->rec.data.stream_seq !=
948 asoc->ssn_of_pdapi) {
949 /* Got to be the right STR Seq */
950 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
951 chk->rec.data.stream_seq,
953 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
954 0, M_DONTWAIT, 1, MT_DATA);
956 struct sctp_paramhdr *ph;
960 sizeof(struct sctp_paramhdr) +
961 (3 * sizeof(uint32_t));
963 struct sctp_paramhdr *);
965 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
967 htons(SCTP_BUF_LEN(oper));
968 ippp = (uint32_t *) (ph + 1);
969 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
971 *ippp = chk->rec.data.TSN_seq;
973 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
976 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
977 sctp_abort_an_association(stcb->sctp_ep,
978 stcb, SCTP_PEER_FAULTY, oper);
986 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
987 if (compare_with_wrap(at->rec.data.TSN_seq,
988 chk->rec.data.TSN_seq, MAX_TSN)) {
990 * one in queue is bigger than the new one, insert
994 asoc->size_on_reasm_queue += chk->send_size;
995 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
997 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
999 } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1000 /* Gak, He sent me a duplicate str seq number */
1002 * foo bar, I guess I will just free this new guy,
1003 * should we abort too? FIX ME MAYBE? Or it COULD be
1004 * that the SSN's have wrapped. Maybe I should
1005 * compare to TSN somehow... sigh for now just blow
1009 sctp_m_freem(chk->data);
1012 sctp_free_remote_addr(chk->whoTo);
1013 sctp_free_a_chunk(stcb, chk);
1016 last_flags = at->rec.data.rcv_flags;
1017 last_tsn = at->rec.data.TSN_seq;
1019 if (TAILQ_NEXT(at, sctp_next) == NULL) {
1021 * We are at the end, insert it after this
1024 /* check it first */
1025 asoc->size_on_reasm_queue += chk->send_size;
1026 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1027 TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1032 /* Now the audits */
1034 prev_tsn = chk->rec.data.TSN_seq - 1;
1035 if (prev_tsn == prev->rec.data.TSN_seq) {
1037 * Ok the one I am dropping onto the end is the
1038 * NEXT. A bit of valdiation here.
1040 if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1041 SCTP_DATA_FIRST_FRAG ||
1042 (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1043 SCTP_DATA_MIDDLE_FRAG) {
1045 * Insert chk MUST be a MIDDLE or LAST
1048 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1049 SCTP_DATA_FIRST_FRAG) {
1050 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1051 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1052 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1053 0, M_DONTWAIT, 1, MT_DATA);
1055 struct sctp_paramhdr *ph;
1058 SCTP_BUF_LEN(oper) =
1059 sizeof(struct sctp_paramhdr) +
1060 (3 * sizeof(uint32_t));
1062 struct sctp_paramhdr *);
1064 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1066 htons(SCTP_BUF_LEN(oper));
1067 ippp = (uint32_t *) (ph + 1);
1068 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1070 *ippp = chk->rec.data.TSN_seq;
1072 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1075 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1076 sctp_abort_an_association(stcb->sctp_ep,
1077 stcb, SCTP_PEER_FAULTY, oper);
1081 if (chk->rec.data.stream_number !=
1082 prev->rec.data.stream_number) {
1084 * Huh, need the correct STR here,
1085 * they must be the same.
1087 SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1088 chk->rec.data.stream_number,
1089 prev->rec.data.stream_number);
1090 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1091 0, M_DONTWAIT, 1, MT_DATA);
1093 struct sctp_paramhdr *ph;
1096 SCTP_BUF_LEN(oper) =
1097 sizeof(struct sctp_paramhdr) +
1098 (3 * sizeof(uint32_t));
1100 struct sctp_paramhdr *);
1102 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1104 htons(SCTP_BUF_LEN(oper));
1105 ippp = (uint32_t *) (ph + 1);
1106 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1108 *ippp = chk->rec.data.TSN_seq;
1110 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1112 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1113 sctp_abort_an_association(stcb->sctp_ep,
1114 stcb, SCTP_PEER_FAULTY, oper);
1119 if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1120 chk->rec.data.stream_seq !=
1121 prev->rec.data.stream_seq) {
1123 * Huh, need the correct STR here,
1124 * they must be the same.
1126 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1127 chk->rec.data.stream_seq,
1128 prev->rec.data.stream_seq);
1129 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1130 0, M_DONTWAIT, 1, MT_DATA);
1132 struct sctp_paramhdr *ph;
1135 SCTP_BUF_LEN(oper) =
1136 sizeof(struct sctp_paramhdr) +
1137 (3 * sizeof(uint32_t));
1139 struct sctp_paramhdr *);
1141 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1143 htons(SCTP_BUF_LEN(oper));
1144 ippp = (uint32_t *) (ph + 1);
1145 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1147 *ippp = chk->rec.data.TSN_seq;
1149 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1151 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1152 sctp_abort_an_association(stcb->sctp_ep,
1153 stcb, SCTP_PEER_FAULTY, oper);
1158 } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1159 SCTP_DATA_LAST_FRAG) {
1160 /* Insert chk MUST be a FIRST */
1161 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1162 SCTP_DATA_FIRST_FRAG) {
1163 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1164 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1165 0, M_DONTWAIT, 1, MT_DATA);
1167 struct sctp_paramhdr *ph;
1170 SCTP_BUF_LEN(oper) =
1171 sizeof(struct sctp_paramhdr) +
1172 (3 * sizeof(uint32_t));
1174 struct sctp_paramhdr *);
1176 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1178 htons(SCTP_BUF_LEN(oper));
1179 ippp = (uint32_t *) (ph + 1);
1180 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1182 *ippp = chk->rec.data.TSN_seq;
1184 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1187 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1188 sctp_abort_an_association(stcb->sctp_ep,
1189 stcb, SCTP_PEER_FAULTY, oper);
1198 post_tsn = chk->rec.data.TSN_seq + 1;
1199 if (post_tsn == next->rec.data.TSN_seq) {
1201 * Ok the one I am inserting ahead of is my NEXT
1202 * one. A bit of valdiation here.
1204 if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1205 /* Insert chk MUST be a last fragment */
1206 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1207 != SCTP_DATA_LAST_FRAG) {
1208 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1209 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1210 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1211 0, M_DONTWAIT, 1, MT_DATA);
1213 struct sctp_paramhdr *ph;
1216 SCTP_BUF_LEN(oper) =
1217 sizeof(struct sctp_paramhdr) +
1218 (3 * sizeof(uint32_t));
1220 struct sctp_paramhdr *);
1222 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1224 htons(SCTP_BUF_LEN(oper));
1225 ippp = (uint32_t *) (ph + 1);
1226 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1228 *ippp = chk->rec.data.TSN_seq;
1230 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1232 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1233 sctp_abort_an_association(stcb->sctp_ep,
1234 stcb, SCTP_PEER_FAULTY, oper);
1239 } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1240 SCTP_DATA_MIDDLE_FRAG ||
1241 (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1242 SCTP_DATA_LAST_FRAG) {
1244 * Insert chk CAN be MIDDLE or FIRST NOT
1247 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1248 SCTP_DATA_LAST_FRAG) {
1249 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1250 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1251 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1252 0, M_DONTWAIT, 1, MT_DATA);
1254 struct sctp_paramhdr *ph;
1257 SCTP_BUF_LEN(oper) =
1258 sizeof(struct sctp_paramhdr) +
1259 (3 * sizeof(uint32_t));
1261 struct sctp_paramhdr *);
1263 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1265 htons(SCTP_BUF_LEN(oper));
1266 ippp = (uint32_t *) (ph + 1);
1267 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1269 *ippp = chk->rec.data.TSN_seq;
1271 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1274 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1275 sctp_abort_an_association(stcb->sctp_ep,
1276 stcb, SCTP_PEER_FAULTY, oper);
1281 if (chk->rec.data.stream_number !=
1282 next->rec.data.stream_number) {
1284 * Huh, need the correct STR here,
1285 * they must be the same.
1287 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1288 chk->rec.data.stream_number,
1289 next->rec.data.stream_number);
1290 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1291 0, M_DONTWAIT, 1, MT_DATA);
1293 struct sctp_paramhdr *ph;
1296 SCTP_BUF_LEN(oper) =
1297 sizeof(struct sctp_paramhdr) +
1298 (3 * sizeof(uint32_t));
1300 struct sctp_paramhdr *);
1302 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1304 htons(SCTP_BUF_LEN(oper));
1305 ippp = (uint32_t *) (ph + 1);
1306 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1308 *ippp = chk->rec.data.TSN_seq;
1310 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1313 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1314 sctp_abort_an_association(stcb->sctp_ep,
1315 stcb, SCTP_PEER_FAULTY, oper);
1320 if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1321 chk->rec.data.stream_seq !=
1322 next->rec.data.stream_seq) {
1324 * Huh, need the correct STR here,
1325 * they must be the same.
1327 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1328 chk->rec.data.stream_seq,
1329 next->rec.data.stream_seq);
1330 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1331 0, M_DONTWAIT, 1, MT_DATA);
1333 struct sctp_paramhdr *ph;
1336 SCTP_BUF_LEN(oper) =
1337 sizeof(struct sctp_paramhdr) +
1338 (3 * sizeof(uint32_t));
1340 struct sctp_paramhdr *);
1342 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1344 htons(SCTP_BUF_LEN(oper));
1345 ippp = (uint32_t *) (ph + 1);
1346 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1348 *ippp = chk->rec.data.TSN_seq;
1350 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1352 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1353 sctp_abort_an_association(stcb->sctp_ep,
1354 stcb, SCTP_PEER_FAULTY, oper);
1363 /* Do we need to do some delivery? check */
1364 sctp_deliver_reasm_check(stcb, asoc);
1368 * This is an unfortunate routine. It checks to make sure a evil guy is not
1369 * stuffing us full of bad packet fragments. A broken peer could also do this
1370 * but this is doubtful. It is to bad I must worry about evil crackers sigh
1374 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1377 struct sctp_tmit_chunk *at;
1380 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1381 if (compare_with_wrap(TSN_seq,
1382 at->rec.data.TSN_seq, MAX_TSN)) {
1383 /* is it one bigger? */
1384 tsn_est = at->rec.data.TSN_seq + 1;
1385 if (tsn_est == TSN_seq) {
1386 /* yep. It better be a last then */
1387 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1388 SCTP_DATA_LAST_FRAG) {
1390 * Ok this guy belongs next to a guy
1391 * that is NOT last, it should be a
1392 * middle/last, not a complete
1398 * This guy is ok since its a LAST
1399 * and the new chunk is a fully
1400 * self- contained one.
1405 } else if (TSN_seq == at->rec.data.TSN_seq) {
1406 /* Software error since I have a dup? */
1410 * Ok, 'at' is larger than new chunk but does it
1411 * need to be right before it.
1413 tsn_est = TSN_seq + 1;
1414 if (tsn_est == at->rec.data.TSN_seq) {
1415 /* Yep, It better be a first */
1416 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1417 SCTP_DATA_FIRST_FRAG) {
1430 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1431 struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1432 struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1433 int *break_flag, int last_chunk)
1435 /* Process a data chunk */
1436 /* struct sctp_tmit_chunk *chk; */
1437 struct sctp_tmit_chunk *chk;
1441 int need_reasm_check = 0;
1442 uint16_t strmno, strmseq;
1444 struct sctp_queued_to_read *control;
1446 uint32_t protocol_id;
1447 uint8_t chunk_flags;
1448 struct sctp_stream_reset_list *liste;
1451 tsn = ntohl(ch->dp.tsn);
1452 chunk_flags = ch->ch.chunk_flags;
1453 protocol_id = ch->dp.protocol_id;
1454 ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1455 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1456 sctp_log_map(0, tsn, asoc->cumulative_tsn, SCTP_MAP_PREPARE_SLIDE);
1461 SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1462 if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1463 asoc->cumulative_tsn == tsn) {
1464 /* It is a duplicate */
1465 SCTP_STAT_INCR(sctps_recvdupdata);
1466 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1467 /* Record a dup for the next outbound sack */
1468 asoc->dup_tsns[asoc->numduptsns] = tsn;
1473 /* Calculate the number of TSN's between the base and this TSN */
1474 if (tsn >= asoc->mapping_array_base_tsn) {
1475 gap = tsn - asoc->mapping_array_base_tsn;
1477 gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1479 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1480 /* Can't hold the bit in the mapping at max array, toss it */
1483 if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1484 SCTP_TCB_LOCK_ASSERT(stcb);
1485 if (sctp_expand_mapping_array(asoc, gap)) {
1486 /* Can't expand, drop it */
1490 if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1493 /* See if we have received this one already */
1494 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1495 SCTP_STAT_INCR(sctps_recvdupdata);
1496 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1497 /* Record a dup for the next outbound sack */
1498 asoc->dup_tsns[asoc->numduptsns] = tsn;
1501 asoc->send_sack = 1;
1505 * Check to see about the GONE flag, duplicates would cause a sack
1506 * to be sent up above
1508 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1509 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1510 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1513 * wait a minute, this guy is gone, there is no longer a
1514 * receiver. Send peer an ABORT!
1516 struct mbuf *op_err;
1518 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1519 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
1524 * Now before going further we see if there is room. If NOT then we
1525 * MAY let one through only IF this TSN is the one we are waiting
1526 * for on a partial delivery API.
1529 /* now do the tests */
1530 if (((asoc->cnt_on_all_streams +
1531 asoc->cnt_on_reasm_queue +
1532 asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
1533 (((int)asoc->my_rwnd) <= 0)) {
1535 * When we have NO room in the rwnd we check to make sure
1536 * the reader is doing its job...
1538 if (stcb->sctp_socket->so_rcv.sb_cc) {
1539 /* some to read, wake-up */
1540 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1542 /* now is it in the mapping array of what we have accepted? */
1543 if (compare_with_wrap(tsn,
1544 asoc->highest_tsn_inside_map, MAX_TSN)) {
1546 /* Nope not in the valid range dump it */
1547 SCTPDBG(SCTP_DEBUG_INDATA1, "My rwnd overrun1:tsn:%lx rwnd %lu sbspace:%ld\n",
1548 (u_long)tsn, (u_long)asoc->my_rwnd,
1549 sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv));
1550 sctp_set_rwnd(stcb, asoc);
1551 if ((asoc->cnt_on_all_streams +
1552 asoc->cnt_on_reasm_queue +
1553 asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
1554 SCTP_STAT_INCR(sctps_datadropchklmt);
1556 SCTP_STAT_INCR(sctps_datadroprwnd);
1563 strmno = ntohs(ch->dp.stream_id);
1564 if (strmno >= asoc->streamincnt) {
1565 struct sctp_paramhdr *phdr;
1568 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1569 0, M_DONTWAIT, 1, MT_DATA);
1571 /* add some space up front so prepend will work well */
1572 SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1573 phdr = mtod(mb, struct sctp_paramhdr *);
1575 * Error causes are just param's and this one has
1576 * two back to back phdr, one with the error type
1577 * and size, the other with the streamid and a rsvd
1579 SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1580 phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1581 phdr->param_length =
1582 htons(sizeof(struct sctp_paramhdr) * 2);
1584 /* We insert the stream in the type field */
1585 phdr->param_type = ch->dp.stream_id;
1586 /* And set the length to 0 for the rsvd field */
1587 phdr->param_length = 0;
1588 sctp_queue_op_err(stcb, mb);
1590 SCTP_STAT_INCR(sctps_badsid);
1591 SCTP_TCB_LOCK_ASSERT(stcb);
1592 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1593 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1594 /* we have a new high score */
1595 asoc->highest_tsn_inside_map = tsn;
1596 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1597 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1600 if (tsn == (asoc->cumulative_tsn + 1)) {
1601 /* Update cum-ack */
1602 asoc->cumulative_tsn = tsn;
1607 * Before we continue lets validate that we are not being fooled by
1608 * an evil attacker. We can only have 4k chunks based on our TSN
1609 * spread allowed by the mapping array 512 * 8 bits, so there is no
1610 * way our stream sequence numbers could have wrapped. We of course
1611 * only validate the FIRST fragment so the bit must be set.
1613 strmseq = ntohs(ch->dp.stream_sequence);
1614 #ifdef SCTP_ASOCLOG_OF_TSNS
1615 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1616 asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1617 asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1618 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1619 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1621 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1622 asoc->tsn_in_at = 0;
1623 asoc->tsn_in_wrapped = 1;
1626 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1627 (TAILQ_EMPTY(&asoc->resetHead)) &&
1628 (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1629 (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1630 strmseq, MAX_SEQ) ||
1631 asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1632 /* The incoming sseq is behind where we last delivered? */
1633 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1634 strmseq, asoc->strmin[strmno].last_sequence_delivered);
1635 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1636 0, M_DONTWAIT, 1, MT_DATA);
1638 struct sctp_paramhdr *ph;
1641 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1642 (3 * sizeof(uint32_t));
1643 ph = mtod(oper, struct sctp_paramhdr *);
1644 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1645 ph->param_length = htons(SCTP_BUF_LEN(oper));
1646 ippp = (uint32_t *) (ph + 1);
1647 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1651 *ippp = ((strmno << 16) | strmseq);
1654 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1655 sctp_abort_an_association(stcb->sctp_ep, stcb,
1656 SCTP_PEER_FAULTY, oper);
1660 /************************************
1661 * From here down we may find ch-> invalid
1662 * so its a good idea NOT to use it.
1663 *************************************/
1665 the_len = (chk_length - sizeof(struct sctp_data_chunk));
1666 if (last_chunk == 0) {
1667 dmbuf = SCTP_M_COPYM(*m,
1668 (offset + sizeof(struct sctp_data_chunk)),
1669 the_len, M_DONTWAIT);
1670 #ifdef SCTP_MBUF_LOGGING
1671 if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
1676 if (SCTP_BUF_IS_EXTENDED(mat)) {
1677 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1679 mat = SCTP_BUF_NEXT(mat);
1684 /* We can steal the last chunk */
1688 /* lop off the top part */
1689 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1690 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1691 l_len = SCTP_BUF_LEN(dmbuf);
1694 * need to count up the size hopefully does not hit
1702 l_len += SCTP_BUF_LEN(lat);
1703 lat = SCTP_BUF_NEXT(lat);
1706 if (l_len > the_len) {
1707 /* Trim the end round bytes off too */
1708 m_adj(dmbuf, -(l_len - the_len));
1711 if (dmbuf == NULL) {
1712 SCTP_STAT_INCR(sctps_nomem);
1715 if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1716 asoc->fragmented_delivery_inprogress == 0 &&
1717 TAILQ_EMPTY(&asoc->resetHead) &&
1719 ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1720 TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1721 /* Candidate for express delivery */
1723 * Its not fragmented, No PD-API is up, Nothing in the
1724 * delivery queue, Its un-ordered OR ordered and the next to
1725 * deliver AND nothing else is stuck on the stream queue,
1726 * And there is room for it in the socket buffer. Lets just
1727 * stuff it up the buffer....
1730 /* It would be nice to avoid this copy if we could :< */
1731 sctp_alloc_a_readq(stcb, control);
1732 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1738 if (control == NULL) {
1739 goto failed_express_del;
1741 sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1);
1742 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1743 /* for ordered, bump what we delivered */
1744 asoc->strmin[strmno].last_sequence_delivered++;
1746 SCTP_STAT_INCR(sctps_recvexpress);
1747 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
1748 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1749 SCTP_STR_LOG_FROM_EXPRS_DEL);
1752 goto finish_express_del;
1755 /* If we reach here this is a new chunk */
1758 /* Express for fragmented delivery? */
1759 if ((asoc->fragmented_delivery_inprogress) &&
1760 (stcb->asoc.control_pdapi) &&
1761 (asoc->str_of_pdapi == strmno) &&
1762 (asoc->ssn_of_pdapi == strmseq)
1764 control = stcb->asoc.control_pdapi;
1765 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1766 /* Can't be another first? */
1767 goto failed_pdapi_express_del;
1769 if (tsn == (control->sinfo_tsn + 1)) {
1770 /* Yep, we can add it on */
1774 if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1777 cumack = asoc->cumulative_tsn;
1778 if ((cumack + 1) == tsn)
1781 if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1783 &stcb->sctp_socket->so_rcv)) {
1784 SCTP_PRINTF("Append fails end:%d\n", end);
1785 goto failed_pdapi_express_del;
1787 SCTP_STAT_INCR(sctps_recvexpressm);
1788 control->sinfo_tsn = tsn;
1789 asoc->tsn_last_delivered = tsn;
1790 asoc->fragment_flags = chunk_flags;
1791 asoc->tsn_of_pdapi_last_delivered = tsn;
1792 asoc->last_flags_delivered = chunk_flags;
1793 asoc->last_strm_seq_delivered = strmseq;
1794 asoc->last_strm_no_delivered = strmno;
1796 /* clean up the flags and such */
1797 asoc->fragmented_delivery_inprogress = 0;
1798 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1799 asoc->strmin[strmno].last_sequence_delivered++;
1801 stcb->asoc.control_pdapi = NULL;
1802 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1804 * There could be another message
1807 need_reasm_check = 1;
1811 goto finish_express_del;
1814 failed_pdapi_express_del:
1816 if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1817 sctp_alloc_a_chunk(stcb, chk);
1819 /* No memory so we drop the chunk */
1820 SCTP_STAT_INCR(sctps_nomem);
1821 if (last_chunk == 0) {
1822 /* we copied it, free the copy */
1823 sctp_m_freem(dmbuf);
1827 chk->rec.data.TSN_seq = tsn;
1828 chk->no_fr_allowed = 0;
1829 chk->rec.data.stream_seq = strmseq;
1830 chk->rec.data.stream_number = strmno;
1831 chk->rec.data.payloadtype = protocol_id;
1832 chk->rec.data.context = stcb->asoc.context;
1833 chk->rec.data.doing_fast_retransmit = 0;
1834 chk->rec.data.rcv_flags = chunk_flags;
1836 chk->send_size = the_len;
1838 atomic_add_int(&net->ref_count, 1);
1841 sctp_alloc_a_readq(stcb, control);
1842 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1848 if (control == NULL) {
1849 /* No memory so we drop the chunk */
1850 SCTP_STAT_INCR(sctps_nomem);
1851 if (last_chunk == 0) {
1852 /* we copied it, free the copy */
1853 sctp_m_freem(dmbuf);
1857 control->length = the_len;
1860 /* Mark it as received */
1861 /* Now queue it where it belongs */
1862 if (control != NULL) {
1863 /* First a sanity check */
1864 if (asoc->fragmented_delivery_inprogress) {
1866 * Ok, we have a fragmented delivery in progress if
1867 * this chunk is next to deliver OR belongs in our
1868 * view to the reassembly, the peer is evil or
1871 uint32_t estimate_tsn;
1873 estimate_tsn = asoc->tsn_last_delivered + 1;
1874 if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1875 (estimate_tsn == control->sinfo_tsn)) {
1876 /* Evil/Broke peer */
1877 sctp_m_freem(control->data);
1878 control->data = NULL;
1879 sctp_free_remote_addr(control->whoFrom);
1880 sctp_free_a_readq(stcb, control);
1881 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1882 0, M_DONTWAIT, 1, MT_DATA);
1884 struct sctp_paramhdr *ph;
1887 SCTP_BUF_LEN(oper) =
1888 sizeof(struct sctp_paramhdr) +
1889 (3 * sizeof(uint32_t));
1890 ph = mtod(oper, struct sctp_paramhdr *);
1892 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1893 ph->param_length = htons(SCTP_BUF_LEN(oper));
1894 ippp = (uint32_t *) (ph + 1);
1895 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1899 *ippp = ((strmno << 16) | strmseq);
1901 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1902 sctp_abort_an_association(stcb->sctp_ep, stcb,
1903 SCTP_PEER_FAULTY, oper);
1908 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1909 sctp_m_freem(control->data);
1910 control->data = NULL;
1911 sctp_free_remote_addr(control->whoFrom);
1912 sctp_free_a_readq(stcb, control);
1914 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1915 0, M_DONTWAIT, 1, MT_DATA);
1917 struct sctp_paramhdr *ph;
1920 SCTP_BUF_LEN(oper) =
1921 sizeof(struct sctp_paramhdr) +
1922 (3 * sizeof(uint32_t));
1924 struct sctp_paramhdr *);
1926 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1928 htons(SCTP_BUF_LEN(oper));
1929 ippp = (uint32_t *) (ph + 1);
1930 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1934 *ippp = ((strmno << 16) | strmseq);
1936 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1937 sctp_abort_an_association(stcb->sctp_ep,
1938 stcb, SCTP_PEER_FAULTY, oper);
1945 /* No PDAPI running */
1946 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1948 * Reassembly queue is NOT empty validate
1949 * that this tsn does not need to be in
1950 * reasembly queue. If it does then our peer
1951 * is broken or evil.
1953 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1954 sctp_m_freem(control->data);
1955 control->data = NULL;
1956 sctp_free_remote_addr(control->whoFrom);
1957 sctp_free_a_readq(stcb, control);
1958 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1959 0, M_DONTWAIT, 1, MT_DATA);
1961 struct sctp_paramhdr *ph;
1964 SCTP_BUF_LEN(oper) =
1965 sizeof(struct sctp_paramhdr) +
1966 (3 * sizeof(uint32_t));
1968 struct sctp_paramhdr *);
1970 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1972 htons(SCTP_BUF_LEN(oper));
1973 ippp = (uint32_t *) (ph + 1);
1974 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
1978 *ippp = ((strmno << 16) | strmseq);
1980 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1981 sctp_abort_an_association(stcb->sctp_ep,
1982 stcb, SCTP_PEER_FAULTY, oper);
1989 /* ok, if we reach here we have passed the sanity checks */
1990 if (chunk_flags & SCTP_DATA_UNORDERED) {
1991 /* queue directly into socket buffer */
1992 sctp_add_to_readq(stcb->sctp_ep, stcb,
1994 &stcb->sctp_socket->so_rcv, 1);
1997 * Special check for when streams are resetting. We
1998 * could be more smart about this and check the
1999 * actual stream to see if it is not being reset..
2000 * that way we would not create a HOLB when amongst
2001 * streams being reset and those not being reset.
2003 * We take complete messages that have a stream reset
2004 * intervening (aka the TSN is after where our
2005 * cum-ack needs to be) off and put them on a
2006 * pending_reply_queue. The reassembly ones we do
2007 * not have to worry about since they are all sorted
2008 * and proceessed by TSN order. It is only the
2009 * singletons I must worry about.
2011 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2012 ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2015 * yep its past where we need to reset... go
2016 * ahead and queue it.
2018 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2020 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2022 struct sctp_queued_to_read *ctlOn;
2023 unsigned char inserted = 0;
2025 ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2027 if (compare_with_wrap(control->sinfo_tsn,
2028 ctlOn->sinfo_tsn, MAX_TSN)) {
2029 ctlOn = TAILQ_NEXT(ctlOn, next);
2032 TAILQ_INSERT_BEFORE(ctlOn, control, next);
2037 if (inserted == 0) {
2039 * must be put at end, use
2040 * prevP (all setup from
2041 * loop) to setup nextP.
2043 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2047 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2054 /* Into the re-assembly queue */
2055 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2058 * the assoc is now gone and chk was put onto the
2059 * reasm queue, which has all been freed.
2066 if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2067 /* we have a new high score */
2068 asoc->highest_tsn_inside_map = tsn;
2069 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2070 sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2073 if (tsn == (asoc->cumulative_tsn + 1)) {
2074 /* Update cum-ack */
2075 asoc->cumulative_tsn = tsn;
2081 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2083 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2085 SCTP_STAT_INCR(sctps_recvdata);
2086 /* Set it present please */
2087 if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
2088 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2090 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2091 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2092 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2094 SCTP_TCB_LOCK_ASSERT(stcb);
2095 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2096 /* check the special flag for stream resets */
2097 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2098 ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2099 (asoc->cumulative_tsn == liste->tsn))
2102 * we have finished working through the backlogged TSN's now
2103 * time to reset streams. 1: call reset function. 2: free
2104 * pending_reply space 3: distribute any chunks in
2105 * pending_reply_queue.
2107 struct sctp_queued_to_read *ctl;
2109 sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2110 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2111 SCTP_FREE(liste, SCTP_M_STRESET);
2112 /* sa_ignore FREED_MEMORY */
2113 liste = TAILQ_FIRST(&asoc->resetHead);
2114 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2115 if (ctl && (liste == NULL)) {
2116 /* All can be removed */
2118 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2119 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2123 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2126 /* more than one in queue */
2127 while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2129 * if ctl->sinfo_tsn is <= liste->tsn we can
2130 * process it which is the NOT of
2131 * ctl->sinfo_tsn > liste->tsn
2133 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2134 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2138 ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2142 * Now service re-assembly to pick up anything that has been
2143 * held on reassembly queue?
2145 sctp_deliver_reasm_check(stcb, asoc);
2146 need_reasm_check = 0;
2148 if (need_reasm_check) {
2149 /* Another one waits ? */
2150 sctp_deliver_reasm_check(stcb, asoc);
2155 int8_t sctp_map_lookup_tab[256] = {
2156 -1, 0, -1, 1, -1, 0, -1, 2,
2157 -1, 0, -1, 1, -1, 0, -1, 3,
2158 -1, 0, -1, 1, -1, 0, -1, 2,
2159 -1, 0, -1, 1, -1, 0, -1, 4,
2160 -1, 0, -1, 1, -1, 0, -1, 2,
2161 -1, 0, -1, 1, -1, 0, -1, 3,
2162 -1, 0, -1, 1, -1, 0, -1, 2,
2163 -1, 0, -1, 1, -1, 0, -1, 5,
2164 -1, 0, -1, 1, -1, 0, -1, 2,
2165 -1, 0, -1, 1, -1, 0, -1, 3,
2166 -1, 0, -1, 1, -1, 0, -1, 2,
2167 -1, 0, -1, 1, -1, 0, -1, 4,
2168 -1, 0, -1, 1, -1, 0, -1, 2,
2169 -1, 0, -1, 1, -1, 0, -1, 3,
2170 -1, 0, -1, 1, -1, 0, -1, 2,
2171 -1, 0, -1, 1, -1, 0, -1, 6,
2172 -1, 0, -1, 1, -1, 0, -1, 2,
2173 -1, 0, -1, 1, -1, 0, -1, 3,
2174 -1, 0, -1, 1, -1, 0, -1, 2,
2175 -1, 0, -1, 1, -1, 0, -1, 4,
2176 -1, 0, -1, 1, -1, 0, -1, 2,
2177 -1, 0, -1, 1, -1, 0, -1, 3,
2178 -1, 0, -1, 1, -1, 0, -1, 2,
2179 -1, 0, -1, 1, -1, 0, -1, 5,
2180 -1, 0, -1, 1, -1, 0, -1, 2,
2181 -1, 0, -1, 1, -1, 0, -1, 3,
2182 -1, 0, -1, 1, -1, 0, -1, 2,
2183 -1, 0, -1, 1, -1, 0, -1, 4,
2184 -1, 0, -1, 1, -1, 0, -1, 2,
2185 -1, 0, -1, 1, -1, 0, -1, 3,
2186 -1, 0, -1, 1, -1, 0, -1, 2,
2187 -1, 0, -1, 1, -1, 0, -1, 7,
2192 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2195 * Now we also need to check the mapping array in a couple of ways.
2196 * 1) Did we move the cum-ack point?
2198 struct sctp_association *asoc;
2200 int all_ones, last_all_ones = 0;
2201 int slide_from, slide_end, lgap, distance;
2202 uint32_t old_cumack, old_base, old_highest;
2203 unsigned char aux_array[64];
2209 old_cumack = asoc->cumulative_tsn;
2210 old_base = asoc->mapping_array_base_tsn;
2211 old_highest = asoc->highest_tsn_inside_map;
2212 if (asoc->mapping_array_size < 64)
2213 memcpy(aux_array, asoc->mapping_array,
2214 asoc->mapping_array_size);
2216 memcpy(aux_array, asoc->mapping_array, 64);
2219 * We could probably improve this a small bit by calculating the
2220 * offset of the current cum-ack as the starting point.
2224 for (i = 0; i < stcb->asoc.mapping_array_size; i++) {
2226 if (asoc->mapping_array[i] == 0xff) {
2230 /* there is a 0 bit */
2232 at += sctp_map_lookup_tab[asoc->mapping_array[i]];
2237 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2238 /* at is one off, since in the table a embedded -1 is present */
2241 if (compare_with_wrap(asoc->cumulative_tsn,
2242 asoc->highest_tsn_inside_map,
2245 panic("huh, cumack greater than high-tsn in map");
2247 SCTP_PRINTF("huh, cumack greater than high-tsn in map - should panic?\n");
2248 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2252 (asoc->cumulative_tsn == asoc->highest_tsn_inside_map && at >= 8)) {
2253 /* The complete array was completed by a single FR */
2254 /* higest becomes the cum-ack */
2257 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2258 /* clear the array */
2260 clr = asoc->mapping_array_size;
2262 clr = (at >> 3) + 1;
2264 * this should be the allones case but just in case
2267 if (clr > asoc->mapping_array_size)
2268 clr = asoc->mapping_array_size;
2270 memset(asoc->mapping_array, 0, clr);
2271 /* base becomes one ahead of the cum-ack */
2272 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2273 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2274 sctp_log_map(old_base, old_cumack, old_highest,
2275 SCTP_MAP_PREPARE_SLIDE);
2276 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2277 asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2279 } else if (at >= 8) {
2280 /* we can slide the mapping array down */
2281 /* Calculate the new byte postion we can move down */
2282 slide_from = at >> 3;
2284 * now calculate the ceiling of the move using our highest
2287 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2288 lgap = asoc->highest_tsn_inside_map -
2289 asoc->mapping_array_base_tsn;
2291 lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2292 asoc->highest_tsn_inside_map + 1;
2294 slide_end = lgap >> 3;
2295 if (slide_end < slide_from) {
2296 panic("impossible slide");
2298 distance = (slide_end - slide_from) + 1;
2299 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2300 sctp_log_map(old_base, old_cumack, old_highest,
2301 SCTP_MAP_PREPARE_SLIDE);
2302 sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2303 (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2305 if (distance + slide_from > asoc->mapping_array_size ||
2308 * Here we do NOT slide forward the array so that
2309 * hopefully when more data comes in to fill it up
2310 * we will be able to slide it forward. Really I
2311 * don't think this should happen :-0
2314 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2315 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2316 (uint32_t) asoc->mapping_array_size,
2317 SCTP_MAP_SLIDE_NONE);
2322 for (ii = 0; ii < distance; ii++) {
2323 asoc->mapping_array[ii] =
2324 asoc->mapping_array[slide_from + ii];
2326 for (ii = distance; ii <= slide_end; ii++) {
2327 asoc->mapping_array[ii] = 0;
2329 asoc->mapping_array_base_tsn += (slide_from << 3);
2330 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2331 sctp_log_map(asoc->mapping_array_base_tsn,
2332 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2333 SCTP_MAP_SLIDE_RESULT);
2338 * Now we need to see if we need to queue a sack or just start the
2339 * timer (if allowed).
2342 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2344 * Ok special case, in SHUTDOWN-SENT case. here we
2345 * maker sure SACK timer is off and instead send a
2346 * SHUTDOWN and a SACK
2348 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2349 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2350 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2352 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2353 sctp_send_sack(stcb);
2357 /* is there a gap now ? */
2358 is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2359 stcb->asoc.cumulative_tsn, MAX_TSN);
2362 * CMT DAC algorithm: increase number of packets
2363 * received since last ack
2365 stcb->asoc.cmt_dac_pkts_rcvd++;
2367 if ((stcb->asoc.send_sack == 1) || /* We need to send a
2369 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2371 (stcb->asoc.numduptsns) || /* we have dup's */
2372 (is_a_gap) || /* is still a gap */
2373 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2374 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2377 if ((sctp_cmt_on_off) && (sctp_cmt_use_dac) &&
2378 (stcb->asoc.send_sack == 0) &&
2379 (stcb->asoc.numduptsns == 0) &&
2380 (stcb->asoc.delayed_ack) &&
2381 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2384 * CMT DAC algorithm: With CMT,
2385 * delay acks even in the face of
2387 * reordering. Therefore, if acks that
2388 * do not have to be sent because of
2389 * the above reasons, will be
2390 * delayed. That is, acks that would
2391 * have been sent due to gap reports
2392 * will be delayed with DAC. Start
2393 * the delayed ack timer.
2395 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2396 stcb->sctp_ep, stcb, NULL);
2399 * Ok we must build a SACK since the
2400 * timer is pending, we got our
2401 * first packet OR there are gaps or
2404 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2405 sctp_send_sack(stcb);
2408 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2409 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2410 stcb->sctp_ep, stcb, NULL);
2418 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2420 struct sctp_tmit_chunk *chk;
2424 if (asoc->fragmented_delivery_inprogress) {
2425 sctp_service_reassembly(stcb, asoc);
2427 /* Can we proceed further, i.e. the PD-API is complete */
2428 if (asoc->fragmented_delivery_inprogress) {
2433 * Now is there some other chunk I can deliver from the reassembly
2437 chk = TAILQ_FIRST(&asoc->reasmqueue);
2439 asoc->size_on_reasm_queue = 0;
2440 asoc->cnt_on_reasm_queue = 0;
2443 nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2444 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2445 ((nxt_todel == chk->rec.data.stream_seq) ||
2446 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2448 * Yep the first one is here. We setup to start reception,
2449 * by backing down the TSN just in case we can't deliver.
2453 * Before we start though either all of the message should
2454 * be here or 1/4 the socket buffer max or nothing on the
2455 * delivery queue and something can be delivered.
2457 if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2458 (tsize > stcb->sctp_ep->partial_delivery_point))) {
2459 asoc->fragmented_delivery_inprogress = 1;
2460 asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2461 asoc->str_of_pdapi = chk->rec.data.stream_number;
2462 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2463 asoc->pdapi_ppid = chk->rec.data.payloadtype;
2464 asoc->fragment_flags = chk->rec.data.rcv_flags;
2465 sctp_service_reassembly(stcb, asoc);
2466 if (asoc->fragmented_delivery_inprogress == 0) {
2474 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2475 struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2476 struct sctp_nets *net, uint32_t * high_tsn)
2478 struct sctp_data_chunk *ch, chunk_buf;
2479 struct sctp_association *asoc;
2480 int num_chunks = 0; /* number of control chunks processed */
2482 int chk_length, break_flag, last_chunk;
2483 int abort_flag = 0, was_a_gap = 0;
2487 sctp_set_rwnd(stcb, &stcb->asoc);
2490 SCTP_TCB_LOCK_ASSERT(stcb);
2492 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
2493 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2494 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
2496 * wait a minute, this guy is gone, there is no longer a
2497 * receiver. Send peer an ABORT!
2499 struct mbuf *op_err;
2501 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2502 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
2505 if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2506 stcb->asoc.cumulative_tsn, MAX_TSN)) {
2507 /* there was a gap before this data was processed */
2511 * setup where we got the last DATA packet from for any SACK that
2512 * may need to go out. Don't bump the net. This is done ONLY when a
2513 * chunk is assigned.
2515 asoc->last_data_chunk_from = net;
2518 * Now before we proceed we must figure out if this is a wasted
2519 * cluster... i.e. it is a small packet sent in and yet the driver
2520 * underneath allocated a full cluster for it. If so we must copy it
2521 * to a smaller mbuf and free up the cluster mbuf. This will help
2522 * with cluster starvation. Note for __Panda__ we don't do this
2523 * since it has clusters all the way down to 64 bytes.
2525 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2526 /* we only handle mbufs that are singletons.. not chains */
2527 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2529 /* ok lets see if we can copy the data up */
2532 /* get the pointers and copy */
2533 to = mtod(m, caddr_t *);
2534 from = mtod((*mm), caddr_t *);
2535 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2536 /* copy the length and free up the old */
2537 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2539 /* sucess, back copy */
2542 /* We are in trouble in the mbuf world .. yikes */
2546 /* get pointer to the first chunk header */
2547 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2548 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2553 * process all DATA chunks...
2555 *high_tsn = asoc->cumulative_tsn;
2557 asoc->data_pkts_seen++;
2558 while (stop_proc == 0) {
2559 /* validate chunk length */
2560 chk_length = ntohs(ch->ch.chunk_length);
2561 if (length - *offset < chk_length) {
2562 /* all done, mutulated chunk */
2566 if (ch->ch.chunk_type == SCTP_DATA) {
2567 if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2569 * Need to send an abort since we had a
2570 * invalid data chunk.
2572 struct mbuf *op_err;
2574 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2575 0, M_DONTWAIT, 1, MT_DATA);
2578 struct sctp_paramhdr *ph;
2581 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2582 (2 * sizeof(uint32_t));
2583 ph = mtod(op_err, struct sctp_paramhdr *);
2585 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2586 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2587 ippp = (uint32_t *) (ph + 1);
2588 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2590 *ippp = asoc->cumulative_tsn;
2593 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2594 sctp_abort_association(inp, stcb, m, iphlen, sh,
2598 #ifdef SCTP_AUDITING_ENABLED
2599 sctp_audit_log(0xB1, 0);
2601 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2606 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2607 chk_length, net, high_tsn, &abort_flag, &break_flag,
2616 * Set because of out of rwnd space and no
2617 * drop rep space left.
2623 /* not a data chunk in the data region */
2624 switch (ch->ch.chunk_type) {
2625 case SCTP_INITIATION:
2626 case SCTP_INITIATION_ACK:
2627 case SCTP_SELECTIVE_ACK:
2628 case SCTP_HEARTBEAT_REQUEST:
2629 case SCTP_HEARTBEAT_ACK:
2630 case SCTP_ABORT_ASSOCIATION:
2632 case SCTP_SHUTDOWN_ACK:
2633 case SCTP_OPERATION_ERROR:
2634 case SCTP_COOKIE_ECHO:
2635 case SCTP_COOKIE_ACK:
2638 case SCTP_SHUTDOWN_COMPLETE:
2639 case SCTP_AUTHENTICATION:
2640 case SCTP_ASCONF_ACK:
2641 case SCTP_PACKET_DROPPED:
2642 case SCTP_STREAM_RESET:
2643 case SCTP_FORWARD_CUM_TSN:
2646 * Now, what do we do with KNOWN chunks that
2647 * are NOT in the right place?
2649 * For now, I do nothing but ignore them. We
2650 * may later want to add sysctl stuff to
2651 * switch out and do either an ABORT() or
2652 * possibly process them.
2654 if (sctp_strict_data_order) {
2655 struct mbuf *op_err;
2657 op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2658 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0);
2663 /* unknown chunk type, use bit rules */
2664 if (ch->ch.chunk_type & 0x40) {
2665 /* Add a error report to the queue */
2667 struct sctp_paramhdr *phd;
2669 merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2671 phd = mtod(merr, struct sctp_paramhdr *);
2673 * We cheat and use param
2674 * type since we did not
2675 * bother to define a error
2676 * cause struct. They are
2677 * the same basic format
2678 * with different names.
2681 htons(SCTP_CAUSE_UNRECOG_CHUNK);
2683 htons(chk_length + sizeof(*phd));
2684 SCTP_BUF_LEN(merr) = sizeof(*phd);
2685 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2686 SCTP_SIZE32(chk_length),
2688 if (SCTP_BUF_NEXT(merr)) {
2689 sctp_queue_op_err(stcb, merr);
2695 if ((ch->ch.chunk_type & 0x80) == 0) {
2696 /* discard the rest of this packet */
2698 } /* else skip this bad chunk and
2701 }; /* switch of chunk type */
2703 *offset += SCTP_SIZE32(chk_length);
2704 if ((*offset >= length) || stop_proc) {
2705 /* no more data left in the mbuf chain */
2709 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2710 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2720 * we need to report rwnd overrun drops.
2722 sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2726 * Did we get data, if so update the time for auto-close and
2727 * give peer credit for being alive.
2729 SCTP_STAT_INCR(sctps_recvpktwithdata);
2730 stcb->asoc.overall_error_count = 0;
2731 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2733 /* now service all of the reassm queue if needed */
2734 if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2735 sctp_service_queues(stcb, asoc);
2737 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2738 /* Assure that we ack right away */
2739 stcb->asoc.send_sack = 1;
2741 /* Start a sack timer or QUEUE a SACK for sending */
2742 if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
2743 (stcb->asoc.mapping_array[0] != 0xff)) {
2744 if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
2745 (stcb->asoc.delayed_ack == 0) ||
2746 (stcb->asoc.send_sack == 1)) {
2747 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2748 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2750 sctp_send_sack(stcb);
2752 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2753 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2754 stcb->sctp_ep, stcb, NULL);
2758 sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2767 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2768 struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2769 uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2770 int num_seg, int *ecn_seg_sums)
2772 /************************************************/
2773 /* process fragments and update sendqueue */
2774 /************************************************/
2775 struct sctp_sack *sack;
2776 struct sctp_gap_ack_block *frag, block;
2777 struct sctp_tmit_chunk *tp1;
2782 uint16_t frag_strt, frag_end, primary_flag_set;
2783 u_long last_frag_high;
2786 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
2788 if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
2789 primary_flag_set = 1;
2791 primary_flag_set = 0;
2795 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2796 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2797 *offset += sizeof(block);
2803 for (i = 0; i < num_seg; i++) {
2804 frag_strt = ntohs(frag->start);
2805 frag_end = ntohs(frag->end);
2806 /* some sanity checks on the fargment offsets */
2807 if (frag_strt > frag_end) {
2808 /* this one is malformed, skip */
2812 if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
2814 *biggest_tsn_acked = frag_end + last_tsn;
2816 /* mark acked dgs and find out the highestTSN being acked */
2818 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2820 /* save the locations of the last frags */
2821 last_frag_high = frag_end + last_tsn;
2824 * now lets see if we need to reset the queue due to
2825 * a out-of-order SACK fragment
2827 if (compare_with_wrap(frag_strt + last_tsn,
2828 last_frag_high, MAX_TSN)) {
2830 * if the new frag starts after the last TSN
2831 * frag covered, we are ok and this one is
2832 * beyond the last one
2837 * ok, they have reset us, so we need to
2838 * reset the queue this will cause extra
2839 * hunting but hey, they chose the
2840 * performance hit when they failed to order
2843 tp1 = TAILQ_FIRST(&asoc->sent_queue);
2845 last_frag_high = frag_end + last_tsn;
2847 for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
2849 if (tp1->rec.data.doing_fast_retransmit)
2853 * CMT: CUCv2 algorithm. For each TSN being
2854 * processed from the sent queue, track the
2855 * next expected pseudo-cumack, or
2856 * rtx_pseudo_cumack, if required. Separate
2857 * cumack trackers for first transmissions,
2858 * and retransmissions.
2860 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2861 (tp1->snd_count == 1)) {
2862 tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2863 tp1->whoTo->find_pseudo_cumack = 0;
2865 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2866 (tp1->snd_count > 1)) {
2867 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2868 tp1->whoTo->find_rtx_pseudo_cumack = 0;
2870 if (tp1->rec.data.TSN_seq == j) {
2871 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2873 * must be held until
2877 * ECN Nonce: Add the nonce
2878 * value to the sender's
2881 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2883 * If it is less than RESEND, it is
2884 * now no-longer in flight.
2885 * Higher values may already be set
2886 * via previous Gap Ack Blocks...
2887 * i.e. ACKED or RESEND.
2889 if (compare_with_wrap(tp1->rec.data.TSN_seq,
2890 *biggest_newly_acked_tsn, MAX_TSN)) {
2891 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2900 * this_sack_highest_
2904 if (tp1->rec.data.chunk_was_revoked == 0)
2905 tp1->whoTo->saw_newack = 1;
2907 if (compare_with_wrap(tp1->rec.data.TSN_seq,
2908 tp1->whoTo->this_sack_highest_newack,
2910 tp1->whoTo->this_sack_highest_newack =
2911 tp1->rec.data.TSN_seq;
2916 * this_sack_lowest_n
2919 if (*this_sack_lowest_newack == 0) {
2920 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2921 sctp_log_sack(*this_sack_lowest_newack,
2923 tp1->rec.data.TSN_seq,
2926 SCTP_LOG_TSN_ACKED);
2928 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2933 * (rtx-)pseudo-cumac
2938 * (rtx-)pseudo-cumac
2940 * new_(rtx_)pseudo_c
2948 * (rtx-)pseudo-cumac
2956 if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2957 if (tp1->rec.data.chunk_was_revoked == 0) {
2958 tp1->whoTo->new_pseudo_cumack = 1;
2960 tp1->whoTo->find_pseudo_cumack = 1;
2962 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
2963 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2965 if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2966 if (tp1->rec.data.chunk_was_revoked == 0) {
2967 tp1->whoTo->new_pseudo_cumack = 1;
2969 tp1->whoTo->find_rtx_pseudo_cumack = 1;
2971 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2972 sctp_log_sack(*biggest_newly_acked_tsn,
2974 tp1->rec.data.TSN_seq,
2977 SCTP_LOG_TSN_ACKED);
2979 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
2980 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
2981 tp1->whoTo->flight_size,
2983 (uintptr_t) tp1->whoTo,
2984 tp1->rec.data.TSN_seq);
2986 sctp_flight_size_decrease(tp1);
2987 sctp_total_flight_decrease(stcb, tp1);
2989 tp1->whoTo->net_ack += tp1->send_size;
2990 if (tp1->snd_count < 2) {
2996 tp1->whoTo->net_ack2 += tp1->send_size;
3003 sctp_calculate_rto(stcb,
3006 &tp1->sent_rcv_time);
3011 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3012 (*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3013 (*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3014 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3015 asoc->this_sack_highest_gap,
3017 asoc->this_sack_highest_gap =
3018 tp1->rec.data.TSN_seq;
3020 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3021 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3022 #ifdef SCTP_AUDITING_ENABLED
3023 sctp_audit_log(0xB2,
3024 (asoc->sent_queue_retran_cnt & 0x000000ff));
3029 * All chunks NOT UNSENT
3030 * fall through here and are
3033 tp1->sent = SCTP_DATAGRAM_MARKED;
3034 if (tp1->rec.data.chunk_was_revoked) {
3035 /* deflate the cwnd */
3036 tp1->whoTo->cwnd -= tp1->book_size;
3037 tp1->rec.data.chunk_was_revoked = 0;
3041 } /* if (tp1->TSN_seq == j) */
3042 if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
3046 tp1 = TAILQ_NEXT(tp1, sctp_next);
3047 } /* end while (tp1) */
3048 } /* end for (j = fragStart */
3049 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3050 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3051 *offset += sizeof(block);
3056 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3058 sctp_log_fr(*biggest_tsn_acked,
3059 *biggest_newly_acked_tsn,
3060 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3065 sctp_check_for_revoked(struct sctp_tcb *stcb,
3066 struct sctp_association *asoc, uint32_t cumack,
3067 u_long biggest_tsn_acked)
3069 struct sctp_tmit_chunk *tp1;
3070 int tot_revoked = 0;
3072 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3074 if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3077 * ok this guy is either ACK or MARKED. If it is
3078 * ACKED it has been previously acked but not this
3079 * time i.e. revoked. If it is MARKED it was ACK'ed
3082 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3087 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3088 /* it has been revoked */
3089 tp1->sent = SCTP_DATAGRAM_SENT;
3090 tp1->rec.data.chunk_was_revoked = 1;
3092 * We must add this stuff back in to assure
3093 * timers and such get started.
3095 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3096 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3097 tp1->whoTo->flight_size,
3099 (uintptr_t) tp1->whoTo,
3100 tp1->rec.data.TSN_seq);
3102 sctp_flight_size_increase(tp1);
3103 sctp_total_flight_increase(stcb, tp1);
3105 * We inflate the cwnd to compensate for our
3106 * artificial inflation of the flight_size.
3108 tp1->whoTo->cwnd += tp1->book_size;
3110 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3111 sctp_log_sack(asoc->last_acked_seq,
3113 tp1->rec.data.TSN_seq,
3116 SCTP_LOG_TSN_REVOKED);
3118 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3119 /* it has been re-acked in this SACK */
3120 tp1->sent = SCTP_DATAGRAM_ACKED;
3123 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3125 tp1 = TAILQ_NEXT(tp1, sctp_next);
3127 if (tot_revoked > 0) {
3129 * Setup the ecn nonce re-sync point. We do this since once
3130 * data is revoked we begin to retransmit things, which do
3131 * NOT have the ECN bits set. This means we are now out of
3132 * sync and must wait until we get back in sync with the
3133 * peer to check ECN bits.
3135 tp1 = TAILQ_FIRST(&asoc->send_queue);
3137 asoc->nonce_resync_tsn = asoc->sending_seq;
3139 asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3141 asoc->nonce_wait_for_ecne = 0;
3142 asoc->nonce_sum_check = 0;
3147 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3148 u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3150 struct sctp_tmit_chunk *tp1;
3151 int strike_flag = 0;
3153 int tot_retrans = 0;
3154 uint32_t sending_seq;
3155 struct sctp_nets *net;
3156 int num_dests_sacked = 0;
3159 * select the sending_seq, this is either the next thing ready to be
3160 * sent but not transmitted, OR, the next seq we assign.
3162 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3164 sending_seq = asoc->sending_seq;
3166 sending_seq = tp1->rec.data.TSN_seq;
3169 /* CMT DAC algo: finding out if SACK is a mixed SACK */
3170 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3171 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3172 if (net->saw_newack)
3176 if (stcb->asoc.peer_supports_prsctp) {
3177 (void)SCTP_GETTIME_TIMEVAL(&now);
3179 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3182 if (tp1->no_fr_allowed) {
3183 /* this one had a timeout or something */
3184 tp1 = TAILQ_NEXT(tp1, sctp_next);
3187 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3188 if (tp1->sent < SCTP_DATAGRAM_RESEND)
3189 sctp_log_fr(biggest_tsn_newly_acked,
3190 tp1->rec.data.TSN_seq,
3192 SCTP_FR_LOG_CHECK_STRIKE);
3194 if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3196 tp1->sent == SCTP_DATAGRAM_UNSENT) {
3200 if (stcb->asoc.peer_supports_prsctp) {
3201 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3202 /* Is it expired? */
3204 (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3206 /* Yes so drop it */
3207 if (tp1->data != NULL) {
3208 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3209 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3212 tp1 = TAILQ_NEXT(tp1, sctp_next);
3216 if ((PR_SCTP_RTX_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3217 /* Has it been retransmitted tv_sec times? */
3218 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3219 /* Yes, so drop it */
3220 if (tp1->data != NULL) {
3221 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3222 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3225 tp1 = TAILQ_NEXT(tp1, sctp_next);
3230 if (compare_with_wrap(tp1->rec.data.TSN_seq,
3231 asoc->this_sack_highest_gap, MAX_TSN)) {
3232 /* we are beyond the tsn in the sack */
3235 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3236 /* either a RESEND, ACKED, or MARKED */
3238 tp1 = TAILQ_NEXT(tp1, sctp_next);
3242 * CMT : SFR algo (covers part of DAC and HTNA as well)
3244 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3246 * No new acks were receieved for data sent to this
3247 * dest. Therefore, according to the SFR algo for
3248 * CMT, no data sent to this dest can be marked for
3249 * FR using this SACK.
3251 tp1 = TAILQ_NEXT(tp1, sctp_next);
3253 } else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3254 tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3256 * CMT: New acks were receieved for data sent to
3257 * this dest. But no new acks were seen for data
3258 * sent after tp1. Therefore, according to the SFR
3259 * algo for CMT, tp1 cannot be marked for FR using
3260 * this SACK. This step covers part of the DAC algo
3261 * and the HTNA algo as well.
3263 tp1 = TAILQ_NEXT(tp1, sctp_next);
3267 * Here we check to see if we were have already done a FR
3268 * and if so we see if the biggest TSN we saw in the sack is
3269 * smaller than the recovery point. If so we don't strike
3270 * the tsn... otherwise we CAN strike the TSN.
3273 * @@@ JRI: Check for CMT if (accum_moved &&
3274 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3277 if (accum_moved && asoc->fast_retran_loss_recovery) {
3279 * Strike the TSN if in fast-recovery and cum-ack
3282 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3283 sctp_log_fr(biggest_tsn_newly_acked,
3284 tp1->rec.data.TSN_seq,
3286 SCTP_FR_LOG_STRIKE_CHUNK);
3288 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3291 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3293 * CMT DAC algorithm: If SACK flag is set to
3294 * 0, then lowest_newack test will not pass
3295 * because it would have been set to the
3296 * cumack earlier. If not already to be
3297 * rtx'd, If not a mixed sack and if tp1 is
3298 * not between two sacked TSNs, then mark by
3299 * one more. NOTE that we are marking by one
3300 * additional time since the SACK DAC flag
3301 * indicates that two packets have been
3302 * received after this missing TSN.
3304 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3305 compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3306 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3307 sctp_log_fr(16 + num_dests_sacked,
3308 tp1->rec.data.TSN_seq,
3310 SCTP_FR_LOG_STRIKE_CHUNK);
3315 } else if (tp1->rec.data.doing_fast_retransmit) {
3317 * For those that have done a FR we must take
3318 * special consideration if we strike. I.e the
3319 * biggest_newly_acked must be higher than the
3320 * sending_seq at the time we did the FR.
3323 #ifdef SCTP_FR_TO_ALTERNATE
3325 * If FR's go to new networks, then we must only do
3326 * this for singly homed asoc's. However if the FR's
3327 * go to the same network (Armando's work) then its
3328 * ok to FR multiple times.
3336 if ((compare_with_wrap(biggest_tsn_newly_acked,
3337 tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3338 (biggest_tsn_newly_acked ==
3339 tp1->rec.data.fast_retran_tsn)) {
3341 * Strike the TSN, since this ack is
3342 * beyond where things were when we
3345 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3346 sctp_log_fr(biggest_tsn_newly_acked,
3347 tp1->rec.data.TSN_seq,
3349 SCTP_FR_LOG_STRIKE_CHUNK);
3351 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3355 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3357 * CMT DAC algorithm: If
3358 * SACK flag is set to 0,
3359 * then lowest_newack test
3360 * will not pass because it
3361 * would have been set to
3362 * the cumack earlier. If
3363 * not already to be rtx'd,
3364 * If not a mixed sack and
3365 * if tp1 is not between two
3366 * sacked TSNs, then mark by
3367 * one more. NOTE that we
3368 * are marking by one
3369 * additional time since the
3370 * SACK DAC flag indicates
3371 * that two packets have
3372 * been received after this
3375 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3376 (num_dests_sacked == 1) &&
3377 compare_with_wrap(this_sack_lowest_newack,
3378 tp1->rec.data.TSN_seq, MAX_TSN)) {
3379 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3380 sctp_log_fr(32 + num_dests_sacked,
3381 tp1->rec.data.TSN_seq,
3383 SCTP_FR_LOG_STRIKE_CHUNK);
3385 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3394 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3397 } else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3398 biggest_tsn_newly_acked, MAX_TSN)) {
3400 * We don't strike these: This is the HTNA
3401 * algorithm i.e. we don't strike If our TSN is
3402 * larger than the Highest TSN Newly Acked.
3406 /* Strike the TSN */
3407 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3408 sctp_log_fr(biggest_tsn_newly_acked,
3409 tp1->rec.data.TSN_seq,
3411 SCTP_FR_LOG_STRIKE_CHUNK);
3413 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3416 if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3418 * CMT DAC algorithm: If SACK flag is set to
3419 * 0, then lowest_newack test will not pass
3420 * because it would have been set to the
3421 * cumack earlier. If not already to be
3422 * rtx'd, If not a mixed sack and if tp1 is
3423 * not between two sacked TSNs, then mark by
3424 * one more. NOTE that we are marking by one
3425 * additional time since the SACK DAC flag
3426 * indicates that two packets have been
3427 * received after this missing TSN.
3429 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3430 compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3431 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3432 sctp_log_fr(48 + num_dests_sacked,
3433 tp1->rec.data.TSN_seq,
3435 SCTP_FR_LOG_STRIKE_CHUNK);
3441 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3442 /* Increment the count to resend */
3443 struct sctp_nets *alt;
3445 /* printf("OK, we are now ready to FR this guy\n"); */
3446 if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3447 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3451 /* This is a subsequent FR */
3452 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3454 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3455 if (sctp_cmt_on_off) {
3457 * CMT: Using RTX_SSTHRESH policy for CMT.
3458 * If CMT is being used, then pick dest with
3459 * largest ssthresh for any retransmission.
3461 tp1->no_fr_allowed = 1;
3463 /* sa_ignore NO_NULL_CHK */
3464 alt = sctp_find_alternate_net(stcb, alt, 1);
3469 * CUCv2: If a different dest is picked for
3470 * the retransmission, then new
3471 * (rtx-)pseudo_cumack needs to be tracked
3472 * for orig dest. Let CUCv2 track new (rtx-)
3473 * pseudo-cumack always.
3476 tp1->whoTo->find_pseudo_cumack = 1;
3477 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3479 } else {/* CMT is OFF */
3481 #ifdef SCTP_FR_TO_ALTERNATE
3482 /* Can we find an alternate? */
3483 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3486 * default behavior is to NOT retransmit
3487 * FR's to an alternate. Armando Caro's
3488 * paper details why.
3494 tp1->rec.data.doing_fast_retransmit = 1;
3496 /* mark the sending seq for possible subsequent FR's */
3498 * printf("Marking TSN for FR new value %x\n",
3499 * (uint32_t)tpi->rec.data.TSN_seq);
3501 if (TAILQ_EMPTY(&asoc->send_queue)) {
3503 * If the queue of send is empty then its
3504 * the next sequence number that will be
3505 * assigned so we subtract one from this to
3506 * get the one we last sent.
3508 tp1->rec.data.fast_retran_tsn = sending_seq;
3511 * If there are chunks on the send queue
3512 * (unsent data that has made it from the
3513 * stream queues but not out the door, we
3514 * take the first one (which will have the
3515 * lowest TSN) and subtract one to get the
3518 struct sctp_tmit_chunk *ttt;
3520 ttt = TAILQ_FIRST(&asoc->send_queue);
3521 tp1->rec.data.fast_retran_tsn =
3522 ttt->rec.data.TSN_seq;
3527 * this guy had a RTO calculation pending on
3532 /* fix counts and things */
3533 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3534 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3535 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3537 (uintptr_t) tp1->whoTo,
3538 tp1->rec.data.TSN_seq);
3541 tp1->whoTo->net_ack++;
3542 sctp_flight_size_decrease(tp1);
3544 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
3545 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3546 asoc->peers_rwnd, tp1->send_size, sctp_peer_chunk_oh);
3548 /* add back to the rwnd */
3549 asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
3551 /* remove from the total flight */
3552 sctp_total_flight_decrease(stcb, tp1);
3553 if (alt != tp1->whoTo) {
3554 /* yes, there is an alternate. */
3555 sctp_free_remote_addr(tp1->whoTo);
3556 /* sa_ignore FREED_MEMORY */
3558 atomic_add_int(&alt->ref_count, 1);
3561 tp1 = TAILQ_NEXT(tp1, sctp_next);
3564 if (tot_retrans > 0) {
3566 * Setup the ecn nonce re-sync point. We do this since once
3567 * we go to FR something we introduce a Karn's rule scenario
3568 * and won't know the totals for the ECN bits.
3570 asoc->nonce_resync_tsn = sending_seq;
3571 asoc->nonce_wait_for_ecne = 0;
3572 asoc->nonce_sum_check = 0;
3576 struct sctp_tmit_chunk *
3577 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3578 struct sctp_association *asoc)
3580 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3584 if (asoc->peer_supports_prsctp == 0) {
3587 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3589 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3590 tp1->sent != SCTP_DATAGRAM_RESEND) {
3591 /* no chance to advance, out of here */
3594 if (!PR_SCTP_ENABLED(tp1->flags)) {
3596 * We can't fwd-tsn past any that are reliable aka
3597 * retransmitted until the asoc fails.
3602 (void)SCTP_GETTIME_TIMEVAL(&now);
3605 tp2 = TAILQ_NEXT(tp1, sctp_next);
3607 * now we got a chunk which is marked for another
3608 * retransmission to a PR-stream but has run out its chances
3609 * already maybe OR has been marked to skip now. Can we skip
3610 * it if its a resend?
3612 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3613 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3615 * Now is this one marked for resend and its time is
3618 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3619 /* Yes so drop it */
3621 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3622 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3627 * No, we are done when hit one for resend
3628 * whos time as not expired.
3634 * Ok now if this chunk is marked to drop it we can clean up
3635 * the chunk, advance our peer ack point and we can check
3638 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3639 /* advance PeerAckPoint goes forward */
3640 asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3643 * we don't want to de-queue it here. Just wait for
3644 * the next peer SACK to come with a new cumTSN and
3645 * then the chunk will be droped in the normal
3649 sctp_free_bufspace(stcb, asoc, tp1, 1);
3651 * Maybe there should be another
3654 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3655 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3657 sctp_m_freem(tp1->data);
3659 if (stcb->sctp_socket) {
3660 sctp_sowwakeup(stcb->sctp_ep,
3662 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
3663 sctp_wakeup_log(stcb, tp1->rec.data.TSN_seq, 1, SCTP_WAKESND_FROM_FWDTSN);
3669 * If it is still in RESEND we can advance no
3675 * If we hit here we just dumped tp1, move to next tsn on
3683 #ifdef SCTP_HIGH_SPEED
3684 struct sctp_hs_raise_drop {
3687 int32_t drop_percent;
3690 #define SCTP_HS_TABLE_SIZE 73
3692 struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
3693 {38, 1, 50}, /* 0 */
3694 {118, 2, 44}, /* 1 */
3695 {221, 3, 41}, /* 2 */
3696 {347, 4, 38}, /* 3 */
3697 {495, 5, 37}, /* 4 */
3698 {663, 6, 35}, /* 5 */
3699 {851, 7, 34}, /* 6 */
3700 {1058, 8, 33}, /* 7 */
3701 {1284, 9, 32}, /* 8 */
3702 {1529, 10, 31}, /* 9 */
3703 {1793, 11, 30}, /* 10 */
3704 {2076, 12, 29}, /* 11 */
3705 {2378, 13, 28}, /* 12 */
3706 {2699, 14, 28}, /* 13 */
3707 {3039, 15, 27}, /* 14 */
3708 {3399, 16, 27}, /* 15 */
3709 {3778, 17, 26}, /* 16 */
3710 {4177, 18, 26}, /* 17 */
3711 {4596, 19, 25}, /* 18 */
3712 {5036, 20, 25}, /* 19 */
3713 {5497, 21, 24}, /* 20 */
3714 {5979, 22, 24}, /* 21 */
3715 {6483, 23, 23}, /* 22 */
3716 {7009, 24, 23}, /* 23 */
3717 {7558, 25, 22}, /* 24 */
3718 {8130, 26, 22}, /* 25 */
3719 {8726, 27, 22}, /* 26 */
3720 {9346, 28, 21}, /* 27 */
3721 {9991, 29, 21}, /* 28 */
3722 {10661, 30, 21}, /* 29 */
3723 {11358, 31, 20}, /* 30 */
3724 {12082, 32, 20}, /* 31 */
3725 {12834, 33, 20}, /* 32 */
3726 {13614, 34, 19}, /* 33 */
3727 {14424, 35, 19}, /* 34 */
3728 {15265, 36, 19}, /* 35 */
3729 {16137, 37, 19}, /* 36 */
3730 {17042, 38, 18}, /* 37 */
3731 {17981, 39, 18}, /* 38 */
3732 {18955, 40, 18}, /* 39 */
3733 {19965, 41, 17}, /* 40 */
3734 {21013, 42, 17}, /* 41 */
3735 {22101, 43, 17}, /* 42 */
3736 {23230, 44, 17}, /* 43 */
3737 {24402, 45, 16}, /* 44 */
3738 {25618, 46, 16}, /* 45 */
3739 {26881, 47, 16}, /* 46 */
3740 {28193, 48, 16}, /* 47 */
3741 {29557, 49, 15}, /* 48 */
3742 {30975, 50, 15}, /* 49 */
3743 {32450, 51, 15}, /* 50 */
3744 {33986, 52, 15}, /* 51 */
3745 {35586, 53, 14}, /* 52 */
3746 {37253, 54, 14}, /* 53 */
3747 {38992, 55, 14}, /* 54 */
3748 {40808, 56, 14}, /* 55 */
3749 {42707, 57, 13}, /* 56 */
3750 {44694, 58, 13}, /* 57 */
3751 {46776, 59, 13}, /* 58 */
3752 {48961, 60, 13}, /* 59 */
3753 {51258, 61, 13}, /* 60 */
3754 {53677, 62, 12}, /* 61 */
3755 {56230, 63, 12}, /* 62 */
3756 {58932, 64, 12}, /* 63 */
3757 {61799, 65, 12}, /* 64 */
3758 {64851, 66, 11}, /* 65 */
3759 {68113, 67, 11}, /* 66 */
3760 {71617, 68, 11}, /* 67 */
3761 {75401, 69, 10}, /* 68 */
3762 {79517, 70, 10}, /* 69 */
3763 {84035, 71, 10}, /* 70 */
3764 {89053, 72, 10}, /* 71 */
3765 {94717, 73, 9} /* 72 */
3769 sctp_hs_cwnd_increase(struct sctp_tcb *stcb, struct sctp_nets *net)
3771 int cur_val, i, indx, incr;
3773 cur_val = net->cwnd >> 10;
3774 indx = SCTP_HS_TABLE_SIZE - 1;
3776 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3778 if (net->net_ack > net->mtu) {
3779 net->cwnd += net->mtu;
3780 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3781 sctp_log_cwnd(stcb, net, net->mtu, SCTP_CWND_LOG_FROM_SS);
3784 net->cwnd += net->net_ack;
3785 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3786 sctp_log_cwnd(stcb, net, net->net_ack, SCTP_CWND_LOG_FROM_SS);
3790 for (i = net->last_hs_used; i < SCTP_HS_TABLE_SIZE; i++) {
3791 if (cur_val < sctp_cwnd_adjust[i].cwnd) {
3796 net->last_hs_used = indx;
3797 incr = ((sctp_cwnd_adjust[indx].increase) << 10);
3799 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3800 sctp_log_cwnd(stcb, net, incr, SCTP_CWND_LOG_FROM_SS);
3806 sctp_hs_cwnd_decrease(struct sctp_tcb *stcb, struct sctp_nets *net)
3808 int cur_val, i, indx;
3809 int old_cwnd = net->cwnd;
3811 cur_val = net->cwnd >> 10;
3812 indx = net->last_hs_used;
3813 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3815 net->ssthresh = net->cwnd / 2;
3816 if (net->ssthresh < (net->mtu * 2)) {
3817 net->ssthresh = 2 * net->mtu;
3819 net->cwnd = net->ssthresh;
3821 /* drop by the proper amount */
3822 net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
3823 sctp_cwnd_adjust[net->last_hs_used].drop_percent);
3824 net->cwnd = net->ssthresh;
3825 /* now where are we */
3826 indx = net->last_hs_used;
3827 cur_val = net->cwnd >> 10;
3828 /* reset where we are in the table */
3829 if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3830 /* feel out of hs */
3831 net->last_hs_used = 0;
3833 for (i = indx; i >= 1; i--) {
3834 if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
3838 net->last_hs_used = indx;
3841 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3842 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_FR);
3850 sctp_cwnd_update(struct sctp_tcb *stcb,
3851 struct sctp_association *asoc,
3852 int accum_moved, int reneged_all, int will_exit)
3854 struct sctp_nets *net;
3856 /******************************/
3857 /* update cwnd and Early FR */
3858 /******************************/
3859 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3861 #ifdef JANA_CMT_FAST_RECOVERY
3863 * CMT fast recovery code. Need to debug.
3865 if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
3866 if (compare_with_wrap(asoc->last_acked_seq,
3867 net->fast_recovery_tsn, MAX_TSN) ||
3868 (asoc->last_acked_seq == net->fast_recovery_tsn) ||
3869 compare_with_wrap(net->pseudo_cumack, net->fast_recovery_tsn, MAX_TSN) ||
3870 (net->pseudo_cumack == net->fast_recovery_tsn)) {
3871 net->will_exit_fast_recovery = 1;
3875 if (sctp_early_fr) {
3877 * So, first of all do we need to have a Early FR
3880 if (((TAILQ_FIRST(&asoc->sent_queue)) &&
3881 (net->ref_count > 1) &&
3882 (net->flight_size < net->cwnd)) ||
3885 * yes, so in this case stop it if its
3886 * running, and then restart it. Reneging
3887 * all is a special case where we want to
3888 * run the Early FR timer and then force the
3889 * last few unacked to be sent, causing us
3890 * to illicit a sack with gaps to force out
3893 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
3894 SCTP_STAT_INCR(sctps_earlyfrstpidsck2);
3895 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
3896 SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
3898 SCTP_STAT_INCR(sctps_earlyfrstrid);
3899 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
3901 /* No, stop it if its running */
3902 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
3903 SCTP_STAT_INCR(sctps_earlyfrstpidsck3);
3904 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
3905 SCTP_FROM_SCTP_INDATA + SCTP_LOC_21);
3909 /* if nothing was acked on this destination skip it */
3910 if (net->net_ack == 0) {
3911 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
3912 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
3916 if (net->net_ack2 > 0) {
3918 * Karn's rule applies to clearing error count, this
3921 net->error_count = 0;
3922 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
3923 SCTP_ADDR_NOT_REACHABLE) {
3924 /* addr came good */
3925 net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
3926 net->dest_state |= SCTP_ADDR_REACHABLE;
3927 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
3928 SCTP_RECEIVED_SACK, (void *)net);
3929 /* now was it the primary? if so restore */
3930 if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
3931 (void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
3935 #ifdef JANA_CMT_FAST_RECOVERY
3937 * CMT fast recovery code
3940 * if (sctp_cmt_on_off == 1 &&
3941 * net->fast_retran_loss_recovery &&
3942 * net->will_exit_fast_recovery == 0) { // @@@ Do something
3943 * } else if (sctp_cmt_on_off == 0 &&
3944 * asoc->fast_retran_loss_recovery && will_exit == 0) {
3948 if (asoc->fast_retran_loss_recovery && will_exit == 0 && sctp_cmt_on_off == 0) {
3950 * If we are in loss recovery we skip any cwnd
3953 goto skip_cwnd_update;
3956 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
3959 if (accum_moved || (sctp_cmt_on_off && net->new_pseudo_cumack)) {
3960 /* If the cumulative ack moved we can proceed */
3961 if (net->cwnd <= net->ssthresh) {
3962 /* We are in slow start */
3963 if (net->flight_size + net->net_ack >=
3965 #ifdef SCTP_HIGH_SPEED
3966 sctp_hs_cwnd_increase(stcb, net);
3968 if (net->net_ack > (net->mtu * sctp_L2_abc_variable)) {
3969 net->cwnd += (net->mtu * sctp_L2_abc_variable);
3970 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
3971 sctp_log_cwnd(stcb, net, net->mtu,
3972 SCTP_CWND_LOG_FROM_SS);
3975 net->cwnd += net->net_ack;
3976 sctp_log_cwnd(stcb, net, net->net_ack,
3977 SCTP_CWND_LOG_FROM_SS);
3983 dif = net->cwnd - (net->flight_size +
3985 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
3986 sctp_log_cwnd(stcb, net, net->net_ack,
3987 SCTP_CWND_LOG_NOADV_SS);
3991 /* We are in congestion avoidance */
3992 if (net->flight_size + net->net_ack >=
3995 * add to pba only if we had a
3996 * cwnd's worth (or so) in flight OR
3997 * the burst limit was applied.
3999 net->partial_bytes_acked +=
4003 * Do we need to increase (if pba is
4006 if (net->partial_bytes_acked >=
4009 net->partial_bytes_acked) {
4010 net->partial_bytes_acked -=
4013 net->partial_bytes_acked =
4016 net->cwnd += net->mtu;
4017 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
4018 sctp_log_cwnd(stcb, net, net->mtu,
4019 SCTP_CWND_LOG_FROM_CA);
4022 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4023 sctp_log_cwnd(stcb, net, net->net_ack,
4024 SCTP_CWND_LOG_NOADV_CA);
4030 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4031 sctp_log_cwnd(stcb, net, net->net_ack,
4032 SCTP_CWND_LOG_NOADV_CA);
4034 dif = net->cwnd - (net->flight_size +
4039 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4040 sctp_log_cwnd(stcb, net, net->mtu,
4041 SCTP_CWND_LOG_NO_CUMACK);
4046 * NOW, according to Karn's rule do we need to restore the
4047 * RTO timer back? Check our net_ack2. If not set then we
4048 * have a ambiguity.. i.e. all data ack'd was sent to more
4051 if (net->net_ack2) {
4052 /* restore any doubled timers */
4053 net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
4054 if (net->RTO < stcb->asoc.minrto) {
4055 net->RTO = stcb->asoc.minrto;
4057 if (net->RTO > stcb->asoc.maxrto) {
4058 net->RTO = stcb->asoc.maxrto;
4065 sctp_fs_audit(struct sctp_association *asoc)
4067 struct sctp_tmit_chunk *chk;
4068 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
4070 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4071 if (chk->sent < SCTP_DATAGRAM_RESEND) {
4073 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4075 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4077 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4084 if ((inflight > 0) || (inbetween > 0)) {
4086 panic("Flight size-express incorrect? \n");
4088 SCTP_PRINTF("Flight size-express incorrect inflight:%d inbetween:%d\n",
4089 inflight, inbetween);
4096 sctp_window_probe_recovery(struct sctp_tcb *stcb,
4097 struct sctp_association *asoc,
4098 struct sctp_nets *net,
4099 struct sctp_tmit_chunk *tp1)
4101 struct sctp_tmit_chunk *chk;
4103 /* First setup this one and get it moved back */
4104 tp1->sent = SCTP_DATAGRAM_UNSENT;
4105 tp1->window_probe = 0;
4106 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4107 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4108 tp1->whoTo->flight_size,
4110 (uintptr_t) tp1->whoTo,
4111 tp1->rec.data.TSN_seq);
4113 sctp_flight_size_decrease(tp1);
4114 sctp_total_flight_decrease(stcb, tp1);
4115 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4116 TAILQ_INSERT_HEAD(&asoc->send_queue, tp1, sctp_next);
4117 asoc->sent_queue_cnt--;
4118 asoc->send_queue_cnt++;
4120 * Now all guys marked for RESEND on the sent_queue must be moved
4123 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4124 if (chk->sent == SCTP_DATAGRAM_RESEND) {
4125 /* Another chunk to move */
4126 chk->sent = SCTP_DATAGRAM_UNSENT;
4127 chk->window_probe = 0;
4128 /* It should not be in flight */
4129 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4130 TAILQ_INSERT_AFTER(&asoc->send_queue, tp1, chk, sctp_next);
4131 asoc->sent_queue_cnt--;
4132 asoc->send_queue_cnt++;
4133 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4140 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4141 uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4143 struct sctp_nets *net;
4144 struct sctp_association *asoc;
4145 struct sctp_tmit_chunk *tp1, *tp2;
4147 int win_probe_recovery = 0;
4148 int win_probe_recovered = 0;
4149 int j, done_once = 0;
4152 if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4153 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4154 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4156 SCTP_TCB_LOCK_ASSERT(stcb);
4158 old_rwnd = asoc->peers_rwnd;
4159 if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
4162 } else if (asoc->last_acked_seq == cumack) {
4163 /* Window update sack */
4164 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4165 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4166 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4167 /* SWS sender side engages */
4168 asoc->peers_rwnd = 0;
4170 if (asoc->peers_rwnd > old_rwnd) {
4176 /* First setup for CC stuff */
4177 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4178 net->prev_cwnd = net->cwnd;
4183 * CMT: Reset CUC and Fast recovery algo variables before
4186 net->new_pseudo_cumack = 0;
4187 net->will_exit_fast_recovery = 0;
4189 if (sctp_strict_sacks) {
4192 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4193 tp1 = TAILQ_LAST(&asoc->sent_queue,
4194 sctpchunk_listhead);
4195 send_s = tp1->rec.data.TSN_seq + 1;
4197 send_s = asoc->sending_seq;
4199 if ((cumack == send_s) ||
4200 compare_with_wrap(cumack, send_s, MAX_TSN)) {
4206 panic("Impossible sack 1");
4210 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4211 0, M_DONTWAIT, 1, MT_DATA);
4213 struct sctp_paramhdr *ph;
4216 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4218 ph = mtod(oper, struct sctp_paramhdr *);
4219 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4220 ph->param_length = htons(SCTP_BUF_LEN(oper));
4221 ippp = (uint32_t *) (ph + 1);
4222 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4224 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4225 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
4230 asoc->this_sack_highest_gap = cumack;
4231 stcb->asoc.overall_error_count = 0;
4232 if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4233 /* process the new consecutive TSN first */
4234 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4236 tp2 = TAILQ_NEXT(tp1, sctp_next);
4237 if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4239 cumack == tp1->rec.data.TSN_seq) {
4240 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4242 * ECN Nonce: Add the nonce to the
4243 * sender's nonce sum
4245 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4246 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4248 * If it is less than ACKED,
4249 * it is now no-longer in
4250 * flight. Higher values may
4251 * occur during marking
4253 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4254 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4255 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4256 tp1->whoTo->flight_size,
4258 (uintptr_t) tp1->whoTo,
4259 tp1->rec.data.TSN_seq);
4261 sctp_flight_size_decrease(tp1);
4262 sctp_total_flight_decrease(stcb, tp1);
4264 tp1->whoTo->net_ack += tp1->send_size;
4265 if (tp1->snd_count < 2) {
4271 tp1->whoTo->net_ack2 +=
4274 /* update RTO too? */
4277 sctp_calculate_rto(stcb,
4279 &tp1->sent_rcv_time);
4284 * CMT: CUCv2 algorithm.
4285 * From the cumack'd TSNs,
4286 * for each TSN being acked
4287 * for the first time, set
4288 * the following variables
4291 * new_pseudo_cumack will
4292 * trigger a cwnd update.
4293 * find_(rtx_)pseudo_cumack
4294 * will trigger search for
4296 * (rtx-)pseudo-cumack.
4298 tp1->whoTo->new_pseudo_cumack = 1;
4299 tp1->whoTo->find_pseudo_cumack = 1;
4300 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4302 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4303 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4306 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4307 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4309 if (tp1->rec.data.chunk_was_revoked) {
4310 /* deflate the cwnd */
4311 tp1->whoTo->cwnd -= tp1->book_size;
4312 tp1->rec.data.chunk_was_revoked = 0;
4314 tp1->sent = SCTP_DATAGRAM_ACKED;
4319 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4321 sctp_free_bufspace(stcb, asoc, tp1, 1);
4322 sctp_m_freem(tp1->data);
4324 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4325 sctp_log_sack(asoc->last_acked_seq,
4327 tp1->rec.data.TSN_seq,
4330 SCTP_LOG_FREE_SENT);
4333 asoc->sent_queue_cnt--;
4334 sctp_free_remote_addr(tp1->whoTo);
4335 sctp_free_a_chunk(stcb, tp1);
4339 if (stcb->sctp_socket) {
4340 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4341 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4342 sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4344 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4346 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4347 sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4352 if (asoc->last_acked_seq != cumack)
4353 sctp_cwnd_update(stcb, asoc, 1, 0, 0);
4355 asoc->last_acked_seq = cumack;
4357 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4358 /* nothing left in-flight */
4359 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4360 net->flight_size = 0;
4361 net->partial_bytes_acked = 0;
4363 asoc->total_flight = 0;
4364 asoc->total_flight_count = 0;
4366 /* Fix up the a-p-a-p for future PR-SCTP sends */
4367 if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4368 asoc->advanced_peer_ack_point = cumack;
4370 /* ECN Nonce updates */
4371 if (asoc->ecn_nonce_allowed) {
4372 if (asoc->nonce_sum_check) {
4373 if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4374 if (asoc->nonce_wait_for_ecne == 0) {
4375 struct sctp_tmit_chunk *lchk;
4377 lchk = TAILQ_FIRST(&asoc->send_queue);
4378 asoc->nonce_wait_for_ecne = 1;
4380 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4382 asoc->nonce_wait_tsn = asoc->sending_seq;
4385 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4386 (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4388 * Misbehaving peer. We need
4389 * to react to this guy
4391 asoc->ecn_allowed = 0;
4392 asoc->ecn_nonce_allowed = 0;
4397 /* See if Resynchronization Possible */
4398 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4399 asoc->nonce_sum_check = 1;
4401 * now we must calculate what the base is.
4402 * We do this based on two things, we know
4403 * the total's for all the segments
4404 * gap-acked in the SACK (none), We also
4405 * know the SACK's nonce sum, its in
4406 * nonce_sum_flag. So we can build a truth
4407 * table to back-calculate the new value of
4408 * asoc->nonce_sum_expect_base:
4410 * SACK-flag-Value Seg-Sums Base 0 0 0
4414 asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4419 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4420 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4421 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4422 /* SWS sender side engages */
4423 asoc->peers_rwnd = 0;
4425 if (asoc->peers_rwnd > old_rwnd) {
4426 win_probe_recovery = 1;
4428 /* Now assure a timer where data is queued at */
4431 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4432 if (win_probe_recovery && (net->window_probe)) {
4433 net->window_probe = 0;
4434 win_probe_recovered = 1;
4436 * Find first chunk that was used with window probe
4437 * and clear the sent
4439 /* sa_ignore FREED_MEMORY */
4440 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4441 if (tp1->window_probe) {
4442 /* move back to data send queue */
4443 sctp_window_probe_recovery(stcb, asoc, net, tp1);
4448 if (net->flight_size) {
4451 if (net->RTO == 0) {
4452 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4454 to_ticks = MSEC_TO_TICKS(net->RTO);
4457 (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4458 sctp_timeout_handler, &net->rxt_timer);
4460 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4461 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4463 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4465 if (sctp_early_fr) {
4466 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4467 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4468 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4469 SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4475 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4476 (asoc->sent_queue_retran_cnt == 0) &&
4477 (win_probe_recovered == 0) &&
4479 /* huh, this should not happen */
4480 sctp_fs_audit(asoc);
4481 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4482 net->flight_size = 0;
4484 asoc->total_flight = 0;
4485 asoc->total_flight_count = 0;
4486 asoc->sent_queue_retran_cnt = 0;
4487 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4488 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4489 sctp_flight_size_increase(tp1);
4490 sctp_total_flight_increase(stcb, tp1);
4491 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4492 asoc->sent_queue_retran_cnt++;
4498 /**********************************/
4499 /* Now what about shutdown issues */
4500 /**********************************/
4501 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4502 /* nothing left on sendqueue.. consider done */
4504 if ((asoc->stream_queue_cnt == 1) &&
4505 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4506 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4507 (asoc->locked_on_sending)
4509 struct sctp_stream_queue_pending *sp;
4512 * I may be in a state where we got all across.. but
4513 * cannot write more due to a shutdown... we abort
4514 * since the user did not indicate EOR in this case.
4515 * The sp will be cleaned during free of the asoc.
4517 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4519 if ((sp) && (sp->length == 0) && (sp->msg_is_complete == 0)) {
4520 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4521 asoc->locked_on_sending = NULL;
4522 asoc->stream_queue_cnt--;
4525 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4526 (asoc->stream_queue_cnt == 0)) {
4527 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4528 /* Need to abort here */
4534 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4535 0, M_DONTWAIT, 1, MT_DATA);
4537 struct sctp_paramhdr *ph;
4540 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4542 ph = mtod(oper, struct sctp_paramhdr *);
4543 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4544 ph->param_length = htons(SCTP_BUF_LEN(oper));
4545 ippp = (uint32_t *) (ph + 1);
4546 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4548 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4549 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper);
4551 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4552 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4553 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4555 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4556 sctp_stop_timers_for_shutdown(stcb);
4557 sctp_send_shutdown(stcb,
4558 stcb->asoc.primary_destination);
4559 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4560 stcb->sctp_ep, stcb, asoc->primary_destination);
4561 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4562 stcb->sctp_ep, stcb, asoc->primary_destination);
4564 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4565 (asoc->stream_queue_cnt == 0)) {
4566 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4569 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4570 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
4571 sctp_send_shutdown_ack(stcb,
4572 stcb->asoc.primary_destination);
4574 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4575 stcb->sctp_ep, stcb, asoc->primary_destination);
4578 if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
4579 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4581 stcb->asoc.peers_rwnd,
4582 stcb->asoc.total_flight,
4583 stcb->asoc.total_output_queue_size);
4590 sctp_handle_sack(struct mbuf *m, int offset,
4591 struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4592 struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4594 struct sctp_association *asoc;
4595 struct sctp_sack *sack;
4596 struct sctp_tmit_chunk *tp1, *tp2;
4597 uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4598 this_sack_lowest_newack;
4599 uint32_t sav_cum_ack;
4600 uint16_t num_seg, num_dup;
4601 uint16_t wake_him = 0;
4602 unsigned int sack_length;
4603 uint32_t send_s = 0;
4605 int accum_moved = 0;
4606 int will_exit_fast_recovery = 0;
4607 uint32_t a_rwnd, old_rwnd;
4608 int win_probe_recovery = 0;
4609 int win_probe_recovered = 0;
4610 struct sctp_nets *net = NULL;
4611 int nonce_sum_flag, ecn_seg_sums = 0;
4613 uint8_t reneged_all = 0;
4614 uint8_t cmt_dac_flag;
4617 * we take any chance we can to service our queues since we cannot
4618 * get awoken when the socket is read from :<
4621 * Now perform the actual SACK handling: 1) Verify that it is not an
4622 * old sack, if so discard. 2) If there is nothing left in the send
4623 * queue (cum-ack is equal to last acked) then you have a duplicate
4624 * too, update any rwnd change and verify no timers are running.
4625 * then return. 3) Process any new consequtive data i.e. cum-ack
4626 * moved process these first and note that it moved. 4) Process any
4627 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4628 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4629 * sync up flightsizes and things, stop all timers and also check
4630 * for shutdown_pending state. If so then go ahead and send off the
4631 * shutdown. If in shutdown recv, send off the shutdown-ack and
4632 * start that timer, Ret. 9) Strike any non-acked things and do FR
4633 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4634 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4635 * if in shutdown_recv state.
4637 SCTP_TCB_LOCK_ASSERT(stcb);
4640 this_sack_lowest_newack = 0;
4642 sack_length = (unsigned int)sack_len;
4644 SCTP_STAT_INCR(sctps_slowpath_sack);
4645 nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4646 cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4647 num_seg = ntohs(sack->num_gap_ack_blks);
4650 if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4651 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4652 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4655 cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4656 num_dup = ntohs(sack->num_dup_tsns);
4658 old_rwnd = stcb->asoc.peers_rwnd;
4659 stcb->asoc.overall_error_count = 0;
4661 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4662 sctp_log_sack(asoc->last_acked_seq,
4669 if ((num_dup) && (sctp_logging_level & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4670 int off_to_dup, iii;
4671 uint32_t *dupdata, dblock;
4673 off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4674 if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4675 dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4676 sizeof(uint32_t), (uint8_t *) & dblock);
4677 off_to_dup += sizeof(uint32_t);
4679 for (iii = 0; iii < num_dup; iii++) {
4680 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4681 dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4682 sizeof(uint32_t), (uint8_t *) & dblock);
4683 if (dupdata == NULL)
4685 off_to_dup += sizeof(uint32_t);
4691 SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4692 off_to_dup, num_dup, sack_length, num_seg);
4695 if (sctp_strict_sacks) {
4697 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4698 tp1 = TAILQ_LAST(&asoc->sent_queue,
4699 sctpchunk_listhead);
4700 send_s = tp1->rec.data.TSN_seq + 1;
4702 send_s = asoc->sending_seq;
4704 if (cum_ack == send_s ||
4705 compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4712 panic("Impossible sack 1");
4717 * no way, we have not even sent this TSN out yet.
4718 * Peer is hopelessly messed up with us.
4723 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4724 0, M_DONTWAIT, 1, MT_DATA);
4726 struct sctp_paramhdr *ph;
4729 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4731 ph = mtod(oper, struct sctp_paramhdr *);
4732 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4733 ph->param_length = htons(SCTP_BUF_LEN(oper));
4734 ippp = (uint32_t *) (ph + 1);
4735 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4737 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4738 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
4743 /**********************/
4744 /* 1) check the range */
4745 /**********************/
4746 if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4747 /* acking something behind */
4750 sav_cum_ack = asoc->last_acked_seq;
4752 /* update the Rwnd of the peer */
4753 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4754 TAILQ_EMPTY(&asoc->send_queue) &&
4755 (asoc->stream_queue_cnt == 0)
4757 /* nothing left on send/sent and strmq */
4758 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4759 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4760 asoc->peers_rwnd, 0, 0, a_rwnd);
4762 asoc->peers_rwnd = a_rwnd;
4763 if (asoc->sent_queue_retran_cnt) {
4764 asoc->sent_queue_retran_cnt = 0;
4766 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4767 /* SWS sender side engages */
4768 asoc->peers_rwnd = 0;
4770 /* stop any timers */
4771 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4772 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4773 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4774 if (sctp_early_fr) {
4775 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4776 SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4777 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4778 SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4781 net->partial_bytes_acked = 0;
4782 net->flight_size = 0;
4784 asoc->total_flight = 0;
4785 asoc->total_flight_count = 0;
4789 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4790 * things. The total byte count acked is tracked in netAckSz AND
4791 * netAck2 is used to track the total bytes acked that are un-
4792 * amibguious and were never retransmitted. We track these on a per
4793 * destination address basis.
4795 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4796 net->prev_cwnd = net->cwnd;
4801 * CMT: Reset CUC and Fast recovery algo variables before
4804 net->new_pseudo_cumack = 0;
4805 net->will_exit_fast_recovery = 0;
4807 /* process the new consecutive TSN first */
4808 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4810 if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4812 last_tsn == tp1->rec.data.TSN_seq) {
4813 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4815 * ECN Nonce: Add the nonce to the sender's
4818 asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4820 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4822 * If it is less than ACKED, it is
4823 * now no-longer in flight. Higher
4824 * values may occur during marking
4826 if ((tp1->whoTo->dest_state &
4827 SCTP_ADDR_UNCONFIRMED) &&
4828 (tp1->snd_count < 2)) {
4830 * If there was no retran
4831 * and the address is
4832 * un-confirmed and we sent
4834 * sacked.. its confirmed,
4837 tp1->whoTo->dest_state &=
4838 ~SCTP_ADDR_UNCONFIRMED;
4840 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4841 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4842 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4843 tp1->whoTo->flight_size,
4845 (uintptr_t) tp1->whoTo,
4846 tp1->rec.data.TSN_seq);
4848 sctp_flight_size_decrease(tp1);
4849 sctp_total_flight_decrease(stcb, tp1);
4851 tp1->whoTo->net_ack += tp1->send_size;
4853 /* CMT SFR and DAC algos */
4854 this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4855 tp1->whoTo->saw_newack = 1;
4857 if (tp1->snd_count < 2) {
4859 * True non-retransmited
4862 tp1->whoTo->net_ack2 +=
4865 /* update RTO too? */
4868 sctp_calculate_rto(stcb,
4870 &tp1->sent_rcv_time);
4875 * CMT: CUCv2 algorithm. From the
4876 * cumack'd TSNs, for each TSN being
4877 * acked for the first time, set the
4878 * following variables for the
4879 * corresp destination.
4880 * new_pseudo_cumack will trigger a
4882 * find_(rtx_)pseudo_cumack will
4883 * trigger search for the next
4884 * expected (rtx-)pseudo-cumack.
4886 tp1->whoTo->new_pseudo_cumack = 1;
4887 tp1->whoTo->find_pseudo_cumack = 1;
4888 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4891 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4892 sctp_log_sack(asoc->last_acked_seq,
4894 tp1->rec.data.TSN_seq,
4897 SCTP_LOG_TSN_ACKED);
4899 if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4900 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4903 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4904 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4905 #ifdef SCTP_AUDITING_ENABLED
4906 sctp_audit_log(0xB3,
4907 (asoc->sent_queue_retran_cnt & 0x000000ff));
4910 if (tp1->rec.data.chunk_was_revoked) {
4911 /* deflate the cwnd */
4912 tp1->whoTo->cwnd -= tp1->book_size;
4913 tp1->rec.data.chunk_was_revoked = 0;
4915 tp1->sent = SCTP_DATAGRAM_ACKED;
4920 tp1 = TAILQ_NEXT(tp1, sctp_next);
4922 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4923 /* always set this up to cum-ack */
4924 asoc->this_sack_highest_gap = last_tsn;
4926 /* Move offset up to point to gaps/dups */
4927 offset += sizeof(struct sctp_sack_chunk);
4928 if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
4930 /* skip corrupt segments */
4936 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4937 * to be greater than the cumack. Also reset saw_newack to 0
4940 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4941 net->saw_newack = 0;
4942 net->this_sack_highest_newack = last_tsn;
4946 * thisSackHighestGap will increase while handling NEW
4947 * segments this_sack_highest_newack will increase while
4948 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4949 * used for CMT DAC algo. saw_newack will also change.
4951 sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
4952 &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4953 num_seg, &ecn_seg_sums);
4955 if (sctp_strict_sacks) {
4957 * validate the biggest_tsn_acked in the gap acks if
4958 * strict adherence is wanted.
4960 if ((biggest_tsn_acked == send_s) ||
4961 (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4963 * peer is either confused or we are under
4964 * attack. We must abort.
4971 /*******************************************/
4972 /* cancel ALL T3-send timer if accum moved */
4973 /*******************************************/
4974 if (sctp_cmt_on_off) {
4975 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4976 if (net->new_pseudo_cumack)
4977 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4979 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4984 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4985 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4986 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4990 /********************************************/
4991 /* drop the acked chunks from the sendqueue */
4992 /********************************************/
4993 asoc->last_acked_seq = cum_ack;
4995 tp1 = TAILQ_FIRST(&asoc->sent_queue);
4999 if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5003 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5004 /* no more sent on list */
5007 tp2 = TAILQ_NEXT(tp1, sctp_next);
5008 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5010 * Friendlier printf in lieu of panic now that I think its
5014 if (tp1->pr_sctp_on) {
5015 if (asoc->pr_sctp_cnt != 0)
5016 asoc->pr_sctp_cnt--;
5018 if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5019 (asoc->total_flight > 0)) {
5021 panic("Warning flight size is postive and should be 0");
5023 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5024 asoc->total_flight);
5026 asoc->total_flight = 0;
5029 sctp_free_bufspace(stcb, asoc, tp1, 1);
5030 sctp_m_freem(tp1->data);
5031 if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5032 asoc->sent_queue_cnt_removeable--;
5035 if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
5036 sctp_log_sack(asoc->last_acked_seq,
5038 tp1->rec.data.TSN_seq,
5041 SCTP_LOG_FREE_SENT);
5044 asoc->sent_queue_cnt--;
5045 sctp_free_remote_addr(tp1->whoTo);
5047 sctp_free_a_chunk(stcb, tp1);
5050 } while (tp1 != NULL);
5053 if ((wake_him) && (stcb->sctp_socket)) {
5054 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5055 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
5056 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
5058 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5060 if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
5061 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
5065 if (asoc->fast_retran_loss_recovery && accum_moved) {
5066 if (compare_with_wrap(asoc->last_acked_seq,
5067 asoc->fast_recovery_tsn, MAX_TSN) ||
5068 asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5069 /* Setup so we will exit RFC2582 fast recovery */
5070 will_exit_fast_recovery = 1;
5074 * Check for revoked fragments:
5076 * if Previous sack - Had no frags then we can't have any revoked if
5077 * Previous sack - Had frag's then - If we now have frags aka
5078 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5079 * some of them. else - The peer revoked all ACKED fragments, since
5080 * we had some before and now we have NONE.
5084 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5085 else if (asoc->saw_sack_with_frags) {
5086 int cnt_revoked = 0;
5088 tp1 = TAILQ_FIRST(&asoc->sent_queue);
5090 /* Peer revoked all dg's marked or acked */
5091 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5092 if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5093 (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5094 tp1->sent = SCTP_DATAGRAM_SENT;
5095 if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
5096 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5097 tp1->whoTo->flight_size,
5099 (uintptr_t) tp1->whoTo,
5100 tp1->rec.data.TSN_seq);
5102 sctp_flight_size_increase(tp1);
5103 sctp_total_flight_increase(stcb, tp1);
5104 tp1->rec.data.chunk_was_revoked = 1;
5106 * To ensure that this increase in
5107 * flightsize, which is artificial,
5108 * does not throttle the sender, we
5109 * also increase the cwnd
5112 tp1->whoTo->cwnd += tp1->book_size;
5120 asoc->saw_sack_with_frags = 0;
5123 asoc->saw_sack_with_frags = 1;
5125 asoc->saw_sack_with_frags = 0;
5128 sctp_cwnd_update(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5130 if (TAILQ_EMPTY(&asoc->sent_queue)) {
5131 /* nothing left in-flight */
5132 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5133 /* stop all timers */
5134 if (sctp_early_fr) {
5135 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5136 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5137 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5138 SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5141 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5142 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5143 net->flight_size = 0;
5144 net->partial_bytes_acked = 0;
5146 asoc->total_flight = 0;
5147 asoc->total_flight_count = 0;
5149 /**********************************/
5150 /* Now what about shutdown issues */
5151 /**********************************/
5152 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5153 /* nothing left on sendqueue.. consider done */
5154 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5155 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5156 asoc->peers_rwnd, 0, 0, a_rwnd);
5158 asoc->peers_rwnd = a_rwnd;
5159 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5160 /* SWS sender side engages */
5161 asoc->peers_rwnd = 0;
5164 if ((asoc->stream_queue_cnt == 1) &&
5165 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5166 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5167 (asoc->locked_on_sending)
5169 struct sctp_stream_queue_pending *sp;
5172 * I may be in a state where we got all across.. but
5173 * cannot write more due to a shutdown... we abort
5174 * since the user did not indicate EOR in this case.
5176 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5178 if ((sp) && (sp->length == 0) && (sp->msg_is_complete == 0)) {
5179 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5180 asoc->locked_on_sending = NULL;
5181 asoc->stream_queue_cnt--;
5184 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5185 (asoc->stream_queue_cnt == 0)) {
5186 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5187 /* Need to abort here */
5193 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5194 0, M_DONTWAIT, 1, MT_DATA);
5196 struct sctp_paramhdr *ph;
5199 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5201 ph = mtod(oper, struct sctp_paramhdr *);
5202 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5203 ph->param_length = htons(SCTP_BUF_LEN(oper));
5204 ippp = (uint32_t *) (ph + 1);
5205 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5207 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5208 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper);
5211 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5212 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5213 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5215 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
5216 sctp_stop_timers_for_shutdown(stcb);
5217 sctp_send_shutdown(stcb,
5218 stcb->asoc.primary_destination);
5219 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5220 stcb->sctp_ep, stcb, asoc->primary_destination);
5221 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5222 stcb->sctp_ep, stcb, asoc->primary_destination);
5225 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5226 (asoc->stream_queue_cnt == 0)) {
5227 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5230 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5231 asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
5232 sctp_send_shutdown_ack(stcb,
5233 stcb->asoc.primary_destination);
5235 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5236 stcb->sctp_ep, stcb, asoc->primary_destination);
5241 * Now here we are going to recycle net_ack for a different use...
5244 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5249 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5250 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5251 * automatically ensure that.
5253 if (sctp_cmt_on_off && sctp_cmt_use_dac && (cmt_dac_flag == 0)) {
5254 this_sack_lowest_newack = cum_ack;
5257 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5258 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5260 /*********************************************/
5261 /* Here we perform PR-SCTP procedures */
5263 /*********************************************/
5264 /* C1. update advancedPeerAckPoint */
5265 if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5266 asoc->advanced_peer_ack_point = cum_ack;
5268 /* C2. try to further move advancedPeerAckPoint ahead */
5270 if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5271 struct sctp_tmit_chunk *lchk;
5273 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5274 /* C3. See if we need to send a Fwd-TSN */
5275 if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5278 * ISSUE with ECN, see FWD-TSN processing for notes
5279 * on issues that will occur when the ECN NONCE
5280 * stuff is put into SCTP for cross checking.
5282 send_forward_tsn(stcb, asoc);
5285 * ECN Nonce: Disable Nonce Sum check when FWD TSN
5286 * is sent and store resync tsn
5288 asoc->nonce_sum_check = 0;
5289 asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5291 /* Assure a timer is up */
5292 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5293 stcb->sctp_ep, stcb, lchk->whoTo);
5298 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off == 1) &&
5299 * (net->fast_retran_loss_recovery == 0)))
5301 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5302 if ((asoc->fast_retran_loss_recovery == 0) || (sctp_cmt_on_off == 1)) {
5303 /* out of a RFC2582 Fast recovery window? */
5304 if (net->net_ack > 0) {
5306 * per section 7.2.3, are there any
5307 * destinations that had a fast retransmit
5308 * to them. If so what we need to do is
5309 * adjust ssthresh and cwnd.
5311 struct sctp_tmit_chunk *lchk;
5313 #ifdef SCTP_HIGH_SPEED
5314 sctp_hs_cwnd_decrease(stcb, net);
5316 int old_cwnd = net->cwnd;
5318 net->ssthresh = net->cwnd / 2;
5319 if (net->ssthresh < (net->mtu * 2)) {
5320 net->ssthresh = 2 * net->mtu;
5322 net->cwnd = net->ssthresh;
5323 if (sctp_logging_level & SCTP_CWND_MONITOR_ENABLE) {
5324 sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
5325 SCTP_CWND_LOG_FROM_FR);
5329 lchk = TAILQ_FIRST(&asoc->send_queue);
5331 net->partial_bytes_acked = 0;
5332 /* Turn on fast recovery window */
5333 asoc->fast_retran_loss_recovery = 1;
5335 /* Mark end of the window */
5336 asoc->fast_recovery_tsn = asoc->sending_seq - 1;
5338 asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
5342 * CMT fast recovery -- per destination
5343 * recovery variable.
5345 net->fast_retran_loss_recovery = 1;
5348 /* Mark end of the window */
5349 net->fast_recovery_tsn = asoc->sending_seq - 1;
5351 net->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
5357 * Disable Nonce Sum Checking and store the
5360 asoc->nonce_sum_check = 0;
5361 asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
5363 sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
5364 stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
5365 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5366 stcb->sctp_ep, stcb, net);
5368 } else if (net->net_ack > 0) {
5370 * Mark a peg that we WOULD have done a cwnd
5371 * reduction but RFC2582 prevented this action.
5373 SCTP_STAT_INCR(sctps_fastretransinrtt);
5378 /******************************************************************
5379 * Here we do the stuff with ECN Nonce checking.
5380 * We basically check to see if the nonce sum flag was incorrect
5381 * or if resynchronization needs to be done. Also if we catch a
5382 * misbehaving receiver we give him the kick.
5383 ******************************************************************/
5385 if (asoc->ecn_nonce_allowed) {
5386 if (asoc->nonce_sum_check) {
5387 if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5388 if (asoc->nonce_wait_for_ecne == 0) {
5389 struct sctp_tmit_chunk *lchk;
5391 lchk = TAILQ_FIRST(&asoc->send_queue);
5392 asoc->nonce_wait_for_ecne = 1;
5394 asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5396 asoc->nonce_wait_tsn = asoc->sending_seq;
5399 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5400 (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5402 * Misbehaving peer. We need
5403 * to react to this guy
5405 asoc->ecn_allowed = 0;
5406 asoc->ecn_nonce_allowed = 0;
5411 /* See if Resynchronization Possible */
5412 if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5413 asoc->nonce_sum_check = 1;
5415 * now we must calculate what the base is.
5416 * We do this based on two things, we know
5417 * the total's for all the segments
5418 * gap-acked in the SACK, its stored in
5419 * ecn_seg_sums. We also know the SACK's
5420 * nonce sum, its in nonce_sum_flag. So we
5421 * can build a truth table to back-calculate
5423 * asoc->nonce_sum_expect_base:
5425 * SACK-flag-Value Seg-Sums Base 0 0 0
5429 asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5433 /* Now are we exiting loss recovery ? */
5434 if (will_exit_fast_recovery) {
5435 /* Ok, we must exit fast recovery */
5436 asoc->fast_retran_loss_recovery = 0;
5438 if ((asoc->sat_t3_loss_recovery) &&
5439 ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5441 (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5442 /* end satellite t3 loss recovery */
5443 asoc->sat_t3_loss_recovery = 0;
5448 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5449 if (net->will_exit_fast_recovery) {
5450 /* Ok, we must exit fast recovery */
5451 net->fast_retran_loss_recovery = 0;
5455 /* Adjust and set the new rwnd value */
5456 if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5457 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5458 asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
5460 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5461 (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
5462 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5463 /* SWS sender side engages */
5464 asoc->peers_rwnd = 0;
5466 if (asoc->peers_rwnd > old_rwnd) {
5467 win_probe_recovery = 1;
5470 * Now we must setup so we have a timer up for anyone with
5476 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5477 if (win_probe_recovery && (net->window_probe)) {
5478 net->window_probe = 0;
5479 win_probe_recovered = 1;
5481 * Find first chunk that was used with
5482 * window probe and clear the event. Put
5483 * it back into the send queue as if has
5486 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5487 if (tp1->window_probe) {
5488 sctp_window_probe_recovery(stcb, asoc, net, tp1);
5493 if (net->flight_size) {
5495 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5496 stcb->sctp_ep, stcb, net);
5498 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5499 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5501 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5503 if (sctp_early_fr) {
5504 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5505 SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5506 sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5507 SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5513 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5514 (asoc->sent_queue_retran_cnt == 0) &&
5515 (win_probe_recovered == 0) &&
5517 /* huh, this should not happen */
5518 sctp_fs_audit(asoc);
5519 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5520 net->flight_size = 0;
5522 asoc->total_flight = 0;
5523 asoc->total_flight_count = 0;
5524 asoc->sent_queue_retran_cnt = 0;
5525 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5526 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5527 sctp_flight_size_increase(tp1);
5528 sctp_total_flight_increase(stcb, tp1);
5529 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5530 asoc->sent_queue_retran_cnt++;
5536 if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
5537 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5539 stcb->asoc.peers_rwnd,
5540 stcb->asoc.total_flight,
5541 stcb->asoc.total_output_queue_size);
5546 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5547 struct sctp_nets *netp, int *abort_flag)
5550 uint32_t cum_ack, a_rwnd;
5552 cum_ack = ntohl(cp->cumulative_tsn_ack);
5553 /* Arrange so a_rwnd does NOT change */
5554 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5556 /* Now call the express sack handling */
5557 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5561 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5562 struct sctp_stream_in *strmin)
5564 struct sctp_queued_to_read *ctl, *nctl;
5565 struct sctp_association *asoc;
5569 tt = strmin->last_sequence_delivered;
5571 * First deliver anything prior to and including the stream no that
5574 ctl = TAILQ_FIRST(&strmin->inqueue);
5576 nctl = TAILQ_NEXT(ctl, next);
5577 if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5578 (tt == ctl->sinfo_ssn)) {
5579 /* this is deliverable now */
5580 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5581 /* subtract pending on streams */
5582 asoc->size_on_all_streams -= ctl->length;
5583 sctp_ucount_decr(asoc->cnt_on_all_streams);
5584 /* deliver it to at least the delivery-q */
5585 if (stcb->sctp_socket) {
5586 sctp_add_to_readq(stcb->sctp_ep, stcb,
5588 &stcb->sctp_socket->so_rcv, 1);
5591 /* no more delivery now. */
5597 * now we must deliver things in queue the normal way if any are
5600 tt = strmin->last_sequence_delivered + 1;
5601 ctl = TAILQ_FIRST(&strmin->inqueue);
5603 nctl = TAILQ_NEXT(ctl, next);
5604 if (tt == ctl->sinfo_ssn) {
5605 /* this is deliverable now */
5606 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5607 /* subtract pending on streams */
5608 asoc->size_on_all_streams -= ctl->length;
5609 sctp_ucount_decr(asoc->cnt_on_all_streams);
5610 /* deliver it to at least the delivery-q */
5611 strmin->last_sequence_delivered = ctl->sinfo_ssn;
5612 if (stcb->sctp_socket) {
5613 sctp_add_to_readq(stcb->sctp_ep, stcb,
5615 &stcb->sctp_socket->so_rcv, 1);
5617 tt = strmin->last_sequence_delivered + 1;
5626 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5627 struct sctp_forward_tsn_chunk *fwd, int *abort_flag)
5630 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5631 * forward TSN, when the SACK comes back that acknowledges the
5632 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5633 * get quite tricky since we may have sent more data interveneing
5634 * and must carefully account for what the SACK says on the nonce
5635 * and any gaps that are reported. This work will NOT be done here,
5636 * but I note it here since it is really related to PR-SCTP and
5640 /* The pr-sctp fwd tsn */
5642 * here we will perform all the data receiver side steps for
5643 * processing FwdTSN, as required in by pr-sctp draft:
5645 * Assume we get FwdTSN(x):
5647 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5648 * others we have 3) examine and update re-ordering queue on
5649 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5650 * report where we are.
5652 struct sctp_strseq *stseq;
5653 struct sctp_association *asoc;
5654 uint32_t new_cum_tsn, gap, back_out_htsn;
5655 unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
5656 struct sctp_stream_in *strm;
5657 struct sctp_tmit_chunk *chk, *at;
5659 cumack_set_flag = 0;
5662 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5663 SCTPDBG(SCTP_DEBUG_INDATA1,
5664 "Bad size too small/big fwd-tsn\n");
5667 m_size = (stcb->asoc.mapping_array_size << 3);
5668 /*************************************************************/
5669 /* 1. Here we update local cumTSN and shift the bitmap array */
5670 /*************************************************************/
5671 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5673 if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5674 asoc->cumulative_tsn == new_cum_tsn) {
5675 /* Already got there ... */
5678 back_out_htsn = asoc->highest_tsn_inside_map;
5679 if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5681 asoc->highest_tsn_inside_map = new_cum_tsn;
5682 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5683 sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5687 * now we know the new TSN is more advanced, let's find the actual
5690 if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
5692 (new_cum_tsn == asoc->mapping_array_base_tsn)) {
5693 gap = new_cum_tsn - asoc->mapping_array_base_tsn;
5695 /* try to prevent underflow here */
5696 gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5700 asoc->highest_tsn_inside_map = back_out_htsn;
5701 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5705 * out of range (of single byte chunks in the rwnd I
5706 * give out). This must be an attacker.
5709 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5710 0, M_DONTWAIT, 1, MT_DATA);
5712 struct sctp_paramhdr *ph;
5715 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5716 (sizeof(uint32_t) * 3);
5717 ph = mtod(oper, struct sctp_paramhdr *);
5718 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5719 ph->param_length = htons(SCTP_BUF_LEN(oper));
5720 ippp = (uint32_t *) (ph + 1);
5721 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5723 *ippp = asoc->highest_tsn_inside_map;
5725 *ippp = new_cum_tsn;
5727 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5728 sctp_abort_an_association(stcb->sctp_ep, stcb,
5729 SCTP_PEER_FAULTY, oper);
5732 if (asoc->highest_tsn_inside_map >
5733 asoc->mapping_array_base_tsn) {
5734 gap = asoc->highest_tsn_inside_map -
5735 asoc->mapping_array_base_tsn;
5737 gap = asoc->highest_tsn_inside_map +
5738 (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5740 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5741 cumack_set_flag = 1;
5743 SCTP_TCB_LOCK_ASSERT(stcb);
5744 for (i = 0; i <= gap; i++) {
5745 SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
5748 * Now after marking all, slide thing forward but no sack please.
5750 sctp_sack_check(stcb, 0, 0, abort_flag);
5754 if (cumack_set_flag) {
5756 * fwd-tsn went outside my gap array - not a common
5757 * occurance. Do the same thing we do when a cookie-echo
5760 asoc->highest_tsn_inside_map = new_cum_tsn - 1;
5761 asoc->mapping_array_base_tsn = new_cum_tsn;
5762 asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
5763 if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5764 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5766 asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
5768 /*************************************************************/
5769 /* 2. Clear up re-assembly queue */
5770 /*************************************************************/
5773 * First service it if pd-api is up, just in case we can progress it
5776 if (asoc->fragmented_delivery_inprogress) {
5777 sctp_service_reassembly(stcb, asoc);
5779 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5780 /* For each one on here see if we need to toss it */
5782 * For now large messages held on the reasmqueue that are
5783 * complete will be tossed too. We could in theory do more
5784 * work to spin through and stop after dumping one msg aka
5785 * seeing the start of a new msg at the head, and call the
5786 * delivery function... to see if it can be delivered... But
5787 * for now we just dump everything on the queue.
5789 chk = TAILQ_FIRST(&asoc->reasmqueue);
5791 at = TAILQ_NEXT(chk, sctp_next);
5792 if (compare_with_wrap(asoc->cumulative_tsn,
5793 chk->rec.data.TSN_seq, MAX_TSN) ||
5794 asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
5795 /* It needs to be tossed */
5796 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5797 if (compare_with_wrap(chk->rec.data.TSN_seq,
5798 asoc->tsn_last_delivered, MAX_TSN)) {
5799 asoc->tsn_last_delivered =
5800 chk->rec.data.TSN_seq;
5801 asoc->str_of_pdapi =
5802 chk->rec.data.stream_number;
5803 asoc->ssn_of_pdapi =
5804 chk->rec.data.stream_seq;
5805 asoc->fragment_flags =
5806 chk->rec.data.rcv_flags;
5808 asoc->size_on_reasm_queue -= chk->send_size;
5809 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5812 /* Clear up any stream problem */
5813 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5814 SCTP_DATA_UNORDERED &&
5815 (compare_with_wrap(chk->rec.data.stream_seq,
5816 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5819 * We must dump forward this streams
5820 * sequence number if the chunk is
5821 * not unordered that is being
5822 * skipped. There is a chance that
5823 * if the peer does not include the
5824 * last fragment in its FWD-TSN we
5825 * WILL have a problem here since
5826 * you would have a partial chunk in
5827 * queue that may not be
5828 * deliverable. Also if a Partial
5829 * delivery API as started the user
5830 * may get a partial chunk. The next
5831 * read returning a new chunk...
5832 * really ugly but I see no way
5833 * around it! Maybe a notify??
5835 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5836 chk->rec.data.stream_seq;
5839 sctp_m_freem(chk->data);
5842 sctp_free_remote_addr(chk->whoTo);
5843 sctp_free_a_chunk(stcb, chk);
5846 * Ok we have gone beyond the end of the
5847 * fwd-tsn's mark. Some checks...
5849 if ((asoc->fragmented_delivery_inprogress) &&
5850 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5854 * Special case PD-API is up and
5855 * what we fwd-tsn' over includes
5856 * one that had the LAST_FRAG. We no
5857 * longer need to do the PD-API.
5859 asoc->fragmented_delivery_inprogress = 0;
5861 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5862 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5863 stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq);
5871 if (asoc->fragmented_delivery_inprogress) {
5873 * Ok we removed cnt_gone chunks in the PD-API queue that
5874 * were being delivered. So now we must turn off the flag.
5878 str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5879 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5880 stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq);
5881 asoc->fragmented_delivery_inprogress = 0;
5883 /*************************************************************/
5884 /* 3. Update the PR-stream re-ordering queues */
5885 /*************************************************************/
5886 stseq = (struct sctp_strseq *)((caddr_t)fwd + sizeof(*fwd));
5887 fwd_sz -= sizeof(*fwd);
5890 unsigned int num_str;
5892 num_str = fwd_sz / sizeof(struct sctp_strseq);
5893 for (i = 0; i < num_str; i++) {
5898 xx = (unsigned char *)&stseq[i];
5899 st = ntohs(stseq[i].stream);
5900 stseq[i].stream = st;
5901 st = ntohs(stseq[i].sequence);
5902 stseq[i].sequence = st;
5904 if (stseq[i].stream > asoc->streamincnt) {
5906 * It is arguable if we should continue.
5907 * Since the peer sent bogus stream info we
5908 * may be in deep trouble.. a return may be
5913 strm = &asoc->strmin[stseq[i].stream];
5914 if (compare_with_wrap(stseq[i].sequence,
5915 strm->last_sequence_delivered, MAX_SEQ)) {
5916 /* Update the sequence number */
5917 strm->last_sequence_delivered =
5920 /* now kick the stream the new way */
5921 sctp_kick_prsctp_reorder_queue(stcb, strm);
5924 if (TAILQ_FIRST(&asoc->reasmqueue)) {
5925 /* now lets kick out and check for more fragmented delivery */
5926 sctp_deliver_reasm_check(stcb, &stcb->asoc);