2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_indata.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctp_timer.h>
52 * NOTES: On the outbound side of things I need to check the sack timer to
53 * see if I should generate a sack into the chunk queue (if I have data to
54 * send that is and will be sending it .. for bundling.
56 * The callback in sctp_usrreq.c will get called when the socket is read from.
57 * This will cause sctp_service_queues() to get called on the top entry in
62 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
64 asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
67 /* Calculate what the rwnd would be */
69 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
74 * This is really set wrong with respect to a 1-2-m socket. Since
75 * the sb_cc is the count that everyone as put up. When we re-write
76 * sctp_soreceive then we will fix this so that ONLY this
77 * associations data is taken into account.
79 if (stcb->sctp_socket == NULL)
82 if (stcb->asoc.sb_cc == 0 &&
83 asoc->size_on_reasm_queue == 0 &&
84 asoc->size_on_all_streams == 0) {
85 /* Full rwnd granted */
86 calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
89 /* get actual space */
90 calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
93 * take out what has NOT been put on socket queue and we yet hold
96 calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
97 asoc->cnt_on_reasm_queue * MSIZE));
98 calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
99 asoc->cnt_on_all_streams * MSIZE));
105 /* what is the overhead of all these rwnd's */
106 calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
108 * If the window gets too small due to ctrl-stuff, reduce it to 1,
109 * even it is 0. SWS engaged
111 if (calc < stcb->asoc.my_rwnd_control_len) {
120 * Build out our readq entry based on the incoming packet.
122 struct sctp_queued_to_read *
123 sctp_build_readq_entry(struct sctp_tcb *stcb,
124 struct sctp_nets *net,
125 uint32_t tsn, uint32_t ppid,
126 uint32_t context, uint16_t stream_no,
127 uint16_t stream_seq, uint8_t flags,
130 struct sctp_queued_to_read *read_queue_e = NULL;
132 sctp_alloc_a_readq(stcb, read_queue_e);
133 if (read_queue_e == NULL) {
136 read_queue_e->sinfo_stream = stream_no;
137 read_queue_e->sinfo_ssn = stream_seq;
138 read_queue_e->sinfo_flags = (flags << 8);
139 read_queue_e->sinfo_ppid = ppid;
140 read_queue_e->sinfo_context = stcb->asoc.context;
141 read_queue_e->sinfo_timetolive = 0;
142 read_queue_e->sinfo_tsn = tsn;
143 read_queue_e->sinfo_cumtsn = tsn;
144 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
145 read_queue_e->whoFrom = net;
146 read_queue_e->length = 0;
147 atomic_add_int(&net->ref_count, 1);
148 read_queue_e->data = dm;
149 read_queue_e->spec_flags = 0;
150 read_queue_e->tail_mbuf = NULL;
151 read_queue_e->aux_data = NULL;
152 read_queue_e->stcb = stcb;
153 read_queue_e->port_from = stcb->rport;
154 read_queue_e->do_not_ref_stcb = 0;
155 read_queue_e->end_added = 0;
156 read_queue_e->some_taken = 0;
157 read_queue_e->pdapi_aborted = 0;
159 return (read_queue_e);
164 * Build out our readq entry based on the incoming packet.
166 static struct sctp_queued_to_read *
167 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
168 struct sctp_tmit_chunk *chk)
170 struct sctp_queued_to_read *read_queue_e = NULL;
172 sctp_alloc_a_readq(stcb, read_queue_e);
173 if (read_queue_e == NULL) {
176 read_queue_e->sinfo_stream = chk->rec.data.stream_number;
177 read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
178 read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
179 read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
180 read_queue_e->sinfo_context = stcb->asoc.context;
181 read_queue_e->sinfo_timetolive = 0;
182 read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
183 read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
184 read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
185 read_queue_e->whoFrom = chk->whoTo;
186 read_queue_e->aux_data = NULL;
187 read_queue_e->length = 0;
188 atomic_add_int(&chk->whoTo->ref_count, 1);
189 read_queue_e->data = chk->data;
190 read_queue_e->tail_mbuf = NULL;
191 read_queue_e->stcb = stcb;
192 read_queue_e->port_from = stcb->rport;
193 read_queue_e->spec_flags = 0;
194 read_queue_e->do_not_ref_stcb = 0;
195 read_queue_e->end_added = 0;
196 read_queue_e->some_taken = 0;
197 read_queue_e->pdapi_aborted = 0;
199 return (read_queue_e);
204 sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
206 struct sctp_extrcvinfo *seinfo;
207 struct sctp_sndrcvinfo *outinfo;
208 struct sctp_rcvinfo *rcvinfo;
209 struct sctp_nxtinfo *nxtinfo;
216 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
217 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
218 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
219 /* user does not want any ancillary data */
223 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
224 len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
226 seinfo = (struct sctp_extrcvinfo *)sinfo;
227 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
228 (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
230 len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
234 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
235 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
237 len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
240 len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
246 ret = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
251 SCTP_BUF_LEN(ret) = 0;
253 /* We need a CMSG header followed by the struct */
254 cmh = mtod(ret, struct cmsghdr *);
255 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
256 cmh->cmsg_level = IPPROTO_SCTP;
257 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
258 cmh->cmsg_type = SCTP_RCVINFO;
259 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
260 rcvinfo->rcv_sid = sinfo->sinfo_stream;
261 rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
262 rcvinfo->rcv_flags = sinfo->sinfo_flags;
263 rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
264 rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
265 rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
266 rcvinfo->rcv_context = sinfo->sinfo_context;
267 rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
268 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
269 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
272 cmh->cmsg_level = IPPROTO_SCTP;
273 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
274 cmh->cmsg_type = SCTP_NXTINFO;
275 nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
276 nxtinfo->nxt_sid = seinfo->sreinfo_next_stream;
277 nxtinfo->nxt_flags = 0;
278 if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
279 nxtinfo->nxt_flags |= SCTP_UNORDERED;
281 if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
282 nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
284 if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
285 nxtinfo->nxt_flags |= SCTP_COMPLETE;
287 nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid;
288 nxtinfo->nxt_length = seinfo->sreinfo_next_length;
289 nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid;
290 cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
291 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
293 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
294 cmh->cmsg_level = IPPROTO_SCTP;
295 outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
297 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
298 cmh->cmsg_type = SCTP_EXTRCV;
299 memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
300 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
302 cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
303 cmh->cmsg_type = SCTP_SNDRCV;
305 SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
313 sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
315 uint32_t gap, i, cumackp1;
318 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
321 cumackp1 = asoc->cumulative_tsn + 1;
322 if (SCTP_TSN_GT(cumackp1, tsn)) {
324 * this tsn is behind the cum ack and thus we don't need to
325 * worry about it being moved from one to the other.
329 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
330 if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
331 printf("gap:%x tsn:%x\n", gap, tsn);
332 sctp_print_mapping_array(asoc);
334 panic("Things are really messed up now!!");
337 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
338 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
339 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
340 asoc->highest_tsn_inside_nr_map = tsn;
342 if (tsn == asoc->highest_tsn_inside_map) {
343 /* We must back down to see what the new highest is */
344 for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
345 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
346 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
347 asoc->highest_tsn_inside_map = i;
353 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
360 * We are delivering currently from the reassembly queue. We must continue to
361 * deliver until we either: 1) run out of space. 2) run out of sequential
362 * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
365 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
367 struct sctp_tmit_chunk *chk, *nchk;
372 struct sctp_queued_to_read *control, *ctl, *nctl;
377 cntDel = stream_no = 0;
378 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
379 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
380 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
381 /* socket above is long gone or going.. */
383 asoc->fragmented_delivery_inprogress = 0;
384 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
385 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
386 asoc->size_on_reasm_queue -= chk->send_size;
387 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
389 * Lose the data pointer, since its in the socket
393 sctp_m_freem(chk->data);
396 /* Now free the address and data */
397 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
398 /* sa_ignore FREED_MEMORY */
402 SCTP_TCB_LOCK_ASSERT(stcb);
403 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
404 if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
405 /* Can't deliver more :< */
408 stream_no = chk->rec.data.stream_number;
409 nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
410 if (nxt_todel != chk->rec.data.stream_seq &&
411 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
413 * Not the next sequence to deliver in its stream OR
418 if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
420 control = sctp_build_readq_entry_chk(stcb, chk);
421 if (control == NULL) {
425 /* save it off for our future deliveries */
426 stcb->asoc.control_pdapi = control;
427 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
431 sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
432 sctp_add_to_readq(stcb->sctp_ep,
433 stcb, control, &stcb->sctp_socket->so_rcv, end,
434 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
437 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
441 sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
442 if (sctp_append_to_readq(stcb->sctp_ep, stcb,
443 stcb->asoc.control_pdapi,
444 chk->data, end, chk->rec.data.TSN_seq,
445 &stcb->sctp_socket->so_rcv)) {
447 * something is very wrong, either
448 * control_pdapi is NULL, or the tail_mbuf
449 * is corrupt, or there is a EOM already on
452 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
456 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
457 panic("This should not happen control_pdapi NULL?");
459 /* if we did not panic, it was a EOM */
460 panic("Bad chunking ??");
462 if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
463 SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
465 SCTP_PRINTF("Bad chunking ??\n");
466 SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
474 /* pull it we did it */
475 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
476 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
477 asoc->fragmented_delivery_inprogress = 0;
478 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
479 asoc->strmin[stream_no].last_sequence_delivered++;
481 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
482 SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
484 } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
486 * turn the flag back on since we just delivered
489 asoc->fragmented_delivery_inprogress = 1;
491 asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
492 asoc->last_flags_delivered = chk->rec.data.rcv_flags;
493 asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
494 asoc->last_strm_no_delivered = chk->rec.data.stream_number;
496 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
497 asoc->size_on_reasm_queue -= chk->send_size;
498 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
499 /* free up the chk */
501 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
503 if (asoc->fragmented_delivery_inprogress == 0) {
505 * Now lets see if we can deliver the next one on
508 struct sctp_stream_in *strm;
510 strm = &asoc->strmin[stream_no];
511 nxt_todel = strm->last_sequence_delivered + 1;
512 TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
513 /* Deliver more if we can. */
514 if (nxt_todel == ctl->sinfo_ssn) {
515 TAILQ_REMOVE(&strm->inqueue, ctl, next);
516 asoc->size_on_all_streams -= ctl->length;
517 sctp_ucount_decr(asoc->cnt_on_all_streams);
518 strm->last_sequence_delivered++;
519 sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
520 sctp_add_to_readq(stcb->sctp_ep, stcb,
522 &stcb->sctp_socket->so_rcv, 1,
523 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
527 nxt_todel = strm->last_sequence_delivered + 1;
535 * Queue the chunk either right into the socket buffer if it is the next one
536 * to go OR put it in the correct place in the delivery queue. If we do
537 * append to the so_buf, keep doing so until we are out of order. One big
538 * question still remains, what to do when the socket buffer is FULL??
541 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
542 struct sctp_queued_to_read *control, int *abort_flag)
545 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
546 * all the data in one stream this could happen quite rapidly. One
547 * could use the TSN to keep track of things, but this scheme breaks
548 * down in the other type of stream useage that could occur. Send a
549 * single msg to stream 0, send 4Billion messages to stream 1, now
550 * send a message to stream 0. You have a situation where the TSN
551 * has wrapped but not in the stream. Is this worth worrying about
552 * or should we just change our queue sort at the bottom to be by
555 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
556 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
557 * assignment this could happen... and I don't see how this would be
558 * a violation. So for now I am undecided an will leave the sort by
559 * SSN alone. Maybe a hybred approach is the answer
562 struct sctp_stream_in *strm;
563 struct sctp_queued_to_read *at;
569 asoc->size_on_all_streams += control->length;
570 sctp_ucount_incr(asoc->cnt_on_all_streams);
571 strm = &asoc->strmin[control->sinfo_stream];
572 nxt_todel = strm->last_sequence_delivered + 1;
573 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
574 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
576 SCTPDBG(SCTP_DEBUG_INDATA1,
577 "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
578 (uint32_t) control->sinfo_stream,
579 (uint32_t) strm->last_sequence_delivered,
580 (uint32_t) nxt_todel);
581 if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
582 /* The incoming sseq is behind where we last delivered? */
583 SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
584 control->sinfo_ssn, strm->last_sequence_delivered);
587 * throw it in the stream so it gets cleaned up in
588 * association destruction
590 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
591 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
592 0, M_DONTWAIT, 1, MT_DATA);
594 struct sctp_paramhdr *ph;
597 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
598 (sizeof(uint32_t) * 3);
599 ph = mtod(oper, struct sctp_paramhdr *);
600 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
601 ph->param_length = htons(SCTP_BUF_LEN(oper));
602 ippp = (uint32_t *) (ph + 1);
603 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
605 *ippp = control->sinfo_tsn;
607 *ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
609 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
610 sctp_abort_an_association(stcb->sctp_ep, stcb,
611 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
617 if (nxt_todel == control->sinfo_ssn) {
618 /* can be delivered right away? */
619 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
620 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
622 /* EY it wont be queued if it could be delivered directly */
624 asoc->size_on_all_streams -= control->length;
625 sctp_ucount_decr(asoc->cnt_on_all_streams);
626 strm->last_sequence_delivered++;
628 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
629 sctp_add_to_readq(stcb->sctp_ep, stcb,
631 &stcb->sctp_socket->so_rcv, 1,
632 SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
633 TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
635 nxt_todel = strm->last_sequence_delivered + 1;
636 if (nxt_todel == control->sinfo_ssn) {
637 TAILQ_REMOVE(&strm->inqueue, control, next);
638 asoc->size_on_all_streams -= control->length;
639 sctp_ucount_decr(asoc->cnt_on_all_streams);
640 strm->last_sequence_delivered++;
642 * We ignore the return of deliver_data here
643 * since we always can hold the chunk on the
644 * d-queue. And we have a finite number that
645 * can be delivered from the strq.
647 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
648 sctp_log_strm_del(control, NULL,
649 SCTP_STR_LOG_FROM_IMMED_DEL);
651 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
652 sctp_add_to_readq(stcb->sctp_ep, stcb,
654 &stcb->sctp_socket->so_rcv, 1,
655 SCTP_READ_LOCK_NOT_HELD,
664 * Ok, we did not deliver this guy, find the correct place
665 * to put it on the queue.
667 if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
670 if (TAILQ_EMPTY(&strm->inqueue)) {
672 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
673 sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
675 TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
677 TAILQ_FOREACH(at, &strm->inqueue, next) {
678 if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
680 * one in queue is bigger than the
681 * new one, insert before this one
683 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
684 sctp_log_strm_del(control, at,
685 SCTP_STR_LOG_FROM_INSERT_MD);
687 TAILQ_INSERT_BEFORE(at, control, next);
689 } else if (at->sinfo_ssn == control->sinfo_ssn) {
691 * Gak, He sent me a duplicate str
695 * foo bar, I guess I will just free
696 * this new guy, should we abort
697 * too? FIX ME MAYBE? Or it COULD be
698 * that the SSN's have wrapped.
699 * Maybe I should compare to TSN
700 * somehow... sigh for now just blow
705 sctp_m_freem(control->data);
706 control->data = NULL;
707 asoc->size_on_all_streams -= control->length;
708 sctp_ucount_decr(asoc->cnt_on_all_streams);
709 if (control->whoFrom) {
710 sctp_free_remote_addr(control->whoFrom);
711 control->whoFrom = NULL;
713 sctp_free_a_readq(stcb, control);
716 if (TAILQ_NEXT(at, next) == NULL) {
718 * We are at the end, insert
721 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
722 sctp_log_strm_del(control, at,
723 SCTP_STR_LOG_FROM_INSERT_TL);
725 TAILQ_INSERT_AFTER(&strm->inqueue,
736 * Returns two things: You get the total size of the deliverable parts of the
737 * first fragmented message on the reassembly queue. And you get a 1 back if
738 * all of the message is ready or a 0 back if the message is still incomplete
741 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
743 struct sctp_tmit_chunk *chk;
747 chk = TAILQ_FIRST(&asoc->reasmqueue);
749 /* nothing on the queue */
752 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
753 /* Not a first on the queue */
756 tsn = chk->rec.data.TSN_seq;
757 TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
758 if (tsn != chk->rec.data.TSN_seq) {
761 *t_size += chk->send_size;
762 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
771 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
773 struct sctp_tmit_chunk *chk;
775 uint32_t tsize, pd_point;
778 chk = TAILQ_FIRST(&asoc->reasmqueue);
781 asoc->size_on_reasm_queue = 0;
782 asoc->cnt_on_reasm_queue = 0;
785 if (asoc->fragmented_delivery_inprogress == 0) {
787 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
788 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
789 (nxt_todel == chk->rec.data.stream_seq ||
790 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
792 * Yep the first one is here and its ok to deliver
795 if (stcb->sctp_socket) {
796 pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
797 stcb->sctp_ep->partial_delivery_point);
799 pd_point = stcb->sctp_ep->partial_delivery_point;
801 if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
804 * Yes, we setup to start reception, by
805 * backing down the TSN just in case we
806 * can't deliver. If we
808 asoc->fragmented_delivery_inprogress = 1;
809 asoc->tsn_last_delivered =
810 chk->rec.data.TSN_seq - 1;
812 chk->rec.data.stream_number;
813 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
814 asoc->pdapi_ppid = chk->rec.data.payloadtype;
815 asoc->fragment_flags = chk->rec.data.rcv_flags;
816 sctp_service_reassembly(stcb, asoc);
821 * Service re-assembly will deliver stream data queued at
822 * the end of fragmented delivery.. but it wont know to go
823 * back and call itself again... we do that here with the
826 sctp_service_reassembly(stcb, asoc);
827 if (asoc->fragmented_delivery_inprogress == 0) {
829 * finished our Fragmented delivery, could be more
838 * Dump onto the re-assembly queue, in its proper place. After dumping on the
839 * queue, see if anthing can be delivered. If so pull it off (or as much as
840 * we can. If we run out of space then we must dump what we can and set the
841 * appropriate flag to say we queued what we could.
844 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
845 struct sctp_tmit_chunk *chk, int *abort_flag)
848 uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
850 struct sctp_tmit_chunk *at, *prev, *next;
853 cum_ackp1 = asoc->tsn_last_delivered + 1;
854 if (TAILQ_EMPTY(&asoc->reasmqueue)) {
855 /* This is the first one on the queue */
856 TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
858 * we do not check for delivery of anything when only one
861 asoc->size_on_reasm_queue = chk->send_size;
862 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
863 if (chk->rec.data.TSN_seq == cum_ackp1) {
864 if (asoc->fragmented_delivery_inprogress == 0 &&
865 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
866 SCTP_DATA_FIRST_FRAG) {
868 * An empty queue, no delivery inprogress,
869 * we hit the next one and it does NOT have
870 * a FIRST fragment mark.
872 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
873 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
874 0, M_DONTWAIT, 1, MT_DATA);
877 struct sctp_paramhdr *ph;
881 sizeof(struct sctp_paramhdr) +
882 (sizeof(uint32_t) * 3);
883 ph = mtod(oper, struct sctp_paramhdr *);
885 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
886 ph->param_length = htons(SCTP_BUF_LEN(oper));
887 ippp = (uint32_t *) (ph + 1);
888 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
890 *ippp = chk->rec.data.TSN_seq;
892 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
895 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
896 sctp_abort_an_association(stcb->sctp_ep, stcb,
897 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
899 } else if (asoc->fragmented_delivery_inprogress &&
900 (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
902 * We are doing a partial delivery and the
903 * NEXT chunk MUST be either the LAST or
904 * MIDDLE fragment NOT a FIRST
906 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
907 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
908 0, M_DONTWAIT, 1, MT_DATA);
910 struct sctp_paramhdr *ph;
914 sizeof(struct sctp_paramhdr) +
915 (3 * sizeof(uint32_t));
916 ph = mtod(oper, struct sctp_paramhdr *);
918 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
919 ph->param_length = htons(SCTP_BUF_LEN(oper));
920 ippp = (uint32_t *) (ph + 1);
921 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
923 *ippp = chk->rec.data.TSN_seq;
925 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
927 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
928 sctp_abort_an_association(stcb->sctp_ep, stcb,
929 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
931 } else if (asoc->fragmented_delivery_inprogress) {
933 * Here we are ok with a MIDDLE or LAST
936 if (chk->rec.data.stream_number !=
937 asoc->str_of_pdapi) {
938 /* Got to be the right STR No */
939 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
940 chk->rec.data.stream_number,
942 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
943 0, M_DONTWAIT, 1, MT_DATA);
945 struct sctp_paramhdr *ph;
949 sizeof(struct sctp_paramhdr) +
950 (sizeof(uint32_t) * 3);
952 struct sctp_paramhdr *);
954 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
956 htons(SCTP_BUF_LEN(oper));
957 ippp = (uint32_t *) (ph + 1);
958 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
960 *ippp = chk->rec.data.TSN_seq;
962 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
964 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
965 sctp_abort_an_association(stcb->sctp_ep,
966 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
968 } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
969 SCTP_DATA_UNORDERED &&
970 chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
971 /* Got to be the right STR Seq */
972 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
973 chk->rec.data.stream_seq,
975 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
976 0, M_DONTWAIT, 1, MT_DATA);
978 struct sctp_paramhdr *ph;
982 sizeof(struct sctp_paramhdr) +
983 (3 * sizeof(uint32_t));
985 struct sctp_paramhdr *);
987 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
989 htons(SCTP_BUF_LEN(oper));
990 ippp = (uint32_t *) (ph + 1);
991 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
993 *ippp = chk->rec.data.TSN_seq;
995 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
998 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
999 sctp_abort_an_association(stcb->sctp_ep,
1000 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1007 /* Find its place */
1008 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1009 if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
1011 * one in queue is bigger than the new one, insert
1015 asoc->size_on_reasm_queue += chk->send_size;
1016 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1018 TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1020 } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1021 /* Gak, He sent me a duplicate str seq number */
1023 * foo bar, I guess I will just free this new guy,
1024 * should we abort too? FIX ME MAYBE? Or it COULD be
1025 * that the SSN's have wrapped. Maybe I should
1026 * compare to TSN somehow... sigh for now just blow
1030 sctp_m_freem(chk->data);
1033 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1036 last_flags = at->rec.data.rcv_flags;
1037 last_tsn = at->rec.data.TSN_seq;
1039 if (TAILQ_NEXT(at, sctp_next) == NULL) {
1041 * We are at the end, insert it after this
1044 /* check it first */
1045 asoc->size_on_reasm_queue += chk->send_size;
1046 sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1047 TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1052 /* Now the audits */
1054 prev_tsn = chk->rec.data.TSN_seq - 1;
1055 if (prev_tsn == prev->rec.data.TSN_seq) {
1057 * Ok the one I am dropping onto the end is the
1058 * NEXT. A bit of valdiation here.
1060 if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1061 SCTP_DATA_FIRST_FRAG ||
1062 (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1063 SCTP_DATA_MIDDLE_FRAG) {
1065 * Insert chk MUST be a MIDDLE or LAST
1068 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1069 SCTP_DATA_FIRST_FRAG) {
1070 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1071 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1072 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1073 0, M_DONTWAIT, 1, MT_DATA);
1075 struct sctp_paramhdr *ph;
1078 SCTP_BUF_LEN(oper) =
1079 sizeof(struct sctp_paramhdr) +
1080 (3 * sizeof(uint32_t));
1082 struct sctp_paramhdr *);
1084 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1086 htons(SCTP_BUF_LEN(oper));
1087 ippp = (uint32_t *) (ph + 1);
1088 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1090 *ippp = chk->rec.data.TSN_seq;
1092 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1095 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1096 sctp_abort_an_association(stcb->sctp_ep,
1097 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1101 if (chk->rec.data.stream_number !=
1102 prev->rec.data.stream_number) {
1104 * Huh, need the correct STR here,
1105 * they must be the same.
1107 SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1108 chk->rec.data.stream_number,
1109 prev->rec.data.stream_number);
1110 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1111 0, M_DONTWAIT, 1, MT_DATA);
1113 struct sctp_paramhdr *ph;
1116 SCTP_BUF_LEN(oper) =
1117 sizeof(struct sctp_paramhdr) +
1118 (3 * sizeof(uint32_t));
1120 struct sctp_paramhdr *);
1122 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1124 htons(SCTP_BUF_LEN(oper));
1125 ippp = (uint32_t *) (ph + 1);
1126 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1128 *ippp = chk->rec.data.TSN_seq;
1130 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1132 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1133 sctp_abort_an_association(stcb->sctp_ep,
1134 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1139 if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1140 chk->rec.data.stream_seq !=
1141 prev->rec.data.stream_seq) {
1143 * Huh, need the correct STR here,
1144 * they must be the same.
1146 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1147 chk->rec.data.stream_seq,
1148 prev->rec.data.stream_seq);
1149 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1150 0, M_DONTWAIT, 1, MT_DATA);
1152 struct sctp_paramhdr *ph;
1155 SCTP_BUF_LEN(oper) =
1156 sizeof(struct sctp_paramhdr) +
1157 (3 * sizeof(uint32_t));
1159 struct sctp_paramhdr *);
1161 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1163 htons(SCTP_BUF_LEN(oper));
1164 ippp = (uint32_t *) (ph + 1);
1165 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1167 *ippp = chk->rec.data.TSN_seq;
1169 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1171 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1172 sctp_abort_an_association(stcb->sctp_ep,
1173 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1178 } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1179 SCTP_DATA_LAST_FRAG) {
1180 /* Insert chk MUST be a FIRST */
1181 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1182 SCTP_DATA_FIRST_FRAG) {
1183 SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1184 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1185 0, M_DONTWAIT, 1, MT_DATA);
1187 struct sctp_paramhdr *ph;
1190 SCTP_BUF_LEN(oper) =
1191 sizeof(struct sctp_paramhdr) +
1192 (3 * sizeof(uint32_t));
1194 struct sctp_paramhdr *);
1196 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1198 htons(SCTP_BUF_LEN(oper));
1199 ippp = (uint32_t *) (ph + 1);
1200 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1202 *ippp = chk->rec.data.TSN_seq;
1204 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1207 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1208 sctp_abort_an_association(stcb->sctp_ep,
1209 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1218 post_tsn = chk->rec.data.TSN_seq + 1;
1219 if (post_tsn == next->rec.data.TSN_seq) {
1221 * Ok the one I am inserting ahead of is my NEXT
1222 * one. A bit of valdiation here.
1224 if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1225 /* Insert chk MUST be a last fragment */
1226 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1227 != SCTP_DATA_LAST_FRAG) {
1228 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1229 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1230 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1231 0, M_DONTWAIT, 1, MT_DATA);
1233 struct sctp_paramhdr *ph;
1236 SCTP_BUF_LEN(oper) =
1237 sizeof(struct sctp_paramhdr) +
1238 (3 * sizeof(uint32_t));
1240 struct sctp_paramhdr *);
1242 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1244 htons(SCTP_BUF_LEN(oper));
1245 ippp = (uint32_t *) (ph + 1);
1246 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1248 *ippp = chk->rec.data.TSN_seq;
1250 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1252 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1253 sctp_abort_an_association(stcb->sctp_ep,
1254 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1259 } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1260 SCTP_DATA_MIDDLE_FRAG ||
1261 (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1262 SCTP_DATA_LAST_FRAG) {
1264 * Insert chk CAN be MIDDLE or FIRST NOT
1267 if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1268 SCTP_DATA_LAST_FRAG) {
1269 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1270 SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1271 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1272 0, M_DONTWAIT, 1, MT_DATA);
1274 struct sctp_paramhdr *ph;
1277 SCTP_BUF_LEN(oper) =
1278 sizeof(struct sctp_paramhdr) +
1279 (3 * sizeof(uint32_t));
1281 struct sctp_paramhdr *);
1283 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1285 htons(SCTP_BUF_LEN(oper));
1286 ippp = (uint32_t *) (ph + 1);
1287 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1289 *ippp = chk->rec.data.TSN_seq;
1291 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1294 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1295 sctp_abort_an_association(stcb->sctp_ep,
1296 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1301 if (chk->rec.data.stream_number !=
1302 next->rec.data.stream_number) {
1304 * Huh, need the correct STR here,
1305 * they must be the same.
1307 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1308 chk->rec.data.stream_number,
1309 next->rec.data.stream_number);
1310 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1311 0, M_DONTWAIT, 1, MT_DATA);
1313 struct sctp_paramhdr *ph;
1316 SCTP_BUF_LEN(oper) =
1317 sizeof(struct sctp_paramhdr) +
1318 (3 * sizeof(uint32_t));
1320 struct sctp_paramhdr *);
1322 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1324 htons(SCTP_BUF_LEN(oper));
1325 ippp = (uint32_t *) (ph + 1);
1326 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1328 *ippp = chk->rec.data.TSN_seq;
1330 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1333 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1334 sctp_abort_an_association(stcb->sctp_ep,
1335 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1340 if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1341 chk->rec.data.stream_seq !=
1342 next->rec.data.stream_seq) {
1344 * Huh, need the correct STR here,
1345 * they must be the same.
1347 SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1348 chk->rec.data.stream_seq,
1349 next->rec.data.stream_seq);
1350 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1351 0, M_DONTWAIT, 1, MT_DATA);
1353 struct sctp_paramhdr *ph;
1356 SCTP_BUF_LEN(oper) =
1357 sizeof(struct sctp_paramhdr) +
1358 (3 * sizeof(uint32_t));
1360 struct sctp_paramhdr *);
1362 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1364 htons(SCTP_BUF_LEN(oper));
1365 ippp = (uint32_t *) (ph + 1);
1366 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1368 *ippp = chk->rec.data.TSN_seq;
1370 *ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1372 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1373 sctp_abort_an_association(stcb->sctp_ep,
1374 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1382 /* Do we need to do some delivery? check */
1383 sctp_deliver_reasm_check(stcb, asoc);
1387 * This is an unfortunate routine. It checks to make sure a evil guy is not
1388 * stuffing us full of bad packet fragments. A broken peer could also do this
1389 * but this is doubtful. It is to bad I must worry about evil crackers sigh
1393 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1396 struct sctp_tmit_chunk *at;
1399 TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1400 if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1401 /* is it one bigger? */
1402 tsn_est = at->rec.data.TSN_seq + 1;
1403 if (tsn_est == TSN_seq) {
1404 /* yep. It better be a last then */
1405 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1406 SCTP_DATA_LAST_FRAG) {
1408 * Ok this guy belongs next to a guy
1409 * that is NOT last, it should be a
1410 * middle/last, not a complete
1416 * This guy is ok since its a LAST
1417 * and the new chunk is a fully
1418 * self- contained one.
1423 } else if (TSN_seq == at->rec.data.TSN_seq) {
1424 /* Software error since I have a dup? */
1428 * Ok, 'at' is larger than new chunk but does it
1429 * need to be right before it.
1431 tsn_est = TSN_seq + 1;
1432 if (tsn_est == at->rec.data.TSN_seq) {
1433 /* Yep, It better be a first */
1434 if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1435 SCTP_DATA_FIRST_FRAG) {
1448 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1449 struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1450 struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1451 int *break_flag, int last_chunk)
1453 /* Process a data chunk */
1454 /* struct sctp_tmit_chunk *chk; */
1455 struct sctp_tmit_chunk *chk;
1459 int need_reasm_check = 0;
1460 uint16_t strmno, strmseq;
1462 struct sctp_queued_to_read *control;
1464 uint32_t protocol_id;
1465 uint8_t chunk_flags;
1466 struct sctp_stream_reset_list *liste;
1469 tsn = ntohl(ch->dp.tsn);
1470 chunk_flags = ch->ch.chunk_flags;
1471 if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1472 asoc->send_sack = 1;
1474 protocol_id = ch->dp.protocol_id;
1475 ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1476 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1477 sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1482 SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1483 if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1484 /* It is a duplicate */
1485 SCTP_STAT_INCR(sctps_recvdupdata);
1486 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1487 /* Record a dup for the next outbound sack */
1488 asoc->dup_tsns[asoc->numduptsns] = tsn;
1491 asoc->send_sack = 1;
1494 /* Calculate the number of TSN's between the base and this TSN */
1495 SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1496 if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1497 /* Can't hold the bit in the mapping at max array, toss it */
1500 if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1501 SCTP_TCB_LOCK_ASSERT(stcb);
1502 if (sctp_expand_mapping_array(asoc, gap)) {
1503 /* Can't expand, drop it */
1507 if (SCTP_TSN_GT(tsn, *high_tsn)) {
1510 /* See if we have received this one already */
1511 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
1512 SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1513 SCTP_STAT_INCR(sctps_recvdupdata);
1514 if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1515 /* Record a dup for the next outbound sack */
1516 asoc->dup_tsns[asoc->numduptsns] = tsn;
1519 asoc->send_sack = 1;
1523 * Check to see about the GONE flag, duplicates would cause a sack
1524 * to be sent up above
1526 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1527 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1528 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1531 * wait a minute, this guy is gone, there is no longer a
1532 * receiver. Send peer an ABORT!
1534 struct mbuf *op_err;
1536 op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1537 sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1542 * Now before going further we see if there is room. If NOT then we
1543 * MAY let one through only IF this TSN is the one we are waiting
1544 * for on a partial delivery API.
1547 /* now do the tests */
1548 if (((asoc->cnt_on_all_streams +
1549 asoc->cnt_on_reasm_queue +
1550 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1551 (((int)asoc->my_rwnd) <= 0)) {
1553 * When we have NO room in the rwnd we check to make sure
1554 * the reader is doing its job...
1556 if (stcb->sctp_socket->so_rcv.sb_cc) {
1557 /* some to read, wake-up */
1558 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1561 so = SCTP_INP_SO(stcb->sctp_ep);
1562 atomic_add_int(&stcb->asoc.refcnt, 1);
1563 SCTP_TCB_UNLOCK(stcb);
1564 SCTP_SOCKET_LOCK(so, 1);
1565 SCTP_TCB_LOCK(stcb);
1566 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1567 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1568 /* assoc was freed while we were unlocked */
1569 SCTP_SOCKET_UNLOCK(so, 1);
1573 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1574 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1575 SCTP_SOCKET_UNLOCK(so, 1);
1578 /* now is it in the mapping array of what we have accepted? */
1579 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
1580 SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1581 /* Nope not in the valid range dump it */
1582 sctp_set_rwnd(stcb, asoc);
1583 if ((asoc->cnt_on_all_streams +
1584 asoc->cnt_on_reasm_queue +
1585 asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1586 SCTP_STAT_INCR(sctps_datadropchklmt);
1588 SCTP_STAT_INCR(sctps_datadroprwnd);
1595 strmno = ntohs(ch->dp.stream_id);
1596 if (strmno >= asoc->streamincnt) {
1597 struct sctp_paramhdr *phdr;
1600 mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1601 0, M_DONTWAIT, 1, MT_DATA);
1603 /* add some space up front so prepend will work well */
1604 SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1605 phdr = mtod(mb, struct sctp_paramhdr *);
1607 * Error causes are just param's and this one has
1608 * two back to back phdr, one with the error type
1609 * and size, the other with the streamid and a rsvd
1611 SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1612 phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1613 phdr->param_length =
1614 htons(sizeof(struct sctp_paramhdr) * 2);
1616 /* We insert the stream in the type field */
1617 phdr->param_type = ch->dp.stream_id;
1618 /* And set the length to 0 for the rsvd field */
1619 phdr->param_length = 0;
1620 sctp_queue_op_err(stcb, mb);
1622 SCTP_STAT_INCR(sctps_badsid);
1623 SCTP_TCB_LOCK_ASSERT(stcb);
1624 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1625 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1626 asoc->highest_tsn_inside_nr_map = tsn;
1628 if (tsn == (asoc->cumulative_tsn + 1)) {
1629 /* Update cum-ack */
1630 asoc->cumulative_tsn = tsn;
1635 * Before we continue lets validate that we are not being fooled by
1636 * an evil attacker. We can only have 4k chunks based on our TSN
1637 * spread allowed by the mapping array 512 * 8 bits, so there is no
1638 * way our stream sequence numbers could have wrapped. We of course
1639 * only validate the FIRST fragment so the bit must be set.
1641 strmseq = ntohs(ch->dp.stream_sequence);
1642 #ifdef SCTP_ASOCLOG_OF_TSNS
1643 SCTP_TCB_LOCK_ASSERT(stcb);
1644 if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1645 asoc->tsn_in_at = 0;
1646 asoc->tsn_in_wrapped = 1;
1648 asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1649 asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1650 asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1651 asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1652 asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1653 asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1654 asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1655 asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1658 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1659 (TAILQ_EMPTY(&asoc->resetHead)) &&
1660 (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1661 SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1662 /* The incoming sseq is behind where we last delivered? */
1663 SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1664 strmseq, asoc->strmin[strmno].last_sequence_delivered);
1665 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1666 0, M_DONTWAIT, 1, MT_DATA);
1668 struct sctp_paramhdr *ph;
1671 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1672 (3 * sizeof(uint32_t));
1673 ph = mtod(oper, struct sctp_paramhdr *);
1674 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1675 ph->param_length = htons(SCTP_BUF_LEN(oper));
1676 ippp = (uint32_t *) (ph + 1);
1677 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1681 *ippp = ((strmno << 16) | strmseq);
1684 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1685 sctp_abort_an_association(stcb->sctp_ep, stcb,
1686 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1690 /************************************
1691 * From here down we may find ch-> invalid
1692 * so its a good idea NOT to use it.
1693 *************************************/
1695 the_len = (chk_length - sizeof(struct sctp_data_chunk));
1696 if (last_chunk == 0) {
1697 dmbuf = SCTP_M_COPYM(*m,
1698 (offset + sizeof(struct sctp_data_chunk)),
1699 the_len, M_DONTWAIT);
1700 #ifdef SCTP_MBUF_LOGGING
1701 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1706 if (SCTP_BUF_IS_EXTENDED(mat)) {
1707 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1709 mat = SCTP_BUF_NEXT(mat);
1714 /* We can steal the last chunk */
1718 /* lop off the top part */
1719 m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1720 if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1721 l_len = SCTP_BUF_LEN(dmbuf);
1724 * need to count up the size hopefully does not hit
1732 l_len += SCTP_BUF_LEN(lat);
1733 lat = SCTP_BUF_NEXT(lat);
1736 if (l_len > the_len) {
1737 /* Trim the end round bytes off too */
1738 m_adj(dmbuf, -(l_len - the_len));
1741 if (dmbuf == NULL) {
1742 SCTP_STAT_INCR(sctps_nomem);
1745 if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1746 asoc->fragmented_delivery_inprogress == 0 &&
1747 TAILQ_EMPTY(&asoc->resetHead) &&
1749 ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1750 TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1751 /* Candidate for express delivery */
1753 * Its not fragmented, No PD-API is up, Nothing in the
1754 * delivery queue, Its un-ordered OR ordered and the next to
1755 * deliver AND nothing else is stuck on the stream queue,
1756 * And there is room for it in the socket buffer. Lets just
1757 * stuff it up the buffer....
1760 /* It would be nice to avoid this copy if we could :< */
1761 sctp_alloc_a_readq(stcb, control);
1762 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1768 if (control == NULL) {
1769 goto failed_express_del;
1771 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1772 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1773 asoc->highest_tsn_inside_nr_map = tsn;
1775 sctp_add_to_readq(stcb->sctp_ep, stcb,
1776 control, &stcb->sctp_socket->so_rcv,
1777 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1779 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1780 /* for ordered, bump what we delivered */
1781 asoc->strmin[strmno].last_sequence_delivered++;
1783 SCTP_STAT_INCR(sctps_recvexpress);
1784 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
1785 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1786 SCTP_STR_LOG_FROM_EXPRS_DEL);
1790 goto finish_express_del;
1793 /* If we reach here this is a new chunk */
1796 /* Express for fragmented delivery? */
1797 if ((asoc->fragmented_delivery_inprogress) &&
1798 (stcb->asoc.control_pdapi) &&
1799 (asoc->str_of_pdapi == strmno) &&
1800 (asoc->ssn_of_pdapi == strmseq)
1802 control = stcb->asoc.control_pdapi;
1803 if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1804 /* Can't be another first? */
1805 goto failed_pdapi_express_del;
1807 if (tsn == (control->sinfo_tsn + 1)) {
1808 /* Yep, we can add it on */
1812 if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1815 cumack = asoc->cumulative_tsn;
1816 if ((cumack + 1) == tsn)
1819 if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1821 &stcb->sctp_socket->so_rcv)) {
1822 SCTP_PRINTF("Append fails end:%d\n", end);
1823 goto failed_pdapi_express_del;
1825 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1826 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1827 asoc->highest_tsn_inside_nr_map = tsn;
1829 SCTP_STAT_INCR(sctps_recvexpressm);
1830 control->sinfo_tsn = tsn;
1831 asoc->tsn_last_delivered = tsn;
1832 asoc->fragment_flags = chunk_flags;
1833 asoc->tsn_of_pdapi_last_delivered = tsn;
1834 asoc->last_flags_delivered = chunk_flags;
1835 asoc->last_strm_seq_delivered = strmseq;
1836 asoc->last_strm_no_delivered = strmno;
1838 /* clean up the flags and such */
1839 asoc->fragmented_delivery_inprogress = 0;
1840 if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1841 asoc->strmin[strmno].last_sequence_delivered++;
1843 stcb->asoc.control_pdapi = NULL;
1844 if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1846 * There could be another message
1849 need_reasm_check = 1;
1853 goto finish_express_del;
1856 failed_pdapi_express_del:
1858 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
1859 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1860 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1861 asoc->highest_tsn_inside_nr_map = tsn;
1864 SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1865 if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
1866 asoc->highest_tsn_inside_map = tsn;
1869 if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1870 sctp_alloc_a_chunk(stcb, chk);
1872 /* No memory so we drop the chunk */
1873 SCTP_STAT_INCR(sctps_nomem);
1874 if (last_chunk == 0) {
1875 /* we copied it, free the copy */
1876 sctp_m_freem(dmbuf);
1880 chk->rec.data.TSN_seq = tsn;
1881 chk->no_fr_allowed = 0;
1882 chk->rec.data.stream_seq = strmseq;
1883 chk->rec.data.stream_number = strmno;
1884 chk->rec.data.payloadtype = protocol_id;
1885 chk->rec.data.context = stcb->asoc.context;
1886 chk->rec.data.doing_fast_retransmit = 0;
1887 chk->rec.data.rcv_flags = chunk_flags;
1889 chk->send_size = the_len;
1891 atomic_add_int(&net->ref_count, 1);
1894 sctp_alloc_a_readq(stcb, control);
1895 sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1901 if (control == NULL) {
1902 /* No memory so we drop the chunk */
1903 SCTP_STAT_INCR(sctps_nomem);
1904 if (last_chunk == 0) {
1905 /* we copied it, free the copy */
1906 sctp_m_freem(dmbuf);
1910 control->length = the_len;
1913 /* Mark it as received */
1914 /* Now queue it where it belongs */
1915 if (control != NULL) {
1916 /* First a sanity check */
1917 if (asoc->fragmented_delivery_inprogress) {
1919 * Ok, we have a fragmented delivery in progress if
1920 * this chunk is next to deliver OR belongs in our
1921 * view to the reassembly, the peer is evil or
1924 uint32_t estimate_tsn;
1926 estimate_tsn = asoc->tsn_last_delivered + 1;
1927 if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1928 (estimate_tsn == control->sinfo_tsn)) {
1929 /* Evil/Broke peer */
1930 sctp_m_freem(control->data);
1931 control->data = NULL;
1932 if (control->whoFrom) {
1933 sctp_free_remote_addr(control->whoFrom);
1934 control->whoFrom = NULL;
1936 sctp_free_a_readq(stcb, control);
1937 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1938 0, M_DONTWAIT, 1, MT_DATA);
1940 struct sctp_paramhdr *ph;
1943 SCTP_BUF_LEN(oper) =
1944 sizeof(struct sctp_paramhdr) +
1945 (3 * sizeof(uint32_t));
1946 ph = mtod(oper, struct sctp_paramhdr *);
1948 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1949 ph->param_length = htons(SCTP_BUF_LEN(oper));
1950 ippp = (uint32_t *) (ph + 1);
1951 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1955 *ippp = ((strmno << 16) | strmseq);
1957 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1958 sctp_abort_an_association(stcb->sctp_ep, stcb,
1959 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1964 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1965 sctp_m_freem(control->data);
1966 control->data = NULL;
1967 if (control->whoFrom) {
1968 sctp_free_remote_addr(control->whoFrom);
1969 control->whoFrom = NULL;
1971 sctp_free_a_readq(stcb, control);
1973 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1974 0, M_DONTWAIT, 1, MT_DATA);
1976 struct sctp_paramhdr *ph;
1979 SCTP_BUF_LEN(oper) =
1980 sizeof(struct sctp_paramhdr) +
1981 (3 * sizeof(uint32_t));
1983 struct sctp_paramhdr *);
1985 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1987 htons(SCTP_BUF_LEN(oper));
1988 ippp = (uint32_t *) (ph + 1);
1989 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1993 *ippp = ((strmno << 16) | strmseq);
1995 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1996 sctp_abort_an_association(stcb->sctp_ep,
1997 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2004 /* No PDAPI running */
2005 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2007 * Reassembly queue is NOT empty validate
2008 * that this tsn does not need to be in
2009 * reasembly queue. If it does then our peer
2010 * is broken or evil.
2012 if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2013 sctp_m_freem(control->data);
2014 control->data = NULL;
2015 if (control->whoFrom) {
2016 sctp_free_remote_addr(control->whoFrom);
2017 control->whoFrom = NULL;
2019 sctp_free_a_readq(stcb, control);
2020 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2021 0, M_DONTWAIT, 1, MT_DATA);
2023 struct sctp_paramhdr *ph;
2026 SCTP_BUF_LEN(oper) =
2027 sizeof(struct sctp_paramhdr) +
2028 (3 * sizeof(uint32_t));
2030 struct sctp_paramhdr *);
2032 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2034 htons(SCTP_BUF_LEN(oper));
2035 ippp = (uint32_t *) (ph + 1);
2036 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2040 *ippp = ((strmno << 16) | strmseq);
2042 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2043 sctp_abort_an_association(stcb->sctp_ep,
2044 stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2051 /* ok, if we reach here we have passed the sanity checks */
2052 if (chunk_flags & SCTP_DATA_UNORDERED) {
2053 /* queue directly into socket buffer */
2054 sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2055 sctp_add_to_readq(stcb->sctp_ep, stcb,
2057 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2060 * Special check for when streams are resetting. We
2061 * could be more smart about this and check the
2062 * actual stream to see if it is not being reset..
2063 * that way we would not create a HOLB when amongst
2064 * streams being reset and those not being reset.
2066 * We take complete messages that have a stream reset
2067 * intervening (aka the TSN is after where our
2068 * cum-ack needs to be) off and put them on a
2069 * pending_reply_queue. The reassembly ones we do
2070 * not have to worry about since they are all sorted
2071 * and proceessed by TSN order. It is only the
2072 * singletons I must worry about.
2074 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2075 SCTP_TSN_GT(tsn, liste->tsn)) {
2077 * yep its past where we need to reset... go
2078 * ahead and queue it.
2080 if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2082 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2084 struct sctp_queued_to_read *ctlOn,
2086 unsigned char inserted = 0;
2088 TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
2089 if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
2093 TAILQ_INSERT_BEFORE(ctlOn, control, next);
2098 if (inserted == 0) {
2100 * must be put at end, use
2101 * prevP (all setup from
2102 * loop) to setup nextP.
2104 TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2108 sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2115 /* Into the re-assembly queue */
2116 sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2119 * the assoc is now gone and chk was put onto the
2120 * reasm queue, which has all been freed.
2127 if (tsn == (asoc->cumulative_tsn + 1)) {
2128 /* Update cum-ack */
2129 asoc->cumulative_tsn = tsn;
2135 SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2137 SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2139 SCTP_STAT_INCR(sctps_recvdata);
2140 /* Set it present please */
2141 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2142 sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2144 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2145 sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2146 asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2148 /* check the special flag for stream resets */
2149 if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2150 SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
2152 * we have finished working through the backlogged TSN's now
2153 * time to reset streams. 1: call reset function. 2: free
2154 * pending_reply space 3: distribute any chunks in
2155 * pending_reply_queue.
2157 struct sctp_queued_to_read *ctl, *nctl;
2159 sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2160 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2161 SCTP_FREE(liste, SCTP_M_STRESET);
2162 /* sa_ignore FREED_MEMORY */
2163 liste = TAILQ_FIRST(&asoc->resetHead);
2164 if (TAILQ_EMPTY(&asoc->resetHead)) {
2165 /* All can be removed */
2166 TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
2167 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2168 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2174 TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
2175 if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
2179 * if ctl->sinfo_tsn is <= liste->tsn we can
2180 * process it which is the NOT of
2181 * ctl->sinfo_tsn > liste->tsn
2183 TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2184 sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2191 * Now service re-assembly to pick up anything that has been
2192 * held on reassembly queue?
2194 sctp_deliver_reasm_check(stcb, asoc);
2195 need_reasm_check = 0;
2197 if (need_reasm_check) {
2198 /* Another one waits ? */
2199 sctp_deliver_reasm_check(stcb, asoc);
2204 int8_t sctp_map_lookup_tab[256] = {
2205 0, 1, 0, 2, 0, 1, 0, 3,
2206 0, 1, 0, 2, 0, 1, 0, 4,
2207 0, 1, 0, 2, 0, 1, 0, 3,
2208 0, 1, 0, 2, 0, 1, 0, 5,
2209 0, 1, 0, 2, 0, 1, 0, 3,
2210 0, 1, 0, 2, 0, 1, 0, 4,
2211 0, 1, 0, 2, 0, 1, 0, 3,
2212 0, 1, 0, 2, 0, 1, 0, 6,
2213 0, 1, 0, 2, 0, 1, 0, 3,
2214 0, 1, 0, 2, 0, 1, 0, 4,
2215 0, 1, 0, 2, 0, 1, 0, 3,
2216 0, 1, 0, 2, 0, 1, 0, 5,
2217 0, 1, 0, 2, 0, 1, 0, 3,
2218 0, 1, 0, 2, 0, 1, 0, 4,
2219 0, 1, 0, 2, 0, 1, 0, 3,
2220 0, 1, 0, 2, 0, 1, 0, 7,
2221 0, 1, 0, 2, 0, 1, 0, 3,
2222 0, 1, 0, 2, 0, 1, 0, 4,
2223 0, 1, 0, 2, 0, 1, 0, 3,
2224 0, 1, 0, 2, 0, 1, 0, 5,
2225 0, 1, 0, 2, 0, 1, 0, 3,
2226 0, 1, 0, 2, 0, 1, 0, 4,
2227 0, 1, 0, 2, 0, 1, 0, 3,
2228 0, 1, 0, 2, 0, 1, 0, 6,
2229 0, 1, 0, 2, 0, 1, 0, 3,
2230 0, 1, 0, 2, 0, 1, 0, 4,
2231 0, 1, 0, 2, 0, 1, 0, 3,
2232 0, 1, 0, 2, 0, 1, 0, 5,
2233 0, 1, 0, 2, 0, 1, 0, 3,
2234 0, 1, 0, 2, 0, 1, 0, 4,
2235 0, 1, 0, 2, 0, 1, 0, 3,
2236 0, 1, 0, 2, 0, 1, 0, 8
2241 sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2244 * Now we also need to check the mapping array in a couple of ways.
2245 * 1) Did we move the cum-ack point?
2247 * When you first glance at this you might think that all entries that
2248 * make up the postion of the cum-ack would be in the nr-mapping
2249 * array only.. i.e. things up to the cum-ack are always
2250 * deliverable. Thats true with one exception, when its a fragmented
2251 * message we may not deliver the data until some threshold (or all
2252 * of it) is in place. So we must OR the nr_mapping_array and
2253 * mapping_array to get a true picture of the cum-ack.
2255 struct sctp_association *asoc;
2258 int slide_from, slide_end, lgap, distance;
2259 uint32_t old_cumack, old_base, old_highest, highest_tsn;
2264 old_cumack = asoc->cumulative_tsn;
2265 old_base = asoc->mapping_array_base_tsn;
2266 old_highest = asoc->highest_tsn_inside_map;
2268 * We could probably improve this a small bit by calculating the
2269 * offset of the current cum-ack as the starting point.
2272 for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2273 val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
2277 /* there is a 0 bit */
2278 at += sctp_map_lookup_tab[val];
2282 asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2284 if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
2285 SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2287 panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2288 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2290 SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2291 asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2292 sctp_print_mapping_array(asoc);
2293 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2294 sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2296 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2297 asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2300 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2301 highest_tsn = asoc->highest_tsn_inside_nr_map;
2303 highest_tsn = asoc->highest_tsn_inside_map;
2305 if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2306 /* The complete array was completed by a single FR */
2307 /* highest becomes the cum-ack */
2315 /* clear the array */
2316 clr = ((at + 7) >> 3);
2317 if (clr > asoc->mapping_array_size) {
2318 clr = asoc->mapping_array_size;
2320 memset(asoc->mapping_array, 0, clr);
2321 memset(asoc->nr_mapping_array, 0, clr);
2323 for (i = 0; i < asoc->mapping_array_size; i++) {
2324 if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2325 printf("Error Mapping array's not clean at clear\n");
2326 sctp_print_mapping_array(asoc);
2330 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2331 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2332 } else if (at >= 8) {
2333 /* we can slide the mapping array down */
2334 /* slide_from holds where we hit the first NON 0xff byte */
2337 * now calculate the ceiling of the move using our highest
2340 SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
2341 slide_end = (lgap >> 3);
2342 if (slide_end < slide_from) {
2343 sctp_print_mapping_array(asoc);
2345 panic("impossible slide");
2347 printf("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
2348 lgap, slide_end, slide_from, at);
2352 if (slide_end > asoc->mapping_array_size) {
2354 panic("would overrun buffer");
2356 printf("Gak, would have overrun map end:%d slide_end:%d\n",
2357 asoc->mapping_array_size, slide_end);
2358 slide_end = asoc->mapping_array_size;
2361 distance = (slide_end - slide_from) + 1;
2362 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2363 sctp_log_map(old_base, old_cumack, old_highest,
2364 SCTP_MAP_PREPARE_SLIDE);
2365 sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2366 (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2368 if (distance + slide_from > asoc->mapping_array_size ||
2371 * Here we do NOT slide forward the array so that
2372 * hopefully when more data comes in to fill it up
2373 * we will be able to slide it forward. Really I
2374 * don't think this should happen :-0
2377 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2378 sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2379 (uint32_t) asoc->mapping_array_size,
2380 SCTP_MAP_SLIDE_NONE);
2385 for (ii = 0; ii < distance; ii++) {
2386 asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
2387 asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
2390 for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2391 asoc->mapping_array[ii] = 0;
2392 asoc->nr_mapping_array[ii] = 0;
2394 if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2395 asoc->highest_tsn_inside_map += (slide_from << 3);
2397 if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2398 asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2400 asoc->mapping_array_base_tsn += (slide_from << 3);
2401 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2402 sctp_log_map(asoc->mapping_array_base_tsn,
2403 asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2404 SCTP_MAP_SLIDE_RESULT);
2411 sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap, int *abort_flag)
2413 struct sctp_association *asoc;
2414 uint32_t highest_tsn;
2417 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2418 highest_tsn = asoc->highest_tsn_inside_nr_map;
2420 highest_tsn = asoc->highest_tsn_inside_map;
2424 * Now we need to see if we need to queue a sack or just start the
2425 * timer (if allowed).
2427 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2429 * Ok special case, in SHUTDOWN-SENT case. here we maker
2430 * sure SACK timer is off and instead send a SHUTDOWN and a
2433 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2434 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2435 stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2437 sctp_send_shutdown(stcb,
2438 ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2439 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2443 /* is there a gap now ? */
2444 is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2447 * CMT DAC algorithm: increase number of packets received
2450 stcb->asoc.cmt_dac_pkts_rcvd++;
2452 if ((stcb->asoc.send_sack == 1) || /* We need to send a
2454 ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no
2456 (stcb->asoc.numduptsns) || /* we have dup's */
2457 (is_a_gap) || /* is still a gap */
2458 (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */
2459 (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */
2462 if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2463 (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
2464 (stcb->asoc.send_sack == 0) &&
2465 (stcb->asoc.numduptsns == 0) &&
2466 (stcb->asoc.delayed_ack) &&
2467 (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2470 * CMT DAC algorithm: With CMT, delay acks
2471 * even in the face of
2473 * reordering. Therefore, if acks that do not
2474 * have to be sent because of the above
2475 * reasons, will be delayed. That is, acks
2476 * that would have been sent due to gap
2477 * reports will be delayed with DAC. Start
2478 * the delayed ack timer.
2480 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2481 stcb->sctp_ep, stcb, NULL);
2484 * Ok we must build a SACK since the timer
2485 * is pending, we got our first packet OR
2486 * there are gaps or duplicates.
2488 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2489 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2492 if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2493 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2494 stcb->sctp_ep, stcb, NULL);
2501 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2503 struct sctp_tmit_chunk *chk;
2504 uint32_t tsize, pd_point;
2507 if (asoc->fragmented_delivery_inprogress) {
2508 sctp_service_reassembly(stcb, asoc);
2510 /* Can we proceed further, i.e. the PD-API is complete */
2511 if (asoc->fragmented_delivery_inprogress) {
2516 * Now is there some other chunk I can deliver from the reassembly
2520 chk = TAILQ_FIRST(&asoc->reasmqueue);
2522 asoc->size_on_reasm_queue = 0;
2523 asoc->cnt_on_reasm_queue = 0;
2526 nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2527 if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2528 ((nxt_todel == chk->rec.data.stream_seq) ||
2529 (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2531 * Yep the first one is here. We setup to start reception,
2532 * by backing down the TSN just in case we can't deliver.
2536 * Before we start though either all of the message should
2537 * be here or the socket buffer max or nothing on the
2538 * delivery queue and something can be delivered.
2540 if (stcb->sctp_socket) {
2541 pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
2542 stcb->sctp_ep->partial_delivery_point);
2544 pd_point = stcb->sctp_ep->partial_delivery_point;
2546 if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2547 asoc->fragmented_delivery_inprogress = 1;
2548 asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2549 asoc->str_of_pdapi = chk->rec.data.stream_number;
2550 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2551 asoc->pdapi_ppid = chk->rec.data.payloadtype;
2552 asoc->fragment_flags = chk->rec.data.rcv_flags;
2553 sctp_service_reassembly(stcb, asoc);
2554 if (asoc->fragmented_delivery_inprogress == 0) {
2562 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2563 struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2564 struct sctp_nets *net, uint32_t * high_tsn)
2566 struct sctp_data_chunk *ch, chunk_buf;
2567 struct sctp_association *asoc;
2568 int num_chunks = 0; /* number of control chunks processed */
2570 int chk_length, break_flag, last_chunk;
2571 int abort_flag = 0, was_a_gap;
2573 uint32_t highest_tsn;
2576 sctp_set_rwnd(stcb, &stcb->asoc);
2579 SCTP_TCB_LOCK_ASSERT(stcb);
2581 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2582 highest_tsn = asoc->highest_tsn_inside_nr_map;
2584 highest_tsn = asoc->highest_tsn_inside_map;
2586 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2588 * setup where we got the last DATA packet from for any SACK that
2589 * may need to go out. Don't bump the net. This is done ONLY when a
2590 * chunk is assigned.
2592 asoc->last_data_chunk_from = net;
2595 * Now before we proceed we must figure out if this is a wasted
2596 * cluster... i.e. it is a small packet sent in and yet the driver
2597 * underneath allocated a full cluster for it. If so we must copy it
2598 * to a smaller mbuf and free up the cluster mbuf. This will help
2599 * with cluster starvation. Note for __Panda__ we don't do this
2600 * since it has clusters all the way down to 64 bytes.
2602 if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2603 /* we only handle mbufs that are singletons.. not chains */
2604 m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2606 /* ok lets see if we can copy the data up */
2609 /* get the pointers and copy */
2610 to = mtod(m, caddr_t *);
2611 from = mtod((*mm), caddr_t *);
2612 memcpy(to, from, SCTP_BUF_LEN((*mm)));
2613 /* copy the length and free up the old */
2614 SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2616 /* sucess, back copy */
2619 /* We are in trouble in the mbuf world .. yikes */
2623 /* get pointer to the first chunk header */
2624 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2625 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2630 * process all DATA chunks...
2632 *high_tsn = asoc->cumulative_tsn;
2634 asoc->data_pkts_seen++;
2635 while (stop_proc == 0) {
2636 /* validate chunk length */
2637 chk_length = ntohs(ch->ch.chunk_length);
2638 if (length - *offset < chk_length) {
2639 /* all done, mutulated chunk */
2643 if (ch->ch.chunk_type == SCTP_DATA) {
2644 if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2646 * Need to send an abort since we had a
2647 * invalid data chunk.
2649 struct mbuf *op_err;
2651 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2652 0, M_DONTWAIT, 1, MT_DATA);
2655 struct sctp_paramhdr *ph;
2658 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2659 (2 * sizeof(uint32_t));
2660 ph = mtod(op_err, struct sctp_paramhdr *);
2662 htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2663 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2664 ippp = (uint32_t *) (ph + 1);
2665 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2667 *ippp = asoc->cumulative_tsn;
2670 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2671 sctp_abort_association(inp, stcb, m, iphlen, sh,
2672 op_err, 0, net->port);
2675 #ifdef SCTP_AUDITING_ENABLED
2676 sctp_audit_log(0xB1, 0);
2678 if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2683 if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2684 chk_length, net, high_tsn, &abort_flag, &break_flag,
2693 * Set because of out of rwnd space and no
2694 * drop rep space left.
2700 /* not a data chunk in the data region */
2701 switch (ch->ch.chunk_type) {
2702 case SCTP_INITIATION:
2703 case SCTP_INITIATION_ACK:
2704 case SCTP_SELECTIVE_ACK:
2705 case SCTP_NR_SELECTIVE_ACK: /* EY */
2706 case SCTP_HEARTBEAT_REQUEST:
2707 case SCTP_HEARTBEAT_ACK:
2708 case SCTP_ABORT_ASSOCIATION:
2710 case SCTP_SHUTDOWN_ACK:
2711 case SCTP_OPERATION_ERROR:
2712 case SCTP_COOKIE_ECHO:
2713 case SCTP_COOKIE_ACK:
2716 case SCTP_SHUTDOWN_COMPLETE:
2717 case SCTP_AUTHENTICATION:
2718 case SCTP_ASCONF_ACK:
2719 case SCTP_PACKET_DROPPED:
2720 case SCTP_STREAM_RESET:
2721 case SCTP_FORWARD_CUM_TSN:
2724 * Now, what do we do with KNOWN chunks that
2725 * are NOT in the right place?
2727 * For now, I do nothing but ignore them. We
2728 * may later want to add sysctl stuff to
2729 * switch out and do either an ABORT() or
2730 * possibly process them.
2732 if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2733 struct mbuf *op_err;
2735 op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2736 sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
2741 /* unknown chunk type, use bit rules */
2742 if (ch->ch.chunk_type & 0x40) {
2743 /* Add a error report to the queue */
2745 struct sctp_paramhdr *phd;
2747 merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2749 phd = mtod(merr, struct sctp_paramhdr *);
2751 * We cheat and use param
2752 * type since we did not
2753 * bother to define a error
2754 * cause struct. They are
2755 * the same basic format
2756 * with different names.
2759 htons(SCTP_CAUSE_UNRECOG_CHUNK);
2761 htons(chk_length + sizeof(*phd));
2762 SCTP_BUF_LEN(merr) = sizeof(*phd);
2763 SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2764 SCTP_SIZE32(chk_length),
2766 if (SCTP_BUF_NEXT(merr)) {
2767 sctp_queue_op_err(stcb, merr);
2773 if ((ch->ch.chunk_type & 0x80) == 0) {
2774 /* discard the rest of this packet */
2776 } /* else skip this bad chunk and
2779 }; /* switch of chunk type */
2781 *offset += SCTP_SIZE32(chk_length);
2782 if ((*offset >= length) || stop_proc) {
2783 /* no more data left in the mbuf chain */
2787 ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2788 sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2798 * we need to report rwnd overrun drops.
2800 sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2804 * Did we get data, if so update the time for auto-close and
2805 * give peer credit for being alive.
2807 SCTP_STAT_INCR(sctps_recvpktwithdata);
2808 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2809 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2810 stcb->asoc.overall_error_count,
2812 SCTP_FROM_SCTP_INDATA,
2815 stcb->asoc.overall_error_count = 0;
2816 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2818 /* now service all of the reassm queue if needed */
2819 if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2820 sctp_service_queues(stcb, asoc);
2822 if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2823 /* Assure that we ack right away */
2824 stcb->asoc.send_sack = 1;
2826 /* Start a sack timer or QUEUE a SACK for sending */
2827 sctp_sack_check(stcb, was_a_gap, &abort_flag);
2835 sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
2836 uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
2838 uint32_t * biggest_newly_acked_tsn,
2839 uint32_t * this_sack_lowest_newack,
2840 int *ecn_seg_sums, int *rto_ok)
2842 struct sctp_tmit_chunk *tp1;
2843 unsigned int theTSN;
2844 int j, wake_him = 0, circled = 0;
2846 /* Recover the tp1 we last saw */
2849 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2851 for (j = frag_strt; j <= frag_end; j++) {
2852 theTSN = j + last_tsn;
2854 if (tp1->rec.data.doing_fast_retransmit)
2858 * CMT: CUCv2 algorithm. For each TSN being
2859 * processed from the sent queue, track the
2860 * next expected pseudo-cumack, or
2861 * rtx_pseudo_cumack, if required. Separate
2862 * cumack trackers for first transmissions,
2863 * and retransmissions.
2865 if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2866 (tp1->snd_count == 1)) {
2867 tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2868 tp1->whoTo->find_pseudo_cumack = 0;
2870 if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2871 (tp1->snd_count > 1)) {
2872 tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2873 tp1->whoTo->find_rtx_pseudo_cumack = 0;
2875 if (tp1->rec.data.TSN_seq == theTSN) {
2876 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2878 * must be held until
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 (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
2890 *biggest_newly_acked_tsn)) {
2891 *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2894 * CMT: SFR algo (and HTNA) - set
2895 * saw_newack to 1 for dest being
2896 * newly acked. update
2897 * this_sack_highest_newack if
2900 if (tp1->rec.data.chunk_was_revoked == 0)
2901 tp1->whoTo->saw_newack = 1;
2903 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
2904 tp1->whoTo->this_sack_highest_newack)) {
2905 tp1->whoTo->this_sack_highest_newack =
2906 tp1->rec.data.TSN_seq;
2909 * CMT DAC algo: also update
2910 * this_sack_lowest_newack
2912 if (*this_sack_lowest_newack == 0) {
2913 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2914 sctp_log_sack(*this_sack_lowest_newack,
2916 tp1->rec.data.TSN_seq,
2919 SCTP_LOG_TSN_ACKED);
2921 *this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2924 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
2925 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
2926 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
2927 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
2928 * Separate pseudo_cumack trackers for first transmissions and
2931 if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2932 if (tp1->rec.data.chunk_was_revoked == 0) {
2933 tp1->whoTo->new_pseudo_cumack = 1;
2935 tp1->whoTo->find_pseudo_cumack = 1;
2937 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
2938 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2940 if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2941 if (tp1->rec.data.chunk_was_revoked == 0) {
2942 tp1->whoTo->new_pseudo_cumack = 1;
2944 tp1->whoTo->find_rtx_pseudo_cumack = 1;
2946 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2947 sctp_log_sack(*biggest_newly_acked_tsn,
2949 tp1->rec.data.TSN_seq,
2952 SCTP_LOG_TSN_ACKED);
2954 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2955 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
2956 tp1->whoTo->flight_size,
2958 (uintptr_t) tp1->whoTo,
2959 tp1->rec.data.TSN_seq);
2961 sctp_flight_size_decrease(tp1);
2962 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2963 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2966 sctp_total_flight_decrease(stcb, tp1);
2968 tp1->whoTo->net_ack += tp1->send_size;
2969 if (tp1->snd_count < 2) {
2971 * True non-retransmited chunk
2973 tp1->whoTo->net_ack2 += tp1->send_size;
2981 sctp_calculate_rto(stcb,
2984 &tp1->sent_rcv_time,
2985 sctp_align_safe_nocopy,
2986 SCTP_RTT_FROM_DATA);
2989 if (tp1->whoTo->rto_needed == 0) {
2990 tp1->whoTo->rto_needed = 1;
2996 if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
2997 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
2998 stcb->asoc.this_sack_highest_gap)) {
2999 stcb->asoc.this_sack_highest_gap =
3000 tp1->rec.data.TSN_seq;
3002 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3003 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
3004 #ifdef SCTP_AUDITING_ENABLED
3005 sctp_audit_log(0xB2,
3006 (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
3011 * All chunks NOT UNSENT fall through here and are marked
3012 * (leave PR-SCTP ones that are to skip alone though)
3014 if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
3015 tp1->sent = SCTP_DATAGRAM_MARKED;
3017 if (tp1->rec.data.chunk_was_revoked) {
3018 /* deflate the cwnd */
3019 tp1->whoTo->cwnd -= tp1->book_size;
3020 tp1->rec.data.chunk_was_revoked = 0;
3022 /* NR Sack code here */
3029 sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
3030 sctp_m_freem(tp1->data);
3037 } /* if (tp1->TSN_seq == theTSN) */
3038 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
3041 tp1 = TAILQ_NEXT(tp1, sctp_next);
3042 if ((tp1 == NULL) && (circled == 0)) {
3044 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3046 } /* end while (tp1) */
3049 tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3051 /* In case the fragments were not in order we must reset */
3052 } /* end for (j = fragStart */
3054 return (wake_him); /* Return value only used for nr-sack */
3059 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3060 uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3061 uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3062 int num_seg, int num_nr_seg, int *ecn_seg_sums,
3065 struct sctp_gap_ack_block *frag, block;
3066 struct sctp_tmit_chunk *tp1;
3071 uint16_t frag_strt, frag_end, prev_frag_end;
3073 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3077 for (i = 0; i < (num_seg + num_nr_seg); i++) {
3080 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3082 frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3083 sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3084 *offset += sizeof(block);
3086 return (chunk_freed);
3088 frag_strt = ntohs(frag->start);
3089 frag_end = ntohs(frag->end);
3091 if (frag_strt > frag_end) {
3092 /* This gap report is malformed, skip it. */
3095 if (frag_strt <= prev_frag_end) {
3096 /* This gap report is not in order, so restart. */
3097 tp1 = TAILQ_FIRST(&asoc->sent_queue);
3099 if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3100 *biggest_tsn_acked = last_tsn + frag_end;
3107 if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3108 non_revocable, &num_frs, biggest_newly_acked_tsn,
3109 this_sack_lowest_newack, ecn_seg_sums, rto_ok)) {
3112 prev_frag_end = frag_end;
3114 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3116 sctp_log_fr(*biggest_tsn_acked,
3117 *biggest_newly_acked_tsn,
3118 last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3120 return (chunk_freed);
3124 sctp_check_for_revoked(struct sctp_tcb *stcb,
3125 struct sctp_association *asoc, uint32_t cumack,
3126 uint32_t biggest_tsn_acked)
3128 struct sctp_tmit_chunk *tp1;
3129 int tot_revoked = 0;
3131 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3132 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3134 * ok this guy is either ACK or MARKED. If it is
3135 * ACKED it has been previously acked but not this
3136 * time i.e. revoked. If it is MARKED it was ACK'ed
3139 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3142 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3143 /* it has been revoked */
3144 tp1->sent = SCTP_DATAGRAM_SENT;
3145 tp1->rec.data.chunk_was_revoked = 1;
3147 * We must add this stuff back in to assure
3148 * timers and such get started.
3150 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3151 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3152 tp1->whoTo->flight_size,
3154 (uintptr_t) tp1->whoTo,
3155 tp1->rec.data.TSN_seq);
3157 sctp_flight_size_increase(tp1);
3158 sctp_total_flight_increase(stcb, tp1);
3160 * We inflate the cwnd to compensate for our
3161 * artificial inflation of the flight_size.
3163 tp1->whoTo->cwnd += tp1->book_size;
3165 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3166 sctp_log_sack(asoc->last_acked_seq,
3168 tp1->rec.data.TSN_seq,
3171 SCTP_LOG_TSN_REVOKED);
3173 } else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3174 /* it has been re-acked in this SACK */
3175 tp1->sent = SCTP_DATAGRAM_ACKED;
3178 if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3185 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3186 uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3188 struct sctp_tmit_chunk *tp1;
3189 int strike_flag = 0;
3191 int tot_retrans = 0;
3192 uint32_t sending_seq;
3193 struct sctp_nets *net;
3194 int num_dests_sacked = 0;
3197 * select the sending_seq, this is either the next thing ready to be
3198 * sent but not transmitted, OR, the next seq we assign.
3200 tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3202 sending_seq = asoc->sending_seq;
3204 sending_seq = tp1->rec.data.TSN_seq;
3207 /* CMT DAC algo: finding out if SACK is a mixed SACK */
3208 if ((asoc->sctp_cmt_on_off > 0) &&
3209 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3210 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3211 if (net->saw_newack)
3215 if (stcb->asoc.peer_supports_prsctp) {
3216 (void)SCTP_GETTIME_TIMEVAL(&now);
3218 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3220 if (tp1->no_fr_allowed) {
3221 /* this one had a timeout or something */
3224 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3225 if (tp1->sent < SCTP_DATAGRAM_RESEND)
3226 sctp_log_fr(biggest_tsn_newly_acked,
3227 tp1->rec.data.TSN_seq,
3229 SCTP_FR_LOG_CHECK_STRIKE);
3231 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3232 tp1->sent == SCTP_DATAGRAM_UNSENT) {
3236 if (stcb->asoc.peer_supports_prsctp) {
3237 if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3238 /* Is it expired? */
3239 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3240 /* Yes so drop it */
3241 if (tp1->data != NULL) {
3242 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3243 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3244 SCTP_SO_NOT_LOCKED);
3250 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3251 /* we are beyond the tsn in the sack */
3254 if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3255 /* either a RESEND, ACKED, or MARKED */
3257 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3258 /* Continue strikin FWD-TSN chunks */
3259 tp1->rec.data.fwd_tsn_cnt++;
3264 * CMT : SFR algo (covers part of DAC and HTNA as well)
3266 if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3268 * No new acks were receieved for data sent to this
3269 * dest. Therefore, according to the SFR algo for
3270 * CMT, no data sent to this dest can be marked for
3271 * FR using this SACK.
3274 } else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
3275 tp1->whoTo->this_sack_highest_newack)) {
3277 * CMT: New acks were receieved for data sent to
3278 * this dest. But no new acks were seen for data
3279 * sent after tp1. Therefore, according to the SFR
3280 * algo for CMT, tp1 cannot be marked for FR using
3281 * this SACK. This step covers part of the DAC algo
3282 * and the HTNA algo as well.
3287 * Here we check to see if we were have already done a FR
3288 * and if so we see if the biggest TSN we saw in the sack is
3289 * smaller than the recovery point. If so we don't strike
3290 * the tsn... otherwise we CAN strike the TSN.
3293 * @@@ JRI: Check for CMT if (accum_moved &&
3294 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3297 if (accum_moved && asoc->fast_retran_loss_recovery) {
3299 * Strike the TSN if in fast-recovery and cum-ack
3302 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3303 sctp_log_fr(biggest_tsn_newly_acked,
3304 tp1->rec.data.TSN_seq,
3306 SCTP_FR_LOG_STRIKE_CHUNK);
3308 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3311 if ((asoc->sctp_cmt_on_off > 0) &&
3312 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3314 * CMT DAC algorithm: If SACK flag is set to
3315 * 0, then lowest_newack test will not pass
3316 * because it would have been set to the
3317 * cumack earlier. If not already to be
3318 * rtx'd, If not a mixed sack and if tp1 is
3319 * not between two sacked TSNs, then mark by
3320 * one more. NOTE that we are marking by one
3321 * additional time since the SACK DAC flag
3322 * indicates that two packets have been
3323 * received after this missing TSN.
3325 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3326 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3327 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3328 sctp_log_fr(16 + num_dests_sacked,
3329 tp1->rec.data.TSN_seq,
3331 SCTP_FR_LOG_STRIKE_CHUNK);
3336 } else if ((tp1->rec.data.doing_fast_retransmit) &&
3337 (asoc->sctp_cmt_on_off == 0)) {
3339 * For those that have done a FR we must take
3340 * special consideration if we strike. I.e the
3341 * biggest_newly_acked must be higher than the
3342 * sending_seq at the time we did the FR.
3345 #ifdef SCTP_FR_TO_ALTERNATE
3347 * If FR's go to new networks, then we must only do
3348 * this for singly homed asoc's. However if the FR's
3349 * go to the same network (Armando's work) then its
3350 * ok to FR multiple times.
3358 if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3359 tp1->rec.data.fast_retran_tsn)) {
3361 * Strike the TSN, since this ack is
3362 * beyond where things were when we
3365 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3366 sctp_log_fr(biggest_tsn_newly_acked,
3367 tp1->rec.data.TSN_seq,
3369 SCTP_FR_LOG_STRIKE_CHUNK);
3371 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3375 if ((asoc->sctp_cmt_on_off > 0) &&
3376 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3378 * CMT DAC algorithm: If
3379 * SACK flag is set to 0,
3380 * then lowest_newack test
3381 * will not pass because it
3382 * would have been set to
3383 * the cumack earlier. If
3384 * not already to be rtx'd,
3385 * If not a mixed sack and
3386 * if tp1 is not between two
3387 * sacked TSNs, then mark by
3388 * one more. NOTE that we
3389 * are marking by one
3390 * additional time since the
3391 * SACK DAC flag indicates
3392 * that two packets have
3393 * been received after this
3396 if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3397 (num_dests_sacked == 1) &&
3398 SCTP_TSN_GT(this_sack_lowest_newack,
3399 tp1->rec.data.TSN_seq)) {
3400 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3401 sctp_log_fr(32 + num_dests_sacked,
3402 tp1->rec.data.TSN_seq,
3404 SCTP_FR_LOG_STRIKE_CHUNK);
3406 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3414 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3417 } else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
3418 biggest_tsn_newly_acked)) {
3420 * We don't strike these: This is the HTNA
3421 * algorithm i.e. we don't strike If our TSN is
3422 * larger than the Highest TSN Newly Acked.
3426 /* Strike the TSN */
3427 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3428 sctp_log_fr(biggest_tsn_newly_acked,
3429 tp1->rec.data.TSN_seq,
3431 SCTP_FR_LOG_STRIKE_CHUNK);
3433 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3436 if ((asoc->sctp_cmt_on_off > 0) &&
3437 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3439 * CMT DAC algorithm: If SACK flag is set to
3440 * 0, then lowest_newack test will not pass
3441 * because it would have been set to the
3442 * cumack earlier. If not already to be
3443 * rtx'd, If not a mixed sack and if tp1 is
3444 * not between two sacked TSNs, then mark by
3445 * one more. NOTE that we are marking by one
3446 * additional time since the SACK DAC flag
3447 * indicates that two packets have been
3448 * received after this missing TSN.
3450 if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3451 SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3452 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3453 sctp_log_fr(48 + num_dests_sacked,
3454 tp1->rec.data.TSN_seq,
3456 SCTP_FR_LOG_STRIKE_CHUNK);
3462 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3463 struct sctp_nets *alt;
3465 /* fix counts and things */
3466 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3467 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3468 (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3470 (uintptr_t) tp1->whoTo,
3471 tp1->rec.data.TSN_seq);
3474 tp1->whoTo->net_ack++;
3475 sctp_flight_size_decrease(tp1);
3476 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3477 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3481 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3482 sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3483 asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3485 /* add back to the rwnd */
3486 asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3488 /* remove from the total flight */
3489 sctp_total_flight_decrease(stcb, tp1);
3491 if ((stcb->asoc.peer_supports_prsctp) &&
3492 (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3494 * Has it been retransmitted tv_sec times? -
3495 * we store the retran count there.
3497 if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3498 /* Yes, so drop it */
3499 if (tp1->data != NULL) {
3500 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3501 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3502 SCTP_SO_NOT_LOCKED);
3504 /* Make sure to flag we had a FR */
3505 tp1->whoTo->net_ack++;
3509 /* printf("OK, we are now ready to FR this guy\n"); */
3510 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3511 sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3515 /* This is a subsequent FR */
3516 SCTP_STAT_INCR(sctps_sendmultfastretrans);
3518 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3519 if (asoc->sctp_cmt_on_off > 0) {
3521 * CMT: Using RTX_SSTHRESH policy for CMT.
3522 * If CMT is being used, then pick dest with
3523 * largest ssthresh for any retransmission.
3525 tp1->no_fr_allowed = 1;
3527 /* sa_ignore NO_NULL_CHK */
3528 if (asoc->sctp_cmt_pf > 0) {
3530 * JRS 5/18/07 - If CMT PF is on,
3531 * use the PF version of
3534 alt = sctp_find_alternate_net(stcb, alt, 2);
3537 * JRS 5/18/07 - If only CMT is on,
3538 * use the CMT version of
3541 /* sa_ignore NO_NULL_CHK */
3542 alt = sctp_find_alternate_net(stcb, alt, 1);
3548 * CUCv2: If a different dest is picked for
3549 * the retransmission, then new
3550 * (rtx-)pseudo_cumack needs to be tracked
3551 * for orig dest. Let CUCv2 track new (rtx-)
3552 * pseudo-cumack always.
3555 tp1->whoTo->find_pseudo_cumack = 1;
3556 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3558 } else {/* CMT is OFF */
3560 #ifdef SCTP_FR_TO_ALTERNATE
3561 /* Can we find an alternate? */
3562 alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3565 * default behavior is to NOT retransmit
3566 * FR's to an alternate. Armando Caro's
3567 * paper details why.
3573 tp1->rec.data.doing_fast_retransmit = 1;
3575 /* mark the sending seq for possible subsequent FR's */
3577 * printf("Marking TSN for FR new value %x\n",
3578 * (uint32_t)tpi->rec.data.TSN_seq);
3580 if (TAILQ_EMPTY(&asoc->send_queue)) {
3582 * If the queue of send is empty then its
3583 * the next sequence number that will be
3584 * assigned so we subtract one from this to
3585 * get the one we last sent.
3587 tp1->rec.data.fast_retran_tsn = sending_seq;
3590 * If there are chunks on the send queue
3591 * (unsent data that has made it from the
3592 * stream queues but not out the door, we
3593 * take the first one (which will have the
3594 * lowest TSN) and subtract one to get the
3597 struct sctp_tmit_chunk *ttt;
3599 ttt = TAILQ_FIRST(&asoc->send_queue);
3600 tp1->rec.data.fast_retran_tsn =
3601 ttt->rec.data.TSN_seq;
3606 * this guy had a RTO calculation pending on
3609 if (tp1->whoTo->rto_needed == 0) {
3610 tp1->whoTo->rto_needed = 1;
3614 if (alt != tp1->whoTo) {
3615 /* yes, there is an alternate. */
3616 sctp_free_remote_addr(tp1->whoTo);
3617 /* sa_ignore FREED_MEMORY */
3619 atomic_add_int(&alt->ref_count, 1);
3625 struct sctp_tmit_chunk *
3626 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3627 struct sctp_association *asoc)
3629 struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3633 if (asoc->peer_supports_prsctp == 0) {
3636 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3637 if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3638 tp1->sent != SCTP_DATAGRAM_RESEND) {
3639 /* no chance to advance, out of here */
3642 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
3643 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3644 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
3645 asoc->advanced_peer_ack_point,
3646 tp1->rec.data.TSN_seq, 0, 0);
3649 if (!PR_SCTP_ENABLED(tp1->flags)) {
3651 * We can't fwd-tsn past any that are reliable aka
3652 * retransmitted until the asoc fails.
3657 (void)SCTP_GETTIME_TIMEVAL(&now);
3661 * now we got a chunk which is marked for another
3662 * retransmission to a PR-stream but has run out its chances
3663 * already maybe OR has been marked to skip now. Can we skip
3664 * it if its a resend?
3666 if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3667 (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3669 * Now is this one marked for resend and its time is
3672 if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3673 /* Yes so drop it */
3675 (void)sctp_release_pr_sctp_chunk(stcb, tp1,
3676 (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3677 SCTP_SO_NOT_LOCKED);
3681 * No, we are done when hit one for resend
3682 * whos time as not expired.
3688 * Ok now if this chunk is marked to drop it we can clean up
3689 * the chunk, advance our peer ack point and we can check
3692 if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3693 /* advance PeerAckPoint goes forward */
3694 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3695 asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3697 } else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
3698 /* No update but we do save the chk */
3703 * If it is still in RESEND we can advance no
3713 sctp_fs_audit(struct sctp_association *asoc)
3715 struct sctp_tmit_chunk *chk;
3716 int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3717 int entry_flight, entry_cnt, ret;
3719 entry_flight = asoc->total_flight;
3720 entry_cnt = asoc->total_flight_count;
3723 if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
3726 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3727 if (chk->sent < SCTP_DATAGRAM_RESEND) {
3728 printf("Chk TSN:%u size:%d inflight cnt:%d\n",
3729 chk->rec.data.TSN_seq,
3734 } else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3736 } else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3738 } else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3745 if ((inflight > 0) || (inbetween > 0)) {
3747 panic("Flight size-express incorrect? \n");
3749 printf("asoc->total_flight:%d cnt:%d\n",
3750 entry_flight, entry_cnt);
3752 SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
3753 inflight, inbetween, resend, above, acked);
3762 sctp_window_probe_recovery(struct sctp_tcb *stcb,
3763 struct sctp_association *asoc,
3764 struct sctp_nets *net,
3765 struct sctp_tmit_chunk *tp1)
3767 tp1->window_probe = 0;
3768 if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3769 /* TSN's skipped we do NOT move back. */
3770 sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3771 tp1->whoTo->flight_size,
3773 (uintptr_t) tp1->whoTo,
3774 tp1->rec.data.TSN_seq);
3777 /* First setup this by shrinking flight */
3778 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3779 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3782 sctp_flight_size_decrease(tp1);
3783 sctp_total_flight_decrease(stcb, tp1);
3784 /* Now mark for resend */
3785 tp1->sent = SCTP_DATAGRAM_RESEND;
3786 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3788 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3789 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3790 tp1->whoTo->flight_size,
3792 (uintptr_t) tp1->whoTo,
3793 tp1->rec.data.TSN_seq);
3798 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3799 uint32_t rwnd, int *abort_now, int ecne_seen)
3801 struct sctp_nets *net;
3802 struct sctp_association *asoc;
3803 struct sctp_tmit_chunk *tp1, *tp2;
3805 int win_probe_recovery = 0;
3806 int win_probe_recovered = 0;
3807 int j, done_once = 0;
3810 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3811 sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3812 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3814 SCTP_TCB_LOCK_ASSERT(stcb);
3815 #ifdef SCTP_ASOCLOG_OF_TSNS
3816 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3817 stcb->asoc.cumack_log_at++;
3818 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3819 stcb->asoc.cumack_log_at = 0;
3823 old_rwnd = asoc->peers_rwnd;
3824 if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
3827 } else if (asoc->last_acked_seq == cumack) {
3828 /* Window update sack */
3829 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3830 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3831 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3832 /* SWS sender side engages */
3833 asoc->peers_rwnd = 0;
3835 if (asoc->peers_rwnd > old_rwnd) {
3840 /* First setup for CC stuff */
3841 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3842 if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3843 /* Drag along the window_tsn for cwr's */
3844 net->cwr_window_tsn = cumack;
3846 net->prev_cwnd = net->cwnd;
3851 * CMT: Reset CUC and Fast recovery algo variables before
3854 net->new_pseudo_cumack = 0;
3855 net->will_exit_fast_recovery = 0;
3856 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3857 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3860 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3863 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3864 tp1 = TAILQ_LAST(&asoc->sent_queue,
3865 sctpchunk_listhead);
3866 send_s = tp1->rec.data.TSN_seq + 1;
3868 send_s = asoc->sending_seq;
3870 if (SCTP_TSN_GE(cumack, send_s)) {
3876 panic("Impossible sack 1");
3881 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3882 0, M_DONTWAIT, 1, MT_DATA);
3884 struct sctp_paramhdr *ph;
3887 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3889 ph = mtod(oper, struct sctp_paramhdr *);
3890 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3891 ph->param_length = htons(SCTP_BUF_LEN(oper));
3892 ippp = (uint32_t *) (ph + 1);
3893 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3895 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3896 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3901 asoc->this_sack_highest_gap = cumack;
3902 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3903 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3904 stcb->asoc.overall_error_count,
3906 SCTP_FROM_SCTP_INDATA,
3909 stcb->asoc.overall_error_count = 0;
3910 if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3911 /* process the new consecutive TSN first */
3912 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3913 if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
3914 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3915 printf("Warning, an unsent is now acked?\n");
3917 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3919 * If it is less than ACKED, it is
3920 * now no-longer in flight. Higher
3921 * values may occur during marking
3923 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3924 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3925 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3926 tp1->whoTo->flight_size,
3928 (uintptr_t) tp1->whoTo,
3929 tp1->rec.data.TSN_seq);
3931 sctp_flight_size_decrease(tp1);
3932 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3933 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3936 /* sa_ignore NO_NULL_CHK */
3937 sctp_total_flight_decrease(stcb, tp1);
3939 tp1->whoTo->net_ack += tp1->send_size;
3940 if (tp1->snd_count < 2) {
3942 * True non-retransmited
3945 tp1->whoTo->net_ack2 +=
3948 /* update RTO too? */
3957 sctp_calculate_rto(stcb,
3959 &tp1->sent_rcv_time,
3960 sctp_align_safe_nocopy,
3961 SCTP_RTT_FROM_DATA);
3964 if (tp1->whoTo->rto_needed == 0) {
3965 tp1->whoTo->rto_needed = 1;
3971 * CMT: CUCv2 algorithm. From the
3972 * cumack'd TSNs, for each TSN being
3973 * acked for the first time, set the
3974 * following variables for the
3975 * corresp destination.
3976 * new_pseudo_cumack will trigger a
3978 * find_(rtx_)pseudo_cumack will
3979 * trigger search for the next
3980 * expected (rtx-)pseudo-cumack.
3982 tp1->whoTo->new_pseudo_cumack = 1;
3983 tp1->whoTo->find_pseudo_cumack = 1;
3984 tp1->whoTo->find_rtx_pseudo_cumack = 1;
3986 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3987 /* sa_ignore NO_NULL_CHK */
3988 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
3991 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3992 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3994 if (tp1->rec.data.chunk_was_revoked) {
3995 /* deflate the cwnd */
3996 tp1->whoTo->cwnd -= tp1->book_size;
3997 tp1->rec.data.chunk_was_revoked = 0;
3999 tp1->sent = SCTP_DATAGRAM_ACKED;
4000 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4002 /* sa_ignore NO_NULL_CHK */
4003 sctp_free_bufspace(stcb, asoc, tp1, 1);
4004 sctp_m_freem(tp1->data);
4007 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4008 sctp_log_sack(asoc->last_acked_seq,
4010 tp1->rec.data.TSN_seq,
4013 SCTP_LOG_FREE_SENT);
4015 asoc->sent_queue_cnt--;
4016 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4023 /* sa_ignore NO_NULL_CHK */
4024 if (stcb->sctp_socket) {
4025 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4029 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4030 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4031 /* sa_ignore NO_NULL_CHK */
4032 sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4034 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4035 so = SCTP_INP_SO(stcb->sctp_ep);
4036 atomic_add_int(&stcb->asoc.refcnt, 1);
4037 SCTP_TCB_UNLOCK(stcb);
4038 SCTP_SOCKET_LOCK(so, 1);
4039 SCTP_TCB_LOCK(stcb);
4040 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4041 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4042 /* assoc was freed while we were unlocked */
4043 SCTP_SOCKET_UNLOCK(so, 1);
4047 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4048 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4049 SCTP_SOCKET_UNLOCK(so, 1);
4052 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4053 sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4057 /* JRS - Use the congestion control given in the CC module */
4058 if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4059 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4060 if (net->net_ack2 > 0) {
4062 * Karn's rule applies to clearing error
4063 * count, this is optional.
4065 net->error_count = 0;
4066 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4067 /* addr came good */
4068 net->dest_state |= SCTP_ADDR_REACHABLE;
4069 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4070 SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
4072 if (net == stcb->asoc.primary_destination) {
4073 if (stcb->asoc.alternate) {
4075 * release the alternate,
4078 sctp_free_remote_addr(stcb->asoc.alternate);
4079 stcb->asoc.alternate = NULL;
4082 if (net->dest_state & SCTP_ADDR_PF) {
4083 net->dest_state &= ~SCTP_ADDR_PF;
4084 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4085 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4086 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4087 /* Done with this net */
4090 /* restore any doubled timers */
4091 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4092 if (net->RTO < stcb->asoc.minrto) {
4093 net->RTO = stcb->asoc.minrto;
4095 if (net->RTO > stcb->asoc.maxrto) {
4096 net->RTO = stcb->asoc.maxrto;
4100 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4102 asoc->last_acked_seq = cumack;
4104 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4105 /* nothing left in-flight */
4106 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4107 net->flight_size = 0;
4108 net->partial_bytes_acked = 0;
4110 asoc->total_flight = 0;
4111 asoc->total_flight_count = 0;
4114 asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4115 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4116 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4117 /* SWS sender side engages */
4118 asoc->peers_rwnd = 0;
4120 if (asoc->peers_rwnd > old_rwnd) {
4121 win_probe_recovery = 1;
4123 /* Now assure a timer where data is queued at */
4126 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4129 if (win_probe_recovery && (net->window_probe)) {
4130 win_probe_recovered = 1;
4132 * Find first chunk that was used with window probe
4133 * and clear the sent
4135 /* sa_ignore FREED_MEMORY */
4136 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4137 if (tp1->window_probe) {
4138 /* move back to data send queue */
4139 sctp_window_probe_recovery(stcb, asoc, net, tp1);
4144 if (net->RTO == 0) {
4145 to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4147 to_ticks = MSEC_TO_TICKS(net->RTO);
4149 if (net->flight_size) {
4151 (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4152 sctp_timeout_handler, &net->rxt_timer);
4153 if (net->window_probe) {
4154 net->window_probe = 0;
4157 if (net->window_probe) {
4159 * In window probes we must assure a timer
4160 * is still running there
4162 net->window_probe = 0;
4163 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4164 SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4165 sctp_timeout_handler, &net->rxt_timer);
4167 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4168 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4170 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4175 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4176 (asoc->sent_queue_retran_cnt == 0) &&
4177 (win_probe_recovered == 0) &&
4180 * huh, this should not happen unless all packets are
4181 * PR-SCTP and marked to skip of course.
4183 if (sctp_fs_audit(asoc)) {
4184 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4185 net->flight_size = 0;
4187 asoc->total_flight = 0;
4188 asoc->total_flight_count = 0;
4189 asoc->sent_queue_retran_cnt = 0;
4190 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4191 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4192 sctp_flight_size_increase(tp1);
4193 sctp_total_flight_increase(stcb, tp1);
4194 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4195 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4202 /**********************************/
4203 /* Now what about shutdown issues */
4204 /**********************************/
4205 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4206 /* nothing left on sendqueue.. consider done */
4208 if ((asoc->stream_queue_cnt == 1) &&
4209 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4210 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4211 (asoc->locked_on_sending)
4213 struct sctp_stream_queue_pending *sp;
4216 * I may be in a state where we got all across.. but
4217 * cannot write more due to a shutdown... we abort
4218 * since the user did not indicate EOR in this case.
4219 * The sp will be cleaned during free of the asoc.
4221 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4223 if ((sp) && (sp->length == 0)) {
4224 /* Let cleanup code purge it */
4225 if (sp->msg_is_complete) {
4226 asoc->stream_queue_cnt--;
4228 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4229 asoc->locked_on_sending = NULL;
4230 asoc->stream_queue_cnt--;
4234 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4235 (asoc->stream_queue_cnt == 0)) {
4236 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4237 /* Need to abort here */
4243 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4244 0, M_DONTWAIT, 1, MT_DATA);
4246 struct sctp_paramhdr *ph;
4249 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4251 ph = mtod(oper, struct sctp_paramhdr *);
4252 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4253 ph->param_length = htons(SCTP_BUF_LEN(oper));
4254 ippp = (uint32_t *) (ph + 1);
4255 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4257 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4258 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4260 struct sctp_nets *netp;
4262 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4263 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4264 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4266 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4267 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4268 sctp_stop_timers_for_shutdown(stcb);
4269 if (asoc->alternate) {
4270 netp = asoc->alternate;
4272 netp = asoc->primary_destination;
4274 sctp_send_shutdown(stcb, netp);
4275 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4276 stcb->sctp_ep, stcb, netp);
4277 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4278 stcb->sctp_ep, stcb, netp);
4280 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4281 (asoc->stream_queue_cnt == 0)) {
4282 struct sctp_nets *netp;
4284 if (asoc->alternate) {
4285 netp = asoc->alternate;
4287 netp = asoc->primary_destination;
4289 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4292 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4293 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4294 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4295 sctp_send_shutdown_ack(stcb, netp);
4296 sctp_stop_timers_for_shutdown(stcb);
4297 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4298 stcb->sctp_ep, stcb, netp);
4301 /*********************************************/
4302 /* Here we perform PR-SCTP procedures */
4304 /*********************************************/
4305 /* C1. update advancedPeerAckPoint */
4306 if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4307 asoc->advanced_peer_ack_point = cumack;
4309 /* PR-Sctp issues need to be addressed too */
4310 if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4311 struct sctp_tmit_chunk *lchk;
4312 uint32_t old_adv_peer_ack_point;
4314 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4315 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4316 /* C3. See if we need to send a Fwd-TSN */
4317 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4319 * ISSUE with ECN, see FWD-TSN processing.
4321 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4322 send_forward_tsn(stcb, asoc);
4324 /* try to FR fwd-tsn's that get lost too */
4325 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
4326 send_forward_tsn(stcb, asoc);
4331 /* Assure a timer is up */
4332 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4333 stcb->sctp_ep, stcb, lchk->whoTo);
4336 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4337 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4339 stcb->asoc.peers_rwnd,
4340 stcb->asoc.total_flight,
4341 stcb->asoc.total_output_queue_size);
4346 sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
4347 struct sctp_tcb *stcb, struct sctp_nets *net_from,
4348 uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4349 int *abort_now, uint8_t flags,
4350 uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4352 struct sctp_association *asoc;
4353 struct sctp_tmit_chunk *tp1, *tp2;
4354 uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4355 uint32_t sav_cum_ack;
4356 uint16_t wake_him = 0;
4357 uint32_t send_s = 0;
4359 int accum_moved = 0;
4360 int will_exit_fast_recovery = 0;
4361 uint32_t a_rwnd, old_rwnd;
4362 int win_probe_recovery = 0;
4363 int win_probe_recovered = 0;
4364 struct sctp_nets *net = NULL;
4365 int ecn_seg_sums = 0;
4368 uint8_t reneged_all = 0;
4369 uint8_t cmt_dac_flag;
4372 * we take any chance we can to service our queues since we cannot
4373 * get awoken when the socket is read from :<
4376 * Now perform the actual SACK handling: 1) Verify that it is not an
4377 * old sack, if so discard. 2) If there is nothing left in the send
4378 * queue (cum-ack is equal to last acked) then you have a duplicate
4379 * too, update any rwnd change and verify no timers are running.
4380 * then return. 3) Process any new consequtive data i.e. cum-ack
4381 * moved process these first and note that it moved. 4) Process any
4382 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4383 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4384 * sync up flightsizes and things, stop all timers and also check
4385 * for shutdown_pending state. If so then go ahead and send off the
4386 * shutdown. If in shutdown recv, send off the shutdown-ack and
4387 * start that timer, Ret. 9) Strike any non-acked things and do FR
4388 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4389 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4390 * if in shutdown_recv state.
4392 SCTP_TCB_LOCK_ASSERT(stcb);
4394 this_sack_lowest_newack = 0;
4396 SCTP_STAT_INCR(sctps_slowpath_sack);
4398 cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
4399 #ifdef SCTP_ASOCLOG_OF_TSNS
4400 stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4401 stcb->asoc.cumack_log_at++;
4402 if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4403 stcb->asoc.cumack_log_at = 0;
4408 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4409 sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4410 rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4412 old_rwnd = stcb->asoc.peers_rwnd;
4413 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4414 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4415 stcb->asoc.overall_error_count,
4417 SCTP_FROM_SCTP_INDATA,
4420 stcb->asoc.overall_error_count = 0;
4422 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4423 sctp_log_sack(asoc->last_acked_seq,
4430 if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4432 uint32_t *dupdata, dblock;
4434 for (i = 0; i < num_dup; i++) {
4435 dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4436 sizeof(uint32_t), (uint8_t *) & dblock);
4437 if (dupdata == NULL) {
4440 sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4443 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4445 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4446 tp1 = TAILQ_LAST(&asoc->sent_queue,
4447 sctpchunk_listhead);
4448 send_s = tp1->rec.data.TSN_seq + 1;
4451 send_s = asoc->sending_seq;
4453 if (SCTP_TSN_GE(cum_ack, send_s)) {
4457 * no way, we have not even sent this TSN out yet.
4458 * Peer is hopelessly messed up with us.
4460 printf("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4463 printf("Got send_s from tsn:%x + 1 of tp1:%p\n",
4464 tp1->rec.data.TSN_seq, tp1);
4469 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4470 0, M_DONTWAIT, 1, MT_DATA);
4472 struct sctp_paramhdr *ph;
4475 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4477 ph = mtod(oper, struct sctp_paramhdr *);
4478 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4479 ph->param_length = htons(SCTP_BUF_LEN(oper));
4480 ippp = (uint32_t *) (ph + 1);
4481 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4483 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4484 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4488 /**********************/
4489 /* 1) check the range */
4490 /**********************/
4491 if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4492 /* acking something behind */
4495 sav_cum_ack = asoc->last_acked_seq;
4497 /* update the Rwnd of the peer */
4498 if (TAILQ_EMPTY(&asoc->sent_queue) &&
4499 TAILQ_EMPTY(&asoc->send_queue) &&
4500 (asoc->stream_queue_cnt == 0)) {
4501 /* nothing left on send/sent and strmq */
4502 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4503 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4504 asoc->peers_rwnd, 0, 0, a_rwnd);
4506 asoc->peers_rwnd = a_rwnd;
4507 if (asoc->sent_queue_retran_cnt) {
4508 asoc->sent_queue_retran_cnt = 0;
4510 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4511 /* SWS sender side engages */
4512 asoc->peers_rwnd = 0;
4514 /* stop any timers */
4515 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4516 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4517 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4518 net->partial_bytes_acked = 0;
4519 net->flight_size = 0;
4521 asoc->total_flight = 0;
4522 asoc->total_flight_count = 0;
4526 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4527 * things. The total byte count acked is tracked in netAckSz AND
4528 * netAck2 is used to track the total bytes acked that are un-
4529 * amibguious and were never retransmitted. We track these on a per
4530 * destination address basis.
4532 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4533 if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4534 /* Drag along the window_tsn for cwr's */
4535 net->cwr_window_tsn = cum_ack;
4537 net->prev_cwnd = net->cwnd;
4542 * CMT: Reset CUC and Fast recovery algo variables before
4545 net->new_pseudo_cumack = 0;
4546 net->will_exit_fast_recovery = 0;
4547 if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4548 (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4551 /* process the new consecutive TSN first */
4552 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4553 if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4554 if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4556 if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4558 * If it is less than ACKED, it is
4559 * now no-longer in flight. Higher
4560 * values may occur during marking
4562 if ((tp1->whoTo->dest_state &
4563 SCTP_ADDR_UNCONFIRMED) &&
4564 (tp1->snd_count < 2)) {
4566 * If there was no retran
4567 * and the address is
4568 * un-confirmed and we sent
4570 * sacked.. its confirmed,
4573 tp1->whoTo->dest_state &=
4574 ~SCTP_ADDR_UNCONFIRMED;
4576 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4577 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4578 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4579 tp1->whoTo->flight_size,
4581 (uintptr_t) tp1->whoTo,
4582 tp1->rec.data.TSN_seq);
4584 sctp_flight_size_decrease(tp1);
4585 sctp_total_flight_decrease(stcb, tp1);
4586 if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4587 (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4591 tp1->whoTo->net_ack += tp1->send_size;
4593 /* CMT SFR and DAC algos */
4594 this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4595 tp1->whoTo->saw_newack = 1;
4597 if (tp1->snd_count < 2) {
4599 * True non-retransmited
4602 tp1->whoTo->net_ack2 +=
4605 /* update RTO too? */
4609 sctp_calculate_rto(stcb,
4611 &tp1->sent_rcv_time,
4612 sctp_align_safe_nocopy,
4613 SCTP_RTT_FROM_DATA);
4616 if (tp1->whoTo->rto_needed == 0) {
4617 tp1->whoTo->rto_needed = 1;
4623 * CMT: CUCv2 algorithm. From the
4624 * cumack'd TSNs, for each TSN being
4625 * acked for the first time, set the
4626 * following variables for the
4627 * corresp destination.
4628 * new_pseudo_cumack will trigger a
4630 * find_(rtx_)pseudo_cumack will
4631 * trigger search for the next
4632 * expected (rtx-)pseudo-cumack.
4634 tp1->whoTo->new_pseudo_cumack = 1;
4635 tp1->whoTo->find_pseudo_cumack = 1;
4636 tp1->whoTo->find_rtx_pseudo_cumack = 1;
4639 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4640 sctp_log_sack(asoc->last_acked_seq,
4642 tp1->rec.data.TSN_seq,
4645 SCTP_LOG_TSN_ACKED);
4647 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4648 sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4651 if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4652 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4653 #ifdef SCTP_AUDITING_ENABLED
4654 sctp_audit_log(0xB3,
4655 (asoc->sent_queue_retran_cnt & 0x000000ff));
4658 if (tp1->rec.data.chunk_was_revoked) {
4659 /* deflate the cwnd */
4660 tp1->whoTo->cwnd -= tp1->book_size;
4661 tp1->rec.data.chunk_was_revoked = 0;
4663 tp1->sent = SCTP_DATAGRAM_ACKED;
4669 biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4670 /* always set this up to cum-ack */
4671 asoc->this_sack_highest_gap = last_tsn;
4673 if ((num_seg > 0) || (num_nr_seg > 0)) {
4676 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4677 * to be greater than the cumack. Also reset saw_newack to 0
4680 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4681 net->saw_newack = 0;
4682 net->this_sack_highest_newack = last_tsn;
4686 * thisSackHighestGap will increase while handling NEW
4687 * segments this_sack_highest_newack will increase while
4688 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4689 * used for CMT DAC algo. saw_newack will also change.
4691 if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4692 &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4693 num_seg, num_nr_seg, &ecn_seg_sums,
4697 if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4699 * validate the biggest_tsn_acked in the gap acks if
4700 * strict adherence is wanted.
4702 if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4704 * peer is either confused or we are under
4705 * attack. We must abort.
4707 printf("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4715 /*******************************************/
4716 /* cancel ALL T3-send timer if accum moved */
4717 /*******************************************/
4718 if (asoc->sctp_cmt_on_off > 0) {
4719 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4720 if (net->new_pseudo_cumack)
4721 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4723 SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4728 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4729 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4730 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4734 /********************************************/
4735 /* drop the acked chunks from the sentqueue */
4736 /********************************************/
4737 asoc->last_acked_seq = cum_ack;
4739 TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
4740 if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4743 if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4744 /* no more sent on list */
4745 printf("Warning, tp1->sent == %d and its now acked?\n",
4748 TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4749 if (tp1->pr_sctp_on) {
4750 if (asoc->pr_sctp_cnt != 0)
4751 asoc->pr_sctp_cnt--;
4753 asoc->sent_queue_cnt--;
4755 /* sa_ignore NO_NULL_CHK */
4756 sctp_free_bufspace(stcb, asoc, tp1, 1);
4757 sctp_m_freem(tp1->data);
4759 if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4760 asoc->sent_queue_cnt_removeable--;
4763 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4764 sctp_log_sack(asoc->last_acked_seq,
4766 tp1->rec.data.TSN_seq,
4769 SCTP_LOG_FREE_SENT);
4771 sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4774 if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
4776 panic("Warning flight size is postive and should be 0");
4778 SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4779 asoc->total_flight);
4781 asoc->total_flight = 0;
4783 /* sa_ignore NO_NULL_CHK */
4784 if ((wake_him) && (stcb->sctp_socket)) {
4785 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4789 SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4790 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4791 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
4793 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4794 so = SCTP_INP_SO(stcb->sctp_ep);
4795 atomic_add_int(&stcb->asoc.refcnt, 1);
4796 SCTP_TCB_UNLOCK(stcb);
4797 SCTP_SOCKET_LOCK(so, 1);
4798 SCTP_TCB_LOCK(stcb);
4799 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4800 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4801 /* assoc was freed while we were unlocked */
4802 SCTP_SOCKET_UNLOCK(so, 1);
4806 sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4807 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4808 SCTP_SOCKET_UNLOCK(so, 1);
4811 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4812 sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
4816 if (asoc->fast_retran_loss_recovery && accum_moved) {
4817 if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4818 /* Setup so we will exit RFC2582 fast recovery */
4819 will_exit_fast_recovery = 1;
4823 * Check for revoked fragments:
4825 * if Previous sack - Had no frags then we can't have any revoked if
4826 * Previous sack - Had frag's then - If we now have frags aka
4827 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4828 * some of them. else - The peer revoked all ACKED fragments, since
4829 * we had some before and now we have NONE.
4833 sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4834 asoc->saw_sack_with_frags = 1;
4835 } else if (asoc->saw_sack_with_frags) {
4836 int cnt_revoked = 0;
4838 /* Peer revoked all dg's marked or acked */
4839 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4840 if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4841 tp1->sent = SCTP_DATAGRAM_SENT;
4842 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4843 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4844 tp1->whoTo->flight_size,
4846 (uintptr_t) tp1->whoTo,
4847 tp1->rec.data.TSN_seq);
4849 sctp_flight_size_increase(tp1);
4850 sctp_total_flight_increase(stcb, tp1);
4851 tp1->rec.data.chunk_was_revoked = 1;
4853 * To ensure that this increase in
4854 * flightsize, which is artificial, does not
4855 * throttle the sender, we also increase the
4856 * cwnd artificially.
4858 tp1->whoTo->cwnd += tp1->book_size;
4865 asoc->saw_sack_with_frags = 0;
4868 asoc->saw_sack_with_nr_frags = 1;
4870 asoc->saw_sack_with_nr_frags = 0;
4872 /* JRS - Use the congestion control given in the CC module */
4873 if (ecne_seen == 0) {
4874 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4875 if (net->net_ack2 > 0) {
4877 * Karn's rule applies to clearing error
4878 * count, this is optional.
4880 net->error_count = 0;
4881 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4882 /* addr came good */
4883 net->dest_state |= SCTP_ADDR_REACHABLE;
4884 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
4885 SCTP_RECEIVED_SACK, (void *)net, SCTP_SO_NOT_LOCKED);
4887 if (net == stcb->asoc.primary_destination) {
4888 if (stcb->asoc.alternate) {
4890 * release the alternate,
4893 sctp_free_remote_addr(stcb->asoc.alternate);
4894 stcb->asoc.alternate = NULL;
4897 if (net->dest_state & SCTP_ADDR_PF) {
4898 net->dest_state &= ~SCTP_ADDR_PF;
4899 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4900 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4901 asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4902 /* Done with this net */
4905 /* restore any doubled timers */
4906 net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4907 if (net->RTO < stcb->asoc.minrto) {
4908 net->RTO = stcb->asoc.minrto;
4910 if (net->RTO > stcb->asoc.maxrto) {
4911 net->RTO = stcb->asoc.maxrto;
4915 asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4917 if (TAILQ_EMPTY(&asoc->sent_queue)) {
4918 /* nothing left in-flight */
4919 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4920 /* stop all timers */
4921 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4922 stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4923 net->flight_size = 0;
4924 net->partial_bytes_acked = 0;
4926 asoc->total_flight = 0;
4927 asoc->total_flight_count = 0;
4929 /**********************************/
4930 /* Now what about shutdown issues */
4931 /**********************************/
4932 if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4933 /* nothing left on sendqueue.. consider done */
4934 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4935 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4936 asoc->peers_rwnd, 0, 0, a_rwnd);
4938 asoc->peers_rwnd = a_rwnd;
4939 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4940 /* SWS sender side engages */
4941 asoc->peers_rwnd = 0;
4944 if ((asoc->stream_queue_cnt == 1) &&
4945 ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4946 (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4947 (asoc->locked_on_sending)
4949 struct sctp_stream_queue_pending *sp;
4952 * I may be in a state where we got all across.. but
4953 * cannot write more due to a shutdown... we abort
4954 * since the user did not indicate EOR in this case.
4956 sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4958 if ((sp) && (sp->length == 0)) {
4959 asoc->locked_on_sending = NULL;
4960 if (sp->msg_is_complete) {
4961 asoc->stream_queue_cnt--;
4963 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4964 asoc->stream_queue_cnt--;
4968 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4969 (asoc->stream_queue_cnt == 0)) {
4970 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4971 /* Need to abort here */
4977 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4978 0, M_DONTWAIT, 1, MT_DATA);
4980 struct sctp_paramhdr *ph;
4983 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4985 ph = mtod(oper, struct sctp_paramhdr *);
4986 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4987 ph->param_length = htons(SCTP_BUF_LEN(oper));
4988 ippp = (uint32_t *) (ph + 1);
4989 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4991 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4992 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4995 struct sctp_nets *netp;
4997 if (asoc->alternate) {
4998 netp = asoc->alternate;
5000 netp = asoc->primary_destination;
5002 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5003 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5004 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5006 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5007 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5008 sctp_stop_timers_for_shutdown(stcb);
5009 sctp_send_shutdown(stcb, netp);
5010 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5011 stcb->sctp_ep, stcb, netp);
5012 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5013 stcb->sctp_ep, stcb, netp);
5016 } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5017 (asoc->stream_queue_cnt == 0)) {
5018 struct sctp_nets *netp;
5020 if (asoc->alternate) {
5021 netp = asoc->alternate;
5023 netp = asoc->primary_destination;
5025 if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5028 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5029 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5030 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5031 sctp_send_shutdown_ack(stcb, netp);
5032 sctp_stop_timers_for_shutdown(stcb);
5033 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5034 stcb->sctp_ep, stcb, netp);
5039 * Now here we are going to recycle net_ack for a different use...
5042 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5047 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5048 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5049 * automatically ensure that.
5051 if ((asoc->sctp_cmt_on_off > 0) &&
5052 SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
5053 (cmt_dac_flag == 0)) {
5054 this_sack_lowest_newack = cum_ack;
5056 if ((num_seg > 0) || (num_nr_seg > 0)) {
5057 sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5058 biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5060 /* JRS - Use the congestion control given in the CC module */
5061 asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5063 /* Now are we exiting loss recovery ? */
5064 if (will_exit_fast_recovery) {
5065 /* Ok, we must exit fast recovery */
5066 asoc->fast_retran_loss_recovery = 0;
5068 if ((asoc->sat_t3_loss_recovery) &&
5069 SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
5070 /* end satellite t3 loss recovery */
5071 asoc->sat_t3_loss_recovery = 0;
5076 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5077 if (net->will_exit_fast_recovery) {
5078 /* Ok, we must exit fast recovery */
5079 net->fast_retran_loss_recovery = 0;
5083 /* Adjust and set the new rwnd value */
5084 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5085 sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5086 asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
5088 asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5089 (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5090 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5091 /* SWS sender side engages */
5092 asoc->peers_rwnd = 0;
5094 if (asoc->peers_rwnd > old_rwnd) {
5095 win_probe_recovery = 1;
5098 * Now we must setup so we have a timer up for anyone with
5104 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5105 if (win_probe_recovery && (net->window_probe)) {
5106 win_probe_recovered = 1;
5108 * Find first chunk that was used with
5109 * window probe and clear the event. Put
5110 * it back into the send queue as if has
5113 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5114 if (tp1->window_probe) {
5115 sctp_window_probe_recovery(stcb, asoc, net, tp1);
5120 if (net->flight_size) {
5122 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5123 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5124 stcb->sctp_ep, stcb, net);
5126 if (net->window_probe) {
5127 net->window_probe = 0;
5130 if (net->window_probe) {
5132 * In window probes we must assure a timer
5133 * is still running there
5135 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5136 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5137 stcb->sctp_ep, stcb, net);
5140 } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5141 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5143 SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5148 (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5149 (asoc->sent_queue_retran_cnt == 0) &&
5150 (win_probe_recovered == 0) &&
5153 * huh, this should not happen unless all packets are
5154 * PR-SCTP and marked to skip of course.
5156 if (sctp_fs_audit(asoc)) {
5157 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5158 net->flight_size = 0;
5160 asoc->total_flight = 0;
5161 asoc->total_flight_count = 0;
5162 asoc->sent_queue_retran_cnt = 0;
5163 TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5164 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5165 sctp_flight_size_increase(tp1);
5166 sctp_total_flight_increase(stcb, tp1);
5167 } else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5168 sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5175 /*********************************************/
5176 /* Here we perform PR-SCTP procedures */
5178 /*********************************************/
5179 /* C1. update advancedPeerAckPoint */
5180 if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5181 asoc->advanced_peer_ack_point = cum_ack;
5183 /* C2. try to further move advancedPeerAckPoint ahead */
5184 if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5185 struct sctp_tmit_chunk *lchk;
5186 uint32_t old_adv_peer_ack_point;
5188 old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5189 lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5190 /* C3. See if we need to send a Fwd-TSN */
5191 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5193 * ISSUE with ECN, see FWD-TSN processing.
5195 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
5196 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
5197 0xee, cum_ack, asoc->advanced_peer_ack_point,
5198 old_adv_peer_ack_point);
5200 if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5201 send_forward_tsn(stcb, asoc);
5203 /* try to FR fwd-tsn's that get lost too */
5204 if (lchk->rec.data.fwd_tsn_cnt >= 3) {
5205 send_forward_tsn(stcb, asoc);
5210 /* Assure a timer is up */
5211 sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5212 stcb->sctp_ep, stcb, lchk->whoTo);
5215 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5216 sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5218 stcb->asoc.peers_rwnd,
5219 stcb->asoc.total_flight,
5220 stcb->asoc.total_output_queue_size);
5225 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5226 struct sctp_nets *netp, int *abort_flag)
5229 uint32_t cum_ack, a_rwnd;
5231 cum_ack = ntohl(cp->cumulative_tsn_ack);
5232 /* Arrange so a_rwnd does NOT change */
5233 a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5235 /* Now call the express sack handling */
5236 sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5240 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5241 struct sctp_stream_in *strmin)
5243 struct sctp_queued_to_read *ctl, *nctl;
5244 struct sctp_association *asoc;
5248 tt = strmin->last_sequence_delivered;
5250 * First deliver anything prior to and including the stream no that
5253 TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
5254 if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
5255 /* this is deliverable now */
5256 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5257 /* subtract pending on streams */
5258 asoc->size_on_all_streams -= ctl->length;
5259 sctp_ucount_decr(asoc->cnt_on_all_streams);
5260 /* deliver it to at least the delivery-q */
5261 if (stcb->sctp_socket) {
5262 sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5263 sctp_add_to_readq(stcb->sctp_ep, stcb,
5265 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5268 /* no more delivery now. */
5273 * now we must deliver things in queue the normal way if any are
5276 tt = strmin->last_sequence_delivered + 1;
5277 TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
5278 if (tt == ctl->sinfo_ssn) {
5279 /* this is deliverable now */
5280 TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5281 /* subtract pending on streams */
5282 asoc->size_on_all_streams -= ctl->length;
5283 sctp_ucount_decr(asoc->cnt_on_all_streams);
5284 /* deliver it to at least the delivery-q */
5285 strmin->last_sequence_delivered = ctl->sinfo_ssn;
5286 if (stcb->sctp_socket) {
5287 sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5288 sctp_add_to_readq(stcb->sctp_ep, stcb,
5290 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5293 tt = strmin->last_sequence_delivered + 1;
5301 sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
5302 struct sctp_association *asoc,
5303 uint16_t stream, uint16_t seq)
5305 struct sctp_tmit_chunk *chk, *nchk;
5307 /* For each one on here see if we need to toss it */
5309 * For now large messages held on the reasmqueue that are complete
5310 * will be tossed too. We could in theory do more work to spin
5311 * through and stop after dumping one msg aka seeing the start of a
5312 * new msg at the head, and call the delivery function... to see if
5313 * it can be delivered... But for now we just dump everything on the
5316 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5318 * Do not toss it if on a different stream or marked for
5319 * unordered delivery in which case the stream sequence
5320 * number has no meaning.
5322 if ((chk->rec.data.stream_number != stream) ||
5323 ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
5326 if (chk->rec.data.stream_seq == seq) {
5327 /* It needs to be tossed */
5328 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5329 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
5330 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
5331 asoc->str_of_pdapi = chk->rec.data.stream_number;
5332 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
5333 asoc->fragment_flags = chk->rec.data.rcv_flags;
5335 asoc->size_on_reasm_queue -= chk->send_size;
5336 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5338 /* Clear up any stream problem */
5339 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
5340 SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5342 * We must dump forward this streams
5343 * sequence number if the chunk is not
5344 * unordered that is being skipped. There is
5345 * a chance that if the peer does not
5346 * include the last fragment in its FWD-TSN
5347 * we WILL have a problem here since you
5348 * would have a partial chunk in queue that
5349 * may not be deliverable. Also if a Partial
5350 * delivery API as started the user may get
5351 * a partial chunk. The next read returning
5352 * a new chunk... really ugly but I see no
5353 * way around it! Maybe a notify??
5355 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5358 sctp_m_freem(chk->data);
5361 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5362 } else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
5364 * If the stream_seq is > than the purging one, we
5374 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5375 struct sctp_forward_tsn_chunk *fwd,
5376 int *abort_flag, struct mbuf *m, int offset)
5378 /* The pr-sctp fwd tsn */
5380 * here we will perform all the data receiver side steps for
5381 * processing FwdTSN, as required in by pr-sctp draft:
5383 * Assume we get FwdTSN(x):
5385 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5386 * others we have 3) examine and update re-ordering queue on
5387 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5388 * report where we are.
5390 struct sctp_association *asoc;
5391 uint32_t new_cum_tsn, gap;
5392 unsigned int i, fwd_sz, cumack_set_flag, m_size;
5394 struct sctp_stream_in *strm;
5395 struct sctp_tmit_chunk *chk, *nchk;
5396 struct sctp_queued_to_read *ctl, *sv;
5398 cumack_set_flag = 0;
5400 if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5401 SCTPDBG(SCTP_DEBUG_INDATA1,
5402 "Bad size too small/big fwd-tsn\n");
5405 m_size = (stcb->asoc.mapping_array_size << 3);
5406 /*************************************************************/
5407 /* 1. Here we update local cumTSN and shift the bitmap array */
5408 /*************************************************************/
5409 new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5411 if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5412 /* Already got there ... */
5416 * now we know the new TSN is more advanced, let's find the actual
5419 SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
5420 asoc->cumulative_tsn = new_cum_tsn;
5421 if (gap >= m_size) {
5422 if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5426 * out of range (of single byte chunks in the rwnd I
5427 * give out). This must be an attacker.
5430 oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5431 0, M_DONTWAIT, 1, MT_DATA);
5433 struct sctp_paramhdr *ph;
5436 SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5437 (sizeof(uint32_t) * 3);
5438 ph = mtod(oper, struct sctp_paramhdr *);
5439 ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5440 ph->param_length = htons(SCTP_BUF_LEN(oper));
5441 ippp = (uint32_t *) (ph + 1);
5442 *ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5444 *ippp = asoc->highest_tsn_inside_map;
5446 *ippp = new_cum_tsn;
5448 stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5449 sctp_abort_an_association(stcb->sctp_ep, stcb,
5450 SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5453 SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5455 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5456 asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5457 asoc->highest_tsn_inside_map = new_cum_tsn;
5459 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5460 asoc->highest_tsn_inside_nr_map = new_cum_tsn;
5462 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5463 sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5466 SCTP_TCB_LOCK_ASSERT(stcb);
5467 for (i = 0; i <= gap; i++) {
5468 if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
5469 !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
5470 SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
5471 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
5472 asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5477 /*************************************************************/
5478 /* 2. Clear up re-assembly queue */
5479 /*************************************************************/
5481 * First service it if pd-api is up, just in case we can progress it
5484 if (asoc->fragmented_delivery_inprogress) {
5485 sctp_service_reassembly(stcb, asoc);
5487 /* For each one on here see if we need to toss it */
5489 * For now large messages held on the reasmqueue that are complete
5490 * will be tossed too. We could in theory do more work to spin
5491 * through and stop after dumping one msg aka seeing the start of a
5492 * new msg at the head, and call the delivery function... to see if
5493 * it can be delivered... But for now we just dump everything on the
5496 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5497 if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5498 /* It needs to be tossed */
5499 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5500 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
5501 asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
5502 asoc->str_of_pdapi = chk->rec.data.stream_number;
5503 asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
5504 asoc->fragment_flags = chk->rec.data.rcv_flags;
5506 asoc->size_on_reasm_queue -= chk->send_size;
5507 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5509 /* Clear up any stream problem */
5510 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
5511 SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5513 * We must dump forward this streams
5514 * sequence number if the chunk is not
5515 * unordered that is being skipped. There is
5516 * a chance that if the peer does not
5517 * include the last fragment in its FWD-TSN
5518 * we WILL have a problem here since you
5519 * would have a partial chunk in queue that
5520 * may not be deliverable. Also if a Partial
5521 * delivery API as started the user may get
5522 * a partial chunk. The next read returning
5523 * a new chunk... really ugly but I see no
5524 * way around it! Maybe a notify??
5526 asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5529 sctp_m_freem(chk->data);
5532 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5535 * Ok we have gone beyond the end of the fwd-tsn's
5541 /*******************************************************/
5542 /* 3. Update the PR-stream re-ordering queues and fix */
5543 /* delivery issues as needed. */
5544 /*******************************************************/
5545 fwd_sz -= sizeof(*fwd);
5548 unsigned int num_str;
5549 struct sctp_strseq *stseq, strseqbuf;
5551 offset += sizeof(*fwd);
5553 SCTP_INP_READ_LOCK(stcb->sctp_ep);
5554 num_str = fwd_sz / sizeof(struct sctp_strseq);
5555 for (i = 0; i < num_str; i++) {
5558 stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5559 sizeof(struct sctp_strseq),
5560 (uint8_t *) & strseqbuf);
5561 offset += sizeof(struct sctp_strseq);
5562 if (stseq == NULL) {
5566 st = ntohs(stseq->stream);
5568 st = ntohs(stseq->sequence);
5569 stseq->sequence = st;
5574 * Ok we now look for the stream/seq on the read
5575 * queue where its not all delivered. If we find it
5576 * we transmute the read entry into a PDI_ABORTED.
5578 if (stseq->stream >= asoc->streamincnt) {
5579 /* screwed up streams, stop! */
5582 if ((asoc->str_of_pdapi == stseq->stream) &&
5583 (asoc->ssn_of_pdapi == stseq->sequence)) {
5585 * If this is the one we were partially
5586 * delivering now then we no longer are.
5587 * Note this will change with the reassembly
5590 asoc->fragmented_delivery_inprogress = 0;
5592 sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
5593 TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
5594 if ((ctl->sinfo_stream == stseq->stream) &&
5595 (ctl->sinfo_ssn == stseq->sequence)) {
5596 str_seq = (stseq->stream << 16) | stseq->sequence;
5598 ctl->pdapi_aborted = 1;
5599 sv = stcb->asoc.control_pdapi;
5600 stcb->asoc.control_pdapi = ctl;
5601 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5603 SCTP_PARTIAL_DELIVERY_ABORTED,
5605 SCTP_SO_NOT_LOCKED);
5606 stcb->asoc.control_pdapi = sv;
5608 } else if ((ctl->sinfo_stream == stseq->stream) &&
5609 SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
5610 /* We are past our victim SSN */
5614 strm = &asoc->strmin[stseq->stream];
5615 if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5616 /* Update the sequence number */
5617 strm->last_sequence_delivered = stseq->sequence;
5619 /* now kick the stream the new way */
5620 /* sa_ignore NO_NULL_CHK */
5621 sctp_kick_prsctp_reorder_queue(stcb, strm);
5623 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5626 * Now slide thing forward.
5628 sctp_slide_mapping_arrays(stcb);
5630 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5631 /* now lets kick out and check for more fragmented delivery */
5632 /* sa_ignore NO_NULL_CHK */
5633 sctp_deliver_reasm_check(stcb, &stcb->asoc);