xref: /freebsd/sys/netinet/sctp_indata.c (revision ff1ffd7499666a1cd73cc86e7974b3a34c4b89d1)
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;
564*ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
565*ff1ffd74SMichael 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);
590*ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
591*ff1ffd74SMichael Tuexen 		    strm->last_sequence_delivered, control->sinfo_tsn,
592*ff1ffd74SMichael Tuexen 		    control->sinfo_stream, control->sinfo_ssn);
593*ff1ffd74SMichael 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;
595*ff1ffd74SMichael 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 {
829*ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
830*ff1ffd74SMichael Tuexen 	char msg[SCTP_DIAG_INFO_LEN];
831*ff1ffd74SMichael 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");
856*ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg),
857*ff1ffd74SMichael Tuexen 				    "Expected B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
858*ff1ffd74SMichael Tuexen 				    chk->rec.data.TSN_seq,
859*ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_number,
860*ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_seq);
861*ff1ffd74SMichael 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;
863*ff1ffd74SMichael 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");
873*ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg),
874*ff1ffd74SMichael Tuexen 				    "Didn't expect B-bit for TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
875*ff1ffd74SMichael Tuexen 				    chk->rec.data.TSN_seq,
876*ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_number,
877*ff1ffd74SMichael Tuexen 				    chk->rec.data.stream_seq);
878*ff1ffd74SMichael 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;
880*ff1ffd74SMichael 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);
893*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
894*ff1ffd74SMichael Tuexen 					    "Expected SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
895*ff1ffd74SMichael Tuexen 					    asoc->str_of_pdapi,
896*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
897*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
898*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
899*ff1ffd74SMichael 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;
901*ff1ffd74SMichael 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);
910*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
911*ff1ffd74SMichael Tuexen 					    "Expected SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
912*ff1ffd74SMichael Tuexen 					    asoc->ssn_of_pdapi,
913*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
914*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
915*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
916*ff1ffd74SMichael 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;
918*ff1ffd74SMichael 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");
988*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
989*ff1ffd74SMichael Tuexen 					    "Can't handle B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
990*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
991*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
992*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
993*ff1ffd74SMichael 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;
995*ff1ffd74SMichael 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 					 */
1005*ff1ffd74SMichael 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);
1008*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1009*ff1ffd74SMichael Tuexen 					    "Expect SID=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1010*ff1ffd74SMichael Tuexen 					    prev->rec.data.stream_number,
1011*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1012*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1013*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1014*ff1ffd74SMichael 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;
1016*ff1ffd74SMichael 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);
1030*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1031*ff1ffd74SMichael Tuexen 					    "Expect SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1032*ff1ffd74SMichael Tuexen 					    prev->rec.data.stream_seq,
1033*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1034*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1035*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1036*ff1ffd74SMichael 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;
1038*ff1ffd74SMichael 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");
1048*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1049*ff1ffd74SMichael Tuexen 					    "Expect B-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1050*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1051*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1052*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1053*ff1ffd74SMichael 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;
1055*ff1ffd74SMichael 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");
1075*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1076*ff1ffd74SMichael Tuexen 					    "Expect only E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1077*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1078*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1079*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1080*ff1ffd74SMichael 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;
1082*ff1ffd74SMichael 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");
1098*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1099*ff1ffd74SMichael Tuexen 					    "Didn't expect E-bit, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1100*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1101*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1102*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1103*ff1ffd74SMichael 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;
1105*ff1ffd74SMichael 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);
1118*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1119*ff1ffd74SMichael Tuexen 					    "Required SID %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1120*ff1ffd74SMichael Tuexen 					    next->rec.data.stream_number,
1121*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1122*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1123*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1124*ff1ffd74SMichael 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;
1126*ff1ffd74SMichael 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);
1140*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg),
1141*ff1ffd74SMichael Tuexen 					    "Required SSN %4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1142*ff1ffd74SMichael Tuexen 					    next->rec.data.stream_seq,
1143*ff1ffd74SMichael Tuexen 					    chk->rec.data.TSN_seq,
1144*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_number,
1145*ff1ffd74SMichael Tuexen 					    chk->rec.data.stream_seq);
1146*ff1ffd74SMichael 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;
1148*ff1ffd74SMichael 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;
1233*ff1ffd74SMichael Tuexen 	struct mbuf *op_err;
1234*ff1ffd74SMichael 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) ||
1301*ff1ffd74SMichael 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 		 */
1306*ff1ffd74SMichael 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 
1435*ff1ffd74SMichael Tuexen 		snprintf(msg, sizeof(msg), "Delivered SSN=%4.4x, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1436*ff1ffd74SMichael Tuexen 		    asoc->strmin[strmno].last_sequence_delivered,
1437*ff1ffd74SMichael Tuexen 		    tsn, strmno, strmseq);
1438*ff1ffd74SMichael 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;
1440*ff1ffd74SMichael 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);
1679*ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "Reas. queue emtpy, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1680*ff1ffd74SMichael Tuexen 				    tsn, strmno, strmseq);
1681*ff1ffd74SMichael 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;
1683*ff1ffd74SMichael 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);
1695*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg), "PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1696*ff1ffd74SMichael Tuexen 					    tsn, strmno, strmseq);
1697*ff1ffd74SMichael 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;
1699*ff1ffd74SMichael 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);
1721*ff1ffd74SMichael Tuexen 					snprintf(msg, sizeof(msg), "No PD ongoing, got TSN=%8.8x, SID=%4.4x, SSN=%4.4x",
1722*ff1ffd74SMichael Tuexen 					    tsn, strmno, strmseq);
1723*ff1ffd74SMichael 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;
1725*ff1ffd74SMichael 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) {
2326f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
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;
2332*ff1ffd74SMichael Tuexen 				char msg[SCTP_DIAG_INFO_LEN];
2333f8829a4aSRandall Stewart 
2334*ff1ffd74SMichael Tuexen 				snprintf(msg, sizeof(msg), "DATA chunk of length %d",
2335*ff1ffd74SMichael Tuexen 				    chk_length);
2336*ff1ffd74SMichael 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 			}
2344f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2345f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2346f8829a4aSRandall Stewart #endif
2347f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2348f8829a4aSRandall Stewart 				last_chunk = 1;
2349f8829a4aSRandall Stewart 			} else {
2350f8829a4aSRandall Stewart 				last_chunk = 0;
2351f8829a4aSRandall Stewart 			}
2352f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2353f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2354f8829a4aSRandall Stewart 			    last_chunk)) {
2355f8829a4aSRandall Stewart 				num_chunks++;
2356f8829a4aSRandall Stewart 			}
2357f8829a4aSRandall Stewart 			if (abort_flag)
2358f8829a4aSRandall Stewart 				return (2);
2359f8829a4aSRandall Stewart 
2360f8829a4aSRandall Stewart 			if (break_flag) {
2361f8829a4aSRandall Stewart 				/*
2362f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2363f8829a4aSRandall Stewart 				 * drop rep space left.
2364f8829a4aSRandall Stewart 				 */
2365f8829a4aSRandall Stewart 				stop_proc = 1;
236660990c0cSMichael Tuexen 				continue;
2367f8829a4aSRandall Stewart 			}
2368f8829a4aSRandall Stewart 		} else {
2369f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2370f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2371f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2372f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2373f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
237460990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2375f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2376f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2377f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2378f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2379f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2380f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2381f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2382f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2383f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2384f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2385f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2386f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2387f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2388f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2389f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2390f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2391f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2392f8829a4aSRandall Stewart 				/*
2393f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2394f8829a4aSRandall Stewart 				 * are NOT in the right place?
2395f8829a4aSRandall Stewart 				 *
2396f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2397f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2398f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2399f8829a4aSRandall Stewart 				 * possibly process them.
2400f8829a4aSRandall Stewart 				 */
2401b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2402f8829a4aSRandall Stewart 					struct mbuf *op_err;
2403f8829a4aSRandall Stewart 
2404*ff1ffd74SMichael Tuexen 					op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
2405f30ac432SMichael Tuexen 					sctp_abort_association(inp, stcb,
2406f30ac432SMichael Tuexen 					    m, iphlen,
2407b1754ad1SMichael Tuexen 					    src, dst,
2408f30ac432SMichael Tuexen 					    sh, op_err,
2409f30ac432SMichael Tuexen 					    use_mflowid, mflowid,
2410f30ac432SMichael Tuexen 					    vrf_id, port);
2411f8829a4aSRandall Stewart 					return (2);
2412f8829a4aSRandall Stewart 				}
2413f8829a4aSRandall Stewart 				break;
2414f8829a4aSRandall Stewart 			default:
2415f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2416f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2417f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2418d61a0ae0SRandall Stewart 					struct mbuf *merr;
2419f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2420f8829a4aSRandall Stewart 
2421eb1b1807SGleb Smirnoff 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_NOWAIT, 1, MT_DATA);
2422d61a0ae0SRandall Stewart 					if (merr) {
2423d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2424f8829a4aSRandall Stewart 						/*
2425f8829a4aSRandall Stewart 						 * We cheat and use param
2426f8829a4aSRandall Stewart 						 * type since we did not
2427f8829a4aSRandall Stewart 						 * bother to define a error
2428f8829a4aSRandall Stewart 						 * cause struct. They are
2429f8829a4aSRandall Stewart 						 * the same basic format
2430f8829a4aSRandall Stewart 						 * with different names.
2431f8829a4aSRandall Stewart 						 */
2432f8829a4aSRandall Stewart 						phd->param_type =
2433f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2434f8829a4aSRandall Stewart 						phd->param_length =
2435f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2436d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2437eb1b1807SGleb Smirnoff 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_NOWAIT);
2438d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2439921569e2SMichael Tuexen 							if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
2440921569e2SMichael Tuexen 								sctp_m_freem(merr);
2441921569e2SMichael Tuexen 							} else {
2442d61a0ae0SRandall Stewart 								sctp_queue_op_err(stcb, merr);
2443921569e2SMichael Tuexen 							}
2444f8829a4aSRandall Stewart 						} else {
2445d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2446f8829a4aSRandall Stewart 						}
2447f8829a4aSRandall Stewart 					}
2448f8829a4aSRandall Stewart 				}
2449f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2450f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2451f8829a4aSRandall Stewart 					stop_proc = 1;
2452f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2453f8829a4aSRandall Stewart 					 * continue... */
2454f8829a4aSRandall Stewart 				break;
245560990c0cSMichael Tuexen 			}	/* switch of chunk type */
2456f8829a4aSRandall Stewart 		}
2457f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2458f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2459f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2460f8829a4aSRandall Stewart 			stop_proc = 1;
2461f8829a4aSRandall Stewart 			continue;
2462f8829a4aSRandall Stewart 		}
2463f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2464f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2465f8829a4aSRandall Stewart 		if (ch == NULL) {
2466f8829a4aSRandall Stewart 			*offset = length;
2467f8829a4aSRandall Stewart 			stop_proc = 1;
246860990c0cSMichael Tuexen 			continue;
2469f8829a4aSRandall Stewart 		}
247060990c0cSMichael Tuexen 	}
2471f8829a4aSRandall Stewart 	if (break_flag) {
2472f8829a4aSRandall Stewart 		/*
2473f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2474f8829a4aSRandall Stewart 		 */
247520cc2188SMichael Tuexen 		sctp_send_packet_dropped(stcb, net, *mm, length, iphlen, 0);
2476f8829a4aSRandall Stewart 	}
2477f8829a4aSRandall Stewart 	if (num_chunks) {
2478f8829a4aSRandall Stewart 		/*
2479ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2480f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2481f8829a4aSRandall Stewart 		 */
2482f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2483b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2484c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2485c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2486c4739e2fSRandall Stewart 			    0,
2487c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2488c4739e2fSRandall Stewart 			    __LINE__);
2489c4739e2fSRandall Stewart 		}
2490f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
24916e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2492f8829a4aSRandall Stewart 	}
2493f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2494f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2495f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2496f8829a4aSRandall Stewart 
2497f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
249842551e99SRandall Stewart 		/* Assure that we ack right away */
249942551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2500f8829a4aSRandall Stewart 	}
2501f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
25027215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2503f8829a4aSRandall Stewart 	return (0);
2504f8829a4aSRandall Stewart }
2505f8829a4aSRandall Stewart 
25060fa753b3SRandall Stewart static int
25070fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
25080fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
25090fa753b3SRandall Stewart     int *num_frs,
25100fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
25110fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
25127215cc1bSMichael Tuexen     int *rto_ok)
25130fa753b3SRandall Stewart {
25140fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
25150fa753b3SRandall Stewart 	unsigned int theTSN;
2516b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
25170fa753b3SRandall Stewart 
25180fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
25190fa753b3SRandall Stewart 	tp1 = *p_tp1;
25200fa753b3SRandall Stewart 	if (tp1 == NULL) {
25210fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
25220fa753b3SRandall Stewart 	}
25230fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
25240fa753b3SRandall Stewart 		theTSN = j + last_tsn;
25250fa753b3SRandall Stewart 		while (tp1) {
25260fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
25270fa753b3SRandall Stewart 				(*num_frs) += 1;
25280fa753b3SRandall Stewart 
25290fa753b3SRandall Stewart 			/*-
25300fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
25310fa753b3SRandall Stewart 			 * processed from the sent queue, track the
25320fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
25330fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
25340fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
25350fa753b3SRandall Stewart 			 * and retransmissions.
25360fa753b3SRandall Stewart 			 */
25370fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
25380fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
25390fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
25400fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
25410fa753b3SRandall Stewart 			}
25420fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
25430fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
25440fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
25450fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
25460fa753b3SRandall Stewart 			}
25470fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
25480fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
25490fa753b3SRandall Stewart 					/*-
25500fa753b3SRandall Stewart 					 * must be held until
25510fa753b3SRandall Stewart 					 * cum-ack passes
25520fa753b3SRandall Stewart 					 */
25530fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
25540fa753b3SRandall Stewart 						/*-
25550fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
25560fa753b3SRandall Stewart 						 * now no-longer in flight.
25570fa753b3SRandall Stewart 						 * Higher values may already be set
25580fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
25590fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
25600fa753b3SRandall Stewart 						 */
256120b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
256220b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
25630fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
25640fa753b3SRandall Stewart 						}
25650fa753b3SRandall Stewart 						/*-
25660fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
25670fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
25680fa753b3SRandall Stewart 						 * newly acked. update
25690fa753b3SRandall Stewart 						 * this_sack_highest_newack if
25700fa753b3SRandall Stewart 						 * appropriate.
25710fa753b3SRandall Stewart 						 */
25720fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
25730fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
25740fa753b3SRandall Stewart 
257520b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
257620b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
25770fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
25780fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
25790fa753b3SRandall Stewart 						}
25800fa753b3SRandall Stewart 						/*-
25810fa753b3SRandall Stewart 						 * CMT DAC algo: also update
25820fa753b3SRandall Stewart 						 * this_sack_lowest_newack
25830fa753b3SRandall Stewart 						 */
25840fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
25850fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
25860fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
25870fa753b3SRandall Stewart 								    last_tsn,
25880fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
25890fa753b3SRandall Stewart 								    0,
25900fa753b3SRandall Stewart 								    0,
25910fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
25920fa753b3SRandall Stewart 							}
25930fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
25940fa753b3SRandall Stewart 						}
25950fa753b3SRandall Stewart 						/*-
25960fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
25970fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
25980fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
25990fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
26000fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
26010fa753b3SRandall Stewart 						 * retransmissions.
26020fa753b3SRandall Stewart 						 */
26030fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
26040fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
26050fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
26060fa753b3SRandall Stewart 							}
26070fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
26080fa753b3SRandall Stewart 						}
26090fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
26100fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
26110fa753b3SRandall Stewart 						}
26120fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
26130fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
26140fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
26150fa753b3SRandall Stewart 							}
26160fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
26170fa753b3SRandall Stewart 						}
26180fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
26190fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
26200fa753b3SRandall Stewart 							    last_tsn,
26210fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
26220fa753b3SRandall Stewart 							    frag_strt,
26230fa753b3SRandall Stewart 							    frag_end,
26240fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
26250fa753b3SRandall Stewart 						}
26260fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
26270fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
26280fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
26290fa753b3SRandall Stewart 							    tp1->book_size,
26300fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
26310fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
26320fa753b3SRandall Stewart 						}
26330fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2634299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2635299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2636299108c5SRandall Stewart 							    tp1);
2637299108c5SRandall Stewart 						}
26380fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
26390fa753b3SRandall Stewart 
26400fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
26410fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
26420fa753b3SRandall Stewart 							/*-
26430fa753b3SRandall Stewart 							 * True non-retransmited chunk
26440fa753b3SRandall Stewart 							 */
26450fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
26460fa753b3SRandall Stewart 
26470fa753b3SRandall Stewart 							/*-
26480fa753b3SRandall Stewart 							 * update RTO too ?
26490fa753b3SRandall Stewart 							 */
26500fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2651f79aab18SRandall Stewart 								if (*rto_ok) {
26520fa753b3SRandall Stewart 									tp1->whoTo->RTO =
26530fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
26540fa753b3SRandall Stewart 									    &stcb->asoc,
26550fa753b3SRandall Stewart 									    tp1->whoTo,
26560fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2657899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2658f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2659f79aab18SRandall Stewart 									*rto_ok = 0;
2660f79aab18SRandall Stewart 								}
2661f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2662f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2663f79aab18SRandall Stewart 								}
26640fa753b3SRandall Stewart 								tp1->do_rtt = 0;
26650fa753b3SRandall Stewart 							}
26660fa753b3SRandall Stewart 						}
26670fa753b3SRandall Stewart 					}
26680fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
266920b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
267020b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
26710fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
26720fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
26730fa753b3SRandall Stewart 						}
26740fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
26750fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
26760fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
26770fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
26780fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
26790fa753b3SRandall Stewart #endif
26800fa753b3SRandall Stewart 						}
26810fa753b3SRandall Stewart 					}
26820fa753b3SRandall Stewart 					/*-
26830fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
26840fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
26850fa753b3SRandall Stewart 					 */
26862a498584SMichael Tuexen 					if ((tp1->sent != SCTP_FORWARD_TSN_SKIP) &&
2687325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
26880fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
26892a498584SMichael Tuexen 					}
26900fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
26910fa753b3SRandall Stewart 						/* deflate the cwnd */
26920fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
26930fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
26940fa753b3SRandall Stewart 					}
26950fa753b3SRandall Stewart 					/* NR Sack code here */
2696325c8c46SMichael Tuexen 					if (nr_sacking &&
2697325c8c46SMichael Tuexen 					    (tp1->sent != SCTP_DATAGRAM_NR_ACKED)) {
2698325c8c46SMichael Tuexen 						if (stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
2699325c8c46SMichael Tuexen 							stcb->asoc.strmout[tp1->rec.data.stream_number].chunks_on_queues--;
2700325c8c46SMichael Tuexen #ifdef INVARIANTS
2701325c8c46SMichael Tuexen 						} else {
2702325c8c46SMichael Tuexen 							panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
2703325c8c46SMichael Tuexen #endif
2704325c8c46SMichael Tuexen 						}
2705325c8c46SMichael Tuexen 						tp1->sent = SCTP_DATAGRAM_NR_ACKED;
27060fa753b3SRandall Stewart 						if (tp1->data) {
27070fa753b3SRandall Stewart 							/*
27080fa753b3SRandall Stewart 							 * sa_ignore
27090fa753b3SRandall Stewart 							 * NO_NULL_CHK
27100fa753b3SRandall Stewart 							 */
27110fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
27120fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
27130fa753b3SRandall Stewart 							tp1->data = NULL;
2714b5c16493SMichael Tuexen 						}
27150fa753b3SRandall Stewart 						wake_him++;
27160fa753b3SRandall Stewart 					}
27170fa753b3SRandall Stewart 				}
27180fa753b3SRandall Stewart 				break;
27190fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
272020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
27210fa753b3SRandall Stewart 				break;
272220b07a4dSMichael Tuexen 			}
27230fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
2724b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
2725b5c16493SMichael Tuexen 				circled++;
27260fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
27270fa753b3SRandall Stewart 			}
2728b5c16493SMichael Tuexen 		}		/* end while (tp1) */
2729b5c16493SMichael Tuexen 		if (tp1 == NULL) {
2730b5c16493SMichael Tuexen 			circled = 0;
2731b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2732b5c16493SMichael Tuexen 		}
2733b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
27340fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
27350fa753b3SRandall Stewart 	*p_tp1 = tp1;
27360fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
27370fa753b3SRandall Stewart }
27380fa753b3SRandall Stewart 
27390fa753b3SRandall Stewart 
2740cd554309SMichael Tuexen static int
2741458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2742cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2743139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
27447215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
2745f8829a4aSRandall Stewart {
2746458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
2747f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
27480fa753b3SRandall Stewart 	int i;
2749f8829a4aSRandall Stewart 	int num_frs = 0;
2750cd554309SMichael Tuexen 	int chunk_freed;
2751cd554309SMichael Tuexen 	int non_revocable;
2752d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
2753f8829a4aSRandall Stewart 
2754d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
2755d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
2756cd554309SMichael Tuexen 	chunk_freed = 0;
2757f8829a4aSRandall Stewart 
2758cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
2759d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
2760d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
2761d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2762d9c5cfeaSMichael Tuexen 		}
2763458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2764458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2765458303daSRandall Stewart 		*offset += sizeof(block);
2766458303daSRandall Stewart 		if (frag == NULL) {
2767cd554309SMichael Tuexen 			return (chunk_freed);
2768458303daSRandall Stewart 		}
2769f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
2770f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
2771d9c5cfeaSMichael Tuexen 
2772f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
2773d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
2774f8829a4aSRandall Stewart 			continue;
2775f8829a4aSRandall Stewart 		}
2776d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
2777d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
2778f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2779f8829a4aSRandall Stewart 		}
278020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
2781d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
2782f8829a4aSRandall Stewart 		}
2783cd554309SMichael Tuexen 		if (i < num_seg) {
2784cd554309SMichael Tuexen 			non_revocable = 0;
2785cd554309SMichael Tuexen 		} else {
2786cd554309SMichael Tuexen 			non_revocable = 1;
2787cd554309SMichael Tuexen 		}
2788cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
2789cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
27907215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
2791cd554309SMichael Tuexen 			chunk_freed = 1;
2792458303daSRandall Stewart 		}
2793d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
2794f8829a4aSRandall Stewart 	}
2795b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
279680fefe0aSRandall Stewart 		if (num_frs)
279780fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
279880fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
279980fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
280080fefe0aSRandall Stewart 	}
2801cd554309SMichael Tuexen 	return (chunk_freed);
2802f8829a4aSRandall Stewart }
2803f8829a4aSRandall Stewart 
2804f8829a4aSRandall Stewart static void
2805c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
2806c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
280763eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
2808f8829a4aSRandall Stewart {
2809f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
2810f8829a4aSRandall Stewart 
28114a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
281220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
2813f8829a4aSRandall Stewart 			/*
2814f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
2815f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
2816f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
2817f8829a4aSRandall Stewart 			 * again.
2818f8829a4aSRandall Stewart 			 */
281920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
2820d06c82f1SRandall Stewart 				break;
282120b07a4dSMichael Tuexen 			}
2822f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
2823f8829a4aSRandall Stewart 				/* it has been revoked */
2824f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
2825f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
2826a5d547adSRandall Stewart 				/*
282742551e99SRandall Stewart 				 * We must add this stuff back in to assure
282842551e99SRandall Stewart 				 * timers and such get started.
2829a5d547adSRandall Stewart 				 */
2830b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
2831c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
2832c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
2833c105859eSRandall Stewart 					    tp1->book_size,
2834c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
2835c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
283680fefe0aSRandall Stewart 				}
2837c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
2838c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
283942551e99SRandall Stewart 				/*
284042551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
284142551e99SRandall Stewart 				 * artificial inflation of the flight_size.
284242551e99SRandall Stewart 				 */
284342551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
2844b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
2845f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
2846f8829a4aSRandall Stewart 					    cumack,
2847f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
2848f8829a4aSRandall Stewart 					    0,
2849f8829a4aSRandall Stewart 					    0,
2850f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
285180fefe0aSRandall Stewart 				}
2852f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
2853f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
2854f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
2855f8829a4aSRandall Stewart 			}
2856f8829a4aSRandall Stewart 		}
2857f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
2858f8829a4aSRandall Stewart 			break;
2859f8829a4aSRandall Stewart 	}
2860f8829a4aSRandall Stewart }
2861f8829a4aSRandall Stewart 
2862830d754dSRandall Stewart 
2863f8829a4aSRandall Stewart static void
2864f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
286563eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
2866f8829a4aSRandall Stewart {
2867f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
2868f8829a4aSRandall Stewart 	int strike_flag = 0;
2869f8829a4aSRandall Stewart 	struct timeval now;
2870f8829a4aSRandall Stewart 	int tot_retrans = 0;
2871f8829a4aSRandall Stewart 	uint32_t sending_seq;
2872f8829a4aSRandall Stewart 	struct sctp_nets *net;
2873f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
2874f8829a4aSRandall Stewart 
2875f8829a4aSRandall Stewart 	/*
2876f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
2877f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
2878f8829a4aSRandall Stewart 	 */
2879f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
2880f8829a4aSRandall Stewart 	if (tp1 == NULL) {
2881f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
2882f8829a4aSRandall Stewart 	} else {
2883f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
2884f8829a4aSRandall Stewart 	}
2885f8829a4aSRandall Stewart 
2886f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
28877c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
288820083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
2889f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
2890f8829a4aSRandall Stewart 			if (net->saw_newack)
2891f8829a4aSRandall Stewart 				num_dests_sacked++;
2892f8829a4aSRandall Stewart 		}
2893f8829a4aSRandall Stewart 	}
2894f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
28956e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
2896f8829a4aSRandall Stewart 	}
28974a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
2898f8829a4aSRandall Stewart 		strike_flag = 0;
2899f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
2900f8829a4aSRandall Stewart 			/* this one had a timeout or something */
2901f8829a4aSRandall Stewart 			continue;
2902f8829a4aSRandall Stewart 		}
2903b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2904f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
2905f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
2906f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
2907f8829a4aSRandall Stewart 				    tp1->sent,
2908f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
290980fefe0aSRandall Stewart 		}
291020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
2911f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
2912f8829a4aSRandall Stewart 			/* done */
2913f8829a4aSRandall Stewart 			break;
2914f8829a4aSRandall Stewart 		}
2915f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
2916f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
2917f8829a4aSRandall Stewart 				/* Is it expired? */
291899ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
2919f8829a4aSRandall Stewart 					/* Yes so drop it */
2920f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
29211edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
29220c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
2923f8829a4aSRandall Stewart 					}
2924f8829a4aSRandall Stewart 					continue;
2925f8829a4aSRandall Stewart 				}
2926f8829a4aSRandall Stewart 			}
2927f8829a4aSRandall Stewart 		}
292820b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
2929f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
2930f8829a4aSRandall Stewart 			break;
2931f8829a4aSRandall Stewart 		}
2932f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
2933f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
2934f8829a4aSRandall Stewart 			/* skip */
293544fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
293644fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
293744fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
293844fbe462SRandall Stewart 			}
2939f8829a4aSRandall Stewart 			continue;
2940f8829a4aSRandall Stewart 		}
2941f8829a4aSRandall Stewart 		/*
2942f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
2943f8829a4aSRandall Stewart 		 */
2944ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
2945f8829a4aSRandall Stewart 			/*
2946f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
2947f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
2948f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
2949c105859eSRandall Stewart 			 * FR using this SACK.
2950f8829a4aSRandall Stewart 			 */
2951f8829a4aSRandall Stewart 			continue;
295220b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
295320b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
2954f8829a4aSRandall Stewart 			/*
2955f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
2956f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
2957f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
2958f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
2959f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
2960f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
2961f8829a4aSRandall Stewart 			 */
2962f8829a4aSRandall Stewart 			continue;
2963f8829a4aSRandall Stewart 		}
2964f8829a4aSRandall Stewart 		/*
2965f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
2966f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
2967f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
2968f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
2969f8829a4aSRandall Stewart 		 */
2970f8829a4aSRandall Stewart 		/*
297142551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
297242551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
297342551e99SRandall Stewart 		 * 0)) {
2974f8829a4aSRandall Stewart 		 */
297542551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
2976f8829a4aSRandall Stewart 			/*
2977f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
2978f8829a4aSRandall Stewart 			 * moved.
2979f8829a4aSRandall Stewart 			 */
2980b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
2981f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
2982f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
2983f8829a4aSRandall Stewart 				    tp1->sent,
2984f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
298580fefe0aSRandall Stewart 			}
29865e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2987f8829a4aSRandall Stewart 				tp1->sent++;
29885e54f665SRandall Stewart 			}
29897c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
299020083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
2991f8829a4aSRandall Stewart 				/*
2992f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
2993f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
2994f8829a4aSRandall Stewart 				 * because it would have been set to the
2995f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
2996f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
2997f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
2998c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
2999c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3000c105859eSRandall Stewart 				 * indicates that two packets have been
3001c105859eSRandall Stewart 				 * received after this missing TSN.
30025e54f665SRandall Stewart 				 */
30035e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
300420b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3005b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3006f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3007f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3008f8829a4aSRandall Stewart 						    tp1->sent,
3009f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
301080fefe0aSRandall Stewart 					}
3011f8829a4aSRandall Stewart 					tp1->sent++;
3012f8829a4aSRandall Stewart 				}
3013f8829a4aSRandall Stewart 			}
301420083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
301520083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3016f8829a4aSRandall Stewart 			/*
3017f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3018f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3019f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3020f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3021f8829a4aSRandall Stewart 			 */
30225e54f665SRandall Stewart 			if (
3023f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3024f8829a4aSRandall Stewart 			/*
3025f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3026f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3027f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3028f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3029f8829a4aSRandall Stewart 			 */
30305e54f665SRandall Stewart 			    (asoc->numnets < 2)
3031f8829a4aSRandall Stewart #else
30325e54f665SRandall Stewart 			    (1)
3033f8829a4aSRandall Stewart #endif
30345e54f665SRandall Stewart 			    ) {
30355e54f665SRandall Stewart 
303620b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3037f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3038f8829a4aSRandall Stewart 					/*
3039f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3040f8829a4aSRandall Stewart 					 * beyond where things were when we
3041f8829a4aSRandall Stewart 					 * did a FR.
3042f8829a4aSRandall Stewart 					 */
3043b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3044f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3045f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3046f8829a4aSRandall Stewart 						    tp1->sent,
3047f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
304880fefe0aSRandall Stewart 					}
30495e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3050f8829a4aSRandall Stewart 						tp1->sent++;
30515e54f665SRandall Stewart 					}
3052f8829a4aSRandall Stewart 					strike_flag = 1;
30537c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
305420083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3055f8829a4aSRandall Stewart 						/*
3056f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3057f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3058f8829a4aSRandall Stewart 						 * then lowest_newack test
3059f8829a4aSRandall Stewart 						 * will not pass because it
3060f8829a4aSRandall Stewart 						 * would have been set to
3061f8829a4aSRandall Stewart 						 * the cumack earlier. If
3062f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3063f8829a4aSRandall Stewart 						 * If not a mixed sack and
3064f8829a4aSRandall Stewart 						 * if tp1 is not between two
3065f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3066c105859eSRandall Stewart 						 * one more. NOTE that we
3067c105859eSRandall Stewart 						 * are marking by one
3068c105859eSRandall Stewart 						 * additional time since the
3069c105859eSRandall Stewart 						 * SACK DAC flag indicates
3070c105859eSRandall Stewart 						 * that two packets have
3071c105859eSRandall Stewart 						 * been received after this
3072c105859eSRandall Stewart 						 * missing TSN.
3073f8829a4aSRandall Stewart 						 */
30745e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
30755e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
307620b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
307720b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3078b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3079f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3080f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3081f8829a4aSRandall Stewart 								    tp1->sent,
3082f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
308380fefe0aSRandall Stewart 							}
30845e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3085f8829a4aSRandall Stewart 								tp1->sent++;
30865e54f665SRandall Stewart 							}
3087f8829a4aSRandall Stewart 						}
3088f8829a4aSRandall Stewart 					}
3089f8829a4aSRandall Stewart 				}
3090f8829a4aSRandall Stewart 			}
3091f8829a4aSRandall Stewart 			/*
309242551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
309342551e99SRandall Stewart 			 * algo covers HTNA.
3094f8829a4aSRandall Stewart 			 */
309520b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
309620b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3097f8829a4aSRandall Stewart 			/*
3098f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3099f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3100f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3101f8829a4aSRandall Stewart 			 */
3102f8829a4aSRandall Stewart 			;
3103f8829a4aSRandall Stewart 		} else {
3104f8829a4aSRandall Stewart 			/* Strike the TSN */
3105b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3106f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3107f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3108f8829a4aSRandall Stewart 				    tp1->sent,
3109f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
311080fefe0aSRandall Stewart 			}
31115e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3112f8829a4aSRandall Stewart 				tp1->sent++;
31135e54f665SRandall Stewart 			}
31147c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
311520083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3116f8829a4aSRandall Stewart 				/*
3117f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3118f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3119f8829a4aSRandall Stewart 				 * because it would have been set to the
3120f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3121f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3122f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3123c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3124c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3125c105859eSRandall Stewart 				 * indicates that two packets have been
3126c105859eSRandall Stewart 				 * received after this missing TSN.
3127f8829a4aSRandall Stewart 				 */
31285e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
312920b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3130b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3131f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3132f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3133f8829a4aSRandall Stewart 						    tp1->sent,
3134f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
313580fefe0aSRandall Stewart 					}
3136f8829a4aSRandall Stewart 					tp1->sent++;
3137f8829a4aSRandall Stewart 				}
3138f8829a4aSRandall Stewart 			}
3139f8829a4aSRandall Stewart 		}
3140f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3141f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3142f8829a4aSRandall Stewart 
3143544e35bdSRandall Stewart 			/* fix counts and things */
3144544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3145544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3146544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3147544e35bdSRandall Stewart 				    tp1->book_size,
3148544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3149544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3150544e35bdSRandall Stewart 			}
3151544e35bdSRandall Stewart 			if (tp1->whoTo) {
3152544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3153544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3154299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3155299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3156299108c5SRandall Stewart 					    tp1);
3157299108c5SRandall Stewart 				}
3158544e35bdSRandall Stewart 			}
3159544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3160544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3161544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3162544e35bdSRandall Stewart 			}
3163544e35bdSRandall Stewart 			/* add back to the rwnd */
3164544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3165544e35bdSRandall Stewart 
3166544e35bdSRandall Stewart 			/* remove from the total flight */
3167544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3168544e35bdSRandall Stewart 
3169475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3170475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3171475d0674SMichael Tuexen 				/*
3172475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3173475d0674SMichael Tuexen 				 * we store the retran count there.
3174475d0674SMichael Tuexen 				 */
3175475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3176475d0674SMichael Tuexen 					/* Yes, so drop it */
3177475d0674SMichael Tuexen 					if (tp1->data != NULL) {
31781edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3179475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3180475d0674SMichael Tuexen 					}
3181475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3182475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3183475d0674SMichael Tuexen 					continue;
3184475d0674SMichael Tuexen 				}
3185475d0674SMichael Tuexen 			}
3186cd3fd531SMichael Tuexen 			/*
3187cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3188cd3fd531SMichael Tuexen 			 * guy\n");
3189cd3fd531SMichael Tuexen 			 */
3190b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3191f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3192f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
319380fefe0aSRandall Stewart 			}
3194f8829a4aSRandall Stewart 			if (strike_flag) {
3195f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3196f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3197f8829a4aSRandall Stewart 			}
31985e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
31997c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3200f8829a4aSRandall Stewart 				/*
3201f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3202f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3203f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3204f8829a4aSRandall Stewart 				 */
3205f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3206f8829a4aSRandall Stewart 				alt = tp1->whoTo;
32073c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
320820083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3209b54d3a6cSRandall Stewart 					/*
3210b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3211b54d3a6cSRandall Stewart 					 * use the PF version of
3212b54d3a6cSRandall Stewart 					 * find_alt_net()
3213b54d3a6cSRandall Stewart 					 */
3214b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3215b54d3a6cSRandall Stewart 				} else {
3216b54d3a6cSRandall Stewart 					/*
3217b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3218b54d3a6cSRandall Stewart 					 * use the CMT version of
3219b54d3a6cSRandall Stewart 					 * find_alt_net()
3220b54d3a6cSRandall Stewart 					 */
322152be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3222f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3223b54d3a6cSRandall Stewart 				}
3224ad81507eSRandall Stewart 				if (alt == NULL) {
3225ad81507eSRandall Stewart 					alt = tp1->whoTo;
3226ad81507eSRandall Stewart 				}
3227f8829a4aSRandall Stewart 				/*
3228f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3229f8829a4aSRandall Stewart 				 * the retransmission, then new
3230f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3231f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3232f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3233f8829a4aSRandall Stewart 				 */
3234ad81507eSRandall Stewart 				if (tp1->whoTo) {
3235f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3236f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3237ad81507eSRandall Stewart 				}
3238f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3239f8829a4aSRandall Stewart 
3240f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3241f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3242f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3243f8829a4aSRandall Stewart #else
3244f8829a4aSRandall Stewart 				/*
3245f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3246f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3247f8829a4aSRandall Stewart 				 * paper details why.
3248f8829a4aSRandall Stewart 				 */
3249f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3250f8829a4aSRandall Stewart #endif
3251f8829a4aSRandall Stewart 			}
3252f8829a4aSRandall Stewart 
3253f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3254f8829a4aSRandall Stewart 			tot_retrans++;
3255f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3256f8829a4aSRandall Stewart 			/*
3257cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3258f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3259f8829a4aSRandall Stewart 			 */
3260f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3261f8829a4aSRandall Stewart 				/*
3262f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3263f8829a4aSRandall Stewart 				 * the next sequence number that will be
3264f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3265f8829a4aSRandall Stewart 				 * get the one we last sent.
3266f8829a4aSRandall Stewart 				 */
3267f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3268f8829a4aSRandall Stewart 			} else {
3269f8829a4aSRandall Stewart 				/*
3270f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3271f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3272f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3273f8829a4aSRandall Stewart 				 * take the first one (which will have the
3274f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3275f8829a4aSRandall Stewart 				 * one we last sent.
3276f8829a4aSRandall Stewart 				 */
3277f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3278f8829a4aSRandall Stewart 
3279f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3280f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3281f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3282f8829a4aSRandall Stewart 			}
3283f8829a4aSRandall Stewart 
3284f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3285f8829a4aSRandall Stewart 				/*
3286f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3287f8829a4aSRandall Stewart 				 * it, cancel it
3288f8829a4aSRandall Stewart 				 */
328960990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
329060990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3291f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3292f79aab18SRandall Stewart 				}
3293f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3294f8829a4aSRandall Stewart 			}
3295f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3296f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3297f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
32983c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3299f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3300f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3301f8829a4aSRandall Stewart 			}
3302f8829a4aSRandall Stewart 		}
33034a9ef3f8SMichael Tuexen 	}
3304f8829a4aSRandall Stewart }
3305f8829a4aSRandall Stewart 
3306f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3307f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3308f8829a4aSRandall Stewart     struct sctp_association *asoc)
3309f8829a4aSRandall Stewart {
3310f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3311f8829a4aSRandall Stewart 	struct timeval now;
3312f8829a4aSRandall Stewart 	int now_filled = 0;
3313f8829a4aSRandall Stewart 
3314f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3315f8829a4aSRandall Stewart 		return (NULL);
3316f8829a4aSRandall Stewart 	}
33174a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3318f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
331998f2956cSMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_RESEND &&
3320325c8c46SMichael Tuexen 		    tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3321f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3322f8829a4aSRandall Stewart 			break;
3323f8829a4aSRandall Stewart 		}
33240c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
33252a498584SMichael Tuexen 			if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3326325c8c46SMichael Tuexen 			    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
33270c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
33280c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
33290c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
33300c0982b8SRandall Stewart 			}
33310c0982b8SRandall Stewart 		}
3332f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3333f8829a4aSRandall Stewart 			/*
3334f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3335f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3336f8829a4aSRandall Stewart 			 */
3337f8829a4aSRandall Stewart 			break;
3338f8829a4aSRandall Stewart 		}
3339f8829a4aSRandall Stewart 		if (!now_filled) {
33406e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3341f8829a4aSRandall Stewart 			now_filled = 1;
3342f8829a4aSRandall Stewart 		}
3343f8829a4aSRandall Stewart 		/*
3344f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3345f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3346f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3347f8829a4aSRandall Stewart 		 * it if its a resend?
3348f8829a4aSRandall Stewart 		 */
3349f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3350f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3351f8829a4aSRandall Stewart 			/*
3352f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3353f8829a4aSRandall Stewart 			 * now up?
3354f8829a4aSRandall Stewart 			 */
3355f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3356f8829a4aSRandall Stewart 				/* Yes so drop it */
3357f8829a4aSRandall Stewart 				if (tp1->data) {
3358ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
33591edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3360f8829a4aSRandall Stewart 				}
3361f8829a4aSRandall Stewart 			} else {
3362f8829a4aSRandall Stewart 				/*
3363f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3364f8829a4aSRandall Stewart 				 * whos time as not expired.
3365f8829a4aSRandall Stewart 				 */
3366f8829a4aSRandall Stewart 				break;
3367f8829a4aSRandall Stewart 			}
3368f8829a4aSRandall Stewart 		}
3369f8829a4aSRandall Stewart 		/*
3370f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3371f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3372f8829a4aSRandall Stewart 		 * the next chunk.
3373f8829a4aSRandall Stewart 		 */
337498f2956cSMichael Tuexen 		if ((tp1->sent == SCTP_FORWARD_TSN_SKIP) ||
3375325c8c46SMichael Tuexen 		    (tp1->sent == SCTP_DATAGRAM_NR_ACKED)) {
3376f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
337720b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3378f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3379f8829a4aSRandall Stewart 				a_adv = tp1;
33800c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
33810c0982b8SRandall Stewart 				/* No update but we do save the chk */
33820c0982b8SRandall Stewart 				a_adv = tp1;
33830c0982b8SRandall Stewart 			}
3384f8829a4aSRandall Stewart 		} else {
3385f8829a4aSRandall Stewart 			/*
3386f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3387f8829a4aSRandall Stewart 			 * further
3388f8829a4aSRandall Stewart 			 */
3389f8829a4aSRandall Stewart 			break;
3390f8829a4aSRandall Stewart 		}
3391f8829a4aSRandall Stewart 	}
3392f8829a4aSRandall Stewart 	return (a_adv);
3393f8829a4aSRandall Stewart }
3394f8829a4aSRandall Stewart 
33950c0982b8SRandall Stewart static int
3396c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3397bff64a4dSRandall Stewart {
3398bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3399bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
34000c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
34010c0982b8SRandall Stewart 
34020c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
34030c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
34040c0982b8SRandall Stewart 	ret = 0;
34050c0982b8SRandall Stewart 
34060c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
34070c0982b8SRandall Stewart 		return (0);
3408bff64a4dSRandall Stewart 
3409bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3410bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3411cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n",
34120c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
34130c0982b8SRandall Stewart 			    chk->send_size,
3414cd3fd531SMichael Tuexen 			    chk->snd_count);
3415bff64a4dSRandall Stewart 			inflight++;
3416bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3417bff64a4dSRandall Stewart 			resend++;
3418bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3419bff64a4dSRandall Stewart 			inbetween++;
3420bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3421bff64a4dSRandall Stewart 			above++;
3422bff64a4dSRandall Stewart 		} else {
3423bff64a4dSRandall Stewart 			acked++;
3424bff64a4dSRandall Stewart 		}
3425bff64a4dSRandall Stewart 	}
3426f1f73e57SRandall Stewart 
3427c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3428f1f73e57SRandall Stewart #ifdef INVARIANTS
3429c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3430f1f73e57SRandall Stewart #else
3431cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n",
34320c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
34330c0982b8SRandall Stewart 
34340c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
34350c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
34360c0982b8SRandall Stewart 		ret = 1;
3437f1f73e57SRandall Stewart #endif
3438bff64a4dSRandall Stewart 	}
34390c0982b8SRandall Stewart 	return (ret);
3440c105859eSRandall Stewart }
3441c105859eSRandall Stewart 
3442c105859eSRandall Stewart 
3443c105859eSRandall Stewart static void
3444c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3445c105859eSRandall Stewart     struct sctp_association *asoc,
3446c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3447c105859eSRandall Stewart {
3448dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
34495171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3450dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3451dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3452dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3453dfb11ef8SRandall Stewart 		    tp1->book_size,
3454dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3455dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3456dfb11ef8SRandall Stewart 		return;
3457dfb11ef8SRandall Stewart 	}
34585171328bSRandall Stewart 	/* First setup this by shrinking flight */
3459299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3460299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3461299108c5SRandall Stewart 		    tp1);
3462299108c5SRandall Stewart 	}
34635171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
34645171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
34655171328bSRandall Stewart 	/* Now mark for resend */
34665171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3467791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3468791437b5SRandall Stewart 
3469b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3470c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3471c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3472c105859eSRandall Stewart 		    tp1->book_size,
3473c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3474c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
347580fefe0aSRandall Stewart 	}
3476c105859eSRandall Stewart }
3477c105859eSRandall Stewart 
3478f8829a4aSRandall Stewart void
3479f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3480899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3481f8829a4aSRandall Stewart {
3482f8829a4aSRandall Stewart 	struct sctp_nets *net;
3483f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3484f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
34855e54f665SRandall Stewart 	uint32_t old_rwnd;
34865e54f665SRandall Stewart 	int win_probe_recovery = 0;
3487c105859eSRandall Stewart 	int win_probe_recovered = 0;
3488d06c82f1SRandall Stewart 	int j, done_once = 0;
3489f79aab18SRandall Stewart 	int rto_ok = 1;
3490f8829a4aSRandall Stewart 
3491b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3492d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3493d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
349480fefe0aSRandall Stewart 	}
3495f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
349618e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
349718e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
349818e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
349918e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
350018e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
350118e198d3SRandall Stewart 	}
350218e198d3SRandall Stewart #endif
3503f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3504d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
350520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
35065e54f665SRandall Stewart 		/* old ack */
35075e54f665SRandall Stewart 		return;
3508d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3509d06c82f1SRandall Stewart 		/* Window update sack */
3510d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
351144fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3512d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3513d06c82f1SRandall Stewart 			/* SWS sender side engages */
3514d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3515d06c82f1SRandall Stewart 		}
3516d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3517d06c82f1SRandall Stewart 			goto again;
3518d06c82f1SRandall Stewart 		}
3519d06c82f1SRandall Stewart 		return;
35205e54f665SRandall Stewart 	}
3521f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3522f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3523a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3524a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3525a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3526a21779f0SRandall Stewart 		}
3527f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3528f8829a4aSRandall Stewart 		net->net_ack = 0;
3529f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3530132dea7dSRandall Stewart 
3531132dea7dSRandall Stewart 		/*
3532132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3533132dea7dSRandall Stewart 		 * SACK processing
3534132dea7dSRandall Stewart 		 */
3535132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3536132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3537299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3538299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3539299108c5SRandall Stewart 		}
3540f8829a4aSRandall Stewart 	}
3541b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3542139bc87fSRandall Stewart 		uint32_t send_s;
3543139bc87fSRandall Stewart 
3544c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3545c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3546c105859eSRandall Stewart 			    sctpchunk_listhead);
3547c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3548139bc87fSRandall Stewart 		} else {
3549c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3550139bc87fSRandall Stewart 		}
355120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3552c105859eSRandall Stewart #ifndef INVARIANTS
3553*ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
3554*ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
3555139bc87fSRandall Stewart 
3556c105859eSRandall Stewart #endif
3557c105859eSRandall Stewart #ifdef INVARIANTS
3558c105859eSRandall Stewart 			panic("Impossible sack 1");
3559c105859eSRandall Stewart #else
3560b5c16493SMichael Tuexen 
3561139bc87fSRandall Stewart 			*abort_now = 1;
3562139bc87fSRandall Stewart 			/* XXX */
3563*ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal then TSN %8.8x",
3564*ff1ffd74SMichael Tuexen 			    cumack, send_s);
3565*ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
3566139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3567*ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3568139bc87fSRandall Stewart 			return;
3569139bc87fSRandall Stewart #endif
3570139bc87fSRandall Stewart 		}
3571139bc87fSRandall Stewart 	}
3572f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3573b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3574c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3575c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3576c4739e2fSRandall Stewart 		    0,
3577c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3578c4739e2fSRandall Stewart 		    __LINE__);
3579c4739e2fSRandall Stewart 	}
3580f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
358120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3582f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
35834a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
358420b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
358518e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3586cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
358718e198d3SRandall Stewart 				}
3588f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3589f8829a4aSRandall Stewart 					/*
359018e198d3SRandall Stewart 					 * If it is less than ACKED, it is
359118e198d3SRandall Stewart 					 * now no-longer in flight. Higher
359218e198d3SRandall Stewart 					 * values may occur during marking
3593f8829a4aSRandall Stewart 					 */
3594c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3595b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3596c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3597a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3598a5d547adSRandall Stewart 							    tp1->book_size,
3599c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3600a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
360180fefe0aSRandall Stewart 						}
3602c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3603299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3604299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3605299108c5SRandall Stewart 							    tp1);
3606299108c5SRandall Stewart 						}
360704ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3608c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3609f8829a4aSRandall Stewart 					}
3610f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3611f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3612f8829a4aSRandall Stewart 						/*
361318e198d3SRandall Stewart 						 * True non-retransmited
3614f8829a4aSRandall Stewart 						 * chunk
3615f8829a4aSRandall Stewart 						 */
3616f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3617f8829a4aSRandall Stewart 						    tp1->send_size;
3618f8829a4aSRandall Stewart 
3619f8829a4aSRandall Stewart 						/* update RTO too? */
362062c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3621f79aab18SRandall Stewart 							if (rto_ok) {
3622f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
362304ee05e8SRandall Stewart 								/*
362404ee05e8SRandall Stewart 								 * sa_ignore
3625f79aab18SRandall Stewart 								 * NO_NULL_CH
3626f79aab18SRandall Stewart 								 * K
362704ee05e8SRandall Stewart 								 */
3628f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3629f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
363018e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3631899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3632f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3633f79aab18SRandall Stewart 								rto_ok = 0;
3634f79aab18SRandall Stewart 							}
3635f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3636f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3637f79aab18SRandall Stewart 							}
3638f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3639f8829a4aSRandall Stewart 						}
3640f8829a4aSRandall Stewart 					}
3641132dea7dSRandall Stewart 					/*
364218e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
364318e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
364418e198d3SRandall Stewart 					 * acked for the first time, set the
364518e198d3SRandall Stewart 					 * following variables for the
364618e198d3SRandall Stewart 					 * corresp destination.
364718e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
364818e198d3SRandall Stewart 					 * cwnd update.
364918e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
365018e198d3SRandall Stewart 					 * trigger search for the next
365118e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3652132dea7dSRandall Stewart 					 */
3653132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3654132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3655132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3656132dea7dSRandall Stewart 
3657b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
365804ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3659f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
366080fefe0aSRandall Stewart 					}
3661f8829a4aSRandall Stewart 				}
3662f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3663f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3664f8829a4aSRandall Stewart 				}
366542551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
366642551e99SRandall Stewart 					/* deflate the cwnd */
366742551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
366842551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
366942551e99SRandall Stewart 				}
3670325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
3671a7ad6026SMichael Tuexen 					if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
3672a7ad6026SMichael Tuexen 						asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
3673a7ad6026SMichael Tuexen #ifdef INVARIANTS
3674a7ad6026SMichael Tuexen 					} else {
3675a7ad6026SMichael Tuexen 						panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
3676a7ad6026SMichael Tuexen #endif
3677a7ad6026SMichael Tuexen 					}
3678a7ad6026SMichael Tuexen 				}
3679f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3680f8829a4aSRandall Stewart 				if (tp1->data) {
368104ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3682f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3683f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
36844a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3685f8829a4aSRandall Stewart 				}
3686b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3687f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3688f8829a4aSRandall Stewart 					    cumack,
3689f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3690f8829a4aSRandall Stewart 					    0,
3691f8829a4aSRandall Stewart 					    0,
3692f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
369380fefe0aSRandall Stewart 				}
3694f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3695689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
369618e198d3SRandall Stewart 			} else {
369718e198d3SRandall Stewart 				break;
3698f8829a4aSRandall Stewart 			}
36995e54f665SRandall Stewart 		}
370018e198d3SRandall Stewart 
370118e198d3SRandall Stewart 	}
370204ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3703f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3704ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3705ceaad40aSRandall Stewart 		struct socket *so;
3706ceaad40aSRandall Stewart 
3707ceaad40aSRandall Stewart #endif
3708f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3709b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
371004ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
37117215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
371280fefe0aSRandall Stewart 		}
3713ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3714ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3715ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3716ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3717ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3718ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3719ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3720ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3721ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
3722ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3723ceaad40aSRandall Stewart 			return;
3724ceaad40aSRandall Stewart 		}
3725ceaad40aSRandall Stewart #endif
3726f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
3727ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3728ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
3729ceaad40aSRandall Stewart #endif
3730f8829a4aSRandall Stewart 	} else {
3731b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
37327215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
373380fefe0aSRandall Stewart 		}
3734f8829a4aSRandall Stewart 	}
3735f8829a4aSRandall Stewart 
3736b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
3737ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
3738ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3739ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
3740ca85e948SMichael Tuexen 				/*
3741ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
3742ca85e948SMichael Tuexen 				 * count, this is optional.
3743ca85e948SMichael Tuexen 				 */
3744ca85e948SMichael Tuexen 				net->error_count = 0;
3745ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
3746ca85e948SMichael Tuexen 					/* addr came good */
3747ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
3748ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
37494b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
3750ca85e948SMichael Tuexen 				}
3751ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
3752ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
3753ca85e948SMichael Tuexen 						/*
3754ca85e948SMichael Tuexen 						 * release the alternate,
3755ca85e948SMichael Tuexen 						 * primary is good
3756ca85e948SMichael Tuexen 						 */
3757ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
3758ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
3759ca85e948SMichael Tuexen 					}
3760ca85e948SMichael Tuexen 				}
3761ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
3762ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
3763ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
3764ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
3765ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
3766ca85e948SMichael Tuexen 					/* Done with this net */
3767ca85e948SMichael Tuexen 					net->net_ack = 0;
3768ca85e948SMichael Tuexen 				}
3769ca85e948SMichael Tuexen 				/* restore any doubled timers */
3770ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3771ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
3772ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
3773ca85e948SMichael Tuexen 				}
3774ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
3775ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
3776ca85e948SMichael Tuexen 				}
3777ca85e948SMichael Tuexen 			}
3778ca85e948SMichael Tuexen 		}
3779b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
3780ca85e948SMichael Tuexen 	}
3781f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
37825e54f665SRandall Stewart 
3783f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
3784f8829a4aSRandall Stewart 		/* nothing left in-flight */
3785f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3786f8829a4aSRandall Stewart 			net->flight_size = 0;
3787f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
3788f8829a4aSRandall Stewart 		}
3789f8829a4aSRandall Stewart 		asoc->total_flight = 0;
3790f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
3791f8829a4aSRandall Stewart 	}
3792f8829a4aSRandall Stewart 	/* RWND update */
3793f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
379444fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3795f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3796f8829a4aSRandall Stewart 		/* SWS sender side engages */
3797f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
3798f8829a4aSRandall Stewart 	}
37995e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
38005e54f665SRandall Stewart 		win_probe_recovery = 1;
38015e54f665SRandall Stewart 	}
3802f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
3803a5d547adSRandall Stewart again:
3804a5d547adSRandall Stewart 	j = 0;
3805f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
38065171328bSRandall Stewart 		int to_ticks;
38075171328bSRandall Stewart 
38085e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
3809c105859eSRandall Stewart 			win_probe_recovered = 1;
38105e54f665SRandall Stewart 			/*
38115e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
38125e54f665SRandall Stewart 			 * and clear the sent
38135e54f665SRandall Stewart 			 */
38143c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
38155e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
38165e54f665SRandall Stewart 				if (tp1->window_probe) {
3817cd554309SMichael Tuexen 					/* move back to data send queue */
38187215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
38195e54f665SRandall Stewart 					break;
38205e54f665SRandall Stewart 				}
38215e54f665SRandall Stewart 			}
38225e54f665SRandall Stewart 		}
3823f8829a4aSRandall Stewart 		if (net->RTO == 0) {
3824f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
3825f8829a4aSRandall Stewart 		} else {
3826f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
3827f8829a4aSRandall Stewart 		}
38285171328bSRandall Stewart 		if (net->flight_size) {
3829a5d547adSRandall Stewart 			j++;
3830ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
3831f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
38325171328bSRandall Stewart 			if (net->window_probe) {
38335171328bSRandall Stewart 				net->window_probe = 0;
38345171328bSRandall Stewart 			}
3835f8829a4aSRandall Stewart 		} else {
38365171328bSRandall Stewart 			if (net->window_probe) {
38375171328bSRandall Stewart 				/*
38385171328bSRandall Stewart 				 * In window probes we must assure a timer
38395171328bSRandall Stewart 				 * is still running there
38405171328bSRandall Stewart 				 */
38415171328bSRandall Stewart 				net->window_probe = 0;
38425171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
38435171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
38445171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
38455171328bSRandall Stewart 				}
38465171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
3847f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3848a5d547adSRandall Stewart 				    stcb, net,
3849a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
3850f8829a4aSRandall Stewart 			}
3851f8829a4aSRandall Stewart 		}
3852f8829a4aSRandall Stewart 	}
3853bff64a4dSRandall Stewart 	if ((j == 0) &&
3854bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
3855bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
3856c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
3857bff64a4dSRandall Stewart 	    (done_once == 0)) {
38580c0982b8SRandall Stewart 		/*
38590c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
38600c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
38610c0982b8SRandall Stewart 		 */
38620c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
3863a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3864a5d547adSRandall Stewart 				net->flight_size = 0;
3865a5d547adSRandall Stewart 			}
3866a5d547adSRandall Stewart 			asoc->total_flight = 0;
3867a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
3868a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
3869a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3870a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3871c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
3872c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
3873a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3874791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3875a5d547adSRandall Stewart 				}
3876a5d547adSRandall Stewart 			}
38770c0982b8SRandall Stewart 		}
3878bff64a4dSRandall Stewart 		done_once = 1;
3879a5d547adSRandall Stewart 		goto again;
3880a5d547adSRandall Stewart 	}
3881f8829a4aSRandall Stewart 	/**********************************/
3882f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
3883f8829a4aSRandall Stewart 	/**********************************/
3884f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
3885f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
3886f8829a4aSRandall Stewart 		/* clean up */
3887f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
3888f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
3889f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
3890f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
3891f8829a4aSRandall Stewart 		    ) {
3892f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
3893f8829a4aSRandall Stewart 
3894f8829a4aSRandall Stewart 			/*
3895f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
3896f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
3897f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
3898f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
3899f8829a4aSRandall Stewart 			 */
3900f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
3901f8829a4aSRandall Stewart 			    sctp_streamhead);
39022afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
39032afb3e84SRandall Stewart 				/* Let cleanup code purge it */
39042afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
39052afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
39062afb3e84SRandall Stewart 				} else {
3907f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3908f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
3909f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
3910f8829a4aSRandall Stewart 				}
3911f8829a4aSRandall Stewart 			}
39122afb3e84SRandall Stewart 		}
3913f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
3914f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
3915f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
3916f8829a4aSRandall Stewart 				/* Need to abort here */
3917*ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
3918f8829a4aSRandall Stewart 
3919f8829a4aSRandall Stewart 		abort_out_now:
3920f8829a4aSRandall Stewart 				*abort_now = 1;
3921f8829a4aSRandall Stewart 				/* XXX */
3922*ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3923a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
3924*ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
3925f8829a4aSRandall Stewart 			} else {
3926ca85e948SMichael Tuexen 				struct sctp_nets *netp;
3927ca85e948SMichael Tuexen 
3928f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3929f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3930f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3931f42a358aSRandall Stewart 				}
3932c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
3933b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
3934f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
3935ca85e948SMichael Tuexen 				if (asoc->alternate) {
3936ca85e948SMichael Tuexen 					netp = asoc->alternate;
3937ca85e948SMichael Tuexen 				} else {
3938ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
3939ca85e948SMichael Tuexen 				}
3940ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
3941f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
3942ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
3943f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
3944ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
3945f8829a4aSRandall Stewart 			}
3946f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
3947f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
3948ca85e948SMichael Tuexen 			struct sctp_nets *netp;
3949ca85e948SMichael Tuexen 
3950f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
3951f8829a4aSRandall Stewart 				goto abort_out_now;
3952f8829a4aSRandall Stewart 			}
3953f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3954c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
3955b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
395612af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
3957c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
3958c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
3959c39cfa1fSMichael Tuexen 			} else {
3960c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
3961c39cfa1fSMichael Tuexen 			}
3962c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
3963f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
3964ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
3965f8829a4aSRandall Stewart 		}
3966f8829a4aSRandall Stewart 	}
3967dfb11ef8SRandall Stewart 	/*********************************************/
3968dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
3969dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
3970dfb11ef8SRandall Stewart 	/*********************************************/
3971dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
397220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
3973dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
3974dfb11ef8SRandall Stewart 	}
3975830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
3976830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
3977830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
3978830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
3979830d754dSRandall Stewart 
3980830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
3981830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
3982830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
398320b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
3984830d754dSRandall Stewart 			/*
3985493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
3986830d754dSRandall Stewart 			 */
398720b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
3988830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
39890c0982b8SRandall Stewart 			} else if (lchk) {
39900c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
399144fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
39920c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
39930c0982b8SRandall Stewart 				}
3994830d754dSRandall Stewart 			}
3995830d754dSRandall Stewart 		}
3996830d754dSRandall Stewart 		if (lchk) {
3997830d754dSRandall Stewart 			/* Assure a timer is up */
3998830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
3999830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4000830d754dSRandall Stewart 		}
4001830d754dSRandall Stewart 	}
4002b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4003f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4004f8829a4aSRandall Stewart 		    rwnd,
4005f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4006f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4007f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
400880fefe0aSRandall Stewart 	}
4009f8829a4aSRandall Stewart }
4010f8829a4aSRandall Stewart 
4011f8829a4aSRandall Stewart void
4012cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
40137215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4014cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4015cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4016899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4017f8829a4aSRandall Stewart {
4018f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4019f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4020cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4021f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4022c105859eSRandall Stewart 	uint32_t send_s = 0;
4023f8829a4aSRandall Stewart 	long j;
4024f8829a4aSRandall Stewart 	int accum_moved = 0;
4025f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
40265e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
40275e54f665SRandall Stewart 	int win_probe_recovery = 0;
4028c105859eSRandall Stewart 	int win_probe_recovered = 0;
4029f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4030bff64a4dSRandall Stewart 	int done_once;
4031f79aab18SRandall Stewart 	int rto_ok = 1;
4032f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4033f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4034f8829a4aSRandall Stewart 
4035f8829a4aSRandall Stewart 	/*
4036f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4037f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4038f8829a4aSRandall Stewart 	 */
4039f8829a4aSRandall Stewart 	/*
4040f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4041f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4042f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4043f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4044f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4045f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4046f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4047f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4048f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4049f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4050f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4051f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4052f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4053f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4054f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4055f8829a4aSRandall Stewart 	 */
4056f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4057f8829a4aSRandall Stewart 	/* CMT DAC algo */
4058f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4059f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4060cd554309SMichael Tuexen 	last_tsn = cum_ack;
4061cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
406218e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
406318e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
406418e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
406518e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
406618e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
406718e198d3SRandall Stewart 	}
406818e198d3SRandall Stewart #endif
4069d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4070f8829a4aSRandall Stewart 
4071cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4072cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4073cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4074cd554309SMichael Tuexen 	}
40755e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4076b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4077c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4078c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4079c4739e2fSRandall Stewart 		    0,
4080c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4081c4739e2fSRandall Stewart 		    __LINE__);
4082c4739e2fSRandall Stewart 	}
4083f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4084f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4085b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4086f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4087f8829a4aSRandall Stewart 		    cum_ack,
4088f8829a4aSRandall Stewart 		    0,
4089f8829a4aSRandall Stewart 		    num_seg,
4090f8829a4aSRandall Stewart 		    num_dup,
4091f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
409280fefe0aSRandall Stewart 	}
4093ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4094cd554309SMichael Tuexen 		uint16_t i;
4095458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4096f8829a4aSRandall Stewart 
4097cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4098cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4099458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4100cd554309SMichael Tuexen 			if (dupdata == NULL) {
4101458303daSRandall Stewart 				break;
4102458303daSRandall Stewart 			}
4103cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4104f8829a4aSRandall Stewart 		}
4105f8829a4aSRandall Stewart 	}
4106b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4107c105859eSRandall Stewart 		/* reality check */
4108c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4109c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4110c105859eSRandall Stewart 			    sctpchunk_listhead);
4111c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4112c105859eSRandall Stewart 		} else {
4113b5c16493SMichael Tuexen 			tp1 = NULL;
4114c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4115c105859eSRandall Stewart 		}
411620b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4117*ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
4118*ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
4119c105859eSRandall Stewart 
4120f8829a4aSRandall Stewart 			/*
4121f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4122f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4123f8829a4aSRandall Stewart 			 */
4124cd3fd531SMichael Tuexen 			SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4125b5c16493SMichael Tuexen 			    cum_ack, send_s);
4126b5c16493SMichael Tuexen 			if (tp1) {
4127cd3fd531SMichael Tuexen 				SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n",
4128dd294dceSMichael Tuexen 				    tp1->rec.data.TSN_seq, (void *)tp1);
4129b5c16493SMichael Tuexen 			}
4130f8829a4aSRandall Stewart 	hopeless_peer:
4131f8829a4aSRandall Stewart 			*abort_now = 1;
4132f8829a4aSRandall Stewart 			/* XXX */
4133*ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg), "Cum ack %8.8x greater or equal then TSN %8.8x",
4134*ff1ffd74SMichael Tuexen 			    cum_ack, send_s);
4135*ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
4136a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4137*ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4138f8829a4aSRandall Stewart 			return;
4139f8829a4aSRandall Stewart 		}
4140f8829a4aSRandall Stewart 	}
4141f8829a4aSRandall Stewart 	/**********************/
4142f8829a4aSRandall Stewart 	/* 1) check the range */
4143f8829a4aSRandall Stewart 	/**********************/
414420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4145f8829a4aSRandall Stewart 		/* acking something behind */
4146f8829a4aSRandall Stewart 		return;
4147f8829a4aSRandall Stewart 	}
4148f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4149f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4150f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4151cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4152f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4153b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4154f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4155f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
415680fefe0aSRandall Stewart 		}
4157f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4158f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4159f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4160f8829a4aSRandall Stewart 		}
4161f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4162f8829a4aSRandall Stewart 			/* SWS sender side engages */
4163f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4164f8829a4aSRandall Stewart 		}
4165f8829a4aSRandall Stewart 		/* stop any timers */
4166f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4167f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4168a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4169f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4170f8829a4aSRandall Stewart 			net->flight_size = 0;
4171f8829a4aSRandall Stewart 		}
4172f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4173f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4174f8829a4aSRandall Stewart 		return;
4175f8829a4aSRandall Stewart 	}
4176f8829a4aSRandall Stewart 	/*
4177f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4178f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4179f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4180f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4181f8829a4aSRandall Stewart 	 * destination address basis.
4182f8829a4aSRandall Stewart 	 */
4183f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4184a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4185a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4186a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4187a21779f0SRandall Stewart 		}
4188f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4189f8829a4aSRandall Stewart 		net->net_ack = 0;
4190f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4191f8829a4aSRandall Stewart 
4192f8829a4aSRandall Stewart 		/*
419342551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
419442551e99SRandall Stewart 		 * SACK processing
4195f8829a4aSRandall Stewart 		 */
4196f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4197f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4198299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4199299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4200299108c5SRandall Stewart 		}
4201f8829a4aSRandall Stewart 	}
4202f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
42034a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
420420b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4205f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4206f8829a4aSRandall Stewart 				accum_moved = 1;
4207f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4208f8829a4aSRandall Stewart 					/*
4209f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4210f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4211f8829a4aSRandall Stewart 					 * values may occur during marking
4212f8829a4aSRandall Stewart 					 */
4213f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4214f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4215f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4216f8829a4aSRandall Stewart 						/*
4217f8829a4aSRandall Stewart 						 * If there was no retran
4218f8829a4aSRandall Stewart 						 * and the address is
4219f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4220f8829a4aSRandall Stewart 						 * there and are now
4221f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4222f8829a4aSRandall Stewart 						 * mark it so.
4223f8829a4aSRandall Stewart 						 */
4224f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4225f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4226f8829a4aSRandall Stewart 					}
4227c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4228b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4229c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4230a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4231a5d547adSRandall Stewart 							    tp1->book_size,
4232c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4233a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
423480fefe0aSRandall Stewart 						}
4235c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4236c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4237299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4238299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4239299108c5SRandall Stewart 							    tp1);
4240299108c5SRandall Stewart 						}
4241f8829a4aSRandall Stewart 					}
4242f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4243f8829a4aSRandall Stewart 
4244f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4245f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4246f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4247f8829a4aSRandall Stewart 
4248f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4249f8829a4aSRandall Stewart 						/*
4250f8829a4aSRandall Stewart 						 * True non-retransmited
4251f8829a4aSRandall Stewart 						 * chunk
4252f8829a4aSRandall Stewart 						 */
4253f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4254f8829a4aSRandall Stewart 						    tp1->send_size;
4255f8829a4aSRandall Stewart 
4256f8829a4aSRandall Stewart 						/* update RTO too? */
4257f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4258f79aab18SRandall Stewart 							if (rto_ok) {
4259f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4260f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4261f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
426218e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4263899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4264f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4265f79aab18SRandall Stewart 								rto_ok = 0;
4266f79aab18SRandall Stewart 							}
4267f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4268f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4269f79aab18SRandall Stewart 							}
4270f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4271f8829a4aSRandall Stewart 						}
4272f8829a4aSRandall Stewart 					}
4273f8829a4aSRandall Stewart 					/*
4274f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4275f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4276f8829a4aSRandall Stewart 					 * acked for the first time, set the
4277f8829a4aSRandall Stewart 					 * following variables for the
4278f8829a4aSRandall Stewart 					 * corresp destination.
4279f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4280f8829a4aSRandall Stewart 					 * cwnd update.
4281f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4282f8829a4aSRandall Stewart 					 * trigger search for the next
4283f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4284f8829a4aSRandall Stewart 					 */
4285f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4286f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4287f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4288f8829a4aSRandall Stewart 
4289f8829a4aSRandall Stewart 
4290b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4291f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4292f8829a4aSRandall Stewart 						    cum_ack,
4293f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4294f8829a4aSRandall Stewart 						    0,
4295f8829a4aSRandall Stewart 						    0,
4296f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
429780fefe0aSRandall Stewart 					}
4298b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4299f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
430080fefe0aSRandall Stewart 					}
4301f8829a4aSRandall Stewart 				}
4302f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4303f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4304f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4305f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4306f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4307f8829a4aSRandall Stewart #endif
4308f8829a4aSRandall Stewart 				}
430942551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
431042551e99SRandall Stewart 					/* deflate the cwnd */
431142551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
431242551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
431342551e99SRandall Stewart 				}
4314325c8c46SMichael Tuexen 				if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4315f8829a4aSRandall Stewart 					tp1->sent = SCTP_DATAGRAM_ACKED;
4316f8829a4aSRandall Stewart 				}
4317325c8c46SMichael Tuexen 			}
4318f8829a4aSRandall Stewart 		} else {
4319f8829a4aSRandall Stewart 			break;
4320f8829a4aSRandall Stewart 		}
4321f8829a4aSRandall Stewart 	}
4322f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4323f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4324f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4325f8829a4aSRandall Stewart 
4326cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4327f8829a4aSRandall Stewart 
4328f8829a4aSRandall Stewart 		/*
4329f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4330f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4331f8829a4aSRandall Stewart 		 * for all dests.
4332f8829a4aSRandall Stewart 		 */
4333f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4334f8829a4aSRandall Stewart 			net->saw_newack = 0;
4335f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4336f8829a4aSRandall Stewart 		}
4337f8829a4aSRandall Stewart 
4338f8829a4aSRandall Stewart 		/*
4339f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4340f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4341f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4342f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4343f8829a4aSRandall Stewart 		 */
4344cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4345cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
43467215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4347cd554309SMichael Tuexen 			wake_him++;
4348cd554309SMichael Tuexen 		}
4349b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4350f8829a4aSRandall Stewart 			/*
4351f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4352f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4353f8829a4aSRandall Stewart 			 */
435420b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4355f8829a4aSRandall Stewart 				/*
4356f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4357f8829a4aSRandall Stewart 				 * attack. We must abort.
4358f8829a4aSRandall Stewart 				 */
4359cd3fd531SMichael Tuexen 				SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4360cd3fd531SMichael Tuexen 				    biggest_tsn_acked, send_s);
4361f8829a4aSRandall Stewart 				goto hopeless_peer;
4362f8829a4aSRandall Stewart 			}
4363f8829a4aSRandall Stewart 		}
4364f8829a4aSRandall Stewart 	}
4365f8829a4aSRandall Stewart 	/*******************************************/
4366f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4367f8829a4aSRandall Stewart 	/*******************************************/
43687c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4369f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4370f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4371f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4372a5d547adSRandall Stewart 				    stcb, net,
4373a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4374f8829a4aSRandall Stewart 
4375f8829a4aSRandall Stewart 		}
4376f8829a4aSRandall Stewart 	} else {
4377f8829a4aSRandall Stewart 		if (accum_moved) {
4378f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4379f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4380a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4381f8829a4aSRandall Stewart 			}
4382f8829a4aSRandall Stewart 		}
4383f8829a4aSRandall Stewart 	}
4384f8829a4aSRandall Stewart 	/********************************************/
4385d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4386f8829a4aSRandall Stewart 	/********************************************/
4387f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4388f8829a4aSRandall Stewart 
43897c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
439020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4391f8829a4aSRandall Stewart 			break;
4392f8829a4aSRandall Stewart 		}
4393325c8c46SMichael Tuexen 		if (tp1->sent != SCTP_DATAGRAM_NR_ACKED) {
4394a7ad6026SMichael Tuexen 			if (asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues > 0) {
4395a7ad6026SMichael Tuexen 				asoc->strmout[tp1->rec.data.stream_number].chunks_on_queues--;
4396a7ad6026SMichael Tuexen #ifdef INVARIANTS
4397a7ad6026SMichael Tuexen 			} else {
4398a7ad6026SMichael Tuexen 				panic("No chunks on the queues for sid %u.", tp1->rec.data.stream_number);
4399a7ad6026SMichael Tuexen #endif
4400a7ad6026SMichael Tuexen 			}
4401f8829a4aSRandall Stewart 		}
4402f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
44030ddb4299SMichael Tuexen 		if (PR_SCTP_ENABLED(tp1->flags)) {
4404f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4405f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4406f8829a4aSRandall Stewart 		}
44077c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4408f8829a4aSRandall Stewart 		if (tp1->data) {
440904ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4410f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4411f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
44127c99d56fSMichael Tuexen 			tp1->data = NULL;
4413810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4414f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4415f8829a4aSRandall Stewart 			}
4416f8829a4aSRandall Stewart 		}
4417b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4418f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4419f8829a4aSRandall Stewart 			    cum_ack,
4420f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4421f8829a4aSRandall Stewart 			    0,
4422f8829a4aSRandall Stewart 			    0,
4423f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
442480fefe0aSRandall Stewart 		}
4425689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4426f8829a4aSRandall Stewart 		wake_him++;
44277c99d56fSMichael Tuexen 	}
44287c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
44297c99d56fSMichael Tuexen #ifdef INVARIANTS
44307c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
44317c99d56fSMichael Tuexen #else
44327c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
44337c99d56fSMichael Tuexen 		    asoc->total_flight);
44347c99d56fSMichael Tuexen #endif
44357c99d56fSMichael Tuexen 		asoc->total_flight = 0;
44367c99d56fSMichael Tuexen 	}
443704ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4438f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4439ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4440ceaad40aSRandall Stewart 		struct socket *so;
4441ceaad40aSRandall Stewart 
4442ceaad40aSRandall Stewart #endif
4443f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4444b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
44457215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
444680fefe0aSRandall Stewart 		}
4447ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4448ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4449ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4450ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4451ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4452ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4453ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4454ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4455ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4456ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4457ceaad40aSRandall Stewart 			return;
4458ceaad40aSRandall Stewart 		}
4459ceaad40aSRandall Stewart #endif
4460f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4461ceaad40aSRandall Stewart #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4462ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4463ceaad40aSRandall Stewart #endif
4464f8829a4aSRandall Stewart 	} else {
4465b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
44667215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
446780fefe0aSRandall Stewart 		}
4468f8829a4aSRandall Stewart 	}
4469f8829a4aSRandall Stewart 
447042551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
447120b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4472f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4473f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4474f8829a4aSRandall Stewart 		}
4475f8829a4aSRandall Stewart 	}
4476f8829a4aSRandall Stewart 	/*
4477f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4478f8829a4aSRandall Stewart 	 *
4479f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4480f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4481f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4482f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4483f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4484f8829a4aSRandall Stewart 	 */
4485f8829a4aSRandall Stewart 
4486d9c5cfeaSMichael Tuexen 	if (num_seg) {
4487c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4488d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4489d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4490f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4491f8829a4aSRandall Stewart 
4492f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4493f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4494b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4495f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4496b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4497c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4498c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4499c105859eSRandall Stewart 					    tp1->book_size,
4500c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4501c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
450280fefe0aSRandall Stewart 				}
4503c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4504c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4505a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
450642551e99SRandall Stewart 				/*
450742551e99SRandall Stewart 				 * To ensure that this increase in
45084a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
45094a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
45104a9ef3f8SMichael Tuexen 				 * cwnd artificially.
451142551e99SRandall Stewart 				 */
451242551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4513f8829a4aSRandall Stewart 				cnt_revoked++;
4514f8829a4aSRandall Stewart 			}
4515f8829a4aSRandall Stewart 		}
4516f8829a4aSRandall Stewart 		if (cnt_revoked) {
4517f8829a4aSRandall Stewart 			reneged_all = 1;
4518f8829a4aSRandall Stewart 		}
4519f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4520f8829a4aSRandall Stewart 	}
4521d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4522d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4523f8829a4aSRandall Stewart 	else
4524d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4525f8829a4aSRandall Stewart 
4526b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4527ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4528ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4529ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4530ca85e948SMichael Tuexen 				/*
4531ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4532ca85e948SMichael Tuexen 				 * count, this is optional.
4533ca85e948SMichael Tuexen 				 */
4534ca85e948SMichael Tuexen 				net->error_count = 0;
4535ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4536ca85e948SMichael Tuexen 					/* addr came good */
4537ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4538ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
45394b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4540ca85e948SMichael Tuexen 				}
4541ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4542ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4543ca85e948SMichael Tuexen 						/*
4544ca85e948SMichael Tuexen 						 * release the alternate,
4545ca85e948SMichael Tuexen 						 * primary is good
4546ca85e948SMichael Tuexen 						 */
4547ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4548ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4549ca85e948SMichael Tuexen 					}
4550ca85e948SMichael Tuexen 				}
4551ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4552ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4553ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4554ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4555ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4556ca85e948SMichael Tuexen 					/* Done with this net */
4557ca85e948SMichael Tuexen 					net->net_ack = 0;
4558ca85e948SMichael Tuexen 				}
4559ca85e948SMichael Tuexen 				/* restore any doubled timers */
4560ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4561ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4562ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4563ca85e948SMichael Tuexen 				}
4564ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4565ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4566ca85e948SMichael Tuexen 				}
4567ca85e948SMichael Tuexen 			}
4568ca85e948SMichael Tuexen 		}
4569b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4570ca85e948SMichael Tuexen 	}
4571f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4572f8829a4aSRandall Stewart 		/* nothing left in-flight */
4573f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4574f8829a4aSRandall Stewart 			/* stop all timers */
4575f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4576a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4577f8829a4aSRandall Stewart 			net->flight_size = 0;
4578f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4579f8829a4aSRandall Stewart 		}
4580f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4581f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4582f8829a4aSRandall Stewart 	}
4583f8829a4aSRandall Stewart 	/**********************************/
4584f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4585f8829a4aSRandall Stewart 	/**********************************/
4586f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4587f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4588b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4589f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4590f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
459180fefe0aSRandall Stewart 		}
4592f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4593f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4594f8829a4aSRandall Stewart 			/* SWS sender side engages */
4595f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4596f8829a4aSRandall Stewart 		}
4597f8829a4aSRandall Stewart 		/* clean up */
4598f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4599f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4600f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4601f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4602f8829a4aSRandall Stewart 		    ) {
4603f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4604f8829a4aSRandall Stewart 
4605f8829a4aSRandall Stewart 			/*
4606f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4607f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4608f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4609f8829a4aSRandall Stewart 			 */
4610f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4611f8829a4aSRandall Stewart 			    sctp_streamhead);
46122afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4613f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
46142afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4615f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
46162afb3e84SRandall Stewart 				} else {
46172afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
46182afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
46192afb3e84SRandall Stewart 				}
4620f8829a4aSRandall Stewart 			}
4621f8829a4aSRandall Stewart 		}
4622f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4623f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4624f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4625f8829a4aSRandall Stewart 				/* Need to abort here */
4626*ff1ffd74SMichael Tuexen 				struct mbuf *op_err;
4627f8829a4aSRandall Stewart 
4628f8829a4aSRandall Stewart 		abort_out_now:
4629f8829a4aSRandall Stewart 				*abort_now = 1;
4630f8829a4aSRandall Stewart 				/* XXX */
4631*ff1ffd74SMichael Tuexen 				op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4632a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4633*ff1ffd74SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
4634f8829a4aSRandall Stewart 				return;
4635f8829a4aSRandall Stewart 			} else {
4636ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4637ca85e948SMichael Tuexen 
4638f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4639f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4640f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4641f42a358aSRandall Stewart 				}
4642c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4643b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4644f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4645c39cfa1fSMichael Tuexen 				if (asoc->alternate) {
4646c39cfa1fSMichael Tuexen 					netp = asoc->alternate;
4647c39cfa1fSMichael Tuexen 				} else {
4648c39cfa1fSMichael Tuexen 					netp = asoc->primary_destination;
4649c39cfa1fSMichael Tuexen 				}
4650ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4651f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4652ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4653f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4654ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4655f8829a4aSRandall Stewart 			}
4656f8829a4aSRandall Stewart 			return;
4657f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4658f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4659ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4660ca85e948SMichael Tuexen 
4661f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4662f8829a4aSRandall Stewart 				goto abort_out_now;
4663f8829a4aSRandall Stewart 			}
4664f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4665c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4666b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
466712af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4668c39cfa1fSMichael Tuexen 			if (asoc->alternate) {
4669c39cfa1fSMichael Tuexen 				netp = asoc->alternate;
4670c39cfa1fSMichael Tuexen 			} else {
4671c39cfa1fSMichael Tuexen 				netp = asoc->primary_destination;
4672c39cfa1fSMichael Tuexen 			}
4673c39cfa1fSMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
4674f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4675ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4676f8829a4aSRandall Stewart 			return;
4677f8829a4aSRandall Stewart 		}
4678f8829a4aSRandall Stewart 	}
4679f8829a4aSRandall Stewart 	/*
4680f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4681f8829a4aSRandall Stewart 	 * HEADS UP.
4682f8829a4aSRandall Stewart 	 */
4683f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4684f8829a4aSRandall Stewart 		net->net_ack = 0;
4685f8829a4aSRandall Stewart 	}
4686f8829a4aSRandall Stewart 
4687f8829a4aSRandall Stewart 	/*
4688f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4689f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4690f8829a4aSRandall Stewart 	 * automatically ensure that.
4691f8829a4aSRandall Stewart 	 */
46927c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
469320083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
469420083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
4695f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
4696f8829a4aSRandall Stewart 	}
4697cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4698f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
4699f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
4700f8829a4aSRandall Stewart 	}
4701b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4702b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
4703f8829a4aSRandall Stewart 
4704f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
4705f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
4706f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
4707f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
4708f8829a4aSRandall Stewart 	}
4709f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
471020b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
4711f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
4712f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
4713f8829a4aSRandall Stewart 	}
471442551e99SRandall Stewart 	/*
471542551e99SRandall Stewart 	 * CMT Fast recovery
471642551e99SRandall Stewart 	 */
4717f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4718f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
4719f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
4720f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
4721f8829a4aSRandall Stewart 		}
4722f8829a4aSRandall Stewart 	}
4723f8829a4aSRandall Stewart 
4724f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
4725b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4726f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
472744fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
472880fefe0aSRandall Stewart 	}
4729f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
473044fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4731f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4732f8829a4aSRandall Stewart 		/* SWS sender side engages */
4733f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4734f8829a4aSRandall Stewart 	}
47355e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
47365e54f665SRandall Stewart 		win_probe_recovery = 1;
47375e54f665SRandall Stewart 	}
4738f8829a4aSRandall Stewart 	/*
4739f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
4740f8829a4aSRandall Stewart 	 * outstanding data.
4741f8829a4aSRandall Stewart 	 */
4742bff64a4dSRandall Stewart 	done_once = 0;
4743a5d547adSRandall Stewart again:
4744a5d547adSRandall Stewart 	j = 0;
4745f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
47465e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4747c105859eSRandall Stewart 			win_probe_recovered = 1;
47485e54f665SRandall Stewart 			/*-
47495e54f665SRandall Stewart 			 * Find first chunk that was used with
47505e54f665SRandall Stewart 			 * window probe and clear the event. Put
47515e54f665SRandall Stewart 			 * it back into the send queue as if has
47525e54f665SRandall Stewart 			 * not been sent.
47535e54f665SRandall Stewart 			 */
47545e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
47555e54f665SRandall Stewart 				if (tp1->window_probe) {
47567215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
47575e54f665SRandall Stewart 					break;
47585e54f665SRandall Stewart 				}
47595e54f665SRandall Stewart 			}
47605e54f665SRandall Stewart 		}
4761f8829a4aSRandall Stewart 		if (net->flight_size) {
4762a5d547adSRandall Stewart 			j++;
4763cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4764f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4765f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
4766cd554309SMichael Tuexen 			}
47675171328bSRandall Stewart 			if (net->window_probe) {
4768cd554309SMichael Tuexen 				net->window_probe = 0;
47695171328bSRandall Stewart 			}
4770c105859eSRandall Stewart 		} else {
47715171328bSRandall Stewart 			if (net->window_probe) {
47725171328bSRandall Stewart 				/*
47735171328bSRandall Stewart 				 * In window probes we must assure a timer
47745171328bSRandall Stewart 				 * is still running there
47755171328bSRandall Stewart 				 */
47765171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
47775171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
47785171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
47795171328bSRandall Stewart 
47805171328bSRandall Stewart 				}
47815171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4782c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4783c105859eSRandall Stewart 				    stcb, net,
4784c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4785c105859eSRandall Stewart 			}
4786f8829a4aSRandall Stewart 		}
4787f8829a4aSRandall Stewart 	}
4788bff64a4dSRandall Stewart 	if ((j == 0) &&
4789bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4790bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4791c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4792bff64a4dSRandall Stewart 	    (done_once == 0)) {
47930c0982b8SRandall Stewart 		/*
47940c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
47950c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
47960c0982b8SRandall Stewart 		 */
47970c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4798a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4799a5d547adSRandall Stewart 				net->flight_size = 0;
4800a5d547adSRandall Stewart 			}
4801a5d547adSRandall Stewart 			asoc->total_flight = 0;
4802a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4803a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4804a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4805a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4806c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4807c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4808a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4809791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4810a5d547adSRandall Stewart 				}
4811a5d547adSRandall Stewart 			}
48120c0982b8SRandall Stewart 		}
4813bff64a4dSRandall Stewart 		done_once = 1;
4814a5d547adSRandall Stewart 		goto again;
4815a5d547adSRandall Stewart 	}
4816cd554309SMichael Tuexen 	/*********************************************/
4817cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
4818cd554309SMichael Tuexen 	/* (section 4.2)                             */
4819cd554309SMichael Tuexen 	/*********************************************/
4820cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
482120b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
4822dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
4823dfb11ef8SRandall Stewart 	}
4824830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
4825830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4826830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4827830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4828830d754dSRandall Stewart 
4829830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4830830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4831830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
483220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
4833830d754dSRandall Stewart 			/*
4834493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4835830d754dSRandall Stewart 			 */
48360c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
48370c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
48380c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
48390c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
48400c0982b8SRandall Stewart 			}
484120b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4842830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
48430c0982b8SRandall Stewart 			} else if (lchk) {
48440c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
484544fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
48460c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
48470c0982b8SRandall Stewart 				}
4848830d754dSRandall Stewart 			}
4849830d754dSRandall Stewart 		}
4850830d754dSRandall Stewart 		if (lchk) {
4851830d754dSRandall Stewart 			/* Assure a timer is up */
4852830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4853830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4854830d754dSRandall Stewart 		}
4855830d754dSRandall Stewart 	}
4856b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4857f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4858f8829a4aSRandall Stewart 		    a_rwnd,
4859f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4860f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4861f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
486280fefe0aSRandall Stewart 	}
4863f8829a4aSRandall Stewart }
4864f8829a4aSRandall Stewart 
4865f8829a4aSRandall Stewart void
48667215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
4867f8829a4aSRandall Stewart {
4868f8829a4aSRandall Stewart 	/* Copy cum-ack */
4869f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
4870f8829a4aSRandall Stewart 
4871f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
4872f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
4873f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
4874f8829a4aSRandall Stewart 
4875f8829a4aSRandall Stewart 	/* Now call the express sack handling */
4876899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
4877f8829a4aSRandall Stewart }
4878f8829a4aSRandall Stewart 
4879f8829a4aSRandall Stewart static void
4880f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
4881f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
4882f8829a4aSRandall Stewart {
4883f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
4884f8829a4aSRandall Stewart 	struct sctp_association *asoc;
488583128708SRandall Stewart 	uint16_t tt;
4886f8829a4aSRandall Stewart 
4887f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4888f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
4889f8829a4aSRandall Stewart 	/*
4890f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
4891f8829a4aSRandall Stewart 	 * came in
4892f8829a4aSRandall Stewart 	 */
48934a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
489420b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
4895f8829a4aSRandall Stewart 			/* this is deliverable now */
4896f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4897f8829a4aSRandall Stewart 			/* subtract pending on streams */
4898f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
4899f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
4900f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
4901f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
4902b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4903f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
4904f8829a4aSRandall Stewart 				    ctl,
4905cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
4906f8829a4aSRandall Stewart 			}
4907f8829a4aSRandall Stewart 		} else {
4908f8829a4aSRandall Stewart 			/* no more delivery now. */
4909f8829a4aSRandall Stewart 			break;
4910f8829a4aSRandall Stewart 		}
4911f8829a4aSRandall Stewart 	}
4912f8829a4aSRandall Stewart 	/*
4913f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
4914f8829a4aSRandall Stewart 	 * now ready.
4915f8829a4aSRandall Stewart 	 */
4916f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
49174a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
4918f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
4919f8829a4aSRandall Stewart 			/* this is deliverable now */
4920f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
4921f8829a4aSRandall Stewart 			/* subtract pending on streams */
4922f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
4923f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
4924f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
4925f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
4926f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
4927b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
4928f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
4929f8829a4aSRandall Stewart 				    ctl,
4930cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
493177acdc25SRandall Stewart 
4932f8829a4aSRandall Stewart 			}
4933f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
4934f8829a4aSRandall Stewart 		} else {
4935f8829a4aSRandall Stewart 			break;
4936f8829a4aSRandall Stewart 		}
4937f8829a4aSRandall Stewart 	}
4938f8829a4aSRandall Stewart }
4939f8829a4aSRandall Stewart 
49408933fa13SRandall Stewart static void
49418933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
49428933fa13SRandall Stewart     struct sctp_association *asoc,
49438933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
49448933fa13SRandall Stewart {
49454a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
49468933fa13SRandall Stewart 
49478933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
49488933fa13SRandall Stewart 	/*
49494a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
49504a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
49514a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
49524a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
49534a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
49544a9ef3f8SMichael Tuexen 	 * queue.
49558933fa13SRandall Stewart 	 */
49564a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
49578e71b694SMichael Tuexen 		/*
49584a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
49594a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
49604a9ef3f8SMichael Tuexen 		 * number has no meaning.
49618e71b694SMichael Tuexen 		 */
49628e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
49638e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
49648933fa13SRandall Stewart 			continue;
49658933fa13SRandall Stewart 		}
49668933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
49678933fa13SRandall Stewart 			/* It needs to be tossed */
49688933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
496920b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
49704a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
49714a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
49724a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
49734a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
49748933fa13SRandall Stewart 			}
49758933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
49768933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
49778933fa13SRandall Stewart 
49788933fa13SRandall Stewart 			/* Clear up any stream problem */
497920b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
498020b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
49818933fa13SRandall Stewart 				/*
49828933fa13SRandall Stewart 				 * We must dump forward this streams
49834a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
49844a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
49854a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
49864a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
49874a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
49884a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
49894a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
49904a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
49914a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
49924a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
49934a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
49948933fa13SRandall Stewart 				 */
49954a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
49968933fa13SRandall Stewart 			}
49978933fa13SRandall Stewart 			if (chk->data) {
49988933fa13SRandall Stewart 				sctp_m_freem(chk->data);
49998933fa13SRandall Stewart 				chk->data = NULL;
50008933fa13SRandall Stewart 			}
5001689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
500220b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
50038933fa13SRandall Stewart 			/*
50044a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
50054a9ef3f8SMichael Tuexen 			 * are done
50068933fa13SRandall Stewart 			 */
50078933fa13SRandall Stewart 			break;
50088933fa13SRandall Stewart 		}
50098933fa13SRandall Stewart 	}
50108933fa13SRandall Stewart }
50118933fa13SRandall Stewart 
50128933fa13SRandall Stewart 
5013f8829a4aSRandall Stewart void
5014f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5015b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5016b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5017f8829a4aSRandall Stewart {
5018f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5019f8829a4aSRandall Stewart 	/*
5020f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5021f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5022f8829a4aSRandall Stewart 	 *
5023f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5024f8829a4aSRandall Stewart 	 *
5025f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5026f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5027f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5028f8829a4aSRandall Stewart 	 * report where we are.
5029f8829a4aSRandall Stewart 	 */
5030f8829a4aSRandall Stewart 	struct sctp_association *asoc;
50317898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
50327215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
50338933fa13SRandall Stewart 	uint32_t str_seq;
5034f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
50354a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
50368933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5037f8829a4aSRandall Stewart 
5038f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5039f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5040ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5041ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5042f8829a4aSRandall Stewart 		return;
5043f8829a4aSRandall Stewart 	}
5044f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5045f8829a4aSRandall Stewart 	/*************************************************************/
5046f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5047f8829a4aSRandall Stewart 	/*************************************************************/
5048f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5049f8829a4aSRandall Stewart 
505020b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5051f8829a4aSRandall Stewart 		/* Already got there ... */
5052f8829a4aSRandall Stewart 		return;
5053f8829a4aSRandall Stewart 	}
5054f8829a4aSRandall Stewart 	/*
5055f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5056f8829a4aSRandall Stewart 	 * gap
5057f8829a4aSRandall Stewart 	 */
5058b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
505977acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
50602afb3e84SRandall Stewart 	if (gap >= m_size) {
5061f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5062*ff1ffd74SMichael Tuexen 			struct mbuf *op_err;
5063*ff1ffd74SMichael Tuexen 			char msg[SCTP_DIAG_INFO_LEN];
506417205eccSRandall Stewart 
5065f8829a4aSRandall Stewart 			/*
5066f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
506717205eccSRandall Stewart 			 * give out). This must be an attacker.
5068f8829a4aSRandall Stewart 			 */
506917205eccSRandall Stewart 			*abort_flag = 1;
5070*ff1ffd74SMichael Tuexen 			snprintf(msg, sizeof(msg),
5071*ff1ffd74SMichael Tuexen 			    "New cum ack %8.8x too high, highest TSN %8.8x",
5072*ff1ffd74SMichael Tuexen 			    new_cum_tsn, asoc->highest_tsn_inside_map);
5073*ff1ffd74SMichael Tuexen 			op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, msg);
507417205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5075*ff1ffd74SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
5076f8829a4aSRandall Stewart 			return;
5077f8829a4aSRandall Stewart 		}
5078207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
507977acdc25SRandall Stewart 
50802afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
50812afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
508277acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
508377acdc25SRandall Stewart 
5084b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5085830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
508677acdc25SRandall Stewart 
5087b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5088f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
508980fefe0aSRandall Stewart 		}
50902afb3e84SRandall Stewart 	} else {
50912afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
50922afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
50937898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
50947898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
50958933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
509620b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
50977898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5098b5c16493SMichael Tuexen 				}
5099b5c16493SMichael Tuexen 			}
5100b5c16493SMichael Tuexen 		}
5101f8829a4aSRandall Stewart 	}
5102f8829a4aSRandall Stewart 	/*************************************************************/
5103f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5104f8829a4aSRandall Stewart 	/*************************************************************/
5105f8829a4aSRandall Stewart 	/*
5106f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5107f8829a4aSRandall Stewart 	 * forward
5108f8829a4aSRandall Stewart 	 */
5109f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5110f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5111f8829a4aSRandall Stewart 	}
5112f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5113f8829a4aSRandall Stewart 	/*
51144a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
51154a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
51164a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
51174a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
51184a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
51194a9ef3f8SMichael Tuexen 	 * queue.
5120f8829a4aSRandall Stewart 	 */
51214a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
512220b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5123f8829a4aSRandall Stewart 			/* It needs to be tossed */
5124f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
512520b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
51264a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
51274a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
51284a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
51294a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5130f8829a4aSRandall Stewart 			}
5131f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5132f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5133f8829a4aSRandall Stewart 
5134f8829a4aSRandall Stewart 			/* Clear up any stream problem */
513520b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
513620b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5137f8829a4aSRandall Stewart 				/*
5138f8829a4aSRandall Stewart 				 * We must dump forward this streams
51394a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
51404a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
51414a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
51424a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
51434a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
51444a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
51454a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
51464a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
51474a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
51484a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
51494a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5150f8829a4aSRandall Stewart 				 */
51514a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5152f8829a4aSRandall Stewart 			}
5153f8829a4aSRandall Stewart 			if (chk->data) {
5154f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5155f8829a4aSRandall Stewart 				chk->data = NULL;
5156f8829a4aSRandall Stewart 			}
5157689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5158f8829a4aSRandall Stewart 		} else {
5159f8829a4aSRandall Stewart 			/*
51604a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
51614a9ef3f8SMichael Tuexen 			 * mark.
5162f8829a4aSRandall Stewart 			 */
5163f8829a4aSRandall Stewart 			break;
5164f8829a4aSRandall Stewart 		}
5165f8829a4aSRandall Stewart 	}
51668933fa13SRandall Stewart 	/*******************************************************/
51678933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
51688933fa13SRandall Stewart 	/* delivery issues as needed.                       */
51698933fa13SRandall Stewart 	/*******************************************************/
5170f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5171671d309cSRandall Stewart 	if (m && fwd_sz) {
5172f8829a4aSRandall Stewart 		/* New method. */
5173d61a0ae0SRandall Stewart 		unsigned int num_str;
5174671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5175671d309cSRandall Stewart 
5176671d309cSRandall Stewart 		offset += sizeof(*fwd);
5177f8829a4aSRandall Stewart 
51788933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5179f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5180f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5181f8829a4aSRandall Stewart 			uint16_t st;
5182f8829a4aSRandall Stewart 
5183671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5184671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5185671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5186671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
51872afb3e84SRandall Stewart 			if (stseq == NULL) {
5188671d309cSRandall Stewart 				break;
51892afb3e84SRandall Stewart 			}
5190f8829a4aSRandall Stewart 			/* Convert */
5191851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5192851b7298SRandall Stewart 			stseq->stream = st;
5193851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5194851b7298SRandall Stewart 			stseq->sequence = st;
51958933fa13SRandall Stewart 
5196f8829a4aSRandall Stewart 			/* now process */
51978933fa13SRandall Stewart 
51988933fa13SRandall Stewart 			/*
51998933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
52008933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
52018933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
52028933fa13SRandall Stewart 			 */
5203851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
52042afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
52052afb3e84SRandall Stewart 				break;
5206f8829a4aSRandall Stewart 			}
52078933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
52088933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
52098933fa13SRandall Stewart 				/*
52108933fa13SRandall Stewart 				 * If this is the one we were partially
52118933fa13SRandall Stewart 				 * delivering now then we no longer are.
52128933fa13SRandall Stewart 				 * Note this will change with the reassembly
52138933fa13SRandall Stewart 				 * re-write.
52148933fa13SRandall Stewart 				 */
52158933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
52168933fa13SRandall Stewart 			}
52178933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
52188933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
52198933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
52208933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
52218933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
52228933fa13SRandall Stewart 					ctl->end_added = 1;
52238933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
52248933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
52258933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5226810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5227810ec536SMichael Tuexen 					    stcb,
52288933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5229810ec536SMichael Tuexen 					    (void *)&str_seq,
5230810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
52318933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
52328933fa13SRandall Stewart 					break;
52338933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
523420b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
52358933fa13SRandall Stewart 					/* We are past our victim SSN */
52368933fa13SRandall Stewart 					break;
52378933fa13SRandall Stewart 				}
52388933fa13SRandall Stewart 			}
5239851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
524020b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5241f8829a4aSRandall Stewart 				/* Update the sequence number */
524220b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5243f8829a4aSRandall Stewart 			}
5244f8829a4aSRandall Stewart 			/* now kick the stream the new way */
524504ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5246f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5247f8829a4aSRandall Stewart 		}
52488933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5249f8829a4aSRandall Stewart 	}
52507898f408SRandall Stewart 	/*
52517898f408SRandall Stewart 	 * Now slide thing forward.
52527898f408SRandall Stewart 	 */
52537898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
52547898f408SRandall Stewart 
525524f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5256139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
525704ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5258139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5259139bc87fSRandall Stewart 	}
5260f8829a4aSRandall Stewart }
5261