xref: /freebsd/sys/netinet/sctp_indata.c (revision e432298ade8c0f7cc2bc0969c318086dc12ce86a)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4807aad63SMichael Tuexen  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5f8829a4aSRandall Stewart  *
6f8829a4aSRandall Stewart  * Redistribution and use in source and binary forms, with or without
7f8829a4aSRandall Stewart  * modification, are permitted provided that the following conditions are met:
8f8829a4aSRandall Stewart  *
9f8829a4aSRandall Stewart  * a) Redistributions of source code must retain the above copyright notice,
10f8829a4aSRandall Stewart  *    this list of conditions and the following disclaimer.
11f8829a4aSRandall Stewart  *
12f8829a4aSRandall Stewart  * b) Redistributions in binary form must reproduce the above copyright
13f8829a4aSRandall Stewart  *    notice, this list of conditions and the following disclaimer in
14f8829a4aSRandall Stewart  *    the documentation and/or other materials provided with the distribution.
15f8829a4aSRandall Stewart  *
16f8829a4aSRandall Stewart  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17f8829a4aSRandall Stewart  *    contributors may be used to endorse or promote products derived
18f8829a4aSRandall Stewart  *    from this software without specific prior written permission.
19f8829a4aSRandall Stewart  *
20f8829a4aSRandall Stewart  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f8829a4aSRandall Stewart  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22f8829a4aSRandall Stewart  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f8829a4aSRandall Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24f8829a4aSRandall Stewart  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25f8829a4aSRandall Stewart  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26f8829a4aSRandall Stewart  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27f8829a4aSRandall Stewart  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28f8829a4aSRandall Stewart  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29f8829a4aSRandall Stewart  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30f8829a4aSRandall Stewart  * THE POSSIBILITY OF SUCH DAMAGE.
31f8829a4aSRandall Stewart  */
32f8829a4aSRandall Stewart 
33f8829a4aSRandall Stewart #include <sys/cdefs.h>
34f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
35f8829a4aSRandall Stewart 
36f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
3842551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
41f8829a4aSRandall Stewart #include <netinet/sctputil.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
47f8829a4aSRandall Stewart 
48d50c1d79SRandall Stewart 
49f8829a4aSRandall Stewart /*
50f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
51f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
52f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
53f8829a4aSRandall Stewart  *
54f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
55f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
56f8829a4aSRandall Stewart  * the list.
57f8829a4aSRandall Stewart  */
58f8829a4aSRandall Stewart 
5972fb6fdbSRandall Stewart void
60f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
61f8829a4aSRandall Stewart {
62b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
63f8829a4aSRandall Stewart }
64f8829a4aSRandall Stewart 
65f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
6672fb6fdbSRandall Stewart uint32_t
67f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
68f8829a4aSRandall Stewart {
69b3f1ea41SRandall Stewart 	uint32_t calc = 0;
70f8829a4aSRandall Stewart 
71f8829a4aSRandall Stewart 	/*
72f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
73f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
74f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
75f8829a4aSRandall Stewart 	 * associations data is taken into account.
76f8829a4aSRandall Stewart 	 */
77f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
78f8829a4aSRandall Stewart 		return (calc);
79f8829a4aSRandall Stewart 
80f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
81f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
82f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
83f8829a4aSRandall Stewart 		/* Full rwnd granted */
84b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
85f8829a4aSRandall Stewart 		return (calc);
86f8829a4aSRandall Stewart 	}
87f8829a4aSRandall Stewart 	/* get actual space */
88f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
89f8829a4aSRandall Stewart 
90f8829a4aSRandall Stewart 	/*
91f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
92f8829a4aSRandall Stewart 	 * for putting up.
93f8829a4aSRandall Stewart 	 */
9444fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
9544fbe462SRandall Stewart 	    asoc->cnt_on_reasm_queue * MSIZE));
9644fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
9744fbe462SRandall Stewart 	    asoc->cnt_on_all_streams * MSIZE));
98f8829a4aSRandall Stewart 
99f8829a4aSRandall Stewart 	if (calc == 0) {
100f8829a4aSRandall Stewart 		/* out of space */
101f8829a4aSRandall Stewart 		return (calc);
102f8829a4aSRandall Stewart 	}
103f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1042afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
105b3f1ea41SRandall Stewart 	/*
106b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
107b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
108f8829a4aSRandall Stewart 	 */
109b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
110b3f1ea41SRandall Stewart 		calc = 1;
1112afb3e84SRandall Stewart 	}
112b3f1ea41SRandall Stewart 	return (calc);
113f8829a4aSRandall Stewart }
114f8829a4aSRandall Stewart 
115f8829a4aSRandall Stewart 
116f8829a4aSRandall Stewart 
117f8829a4aSRandall Stewart /*
118f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
119f8829a4aSRandall Stewart  */
120f8829a4aSRandall Stewart struct sctp_queued_to_read *
121f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
122f8829a4aSRandall Stewart     struct sctp_nets *net,
123f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
124f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
125f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
126f8829a4aSRandall Stewart     struct mbuf *dm)
127f8829a4aSRandall Stewart {
128f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
129f8829a4aSRandall Stewart 
130f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
131f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
132f8829a4aSRandall Stewart 		goto failed_build;
133f8829a4aSRandall Stewart 	}
134f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
135f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
136f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
137f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
1387215cc1bSMichael Tuexen 	read_queue_e->sinfo_context = context;
139f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
140f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
141f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
142f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
143f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
144f8829a4aSRandall Stewart 	read_queue_e->length = 0;
145f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
146f8829a4aSRandall Stewart 	read_queue_e->data = dm;
147139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
148f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
14917205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
150f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
151f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
152f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
153f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1549a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
15503b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
156f8829a4aSRandall Stewart failed_build:
157f8829a4aSRandall Stewart 	return (read_queue_e);
158f8829a4aSRandall Stewart }
159f8829a4aSRandall Stewart 
160f8829a4aSRandall Stewart 
161f8829a4aSRandall Stewart /*
162f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
163f8829a4aSRandall Stewart  */
164f8829a4aSRandall Stewart static struct sctp_queued_to_read *
165f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
166f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
167f8829a4aSRandall Stewart {
168f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
169f8829a4aSRandall Stewart 
170f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
171f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
172f8829a4aSRandall Stewart 		goto failed_build;
173f8829a4aSRandall Stewart 	}
174f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
175f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
176f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
177f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
178f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
179f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
180f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
181f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
182f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
183f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
18417205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
185f8829a4aSRandall Stewart 	read_queue_e->length = 0;
186f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
187f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
188f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
189f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
190f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
191139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
192f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
193f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1949a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
19503b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
196f8829a4aSRandall Stewart failed_build:
197f8829a4aSRandall Stewart 	return (read_queue_e);
198f8829a4aSRandall Stewart }
199f8829a4aSRandall Stewart 
200f8829a4aSRandall Stewart 
201f8829a4aSRandall Stewart struct mbuf *
202e2e7c62eSMichael Tuexen sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
203f8829a4aSRandall Stewart {
204e2e7c62eSMichael Tuexen 	struct sctp_extrcvinfo *seinfo;
205f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
206e2e7c62eSMichael Tuexen 	struct sctp_rcvinfo *rcvinfo;
207e2e7c62eSMichael Tuexen 	struct sctp_nxtinfo *nxtinfo;
208f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
209f8829a4aSRandall Stewart 	struct mbuf *ret;
210f8829a4aSRandall Stewart 	int len;
211e2e7c62eSMichael Tuexen 	int use_extended;
212e2e7c62eSMichael Tuexen 	int provide_nxt;
213f8829a4aSRandall Stewart 
214e2e7c62eSMichael Tuexen 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
215e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
216e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
217e2e7c62eSMichael Tuexen 		/* user does not want any ancillary data */
218f8829a4aSRandall Stewart 		return (NULL);
219f8829a4aSRandall Stewart 	}
220e2e7c62eSMichael Tuexen 	len = 0;
221e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
222e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
223e2e7c62eSMichael Tuexen 	}
224e2e7c62eSMichael Tuexen 	seinfo = (struct sctp_extrcvinfo *)sinfo;
225e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
226e2e7c62eSMichael Tuexen 	    (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
227e2e7c62eSMichael Tuexen 		provide_nxt = 1;
228e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
229e2e7c62eSMichael Tuexen 	} else {
230e2e7c62eSMichael Tuexen 		provide_nxt = 0;
231e2e7c62eSMichael Tuexen 	}
232e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
233f8829a4aSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
234f8829a4aSRandall Stewart 			use_extended = 1;
235e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
236f8829a4aSRandall Stewart 		} else {
237e2e7c62eSMichael Tuexen 			use_extended = 0;
238e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
239e2e7c62eSMichael Tuexen 		}
240e2e7c62eSMichael Tuexen 	} else {
241e2e7c62eSMichael Tuexen 		use_extended = 0;
242f8829a4aSRandall Stewart 	}
243f8829a4aSRandall Stewart 
244eb1b1807SGleb Smirnoff 	ret = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
245f8829a4aSRandall Stewart 	if (ret == NULL) {
246f8829a4aSRandall Stewart 		/* No space */
247f8829a4aSRandall Stewart 		return (ret);
248f8829a4aSRandall Stewart 	}
249e2e7c62eSMichael Tuexen 	SCTP_BUF_LEN(ret) = 0;
250e2e7c62eSMichael Tuexen 
251f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct */
252f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
253*e432298aSXin LI 	/*
254*e432298aSXin LI 	 * Make sure that there is no un-initialized padding between the
255*e432298aSXin LI 	 * cmsg header and cmsg data and after the cmsg data.
256*e432298aSXin LI 	 */
257*e432298aSXin LI 	memset(cmh, 0, len);
258e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
259f8829a4aSRandall Stewart 		cmh->cmsg_level = IPPROTO_SCTP;
260e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
261e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_RCVINFO;
262e2e7c62eSMichael Tuexen 		rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
263e2e7c62eSMichael Tuexen 		rcvinfo->rcv_sid = sinfo->sinfo_stream;
264e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
265e2e7c62eSMichael Tuexen 		rcvinfo->rcv_flags = sinfo->sinfo_flags;
266e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
267e2e7c62eSMichael Tuexen 		rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
268e2e7c62eSMichael Tuexen 		rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
269e2e7c62eSMichael Tuexen 		rcvinfo->rcv_context = sinfo->sinfo_context;
270e2e7c62eSMichael Tuexen 		rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
271e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
272e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
273f8829a4aSRandall Stewart 	}
274e2e7c62eSMichael Tuexen 	if (provide_nxt) {
275e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
276e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
277e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_NXTINFO;
278e2e7c62eSMichael Tuexen 		nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
279e2e7c62eSMichael Tuexen 		nxtinfo->nxt_sid = seinfo->sreinfo_next_stream;
280e2e7c62eSMichael Tuexen 		nxtinfo->nxt_flags = 0;
281e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
282e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_UNORDERED;
283e2e7c62eSMichael Tuexen 		}
284e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
285e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
286e2e7c62eSMichael Tuexen 		}
287e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
288e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_COMPLETE;
289e2e7c62eSMichael Tuexen 		}
290e2e7c62eSMichael Tuexen 		nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid;
291e2e7c62eSMichael Tuexen 		nxtinfo->nxt_length = seinfo->sreinfo_next_length;
292e2e7c62eSMichael Tuexen 		nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid;
293e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
294e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
295e2e7c62eSMichael Tuexen 	}
296e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
297e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
298e2e7c62eSMichael Tuexen 		outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
299e2e7c62eSMichael Tuexen 		if (use_extended) {
300e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
301e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_EXTRCV;
302e2e7c62eSMichael Tuexen 			memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
303e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
304e2e7c62eSMichael Tuexen 		} else {
305e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
306e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_SNDRCV;
307e2e7c62eSMichael Tuexen 			*outinfo = *sinfo;
308e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
309e2e7c62eSMichael Tuexen 		}
310e2e7c62eSMichael Tuexen 	}
311f8829a4aSRandall Stewart 	return (ret);
312f8829a4aSRandall Stewart }
313f8829a4aSRandall Stewart 
314139bc87fSRandall Stewart 
31577acdc25SRandall Stewart static void
31677acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
31777acdc25SRandall Stewart {
3189b2e0767SRandall Stewart 	uint32_t gap, i, cumackp1;
31977acdc25SRandall Stewart 	int fnd = 0;
32077acdc25SRandall Stewart 
32177acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
32277acdc25SRandall Stewart 		return;
32377acdc25SRandall Stewart 	}
3249b2e0767SRandall Stewart 	cumackp1 = asoc->cumulative_tsn + 1;
32520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumackp1, tsn)) {
3269b2e0767SRandall Stewart 		/*
3279b2e0767SRandall Stewart 		 * this tsn is behind the cum ack and thus we don't need to
3289b2e0767SRandall Stewart 		 * worry about it being moved from one to the other.
3299b2e0767SRandall Stewart 		 */
3309b2e0767SRandall Stewart 		return;
3319b2e0767SRandall Stewart 	}
33277acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
33377acdc25SRandall Stewart 	if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
334cd3fd531SMichael Tuexen 		SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
33577acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
336b5c16493SMichael Tuexen #ifdef INVARIANTS
33777acdc25SRandall Stewart 		panic("Things are really messed up now!!");
33877acdc25SRandall Stewart #endif
339b5c16493SMichael Tuexen 	}
34077acdc25SRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
34177acdc25SRandall Stewart 	SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
34220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
34377acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
34477acdc25SRandall Stewart 	}
34577acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
34677acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
34720b07a4dSMichael Tuexen 		for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
34877acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
34977acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
35077acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
35177acdc25SRandall Stewart 				fnd = 1;
35277acdc25SRandall Stewart 				break;
35377acdc25SRandall Stewart 			}
35477acdc25SRandall Stewart 		}
35577acdc25SRandall Stewart 		if (!fnd) {
35677acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
35777acdc25SRandall Stewart 		}
35877acdc25SRandall Stewart 	}
35977acdc25SRandall Stewart }
36077acdc25SRandall Stewart 
36117205eccSRandall Stewart 
362f8829a4aSRandall Stewart /*
363f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
364f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
365f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
366f8829a4aSRandall Stewart  */
367f8829a4aSRandall Stewart static void
368f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
369f8829a4aSRandall Stewart {
3704a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
371f8829a4aSRandall Stewart 	uint16_t nxt_todel;
372f8829a4aSRandall Stewart 	uint16_t stream_no;
373f8829a4aSRandall Stewart 	int end = 0;
374f8829a4aSRandall Stewart 	int cntDel;
3754a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *control, *ctl, *nctl;
376f8829a4aSRandall Stewart 
377ad81507eSRandall Stewart 	if (stcb == NULL)
378ad81507eSRandall Stewart 		return;
379ad81507eSRandall Stewart 
380f42a358aSRandall Stewart 	cntDel = stream_no = 0;
381ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
382851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
383ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
384851b7298SRandall Stewart 		/* socket above is long gone or going.. */
385851b7298SRandall Stewart abandon:
386f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
3874a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
388f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
389f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
390f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
391f8829a4aSRandall Stewart 			/*
392f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
393f8829a4aSRandall Stewart 			 * buffer
394f8829a4aSRandall Stewart 			 */
395f8829a4aSRandall Stewart 			if (chk->data) {
396f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
397f8829a4aSRandall Stewart 				chk->data = NULL;
398f8829a4aSRandall Stewart 			}
399f8829a4aSRandall Stewart 			/* Now free the address and data */
400689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4013c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
402f8829a4aSRandall Stewart 		}
403f8829a4aSRandall Stewart 		return;
404f8829a4aSRandall Stewart 	}
405f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4064a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
407f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
408f8829a4aSRandall Stewart 			/* Can't deliver more :< */
409f8829a4aSRandall Stewart 			return;
410f8829a4aSRandall Stewart 		}
411f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
412f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
413f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
414f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
415f8829a4aSRandall Stewart 			/*
416f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
417f8829a4aSRandall Stewart 			 * unordered
418f8829a4aSRandall Stewart 			 */
419f8829a4aSRandall Stewart 			return;
420f8829a4aSRandall Stewart 		}
421f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
422f8829a4aSRandall Stewart 
423f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
424f8829a4aSRandall Stewart 			if (control == NULL) {
425f8829a4aSRandall Stewart 				/* out of memory? */
426f8829a4aSRandall Stewart 				return;
427f8829a4aSRandall Stewart 			}
428f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
429f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
430f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
431f8829a4aSRandall Stewart 				end = 1;
432f8829a4aSRandall Stewart 			else
433f8829a4aSRandall Stewart 				end = 0;
434b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
435f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
436cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
437cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
438f8829a4aSRandall Stewart 			cntDel++;
439f8829a4aSRandall Stewart 		} else {
440f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
441f8829a4aSRandall Stewart 				end = 1;
442f8829a4aSRandall Stewart 			else
443f8829a4aSRandall Stewart 				end = 0;
444b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
445f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
446f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
447f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
448f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
449f8829a4aSRandall Stewart 				/*
450f8829a4aSRandall Stewart 				 * something is very wrong, either
451f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
452f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
453f8829a4aSRandall Stewart 				 * the mbuf chain.
454f8829a4aSRandall Stewart 				 */
455851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
456851b7298SRandall Stewart 					goto abandon;
457851b7298SRandall Stewart 				} else {
458df6e0cc3SRandall Stewart #ifdef INVARIANTS
459ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
460f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
461f8829a4aSRandall Stewart 					}
462f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
463f8829a4aSRandall Stewart 					panic("Bad chunking ??");
464df6e0cc3SRandall Stewart #else
465df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
466df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
467df6e0cc3SRandall Stewart 					}
468df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
469df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
470df6e0cc3SRandall Stewart 
471df6e0cc3SRandall Stewart #endif
472df6e0cc3SRandall Stewart 					goto abandon;
473f8829a4aSRandall Stewart 				}
474851b7298SRandall Stewart 			}
475f8829a4aSRandall Stewart 			cntDel++;
476f8829a4aSRandall Stewart 		}
477f8829a4aSRandall Stewart 		/* pull it we did it */
478f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
479f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
480f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
481f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
482f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
483f8829a4aSRandall Stewart 			}
484f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
485f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
486f8829a4aSRandall Stewart 			}
487f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
488f8829a4aSRandall Stewart 			/*
489f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
490f8829a4aSRandall Stewart 			 * yet another one.
491f8829a4aSRandall Stewart 			 */
492f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
493f8829a4aSRandall Stewart 		}
494f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
495f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
496f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
497f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
498f8829a4aSRandall Stewart 
499f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
500f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
501f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
502f8829a4aSRandall Stewart 		/* free up the chk */
503f8829a4aSRandall Stewart 		chk->data = NULL;
504689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
505f8829a4aSRandall Stewart 
506f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
507f8829a4aSRandall Stewart 			/*
508f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
509f8829a4aSRandall Stewart 			 * the stream
510f8829a4aSRandall Stewart 			 */
511f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
512f8829a4aSRandall Stewart 
513f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
514f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
5154a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
516f8829a4aSRandall Stewart 				/* Deliver more if we can. */
517f8829a4aSRandall Stewart 				if (nxt_todel == ctl->sinfo_ssn) {
518f8829a4aSRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, ctl, next);
519f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= ctl->length;
520f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
521f8829a4aSRandall Stewart 					strm->last_sequence_delivered++;
522b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
523f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
524f8829a4aSRandall Stewart 					    ctl,
525cfde3ff7SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
526cfde3ff7SRandall Stewart 					    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
527f8829a4aSRandall Stewart 				} else {
528f8829a4aSRandall Stewart 					break;
529f8829a4aSRandall Stewart 				}
530f8829a4aSRandall Stewart 				nxt_todel = strm->last_sequence_delivered + 1;
531f8829a4aSRandall Stewart 			}
532139bc87fSRandall Stewart 			break;
533f8829a4aSRandall Stewart 		}
5344a9ef3f8SMichael Tuexen 	}
535f8829a4aSRandall Stewart }
536f8829a4aSRandall Stewart 
537f8829a4aSRandall Stewart /*
538f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
539f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
540f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
541f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
542f8829a4aSRandall Stewart  */
543f8829a4aSRandall Stewart static void
544f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
545f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
546f8829a4aSRandall Stewart {
547f8829a4aSRandall Stewart 	/*
548f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
549f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
550f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
551f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
552f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
553f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
554f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
555f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
556f8829a4aSRandall Stewart 	 * TSN.
557f8829a4aSRandall Stewart 	 *
558f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
559f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
560f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
561f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
562f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
563f8829a4aSRandall Stewart 	 *
564f8829a4aSRandall Stewart 	 */
565f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
566f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
567f8829a4aSRandall Stewart 	int queue_needed;
568f8829a4aSRandall Stewart 	uint16_t nxt_todel;
569ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
570ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
571f8829a4aSRandall Stewart 
572f8829a4aSRandall Stewart 	queue_needed = 1;
573f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
574f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
575f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
576f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
577b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
578f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
57980fefe0aSRandall Stewart 	}
580ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
581ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
582f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
583ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
584ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
58520b07a4dSMichael Tuexen 	if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
586f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
587ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
588ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
589c40e9cf2SRandall Stewart protocol_error:
590f8829a4aSRandall Stewart 		/*
591f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
592f8829a4aSRandall Stewart 		 * association destruction
593f8829a4aSRandall Stewart 		 */
594f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
595ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
596ff1ffd74SMichael Tuexen 		    strm->last_sequence_delivered, control->sinfo_tsn,
597ff1ffd74SMichael Tuexen 		    control->sinfo_stream, control->sinfo_ssn);
598ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
599a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
600ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
601f8829a4aSRandall Stewart 		*abort_flag = 1;
602f8829a4aSRandall Stewart 		return;
603f8829a4aSRandall Stewart 
604f8829a4aSRandall Stewart 	}
605f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
606f8829a4aSRandall Stewart 		/* can be delivered right away? */
607b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
608f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
60980fefe0aSRandall Stewart 		}
610830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
611f8829a4aSRandall Stewart 		queue_needed = 0;
612f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
613f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
614f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
61577acdc25SRandall Stewart 
616b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
617f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
618f8829a4aSRandall Stewart 		    control,
619cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
620cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
6214a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
622f8829a4aSRandall Stewart 			/* all delivered */
623f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
624f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
625f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
626f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
627f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
628f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
629f8829a4aSRandall Stewart 				/*
630f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
631f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
632f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
633f8829a4aSRandall Stewart 				 * can be delivered from the strq.
634f8829a4aSRandall Stewart 				 */
635b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
636f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
637f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
63880fefe0aSRandall Stewart 				}
639b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
640f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
641f8829a4aSRandall Stewart 				    control,
642cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
643cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
644cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
645f8829a4aSRandall Stewart 				continue;
646f8829a4aSRandall Stewart 			}
647f8829a4aSRandall Stewart 			break;
648f8829a4aSRandall Stewart 		}
649f8829a4aSRandall Stewart 	}
650f8829a4aSRandall Stewart 	if (queue_needed) {
651f8829a4aSRandall Stewart 		/*
652f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
653f8829a4aSRandall Stewart 		 * to put it on the queue.
654f8829a4aSRandall Stewart 		 */
65520b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
656c40e9cf2SRandall Stewart 			goto protocol_error;
657c40e9cf2SRandall Stewart 		}
658f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
659f8829a4aSRandall Stewart 			/* Empty queue */
660b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
661f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
66280fefe0aSRandall Stewart 			}
663f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
664f8829a4aSRandall Stewart 		} else {
665f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
66620b07a4dSMichael Tuexen 				if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
667f8829a4aSRandall Stewart 					/*
668f8829a4aSRandall Stewart 					 * one in queue is bigger than the
669f8829a4aSRandall Stewart 					 * new one, insert before this one
670f8829a4aSRandall Stewart 					 */
671b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
672f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
673f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
67480fefe0aSRandall Stewart 					}
675f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
676f8829a4aSRandall Stewart 					break;
677f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
678f8829a4aSRandall Stewart 					/*
679f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
680f8829a4aSRandall Stewart 					 * seq number
681f8829a4aSRandall Stewart 					 */
682f8829a4aSRandall Stewart 					/*
683f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
684f8829a4aSRandall Stewart 					 * this new guy, should we abort
685f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
686f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
687f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
688f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
689f8829a4aSRandall Stewart 					 * away the chunk!
690f8829a4aSRandall Stewart 					 */
691f8829a4aSRandall Stewart 
692f8829a4aSRandall Stewart 					if (control->data)
693f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
694f8829a4aSRandall Stewart 					control->data = NULL;
695f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
696f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
69724f52bbdSMichael Tuexen 					if (control->whoFrom) {
698f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
6995bead436SRandall Stewart 						control->whoFrom = NULL;
70024f52bbdSMichael Tuexen 					}
701f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
702f8829a4aSRandall Stewart 					return;
703f8829a4aSRandall Stewart 				} else {
704f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
705f8829a4aSRandall Stewart 						/*
706f8829a4aSRandall Stewart 						 * We are at the end, insert
707f8829a4aSRandall Stewart 						 * it after this one
708f8829a4aSRandall Stewart 						 */
709b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
710f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
711f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
71280fefe0aSRandall Stewart 						}
713f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
714f8829a4aSRandall Stewart 						    at, control, next);
715f8829a4aSRandall Stewart 						break;
716f8829a4aSRandall Stewart 					}
717f8829a4aSRandall Stewart 				}
718f8829a4aSRandall Stewart 			}
719f8829a4aSRandall Stewart 		}
720f8829a4aSRandall Stewart 	}
721f8829a4aSRandall Stewart }
722f8829a4aSRandall Stewart 
723f8829a4aSRandall Stewart /*
724f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
725f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
726f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
727f8829a4aSRandall Stewart  */
728f8829a4aSRandall Stewart static int
729f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
730f8829a4aSRandall Stewart {
731f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
732f8829a4aSRandall Stewart 	uint32_t tsn;
733f8829a4aSRandall Stewart 
734f8829a4aSRandall Stewart 	*t_size = 0;
735f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
736f8829a4aSRandall Stewart 	if (chk == NULL) {
737f8829a4aSRandall Stewart 		/* nothing on the queue */
738f8829a4aSRandall Stewart 		return (0);
739f8829a4aSRandall Stewart 	}
740f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
741f8829a4aSRandall Stewart 		/* Not a first on the queue */
742f8829a4aSRandall Stewart 		return (0);
743f8829a4aSRandall Stewart 	}
744f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
7454a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
746f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
747f8829a4aSRandall Stewart 			return (0);
748f8829a4aSRandall Stewart 		}
749f8829a4aSRandall Stewart 		*t_size += chk->send_size;
750f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
751f8829a4aSRandall Stewart 			return (1);
752f8829a4aSRandall Stewart 		}
753f8829a4aSRandall Stewart 		tsn++;
754f8829a4aSRandall Stewart 	}
755f8829a4aSRandall Stewart 	return (0);
756f8829a4aSRandall Stewart }
757f8829a4aSRandall Stewart 
758f8829a4aSRandall Stewart static void
759f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
760f8829a4aSRandall Stewart {
761f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
762f8829a4aSRandall Stewart 	uint16_t nxt_todel;
763810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
764f8829a4aSRandall Stewart 
765139bc87fSRandall Stewart doit_again:
766f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
767f8829a4aSRandall Stewart 	if (chk == NULL) {
768f8829a4aSRandall Stewart 		/* Huh? */
769f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
770f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
771f8829a4aSRandall Stewart 		return;
772f8829a4aSRandall Stewart 	}
773f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
774f8829a4aSRandall Stewart 		nxt_todel =
775f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
776f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
777f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
778f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
779f8829a4aSRandall Stewart 			/*
780f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
781f8829a4aSRandall Stewart 			 * but should we?
782f8829a4aSRandall Stewart 			 */
783810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
784d4d23375SMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
785810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
786810ec536SMichael Tuexen 			} else {
787810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
788810ec536SMichael Tuexen 			}
789810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
790f8829a4aSRandall Stewart 				/*
791f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
792f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
793f8829a4aSRandall Stewart 				 * can't deliver. If we
794f8829a4aSRandall Stewart 				 */
795f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
796f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
797f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
798f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
799f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
800f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
801f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
802f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
803f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
804f8829a4aSRandall Stewart 			}
805f8829a4aSRandall Stewart 		}
806f8829a4aSRandall Stewart 	} else {
807139bc87fSRandall Stewart 		/*
808139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
809139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
810139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
811139bc87fSRandall Stewart 		 * got doit_again
812139bc87fSRandall Stewart 		 */
813f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
814139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
815139bc87fSRandall Stewart 			/*
816139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
817139bc87fSRandall Stewart 			 * waiting?
818139bc87fSRandall Stewart 			 */
819139bc87fSRandall Stewart 			goto doit_again;
820139bc87fSRandall Stewart 		}
821f8829a4aSRandall Stewart 	}
822f8829a4aSRandall Stewart }
823f8829a4aSRandall Stewart 
824f8829a4aSRandall Stewart /*
825f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
826f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
827f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
828f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
829f8829a4aSRandall Stewart  */
830f8829a4aSRandall Stewart static void
831f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
832f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
833f8829a4aSRandall Stewart {
834ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
835ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
83660990c0cSMichael Tuexen 	uint32_t cum_ackp1, prev_tsn, post_tsn;
837f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
838f8829a4aSRandall Stewart 
839f8829a4aSRandall Stewart 	prev = next = NULL;
840f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
841f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
842f8829a4aSRandall Stewart 		/* This is the first one on the queue */
843f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
844f8829a4aSRandall Stewart 		/*
845f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
846f8829a4aSRandall Stewart 		 * fragment is here
847f8829a4aSRandall Stewart 		 */
848f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
849f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
850f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
851f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
852f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
853f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
854f8829a4aSRandall Stewart 				/*
855f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
856f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
857f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
858f8829a4aSRandall Stewart 				 */
859ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
860ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg),
861ff1ffd74SMichael Tuexen 				    "Expected B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
862ff1ffd74SMichael Tuexen 				    chk->rec.data.TSN_seq,
863ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_number,
864ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_seq);
865ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
866a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
867ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
868f8829a4aSRandall Stewart 				*abort_flag = 1;
869f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
870f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
871f8829a4aSRandall Stewart 				/*
872f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
873f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
874f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
875f8829a4aSRandall Stewart 				 */
876ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
877ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg),
878ff1ffd74SMichael Tuexen 				    "Didn't expect B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
879ff1ffd74SMichael Tuexen 				    chk->rec.data.TSN_seq,
880ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_number,
881ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_seq);
882ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
883a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
884ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
885f8829a4aSRandall Stewart 				*abort_flag = 1;
886f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
887f8829a4aSRandall Stewart 				/*
888f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
889f8829a4aSRandall Stewart 				 * piece
890f8829a4aSRandall Stewart 				 */
891f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
892f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
893f8829a4aSRandall Stewart 					/* Got to be the right STR No */
894ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
895f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
896f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
897ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
898ff1ffd74SMichael Tuexen 					    "Expected SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
899ff1ffd74SMichael Tuexen 					    asoc->str_of_pdapi,
900ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
901ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
902ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
903ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
904a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
905ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
906f8829a4aSRandall Stewart 					*abort_flag = 1;
907f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
908f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
909b5c16493SMichael Tuexen 				    chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
910f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
911ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
912f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
913f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
914ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
915ff1ffd74SMichael Tuexen 					    "Expected SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
916ff1ffd74SMichael Tuexen 					    asoc->ssn_of_pdapi,
917ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
918ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
919ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
920ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
921a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
922ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
923f8829a4aSRandall Stewart 					*abort_flag = 1;
924f8829a4aSRandall Stewart 				}
925f8829a4aSRandall Stewart 			}
926f8829a4aSRandall Stewart 		}
927f8829a4aSRandall Stewart 		return;
928f8829a4aSRandall Stewart 	}
929f8829a4aSRandall Stewart 	/* Find its place */
930f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
93120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
932f8829a4aSRandall Stewart 			/*
933f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
934f8829a4aSRandall Stewart 			 * before this one
935f8829a4aSRandall Stewart 			 */
936f8829a4aSRandall Stewart 			/* A check */
937f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
938f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
939f8829a4aSRandall Stewart 			next = at;
940f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
941f8829a4aSRandall Stewart 			break;
942f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
943f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
944f8829a4aSRandall Stewart 			/*
945f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
946f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
947f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
948f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
949f8829a4aSRandall Stewart 			 * away the chunk!
950f8829a4aSRandall Stewart 			 */
951f8829a4aSRandall Stewart 			if (chk->data) {
952f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
953f8829a4aSRandall Stewart 				chk->data = NULL;
954f8829a4aSRandall Stewart 			}
955689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
956f8829a4aSRandall Stewart 			return;
957f8829a4aSRandall Stewart 		} else {
958f8829a4aSRandall Stewart 			prev = at;
959f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
960f8829a4aSRandall Stewart 				/*
961f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
962f8829a4aSRandall Stewart 				 * one
963f8829a4aSRandall Stewart 				 */
964f8829a4aSRandall Stewart 				/* check it first */
965f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
966f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
967f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
968f8829a4aSRandall Stewart 				break;
969f8829a4aSRandall Stewart 			}
970f8829a4aSRandall Stewart 		}
971f8829a4aSRandall Stewart 	}
972f8829a4aSRandall Stewart 	/* Now the audits */
973f8829a4aSRandall Stewart 	if (prev) {
974f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
975f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
976f8829a4aSRandall Stewart 			/*
977f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
978f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
979f8829a4aSRandall Stewart 			 */
980f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
981f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
982f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
983f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
984f8829a4aSRandall Stewart 				/*
985f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
986f8829a4aSRandall Stewart 				 * fragment
987f8829a4aSRandall Stewart 				 */
988f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
989f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
990ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
991ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
992ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
993ff1ffd74SMichael Tuexen 					    "Can't handle B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
994ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
995ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
996ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
997ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
998a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
999ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1000f8829a4aSRandall Stewart 					*abort_flag = 1;
1001f8829a4aSRandall Stewart 					return;
1002f8829a4aSRandall Stewart 				}
1003f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1004f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1005f8829a4aSRandall Stewart 					/*
1006f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1007f8829a4aSRandall Stewart 					 * they must be the same.
1008f8829a4aSRandall Stewart 					 */
10092dec1efcSMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sid:%d not the same as at:%d\n",
1010f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1011f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1012ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1013ff1ffd74SMichael Tuexen 					    "Expect SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1014ff1ffd74SMichael Tuexen 					    prev->rec.data.stream_number,
1015ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1016ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1017ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1018ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1019a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1020ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1021f8829a4aSRandall Stewart 					*abort_flag = 1;
1022f8829a4aSRandall Stewart 					return;
1023f8829a4aSRandall Stewart 				}
1024eb67ee5fSMichael Tuexen 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
1025eb67ee5fSMichael Tuexen 				    (prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
1026eb67ee5fSMichael Tuexen 					/*
1027eb67ee5fSMichael Tuexen 					 * Huh, need the same ordering here,
1028eb67ee5fSMichael Tuexen 					 * they must be the same.
1029eb67ee5fSMichael Tuexen 					 */
1030eb67ee5fSMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, U-bit not constant\n");
1031eb67ee5fSMichael Tuexen 					snprintf(msg, sizeof(msg),
1032eb67ee5fSMichael Tuexen 					    "Expect U-bit=%d for TSN=%8.8x, got U-bit=%d",
1033eb67ee5fSMichael Tuexen 					    (prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0,
1034eb67ee5fSMichael Tuexen 					    chk->rec.data.TSN_seq,
1035eb67ee5fSMichael Tuexen 					    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0);
1036eb67ee5fSMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1037eb67ee5fSMichael Tuexen 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1038eb67ee5fSMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1039eb67ee5fSMichael Tuexen 					*abort_flag = 1;
1040eb67ee5fSMichael Tuexen 					return;
1041eb67ee5fSMichael Tuexen 				}
1042f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1043f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1044f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1045f8829a4aSRandall Stewart 					/*
1046f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1047f8829a4aSRandall Stewart 					 * they must be the same.
1048f8829a4aSRandall Stewart 					 */
1049ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1050f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1051f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1052ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1053ff1ffd74SMichael Tuexen 					    "Expect SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1054ff1ffd74SMichael Tuexen 					    prev->rec.data.stream_seq,
1055ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1056ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1057ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1058ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1059a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1060ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1061f8829a4aSRandall Stewart 					*abort_flag = 1;
1062f8829a4aSRandall Stewart 					return;
1063f8829a4aSRandall Stewart 				}
1064f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1065f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1066f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1067f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1068f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1069ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1070ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1071ff1ffd74SMichael Tuexen 					    "Expect B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1072ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1073ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1074ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1075ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1076a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1077ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1078f8829a4aSRandall Stewart 					*abort_flag = 1;
1079f8829a4aSRandall Stewart 					return;
1080f8829a4aSRandall Stewart 				}
1081f8829a4aSRandall Stewart 			}
1082f8829a4aSRandall Stewart 		}
1083f8829a4aSRandall Stewart 	}
1084f8829a4aSRandall Stewart 	if (next) {
1085f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1086f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1087f8829a4aSRandall Stewart 			/*
1088f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1089f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1090f8829a4aSRandall Stewart 			 */
1091f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1092f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1093f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1094f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1095ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1096ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1097ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1098ff1ffd74SMichael Tuexen 					    "Expect only E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1099ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1100ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1101ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1102ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1103a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1104ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1105f8829a4aSRandall Stewart 					*abort_flag = 1;
1106f8829a4aSRandall Stewart 					return;
1107f8829a4aSRandall Stewart 				}
1108f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1109f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1110f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1111f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1112f8829a4aSRandall Stewart 				/*
1113f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1114f8829a4aSRandall Stewart 				 * LAST
1115f8829a4aSRandall Stewart 				 */
1116f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1117f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1118ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1119ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1120ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1121ff1ffd74SMichael Tuexen 					    "Didn't expect E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1122ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1123ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1124ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1125ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1126a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1127ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1128f8829a4aSRandall Stewart 					*abort_flag = 1;
1129f8829a4aSRandall Stewart 					return;
1130f8829a4aSRandall Stewart 				}
1131f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1132f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1133f8829a4aSRandall Stewart 					/*
1134f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1135f8829a4aSRandall Stewart 					 * they must be the same.
1136f8829a4aSRandall Stewart 					 */
1137ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1138f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1139f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1140ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1141ff1ffd74SMichael Tuexen 					    "Required SID %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1142ff1ffd74SMichael Tuexen 					    next->rec.data.stream_number,
1143ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1144ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1145ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1146ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1147a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1148ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1149f8829a4aSRandall Stewart 					*abort_flag = 1;
1150f8829a4aSRandall Stewart 					return;
1151f8829a4aSRandall Stewart 				}
1152eb67ee5fSMichael Tuexen 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
1153eb67ee5fSMichael Tuexen 				    (next->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
1154eb67ee5fSMichael Tuexen 					/*
1155eb67ee5fSMichael Tuexen 					 * Huh, need the same ordering here,
1156eb67ee5fSMichael Tuexen 					 * they must be the same.
1157eb67ee5fSMichael Tuexen 					 */
1158eb67ee5fSMichael Tuexen 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next check - Gak, Evil plot, U-bit not constant\n");
1159eb67ee5fSMichael Tuexen 					snprintf(msg, sizeof(msg),
1160eb67ee5fSMichael Tuexen 					    "Expect U-bit=%d for TSN=%8.8x, got U-bit=%d",
1161eb67ee5fSMichael Tuexen 					    (next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0,
1162eb67ee5fSMichael Tuexen 					    chk->rec.data.TSN_seq,
1163eb67ee5fSMichael Tuexen 					    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) ? 1 : 0);
1164eb67ee5fSMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1165eb67ee5fSMichael Tuexen 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1166eb67ee5fSMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1167eb67ee5fSMichael Tuexen 					*abort_flag = 1;
1168eb67ee5fSMichael Tuexen 					return;
1169eb67ee5fSMichael Tuexen 				}
1170f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1171f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1172f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1173f8829a4aSRandall Stewart 					/*
1174f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1175f8829a4aSRandall Stewart 					 * they must be the same.
1176f8829a4aSRandall Stewart 					 */
1177ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1178f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1179f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1180ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1181ff1ffd74SMichael Tuexen 					    "Required SSN %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1182ff1ffd74SMichael Tuexen 					    next->rec.data.stream_seq,
1183ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1184ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1185ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1186ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1187a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1188ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1189f8829a4aSRandall Stewart 					*abort_flag = 1;
1190f8829a4aSRandall Stewart 					return;
1191f8829a4aSRandall Stewart 				}
1192f8829a4aSRandall Stewart 			}
1193f8829a4aSRandall Stewart 		}
1194f8829a4aSRandall Stewart 	}
1195f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1196f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1197f8829a4aSRandall Stewart }
1198f8829a4aSRandall Stewart 
1199f8829a4aSRandall Stewart /*
1200f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1201f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1202f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1203f8829a4aSRandall Stewart  * :< more cycles.
1204f8829a4aSRandall Stewart  */
1205f8829a4aSRandall Stewart static int
1206f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1207f8829a4aSRandall Stewart     uint32_t TSN_seq)
1208f8829a4aSRandall Stewart {
1209f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1210f8829a4aSRandall Stewart 	uint32_t tsn_est;
1211f8829a4aSRandall Stewart 
1212f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
121320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1214f8829a4aSRandall Stewart 			/* is it one bigger? */
1215f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1216f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1217f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1218f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1219f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1220f8829a4aSRandall Stewart 					/*
1221f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1222f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1223f8829a4aSRandall Stewart 					 * middle/last, not a complete
1224f8829a4aSRandall Stewart 					 * chunk.
1225f8829a4aSRandall Stewart 					 */
1226f8829a4aSRandall Stewart 					return (1);
1227f8829a4aSRandall Stewart 				} else {
1228f8829a4aSRandall Stewart 					/*
1229f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1230f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1231f8829a4aSRandall Stewart 					 * self- contained one.
1232f8829a4aSRandall Stewart 					 */
1233f8829a4aSRandall Stewart 					return (0);
1234f8829a4aSRandall Stewart 				}
1235f8829a4aSRandall Stewart 			}
1236f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1237f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1238f8829a4aSRandall Stewart 			return (1);
1239f8829a4aSRandall Stewart 		} else {
1240f8829a4aSRandall Stewart 			/*
1241f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1242f8829a4aSRandall Stewart 			 * need to be right before it.
1243f8829a4aSRandall Stewart 			 */
1244f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1245f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1246f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1247f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1248f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1249f8829a4aSRandall Stewart 					return (1);
1250f8829a4aSRandall Stewart 				} else {
1251f8829a4aSRandall Stewart 					return (0);
1252f8829a4aSRandall Stewart 				}
1253f8829a4aSRandall Stewart 			}
1254f8829a4aSRandall Stewart 		}
1255f8829a4aSRandall Stewart 	}
1256f8829a4aSRandall Stewart 	return (0);
1257f8829a4aSRandall Stewart }
1258f8829a4aSRandall Stewart 
1259f8829a4aSRandall Stewart static int
1260f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1261f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1262f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1263f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1264f8829a4aSRandall Stewart {
1265f8829a4aSRandall Stewart 	/* Process a data chunk */
1266f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1267f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1268f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1269f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
12707215cc1bSMichael Tuexen 	int the_len;
1271139bc87fSRandall Stewart 	int need_reasm_check = 0;
1272f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1273ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
1274ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
1275f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1276f42a358aSRandall Stewart 	int ordered;
1277f42a358aSRandall Stewart 	uint32_t protocol_id;
1278f42a358aSRandall Stewart 	uint8_t chunk_flags;
127917205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1280f8829a4aSRandall Stewart 
1281f8829a4aSRandall Stewart 	chk = NULL;
1282f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1283f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1284b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1285b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1286b3f1ea41SRandall Stewart 	}
1287f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
128837f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1289b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1290c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
129180fefe0aSRandall Stewart 	}
1292ad81507eSRandall Stewart 	if (stcb == NULL) {
1293ad81507eSRandall Stewart 		return (0);
1294ad81507eSRandall Stewart 	}
129580fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
129620b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1297f8829a4aSRandall Stewart 		/* It is a duplicate */
1298f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1299f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1300f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1301f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1302f8829a4aSRandall Stewart 			asoc->numduptsns++;
1303f8829a4aSRandall Stewart 		}
1304b201f536SRandall Stewart 		asoc->send_sack = 1;
1305f8829a4aSRandall Stewart 		return (0);
1306f8829a4aSRandall Stewart 	}
1307f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1308d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1309f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1310f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1311f8829a4aSRandall Stewart 		return (0);
1312f8829a4aSRandall Stewart 	}
1313f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1314207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
13150696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1316f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1317f8829a4aSRandall Stewart 			return (0);
1318f8829a4aSRandall Stewart 		}
1319f8829a4aSRandall Stewart 	}
132020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1321f8829a4aSRandall Stewart 		*high_tsn = tsn;
1322f8829a4aSRandall Stewart 	}
1323f8829a4aSRandall Stewart 	/* See if we have received this one already */
132477acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
132577acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1326f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1327f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1328f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1329f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1330f8829a4aSRandall Stewart 			asoc->numduptsns++;
1331f8829a4aSRandall Stewart 		}
133242551e99SRandall Stewart 		asoc->send_sack = 1;
1333f8829a4aSRandall Stewart 		return (0);
1334f8829a4aSRandall Stewart 	}
1335f8829a4aSRandall Stewart 	/*
1336f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1337f8829a4aSRandall Stewart 	 * to be sent up above
1338f8829a4aSRandall Stewart 	 */
1339ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1340f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1341ff1ffd74SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1342f8829a4aSRandall Stewart 		/*
1343f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1344f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1345f8829a4aSRandall Stewart 		 */
1346ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1347a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1348f8829a4aSRandall Stewart 		*abort_flag = 1;
1349f8829a4aSRandall Stewart 		return (0);
1350f8829a4aSRandall Stewart 	}
1351f8829a4aSRandall Stewart 	/*
1352f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1353f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1354f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1355f8829a4aSRandall Stewart 	 */
1356f8829a4aSRandall Stewart 
1357f8829a4aSRandall Stewart 	/* now do the tests */
1358f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1359f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1360b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1361f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1362f8829a4aSRandall Stewart 		/*
1363f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1364f8829a4aSRandall Stewart 		 * the reader is doing its job...
1365f8829a4aSRandall Stewart 		 */
1366f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1367f8829a4aSRandall Stewart 			/* some to read, wake-up */
1368ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1369ceaad40aSRandall Stewart 			struct socket *so;
1370ceaad40aSRandall Stewart 
1371ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1372ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1373ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1374ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1375ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1376ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1377ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1378ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1379ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1380ceaad40aSRandall Stewart 				return (0);
1381ceaad40aSRandall Stewart 			}
1382ceaad40aSRandall Stewart #endif
1383f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1384ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1385ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1386ceaad40aSRandall Stewart #endif
1387f8829a4aSRandall Stewart 		}
1388f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
138920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
139020b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1391f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1392f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1393f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1394f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1395b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1396f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1397f8829a4aSRandall Stewart 			} else {
1398f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1399f8829a4aSRandall Stewart 			}
1400f8829a4aSRandall Stewart 			*break_flag = 1;
1401f8829a4aSRandall Stewart 			return (0);
1402f8829a4aSRandall Stewart 		}
1403f8829a4aSRandall Stewart 	}
1404f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1405f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1406f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1407f8829a4aSRandall Stewart 		struct mbuf *mb;
1408f8829a4aSRandall Stewart 
1409f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1410eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
1411f8829a4aSRandall Stewart 		if (mb != NULL) {
1412f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1413139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1414f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1415f8829a4aSRandall Stewart 			/*
1416f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1417f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1418f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1419f8829a4aSRandall Stewart 			 */
1420139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1421f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1422f8829a4aSRandall Stewart 			phdr->param_length =
1423f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1424f8829a4aSRandall Stewart 			phdr++;
1425f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1426f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1427f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1428f8829a4aSRandall Stewart 			phdr->param_length = 0;
1429f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1430f8829a4aSRandall Stewart 		}
1431f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1432207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1433830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
143420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1435830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1436d06c82f1SRandall Stewart 		}
1437d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1438d06c82f1SRandall Stewart 			/* Update cum-ack */
1439d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1440d06c82f1SRandall Stewart 		}
1441f8829a4aSRandall Stewart 		return (0);
1442f8829a4aSRandall Stewart 	}
1443f8829a4aSRandall Stewart 	/*
1444f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1445f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1446f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1447f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1448f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1449f8829a4aSRandall Stewart 	 */
1450f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1451f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
145218e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
145318e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
145418e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
145518e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
145618e198d3SRandall Stewart 	}
1457f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1458f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1459f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1460f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1461f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
146218e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
146318e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
146418e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1465f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1466f42a358aSRandall Stewart #endif
1467f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1468d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1469f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
147020b07a4dSMichael Tuexen 	    SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1471f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1472ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1473ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1474f8829a4aSRandall Stewart 
1475ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1476ff1ffd74SMichael Tuexen 		    asoc->strmin[strmno].last_sequence_delivered,
1477ff1ffd74SMichael Tuexen 		    tsn, strmno, strmseq);
1478ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1479a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1480ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1481f8829a4aSRandall Stewart 		*abort_flag = 1;
1482f8829a4aSRandall Stewart 		return (0);
1483f8829a4aSRandall Stewart 	}
1484f42a358aSRandall Stewart 	/************************************
1485f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1486f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1487f42a358aSRandall Stewart 	 *************************************/
1488f42a358aSRandall Stewart 
1489f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1490f8829a4aSRandall Stewart 	if (last_chunk == 0) {
149144b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1492f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1493eb1b1807SGleb Smirnoff 		    the_len, M_NOWAIT);
1494f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1495b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1496f8829a4aSRandall Stewart 			struct mbuf *mat;
1497f8829a4aSRandall Stewart 
149860990c0cSMichael Tuexen 			for (mat = dmbuf; mat; mat = SCTP_BUF_NEXT(mat)) {
1499139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1500f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1501f8829a4aSRandall Stewart 				}
1502f8829a4aSRandall Stewart 			}
1503f8829a4aSRandall Stewart 		}
1504f8829a4aSRandall Stewart #endif
1505f8829a4aSRandall Stewart 	} else {
1506f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1507139bc87fSRandall Stewart 		int l_len;
1508139bc87fSRandall Stewart 
1509f8829a4aSRandall Stewart 		dmbuf = *m;
1510f8829a4aSRandall Stewart 		/* lop off the top part */
1511f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1512139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1513139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1514139bc87fSRandall Stewart 		} else {
1515139bc87fSRandall Stewart 			/*
1516139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1517139bc87fSRandall Stewart 			 * this to often :-0
1518139bc87fSRandall Stewart 			 */
1519139bc87fSRandall Stewart 			struct mbuf *lat;
1520139bc87fSRandall Stewart 
1521139bc87fSRandall Stewart 			l_len = 0;
152260990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1523139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1524139bc87fSRandall Stewart 			}
1525139bc87fSRandall Stewart 		}
1526139bc87fSRandall Stewart 		if (l_len > the_len) {
1527f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1528139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1529f8829a4aSRandall Stewart 		}
1530f8829a4aSRandall Stewart 	}
1531f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1532f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1533f8829a4aSRandall Stewart 		return (0);
1534f8829a4aSRandall Stewart 	}
1535f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1536f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1537f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1538f42a358aSRandall Stewart 	    ((ordered == 0) ||
153936ec9f81SMichael Tuexen 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1540f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1541f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1542f8829a4aSRandall Stewart 		/*
1543f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1544f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1545f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1546f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1547f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1548f8829a4aSRandall Stewart 		 */
1549f8829a4aSRandall Stewart 
1550f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1551f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1552f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1553f42a358aSRandall Stewart 		    protocol_id,
1554f8829a4aSRandall Stewart 		    strmno, strmseq,
1555f42a358aSRandall Stewart 		    chunk_flags,
1556f8829a4aSRandall Stewart 		    dmbuf);
1557f8829a4aSRandall Stewart 		if (control == NULL) {
1558f8829a4aSRandall Stewart 			goto failed_express_del;
1559f8829a4aSRandall Stewart 		}
15601ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
156120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
15621ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
15631ea735c8SMichael Tuexen 		}
1564cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1565cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1566cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1567830d754dSRandall Stewart 
1568f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1569f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1570f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1571f8829a4aSRandall Stewart 		}
1572f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1573b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
15746a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1575f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
157680fefe0aSRandall Stewart 		}
1577f8829a4aSRandall Stewart 		control = NULL;
1578b5c16493SMichael Tuexen 
1579f8829a4aSRandall Stewart 		goto finish_express_del;
1580f8829a4aSRandall Stewart 	}
1581f8829a4aSRandall Stewart failed_express_del:
1582f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1583f8829a4aSRandall Stewart 	chk = NULL;
1584f8829a4aSRandall Stewart 	control = NULL;
1585f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1586f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1587f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1588f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1589f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1590f8829a4aSRandall Stewart 	    ) {
1591f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1592f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1593f8829a4aSRandall Stewart 			/* Can't be another first? */
1594f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1595f8829a4aSRandall Stewart 		}
1596f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1597f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1598f8829a4aSRandall Stewart 			int end = 0;
1599f8829a4aSRandall Stewart 
1600f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1601f8829a4aSRandall Stewart 				end = 1;
1602f8829a4aSRandall Stewart 			}
1603f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1604f8829a4aSRandall Stewart 			    tsn,
1605f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1606ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1607f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1608f8829a4aSRandall Stewart 			}
160977acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
161020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1611830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1612830d754dSRandall Stewart 			}
1613f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1614f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1615f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1616f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1617f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1618f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1619f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1620f8829a4aSRandall Stewart 			if (end) {
1621f8829a4aSRandall Stewart 				/* clean up the flags and such */
1622f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1623f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1624f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1625a5d547adSRandall Stewart 				}
1626f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1627139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1628139bc87fSRandall Stewart 					/*
1629139bc87fSRandall Stewart 					 * There could be another message
1630139bc87fSRandall Stewart 					 * ready
1631139bc87fSRandall Stewart 					 */
1632139bc87fSRandall Stewart 					need_reasm_check = 1;
1633139bc87fSRandall Stewart 				}
1634f8829a4aSRandall Stewart 			}
1635f8829a4aSRandall Stewart 			control = NULL;
1636f8829a4aSRandall Stewart 			goto finish_express_del;
1637f8829a4aSRandall Stewart 		}
1638f8829a4aSRandall Stewart 	}
1639f8829a4aSRandall Stewart failed_pdapi_express_del:
1640f8829a4aSRandall Stewart 	control = NULL;
164177acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
164277acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
164320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
164477acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
164577acdc25SRandall Stewart 		}
164677acdc25SRandall Stewart 	} else {
164777acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
164820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
164977acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
165077acdc25SRandall Stewart 		}
165177acdc25SRandall Stewart 	}
1652f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1653f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1654f8829a4aSRandall Stewart 		if (chk == NULL) {
1655f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1656f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1657f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1658f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1659f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1660f8829a4aSRandall Stewart 			}
1661f8829a4aSRandall Stewart 			return (0);
1662f8829a4aSRandall Stewart 		}
1663f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1664f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1665f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1666f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1667f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1668f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1669f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1670f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1671f8829a4aSRandall Stewart 		chk->asoc = asoc;
1672f8829a4aSRandall Stewart 		chk->send_size = the_len;
1673f8829a4aSRandall Stewart 		chk->whoTo = net;
1674f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1675f8829a4aSRandall Stewart 		chk->data = dmbuf;
1676f8829a4aSRandall Stewart 	} else {
1677f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1678f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1679f42a358aSRandall Stewart 		    protocol_id,
1680f8829a4aSRandall Stewart 		    strmno, strmseq,
1681f42a358aSRandall Stewart 		    chunk_flags,
1682f8829a4aSRandall Stewart 		    dmbuf);
1683f8829a4aSRandall Stewart 		if (control == NULL) {
1684f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1685f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1686f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1687f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1688f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1689f8829a4aSRandall Stewart 			}
1690f8829a4aSRandall Stewart 			return (0);
1691f8829a4aSRandall Stewart 		}
1692f8829a4aSRandall Stewart 		control->length = the_len;
1693f8829a4aSRandall Stewart 	}
1694f8829a4aSRandall Stewart 
1695f8829a4aSRandall Stewart 	/* Mark it as received */
1696f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1697f8829a4aSRandall Stewart 	if (control != NULL) {
1698f8829a4aSRandall Stewart 		/* First a sanity check */
1699f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1700f8829a4aSRandall Stewart 			/*
1701f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1702f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1703f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1704f8829a4aSRandall Stewart 			 * broken.
1705f8829a4aSRandall Stewart 			 */
1706f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1707f8829a4aSRandall Stewart 
1708f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1709f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1710f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1711f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1712f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1713f8829a4aSRandall Stewart 				control->data = NULL;
17145bead436SRandall Stewart 				if (control->whoFrom) {
1715f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
17165bead436SRandall Stewart 					control->whoFrom = NULL;
17175bead436SRandall Stewart 				}
1718f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1719ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "Reas. queue emtpy, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1720ff1ffd74SMichael Tuexen 				    tsn, strmno, strmseq);
1721ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1722a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1723ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1724f8829a4aSRandall Stewart 				*abort_flag = 1;
17258be0fd55SMichael Tuexen 				if (last_chunk) {
17268be0fd55SMichael Tuexen 					*m = NULL;
17278be0fd55SMichael Tuexen 				}
1728f8829a4aSRandall Stewart 				return (0);
1729f8829a4aSRandall Stewart 			} else {
1730f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1731f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1732f8829a4aSRandall Stewart 					control->data = NULL;
17335bead436SRandall Stewart 					if (control->whoFrom) {
1734f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
17355bead436SRandall Stewart 						control->whoFrom = NULL;
17365bead436SRandall Stewart 					}
1737f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1738ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg), "PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1739ff1ffd74SMichael Tuexen 					    tsn, strmno, strmseq);
1740ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1741a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1742ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1743f8829a4aSRandall Stewart 					*abort_flag = 1;
17448be0fd55SMichael Tuexen 					if (last_chunk) {
17458be0fd55SMichael Tuexen 						*m = NULL;
17468be0fd55SMichael Tuexen 					}
1747f8829a4aSRandall Stewart 					return (0);
1748f8829a4aSRandall Stewart 				}
1749f8829a4aSRandall Stewart 			}
1750f8829a4aSRandall Stewart 		} else {
1751f8829a4aSRandall Stewart 			/* No PDAPI running */
1752f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1753f8829a4aSRandall Stewart 				/*
1754f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
1755f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
1756f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
1757f8829a4aSRandall Stewart 				 * is broken or evil.
1758f8829a4aSRandall Stewart 				 */
1759f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1760f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1761f8829a4aSRandall Stewart 					control->data = NULL;
17625bead436SRandall Stewart 					if (control->whoFrom) {
1763f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
17645bead436SRandall Stewart 						control->whoFrom = NULL;
17655bead436SRandall Stewart 					}
1766f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1767ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg), "No PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1768ff1ffd74SMichael Tuexen 					    tsn, strmno, strmseq);
1769ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1770a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1771ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1772f8829a4aSRandall Stewart 					*abort_flag = 1;
17738be0fd55SMichael Tuexen 					if (last_chunk) {
17748be0fd55SMichael Tuexen 						*m = NULL;
17758be0fd55SMichael Tuexen 					}
1776f8829a4aSRandall Stewart 					return (0);
1777f8829a4aSRandall Stewart 				}
1778f8829a4aSRandall Stewart 			}
1779f8829a4aSRandall Stewart 		}
1780f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
1781f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
1782f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
1783b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
1784f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
1785f8829a4aSRandall Stewart 			    control,
1786cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1787f8829a4aSRandall Stewart 		} else {
1788f8829a4aSRandall Stewart 			/*
1789f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
1790f8829a4aSRandall Stewart 			 * could be more smart about this and check the
1791f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
1792f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
1793f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
1794f8829a4aSRandall Stewart 			 *
1795f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
1796f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
1797f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
1798f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
1799f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
1800f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
1801f8829a4aSRandall Stewart 			 * singletons I must worry about.
1802f8829a4aSRandall Stewart 			 */
1803f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
180420b07a4dSMichael Tuexen 			    SCTP_TSN_GT(tsn, liste->tsn)) {
1805f8829a4aSRandall Stewart 				/*
1806f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
1807f8829a4aSRandall Stewart 				 * ahead and queue it.
1808f8829a4aSRandall Stewart 				 */
1809f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
1810f8829a4aSRandall Stewart 					/* first one on */
1811f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
1812f8829a4aSRandall Stewart 				} else {
18134a9ef3f8SMichael Tuexen 					struct sctp_queued_to_read *ctlOn,
18144a9ef3f8SMichael Tuexen 					                   *nctlOn;
1815f8829a4aSRandall Stewart 					unsigned char inserted = 0;
1816f8829a4aSRandall Stewart 
18174a9ef3f8SMichael Tuexen 					TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
181820b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
18194a9ef3f8SMichael Tuexen 							continue;
1820f8829a4aSRandall Stewart 						} else {
1821f8829a4aSRandall Stewart 							/* found it */
1822f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
1823f8829a4aSRandall Stewart 							inserted = 1;
1824f8829a4aSRandall Stewart 							break;
1825f8829a4aSRandall Stewart 						}
1826f8829a4aSRandall Stewart 					}
1827f8829a4aSRandall Stewart 					if (inserted == 0) {
1828f8829a4aSRandall Stewart 						/*
1829f8829a4aSRandall Stewart 						 * must be put at end, use
1830f8829a4aSRandall Stewart 						 * prevP (all setup from
1831f8829a4aSRandall Stewart 						 * loop) to setup nextP.
1832f8829a4aSRandall Stewart 						 */
1833f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
1834f8829a4aSRandall Stewart 					}
1835f8829a4aSRandall Stewart 				}
1836f8829a4aSRandall Stewart 			} else {
1837f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
1838f8829a4aSRandall Stewart 				if (*abort_flag) {
18398be0fd55SMichael Tuexen 					if (last_chunk) {
18408be0fd55SMichael Tuexen 						*m = NULL;
18418be0fd55SMichael Tuexen 					}
1842f8829a4aSRandall Stewart 					return (0);
1843f8829a4aSRandall Stewart 				}
1844f8829a4aSRandall Stewart 			}
1845f8829a4aSRandall Stewart 		}
1846f8829a4aSRandall Stewart 	} else {
1847f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
1848f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
1849f8829a4aSRandall Stewart 		if (*abort_flag) {
1850a5d547adSRandall Stewart 			/*
1851a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
1852a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
1853a5d547adSRandall Stewart 			 */
18548be0fd55SMichael Tuexen 			if (last_chunk) {
1855a5d547adSRandall Stewart 				*m = NULL;
18568be0fd55SMichael Tuexen 			}
1857f8829a4aSRandall Stewart 			return (0);
1858f8829a4aSRandall Stewart 		}
1859f8829a4aSRandall Stewart 	}
1860f8829a4aSRandall Stewart finish_express_del:
1861307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
1862307b49efSMichael Tuexen 		/* Update cum-ack */
1863307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
1864307b49efSMichael Tuexen 	}
1865f8829a4aSRandall Stewart 	if (last_chunk) {
1866f8829a4aSRandall Stewart 		*m = NULL;
1867f8829a4aSRandall Stewart 	}
1868f42a358aSRandall Stewart 	if (ordered) {
1869f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
1870f8829a4aSRandall Stewart 	} else {
1871f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
1872f8829a4aSRandall Stewart 	}
1873f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
1874f8829a4aSRandall Stewart 	/* Set it present please */
1875b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
18766a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
187780fefe0aSRandall Stewart 	}
1878b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1879f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
1880f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
188180fefe0aSRandall Stewart 	}
188217205eccSRandall Stewart 	/* check the special flag for stream resets */
188317205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
188420b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
188517205eccSRandall Stewart 		/*
188617205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
188717205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
188817205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
188917205eccSRandall Stewart 		 * pending_reply_queue.
189017205eccSRandall Stewart 		 */
18914a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
189217205eccSRandall Stewart 
1893a169d6ecSMichael Tuexen 		sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
189417205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
1895207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
18963c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
189717205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
18984a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
189917205eccSRandall Stewart 			/* All can be removed */
19004a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
190117205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
190217205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
190317205eccSRandall Stewart 				if (*abort_flag) {
190417205eccSRandall Stewart 					return (0);
190517205eccSRandall Stewart 				}
190617205eccSRandall Stewart 			}
19074a9ef3f8SMichael Tuexen 		} else {
19084a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
190920b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
19104a9ef3f8SMichael Tuexen 					break;
19114a9ef3f8SMichael Tuexen 				}
191217205eccSRandall Stewart 				/*
191317205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
191417205eccSRandall Stewart 				 * process it which is the NOT of
191517205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
191617205eccSRandall Stewart 				 */
191717205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
191817205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
191917205eccSRandall Stewart 				if (*abort_flag) {
192017205eccSRandall Stewart 					return (0);
192117205eccSRandall Stewart 				}
192217205eccSRandall Stewart 			}
192317205eccSRandall Stewart 		}
192417205eccSRandall Stewart 		/*
192517205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
192617205eccSRandall Stewart 		 * held on reassembly queue?
192717205eccSRandall Stewart 		 */
192817205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
192917205eccSRandall Stewart 		need_reasm_check = 0;
193017205eccSRandall Stewart 	}
1931139bc87fSRandall Stewart 	if (need_reasm_check) {
1932139bc87fSRandall Stewart 		/* Another one waits ? */
1933139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
1934139bc87fSRandall Stewart 	}
1935f8829a4aSRandall Stewart 	return (1);
1936f8829a4aSRandall Stewart }
1937f8829a4aSRandall Stewart 
1938f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
1939b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1940b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1941b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1942b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1943b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1944b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1945b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1946b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
1947b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1948b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1949b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1950b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1951b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1952b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1953b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1954b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
1955b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1956b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1957b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1958b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1959b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1960b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1961b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1962b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
1963b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1964b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1965b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1966b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1967b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1968b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1969b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1970b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
1971f8829a4aSRandall Stewart };
1972f8829a4aSRandall Stewart 
1973f8829a4aSRandall Stewart 
1974f8829a4aSRandall Stewart void
1975b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
1976f8829a4aSRandall Stewart {
1977f8829a4aSRandall Stewart 	/*
1978f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
1979f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
198066bd30bdSRandall Stewart 	 *
198166bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
198266bd30bdSRandall Stewart 	 * make up the postion of the cum-ack would be in the nr-mapping
198366bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
198466bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
198566bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
198666bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
198766bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
1988f8829a4aSRandall Stewart 	 */
1989f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1990b3f1ea41SRandall Stewart 	int at;
199166bd30bdSRandall Stewart 	uint8_t val;
1992f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
199377acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
1994f8829a4aSRandall Stewart 
1995f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1996f8829a4aSRandall Stewart 
1997f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
1998f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
1999f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2000f8829a4aSRandall Stewart 	/*
2001f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2002f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2003f8829a4aSRandall Stewart 	 */
2004f8829a4aSRandall Stewart 	at = 0;
2005b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
200666bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
200766bd30bdSRandall Stewart 		if (val == 0xff) {
2008f8829a4aSRandall Stewart 			at += 8;
2009f8829a4aSRandall Stewart 		} else {
2010f8829a4aSRandall Stewart 			/* there is a 0 bit */
201166bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2012f8829a4aSRandall Stewart 			break;
2013f8829a4aSRandall Stewart 		}
2014f8829a4aSRandall Stewart 	}
2015b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2016f8829a4aSRandall Stewart 
201720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
201820b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2019a5d547adSRandall Stewart #ifdef INVARIANTS
2020ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2021ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2022f8829a4aSRandall Stewart #else
2023ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2024ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
20250e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2026b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2027b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2028b3f1ea41SRandall Stewart 		}
2029f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2030830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2031f8829a4aSRandall Stewart #endif
2032f8829a4aSRandall Stewart 	}
203320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
203477acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
203577acdc25SRandall Stewart 	} else {
203677acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
203777acdc25SRandall Stewart 	}
203877acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2039f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
204077acdc25SRandall Stewart 		/* highest becomes the cum-ack */
204137f144ebSMichael Tuexen 		int clr;
204237f144ebSMichael Tuexen 
204337f144ebSMichael Tuexen #ifdef INVARIANTS
204437f144ebSMichael Tuexen 		unsigned int i;
204537f144ebSMichael Tuexen 
204637f144ebSMichael Tuexen #endif
2047f8829a4aSRandall Stewart 
2048f8829a4aSRandall Stewart 		/* clear the array */
2049b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2050c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2051f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2052f8829a4aSRandall Stewart 		}
2053f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2054830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
205537f144ebSMichael Tuexen #ifdef INVARIANTS
2056b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2057b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2058cd3fd531SMichael Tuexen 				SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2059b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2060b5c16493SMichael Tuexen 			}
2061b5c16493SMichael Tuexen 		}
206237f144ebSMichael Tuexen #endif
206377acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
206477acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2065f8829a4aSRandall Stewart 	} else if (at >= 8) {
2066f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2067b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2068b3f1ea41SRandall Stewart 
2069f8829a4aSRandall Stewart 		/*
2070f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2071f8829a4aSRandall Stewart 		 * TSN value
2072f8829a4aSRandall Stewart 		 */
207377acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
207477acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2075f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
207677acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2077d55b0b1bSRandall Stewart #ifdef INVARIANTS
2078f8829a4aSRandall Stewart 			panic("impossible slide");
2079d55b0b1bSRandall Stewart #else
2080cd3fd531SMichael Tuexen 			SCTP_PRINTF("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
208177acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2082d55b0b1bSRandall Stewart 			return;
2083d55b0b1bSRandall Stewart #endif
2084f8829a4aSRandall Stewart 		}
2085b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2086b3f1ea41SRandall Stewart #ifdef INVARIANTS
2087b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2088b3f1ea41SRandall Stewart #else
2089cd3fd531SMichael Tuexen 			SCTP_PRINTF("Gak, would have overrun map end:%d slide_end:%d\n",
2090b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2091b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2092b3f1ea41SRandall Stewart #endif
2093b3f1ea41SRandall Stewart 		}
2094f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2095b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2096f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2097f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2098f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2099f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
210080fefe0aSRandall Stewart 		}
2101f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2102f8829a4aSRandall Stewart 		    distance < 0) {
2103f8829a4aSRandall Stewart 			/*
2104f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2105f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2106f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2107f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2108f8829a4aSRandall Stewart 			 */
2109f8829a4aSRandall Stewart 
2110b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2111f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2112f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2113f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
211480fefe0aSRandall Stewart 			}
2115f8829a4aSRandall Stewart 		} else {
2116f8829a4aSRandall Stewart 			int ii;
2117f8829a4aSRandall Stewart 
2118f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
211937f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
212037f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
212177acdc25SRandall Stewart 
2122f8829a4aSRandall Stewart 			}
2123aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2124f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
212577acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2126f8829a4aSRandall Stewart 			}
2127ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2128ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2129ee94f0a2SMichael Tuexen 			}
2130ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2131ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2132ee94f0a2SMichael Tuexen 			}
2133f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2134b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2135f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2136f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2137f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
213880fefe0aSRandall Stewart 			}
2139830d754dSRandall Stewart 		}
2140830d754dSRandall Stewart 	}
2141b5c16493SMichael Tuexen }
2142b5c16493SMichael Tuexen 
2143b5c16493SMichael Tuexen void
21447215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2145b5c16493SMichael Tuexen {
2146b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2147b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2148b5c16493SMichael Tuexen 
2149b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
215020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2151b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2152b5c16493SMichael Tuexen 	} else {
2153b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2154b5c16493SMichael Tuexen 	}
2155b5c16493SMichael Tuexen 
2156830d754dSRandall Stewart 	/*
2157f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2158f8829a4aSRandall Stewart 	 * timer (if allowed).
2159f8829a4aSRandall Stewart 	 */
2160f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2161f8829a4aSRandall Stewart 		/*
2162b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2163b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2164b5c16493SMichael Tuexen 		 * SACK
2165f8829a4aSRandall Stewart 		 */
2166139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2167f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2168a5d547adSRandall Stewart 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2169f8829a4aSRandall Stewart 		}
2170ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2171ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2172689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2173f8829a4aSRandall Stewart 	} else {
2174f8829a4aSRandall Stewart 		int is_a_gap;
2175f8829a4aSRandall Stewart 
2176f8829a4aSRandall Stewart 		/* is there a gap now ? */
217720b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2178f8829a4aSRandall Stewart 
2179f8829a4aSRandall Stewart 		/*
2180b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2181b5c16493SMichael Tuexen 		 * since last ack
2182f8829a4aSRandall Stewart 		 */
2183f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2184f8829a4aSRandall Stewart 
218542551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
218642551e99SRandall Stewart 							 * SACK */
2187f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2188f8829a4aSRandall Stewart 							 * longer is one */
2189f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2190f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
219142551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
219242551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2193f8829a4aSRandall Stewart 		    ) {
2194f8829a4aSRandall Stewart 
21957c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2196b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
219742551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2198f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2199f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2200139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2201f8829a4aSRandall Stewart 
2202f8829a4aSRandall Stewart 				/*
2203b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2204b5c16493SMichael Tuexen 				 * even in the face of
2205f8829a4aSRandall Stewart 				 *
2206b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2207b5c16493SMichael Tuexen 				 * have to be sent because of the above
2208b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2209b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2210b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2211f8829a4aSRandall Stewart 				 * the delayed ack timer.
2212f8829a4aSRandall Stewart 				 */
2213f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2214f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2215f8829a4aSRandall Stewart 			} else {
2216f8829a4aSRandall Stewart 				/*
2217b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2218b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2219b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2220f8829a4aSRandall Stewart 				 */
2221ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2222689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2223f8829a4aSRandall Stewart 			}
2224f8829a4aSRandall Stewart 		} else {
222542551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2226f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2227f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2228f8829a4aSRandall Stewart 			}
2229f8829a4aSRandall Stewart 		}
2230f8829a4aSRandall Stewart 	}
2231f8829a4aSRandall Stewart }
2232f8829a4aSRandall Stewart 
2233f8829a4aSRandall Stewart void
2234f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2235f8829a4aSRandall Stewart {
2236f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2237810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2238f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2239f8829a4aSRandall Stewart 
2240f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2241f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2242f8829a4aSRandall Stewart 	}
2243f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2244f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2245f8829a4aSRandall Stewart 		/* no */
2246f8829a4aSRandall Stewart 		return;
2247f8829a4aSRandall Stewart 	}
2248f8829a4aSRandall Stewart 	/*
2249f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2250f8829a4aSRandall Stewart 	 * queue.
2251f8829a4aSRandall Stewart 	 */
2252139bc87fSRandall Stewart doit_again:
2253f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2254f8829a4aSRandall Stewart 	if (chk == NULL) {
2255f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2256f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2257f8829a4aSRandall Stewart 		return;
2258f8829a4aSRandall Stewart 	}
2259f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2260f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2261f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2262f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2263f8829a4aSRandall Stewart 		/*
2264f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2265f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2266f8829a4aSRandall Stewart 		 */
2267f8829a4aSRandall Stewart 
2268f8829a4aSRandall Stewart 		/*
2269f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
227024ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2271f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2272f8829a4aSRandall Stewart 		 */
2273810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
2274d4d23375SMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
2275810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2276810ec536SMichael Tuexen 		} else {
2277810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2278810ec536SMichael Tuexen 		}
2279810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2280f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2281f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2282f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2283f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2284f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2285f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2286f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2287139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2288139bc87fSRandall Stewart 				goto doit_again;
2289139bc87fSRandall Stewart 			}
2290f8829a4aSRandall Stewart 		}
2291f8829a4aSRandall Stewart 	}
2292f8829a4aSRandall Stewart }
2293f8829a4aSRandall Stewart 
2294f8829a4aSRandall Stewart int
2295f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2296b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2297f30ac432SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
2298f30ac432SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t * high_tsn,
2299f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
2300f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2301f8829a4aSRandall Stewart {
2302f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2303f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2304f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2305f8829a4aSRandall Stewart 	int stop_proc = 0;
2306f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
23078f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2308f8829a4aSRandall Stewart 	struct mbuf *m;
23098f777478SMichael Tuexen 	uint32_t highest_tsn;
2310f8829a4aSRandall Stewart 
2311f8829a4aSRandall Stewart 	/* set the rwnd */
2312f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2313f8829a4aSRandall Stewart 
2314f8829a4aSRandall Stewart 	m = *mm;
2315f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2316f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
231720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
23188f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
23198f777478SMichael Tuexen 	} else {
23208f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2321f8829a4aSRandall Stewart 	}
232220b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2323f8829a4aSRandall Stewart 	/*
2324f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2325f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2326f8829a4aSRandall Stewart 	 * chunk is assigned.
2327f8829a4aSRandall Stewart 	 */
2328f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2329f8829a4aSRandall Stewart 
2330d06c82f1SRandall Stewart 	/*-
2331f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2332f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2333f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2334f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2335d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2336d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2337f8829a4aSRandall Stewart 	 */
233844b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2339f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2340eb1b1807SGleb Smirnoff 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2341f8829a4aSRandall Stewart 		if (m) {
2342f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2343f8829a4aSRandall Stewart 			caddr_t *from, *to;
2344f8829a4aSRandall Stewart 
2345f8829a4aSRandall Stewart 			/* get the pointers and copy */
2346f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2347f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2348139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2349f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2350139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2351f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2352f8829a4aSRandall Stewart 			/* sucess, back copy */
2353f8829a4aSRandall Stewart 			*mm = m;
2354f8829a4aSRandall Stewart 		} else {
2355f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2356f8829a4aSRandall Stewart 			m = *mm;
2357f8829a4aSRandall Stewart 		}
2358f8829a4aSRandall Stewart 	}
2359f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2360f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2361f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2362f8829a4aSRandall Stewart 	if (ch == NULL) {
2363f8829a4aSRandall Stewart 		return (1);
2364f8829a4aSRandall Stewart 	}
2365f8829a4aSRandall Stewart 	/*
2366f8829a4aSRandall Stewart 	 * process all DATA chunks...
2367f8829a4aSRandall Stewart 	 */
2368f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2369f8829a4aSRandall Stewart 	break_flag = 0;
237042551e99SRandall Stewart 	asoc->data_pkts_seen++;
2371f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2372f8829a4aSRandall Stewart 		/* validate chunk length */
2373f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2374f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2375f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2376f8829a4aSRandall Stewart 			stop_proc = 1;
237760990c0cSMichael Tuexen 			continue;
2378f8829a4aSRandall Stewart 		}
2379f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
238032451da4SMichael Tuexen 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk)) {
2381f8829a4aSRandall Stewart 				/*
2382f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2383f8829a4aSRandall Stewart 				 * invalid data chunk.
2384f8829a4aSRandall Stewart 				 */
2385f8829a4aSRandall Stewart 				struct mbuf *op_err;
2386ff1ffd74SMichael Tuexen 				char msg[SCTP_DIAG_INFO_LEN];
2387f8829a4aSRandall Stewart 
2388ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "DATA chunk of length %d",
2389ff1ffd74SMichael Tuexen 				    chk_length);
2390ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2391a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2392b1754ad1SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
2393b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2394f30ac432SMichael Tuexen 				    use_mflowid, mflowid,
2395f30ac432SMichael Tuexen 				    vrf_id, port);
2396f8829a4aSRandall Stewart 				return (2);
2397f8829a4aSRandall Stewart 			}
239832451da4SMichael Tuexen 			if ((size_t)chk_length == sizeof(struct sctp_data_chunk)) {
239932451da4SMichael Tuexen 				/*
240032451da4SMichael Tuexen 				 * Need to send an abort since we had an
240132451da4SMichael Tuexen 				 * empty data chunk.
240232451da4SMichael Tuexen 				 */
240332451da4SMichael Tuexen 				struct mbuf *op_err;
240432451da4SMichael Tuexen 
240532451da4SMichael Tuexen 				op_err = sctp_generate_no_user_data_cause(ch->dp.tsn);
240632451da4SMichael Tuexen 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
240732451da4SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
240832451da4SMichael Tuexen 				    src, dst, sh, op_err,
240932451da4SMichael Tuexen 				    use_mflowid, mflowid,
241032451da4SMichael Tuexen 				    vrf_id, port);
241132451da4SMichael Tuexen 				return (2);
241232451da4SMichael Tuexen 			}
2413f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2414f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2415f8829a4aSRandall Stewart #endif
2416f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2417f8829a4aSRandall Stewart 				last_chunk = 1;
2418f8829a4aSRandall Stewart 			} else {
2419f8829a4aSRandall Stewart 				last_chunk = 0;
2420f8829a4aSRandall Stewart 			}
2421f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2422f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2423f8829a4aSRandall Stewart 			    last_chunk)) {
2424f8829a4aSRandall Stewart 				num_chunks++;
2425f8829a4aSRandall Stewart 			}
2426f8829a4aSRandall Stewart 			if (abort_flag)
2427f8829a4aSRandall Stewart 				return (2);
2428f8829a4aSRandall Stewart 
2429f8829a4aSRandall Stewart 			if (break_flag) {
2430f8829a4aSRandall Stewart 				/*
2431f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2432f8829a4aSRandall Stewart 				 * drop rep space left.
2433f8829a4aSRandall Stewart 				 */
2434f8829a4aSRandall Stewart 				stop_proc = 1;
243560990c0cSMichael Tuexen 				continue;
2436f8829a4aSRandall Stewart 			}
2437f8829a4aSRandall Stewart 		} else {
2438f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2439f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2440f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2441f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2442f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
244360990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2444f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2445f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2446f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2447f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2448f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2449f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2450f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2451f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2452f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2453f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2454f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2455f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2456f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2457f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2458f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2459f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2460f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2461f8829a4aSRandall Stewart 				/*
2462f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2463f8829a4aSRandall Stewart 				 * are NOT in the right place?
2464f8829a4aSRandall Stewart 				 *
2465f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2466f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2467f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2468f8829a4aSRandall Stewart 				 * possibly process them.
2469f8829a4aSRandall Stewart 				 */
2470b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2471f8829a4aSRandall Stewart 					struct mbuf *op_err;
2472f8829a4aSRandall Stewart 
2473ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
2474f30ac432SMichael Tuexen 					sctp_abort_association(inp, stcb,
2475f30ac432SMichael Tuexen 					    m, iphlen,
2476b1754ad1SMichael Tuexen 					    src, dst,
2477f30ac432SMichael Tuexen 					    sh, op_err,
2478f30ac432SMichael Tuexen 					    use_mflowid, mflowid,
2479f30ac432SMichael Tuexen 					    vrf_id, port);
2480f8829a4aSRandall Stewart 					return (2);
2481f8829a4aSRandall Stewart 				}
2482f8829a4aSRandall Stewart 				break;
2483f8829a4aSRandall Stewart 			default:
2484f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2485f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2486f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2487d61a0ae0SRandall Stewart 					struct mbuf *merr;
2488f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2489f8829a4aSRandall Stewart 
2490eb1b1807SGleb Smirnoff 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_NOWAIT, 1, MT_DATA);
2491d61a0ae0SRandall Stewart 					if (merr) {
2492d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2493f8829a4aSRandall Stewart 						/*
2494f8829a4aSRandall Stewart 						 * We cheat and use param
2495f8829a4aSRandall Stewart 						 * type since we did not
2496f8829a4aSRandall Stewart 						 * bother to define a error
2497f8829a4aSRandall Stewart 						 * cause struct. They are
2498f8829a4aSRandall Stewart 						 * the same basic format
2499f8829a4aSRandall Stewart 						 * with different names.
2500f8829a4aSRandall Stewart 						 */
2501f8829a4aSRandall Stewart 						phd->param_type =
2502f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2503f8829a4aSRandall Stewart 						phd->param_length =
2504f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2505d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2506eb1b1807SGleb Smirnoff 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
2507d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2508921569e2SMichael Tuexen 							if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
2509921569e2SMichael Tuexen 								sctp_m_freem(merr);
2510921569e2SMichael Tuexen 							} else {
2511d61a0ae0SRandall Stewart 								sctp_queue_op_err(stcb, merr);
2512921569e2SMichael Tuexen 							}
2513f8829a4aSRandall Stewart 						} else {
2514d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2515f8829a4aSRandall Stewart 						}
2516f8829a4aSRandall Stewart 					}
2517f8829a4aSRandall Stewart 				}
2518f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2519f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2520f8829a4aSRandall Stewart 					stop_proc = 1;
2521f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2522f8829a4aSRandall Stewart 					 * continue... */
2523f8829a4aSRandall Stewart 				break;
252460990c0cSMichael Tuexen 			}	/* switch of chunk type */
2525f8829a4aSRandall Stewart 		}
2526f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2527f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2528f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2529f8829a4aSRandall Stewart 			stop_proc = 1;
2530f8829a4aSRandall Stewart 			continue;
2531f8829a4aSRandall Stewart 		}
2532f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2533f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2534f8829a4aSRandall Stewart 		if (ch == NULL) {
2535f8829a4aSRandall Stewart 			*offset = length;
2536f8829a4aSRandall Stewart 			stop_proc = 1;
253760990c0cSMichael Tuexen 			continue;
2538f8829a4aSRandall Stewart 		}
253960990c0cSMichael Tuexen 	}
2540f8829a4aSRandall Stewart 	if (break_flag) {
2541f8829a4aSRandall Stewart 		/*
2542f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2543f8829a4aSRandall Stewart 		 */
254420cc2188SMichael Tuexen 		sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2545f8829a4aSRandall Stewart 	}
2546f8829a4aSRandall Stewart 	if (num_chunks) {
2547f8829a4aSRandall Stewart 		/*
2548ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2549f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2550f8829a4aSRandall Stewart 		 */
2551f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2552b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2553c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2554c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2555c4739e2fSRandall Stewart 			    0,
2556c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2557c4739e2fSRandall Stewart 			    __LINE__);
2558c4739e2fSRandall Stewart 		}
2559f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
25606e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2561f8829a4aSRandall Stewart 	}
2562f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2563f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2564f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2565f8829a4aSRandall Stewart 
2566f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
256742551e99SRandall Stewart 		/* Assure that we ack right away */
256842551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2569f8829a4aSRandall Stewart 	}
2570f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
25717215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2572f8829a4aSRandall Stewart 	return (0);
2573f8829a4aSRandall Stewart }
2574f8829a4aSRandall Stewart 
25750fa753b3SRandall Stewart static int
25760fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
25770fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
25780fa753b3SRandall Stewart     int *num_frs,
25790fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
25800fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
25817215cc1bSMichael Tuexen     int *rto_ok)
25820fa753b3SRandall Stewart {
25830fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
25840fa753b3SRandall Stewart 	unsigned int theTSN;
2585b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
25860fa753b3SRandall Stewart 
25870fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
25880fa753b3SRandall Stewart 	tp1 = *p_tp1;
25890fa753b3SRandall Stewart 	if (tp1 == NULL) {
25900fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
25910fa753b3SRandall Stewart 	}
25920fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
25930fa753b3SRandall Stewart 		theTSN = j + last_tsn;
25940fa753b3SRandall Stewart 		while (tp1) {
25950fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
25960fa753b3SRandall Stewart 				(*num_frs) += 1;
25970fa753b3SRandall Stewart 
25980fa753b3SRandall Stewart 			/*-
25990fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
26000fa753b3SRandall Stewart 			 * processed from the sent queue, track the
26010fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
26020fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
26030fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
26040fa753b3SRandall Stewart 			 * and retransmissions.
26050fa753b3SRandall Stewart 			 */
26060fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
26070fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
26080fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
26090fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
26100fa753b3SRandall Stewart 			}
26110fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
26120fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
26130fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
26140fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
26150fa753b3SRandall Stewart 			}
26160fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
26170fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
26180fa753b3SRandall Stewart 					/*-
26190fa753b3SRandall Stewart 					 * must be held until
26200fa753b3SRandall Stewart 					 * cum-ack passes
26210fa753b3SRandall Stewart 					 */
26220fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
26230fa753b3SRandall Stewart 						/*-
26240fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
26250fa753b3SRandall Stewart 						 * now no-longer in flight.
26260fa753b3SRandall Stewart 						 * Higher values may already be set
26270fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
26280fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
26290fa753b3SRandall Stewart 						 */
263020b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
263120b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
26320fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
26330fa753b3SRandall Stewart 						}
26340fa753b3SRandall Stewart 						/*-
26350fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
26360fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
26370fa753b3SRandall Stewart 						 * newly acked. update
26380fa753b3SRandall Stewart 						 * this_sack_highest_newack if
26390fa753b3SRandall Stewart 						 * appropriate.
26400fa753b3SRandall Stewart 						 */
26410fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
26420fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
26430fa753b3SRandall Stewart 
264420b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
264520b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
26460fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
26470fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
26480fa753b3SRandall Stewart 						}
26490fa753b3SRandall Stewart 						/*-
26500fa753b3SRandall Stewart 						 * CMT DAC algo: also update
26510fa753b3SRandall Stewart 						 * this_sack_lowest_newack
26520fa753b3SRandall Stewart 						 */
26530fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
26540fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
26550fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
26560fa753b3SRandall Stewart 								    last_tsn,
26570fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
26580fa753b3SRandall Stewart 								    0,
26590fa753b3SRandall Stewart 								    0,
26600fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
26610fa753b3SRandall Stewart 							}
26620fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
26630fa753b3SRandall Stewart 						}
26640fa753b3SRandall Stewart 						/*-
26650fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
26660fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
26670fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
26680fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
26690fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
26700fa753b3SRandall Stewart 						 * retransmissions.
26710fa753b3SRandall Stewart 						 */
26720fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
26730fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
26740fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
26750fa753b3SRandall Stewart 							}
26760fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
26770fa753b3SRandall Stewart 						}
26780fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
26790fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
26800fa753b3SRandall Stewart 						}
26810fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
26820fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
26830fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
26840fa753b3SRandall Stewart 							}
26850fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
26860fa753b3SRandall Stewart 						}
26870fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
26880fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
26890fa753b3SRandall Stewart 							    last_tsn,
26900fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
26910fa753b3SRandall Stewart 							    frag_strt,
26920fa753b3SRandall Stewart 							    frag_end,
26930fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
26940fa753b3SRandall Stewart 						}
26950fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
26960fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
26970fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
26980fa753b3SRandall Stewart 							    tp1->book_size,
26990fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
27000fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
27010fa753b3SRandall Stewart 						}
27020fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2703299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2704299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2705299108c5SRandall Stewart 							    tp1);
2706299108c5SRandall Stewart 						}
27070fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
27080fa753b3SRandall Stewart 
27090fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
27100fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
27110fa753b3SRandall Stewart 							/*-
27120fa753b3SRandall Stewart 							 * True non-retransmited chunk
27130fa753b3SRandall Stewart 							 */
27140fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
27150fa753b3SRandall Stewart 
27160fa753b3SRandall Stewart 							/*-
27170fa753b3SRandall Stewart 							 * update RTO too ?
27180fa753b3SRandall Stewart 							 */
27190fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2720f79aab18SRandall Stewart 								if (*rto_ok) {
27210fa753b3SRandall Stewart 									tp1->whoTo->RTO =
27220fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
27230fa753b3SRandall Stewart 									    &stcb->asoc,
27240fa753b3SRandall Stewart 									    tp1->whoTo,
27250fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2726899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2727f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2728f79aab18SRandall Stewart 									*rto_ok = 0;
2729f79aab18SRandall Stewart 								}
2730f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2731f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2732f79aab18SRandall Stewart 								}
27330fa753b3SRandall Stewart 								tp1->do_rtt = 0;
27340fa753b3SRandall Stewart 							}
27350fa753b3SRandall Stewart 						}
27360fa753b3SRandall Stewart 					}
27370fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
273820b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
273920b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
27400fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
27410fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
27420fa753b3SRandall Stewart 						}
27430fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
27440fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
27450fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
27460fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
27470fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
27480fa753b3SRandall Stewart #endif
27490fa753b3SRandall Stewart 						}
27500fa753b3SRandall Stewart 					}
27510fa753b3SRandall Stewart 					/*-
27520fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
27530fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
27540fa753b3SRandall Stewart 					 */
27552a498584SMichael Tuexen 					if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
2756325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
27570fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
27582a498584SMichael Tuexen 					}
27590fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
27600fa753b3SRandall Stewart 						/* deflate the cwnd */
27610fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
27620fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
27630fa753b3SRandall Stewart 					}
27640fa753b3SRandall Stewart 					/* NR Sack code here */
2765325c8c46SMichael Tuexen 					if (nr_sacking &&
2766325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2767325c8c46SMichael Tuexen 						if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
2768325c8c46SMichael Tuexen 							stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--;
2769325c8c46SMichael Tuexen #ifdef INVARIANTS
2770325c8c46SMichael Tuexen 						} else {
2771325c8c46SMichael Tuexen 							panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
2772325c8c46SMichael Tuexen #endif
2773325c8c46SMichael Tuexen 						}
2774325c8c46SMichael Tuexen 						tp1->sent = SCTP_DATAGRAM_NR_ACKED;
27750fa753b3SRandall Stewart 						if (tp1->data) {
27760fa753b3SRandall Stewart 							/*
27770fa753b3SRandall Stewart 							 * sa_ignore
27780fa753b3SRandall Stewart 							 * NO_NULL_CHK
27790fa753b3SRandall Stewart 							 */
27800fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
27810fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
27820fa753b3SRandall Stewart 							tp1->data = NULL;
2783b5c16493SMichael Tuexen 						}
27840fa753b3SRandall Stewart 						wake_him++;
27850fa753b3SRandall Stewart 					}
27860fa753b3SRandall Stewart 				}
27870fa753b3SRandall Stewart 				break;
27880fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
278920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
27900fa753b3SRandall Stewart 				break;
279120b07a4dSMichael Tuexen 			}
27920fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
2793b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
2794b5c16493SMichael Tuexen 				circled++;
27950fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
27960fa753b3SRandall Stewart 			}
2797b5c16493SMichael Tuexen 		}		/* end while (tp1) */
2798b5c16493SMichael Tuexen 		if (tp1 == NULL) {
2799b5c16493SMichael Tuexen 			circled = 0;
2800b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2801b5c16493SMichael Tuexen 		}
2802b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
28030fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
28040fa753b3SRandall Stewart 	*p_tp1 = tp1;
28050fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
28060fa753b3SRandall Stewart }
28070fa753b3SRandall Stewart 
28080fa753b3SRandall Stewart 
2809cd554309SMichael Tuexen static int
2810458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2811cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2812139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
28137215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
2814f8829a4aSRandall Stewart {
2815458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
2816f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
28170fa753b3SRandall Stewart 	int i;
2818f8829a4aSRandall Stewart 	int num_frs = 0;
2819cd554309SMichael Tuexen 	int chunk_freed;
2820cd554309SMichael Tuexen 	int non_revocable;
2821d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
2822f8829a4aSRandall Stewart 
2823d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
2824d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
2825cd554309SMichael Tuexen 	chunk_freed = 0;
2826f8829a4aSRandall Stewart 
2827cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
2828d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
2829d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
2830d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2831d9c5cfeaSMichael Tuexen 		}
2832458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2833458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2834458303daSRandall Stewart 		*offset += sizeof(block);
2835458303daSRandall Stewart 		if (frag == NULL) {
2836cd554309SMichael Tuexen 			return (chunk_freed);
2837458303daSRandall Stewart 		}
2838f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
2839f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
2840d9c5cfeaSMichael Tuexen 
2841f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
2842d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
2843f8829a4aSRandall Stewart 			continue;
2844f8829a4aSRandall Stewart 		}
2845d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
2846d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
2847f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2848f8829a4aSRandall Stewart 		}
284920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
2850d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
2851f8829a4aSRandall Stewart 		}
2852cd554309SMichael Tuexen 		if (i < num_seg) {
2853cd554309SMichael Tuexen 			non_revocable = 0;
2854cd554309SMichael Tuexen 		} else {
2855cd554309SMichael Tuexen 			non_revocable = 1;
2856cd554309SMichael Tuexen 		}
2857cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
2858cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
28597215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
2860cd554309SMichael Tuexen 			chunk_freed = 1;
2861458303daSRandall Stewart 		}
2862d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
2863f8829a4aSRandall Stewart 	}
2864b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
286580fefe0aSRandall Stewart 		if (num_frs)
286680fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
286780fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
286880fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
286980fefe0aSRandall Stewart 	}
2870cd554309SMichael Tuexen 	return (chunk_freed);
2871f8829a4aSRandall Stewart }
2872f8829a4aSRandall Stewart 
2873f8829a4aSRandall Stewart static void
2874c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
2875c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
287663eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
2877f8829a4aSRandall Stewart {
2878f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
2879f8829a4aSRandall Stewart 
28804a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
288120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
2882f8829a4aSRandall Stewart 			/*
2883f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
2884f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
2885f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
2886f8829a4aSRandall Stewart 			 * again.
2887f8829a4aSRandall Stewart 			 */
288820b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
2889d06c82f1SRandall Stewart 				break;
289020b07a4dSMichael Tuexen 			}
2891f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
2892f8829a4aSRandall Stewart 				/* it has been revoked */
2893f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
2894f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
2895a5d547adSRandall Stewart 				/*
289642551e99SRandall Stewart 				 * We must add this stuff back in to assure
289742551e99SRandall Stewart 				 * timers and such get started.
2898a5d547adSRandall Stewart 				 */
2899b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2900c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
2901c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
2902c105859eSRandall Stewart 					    tp1->book_size,
2903c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
2904c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
290580fefe0aSRandall Stewart 				}
2906c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
2907c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
290842551e99SRandall Stewart 				/*
290942551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
291042551e99SRandall Stewart 				 * artificial inflation of the flight_size.
291142551e99SRandall Stewart 				 */
291242551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
2913b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2914f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
2915f8829a4aSRandall Stewart 					    cumack,
2916f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
2917f8829a4aSRandall Stewart 					    0,
2918f8829a4aSRandall Stewart 					    0,
2919f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
292080fefe0aSRandall Stewart 				}
2921f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
2922f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
2923f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
2924f8829a4aSRandall Stewart 			}
2925f8829a4aSRandall Stewart 		}
2926f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
2927f8829a4aSRandall Stewart 			break;
2928f8829a4aSRandall Stewart 	}
2929f8829a4aSRandall Stewart }
2930f8829a4aSRandall Stewart 
2931830d754dSRandall Stewart 
2932f8829a4aSRandall Stewart static void
2933f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
293463eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
2935f8829a4aSRandall Stewart {
2936f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
2937f8829a4aSRandall Stewart 	int strike_flag = 0;
2938f8829a4aSRandall Stewart 	struct timeval now;
2939f8829a4aSRandall Stewart 	int tot_retrans = 0;
2940f8829a4aSRandall Stewart 	uint32_t sending_seq;
2941f8829a4aSRandall Stewart 	struct sctp_nets *net;
2942f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
2943f8829a4aSRandall Stewart 
2944f8829a4aSRandall Stewart 	/*
2945f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
2946f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
2947f8829a4aSRandall Stewart 	 */
2948f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
2949f8829a4aSRandall Stewart 	if (tp1 == NULL) {
2950f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
2951f8829a4aSRandall Stewart 	} else {
2952f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
2953f8829a4aSRandall Stewart 	}
2954f8829a4aSRandall Stewart 
2955f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
29567c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
295720083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
2958f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2959f8829a4aSRandall Stewart 			if (net->saw_newack)
2960f8829a4aSRandall Stewart 				num_dests_sacked++;
2961f8829a4aSRandall Stewart 		}
2962f8829a4aSRandall Stewart 	}
2963f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
29646e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2965f8829a4aSRandall Stewart 	}
29664a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
2967f8829a4aSRandall Stewart 		strike_flag = 0;
2968f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
2969f8829a4aSRandall Stewart 			/* this one had a timeout or something */
2970f8829a4aSRandall Stewart 			continue;
2971f8829a4aSRandall Stewart 		}
2972b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2973f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
2974f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
2975f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
2976f8829a4aSRandall Stewart 				    tp1->sent,
2977f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
297880fefe0aSRandall Stewart 		}
297920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
2980f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
2981f8829a4aSRandall Stewart 			/* done */
2982f8829a4aSRandall Stewart 			break;
2983f8829a4aSRandall Stewart 		}
2984f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
2985f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
2986f8829a4aSRandall Stewart 				/* Is it expired? */
298799ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
2988f8829a4aSRandall Stewart 					/* Yes so drop it */
2989f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
29901edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
29910c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
2992f8829a4aSRandall Stewart 					}
2993f8829a4aSRandall Stewart 					continue;
2994f8829a4aSRandall Stewart 				}
2995f8829a4aSRandall Stewart 			}
2996f8829a4aSRandall Stewart 		}
299720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
2998f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
2999f8829a4aSRandall Stewart 			break;
3000f8829a4aSRandall Stewart 		}
3001f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3002f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3003f8829a4aSRandall Stewart 			/* skip */
300444fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
300544fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
300644fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
300744fbe462SRandall Stewart 			}
3008f8829a4aSRandall Stewart 			continue;
3009f8829a4aSRandall Stewart 		}
3010f8829a4aSRandall Stewart 		/*
3011f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3012f8829a4aSRandall Stewart 		 */
3013ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3014f8829a4aSRandall Stewart 			/*
3015f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3016f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3017f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3018c105859eSRandall Stewart 			 * FR using this SACK.
3019f8829a4aSRandall Stewart 			 */
3020f8829a4aSRandall Stewart 			continue;
302120b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
302220b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3023f8829a4aSRandall Stewart 			/*
3024f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3025f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3026f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3027f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3028f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3029f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3030f8829a4aSRandall Stewart 			 */
3031f8829a4aSRandall Stewart 			continue;
3032f8829a4aSRandall Stewart 		}
3033f8829a4aSRandall Stewart 		/*
3034f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3035f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3036f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3037f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3038f8829a4aSRandall Stewart 		 */
3039f8829a4aSRandall Stewart 		/*
304042551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
304142551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
304242551e99SRandall Stewart 		 * 0)) {
3043f8829a4aSRandall Stewart 		 */
304442551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3045f8829a4aSRandall Stewart 			/*
3046f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3047f8829a4aSRandall Stewart 			 * moved.
3048f8829a4aSRandall Stewart 			 */
3049b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3050f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3051f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3052f8829a4aSRandall Stewart 				    tp1->sent,
3053f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
305480fefe0aSRandall Stewart 			}
30555e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3056f8829a4aSRandall Stewart 				tp1->sent++;
30575e54f665SRandall Stewart 			}
30587c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
305920083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3060f8829a4aSRandall Stewart 				/*
3061f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3062f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3063f8829a4aSRandall Stewart 				 * because it would have been set to the
3064f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3065f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3066f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3067c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3068c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3069c105859eSRandall Stewart 				 * indicates that two packets have been
3070c105859eSRandall Stewart 				 * received after this missing TSN.
30715e54f665SRandall Stewart 				 */
30725e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
307320b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3074b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3075f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3076f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3077f8829a4aSRandall Stewart 						    tp1->sent,
3078f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
307980fefe0aSRandall Stewart 					}
3080f8829a4aSRandall Stewart 					tp1->sent++;
3081f8829a4aSRandall Stewart 				}
3082f8829a4aSRandall Stewart 			}
308320083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
308420083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3085f8829a4aSRandall Stewart 			/*
3086f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3087f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3088f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3089f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3090f8829a4aSRandall Stewart 			 */
30915e54f665SRandall Stewart 			if (
3092f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3093f8829a4aSRandall Stewart 			/*
3094f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3095f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3096f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3097f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3098f8829a4aSRandall Stewart 			 */
30995e54f665SRandall Stewart 			    (asoc->numnets < 2)
3100f8829a4aSRandall Stewart #else
31015e54f665SRandall Stewart 			    (1)
3102f8829a4aSRandall Stewart #endif
31035e54f665SRandall Stewart 			    ) {
31045e54f665SRandall Stewart 
310520b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3106f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3107f8829a4aSRandall Stewart 					/*
3108f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3109f8829a4aSRandall Stewart 					 * beyond where things were when we
3110f8829a4aSRandall Stewart 					 * did a FR.
3111f8829a4aSRandall Stewart 					 */
3112b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3113f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3114f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3115f8829a4aSRandall Stewart 						    tp1->sent,
3116f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
311780fefe0aSRandall Stewart 					}
31185e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3119f8829a4aSRandall Stewart 						tp1->sent++;
31205e54f665SRandall Stewart 					}
3121f8829a4aSRandall Stewart 					strike_flag = 1;
31227c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
312320083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3124f8829a4aSRandall Stewart 						/*
3125f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3126f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3127f8829a4aSRandall Stewart 						 * then lowest_newack test
3128f8829a4aSRandall Stewart 						 * will not pass because it
3129f8829a4aSRandall Stewart 						 * would have been set to
3130f8829a4aSRandall Stewart 						 * the cumack earlier. If
3131f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3132f8829a4aSRandall Stewart 						 * If not a mixed sack and
3133f8829a4aSRandall Stewart 						 * if tp1 is not between two
3134f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3135c105859eSRandall Stewart 						 * one more. NOTE that we
3136c105859eSRandall Stewart 						 * are marking by one
3137c105859eSRandall Stewart 						 * additional time since the
3138c105859eSRandall Stewart 						 * SACK DAC flag indicates
3139c105859eSRandall Stewart 						 * that two packets have
3140c105859eSRandall Stewart 						 * been received after this
3141c105859eSRandall Stewart 						 * missing TSN.
3142f8829a4aSRandall Stewart 						 */
31435e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
31445e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
314520b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
314620b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3147b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3148f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3149f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3150f8829a4aSRandall Stewart 								    tp1->sent,
3151f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
315280fefe0aSRandall Stewart 							}
31535e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3154f8829a4aSRandall Stewart 								tp1->sent++;
31555e54f665SRandall Stewart 							}
3156f8829a4aSRandall Stewart 						}
3157f8829a4aSRandall Stewart 					}
3158f8829a4aSRandall Stewart 				}
3159f8829a4aSRandall Stewart 			}
3160f8829a4aSRandall Stewart 			/*
316142551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
316242551e99SRandall Stewart 			 * algo covers HTNA.
3163f8829a4aSRandall Stewart 			 */
316420b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
316520b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3166f8829a4aSRandall Stewart 			/*
3167f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3168f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3169f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3170f8829a4aSRandall Stewart 			 */
3171f8829a4aSRandall Stewart 			;
3172f8829a4aSRandall Stewart 		} else {
3173f8829a4aSRandall Stewart 			/* Strike the TSN */
3174b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3175f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3176f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3177f8829a4aSRandall Stewart 				    tp1->sent,
3178f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
317980fefe0aSRandall Stewart 			}
31805e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3181f8829a4aSRandall Stewart 				tp1->sent++;
31825e54f665SRandall Stewart 			}
31837c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
318420083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3185f8829a4aSRandall Stewart 				/*
3186f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3187f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3188f8829a4aSRandall Stewart 				 * because it would have been set to the
3189f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3190f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3191f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3192c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3193c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3194c105859eSRandall Stewart 				 * indicates that two packets have been
3195c105859eSRandall Stewart 				 * received after this missing TSN.
3196f8829a4aSRandall Stewart 				 */
31975e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
319820b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3199b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3200f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3201f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3202f8829a4aSRandall Stewart 						    tp1->sent,
3203f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
320480fefe0aSRandall Stewart 					}
3205f8829a4aSRandall Stewart 					tp1->sent++;
3206f8829a4aSRandall Stewart 				}
3207f8829a4aSRandall Stewart 			}
3208f8829a4aSRandall Stewart 		}
3209f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3210f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3211f8829a4aSRandall Stewart 
3212544e35bdSRandall Stewart 			/* fix counts and things */
3213544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3214544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3215544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3216544e35bdSRandall Stewart 				    tp1->book_size,
3217544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3218544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3219544e35bdSRandall Stewart 			}
3220544e35bdSRandall Stewart 			if (tp1->whoTo) {
3221544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3222544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3223299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3224299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3225299108c5SRandall Stewart 					    tp1);
3226299108c5SRandall Stewart 				}
3227544e35bdSRandall Stewart 			}
3228544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3229544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3230544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3231544e35bdSRandall Stewart 			}
3232544e35bdSRandall Stewart 			/* add back to the rwnd */
3233544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3234544e35bdSRandall Stewart 
3235544e35bdSRandall Stewart 			/* remove from the total flight */
3236544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3237544e35bdSRandall Stewart 
3238475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3239475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3240475d0674SMichael Tuexen 				/*
3241475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3242475d0674SMichael Tuexen 				 * we store the retran count there.
3243475d0674SMichael Tuexen 				 */
3244475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3245475d0674SMichael Tuexen 					/* Yes, so drop it */
3246475d0674SMichael Tuexen 					if (tp1->data != NULL) {
32471edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3248475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3249475d0674SMichael Tuexen 					}
3250475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3251475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3252475d0674SMichael Tuexen 					continue;
3253475d0674SMichael Tuexen 				}
3254475d0674SMichael Tuexen 			}
3255cd3fd531SMichael Tuexen 			/*
3256cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3257cd3fd531SMichael Tuexen 			 * guy\n");
3258cd3fd531SMichael Tuexen 			 */
3259b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3260f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3261f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
326280fefe0aSRandall Stewart 			}
3263f8829a4aSRandall Stewart 			if (strike_flag) {
3264f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3265f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3266f8829a4aSRandall Stewart 			}
32675e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
32687c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3269f8829a4aSRandall Stewart 				/*
3270f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3271f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3272f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3273f8829a4aSRandall Stewart 				 */
3274f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3275f8829a4aSRandall Stewart 				alt = tp1->whoTo;
32763c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
327720083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3278b54d3a6cSRandall Stewart 					/*
3279b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3280b54d3a6cSRandall Stewart 					 * use the PF version of
3281b54d3a6cSRandall Stewart 					 * find_alt_net()
3282b54d3a6cSRandall Stewart 					 */
3283b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3284b54d3a6cSRandall Stewart 				} else {
3285b54d3a6cSRandall Stewart 					/*
3286b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3287b54d3a6cSRandall Stewart 					 * use the CMT version of
3288b54d3a6cSRandall Stewart 					 * find_alt_net()
3289b54d3a6cSRandall Stewart 					 */
329052be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3291f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3292b54d3a6cSRandall Stewart 				}
3293ad81507eSRandall Stewart 				if (alt == NULL) {
3294ad81507eSRandall Stewart 					alt = tp1->whoTo;
3295ad81507eSRandall Stewart 				}
3296f8829a4aSRandall Stewart 				/*
3297f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3298f8829a4aSRandall Stewart 				 * the retransmission, then new
3299f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3300f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3301f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3302f8829a4aSRandall Stewart 				 */
3303ad81507eSRandall Stewart 				if (tp1->whoTo) {
3304f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3305f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3306ad81507eSRandall Stewart 				}
3307f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3308f8829a4aSRandall Stewart 
3309f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3310f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3311f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3312f8829a4aSRandall Stewart #else
3313f8829a4aSRandall Stewart 				/*
3314f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3315f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3316f8829a4aSRandall Stewart 				 * paper details why.
3317f8829a4aSRandall Stewart 				 */
3318f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3319f8829a4aSRandall Stewart #endif
3320f8829a4aSRandall Stewart 			}
3321f8829a4aSRandall Stewart 
3322f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3323f8829a4aSRandall Stewart 			tot_retrans++;
3324f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3325f8829a4aSRandall Stewart 			/*
3326cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3327f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3328f8829a4aSRandall Stewart 			 */
3329f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3330f8829a4aSRandall Stewart 				/*
3331f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3332f8829a4aSRandall Stewart 				 * the next sequence number that will be
3333f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3334f8829a4aSRandall Stewart 				 * get the one we last sent.
3335f8829a4aSRandall Stewart 				 */
3336f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3337f8829a4aSRandall Stewart 			} else {
3338f8829a4aSRandall Stewart 				/*
3339f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3340f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3341f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3342f8829a4aSRandall Stewart 				 * take the first one (which will have the
3343f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3344f8829a4aSRandall Stewart 				 * one we last sent.
3345f8829a4aSRandall Stewart 				 */
3346f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3347f8829a4aSRandall Stewart 
3348f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3349f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3350f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3351f8829a4aSRandall Stewart 			}
3352f8829a4aSRandall Stewart 
3353f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3354f8829a4aSRandall Stewart 				/*
3355f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3356f8829a4aSRandall Stewart 				 * it, cancel it
3357f8829a4aSRandall Stewart 				 */
335860990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
335960990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3360f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3361f79aab18SRandall Stewart 				}
3362f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3363f8829a4aSRandall Stewart 			}
3364f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3365f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3366f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
33673c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3368f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3369f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3370f8829a4aSRandall Stewart 			}
3371f8829a4aSRandall Stewart 		}
33724a9ef3f8SMichael Tuexen 	}
3373f8829a4aSRandall Stewart }
3374f8829a4aSRandall Stewart 
3375f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3376f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3377f8829a4aSRandall Stewart     struct sctp_association *asoc)
3378f8829a4aSRandall Stewart {
3379f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3380f8829a4aSRandall Stewart 	struct timeval now;
3381f8829a4aSRandall Stewart 	int now_filled = 0;
3382f8829a4aSRandall Stewart 
3383f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3384f8829a4aSRandall Stewart 		return (NULL);
3385f8829a4aSRandall Stewart 	}
33864a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3387f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
338898f2956cSMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_RESEND &&
3389325c8c46SMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3390f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3391f8829a4aSRandall Stewart 			break;
3392f8829a4aSRandall Stewart 		}
33930c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
33942a498584SMichael Tuexen 			if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3395325c8c46SMichael Tuexen 			    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
33960c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
33970c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
33980c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
33990c0982b8SRandall Stewart 			}
34000c0982b8SRandall Stewart 		}
3401f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3402f8829a4aSRandall Stewart 			/*
3403f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3404f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3405f8829a4aSRandall Stewart 			 */
3406f8829a4aSRandall Stewart 			break;
3407f8829a4aSRandall Stewart 		}
3408f8829a4aSRandall Stewart 		if (!now_filled) {
34096e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3410f8829a4aSRandall Stewart 			now_filled = 1;
3411f8829a4aSRandall Stewart 		}
3412f8829a4aSRandall Stewart 		/*
3413f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3414f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3415f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3416f8829a4aSRandall Stewart 		 * it if its a resend?
3417f8829a4aSRandall Stewart 		 */
3418f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3419f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3420f8829a4aSRandall Stewart 			/*
3421f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3422f8829a4aSRandall Stewart 			 * now up?
3423f8829a4aSRandall Stewart 			 */
3424f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3425f8829a4aSRandall Stewart 				/* Yes so drop it */
3426f8829a4aSRandall Stewart 				if (tp1->data) {
3427ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
34281edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3429f8829a4aSRandall Stewart 				}
3430f8829a4aSRandall Stewart 			} else {
3431f8829a4aSRandall Stewart 				/*
3432f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3433f8829a4aSRandall Stewart 				 * whos time as not expired.
3434f8829a4aSRandall Stewart 				 */
3435f8829a4aSRandall Stewart 				break;
3436f8829a4aSRandall Stewart 			}
3437f8829a4aSRandall Stewart 		}
3438f8829a4aSRandall Stewart 		/*
3439f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3440f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3441f8829a4aSRandall Stewart 		 * the next chunk.
3442f8829a4aSRandall Stewart 		 */
344398f2956cSMichael Tuexen 		if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3444325c8c46SMichael Tuexen 		    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3445f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
344620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3447f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3448f8829a4aSRandall Stewart 				a_adv = tp1;
34490c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
34500c0982b8SRandall Stewart 				/* No update but we do save the chk */
34510c0982b8SRandall Stewart 				a_adv = tp1;
34520c0982b8SRandall Stewart 			}
3453f8829a4aSRandall Stewart 		} else {
3454f8829a4aSRandall Stewart 			/*
3455f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3456f8829a4aSRandall Stewart 			 * further
3457f8829a4aSRandall Stewart 			 */
3458f8829a4aSRandall Stewart 			break;
3459f8829a4aSRandall Stewart 		}
3460f8829a4aSRandall Stewart 	}
3461f8829a4aSRandall Stewart 	return (a_adv);
3462f8829a4aSRandall Stewart }
3463f8829a4aSRandall Stewart 
34640c0982b8SRandall Stewart static int
3465c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3466bff64a4dSRandall Stewart {
3467bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3468bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
34690c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
34700c0982b8SRandall Stewart 
34710c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
34720c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
34730c0982b8SRandall Stewart 	ret = 0;
34740c0982b8SRandall Stewart 
34750c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
34760c0982b8SRandall Stewart 		return (0);
3477bff64a4dSRandall Stewart 
3478bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3479bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3480cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n",
34810c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
34820c0982b8SRandall Stewart 			    chk->send_size,
3483cd3fd531SMichael Tuexen 			    chk->snd_count);
3484bff64a4dSRandall Stewart 			inflight++;
3485bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3486bff64a4dSRandall Stewart 			resend++;
3487bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3488bff64a4dSRandall Stewart 			inbetween++;
3489bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3490bff64a4dSRandall Stewart 			above++;
3491bff64a4dSRandall Stewart 		} else {
3492bff64a4dSRandall Stewart 			acked++;
3493bff64a4dSRandall Stewart 		}
3494bff64a4dSRandall Stewart 	}
3495f1f73e57SRandall Stewart 
3496c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3497f1f73e57SRandall Stewart #ifdef INVARIANTS
3498c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3499f1f73e57SRandall Stewart #else
3500cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n",
35010c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
35020c0982b8SRandall Stewart 
35030c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
35040c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
35050c0982b8SRandall Stewart 		ret = 1;
3506f1f73e57SRandall Stewart #endif
3507bff64a4dSRandall Stewart 	}
35080c0982b8SRandall Stewart 	return (ret);
3509c105859eSRandall Stewart }
3510c105859eSRandall Stewart 
3511c105859eSRandall Stewart 
3512c105859eSRandall Stewart static void
3513c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3514c105859eSRandall Stewart     struct sctp_association *asoc,
3515c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3516c105859eSRandall Stewart {
3517dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
35185171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3519dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3520dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3521dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3522dfb11ef8SRandall Stewart 		    tp1->book_size,
3523dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3524dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3525dfb11ef8SRandall Stewart 		return;
3526dfb11ef8SRandall Stewart 	}
35275171328bSRandall Stewart 	/* First setup this by shrinking flight */
3528299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3529299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3530299108c5SRandall Stewart 		    tp1);
3531299108c5SRandall Stewart 	}
35325171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
35335171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
35345171328bSRandall Stewart 	/* Now mark for resend */
35355171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3536791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3537791437b5SRandall Stewart 
3538b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3539c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3540c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3541c105859eSRandall Stewart 		    tp1->book_size,
3542c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3543c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
354480fefe0aSRandall Stewart 	}
3545c105859eSRandall Stewart }
3546c105859eSRandall Stewart 
3547f8829a4aSRandall Stewart void
3548f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3549899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3550f8829a4aSRandall Stewart {
3551f8829a4aSRandall Stewart 	struct sctp_nets *net;
3552f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3553f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
35545e54f665SRandall Stewart 	uint32_t old_rwnd;
35555e54f665SRandall Stewart 	int win_probe_recovery = 0;
3556c105859eSRandall Stewart 	int win_probe_recovered = 0;
3557d06c82f1SRandall Stewart 	int j, done_once = 0;
3558f79aab18SRandall Stewart 	int rto_ok = 1;
3559f8829a4aSRandall Stewart 
3560b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3561d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3562d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
356380fefe0aSRandall Stewart 	}
3564f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
356518e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
356618e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
356718e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
356818e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
356918e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
357018e198d3SRandall Stewart 	}
357118e198d3SRandall Stewart #endif
3572f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3573d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
357420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
35755e54f665SRandall Stewart 		/* old ack */
35765e54f665SRandall Stewart 		return;
3577d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3578d06c82f1SRandall Stewart 		/* Window update sack */
3579d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
358044fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3581d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3582d06c82f1SRandall Stewart 			/* SWS sender side engages */
3583d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3584d06c82f1SRandall Stewart 		}
3585d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3586d06c82f1SRandall Stewart 			goto again;
3587d06c82f1SRandall Stewart 		}
3588d06c82f1SRandall Stewart 		return;
35895e54f665SRandall Stewart 	}
3590f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3591f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3592a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3593a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3594a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3595a21779f0SRandall Stewart 		}
3596f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3597f8829a4aSRandall Stewart 		net->net_ack = 0;
3598f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3599132dea7dSRandall Stewart 
3600132dea7dSRandall Stewart 		/*
3601132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3602132dea7dSRandall Stewart 		 * SACK processing
3603132dea7dSRandall Stewart 		 */
3604132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3605132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3606299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3607299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3608299108c5SRandall Stewart 		}
3609f8829a4aSRandall Stewart 	}
3610b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3611139bc87fSRandall Stewart 		uint32_t send_s;
3612139bc87fSRandall Stewart 
3613c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3614c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3615c105859eSRandall Stewart 			    sctpchunk_listhead);
3616c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3617139bc87fSRandall Stewart 		} else {
3618c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3619139bc87fSRandall Stewart 		}
362020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3621c105859eSRandall Stewart #ifndef INVARIANTS
3622ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
3623ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
3624139bc87fSRandall Stewart 
3625c105859eSRandall Stewart #endif
3626c105859eSRandall Stewart #ifdef INVARIANTS
3627c105859eSRandall Stewart 			panic("Impossible sack 1");
3628c105859eSRandall Stewart #else
3629b5c16493SMichael Tuexen 
3630139bc87fSRandall Stewart 			*abort_now = 1;
3631139bc87fSRandall Stewart 			/* XXX */
3632ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal then TSN %8.8x",
3633ff1ffd74SMichael Tuexen 			    cumack, send_s);
3634ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
3635139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3636ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3637139bc87fSRandall Stewart 			return;
3638139bc87fSRandall Stewart #endif
3639139bc87fSRandall Stewart 		}
3640139bc87fSRandall Stewart 	}
3641f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3642b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3643c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3644c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3645c4739e2fSRandall Stewart 		    0,
3646c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3647c4739e2fSRandall Stewart 		    __LINE__);
3648c4739e2fSRandall Stewart 	}
3649f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
365020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3651f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
36524a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
365320b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
365418e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3655cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
365618e198d3SRandall Stewart 				}
3657f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3658f8829a4aSRandall Stewart 					/*
365918e198d3SRandall Stewart 					 * If it is less than ACKED, it is
366018e198d3SRandall Stewart 					 * now no-longer in flight. Higher
366118e198d3SRandall Stewart 					 * values may occur during marking
3662f8829a4aSRandall Stewart 					 */
3663c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3664b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3665c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3666a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3667a5d547adSRandall Stewart 							    tp1->book_size,
3668c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3669a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
367080fefe0aSRandall Stewart 						}
3671c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3672299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3673299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3674299108c5SRandall Stewart 							    tp1);
3675299108c5SRandall Stewart 						}
367604ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3677c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3678f8829a4aSRandall Stewart 					}
3679f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3680f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3681f8829a4aSRandall Stewart 						/*
368218e198d3SRandall Stewart 						 * True non-retransmited
3683f8829a4aSRandall Stewart 						 * chunk
3684f8829a4aSRandall Stewart 						 */
3685f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3686f8829a4aSRandall Stewart 						    tp1->send_size;
3687f8829a4aSRandall Stewart 
3688f8829a4aSRandall Stewart 						/* update RTO too? */
368962c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3690f79aab18SRandall Stewart 							if (rto_ok) {
3691f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
369204ee05e8SRandall Stewart 								/*
369304ee05e8SRandall Stewart 								 * sa_ignore
3694f79aab18SRandall Stewart 								 * NO_NULL_CH
3695f79aab18SRandall Stewart 								 * K
369604ee05e8SRandall Stewart 								 */
3697f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3698f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
369918e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3700899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3701f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3702f79aab18SRandall Stewart 								rto_ok = 0;
3703f79aab18SRandall Stewart 							}
3704f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3705f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3706f79aab18SRandall Stewart 							}
3707f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3708f8829a4aSRandall Stewart 						}
3709f8829a4aSRandall Stewart 					}
3710132dea7dSRandall Stewart 					/*
371118e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
371218e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
371318e198d3SRandall Stewart 					 * acked for the first time, set the
371418e198d3SRandall Stewart 					 * following variables for the
371518e198d3SRandall Stewart 					 * corresp destination.
371618e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
371718e198d3SRandall Stewart 					 * cwnd update.
371818e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
371918e198d3SRandall Stewart 					 * trigger search for the next
372018e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3721132dea7dSRandall Stewart 					 */
3722132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3723132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3724132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3725132dea7dSRandall Stewart 
3726b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
372704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3728f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
372980fefe0aSRandall Stewart 					}
3730f8829a4aSRandall Stewart 				}
3731f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3732f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3733f8829a4aSRandall Stewart 				}
373442551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
373542551e99SRandall Stewart 					/* deflate the cwnd */
373642551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
373742551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
373842551e99SRandall Stewart 				}
3739325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3740a7ad6026SMichael Tuexen 					if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
3741a7ad6026SMichael Tuexen 						asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
3742a7ad6026SMichael Tuexen #ifdef INVARIANTS
3743a7ad6026SMichael Tuexen 					} else {
3744a7ad6026SMichael Tuexen 						panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
3745a7ad6026SMichael Tuexen #endif
3746a7ad6026SMichael Tuexen 					}
3747a7ad6026SMichael Tuexen 				}
3748f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3749f8829a4aSRandall Stewart 				if (tp1->data) {
375004ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3751f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3752f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
37534a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3754f8829a4aSRandall Stewart 				}
3755b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3756f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3757f8829a4aSRandall Stewart 					    cumack,
3758f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3759f8829a4aSRandall Stewart 					    0,
3760f8829a4aSRandall Stewart 					    0,
3761f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
376280fefe0aSRandall Stewart 				}
3763f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3764689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
376518e198d3SRandall Stewart 			} else {
376618e198d3SRandall Stewart 				break;
3767f8829a4aSRandall Stewart 			}
37685e54f665SRandall Stewart 		}
376918e198d3SRandall Stewart 
377018e198d3SRandall Stewart 	}
377104ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3772f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3773ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3774ceaad40aSRandall Stewart 		struct socket *so;
3775ceaad40aSRandall Stewart 
3776ceaad40aSRandall Stewart #endif
3777f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3778b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
377904ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
37807215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
378180fefe0aSRandall Stewart 		}
3782ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3783ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3784ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3785ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3786ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3787ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3788ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3789ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3790ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
3791ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3792ceaad40aSRandall Stewart 			return;
3793ceaad40aSRandall Stewart 		}
3794ceaad40aSRandall Stewart #endif
3795f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
3796ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3797ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3798ceaad40aSRandall Stewart #endif
3799f8829a4aSRandall Stewart 	} else {
3800b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
38017215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
380280fefe0aSRandall Stewart 		}
3803f8829a4aSRandall Stewart 	}
3804f8829a4aSRandall Stewart 
3805b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
3806ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
3807ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3808ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
3809ca85e948SMichael Tuexen 				/*
3810ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
3811ca85e948SMichael Tuexen 				 * count, this is optional.
3812ca85e948SMichael Tuexen 				 */
3813ca85e948SMichael Tuexen 				net->error_count = 0;
3814ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
3815ca85e948SMichael Tuexen 					/* addr came good */
3816ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
3817ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
38184b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
3819ca85e948SMichael Tuexen 				}
3820ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
3821ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
3822ca85e948SMichael Tuexen 						/*
3823ca85e948SMichael Tuexen 						 * release the alternate,
3824ca85e948SMichael Tuexen 						 * primary is good
3825ca85e948SMichael Tuexen 						 */
3826ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
3827ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
3828ca85e948SMichael Tuexen 					}
3829ca85e948SMichael Tuexen 				}
3830ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
3831ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
3832ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
3833ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
3834ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
3835ca85e948SMichael Tuexen 					/* Done with this net */
3836ca85e948SMichael Tuexen 					net->net_ack = 0;
3837ca85e948SMichael Tuexen 				}
3838ca85e948SMichael Tuexen 				/* restore any doubled timers */
3839ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3840ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
3841ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
3842ca85e948SMichael Tuexen 				}
3843ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
3844ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
3845ca85e948SMichael Tuexen 				}
3846ca85e948SMichael Tuexen 			}
3847ca85e948SMichael Tuexen 		}
3848b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
3849ca85e948SMichael Tuexen 	}
3850f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
38515e54f665SRandall Stewart 
3852f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
3853f8829a4aSRandall Stewart 		/* nothing left in-flight */
3854f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3855f8829a4aSRandall Stewart 			net->flight_size = 0;
3856f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
3857f8829a4aSRandall Stewart 		}
3858f8829a4aSRandall Stewart 		asoc->total_flight = 0;
3859f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
3860f8829a4aSRandall Stewart 	}
3861f8829a4aSRandall Stewart 	/* RWND update */
3862f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
386344fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3864f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3865f8829a4aSRandall Stewart 		/* SWS sender side engages */
3866f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
3867f8829a4aSRandall Stewart 	}
38685e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
38695e54f665SRandall Stewart 		win_probe_recovery = 1;
38705e54f665SRandall Stewart 	}
3871f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
3872a5d547adSRandall Stewart again:
3873a5d547adSRandall Stewart 	j = 0;
3874f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
38755171328bSRandall Stewart 		int to_ticks;
38765171328bSRandall Stewart 
38775e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
3878c105859eSRandall Stewart 			win_probe_recovered = 1;
38795e54f665SRandall Stewart 			/*
38805e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
38815e54f665SRandall Stewart 			 * and clear the sent
38825e54f665SRandall Stewart 			 */
38833c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
38845e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
38855e54f665SRandall Stewart 				if (tp1->window_probe) {
3886cd554309SMichael Tuexen 					/* move back to data send queue */
38877215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
38885e54f665SRandall Stewart 					break;
38895e54f665SRandall Stewart 				}
38905e54f665SRandall Stewart 			}
38915e54f665SRandall Stewart 		}
3892f8829a4aSRandall Stewart 		if (net->RTO == 0) {
3893f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
3894f8829a4aSRandall Stewart 		} else {
3895f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
3896f8829a4aSRandall Stewart 		}
38975171328bSRandall Stewart 		if (net->flight_size) {
3898a5d547adSRandall Stewart 			j++;
3899ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
3900f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
39015171328bSRandall Stewart 			if (net->window_probe) {
39025171328bSRandall Stewart 				net->window_probe = 0;
39035171328bSRandall Stewart 			}
3904f8829a4aSRandall Stewart 		} else {
39055171328bSRandall Stewart 			if (net->window_probe) {
39065171328bSRandall Stewart 				/*
39075171328bSRandall Stewart 				 * In window probes we must assure a timer
39085171328bSRandall Stewart 				 * is still running there
39095171328bSRandall Stewart 				 */
39105171328bSRandall Stewart 				net->window_probe = 0;
39115171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
39125171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
39135171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
39145171328bSRandall Stewart 				}
39155171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3916f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3917a5d547adSRandall Stewart 				    stcb, net,
3918a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
3919f8829a4aSRandall Stewart 			}
3920f8829a4aSRandall Stewart 		}
3921f8829a4aSRandall Stewart 	}
3922bff64a4dSRandall Stewart 	if ((j == 0) &&
3923bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
3924bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
3925c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
3926bff64a4dSRandall Stewart 	    (done_once == 0)) {
39270c0982b8SRandall Stewart 		/*
39280c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
39290c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
39300c0982b8SRandall Stewart 		 */
39310c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
3932a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3933a5d547adSRandall Stewart 				net->flight_size = 0;
3934a5d547adSRandall Stewart 			}
3935a5d547adSRandall Stewart 			asoc->total_flight = 0;
3936a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
3937a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
3938a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3939a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3940c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
3941c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
3942a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3943791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3944a5d547adSRandall Stewart 				}
3945a5d547adSRandall Stewart 			}
39460c0982b8SRandall Stewart 		}
3947bff64a4dSRandall Stewart 		done_once = 1;
3948a5d547adSRandall Stewart 		goto again;
3949a5d547adSRandall Stewart 	}
3950f8829a4aSRandall Stewart 	/**********************************/
3951f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
3952f8829a4aSRandall Stewart 	/**********************************/
3953f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
3954f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
3955f8829a4aSRandall Stewart 		/* clean up */
3956f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
3957f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
3958f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
3959f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
3960f8829a4aSRandall Stewart 		    ) {
3961f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
3962f8829a4aSRandall Stewart 
3963f8829a4aSRandall Stewart 			/*
3964f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
3965f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
3966f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
3967f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
3968f8829a4aSRandall Stewart 			 */
3969f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
3970f8829a4aSRandall Stewart 			    sctp_streamhead);
39712afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
39722afb3e84SRandall Stewart 				/* Let cleanup code purge it */
39732afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
39742afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
39752afb3e84SRandall Stewart 				} else {
3976f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3977f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
3978f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
3979f8829a4aSRandall Stewart 				}
3980f8829a4aSRandall Stewart 			}
39812afb3e84SRandall Stewart 		}
3982f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
3983f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
3984f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
3985f8829a4aSRandall Stewart 				/* Need to abort here */
3986ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
3987f8829a4aSRandall Stewart 
3988f8829a4aSRandall Stewart 		abort_out_now:
3989f8829a4aSRandall Stewart 				*abort_now = 1;
3990f8829a4aSRandall Stewart 				/* XXX */
3991ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3992a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
3993ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3994f8829a4aSRandall Stewart 			} else {
3995ca85e948SMichael Tuexen 				struct sctp_nets *netp;
3996ca85e948SMichael Tuexen 
3997f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3998f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3999f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4000f42a358aSRandall Stewart 				}
4001c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4002b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4003f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4004ca85e948SMichael Tuexen 				if (asoc->alternate) {
4005ca85e948SMichael Tuexen 					netp = asoc->alternate;
4006ca85e948SMichael Tuexen 				} else {
4007ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4008ca85e948SMichael Tuexen 				}
4009ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4010f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4011ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4012f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4013ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4014f8829a4aSRandall Stewart 			}
4015f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4016f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4017ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4018ca85e948SMichael Tuexen 
4019f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4020f8829a4aSRandall Stewart 				goto abort_out_now;
4021f8829a4aSRandall Stewart 			}
4022f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4023c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4024b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
402512af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4026c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4027c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4028c39cfa1fSMichael Tuexen 			} else {
4029c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4030c39cfa1fSMichael Tuexen 			}
4031c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4032f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4033ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4034f8829a4aSRandall Stewart 		}
4035f8829a4aSRandall Stewart 	}
4036dfb11ef8SRandall Stewart 	/*********************************************/
4037dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4038dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4039dfb11ef8SRandall Stewart 	/*********************************************/
4040dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
404120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4042dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4043dfb11ef8SRandall Stewart 	}
4044830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4045830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4046830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4047830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4048830d754dSRandall Stewart 
4049830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4050830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4051830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
405220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4053830d754dSRandall Stewart 			/*
4054493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4055830d754dSRandall Stewart 			 */
405620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4057830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
40580c0982b8SRandall Stewart 			} else if (lchk) {
40590c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
406044fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
40610c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
40620c0982b8SRandall Stewart 				}
4063830d754dSRandall Stewart 			}
4064830d754dSRandall Stewart 		}
4065830d754dSRandall Stewart 		if (lchk) {
4066830d754dSRandall Stewart 			/* Assure a timer is up */
4067830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4068830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4069830d754dSRandall Stewart 		}
4070830d754dSRandall Stewart 	}
4071b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4072f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4073f8829a4aSRandall Stewart 		    rwnd,
4074f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4075f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4076f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
407780fefe0aSRandall Stewart 	}
4078f8829a4aSRandall Stewart }
4079f8829a4aSRandall Stewart 
4080f8829a4aSRandall Stewart void
4081cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
40827215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4083cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4084cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4085899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4086f8829a4aSRandall Stewart {
4087f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4088f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4089cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4090f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4091c105859eSRandall Stewart 	uint32_t send_s = 0;
4092f8829a4aSRandall Stewart 	long j;
4093f8829a4aSRandall Stewart 	int accum_moved = 0;
4094f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
40955e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
40965e54f665SRandall Stewart 	int win_probe_recovery = 0;
4097c105859eSRandall Stewart 	int win_probe_recovered = 0;
4098f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4099bff64a4dSRandall Stewart 	int done_once;
4100f79aab18SRandall Stewart 	int rto_ok = 1;
4101f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4102f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4103f8829a4aSRandall Stewart 
4104f8829a4aSRandall Stewart 	/*
4105f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4106f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4107f8829a4aSRandall Stewart 	 */
4108f8829a4aSRandall Stewart 	/*
4109f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4110f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4111f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4112f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4113f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4114f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4115f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4116f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4117f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4118f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4119f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4120f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4121f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4122f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4123f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4124f8829a4aSRandall Stewart 	 */
4125f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4126f8829a4aSRandall Stewart 	/* CMT DAC algo */
4127f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4128f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4129cd554309SMichael Tuexen 	last_tsn = cum_ack;
4130cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
413118e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
413218e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
413318e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
413418e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
413518e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
413618e198d3SRandall Stewart 	}
413718e198d3SRandall Stewart #endif
4138d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4139f8829a4aSRandall Stewart 
4140cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4141cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4142cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4143cd554309SMichael Tuexen 	}
41445e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4145b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4146c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4147c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4148c4739e2fSRandall Stewart 		    0,
4149c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4150c4739e2fSRandall Stewart 		    __LINE__);
4151c4739e2fSRandall Stewart 	}
4152f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4153f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4154b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4155f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4156f8829a4aSRandall Stewart 		    cum_ack,
4157f8829a4aSRandall Stewart 		    0,
4158f8829a4aSRandall Stewart 		    num_seg,
4159f8829a4aSRandall Stewart 		    num_dup,
4160f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
416180fefe0aSRandall Stewart 	}
4162ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4163cd554309SMichael Tuexen 		uint16_t i;
4164458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4165f8829a4aSRandall Stewart 
4166cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4167cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4168458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4169cd554309SMichael Tuexen 			if (dupdata == NULL) {
4170458303daSRandall Stewart 				break;
4171458303daSRandall Stewart 			}
4172cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4173f8829a4aSRandall Stewart 		}
4174f8829a4aSRandall Stewart 	}
4175b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4176c105859eSRandall Stewart 		/* reality check */
4177c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4178c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4179c105859eSRandall Stewart 			    sctpchunk_listhead);
4180c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4181c105859eSRandall Stewart 		} else {
4182b5c16493SMichael Tuexen 			tp1 = NULL;
4183c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4184c105859eSRandall Stewart 		}
418520b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4186ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
4187ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
4188c105859eSRandall Stewart 
4189f8829a4aSRandall Stewart 			/*
4190f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4191f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4192f8829a4aSRandall Stewart 			 */
4193cd3fd531SMichael Tuexen 			SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4194b5c16493SMichael Tuexen 			    cum_ack, send_s);
4195b5c16493SMichael Tuexen 			if (tp1) {
4196cd3fd531SMichael Tuexen 				SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n",
4197dd294dceSMichael Tuexen 				    tp1->rec.data.TSN_seq, (void *)tp1);
4198b5c16493SMichael Tuexen 			}
4199f8829a4aSRandall Stewart 	hopeless_peer:
4200f8829a4aSRandall Stewart 			*abort_now = 1;
4201f8829a4aSRandall Stewart 			/* XXX */
4202ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal then TSN %8.8x",
4203ff1ffd74SMichael Tuexen 			    cum_ack, send_s);
4204ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4205a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4206ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4207f8829a4aSRandall Stewart 			return;
4208f8829a4aSRandall Stewart 		}
4209f8829a4aSRandall Stewart 	}
4210f8829a4aSRandall Stewart 	/**********************/
4211f8829a4aSRandall Stewart 	/* 1) check the range */
4212f8829a4aSRandall Stewart 	/**********************/
421320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4214f8829a4aSRandall Stewart 		/* acking something behind */
4215f8829a4aSRandall Stewart 		return;
4216f8829a4aSRandall Stewart 	}
4217f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4218f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4219f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4220cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4221f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4222b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4223f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4224f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
422580fefe0aSRandall Stewart 		}
4226f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4227f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4228f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4229f8829a4aSRandall Stewart 		}
4230f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4231f8829a4aSRandall Stewart 			/* SWS sender side engages */
4232f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4233f8829a4aSRandall Stewart 		}
4234f8829a4aSRandall Stewart 		/* stop any timers */
4235f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4236f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4237a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4238f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4239f8829a4aSRandall Stewart 			net->flight_size = 0;
4240f8829a4aSRandall Stewart 		}
4241f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4242f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4243f8829a4aSRandall Stewart 		return;
4244f8829a4aSRandall Stewart 	}
4245f8829a4aSRandall Stewart 	/*
4246f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4247f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4248f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4249f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4250f8829a4aSRandall Stewart 	 * destination address basis.
4251f8829a4aSRandall Stewart 	 */
4252f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4253a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4254a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4255a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4256a21779f0SRandall Stewart 		}
4257f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4258f8829a4aSRandall Stewart 		net->net_ack = 0;
4259f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4260f8829a4aSRandall Stewart 
4261f8829a4aSRandall Stewart 		/*
426242551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
426342551e99SRandall Stewart 		 * SACK processing
4264f8829a4aSRandall Stewart 		 */
4265f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4266f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4267299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4268299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4269299108c5SRandall Stewart 		}
4270f8829a4aSRandall Stewart 	}
4271f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
42724a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
427320b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4274f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4275f8829a4aSRandall Stewart 				accum_moved = 1;
4276f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4277f8829a4aSRandall Stewart 					/*
4278f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4279f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4280f8829a4aSRandall Stewart 					 * values may occur during marking
4281f8829a4aSRandall Stewart 					 */
4282f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4283f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4284f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4285f8829a4aSRandall Stewart 						/*
4286f8829a4aSRandall Stewart 						 * If there was no retran
4287f8829a4aSRandall Stewart 						 * and the address is
4288f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4289f8829a4aSRandall Stewart 						 * there and are now
4290f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4291f8829a4aSRandall Stewart 						 * mark it so.
4292f8829a4aSRandall Stewart 						 */
4293f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4294f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4295f8829a4aSRandall Stewart 					}
4296c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4297b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4298c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4299a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4300a5d547adSRandall Stewart 							    tp1->book_size,
4301c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4302a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
430380fefe0aSRandall Stewart 						}
4304c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4305c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4306299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4307299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4308299108c5SRandall Stewart 							    tp1);
4309299108c5SRandall Stewart 						}
4310f8829a4aSRandall Stewart 					}
4311f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4312f8829a4aSRandall Stewart 
4313f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4314f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4315f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4316f8829a4aSRandall Stewart 
4317f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4318f8829a4aSRandall Stewart 						/*
4319f8829a4aSRandall Stewart 						 * True non-retransmited
4320f8829a4aSRandall Stewart 						 * chunk
4321f8829a4aSRandall Stewart 						 */
4322f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4323f8829a4aSRandall Stewart 						    tp1->send_size;
4324f8829a4aSRandall Stewart 
4325f8829a4aSRandall Stewart 						/* update RTO too? */
4326f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4327f79aab18SRandall Stewart 							if (rto_ok) {
4328f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4329f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4330f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
433118e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4332899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4333f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4334f79aab18SRandall Stewart 								rto_ok = 0;
4335f79aab18SRandall Stewart 							}
4336f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4337f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4338f79aab18SRandall Stewart 							}
4339f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4340f8829a4aSRandall Stewart 						}
4341f8829a4aSRandall Stewart 					}
4342f8829a4aSRandall Stewart 					/*
4343f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4344f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4345f8829a4aSRandall Stewart 					 * acked for the first time, set the
4346f8829a4aSRandall Stewart 					 * following variables for the
4347f8829a4aSRandall Stewart 					 * corresp destination.
4348f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4349f8829a4aSRandall Stewart 					 * cwnd update.
4350f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4351f8829a4aSRandall Stewart 					 * trigger search for the next
4352f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4353f8829a4aSRandall Stewart 					 */
4354f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4355f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4356f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4357f8829a4aSRandall Stewart 
4358f8829a4aSRandall Stewart 
4359b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4360f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4361f8829a4aSRandall Stewart 						    cum_ack,
4362f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4363f8829a4aSRandall Stewart 						    0,
4364f8829a4aSRandall Stewart 						    0,
4365f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
436680fefe0aSRandall Stewart 					}
4367b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4368f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
436980fefe0aSRandall Stewart 					}
4370f8829a4aSRandall Stewart 				}
4371f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4372f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4373f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4374f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4375f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4376f8829a4aSRandall Stewart #endif
4377f8829a4aSRandall Stewart 				}
437842551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
437942551e99SRandall Stewart 					/* deflate the cwnd */
438042551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
438142551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
438242551e99SRandall Stewart 				}
4383325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4384f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_ACKED;
4385f8829a4aSRandall Stewart 				}
4386325c8c46SMichael Tuexen 			}
4387f8829a4aSRandall Stewart 		} else {
4388f8829a4aSRandall Stewart 			break;
4389f8829a4aSRandall Stewart 		}
4390f8829a4aSRandall Stewart 	}
4391f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4392f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4393f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4394f8829a4aSRandall Stewart 
4395cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4396f8829a4aSRandall Stewart 
4397f8829a4aSRandall Stewart 		/*
4398f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4399f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4400f8829a4aSRandall Stewart 		 * for all dests.
4401f8829a4aSRandall Stewart 		 */
4402f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4403f8829a4aSRandall Stewart 			net->saw_newack = 0;
4404f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4405f8829a4aSRandall Stewart 		}
4406f8829a4aSRandall Stewart 
4407f8829a4aSRandall Stewart 		/*
4408f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4409f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4410f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4411f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4412f8829a4aSRandall Stewart 		 */
4413cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4414cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
44157215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4416cd554309SMichael Tuexen 			wake_him++;
4417cd554309SMichael Tuexen 		}
4418b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4419f8829a4aSRandall Stewart 			/*
4420f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4421f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4422f8829a4aSRandall Stewart 			 */
442320b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4424f8829a4aSRandall Stewart 				/*
4425f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4426f8829a4aSRandall Stewart 				 * attack. We must abort.
4427f8829a4aSRandall Stewart 				 */
4428cd3fd531SMichael Tuexen 				SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4429cd3fd531SMichael Tuexen 				    biggest_tsn_acked, send_s);
4430f8829a4aSRandall Stewart 				goto hopeless_peer;
4431f8829a4aSRandall Stewart 			}
4432f8829a4aSRandall Stewart 		}
4433f8829a4aSRandall Stewart 	}
4434f8829a4aSRandall Stewart 	/*******************************************/
4435f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4436f8829a4aSRandall Stewart 	/*******************************************/
44377c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4438f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4439f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4440f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4441a5d547adSRandall Stewart 				    stcb, net,
4442a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4443f8829a4aSRandall Stewart 
4444f8829a4aSRandall Stewart 		}
4445f8829a4aSRandall Stewart 	} else {
4446f8829a4aSRandall Stewart 		if (accum_moved) {
4447f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4448f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4449a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4450f8829a4aSRandall Stewart 			}
4451f8829a4aSRandall Stewart 		}
4452f8829a4aSRandall Stewart 	}
4453f8829a4aSRandall Stewart 	/********************************************/
4454d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4455f8829a4aSRandall Stewart 	/********************************************/
4456f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4457f8829a4aSRandall Stewart 
44587c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
445920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4460f8829a4aSRandall Stewart 			break;
4461f8829a4aSRandall Stewart 		}
4462325c8c46SMichael Tuexen 		if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4463a7ad6026SMichael Tuexen 			if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
4464a7ad6026SMichael Tuexen 				asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
4465a7ad6026SMichael Tuexen #ifdef INVARIANTS
4466a7ad6026SMichael Tuexen 			} else {
4467a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
4468a7ad6026SMichael Tuexen #endif
4469a7ad6026SMichael Tuexen 			}
4470f8829a4aSRandall Stewart 		}
4471f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
44720ddb4299SMichael Tuexen 		if (PR_SCTP_ENABLED(tp1->flags)) {
4473f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4474f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4475f8829a4aSRandall Stewart 		}
44767c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4477f8829a4aSRandall Stewart 		if (tp1->data) {
447804ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4479f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4480f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
44817c99d56fSMichael Tuexen 			tp1->data = NULL;
4482810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4483f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4484f8829a4aSRandall Stewart 			}
4485f8829a4aSRandall Stewart 		}
4486b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4487f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4488f8829a4aSRandall Stewart 			    cum_ack,
4489f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4490f8829a4aSRandall Stewart 			    0,
4491f8829a4aSRandall Stewart 			    0,
4492f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
449380fefe0aSRandall Stewart 		}
4494689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4495f8829a4aSRandall Stewart 		wake_him++;
44967c99d56fSMichael Tuexen 	}
44977c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
44987c99d56fSMichael Tuexen #ifdef INVARIANTS
44997c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
45007c99d56fSMichael Tuexen #else
45017c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
45027c99d56fSMichael Tuexen 		    asoc->total_flight);
45037c99d56fSMichael Tuexen #endif
45047c99d56fSMichael Tuexen 		asoc->total_flight = 0;
45057c99d56fSMichael Tuexen 	}
450604ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4507f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4508ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4509ceaad40aSRandall Stewart 		struct socket *so;
4510ceaad40aSRandall Stewart 
4511ceaad40aSRandall Stewart #endif
4512f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4513b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
45147215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
451580fefe0aSRandall Stewart 		}
4516ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4517ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4518ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4519ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4520ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4521ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4522ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4523ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4524ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4525ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4526ceaad40aSRandall Stewart 			return;
4527ceaad40aSRandall Stewart 		}
4528ceaad40aSRandall Stewart #endif
4529f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4530ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4531ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4532ceaad40aSRandall Stewart #endif
4533f8829a4aSRandall Stewart 	} else {
4534b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
45357215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
453680fefe0aSRandall Stewart 		}
4537f8829a4aSRandall Stewart 	}
4538f8829a4aSRandall Stewart 
453942551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
454020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4541f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4542f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4543f8829a4aSRandall Stewart 		}
4544f8829a4aSRandall Stewart 	}
4545f8829a4aSRandall Stewart 	/*
4546f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4547f8829a4aSRandall Stewart 	 *
4548f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4549f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4550f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4551f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4552f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4553f8829a4aSRandall Stewart 	 */
4554f8829a4aSRandall Stewart 
4555d9c5cfeaSMichael Tuexen 	if (num_seg) {
4556c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4557d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4558d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4559f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4560f8829a4aSRandall Stewart 
4561f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4562f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4563b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4564f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4565b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4566c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4567c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4568c105859eSRandall Stewart 					    tp1->book_size,
4569c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4570c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
457180fefe0aSRandall Stewart 				}
4572c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4573c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4574a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
457542551e99SRandall Stewart 				/*
457642551e99SRandall Stewart 				 * To ensure that this increase in
45774a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
45784a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
45794a9ef3f8SMichael Tuexen 				 * cwnd artificially.
458042551e99SRandall Stewart 				 */
458142551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4582f8829a4aSRandall Stewart 				cnt_revoked++;
4583f8829a4aSRandall Stewart 			}
4584f8829a4aSRandall Stewart 		}
4585f8829a4aSRandall Stewart 		if (cnt_revoked) {
4586f8829a4aSRandall Stewart 			reneged_all = 1;
4587f8829a4aSRandall Stewart 		}
4588f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4589f8829a4aSRandall Stewart 	}
4590d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4591d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4592f8829a4aSRandall Stewart 	else
4593d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4594f8829a4aSRandall Stewart 
4595b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4596ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4597ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4598ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4599ca85e948SMichael Tuexen 				/*
4600ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4601ca85e948SMichael Tuexen 				 * count, this is optional.
4602ca85e948SMichael Tuexen 				 */
4603ca85e948SMichael Tuexen 				net->error_count = 0;
4604ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4605ca85e948SMichael Tuexen 					/* addr came good */
4606ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4607ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
46084b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4609ca85e948SMichael Tuexen 				}
4610ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4611ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4612ca85e948SMichael Tuexen 						/*
4613ca85e948SMichael Tuexen 						 * release the alternate,
4614ca85e948SMichael Tuexen 						 * primary is good
4615ca85e948SMichael Tuexen 						 */
4616ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4617ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4618ca85e948SMichael Tuexen 					}
4619ca85e948SMichael Tuexen 				}
4620ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4621ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4622ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4623ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4624ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4625ca85e948SMichael Tuexen 					/* Done with this net */
4626ca85e948SMichael Tuexen 					net->net_ack = 0;
4627ca85e948SMichael Tuexen 				}
4628ca85e948SMichael Tuexen 				/* restore any doubled timers */
4629ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4630ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4631ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4632ca85e948SMichael Tuexen 				}
4633ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4634ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4635ca85e948SMichael Tuexen 				}
4636ca85e948SMichael Tuexen 			}
4637ca85e948SMichael Tuexen 		}
4638b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4639ca85e948SMichael Tuexen 	}
4640f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4641f8829a4aSRandall Stewart 		/* nothing left in-flight */
4642f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4643f8829a4aSRandall Stewart 			/* stop all timers */
4644f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4645a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4646f8829a4aSRandall Stewart 			net->flight_size = 0;
4647f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4648f8829a4aSRandall Stewart 		}
4649f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4650f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4651f8829a4aSRandall Stewart 	}
4652f8829a4aSRandall Stewart 	/**********************************/
4653f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4654f8829a4aSRandall Stewart 	/**********************************/
4655f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4656f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4657b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4658f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4659f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
466080fefe0aSRandall Stewart 		}
4661f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4662f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4663f8829a4aSRandall Stewart 			/* SWS sender side engages */
4664f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4665f8829a4aSRandall Stewart 		}
4666f8829a4aSRandall Stewart 		/* clean up */
4667f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4668f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4669f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4670f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4671f8829a4aSRandall Stewart 		    ) {
4672f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4673f8829a4aSRandall Stewart 
4674f8829a4aSRandall Stewart 			/*
4675f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4676f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4677f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4678f8829a4aSRandall Stewart 			 */
4679f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4680f8829a4aSRandall Stewart 			    sctp_streamhead);
46812afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4682f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
46832afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4684f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
46852afb3e84SRandall Stewart 				} else {
46862afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
46872afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46882afb3e84SRandall Stewart 				}
4689f8829a4aSRandall Stewart 			}
4690f8829a4aSRandall Stewart 		}
4691f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4692f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4693f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4694f8829a4aSRandall Stewart 				/* Need to abort here */
4695ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4696f8829a4aSRandall Stewart 
4697f8829a4aSRandall Stewart 		abort_out_now:
4698f8829a4aSRandall Stewart 				*abort_now = 1;
4699f8829a4aSRandall Stewart 				/* XXX */
4700ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4701a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4702ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4703f8829a4aSRandall Stewart 				return;
4704f8829a4aSRandall Stewart 			} else {
4705ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4706ca85e948SMichael Tuexen 
4707f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4708f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4709f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4710f42a358aSRandall Stewart 				}
4711c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4712b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4713f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4714c39cfa1fSMichael Tuexen 				if (asoc->alternate) {
4715c39cfa1fSMichael Tuexen 					netp = asoc->alternate;
4716c39cfa1fSMichael Tuexen 				} else {
4717c39cfa1fSMichael Tuexen 					netp = asoc->primary_destination;
4718c39cfa1fSMichael Tuexen 				}
4719ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4720f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4721ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4722f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4723ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4724f8829a4aSRandall Stewart 			}
4725f8829a4aSRandall Stewart 			return;
4726f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4727f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4728ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4729ca85e948SMichael Tuexen 
4730f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4731f8829a4aSRandall Stewart 				goto abort_out_now;
4732f8829a4aSRandall Stewart 			}
4733f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4734c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4735b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
473612af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4737c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4738c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4739c39cfa1fSMichael Tuexen 			} else {
4740c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4741c39cfa1fSMichael Tuexen 			}
4742c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4743f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4744ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4745f8829a4aSRandall Stewart 			return;
4746f8829a4aSRandall Stewart 		}
4747f8829a4aSRandall Stewart 	}
4748f8829a4aSRandall Stewart 	/*
4749f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4750f8829a4aSRandall Stewart 	 * HEADS UP.
4751f8829a4aSRandall Stewart 	 */
4752f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4753f8829a4aSRandall Stewart 		net->net_ack = 0;
4754f8829a4aSRandall Stewart 	}
4755f8829a4aSRandall Stewart 
4756f8829a4aSRandall Stewart 	/*
4757f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4758f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4759f8829a4aSRandall Stewart 	 * automatically ensure that.
4760f8829a4aSRandall Stewart 	 */
47617c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
476220083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
476320083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
4764f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
4765f8829a4aSRandall Stewart 	}
4766cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4767f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4768f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4769f8829a4aSRandall Stewart 	}
4770b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4771b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4772f8829a4aSRandall Stewart 
4773f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
4774f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
4775f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
4776f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
4777f8829a4aSRandall Stewart 	}
4778f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
477920b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4780f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
4781f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
4782f8829a4aSRandall Stewart 	}
478342551e99SRandall Stewart 	/*
478442551e99SRandall Stewart 	 * CMT Fast recovery
478542551e99SRandall Stewart 	 */
4786f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4787f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
4788f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
4789f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
4790f8829a4aSRandall Stewart 		}
4791f8829a4aSRandall Stewart 	}
4792f8829a4aSRandall Stewart 
4793f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
4794b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4795f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
479644fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
479780fefe0aSRandall Stewart 	}
4798f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
479944fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4800f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4801f8829a4aSRandall Stewart 		/* SWS sender side engages */
4802f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4803f8829a4aSRandall Stewart 	}
48045e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
48055e54f665SRandall Stewart 		win_probe_recovery = 1;
48065e54f665SRandall Stewart 	}
4807f8829a4aSRandall Stewart 	/*
4808f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
4809f8829a4aSRandall Stewart 	 * outstanding data.
4810f8829a4aSRandall Stewart 	 */
4811bff64a4dSRandall Stewart 	done_once = 0;
4812a5d547adSRandall Stewart again:
4813a5d547adSRandall Stewart 	j = 0;
4814f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
48155e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4816c105859eSRandall Stewart 			win_probe_recovered = 1;
48175e54f665SRandall Stewart 			/*-
48185e54f665SRandall Stewart 			 * Find first chunk that was used with
48195e54f665SRandall Stewart 			 * window probe and clear the event. Put
48205e54f665SRandall Stewart 			 * it back into the send queue as if has
48215e54f665SRandall Stewart 			 * not been sent.
48225e54f665SRandall Stewart 			 */
48235e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
48245e54f665SRandall Stewart 				if (tp1->window_probe) {
48257215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
48265e54f665SRandall Stewart 					break;
48275e54f665SRandall Stewart 				}
48285e54f665SRandall Stewart 			}
48295e54f665SRandall Stewart 		}
4830f8829a4aSRandall Stewart 		if (net->flight_size) {
4831a5d547adSRandall Stewart 			j++;
4832cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4833f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4834f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
4835cd554309SMichael Tuexen 			}
48365171328bSRandall Stewart 			if (net->window_probe) {
4837cd554309SMichael Tuexen 				net->window_probe = 0;
48385171328bSRandall Stewart 			}
4839c105859eSRandall Stewart 		} else {
48405171328bSRandall Stewart 			if (net->window_probe) {
48415171328bSRandall Stewart 				/*
48425171328bSRandall Stewart 				 * In window probes we must assure a timer
48435171328bSRandall Stewart 				 * is still running there
48445171328bSRandall Stewart 				 */
48455171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
48465171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
48475171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
48485171328bSRandall Stewart 
48495171328bSRandall Stewart 				}
48505171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4851c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4852c105859eSRandall Stewart 				    stcb, net,
4853c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4854c105859eSRandall Stewart 			}
4855f8829a4aSRandall Stewart 		}
4856f8829a4aSRandall Stewart 	}
4857bff64a4dSRandall Stewart 	if ((j == 0) &&
4858bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4859bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4860c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4861bff64a4dSRandall Stewart 	    (done_once == 0)) {
48620c0982b8SRandall Stewart 		/*
48630c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
48640c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
48650c0982b8SRandall Stewart 		 */
48660c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4867a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4868a5d547adSRandall Stewart 				net->flight_size = 0;
4869a5d547adSRandall Stewart 			}
4870a5d547adSRandall Stewart 			asoc->total_flight = 0;
4871a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4872a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4873a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4874a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4875c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4876c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4877a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4878791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4879a5d547adSRandall Stewart 				}
4880a5d547adSRandall Stewart 			}
48810c0982b8SRandall Stewart 		}
4882bff64a4dSRandall Stewart 		done_once = 1;
4883a5d547adSRandall Stewart 		goto again;
4884a5d547adSRandall Stewart 	}
4885cd554309SMichael Tuexen 	/*********************************************/
4886cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
4887cd554309SMichael Tuexen 	/* (section 4.2)                             */
4888cd554309SMichael Tuexen 	/*********************************************/
4889cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
489020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
4891dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
4892dfb11ef8SRandall Stewart 	}
4893830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
4894830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4895830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4896830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4897830d754dSRandall Stewart 
4898830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4899830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4900830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
490120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
4902830d754dSRandall Stewart 			/*
4903493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4904830d754dSRandall Stewart 			 */
49050c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
49060c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
49070c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
49080c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
49090c0982b8SRandall Stewart 			}
491020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4911830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
49120c0982b8SRandall Stewart 			} else if (lchk) {
49130c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
491444fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
49150c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
49160c0982b8SRandall Stewart 				}
4917830d754dSRandall Stewart 			}
4918830d754dSRandall Stewart 		}
4919830d754dSRandall Stewart 		if (lchk) {
4920830d754dSRandall Stewart 			/* Assure a timer is up */
4921830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4922830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4923830d754dSRandall Stewart 		}
4924830d754dSRandall Stewart 	}
4925b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4926f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4927f8829a4aSRandall Stewart 		    a_rwnd,
4928f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4929f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4930f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
493180fefe0aSRandall Stewart 	}
4932f8829a4aSRandall Stewart }
4933f8829a4aSRandall Stewart 
4934f8829a4aSRandall Stewart void
49357215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
4936f8829a4aSRandall Stewart {
4937f8829a4aSRandall Stewart 	/* Copy cum-ack */
4938f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
4939f8829a4aSRandall Stewart 
4940f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
4941f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
4942f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
4943f8829a4aSRandall Stewart 
4944f8829a4aSRandall Stewart 	/* Now call the express sack handling */
4945899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
4946f8829a4aSRandall Stewart }
4947f8829a4aSRandall Stewart 
4948f8829a4aSRandall Stewart static void
4949f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
4950f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
4951f8829a4aSRandall Stewart {
4952f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
4953f8829a4aSRandall Stewart 	struct sctp_association *asoc;
495483128708SRandall Stewart 	uint16_t tt;
4955f8829a4aSRandall Stewart 
4956f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4957f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
4958f8829a4aSRandall Stewart 	/*
4959f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
4960f8829a4aSRandall Stewart 	 * came in
4961f8829a4aSRandall Stewart 	 */
49624a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
496320b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
4964f8829a4aSRandall Stewart 			/* this is deliverable now */
4965f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4966f8829a4aSRandall Stewart 			/* subtract pending on streams */
4967f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
4968f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
4969f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
4970f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
4971b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4972f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
4973f8829a4aSRandall Stewart 				    ctl,
4974cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
4975f8829a4aSRandall Stewart 			}
4976f8829a4aSRandall Stewart 		} else {
4977f8829a4aSRandall Stewart 			/* no more delivery now. */
4978f8829a4aSRandall Stewart 			break;
4979f8829a4aSRandall Stewart 		}
4980f8829a4aSRandall Stewart 	}
4981f8829a4aSRandall Stewart 	/*
4982f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
4983f8829a4aSRandall Stewart 	 * now ready.
4984f8829a4aSRandall Stewart 	 */
4985f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
49864a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
4987f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
4988f8829a4aSRandall Stewart 			/* this is deliverable now */
4989f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4990f8829a4aSRandall Stewart 			/* subtract pending on streams */
4991f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
4992f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
4993f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
4994f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
4995f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
4996b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4997f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
4998f8829a4aSRandall Stewart 				    ctl,
4999cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
500077acdc25SRandall Stewart 
5001f8829a4aSRandall Stewart 			}
5002f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5003f8829a4aSRandall Stewart 		} else {
5004f8829a4aSRandall Stewart 			break;
5005f8829a4aSRandall Stewart 		}
5006f8829a4aSRandall Stewart 	}
5007f8829a4aSRandall Stewart }
5008f8829a4aSRandall Stewart 
50098933fa13SRandall Stewart static void
50108933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
50118933fa13SRandall Stewart     struct sctp_association *asoc,
50128933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
50138933fa13SRandall Stewart {
50144a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
50158933fa13SRandall Stewart 
50168933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
50178933fa13SRandall Stewart 	/*
50184a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
50194a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
50204a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
50214a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
50224a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
50234a9ef3f8SMichael Tuexen 	 * queue.
50248933fa13SRandall Stewart 	 */
50254a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
50268e71b694SMichael Tuexen 		/*
50274a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
50284a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
50294a9ef3f8SMichael Tuexen 		 * number has no meaning.
50308e71b694SMichael Tuexen 		 */
50318e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
50328e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
50338933fa13SRandall Stewart 			continue;
50348933fa13SRandall Stewart 		}
50358933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
50368933fa13SRandall Stewart 			/* It needs to be tossed */
50378933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
503820b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
50394a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
50404a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
50414a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
50424a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
50438933fa13SRandall Stewart 			}
50448933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
50458933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
50468933fa13SRandall Stewart 
50478933fa13SRandall Stewart 			/* Clear up any stream problem */
504820b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
504920b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
50508933fa13SRandall Stewart 				/*
50518933fa13SRandall Stewart 				 * We must dump forward this streams
50524a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
50534a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
50544a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
50554a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
50564a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
50574a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
50584a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
50594a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
50604a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
50614a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
50624a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
50638933fa13SRandall Stewart 				 */
50644a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
50658933fa13SRandall Stewart 			}
50668933fa13SRandall Stewart 			if (chk->data) {
50678933fa13SRandall Stewart 				sctp_m_freem(chk->data);
50688933fa13SRandall Stewart 				chk->data = NULL;
50698933fa13SRandall Stewart 			}
5070689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
507120b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
50728933fa13SRandall Stewart 			/*
50734a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
50744a9ef3f8SMichael Tuexen 			 * are done
50758933fa13SRandall Stewart 			 */
50768933fa13SRandall Stewart 			break;
50778933fa13SRandall Stewart 		}
50788933fa13SRandall Stewart 	}
50798933fa13SRandall Stewart }
50808933fa13SRandall Stewart 
50818933fa13SRandall Stewart 
5082f8829a4aSRandall Stewart void
5083f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5084b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5085b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5086f8829a4aSRandall Stewart {
5087f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5088f8829a4aSRandall Stewart 	/*
5089f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5090f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5091f8829a4aSRandall Stewart 	 *
5092f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5093f8829a4aSRandall Stewart 	 *
5094f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5095f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5096f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5097f8829a4aSRandall Stewart 	 * report where we are.
5098f8829a4aSRandall Stewart 	 */
5099f8829a4aSRandall Stewart 	struct sctp_association *asoc;
51007898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
51017215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
51028933fa13SRandall Stewart 	uint32_t str_seq;
5103f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
51044a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
51058933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5106f8829a4aSRandall Stewart 
5107f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5108f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5109ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5110ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5111f8829a4aSRandall Stewart 		return;
5112f8829a4aSRandall Stewart 	}
5113f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5114f8829a4aSRandall Stewart 	/*************************************************************/
5115f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5116f8829a4aSRandall Stewart 	/*************************************************************/
5117f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5118f8829a4aSRandall Stewart 
511920b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5120f8829a4aSRandall Stewart 		/* Already got there ... */
5121f8829a4aSRandall Stewart 		return;
5122f8829a4aSRandall Stewart 	}
5123f8829a4aSRandall Stewart 	/*
5124f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5125f8829a4aSRandall Stewart 	 * gap
5126f8829a4aSRandall Stewart 	 */
5127b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
512877acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
51292afb3e84SRandall Stewart 	if (gap >= m_size) {
5130f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5131ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
5132ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
513317205eccSRandall Stewart 
5134f8829a4aSRandall Stewart 			/*
5135f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
513617205eccSRandall Stewart 			 * give out). This must be an attacker.
5137f8829a4aSRandall Stewart 			 */
513817205eccSRandall Stewart 			*abort_flag = 1;
5139ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg),
5140ff1ffd74SMichael Tuexen 			    "New cum ack %8.8x too high, highest TSN %8.8x",
5141ff1ffd74SMichael Tuexen 			    new_cum_tsn, asoc->highest_tsn_inside_map);
5142ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
514317205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5144ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5145f8829a4aSRandall Stewart 			return;
5146f8829a4aSRandall Stewart 		}
5147207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
514877acdc25SRandall Stewart 
51492afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
51502afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
515177acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
515277acdc25SRandall Stewart 
5153b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5154830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
515577acdc25SRandall Stewart 
5156b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5157f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
515880fefe0aSRandall Stewart 		}
51592afb3e84SRandall Stewart 	} else {
51602afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
51612afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
51627898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
51637898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
51648933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
516520b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
51667898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5167b5c16493SMichael Tuexen 				}
5168b5c16493SMichael Tuexen 			}
5169b5c16493SMichael Tuexen 		}
5170f8829a4aSRandall Stewart 	}
5171f8829a4aSRandall Stewart 	/*************************************************************/
5172f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5173f8829a4aSRandall Stewart 	/*************************************************************/
5174f8829a4aSRandall Stewart 	/*
5175f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5176f8829a4aSRandall Stewart 	 * forward
5177f8829a4aSRandall Stewart 	 */
5178f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5179f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5180f8829a4aSRandall Stewart 	}
5181f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5182f8829a4aSRandall Stewart 	/*
51834a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
51844a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
51854a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
51864a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
51874a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
51884a9ef3f8SMichael Tuexen 	 * queue.
5189f8829a4aSRandall Stewart 	 */
51904a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
519120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5192f8829a4aSRandall Stewart 			/* It needs to be tossed */
5193f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
519420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
51954a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
51964a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
51974a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
51984a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5199f8829a4aSRandall Stewart 			}
5200f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5201f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5202f8829a4aSRandall Stewart 
5203f8829a4aSRandall Stewart 			/* Clear up any stream problem */
520420b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
520520b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5206f8829a4aSRandall Stewart 				/*
5207f8829a4aSRandall Stewart 				 * We must dump forward this streams
52084a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
52094a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
52104a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
52114a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
52124a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
52134a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
52144a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
52154a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
52164a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
52174a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
52184a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5219f8829a4aSRandall Stewart 				 */
52204a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5221f8829a4aSRandall Stewart 			}
5222f8829a4aSRandall Stewart 			if (chk->data) {
5223f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5224f8829a4aSRandall Stewart 				chk->data = NULL;
5225f8829a4aSRandall Stewart 			}
5226689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5227f8829a4aSRandall Stewart 		} else {
5228f8829a4aSRandall Stewart 			/*
52294a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
52304a9ef3f8SMichael Tuexen 			 * mark.
5231f8829a4aSRandall Stewart 			 */
5232f8829a4aSRandall Stewart 			break;
5233f8829a4aSRandall Stewart 		}
5234f8829a4aSRandall Stewart 	}
52358933fa13SRandall Stewart 	/*******************************************************/
52368933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
52378933fa13SRandall Stewart 	/* delivery issues as needed.                       */
52388933fa13SRandall Stewart 	/*******************************************************/
5239f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5240671d309cSRandall Stewart 	if (m && fwd_sz) {
5241f8829a4aSRandall Stewart 		/* New method. */
5242d61a0ae0SRandall Stewart 		unsigned int num_str;
5243671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5244671d309cSRandall Stewart 
5245671d309cSRandall Stewart 		offset += sizeof(*fwd);
5246f8829a4aSRandall Stewart 
52478933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5248f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5249f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5250f8829a4aSRandall Stewart 			uint16_t st;
5251f8829a4aSRandall Stewart 
5252671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5253671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5254671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5255671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
52562afb3e84SRandall Stewart 			if (stseq == NULL) {
5257671d309cSRandall Stewart 				break;
52582afb3e84SRandall Stewart 			}
5259f8829a4aSRandall Stewart 			/* Convert */
5260851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5261851b7298SRandall Stewart 			stseq->stream = st;
5262851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5263851b7298SRandall Stewart 			stseq->sequence = st;
52648933fa13SRandall Stewart 
5265f8829a4aSRandall Stewart 			/* now process */
52668933fa13SRandall Stewart 
52678933fa13SRandall Stewart 			/*
52688933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
52698933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
52708933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
52718933fa13SRandall Stewart 			 */
5272851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
52732afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
52742afb3e84SRandall Stewart 				break;
5275f8829a4aSRandall Stewart 			}
52768933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
52778933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
52788933fa13SRandall Stewart 				/*
52798933fa13SRandall Stewart 				 * If this is the one we were partially
52808933fa13SRandall Stewart 				 * delivering now then we no longer are.
52818933fa13SRandall Stewart 				 * Note this will change with the reassembly
52828933fa13SRandall Stewart 				 * re-write.
52838933fa13SRandall Stewart 				 */
52848933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
52858933fa13SRandall Stewart 			}
52868933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
52878933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
52888933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
52898933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
52908933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
52918933fa13SRandall Stewart 					ctl->end_added = 1;
52928933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
52938933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
52948933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5295810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5296810ec536SMichael Tuexen 					    stcb,
52978933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5298810ec536SMichael Tuexen 					    (void *)&str_seq,
5299810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
53008933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
53018933fa13SRandall Stewart 					break;
53028933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
530320b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
53048933fa13SRandall Stewart 					/* We are past our victim SSN */
53058933fa13SRandall Stewart 					break;
53068933fa13SRandall Stewart 				}
53078933fa13SRandall Stewart 			}
5308851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
530920b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5310f8829a4aSRandall Stewart 				/* Update the sequence number */
531120b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5312f8829a4aSRandall Stewart 			}
5313f8829a4aSRandall Stewart 			/* now kick the stream the new way */
531404ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5315f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5316f8829a4aSRandall Stewart 		}
53178933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5318f8829a4aSRandall Stewart 	}
53197898f408SRandall Stewart 	/*
53207898f408SRandall Stewart 	 * Now slide thing forward.
53217898f408SRandall Stewart 	 */
53227898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
53237898f408SRandall Stewart 
532424f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5325139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
532604ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5327139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5328139bc87fSRandall Stewart 	}
5329f8829a4aSRandall Stewart }
5330