xref: /freebsd/sys/netinet/sctp_indata.c (revision d1ea5fa9c2e99d46da7d02b83476d77889dd6a55)
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>
3744249214SRandall Stewart #include <sys/proc.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3942551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
4144249214SRandall Stewart #include <netinet/sctp_pcb.h>
42f8829a4aSRandall Stewart #include <netinet/sctputil.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
4544249214SRandall Stewart #include <netinet/sctp_auth.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
4744249214SRandall Stewart #include <netinet/sctp_asconf.h>
4844249214SRandall Stewart #include <netinet/sctp_indata.h>
4944249214SRandall Stewart #include <netinet/sctp_bsd_addr.h>
5044249214SRandall Stewart #include <netinet/sctp_input.h>
5144249214SRandall Stewart #include <netinet/sctp_crc32.h>
5244249214SRandall Stewart #include <netinet/sctp_lock_bsd.h>
53f8829a4aSRandall Stewart /*
54f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
55f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
56f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
57f8829a4aSRandall Stewart  *
58f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
59f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
60f8829a4aSRandall Stewart  * the list.
61f8829a4aSRandall Stewart  */
6244249214SRandall Stewart static void
6344249214SRandall Stewart sctp_add_chk_to_control(struct sctp_queued_to_read *control,
6444249214SRandall Stewart     struct sctp_stream_in *strm,
6544249214SRandall Stewart     struct sctp_tcb *stcb,
6644249214SRandall Stewart     struct sctp_association *asoc,
67*d1ea5fa9SMichael Tuexen     struct sctp_tmit_chunk *chk, int lock_held);
6844249214SRandall Stewart 
69f8829a4aSRandall Stewart 
7072fb6fdbSRandall Stewart void
71f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
72f8829a4aSRandall Stewart {
73b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
74f8829a4aSRandall Stewart }
75f8829a4aSRandall Stewart 
76f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
7772fb6fdbSRandall Stewart uint32_t
78f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
79f8829a4aSRandall Stewart {
80b3f1ea41SRandall Stewart 	uint32_t calc = 0;
81f8829a4aSRandall Stewart 
82f8829a4aSRandall Stewart 	/*
83f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
844e88d37aSMichael Tuexen 	 * the sb_cc is the count that everyone as put up. When we re-write
85f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
86f8829a4aSRandall Stewart 	 * associations data is taken into account.
87f8829a4aSRandall Stewart 	 */
8844249214SRandall Stewart 	if (stcb->sctp_socket == NULL) {
89f8829a4aSRandall Stewart 		return (calc);
9044249214SRandall Stewart 	}
914e88d37aSMichael Tuexen 	if (stcb->asoc.sb_cc == 0 &&
92f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
93f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
94f8829a4aSRandall Stewart 		/* Full rwnd granted */
95b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
96f8829a4aSRandall Stewart 		return (calc);
97f8829a4aSRandall Stewart 	}
98f8829a4aSRandall Stewart 	/* get actual space */
99f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
100f8829a4aSRandall Stewart 	/*
101f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
102f8829a4aSRandall Stewart 	 * for putting up.
103f8829a4aSRandall Stewart 	 */
10444fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
10544fbe462SRandall Stewart 	    asoc->cnt_on_reasm_queue * MSIZE));
10644fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
10744fbe462SRandall Stewart 	    asoc->cnt_on_all_streams * MSIZE));
108f8829a4aSRandall Stewart 	if (calc == 0) {
109f8829a4aSRandall Stewart 		/* out of space */
110f8829a4aSRandall Stewart 		return (calc);
111f8829a4aSRandall Stewart 	}
112f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1132afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
114b3f1ea41SRandall Stewart 	/*
115b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
116b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
117f8829a4aSRandall Stewart 	 */
118b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
119b3f1ea41SRandall Stewart 		calc = 1;
1202afb3e84SRandall Stewart 	}
121b3f1ea41SRandall Stewart 	return (calc);
122f8829a4aSRandall Stewart }
123f8829a4aSRandall Stewart 
124f8829a4aSRandall Stewart 
125f8829a4aSRandall Stewart 
126f8829a4aSRandall Stewart /*
127f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
128f8829a4aSRandall Stewart  */
129f8829a4aSRandall Stewart struct sctp_queued_to_read *
130f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
131f8829a4aSRandall Stewart     struct sctp_nets *net,
132f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
133f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
13444249214SRandall Stewart     uint32_t stream_seq, uint8_t flags,
135f8829a4aSRandall Stewart     struct mbuf *dm)
136f8829a4aSRandall Stewart {
137f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
138f8829a4aSRandall Stewart 
139f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
140f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
141f8829a4aSRandall Stewart 		goto failed_build;
142f8829a4aSRandall Stewart 	}
14344249214SRandall Stewart 	memset(read_queue_e, 0, sizeof(struct sctp_queued_to_read));
144f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
145f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
146f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
147f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
1487215cc1bSMichael Tuexen 	read_queue_e->sinfo_context = context;
149f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
150f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
151f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
15244249214SRandall Stewart 	read_queue_e->top_fsn = read_queue_e->fsn_included = 0xffffffff;
15344249214SRandall Stewart 	TAILQ_INIT(&read_queue_e->reasm);
154f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
155f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
156f8829a4aSRandall Stewart 	read_queue_e->data = dm;
157f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
158f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
159f8829a4aSRandall Stewart failed_build:
160f8829a4aSRandall Stewart 	return (read_queue_e);
161f8829a4aSRandall Stewart }
162f8829a4aSRandall Stewart 
163f8829a4aSRandall Stewart struct mbuf *
164e2e7c62eSMichael Tuexen sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
165f8829a4aSRandall Stewart {
166e2e7c62eSMichael Tuexen 	struct sctp_extrcvinfo *seinfo;
167f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
168e2e7c62eSMichael Tuexen 	struct sctp_rcvinfo *rcvinfo;
169e2e7c62eSMichael Tuexen 	struct sctp_nxtinfo *nxtinfo;
170f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
171f8829a4aSRandall Stewart 	struct mbuf *ret;
172f8829a4aSRandall Stewart 	int len;
173e2e7c62eSMichael Tuexen 	int use_extended;
174e2e7c62eSMichael Tuexen 	int provide_nxt;
175f8829a4aSRandall Stewart 
176e2e7c62eSMichael Tuexen 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
177e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
178e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
179e2e7c62eSMichael Tuexen 		/* user does not want any ancillary data */
180f8829a4aSRandall Stewart 		return (NULL);
181f8829a4aSRandall Stewart 	}
182e2e7c62eSMichael Tuexen 	len = 0;
183e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
184e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
185e2e7c62eSMichael Tuexen 	}
186e2e7c62eSMichael Tuexen 	seinfo = (struct sctp_extrcvinfo *)sinfo;
187e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
188b70b526dSMichael Tuexen 	    (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
189e2e7c62eSMichael Tuexen 		provide_nxt = 1;
1900bfc52beSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
191e2e7c62eSMichael Tuexen 	} else {
192e2e7c62eSMichael Tuexen 		provide_nxt = 0;
193e2e7c62eSMichael Tuexen 	}
194e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
195f8829a4aSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
196f8829a4aSRandall Stewart 			use_extended = 1;
197e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
198f8829a4aSRandall Stewart 		} else {
199e2e7c62eSMichael Tuexen 			use_extended = 0;
200e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
201e2e7c62eSMichael Tuexen 		}
202e2e7c62eSMichael Tuexen 	} else {
203e2e7c62eSMichael Tuexen 		use_extended = 0;
204f8829a4aSRandall Stewart 	}
205f8829a4aSRandall Stewart 
206eb1b1807SGleb Smirnoff 	ret = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
207f8829a4aSRandall Stewart 	if (ret == NULL) {
208f8829a4aSRandall Stewart 		/* No space */
209f8829a4aSRandall Stewart 		return (ret);
210f8829a4aSRandall Stewart 	}
211e2e7c62eSMichael Tuexen 	SCTP_BUF_LEN(ret) = 0;
212e2e7c62eSMichael Tuexen 
213f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct */
214f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
215e432298aSXin LI 	/*
216e432298aSXin LI 	 * Make sure that there is no un-initialized padding between the
217e432298aSXin LI 	 * cmsg header and cmsg data and after the cmsg data.
218e432298aSXin LI 	 */
219e432298aSXin LI 	memset(cmh, 0, len);
220e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
221f8829a4aSRandall Stewart 		cmh->cmsg_level = IPPROTO_SCTP;
222e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
223e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_RCVINFO;
224e2e7c62eSMichael Tuexen 		rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
225e2e7c62eSMichael Tuexen 		rcvinfo->rcv_sid = sinfo->sinfo_stream;
226e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
227e2e7c62eSMichael Tuexen 		rcvinfo->rcv_flags = sinfo->sinfo_flags;
228e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
229e2e7c62eSMichael Tuexen 		rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
230e2e7c62eSMichael Tuexen 		rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
231e2e7c62eSMichael Tuexen 		rcvinfo->rcv_context = sinfo->sinfo_context;
232e2e7c62eSMichael Tuexen 		rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
233e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
234e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
235f8829a4aSRandall Stewart 	}
236e2e7c62eSMichael Tuexen 	if (provide_nxt) {
237e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
238e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
239e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_NXTINFO;
240e2e7c62eSMichael Tuexen 		nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
241b70b526dSMichael Tuexen 		nxtinfo->nxt_sid = seinfo->serinfo_next_stream;
242e2e7c62eSMichael Tuexen 		nxtinfo->nxt_flags = 0;
243b70b526dSMichael Tuexen 		if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
244e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_UNORDERED;
245e2e7c62eSMichael Tuexen 		}
246b70b526dSMichael Tuexen 		if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
247e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
248e2e7c62eSMichael Tuexen 		}
249b70b526dSMichael Tuexen 		if (seinfo->serinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
250e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_COMPLETE;
251e2e7c62eSMichael Tuexen 		}
252b70b526dSMichael Tuexen 		nxtinfo->nxt_ppid = seinfo->serinfo_next_ppid;
253b70b526dSMichael Tuexen 		nxtinfo->nxt_length = seinfo->serinfo_next_length;
254b70b526dSMichael Tuexen 		nxtinfo->nxt_assoc_id = seinfo->serinfo_next_aid;
255e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
256e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
257e2e7c62eSMichael Tuexen 	}
258e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
259e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
260e2e7c62eSMichael Tuexen 		outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
261e2e7c62eSMichael Tuexen 		if (use_extended) {
262e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
263e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_EXTRCV;
264e2e7c62eSMichael Tuexen 			memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
265e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
266e2e7c62eSMichael Tuexen 		} else {
267e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
268e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_SNDRCV;
269e2e7c62eSMichael Tuexen 			*outinfo = *sinfo;
270e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
271e2e7c62eSMichael Tuexen 		}
272e2e7c62eSMichael Tuexen 	}
273f8829a4aSRandall Stewart 	return (ret);
274f8829a4aSRandall Stewart }
275f8829a4aSRandall Stewart 
276139bc87fSRandall Stewart 
27777acdc25SRandall Stewart static void
27877acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
27977acdc25SRandall Stewart {
2809b2e0767SRandall Stewart 	uint32_t gap, i, cumackp1;
28177acdc25SRandall Stewart 	int fnd = 0;
28244249214SRandall Stewart 	int in_r = 0, in_nr = 0;
28377acdc25SRandall Stewart 
28477acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
28577acdc25SRandall Stewart 		return;
28677acdc25SRandall Stewart 	}
2879b2e0767SRandall Stewart 	cumackp1 = asoc->cumulative_tsn + 1;
28820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumackp1, tsn)) {
2899b2e0767SRandall Stewart 		/*
2909b2e0767SRandall Stewart 		 * this tsn is behind the cum ack and thus we don't need to
2919b2e0767SRandall Stewart 		 * worry about it being moved from one to the other.
2929b2e0767SRandall Stewart 		 */
2939b2e0767SRandall Stewart 		return;
2949b2e0767SRandall Stewart 	}
29577acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
29644249214SRandall Stewart 	in_r = SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap);
29744249214SRandall Stewart 	in_nr = SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap);
29844249214SRandall Stewart 	if ((in_r == 0) && (in_nr == 0)) {
29944249214SRandall Stewart #ifdef INVARIANTS
30044249214SRandall Stewart 		panic("Things are really messed up now");
30144249214SRandall Stewart #else
302cd3fd531SMichael Tuexen 		SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
30377acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
30477acdc25SRandall Stewart #endif
305b5c16493SMichael Tuexen 	}
30644249214SRandall Stewart 	if (in_nr == 0)
30777acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
30844249214SRandall Stewart 	if (in_r)
30977acdc25SRandall Stewart 		SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
31020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
31177acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
31277acdc25SRandall Stewart 	}
31377acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
31477acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
31520b07a4dSMichael Tuexen 		for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
31677acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
31777acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
31877acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
31977acdc25SRandall Stewart 				fnd = 1;
32077acdc25SRandall Stewart 				break;
32177acdc25SRandall Stewart 			}
32277acdc25SRandall Stewart 		}
32377acdc25SRandall Stewart 		if (!fnd) {
32477acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
32577acdc25SRandall Stewart 		}
32677acdc25SRandall Stewart 	}
32777acdc25SRandall Stewart }
32877acdc25SRandall Stewart 
32944249214SRandall Stewart static int
33044249214SRandall Stewart sctp_place_control_in_stream(struct sctp_stream_in *strm,
33144249214SRandall Stewart     struct sctp_association *asoc,
33244249214SRandall Stewart     struct sctp_queued_to_read *control)
333f8829a4aSRandall Stewart {
33444249214SRandall Stewart 	struct sctp_queued_to_read *at;
33544249214SRandall Stewart 	struct sctp_readhead *q;
33644249214SRandall Stewart 	uint8_t bits, unordered;
337f8829a4aSRandall Stewart 
33844249214SRandall Stewart 	bits = (control->sinfo_flags >> 8);
33944249214SRandall Stewart 	unordered = bits & SCTP_DATA_UNORDERED;
34044249214SRandall Stewart 	if (unordered) {
34144249214SRandall Stewart 		q = &strm->uno_inqueue;
34244249214SRandall Stewart 		if (asoc->idata_supported == 0) {
34344249214SRandall Stewart 			if (!TAILQ_EMPTY(q)) {
344f8829a4aSRandall Stewart 				/*
34544249214SRandall Stewart 				 * Only one stream can be here in old style
34644249214SRandall Stewart 				 * -- abort
347f8829a4aSRandall Stewart 				 */
34844249214SRandall Stewart 				return (-1);
34944249214SRandall Stewart 			}
35044249214SRandall Stewart 			TAILQ_INSERT_TAIL(q, control, next_instrm);
35144249214SRandall Stewart 			control->on_strm_q = SCTP_ON_UNORDERED;
35244249214SRandall Stewart 			return (0);
35344249214SRandall Stewart 		}
35444249214SRandall Stewart 	} else {
35544249214SRandall Stewart 		q = &strm->inqueue;
35644249214SRandall Stewart 	}
35744249214SRandall Stewart 	if ((bits & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
35844249214SRandall Stewart 		control->end_added = control->last_frag_seen = control->first_frag_seen = 1;
35944249214SRandall Stewart 	}
36044249214SRandall Stewart 	if (TAILQ_EMPTY(q)) {
36144249214SRandall Stewart 		/* Empty queue */
36244249214SRandall Stewart 		TAILQ_INSERT_HEAD(q, control, next_instrm);
36344249214SRandall Stewart 		if (unordered) {
36444249214SRandall Stewart 			control->on_strm_q = SCTP_ON_UNORDERED;
36544249214SRandall Stewart 		} else {
36644249214SRandall Stewart 			control->on_strm_q = SCTP_ON_ORDERED;
36744249214SRandall Stewart 		}
36844249214SRandall Stewart 		return (0);
36944249214SRandall Stewart 	} else {
37044249214SRandall Stewart 		TAILQ_FOREACH(at, q, next_instrm) {
37144249214SRandall Stewart 			if (SCTP_TSN_GT(at->msg_id, control->msg_id)) {
37244249214SRandall Stewart 				/*
37344249214SRandall Stewart 				 * one in queue is bigger than the new one,
37444249214SRandall Stewart 				 * insert before this one
37544249214SRandall Stewart 				 */
37644249214SRandall Stewart 				TAILQ_INSERT_BEFORE(at, control, next_instrm);
37744249214SRandall Stewart 				if (unordered) {
37844249214SRandall Stewart 					control->on_strm_q = SCTP_ON_UNORDERED;
37944249214SRandall Stewart 				} else {
38044249214SRandall Stewart 					control->on_strm_q = SCTP_ON_ORDERED;
38144249214SRandall Stewart 				}
38244249214SRandall Stewart 				break;
38344249214SRandall Stewart 			} else if (at->msg_id == control->msg_id) {
38444249214SRandall Stewart 				/*
38544249214SRandall Stewart 				 * Gak, He sent me a duplicate msg id
38644249214SRandall Stewart 				 * number?? return -1 to abort.
38744249214SRandall Stewart 				 */
38844249214SRandall Stewart 				return (-1);
38944249214SRandall Stewart 			} else {
39044249214SRandall Stewart 				if (TAILQ_NEXT(at, next_instrm) == NULL) {
39144249214SRandall Stewart 					/*
39244249214SRandall Stewart 					 * We are at the end, insert it
39344249214SRandall Stewart 					 * after this one
39444249214SRandall Stewart 					 */
39544249214SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
39644249214SRandall Stewart 						sctp_log_strm_del(control, at,
39744249214SRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_TL);
39844249214SRandall Stewart 					}
39944249214SRandall Stewart 					TAILQ_INSERT_AFTER(q,
40044249214SRandall Stewart 					    at, control, next_instrm);
40144249214SRandall Stewart 					if (unordered) {
40244249214SRandall Stewart 						control->on_strm_q = SCTP_ON_UNORDERED;
40344249214SRandall Stewart 					} else {
40444249214SRandall Stewart 						control->on_strm_q = SCTP_ON_ORDERED;
40544249214SRandall Stewart 					}
40644249214SRandall Stewart 					break;
40744249214SRandall Stewart 				}
40844249214SRandall Stewart 			}
40944249214SRandall Stewart 		}
41044249214SRandall Stewart 	}
41144249214SRandall Stewart 	return (0);
41244249214SRandall Stewart }
41344249214SRandall Stewart 
41444249214SRandall Stewart static void
41544249214SRandall Stewart sctp_abort_in_reasm(struct sctp_tcb *stcb,
41644249214SRandall Stewart     struct sctp_queued_to_read *control,
41744249214SRandall Stewart     struct sctp_tmit_chunk *chk,
41844249214SRandall Stewart     int *abort_flag, int opspot)
41944249214SRandall Stewart {
42044249214SRandall Stewart 	char msg[SCTP_DIAG_INFO_LEN];
42144249214SRandall Stewart 	struct mbuf *oper;
42244249214SRandall Stewart 
42344249214SRandall Stewart 	if (stcb->asoc.idata_supported) {
42444249214SRandall Stewart 		snprintf(msg, sizeof(msg),
42544249214SRandall Stewart 		    "Reass %x,CF:%x,TSN=%8.8x,SID=%4.4x,FSN=%8.8x,MID:%8.8x",
42644249214SRandall Stewart 		    opspot,
42744249214SRandall Stewart 		    control->fsn_included,
42844249214SRandall Stewart 		    chk->rec.data.TSN_seq,
42944249214SRandall Stewart 		    chk->rec.data.stream_number,
43044249214SRandall Stewart 		    chk->rec.data.fsn_num, chk->rec.data.stream_seq);
43144249214SRandall Stewart 	} else {
43244249214SRandall Stewart 		snprintf(msg, sizeof(msg),
43344249214SRandall Stewart 		    "Reass %x,CI:%x,TSN=%8.8x,SID=%4.4x,FSN=%4.4x,SSN:%4.4x",
43444249214SRandall Stewart 		    opspot,
43544249214SRandall Stewart 		    control->fsn_included,
43644249214SRandall Stewart 		    chk->rec.data.TSN_seq,
43744249214SRandall Stewart 		    chk->rec.data.stream_number,
43844249214SRandall Stewart 		    chk->rec.data.fsn_num,
43944249214SRandall Stewart 		    (uint16_t) chk->rec.data.stream_seq);
44044249214SRandall Stewart 	}
44144249214SRandall Stewart 	oper = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
442f8829a4aSRandall Stewart 	sctp_m_freem(chk->data);
443f8829a4aSRandall Stewart 	chk->data = NULL;
444689e6a5fSMichael Tuexen 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
44544249214SRandall Stewart 	stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
44644249214SRandall Stewart 	sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
44744249214SRandall Stewart 	*abort_flag = 1;
448f8829a4aSRandall Stewart }
449f8829a4aSRandall Stewart 
45044249214SRandall Stewart static void
451*d1ea5fa9SMichael Tuexen sctp_clean_up_control(struct sctp_tcb *stcb, struct sctp_queued_to_read *control)
45244249214SRandall Stewart {
453f8829a4aSRandall Stewart 	/*
45444249214SRandall Stewart 	 * The control could not be placed and must be cleaned.
455f8829a4aSRandall Stewart 	 */
45644249214SRandall Stewart 	struct sctp_tmit_chunk *chk, *nchk;
457df6e0cc3SRandall Stewart 
45844249214SRandall Stewart 	TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
45944249214SRandall Stewart 		TAILQ_REMOVE(&control->reasm, chk, sctp_next);
46044249214SRandall Stewart 		if (chk->data)
46144249214SRandall Stewart 			sctp_m_freem(chk->data);
462f8829a4aSRandall Stewart 		chk->data = NULL;
463689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
464f8829a4aSRandall Stewart 	}
46544249214SRandall Stewart 	sctp_free_a_readq(stcb, control);
466f8829a4aSRandall Stewart }
467f8829a4aSRandall Stewart 
468f8829a4aSRandall Stewart /*
469f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
470f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
47144249214SRandall Stewart  * append to the so_buf, keep doing so until we are out of order as
47244249214SRandall Stewart  * long as the control's entered are non-fragmented.
473f8829a4aSRandall Stewart  */
474f8829a4aSRandall Stewart static void
47544249214SRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb,
47644249214SRandall Stewart     struct sctp_stream_in *strm,
47744249214SRandall Stewart     struct sctp_association *asoc,
47844249214SRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag, int *need_reasm)
479f8829a4aSRandall Stewart {
480f8829a4aSRandall Stewart 	/*
481f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
482f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
483f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
484cd0a4ff6SPedro F. Giffuni 	 * down in the other type of stream usage that could occur. Send a
485f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
486f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
487f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
488f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
489f8829a4aSRandall Stewart 	 * TSN.
490f8829a4aSRandall Stewart 	 *
491f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
492f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
493f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
494f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
495f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
496f8829a4aSRandall Stewart 	 *
497f8829a4aSRandall Stewart 	 */
498f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
499f8829a4aSRandall Stewart 	int queue_needed;
50044249214SRandall Stewart 	uint32_t nxt_todel;
501ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
502ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
503f8829a4aSRandall Stewart 
504b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
505f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
50680fefe0aSRandall Stewart 	}
50744249214SRandall Stewart 	if (SCTP_MSGID_GT((!asoc->idata_supported), strm->last_sequence_delivered, control->sinfo_ssn)) {
508f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
509f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ: %u delivered: %u from peer, Abort association\n",
510ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
511c40e9cf2SRandall Stewart protocol_error:
512f8829a4aSRandall Stewart 		/*
513f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
514f8829a4aSRandall Stewart 		 * association destruction
515f8829a4aSRandall Stewart 		 */
51644249214SRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next_instrm);
517ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
518ff1ffd74SMichael Tuexen 		    strm->last_sequence_delivered, control->sinfo_tsn,
519ff1ffd74SMichael Tuexen 		    control->sinfo_stream, control->sinfo_ssn);
520ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
52144249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
522ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
523f8829a4aSRandall Stewart 		*abort_flag = 1;
524f8829a4aSRandall Stewart 		return;
525f8829a4aSRandall Stewart 
526f8829a4aSRandall Stewart 	}
52744249214SRandall Stewart 	if ((SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) && (asoc->idata_supported == 0)) {
52844249214SRandall Stewart 		goto protocol_error;
52944249214SRandall Stewart 	}
53044249214SRandall Stewart 	queue_needed = 1;
53144249214SRandall Stewart 	asoc->size_on_all_streams += control->length;
53244249214SRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
53344249214SRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
53444249214SRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
535cf9e47b2SMichael Tuexen #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
536cf9e47b2SMichael Tuexen 		struct socket *so;
537cf9e47b2SMichael Tuexen 
538cf9e47b2SMichael Tuexen 		so = SCTP_INP_SO(stcb->sctp_ep);
539cf9e47b2SMichael Tuexen 		atomic_add_int(&stcb->asoc.refcnt, 1);
540cf9e47b2SMichael Tuexen 		SCTP_TCB_UNLOCK(stcb);
541cf9e47b2SMichael Tuexen 		SCTP_SOCKET_LOCK(so, 1);
542cf9e47b2SMichael Tuexen 		SCTP_TCB_LOCK(stcb);
543cf9e47b2SMichael Tuexen 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
544cf9e47b2SMichael Tuexen 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
545cf9e47b2SMichael Tuexen 			SCTP_SOCKET_UNLOCK(so, 1);
546cf9e47b2SMichael Tuexen 			return;
547cf9e47b2SMichael Tuexen 		}
548cf9e47b2SMichael Tuexen #endif
549f8829a4aSRandall Stewart 		/* can be delivered right away? */
550b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
551f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
55280fefe0aSRandall Stewart 		}
553830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
554f8829a4aSRandall Stewart 		queue_needed = 0;
555f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
556f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
557f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
558b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
559f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
560f8829a4aSRandall Stewart 		    control,
561cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
562574679afSMichael Tuexen 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_LOCKED);
56344249214SRandall Stewart 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next_instrm, at) {
564f8829a4aSRandall Stewart 			/* all delivered */
565f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
56644249214SRandall Stewart 			if ((nxt_todel == control->sinfo_ssn) &&
56744249214SRandall Stewart 			    (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG)) {
568f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
569f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
57044249214SRandall Stewart 				if (control->on_strm_q == SCTP_ON_ORDERED) {
57144249214SRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
57298d5fd97SMichael Tuexen #ifdef INVARIANTS
57344249214SRandall Stewart 				} else {
57444249214SRandall Stewart 					panic("Huh control: %p is on_strm_q: %d",
57544249214SRandall Stewart 					    control, control->on_strm_q);
57698d5fd97SMichael Tuexen #endif
57744249214SRandall Stewart 				}
57844249214SRandall Stewart 				control->on_strm_q = 0;
579f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
580f8829a4aSRandall Stewart 				/*
581f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
582f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
583f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
584f8829a4aSRandall Stewart 				 * can be delivered from the strq.
585f8829a4aSRandall Stewart 				 */
586b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
587f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
588f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
58980fefe0aSRandall Stewart 				}
590b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
591f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
592f8829a4aSRandall Stewart 				    control,
593cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
594cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
595574679afSMichael Tuexen 				    SCTP_SO_LOCKED);
596f8829a4aSRandall Stewart 				continue;
59744249214SRandall Stewart 			} else if (nxt_todel == control->sinfo_ssn) {
59844249214SRandall Stewart 				*need_reasm = 1;
599f8829a4aSRandall Stewart 			}
600f8829a4aSRandall Stewart 			break;
601f8829a4aSRandall Stewart 		}
60244249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
60344249214SRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
60444249214SRandall Stewart #endif
605f8829a4aSRandall Stewart 	}
606f8829a4aSRandall Stewart 	if (queue_needed) {
607f8829a4aSRandall Stewart 		/*
608f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
609f8829a4aSRandall Stewart 		 * to put it on the queue.
610f8829a4aSRandall Stewart 		 */
61144249214SRandall Stewart 		if (sctp_place_control_in_stream(strm, asoc, control)) {
61244249214SRandall Stewart 			snprintf(msg, sizeof(msg),
613f2ea2a2dSMichael Tuexen 			    "Queue to str msg_id: %u duplicate",
61444249214SRandall Stewart 			    control->msg_id);
615*d1ea5fa9SMichael Tuexen 			sctp_clean_up_control(stcb, control);
616b1deed45SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
61744249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
618b1deed45SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
61944249214SRandall Stewart 			*abort_flag = 1;
620f8829a4aSRandall Stewart 		}
621f8829a4aSRandall Stewart 	}
622f8829a4aSRandall Stewart }
623f8829a4aSRandall Stewart 
62444249214SRandall Stewart 
62544249214SRandall Stewart static void
62644249214SRandall Stewart sctp_setup_tail_pointer(struct sctp_queued_to_read *control)
627f8829a4aSRandall Stewart {
62844249214SRandall Stewart 	struct mbuf *m, *prev = NULL;
62944249214SRandall Stewart 	struct sctp_tcb *stcb;
630f8829a4aSRandall Stewart 
63144249214SRandall Stewart 	stcb = control->stcb;
63244249214SRandall Stewart 	control->held_length = 0;
63344249214SRandall Stewart 	control->length = 0;
63444249214SRandall Stewart 	m = control->data;
63544249214SRandall Stewart 	while (m) {
63644249214SRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
63744249214SRandall Stewart 			/* Skip mbufs with NO length */
63844249214SRandall Stewart 			if (prev == NULL) {
63944249214SRandall Stewart 				/* First one */
64044249214SRandall Stewart 				control->data = sctp_m_free(m);
64144249214SRandall Stewart 				m = control->data;
64244249214SRandall Stewart 			} else {
64344249214SRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
64444249214SRandall Stewart 				m = SCTP_BUF_NEXT(prev);
645f8829a4aSRandall Stewart 			}
64644249214SRandall Stewart 			if (m == NULL) {
64744249214SRandall Stewart 				control->tail_mbuf = prev;
648f8829a4aSRandall Stewart 			}
64944249214SRandall Stewart 			continue;
650f8829a4aSRandall Stewart 		}
65144249214SRandall Stewart 		prev = m;
65244249214SRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
65344249214SRandall Stewart 		if (control->on_read_q) {
65444249214SRandall Stewart 			/*
65544249214SRandall Stewart 			 * On read queue so we must increment the SB stuff,
65644249214SRandall Stewart 			 * we assume caller has done any locks of SB.
65744249214SRandall Stewart 			 */
65844249214SRandall Stewart 			sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
659f8829a4aSRandall Stewart 		}
66044249214SRandall Stewart 		m = SCTP_BUF_NEXT(m);
661f8829a4aSRandall Stewart 	}
66244249214SRandall Stewart 	if (prev) {
66344249214SRandall Stewart 		control->tail_mbuf = prev;
66444249214SRandall Stewart 	}
665f8829a4aSRandall Stewart }
666f8829a4aSRandall Stewart 
667f8829a4aSRandall Stewart static void
66844249214SRandall Stewart sctp_add_to_tail_pointer(struct sctp_queued_to_read *control, struct mbuf *m)
669f8829a4aSRandall Stewart {
67044249214SRandall Stewart 	struct mbuf *prev = NULL;
67144249214SRandall Stewart 	struct sctp_tcb *stcb;
672f8829a4aSRandall Stewart 
67344249214SRandall Stewart 	stcb = control->stcb;
67444249214SRandall Stewart 	if (stcb == NULL) {
67598d5fd97SMichael Tuexen #ifdef INVARIANTS
67644249214SRandall Stewart 		panic("Control broken");
67798d5fd97SMichael Tuexen #else
67898d5fd97SMichael Tuexen 		return;
67998d5fd97SMichael Tuexen #endif
68044249214SRandall Stewart 	}
68144249214SRandall Stewart 	if (control->tail_mbuf == NULL) {
68244249214SRandall Stewart 		/* TSNH */
68344249214SRandall Stewart 		control->data = m;
68444249214SRandall Stewart 		sctp_setup_tail_pointer(control);
685f8829a4aSRandall Stewart 		return;
686f8829a4aSRandall Stewart 	}
68744249214SRandall Stewart 	control->tail_mbuf->m_next = m;
68844249214SRandall Stewart 	while (m) {
68944249214SRandall Stewart 		if (SCTP_BUF_LEN(m) == 0) {
69044249214SRandall Stewart 			/* Skip mbufs with NO length */
69144249214SRandall Stewart 			if (prev == NULL) {
69244249214SRandall Stewart 				/* First one */
69344249214SRandall Stewart 				control->tail_mbuf->m_next = sctp_m_free(m);
69444249214SRandall Stewart 				m = control->tail_mbuf->m_next;
69544249214SRandall Stewart 			} else {
69644249214SRandall Stewart 				SCTP_BUF_NEXT(prev) = sctp_m_free(m);
69744249214SRandall Stewart 				m = SCTP_BUF_NEXT(prev);
69844249214SRandall Stewart 			}
69944249214SRandall Stewart 			if (m == NULL) {
70044249214SRandall Stewart 				control->tail_mbuf = prev;
70144249214SRandall Stewart 			}
70244249214SRandall Stewart 			continue;
70344249214SRandall Stewart 		}
70444249214SRandall Stewart 		prev = m;
70544249214SRandall Stewart 		if (control->on_read_q) {
706f8829a4aSRandall Stewart 			/*
70744249214SRandall Stewart 			 * On read queue so we must increment the SB stuff,
70844249214SRandall Stewart 			 * we assume caller has done any locks of SB.
709f8829a4aSRandall Stewart 			 */
71044249214SRandall Stewart 			sctp_sballoc(stcb, &stcb->sctp_socket->so_rcv, m);
71144249214SRandall Stewart 		}
71244249214SRandall Stewart 		atomic_add_int(&control->length, SCTP_BUF_LEN(m));
71344249214SRandall Stewart 		m = SCTP_BUF_NEXT(m);
71444249214SRandall Stewart 	}
71544249214SRandall Stewart 	if (prev) {
71644249214SRandall Stewart 		control->tail_mbuf = prev;
71744249214SRandall Stewart 	}
71844249214SRandall Stewart }
71944249214SRandall Stewart 
72044249214SRandall Stewart static void
72144249214SRandall Stewart sctp_build_readq_entry_from_ctl(struct sctp_queued_to_read *nc, struct sctp_queued_to_read *control)
72244249214SRandall Stewart {
72344249214SRandall Stewart 	memset(nc, 0, sizeof(struct sctp_queued_to_read));
72444249214SRandall Stewart 	nc->sinfo_stream = control->sinfo_stream;
72544249214SRandall Stewart 	nc->sinfo_ssn = control->sinfo_ssn;
72644249214SRandall Stewart 	TAILQ_INIT(&nc->reasm);
72744249214SRandall Stewart 	nc->top_fsn = control->top_fsn;
72844249214SRandall Stewart 	nc->msg_id = control->msg_id;
72944249214SRandall Stewart 	nc->sinfo_flags = control->sinfo_flags;
73044249214SRandall Stewart 	nc->sinfo_ppid = control->sinfo_ppid;
73144249214SRandall Stewart 	nc->sinfo_context = control->sinfo_context;
73244249214SRandall Stewart 	nc->fsn_included = 0xffffffff;
73344249214SRandall Stewart 	nc->sinfo_tsn = control->sinfo_tsn;
73444249214SRandall Stewart 	nc->sinfo_cumtsn = control->sinfo_cumtsn;
73544249214SRandall Stewart 	nc->sinfo_assoc_id = control->sinfo_assoc_id;
73644249214SRandall Stewart 	nc->whoFrom = control->whoFrom;
73744249214SRandall Stewart 	atomic_add_int(&nc->whoFrom->ref_count, 1);
73844249214SRandall Stewart 	nc->stcb = control->stcb;
73944249214SRandall Stewart 	nc->port_from = control->port_from;
74044249214SRandall Stewart }
74144249214SRandall Stewart 
742*d1ea5fa9SMichael Tuexen static void
743*d1ea5fa9SMichael Tuexen sctp_reset_a_control(struct sctp_queued_to_read *control,
744*d1ea5fa9SMichael Tuexen     struct sctp_inpcb *inp, uint32_t tsn)
745*d1ea5fa9SMichael Tuexen {
746*d1ea5fa9SMichael Tuexen 	control->fsn_included = tsn;
747*d1ea5fa9SMichael Tuexen 	if (control->on_read_q) {
748*d1ea5fa9SMichael Tuexen 		/*
749*d1ea5fa9SMichael Tuexen 		 * We have to purge it from there, hopefully this will work
750*d1ea5fa9SMichael Tuexen 		 * :-)
751*d1ea5fa9SMichael Tuexen 		 */
752*d1ea5fa9SMichael Tuexen 		TAILQ_REMOVE(&inp->read_queue, control, next);
753*d1ea5fa9SMichael Tuexen 		control->on_read_q = 0;
754*d1ea5fa9SMichael Tuexen 	}
755*d1ea5fa9SMichael Tuexen }
756*d1ea5fa9SMichael Tuexen 
75744249214SRandall Stewart static int
758*d1ea5fa9SMichael Tuexen sctp_handle_old_unordered_data(struct sctp_tcb *stcb,
759*d1ea5fa9SMichael Tuexen     struct sctp_association *asoc,
760*d1ea5fa9SMichael Tuexen     struct sctp_stream_in *strm,
761*d1ea5fa9SMichael Tuexen     struct sctp_queued_to_read *control,
762*d1ea5fa9SMichael Tuexen     uint32_t pd_point,
763*d1ea5fa9SMichael Tuexen     int inp_read_lock_held)
76444249214SRandall Stewart {
76544249214SRandall Stewart 	/*
76644249214SRandall Stewart 	 * Special handling for the old un-ordered data chunk. All the
76744249214SRandall Stewart 	 * chunks/TSN's go to msg_id 0. So we have to do the old style
76844249214SRandall Stewart 	 * watching to see if we have it all. If you return one, no other
76944249214SRandall Stewart 	 * control entries on the un-ordered queue will be looked at. In
77044249214SRandall Stewart 	 * theory there should be no others entries in reality, unless the
77144249214SRandall Stewart 	 * guy is sending both unordered NDATA and unordered DATA...
77244249214SRandall Stewart 	 */
77344249214SRandall Stewart 	struct sctp_tmit_chunk *chk, *lchk, *tchk;
77444249214SRandall Stewart 	uint32_t fsn;
775643fd575SMichael Tuexen 	struct sctp_queued_to_read *nc;
77644249214SRandall Stewart 	int cnt_added;
77744249214SRandall Stewart 
77844249214SRandall Stewart 	if (control->first_frag_seen == 0) {
77944249214SRandall Stewart 		/* Nothing we can do, we have not seen the first piece yet */
78044249214SRandall Stewart 		return (1);
78144249214SRandall Stewart 	}
78244249214SRandall Stewart 	/* Collapse any we can */
78344249214SRandall Stewart 	cnt_added = 0;
78444249214SRandall Stewart restart:
78544249214SRandall Stewart 	fsn = control->fsn_included + 1;
78644249214SRandall Stewart 	/* Now what can we add? */
78744249214SRandall Stewart 	TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, lchk) {
78844249214SRandall Stewart 		if (chk->rec.data.fsn_num == fsn) {
78944249214SRandall Stewart 			/* Ok lets add it */
790643fd575SMichael Tuexen 			sctp_alloc_a_readq(stcb, nc);
791643fd575SMichael Tuexen 			if (nc == NULL) {
792643fd575SMichael Tuexen 				break;
793643fd575SMichael Tuexen 			}
794643fd575SMichael Tuexen 			memset(nc, 0, sizeof(struct sctp_queued_to_read));
79544249214SRandall Stewart 			TAILQ_REMOVE(&control->reasm, chk, sctp_next);
796*d1ea5fa9SMichael Tuexen 			sctp_add_chk_to_control(control, strm, stcb, asoc, chk, SCTP_READ_LOCK_NOT_HELD);
79744249214SRandall Stewart 			fsn++;
79844249214SRandall Stewart 			cnt_added++;
79944249214SRandall Stewart 			chk = NULL;
80044249214SRandall Stewart 			if (control->end_added) {
80144249214SRandall Stewart 				/* We are done */
80244249214SRandall Stewart 				if (!TAILQ_EMPTY(&control->reasm)) {
80344249214SRandall Stewart 					/*
80444249214SRandall Stewart 					 * Ok we have to move anything left
80544249214SRandall Stewart 					 * on the control queue to a new
80644249214SRandall Stewart 					 * control.
80744249214SRandall Stewart 					 */
80844249214SRandall Stewart 					sctp_build_readq_entry_from_ctl(nc, control);
80944249214SRandall Stewart 					tchk = TAILQ_FIRST(&control->reasm);
81044249214SRandall Stewart 					if (tchk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
81144249214SRandall Stewart 						TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
81244249214SRandall Stewart 						nc->first_frag_seen = 1;
81344249214SRandall Stewart 						nc->fsn_included = tchk->rec.data.fsn_num;
81444249214SRandall Stewart 						nc->data = tchk->data;
815*d1ea5fa9SMichael Tuexen 						nc->sinfo_ppid = tchk->rec.data.payloadtype;
816*d1ea5fa9SMichael Tuexen 						nc->sinfo_tsn = tchk->rec.data.TSN_seq;
81744249214SRandall Stewart 						sctp_mark_non_revokable(asoc, tchk->rec.data.TSN_seq);
81844249214SRandall Stewart 						tchk->data = NULL;
81944249214SRandall Stewart 						sctp_free_a_chunk(stcb, tchk, SCTP_SO_NOT_LOCKED);
82044249214SRandall Stewart 						sctp_setup_tail_pointer(nc);
82144249214SRandall Stewart 						tchk = TAILQ_FIRST(&control->reasm);
82244249214SRandall Stewart 					}
82344249214SRandall Stewart 					/* Spin the rest onto the queue */
82444249214SRandall Stewart 					while (tchk) {
82544249214SRandall Stewart 						TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
82644249214SRandall Stewart 						TAILQ_INSERT_TAIL(&nc->reasm, tchk, sctp_next);
82744249214SRandall Stewart 						tchk = TAILQ_FIRST(&control->reasm);
82844249214SRandall Stewart 					}
82944249214SRandall Stewart 					/*
83044249214SRandall Stewart 					 * Now lets add it to the queue
83144249214SRandall Stewart 					 * after removing control
83244249214SRandall Stewart 					 */
83344249214SRandall Stewart 					TAILQ_INSERT_TAIL(&strm->uno_inqueue, nc, next_instrm);
83444249214SRandall Stewart 					nc->on_strm_q = SCTP_ON_UNORDERED;
83544249214SRandall Stewart 					if (control->on_strm_q) {
83644249214SRandall Stewart 						TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
83744249214SRandall Stewart 						control->on_strm_q = 0;
83844249214SRandall Stewart 					}
83944249214SRandall Stewart 				}
84044249214SRandall Stewart 				if (control->pdapi_started) {
84144249214SRandall Stewart 					strm->pd_api_started = 0;
84244249214SRandall Stewart 					control->pdapi_started = 0;
84344249214SRandall Stewart 				}
84444249214SRandall Stewart 				if (control->on_strm_q) {
84544249214SRandall Stewart 					TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
84644249214SRandall Stewart 					control->on_strm_q = 0;
84726f0250aSMichael Tuexen 					SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
84844249214SRandall Stewart 				}
849c09a1534SMichael Tuexen 				if (control->on_read_q == 0) {
850c09a1534SMichael Tuexen 					sctp_add_to_readq(stcb->sctp_ep, stcb, control,
851c09a1534SMichael Tuexen 					    &stcb->sctp_socket->so_rcv, control->end_added,
852*d1ea5fa9SMichael Tuexen 					    inp_read_lock_held, SCTP_SO_NOT_LOCKED);
853c09a1534SMichael Tuexen 				}
854b1deed45SMichael Tuexen 				sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
8550fa7377aSMichael Tuexen 				if ((nc->first_frag_seen) && !TAILQ_EMPTY(&nc->reasm)) {
85644249214SRandall Stewart 					/*
85744249214SRandall Stewart 					 * Switch to the new guy and
85844249214SRandall Stewart 					 * continue
85944249214SRandall Stewart 					 */
86044249214SRandall Stewart 					control = nc;
86144249214SRandall Stewart 					goto restart;
862643fd575SMichael Tuexen 				} else {
863*d1ea5fa9SMichael Tuexen 					if (nc->on_strm_q == 0) {
864643fd575SMichael Tuexen 						sctp_free_a_readq(stcb, nc);
86544249214SRandall Stewart 					}
866*d1ea5fa9SMichael Tuexen 				}
86744249214SRandall Stewart 				return (1);
868643fd575SMichael Tuexen 			} else {
869643fd575SMichael Tuexen 				sctp_free_a_readq(stcb, nc);
87044249214SRandall Stewart 			}
87144249214SRandall Stewart 		} else {
87244249214SRandall Stewart 			/* Can't add more */
87344249214SRandall Stewart 			break;
87444249214SRandall Stewart 		}
87544249214SRandall Stewart 	}
87644249214SRandall Stewart 	if ((control->length > pd_point) && (strm->pd_api_started == 0)) {
877c09a1534SMichael Tuexen 		strm->pd_api_started = 1;
878c09a1534SMichael Tuexen 		control->pdapi_started = 1;
87944249214SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb, control,
88044249214SRandall Stewart 		    &stcb->sctp_socket->so_rcv, control->end_added,
881*d1ea5fa9SMichael Tuexen 		    inp_read_lock_held, SCTP_SO_NOT_LOCKED);
882b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
88344249214SRandall Stewart 		return (0);
88444249214SRandall Stewart 	} else {
88544249214SRandall Stewart 		return (1);
88644249214SRandall Stewart 	}
88744249214SRandall Stewart }
88844249214SRandall Stewart 
88944249214SRandall Stewart static void
890*d1ea5fa9SMichael Tuexen sctp_inject_old_unordered_data(struct sctp_tcb *stcb,
891*d1ea5fa9SMichael Tuexen     struct sctp_association *asoc,
89244249214SRandall Stewart     struct sctp_queued_to_read *control,
89344249214SRandall Stewart     struct sctp_tmit_chunk *chk,
89444249214SRandall Stewart     int *abort_flag)
89544249214SRandall Stewart {
89644249214SRandall Stewart 	struct sctp_tmit_chunk *at;
897*d1ea5fa9SMichael Tuexen 	int inserted;
89844249214SRandall Stewart 
89944249214SRandall Stewart 	/*
90044249214SRandall Stewart 	 * Here we need to place the chunk into the control structure sorted
90144249214SRandall Stewart 	 * in the correct order.
90244249214SRandall Stewart 	 */
90344249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
90444249214SRandall Stewart 		/* Its the very first one. */
90544249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
906f2ea2a2dSMichael Tuexen 		    "chunk is a first fsn: %u becomes fsn_included\n",
90744249214SRandall Stewart 		    chk->rec.data.fsn_num);
90844249214SRandall Stewart 		if (control->first_frag_seen) {
90944249214SRandall Stewart 			/*
91044249214SRandall Stewart 			 * In old un-ordered we can reassembly on one
91144249214SRandall Stewart 			 * control multiple messages. As long as the next
91244249214SRandall Stewart 			 * FIRST is greater then the old first (TSN i.e. FSN
91344249214SRandall Stewart 			 * wise)
91444249214SRandall Stewart 			 */
91544249214SRandall Stewart 			struct mbuf *tdata;
91644249214SRandall Stewart 			uint32_t tmp;
91744249214SRandall Stewart 
91844249214SRandall Stewart 			if (SCTP_TSN_GT(chk->rec.data.fsn_num, control->fsn_included)) {
91944249214SRandall Stewart 				/*
92044249214SRandall Stewart 				 * Easy way the start of a new guy beyond
92144249214SRandall Stewart 				 * the lowest
92244249214SRandall Stewart 				 */
92344249214SRandall Stewart 				goto place_chunk;
92444249214SRandall Stewart 			}
92544249214SRandall Stewart 			if ((chk->rec.data.fsn_num == control->fsn_included) ||
92644249214SRandall Stewart 			    (control->pdapi_started)) {
92744249214SRandall Stewart 				/*
92844249214SRandall Stewart 				 * Ok this should not happen, if it does we
92944249214SRandall Stewart 				 * started the pd-api on the higher TSN
93044249214SRandall Stewart 				 * (since the equals part is a TSN failure
93144249214SRandall Stewart 				 * it must be that).
93244249214SRandall Stewart 				 *
93344249214SRandall Stewart 				 * We are completly hosed in that case since I
93444249214SRandall Stewart 				 * have no way to recover. This really will
93544249214SRandall Stewart 				 * only happen if we can get more TSN's
93644249214SRandall Stewart 				 * higher before the pd-api-point.
93744249214SRandall Stewart 				 */
938b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control, chk,
93944249214SRandall Stewart 				    abort_flag,
94044249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
94144249214SRandall Stewart 
94244249214SRandall Stewart 				return;
94344249214SRandall Stewart 			}
94444249214SRandall Stewart 			/*
94544249214SRandall Stewart 			 * Ok we have two firsts and the one we just got is
94644249214SRandall Stewart 			 * smaller than the one we previously placed.. yuck!
94744249214SRandall Stewart 			 * We must swap them out.
94844249214SRandall Stewart 			 */
94944249214SRandall Stewart 			/* swap the mbufs */
95044249214SRandall Stewart 			tdata = control->data;
95144249214SRandall Stewart 			control->data = chk->data;
95244249214SRandall Stewart 			chk->data = tdata;
953*d1ea5fa9SMichael Tuexen 			/* Save the lengths */
954*d1ea5fa9SMichael Tuexen 			chk->send_size = control->length;
955*d1ea5fa9SMichael Tuexen 			/* Recompute length of control and tail pointer */
956*d1ea5fa9SMichael Tuexen 			sctp_setup_tail_pointer(control);
95744249214SRandall Stewart 			/* Fix the FSN included */
95844249214SRandall Stewart 			tmp = control->fsn_included;
95944249214SRandall Stewart 			control->fsn_included = chk->rec.data.fsn_num;
96044249214SRandall Stewart 			chk->rec.data.fsn_num = tmp;
961*d1ea5fa9SMichael Tuexen 			/* Fix the TSN included */
962*d1ea5fa9SMichael Tuexen 			tmp = control->sinfo_tsn;
963*d1ea5fa9SMichael Tuexen 			control->sinfo_tsn = chk->rec.data.TSN_seq;
964*d1ea5fa9SMichael Tuexen 			chk->rec.data.TSN_seq = tmp;
965*d1ea5fa9SMichael Tuexen 			/* Fix the PPID included */
966*d1ea5fa9SMichael Tuexen 			tmp = control->sinfo_ppid;
967*d1ea5fa9SMichael Tuexen 			control->sinfo_ppid = chk->rec.data.payloadtype;
968*d1ea5fa9SMichael Tuexen 			chk->rec.data.payloadtype = tmp;
969*d1ea5fa9SMichael Tuexen 			/* Fix tail pointer */
97044249214SRandall Stewart 			goto place_chunk;
97144249214SRandall Stewart 		}
97244249214SRandall Stewart 		control->first_frag_seen = 1;
97344249214SRandall Stewart 		control->top_fsn = control->fsn_included = chk->rec.data.fsn_num;
974*d1ea5fa9SMichael Tuexen 		control->sinfo_tsn = chk->rec.data.TSN_seq;
975*d1ea5fa9SMichael Tuexen 		control->sinfo_ppid = chk->rec.data.payloadtype;
97644249214SRandall Stewart 		control->data = chk->data;
97744249214SRandall Stewart 		sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
97844249214SRandall Stewart 		chk->data = NULL;
97944249214SRandall Stewart 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
98044249214SRandall Stewart 		sctp_setup_tail_pointer(control);
98144249214SRandall Stewart 		return;
98244249214SRandall Stewart 	}
98344249214SRandall Stewart place_chunk:
984*d1ea5fa9SMichael Tuexen 	inserted = 0;
98544249214SRandall Stewart 	TAILQ_FOREACH(at, &control->reasm, sctp_next) {
98644249214SRandall Stewart 		if (SCTP_TSN_GT(at->rec.data.fsn_num, chk->rec.data.fsn_num)) {
98744249214SRandall Stewart 			/*
98844249214SRandall Stewart 			 * This one in queue is bigger than the new one,
98944249214SRandall Stewart 			 * insert the new one before at.
99044249214SRandall Stewart 			 */
99144249214SRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
99244249214SRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
99344249214SRandall Stewart 			inserted = 1;
99444249214SRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
99544249214SRandall Stewart 			break;
99644249214SRandall Stewart 		} else if (at->rec.data.fsn_num == chk->rec.data.fsn_num) {
99744249214SRandall Stewart 			/*
99844249214SRandall Stewart 			 * They sent a duplicate fsn number. This really
99944249214SRandall Stewart 			 * should not happen since the FSN is a TSN and it
100044249214SRandall Stewart 			 * should have been dropped earlier.
100144249214SRandall Stewart 			 */
1002b1deed45SMichael Tuexen 			sctp_abort_in_reasm(stcb, control, chk,
100344249214SRandall Stewart 			    abort_flag,
100444249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
100544249214SRandall Stewart 			return;
100644249214SRandall Stewart 		}
100744249214SRandall Stewart 	}
100844249214SRandall Stewart 	if (inserted == 0) {
100944249214SRandall Stewart 		/* Its at the end */
101044249214SRandall Stewart 		asoc->size_on_reasm_queue += chk->send_size;
101144249214SRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
101244249214SRandall Stewart 		control->top_fsn = chk->rec.data.fsn_num;
101344249214SRandall Stewart 		TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
101444249214SRandall Stewart 	}
101544249214SRandall Stewart }
101644249214SRandall Stewart 
101744249214SRandall Stewart static int
1018*d1ea5fa9SMichael Tuexen sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc,
1019*d1ea5fa9SMichael Tuexen     struct sctp_stream_in *strm, int inp_read_lock_held)
102044249214SRandall Stewart {
102144249214SRandall Stewart 	/*
102244249214SRandall Stewart 	 * Given a stream, strm, see if any of the SSN's on it that are
102344249214SRandall Stewart 	 * fragmented are ready to deliver. If so go ahead and place them on
102444249214SRandall Stewart 	 * the read queue. In so placing if we have hit the end, then we
102544249214SRandall Stewart 	 * need to remove them from the stream's queue.
102644249214SRandall Stewart 	 */
102744249214SRandall Stewart 	struct sctp_queued_to_read *control, *nctl = NULL;
102844249214SRandall Stewart 	uint32_t next_to_del;
102944249214SRandall Stewart 	uint32_t pd_point;
103044249214SRandall Stewart 	int ret = 0;
103144249214SRandall Stewart 
1032810ec536SMichael Tuexen 	if (stcb->sctp_socket) {
1033d4d23375SMichael Tuexen 		pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
1034810ec536SMichael Tuexen 		    stcb->sctp_ep->partial_delivery_point);
1035810ec536SMichael Tuexen 	} else {
1036810ec536SMichael Tuexen 		pd_point = stcb->sctp_ep->partial_delivery_point;
1037810ec536SMichael Tuexen 	}
103844249214SRandall Stewart 	control = TAILQ_FIRST(&strm->uno_inqueue);
1039*d1ea5fa9SMichael Tuexen 
104044249214SRandall Stewart 	if ((control) &&
104144249214SRandall Stewart 	    (asoc->idata_supported == 0)) {
104244249214SRandall Stewart 		/* Special handling needed for "old" data format */
1043*d1ea5fa9SMichael Tuexen 		if (sctp_handle_old_unordered_data(stcb, asoc, strm, control, pd_point, inp_read_lock_held)) {
104444249214SRandall Stewart 			goto done_un;
1045f8829a4aSRandall Stewart 		}
1046f8829a4aSRandall Stewart 	}
104744249214SRandall Stewart 	if (strm->pd_api_started) {
104844249214SRandall Stewart 		/* Can't add more */
104944249214SRandall Stewart 		return (0);
105044249214SRandall Stewart 	}
105144249214SRandall Stewart 	while (control) {
1052f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_XXX, "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u -uo\n",
105344249214SRandall Stewart 		    control, control->end_added, control->sinfo_ssn, control->top_fsn, control->fsn_included);
105444249214SRandall Stewart 		nctl = TAILQ_NEXT(control, next_instrm);
105544249214SRandall Stewart 		if (control->end_added) {
105644249214SRandall Stewart 			/* We just put the last bit on */
105744249214SRandall Stewart 			if (control->on_strm_q) {
105898d5fd97SMichael Tuexen #ifdef INVARIANTS
105944249214SRandall Stewart 				if (control->on_strm_q != SCTP_ON_UNORDERED) {
106044249214SRandall Stewart 					panic("Huh control: %p on_q: %d -- not unordered?",
106144249214SRandall Stewart 					    control, control->on_strm_q);
106244249214SRandall Stewart 				}
106398d5fd97SMichael Tuexen #endif
106426f0250aSMichael Tuexen 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
106544249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
106644249214SRandall Stewart 				control->on_strm_q = 0;
106744249214SRandall Stewart 			}
106844249214SRandall Stewart 			if (control->on_read_q == 0) {
106944249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
107044249214SRandall Stewart 				    control,
107144249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
1072*d1ea5fa9SMichael Tuexen 				    inp_read_lock_held, SCTP_SO_NOT_LOCKED);
107344249214SRandall Stewart 			}
1074f8829a4aSRandall Stewart 		} else {
107544249214SRandall Stewart 			/* Can we do a PD-API for this un-ordered guy? */
107644249214SRandall Stewart 			if ((control->length >= pd_point) && (strm->pd_api_started == 0)) {
107744249214SRandall Stewart 				strm->pd_api_started = 1;
107844249214SRandall Stewart 				control->pdapi_started = 1;
107944249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
108044249214SRandall Stewart 				    control,
108144249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
1082*d1ea5fa9SMichael Tuexen 				    inp_read_lock_held, SCTP_SO_NOT_LOCKED);
108344249214SRandall Stewart 
108444249214SRandall Stewart 				break;
1085139bc87fSRandall Stewart 			}
1086f8829a4aSRandall Stewart 		}
108744249214SRandall Stewart 		control = nctl;
108844249214SRandall Stewart 	}
108944249214SRandall Stewart done_un:
109044249214SRandall Stewart 	control = TAILQ_FIRST(&strm->inqueue);
109144249214SRandall Stewart 	if (strm->pd_api_started) {
109244249214SRandall Stewart 		/* Can't add more */
109344249214SRandall Stewart 		return (0);
109444249214SRandall Stewart 	}
109544249214SRandall Stewart 	if (control == NULL) {
109644249214SRandall Stewart 		return (ret);
109744249214SRandall Stewart 	}
109844249214SRandall Stewart 	if (strm->last_sequence_delivered == control->sinfo_ssn) {
109944249214SRandall Stewart 		/*
110044249214SRandall Stewart 		 * Ok the guy at the top was being partially delivered
110144249214SRandall Stewart 		 * completed, so we remove it. Note the pd_api flag was
110244249214SRandall Stewart 		 * taken off when the chunk was merged on in
110344249214SRandall Stewart 		 * sctp_queue_data_for_reasm below.
110444249214SRandall Stewart 		 */
110544249214SRandall Stewart 		nctl = TAILQ_NEXT(control, next_instrm);
110644249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1107f2ea2a2dSMichael Tuexen 		    "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (lastdel: %u)- o\n",
110844249214SRandall Stewart 		    control, control->end_added, control->sinfo_ssn,
110944249214SRandall Stewart 		    control->top_fsn, control->fsn_included,
111044249214SRandall Stewart 		    strm->last_sequence_delivered);
111144249214SRandall Stewart 		if (control->end_added) {
111244249214SRandall Stewart 			if (control->on_strm_q) {
111398d5fd97SMichael Tuexen #ifdef INVARIANTS
111444249214SRandall Stewart 				if (control->on_strm_q != SCTP_ON_ORDERED) {
111544249214SRandall Stewart 					panic("Huh control: %p on_q: %d -- not ordered?",
111644249214SRandall Stewart 					    control, control->on_strm_q);
111744249214SRandall Stewart 				}
111898d5fd97SMichael Tuexen #endif
111926f0250aSMichael Tuexen 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
112044249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
112144249214SRandall Stewart 				control->on_strm_q = 0;
112244249214SRandall Stewart 			}
1123c09a1534SMichael Tuexen 			if (strm->pd_api_started && control->pdapi_started) {
1124c09a1534SMichael Tuexen 				control->pdapi_started = 0;
1125c09a1534SMichael Tuexen 				strm->pd_api_started = 0;
1126c09a1534SMichael Tuexen 			}
112744249214SRandall Stewart 			if (control->on_read_q == 0) {
112844249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
112944249214SRandall Stewart 				    control,
113044249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
1131*d1ea5fa9SMichael Tuexen 				    inp_read_lock_held, SCTP_SO_NOT_LOCKED);
113244249214SRandall Stewart 			}
113344249214SRandall Stewart 			control = nctl;
113444249214SRandall Stewart 		}
113544249214SRandall Stewart 	}
113644249214SRandall Stewart 	if (strm->pd_api_started) {
113744249214SRandall Stewart 		/*
113844249214SRandall Stewart 		 * Can't add more must have gotten an un-ordered above being
113944249214SRandall Stewart 		 * partially delivered.
114044249214SRandall Stewart 		 */
114144249214SRandall Stewart 		return (0);
114244249214SRandall Stewart 	}
114344249214SRandall Stewart deliver_more:
114444249214SRandall Stewart 	next_to_del = strm->last_sequence_delivered + 1;
114544249214SRandall Stewart 	if (control) {
114644249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1147f2ea2a2dSMichael Tuexen 		    "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (nxtdel: %u)- o\n",
114844249214SRandall Stewart 		    control, control->end_added, control->sinfo_ssn, control->top_fsn, control->fsn_included,
114944249214SRandall Stewart 		    next_to_del);
115044249214SRandall Stewart 		nctl = TAILQ_NEXT(control, next_instrm);
115144249214SRandall Stewart 		if ((control->sinfo_ssn == next_to_del) &&
115244249214SRandall Stewart 		    (control->first_frag_seen)) {
1153c09a1534SMichael Tuexen 			int done;
1154c09a1534SMichael Tuexen 
115544249214SRandall Stewart 			/* Ok we can deliver it onto the stream. */
115644249214SRandall Stewart 			if (control->end_added) {
115744249214SRandall Stewart 				/* We are done with it afterwards */
115844249214SRandall Stewart 				if (control->on_strm_q) {
115998d5fd97SMichael Tuexen #ifdef INVARIANTS
116044249214SRandall Stewart 					if (control->on_strm_q != SCTP_ON_ORDERED) {
116144249214SRandall Stewart 						panic("Huh control: %p on_q: %d -- not ordered?",
116244249214SRandall Stewart 						    control, control->on_strm_q);
116344249214SRandall Stewart 					}
116498d5fd97SMichael Tuexen #endif
116526f0250aSMichael Tuexen 					SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
116644249214SRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
116744249214SRandall Stewart 					control->on_strm_q = 0;
116844249214SRandall Stewart 				}
116944249214SRandall Stewart 				ret++;
117044249214SRandall Stewart 			}
117144249214SRandall Stewart 			if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
117244249214SRandall Stewart 				/*
117344249214SRandall Stewart 				 * A singleton now slipping through - mark
117444249214SRandall Stewart 				 * it non-revokable too
117544249214SRandall Stewart 				 */
117644249214SRandall Stewart 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
117744249214SRandall Stewart 			} else if (control->end_added == 0) {
117844249214SRandall Stewart 				/*
117944249214SRandall Stewart 				 * Check if we can defer adding until its
118044249214SRandall Stewart 				 * all there
118144249214SRandall Stewart 				 */
118244249214SRandall Stewart 				if ((control->length < pd_point) || (strm->pd_api_started)) {
118344249214SRandall Stewart 					/*
118444249214SRandall Stewart 					 * Don't need it or cannot add more
118544249214SRandall Stewart 					 * (one being delivered that way)
118644249214SRandall Stewart 					 */
118744249214SRandall Stewart 					goto out;
118844249214SRandall Stewart 				}
118944249214SRandall Stewart 			}
1190c09a1534SMichael Tuexen 			done = (control->end_added) && (control->last_frag_seen);
119144249214SRandall Stewart 			if (control->on_read_q == 0) {
119244249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
119344249214SRandall Stewart 				    control,
119444249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
1195*d1ea5fa9SMichael Tuexen 				    inp_read_lock_held, SCTP_SO_NOT_LOCKED);
119644249214SRandall Stewart 			}
119744249214SRandall Stewart 			strm->last_sequence_delivered = next_to_del;
1198c09a1534SMichael Tuexen 			if (done) {
119944249214SRandall Stewart 				control = nctl;
120044249214SRandall Stewart 				goto deliver_more;
120144249214SRandall Stewart 			} else {
120244249214SRandall Stewart 				/* We are now doing PD API */
120344249214SRandall Stewart 				strm->pd_api_started = 1;
120444249214SRandall Stewart 				control->pdapi_started = 1;
120544249214SRandall Stewart 			}
120644249214SRandall Stewart 		}
120744249214SRandall Stewart 	}
120844249214SRandall Stewart out:
120944249214SRandall Stewart 	return (ret);
121044249214SRandall Stewart }
121144249214SRandall Stewart 
1212*d1ea5fa9SMichael Tuexen 
121344249214SRandall Stewart void
121444249214SRandall Stewart sctp_add_chk_to_control(struct sctp_queued_to_read *control,
121544249214SRandall Stewart     struct sctp_stream_in *strm,
121644249214SRandall Stewart     struct sctp_tcb *stcb, struct sctp_association *asoc,
1217*d1ea5fa9SMichael Tuexen     struct sctp_tmit_chunk *chk, int hold_rlock)
121844249214SRandall Stewart {
121944249214SRandall Stewart 	/*
122044249214SRandall Stewart 	 * Given a control and a chunk, merge the data from the chk onto the
122144249214SRandall Stewart 	 * control and free up the chunk resources.
122244249214SRandall Stewart 	 */
122344249214SRandall Stewart 	int i_locked = 0;
122444249214SRandall Stewart 
1225*d1ea5fa9SMichael Tuexen 	if (control->on_read_q && (hold_rlock == 0)) {
122644249214SRandall Stewart 		/*
122744249214SRandall Stewart 		 * Its being pd-api'd so we must do some locks.
122844249214SRandall Stewart 		 */
122944249214SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
123044249214SRandall Stewart 		i_locked = 1;
123144249214SRandall Stewart 	}
123244249214SRandall Stewart 	if (control->data == NULL) {
123344249214SRandall Stewart 		control->data = chk->data;
123444249214SRandall Stewart 		sctp_setup_tail_pointer(control);
123544249214SRandall Stewart 	} else {
123644249214SRandall Stewart 		sctp_add_to_tail_pointer(control, chk->data);
123744249214SRandall Stewart 	}
123844249214SRandall Stewart 	control->fsn_included = chk->rec.data.fsn_num;
123944249214SRandall Stewart 	asoc->size_on_reasm_queue -= chk->send_size;
124044249214SRandall Stewart 	sctp_ucount_decr(asoc->cnt_on_reasm_queue);
124144249214SRandall Stewart 	sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
124244249214SRandall Stewart 	chk->data = NULL;
124344249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
124444249214SRandall Stewart 		control->first_frag_seen = 1;
124544249214SRandall Stewart 	}
124644249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
124744249214SRandall Stewart 		/* Its complete */
124844249214SRandall Stewart 		if ((control->on_strm_q) && (control->on_read_q)) {
124944249214SRandall Stewart 			if (control->pdapi_started) {
125044249214SRandall Stewart 				control->pdapi_started = 0;
125144249214SRandall Stewart 				strm->pd_api_started = 0;
125244249214SRandall Stewart 			}
125344249214SRandall Stewart 			if (control->on_strm_q == SCTP_ON_UNORDERED) {
125444249214SRandall Stewart 				/* Unordered */
125544249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
125644249214SRandall Stewart 				control->on_strm_q = 0;
125744249214SRandall Stewart 			} else if (control->on_strm_q == SCTP_ON_ORDERED) {
125844249214SRandall Stewart 				/* Ordered */
125944249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
126044249214SRandall Stewart 				control->on_strm_q = 0;
126198d5fd97SMichael Tuexen #ifdef INVARIANTS
126244249214SRandall Stewart 			} else if (control->on_strm_q) {
126344249214SRandall Stewart 				panic("Unknown state on ctrl: %p on_strm_q: %d", control,
126444249214SRandall Stewart 				    control->on_strm_q);
126598d5fd97SMichael Tuexen #endif
126644249214SRandall Stewart 			}
126744249214SRandall Stewart 		}
126844249214SRandall Stewart 		control->end_added = 1;
126944249214SRandall Stewart 		control->last_frag_seen = 1;
127044249214SRandall Stewart 	}
127144249214SRandall Stewart 	if (i_locked) {
127244249214SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
127344249214SRandall Stewart 	}
127444249214SRandall Stewart 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1275f8829a4aSRandall Stewart }
1276f8829a4aSRandall Stewart 
1277f8829a4aSRandall Stewart /*
1278f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
1279f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
1280f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
1281f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
1282f8829a4aSRandall Stewart  */
1283f8829a4aSRandall Stewart static void
1284f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
128544249214SRandall Stewart     struct sctp_stream_in *strm,
128644249214SRandall Stewart     struct sctp_queued_to_read *control,
128744249214SRandall Stewart     struct sctp_tmit_chunk *chk,
128844249214SRandall Stewart     int created_control,
128944249214SRandall Stewart     int *abort_flag, uint32_t tsn)
1290f8829a4aSRandall Stewart {
129144249214SRandall Stewart 	uint32_t next_fsn;
129244249214SRandall Stewart 	struct sctp_tmit_chunk *at, *nat;
1293c09a1534SMichael Tuexen 	int do_wakeup, unordered;
1294f8829a4aSRandall Stewart 
1295f8829a4aSRandall Stewart 	/*
129644249214SRandall Stewart 	 * For old un-ordered data chunks.
1297f8829a4aSRandall Stewart 	 */
129844249214SRandall Stewart 	if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
129944249214SRandall Stewart 		unordered = 1;
1300f8829a4aSRandall Stewart 	} else {
130144249214SRandall Stewart 		unordered = 0;
130244249214SRandall Stewart 	}
130344249214SRandall Stewart 	/* Must be added to the stream-in queue */
130444249214SRandall Stewart 	if (created_control) {
130544249214SRandall Stewart 		if (sctp_place_control_in_stream(strm, asoc, control)) {
130644249214SRandall Stewart 			/* Duplicate SSN? */
1307*d1ea5fa9SMichael Tuexen 			sctp_clean_up_control(stcb, control);
1308b1deed45SMichael Tuexen 			sctp_abort_in_reasm(stcb, control, chk,
130944249214SRandall Stewart 			    abort_flag,
131044249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
131144249214SRandall Stewart 			return;
131244249214SRandall Stewart 		}
131344249214SRandall Stewart 		if ((tsn == (asoc->cumulative_tsn + 1) && (asoc->idata_supported == 0))) {
1314f8829a4aSRandall Stewart 			/*
131544249214SRandall Stewart 			 * Ok we created this control and now lets validate
131644249214SRandall Stewart 			 * that its legal i.e. there is a B bit set, if not
131744249214SRandall Stewart 			 * and we have up to the cum-ack then its invalid.
131844249214SRandall Stewart 			 */
131944249214SRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
1320b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control, chk,
132144249214SRandall Stewart 				    abort_flag,
132244249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
132344249214SRandall Stewart 				return;
132444249214SRandall Stewart 			}
132544249214SRandall Stewart 		}
132644249214SRandall Stewart 	}
1327a39ddef0SMichael Tuexen 	if ((asoc->idata_supported == 0) && (unordered == 1)) {
1328*d1ea5fa9SMichael Tuexen 		sctp_inject_old_unordered_data(stcb, asoc, control, chk, abort_flag);
132944249214SRandall Stewart 		return;
133044249214SRandall Stewart 	}
133144249214SRandall Stewart 	/*
133244249214SRandall Stewart 	 * Ok we must queue the chunk into the reasembly portion: o if its
133344249214SRandall Stewart 	 * the first it goes to the control mbuf. o if its not first but the
133444249214SRandall Stewart 	 * next in sequence it goes to the control, and each succeeding one
133544249214SRandall Stewart 	 * in order also goes. o if its not in order we place it on the list
133644249214SRandall Stewart 	 * in its place.
133744249214SRandall Stewart 	 */
133844249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
133944249214SRandall Stewart 		/* Its the very first one. */
134044249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1341f2ea2a2dSMichael Tuexen 		    "chunk is a first fsn: %u becomes fsn_included\n",
134244249214SRandall Stewart 		    chk->rec.data.fsn_num);
134344249214SRandall Stewart 		if (control->first_frag_seen) {
134444249214SRandall Stewart 			/*
134544249214SRandall Stewart 			 * Error on senders part, they either sent us two
134644249214SRandall Stewart 			 * data chunks with FIRST, or they sent two
134744249214SRandall Stewart 			 * un-ordered chunks that were fragmented at the
134844249214SRandall Stewart 			 * same time in the same stream.
134944249214SRandall Stewart 			 */
1350b1deed45SMichael Tuexen 			sctp_abort_in_reasm(stcb, control, chk,
135144249214SRandall Stewart 			    abort_flag,
135244249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
135344249214SRandall Stewart 			return;
135444249214SRandall Stewart 		}
135544249214SRandall Stewart 		control->first_frag_seen = 1;
135644249214SRandall Stewart 		control->fsn_included = chk->rec.data.fsn_num;
135744249214SRandall Stewart 		control->data = chk->data;
135844249214SRandall Stewart 		sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
135944249214SRandall Stewart 		chk->data = NULL;
136044249214SRandall Stewart 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
136144249214SRandall Stewart 		sctp_setup_tail_pointer(control);
136244249214SRandall Stewart 	} else {
136344249214SRandall Stewart 		/* Place the chunk in our list */
136444249214SRandall Stewart 		int inserted = 0;
136544249214SRandall Stewart 
136644249214SRandall Stewart 		if (control->last_frag_seen == 0) {
136744249214SRandall Stewart 			/* Still willing to raise highest FSN seen */
136844249214SRandall Stewart 			if (SCTP_TSN_GT(chk->rec.data.fsn_num, control->top_fsn)) {
136944249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1370f2ea2a2dSMichael Tuexen 				    "We have a new top_fsn: %u\n",
137144249214SRandall Stewart 				    chk->rec.data.fsn_num);
137244249214SRandall Stewart 				control->top_fsn = chk->rec.data.fsn_num;
137344249214SRandall Stewart 			}
137444249214SRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
137544249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1376f2ea2a2dSMichael Tuexen 				    "The last fsn is now in place fsn: %u\n",
137744249214SRandall Stewart 				    chk->rec.data.fsn_num);
137844249214SRandall Stewart 				control->last_frag_seen = 1;
137944249214SRandall Stewart 			}
138044249214SRandall Stewart 			if (asoc->idata_supported || control->first_frag_seen) {
138144249214SRandall Stewart 				/*
138244249214SRandall Stewart 				 * For IDATA we always check since we know
138344249214SRandall Stewart 				 * that the first fragment is 0. For old
138444249214SRandall Stewart 				 * DATA we have to receive the first before
1385cd0a4ff6SPedro F. Giffuni 				 * we know the first FSN (which is the TSN).
138644249214SRandall Stewart 				 */
138744249214SRandall Stewart 				if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn_num)) {
138844249214SRandall Stewart 					/*
138944249214SRandall Stewart 					 * We have already delivered up to
139044249214SRandall Stewart 					 * this so its a dup
139144249214SRandall Stewart 					 */
1392b1deed45SMichael Tuexen 					sctp_abort_in_reasm(stcb, control, chk,
139344249214SRandall Stewart 					    abort_flag,
139444249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
139544249214SRandall Stewart 					return;
139644249214SRandall Stewart 				}
139744249214SRandall Stewart 			}
139844249214SRandall Stewart 		} else {
139944249214SRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
140044249214SRandall Stewart 				/* Second last? huh? */
140144249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1402f2ea2a2dSMichael Tuexen 				    "Duplicate last fsn: %u (top: %u) -- abort\n",
140344249214SRandall Stewart 				    chk->rec.data.fsn_num, control->top_fsn);
1404b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control,
140544249214SRandall Stewart 				    chk, abort_flag,
140644249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
140744249214SRandall Stewart 				return;
140844249214SRandall Stewart 			}
140944249214SRandall Stewart 			if (asoc->idata_supported || control->first_frag_seen) {
141044249214SRandall Stewart 				/*
141144249214SRandall Stewart 				 * For IDATA we always check since we know
141244249214SRandall Stewart 				 * that the first fragment is 0. For old
141344249214SRandall Stewart 				 * DATA we have to receive the first before
1414cd0a4ff6SPedro F. Giffuni 				 * we know the first FSN (which is the TSN).
141544249214SRandall Stewart 				 */
141644249214SRandall Stewart 
141744249214SRandall Stewart 				if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn_num)) {
141844249214SRandall Stewart 					/*
141944249214SRandall Stewart 					 * We have already delivered up to
142044249214SRandall Stewart 					 * this so its a dup
142144249214SRandall Stewart 					 */
142244249214SRandall Stewart 					SCTPDBG(SCTP_DEBUG_XXX,
1423f2ea2a2dSMichael Tuexen 					    "New fsn: %u is already seen in included_fsn: %u -- abort\n",
142444249214SRandall Stewart 					    chk->rec.data.fsn_num, control->fsn_included);
1425b1deed45SMichael Tuexen 					sctp_abort_in_reasm(stcb, control, chk,
142644249214SRandall Stewart 					    abort_flag,
142744249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
142844249214SRandall Stewart 					return;
142944249214SRandall Stewart 				}
143044249214SRandall Stewart 			}
143144249214SRandall Stewart 			/*
143244249214SRandall Stewart 			 * validate not beyond top FSN if we have seen last
1433f8829a4aSRandall Stewart 			 * one
1434f8829a4aSRandall Stewart 			 */
143544249214SRandall Stewart 			if (SCTP_TSN_GT(chk->rec.data.fsn_num, control->top_fsn)) {
143644249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1437f2ea2a2dSMichael Tuexen 				    "New fsn: %u is beyond or at top_fsn: %u -- abort\n",
143844249214SRandall Stewart 				    chk->rec.data.fsn_num,
143944249214SRandall Stewart 				    control->top_fsn);
1440b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control, chk,
144144249214SRandall Stewart 				    abort_flag,
144244249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
144344249214SRandall Stewart 				return;
144444249214SRandall Stewart 			}
144544249214SRandall Stewart 		}
144644249214SRandall Stewart 		/*
144744249214SRandall Stewart 		 * If we reach here, we need to place the new chunk in the
144844249214SRandall Stewart 		 * reassembly for this control.
144944249214SRandall Stewart 		 */
145044249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1451f2ea2a2dSMichael Tuexen 		    "chunk is a not first fsn: %u needs to be inserted\n",
145244249214SRandall Stewart 		    chk->rec.data.fsn_num);
145344249214SRandall Stewart 		TAILQ_FOREACH(at, &control->reasm, sctp_next) {
145444249214SRandall Stewart 			if (SCTP_TSN_GT(at->rec.data.fsn_num, chk->rec.data.fsn_num)) {
145544249214SRandall Stewart 				/*
145644249214SRandall Stewart 				 * This one in queue is bigger than the new
145744249214SRandall Stewart 				 * one, insert the new one before at.
145844249214SRandall Stewart 				 */
145944249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1460f2ea2a2dSMichael Tuexen 				    "Insert it before fsn: %u\n",
146144249214SRandall Stewart 				    at->rec.data.fsn_num);
1462f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1463f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
146444249214SRandall Stewart 				TAILQ_INSERT_BEFORE(at, chk, sctp_next);
146544249214SRandall Stewart 				inserted = 1;
146644249214SRandall Stewart 				break;
146744249214SRandall Stewart 			} else if (at->rec.data.fsn_num == chk->rec.data.fsn_num) {
146844249214SRandall Stewart 				/*
146944249214SRandall Stewart 				 * Gak, He sent me a duplicate str seq
147044249214SRandall Stewart 				 * number
147144249214SRandall Stewart 				 */
147244249214SRandall Stewart 				/*
147344249214SRandall Stewart 				 * foo bar, I guess I will just free this
147444249214SRandall Stewart 				 * new guy, should we abort too? FIX ME
147544249214SRandall Stewart 				 * MAYBE? Or it COULD be that the SSN's have
147644249214SRandall Stewart 				 * wrapped. Maybe I should compare to TSN
147744249214SRandall Stewart 				 * somehow... sigh for now just blow away
147844249214SRandall Stewart 				 * the chunk!
147944249214SRandall Stewart 				 */
148044249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1481f2ea2a2dSMichael Tuexen 				    "Duplicate to fsn: %u -- abort\n",
148244249214SRandall Stewart 				    at->rec.data.fsn_num);
1483b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control,
148444249214SRandall Stewart 				    chk, abort_flag,
148544249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
148644249214SRandall Stewart 				return;
148744249214SRandall Stewart 			}
148844249214SRandall Stewart 		}
148944249214SRandall Stewart 		if (inserted == 0) {
149044249214SRandall Stewart 			/* Goes on the end */
1491f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "Inserting at tail of list fsn: %u\n",
149244249214SRandall Stewart 			    chk->rec.data.fsn_num);
149344249214SRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
149444249214SRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
149544249214SRandall Stewart 			TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
149644249214SRandall Stewart 		}
149744249214SRandall Stewart 	}
149844249214SRandall Stewart 	/*
149944249214SRandall Stewart 	 * Ok lets see if we can suck any up into the control structure that
150044249214SRandall Stewart 	 * are in seq if it makes sense.
150144249214SRandall Stewart 	 */
1502c09a1534SMichael Tuexen 	do_wakeup = 0;
150344249214SRandall Stewart 	/*
150444249214SRandall Stewart 	 * If the first fragment has not been seen there is no sense in
150544249214SRandall Stewart 	 * looking.
150644249214SRandall Stewart 	 */
150744249214SRandall Stewart 	if (control->first_frag_seen) {
150844249214SRandall Stewart 		next_fsn = control->fsn_included + 1;
150944249214SRandall Stewart 		TAILQ_FOREACH_SAFE(at, &control->reasm, sctp_next, nat) {
151044249214SRandall Stewart 			if (at->rec.data.fsn_num == next_fsn) {
151144249214SRandall Stewart 				/* We can add this one now to the control */
151244249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1513f2ea2a2dSMichael Tuexen 				    "Adding more to control: %p at: %p fsn: %u next_fsn: %u included: %u\n",
151444249214SRandall Stewart 				    control, at,
151544249214SRandall Stewart 				    at->rec.data.fsn_num,
151644249214SRandall Stewart 				    next_fsn, control->fsn_included);
151744249214SRandall Stewart 				TAILQ_REMOVE(&control->reasm, at, sctp_next);
1518*d1ea5fa9SMichael Tuexen 				sctp_add_chk_to_control(control, strm, stcb, asoc, at, SCTP_READ_LOCK_NOT_HELD);
1519c09a1534SMichael Tuexen 				if (control->on_read_q) {
1520c09a1534SMichael Tuexen 					do_wakeup = 1;
1521c09a1534SMichael Tuexen 				}
152244249214SRandall Stewart 				next_fsn++;
152344249214SRandall Stewart 				if (control->end_added && control->pdapi_started) {
152444249214SRandall Stewart 					if (strm->pd_api_started) {
152544249214SRandall Stewart 						strm->pd_api_started = 0;
152644249214SRandall Stewart 						control->pdapi_started = 0;
152744249214SRandall Stewart 					}
152844249214SRandall Stewart 					if (control->on_read_q == 0) {
152944249214SRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
153044249214SRandall Stewart 						    control,
153144249214SRandall Stewart 						    &stcb->sctp_socket->so_rcv, control->end_added,
153244249214SRandall Stewart 						    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1533c09a1534SMichael Tuexen 						do_wakeup = 1;
153444249214SRandall Stewart 					}
153544249214SRandall Stewart 					break;
153644249214SRandall Stewart 				}
153744249214SRandall Stewart 			} else {
1538f8829a4aSRandall Stewart 				break;
1539f8829a4aSRandall Stewart 			}
1540f8829a4aSRandall Stewart 		}
1541f8829a4aSRandall Stewart 	}
1542c09a1534SMichael Tuexen 	if (do_wakeup) {
154344249214SRandall Stewart 		/* Need to wakeup the reader */
1544b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1545f8829a4aSRandall Stewart 	}
1546f8829a4aSRandall Stewart }
1547f8829a4aSRandall Stewart 
154844249214SRandall Stewart static struct sctp_queued_to_read *
1549*d1ea5fa9SMichael Tuexen sctp_find_reasm_entry(struct sctp_stream_in *strm, uint32_t msg_id, int ordered, int old)
1550f8829a4aSRandall Stewart {
1551c09a1534SMichael Tuexen 	struct sctp_queued_to_read *control;
1552f8829a4aSRandall Stewart 
155344249214SRandall Stewart 	if (ordered) {
1554c09a1534SMichael Tuexen 		TAILQ_FOREACH(control, &strm->inqueue, next_instrm) {
1555c09a1534SMichael Tuexen 			if (control->msg_id == msg_id) {
155644249214SRandall Stewart 				break;
155744249214SRandall Stewart 			}
155844249214SRandall Stewart 		}
1559f8829a4aSRandall Stewart 	} else {
156044249214SRandall Stewart 		if (old) {
1561c09a1534SMichael Tuexen 			control = TAILQ_FIRST(&strm->uno_inqueue);
1562c09a1534SMichael Tuexen 			return (control);
1563f8829a4aSRandall Stewart 		}
1564c09a1534SMichael Tuexen 		TAILQ_FOREACH(control, &strm->uno_inqueue, next_instrm) {
1565c09a1534SMichael Tuexen 			if (control->msg_id == msg_id) {
156644249214SRandall Stewart 				break;
1567f8829a4aSRandall Stewart 			}
1568f8829a4aSRandall Stewart 		}
1569f8829a4aSRandall Stewart 	}
1570c09a1534SMichael Tuexen 	return (control);
1571f8829a4aSRandall Stewart }
157244249214SRandall Stewart 
1573f8829a4aSRandall Stewart static int
1574f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
157544249214SRandall Stewart     struct mbuf **m, int offset, int chk_length,
1576f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
157744249214SRandall Stewart     int *break_flag, int last_chunk, uint8_t chtype)
1578f8829a4aSRandall Stewart {
1579f8829a4aSRandall Stewart 	/* Process a data chunk */
1580f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
158144249214SRandall Stewart 	struct sctp_data_chunk *ch;
158244249214SRandall Stewart 	struct sctp_idata_chunk *nch, chunk_buf;
1583f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
158444249214SRandall Stewart 	uint32_t tsn, fsn, gap, msg_id;
1585f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
15867215cc1bSMichael Tuexen 	int the_len;
1587139bc87fSRandall Stewart 	int need_reasm_check = 0;
158844249214SRandall Stewart 	uint16_t strmno;
1589ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
1590ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
159144249214SRandall Stewart 	struct sctp_queued_to_read *control = NULL;
1592f42a358aSRandall Stewart 	uint32_t protocol_id;
1593f42a358aSRandall Stewart 	uint8_t chunk_flags;
159417205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
159544249214SRandall Stewart 	struct sctp_stream_in *strm;
159644249214SRandall Stewart 	int ordered;
159744249214SRandall Stewart 	size_t clen;
159844249214SRandall Stewart 	int created_control = 0;
159944249214SRandall Stewart 	uint8_t old_data;
1600f8829a4aSRandall Stewart 
1601f8829a4aSRandall Stewart 	chk = NULL;
160244249214SRandall Stewart 	if (chtype == SCTP_IDATA) {
160344249214SRandall Stewart 		nch = (struct sctp_idata_chunk *)sctp_m_getptr(*m, offset,
160444249214SRandall Stewart 		    sizeof(struct sctp_idata_chunk), (uint8_t *) & chunk_buf);
160544249214SRandall Stewart 		ch = (struct sctp_data_chunk *)nch;
160644249214SRandall Stewart 		clen = sizeof(struct sctp_idata_chunk);
1607f8829a4aSRandall Stewart 		tsn = ntohl(ch->dp.tsn);
160844249214SRandall Stewart 		msg_id = ntohl(nch->dp.msg_id);
1609*d1ea5fa9SMichael Tuexen 		protocol_id = nch->dp.ppid_fsn.protocol_id;
161044249214SRandall Stewart 		if (ch->ch.chunk_flags & SCTP_DATA_FIRST_FRAG)
161144249214SRandall Stewart 			fsn = 0;
161244249214SRandall Stewart 		else
1613e187bac2SMichael Tuexen 			fsn = ntohl(nch->dp.ppid_fsn.fsn);
161444249214SRandall Stewart 		old_data = 0;
161544249214SRandall Stewart 	} else {
161644249214SRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(*m, offset,
161744249214SRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
161844249214SRandall Stewart 		tsn = ntohl(ch->dp.tsn);
1619*d1ea5fa9SMichael Tuexen 		protocol_id = ch->dp.protocol_id;
162044249214SRandall Stewart 		clen = sizeof(struct sctp_data_chunk);
162144249214SRandall Stewart 		fsn = tsn;
162244249214SRandall Stewart 		msg_id = (uint32_t) (ntohs(ch->dp.stream_sequence));
162344249214SRandall Stewart 		nch = NULL;
162444249214SRandall Stewart 		old_data = 1;
162544249214SRandall Stewart 	}
1626f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
162744249214SRandall Stewart 	if ((size_t)chk_length == clen) {
162844249214SRandall Stewart 		/*
162944249214SRandall Stewart 		 * Need to send an abort since we had a empty data chunk.
163044249214SRandall Stewart 		 */
163144249214SRandall Stewart 		op_err = sctp_generate_no_user_data_cause(ch->dp.tsn);
163244249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
163344249214SRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
163444249214SRandall Stewart 		*abort_flag = 1;
163544249214SRandall Stewart 		return (0);
163644249214SRandall Stewart 	}
1637b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1638b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1639b3f1ea41SRandall Stewart 	}
164037f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1641b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1642c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
164380fefe0aSRandall Stewart 	}
1644ad81507eSRandall Stewart 	if (stcb == NULL) {
1645ad81507eSRandall Stewart 		return (0);
1646ad81507eSRandall Stewart 	}
164780fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
164820b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1649f8829a4aSRandall Stewart 		/* It is a duplicate */
1650f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1651f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1652f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1653f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1654f8829a4aSRandall Stewart 			asoc->numduptsns++;
1655f8829a4aSRandall Stewart 		}
1656b201f536SRandall Stewart 		asoc->send_sack = 1;
1657f8829a4aSRandall Stewart 		return (0);
1658f8829a4aSRandall Stewart 	}
1659f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1660d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1661f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1662f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1663f8829a4aSRandall Stewart 		return (0);
1664f8829a4aSRandall Stewart 	}
1665f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1666207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
16670696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1668f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1669f8829a4aSRandall Stewart 			return (0);
1670f8829a4aSRandall Stewart 		}
1671f8829a4aSRandall Stewart 	}
167220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1673f8829a4aSRandall Stewart 		*high_tsn = tsn;
1674f8829a4aSRandall Stewart 	}
1675f8829a4aSRandall Stewart 	/* See if we have received this one already */
167677acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
167777acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1678f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1679f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1680f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1681f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1682f8829a4aSRandall Stewart 			asoc->numduptsns++;
1683f8829a4aSRandall Stewart 		}
168442551e99SRandall Stewart 		asoc->send_sack = 1;
1685f8829a4aSRandall Stewart 		return (0);
1686f8829a4aSRandall Stewart 	}
1687f8829a4aSRandall Stewart 	/*
1688f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1689f8829a4aSRandall Stewart 	 * to be sent up above
1690f8829a4aSRandall Stewart 	 */
1691ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1692f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1693ff1ffd74SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1694f8829a4aSRandall Stewart 		/*
1695f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1696f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1697f8829a4aSRandall Stewart 		 */
1698ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1699a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1700f8829a4aSRandall Stewart 		*abort_flag = 1;
1701f8829a4aSRandall Stewart 		return (0);
1702f8829a4aSRandall Stewart 	}
1703f8829a4aSRandall Stewart 	/*
1704f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1705f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1706f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1707f8829a4aSRandall Stewart 	 */
1708f8829a4aSRandall Stewart 
170944249214SRandall Stewart 	/* Is the stream valid? */
1710f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
171144249214SRandall Stewart 
1712f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
171386eda749SMichael Tuexen 		struct sctp_error_invalid_stream *cause;
1714f8829a4aSRandall Stewart 
171586eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_invalid_stream),
1716eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
171786eda749SMichael Tuexen 		if (op_err != NULL) {
1718f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
171986eda749SMichael Tuexen 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
172086eda749SMichael Tuexen 			cause = mtod(op_err, struct sctp_error_invalid_stream *);
1721f8829a4aSRandall Stewart 			/*
1722f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1723f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1724f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1725f8829a4aSRandall Stewart 			 */
172686eda749SMichael Tuexen 			SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_invalid_stream);
172786eda749SMichael Tuexen 			cause->cause.code = htons(SCTP_CAUSE_INVALID_STREAM);
172886eda749SMichael Tuexen 			cause->cause.length = htons(sizeof(struct sctp_error_invalid_stream));
172986eda749SMichael Tuexen 			cause->stream_id = ch->dp.stream_id;
173086eda749SMichael Tuexen 			cause->reserved = htons(0);
173186eda749SMichael Tuexen 			sctp_queue_op_err(stcb, op_err);
1732f8829a4aSRandall Stewart 		}
1733f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1734207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1735830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
173620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1737830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1738d06c82f1SRandall Stewart 		}
1739d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1740d06c82f1SRandall Stewart 			/* Update cum-ack */
1741d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1742d06c82f1SRandall Stewart 		}
1743f8829a4aSRandall Stewart 		return (0);
1744f8829a4aSRandall Stewart 	}
174544249214SRandall Stewart 	strm = &asoc->strmin[strmno];
1746f8829a4aSRandall Stewart 	/*
174744249214SRandall Stewart 	 * If its a fragmented message, lets see if we can find the control
174844249214SRandall Stewart 	 * on the reassembly queues.
1749f8829a4aSRandall Stewart 	 */
175044249214SRandall Stewart 	if ((chtype == SCTP_IDATA) && ((chunk_flags & SCTP_DATA_FIRST_FRAG) == 0) && (fsn == 0)) {
175144249214SRandall Stewart 		/*
175244249214SRandall Stewart 		 * The first *must* be fsn 0, and other (middle/end) pieces
175344249214SRandall Stewart 		 * can *not* be fsn 0.
175444249214SRandall Stewart 		 */
175544249214SRandall Stewart 		goto err_out;
175644249214SRandall Stewart 	}
175744249214SRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
175844249214SRandall Stewart 		/* See if we can find the re-assembly entity */
1759*d1ea5fa9SMichael Tuexen 		control = sctp_find_reasm_entry(strm, msg_id, ordered, old_data);
176044249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags:0x%x look for control on queues %p\n",
176144249214SRandall Stewart 		    chunk_flags, control);
176244249214SRandall Stewart 		if (control) {
176344249214SRandall Stewart 			/* We found something, does it belong? */
176444249214SRandall Stewart 			if (ordered && (msg_id != control->sinfo_ssn)) {
176544249214SRandall Stewart 		err_out:
176644249214SRandall Stewart 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
176744249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
176844249214SRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
176944249214SRandall Stewart 				*abort_flag = 1;
177044249214SRandall Stewart 				return (0);
177144249214SRandall Stewart 			}
177244249214SRandall Stewart 			if (ordered && ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED)) {
177344249214SRandall Stewart 				/*
177444249214SRandall Stewart 				 * We can't have a switched order with an
177544249214SRandall Stewart 				 * unordered chunk
177644249214SRandall Stewart 				 */
177744249214SRandall Stewart 				goto err_out;
177844249214SRandall Stewart 			}
177944249214SRandall Stewart 			if (!ordered && (((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) == 0)) {
178044249214SRandall Stewart 				/*
178144249214SRandall Stewart 				 * We can't have a switched unordered with a
178244249214SRandall Stewart 				 * ordered chunk
178344249214SRandall Stewart 				 */
178444249214SRandall Stewart 				goto err_out;
178544249214SRandall Stewart 			}
178644249214SRandall Stewart 		}
178744249214SRandall Stewart 	} else {
178844249214SRandall Stewart 		/*
178944249214SRandall Stewart 		 * Its a complete segment. Lets validate we don't have a
179044249214SRandall Stewart 		 * re-assembly going on with the same Stream/Seq (for
179144249214SRandall Stewart 		 * ordered) or in the same Stream for unordered.
179244249214SRandall Stewart 		 */
179344249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags:0x%x look for msg in case we have dup\n",
179444249214SRandall Stewart 		    chunk_flags);
1795*d1ea5fa9SMichael Tuexen 		if (sctp_find_reasm_entry(strm, msg_id, ordered, old_data)) {
1796f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x dup detected on msg_id: %u\n",
179744249214SRandall Stewart 			    chunk_flags,
179844249214SRandall Stewart 			    msg_id);
179944249214SRandall Stewart 
180044249214SRandall Stewart 			goto err_out;
180144249214SRandall Stewart 		}
180244249214SRandall Stewart 	}
180344249214SRandall Stewart 	/* now do the tests */
180444249214SRandall Stewart 	if (((asoc->cnt_on_all_streams +
180544249214SRandall Stewart 	    asoc->cnt_on_reasm_queue +
180644249214SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
180744249214SRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
180844249214SRandall Stewart 		/*
180944249214SRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
181044249214SRandall Stewart 		 * the reader is doing its job...
181144249214SRandall Stewart 		 */
181244249214SRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
181344249214SRandall Stewart 			/* some to read, wake-up */
181444249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
181544249214SRandall Stewart 			struct socket *so;
181644249214SRandall Stewart 
181744249214SRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
181844249214SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
181944249214SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
182044249214SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
182144249214SRandall Stewart 			SCTP_TCB_LOCK(stcb);
182244249214SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
182344249214SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
182444249214SRandall Stewart 				/* assoc was freed while we were unlocked */
182544249214SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
182644249214SRandall Stewart 				return (0);
182744249214SRandall Stewart 			}
182844249214SRandall Stewart #endif
182944249214SRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
183044249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
183144249214SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
183244249214SRandall Stewart #endif
183344249214SRandall Stewart 		}
183444249214SRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
183544249214SRandall Stewart 		if (nch == NULL) {
183644249214SRandall Stewart 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
183744249214SRandall Stewart 			    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
183844249214SRandall Stewart 				/* Nope not in the valid range dump it */
183944249214SRandall Stewart 		dump_packet:
184044249214SRandall Stewart 				sctp_set_rwnd(stcb, asoc);
184144249214SRandall Stewart 				if ((asoc->cnt_on_all_streams +
184244249214SRandall Stewart 				    asoc->cnt_on_reasm_queue +
184344249214SRandall Stewart 				    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
184444249214SRandall Stewart 					SCTP_STAT_INCR(sctps_datadropchklmt);
184544249214SRandall Stewart 				} else {
184644249214SRandall Stewart 					SCTP_STAT_INCR(sctps_datadroprwnd);
184744249214SRandall Stewart 				}
184844249214SRandall Stewart 				*break_flag = 1;
184944249214SRandall Stewart 				return (0);
185044249214SRandall Stewart 			}
185144249214SRandall Stewart 		} else {
185244249214SRandall Stewart 			if (control == NULL) {
185344249214SRandall Stewart 				goto dump_packet;
185444249214SRandall Stewart 			}
185544249214SRandall Stewart 			if (SCTP_TSN_GT(fsn, control->top_fsn)) {
185644249214SRandall Stewart 				goto dump_packet;
185744249214SRandall Stewart 			}
185844249214SRandall Stewart 		}
185944249214SRandall Stewart 	}
1860f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
186118e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
186218e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
186318e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
186418e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
186518e198d3SRandall Stewart 	}
1866f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1867f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
186844249214SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = msg_id;
1869f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1870f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
187118e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
187218e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
187318e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1874f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1875f42a358aSRandall Stewart #endif
187644249214SRandall Stewart 	/*
187744249214SRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
187844249214SRandall Stewart 	 * an evil attacker. We can only have Nk chunks based on our TSN
187944249214SRandall Stewart 	 * spread allowed by the mapping array N * 8 bits, so there is no
188044249214SRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
188144249214SRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
188244249214SRandall Stewart 	 */
1883f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1884d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1885f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
188644249214SRandall Stewart 	    SCTP_MSGID_GE(old_data, asoc->strmin[strmno].last_sequence_delivered, msg_id)) {
1887f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1888f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ: %u delivered: %u from peer, Abort!\n",
188944249214SRandall Stewart 		    msg_id, asoc->strmin[strmno].last_sequence_delivered);
1890f8829a4aSRandall Stewart 
1891ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1892ff1ffd74SMichael Tuexen 		    asoc->strmin[strmno].last_sequence_delivered,
189344249214SRandall Stewart 		    tsn, strmno, msg_id);
1894ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1895b7d130beSMichael Tuexen 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1896ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1897f8829a4aSRandall Stewart 		*abort_flag = 1;
1898f8829a4aSRandall Stewart 		return (0);
1899f8829a4aSRandall Stewart 	}
1900f42a358aSRandall Stewart 	/************************************
1901f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1902f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1903f42a358aSRandall Stewart 	 *************************************/
190444249214SRandall Stewart 	if (nch) {
190544249214SRandall Stewart 		the_len = (chk_length - sizeof(struct sctp_idata_chunk));
190644249214SRandall Stewart 	} else {
1907f8829a4aSRandall Stewart 		the_len = (chk_length - sizeof(struct sctp_data_chunk));
190844249214SRandall Stewart 	}
1909f8829a4aSRandall Stewart 	if (last_chunk == 0) {
191044249214SRandall Stewart 		if (nch) {
191144249214SRandall Stewart 			dmbuf = SCTP_M_COPYM(*m,
191244249214SRandall Stewart 			    (offset + sizeof(struct sctp_idata_chunk)),
191344249214SRandall Stewart 			    the_len, M_NOWAIT);
191444249214SRandall Stewart 		} else {
191544b7479bSRandall Stewart 			dmbuf = SCTP_M_COPYM(*m,
1916f8829a4aSRandall Stewart 			    (offset + sizeof(struct sctp_data_chunk)),
1917eb1b1807SGleb Smirnoff 			    the_len, M_NOWAIT);
191844249214SRandall Stewart 		}
1919f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1920b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
19214be807c4SMichael Tuexen 			sctp_log_mbc(dmbuf, SCTP_MBUF_ICOPY);
1922f8829a4aSRandall Stewart 		}
1923f8829a4aSRandall Stewart #endif
1924f8829a4aSRandall Stewart 	} else {
1925f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1926139bc87fSRandall Stewart 		int l_len;
1927139bc87fSRandall Stewart 
1928f8829a4aSRandall Stewart 		dmbuf = *m;
1929f8829a4aSRandall Stewart 		/* lop off the top part */
193044249214SRandall Stewart 		if (nch) {
193144249214SRandall Stewart 			m_adj(dmbuf, (offset + sizeof(struct sctp_idata_chunk)));
193244249214SRandall Stewart 		} else {
1933f8829a4aSRandall Stewart 			m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
193444249214SRandall Stewart 		}
1935139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1936139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1937139bc87fSRandall Stewart 		} else {
1938139bc87fSRandall Stewart 			/*
1939139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1940139bc87fSRandall Stewart 			 * this to often :-0
1941139bc87fSRandall Stewart 			 */
1942139bc87fSRandall Stewart 			struct mbuf *lat;
1943139bc87fSRandall Stewart 
1944139bc87fSRandall Stewart 			l_len = 0;
194560990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1946139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1947139bc87fSRandall Stewart 			}
1948139bc87fSRandall Stewart 		}
1949139bc87fSRandall Stewart 		if (l_len > the_len) {
1950f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1951139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1952f8829a4aSRandall Stewart 		}
1953f8829a4aSRandall Stewart 	}
1954f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1955f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1956f8829a4aSRandall Stewart 		return (0);
1957f8829a4aSRandall Stewart 	}
195844249214SRandall Stewart 	/*
195944249214SRandall Stewart 	 * Now no matter what we need a control, get one if we don't have
196044249214SRandall Stewart 	 * one (we may have gotten it above when we found the message was
196144249214SRandall Stewart 	 * fragmented
196244249214SRandall Stewart 	 */
196344249214SRandall Stewart 	if (control == NULL) {
196444249214SRandall Stewart 		sctp_alloc_a_readq(stcb, control);
196544249214SRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
196644249214SRandall Stewart 		    protocol_id,
196744249214SRandall Stewart 		    strmno, msg_id,
196844249214SRandall Stewart 		    chunk_flags,
196944249214SRandall Stewart 		    NULL, fsn, msg_id);
197044249214SRandall Stewart 		if (control == NULL) {
197144249214SRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
197244249214SRandall Stewart 			return (0);
197344249214SRandall Stewart 		}
197444249214SRandall Stewart 		if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
197544249214SRandall Stewart 			control->data = dmbuf;
197644249214SRandall Stewart 			control->tail_mbuf = NULL;
197744249214SRandall Stewart 			control->end_added = control->last_frag_seen = control->first_frag_seen = 1;
197844249214SRandall Stewart 			control->top_fsn = control->fsn_included = fsn;
197944249214SRandall Stewart 		}
198044249214SRandall Stewart 		created_control = 1;
198144249214SRandall Stewart 	}
1982f2ea2a2dSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x ordered: %d msgid: %u control: %p\n",
198344249214SRandall Stewart 	    chunk_flags, ordered, msg_id, control);
1984f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1985f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1986f42a358aSRandall Stewart 	    ((ordered == 0) ||
198744249214SRandall Stewart 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == msg_id &&
1988f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1989f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1990f8829a4aSRandall Stewart 		/*
1991f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1992f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1993f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1994f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1995f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1996f8829a4aSRandall Stewart 		 */
19971ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
199820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
19991ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
20001ea735c8SMichael Tuexen 		}
2001f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_XXX, "Injecting control: %p to be read (msg_id: %u)\n",
200244249214SRandall Stewart 		    control, msg_id);
200344249214SRandall Stewart 
2004cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
2005cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
2006cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2007830d754dSRandall Stewart 
2008f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2009f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
201044249214SRandall Stewart 			strm->last_sequence_delivered++;
2011f8829a4aSRandall Stewart 		}
2012f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
2013b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
201444249214SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, msg_id, strmno,
2015f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
201680fefe0aSRandall Stewart 		}
2017f8829a4aSRandall Stewart 		control = NULL;
2018f8829a4aSRandall Stewart 		goto finish_express_del;
2019f8829a4aSRandall Stewart 	}
202044249214SRandall Stewart 	/* Now will we need a chunk too? */
2021f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2022f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
2023f8829a4aSRandall Stewart 		if (chk == NULL) {
2024f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
2025f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
2026f8829a4aSRandall Stewart 			if (last_chunk == 0) {
2027f8829a4aSRandall Stewart 				/* we copied it, free the copy */
2028f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
2029f8829a4aSRandall Stewart 			}
2030f8829a4aSRandall Stewart 			return (0);
2031f8829a4aSRandall Stewart 		}
2032f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
2033f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
203444249214SRandall Stewart 		chk->rec.data.fsn_num = fsn;
203544249214SRandall Stewart 		chk->rec.data.stream_seq = msg_id;
2036f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
2037f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
2038f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
2039f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
2040f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
2041f8829a4aSRandall Stewart 		chk->asoc = asoc;
2042f8829a4aSRandall Stewart 		chk->send_size = the_len;
2043f8829a4aSRandall Stewart 		chk->whoTo = net;
2044f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_XXX, "Building ck: %p for control: %p to be read (msg_id: %u)\n",
204544249214SRandall Stewart 		    chk,
204644249214SRandall Stewart 		    control, msg_id);
2047f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
2048f8829a4aSRandall Stewart 		chk->data = dmbuf;
204944249214SRandall Stewart 	}
205044249214SRandall Stewart 	/* Set the appropriate TSN mark */
205144249214SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
205244249214SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
205344249214SRandall Stewart 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
205444249214SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
205544249214SRandall Stewart 		}
2056f8829a4aSRandall Stewart 	} else {
205744249214SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
205844249214SRandall Stewart 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
205944249214SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
2060f8829a4aSRandall Stewart 		}
2061f8829a4aSRandall Stewart 	}
206244249214SRandall Stewart 	/* Now is it complete (i.e. not fragmented)? */
206344249214SRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
2064f8829a4aSRandall Stewart 		/*
206544249214SRandall Stewart 		 * Special check for when streams are resetting. We could be
206644249214SRandall Stewart 		 * more smart about this and check the actual stream to see
206744249214SRandall Stewart 		 * if it is not being reset.. that way we would not create a
206844249214SRandall Stewart 		 * HOLB when amongst streams being reset and those not being
206944249214SRandall Stewart 		 * reset.
2070f8829a4aSRandall Stewart 		 *
2071f8829a4aSRandall Stewart 		 */
2072f8829a4aSRandall Stewart 		if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
207320b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, liste->tsn)) {
2074f8829a4aSRandall Stewart 			/*
207544249214SRandall Stewart 			 * yep its past where we need to reset... go ahead
207644249214SRandall Stewart 			 * and queue it.
2077f8829a4aSRandall Stewart 			 */
2078f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2079f8829a4aSRandall Stewart 				/* first one on */
2080f8829a4aSRandall Stewart 				TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2081f8829a4aSRandall Stewart 			} else {
208244249214SRandall Stewart 				struct sctp_queued_to_read *ctlOn, *nctlOn;
2083f8829a4aSRandall Stewart 				unsigned char inserted = 0;
2084f8829a4aSRandall Stewart 
20854a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
208620b07a4dSMichael Tuexen 					if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
208744249214SRandall Stewart 
20884a9ef3f8SMichael Tuexen 						continue;
2089f8829a4aSRandall Stewart 					} else {
2090f8829a4aSRandall Stewart 						/* found it */
2091f8829a4aSRandall Stewart 						TAILQ_INSERT_BEFORE(ctlOn, control, next);
2092f8829a4aSRandall Stewart 						inserted = 1;
2093f8829a4aSRandall Stewart 						break;
2094f8829a4aSRandall Stewart 					}
2095f8829a4aSRandall Stewart 				}
2096f8829a4aSRandall Stewart 				if (inserted == 0) {
2097f8829a4aSRandall Stewart 					/*
209844249214SRandall Stewart 					 * must be put at end, use prevP
209944249214SRandall Stewart 					 * (all setup from loop) to setup
210044249214SRandall Stewart 					 * nextP.
2101f8829a4aSRandall Stewart 					 */
2102f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2103f8829a4aSRandall Stewart 				}
2104f8829a4aSRandall Stewart 			}
210544249214SRandall Stewart 			goto finish_express_del;
210644249214SRandall Stewart 		}
210744249214SRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
210844249214SRandall Stewart 			/* queue directly into socket buffer */
2109f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "Unordered data to be read control: %p msg_id: %u\n",
211044249214SRandall Stewart 			    control, msg_id);
211144249214SRandall Stewart 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
211244249214SRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
211344249214SRandall Stewart 			    control,
211444249214SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1,
211544249214SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
211644249214SRandall Stewart 
2117f8829a4aSRandall Stewart 		} else {
2118f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "Queue control: %p for reordering msg_id: %u\n", control,
211944249214SRandall Stewart 			    msg_id);
212044249214SRandall Stewart 			sctp_queue_data_to_stream(stcb, strm, asoc, control, abort_flag, &need_reasm_check);
2121f8829a4aSRandall Stewart 			if (*abort_flag) {
21228be0fd55SMichael Tuexen 				if (last_chunk) {
21238be0fd55SMichael Tuexen 					*m = NULL;
21248be0fd55SMichael Tuexen 				}
2125f8829a4aSRandall Stewart 				return (0);
2126f8829a4aSRandall Stewart 			}
2127f8829a4aSRandall Stewart 		}
212844249214SRandall Stewart 		goto finish_express_del;
2129f8829a4aSRandall Stewart 	}
213044249214SRandall Stewart 	/* If we reach here its a reassembly */
213144249214SRandall Stewart 	need_reasm_check = 1;
213244249214SRandall Stewart 	SCTPDBG(SCTP_DEBUG_XXX,
2133f2ea2a2dSMichael Tuexen 	    "Queue data to stream for reasm control: %p msg_id: %u\n",
213444249214SRandall Stewart 	    control, msg_id);
213544249214SRandall Stewart 	sctp_queue_data_for_reasm(stcb, asoc, strm, control, chk, created_control, abort_flag, tsn);
2136f8829a4aSRandall Stewart 	if (*abort_flag) {
2137a5d547adSRandall Stewart 		/*
213844249214SRandall Stewart 		 * the assoc is now gone and chk was put onto the reasm
213944249214SRandall Stewart 		 * queue, which has all been freed.
2140a5d547adSRandall Stewart 		 */
21418be0fd55SMichael Tuexen 		if (last_chunk) {
2142a5d547adSRandall Stewart 			*m = NULL;
21438be0fd55SMichael Tuexen 		}
2144f8829a4aSRandall Stewart 		return (0);
2145f8829a4aSRandall Stewart 	}
2146f8829a4aSRandall Stewart finish_express_del:
214744249214SRandall Stewart 	/* Here we tidy up things */
2148307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
2149307b49efSMichael Tuexen 		/* Update cum-ack */
2150307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
2151307b49efSMichael Tuexen 	}
2152f8829a4aSRandall Stewart 	if (last_chunk) {
2153f8829a4aSRandall Stewart 		*m = NULL;
2154f8829a4aSRandall Stewart 	}
2155f42a358aSRandall Stewart 	if (ordered) {
2156f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2157f8829a4aSRandall Stewart 	} else {
2158f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2159f8829a4aSRandall Stewart 	}
2160f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2161f8829a4aSRandall Stewart 	/* Set it present please */
2162b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
216344249214SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, msg_id, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
216480fefe0aSRandall Stewart 	}
2165b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2166f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2167f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
216880fefe0aSRandall Stewart 	}
216917205eccSRandall Stewart 	/* check the special flag for stream resets */
217017205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
217120b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
217217205eccSRandall Stewart 		/*
217317205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
217417205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
217517205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
217617205eccSRandall Stewart 		 * pending_reply_queue.
217717205eccSRandall Stewart 		 */
21784a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
217917205eccSRandall Stewart 
2180a169d6ecSMichael Tuexen 		sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
218117205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
21827cca1775SRandall Stewart 		sctp_send_deferred_reset_response(stcb, liste, SCTP_STREAM_RESET_RESULT_PERFORMED);
2183207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21843c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
218517205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
21864a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
218717205eccSRandall Stewart 			/* All can be removed */
21884a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
218917205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
219044249214SRandall Stewart 				sctp_queue_data_to_stream(stcb, strm, asoc, ctl, abort_flag, &need_reasm_check);
219117205eccSRandall Stewart 				if (*abort_flag) {
219217205eccSRandall Stewart 					return (0);
219317205eccSRandall Stewart 				}
219417205eccSRandall Stewart 			}
21954a9ef3f8SMichael Tuexen 		} else {
21964a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
219720b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
21984a9ef3f8SMichael Tuexen 					break;
21994a9ef3f8SMichael Tuexen 				}
220017205eccSRandall Stewart 				/*
220117205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
220217205eccSRandall Stewart 				 * process it which is the NOT of
220317205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
220417205eccSRandall Stewart 				 */
220517205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
220644249214SRandall Stewart 				sctp_queue_data_to_stream(stcb, strm, asoc, ctl, abort_flag, &need_reasm_check);
220717205eccSRandall Stewart 				if (*abort_flag) {
220817205eccSRandall Stewart 					return (0);
220917205eccSRandall Stewart 				}
221017205eccSRandall Stewart 			}
221117205eccSRandall Stewart 		}
221217205eccSRandall Stewart 		/*
221317205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
221417205eccSRandall Stewart 		 * held on reassembly queue?
221517205eccSRandall Stewart 		 */
2216*d1ea5fa9SMichael Tuexen 		(void)sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_NOT_HELD);
221717205eccSRandall Stewart 		need_reasm_check = 0;
221817205eccSRandall Stewart 	}
2219139bc87fSRandall Stewart 	if (need_reasm_check) {
2220139bc87fSRandall Stewart 		/* Another one waits ? */
2221*d1ea5fa9SMichael Tuexen 		(void)sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_NOT_HELD);
2222139bc87fSRandall Stewart 	}
2223f8829a4aSRandall Stewart 	return (1);
2224f8829a4aSRandall Stewart }
2225f8829a4aSRandall Stewart 
2226ed654363SMichael Tuexen static const int8_t sctp_map_lookup_tab[256] = {
2227b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2228b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2229b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2230b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2231b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2232b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2233b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2234b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2235b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2236b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2237b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2238b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2239b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2240b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2241b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2242b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
2243b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2244b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2245b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2246b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2247b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2248b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2249b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2250b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2251b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2252b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2253b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2254b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2255b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2256b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2257b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2258b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
2259f8829a4aSRandall Stewart };
2260f8829a4aSRandall Stewart 
2261f8829a4aSRandall Stewart 
2262f8829a4aSRandall Stewart void
2263b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2264f8829a4aSRandall Stewart {
2265f8829a4aSRandall Stewart 	/*
2266f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2267f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
226866bd30bdSRandall Stewart 	 *
226966bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
2270cd0a4ff6SPedro F. Giffuni 	 * make up the position of the cum-ack would be in the nr-mapping
227166bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
227266bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
227366bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
227466bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
227566bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
2276f8829a4aSRandall Stewart 	 */
2277f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2278b3f1ea41SRandall Stewart 	int at;
227966bd30bdSRandall Stewart 	uint8_t val;
2280f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
228177acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2282f8829a4aSRandall Stewart 
2283f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2284f8829a4aSRandall Stewart 
2285f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2286f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2287f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2288f8829a4aSRandall Stewart 	/*
2289f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2290f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2291f8829a4aSRandall Stewart 	 */
2292f8829a4aSRandall Stewart 	at = 0;
2293b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
229466bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
229566bd30bdSRandall Stewart 		if (val == 0xff) {
2296f8829a4aSRandall Stewart 			at += 8;
2297f8829a4aSRandall Stewart 		} else {
2298f8829a4aSRandall Stewart 			/* there is a 0 bit */
229966bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2300f8829a4aSRandall Stewart 			break;
2301f8829a4aSRandall Stewart 		}
2302f8829a4aSRandall Stewart 	}
2303b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2304f8829a4aSRandall Stewart 
230520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
230620b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2307a5d547adSRandall Stewart #ifdef INVARIANTS
2308ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2309ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2310f8829a4aSRandall Stewart #else
2311ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2312ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
23130e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2314b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2315b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2316b3f1ea41SRandall Stewart 		}
2317f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2318830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2319f8829a4aSRandall Stewart #endif
2320f8829a4aSRandall Stewart 	}
232120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
232277acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
232377acdc25SRandall Stewart 	} else {
232477acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
232577acdc25SRandall Stewart 	}
232677acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2327f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
232877acdc25SRandall Stewart 		/* highest becomes the cum-ack */
232937f144ebSMichael Tuexen 		int clr;
233037f144ebSMichael Tuexen 
233137f144ebSMichael Tuexen #ifdef INVARIANTS
233237f144ebSMichael Tuexen 		unsigned int i;
233337f144ebSMichael Tuexen 
233437f144ebSMichael Tuexen #endif
2335f8829a4aSRandall Stewart 
2336f8829a4aSRandall Stewart 		/* clear the array */
2337b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2338c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2339f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2340f8829a4aSRandall Stewart 		}
2341f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2342830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
234337f144ebSMichael Tuexen #ifdef INVARIANTS
2344b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2345b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2346cd3fd531SMichael Tuexen 				SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2347b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2348b5c16493SMichael Tuexen 			}
2349b5c16493SMichael Tuexen 		}
235037f144ebSMichael Tuexen #endif
235177acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
235277acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2353f8829a4aSRandall Stewart 	} else if (at >= 8) {
2354f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2355b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2356b3f1ea41SRandall Stewart 
2357f8829a4aSRandall Stewart 		/*
2358f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2359f8829a4aSRandall Stewart 		 * TSN value
2360f8829a4aSRandall Stewart 		 */
236177acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
236277acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2363f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
236477acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2365d55b0b1bSRandall Stewart #ifdef INVARIANTS
2366f8829a4aSRandall Stewart 			panic("impossible slide");
2367d55b0b1bSRandall Stewart #else
2368cd3fd531SMichael Tuexen 			SCTP_PRINTF("impossible slide lgap: %x slide_end: %x slide_from: %x? at: %d\n",
236977acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2370d55b0b1bSRandall Stewart 			return;
2371d55b0b1bSRandall Stewart #endif
2372f8829a4aSRandall Stewart 		}
2373b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2374b3f1ea41SRandall Stewart #ifdef INVARIANTS
2375b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2376b3f1ea41SRandall Stewart #else
2377cd3fd531SMichael Tuexen 			SCTP_PRINTF("Gak, would have overrun map end: %d slide_end: %d\n",
2378b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2379b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2380b3f1ea41SRandall Stewart #endif
2381b3f1ea41SRandall Stewart 		}
2382f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2383b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2384f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2385f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2386f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2387f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
238880fefe0aSRandall Stewart 		}
2389f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2390f8829a4aSRandall Stewart 		    distance < 0) {
2391f8829a4aSRandall Stewart 			/*
2392f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2393f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2394f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2395f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2396f8829a4aSRandall Stewart 			 */
2397f8829a4aSRandall Stewart 
2398b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2399f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2400f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2401f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
240280fefe0aSRandall Stewart 			}
2403f8829a4aSRandall Stewart 		} else {
2404f8829a4aSRandall Stewart 			int ii;
2405f8829a4aSRandall Stewart 
2406f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
240737f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
240837f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
240977acdc25SRandall Stewart 
2410f8829a4aSRandall Stewart 			}
2411aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2412f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
241377acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2414f8829a4aSRandall Stewart 			}
2415ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2416ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2417ee94f0a2SMichael Tuexen 			}
2418ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2419ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2420ee94f0a2SMichael Tuexen 			}
2421f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2422b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2423f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2424f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2425f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
242680fefe0aSRandall Stewart 			}
2427830d754dSRandall Stewart 		}
2428830d754dSRandall Stewart 	}
2429b5c16493SMichael Tuexen }
2430b5c16493SMichael Tuexen 
2431b5c16493SMichael Tuexen void
24327215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2433b5c16493SMichael Tuexen {
2434b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2435b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2436b5c16493SMichael Tuexen 
2437b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
243820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2439b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2440b5c16493SMichael Tuexen 	} else {
2441b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2442b5c16493SMichael Tuexen 	}
2443b5c16493SMichael Tuexen 
2444830d754dSRandall Stewart 	/*
2445f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2446f8829a4aSRandall Stewart 	 * timer (if allowed).
2447f8829a4aSRandall Stewart 	 */
2448f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2449f8829a4aSRandall Stewart 		/*
2450b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2451b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2452b5c16493SMichael Tuexen 		 * SACK
2453f8829a4aSRandall Stewart 		 */
2454139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2455f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2456b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
245744249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2458f8829a4aSRandall Stewart 		}
2459ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2460ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2461689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2462f8829a4aSRandall Stewart 	} else {
2463f8829a4aSRandall Stewart 		int is_a_gap;
2464f8829a4aSRandall Stewart 
2465f8829a4aSRandall Stewart 		/* is there a gap now ? */
246620b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2467f8829a4aSRandall Stewart 
2468f8829a4aSRandall Stewart 		/*
2469b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2470b5c16493SMichael Tuexen 		 * since last ack
2471f8829a4aSRandall Stewart 		 */
2472f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2473f8829a4aSRandall Stewart 
247442551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
247542551e99SRandall Stewart 							 * SACK */
2476f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2477f8829a4aSRandall Stewart 							 * longer is one */
2478f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2479f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
248042551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
248142551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2482f8829a4aSRandall Stewart 		    ) {
2483f8829a4aSRandall Stewart 
24847c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2485b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
248642551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2487f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2488f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2489139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2490f8829a4aSRandall Stewart 
2491f8829a4aSRandall Stewart 				/*
2492b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2493b5c16493SMichael Tuexen 				 * even in the face of
2494f8829a4aSRandall Stewart 				 *
2495b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2496b5c16493SMichael Tuexen 				 * have to be sent because of the above
2497b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2498b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2499b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2500f8829a4aSRandall Stewart 				 * the delayed ack timer.
2501f8829a4aSRandall Stewart 				 */
2502f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2503f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2504f8829a4aSRandall Stewart 			} else {
2505f8829a4aSRandall Stewart 				/*
2506b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2507b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2508b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2509f8829a4aSRandall Stewart 				 */
2510ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2511689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2512f8829a4aSRandall Stewart 			}
2513f8829a4aSRandall Stewart 		} else {
251442551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2515f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2516f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2517f8829a4aSRandall Stewart 			}
2518f8829a4aSRandall Stewart 		}
2519f8829a4aSRandall Stewart 	}
2520f8829a4aSRandall Stewart }
2521f8829a4aSRandall Stewart 
2522f8829a4aSRandall Stewart int
2523f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2524e7e71dd7SMichael Tuexen     struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2525e7e71dd7SMichael Tuexen     struct sctp_nets *net, uint32_t * high_tsn)
2526f8829a4aSRandall Stewart {
252744249214SRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
2528f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2529f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2530f8829a4aSRandall Stewart 	int stop_proc = 0;
2531f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
25328f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2533f8829a4aSRandall Stewart 	struct mbuf *m;
25348f777478SMichael Tuexen 	uint32_t highest_tsn;
2535f8829a4aSRandall Stewart 
2536f8829a4aSRandall Stewart 	/* set the rwnd */
2537f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2538f8829a4aSRandall Stewart 
2539f8829a4aSRandall Stewart 	m = *mm;
2540f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2541f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
254220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
25438f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
25448f777478SMichael Tuexen 	} else {
25458f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2546f8829a4aSRandall Stewart 	}
254720b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2548f8829a4aSRandall Stewart 	/*
2549f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2550f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2551f8829a4aSRandall Stewart 	 * chunk is assigned.
2552f8829a4aSRandall Stewart 	 */
2553f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2554f8829a4aSRandall Stewart 
2555d06c82f1SRandall Stewart 	/*-
2556f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2557f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2558f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2559f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2560d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2561d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2562f8829a4aSRandall Stewart 	 */
256344b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2564f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2565eb1b1807SGleb Smirnoff 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2566f8829a4aSRandall Stewart 		if (m) {
2567f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2568f8829a4aSRandall Stewart 			caddr_t *from, *to;
2569f8829a4aSRandall Stewart 
2570f8829a4aSRandall Stewart 			/* get the pointers and copy */
2571f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2572f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2573139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2574f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2575139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2576f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2577cd0a4ff6SPedro F. Giffuni 			/* success, back copy */
2578f8829a4aSRandall Stewart 			*mm = m;
2579f8829a4aSRandall Stewart 		} else {
2580f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2581f8829a4aSRandall Stewart 			m = *mm;
2582f8829a4aSRandall Stewart 		}
2583f8829a4aSRandall Stewart 	}
2584f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
258544249214SRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
258644249214SRandall Stewart 	    sizeof(struct sctp_chunkhdr), (uint8_t *) & chunk_buf);
2587f8829a4aSRandall Stewart 	if (ch == NULL) {
2588f8829a4aSRandall Stewart 		return (1);
2589f8829a4aSRandall Stewart 	}
2590f8829a4aSRandall Stewart 	/*
2591f8829a4aSRandall Stewart 	 * process all DATA chunks...
2592f8829a4aSRandall Stewart 	 */
2593f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2594f8829a4aSRandall Stewart 	break_flag = 0;
259542551e99SRandall Stewart 	asoc->data_pkts_seen++;
2596f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2597f8829a4aSRandall Stewart 		/* validate chunk length */
259844249214SRandall Stewart 		chk_length = ntohs(ch->chunk_length);
2599f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2600f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2601f8829a4aSRandall Stewart 			stop_proc = 1;
260260990c0cSMichael Tuexen 			continue;
2603f8829a4aSRandall Stewart 		}
260444249214SRandall Stewart 		if ((asoc->idata_supported == 1) &&
260544249214SRandall Stewart 		    (ch->chunk_type == SCTP_DATA)) {
260644249214SRandall Stewart 			struct mbuf *op_err;
260744249214SRandall Stewart 			char msg[SCTP_DIAG_INFO_LEN];
260844249214SRandall Stewart 
2609e7f232a0SMichael Tuexen 			snprintf(msg, sizeof(msg), "%s", "I-DATA chunk received when DATA was negotiated");
261044249214SRandall Stewart 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
261144249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_18;
261244249214SRandall Stewart 			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
261344249214SRandall Stewart 			return (2);
261444249214SRandall Stewart 		}
261544249214SRandall Stewart 		if ((asoc->idata_supported == 0) &&
261644249214SRandall Stewart 		    (ch->chunk_type == SCTP_IDATA)) {
261744249214SRandall Stewart 			struct mbuf *op_err;
261844249214SRandall Stewart 			char msg[SCTP_DIAG_INFO_LEN];
261944249214SRandall Stewart 
2620e7f232a0SMichael Tuexen 			snprintf(msg, sizeof(msg), "%s", "DATA chunk received when I-DATA was negotiated");
262144249214SRandall Stewart 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
262244249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
262344249214SRandall Stewart 			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
262444249214SRandall Stewart 			return (2);
262544249214SRandall Stewart 		}
262644249214SRandall Stewart 		if ((ch->chunk_type == SCTP_DATA) ||
262744249214SRandall Stewart 		    (ch->chunk_type == SCTP_IDATA)) {
262844249214SRandall Stewart 			int clen;
262944249214SRandall Stewart 
263044249214SRandall Stewart 			if (ch->chunk_type == SCTP_DATA) {
263144249214SRandall Stewart 				clen = sizeof(struct sctp_data_chunk);
263244249214SRandall Stewart 			} else {
263344249214SRandall Stewart 				clen = sizeof(struct sctp_idata_chunk);
263444249214SRandall Stewart 			}
2635f8ee69bfSMichael Tuexen 			if (chk_length < clen) {
2636f8829a4aSRandall Stewart 				/*
2637f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2638f8829a4aSRandall Stewart 				 * invalid data chunk.
2639f8829a4aSRandall Stewart 				 */
2640f8829a4aSRandall Stewart 				struct mbuf *op_err;
2641ff1ffd74SMichael Tuexen 				char msg[SCTP_DIAG_INFO_LEN];
2642f8829a4aSRandall Stewart 
2643ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "DATA chunk of length %d",
2644ff1ffd74SMichael Tuexen 				    chk_length);
2645ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
264644249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_20;
2647e7e71dd7SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
264832451da4SMichael Tuexen 				return (2);
264932451da4SMichael Tuexen 			}
2650f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2651f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2652f8829a4aSRandall Stewart #endif
2653f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2654f8829a4aSRandall Stewart 				last_chunk = 1;
2655f8829a4aSRandall Stewart 			} else {
2656f8829a4aSRandall Stewart 				last_chunk = 0;
2657f8829a4aSRandall Stewart 			}
265844249214SRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset,
2659f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
266044249214SRandall Stewart 			    last_chunk, ch->chunk_type)) {
2661f8829a4aSRandall Stewart 				num_chunks++;
2662f8829a4aSRandall Stewart 			}
2663f8829a4aSRandall Stewart 			if (abort_flag)
2664f8829a4aSRandall Stewart 				return (2);
2665f8829a4aSRandall Stewart 
2666f8829a4aSRandall Stewart 			if (break_flag) {
2667f8829a4aSRandall Stewart 				/*
2668f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2669f8829a4aSRandall Stewart 				 * drop rep space left.
2670f8829a4aSRandall Stewart 				 */
2671f8829a4aSRandall Stewart 				stop_proc = 1;
267260990c0cSMichael Tuexen 				continue;
2673f8829a4aSRandall Stewart 			}
2674f8829a4aSRandall Stewart 		} else {
2675f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
267644249214SRandall Stewart 			switch (ch->chunk_type) {
2677f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2678f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2679f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
268060990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2681f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2682f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2683f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2684f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2685f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2686f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2687f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2688f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2689f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2690f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2691f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2692f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2693f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2694f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2695f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2696f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2697f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2698fd60718dSMichael Tuexen 				{
2699f8829a4aSRandall Stewart 					/*
2700fd60718dSMichael Tuexen 					 * Now, what do we do with KNOWN
2701fd60718dSMichael Tuexen 					 * chunks that are NOT in the right
2702fd60718dSMichael Tuexen 					 * place?
2703f8829a4aSRandall Stewart 					 *
2704fd60718dSMichael Tuexen 					 * For now, I do nothing but ignore
2705fd60718dSMichael Tuexen 					 * them. We may later want to add
2706fd60718dSMichael Tuexen 					 * sysctl stuff to switch out and do
2707fd60718dSMichael Tuexen 					 * either an ABORT() or possibly
2708fd60718dSMichael Tuexen 					 * process them.
2709f8829a4aSRandall Stewart 					 */
2710f8829a4aSRandall Stewart 					struct mbuf *op_err;
2711267dbe63SMichael Tuexen 					char msg[SCTP_DIAG_INFO_LEN];
2712f8829a4aSRandall Stewart 
27139ae56375SMichael Tuexen 					snprintf(msg, sizeof(msg), "DATA chunk followed by chunk of type %2.2x",
271444249214SRandall Stewart 					    ch->chunk_type);
2715267dbe63SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2716e7e71dd7SMichael Tuexen 					sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2717f8829a4aSRandall Stewart 					return (2);
2718f8829a4aSRandall Stewart 				}
2719f8829a4aSRandall Stewart 			default:
2720f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
272144249214SRandall Stewart 				if (ch->chunk_type & 0x40) {
2722f8829a4aSRandall Stewart 					/* Add a error report to the queue */
272386eda749SMichael Tuexen 					struct mbuf *op_err;
272486eda749SMichael Tuexen 					struct sctp_gen_error_cause *cause;
2725f8829a4aSRandall Stewart 
272686eda749SMichael Tuexen 					op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
272786eda749SMichael Tuexen 					    0, M_NOWAIT, 1, MT_DATA);
272886eda749SMichael Tuexen 					if (op_err != NULL) {
272986eda749SMichael Tuexen 						cause = mtod(op_err, struct sctp_gen_error_cause *);
273086eda749SMichael Tuexen 						cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
27319a8e3088SMichael Tuexen 						cause->length = htons((uint16_t) (chk_length + sizeof(struct sctp_gen_error_cause)));
273286eda749SMichael Tuexen 						SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
273386eda749SMichael Tuexen 						SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
273486eda749SMichael Tuexen 						if (SCTP_BUF_NEXT(op_err) != NULL) {
273586eda749SMichael Tuexen 							sctp_queue_op_err(stcb, op_err);
2736f8829a4aSRandall Stewart 						} else {
273786eda749SMichael Tuexen 							sctp_m_freem(op_err);
2738f8829a4aSRandall Stewart 						}
2739f8829a4aSRandall Stewart 					}
2740f8829a4aSRandall Stewart 				}
274144249214SRandall Stewart 				if ((ch->chunk_type & 0x80) == 0) {
2742f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2743f8829a4aSRandall Stewart 					stop_proc = 1;
2744f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2745f8829a4aSRandall Stewart 					 * continue... */
2746f8829a4aSRandall Stewart 				break;
274760990c0cSMichael Tuexen 			}	/* switch of chunk type */
2748f8829a4aSRandall Stewart 		}
2749f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2750f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2751f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2752f8829a4aSRandall Stewart 			stop_proc = 1;
2753f8829a4aSRandall Stewart 			continue;
2754f8829a4aSRandall Stewart 		}
275544249214SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
275644249214SRandall Stewart 		    sizeof(struct sctp_chunkhdr), (uint8_t *) & chunk_buf);
2757f8829a4aSRandall Stewart 		if (ch == NULL) {
2758f8829a4aSRandall Stewart 			*offset = length;
2759f8829a4aSRandall Stewart 			stop_proc = 1;
276060990c0cSMichael Tuexen 			continue;
2761f8829a4aSRandall Stewart 		}
276260990c0cSMichael Tuexen 	}
2763f8829a4aSRandall Stewart 	if (break_flag) {
2764f8829a4aSRandall Stewart 		/*
2765f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2766f8829a4aSRandall Stewart 		 */
276720cc2188SMichael Tuexen 		sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2768f8829a4aSRandall Stewart 	}
2769f8829a4aSRandall Stewart 	if (num_chunks) {
2770f8829a4aSRandall Stewart 		/*
2771ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2772f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2773f8829a4aSRandall Stewart 		 */
2774f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2775b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2776c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2777c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2778c4739e2fSRandall Stewart 			    0,
2779c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2780c4739e2fSRandall Stewart 			    __LINE__);
2781c4739e2fSRandall Stewart 		}
2782f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
27836e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2784f8829a4aSRandall Stewart 	}
2785f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2786f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
278742551e99SRandall Stewart 		/* Assure that we ack right away */
278842551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2789f8829a4aSRandall Stewart 	}
2790f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
27917215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2792f8829a4aSRandall Stewart 	return (0);
2793f8829a4aSRandall Stewart }
2794f8829a4aSRandall Stewart 
27950fa753b3SRandall Stewart static int
27960fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
27970fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
27980fa753b3SRandall Stewart     int *num_frs,
27990fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
28000fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
28017215cc1bSMichael Tuexen     int *rto_ok)
28020fa753b3SRandall Stewart {
28030fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
28040fa753b3SRandall Stewart 	unsigned int theTSN;
2805b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
28060fa753b3SRandall Stewart 
28070fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
28080fa753b3SRandall Stewart 	tp1 = *p_tp1;
28090fa753b3SRandall Stewart 	if (tp1 == NULL) {
28100fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
28110fa753b3SRandall Stewart 	}
28120fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
28130fa753b3SRandall Stewart 		theTSN = j + last_tsn;
28140fa753b3SRandall Stewart 		while (tp1) {
28150fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
28160fa753b3SRandall Stewart 				(*num_frs) += 1;
28170fa753b3SRandall Stewart 
28180fa753b3SRandall Stewart 			/*-
28190fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
28200fa753b3SRandall Stewart 			 * processed from the sent queue, track the
28210fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
28220fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
28230fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
28240fa753b3SRandall Stewart 			 * and retransmissions.
28250fa753b3SRandall Stewart 			 */
28268427b3fdSMichael Tuexen 			if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
28278427b3fdSMichael Tuexen 			    (tp1->whoTo->find_pseudo_cumack == 1) &&
28280fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
28290fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
28300fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
28310fa753b3SRandall Stewart 			}
28328427b3fdSMichael Tuexen 			if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
28338427b3fdSMichael Tuexen 			    (tp1->whoTo->find_rtx_pseudo_cumack == 1) &&
28340fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28350fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28360fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
28370fa753b3SRandall Stewart 			}
28380fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
28390fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
28400fa753b3SRandall Stewart 					/*-
28410fa753b3SRandall Stewart 					 * must be held until
28420fa753b3SRandall Stewart 					 * cum-ack passes
28430fa753b3SRandall Stewart 					 */
28440fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
28450fa753b3SRandall Stewart 						/*-
28460fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
28470fa753b3SRandall Stewart 						 * now no-longer in flight.
28480fa753b3SRandall Stewart 						 * Higher values may already be set
28490fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
28500fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
28510fa753b3SRandall Stewart 						 */
285220b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
285320b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
28540fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
28550fa753b3SRandall Stewart 						}
28560fa753b3SRandall Stewart 						/*-
28570fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
28580fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
28590fa753b3SRandall Stewart 						 * newly acked. update
28600fa753b3SRandall Stewart 						 * this_sack_highest_newack if
28610fa753b3SRandall Stewart 						 * appropriate.
28620fa753b3SRandall Stewart 						 */
28630fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
28640fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
28650fa753b3SRandall Stewart 
286620b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
286720b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
28680fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
28690fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
28700fa753b3SRandall Stewart 						}
28710fa753b3SRandall Stewart 						/*-
28720fa753b3SRandall Stewart 						 * CMT DAC algo: also update
28730fa753b3SRandall Stewart 						 * this_sack_lowest_newack
28740fa753b3SRandall Stewart 						 */
28750fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
28760fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28770fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
28780fa753b3SRandall Stewart 								    last_tsn,
28790fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
28800fa753b3SRandall Stewart 								    0,
28810fa753b3SRandall Stewart 								    0,
28820fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
28830fa753b3SRandall Stewart 							}
28840fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
28850fa753b3SRandall Stewart 						}
28860fa753b3SRandall Stewart 						/*-
28870fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
28880fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
28890fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
28900fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
28910fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
28920fa753b3SRandall Stewart 						 * retransmissions.
28930fa753b3SRandall Stewart 						 */
28940fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
28950fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
28960fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
28970fa753b3SRandall Stewart 							}
28980fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
28990fa753b3SRandall Stewart 						}
29000fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
29010fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
29020fa753b3SRandall Stewart 						}
29030fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
29040fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
29050fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
29060fa753b3SRandall Stewart 							}
29070fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
29080fa753b3SRandall Stewart 						}
29090fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29100fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
29110fa753b3SRandall Stewart 							    last_tsn,
29120fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
29130fa753b3SRandall Stewart 							    frag_strt,
29140fa753b3SRandall Stewart 							    frag_end,
29150fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
29160fa753b3SRandall Stewart 						}
29170fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
29180fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
29190fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
29200fa753b3SRandall Stewart 							    tp1->book_size,
29219a8e3088SMichael Tuexen 							    (uint32_t) (uintptr_t) tp1->whoTo,
29220fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
29230fa753b3SRandall Stewart 						}
29240fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2925299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2926299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2927299108c5SRandall Stewart 							    tp1);
2928299108c5SRandall Stewart 						}
29290fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
29300fa753b3SRandall Stewart 
29310fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
29320fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
29330fa753b3SRandall Stewart 							/*-
29340fa753b3SRandall Stewart 							 * True non-retransmited chunk
29350fa753b3SRandall Stewart 							 */
29360fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
29370fa753b3SRandall Stewart 
29380fa753b3SRandall Stewart 							/*-
29390fa753b3SRandall Stewart 							 * update RTO too ?
29400fa753b3SRandall Stewart 							 */
29410fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2942f79aab18SRandall Stewart 								if (*rto_ok) {
29430fa753b3SRandall Stewart 									tp1->whoTo->RTO =
29440fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
29450fa753b3SRandall Stewart 									    &stcb->asoc,
29460fa753b3SRandall Stewart 									    tp1->whoTo,
29470fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2948899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2949f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2950f79aab18SRandall Stewart 									*rto_ok = 0;
2951f79aab18SRandall Stewart 								}
2952f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2953f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2954f79aab18SRandall Stewart 								}
29550fa753b3SRandall Stewart 								tp1->do_rtt = 0;
29560fa753b3SRandall Stewart 							}
29570fa753b3SRandall Stewart 						}
29580fa753b3SRandall Stewart 					}
29590fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
296020b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
296120b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
29620fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
29630fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29640fa753b3SRandall Stewart 						}
29650fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
29660fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
29670fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
29680fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
29690fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
29700fa753b3SRandall Stewart #endif
29710fa753b3SRandall Stewart 						}
29720fa753b3SRandall Stewart 					}
29730fa753b3SRandall Stewart 					/*-
29740fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
29750fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
29760fa753b3SRandall Stewart 					 */
29772a498584SMichael Tuexen 					if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
2978325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
29790fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
29802a498584SMichael Tuexen 					}
29810fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
29820fa753b3SRandall Stewart 						/* deflate the cwnd */
29830fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
29840fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
29850fa753b3SRandall Stewart 					}
29860fa753b3SRandall Stewart 					/* NR Sack code here */
2987325c8c46SMichael Tuexen 					if (nr_sacking &&
2988325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2989325c8c46SMichael Tuexen 						if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
2990325c8c46SMichael Tuexen 							stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--;
2991325c8c46SMichael Tuexen #ifdef INVARIANTS
2992325c8c46SMichael Tuexen 						} else {
2993325c8c46SMichael Tuexen 							panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
2994325c8c46SMichael Tuexen #endif
2995325c8c46SMichael Tuexen 						}
2996d96bef9cSMichael Tuexen 						if ((stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues == 0) &&
2997d96bef9cSMichael Tuexen 						    (stcb->asoc.strmout[tp1->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
2998d96bef9cSMichael Tuexen 						    TAILQ_EMPTY(&stcb->asoc.strmout[tp1->rec.data.stream_number].outqueue)) {
2999d96bef9cSMichael Tuexen 							stcb->asoc.trigger_reset = 1;
3000d96bef9cSMichael Tuexen 						}
3001325c8c46SMichael Tuexen 						tp1->sent = SCTP_DATAGRAM_NR_ACKED;
30020fa753b3SRandall Stewart 						if (tp1->data) {
30030fa753b3SRandall Stewart 							/*
30040fa753b3SRandall Stewart 							 * sa_ignore
30050fa753b3SRandall Stewart 							 * NO_NULL_CHK
30060fa753b3SRandall Stewart 							 */
30070fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
30080fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
30090fa753b3SRandall Stewart 							tp1->data = NULL;
3010b5c16493SMichael Tuexen 						}
30110fa753b3SRandall Stewart 						wake_him++;
30120fa753b3SRandall Stewart 					}
30130fa753b3SRandall Stewart 				}
30140fa753b3SRandall Stewart 				break;
30150fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
301620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
30170fa753b3SRandall Stewart 				break;
301820b07a4dSMichael Tuexen 			}
30190fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3020b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
3021b5c16493SMichael Tuexen 				circled++;
30220fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
30230fa753b3SRandall Stewart 			}
3024b5c16493SMichael Tuexen 		}		/* end while (tp1) */
3025b5c16493SMichael Tuexen 		if (tp1 == NULL) {
3026b5c16493SMichael Tuexen 			circled = 0;
3027b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3028b5c16493SMichael Tuexen 		}
3029b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
30300fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
30310fa753b3SRandall Stewart 	*p_tp1 = tp1;
30320fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
30330fa753b3SRandall Stewart }
30340fa753b3SRandall Stewart 
30350fa753b3SRandall Stewart 
3036cd554309SMichael Tuexen static int
3037458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3038cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3039139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
30407215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
3041f8829a4aSRandall Stewart {
3042458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3043f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30440fa753b3SRandall Stewart 	int i;
3045f8829a4aSRandall Stewart 	int num_frs = 0;
3046cd554309SMichael Tuexen 	int chunk_freed;
3047cd554309SMichael Tuexen 	int non_revocable;
3048d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
3049f8829a4aSRandall Stewart 
3050d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3051d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
3052cd554309SMichael Tuexen 	chunk_freed = 0;
3053f8829a4aSRandall Stewart 
3054cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3055d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
3056d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
3057d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3058d9c5cfeaSMichael Tuexen 		}
3059458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3060458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3061458303daSRandall Stewart 		*offset += sizeof(block);
3062458303daSRandall Stewart 		if (frag == NULL) {
3063cd554309SMichael Tuexen 			return (chunk_freed);
3064458303daSRandall Stewart 		}
3065f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3066f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3067d9c5cfeaSMichael Tuexen 
3068f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3069d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
3070f8829a4aSRandall Stewart 			continue;
3071f8829a4aSRandall Stewart 		}
3072d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
3073d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
3074f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3075f8829a4aSRandall Stewart 		}
307620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3077d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
3078f8829a4aSRandall Stewart 		}
3079cd554309SMichael Tuexen 		if (i < num_seg) {
3080cd554309SMichael Tuexen 			non_revocable = 0;
3081cd554309SMichael Tuexen 		} else {
3082cd554309SMichael Tuexen 			non_revocable = 1;
3083cd554309SMichael Tuexen 		}
3084cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3085cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
30867215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
3087cd554309SMichael Tuexen 			chunk_freed = 1;
3088458303daSRandall Stewart 		}
3089d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
3090f8829a4aSRandall Stewart 	}
3091b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
309280fefe0aSRandall Stewart 		if (num_frs)
309380fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
309480fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
309580fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
309680fefe0aSRandall Stewart 	}
3097cd554309SMichael Tuexen 	return (chunk_freed);
3098f8829a4aSRandall Stewart }
3099f8829a4aSRandall Stewart 
3100f8829a4aSRandall Stewart static void
3101c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3102c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
310363eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3104f8829a4aSRandall Stewart {
3105f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3106f8829a4aSRandall Stewart 
31074a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
310820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3109f8829a4aSRandall Stewart 			/*
3110f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3111f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3112f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3113f8829a4aSRandall Stewart 			 * again.
3114f8829a4aSRandall Stewart 			 */
311520b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3116d06c82f1SRandall Stewart 				break;
311720b07a4dSMichael Tuexen 			}
3118f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3119f8829a4aSRandall Stewart 				/* it has been revoked */
3120f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3121f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3122a5d547adSRandall Stewart 				/*
312342551e99SRandall Stewart 				 * We must add this stuff back in to assure
312442551e99SRandall Stewart 				 * timers and such get started.
3125a5d547adSRandall Stewart 				 */
3126b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3127c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3128c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3129c105859eSRandall Stewart 					    tp1->book_size,
31309a8e3088SMichael Tuexen 					    (uint32_t) (uintptr_t) tp1->whoTo,
3131c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
313280fefe0aSRandall Stewart 				}
3133c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3134c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
313542551e99SRandall Stewart 				/*
313642551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
313742551e99SRandall Stewart 				 * artificial inflation of the flight_size.
313842551e99SRandall Stewart 				 */
313942551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3140b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3141f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3142f8829a4aSRandall Stewart 					    cumack,
3143f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3144f8829a4aSRandall Stewart 					    0,
3145f8829a4aSRandall Stewart 					    0,
3146f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
314780fefe0aSRandall Stewart 				}
3148f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3149f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3150f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3151f8829a4aSRandall Stewart 			}
3152f8829a4aSRandall Stewart 		}
3153f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3154f8829a4aSRandall Stewart 			break;
3155f8829a4aSRandall Stewart 	}
3156f8829a4aSRandall Stewart }
3157f8829a4aSRandall Stewart 
3158830d754dSRandall Stewart 
3159f8829a4aSRandall Stewart static void
3160f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
316163eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3162f8829a4aSRandall Stewart {
3163f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3164f8829a4aSRandall Stewart 	int strike_flag = 0;
3165f8829a4aSRandall Stewart 	struct timeval now;
3166f8829a4aSRandall Stewart 	int tot_retrans = 0;
3167f8829a4aSRandall Stewart 	uint32_t sending_seq;
3168f8829a4aSRandall Stewart 	struct sctp_nets *net;
3169f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3170f8829a4aSRandall Stewart 
3171f8829a4aSRandall Stewart 	/*
3172f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3173f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3174f8829a4aSRandall Stewart 	 */
3175f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3176f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3177f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3178f8829a4aSRandall Stewart 	} else {
3179f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3180f8829a4aSRandall Stewart 	}
3181f8829a4aSRandall Stewart 
3182f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
31837c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
318420083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3185f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3186f8829a4aSRandall Stewart 			if (net->saw_newack)
3187f8829a4aSRandall Stewart 				num_dests_sacked++;
3188f8829a4aSRandall Stewart 		}
3189f8829a4aSRandall Stewart 	}
3190dd973b0eSMichael Tuexen 	if (stcb->asoc.prsctp_supported) {
31916e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3192f8829a4aSRandall Stewart 	}
31934a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3194f8829a4aSRandall Stewart 		strike_flag = 0;
3195f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3196f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3197f8829a4aSRandall Stewart 			continue;
3198f8829a4aSRandall Stewart 		}
3199b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3200f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3201f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3202f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3203f8829a4aSRandall Stewart 				    tp1->sent,
3204f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
320580fefe0aSRandall Stewart 		}
320620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3207f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3208f8829a4aSRandall Stewart 			/* done */
3209f8829a4aSRandall Stewart 			break;
3210f8829a4aSRandall Stewart 		}
3211dd973b0eSMichael Tuexen 		if (stcb->asoc.prsctp_supported) {
3212f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3213f8829a4aSRandall Stewart 				/* Is it expired? */
321499ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3215f8829a4aSRandall Stewart 					/* Yes so drop it */
3216f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
32171edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
32180c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3219f8829a4aSRandall Stewart 					}
3220f8829a4aSRandall Stewart 					continue;
3221f8829a4aSRandall Stewart 				}
3222f8829a4aSRandall Stewart 			}
3223f8829a4aSRandall Stewart 		}
322420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3225f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3226f8829a4aSRandall Stewart 			break;
3227f8829a4aSRandall Stewart 		}
3228f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3229f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3230f8829a4aSRandall Stewart 			/* skip */
323144fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
323244fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
323344fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
323444fbe462SRandall Stewart 			}
3235f8829a4aSRandall Stewart 			continue;
3236f8829a4aSRandall Stewart 		}
3237f8829a4aSRandall Stewart 		/*
3238f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3239f8829a4aSRandall Stewart 		 */
3240ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3241f8829a4aSRandall Stewart 			/*
3242f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3243f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3244f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3245c105859eSRandall Stewart 			 * FR using this SACK.
3246f8829a4aSRandall Stewart 			 */
3247f8829a4aSRandall Stewart 			continue;
324820b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
324920b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3250f8829a4aSRandall Stewart 			/*
3251f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3252f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3253f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3254f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3255f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3256f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3257f8829a4aSRandall Stewart 			 */
3258f8829a4aSRandall Stewart 			continue;
3259f8829a4aSRandall Stewart 		}
3260f8829a4aSRandall Stewart 		/*
3261f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3262f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3263f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3264f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3265f8829a4aSRandall Stewart 		 */
3266f8829a4aSRandall Stewart 		/*
326742551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
326842551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
326942551e99SRandall Stewart 		 * 0)) {
3270f8829a4aSRandall Stewart 		 */
327142551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3272f8829a4aSRandall Stewart 			/*
3273f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3274f8829a4aSRandall Stewart 			 * moved.
3275f8829a4aSRandall Stewart 			 */
3276b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3277f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3278f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3279f8829a4aSRandall Stewart 				    tp1->sent,
3280f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
328180fefe0aSRandall Stewart 			}
32825e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3283f8829a4aSRandall Stewart 				tp1->sent++;
32845e54f665SRandall Stewart 			}
32857c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
328620083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3287f8829a4aSRandall Stewart 				/*
3288f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3289f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3290f8829a4aSRandall Stewart 				 * because it would have been set to the
3291f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3292f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3293f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3294c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3295c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3296c105859eSRandall Stewart 				 * indicates that two packets have been
3297c105859eSRandall Stewart 				 * received after this missing TSN.
32985e54f665SRandall Stewart 				 */
32995e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
330020b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3301b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3302f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3303f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3304f8829a4aSRandall Stewart 						    tp1->sent,
3305f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
330680fefe0aSRandall Stewart 					}
3307f8829a4aSRandall Stewart 					tp1->sent++;
3308f8829a4aSRandall Stewart 				}
3309f8829a4aSRandall Stewart 			}
331020083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
331120083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3312f8829a4aSRandall Stewart 			/*
3313f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3314f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3315f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3316f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3317f8829a4aSRandall Stewart 			 */
33185e54f665SRandall Stewart 			if (
3319f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3320f8829a4aSRandall Stewart 			/*
3321f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3322f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3323f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3324f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3325f8829a4aSRandall Stewart 			 */
33265e54f665SRandall Stewart 			    (asoc->numnets < 2)
3327f8829a4aSRandall Stewart #else
33285e54f665SRandall Stewart 			    (1)
3329f8829a4aSRandall Stewart #endif
33305e54f665SRandall Stewart 			    ) {
33315e54f665SRandall Stewart 
333220b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3333f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3334f8829a4aSRandall Stewart 					/*
3335f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3336f8829a4aSRandall Stewart 					 * beyond where things were when we
3337f8829a4aSRandall Stewart 					 * did a FR.
3338f8829a4aSRandall Stewart 					 */
3339b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3340f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3341f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3342f8829a4aSRandall Stewart 						    tp1->sent,
3343f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
334480fefe0aSRandall Stewart 					}
33455e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3346f8829a4aSRandall Stewart 						tp1->sent++;
33475e54f665SRandall Stewart 					}
3348f8829a4aSRandall Stewart 					strike_flag = 1;
33497c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
335020083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3351f8829a4aSRandall Stewart 						/*
3352f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3353f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3354f8829a4aSRandall Stewart 						 * then lowest_newack test
3355f8829a4aSRandall Stewart 						 * will not pass because it
3356f8829a4aSRandall Stewart 						 * would have been set to
3357f8829a4aSRandall Stewart 						 * the cumack earlier. If
3358f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3359f8829a4aSRandall Stewart 						 * If not a mixed sack and
3360f8829a4aSRandall Stewart 						 * if tp1 is not between two
3361f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3362c105859eSRandall Stewart 						 * one more. NOTE that we
3363c105859eSRandall Stewart 						 * are marking by one
3364c105859eSRandall Stewart 						 * additional time since the
3365c105859eSRandall Stewart 						 * SACK DAC flag indicates
3366c105859eSRandall Stewart 						 * that two packets have
3367c105859eSRandall Stewart 						 * been received after this
3368c105859eSRandall Stewart 						 * missing TSN.
3369f8829a4aSRandall Stewart 						 */
33705e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
33715e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
337220b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
337320b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3374b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3375f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3376f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3377f8829a4aSRandall Stewart 								    tp1->sent,
3378f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
337980fefe0aSRandall Stewart 							}
33805e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3381f8829a4aSRandall Stewart 								tp1->sent++;
33825e54f665SRandall Stewart 							}
3383f8829a4aSRandall Stewart 						}
3384f8829a4aSRandall Stewart 					}
3385f8829a4aSRandall Stewart 				}
3386f8829a4aSRandall Stewart 			}
3387f8829a4aSRandall Stewart 			/*
338842551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
338942551e99SRandall Stewart 			 * algo covers HTNA.
3390f8829a4aSRandall Stewart 			 */
339120b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
339220b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3393f8829a4aSRandall Stewart 			/*
3394f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3395f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3396f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3397f8829a4aSRandall Stewart 			 */
3398f8829a4aSRandall Stewart 			;
3399f8829a4aSRandall Stewart 		} else {
3400f8829a4aSRandall Stewart 			/* Strike the TSN */
3401b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3402f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3403f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3404f8829a4aSRandall Stewart 				    tp1->sent,
3405f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
340680fefe0aSRandall Stewart 			}
34075e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3408f8829a4aSRandall Stewart 				tp1->sent++;
34095e54f665SRandall Stewart 			}
34107c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
341120083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3412f8829a4aSRandall Stewart 				/*
3413f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3414f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3415f8829a4aSRandall Stewart 				 * because it would have been set to the
3416f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3417f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3418f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3419c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3420c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3421c105859eSRandall Stewart 				 * indicates that two packets have been
3422c105859eSRandall Stewart 				 * received after this missing TSN.
3423f8829a4aSRandall Stewart 				 */
34245e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
342520b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3426b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3427f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3428f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3429f8829a4aSRandall Stewart 						    tp1->sent,
3430f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
343180fefe0aSRandall Stewart 					}
3432f8829a4aSRandall Stewart 					tp1->sent++;
3433f8829a4aSRandall Stewart 				}
3434f8829a4aSRandall Stewart 			}
3435f8829a4aSRandall Stewart 		}
3436f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3437f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3438f8829a4aSRandall Stewart 
3439544e35bdSRandall Stewart 			/* fix counts and things */
3440544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3441544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3442544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3443544e35bdSRandall Stewart 				    tp1->book_size,
34449a8e3088SMichael Tuexen 				    (uint32_t) (uintptr_t) tp1->whoTo,
3445544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3446544e35bdSRandall Stewart 			}
3447544e35bdSRandall Stewart 			if (tp1->whoTo) {
3448544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3449544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3450299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3451299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3452299108c5SRandall Stewart 					    tp1);
3453299108c5SRandall Stewart 				}
3454544e35bdSRandall Stewart 			}
3455544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3456544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3457544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3458544e35bdSRandall Stewart 			}
3459544e35bdSRandall Stewart 			/* add back to the rwnd */
3460544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3461544e35bdSRandall Stewart 
3462544e35bdSRandall Stewart 			/* remove from the total flight */
3463544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3464544e35bdSRandall Stewart 
3465dd973b0eSMichael Tuexen 			if ((stcb->asoc.prsctp_supported) &&
3466475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3467475d0674SMichael Tuexen 				/*
3468475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3469475d0674SMichael Tuexen 				 * we store the retran count there.
3470475d0674SMichael Tuexen 				 */
3471475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3472475d0674SMichael Tuexen 					/* Yes, so drop it */
3473475d0674SMichael Tuexen 					if (tp1->data != NULL) {
34741edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3475475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3476475d0674SMichael Tuexen 					}
3477475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3478475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3479475d0674SMichael Tuexen 					continue;
3480475d0674SMichael Tuexen 				}
3481475d0674SMichael Tuexen 			}
3482cd3fd531SMichael Tuexen 			/*
3483cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3484cd3fd531SMichael Tuexen 			 * guy\n");
3485cd3fd531SMichael Tuexen 			 */
3486b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3487f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3488f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
348980fefe0aSRandall Stewart 			}
3490f8829a4aSRandall Stewart 			if (strike_flag) {
3491f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3492f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3493f8829a4aSRandall Stewart 			}
34945e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
34957c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3496f8829a4aSRandall Stewart 				/*
3497f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3498f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3499f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3500f8829a4aSRandall Stewart 				 */
3501f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3502f8829a4aSRandall Stewart 				alt = tp1->whoTo;
35033c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
350420083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3505b54d3a6cSRandall Stewart 					/*
3506b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3507b54d3a6cSRandall Stewart 					 * use the PF version of
3508b54d3a6cSRandall Stewart 					 * find_alt_net()
3509b54d3a6cSRandall Stewart 					 */
3510b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3511b54d3a6cSRandall Stewart 				} else {
3512b54d3a6cSRandall Stewart 					/*
3513b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3514b54d3a6cSRandall Stewart 					 * use the CMT version of
3515b54d3a6cSRandall Stewart 					 * find_alt_net()
3516b54d3a6cSRandall Stewart 					 */
351752be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3518f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3519b54d3a6cSRandall Stewart 				}
3520ad81507eSRandall Stewart 				if (alt == NULL) {
3521ad81507eSRandall Stewart 					alt = tp1->whoTo;
3522ad81507eSRandall Stewart 				}
3523f8829a4aSRandall Stewart 				/*
3524f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3525f8829a4aSRandall Stewart 				 * the retransmission, then new
3526f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3527f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3528f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3529f8829a4aSRandall Stewart 				 */
3530ad81507eSRandall Stewart 				if (tp1->whoTo) {
3531f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3532f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3533ad81507eSRandall Stewart 				}
3534f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3535f8829a4aSRandall Stewart 
3536f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3537f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3538f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3539f8829a4aSRandall Stewart #else
3540f8829a4aSRandall Stewart 				/*
3541f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3542f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3543f8829a4aSRandall Stewart 				 * paper details why.
3544f8829a4aSRandall Stewart 				 */
3545f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3546f8829a4aSRandall Stewart #endif
3547f8829a4aSRandall Stewart 			}
3548f8829a4aSRandall Stewart 
3549f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3550f8829a4aSRandall Stewart 			tot_retrans++;
3551f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3552f8829a4aSRandall Stewart 			/*
3553cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3554f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3555f8829a4aSRandall Stewart 			 */
3556f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3557f8829a4aSRandall Stewart 				/*
3558f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3559f8829a4aSRandall Stewart 				 * the next sequence number that will be
3560f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3561f8829a4aSRandall Stewart 				 * get the one we last sent.
3562f8829a4aSRandall Stewart 				 */
3563f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3564f8829a4aSRandall Stewart 			} else {
3565f8829a4aSRandall Stewart 				/*
3566f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3567f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3568f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3569f8829a4aSRandall Stewart 				 * take the first one (which will have the
3570f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3571f8829a4aSRandall Stewart 				 * one we last sent.
3572f8829a4aSRandall Stewart 				 */
3573f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3574f8829a4aSRandall Stewart 
3575f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3576f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3577f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3578f8829a4aSRandall Stewart 			}
3579f8829a4aSRandall Stewart 
3580f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3581f8829a4aSRandall Stewart 				/*
3582f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3583f8829a4aSRandall Stewart 				 * it, cancel it
3584f8829a4aSRandall Stewart 				 */
358560990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
358660990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3587f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3588f79aab18SRandall Stewart 				}
3589f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3590f8829a4aSRandall Stewart 			}
3591f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3592f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3593f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
35943c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3595f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3596f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3597f8829a4aSRandall Stewart 			}
3598f8829a4aSRandall Stewart 		}
35994a9ef3f8SMichael Tuexen 	}
3600f8829a4aSRandall Stewart }
3601f8829a4aSRandall Stewart 
3602f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3603f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3604f8829a4aSRandall Stewart     struct sctp_association *asoc)
3605f8829a4aSRandall Stewart {
3606f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3607f8829a4aSRandall Stewart 	struct timeval now;
3608f8829a4aSRandall Stewart 	int now_filled = 0;
3609f8829a4aSRandall Stewart 
3610dd973b0eSMichael Tuexen 	if (asoc->prsctp_supported == 0) {
3611f8829a4aSRandall Stewart 		return (NULL);
3612f8829a4aSRandall Stewart 	}
36134a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3614f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
361598f2956cSMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_RESEND &&
3616325c8c46SMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3617f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3618f8829a4aSRandall Stewart 			break;
3619f8829a4aSRandall Stewart 		}
36200c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
36212a498584SMichael Tuexen 			if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3622325c8c46SMichael Tuexen 			    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
36230c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
36240c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
36250c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
36260c0982b8SRandall Stewart 			}
36270c0982b8SRandall Stewart 		}
3628f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3629f8829a4aSRandall Stewart 			/*
3630f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3631f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3632f8829a4aSRandall Stewart 			 */
3633f8829a4aSRandall Stewart 			break;
3634f8829a4aSRandall Stewart 		}
3635f8829a4aSRandall Stewart 		if (!now_filled) {
36366e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3637f8829a4aSRandall Stewart 			now_filled = 1;
3638f8829a4aSRandall Stewart 		}
3639f8829a4aSRandall Stewart 		/*
3640f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3641f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3642f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3643f8829a4aSRandall Stewart 		 * it if its a resend?
3644f8829a4aSRandall Stewart 		 */
3645f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3646f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3647f8829a4aSRandall Stewart 			/*
3648f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3649f8829a4aSRandall Stewart 			 * now up?
3650f8829a4aSRandall Stewart 			 */
3651f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3652f8829a4aSRandall Stewart 				/* Yes so drop it */
3653f8829a4aSRandall Stewart 				if (tp1->data) {
3654ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
36551edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3656f8829a4aSRandall Stewart 				}
3657f8829a4aSRandall Stewart 			} else {
3658f8829a4aSRandall Stewart 				/*
3659f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3660f8829a4aSRandall Stewart 				 * whos time as not expired.
3661f8829a4aSRandall Stewart 				 */
3662f8829a4aSRandall Stewart 				break;
3663f8829a4aSRandall Stewart 			}
3664f8829a4aSRandall Stewart 		}
3665f8829a4aSRandall Stewart 		/*
3666f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3667f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3668f8829a4aSRandall Stewart 		 * the next chunk.
3669f8829a4aSRandall Stewart 		 */
367098f2956cSMichael Tuexen 		if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3671325c8c46SMichael Tuexen 		    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3672f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
367320b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3674f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3675f8829a4aSRandall Stewart 				a_adv = tp1;
36760c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
36770c0982b8SRandall Stewart 				/* No update but we do save the chk */
36780c0982b8SRandall Stewart 				a_adv = tp1;
36790c0982b8SRandall Stewart 			}
3680f8829a4aSRandall Stewart 		} else {
3681f8829a4aSRandall Stewart 			/*
3682f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3683f8829a4aSRandall Stewart 			 * further
3684f8829a4aSRandall Stewart 			 */
3685f8829a4aSRandall Stewart 			break;
3686f8829a4aSRandall Stewart 		}
3687f8829a4aSRandall Stewart 	}
3688f8829a4aSRandall Stewart 	return (a_adv);
3689f8829a4aSRandall Stewart }
3690f8829a4aSRandall Stewart 
36910c0982b8SRandall Stewart static int
3692c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3693bff64a4dSRandall Stewart {
3694bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3695afd67482SMichael Tuexen 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3696548f47a8SMichael Tuexen 	int ret;
36970c0982b8SRandall Stewart 
3698548f47a8SMichael Tuexen #ifndef INVARIANTS
3699548f47a8SMichael Tuexen 	int entry_flight, entry_cnt;
3700548f47a8SMichael Tuexen 
3701548f47a8SMichael Tuexen #endif
3702548f47a8SMichael Tuexen 
3703548f47a8SMichael Tuexen 	ret = 0;
3704548f47a8SMichael Tuexen #ifndef INVARIANTS
37050c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
37060c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
3707548f47a8SMichael Tuexen #endif
37080c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
37090c0982b8SRandall Stewart 		return (0);
3710bff64a4dSRandall Stewart 
3711bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3712bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3713cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN: %u size: %d inflight cnt: %d\n",
37140c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
37150c0982b8SRandall Stewart 			    chk->send_size,
3716cd3fd531SMichael Tuexen 			    chk->snd_count);
3717bff64a4dSRandall Stewart 			inflight++;
3718bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3719bff64a4dSRandall Stewart 			resend++;
3720bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3721bff64a4dSRandall Stewart 			inbetween++;
3722bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3723bff64a4dSRandall Stewart 			above++;
3724bff64a4dSRandall Stewart 		} else {
3725bff64a4dSRandall Stewart 			acked++;
3726bff64a4dSRandall Stewart 		}
3727bff64a4dSRandall Stewart 	}
3728f1f73e57SRandall Stewart 
3729c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3730f1f73e57SRandall Stewart #ifdef INVARIANTS
3731c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3732f1f73e57SRandall Stewart #else
3733cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight: %d cnt: %d\n",
37340c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
37350c0982b8SRandall Stewart 
37360c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F: %d I: %d R: %d Ab: %d ACK: %d\n",
37370c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
37380c0982b8SRandall Stewart 		ret = 1;
3739f1f73e57SRandall Stewart #endif
3740bff64a4dSRandall Stewart 	}
37410c0982b8SRandall Stewart 	return (ret);
3742c105859eSRandall Stewart }
3743c105859eSRandall Stewart 
3744c105859eSRandall Stewart 
3745c105859eSRandall Stewart static void
3746c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3747c105859eSRandall Stewart     struct sctp_association *asoc,
3748c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3749c105859eSRandall Stewart {
3750dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
37515171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3752dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3753dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
37548427b3fdSMichael Tuexen 		    tp1->whoTo ? tp1->whoTo->flight_size : 0,
3755dfb11ef8SRandall Stewart 		    tp1->book_size,
37569a8e3088SMichael Tuexen 		    (uint32_t) (uintptr_t) tp1->whoTo,
3757dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3758dfb11ef8SRandall Stewart 		return;
3759dfb11ef8SRandall Stewart 	}
37605171328bSRandall Stewart 	/* First setup this by shrinking flight */
3761299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3762299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3763299108c5SRandall Stewart 		    tp1);
3764299108c5SRandall Stewart 	}
37655171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
37665171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
37675171328bSRandall Stewart 	/* Now mark for resend */
37685171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3769791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3770791437b5SRandall Stewart 
3771b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3772c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3773c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3774c105859eSRandall Stewart 		    tp1->book_size,
37759a8e3088SMichael Tuexen 		    (uint32_t) (uintptr_t) tp1->whoTo,
3776c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
377780fefe0aSRandall Stewart 	}
3778c105859eSRandall Stewart }
3779c105859eSRandall Stewart 
3780f8829a4aSRandall Stewart void
3781f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3782899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3783f8829a4aSRandall Stewart {
3784f8829a4aSRandall Stewart 	struct sctp_nets *net;
3785f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3786f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
37875e54f665SRandall Stewart 	uint32_t old_rwnd;
37885e54f665SRandall Stewart 	int win_probe_recovery = 0;
3789c105859eSRandall Stewart 	int win_probe_recovered = 0;
3790d06c82f1SRandall Stewart 	int j, done_once = 0;
3791f79aab18SRandall Stewart 	int rto_ok = 1;
3792fd60718dSMichael Tuexen 	uint32_t send_s;
3793f8829a4aSRandall Stewart 
3794b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3795d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3796d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
379780fefe0aSRandall Stewart 	}
3798f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
379918e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
380018e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
380118e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
380218e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
380318e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
380418e198d3SRandall Stewart 	}
380518e198d3SRandall Stewart #endif
3806f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3807d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
380820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
38095e54f665SRandall Stewart 		/* old ack */
38105e54f665SRandall Stewart 		return;
3811d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3812d06c82f1SRandall Stewart 		/* Window update sack */
3813d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
381444fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3815d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3816d06c82f1SRandall Stewart 			/* SWS sender side engages */
3817d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3818d06c82f1SRandall Stewart 		}
3819d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3820d06c82f1SRandall Stewart 			goto again;
3821d06c82f1SRandall Stewart 		}
3822d06c82f1SRandall Stewart 		return;
38235e54f665SRandall Stewart 	}
3824f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3825f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3826a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3827a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3828a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3829a21779f0SRandall Stewart 		}
3830f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3831f8829a4aSRandall Stewart 		net->net_ack = 0;
3832f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3833132dea7dSRandall Stewart 
3834132dea7dSRandall Stewart 		/*
3835132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3836132dea7dSRandall Stewart 		 * SACK processing
3837132dea7dSRandall Stewart 		 */
3838132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3839132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3840299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3841299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3842299108c5SRandall Stewart 		}
3843f8829a4aSRandall Stewart 	}
3844c105859eSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3845c105859eSRandall Stewart 		tp1 = TAILQ_LAST(&asoc->sent_queue,
3846c105859eSRandall Stewart 		    sctpchunk_listhead);
3847c105859eSRandall Stewart 		send_s = tp1->rec.data.TSN_seq + 1;
3848139bc87fSRandall Stewart 	} else {
3849c105859eSRandall Stewart 		send_s = asoc->sending_seq;
3850139bc87fSRandall Stewart 	}
385120b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(cumack, send_s)) {
3852ff1ffd74SMichael Tuexen 		struct mbuf *op_err;
3853ff1ffd74SMichael Tuexen 		char msg[SCTP_DIAG_INFO_LEN];
3854139bc87fSRandall Stewart 
3855139bc87fSRandall Stewart 		*abort_now = 1;
3856139bc87fSRandall Stewart 		/* XXX */
385755f8a4bbSMichael Tuexen 		snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x",
3858ff1ffd74SMichael Tuexen 		    cumack, send_s);
3859ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
386044249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_21;
3861ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3862139bc87fSRandall Stewart 		return;
3863139bc87fSRandall Stewart 	}
3864f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3865b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3866c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3867c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3868c4739e2fSRandall Stewart 		    0,
3869c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3870c4739e2fSRandall Stewart 		    __LINE__);
3871c4739e2fSRandall Stewart 	}
3872f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
387320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3874f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
38754a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
387620b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
387718e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3878cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
387918e198d3SRandall Stewart 				}
3880f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3881f8829a4aSRandall Stewart 					/*
388218e198d3SRandall Stewart 					 * If it is less than ACKED, it is
388318e198d3SRandall Stewart 					 * now no-longer in flight. Higher
388418e198d3SRandall Stewart 					 * values may occur during marking
3885f8829a4aSRandall Stewart 					 */
3886c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3887b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3888c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3889a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3890a5d547adSRandall Stewart 							    tp1->book_size,
38919a8e3088SMichael Tuexen 							    (uint32_t) (uintptr_t) tp1->whoTo,
3892a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
389380fefe0aSRandall Stewart 						}
3894c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3895299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3896299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3897299108c5SRandall Stewart 							    tp1);
3898299108c5SRandall Stewart 						}
389904ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3900c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3901f8829a4aSRandall Stewart 					}
3902f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3903f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3904f8829a4aSRandall Stewart 						/*
390518e198d3SRandall Stewart 						 * True non-retransmited
3906f8829a4aSRandall Stewart 						 * chunk
3907f8829a4aSRandall Stewart 						 */
3908f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3909f8829a4aSRandall Stewart 						    tp1->send_size;
3910f8829a4aSRandall Stewart 
3911f8829a4aSRandall Stewart 						/* update RTO too? */
391262c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3913f79aab18SRandall Stewart 							if (rto_ok) {
3914f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
391504ee05e8SRandall Stewart 								/*
391604ee05e8SRandall Stewart 								 * sa_ignore
3917f79aab18SRandall Stewart 								 * NO_NULL_CH
3918f79aab18SRandall Stewart 								 * K
391904ee05e8SRandall Stewart 								 */
3920f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3921f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
392218e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3923899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3924f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3925f79aab18SRandall Stewart 								rto_ok = 0;
3926f79aab18SRandall Stewart 							}
3927f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3928f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3929f79aab18SRandall Stewart 							}
3930f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3931f8829a4aSRandall Stewart 						}
3932f8829a4aSRandall Stewart 					}
3933132dea7dSRandall Stewart 					/*
393418e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
393518e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
393618e198d3SRandall Stewart 					 * acked for the first time, set the
393718e198d3SRandall Stewart 					 * following variables for the
393818e198d3SRandall Stewart 					 * corresp destination.
393918e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
394018e198d3SRandall Stewart 					 * cwnd update.
394118e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
394218e198d3SRandall Stewart 					 * trigger search for the next
394318e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3944132dea7dSRandall Stewart 					 */
3945132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3946132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3947132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3948132dea7dSRandall Stewart 
3949b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
395004ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3951f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
395280fefe0aSRandall Stewart 					}
3953f8829a4aSRandall Stewart 				}
3954f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3955f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3956f8829a4aSRandall Stewart 				}
395742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
395842551e99SRandall Stewart 					/* deflate the cwnd */
395942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
396042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
396142551e99SRandall Stewart 				}
3962325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3963a7ad6026SMichael Tuexen 					if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
3964a7ad6026SMichael Tuexen 						asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
3965a7ad6026SMichael Tuexen #ifdef INVARIANTS
3966a7ad6026SMichael Tuexen 					} else {
3967a7ad6026SMichael Tuexen 						panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
3968a7ad6026SMichael Tuexen #endif
3969a7ad6026SMichael Tuexen 					}
3970a7ad6026SMichael Tuexen 				}
3971d96bef9cSMichael Tuexen 				if ((asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues == 0) &&
3972d96bef9cSMichael Tuexen 				    (asoc->strmout[tp1->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
3973d96bef9cSMichael Tuexen 				    TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.stream_number].outqueue)) {
3974d96bef9cSMichael Tuexen 					asoc->trigger_reset = 1;
3975d96bef9cSMichael Tuexen 				}
3976f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3977f8829a4aSRandall Stewart 				if (tp1->data) {
397804ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3979f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3980f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
39814a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3982f8829a4aSRandall Stewart 				}
3983b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3984f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3985f8829a4aSRandall Stewart 					    cumack,
3986f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3987f8829a4aSRandall Stewart 					    0,
3988f8829a4aSRandall Stewart 					    0,
3989f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
399080fefe0aSRandall Stewart 				}
3991f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3992689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
399318e198d3SRandall Stewart 			} else {
399418e198d3SRandall Stewart 				break;
3995f8829a4aSRandall Stewart 			}
39965e54f665SRandall Stewart 		}
399718e198d3SRandall Stewart 
399818e198d3SRandall Stewart 	}
399904ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4000f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
4001ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4002ceaad40aSRandall Stewart 		struct socket *so;
4003ceaad40aSRandall Stewart 
4004ceaad40aSRandall Stewart #endif
4005f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4006b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
400704ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
40087215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
400980fefe0aSRandall Stewart 		}
4010ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4011ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4012ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4013ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4014ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4015ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4016ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4017ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4018ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4019ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4020ceaad40aSRandall Stewart 			return;
4021ceaad40aSRandall Stewart 		}
4022ceaad40aSRandall Stewart #endif
4023f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4024ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4025ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4026ceaad40aSRandall Stewart #endif
4027f8829a4aSRandall Stewart 	} else {
4028b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
40297215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
403080fefe0aSRandall Stewart 		}
4031f8829a4aSRandall Stewart 	}
4032f8829a4aSRandall Stewart 
4033b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4034ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4035ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4036ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4037ca85e948SMichael Tuexen 				/*
4038ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4039ca85e948SMichael Tuexen 				 * count, this is optional.
4040ca85e948SMichael Tuexen 				 */
4041ca85e948SMichael Tuexen 				net->error_count = 0;
4042ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4043ca85e948SMichael Tuexen 					/* addr came good */
4044ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4045ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
40464b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4047ca85e948SMichael Tuexen 				}
4048ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4049ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4050ca85e948SMichael Tuexen 						/*
4051ca85e948SMichael Tuexen 						 * release the alternate,
4052ca85e948SMichael Tuexen 						 * primary is good
4053ca85e948SMichael Tuexen 						 */
4054ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4055ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4056ca85e948SMichael Tuexen 					}
4057ca85e948SMichael Tuexen 				}
4058ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4059ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4060b7d130beSMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4061b7d130beSMichael Tuexen 					    stcb->sctp_ep, stcb, net,
406244249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4063ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4064ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4065ca85e948SMichael Tuexen 					/* Done with this net */
4066ca85e948SMichael Tuexen 					net->net_ack = 0;
4067ca85e948SMichael Tuexen 				}
4068ca85e948SMichael Tuexen 				/* restore any doubled timers */
4069ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4070ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4071ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4072ca85e948SMichael Tuexen 				}
4073ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4074ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4075ca85e948SMichael Tuexen 				}
4076ca85e948SMichael Tuexen 			}
4077ca85e948SMichael Tuexen 		}
4078b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4079ca85e948SMichael Tuexen 	}
4080f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
40815e54f665SRandall Stewart 
4082f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4083f8829a4aSRandall Stewart 		/* nothing left in-flight */
4084f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4085f8829a4aSRandall Stewart 			net->flight_size = 0;
4086f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4087f8829a4aSRandall Stewart 		}
4088f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4089f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4090f8829a4aSRandall Stewart 	}
4091f8829a4aSRandall Stewart 	/* RWND update */
4092f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
409344fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4094f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4095f8829a4aSRandall Stewart 		/* SWS sender side engages */
4096f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4097f8829a4aSRandall Stewart 	}
40985e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
40995e54f665SRandall Stewart 		win_probe_recovery = 1;
41005e54f665SRandall Stewart 	}
4101f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4102a5d547adSRandall Stewart again:
4103a5d547adSRandall Stewart 	j = 0;
4104f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
41055171328bSRandall Stewart 		int to_ticks;
41065171328bSRandall Stewart 
41075e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4108c105859eSRandall Stewart 			win_probe_recovered = 1;
41095e54f665SRandall Stewart 			/*
41105e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
41115e54f665SRandall Stewart 			 * and clear the sent
41125e54f665SRandall Stewart 			 */
41133c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
41145e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
41155e54f665SRandall Stewart 				if (tp1->window_probe) {
4116cd554309SMichael Tuexen 					/* move back to data send queue */
41177215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
41185e54f665SRandall Stewart 					break;
41195e54f665SRandall Stewart 				}
41205e54f665SRandall Stewart 			}
41215e54f665SRandall Stewart 		}
4122f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4123f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4124f8829a4aSRandall Stewart 		} else {
4125f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4126f8829a4aSRandall Stewart 		}
41275171328bSRandall Stewart 		if (net->flight_size) {
4128a5d547adSRandall Stewart 			j++;
4129ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4130f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
41315171328bSRandall Stewart 			if (net->window_probe) {
41325171328bSRandall Stewart 				net->window_probe = 0;
41335171328bSRandall Stewart 			}
4134f8829a4aSRandall Stewart 		} else {
41355171328bSRandall Stewart 			if (net->window_probe) {
41365171328bSRandall Stewart 				/*
41375171328bSRandall Stewart 				 * In window probes we must assure a timer
41385171328bSRandall Stewart 				 * is still running there
41395171328bSRandall Stewart 				 */
41405171328bSRandall Stewart 				net->window_probe = 0;
41415171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
41425171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
41435171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
41445171328bSRandall Stewart 				}
41455171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4146f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4147a5d547adSRandall Stewart 				    stcb, net,
414844249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4149f8829a4aSRandall Stewart 			}
4150f8829a4aSRandall Stewart 		}
4151f8829a4aSRandall Stewart 	}
4152bff64a4dSRandall Stewart 	if ((j == 0) &&
4153bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4154bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4155c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4156bff64a4dSRandall Stewart 	    (done_once == 0)) {
41570c0982b8SRandall Stewart 		/*
41580c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
41590c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
41600c0982b8SRandall Stewart 		 */
41610c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4162a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4163a5d547adSRandall Stewart 				net->flight_size = 0;
4164a5d547adSRandall Stewart 			}
4165a5d547adSRandall Stewart 			asoc->total_flight = 0;
4166a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4167a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4168a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4169a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4170c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4171c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4172a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4173791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4174a5d547adSRandall Stewart 				}
4175a5d547adSRandall Stewart 			}
41760c0982b8SRandall Stewart 		}
4177bff64a4dSRandall Stewart 		done_once = 1;
4178a5d547adSRandall Stewart 		goto again;
4179a5d547adSRandall Stewart 	}
4180f8829a4aSRandall Stewart 	/**********************************/
4181f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4182f8829a4aSRandall Stewart 	/**********************************/
4183f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4184f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4185f8829a4aSRandall Stewart 		/* clean up */
4186f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4187f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4188f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4189*d1ea5fa9SMichael Tuexen 		    ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc))) {
4190f8829a4aSRandall Stewart 			asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
41912afb3e84SRandall Stewart 		}
4192f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4193f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4194f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4195f8829a4aSRandall Stewart 				/* Need to abort here */
4196ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4197f8829a4aSRandall Stewart 
4198f8829a4aSRandall Stewart 		abort_out_now:
4199f8829a4aSRandall Stewart 				*abort_now = 1;
4200f8829a4aSRandall Stewart 				/* XXX */
4201ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
420244249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4203ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4204fe4a59b3SMichael Tuexen 				return;
4205f8829a4aSRandall Stewart 			} else {
4206ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4207ca85e948SMichael Tuexen 
4208f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4209f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4210f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4211f42a358aSRandall Stewart 				}
4212c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4213b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4214f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4215ca85e948SMichael Tuexen 				if (asoc->alternate) {
4216ca85e948SMichael Tuexen 					netp = asoc->alternate;
4217ca85e948SMichael Tuexen 				} else {
4218ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4219ca85e948SMichael Tuexen 				}
4220ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4221f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4222ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4223f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4224ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4225f8829a4aSRandall Stewart 			}
4226f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4227f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4228ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4229ca85e948SMichael Tuexen 
4230f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4231f8829a4aSRandall Stewart 				goto abort_out_now;
4232f8829a4aSRandall Stewart 			}
4233f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4234c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4235b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
423612af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4237c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4238c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4239c39cfa1fSMichael Tuexen 			} else {
4240c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4241c39cfa1fSMichael Tuexen 			}
4242c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4243f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4244ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4245f8829a4aSRandall Stewart 		}
4246f8829a4aSRandall Stewart 	}
4247dfb11ef8SRandall Stewart 	/*********************************************/
4248dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4249dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4250dfb11ef8SRandall Stewart 	/*********************************************/
4251dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
425220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4253dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4254dfb11ef8SRandall Stewart 	}
4255830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4256dd973b0eSMichael Tuexen 	if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
4257830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4258830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4259830d754dSRandall Stewart 
4260830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4261830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4262830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
426320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4264830d754dSRandall Stewart 			/*
4265493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4266830d754dSRandall Stewart 			 */
426720b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4268830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
42690c0982b8SRandall Stewart 			} else if (lchk) {
42700c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
427144fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
42720c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
42730c0982b8SRandall Stewart 				}
4274830d754dSRandall Stewart 			}
4275830d754dSRandall Stewart 		}
4276830d754dSRandall Stewart 		if (lchk) {
4277830d754dSRandall Stewart 			/* Assure a timer is up */
4278830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4279830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4280830d754dSRandall Stewart 		}
4281830d754dSRandall Stewart 	}
4282b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4283f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4284f8829a4aSRandall Stewart 		    rwnd,
4285f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4286f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4287f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
428880fefe0aSRandall Stewart 	}
4289f8829a4aSRandall Stewart }
4290f8829a4aSRandall Stewart 
4291f8829a4aSRandall Stewart void
4292cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
42937215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4294cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4295cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4296899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4297f8829a4aSRandall Stewart {
4298f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4299f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4300cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4301f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4302c105859eSRandall Stewart 	uint32_t send_s = 0;
4303f8829a4aSRandall Stewart 	long j;
4304f8829a4aSRandall Stewart 	int accum_moved = 0;
4305f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
43065e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
43075e54f665SRandall Stewart 	int win_probe_recovery = 0;
4308c105859eSRandall Stewart 	int win_probe_recovered = 0;
4309f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4310bff64a4dSRandall Stewart 	int done_once;
4311f79aab18SRandall Stewart 	int rto_ok = 1;
4312f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4313f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4314f8829a4aSRandall Stewart 
4315f8829a4aSRandall Stewart 	/*
4316f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4317f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4318f8829a4aSRandall Stewart 	 */
4319f8829a4aSRandall Stewart 	/*
4320f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4321f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4322f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4323f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4324f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4325f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4326f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4327f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4328f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4329f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4330f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4331f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4332f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4333f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4334f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4335f8829a4aSRandall Stewart 	 */
4336f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4337f8829a4aSRandall Stewart 	/* CMT DAC algo */
4338f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4339f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4340cd554309SMichael Tuexen 	last_tsn = cum_ack;
4341cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
434218e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
434318e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
434418e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
434518e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
434618e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
434718e198d3SRandall Stewart 	}
434818e198d3SRandall Stewart #endif
4349d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4350f8829a4aSRandall Stewart 
4351cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4352cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4353cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4354cd554309SMichael Tuexen 	}
43555e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4356b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4357c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4358c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4359c4739e2fSRandall Stewart 		    0,
4360c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4361c4739e2fSRandall Stewart 		    __LINE__);
4362c4739e2fSRandall Stewart 	}
4363f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4364f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4365b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4366f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4367f8829a4aSRandall Stewart 		    cum_ack,
4368f8829a4aSRandall Stewart 		    0,
4369f8829a4aSRandall Stewart 		    num_seg,
4370f8829a4aSRandall Stewart 		    num_dup,
4371f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
437280fefe0aSRandall Stewart 	}
4373ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4374cd554309SMichael Tuexen 		uint16_t i;
4375458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4376f8829a4aSRandall Stewart 
4377cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4378cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4379458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4380cd554309SMichael Tuexen 			if (dupdata == NULL) {
4381458303daSRandall Stewart 				break;
4382458303daSRandall Stewart 			}
4383cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4384f8829a4aSRandall Stewart 		}
4385f8829a4aSRandall Stewart 	}
4386c105859eSRandall Stewart 	/* reality check */
4387c105859eSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4388c105859eSRandall Stewart 		tp1 = TAILQ_LAST(&asoc->sent_queue,
4389c105859eSRandall Stewart 		    sctpchunk_listhead);
4390c105859eSRandall Stewart 		send_s = tp1->rec.data.TSN_seq + 1;
4391c105859eSRandall Stewart 	} else {
4392b5c16493SMichael Tuexen 		tp1 = NULL;
4393c105859eSRandall Stewart 		send_s = asoc->sending_seq;
4394c105859eSRandall Stewart 	}
439520b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(cum_ack, send_s)) {
4396ff1ffd74SMichael Tuexen 		struct mbuf *op_err;
4397ff1ffd74SMichael Tuexen 		char msg[SCTP_DIAG_INFO_LEN];
4398c105859eSRandall Stewart 
4399f8829a4aSRandall Stewart 		/*
4400fd60718dSMichael Tuexen 		 * no way, we have not even sent this TSN out yet. Peer is
4401fd60718dSMichael Tuexen 		 * hopelessly messed up with us.
4402f8829a4aSRandall Stewart 		 */
4403cd3fd531SMichael Tuexen 		SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4404b5c16493SMichael Tuexen 		    cum_ack, send_s);
4405b5c16493SMichael Tuexen 		if (tp1) {
4406cd3fd531SMichael Tuexen 			SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1: %p\n",
4407dd294dceSMichael Tuexen 			    tp1->rec.data.TSN_seq, (void *)tp1);
4408b5c16493SMichael Tuexen 		}
4409f8829a4aSRandall Stewart hopeless_peer:
4410f8829a4aSRandall Stewart 		*abort_now = 1;
4411f8829a4aSRandall Stewart 		/* XXX */
441255f8a4bbSMichael Tuexen 		snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x",
4413ff1ffd74SMichael Tuexen 		    cum_ack, send_s);
4414ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
441544249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4416ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4417f8829a4aSRandall Stewart 		return;
4418f8829a4aSRandall Stewart 	}
4419f8829a4aSRandall Stewart 	/**********************/
4420f8829a4aSRandall Stewart 	/* 1) check the range */
4421f8829a4aSRandall Stewart 	/**********************/
442220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4423f8829a4aSRandall Stewart 		/* acking something behind */
4424f8829a4aSRandall Stewart 		return;
4425f8829a4aSRandall Stewart 	}
4426f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4427f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4428f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4429cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4430f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4431b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4432f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4433f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
443480fefe0aSRandall Stewart 		}
4435f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4436f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4437f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4438f8829a4aSRandall Stewart 		}
4439f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4440f8829a4aSRandall Stewart 			/* SWS sender side engages */
4441f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4442f8829a4aSRandall Stewart 		}
4443f8829a4aSRandall Stewart 		/* stop any timers */
4444f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4445f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
444644249214SRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4447f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4448f8829a4aSRandall Stewart 			net->flight_size = 0;
4449f8829a4aSRandall Stewart 		}
4450f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4451f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4452f8829a4aSRandall Stewart 		return;
4453f8829a4aSRandall Stewart 	}
4454f8829a4aSRandall Stewart 	/*
4455f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4456f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4457f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4458f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4459f8829a4aSRandall Stewart 	 * destination address basis.
4460f8829a4aSRandall Stewart 	 */
4461f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4462a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4463a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4464a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4465a21779f0SRandall Stewart 		}
4466f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4467f8829a4aSRandall Stewart 		net->net_ack = 0;
4468f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4469f8829a4aSRandall Stewart 
4470f8829a4aSRandall Stewart 		/*
447142551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
447242551e99SRandall Stewart 		 * SACK processing
4473f8829a4aSRandall Stewart 		 */
4474f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4475f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4476299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4477299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4478299108c5SRandall Stewart 		}
4479f8829a4aSRandall Stewart 	}
4480f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
44814a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
448220b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4483f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4484f8829a4aSRandall Stewart 				accum_moved = 1;
4485f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4486f8829a4aSRandall Stewart 					/*
4487f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4488f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4489f8829a4aSRandall Stewart 					 * values may occur during marking
4490f8829a4aSRandall Stewart 					 */
4491f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4492f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4493f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4494f8829a4aSRandall Stewart 						/*
4495f8829a4aSRandall Stewart 						 * If there was no retran
4496f8829a4aSRandall Stewart 						 * and the address is
4497f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4498f8829a4aSRandall Stewart 						 * there and are now
4499f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4500f8829a4aSRandall Stewart 						 * mark it so.
4501f8829a4aSRandall Stewart 						 */
4502f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4503f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4504f8829a4aSRandall Stewart 					}
4505c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4506b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4507c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4508a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4509a5d547adSRandall Stewart 							    tp1->book_size,
45109a8e3088SMichael Tuexen 							    (uint32_t) (uintptr_t) tp1->whoTo,
4511a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
451280fefe0aSRandall Stewart 						}
4513c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4514c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4515299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4516299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4517299108c5SRandall Stewart 							    tp1);
4518299108c5SRandall Stewart 						}
4519f8829a4aSRandall Stewart 					}
4520f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4521f8829a4aSRandall Stewart 
4522f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4523f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4524f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4525f8829a4aSRandall Stewart 
4526f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4527f8829a4aSRandall Stewart 						/*
4528f8829a4aSRandall Stewart 						 * True non-retransmited
4529f8829a4aSRandall Stewart 						 * chunk
4530f8829a4aSRandall Stewart 						 */
4531f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4532f8829a4aSRandall Stewart 						    tp1->send_size;
4533f8829a4aSRandall Stewart 
4534f8829a4aSRandall Stewart 						/* update RTO too? */
4535f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4536f79aab18SRandall Stewart 							if (rto_ok) {
4537f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4538f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4539f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
454018e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4541899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4542f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4543f79aab18SRandall Stewart 								rto_ok = 0;
4544f79aab18SRandall Stewart 							}
4545f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4546f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4547f79aab18SRandall Stewart 							}
4548f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4549f8829a4aSRandall Stewart 						}
4550f8829a4aSRandall Stewart 					}
4551f8829a4aSRandall Stewart 					/*
4552f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4553f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4554f8829a4aSRandall Stewart 					 * acked for the first time, set the
4555f8829a4aSRandall Stewart 					 * following variables for the
4556f8829a4aSRandall Stewart 					 * corresp destination.
4557f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4558f8829a4aSRandall Stewart 					 * cwnd update.
4559f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4560f8829a4aSRandall Stewart 					 * trigger search for the next
4561f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4562f8829a4aSRandall Stewart 					 */
4563f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4564f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4565f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4566f8829a4aSRandall Stewart 
4567f8829a4aSRandall Stewart 
4568b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4569f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4570f8829a4aSRandall Stewart 						    cum_ack,
4571f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4572f8829a4aSRandall Stewart 						    0,
4573f8829a4aSRandall Stewart 						    0,
4574f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
457580fefe0aSRandall Stewart 					}
4576b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4577f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
457880fefe0aSRandall Stewart 					}
4579f8829a4aSRandall Stewart 				}
4580f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4581f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4582f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4583f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4584f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4585f8829a4aSRandall Stewart #endif
4586f8829a4aSRandall Stewart 				}
458742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
458842551e99SRandall Stewart 					/* deflate the cwnd */
458942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
459042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
459142551e99SRandall Stewart 				}
4592325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4593f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_ACKED;
4594f8829a4aSRandall Stewart 				}
4595325c8c46SMichael Tuexen 			}
4596f8829a4aSRandall Stewart 		} else {
4597f8829a4aSRandall Stewart 			break;
4598f8829a4aSRandall Stewart 		}
4599f8829a4aSRandall Stewart 	}
4600f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4601f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4602f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4603f8829a4aSRandall Stewart 
4604cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4605f8829a4aSRandall Stewart 
4606f8829a4aSRandall Stewart 		/*
4607f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4608f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4609f8829a4aSRandall Stewart 		 * for all dests.
4610f8829a4aSRandall Stewart 		 */
4611f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4612f8829a4aSRandall Stewart 			net->saw_newack = 0;
4613f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4614f8829a4aSRandall Stewart 		}
4615f8829a4aSRandall Stewart 
4616f8829a4aSRandall Stewart 		/*
4617f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4618f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4619f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4620f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4621f8829a4aSRandall Stewart 		 */
4622cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4623cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
46247215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4625cd554309SMichael Tuexen 			wake_him++;
4626cd554309SMichael Tuexen 		}
4627f8829a4aSRandall Stewart 		/*
4628fd60718dSMichael Tuexen 		 * validate the biggest_tsn_acked in the gap acks if strict
4629fd60718dSMichael Tuexen 		 * adherence is wanted.
4630f8829a4aSRandall Stewart 		 */
463120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4632f8829a4aSRandall Stewart 			/*
4633fd60718dSMichael Tuexen 			 * peer is either confused or we are under attack.
4634fd60718dSMichael Tuexen 			 * We must abort.
4635f8829a4aSRandall Stewart 			 */
4636cd3fd531SMichael Tuexen 			SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4637cd3fd531SMichael Tuexen 			    biggest_tsn_acked, send_s);
4638f8829a4aSRandall Stewart 			goto hopeless_peer;
4639f8829a4aSRandall Stewart 		}
4640f8829a4aSRandall Stewart 	}
4641f8829a4aSRandall Stewart 	/*******************************************/
4642f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4643f8829a4aSRandall Stewart 	/*******************************************/
46447c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4645f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4646f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4647f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4648a5d547adSRandall Stewart 				    stcb, net,
464944249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4650f8829a4aSRandall Stewart 
4651f8829a4aSRandall Stewart 		}
4652f8829a4aSRandall Stewart 	} else {
4653f8829a4aSRandall Stewart 		if (accum_moved) {
4654f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4655f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
465644249214SRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4657f8829a4aSRandall Stewart 			}
4658f8829a4aSRandall Stewart 		}
4659f8829a4aSRandall Stewart 	}
4660f8829a4aSRandall Stewart 	/********************************************/
4661d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4662f8829a4aSRandall Stewart 	/********************************************/
4663f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4664f8829a4aSRandall Stewart 
46657c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
466620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4667f8829a4aSRandall Stewart 			break;
4668f8829a4aSRandall Stewart 		}
4669325c8c46SMichael Tuexen 		if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4670a7ad6026SMichael Tuexen 			if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
4671a7ad6026SMichael Tuexen 				asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
4672a7ad6026SMichael Tuexen #ifdef INVARIANTS
4673a7ad6026SMichael Tuexen 			} else {
4674a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
4675a7ad6026SMichael Tuexen #endif
4676a7ad6026SMichael Tuexen 			}
4677f8829a4aSRandall Stewart 		}
4678d96bef9cSMichael Tuexen 		if ((asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues == 0) &&
4679d96bef9cSMichael Tuexen 		    (asoc->strmout[tp1->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
4680d96bef9cSMichael Tuexen 		    TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.stream_number].outqueue)) {
4681d96bef9cSMichael Tuexen 			asoc->trigger_reset = 1;
4682d96bef9cSMichael Tuexen 		}
4683f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
46840ddb4299SMichael Tuexen 		if (PR_SCTP_ENABLED(tp1->flags)) {
4685f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4686f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4687f8829a4aSRandall Stewart 		}
46887c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4689f8829a4aSRandall Stewart 		if (tp1->data) {
469004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4691f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4692f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
46937c99d56fSMichael Tuexen 			tp1->data = NULL;
4694dd973b0eSMichael Tuexen 			if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4695f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4696f8829a4aSRandall Stewart 			}
4697f8829a4aSRandall Stewart 		}
4698b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4699f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4700f8829a4aSRandall Stewart 			    cum_ack,
4701f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4702f8829a4aSRandall Stewart 			    0,
4703f8829a4aSRandall Stewart 			    0,
4704f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
470580fefe0aSRandall Stewart 		}
4706689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4707f8829a4aSRandall Stewart 		wake_him++;
47087c99d56fSMichael Tuexen 	}
47097c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
47107c99d56fSMichael Tuexen #ifdef INVARIANTS
4711cd0a4ff6SPedro F. Giffuni 		panic("Warning flight size is positive and should be 0");
47127c99d56fSMichael Tuexen #else
47137c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
47147c99d56fSMichael Tuexen 		    asoc->total_flight);
47157c99d56fSMichael Tuexen #endif
47167c99d56fSMichael Tuexen 		asoc->total_flight = 0;
47177c99d56fSMichael Tuexen 	}
471804ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4719f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4720ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4721ceaad40aSRandall Stewart 		struct socket *so;
4722ceaad40aSRandall Stewart 
4723ceaad40aSRandall Stewart #endif
4724f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4725b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47267215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
472780fefe0aSRandall Stewart 		}
4728ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4729ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4730ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4731ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4732ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4733ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4734ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4735ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4736ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4737ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4738ceaad40aSRandall Stewart 			return;
4739ceaad40aSRandall Stewart 		}
4740ceaad40aSRandall Stewart #endif
4741f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4742ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4743ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4744ceaad40aSRandall Stewart #endif
4745f8829a4aSRandall Stewart 	} else {
4746b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47477215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
474880fefe0aSRandall Stewart 		}
4749f8829a4aSRandall Stewart 	}
4750f8829a4aSRandall Stewart 
475142551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
475220b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4753f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4754f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4755f8829a4aSRandall Stewart 		}
4756f8829a4aSRandall Stewart 	}
4757f8829a4aSRandall Stewart 	/*
4758f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4759f8829a4aSRandall Stewart 	 *
4760f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4761f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4762f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4763f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4764f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4765f8829a4aSRandall Stewart 	 */
4766f8829a4aSRandall Stewart 
4767d9c5cfeaSMichael Tuexen 	if (num_seg) {
4768c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4769d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4770d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4771f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4772f8829a4aSRandall Stewart 
4773f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4774f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4775b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4776f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4777b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4778c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4779c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4780c105859eSRandall Stewart 					    tp1->book_size,
47819a8e3088SMichael Tuexen 					    (uint32_t) (uintptr_t) tp1->whoTo,
4782c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
478380fefe0aSRandall Stewart 				}
4784c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4785c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4786a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
478742551e99SRandall Stewart 				/*
478842551e99SRandall Stewart 				 * To ensure that this increase in
47894a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
47904a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
47914a9ef3f8SMichael Tuexen 				 * cwnd artificially.
479242551e99SRandall Stewart 				 */
479342551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4794f8829a4aSRandall Stewart 				cnt_revoked++;
4795f8829a4aSRandall Stewart 			}
4796f8829a4aSRandall Stewart 		}
4797f8829a4aSRandall Stewart 		if (cnt_revoked) {
4798f8829a4aSRandall Stewart 			reneged_all = 1;
4799f8829a4aSRandall Stewart 		}
4800f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4801f8829a4aSRandall Stewart 	}
4802d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4803d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4804f8829a4aSRandall Stewart 	else
4805d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4806f8829a4aSRandall Stewart 
4807b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4808ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4809ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4810ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4811ca85e948SMichael Tuexen 				/*
4812ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4813ca85e948SMichael Tuexen 				 * count, this is optional.
4814ca85e948SMichael Tuexen 				 */
4815ca85e948SMichael Tuexen 				net->error_count = 0;
4816ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4817ca85e948SMichael Tuexen 					/* addr came good */
4818ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4819ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
48204b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4821ca85e948SMichael Tuexen 				}
4822ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4823ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4824ca85e948SMichael Tuexen 						/*
4825ca85e948SMichael Tuexen 						 * release the alternate,
4826ca85e948SMichael Tuexen 						 * primary is good
4827ca85e948SMichael Tuexen 						 */
4828ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4829ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4830ca85e948SMichael Tuexen 					}
4831ca85e948SMichael Tuexen 				}
4832ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4833ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4834b7d130beSMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4835b7d130beSMichael Tuexen 					    stcb->sctp_ep, stcb, net,
483644249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
4837ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4838ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4839ca85e948SMichael Tuexen 					/* Done with this net */
4840ca85e948SMichael Tuexen 					net->net_ack = 0;
4841ca85e948SMichael Tuexen 				}
4842ca85e948SMichael Tuexen 				/* restore any doubled timers */
4843ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4844ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4845ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4846ca85e948SMichael Tuexen 				}
4847ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4848ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4849ca85e948SMichael Tuexen 				}
4850ca85e948SMichael Tuexen 			}
4851ca85e948SMichael Tuexen 		}
4852b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4853ca85e948SMichael Tuexen 	}
4854f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4855f8829a4aSRandall Stewart 		/* nothing left in-flight */
4856f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4857f8829a4aSRandall Stewart 			/* stop all timers */
4858f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4859b7d130beSMichael Tuexen 			    stcb, net,
486044249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4861f8829a4aSRandall Stewart 			net->flight_size = 0;
4862f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4863f8829a4aSRandall Stewart 		}
4864f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4865f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4866f8829a4aSRandall Stewart 	}
4867f8829a4aSRandall Stewart 	/**********************************/
4868f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4869f8829a4aSRandall Stewart 	/**********************************/
4870f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4871f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4872b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4873f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4874f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
487580fefe0aSRandall Stewart 		}
4876f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4877f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4878f8829a4aSRandall Stewart 			/* SWS sender side engages */
4879f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4880f8829a4aSRandall Stewart 		}
4881f8829a4aSRandall Stewart 		/* clean up */
4882f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4883f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4884f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4885*d1ea5fa9SMichael Tuexen 		    ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc))) {
48862afb3e84SRandall Stewart 			asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4887f8829a4aSRandall Stewart 		}
4888f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4889f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4890f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4891f8829a4aSRandall Stewart 				/* Need to abort here */
4892ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4893f8829a4aSRandall Stewart 
4894f8829a4aSRandall Stewart 		abort_out_now:
4895f8829a4aSRandall Stewart 				*abort_now = 1;
4896f8829a4aSRandall Stewart 				/* XXX */
4897ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
489844249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4899ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4900f8829a4aSRandall Stewart 				return;
4901f8829a4aSRandall Stewart 			} else {
4902ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4903ca85e948SMichael Tuexen 
4904f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4905f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4906f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4907f42a358aSRandall Stewart 				}
4908c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4909b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4910f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4911c39cfa1fSMichael Tuexen 				if (asoc->alternate) {
4912c39cfa1fSMichael Tuexen 					netp = asoc->alternate;
4913c39cfa1fSMichael Tuexen 				} else {
4914c39cfa1fSMichael Tuexen 					netp = asoc->primary_destination;
4915c39cfa1fSMichael Tuexen 				}
4916ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4917f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4918ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4919f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4920ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4921f8829a4aSRandall Stewart 			}
4922f8829a4aSRandall Stewart 			return;
4923f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4924f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4925ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4926ca85e948SMichael Tuexen 
4927f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4928f8829a4aSRandall Stewart 				goto abort_out_now;
4929f8829a4aSRandall Stewart 			}
4930f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4931c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4932b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
493312af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4934c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4935c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4936c39cfa1fSMichael Tuexen 			} else {
4937c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4938c39cfa1fSMichael Tuexen 			}
4939c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4940f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4941ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4942f8829a4aSRandall Stewart 			return;
4943f8829a4aSRandall Stewart 		}
4944f8829a4aSRandall Stewart 	}
4945f8829a4aSRandall Stewart 	/*
4946f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4947f8829a4aSRandall Stewart 	 * HEADS UP.
4948f8829a4aSRandall Stewart 	 */
4949f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4950f8829a4aSRandall Stewart 		net->net_ack = 0;
4951f8829a4aSRandall Stewart 	}
4952f8829a4aSRandall Stewart 
4953f8829a4aSRandall Stewart 	/*
4954f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4955f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4956f8829a4aSRandall Stewart 	 * automatically ensure that.
4957f8829a4aSRandall Stewart 	 */
49587c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
495920083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
496020083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
4961f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
4962f8829a4aSRandall Stewart 	}
4963cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4964f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4965f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4966f8829a4aSRandall Stewart 	}
4967b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4968b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4969f8829a4aSRandall Stewart 
4970f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
4971f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
4972f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
4973f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
4974f8829a4aSRandall Stewart 	}
4975f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
497620b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4977f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
4978f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
4979f8829a4aSRandall Stewart 	}
498042551e99SRandall Stewart 	/*
498142551e99SRandall Stewart 	 * CMT Fast recovery
498242551e99SRandall Stewart 	 */
4983f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4984f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
4985f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
4986f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
4987f8829a4aSRandall Stewart 		}
4988f8829a4aSRandall Stewart 	}
4989f8829a4aSRandall Stewart 
4990f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
4991b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4992f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
499344fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
499480fefe0aSRandall Stewart 	}
4995f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
499644fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4997f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4998f8829a4aSRandall Stewart 		/* SWS sender side engages */
4999f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5000f8829a4aSRandall Stewart 	}
50015e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
50025e54f665SRandall Stewart 		win_probe_recovery = 1;
50035e54f665SRandall Stewart 	}
5004f8829a4aSRandall Stewart 	/*
5005f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5006f8829a4aSRandall Stewart 	 * outstanding data.
5007f8829a4aSRandall Stewart 	 */
5008bff64a4dSRandall Stewart 	done_once = 0;
5009a5d547adSRandall Stewart again:
5010a5d547adSRandall Stewart 	j = 0;
5011f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
50125e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5013c105859eSRandall Stewart 			win_probe_recovered = 1;
50145e54f665SRandall Stewart 			/*-
50155e54f665SRandall Stewart 			 * Find first chunk that was used with
50165e54f665SRandall Stewart 			 * window probe and clear the event. Put
50175e54f665SRandall Stewart 			 * it back into the send queue as if has
50185e54f665SRandall Stewart 			 * not been sent.
50195e54f665SRandall Stewart 			 */
50205e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
50215e54f665SRandall Stewart 				if (tp1->window_probe) {
50227215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
50235e54f665SRandall Stewart 					break;
50245e54f665SRandall Stewart 				}
50255e54f665SRandall Stewart 			}
50265e54f665SRandall Stewart 		}
5027f8829a4aSRandall Stewart 		if (net->flight_size) {
5028a5d547adSRandall Stewart 			j++;
5029cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5030f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5031f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5032cd554309SMichael Tuexen 			}
50335171328bSRandall Stewart 			if (net->window_probe) {
5034cd554309SMichael Tuexen 				net->window_probe = 0;
50355171328bSRandall Stewart 			}
5036c105859eSRandall Stewart 		} else {
50375171328bSRandall Stewart 			if (net->window_probe) {
50385171328bSRandall Stewart 				/*
50395171328bSRandall Stewart 				 * In window probes we must assure a timer
50405171328bSRandall Stewart 				 * is still running there
50415171328bSRandall Stewart 				 */
50425171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
50435171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
50445171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
50455171328bSRandall Stewart 
50465171328bSRandall Stewart 				}
50475171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5048c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5049c105859eSRandall Stewart 				    stcb, net,
505044249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
5051c105859eSRandall Stewart 			}
5052f8829a4aSRandall Stewart 		}
5053f8829a4aSRandall Stewart 	}
5054bff64a4dSRandall Stewart 	if ((j == 0) &&
5055bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5056bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5057c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5058bff64a4dSRandall Stewart 	    (done_once == 0)) {
50590c0982b8SRandall Stewart 		/*
50600c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
50610c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
50620c0982b8SRandall Stewart 		 */
50630c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5064a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5065a5d547adSRandall Stewart 				net->flight_size = 0;
5066a5d547adSRandall Stewart 			}
5067a5d547adSRandall Stewart 			asoc->total_flight = 0;
5068a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5069a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5070a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5071a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5072c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5073c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5074a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5075791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5076a5d547adSRandall Stewart 				}
5077a5d547adSRandall Stewart 			}
50780c0982b8SRandall Stewart 		}
5079bff64a4dSRandall Stewart 		done_once = 1;
5080a5d547adSRandall Stewart 		goto again;
5081a5d547adSRandall Stewart 	}
5082cd554309SMichael Tuexen 	/*********************************************/
5083cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5084cd554309SMichael Tuexen 	/* (section 4.2)                             */
5085cd554309SMichael Tuexen 	/*********************************************/
5086cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
508720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5088dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5089dfb11ef8SRandall Stewart 	}
5090830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5091dd973b0eSMichael Tuexen 	if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
5092830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5093830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5094830d754dSRandall Stewart 
5095830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5096830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5097830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
509820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5099830d754dSRandall Stewart 			/*
5100493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
5101830d754dSRandall Stewart 			 */
51020c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
51030c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
51040c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
51050c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
51060c0982b8SRandall Stewart 			}
510720b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5108830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
51090c0982b8SRandall Stewart 			} else if (lchk) {
51100c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
511144fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
51120c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
51130c0982b8SRandall Stewart 				}
5114830d754dSRandall Stewart 			}
5115830d754dSRandall Stewart 		}
5116830d754dSRandall Stewart 		if (lchk) {
5117830d754dSRandall Stewart 			/* Assure a timer is up */
5118830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5119830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5120830d754dSRandall Stewart 		}
5121830d754dSRandall Stewart 	}
5122b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5123f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5124f8829a4aSRandall Stewart 		    a_rwnd,
5125f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5126f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5127f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
512880fefe0aSRandall Stewart 	}
5129f8829a4aSRandall Stewart }
5130f8829a4aSRandall Stewart 
5131f8829a4aSRandall Stewart void
51327215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5133f8829a4aSRandall Stewart {
5134f8829a4aSRandall Stewart 	/* Copy cum-ack */
5135f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5136f8829a4aSRandall Stewart 
5137f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5138f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5139f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5140f8829a4aSRandall Stewart 
5141f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5142899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5143f8829a4aSRandall Stewart }
5144f8829a4aSRandall Stewart 
5145f8829a4aSRandall Stewart static void
5146f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5147f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5148f8829a4aSRandall Stewart {
5149f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5150f8829a4aSRandall Stewart 	struct sctp_association *asoc;
515144249214SRandall Stewart 	uint32_t tt;
515244249214SRandall Stewart 	int need_reasm_check = 0, old;
5153f8829a4aSRandall Stewart 
5154f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5155f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
515644249214SRandall Stewart 	if (asoc->idata_supported) {
515744249214SRandall Stewart 		old = 0;
515844249214SRandall Stewart 	} else {
515944249214SRandall Stewart 		old = 1;
516044249214SRandall Stewart 	}
5161f8829a4aSRandall Stewart 	/*
5162f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
516344249214SRandall Stewart 	 * came in.
5164f8829a4aSRandall Stewart 	 */
516544249214SRandall Stewart 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next_instrm, nctl) {
516644249214SRandall Stewart 		if (SCTP_MSGID_GE(old, tt, ctl->sinfo_ssn)) {
5167f8829a4aSRandall Stewart 			/* this is deliverable now */
516844249214SRandall Stewart 			if (((ctl->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
516944249214SRandall Stewart 				if (ctl->on_strm_q) {
517044249214SRandall Stewart 					if (ctl->on_strm_q == SCTP_ON_ORDERED) {
517144249214SRandall Stewart 						TAILQ_REMOVE(&strmin->inqueue, ctl, next_instrm);
517244249214SRandall Stewart 					} else if (ctl->on_strm_q == SCTP_ON_UNORDERED) {
517344249214SRandall Stewart 						TAILQ_REMOVE(&strmin->uno_inqueue, ctl, next_instrm);
517498d5fd97SMichael Tuexen #ifdef INVARIANTS
517544249214SRandall Stewart 					} else {
517644249214SRandall Stewart 						panic("strmin: %p ctl: %p unknown %d",
517744249214SRandall Stewart 						    strmin, ctl, ctl->on_strm_q);
517898d5fd97SMichael Tuexen #endif
517944249214SRandall Stewart 					}
518044249214SRandall Stewart 					ctl->on_strm_q = 0;
518144249214SRandall Stewart 				}
5182f8829a4aSRandall Stewart 				/* subtract pending on streams */
5183f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= ctl->length;
5184f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
5185f8829a4aSRandall Stewart 				/* deliver it to at least the delivery-q */
5186f8829a4aSRandall Stewart 				if (stcb->sctp_socket) {
5187b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5188f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
5189f8829a4aSRandall Stewart 					    ctl,
519044249214SRandall Stewart 					    &stcb->sctp_socket->so_rcv,
519144249214SRandall Stewart 					    1, SCTP_READ_LOCK_HELD,
519244249214SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
519344249214SRandall Stewart 				}
519444249214SRandall Stewart 			} else {
519544249214SRandall Stewart 				/* Its a fragmented message */
519644249214SRandall Stewart 				if (ctl->first_frag_seen) {
519744249214SRandall Stewart 					/*
519844249214SRandall Stewart 					 * Make it so this is next to
519944249214SRandall Stewart 					 * deliver, we restore later
520044249214SRandall Stewart 					 */
520144249214SRandall Stewart 					strmin->last_sequence_delivered = ctl->sinfo_ssn - 1;
520244249214SRandall Stewart 					need_reasm_check = 1;
520344249214SRandall Stewart 					break;
520444249214SRandall Stewart 				}
5205f8829a4aSRandall Stewart 			}
5206f8829a4aSRandall Stewart 		} else {
5207f8829a4aSRandall Stewart 			/* no more delivery now. */
5208f8829a4aSRandall Stewart 			break;
5209f8829a4aSRandall Stewart 		}
5210f8829a4aSRandall Stewart 	}
521144249214SRandall Stewart 	if (need_reasm_check) {
521244249214SRandall Stewart 		int ret;
521344249214SRandall Stewart 
5214*d1ea5fa9SMichael Tuexen 		ret = sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin, SCTP_READ_LOCK_HELD);
521544249214SRandall Stewart 		if (SCTP_MSGID_GT(old, tt, strmin->last_sequence_delivered)) {
521644249214SRandall Stewart 			/* Restore the next to deliver unless we are ahead */
521744249214SRandall Stewart 			strmin->last_sequence_delivered = tt;
521844249214SRandall Stewart 		}
521944249214SRandall Stewart 		if (ret == 0) {
522044249214SRandall Stewart 			/* Left the front Partial one on */
522144249214SRandall Stewart 			return;
522244249214SRandall Stewart 		}
522344249214SRandall Stewart 		need_reasm_check = 0;
522444249214SRandall Stewart 	}
5225f8829a4aSRandall Stewart 	/*
5226f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5227f8829a4aSRandall Stewart 	 * now ready.
5228f8829a4aSRandall Stewart 	 */
5229f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
523044249214SRandall Stewart 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next_instrm, nctl) {
5231f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
523244249214SRandall Stewart 			if (((ctl->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
5233f8829a4aSRandall Stewart 				/* this is deliverable now */
523444249214SRandall Stewart 				if (ctl->on_strm_q) {
523544249214SRandall Stewart 					if (ctl->on_strm_q == SCTP_ON_ORDERED) {
523644249214SRandall Stewart 						TAILQ_REMOVE(&strmin->inqueue, ctl, next_instrm);
523744249214SRandall Stewart 					} else if (ctl->on_strm_q == SCTP_ON_UNORDERED) {
523844249214SRandall Stewart 						TAILQ_REMOVE(&strmin->uno_inqueue, ctl, next_instrm);
523998d5fd97SMichael Tuexen #ifdef INVARIANTS
524044249214SRandall Stewart 					} else {
524144249214SRandall Stewart 						panic("strmin: %p ctl: %p unknown %d",
524244249214SRandall Stewart 						    strmin, ctl, ctl->on_strm_q);
524398d5fd97SMichael Tuexen #endif
524444249214SRandall Stewart 					}
524544249214SRandall Stewart 					ctl->on_strm_q = 0;
524644249214SRandall Stewart 				}
5247f8829a4aSRandall Stewart 				/* subtract pending on streams */
5248f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= ctl->length;
5249f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
5250f8829a4aSRandall Stewart 				/* deliver it to at least the delivery-q */
5251f8829a4aSRandall Stewart 				strmin->last_sequence_delivered = ctl->sinfo_ssn;
5252f8829a4aSRandall Stewart 				if (stcb->sctp_socket) {
5253b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5254f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
5255f8829a4aSRandall Stewart 					    ctl,
525644249214SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
525744249214SRandall Stewart 					    SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
525877acdc25SRandall Stewart 
5259f8829a4aSRandall Stewart 				}
5260f8829a4aSRandall Stewart 				tt = strmin->last_sequence_delivered + 1;
5261f8829a4aSRandall Stewart 			} else {
526244249214SRandall Stewart 				/* Its a fragmented message */
526344249214SRandall Stewart 				if (ctl->first_frag_seen) {
526444249214SRandall Stewart 					/*
526544249214SRandall Stewart 					 * Make it so this is next to
526644249214SRandall Stewart 					 * deliver
526744249214SRandall Stewart 					 */
526844249214SRandall Stewart 					strmin->last_sequence_delivered = ctl->sinfo_ssn - 1;
526944249214SRandall Stewart 					need_reasm_check = 1;
5270f8829a4aSRandall Stewart 					break;
5271f8829a4aSRandall Stewart 				}
5272f8829a4aSRandall Stewart 			}
527344249214SRandall Stewart 		} else {
527444249214SRandall Stewart 			break;
527544249214SRandall Stewart 		}
527644249214SRandall Stewart 	}
527744249214SRandall Stewart 	if (need_reasm_check) {
5278*d1ea5fa9SMichael Tuexen 		(void)sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin, SCTP_READ_LOCK_HELD);
527944249214SRandall Stewart 	}
5280f8829a4aSRandall Stewart }
5281f8829a4aSRandall Stewart 
52828e1b295fSMichael Tuexen 
5283*d1ea5fa9SMichael Tuexen 
52848933fa13SRandall Stewart static void
52858933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
52868933fa13SRandall Stewart     struct sctp_association *asoc,
5287*d1ea5fa9SMichael Tuexen     uint16_t stream, uint32_t seq, int ordered, int old, uint32_t cumtsn)
52888933fa13SRandall Stewart {
528944249214SRandall Stewart 	struct sctp_queued_to_read *control;
529044249214SRandall Stewart 	struct sctp_stream_in *strm;
52914a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
5292*d1ea5fa9SMichael Tuexen 	int cnt_removed = 0;
52938933fa13SRandall Stewart 
52948933fa13SRandall Stewart 	/*
529544249214SRandall Stewart 	 * For now large messages held on the stream reasm that are complete
52964a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
52974a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
52984a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
52994a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
53004a9ef3f8SMichael Tuexen 	 * queue.
53018933fa13SRandall Stewart 	 */
530244249214SRandall Stewart 	strm = &asoc->strmin[stream];
5303*d1ea5fa9SMichael Tuexen 	control = sctp_find_reasm_entry(strm, (uint32_t) seq, ordered, old);
530444249214SRandall Stewart 	if (control == NULL) {
530544249214SRandall Stewart 		/* Not found */
530644249214SRandall Stewart 		return;
53078933fa13SRandall Stewart 	}
530844249214SRandall Stewart 	TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
530944249214SRandall Stewart 		/* Purge hanging chunks */
5310*d1ea5fa9SMichael Tuexen 		if (old && (ordered == 0)) {
5311*d1ea5fa9SMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumtsn)) {
5312*d1ea5fa9SMichael Tuexen 				break;
5313*d1ea5fa9SMichael Tuexen 			}
5314*d1ea5fa9SMichael Tuexen 		}
5315*d1ea5fa9SMichael Tuexen 		cnt_removed++;
531644249214SRandall Stewart 		TAILQ_REMOVE(&control->reasm, chk, sctp_next);
53178933fa13SRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
53188933fa13SRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
53198933fa13SRandall Stewart 		if (chk->data) {
53208933fa13SRandall Stewart 			sctp_m_freem(chk->data);
53218933fa13SRandall Stewart 			chk->data = NULL;
53228933fa13SRandall Stewart 		}
5323689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
53248933fa13SRandall Stewart 	}
5325*d1ea5fa9SMichael Tuexen 	if (!TAILQ_EMPTY(&control->reasm)) {
5326*d1ea5fa9SMichael Tuexen 		/* This has to be old data, unordered */
5327*d1ea5fa9SMichael Tuexen 		if (control->data) {
5328*d1ea5fa9SMichael Tuexen 			sctp_m_freem(control->data);
5329*d1ea5fa9SMichael Tuexen 			control->data = NULL;
5330*d1ea5fa9SMichael Tuexen 		}
5331*d1ea5fa9SMichael Tuexen 		sctp_reset_a_control(control, stcb->sctp_ep, cumtsn);
5332*d1ea5fa9SMichael Tuexen 		chk = TAILQ_FIRST(&control->reasm);
5333*d1ea5fa9SMichael Tuexen 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
5334*d1ea5fa9SMichael Tuexen 			TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5335*d1ea5fa9SMichael Tuexen 			sctp_add_chk_to_control(control, strm, stcb, asoc,
5336*d1ea5fa9SMichael Tuexen 			    chk, SCTP_READ_LOCK_HELD);
5337*d1ea5fa9SMichael Tuexen 		}
5338*d1ea5fa9SMichael Tuexen 		sctp_deliver_reasm_check(stcb, asoc, strm, SCTP_READ_LOCK_HELD);
5339*d1ea5fa9SMichael Tuexen 		return;
5340*d1ea5fa9SMichael Tuexen 	}
5341*d1ea5fa9SMichael Tuexen 	if (control->on_strm_q == SCTP_ON_ORDERED) {
534244249214SRandall Stewart 		TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
5343*d1ea5fa9SMichael Tuexen 		control->on_strm_q = 0;
5344*d1ea5fa9SMichael Tuexen 	} else if (control->on_strm_q == SCTP_ON_UNORDERED) {
5345*d1ea5fa9SMichael Tuexen 		TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
5346*d1ea5fa9SMichael Tuexen 		control->on_strm_q = 0;
5347*d1ea5fa9SMichael Tuexen #ifdef INVARIANTS
5348*d1ea5fa9SMichael Tuexen 	} else if (control->on_strm_q) {
5349*d1ea5fa9SMichael Tuexen 		panic("strm: %p ctl: %p unknown %d",
5350*d1ea5fa9SMichael Tuexen 		    strm, control, control->on_strm_q);
5351*d1ea5fa9SMichael Tuexen #endif
5352*d1ea5fa9SMichael Tuexen 	}
5353*d1ea5fa9SMichael Tuexen 	control->on_strm_q = 0;
535444249214SRandall Stewart 	if (control->on_read_q == 0) {
535544249214SRandall Stewart 		sctp_free_remote_addr(control->whoFrom);
535644249214SRandall Stewart 		if (control->data) {
535744249214SRandall Stewart 			sctp_m_freem(control->data);
535844249214SRandall Stewart 			control->data = NULL;
535944249214SRandall Stewart 		}
536044249214SRandall Stewart 		sctp_free_a_readq(stcb, control);
53618933fa13SRandall Stewart 	}
53628933fa13SRandall Stewart }
53638933fa13SRandall Stewart 
5364f8829a4aSRandall Stewart void
5365f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5366b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5367b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5368f8829a4aSRandall Stewart {
5369f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5370f8829a4aSRandall Stewart 	/*
5371f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5372f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5373f8829a4aSRandall Stewart 	 *
5374f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5375f8829a4aSRandall Stewart 	 *
5376f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5377f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5378f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5379f8829a4aSRandall Stewart 	 * report where we are.
5380f8829a4aSRandall Stewart 	 */
5381f8829a4aSRandall Stewart 	struct sctp_association *asoc;
53827898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
53837215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
53848933fa13SRandall Stewart 	uint32_t str_seq;
5385f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
53868933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5387f8829a4aSRandall Stewart 
5388f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5389f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5390ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5391ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5392f8829a4aSRandall Stewart 		return;
5393f8829a4aSRandall Stewart 	}
5394f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5395f8829a4aSRandall Stewart 	/*************************************************************/
5396f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5397f8829a4aSRandall Stewart 	/*************************************************************/
5398f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5399f8829a4aSRandall Stewart 
540020b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5401f8829a4aSRandall Stewart 		/* Already got there ... */
5402f8829a4aSRandall Stewart 		return;
5403f8829a4aSRandall Stewart 	}
5404f8829a4aSRandall Stewart 	/*
5405f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5406f8829a4aSRandall Stewart 	 * gap
5407f8829a4aSRandall Stewart 	 */
5408b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
540977acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
54102afb3e84SRandall Stewart 	if (gap >= m_size) {
5411f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5412ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
5413ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
541417205eccSRandall Stewart 
5415f8829a4aSRandall Stewart 			/*
5416f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
541717205eccSRandall Stewart 			 * give out). This must be an attacker.
5418f8829a4aSRandall Stewart 			 */
541917205eccSRandall Stewart 			*abort_flag = 1;
5420ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg),
5421ff1ffd74SMichael Tuexen 			    "New cum ack %8.8x too high, highest TSN %8.8x",
5422ff1ffd74SMichael Tuexen 			    new_cum_tsn, asoc->highest_tsn_inside_map);
5423ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
542444249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5425ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5426f8829a4aSRandall Stewart 			return;
5427f8829a4aSRandall Stewart 		}
5428207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
542977acdc25SRandall Stewart 
54302afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
54312afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
543277acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
543377acdc25SRandall Stewart 
5434b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5435830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
543677acdc25SRandall Stewart 
5437b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5438f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
543980fefe0aSRandall Stewart 		}
54402afb3e84SRandall Stewart 	} else {
54412afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
54422afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
54437898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
54447898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
54458933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
544620b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
54477898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5448b5c16493SMichael Tuexen 				}
5449b5c16493SMichael Tuexen 			}
5450b5c16493SMichael Tuexen 		}
5451f8829a4aSRandall Stewart 	}
5452f8829a4aSRandall Stewart 	/*************************************************************/
5453f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5454f8829a4aSRandall Stewart 	/*************************************************************/
5455f8829a4aSRandall Stewart 
545644249214SRandall Stewart 	/* This is now done as part of clearing up the stream/seq */
5457*d1ea5fa9SMichael Tuexen 	if (asoc->idata_supported == 0) {
5458*d1ea5fa9SMichael Tuexen 		uint16_t sid;
545944249214SRandall Stewart 
5460*d1ea5fa9SMichael Tuexen 		/* Flush all the un-ordered data based on cum-tsn */
5461*d1ea5fa9SMichael Tuexen 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5462*d1ea5fa9SMichael Tuexen 		for (sid = 0; sid < asoc->streamincnt; sid++) {
5463*d1ea5fa9SMichael Tuexen 			sctp_flush_reassm_for_str_seq(stcb, asoc, sid, 0, 0, 1, new_cum_tsn);
5464*d1ea5fa9SMichael Tuexen 		}
5465*d1ea5fa9SMichael Tuexen 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5466*d1ea5fa9SMichael Tuexen 	}
54678933fa13SRandall Stewart 	/*******************************************************/
54688933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
54698933fa13SRandall Stewart 	/* delivery issues as needed.                       */
54708933fa13SRandall Stewart 	/*******************************************************/
5471f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5472671d309cSRandall Stewart 	if (m && fwd_sz) {
5473f8829a4aSRandall Stewart 		/* New method. */
5474d61a0ae0SRandall Stewart 		unsigned int num_str;
547544249214SRandall Stewart 		uint32_t sequence;
547644249214SRandall Stewart 		uint16_t stream;
54778e1b295fSMichael Tuexen 		uint16_t ordered, flags;
547844249214SRandall Stewart 		int old;
5479671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
548044249214SRandall Stewart 		struct sctp_strseq_mid *stseq_m, strseqbuf_m;
5481671d309cSRandall Stewart 
5482671d309cSRandall Stewart 		offset += sizeof(*fwd);
5483f8829a4aSRandall Stewart 
54848933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
548544249214SRandall Stewart 		if (asoc->idata_supported) {
548644249214SRandall Stewart 			num_str = fwd_sz / sizeof(struct sctp_strseq_mid);
548744249214SRandall Stewart 			old = 0;
548844249214SRandall Stewart 		} else {
5489f8829a4aSRandall Stewart 			num_str = fwd_sz / sizeof(struct sctp_strseq);
549044249214SRandall Stewart 			old = 1;
549144249214SRandall Stewart 		}
5492f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
549344249214SRandall Stewart 			if (asoc->idata_supported) {
549444249214SRandall Stewart 				stseq_m = (struct sctp_strseq_mid *)sctp_m_getptr(m, offset,
549544249214SRandall Stewart 				    sizeof(struct sctp_strseq_mid),
549644249214SRandall Stewart 				    (uint8_t *) & strseqbuf_m);
549744249214SRandall Stewart 				offset += sizeof(struct sctp_strseq_mid);
549844249214SRandall Stewart 				if (stseq_m == NULL) {
549944249214SRandall Stewart 					break;
550044249214SRandall Stewart 				}
550144249214SRandall Stewart 				stream = ntohs(stseq_m->stream);
550244249214SRandall Stewart 				sequence = ntohl(stseq_m->msg_id);
55038e1b295fSMichael Tuexen 				flags = ntohs(stseq_m->flags);
55048e1b295fSMichael Tuexen 				if (flags & PR_SCTP_UNORDERED_FLAG) {
55058e1b295fSMichael Tuexen 					ordered = 0;
55068e1b295fSMichael Tuexen 				} else {
55078e1b295fSMichael Tuexen 					ordered = 1;
55088e1b295fSMichael Tuexen 				}
550944249214SRandall Stewart 			} else {
5510671d309cSRandall Stewart 				stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5511671d309cSRandall Stewart 				    sizeof(struct sctp_strseq),
5512671d309cSRandall Stewart 				    (uint8_t *) & strseqbuf);
5513671d309cSRandall Stewart 				offset += sizeof(struct sctp_strseq);
55142afb3e84SRandall Stewart 				if (stseq == NULL) {
5515671d309cSRandall Stewart 					break;
55162afb3e84SRandall Stewart 				}
551744249214SRandall Stewart 				stream = ntohs(stseq->stream);
551844249214SRandall Stewart 				sequence = (uint32_t) ntohs(stseq->sequence);
55198e1b295fSMichael Tuexen 				ordered = 1;
552044249214SRandall Stewart 			}
5521f8829a4aSRandall Stewart 			/* Convert */
55228933fa13SRandall Stewart 
5523f8829a4aSRandall Stewart 			/* now process */
55248933fa13SRandall Stewart 
55258933fa13SRandall Stewart 			/*
55268933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
55278933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
55288933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
55298933fa13SRandall Stewart 			 */
553044249214SRandall Stewart 			if (stream >= asoc->streamincnt) {
55312afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
55322afb3e84SRandall Stewart 				break;
5533f8829a4aSRandall Stewart 			}
553444249214SRandall Stewart 			if ((asoc->str_of_pdapi == stream) &&
553544249214SRandall Stewart 			    (asoc->ssn_of_pdapi == sequence)) {
55368933fa13SRandall Stewart 				/*
55378933fa13SRandall Stewart 				 * If this is the one we were partially
55388933fa13SRandall Stewart 				 * delivering now then we no longer are.
55398933fa13SRandall Stewart 				 * Note this will change with the reassembly
55408933fa13SRandall Stewart 				 * re-write.
55418933fa13SRandall Stewart 				 */
55428933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
55438933fa13SRandall Stewart 			}
554444249214SRandall Stewart 			strm = &asoc->strmin[stream];
5545*d1ea5fa9SMichael Tuexen 			if (asoc->idata_supported == 0) {
5546*d1ea5fa9SMichael Tuexen 				uint16_t strm_at;
5547*d1ea5fa9SMichael Tuexen 
5548*d1ea5fa9SMichael Tuexen 				for (strm_at = strm->last_sequence_delivered; SCTP_MSGID_GE(1, sequence, strm_at); strm_at++) {
5549*d1ea5fa9SMichael Tuexen 					sctp_flush_reassm_for_str_seq(stcb, asoc, stream, strm_at, ordered, old, new_cum_tsn);
5550*d1ea5fa9SMichael Tuexen 				}
5551*d1ea5fa9SMichael Tuexen 			} else {
5552*d1ea5fa9SMichael Tuexen 				uint32_t strm_at;
5553*d1ea5fa9SMichael Tuexen 
5554*d1ea5fa9SMichael Tuexen 				for (strm_at = strm->last_sequence_delivered; SCTP_MSGID_GE(0, sequence, strm_at); strm_at++) {
5555*d1ea5fa9SMichael Tuexen 					sctp_flush_reassm_for_str_seq(stcb, asoc, stream, strm_at, ordered, old, new_cum_tsn);
5556*d1ea5fa9SMichael Tuexen 				}
5557*d1ea5fa9SMichael Tuexen 			}
55588933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
555944249214SRandall Stewart 				if ((ctl->sinfo_stream == stream) &&
556044249214SRandall Stewart 				    (ctl->sinfo_ssn == sequence)) {
556144249214SRandall Stewart 					str_seq = (stream << 16) | (0x0000ffff & sequence);
55628933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
55638933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
556444249214SRandall Stewart 					ctl->end_added = 1;
556544249214SRandall Stewart 					if (ctl->on_strm_q == SCTP_ON_ORDERED) {
556644249214SRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next_instrm);
556744249214SRandall Stewart 					} else if (ctl->on_strm_q == SCTP_ON_UNORDERED) {
556844249214SRandall Stewart 						TAILQ_REMOVE(&strm->uno_inqueue, ctl, next_instrm);
556998d5fd97SMichael Tuexen #ifdef INVARIANTS
557044249214SRandall Stewart 					} else if (ctl->on_strm_q) {
557144249214SRandall Stewart 						panic("strm: %p ctl: %p unknown %d",
557244249214SRandall Stewart 						    strm, ctl, ctl->on_strm_q);
557398d5fd97SMichael Tuexen #endif
557444249214SRandall Stewart 					}
557544249214SRandall Stewart 					ctl->on_strm_q = 0;
55768933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5577810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5578810ec536SMichael Tuexen 					    stcb,
55798933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5580810ec536SMichael Tuexen 					    (void *)&str_seq,
5581810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
55828933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
55838933fa13SRandall Stewart 					break;
558444249214SRandall Stewart 				} else if ((ctl->sinfo_stream == stream) &&
558544249214SRandall Stewart 				    SCTP_MSGID_GT(old, ctl->sinfo_ssn, sequence)) {
55868933fa13SRandall Stewart 					/* We are past our victim SSN */
55878933fa13SRandall Stewart 					break;
55888933fa13SRandall Stewart 				}
55898933fa13SRandall Stewart 			}
559044249214SRandall Stewart 			if (SCTP_MSGID_GT(old, sequence, strm->last_sequence_delivered)) {
5591f8829a4aSRandall Stewart 				/* Update the sequence number */
559244249214SRandall Stewart 				strm->last_sequence_delivered = sequence;
5593f8829a4aSRandall Stewart 			}
5594f8829a4aSRandall Stewart 			/* now kick the stream the new way */
559504ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5596f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5597f8829a4aSRandall Stewart 		}
55988933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5599f8829a4aSRandall Stewart 	}
56007898f408SRandall Stewart 	/*
56017898f408SRandall Stewart 	 * Now slide thing forward.
56027898f408SRandall Stewart 	 */
56037898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
5604f8829a4aSRandall Stewart }
5605