xref: /freebsd/sys/netinet/sctp_indata.c (revision 8e1b295f09433385acabe355e24bba9bdd2bee20)
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,
6744249214SRandall Stewart     struct sctp_tmit_chunk *chk);
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
45144249214SRandall Stewart 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);
61544249214SRandall Stewart 			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 
74244249214SRandall Stewart static int
74344249214SRandall Stewart sctp_handle_old_data(struct sctp_tcb *stcb, struct sctp_association *asoc, struct sctp_stream_in *strm,
74444249214SRandall Stewart     struct sctp_queued_to_read *control, uint32_t pd_point)
74544249214SRandall Stewart {
74644249214SRandall Stewart 	/*
74744249214SRandall Stewart 	 * Special handling for the old un-ordered data chunk. All the
74844249214SRandall Stewart 	 * chunks/TSN's go to msg_id 0. So we have to do the old style
74944249214SRandall Stewart 	 * watching to see if we have it all. If you return one, no other
75044249214SRandall Stewart 	 * control entries on the un-ordered queue will be looked at. In
75144249214SRandall Stewart 	 * theory there should be no others entries in reality, unless the
75244249214SRandall Stewart 	 * guy is sending both unordered NDATA and unordered DATA...
75344249214SRandall Stewart 	 */
75444249214SRandall Stewart 	struct sctp_tmit_chunk *chk, *lchk, *tchk;
75544249214SRandall Stewart 	uint32_t fsn;
756643fd575SMichael Tuexen 	struct sctp_queued_to_read *nc;
75744249214SRandall Stewart 	int cnt_added;
75844249214SRandall Stewart 
75944249214SRandall Stewart 	if (control->first_frag_seen == 0) {
76044249214SRandall Stewart 		/* Nothing we can do, we have not seen the first piece yet */
76144249214SRandall Stewart 		return (1);
76244249214SRandall Stewart 	}
76344249214SRandall Stewart 	/* Collapse any we can */
76444249214SRandall Stewart 	cnt_added = 0;
76544249214SRandall Stewart restart:
76644249214SRandall Stewart 	fsn = control->fsn_included + 1;
76744249214SRandall Stewart 	/* Now what can we add? */
76844249214SRandall Stewart 	TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, lchk) {
76944249214SRandall Stewart 		if (chk->rec.data.fsn_num == fsn) {
77044249214SRandall Stewart 			/* Ok lets add it */
771643fd575SMichael Tuexen 			sctp_alloc_a_readq(stcb, nc);
772643fd575SMichael Tuexen 			if (nc == NULL) {
773643fd575SMichael Tuexen 				break;
774643fd575SMichael Tuexen 			}
775643fd575SMichael Tuexen 			memset(nc, 0, sizeof(struct sctp_queued_to_read));
77644249214SRandall Stewart 			TAILQ_REMOVE(&control->reasm, chk, sctp_next);
77744249214SRandall Stewart 			sctp_add_chk_to_control(control, strm, stcb, asoc, chk);
77844249214SRandall Stewart 			fsn++;
77944249214SRandall Stewart 			cnt_added++;
78044249214SRandall Stewart 			chk = NULL;
78144249214SRandall Stewart 			if (control->end_added) {
78244249214SRandall Stewart 				/* We are done */
78344249214SRandall Stewart 				if (!TAILQ_EMPTY(&control->reasm)) {
78444249214SRandall Stewart 					/*
78544249214SRandall Stewart 					 * Ok we have to move anything left
78644249214SRandall Stewart 					 * on the control queue to a new
78744249214SRandall Stewart 					 * control.
78844249214SRandall Stewart 					 */
78944249214SRandall Stewart 					sctp_build_readq_entry_from_ctl(nc, control);
79044249214SRandall Stewart 					tchk = TAILQ_FIRST(&control->reasm);
79144249214SRandall Stewart 					if (tchk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
79244249214SRandall Stewart 						TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
79344249214SRandall Stewart 						nc->first_frag_seen = 1;
79444249214SRandall Stewart 						nc->fsn_included = tchk->rec.data.fsn_num;
79544249214SRandall Stewart 						nc->data = tchk->data;
79644249214SRandall Stewart 						sctp_mark_non_revokable(asoc, tchk->rec.data.TSN_seq);
79744249214SRandall Stewart 						tchk->data = NULL;
79844249214SRandall Stewart 						sctp_free_a_chunk(stcb, tchk, SCTP_SO_NOT_LOCKED);
79944249214SRandall Stewart 						sctp_setup_tail_pointer(nc);
80044249214SRandall Stewart 						tchk = TAILQ_FIRST(&control->reasm);
80144249214SRandall Stewart 					}
80244249214SRandall Stewart 					/* Spin the rest onto the queue */
80344249214SRandall Stewart 					while (tchk) {
80444249214SRandall Stewart 						TAILQ_REMOVE(&control->reasm, tchk, sctp_next);
80544249214SRandall Stewart 						TAILQ_INSERT_TAIL(&nc->reasm, tchk, sctp_next);
80644249214SRandall Stewart 						tchk = TAILQ_FIRST(&control->reasm);
80744249214SRandall Stewart 					}
80844249214SRandall Stewart 					/*
80944249214SRandall Stewart 					 * Now lets add it to the queue
81044249214SRandall Stewart 					 * after removing control
81144249214SRandall Stewart 					 */
81244249214SRandall Stewart 					TAILQ_INSERT_TAIL(&strm->uno_inqueue, nc, next_instrm);
81344249214SRandall Stewart 					nc->on_strm_q = SCTP_ON_UNORDERED;
81444249214SRandall Stewart 					if (control->on_strm_q) {
81544249214SRandall Stewart 						TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
81644249214SRandall Stewart 						control->on_strm_q = 0;
81744249214SRandall Stewart 					}
81844249214SRandall Stewart 				}
81944249214SRandall Stewart 				if (control->pdapi_started) {
82044249214SRandall Stewart 					strm->pd_api_started = 0;
82144249214SRandall Stewart 					control->pdapi_started = 0;
82244249214SRandall Stewart 				}
82344249214SRandall Stewart 				if (control->on_strm_q) {
82444249214SRandall Stewart 					TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
82544249214SRandall Stewart 					control->on_strm_q = 0;
82626f0250aSMichael Tuexen 					SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
82744249214SRandall Stewart 				}
828c09a1534SMichael Tuexen 				if (control->on_read_q == 0) {
829c09a1534SMichael Tuexen 					sctp_add_to_readq(stcb->sctp_ep, stcb, control,
830c09a1534SMichael Tuexen 					    &stcb->sctp_socket->so_rcv, control->end_added,
831c09a1534SMichael Tuexen 					    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
832c09a1534SMichael Tuexen 				}
833b1deed45SMichael Tuexen 				sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
834643fd575SMichael Tuexen 				if (!TAILQ_EMPTY(&nc->reasm) && (nc->first_frag_seen)) {
83544249214SRandall Stewart 					/*
83644249214SRandall Stewart 					 * Switch to the new guy and
83744249214SRandall Stewart 					 * continue
83844249214SRandall Stewart 					 */
83944249214SRandall Stewart 					control = nc;
84044249214SRandall Stewart 					goto restart;
841643fd575SMichael Tuexen 				} else {
842643fd575SMichael Tuexen 					sctp_free_a_readq(stcb, nc);
84344249214SRandall Stewart 				}
84444249214SRandall Stewart 				return (1);
845643fd575SMichael Tuexen 			} else {
846643fd575SMichael Tuexen 				sctp_free_a_readq(stcb, nc);
84744249214SRandall Stewart 			}
84844249214SRandall Stewart 		} else {
84944249214SRandall Stewart 			/* Can't add more */
85044249214SRandall Stewart 			break;
85144249214SRandall Stewart 		}
85244249214SRandall Stewart 	}
85344249214SRandall Stewart 	if ((control->length > pd_point) && (strm->pd_api_started == 0)) {
854c09a1534SMichael Tuexen 		strm->pd_api_started = 1;
855c09a1534SMichael Tuexen 		control->pdapi_started = 1;
85644249214SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb, control,
85744249214SRandall Stewart 		    &stcb->sctp_socket->so_rcv, control->end_added,
85844249214SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
859b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
86044249214SRandall Stewart 		return (0);
86144249214SRandall Stewart 	} else {
86244249214SRandall Stewart 		return (1);
86344249214SRandall Stewart 	}
86444249214SRandall Stewart }
86544249214SRandall Stewart 
86644249214SRandall Stewart static void
86744249214SRandall Stewart sctp_inject_old_data_unordered(struct sctp_tcb *stcb, struct sctp_association *asoc,
86844249214SRandall Stewart     struct sctp_queued_to_read *control,
86944249214SRandall Stewart     struct sctp_tmit_chunk *chk,
87044249214SRandall Stewart     int *abort_flag)
87144249214SRandall Stewart {
87244249214SRandall Stewart 	struct sctp_tmit_chunk *at;
87344249214SRandall Stewart 	int inserted = 0;
87444249214SRandall Stewart 
87544249214SRandall Stewart 	/*
87644249214SRandall Stewart 	 * Here we need to place the chunk into the control structure sorted
87744249214SRandall Stewart 	 * in the correct order.
87844249214SRandall Stewart 	 */
87944249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
88044249214SRandall Stewart 		/* Its the very first one. */
88144249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
882f2ea2a2dSMichael Tuexen 		    "chunk is a first fsn: %u becomes fsn_included\n",
88344249214SRandall Stewart 		    chk->rec.data.fsn_num);
88444249214SRandall Stewart 		if (control->first_frag_seen) {
88544249214SRandall Stewart 			/*
88644249214SRandall Stewart 			 * In old un-ordered we can reassembly on one
88744249214SRandall Stewart 			 * control multiple messages. As long as the next
88844249214SRandall Stewart 			 * FIRST is greater then the old first (TSN i.e. FSN
88944249214SRandall Stewart 			 * wise)
89044249214SRandall Stewart 			 */
89144249214SRandall Stewart 			struct mbuf *tdata;
89244249214SRandall Stewart 			uint32_t tmp;
89344249214SRandall Stewart 
89444249214SRandall Stewart 			if (SCTP_TSN_GT(chk->rec.data.fsn_num, control->fsn_included)) {
89544249214SRandall Stewart 				/*
89644249214SRandall Stewart 				 * Easy way the start of a new guy beyond
89744249214SRandall Stewart 				 * the lowest
89844249214SRandall Stewart 				 */
89944249214SRandall Stewart 				goto place_chunk;
90044249214SRandall Stewart 			}
90144249214SRandall Stewart 			if ((chk->rec.data.fsn_num == control->fsn_included) ||
90244249214SRandall Stewart 			    (control->pdapi_started)) {
90344249214SRandall Stewart 				/*
90444249214SRandall Stewart 				 * Ok this should not happen, if it does we
90544249214SRandall Stewart 				 * started the pd-api on the higher TSN
90644249214SRandall Stewart 				 * (since the equals part is a TSN failure
90744249214SRandall Stewart 				 * it must be that).
90844249214SRandall Stewart 				 *
90944249214SRandall Stewart 				 * We are completly hosed in that case since I
91044249214SRandall Stewart 				 * have no way to recover. This really will
91144249214SRandall Stewart 				 * only happen if we can get more TSN's
91244249214SRandall Stewart 				 * higher before the pd-api-point.
91344249214SRandall Stewart 				 */
914b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control, chk,
91544249214SRandall Stewart 				    abort_flag,
91644249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
91744249214SRandall Stewart 
91844249214SRandall Stewart 				return;
91944249214SRandall Stewart 			}
92044249214SRandall Stewart 			/*
92144249214SRandall Stewart 			 * Ok we have two firsts and the one we just got is
92244249214SRandall Stewart 			 * smaller than the one we previously placed.. yuck!
92344249214SRandall Stewart 			 * We must swap them out.
92444249214SRandall Stewart 			 */
92544249214SRandall Stewart 			/* swap the mbufs */
92644249214SRandall Stewart 			tdata = control->data;
92744249214SRandall Stewart 			control->data = chk->data;
92844249214SRandall Stewart 			chk->data = tdata;
92944249214SRandall Stewart 			/* Swap the lengths */
93044249214SRandall Stewart 			tmp = control->length;
93144249214SRandall Stewart 			control->length = chk->send_size;
93244249214SRandall Stewart 			chk->send_size = tmp;
93344249214SRandall Stewart 			/* Fix the FSN included */
93444249214SRandall Stewart 			tmp = control->fsn_included;
93544249214SRandall Stewart 			control->fsn_included = chk->rec.data.fsn_num;
93644249214SRandall Stewart 			chk->rec.data.fsn_num = tmp;
93744249214SRandall Stewart 			goto place_chunk;
93844249214SRandall Stewart 		}
93944249214SRandall Stewart 		control->first_frag_seen = 1;
94044249214SRandall Stewart 		control->top_fsn = control->fsn_included = chk->rec.data.fsn_num;
94144249214SRandall Stewart 		control->data = chk->data;
94244249214SRandall Stewart 		sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
94344249214SRandall Stewart 		chk->data = NULL;
94444249214SRandall Stewart 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
94544249214SRandall Stewart 		sctp_setup_tail_pointer(control);
94644249214SRandall Stewart 		return;
94744249214SRandall Stewart 	}
94844249214SRandall Stewart place_chunk:
94944249214SRandall Stewart 	if (TAILQ_EMPTY(&control->reasm)) {
95044249214SRandall Stewart 		TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
95144249214SRandall Stewart 		asoc->size_on_reasm_queue += chk->send_size;
95244249214SRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
95344249214SRandall Stewart 		return;
95444249214SRandall Stewart 	}
95544249214SRandall Stewart 	TAILQ_FOREACH(at, &control->reasm, sctp_next) {
95644249214SRandall Stewart 		if (SCTP_TSN_GT(at->rec.data.fsn_num, chk->rec.data.fsn_num)) {
95744249214SRandall Stewart 			/*
95844249214SRandall Stewart 			 * This one in queue is bigger than the new one,
95944249214SRandall Stewart 			 * insert the new one before at.
96044249214SRandall Stewart 			 */
96144249214SRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
96244249214SRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
96344249214SRandall Stewart 			inserted = 1;
96444249214SRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
96544249214SRandall Stewart 			break;
96644249214SRandall Stewart 		} else if (at->rec.data.fsn_num == chk->rec.data.fsn_num) {
96744249214SRandall Stewart 			/*
96844249214SRandall Stewart 			 * They sent a duplicate fsn number. This really
96944249214SRandall Stewart 			 * should not happen since the FSN is a TSN and it
97044249214SRandall Stewart 			 * should have been dropped earlier.
97144249214SRandall Stewart 			 */
972b1deed45SMichael Tuexen 			sctp_abort_in_reasm(stcb, control, chk,
97344249214SRandall Stewart 			    abort_flag,
97444249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
97544249214SRandall Stewart 			return;
97644249214SRandall Stewart 		}
97744249214SRandall Stewart 	}
97844249214SRandall Stewart 	if (inserted == 0) {
97944249214SRandall Stewart 		/* Its at the end */
98044249214SRandall Stewart 		asoc->size_on_reasm_queue += chk->send_size;
98144249214SRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
98244249214SRandall Stewart 		control->top_fsn = chk->rec.data.fsn_num;
98344249214SRandall Stewart 		TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
98444249214SRandall Stewart 	}
98544249214SRandall Stewart }
98644249214SRandall Stewart 
98744249214SRandall Stewart static int
98844249214SRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc, struct sctp_stream_in *strm)
98944249214SRandall Stewart {
99044249214SRandall Stewart 	/*
99144249214SRandall Stewart 	 * Given a stream, strm, see if any of the SSN's on it that are
99244249214SRandall Stewart 	 * fragmented are ready to deliver. If so go ahead and place them on
99344249214SRandall Stewart 	 * the read queue. In so placing if we have hit the end, then we
99444249214SRandall Stewart 	 * need to remove them from the stream's queue.
99544249214SRandall Stewart 	 */
99644249214SRandall Stewart 	struct sctp_queued_to_read *control, *nctl = NULL;
99744249214SRandall Stewart 	uint32_t next_to_del;
99844249214SRandall Stewart 	uint32_t pd_point;
99944249214SRandall Stewart 	int ret = 0;
100044249214SRandall Stewart 
1001810ec536SMichael Tuexen 	if (stcb->sctp_socket) {
1002d4d23375SMichael Tuexen 		pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
1003810ec536SMichael Tuexen 		    stcb->sctp_ep->partial_delivery_point);
1004810ec536SMichael Tuexen 	} else {
1005810ec536SMichael Tuexen 		pd_point = stcb->sctp_ep->partial_delivery_point;
1006810ec536SMichael Tuexen 	}
100744249214SRandall Stewart 	control = TAILQ_FIRST(&strm->uno_inqueue);
100844249214SRandall Stewart 	if ((control) &&
100944249214SRandall Stewart 	    (asoc->idata_supported == 0)) {
101044249214SRandall Stewart 		/* Special handling needed for "old" data format */
101144249214SRandall Stewart 		if (sctp_handle_old_data(stcb, asoc, strm, control, pd_point)) {
101244249214SRandall Stewart 			goto done_un;
1013f8829a4aSRandall Stewart 		}
1014f8829a4aSRandall Stewart 	}
101544249214SRandall Stewart 	if (strm->pd_api_started) {
101644249214SRandall Stewart 		/* Can't add more */
101744249214SRandall Stewart 		return (0);
101844249214SRandall Stewart 	}
101944249214SRandall Stewart 	while (control) {
1020f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_XXX, "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u -uo\n",
102144249214SRandall Stewart 		    control, control->end_added, control->sinfo_ssn, control->top_fsn, control->fsn_included);
102244249214SRandall Stewart 		nctl = TAILQ_NEXT(control, next_instrm);
102344249214SRandall Stewart 		if (control->end_added) {
102444249214SRandall Stewart 			/* We just put the last bit on */
102544249214SRandall Stewart 			if (control->on_strm_q) {
102698d5fd97SMichael Tuexen #ifdef INVARIANTS
102744249214SRandall Stewart 				if (control->on_strm_q != SCTP_ON_UNORDERED) {
102844249214SRandall Stewart 					panic("Huh control: %p on_q: %d -- not unordered?",
102944249214SRandall Stewart 					    control, control->on_strm_q);
103044249214SRandall Stewart 				}
103198d5fd97SMichael Tuexen #endif
103226f0250aSMichael Tuexen 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
103344249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
103444249214SRandall Stewart 				control->on_strm_q = 0;
103544249214SRandall Stewart 			}
103644249214SRandall Stewart 			if (control->on_read_q == 0) {
103744249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
103844249214SRandall Stewart 				    control,
103944249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
104044249214SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
104144249214SRandall Stewart 			}
1042f8829a4aSRandall Stewart 		} else {
104344249214SRandall Stewart 			/* Can we do a PD-API for this un-ordered guy? */
104444249214SRandall Stewart 			if ((control->length >= pd_point) && (strm->pd_api_started == 0)) {
104544249214SRandall Stewart 				strm->pd_api_started = 1;
104644249214SRandall Stewart 				control->pdapi_started = 1;
104744249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
104844249214SRandall Stewart 				    control,
104944249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
105044249214SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
105144249214SRandall Stewart 
105244249214SRandall Stewart 				break;
1053139bc87fSRandall Stewart 			}
1054f8829a4aSRandall Stewart 		}
105544249214SRandall Stewart 		control = nctl;
105644249214SRandall Stewart 	}
105744249214SRandall Stewart done_un:
105844249214SRandall Stewart 	control = TAILQ_FIRST(&strm->inqueue);
105944249214SRandall Stewart 	if (strm->pd_api_started) {
106044249214SRandall Stewart 		/* Can't add more */
106144249214SRandall Stewart 		return (0);
106244249214SRandall Stewart 	}
106344249214SRandall Stewart 	if (control == NULL) {
106444249214SRandall Stewart 		return (ret);
106544249214SRandall Stewart 	}
106644249214SRandall Stewart 	if (strm->last_sequence_delivered == control->sinfo_ssn) {
106744249214SRandall Stewart 		/*
106844249214SRandall Stewart 		 * Ok the guy at the top was being partially delivered
106944249214SRandall Stewart 		 * completed, so we remove it. Note the pd_api flag was
107044249214SRandall Stewart 		 * taken off when the chunk was merged on in
107144249214SRandall Stewart 		 * sctp_queue_data_for_reasm below.
107244249214SRandall Stewart 		 */
107344249214SRandall Stewart 		nctl = TAILQ_NEXT(control, next_instrm);
107444249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1075f2ea2a2dSMichael Tuexen 		    "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (lastdel: %u)- o\n",
107644249214SRandall Stewart 		    control, control->end_added, control->sinfo_ssn,
107744249214SRandall Stewart 		    control->top_fsn, control->fsn_included,
107844249214SRandall Stewart 		    strm->last_sequence_delivered);
107944249214SRandall Stewart 		if (control->end_added) {
108044249214SRandall Stewart 			if (control->on_strm_q) {
108198d5fd97SMichael Tuexen #ifdef INVARIANTS
108244249214SRandall Stewart 				if (control->on_strm_q != SCTP_ON_ORDERED) {
108344249214SRandall Stewart 					panic("Huh control: %p on_q: %d -- not ordered?",
108444249214SRandall Stewart 					    control, control->on_strm_q);
108544249214SRandall Stewart 				}
108698d5fd97SMichael Tuexen #endif
108726f0250aSMichael Tuexen 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
108844249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
108944249214SRandall Stewart 				control->on_strm_q = 0;
109044249214SRandall Stewart 			}
1091c09a1534SMichael Tuexen 			if (strm->pd_api_started && control->pdapi_started) {
1092c09a1534SMichael Tuexen 				control->pdapi_started = 0;
1093c09a1534SMichael Tuexen 				strm->pd_api_started = 0;
1094c09a1534SMichael Tuexen 			}
109544249214SRandall Stewart 			if (control->on_read_q == 0) {
109644249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
109744249214SRandall Stewart 				    control,
109844249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
109944249214SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
110044249214SRandall Stewart 			}
110144249214SRandall Stewart 			control = nctl;
110244249214SRandall Stewart 		}
110344249214SRandall Stewart 	}
110444249214SRandall Stewart 	if (strm->pd_api_started) {
110544249214SRandall Stewart 		/*
110644249214SRandall Stewart 		 * Can't add more must have gotten an un-ordered above being
110744249214SRandall Stewart 		 * partially delivered.
110844249214SRandall Stewart 		 */
110944249214SRandall Stewart 		return (0);
111044249214SRandall Stewart 	}
111144249214SRandall Stewart deliver_more:
111244249214SRandall Stewart 	next_to_del = strm->last_sequence_delivered + 1;
111344249214SRandall Stewart 	if (control) {
111444249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1115f2ea2a2dSMichael Tuexen 		    "Looking at control: %p e(%d) ssn: %u top_fsn: %u inc_fsn: %u (nxtdel: %u)- o\n",
111644249214SRandall Stewart 		    control, control->end_added, control->sinfo_ssn, control->top_fsn, control->fsn_included,
111744249214SRandall Stewart 		    next_to_del);
111844249214SRandall Stewart 		nctl = TAILQ_NEXT(control, next_instrm);
111944249214SRandall Stewart 		if ((control->sinfo_ssn == next_to_del) &&
112044249214SRandall Stewart 		    (control->first_frag_seen)) {
1121c09a1534SMichael Tuexen 			int done;
1122c09a1534SMichael Tuexen 
112344249214SRandall Stewart 			/* Ok we can deliver it onto the stream. */
112444249214SRandall Stewart 			if (control->end_added) {
112544249214SRandall Stewart 				/* We are done with it afterwards */
112644249214SRandall Stewart 				if (control->on_strm_q) {
112798d5fd97SMichael Tuexen #ifdef INVARIANTS
112844249214SRandall Stewart 					if (control->on_strm_q != SCTP_ON_ORDERED) {
112944249214SRandall Stewart 						panic("Huh control: %p on_q: %d -- not ordered?",
113044249214SRandall Stewart 						    control, control->on_strm_q);
113144249214SRandall Stewart 					}
113298d5fd97SMichael Tuexen #endif
113326f0250aSMichael Tuexen 					SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
113444249214SRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
113544249214SRandall Stewart 					control->on_strm_q = 0;
113644249214SRandall Stewart 				}
113744249214SRandall Stewart 				ret++;
113844249214SRandall Stewart 			}
113944249214SRandall Stewart 			if (((control->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
114044249214SRandall Stewart 				/*
114144249214SRandall Stewart 				 * A singleton now slipping through - mark
114244249214SRandall Stewart 				 * it non-revokable too
114344249214SRandall Stewart 				 */
114444249214SRandall Stewart 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
114544249214SRandall Stewart 			} else if (control->end_added == 0) {
114644249214SRandall Stewart 				/*
114744249214SRandall Stewart 				 * Check if we can defer adding until its
114844249214SRandall Stewart 				 * all there
114944249214SRandall Stewart 				 */
115044249214SRandall Stewart 				if ((control->length < pd_point) || (strm->pd_api_started)) {
115144249214SRandall Stewart 					/*
115244249214SRandall Stewart 					 * Don't need it or cannot add more
115344249214SRandall Stewart 					 * (one being delivered that way)
115444249214SRandall Stewart 					 */
115544249214SRandall Stewart 					goto out;
115644249214SRandall Stewart 				}
115744249214SRandall Stewart 			}
1158c09a1534SMichael Tuexen 			done = (control->end_added) && (control->last_frag_seen);
115944249214SRandall Stewart 			if (control->on_read_q == 0) {
116044249214SRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
116144249214SRandall Stewart 				    control,
116244249214SRandall Stewart 				    &stcb->sctp_socket->so_rcv, control->end_added,
116344249214SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
116444249214SRandall Stewart 			}
116544249214SRandall Stewart 			strm->last_sequence_delivered = next_to_del;
1166c09a1534SMichael Tuexen 			if (done) {
116744249214SRandall Stewart 				control = nctl;
116844249214SRandall Stewart 				goto deliver_more;
116944249214SRandall Stewart 			} else {
117044249214SRandall Stewart 				/* We are now doing PD API */
117144249214SRandall Stewart 				strm->pd_api_started = 1;
117244249214SRandall Stewart 				control->pdapi_started = 1;
117344249214SRandall Stewart 			}
117444249214SRandall Stewart 		}
117544249214SRandall Stewart 	}
117644249214SRandall Stewart out:
117744249214SRandall Stewart 	return (ret);
117844249214SRandall Stewart }
117944249214SRandall Stewart 
118044249214SRandall Stewart void
118144249214SRandall Stewart sctp_add_chk_to_control(struct sctp_queued_to_read *control,
118244249214SRandall Stewart     struct sctp_stream_in *strm,
118344249214SRandall Stewart     struct sctp_tcb *stcb, struct sctp_association *asoc,
118444249214SRandall Stewart     struct sctp_tmit_chunk *chk)
118544249214SRandall Stewart {
118644249214SRandall Stewart 	/*
118744249214SRandall Stewart 	 * Given a control and a chunk, merge the data from the chk onto the
118844249214SRandall Stewart 	 * control and free up the chunk resources.
118944249214SRandall Stewart 	 */
119044249214SRandall Stewart 	int i_locked = 0;
119144249214SRandall Stewart 
119244249214SRandall Stewart 	if (control->on_read_q) {
119344249214SRandall Stewart 		/*
119444249214SRandall Stewart 		 * Its being pd-api'd so we must do some locks.
119544249214SRandall Stewart 		 */
119644249214SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
119744249214SRandall Stewart 		i_locked = 1;
119844249214SRandall Stewart 	}
119944249214SRandall Stewart 	if (control->data == NULL) {
120044249214SRandall Stewart 		control->data = chk->data;
120144249214SRandall Stewart 		sctp_setup_tail_pointer(control);
120244249214SRandall Stewart 	} else {
120344249214SRandall Stewart 		sctp_add_to_tail_pointer(control, chk->data);
120444249214SRandall Stewart 	}
120544249214SRandall Stewart 	control->fsn_included = chk->rec.data.fsn_num;
120644249214SRandall Stewart 	asoc->size_on_reasm_queue -= chk->send_size;
120744249214SRandall Stewart 	sctp_ucount_decr(asoc->cnt_on_reasm_queue);
120844249214SRandall Stewart 	sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
120944249214SRandall Stewart 	chk->data = NULL;
121044249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
121144249214SRandall Stewart 		control->first_frag_seen = 1;
121244249214SRandall Stewart 	}
121344249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
121444249214SRandall Stewart 		/* Its complete */
121544249214SRandall Stewart 		if ((control->on_strm_q) && (control->on_read_q)) {
121644249214SRandall Stewart 			if (control->pdapi_started) {
121744249214SRandall Stewart 				control->pdapi_started = 0;
121844249214SRandall Stewart 				strm->pd_api_started = 0;
121944249214SRandall Stewart 			}
122044249214SRandall Stewart 			if (control->on_strm_q == SCTP_ON_UNORDERED) {
122144249214SRandall Stewart 				/* Unordered */
122244249214SRandall Stewart 				TAILQ_REMOVE(&strm->uno_inqueue, control, next_instrm);
122344249214SRandall Stewart 				control->on_strm_q = 0;
122444249214SRandall Stewart 			} else if (control->on_strm_q == SCTP_ON_ORDERED) {
122544249214SRandall Stewart 				/* Ordered */
122644249214SRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
122744249214SRandall Stewart 				control->on_strm_q = 0;
122898d5fd97SMichael Tuexen #ifdef INVARIANTS
122944249214SRandall Stewart 			} else if (control->on_strm_q) {
123044249214SRandall Stewart 				panic("Unknown state on ctrl: %p on_strm_q: %d", control,
123144249214SRandall Stewart 				    control->on_strm_q);
123298d5fd97SMichael Tuexen #endif
123344249214SRandall Stewart 			}
123444249214SRandall Stewart 		}
123544249214SRandall Stewart 		control->end_added = 1;
123644249214SRandall Stewart 		control->last_frag_seen = 1;
123744249214SRandall Stewart 	}
123844249214SRandall Stewart 	if (i_locked) {
123944249214SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
124044249214SRandall Stewart 	}
124144249214SRandall Stewart 	sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1242f8829a4aSRandall Stewart }
1243f8829a4aSRandall Stewart 
1244f8829a4aSRandall Stewart /*
1245f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
1246f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
1247f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
1248f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
1249f8829a4aSRandall Stewart  */
1250f8829a4aSRandall Stewart static void
1251f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
125244249214SRandall Stewart     struct sctp_stream_in *strm,
125344249214SRandall Stewart     struct sctp_queued_to_read *control,
125444249214SRandall Stewart     struct sctp_tmit_chunk *chk,
125544249214SRandall Stewart     int created_control,
125644249214SRandall Stewart     int *abort_flag, uint32_t tsn)
1257f8829a4aSRandall Stewart {
125844249214SRandall Stewart 	uint32_t next_fsn;
125944249214SRandall Stewart 	struct sctp_tmit_chunk *at, *nat;
1260c09a1534SMichael Tuexen 	int do_wakeup, unordered;
1261f8829a4aSRandall Stewart 
1262f8829a4aSRandall Stewart 	/*
126344249214SRandall Stewart 	 * For old un-ordered data chunks.
1264f8829a4aSRandall Stewart 	 */
126544249214SRandall Stewart 	if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
126644249214SRandall Stewart 		unordered = 1;
1267f8829a4aSRandall Stewart 	} else {
126844249214SRandall Stewart 		unordered = 0;
126944249214SRandall Stewart 	}
127044249214SRandall Stewart 	/* Must be added to the stream-in queue */
127144249214SRandall Stewart 	if (created_control) {
127244249214SRandall Stewart 		if (sctp_place_control_in_stream(strm, asoc, control)) {
127344249214SRandall Stewart 			/* Duplicate SSN? */
127444249214SRandall Stewart 			clean_up_control(stcb, control);
1275b1deed45SMichael Tuexen 			sctp_abort_in_reasm(stcb, control, chk,
127644249214SRandall Stewart 			    abort_flag,
127744249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
127844249214SRandall Stewart 			return;
127944249214SRandall Stewart 		}
128044249214SRandall Stewart 		if ((tsn == (asoc->cumulative_tsn + 1) && (asoc->idata_supported == 0))) {
1281f8829a4aSRandall Stewart 			/*
128244249214SRandall Stewart 			 * Ok we created this control and now lets validate
128344249214SRandall Stewart 			 * that its legal i.e. there is a B bit set, if not
128444249214SRandall Stewart 			 * and we have up to the cum-ack then its invalid.
128544249214SRandall Stewart 			 */
128644249214SRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
1287b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control, chk,
128844249214SRandall Stewart 				    abort_flag,
128944249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
129044249214SRandall Stewart 				return;
129144249214SRandall Stewart 			}
129244249214SRandall Stewart 		}
129344249214SRandall Stewart 	}
1294a39ddef0SMichael Tuexen 	if ((asoc->idata_supported == 0) && (unordered == 1)) {
1295b1deed45SMichael Tuexen 		sctp_inject_old_data_unordered(stcb, asoc, control, chk, abort_flag);
129644249214SRandall Stewart 		return;
129744249214SRandall Stewart 	}
129844249214SRandall Stewart 	/*
129944249214SRandall Stewart 	 * Ok we must queue the chunk into the reasembly portion: o if its
130044249214SRandall Stewart 	 * the first it goes to the control mbuf. o if its not first but the
130144249214SRandall Stewart 	 * next in sequence it goes to the control, and each succeeding one
130244249214SRandall Stewart 	 * in order also goes. o if its not in order we place it on the list
130344249214SRandall Stewart 	 * in its place.
130444249214SRandall Stewart 	 */
130544249214SRandall Stewart 	if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
130644249214SRandall Stewart 		/* Its the very first one. */
130744249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1308f2ea2a2dSMichael Tuexen 		    "chunk is a first fsn: %u becomes fsn_included\n",
130944249214SRandall Stewart 		    chk->rec.data.fsn_num);
131044249214SRandall Stewart 		if (control->first_frag_seen) {
131144249214SRandall Stewart 			/*
131244249214SRandall Stewart 			 * Error on senders part, they either sent us two
131344249214SRandall Stewart 			 * data chunks with FIRST, or they sent two
131444249214SRandall Stewart 			 * un-ordered chunks that were fragmented at the
131544249214SRandall Stewart 			 * same time in the same stream.
131644249214SRandall Stewart 			 */
1317b1deed45SMichael Tuexen 			sctp_abort_in_reasm(stcb, control, chk,
131844249214SRandall Stewart 			    abort_flag,
131944249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
132044249214SRandall Stewart 			return;
132144249214SRandall Stewart 		}
132244249214SRandall Stewart 		control->first_frag_seen = 1;
132344249214SRandall Stewart 		control->fsn_included = chk->rec.data.fsn_num;
132444249214SRandall Stewart 		control->data = chk->data;
132544249214SRandall Stewart 		sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
132644249214SRandall Stewart 		chk->data = NULL;
132744249214SRandall Stewart 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
132844249214SRandall Stewart 		sctp_setup_tail_pointer(control);
132944249214SRandall Stewart 	} else {
133044249214SRandall Stewart 		/* Place the chunk in our list */
133144249214SRandall Stewart 		int inserted = 0;
133244249214SRandall Stewart 
133344249214SRandall Stewart 		if (control->last_frag_seen == 0) {
133444249214SRandall Stewart 			/* Still willing to raise highest FSN seen */
133544249214SRandall Stewart 			if (SCTP_TSN_GT(chk->rec.data.fsn_num, control->top_fsn)) {
133644249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1337f2ea2a2dSMichael Tuexen 				    "We have a new top_fsn: %u\n",
133844249214SRandall Stewart 				    chk->rec.data.fsn_num);
133944249214SRandall Stewart 				control->top_fsn = chk->rec.data.fsn_num;
134044249214SRandall Stewart 			}
134144249214SRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
134244249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1343f2ea2a2dSMichael Tuexen 				    "The last fsn is now in place fsn: %u\n",
134444249214SRandall Stewart 				    chk->rec.data.fsn_num);
134544249214SRandall Stewart 				control->last_frag_seen = 1;
134644249214SRandall Stewart 			}
134744249214SRandall Stewart 			if (asoc->idata_supported || control->first_frag_seen) {
134844249214SRandall Stewart 				/*
134944249214SRandall Stewart 				 * For IDATA we always check since we know
135044249214SRandall Stewart 				 * that the first fragment is 0. For old
135144249214SRandall Stewart 				 * DATA we have to receive the first before
1352cd0a4ff6SPedro F. Giffuni 				 * we know the first FSN (which is the TSN).
135344249214SRandall Stewart 				 */
135444249214SRandall Stewart 				if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn_num)) {
135544249214SRandall Stewart 					/*
135644249214SRandall Stewart 					 * We have already delivered up to
135744249214SRandall Stewart 					 * this so its a dup
135844249214SRandall Stewart 					 */
1359b1deed45SMichael Tuexen 					sctp_abort_in_reasm(stcb, control, chk,
136044249214SRandall Stewart 					    abort_flag,
136144249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
136244249214SRandall Stewart 					return;
136344249214SRandall Stewart 				}
136444249214SRandall Stewart 			}
136544249214SRandall Stewart 		} else {
136644249214SRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
136744249214SRandall Stewart 				/* Second last? huh? */
136844249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1369f2ea2a2dSMichael Tuexen 				    "Duplicate last fsn: %u (top: %u) -- abort\n",
137044249214SRandall Stewart 				    chk->rec.data.fsn_num, control->top_fsn);
1371b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control,
137244249214SRandall Stewart 				    chk, abort_flag,
137344249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
137444249214SRandall Stewart 				return;
137544249214SRandall Stewart 			}
137644249214SRandall Stewart 			if (asoc->idata_supported || control->first_frag_seen) {
137744249214SRandall Stewart 				/*
137844249214SRandall Stewart 				 * For IDATA we always check since we know
137944249214SRandall Stewart 				 * that the first fragment is 0. For old
138044249214SRandall Stewart 				 * DATA we have to receive the first before
1381cd0a4ff6SPedro F. Giffuni 				 * we know the first FSN (which is the TSN).
138244249214SRandall Stewart 				 */
138344249214SRandall Stewart 
138444249214SRandall Stewart 				if (SCTP_TSN_GE(control->fsn_included, chk->rec.data.fsn_num)) {
138544249214SRandall Stewart 					/*
138644249214SRandall Stewart 					 * We have already delivered up to
138744249214SRandall Stewart 					 * this so its a dup
138844249214SRandall Stewart 					 */
138944249214SRandall Stewart 					SCTPDBG(SCTP_DEBUG_XXX,
1390f2ea2a2dSMichael Tuexen 					    "New fsn: %u is already seen in included_fsn: %u -- abort\n",
139144249214SRandall Stewart 					    chk->rec.data.fsn_num, control->fsn_included);
1392b1deed45SMichael Tuexen 					sctp_abort_in_reasm(stcb, control, chk,
139344249214SRandall Stewart 					    abort_flag,
139444249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
139544249214SRandall Stewart 					return;
139644249214SRandall Stewart 				}
139744249214SRandall Stewart 			}
139844249214SRandall Stewart 			/*
139944249214SRandall Stewart 			 * validate not beyond top FSN if we have seen last
1400f8829a4aSRandall Stewart 			 * one
1401f8829a4aSRandall Stewart 			 */
140244249214SRandall Stewart 			if (SCTP_TSN_GT(chk->rec.data.fsn_num, control->top_fsn)) {
140344249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1404f2ea2a2dSMichael Tuexen 				    "New fsn: %u is beyond or at top_fsn: %u -- abort\n",
140544249214SRandall Stewart 				    chk->rec.data.fsn_num,
140644249214SRandall Stewart 				    control->top_fsn);
1407b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control, chk,
140844249214SRandall Stewart 				    abort_flag,
140944249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
141044249214SRandall Stewart 				return;
141144249214SRandall Stewart 			}
141244249214SRandall Stewart 		}
141344249214SRandall Stewart 		/*
141444249214SRandall Stewart 		 * If we reach here, we need to place the new chunk in the
141544249214SRandall Stewart 		 * reassembly for this control.
141644249214SRandall Stewart 		 */
141744249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX,
1418f2ea2a2dSMichael Tuexen 		    "chunk is a not first fsn: %u needs to be inserted\n",
141944249214SRandall Stewart 		    chk->rec.data.fsn_num);
142044249214SRandall Stewart 		TAILQ_FOREACH(at, &control->reasm, sctp_next) {
142144249214SRandall Stewart 			if (SCTP_TSN_GT(at->rec.data.fsn_num, chk->rec.data.fsn_num)) {
142244249214SRandall Stewart 				/*
142344249214SRandall Stewart 				 * This one in queue is bigger than the new
142444249214SRandall Stewart 				 * one, insert the new one before at.
142544249214SRandall Stewart 				 */
142644249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1427f2ea2a2dSMichael Tuexen 				    "Insert it before fsn: %u\n",
142844249214SRandall Stewart 				    at->rec.data.fsn_num);
1429f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1430f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
143144249214SRandall Stewart 				TAILQ_INSERT_BEFORE(at, chk, sctp_next);
143244249214SRandall Stewart 				inserted = 1;
143344249214SRandall Stewart 				break;
143444249214SRandall Stewart 			} else if (at->rec.data.fsn_num == chk->rec.data.fsn_num) {
143544249214SRandall Stewart 				/*
143644249214SRandall Stewart 				 * Gak, He sent me a duplicate str seq
143744249214SRandall Stewart 				 * number
143844249214SRandall Stewart 				 */
143944249214SRandall Stewart 				/*
144044249214SRandall Stewart 				 * foo bar, I guess I will just free this
144144249214SRandall Stewart 				 * new guy, should we abort too? FIX ME
144244249214SRandall Stewart 				 * MAYBE? Or it COULD be that the SSN's have
144344249214SRandall Stewart 				 * wrapped. Maybe I should compare to TSN
144444249214SRandall Stewart 				 * somehow... sigh for now just blow away
144544249214SRandall Stewart 				 * the chunk!
144644249214SRandall Stewart 				 */
144744249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1448f2ea2a2dSMichael Tuexen 				    "Duplicate to fsn: %u -- abort\n",
144944249214SRandall Stewart 				    at->rec.data.fsn_num);
1450b1deed45SMichael Tuexen 				sctp_abort_in_reasm(stcb, control,
145144249214SRandall Stewart 				    chk, abort_flag,
145244249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
145344249214SRandall Stewart 				return;
145444249214SRandall Stewart 			}
145544249214SRandall Stewart 		}
145644249214SRandall Stewart 		if (inserted == 0) {
145744249214SRandall Stewart 			/* Goes on the end */
1458f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "Inserting at tail of list fsn: %u\n",
145944249214SRandall Stewart 			    chk->rec.data.fsn_num);
146044249214SRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
146144249214SRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
146244249214SRandall Stewart 			TAILQ_INSERT_TAIL(&control->reasm, chk, sctp_next);
146344249214SRandall Stewart 		}
146444249214SRandall Stewart 	}
146544249214SRandall Stewart 	/*
146644249214SRandall Stewart 	 * Ok lets see if we can suck any up into the control structure that
146744249214SRandall Stewart 	 * are in seq if it makes sense.
146844249214SRandall Stewart 	 */
1469c09a1534SMichael Tuexen 	do_wakeup = 0;
147044249214SRandall Stewart 	/*
147144249214SRandall Stewart 	 * If the first fragment has not been seen there is no sense in
147244249214SRandall Stewart 	 * looking.
147344249214SRandall Stewart 	 */
147444249214SRandall Stewart 	if (control->first_frag_seen) {
147544249214SRandall Stewart 		next_fsn = control->fsn_included + 1;
147644249214SRandall Stewart 		TAILQ_FOREACH_SAFE(at, &control->reasm, sctp_next, nat) {
147744249214SRandall Stewart 			if (at->rec.data.fsn_num == next_fsn) {
147844249214SRandall Stewart 				/* We can add this one now to the control */
147944249214SRandall Stewart 				SCTPDBG(SCTP_DEBUG_XXX,
1480f2ea2a2dSMichael Tuexen 				    "Adding more to control: %p at: %p fsn: %u next_fsn: %u included: %u\n",
148144249214SRandall Stewart 				    control, at,
148244249214SRandall Stewart 				    at->rec.data.fsn_num,
148344249214SRandall Stewart 				    next_fsn, control->fsn_included);
148444249214SRandall Stewart 				TAILQ_REMOVE(&control->reasm, at, sctp_next);
148544249214SRandall Stewart 				sctp_add_chk_to_control(control, strm, stcb, asoc, at);
1486c09a1534SMichael Tuexen 				if (control->on_read_q) {
1487c09a1534SMichael Tuexen 					do_wakeup = 1;
1488c09a1534SMichael Tuexen 				}
148944249214SRandall Stewart 				next_fsn++;
149044249214SRandall Stewart 				if (control->end_added && control->pdapi_started) {
149144249214SRandall Stewart 					if (strm->pd_api_started) {
149244249214SRandall Stewart 						strm->pd_api_started = 0;
149344249214SRandall Stewart 						control->pdapi_started = 0;
149444249214SRandall Stewart 					}
149544249214SRandall Stewart 					if (control->on_read_q == 0) {
149644249214SRandall Stewart 						sctp_add_to_readq(stcb->sctp_ep, stcb,
149744249214SRandall Stewart 						    control,
149844249214SRandall Stewart 						    &stcb->sctp_socket->so_rcv, control->end_added,
149944249214SRandall Stewart 						    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1500c09a1534SMichael Tuexen 						do_wakeup = 1;
150144249214SRandall Stewart 					}
150244249214SRandall Stewart 					break;
150344249214SRandall Stewart 				}
150444249214SRandall Stewart 			} else {
1505f8829a4aSRandall Stewart 				break;
1506f8829a4aSRandall Stewart 			}
1507f8829a4aSRandall Stewart 		}
1508f8829a4aSRandall Stewart 	}
1509c09a1534SMichael Tuexen 	if (do_wakeup) {
151044249214SRandall Stewart 		/* Need to wakeup the reader */
1511b1deed45SMichael Tuexen 		sctp_wakeup_the_read_socket(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1512f8829a4aSRandall Stewart 	}
1513f8829a4aSRandall Stewart }
1514f8829a4aSRandall Stewart 
151544249214SRandall Stewart static struct sctp_queued_to_read *
151644249214SRandall Stewart find_reasm_entry(struct sctp_stream_in *strm, uint32_t msg_id, int ordered, int old)
1517f8829a4aSRandall Stewart {
1518c09a1534SMichael Tuexen 	struct sctp_queued_to_read *control;
1519f8829a4aSRandall Stewart 
152044249214SRandall Stewart 	if (ordered) {
1521c09a1534SMichael Tuexen 		TAILQ_FOREACH(control, &strm->inqueue, next_instrm) {
1522c09a1534SMichael Tuexen 			if (control->msg_id == msg_id) {
152344249214SRandall Stewart 				break;
152444249214SRandall Stewart 			}
152544249214SRandall Stewart 		}
1526f8829a4aSRandall Stewart 	} else {
152744249214SRandall Stewart 		if (old) {
1528c09a1534SMichael Tuexen 			control = TAILQ_FIRST(&strm->uno_inqueue);
1529c09a1534SMichael Tuexen 			return (control);
1530f8829a4aSRandall Stewart 		}
1531c09a1534SMichael Tuexen 		TAILQ_FOREACH(control, &strm->uno_inqueue, next_instrm) {
1532c09a1534SMichael Tuexen 			if (control->msg_id == msg_id) {
153344249214SRandall Stewart 				break;
1534f8829a4aSRandall Stewart 			}
1535f8829a4aSRandall Stewart 		}
1536f8829a4aSRandall Stewart 	}
1537c09a1534SMichael Tuexen 	return (control);
1538f8829a4aSRandall Stewart }
153944249214SRandall Stewart 
1540f8829a4aSRandall Stewart static int
1541f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
154244249214SRandall Stewart     struct mbuf **m, int offset, int chk_length,
1543f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
154444249214SRandall Stewart     int *break_flag, int last_chunk, uint8_t chtype)
1545f8829a4aSRandall Stewart {
1546f8829a4aSRandall Stewart 	/* Process a data chunk */
1547f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
154844249214SRandall Stewart 	struct sctp_data_chunk *ch;
154944249214SRandall Stewart 	struct sctp_idata_chunk *nch, chunk_buf;
1550f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
155144249214SRandall Stewart 	uint32_t tsn, fsn, gap, msg_id;
1552f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
15537215cc1bSMichael Tuexen 	int the_len;
1554139bc87fSRandall Stewart 	int need_reasm_check = 0;
155544249214SRandall Stewart 	uint16_t strmno;
1556ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
1557ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
155844249214SRandall Stewart 	struct sctp_queued_to_read *control = NULL;
1559f42a358aSRandall Stewart 	uint32_t protocol_id;
1560f42a358aSRandall Stewart 	uint8_t chunk_flags;
156117205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
156244249214SRandall Stewart 	struct sctp_stream_in *strm;
156344249214SRandall Stewart 	int ordered;
156444249214SRandall Stewart 	size_t clen;
156544249214SRandall Stewart 	int created_control = 0;
156644249214SRandall Stewart 	uint8_t old_data;
1567f8829a4aSRandall Stewart 
1568f8829a4aSRandall Stewart 	chk = NULL;
156944249214SRandall Stewart 	if (chtype == SCTP_IDATA) {
157044249214SRandall Stewart 		nch = (struct sctp_idata_chunk *)sctp_m_getptr(*m, offset,
157144249214SRandall Stewart 		    sizeof(struct sctp_idata_chunk), (uint8_t *) & chunk_buf);
157244249214SRandall Stewart 		ch = (struct sctp_data_chunk *)nch;
157344249214SRandall Stewart 		clen = sizeof(struct sctp_idata_chunk);
1574f8829a4aSRandall Stewart 		tsn = ntohl(ch->dp.tsn);
157544249214SRandall Stewart 		msg_id = ntohl(nch->dp.msg_id);
157644249214SRandall Stewart 		if (ch->ch.chunk_flags & SCTP_DATA_FIRST_FRAG)
157744249214SRandall Stewart 			fsn = 0;
157844249214SRandall Stewart 		else
1579e187bac2SMichael Tuexen 			fsn = ntohl(nch->dp.ppid_fsn.fsn);
158044249214SRandall Stewart 		old_data = 0;
158144249214SRandall Stewart 	} else {
158244249214SRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(*m, offset,
158344249214SRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
158444249214SRandall Stewart 		tsn = ntohl(ch->dp.tsn);
158544249214SRandall Stewart 		clen = sizeof(struct sctp_data_chunk);
158644249214SRandall Stewart 		fsn = tsn;
158744249214SRandall Stewart 		msg_id = (uint32_t) (ntohs(ch->dp.stream_sequence));
158844249214SRandall Stewart 		nch = NULL;
158944249214SRandall Stewart 		old_data = 1;
159044249214SRandall Stewart 	}
1591f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
159244249214SRandall Stewart 	if ((size_t)chk_length == clen) {
159344249214SRandall Stewart 		/*
159444249214SRandall Stewart 		 * Need to send an abort since we had a empty data chunk.
159544249214SRandall Stewart 		 */
159644249214SRandall Stewart 		op_err = sctp_generate_no_user_data_cause(ch->dp.tsn);
159744249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
159844249214SRandall Stewart 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
159944249214SRandall Stewart 		*abort_flag = 1;
160044249214SRandall Stewart 		return (0);
160144249214SRandall Stewart 	}
1602b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1603b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1604b3f1ea41SRandall Stewart 	}
1605f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
160637f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1607b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1608c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
160980fefe0aSRandall Stewart 	}
1610ad81507eSRandall Stewart 	if (stcb == NULL) {
1611ad81507eSRandall Stewart 		return (0);
1612ad81507eSRandall Stewart 	}
161380fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
161420b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1615f8829a4aSRandall Stewart 		/* It is a duplicate */
1616f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1617f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1618f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1619f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1620f8829a4aSRandall Stewart 			asoc->numduptsns++;
1621f8829a4aSRandall Stewart 		}
1622b201f536SRandall Stewart 		asoc->send_sack = 1;
1623f8829a4aSRandall Stewart 		return (0);
1624f8829a4aSRandall Stewart 	}
1625f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1626d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1627f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1628f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1629f8829a4aSRandall Stewart 		return (0);
1630f8829a4aSRandall Stewart 	}
1631f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1632207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
16330696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1634f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1635f8829a4aSRandall Stewart 			return (0);
1636f8829a4aSRandall Stewart 		}
1637f8829a4aSRandall Stewart 	}
163820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1639f8829a4aSRandall Stewart 		*high_tsn = tsn;
1640f8829a4aSRandall Stewart 	}
1641f8829a4aSRandall Stewart 	/* See if we have received this one already */
164277acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
164377acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1644f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1645f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1646f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1647f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1648f8829a4aSRandall Stewart 			asoc->numduptsns++;
1649f8829a4aSRandall Stewart 		}
165042551e99SRandall Stewart 		asoc->send_sack = 1;
1651f8829a4aSRandall Stewart 		return (0);
1652f8829a4aSRandall Stewart 	}
1653f8829a4aSRandall Stewart 	/*
1654f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1655f8829a4aSRandall Stewart 	 * to be sent up above
1656f8829a4aSRandall Stewart 	 */
1657ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1658f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1659ff1ffd74SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1660f8829a4aSRandall Stewart 		/*
1661f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1662f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1663f8829a4aSRandall Stewart 		 */
1664ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1665a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1666f8829a4aSRandall Stewart 		*abort_flag = 1;
1667f8829a4aSRandall Stewart 		return (0);
1668f8829a4aSRandall Stewart 	}
1669f8829a4aSRandall Stewart 	/*
1670f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1671f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1672f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1673f8829a4aSRandall Stewart 	 */
1674f8829a4aSRandall Stewart 
167544249214SRandall Stewart 	/* Is the stream valid? */
1676f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
167744249214SRandall Stewart 
1678f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
167986eda749SMichael Tuexen 		struct sctp_error_invalid_stream *cause;
1680f8829a4aSRandall Stewart 
168186eda749SMichael Tuexen 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_invalid_stream),
1682eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
168386eda749SMichael Tuexen 		if (op_err != NULL) {
1684f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
168586eda749SMichael Tuexen 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
168686eda749SMichael Tuexen 			cause = mtod(op_err, struct sctp_error_invalid_stream *);
1687f8829a4aSRandall Stewart 			/*
1688f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1689f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1690f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1691f8829a4aSRandall Stewart 			 */
169286eda749SMichael Tuexen 			SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_invalid_stream);
169386eda749SMichael Tuexen 			cause->cause.code = htons(SCTP_CAUSE_INVALID_STREAM);
169486eda749SMichael Tuexen 			cause->cause.length = htons(sizeof(struct sctp_error_invalid_stream));
169586eda749SMichael Tuexen 			cause->stream_id = ch->dp.stream_id;
169686eda749SMichael Tuexen 			cause->reserved = htons(0);
169786eda749SMichael Tuexen 			sctp_queue_op_err(stcb, op_err);
1698f8829a4aSRandall Stewart 		}
1699f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1700207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1701830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
170220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1703830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1704d06c82f1SRandall Stewart 		}
1705d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1706d06c82f1SRandall Stewart 			/* Update cum-ack */
1707d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1708d06c82f1SRandall Stewart 		}
1709f8829a4aSRandall Stewart 		return (0);
1710f8829a4aSRandall Stewart 	}
171144249214SRandall Stewart 	strm = &asoc->strmin[strmno];
1712f8829a4aSRandall Stewart 	/*
171344249214SRandall Stewart 	 * If its a fragmented message, lets see if we can find the control
171444249214SRandall Stewart 	 * on the reassembly queues.
1715f8829a4aSRandall Stewart 	 */
171644249214SRandall Stewart 	if ((chtype == SCTP_IDATA) && ((chunk_flags & SCTP_DATA_FIRST_FRAG) == 0) && (fsn == 0)) {
171744249214SRandall Stewart 		/*
171844249214SRandall Stewart 		 * The first *must* be fsn 0, and other (middle/end) pieces
171944249214SRandall Stewart 		 * can *not* be fsn 0.
172044249214SRandall Stewart 		 */
172144249214SRandall Stewart 		goto err_out;
172244249214SRandall Stewart 	}
172344249214SRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
172444249214SRandall Stewart 		/* See if we can find the re-assembly entity */
172544249214SRandall Stewart 		control = find_reasm_entry(strm, msg_id, ordered, old_data);
172644249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags:0x%x look for control on queues %p\n",
172744249214SRandall Stewart 		    chunk_flags, control);
172844249214SRandall Stewart 		if (control) {
172944249214SRandall Stewart 			/* We found something, does it belong? */
173044249214SRandall Stewart 			if (ordered && (msg_id != control->sinfo_ssn)) {
173144249214SRandall Stewart 		err_out:
173244249214SRandall Stewart 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
173344249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
173444249214SRandall Stewart 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
173544249214SRandall Stewart 				*abort_flag = 1;
173644249214SRandall Stewart 				return (0);
173744249214SRandall Stewart 			}
173844249214SRandall Stewart 			if (ordered && ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED)) {
173944249214SRandall Stewart 				/*
174044249214SRandall Stewart 				 * We can't have a switched order with an
174144249214SRandall Stewart 				 * unordered chunk
174244249214SRandall Stewart 				 */
174344249214SRandall Stewart 				goto err_out;
174444249214SRandall Stewart 			}
174544249214SRandall Stewart 			if (!ordered && (((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) == 0)) {
174644249214SRandall Stewart 				/*
174744249214SRandall Stewart 				 * We can't have a switched unordered with a
174844249214SRandall Stewart 				 * ordered chunk
174944249214SRandall Stewart 				 */
175044249214SRandall Stewart 				goto err_out;
175144249214SRandall Stewart 			}
175244249214SRandall Stewart 		}
175344249214SRandall Stewart 	} else {
175444249214SRandall Stewart 		/*
175544249214SRandall Stewart 		 * Its a complete segment. Lets validate we don't have a
175644249214SRandall Stewart 		 * re-assembly going on with the same Stream/Seq (for
175744249214SRandall Stewart 		 * ordered) or in the same Stream for unordered.
175844249214SRandall Stewart 		 */
175944249214SRandall Stewart 		SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags:0x%x look for msg in case we have dup\n",
176044249214SRandall Stewart 		    chunk_flags);
176144249214SRandall Stewart 		if (find_reasm_entry(strm, msg_id, ordered, old_data)) {
1762f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x dup detected on msg_id: %u\n",
176344249214SRandall Stewart 			    chunk_flags,
176444249214SRandall Stewart 			    msg_id);
176544249214SRandall Stewart 
176644249214SRandall Stewart 			goto err_out;
176744249214SRandall Stewart 		}
176844249214SRandall Stewart 	}
176944249214SRandall Stewart 	/* now do the tests */
177044249214SRandall Stewart 	if (((asoc->cnt_on_all_streams +
177144249214SRandall Stewart 	    asoc->cnt_on_reasm_queue +
177244249214SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
177344249214SRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
177444249214SRandall Stewart 		/*
177544249214SRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
177644249214SRandall Stewart 		 * the reader is doing its job...
177744249214SRandall Stewart 		 */
177844249214SRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
177944249214SRandall Stewart 			/* some to read, wake-up */
178044249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
178144249214SRandall Stewart 			struct socket *so;
178244249214SRandall Stewart 
178344249214SRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
178444249214SRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
178544249214SRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
178644249214SRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
178744249214SRandall Stewart 			SCTP_TCB_LOCK(stcb);
178844249214SRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
178944249214SRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
179044249214SRandall Stewart 				/* assoc was freed while we were unlocked */
179144249214SRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
179244249214SRandall Stewart 				return (0);
179344249214SRandall Stewart 			}
179444249214SRandall Stewart #endif
179544249214SRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
179644249214SRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
179744249214SRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
179844249214SRandall Stewart #endif
179944249214SRandall Stewart 		}
180044249214SRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
180144249214SRandall Stewart 		if (nch == NULL) {
180244249214SRandall Stewart 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
180344249214SRandall Stewart 			    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
180444249214SRandall Stewart 				/* Nope not in the valid range dump it */
180544249214SRandall Stewart 		dump_packet:
180644249214SRandall Stewart 				sctp_set_rwnd(stcb, asoc);
180744249214SRandall Stewart 				if ((asoc->cnt_on_all_streams +
180844249214SRandall Stewart 				    asoc->cnt_on_reasm_queue +
180944249214SRandall Stewart 				    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
181044249214SRandall Stewart 					SCTP_STAT_INCR(sctps_datadropchklmt);
181144249214SRandall Stewart 				} else {
181244249214SRandall Stewart 					SCTP_STAT_INCR(sctps_datadroprwnd);
181344249214SRandall Stewart 				}
181444249214SRandall Stewart 				*break_flag = 1;
181544249214SRandall Stewart 				return (0);
181644249214SRandall Stewart 			}
181744249214SRandall Stewart 		} else {
181844249214SRandall Stewart 			if (control == NULL) {
181944249214SRandall Stewart 				goto dump_packet;
182044249214SRandall Stewart 			}
182144249214SRandall Stewart 			if (SCTP_TSN_GT(fsn, control->top_fsn)) {
182244249214SRandall Stewart 				goto dump_packet;
182344249214SRandall Stewart 			}
182444249214SRandall Stewart 		}
182544249214SRandall Stewart 	}
1826f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
182718e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
182818e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
182918e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
183018e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
183118e198d3SRandall Stewart 	}
1832f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1833f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
183444249214SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = msg_id;
1835f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1836f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
183718e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
183818e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
183918e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1840f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1841f42a358aSRandall Stewart #endif
184244249214SRandall Stewart 	/*
184344249214SRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
184444249214SRandall Stewart 	 * an evil attacker. We can only have Nk chunks based on our TSN
184544249214SRandall Stewart 	 * spread allowed by the mapping array N * 8 bits, so there is no
184644249214SRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
184744249214SRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
184844249214SRandall Stewart 	 */
1849f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1850d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1851f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
185244249214SRandall Stewart 	    SCTP_MSGID_GE(old_data, asoc->strmin[strmno].last_sequence_delivered, msg_id)) {
1853f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1854f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ: %u delivered: %u from peer, Abort!\n",
185544249214SRandall Stewart 		    msg_id, asoc->strmin[strmno].last_sequence_delivered);
1856f8829a4aSRandall Stewart 
1857ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1858ff1ffd74SMichael Tuexen 		    asoc->strmin[strmno].last_sequence_delivered,
185944249214SRandall Stewart 		    tsn, strmno, msg_id);
1860ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1861b7d130beSMichael Tuexen 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1862ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1863f8829a4aSRandall Stewart 		*abort_flag = 1;
1864f8829a4aSRandall Stewart 		return (0);
1865f8829a4aSRandall Stewart 	}
1866f42a358aSRandall Stewart 	/************************************
1867f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1868f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1869f42a358aSRandall Stewart 	 *************************************/
187044249214SRandall Stewart 	if (nch) {
187144249214SRandall Stewart 		the_len = (chk_length - sizeof(struct sctp_idata_chunk));
187244249214SRandall Stewart 	} else {
1873f8829a4aSRandall Stewart 		the_len = (chk_length - sizeof(struct sctp_data_chunk));
187444249214SRandall Stewart 	}
1875f8829a4aSRandall Stewart 	if (last_chunk == 0) {
187644249214SRandall Stewart 		if (nch) {
187744249214SRandall Stewart 			dmbuf = SCTP_M_COPYM(*m,
187844249214SRandall Stewart 			    (offset + sizeof(struct sctp_idata_chunk)),
187944249214SRandall Stewart 			    the_len, M_NOWAIT);
188044249214SRandall Stewart 		} else {
188144b7479bSRandall Stewart 			dmbuf = SCTP_M_COPYM(*m,
1882f8829a4aSRandall Stewart 			    (offset + sizeof(struct sctp_data_chunk)),
1883eb1b1807SGleb Smirnoff 			    the_len, M_NOWAIT);
188444249214SRandall Stewart 		}
1885f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1886b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
18874be807c4SMichael Tuexen 			sctp_log_mbc(dmbuf, SCTP_MBUF_ICOPY);
1888f8829a4aSRandall Stewart 		}
1889f8829a4aSRandall Stewart #endif
1890f8829a4aSRandall Stewart 	} else {
1891f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1892139bc87fSRandall Stewart 		int l_len;
1893139bc87fSRandall Stewart 
1894f8829a4aSRandall Stewart 		dmbuf = *m;
1895f8829a4aSRandall Stewart 		/* lop off the top part */
189644249214SRandall Stewart 		if (nch) {
189744249214SRandall Stewart 			m_adj(dmbuf, (offset + sizeof(struct sctp_idata_chunk)));
189844249214SRandall Stewart 		} else {
1899f8829a4aSRandall Stewart 			m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
190044249214SRandall Stewart 		}
1901139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1902139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1903139bc87fSRandall Stewart 		} else {
1904139bc87fSRandall Stewart 			/*
1905139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1906139bc87fSRandall Stewart 			 * this to often :-0
1907139bc87fSRandall Stewart 			 */
1908139bc87fSRandall Stewart 			struct mbuf *lat;
1909139bc87fSRandall Stewart 
1910139bc87fSRandall Stewart 			l_len = 0;
191160990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1912139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1913139bc87fSRandall Stewart 			}
1914139bc87fSRandall Stewart 		}
1915139bc87fSRandall Stewart 		if (l_len > the_len) {
1916f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1917139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1918f8829a4aSRandall Stewart 		}
1919f8829a4aSRandall Stewart 	}
1920f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1921f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1922f8829a4aSRandall Stewart 		return (0);
1923f8829a4aSRandall Stewart 	}
192444249214SRandall Stewart 	/*
192544249214SRandall Stewart 	 * Now no matter what we need a control, get one if we don't have
192644249214SRandall Stewart 	 * one (we may have gotten it above when we found the message was
192744249214SRandall Stewart 	 * fragmented
192844249214SRandall Stewart 	 */
192944249214SRandall Stewart 	if (control == NULL) {
193044249214SRandall Stewart 		sctp_alloc_a_readq(stcb, control);
193144249214SRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
193244249214SRandall Stewart 		    protocol_id,
193344249214SRandall Stewart 		    strmno, msg_id,
193444249214SRandall Stewart 		    chunk_flags,
193544249214SRandall Stewart 		    NULL, fsn, msg_id);
193644249214SRandall Stewart 		if (control == NULL) {
193744249214SRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
193844249214SRandall Stewart 			return (0);
193944249214SRandall Stewart 		}
194044249214SRandall Stewart 		if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
194144249214SRandall Stewart 			control->data = dmbuf;
194244249214SRandall Stewart 			control->tail_mbuf = NULL;
194344249214SRandall Stewart 			control->end_added = control->last_frag_seen = control->first_frag_seen = 1;
194444249214SRandall Stewart 			control->top_fsn = control->fsn_included = fsn;
194544249214SRandall Stewart 		}
194644249214SRandall Stewart 		created_control = 1;
194744249214SRandall Stewart 	}
1948f2ea2a2dSMichael Tuexen 	SCTPDBG(SCTP_DEBUG_XXX, "chunk_flags: 0x%x ordered: %d msgid: %u control: %p\n",
194944249214SRandall Stewart 	    chunk_flags, ordered, msg_id, control);
1950f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1951f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1952f42a358aSRandall Stewart 	    ((ordered == 0) ||
195344249214SRandall Stewart 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == msg_id &&
1954f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1955f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1956f8829a4aSRandall Stewart 		/*
1957f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1958f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1959f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1960f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1961f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1962f8829a4aSRandall Stewart 		 */
19631ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
196420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
19651ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
19661ea735c8SMichael Tuexen 		}
1967f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_XXX, "Injecting control: %p to be read (msg_id: %u)\n",
196844249214SRandall Stewart 		    control, msg_id);
196944249214SRandall Stewart 
1970cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1971cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1972cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1973830d754dSRandall Stewart 
1974f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1975f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
197644249214SRandall Stewart 			strm->last_sequence_delivered++;
1977f8829a4aSRandall Stewart 		}
1978f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1979b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
198044249214SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, msg_id, strmno,
1981f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
198280fefe0aSRandall Stewart 		}
1983f8829a4aSRandall Stewart 		control = NULL;
1984f8829a4aSRandall Stewart 		goto finish_express_del;
1985f8829a4aSRandall Stewart 	}
198644249214SRandall Stewart 	/* Now will we need a chunk too? */
1987f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1988f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1989f8829a4aSRandall Stewart 		if (chk == NULL) {
1990f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1991f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1992f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1993f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1994f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1995f8829a4aSRandall Stewart 			}
1996f8829a4aSRandall Stewart 			return (0);
1997f8829a4aSRandall Stewart 		}
1998f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1999f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
200044249214SRandall Stewart 		chk->rec.data.fsn_num = fsn;
200144249214SRandall Stewart 		chk->rec.data.stream_seq = msg_id;
2002f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
2003f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
2004f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
2005f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
2006f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
2007f8829a4aSRandall Stewart 		chk->asoc = asoc;
2008f8829a4aSRandall Stewart 		chk->send_size = the_len;
2009f8829a4aSRandall Stewart 		chk->whoTo = net;
2010f2ea2a2dSMichael Tuexen 		SCTPDBG(SCTP_DEBUG_XXX, "Building ck: %p for control: %p to be read (msg_id: %u)\n",
201144249214SRandall Stewart 		    chk,
201244249214SRandall Stewart 		    control, msg_id);
2013f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
2014f8829a4aSRandall Stewart 		chk->data = dmbuf;
201544249214SRandall Stewart 	}
201644249214SRandall Stewart 	/* Set the appropriate TSN mark */
201744249214SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
201844249214SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
201944249214SRandall Stewart 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
202044249214SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
202144249214SRandall Stewart 		}
2022f8829a4aSRandall Stewart 	} else {
202344249214SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
202444249214SRandall Stewart 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
202544249214SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
2026f8829a4aSRandall Stewart 		}
2027f8829a4aSRandall Stewart 	}
202844249214SRandall Stewart 	/* Now is it complete (i.e. not fragmented)? */
202944249214SRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
2030f8829a4aSRandall Stewart 		/*
203144249214SRandall Stewart 		 * Special check for when streams are resetting. We could be
203244249214SRandall Stewart 		 * more smart about this and check the actual stream to see
203344249214SRandall Stewart 		 * if it is not being reset.. that way we would not create a
203444249214SRandall Stewart 		 * HOLB when amongst streams being reset and those not being
203544249214SRandall Stewart 		 * reset.
2036f8829a4aSRandall Stewart 		 *
2037f8829a4aSRandall Stewart 		 */
2038f8829a4aSRandall Stewart 		if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
203920b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, liste->tsn)) {
2040f8829a4aSRandall Stewart 			/*
204144249214SRandall Stewart 			 * yep its past where we need to reset... go ahead
204244249214SRandall Stewart 			 * and queue it.
2043f8829a4aSRandall Stewart 			 */
2044f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2045f8829a4aSRandall Stewart 				/* first one on */
2046f8829a4aSRandall Stewart 				TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2047f8829a4aSRandall Stewart 			} else {
204844249214SRandall Stewart 				struct sctp_queued_to_read *ctlOn, *nctlOn;
2049f8829a4aSRandall Stewart 				unsigned char inserted = 0;
2050f8829a4aSRandall Stewart 
20514a9ef3f8SMichael Tuexen 				TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
205220b07a4dSMichael Tuexen 					if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
205344249214SRandall Stewart 
20544a9ef3f8SMichael Tuexen 						continue;
2055f8829a4aSRandall Stewart 					} else {
2056f8829a4aSRandall Stewart 						/* found it */
2057f8829a4aSRandall Stewart 						TAILQ_INSERT_BEFORE(ctlOn, control, next);
2058f8829a4aSRandall Stewart 						inserted = 1;
2059f8829a4aSRandall Stewart 						break;
2060f8829a4aSRandall Stewart 					}
2061f8829a4aSRandall Stewart 				}
2062f8829a4aSRandall Stewart 				if (inserted == 0) {
2063f8829a4aSRandall Stewart 					/*
206444249214SRandall Stewart 					 * must be put at end, use prevP
206544249214SRandall Stewart 					 * (all setup from loop) to setup
206644249214SRandall Stewart 					 * nextP.
2067f8829a4aSRandall Stewart 					 */
2068f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2069f8829a4aSRandall Stewart 				}
2070f8829a4aSRandall Stewart 			}
207144249214SRandall Stewart 			goto finish_express_del;
207244249214SRandall Stewart 		}
207344249214SRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
207444249214SRandall Stewart 			/* queue directly into socket buffer */
2075f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "Unordered data to be read control: %p msg_id: %u\n",
207644249214SRandall Stewart 			    control, msg_id);
207744249214SRandall Stewart 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
207844249214SRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
207944249214SRandall Stewart 			    control,
208044249214SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1,
208144249214SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
208244249214SRandall Stewart 
2083f8829a4aSRandall Stewart 		} else {
2084f2ea2a2dSMichael Tuexen 			SCTPDBG(SCTP_DEBUG_XXX, "Queue control: %p for reordering msg_id: %u\n", control,
208544249214SRandall Stewart 			    msg_id);
208644249214SRandall Stewart 			sctp_queue_data_to_stream(stcb, strm, asoc, control, abort_flag, &need_reasm_check);
2087f8829a4aSRandall Stewart 			if (*abort_flag) {
20888be0fd55SMichael Tuexen 				if (last_chunk) {
20898be0fd55SMichael Tuexen 					*m = NULL;
20908be0fd55SMichael Tuexen 				}
2091f8829a4aSRandall Stewart 				return (0);
2092f8829a4aSRandall Stewart 			}
2093f8829a4aSRandall Stewart 		}
209444249214SRandall Stewart 		goto finish_express_del;
2095f8829a4aSRandall Stewart 	}
209644249214SRandall Stewart 	/* If we reach here its a reassembly */
209744249214SRandall Stewart 	need_reasm_check = 1;
209844249214SRandall Stewart 	SCTPDBG(SCTP_DEBUG_XXX,
2099f2ea2a2dSMichael Tuexen 	    "Queue data to stream for reasm control: %p msg_id: %u\n",
210044249214SRandall Stewart 	    control, msg_id);
210144249214SRandall Stewart 	sctp_queue_data_for_reasm(stcb, asoc, strm, control, chk, created_control, abort_flag, tsn);
2102f8829a4aSRandall Stewart 	if (*abort_flag) {
2103a5d547adSRandall Stewart 		/*
210444249214SRandall Stewart 		 * the assoc is now gone and chk was put onto the reasm
210544249214SRandall Stewart 		 * queue, which has all been freed.
2106a5d547adSRandall Stewart 		 */
21078be0fd55SMichael Tuexen 		if (last_chunk) {
2108a5d547adSRandall Stewart 			*m = NULL;
21098be0fd55SMichael Tuexen 		}
2110f8829a4aSRandall Stewart 		return (0);
2111f8829a4aSRandall Stewart 	}
2112f8829a4aSRandall Stewart finish_express_del:
211344249214SRandall Stewart 	/* Here we tidy up things */
2114307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
2115307b49efSMichael Tuexen 		/* Update cum-ack */
2116307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
2117307b49efSMichael Tuexen 	}
2118f8829a4aSRandall Stewart 	if (last_chunk) {
2119f8829a4aSRandall Stewart 		*m = NULL;
2120f8829a4aSRandall Stewart 	}
2121f42a358aSRandall Stewart 	if (ordered) {
2122f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2123f8829a4aSRandall Stewart 	} else {
2124f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2125f8829a4aSRandall Stewart 	}
2126f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2127f8829a4aSRandall Stewart 	/* Set it present please */
2128b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
212944249214SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, msg_id, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
213080fefe0aSRandall Stewart 	}
2131b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2132f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2133f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
213480fefe0aSRandall Stewart 	}
213517205eccSRandall Stewart 	/* check the special flag for stream resets */
213617205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
213720b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
213817205eccSRandall Stewart 		/*
213917205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
214017205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
214117205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
214217205eccSRandall Stewart 		 * pending_reply_queue.
214317205eccSRandall Stewart 		 */
21444a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
214517205eccSRandall Stewart 
2146a169d6ecSMichael Tuexen 		sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
214717205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
21487cca1775SRandall Stewart 		sctp_send_deferred_reset_response(stcb, liste, SCTP_STREAM_RESET_RESULT_PERFORMED);
2149207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21503c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
215117205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
21524a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
215317205eccSRandall Stewart 			/* All can be removed */
21544a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
215517205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
215644249214SRandall Stewart 				sctp_queue_data_to_stream(stcb, strm, asoc, ctl, abort_flag, &need_reasm_check);
215717205eccSRandall Stewart 				if (*abort_flag) {
215817205eccSRandall Stewart 					return (0);
215917205eccSRandall Stewart 				}
216017205eccSRandall Stewart 			}
21614a9ef3f8SMichael Tuexen 		} else {
21624a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
216320b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
21644a9ef3f8SMichael Tuexen 					break;
21654a9ef3f8SMichael Tuexen 				}
216617205eccSRandall Stewart 				/*
216717205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
216817205eccSRandall Stewart 				 * process it which is the NOT of
216917205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
217017205eccSRandall Stewart 				 */
217117205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
217244249214SRandall Stewart 				sctp_queue_data_to_stream(stcb, strm, asoc, ctl, abort_flag, &need_reasm_check);
217317205eccSRandall Stewart 				if (*abort_flag) {
217417205eccSRandall Stewart 					return (0);
217517205eccSRandall Stewart 				}
217617205eccSRandall Stewart 			}
217717205eccSRandall Stewart 		}
217817205eccSRandall Stewart 		/*
217917205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
218017205eccSRandall Stewart 		 * held on reassembly queue?
218117205eccSRandall Stewart 		 */
218244249214SRandall Stewart 		(void)sctp_deliver_reasm_check(stcb, asoc, strm);
218317205eccSRandall Stewart 		need_reasm_check = 0;
218417205eccSRandall Stewart 	}
2185139bc87fSRandall Stewart 	if (need_reasm_check) {
2186139bc87fSRandall Stewart 		/* Another one waits ? */
218744249214SRandall Stewart 		(void)sctp_deliver_reasm_check(stcb, asoc, strm);
2188139bc87fSRandall Stewart 	}
2189f8829a4aSRandall Stewart 	return (1);
2190f8829a4aSRandall Stewart }
2191f8829a4aSRandall Stewart 
2192ed654363SMichael Tuexen static const int8_t sctp_map_lookup_tab[256] = {
2193b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2194b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2195b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2196b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2197b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2198b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2199b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2200b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2201b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2202b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2203b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2204b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2205b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2206b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2207b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2208b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
2209b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2210b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2211b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2212b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2213b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2214b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2215b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2216b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2217b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2218b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2219b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2220b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2221b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2222b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2223b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2224b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
2225f8829a4aSRandall Stewart };
2226f8829a4aSRandall Stewart 
2227f8829a4aSRandall Stewart 
2228f8829a4aSRandall Stewart void
2229b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2230f8829a4aSRandall Stewart {
2231f8829a4aSRandall Stewart 	/*
2232f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2233f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
223466bd30bdSRandall Stewart 	 *
223566bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
2236cd0a4ff6SPedro F. Giffuni 	 * make up the position of the cum-ack would be in the nr-mapping
223766bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
223866bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
223966bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
224066bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
224166bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
2242f8829a4aSRandall Stewart 	 */
2243f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2244b3f1ea41SRandall Stewart 	int at;
224566bd30bdSRandall Stewart 	uint8_t val;
2246f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
224777acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2248f8829a4aSRandall Stewart 
2249f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2250f8829a4aSRandall Stewart 
2251f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2252f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2253f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2254f8829a4aSRandall Stewart 	/*
2255f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2256f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2257f8829a4aSRandall Stewart 	 */
2258f8829a4aSRandall Stewart 	at = 0;
2259b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
226066bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
226166bd30bdSRandall Stewart 		if (val == 0xff) {
2262f8829a4aSRandall Stewart 			at += 8;
2263f8829a4aSRandall Stewart 		} else {
2264f8829a4aSRandall Stewart 			/* there is a 0 bit */
226566bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2266f8829a4aSRandall Stewart 			break;
2267f8829a4aSRandall Stewart 		}
2268f8829a4aSRandall Stewart 	}
2269b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2270f8829a4aSRandall Stewart 
227120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
227220b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2273a5d547adSRandall Stewart #ifdef INVARIANTS
2274ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2275ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2276f8829a4aSRandall Stewart #else
2277ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2278ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
22790e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2280b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2281b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2282b3f1ea41SRandall Stewart 		}
2283f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2284830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2285f8829a4aSRandall Stewart #endif
2286f8829a4aSRandall Stewart 	}
228720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
228877acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
228977acdc25SRandall Stewart 	} else {
229077acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
229177acdc25SRandall Stewart 	}
229277acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2293f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
229477acdc25SRandall Stewart 		/* highest becomes the cum-ack */
229537f144ebSMichael Tuexen 		int clr;
229637f144ebSMichael Tuexen 
229737f144ebSMichael Tuexen #ifdef INVARIANTS
229837f144ebSMichael Tuexen 		unsigned int i;
229937f144ebSMichael Tuexen 
230037f144ebSMichael Tuexen #endif
2301f8829a4aSRandall Stewart 
2302f8829a4aSRandall Stewart 		/* clear the array */
2303b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2304c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2305f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2306f8829a4aSRandall Stewart 		}
2307f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2308830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
230937f144ebSMichael Tuexen #ifdef INVARIANTS
2310b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2311b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2312cd3fd531SMichael Tuexen 				SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2313b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2314b5c16493SMichael Tuexen 			}
2315b5c16493SMichael Tuexen 		}
231637f144ebSMichael Tuexen #endif
231777acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
231877acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2319f8829a4aSRandall Stewart 	} else if (at >= 8) {
2320f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2321b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2322b3f1ea41SRandall Stewart 
2323f8829a4aSRandall Stewart 		/*
2324f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2325f8829a4aSRandall Stewart 		 * TSN value
2326f8829a4aSRandall Stewart 		 */
232777acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
232877acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2329f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
233077acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2331d55b0b1bSRandall Stewart #ifdef INVARIANTS
2332f8829a4aSRandall Stewart 			panic("impossible slide");
2333d55b0b1bSRandall Stewart #else
2334cd3fd531SMichael Tuexen 			SCTP_PRINTF("impossible slide lgap: %x slide_end: %x slide_from: %x? at: %d\n",
233577acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2336d55b0b1bSRandall Stewart 			return;
2337d55b0b1bSRandall Stewart #endif
2338f8829a4aSRandall Stewart 		}
2339b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2340b3f1ea41SRandall Stewart #ifdef INVARIANTS
2341b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2342b3f1ea41SRandall Stewart #else
2343cd3fd531SMichael Tuexen 			SCTP_PRINTF("Gak, would have overrun map end: %d slide_end: %d\n",
2344b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2345b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2346b3f1ea41SRandall Stewart #endif
2347b3f1ea41SRandall Stewart 		}
2348f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2349b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2350f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2351f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2352f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2353f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
235480fefe0aSRandall Stewart 		}
2355f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2356f8829a4aSRandall Stewart 		    distance < 0) {
2357f8829a4aSRandall Stewart 			/*
2358f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2359f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2360f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2361f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2362f8829a4aSRandall Stewart 			 */
2363f8829a4aSRandall Stewart 
2364b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2365f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2366f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2367f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
236880fefe0aSRandall Stewart 			}
2369f8829a4aSRandall Stewart 		} else {
2370f8829a4aSRandall Stewart 			int ii;
2371f8829a4aSRandall Stewart 
2372f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
237337f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
237437f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
237577acdc25SRandall Stewart 
2376f8829a4aSRandall Stewart 			}
2377aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2378f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
237977acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2380f8829a4aSRandall Stewart 			}
2381ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2382ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2383ee94f0a2SMichael Tuexen 			}
2384ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2385ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2386ee94f0a2SMichael Tuexen 			}
2387f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2388b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2389f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2390f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2391f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
239280fefe0aSRandall Stewart 			}
2393830d754dSRandall Stewart 		}
2394830d754dSRandall Stewart 	}
2395b5c16493SMichael Tuexen }
2396b5c16493SMichael Tuexen 
2397b5c16493SMichael Tuexen void
23987215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2399b5c16493SMichael Tuexen {
2400b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2401b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2402b5c16493SMichael Tuexen 
2403b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
240420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2405b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2406b5c16493SMichael Tuexen 	} else {
2407b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2408b5c16493SMichael Tuexen 	}
2409b5c16493SMichael Tuexen 
2410830d754dSRandall Stewart 	/*
2411f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2412f8829a4aSRandall Stewart 	 * timer (if allowed).
2413f8829a4aSRandall Stewart 	 */
2414f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2415f8829a4aSRandall Stewart 		/*
2416b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2417b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2418b5c16493SMichael Tuexen 		 * SACK
2419f8829a4aSRandall Stewart 		 */
2420139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2421f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2422b7d130beSMichael Tuexen 			    stcb->sctp_ep, stcb, NULL,
242344249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2424f8829a4aSRandall Stewart 		}
2425ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2426ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2427689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2428f8829a4aSRandall Stewart 	} else {
2429f8829a4aSRandall Stewart 		int is_a_gap;
2430f8829a4aSRandall Stewart 
2431f8829a4aSRandall Stewart 		/* is there a gap now ? */
243220b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2433f8829a4aSRandall Stewart 
2434f8829a4aSRandall Stewart 		/*
2435b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2436b5c16493SMichael Tuexen 		 * since last ack
2437f8829a4aSRandall Stewart 		 */
2438f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2439f8829a4aSRandall Stewart 
244042551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
244142551e99SRandall Stewart 							 * SACK */
2442f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2443f8829a4aSRandall Stewart 							 * longer is one */
2444f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2445f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
244642551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
244742551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2448f8829a4aSRandall Stewart 		    ) {
2449f8829a4aSRandall Stewart 
24507c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2451b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
245242551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2453f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2454f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2455139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2456f8829a4aSRandall Stewart 
2457f8829a4aSRandall Stewart 				/*
2458b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2459b5c16493SMichael Tuexen 				 * even in the face of
2460f8829a4aSRandall Stewart 				 *
2461b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2462b5c16493SMichael Tuexen 				 * have to be sent because of the above
2463b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2464b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2465b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2466f8829a4aSRandall Stewart 				 * the delayed ack timer.
2467f8829a4aSRandall Stewart 				 */
2468f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2469f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2470f8829a4aSRandall Stewart 			} else {
2471f8829a4aSRandall Stewart 				/*
2472b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2473b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2474b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2475f8829a4aSRandall Stewart 				 */
2476ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2477689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2478f8829a4aSRandall Stewart 			}
2479f8829a4aSRandall Stewart 		} else {
248042551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2481f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2482f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2483f8829a4aSRandall Stewart 			}
2484f8829a4aSRandall Stewart 		}
2485f8829a4aSRandall Stewart 	}
2486f8829a4aSRandall Stewart }
2487f8829a4aSRandall Stewart 
2488f8829a4aSRandall Stewart int
2489f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2490e7e71dd7SMichael Tuexen     struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2491e7e71dd7SMichael Tuexen     struct sctp_nets *net, uint32_t * high_tsn)
2492f8829a4aSRandall Stewart {
249344249214SRandall Stewart 	struct sctp_chunkhdr *ch, chunk_buf;
2494f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2495f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2496f8829a4aSRandall Stewart 	int stop_proc = 0;
2497f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
24988f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2499f8829a4aSRandall Stewart 	struct mbuf *m;
25008f777478SMichael Tuexen 	uint32_t highest_tsn;
2501f8829a4aSRandall Stewart 
2502f8829a4aSRandall Stewart 	/* set the rwnd */
2503f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2504f8829a4aSRandall Stewart 
2505f8829a4aSRandall Stewart 	m = *mm;
2506f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2507f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
250820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
25098f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
25108f777478SMichael Tuexen 	} else {
25118f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2512f8829a4aSRandall Stewart 	}
251320b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2514f8829a4aSRandall Stewart 	/*
2515f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2516f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2517f8829a4aSRandall Stewart 	 * chunk is assigned.
2518f8829a4aSRandall Stewart 	 */
2519f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2520f8829a4aSRandall Stewart 
2521d06c82f1SRandall Stewart 	/*-
2522f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2523f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2524f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2525f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2526d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2527d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2528f8829a4aSRandall Stewart 	 */
252944b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2530f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2531eb1b1807SGleb Smirnoff 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2532f8829a4aSRandall Stewart 		if (m) {
2533f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2534f8829a4aSRandall Stewart 			caddr_t *from, *to;
2535f8829a4aSRandall Stewart 
2536f8829a4aSRandall Stewart 			/* get the pointers and copy */
2537f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2538f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2539139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2540f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2541139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2542f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2543cd0a4ff6SPedro F. Giffuni 			/* success, back copy */
2544f8829a4aSRandall Stewart 			*mm = m;
2545f8829a4aSRandall Stewart 		} else {
2546f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2547f8829a4aSRandall Stewart 			m = *mm;
2548f8829a4aSRandall Stewart 		}
2549f8829a4aSRandall Stewart 	}
2550f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
255144249214SRandall Stewart 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
255244249214SRandall Stewart 	    sizeof(struct sctp_chunkhdr), (uint8_t *) & chunk_buf);
2553f8829a4aSRandall Stewart 	if (ch == NULL) {
2554f8829a4aSRandall Stewart 		return (1);
2555f8829a4aSRandall Stewart 	}
2556f8829a4aSRandall Stewart 	/*
2557f8829a4aSRandall Stewart 	 * process all DATA chunks...
2558f8829a4aSRandall Stewart 	 */
2559f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2560f8829a4aSRandall Stewart 	break_flag = 0;
256142551e99SRandall Stewart 	asoc->data_pkts_seen++;
2562f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2563f8829a4aSRandall Stewart 		/* validate chunk length */
256444249214SRandall Stewart 		chk_length = ntohs(ch->chunk_length);
2565f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2566f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2567f8829a4aSRandall Stewart 			stop_proc = 1;
256860990c0cSMichael Tuexen 			continue;
2569f8829a4aSRandall Stewart 		}
257044249214SRandall Stewart 		if ((asoc->idata_supported == 1) &&
257144249214SRandall Stewart 		    (ch->chunk_type == SCTP_DATA)) {
257244249214SRandall Stewart 			struct mbuf *op_err;
257344249214SRandall Stewart 			char msg[SCTP_DIAG_INFO_LEN];
257444249214SRandall Stewart 
2575e7f232a0SMichael Tuexen 			snprintf(msg, sizeof(msg), "%s", "I-DATA chunk received when DATA was negotiated");
257644249214SRandall Stewart 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
257744249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_18;
257844249214SRandall Stewart 			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
257944249214SRandall Stewart 			return (2);
258044249214SRandall Stewart 		}
258144249214SRandall Stewart 		if ((asoc->idata_supported == 0) &&
258244249214SRandall Stewart 		    (ch->chunk_type == SCTP_IDATA)) {
258344249214SRandall Stewart 			struct mbuf *op_err;
258444249214SRandall Stewart 			char msg[SCTP_DIAG_INFO_LEN];
258544249214SRandall Stewart 
2586e7f232a0SMichael Tuexen 			snprintf(msg, sizeof(msg), "%s", "DATA chunk received when I-DATA was negotiated");
258744249214SRandall Stewart 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
258844249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
258944249214SRandall Stewart 			sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
259044249214SRandall Stewart 			return (2);
259144249214SRandall Stewart 		}
259244249214SRandall Stewart 		if ((ch->chunk_type == SCTP_DATA) ||
259344249214SRandall Stewart 		    (ch->chunk_type == SCTP_IDATA)) {
259444249214SRandall Stewart 			int clen;
259544249214SRandall Stewart 
259644249214SRandall Stewart 			if (ch->chunk_type == SCTP_DATA) {
259744249214SRandall Stewart 				clen = sizeof(struct sctp_data_chunk);
259844249214SRandall Stewart 			} else {
259944249214SRandall Stewart 				clen = sizeof(struct sctp_idata_chunk);
260044249214SRandall Stewart 			}
2601f8ee69bfSMichael Tuexen 			if (chk_length < clen) {
2602f8829a4aSRandall Stewart 				/*
2603f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2604f8829a4aSRandall Stewart 				 * invalid data chunk.
2605f8829a4aSRandall Stewart 				 */
2606f8829a4aSRandall Stewart 				struct mbuf *op_err;
2607ff1ffd74SMichael Tuexen 				char msg[SCTP_DIAG_INFO_LEN];
2608f8829a4aSRandall Stewart 
2609ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "DATA chunk of length %d",
2610ff1ffd74SMichael Tuexen 				    chk_length);
2611ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
261244249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_20;
2613e7e71dd7SMichael Tuexen 				sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
261432451da4SMichael Tuexen 				return (2);
261532451da4SMichael Tuexen 			}
2616f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2617f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2618f8829a4aSRandall Stewart #endif
2619f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2620f8829a4aSRandall Stewart 				last_chunk = 1;
2621f8829a4aSRandall Stewart 			} else {
2622f8829a4aSRandall Stewart 				last_chunk = 0;
2623f8829a4aSRandall Stewart 			}
262444249214SRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset,
2625f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
262644249214SRandall Stewart 			    last_chunk, ch->chunk_type)) {
2627f8829a4aSRandall Stewart 				num_chunks++;
2628f8829a4aSRandall Stewart 			}
2629f8829a4aSRandall Stewart 			if (abort_flag)
2630f8829a4aSRandall Stewart 				return (2);
2631f8829a4aSRandall Stewart 
2632f8829a4aSRandall Stewart 			if (break_flag) {
2633f8829a4aSRandall Stewart 				/*
2634f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2635f8829a4aSRandall Stewart 				 * drop rep space left.
2636f8829a4aSRandall Stewart 				 */
2637f8829a4aSRandall Stewart 				stop_proc = 1;
263860990c0cSMichael Tuexen 				continue;
2639f8829a4aSRandall Stewart 			}
2640f8829a4aSRandall Stewart 		} else {
2641f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
264244249214SRandall Stewart 			switch (ch->chunk_type) {
2643f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2644f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2645f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
264660990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2647f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2648f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2649f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2650f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2651f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2652f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2653f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2654f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2655f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2656f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2657f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2658f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2659f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2660f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2661f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2662f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2663f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2664fd60718dSMichael Tuexen 				{
2665f8829a4aSRandall Stewart 					/*
2666fd60718dSMichael Tuexen 					 * Now, what do we do with KNOWN
2667fd60718dSMichael Tuexen 					 * chunks that are NOT in the right
2668fd60718dSMichael Tuexen 					 * place?
2669f8829a4aSRandall Stewart 					 *
2670fd60718dSMichael Tuexen 					 * For now, I do nothing but ignore
2671fd60718dSMichael Tuexen 					 * them. We may later want to add
2672fd60718dSMichael Tuexen 					 * sysctl stuff to switch out and do
2673fd60718dSMichael Tuexen 					 * either an ABORT() or possibly
2674fd60718dSMichael Tuexen 					 * process them.
2675f8829a4aSRandall Stewart 					 */
2676f8829a4aSRandall Stewart 					struct mbuf *op_err;
2677267dbe63SMichael Tuexen 					char msg[SCTP_DIAG_INFO_LEN];
2678f8829a4aSRandall Stewart 
26799ae56375SMichael Tuexen 					snprintf(msg, sizeof(msg), "DATA chunk followed by chunk of type %2.2x",
268044249214SRandall Stewart 					    ch->chunk_type);
2681267dbe63SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2682e7e71dd7SMichael Tuexen 					sctp_abort_an_association(inp, stcb, op_err, SCTP_SO_NOT_LOCKED);
2683f8829a4aSRandall Stewart 					return (2);
2684f8829a4aSRandall Stewart 				}
2685f8829a4aSRandall Stewart 			default:
2686f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
268744249214SRandall Stewart 				if (ch->chunk_type & 0x40) {
2688f8829a4aSRandall Stewart 					/* Add a error report to the queue */
268986eda749SMichael Tuexen 					struct mbuf *op_err;
269086eda749SMichael Tuexen 					struct sctp_gen_error_cause *cause;
2691f8829a4aSRandall Stewart 
269286eda749SMichael Tuexen 					op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
269386eda749SMichael Tuexen 					    0, M_NOWAIT, 1, MT_DATA);
269486eda749SMichael Tuexen 					if (op_err != NULL) {
269586eda749SMichael Tuexen 						cause = mtod(op_err, struct sctp_gen_error_cause *);
269686eda749SMichael Tuexen 						cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
26979a8e3088SMichael Tuexen 						cause->length = htons((uint16_t) (chk_length + sizeof(struct sctp_gen_error_cause)));
269886eda749SMichael Tuexen 						SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
269986eda749SMichael Tuexen 						SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
270086eda749SMichael Tuexen 						if (SCTP_BUF_NEXT(op_err) != NULL) {
270186eda749SMichael Tuexen 							sctp_queue_op_err(stcb, op_err);
2702f8829a4aSRandall Stewart 						} else {
270386eda749SMichael Tuexen 							sctp_m_freem(op_err);
2704f8829a4aSRandall Stewart 						}
2705f8829a4aSRandall Stewart 					}
2706f8829a4aSRandall Stewart 				}
270744249214SRandall Stewart 				if ((ch->chunk_type & 0x80) == 0) {
2708f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2709f8829a4aSRandall Stewart 					stop_proc = 1;
2710f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2711f8829a4aSRandall Stewart 					 * continue... */
2712f8829a4aSRandall Stewart 				break;
271360990c0cSMichael Tuexen 			}	/* switch of chunk type */
2714f8829a4aSRandall Stewart 		}
2715f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2716f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2717f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2718f8829a4aSRandall Stewart 			stop_proc = 1;
2719f8829a4aSRandall Stewart 			continue;
2720f8829a4aSRandall Stewart 		}
272144249214SRandall Stewart 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
272244249214SRandall Stewart 		    sizeof(struct sctp_chunkhdr), (uint8_t *) & chunk_buf);
2723f8829a4aSRandall Stewart 		if (ch == NULL) {
2724f8829a4aSRandall Stewart 			*offset = length;
2725f8829a4aSRandall Stewart 			stop_proc = 1;
272660990c0cSMichael Tuexen 			continue;
2727f8829a4aSRandall Stewart 		}
272860990c0cSMichael Tuexen 	}
2729f8829a4aSRandall Stewart 	if (break_flag) {
2730f8829a4aSRandall Stewart 		/*
2731f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2732f8829a4aSRandall Stewart 		 */
273320cc2188SMichael Tuexen 		sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2734f8829a4aSRandall Stewart 	}
2735f8829a4aSRandall Stewart 	if (num_chunks) {
2736f8829a4aSRandall Stewart 		/*
2737ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2738f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2739f8829a4aSRandall Stewart 		 */
2740f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2741b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2742c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2743c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2744c4739e2fSRandall Stewart 			    0,
2745c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2746c4739e2fSRandall Stewart 			    __LINE__);
2747c4739e2fSRandall Stewart 		}
2748f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
27496e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2750f8829a4aSRandall Stewart 	}
2751f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2752f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
275342551e99SRandall Stewart 		/* Assure that we ack right away */
275442551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2755f8829a4aSRandall Stewart 	}
2756f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
27577215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2758f8829a4aSRandall Stewart 	return (0);
2759f8829a4aSRandall Stewart }
2760f8829a4aSRandall Stewart 
27610fa753b3SRandall Stewart static int
27620fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
27630fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
27640fa753b3SRandall Stewart     int *num_frs,
27650fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
27660fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
27677215cc1bSMichael Tuexen     int *rto_ok)
27680fa753b3SRandall Stewart {
27690fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
27700fa753b3SRandall Stewart 	unsigned int theTSN;
2771b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
27720fa753b3SRandall Stewart 
27730fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
27740fa753b3SRandall Stewart 	tp1 = *p_tp1;
27750fa753b3SRandall Stewart 	if (tp1 == NULL) {
27760fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
27770fa753b3SRandall Stewart 	}
27780fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
27790fa753b3SRandall Stewart 		theTSN = j + last_tsn;
27800fa753b3SRandall Stewart 		while (tp1) {
27810fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
27820fa753b3SRandall Stewart 				(*num_frs) += 1;
27830fa753b3SRandall Stewart 
27840fa753b3SRandall Stewart 			/*-
27850fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
27860fa753b3SRandall Stewart 			 * processed from the sent queue, track the
27870fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
27880fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
27890fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
27900fa753b3SRandall Stewart 			 * and retransmissions.
27910fa753b3SRandall Stewart 			 */
27928427b3fdSMichael Tuexen 			if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
27938427b3fdSMichael Tuexen 			    (tp1->whoTo->find_pseudo_cumack == 1) &&
27940fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
27950fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
27960fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
27970fa753b3SRandall Stewart 			}
27988427b3fdSMichael Tuexen 			if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
27998427b3fdSMichael Tuexen 			    (tp1->whoTo->find_rtx_pseudo_cumack == 1) &&
28000fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28010fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28020fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
28030fa753b3SRandall Stewart 			}
28040fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
28050fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
28060fa753b3SRandall Stewart 					/*-
28070fa753b3SRandall Stewart 					 * must be held until
28080fa753b3SRandall Stewart 					 * cum-ack passes
28090fa753b3SRandall Stewart 					 */
28100fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
28110fa753b3SRandall Stewart 						/*-
28120fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
28130fa753b3SRandall Stewart 						 * now no-longer in flight.
28140fa753b3SRandall Stewart 						 * Higher values may already be set
28150fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
28160fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
28170fa753b3SRandall Stewart 						 */
281820b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
281920b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
28200fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
28210fa753b3SRandall Stewart 						}
28220fa753b3SRandall Stewart 						/*-
28230fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
28240fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
28250fa753b3SRandall Stewart 						 * newly acked. update
28260fa753b3SRandall Stewart 						 * this_sack_highest_newack if
28270fa753b3SRandall Stewart 						 * appropriate.
28280fa753b3SRandall Stewart 						 */
28290fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
28300fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
28310fa753b3SRandall Stewart 
283220b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
283320b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
28340fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
28350fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
28360fa753b3SRandall Stewart 						}
28370fa753b3SRandall Stewart 						/*-
28380fa753b3SRandall Stewart 						 * CMT DAC algo: also update
28390fa753b3SRandall Stewart 						 * this_sack_lowest_newack
28400fa753b3SRandall Stewart 						 */
28410fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
28420fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28430fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
28440fa753b3SRandall Stewart 								    last_tsn,
28450fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
28460fa753b3SRandall Stewart 								    0,
28470fa753b3SRandall Stewart 								    0,
28480fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
28490fa753b3SRandall Stewart 							}
28500fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
28510fa753b3SRandall Stewart 						}
28520fa753b3SRandall Stewart 						/*-
28530fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
28540fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
28550fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
28560fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
28570fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
28580fa753b3SRandall Stewart 						 * retransmissions.
28590fa753b3SRandall Stewart 						 */
28600fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
28610fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
28620fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
28630fa753b3SRandall Stewart 							}
28640fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
28650fa753b3SRandall Stewart 						}
28660fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
28670fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
28680fa753b3SRandall Stewart 						}
28690fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
28700fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
28710fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
28720fa753b3SRandall Stewart 							}
28730fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
28740fa753b3SRandall Stewart 						}
28750fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28760fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
28770fa753b3SRandall Stewart 							    last_tsn,
28780fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
28790fa753b3SRandall Stewart 							    frag_strt,
28800fa753b3SRandall Stewart 							    frag_end,
28810fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
28820fa753b3SRandall Stewart 						}
28830fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
28840fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
28850fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
28860fa753b3SRandall Stewart 							    tp1->book_size,
28879a8e3088SMichael Tuexen 							    (uint32_t) (uintptr_t) tp1->whoTo,
28880fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
28890fa753b3SRandall Stewart 						}
28900fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2891299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2892299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2893299108c5SRandall Stewart 							    tp1);
2894299108c5SRandall Stewart 						}
28950fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
28960fa753b3SRandall Stewart 
28970fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
28980fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
28990fa753b3SRandall Stewart 							/*-
29000fa753b3SRandall Stewart 							 * True non-retransmited chunk
29010fa753b3SRandall Stewart 							 */
29020fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
29030fa753b3SRandall Stewart 
29040fa753b3SRandall Stewart 							/*-
29050fa753b3SRandall Stewart 							 * update RTO too ?
29060fa753b3SRandall Stewart 							 */
29070fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2908f79aab18SRandall Stewart 								if (*rto_ok) {
29090fa753b3SRandall Stewart 									tp1->whoTo->RTO =
29100fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
29110fa753b3SRandall Stewart 									    &stcb->asoc,
29120fa753b3SRandall Stewart 									    tp1->whoTo,
29130fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2914899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2915f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2916f79aab18SRandall Stewart 									*rto_ok = 0;
2917f79aab18SRandall Stewart 								}
2918f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2919f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2920f79aab18SRandall Stewart 								}
29210fa753b3SRandall Stewart 								tp1->do_rtt = 0;
29220fa753b3SRandall Stewart 							}
29230fa753b3SRandall Stewart 						}
29240fa753b3SRandall Stewart 					}
29250fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
292620b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
292720b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
29280fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
29290fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29300fa753b3SRandall Stewart 						}
29310fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
29320fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
29330fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
29340fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
29350fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
29360fa753b3SRandall Stewart #endif
29370fa753b3SRandall Stewart 						}
29380fa753b3SRandall Stewart 					}
29390fa753b3SRandall Stewart 					/*-
29400fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
29410fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
29420fa753b3SRandall Stewart 					 */
29432a498584SMichael Tuexen 					if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
2944325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
29450fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
29462a498584SMichael Tuexen 					}
29470fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
29480fa753b3SRandall Stewart 						/* deflate the cwnd */
29490fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
29500fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
29510fa753b3SRandall Stewart 					}
29520fa753b3SRandall Stewart 					/* NR Sack code here */
2953325c8c46SMichael Tuexen 					if (nr_sacking &&
2954325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2955325c8c46SMichael Tuexen 						if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
2956325c8c46SMichael Tuexen 							stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--;
2957325c8c46SMichael Tuexen #ifdef INVARIANTS
2958325c8c46SMichael Tuexen 						} else {
2959325c8c46SMichael Tuexen 							panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
2960325c8c46SMichael Tuexen #endif
2961325c8c46SMichael Tuexen 						}
2962d96bef9cSMichael Tuexen 						if ((stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues == 0) &&
2963d96bef9cSMichael Tuexen 						    (stcb->asoc.strmout[tp1->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
2964d96bef9cSMichael Tuexen 						    TAILQ_EMPTY(&stcb->asoc.strmout[tp1->rec.data.stream_number].outqueue)) {
2965d96bef9cSMichael Tuexen 							stcb->asoc.trigger_reset = 1;
2966d96bef9cSMichael Tuexen 						}
2967325c8c46SMichael Tuexen 						tp1->sent = SCTP_DATAGRAM_NR_ACKED;
29680fa753b3SRandall Stewart 						if (tp1->data) {
29690fa753b3SRandall Stewart 							/*
29700fa753b3SRandall Stewart 							 * sa_ignore
29710fa753b3SRandall Stewart 							 * NO_NULL_CHK
29720fa753b3SRandall Stewart 							 */
29730fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
29740fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
29750fa753b3SRandall Stewart 							tp1->data = NULL;
2976b5c16493SMichael Tuexen 						}
29770fa753b3SRandall Stewart 						wake_him++;
29780fa753b3SRandall Stewart 					}
29790fa753b3SRandall Stewart 				}
29800fa753b3SRandall Stewart 				break;
29810fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
298220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
29830fa753b3SRandall Stewart 				break;
298420b07a4dSMichael Tuexen 			}
29850fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
2986b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
2987b5c16493SMichael Tuexen 				circled++;
29880fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
29890fa753b3SRandall Stewart 			}
2990b5c16493SMichael Tuexen 		}		/* end while (tp1) */
2991b5c16493SMichael Tuexen 		if (tp1 == NULL) {
2992b5c16493SMichael Tuexen 			circled = 0;
2993b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2994b5c16493SMichael Tuexen 		}
2995b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
29960fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
29970fa753b3SRandall Stewart 	*p_tp1 = tp1;
29980fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
29990fa753b3SRandall Stewart }
30000fa753b3SRandall Stewart 
30010fa753b3SRandall Stewart 
3002cd554309SMichael Tuexen static int
3003458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3004cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3005139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
30067215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
3007f8829a4aSRandall Stewart {
3008458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3009f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30100fa753b3SRandall Stewart 	int i;
3011f8829a4aSRandall Stewart 	int num_frs = 0;
3012cd554309SMichael Tuexen 	int chunk_freed;
3013cd554309SMichael Tuexen 	int non_revocable;
3014d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
3015f8829a4aSRandall Stewart 
3016d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3017d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
3018cd554309SMichael Tuexen 	chunk_freed = 0;
3019f8829a4aSRandall Stewart 
3020cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3021d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
3022d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
3023d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3024d9c5cfeaSMichael Tuexen 		}
3025458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3026458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3027458303daSRandall Stewart 		*offset += sizeof(block);
3028458303daSRandall Stewart 		if (frag == NULL) {
3029cd554309SMichael Tuexen 			return (chunk_freed);
3030458303daSRandall Stewart 		}
3031f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3032f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3033d9c5cfeaSMichael Tuexen 
3034f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3035d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
3036f8829a4aSRandall Stewart 			continue;
3037f8829a4aSRandall Stewart 		}
3038d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
3039d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
3040f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3041f8829a4aSRandall Stewart 		}
304220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3043d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
3044f8829a4aSRandall Stewart 		}
3045cd554309SMichael Tuexen 		if (i < num_seg) {
3046cd554309SMichael Tuexen 			non_revocable = 0;
3047cd554309SMichael Tuexen 		} else {
3048cd554309SMichael Tuexen 			non_revocable = 1;
3049cd554309SMichael Tuexen 		}
3050cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3051cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
30527215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
3053cd554309SMichael Tuexen 			chunk_freed = 1;
3054458303daSRandall Stewart 		}
3055d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
3056f8829a4aSRandall Stewart 	}
3057b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
305880fefe0aSRandall Stewart 		if (num_frs)
305980fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
306080fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
306180fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
306280fefe0aSRandall Stewart 	}
3063cd554309SMichael Tuexen 	return (chunk_freed);
3064f8829a4aSRandall Stewart }
3065f8829a4aSRandall Stewart 
3066f8829a4aSRandall Stewart static void
3067c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3068c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
306963eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3070f8829a4aSRandall Stewart {
3071f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3072f8829a4aSRandall Stewart 
30734a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
307420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3075f8829a4aSRandall Stewart 			/*
3076f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3077f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3078f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3079f8829a4aSRandall Stewart 			 * again.
3080f8829a4aSRandall Stewart 			 */
308120b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3082d06c82f1SRandall Stewart 				break;
308320b07a4dSMichael Tuexen 			}
3084f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3085f8829a4aSRandall Stewart 				/* it has been revoked */
3086f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3087f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3088a5d547adSRandall Stewart 				/*
308942551e99SRandall Stewart 				 * We must add this stuff back in to assure
309042551e99SRandall Stewart 				 * timers and such get started.
3091a5d547adSRandall Stewart 				 */
3092b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3093c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3094c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3095c105859eSRandall Stewart 					    tp1->book_size,
30969a8e3088SMichael Tuexen 					    (uint32_t) (uintptr_t) tp1->whoTo,
3097c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
309880fefe0aSRandall Stewart 				}
3099c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3100c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
310142551e99SRandall Stewart 				/*
310242551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
310342551e99SRandall Stewart 				 * artificial inflation of the flight_size.
310442551e99SRandall Stewart 				 */
310542551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3106b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3107f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3108f8829a4aSRandall Stewart 					    cumack,
3109f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3110f8829a4aSRandall Stewart 					    0,
3111f8829a4aSRandall Stewart 					    0,
3112f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
311380fefe0aSRandall Stewart 				}
3114f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3115f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3116f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3117f8829a4aSRandall Stewart 			}
3118f8829a4aSRandall Stewart 		}
3119f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3120f8829a4aSRandall Stewart 			break;
3121f8829a4aSRandall Stewart 	}
3122f8829a4aSRandall Stewart }
3123f8829a4aSRandall Stewart 
3124830d754dSRandall Stewart 
3125f8829a4aSRandall Stewart static void
3126f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
312763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3128f8829a4aSRandall Stewart {
3129f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3130f8829a4aSRandall Stewart 	int strike_flag = 0;
3131f8829a4aSRandall Stewart 	struct timeval now;
3132f8829a4aSRandall Stewart 	int tot_retrans = 0;
3133f8829a4aSRandall Stewart 	uint32_t sending_seq;
3134f8829a4aSRandall Stewart 	struct sctp_nets *net;
3135f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3136f8829a4aSRandall Stewart 
3137f8829a4aSRandall Stewart 	/*
3138f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3139f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3140f8829a4aSRandall Stewart 	 */
3141f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3142f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3143f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3144f8829a4aSRandall Stewart 	} else {
3145f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3146f8829a4aSRandall Stewart 	}
3147f8829a4aSRandall Stewart 
3148f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
31497c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
315020083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3151f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3152f8829a4aSRandall Stewart 			if (net->saw_newack)
3153f8829a4aSRandall Stewart 				num_dests_sacked++;
3154f8829a4aSRandall Stewart 		}
3155f8829a4aSRandall Stewart 	}
3156dd973b0eSMichael Tuexen 	if (stcb->asoc.prsctp_supported) {
31576e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3158f8829a4aSRandall Stewart 	}
31594a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3160f8829a4aSRandall Stewart 		strike_flag = 0;
3161f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3162f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3163f8829a4aSRandall Stewart 			continue;
3164f8829a4aSRandall Stewart 		}
3165b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3166f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3167f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3168f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3169f8829a4aSRandall Stewart 				    tp1->sent,
3170f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
317180fefe0aSRandall Stewart 		}
317220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3173f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3174f8829a4aSRandall Stewart 			/* done */
3175f8829a4aSRandall Stewart 			break;
3176f8829a4aSRandall Stewart 		}
3177dd973b0eSMichael Tuexen 		if (stcb->asoc.prsctp_supported) {
3178f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3179f8829a4aSRandall Stewart 				/* Is it expired? */
318099ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3181f8829a4aSRandall Stewart 					/* Yes so drop it */
3182f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
31831edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
31840c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3185f8829a4aSRandall Stewart 					}
3186f8829a4aSRandall Stewart 					continue;
3187f8829a4aSRandall Stewart 				}
3188f8829a4aSRandall Stewart 			}
3189f8829a4aSRandall Stewart 		}
319020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3191f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3192f8829a4aSRandall Stewart 			break;
3193f8829a4aSRandall Stewart 		}
3194f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3195f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3196f8829a4aSRandall Stewart 			/* skip */
319744fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
319844fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
319944fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
320044fbe462SRandall Stewart 			}
3201f8829a4aSRandall Stewart 			continue;
3202f8829a4aSRandall Stewart 		}
3203f8829a4aSRandall Stewart 		/*
3204f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3205f8829a4aSRandall Stewart 		 */
3206ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3207f8829a4aSRandall Stewart 			/*
3208f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3209f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3210f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3211c105859eSRandall Stewart 			 * FR using this SACK.
3212f8829a4aSRandall Stewart 			 */
3213f8829a4aSRandall Stewart 			continue;
321420b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
321520b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3216f8829a4aSRandall Stewart 			/*
3217f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3218f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3219f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3220f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3221f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3222f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3223f8829a4aSRandall Stewart 			 */
3224f8829a4aSRandall Stewart 			continue;
3225f8829a4aSRandall Stewart 		}
3226f8829a4aSRandall Stewart 		/*
3227f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3228f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3229f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3230f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3231f8829a4aSRandall Stewart 		 */
3232f8829a4aSRandall Stewart 		/*
323342551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
323442551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
323542551e99SRandall Stewart 		 * 0)) {
3236f8829a4aSRandall Stewart 		 */
323742551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3238f8829a4aSRandall Stewart 			/*
3239f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3240f8829a4aSRandall Stewart 			 * moved.
3241f8829a4aSRandall Stewart 			 */
3242b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3243f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3244f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3245f8829a4aSRandall Stewart 				    tp1->sent,
3246f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
324780fefe0aSRandall Stewart 			}
32485e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3249f8829a4aSRandall Stewart 				tp1->sent++;
32505e54f665SRandall Stewart 			}
32517c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
325220083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3253f8829a4aSRandall Stewart 				/*
3254f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3255f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3256f8829a4aSRandall Stewart 				 * because it would have been set to the
3257f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3258f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3259f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3260c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3261c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3262c105859eSRandall Stewart 				 * indicates that two packets have been
3263c105859eSRandall Stewart 				 * received after this missing TSN.
32645e54f665SRandall Stewart 				 */
32655e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
326620b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3267b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3268f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3269f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3270f8829a4aSRandall Stewart 						    tp1->sent,
3271f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
327280fefe0aSRandall Stewart 					}
3273f8829a4aSRandall Stewart 					tp1->sent++;
3274f8829a4aSRandall Stewart 				}
3275f8829a4aSRandall Stewart 			}
327620083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
327720083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3278f8829a4aSRandall Stewart 			/*
3279f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3280f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3281f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3282f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3283f8829a4aSRandall Stewart 			 */
32845e54f665SRandall Stewart 			if (
3285f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3286f8829a4aSRandall Stewart 			/*
3287f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3288f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3289f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3290f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3291f8829a4aSRandall Stewart 			 */
32925e54f665SRandall Stewart 			    (asoc->numnets < 2)
3293f8829a4aSRandall Stewart #else
32945e54f665SRandall Stewart 			    (1)
3295f8829a4aSRandall Stewart #endif
32965e54f665SRandall Stewart 			    ) {
32975e54f665SRandall Stewart 
329820b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3299f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3300f8829a4aSRandall Stewart 					/*
3301f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3302f8829a4aSRandall Stewart 					 * beyond where things were when we
3303f8829a4aSRandall Stewart 					 * did a FR.
3304f8829a4aSRandall Stewart 					 */
3305b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3306f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3307f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3308f8829a4aSRandall Stewart 						    tp1->sent,
3309f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
331080fefe0aSRandall Stewart 					}
33115e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3312f8829a4aSRandall Stewart 						tp1->sent++;
33135e54f665SRandall Stewart 					}
3314f8829a4aSRandall Stewart 					strike_flag = 1;
33157c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
331620083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3317f8829a4aSRandall Stewart 						/*
3318f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3319f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3320f8829a4aSRandall Stewart 						 * then lowest_newack test
3321f8829a4aSRandall Stewart 						 * will not pass because it
3322f8829a4aSRandall Stewart 						 * would have been set to
3323f8829a4aSRandall Stewart 						 * the cumack earlier. If
3324f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3325f8829a4aSRandall Stewart 						 * If not a mixed sack and
3326f8829a4aSRandall Stewart 						 * if tp1 is not between two
3327f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3328c105859eSRandall Stewart 						 * one more. NOTE that we
3329c105859eSRandall Stewart 						 * are marking by one
3330c105859eSRandall Stewart 						 * additional time since the
3331c105859eSRandall Stewart 						 * SACK DAC flag indicates
3332c105859eSRandall Stewart 						 * that two packets have
3333c105859eSRandall Stewart 						 * been received after this
3334c105859eSRandall Stewart 						 * missing TSN.
3335f8829a4aSRandall Stewart 						 */
33365e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
33375e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
333820b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
333920b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3340b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3341f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3342f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3343f8829a4aSRandall Stewart 								    tp1->sent,
3344f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
334580fefe0aSRandall Stewart 							}
33465e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3347f8829a4aSRandall Stewart 								tp1->sent++;
33485e54f665SRandall Stewart 							}
3349f8829a4aSRandall Stewart 						}
3350f8829a4aSRandall Stewart 					}
3351f8829a4aSRandall Stewart 				}
3352f8829a4aSRandall Stewart 			}
3353f8829a4aSRandall Stewart 			/*
335442551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
335542551e99SRandall Stewart 			 * algo covers HTNA.
3356f8829a4aSRandall Stewart 			 */
335720b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
335820b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3359f8829a4aSRandall Stewart 			/*
3360f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3361f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3362f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3363f8829a4aSRandall Stewart 			 */
3364f8829a4aSRandall Stewart 			;
3365f8829a4aSRandall Stewart 		} else {
3366f8829a4aSRandall Stewart 			/* Strike the TSN */
3367b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3368f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3369f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3370f8829a4aSRandall Stewart 				    tp1->sent,
3371f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
337280fefe0aSRandall Stewart 			}
33735e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3374f8829a4aSRandall Stewart 				tp1->sent++;
33755e54f665SRandall Stewart 			}
33767c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
337720083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3378f8829a4aSRandall Stewart 				/*
3379f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3380f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3381f8829a4aSRandall Stewart 				 * because it would have been set to the
3382f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3383f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3384f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3385c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3386c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3387c105859eSRandall Stewart 				 * indicates that two packets have been
3388c105859eSRandall Stewart 				 * received after this missing TSN.
3389f8829a4aSRandall Stewart 				 */
33905e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
339120b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3392b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3393f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3394f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3395f8829a4aSRandall Stewart 						    tp1->sent,
3396f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
339780fefe0aSRandall Stewart 					}
3398f8829a4aSRandall Stewart 					tp1->sent++;
3399f8829a4aSRandall Stewart 				}
3400f8829a4aSRandall Stewart 			}
3401f8829a4aSRandall Stewart 		}
3402f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3403f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3404f8829a4aSRandall Stewart 
3405544e35bdSRandall Stewart 			/* fix counts and things */
3406544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3407544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3408544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3409544e35bdSRandall Stewart 				    tp1->book_size,
34109a8e3088SMichael Tuexen 				    (uint32_t) (uintptr_t) tp1->whoTo,
3411544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3412544e35bdSRandall Stewart 			}
3413544e35bdSRandall Stewart 			if (tp1->whoTo) {
3414544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3415544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3416299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3417299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3418299108c5SRandall Stewart 					    tp1);
3419299108c5SRandall Stewart 				}
3420544e35bdSRandall Stewart 			}
3421544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3422544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3423544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3424544e35bdSRandall Stewart 			}
3425544e35bdSRandall Stewart 			/* add back to the rwnd */
3426544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3427544e35bdSRandall Stewart 
3428544e35bdSRandall Stewart 			/* remove from the total flight */
3429544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3430544e35bdSRandall Stewart 
3431dd973b0eSMichael Tuexen 			if ((stcb->asoc.prsctp_supported) &&
3432475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3433475d0674SMichael Tuexen 				/*
3434475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3435475d0674SMichael Tuexen 				 * we store the retran count there.
3436475d0674SMichael Tuexen 				 */
3437475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3438475d0674SMichael Tuexen 					/* Yes, so drop it */
3439475d0674SMichael Tuexen 					if (tp1->data != NULL) {
34401edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3441475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3442475d0674SMichael Tuexen 					}
3443475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3444475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3445475d0674SMichael Tuexen 					continue;
3446475d0674SMichael Tuexen 				}
3447475d0674SMichael Tuexen 			}
3448cd3fd531SMichael Tuexen 			/*
3449cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3450cd3fd531SMichael Tuexen 			 * guy\n");
3451cd3fd531SMichael Tuexen 			 */
3452b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3453f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3454f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
345580fefe0aSRandall Stewart 			}
3456f8829a4aSRandall Stewart 			if (strike_flag) {
3457f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3458f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3459f8829a4aSRandall Stewart 			}
34605e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
34617c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3462f8829a4aSRandall Stewart 				/*
3463f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3464f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3465f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3466f8829a4aSRandall Stewart 				 */
3467f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3468f8829a4aSRandall Stewart 				alt = tp1->whoTo;
34693c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
347020083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3471b54d3a6cSRandall Stewart 					/*
3472b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3473b54d3a6cSRandall Stewart 					 * use the PF version of
3474b54d3a6cSRandall Stewart 					 * find_alt_net()
3475b54d3a6cSRandall Stewart 					 */
3476b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3477b54d3a6cSRandall Stewart 				} else {
3478b54d3a6cSRandall Stewart 					/*
3479b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3480b54d3a6cSRandall Stewart 					 * use the CMT version of
3481b54d3a6cSRandall Stewart 					 * find_alt_net()
3482b54d3a6cSRandall Stewart 					 */
348352be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3484f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3485b54d3a6cSRandall Stewart 				}
3486ad81507eSRandall Stewart 				if (alt == NULL) {
3487ad81507eSRandall Stewart 					alt = tp1->whoTo;
3488ad81507eSRandall Stewart 				}
3489f8829a4aSRandall Stewart 				/*
3490f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3491f8829a4aSRandall Stewart 				 * the retransmission, then new
3492f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3493f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3494f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3495f8829a4aSRandall Stewart 				 */
3496ad81507eSRandall Stewart 				if (tp1->whoTo) {
3497f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3498f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3499ad81507eSRandall Stewart 				}
3500f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3501f8829a4aSRandall Stewart 
3502f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3503f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3504f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3505f8829a4aSRandall Stewart #else
3506f8829a4aSRandall Stewart 				/*
3507f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3508f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3509f8829a4aSRandall Stewart 				 * paper details why.
3510f8829a4aSRandall Stewart 				 */
3511f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3512f8829a4aSRandall Stewart #endif
3513f8829a4aSRandall Stewart 			}
3514f8829a4aSRandall Stewart 
3515f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3516f8829a4aSRandall Stewart 			tot_retrans++;
3517f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3518f8829a4aSRandall Stewart 			/*
3519cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3520f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3521f8829a4aSRandall Stewart 			 */
3522f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3523f8829a4aSRandall Stewart 				/*
3524f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3525f8829a4aSRandall Stewart 				 * the next sequence number that will be
3526f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3527f8829a4aSRandall Stewart 				 * get the one we last sent.
3528f8829a4aSRandall Stewart 				 */
3529f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3530f8829a4aSRandall Stewart 			} else {
3531f8829a4aSRandall Stewart 				/*
3532f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3533f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3534f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3535f8829a4aSRandall Stewart 				 * take the first one (which will have the
3536f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3537f8829a4aSRandall Stewart 				 * one we last sent.
3538f8829a4aSRandall Stewart 				 */
3539f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3540f8829a4aSRandall Stewart 
3541f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3542f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3543f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3544f8829a4aSRandall Stewart 			}
3545f8829a4aSRandall Stewart 
3546f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3547f8829a4aSRandall Stewart 				/*
3548f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3549f8829a4aSRandall Stewart 				 * it, cancel it
3550f8829a4aSRandall Stewart 				 */
355160990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
355260990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3553f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3554f79aab18SRandall Stewart 				}
3555f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3556f8829a4aSRandall Stewart 			}
3557f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3558f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3559f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
35603c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3561f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3562f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3563f8829a4aSRandall Stewart 			}
3564f8829a4aSRandall Stewart 		}
35654a9ef3f8SMichael Tuexen 	}
3566f8829a4aSRandall Stewart }
3567f8829a4aSRandall Stewart 
3568f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3569f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3570f8829a4aSRandall Stewart     struct sctp_association *asoc)
3571f8829a4aSRandall Stewart {
3572f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3573f8829a4aSRandall Stewart 	struct timeval now;
3574f8829a4aSRandall Stewart 	int now_filled = 0;
3575f8829a4aSRandall Stewart 
3576dd973b0eSMichael Tuexen 	if (asoc->prsctp_supported == 0) {
3577f8829a4aSRandall Stewart 		return (NULL);
3578f8829a4aSRandall Stewart 	}
35794a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3580f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
358198f2956cSMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_RESEND &&
3582325c8c46SMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3583f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3584f8829a4aSRandall Stewart 			break;
3585f8829a4aSRandall Stewart 		}
35860c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
35872a498584SMichael Tuexen 			if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3588325c8c46SMichael Tuexen 			    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
35890c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
35900c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
35910c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
35920c0982b8SRandall Stewart 			}
35930c0982b8SRandall Stewart 		}
3594f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3595f8829a4aSRandall Stewart 			/*
3596f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3597f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3598f8829a4aSRandall Stewart 			 */
3599f8829a4aSRandall Stewart 			break;
3600f8829a4aSRandall Stewart 		}
3601f8829a4aSRandall Stewart 		if (!now_filled) {
36026e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3603f8829a4aSRandall Stewart 			now_filled = 1;
3604f8829a4aSRandall Stewart 		}
3605f8829a4aSRandall Stewart 		/*
3606f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3607f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3608f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3609f8829a4aSRandall Stewart 		 * it if its a resend?
3610f8829a4aSRandall Stewart 		 */
3611f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3612f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3613f8829a4aSRandall Stewart 			/*
3614f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3615f8829a4aSRandall Stewart 			 * now up?
3616f8829a4aSRandall Stewart 			 */
3617f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3618f8829a4aSRandall Stewart 				/* Yes so drop it */
3619f8829a4aSRandall Stewart 				if (tp1->data) {
3620ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
36211edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3622f8829a4aSRandall Stewart 				}
3623f8829a4aSRandall Stewart 			} else {
3624f8829a4aSRandall Stewart 				/*
3625f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3626f8829a4aSRandall Stewart 				 * whos time as not expired.
3627f8829a4aSRandall Stewart 				 */
3628f8829a4aSRandall Stewart 				break;
3629f8829a4aSRandall Stewart 			}
3630f8829a4aSRandall Stewart 		}
3631f8829a4aSRandall Stewart 		/*
3632f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3633f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3634f8829a4aSRandall Stewart 		 * the next chunk.
3635f8829a4aSRandall Stewart 		 */
363698f2956cSMichael Tuexen 		if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3637325c8c46SMichael Tuexen 		    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3638f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
363920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3640f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3641f8829a4aSRandall Stewart 				a_adv = tp1;
36420c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
36430c0982b8SRandall Stewart 				/* No update but we do save the chk */
36440c0982b8SRandall Stewart 				a_adv = tp1;
36450c0982b8SRandall Stewart 			}
3646f8829a4aSRandall Stewart 		} else {
3647f8829a4aSRandall Stewart 			/*
3648f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3649f8829a4aSRandall Stewart 			 * further
3650f8829a4aSRandall Stewart 			 */
3651f8829a4aSRandall Stewart 			break;
3652f8829a4aSRandall Stewart 		}
3653f8829a4aSRandall Stewart 	}
3654f8829a4aSRandall Stewart 	return (a_adv);
3655f8829a4aSRandall Stewart }
3656f8829a4aSRandall Stewart 
36570c0982b8SRandall Stewart static int
3658c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3659bff64a4dSRandall Stewart {
3660bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3661afd67482SMichael Tuexen 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3662548f47a8SMichael Tuexen 	int ret;
36630c0982b8SRandall Stewart 
3664548f47a8SMichael Tuexen #ifndef INVARIANTS
3665548f47a8SMichael Tuexen 	int entry_flight, entry_cnt;
3666548f47a8SMichael Tuexen 
3667548f47a8SMichael Tuexen #endif
3668548f47a8SMichael Tuexen 
3669548f47a8SMichael Tuexen 	ret = 0;
3670548f47a8SMichael Tuexen #ifndef INVARIANTS
36710c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
36720c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
3673548f47a8SMichael Tuexen #endif
36740c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
36750c0982b8SRandall Stewart 		return (0);
3676bff64a4dSRandall Stewart 
3677bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3678bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3679cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN: %u size: %d inflight cnt: %d\n",
36800c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
36810c0982b8SRandall Stewart 			    chk->send_size,
3682cd3fd531SMichael Tuexen 			    chk->snd_count);
3683bff64a4dSRandall Stewart 			inflight++;
3684bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3685bff64a4dSRandall Stewart 			resend++;
3686bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3687bff64a4dSRandall Stewart 			inbetween++;
3688bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3689bff64a4dSRandall Stewart 			above++;
3690bff64a4dSRandall Stewart 		} else {
3691bff64a4dSRandall Stewart 			acked++;
3692bff64a4dSRandall Stewart 		}
3693bff64a4dSRandall Stewart 	}
3694f1f73e57SRandall Stewart 
3695c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3696f1f73e57SRandall Stewart #ifdef INVARIANTS
3697c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3698f1f73e57SRandall Stewart #else
3699cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight: %d cnt: %d\n",
37000c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
37010c0982b8SRandall Stewart 
37020c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F: %d I: %d R: %d Ab: %d ACK: %d\n",
37030c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
37040c0982b8SRandall Stewart 		ret = 1;
3705f1f73e57SRandall Stewart #endif
3706bff64a4dSRandall Stewart 	}
37070c0982b8SRandall Stewart 	return (ret);
3708c105859eSRandall Stewart }
3709c105859eSRandall Stewart 
3710c105859eSRandall Stewart 
3711c105859eSRandall Stewart static void
3712c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3713c105859eSRandall Stewart     struct sctp_association *asoc,
3714c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3715c105859eSRandall Stewart {
3716dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
37175171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3718dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3719dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
37208427b3fdSMichael Tuexen 		    tp1->whoTo ? tp1->whoTo->flight_size : 0,
3721dfb11ef8SRandall Stewart 		    tp1->book_size,
37229a8e3088SMichael Tuexen 		    (uint32_t) (uintptr_t) tp1->whoTo,
3723dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3724dfb11ef8SRandall Stewart 		return;
3725dfb11ef8SRandall Stewart 	}
37265171328bSRandall Stewart 	/* First setup this by shrinking flight */
3727299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3728299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3729299108c5SRandall Stewart 		    tp1);
3730299108c5SRandall Stewart 	}
37315171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
37325171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
37335171328bSRandall Stewart 	/* Now mark for resend */
37345171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3735791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3736791437b5SRandall Stewart 
3737b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3738c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3739c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3740c105859eSRandall Stewart 		    tp1->book_size,
37419a8e3088SMichael Tuexen 		    (uint32_t) (uintptr_t) tp1->whoTo,
3742c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
374380fefe0aSRandall Stewart 	}
3744c105859eSRandall Stewart }
3745c105859eSRandall Stewart 
3746f8829a4aSRandall Stewart void
3747f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3748899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3749f8829a4aSRandall Stewart {
3750f8829a4aSRandall Stewart 	struct sctp_nets *net;
3751f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3752f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
37535e54f665SRandall Stewart 	uint32_t old_rwnd;
37545e54f665SRandall Stewart 	int win_probe_recovery = 0;
3755c105859eSRandall Stewart 	int win_probe_recovered = 0;
3756d06c82f1SRandall Stewart 	int j, done_once = 0;
3757f79aab18SRandall Stewart 	int rto_ok = 1;
3758fd60718dSMichael Tuexen 	uint32_t send_s;
3759f8829a4aSRandall Stewart 
3760b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3761d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3762d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
376380fefe0aSRandall Stewart 	}
3764f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
376518e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
376618e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
376718e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
376818e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
376918e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
377018e198d3SRandall Stewart 	}
377118e198d3SRandall Stewart #endif
3772f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3773d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
377420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
37755e54f665SRandall Stewart 		/* old ack */
37765e54f665SRandall Stewart 		return;
3777d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3778d06c82f1SRandall Stewart 		/* Window update sack */
3779d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
378044fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3781d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3782d06c82f1SRandall Stewart 			/* SWS sender side engages */
3783d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3784d06c82f1SRandall Stewart 		}
3785d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3786d06c82f1SRandall Stewart 			goto again;
3787d06c82f1SRandall Stewart 		}
3788d06c82f1SRandall Stewart 		return;
37895e54f665SRandall Stewart 	}
3790f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3791f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3792a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3793a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3794a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3795a21779f0SRandall Stewart 		}
3796f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3797f8829a4aSRandall Stewart 		net->net_ack = 0;
3798f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3799132dea7dSRandall Stewart 
3800132dea7dSRandall Stewart 		/*
3801132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3802132dea7dSRandall Stewart 		 * SACK processing
3803132dea7dSRandall Stewart 		 */
3804132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3805132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3806299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3807299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3808299108c5SRandall Stewart 		}
3809f8829a4aSRandall Stewart 	}
3810c105859eSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3811c105859eSRandall Stewart 		tp1 = TAILQ_LAST(&asoc->sent_queue,
3812c105859eSRandall Stewart 		    sctpchunk_listhead);
3813c105859eSRandall Stewart 		send_s = tp1->rec.data.TSN_seq + 1;
3814139bc87fSRandall Stewart 	} else {
3815c105859eSRandall Stewart 		send_s = asoc->sending_seq;
3816139bc87fSRandall Stewart 	}
381720b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(cumack, send_s)) {
3818ff1ffd74SMichael Tuexen 		struct mbuf *op_err;
3819ff1ffd74SMichael Tuexen 		char msg[SCTP_DIAG_INFO_LEN];
3820139bc87fSRandall Stewart 
3821139bc87fSRandall Stewart 		*abort_now = 1;
3822139bc87fSRandall Stewart 		/* XXX */
382355f8a4bbSMichael Tuexen 		snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x",
3824ff1ffd74SMichael Tuexen 		    cumack, send_s);
3825ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
382644249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_21;
3827ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3828139bc87fSRandall Stewart 		return;
3829139bc87fSRandall Stewart 	}
3830f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3831b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3832c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3833c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3834c4739e2fSRandall Stewart 		    0,
3835c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3836c4739e2fSRandall Stewart 		    __LINE__);
3837c4739e2fSRandall Stewart 	}
3838f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
383920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3840f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
38414a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
384220b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
384318e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3844cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
384518e198d3SRandall Stewart 				}
3846f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3847f8829a4aSRandall Stewart 					/*
384818e198d3SRandall Stewart 					 * If it is less than ACKED, it is
384918e198d3SRandall Stewart 					 * now no-longer in flight. Higher
385018e198d3SRandall Stewart 					 * values may occur during marking
3851f8829a4aSRandall Stewart 					 */
3852c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3853b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3854c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3855a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3856a5d547adSRandall Stewart 							    tp1->book_size,
38579a8e3088SMichael Tuexen 							    (uint32_t) (uintptr_t) tp1->whoTo,
3858a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
385980fefe0aSRandall Stewart 						}
3860c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3861299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3862299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3863299108c5SRandall Stewart 							    tp1);
3864299108c5SRandall Stewart 						}
386504ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3866c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3867f8829a4aSRandall Stewart 					}
3868f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3869f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3870f8829a4aSRandall Stewart 						/*
387118e198d3SRandall Stewart 						 * True non-retransmited
3872f8829a4aSRandall Stewart 						 * chunk
3873f8829a4aSRandall Stewart 						 */
3874f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3875f8829a4aSRandall Stewart 						    tp1->send_size;
3876f8829a4aSRandall Stewart 
3877f8829a4aSRandall Stewart 						/* update RTO too? */
387862c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3879f79aab18SRandall Stewart 							if (rto_ok) {
3880f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
388104ee05e8SRandall Stewart 								/*
388204ee05e8SRandall Stewart 								 * sa_ignore
3883f79aab18SRandall Stewart 								 * NO_NULL_CH
3884f79aab18SRandall Stewart 								 * K
388504ee05e8SRandall Stewart 								 */
3886f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3887f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
388818e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3889899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3890f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3891f79aab18SRandall Stewart 								rto_ok = 0;
3892f79aab18SRandall Stewart 							}
3893f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3894f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3895f79aab18SRandall Stewart 							}
3896f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3897f8829a4aSRandall Stewart 						}
3898f8829a4aSRandall Stewart 					}
3899132dea7dSRandall Stewart 					/*
390018e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
390118e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
390218e198d3SRandall Stewart 					 * acked for the first time, set the
390318e198d3SRandall Stewart 					 * following variables for the
390418e198d3SRandall Stewart 					 * corresp destination.
390518e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
390618e198d3SRandall Stewart 					 * cwnd update.
390718e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
390818e198d3SRandall Stewart 					 * trigger search for the next
390918e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3910132dea7dSRandall Stewart 					 */
3911132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3912132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3913132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3914132dea7dSRandall Stewart 
3915b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
391604ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3917f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
391880fefe0aSRandall Stewart 					}
3919f8829a4aSRandall Stewart 				}
3920f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3921f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3922f8829a4aSRandall Stewart 				}
392342551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
392442551e99SRandall Stewart 					/* deflate the cwnd */
392542551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
392642551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
392742551e99SRandall Stewart 				}
3928325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3929a7ad6026SMichael Tuexen 					if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
3930a7ad6026SMichael Tuexen 						asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
3931a7ad6026SMichael Tuexen #ifdef INVARIANTS
3932a7ad6026SMichael Tuexen 					} else {
3933a7ad6026SMichael Tuexen 						panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
3934a7ad6026SMichael Tuexen #endif
3935a7ad6026SMichael Tuexen 					}
3936a7ad6026SMichael Tuexen 				}
3937d96bef9cSMichael Tuexen 				if ((asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues == 0) &&
3938d96bef9cSMichael Tuexen 				    (asoc->strmout[tp1->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
3939d96bef9cSMichael Tuexen 				    TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.stream_number].outqueue)) {
3940d96bef9cSMichael Tuexen 					asoc->trigger_reset = 1;
3941d96bef9cSMichael Tuexen 				}
3942f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3943f8829a4aSRandall Stewart 				if (tp1->data) {
394404ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3945f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3946f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
39474a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3948f8829a4aSRandall Stewart 				}
3949b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3950f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3951f8829a4aSRandall Stewart 					    cumack,
3952f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3953f8829a4aSRandall Stewart 					    0,
3954f8829a4aSRandall Stewart 					    0,
3955f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
395680fefe0aSRandall Stewart 				}
3957f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3958689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
395918e198d3SRandall Stewart 			} else {
396018e198d3SRandall Stewart 				break;
3961f8829a4aSRandall Stewart 			}
39625e54f665SRandall Stewart 		}
396318e198d3SRandall Stewart 
396418e198d3SRandall Stewart 	}
396504ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3966f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3967ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3968ceaad40aSRandall Stewart 		struct socket *so;
3969ceaad40aSRandall Stewart 
3970ceaad40aSRandall Stewart #endif
3971f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3972b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
397304ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
39747215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
397580fefe0aSRandall Stewart 		}
3976ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3977ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3978ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3979ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3980ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3981ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3982ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3983ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3984ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
3985ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3986ceaad40aSRandall Stewart 			return;
3987ceaad40aSRandall Stewart 		}
3988ceaad40aSRandall Stewart #endif
3989f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
3990ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3991ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3992ceaad40aSRandall Stewart #endif
3993f8829a4aSRandall Stewart 	} else {
3994b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
39957215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
399680fefe0aSRandall Stewart 		}
3997f8829a4aSRandall Stewart 	}
3998f8829a4aSRandall Stewart 
3999b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4000ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4001ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4002ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4003ca85e948SMichael Tuexen 				/*
4004ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4005ca85e948SMichael Tuexen 				 * count, this is optional.
4006ca85e948SMichael Tuexen 				 */
4007ca85e948SMichael Tuexen 				net->error_count = 0;
4008ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4009ca85e948SMichael Tuexen 					/* addr came good */
4010ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4011ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
40124b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4013ca85e948SMichael Tuexen 				}
4014ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4015ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4016ca85e948SMichael Tuexen 						/*
4017ca85e948SMichael Tuexen 						 * release the alternate,
4018ca85e948SMichael Tuexen 						 * primary is good
4019ca85e948SMichael Tuexen 						 */
4020ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4021ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4022ca85e948SMichael Tuexen 					}
4023ca85e948SMichael Tuexen 				}
4024ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4025ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4026b7d130beSMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4027b7d130beSMichael Tuexen 					    stcb->sctp_ep, stcb, net,
402844249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4029ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4030ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4031ca85e948SMichael Tuexen 					/* Done with this net */
4032ca85e948SMichael Tuexen 					net->net_ack = 0;
4033ca85e948SMichael Tuexen 				}
4034ca85e948SMichael Tuexen 				/* restore any doubled timers */
4035ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4036ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4037ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4038ca85e948SMichael Tuexen 				}
4039ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4040ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4041ca85e948SMichael Tuexen 				}
4042ca85e948SMichael Tuexen 			}
4043ca85e948SMichael Tuexen 		}
4044b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4045ca85e948SMichael Tuexen 	}
4046f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
40475e54f665SRandall Stewart 
4048f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4049f8829a4aSRandall Stewart 		/* nothing left in-flight */
4050f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4051f8829a4aSRandall Stewart 			net->flight_size = 0;
4052f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4053f8829a4aSRandall Stewart 		}
4054f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4055f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4056f8829a4aSRandall Stewart 	}
4057f8829a4aSRandall Stewart 	/* RWND update */
4058f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
405944fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4060f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4061f8829a4aSRandall Stewart 		/* SWS sender side engages */
4062f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4063f8829a4aSRandall Stewart 	}
40645e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
40655e54f665SRandall Stewart 		win_probe_recovery = 1;
40665e54f665SRandall Stewart 	}
4067f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4068a5d547adSRandall Stewart again:
4069a5d547adSRandall Stewart 	j = 0;
4070f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
40715171328bSRandall Stewart 		int to_ticks;
40725171328bSRandall Stewart 
40735e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4074c105859eSRandall Stewart 			win_probe_recovered = 1;
40755e54f665SRandall Stewart 			/*
40765e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
40775e54f665SRandall Stewart 			 * and clear the sent
40785e54f665SRandall Stewart 			 */
40793c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
40805e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
40815e54f665SRandall Stewart 				if (tp1->window_probe) {
4082cd554309SMichael Tuexen 					/* move back to data send queue */
40837215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
40845e54f665SRandall Stewart 					break;
40855e54f665SRandall Stewart 				}
40865e54f665SRandall Stewart 			}
40875e54f665SRandall Stewart 		}
4088f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4089f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4090f8829a4aSRandall Stewart 		} else {
4091f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4092f8829a4aSRandall Stewart 		}
40935171328bSRandall Stewart 		if (net->flight_size) {
4094a5d547adSRandall Stewart 			j++;
4095ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4096f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
40975171328bSRandall Stewart 			if (net->window_probe) {
40985171328bSRandall Stewart 				net->window_probe = 0;
40995171328bSRandall Stewart 			}
4100f8829a4aSRandall Stewart 		} else {
41015171328bSRandall Stewart 			if (net->window_probe) {
41025171328bSRandall Stewart 				/*
41035171328bSRandall Stewart 				 * In window probes we must assure a timer
41045171328bSRandall Stewart 				 * is still running there
41055171328bSRandall Stewart 				 */
41065171328bSRandall Stewart 				net->window_probe = 0;
41075171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
41085171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
41095171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
41105171328bSRandall Stewart 				}
41115171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4112f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4113a5d547adSRandall Stewart 				    stcb, net,
411444249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4115f8829a4aSRandall Stewart 			}
4116f8829a4aSRandall Stewart 		}
4117f8829a4aSRandall Stewart 	}
4118bff64a4dSRandall Stewart 	if ((j == 0) &&
4119bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4120bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4121c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4122bff64a4dSRandall Stewart 	    (done_once == 0)) {
41230c0982b8SRandall Stewart 		/*
41240c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
41250c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
41260c0982b8SRandall Stewart 		 */
41270c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4128a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4129a5d547adSRandall Stewart 				net->flight_size = 0;
4130a5d547adSRandall Stewart 			}
4131a5d547adSRandall Stewart 			asoc->total_flight = 0;
4132a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4133a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4134a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4135a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4136c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4137c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4138a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4139791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4140a5d547adSRandall Stewart 				}
4141a5d547adSRandall Stewart 			}
41420c0982b8SRandall Stewart 		}
4143bff64a4dSRandall Stewart 		done_once = 1;
4144a5d547adSRandall Stewart 		goto again;
4145a5d547adSRandall Stewart 	}
4146f8829a4aSRandall Stewart 	/**********************************/
4147f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4148f8829a4aSRandall Stewart 	/**********************************/
4149f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4150f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4151f8829a4aSRandall Stewart 		/* clean up */
4152f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4153f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4154f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4155f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4156f8829a4aSRandall Stewart 		    ) {
4157f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4158f8829a4aSRandall Stewart 
4159f8829a4aSRandall Stewart 			/*
4160f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4161f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4162f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4163f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4164f8829a4aSRandall Stewart 			 */
4165f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4166f8829a4aSRandall Stewart 			    sctp_streamhead);
41672afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
41682afb3e84SRandall Stewart 				/* Let cleanup code purge it */
41692afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
41702afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
41712afb3e84SRandall Stewart 				} else {
4172f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4173f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4174f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4175f8829a4aSRandall Stewart 				}
4176f8829a4aSRandall Stewart 			}
41772afb3e84SRandall Stewart 		}
4178f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4179f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4180f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4181f8829a4aSRandall Stewart 				/* Need to abort here */
4182ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4183f8829a4aSRandall Stewart 
4184f8829a4aSRandall Stewart 		abort_out_now:
4185f8829a4aSRandall Stewart 				*abort_now = 1;
4186f8829a4aSRandall Stewart 				/* XXX */
4187ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
418844249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4189ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4190fe4a59b3SMichael Tuexen 				return;
4191f8829a4aSRandall Stewart 			} else {
4192ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4193ca85e948SMichael Tuexen 
4194f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4195f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4196f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4197f42a358aSRandall Stewart 				}
4198c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4199b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4200f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4201ca85e948SMichael Tuexen 				if (asoc->alternate) {
4202ca85e948SMichael Tuexen 					netp = asoc->alternate;
4203ca85e948SMichael Tuexen 				} else {
4204ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4205ca85e948SMichael Tuexen 				}
4206ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4207f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4208ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4209f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4210ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4211f8829a4aSRandall Stewart 			}
4212f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4213f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4214ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4215ca85e948SMichael Tuexen 
4216f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4217f8829a4aSRandall Stewart 				goto abort_out_now;
4218f8829a4aSRandall Stewart 			}
4219f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4220c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4221b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
422212af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4223c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4224c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4225c39cfa1fSMichael Tuexen 			} else {
4226c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4227c39cfa1fSMichael Tuexen 			}
4228c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4229f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4230ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4231f8829a4aSRandall Stewart 		}
4232f8829a4aSRandall Stewart 	}
4233dfb11ef8SRandall Stewart 	/*********************************************/
4234dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4235dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4236dfb11ef8SRandall Stewart 	/*********************************************/
4237dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
423820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4239dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4240dfb11ef8SRandall Stewart 	}
4241830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4242dd973b0eSMichael Tuexen 	if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
4243830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4244830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4245830d754dSRandall Stewart 
4246830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4247830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4248830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
424920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4250830d754dSRandall Stewart 			/*
4251493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4252830d754dSRandall Stewart 			 */
425320b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4254830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
42550c0982b8SRandall Stewart 			} else if (lchk) {
42560c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
425744fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
42580c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
42590c0982b8SRandall Stewart 				}
4260830d754dSRandall Stewart 			}
4261830d754dSRandall Stewart 		}
4262830d754dSRandall Stewart 		if (lchk) {
4263830d754dSRandall Stewart 			/* Assure a timer is up */
4264830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4265830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4266830d754dSRandall Stewart 		}
4267830d754dSRandall Stewart 	}
4268b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4269f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4270f8829a4aSRandall Stewart 		    rwnd,
4271f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4272f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4273f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
427480fefe0aSRandall Stewart 	}
4275f8829a4aSRandall Stewart }
4276f8829a4aSRandall Stewart 
4277f8829a4aSRandall Stewart void
4278cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
42797215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4280cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4281cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4282899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4283f8829a4aSRandall Stewart {
4284f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4285f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4286cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4287f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4288c105859eSRandall Stewart 	uint32_t send_s = 0;
4289f8829a4aSRandall Stewart 	long j;
4290f8829a4aSRandall Stewart 	int accum_moved = 0;
4291f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
42925e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
42935e54f665SRandall Stewart 	int win_probe_recovery = 0;
4294c105859eSRandall Stewart 	int win_probe_recovered = 0;
4295f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4296bff64a4dSRandall Stewart 	int done_once;
4297f79aab18SRandall Stewart 	int rto_ok = 1;
4298f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4299f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4300f8829a4aSRandall Stewart 
4301f8829a4aSRandall Stewart 	/*
4302f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4303f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4304f8829a4aSRandall Stewart 	 */
4305f8829a4aSRandall Stewart 	/*
4306f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4307f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4308f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4309f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4310f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4311f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4312f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4313f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4314f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4315f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4316f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4317f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4318f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4319f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4320f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4321f8829a4aSRandall Stewart 	 */
4322f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4323f8829a4aSRandall Stewart 	/* CMT DAC algo */
4324f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4325f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4326cd554309SMichael Tuexen 	last_tsn = cum_ack;
4327cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
432818e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
432918e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
433018e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
433118e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
433218e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
433318e198d3SRandall Stewart 	}
433418e198d3SRandall Stewart #endif
4335d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4336f8829a4aSRandall Stewart 
4337cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4338cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4339cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4340cd554309SMichael Tuexen 	}
43415e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4342b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4343c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4344c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4345c4739e2fSRandall Stewart 		    0,
4346c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4347c4739e2fSRandall Stewart 		    __LINE__);
4348c4739e2fSRandall Stewart 	}
4349f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4350f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4351b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4352f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4353f8829a4aSRandall Stewart 		    cum_ack,
4354f8829a4aSRandall Stewart 		    0,
4355f8829a4aSRandall Stewart 		    num_seg,
4356f8829a4aSRandall Stewart 		    num_dup,
4357f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
435880fefe0aSRandall Stewart 	}
4359ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4360cd554309SMichael Tuexen 		uint16_t i;
4361458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4362f8829a4aSRandall Stewart 
4363cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4364cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4365458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4366cd554309SMichael Tuexen 			if (dupdata == NULL) {
4367458303daSRandall Stewart 				break;
4368458303daSRandall Stewart 			}
4369cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4370f8829a4aSRandall Stewart 		}
4371f8829a4aSRandall Stewart 	}
4372c105859eSRandall Stewart 	/* reality check */
4373c105859eSRandall Stewart 	if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4374c105859eSRandall Stewart 		tp1 = TAILQ_LAST(&asoc->sent_queue,
4375c105859eSRandall Stewart 		    sctpchunk_listhead);
4376c105859eSRandall Stewart 		send_s = tp1->rec.data.TSN_seq + 1;
4377c105859eSRandall Stewart 	} else {
4378b5c16493SMichael Tuexen 		tp1 = NULL;
4379c105859eSRandall Stewart 		send_s = asoc->sending_seq;
4380c105859eSRandall Stewart 	}
438120b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(cum_ack, send_s)) {
4382ff1ffd74SMichael Tuexen 		struct mbuf *op_err;
4383ff1ffd74SMichael Tuexen 		char msg[SCTP_DIAG_INFO_LEN];
4384c105859eSRandall Stewart 
4385f8829a4aSRandall Stewart 		/*
4386fd60718dSMichael Tuexen 		 * no way, we have not even sent this TSN out yet. Peer is
4387fd60718dSMichael Tuexen 		 * hopelessly messed up with us.
4388f8829a4aSRandall Stewart 		 */
4389cd3fd531SMichael Tuexen 		SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4390b5c16493SMichael Tuexen 		    cum_ack, send_s);
4391b5c16493SMichael Tuexen 		if (tp1) {
4392cd3fd531SMichael Tuexen 			SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1: %p\n",
4393dd294dceSMichael Tuexen 			    tp1->rec.data.TSN_seq, (void *)tp1);
4394b5c16493SMichael Tuexen 		}
4395f8829a4aSRandall Stewart hopeless_peer:
4396f8829a4aSRandall Stewart 		*abort_now = 1;
4397f8829a4aSRandall Stewart 		/* XXX */
439855f8a4bbSMichael Tuexen 		snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal than TSN %8.8x",
4399ff1ffd74SMichael Tuexen 		    cum_ack, send_s);
4400ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
440144249214SRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4402ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4403f8829a4aSRandall Stewart 		return;
4404f8829a4aSRandall Stewart 	}
4405f8829a4aSRandall Stewart 	/**********************/
4406f8829a4aSRandall Stewart 	/* 1) check the range */
4407f8829a4aSRandall Stewart 	/**********************/
440820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4409f8829a4aSRandall Stewart 		/* acking something behind */
4410f8829a4aSRandall Stewart 		return;
4411f8829a4aSRandall Stewart 	}
4412f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4413f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4414f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4415cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4416f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4417b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4418f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4419f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
442080fefe0aSRandall Stewart 		}
4421f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4422f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4423f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4424f8829a4aSRandall Stewart 		}
4425f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4426f8829a4aSRandall Stewart 			/* SWS sender side engages */
4427f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4428f8829a4aSRandall Stewart 		}
4429f8829a4aSRandall Stewart 		/* stop any timers */
4430f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4431f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
443244249214SRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4433f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4434f8829a4aSRandall Stewart 			net->flight_size = 0;
4435f8829a4aSRandall Stewart 		}
4436f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4437f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4438f8829a4aSRandall Stewart 		return;
4439f8829a4aSRandall Stewart 	}
4440f8829a4aSRandall Stewart 	/*
4441f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4442f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4443f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4444f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4445f8829a4aSRandall Stewart 	 * destination address basis.
4446f8829a4aSRandall Stewart 	 */
4447f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4448a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4449a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4450a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4451a21779f0SRandall Stewart 		}
4452f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4453f8829a4aSRandall Stewart 		net->net_ack = 0;
4454f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4455f8829a4aSRandall Stewart 
4456f8829a4aSRandall Stewart 		/*
445742551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
445842551e99SRandall Stewart 		 * SACK processing
4459f8829a4aSRandall Stewart 		 */
4460f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4461f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4462299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4463299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4464299108c5SRandall Stewart 		}
4465f8829a4aSRandall Stewart 	}
4466f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
44674a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
446820b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4469f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4470f8829a4aSRandall Stewart 				accum_moved = 1;
4471f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4472f8829a4aSRandall Stewart 					/*
4473f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4474f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4475f8829a4aSRandall Stewart 					 * values may occur during marking
4476f8829a4aSRandall Stewart 					 */
4477f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4478f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4479f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4480f8829a4aSRandall Stewart 						/*
4481f8829a4aSRandall Stewart 						 * If there was no retran
4482f8829a4aSRandall Stewart 						 * and the address is
4483f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4484f8829a4aSRandall Stewart 						 * there and are now
4485f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4486f8829a4aSRandall Stewart 						 * mark it so.
4487f8829a4aSRandall Stewart 						 */
4488f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4489f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4490f8829a4aSRandall Stewart 					}
4491c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4492b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4493c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4494a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4495a5d547adSRandall Stewart 							    tp1->book_size,
44969a8e3088SMichael Tuexen 							    (uint32_t) (uintptr_t) tp1->whoTo,
4497a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
449880fefe0aSRandall Stewart 						}
4499c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4500c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4501299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4502299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4503299108c5SRandall Stewart 							    tp1);
4504299108c5SRandall Stewart 						}
4505f8829a4aSRandall Stewart 					}
4506f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4507f8829a4aSRandall Stewart 
4508f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4509f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4510f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4511f8829a4aSRandall Stewart 
4512f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4513f8829a4aSRandall Stewart 						/*
4514f8829a4aSRandall Stewart 						 * True non-retransmited
4515f8829a4aSRandall Stewart 						 * chunk
4516f8829a4aSRandall Stewart 						 */
4517f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4518f8829a4aSRandall Stewart 						    tp1->send_size;
4519f8829a4aSRandall Stewart 
4520f8829a4aSRandall Stewart 						/* update RTO too? */
4521f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4522f79aab18SRandall Stewart 							if (rto_ok) {
4523f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4524f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4525f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
452618e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4527899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4528f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4529f79aab18SRandall Stewart 								rto_ok = 0;
4530f79aab18SRandall Stewart 							}
4531f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4532f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4533f79aab18SRandall Stewart 							}
4534f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4535f8829a4aSRandall Stewart 						}
4536f8829a4aSRandall Stewart 					}
4537f8829a4aSRandall Stewart 					/*
4538f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4539f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4540f8829a4aSRandall Stewart 					 * acked for the first time, set the
4541f8829a4aSRandall Stewart 					 * following variables for the
4542f8829a4aSRandall Stewart 					 * corresp destination.
4543f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4544f8829a4aSRandall Stewart 					 * cwnd update.
4545f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4546f8829a4aSRandall Stewart 					 * trigger search for the next
4547f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4548f8829a4aSRandall Stewart 					 */
4549f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4550f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4551f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4552f8829a4aSRandall Stewart 
4553f8829a4aSRandall Stewart 
4554b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4555f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4556f8829a4aSRandall Stewart 						    cum_ack,
4557f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4558f8829a4aSRandall Stewart 						    0,
4559f8829a4aSRandall Stewart 						    0,
4560f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
456180fefe0aSRandall Stewart 					}
4562b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4563f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
456480fefe0aSRandall Stewart 					}
4565f8829a4aSRandall Stewart 				}
4566f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4567f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4568f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4569f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4570f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4571f8829a4aSRandall Stewart #endif
4572f8829a4aSRandall Stewart 				}
457342551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
457442551e99SRandall Stewart 					/* deflate the cwnd */
457542551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
457642551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
457742551e99SRandall Stewart 				}
4578325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4579f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_ACKED;
4580f8829a4aSRandall Stewart 				}
4581325c8c46SMichael Tuexen 			}
4582f8829a4aSRandall Stewart 		} else {
4583f8829a4aSRandall Stewart 			break;
4584f8829a4aSRandall Stewart 		}
4585f8829a4aSRandall Stewart 	}
4586f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4587f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4588f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4589f8829a4aSRandall Stewart 
4590cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4591f8829a4aSRandall Stewart 
4592f8829a4aSRandall Stewart 		/*
4593f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4594f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4595f8829a4aSRandall Stewart 		 * for all dests.
4596f8829a4aSRandall Stewart 		 */
4597f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4598f8829a4aSRandall Stewart 			net->saw_newack = 0;
4599f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4600f8829a4aSRandall Stewart 		}
4601f8829a4aSRandall Stewart 
4602f8829a4aSRandall Stewart 		/*
4603f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4604f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4605f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4606f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4607f8829a4aSRandall Stewart 		 */
4608cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4609cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
46107215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4611cd554309SMichael Tuexen 			wake_him++;
4612cd554309SMichael Tuexen 		}
4613f8829a4aSRandall Stewart 		/*
4614fd60718dSMichael Tuexen 		 * validate the biggest_tsn_acked in the gap acks if strict
4615fd60718dSMichael Tuexen 		 * adherence is wanted.
4616f8829a4aSRandall Stewart 		 */
461720b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4618f8829a4aSRandall Stewart 			/*
4619fd60718dSMichael Tuexen 			 * peer is either confused or we are under attack.
4620fd60718dSMichael Tuexen 			 * We must abort.
4621f8829a4aSRandall Stewart 			 */
4622cd3fd531SMichael Tuexen 			SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4623cd3fd531SMichael Tuexen 			    biggest_tsn_acked, send_s);
4624f8829a4aSRandall Stewart 			goto hopeless_peer;
4625f8829a4aSRandall Stewart 		}
4626f8829a4aSRandall Stewart 	}
4627f8829a4aSRandall Stewart 	/*******************************************/
4628f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4629f8829a4aSRandall Stewart 	/*******************************************/
46307c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4631f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4632f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4633f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4634a5d547adSRandall Stewart 				    stcb, net,
463544249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4636f8829a4aSRandall Stewart 
4637f8829a4aSRandall Stewart 		}
4638f8829a4aSRandall Stewart 	} else {
4639f8829a4aSRandall Stewart 		if (accum_moved) {
4640f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4641f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
464244249214SRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4643f8829a4aSRandall Stewart 			}
4644f8829a4aSRandall Stewart 		}
4645f8829a4aSRandall Stewart 	}
4646f8829a4aSRandall Stewart 	/********************************************/
4647d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4648f8829a4aSRandall Stewart 	/********************************************/
4649f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4650f8829a4aSRandall Stewart 
46517c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
465220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4653f8829a4aSRandall Stewart 			break;
4654f8829a4aSRandall Stewart 		}
4655325c8c46SMichael Tuexen 		if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4656a7ad6026SMichael Tuexen 			if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
4657a7ad6026SMichael Tuexen 				asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
4658a7ad6026SMichael Tuexen #ifdef INVARIANTS
4659a7ad6026SMichael Tuexen 			} else {
4660a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
4661a7ad6026SMichael Tuexen #endif
4662a7ad6026SMichael Tuexen 			}
4663f8829a4aSRandall Stewart 		}
4664d96bef9cSMichael Tuexen 		if ((asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues == 0) &&
4665d96bef9cSMichael Tuexen 		    (asoc->strmout[tp1->rec.data.stream_number].state == SCTP_STREAM_RESET_PENDING) &&
4666d96bef9cSMichael Tuexen 		    TAILQ_EMPTY(&asoc->strmout[tp1->rec.data.stream_number].outqueue)) {
4667d96bef9cSMichael Tuexen 			asoc->trigger_reset = 1;
4668d96bef9cSMichael Tuexen 		}
4669f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
46700ddb4299SMichael Tuexen 		if (PR_SCTP_ENABLED(tp1->flags)) {
4671f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4672f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4673f8829a4aSRandall Stewart 		}
46747c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4675f8829a4aSRandall Stewart 		if (tp1->data) {
467604ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4677f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4678f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
46797c99d56fSMichael Tuexen 			tp1->data = NULL;
4680dd973b0eSMichael Tuexen 			if (asoc->prsctp_supported && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4681f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4682f8829a4aSRandall Stewart 			}
4683f8829a4aSRandall Stewart 		}
4684b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4685f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4686f8829a4aSRandall Stewart 			    cum_ack,
4687f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4688f8829a4aSRandall Stewart 			    0,
4689f8829a4aSRandall Stewart 			    0,
4690f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
469180fefe0aSRandall Stewart 		}
4692689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4693f8829a4aSRandall Stewart 		wake_him++;
46947c99d56fSMichael Tuexen 	}
46957c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
46967c99d56fSMichael Tuexen #ifdef INVARIANTS
4697cd0a4ff6SPedro F. Giffuni 		panic("Warning flight size is positive and should be 0");
46987c99d56fSMichael Tuexen #else
46997c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
47007c99d56fSMichael Tuexen 		    asoc->total_flight);
47017c99d56fSMichael Tuexen #endif
47027c99d56fSMichael Tuexen 		asoc->total_flight = 0;
47037c99d56fSMichael Tuexen 	}
470404ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4705f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4706ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4707ceaad40aSRandall Stewart 		struct socket *so;
4708ceaad40aSRandall Stewart 
4709ceaad40aSRandall Stewart #endif
4710f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4711b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47127215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
471380fefe0aSRandall Stewart 		}
4714ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4715ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4716ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4717ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4718ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4719ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4720ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4721ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4722ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4723ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4724ceaad40aSRandall Stewart 			return;
4725ceaad40aSRandall Stewart 		}
4726ceaad40aSRandall Stewart #endif
4727f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4728ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4729ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4730ceaad40aSRandall Stewart #endif
4731f8829a4aSRandall Stewart 	} else {
4732b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47337215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
473480fefe0aSRandall Stewart 		}
4735f8829a4aSRandall Stewart 	}
4736f8829a4aSRandall Stewart 
473742551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
473820b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4739f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4740f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4741f8829a4aSRandall Stewart 		}
4742f8829a4aSRandall Stewart 	}
4743f8829a4aSRandall Stewart 	/*
4744f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4745f8829a4aSRandall Stewart 	 *
4746f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4747f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4748f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4749f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4750f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4751f8829a4aSRandall Stewart 	 */
4752f8829a4aSRandall Stewart 
4753d9c5cfeaSMichael Tuexen 	if (num_seg) {
4754c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4755d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4756d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4757f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4758f8829a4aSRandall Stewart 
4759f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4760f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4761b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4762f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4763b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4764c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4765c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4766c105859eSRandall Stewart 					    tp1->book_size,
47679a8e3088SMichael Tuexen 					    (uint32_t) (uintptr_t) tp1->whoTo,
4768c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
476980fefe0aSRandall Stewart 				}
4770c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4771c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4772a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
477342551e99SRandall Stewart 				/*
477442551e99SRandall Stewart 				 * To ensure that this increase in
47754a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
47764a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
47774a9ef3f8SMichael Tuexen 				 * cwnd artificially.
477842551e99SRandall Stewart 				 */
477942551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4780f8829a4aSRandall Stewart 				cnt_revoked++;
4781f8829a4aSRandall Stewart 			}
4782f8829a4aSRandall Stewart 		}
4783f8829a4aSRandall Stewart 		if (cnt_revoked) {
4784f8829a4aSRandall Stewart 			reneged_all = 1;
4785f8829a4aSRandall Stewart 		}
4786f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4787f8829a4aSRandall Stewart 	}
4788d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4789d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4790f8829a4aSRandall Stewart 	else
4791d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4792f8829a4aSRandall Stewart 
4793b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4794ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4795ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4796ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4797ca85e948SMichael Tuexen 				/*
4798ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4799ca85e948SMichael Tuexen 				 * count, this is optional.
4800ca85e948SMichael Tuexen 				 */
4801ca85e948SMichael Tuexen 				net->error_count = 0;
4802ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4803ca85e948SMichael Tuexen 					/* addr came good */
4804ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4805ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
48064b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4807ca85e948SMichael Tuexen 				}
4808ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4809ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4810ca85e948SMichael Tuexen 						/*
4811ca85e948SMichael Tuexen 						 * release the alternate,
4812ca85e948SMichael Tuexen 						 * primary is good
4813ca85e948SMichael Tuexen 						 */
4814ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4815ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4816ca85e948SMichael Tuexen 					}
4817ca85e948SMichael Tuexen 				}
4818ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4819ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4820b7d130beSMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT,
4821b7d130beSMichael Tuexen 					    stcb->sctp_ep, stcb, net,
482244249214SRandall Stewart 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
4823ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4824ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4825ca85e948SMichael Tuexen 					/* Done with this net */
4826ca85e948SMichael Tuexen 					net->net_ack = 0;
4827ca85e948SMichael Tuexen 				}
4828ca85e948SMichael Tuexen 				/* restore any doubled timers */
4829ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4830ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4831ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4832ca85e948SMichael Tuexen 				}
4833ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4834ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4835ca85e948SMichael Tuexen 				}
4836ca85e948SMichael Tuexen 			}
4837ca85e948SMichael Tuexen 		}
4838b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4839ca85e948SMichael Tuexen 	}
4840f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4841f8829a4aSRandall Stewart 		/* nothing left in-flight */
4842f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4843f8829a4aSRandall Stewart 			/* stop all timers */
4844f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4845b7d130beSMichael Tuexen 			    stcb, net,
484644249214SRandall Stewart 			    SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4847f8829a4aSRandall Stewart 			net->flight_size = 0;
4848f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4849f8829a4aSRandall Stewart 		}
4850f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4851f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4852f8829a4aSRandall Stewart 	}
4853f8829a4aSRandall Stewart 	/**********************************/
4854f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4855f8829a4aSRandall Stewart 	/**********************************/
4856f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4857f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4858b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4859f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4860f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
486180fefe0aSRandall Stewart 		}
4862f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4863f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4864f8829a4aSRandall Stewart 			/* SWS sender side engages */
4865f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4866f8829a4aSRandall Stewart 		}
4867f8829a4aSRandall Stewart 		/* clean up */
4868f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4869f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4870f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4871f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4872f8829a4aSRandall Stewart 		    ) {
4873f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4874f8829a4aSRandall Stewart 
4875f8829a4aSRandall Stewart 			/*
4876f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4877f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4878f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4879f8829a4aSRandall Stewart 			 */
4880f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4881f8829a4aSRandall Stewart 			    sctp_streamhead);
48822afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4883f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
48842afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4885f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
48862afb3e84SRandall Stewart 				} else {
48872afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
48882afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
48892afb3e84SRandall Stewart 				}
4890f8829a4aSRandall Stewart 			}
4891f8829a4aSRandall Stewart 		}
4892f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4893f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4894f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4895f8829a4aSRandall Stewart 				/* Need to abort here */
4896ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4897f8829a4aSRandall Stewart 
4898f8829a4aSRandall Stewart 		abort_out_now:
4899f8829a4aSRandall Stewart 				*abort_now = 1;
4900f8829a4aSRandall Stewart 				/* XXX */
4901ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
490244249214SRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4903ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4904f8829a4aSRandall Stewart 				return;
4905f8829a4aSRandall Stewart 			} else {
4906ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4907ca85e948SMichael Tuexen 
4908f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4909f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4910f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4911f42a358aSRandall Stewart 				}
4912c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4913b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4914f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4915c39cfa1fSMichael Tuexen 				if (asoc->alternate) {
4916c39cfa1fSMichael Tuexen 					netp = asoc->alternate;
4917c39cfa1fSMichael Tuexen 				} else {
4918c39cfa1fSMichael Tuexen 					netp = asoc->primary_destination;
4919c39cfa1fSMichael Tuexen 				}
4920ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4921f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4922ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4923f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4924ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4925f8829a4aSRandall Stewart 			}
4926f8829a4aSRandall Stewart 			return;
4927f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4928f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4929ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4930ca85e948SMichael Tuexen 
4931f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4932f8829a4aSRandall Stewart 				goto abort_out_now;
4933f8829a4aSRandall Stewart 			}
4934f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4935c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4936b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
493712af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4938c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4939c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4940c39cfa1fSMichael Tuexen 			} else {
4941c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4942c39cfa1fSMichael Tuexen 			}
4943c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4944f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4945ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4946f8829a4aSRandall Stewart 			return;
4947f8829a4aSRandall Stewart 		}
4948f8829a4aSRandall Stewart 	}
4949f8829a4aSRandall Stewart 	/*
4950f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4951f8829a4aSRandall Stewart 	 * HEADS UP.
4952f8829a4aSRandall Stewart 	 */
4953f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4954f8829a4aSRandall Stewart 		net->net_ack = 0;
4955f8829a4aSRandall Stewart 	}
4956f8829a4aSRandall Stewart 
4957f8829a4aSRandall Stewart 	/*
4958f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4959f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4960f8829a4aSRandall Stewart 	 * automatically ensure that.
4961f8829a4aSRandall Stewart 	 */
49627c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
496320083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
496420083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
4965f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
4966f8829a4aSRandall Stewart 	}
4967cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4968f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4969f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4970f8829a4aSRandall Stewart 	}
4971b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4972b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4973f8829a4aSRandall Stewart 
4974f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
4975f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
4976f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
4977f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
4978f8829a4aSRandall Stewart 	}
4979f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
498020b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4981f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
4982f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
4983f8829a4aSRandall Stewart 	}
498442551e99SRandall Stewart 	/*
498542551e99SRandall Stewart 	 * CMT Fast recovery
498642551e99SRandall Stewart 	 */
4987f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4988f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
4989f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
4990f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
4991f8829a4aSRandall Stewart 		}
4992f8829a4aSRandall Stewart 	}
4993f8829a4aSRandall Stewart 
4994f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
4995b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4996f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
499744fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
499880fefe0aSRandall Stewart 	}
4999f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
500044fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5001f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5002f8829a4aSRandall Stewart 		/* SWS sender side engages */
5003f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5004f8829a4aSRandall Stewart 	}
50055e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
50065e54f665SRandall Stewart 		win_probe_recovery = 1;
50075e54f665SRandall Stewart 	}
5008f8829a4aSRandall Stewart 	/*
5009f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5010f8829a4aSRandall Stewart 	 * outstanding data.
5011f8829a4aSRandall Stewart 	 */
5012bff64a4dSRandall Stewart 	done_once = 0;
5013a5d547adSRandall Stewart again:
5014a5d547adSRandall Stewart 	j = 0;
5015f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
50165e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5017c105859eSRandall Stewart 			win_probe_recovered = 1;
50185e54f665SRandall Stewart 			/*-
50195e54f665SRandall Stewart 			 * Find first chunk that was used with
50205e54f665SRandall Stewart 			 * window probe and clear the event. Put
50215e54f665SRandall Stewart 			 * it back into the send queue as if has
50225e54f665SRandall Stewart 			 * not been sent.
50235e54f665SRandall Stewart 			 */
50245e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
50255e54f665SRandall Stewart 				if (tp1->window_probe) {
50267215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
50275e54f665SRandall Stewart 					break;
50285e54f665SRandall Stewart 				}
50295e54f665SRandall Stewart 			}
50305e54f665SRandall Stewart 		}
5031f8829a4aSRandall Stewart 		if (net->flight_size) {
5032a5d547adSRandall Stewart 			j++;
5033cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5034f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5035f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5036cd554309SMichael Tuexen 			}
50375171328bSRandall Stewart 			if (net->window_probe) {
5038cd554309SMichael Tuexen 				net->window_probe = 0;
50395171328bSRandall Stewart 			}
5040c105859eSRandall Stewart 		} else {
50415171328bSRandall Stewart 			if (net->window_probe) {
50425171328bSRandall Stewart 				/*
50435171328bSRandall Stewart 				 * In window probes we must assure a timer
50445171328bSRandall Stewart 				 * is still running there
50455171328bSRandall Stewart 				 */
50465171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
50475171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
50485171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
50495171328bSRandall Stewart 
50505171328bSRandall Stewart 				}
50515171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5052c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5053c105859eSRandall Stewart 				    stcb, net,
505444249214SRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
5055c105859eSRandall Stewart 			}
5056f8829a4aSRandall Stewart 		}
5057f8829a4aSRandall Stewart 	}
5058bff64a4dSRandall Stewart 	if ((j == 0) &&
5059bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5060bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5061c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5062bff64a4dSRandall Stewart 	    (done_once == 0)) {
50630c0982b8SRandall Stewart 		/*
50640c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
50650c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
50660c0982b8SRandall Stewart 		 */
50670c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5068a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5069a5d547adSRandall Stewart 				net->flight_size = 0;
5070a5d547adSRandall Stewart 			}
5071a5d547adSRandall Stewart 			asoc->total_flight = 0;
5072a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5073a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5074a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5075a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5076c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5077c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5078a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5079791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5080a5d547adSRandall Stewart 				}
5081a5d547adSRandall Stewart 			}
50820c0982b8SRandall Stewart 		}
5083bff64a4dSRandall Stewart 		done_once = 1;
5084a5d547adSRandall Stewart 		goto again;
5085a5d547adSRandall Stewart 	}
5086cd554309SMichael Tuexen 	/*********************************************/
5087cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5088cd554309SMichael Tuexen 	/* (section 4.2)                             */
5089cd554309SMichael Tuexen 	/*********************************************/
5090cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
509120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5092dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5093dfb11ef8SRandall Stewart 	}
5094830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5095dd973b0eSMichael Tuexen 	if ((asoc->prsctp_supported) && (asoc->pr_sctp_cnt > 0)) {
5096830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5097830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5098830d754dSRandall Stewart 
5099830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5100830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5101830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
510220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5103830d754dSRandall Stewart 			/*
5104493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
5105830d754dSRandall Stewart 			 */
51060c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
51070c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
51080c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
51090c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
51100c0982b8SRandall Stewart 			}
511120b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5112830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
51130c0982b8SRandall Stewart 			} else if (lchk) {
51140c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
511544fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
51160c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
51170c0982b8SRandall Stewart 				}
5118830d754dSRandall Stewart 			}
5119830d754dSRandall Stewart 		}
5120830d754dSRandall Stewart 		if (lchk) {
5121830d754dSRandall Stewart 			/* Assure a timer is up */
5122830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5123830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5124830d754dSRandall Stewart 		}
5125830d754dSRandall Stewart 	}
5126b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5127f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5128f8829a4aSRandall Stewart 		    a_rwnd,
5129f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5130f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5131f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
513280fefe0aSRandall Stewart 	}
5133f8829a4aSRandall Stewart }
5134f8829a4aSRandall Stewart 
5135f8829a4aSRandall Stewart void
51367215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5137f8829a4aSRandall Stewart {
5138f8829a4aSRandall Stewart 	/* Copy cum-ack */
5139f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5140f8829a4aSRandall Stewart 
5141f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5142f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5143f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5144f8829a4aSRandall Stewart 
5145f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5146899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5147f8829a4aSRandall Stewart }
5148f8829a4aSRandall Stewart 
5149f8829a4aSRandall Stewart static void
5150f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5151f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5152f8829a4aSRandall Stewart {
5153f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5154f8829a4aSRandall Stewart 	struct sctp_association *asoc;
515544249214SRandall Stewart 	uint32_t tt;
515644249214SRandall Stewart 	int need_reasm_check = 0, old;
5157f8829a4aSRandall Stewart 
5158f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5159f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
516044249214SRandall Stewart 	if (asoc->idata_supported) {
516144249214SRandall Stewart 		old = 0;
516244249214SRandall Stewart 	} else {
516344249214SRandall Stewart 		old = 1;
516444249214SRandall Stewart 	}
5165f8829a4aSRandall Stewart 	/*
5166f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
516744249214SRandall Stewart 	 * came in.
5168f8829a4aSRandall Stewart 	 */
516944249214SRandall Stewart 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next_instrm, nctl) {
517044249214SRandall Stewart 		if (SCTP_MSGID_GE(old, tt, ctl->sinfo_ssn)) {
5171f8829a4aSRandall Stewart 			/* this is deliverable now */
517244249214SRandall Stewart 			if (((ctl->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
517344249214SRandall Stewart 				if (ctl->on_strm_q) {
517444249214SRandall Stewart 					if (ctl->on_strm_q == SCTP_ON_ORDERED) {
517544249214SRandall Stewart 						TAILQ_REMOVE(&strmin->inqueue, ctl, next_instrm);
517644249214SRandall Stewart 					} else if (ctl->on_strm_q == SCTP_ON_UNORDERED) {
517744249214SRandall Stewart 						TAILQ_REMOVE(&strmin->uno_inqueue, ctl, next_instrm);
517898d5fd97SMichael Tuexen #ifdef INVARIANTS
517944249214SRandall Stewart 					} else {
518044249214SRandall Stewart 						panic("strmin: %p ctl: %p unknown %d",
518144249214SRandall Stewart 						    strmin, ctl, ctl->on_strm_q);
518298d5fd97SMichael Tuexen #endif
518344249214SRandall Stewart 					}
518444249214SRandall Stewart 					ctl->on_strm_q = 0;
518544249214SRandall Stewart 				}
5186f8829a4aSRandall Stewart 				/* subtract pending on streams */
5187f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= ctl->length;
5188f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
5189f8829a4aSRandall Stewart 				/* deliver it to at least the delivery-q */
5190f8829a4aSRandall Stewart 				if (stcb->sctp_socket) {
5191b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5192f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
5193f8829a4aSRandall Stewart 					    ctl,
519444249214SRandall Stewart 					    &stcb->sctp_socket->so_rcv,
519544249214SRandall Stewart 					    1, SCTP_READ_LOCK_HELD,
519644249214SRandall Stewart 					    SCTP_SO_NOT_LOCKED);
519744249214SRandall Stewart 				}
519844249214SRandall Stewart 			} else {
519944249214SRandall Stewart 				/* Its a fragmented message */
520044249214SRandall Stewart 				if (ctl->first_frag_seen) {
520144249214SRandall Stewart 					/*
520244249214SRandall Stewart 					 * Make it so this is next to
520344249214SRandall Stewart 					 * deliver, we restore later
520444249214SRandall Stewart 					 */
520544249214SRandall Stewart 					strmin->last_sequence_delivered = ctl->sinfo_ssn - 1;
520644249214SRandall Stewart 					need_reasm_check = 1;
520744249214SRandall Stewart 					break;
520844249214SRandall Stewart 				}
5209f8829a4aSRandall Stewart 			}
5210f8829a4aSRandall Stewart 		} else {
5211f8829a4aSRandall Stewart 			/* no more delivery now. */
5212f8829a4aSRandall Stewart 			break;
5213f8829a4aSRandall Stewart 		}
5214f8829a4aSRandall Stewart 	}
521544249214SRandall Stewart 	if (need_reasm_check) {
521644249214SRandall Stewart 		int ret;
521744249214SRandall Stewart 
521844249214SRandall Stewart 		ret = sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin);
521944249214SRandall Stewart 		if (SCTP_MSGID_GT(old, tt, strmin->last_sequence_delivered)) {
522044249214SRandall Stewart 			/* Restore the next to deliver unless we are ahead */
522144249214SRandall Stewart 			strmin->last_sequence_delivered = tt;
522244249214SRandall Stewart 		}
522344249214SRandall Stewart 		if (ret == 0) {
522444249214SRandall Stewart 			/* Left the front Partial one on */
522544249214SRandall Stewart 			return;
522644249214SRandall Stewart 		}
522744249214SRandall Stewart 		need_reasm_check = 0;
522844249214SRandall Stewart 	}
5229f8829a4aSRandall Stewart 	/*
5230f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5231f8829a4aSRandall Stewart 	 * now ready.
5232f8829a4aSRandall Stewart 	 */
5233f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
523444249214SRandall Stewart 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next_instrm, nctl) {
5235f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
523644249214SRandall Stewart 			if (((ctl->sinfo_flags >> 8) & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG) {
5237f8829a4aSRandall Stewart 				/* this is deliverable now */
523844249214SRandall Stewart 				if (ctl->on_strm_q) {
523944249214SRandall Stewart 					if (ctl->on_strm_q == SCTP_ON_ORDERED) {
524044249214SRandall Stewart 						TAILQ_REMOVE(&strmin->inqueue, ctl, next_instrm);
524144249214SRandall Stewart 					} else if (ctl->on_strm_q == SCTP_ON_UNORDERED) {
524244249214SRandall Stewart 						TAILQ_REMOVE(&strmin->uno_inqueue, ctl, next_instrm);
524398d5fd97SMichael Tuexen #ifdef INVARIANTS
524444249214SRandall Stewart 					} else {
524544249214SRandall Stewart 						panic("strmin: %p ctl: %p unknown %d",
524644249214SRandall Stewart 						    strmin, ctl, ctl->on_strm_q);
524798d5fd97SMichael Tuexen #endif
524844249214SRandall Stewart 					}
524944249214SRandall Stewart 					ctl->on_strm_q = 0;
525044249214SRandall Stewart 				}
5251f8829a4aSRandall Stewart 				/* subtract pending on streams */
5252f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= ctl->length;
5253f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
5254f8829a4aSRandall Stewart 				/* deliver it to at least the delivery-q */
5255f8829a4aSRandall Stewart 				strmin->last_sequence_delivered = ctl->sinfo_ssn;
5256f8829a4aSRandall Stewart 				if (stcb->sctp_socket) {
5257b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5258f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
5259f8829a4aSRandall Stewart 					    ctl,
526044249214SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
526144249214SRandall Stewart 					    SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
526277acdc25SRandall Stewart 
5263f8829a4aSRandall Stewart 				}
5264f8829a4aSRandall Stewart 				tt = strmin->last_sequence_delivered + 1;
5265f8829a4aSRandall Stewart 			} else {
526644249214SRandall Stewart 				/* Its a fragmented message */
526744249214SRandall Stewart 				if (ctl->first_frag_seen) {
526844249214SRandall Stewart 					/*
526944249214SRandall Stewart 					 * Make it so this is next to
527044249214SRandall Stewart 					 * deliver
527144249214SRandall Stewart 					 */
527244249214SRandall Stewart 					strmin->last_sequence_delivered = ctl->sinfo_ssn - 1;
527344249214SRandall Stewart 					need_reasm_check = 1;
5274f8829a4aSRandall Stewart 					break;
5275f8829a4aSRandall Stewart 				}
5276f8829a4aSRandall Stewart 			}
527744249214SRandall Stewart 		} else {
527844249214SRandall Stewart 			break;
527944249214SRandall Stewart 		}
528044249214SRandall Stewart 	}
528144249214SRandall Stewart 	if (need_reasm_check) {
528244249214SRandall Stewart 		(void)sctp_deliver_reasm_check(stcb, &stcb->asoc, strmin);
528344249214SRandall Stewart 	}
5284f8829a4aSRandall Stewart }
5285f8829a4aSRandall Stewart 
5286*8e1b295fSMichael Tuexen 
52878933fa13SRandall Stewart static void
52888933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
52898933fa13SRandall Stewart     struct sctp_association *asoc,
5290*8e1b295fSMichael Tuexen     uint16_t stream, uint32_t seq, int ordered, int old)
52918933fa13SRandall Stewart {
529244249214SRandall Stewart 	struct sctp_queued_to_read *control;
529344249214SRandall Stewart 	struct sctp_stream_in *strm;
52944a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
52958933fa13SRandall Stewart 
52968933fa13SRandall Stewart 	/*
529744249214SRandall Stewart 	 * For now large messages held on the stream reasm that are complete
52984a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
52994a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
53004a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
53014a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
53024a9ef3f8SMichael Tuexen 	 * queue.
53038933fa13SRandall Stewart 	 */
530444249214SRandall Stewart 	strm = &asoc->strmin[stream];
5305*8e1b295fSMichael Tuexen 	control = find_reasm_entry(strm, (uint32_t) seq, ordered, old);
530644249214SRandall Stewart 	if (control == NULL) {
530744249214SRandall Stewart 		/* Not found */
530844249214SRandall Stewart 		return;
53098933fa13SRandall Stewart 	}
531044249214SRandall Stewart 	TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
531144249214SRandall Stewart 		/* Purge hanging chunks */
531244249214SRandall Stewart 		TAILQ_REMOVE(&control->reasm, chk, sctp_next);
53138933fa13SRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
53148933fa13SRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
53158933fa13SRandall Stewart 		if (chk->data) {
53168933fa13SRandall Stewart 			sctp_m_freem(chk->data);
53178933fa13SRandall Stewart 			chk->data = NULL;
53188933fa13SRandall Stewart 		}
5319689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
53208933fa13SRandall Stewart 	}
532144249214SRandall Stewart 	TAILQ_REMOVE(&strm->inqueue, control, next_instrm);
532244249214SRandall Stewart 	if (control->on_read_q == 0) {
532344249214SRandall Stewart 		sctp_free_remote_addr(control->whoFrom);
532444249214SRandall Stewart 		if (control->data) {
532544249214SRandall Stewart 			sctp_m_freem(control->data);
532644249214SRandall Stewart 			control->data = NULL;
532744249214SRandall Stewart 		}
532844249214SRandall Stewart 		sctp_free_a_readq(stcb, control);
53298933fa13SRandall Stewart 	}
53308933fa13SRandall Stewart }
53318933fa13SRandall Stewart 
53328933fa13SRandall Stewart 
5333f8829a4aSRandall Stewart void
5334f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5335b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5336b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5337f8829a4aSRandall Stewart {
5338f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5339f8829a4aSRandall Stewart 	/*
5340f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5341f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5342f8829a4aSRandall Stewart 	 *
5343f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5344f8829a4aSRandall Stewart 	 *
5345f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5346f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5347f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5348f8829a4aSRandall Stewart 	 * report where we are.
5349f8829a4aSRandall Stewart 	 */
5350f8829a4aSRandall Stewart 	struct sctp_association *asoc;
53517898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
53527215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
53538933fa13SRandall Stewart 	uint32_t str_seq;
5354f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
53558933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5356f8829a4aSRandall Stewart 
5357f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5358f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5359ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5360ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5361f8829a4aSRandall Stewart 		return;
5362f8829a4aSRandall Stewart 	}
5363f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5364f8829a4aSRandall Stewart 	/*************************************************************/
5365f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5366f8829a4aSRandall Stewart 	/*************************************************************/
5367f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5368f8829a4aSRandall Stewart 
536920b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5370f8829a4aSRandall Stewart 		/* Already got there ... */
5371f8829a4aSRandall Stewart 		return;
5372f8829a4aSRandall Stewart 	}
5373f8829a4aSRandall Stewart 	/*
5374f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5375f8829a4aSRandall Stewart 	 * gap
5376f8829a4aSRandall Stewart 	 */
5377b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
537877acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
53792afb3e84SRandall Stewart 	if (gap >= m_size) {
5380f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5381ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
5382ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
538317205eccSRandall Stewart 
5384f8829a4aSRandall Stewart 			/*
5385f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
538617205eccSRandall Stewart 			 * give out). This must be an attacker.
5387f8829a4aSRandall Stewart 			 */
538817205eccSRandall Stewart 			*abort_flag = 1;
5389ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg),
5390ff1ffd74SMichael Tuexen 			    "New cum ack %8.8x too high, highest TSN %8.8x",
5391ff1ffd74SMichael Tuexen 			    new_cum_tsn, asoc->highest_tsn_inside_map);
5392ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
539344249214SRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5394ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5395f8829a4aSRandall Stewart 			return;
5396f8829a4aSRandall Stewart 		}
5397207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
539877acdc25SRandall Stewart 
53992afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
54002afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
540177acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
540277acdc25SRandall Stewart 
5403b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5404830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
540577acdc25SRandall Stewart 
5406b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5407f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
540880fefe0aSRandall Stewart 		}
54092afb3e84SRandall Stewart 	} else {
54102afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
54112afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
54127898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
54137898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
54148933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
541520b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
54167898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5417b5c16493SMichael Tuexen 				}
5418b5c16493SMichael Tuexen 			}
5419b5c16493SMichael Tuexen 		}
5420f8829a4aSRandall Stewart 	}
5421f8829a4aSRandall Stewart 	/*************************************************************/
5422f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5423f8829a4aSRandall Stewart 	/*************************************************************/
5424f8829a4aSRandall Stewart 
542544249214SRandall Stewart 	/* This is now done as part of clearing up the stream/seq */
542644249214SRandall Stewart 
54278933fa13SRandall Stewart 	/*******************************************************/
54288933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
54298933fa13SRandall Stewart 	/* delivery issues as needed.                       */
54308933fa13SRandall Stewart 	/*******************************************************/
5431f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5432671d309cSRandall Stewart 	if (m && fwd_sz) {
5433f8829a4aSRandall Stewart 		/* New method. */
5434d61a0ae0SRandall Stewart 		unsigned int num_str;
543544249214SRandall Stewart 		uint32_t sequence;
543644249214SRandall Stewart 		uint16_t stream;
5437*8e1b295fSMichael Tuexen 		uint16_t ordered, flags;
543844249214SRandall Stewart 		int old;
5439671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
544044249214SRandall Stewart 		struct sctp_strseq_mid *stseq_m, strseqbuf_m;
5441671d309cSRandall Stewart 
5442671d309cSRandall Stewart 		offset += sizeof(*fwd);
5443f8829a4aSRandall Stewart 
54448933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
544544249214SRandall Stewart 		if (asoc->idata_supported) {
544644249214SRandall Stewart 			num_str = fwd_sz / sizeof(struct sctp_strseq_mid);
544744249214SRandall Stewart 			old = 0;
544844249214SRandall Stewart 		} else {
5449f8829a4aSRandall Stewart 			num_str = fwd_sz / sizeof(struct sctp_strseq);
545044249214SRandall Stewart 			old = 1;
545144249214SRandall Stewart 		}
5452f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
545344249214SRandall Stewart 			if (asoc->idata_supported) {
545444249214SRandall Stewart 				stseq_m = (struct sctp_strseq_mid *)sctp_m_getptr(m, offset,
545544249214SRandall Stewart 				    sizeof(struct sctp_strseq_mid),
545644249214SRandall Stewart 				    (uint8_t *) & strseqbuf_m);
545744249214SRandall Stewart 				offset += sizeof(struct sctp_strseq_mid);
545844249214SRandall Stewart 				if (stseq_m == NULL) {
545944249214SRandall Stewart 					break;
546044249214SRandall Stewart 				}
546144249214SRandall Stewart 				stream = ntohs(stseq_m->stream);
546244249214SRandall Stewart 				sequence = ntohl(stseq_m->msg_id);
5463*8e1b295fSMichael Tuexen 				flags = ntohs(stseq_m->flags);
5464*8e1b295fSMichael Tuexen 				if (flags & PR_SCTP_UNORDERED_FLAG) {
5465*8e1b295fSMichael Tuexen 					ordered = 0;
5466*8e1b295fSMichael Tuexen 				} else {
5467*8e1b295fSMichael Tuexen 					ordered = 1;
5468*8e1b295fSMichael Tuexen 				}
546944249214SRandall Stewart 			} else {
5470671d309cSRandall Stewart 				stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5471671d309cSRandall Stewart 				    sizeof(struct sctp_strseq),
5472671d309cSRandall Stewart 				    (uint8_t *) & strseqbuf);
5473671d309cSRandall Stewart 				offset += sizeof(struct sctp_strseq);
54742afb3e84SRandall Stewart 				if (stseq == NULL) {
5475671d309cSRandall Stewart 					break;
54762afb3e84SRandall Stewart 				}
547744249214SRandall Stewart 				stream = ntohs(stseq->stream);
547844249214SRandall Stewart 				sequence = (uint32_t) ntohs(stseq->sequence);
5479*8e1b295fSMichael Tuexen 				ordered = 1;
548044249214SRandall Stewart 			}
5481f8829a4aSRandall Stewart 			/* Convert */
54828933fa13SRandall Stewart 
5483f8829a4aSRandall Stewart 			/* now process */
54848933fa13SRandall Stewart 
54858933fa13SRandall Stewart 			/*
54868933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
54878933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
54888933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
54898933fa13SRandall Stewart 			 */
549044249214SRandall Stewart 			if (stream >= asoc->streamincnt) {
54912afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
54922afb3e84SRandall Stewart 				break;
5493f8829a4aSRandall Stewart 			}
549444249214SRandall Stewart 			if ((asoc->str_of_pdapi == stream) &&
549544249214SRandall Stewart 			    (asoc->ssn_of_pdapi == sequence)) {
54968933fa13SRandall Stewart 				/*
54978933fa13SRandall Stewart 				 * If this is the one we were partially
54988933fa13SRandall Stewart 				 * delivering now then we no longer are.
54998933fa13SRandall Stewart 				 * Note this will change with the reassembly
55008933fa13SRandall Stewart 				 * re-write.
55018933fa13SRandall Stewart 				 */
55028933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
55038933fa13SRandall Stewart 			}
550444249214SRandall Stewart 			strm = &asoc->strmin[stream];
5505*8e1b295fSMichael Tuexen 			sctp_flush_reassm_for_str_seq(stcb, asoc, stream, sequence, ordered, old);
55068933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
550744249214SRandall Stewart 				if ((ctl->sinfo_stream == stream) &&
550844249214SRandall Stewart 				    (ctl->sinfo_ssn == sequence)) {
550944249214SRandall Stewart 					str_seq = (stream << 16) | (0x0000ffff & sequence);
55108933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
55118933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
551244249214SRandall Stewart 					ctl->end_added = 1;
551344249214SRandall Stewart 					if (ctl->on_strm_q == SCTP_ON_ORDERED) {
551444249214SRandall Stewart 						TAILQ_REMOVE(&strm->inqueue, ctl, next_instrm);
551544249214SRandall Stewart 					} else if (ctl->on_strm_q == SCTP_ON_UNORDERED) {
551644249214SRandall Stewart 						TAILQ_REMOVE(&strm->uno_inqueue, ctl, next_instrm);
551798d5fd97SMichael Tuexen #ifdef INVARIANTS
551844249214SRandall Stewart 					} else if (ctl->on_strm_q) {
551944249214SRandall Stewart 						panic("strm: %p ctl: %p unknown %d",
552044249214SRandall Stewart 						    strm, ctl, ctl->on_strm_q);
552198d5fd97SMichael Tuexen #endif
552244249214SRandall Stewart 					}
552344249214SRandall Stewart 					ctl->on_strm_q = 0;
55248933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5525810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5526810ec536SMichael Tuexen 					    stcb,
55278933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5528810ec536SMichael Tuexen 					    (void *)&str_seq,
5529810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
55308933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
55318933fa13SRandall Stewart 					break;
553244249214SRandall Stewart 				} else if ((ctl->sinfo_stream == stream) &&
553344249214SRandall Stewart 				    SCTP_MSGID_GT(old, ctl->sinfo_ssn, sequence)) {
55348933fa13SRandall Stewart 					/* We are past our victim SSN */
55358933fa13SRandall Stewart 					break;
55368933fa13SRandall Stewart 				}
55378933fa13SRandall Stewart 			}
553844249214SRandall Stewart 			if (SCTP_MSGID_GT(old, sequence, strm->last_sequence_delivered)) {
5539f8829a4aSRandall Stewart 				/* Update the sequence number */
554044249214SRandall Stewart 				strm->last_sequence_delivered = sequence;
5541f8829a4aSRandall Stewart 			}
5542f8829a4aSRandall Stewart 			/* now kick the stream the new way */
554304ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5544f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5545f8829a4aSRandall Stewart 		}
55468933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5547f8829a4aSRandall Stewart 	}
55487898f408SRandall Stewart 	/*
55497898f408SRandall Stewart 	 * Now slide thing forward.
55507898f408SRandall Stewart 	 */
55517898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
5552f8829a4aSRandall Stewart }
5553