1f8829a4aSRandall Stewart /*- 2b1006367SRandall Stewart * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5f8829a4aSRandall Stewart * 6f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without 7f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met: 8f8829a4aSRandall Stewart * 9f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice, 10f8829a4aSRandall Stewart * this list of conditions and the following disclaimer. 11f8829a4aSRandall Stewart * 12f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright 13f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in 14f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution. 15f8829a4aSRandall Stewart * 16f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its 17f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived 18f8829a4aSRandall Stewart * from this software without specific prior written permission. 19f8829a4aSRandall Stewart * 20f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE. 31f8829a4aSRandall Stewart */ 32f8829a4aSRandall Stewart 33f8829a4aSRandall Stewart #include <sys/cdefs.h> 34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$"); 35f8829a4aSRandall Stewart 36f8829a4aSRandall Stewart #include <netinet/sctp_os.h> 37f8829a4aSRandall Stewart #include <netinet/sctp_var.h> 3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h> 39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h> 40f8829a4aSRandall Stewart #include <netinet/sctp_header.h> 41f8829a4aSRandall Stewart #include <netinet/sctputil.h> 42f8829a4aSRandall Stewart #include <netinet/sctp_output.h> 43f8829a4aSRandall Stewart #include <netinet/sctp_input.h> 44f8829a4aSRandall Stewart #include <netinet/sctp_indata.h> 45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h> 46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h> 47f8829a4aSRandall Stewart 48d50c1d79SRandall Stewart 49f8829a4aSRandall Stewart /* 50f8829a4aSRandall Stewart * NOTES: On the outbound side of things I need to check the sack timer to 51f8829a4aSRandall Stewart * see if I should generate a sack into the chunk queue (if I have data to 52f8829a4aSRandall Stewart * send that is and will be sending it .. for bundling. 53f8829a4aSRandall Stewart * 54f8829a4aSRandall Stewart * The callback in sctp_usrreq.c will get called when the socket is read from. 55f8829a4aSRandall Stewart * This will cause sctp_service_queues() to get called on the top entry in 56f8829a4aSRandall Stewart * the list. 57f8829a4aSRandall Stewart */ 58f8829a4aSRandall Stewart 5972fb6fdbSRandall Stewart void 60f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc) 61f8829a4aSRandall Stewart { 62b3f1ea41SRandall Stewart asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc); 63f8829a4aSRandall Stewart } 64f8829a4aSRandall Stewart 65f8829a4aSRandall Stewart /* Calculate what the rwnd would be */ 6672fb6fdbSRandall Stewart uint32_t 67f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc) 68f8829a4aSRandall Stewart { 69b3f1ea41SRandall Stewart uint32_t calc = 0; 70f8829a4aSRandall Stewart 71f8829a4aSRandall Stewart /* 72f8829a4aSRandall Stewart * This is really set wrong with respect to a 1-2-m socket. Since 734e88d37aSMichael Tuexen * the sb_cc is the count that everyone as put up. When we re-write 74f8829a4aSRandall Stewart * sctp_soreceive then we will fix this so that ONLY this 75f8829a4aSRandall Stewart * associations data is taken into account. 76f8829a4aSRandall Stewart */ 77f8829a4aSRandall Stewart if (stcb->sctp_socket == NULL) 78f8829a4aSRandall Stewart return (calc); 79f8829a4aSRandall Stewart 804e88d37aSMichael Tuexen if (stcb->asoc.sb_cc == 0 && 81f8829a4aSRandall Stewart asoc->size_on_reasm_queue == 0 && 82f8829a4aSRandall Stewart asoc->size_on_all_streams == 0) { 83f8829a4aSRandall Stewart /* Full rwnd granted */ 84b3f1ea41SRandall Stewart calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND); 85f8829a4aSRandall Stewart return (calc); 86f8829a4aSRandall Stewart } 87f8829a4aSRandall Stewart /* get actual space */ 88f8829a4aSRandall Stewart calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv); 89f8829a4aSRandall Stewart 90f8829a4aSRandall Stewart /* 91f8829a4aSRandall Stewart * take out what has NOT been put on socket queue and we yet hold 92f8829a4aSRandall Stewart * for putting up. 93f8829a4aSRandall Stewart */ 9444fbe462SRandall Stewart calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue + 9544fbe462SRandall Stewart asoc->cnt_on_reasm_queue * MSIZE)); 9644fbe462SRandall Stewart calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams + 9744fbe462SRandall Stewart asoc->cnt_on_all_streams * MSIZE)); 98f8829a4aSRandall Stewart 99f8829a4aSRandall Stewart if (calc == 0) { 100f8829a4aSRandall Stewart /* out of space */ 101f8829a4aSRandall Stewart return (calc); 102f8829a4aSRandall Stewart } 103f8829a4aSRandall Stewart /* what is the overhead of all these rwnd's */ 1042afb3e84SRandall Stewart calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len); 105b3f1ea41SRandall Stewart /* 106b3f1ea41SRandall Stewart * If the window gets too small due to ctrl-stuff, reduce it to 1, 107b3f1ea41SRandall Stewart * even it is 0. SWS engaged 108f8829a4aSRandall Stewart */ 109b3f1ea41SRandall Stewart if (calc < stcb->asoc.my_rwnd_control_len) { 110b3f1ea41SRandall Stewart calc = 1; 1112afb3e84SRandall Stewart } 112b3f1ea41SRandall Stewart return (calc); 113f8829a4aSRandall Stewart } 114f8829a4aSRandall Stewart 115f8829a4aSRandall Stewart 116f8829a4aSRandall Stewart 117f8829a4aSRandall Stewart /* 118f8829a4aSRandall Stewart * Build out our readq entry based on the incoming packet. 119f8829a4aSRandall Stewart */ 120f8829a4aSRandall Stewart struct sctp_queued_to_read * 121f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb, 122f8829a4aSRandall Stewart struct sctp_nets *net, 123f8829a4aSRandall Stewart uint32_t tsn, uint32_t ppid, 124f8829a4aSRandall Stewart uint32_t context, uint16_t stream_no, 125f8829a4aSRandall Stewart uint16_t stream_seq, uint8_t flags, 126f8829a4aSRandall Stewart struct mbuf *dm) 127f8829a4aSRandall Stewart { 128f8829a4aSRandall Stewart struct sctp_queued_to_read *read_queue_e = NULL; 129f8829a4aSRandall Stewart 130f8829a4aSRandall Stewart sctp_alloc_a_readq(stcb, read_queue_e); 131f8829a4aSRandall Stewart if (read_queue_e == NULL) { 132f8829a4aSRandall Stewart goto failed_build; 133f8829a4aSRandall Stewart } 134f8829a4aSRandall Stewart read_queue_e->sinfo_stream = stream_no; 135f8829a4aSRandall Stewart read_queue_e->sinfo_ssn = stream_seq; 136f8829a4aSRandall Stewart read_queue_e->sinfo_flags = (flags << 8); 137f8829a4aSRandall Stewart read_queue_e->sinfo_ppid = ppid; 1387215cc1bSMichael Tuexen read_queue_e->sinfo_context = context; 139f8829a4aSRandall Stewart read_queue_e->sinfo_timetolive = 0; 140f8829a4aSRandall Stewart read_queue_e->sinfo_tsn = tsn; 141f8829a4aSRandall Stewart read_queue_e->sinfo_cumtsn = tsn; 142f8829a4aSRandall Stewart read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb); 143f8829a4aSRandall Stewart read_queue_e->whoFrom = net; 144f8829a4aSRandall Stewart read_queue_e->length = 0; 145f8829a4aSRandall Stewart atomic_add_int(&net->ref_count, 1); 146f8829a4aSRandall Stewart read_queue_e->data = dm; 147139bc87fSRandall Stewart read_queue_e->spec_flags = 0; 148f8829a4aSRandall Stewart read_queue_e->tail_mbuf = NULL; 14917205eccSRandall Stewart read_queue_e->aux_data = NULL; 150f8829a4aSRandall Stewart read_queue_e->stcb = stcb; 151f8829a4aSRandall Stewart read_queue_e->port_from = stcb->rport; 152f8829a4aSRandall Stewart read_queue_e->do_not_ref_stcb = 0; 153f8829a4aSRandall Stewart read_queue_e->end_added = 0; 1549a6142d8SRandall Stewart read_queue_e->some_taken = 0; 15503b0b021SRandall Stewart read_queue_e->pdapi_aborted = 0; 156f8829a4aSRandall Stewart failed_build: 157f8829a4aSRandall Stewart return (read_queue_e); 158f8829a4aSRandall Stewart } 159f8829a4aSRandall Stewart 160f8829a4aSRandall Stewart 161f8829a4aSRandall Stewart /* 162f8829a4aSRandall Stewart * Build out our readq entry based on the incoming packet. 163f8829a4aSRandall Stewart */ 164f8829a4aSRandall Stewart static struct sctp_queued_to_read * 165f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb, 166f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk) 167f8829a4aSRandall Stewart { 168f8829a4aSRandall Stewart struct sctp_queued_to_read *read_queue_e = NULL; 169f8829a4aSRandall Stewart 170f8829a4aSRandall Stewart sctp_alloc_a_readq(stcb, read_queue_e); 171f8829a4aSRandall Stewart if (read_queue_e == NULL) { 172f8829a4aSRandall Stewart goto failed_build; 173f8829a4aSRandall Stewart } 174f8829a4aSRandall Stewart read_queue_e->sinfo_stream = chk->rec.data.stream_number; 175f8829a4aSRandall Stewart read_queue_e->sinfo_ssn = chk->rec.data.stream_seq; 176f8829a4aSRandall Stewart read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8); 177f8829a4aSRandall Stewart read_queue_e->sinfo_ppid = chk->rec.data.payloadtype; 178f8829a4aSRandall Stewart read_queue_e->sinfo_context = stcb->asoc.context; 179f8829a4aSRandall Stewart read_queue_e->sinfo_timetolive = 0; 180f8829a4aSRandall Stewart read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq; 181f8829a4aSRandall Stewart read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq; 182f8829a4aSRandall Stewart read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb); 183f8829a4aSRandall Stewart read_queue_e->whoFrom = chk->whoTo; 18417205eccSRandall Stewart read_queue_e->aux_data = NULL; 185f8829a4aSRandall Stewart read_queue_e->length = 0; 186f8829a4aSRandall Stewart atomic_add_int(&chk->whoTo->ref_count, 1); 187f8829a4aSRandall Stewart read_queue_e->data = chk->data; 188f8829a4aSRandall Stewart read_queue_e->tail_mbuf = NULL; 189f8829a4aSRandall Stewart read_queue_e->stcb = stcb; 190f8829a4aSRandall Stewart read_queue_e->port_from = stcb->rport; 191139bc87fSRandall Stewart read_queue_e->spec_flags = 0; 192f8829a4aSRandall Stewart read_queue_e->do_not_ref_stcb = 0; 193f8829a4aSRandall Stewart read_queue_e->end_added = 0; 1949a6142d8SRandall Stewart read_queue_e->some_taken = 0; 19503b0b021SRandall Stewart read_queue_e->pdapi_aborted = 0; 196f8829a4aSRandall Stewart failed_build: 197f8829a4aSRandall Stewart return (read_queue_e); 198f8829a4aSRandall Stewart } 199f8829a4aSRandall Stewart 200f8829a4aSRandall Stewart 201f8829a4aSRandall Stewart struct mbuf * 202e2e7c62eSMichael Tuexen sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo) 203f8829a4aSRandall Stewart { 204e2e7c62eSMichael Tuexen struct sctp_extrcvinfo *seinfo; 205f8829a4aSRandall Stewart struct sctp_sndrcvinfo *outinfo; 206e2e7c62eSMichael Tuexen struct sctp_rcvinfo *rcvinfo; 207e2e7c62eSMichael Tuexen struct sctp_nxtinfo *nxtinfo; 208f8829a4aSRandall Stewart struct cmsghdr *cmh; 209f8829a4aSRandall Stewart struct mbuf *ret; 210f8829a4aSRandall Stewart int len; 211e2e7c62eSMichael Tuexen int use_extended; 212e2e7c62eSMichael Tuexen int provide_nxt; 213f8829a4aSRandall Stewart 214e2e7c62eSMichael Tuexen if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) && 215e2e7c62eSMichael Tuexen sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && 216e2e7c62eSMichael Tuexen sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) { 217e2e7c62eSMichael Tuexen /* user does not want any ancillary data */ 218f8829a4aSRandall Stewart return (NULL); 219f8829a4aSRandall Stewart } 220e2e7c62eSMichael Tuexen len = 0; 221e2e7c62eSMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) { 222e2e7c62eSMichael Tuexen len += CMSG_SPACE(sizeof(struct sctp_rcvinfo)); 223e2e7c62eSMichael Tuexen } 224e2e7c62eSMichael Tuexen seinfo = (struct sctp_extrcvinfo *)sinfo; 225e2e7c62eSMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) && 226e2e7c62eSMichael Tuexen (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) { 227e2e7c62eSMichael Tuexen provide_nxt = 1; 228e2e7c62eSMichael Tuexen len += CMSG_SPACE(sizeof(struct sctp_rcvinfo)); 229e2e7c62eSMichael Tuexen } else { 230e2e7c62eSMichael Tuexen provide_nxt = 0; 231e2e7c62eSMichael Tuexen } 232e2e7c62eSMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) { 233f8829a4aSRandall Stewart if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) { 234f8829a4aSRandall Stewart use_extended = 1; 235e2e7c62eSMichael Tuexen len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo)); 236f8829a4aSRandall Stewart } else { 237e2e7c62eSMichael Tuexen use_extended = 0; 238e2e7c62eSMichael Tuexen len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 239e2e7c62eSMichael Tuexen } 240e2e7c62eSMichael Tuexen } else { 241e2e7c62eSMichael Tuexen use_extended = 0; 242f8829a4aSRandall Stewart } 243f8829a4aSRandall Stewart 244eb1b1807SGleb Smirnoff ret = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA); 245f8829a4aSRandall Stewart if (ret == NULL) { 246f8829a4aSRandall Stewart /* No space */ 247f8829a4aSRandall Stewart return (ret); 248f8829a4aSRandall Stewart } 249e2e7c62eSMichael Tuexen SCTP_BUF_LEN(ret) = 0; 250e2e7c62eSMichael Tuexen 251f8829a4aSRandall Stewart /* We need a CMSG header followed by the struct */ 252f8829a4aSRandall Stewart cmh = mtod(ret, struct cmsghdr *); 253e432298aSXin LI /* 254e432298aSXin LI * Make sure that there is no un-initialized padding between the 255e432298aSXin LI * cmsg header and cmsg data and after the cmsg data. 256e432298aSXin LI */ 257e432298aSXin LI memset(cmh, 0, len); 258e2e7c62eSMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) { 259f8829a4aSRandall Stewart cmh->cmsg_level = IPPROTO_SCTP; 260e2e7c62eSMichael Tuexen cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo)); 261e2e7c62eSMichael Tuexen cmh->cmsg_type = SCTP_RCVINFO; 262e2e7c62eSMichael Tuexen rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh); 263e2e7c62eSMichael Tuexen rcvinfo->rcv_sid = sinfo->sinfo_stream; 264e2e7c62eSMichael Tuexen rcvinfo->rcv_ssn = sinfo->sinfo_ssn; 265e2e7c62eSMichael Tuexen rcvinfo->rcv_flags = sinfo->sinfo_flags; 266e2e7c62eSMichael Tuexen rcvinfo->rcv_ppid = sinfo->sinfo_ppid; 267e2e7c62eSMichael Tuexen rcvinfo->rcv_tsn = sinfo->sinfo_tsn; 268e2e7c62eSMichael Tuexen rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn; 269e2e7c62eSMichael Tuexen rcvinfo->rcv_context = sinfo->sinfo_context; 270e2e7c62eSMichael Tuexen rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id; 271e2e7c62eSMichael Tuexen cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo))); 272e2e7c62eSMichael Tuexen SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo)); 273f8829a4aSRandall Stewart } 274e2e7c62eSMichael Tuexen if (provide_nxt) { 275e2e7c62eSMichael Tuexen cmh->cmsg_level = IPPROTO_SCTP; 276e2e7c62eSMichael Tuexen cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo)); 277e2e7c62eSMichael Tuexen cmh->cmsg_type = SCTP_NXTINFO; 278e2e7c62eSMichael Tuexen nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh); 279e2e7c62eSMichael Tuexen nxtinfo->nxt_sid = seinfo->sreinfo_next_stream; 280e2e7c62eSMichael Tuexen nxtinfo->nxt_flags = 0; 281e2e7c62eSMichael Tuexen if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) { 282e2e7c62eSMichael Tuexen nxtinfo->nxt_flags |= SCTP_UNORDERED; 283e2e7c62eSMichael Tuexen } 284e2e7c62eSMichael Tuexen if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) { 285e2e7c62eSMichael Tuexen nxtinfo->nxt_flags |= SCTP_NOTIFICATION; 286e2e7c62eSMichael Tuexen } 287e2e7c62eSMichael Tuexen if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) { 288e2e7c62eSMichael Tuexen nxtinfo->nxt_flags |= SCTP_COMPLETE; 289e2e7c62eSMichael Tuexen } 290e2e7c62eSMichael Tuexen nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid; 291e2e7c62eSMichael Tuexen nxtinfo->nxt_length = seinfo->sreinfo_next_length; 292e2e7c62eSMichael Tuexen nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid; 293e2e7c62eSMichael Tuexen cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo))); 294e2e7c62eSMichael Tuexen SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo)); 295e2e7c62eSMichael Tuexen } 296e2e7c62eSMichael Tuexen if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) { 297e2e7c62eSMichael Tuexen cmh->cmsg_level = IPPROTO_SCTP; 298e2e7c62eSMichael Tuexen outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh); 299e2e7c62eSMichael Tuexen if (use_extended) { 300e2e7c62eSMichael Tuexen cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo)); 301e2e7c62eSMichael Tuexen cmh->cmsg_type = SCTP_EXTRCV; 302e2e7c62eSMichael Tuexen memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo)); 303e2e7c62eSMichael Tuexen SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo)); 304e2e7c62eSMichael Tuexen } else { 305e2e7c62eSMichael Tuexen cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 306e2e7c62eSMichael Tuexen cmh->cmsg_type = SCTP_SNDRCV; 307e2e7c62eSMichael Tuexen *outinfo = *sinfo; 308e2e7c62eSMichael Tuexen SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 309e2e7c62eSMichael Tuexen } 310e2e7c62eSMichael Tuexen } 311f8829a4aSRandall Stewart return (ret); 312f8829a4aSRandall Stewart } 313f8829a4aSRandall Stewart 314139bc87fSRandall Stewart 31577acdc25SRandall Stewart static void 31677acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn) 31777acdc25SRandall Stewart { 3189b2e0767SRandall Stewart uint32_t gap, i, cumackp1; 31977acdc25SRandall Stewart int fnd = 0; 32077acdc25SRandall Stewart 32177acdc25SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { 32277acdc25SRandall Stewart return; 32377acdc25SRandall Stewart } 3249b2e0767SRandall Stewart cumackp1 = asoc->cumulative_tsn + 1; 32520b07a4dSMichael Tuexen if (SCTP_TSN_GT(cumackp1, tsn)) { 3269b2e0767SRandall Stewart /* 3279b2e0767SRandall Stewart * this tsn is behind the cum ack and thus we don't need to 3289b2e0767SRandall Stewart * worry about it being moved from one to the other. 3299b2e0767SRandall Stewart */ 3309b2e0767SRandall Stewart return; 3319b2e0767SRandall Stewart } 33277acdc25SRandall Stewart SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn); 33377acdc25SRandall Stewart if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { 334cd3fd531SMichael Tuexen SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn); 33577acdc25SRandall Stewart sctp_print_mapping_array(asoc); 336b5c16493SMichael Tuexen #ifdef INVARIANTS 33777acdc25SRandall Stewart panic("Things are really messed up now!!"); 33877acdc25SRandall Stewart #endif 339b5c16493SMichael Tuexen } 34077acdc25SRandall Stewart SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap); 34177acdc25SRandall Stewart SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); 34220b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) { 34377acdc25SRandall Stewart asoc->highest_tsn_inside_nr_map = tsn; 34477acdc25SRandall Stewart } 34577acdc25SRandall Stewart if (tsn == asoc->highest_tsn_inside_map) { 34677acdc25SRandall Stewart /* We must back down to see what the new highest is */ 34720b07a4dSMichael Tuexen for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { 34877acdc25SRandall Stewart SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); 34977acdc25SRandall Stewart if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { 35077acdc25SRandall Stewart asoc->highest_tsn_inside_map = i; 35177acdc25SRandall Stewart fnd = 1; 35277acdc25SRandall Stewart break; 35377acdc25SRandall Stewart } 35477acdc25SRandall Stewart } 35577acdc25SRandall Stewart if (!fnd) { 35677acdc25SRandall Stewart asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; 35777acdc25SRandall Stewart } 35877acdc25SRandall Stewart } 35977acdc25SRandall Stewart } 36077acdc25SRandall Stewart 36117205eccSRandall Stewart 362f8829a4aSRandall Stewart /* 363f8829a4aSRandall Stewart * We are delivering currently from the reassembly queue. We must continue to 364f8829a4aSRandall Stewart * deliver until we either: 1) run out of space. 2) run out of sequential 365f8829a4aSRandall Stewart * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag. 366f8829a4aSRandall Stewart */ 367f8829a4aSRandall Stewart static void 368f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc) 369f8829a4aSRandall Stewart { 3704a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 371f8829a4aSRandall Stewart uint16_t nxt_todel; 372f8829a4aSRandall Stewart uint16_t stream_no; 373f8829a4aSRandall Stewart int end = 0; 374f8829a4aSRandall Stewart int cntDel; 3754a9ef3f8SMichael Tuexen struct sctp_queued_to_read *control, *ctl, *nctl; 376f8829a4aSRandall Stewart 377ad81507eSRandall Stewart if (stcb == NULL) 378ad81507eSRandall Stewart return; 379ad81507eSRandall Stewart 380f42a358aSRandall Stewart cntDel = stream_no = 0; 381ad81507eSRandall Stewart if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 382851b7298SRandall Stewart (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) || 383ad81507eSRandall Stewart (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) { 384851b7298SRandall Stewart /* socket above is long gone or going.. */ 385851b7298SRandall Stewart abandon: 386f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress = 0; 3874a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { 388f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 389f8829a4aSRandall Stewart asoc->size_on_reasm_queue -= chk->send_size; 390f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_reasm_queue); 391f8829a4aSRandall Stewart /* 392f8829a4aSRandall Stewart * Lose the data pointer, since its in the socket 393f8829a4aSRandall Stewart * buffer 394f8829a4aSRandall Stewart */ 395f8829a4aSRandall Stewart if (chk->data) { 396f8829a4aSRandall Stewart sctp_m_freem(chk->data); 397f8829a4aSRandall Stewart chk->data = NULL; 398f8829a4aSRandall Stewart } 399f8829a4aSRandall Stewart /* Now free the address and data */ 400689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 4013c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 402f8829a4aSRandall Stewart } 403f8829a4aSRandall Stewart return; 404f8829a4aSRandall Stewart } 405f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 4064a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { 407f8829a4aSRandall Stewart if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) { 408f8829a4aSRandall Stewart /* Can't deliver more :< */ 409f8829a4aSRandall Stewart return; 410f8829a4aSRandall Stewart } 411f8829a4aSRandall Stewart stream_no = chk->rec.data.stream_number; 412f8829a4aSRandall Stewart nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1; 413f8829a4aSRandall Stewart if (nxt_todel != chk->rec.data.stream_seq && 414f8829a4aSRandall Stewart (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) { 415f8829a4aSRandall Stewart /* 416f8829a4aSRandall Stewart * Not the next sequence to deliver in its stream OR 417f8829a4aSRandall Stewart * unordered 418f8829a4aSRandall Stewart */ 419f8829a4aSRandall Stewart return; 420f8829a4aSRandall Stewart } 421f8829a4aSRandall Stewart if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) { 422f8829a4aSRandall Stewart 423f8829a4aSRandall Stewart control = sctp_build_readq_entry_chk(stcb, chk); 424f8829a4aSRandall Stewart if (control == NULL) { 425f8829a4aSRandall Stewart /* out of memory? */ 426f8829a4aSRandall Stewart return; 427f8829a4aSRandall Stewart } 428f8829a4aSRandall Stewart /* save it off for our future deliveries */ 429f8829a4aSRandall Stewart stcb->asoc.control_pdapi = control; 430f8829a4aSRandall Stewart if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) 431f8829a4aSRandall Stewart end = 1; 432f8829a4aSRandall Stewart else 433f8829a4aSRandall Stewart end = 0; 434b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq); 435f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, 436cfde3ff7SRandall Stewart stcb, control, &stcb->sctp_socket->so_rcv, end, 437cfde3ff7SRandall Stewart SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 438f8829a4aSRandall Stewart cntDel++; 439f8829a4aSRandall Stewart } else { 440f8829a4aSRandall Stewart if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) 441f8829a4aSRandall Stewart end = 1; 442f8829a4aSRandall Stewart else 443f8829a4aSRandall Stewart end = 0; 444b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq); 445f8829a4aSRandall Stewart if (sctp_append_to_readq(stcb->sctp_ep, stcb, 446f8829a4aSRandall Stewart stcb->asoc.control_pdapi, 447f8829a4aSRandall Stewart chk->data, end, chk->rec.data.TSN_seq, 448f8829a4aSRandall Stewart &stcb->sctp_socket->so_rcv)) { 449f8829a4aSRandall Stewart /* 450f8829a4aSRandall Stewart * something is very wrong, either 451f8829a4aSRandall Stewart * control_pdapi is NULL, or the tail_mbuf 452f8829a4aSRandall Stewart * is corrupt, or there is a EOM already on 453f8829a4aSRandall Stewart * the mbuf chain. 454f8829a4aSRandall Stewart */ 455851b7298SRandall Stewart if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { 456851b7298SRandall Stewart goto abandon; 457851b7298SRandall Stewart } else { 458df6e0cc3SRandall Stewart #ifdef INVARIANTS 459ad81507eSRandall Stewart if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) { 460f8829a4aSRandall Stewart panic("This should not happen control_pdapi NULL?"); 461f8829a4aSRandall Stewart } 462f8829a4aSRandall Stewart /* if we did not panic, it was a EOM */ 463f8829a4aSRandall Stewart panic("Bad chunking ??"); 464df6e0cc3SRandall Stewart #else 465df6e0cc3SRandall Stewart if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) { 466df6e0cc3SRandall Stewart SCTP_PRINTF("This should not happen control_pdapi NULL?\n"); 467df6e0cc3SRandall Stewart } 468df6e0cc3SRandall Stewart SCTP_PRINTF("Bad chunking ??\n"); 469df6e0cc3SRandall Stewart SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n"); 470df6e0cc3SRandall Stewart 471df6e0cc3SRandall Stewart #endif 472df6e0cc3SRandall Stewart goto abandon; 473f8829a4aSRandall Stewart } 474851b7298SRandall Stewart } 475f8829a4aSRandall Stewart cntDel++; 476f8829a4aSRandall Stewart } 477f8829a4aSRandall Stewart /* pull it we did it */ 478f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 479f8829a4aSRandall Stewart if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) { 480f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress = 0; 481f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) { 482f8829a4aSRandall Stewart asoc->strmin[stream_no].last_sequence_delivered++; 483f8829a4aSRandall Stewart } 484f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) { 485f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs); 486f8829a4aSRandall Stewart } 487f8829a4aSRandall Stewart } else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) { 488f8829a4aSRandall Stewart /* 489f8829a4aSRandall Stewart * turn the flag back on since we just delivered 490f8829a4aSRandall Stewart * yet another one. 491f8829a4aSRandall Stewart */ 492f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress = 1; 493f8829a4aSRandall Stewart } 494f8829a4aSRandall Stewart asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq; 495f8829a4aSRandall Stewart asoc->last_flags_delivered = chk->rec.data.rcv_flags; 496f8829a4aSRandall Stewart asoc->last_strm_seq_delivered = chk->rec.data.stream_seq; 497f8829a4aSRandall Stewart asoc->last_strm_no_delivered = chk->rec.data.stream_number; 498f8829a4aSRandall Stewart 499f8829a4aSRandall Stewart asoc->tsn_last_delivered = chk->rec.data.TSN_seq; 500f8829a4aSRandall Stewart asoc->size_on_reasm_queue -= chk->send_size; 501f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_reasm_queue); 502f8829a4aSRandall Stewart /* free up the chk */ 503f8829a4aSRandall Stewart chk->data = NULL; 504689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 505f8829a4aSRandall Stewart 506f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress == 0) { 507f8829a4aSRandall Stewart /* 508f8829a4aSRandall Stewart * Now lets see if we can deliver the next one on 509f8829a4aSRandall Stewart * the stream 510f8829a4aSRandall Stewart */ 511f8829a4aSRandall Stewart struct sctp_stream_in *strm; 512f8829a4aSRandall Stewart 513f8829a4aSRandall Stewart strm = &asoc->strmin[stream_no]; 514f8829a4aSRandall Stewart nxt_todel = strm->last_sequence_delivered + 1; 5154a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) { 516f8829a4aSRandall Stewart /* Deliver more if we can. */ 517f8829a4aSRandall Stewart if (nxt_todel == ctl->sinfo_ssn) { 518f8829a4aSRandall Stewart TAILQ_REMOVE(&strm->inqueue, ctl, next); 519f8829a4aSRandall Stewart asoc->size_on_all_streams -= ctl->length; 520f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 521f8829a4aSRandall Stewart strm->last_sequence_delivered++; 522b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, ctl->sinfo_tsn); 523f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 524f8829a4aSRandall Stewart ctl, 525cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, 526cfde3ff7SRandall Stewart SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 527f8829a4aSRandall Stewart } else { 528f8829a4aSRandall Stewart break; 529f8829a4aSRandall Stewart } 530f8829a4aSRandall Stewart nxt_todel = strm->last_sequence_delivered + 1; 531f8829a4aSRandall Stewart } 532139bc87fSRandall Stewart break; 533f8829a4aSRandall Stewart } 5344a9ef3f8SMichael Tuexen } 535f8829a4aSRandall Stewart } 536f8829a4aSRandall Stewart 537f8829a4aSRandall Stewart /* 538f8829a4aSRandall Stewart * Queue the chunk either right into the socket buffer if it is the next one 539f8829a4aSRandall Stewart * to go OR put it in the correct place in the delivery queue. If we do 540f8829a4aSRandall Stewart * append to the so_buf, keep doing so until we are out of order. One big 541f8829a4aSRandall Stewart * question still remains, what to do when the socket buffer is FULL?? 542f8829a4aSRandall Stewart */ 543f8829a4aSRandall Stewart static void 544f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc, 545f8829a4aSRandall Stewart struct sctp_queued_to_read *control, int *abort_flag) 546f8829a4aSRandall Stewart { 547f8829a4aSRandall Stewart /* 548f8829a4aSRandall Stewart * FIX-ME maybe? What happens when the ssn wraps? If we are getting 549f8829a4aSRandall Stewart * all the data in one stream this could happen quite rapidly. One 550f8829a4aSRandall Stewart * could use the TSN to keep track of things, but this scheme breaks 551f8829a4aSRandall Stewart * down in the other type of stream useage that could occur. Send a 552f8829a4aSRandall Stewart * single msg to stream 0, send 4Billion messages to stream 1, now 553f8829a4aSRandall Stewart * send a message to stream 0. You have a situation where the TSN 554f8829a4aSRandall Stewart * has wrapped but not in the stream. Is this worth worrying about 555f8829a4aSRandall Stewart * or should we just change our queue sort at the bottom to be by 556f8829a4aSRandall Stewart * TSN. 557f8829a4aSRandall Stewart * 558f8829a4aSRandall Stewart * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2 559f8829a4aSRandall Stewart * with TSN 1? If the peer is doing some sort of funky TSN/SSN 560f8829a4aSRandall Stewart * assignment this could happen... and I don't see how this would be 561f8829a4aSRandall Stewart * a violation. So for now I am undecided an will leave the sort by 562f8829a4aSRandall Stewart * SSN alone. Maybe a hybred approach is the answer 563f8829a4aSRandall Stewart * 564f8829a4aSRandall Stewart */ 565f8829a4aSRandall Stewart struct sctp_stream_in *strm; 566f8829a4aSRandall Stewart struct sctp_queued_to_read *at; 567f8829a4aSRandall Stewart int queue_needed; 568f8829a4aSRandall Stewart uint16_t nxt_todel; 569ff1ffd74SMichael Tuexen struct mbuf *op_err; 570ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 571f8829a4aSRandall Stewart 572f8829a4aSRandall Stewart queue_needed = 1; 573f8829a4aSRandall Stewart asoc->size_on_all_streams += control->length; 574f8829a4aSRandall Stewart sctp_ucount_incr(asoc->cnt_on_all_streams); 575f8829a4aSRandall Stewart strm = &asoc->strmin[control->sinfo_stream]; 576f8829a4aSRandall Stewart nxt_todel = strm->last_sequence_delivered + 1; 577b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 578f8829a4aSRandall Stewart sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD); 57980fefe0aSRandall Stewart } 580ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, 5816247db35SMichael Tuexen "queue to stream called for sid:%u ssn:%u tsn:%u lastdel:%u nxt:%u\n", 5826247db35SMichael Tuexen (uint32_t) control->sinfo_stream, (uint32_t) control->sinfo_ssn, 5836247db35SMichael Tuexen (uint32_t) control->sinfo_tsn, 5846247db35SMichael Tuexen (uint32_t) strm->last_sequence_delivered, (uint32_t) nxt_todel); 58520b07a4dSMichael Tuexen if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) { 586f8829a4aSRandall Stewart /* The incoming sseq is behind where we last delivered? */ 587ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n", 588ad81507eSRandall Stewart control->sinfo_ssn, strm->last_sequence_delivered); 589c40e9cf2SRandall Stewart protocol_error: 590f8829a4aSRandall Stewart /* 591f8829a4aSRandall Stewart * throw it in the stream so it gets cleaned up in 592f8829a4aSRandall Stewart * association destruction 593f8829a4aSRandall Stewart */ 594f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&strm->inqueue, control, next); 595ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 596ff1ffd74SMichael Tuexen strm->last_sequence_delivered, control->sinfo_tsn, 597ff1ffd74SMichael Tuexen control->sinfo_stream, control->sinfo_ssn); 598ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 599a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1; 600ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 601f8829a4aSRandall Stewart *abort_flag = 1; 602f8829a4aSRandall Stewart return; 603f8829a4aSRandall Stewart 604f8829a4aSRandall Stewart } 605cf9e47b2SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 606cf9e47b2SMichael Tuexen struct socket *so; 607cf9e47b2SMichael Tuexen 608cf9e47b2SMichael Tuexen so = SCTP_INP_SO(stcb->sctp_ep); 609cf9e47b2SMichael Tuexen atomic_add_int(&stcb->asoc.refcnt, 1); 610cf9e47b2SMichael Tuexen SCTP_TCB_UNLOCK(stcb); 611cf9e47b2SMichael Tuexen SCTP_SOCKET_LOCK(so, 1); 612cf9e47b2SMichael Tuexen SCTP_TCB_LOCK(stcb); 613cf9e47b2SMichael Tuexen atomic_subtract_int(&stcb->asoc.refcnt, 1); 614cf9e47b2SMichael Tuexen if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { 615cf9e47b2SMichael Tuexen SCTP_SOCKET_UNLOCK(so, 1); 616cf9e47b2SMichael Tuexen return; 617cf9e47b2SMichael Tuexen } 618cf9e47b2SMichael Tuexen #endif 619f8829a4aSRandall Stewart if (nxt_todel == control->sinfo_ssn) { 620f8829a4aSRandall Stewart /* can be delivered right away? */ 621b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 622f8829a4aSRandall Stewart sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL); 62380fefe0aSRandall Stewart } 624830d754dSRandall Stewart /* EY it wont be queued if it could be delivered directly */ 625f8829a4aSRandall Stewart queue_needed = 0; 626f8829a4aSRandall Stewart asoc->size_on_all_streams -= control->length; 627f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 628f8829a4aSRandall Stewart strm->last_sequence_delivered++; 62977acdc25SRandall Stewart 630b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, control->sinfo_tsn); 631f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 632f8829a4aSRandall Stewart control, 633cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, 634cf9e47b2SMichael Tuexen SCTP_READ_LOCK_NOT_HELD, SCTP_SO_LOCKED); 6354a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) { 636f8829a4aSRandall Stewart /* all delivered */ 637f8829a4aSRandall Stewart nxt_todel = strm->last_sequence_delivered + 1; 638f8829a4aSRandall Stewart if (nxt_todel == control->sinfo_ssn) { 639f8829a4aSRandall Stewart TAILQ_REMOVE(&strm->inqueue, control, next); 640f8829a4aSRandall Stewart asoc->size_on_all_streams -= control->length; 641f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 642f8829a4aSRandall Stewart strm->last_sequence_delivered++; 643f8829a4aSRandall Stewart /* 644f8829a4aSRandall Stewart * We ignore the return of deliver_data here 645f8829a4aSRandall Stewart * since we always can hold the chunk on the 646f8829a4aSRandall Stewart * d-queue. And we have a finite number that 647f8829a4aSRandall Stewart * can be delivered from the strq. 648f8829a4aSRandall Stewart */ 649b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 650f8829a4aSRandall Stewart sctp_log_strm_del(control, NULL, 651f8829a4aSRandall Stewart SCTP_STR_LOG_FROM_IMMED_DEL); 65280fefe0aSRandall Stewart } 653b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, control->sinfo_tsn); 654f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 655f8829a4aSRandall Stewart control, 656cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, 657cfde3ff7SRandall Stewart SCTP_READ_LOCK_NOT_HELD, 658cf9e47b2SMichael Tuexen SCTP_SO_LOCKED); 659f8829a4aSRandall Stewart continue; 660f8829a4aSRandall Stewart } 661f8829a4aSRandall Stewart break; 662f8829a4aSRandall Stewart } 663f8829a4aSRandall Stewart } 664f8829a4aSRandall Stewart if (queue_needed) { 665f8829a4aSRandall Stewart /* 666f8829a4aSRandall Stewart * Ok, we did not deliver this guy, find the correct place 667f8829a4aSRandall Stewart * to put it on the queue. 668f8829a4aSRandall Stewart */ 66920b07a4dSMichael Tuexen if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) { 670cf9e47b2SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 671cf9e47b2SMichael Tuexen SCTP_SOCKET_UNLOCK(so, 1); 672cf9e47b2SMichael Tuexen #endif 673c40e9cf2SRandall Stewart goto protocol_error; 674c40e9cf2SRandall Stewart } 675f8829a4aSRandall Stewart if (TAILQ_EMPTY(&strm->inqueue)) { 676f8829a4aSRandall Stewart /* Empty queue */ 677b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 678f8829a4aSRandall Stewart sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD); 67980fefe0aSRandall Stewart } 680f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&strm->inqueue, control, next); 681f8829a4aSRandall Stewart } else { 682f8829a4aSRandall Stewart TAILQ_FOREACH(at, &strm->inqueue, next) { 68320b07a4dSMichael Tuexen if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) { 684f8829a4aSRandall Stewart /* 685f8829a4aSRandall Stewart * one in queue is bigger than the 686f8829a4aSRandall Stewart * new one, insert before this one 687f8829a4aSRandall Stewart */ 688b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 689f8829a4aSRandall Stewart sctp_log_strm_del(control, at, 690f8829a4aSRandall Stewart SCTP_STR_LOG_FROM_INSERT_MD); 69180fefe0aSRandall Stewart } 692f8829a4aSRandall Stewart TAILQ_INSERT_BEFORE(at, control, next); 693f8829a4aSRandall Stewart break; 694f8829a4aSRandall Stewart } else if (at->sinfo_ssn == control->sinfo_ssn) { 695f8829a4aSRandall Stewart /* 696f8829a4aSRandall Stewart * Gak, He sent me a duplicate str 697f8829a4aSRandall Stewart * seq number 698f8829a4aSRandall Stewart */ 699f8829a4aSRandall Stewart /* 700f8829a4aSRandall Stewart * foo bar, I guess I will just free 701f8829a4aSRandall Stewart * this new guy, should we abort 702f8829a4aSRandall Stewart * too? FIX ME MAYBE? Or it COULD be 703f8829a4aSRandall Stewart * that the SSN's have wrapped. 704f8829a4aSRandall Stewart * Maybe I should compare to TSN 705f8829a4aSRandall Stewart * somehow... sigh for now just blow 706f8829a4aSRandall Stewart * away the chunk! 707f8829a4aSRandall Stewart */ 708f8829a4aSRandall Stewart 709f8829a4aSRandall Stewart if (control->data) 710f8829a4aSRandall Stewart sctp_m_freem(control->data); 711f8829a4aSRandall Stewart control->data = NULL; 712f8829a4aSRandall Stewart asoc->size_on_all_streams -= control->length; 713f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 71424f52bbdSMichael Tuexen if (control->whoFrom) { 715f8829a4aSRandall Stewart sctp_free_remote_addr(control->whoFrom); 7165bead436SRandall Stewart control->whoFrom = NULL; 71724f52bbdSMichael Tuexen } 718f8829a4aSRandall Stewart sctp_free_a_readq(stcb, control); 719cf9e47b2SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 720cf9e47b2SMichael Tuexen SCTP_SOCKET_UNLOCK(so, 1); 721cf9e47b2SMichael Tuexen #endif 722f8829a4aSRandall Stewart return; 723f8829a4aSRandall Stewart } else { 724f8829a4aSRandall Stewart if (TAILQ_NEXT(at, next) == NULL) { 725f8829a4aSRandall Stewart /* 726f8829a4aSRandall Stewart * We are at the end, insert 727f8829a4aSRandall Stewart * it after this one 728f8829a4aSRandall Stewart */ 729b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 730f8829a4aSRandall Stewart sctp_log_strm_del(control, at, 731f8829a4aSRandall Stewart SCTP_STR_LOG_FROM_INSERT_TL); 73280fefe0aSRandall Stewart } 733f8829a4aSRandall Stewart TAILQ_INSERT_AFTER(&strm->inqueue, 734f8829a4aSRandall Stewart at, control, next); 735f8829a4aSRandall Stewart break; 736f8829a4aSRandall Stewart } 737f8829a4aSRandall Stewart } 738f8829a4aSRandall Stewart } 739f8829a4aSRandall Stewart } 740f8829a4aSRandall Stewart } 741cf9e47b2SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 742cf9e47b2SMichael Tuexen SCTP_SOCKET_UNLOCK(so, 1); 743cf9e47b2SMichael Tuexen #endif 744f8829a4aSRandall Stewart } 745f8829a4aSRandall Stewart 746f8829a4aSRandall Stewart /* 747f8829a4aSRandall Stewart * Returns two things: You get the total size of the deliverable parts of the 748f8829a4aSRandall Stewart * first fragmented message on the reassembly queue. And you get a 1 back if 749f8829a4aSRandall Stewart * all of the message is ready or a 0 back if the message is still incomplete 750f8829a4aSRandall Stewart */ 751f8829a4aSRandall Stewart static int 752f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size) 753f8829a4aSRandall Stewart { 754f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 755f8829a4aSRandall Stewart uint32_t tsn; 756f8829a4aSRandall Stewart 757f8829a4aSRandall Stewart *t_size = 0; 758f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->reasmqueue); 759f8829a4aSRandall Stewart if (chk == NULL) { 760f8829a4aSRandall Stewart /* nothing on the queue */ 761f8829a4aSRandall Stewart return (0); 762f8829a4aSRandall Stewart } 763f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) { 764f8829a4aSRandall Stewart /* Not a first on the queue */ 765f8829a4aSRandall Stewart return (0); 766f8829a4aSRandall Stewart } 767f8829a4aSRandall Stewart tsn = chk->rec.data.TSN_seq; 7684a9ef3f8SMichael Tuexen TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) { 769f8829a4aSRandall Stewart if (tsn != chk->rec.data.TSN_seq) { 770f8829a4aSRandall Stewart return (0); 771f8829a4aSRandall Stewart } 772f8829a4aSRandall Stewart *t_size += chk->send_size; 773f8829a4aSRandall Stewart if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) { 774f8829a4aSRandall Stewart return (1); 775f8829a4aSRandall Stewart } 776f8829a4aSRandall Stewart tsn++; 777f8829a4aSRandall Stewart } 778f8829a4aSRandall Stewart return (0); 779f8829a4aSRandall Stewart } 780f8829a4aSRandall Stewart 781f8829a4aSRandall Stewart static void 782f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc) 783f8829a4aSRandall Stewart { 784f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 785f8829a4aSRandall Stewart uint16_t nxt_todel; 786810ec536SMichael Tuexen uint32_t tsize, pd_point; 787f8829a4aSRandall Stewart 788139bc87fSRandall Stewart doit_again: 789f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->reasmqueue); 790f8829a4aSRandall Stewart if (chk == NULL) { 791f8829a4aSRandall Stewart /* Huh? */ 792f8829a4aSRandall Stewart asoc->size_on_reasm_queue = 0; 793f8829a4aSRandall Stewart asoc->cnt_on_reasm_queue = 0; 794f8829a4aSRandall Stewart return; 795f8829a4aSRandall Stewart } 796f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress == 0) { 797f8829a4aSRandall Stewart nxt_todel = 798f8829a4aSRandall Stewart asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1; 799f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) && 800f8829a4aSRandall Stewart (nxt_todel == chk->rec.data.stream_seq || 801f8829a4aSRandall Stewart (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) { 802f8829a4aSRandall Stewart /* 803f8829a4aSRandall Stewart * Yep the first one is here and its ok to deliver 804f8829a4aSRandall Stewart * but should we? 805f8829a4aSRandall Stewart */ 806810ec536SMichael Tuexen if (stcb->sctp_socket) { 807d4d23375SMichael Tuexen pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT, 808810ec536SMichael Tuexen stcb->sctp_ep->partial_delivery_point); 809810ec536SMichael Tuexen } else { 810810ec536SMichael Tuexen pd_point = stcb->sctp_ep->partial_delivery_point; 811810ec536SMichael Tuexen } 812810ec536SMichael Tuexen if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) { 813f8829a4aSRandall Stewart /* 814f8829a4aSRandall Stewart * Yes, we setup to start reception, by 815f8829a4aSRandall Stewart * backing down the TSN just in case we 816f8829a4aSRandall Stewart * can't deliver. If we 817f8829a4aSRandall Stewart */ 818f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress = 1; 819f8829a4aSRandall Stewart asoc->tsn_last_delivered = 820f8829a4aSRandall Stewart chk->rec.data.TSN_seq - 1; 821f8829a4aSRandall Stewart asoc->str_of_pdapi = 822f8829a4aSRandall Stewart chk->rec.data.stream_number; 823f8829a4aSRandall Stewart asoc->ssn_of_pdapi = chk->rec.data.stream_seq; 824f8829a4aSRandall Stewart asoc->pdapi_ppid = chk->rec.data.payloadtype; 825f8829a4aSRandall Stewart asoc->fragment_flags = chk->rec.data.rcv_flags; 826f8829a4aSRandall Stewart sctp_service_reassembly(stcb, asoc); 827f8829a4aSRandall Stewart } 828f8829a4aSRandall Stewart } 829f8829a4aSRandall Stewart } else { 830139bc87fSRandall Stewart /* 831139bc87fSRandall Stewart * Service re-assembly will deliver stream data queued at 832139bc87fSRandall Stewart * the end of fragmented delivery.. but it wont know to go 833139bc87fSRandall Stewart * back and call itself again... we do that here with the 834139bc87fSRandall Stewart * got doit_again 835139bc87fSRandall Stewart */ 836f8829a4aSRandall Stewart sctp_service_reassembly(stcb, asoc); 837139bc87fSRandall Stewart if (asoc->fragmented_delivery_inprogress == 0) { 838139bc87fSRandall Stewart /* 839139bc87fSRandall Stewart * finished our Fragmented delivery, could be more 840139bc87fSRandall Stewart * waiting? 841139bc87fSRandall Stewart */ 842139bc87fSRandall Stewart goto doit_again; 843139bc87fSRandall Stewart } 844f8829a4aSRandall Stewart } 845f8829a4aSRandall Stewart } 846f8829a4aSRandall Stewart 847f8829a4aSRandall Stewart /* 848f8829a4aSRandall Stewart * Dump onto the re-assembly queue, in its proper place. After dumping on the 849f8829a4aSRandall Stewart * queue, see if anthing can be delivered. If so pull it off (or as much as 850f8829a4aSRandall Stewart * we can. If we run out of space then we must dump what we can and set the 851f8829a4aSRandall Stewart * appropriate flag to say we queued what we could. 852f8829a4aSRandall Stewart */ 853f8829a4aSRandall Stewart static void 854f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc, 855f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk, int *abort_flag) 856f8829a4aSRandall Stewart { 857ff1ffd74SMichael Tuexen struct mbuf *op_err; 858ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 85960990c0cSMichael Tuexen uint32_t cum_ackp1, prev_tsn, post_tsn; 860f8829a4aSRandall Stewart struct sctp_tmit_chunk *at, *prev, *next; 861f8829a4aSRandall Stewart 862f8829a4aSRandall Stewart prev = next = NULL; 863f8829a4aSRandall Stewart cum_ackp1 = asoc->tsn_last_delivered + 1; 864f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->reasmqueue)) { 865f8829a4aSRandall Stewart /* This is the first one on the queue */ 866f8829a4aSRandall Stewart TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next); 867f8829a4aSRandall Stewart /* 868f8829a4aSRandall Stewart * we do not check for delivery of anything when only one 869f8829a4aSRandall Stewart * fragment is here 870f8829a4aSRandall Stewart */ 871f8829a4aSRandall Stewart asoc->size_on_reasm_queue = chk->send_size; 872f8829a4aSRandall Stewart sctp_ucount_incr(asoc->cnt_on_reasm_queue); 873f8829a4aSRandall Stewart if (chk->rec.data.TSN_seq == cum_ackp1) { 874f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress == 0 && 875f8829a4aSRandall Stewart (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) != 876f8829a4aSRandall Stewart SCTP_DATA_FIRST_FRAG) { 877f8829a4aSRandall Stewart /* 878f8829a4aSRandall Stewart * An empty queue, no delivery inprogress, 879f8829a4aSRandall Stewart * we hit the next one and it does NOT have 880f8829a4aSRandall Stewart * a FIRST fragment mark. 881f8829a4aSRandall Stewart */ 882ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n"); 883ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 884ff1ffd74SMichael Tuexen "Expected B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 885ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 886ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 887ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 888ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 889a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2; 890ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 891f8829a4aSRandall Stewart *abort_flag = 1; 892f8829a4aSRandall Stewart } else if (asoc->fragmented_delivery_inprogress && 893f8829a4aSRandall Stewart (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) { 894f8829a4aSRandall Stewart /* 895f8829a4aSRandall Stewart * We are doing a partial delivery and the 896f8829a4aSRandall Stewart * NEXT chunk MUST be either the LAST or 897f8829a4aSRandall Stewart * MIDDLE fragment NOT a FIRST 898f8829a4aSRandall Stewart */ 899ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n"); 900ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 901ff1ffd74SMichael Tuexen "Didn't expect B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 902ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 903ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 904ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 905ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 906a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3; 907ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 908f8829a4aSRandall Stewart *abort_flag = 1; 909f8829a4aSRandall Stewart } else if (asoc->fragmented_delivery_inprogress) { 910f8829a4aSRandall Stewart /* 911f8829a4aSRandall Stewart * Here we are ok with a MIDDLE or LAST 912f8829a4aSRandall Stewart * piece 913f8829a4aSRandall Stewart */ 914f8829a4aSRandall Stewart if (chk->rec.data.stream_number != 915f8829a4aSRandall Stewart asoc->str_of_pdapi) { 916f8829a4aSRandall Stewart /* Got to be the right STR No */ 917ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n", 918f8829a4aSRandall Stewart chk->rec.data.stream_number, 919f8829a4aSRandall Stewart asoc->str_of_pdapi); 920ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 921ff1ffd74SMichael Tuexen "Expected SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 922ff1ffd74SMichael Tuexen asoc->str_of_pdapi, 923ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 924ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 925ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 926ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 927a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4; 928ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 929f8829a4aSRandall Stewart *abort_flag = 1; 930f8829a4aSRandall Stewart } else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) != 931f8829a4aSRandall Stewart SCTP_DATA_UNORDERED && 932b5c16493SMichael Tuexen chk->rec.data.stream_seq != asoc->ssn_of_pdapi) { 933f8829a4aSRandall Stewart /* Got to be the right STR Seq */ 934ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n", 935f8829a4aSRandall Stewart chk->rec.data.stream_seq, 936f8829a4aSRandall Stewart asoc->ssn_of_pdapi); 937ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 938ff1ffd74SMichael Tuexen "Expected SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 939ff1ffd74SMichael Tuexen asoc->ssn_of_pdapi, 940ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 941ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 942ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 943ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 944a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5; 945ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 946f8829a4aSRandall Stewart *abort_flag = 1; 947f8829a4aSRandall Stewart } 948f8829a4aSRandall Stewart } 949f8829a4aSRandall Stewart } 950f8829a4aSRandall Stewart return; 951f8829a4aSRandall Stewart } 952f8829a4aSRandall Stewart /* Find its place */ 953f8829a4aSRandall Stewart TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) { 95420b07a4dSMichael Tuexen if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) { 955f8829a4aSRandall Stewart /* 956f8829a4aSRandall Stewart * one in queue is bigger than the new one, insert 957f8829a4aSRandall Stewart * before this one 958f8829a4aSRandall Stewart */ 959f8829a4aSRandall Stewart /* A check */ 960f8829a4aSRandall Stewart asoc->size_on_reasm_queue += chk->send_size; 961f8829a4aSRandall Stewart sctp_ucount_incr(asoc->cnt_on_reasm_queue); 962f8829a4aSRandall Stewart next = at; 963f8829a4aSRandall Stewart TAILQ_INSERT_BEFORE(at, chk, sctp_next); 964f8829a4aSRandall Stewart break; 965f8829a4aSRandall Stewart } else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) { 966f8829a4aSRandall Stewart /* Gak, He sent me a duplicate str seq number */ 967f8829a4aSRandall Stewart /* 968f8829a4aSRandall Stewart * foo bar, I guess I will just free this new guy, 969f8829a4aSRandall Stewart * should we abort too? FIX ME MAYBE? Or it COULD be 970f8829a4aSRandall Stewart * that the SSN's have wrapped. Maybe I should 971f8829a4aSRandall Stewart * compare to TSN somehow... sigh for now just blow 972f8829a4aSRandall Stewart * away the chunk! 973f8829a4aSRandall Stewart */ 974f8829a4aSRandall Stewart if (chk->data) { 975f8829a4aSRandall Stewart sctp_m_freem(chk->data); 976f8829a4aSRandall Stewart chk->data = NULL; 977f8829a4aSRandall Stewart } 978689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 979f8829a4aSRandall Stewart return; 980f8829a4aSRandall Stewart } else { 981f8829a4aSRandall Stewart prev = at; 982f8829a4aSRandall Stewart if (TAILQ_NEXT(at, sctp_next) == NULL) { 983f8829a4aSRandall Stewart /* 984f8829a4aSRandall Stewart * We are at the end, insert it after this 985f8829a4aSRandall Stewart * one 986f8829a4aSRandall Stewart */ 987f8829a4aSRandall Stewart /* check it first */ 988f8829a4aSRandall Stewart asoc->size_on_reasm_queue += chk->send_size; 989f8829a4aSRandall Stewart sctp_ucount_incr(asoc->cnt_on_reasm_queue); 990f8829a4aSRandall Stewart TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next); 991f8829a4aSRandall Stewart break; 992f8829a4aSRandall Stewart } 993f8829a4aSRandall Stewart } 994f8829a4aSRandall Stewart } 995f8829a4aSRandall Stewart /* Now the audits */ 996f8829a4aSRandall Stewart if (prev) { 997f8829a4aSRandall Stewart prev_tsn = chk->rec.data.TSN_seq - 1; 998f8829a4aSRandall Stewart if (prev_tsn == prev->rec.data.TSN_seq) { 999f8829a4aSRandall Stewart /* 1000f8829a4aSRandall Stewart * Ok the one I am dropping onto the end is the 1001f8829a4aSRandall Stewart * NEXT. A bit of valdiation here. 1002f8829a4aSRandall Stewart */ 1003f8829a4aSRandall Stewart if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1004f8829a4aSRandall Stewart SCTP_DATA_FIRST_FRAG || 1005f8829a4aSRandall Stewart (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1006f8829a4aSRandall Stewart SCTP_DATA_MIDDLE_FRAG) { 1007f8829a4aSRandall Stewart /* 1008f8829a4aSRandall Stewart * Insert chk MUST be a MIDDLE or LAST 1009f8829a4aSRandall Stewart * fragment 1010f8829a4aSRandall Stewart */ 1011f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1012f8829a4aSRandall Stewart SCTP_DATA_FIRST_FRAG) { 1013ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n"); 1014ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n"); 1015ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1016ff1ffd74SMichael Tuexen "Can't handle B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1017ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1018ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1019ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1020ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1021a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6; 1022ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1023f8829a4aSRandall Stewart *abort_flag = 1; 1024f8829a4aSRandall Stewart return; 1025f8829a4aSRandall Stewart } 1026f8829a4aSRandall Stewart if (chk->rec.data.stream_number != 1027f8829a4aSRandall Stewart prev->rec.data.stream_number) { 1028f8829a4aSRandall Stewart /* 1029f8829a4aSRandall Stewart * Huh, need the correct STR here, 1030f8829a4aSRandall Stewart * they must be the same. 1031f8829a4aSRandall Stewart */ 10322dec1efcSMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sid:%d not the same as at:%d\n", 1033f8829a4aSRandall Stewart chk->rec.data.stream_number, 1034f8829a4aSRandall Stewart prev->rec.data.stream_number); 1035ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1036ff1ffd74SMichael Tuexen "Expect SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1037ff1ffd74SMichael Tuexen prev->rec.data.stream_number, 1038ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1039ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1040ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1041ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1042a5d547adSRandall Stewart stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7; 1043ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1044f8829a4aSRandall Stewart *abort_flag = 1; 1045f8829a4aSRandall Stewart return; 1046f8829a4aSRandall Stewart } 1047eb67ee5fSMichael Tuexen if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != 1048eb67ee5fSMichael Tuexen (prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) { 1049eb67ee5fSMichael Tuexen /* 1050eb67ee5fSMichael Tuexen * Huh, need the same ordering here, 1051eb67ee5fSMichael Tuexen * they must be the same. 1052eb67ee5fSMichael Tuexen */ 1053eb67ee5fSMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, U-bit not constant\n"); 1054eb67ee5fSMichael Tuexen snprintf(msg, sizeof(msg), 1055eb67ee5fSMichael Tuexen "Expect U-bit=%d for TSN=%8.8x, got U-bit=%d", 1056eb67ee5fSMichael Tuexen (prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0, 1057eb67ee5fSMichael Tuexen chk->rec.data.TSN_seq, 1058eb67ee5fSMichael Tuexen (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0); 1059eb67ee5fSMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1060b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8; 1061eb67ee5fSMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1062eb67ee5fSMichael Tuexen *abort_flag = 1; 1063eb67ee5fSMichael Tuexen return; 1064eb67ee5fSMichael Tuexen } 1065f8829a4aSRandall Stewart if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 && 1066f8829a4aSRandall Stewart chk->rec.data.stream_seq != 1067f8829a4aSRandall Stewart prev->rec.data.stream_seq) { 1068f8829a4aSRandall Stewart /* 1069f8829a4aSRandall Stewart * Huh, need the correct STR here, 1070f8829a4aSRandall Stewart * they must be the same. 1071f8829a4aSRandall Stewart */ 1072ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n", 1073f8829a4aSRandall Stewart chk->rec.data.stream_seq, 1074f8829a4aSRandall Stewart prev->rec.data.stream_seq); 1075ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1076ff1ffd74SMichael Tuexen "Expect SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1077ff1ffd74SMichael Tuexen prev->rec.data.stream_seq, 1078ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1079ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1080ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1081ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1082b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9; 1083ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1084f8829a4aSRandall Stewart *abort_flag = 1; 1085f8829a4aSRandall Stewart return; 1086f8829a4aSRandall Stewart } 1087f8829a4aSRandall Stewart } else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1088f8829a4aSRandall Stewart SCTP_DATA_LAST_FRAG) { 1089f8829a4aSRandall Stewart /* Insert chk MUST be a FIRST */ 1090f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) != 1091f8829a4aSRandall Stewart SCTP_DATA_FIRST_FRAG) { 1092ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n"); 1093ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1094ff1ffd74SMichael Tuexen "Expect B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1095ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1096ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1097ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1098ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1099b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10; 1100ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1101f8829a4aSRandall Stewart *abort_flag = 1; 1102f8829a4aSRandall Stewart return; 1103f8829a4aSRandall Stewart } 1104f8829a4aSRandall Stewart } 1105f8829a4aSRandall Stewart } 1106f8829a4aSRandall Stewart } 1107f8829a4aSRandall Stewart if (next) { 1108f8829a4aSRandall Stewart post_tsn = chk->rec.data.TSN_seq + 1; 1109f8829a4aSRandall Stewart if (post_tsn == next->rec.data.TSN_seq) { 1110f8829a4aSRandall Stewart /* 1111f8829a4aSRandall Stewart * Ok the one I am inserting ahead of is my NEXT 1112f8829a4aSRandall Stewart * one. A bit of valdiation here. 1113f8829a4aSRandall Stewart */ 1114f8829a4aSRandall Stewart if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) { 1115f8829a4aSRandall Stewart /* Insert chk MUST be a last fragment */ 1116f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) 1117f8829a4aSRandall Stewart != SCTP_DATA_LAST_FRAG) { 1118ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n"); 1119ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n"); 1120ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1121ff1ffd74SMichael Tuexen "Expect only E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1122ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1123ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1124ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1125ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1126b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11; 1127ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1128f8829a4aSRandall Stewart *abort_flag = 1; 1129f8829a4aSRandall Stewart return; 1130f8829a4aSRandall Stewart } 1131f8829a4aSRandall Stewart } else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1132f8829a4aSRandall Stewart SCTP_DATA_MIDDLE_FRAG || 1133f8829a4aSRandall Stewart (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1134f8829a4aSRandall Stewart SCTP_DATA_LAST_FRAG) { 1135f8829a4aSRandall Stewart /* 1136f8829a4aSRandall Stewart * Insert chk CAN be MIDDLE or FIRST NOT 1137f8829a4aSRandall Stewart * LAST 1138f8829a4aSRandall Stewart */ 1139f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) == 1140f8829a4aSRandall Stewart SCTP_DATA_LAST_FRAG) { 1141ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n"); 1142ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n"); 1143ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1144ff1ffd74SMichael Tuexen "Didn't expect E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1145ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1146ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1147ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1148ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1149b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12; 1150ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1151f8829a4aSRandall Stewart *abort_flag = 1; 1152f8829a4aSRandall Stewart return; 1153f8829a4aSRandall Stewart } 1154f8829a4aSRandall Stewart if (chk->rec.data.stream_number != 1155f8829a4aSRandall Stewart next->rec.data.stream_number) { 1156f8829a4aSRandall Stewart /* 1157f8829a4aSRandall Stewart * Huh, need the correct STR here, 1158f8829a4aSRandall Stewart * they must be the same. 1159f8829a4aSRandall Stewart */ 1160ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n", 1161f8829a4aSRandall Stewart chk->rec.data.stream_number, 1162f8829a4aSRandall Stewart next->rec.data.stream_number); 1163ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1164ff1ffd74SMichael Tuexen "Required SID %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1165ff1ffd74SMichael Tuexen next->rec.data.stream_number, 1166ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1167ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1168ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1169ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1170b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13; 1171ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1172f8829a4aSRandall Stewart *abort_flag = 1; 1173f8829a4aSRandall Stewart return; 1174f8829a4aSRandall Stewart } 1175eb67ee5fSMichael Tuexen if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != 1176eb67ee5fSMichael Tuexen (next->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) { 1177eb67ee5fSMichael Tuexen /* 1178eb67ee5fSMichael Tuexen * Huh, need the same ordering here, 1179eb67ee5fSMichael Tuexen * they must be the same. 1180eb67ee5fSMichael Tuexen */ 1181eb67ee5fSMichael Tuexen SCTPDBG(SCTP_DEBUG_INDATA1, "Next check - Gak, Evil plot, U-bit not constant\n"); 1182eb67ee5fSMichael Tuexen snprintf(msg, sizeof(msg), 1183eb67ee5fSMichael Tuexen "Expect U-bit=%d for TSN=%8.8x, got U-bit=%d", 1184eb67ee5fSMichael Tuexen (next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0, 1185eb67ee5fSMichael Tuexen chk->rec.data.TSN_seq, 1186eb67ee5fSMichael Tuexen (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0); 1187eb67ee5fSMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1188b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14; 1189eb67ee5fSMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1190eb67ee5fSMichael Tuexen *abort_flag = 1; 1191eb67ee5fSMichael Tuexen return; 1192eb67ee5fSMichael Tuexen } 1193f8829a4aSRandall Stewart if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 && 1194f8829a4aSRandall Stewart chk->rec.data.stream_seq != 1195f8829a4aSRandall Stewart next->rec.data.stream_seq) { 1196f8829a4aSRandall Stewart /* 1197f8829a4aSRandall Stewart * Huh, need the correct STR here, 1198f8829a4aSRandall Stewart * they must be the same. 1199f8829a4aSRandall Stewart */ 1200ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n", 1201f8829a4aSRandall Stewart chk->rec.data.stream_seq, 1202f8829a4aSRandall Stewart next->rec.data.stream_seq); 1203ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 1204ff1ffd74SMichael Tuexen "Required SSN %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1205ff1ffd74SMichael Tuexen next->rec.data.stream_seq, 1206ff1ffd74SMichael Tuexen chk->rec.data.TSN_seq, 1207ff1ffd74SMichael Tuexen chk->rec.data.stream_number, 1208ff1ffd74SMichael Tuexen chk->rec.data.stream_seq); 1209ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1210b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15; 1211ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1212f8829a4aSRandall Stewart *abort_flag = 1; 1213f8829a4aSRandall Stewart return; 1214f8829a4aSRandall Stewart } 1215f8829a4aSRandall Stewart } 1216f8829a4aSRandall Stewart } 1217f8829a4aSRandall Stewart } 1218f8829a4aSRandall Stewart /* Do we need to do some delivery? check */ 1219f8829a4aSRandall Stewart sctp_deliver_reasm_check(stcb, asoc); 1220f8829a4aSRandall Stewart } 1221f8829a4aSRandall Stewart 1222f8829a4aSRandall Stewart /* 1223f8829a4aSRandall Stewart * This is an unfortunate routine. It checks to make sure a evil guy is not 1224f8829a4aSRandall Stewart * stuffing us full of bad packet fragments. A broken peer could also do this 1225f8829a4aSRandall Stewart * but this is doubtful. It is to bad I must worry about evil crackers sigh 1226f8829a4aSRandall Stewart * :< more cycles. 1227f8829a4aSRandall Stewart */ 1228f8829a4aSRandall Stewart static int 1229f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc, 1230f8829a4aSRandall Stewart uint32_t TSN_seq) 1231f8829a4aSRandall Stewart { 1232f8829a4aSRandall Stewart struct sctp_tmit_chunk *at; 1233f8829a4aSRandall Stewart uint32_t tsn_est; 1234f8829a4aSRandall Stewart 1235f8829a4aSRandall Stewart TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) { 123620b07a4dSMichael Tuexen if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) { 1237f8829a4aSRandall Stewart /* is it one bigger? */ 1238f8829a4aSRandall Stewart tsn_est = at->rec.data.TSN_seq + 1; 1239f8829a4aSRandall Stewart if (tsn_est == TSN_seq) { 1240f8829a4aSRandall Stewart /* yep. It better be a last then */ 1241f8829a4aSRandall Stewart if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) != 1242f8829a4aSRandall Stewart SCTP_DATA_LAST_FRAG) { 1243f8829a4aSRandall Stewart /* 1244f8829a4aSRandall Stewart * Ok this guy belongs next to a guy 1245f8829a4aSRandall Stewart * that is NOT last, it should be a 1246f8829a4aSRandall Stewart * middle/last, not a complete 1247f8829a4aSRandall Stewart * chunk. 1248f8829a4aSRandall Stewart */ 1249f8829a4aSRandall Stewart return (1); 1250f8829a4aSRandall Stewart } else { 1251f8829a4aSRandall Stewart /* 1252f8829a4aSRandall Stewart * This guy is ok since its a LAST 1253f8829a4aSRandall Stewart * and the new chunk is a fully 1254f8829a4aSRandall Stewart * self- contained one. 1255f8829a4aSRandall Stewart */ 1256f8829a4aSRandall Stewart return (0); 1257f8829a4aSRandall Stewart } 1258f8829a4aSRandall Stewart } 1259f8829a4aSRandall Stewart } else if (TSN_seq == at->rec.data.TSN_seq) { 1260f8829a4aSRandall Stewart /* Software error since I have a dup? */ 1261f8829a4aSRandall Stewart return (1); 1262f8829a4aSRandall Stewart } else { 1263f8829a4aSRandall Stewart /* 1264f8829a4aSRandall Stewart * Ok, 'at' is larger than new chunk but does it 1265f8829a4aSRandall Stewart * need to be right before it. 1266f8829a4aSRandall Stewart */ 1267f8829a4aSRandall Stewart tsn_est = TSN_seq + 1; 1268f8829a4aSRandall Stewart if (tsn_est == at->rec.data.TSN_seq) { 1269f8829a4aSRandall Stewart /* Yep, It better be a first */ 1270f8829a4aSRandall Stewart if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) != 1271f8829a4aSRandall Stewart SCTP_DATA_FIRST_FRAG) { 1272f8829a4aSRandall Stewart return (1); 1273f8829a4aSRandall Stewart } else { 1274f8829a4aSRandall Stewart return (0); 1275f8829a4aSRandall Stewart } 1276f8829a4aSRandall Stewart } 1277f8829a4aSRandall Stewart } 1278f8829a4aSRandall Stewart } 1279f8829a4aSRandall Stewart return (0); 1280f8829a4aSRandall Stewart } 1281f8829a4aSRandall Stewart 1282f8829a4aSRandall Stewart static int 1283f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc, 1284f8829a4aSRandall Stewart struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length, 1285f8829a4aSRandall Stewart struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag, 1286f8829a4aSRandall Stewart int *break_flag, int last_chunk) 1287f8829a4aSRandall Stewart { 1288f8829a4aSRandall Stewart /* Process a data chunk */ 1289f8829a4aSRandall Stewart /* struct sctp_tmit_chunk *chk; */ 1290f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 1291f8829a4aSRandall Stewart uint32_t tsn, gap; 1292f8829a4aSRandall Stewart struct mbuf *dmbuf; 12937215cc1bSMichael Tuexen int the_len; 1294139bc87fSRandall Stewart int need_reasm_check = 0; 1295f8829a4aSRandall Stewart uint16_t strmno, strmseq; 1296ff1ffd74SMichael Tuexen struct mbuf *op_err; 1297ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 1298f8829a4aSRandall Stewart struct sctp_queued_to_read *control; 1299f42a358aSRandall Stewart int ordered; 1300f42a358aSRandall Stewart uint32_t protocol_id; 1301f42a358aSRandall Stewart uint8_t chunk_flags; 130217205eccSRandall Stewart struct sctp_stream_reset_list *liste; 1303f8829a4aSRandall Stewart 1304f8829a4aSRandall Stewart chk = NULL; 1305f8829a4aSRandall Stewart tsn = ntohl(ch->dp.tsn); 1306f42a358aSRandall Stewart chunk_flags = ch->ch.chunk_flags; 1307b3f1ea41SRandall Stewart if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) { 1308b3f1ea41SRandall Stewart asoc->send_sack = 1; 1309b3f1ea41SRandall Stewart } 1310f42a358aSRandall Stewart protocol_id = ch->dp.protocol_id; 131137f144ebSMichael Tuexen ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0); 1312b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 1313c4739e2fSRandall Stewart sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS); 131480fefe0aSRandall Stewart } 1315ad81507eSRandall Stewart if (stcb == NULL) { 1316ad81507eSRandall Stewart return (0); 1317ad81507eSRandall Stewart } 131880fefe0aSRandall Stewart SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn); 131920b07a4dSMichael Tuexen if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) { 1320f8829a4aSRandall Stewart /* It is a duplicate */ 1321f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvdupdata); 1322f8829a4aSRandall Stewart if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) { 1323f8829a4aSRandall Stewart /* Record a dup for the next outbound sack */ 1324f8829a4aSRandall Stewart asoc->dup_tsns[asoc->numduptsns] = tsn; 1325f8829a4aSRandall Stewart asoc->numduptsns++; 1326f8829a4aSRandall Stewart } 1327b201f536SRandall Stewart asoc->send_sack = 1; 1328f8829a4aSRandall Stewart return (0); 1329f8829a4aSRandall Stewart } 1330f8829a4aSRandall Stewart /* Calculate the number of TSN's between the base and this TSN */ 1331d50c1d79SRandall Stewart SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn); 1332f8829a4aSRandall Stewart if (gap >= (SCTP_MAPPING_ARRAY << 3)) { 1333f8829a4aSRandall Stewart /* Can't hold the bit in the mapping at max array, toss it */ 1334f8829a4aSRandall Stewart return (0); 1335f8829a4aSRandall Stewart } 1336f8829a4aSRandall Stewart if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) { 1337207304d4SRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 13380696e120SRandall Stewart if (sctp_expand_mapping_array(asoc, gap)) { 1339f8829a4aSRandall Stewart /* Can't expand, drop it */ 1340f8829a4aSRandall Stewart return (0); 1341f8829a4aSRandall Stewart } 1342f8829a4aSRandall Stewart } 134320b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, *high_tsn)) { 1344f8829a4aSRandall Stewart *high_tsn = tsn; 1345f8829a4aSRandall Stewart } 1346f8829a4aSRandall Stewart /* See if we have received this one already */ 134777acdc25SRandall Stewart if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) || 134877acdc25SRandall Stewart SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) { 1349f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvdupdata); 1350f8829a4aSRandall Stewart if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) { 1351f8829a4aSRandall Stewart /* Record a dup for the next outbound sack */ 1352f8829a4aSRandall Stewart asoc->dup_tsns[asoc->numduptsns] = tsn; 1353f8829a4aSRandall Stewart asoc->numduptsns++; 1354f8829a4aSRandall Stewart } 135542551e99SRandall Stewart asoc->send_sack = 1; 1356f8829a4aSRandall Stewart return (0); 1357f8829a4aSRandall Stewart } 1358f8829a4aSRandall Stewart /* 1359f8829a4aSRandall Stewart * Check to see about the GONE flag, duplicates would cause a sack 1360f8829a4aSRandall Stewart * to be sent up above 1361f8829a4aSRandall Stewart */ 1362ad81507eSRandall Stewart if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 1363f8829a4aSRandall Stewart (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1364ff1ffd74SMichael Tuexen (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) { 1365f8829a4aSRandall Stewart /* 1366f8829a4aSRandall Stewart * wait a minute, this guy is gone, there is no longer a 1367f8829a4aSRandall Stewart * receiver. Send peer an ABORT! 1368f8829a4aSRandall Stewart */ 1369ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, ""); 1370a2b42326SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1371f8829a4aSRandall Stewart *abort_flag = 1; 1372f8829a4aSRandall Stewart return (0); 1373f8829a4aSRandall Stewart } 1374f8829a4aSRandall Stewart /* 1375f8829a4aSRandall Stewart * Now before going further we see if there is room. If NOT then we 1376f8829a4aSRandall Stewart * MAY let one through only IF this TSN is the one we are waiting 1377f8829a4aSRandall Stewart * for on a partial delivery API. 1378f8829a4aSRandall Stewart */ 1379f8829a4aSRandall Stewart 1380f8829a4aSRandall Stewart /* now do the tests */ 1381f8829a4aSRandall Stewart if (((asoc->cnt_on_all_streams + 1382f8829a4aSRandall Stewart asoc->cnt_on_reasm_queue + 1383b3f1ea41SRandall Stewart asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) || 1384f8829a4aSRandall Stewart (((int)asoc->my_rwnd) <= 0)) { 1385f8829a4aSRandall Stewart /* 1386f8829a4aSRandall Stewart * When we have NO room in the rwnd we check to make sure 1387f8829a4aSRandall Stewart * the reader is doing its job... 1388f8829a4aSRandall Stewart */ 13894e88d37aSMichael Tuexen if (stcb->sctp_socket->so_rcv.sb_cc) { 1390f8829a4aSRandall Stewart /* some to read, wake-up */ 1391ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1392ceaad40aSRandall Stewart struct socket *so; 1393ceaad40aSRandall Stewart 1394ceaad40aSRandall Stewart so = SCTP_INP_SO(stcb->sctp_ep); 1395ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 1396ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 1397ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 1398ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 1399ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 1400ceaad40aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 1401ceaad40aSRandall Stewart /* assoc was freed while we were unlocked */ 1402ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 1403ceaad40aSRandall Stewart return (0); 1404ceaad40aSRandall Stewart } 1405ceaad40aSRandall Stewart #endif 1406f8829a4aSRandall Stewart sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket); 1407ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 1408ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 1409ceaad40aSRandall Stewart #endif 1410f8829a4aSRandall Stewart } 1411f8829a4aSRandall Stewart /* now is it in the mapping array of what we have accepted? */ 141220b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) && 141320b07a4dSMichael Tuexen SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) { 1414f8829a4aSRandall Stewart /* Nope not in the valid range dump it */ 1415f8829a4aSRandall Stewart sctp_set_rwnd(stcb, asoc); 1416f8829a4aSRandall Stewart if ((asoc->cnt_on_all_streams + 1417f8829a4aSRandall Stewart asoc->cnt_on_reasm_queue + 1418b3f1ea41SRandall Stewart asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) { 1419f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_datadropchklmt); 1420f8829a4aSRandall Stewart } else { 1421f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_datadroprwnd); 1422f8829a4aSRandall Stewart } 1423f8829a4aSRandall Stewart *break_flag = 1; 1424f8829a4aSRandall Stewart return (0); 1425f8829a4aSRandall Stewart } 1426f8829a4aSRandall Stewart } 1427f8829a4aSRandall Stewart strmno = ntohs(ch->dp.stream_id); 1428f8829a4aSRandall Stewart if (strmno >= asoc->streamincnt) { 1429f8829a4aSRandall Stewart struct sctp_paramhdr *phdr; 1430f8829a4aSRandall Stewart struct mbuf *mb; 1431f8829a4aSRandall Stewart 1432f8829a4aSRandall Stewart mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2), 1433eb1b1807SGleb Smirnoff 0, M_NOWAIT, 1, MT_DATA); 1434f8829a4aSRandall Stewart if (mb != NULL) { 1435f8829a4aSRandall Stewart /* add some space up front so prepend will work well */ 1436139bc87fSRandall Stewart SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr)); 1437f8829a4aSRandall Stewart phdr = mtod(mb, struct sctp_paramhdr *); 1438f8829a4aSRandall Stewart /* 1439f8829a4aSRandall Stewart * Error causes are just param's and this one has 1440f8829a4aSRandall Stewart * two back to back phdr, one with the error type 1441f8829a4aSRandall Stewart * and size, the other with the streamid and a rsvd 1442f8829a4aSRandall Stewart */ 1443139bc87fSRandall Stewart SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2); 1444f8829a4aSRandall Stewart phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM); 1445f8829a4aSRandall Stewart phdr->param_length = 1446f8829a4aSRandall Stewart htons(sizeof(struct sctp_paramhdr) * 2); 1447f8829a4aSRandall Stewart phdr++; 1448f8829a4aSRandall Stewart /* We insert the stream in the type field */ 1449f8829a4aSRandall Stewart phdr->param_type = ch->dp.stream_id; 1450f8829a4aSRandall Stewart /* And set the length to 0 for the rsvd field */ 1451f8829a4aSRandall Stewart phdr->param_length = 0; 1452f8829a4aSRandall Stewart sctp_queue_op_err(stcb, mb); 1453f8829a4aSRandall Stewart } 1454f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_badsid); 1455207304d4SRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 1456830d754dSRandall Stewart SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap); 145720b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) { 1458830d754dSRandall Stewart asoc->highest_tsn_inside_nr_map = tsn; 1459d06c82f1SRandall Stewart } 1460d06c82f1SRandall Stewart if (tsn == (asoc->cumulative_tsn + 1)) { 1461d06c82f1SRandall Stewart /* Update cum-ack */ 1462d06c82f1SRandall Stewart asoc->cumulative_tsn = tsn; 1463d06c82f1SRandall Stewart } 1464f8829a4aSRandall Stewart return (0); 1465f8829a4aSRandall Stewart } 1466f8829a4aSRandall Stewart /* 1467f8829a4aSRandall Stewart * Before we continue lets validate that we are not being fooled by 1468f8829a4aSRandall Stewart * an evil attacker. We can only have 4k chunks based on our TSN 1469f8829a4aSRandall Stewart * spread allowed by the mapping array 512 * 8 bits, so there is no 1470f8829a4aSRandall Stewart * way our stream sequence numbers could have wrapped. We of course 1471f8829a4aSRandall Stewart * only validate the FIRST fragment so the bit must be set. 1472f8829a4aSRandall Stewart */ 1473f8829a4aSRandall Stewart strmseq = ntohs(ch->dp.stream_sequence); 1474f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 147518e198d3SRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 147618e198d3SRandall Stewart if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) { 147718e198d3SRandall Stewart asoc->tsn_in_at = 0; 147818e198d3SRandall Stewart asoc->tsn_in_wrapped = 1; 147918e198d3SRandall Stewart } 1480f42a358aSRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn; 1481f42a358aSRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno; 1482f42a358aSRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq; 1483f1f73e57SRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length; 1484f1f73e57SRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags; 148518e198d3SRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb; 148618e198d3SRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at; 148718e198d3SRandall Stewart asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1; 1488f42a358aSRandall Stewart asoc->tsn_in_at++; 1489f42a358aSRandall Stewart #endif 1490f42a358aSRandall Stewart if ((chunk_flags & SCTP_DATA_FIRST_FRAG) && 1491d61a0ae0SRandall Stewart (TAILQ_EMPTY(&asoc->resetHead)) && 1492f42a358aSRandall Stewart (chunk_flags & SCTP_DATA_UNORDERED) == 0 && 149320b07a4dSMichael Tuexen SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) { 1494f8829a4aSRandall Stewart /* The incoming sseq is behind where we last delivered? */ 1495ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n", 1496ad81507eSRandall Stewart strmseq, asoc->strmin[strmno].last_sequence_delivered); 1497f8829a4aSRandall Stewart 1498ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1499ff1ffd74SMichael Tuexen asoc->strmin[strmno].last_sequence_delivered, 1500ff1ffd74SMichael Tuexen tsn, strmno, strmseq); 1501ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1502b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16; 1503ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1504f8829a4aSRandall Stewart *abort_flag = 1; 1505f8829a4aSRandall Stewart return (0); 1506f8829a4aSRandall Stewart } 1507f42a358aSRandall Stewart /************************************ 1508f42a358aSRandall Stewart * From here down we may find ch-> invalid 1509f42a358aSRandall Stewart * so its a good idea NOT to use it. 1510f42a358aSRandall Stewart *************************************/ 1511f42a358aSRandall Stewart 1512f8829a4aSRandall Stewart the_len = (chk_length - sizeof(struct sctp_data_chunk)); 1513f8829a4aSRandall Stewart if (last_chunk == 0) { 151444b7479bSRandall Stewart dmbuf = SCTP_M_COPYM(*m, 1515f8829a4aSRandall Stewart (offset + sizeof(struct sctp_data_chunk)), 1516eb1b1807SGleb Smirnoff the_len, M_NOWAIT); 1517f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING 1518b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 15194be807c4SMichael Tuexen sctp_log_mbc(dmbuf, SCTP_MBUF_ICOPY); 1520f8829a4aSRandall Stewart } 1521f8829a4aSRandall Stewart #endif 1522f8829a4aSRandall Stewart } else { 1523f8829a4aSRandall Stewart /* We can steal the last chunk */ 1524139bc87fSRandall Stewart int l_len; 1525139bc87fSRandall Stewart 1526f8829a4aSRandall Stewart dmbuf = *m; 1527f8829a4aSRandall Stewart /* lop off the top part */ 1528f8829a4aSRandall Stewart m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk))); 1529139bc87fSRandall Stewart if (SCTP_BUF_NEXT(dmbuf) == NULL) { 1530139bc87fSRandall Stewart l_len = SCTP_BUF_LEN(dmbuf); 1531139bc87fSRandall Stewart } else { 1532139bc87fSRandall Stewart /* 1533139bc87fSRandall Stewart * need to count up the size hopefully does not hit 1534139bc87fSRandall Stewart * this to often :-0 1535139bc87fSRandall Stewart */ 1536139bc87fSRandall Stewart struct mbuf *lat; 1537139bc87fSRandall Stewart 1538139bc87fSRandall Stewart l_len = 0; 153960990c0cSMichael Tuexen for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) { 1540139bc87fSRandall Stewart l_len += SCTP_BUF_LEN(lat); 1541139bc87fSRandall Stewart } 1542139bc87fSRandall Stewart } 1543139bc87fSRandall Stewart if (l_len > the_len) { 1544f8829a4aSRandall Stewart /* Trim the end round bytes off too */ 1545139bc87fSRandall Stewart m_adj(dmbuf, -(l_len - the_len)); 1546f8829a4aSRandall Stewart } 1547f8829a4aSRandall Stewart } 1548f8829a4aSRandall Stewart if (dmbuf == NULL) { 1549f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_nomem); 1550f8829a4aSRandall Stewart return (0); 1551f8829a4aSRandall Stewart } 1552f42a358aSRandall Stewart if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG && 1553f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress == 0 && 1554f8829a4aSRandall Stewart TAILQ_EMPTY(&asoc->resetHead) && 1555f42a358aSRandall Stewart ((ordered == 0) || 155636ec9f81SMichael Tuexen ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq && 1557f8829a4aSRandall Stewart TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) { 1558f8829a4aSRandall Stewart /* Candidate for express delivery */ 1559f8829a4aSRandall Stewart /* 1560f8829a4aSRandall Stewart * Its not fragmented, No PD-API is up, Nothing in the 1561f8829a4aSRandall Stewart * delivery queue, Its un-ordered OR ordered and the next to 1562f8829a4aSRandall Stewart * deliver AND nothing else is stuck on the stream queue, 1563f8829a4aSRandall Stewart * And there is room for it in the socket buffer. Lets just 1564f8829a4aSRandall Stewart * stuff it up the buffer.... 1565f8829a4aSRandall Stewart */ 1566f8829a4aSRandall Stewart 1567f8829a4aSRandall Stewart /* It would be nice to avoid this copy if we could :< */ 1568f8829a4aSRandall Stewart sctp_alloc_a_readq(stcb, control); 1569f8829a4aSRandall Stewart sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn, 1570f42a358aSRandall Stewart protocol_id, 1571f8829a4aSRandall Stewart strmno, strmseq, 1572f42a358aSRandall Stewart chunk_flags, 1573f8829a4aSRandall Stewart dmbuf); 1574f8829a4aSRandall Stewart if (control == NULL) { 1575f8829a4aSRandall Stewart goto failed_express_del; 1576f8829a4aSRandall Stewart } 15771ea735c8SMichael Tuexen SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap); 157820b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) { 15791ea735c8SMichael Tuexen asoc->highest_tsn_inside_nr_map = tsn; 15801ea735c8SMichael Tuexen } 1581cfde3ff7SRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 1582cfde3ff7SRandall Stewart control, &stcb->sctp_socket->so_rcv, 1583cfde3ff7SRandall Stewart 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 1584830d754dSRandall Stewart 1585f42a358aSRandall Stewart if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) { 1586f8829a4aSRandall Stewart /* for ordered, bump what we delivered */ 1587f8829a4aSRandall Stewart asoc->strmin[strmno].last_sequence_delivered++; 1588f8829a4aSRandall Stewart } 1589f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvexpress); 1590b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 15916a91f103SRandall Stewart sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, 1592f8829a4aSRandall Stewart SCTP_STR_LOG_FROM_EXPRS_DEL); 159380fefe0aSRandall Stewart } 1594f8829a4aSRandall Stewart control = NULL; 1595b5c16493SMichael Tuexen 1596f8829a4aSRandall Stewart goto finish_express_del; 1597f8829a4aSRandall Stewart } 1598f8829a4aSRandall Stewart failed_express_del: 1599f8829a4aSRandall Stewart /* If we reach here this is a new chunk */ 1600f8829a4aSRandall Stewart chk = NULL; 1601f8829a4aSRandall Stewart control = NULL; 1602f8829a4aSRandall Stewart /* Express for fragmented delivery? */ 1603f8829a4aSRandall Stewart if ((asoc->fragmented_delivery_inprogress) && 1604f8829a4aSRandall Stewart (stcb->asoc.control_pdapi) && 1605f8829a4aSRandall Stewart (asoc->str_of_pdapi == strmno) && 1606f8829a4aSRandall Stewart (asoc->ssn_of_pdapi == strmseq) 1607f8829a4aSRandall Stewart ) { 1608f8829a4aSRandall Stewart control = stcb->asoc.control_pdapi; 1609f42a358aSRandall Stewart if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) { 1610f8829a4aSRandall Stewart /* Can't be another first? */ 1611f8829a4aSRandall Stewart goto failed_pdapi_express_del; 1612f8829a4aSRandall Stewart } 1613f8829a4aSRandall Stewart if (tsn == (control->sinfo_tsn + 1)) { 1614f8829a4aSRandall Stewart /* Yep, we can add it on */ 1615f8829a4aSRandall Stewart int end = 0; 1616f8829a4aSRandall Stewart 1617f42a358aSRandall Stewart if (chunk_flags & SCTP_DATA_LAST_FRAG) { 1618f8829a4aSRandall Stewart end = 1; 1619f8829a4aSRandall Stewart } 1620f8829a4aSRandall Stewart if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end, 1621f8829a4aSRandall Stewart tsn, 1622f8829a4aSRandall Stewart &stcb->sctp_socket->so_rcv)) { 1623ad81507eSRandall Stewart SCTP_PRINTF("Append fails end:%d\n", end); 1624f8829a4aSRandall Stewart goto failed_pdapi_express_del; 1625f8829a4aSRandall Stewart } 162677acdc25SRandall Stewart SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap); 162720b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) { 1628830d754dSRandall Stewart asoc->highest_tsn_inside_nr_map = tsn; 1629830d754dSRandall Stewart } 1630f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvexpressm); 1631f8829a4aSRandall Stewart asoc->tsn_last_delivered = tsn; 1632f42a358aSRandall Stewart asoc->fragment_flags = chunk_flags; 1633f8829a4aSRandall Stewart asoc->tsn_of_pdapi_last_delivered = tsn; 1634f42a358aSRandall Stewart asoc->last_flags_delivered = chunk_flags; 1635f8829a4aSRandall Stewart asoc->last_strm_seq_delivered = strmseq; 1636f8829a4aSRandall Stewart asoc->last_strm_no_delivered = strmno; 1637f8829a4aSRandall Stewart if (end) { 1638f8829a4aSRandall Stewart /* clean up the flags and such */ 1639f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress = 0; 1640f42a358aSRandall Stewart if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) { 1641f8829a4aSRandall Stewart asoc->strmin[strmno].last_sequence_delivered++; 1642a5d547adSRandall Stewart } 1643f8829a4aSRandall Stewart stcb->asoc.control_pdapi = NULL; 1644139bc87fSRandall Stewart if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) { 1645139bc87fSRandall Stewart /* 1646139bc87fSRandall Stewart * There could be another message 1647139bc87fSRandall Stewart * ready 1648139bc87fSRandall Stewart */ 1649139bc87fSRandall Stewart need_reasm_check = 1; 1650139bc87fSRandall Stewart } 1651f8829a4aSRandall Stewart } 1652f8829a4aSRandall Stewart control = NULL; 1653f8829a4aSRandall Stewart goto finish_express_del; 1654f8829a4aSRandall Stewart } 1655f8829a4aSRandall Stewart } 1656f8829a4aSRandall Stewart failed_pdapi_express_del: 1657f8829a4aSRandall Stewart control = NULL; 165877acdc25SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { 165977acdc25SRandall Stewart SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap); 166020b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) { 166177acdc25SRandall Stewart asoc->highest_tsn_inside_nr_map = tsn; 166277acdc25SRandall Stewart } 166377acdc25SRandall Stewart } else { 166477acdc25SRandall Stewart SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap); 166520b07a4dSMichael Tuexen if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) { 166677acdc25SRandall Stewart asoc->highest_tsn_inside_map = tsn; 166777acdc25SRandall Stewart } 166877acdc25SRandall Stewart } 1669f42a358aSRandall Stewart if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) { 1670f8829a4aSRandall Stewart sctp_alloc_a_chunk(stcb, chk); 1671f8829a4aSRandall Stewart if (chk == NULL) { 1672f8829a4aSRandall Stewart /* No memory so we drop the chunk */ 1673f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_nomem); 1674f8829a4aSRandall Stewart if (last_chunk == 0) { 1675f8829a4aSRandall Stewart /* we copied it, free the copy */ 1676f8829a4aSRandall Stewart sctp_m_freem(dmbuf); 1677f8829a4aSRandall Stewart } 1678f8829a4aSRandall Stewart return (0); 1679f8829a4aSRandall Stewart } 1680f8829a4aSRandall Stewart chk->rec.data.TSN_seq = tsn; 1681f8829a4aSRandall Stewart chk->no_fr_allowed = 0; 1682f8829a4aSRandall Stewart chk->rec.data.stream_seq = strmseq; 1683f8829a4aSRandall Stewart chk->rec.data.stream_number = strmno; 1684f42a358aSRandall Stewart chk->rec.data.payloadtype = protocol_id; 1685f8829a4aSRandall Stewart chk->rec.data.context = stcb->asoc.context; 1686f8829a4aSRandall Stewart chk->rec.data.doing_fast_retransmit = 0; 1687f42a358aSRandall Stewart chk->rec.data.rcv_flags = chunk_flags; 1688f8829a4aSRandall Stewart chk->asoc = asoc; 1689f8829a4aSRandall Stewart chk->send_size = the_len; 1690f8829a4aSRandall Stewart chk->whoTo = net; 1691f8829a4aSRandall Stewart atomic_add_int(&net->ref_count, 1); 1692f8829a4aSRandall Stewart chk->data = dmbuf; 1693f8829a4aSRandall Stewart } else { 1694f8829a4aSRandall Stewart sctp_alloc_a_readq(stcb, control); 1695f8829a4aSRandall Stewart sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn, 1696f42a358aSRandall Stewart protocol_id, 1697f8829a4aSRandall Stewart strmno, strmseq, 1698f42a358aSRandall Stewart chunk_flags, 1699f8829a4aSRandall Stewart dmbuf); 1700f8829a4aSRandall Stewart if (control == NULL) { 1701f8829a4aSRandall Stewart /* No memory so we drop the chunk */ 1702f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_nomem); 1703f8829a4aSRandall Stewart if (last_chunk == 0) { 1704f8829a4aSRandall Stewart /* we copied it, free the copy */ 1705f8829a4aSRandall Stewart sctp_m_freem(dmbuf); 1706f8829a4aSRandall Stewart } 1707f8829a4aSRandall Stewart return (0); 1708f8829a4aSRandall Stewart } 1709f8829a4aSRandall Stewart control->length = the_len; 1710f8829a4aSRandall Stewart } 1711f8829a4aSRandall Stewart 1712f8829a4aSRandall Stewart /* Mark it as received */ 1713f8829a4aSRandall Stewart /* Now queue it where it belongs */ 1714f8829a4aSRandall Stewart if (control != NULL) { 1715f8829a4aSRandall Stewart /* First a sanity check */ 1716f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress) { 1717f8829a4aSRandall Stewart /* 1718f8829a4aSRandall Stewart * Ok, we have a fragmented delivery in progress if 1719f8829a4aSRandall Stewart * this chunk is next to deliver OR belongs in our 1720f8829a4aSRandall Stewart * view to the reassembly, the peer is evil or 1721f8829a4aSRandall Stewart * broken. 1722f8829a4aSRandall Stewart */ 1723f8829a4aSRandall Stewart uint32_t estimate_tsn; 1724f8829a4aSRandall Stewart 1725f8829a4aSRandall Stewart estimate_tsn = asoc->tsn_last_delivered + 1; 1726f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->reasmqueue) && 1727f8829a4aSRandall Stewart (estimate_tsn == control->sinfo_tsn)) { 1728f8829a4aSRandall Stewart /* Evil/Broke peer */ 1729f8829a4aSRandall Stewart sctp_m_freem(control->data); 1730f8829a4aSRandall Stewart control->data = NULL; 17315bead436SRandall Stewart if (control->whoFrom) { 1732f8829a4aSRandall Stewart sctp_free_remote_addr(control->whoFrom); 17335bead436SRandall Stewart control->whoFrom = NULL; 17345bead436SRandall Stewart } 1735f8829a4aSRandall Stewart sctp_free_a_readq(stcb, control); 1736ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), "Reas. queue emtpy, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1737ff1ffd74SMichael Tuexen tsn, strmno, strmseq); 1738ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1739b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17; 1740ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1741f8829a4aSRandall Stewart *abort_flag = 1; 17428be0fd55SMichael Tuexen if (last_chunk) { 17438be0fd55SMichael Tuexen *m = NULL; 17448be0fd55SMichael Tuexen } 1745f8829a4aSRandall Stewart return (0); 1746f8829a4aSRandall Stewart } else { 1747f8829a4aSRandall Stewart if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) { 1748f8829a4aSRandall Stewart sctp_m_freem(control->data); 1749f8829a4aSRandall Stewart control->data = NULL; 17505bead436SRandall Stewart if (control->whoFrom) { 1751f8829a4aSRandall Stewart sctp_free_remote_addr(control->whoFrom); 17525bead436SRandall Stewart control->whoFrom = NULL; 17535bead436SRandall Stewart } 1754f8829a4aSRandall Stewart sctp_free_a_readq(stcb, control); 1755ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), "PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1756ff1ffd74SMichael Tuexen tsn, strmno, strmseq); 1757ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1758b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_18; 1759ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1760f8829a4aSRandall Stewart *abort_flag = 1; 17618be0fd55SMichael Tuexen if (last_chunk) { 17628be0fd55SMichael Tuexen *m = NULL; 17638be0fd55SMichael Tuexen } 1764f8829a4aSRandall Stewart return (0); 1765f8829a4aSRandall Stewart } 1766f8829a4aSRandall Stewart } 1767f8829a4aSRandall Stewart } else { 1768f8829a4aSRandall Stewart /* No PDAPI running */ 1769f8829a4aSRandall Stewart if (!TAILQ_EMPTY(&asoc->reasmqueue)) { 1770f8829a4aSRandall Stewart /* 1771f8829a4aSRandall Stewart * Reassembly queue is NOT empty validate 1772f8829a4aSRandall Stewart * that this tsn does not need to be in 1773f8829a4aSRandall Stewart * reasembly queue. If it does then our peer 1774f8829a4aSRandall Stewart * is broken or evil. 1775f8829a4aSRandall Stewart */ 1776f8829a4aSRandall Stewart if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) { 1777f8829a4aSRandall Stewart sctp_m_freem(control->data); 1778f8829a4aSRandall Stewart control->data = NULL; 17795bead436SRandall Stewart if (control->whoFrom) { 1780f8829a4aSRandall Stewart sctp_free_remote_addr(control->whoFrom); 17815bead436SRandall Stewart control->whoFrom = NULL; 17825bead436SRandall Stewart } 1783f8829a4aSRandall Stewart sctp_free_a_readq(stcb, control); 1784ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), "No PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x", 1785ff1ffd74SMichael Tuexen tsn, strmno, strmseq); 1786ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 1787b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19; 1788ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 1789f8829a4aSRandall Stewart *abort_flag = 1; 17908be0fd55SMichael Tuexen if (last_chunk) { 17918be0fd55SMichael Tuexen *m = NULL; 17928be0fd55SMichael Tuexen } 1793f8829a4aSRandall Stewart return (0); 1794f8829a4aSRandall Stewart } 1795f8829a4aSRandall Stewart } 1796f8829a4aSRandall Stewart } 1797f8829a4aSRandall Stewart /* ok, if we reach here we have passed the sanity checks */ 1798f42a358aSRandall Stewart if (chunk_flags & SCTP_DATA_UNORDERED) { 1799f8829a4aSRandall Stewart /* queue directly into socket buffer */ 1800b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, control->sinfo_tsn); 1801f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 1802f8829a4aSRandall Stewart control, 1803cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED); 1804f8829a4aSRandall Stewart } else { 1805f8829a4aSRandall Stewart /* 1806f8829a4aSRandall Stewart * Special check for when streams are resetting. We 1807f8829a4aSRandall Stewart * could be more smart about this and check the 1808f8829a4aSRandall Stewart * actual stream to see if it is not being reset.. 1809f8829a4aSRandall Stewart * that way we would not create a HOLB when amongst 1810f8829a4aSRandall Stewart * streams being reset and those not being reset. 1811f8829a4aSRandall Stewart * 1812f8829a4aSRandall Stewart * We take complete messages that have a stream reset 1813f8829a4aSRandall Stewart * intervening (aka the TSN is after where our 1814f8829a4aSRandall Stewart * cum-ack needs to be) off and put them on a 1815f8829a4aSRandall Stewart * pending_reply_queue. The reassembly ones we do 1816f8829a4aSRandall Stewart * not have to worry about since they are all sorted 1817f8829a4aSRandall Stewart * and proceessed by TSN order. It is only the 1818f8829a4aSRandall Stewart * singletons I must worry about. 1819f8829a4aSRandall Stewart */ 1820f8829a4aSRandall Stewart if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) && 182120b07a4dSMichael Tuexen SCTP_TSN_GT(tsn, liste->tsn)) { 1822f8829a4aSRandall Stewart /* 1823f8829a4aSRandall Stewart * yep its past where we need to reset... go 1824f8829a4aSRandall Stewart * ahead and queue it. 1825f8829a4aSRandall Stewart */ 1826f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->pending_reply_queue)) { 1827f8829a4aSRandall Stewart /* first one on */ 1828f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next); 1829f8829a4aSRandall Stewart } else { 18304a9ef3f8SMichael Tuexen struct sctp_queued_to_read *ctlOn, 18314a9ef3f8SMichael Tuexen *nctlOn; 1832f8829a4aSRandall Stewart unsigned char inserted = 0; 1833f8829a4aSRandall Stewart 18344a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) { 183520b07a4dSMichael Tuexen if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) { 18364a9ef3f8SMichael Tuexen continue; 1837f8829a4aSRandall Stewart } else { 1838f8829a4aSRandall Stewart /* found it */ 1839f8829a4aSRandall Stewart TAILQ_INSERT_BEFORE(ctlOn, control, next); 1840f8829a4aSRandall Stewart inserted = 1; 1841f8829a4aSRandall Stewart break; 1842f8829a4aSRandall Stewart } 1843f8829a4aSRandall Stewart } 1844f8829a4aSRandall Stewart if (inserted == 0) { 1845f8829a4aSRandall Stewart /* 1846f8829a4aSRandall Stewart * must be put at end, use 1847f8829a4aSRandall Stewart * prevP (all setup from 1848f8829a4aSRandall Stewart * loop) to setup nextP. 1849f8829a4aSRandall Stewart */ 1850f8829a4aSRandall Stewart TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next); 1851f8829a4aSRandall Stewart } 1852f8829a4aSRandall Stewart } 1853f8829a4aSRandall Stewart } else { 1854f8829a4aSRandall Stewart sctp_queue_data_to_stream(stcb, asoc, control, abort_flag); 1855f8829a4aSRandall Stewart if (*abort_flag) { 18568be0fd55SMichael Tuexen if (last_chunk) { 18578be0fd55SMichael Tuexen *m = NULL; 18588be0fd55SMichael Tuexen } 1859f8829a4aSRandall Stewart return (0); 1860f8829a4aSRandall Stewart } 1861f8829a4aSRandall Stewart } 1862f8829a4aSRandall Stewart } 1863f8829a4aSRandall Stewart } else { 1864f8829a4aSRandall Stewart /* Into the re-assembly queue */ 1865f8829a4aSRandall Stewart sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag); 1866f8829a4aSRandall Stewart if (*abort_flag) { 1867a5d547adSRandall Stewart /* 1868a5d547adSRandall Stewart * the assoc is now gone and chk was put onto the 1869a5d547adSRandall Stewart * reasm queue, which has all been freed. 1870a5d547adSRandall Stewart */ 18718be0fd55SMichael Tuexen if (last_chunk) { 1872a5d547adSRandall Stewart *m = NULL; 18738be0fd55SMichael Tuexen } 1874f8829a4aSRandall Stewart return (0); 1875f8829a4aSRandall Stewart } 1876f8829a4aSRandall Stewart } 1877f8829a4aSRandall Stewart finish_express_del: 1878307b49efSMichael Tuexen if (tsn == (asoc->cumulative_tsn + 1)) { 1879307b49efSMichael Tuexen /* Update cum-ack */ 1880307b49efSMichael Tuexen asoc->cumulative_tsn = tsn; 1881307b49efSMichael Tuexen } 1882f8829a4aSRandall Stewart if (last_chunk) { 1883f8829a4aSRandall Stewart *m = NULL; 1884f8829a4aSRandall Stewart } 1885f42a358aSRandall Stewart if (ordered) { 1886f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks); 1887f8829a4aSRandall Stewart } else { 1888f8829a4aSRandall Stewart SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks); 1889f8829a4aSRandall Stewart } 1890f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvdata); 1891f8829a4aSRandall Stewart /* Set it present please */ 1892b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) { 18936a91f103SRandall Stewart sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN); 189480fefe0aSRandall Stewart } 1895b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 1896f8829a4aSRandall Stewart sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, 1897f8829a4aSRandall Stewart asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE); 189880fefe0aSRandall Stewart } 189917205eccSRandall Stewart /* check the special flag for stream resets */ 190017205eccSRandall Stewart if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) && 190120b07a4dSMichael Tuexen SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) { 190217205eccSRandall Stewart /* 190317205eccSRandall Stewart * we have finished working through the backlogged TSN's now 190417205eccSRandall Stewart * time to reset streams. 1: call reset function. 2: free 190517205eccSRandall Stewart * pending_reply space 3: distribute any chunks in 190617205eccSRandall Stewart * pending_reply_queue. 190717205eccSRandall Stewart */ 19084a9ef3f8SMichael Tuexen struct sctp_queued_to_read *ctl, *nctl; 190917205eccSRandall Stewart 1910a169d6ecSMichael Tuexen sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams); 191117205eccSRandall Stewart TAILQ_REMOVE(&asoc->resetHead, liste, next_resp); 19127cca1775SRandall Stewart sctp_send_deferred_reset_response(stcb, liste, SCTP_STREAM_RESET_RESULT_PERFORMED); 1913207304d4SRandall Stewart SCTP_FREE(liste, SCTP_M_STRESET); 19143c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 191517205eccSRandall Stewart liste = TAILQ_FIRST(&asoc->resetHead); 19164a9ef3f8SMichael Tuexen if (TAILQ_EMPTY(&asoc->resetHead)) { 191717205eccSRandall Stewart /* All can be removed */ 19184a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) { 191917205eccSRandall Stewart TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next); 192017205eccSRandall Stewart sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag); 192117205eccSRandall Stewart if (*abort_flag) { 192217205eccSRandall Stewart return (0); 192317205eccSRandall Stewart } 192417205eccSRandall Stewart } 19254a9ef3f8SMichael Tuexen } else { 19264a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) { 192720b07a4dSMichael Tuexen if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) { 19284a9ef3f8SMichael Tuexen break; 19294a9ef3f8SMichael Tuexen } 193017205eccSRandall Stewart /* 193117205eccSRandall Stewart * if ctl->sinfo_tsn is <= liste->tsn we can 193217205eccSRandall Stewart * process it which is the NOT of 193317205eccSRandall Stewart * ctl->sinfo_tsn > liste->tsn 193417205eccSRandall Stewart */ 193517205eccSRandall Stewart TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next); 193617205eccSRandall Stewart sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag); 193717205eccSRandall Stewart if (*abort_flag) { 193817205eccSRandall Stewart return (0); 193917205eccSRandall Stewart } 194017205eccSRandall Stewart } 194117205eccSRandall Stewart } 194217205eccSRandall Stewart /* 194317205eccSRandall Stewart * Now service re-assembly to pick up anything that has been 194417205eccSRandall Stewart * held on reassembly queue? 194517205eccSRandall Stewart */ 194617205eccSRandall Stewart sctp_deliver_reasm_check(stcb, asoc); 194717205eccSRandall Stewart need_reasm_check = 0; 194817205eccSRandall Stewart } 1949139bc87fSRandall Stewart if (need_reasm_check) { 1950139bc87fSRandall Stewart /* Another one waits ? */ 1951139bc87fSRandall Stewart sctp_deliver_reasm_check(stcb, asoc); 1952139bc87fSRandall Stewart } 1953f8829a4aSRandall Stewart return (1); 1954f8829a4aSRandall Stewart } 1955f8829a4aSRandall Stewart 1956f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = { 1957b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1958b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1959b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1960b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 5, 1961b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1962b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1963b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1964b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 6, 1965b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1966b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1967b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1968b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 5, 1969b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1970b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1971b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1972b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 7, 1973b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1974b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1975b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1976b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 5, 1977b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1978b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1979b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1980b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 6, 1981b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1982b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1983b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1984b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 5, 1985b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1986b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 4, 1987b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 3, 1988b5c16493SMichael Tuexen 0, 1, 0, 2, 0, 1, 0, 8 1989f8829a4aSRandall Stewart }; 1990f8829a4aSRandall Stewart 1991f8829a4aSRandall Stewart 1992f8829a4aSRandall Stewart void 1993b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb) 1994f8829a4aSRandall Stewart { 1995f8829a4aSRandall Stewart /* 1996f8829a4aSRandall Stewart * Now we also need to check the mapping array in a couple of ways. 1997f8829a4aSRandall Stewart * 1) Did we move the cum-ack point? 199866bd30bdSRandall Stewart * 199966bd30bdSRandall Stewart * When you first glance at this you might think that all entries that 200066bd30bdSRandall Stewart * make up the postion of the cum-ack would be in the nr-mapping 200166bd30bdSRandall Stewart * array only.. i.e. things up to the cum-ack are always 200266bd30bdSRandall Stewart * deliverable. Thats true with one exception, when its a fragmented 200366bd30bdSRandall Stewart * message we may not deliver the data until some threshold (or all 200466bd30bdSRandall Stewart * of it) is in place. So we must OR the nr_mapping_array and 200566bd30bdSRandall Stewart * mapping_array to get a true picture of the cum-ack. 2006f8829a4aSRandall Stewart */ 2007f8829a4aSRandall Stewart struct sctp_association *asoc; 2008b3f1ea41SRandall Stewart int at; 200966bd30bdSRandall Stewart uint8_t val; 2010f8829a4aSRandall Stewart int slide_from, slide_end, lgap, distance; 201177acdc25SRandall Stewart uint32_t old_cumack, old_base, old_highest, highest_tsn; 2012f8829a4aSRandall Stewart 2013f8829a4aSRandall Stewart asoc = &stcb->asoc; 2014f8829a4aSRandall Stewart 2015f8829a4aSRandall Stewart old_cumack = asoc->cumulative_tsn; 2016f8829a4aSRandall Stewart old_base = asoc->mapping_array_base_tsn; 2017f8829a4aSRandall Stewart old_highest = asoc->highest_tsn_inside_map; 2018f8829a4aSRandall Stewart /* 2019f8829a4aSRandall Stewart * We could probably improve this a small bit by calculating the 2020f8829a4aSRandall Stewart * offset of the current cum-ack as the starting point. 2021f8829a4aSRandall Stewart */ 2022f8829a4aSRandall Stewart at = 0; 2023b5c16493SMichael Tuexen for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) { 202466bd30bdSRandall Stewart val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from]; 202566bd30bdSRandall Stewart if (val == 0xff) { 2026f8829a4aSRandall Stewart at += 8; 2027f8829a4aSRandall Stewart } else { 2028f8829a4aSRandall Stewart /* there is a 0 bit */ 202966bd30bdSRandall Stewart at += sctp_map_lookup_tab[val]; 2030f8829a4aSRandall Stewart break; 2031f8829a4aSRandall Stewart } 2032f8829a4aSRandall Stewart } 2033b5c16493SMichael Tuexen asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1); 2034f8829a4aSRandall Stewart 203520b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) && 203620b07a4dSMichael Tuexen SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) { 2037a5d547adSRandall Stewart #ifdef INVARIANTS 2038ceaad40aSRandall Stewart panic("huh, cumack 0x%x greater than high-tsn 0x%x in map", 2039ceaad40aSRandall Stewart asoc->cumulative_tsn, asoc->highest_tsn_inside_map); 2040f8829a4aSRandall Stewart #else 2041ceaad40aSRandall Stewart SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n", 2042ceaad40aSRandall Stewart asoc->cumulative_tsn, asoc->highest_tsn_inside_map); 20430e13104dSRandall Stewart sctp_print_mapping_array(asoc); 2044b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 2045b3f1ea41SRandall Stewart sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 2046b3f1ea41SRandall Stewart } 2047f8829a4aSRandall Stewart asoc->highest_tsn_inside_map = asoc->cumulative_tsn; 2048830d754dSRandall Stewart asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn; 2049f8829a4aSRandall Stewart #endif 2050f8829a4aSRandall Stewart } 205120b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) { 205277acdc25SRandall Stewart highest_tsn = asoc->highest_tsn_inside_nr_map; 205377acdc25SRandall Stewart } else { 205477acdc25SRandall Stewart highest_tsn = asoc->highest_tsn_inside_map; 205577acdc25SRandall Stewart } 205677acdc25SRandall Stewart if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) { 2057f8829a4aSRandall Stewart /* The complete array was completed by a single FR */ 205877acdc25SRandall Stewart /* highest becomes the cum-ack */ 205937f144ebSMichael Tuexen int clr; 206037f144ebSMichael Tuexen 206137f144ebSMichael Tuexen #ifdef INVARIANTS 206237f144ebSMichael Tuexen unsigned int i; 206337f144ebSMichael Tuexen 206437f144ebSMichael Tuexen #endif 2065f8829a4aSRandall Stewart 2066f8829a4aSRandall Stewart /* clear the array */ 2067b5c16493SMichael Tuexen clr = ((at + 7) >> 3); 2068c4739e2fSRandall Stewart if (clr > asoc->mapping_array_size) { 2069f8829a4aSRandall Stewart clr = asoc->mapping_array_size; 2070f8829a4aSRandall Stewart } 2071f8829a4aSRandall Stewart memset(asoc->mapping_array, 0, clr); 2072830d754dSRandall Stewart memset(asoc->nr_mapping_array, 0, clr); 207337f144ebSMichael Tuexen #ifdef INVARIANTS 2074b5c16493SMichael Tuexen for (i = 0; i < asoc->mapping_array_size; i++) { 2075b5c16493SMichael Tuexen if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) { 2076cd3fd531SMichael Tuexen SCTP_PRINTF("Error Mapping array's not clean at clear\n"); 2077b5c16493SMichael Tuexen sctp_print_mapping_array(asoc); 2078b5c16493SMichael Tuexen } 2079b5c16493SMichael Tuexen } 208037f144ebSMichael Tuexen #endif 208177acdc25SRandall Stewart asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1; 208277acdc25SRandall Stewart asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn; 2083f8829a4aSRandall Stewart } else if (at >= 8) { 2084f8829a4aSRandall Stewart /* we can slide the mapping array down */ 2085b3f1ea41SRandall Stewart /* slide_from holds where we hit the first NON 0xff byte */ 2086b3f1ea41SRandall Stewart 2087f8829a4aSRandall Stewart /* 2088f8829a4aSRandall Stewart * now calculate the ceiling of the move using our highest 2089f8829a4aSRandall Stewart * TSN value 2090f8829a4aSRandall Stewart */ 209177acdc25SRandall Stewart SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn); 209277acdc25SRandall Stewart slide_end = (lgap >> 3); 2093f8829a4aSRandall Stewart if (slide_end < slide_from) { 209477acdc25SRandall Stewart sctp_print_mapping_array(asoc); 2095d55b0b1bSRandall Stewart #ifdef INVARIANTS 2096f8829a4aSRandall Stewart panic("impossible slide"); 2097d55b0b1bSRandall Stewart #else 2098cd3fd531SMichael Tuexen SCTP_PRINTF("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n", 209977acdc25SRandall Stewart lgap, slide_end, slide_from, at); 2100d55b0b1bSRandall Stewart return; 2101d55b0b1bSRandall Stewart #endif 2102f8829a4aSRandall Stewart } 2103b3f1ea41SRandall Stewart if (slide_end > asoc->mapping_array_size) { 2104b3f1ea41SRandall Stewart #ifdef INVARIANTS 2105b3f1ea41SRandall Stewart panic("would overrun buffer"); 2106b3f1ea41SRandall Stewart #else 2107cd3fd531SMichael Tuexen SCTP_PRINTF("Gak, would have overrun map end:%d slide_end:%d\n", 2108b3f1ea41SRandall Stewart asoc->mapping_array_size, slide_end); 2109b3f1ea41SRandall Stewart slide_end = asoc->mapping_array_size; 2110b3f1ea41SRandall Stewart #endif 2111b3f1ea41SRandall Stewart } 2112f8829a4aSRandall Stewart distance = (slide_end - slide_from) + 1; 2113b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 2114f8829a4aSRandall Stewart sctp_log_map(old_base, old_cumack, old_highest, 2115f8829a4aSRandall Stewart SCTP_MAP_PREPARE_SLIDE); 2116f8829a4aSRandall Stewart sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end, 2117f8829a4aSRandall Stewart (uint32_t) lgap, SCTP_MAP_SLIDE_FROM); 211880fefe0aSRandall Stewart } 2119f8829a4aSRandall Stewart if (distance + slide_from > asoc->mapping_array_size || 2120f8829a4aSRandall Stewart distance < 0) { 2121f8829a4aSRandall Stewart /* 2122f8829a4aSRandall Stewart * Here we do NOT slide forward the array so that 2123f8829a4aSRandall Stewart * hopefully when more data comes in to fill it up 2124f8829a4aSRandall Stewart * we will be able to slide it forward. Really I 2125f8829a4aSRandall Stewart * don't think this should happen :-0 2126f8829a4aSRandall Stewart */ 2127f8829a4aSRandall Stewart 2128b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 2129f8829a4aSRandall Stewart sctp_log_map((uint32_t) distance, (uint32_t) slide_from, 2130f8829a4aSRandall Stewart (uint32_t) asoc->mapping_array_size, 2131f8829a4aSRandall Stewart SCTP_MAP_SLIDE_NONE); 213280fefe0aSRandall Stewart } 2133f8829a4aSRandall Stewart } else { 2134f8829a4aSRandall Stewart int ii; 2135f8829a4aSRandall Stewart 2136f8829a4aSRandall Stewart for (ii = 0; ii < distance; ii++) { 213737f144ebSMichael Tuexen asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii]; 213837f144ebSMichael Tuexen asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii]; 213977acdc25SRandall Stewart 2140f8829a4aSRandall Stewart } 2141aed5947cSMichael Tuexen for (ii = distance; ii < asoc->mapping_array_size; ii++) { 2142f8829a4aSRandall Stewart asoc->mapping_array[ii] = 0; 214377acdc25SRandall Stewart asoc->nr_mapping_array[ii] = 0; 2144f8829a4aSRandall Stewart } 2145ee94f0a2SMichael Tuexen if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) { 2146ee94f0a2SMichael Tuexen asoc->highest_tsn_inside_map += (slide_from << 3); 2147ee94f0a2SMichael Tuexen } 2148ee94f0a2SMichael Tuexen if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) { 2149ee94f0a2SMichael Tuexen asoc->highest_tsn_inside_nr_map += (slide_from << 3); 2150ee94f0a2SMichael Tuexen } 2151f8829a4aSRandall Stewart asoc->mapping_array_base_tsn += (slide_from << 3); 2152b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 2153f8829a4aSRandall Stewart sctp_log_map(asoc->mapping_array_base_tsn, 2154f8829a4aSRandall Stewart asoc->cumulative_tsn, asoc->highest_tsn_inside_map, 2155f8829a4aSRandall Stewart SCTP_MAP_SLIDE_RESULT); 215680fefe0aSRandall Stewart } 2157830d754dSRandall Stewart } 2158830d754dSRandall Stewart } 2159b5c16493SMichael Tuexen } 2160b5c16493SMichael Tuexen 2161b5c16493SMichael Tuexen void 21627215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap) 2163b5c16493SMichael Tuexen { 2164b5c16493SMichael Tuexen struct sctp_association *asoc; 2165b5c16493SMichael Tuexen uint32_t highest_tsn; 2166b5c16493SMichael Tuexen 2167b5c16493SMichael Tuexen asoc = &stcb->asoc; 216820b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) { 2169b5c16493SMichael Tuexen highest_tsn = asoc->highest_tsn_inside_nr_map; 2170b5c16493SMichael Tuexen } else { 2171b5c16493SMichael Tuexen highest_tsn = asoc->highest_tsn_inside_map; 2172b5c16493SMichael Tuexen } 2173b5c16493SMichael Tuexen 2174830d754dSRandall Stewart /* 2175f8829a4aSRandall Stewart * Now we need to see if we need to queue a sack or just start the 2176f8829a4aSRandall Stewart * timer (if allowed). 2177f8829a4aSRandall Stewart */ 2178f8829a4aSRandall Stewart if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 2179f8829a4aSRandall Stewart /* 2180b5c16493SMichael Tuexen * Ok special case, in SHUTDOWN-SENT case. here we maker 2181b5c16493SMichael Tuexen * sure SACK timer is off and instead send a SHUTDOWN and a 2182b5c16493SMichael Tuexen * SACK 2183f8829a4aSRandall Stewart */ 2184139bc87fSRandall Stewart if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { 2185f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_RECV, 2186b7d130beSMichael Tuexen stcb->sctp_ep, stcb, NULL, 2187b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_20); 2188f8829a4aSRandall Stewart } 2189ca85e948SMichael Tuexen sctp_send_shutdown(stcb, 2190ca85e948SMichael Tuexen ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination)); 2191689e6a5fSMichael Tuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 2192f8829a4aSRandall Stewart } else { 2193f8829a4aSRandall Stewart int is_a_gap; 2194f8829a4aSRandall Stewart 2195f8829a4aSRandall Stewart /* is there a gap now ? */ 219620b07a4dSMichael Tuexen is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 2197f8829a4aSRandall Stewart 2198f8829a4aSRandall Stewart /* 2199b5c16493SMichael Tuexen * CMT DAC algorithm: increase number of packets received 2200b5c16493SMichael Tuexen * since last ack 2201f8829a4aSRandall Stewart */ 2202f8829a4aSRandall Stewart stcb->asoc.cmt_dac_pkts_rcvd++; 2203f8829a4aSRandall Stewart 220442551e99SRandall Stewart if ((stcb->asoc.send_sack == 1) || /* We need to send a 220542551e99SRandall Stewart * SACK */ 2206f8829a4aSRandall Stewart ((was_a_gap) && (is_a_gap == 0)) || /* was a gap, but no 2207f8829a4aSRandall Stewart * longer is one */ 2208f8829a4aSRandall Stewart (stcb->asoc.numduptsns) || /* we have dup's */ 2209f8829a4aSRandall Stewart (is_a_gap) || /* is still a gap */ 221042551e99SRandall Stewart (stcb->asoc.delayed_ack == 0) || /* Delayed sack disabled */ 221142551e99SRandall Stewart (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) /* hit limit of pkts */ 2212f8829a4aSRandall Stewart ) { 2213f8829a4aSRandall Stewart 22147c99d56fSMichael Tuexen if ((stcb->asoc.sctp_cmt_on_off > 0) && 2215b3f1ea41SRandall Stewart (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) && 221642551e99SRandall Stewart (stcb->asoc.send_sack == 0) && 2217f8829a4aSRandall Stewart (stcb->asoc.numduptsns == 0) && 2218f8829a4aSRandall Stewart (stcb->asoc.delayed_ack) && 2219139bc87fSRandall Stewart (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) { 2220f8829a4aSRandall Stewart 2221f8829a4aSRandall Stewart /* 2222b5c16493SMichael Tuexen * CMT DAC algorithm: With CMT, delay acks 2223b5c16493SMichael Tuexen * even in the face of 2224f8829a4aSRandall Stewart * 2225b5c16493SMichael Tuexen * reordering. Therefore, if acks that do not 2226b5c16493SMichael Tuexen * have to be sent because of the above 2227b5c16493SMichael Tuexen * reasons, will be delayed. That is, acks 2228b5c16493SMichael Tuexen * that would have been sent due to gap 2229b5c16493SMichael Tuexen * reports will be delayed with DAC. Start 2230f8829a4aSRandall Stewart * the delayed ack timer. 2231f8829a4aSRandall Stewart */ 2232f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_RECV, 2233f8829a4aSRandall Stewart stcb->sctp_ep, stcb, NULL); 2234f8829a4aSRandall Stewart } else { 2235f8829a4aSRandall Stewart /* 2236b5c16493SMichael Tuexen * Ok we must build a SACK since the timer 2237b5c16493SMichael Tuexen * is pending, we got our first packet OR 2238b5c16493SMichael Tuexen * there are gaps or duplicates. 2239f8829a4aSRandall Stewart */ 2240ad81507eSRandall Stewart (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); 2241689e6a5fSMichael Tuexen sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); 2242f8829a4aSRandall Stewart } 2243f8829a4aSRandall Stewart } else { 224442551e99SRandall Stewart if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) { 2245f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_RECV, 2246f8829a4aSRandall Stewart stcb->sctp_ep, stcb, NULL); 2247f8829a4aSRandall Stewart } 2248f8829a4aSRandall Stewart } 2249f8829a4aSRandall Stewart } 2250f8829a4aSRandall Stewart } 2251f8829a4aSRandall Stewart 2252f8829a4aSRandall Stewart void 2253f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc) 2254f8829a4aSRandall Stewart { 2255f8829a4aSRandall Stewart struct sctp_tmit_chunk *chk; 2256810ec536SMichael Tuexen uint32_t tsize, pd_point; 2257f8829a4aSRandall Stewart uint16_t nxt_todel; 2258f8829a4aSRandall Stewart 2259f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress) { 2260f8829a4aSRandall Stewart sctp_service_reassembly(stcb, asoc); 2261f8829a4aSRandall Stewart } 2262f8829a4aSRandall Stewart /* Can we proceed further, i.e. the PD-API is complete */ 2263f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress) { 2264f8829a4aSRandall Stewart /* no */ 2265f8829a4aSRandall Stewart return; 2266f8829a4aSRandall Stewart } 2267f8829a4aSRandall Stewart /* 2268f8829a4aSRandall Stewart * Now is there some other chunk I can deliver from the reassembly 2269f8829a4aSRandall Stewart * queue. 2270f8829a4aSRandall Stewart */ 2271139bc87fSRandall Stewart doit_again: 2272f8829a4aSRandall Stewart chk = TAILQ_FIRST(&asoc->reasmqueue); 2273f8829a4aSRandall Stewart if (chk == NULL) { 2274f8829a4aSRandall Stewart asoc->size_on_reasm_queue = 0; 2275f8829a4aSRandall Stewart asoc->cnt_on_reasm_queue = 0; 2276f8829a4aSRandall Stewart return; 2277f8829a4aSRandall Stewart } 2278f8829a4aSRandall Stewart nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1; 2279f8829a4aSRandall Stewart if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) && 2280f8829a4aSRandall Stewart ((nxt_todel == chk->rec.data.stream_seq) || 2281f8829a4aSRandall Stewart (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) { 2282f8829a4aSRandall Stewart /* 2283f8829a4aSRandall Stewart * Yep the first one is here. We setup to start reception, 2284f8829a4aSRandall Stewart * by backing down the TSN just in case we can't deliver. 2285f8829a4aSRandall Stewart */ 2286f8829a4aSRandall Stewart 2287f8829a4aSRandall Stewart /* 2288f8829a4aSRandall Stewart * Before we start though either all of the message should 228924ae5c4aSMichael Tuexen * be here or the socket buffer max or nothing on the 2290f8829a4aSRandall Stewart * delivery queue and something can be delivered. 2291f8829a4aSRandall Stewart */ 2292810ec536SMichael Tuexen if (stcb->sctp_socket) { 2293d4d23375SMichael Tuexen pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT, 2294810ec536SMichael Tuexen stcb->sctp_ep->partial_delivery_point); 2295810ec536SMichael Tuexen } else { 2296810ec536SMichael Tuexen pd_point = stcb->sctp_ep->partial_delivery_point; 2297810ec536SMichael Tuexen } 2298810ec536SMichael Tuexen if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) { 2299f8829a4aSRandall Stewart asoc->fragmented_delivery_inprogress = 1; 2300f8829a4aSRandall Stewart asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1; 2301f8829a4aSRandall Stewart asoc->str_of_pdapi = chk->rec.data.stream_number; 2302f8829a4aSRandall Stewart asoc->ssn_of_pdapi = chk->rec.data.stream_seq; 2303f8829a4aSRandall Stewart asoc->pdapi_ppid = chk->rec.data.payloadtype; 2304f8829a4aSRandall Stewart asoc->fragment_flags = chk->rec.data.rcv_flags; 2305f8829a4aSRandall Stewart sctp_service_reassembly(stcb, asoc); 2306139bc87fSRandall Stewart if (asoc->fragmented_delivery_inprogress == 0) { 2307139bc87fSRandall Stewart goto doit_again; 2308139bc87fSRandall Stewart } 2309f8829a4aSRandall Stewart } 2310f8829a4aSRandall Stewart } 2311f8829a4aSRandall Stewart } 2312f8829a4aSRandall Stewart 2313f8829a4aSRandall Stewart int 2314f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length, 2315b1754ad1SMichael Tuexen struct sockaddr *src, struct sockaddr *dst, 2316f30ac432SMichael Tuexen struct sctphdr *sh, struct sctp_inpcb *inp, 2317f30ac432SMichael Tuexen struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t * high_tsn, 2318457b4b88SMichael Tuexen uint8_t mflowtype, uint32_t mflowid, 2319f30ac432SMichael Tuexen uint32_t vrf_id, uint16_t port) 2320f8829a4aSRandall Stewart { 2321f8829a4aSRandall Stewart struct sctp_data_chunk *ch, chunk_buf; 2322f8829a4aSRandall Stewart struct sctp_association *asoc; 2323f8829a4aSRandall Stewart int num_chunks = 0; /* number of control chunks processed */ 2324f8829a4aSRandall Stewart int stop_proc = 0; 2325f8829a4aSRandall Stewart int chk_length, break_flag, last_chunk; 23268f777478SMichael Tuexen int abort_flag = 0, was_a_gap; 2327f8829a4aSRandall Stewart struct mbuf *m; 23288f777478SMichael Tuexen uint32_t highest_tsn; 2329f8829a4aSRandall Stewart 2330f8829a4aSRandall Stewart /* set the rwnd */ 2331f8829a4aSRandall Stewart sctp_set_rwnd(stcb, &stcb->asoc); 2332f8829a4aSRandall Stewart 2333f8829a4aSRandall Stewart m = *mm; 2334f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 2335f8829a4aSRandall Stewart asoc = &stcb->asoc; 233620b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) { 23378f777478SMichael Tuexen highest_tsn = asoc->highest_tsn_inside_nr_map; 23388f777478SMichael Tuexen } else { 23398f777478SMichael Tuexen highest_tsn = asoc->highest_tsn_inside_map; 2340f8829a4aSRandall Stewart } 234120b07a4dSMichael Tuexen was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn); 2342f8829a4aSRandall Stewart /* 2343f8829a4aSRandall Stewart * setup where we got the last DATA packet from for any SACK that 2344f8829a4aSRandall Stewart * may need to go out. Don't bump the net. This is done ONLY when a 2345f8829a4aSRandall Stewart * chunk is assigned. 2346f8829a4aSRandall Stewart */ 2347f8829a4aSRandall Stewart asoc->last_data_chunk_from = net; 2348f8829a4aSRandall Stewart 2349d06c82f1SRandall Stewart /*- 2350f8829a4aSRandall Stewart * Now before we proceed we must figure out if this is a wasted 2351f8829a4aSRandall Stewart * cluster... i.e. it is a small packet sent in and yet the driver 2352f8829a4aSRandall Stewart * underneath allocated a full cluster for it. If so we must copy it 2353f8829a4aSRandall Stewart * to a smaller mbuf and free up the cluster mbuf. This will help 2354d06c82f1SRandall Stewart * with cluster starvation. Note for __Panda__ we don't do this 2355d06c82f1SRandall Stewart * since it has clusters all the way down to 64 bytes. 2356f8829a4aSRandall Stewart */ 235744b7479bSRandall Stewart if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) { 2358f8829a4aSRandall Stewart /* we only handle mbufs that are singletons.. not chains */ 2359eb1b1807SGleb Smirnoff m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA); 2360f8829a4aSRandall Stewart if (m) { 2361f8829a4aSRandall Stewart /* ok lets see if we can copy the data up */ 2362f8829a4aSRandall Stewart caddr_t *from, *to; 2363f8829a4aSRandall Stewart 2364f8829a4aSRandall Stewart /* get the pointers and copy */ 2365f8829a4aSRandall Stewart to = mtod(m, caddr_t *); 2366f8829a4aSRandall Stewart from = mtod((*mm), caddr_t *); 2367139bc87fSRandall Stewart memcpy(to, from, SCTP_BUF_LEN((*mm))); 2368f8829a4aSRandall Stewart /* copy the length and free up the old */ 2369139bc87fSRandall Stewart SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm)); 2370f8829a4aSRandall Stewart sctp_m_freem(*mm); 2371f8829a4aSRandall Stewart /* sucess, back copy */ 2372f8829a4aSRandall Stewart *mm = m; 2373f8829a4aSRandall Stewart } else { 2374f8829a4aSRandall Stewart /* We are in trouble in the mbuf world .. yikes */ 2375f8829a4aSRandall Stewart m = *mm; 2376f8829a4aSRandall Stewart } 2377f8829a4aSRandall Stewart } 2378f8829a4aSRandall Stewart /* get pointer to the first chunk header */ 2379f8829a4aSRandall Stewart ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset, 2380f8829a4aSRandall Stewart sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf); 2381f8829a4aSRandall Stewart if (ch == NULL) { 2382f8829a4aSRandall Stewart return (1); 2383f8829a4aSRandall Stewart } 2384f8829a4aSRandall Stewart /* 2385f8829a4aSRandall Stewart * process all DATA chunks... 2386f8829a4aSRandall Stewart */ 2387f8829a4aSRandall Stewart *high_tsn = asoc->cumulative_tsn; 2388f8829a4aSRandall Stewart break_flag = 0; 238942551e99SRandall Stewart asoc->data_pkts_seen++; 2390f8829a4aSRandall Stewart while (stop_proc == 0) { 2391f8829a4aSRandall Stewart /* validate chunk length */ 2392f8829a4aSRandall Stewart chk_length = ntohs(ch->ch.chunk_length); 2393f8829a4aSRandall Stewart if (length - *offset < chk_length) { 2394f8829a4aSRandall Stewart /* all done, mutulated chunk */ 2395f8829a4aSRandall Stewart stop_proc = 1; 239660990c0cSMichael Tuexen continue; 2397f8829a4aSRandall Stewart } 2398f8829a4aSRandall Stewart if (ch->ch.chunk_type == SCTP_DATA) { 239932451da4SMichael Tuexen if ((size_t)chk_length < sizeof(struct sctp_data_chunk)) { 2400f8829a4aSRandall Stewart /* 2401f8829a4aSRandall Stewart * Need to send an abort since we had a 2402f8829a4aSRandall Stewart * invalid data chunk. 2403f8829a4aSRandall Stewart */ 2404f8829a4aSRandall Stewart struct mbuf *op_err; 2405ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 2406f8829a4aSRandall Stewart 2407ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), "DATA chunk of length %d", 2408ff1ffd74SMichael Tuexen chk_length); 2409ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 2410b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_21; 2411b1754ad1SMichael Tuexen sctp_abort_association(inp, stcb, m, iphlen, 2412b1754ad1SMichael Tuexen src, dst, sh, op_err, 2413457b4b88SMichael Tuexen mflowtype, mflowid, 2414f30ac432SMichael Tuexen vrf_id, port); 2415f8829a4aSRandall Stewart return (2); 2416f8829a4aSRandall Stewart } 241732451da4SMichael Tuexen if ((size_t)chk_length == sizeof(struct sctp_data_chunk)) { 241832451da4SMichael Tuexen /* 241932451da4SMichael Tuexen * Need to send an abort since we had an 242032451da4SMichael Tuexen * empty data chunk. 242132451da4SMichael Tuexen */ 242232451da4SMichael Tuexen struct mbuf *op_err; 242332451da4SMichael Tuexen 242432451da4SMichael Tuexen op_err = sctp_generate_no_user_data_cause(ch->dp.tsn); 2425b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_22; 242632451da4SMichael Tuexen sctp_abort_association(inp, stcb, m, iphlen, 242732451da4SMichael Tuexen src, dst, sh, op_err, 2428457b4b88SMichael Tuexen mflowtype, mflowid, 242932451da4SMichael Tuexen vrf_id, port); 243032451da4SMichael Tuexen return (2); 243132451da4SMichael Tuexen } 2432f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 2433f8829a4aSRandall Stewart sctp_audit_log(0xB1, 0); 2434f8829a4aSRandall Stewart #endif 2435f8829a4aSRandall Stewart if (SCTP_SIZE32(chk_length) == (length - *offset)) { 2436f8829a4aSRandall Stewart last_chunk = 1; 2437f8829a4aSRandall Stewart } else { 2438f8829a4aSRandall Stewart last_chunk = 0; 2439f8829a4aSRandall Stewart } 2440f8829a4aSRandall Stewart if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch, 2441f8829a4aSRandall Stewart chk_length, net, high_tsn, &abort_flag, &break_flag, 2442f8829a4aSRandall Stewart last_chunk)) { 2443f8829a4aSRandall Stewart num_chunks++; 2444f8829a4aSRandall Stewart } 2445f8829a4aSRandall Stewart if (abort_flag) 2446f8829a4aSRandall Stewart return (2); 2447f8829a4aSRandall Stewart 2448f8829a4aSRandall Stewart if (break_flag) { 2449f8829a4aSRandall Stewart /* 2450f8829a4aSRandall Stewart * Set because of out of rwnd space and no 2451f8829a4aSRandall Stewart * drop rep space left. 2452f8829a4aSRandall Stewart */ 2453f8829a4aSRandall Stewart stop_proc = 1; 245460990c0cSMichael Tuexen continue; 2455f8829a4aSRandall Stewart } 2456f8829a4aSRandall Stewart } else { 2457f8829a4aSRandall Stewart /* not a data chunk in the data region */ 2458f8829a4aSRandall Stewart switch (ch->ch.chunk_type) { 2459f8829a4aSRandall Stewart case SCTP_INITIATION: 2460f8829a4aSRandall Stewart case SCTP_INITIATION_ACK: 2461f8829a4aSRandall Stewart case SCTP_SELECTIVE_ACK: 246260990c0cSMichael Tuexen case SCTP_NR_SELECTIVE_ACK: 2463f8829a4aSRandall Stewart case SCTP_HEARTBEAT_REQUEST: 2464f8829a4aSRandall Stewart case SCTP_HEARTBEAT_ACK: 2465f8829a4aSRandall Stewart case SCTP_ABORT_ASSOCIATION: 2466f8829a4aSRandall Stewart case SCTP_SHUTDOWN: 2467f8829a4aSRandall Stewart case SCTP_SHUTDOWN_ACK: 2468f8829a4aSRandall Stewart case SCTP_OPERATION_ERROR: 2469f8829a4aSRandall Stewart case SCTP_COOKIE_ECHO: 2470f8829a4aSRandall Stewart case SCTP_COOKIE_ACK: 2471f8829a4aSRandall Stewart case SCTP_ECN_ECHO: 2472f8829a4aSRandall Stewart case SCTP_ECN_CWR: 2473f8829a4aSRandall Stewart case SCTP_SHUTDOWN_COMPLETE: 2474f8829a4aSRandall Stewart case SCTP_AUTHENTICATION: 2475f8829a4aSRandall Stewart case SCTP_ASCONF_ACK: 2476f8829a4aSRandall Stewart case SCTP_PACKET_DROPPED: 2477f8829a4aSRandall Stewart case SCTP_STREAM_RESET: 2478f8829a4aSRandall Stewart case SCTP_FORWARD_CUM_TSN: 2479f8829a4aSRandall Stewart case SCTP_ASCONF: 2480f8829a4aSRandall Stewart /* 2481f8829a4aSRandall Stewart * Now, what do we do with KNOWN chunks that 2482f8829a4aSRandall Stewart * are NOT in the right place? 2483f8829a4aSRandall Stewart * 2484f8829a4aSRandall Stewart * For now, I do nothing but ignore them. We 2485f8829a4aSRandall Stewart * may later want to add sysctl stuff to 2486f8829a4aSRandall Stewart * switch out and do either an ABORT() or 2487f8829a4aSRandall Stewart * possibly process them. 2488f8829a4aSRandall Stewart */ 2489b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) { 2490f8829a4aSRandall Stewart struct mbuf *op_err; 2491*267dbe63SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 2492f8829a4aSRandall Stewart 2493*267dbe63SMichael Tuexen snprintf(msg, sizeof(msg), "DATA chunk followwd by chunk of type %2.2x", 2494*267dbe63SMichael Tuexen ch->ch.chunk_type); 2495*267dbe63SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 2496f30ac432SMichael Tuexen sctp_abort_association(inp, stcb, 2497f30ac432SMichael Tuexen m, iphlen, 2498b1754ad1SMichael Tuexen src, dst, 2499f30ac432SMichael Tuexen sh, op_err, 2500457b4b88SMichael Tuexen mflowtype, mflowid, 2501f30ac432SMichael Tuexen vrf_id, port); 2502f8829a4aSRandall Stewart return (2); 2503f8829a4aSRandall Stewart } 2504f8829a4aSRandall Stewart break; 2505f8829a4aSRandall Stewart default: 2506f8829a4aSRandall Stewart /* unknown chunk type, use bit rules */ 2507f8829a4aSRandall Stewart if (ch->ch.chunk_type & 0x40) { 2508f8829a4aSRandall Stewart /* Add a error report to the queue */ 2509d61a0ae0SRandall Stewart struct mbuf *merr; 2510f8829a4aSRandall Stewart struct sctp_paramhdr *phd; 2511f8829a4aSRandall Stewart 2512eb1b1807SGleb Smirnoff merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_NOWAIT, 1, MT_DATA); 2513d61a0ae0SRandall Stewart if (merr) { 2514d61a0ae0SRandall Stewart phd = mtod(merr, struct sctp_paramhdr *); 2515f8829a4aSRandall Stewart /* 2516f8829a4aSRandall Stewart * We cheat and use param 2517f8829a4aSRandall Stewart * type since we did not 2518f8829a4aSRandall Stewart * bother to define a error 2519f8829a4aSRandall Stewart * cause struct. They are 2520f8829a4aSRandall Stewart * the same basic format 2521f8829a4aSRandall Stewart * with different names. 2522f8829a4aSRandall Stewart */ 2523f8829a4aSRandall Stewart phd->param_type = 2524f8829a4aSRandall Stewart htons(SCTP_CAUSE_UNRECOG_CHUNK); 2525f8829a4aSRandall Stewart phd->param_length = 2526f8829a4aSRandall Stewart htons(chk_length + sizeof(*phd)); 2527d61a0ae0SRandall Stewart SCTP_BUF_LEN(merr) = sizeof(*phd); 2528eb1b1807SGleb Smirnoff SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT); 2529d61a0ae0SRandall Stewart if (SCTP_BUF_NEXT(merr)) { 2530ce11b842SMichael Tuexen if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL) == NULL) { 2531921569e2SMichael Tuexen sctp_m_freem(merr); 2532921569e2SMichael Tuexen } else { 2533d61a0ae0SRandall Stewart sctp_queue_op_err(stcb, merr); 2534921569e2SMichael Tuexen } 2535f8829a4aSRandall Stewart } else { 2536d61a0ae0SRandall Stewart sctp_m_freem(merr); 2537f8829a4aSRandall Stewart } 2538f8829a4aSRandall Stewart } 2539f8829a4aSRandall Stewart } 2540f8829a4aSRandall Stewart if ((ch->ch.chunk_type & 0x80) == 0) { 2541f8829a4aSRandall Stewart /* discard the rest of this packet */ 2542f8829a4aSRandall Stewart stop_proc = 1; 2543f8829a4aSRandall Stewart } /* else skip this bad chunk and 2544f8829a4aSRandall Stewart * continue... */ 2545f8829a4aSRandall Stewart break; 254660990c0cSMichael Tuexen } /* switch of chunk type */ 2547f8829a4aSRandall Stewart } 2548f8829a4aSRandall Stewart *offset += SCTP_SIZE32(chk_length); 2549f8829a4aSRandall Stewart if ((*offset >= length) || stop_proc) { 2550f8829a4aSRandall Stewart /* no more data left in the mbuf chain */ 2551f8829a4aSRandall Stewart stop_proc = 1; 2552f8829a4aSRandall Stewart continue; 2553f8829a4aSRandall Stewart } 2554f8829a4aSRandall Stewart ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset, 2555f8829a4aSRandall Stewart sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf); 2556f8829a4aSRandall Stewart if (ch == NULL) { 2557f8829a4aSRandall Stewart *offset = length; 2558f8829a4aSRandall Stewart stop_proc = 1; 255960990c0cSMichael Tuexen continue; 2560f8829a4aSRandall Stewart } 256160990c0cSMichael Tuexen } 2562f8829a4aSRandall Stewart if (break_flag) { 2563f8829a4aSRandall Stewart /* 2564f8829a4aSRandall Stewart * we need to report rwnd overrun drops. 2565f8829a4aSRandall Stewart */ 256620cc2188SMichael Tuexen sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0); 2567f8829a4aSRandall Stewart } 2568f8829a4aSRandall Stewart if (num_chunks) { 2569f8829a4aSRandall Stewart /* 2570ceaad40aSRandall Stewart * Did we get data, if so update the time for auto-close and 2571f8829a4aSRandall Stewart * give peer credit for being alive. 2572f8829a4aSRandall Stewart */ 2573f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_recvpktwithdata); 2574b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 2575c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 2576c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 2577c4739e2fSRandall Stewart 0, 2578c4739e2fSRandall Stewart SCTP_FROM_SCTP_INDATA, 2579c4739e2fSRandall Stewart __LINE__); 2580c4739e2fSRandall Stewart } 2581f8829a4aSRandall Stewart stcb->asoc.overall_error_count = 0; 25826e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd); 2583f8829a4aSRandall Stewart } 2584f8829a4aSRandall Stewart /* now service all of the reassm queue if needed */ 2585f8829a4aSRandall Stewart if (!(TAILQ_EMPTY(&asoc->reasmqueue))) 2586f8829a4aSRandall Stewart sctp_service_queues(stcb, asoc); 2587f8829a4aSRandall Stewart 2588f8829a4aSRandall Stewart if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) { 258942551e99SRandall Stewart /* Assure that we ack right away */ 259042551e99SRandall Stewart stcb->asoc.send_sack = 1; 2591f8829a4aSRandall Stewart } 2592f8829a4aSRandall Stewart /* Start a sack timer or QUEUE a SACK for sending */ 25937215cc1bSMichael Tuexen sctp_sack_check(stcb, was_a_gap); 2594f8829a4aSRandall Stewart return (0); 2595f8829a4aSRandall Stewart } 2596f8829a4aSRandall Stewart 25970fa753b3SRandall Stewart static int 25980fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn, 25990fa753b3SRandall Stewart uint16_t frag_strt, uint16_t frag_end, int nr_sacking, 26000fa753b3SRandall Stewart int *num_frs, 26010fa753b3SRandall Stewart uint32_t * biggest_newly_acked_tsn, 26020fa753b3SRandall Stewart uint32_t * this_sack_lowest_newack, 26037215cc1bSMichael Tuexen int *rto_ok) 26040fa753b3SRandall Stewart { 26050fa753b3SRandall Stewart struct sctp_tmit_chunk *tp1; 26060fa753b3SRandall Stewart unsigned int theTSN; 2607b5c16493SMichael Tuexen int j, wake_him = 0, circled = 0; 26080fa753b3SRandall Stewart 26090fa753b3SRandall Stewart /* Recover the tp1 we last saw */ 26100fa753b3SRandall Stewart tp1 = *p_tp1; 26110fa753b3SRandall Stewart if (tp1 == NULL) { 26120fa753b3SRandall Stewart tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 26130fa753b3SRandall Stewart } 26140fa753b3SRandall Stewart for (j = frag_strt; j <= frag_end; j++) { 26150fa753b3SRandall Stewart theTSN = j + last_tsn; 26160fa753b3SRandall Stewart while (tp1) { 26170fa753b3SRandall Stewart if (tp1->rec.data.doing_fast_retransmit) 26180fa753b3SRandall Stewart (*num_frs) += 1; 26190fa753b3SRandall Stewart 26200fa753b3SRandall Stewart /*- 26210fa753b3SRandall Stewart * CMT: CUCv2 algorithm. For each TSN being 26220fa753b3SRandall Stewart * processed from the sent queue, track the 26230fa753b3SRandall Stewart * next expected pseudo-cumack, or 26240fa753b3SRandall Stewart * rtx_pseudo_cumack, if required. Separate 26250fa753b3SRandall Stewart * cumack trackers for first transmissions, 26260fa753b3SRandall Stewart * and retransmissions. 26270fa753b3SRandall Stewart */ 26288427b3fdSMichael Tuexen if ((tp1->sent < SCTP_DATAGRAM_RESEND) && 26298427b3fdSMichael Tuexen (tp1->whoTo->find_pseudo_cumack == 1) && 26300fa753b3SRandall Stewart (tp1->snd_count == 1)) { 26310fa753b3SRandall Stewart tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq; 26320fa753b3SRandall Stewart tp1->whoTo->find_pseudo_cumack = 0; 26330fa753b3SRandall Stewart } 26348427b3fdSMichael Tuexen if ((tp1->sent < SCTP_DATAGRAM_RESEND) && 26358427b3fdSMichael Tuexen (tp1->whoTo->find_rtx_pseudo_cumack == 1) && 26360fa753b3SRandall Stewart (tp1->snd_count > 1)) { 26370fa753b3SRandall Stewart tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq; 26380fa753b3SRandall Stewart tp1->whoTo->find_rtx_pseudo_cumack = 0; 26390fa753b3SRandall Stewart } 26400fa753b3SRandall Stewart if (tp1->rec.data.TSN_seq == theTSN) { 26410fa753b3SRandall Stewart if (tp1->sent != SCTP_DATAGRAM_UNSENT) { 26420fa753b3SRandall Stewart /*- 26430fa753b3SRandall Stewart * must be held until 26440fa753b3SRandall Stewart * cum-ack passes 26450fa753b3SRandall Stewart */ 26460fa753b3SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 26470fa753b3SRandall Stewart /*- 26480fa753b3SRandall Stewart * If it is less than RESEND, it is 26490fa753b3SRandall Stewart * now no-longer in flight. 26500fa753b3SRandall Stewart * Higher values may already be set 26510fa753b3SRandall Stewart * via previous Gap Ack Blocks... 26520fa753b3SRandall Stewart * i.e. ACKED or RESEND. 26530fa753b3SRandall Stewart */ 265420b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, 265520b07a4dSMichael Tuexen *biggest_newly_acked_tsn)) { 26560fa753b3SRandall Stewart *biggest_newly_acked_tsn = tp1->rec.data.TSN_seq; 26570fa753b3SRandall Stewart } 26580fa753b3SRandall Stewart /*- 26590fa753b3SRandall Stewart * CMT: SFR algo (and HTNA) - set 26600fa753b3SRandall Stewart * saw_newack to 1 for dest being 26610fa753b3SRandall Stewart * newly acked. update 26620fa753b3SRandall Stewart * this_sack_highest_newack if 26630fa753b3SRandall Stewart * appropriate. 26640fa753b3SRandall Stewart */ 26650fa753b3SRandall Stewart if (tp1->rec.data.chunk_was_revoked == 0) 26660fa753b3SRandall Stewart tp1->whoTo->saw_newack = 1; 26670fa753b3SRandall Stewart 266820b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, 266920b07a4dSMichael Tuexen tp1->whoTo->this_sack_highest_newack)) { 26700fa753b3SRandall Stewart tp1->whoTo->this_sack_highest_newack = 26710fa753b3SRandall Stewart tp1->rec.data.TSN_seq; 26720fa753b3SRandall Stewart } 26730fa753b3SRandall Stewart /*- 26740fa753b3SRandall Stewart * CMT DAC algo: also update 26750fa753b3SRandall Stewart * this_sack_lowest_newack 26760fa753b3SRandall Stewart */ 26770fa753b3SRandall Stewart if (*this_sack_lowest_newack == 0) { 26780fa753b3SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 26790fa753b3SRandall Stewart sctp_log_sack(*this_sack_lowest_newack, 26800fa753b3SRandall Stewart last_tsn, 26810fa753b3SRandall Stewart tp1->rec.data.TSN_seq, 26820fa753b3SRandall Stewart 0, 26830fa753b3SRandall Stewart 0, 26840fa753b3SRandall Stewart SCTP_LOG_TSN_ACKED); 26850fa753b3SRandall Stewart } 26860fa753b3SRandall Stewart *this_sack_lowest_newack = tp1->rec.data.TSN_seq; 26870fa753b3SRandall Stewart } 26880fa753b3SRandall Stewart /*- 26890fa753b3SRandall Stewart * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp 26900fa753b3SRandall Stewart * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set 26910fa753b3SRandall Stewart * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be 26920fa753b3SRandall Stewart * updated. Also trigger search for the next expected (rtx-)pseudo-cumack. 26930fa753b3SRandall Stewart * Separate pseudo_cumack trackers for first transmissions and 26940fa753b3SRandall Stewart * retransmissions. 26950fa753b3SRandall Stewart */ 26960fa753b3SRandall Stewart if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) { 26970fa753b3SRandall Stewart if (tp1->rec.data.chunk_was_revoked == 0) { 26980fa753b3SRandall Stewart tp1->whoTo->new_pseudo_cumack = 1; 26990fa753b3SRandall Stewart } 27000fa753b3SRandall Stewart tp1->whoTo->find_pseudo_cumack = 1; 27010fa753b3SRandall Stewart } 27020fa753b3SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 27030fa753b3SRandall Stewart sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK); 27040fa753b3SRandall Stewart } 27050fa753b3SRandall Stewart if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) { 27060fa753b3SRandall Stewart if (tp1->rec.data.chunk_was_revoked == 0) { 27070fa753b3SRandall Stewart tp1->whoTo->new_pseudo_cumack = 1; 27080fa753b3SRandall Stewart } 27090fa753b3SRandall Stewart tp1->whoTo->find_rtx_pseudo_cumack = 1; 27100fa753b3SRandall Stewart } 27110fa753b3SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 27120fa753b3SRandall Stewart sctp_log_sack(*biggest_newly_acked_tsn, 27130fa753b3SRandall Stewart last_tsn, 27140fa753b3SRandall Stewart tp1->rec.data.TSN_seq, 27150fa753b3SRandall Stewart frag_strt, 27160fa753b3SRandall Stewart frag_end, 27170fa753b3SRandall Stewart SCTP_LOG_TSN_ACKED); 27180fa753b3SRandall Stewart } 27190fa753b3SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 27200fa753b3SRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP, 27210fa753b3SRandall Stewart tp1->whoTo->flight_size, 27220fa753b3SRandall Stewart tp1->book_size, 27230fa753b3SRandall Stewart (uintptr_t) tp1->whoTo, 27240fa753b3SRandall Stewart tp1->rec.data.TSN_seq); 27250fa753b3SRandall Stewart } 27260fa753b3SRandall Stewart sctp_flight_size_decrease(tp1); 2727299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) { 2728299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo, 2729299108c5SRandall Stewart tp1); 2730299108c5SRandall Stewart } 27310fa753b3SRandall Stewart sctp_total_flight_decrease(stcb, tp1); 27320fa753b3SRandall Stewart 27330fa753b3SRandall Stewart tp1->whoTo->net_ack += tp1->send_size; 27340fa753b3SRandall Stewart if (tp1->snd_count < 2) { 27350fa753b3SRandall Stewart /*- 27360fa753b3SRandall Stewart * True non-retransmited chunk 27370fa753b3SRandall Stewart */ 27380fa753b3SRandall Stewart tp1->whoTo->net_ack2 += tp1->send_size; 27390fa753b3SRandall Stewart 27400fa753b3SRandall Stewart /*- 27410fa753b3SRandall Stewart * update RTO too ? 27420fa753b3SRandall Stewart */ 27430fa753b3SRandall Stewart if (tp1->do_rtt) { 2744f79aab18SRandall Stewart if (*rto_ok) { 27450fa753b3SRandall Stewart tp1->whoTo->RTO = 27460fa753b3SRandall Stewart sctp_calculate_rto(stcb, 27470fa753b3SRandall Stewart &stcb->asoc, 27480fa753b3SRandall Stewart tp1->whoTo, 27490fa753b3SRandall Stewart &tp1->sent_rcv_time, 2750899288aeSRandall Stewart sctp_align_safe_nocopy, 2751f79aab18SRandall Stewart SCTP_RTT_FROM_DATA); 2752f79aab18SRandall Stewart *rto_ok = 0; 2753f79aab18SRandall Stewart } 2754f79aab18SRandall Stewart if (tp1->whoTo->rto_needed == 0) { 2755f79aab18SRandall Stewart tp1->whoTo->rto_needed = 1; 2756f79aab18SRandall Stewart } 27570fa753b3SRandall Stewart tp1->do_rtt = 0; 27580fa753b3SRandall Stewart } 27590fa753b3SRandall Stewart } 27600fa753b3SRandall Stewart } 27610fa753b3SRandall Stewart if (tp1->sent <= SCTP_DATAGRAM_RESEND) { 276220b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, 276320b07a4dSMichael Tuexen stcb->asoc.this_sack_highest_gap)) { 27640fa753b3SRandall Stewart stcb->asoc.this_sack_highest_gap = 27650fa753b3SRandall Stewart tp1->rec.data.TSN_seq; 27660fa753b3SRandall Stewart } 27670fa753b3SRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND) { 27680fa753b3SRandall Stewart sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt); 27690fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED 27700fa753b3SRandall Stewart sctp_audit_log(0xB2, 27710fa753b3SRandall Stewart (stcb->asoc.sent_queue_retran_cnt & 0x000000ff)); 27720fa753b3SRandall Stewart #endif 27730fa753b3SRandall Stewart } 27740fa753b3SRandall Stewart } 27750fa753b3SRandall Stewart /*- 27760fa753b3SRandall Stewart * All chunks NOT UNSENT fall through here and are marked 27770fa753b3SRandall Stewart * (leave PR-SCTP ones that are to skip alone though) 27780fa753b3SRandall Stewart */ 27792a498584SMichael Tuexen if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) && 2780325c8c46SMichael Tuexen (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) { 27810fa753b3SRandall Stewart tp1->sent = SCTP_DATAGRAM_MARKED; 27822a498584SMichael Tuexen } 27830fa753b3SRandall Stewart if (tp1->rec.data.chunk_was_revoked) { 27840fa753b3SRandall Stewart /* deflate the cwnd */ 27850fa753b3SRandall Stewart tp1->whoTo->cwnd -= tp1->book_size; 27860fa753b3SRandall Stewart tp1->rec.data.chunk_was_revoked = 0; 27870fa753b3SRandall Stewart } 27880fa753b3SRandall Stewart /* NR Sack code here */ 2789325c8c46SMichael Tuexen if (nr_sacking && 2790325c8c46SMichael Tuexen (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) { 2791325c8c46SMichael Tuexen if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) { 2792325c8c46SMichael Tuexen stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--; 2793325c8c46SMichael Tuexen #ifdef INVARIANTS 2794325c8c46SMichael Tuexen } else { 2795325c8c46SMichael Tuexen panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number); 2796325c8c46SMichael Tuexen #endif 2797325c8c46SMichael Tuexen } 2798325c8c46SMichael Tuexen tp1->sent = SCTP_DATAGRAM_NR_ACKED; 27990fa753b3SRandall Stewart if (tp1->data) { 28000fa753b3SRandall Stewart /* 28010fa753b3SRandall Stewart * sa_ignore 28020fa753b3SRandall Stewart * NO_NULL_CHK 28030fa753b3SRandall Stewart */ 28040fa753b3SRandall Stewart sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1); 28050fa753b3SRandall Stewart sctp_m_freem(tp1->data); 28060fa753b3SRandall Stewart tp1->data = NULL; 2807b5c16493SMichael Tuexen } 28080fa753b3SRandall Stewart wake_him++; 28090fa753b3SRandall Stewart } 28100fa753b3SRandall Stewart } 28110fa753b3SRandall Stewart break; 28120fa753b3SRandall Stewart } /* if (tp1->TSN_seq == theTSN) */ 281320b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) { 28140fa753b3SRandall Stewart break; 281520b07a4dSMichael Tuexen } 28160fa753b3SRandall Stewart tp1 = TAILQ_NEXT(tp1, sctp_next); 2817b5c16493SMichael Tuexen if ((tp1 == NULL) && (circled == 0)) { 2818b5c16493SMichael Tuexen circled++; 28190fa753b3SRandall Stewart tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 28200fa753b3SRandall Stewart } 2821b5c16493SMichael Tuexen } /* end while (tp1) */ 2822b5c16493SMichael Tuexen if (tp1 == NULL) { 2823b5c16493SMichael Tuexen circled = 0; 2824b5c16493SMichael Tuexen tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue); 2825b5c16493SMichael Tuexen } 2826b5c16493SMichael Tuexen /* In case the fragments were not in order we must reset */ 28270fa753b3SRandall Stewart } /* end for (j = fragStart */ 28280fa753b3SRandall Stewart *p_tp1 = tp1; 28290fa753b3SRandall Stewart return (wake_him); /* Return value only used for nr-sack */ 28300fa753b3SRandall Stewart } 28310fa753b3SRandall Stewart 28320fa753b3SRandall Stewart 2833cd554309SMichael Tuexen static int 2834458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc, 2835cd554309SMichael Tuexen uint32_t last_tsn, uint32_t * biggest_tsn_acked, 2836139bc87fSRandall Stewart uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack, 28377215cc1bSMichael Tuexen int num_seg, int num_nr_seg, int *rto_ok) 2838f8829a4aSRandall Stewart { 2839458303daSRandall Stewart struct sctp_gap_ack_block *frag, block; 2840f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1; 28410fa753b3SRandall Stewart int i; 2842f8829a4aSRandall Stewart int num_frs = 0; 2843cd554309SMichael Tuexen int chunk_freed; 2844cd554309SMichael Tuexen int non_revocable; 2845d9c5cfeaSMichael Tuexen uint16_t frag_strt, frag_end, prev_frag_end; 2846f8829a4aSRandall Stewart 2847d9c5cfeaSMichael Tuexen tp1 = TAILQ_FIRST(&asoc->sent_queue); 2848d9c5cfeaSMichael Tuexen prev_frag_end = 0; 2849cd554309SMichael Tuexen chunk_freed = 0; 2850f8829a4aSRandall Stewart 2851cd554309SMichael Tuexen for (i = 0; i < (num_seg + num_nr_seg); i++) { 2852d9c5cfeaSMichael Tuexen if (i == num_seg) { 2853d9c5cfeaSMichael Tuexen prev_frag_end = 0; 2854d9c5cfeaSMichael Tuexen tp1 = TAILQ_FIRST(&asoc->sent_queue); 2855d9c5cfeaSMichael Tuexen } 2856458303daSRandall Stewart frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset, 2857458303daSRandall Stewart sizeof(struct sctp_gap_ack_block), (uint8_t *) & block); 2858458303daSRandall Stewart *offset += sizeof(block); 2859458303daSRandall Stewart if (frag == NULL) { 2860cd554309SMichael Tuexen return (chunk_freed); 2861458303daSRandall Stewart } 2862f8829a4aSRandall Stewart frag_strt = ntohs(frag->start); 2863f8829a4aSRandall Stewart frag_end = ntohs(frag->end); 2864d9c5cfeaSMichael Tuexen 2865f8829a4aSRandall Stewart if (frag_strt > frag_end) { 2866d9c5cfeaSMichael Tuexen /* This gap report is malformed, skip it. */ 2867f8829a4aSRandall Stewart continue; 2868f8829a4aSRandall Stewart } 2869d9c5cfeaSMichael Tuexen if (frag_strt <= prev_frag_end) { 2870d9c5cfeaSMichael Tuexen /* This gap report is not in order, so restart. */ 2871f8829a4aSRandall Stewart tp1 = TAILQ_FIRST(&asoc->sent_queue); 2872f8829a4aSRandall Stewart } 287320b07a4dSMichael Tuexen if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) { 2874d9c5cfeaSMichael Tuexen *biggest_tsn_acked = last_tsn + frag_end; 2875f8829a4aSRandall Stewart } 2876cd554309SMichael Tuexen if (i < num_seg) { 2877cd554309SMichael Tuexen non_revocable = 0; 2878cd554309SMichael Tuexen } else { 2879cd554309SMichael Tuexen non_revocable = 1; 2880cd554309SMichael Tuexen } 2881cd554309SMichael Tuexen if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end, 2882cd554309SMichael Tuexen non_revocable, &num_frs, biggest_newly_acked_tsn, 28837215cc1bSMichael Tuexen this_sack_lowest_newack, rto_ok)) { 2884cd554309SMichael Tuexen chunk_freed = 1; 2885458303daSRandall Stewart } 2886d9c5cfeaSMichael Tuexen prev_frag_end = frag_end; 2887f8829a4aSRandall Stewart } 2888b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 288980fefe0aSRandall Stewart if (num_frs) 289080fefe0aSRandall Stewart sctp_log_fr(*biggest_tsn_acked, 289180fefe0aSRandall Stewart *biggest_newly_acked_tsn, 289280fefe0aSRandall Stewart last_tsn, SCTP_FR_LOG_BIGGEST_TSNS); 289380fefe0aSRandall Stewart } 2894cd554309SMichael Tuexen return (chunk_freed); 2895f8829a4aSRandall Stewart } 2896f8829a4aSRandall Stewart 2897f8829a4aSRandall Stewart static void 2898c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb, 2899c105859eSRandall Stewart struct sctp_association *asoc, uint32_t cumack, 290063eda93dSMichael Tuexen uint32_t biggest_tsn_acked) 2901f8829a4aSRandall Stewart { 2902f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1; 2903f8829a4aSRandall Stewart 29044a9ef3f8SMichael Tuexen TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 290520b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) { 2906f8829a4aSRandall Stewart /* 2907f8829a4aSRandall Stewart * ok this guy is either ACK or MARKED. If it is 2908f8829a4aSRandall Stewart * ACKED it has been previously acked but not this 2909f8829a4aSRandall Stewart * time i.e. revoked. If it is MARKED it was ACK'ed 2910f8829a4aSRandall Stewart * again. 2911f8829a4aSRandall Stewart */ 291220b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) { 2913d06c82f1SRandall Stewart break; 291420b07a4dSMichael Tuexen } 2915f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_ACKED) { 2916f8829a4aSRandall Stewart /* it has been revoked */ 2917f8829a4aSRandall Stewart tp1->sent = SCTP_DATAGRAM_SENT; 2918f8829a4aSRandall Stewart tp1->rec.data.chunk_was_revoked = 1; 2919a5d547adSRandall Stewart /* 292042551e99SRandall Stewart * We must add this stuff back in to assure 292142551e99SRandall Stewart * timers and such get started. 2922a5d547adSRandall Stewart */ 2923b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 2924c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE, 2925c105859eSRandall Stewart tp1->whoTo->flight_size, 2926c105859eSRandall Stewart tp1->book_size, 2927c105859eSRandall Stewart (uintptr_t) tp1->whoTo, 2928c105859eSRandall Stewart tp1->rec.data.TSN_seq); 292980fefe0aSRandall Stewart } 2930c105859eSRandall Stewart sctp_flight_size_increase(tp1); 2931c105859eSRandall Stewart sctp_total_flight_increase(stcb, tp1); 293242551e99SRandall Stewart /* 293342551e99SRandall Stewart * We inflate the cwnd to compensate for our 293442551e99SRandall Stewart * artificial inflation of the flight_size. 293542551e99SRandall Stewart */ 293642551e99SRandall Stewart tp1->whoTo->cwnd += tp1->book_size; 2937b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 2938f8829a4aSRandall Stewart sctp_log_sack(asoc->last_acked_seq, 2939f8829a4aSRandall Stewart cumack, 2940f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 2941f8829a4aSRandall Stewart 0, 2942f8829a4aSRandall Stewart 0, 2943f8829a4aSRandall Stewart SCTP_LOG_TSN_REVOKED); 294480fefe0aSRandall Stewart } 2945f8829a4aSRandall Stewart } else if (tp1->sent == SCTP_DATAGRAM_MARKED) { 2946f8829a4aSRandall Stewart /* it has been re-acked in this SACK */ 2947f8829a4aSRandall Stewart tp1->sent = SCTP_DATAGRAM_ACKED; 2948f8829a4aSRandall Stewart } 2949f8829a4aSRandall Stewart } 2950f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_UNSENT) 2951f8829a4aSRandall Stewart break; 2952f8829a4aSRandall Stewart } 2953f8829a4aSRandall Stewart } 2954f8829a4aSRandall Stewart 2955830d754dSRandall Stewart 2956f8829a4aSRandall Stewart static void 2957f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc, 295863eda93dSMichael Tuexen uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved) 2959f8829a4aSRandall Stewart { 2960f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1; 2961f8829a4aSRandall Stewart int strike_flag = 0; 2962f8829a4aSRandall Stewart struct timeval now; 2963f8829a4aSRandall Stewart int tot_retrans = 0; 2964f8829a4aSRandall Stewart uint32_t sending_seq; 2965f8829a4aSRandall Stewart struct sctp_nets *net; 2966f8829a4aSRandall Stewart int num_dests_sacked = 0; 2967f8829a4aSRandall Stewart 2968f8829a4aSRandall Stewart /* 2969f8829a4aSRandall Stewart * select the sending_seq, this is either the next thing ready to be 2970f8829a4aSRandall Stewart * sent but not transmitted, OR, the next seq we assign. 2971f8829a4aSRandall Stewart */ 2972f8829a4aSRandall Stewart tp1 = TAILQ_FIRST(&stcb->asoc.send_queue); 2973f8829a4aSRandall Stewart if (tp1 == NULL) { 2974f8829a4aSRandall Stewart sending_seq = asoc->sending_seq; 2975f8829a4aSRandall Stewart } else { 2976f8829a4aSRandall Stewart sending_seq = tp1->rec.data.TSN_seq; 2977f8829a4aSRandall Stewart } 2978f8829a4aSRandall Stewart 2979f8829a4aSRandall Stewart /* CMT DAC algo: finding out if SACK is a mixed SACK */ 29807c99d56fSMichael Tuexen if ((asoc->sctp_cmt_on_off > 0) && 298120083c2eSMichael Tuexen SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { 2982f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 2983f8829a4aSRandall Stewart if (net->saw_newack) 2984f8829a4aSRandall Stewart num_dests_sacked++; 2985f8829a4aSRandall Stewart } 2986f8829a4aSRandall Stewart } 2987dd973b0eSMichael Tuexen if (stcb->asoc.prsctp_supported) { 29886e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 2989f8829a4aSRandall Stewart } 29904a9ef3f8SMichael Tuexen TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 2991f8829a4aSRandall Stewart strike_flag = 0; 2992f8829a4aSRandall Stewart if (tp1->no_fr_allowed) { 2993f8829a4aSRandall Stewart /* this one had a timeout or something */ 2994f8829a4aSRandall Stewart continue; 2995f8829a4aSRandall Stewart } 2996b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 2997f8829a4aSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) 2998f8829a4aSRandall Stewart sctp_log_fr(biggest_tsn_newly_acked, 2999f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3000f8829a4aSRandall Stewart tp1->sent, 3001f8829a4aSRandall Stewart SCTP_FR_LOG_CHECK_STRIKE); 300280fefe0aSRandall Stewart } 300320b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) || 3004f8829a4aSRandall Stewart tp1->sent == SCTP_DATAGRAM_UNSENT) { 3005f8829a4aSRandall Stewart /* done */ 3006f8829a4aSRandall Stewart break; 3007f8829a4aSRandall Stewart } 3008dd973b0eSMichael Tuexen if (stcb->asoc.prsctp_supported) { 3009f8829a4aSRandall Stewart if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) { 3010f8829a4aSRandall Stewart /* Is it expired? */ 301199ddc825SMichael Tuexen if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) { 3012f8829a4aSRandall Stewart /* Yes so drop it */ 3013f8829a4aSRandall Stewart if (tp1->data != NULL) { 30141edc9dbaSMichael Tuexen (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1, 30150c0982b8SRandall Stewart SCTP_SO_NOT_LOCKED); 3016f8829a4aSRandall Stewart } 3017f8829a4aSRandall Stewart continue; 3018f8829a4aSRandall Stewart } 3019f8829a4aSRandall Stewart } 3020f8829a4aSRandall Stewart } 302120b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) { 3022f8829a4aSRandall Stewart /* we are beyond the tsn in the sack */ 3023f8829a4aSRandall Stewart break; 3024f8829a4aSRandall Stewart } 3025f8829a4aSRandall Stewart if (tp1->sent >= SCTP_DATAGRAM_RESEND) { 3026f8829a4aSRandall Stewart /* either a RESEND, ACKED, or MARKED */ 3027f8829a4aSRandall Stewart /* skip */ 302844fbe462SRandall Stewart if (tp1->sent == SCTP_FORWARD_TSN_SKIP) { 302944fbe462SRandall Stewart /* Continue strikin FWD-TSN chunks */ 303044fbe462SRandall Stewart tp1->rec.data.fwd_tsn_cnt++; 303144fbe462SRandall Stewart } 3032f8829a4aSRandall Stewart continue; 3033f8829a4aSRandall Stewart } 3034f8829a4aSRandall Stewart /* 3035f8829a4aSRandall Stewart * CMT : SFR algo (covers part of DAC and HTNA as well) 3036f8829a4aSRandall Stewart */ 3037ad81507eSRandall Stewart if (tp1->whoTo && tp1->whoTo->saw_newack == 0) { 3038f8829a4aSRandall Stewart /* 3039f8829a4aSRandall Stewart * No new acks were receieved for data sent to this 3040f8829a4aSRandall Stewart * dest. Therefore, according to the SFR algo for 3041f8829a4aSRandall Stewart * CMT, no data sent to this dest can be marked for 3042c105859eSRandall Stewart * FR using this SACK. 3043f8829a4aSRandall Stewart */ 3044f8829a4aSRandall Stewart continue; 304520b07a4dSMichael Tuexen } else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq, 304620b07a4dSMichael Tuexen tp1->whoTo->this_sack_highest_newack)) { 3047f8829a4aSRandall Stewart /* 3048f8829a4aSRandall Stewart * CMT: New acks were receieved for data sent to 3049f8829a4aSRandall Stewart * this dest. But no new acks were seen for data 3050f8829a4aSRandall Stewart * sent after tp1. Therefore, according to the SFR 3051f8829a4aSRandall Stewart * algo for CMT, tp1 cannot be marked for FR using 3052f8829a4aSRandall Stewart * this SACK. This step covers part of the DAC algo 3053f8829a4aSRandall Stewart * and the HTNA algo as well. 3054f8829a4aSRandall Stewart */ 3055f8829a4aSRandall Stewart continue; 3056f8829a4aSRandall Stewart } 3057f8829a4aSRandall Stewart /* 3058f8829a4aSRandall Stewart * Here we check to see if we were have already done a FR 3059f8829a4aSRandall Stewart * and if so we see if the biggest TSN we saw in the sack is 3060f8829a4aSRandall Stewart * smaller than the recovery point. If so we don't strike 3061f8829a4aSRandall Stewart * the tsn... otherwise we CAN strike the TSN. 3062f8829a4aSRandall Stewart */ 3063f8829a4aSRandall Stewart /* 306442551e99SRandall Stewart * @@@ JRI: Check for CMT if (accum_moved && 306542551e99SRandall Stewart * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off == 306642551e99SRandall Stewart * 0)) { 3067f8829a4aSRandall Stewart */ 306842551e99SRandall Stewart if (accum_moved && asoc->fast_retran_loss_recovery) { 3069f8829a4aSRandall Stewart /* 3070f8829a4aSRandall Stewart * Strike the TSN if in fast-recovery and cum-ack 3071f8829a4aSRandall Stewart * moved. 3072f8829a4aSRandall Stewart */ 3073b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3074f8829a4aSRandall Stewart sctp_log_fr(biggest_tsn_newly_acked, 3075f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3076f8829a4aSRandall Stewart tp1->sent, 3077f8829a4aSRandall Stewart SCTP_FR_LOG_STRIKE_CHUNK); 307880fefe0aSRandall Stewart } 30795e54f665SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3080f8829a4aSRandall Stewart tp1->sent++; 30815e54f665SRandall Stewart } 30827c99d56fSMichael Tuexen if ((asoc->sctp_cmt_on_off > 0) && 308320083c2eSMichael Tuexen SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { 3084f8829a4aSRandall Stewart /* 3085f8829a4aSRandall Stewart * CMT DAC algorithm: If SACK flag is set to 3086f8829a4aSRandall Stewart * 0, then lowest_newack test will not pass 3087f8829a4aSRandall Stewart * because it would have been set to the 3088f8829a4aSRandall Stewart * cumack earlier. If not already to be 3089f8829a4aSRandall Stewart * rtx'd, If not a mixed sack and if tp1 is 3090f8829a4aSRandall Stewart * not between two sacked TSNs, then mark by 3091c105859eSRandall Stewart * one more. NOTE that we are marking by one 3092c105859eSRandall Stewart * additional time since the SACK DAC flag 3093c105859eSRandall Stewart * indicates that two packets have been 3094c105859eSRandall Stewart * received after this missing TSN. 30955e54f665SRandall Stewart */ 30965e54f665SRandall Stewart if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) && 309720b07a4dSMichael Tuexen SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) { 3098b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3099f8829a4aSRandall Stewart sctp_log_fr(16 + num_dests_sacked, 3100f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3101f8829a4aSRandall Stewart tp1->sent, 3102f8829a4aSRandall Stewart SCTP_FR_LOG_STRIKE_CHUNK); 310380fefe0aSRandall Stewart } 3104f8829a4aSRandall Stewart tp1->sent++; 3105f8829a4aSRandall Stewart } 3106f8829a4aSRandall Stewart } 310720083c2eSMichael Tuexen } else if ((tp1->rec.data.doing_fast_retransmit) && 310820083c2eSMichael Tuexen (asoc->sctp_cmt_on_off == 0)) { 3109f8829a4aSRandall Stewart /* 3110f8829a4aSRandall Stewart * For those that have done a FR we must take 3111f8829a4aSRandall Stewart * special consideration if we strike. I.e the 3112f8829a4aSRandall Stewart * biggest_newly_acked must be higher than the 3113f8829a4aSRandall Stewart * sending_seq at the time we did the FR. 3114f8829a4aSRandall Stewart */ 31155e54f665SRandall Stewart if ( 3116f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE 3117f8829a4aSRandall Stewart /* 3118f8829a4aSRandall Stewart * If FR's go to new networks, then we must only do 3119f8829a4aSRandall Stewart * this for singly homed asoc's. However if the FR's 3120f8829a4aSRandall Stewart * go to the same network (Armando's work) then its 3121f8829a4aSRandall Stewart * ok to FR multiple times. 3122f8829a4aSRandall Stewart */ 31235e54f665SRandall Stewart (asoc->numnets < 2) 3124f8829a4aSRandall Stewart #else 31255e54f665SRandall Stewart (1) 3126f8829a4aSRandall Stewart #endif 31275e54f665SRandall Stewart ) { 31285e54f665SRandall Stewart 312920b07a4dSMichael Tuexen if (SCTP_TSN_GE(biggest_tsn_newly_acked, 3130f8829a4aSRandall Stewart tp1->rec.data.fast_retran_tsn)) { 3131f8829a4aSRandall Stewart /* 3132f8829a4aSRandall Stewart * Strike the TSN, since this ack is 3133f8829a4aSRandall Stewart * beyond where things were when we 3134f8829a4aSRandall Stewart * did a FR. 3135f8829a4aSRandall Stewart */ 3136b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3137f8829a4aSRandall Stewart sctp_log_fr(biggest_tsn_newly_acked, 3138f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3139f8829a4aSRandall Stewart tp1->sent, 3140f8829a4aSRandall Stewart SCTP_FR_LOG_STRIKE_CHUNK); 314180fefe0aSRandall Stewart } 31425e54f665SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3143f8829a4aSRandall Stewart tp1->sent++; 31445e54f665SRandall Stewart } 3145f8829a4aSRandall Stewart strike_flag = 1; 31467c99d56fSMichael Tuexen if ((asoc->sctp_cmt_on_off > 0) && 314720083c2eSMichael Tuexen SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { 3148f8829a4aSRandall Stewart /* 3149f8829a4aSRandall Stewart * CMT DAC algorithm: If 3150f8829a4aSRandall Stewart * SACK flag is set to 0, 3151f8829a4aSRandall Stewart * then lowest_newack test 3152f8829a4aSRandall Stewart * will not pass because it 3153f8829a4aSRandall Stewart * would have been set to 3154f8829a4aSRandall Stewart * the cumack earlier. If 3155f8829a4aSRandall Stewart * not already to be rtx'd, 3156f8829a4aSRandall Stewart * If not a mixed sack and 3157f8829a4aSRandall Stewart * if tp1 is not between two 3158f8829a4aSRandall Stewart * sacked TSNs, then mark by 3159c105859eSRandall Stewart * one more. NOTE that we 3160c105859eSRandall Stewart * are marking by one 3161c105859eSRandall Stewart * additional time since the 3162c105859eSRandall Stewart * SACK DAC flag indicates 3163c105859eSRandall Stewart * that two packets have 3164c105859eSRandall Stewart * been received after this 3165c105859eSRandall Stewart * missing TSN. 3166f8829a4aSRandall Stewart */ 31675e54f665SRandall Stewart if ((tp1->sent < SCTP_DATAGRAM_RESEND) && 31685e54f665SRandall Stewart (num_dests_sacked == 1) && 316920b07a4dSMichael Tuexen SCTP_TSN_GT(this_sack_lowest_newack, 317020b07a4dSMichael Tuexen tp1->rec.data.TSN_seq)) { 3171b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3172f8829a4aSRandall Stewart sctp_log_fr(32 + num_dests_sacked, 3173f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3174f8829a4aSRandall Stewart tp1->sent, 3175f8829a4aSRandall Stewart SCTP_FR_LOG_STRIKE_CHUNK); 317680fefe0aSRandall Stewart } 31775e54f665SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3178f8829a4aSRandall Stewart tp1->sent++; 31795e54f665SRandall Stewart } 3180f8829a4aSRandall Stewart } 3181f8829a4aSRandall Stewart } 3182f8829a4aSRandall Stewart } 3183f8829a4aSRandall Stewart } 3184f8829a4aSRandall Stewart /* 318542551e99SRandall Stewart * JRI: TODO: remove code for HTNA algo. CMT's SFR 318642551e99SRandall Stewart * algo covers HTNA. 3187f8829a4aSRandall Stewart */ 318820b07a4dSMichael Tuexen } else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, 318920b07a4dSMichael Tuexen biggest_tsn_newly_acked)) { 3190f8829a4aSRandall Stewart /* 3191f8829a4aSRandall Stewart * We don't strike these: This is the HTNA 3192f8829a4aSRandall Stewart * algorithm i.e. we don't strike If our TSN is 3193f8829a4aSRandall Stewart * larger than the Highest TSN Newly Acked. 3194f8829a4aSRandall Stewart */ 3195f8829a4aSRandall Stewart ; 3196f8829a4aSRandall Stewart } else { 3197f8829a4aSRandall Stewart /* Strike the TSN */ 3198b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3199f8829a4aSRandall Stewart sctp_log_fr(biggest_tsn_newly_acked, 3200f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3201f8829a4aSRandall Stewart tp1->sent, 3202f8829a4aSRandall Stewart SCTP_FR_LOG_STRIKE_CHUNK); 320380fefe0aSRandall Stewart } 32045e54f665SRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3205f8829a4aSRandall Stewart tp1->sent++; 32065e54f665SRandall Stewart } 32077c99d56fSMichael Tuexen if ((asoc->sctp_cmt_on_off > 0) && 320820083c2eSMichael Tuexen SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) { 3209f8829a4aSRandall Stewart /* 3210f8829a4aSRandall Stewart * CMT DAC algorithm: If SACK flag is set to 3211f8829a4aSRandall Stewart * 0, then lowest_newack test will not pass 3212f8829a4aSRandall Stewart * because it would have been set to the 3213f8829a4aSRandall Stewart * cumack earlier. If not already to be 3214f8829a4aSRandall Stewart * rtx'd, If not a mixed sack and if tp1 is 3215f8829a4aSRandall Stewart * not between two sacked TSNs, then mark by 3216c105859eSRandall Stewart * one more. NOTE that we are marking by one 3217c105859eSRandall Stewart * additional time since the SACK DAC flag 3218c105859eSRandall Stewart * indicates that two packets have been 3219c105859eSRandall Stewart * received after this missing TSN. 3220f8829a4aSRandall Stewart */ 32215e54f665SRandall Stewart if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) && 322220b07a4dSMichael Tuexen SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) { 3223b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3224f8829a4aSRandall Stewart sctp_log_fr(48 + num_dests_sacked, 3225f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3226f8829a4aSRandall Stewart tp1->sent, 3227f8829a4aSRandall Stewart SCTP_FR_LOG_STRIKE_CHUNK); 322880fefe0aSRandall Stewart } 3229f8829a4aSRandall Stewart tp1->sent++; 3230f8829a4aSRandall Stewart } 3231f8829a4aSRandall Stewart } 3232f8829a4aSRandall Stewart } 3233f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND) { 3234f8829a4aSRandall Stewart struct sctp_nets *alt; 3235f8829a4aSRandall Stewart 3236544e35bdSRandall Stewart /* fix counts and things */ 3237544e35bdSRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3238544e35bdSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND, 3239544e35bdSRandall Stewart (tp1->whoTo ? (tp1->whoTo->flight_size) : 0), 3240544e35bdSRandall Stewart tp1->book_size, 3241544e35bdSRandall Stewart (uintptr_t) tp1->whoTo, 3242544e35bdSRandall Stewart tp1->rec.data.TSN_seq); 3243544e35bdSRandall Stewart } 3244544e35bdSRandall Stewart if (tp1->whoTo) { 3245544e35bdSRandall Stewart tp1->whoTo->net_ack++; 3246544e35bdSRandall Stewart sctp_flight_size_decrease(tp1); 3247299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) { 3248299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo, 3249299108c5SRandall Stewart tp1); 3250299108c5SRandall Stewart } 3251544e35bdSRandall Stewart } 3252544e35bdSRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { 3253544e35bdSRandall Stewart sctp_log_rwnd(SCTP_INCREASE_PEER_RWND, 3254544e35bdSRandall Stewart asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); 3255544e35bdSRandall Stewart } 3256544e35bdSRandall Stewart /* add back to the rwnd */ 3257544e35bdSRandall Stewart asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)); 3258544e35bdSRandall Stewart 3259544e35bdSRandall Stewart /* remove from the total flight */ 3260544e35bdSRandall Stewart sctp_total_flight_decrease(stcb, tp1); 3261544e35bdSRandall Stewart 3262dd973b0eSMichael Tuexen if ((stcb->asoc.prsctp_supported) && 3263475d0674SMichael Tuexen (PR_SCTP_RTX_ENABLED(tp1->flags))) { 3264475d0674SMichael Tuexen /* 3265475d0674SMichael Tuexen * Has it been retransmitted tv_sec times? - 3266475d0674SMichael Tuexen * we store the retran count there. 3267475d0674SMichael Tuexen */ 3268475d0674SMichael Tuexen if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) { 3269475d0674SMichael Tuexen /* Yes, so drop it */ 3270475d0674SMichael Tuexen if (tp1->data != NULL) { 32711edc9dbaSMichael Tuexen (void)sctp_release_pr_sctp_chunk(stcb, tp1, 1, 3272475d0674SMichael Tuexen SCTP_SO_NOT_LOCKED); 3273475d0674SMichael Tuexen } 3274475d0674SMichael Tuexen /* Make sure to flag we had a FR */ 3275475d0674SMichael Tuexen tp1->whoTo->net_ack++; 3276475d0674SMichael Tuexen continue; 3277475d0674SMichael Tuexen } 3278475d0674SMichael Tuexen } 3279cd3fd531SMichael Tuexen /* 3280cd3fd531SMichael Tuexen * SCTP_PRINTF("OK, we are now ready to FR this 3281cd3fd531SMichael Tuexen * guy\n"); 3282cd3fd531SMichael Tuexen */ 3283b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) { 3284f8829a4aSRandall Stewart sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count, 3285f8829a4aSRandall Stewart 0, SCTP_FR_MARKED); 328680fefe0aSRandall Stewart } 3287f8829a4aSRandall Stewart if (strike_flag) { 3288f8829a4aSRandall Stewart /* This is a subsequent FR */ 3289f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_sendmultfastretrans); 3290f8829a4aSRandall Stewart } 32915e54f665SRandall Stewart sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt); 32927c99d56fSMichael Tuexen if (asoc->sctp_cmt_on_off > 0) { 3293f8829a4aSRandall Stewart /* 3294f8829a4aSRandall Stewart * CMT: Using RTX_SSTHRESH policy for CMT. 3295f8829a4aSRandall Stewart * If CMT is being used, then pick dest with 3296f8829a4aSRandall Stewart * largest ssthresh for any retransmission. 3297f8829a4aSRandall Stewart */ 3298f8829a4aSRandall Stewart tp1->no_fr_allowed = 1; 3299f8829a4aSRandall Stewart alt = tp1->whoTo; 33003c503c28SRandall Stewart /* sa_ignore NO_NULL_CHK */ 330120083c2eSMichael Tuexen if (asoc->sctp_cmt_pf > 0) { 3302b54d3a6cSRandall Stewart /* 3303b54d3a6cSRandall Stewart * JRS 5/18/07 - If CMT PF is on, 3304b54d3a6cSRandall Stewart * use the PF version of 3305b54d3a6cSRandall Stewart * find_alt_net() 3306b54d3a6cSRandall Stewart */ 3307b54d3a6cSRandall Stewart alt = sctp_find_alternate_net(stcb, alt, 2); 3308b54d3a6cSRandall Stewart } else { 3309b54d3a6cSRandall Stewart /* 3310b54d3a6cSRandall Stewart * JRS 5/18/07 - If only CMT is on, 3311b54d3a6cSRandall Stewart * use the CMT version of 3312b54d3a6cSRandall Stewart * find_alt_net() 3313b54d3a6cSRandall Stewart */ 331452be287eSRandall Stewart /* sa_ignore NO_NULL_CHK */ 3315f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, alt, 1); 3316b54d3a6cSRandall Stewart } 3317ad81507eSRandall Stewart if (alt == NULL) { 3318ad81507eSRandall Stewart alt = tp1->whoTo; 3319ad81507eSRandall Stewart } 3320f8829a4aSRandall Stewart /* 3321f8829a4aSRandall Stewart * CUCv2: If a different dest is picked for 3322f8829a4aSRandall Stewart * the retransmission, then new 3323f8829a4aSRandall Stewart * (rtx-)pseudo_cumack needs to be tracked 3324f8829a4aSRandall Stewart * for orig dest. Let CUCv2 track new (rtx-) 3325f8829a4aSRandall Stewart * pseudo-cumack always. 3326f8829a4aSRandall Stewart */ 3327ad81507eSRandall Stewart if (tp1->whoTo) { 3328f8829a4aSRandall Stewart tp1->whoTo->find_pseudo_cumack = 1; 3329f8829a4aSRandall Stewart tp1->whoTo->find_rtx_pseudo_cumack = 1; 3330ad81507eSRandall Stewart } 3331f8829a4aSRandall Stewart } else {/* CMT is OFF */ 3332f8829a4aSRandall Stewart 3333f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE 3334f8829a4aSRandall Stewart /* Can we find an alternate? */ 3335f8829a4aSRandall Stewart alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0); 3336f8829a4aSRandall Stewart #else 3337f8829a4aSRandall Stewart /* 3338f8829a4aSRandall Stewart * default behavior is to NOT retransmit 3339f8829a4aSRandall Stewart * FR's to an alternate. Armando Caro's 3340f8829a4aSRandall Stewart * paper details why. 3341f8829a4aSRandall Stewart */ 3342f8829a4aSRandall Stewart alt = tp1->whoTo; 3343f8829a4aSRandall Stewart #endif 3344f8829a4aSRandall Stewart } 3345f8829a4aSRandall Stewart 3346f8829a4aSRandall Stewart tp1->rec.data.doing_fast_retransmit = 1; 3347f8829a4aSRandall Stewart tot_retrans++; 3348f8829a4aSRandall Stewart /* mark the sending seq for possible subsequent FR's */ 3349f8829a4aSRandall Stewart /* 3350cd3fd531SMichael Tuexen * SCTP_PRINTF("Marking TSN for FR new value %x\n", 3351f8829a4aSRandall Stewart * (uint32_t)tpi->rec.data.TSN_seq); 3352f8829a4aSRandall Stewart */ 3353f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->send_queue)) { 3354f8829a4aSRandall Stewart /* 3355f8829a4aSRandall Stewart * If the queue of send is empty then its 3356f8829a4aSRandall Stewart * the next sequence number that will be 3357f8829a4aSRandall Stewart * assigned so we subtract one from this to 3358f8829a4aSRandall Stewart * get the one we last sent. 3359f8829a4aSRandall Stewart */ 3360f8829a4aSRandall Stewart tp1->rec.data.fast_retran_tsn = sending_seq; 3361f8829a4aSRandall Stewart } else { 3362f8829a4aSRandall Stewart /* 3363f8829a4aSRandall Stewart * If there are chunks on the send queue 3364f8829a4aSRandall Stewart * (unsent data that has made it from the 3365f8829a4aSRandall Stewart * stream queues but not out the door, we 3366f8829a4aSRandall Stewart * take the first one (which will have the 3367f8829a4aSRandall Stewart * lowest TSN) and subtract one to get the 3368f8829a4aSRandall Stewart * one we last sent. 3369f8829a4aSRandall Stewart */ 3370f8829a4aSRandall Stewart struct sctp_tmit_chunk *ttt; 3371f8829a4aSRandall Stewart 3372f8829a4aSRandall Stewart ttt = TAILQ_FIRST(&asoc->send_queue); 3373f8829a4aSRandall Stewart tp1->rec.data.fast_retran_tsn = 3374f8829a4aSRandall Stewart ttt->rec.data.TSN_seq; 3375f8829a4aSRandall Stewart } 3376f8829a4aSRandall Stewart 3377f8829a4aSRandall Stewart if (tp1->do_rtt) { 3378f8829a4aSRandall Stewart /* 3379f8829a4aSRandall Stewart * this guy had a RTO calculation pending on 3380f8829a4aSRandall Stewart * it, cancel it 3381f8829a4aSRandall Stewart */ 338260990c0cSMichael Tuexen if ((tp1->whoTo != NULL) && 338360990c0cSMichael Tuexen (tp1->whoTo->rto_needed == 0)) { 3384f79aab18SRandall Stewart tp1->whoTo->rto_needed = 1; 3385f79aab18SRandall Stewart } 3386f8829a4aSRandall Stewart tp1->do_rtt = 0; 3387f8829a4aSRandall Stewart } 3388f8829a4aSRandall Stewart if (alt != tp1->whoTo) { 3389f8829a4aSRandall Stewart /* yes, there is an alternate. */ 3390f8829a4aSRandall Stewart sctp_free_remote_addr(tp1->whoTo); 33913c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 3392f8829a4aSRandall Stewart tp1->whoTo = alt; 3393f8829a4aSRandall Stewart atomic_add_int(&alt->ref_count, 1); 3394f8829a4aSRandall Stewart } 3395f8829a4aSRandall Stewart } 33964a9ef3f8SMichael Tuexen } 3397f8829a4aSRandall Stewart } 3398f8829a4aSRandall Stewart 3399f8829a4aSRandall Stewart struct sctp_tmit_chunk * 3400f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb, 3401f8829a4aSRandall Stewart struct sctp_association *asoc) 3402f8829a4aSRandall Stewart { 3403f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL; 3404f8829a4aSRandall Stewart struct timeval now; 3405f8829a4aSRandall Stewart int now_filled = 0; 3406f8829a4aSRandall Stewart 3407dd973b0eSMichael Tuexen if (asoc->prsctp_supported == 0) { 3408f8829a4aSRandall Stewart return (NULL); 3409f8829a4aSRandall Stewart } 34104a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) { 3411f8829a4aSRandall Stewart if (tp1->sent != SCTP_FORWARD_TSN_SKIP && 341298f2956cSMichael Tuexen tp1->sent != SCTP_DATAGRAM_RESEND && 3413325c8c46SMichael Tuexen tp1->sent != SCTP_DATAGRAM_NR_ACKED) { 3414f8829a4aSRandall Stewart /* no chance to advance, out of here */ 3415f8829a4aSRandall Stewart break; 3416f8829a4aSRandall Stewart } 34170c0982b8SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { 34182a498584SMichael Tuexen if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) || 3419325c8c46SMichael Tuexen (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) { 34200c0982b8SRandall Stewart sctp_misc_ints(SCTP_FWD_TSN_CHECK, 34210c0982b8SRandall Stewart asoc->advanced_peer_ack_point, 34220c0982b8SRandall Stewart tp1->rec.data.TSN_seq, 0, 0); 34230c0982b8SRandall Stewart } 34240c0982b8SRandall Stewart } 3425f8829a4aSRandall Stewart if (!PR_SCTP_ENABLED(tp1->flags)) { 3426f8829a4aSRandall Stewart /* 3427f8829a4aSRandall Stewart * We can't fwd-tsn past any that are reliable aka 3428f8829a4aSRandall Stewart * retransmitted until the asoc fails. 3429f8829a4aSRandall Stewart */ 3430f8829a4aSRandall Stewart break; 3431f8829a4aSRandall Stewart } 3432f8829a4aSRandall Stewart if (!now_filled) { 34336e55db54SRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&now); 3434f8829a4aSRandall Stewart now_filled = 1; 3435f8829a4aSRandall Stewart } 3436f8829a4aSRandall Stewart /* 3437f8829a4aSRandall Stewart * now we got a chunk which is marked for another 3438f8829a4aSRandall Stewart * retransmission to a PR-stream but has run out its chances 3439f8829a4aSRandall Stewart * already maybe OR has been marked to skip now. Can we skip 3440f8829a4aSRandall Stewart * it if its a resend? 3441f8829a4aSRandall Stewart */ 3442f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND && 3443f8829a4aSRandall Stewart (PR_SCTP_TTL_ENABLED(tp1->flags))) { 3444f8829a4aSRandall Stewart /* 3445f8829a4aSRandall Stewart * Now is this one marked for resend and its time is 3446f8829a4aSRandall Stewart * now up? 3447f8829a4aSRandall Stewart */ 3448f8829a4aSRandall Stewart if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) { 3449f8829a4aSRandall Stewart /* Yes so drop it */ 3450f8829a4aSRandall Stewart if (tp1->data) { 3451ad81507eSRandall Stewart (void)sctp_release_pr_sctp_chunk(stcb, tp1, 34521edc9dbaSMichael Tuexen 1, SCTP_SO_NOT_LOCKED); 3453f8829a4aSRandall Stewart } 3454f8829a4aSRandall Stewart } else { 3455f8829a4aSRandall Stewart /* 3456f8829a4aSRandall Stewart * No, we are done when hit one for resend 3457f8829a4aSRandall Stewart * whos time as not expired. 3458f8829a4aSRandall Stewart */ 3459f8829a4aSRandall Stewart break; 3460f8829a4aSRandall Stewart } 3461f8829a4aSRandall Stewart } 3462f8829a4aSRandall Stewart /* 3463f8829a4aSRandall Stewart * Ok now if this chunk is marked to drop it we can clean up 3464f8829a4aSRandall Stewart * the chunk, advance our peer ack point and we can check 3465f8829a4aSRandall Stewart * the next chunk. 3466f8829a4aSRandall Stewart */ 346798f2956cSMichael Tuexen if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) || 3468325c8c46SMichael Tuexen (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) { 3469f8829a4aSRandall Stewart /* advance PeerAckPoint goes forward */ 347020b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) { 3471f8829a4aSRandall Stewart asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq; 3472f8829a4aSRandall Stewart a_adv = tp1; 34730c0982b8SRandall Stewart } else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) { 34740c0982b8SRandall Stewart /* No update but we do save the chk */ 34750c0982b8SRandall Stewart a_adv = tp1; 34760c0982b8SRandall Stewart } 3477f8829a4aSRandall Stewart } else { 3478f8829a4aSRandall Stewart /* 3479f8829a4aSRandall Stewart * If it is still in RESEND we can advance no 3480f8829a4aSRandall Stewart * further 3481f8829a4aSRandall Stewart */ 3482f8829a4aSRandall Stewart break; 3483f8829a4aSRandall Stewart } 3484f8829a4aSRandall Stewart } 3485f8829a4aSRandall Stewart return (a_adv); 3486f8829a4aSRandall Stewart } 3487f8829a4aSRandall Stewart 34880c0982b8SRandall Stewart static int 3489c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc) 3490bff64a4dSRandall Stewart { 3491bff64a4dSRandall Stewart struct sctp_tmit_chunk *chk; 3492bff64a4dSRandall Stewart int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0; 3493548f47a8SMichael Tuexen int ret; 34940c0982b8SRandall Stewart 3495548f47a8SMichael Tuexen #ifndef INVARIANTS 3496548f47a8SMichael Tuexen int entry_flight, entry_cnt; 3497548f47a8SMichael Tuexen 3498548f47a8SMichael Tuexen #endif 3499548f47a8SMichael Tuexen 3500548f47a8SMichael Tuexen ret = 0; 3501548f47a8SMichael Tuexen #ifndef INVARIANTS 35020c0982b8SRandall Stewart entry_flight = asoc->total_flight; 35030c0982b8SRandall Stewart entry_cnt = asoc->total_flight_count; 3504548f47a8SMichael Tuexen #endif 35050c0982b8SRandall Stewart if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt) 35060c0982b8SRandall Stewart return (0); 3507bff64a4dSRandall Stewart 3508bff64a4dSRandall Stewart TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) { 3509bff64a4dSRandall Stewart if (chk->sent < SCTP_DATAGRAM_RESEND) { 3510cd3fd531SMichael Tuexen SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n", 35110c0982b8SRandall Stewart chk->rec.data.TSN_seq, 35120c0982b8SRandall Stewart chk->send_size, 3513cd3fd531SMichael Tuexen chk->snd_count); 3514bff64a4dSRandall Stewart inflight++; 3515bff64a4dSRandall Stewart } else if (chk->sent == SCTP_DATAGRAM_RESEND) { 3516bff64a4dSRandall Stewart resend++; 3517bff64a4dSRandall Stewart } else if (chk->sent < SCTP_DATAGRAM_ACKED) { 3518bff64a4dSRandall Stewart inbetween++; 3519bff64a4dSRandall Stewart } else if (chk->sent > SCTP_DATAGRAM_ACKED) { 3520bff64a4dSRandall Stewart above++; 3521bff64a4dSRandall Stewart } else { 3522bff64a4dSRandall Stewart acked++; 3523bff64a4dSRandall Stewart } 3524bff64a4dSRandall Stewart } 3525f1f73e57SRandall Stewart 3526c105859eSRandall Stewart if ((inflight > 0) || (inbetween > 0)) { 3527f1f73e57SRandall Stewart #ifdef INVARIANTS 3528c105859eSRandall Stewart panic("Flight size-express incorrect? \n"); 3529f1f73e57SRandall Stewart #else 3530cd3fd531SMichael Tuexen SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n", 35310c0982b8SRandall Stewart entry_flight, entry_cnt); 35320c0982b8SRandall Stewart 35330c0982b8SRandall Stewart SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n", 35340c0982b8SRandall Stewart inflight, inbetween, resend, above, acked); 35350c0982b8SRandall Stewart ret = 1; 3536f1f73e57SRandall Stewart #endif 3537bff64a4dSRandall Stewart } 35380c0982b8SRandall Stewart return (ret); 3539c105859eSRandall Stewart } 3540c105859eSRandall Stewart 3541c105859eSRandall Stewart 3542c105859eSRandall Stewart static void 3543c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb, 3544c105859eSRandall Stewart struct sctp_association *asoc, 3545c105859eSRandall Stewart struct sctp_tmit_chunk *tp1) 3546c105859eSRandall Stewart { 3547dfb11ef8SRandall Stewart tp1->window_probe = 0; 35485171328bSRandall Stewart if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) { 3549dfb11ef8SRandall Stewart /* TSN's skipped we do NOT move back. */ 3550dfb11ef8SRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD, 35518427b3fdSMichael Tuexen tp1->whoTo ? tp1->whoTo->flight_size : 0, 3552dfb11ef8SRandall Stewart tp1->book_size, 3553dfb11ef8SRandall Stewart (uintptr_t) tp1->whoTo, 3554dfb11ef8SRandall Stewart tp1->rec.data.TSN_seq); 3555dfb11ef8SRandall Stewart return; 3556dfb11ef8SRandall Stewart } 35575171328bSRandall Stewart /* First setup this by shrinking flight */ 3558299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) { 3559299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo, 3560299108c5SRandall Stewart tp1); 3561299108c5SRandall Stewart } 35625171328bSRandall Stewart sctp_flight_size_decrease(tp1); 35635171328bSRandall Stewart sctp_total_flight_decrease(stcb, tp1); 35645171328bSRandall Stewart /* Now mark for resend */ 35655171328bSRandall Stewart tp1->sent = SCTP_DATAGRAM_RESEND; 3566791437b5SRandall Stewart sctp_ucount_incr(asoc->sent_queue_retran_cnt); 3567791437b5SRandall Stewart 3568b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3569c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP, 3570c105859eSRandall Stewart tp1->whoTo->flight_size, 3571c105859eSRandall Stewart tp1->book_size, 3572c105859eSRandall Stewart (uintptr_t) tp1->whoTo, 3573c105859eSRandall Stewart tp1->rec.data.TSN_seq); 357480fefe0aSRandall Stewart } 3575c105859eSRandall Stewart } 3576c105859eSRandall Stewart 3577f8829a4aSRandall Stewart void 3578f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack, 3579899288aeSRandall Stewart uint32_t rwnd, int *abort_now, int ecne_seen) 3580f8829a4aSRandall Stewart { 3581f8829a4aSRandall Stewart struct sctp_nets *net; 3582f8829a4aSRandall Stewart struct sctp_association *asoc; 3583f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1, *tp2; 35845e54f665SRandall Stewart uint32_t old_rwnd; 35855e54f665SRandall Stewart int win_probe_recovery = 0; 3586c105859eSRandall Stewart int win_probe_recovered = 0; 3587d06c82f1SRandall Stewart int j, done_once = 0; 3588f79aab18SRandall Stewart int rto_ok = 1; 3589f8829a4aSRandall Stewart 3590b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) { 3591d06c82f1SRandall Stewart sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack, 3592d06c82f1SRandall Stewart rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd); 359380fefe0aSRandall Stewart } 3594f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 359518e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 359618e198d3SRandall Stewart stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack; 359718e198d3SRandall Stewart stcb->asoc.cumack_log_at++; 359818e198d3SRandall Stewart if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) { 359918e198d3SRandall Stewart stcb->asoc.cumack_log_at = 0; 360018e198d3SRandall Stewart } 360118e198d3SRandall Stewart #endif 3602f8829a4aSRandall Stewart asoc = &stcb->asoc; 3603d06c82f1SRandall Stewart old_rwnd = asoc->peers_rwnd; 360420b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) { 36055e54f665SRandall Stewart /* old ack */ 36065e54f665SRandall Stewart return; 3607d06c82f1SRandall Stewart } else if (asoc->last_acked_seq == cumack) { 3608d06c82f1SRandall Stewart /* Window update sack */ 3609d06c82f1SRandall Stewart asoc->peers_rwnd = sctp_sbspace_sub(rwnd, 361044fbe462SRandall Stewart (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)))); 3611d06c82f1SRandall Stewart if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3612d06c82f1SRandall Stewart /* SWS sender side engages */ 3613d06c82f1SRandall Stewart asoc->peers_rwnd = 0; 3614d06c82f1SRandall Stewart } 3615d06c82f1SRandall Stewart if (asoc->peers_rwnd > old_rwnd) { 3616d06c82f1SRandall Stewart goto again; 3617d06c82f1SRandall Stewart } 3618d06c82f1SRandall Stewart return; 36195e54f665SRandall Stewart } 3620f8829a4aSRandall Stewart /* First setup for CC stuff */ 3621f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 3622a21779f0SRandall Stewart if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) { 3623a21779f0SRandall Stewart /* Drag along the window_tsn for cwr's */ 3624a21779f0SRandall Stewart net->cwr_window_tsn = cumack; 3625a21779f0SRandall Stewart } 3626f8829a4aSRandall Stewart net->prev_cwnd = net->cwnd; 3627f8829a4aSRandall Stewart net->net_ack = 0; 3628f8829a4aSRandall Stewart net->net_ack2 = 0; 3629132dea7dSRandall Stewart 3630132dea7dSRandall Stewart /* 3631132dea7dSRandall Stewart * CMT: Reset CUC and Fast recovery algo variables before 3632132dea7dSRandall Stewart * SACK processing 3633132dea7dSRandall Stewart */ 3634132dea7dSRandall Stewart net->new_pseudo_cumack = 0; 3635132dea7dSRandall Stewart net->will_exit_fast_recovery = 0; 3636299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) { 3637299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net); 3638299108c5SRandall Stewart } 3639f8829a4aSRandall Stewart } 3640b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) { 3641139bc87fSRandall Stewart uint32_t send_s; 3642139bc87fSRandall Stewart 3643c105859eSRandall Stewart if (!TAILQ_EMPTY(&asoc->sent_queue)) { 3644c105859eSRandall Stewart tp1 = TAILQ_LAST(&asoc->sent_queue, 3645c105859eSRandall Stewart sctpchunk_listhead); 3646c105859eSRandall Stewart send_s = tp1->rec.data.TSN_seq + 1; 3647139bc87fSRandall Stewart } else { 3648c105859eSRandall Stewart send_s = asoc->sending_seq; 3649139bc87fSRandall Stewart } 365020b07a4dSMichael Tuexen if (SCTP_TSN_GE(cumack, send_s)) { 3651ff1ffd74SMichael Tuexen struct mbuf *op_err; 3652ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 3653139bc87fSRandall Stewart 3654139bc87fSRandall Stewart *abort_now = 1; 3655139bc87fSRandall Stewart /* XXX */ 365655f8a4bbSMichael Tuexen snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x", 3657ff1ffd74SMichael Tuexen cumack, send_s); 3658ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 3659b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_23; 3660ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 3661139bc87fSRandall Stewart return; 3662139bc87fSRandall Stewart } 3663139bc87fSRandall Stewart } 3664f8829a4aSRandall Stewart asoc->this_sack_highest_gap = cumack; 3665b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 3666c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 3667c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 3668c4739e2fSRandall Stewart 0, 3669c4739e2fSRandall Stewart SCTP_FROM_SCTP_INDATA, 3670c4739e2fSRandall Stewart __LINE__); 3671c4739e2fSRandall Stewart } 3672f8829a4aSRandall Stewart stcb->asoc.overall_error_count = 0; 367320b07a4dSMichael Tuexen if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) { 3674f8829a4aSRandall Stewart /* process the new consecutive TSN first */ 36754a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) { 367620b07a4dSMichael Tuexen if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) { 367718e198d3SRandall Stewart if (tp1->sent == SCTP_DATAGRAM_UNSENT) { 3678cd3fd531SMichael Tuexen SCTP_PRINTF("Warning, an unsent is now acked?\n"); 367918e198d3SRandall Stewart } 3680f8829a4aSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_ACKED) { 3681f8829a4aSRandall Stewart /* 368218e198d3SRandall Stewart * If it is less than ACKED, it is 368318e198d3SRandall Stewart * now no-longer in flight. Higher 368418e198d3SRandall Stewart * values may occur during marking 3685f8829a4aSRandall Stewart */ 3686c105859eSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3687b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 3688c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA, 3689a5d547adSRandall Stewart tp1->whoTo->flight_size, 3690a5d547adSRandall Stewart tp1->book_size, 3691c105859eSRandall Stewart (uintptr_t) tp1->whoTo, 3692a5d547adSRandall Stewart tp1->rec.data.TSN_seq); 369380fefe0aSRandall Stewart } 3694c105859eSRandall Stewart sctp_flight_size_decrease(tp1); 3695299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) { 3696299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo, 3697299108c5SRandall Stewart tp1); 3698299108c5SRandall Stewart } 369904ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 3700c105859eSRandall Stewart sctp_total_flight_decrease(stcb, tp1); 3701f8829a4aSRandall Stewart } 3702f8829a4aSRandall Stewart tp1->whoTo->net_ack += tp1->send_size; 3703f8829a4aSRandall Stewart if (tp1->snd_count < 2) { 3704f8829a4aSRandall Stewart /* 370518e198d3SRandall Stewart * True non-retransmited 3706f8829a4aSRandall Stewart * chunk 3707f8829a4aSRandall Stewart */ 3708f8829a4aSRandall Stewart tp1->whoTo->net_ack2 += 3709f8829a4aSRandall Stewart tp1->send_size; 3710f8829a4aSRandall Stewart 3711f8829a4aSRandall Stewart /* update RTO too? */ 371262c1ff9cSRandall Stewart if (tp1->do_rtt) { 3713f79aab18SRandall Stewart if (rto_ok) { 3714f8829a4aSRandall Stewart tp1->whoTo->RTO = 371504ee05e8SRandall Stewart /* 371604ee05e8SRandall Stewart * sa_ignore 3717f79aab18SRandall Stewart * NO_NULL_CH 3718f79aab18SRandall Stewart * K 371904ee05e8SRandall Stewart */ 3720f8829a4aSRandall Stewart sctp_calculate_rto(stcb, 3721f8829a4aSRandall Stewart asoc, tp1->whoTo, 372218e198d3SRandall Stewart &tp1->sent_rcv_time, 3723899288aeSRandall Stewart sctp_align_safe_nocopy, 3724f79aab18SRandall Stewart SCTP_RTT_FROM_DATA); 3725f79aab18SRandall Stewart rto_ok = 0; 3726f79aab18SRandall Stewart } 3727f79aab18SRandall Stewart if (tp1->whoTo->rto_needed == 0) { 3728f79aab18SRandall Stewart tp1->whoTo->rto_needed = 1; 3729f79aab18SRandall Stewart } 3730f8829a4aSRandall Stewart tp1->do_rtt = 0; 3731f8829a4aSRandall Stewart } 3732f8829a4aSRandall Stewart } 3733132dea7dSRandall Stewart /* 373418e198d3SRandall Stewart * CMT: CUCv2 algorithm. From the 373518e198d3SRandall Stewart * cumack'd TSNs, for each TSN being 373618e198d3SRandall Stewart * acked for the first time, set the 373718e198d3SRandall Stewart * following variables for the 373818e198d3SRandall Stewart * corresp destination. 373918e198d3SRandall Stewart * new_pseudo_cumack will trigger a 374018e198d3SRandall Stewart * cwnd update. 374118e198d3SRandall Stewart * find_(rtx_)pseudo_cumack will 374218e198d3SRandall Stewart * trigger search for the next 374318e198d3SRandall Stewart * expected (rtx-)pseudo-cumack. 3744132dea7dSRandall Stewart */ 3745132dea7dSRandall Stewart tp1->whoTo->new_pseudo_cumack = 1; 3746132dea7dSRandall Stewart tp1->whoTo->find_pseudo_cumack = 1; 3747132dea7dSRandall Stewart tp1->whoTo->find_rtx_pseudo_cumack = 1; 3748132dea7dSRandall Stewart 3749b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 375004ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 3751f8829a4aSRandall Stewart sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK); 375280fefe0aSRandall Stewart } 3753f8829a4aSRandall Stewart } 3754f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND) { 3755f8829a4aSRandall Stewart sctp_ucount_decr(asoc->sent_queue_retran_cnt); 3756f8829a4aSRandall Stewart } 375742551e99SRandall Stewart if (tp1->rec.data.chunk_was_revoked) { 375842551e99SRandall Stewart /* deflate the cwnd */ 375942551e99SRandall Stewart tp1->whoTo->cwnd -= tp1->book_size; 376042551e99SRandall Stewart tp1->rec.data.chunk_was_revoked = 0; 376142551e99SRandall Stewart } 3762325c8c46SMichael Tuexen if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) { 3763a7ad6026SMichael Tuexen if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) { 3764a7ad6026SMichael Tuexen asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--; 3765a7ad6026SMichael Tuexen #ifdef INVARIANTS 3766a7ad6026SMichael Tuexen } else { 3767a7ad6026SMichael Tuexen panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number); 3768a7ad6026SMichael Tuexen #endif 3769a7ad6026SMichael Tuexen } 3770a7ad6026SMichael Tuexen } 3771f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next); 3772f8829a4aSRandall Stewart if (tp1->data) { 377304ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 3774f8829a4aSRandall Stewart sctp_free_bufspace(stcb, asoc, tp1, 1); 3775f8829a4aSRandall Stewart sctp_m_freem(tp1->data); 37764a9ef3f8SMichael Tuexen tp1->data = NULL; 3777f8829a4aSRandall Stewart } 3778b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 3779f8829a4aSRandall Stewart sctp_log_sack(asoc->last_acked_seq, 3780f8829a4aSRandall Stewart cumack, 3781f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 3782f8829a4aSRandall Stewart 0, 3783f8829a4aSRandall Stewart 0, 3784f8829a4aSRandall Stewart SCTP_LOG_FREE_SENT); 378580fefe0aSRandall Stewart } 3786f8829a4aSRandall Stewart asoc->sent_queue_cnt--; 3787689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED); 378818e198d3SRandall Stewart } else { 378918e198d3SRandall Stewart break; 3790f8829a4aSRandall Stewart } 37915e54f665SRandall Stewart } 379218e198d3SRandall Stewart 379318e198d3SRandall Stewart } 379404ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 3795f8829a4aSRandall Stewart if (stcb->sctp_socket) { 3796ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3797ceaad40aSRandall Stewart struct socket *so; 3798ceaad40aSRandall Stewart 3799ceaad40aSRandall Stewart #endif 3800f8829a4aSRandall Stewart SOCKBUF_LOCK(&stcb->sctp_socket->so_snd); 3801b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) { 380204ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 38037215cc1bSMichael Tuexen sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK); 380480fefe0aSRandall Stewart } 3805ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3806ceaad40aSRandall Stewart so = SCTP_INP_SO(stcb->sctp_ep); 3807ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 3808ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 3809ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 3810ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 3811ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 3812ceaad40aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 3813ceaad40aSRandall Stewart /* assoc was freed while we were unlocked */ 3814ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 3815ceaad40aSRandall Stewart return; 3816ceaad40aSRandall Stewart } 3817ceaad40aSRandall Stewart #endif 3818f8829a4aSRandall Stewart sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket); 3819ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 3820ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 3821ceaad40aSRandall Stewart #endif 3822f8829a4aSRandall Stewart } else { 3823b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) { 38247215cc1bSMichael Tuexen sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK); 382580fefe0aSRandall Stewart } 3826f8829a4aSRandall Stewart } 3827f8829a4aSRandall Stewart 3828b54d3a6cSRandall Stewart /* JRS - Use the congestion control given in the CC module */ 3829ca85e948SMichael Tuexen if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) { 3830ca85e948SMichael Tuexen TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 3831ca85e948SMichael Tuexen if (net->net_ack2 > 0) { 3832ca85e948SMichael Tuexen /* 3833ca85e948SMichael Tuexen * Karn's rule applies to clearing error 3834ca85e948SMichael Tuexen * count, this is optional. 3835ca85e948SMichael Tuexen */ 3836ca85e948SMichael Tuexen net->error_count = 0; 3837ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { 3838ca85e948SMichael Tuexen /* addr came good */ 3839ca85e948SMichael Tuexen net->dest_state |= SCTP_ADDR_REACHABLE; 3840ca85e948SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 38414b1f78e1SMichael Tuexen 0, (void *)net, SCTP_SO_NOT_LOCKED); 3842ca85e948SMichael Tuexen } 3843ca85e948SMichael Tuexen if (net == stcb->asoc.primary_destination) { 3844ca85e948SMichael Tuexen if (stcb->asoc.alternate) { 3845ca85e948SMichael Tuexen /* 3846ca85e948SMichael Tuexen * release the alternate, 3847ca85e948SMichael Tuexen * primary is good 3848ca85e948SMichael Tuexen */ 3849ca85e948SMichael Tuexen sctp_free_remote_addr(stcb->asoc.alternate); 3850ca85e948SMichael Tuexen stcb->asoc.alternate = NULL; 3851ca85e948SMichael Tuexen } 3852ca85e948SMichael Tuexen } 3853ca85e948SMichael Tuexen if (net->dest_state & SCTP_ADDR_PF) { 3854ca85e948SMichael Tuexen net->dest_state &= ~SCTP_ADDR_PF; 3855b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, 3856b7d130beSMichael Tuexen stcb->sctp_ep, stcb, net, 3857b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_24); 3858ca85e948SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 3859ca85e948SMichael Tuexen asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 3860ca85e948SMichael Tuexen /* Done with this net */ 3861ca85e948SMichael Tuexen net->net_ack = 0; 3862ca85e948SMichael Tuexen } 3863ca85e948SMichael Tuexen /* restore any doubled timers */ 3864ca85e948SMichael Tuexen net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv; 3865ca85e948SMichael Tuexen if (net->RTO < stcb->asoc.minrto) { 3866ca85e948SMichael Tuexen net->RTO = stcb->asoc.minrto; 3867ca85e948SMichael Tuexen } 3868ca85e948SMichael Tuexen if (net->RTO > stcb->asoc.maxrto) { 3869ca85e948SMichael Tuexen net->RTO = stcb->asoc.maxrto; 3870ca85e948SMichael Tuexen } 3871ca85e948SMichael Tuexen } 3872ca85e948SMichael Tuexen } 3873b54d3a6cSRandall Stewart asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0); 3874ca85e948SMichael Tuexen } 3875f8829a4aSRandall Stewart asoc->last_acked_seq = cumack; 38765e54f665SRandall Stewart 3877f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->sent_queue)) { 3878f8829a4aSRandall Stewart /* nothing left in-flight */ 3879f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 3880f8829a4aSRandall Stewart net->flight_size = 0; 3881f8829a4aSRandall Stewart net->partial_bytes_acked = 0; 3882f8829a4aSRandall Stewart } 3883f8829a4aSRandall Stewart asoc->total_flight = 0; 3884f8829a4aSRandall Stewart asoc->total_flight_count = 0; 3885f8829a4aSRandall Stewart } 3886f8829a4aSRandall Stewart /* RWND update */ 3887f8829a4aSRandall Stewart asoc->peers_rwnd = sctp_sbspace_sub(rwnd, 388844fbe462SRandall Stewart (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)))); 3889f8829a4aSRandall Stewart if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 3890f8829a4aSRandall Stewart /* SWS sender side engages */ 3891f8829a4aSRandall Stewart asoc->peers_rwnd = 0; 3892f8829a4aSRandall Stewart } 38935e54f665SRandall Stewart if (asoc->peers_rwnd > old_rwnd) { 38945e54f665SRandall Stewart win_probe_recovery = 1; 38955e54f665SRandall Stewart } 3896f8829a4aSRandall Stewart /* Now assure a timer where data is queued at */ 3897a5d547adSRandall Stewart again: 3898a5d547adSRandall Stewart j = 0; 3899f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 39005171328bSRandall Stewart int to_ticks; 39015171328bSRandall Stewart 39025e54f665SRandall Stewart if (win_probe_recovery && (net->window_probe)) { 3903c105859eSRandall Stewart win_probe_recovered = 1; 39045e54f665SRandall Stewart /* 39055e54f665SRandall Stewart * Find first chunk that was used with window probe 39065e54f665SRandall Stewart * and clear the sent 39075e54f665SRandall Stewart */ 39083c503c28SRandall Stewart /* sa_ignore FREED_MEMORY */ 39095e54f665SRandall Stewart TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 39105e54f665SRandall Stewart if (tp1->window_probe) { 3911cd554309SMichael Tuexen /* move back to data send queue */ 39127215cc1bSMichael Tuexen sctp_window_probe_recovery(stcb, asoc, tp1); 39135e54f665SRandall Stewart break; 39145e54f665SRandall Stewart } 39155e54f665SRandall Stewart } 39165e54f665SRandall Stewart } 3917f8829a4aSRandall Stewart if (net->RTO == 0) { 3918f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto); 3919f8829a4aSRandall Stewart } else { 3920f8829a4aSRandall Stewart to_ticks = MSEC_TO_TICKS(net->RTO); 3921f8829a4aSRandall Stewart } 39225171328bSRandall Stewart if (net->flight_size) { 3923a5d547adSRandall Stewart j++; 3924ad81507eSRandall Stewart (void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks, 3925f8829a4aSRandall Stewart sctp_timeout_handler, &net->rxt_timer); 39265171328bSRandall Stewart if (net->window_probe) { 39275171328bSRandall Stewart net->window_probe = 0; 39285171328bSRandall Stewart } 3929f8829a4aSRandall Stewart } else { 39305171328bSRandall Stewart if (net->window_probe) { 39315171328bSRandall Stewart /* 39325171328bSRandall Stewart * In window probes we must assure a timer 39335171328bSRandall Stewart * is still running there 39345171328bSRandall Stewart */ 39355171328bSRandall Stewart net->window_probe = 0; 39365171328bSRandall Stewart if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 39375171328bSRandall Stewart SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks, 39385171328bSRandall Stewart sctp_timeout_handler, &net->rxt_timer); 39395171328bSRandall Stewart } 39405171328bSRandall Stewart } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 3941f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 3942a5d547adSRandall Stewart stcb, net, 3943b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_25); 3944f8829a4aSRandall Stewart } 3945f8829a4aSRandall Stewart } 3946f8829a4aSRandall Stewart } 3947bff64a4dSRandall Stewart if ((j == 0) && 3948bff64a4dSRandall Stewart (!TAILQ_EMPTY(&asoc->sent_queue)) && 3949bff64a4dSRandall Stewart (asoc->sent_queue_retran_cnt == 0) && 3950c105859eSRandall Stewart (win_probe_recovered == 0) && 3951bff64a4dSRandall Stewart (done_once == 0)) { 39520c0982b8SRandall Stewart /* 39530c0982b8SRandall Stewart * huh, this should not happen unless all packets are 39540c0982b8SRandall Stewart * PR-SCTP and marked to skip of course. 39550c0982b8SRandall Stewart */ 39560c0982b8SRandall Stewart if (sctp_fs_audit(asoc)) { 3957a5d547adSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 3958a5d547adSRandall Stewart net->flight_size = 0; 3959a5d547adSRandall Stewart } 3960a5d547adSRandall Stewart asoc->total_flight = 0; 3961a5d547adSRandall Stewart asoc->total_flight_count = 0; 3962a5d547adSRandall Stewart asoc->sent_queue_retran_cnt = 0; 3963a5d547adSRandall Stewart TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 3964a5d547adSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 3965c105859eSRandall Stewart sctp_flight_size_increase(tp1); 3966c105859eSRandall Stewart sctp_total_flight_increase(stcb, tp1); 3967a5d547adSRandall Stewart } else if (tp1->sent == SCTP_DATAGRAM_RESEND) { 3968791437b5SRandall Stewart sctp_ucount_incr(asoc->sent_queue_retran_cnt); 3969a5d547adSRandall Stewart } 3970a5d547adSRandall Stewart } 39710c0982b8SRandall Stewart } 3972bff64a4dSRandall Stewart done_once = 1; 3973a5d547adSRandall Stewart goto again; 3974a5d547adSRandall Stewart } 3975f8829a4aSRandall Stewart /**********************************/ 3976f8829a4aSRandall Stewart /* Now what about shutdown issues */ 3977f8829a4aSRandall Stewart /**********************************/ 3978f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) { 3979f8829a4aSRandall Stewart /* nothing left on sendqueue.. consider done */ 3980f8829a4aSRandall Stewart /* clean up */ 3981f8829a4aSRandall Stewart if ((asoc->stream_queue_cnt == 1) && 3982f8829a4aSRandall Stewart ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) || 3983f8829a4aSRandall Stewart (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) && 3984f8829a4aSRandall Stewart (asoc->locked_on_sending) 3985f8829a4aSRandall Stewart ) { 3986f8829a4aSRandall Stewart struct sctp_stream_queue_pending *sp; 3987f8829a4aSRandall Stewart 3988f8829a4aSRandall Stewart /* 3989f8829a4aSRandall Stewart * I may be in a state where we got all across.. but 3990f8829a4aSRandall Stewart * cannot write more due to a shutdown... we abort 3991f8829a4aSRandall Stewart * since the user did not indicate EOR in this case. 3992f8829a4aSRandall Stewart * The sp will be cleaned during free of the asoc. 3993f8829a4aSRandall Stewart */ 3994f8829a4aSRandall Stewart sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue), 3995f8829a4aSRandall Stewart sctp_streamhead); 39962afb3e84SRandall Stewart if ((sp) && (sp->length == 0)) { 39972afb3e84SRandall Stewart /* Let cleanup code purge it */ 39982afb3e84SRandall Stewart if (sp->msg_is_complete) { 39992afb3e84SRandall Stewart asoc->stream_queue_cnt--; 40002afb3e84SRandall Stewart } else { 4001f8829a4aSRandall Stewart asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 4002f8829a4aSRandall Stewart asoc->locked_on_sending = NULL; 4003f8829a4aSRandall Stewart asoc->stream_queue_cnt--; 4004f8829a4aSRandall Stewart } 4005f8829a4aSRandall Stewart } 40062afb3e84SRandall Stewart } 4007f8829a4aSRandall Stewart if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) && 4008f8829a4aSRandall Stewart (asoc->stream_queue_cnt == 0)) { 4009f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) { 4010f8829a4aSRandall Stewart /* Need to abort here */ 4011ff1ffd74SMichael Tuexen struct mbuf *op_err; 4012f8829a4aSRandall Stewart 4013f8829a4aSRandall Stewart abort_out_now: 4014f8829a4aSRandall Stewart *abort_now = 1; 4015f8829a4aSRandall Stewart /* XXX */ 4016ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); 4017b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_26; 4018ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 4019f8829a4aSRandall Stewart } else { 4020ca85e948SMichael Tuexen struct sctp_nets *netp; 4021ca85e948SMichael Tuexen 4022f42a358aSRandall Stewart if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 4023f42a358aSRandall Stewart (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 4024f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 4025f42a358aSRandall Stewart } 4026c4739e2fSRandall Stewart SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); 4027b201f536SRandall Stewart SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 4028f8829a4aSRandall Stewart sctp_stop_timers_for_shutdown(stcb); 4029ca85e948SMichael Tuexen if (asoc->alternate) { 4030ca85e948SMichael Tuexen netp = asoc->alternate; 4031ca85e948SMichael Tuexen } else { 4032ca85e948SMichael Tuexen netp = asoc->primary_destination; 4033ca85e948SMichael Tuexen } 4034ca85e948SMichael Tuexen sctp_send_shutdown(stcb, netp); 4035f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 4036ca85e948SMichael Tuexen stcb->sctp_ep, stcb, netp); 4037f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 4038ca85e948SMichael Tuexen stcb->sctp_ep, stcb, netp); 4039f8829a4aSRandall Stewart } 4040f8829a4aSRandall Stewart } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) && 4041f8829a4aSRandall Stewart (asoc->stream_queue_cnt == 0)) { 4042ca85e948SMichael Tuexen struct sctp_nets *netp; 4043ca85e948SMichael Tuexen 4044f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) { 4045f8829a4aSRandall Stewart goto abort_out_now; 4046f8829a4aSRandall Stewart } 4047f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 4048c4739e2fSRandall Stewart SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 4049b201f536SRandall Stewart SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 405012af6654SMichael Tuexen sctp_stop_timers_for_shutdown(stcb); 4051c39cfa1fSMichael Tuexen if (asoc->alternate) { 4052c39cfa1fSMichael Tuexen netp = asoc->alternate; 4053c39cfa1fSMichael Tuexen } else { 4054c39cfa1fSMichael Tuexen netp = asoc->primary_destination; 4055c39cfa1fSMichael Tuexen } 4056c39cfa1fSMichael Tuexen sctp_send_shutdown_ack(stcb, netp); 4057f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, 4058ca85e948SMichael Tuexen stcb->sctp_ep, stcb, netp); 4059f8829a4aSRandall Stewart } 4060f8829a4aSRandall Stewart } 4061dfb11ef8SRandall Stewart /*********************************************/ 4062dfb11ef8SRandall Stewart /* Here we perform PR-SCTP procedures */ 4063dfb11ef8SRandall Stewart /* (section 4.2) */ 4064dfb11ef8SRandall Stewart /*********************************************/ 4065dfb11ef8SRandall Stewart /* C1. update advancedPeerAckPoint */ 406620b07a4dSMichael Tuexen if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) { 4067dfb11ef8SRandall Stewart asoc->advanced_peer_ack_point = cumack; 4068dfb11ef8SRandall Stewart } 4069830d754dSRandall Stewart /* PR-Sctp issues need to be addressed too */ 4070dd973b0eSMichael Tuexen if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) { 4071830d754dSRandall Stewart struct sctp_tmit_chunk *lchk; 4072830d754dSRandall Stewart uint32_t old_adv_peer_ack_point; 4073830d754dSRandall Stewart 4074830d754dSRandall Stewart old_adv_peer_ack_point = asoc->advanced_peer_ack_point; 4075830d754dSRandall Stewart lchk = sctp_try_advance_peer_ack_point(stcb, asoc); 4076830d754dSRandall Stewart /* C3. See if we need to send a Fwd-TSN */ 407720b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) { 4078830d754dSRandall Stewart /* 4079493d8e5aSRandall Stewart * ISSUE with ECN, see FWD-TSN processing. 4080830d754dSRandall Stewart */ 408120b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) { 4082830d754dSRandall Stewart send_forward_tsn(stcb, asoc); 40830c0982b8SRandall Stewart } else if (lchk) { 40840c0982b8SRandall Stewart /* try to FR fwd-tsn's that get lost too */ 408544fbe462SRandall Stewart if (lchk->rec.data.fwd_tsn_cnt >= 3) { 40860c0982b8SRandall Stewart send_forward_tsn(stcb, asoc); 40870c0982b8SRandall Stewart } 4088830d754dSRandall Stewart } 4089830d754dSRandall Stewart } 4090830d754dSRandall Stewart if (lchk) { 4091830d754dSRandall Stewart /* Assure a timer is up */ 4092830d754dSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4093830d754dSRandall Stewart stcb->sctp_ep, stcb, lchk->whoTo); 4094830d754dSRandall Stewart } 4095830d754dSRandall Stewart } 4096b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) { 4097f8829a4aSRandall Stewart sctp_misc_ints(SCTP_SACK_RWND_UPDATE, 4098f8829a4aSRandall Stewart rwnd, 4099f8829a4aSRandall Stewart stcb->asoc.peers_rwnd, 4100f8829a4aSRandall Stewart stcb->asoc.total_flight, 4101f8829a4aSRandall Stewart stcb->asoc.total_output_queue_size); 410280fefe0aSRandall Stewart } 4103f8829a4aSRandall Stewart } 4104f8829a4aSRandall Stewart 4105f8829a4aSRandall Stewart void 4106cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup, 41077215cc1bSMichael Tuexen struct sctp_tcb *stcb, 4108cd554309SMichael Tuexen uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup, 4109cd554309SMichael Tuexen int *abort_now, uint8_t flags, 4110899288aeSRandall Stewart uint32_t cum_ack, uint32_t rwnd, int ecne_seen) 4111f8829a4aSRandall Stewart { 4112f8829a4aSRandall Stewart struct sctp_association *asoc; 4113f8829a4aSRandall Stewart struct sctp_tmit_chunk *tp1, *tp2; 4114cd554309SMichael Tuexen uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack; 4115f8829a4aSRandall Stewart uint16_t wake_him = 0; 4116c105859eSRandall Stewart uint32_t send_s = 0; 4117f8829a4aSRandall Stewart long j; 4118f8829a4aSRandall Stewart int accum_moved = 0; 4119f8829a4aSRandall Stewart int will_exit_fast_recovery = 0; 41205e54f665SRandall Stewart uint32_t a_rwnd, old_rwnd; 41215e54f665SRandall Stewart int win_probe_recovery = 0; 4122c105859eSRandall Stewart int win_probe_recovered = 0; 4123f8829a4aSRandall Stewart struct sctp_nets *net = NULL; 4124bff64a4dSRandall Stewart int done_once; 4125f79aab18SRandall Stewart int rto_ok = 1; 4126f8829a4aSRandall Stewart uint8_t reneged_all = 0; 4127f8829a4aSRandall Stewart uint8_t cmt_dac_flag; 4128f8829a4aSRandall Stewart 4129f8829a4aSRandall Stewart /* 4130f8829a4aSRandall Stewart * we take any chance we can to service our queues since we cannot 4131f8829a4aSRandall Stewart * get awoken when the socket is read from :< 4132f8829a4aSRandall Stewart */ 4133f8829a4aSRandall Stewart /* 4134f8829a4aSRandall Stewart * Now perform the actual SACK handling: 1) Verify that it is not an 4135f8829a4aSRandall Stewart * old sack, if so discard. 2) If there is nothing left in the send 4136f8829a4aSRandall Stewart * queue (cum-ack is equal to last acked) then you have a duplicate 4137f8829a4aSRandall Stewart * too, update any rwnd change and verify no timers are running. 4138f8829a4aSRandall Stewart * then return. 3) Process any new consequtive data i.e. cum-ack 4139f8829a4aSRandall Stewart * moved process these first and note that it moved. 4) Process any 4140f8829a4aSRandall Stewart * sack blocks. 5) Drop any acked from the queue. 6) Check for any 4141f8829a4aSRandall Stewart * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left, 4142f8829a4aSRandall Stewart * sync up flightsizes and things, stop all timers and also check 4143f8829a4aSRandall Stewart * for shutdown_pending state. If so then go ahead and send off the 4144f8829a4aSRandall Stewart * shutdown. If in shutdown recv, send off the shutdown-ack and 4145f8829a4aSRandall Stewart * start that timer, Ret. 9) Strike any non-acked things and do FR 4146f8829a4aSRandall Stewart * procedure if needed being sure to set the FR flag. 10) Do pr-sctp 4147f8829a4aSRandall Stewart * procedures. 11) Apply any FR penalties. 12) Assure we will SACK 4148f8829a4aSRandall Stewart * if in shutdown_recv state. 4149f8829a4aSRandall Stewart */ 4150f8829a4aSRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 4151f8829a4aSRandall Stewart /* CMT DAC algo */ 4152f8829a4aSRandall Stewart this_sack_lowest_newack = 0; 4153f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_slowpath_sack); 4154cd554309SMichael Tuexen last_tsn = cum_ack; 4155cd554309SMichael Tuexen cmt_dac_flag = flags & SCTP_SACK_CMT_DAC; 415618e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS 415718e198d3SRandall Stewart stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack; 415818e198d3SRandall Stewart stcb->asoc.cumack_log_at++; 415918e198d3SRandall Stewart if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) { 416018e198d3SRandall Stewart stcb->asoc.cumack_log_at = 0; 416118e198d3SRandall Stewart } 416218e198d3SRandall Stewart #endif 4163d06c82f1SRandall Stewart a_rwnd = rwnd; 4164f8829a4aSRandall Stewart 4165cd554309SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) { 4166cd554309SMichael Tuexen sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack, 4167cd554309SMichael Tuexen rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd); 4168cd554309SMichael Tuexen } 41695e54f665SRandall Stewart old_rwnd = stcb->asoc.peers_rwnd; 4170b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) { 4171c4739e2fSRandall Stewart sctp_misc_ints(SCTP_THRESHOLD_CLEAR, 4172c4739e2fSRandall Stewart stcb->asoc.overall_error_count, 4173c4739e2fSRandall Stewart 0, 4174c4739e2fSRandall Stewart SCTP_FROM_SCTP_INDATA, 4175c4739e2fSRandall Stewart __LINE__); 4176c4739e2fSRandall Stewart } 4177f8829a4aSRandall Stewart stcb->asoc.overall_error_count = 0; 4178f8829a4aSRandall Stewart asoc = &stcb->asoc; 4179b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 4180f8829a4aSRandall Stewart sctp_log_sack(asoc->last_acked_seq, 4181f8829a4aSRandall Stewart cum_ack, 4182f8829a4aSRandall Stewart 0, 4183f8829a4aSRandall Stewart num_seg, 4184f8829a4aSRandall Stewart num_dup, 4185f8829a4aSRandall Stewart SCTP_LOG_NEW_SACK); 418680fefe0aSRandall Stewart } 4187ca85e948SMichael Tuexen if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) { 4188cd554309SMichael Tuexen uint16_t i; 4189458303daSRandall Stewart uint32_t *dupdata, dblock; 4190f8829a4aSRandall Stewart 4191cd554309SMichael Tuexen for (i = 0; i < num_dup; i++) { 4192cd554309SMichael Tuexen dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t), 4193458303daSRandall Stewart sizeof(uint32_t), (uint8_t *) & dblock); 4194cd554309SMichael Tuexen if (dupdata == NULL) { 4195458303daSRandall Stewart break; 4196458303daSRandall Stewart } 4197cd554309SMichael Tuexen sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED); 4198f8829a4aSRandall Stewart } 4199f8829a4aSRandall Stewart } 4200b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) { 4201c105859eSRandall Stewart /* reality check */ 4202c105859eSRandall Stewart if (!TAILQ_EMPTY(&asoc->sent_queue)) { 4203c105859eSRandall Stewart tp1 = TAILQ_LAST(&asoc->sent_queue, 4204c105859eSRandall Stewart sctpchunk_listhead); 4205c105859eSRandall Stewart send_s = tp1->rec.data.TSN_seq + 1; 4206c105859eSRandall Stewart } else { 4207b5c16493SMichael Tuexen tp1 = NULL; 4208c105859eSRandall Stewart send_s = asoc->sending_seq; 4209c105859eSRandall Stewart } 421020b07a4dSMichael Tuexen if (SCTP_TSN_GE(cum_ack, send_s)) { 4211ff1ffd74SMichael Tuexen struct mbuf *op_err; 4212ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 4213c105859eSRandall Stewart 4214f8829a4aSRandall Stewart /* 4215f8829a4aSRandall Stewart * no way, we have not even sent this TSN out yet. 4216f8829a4aSRandall Stewart * Peer is hopelessly messed up with us. 4217f8829a4aSRandall Stewart */ 4218cd3fd531SMichael Tuexen SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n", 4219b5c16493SMichael Tuexen cum_ack, send_s); 4220b5c16493SMichael Tuexen if (tp1) { 4221cd3fd531SMichael Tuexen SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n", 4222dd294dceSMichael Tuexen tp1->rec.data.TSN_seq, (void *)tp1); 4223b5c16493SMichael Tuexen } 4224f8829a4aSRandall Stewart hopeless_peer: 4225f8829a4aSRandall Stewart *abort_now = 1; 4226f8829a4aSRandall Stewart /* XXX */ 422755f8a4bbSMichael Tuexen snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x", 4228ff1ffd74SMichael Tuexen cum_ack, send_s); 4229ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 4230b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_27; 4231ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 4232f8829a4aSRandall Stewart return; 4233f8829a4aSRandall Stewart } 4234f8829a4aSRandall Stewart } 4235f8829a4aSRandall Stewart /**********************/ 4236f8829a4aSRandall Stewart /* 1) check the range */ 4237f8829a4aSRandall Stewart /**********************/ 423820b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) { 4239f8829a4aSRandall Stewart /* acking something behind */ 4240f8829a4aSRandall Stewart return; 4241f8829a4aSRandall Stewart } 4242f8829a4aSRandall Stewart /* update the Rwnd of the peer */ 4243f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->sent_queue) && 4244f8829a4aSRandall Stewart TAILQ_EMPTY(&asoc->send_queue) && 4245cd554309SMichael Tuexen (asoc->stream_queue_cnt == 0)) { 4246f8829a4aSRandall Stewart /* nothing left on send/sent and strmq */ 4247b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { 4248f8829a4aSRandall Stewart sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK, 4249f8829a4aSRandall Stewart asoc->peers_rwnd, 0, 0, a_rwnd); 425080fefe0aSRandall Stewart } 4251f8829a4aSRandall Stewart asoc->peers_rwnd = a_rwnd; 4252f8829a4aSRandall Stewart if (asoc->sent_queue_retran_cnt) { 4253f8829a4aSRandall Stewart asoc->sent_queue_retran_cnt = 0; 4254f8829a4aSRandall Stewart } 4255f8829a4aSRandall Stewart if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4256f8829a4aSRandall Stewart /* SWS sender side engages */ 4257f8829a4aSRandall Stewart asoc->peers_rwnd = 0; 4258f8829a4aSRandall Stewart } 4259f8829a4aSRandall Stewart /* stop any timers */ 4260f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4261f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 4262b7d130beSMichael Tuexen stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28); 4263f8829a4aSRandall Stewart net->partial_bytes_acked = 0; 4264f8829a4aSRandall Stewart net->flight_size = 0; 4265f8829a4aSRandall Stewart } 4266f8829a4aSRandall Stewart asoc->total_flight = 0; 4267f8829a4aSRandall Stewart asoc->total_flight_count = 0; 4268f8829a4aSRandall Stewart return; 4269f8829a4aSRandall Stewart } 4270f8829a4aSRandall Stewart /* 4271f8829a4aSRandall Stewart * We init netAckSz and netAckSz2 to 0. These are used to track 2 4272f8829a4aSRandall Stewart * things. The total byte count acked is tracked in netAckSz AND 4273f8829a4aSRandall Stewart * netAck2 is used to track the total bytes acked that are un- 4274f8829a4aSRandall Stewart * amibguious and were never retransmitted. We track these on a per 4275f8829a4aSRandall Stewart * destination address basis. 4276f8829a4aSRandall Stewart */ 4277f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4278a21779f0SRandall Stewart if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) { 4279a21779f0SRandall Stewart /* Drag along the window_tsn for cwr's */ 4280a21779f0SRandall Stewart net->cwr_window_tsn = cum_ack; 4281a21779f0SRandall Stewart } 4282f8829a4aSRandall Stewart net->prev_cwnd = net->cwnd; 4283f8829a4aSRandall Stewart net->net_ack = 0; 4284f8829a4aSRandall Stewart net->net_ack2 = 0; 4285f8829a4aSRandall Stewart 4286f8829a4aSRandall Stewart /* 428742551e99SRandall Stewart * CMT: Reset CUC and Fast recovery algo variables before 428842551e99SRandall Stewart * SACK processing 4289f8829a4aSRandall Stewart */ 4290f8829a4aSRandall Stewart net->new_pseudo_cumack = 0; 4291f8829a4aSRandall Stewart net->will_exit_fast_recovery = 0; 4292299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) { 4293299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net); 4294299108c5SRandall Stewart } 4295f8829a4aSRandall Stewart } 4296f8829a4aSRandall Stewart /* process the new consecutive TSN first */ 42974a9ef3f8SMichael Tuexen TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 429820b07a4dSMichael Tuexen if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) { 4299f8829a4aSRandall Stewart if (tp1->sent != SCTP_DATAGRAM_UNSENT) { 4300f8829a4aSRandall Stewart accum_moved = 1; 4301f8829a4aSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_ACKED) { 4302f8829a4aSRandall Stewart /* 4303f8829a4aSRandall Stewart * If it is less than ACKED, it is 4304f8829a4aSRandall Stewart * now no-longer in flight. Higher 4305f8829a4aSRandall Stewart * values may occur during marking 4306f8829a4aSRandall Stewart */ 4307f8829a4aSRandall Stewart if ((tp1->whoTo->dest_state & 4308f8829a4aSRandall Stewart SCTP_ADDR_UNCONFIRMED) && 4309f8829a4aSRandall Stewart (tp1->snd_count < 2)) { 4310f8829a4aSRandall Stewart /* 4311f8829a4aSRandall Stewart * If there was no retran 4312f8829a4aSRandall Stewart * and the address is 4313f8829a4aSRandall Stewart * un-confirmed and we sent 4314f8829a4aSRandall Stewart * there and are now 4315f8829a4aSRandall Stewart * sacked.. its confirmed, 4316f8829a4aSRandall Stewart * mark it so. 4317f8829a4aSRandall Stewart */ 4318f8829a4aSRandall Stewart tp1->whoTo->dest_state &= 4319f8829a4aSRandall Stewart ~SCTP_ADDR_UNCONFIRMED; 4320f8829a4aSRandall Stewart } 4321c105859eSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 4322b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 4323c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA, 4324a5d547adSRandall Stewart tp1->whoTo->flight_size, 4325a5d547adSRandall Stewart tp1->book_size, 4326c105859eSRandall Stewart (uintptr_t) tp1->whoTo, 4327a5d547adSRandall Stewart tp1->rec.data.TSN_seq); 432880fefe0aSRandall Stewart } 4329c105859eSRandall Stewart sctp_flight_size_decrease(tp1); 4330c105859eSRandall Stewart sctp_total_flight_decrease(stcb, tp1); 4331299108c5SRandall Stewart if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) { 4332299108c5SRandall Stewart (*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo, 4333299108c5SRandall Stewart tp1); 4334299108c5SRandall Stewart } 4335f8829a4aSRandall Stewart } 4336f8829a4aSRandall Stewart tp1->whoTo->net_ack += tp1->send_size; 4337f8829a4aSRandall Stewart 4338f8829a4aSRandall Stewart /* CMT SFR and DAC algos */ 4339f8829a4aSRandall Stewart this_sack_lowest_newack = tp1->rec.data.TSN_seq; 4340f8829a4aSRandall Stewart tp1->whoTo->saw_newack = 1; 4341f8829a4aSRandall Stewart 4342f8829a4aSRandall Stewart if (tp1->snd_count < 2) { 4343f8829a4aSRandall Stewart /* 4344f8829a4aSRandall Stewart * True non-retransmited 4345f8829a4aSRandall Stewart * chunk 4346f8829a4aSRandall Stewart */ 4347f8829a4aSRandall Stewart tp1->whoTo->net_ack2 += 4348f8829a4aSRandall Stewart tp1->send_size; 4349f8829a4aSRandall Stewart 4350f8829a4aSRandall Stewart /* update RTO too? */ 4351f8829a4aSRandall Stewart if (tp1->do_rtt) { 4352f79aab18SRandall Stewart if (rto_ok) { 4353f8829a4aSRandall Stewart tp1->whoTo->RTO = 4354f8829a4aSRandall Stewart sctp_calculate_rto(stcb, 4355f8829a4aSRandall Stewart asoc, tp1->whoTo, 435618e198d3SRandall Stewart &tp1->sent_rcv_time, 4357899288aeSRandall Stewart sctp_align_safe_nocopy, 4358f79aab18SRandall Stewart SCTP_RTT_FROM_DATA); 4359f79aab18SRandall Stewart rto_ok = 0; 4360f79aab18SRandall Stewart } 4361f79aab18SRandall Stewart if (tp1->whoTo->rto_needed == 0) { 4362f79aab18SRandall Stewart tp1->whoTo->rto_needed = 1; 4363f79aab18SRandall Stewart } 4364f8829a4aSRandall Stewart tp1->do_rtt = 0; 4365f8829a4aSRandall Stewart } 4366f8829a4aSRandall Stewart } 4367f8829a4aSRandall Stewart /* 4368f8829a4aSRandall Stewart * CMT: CUCv2 algorithm. From the 4369f8829a4aSRandall Stewart * cumack'd TSNs, for each TSN being 4370f8829a4aSRandall Stewart * acked for the first time, set the 4371f8829a4aSRandall Stewart * following variables for the 4372f8829a4aSRandall Stewart * corresp destination. 4373f8829a4aSRandall Stewart * new_pseudo_cumack will trigger a 4374f8829a4aSRandall Stewart * cwnd update. 4375f8829a4aSRandall Stewart * find_(rtx_)pseudo_cumack will 4376f8829a4aSRandall Stewart * trigger search for the next 4377f8829a4aSRandall Stewart * expected (rtx-)pseudo-cumack. 4378f8829a4aSRandall Stewart */ 4379f8829a4aSRandall Stewart tp1->whoTo->new_pseudo_cumack = 1; 4380f8829a4aSRandall Stewart tp1->whoTo->find_pseudo_cumack = 1; 4381f8829a4aSRandall Stewart tp1->whoTo->find_rtx_pseudo_cumack = 1; 4382f8829a4aSRandall Stewart 4383f8829a4aSRandall Stewart 4384b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 4385f8829a4aSRandall Stewart sctp_log_sack(asoc->last_acked_seq, 4386f8829a4aSRandall Stewart cum_ack, 4387f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 4388f8829a4aSRandall Stewart 0, 4389f8829a4aSRandall Stewart 0, 4390f8829a4aSRandall Stewart SCTP_LOG_TSN_ACKED); 439180fefe0aSRandall Stewart } 4392b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) { 4393f8829a4aSRandall Stewart sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK); 439480fefe0aSRandall Stewart } 4395f8829a4aSRandall Stewart } 4396f8829a4aSRandall Stewart if (tp1->sent == SCTP_DATAGRAM_RESEND) { 4397f8829a4aSRandall Stewart sctp_ucount_decr(asoc->sent_queue_retran_cnt); 4398f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED 4399f8829a4aSRandall Stewart sctp_audit_log(0xB3, 4400f8829a4aSRandall Stewart (asoc->sent_queue_retran_cnt & 0x000000ff)); 4401f8829a4aSRandall Stewart #endif 4402f8829a4aSRandall Stewart } 440342551e99SRandall Stewart if (tp1->rec.data.chunk_was_revoked) { 440442551e99SRandall Stewart /* deflate the cwnd */ 440542551e99SRandall Stewart tp1->whoTo->cwnd -= tp1->book_size; 440642551e99SRandall Stewart tp1->rec.data.chunk_was_revoked = 0; 440742551e99SRandall Stewart } 4408325c8c46SMichael Tuexen if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) { 4409f8829a4aSRandall Stewart tp1->sent = SCTP_DATAGRAM_ACKED; 4410f8829a4aSRandall Stewart } 4411325c8c46SMichael Tuexen } 4412f8829a4aSRandall Stewart } else { 4413f8829a4aSRandall Stewart break; 4414f8829a4aSRandall Stewart } 4415f8829a4aSRandall Stewart } 4416f8829a4aSRandall Stewart biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn; 4417f8829a4aSRandall Stewart /* always set this up to cum-ack */ 4418f8829a4aSRandall Stewart asoc->this_sack_highest_gap = last_tsn; 4419f8829a4aSRandall Stewart 4420cd554309SMichael Tuexen if ((num_seg > 0) || (num_nr_seg > 0)) { 4421f8829a4aSRandall Stewart 4422f8829a4aSRandall Stewart /* 4423f8829a4aSRandall Stewart * CMT: SFR algo (and HTNA) - this_sack_highest_newack has 4424f8829a4aSRandall Stewart * to be greater than the cumack. Also reset saw_newack to 0 4425f8829a4aSRandall Stewart * for all dests. 4426f8829a4aSRandall Stewart */ 4427f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4428f8829a4aSRandall Stewart net->saw_newack = 0; 4429f8829a4aSRandall Stewart net->this_sack_highest_newack = last_tsn; 4430f8829a4aSRandall Stewart } 4431f8829a4aSRandall Stewart 4432f8829a4aSRandall Stewart /* 4433f8829a4aSRandall Stewart * thisSackHighestGap will increase while handling NEW 4434f8829a4aSRandall Stewart * segments this_sack_highest_newack will increase while 4435f8829a4aSRandall Stewart * handling NEWLY ACKED chunks. this_sack_lowest_newack is 4436f8829a4aSRandall Stewart * used for CMT DAC algo. saw_newack will also change. 4437f8829a4aSRandall Stewart */ 4438cd554309SMichael Tuexen if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked, 4439cd554309SMichael Tuexen &biggest_tsn_newly_acked, &this_sack_lowest_newack, 44407215cc1bSMichael Tuexen num_seg, num_nr_seg, &rto_ok)) { 4441cd554309SMichael Tuexen wake_him++; 4442cd554309SMichael Tuexen } 4443b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) { 4444f8829a4aSRandall Stewart /* 4445f8829a4aSRandall Stewart * validate the biggest_tsn_acked in the gap acks if 4446f8829a4aSRandall Stewart * strict adherence is wanted. 4447f8829a4aSRandall Stewart */ 444820b07a4dSMichael Tuexen if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) { 4449f8829a4aSRandall Stewart /* 4450f8829a4aSRandall Stewart * peer is either confused or we are under 4451f8829a4aSRandall Stewart * attack. We must abort. 4452f8829a4aSRandall Stewart */ 4453cd3fd531SMichael Tuexen SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n", 4454cd3fd531SMichael Tuexen biggest_tsn_acked, send_s); 4455f8829a4aSRandall Stewart goto hopeless_peer; 4456f8829a4aSRandall Stewart } 4457f8829a4aSRandall Stewart } 4458f8829a4aSRandall Stewart } 4459f8829a4aSRandall Stewart /*******************************************/ 4460f8829a4aSRandall Stewart /* cancel ALL T3-send timer if accum moved */ 4461f8829a4aSRandall Stewart /*******************************************/ 44627c99d56fSMichael Tuexen if (asoc->sctp_cmt_on_off > 0) { 4463f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4464f8829a4aSRandall Stewart if (net->new_pseudo_cumack) 4465f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 4466a5d547adSRandall Stewart stcb, net, 4467b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_29); 4468f8829a4aSRandall Stewart 4469f8829a4aSRandall Stewart } 4470f8829a4aSRandall Stewart } else { 4471f8829a4aSRandall Stewart if (accum_moved) { 4472f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4473f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 4474b7d130beSMichael Tuexen stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30); 4475f8829a4aSRandall Stewart } 4476f8829a4aSRandall Stewart } 4477f8829a4aSRandall Stewart } 4478f8829a4aSRandall Stewart /********************************************/ 4479d9c5cfeaSMichael Tuexen /* drop the acked chunks from the sentqueue */ 4480f8829a4aSRandall Stewart /********************************************/ 4481f8829a4aSRandall Stewart asoc->last_acked_seq = cum_ack; 4482f8829a4aSRandall Stewart 44837c99d56fSMichael Tuexen TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) { 448420b07a4dSMichael Tuexen if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) { 4485f8829a4aSRandall Stewart break; 4486f8829a4aSRandall Stewart } 4487325c8c46SMichael Tuexen if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) { 4488a7ad6026SMichael Tuexen if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) { 4489a7ad6026SMichael Tuexen asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--; 4490a7ad6026SMichael Tuexen #ifdef INVARIANTS 4491a7ad6026SMichael Tuexen } else { 4492a7ad6026SMichael Tuexen panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number); 4493a7ad6026SMichael Tuexen #endif 4494a7ad6026SMichael Tuexen } 4495f8829a4aSRandall Stewart } 4496f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next); 44970ddb4299SMichael Tuexen if (PR_SCTP_ENABLED(tp1->flags)) { 4498f8829a4aSRandall Stewart if (asoc->pr_sctp_cnt != 0) 4499f8829a4aSRandall Stewart asoc->pr_sctp_cnt--; 4500f8829a4aSRandall Stewart } 45017c99d56fSMichael Tuexen asoc->sent_queue_cnt--; 4502f8829a4aSRandall Stewart if (tp1->data) { 450304ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 4504f8829a4aSRandall Stewart sctp_free_bufspace(stcb, asoc, tp1, 1); 4505f8829a4aSRandall Stewart sctp_m_freem(tp1->data); 45067c99d56fSMichael Tuexen tp1->data = NULL; 4507dd973b0eSMichael Tuexen if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(tp1->flags)) { 4508f8829a4aSRandall Stewart asoc->sent_queue_cnt_removeable--; 4509f8829a4aSRandall Stewart } 4510f8829a4aSRandall Stewart } 4511b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) { 4512f8829a4aSRandall Stewart sctp_log_sack(asoc->last_acked_seq, 4513f8829a4aSRandall Stewart cum_ack, 4514f8829a4aSRandall Stewart tp1->rec.data.TSN_seq, 4515f8829a4aSRandall Stewart 0, 4516f8829a4aSRandall Stewart 0, 4517f8829a4aSRandall Stewart SCTP_LOG_FREE_SENT); 451880fefe0aSRandall Stewart } 4519689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED); 4520f8829a4aSRandall Stewart wake_him++; 45217c99d56fSMichael Tuexen } 45227c99d56fSMichael Tuexen if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) { 45237c99d56fSMichael Tuexen #ifdef INVARIANTS 45247c99d56fSMichael Tuexen panic("Warning flight size is postive and should be 0"); 45257c99d56fSMichael Tuexen #else 45267c99d56fSMichael Tuexen SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n", 45277c99d56fSMichael Tuexen asoc->total_flight); 45287c99d56fSMichael Tuexen #endif 45297c99d56fSMichael Tuexen asoc->total_flight = 0; 45307c99d56fSMichael Tuexen } 453104ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 4532f8829a4aSRandall Stewart if ((wake_him) && (stcb->sctp_socket)) { 4533ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4534ceaad40aSRandall Stewart struct socket *so; 4535ceaad40aSRandall Stewart 4536ceaad40aSRandall Stewart #endif 4537f8829a4aSRandall Stewart SOCKBUF_LOCK(&stcb->sctp_socket->so_snd); 4538b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) { 45397215cc1bSMichael Tuexen sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK); 454080fefe0aSRandall Stewart } 4541ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4542ceaad40aSRandall Stewart so = SCTP_INP_SO(stcb->sctp_ep); 4543ceaad40aSRandall Stewart atomic_add_int(&stcb->asoc.refcnt, 1); 4544ceaad40aSRandall Stewart SCTP_TCB_UNLOCK(stcb); 4545ceaad40aSRandall Stewart SCTP_SOCKET_LOCK(so, 1); 4546ceaad40aSRandall Stewart SCTP_TCB_LOCK(stcb); 4547ceaad40aSRandall Stewart atomic_subtract_int(&stcb->asoc.refcnt, 1); 4548ceaad40aSRandall Stewart if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) { 4549ceaad40aSRandall Stewart /* assoc was freed while we were unlocked */ 4550ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 4551ceaad40aSRandall Stewart return; 4552ceaad40aSRandall Stewart } 4553ceaad40aSRandall Stewart #endif 4554f8829a4aSRandall Stewart sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket); 4555ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING) 4556ceaad40aSRandall Stewart SCTP_SOCKET_UNLOCK(so, 1); 4557ceaad40aSRandall Stewart #endif 4558f8829a4aSRandall Stewart } else { 4559b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) { 45607215cc1bSMichael Tuexen sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK); 456180fefe0aSRandall Stewart } 4562f8829a4aSRandall Stewart } 4563f8829a4aSRandall Stewart 456442551e99SRandall Stewart if (asoc->fast_retran_loss_recovery && accum_moved) { 456520b07a4dSMichael Tuexen if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) { 4566f8829a4aSRandall Stewart /* Setup so we will exit RFC2582 fast recovery */ 4567f8829a4aSRandall Stewart will_exit_fast_recovery = 1; 4568f8829a4aSRandall Stewart } 4569f8829a4aSRandall Stewart } 4570f8829a4aSRandall Stewart /* 4571f8829a4aSRandall Stewart * Check for revoked fragments: 4572f8829a4aSRandall Stewart * 4573f8829a4aSRandall Stewart * if Previous sack - Had no frags then we can't have any revoked if 4574f8829a4aSRandall Stewart * Previous sack - Had frag's then - If we now have frags aka 4575f8829a4aSRandall Stewart * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked 4576f8829a4aSRandall Stewart * some of them. else - The peer revoked all ACKED fragments, since 4577f8829a4aSRandall Stewart * we had some before and now we have NONE. 4578f8829a4aSRandall Stewart */ 4579f8829a4aSRandall Stewart 4580d9c5cfeaSMichael Tuexen if (num_seg) { 4581c105859eSRandall Stewart sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked); 4582d9c5cfeaSMichael Tuexen asoc->saw_sack_with_frags = 1; 4583d9c5cfeaSMichael Tuexen } else if (asoc->saw_sack_with_frags) { 4584f8829a4aSRandall Stewart int cnt_revoked = 0; 4585f8829a4aSRandall Stewart 4586f8829a4aSRandall Stewart /* Peer revoked all dg's marked or acked */ 4587f8829a4aSRandall Stewart TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 4588b5c16493SMichael Tuexen if (tp1->sent == SCTP_DATAGRAM_ACKED) { 4589f8829a4aSRandall Stewart tp1->sent = SCTP_DATAGRAM_SENT; 4590b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) { 4591c105859eSRandall Stewart sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE, 4592c105859eSRandall Stewart tp1->whoTo->flight_size, 4593c105859eSRandall Stewart tp1->book_size, 4594c105859eSRandall Stewart (uintptr_t) tp1->whoTo, 4595c105859eSRandall Stewart tp1->rec.data.TSN_seq); 459680fefe0aSRandall Stewart } 4597c105859eSRandall Stewart sctp_flight_size_increase(tp1); 4598c105859eSRandall Stewart sctp_total_flight_increase(stcb, tp1); 4599a5d547adSRandall Stewart tp1->rec.data.chunk_was_revoked = 1; 460042551e99SRandall Stewart /* 460142551e99SRandall Stewart * To ensure that this increase in 46024a9ef3f8SMichael Tuexen * flightsize, which is artificial, does not 46034a9ef3f8SMichael Tuexen * throttle the sender, we also increase the 46044a9ef3f8SMichael Tuexen * cwnd artificially. 460542551e99SRandall Stewart */ 460642551e99SRandall Stewart tp1->whoTo->cwnd += tp1->book_size; 4607f8829a4aSRandall Stewart cnt_revoked++; 4608f8829a4aSRandall Stewart } 4609f8829a4aSRandall Stewart } 4610f8829a4aSRandall Stewart if (cnt_revoked) { 4611f8829a4aSRandall Stewart reneged_all = 1; 4612f8829a4aSRandall Stewart } 4613f8829a4aSRandall Stewart asoc->saw_sack_with_frags = 0; 4614f8829a4aSRandall Stewart } 4615d9c5cfeaSMichael Tuexen if (num_nr_seg > 0) 4616d9c5cfeaSMichael Tuexen asoc->saw_sack_with_nr_frags = 1; 4617f8829a4aSRandall Stewart else 4618d9c5cfeaSMichael Tuexen asoc->saw_sack_with_nr_frags = 0; 4619f8829a4aSRandall Stewart 4620b54d3a6cSRandall Stewart /* JRS - Use the congestion control given in the CC module */ 4621ca85e948SMichael Tuexen if (ecne_seen == 0) { 4622ca85e948SMichael Tuexen TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4623ca85e948SMichael Tuexen if (net->net_ack2 > 0) { 4624ca85e948SMichael Tuexen /* 4625ca85e948SMichael Tuexen * Karn's rule applies to clearing error 4626ca85e948SMichael Tuexen * count, this is optional. 4627ca85e948SMichael Tuexen */ 4628ca85e948SMichael Tuexen net->error_count = 0; 4629ca85e948SMichael Tuexen if (!(net->dest_state & SCTP_ADDR_REACHABLE)) { 4630ca85e948SMichael Tuexen /* addr came good */ 4631ca85e948SMichael Tuexen net->dest_state |= SCTP_ADDR_REACHABLE; 4632ca85e948SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb, 46334b1f78e1SMichael Tuexen 0, (void *)net, SCTP_SO_NOT_LOCKED); 4634ca85e948SMichael Tuexen } 4635ca85e948SMichael Tuexen if (net == stcb->asoc.primary_destination) { 4636ca85e948SMichael Tuexen if (stcb->asoc.alternate) { 4637ca85e948SMichael Tuexen /* 4638ca85e948SMichael Tuexen * release the alternate, 4639ca85e948SMichael Tuexen * primary is good 4640ca85e948SMichael Tuexen */ 4641ca85e948SMichael Tuexen sctp_free_remote_addr(stcb->asoc.alternate); 4642ca85e948SMichael Tuexen stcb->asoc.alternate = NULL; 4643ca85e948SMichael Tuexen } 4644ca85e948SMichael Tuexen } 4645ca85e948SMichael Tuexen if (net->dest_state & SCTP_ADDR_PF) { 4646ca85e948SMichael Tuexen net->dest_state &= ~SCTP_ADDR_PF; 4647b7d130beSMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, 4648b7d130beSMichael Tuexen stcb->sctp_ep, stcb, net, 4649b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_31); 4650ca85e948SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net); 4651ca85e948SMichael Tuexen asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net); 4652ca85e948SMichael Tuexen /* Done with this net */ 4653ca85e948SMichael Tuexen net->net_ack = 0; 4654ca85e948SMichael Tuexen } 4655ca85e948SMichael Tuexen /* restore any doubled timers */ 4656ca85e948SMichael Tuexen net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv; 4657ca85e948SMichael Tuexen if (net->RTO < stcb->asoc.minrto) { 4658ca85e948SMichael Tuexen net->RTO = stcb->asoc.minrto; 4659ca85e948SMichael Tuexen } 4660ca85e948SMichael Tuexen if (net->RTO > stcb->asoc.maxrto) { 4661ca85e948SMichael Tuexen net->RTO = stcb->asoc.maxrto; 4662ca85e948SMichael Tuexen } 4663ca85e948SMichael Tuexen } 4664ca85e948SMichael Tuexen } 4665b54d3a6cSRandall Stewart asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery); 4666ca85e948SMichael Tuexen } 4667f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->sent_queue)) { 4668f8829a4aSRandall Stewart /* nothing left in-flight */ 4669f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4670f8829a4aSRandall Stewart /* stop all timers */ 4671f8829a4aSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 4672b7d130beSMichael Tuexen stcb, net, 4673b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_32); 4674f8829a4aSRandall Stewart net->flight_size = 0; 4675f8829a4aSRandall Stewart net->partial_bytes_acked = 0; 4676f8829a4aSRandall Stewart } 4677f8829a4aSRandall Stewart asoc->total_flight = 0; 4678f8829a4aSRandall Stewart asoc->total_flight_count = 0; 4679f8829a4aSRandall Stewart } 4680f8829a4aSRandall Stewart /**********************************/ 4681f8829a4aSRandall Stewart /* Now what about shutdown issues */ 4682f8829a4aSRandall Stewart /**********************************/ 4683f8829a4aSRandall Stewart if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) { 4684f8829a4aSRandall Stewart /* nothing left on sendqueue.. consider done */ 4685b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { 4686f8829a4aSRandall Stewart sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK, 4687f8829a4aSRandall Stewart asoc->peers_rwnd, 0, 0, a_rwnd); 468880fefe0aSRandall Stewart } 4689f8829a4aSRandall Stewart asoc->peers_rwnd = a_rwnd; 4690f8829a4aSRandall Stewart if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4691f8829a4aSRandall Stewart /* SWS sender side engages */ 4692f8829a4aSRandall Stewart asoc->peers_rwnd = 0; 4693f8829a4aSRandall Stewart } 4694f8829a4aSRandall Stewart /* clean up */ 4695f8829a4aSRandall Stewart if ((asoc->stream_queue_cnt == 1) && 4696f8829a4aSRandall Stewart ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) || 4697f8829a4aSRandall Stewart (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) && 4698f8829a4aSRandall Stewart (asoc->locked_on_sending) 4699f8829a4aSRandall Stewart ) { 4700f8829a4aSRandall Stewart struct sctp_stream_queue_pending *sp; 4701f8829a4aSRandall Stewart 4702f8829a4aSRandall Stewart /* 4703f8829a4aSRandall Stewart * I may be in a state where we got all across.. but 4704f8829a4aSRandall Stewart * cannot write more due to a shutdown... we abort 4705f8829a4aSRandall Stewart * since the user did not indicate EOR in this case. 4706f8829a4aSRandall Stewart */ 4707f8829a4aSRandall Stewart sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue), 4708f8829a4aSRandall Stewart sctp_streamhead); 47092afb3e84SRandall Stewart if ((sp) && (sp->length == 0)) { 4710f8829a4aSRandall Stewart asoc->locked_on_sending = NULL; 47112afb3e84SRandall Stewart if (sp->msg_is_complete) { 4712f8829a4aSRandall Stewart asoc->stream_queue_cnt--; 47132afb3e84SRandall Stewart } else { 47142afb3e84SRandall Stewart asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT; 47152afb3e84SRandall Stewart asoc->stream_queue_cnt--; 47162afb3e84SRandall Stewart } 4717f8829a4aSRandall Stewart } 4718f8829a4aSRandall Stewart } 4719f8829a4aSRandall Stewart if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) && 4720f8829a4aSRandall Stewart (asoc->stream_queue_cnt == 0)) { 4721f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) { 4722f8829a4aSRandall Stewart /* Need to abort here */ 4723ff1ffd74SMichael Tuexen struct mbuf *op_err; 4724f8829a4aSRandall Stewart 4725f8829a4aSRandall Stewart abort_out_now: 4726f8829a4aSRandall Stewart *abort_now = 1; 4727f8829a4aSRandall Stewart /* XXX */ 4728ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); 4729b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33; 4730ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 4731f8829a4aSRandall Stewart return; 4732f8829a4aSRandall Stewart } else { 4733ca85e948SMichael Tuexen struct sctp_nets *netp; 4734ca85e948SMichael Tuexen 4735f42a358aSRandall Stewart if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || 4736f42a358aSRandall Stewart (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { 4737f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 4738f42a358aSRandall Stewart } 4739c4739e2fSRandall Stewart SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); 4740b201f536SRandall Stewart SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 4741f8829a4aSRandall Stewart sctp_stop_timers_for_shutdown(stcb); 4742c39cfa1fSMichael Tuexen if (asoc->alternate) { 4743c39cfa1fSMichael Tuexen netp = asoc->alternate; 4744c39cfa1fSMichael Tuexen } else { 4745c39cfa1fSMichael Tuexen netp = asoc->primary_destination; 4746c39cfa1fSMichael Tuexen } 4747ca85e948SMichael Tuexen sctp_send_shutdown(stcb, netp); 4748f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, 4749ca85e948SMichael Tuexen stcb->sctp_ep, stcb, netp); 4750f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, 4751ca85e948SMichael Tuexen stcb->sctp_ep, stcb, netp); 4752f8829a4aSRandall Stewart } 4753f8829a4aSRandall Stewart return; 4754f8829a4aSRandall Stewart } else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) && 4755f8829a4aSRandall Stewart (asoc->stream_queue_cnt == 0)) { 4756ca85e948SMichael Tuexen struct sctp_nets *netp; 4757ca85e948SMichael Tuexen 4758f8829a4aSRandall Stewart if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) { 4759f8829a4aSRandall Stewart goto abort_out_now; 4760f8829a4aSRandall Stewart } 4761f8829a4aSRandall Stewart SCTP_STAT_DECR_GAUGE32(sctps_currestab); 4762c4739e2fSRandall Stewart SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT); 4763b201f536SRandall Stewart SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); 476412af6654SMichael Tuexen sctp_stop_timers_for_shutdown(stcb); 4765c39cfa1fSMichael Tuexen if (asoc->alternate) { 4766c39cfa1fSMichael Tuexen netp = asoc->alternate; 4767c39cfa1fSMichael Tuexen } else { 4768c39cfa1fSMichael Tuexen netp = asoc->primary_destination; 4769c39cfa1fSMichael Tuexen } 4770c39cfa1fSMichael Tuexen sctp_send_shutdown_ack(stcb, netp); 4771f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, 4772ca85e948SMichael Tuexen stcb->sctp_ep, stcb, netp); 4773f8829a4aSRandall Stewart return; 4774f8829a4aSRandall Stewart } 4775f8829a4aSRandall Stewart } 4776f8829a4aSRandall Stewart /* 4777f8829a4aSRandall Stewart * Now here we are going to recycle net_ack for a different use... 4778f8829a4aSRandall Stewart * HEADS UP. 4779f8829a4aSRandall Stewart */ 4780f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4781f8829a4aSRandall Stewart net->net_ack = 0; 4782f8829a4aSRandall Stewart } 4783f8829a4aSRandall Stewart 4784f8829a4aSRandall Stewart /* 4785f8829a4aSRandall Stewart * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking 4786f8829a4aSRandall Stewart * to be done. Setting this_sack_lowest_newack to the cum_ack will 4787f8829a4aSRandall Stewart * automatically ensure that. 4788f8829a4aSRandall Stewart */ 47897c99d56fSMichael Tuexen if ((asoc->sctp_cmt_on_off > 0) && 479020083c2eSMichael Tuexen SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && 479120083c2eSMichael Tuexen (cmt_dac_flag == 0)) { 4792f8829a4aSRandall Stewart this_sack_lowest_newack = cum_ack; 4793f8829a4aSRandall Stewart } 4794cd554309SMichael Tuexen if ((num_seg > 0) || (num_nr_seg > 0)) { 4795f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked, 4796f8829a4aSRandall Stewart biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved); 4797f8829a4aSRandall Stewart } 4798b54d3a6cSRandall Stewart /* JRS - Use the congestion control given in the CC module */ 4799b54d3a6cSRandall Stewart asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc); 4800f8829a4aSRandall Stewart 4801f8829a4aSRandall Stewart /* Now are we exiting loss recovery ? */ 4802f8829a4aSRandall Stewart if (will_exit_fast_recovery) { 4803f8829a4aSRandall Stewart /* Ok, we must exit fast recovery */ 4804f8829a4aSRandall Stewart asoc->fast_retran_loss_recovery = 0; 4805f8829a4aSRandall Stewart } 4806f8829a4aSRandall Stewart if ((asoc->sat_t3_loss_recovery) && 480720b07a4dSMichael Tuexen SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) { 4808f8829a4aSRandall Stewart /* end satellite t3 loss recovery */ 4809f8829a4aSRandall Stewart asoc->sat_t3_loss_recovery = 0; 4810f8829a4aSRandall Stewart } 481142551e99SRandall Stewart /* 481242551e99SRandall Stewart * CMT Fast recovery 481342551e99SRandall Stewart */ 4814f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4815f8829a4aSRandall Stewart if (net->will_exit_fast_recovery) { 4816f8829a4aSRandall Stewart /* Ok, we must exit fast recovery */ 4817f8829a4aSRandall Stewart net->fast_retran_loss_recovery = 0; 4818f8829a4aSRandall Stewart } 4819f8829a4aSRandall Stewart } 4820f8829a4aSRandall Stewart 4821f8829a4aSRandall Stewart /* Adjust and set the new rwnd value */ 4822b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) { 4823f8829a4aSRandall Stewart sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK, 482444fbe462SRandall Stewart asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd); 482580fefe0aSRandall Stewart } 4826f8829a4aSRandall Stewart asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd, 482744fbe462SRandall Stewart (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)))); 4828f8829a4aSRandall Stewart if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) { 4829f8829a4aSRandall Stewart /* SWS sender side engages */ 4830f8829a4aSRandall Stewart asoc->peers_rwnd = 0; 4831f8829a4aSRandall Stewart } 48325e54f665SRandall Stewart if (asoc->peers_rwnd > old_rwnd) { 48335e54f665SRandall Stewart win_probe_recovery = 1; 48345e54f665SRandall Stewart } 4835f8829a4aSRandall Stewart /* 4836f8829a4aSRandall Stewart * Now we must setup so we have a timer up for anyone with 4837f8829a4aSRandall Stewart * outstanding data. 4838f8829a4aSRandall Stewart */ 4839bff64a4dSRandall Stewart done_once = 0; 4840a5d547adSRandall Stewart again: 4841a5d547adSRandall Stewart j = 0; 4842f8829a4aSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 48435e54f665SRandall Stewart if (win_probe_recovery && (net->window_probe)) { 4844c105859eSRandall Stewart win_probe_recovered = 1; 48455e54f665SRandall Stewart /*- 48465e54f665SRandall Stewart * Find first chunk that was used with 48475e54f665SRandall Stewart * window probe and clear the event. Put 48485e54f665SRandall Stewart * it back into the send queue as if has 48495e54f665SRandall Stewart * not been sent. 48505e54f665SRandall Stewart */ 48515e54f665SRandall Stewart TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 48525e54f665SRandall Stewart if (tp1->window_probe) { 48537215cc1bSMichael Tuexen sctp_window_probe_recovery(stcb, asoc, tp1); 48545e54f665SRandall Stewart break; 48555e54f665SRandall Stewart } 48565e54f665SRandall Stewart } 48575e54f665SRandall Stewart } 4858f8829a4aSRandall Stewart if (net->flight_size) { 4859a5d547adSRandall Stewart j++; 4860cd554309SMichael Tuexen if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 4861f8829a4aSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4862f8829a4aSRandall Stewart stcb->sctp_ep, stcb, net); 4863cd554309SMichael Tuexen } 48645171328bSRandall Stewart if (net->window_probe) { 4865cd554309SMichael Tuexen net->window_probe = 0; 48665171328bSRandall Stewart } 4867c105859eSRandall Stewart } else { 48685171328bSRandall Stewart if (net->window_probe) { 48695171328bSRandall Stewart /* 48705171328bSRandall Stewart * In window probes we must assure a timer 48715171328bSRandall Stewart * is still running there 48725171328bSRandall Stewart */ 48735171328bSRandall Stewart if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 48745171328bSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, 48755171328bSRandall Stewart stcb->sctp_ep, stcb, net); 48765171328bSRandall Stewart 48775171328bSRandall Stewart } 48785171328bSRandall Stewart } else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) { 4879c105859eSRandall Stewart sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, 4880c105859eSRandall Stewart stcb, net, 4881b7d130beSMichael Tuexen SCTP_FROM_SCTP_INDATA + SCTP_LOC_34); 4882c105859eSRandall Stewart } 4883f8829a4aSRandall Stewart } 4884f8829a4aSRandall Stewart } 4885bff64a4dSRandall Stewart if ((j == 0) && 4886bff64a4dSRandall Stewart (!TAILQ_EMPTY(&asoc->sent_queue)) && 4887bff64a4dSRandall Stewart (asoc->sent_queue_retran_cnt == 0) && 4888c105859eSRandall Stewart (win_probe_recovered == 0) && 4889bff64a4dSRandall Stewart (done_once == 0)) { 48900c0982b8SRandall Stewart /* 48910c0982b8SRandall Stewart * huh, this should not happen unless all packets are 48920c0982b8SRandall Stewart * PR-SCTP and marked to skip of course. 48930c0982b8SRandall Stewart */ 48940c0982b8SRandall Stewart if (sctp_fs_audit(asoc)) { 4895a5d547adSRandall Stewart TAILQ_FOREACH(net, &asoc->nets, sctp_next) { 4896a5d547adSRandall Stewart net->flight_size = 0; 4897a5d547adSRandall Stewart } 4898a5d547adSRandall Stewart asoc->total_flight = 0; 4899a5d547adSRandall Stewart asoc->total_flight_count = 0; 4900a5d547adSRandall Stewart asoc->sent_queue_retran_cnt = 0; 4901a5d547adSRandall Stewart TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) { 4902a5d547adSRandall Stewart if (tp1->sent < SCTP_DATAGRAM_RESEND) { 4903c105859eSRandall Stewart sctp_flight_size_increase(tp1); 4904c105859eSRandall Stewart sctp_total_flight_increase(stcb, tp1); 4905a5d547adSRandall Stewart } else if (tp1->sent == SCTP_DATAGRAM_RESEND) { 4906791437b5SRandall Stewart sctp_ucount_incr(asoc->sent_queue_retran_cnt); 4907a5d547adSRandall Stewart } 4908a5d547adSRandall Stewart } 49090c0982b8SRandall Stewart } 4910bff64a4dSRandall Stewart done_once = 1; 4911a5d547adSRandall Stewart goto again; 4912a5d547adSRandall Stewart } 4913cd554309SMichael Tuexen /*********************************************/ 4914cd554309SMichael Tuexen /* Here we perform PR-SCTP procedures */ 4915cd554309SMichael Tuexen /* (section 4.2) */ 4916cd554309SMichael Tuexen /*********************************************/ 4917cd554309SMichael Tuexen /* C1. update advancedPeerAckPoint */ 491820b07a4dSMichael Tuexen if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) { 4919dfb11ef8SRandall Stewart asoc->advanced_peer_ack_point = cum_ack; 4920dfb11ef8SRandall Stewart } 4921830d754dSRandall Stewart /* C2. try to further move advancedPeerAckPoint ahead */ 4922dd973b0eSMichael Tuexen if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) { 4923830d754dSRandall Stewart struct sctp_tmit_chunk *lchk; 4924830d754dSRandall Stewart uint32_t old_adv_peer_ack_point; 4925830d754dSRandall Stewart 4926830d754dSRandall Stewart old_adv_peer_ack_point = asoc->advanced_peer_ack_point; 4927830d754dSRandall Stewart lchk = sctp_try_advance_peer_ack_point(stcb, asoc); 4928830d754dSRandall Stewart /* C3. See if we need to send a Fwd-TSN */ 492920b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) { 4930830d754dSRandall Stewart /* 4931493d8e5aSRandall Stewart * ISSUE with ECN, see FWD-TSN processing. 4932830d754dSRandall Stewart */ 49330c0982b8SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) { 49340c0982b8SRandall Stewart sctp_misc_ints(SCTP_FWD_TSN_CHECK, 49350c0982b8SRandall Stewart 0xee, cum_ack, asoc->advanced_peer_ack_point, 49360c0982b8SRandall Stewart old_adv_peer_ack_point); 49370c0982b8SRandall Stewart } 493820b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) { 4939830d754dSRandall Stewart send_forward_tsn(stcb, asoc); 49400c0982b8SRandall Stewart } else if (lchk) { 49410c0982b8SRandall Stewart /* try to FR fwd-tsn's that get lost too */ 494244fbe462SRandall Stewart if (lchk->rec.data.fwd_tsn_cnt >= 3) { 49430c0982b8SRandall Stewart send_forward_tsn(stcb, asoc); 49440c0982b8SRandall Stewart } 4945830d754dSRandall Stewart } 4946830d754dSRandall Stewart } 4947830d754dSRandall Stewart if (lchk) { 4948830d754dSRandall Stewart /* Assure a timer is up */ 4949830d754dSRandall Stewart sctp_timer_start(SCTP_TIMER_TYPE_SEND, 4950830d754dSRandall Stewart stcb->sctp_ep, stcb, lchk->whoTo); 4951830d754dSRandall Stewart } 4952830d754dSRandall Stewart } 4953b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) { 4954f8829a4aSRandall Stewart sctp_misc_ints(SCTP_SACK_RWND_UPDATE, 4955f8829a4aSRandall Stewart a_rwnd, 4956f8829a4aSRandall Stewart stcb->asoc.peers_rwnd, 4957f8829a4aSRandall Stewart stcb->asoc.total_flight, 4958f8829a4aSRandall Stewart stcb->asoc.total_output_queue_size); 495980fefe0aSRandall Stewart } 4960f8829a4aSRandall Stewart } 4961f8829a4aSRandall Stewart 4962f8829a4aSRandall Stewart void 49637215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag) 4964f8829a4aSRandall Stewart { 4965f8829a4aSRandall Stewart /* Copy cum-ack */ 4966f8829a4aSRandall Stewart uint32_t cum_ack, a_rwnd; 4967f8829a4aSRandall Stewart 4968f8829a4aSRandall Stewart cum_ack = ntohl(cp->cumulative_tsn_ack); 4969f8829a4aSRandall Stewart /* Arrange so a_rwnd does NOT change */ 4970f8829a4aSRandall Stewart a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight; 4971f8829a4aSRandall Stewart 4972f8829a4aSRandall Stewart /* Now call the express sack handling */ 4973899288aeSRandall Stewart sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0); 4974f8829a4aSRandall Stewart } 4975f8829a4aSRandall Stewart 4976f8829a4aSRandall Stewart static void 4977f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb, 4978f8829a4aSRandall Stewart struct sctp_stream_in *strmin) 4979f8829a4aSRandall Stewart { 4980f8829a4aSRandall Stewart struct sctp_queued_to_read *ctl, *nctl; 4981f8829a4aSRandall Stewart struct sctp_association *asoc; 498283128708SRandall Stewart uint16_t tt; 4983f8829a4aSRandall Stewart 4984f8829a4aSRandall Stewart asoc = &stcb->asoc; 4985f8829a4aSRandall Stewart tt = strmin->last_sequence_delivered; 4986f8829a4aSRandall Stewart /* 4987f8829a4aSRandall Stewart * First deliver anything prior to and including the stream no that 4988f8829a4aSRandall Stewart * came in 4989f8829a4aSRandall Stewart */ 49904a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) { 499120b07a4dSMichael Tuexen if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) { 4992f8829a4aSRandall Stewart /* this is deliverable now */ 4993f8829a4aSRandall Stewart TAILQ_REMOVE(&strmin->inqueue, ctl, next); 4994f8829a4aSRandall Stewart /* subtract pending on streams */ 4995f8829a4aSRandall Stewart asoc->size_on_all_streams -= ctl->length; 4996f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 4997f8829a4aSRandall Stewart /* deliver it to at least the delivery-q */ 4998f8829a4aSRandall Stewart if (stcb->sctp_socket) { 4999b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, ctl->sinfo_tsn); 5000f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 5001f8829a4aSRandall Stewart ctl, 5002cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED); 5003f8829a4aSRandall Stewart } 5004f8829a4aSRandall Stewart } else { 5005f8829a4aSRandall Stewart /* no more delivery now. */ 5006f8829a4aSRandall Stewart break; 5007f8829a4aSRandall Stewart } 5008f8829a4aSRandall Stewart } 5009f8829a4aSRandall Stewart /* 5010f8829a4aSRandall Stewart * now we must deliver things in queue the normal way if any are 5011f8829a4aSRandall Stewart * now ready. 5012f8829a4aSRandall Stewart */ 5013f8829a4aSRandall Stewart tt = strmin->last_sequence_delivered + 1; 50144a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) { 5015f8829a4aSRandall Stewart if (tt == ctl->sinfo_ssn) { 5016f8829a4aSRandall Stewart /* this is deliverable now */ 5017f8829a4aSRandall Stewart TAILQ_REMOVE(&strmin->inqueue, ctl, next); 5018f8829a4aSRandall Stewart /* subtract pending on streams */ 5019f8829a4aSRandall Stewart asoc->size_on_all_streams -= ctl->length; 5020f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_all_streams); 5021f8829a4aSRandall Stewart /* deliver it to at least the delivery-q */ 5022f8829a4aSRandall Stewart strmin->last_sequence_delivered = ctl->sinfo_ssn; 5023f8829a4aSRandall Stewart if (stcb->sctp_socket) { 5024b5c16493SMichael Tuexen sctp_mark_non_revokable(asoc, ctl->sinfo_tsn); 5025f8829a4aSRandall Stewart sctp_add_to_readq(stcb->sctp_ep, stcb, 5026f8829a4aSRandall Stewart ctl, 5027cfde3ff7SRandall Stewart &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED); 502877acdc25SRandall Stewart 5029f8829a4aSRandall Stewart } 5030f8829a4aSRandall Stewart tt = strmin->last_sequence_delivered + 1; 5031f8829a4aSRandall Stewart } else { 5032f8829a4aSRandall Stewart break; 5033f8829a4aSRandall Stewart } 5034f8829a4aSRandall Stewart } 5035f8829a4aSRandall Stewart } 5036f8829a4aSRandall Stewart 50378933fa13SRandall Stewart static void 50388933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb, 50398933fa13SRandall Stewart struct sctp_association *asoc, 50408933fa13SRandall Stewart uint16_t stream, uint16_t seq) 50418933fa13SRandall Stewart { 50424a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 50438933fa13SRandall Stewart 50448933fa13SRandall Stewart /* For each one on here see if we need to toss it */ 50458933fa13SRandall Stewart /* 50464a9ef3f8SMichael Tuexen * For now large messages held on the reasmqueue that are complete 50474a9ef3f8SMichael Tuexen * will be tossed too. We could in theory do more work to spin 50484a9ef3f8SMichael Tuexen * through and stop after dumping one msg aka seeing the start of a 50494a9ef3f8SMichael Tuexen * new msg at the head, and call the delivery function... to see if 50504a9ef3f8SMichael Tuexen * it can be delivered... But for now we just dump everything on the 50514a9ef3f8SMichael Tuexen * queue. 50528933fa13SRandall Stewart */ 50534a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { 50548e71b694SMichael Tuexen /* 50554a9ef3f8SMichael Tuexen * Do not toss it if on a different stream or marked for 50564a9ef3f8SMichael Tuexen * unordered delivery in which case the stream sequence 50574a9ef3f8SMichael Tuexen * number has no meaning. 50588e71b694SMichael Tuexen */ 50598e71b694SMichael Tuexen if ((chk->rec.data.stream_number != stream) || 50608e71b694SMichael Tuexen ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) { 50618933fa13SRandall Stewart continue; 50628933fa13SRandall Stewart } 50638933fa13SRandall Stewart if (chk->rec.data.stream_seq == seq) { 50648933fa13SRandall Stewart /* It needs to be tossed */ 50658933fa13SRandall Stewart TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 506620b07a4dSMichael Tuexen if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) { 50674a9ef3f8SMichael Tuexen asoc->tsn_last_delivered = chk->rec.data.TSN_seq; 50684a9ef3f8SMichael Tuexen asoc->str_of_pdapi = chk->rec.data.stream_number; 50694a9ef3f8SMichael Tuexen asoc->ssn_of_pdapi = chk->rec.data.stream_seq; 50704a9ef3f8SMichael Tuexen asoc->fragment_flags = chk->rec.data.rcv_flags; 50718933fa13SRandall Stewart } 50728933fa13SRandall Stewart asoc->size_on_reasm_queue -= chk->send_size; 50738933fa13SRandall Stewart sctp_ucount_decr(asoc->cnt_on_reasm_queue); 50748933fa13SRandall Stewart 50758933fa13SRandall Stewart /* Clear up any stream problem */ 507620b07a4dSMichael Tuexen if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED && 507720b07a4dSMichael Tuexen SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) { 50788933fa13SRandall Stewart /* 50798933fa13SRandall Stewart * We must dump forward this streams 50804a9ef3f8SMichael Tuexen * sequence number if the chunk is not 50814a9ef3f8SMichael Tuexen * unordered that is being skipped. There is 50824a9ef3f8SMichael Tuexen * a chance that if the peer does not 50834a9ef3f8SMichael Tuexen * include the last fragment in its FWD-TSN 50844a9ef3f8SMichael Tuexen * we WILL have a problem here since you 50854a9ef3f8SMichael Tuexen * would have a partial chunk in queue that 50864a9ef3f8SMichael Tuexen * may not be deliverable. Also if a Partial 50874a9ef3f8SMichael Tuexen * delivery API as started the user may get 50884a9ef3f8SMichael Tuexen * a partial chunk. The next read returning 50894a9ef3f8SMichael Tuexen * a new chunk... really ugly but I see no 50904a9ef3f8SMichael Tuexen * way around it! Maybe a notify?? 50918933fa13SRandall Stewart */ 50924a9ef3f8SMichael Tuexen asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq; 50938933fa13SRandall Stewart } 50948933fa13SRandall Stewart if (chk->data) { 50958933fa13SRandall Stewart sctp_m_freem(chk->data); 50968933fa13SRandall Stewart chk->data = NULL; 50978933fa13SRandall Stewart } 5098689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 509920b07a4dSMichael Tuexen } else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) { 51008933fa13SRandall Stewart /* 51014a9ef3f8SMichael Tuexen * If the stream_seq is > than the purging one, we 51024a9ef3f8SMichael Tuexen * are done 51038933fa13SRandall Stewart */ 51048933fa13SRandall Stewart break; 51058933fa13SRandall Stewart } 51068933fa13SRandall Stewart } 51078933fa13SRandall Stewart } 51088933fa13SRandall Stewart 51098933fa13SRandall Stewart 5110f8829a4aSRandall Stewart void 5111f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb, 5112b5c16493SMichael Tuexen struct sctp_forward_tsn_chunk *fwd, 5113b5c16493SMichael Tuexen int *abort_flag, struct mbuf *m, int offset) 5114f8829a4aSRandall Stewart { 5115f8829a4aSRandall Stewart /* The pr-sctp fwd tsn */ 5116f8829a4aSRandall Stewart /* 5117f8829a4aSRandall Stewart * here we will perform all the data receiver side steps for 5118f8829a4aSRandall Stewart * processing FwdTSN, as required in by pr-sctp draft: 5119f8829a4aSRandall Stewart * 5120f8829a4aSRandall Stewart * Assume we get FwdTSN(x): 5121f8829a4aSRandall Stewart * 5122f8829a4aSRandall Stewart * 1) update local cumTSN to x 2) try to further advance cumTSN to x + 5123f8829a4aSRandall Stewart * others we have 3) examine and update re-ordering queue on 5124f8829a4aSRandall Stewart * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to 5125f8829a4aSRandall Stewart * report where we are. 5126f8829a4aSRandall Stewart */ 5127f8829a4aSRandall Stewart struct sctp_association *asoc; 51287898f408SRandall Stewart uint32_t new_cum_tsn, gap; 51297215cc1bSMichael Tuexen unsigned int i, fwd_sz, m_size; 51308933fa13SRandall Stewart uint32_t str_seq; 5131f8829a4aSRandall Stewart struct sctp_stream_in *strm; 51324a9ef3f8SMichael Tuexen struct sctp_tmit_chunk *chk, *nchk; 51338933fa13SRandall Stewart struct sctp_queued_to_read *ctl, *sv; 5134f8829a4aSRandall Stewart 5135f8829a4aSRandall Stewart asoc = &stcb->asoc; 5136f8829a4aSRandall Stewart if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) { 5137ad81507eSRandall Stewart SCTPDBG(SCTP_DEBUG_INDATA1, 5138ad81507eSRandall Stewart "Bad size too small/big fwd-tsn\n"); 5139f8829a4aSRandall Stewart return; 5140f8829a4aSRandall Stewart } 5141f8829a4aSRandall Stewart m_size = (stcb->asoc.mapping_array_size << 3); 5142f8829a4aSRandall Stewart /*************************************************************/ 5143f8829a4aSRandall Stewart /* 1. Here we update local cumTSN and shift the bitmap array */ 5144f8829a4aSRandall Stewart /*************************************************************/ 5145f8829a4aSRandall Stewart new_cum_tsn = ntohl(fwd->new_cumulative_tsn); 5146f8829a4aSRandall Stewart 514720b07a4dSMichael Tuexen if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) { 5148f8829a4aSRandall Stewart /* Already got there ... */ 5149f8829a4aSRandall Stewart return; 5150f8829a4aSRandall Stewart } 5151f8829a4aSRandall Stewart /* 5152f8829a4aSRandall Stewart * now we know the new TSN is more advanced, let's find the actual 5153f8829a4aSRandall Stewart * gap 5154f8829a4aSRandall Stewart */ 5155b5c16493SMichael Tuexen SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn); 515677acdc25SRandall Stewart asoc->cumulative_tsn = new_cum_tsn; 51572afb3e84SRandall Stewart if (gap >= m_size) { 5158f8829a4aSRandall Stewart if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) { 5159ff1ffd74SMichael Tuexen struct mbuf *op_err; 5160ff1ffd74SMichael Tuexen char msg[SCTP_DIAG_INFO_LEN]; 516117205eccSRandall Stewart 5162f8829a4aSRandall Stewart /* 5163f8829a4aSRandall Stewart * out of range (of single byte chunks in the rwnd I 516417205eccSRandall Stewart * give out). This must be an attacker. 5165f8829a4aSRandall Stewart */ 516617205eccSRandall Stewart *abort_flag = 1; 5167ff1ffd74SMichael Tuexen snprintf(msg, sizeof(msg), 5168ff1ffd74SMichael Tuexen "New cum ack %8.8x too high, highest TSN %8.8x", 5169ff1ffd74SMichael Tuexen new_cum_tsn, asoc->highest_tsn_inside_map); 5170ff1ffd74SMichael Tuexen op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg); 5171b7d130beSMichael Tuexen stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_35; 5172ff1ffd74SMichael Tuexen sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED); 5173f8829a4aSRandall Stewart return; 5174f8829a4aSRandall Stewart } 5175207304d4SRandall Stewart SCTP_STAT_INCR(sctps_fwdtsn_map_over); 517677acdc25SRandall Stewart 51772afb3e84SRandall Stewart memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size); 51782afb3e84SRandall Stewart asoc->mapping_array_base_tsn = new_cum_tsn + 1; 517977acdc25SRandall Stewart asoc->highest_tsn_inside_map = new_cum_tsn; 518077acdc25SRandall Stewart 5181b5c16493SMichael Tuexen memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size); 5182830d754dSRandall Stewart asoc->highest_tsn_inside_nr_map = new_cum_tsn; 518377acdc25SRandall Stewart 5184b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) { 5185f8829a4aSRandall Stewart sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT); 518680fefe0aSRandall Stewart } 51872afb3e84SRandall Stewart } else { 51882afb3e84SRandall Stewart SCTP_TCB_LOCK_ASSERT(stcb); 51892afb3e84SRandall Stewart for (i = 0; i <= gap; i++) { 51907898f408SRandall Stewart if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) && 51917898f408SRandall Stewart !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) { 51928933fa13SRandall Stewart SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i); 519320b07a4dSMichael Tuexen if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) { 51947898f408SRandall Stewart asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i; 5195b5c16493SMichael Tuexen } 5196b5c16493SMichael Tuexen } 5197b5c16493SMichael Tuexen } 5198f8829a4aSRandall Stewart } 5199f8829a4aSRandall Stewart /*************************************************************/ 5200f8829a4aSRandall Stewart /* 2. Clear up re-assembly queue */ 5201f8829a4aSRandall Stewart /*************************************************************/ 5202f8829a4aSRandall Stewart /* 5203f8829a4aSRandall Stewart * First service it if pd-api is up, just in case we can progress it 5204f8829a4aSRandall Stewart * forward 5205f8829a4aSRandall Stewart */ 5206f8829a4aSRandall Stewart if (asoc->fragmented_delivery_inprogress) { 5207f8829a4aSRandall Stewart sctp_service_reassembly(stcb, asoc); 5208f8829a4aSRandall Stewart } 5209f8829a4aSRandall Stewart /* For each one on here see if we need to toss it */ 5210f8829a4aSRandall Stewart /* 52114a9ef3f8SMichael Tuexen * For now large messages held on the reasmqueue that are complete 52124a9ef3f8SMichael Tuexen * will be tossed too. We could in theory do more work to spin 52134a9ef3f8SMichael Tuexen * through and stop after dumping one msg aka seeing the start of a 52144a9ef3f8SMichael Tuexen * new msg at the head, and call the delivery function... to see if 52154a9ef3f8SMichael Tuexen * it can be delivered... But for now we just dump everything on the 52164a9ef3f8SMichael Tuexen * queue. 5217f8829a4aSRandall Stewart */ 52184a9ef3f8SMichael Tuexen TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { 521920b07a4dSMichael Tuexen if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) { 5220f8829a4aSRandall Stewart /* It needs to be tossed */ 5221f8829a4aSRandall Stewart TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); 522220b07a4dSMichael Tuexen if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) { 52234a9ef3f8SMichael Tuexen asoc->tsn_last_delivered = chk->rec.data.TSN_seq; 52244a9ef3f8SMichael Tuexen asoc->str_of_pdapi = chk->rec.data.stream_number; 52254a9ef3f8SMichael Tuexen asoc->ssn_of_pdapi = chk->rec.data.stream_seq; 52264a9ef3f8SMichael Tuexen asoc->fragment_flags = chk->rec.data.rcv_flags; 5227f8829a4aSRandall Stewart } 5228f8829a4aSRandall Stewart asoc->size_on_reasm_queue -= chk->send_size; 5229f8829a4aSRandall Stewart sctp_ucount_decr(asoc->cnt_on_reasm_queue); 5230f8829a4aSRandall Stewart 5231f8829a4aSRandall Stewart /* Clear up any stream problem */ 523220b07a4dSMichael Tuexen if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED && 523320b07a4dSMichael Tuexen SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) { 5234f8829a4aSRandall Stewart /* 5235f8829a4aSRandall Stewart * We must dump forward this streams 52364a9ef3f8SMichael Tuexen * sequence number if the chunk is not 52374a9ef3f8SMichael Tuexen * unordered that is being skipped. There is 52384a9ef3f8SMichael Tuexen * a chance that if the peer does not 52394a9ef3f8SMichael Tuexen * include the last fragment in its FWD-TSN 52404a9ef3f8SMichael Tuexen * we WILL have a problem here since you 52414a9ef3f8SMichael Tuexen * would have a partial chunk in queue that 52424a9ef3f8SMichael Tuexen * may not be deliverable. Also if a Partial 52434a9ef3f8SMichael Tuexen * delivery API as started the user may get 52444a9ef3f8SMichael Tuexen * a partial chunk. The next read returning 52454a9ef3f8SMichael Tuexen * a new chunk... really ugly but I see no 52464a9ef3f8SMichael Tuexen * way around it! Maybe a notify?? 5247f8829a4aSRandall Stewart */ 52484a9ef3f8SMichael Tuexen asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq; 5249f8829a4aSRandall Stewart } 5250f8829a4aSRandall Stewart if (chk->data) { 5251f8829a4aSRandall Stewart sctp_m_freem(chk->data); 5252f8829a4aSRandall Stewart chk->data = NULL; 5253f8829a4aSRandall Stewart } 5254689e6a5fSMichael Tuexen sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); 5255f8829a4aSRandall Stewart } else { 5256f8829a4aSRandall Stewart /* 52574a9ef3f8SMichael Tuexen * Ok we have gone beyond the end of the fwd-tsn's 52584a9ef3f8SMichael Tuexen * mark. 5259f8829a4aSRandall Stewart */ 5260f8829a4aSRandall Stewart break; 5261f8829a4aSRandall Stewart } 5262f8829a4aSRandall Stewart } 52638933fa13SRandall Stewart /*******************************************************/ 52648933fa13SRandall Stewart /* 3. Update the PR-stream re-ordering queues and fix */ 52658933fa13SRandall Stewart /* delivery issues as needed. */ 52668933fa13SRandall Stewart /*******************************************************/ 5267f8829a4aSRandall Stewart fwd_sz -= sizeof(*fwd); 5268671d309cSRandall Stewart if (m && fwd_sz) { 5269f8829a4aSRandall Stewart /* New method. */ 5270d61a0ae0SRandall Stewart unsigned int num_str; 5271671d309cSRandall Stewart struct sctp_strseq *stseq, strseqbuf; 5272671d309cSRandall Stewart 5273671d309cSRandall Stewart offset += sizeof(*fwd); 5274f8829a4aSRandall Stewart 52758933fa13SRandall Stewart SCTP_INP_READ_LOCK(stcb->sctp_ep); 5276f8829a4aSRandall Stewart num_str = fwd_sz / sizeof(struct sctp_strseq); 5277f8829a4aSRandall Stewart for (i = 0; i < num_str; i++) { 5278f8829a4aSRandall Stewart uint16_t st; 5279f8829a4aSRandall Stewart 5280671d309cSRandall Stewart stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset, 5281671d309cSRandall Stewart sizeof(struct sctp_strseq), 5282671d309cSRandall Stewart (uint8_t *) & strseqbuf); 5283671d309cSRandall Stewart offset += sizeof(struct sctp_strseq); 52842afb3e84SRandall Stewart if (stseq == NULL) { 5285671d309cSRandall Stewart break; 52862afb3e84SRandall Stewart } 5287f8829a4aSRandall Stewart /* Convert */ 5288851b7298SRandall Stewart st = ntohs(stseq->stream); 5289851b7298SRandall Stewart stseq->stream = st; 5290851b7298SRandall Stewart st = ntohs(stseq->sequence); 5291851b7298SRandall Stewart stseq->sequence = st; 52928933fa13SRandall Stewart 5293f8829a4aSRandall Stewart /* now process */ 52948933fa13SRandall Stewart 52958933fa13SRandall Stewart /* 52968933fa13SRandall Stewart * Ok we now look for the stream/seq on the read 52978933fa13SRandall Stewart * queue where its not all delivered. If we find it 52988933fa13SRandall Stewart * we transmute the read entry into a PDI_ABORTED. 52998933fa13SRandall Stewart */ 5300851b7298SRandall Stewart if (stseq->stream >= asoc->streamincnt) { 53012afb3e84SRandall Stewart /* screwed up streams, stop! */ 53022afb3e84SRandall Stewart break; 5303f8829a4aSRandall Stewart } 53048933fa13SRandall Stewart if ((asoc->str_of_pdapi == stseq->stream) && 53058933fa13SRandall Stewart (asoc->ssn_of_pdapi == stseq->sequence)) { 53068933fa13SRandall Stewart /* 53078933fa13SRandall Stewart * If this is the one we were partially 53088933fa13SRandall Stewart * delivering now then we no longer are. 53098933fa13SRandall Stewart * Note this will change with the reassembly 53108933fa13SRandall Stewart * re-write. 53118933fa13SRandall Stewart */ 53128933fa13SRandall Stewart asoc->fragmented_delivery_inprogress = 0; 53138933fa13SRandall Stewart } 53148933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence); 53158933fa13SRandall Stewart TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) { 53168933fa13SRandall Stewart if ((ctl->sinfo_stream == stseq->stream) && 53178933fa13SRandall Stewart (ctl->sinfo_ssn == stseq->sequence)) { 53188933fa13SRandall Stewart str_seq = (stseq->stream << 16) | stseq->sequence; 53198933fa13SRandall Stewart ctl->end_added = 1; 53208933fa13SRandall Stewart ctl->pdapi_aborted = 1; 53218933fa13SRandall Stewart sv = stcb->asoc.control_pdapi; 53228933fa13SRandall Stewart stcb->asoc.control_pdapi = ctl; 5323810ec536SMichael Tuexen sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, 5324810ec536SMichael Tuexen stcb, 53258933fa13SRandall Stewart SCTP_PARTIAL_DELIVERY_ABORTED, 5326810ec536SMichael Tuexen (void *)&str_seq, 5327810ec536SMichael Tuexen SCTP_SO_NOT_LOCKED); 53288933fa13SRandall Stewart stcb->asoc.control_pdapi = sv; 53298933fa13SRandall Stewart break; 53308933fa13SRandall Stewart } else if ((ctl->sinfo_stream == stseq->stream) && 533120b07a4dSMichael Tuexen SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) { 53328933fa13SRandall Stewart /* We are past our victim SSN */ 53338933fa13SRandall Stewart break; 53348933fa13SRandall Stewart } 53358933fa13SRandall Stewart } 5336851b7298SRandall Stewart strm = &asoc->strmin[stseq->stream]; 533720b07a4dSMichael Tuexen if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) { 5338f8829a4aSRandall Stewart /* Update the sequence number */ 533920b07a4dSMichael Tuexen strm->last_sequence_delivered = stseq->sequence; 5340f8829a4aSRandall Stewart } 5341f8829a4aSRandall Stewart /* now kick the stream the new way */ 534204ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 5343f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(stcb, strm); 5344f8829a4aSRandall Stewart } 53458933fa13SRandall Stewart SCTP_INP_READ_UNLOCK(stcb->sctp_ep); 5346f8829a4aSRandall Stewart } 53477898f408SRandall Stewart /* 53487898f408SRandall Stewart * Now slide thing forward. 53497898f408SRandall Stewart */ 53507898f408SRandall Stewart sctp_slide_mapping_arrays(stcb); 53517898f408SRandall Stewart 535224f52bbdSMichael Tuexen if (!TAILQ_EMPTY(&asoc->reasmqueue)) { 5353139bc87fSRandall Stewart /* now lets kick out and check for more fragmented delivery */ 535404ee05e8SRandall Stewart /* sa_ignore NO_NULL_CHK */ 5355139bc87fSRandall Stewart sctp_deliver_reasm_check(stcb, &stcb->asoc); 5356139bc87fSRandall Stewart } 5357f8829a4aSRandall Stewart } 5358