xref: /freebsd/sys/netinet/sctp_indata.c (revision 32451da4160e1d9e4f25dfd6a56e729c55ca89e3)
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 *);
253e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
254f8829a4aSRandall Stewart 		cmh->cmsg_level = IPPROTO_SCTP;
255e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
256e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_RCVINFO;
257e2e7c62eSMichael Tuexen 		rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
258e2e7c62eSMichael Tuexen 		rcvinfo->rcv_sid = sinfo->sinfo_stream;
259e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
260e2e7c62eSMichael Tuexen 		rcvinfo->rcv_flags = sinfo->sinfo_flags;
261e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
262e2e7c62eSMichael Tuexen 		rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
263e2e7c62eSMichael Tuexen 		rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
264e2e7c62eSMichael Tuexen 		rcvinfo->rcv_context = sinfo->sinfo_context;
265e2e7c62eSMichael Tuexen 		rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
266e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
267e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
268f8829a4aSRandall Stewart 	}
269e2e7c62eSMichael Tuexen 	if (provide_nxt) {
270e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
271e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
272e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_NXTINFO;
273e2e7c62eSMichael Tuexen 		nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
274e2e7c62eSMichael Tuexen 		nxtinfo->nxt_sid = seinfo->sreinfo_next_stream;
275e2e7c62eSMichael Tuexen 		nxtinfo->nxt_flags = 0;
276e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
277e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_UNORDERED;
278e2e7c62eSMichael Tuexen 		}
279e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
280e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
281e2e7c62eSMichael Tuexen 		}
282e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
283e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_COMPLETE;
284e2e7c62eSMichael Tuexen 		}
285e2e7c62eSMichael Tuexen 		nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid;
286e2e7c62eSMichael Tuexen 		nxtinfo->nxt_length = seinfo->sreinfo_next_length;
287e2e7c62eSMichael Tuexen 		nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid;
288e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
289e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
290e2e7c62eSMichael Tuexen 	}
291e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
292e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
293e2e7c62eSMichael Tuexen 		outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
294e2e7c62eSMichael Tuexen 		if (use_extended) {
295e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
296e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_EXTRCV;
297e2e7c62eSMichael Tuexen 			memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
298e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
299e2e7c62eSMichael Tuexen 		} else {
300e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
301e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_SNDRCV;
302e2e7c62eSMichael Tuexen 			*outinfo = *sinfo;
303e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
304e2e7c62eSMichael Tuexen 		}
305e2e7c62eSMichael Tuexen 	}
306f8829a4aSRandall Stewart 	return (ret);
307f8829a4aSRandall Stewart }
308f8829a4aSRandall Stewart 
309139bc87fSRandall Stewart 
31077acdc25SRandall Stewart static void
31177acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
31277acdc25SRandall Stewart {
3139b2e0767SRandall Stewart 	uint32_t gap, i, cumackp1;
31477acdc25SRandall Stewart 	int fnd = 0;
31577acdc25SRandall Stewart 
31677acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
31777acdc25SRandall Stewart 		return;
31877acdc25SRandall Stewart 	}
3199b2e0767SRandall Stewart 	cumackp1 = asoc->cumulative_tsn + 1;
32020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumackp1, tsn)) {
3219b2e0767SRandall Stewart 		/*
3229b2e0767SRandall Stewart 		 * this tsn is behind the cum ack and thus we don't need to
3239b2e0767SRandall Stewart 		 * worry about it being moved from one to the other.
3249b2e0767SRandall Stewart 		 */
3259b2e0767SRandall Stewart 		return;
3269b2e0767SRandall Stewart 	}
32777acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
32877acdc25SRandall Stewart 	if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
329cd3fd531SMichael Tuexen 		SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
33077acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
331b5c16493SMichael Tuexen #ifdef INVARIANTS
33277acdc25SRandall Stewart 		panic("Things are really messed up now!!");
33377acdc25SRandall Stewart #endif
334b5c16493SMichael Tuexen 	}
33577acdc25SRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
33677acdc25SRandall Stewart 	SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
33720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
33877acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
33977acdc25SRandall Stewart 	}
34077acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
34177acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
34220b07a4dSMichael Tuexen 		for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
34377acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
34477acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
34577acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
34677acdc25SRandall Stewart 				fnd = 1;
34777acdc25SRandall Stewart 				break;
34877acdc25SRandall Stewart 			}
34977acdc25SRandall Stewart 		}
35077acdc25SRandall Stewart 		if (!fnd) {
35177acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
35277acdc25SRandall Stewart 		}
35377acdc25SRandall Stewart 	}
35477acdc25SRandall Stewart }
35577acdc25SRandall Stewart 
35617205eccSRandall Stewart 
357f8829a4aSRandall Stewart /*
358f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
359f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
360f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
361f8829a4aSRandall Stewart  */
362f8829a4aSRandall Stewart static void
363f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
364f8829a4aSRandall Stewart {
3654a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
366f8829a4aSRandall Stewart 	uint16_t nxt_todel;
367f8829a4aSRandall Stewart 	uint16_t stream_no;
368f8829a4aSRandall Stewart 	int end = 0;
369f8829a4aSRandall Stewart 	int cntDel;
3704a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *control, *ctl, *nctl;
371f8829a4aSRandall Stewart 
372ad81507eSRandall Stewart 	if (stcb == NULL)
373ad81507eSRandall Stewart 		return;
374ad81507eSRandall Stewart 
375f42a358aSRandall Stewart 	cntDel = stream_no = 0;
376ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
377851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
378ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
379851b7298SRandall Stewart 		/* socket above is long gone or going.. */
380851b7298SRandall Stewart abandon:
381f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
3824a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
383f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
384f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
385f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
386f8829a4aSRandall Stewart 			/*
387f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
388f8829a4aSRandall Stewart 			 * buffer
389f8829a4aSRandall Stewart 			 */
390f8829a4aSRandall Stewart 			if (chk->data) {
391f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
392f8829a4aSRandall Stewart 				chk->data = NULL;
393f8829a4aSRandall Stewart 			}
394f8829a4aSRandall Stewart 			/* Now free the address and data */
395689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3963c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
397f8829a4aSRandall Stewart 		}
398f8829a4aSRandall Stewart 		return;
399f8829a4aSRandall Stewart 	}
400f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4014a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
402f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
403f8829a4aSRandall Stewart 			/* Can't deliver more :< */
404f8829a4aSRandall Stewart 			return;
405f8829a4aSRandall Stewart 		}
406f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
407f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
408f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
409f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
410f8829a4aSRandall Stewart 			/*
411f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
412f8829a4aSRandall Stewart 			 * unordered
413f8829a4aSRandall Stewart 			 */
414f8829a4aSRandall Stewart 			return;
415f8829a4aSRandall Stewart 		}
416f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
417f8829a4aSRandall Stewart 
418f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
419f8829a4aSRandall Stewart 			if (control == NULL) {
420f8829a4aSRandall Stewart 				/* out of memory? */
421f8829a4aSRandall Stewart 				return;
422f8829a4aSRandall Stewart 			}
423f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
424f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
425f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
426f8829a4aSRandall Stewart 				end = 1;
427f8829a4aSRandall Stewart 			else
428f8829a4aSRandall Stewart 				end = 0;
429b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
430f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
431cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
432cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
433f8829a4aSRandall Stewart 			cntDel++;
434f8829a4aSRandall Stewart 		} else {
435f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
436f8829a4aSRandall Stewart 				end = 1;
437f8829a4aSRandall Stewart 			else
438f8829a4aSRandall Stewart 				end = 0;
439b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
440f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
441f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
442f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
443f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
444f8829a4aSRandall Stewart 				/*
445f8829a4aSRandall Stewart 				 * something is very wrong, either
446f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
447f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
448f8829a4aSRandall Stewart 				 * the mbuf chain.
449f8829a4aSRandall Stewart 				 */
450851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
451851b7298SRandall Stewart 					goto abandon;
452851b7298SRandall Stewart 				} else {
453df6e0cc3SRandall Stewart #ifdef INVARIANTS
454ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
455f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
456f8829a4aSRandall Stewart 					}
457f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
458f8829a4aSRandall Stewart 					panic("Bad chunking ??");
459df6e0cc3SRandall Stewart #else
460df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
461df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
462df6e0cc3SRandall Stewart 					}
463df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
464df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
465df6e0cc3SRandall Stewart 
466df6e0cc3SRandall Stewart #endif
467df6e0cc3SRandall Stewart 					goto abandon;
468f8829a4aSRandall Stewart 				}
469851b7298SRandall Stewart 			}
470f8829a4aSRandall Stewart 			cntDel++;
471f8829a4aSRandall Stewart 		}
472f8829a4aSRandall Stewart 		/* pull it we did it */
473f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
474f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
475f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
476f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
477f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
478f8829a4aSRandall Stewart 			}
479f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
480f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
481f8829a4aSRandall Stewart 			}
482f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
483f8829a4aSRandall Stewart 			/*
484f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
485f8829a4aSRandall Stewart 			 * yet another one.
486f8829a4aSRandall Stewart 			 */
487f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
488f8829a4aSRandall Stewart 		}
489f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
490f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
491f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
492f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
493f8829a4aSRandall Stewart 
494f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
495f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
496f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
497f8829a4aSRandall Stewart 		/* free up the chk */
498f8829a4aSRandall Stewart 		chk->data = NULL;
499689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
500f8829a4aSRandall Stewart 
501f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
502f8829a4aSRandall Stewart 			/*
503f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
504f8829a4aSRandall Stewart 			 * the stream
505f8829a4aSRandall Stewart 			 */
506f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
507f8829a4aSRandall Stewart 
508f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
509f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
5104a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
511f8829a4aSRandall Stewart 				/* Deliver more if we can. */
512f8829a4aSRandall Stewart 				if (nxt_todel == ctl->sinfo_ssn) {
513f8829a4aSRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, ctl, next);
514f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= ctl->length;
515f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
516f8829a4aSRandall Stewart 					strm->last_sequence_delivered++;
517b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
518f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
519f8829a4aSRandall Stewart 					    ctl,
520cfde3ff7SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
521cfde3ff7SRandall Stewart 					    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
522f8829a4aSRandall Stewart 				} else {
523f8829a4aSRandall Stewart 					break;
524f8829a4aSRandall Stewart 				}
525f8829a4aSRandall Stewart 				nxt_todel = strm->last_sequence_delivered + 1;
526f8829a4aSRandall Stewart 			}
527139bc87fSRandall Stewart 			break;
528f8829a4aSRandall Stewart 		}
5294a9ef3f8SMichael Tuexen 	}
530f8829a4aSRandall Stewart }
531f8829a4aSRandall Stewart 
532f8829a4aSRandall Stewart /*
533f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
534f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
535f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
536f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
537f8829a4aSRandall Stewart  */
538f8829a4aSRandall Stewart static void
539f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
540f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
541f8829a4aSRandall Stewart {
542f8829a4aSRandall Stewart 	/*
543f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
544f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
545f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
546f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
547f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
548f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
549f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
550f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
551f8829a4aSRandall Stewart 	 * TSN.
552f8829a4aSRandall Stewart 	 *
553f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
554f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
555f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
556f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
557f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
558f8829a4aSRandall Stewart 	 *
559f8829a4aSRandall Stewart 	 */
560f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
561f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
562f8829a4aSRandall Stewart 	int queue_needed;
563f8829a4aSRandall Stewart 	uint16_t nxt_todel;
564ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
565ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
566f8829a4aSRandall Stewart 
567f8829a4aSRandall Stewart 	queue_needed = 1;
568f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
569f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
570f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
571f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
572b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
573f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
57480fefe0aSRandall Stewart 	}
575ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
576ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
577f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
578ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
579ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
58020b07a4dSMichael Tuexen 	if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
581f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
582ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort association\n",
583ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
584c40e9cf2SRandall Stewart protocol_error:
585f8829a4aSRandall Stewart 		/*
586f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
587f8829a4aSRandall Stewart 		 * association destruction
588f8829a4aSRandall Stewart 		 */
589f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
590ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
591ff1ffd74SMichael Tuexen 		    strm->last_sequence_delivered, control->sinfo_tsn,
592ff1ffd74SMichael Tuexen 		    control->sinfo_stream, control->sinfo_ssn);
593ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
594a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
595ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
596f8829a4aSRandall Stewart 		*abort_flag = 1;
597f8829a4aSRandall Stewart 		return;
598f8829a4aSRandall Stewart 
599f8829a4aSRandall Stewart 	}
600f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
601f8829a4aSRandall Stewart 		/* can be delivered right away? */
602b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
603f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
60480fefe0aSRandall Stewart 		}
605830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
606f8829a4aSRandall Stewart 		queue_needed = 0;
607f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
608f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
609f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
61077acdc25SRandall Stewart 
611b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
612f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
613f8829a4aSRandall Stewart 		    control,
614cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
615cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
6164a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
617f8829a4aSRandall Stewart 			/* all delivered */
618f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
619f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
620f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
621f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
622f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
623f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
624f8829a4aSRandall Stewart 				/*
625f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
626f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
627f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
628f8829a4aSRandall Stewart 				 * can be delivered from the strq.
629f8829a4aSRandall Stewart 				 */
630b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
631f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
632f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
63380fefe0aSRandall Stewart 				}
634b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
635f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
636f8829a4aSRandall Stewart 				    control,
637cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
638cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
639cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
640f8829a4aSRandall Stewart 				continue;
641f8829a4aSRandall Stewart 			}
642f8829a4aSRandall Stewart 			break;
643f8829a4aSRandall Stewart 		}
644f8829a4aSRandall Stewart 	}
645f8829a4aSRandall Stewart 	if (queue_needed) {
646f8829a4aSRandall Stewart 		/*
647f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
648f8829a4aSRandall Stewart 		 * to put it on the queue.
649f8829a4aSRandall Stewart 		 */
65020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
651c40e9cf2SRandall Stewart 			goto protocol_error;
652c40e9cf2SRandall Stewart 		}
653f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
654f8829a4aSRandall Stewart 			/* Empty queue */
655b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
656f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
65780fefe0aSRandall Stewart 			}
658f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
659f8829a4aSRandall Stewart 		} else {
660f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
66120b07a4dSMichael Tuexen 				if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
662f8829a4aSRandall Stewart 					/*
663f8829a4aSRandall Stewart 					 * one in queue is bigger than the
664f8829a4aSRandall Stewart 					 * new one, insert before this one
665f8829a4aSRandall Stewart 					 */
666b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
667f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
668f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
66980fefe0aSRandall Stewart 					}
670f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
671f8829a4aSRandall Stewart 					break;
672f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
673f8829a4aSRandall Stewart 					/*
674f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
675f8829a4aSRandall Stewart 					 * seq number
676f8829a4aSRandall Stewart 					 */
677f8829a4aSRandall Stewart 					/*
678f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
679f8829a4aSRandall Stewart 					 * this new guy, should we abort
680f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
681f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
682f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
683f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
684f8829a4aSRandall Stewart 					 * away the chunk!
685f8829a4aSRandall Stewart 					 */
686f8829a4aSRandall Stewart 
687f8829a4aSRandall Stewart 					if (control->data)
688f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
689f8829a4aSRandall Stewart 					control->data = NULL;
690f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
691f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
69224f52bbdSMichael Tuexen 					if (control->whoFrom) {
693f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
6945bead436SRandall Stewart 						control->whoFrom = NULL;
69524f52bbdSMichael Tuexen 					}
696f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
697f8829a4aSRandall Stewart 					return;
698f8829a4aSRandall Stewart 				} else {
699f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
700f8829a4aSRandall Stewart 						/*
701f8829a4aSRandall Stewart 						 * We are at the end, insert
702f8829a4aSRandall Stewart 						 * it after this one
703f8829a4aSRandall Stewart 						 */
704b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
705f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
706f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
70780fefe0aSRandall Stewart 						}
708f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
709f8829a4aSRandall Stewart 						    at, control, next);
710f8829a4aSRandall Stewart 						break;
711f8829a4aSRandall Stewart 					}
712f8829a4aSRandall Stewart 				}
713f8829a4aSRandall Stewart 			}
714f8829a4aSRandall Stewart 		}
715f8829a4aSRandall Stewart 	}
716f8829a4aSRandall Stewart }
717f8829a4aSRandall Stewart 
718f8829a4aSRandall Stewart /*
719f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
720f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
721f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
722f8829a4aSRandall Stewart  */
723f8829a4aSRandall Stewart static int
724f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
725f8829a4aSRandall Stewart {
726f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
727f8829a4aSRandall Stewart 	uint32_t tsn;
728f8829a4aSRandall Stewart 
729f8829a4aSRandall Stewart 	*t_size = 0;
730f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
731f8829a4aSRandall Stewart 	if (chk == NULL) {
732f8829a4aSRandall Stewart 		/* nothing on the queue */
733f8829a4aSRandall Stewart 		return (0);
734f8829a4aSRandall Stewart 	}
735f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
736f8829a4aSRandall Stewart 		/* Not a first on the queue */
737f8829a4aSRandall Stewart 		return (0);
738f8829a4aSRandall Stewart 	}
739f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
7404a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
741f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
742f8829a4aSRandall Stewart 			return (0);
743f8829a4aSRandall Stewart 		}
744f8829a4aSRandall Stewart 		*t_size += chk->send_size;
745f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
746f8829a4aSRandall Stewart 			return (1);
747f8829a4aSRandall Stewart 		}
748f8829a4aSRandall Stewart 		tsn++;
749f8829a4aSRandall Stewart 	}
750f8829a4aSRandall Stewart 	return (0);
751f8829a4aSRandall Stewart }
752f8829a4aSRandall Stewart 
753f8829a4aSRandall Stewart static void
754f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
755f8829a4aSRandall Stewart {
756f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
757f8829a4aSRandall Stewart 	uint16_t nxt_todel;
758810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
759f8829a4aSRandall Stewart 
760139bc87fSRandall Stewart doit_again:
761f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
762f8829a4aSRandall Stewart 	if (chk == NULL) {
763f8829a4aSRandall Stewart 		/* Huh? */
764f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
765f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
766f8829a4aSRandall Stewart 		return;
767f8829a4aSRandall Stewart 	}
768f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
769f8829a4aSRandall Stewart 		nxt_todel =
770f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
771f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
772f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
773f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
774f8829a4aSRandall Stewart 			/*
775f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
776f8829a4aSRandall Stewart 			 * but should we?
777f8829a4aSRandall Stewart 			 */
778810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
779d4d23375SMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
780810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
781810ec536SMichael Tuexen 			} else {
782810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
783810ec536SMichael Tuexen 			}
784810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
785f8829a4aSRandall Stewart 				/*
786f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
787f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
788f8829a4aSRandall Stewart 				 * can't deliver. If we
789f8829a4aSRandall Stewart 				 */
790f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
791f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
792f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
793f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
794f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
795f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
796f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
797f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
798f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
799f8829a4aSRandall Stewart 			}
800f8829a4aSRandall Stewart 		}
801f8829a4aSRandall Stewart 	} else {
802139bc87fSRandall Stewart 		/*
803139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
804139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
805139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
806139bc87fSRandall Stewart 		 * got doit_again
807139bc87fSRandall Stewart 		 */
808f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
809139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
810139bc87fSRandall Stewart 			/*
811139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
812139bc87fSRandall Stewart 			 * waiting?
813139bc87fSRandall Stewart 			 */
814139bc87fSRandall Stewart 			goto doit_again;
815139bc87fSRandall Stewart 		}
816f8829a4aSRandall Stewart 	}
817f8829a4aSRandall Stewart }
818f8829a4aSRandall Stewart 
819f8829a4aSRandall Stewart /*
820f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
821f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
822f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
823f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
824f8829a4aSRandall Stewart  */
825f8829a4aSRandall Stewart static void
826f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
827f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
828f8829a4aSRandall Stewart {
829ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
830ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
831ff1ffd74SMichael Tuexen 
83260990c0cSMichael Tuexen 	uint32_t cum_ackp1, prev_tsn, post_tsn;
833f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
834f8829a4aSRandall Stewart 
835f8829a4aSRandall Stewart 	prev = next = NULL;
836f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
837f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
838f8829a4aSRandall Stewart 		/* This is the first one on the queue */
839f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
840f8829a4aSRandall Stewart 		/*
841f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
842f8829a4aSRandall Stewart 		 * fragment is here
843f8829a4aSRandall Stewart 		 */
844f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
845f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
846f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
847f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
848f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
849f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
850f8829a4aSRandall Stewart 				/*
851f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
852f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
853f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
854f8829a4aSRandall Stewart 				 */
855ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
856ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg),
857ff1ffd74SMichael Tuexen 				    "Expected B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
858ff1ffd74SMichael Tuexen 				    chk->rec.data.TSN_seq,
859ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_number,
860ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_seq);
861ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
862a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
863ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
864f8829a4aSRandall Stewart 				*abort_flag = 1;
865f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
866f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
867f8829a4aSRandall Stewart 				/*
868f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
869f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
870f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
871f8829a4aSRandall Stewart 				 */
872ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
873ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg),
874ff1ffd74SMichael Tuexen 				    "Didn't expect B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
875ff1ffd74SMichael Tuexen 				    chk->rec.data.TSN_seq,
876ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_number,
877ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_seq);
878ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
879a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
880ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
881f8829a4aSRandall Stewart 				*abort_flag = 1;
882f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
883f8829a4aSRandall Stewart 				/*
884f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
885f8829a4aSRandall Stewart 				 * piece
886f8829a4aSRandall Stewart 				 */
887f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
888f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
889f8829a4aSRandall Stewart 					/* Got to be the right STR No */
890ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
891f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
892f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
893ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
894ff1ffd74SMichael Tuexen 					    "Expected SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
895ff1ffd74SMichael Tuexen 					    asoc->str_of_pdapi,
896ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
897ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
898ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
899ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
900a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
901ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
902f8829a4aSRandall Stewart 					*abort_flag = 1;
903f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
904f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
905b5c16493SMichael Tuexen 				    chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
906f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
907ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
908f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
909f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
910ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
911ff1ffd74SMichael Tuexen 					    "Expected SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
912ff1ffd74SMichael Tuexen 					    asoc->ssn_of_pdapi,
913ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
914ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
915ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
916ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
917a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
918ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
919f8829a4aSRandall Stewart 					*abort_flag = 1;
920f8829a4aSRandall Stewart 				}
921f8829a4aSRandall Stewart 			}
922f8829a4aSRandall Stewart 		}
923f8829a4aSRandall Stewart 		return;
924f8829a4aSRandall Stewart 	}
925f8829a4aSRandall Stewart 	/* Find its place */
926f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
92720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
928f8829a4aSRandall Stewart 			/*
929f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
930f8829a4aSRandall Stewart 			 * before this one
931f8829a4aSRandall Stewart 			 */
932f8829a4aSRandall Stewart 			/* A check */
933f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
934f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
935f8829a4aSRandall Stewart 			next = at;
936f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
937f8829a4aSRandall Stewart 			break;
938f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
939f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
940f8829a4aSRandall Stewart 			/*
941f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
942f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
943f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
944f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
945f8829a4aSRandall Stewart 			 * away the chunk!
946f8829a4aSRandall Stewart 			 */
947f8829a4aSRandall Stewart 			if (chk->data) {
948f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
949f8829a4aSRandall Stewart 				chk->data = NULL;
950f8829a4aSRandall Stewart 			}
951689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
952f8829a4aSRandall Stewart 			return;
953f8829a4aSRandall Stewart 		} else {
954f8829a4aSRandall Stewart 			prev = at;
955f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
956f8829a4aSRandall Stewart 				/*
957f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
958f8829a4aSRandall Stewart 				 * one
959f8829a4aSRandall Stewart 				 */
960f8829a4aSRandall Stewart 				/* check it first */
961f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
962f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
963f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
964f8829a4aSRandall Stewart 				break;
965f8829a4aSRandall Stewart 			}
966f8829a4aSRandall Stewart 		}
967f8829a4aSRandall Stewart 	}
968f8829a4aSRandall Stewart 	/* Now the audits */
969f8829a4aSRandall Stewart 	if (prev) {
970f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
971f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
972f8829a4aSRandall Stewart 			/*
973f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
974f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
975f8829a4aSRandall Stewart 			 */
976f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
977f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
978f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
979f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
980f8829a4aSRandall Stewart 				/*
981f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
982f8829a4aSRandall Stewart 				 * fragment
983f8829a4aSRandall Stewart 				 */
984f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
985f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
986ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
987ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
988ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
989ff1ffd74SMichael Tuexen 					    "Can't handle B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
990ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
991ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
992ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
993ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
994a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
995ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
996f8829a4aSRandall Stewart 					*abort_flag = 1;
997f8829a4aSRandall Stewart 					return;
998f8829a4aSRandall Stewart 				}
999f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1000f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1001f8829a4aSRandall Stewart 					/*
1002f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1003f8829a4aSRandall Stewart 					 * they must be the same.
1004f8829a4aSRandall Stewart 					 */
1005ff1ffd74SMichael Tuexen 					SCTP_PRINTF("Prev check - Gak, Evil plot, sid:%d not the same as at:%d\n",
1006f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1007f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1008ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1009ff1ffd74SMichael Tuexen 					    "Expect SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1010ff1ffd74SMichael Tuexen 					    prev->rec.data.stream_number,
1011ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1012ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1013ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1014ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1015a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1016ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1017f8829a4aSRandall Stewart 					*abort_flag = 1;
1018f8829a4aSRandall Stewart 					return;
1019f8829a4aSRandall Stewart 				}
1020f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1021f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1022f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1023f8829a4aSRandall Stewart 					/*
1024f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1025f8829a4aSRandall Stewart 					 * they must be the same.
1026f8829a4aSRandall Stewart 					 */
1027ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1028f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1029f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1030ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1031ff1ffd74SMichael Tuexen 					    "Expect SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1032ff1ffd74SMichael Tuexen 					    prev->rec.data.stream_seq,
1033ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1034ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1035ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1036ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1037a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1038ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1039f8829a4aSRandall Stewart 					*abort_flag = 1;
1040f8829a4aSRandall Stewart 					return;
1041f8829a4aSRandall Stewart 				}
1042f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1043f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1044f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1045f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1046f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1047ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1048ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1049ff1ffd74SMichael Tuexen 					    "Expect B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1050ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1051ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1052ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1053ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1054a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1055ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1056f8829a4aSRandall Stewart 					*abort_flag = 1;
1057f8829a4aSRandall Stewart 					return;
1058f8829a4aSRandall Stewart 				}
1059f8829a4aSRandall Stewart 			}
1060f8829a4aSRandall Stewart 		}
1061f8829a4aSRandall Stewart 	}
1062f8829a4aSRandall Stewart 	if (next) {
1063f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1064f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1065f8829a4aSRandall Stewart 			/*
1066f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1067f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1068f8829a4aSRandall Stewart 			 */
1069f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1070f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1071f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1072f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1073ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1074ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1075ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1076ff1ffd74SMichael Tuexen 					    "Expect only E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1077ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1078ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1079ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1080ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1081a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1082ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1083f8829a4aSRandall Stewart 					*abort_flag = 1;
1084f8829a4aSRandall Stewart 					return;
1085f8829a4aSRandall Stewart 				}
1086f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1087f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1088f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1089f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1090f8829a4aSRandall Stewart 				/*
1091f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1092f8829a4aSRandall Stewart 				 * LAST
1093f8829a4aSRandall Stewart 				 */
1094f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1095f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1096ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1097ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1098ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1099ff1ffd74SMichael Tuexen 					    "Didn't expect E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1100ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1101ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1102ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1103ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1104a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1105ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1106f8829a4aSRandall Stewart 					*abort_flag = 1;
1107f8829a4aSRandall Stewart 					return;
1108f8829a4aSRandall Stewart 				}
1109f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1110f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1111f8829a4aSRandall Stewart 					/*
1112f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1113f8829a4aSRandall Stewart 					 * they must be the same.
1114f8829a4aSRandall Stewart 					 */
1115ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1116f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1117f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1118ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1119ff1ffd74SMichael Tuexen 					    "Required SID %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1120ff1ffd74SMichael Tuexen 					    next->rec.data.stream_number,
1121ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1122ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1123ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1124ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1125a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1126ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1127f8829a4aSRandall Stewart 					*abort_flag = 1;
1128f8829a4aSRandall Stewart 					return;
1129f8829a4aSRandall Stewart 				}
1130f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1131f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1132f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
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, sseq:%d not the same as at:%d\n",
1138f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1139f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1140ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1141ff1ffd74SMichael Tuexen 					    "Required SSN %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1142ff1ffd74SMichael Tuexen 					    next->rec.data.stream_seq,
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_13;
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 				}
1152f8829a4aSRandall Stewart 			}
1153f8829a4aSRandall Stewart 		}
1154f8829a4aSRandall Stewart 	}
1155f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1156f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1157f8829a4aSRandall Stewart }
1158f8829a4aSRandall Stewart 
1159f8829a4aSRandall Stewart /*
1160f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1161f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1162f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1163f8829a4aSRandall Stewart  * :< more cycles.
1164f8829a4aSRandall Stewart  */
1165f8829a4aSRandall Stewart static int
1166f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1167f8829a4aSRandall Stewart     uint32_t TSN_seq)
1168f8829a4aSRandall Stewart {
1169f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1170f8829a4aSRandall Stewart 	uint32_t tsn_est;
1171f8829a4aSRandall Stewart 
1172f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
117320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1174f8829a4aSRandall Stewart 			/* is it one bigger? */
1175f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1176f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1177f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1178f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1179f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1180f8829a4aSRandall Stewart 					/*
1181f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1182f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1183f8829a4aSRandall Stewart 					 * middle/last, not a complete
1184f8829a4aSRandall Stewart 					 * chunk.
1185f8829a4aSRandall Stewart 					 */
1186f8829a4aSRandall Stewart 					return (1);
1187f8829a4aSRandall Stewart 				} else {
1188f8829a4aSRandall Stewart 					/*
1189f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1190f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1191f8829a4aSRandall Stewart 					 * self- contained one.
1192f8829a4aSRandall Stewart 					 */
1193f8829a4aSRandall Stewart 					return (0);
1194f8829a4aSRandall Stewart 				}
1195f8829a4aSRandall Stewart 			}
1196f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1197f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1198f8829a4aSRandall Stewart 			return (1);
1199f8829a4aSRandall Stewart 		} else {
1200f8829a4aSRandall Stewart 			/*
1201f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1202f8829a4aSRandall Stewart 			 * need to be right before it.
1203f8829a4aSRandall Stewart 			 */
1204f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1205f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1206f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1207f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1208f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1209f8829a4aSRandall Stewart 					return (1);
1210f8829a4aSRandall Stewart 				} else {
1211f8829a4aSRandall Stewart 					return (0);
1212f8829a4aSRandall Stewart 				}
1213f8829a4aSRandall Stewart 			}
1214f8829a4aSRandall Stewart 		}
1215f8829a4aSRandall Stewart 	}
1216f8829a4aSRandall Stewart 	return (0);
1217f8829a4aSRandall Stewart }
1218f8829a4aSRandall Stewart 
1219f8829a4aSRandall Stewart static int
1220f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1221f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1222f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1223f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1224f8829a4aSRandall Stewart {
1225f8829a4aSRandall Stewart 	/* Process a data chunk */
1226f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1227f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1228f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1229f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
12307215cc1bSMichael Tuexen 	int the_len;
1231139bc87fSRandall Stewart 	int need_reasm_check = 0;
1232f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1233ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
1234ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
1235f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1236f42a358aSRandall Stewart 	int ordered;
1237f42a358aSRandall Stewart 	uint32_t protocol_id;
1238f42a358aSRandall Stewart 	uint8_t chunk_flags;
123917205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1240f8829a4aSRandall Stewart 
1241f8829a4aSRandall Stewart 	chk = NULL;
1242f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1243f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1244b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1245b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1246b3f1ea41SRandall Stewart 	}
1247f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
124837f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1249b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1250c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
125180fefe0aSRandall Stewart 	}
1252ad81507eSRandall Stewart 	if (stcb == NULL) {
1253ad81507eSRandall Stewart 		return (0);
1254ad81507eSRandall Stewart 	}
125580fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
125620b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1257f8829a4aSRandall Stewart 		/* It is a duplicate */
1258f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1259f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1260f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1261f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1262f8829a4aSRandall Stewart 			asoc->numduptsns++;
1263f8829a4aSRandall Stewart 		}
1264b201f536SRandall Stewart 		asoc->send_sack = 1;
1265f8829a4aSRandall Stewart 		return (0);
1266f8829a4aSRandall Stewart 	}
1267f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1268d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1269f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1270f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1271f8829a4aSRandall Stewart 		return (0);
1272f8829a4aSRandall Stewart 	}
1273f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1274207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
12750696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1276f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1277f8829a4aSRandall Stewart 			return (0);
1278f8829a4aSRandall Stewart 		}
1279f8829a4aSRandall Stewart 	}
128020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1281f8829a4aSRandall Stewart 		*high_tsn = tsn;
1282f8829a4aSRandall Stewart 	}
1283f8829a4aSRandall Stewart 	/* See if we have received this one already */
128477acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
128577acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1286f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1287f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1288f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1289f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1290f8829a4aSRandall Stewart 			asoc->numduptsns++;
1291f8829a4aSRandall Stewart 		}
129242551e99SRandall Stewart 		asoc->send_sack = 1;
1293f8829a4aSRandall Stewart 		return (0);
1294f8829a4aSRandall Stewart 	}
1295f8829a4aSRandall Stewart 	/*
1296f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1297f8829a4aSRandall Stewart 	 * to be sent up above
1298f8829a4aSRandall Stewart 	 */
1299ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1300f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1301ff1ffd74SMichael Tuexen 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))) {
1302f8829a4aSRandall Stewart 		/*
1303f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1304f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1305f8829a4aSRandall Stewart 		 */
1306ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
1307a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1308f8829a4aSRandall Stewart 		*abort_flag = 1;
1309f8829a4aSRandall Stewart 		return (0);
1310f8829a4aSRandall Stewart 	}
1311f8829a4aSRandall Stewart 	/*
1312f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1313f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1314f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1315f8829a4aSRandall Stewart 	 */
1316f8829a4aSRandall Stewart 
1317f8829a4aSRandall Stewart 	/* now do the tests */
1318f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1319f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1320b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1321f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1322f8829a4aSRandall Stewart 		/*
1323f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1324f8829a4aSRandall Stewart 		 * the reader is doing its job...
1325f8829a4aSRandall Stewart 		 */
1326f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1327f8829a4aSRandall Stewart 			/* some to read, wake-up */
1328ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1329ceaad40aSRandall Stewart 			struct socket *so;
1330ceaad40aSRandall Stewart 
1331ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1332ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1333ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1334ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1335ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1336ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1337ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1338ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1339ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1340ceaad40aSRandall Stewart 				return (0);
1341ceaad40aSRandall Stewart 			}
1342ceaad40aSRandall Stewart #endif
1343f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1344ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1345ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1346ceaad40aSRandall Stewart #endif
1347f8829a4aSRandall Stewart 		}
1348f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
134920b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
135020b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1351f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1352f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1353f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1354f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1355b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1356f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1357f8829a4aSRandall Stewart 			} else {
1358f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1359f8829a4aSRandall Stewart 			}
1360f8829a4aSRandall Stewart 			*break_flag = 1;
1361f8829a4aSRandall Stewart 			return (0);
1362f8829a4aSRandall Stewart 		}
1363f8829a4aSRandall Stewart 	}
1364f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1365f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1366f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1367f8829a4aSRandall Stewart 		struct mbuf *mb;
1368f8829a4aSRandall Stewart 
1369f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1370eb1b1807SGleb Smirnoff 		    0, M_NOWAIT, 1, MT_DATA);
1371f8829a4aSRandall Stewart 		if (mb != NULL) {
1372f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1373139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1374f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1375f8829a4aSRandall Stewart 			/*
1376f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1377f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1378f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1379f8829a4aSRandall Stewart 			 */
1380139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1381f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1382f8829a4aSRandall Stewart 			phdr->param_length =
1383f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1384f8829a4aSRandall Stewart 			phdr++;
1385f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1386f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1387f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1388f8829a4aSRandall Stewart 			phdr->param_length = 0;
1389f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1390f8829a4aSRandall Stewart 		}
1391f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1392207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1393830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
139420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1395830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1396d06c82f1SRandall Stewart 		}
1397d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1398d06c82f1SRandall Stewart 			/* Update cum-ack */
1399d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1400d06c82f1SRandall Stewart 		}
1401f8829a4aSRandall Stewart 		return (0);
1402f8829a4aSRandall Stewart 	}
1403f8829a4aSRandall Stewart 	/*
1404f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1405f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1406f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1407f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1408f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1409f8829a4aSRandall Stewart 	 */
1410f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1411f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
141218e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
141318e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
141418e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
141518e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
141618e198d3SRandall Stewart 	}
1417f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1418f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1419f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1420f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1421f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
142218e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
142318e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
142418e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1425f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1426f42a358aSRandall Stewart #endif
1427f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1428d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1429f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
143020b07a4dSMichael Tuexen 	    SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1431f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1432ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1433ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1434f8829a4aSRandall Stewart 
1435ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1436ff1ffd74SMichael Tuexen 		    asoc->strmin[strmno].last_sequence_delivered,
1437ff1ffd74SMichael Tuexen 		    tsn, strmno, strmseq);
1438ff1ffd74SMichael Tuexen 		op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1439a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1440ff1ffd74SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1441f8829a4aSRandall Stewart 		*abort_flag = 1;
1442f8829a4aSRandall Stewart 		return (0);
1443f8829a4aSRandall Stewart 	}
1444f42a358aSRandall Stewart 	/************************************
1445f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1446f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1447f42a358aSRandall Stewart 	 *************************************/
1448f42a358aSRandall Stewart 
1449f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1450f8829a4aSRandall Stewart 	if (last_chunk == 0) {
145144b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1452f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1453eb1b1807SGleb Smirnoff 		    the_len, M_NOWAIT);
1454f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1455b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1456f8829a4aSRandall Stewart 			struct mbuf *mat;
1457f8829a4aSRandall Stewart 
145860990c0cSMichael Tuexen 			for (mat = dmbuf; mat; mat = SCTP_BUF_NEXT(mat)) {
1459139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1460f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1461f8829a4aSRandall Stewart 				}
1462f8829a4aSRandall Stewart 			}
1463f8829a4aSRandall Stewart 		}
1464f8829a4aSRandall Stewart #endif
1465f8829a4aSRandall Stewart 	} else {
1466f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1467139bc87fSRandall Stewart 		int l_len;
1468139bc87fSRandall Stewart 
1469f8829a4aSRandall Stewart 		dmbuf = *m;
1470f8829a4aSRandall Stewart 		/* lop off the top part */
1471f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1472139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1473139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1474139bc87fSRandall Stewart 		} else {
1475139bc87fSRandall Stewart 			/*
1476139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1477139bc87fSRandall Stewart 			 * this to often :-0
1478139bc87fSRandall Stewart 			 */
1479139bc87fSRandall Stewart 			struct mbuf *lat;
1480139bc87fSRandall Stewart 
1481139bc87fSRandall Stewart 			l_len = 0;
148260990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1483139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1484139bc87fSRandall Stewart 			}
1485139bc87fSRandall Stewart 		}
1486139bc87fSRandall Stewart 		if (l_len > the_len) {
1487f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1488139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1489f8829a4aSRandall Stewart 		}
1490f8829a4aSRandall Stewart 	}
1491f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1492f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1493f8829a4aSRandall Stewart 		return (0);
1494f8829a4aSRandall Stewart 	}
1495f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1496f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1497f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1498f42a358aSRandall Stewart 	    ((ordered == 0) ||
149936ec9f81SMichael Tuexen 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1500f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1501f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1502f8829a4aSRandall Stewart 		/*
1503f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1504f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1505f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1506f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1507f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1508f8829a4aSRandall Stewart 		 */
1509f8829a4aSRandall Stewart 
1510f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1511f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1512f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1513f42a358aSRandall Stewart 		    protocol_id,
1514f8829a4aSRandall Stewart 		    strmno, strmseq,
1515f42a358aSRandall Stewart 		    chunk_flags,
1516f8829a4aSRandall Stewart 		    dmbuf);
1517f8829a4aSRandall Stewart 		if (control == NULL) {
1518f8829a4aSRandall Stewart 			goto failed_express_del;
1519f8829a4aSRandall Stewart 		}
15201ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
152120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
15221ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
15231ea735c8SMichael Tuexen 		}
1524cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1525cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1526cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1527830d754dSRandall Stewart 
1528f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1529f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1530f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1531f8829a4aSRandall Stewart 		}
1532f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1533b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
15346a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1535f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
153680fefe0aSRandall Stewart 		}
1537f8829a4aSRandall Stewart 		control = NULL;
1538b5c16493SMichael Tuexen 
1539f8829a4aSRandall Stewart 		goto finish_express_del;
1540f8829a4aSRandall Stewart 	}
1541f8829a4aSRandall Stewart failed_express_del:
1542f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1543f8829a4aSRandall Stewart 	chk = NULL;
1544f8829a4aSRandall Stewart 	control = NULL;
1545f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1546f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1547f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1548f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1549f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1550f8829a4aSRandall Stewart 	    ) {
1551f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1552f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1553f8829a4aSRandall Stewart 			/* Can't be another first? */
1554f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1555f8829a4aSRandall Stewart 		}
1556f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1557f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1558f8829a4aSRandall Stewart 			int end = 0;
1559f8829a4aSRandall Stewart 
1560f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1561f8829a4aSRandall Stewart 				end = 1;
1562f8829a4aSRandall Stewart 			}
1563f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1564f8829a4aSRandall Stewart 			    tsn,
1565f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1566ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1567f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1568f8829a4aSRandall Stewart 			}
156977acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
157020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1571830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1572830d754dSRandall Stewart 			}
1573f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1574f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1575f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1576f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1577f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1578f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1579f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1580f8829a4aSRandall Stewart 			if (end) {
1581f8829a4aSRandall Stewart 				/* clean up the flags and such */
1582f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1583f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1584f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1585a5d547adSRandall Stewart 				}
1586f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1587139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1588139bc87fSRandall Stewart 					/*
1589139bc87fSRandall Stewart 					 * There could be another message
1590139bc87fSRandall Stewart 					 * ready
1591139bc87fSRandall Stewart 					 */
1592139bc87fSRandall Stewart 					need_reasm_check = 1;
1593139bc87fSRandall Stewart 				}
1594f8829a4aSRandall Stewart 			}
1595f8829a4aSRandall Stewart 			control = NULL;
1596f8829a4aSRandall Stewart 			goto finish_express_del;
1597f8829a4aSRandall Stewart 		}
1598f8829a4aSRandall Stewart 	}
1599f8829a4aSRandall Stewart failed_pdapi_express_del:
1600f8829a4aSRandall Stewart 	control = NULL;
160177acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
160277acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
160320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
160477acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
160577acdc25SRandall Stewart 		}
160677acdc25SRandall Stewart 	} else {
160777acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
160820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
160977acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
161077acdc25SRandall Stewart 		}
161177acdc25SRandall Stewart 	}
1612f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1613f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1614f8829a4aSRandall Stewart 		if (chk == NULL) {
1615f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1616f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1617f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1618f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1619f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1620f8829a4aSRandall Stewart 			}
1621f8829a4aSRandall Stewart 			return (0);
1622f8829a4aSRandall Stewart 		}
1623f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1624f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1625f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1626f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1627f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1628f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1629f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1630f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1631f8829a4aSRandall Stewart 		chk->asoc = asoc;
1632f8829a4aSRandall Stewart 		chk->send_size = the_len;
1633f8829a4aSRandall Stewart 		chk->whoTo = net;
1634f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1635f8829a4aSRandall Stewart 		chk->data = dmbuf;
1636f8829a4aSRandall Stewart 	} else {
1637f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1638f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1639f42a358aSRandall Stewart 		    protocol_id,
1640f8829a4aSRandall Stewart 		    strmno, strmseq,
1641f42a358aSRandall Stewart 		    chunk_flags,
1642f8829a4aSRandall Stewart 		    dmbuf);
1643f8829a4aSRandall Stewart 		if (control == NULL) {
1644f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1645f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1646f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1647f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1648f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1649f8829a4aSRandall Stewart 			}
1650f8829a4aSRandall Stewart 			return (0);
1651f8829a4aSRandall Stewart 		}
1652f8829a4aSRandall Stewart 		control->length = the_len;
1653f8829a4aSRandall Stewart 	}
1654f8829a4aSRandall Stewart 
1655f8829a4aSRandall Stewart 	/* Mark it as received */
1656f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1657f8829a4aSRandall Stewart 	if (control != NULL) {
1658f8829a4aSRandall Stewart 		/* First a sanity check */
1659f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1660f8829a4aSRandall Stewart 			/*
1661f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1662f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1663f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1664f8829a4aSRandall Stewart 			 * broken.
1665f8829a4aSRandall Stewart 			 */
1666f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1667f8829a4aSRandall Stewart 
1668f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1669f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1670f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1671f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1672f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1673f8829a4aSRandall Stewart 				control->data = NULL;
16745bead436SRandall Stewart 				if (control->whoFrom) {
1675f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
16765bead436SRandall Stewart 					control->whoFrom = NULL;
16775bead436SRandall Stewart 				}
1678f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1679ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "Reas. queue emtpy, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1680ff1ffd74SMichael Tuexen 				    tsn, strmno, strmseq);
1681ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1682a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1683ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1684f8829a4aSRandall Stewart 				*abort_flag = 1;
1685f8829a4aSRandall Stewart 				return (0);
1686f8829a4aSRandall Stewart 			} else {
1687f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1688f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1689f8829a4aSRandall Stewart 					control->data = NULL;
16905bead436SRandall Stewart 					if (control->whoFrom) {
1691f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
16925bead436SRandall Stewart 						control->whoFrom = NULL;
16935bead436SRandall Stewart 					}
1694f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1695ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg), "PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1696ff1ffd74SMichael Tuexen 					    tsn, strmno, strmseq);
1697ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1698a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1699ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1700f8829a4aSRandall Stewart 					*abort_flag = 1;
1701f8829a4aSRandall Stewart 					return (0);
1702f8829a4aSRandall Stewart 				}
1703f8829a4aSRandall Stewart 			}
1704f8829a4aSRandall Stewart 		} else {
1705f8829a4aSRandall Stewart 			/* No PDAPI running */
1706f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1707f8829a4aSRandall Stewart 				/*
1708f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
1709f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
1710f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
1711f8829a4aSRandall Stewart 				 * is broken or evil.
1712f8829a4aSRandall Stewart 				 */
1713f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1714f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1715f8829a4aSRandall Stewart 					control->data = NULL;
17165bead436SRandall Stewart 					if (control->whoFrom) {
1717f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
17185bead436SRandall Stewart 						control->whoFrom = NULL;
17195bead436SRandall Stewart 					}
1720f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1721ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg), "No PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1722ff1ffd74SMichael Tuexen 					    tsn, strmno, strmseq);
1723ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
1724a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1725ff1ffd74SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1726f8829a4aSRandall Stewart 					*abort_flag = 1;
1727f8829a4aSRandall Stewart 					return (0);
1728f8829a4aSRandall Stewart 				}
1729f8829a4aSRandall Stewart 			}
1730f8829a4aSRandall Stewart 		}
1731f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
1732f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
1733f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
1734b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
1735f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
1736f8829a4aSRandall Stewart 			    control,
1737cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1738f8829a4aSRandall Stewart 		} else {
1739f8829a4aSRandall Stewart 			/*
1740f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
1741f8829a4aSRandall Stewart 			 * could be more smart about this and check the
1742f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
1743f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
1744f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
1745f8829a4aSRandall Stewart 			 *
1746f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
1747f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
1748f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
1749f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
1750f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
1751f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
1752f8829a4aSRandall Stewart 			 * singletons I must worry about.
1753f8829a4aSRandall Stewart 			 */
1754f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
175520b07a4dSMichael Tuexen 			    SCTP_TSN_GT(tsn, liste->tsn)) {
1756f8829a4aSRandall Stewart 				/*
1757f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
1758f8829a4aSRandall Stewart 				 * ahead and queue it.
1759f8829a4aSRandall Stewart 				 */
1760f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
1761f8829a4aSRandall Stewart 					/* first one on */
1762f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
1763f8829a4aSRandall Stewart 				} else {
17644a9ef3f8SMichael Tuexen 					struct sctp_queued_to_read *ctlOn,
17654a9ef3f8SMichael Tuexen 					                   *nctlOn;
1766f8829a4aSRandall Stewart 					unsigned char inserted = 0;
1767f8829a4aSRandall Stewart 
17684a9ef3f8SMichael Tuexen 					TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
176920b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
17704a9ef3f8SMichael Tuexen 							continue;
1771f8829a4aSRandall Stewart 						} else {
1772f8829a4aSRandall Stewart 							/* found it */
1773f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
1774f8829a4aSRandall Stewart 							inserted = 1;
1775f8829a4aSRandall Stewart 							break;
1776f8829a4aSRandall Stewart 						}
1777f8829a4aSRandall Stewart 					}
1778f8829a4aSRandall Stewart 					if (inserted == 0) {
1779f8829a4aSRandall Stewart 						/*
1780f8829a4aSRandall Stewart 						 * must be put at end, use
1781f8829a4aSRandall Stewart 						 * prevP (all setup from
1782f8829a4aSRandall Stewart 						 * loop) to setup nextP.
1783f8829a4aSRandall Stewart 						 */
1784f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
1785f8829a4aSRandall Stewart 					}
1786f8829a4aSRandall Stewart 				}
1787f8829a4aSRandall Stewart 			} else {
1788f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
1789f8829a4aSRandall Stewart 				if (*abort_flag) {
1790f8829a4aSRandall Stewart 					return (0);
1791f8829a4aSRandall Stewart 				}
1792f8829a4aSRandall Stewart 			}
1793f8829a4aSRandall Stewart 		}
1794f8829a4aSRandall Stewart 	} else {
1795f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
1796f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
1797f8829a4aSRandall Stewart 		if (*abort_flag) {
1798a5d547adSRandall Stewart 			/*
1799a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
1800a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
1801a5d547adSRandall Stewart 			 */
1802a5d547adSRandall Stewart 			*m = NULL;
1803f8829a4aSRandall Stewart 			return (0);
1804f8829a4aSRandall Stewart 		}
1805f8829a4aSRandall Stewart 	}
1806f8829a4aSRandall Stewart finish_express_del:
1807307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
1808307b49efSMichael Tuexen 		/* Update cum-ack */
1809307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
1810307b49efSMichael Tuexen 	}
1811f8829a4aSRandall Stewart 	if (last_chunk) {
1812f8829a4aSRandall Stewart 		*m = NULL;
1813f8829a4aSRandall Stewart 	}
1814f42a358aSRandall Stewart 	if (ordered) {
1815f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
1816f8829a4aSRandall Stewart 	} else {
1817f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
1818f8829a4aSRandall Stewart 	}
1819f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
1820f8829a4aSRandall Stewart 	/* Set it present please */
1821b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
18226a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
182380fefe0aSRandall Stewart 	}
1824b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1825f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
1826f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
182780fefe0aSRandall Stewart 	}
182817205eccSRandall Stewart 	/* check the special flag for stream resets */
182917205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
183020b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
183117205eccSRandall Stewart 		/*
183217205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
183317205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
183417205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
183517205eccSRandall Stewart 		 * pending_reply_queue.
183617205eccSRandall Stewart 		 */
18374a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
183817205eccSRandall Stewart 
1839a169d6ecSMichael Tuexen 		sctp_reset_in_stream(stcb, liste->number_entries, liste->list_of_streams);
184017205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
1841207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
18423c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
184317205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
18444a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
184517205eccSRandall Stewart 			/* All can be removed */
18464a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
184717205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
184817205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
184917205eccSRandall Stewart 				if (*abort_flag) {
185017205eccSRandall Stewart 					return (0);
185117205eccSRandall Stewart 				}
185217205eccSRandall Stewart 			}
18534a9ef3f8SMichael Tuexen 		} else {
18544a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
185520b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
18564a9ef3f8SMichael Tuexen 					break;
18574a9ef3f8SMichael Tuexen 				}
185817205eccSRandall Stewart 				/*
185917205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
186017205eccSRandall Stewart 				 * process it which is the NOT of
186117205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
186217205eccSRandall Stewart 				 */
186317205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
186417205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
186517205eccSRandall Stewart 				if (*abort_flag) {
186617205eccSRandall Stewart 					return (0);
186717205eccSRandall Stewart 				}
186817205eccSRandall Stewart 			}
186917205eccSRandall Stewart 		}
187017205eccSRandall Stewart 		/*
187117205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
187217205eccSRandall Stewart 		 * held on reassembly queue?
187317205eccSRandall Stewart 		 */
187417205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
187517205eccSRandall Stewart 		need_reasm_check = 0;
187617205eccSRandall Stewart 	}
1877139bc87fSRandall Stewart 	if (need_reasm_check) {
1878139bc87fSRandall Stewart 		/* Another one waits ? */
1879139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
1880139bc87fSRandall Stewart 	}
1881f8829a4aSRandall Stewart 	return (1);
1882f8829a4aSRandall Stewart }
1883f8829a4aSRandall Stewart 
1884f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
1885b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1886b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1887b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1888b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1889b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1890b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1891b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1892b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
1893b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1894b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1895b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1896b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1897b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1898b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1899b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1900b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
1901b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1902b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1903b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1904b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1905b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1906b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1907b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1908b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
1909b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1910b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1911b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1912b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
1913b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1914b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
1915b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
1916b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
1917f8829a4aSRandall Stewart };
1918f8829a4aSRandall Stewart 
1919f8829a4aSRandall Stewart 
1920f8829a4aSRandall Stewart void
1921b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
1922f8829a4aSRandall Stewart {
1923f8829a4aSRandall Stewart 	/*
1924f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
1925f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
192666bd30bdSRandall Stewart 	 *
192766bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
192866bd30bdSRandall Stewart 	 * make up the postion of the cum-ack would be in the nr-mapping
192966bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
193066bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
193166bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
193266bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
193366bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
1934f8829a4aSRandall Stewart 	 */
1935f8829a4aSRandall Stewart 	struct sctp_association *asoc;
1936b3f1ea41SRandall Stewart 	int at;
193766bd30bdSRandall Stewart 	uint8_t val;
1938f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
193977acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
1940f8829a4aSRandall Stewart 
1941f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
1942f8829a4aSRandall Stewart 
1943f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
1944f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
1945f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
1946f8829a4aSRandall Stewart 	/*
1947f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
1948f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
1949f8829a4aSRandall Stewart 	 */
1950f8829a4aSRandall Stewart 	at = 0;
1951b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
195266bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
195366bd30bdSRandall Stewart 		if (val == 0xff) {
1954f8829a4aSRandall Stewart 			at += 8;
1955f8829a4aSRandall Stewart 		} else {
1956f8829a4aSRandall Stewart 			/* there is a 0 bit */
195766bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
1958f8829a4aSRandall Stewart 			break;
1959f8829a4aSRandall Stewart 		}
1960f8829a4aSRandall Stewart 	}
1961b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
1962f8829a4aSRandall Stewart 
196320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
196420b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
1965a5d547adSRandall Stewart #ifdef INVARIANTS
1966ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
1967ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
1968f8829a4aSRandall Stewart #else
1969ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
1970ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
19710e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
1972b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1973b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1974b3f1ea41SRandall Stewart 		}
1975f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
1976830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
1977f8829a4aSRandall Stewart #endif
1978f8829a4aSRandall Stewart 	}
197920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
198077acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
198177acdc25SRandall Stewart 	} else {
198277acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
198377acdc25SRandall Stewart 	}
198477acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
1985f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
198677acdc25SRandall Stewart 		/* highest becomes the cum-ack */
198737f144ebSMichael Tuexen 		int clr;
198837f144ebSMichael Tuexen 
198937f144ebSMichael Tuexen #ifdef INVARIANTS
199037f144ebSMichael Tuexen 		unsigned int i;
199137f144ebSMichael Tuexen 
199237f144ebSMichael Tuexen #endif
1993f8829a4aSRandall Stewart 
1994f8829a4aSRandall Stewart 		/* clear the array */
1995b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
1996c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
1997f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
1998f8829a4aSRandall Stewart 		}
1999f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2000830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
200137f144ebSMichael Tuexen #ifdef INVARIANTS
2002b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2003b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2004cd3fd531SMichael Tuexen 				SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2005b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2006b5c16493SMichael Tuexen 			}
2007b5c16493SMichael Tuexen 		}
200837f144ebSMichael Tuexen #endif
200977acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
201077acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2011f8829a4aSRandall Stewart 	} else if (at >= 8) {
2012f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2013b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2014b3f1ea41SRandall Stewart 
2015f8829a4aSRandall Stewart 		/*
2016f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2017f8829a4aSRandall Stewart 		 * TSN value
2018f8829a4aSRandall Stewart 		 */
201977acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
202077acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2021f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
202277acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2023d55b0b1bSRandall Stewart #ifdef INVARIANTS
2024f8829a4aSRandall Stewart 			panic("impossible slide");
2025d55b0b1bSRandall Stewart #else
2026cd3fd531SMichael Tuexen 			SCTP_PRINTF("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
202777acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2028d55b0b1bSRandall Stewart 			return;
2029d55b0b1bSRandall Stewart #endif
2030f8829a4aSRandall Stewart 		}
2031b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2032b3f1ea41SRandall Stewart #ifdef INVARIANTS
2033b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2034b3f1ea41SRandall Stewart #else
2035cd3fd531SMichael Tuexen 			SCTP_PRINTF("Gak, would have overrun map end:%d slide_end:%d\n",
2036b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2037b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2038b3f1ea41SRandall Stewart #endif
2039b3f1ea41SRandall Stewart 		}
2040f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2041b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2042f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2043f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2044f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2045f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
204680fefe0aSRandall Stewart 		}
2047f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2048f8829a4aSRandall Stewart 		    distance < 0) {
2049f8829a4aSRandall Stewart 			/*
2050f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2051f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2052f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2053f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2054f8829a4aSRandall Stewart 			 */
2055f8829a4aSRandall Stewart 
2056b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2057f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2058f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2059f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
206080fefe0aSRandall Stewart 			}
2061f8829a4aSRandall Stewart 		} else {
2062f8829a4aSRandall Stewart 			int ii;
2063f8829a4aSRandall Stewart 
2064f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
206537f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
206637f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
206777acdc25SRandall Stewart 
2068f8829a4aSRandall Stewart 			}
2069aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2070f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
207177acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2072f8829a4aSRandall Stewart 			}
2073ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2074ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2075ee94f0a2SMichael Tuexen 			}
2076ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2077ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2078ee94f0a2SMichael Tuexen 			}
2079f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2080b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2081f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2082f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2083f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
208480fefe0aSRandall Stewart 			}
2085830d754dSRandall Stewart 		}
2086830d754dSRandall Stewart 	}
2087b5c16493SMichael Tuexen }
2088b5c16493SMichael Tuexen 
2089b5c16493SMichael Tuexen void
20907215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2091b5c16493SMichael Tuexen {
2092b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2093b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2094b5c16493SMichael Tuexen 
2095b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
209620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2097b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2098b5c16493SMichael Tuexen 	} else {
2099b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2100b5c16493SMichael Tuexen 	}
2101b5c16493SMichael Tuexen 
2102830d754dSRandall Stewart 	/*
2103f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2104f8829a4aSRandall Stewart 	 * timer (if allowed).
2105f8829a4aSRandall Stewart 	 */
2106f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2107f8829a4aSRandall Stewart 		/*
2108b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2109b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2110b5c16493SMichael Tuexen 		 * SACK
2111f8829a4aSRandall Stewart 		 */
2112139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2113f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2114a5d547adSRandall Stewart 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2115f8829a4aSRandall Stewart 		}
2116ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2117ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2118689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2119f8829a4aSRandall Stewart 	} else {
2120f8829a4aSRandall Stewart 		int is_a_gap;
2121f8829a4aSRandall Stewart 
2122f8829a4aSRandall Stewart 		/* is there a gap now ? */
212320b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2124f8829a4aSRandall Stewart 
2125f8829a4aSRandall Stewart 		/*
2126b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2127b5c16493SMichael Tuexen 		 * since last ack
2128f8829a4aSRandall Stewart 		 */
2129f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2130f8829a4aSRandall Stewart 
213142551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
213242551e99SRandall Stewart 							 * SACK */
2133f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2134f8829a4aSRandall Stewart 							 * longer is one */
2135f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2136f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
213742551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
213842551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2139f8829a4aSRandall Stewart 		    ) {
2140f8829a4aSRandall Stewart 
21417c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2142b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
214342551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2144f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2145f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2146139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2147f8829a4aSRandall Stewart 
2148f8829a4aSRandall Stewart 				/*
2149b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2150b5c16493SMichael Tuexen 				 * even in the face of
2151f8829a4aSRandall Stewart 				 *
2152b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2153b5c16493SMichael Tuexen 				 * have to be sent because of the above
2154b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2155b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2156b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2157f8829a4aSRandall Stewart 				 * the delayed ack timer.
2158f8829a4aSRandall Stewart 				 */
2159f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2160f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2161f8829a4aSRandall Stewart 			} else {
2162f8829a4aSRandall Stewart 				/*
2163b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2164b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2165b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2166f8829a4aSRandall Stewart 				 */
2167ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2168689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2169f8829a4aSRandall Stewart 			}
2170f8829a4aSRandall Stewart 		} else {
217142551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2172f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2173f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2174f8829a4aSRandall Stewart 			}
2175f8829a4aSRandall Stewart 		}
2176f8829a4aSRandall Stewart 	}
2177f8829a4aSRandall Stewart }
2178f8829a4aSRandall Stewart 
2179f8829a4aSRandall Stewart void
2180f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2181f8829a4aSRandall Stewart {
2182f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2183810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2184f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2185f8829a4aSRandall Stewart 
2186f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2187f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2188f8829a4aSRandall Stewart 	}
2189f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2190f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2191f8829a4aSRandall Stewart 		/* no */
2192f8829a4aSRandall Stewart 		return;
2193f8829a4aSRandall Stewart 	}
2194f8829a4aSRandall Stewart 	/*
2195f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2196f8829a4aSRandall Stewart 	 * queue.
2197f8829a4aSRandall Stewart 	 */
2198139bc87fSRandall Stewart doit_again:
2199f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2200f8829a4aSRandall Stewart 	if (chk == NULL) {
2201f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2202f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2203f8829a4aSRandall Stewart 		return;
2204f8829a4aSRandall Stewart 	}
2205f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2206f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2207f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2208f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2209f8829a4aSRandall Stewart 		/*
2210f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2211f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2212f8829a4aSRandall Stewart 		 */
2213f8829a4aSRandall Stewart 
2214f8829a4aSRandall Stewart 		/*
2215f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
221624ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2217f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2218f8829a4aSRandall Stewart 		 */
2219810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
2220d4d23375SMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
2221810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2222810ec536SMichael Tuexen 		} else {
2223810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2224810ec536SMichael Tuexen 		}
2225810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2226f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2227f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2228f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2229f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2230f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2231f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2232f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2233139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2234139bc87fSRandall Stewart 				goto doit_again;
2235139bc87fSRandall Stewart 			}
2236f8829a4aSRandall Stewart 		}
2237f8829a4aSRandall Stewart 	}
2238f8829a4aSRandall Stewart }
2239f8829a4aSRandall Stewart 
2240f8829a4aSRandall Stewart int
2241f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2242b1754ad1SMichael Tuexen     struct sockaddr *src, struct sockaddr *dst,
2243f30ac432SMichael Tuexen     struct sctphdr *sh, struct sctp_inpcb *inp,
2244f30ac432SMichael Tuexen     struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t * high_tsn,
2245f30ac432SMichael Tuexen     uint8_t use_mflowid, uint32_t mflowid,
2246f30ac432SMichael Tuexen     uint32_t vrf_id, uint16_t port)
2247f8829a4aSRandall Stewart {
2248f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2249f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2250f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2251f8829a4aSRandall Stewart 	int stop_proc = 0;
2252f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
22538f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2254f8829a4aSRandall Stewart 	struct mbuf *m;
22558f777478SMichael Tuexen 	uint32_t highest_tsn;
2256f8829a4aSRandall Stewart 
2257f8829a4aSRandall Stewart 	/* set the rwnd */
2258f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2259f8829a4aSRandall Stewart 
2260f8829a4aSRandall Stewart 	m = *mm;
2261f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2262f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
226320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
22648f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
22658f777478SMichael Tuexen 	} else {
22668f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2267f8829a4aSRandall Stewart 	}
226820b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2269f8829a4aSRandall Stewart 	/*
2270f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2271f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2272f8829a4aSRandall Stewart 	 * chunk is assigned.
2273f8829a4aSRandall Stewart 	 */
2274f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2275f8829a4aSRandall Stewart 
2276d06c82f1SRandall Stewart 	/*-
2277f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2278f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2279f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2280f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2281d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2282d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2283f8829a4aSRandall Stewart 	 */
228444b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2285f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2286eb1b1807SGleb Smirnoff 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_NOWAIT, 1, MT_DATA);
2287f8829a4aSRandall Stewart 		if (m) {
2288f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2289f8829a4aSRandall Stewart 			caddr_t *from, *to;
2290f8829a4aSRandall Stewart 
2291f8829a4aSRandall Stewart 			/* get the pointers and copy */
2292f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2293f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2294139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2295f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2296139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2297f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2298f8829a4aSRandall Stewart 			/* sucess, back copy */
2299f8829a4aSRandall Stewart 			*mm = m;
2300f8829a4aSRandall Stewart 		} else {
2301f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2302f8829a4aSRandall Stewart 			m = *mm;
2303f8829a4aSRandall Stewart 		}
2304f8829a4aSRandall Stewart 	}
2305f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2306f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2307f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2308f8829a4aSRandall Stewart 	if (ch == NULL) {
2309f8829a4aSRandall Stewart 		return (1);
2310f8829a4aSRandall Stewart 	}
2311f8829a4aSRandall Stewart 	/*
2312f8829a4aSRandall Stewart 	 * process all DATA chunks...
2313f8829a4aSRandall Stewart 	 */
2314f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2315f8829a4aSRandall Stewart 	break_flag = 0;
231642551e99SRandall Stewart 	asoc->data_pkts_seen++;
2317f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2318f8829a4aSRandall Stewart 		/* validate chunk length */
2319f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2320f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2321f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2322f8829a4aSRandall Stewart 			stop_proc = 1;
232360990c0cSMichael Tuexen 			continue;
2324f8829a4aSRandall Stewart 		}
2325f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2326*32451da4SMichael Tuexen 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk)) {
2327f8829a4aSRandall Stewart 				/*
2328f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2329f8829a4aSRandall Stewart 				 * invalid data chunk.
2330f8829a4aSRandall Stewart 				 */
2331f8829a4aSRandall Stewart 				struct mbuf *op_err;
2332ff1ffd74SMichael Tuexen 				char msg[SCTP_DIAG_INFO_LEN];
2333f8829a4aSRandall Stewart 
2334ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "DATA chunk of length %d",
2335ff1ffd74SMichael Tuexen 				    chk_length);
2336ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
2337a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2338b1754ad1SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
2339b1754ad1SMichael Tuexen 				    src, dst, sh, op_err,
2340f30ac432SMichael Tuexen 				    use_mflowid, mflowid,
2341f30ac432SMichael Tuexen 				    vrf_id, port);
2342f8829a4aSRandall Stewart 				return (2);
2343f8829a4aSRandall Stewart 			}
2344*32451da4SMichael Tuexen 			if ((size_t)chk_length == sizeof(struct sctp_data_chunk)) {
2345*32451da4SMichael Tuexen 				/*
2346*32451da4SMichael Tuexen 				 * Need to send an abort since we had an
2347*32451da4SMichael Tuexen 				 * empty data chunk.
2348*32451da4SMichael Tuexen 				 */
2349*32451da4SMichael Tuexen 				struct mbuf *op_err;
2350*32451da4SMichael Tuexen 
2351*32451da4SMichael Tuexen 				op_err = sctp_generate_no_user_data_cause(ch->dp.tsn);
2352*32451da4SMichael Tuexen 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2353*32451da4SMichael Tuexen 				sctp_abort_association(inp, stcb, m, iphlen,
2354*32451da4SMichael Tuexen 				    src, dst, sh, op_err,
2355*32451da4SMichael Tuexen 				    use_mflowid, mflowid,
2356*32451da4SMichael Tuexen 				    vrf_id, port);
2357*32451da4SMichael Tuexen 				return (2);
2358*32451da4SMichael Tuexen 			}
2359f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2360f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2361f8829a4aSRandall Stewart #endif
2362f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2363f8829a4aSRandall Stewart 				last_chunk = 1;
2364f8829a4aSRandall Stewart 			} else {
2365f8829a4aSRandall Stewart 				last_chunk = 0;
2366f8829a4aSRandall Stewart 			}
2367f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2368f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2369f8829a4aSRandall Stewart 			    last_chunk)) {
2370f8829a4aSRandall Stewart 				num_chunks++;
2371f8829a4aSRandall Stewart 			}
2372f8829a4aSRandall Stewart 			if (abort_flag)
2373f8829a4aSRandall Stewart 				return (2);
2374f8829a4aSRandall Stewart 
2375f8829a4aSRandall Stewart 			if (break_flag) {
2376f8829a4aSRandall Stewart 				/*
2377f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2378f8829a4aSRandall Stewart 				 * drop rep space left.
2379f8829a4aSRandall Stewart 				 */
2380f8829a4aSRandall Stewart 				stop_proc = 1;
238160990c0cSMichael Tuexen 				continue;
2382f8829a4aSRandall Stewart 			}
2383f8829a4aSRandall Stewart 		} else {
2384f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2385f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2386f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2387f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2388f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
238960990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2390f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2391f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2392f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2393f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2394f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2395f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2396f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2397f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2398f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2399f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2400f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2401f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2402f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2403f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2404f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2405f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2406f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2407f8829a4aSRandall Stewart 				/*
2408f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2409f8829a4aSRandall Stewart 				 * are NOT in the right place?
2410f8829a4aSRandall Stewart 				 *
2411f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2412f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2413f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2414f8829a4aSRandall Stewart 				 * possibly process them.
2415f8829a4aSRandall Stewart 				 */
2416b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2417f8829a4aSRandall Stewart 					struct mbuf *op_err;
2418f8829a4aSRandall Stewart 
2419ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
2420f30ac432SMichael Tuexen 					sctp_abort_association(inp, stcb,
2421f30ac432SMichael Tuexen 					    m, iphlen,
2422b1754ad1SMichael Tuexen 					    src, dst,
2423f30ac432SMichael Tuexen 					    sh, op_err,
2424f30ac432SMichael Tuexen 					    use_mflowid, mflowid,
2425f30ac432SMichael Tuexen 					    vrf_id, port);
2426f8829a4aSRandall Stewart 					return (2);
2427f8829a4aSRandall Stewart 				}
2428f8829a4aSRandall Stewart 				break;
2429f8829a4aSRandall Stewart 			default:
2430f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2431f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2432f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2433d61a0ae0SRandall Stewart 					struct mbuf *merr;
2434f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2435f8829a4aSRandall Stewart 
2436eb1b1807SGleb Smirnoff 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_NOWAIT, 1, MT_DATA);
2437d61a0ae0SRandall Stewart 					if (merr) {
2438d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2439f8829a4aSRandall Stewart 						/*
2440f8829a4aSRandall Stewart 						 * We cheat and use param
2441f8829a4aSRandall Stewart 						 * type since we did not
2442f8829a4aSRandall Stewart 						 * bother to define a error
2443f8829a4aSRandall Stewart 						 * cause struct. They are
2444f8829a4aSRandall Stewart 						 * the same basic format
2445f8829a4aSRandall Stewart 						 * with different names.
2446f8829a4aSRandall Stewart 						 */
2447f8829a4aSRandall Stewart 						phd->param_type =
2448f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2449f8829a4aSRandall Stewart 						phd->param_length =
2450f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2451d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2452eb1b1807SGleb Smirnoff 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
2453d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2454921569e2SMichael Tuexen 							if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
2455921569e2SMichael Tuexen 								sctp_m_freem(merr);
2456921569e2SMichael Tuexen 							} else {
2457d61a0ae0SRandall Stewart 								sctp_queue_op_err(stcb, merr);
2458921569e2SMichael Tuexen 							}
2459f8829a4aSRandall Stewart 						} else {
2460d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2461f8829a4aSRandall Stewart 						}
2462f8829a4aSRandall Stewart 					}
2463f8829a4aSRandall Stewart 				}
2464f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2465f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2466f8829a4aSRandall Stewart 					stop_proc = 1;
2467f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2468f8829a4aSRandall Stewart 					 * continue... */
2469f8829a4aSRandall Stewart 				break;
247060990c0cSMichael Tuexen 			}	/* switch of chunk type */
2471f8829a4aSRandall Stewart 		}
2472f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2473f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2474f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2475f8829a4aSRandall Stewart 			stop_proc = 1;
2476f8829a4aSRandall Stewart 			continue;
2477f8829a4aSRandall Stewart 		}
2478f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2479f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2480f8829a4aSRandall Stewart 		if (ch == NULL) {
2481f8829a4aSRandall Stewart 			*offset = length;
2482f8829a4aSRandall Stewart 			stop_proc = 1;
248360990c0cSMichael Tuexen 			continue;
2484f8829a4aSRandall Stewart 		}
248560990c0cSMichael Tuexen 	}
2486f8829a4aSRandall Stewart 	if (break_flag) {
2487f8829a4aSRandall Stewart 		/*
2488f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2489f8829a4aSRandall Stewart 		 */
249020cc2188SMichael Tuexen 		sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2491f8829a4aSRandall Stewart 	}
2492f8829a4aSRandall Stewart 	if (num_chunks) {
2493f8829a4aSRandall Stewart 		/*
2494ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2495f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2496f8829a4aSRandall Stewart 		 */
2497f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2498b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2499c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2500c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2501c4739e2fSRandall Stewart 			    0,
2502c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2503c4739e2fSRandall Stewart 			    __LINE__);
2504c4739e2fSRandall Stewart 		}
2505f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
25066e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2507f8829a4aSRandall Stewart 	}
2508f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2509f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2510f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2511f8829a4aSRandall Stewart 
2512f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
251342551e99SRandall Stewart 		/* Assure that we ack right away */
251442551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2515f8829a4aSRandall Stewart 	}
2516f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
25177215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2518f8829a4aSRandall Stewart 	return (0);
2519f8829a4aSRandall Stewart }
2520f8829a4aSRandall Stewart 
25210fa753b3SRandall Stewart static int
25220fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
25230fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
25240fa753b3SRandall Stewart     int *num_frs,
25250fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
25260fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
25277215cc1bSMichael Tuexen     int *rto_ok)
25280fa753b3SRandall Stewart {
25290fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
25300fa753b3SRandall Stewart 	unsigned int theTSN;
2531b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
25320fa753b3SRandall Stewart 
25330fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
25340fa753b3SRandall Stewart 	tp1 = *p_tp1;
25350fa753b3SRandall Stewart 	if (tp1 == NULL) {
25360fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
25370fa753b3SRandall Stewart 	}
25380fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
25390fa753b3SRandall Stewart 		theTSN = j + last_tsn;
25400fa753b3SRandall Stewart 		while (tp1) {
25410fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
25420fa753b3SRandall Stewart 				(*num_frs) += 1;
25430fa753b3SRandall Stewart 
25440fa753b3SRandall Stewart 			/*-
25450fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
25460fa753b3SRandall Stewart 			 * processed from the sent queue, track the
25470fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
25480fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
25490fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
25500fa753b3SRandall Stewart 			 * and retransmissions.
25510fa753b3SRandall Stewart 			 */
25520fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
25530fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
25540fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
25550fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
25560fa753b3SRandall Stewart 			}
25570fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
25580fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
25590fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
25600fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
25610fa753b3SRandall Stewart 			}
25620fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
25630fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
25640fa753b3SRandall Stewart 					/*-
25650fa753b3SRandall Stewart 					 * must be held until
25660fa753b3SRandall Stewart 					 * cum-ack passes
25670fa753b3SRandall Stewart 					 */
25680fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
25690fa753b3SRandall Stewart 						/*-
25700fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
25710fa753b3SRandall Stewart 						 * now no-longer in flight.
25720fa753b3SRandall Stewart 						 * Higher values may already be set
25730fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
25740fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
25750fa753b3SRandall Stewart 						 */
257620b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
257720b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
25780fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
25790fa753b3SRandall Stewart 						}
25800fa753b3SRandall Stewart 						/*-
25810fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
25820fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
25830fa753b3SRandall Stewart 						 * newly acked. update
25840fa753b3SRandall Stewart 						 * this_sack_highest_newack if
25850fa753b3SRandall Stewart 						 * appropriate.
25860fa753b3SRandall Stewart 						 */
25870fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
25880fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
25890fa753b3SRandall Stewart 
259020b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
259120b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
25920fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
25930fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
25940fa753b3SRandall Stewart 						}
25950fa753b3SRandall Stewart 						/*-
25960fa753b3SRandall Stewart 						 * CMT DAC algo: also update
25970fa753b3SRandall Stewart 						 * this_sack_lowest_newack
25980fa753b3SRandall Stewart 						 */
25990fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
26000fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
26010fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
26020fa753b3SRandall Stewart 								    last_tsn,
26030fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
26040fa753b3SRandall Stewart 								    0,
26050fa753b3SRandall Stewart 								    0,
26060fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
26070fa753b3SRandall Stewart 							}
26080fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
26090fa753b3SRandall Stewart 						}
26100fa753b3SRandall Stewart 						/*-
26110fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
26120fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
26130fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
26140fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
26150fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
26160fa753b3SRandall Stewart 						 * retransmissions.
26170fa753b3SRandall Stewart 						 */
26180fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
26190fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
26200fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
26210fa753b3SRandall Stewart 							}
26220fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
26230fa753b3SRandall Stewart 						}
26240fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
26250fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
26260fa753b3SRandall Stewart 						}
26270fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
26280fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
26290fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
26300fa753b3SRandall Stewart 							}
26310fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
26320fa753b3SRandall Stewart 						}
26330fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
26340fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
26350fa753b3SRandall Stewart 							    last_tsn,
26360fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
26370fa753b3SRandall Stewart 							    frag_strt,
26380fa753b3SRandall Stewart 							    frag_end,
26390fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
26400fa753b3SRandall Stewart 						}
26410fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
26420fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
26430fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
26440fa753b3SRandall Stewart 							    tp1->book_size,
26450fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
26460fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
26470fa753b3SRandall Stewart 						}
26480fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2649299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2650299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2651299108c5SRandall Stewart 							    tp1);
2652299108c5SRandall Stewart 						}
26530fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
26540fa753b3SRandall Stewart 
26550fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
26560fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
26570fa753b3SRandall Stewart 							/*-
26580fa753b3SRandall Stewart 							 * True non-retransmited chunk
26590fa753b3SRandall Stewart 							 */
26600fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
26610fa753b3SRandall Stewart 
26620fa753b3SRandall Stewart 							/*-
26630fa753b3SRandall Stewart 							 * update RTO too ?
26640fa753b3SRandall Stewart 							 */
26650fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2666f79aab18SRandall Stewart 								if (*rto_ok) {
26670fa753b3SRandall Stewart 									tp1->whoTo->RTO =
26680fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
26690fa753b3SRandall Stewart 									    &stcb->asoc,
26700fa753b3SRandall Stewart 									    tp1->whoTo,
26710fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2672899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2673f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2674f79aab18SRandall Stewart 									*rto_ok = 0;
2675f79aab18SRandall Stewart 								}
2676f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2677f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2678f79aab18SRandall Stewart 								}
26790fa753b3SRandall Stewart 								tp1->do_rtt = 0;
26800fa753b3SRandall Stewart 							}
26810fa753b3SRandall Stewart 						}
26820fa753b3SRandall Stewart 					}
26830fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
268420b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
268520b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
26860fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
26870fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
26880fa753b3SRandall Stewart 						}
26890fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
26900fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
26910fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
26920fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
26930fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
26940fa753b3SRandall Stewart #endif
26950fa753b3SRandall Stewart 						}
26960fa753b3SRandall Stewart 					}
26970fa753b3SRandall Stewart 					/*-
26980fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
26990fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
27000fa753b3SRandall Stewart 					 */
27012a498584SMichael Tuexen 					if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
2702325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
27030fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
27042a498584SMichael Tuexen 					}
27050fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
27060fa753b3SRandall Stewart 						/* deflate the cwnd */
27070fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
27080fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
27090fa753b3SRandall Stewart 					}
27100fa753b3SRandall Stewart 					/* NR Sack code here */
2711325c8c46SMichael Tuexen 					if (nr_sacking &&
2712325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2713325c8c46SMichael Tuexen 						if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
2714325c8c46SMichael Tuexen 							stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--;
2715325c8c46SMichael Tuexen #ifdef INVARIANTS
2716325c8c46SMichael Tuexen 						} else {
2717325c8c46SMichael Tuexen 							panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
2718325c8c46SMichael Tuexen #endif
2719325c8c46SMichael Tuexen 						}
2720325c8c46SMichael Tuexen 						tp1->sent = SCTP_DATAGRAM_NR_ACKED;
27210fa753b3SRandall Stewart 						if (tp1->data) {
27220fa753b3SRandall Stewart 							/*
27230fa753b3SRandall Stewart 							 * sa_ignore
27240fa753b3SRandall Stewart 							 * NO_NULL_CHK
27250fa753b3SRandall Stewart 							 */
27260fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
27270fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
27280fa753b3SRandall Stewart 							tp1->data = NULL;
2729b5c16493SMichael Tuexen 						}
27300fa753b3SRandall Stewart 						wake_him++;
27310fa753b3SRandall Stewart 					}
27320fa753b3SRandall Stewart 				}
27330fa753b3SRandall Stewart 				break;
27340fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
273520b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
27360fa753b3SRandall Stewart 				break;
273720b07a4dSMichael Tuexen 			}
27380fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
2739b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
2740b5c16493SMichael Tuexen 				circled++;
27410fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
27420fa753b3SRandall Stewart 			}
2743b5c16493SMichael Tuexen 		}		/* end while (tp1) */
2744b5c16493SMichael Tuexen 		if (tp1 == NULL) {
2745b5c16493SMichael Tuexen 			circled = 0;
2746b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2747b5c16493SMichael Tuexen 		}
2748b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
27490fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
27500fa753b3SRandall Stewart 	*p_tp1 = tp1;
27510fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
27520fa753b3SRandall Stewart }
27530fa753b3SRandall Stewart 
27540fa753b3SRandall Stewart 
2755cd554309SMichael Tuexen static int
2756458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2757cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2758139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
27597215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
2760f8829a4aSRandall Stewart {
2761458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
2762f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
27630fa753b3SRandall Stewart 	int i;
2764f8829a4aSRandall Stewart 	int num_frs = 0;
2765cd554309SMichael Tuexen 	int chunk_freed;
2766cd554309SMichael Tuexen 	int non_revocable;
2767d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
2768f8829a4aSRandall Stewart 
2769d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
2770d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
2771cd554309SMichael Tuexen 	chunk_freed = 0;
2772f8829a4aSRandall Stewart 
2773cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
2774d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
2775d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
2776d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2777d9c5cfeaSMichael Tuexen 		}
2778458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2779458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2780458303daSRandall Stewart 		*offset += sizeof(block);
2781458303daSRandall Stewart 		if (frag == NULL) {
2782cd554309SMichael Tuexen 			return (chunk_freed);
2783458303daSRandall Stewart 		}
2784f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
2785f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
2786d9c5cfeaSMichael Tuexen 
2787f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
2788d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
2789f8829a4aSRandall Stewart 			continue;
2790f8829a4aSRandall Stewart 		}
2791d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
2792d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
2793f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2794f8829a4aSRandall Stewart 		}
279520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
2796d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
2797f8829a4aSRandall Stewart 		}
2798cd554309SMichael Tuexen 		if (i < num_seg) {
2799cd554309SMichael Tuexen 			non_revocable = 0;
2800cd554309SMichael Tuexen 		} else {
2801cd554309SMichael Tuexen 			non_revocable = 1;
2802cd554309SMichael Tuexen 		}
2803cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
2804cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
28057215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
2806cd554309SMichael Tuexen 			chunk_freed = 1;
2807458303daSRandall Stewart 		}
2808d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
2809f8829a4aSRandall Stewart 	}
2810b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
281180fefe0aSRandall Stewart 		if (num_frs)
281280fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
281380fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
281480fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
281580fefe0aSRandall Stewart 	}
2816cd554309SMichael Tuexen 	return (chunk_freed);
2817f8829a4aSRandall Stewart }
2818f8829a4aSRandall Stewart 
2819f8829a4aSRandall Stewart static void
2820c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
2821c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
282263eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
2823f8829a4aSRandall Stewart {
2824f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
2825f8829a4aSRandall Stewart 
28264a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
282720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
2828f8829a4aSRandall Stewart 			/*
2829f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
2830f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
2831f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
2832f8829a4aSRandall Stewart 			 * again.
2833f8829a4aSRandall Stewart 			 */
283420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
2835d06c82f1SRandall Stewart 				break;
283620b07a4dSMichael Tuexen 			}
2837f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
2838f8829a4aSRandall Stewart 				/* it has been revoked */
2839f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
2840f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
2841a5d547adSRandall Stewart 				/*
284242551e99SRandall Stewart 				 * We must add this stuff back in to assure
284342551e99SRandall Stewart 				 * timers and such get started.
2844a5d547adSRandall Stewart 				 */
2845b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2846c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
2847c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
2848c105859eSRandall Stewart 					    tp1->book_size,
2849c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
2850c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
285180fefe0aSRandall Stewart 				}
2852c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
2853c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
285442551e99SRandall Stewart 				/*
285542551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
285642551e99SRandall Stewart 				 * artificial inflation of the flight_size.
285742551e99SRandall Stewart 				 */
285842551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
2859b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2860f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
2861f8829a4aSRandall Stewart 					    cumack,
2862f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
2863f8829a4aSRandall Stewart 					    0,
2864f8829a4aSRandall Stewart 					    0,
2865f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
286680fefe0aSRandall Stewart 				}
2867f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
2868f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
2869f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
2870f8829a4aSRandall Stewart 			}
2871f8829a4aSRandall Stewart 		}
2872f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
2873f8829a4aSRandall Stewart 			break;
2874f8829a4aSRandall Stewart 	}
2875f8829a4aSRandall Stewart }
2876f8829a4aSRandall Stewart 
2877830d754dSRandall Stewart 
2878f8829a4aSRandall Stewart static void
2879f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
288063eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
2881f8829a4aSRandall Stewart {
2882f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
2883f8829a4aSRandall Stewart 	int strike_flag = 0;
2884f8829a4aSRandall Stewart 	struct timeval now;
2885f8829a4aSRandall Stewart 	int tot_retrans = 0;
2886f8829a4aSRandall Stewart 	uint32_t sending_seq;
2887f8829a4aSRandall Stewart 	struct sctp_nets *net;
2888f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
2889f8829a4aSRandall Stewart 
2890f8829a4aSRandall Stewart 	/*
2891f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
2892f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
2893f8829a4aSRandall Stewart 	 */
2894f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
2895f8829a4aSRandall Stewart 	if (tp1 == NULL) {
2896f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
2897f8829a4aSRandall Stewart 	} else {
2898f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
2899f8829a4aSRandall Stewart 	}
2900f8829a4aSRandall Stewart 
2901f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
29027c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
290320083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
2904f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2905f8829a4aSRandall Stewart 			if (net->saw_newack)
2906f8829a4aSRandall Stewart 				num_dests_sacked++;
2907f8829a4aSRandall Stewart 		}
2908f8829a4aSRandall Stewart 	}
2909f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
29106e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2911f8829a4aSRandall Stewart 	}
29124a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
2913f8829a4aSRandall Stewart 		strike_flag = 0;
2914f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
2915f8829a4aSRandall Stewart 			/* this one had a timeout or something */
2916f8829a4aSRandall Stewart 			continue;
2917f8829a4aSRandall Stewart 		}
2918b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2919f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
2920f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
2921f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
2922f8829a4aSRandall Stewart 				    tp1->sent,
2923f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
292480fefe0aSRandall Stewart 		}
292520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
2926f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
2927f8829a4aSRandall Stewart 			/* done */
2928f8829a4aSRandall Stewart 			break;
2929f8829a4aSRandall Stewart 		}
2930f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
2931f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
2932f8829a4aSRandall Stewart 				/* Is it expired? */
293399ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
2934f8829a4aSRandall Stewart 					/* Yes so drop it */
2935f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
29361edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
29370c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
2938f8829a4aSRandall Stewart 					}
2939f8829a4aSRandall Stewart 					continue;
2940f8829a4aSRandall Stewart 				}
2941f8829a4aSRandall Stewart 			}
2942f8829a4aSRandall Stewart 		}
294320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
2944f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
2945f8829a4aSRandall Stewart 			break;
2946f8829a4aSRandall Stewart 		}
2947f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
2948f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
2949f8829a4aSRandall Stewart 			/* skip */
295044fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
295144fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
295244fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
295344fbe462SRandall Stewart 			}
2954f8829a4aSRandall Stewart 			continue;
2955f8829a4aSRandall Stewart 		}
2956f8829a4aSRandall Stewart 		/*
2957f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
2958f8829a4aSRandall Stewart 		 */
2959ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
2960f8829a4aSRandall Stewart 			/*
2961f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
2962f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
2963f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
2964c105859eSRandall Stewart 			 * FR using this SACK.
2965f8829a4aSRandall Stewart 			 */
2966f8829a4aSRandall Stewart 			continue;
296720b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
296820b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
2969f8829a4aSRandall Stewart 			/*
2970f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
2971f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
2972f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
2973f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
2974f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
2975f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
2976f8829a4aSRandall Stewart 			 */
2977f8829a4aSRandall Stewart 			continue;
2978f8829a4aSRandall Stewart 		}
2979f8829a4aSRandall Stewart 		/*
2980f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
2981f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
2982f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
2983f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
2984f8829a4aSRandall Stewart 		 */
2985f8829a4aSRandall Stewart 		/*
298642551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
298742551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
298842551e99SRandall Stewart 		 * 0)) {
2989f8829a4aSRandall Stewart 		 */
299042551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
2991f8829a4aSRandall Stewart 			/*
2992f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
2993f8829a4aSRandall Stewart 			 * moved.
2994f8829a4aSRandall Stewart 			 */
2995b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2996f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
2997f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
2998f8829a4aSRandall Stewart 				    tp1->sent,
2999f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
300080fefe0aSRandall Stewart 			}
30015e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3002f8829a4aSRandall Stewart 				tp1->sent++;
30035e54f665SRandall Stewart 			}
30047c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
300520083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3006f8829a4aSRandall Stewart 				/*
3007f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3008f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3009f8829a4aSRandall Stewart 				 * because it would have been set to the
3010f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3011f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3012f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3013c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3014c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3015c105859eSRandall Stewart 				 * indicates that two packets have been
3016c105859eSRandall Stewart 				 * received after this missing TSN.
30175e54f665SRandall Stewart 				 */
30185e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
301920b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3020b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3021f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3022f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3023f8829a4aSRandall Stewart 						    tp1->sent,
3024f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
302580fefe0aSRandall Stewart 					}
3026f8829a4aSRandall Stewart 					tp1->sent++;
3027f8829a4aSRandall Stewart 				}
3028f8829a4aSRandall Stewart 			}
302920083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
303020083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3031f8829a4aSRandall Stewart 			/*
3032f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3033f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3034f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3035f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3036f8829a4aSRandall Stewart 			 */
30375e54f665SRandall Stewart 			if (
3038f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3039f8829a4aSRandall Stewart 			/*
3040f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3041f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3042f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3043f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3044f8829a4aSRandall Stewart 			 */
30455e54f665SRandall Stewart 			    (asoc->numnets < 2)
3046f8829a4aSRandall Stewart #else
30475e54f665SRandall Stewart 			    (1)
3048f8829a4aSRandall Stewart #endif
30495e54f665SRandall Stewart 			    ) {
30505e54f665SRandall Stewart 
305120b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3052f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3053f8829a4aSRandall Stewart 					/*
3054f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3055f8829a4aSRandall Stewart 					 * beyond where things were when we
3056f8829a4aSRandall Stewart 					 * did a FR.
3057f8829a4aSRandall Stewart 					 */
3058b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3059f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3060f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3061f8829a4aSRandall Stewart 						    tp1->sent,
3062f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
306380fefe0aSRandall Stewart 					}
30645e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3065f8829a4aSRandall Stewart 						tp1->sent++;
30665e54f665SRandall Stewart 					}
3067f8829a4aSRandall Stewart 					strike_flag = 1;
30687c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
306920083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3070f8829a4aSRandall Stewart 						/*
3071f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3072f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3073f8829a4aSRandall Stewart 						 * then lowest_newack test
3074f8829a4aSRandall Stewart 						 * will not pass because it
3075f8829a4aSRandall Stewart 						 * would have been set to
3076f8829a4aSRandall Stewart 						 * the cumack earlier. If
3077f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3078f8829a4aSRandall Stewart 						 * If not a mixed sack and
3079f8829a4aSRandall Stewart 						 * if tp1 is not between two
3080f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3081c105859eSRandall Stewart 						 * one more. NOTE that we
3082c105859eSRandall Stewart 						 * are marking by one
3083c105859eSRandall Stewart 						 * additional time since the
3084c105859eSRandall Stewart 						 * SACK DAC flag indicates
3085c105859eSRandall Stewart 						 * that two packets have
3086c105859eSRandall Stewart 						 * been received after this
3087c105859eSRandall Stewart 						 * missing TSN.
3088f8829a4aSRandall Stewart 						 */
30895e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
30905e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
309120b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
309220b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3093b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3094f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3095f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3096f8829a4aSRandall Stewart 								    tp1->sent,
3097f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
309880fefe0aSRandall Stewart 							}
30995e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3100f8829a4aSRandall Stewart 								tp1->sent++;
31015e54f665SRandall Stewart 							}
3102f8829a4aSRandall Stewart 						}
3103f8829a4aSRandall Stewart 					}
3104f8829a4aSRandall Stewart 				}
3105f8829a4aSRandall Stewart 			}
3106f8829a4aSRandall Stewart 			/*
310742551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
310842551e99SRandall Stewart 			 * algo covers HTNA.
3109f8829a4aSRandall Stewart 			 */
311020b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
311120b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3112f8829a4aSRandall Stewart 			/*
3113f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3114f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3115f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3116f8829a4aSRandall Stewart 			 */
3117f8829a4aSRandall Stewart 			;
3118f8829a4aSRandall Stewart 		} else {
3119f8829a4aSRandall Stewart 			/* Strike the TSN */
3120b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3121f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3122f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3123f8829a4aSRandall Stewart 				    tp1->sent,
3124f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
312580fefe0aSRandall Stewart 			}
31265e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3127f8829a4aSRandall Stewart 				tp1->sent++;
31285e54f665SRandall Stewart 			}
31297c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
313020083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3131f8829a4aSRandall Stewart 				/*
3132f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3133f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3134f8829a4aSRandall Stewart 				 * because it would have been set to the
3135f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3136f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3137f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3138c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3139c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3140c105859eSRandall Stewart 				 * indicates that two packets have been
3141c105859eSRandall Stewart 				 * received after this missing TSN.
3142f8829a4aSRandall Stewart 				 */
31435e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
314420b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3145b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3146f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3147f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3148f8829a4aSRandall Stewart 						    tp1->sent,
3149f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
315080fefe0aSRandall Stewart 					}
3151f8829a4aSRandall Stewart 					tp1->sent++;
3152f8829a4aSRandall Stewart 				}
3153f8829a4aSRandall Stewart 			}
3154f8829a4aSRandall Stewart 		}
3155f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3156f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3157f8829a4aSRandall Stewart 
3158544e35bdSRandall Stewart 			/* fix counts and things */
3159544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3160544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3161544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3162544e35bdSRandall Stewart 				    tp1->book_size,
3163544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3164544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3165544e35bdSRandall Stewart 			}
3166544e35bdSRandall Stewart 			if (tp1->whoTo) {
3167544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3168544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3169299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3170299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3171299108c5SRandall Stewart 					    tp1);
3172299108c5SRandall Stewart 				}
3173544e35bdSRandall Stewart 			}
3174544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3175544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3176544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3177544e35bdSRandall Stewart 			}
3178544e35bdSRandall Stewart 			/* add back to the rwnd */
3179544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3180544e35bdSRandall Stewart 
3181544e35bdSRandall Stewart 			/* remove from the total flight */
3182544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3183544e35bdSRandall Stewart 
3184475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3185475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3186475d0674SMichael Tuexen 				/*
3187475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3188475d0674SMichael Tuexen 				 * we store the retran count there.
3189475d0674SMichael Tuexen 				 */
3190475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3191475d0674SMichael Tuexen 					/* Yes, so drop it */
3192475d0674SMichael Tuexen 					if (tp1->data != NULL) {
31931edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3194475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3195475d0674SMichael Tuexen 					}
3196475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3197475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3198475d0674SMichael Tuexen 					continue;
3199475d0674SMichael Tuexen 				}
3200475d0674SMichael Tuexen 			}
3201cd3fd531SMichael Tuexen 			/*
3202cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3203cd3fd531SMichael Tuexen 			 * guy\n");
3204cd3fd531SMichael Tuexen 			 */
3205b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3206f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3207f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
320880fefe0aSRandall Stewart 			}
3209f8829a4aSRandall Stewart 			if (strike_flag) {
3210f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3211f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3212f8829a4aSRandall Stewart 			}
32135e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
32147c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3215f8829a4aSRandall Stewart 				/*
3216f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3217f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3218f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3219f8829a4aSRandall Stewart 				 */
3220f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3221f8829a4aSRandall Stewart 				alt = tp1->whoTo;
32223c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
322320083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3224b54d3a6cSRandall Stewart 					/*
3225b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3226b54d3a6cSRandall Stewart 					 * use the PF version of
3227b54d3a6cSRandall Stewart 					 * find_alt_net()
3228b54d3a6cSRandall Stewart 					 */
3229b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3230b54d3a6cSRandall Stewart 				} else {
3231b54d3a6cSRandall Stewart 					/*
3232b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3233b54d3a6cSRandall Stewart 					 * use the CMT version of
3234b54d3a6cSRandall Stewart 					 * find_alt_net()
3235b54d3a6cSRandall Stewart 					 */
323652be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3237f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3238b54d3a6cSRandall Stewart 				}
3239ad81507eSRandall Stewart 				if (alt == NULL) {
3240ad81507eSRandall Stewart 					alt = tp1->whoTo;
3241ad81507eSRandall Stewart 				}
3242f8829a4aSRandall Stewart 				/*
3243f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3244f8829a4aSRandall Stewart 				 * the retransmission, then new
3245f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3246f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3247f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3248f8829a4aSRandall Stewart 				 */
3249ad81507eSRandall Stewart 				if (tp1->whoTo) {
3250f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3251f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3252ad81507eSRandall Stewart 				}
3253f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3254f8829a4aSRandall Stewart 
3255f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3256f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3257f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3258f8829a4aSRandall Stewart #else
3259f8829a4aSRandall Stewart 				/*
3260f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3261f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3262f8829a4aSRandall Stewart 				 * paper details why.
3263f8829a4aSRandall Stewart 				 */
3264f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3265f8829a4aSRandall Stewart #endif
3266f8829a4aSRandall Stewart 			}
3267f8829a4aSRandall Stewart 
3268f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3269f8829a4aSRandall Stewart 			tot_retrans++;
3270f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3271f8829a4aSRandall Stewart 			/*
3272cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3273f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3274f8829a4aSRandall Stewart 			 */
3275f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3276f8829a4aSRandall Stewart 				/*
3277f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3278f8829a4aSRandall Stewart 				 * the next sequence number that will be
3279f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3280f8829a4aSRandall Stewart 				 * get the one we last sent.
3281f8829a4aSRandall Stewart 				 */
3282f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3283f8829a4aSRandall Stewart 			} else {
3284f8829a4aSRandall Stewart 				/*
3285f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3286f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3287f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3288f8829a4aSRandall Stewart 				 * take the first one (which will have the
3289f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3290f8829a4aSRandall Stewart 				 * one we last sent.
3291f8829a4aSRandall Stewart 				 */
3292f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3293f8829a4aSRandall Stewart 
3294f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3295f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3296f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3297f8829a4aSRandall Stewart 			}
3298f8829a4aSRandall Stewart 
3299f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3300f8829a4aSRandall Stewart 				/*
3301f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3302f8829a4aSRandall Stewart 				 * it, cancel it
3303f8829a4aSRandall Stewart 				 */
330460990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
330560990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3306f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3307f79aab18SRandall Stewart 				}
3308f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3309f8829a4aSRandall Stewart 			}
3310f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3311f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3312f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
33133c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3314f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3315f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3316f8829a4aSRandall Stewart 			}
3317f8829a4aSRandall Stewart 		}
33184a9ef3f8SMichael Tuexen 	}
3319f8829a4aSRandall Stewart }
3320f8829a4aSRandall Stewart 
3321f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3322f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3323f8829a4aSRandall Stewart     struct sctp_association *asoc)
3324f8829a4aSRandall Stewart {
3325f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3326f8829a4aSRandall Stewart 	struct timeval now;
3327f8829a4aSRandall Stewart 	int now_filled = 0;
3328f8829a4aSRandall Stewart 
3329f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3330f8829a4aSRandall Stewart 		return (NULL);
3331f8829a4aSRandall Stewart 	}
33324a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3333f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
333498f2956cSMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_RESEND &&
3335325c8c46SMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3336f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3337f8829a4aSRandall Stewart 			break;
3338f8829a4aSRandall Stewart 		}
33390c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
33402a498584SMichael Tuexen 			if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3341325c8c46SMichael Tuexen 			    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
33420c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
33430c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
33440c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
33450c0982b8SRandall Stewart 			}
33460c0982b8SRandall Stewart 		}
3347f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3348f8829a4aSRandall Stewart 			/*
3349f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3350f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3351f8829a4aSRandall Stewart 			 */
3352f8829a4aSRandall Stewart 			break;
3353f8829a4aSRandall Stewart 		}
3354f8829a4aSRandall Stewart 		if (!now_filled) {
33556e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3356f8829a4aSRandall Stewart 			now_filled = 1;
3357f8829a4aSRandall Stewart 		}
3358f8829a4aSRandall Stewart 		/*
3359f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3360f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3361f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3362f8829a4aSRandall Stewart 		 * it if its a resend?
3363f8829a4aSRandall Stewart 		 */
3364f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3365f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3366f8829a4aSRandall Stewart 			/*
3367f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3368f8829a4aSRandall Stewart 			 * now up?
3369f8829a4aSRandall Stewart 			 */
3370f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3371f8829a4aSRandall Stewart 				/* Yes so drop it */
3372f8829a4aSRandall Stewart 				if (tp1->data) {
3373ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
33741edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3375f8829a4aSRandall Stewart 				}
3376f8829a4aSRandall Stewart 			} else {
3377f8829a4aSRandall Stewart 				/*
3378f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3379f8829a4aSRandall Stewart 				 * whos time as not expired.
3380f8829a4aSRandall Stewart 				 */
3381f8829a4aSRandall Stewart 				break;
3382f8829a4aSRandall Stewart 			}
3383f8829a4aSRandall Stewart 		}
3384f8829a4aSRandall Stewart 		/*
3385f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3386f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3387f8829a4aSRandall Stewart 		 * the next chunk.
3388f8829a4aSRandall Stewart 		 */
338998f2956cSMichael Tuexen 		if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3390325c8c46SMichael Tuexen 		    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3391f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
339220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3393f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3394f8829a4aSRandall Stewart 				a_adv = tp1;
33950c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
33960c0982b8SRandall Stewart 				/* No update but we do save the chk */
33970c0982b8SRandall Stewart 				a_adv = tp1;
33980c0982b8SRandall Stewart 			}
3399f8829a4aSRandall Stewart 		} else {
3400f8829a4aSRandall Stewart 			/*
3401f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3402f8829a4aSRandall Stewart 			 * further
3403f8829a4aSRandall Stewart 			 */
3404f8829a4aSRandall Stewart 			break;
3405f8829a4aSRandall Stewart 		}
3406f8829a4aSRandall Stewart 	}
3407f8829a4aSRandall Stewart 	return (a_adv);
3408f8829a4aSRandall Stewart }
3409f8829a4aSRandall Stewart 
34100c0982b8SRandall Stewart static int
3411c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3412bff64a4dSRandall Stewart {
3413bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3414bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
34150c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
34160c0982b8SRandall Stewart 
34170c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
34180c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
34190c0982b8SRandall Stewart 	ret = 0;
34200c0982b8SRandall Stewart 
34210c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
34220c0982b8SRandall Stewart 		return (0);
3423bff64a4dSRandall Stewart 
3424bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3425bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3426cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n",
34270c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
34280c0982b8SRandall Stewart 			    chk->send_size,
3429cd3fd531SMichael Tuexen 			    chk->snd_count);
3430bff64a4dSRandall Stewart 			inflight++;
3431bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3432bff64a4dSRandall Stewart 			resend++;
3433bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3434bff64a4dSRandall Stewart 			inbetween++;
3435bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3436bff64a4dSRandall Stewart 			above++;
3437bff64a4dSRandall Stewart 		} else {
3438bff64a4dSRandall Stewart 			acked++;
3439bff64a4dSRandall Stewart 		}
3440bff64a4dSRandall Stewart 	}
3441f1f73e57SRandall Stewart 
3442c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3443f1f73e57SRandall Stewart #ifdef INVARIANTS
3444c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3445f1f73e57SRandall Stewart #else
3446cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n",
34470c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
34480c0982b8SRandall Stewart 
34490c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
34500c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
34510c0982b8SRandall Stewart 		ret = 1;
3452f1f73e57SRandall Stewart #endif
3453bff64a4dSRandall Stewart 	}
34540c0982b8SRandall Stewart 	return (ret);
3455c105859eSRandall Stewart }
3456c105859eSRandall Stewart 
3457c105859eSRandall Stewart 
3458c105859eSRandall Stewart static void
3459c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3460c105859eSRandall Stewart     struct sctp_association *asoc,
3461c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3462c105859eSRandall Stewart {
3463dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
34645171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3465dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3466dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3467dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3468dfb11ef8SRandall Stewart 		    tp1->book_size,
3469dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3470dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3471dfb11ef8SRandall Stewart 		return;
3472dfb11ef8SRandall Stewart 	}
34735171328bSRandall Stewart 	/* First setup this by shrinking flight */
3474299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3475299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3476299108c5SRandall Stewart 		    tp1);
3477299108c5SRandall Stewart 	}
34785171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
34795171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
34805171328bSRandall Stewart 	/* Now mark for resend */
34815171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3482791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3483791437b5SRandall Stewart 
3484b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3485c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3486c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3487c105859eSRandall Stewart 		    tp1->book_size,
3488c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3489c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
349080fefe0aSRandall Stewart 	}
3491c105859eSRandall Stewart }
3492c105859eSRandall Stewart 
3493f8829a4aSRandall Stewart void
3494f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3495899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3496f8829a4aSRandall Stewart {
3497f8829a4aSRandall Stewart 	struct sctp_nets *net;
3498f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3499f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
35005e54f665SRandall Stewart 	uint32_t old_rwnd;
35015e54f665SRandall Stewart 	int win_probe_recovery = 0;
3502c105859eSRandall Stewart 	int win_probe_recovered = 0;
3503d06c82f1SRandall Stewart 	int j, done_once = 0;
3504f79aab18SRandall Stewart 	int rto_ok = 1;
3505f8829a4aSRandall Stewart 
3506b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3507d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3508d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
350980fefe0aSRandall Stewart 	}
3510f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
351118e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
351218e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
351318e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
351418e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
351518e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
351618e198d3SRandall Stewart 	}
351718e198d3SRandall Stewart #endif
3518f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3519d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
352020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
35215e54f665SRandall Stewart 		/* old ack */
35225e54f665SRandall Stewart 		return;
3523d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3524d06c82f1SRandall Stewart 		/* Window update sack */
3525d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
352644fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3527d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3528d06c82f1SRandall Stewart 			/* SWS sender side engages */
3529d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3530d06c82f1SRandall Stewart 		}
3531d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3532d06c82f1SRandall Stewart 			goto again;
3533d06c82f1SRandall Stewart 		}
3534d06c82f1SRandall Stewart 		return;
35355e54f665SRandall Stewart 	}
3536f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3537f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3538a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3539a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3540a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3541a21779f0SRandall Stewart 		}
3542f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3543f8829a4aSRandall Stewart 		net->net_ack = 0;
3544f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3545132dea7dSRandall Stewart 
3546132dea7dSRandall Stewart 		/*
3547132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3548132dea7dSRandall Stewart 		 * SACK processing
3549132dea7dSRandall Stewart 		 */
3550132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3551132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3552299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3553299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3554299108c5SRandall Stewart 		}
3555f8829a4aSRandall Stewart 	}
3556b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3557139bc87fSRandall Stewart 		uint32_t send_s;
3558139bc87fSRandall Stewart 
3559c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3560c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3561c105859eSRandall Stewart 			    sctpchunk_listhead);
3562c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3563139bc87fSRandall Stewart 		} else {
3564c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3565139bc87fSRandall Stewart 		}
356620b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3567c105859eSRandall Stewart #ifndef INVARIANTS
3568ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
3569ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
3570139bc87fSRandall Stewart 
3571c105859eSRandall Stewart #endif
3572c105859eSRandall Stewart #ifdef INVARIANTS
3573c105859eSRandall Stewart 			panic("Impossible sack 1");
3574c105859eSRandall Stewart #else
3575b5c16493SMichael Tuexen 
3576139bc87fSRandall Stewart 			*abort_now = 1;
3577139bc87fSRandall Stewart 			/* XXX */
3578ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal then TSN %8.8x",
3579ff1ffd74SMichael Tuexen 			    cumack, send_s);
3580ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
3581139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3582ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3583139bc87fSRandall Stewart 			return;
3584139bc87fSRandall Stewart #endif
3585139bc87fSRandall Stewart 		}
3586139bc87fSRandall Stewart 	}
3587f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3588b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3589c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3590c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3591c4739e2fSRandall Stewart 		    0,
3592c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3593c4739e2fSRandall Stewart 		    __LINE__);
3594c4739e2fSRandall Stewart 	}
3595f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
359620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3597f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
35984a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
359920b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
360018e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3601cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
360218e198d3SRandall Stewart 				}
3603f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3604f8829a4aSRandall Stewart 					/*
360518e198d3SRandall Stewart 					 * If it is less than ACKED, it is
360618e198d3SRandall Stewart 					 * now no-longer in flight. Higher
360718e198d3SRandall Stewart 					 * values may occur during marking
3608f8829a4aSRandall Stewart 					 */
3609c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3610b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3611c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3612a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3613a5d547adSRandall Stewart 							    tp1->book_size,
3614c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3615a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
361680fefe0aSRandall Stewart 						}
3617c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3618299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3619299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3620299108c5SRandall Stewart 							    tp1);
3621299108c5SRandall Stewart 						}
362204ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3623c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3624f8829a4aSRandall Stewart 					}
3625f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3626f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3627f8829a4aSRandall Stewart 						/*
362818e198d3SRandall Stewart 						 * True non-retransmited
3629f8829a4aSRandall Stewart 						 * chunk
3630f8829a4aSRandall Stewart 						 */
3631f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3632f8829a4aSRandall Stewart 						    tp1->send_size;
3633f8829a4aSRandall Stewart 
3634f8829a4aSRandall Stewart 						/* update RTO too? */
363562c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3636f79aab18SRandall Stewart 							if (rto_ok) {
3637f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
363804ee05e8SRandall Stewart 								/*
363904ee05e8SRandall Stewart 								 * sa_ignore
3640f79aab18SRandall Stewart 								 * NO_NULL_CH
3641f79aab18SRandall Stewart 								 * K
364204ee05e8SRandall Stewart 								 */
3643f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3644f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
364518e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3646899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3647f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3648f79aab18SRandall Stewart 								rto_ok = 0;
3649f79aab18SRandall Stewart 							}
3650f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3651f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3652f79aab18SRandall Stewart 							}
3653f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3654f8829a4aSRandall Stewart 						}
3655f8829a4aSRandall Stewart 					}
3656132dea7dSRandall Stewart 					/*
365718e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
365818e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
365918e198d3SRandall Stewart 					 * acked for the first time, set the
366018e198d3SRandall Stewart 					 * following variables for the
366118e198d3SRandall Stewart 					 * corresp destination.
366218e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
366318e198d3SRandall Stewart 					 * cwnd update.
366418e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
366518e198d3SRandall Stewart 					 * trigger search for the next
366618e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3667132dea7dSRandall Stewart 					 */
3668132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3669132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3670132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3671132dea7dSRandall Stewart 
3672b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
367304ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3674f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
367580fefe0aSRandall Stewart 					}
3676f8829a4aSRandall Stewart 				}
3677f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3678f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3679f8829a4aSRandall Stewart 				}
368042551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
368142551e99SRandall Stewart 					/* deflate the cwnd */
368242551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
368342551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
368442551e99SRandall Stewart 				}
3685325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3686a7ad6026SMichael Tuexen 					if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
3687a7ad6026SMichael Tuexen 						asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
3688a7ad6026SMichael Tuexen #ifdef INVARIANTS
3689a7ad6026SMichael Tuexen 					} else {
3690a7ad6026SMichael Tuexen 						panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
3691a7ad6026SMichael Tuexen #endif
3692a7ad6026SMichael Tuexen 					}
3693a7ad6026SMichael Tuexen 				}
3694f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3695f8829a4aSRandall Stewart 				if (tp1->data) {
369604ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3697f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3698f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
36994a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3700f8829a4aSRandall Stewart 				}
3701b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3702f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3703f8829a4aSRandall Stewart 					    cumack,
3704f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3705f8829a4aSRandall Stewart 					    0,
3706f8829a4aSRandall Stewart 					    0,
3707f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
370880fefe0aSRandall Stewart 				}
3709f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3710689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
371118e198d3SRandall Stewart 			} else {
371218e198d3SRandall Stewart 				break;
3713f8829a4aSRandall Stewart 			}
37145e54f665SRandall Stewart 		}
371518e198d3SRandall Stewart 
371618e198d3SRandall Stewart 	}
371704ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3718f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3719ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3720ceaad40aSRandall Stewart 		struct socket *so;
3721ceaad40aSRandall Stewart 
3722ceaad40aSRandall Stewart #endif
3723f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3724b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
372504ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
37267215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
372780fefe0aSRandall Stewart 		}
3728ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3729ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3730ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3731ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3732ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3733ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3734ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3735ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3736ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
3737ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3738ceaad40aSRandall Stewart 			return;
3739ceaad40aSRandall Stewart 		}
3740ceaad40aSRandall Stewart #endif
3741f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
3742ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3743ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3744ceaad40aSRandall Stewart #endif
3745f8829a4aSRandall Stewart 	} else {
3746b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
37477215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
374880fefe0aSRandall Stewart 		}
3749f8829a4aSRandall Stewart 	}
3750f8829a4aSRandall Stewart 
3751b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
3752ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
3753ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3754ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
3755ca85e948SMichael Tuexen 				/*
3756ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
3757ca85e948SMichael Tuexen 				 * count, this is optional.
3758ca85e948SMichael Tuexen 				 */
3759ca85e948SMichael Tuexen 				net->error_count = 0;
3760ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
3761ca85e948SMichael Tuexen 					/* addr came good */
3762ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
3763ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
37644b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
3765ca85e948SMichael Tuexen 				}
3766ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
3767ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
3768ca85e948SMichael Tuexen 						/*
3769ca85e948SMichael Tuexen 						 * release the alternate,
3770ca85e948SMichael Tuexen 						 * primary is good
3771ca85e948SMichael Tuexen 						 */
3772ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
3773ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
3774ca85e948SMichael Tuexen 					}
3775ca85e948SMichael Tuexen 				}
3776ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
3777ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
3778ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
3779ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
3780ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
3781ca85e948SMichael Tuexen 					/* Done with this net */
3782ca85e948SMichael Tuexen 					net->net_ack = 0;
3783ca85e948SMichael Tuexen 				}
3784ca85e948SMichael Tuexen 				/* restore any doubled timers */
3785ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3786ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
3787ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
3788ca85e948SMichael Tuexen 				}
3789ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
3790ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
3791ca85e948SMichael Tuexen 				}
3792ca85e948SMichael Tuexen 			}
3793ca85e948SMichael Tuexen 		}
3794b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
3795ca85e948SMichael Tuexen 	}
3796f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
37975e54f665SRandall Stewart 
3798f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
3799f8829a4aSRandall Stewart 		/* nothing left in-flight */
3800f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3801f8829a4aSRandall Stewart 			net->flight_size = 0;
3802f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
3803f8829a4aSRandall Stewart 		}
3804f8829a4aSRandall Stewart 		asoc->total_flight = 0;
3805f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
3806f8829a4aSRandall Stewart 	}
3807f8829a4aSRandall Stewart 	/* RWND update */
3808f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
380944fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3810f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3811f8829a4aSRandall Stewart 		/* SWS sender side engages */
3812f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
3813f8829a4aSRandall Stewart 	}
38145e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
38155e54f665SRandall Stewart 		win_probe_recovery = 1;
38165e54f665SRandall Stewart 	}
3817f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
3818a5d547adSRandall Stewart again:
3819a5d547adSRandall Stewart 	j = 0;
3820f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
38215171328bSRandall Stewart 		int to_ticks;
38225171328bSRandall Stewart 
38235e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
3824c105859eSRandall Stewart 			win_probe_recovered = 1;
38255e54f665SRandall Stewart 			/*
38265e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
38275e54f665SRandall Stewart 			 * and clear the sent
38285e54f665SRandall Stewart 			 */
38293c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
38305e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
38315e54f665SRandall Stewart 				if (tp1->window_probe) {
3832cd554309SMichael Tuexen 					/* move back to data send queue */
38337215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
38345e54f665SRandall Stewart 					break;
38355e54f665SRandall Stewart 				}
38365e54f665SRandall Stewart 			}
38375e54f665SRandall Stewart 		}
3838f8829a4aSRandall Stewart 		if (net->RTO == 0) {
3839f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
3840f8829a4aSRandall Stewart 		} else {
3841f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
3842f8829a4aSRandall Stewart 		}
38435171328bSRandall Stewart 		if (net->flight_size) {
3844a5d547adSRandall Stewart 			j++;
3845ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
3846f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
38475171328bSRandall Stewart 			if (net->window_probe) {
38485171328bSRandall Stewart 				net->window_probe = 0;
38495171328bSRandall Stewart 			}
3850f8829a4aSRandall Stewart 		} else {
38515171328bSRandall Stewart 			if (net->window_probe) {
38525171328bSRandall Stewart 				/*
38535171328bSRandall Stewart 				 * In window probes we must assure a timer
38545171328bSRandall Stewart 				 * is still running there
38555171328bSRandall Stewart 				 */
38565171328bSRandall Stewart 				net->window_probe = 0;
38575171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
38585171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
38595171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
38605171328bSRandall Stewart 				}
38615171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3862f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3863a5d547adSRandall Stewart 				    stcb, net,
3864a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
3865f8829a4aSRandall Stewart 			}
3866f8829a4aSRandall Stewart 		}
3867f8829a4aSRandall Stewart 	}
3868bff64a4dSRandall Stewart 	if ((j == 0) &&
3869bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
3870bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
3871c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
3872bff64a4dSRandall Stewart 	    (done_once == 0)) {
38730c0982b8SRandall Stewart 		/*
38740c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
38750c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
38760c0982b8SRandall Stewart 		 */
38770c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
3878a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3879a5d547adSRandall Stewart 				net->flight_size = 0;
3880a5d547adSRandall Stewart 			}
3881a5d547adSRandall Stewart 			asoc->total_flight = 0;
3882a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
3883a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
3884a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3885a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3886c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
3887c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
3888a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3889791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3890a5d547adSRandall Stewart 				}
3891a5d547adSRandall Stewart 			}
38920c0982b8SRandall Stewart 		}
3893bff64a4dSRandall Stewart 		done_once = 1;
3894a5d547adSRandall Stewart 		goto again;
3895a5d547adSRandall Stewart 	}
3896f8829a4aSRandall Stewart 	/**********************************/
3897f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
3898f8829a4aSRandall Stewart 	/**********************************/
3899f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
3900f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
3901f8829a4aSRandall Stewart 		/* clean up */
3902f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
3903f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
3904f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
3905f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
3906f8829a4aSRandall Stewart 		    ) {
3907f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
3908f8829a4aSRandall Stewart 
3909f8829a4aSRandall Stewart 			/*
3910f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
3911f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
3912f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
3913f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
3914f8829a4aSRandall Stewart 			 */
3915f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
3916f8829a4aSRandall Stewart 			    sctp_streamhead);
39172afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
39182afb3e84SRandall Stewart 				/* Let cleanup code purge it */
39192afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
39202afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
39212afb3e84SRandall Stewart 				} else {
3922f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3923f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
3924f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
3925f8829a4aSRandall Stewart 				}
3926f8829a4aSRandall Stewart 			}
39272afb3e84SRandall Stewart 		}
3928f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
3929f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
3930f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
3931f8829a4aSRandall Stewart 				/* Need to abort here */
3932ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
3933f8829a4aSRandall Stewart 
3934f8829a4aSRandall Stewart 		abort_out_now:
3935f8829a4aSRandall Stewart 				*abort_now = 1;
3936f8829a4aSRandall Stewart 				/* XXX */
3937ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3938a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
3939ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3940f8829a4aSRandall Stewart 			} else {
3941ca85e948SMichael Tuexen 				struct sctp_nets *netp;
3942ca85e948SMichael Tuexen 
3943f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3944f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3945f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3946f42a358aSRandall Stewart 				}
3947c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
3948b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
3949f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
3950ca85e948SMichael Tuexen 				if (asoc->alternate) {
3951ca85e948SMichael Tuexen 					netp = asoc->alternate;
3952ca85e948SMichael Tuexen 				} else {
3953ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
3954ca85e948SMichael Tuexen 				}
3955ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
3956f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
3957ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
3958f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
3959ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
3960f8829a4aSRandall Stewart 			}
3961f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
3962f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
3963ca85e948SMichael Tuexen 			struct sctp_nets *netp;
3964ca85e948SMichael Tuexen 
3965f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
3966f8829a4aSRandall Stewart 				goto abort_out_now;
3967f8829a4aSRandall Stewart 			}
3968f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3969c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
3970b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
397112af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
3972c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
3973c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
3974c39cfa1fSMichael Tuexen 			} else {
3975c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
3976c39cfa1fSMichael Tuexen 			}
3977c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
3978f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
3979ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
3980f8829a4aSRandall Stewart 		}
3981f8829a4aSRandall Stewart 	}
3982dfb11ef8SRandall Stewart 	/*********************************************/
3983dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
3984dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
3985dfb11ef8SRandall Stewart 	/*********************************************/
3986dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
398720b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
3988dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
3989dfb11ef8SRandall Stewart 	}
3990830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
3991830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
3992830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
3993830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
3994830d754dSRandall Stewart 
3995830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
3996830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
3997830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
399820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
3999830d754dSRandall Stewart 			/*
4000493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4001830d754dSRandall Stewart 			 */
400220b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4003830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
40040c0982b8SRandall Stewart 			} else if (lchk) {
40050c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
400644fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
40070c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
40080c0982b8SRandall Stewart 				}
4009830d754dSRandall Stewart 			}
4010830d754dSRandall Stewart 		}
4011830d754dSRandall Stewart 		if (lchk) {
4012830d754dSRandall Stewart 			/* Assure a timer is up */
4013830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4014830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4015830d754dSRandall Stewart 		}
4016830d754dSRandall Stewart 	}
4017b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4018f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4019f8829a4aSRandall Stewart 		    rwnd,
4020f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4021f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4022f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
402380fefe0aSRandall Stewart 	}
4024f8829a4aSRandall Stewart }
4025f8829a4aSRandall Stewart 
4026f8829a4aSRandall Stewart void
4027cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
40287215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4029cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4030cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4031899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4032f8829a4aSRandall Stewart {
4033f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4034f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4035cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4036f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4037c105859eSRandall Stewart 	uint32_t send_s = 0;
4038f8829a4aSRandall Stewart 	long j;
4039f8829a4aSRandall Stewart 	int accum_moved = 0;
4040f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
40415e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
40425e54f665SRandall Stewart 	int win_probe_recovery = 0;
4043c105859eSRandall Stewart 	int win_probe_recovered = 0;
4044f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4045bff64a4dSRandall Stewart 	int done_once;
4046f79aab18SRandall Stewart 	int rto_ok = 1;
4047f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4048f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4049f8829a4aSRandall Stewart 
4050f8829a4aSRandall Stewart 	/*
4051f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4052f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4053f8829a4aSRandall Stewart 	 */
4054f8829a4aSRandall Stewart 	/*
4055f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4056f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4057f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4058f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4059f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4060f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4061f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4062f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4063f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4064f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4065f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4066f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4067f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4068f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4069f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4070f8829a4aSRandall Stewart 	 */
4071f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4072f8829a4aSRandall Stewart 	/* CMT DAC algo */
4073f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4074f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4075cd554309SMichael Tuexen 	last_tsn = cum_ack;
4076cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
407718e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
407818e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
407918e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
408018e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
408118e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
408218e198d3SRandall Stewart 	}
408318e198d3SRandall Stewart #endif
4084d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4085f8829a4aSRandall Stewart 
4086cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4087cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4088cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4089cd554309SMichael Tuexen 	}
40905e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4091b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4092c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4093c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4094c4739e2fSRandall Stewart 		    0,
4095c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4096c4739e2fSRandall Stewart 		    __LINE__);
4097c4739e2fSRandall Stewart 	}
4098f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4099f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4100b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4101f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4102f8829a4aSRandall Stewart 		    cum_ack,
4103f8829a4aSRandall Stewart 		    0,
4104f8829a4aSRandall Stewart 		    num_seg,
4105f8829a4aSRandall Stewart 		    num_dup,
4106f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
410780fefe0aSRandall Stewart 	}
4108ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4109cd554309SMichael Tuexen 		uint16_t i;
4110458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4111f8829a4aSRandall Stewart 
4112cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4113cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4114458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4115cd554309SMichael Tuexen 			if (dupdata == NULL) {
4116458303daSRandall Stewart 				break;
4117458303daSRandall Stewart 			}
4118cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4119f8829a4aSRandall Stewart 		}
4120f8829a4aSRandall Stewart 	}
4121b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4122c105859eSRandall Stewart 		/* reality check */
4123c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4124c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4125c105859eSRandall Stewart 			    sctpchunk_listhead);
4126c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4127c105859eSRandall Stewart 		} else {
4128b5c16493SMichael Tuexen 			tp1 = NULL;
4129c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4130c105859eSRandall Stewart 		}
413120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4132ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
4133ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
4134c105859eSRandall Stewart 
4135f8829a4aSRandall Stewart 			/*
4136f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4137f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4138f8829a4aSRandall Stewart 			 */
4139cd3fd531SMichael Tuexen 			SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4140b5c16493SMichael Tuexen 			    cum_ack, send_s);
4141b5c16493SMichael Tuexen 			if (tp1) {
4142cd3fd531SMichael Tuexen 				SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n",
4143dd294dceSMichael Tuexen 				    tp1->rec.data.TSN_seq, (void *)tp1);
4144b5c16493SMichael Tuexen 			}
4145f8829a4aSRandall Stewart 	hopeless_peer:
4146f8829a4aSRandall Stewart 			*abort_now = 1;
4147f8829a4aSRandall Stewart 			/* XXX */
4148ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal then TSN %8.8x",
4149ff1ffd74SMichael Tuexen 			    cum_ack, send_s);
4150ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4151a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4152ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4153f8829a4aSRandall Stewart 			return;
4154f8829a4aSRandall Stewart 		}
4155f8829a4aSRandall Stewart 	}
4156f8829a4aSRandall Stewart 	/**********************/
4157f8829a4aSRandall Stewart 	/* 1) check the range */
4158f8829a4aSRandall Stewart 	/**********************/
415920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4160f8829a4aSRandall Stewart 		/* acking something behind */
4161f8829a4aSRandall Stewart 		return;
4162f8829a4aSRandall Stewart 	}
4163f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4164f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4165f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4166cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4167f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4168b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4169f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4170f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
417180fefe0aSRandall Stewart 		}
4172f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4173f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4174f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4175f8829a4aSRandall Stewart 		}
4176f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4177f8829a4aSRandall Stewart 			/* SWS sender side engages */
4178f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4179f8829a4aSRandall Stewart 		}
4180f8829a4aSRandall Stewart 		/* stop any timers */
4181f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4182f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4183a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4184f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4185f8829a4aSRandall Stewart 			net->flight_size = 0;
4186f8829a4aSRandall Stewart 		}
4187f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4188f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4189f8829a4aSRandall Stewart 		return;
4190f8829a4aSRandall Stewart 	}
4191f8829a4aSRandall Stewart 	/*
4192f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4193f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4194f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4195f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4196f8829a4aSRandall Stewart 	 * destination address basis.
4197f8829a4aSRandall Stewart 	 */
4198f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4199a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4200a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4201a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4202a21779f0SRandall Stewart 		}
4203f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4204f8829a4aSRandall Stewart 		net->net_ack = 0;
4205f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4206f8829a4aSRandall Stewart 
4207f8829a4aSRandall Stewart 		/*
420842551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
420942551e99SRandall Stewart 		 * SACK processing
4210f8829a4aSRandall Stewart 		 */
4211f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4212f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4213299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4214299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4215299108c5SRandall Stewart 		}
4216f8829a4aSRandall Stewart 	}
4217f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
42184a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
421920b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4220f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4221f8829a4aSRandall Stewart 				accum_moved = 1;
4222f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4223f8829a4aSRandall Stewart 					/*
4224f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4225f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4226f8829a4aSRandall Stewart 					 * values may occur during marking
4227f8829a4aSRandall Stewart 					 */
4228f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4229f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4230f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4231f8829a4aSRandall Stewart 						/*
4232f8829a4aSRandall Stewart 						 * If there was no retran
4233f8829a4aSRandall Stewart 						 * and the address is
4234f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4235f8829a4aSRandall Stewart 						 * there and are now
4236f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4237f8829a4aSRandall Stewart 						 * mark it so.
4238f8829a4aSRandall Stewart 						 */
4239f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4240f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4241f8829a4aSRandall Stewart 					}
4242c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4243b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4244c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4245a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4246a5d547adSRandall Stewart 							    tp1->book_size,
4247c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4248a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
424980fefe0aSRandall Stewart 						}
4250c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4251c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4252299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4253299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4254299108c5SRandall Stewart 							    tp1);
4255299108c5SRandall Stewart 						}
4256f8829a4aSRandall Stewart 					}
4257f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4258f8829a4aSRandall Stewart 
4259f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4260f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4261f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4262f8829a4aSRandall Stewart 
4263f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4264f8829a4aSRandall Stewart 						/*
4265f8829a4aSRandall Stewart 						 * True non-retransmited
4266f8829a4aSRandall Stewart 						 * chunk
4267f8829a4aSRandall Stewart 						 */
4268f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4269f8829a4aSRandall Stewart 						    tp1->send_size;
4270f8829a4aSRandall Stewart 
4271f8829a4aSRandall Stewart 						/* update RTO too? */
4272f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4273f79aab18SRandall Stewart 							if (rto_ok) {
4274f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4275f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4276f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
427718e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4278899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4279f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4280f79aab18SRandall Stewart 								rto_ok = 0;
4281f79aab18SRandall Stewart 							}
4282f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4283f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4284f79aab18SRandall Stewart 							}
4285f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4286f8829a4aSRandall Stewart 						}
4287f8829a4aSRandall Stewart 					}
4288f8829a4aSRandall Stewart 					/*
4289f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4290f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4291f8829a4aSRandall Stewart 					 * acked for the first time, set the
4292f8829a4aSRandall Stewart 					 * following variables for the
4293f8829a4aSRandall Stewart 					 * corresp destination.
4294f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4295f8829a4aSRandall Stewart 					 * cwnd update.
4296f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4297f8829a4aSRandall Stewart 					 * trigger search for the next
4298f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4299f8829a4aSRandall Stewart 					 */
4300f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4301f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4302f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4303f8829a4aSRandall Stewart 
4304f8829a4aSRandall Stewart 
4305b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4306f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4307f8829a4aSRandall Stewart 						    cum_ack,
4308f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4309f8829a4aSRandall Stewart 						    0,
4310f8829a4aSRandall Stewart 						    0,
4311f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
431280fefe0aSRandall Stewart 					}
4313b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4314f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
431580fefe0aSRandall Stewart 					}
4316f8829a4aSRandall Stewart 				}
4317f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4318f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4319f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4320f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4321f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4322f8829a4aSRandall Stewart #endif
4323f8829a4aSRandall Stewart 				}
432442551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
432542551e99SRandall Stewart 					/* deflate the cwnd */
432642551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
432742551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
432842551e99SRandall Stewart 				}
4329325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4330f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_ACKED;
4331f8829a4aSRandall Stewart 				}
4332325c8c46SMichael Tuexen 			}
4333f8829a4aSRandall Stewart 		} else {
4334f8829a4aSRandall Stewart 			break;
4335f8829a4aSRandall Stewart 		}
4336f8829a4aSRandall Stewart 	}
4337f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4338f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4339f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4340f8829a4aSRandall Stewart 
4341cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4342f8829a4aSRandall Stewart 
4343f8829a4aSRandall Stewart 		/*
4344f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4345f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4346f8829a4aSRandall Stewart 		 * for all dests.
4347f8829a4aSRandall Stewart 		 */
4348f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4349f8829a4aSRandall Stewart 			net->saw_newack = 0;
4350f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4351f8829a4aSRandall Stewart 		}
4352f8829a4aSRandall Stewart 
4353f8829a4aSRandall Stewart 		/*
4354f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4355f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4356f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4357f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4358f8829a4aSRandall Stewart 		 */
4359cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4360cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
43617215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4362cd554309SMichael Tuexen 			wake_him++;
4363cd554309SMichael Tuexen 		}
4364b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4365f8829a4aSRandall Stewart 			/*
4366f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4367f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4368f8829a4aSRandall Stewart 			 */
436920b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4370f8829a4aSRandall Stewart 				/*
4371f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4372f8829a4aSRandall Stewart 				 * attack. We must abort.
4373f8829a4aSRandall Stewart 				 */
4374cd3fd531SMichael Tuexen 				SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4375cd3fd531SMichael Tuexen 				    biggest_tsn_acked, send_s);
4376f8829a4aSRandall Stewart 				goto hopeless_peer;
4377f8829a4aSRandall Stewart 			}
4378f8829a4aSRandall Stewart 		}
4379f8829a4aSRandall Stewart 	}
4380f8829a4aSRandall Stewart 	/*******************************************/
4381f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4382f8829a4aSRandall Stewart 	/*******************************************/
43837c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4384f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4385f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4386f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4387a5d547adSRandall Stewart 				    stcb, net,
4388a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4389f8829a4aSRandall Stewart 
4390f8829a4aSRandall Stewart 		}
4391f8829a4aSRandall Stewart 	} else {
4392f8829a4aSRandall Stewart 		if (accum_moved) {
4393f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4394f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4395a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4396f8829a4aSRandall Stewart 			}
4397f8829a4aSRandall Stewart 		}
4398f8829a4aSRandall Stewart 	}
4399f8829a4aSRandall Stewart 	/********************************************/
4400d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4401f8829a4aSRandall Stewart 	/********************************************/
4402f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4403f8829a4aSRandall Stewart 
44047c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
440520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4406f8829a4aSRandall Stewart 			break;
4407f8829a4aSRandall Stewart 		}
4408325c8c46SMichael Tuexen 		if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4409a7ad6026SMichael Tuexen 			if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
4410a7ad6026SMichael Tuexen 				asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
4411a7ad6026SMichael Tuexen #ifdef INVARIANTS
4412a7ad6026SMichael Tuexen 			} else {
4413a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
4414a7ad6026SMichael Tuexen #endif
4415a7ad6026SMichael Tuexen 			}
4416f8829a4aSRandall Stewart 		}
4417f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
44180ddb4299SMichael Tuexen 		if (PR_SCTP_ENABLED(tp1->flags)) {
4419f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4420f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4421f8829a4aSRandall Stewart 		}
44227c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4423f8829a4aSRandall Stewart 		if (tp1->data) {
442404ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4425f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4426f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
44277c99d56fSMichael Tuexen 			tp1->data = NULL;
4428810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4429f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4430f8829a4aSRandall Stewart 			}
4431f8829a4aSRandall Stewart 		}
4432b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4433f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4434f8829a4aSRandall Stewart 			    cum_ack,
4435f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4436f8829a4aSRandall Stewart 			    0,
4437f8829a4aSRandall Stewart 			    0,
4438f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
443980fefe0aSRandall Stewart 		}
4440689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4441f8829a4aSRandall Stewart 		wake_him++;
44427c99d56fSMichael Tuexen 	}
44437c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
44447c99d56fSMichael Tuexen #ifdef INVARIANTS
44457c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
44467c99d56fSMichael Tuexen #else
44477c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
44487c99d56fSMichael Tuexen 		    asoc->total_flight);
44497c99d56fSMichael Tuexen #endif
44507c99d56fSMichael Tuexen 		asoc->total_flight = 0;
44517c99d56fSMichael Tuexen 	}
445204ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4453f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4454ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4455ceaad40aSRandall Stewart 		struct socket *so;
4456ceaad40aSRandall Stewart 
4457ceaad40aSRandall Stewart #endif
4458f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4459b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
44607215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
446180fefe0aSRandall Stewart 		}
4462ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4463ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4464ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4465ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4466ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4467ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4468ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4469ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4470ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4471ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4472ceaad40aSRandall Stewart 			return;
4473ceaad40aSRandall Stewart 		}
4474ceaad40aSRandall Stewart #endif
4475f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4476ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4477ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4478ceaad40aSRandall Stewart #endif
4479f8829a4aSRandall Stewart 	} else {
4480b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
44817215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
448280fefe0aSRandall Stewart 		}
4483f8829a4aSRandall Stewart 	}
4484f8829a4aSRandall Stewart 
448542551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
448620b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4487f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4488f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4489f8829a4aSRandall Stewart 		}
4490f8829a4aSRandall Stewart 	}
4491f8829a4aSRandall Stewart 	/*
4492f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4493f8829a4aSRandall Stewart 	 *
4494f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4495f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4496f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4497f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4498f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4499f8829a4aSRandall Stewart 	 */
4500f8829a4aSRandall Stewart 
4501d9c5cfeaSMichael Tuexen 	if (num_seg) {
4502c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4503d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4504d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4505f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4506f8829a4aSRandall Stewart 
4507f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4508f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4509b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4510f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4511b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4512c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4513c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4514c105859eSRandall Stewart 					    tp1->book_size,
4515c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4516c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
451780fefe0aSRandall Stewart 				}
4518c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4519c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4520a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
452142551e99SRandall Stewart 				/*
452242551e99SRandall Stewart 				 * To ensure that this increase in
45234a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
45244a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
45254a9ef3f8SMichael Tuexen 				 * cwnd artificially.
452642551e99SRandall Stewart 				 */
452742551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4528f8829a4aSRandall Stewart 				cnt_revoked++;
4529f8829a4aSRandall Stewart 			}
4530f8829a4aSRandall Stewart 		}
4531f8829a4aSRandall Stewart 		if (cnt_revoked) {
4532f8829a4aSRandall Stewart 			reneged_all = 1;
4533f8829a4aSRandall Stewart 		}
4534f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4535f8829a4aSRandall Stewart 	}
4536d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4537d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4538f8829a4aSRandall Stewart 	else
4539d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4540f8829a4aSRandall Stewart 
4541b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4542ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4543ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4544ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4545ca85e948SMichael Tuexen 				/*
4546ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4547ca85e948SMichael Tuexen 				 * count, this is optional.
4548ca85e948SMichael Tuexen 				 */
4549ca85e948SMichael Tuexen 				net->error_count = 0;
4550ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4551ca85e948SMichael Tuexen 					/* addr came good */
4552ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4553ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
45544b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4555ca85e948SMichael Tuexen 				}
4556ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4557ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4558ca85e948SMichael Tuexen 						/*
4559ca85e948SMichael Tuexen 						 * release the alternate,
4560ca85e948SMichael Tuexen 						 * primary is good
4561ca85e948SMichael Tuexen 						 */
4562ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4563ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4564ca85e948SMichael Tuexen 					}
4565ca85e948SMichael Tuexen 				}
4566ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4567ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4568ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4569ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4570ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4571ca85e948SMichael Tuexen 					/* Done with this net */
4572ca85e948SMichael Tuexen 					net->net_ack = 0;
4573ca85e948SMichael Tuexen 				}
4574ca85e948SMichael Tuexen 				/* restore any doubled timers */
4575ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4576ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4577ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4578ca85e948SMichael Tuexen 				}
4579ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4580ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4581ca85e948SMichael Tuexen 				}
4582ca85e948SMichael Tuexen 			}
4583ca85e948SMichael Tuexen 		}
4584b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4585ca85e948SMichael Tuexen 	}
4586f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4587f8829a4aSRandall Stewart 		/* nothing left in-flight */
4588f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4589f8829a4aSRandall Stewart 			/* stop all timers */
4590f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4591a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4592f8829a4aSRandall Stewart 			net->flight_size = 0;
4593f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4594f8829a4aSRandall Stewart 		}
4595f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4596f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4597f8829a4aSRandall Stewart 	}
4598f8829a4aSRandall Stewart 	/**********************************/
4599f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4600f8829a4aSRandall Stewart 	/**********************************/
4601f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4602f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4603b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4604f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4605f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
460680fefe0aSRandall Stewart 		}
4607f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4608f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4609f8829a4aSRandall Stewart 			/* SWS sender side engages */
4610f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4611f8829a4aSRandall Stewart 		}
4612f8829a4aSRandall Stewart 		/* clean up */
4613f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4614f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4615f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4616f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4617f8829a4aSRandall Stewart 		    ) {
4618f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4619f8829a4aSRandall Stewart 
4620f8829a4aSRandall Stewart 			/*
4621f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4622f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4623f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4624f8829a4aSRandall Stewart 			 */
4625f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4626f8829a4aSRandall Stewart 			    sctp_streamhead);
46272afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4628f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
46292afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4630f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
46312afb3e84SRandall Stewart 				} else {
46322afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
46332afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46342afb3e84SRandall Stewart 				}
4635f8829a4aSRandall Stewart 			}
4636f8829a4aSRandall Stewart 		}
4637f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4638f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4639f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4640f8829a4aSRandall Stewart 				/* Need to abort here */
4641ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4642f8829a4aSRandall Stewart 
4643f8829a4aSRandall Stewart 		abort_out_now:
4644f8829a4aSRandall Stewart 				*abort_now = 1;
4645f8829a4aSRandall Stewart 				/* XXX */
4646ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4647a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4648ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4649f8829a4aSRandall Stewart 				return;
4650f8829a4aSRandall Stewart 			} else {
4651ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4652ca85e948SMichael Tuexen 
4653f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4654f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4655f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4656f42a358aSRandall Stewart 				}
4657c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4658b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4659f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4660c39cfa1fSMichael Tuexen 				if (asoc->alternate) {
4661c39cfa1fSMichael Tuexen 					netp = asoc->alternate;
4662c39cfa1fSMichael Tuexen 				} else {
4663c39cfa1fSMichael Tuexen 					netp = asoc->primary_destination;
4664c39cfa1fSMichael Tuexen 				}
4665ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4666f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4667ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4668f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4669ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4670f8829a4aSRandall Stewart 			}
4671f8829a4aSRandall Stewart 			return;
4672f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4673f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4674ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4675ca85e948SMichael Tuexen 
4676f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4677f8829a4aSRandall Stewart 				goto abort_out_now;
4678f8829a4aSRandall Stewart 			}
4679f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4680c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4681b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
468212af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4683c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4684c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4685c39cfa1fSMichael Tuexen 			} else {
4686c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4687c39cfa1fSMichael Tuexen 			}
4688c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4689f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4690ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4691f8829a4aSRandall Stewart 			return;
4692f8829a4aSRandall Stewart 		}
4693f8829a4aSRandall Stewart 	}
4694f8829a4aSRandall Stewart 	/*
4695f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4696f8829a4aSRandall Stewart 	 * HEADS UP.
4697f8829a4aSRandall Stewart 	 */
4698f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4699f8829a4aSRandall Stewart 		net->net_ack = 0;
4700f8829a4aSRandall Stewart 	}
4701f8829a4aSRandall Stewart 
4702f8829a4aSRandall Stewart 	/*
4703f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4704f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4705f8829a4aSRandall Stewart 	 * automatically ensure that.
4706f8829a4aSRandall Stewart 	 */
47077c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
470820083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
470920083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
4710f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
4711f8829a4aSRandall Stewart 	}
4712cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4713f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4714f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4715f8829a4aSRandall Stewart 	}
4716b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4717b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4718f8829a4aSRandall Stewart 
4719f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
4720f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
4721f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
4722f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
4723f8829a4aSRandall Stewart 	}
4724f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
472520b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4726f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
4727f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
4728f8829a4aSRandall Stewart 	}
472942551e99SRandall Stewart 	/*
473042551e99SRandall Stewart 	 * CMT Fast recovery
473142551e99SRandall Stewart 	 */
4732f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4733f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
4734f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
4735f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
4736f8829a4aSRandall Stewart 		}
4737f8829a4aSRandall Stewart 	}
4738f8829a4aSRandall Stewart 
4739f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
4740b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4741f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
474244fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
474380fefe0aSRandall Stewart 	}
4744f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
474544fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4746f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4747f8829a4aSRandall Stewart 		/* SWS sender side engages */
4748f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4749f8829a4aSRandall Stewart 	}
47505e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
47515e54f665SRandall Stewart 		win_probe_recovery = 1;
47525e54f665SRandall Stewart 	}
4753f8829a4aSRandall Stewart 	/*
4754f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
4755f8829a4aSRandall Stewart 	 * outstanding data.
4756f8829a4aSRandall Stewart 	 */
4757bff64a4dSRandall Stewart 	done_once = 0;
4758a5d547adSRandall Stewart again:
4759a5d547adSRandall Stewart 	j = 0;
4760f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
47615e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4762c105859eSRandall Stewart 			win_probe_recovered = 1;
47635e54f665SRandall Stewart 			/*-
47645e54f665SRandall Stewart 			 * Find first chunk that was used with
47655e54f665SRandall Stewart 			 * window probe and clear the event. Put
47665e54f665SRandall Stewart 			 * it back into the send queue as if has
47675e54f665SRandall Stewart 			 * not been sent.
47685e54f665SRandall Stewart 			 */
47695e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
47705e54f665SRandall Stewart 				if (tp1->window_probe) {
47717215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
47725e54f665SRandall Stewart 					break;
47735e54f665SRandall Stewart 				}
47745e54f665SRandall Stewart 			}
47755e54f665SRandall Stewart 		}
4776f8829a4aSRandall Stewart 		if (net->flight_size) {
4777a5d547adSRandall Stewart 			j++;
4778cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4779f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4780f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
4781cd554309SMichael Tuexen 			}
47825171328bSRandall Stewart 			if (net->window_probe) {
4783cd554309SMichael Tuexen 				net->window_probe = 0;
47845171328bSRandall Stewart 			}
4785c105859eSRandall Stewart 		} else {
47865171328bSRandall Stewart 			if (net->window_probe) {
47875171328bSRandall Stewart 				/*
47885171328bSRandall Stewart 				 * In window probes we must assure a timer
47895171328bSRandall Stewart 				 * is still running there
47905171328bSRandall Stewart 				 */
47915171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
47925171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
47935171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
47945171328bSRandall Stewart 
47955171328bSRandall Stewart 				}
47965171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4797c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4798c105859eSRandall Stewart 				    stcb, net,
4799c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4800c105859eSRandall Stewart 			}
4801f8829a4aSRandall Stewart 		}
4802f8829a4aSRandall Stewart 	}
4803bff64a4dSRandall Stewart 	if ((j == 0) &&
4804bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4805bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4806c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4807bff64a4dSRandall Stewart 	    (done_once == 0)) {
48080c0982b8SRandall Stewart 		/*
48090c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
48100c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
48110c0982b8SRandall Stewart 		 */
48120c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4813a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4814a5d547adSRandall Stewart 				net->flight_size = 0;
4815a5d547adSRandall Stewart 			}
4816a5d547adSRandall Stewart 			asoc->total_flight = 0;
4817a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4818a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4819a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4820a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4821c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4822c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4823a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4824791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4825a5d547adSRandall Stewart 				}
4826a5d547adSRandall Stewart 			}
48270c0982b8SRandall Stewart 		}
4828bff64a4dSRandall Stewart 		done_once = 1;
4829a5d547adSRandall Stewart 		goto again;
4830a5d547adSRandall Stewart 	}
4831cd554309SMichael Tuexen 	/*********************************************/
4832cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
4833cd554309SMichael Tuexen 	/* (section 4.2)                             */
4834cd554309SMichael Tuexen 	/*********************************************/
4835cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
483620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
4837dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
4838dfb11ef8SRandall Stewart 	}
4839830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
4840830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4841830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4842830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4843830d754dSRandall Stewart 
4844830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4845830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4846830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
484720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
4848830d754dSRandall Stewart 			/*
4849493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4850830d754dSRandall Stewart 			 */
48510c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
48520c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
48530c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
48540c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
48550c0982b8SRandall Stewart 			}
485620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4857830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
48580c0982b8SRandall Stewart 			} else if (lchk) {
48590c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
486044fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
48610c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
48620c0982b8SRandall Stewart 				}
4863830d754dSRandall Stewart 			}
4864830d754dSRandall Stewart 		}
4865830d754dSRandall Stewart 		if (lchk) {
4866830d754dSRandall Stewart 			/* Assure a timer is up */
4867830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4868830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4869830d754dSRandall Stewart 		}
4870830d754dSRandall Stewart 	}
4871b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4872f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4873f8829a4aSRandall Stewart 		    a_rwnd,
4874f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4875f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4876f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
487780fefe0aSRandall Stewart 	}
4878f8829a4aSRandall Stewart }
4879f8829a4aSRandall Stewart 
4880f8829a4aSRandall Stewart void
48817215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
4882f8829a4aSRandall Stewart {
4883f8829a4aSRandall Stewart 	/* Copy cum-ack */
4884f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
4885f8829a4aSRandall Stewart 
4886f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
4887f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
4888f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
4889f8829a4aSRandall Stewart 
4890f8829a4aSRandall Stewart 	/* Now call the express sack handling */
4891899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
4892f8829a4aSRandall Stewart }
4893f8829a4aSRandall Stewart 
4894f8829a4aSRandall Stewart static void
4895f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
4896f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
4897f8829a4aSRandall Stewart {
4898f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
4899f8829a4aSRandall Stewart 	struct sctp_association *asoc;
490083128708SRandall Stewart 	uint16_t tt;
4901f8829a4aSRandall Stewart 
4902f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4903f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
4904f8829a4aSRandall Stewart 	/*
4905f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
4906f8829a4aSRandall Stewart 	 * came in
4907f8829a4aSRandall Stewart 	 */
49084a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
490920b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
4910f8829a4aSRandall Stewart 			/* this is deliverable now */
4911f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4912f8829a4aSRandall Stewart 			/* subtract pending on streams */
4913f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
4914f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
4915f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
4916f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
4917b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4918f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
4919f8829a4aSRandall Stewart 				    ctl,
4920cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
4921f8829a4aSRandall Stewart 			}
4922f8829a4aSRandall Stewart 		} else {
4923f8829a4aSRandall Stewart 			/* no more delivery now. */
4924f8829a4aSRandall Stewart 			break;
4925f8829a4aSRandall Stewart 		}
4926f8829a4aSRandall Stewart 	}
4927f8829a4aSRandall Stewart 	/*
4928f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
4929f8829a4aSRandall Stewart 	 * now ready.
4930f8829a4aSRandall Stewart 	 */
4931f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
49324a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
4933f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
4934f8829a4aSRandall Stewart 			/* this is deliverable now */
4935f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4936f8829a4aSRandall Stewart 			/* subtract pending on streams */
4937f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
4938f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
4939f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
4940f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
4941f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
4942b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4943f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
4944f8829a4aSRandall Stewart 				    ctl,
4945cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
494677acdc25SRandall Stewart 
4947f8829a4aSRandall Stewart 			}
4948f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
4949f8829a4aSRandall Stewart 		} else {
4950f8829a4aSRandall Stewart 			break;
4951f8829a4aSRandall Stewart 		}
4952f8829a4aSRandall Stewart 	}
4953f8829a4aSRandall Stewart }
4954f8829a4aSRandall Stewart 
49558933fa13SRandall Stewart static void
49568933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
49578933fa13SRandall Stewart     struct sctp_association *asoc,
49588933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
49598933fa13SRandall Stewart {
49604a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
49618933fa13SRandall Stewart 
49628933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
49638933fa13SRandall Stewart 	/*
49644a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
49654a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
49664a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
49674a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
49684a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
49694a9ef3f8SMichael Tuexen 	 * queue.
49708933fa13SRandall Stewart 	 */
49714a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
49728e71b694SMichael Tuexen 		/*
49734a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
49744a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
49754a9ef3f8SMichael Tuexen 		 * number has no meaning.
49768e71b694SMichael Tuexen 		 */
49778e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
49788e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
49798933fa13SRandall Stewart 			continue;
49808933fa13SRandall Stewart 		}
49818933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
49828933fa13SRandall Stewart 			/* It needs to be tossed */
49838933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
498420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
49854a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
49864a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
49874a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
49884a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
49898933fa13SRandall Stewart 			}
49908933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
49918933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
49928933fa13SRandall Stewart 
49938933fa13SRandall Stewart 			/* Clear up any stream problem */
499420b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
499520b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
49968933fa13SRandall Stewart 				/*
49978933fa13SRandall Stewart 				 * We must dump forward this streams
49984a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
49994a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
50004a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
50014a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
50024a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
50034a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
50044a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
50054a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
50064a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
50074a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
50084a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
50098933fa13SRandall Stewart 				 */
50104a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
50118933fa13SRandall Stewart 			}
50128933fa13SRandall Stewart 			if (chk->data) {
50138933fa13SRandall Stewart 				sctp_m_freem(chk->data);
50148933fa13SRandall Stewart 				chk->data = NULL;
50158933fa13SRandall Stewart 			}
5016689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
501720b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
50188933fa13SRandall Stewart 			/*
50194a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
50204a9ef3f8SMichael Tuexen 			 * are done
50218933fa13SRandall Stewart 			 */
50228933fa13SRandall Stewart 			break;
50238933fa13SRandall Stewart 		}
50248933fa13SRandall Stewart 	}
50258933fa13SRandall Stewart }
50268933fa13SRandall Stewart 
50278933fa13SRandall Stewart 
5028f8829a4aSRandall Stewart void
5029f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5030b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5031b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5032f8829a4aSRandall Stewart {
5033f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5034f8829a4aSRandall Stewart 	/*
5035f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5036f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5037f8829a4aSRandall Stewart 	 *
5038f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5039f8829a4aSRandall Stewart 	 *
5040f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5041f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5042f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5043f8829a4aSRandall Stewart 	 * report where we are.
5044f8829a4aSRandall Stewart 	 */
5045f8829a4aSRandall Stewart 	struct sctp_association *asoc;
50467898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
50477215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
50488933fa13SRandall Stewart 	uint32_t str_seq;
5049f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
50504a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
50518933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5052f8829a4aSRandall Stewart 
5053f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5054f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5055ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5056ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5057f8829a4aSRandall Stewart 		return;
5058f8829a4aSRandall Stewart 	}
5059f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5060f8829a4aSRandall Stewart 	/*************************************************************/
5061f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5062f8829a4aSRandall Stewart 	/*************************************************************/
5063f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5064f8829a4aSRandall Stewart 
506520b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5066f8829a4aSRandall Stewart 		/* Already got there ... */
5067f8829a4aSRandall Stewart 		return;
5068f8829a4aSRandall Stewart 	}
5069f8829a4aSRandall Stewart 	/*
5070f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5071f8829a4aSRandall Stewart 	 * gap
5072f8829a4aSRandall Stewart 	 */
5073b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
507477acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
50752afb3e84SRandall Stewart 	if (gap >= m_size) {
5076f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5077ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
5078ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
507917205eccSRandall Stewart 
5080f8829a4aSRandall Stewart 			/*
5081f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
508217205eccSRandall Stewart 			 * give out). This must be an attacker.
5083f8829a4aSRandall Stewart 			 */
508417205eccSRandall Stewart 			*abort_flag = 1;
5085ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg),
5086ff1ffd74SMichael Tuexen 			    "New cum ack %8.8x too high, highest TSN %8.8x",
5087ff1ffd74SMichael Tuexen 			    new_cum_tsn, asoc->highest_tsn_inside_map);
5088ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
508917205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5090ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5091f8829a4aSRandall Stewart 			return;
5092f8829a4aSRandall Stewart 		}
5093207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
509477acdc25SRandall Stewart 
50952afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
50962afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
509777acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
509877acdc25SRandall Stewart 
5099b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5100830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
510177acdc25SRandall Stewart 
5102b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5103f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
510480fefe0aSRandall Stewart 		}
51052afb3e84SRandall Stewart 	} else {
51062afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
51072afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
51087898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
51097898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
51108933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
511120b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
51127898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5113b5c16493SMichael Tuexen 				}
5114b5c16493SMichael Tuexen 			}
5115b5c16493SMichael Tuexen 		}
5116f8829a4aSRandall Stewart 	}
5117f8829a4aSRandall Stewart 	/*************************************************************/
5118f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5119f8829a4aSRandall Stewart 	/*************************************************************/
5120f8829a4aSRandall Stewart 	/*
5121f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5122f8829a4aSRandall Stewart 	 * forward
5123f8829a4aSRandall Stewart 	 */
5124f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5125f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5126f8829a4aSRandall Stewart 	}
5127f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5128f8829a4aSRandall Stewart 	/*
51294a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
51304a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
51314a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
51324a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
51334a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
51344a9ef3f8SMichael Tuexen 	 * queue.
5135f8829a4aSRandall Stewart 	 */
51364a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
513720b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5138f8829a4aSRandall Stewart 			/* It needs to be tossed */
5139f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
514020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
51414a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
51424a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
51434a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
51444a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5145f8829a4aSRandall Stewart 			}
5146f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5147f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5148f8829a4aSRandall Stewart 
5149f8829a4aSRandall Stewart 			/* Clear up any stream problem */
515020b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
515120b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5152f8829a4aSRandall Stewart 				/*
5153f8829a4aSRandall Stewart 				 * We must dump forward this streams
51544a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
51554a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
51564a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
51574a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
51584a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
51594a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
51604a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
51614a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
51624a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
51634a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
51644a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5165f8829a4aSRandall Stewart 				 */
51664a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5167f8829a4aSRandall Stewart 			}
5168f8829a4aSRandall Stewart 			if (chk->data) {
5169f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5170f8829a4aSRandall Stewart 				chk->data = NULL;
5171f8829a4aSRandall Stewart 			}
5172689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5173f8829a4aSRandall Stewart 		} else {
5174f8829a4aSRandall Stewart 			/*
51754a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
51764a9ef3f8SMichael Tuexen 			 * mark.
5177f8829a4aSRandall Stewart 			 */
5178f8829a4aSRandall Stewart 			break;
5179f8829a4aSRandall Stewart 		}
5180f8829a4aSRandall Stewart 	}
51818933fa13SRandall Stewart 	/*******************************************************/
51828933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
51838933fa13SRandall Stewart 	/* delivery issues as needed.                       */
51848933fa13SRandall Stewart 	/*******************************************************/
5185f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5186671d309cSRandall Stewart 	if (m && fwd_sz) {
5187f8829a4aSRandall Stewart 		/* New method. */
5188d61a0ae0SRandall Stewart 		unsigned int num_str;
5189671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5190671d309cSRandall Stewart 
5191671d309cSRandall Stewart 		offset += sizeof(*fwd);
5192f8829a4aSRandall Stewart 
51938933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5194f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5195f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5196f8829a4aSRandall Stewart 			uint16_t st;
5197f8829a4aSRandall Stewart 
5198671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5199671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5200671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5201671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
52022afb3e84SRandall Stewart 			if (stseq == NULL) {
5203671d309cSRandall Stewart 				break;
52042afb3e84SRandall Stewart 			}
5205f8829a4aSRandall Stewart 			/* Convert */
5206851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5207851b7298SRandall Stewart 			stseq->stream = st;
5208851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5209851b7298SRandall Stewart 			stseq->sequence = st;
52108933fa13SRandall Stewart 
5211f8829a4aSRandall Stewart 			/* now process */
52128933fa13SRandall Stewart 
52138933fa13SRandall Stewart 			/*
52148933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
52158933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
52168933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
52178933fa13SRandall Stewart 			 */
5218851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
52192afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
52202afb3e84SRandall Stewart 				break;
5221f8829a4aSRandall Stewart 			}
52228933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
52238933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
52248933fa13SRandall Stewart 				/*
52258933fa13SRandall Stewart 				 * If this is the one we were partially
52268933fa13SRandall Stewart 				 * delivering now then we no longer are.
52278933fa13SRandall Stewart 				 * Note this will change with the reassembly
52288933fa13SRandall Stewart 				 * re-write.
52298933fa13SRandall Stewart 				 */
52308933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
52318933fa13SRandall Stewart 			}
52328933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
52338933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
52348933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
52358933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
52368933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
52378933fa13SRandall Stewart 					ctl->end_added = 1;
52388933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
52398933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
52408933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5241810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5242810ec536SMichael Tuexen 					    stcb,
52438933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5244810ec536SMichael Tuexen 					    (void *)&str_seq,
5245810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
52468933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
52478933fa13SRandall Stewart 					break;
52488933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
524920b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
52508933fa13SRandall Stewart 					/* We are past our victim SSN */
52518933fa13SRandall Stewart 					break;
52528933fa13SRandall Stewart 				}
52538933fa13SRandall Stewart 			}
5254851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
525520b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5256f8829a4aSRandall Stewart 				/* Update the sequence number */
525720b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5258f8829a4aSRandall Stewart 			}
5259f8829a4aSRandall Stewart 			/* now kick the stream the new way */
526004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5261f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5262f8829a4aSRandall Stewart 		}
52638933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5264f8829a4aSRandall Stewart 	}
52657898f408SRandall Stewart 	/*
52667898f408SRandall Stewart 	 * Now slide thing forward.
52677898f408SRandall Stewart 	 */
52687898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
52697898f408SRandall Stewart 
527024f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5271139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
527204ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5273139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5274139bc87fSRandall Stewart 	}
5275f8829a4aSRandall Stewart }
5276