xref: /freebsd/sys/netinet/sctp_indata.c (revision 1edc9dbae5dff7375b0f4a8735c671983f4478c4)
1f8829a4aSRandall Stewart /*-
2b1006367SRandall Stewart  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
35d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
45d40cf5dSRandall Stewart  * Copyright (c) 2008-2011, 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 
33a5d547adSRandall Stewart /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	 */
34f8829a4aSRandall Stewart 
35f8829a4aSRandall Stewart #include <sys/cdefs.h>
36f8829a4aSRandall Stewart __FBSDID("$FreeBSD$");
37f8829a4aSRandall Stewart 
38f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
4042551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
43f8829a4aSRandall Stewart #include <netinet/sctputil.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_input.h>
46f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
48f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
49f8829a4aSRandall Stewart 
50d50c1d79SRandall Stewart 
51f8829a4aSRandall Stewart /*
52f8829a4aSRandall Stewart  * NOTES: On the outbound side of things I need to check the sack timer to
53f8829a4aSRandall Stewart  * see if I should generate a sack into the chunk queue (if I have data to
54f8829a4aSRandall Stewart  * send that is and will be sending it .. for bundling.
55f8829a4aSRandall Stewart  *
56f8829a4aSRandall Stewart  * The callback in sctp_usrreq.c will get called when the socket is read from.
57f8829a4aSRandall Stewart  * This will cause sctp_service_queues() to get called on the top entry in
58f8829a4aSRandall Stewart  * the list.
59f8829a4aSRandall Stewart  */
60f8829a4aSRandall Stewart 
6172fb6fdbSRandall Stewart void
62f8829a4aSRandall Stewart sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
63f8829a4aSRandall Stewart {
64b3f1ea41SRandall Stewart 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
65f8829a4aSRandall Stewart }
66f8829a4aSRandall Stewart 
67f8829a4aSRandall Stewart /* Calculate what the rwnd would be */
6872fb6fdbSRandall Stewart uint32_t
69f8829a4aSRandall Stewart sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
70f8829a4aSRandall Stewart {
71b3f1ea41SRandall Stewart 	uint32_t calc = 0;
72f8829a4aSRandall Stewart 
73f8829a4aSRandall Stewart 	/*
74f8829a4aSRandall Stewart 	 * This is really set wrong with respect to a 1-2-m socket. Since
75f8829a4aSRandall Stewart 	 * the sb_cc is the count that everyone as put up. When we re-write
76f8829a4aSRandall Stewart 	 * sctp_soreceive then we will fix this so that ONLY this
77f8829a4aSRandall Stewart 	 * associations data is taken into account.
78f8829a4aSRandall Stewart 	 */
79f8829a4aSRandall Stewart 	if (stcb->sctp_socket == NULL)
80f8829a4aSRandall Stewart 		return (calc);
81f8829a4aSRandall Stewart 
82f8829a4aSRandall Stewart 	if (stcb->asoc.sb_cc == 0 &&
83f8829a4aSRandall Stewart 	    asoc->size_on_reasm_queue == 0 &&
84f8829a4aSRandall Stewart 	    asoc->size_on_all_streams == 0) {
85f8829a4aSRandall Stewart 		/* Full rwnd granted */
86b3f1ea41SRandall Stewart 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
87f8829a4aSRandall Stewart 		return (calc);
88f8829a4aSRandall Stewart 	}
89f8829a4aSRandall Stewart 	/* get actual space */
90f8829a4aSRandall Stewart 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
91f8829a4aSRandall Stewart 
92f8829a4aSRandall Stewart 	/*
93f8829a4aSRandall Stewart 	 * take out what has NOT been put on socket queue and we yet hold
94f8829a4aSRandall Stewart 	 * for putting up.
95f8829a4aSRandall Stewart 	 */
9644fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_reasm_queue +
9744fbe462SRandall Stewart 	    asoc->cnt_on_reasm_queue * MSIZE));
9844fbe462SRandall Stewart 	calc = sctp_sbspace_sub(calc, (uint32_t) (asoc->size_on_all_streams +
9944fbe462SRandall Stewart 	    asoc->cnt_on_all_streams * MSIZE));
100f8829a4aSRandall Stewart 
101f8829a4aSRandall Stewart 	if (calc == 0) {
102f8829a4aSRandall Stewart 		/* out of space */
103f8829a4aSRandall Stewart 		return (calc);
104f8829a4aSRandall Stewart 	}
105f8829a4aSRandall Stewart 	/* what is the overhead of all these rwnd's */
1062afb3e84SRandall Stewart 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
107b3f1ea41SRandall Stewart 	/*
108b3f1ea41SRandall Stewart 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
109b3f1ea41SRandall Stewart 	 * even it is 0. SWS engaged
110f8829a4aSRandall Stewart 	 */
111b3f1ea41SRandall Stewart 	if (calc < stcb->asoc.my_rwnd_control_len) {
112b3f1ea41SRandall Stewart 		calc = 1;
1132afb3e84SRandall Stewart 	}
114b3f1ea41SRandall Stewart 	return (calc);
115f8829a4aSRandall Stewart }
116f8829a4aSRandall Stewart 
117f8829a4aSRandall Stewart 
118f8829a4aSRandall Stewart 
119f8829a4aSRandall Stewart /*
120f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
121f8829a4aSRandall Stewart  */
122f8829a4aSRandall Stewart struct sctp_queued_to_read *
123f8829a4aSRandall Stewart sctp_build_readq_entry(struct sctp_tcb *stcb,
124f8829a4aSRandall Stewart     struct sctp_nets *net,
125f8829a4aSRandall Stewart     uint32_t tsn, uint32_t ppid,
126f8829a4aSRandall Stewart     uint32_t context, uint16_t stream_no,
127f8829a4aSRandall Stewart     uint16_t stream_seq, uint8_t flags,
128f8829a4aSRandall Stewart     struct mbuf *dm)
129f8829a4aSRandall Stewart {
130f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
131f8829a4aSRandall Stewart 
132f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
133f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
134f8829a4aSRandall Stewart 		goto failed_build;
135f8829a4aSRandall Stewart 	}
136f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = stream_no;
137f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = stream_seq;
138f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (flags << 8);
139f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = ppid;
1407215cc1bSMichael Tuexen 	read_queue_e->sinfo_context = context;
141f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
142f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = tsn;
143f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = tsn;
144f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
145f8829a4aSRandall Stewart 	read_queue_e->whoFrom = net;
146f8829a4aSRandall Stewart 	read_queue_e->length = 0;
147f8829a4aSRandall Stewart 	atomic_add_int(&net->ref_count, 1);
148f8829a4aSRandall Stewart 	read_queue_e->data = dm;
149139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
150f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
15117205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
152f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
153f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
154f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
155f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1569a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
15703b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
158f8829a4aSRandall Stewart failed_build:
159f8829a4aSRandall Stewart 	return (read_queue_e);
160f8829a4aSRandall Stewart }
161f8829a4aSRandall Stewart 
162f8829a4aSRandall Stewart 
163f8829a4aSRandall Stewart /*
164f8829a4aSRandall Stewart  * Build out our readq entry based on the incoming packet.
165f8829a4aSRandall Stewart  */
166f8829a4aSRandall Stewart static struct sctp_queued_to_read *
167f8829a4aSRandall Stewart sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
168f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk)
169f8829a4aSRandall Stewart {
170f8829a4aSRandall Stewart 	struct sctp_queued_to_read *read_queue_e = NULL;
171f8829a4aSRandall Stewart 
172f8829a4aSRandall Stewart 	sctp_alloc_a_readq(stcb, read_queue_e);
173f8829a4aSRandall Stewart 	if (read_queue_e == NULL) {
174f8829a4aSRandall Stewart 		goto failed_build;
175f8829a4aSRandall Stewart 	}
176f8829a4aSRandall Stewart 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
177f8829a4aSRandall Stewart 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
178f8829a4aSRandall Stewart 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
179f8829a4aSRandall Stewart 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
180f8829a4aSRandall Stewart 	read_queue_e->sinfo_context = stcb->asoc.context;
181f8829a4aSRandall Stewart 	read_queue_e->sinfo_timetolive = 0;
182f8829a4aSRandall Stewart 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
183f8829a4aSRandall Stewart 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
184f8829a4aSRandall Stewart 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
185f8829a4aSRandall Stewart 	read_queue_e->whoFrom = chk->whoTo;
18617205eccSRandall Stewart 	read_queue_e->aux_data = NULL;
187f8829a4aSRandall Stewart 	read_queue_e->length = 0;
188f8829a4aSRandall Stewart 	atomic_add_int(&chk->whoTo->ref_count, 1);
189f8829a4aSRandall Stewart 	read_queue_e->data = chk->data;
190f8829a4aSRandall Stewart 	read_queue_e->tail_mbuf = NULL;
191f8829a4aSRandall Stewart 	read_queue_e->stcb = stcb;
192f8829a4aSRandall Stewart 	read_queue_e->port_from = stcb->rport;
193139bc87fSRandall Stewart 	read_queue_e->spec_flags = 0;
194f8829a4aSRandall Stewart 	read_queue_e->do_not_ref_stcb = 0;
195f8829a4aSRandall Stewart 	read_queue_e->end_added = 0;
1969a6142d8SRandall Stewart 	read_queue_e->some_taken = 0;
19703b0b021SRandall Stewart 	read_queue_e->pdapi_aborted = 0;
198f8829a4aSRandall Stewart failed_build:
199f8829a4aSRandall Stewart 	return (read_queue_e);
200f8829a4aSRandall Stewart }
201f8829a4aSRandall Stewart 
202f8829a4aSRandall Stewart 
203f8829a4aSRandall Stewart struct mbuf *
204e2e7c62eSMichael Tuexen sctp_build_ctl_nchunk(struct sctp_inpcb *inp, struct sctp_sndrcvinfo *sinfo)
205f8829a4aSRandall Stewart {
206e2e7c62eSMichael Tuexen 	struct sctp_extrcvinfo *seinfo;
207f8829a4aSRandall Stewart 	struct sctp_sndrcvinfo *outinfo;
208e2e7c62eSMichael Tuexen 	struct sctp_rcvinfo *rcvinfo;
209e2e7c62eSMichael Tuexen 	struct sctp_nxtinfo *nxtinfo;
210f8829a4aSRandall Stewart 	struct cmsghdr *cmh;
211f8829a4aSRandall Stewart 	struct mbuf *ret;
212f8829a4aSRandall Stewart 	int len;
213e2e7c62eSMichael Tuexen 	int use_extended;
214e2e7c62eSMichael Tuexen 	int provide_nxt;
215f8829a4aSRandall Stewart 
216e2e7c62eSMichael Tuexen 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
217e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
218e2e7c62eSMichael Tuexen 	    sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
219e2e7c62eSMichael Tuexen 		/* user does not want any ancillary data */
220f8829a4aSRandall Stewart 		return (NULL);
221f8829a4aSRandall Stewart 	}
222e2e7c62eSMichael Tuexen 	len = 0;
223e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
224e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
225e2e7c62eSMichael Tuexen 	}
226e2e7c62eSMichael Tuexen 	seinfo = (struct sctp_extrcvinfo *)sinfo;
227e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
228e2e7c62eSMichael Tuexen 	    (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL)) {
229e2e7c62eSMichael Tuexen 		provide_nxt = 1;
230e2e7c62eSMichael Tuexen 		len += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
231e2e7c62eSMichael Tuexen 	} else {
232e2e7c62eSMichael Tuexen 		provide_nxt = 0;
233e2e7c62eSMichael Tuexen 	}
234e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
235f8829a4aSRandall Stewart 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
236f8829a4aSRandall Stewart 			use_extended = 1;
237e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
238f8829a4aSRandall Stewart 		} else {
239e2e7c62eSMichael Tuexen 			use_extended = 0;
240e2e7c62eSMichael Tuexen 			len += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
241e2e7c62eSMichael Tuexen 		}
242e2e7c62eSMichael Tuexen 	} else {
243e2e7c62eSMichael Tuexen 		use_extended = 0;
244f8829a4aSRandall Stewart 	}
245f8829a4aSRandall Stewart 
246e2e7c62eSMichael Tuexen 	ret = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
247f8829a4aSRandall Stewart 	if (ret == NULL) {
248f8829a4aSRandall Stewart 		/* No space */
249f8829a4aSRandall Stewart 		return (ret);
250f8829a4aSRandall Stewart 	}
251e2e7c62eSMichael Tuexen 	SCTP_BUF_LEN(ret) = 0;
252e2e7c62eSMichael Tuexen 
253f8829a4aSRandall Stewart 	/* We need a CMSG header followed by the struct */
254f8829a4aSRandall Stewart 	cmh = mtod(ret, struct cmsghdr *);
255e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO)) {
256f8829a4aSRandall Stewart 		cmh->cmsg_level = IPPROTO_SCTP;
257e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_rcvinfo));
258e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_RCVINFO;
259e2e7c62eSMichael Tuexen 		rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmh);
260e2e7c62eSMichael Tuexen 		rcvinfo->rcv_sid = sinfo->sinfo_stream;
261e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ssn = sinfo->sinfo_ssn;
262e2e7c62eSMichael Tuexen 		rcvinfo->rcv_flags = sinfo->sinfo_flags;
263e2e7c62eSMichael Tuexen 		rcvinfo->rcv_ppid = sinfo->sinfo_ppid;
264e2e7c62eSMichael Tuexen 		rcvinfo->rcv_tsn = sinfo->sinfo_tsn;
265e2e7c62eSMichael Tuexen 		rcvinfo->rcv_cumtsn = sinfo->sinfo_cumtsn;
266e2e7c62eSMichael Tuexen 		rcvinfo->rcv_context = sinfo->sinfo_context;
267e2e7c62eSMichael Tuexen 		rcvinfo->rcv_assoc_id = sinfo->sinfo_assoc_id;
268e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_rcvinfo)));
269e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_rcvinfo));
270f8829a4aSRandall Stewart 	}
271e2e7c62eSMichael Tuexen 	if (provide_nxt) {
272e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
273e2e7c62eSMichael Tuexen 		cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_nxtinfo));
274e2e7c62eSMichael Tuexen 		cmh->cmsg_type = SCTP_NXTINFO;
275e2e7c62eSMichael Tuexen 		nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmh);
276e2e7c62eSMichael Tuexen 		nxtinfo->nxt_sid = seinfo->sreinfo_next_stream;
277e2e7c62eSMichael Tuexen 		nxtinfo->nxt_flags = 0;
278e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
279e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_UNORDERED;
280e2e7c62eSMichael Tuexen 		}
281e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
282e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_NOTIFICATION;
283e2e7c62eSMichael Tuexen 		}
284e2e7c62eSMichael Tuexen 		if (seinfo->sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
285e2e7c62eSMichael Tuexen 			nxtinfo->nxt_flags |= SCTP_COMPLETE;
286e2e7c62eSMichael Tuexen 		}
287e2e7c62eSMichael Tuexen 		nxtinfo->nxt_ppid = seinfo->sreinfo_next_ppid;
288e2e7c62eSMichael Tuexen 		nxtinfo->nxt_length = seinfo->sreinfo_next_length;
289e2e7c62eSMichael Tuexen 		nxtinfo->nxt_assoc_id = seinfo->sreinfo_next_aid;
290e2e7c62eSMichael Tuexen 		cmh = (struct cmsghdr *)((caddr_t)cmh + CMSG_SPACE(sizeof(struct sctp_nxtinfo)));
291e2e7c62eSMichael Tuexen 		SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_nxtinfo));
292e2e7c62eSMichael Tuexen 	}
293e2e7c62eSMichael Tuexen 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
294e2e7c62eSMichael Tuexen 		cmh->cmsg_level = IPPROTO_SCTP;
295e2e7c62eSMichael Tuexen 		outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
296e2e7c62eSMichael Tuexen 		if (use_extended) {
297e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
298e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_EXTRCV;
299e2e7c62eSMichael Tuexen 			memcpy(outinfo, sinfo, sizeof(struct sctp_extrcvinfo));
300e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_extrcvinfo));
301e2e7c62eSMichael Tuexen 		} else {
302e2e7c62eSMichael Tuexen 			cmh->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
303e2e7c62eSMichael Tuexen 			cmh->cmsg_type = SCTP_SNDRCV;
304e2e7c62eSMichael Tuexen 			*outinfo = *sinfo;
305e2e7c62eSMichael Tuexen 			SCTP_BUF_LEN(ret) += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
306e2e7c62eSMichael Tuexen 		}
307e2e7c62eSMichael Tuexen 	}
308f8829a4aSRandall Stewart 	return (ret);
309f8829a4aSRandall Stewart }
310f8829a4aSRandall Stewart 
311139bc87fSRandall Stewart 
31277acdc25SRandall Stewart static void
31377acdc25SRandall Stewart sctp_mark_non_revokable(struct sctp_association *asoc, uint32_t tsn)
31477acdc25SRandall Stewart {
3159b2e0767SRandall Stewart 	uint32_t gap, i, cumackp1;
31677acdc25SRandall Stewart 	int fnd = 0;
31777acdc25SRandall Stewart 
31877acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
31977acdc25SRandall Stewart 		return;
32077acdc25SRandall Stewart 	}
3219b2e0767SRandall Stewart 	cumackp1 = asoc->cumulative_tsn + 1;
32220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumackp1, tsn)) {
3239b2e0767SRandall Stewart 		/*
3249b2e0767SRandall Stewart 		 * this tsn is behind the cum ack and thus we don't need to
3259b2e0767SRandall Stewart 		 * worry about it being moved from one to the other.
3269b2e0767SRandall Stewart 		 */
3279b2e0767SRandall Stewart 		return;
3289b2e0767SRandall Stewart 	}
32977acdc25SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
33077acdc25SRandall Stewart 	if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
331cd3fd531SMichael Tuexen 		SCTP_PRINTF("gap:%x tsn:%x\n", gap, tsn);
33277acdc25SRandall Stewart 		sctp_print_mapping_array(asoc);
333b5c16493SMichael Tuexen #ifdef INVARIANTS
33477acdc25SRandall Stewart 		panic("Things are really messed up now!!");
33577acdc25SRandall Stewart #endif
336b5c16493SMichael Tuexen 	}
33777acdc25SRandall Stewart 	SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
33877acdc25SRandall Stewart 	SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
33920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
34077acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = tsn;
34177acdc25SRandall Stewart 	}
34277acdc25SRandall Stewart 	if (tsn == asoc->highest_tsn_inside_map) {
34377acdc25SRandall Stewart 		/* We must back down to see what the new highest is */
34420b07a4dSMichael Tuexen 		for (i = tsn - 1; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
34577acdc25SRandall Stewart 			SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
34677acdc25SRandall Stewart 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
34777acdc25SRandall Stewart 				asoc->highest_tsn_inside_map = i;
34877acdc25SRandall Stewart 				fnd = 1;
34977acdc25SRandall Stewart 				break;
35077acdc25SRandall Stewart 			}
35177acdc25SRandall Stewart 		}
35277acdc25SRandall Stewart 		if (!fnd) {
35377acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
35477acdc25SRandall Stewart 		}
35577acdc25SRandall Stewart 	}
35677acdc25SRandall Stewart }
35777acdc25SRandall Stewart 
35817205eccSRandall Stewart 
359f8829a4aSRandall Stewart /*
360f8829a4aSRandall Stewart  * We are delivering currently from the reassembly queue. We must continue to
361f8829a4aSRandall Stewart  * deliver until we either: 1) run out of space. 2) run out of sequential
362f8829a4aSRandall Stewart  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
363f8829a4aSRandall Stewart  */
364f8829a4aSRandall Stewart static void
365f8829a4aSRandall Stewart sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
366f8829a4aSRandall Stewart {
3674a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
368f8829a4aSRandall Stewart 	uint16_t nxt_todel;
369f8829a4aSRandall Stewart 	uint16_t stream_no;
370f8829a4aSRandall Stewart 	int end = 0;
371f8829a4aSRandall Stewart 	int cntDel;
3724a9ef3f8SMichael Tuexen 	struct sctp_queued_to_read *control, *ctl, *nctl;
373f8829a4aSRandall Stewart 
374ad81507eSRandall Stewart 	if (stcb == NULL)
375ad81507eSRandall Stewart 		return;
376ad81507eSRandall Stewart 
377f42a358aSRandall Stewart 	cntDel = stream_no = 0;
378ad81507eSRandall Stewart 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
379851b7298SRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
380ad81507eSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
381851b7298SRandall Stewart 		/* socket above is long gone or going.. */
382851b7298SRandall Stewart abandon:
383f8829a4aSRandall Stewart 		asoc->fragmented_delivery_inprogress = 0;
3844a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
385f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
386f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
387f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
388f8829a4aSRandall Stewart 			/*
389f8829a4aSRandall Stewart 			 * Lose the data pointer, since its in the socket
390f8829a4aSRandall Stewart 			 * buffer
391f8829a4aSRandall Stewart 			 */
392f8829a4aSRandall Stewart 			if (chk->data) {
393f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
394f8829a4aSRandall Stewart 				chk->data = NULL;
395f8829a4aSRandall Stewart 			}
396f8829a4aSRandall Stewart 			/* Now free the address and data */
397689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3983c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
399f8829a4aSRandall Stewart 		}
400f8829a4aSRandall Stewart 		return;
401f8829a4aSRandall Stewart 	}
402f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4034a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
404f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
405f8829a4aSRandall Stewart 			/* Can't deliver more :< */
406f8829a4aSRandall Stewart 			return;
407f8829a4aSRandall Stewart 		}
408f8829a4aSRandall Stewart 		stream_no = chk->rec.data.stream_number;
409f8829a4aSRandall Stewart 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
410f8829a4aSRandall Stewart 		if (nxt_todel != chk->rec.data.stream_seq &&
411f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
412f8829a4aSRandall Stewart 			/*
413f8829a4aSRandall Stewart 			 * Not the next sequence to deliver in its stream OR
414f8829a4aSRandall Stewart 			 * unordered
415f8829a4aSRandall Stewart 			 */
416f8829a4aSRandall Stewart 			return;
417f8829a4aSRandall Stewart 		}
418f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
419f8829a4aSRandall Stewart 
420f8829a4aSRandall Stewart 			control = sctp_build_readq_entry_chk(stcb, chk);
421f8829a4aSRandall Stewart 			if (control == NULL) {
422f8829a4aSRandall Stewart 				/* out of memory? */
423f8829a4aSRandall Stewart 				return;
424f8829a4aSRandall Stewart 			}
425f8829a4aSRandall Stewart 			/* save it off for our future deliveries */
426f8829a4aSRandall Stewart 			stcb->asoc.control_pdapi = control;
427f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
428f8829a4aSRandall Stewart 				end = 1;
429f8829a4aSRandall Stewart 			else
430f8829a4aSRandall Stewart 				end = 0;
431b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
432f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep,
433cfde3ff7SRandall Stewart 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
434cfde3ff7SRandall Stewart 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
435f8829a4aSRandall Stewart 			cntDel++;
436f8829a4aSRandall Stewart 		} else {
437f8829a4aSRandall Stewart 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
438f8829a4aSRandall Stewart 				end = 1;
439f8829a4aSRandall Stewart 			else
440f8829a4aSRandall Stewart 				end = 0;
441b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, chk->rec.data.TSN_seq);
442f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
443f8829a4aSRandall Stewart 			    stcb->asoc.control_pdapi,
444f8829a4aSRandall Stewart 			    chk->data, end, chk->rec.data.TSN_seq,
445f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
446f8829a4aSRandall Stewart 				/*
447f8829a4aSRandall Stewart 				 * something is very wrong, either
448f8829a4aSRandall Stewart 				 * control_pdapi is NULL, or the tail_mbuf
449f8829a4aSRandall Stewart 				 * is corrupt, or there is a EOM already on
450f8829a4aSRandall Stewart 				 * the mbuf chain.
451f8829a4aSRandall Stewart 				 */
452851b7298SRandall Stewart 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
453851b7298SRandall Stewart 					goto abandon;
454851b7298SRandall Stewart 				} else {
455df6e0cc3SRandall Stewart #ifdef INVARIANTS
456ad81507eSRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
457f8829a4aSRandall Stewart 						panic("This should not happen control_pdapi NULL?");
458f8829a4aSRandall Stewart 					}
459f8829a4aSRandall Stewart 					/* if we did not panic, it was a EOM */
460f8829a4aSRandall Stewart 					panic("Bad chunking ??");
461df6e0cc3SRandall Stewart #else
462df6e0cc3SRandall Stewart 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
463df6e0cc3SRandall Stewart 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
464df6e0cc3SRandall Stewart 					}
465df6e0cc3SRandall Stewart 					SCTP_PRINTF("Bad chunking ??\n");
466df6e0cc3SRandall Stewart 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
467df6e0cc3SRandall Stewart 
468df6e0cc3SRandall Stewart #endif
469df6e0cc3SRandall Stewart 					goto abandon;
470f8829a4aSRandall Stewart 				}
471851b7298SRandall Stewart 			}
472f8829a4aSRandall Stewart 			cntDel++;
473f8829a4aSRandall Stewart 		}
474f8829a4aSRandall Stewart 		/* pull it we did it */
475f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
476f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
477f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 0;
478f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
479f8829a4aSRandall Stewart 				asoc->strmin[stream_no].last_sequence_delivered++;
480f8829a4aSRandall Stewart 			}
481f8829a4aSRandall Stewart 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
482f8829a4aSRandall Stewart 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
483f8829a4aSRandall Stewart 			}
484f8829a4aSRandall Stewart 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
485f8829a4aSRandall Stewart 			/*
486f8829a4aSRandall Stewart 			 * turn the flag back on since we just  delivered
487f8829a4aSRandall Stewart 			 * yet another one.
488f8829a4aSRandall Stewart 			 */
489f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
490f8829a4aSRandall Stewart 		}
491f8829a4aSRandall Stewart 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
492f8829a4aSRandall Stewart 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
493f8829a4aSRandall Stewart 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
494f8829a4aSRandall Stewart 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
495f8829a4aSRandall Stewart 
496f8829a4aSRandall Stewart 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
497f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue -= chk->send_size;
498f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
499f8829a4aSRandall Stewart 		/* free up the chk */
500f8829a4aSRandall Stewart 		chk->data = NULL;
501689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
502f8829a4aSRandall Stewart 
503f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
504f8829a4aSRandall Stewart 			/*
505f8829a4aSRandall Stewart 			 * Now lets see if we can deliver the next one on
506f8829a4aSRandall Stewart 			 * the stream
507f8829a4aSRandall Stewart 			 */
508f8829a4aSRandall Stewart 			struct sctp_stream_in *strm;
509f8829a4aSRandall Stewart 
510f8829a4aSRandall Stewart 			strm = &asoc->strmin[stream_no];
511f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
5124a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &strm->inqueue, next, nctl) {
513f8829a4aSRandall Stewart 				/* Deliver more if we can. */
514f8829a4aSRandall Stewart 				if (nxt_todel == ctl->sinfo_ssn) {
515f8829a4aSRandall Stewart 					TAILQ_REMOVE(&strm->inqueue, ctl, next);
516f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= ctl->length;
517f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
518f8829a4aSRandall Stewart 					strm->last_sequence_delivered++;
519b5c16493SMichael Tuexen 					sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
520f8829a4aSRandall Stewart 					sctp_add_to_readq(stcb->sctp_ep, stcb,
521f8829a4aSRandall Stewart 					    ctl,
522cfde3ff7SRandall Stewart 					    &stcb->sctp_socket->so_rcv, 1,
523cfde3ff7SRandall Stewart 					    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
524f8829a4aSRandall Stewart 				} else {
525f8829a4aSRandall Stewart 					break;
526f8829a4aSRandall Stewart 				}
527f8829a4aSRandall Stewart 				nxt_todel = strm->last_sequence_delivered + 1;
528f8829a4aSRandall Stewart 			}
529139bc87fSRandall Stewart 			break;
530f8829a4aSRandall Stewart 		}
5314a9ef3f8SMichael Tuexen 	}
532f8829a4aSRandall Stewart }
533f8829a4aSRandall Stewart 
534f8829a4aSRandall Stewart /*
535f8829a4aSRandall Stewart  * Queue the chunk either right into the socket buffer if it is the next one
536f8829a4aSRandall Stewart  * to go OR put it in the correct place in the delivery queue.  If we do
537f8829a4aSRandall Stewart  * append to the so_buf, keep doing so until we are out of order. One big
538f8829a4aSRandall Stewart  * question still remains, what to do when the socket buffer is FULL??
539f8829a4aSRandall Stewart  */
540f8829a4aSRandall Stewart static void
541f8829a4aSRandall Stewart sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
542f8829a4aSRandall Stewart     struct sctp_queued_to_read *control, int *abort_flag)
543f8829a4aSRandall Stewart {
544f8829a4aSRandall Stewart 	/*
545f8829a4aSRandall Stewart 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
546f8829a4aSRandall Stewart 	 * all the data in one stream this could happen quite rapidly. One
547f8829a4aSRandall Stewart 	 * could use the TSN to keep track of things, but this scheme breaks
548f8829a4aSRandall Stewart 	 * down in the other type of stream useage that could occur. Send a
549f8829a4aSRandall Stewart 	 * single msg to stream 0, send 4Billion messages to stream 1, now
550f8829a4aSRandall Stewart 	 * send a message to stream 0. You have a situation where the TSN
551f8829a4aSRandall Stewart 	 * has wrapped but not in the stream. Is this worth worrying about
552f8829a4aSRandall Stewart 	 * or should we just change our queue sort at the bottom to be by
553f8829a4aSRandall Stewart 	 * TSN.
554f8829a4aSRandall Stewart 	 *
555f8829a4aSRandall Stewart 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
556f8829a4aSRandall Stewart 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
557f8829a4aSRandall Stewart 	 * assignment this could happen... and I don't see how this would be
558f8829a4aSRandall Stewart 	 * a violation. So for now I am undecided an will leave the sort by
559f8829a4aSRandall Stewart 	 * SSN alone. Maybe a hybred approach is the answer
560f8829a4aSRandall Stewart 	 *
561f8829a4aSRandall Stewart 	 */
562f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
563f8829a4aSRandall Stewart 	struct sctp_queued_to_read *at;
564f8829a4aSRandall Stewart 	int queue_needed;
565f8829a4aSRandall Stewart 	uint16_t nxt_todel;
566f8829a4aSRandall Stewart 	struct mbuf *oper;
567f8829a4aSRandall Stewart 
568f8829a4aSRandall Stewart 	queue_needed = 1;
569f8829a4aSRandall Stewart 	asoc->size_on_all_streams += control->length;
570f8829a4aSRandall Stewart 	sctp_ucount_incr(asoc->cnt_on_all_streams);
571f8829a4aSRandall Stewart 	strm = &asoc->strmin[control->sinfo_stream];
572f8829a4aSRandall Stewart 	nxt_todel = strm->last_sequence_delivered + 1;
573b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
574f8829a4aSRandall Stewart 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
57580fefe0aSRandall Stewart 	}
576ad81507eSRandall Stewart 	SCTPDBG(SCTP_DEBUG_INDATA1,
577ad81507eSRandall Stewart 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
578f8829a4aSRandall Stewart 	    (uint32_t) control->sinfo_stream,
579ad81507eSRandall Stewart 	    (uint32_t) strm->last_sequence_delivered,
580ad81507eSRandall Stewart 	    (uint32_t) nxt_todel);
58120b07a4dSMichael Tuexen 	if (SCTP_SSN_GE(strm->last_sequence_delivered, control->sinfo_ssn)) {
582f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
583ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
584ad81507eSRandall Stewart 		    control->sinfo_ssn, strm->last_sequence_delivered);
585c40e9cf2SRandall Stewart protocol_error:
586f8829a4aSRandall Stewart 		/*
587f8829a4aSRandall Stewart 		 * throw it in the stream so it gets cleaned up in
588f8829a4aSRandall Stewart 		 * association destruction
589f8829a4aSRandall Stewart 		 */
590f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
591139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
592f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
593f8829a4aSRandall Stewart 		if (oper) {
594f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
595f8829a4aSRandall Stewart 			uint32_t *ippp;
596f8829a4aSRandall Stewart 
597139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
598f8829a4aSRandall Stewart 			    (sizeof(uint32_t) * 3);
599f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
600f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
601139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
602f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
603a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
604f8829a4aSRandall Stewart 			ippp++;
605f8829a4aSRandall Stewart 			*ippp = control->sinfo_tsn;
606f8829a4aSRandall Stewart 			ippp++;
607f8829a4aSRandall Stewart 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
608f8829a4aSRandall Stewart 		}
609a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
610a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
611f8829a4aSRandall Stewart 		*abort_flag = 1;
612f8829a4aSRandall Stewart 		return;
613f8829a4aSRandall Stewart 
614f8829a4aSRandall Stewart 	}
615f8829a4aSRandall Stewart 	if (nxt_todel == control->sinfo_ssn) {
616f8829a4aSRandall Stewart 		/* can be delivered right away? */
617b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
618f8829a4aSRandall Stewart 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
61980fefe0aSRandall Stewart 		}
620830d754dSRandall Stewart 		/* EY it wont be queued if it could be delivered directly */
621f8829a4aSRandall Stewart 		queue_needed = 0;
622f8829a4aSRandall Stewart 		asoc->size_on_all_streams -= control->length;
623f8829a4aSRandall Stewart 		sctp_ucount_decr(asoc->cnt_on_all_streams);
624f8829a4aSRandall Stewart 		strm->last_sequence_delivered++;
62577acdc25SRandall Stewart 
626b5c16493SMichael Tuexen 		sctp_mark_non_revokable(asoc, control->sinfo_tsn);
627f8829a4aSRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
628f8829a4aSRandall Stewart 		    control,
629cfde3ff7SRandall Stewart 		    &stcb->sctp_socket->so_rcv, 1,
630cfde3ff7SRandall Stewart 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
6314a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(control, &strm->inqueue, next, at) {
632f8829a4aSRandall Stewart 			/* all delivered */
633f8829a4aSRandall Stewart 			nxt_todel = strm->last_sequence_delivered + 1;
634f8829a4aSRandall Stewart 			if (nxt_todel == control->sinfo_ssn) {
635f8829a4aSRandall Stewart 				TAILQ_REMOVE(&strm->inqueue, control, next);
636f8829a4aSRandall Stewart 				asoc->size_on_all_streams -= control->length;
637f8829a4aSRandall Stewart 				sctp_ucount_decr(asoc->cnt_on_all_streams);
638f8829a4aSRandall Stewart 				strm->last_sequence_delivered++;
639f8829a4aSRandall Stewart 				/*
640f8829a4aSRandall Stewart 				 * We ignore the return of deliver_data here
641f8829a4aSRandall Stewart 				 * since we always can hold the chunk on the
642f8829a4aSRandall Stewart 				 * d-queue. And we have a finite number that
643f8829a4aSRandall Stewart 				 * can be delivered from the strq.
644f8829a4aSRandall Stewart 				 */
645b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
646f8829a4aSRandall Stewart 					sctp_log_strm_del(control, NULL,
647f8829a4aSRandall Stewart 					    SCTP_STR_LOG_FROM_IMMED_DEL);
64880fefe0aSRandall Stewart 				}
649b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, control->sinfo_tsn);
650f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
651f8829a4aSRandall Stewart 				    control,
652cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1,
653cfde3ff7SRandall Stewart 				    SCTP_READ_LOCK_NOT_HELD,
654cfde3ff7SRandall Stewart 				    SCTP_SO_NOT_LOCKED);
655f8829a4aSRandall Stewart 				continue;
656f8829a4aSRandall Stewart 			}
657f8829a4aSRandall Stewart 			break;
658f8829a4aSRandall Stewart 		}
659f8829a4aSRandall Stewart 	}
660f8829a4aSRandall Stewart 	if (queue_needed) {
661f8829a4aSRandall Stewart 		/*
662f8829a4aSRandall Stewart 		 * Ok, we did not deliver this guy, find the correct place
663f8829a4aSRandall Stewart 		 * to put it on the queue.
664f8829a4aSRandall Stewart 		 */
66520b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->cumulative_tsn, control->sinfo_tsn)) {
666c40e9cf2SRandall Stewart 			goto protocol_error;
667c40e9cf2SRandall Stewart 		}
668f8829a4aSRandall Stewart 		if (TAILQ_EMPTY(&strm->inqueue)) {
669f8829a4aSRandall Stewart 			/* Empty queue */
670b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
671f8829a4aSRandall Stewart 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
67280fefe0aSRandall Stewart 			}
673f8829a4aSRandall Stewart 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
674f8829a4aSRandall Stewart 		} else {
675f8829a4aSRandall Stewart 			TAILQ_FOREACH(at, &strm->inqueue, next) {
67620b07a4dSMichael Tuexen 				if (SCTP_SSN_GT(at->sinfo_ssn, control->sinfo_ssn)) {
677f8829a4aSRandall Stewart 					/*
678f8829a4aSRandall Stewart 					 * one in queue is bigger than the
679f8829a4aSRandall Stewart 					 * new one, insert before this one
680f8829a4aSRandall Stewart 					 */
681b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
682f8829a4aSRandall Stewart 						sctp_log_strm_del(control, at,
683f8829a4aSRandall Stewart 						    SCTP_STR_LOG_FROM_INSERT_MD);
68480fefe0aSRandall Stewart 					}
685f8829a4aSRandall Stewart 					TAILQ_INSERT_BEFORE(at, control, next);
686f8829a4aSRandall Stewart 					break;
687f8829a4aSRandall Stewart 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
688f8829a4aSRandall Stewart 					/*
689f8829a4aSRandall Stewart 					 * Gak, He sent me a duplicate str
690f8829a4aSRandall Stewart 					 * seq number
691f8829a4aSRandall Stewart 					 */
692f8829a4aSRandall Stewart 					/*
693f8829a4aSRandall Stewart 					 * foo bar, I guess I will just free
694f8829a4aSRandall Stewart 					 * this new guy, should we abort
695f8829a4aSRandall Stewart 					 * too? FIX ME MAYBE? Or it COULD be
696f8829a4aSRandall Stewart 					 * that the SSN's have wrapped.
697f8829a4aSRandall Stewart 					 * Maybe I should compare to TSN
698f8829a4aSRandall Stewart 					 * somehow... sigh for now just blow
699f8829a4aSRandall Stewart 					 * away the chunk!
700f8829a4aSRandall Stewart 					 */
701f8829a4aSRandall Stewart 
702f8829a4aSRandall Stewart 					if (control->data)
703f8829a4aSRandall Stewart 						sctp_m_freem(control->data);
704f8829a4aSRandall Stewart 					control->data = NULL;
705f8829a4aSRandall Stewart 					asoc->size_on_all_streams -= control->length;
706f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->cnt_on_all_streams);
70724f52bbdSMichael Tuexen 					if (control->whoFrom) {
708f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
7095bead436SRandall Stewart 						control->whoFrom = NULL;
71024f52bbdSMichael Tuexen 					}
711f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
712f8829a4aSRandall Stewart 					return;
713f8829a4aSRandall Stewart 				} else {
714f8829a4aSRandall Stewart 					if (TAILQ_NEXT(at, next) == NULL) {
715f8829a4aSRandall Stewart 						/*
716f8829a4aSRandall Stewart 						 * We are at the end, insert
717f8829a4aSRandall Stewart 						 * it after this one
718f8829a4aSRandall Stewart 						 */
719b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
720f8829a4aSRandall Stewart 							sctp_log_strm_del(control, at,
721f8829a4aSRandall Stewart 							    SCTP_STR_LOG_FROM_INSERT_TL);
72280fefe0aSRandall Stewart 						}
723f8829a4aSRandall Stewart 						TAILQ_INSERT_AFTER(&strm->inqueue,
724f8829a4aSRandall Stewart 						    at, control, next);
725f8829a4aSRandall Stewart 						break;
726f8829a4aSRandall Stewart 					}
727f8829a4aSRandall Stewart 				}
728f8829a4aSRandall Stewart 			}
729f8829a4aSRandall Stewart 		}
730f8829a4aSRandall Stewart 	}
731f8829a4aSRandall Stewart }
732f8829a4aSRandall Stewart 
733f8829a4aSRandall Stewart /*
734f8829a4aSRandall Stewart  * Returns two things: You get the total size of the deliverable parts of the
735f8829a4aSRandall Stewart  * first fragmented message on the reassembly queue. And you get a 1 back if
736f8829a4aSRandall Stewart  * all of the message is ready or a 0 back if the message is still incomplete
737f8829a4aSRandall Stewart  */
738f8829a4aSRandall Stewart static int
739f8829a4aSRandall Stewart sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
740f8829a4aSRandall Stewart {
741f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
742f8829a4aSRandall Stewart 	uint32_t tsn;
743f8829a4aSRandall Stewart 
744f8829a4aSRandall Stewart 	*t_size = 0;
745f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
746f8829a4aSRandall Stewart 	if (chk == NULL) {
747f8829a4aSRandall Stewart 		/* nothing on the queue */
748f8829a4aSRandall Stewart 		return (0);
749f8829a4aSRandall Stewart 	}
750f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
751f8829a4aSRandall Stewart 		/* Not a first on the queue */
752f8829a4aSRandall Stewart 		return (0);
753f8829a4aSRandall Stewart 	}
754f8829a4aSRandall Stewart 	tsn = chk->rec.data.TSN_seq;
7554a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(chk, &asoc->reasmqueue, sctp_next) {
756f8829a4aSRandall Stewart 		if (tsn != chk->rec.data.TSN_seq) {
757f8829a4aSRandall Stewart 			return (0);
758f8829a4aSRandall Stewart 		}
759f8829a4aSRandall Stewart 		*t_size += chk->send_size;
760f8829a4aSRandall Stewart 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
761f8829a4aSRandall Stewart 			return (1);
762f8829a4aSRandall Stewart 		}
763f8829a4aSRandall Stewart 		tsn++;
764f8829a4aSRandall Stewart 	}
765f8829a4aSRandall Stewart 	return (0);
766f8829a4aSRandall Stewart }
767f8829a4aSRandall Stewart 
768f8829a4aSRandall Stewart static void
769f8829a4aSRandall Stewart sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
770f8829a4aSRandall Stewart {
771f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
772f8829a4aSRandall Stewart 	uint16_t nxt_todel;
773810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
774f8829a4aSRandall Stewart 
775139bc87fSRandall Stewart doit_again:
776f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
777f8829a4aSRandall Stewart 	if (chk == NULL) {
778f8829a4aSRandall Stewart 		/* Huh? */
779f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
780f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
781f8829a4aSRandall Stewart 		return;
782f8829a4aSRandall Stewart 	}
783f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress == 0) {
784f8829a4aSRandall Stewart 		nxt_todel =
785f8829a4aSRandall Stewart 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
786f8829a4aSRandall Stewart 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
787f8829a4aSRandall Stewart 		    (nxt_todel == chk->rec.data.stream_seq ||
788f8829a4aSRandall Stewart 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
789f8829a4aSRandall Stewart 			/*
790f8829a4aSRandall Stewart 			 * Yep the first one is here and its ok to deliver
791f8829a4aSRandall Stewart 			 * but should we?
792f8829a4aSRandall Stewart 			 */
793810ec536SMichael Tuexen 			if (stcb->sctp_socket) {
79424ae5c4aSMichael Tuexen 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
795810ec536SMichael Tuexen 				    stcb->sctp_ep->partial_delivery_point);
796810ec536SMichael Tuexen 			} else {
797810ec536SMichael Tuexen 				pd_point = stcb->sctp_ep->partial_delivery_point;
798810ec536SMichael Tuexen 			}
799810ec536SMichael Tuexen 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
800f8829a4aSRandall Stewart 
801f8829a4aSRandall Stewart 				/*
802f8829a4aSRandall Stewart 				 * Yes, we setup to start reception, by
803f8829a4aSRandall Stewart 				 * backing down the TSN just in case we
804f8829a4aSRandall Stewart 				 * can't deliver. If we
805f8829a4aSRandall Stewart 				 */
806f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 1;
807f8829a4aSRandall Stewart 				asoc->tsn_last_delivered =
808f8829a4aSRandall Stewart 				    chk->rec.data.TSN_seq - 1;
809f8829a4aSRandall Stewart 				asoc->str_of_pdapi =
810f8829a4aSRandall Stewart 				    chk->rec.data.stream_number;
811f8829a4aSRandall Stewart 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
812f8829a4aSRandall Stewart 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
813f8829a4aSRandall Stewart 				asoc->fragment_flags = chk->rec.data.rcv_flags;
814f8829a4aSRandall Stewart 				sctp_service_reassembly(stcb, asoc);
815f8829a4aSRandall Stewart 			}
816f8829a4aSRandall Stewart 		}
817f8829a4aSRandall Stewart 	} else {
818139bc87fSRandall Stewart 		/*
819139bc87fSRandall Stewart 		 * Service re-assembly will deliver stream data queued at
820139bc87fSRandall Stewart 		 * the end of fragmented delivery.. but it wont know to go
821139bc87fSRandall Stewart 		 * back and call itself again... we do that here with the
822139bc87fSRandall Stewart 		 * got doit_again
823139bc87fSRandall Stewart 		 */
824f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
825139bc87fSRandall Stewart 		if (asoc->fragmented_delivery_inprogress == 0) {
826139bc87fSRandall Stewart 			/*
827139bc87fSRandall Stewart 			 * finished our Fragmented delivery, could be more
828139bc87fSRandall Stewart 			 * waiting?
829139bc87fSRandall Stewart 			 */
830139bc87fSRandall Stewart 			goto doit_again;
831139bc87fSRandall Stewart 		}
832f8829a4aSRandall Stewart 	}
833f8829a4aSRandall Stewart }
834f8829a4aSRandall Stewart 
835f8829a4aSRandall Stewart /*
836f8829a4aSRandall Stewart  * Dump onto the re-assembly queue, in its proper place. After dumping on the
837f8829a4aSRandall Stewart  * queue, see if anthing can be delivered. If so pull it off (or as much as
838f8829a4aSRandall Stewart  * we can. If we run out of space then we must dump what we can and set the
839f8829a4aSRandall Stewart  * appropriate flag to say we queued what we could.
840f8829a4aSRandall Stewart  */
841f8829a4aSRandall Stewart static void
842f8829a4aSRandall Stewart sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
843f8829a4aSRandall Stewart     struct sctp_tmit_chunk *chk, int *abort_flag)
844f8829a4aSRandall Stewart {
845f8829a4aSRandall Stewart 	struct mbuf *oper;
84660990c0cSMichael Tuexen 	uint32_t cum_ackp1, prev_tsn, post_tsn;
847f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at, *prev, *next;
848f8829a4aSRandall Stewart 
849f8829a4aSRandall Stewart 	prev = next = NULL;
850f8829a4aSRandall Stewart 	cum_ackp1 = asoc->tsn_last_delivered + 1;
851f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
852f8829a4aSRandall Stewart 		/* This is the first one on the queue */
853f8829a4aSRandall Stewart 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
854f8829a4aSRandall Stewart 		/*
855f8829a4aSRandall Stewart 		 * we do not check for delivery of anything when only one
856f8829a4aSRandall Stewart 		 * fragment is here
857f8829a4aSRandall Stewart 		 */
858f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = chk->send_size;
859f8829a4aSRandall Stewart 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
860f8829a4aSRandall Stewart 		if (chk->rec.data.TSN_seq == cum_ackp1) {
861f8829a4aSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0 &&
862f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
863f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG) {
864f8829a4aSRandall Stewart 				/*
865f8829a4aSRandall Stewart 				 * An empty queue, no delivery inprogress,
866f8829a4aSRandall Stewart 				 * we hit the next one and it does NOT have
867f8829a4aSRandall Stewart 				 * a FIRST fragment mark.
868f8829a4aSRandall Stewart 				 */
869ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
870139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
871f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
872f8829a4aSRandall Stewart 
873f8829a4aSRandall Stewart 				if (oper) {
874f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
875f8829a4aSRandall Stewart 					uint32_t *ippp;
876f8829a4aSRandall Stewart 
877139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
878f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
879f8829a4aSRandall Stewart 					    (sizeof(uint32_t) * 3);
880f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
881f8829a4aSRandall Stewart 					ph->param_type =
882f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
883139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
884f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
885a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
886f8829a4aSRandall Stewart 					ippp++;
887f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
888f8829a4aSRandall Stewart 					ippp++;
889f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
890f8829a4aSRandall Stewart 
891f8829a4aSRandall Stewart 				}
892a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
893a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
894f8829a4aSRandall Stewart 				*abort_flag = 1;
895f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress &&
896f8829a4aSRandall Stewart 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
897f8829a4aSRandall Stewart 				/*
898f8829a4aSRandall Stewart 				 * We are doing a partial delivery and the
899f8829a4aSRandall Stewart 				 * NEXT chunk MUST be either the LAST or
900f8829a4aSRandall Stewart 				 * MIDDLE fragment NOT a FIRST
901f8829a4aSRandall Stewart 				 */
902ad81507eSRandall Stewart 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
903139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
904f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
905f8829a4aSRandall Stewart 				if (oper) {
906f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
907f8829a4aSRandall Stewart 					uint32_t *ippp;
908f8829a4aSRandall Stewart 
909139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
910f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
911f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
912f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
913f8829a4aSRandall Stewart 					ph->param_type =
914f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
915139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
916f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
917a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
918f8829a4aSRandall Stewart 					ippp++;
919f8829a4aSRandall Stewart 					*ippp = chk->rec.data.TSN_seq;
920f8829a4aSRandall Stewart 					ippp++;
921f8829a4aSRandall Stewart 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
922f8829a4aSRandall Stewart 				}
923a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
924a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
925f8829a4aSRandall Stewart 				*abort_flag = 1;
926f8829a4aSRandall Stewart 			} else if (asoc->fragmented_delivery_inprogress) {
927f8829a4aSRandall Stewart 				/*
928f8829a4aSRandall Stewart 				 * Here we are ok with a MIDDLE or LAST
929f8829a4aSRandall Stewart 				 * piece
930f8829a4aSRandall Stewart 				 */
931f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
932f8829a4aSRandall Stewart 				    asoc->str_of_pdapi) {
933f8829a4aSRandall Stewart 					/* Got to be the right STR No */
934ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
935f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
936f8829a4aSRandall Stewart 					    asoc->str_of_pdapi);
937139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
938f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
939f8829a4aSRandall Stewart 					if (oper) {
940f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
941f8829a4aSRandall Stewart 						uint32_t *ippp;
942f8829a4aSRandall Stewart 
943139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
944f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
945f8829a4aSRandall Stewart 						    (sizeof(uint32_t) * 3);
946f8829a4aSRandall Stewart 						ph = mtod(oper,
947f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
948f8829a4aSRandall Stewart 						ph->param_type =
949f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
950f8829a4aSRandall Stewart 						ph->param_length =
951139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
952f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
953a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
954f8829a4aSRandall Stewart 						ippp++;
955f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
956f8829a4aSRandall Stewart 						ippp++;
957f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
958f8829a4aSRandall Stewart 					}
959a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
960a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
961f8829a4aSRandall Stewart 					*abort_flag = 1;
962f8829a4aSRandall Stewart 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
963f8829a4aSRandall Stewart 					    SCTP_DATA_UNORDERED &&
964b5c16493SMichael Tuexen 				    chk->rec.data.stream_seq != asoc->ssn_of_pdapi) {
965f8829a4aSRandall Stewart 					/* Got to be the right STR Seq */
966ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
967f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
968f8829a4aSRandall Stewart 					    asoc->ssn_of_pdapi);
969139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
970f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
971f8829a4aSRandall Stewart 					if (oper) {
972f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
973f8829a4aSRandall Stewart 						uint32_t *ippp;
974f8829a4aSRandall Stewart 
975139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
976f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
977f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
978f8829a4aSRandall Stewart 						ph = mtod(oper,
979f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
980f8829a4aSRandall Stewart 						ph->param_type =
981f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
982f8829a4aSRandall Stewart 						ph->param_length =
983139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
984f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
985a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
986f8829a4aSRandall Stewart 						ippp++;
987f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
988f8829a4aSRandall Stewart 						ippp++;
989f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
990f8829a4aSRandall Stewart 
991f8829a4aSRandall Stewart 					}
992a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
993a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
994f8829a4aSRandall Stewart 					*abort_flag = 1;
995f8829a4aSRandall Stewart 				}
996f8829a4aSRandall Stewart 			}
997f8829a4aSRandall Stewart 		}
998f8829a4aSRandall Stewart 		return;
999f8829a4aSRandall Stewart 	}
1000f8829a4aSRandall Stewart 	/* Find its place */
1001f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
100220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(at->rec.data.TSN_seq, chk->rec.data.TSN_seq)) {
1003f8829a4aSRandall Stewart 			/*
1004f8829a4aSRandall Stewart 			 * one in queue is bigger than the new one, insert
1005f8829a4aSRandall Stewart 			 * before this one
1006f8829a4aSRandall Stewart 			 */
1007f8829a4aSRandall Stewart 			/* A check */
1008f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue += chk->send_size;
1009f8829a4aSRandall Stewart 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1010f8829a4aSRandall Stewart 			next = at;
1011f8829a4aSRandall Stewart 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1012f8829a4aSRandall Stewart 			break;
1013f8829a4aSRandall Stewart 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1014f8829a4aSRandall Stewart 			/* Gak, He sent me a duplicate str seq number */
1015f8829a4aSRandall Stewart 			/*
1016f8829a4aSRandall Stewart 			 * foo bar, I guess I will just free this new guy,
1017f8829a4aSRandall Stewart 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1018f8829a4aSRandall Stewart 			 * that the SSN's have wrapped. Maybe I should
1019f8829a4aSRandall Stewart 			 * compare to TSN somehow... sigh for now just blow
1020f8829a4aSRandall Stewart 			 * away the chunk!
1021f8829a4aSRandall Stewart 			 */
1022f8829a4aSRandall Stewart 			if (chk->data) {
1023f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
1024f8829a4aSRandall Stewart 				chk->data = NULL;
1025f8829a4aSRandall Stewart 			}
1026689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
1027f8829a4aSRandall Stewart 			return;
1028f8829a4aSRandall Stewart 		} else {
1029f8829a4aSRandall Stewart 			prev = at;
1030f8829a4aSRandall Stewart 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1031f8829a4aSRandall Stewart 				/*
1032f8829a4aSRandall Stewart 				 * We are at the end, insert it after this
1033f8829a4aSRandall Stewart 				 * one
1034f8829a4aSRandall Stewart 				 */
1035f8829a4aSRandall Stewart 				/* check it first */
1036f8829a4aSRandall Stewart 				asoc->size_on_reasm_queue += chk->send_size;
1037f8829a4aSRandall Stewart 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1038f8829a4aSRandall Stewart 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1039f8829a4aSRandall Stewart 				break;
1040f8829a4aSRandall Stewart 			}
1041f8829a4aSRandall Stewart 		}
1042f8829a4aSRandall Stewart 	}
1043f8829a4aSRandall Stewart 	/* Now the audits */
1044f8829a4aSRandall Stewart 	if (prev) {
1045f8829a4aSRandall Stewart 		prev_tsn = chk->rec.data.TSN_seq - 1;
1046f8829a4aSRandall Stewart 		if (prev_tsn == prev->rec.data.TSN_seq) {
1047f8829a4aSRandall Stewart 			/*
1048f8829a4aSRandall Stewart 			 * Ok the one I am dropping onto the end is the
1049f8829a4aSRandall Stewart 			 * NEXT. A bit of valdiation here.
1050f8829a4aSRandall Stewart 			 */
1051f8829a4aSRandall Stewart 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1052f8829a4aSRandall Stewart 			    SCTP_DATA_FIRST_FRAG ||
1053f8829a4aSRandall Stewart 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1054f8829a4aSRandall Stewart 			    SCTP_DATA_MIDDLE_FRAG) {
1055f8829a4aSRandall Stewart 				/*
1056f8829a4aSRandall Stewart 				 * Insert chk MUST be a MIDDLE or LAST
1057f8829a4aSRandall Stewart 				 * fragment
1058f8829a4aSRandall Stewart 				 */
1059f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1060f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1061ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1062ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1063139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1064f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1065f8829a4aSRandall Stewart 					if (oper) {
1066f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1067f8829a4aSRandall Stewart 						uint32_t *ippp;
1068f8829a4aSRandall Stewart 
1069139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1070f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1071f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1072f8829a4aSRandall Stewart 						ph = mtod(oper,
1073f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1074f8829a4aSRandall Stewart 						ph->param_type =
1075f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1076f8829a4aSRandall Stewart 						ph->param_length =
1077139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1078f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1079a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1080f8829a4aSRandall Stewart 						ippp++;
1081f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1082f8829a4aSRandall Stewart 						ippp++;
1083f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1084f8829a4aSRandall Stewart 
1085f8829a4aSRandall Stewart 					}
1086a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1087a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1088f8829a4aSRandall Stewart 					*abort_flag = 1;
1089f8829a4aSRandall Stewart 					return;
1090f8829a4aSRandall Stewart 				}
1091f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1092f8829a4aSRandall Stewart 				    prev->rec.data.stream_number) {
1093f8829a4aSRandall Stewart 					/*
1094f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1095f8829a4aSRandall Stewart 					 * they must be the same.
1096f8829a4aSRandall Stewart 					 */
1097ad81507eSRandall Stewart 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1098f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1099f8829a4aSRandall Stewart 					    prev->rec.data.stream_number);
1100139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1101f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1102f8829a4aSRandall Stewart 					if (oper) {
1103f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1104f8829a4aSRandall Stewart 						uint32_t *ippp;
1105f8829a4aSRandall Stewart 
1106139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1107f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1108f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1109f8829a4aSRandall Stewart 						ph = mtod(oper,
1110f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1111f8829a4aSRandall Stewart 						ph->param_type =
1112f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1113f8829a4aSRandall Stewart 						ph->param_length =
1114139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1115f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1116a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1117f8829a4aSRandall Stewart 						ippp++;
1118f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1119f8829a4aSRandall Stewart 						ippp++;
1120f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1121f8829a4aSRandall Stewart 					}
1122a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1123a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1124f8829a4aSRandall Stewart 					*abort_flag = 1;
1125f8829a4aSRandall Stewart 					return;
1126f8829a4aSRandall Stewart 				}
1127f8829a4aSRandall Stewart 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1128f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1129f8829a4aSRandall Stewart 				    prev->rec.data.stream_seq) {
1130f8829a4aSRandall Stewart 					/*
1131f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1132f8829a4aSRandall Stewart 					 * they must be the same.
1133f8829a4aSRandall Stewart 					 */
1134ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1135f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1136f8829a4aSRandall Stewart 					    prev->rec.data.stream_seq);
1137139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1138f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1139f8829a4aSRandall Stewart 					if (oper) {
1140f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1141f8829a4aSRandall Stewart 						uint32_t *ippp;
1142f8829a4aSRandall Stewart 
1143139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1144f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1145f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1146f8829a4aSRandall Stewart 						ph = mtod(oper,
1147f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1148f8829a4aSRandall Stewart 						ph->param_type =
1149f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1150f8829a4aSRandall Stewart 						ph->param_length =
1151139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1152f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1153a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1154f8829a4aSRandall Stewart 						ippp++;
1155f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1156f8829a4aSRandall Stewart 						ippp++;
1157f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1158f8829a4aSRandall Stewart 					}
1159a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1160a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1161f8829a4aSRandall Stewart 					*abort_flag = 1;
1162f8829a4aSRandall Stewart 					return;
1163f8829a4aSRandall Stewart 				}
1164f8829a4aSRandall Stewart 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1165f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1166f8829a4aSRandall Stewart 				/* Insert chk MUST be a FIRST */
1167f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1168f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1169ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1170139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1171f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1172f8829a4aSRandall Stewart 					if (oper) {
1173f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1174f8829a4aSRandall Stewart 						uint32_t *ippp;
1175f8829a4aSRandall Stewart 
1176139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1177f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1178f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1179f8829a4aSRandall Stewart 						ph = mtod(oper,
1180f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1181f8829a4aSRandall Stewart 						ph->param_type =
1182f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1183f8829a4aSRandall Stewart 						ph->param_length =
1184139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1185f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1186a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1187f8829a4aSRandall Stewart 						ippp++;
1188f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1189f8829a4aSRandall Stewart 						ippp++;
1190f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1191f8829a4aSRandall Stewart 
1192f8829a4aSRandall Stewart 					}
1193a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1194a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1195f8829a4aSRandall Stewart 					*abort_flag = 1;
1196f8829a4aSRandall Stewart 					return;
1197f8829a4aSRandall Stewart 				}
1198f8829a4aSRandall Stewart 			}
1199f8829a4aSRandall Stewart 		}
1200f8829a4aSRandall Stewart 	}
1201f8829a4aSRandall Stewart 	if (next) {
1202f8829a4aSRandall Stewart 		post_tsn = chk->rec.data.TSN_seq + 1;
1203f8829a4aSRandall Stewart 		if (post_tsn == next->rec.data.TSN_seq) {
1204f8829a4aSRandall Stewart 			/*
1205f8829a4aSRandall Stewart 			 * Ok the one I am inserting ahead of is my NEXT
1206f8829a4aSRandall Stewart 			 * one. A bit of valdiation here.
1207f8829a4aSRandall Stewart 			 */
1208f8829a4aSRandall Stewart 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1209f8829a4aSRandall Stewart 				/* Insert chk MUST be a last fragment */
1210f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1211f8829a4aSRandall Stewart 				    != SCTP_DATA_LAST_FRAG) {
1212ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1213ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1214139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1215f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1216f8829a4aSRandall Stewart 					if (oper) {
1217f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1218f8829a4aSRandall Stewart 						uint32_t *ippp;
1219f8829a4aSRandall Stewart 
1220139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1221f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1222f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1223f8829a4aSRandall Stewart 						ph = mtod(oper,
1224f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1225f8829a4aSRandall Stewart 						ph->param_type =
1226f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1227f8829a4aSRandall Stewart 						ph->param_length =
1228139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1229f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1230a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1231f8829a4aSRandall Stewart 						ippp++;
1232f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1233f8829a4aSRandall Stewart 						ippp++;
1234f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1235f8829a4aSRandall Stewart 					}
1236a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1237a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1238f8829a4aSRandall Stewart 					*abort_flag = 1;
1239f8829a4aSRandall Stewart 					return;
1240f8829a4aSRandall Stewart 				}
1241f8829a4aSRandall Stewart 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1242f8829a4aSRandall Stewart 				    SCTP_DATA_MIDDLE_FRAG ||
1243f8829a4aSRandall Stewart 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1244f8829a4aSRandall Stewart 			    SCTP_DATA_LAST_FRAG) {
1245f8829a4aSRandall Stewart 				/*
1246f8829a4aSRandall Stewart 				 * Insert chk CAN be MIDDLE or FIRST NOT
1247f8829a4aSRandall Stewart 				 * LAST
1248f8829a4aSRandall Stewart 				 */
1249f8829a4aSRandall Stewart 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1250f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1251ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1252ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1253139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1254f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1255f8829a4aSRandall Stewart 					if (oper) {
1256f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1257f8829a4aSRandall Stewart 						uint32_t *ippp;
1258f8829a4aSRandall Stewart 
1259139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1260f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1261f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1262f8829a4aSRandall Stewart 						ph = mtod(oper,
1263f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1264f8829a4aSRandall Stewart 						ph->param_type =
1265f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1266f8829a4aSRandall Stewart 						ph->param_length =
1267139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1268f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1269a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1270f8829a4aSRandall Stewart 						ippp++;
1271f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1272f8829a4aSRandall Stewart 						ippp++;
1273f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1274f8829a4aSRandall Stewart 
1275f8829a4aSRandall Stewart 					}
1276a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1277a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1278f8829a4aSRandall Stewart 					*abort_flag = 1;
1279f8829a4aSRandall Stewart 					return;
1280f8829a4aSRandall Stewart 				}
1281f8829a4aSRandall Stewart 				if (chk->rec.data.stream_number !=
1282f8829a4aSRandall Stewart 				    next->rec.data.stream_number) {
1283f8829a4aSRandall Stewart 					/*
1284f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1285f8829a4aSRandall Stewart 					 * they must be the same.
1286f8829a4aSRandall Stewart 					 */
1287ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1288f8829a4aSRandall Stewart 					    chk->rec.data.stream_number,
1289f8829a4aSRandall Stewart 					    next->rec.data.stream_number);
1290139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1291f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1292f8829a4aSRandall Stewart 					if (oper) {
1293f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1294f8829a4aSRandall Stewart 						uint32_t *ippp;
1295f8829a4aSRandall Stewart 
1296139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1297f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1298f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1299f8829a4aSRandall Stewart 						ph = mtod(oper,
1300f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1301f8829a4aSRandall Stewart 						ph->param_type =
1302f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1303f8829a4aSRandall Stewart 						ph->param_length =
1304139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1305f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1306a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1307f8829a4aSRandall Stewart 						ippp++;
1308f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1309f8829a4aSRandall Stewart 						ippp++;
1310f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1311f8829a4aSRandall Stewart 
1312f8829a4aSRandall Stewart 					}
1313a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1314a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1315f8829a4aSRandall Stewart 					*abort_flag = 1;
1316f8829a4aSRandall Stewart 					return;
1317f8829a4aSRandall Stewart 				}
1318f8829a4aSRandall Stewart 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1319f8829a4aSRandall Stewart 				    chk->rec.data.stream_seq !=
1320f8829a4aSRandall Stewart 				    next->rec.data.stream_seq) {
1321f8829a4aSRandall Stewart 					/*
1322f8829a4aSRandall Stewart 					 * Huh, need the correct STR here,
1323f8829a4aSRandall Stewart 					 * they must be the same.
1324f8829a4aSRandall Stewart 					 */
1325ad81507eSRandall Stewart 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1326f8829a4aSRandall Stewart 					    chk->rec.data.stream_seq,
1327f8829a4aSRandall Stewart 					    next->rec.data.stream_seq);
1328139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1329f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1330f8829a4aSRandall Stewart 					if (oper) {
1331f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1332f8829a4aSRandall Stewart 						uint32_t *ippp;
1333f8829a4aSRandall Stewart 
1334139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1335f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1336f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1337f8829a4aSRandall Stewart 						ph = mtod(oper,
1338f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1339f8829a4aSRandall Stewart 						ph->param_type =
1340f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1341f8829a4aSRandall Stewart 						ph->param_length =
1342139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1343f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1344a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1345f8829a4aSRandall Stewart 						ippp++;
1346f8829a4aSRandall Stewart 						*ippp = chk->rec.data.TSN_seq;
1347f8829a4aSRandall Stewart 						ippp++;
1348f8829a4aSRandall Stewart 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1349f8829a4aSRandall Stewart 					}
1350a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1351a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1352f8829a4aSRandall Stewart 					*abort_flag = 1;
1353f8829a4aSRandall Stewart 					return;
1354f8829a4aSRandall Stewart 				}
1355f8829a4aSRandall Stewart 			}
1356f8829a4aSRandall Stewart 		}
1357f8829a4aSRandall Stewart 	}
1358f8829a4aSRandall Stewart 	/* Do we need to do some delivery? check */
1359f8829a4aSRandall Stewart 	sctp_deliver_reasm_check(stcb, asoc);
1360f8829a4aSRandall Stewart }
1361f8829a4aSRandall Stewart 
1362f8829a4aSRandall Stewart /*
1363f8829a4aSRandall Stewart  * This is an unfortunate routine. It checks to make sure a evil guy is not
1364f8829a4aSRandall Stewart  * stuffing us full of bad packet fragments. A broken peer could also do this
1365f8829a4aSRandall Stewart  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1366f8829a4aSRandall Stewart  * :< more cycles.
1367f8829a4aSRandall Stewart  */
1368f8829a4aSRandall Stewart static int
1369f8829a4aSRandall Stewart sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1370f8829a4aSRandall Stewart     uint32_t TSN_seq)
1371f8829a4aSRandall Stewart {
1372f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *at;
1373f8829a4aSRandall Stewart 	uint32_t tsn_est;
1374f8829a4aSRandall Stewart 
1375f8829a4aSRandall Stewart 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
137620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(TSN_seq, at->rec.data.TSN_seq)) {
1377f8829a4aSRandall Stewart 			/* is it one bigger? */
1378f8829a4aSRandall Stewart 			tsn_est = at->rec.data.TSN_seq + 1;
1379f8829a4aSRandall Stewart 			if (tsn_est == TSN_seq) {
1380f8829a4aSRandall Stewart 				/* yep. It better be a last then */
1381f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1382f8829a4aSRandall Stewart 				    SCTP_DATA_LAST_FRAG) {
1383f8829a4aSRandall Stewart 					/*
1384f8829a4aSRandall Stewart 					 * Ok this guy belongs next to a guy
1385f8829a4aSRandall Stewart 					 * that is NOT last, it should be a
1386f8829a4aSRandall Stewart 					 * middle/last, not a complete
1387f8829a4aSRandall Stewart 					 * chunk.
1388f8829a4aSRandall Stewart 					 */
1389f8829a4aSRandall Stewart 					return (1);
1390f8829a4aSRandall Stewart 				} else {
1391f8829a4aSRandall Stewart 					/*
1392f8829a4aSRandall Stewart 					 * This guy is ok since its a LAST
1393f8829a4aSRandall Stewart 					 * and the new chunk is a fully
1394f8829a4aSRandall Stewart 					 * self- contained one.
1395f8829a4aSRandall Stewart 					 */
1396f8829a4aSRandall Stewart 					return (0);
1397f8829a4aSRandall Stewart 				}
1398f8829a4aSRandall Stewart 			}
1399f8829a4aSRandall Stewart 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1400f8829a4aSRandall Stewart 			/* Software error since I have a dup? */
1401f8829a4aSRandall Stewart 			return (1);
1402f8829a4aSRandall Stewart 		} else {
1403f8829a4aSRandall Stewart 			/*
1404f8829a4aSRandall Stewart 			 * Ok, 'at' is larger than new chunk but does it
1405f8829a4aSRandall Stewart 			 * need to be right before it.
1406f8829a4aSRandall Stewart 			 */
1407f8829a4aSRandall Stewart 			tsn_est = TSN_seq + 1;
1408f8829a4aSRandall Stewart 			if (tsn_est == at->rec.data.TSN_seq) {
1409f8829a4aSRandall Stewart 				/* Yep, It better be a first */
1410f8829a4aSRandall Stewart 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1411f8829a4aSRandall Stewart 				    SCTP_DATA_FIRST_FRAG) {
1412f8829a4aSRandall Stewart 					return (1);
1413f8829a4aSRandall Stewart 				} else {
1414f8829a4aSRandall Stewart 					return (0);
1415f8829a4aSRandall Stewart 				}
1416f8829a4aSRandall Stewart 			}
1417f8829a4aSRandall Stewart 		}
1418f8829a4aSRandall Stewart 	}
1419f8829a4aSRandall Stewart 	return (0);
1420f8829a4aSRandall Stewart }
1421f8829a4aSRandall Stewart 
1422f8829a4aSRandall Stewart 
1423f8829a4aSRandall Stewart static int
1424f8829a4aSRandall Stewart sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1425f8829a4aSRandall Stewart     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1426f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1427f8829a4aSRandall Stewart     int *break_flag, int last_chunk)
1428f8829a4aSRandall Stewart {
1429f8829a4aSRandall Stewart 	/* Process a data chunk */
1430f8829a4aSRandall Stewart 	/* struct sctp_tmit_chunk *chk; */
1431f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
1432f8829a4aSRandall Stewart 	uint32_t tsn, gap;
1433f8829a4aSRandall Stewart 	struct mbuf *dmbuf;
14347215cc1bSMichael Tuexen 	int the_len;
1435139bc87fSRandall Stewart 	int need_reasm_check = 0;
1436f8829a4aSRandall Stewart 	uint16_t strmno, strmseq;
1437f8829a4aSRandall Stewart 	struct mbuf *oper;
1438f8829a4aSRandall Stewart 	struct sctp_queued_to_read *control;
1439f42a358aSRandall Stewart 	int ordered;
1440f42a358aSRandall Stewart 	uint32_t protocol_id;
1441f42a358aSRandall Stewart 	uint8_t chunk_flags;
144217205eccSRandall Stewart 	struct sctp_stream_reset_list *liste;
1443f8829a4aSRandall Stewart 
1444f8829a4aSRandall Stewart 	chk = NULL;
1445f8829a4aSRandall Stewart 	tsn = ntohl(ch->dp.tsn);
1446f42a358aSRandall Stewart 	chunk_flags = ch->ch.chunk_flags;
1447b3f1ea41SRandall Stewart 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1448b3f1ea41SRandall Stewart 		asoc->send_sack = 1;
1449b3f1ea41SRandall Stewart 	}
1450f42a358aSRandall Stewart 	protocol_id = ch->dp.protocol_id;
145137f144ebSMichael Tuexen 	ordered = ((chunk_flags & SCTP_DATA_UNORDERED) == 0);
1452b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1453c4739e2fSRandall Stewart 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
145480fefe0aSRandall Stewart 	}
1455ad81507eSRandall Stewart 	if (stcb == NULL) {
1456ad81507eSRandall Stewart 		return (0);
1457ad81507eSRandall Stewart 	}
145880fefe0aSRandall Stewart 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
145920b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
1460f8829a4aSRandall Stewart 		/* It is a duplicate */
1461f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1462f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1463f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1464f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1465f8829a4aSRandall Stewart 			asoc->numduptsns++;
1466f8829a4aSRandall Stewart 		}
1467b201f536SRandall Stewart 		asoc->send_sack = 1;
1468f8829a4aSRandall Stewart 		return (0);
1469f8829a4aSRandall Stewart 	}
1470f8829a4aSRandall Stewart 	/* Calculate the number of TSN's between the base and this TSN */
1471d50c1d79SRandall Stewart 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1472f8829a4aSRandall Stewart 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1473f8829a4aSRandall Stewart 		/* Can't hold the bit in the mapping at max array, toss it */
1474f8829a4aSRandall Stewart 		return (0);
1475f8829a4aSRandall Stewart 	}
1476f8829a4aSRandall Stewart 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1477207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
14780696e120SRandall Stewart 		if (sctp_expand_mapping_array(asoc, gap)) {
1479f8829a4aSRandall Stewart 			/* Can't expand, drop it */
1480f8829a4aSRandall Stewart 			return (0);
1481f8829a4aSRandall Stewart 		}
1482f8829a4aSRandall Stewart 	}
148320b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(tsn, *high_tsn)) {
1484f8829a4aSRandall Stewart 		*high_tsn = tsn;
1485f8829a4aSRandall Stewart 	}
1486f8829a4aSRandall Stewart 	/* See if we have received this one already */
148777acdc25SRandall Stewart 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap) ||
148877acdc25SRandall Stewart 	    SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, gap)) {
1489f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvdupdata);
1490f8829a4aSRandall Stewart 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1491f8829a4aSRandall Stewart 			/* Record a dup for the next outbound sack */
1492f8829a4aSRandall Stewart 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1493f8829a4aSRandall Stewart 			asoc->numduptsns++;
1494f8829a4aSRandall Stewart 		}
149542551e99SRandall Stewart 		asoc->send_sack = 1;
1496f8829a4aSRandall Stewart 		return (0);
1497f8829a4aSRandall Stewart 	}
1498f8829a4aSRandall Stewart 	/*
1499f8829a4aSRandall Stewart 	 * Check to see about the GONE flag, duplicates would cause a sack
1500f8829a4aSRandall Stewart 	 * to be sent up above
1501f8829a4aSRandall Stewart 	 */
1502ad81507eSRandall Stewart 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1503f8829a4aSRandall Stewart 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1504f8829a4aSRandall Stewart 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1505f8829a4aSRandall Stewart 	    ) {
1506f8829a4aSRandall Stewart 		/*
1507f8829a4aSRandall Stewart 		 * wait a minute, this guy is gone, there is no longer a
1508f8829a4aSRandall Stewart 		 * receiver. Send peer an ABORT!
1509f8829a4aSRandall Stewart 		 */
1510f8829a4aSRandall Stewart 		struct mbuf *op_err;
1511f8829a4aSRandall Stewart 
1512f8829a4aSRandall Stewart 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1513a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
1514f8829a4aSRandall Stewart 		*abort_flag = 1;
1515f8829a4aSRandall Stewart 		return (0);
1516f8829a4aSRandall Stewart 	}
1517f8829a4aSRandall Stewart 	/*
1518f8829a4aSRandall Stewart 	 * Now before going further we see if there is room. If NOT then we
1519f8829a4aSRandall Stewart 	 * MAY let one through only IF this TSN is the one we are waiting
1520f8829a4aSRandall Stewart 	 * for on a partial delivery API.
1521f8829a4aSRandall Stewart 	 */
1522f8829a4aSRandall Stewart 
1523f8829a4aSRandall Stewart 	/* now do the tests */
1524f8829a4aSRandall Stewart 	if (((asoc->cnt_on_all_streams +
1525f8829a4aSRandall Stewart 	    asoc->cnt_on_reasm_queue +
1526b3f1ea41SRandall Stewart 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1527f8829a4aSRandall Stewart 	    (((int)asoc->my_rwnd) <= 0)) {
1528f8829a4aSRandall Stewart 		/*
1529f8829a4aSRandall Stewart 		 * When we have NO room in the rwnd we check to make sure
1530f8829a4aSRandall Stewart 		 * the reader is doing its job...
1531f8829a4aSRandall Stewart 		 */
1532f8829a4aSRandall Stewart 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1533f8829a4aSRandall Stewart 			/* some to read, wake-up */
1534ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1535ceaad40aSRandall Stewart 			struct socket *so;
1536ceaad40aSRandall Stewart 
1537ceaad40aSRandall Stewart 			so = SCTP_INP_SO(stcb->sctp_ep);
1538ceaad40aSRandall Stewart 			atomic_add_int(&stcb->asoc.refcnt, 1);
1539ceaad40aSRandall Stewart 			SCTP_TCB_UNLOCK(stcb);
1540ceaad40aSRandall Stewart 			SCTP_SOCKET_LOCK(so, 1);
1541ceaad40aSRandall Stewart 			SCTP_TCB_LOCK(stcb);
1542ceaad40aSRandall Stewart 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1543ceaad40aSRandall Stewart 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1544ceaad40aSRandall Stewart 				/* assoc was freed while we were unlocked */
1545ceaad40aSRandall Stewart 				SCTP_SOCKET_UNLOCK(so, 1);
1546ceaad40aSRandall Stewart 				return (0);
1547ceaad40aSRandall Stewart 			}
1548ceaad40aSRandall Stewart #endif
1549f8829a4aSRandall Stewart 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1550ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1551ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
1552ceaad40aSRandall Stewart #endif
1553f8829a4aSRandall Stewart 		}
1554f8829a4aSRandall Stewart 		/* now is it in the mapping array of what we have accepted? */
155520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map) &&
155620b07a4dSMichael Tuexen 		    SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1557f8829a4aSRandall Stewart 			/* Nope not in the valid range dump it */
1558f8829a4aSRandall Stewart 			sctp_set_rwnd(stcb, asoc);
1559f8829a4aSRandall Stewart 			if ((asoc->cnt_on_all_streams +
1560f8829a4aSRandall Stewart 			    asoc->cnt_on_reasm_queue +
1561b3f1ea41SRandall Stewart 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1562f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadropchklmt);
1563f8829a4aSRandall Stewart 			} else {
1564f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_datadroprwnd);
1565f8829a4aSRandall Stewart 			}
1566f8829a4aSRandall Stewart 			*break_flag = 1;
1567f8829a4aSRandall Stewart 			return (0);
1568f8829a4aSRandall Stewart 		}
1569f8829a4aSRandall Stewart 	}
1570f8829a4aSRandall Stewart 	strmno = ntohs(ch->dp.stream_id);
1571f8829a4aSRandall Stewart 	if (strmno >= asoc->streamincnt) {
1572f8829a4aSRandall Stewart 		struct sctp_paramhdr *phdr;
1573f8829a4aSRandall Stewart 		struct mbuf *mb;
1574f8829a4aSRandall Stewart 
1575f8829a4aSRandall Stewart 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1576139bc87fSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1577f8829a4aSRandall Stewart 		if (mb != NULL) {
1578f8829a4aSRandall Stewart 			/* add some space up front so prepend will work well */
1579139bc87fSRandall Stewart 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1580f8829a4aSRandall Stewart 			phdr = mtod(mb, struct sctp_paramhdr *);
1581f8829a4aSRandall Stewart 			/*
1582f8829a4aSRandall Stewart 			 * Error causes are just param's and this one has
1583f8829a4aSRandall Stewart 			 * two back to back phdr, one with the error type
1584f8829a4aSRandall Stewart 			 * and size, the other with the streamid and a rsvd
1585f8829a4aSRandall Stewart 			 */
1586139bc87fSRandall Stewart 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1587f8829a4aSRandall Stewart 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1588f8829a4aSRandall Stewart 			phdr->param_length =
1589f8829a4aSRandall Stewart 			    htons(sizeof(struct sctp_paramhdr) * 2);
1590f8829a4aSRandall Stewart 			phdr++;
1591f8829a4aSRandall Stewart 			/* We insert the stream in the type field */
1592f8829a4aSRandall Stewart 			phdr->param_type = ch->dp.stream_id;
1593f8829a4aSRandall Stewart 			/* And set the length to 0 for the rsvd field */
1594f8829a4aSRandall Stewart 			phdr->param_length = 0;
1595f8829a4aSRandall Stewart 			sctp_queue_op_err(stcb, mb);
1596f8829a4aSRandall Stewart 		}
1597f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_badsid);
1598207304d4SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
1599830d754dSRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
160020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1601830d754dSRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
1602d06c82f1SRandall Stewart 		}
1603d06c82f1SRandall Stewart 		if (tsn == (asoc->cumulative_tsn + 1)) {
1604d06c82f1SRandall Stewart 			/* Update cum-ack */
1605d06c82f1SRandall Stewart 			asoc->cumulative_tsn = tsn;
1606d06c82f1SRandall Stewart 		}
1607f8829a4aSRandall Stewart 		return (0);
1608f8829a4aSRandall Stewart 	}
1609f8829a4aSRandall Stewart 	/*
1610f8829a4aSRandall Stewart 	 * Before we continue lets validate that we are not being fooled by
1611f8829a4aSRandall Stewart 	 * an evil attacker. We can only have 4k chunks based on our TSN
1612f8829a4aSRandall Stewart 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1613f8829a4aSRandall Stewart 	 * way our stream sequence numbers could have wrapped. We of course
1614f8829a4aSRandall Stewart 	 * only validate the FIRST fragment so the bit must be set.
1615f8829a4aSRandall Stewart 	 */
1616f8829a4aSRandall Stewart 	strmseq = ntohs(ch->dp.stream_sequence);
1617f42a358aSRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
161818e198d3SRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
161918e198d3SRandall Stewart 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
162018e198d3SRandall Stewart 		asoc->tsn_in_at = 0;
162118e198d3SRandall Stewart 		asoc->tsn_in_wrapped = 1;
162218e198d3SRandall Stewart 	}
1623f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1624f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1625f42a358aSRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1626f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1627f1f73e57SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
162818e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
162918e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
163018e198d3SRandall Stewart 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1631f42a358aSRandall Stewart 	asoc->tsn_in_at++;
1632f42a358aSRandall Stewart #endif
1633f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1634d61a0ae0SRandall Stewart 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1635f42a358aSRandall Stewart 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
163620b07a4dSMichael Tuexen 	    SCTP_SSN_GE(asoc->strmin[strmno].last_sequence_delivered, strmseq)) {
1637f8829a4aSRandall Stewart 		/* The incoming sseq is behind where we last delivered? */
1638ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1639ad81507eSRandall Stewart 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1640139bc87fSRandall Stewart 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1641f8829a4aSRandall Stewart 		    0, M_DONTWAIT, 1, MT_DATA);
1642f8829a4aSRandall Stewart 		if (oper) {
1643f8829a4aSRandall Stewart 			struct sctp_paramhdr *ph;
1644f8829a4aSRandall Stewart 			uint32_t *ippp;
1645f8829a4aSRandall Stewart 
1646139bc87fSRandall Stewart 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1647f8829a4aSRandall Stewart 			    (3 * sizeof(uint32_t));
1648f8829a4aSRandall Stewart 			ph = mtod(oper, struct sctp_paramhdr *);
1649f8829a4aSRandall Stewart 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1650139bc87fSRandall Stewart 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1651f8829a4aSRandall Stewart 			ippp = (uint32_t *) (ph + 1);
1652a5d547adSRandall Stewart 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1653f8829a4aSRandall Stewart 			ippp++;
1654f8829a4aSRandall Stewart 			*ippp = tsn;
1655f8829a4aSRandall Stewart 			ippp++;
1656f8829a4aSRandall Stewart 			*ippp = ((strmno << 16) | strmseq);
1657f8829a4aSRandall Stewart 
1658f8829a4aSRandall Stewart 		}
1659a5d547adSRandall Stewart 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1660a2b42326SMichael Tuexen 		sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1661f8829a4aSRandall Stewart 		*abort_flag = 1;
1662f8829a4aSRandall Stewart 		return (0);
1663f8829a4aSRandall Stewart 	}
1664f42a358aSRandall Stewart 	/************************************
1665f42a358aSRandall Stewart 	 * From here down we may find ch-> invalid
1666f42a358aSRandall Stewart 	 * so its a good idea NOT to use it.
1667f42a358aSRandall Stewart 	 *************************************/
1668f42a358aSRandall Stewart 
1669f8829a4aSRandall Stewart 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1670f8829a4aSRandall Stewart 	if (last_chunk == 0) {
167144b7479bSRandall Stewart 		dmbuf = SCTP_M_COPYM(*m,
1672f8829a4aSRandall Stewart 		    (offset + sizeof(struct sctp_data_chunk)),
1673f8829a4aSRandall Stewart 		    the_len, M_DONTWAIT);
1674f8829a4aSRandall Stewart #ifdef SCTP_MBUF_LOGGING
1675b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1676f8829a4aSRandall Stewart 			struct mbuf *mat;
1677f8829a4aSRandall Stewart 
167860990c0cSMichael Tuexen 			for (mat = dmbuf; mat; mat = SCTP_BUF_NEXT(mat)) {
1679139bc87fSRandall Stewart 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1680f8829a4aSRandall Stewart 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1681f8829a4aSRandall Stewart 				}
1682f8829a4aSRandall Stewart 			}
1683f8829a4aSRandall Stewart 		}
1684f8829a4aSRandall Stewart #endif
1685f8829a4aSRandall Stewart 	} else {
1686f8829a4aSRandall Stewart 		/* We can steal the last chunk */
1687139bc87fSRandall Stewart 		int l_len;
1688139bc87fSRandall Stewart 
1689f8829a4aSRandall Stewart 		dmbuf = *m;
1690f8829a4aSRandall Stewart 		/* lop off the top part */
1691f8829a4aSRandall Stewart 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1692139bc87fSRandall Stewart 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1693139bc87fSRandall Stewart 			l_len = SCTP_BUF_LEN(dmbuf);
1694139bc87fSRandall Stewart 		} else {
1695139bc87fSRandall Stewart 			/*
1696139bc87fSRandall Stewart 			 * need to count up the size hopefully does not hit
1697139bc87fSRandall Stewart 			 * this to often :-0
1698139bc87fSRandall Stewart 			 */
1699139bc87fSRandall Stewart 			struct mbuf *lat;
1700139bc87fSRandall Stewart 
1701139bc87fSRandall Stewart 			l_len = 0;
170260990c0cSMichael Tuexen 			for (lat = dmbuf; lat; lat = SCTP_BUF_NEXT(lat)) {
1703139bc87fSRandall Stewart 				l_len += SCTP_BUF_LEN(lat);
1704139bc87fSRandall Stewart 			}
1705139bc87fSRandall Stewart 		}
1706139bc87fSRandall Stewart 		if (l_len > the_len) {
1707f8829a4aSRandall Stewart 			/* Trim the end round bytes off  too */
1708139bc87fSRandall Stewart 			m_adj(dmbuf, -(l_len - the_len));
1709f8829a4aSRandall Stewart 		}
1710f8829a4aSRandall Stewart 	}
1711f8829a4aSRandall Stewart 	if (dmbuf == NULL) {
1712f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_nomem);
1713f8829a4aSRandall Stewart 		return (0);
1714f8829a4aSRandall Stewart 	}
1715f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1716f8829a4aSRandall Stewart 	    asoc->fragmented_delivery_inprogress == 0 &&
1717f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->resetHead) &&
1718f42a358aSRandall Stewart 	    ((ordered == 0) ||
171936ec9f81SMichael Tuexen 	    ((uint16_t) (asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1720f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1721f8829a4aSRandall Stewart 		/* Candidate for express delivery */
1722f8829a4aSRandall Stewart 		/*
1723f8829a4aSRandall Stewart 		 * Its not fragmented, No PD-API is up, Nothing in the
1724f8829a4aSRandall Stewart 		 * delivery queue, Its un-ordered OR ordered and the next to
1725f8829a4aSRandall Stewart 		 * deliver AND nothing else is stuck on the stream queue,
1726f8829a4aSRandall Stewart 		 * And there is room for it in the socket buffer. Lets just
1727f8829a4aSRandall Stewart 		 * stuff it up the buffer....
1728f8829a4aSRandall Stewart 		 */
1729f8829a4aSRandall Stewart 
1730f8829a4aSRandall Stewart 		/* It would be nice to avoid this copy if we could :< */
1731f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1732f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1733f42a358aSRandall Stewart 		    protocol_id,
1734f8829a4aSRandall Stewart 		    stcb->asoc.context,
1735f8829a4aSRandall Stewart 		    strmno, strmseq,
1736f42a358aSRandall Stewart 		    chunk_flags,
1737f8829a4aSRandall Stewart 		    dmbuf);
1738f8829a4aSRandall Stewart 		if (control == NULL) {
1739f8829a4aSRandall Stewart 			goto failed_express_del;
1740f8829a4aSRandall Stewart 		}
17411ea735c8SMichael Tuexen 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
174220b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
17431ea735c8SMichael Tuexen 			asoc->highest_tsn_inside_nr_map = tsn;
17441ea735c8SMichael Tuexen 		}
1745cfde3ff7SRandall Stewart 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1746cfde3ff7SRandall Stewart 		    control, &stcb->sctp_socket->so_rcv,
1747cfde3ff7SRandall Stewart 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1748830d754dSRandall Stewart 
1749f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1750f8829a4aSRandall Stewart 			/* for ordered, bump what we delivered */
1751f8829a4aSRandall Stewart 			asoc->strmin[strmno].last_sequence_delivered++;
1752f8829a4aSRandall Stewart 		}
1753f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvexpress);
1754b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
17556a91f103SRandall Stewart 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1756f8829a4aSRandall Stewart 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
175780fefe0aSRandall Stewart 		}
1758f8829a4aSRandall Stewart 		control = NULL;
1759b5c16493SMichael Tuexen 
1760f8829a4aSRandall Stewart 		goto finish_express_del;
1761f8829a4aSRandall Stewart 	}
1762f8829a4aSRandall Stewart failed_express_del:
1763f8829a4aSRandall Stewart 	/* If we reach here this is a new chunk */
1764f8829a4aSRandall Stewart 	chk = NULL;
1765f8829a4aSRandall Stewart 	control = NULL;
1766f8829a4aSRandall Stewart 	/* Express for fragmented delivery? */
1767f8829a4aSRandall Stewart 	if ((asoc->fragmented_delivery_inprogress) &&
1768f8829a4aSRandall Stewart 	    (stcb->asoc.control_pdapi) &&
1769f8829a4aSRandall Stewart 	    (asoc->str_of_pdapi == strmno) &&
1770f8829a4aSRandall Stewart 	    (asoc->ssn_of_pdapi == strmseq)
1771f8829a4aSRandall Stewart 	    ) {
1772f8829a4aSRandall Stewart 		control = stcb->asoc.control_pdapi;
1773f42a358aSRandall Stewart 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1774f8829a4aSRandall Stewart 			/* Can't be another first? */
1775f8829a4aSRandall Stewart 			goto failed_pdapi_express_del;
1776f8829a4aSRandall Stewart 		}
1777f8829a4aSRandall Stewart 		if (tsn == (control->sinfo_tsn + 1)) {
1778f8829a4aSRandall Stewart 			/* Yep, we can add it on */
1779f8829a4aSRandall Stewart 			int end = 0;
1780f8829a4aSRandall Stewart 
1781f42a358aSRandall Stewart 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1782f8829a4aSRandall Stewart 				end = 1;
1783f8829a4aSRandall Stewart 			}
1784f8829a4aSRandall Stewart 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1785f8829a4aSRandall Stewart 			    tsn,
1786f8829a4aSRandall Stewart 			    &stcb->sctp_socket->so_rcv)) {
1787ad81507eSRandall Stewart 				SCTP_PRINTF("Append fails end:%d\n", end);
1788f8829a4aSRandall Stewart 				goto failed_pdapi_express_del;
1789f8829a4aSRandall Stewart 			}
179077acdc25SRandall Stewart 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
179120b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
1792830d754dSRandall Stewart 				asoc->highest_tsn_inside_nr_map = tsn;
1793830d754dSRandall Stewart 			}
1794f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_recvexpressm);
1795f8829a4aSRandall Stewart 			control->sinfo_tsn = tsn;
1796f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = tsn;
1797f42a358aSRandall Stewart 			asoc->fragment_flags = chunk_flags;
1798f8829a4aSRandall Stewart 			asoc->tsn_of_pdapi_last_delivered = tsn;
1799f42a358aSRandall Stewart 			asoc->last_flags_delivered = chunk_flags;
1800f8829a4aSRandall Stewart 			asoc->last_strm_seq_delivered = strmseq;
1801f8829a4aSRandall Stewart 			asoc->last_strm_no_delivered = strmno;
1802f8829a4aSRandall Stewart 			if (end) {
1803f8829a4aSRandall Stewart 				/* clean up the flags and such */
1804f8829a4aSRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
1805f42a358aSRandall Stewart 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1806f8829a4aSRandall Stewart 					asoc->strmin[strmno].last_sequence_delivered++;
1807a5d547adSRandall Stewart 				}
1808f8829a4aSRandall Stewart 				stcb->asoc.control_pdapi = NULL;
1809139bc87fSRandall Stewart 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1810139bc87fSRandall Stewart 					/*
1811139bc87fSRandall Stewart 					 * There could be another message
1812139bc87fSRandall Stewart 					 * ready
1813139bc87fSRandall Stewart 					 */
1814139bc87fSRandall Stewart 					need_reasm_check = 1;
1815139bc87fSRandall Stewart 				}
1816f8829a4aSRandall Stewart 			}
1817f8829a4aSRandall Stewart 			control = NULL;
1818f8829a4aSRandall Stewart 			goto finish_express_del;
1819f8829a4aSRandall Stewart 		}
1820f8829a4aSRandall Stewart 	}
1821f8829a4aSRandall Stewart failed_pdapi_express_del:
1822f8829a4aSRandall Stewart 	control = NULL;
182377acdc25SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
182477acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
182520b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_nr_map)) {
182677acdc25SRandall Stewart 			asoc->highest_tsn_inside_nr_map = tsn;
182777acdc25SRandall Stewart 		}
182877acdc25SRandall Stewart 	} else {
182977acdc25SRandall Stewart 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
183020b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tsn, asoc->highest_tsn_inside_map)) {
183177acdc25SRandall Stewart 			asoc->highest_tsn_inside_map = tsn;
183277acdc25SRandall Stewart 		}
183377acdc25SRandall Stewart 	}
1834f42a358aSRandall Stewart 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1835f8829a4aSRandall Stewart 		sctp_alloc_a_chunk(stcb, chk);
1836f8829a4aSRandall Stewart 		if (chk == NULL) {
1837f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1838f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1839f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1840f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1841f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1842f8829a4aSRandall Stewart 			}
1843f8829a4aSRandall Stewart 			return (0);
1844f8829a4aSRandall Stewart 		}
1845f8829a4aSRandall Stewart 		chk->rec.data.TSN_seq = tsn;
1846f8829a4aSRandall Stewart 		chk->no_fr_allowed = 0;
1847f8829a4aSRandall Stewart 		chk->rec.data.stream_seq = strmseq;
1848f8829a4aSRandall Stewart 		chk->rec.data.stream_number = strmno;
1849f42a358aSRandall Stewart 		chk->rec.data.payloadtype = protocol_id;
1850f8829a4aSRandall Stewart 		chk->rec.data.context = stcb->asoc.context;
1851f8829a4aSRandall Stewart 		chk->rec.data.doing_fast_retransmit = 0;
1852f42a358aSRandall Stewart 		chk->rec.data.rcv_flags = chunk_flags;
1853f8829a4aSRandall Stewart 		chk->asoc = asoc;
1854f8829a4aSRandall Stewart 		chk->send_size = the_len;
1855f8829a4aSRandall Stewart 		chk->whoTo = net;
1856f8829a4aSRandall Stewart 		atomic_add_int(&net->ref_count, 1);
1857f8829a4aSRandall Stewart 		chk->data = dmbuf;
1858f8829a4aSRandall Stewart 	} else {
1859f8829a4aSRandall Stewart 		sctp_alloc_a_readq(stcb, control);
1860f8829a4aSRandall Stewart 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1861f42a358aSRandall Stewart 		    protocol_id,
1862f8829a4aSRandall Stewart 		    stcb->asoc.context,
1863f8829a4aSRandall Stewart 		    strmno, strmseq,
1864f42a358aSRandall Stewart 		    chunk_flags,
1865f8829a4aSRandall Stewart 		    dmbuf);
1866f8829a4aSRandall Stewart 		if (control == NULL) {
1867f8829a4aSRandall Stewart 			/* No memory so we drop the chunk */
1868f8829a4aSRandall Stewart 			SCTP_STAT_INCR(sctps_nomem);
1869f8829a4aSRandall Stewart 			if (last_chunk == 0) {
1870f8829a4aSRandall Stewart 				/* we copied it, free the copy */
1871f8829a4aSRandall Stewart 				sctp_m_freem(dmbuf);
1872f8829a4aSRandall Stewart 			}
1873f8829a4aSRandall Stewart 			return (0);
1874f8829a4aSRandall Stewart 		}
1875f8829a4aSRandall Stewart 		control->length = the_len;
1876f8829a4aSRandall Stewart 	}
1877f8829a4aSRandall Stewart 
1878f8829a4aSRandall Stewart 	/* Mark it as received */
1879f8829a4aSRandall Stewart 	/* Now queue it where it belongs */
1880f8829a4aSRandall Stewart 	if (control != NULL) {
1881f8829a4aSRandall Stewart 		/* First a sanity check */
1882f8829a4aSRandall Stewart 		if (asoc->fragmented_delivery_inprogress) {
1883f8829a4aSRandall Stewart 			/*
1884f8829a4aSRandall Stewart 			 * Ok, we have a fragmented delivery in progress if
1885f8829a4aSRandall Stewart 			 * this chunk is next to deliver OR belongs in our
1886f8829a4aSRandall Stewart 			 * view to the reassembly, the peer is evil or
1887f8829a4aSRandall Stewart 			 * broken.
1888f8829a4aSRandall Stewart 			 */
1889f8829a4aSRandall Stewart 			uint32_t estimate_tsn;
1890f8829a4aSRandall Stewart 
1891f8829a4aSRandall Stewart 			estimate_tsn = asoc->tsn_last_delivered + 1;
1892f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1893f8829a4aSRandall Stewart 			    (estimate_tsn == control->sinfo_tsn)) {
1894f8829a4aSRandall Stewart 				/* Evil/Broke peer */
1895f8829a4aSRandall Stewart 				sctp_m_freem(control->data);
1896f8829a4aSRandall Stewart 				control->data = NULL;
18975bead436SRandall Stewart 				if (control->whoFrom) {
1898f8829a4aSRandall Stewart 					sctp_free_remote_addr(control->whoFrom);
18995bead436SRandall Stewart 					control->whoFrom = NULL;
19005bead436SRandall Stewart 				}
1901f8829a4aSRandall Stewart 				sctp_free_a_readq(stcb, control);
1902139bc87fSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1903f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
1904f8829a4aSRandall Stewart 				if (oper) {
1905f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
1906f8829a4aSRandall Stewart 					uint32_t *ippp;
1907f8829a4aSRandall Stewart 
1908139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) =
1909f8829a4aSRandall Stewart 					    sizeof(struct sctp_paramhdr) +
1910f8829a4aSRandall Stewart 					    (3 * sizeof(uint32_t));
1911f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
1912f8829a4aSRandall Stewart 					ph->param_type =
1913f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1914139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1915f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
1916a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1917f8829a4aSRandall Stewart 					ippp++;
1918f8829a4aSRandall Stewart 					*ippp = tsn;
1919f8829a4aSRandall Stewart 					ippp++;
1920f8829a4aSRandall Stewart 					*ippp = ((strmno << 16) | strmseq);
1921f8829a4aSRandall Stewart 				}
1922a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1923a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1924f8829a4aSRandall Stewart 				*abort_flag = 1;
1925f8829a4aSRandall Stewart 				return (0);
1926f8829a4aSRandall Stewart 			} else {
1927f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1928f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1929f8829a4aSRandall Stewart 					control->data = NULL;
19305bead436SRandall Stewart 					if (control->whoFrom) {
1931f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19325bead436SRandall Stewart 						control->whoFrom = NULL;
19335bead436SRandall Stewart 					}
1934f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1935f8829a4aSRandall Stewart 
1936139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1937f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1938f8829a4aSRandall Stewart 					if (oper) {
1939f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1940f8829a4aSRandall Stewart 						uint32_t *ippp;
1941f8829a4aSRandall Stewart 
1942139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1943f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1944f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1945f8829a4aSRandall Stewart 						ph = mtod(oper,
1946f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1947f8829a4aSRandall Stewart 						ph->param_type =
1948f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1949f8829a4aSRandall Stewart 						ph->param_length =
1950139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1951f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1952a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1953f8829a4aSRandall Stewart 						ippp++;
1954f8829a4aSRandall Stewart 						*ippp = tsn;
1955f8829a4aSRandall Stewart 						ippp++;
1956f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
1957f8829a4aSRandall Stewart 					}
1958a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1959a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
1960f8829a4aSRandall Stewart 					*abort_flag = 1;
1961f8829a4aSRandall Stewart 					return (0);
1962f8829a4aSRandall Stewart 				}
1963f8829a4aSRandall Stewart 			}
1964f8829a4aSRandall Stewart 		} else {
1965f8829a4aSRandall Stewart 			/* No PDAPI running */
1966f8829a4aSRandall Stewart 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1967f8829a4aSRandall Stewart 				/*
1968f8829a4aSRandall Stewart 				 * Reassembly queue is NOT empty validate
1969f8829a4aSRandall Stewart 				 * that this tsn does not need to be in
1970f8829a4aSRandall Stewart 				 * reasembly queue. If it does then our peer
1971f8829a4aSRandall Stewart 				 * is broken or evil.
1972f8829a4aSRandall Stewart 				 */
1973f8829a4aSRandall Stewart 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1974f8829a4aSRandall Stewart 					sctp_m_freem(control->data);
1975f8829a4aSRandall Stewart 					control->data = NULL;
19765bead436SRandall Stewart 					if (control->whoFrom) {
1977f8829a4aSRandall Stewart 						sctp_free_remote_addr(control->whoFrom);
19785bead436SRandall Stewart 						control->whoFrom = NULL;
19795bead436SRandall Stewart 					}
1980f8829a4aSRandall Stewart 					sctp_free_a_readq(stcb, control);
1981139bc87fSRandall Stewart 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1982f8829a4aSRandall Stewart 					    0, M_DONTWAIT, 1, MT_DATA);
1983f8829a4aSRandall Stewart 					if (oper) {
1984f8829a4aSRandall Stewart 						struct sctp_paramhdr *ph;
1985f8829a4aSRandall Stewart 						uint32_t *ippp;
1986f8829a4aSRandall Stewart 
1987139bc87fSRandall Stewart 						SCTP_BUF_LEN(oper) =
1988f8829a4aSRandall Stewart 						    sizeof(struct sctp_paramhdr) +
1989f8829a4aSRandall Stewart 						    (3 * sizeof(uint32_t));
1990f8829a4aSRandall Stewart 						ph = mtod(oper,
1991f8829a4aSRandall Stewart 						    struct sctp_paramhdr *);
1992f8829a4aSRandall Stewart 						ph->param_type =
1993f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1994f8829a4aSRandall Stewart 						ph->param_length =
1995139bc87fSRandall Stewart 						    htons(SCTP_BUF_LEN(oper));
1996f8829a4aSRandall Stewart 						ippp = (uint32_t *) (ph + 1);
1997a5d547adSRandall Stewart 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
1998f8829a4aSRandall Stewart 						ippp++;
1999f8829a4aSRandall Stewart 						*ippp = tsn;
2000f8829a4aSRandall Stewart 						ippp++;
2001f8829a4aSRandall Stewart 						*ippp = ((strmno << 16) | strmseq);
2002f8829a4aSRandall Stewart 					}
2003a5d547adSRandall Stewart 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2004a2b42326SMichael Tuexen 					sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
2005f8829a4aSRandall Stewart 					*abort_flag = 1;
2006f8829a4aSRandall Stewart 					return (0);
2007f8829a4aSRandall Stewart 				}
2008f8829a4aSRandall Stewart 			}
2009f8829a4aSRandall Stewart 		}
2010f8829a4aSRandall Stewart 		/* ok, if we reach here we have passed the sanity checks */
2011f42a358aSRandall Stewart 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2012f8829a4aSRandall Stewart 			/* queue directly into socket buffer */
2013b5c16493SMichael Tuexen 			sctp_mark_non_revokable(asoc, control->sinfo_tsn);
2014f8829a4aSRandall Stewart 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2015f8829a4aSRandall Stewart 			    control,
2016cfde3ff7SRandall Stewart 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2017f8829a4aSRandall Stewart 		} else {
2018f8829a4aSRandall Stewart 			/*
2019f8829a4aSRandall Stewart 			 * Special check for when streams are resetting. We
2020f8829a4aSRandall Stewart 			 * could be more smart about this and check the
2021f8829a4aSRandall Stewart 			 * actual stream to see if it is not being reset..
2022f8829a4aSRandall Stewart 			 * that way we would not create a HOLB when amongst
2023f8829a4aSRandall Stewart 			 * streams being reset and those not being reset.
2024f8829a4aSRandall Stewart 			 *
2025f8829a4aSRandall Stewart 			 * We take complete messages that have a stream reset
2026f8829a4aSRandall Stewart 			 * intervening (aka the TSN is after where our
2027f8829a4aSRandall Stewart 			 * cum-ack needs to be) off and put them on a
2028f8829a4aSRandall Stewart 			 * pending_reply_queue. The reassembly ones we do
2029f8829a4aSRandall Stewart 			 * not have to worry about since they are all sorted
2030f8829a4aSRandall Stewart 			 * and proceessed by TSN order. It is only the
2031f8829a4aSRandall Stewart 			 * singletons I must worry about.
2032f8829a4aSRandall Stewart 			 */
2033f8829a4aSRandall Stewart 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
203420b07a4dSMichael Tuexen 			    SCTP_TSN_GT(tsn, liste->tsn)) {
2035f8829a4aSRandall Stewart 				/*
2036f8829a4aSRandall Stewart 				 * yep its past where we need to reset... go
2037f8829a4aSRandall Stewart 				 * ahead and queue it.
2038f8829a4aSRandall Stewart 				 */
2039f8829a4aSRandall Stewart 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2040f8829a4aSRandall Stewart 					/* first one on */
2041f8829a4aSRandall Stewart 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2042f8829a4aSRandall Stewart 				} else {
20434a9ef3f8SMichael Tuexen 					struct sctp_queued_to_read *ctlOn,
20444a9ef3f8SMichael Tuexen 					                   *nctlOn;
2045f8829a4aSRandall Stewart 					unsigned char inserted = 0;
2046f8829a4aSRandall Stewart 
20474a9ef3f8SMichael Tuexen 					TAILQ_FOREACH_SAFE(ctlOn, &asoc->pending_reply_queue, next, nctlOn) {
204820b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(control->sinfo_tsn, ctlOn->sinfo_tsn)) {
20494a9ef3f8SMichael Tuexen 							continue;
2050f8829a4aSRandall Stewart 						} else {
2051f8829a4aSRandall Stewart 							/* found it */
2052f8829a4aSRandall Stewart 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2053f8829a4aSRandall Stewart 							inserted = 1;
2054f8829a4aSRandall Stewart 							break;
2055f8829a4aSRandall Stewart 						}
2056f8829a4aSRandall Stewart 					}
2057f8829a4aSRandall Stewart 					if (inserted == 0) {
2058f8829a4aSRandall Stewart 						/*
2059f8829a4aSRandall Stewart 						 * must be put at end, use
2060f8829a4aSRandall Stewart 						 * prevP (all setup from
2061f8829a4aSRandall Stewart 						 * loop) to setup nextP.
2062f8829a4aSRandall Stewart 						 */
2063f8829a4aSRandall Stewart 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2064f8829a4aSRandall Stewart 					}
2065f8829a4aSRandall Stewart 				}
2066f8829a4aSRandall Stewart 			} else {
2067f8829a4aSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2068f8829a4aSRandall Stewart 				if (*abort_flag) {
2069f8829a4aSRandall Stewart 					return (0);
2070f8829a4aSRandall Stewart 				}
2071f8829a4aSRandall Stewart 			}
2072f8829a4aSRandall Stewart 		}
2073f8829a4aSRandall Stewart 	} else {
2074f8829a4aSRandall Stewart 		/* Into the re-assembly queue */
2075f8829a4aSRandall Stewart 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2076f8829a4aSRandall Stewart 		if (*abort_flag) {
2077a5d547adSRandall Stewart 			/*
2078a5d547adSRandall Stewart 			 * the assoc is now gone and chk was put onto the
2079a5d547adSRandall Stewart 			 * reasm queue, which has all been freed.
2080a5d547adSRandall Stewart 			 */
2081a5d547adSRandall Stewart 			*m = NULL;
2082f8829a4aSRandall Stewart 			return (0);
2083f8829a4aSRandall Stewart 		}
2084f8829a4aSRandall Stewart 	}
2085f8829a4aSRandall Stewart finish_express_del:
2086307b49efSMichael Tuexen 	if (tsn == (asoc->cumulative_tsn + 1)) {
2087307b49efSMichael Tuexen 		/* Update cum-ack */
2088307b49efSMichael Tuexen 		asoc->cumulative_tsn = tsn;
2089307b49efSMichael Tuexen 	}
2090f8829a4aSRandall Stewart 	if (last_chunk) {
2091f8829a4aSRandall Stewart 		*m = NULL;
2092f8829a4aSRandall Stewart 	}
2093f42a358aSRandall Stewart 	if (ordered) {
2094f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2095f8829a4aSRandall Stewart 	} else {
2096f8829a4aSRandall Stewart 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2097f8829a4aSRandall Stewart 	}
2098f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_recvdata);
2099f8829a4aSRandall Stewart 	/* Set it present please */
2100b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
21016a91f103SRandall Stewart 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
210280fefe0aSRandall Stewart 	}
2103b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2104f8829a4aSRandall Stewart 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2105f8829a4aSRandall Stewart 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
210680fefe0aSRandall Stewart 	}
210717205eccSRandall Stewart 	/* check the special flag for stream resets */
210817205eccSRandall Stewart 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
210920b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->cumulative_tsn, liste->tsn)) {
211017205eccSRandall Stewart 		/*
211117205eccSRandall Stewart 		 * we have finished working through the backlogged TSN's now
211217205eccSRandall Stewart 		 * time to reset streams. 1: call reset function. 2: free
211317205eccSRandall Stewart 		 * pending_reply space 3: distribute any chunks in
211417205eccSRandall Stewart 		 * pending_reply_queue.
211517205eccSRandall Stewart 		 */
21164a9ef3f8SMichael Tuexen 		struct sctp_queued_to_read *ctl, *nctl;
211717205eccSRandall Stewart 
211817205eccSRandall Stewart 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
211917205eccSRandall Stewart 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2120207304d4SRandall Stewart 		SCTP_FREE(liste, SCTP_M_STRESET);
21213c503c28SRandall Stewart 		/* sa_ignore FREED_MEMORY */
212217205eccSRandall Stewart 		liste = TAILQ_FIRST(&asoc->resetHead);
21234a9ef3f8SMichael Tuexen 		if (TAILQ_EMPTY(&asoc->resetHead)) {
212417205eccSRandall Stewart 			/* All can be removed */
21254a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
212617205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
212717205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
212817205eccSRandall Stewart 				if (*abort_flag) {
212917205eccSRandall Stewart 					return (0);
213017205eccSRandall Stewart 				}
213117205eccSRandall Stewart 			}
21324a9ef3f8SMichael Tuexen 		} else {
21334a9ef3f8SMichael Tuexen 			TAILQ_FOREACH_SAFE(ctl, &asoc->pending_reply_queue, next, nctl) {
213420b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(ctl->sinfo_tsn, liste->tsn)) {
21354a9ef3f8SMichael Tuexen 					break;
21364a9ef3f8SMichael Tuexen 				}
213717205eccSRandall Stewart 				/*
213817205eccSRandall Stewart 				 * if ctl->sinfo_tsn is <= liste->tsn we can
213917205eccSRandall Stewart 				 * process it which is the NOT of
214017205eccSRandall Stewart 				 * ctl->sinfo_tsn > liste->tsn
214117205eccSRandall Stewart 				 */
214217205eccSRandall Stewart 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
214317205eccSRandall Stewart 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
214417205eccSRandall Stewart 				if (*abort_flag) {
214517205eccSRandall Stewart 					return (0);
214617205eccSRandall Stewart 				}
214717205eccSRandall Stewart 			}
214817205eccSRandall Stewart 		}
214917205eccSRandall Stewart 		/*
215017205eccSRandall Stewart 		 * Now service re-assembly to pick up anything that has been
215117205eccSRandall Stewart 		 * held on reassembly queue?
215217205eccSRandall Stewart 		 */
215317205eccSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
215417205eccSRandall Stewart 		need_reasm_check = 0;
215517205eccSRandall Stewart 	}
2156139bc87fSRandall Stewart 	if (need_reasm_check) {
2157139bc87fSRandall Stewart 		/* Another one waits ? */
2158139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, asoc);
2159139bc87fSRandall Stewart 	}
2160f8829a4aSRandall Stewart 	return (1);
2161f8829a4aSRandall Stewart }
2162f8829a4aSRandall Stewart 
2163f8829a4aSRandall Stewart int8_t sctp_map_lookup_tab[256] = {
2164b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2165b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2166b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2167b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2168b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2169b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2170b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2171b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2172b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2173b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2174b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2175b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2176b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2177b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2178b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2179b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 7,
2180b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2181b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2182b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2183b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2184b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2185b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2186b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2187b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 6,
2188b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2189b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2190b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2191b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 5,
2192b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2193b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 4,
2194b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 3,
2195b5c16493SMichael Tuexen 	0, 1, 0, 2, 0, 1, 0, 8
2196f8829a4aSRandall Stewart };
2197f8829a4aSRandall Stewart 
2198f8829a4aSRandall Stewart 
2199f8829a4aSRandall Stewart void
2200b5c16493SMichael Tuexen sctp_slide_mapping_arrays(struct sctp_tcb *stcb)
2201f8829a4aSRandall Stewart {
2202f8829a4aSRandall Stewart 	/*
2203f8829a4aSRandall Stewart 	 * Now we also need to check the mapping array in a couple of ways.
2204f8829a4aSRandall Stewart 	 * 1) Did we move the cum-ack point?
220566bd30bdSRandall Stewart 	 *
220666bd30bdSRandall Stewart 	 * When you first glance at this you might think that all entries that
220766bd30bdSRandall Stewart 	 * make up the postion of the cum-ack would be in the nr-mapping
220866bd30bdSRandall Stewart 	 * array only.. i.e. things up to the cum-ack are always
220966bd30bdSRandall Stewart 	 * deliverable. Thats true with one exception, when its a fragmented
221066bd30bdSRandall Stewart 	 * message we may not deliver the data until some threshold (or all
221166bd30bdSRandall Stewart 	 * of it) is in place. So we must OR the nr_mapping_array and
221266bd30bdSRandall Stewart 	 * mapping_array to get a true picture of the cum-ack.
2213f8829a4aSRandall Stewart 	 */
2214f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2215b3f1ea41SRandall Stewart 	int at;
221666bd30bdSRandall Stewart 	uint8_t val;
2217f8829a4aSRandall Stewart 	int slide_from, slide_end, lgap, distance;
221877acdc25SRandall Stewart 	uint32_t old_cumack, old_base, old_highest, highest_tsn;
2219f8829a4aSRandall Stewart 
2220f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
2221f8829a4aSRandall Stewart 
2222f8829a4aSRandall Stewart 	old_cumack = asoc->cumulative_tsn;
2223f8829a4aSRandall Stewart 	old_base = asoc->mapping_array_base_tsn;
2224f8829a4aSRandall Stewart 	old_highest = asoc->highest_tsn_inside_map;
2225f8829a4aSRandall Stewart 	/*
2226f8829a4aSRandall Stewart 	 * We could probably improve this a small bit by calculating the
2227f8829a4aSRandall Stewart 	 * offset of the current cum-ack as the starting point.
2228f8829a4aSRandall Stewart 	 */
2229f8829a4aSRandall Stewart 	at = 0;
2230b5c16493SMichael Tuexen 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
223166bd30bdSRandall Stewart 		val = asoc->nr_mapping_array[slide_from] | asoc->mapping_array[slide_from];
223266bd30bdSRandall Stewart 		if (val == 0xff) {
2233f8829a4aSRandall Stewart 			at += 8;
2234f8829a4aSRandall Stewart 		} else {
2235f8829a4aSRandall Stewart 			/* there is a 0 bit */
223666bd30bdSRandall Stewart 			at += sctp_map_lookup_tab[val];
2237f8829a4aSRandall Stewart 			break;
2238f8829a4aSRandall Stewart 		}
2239f8829a4aSRandall Stewart 	}
2240b5c16493SMichael Tuexen 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - 1);
2241f8829a4aSRandall Stewart 
224220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_map) &&
224320b07a4dSMichael Tuexen 	    SCTP_TSN_GT(asoc->cumulative_tsn, asoc->highest_tsn_inside_nr_map)) {
2244a5d547adSRandall Stewart #ifdef INVARIANTS
2245ceaad40aSRandall Stewart 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2246ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2247f8829a4aSRandall Stewart #else
2248ceaad40aSRandall Stewart 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2249ceaad40aSRandall Stewart 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
22500e13104dSRandall Stewart 		sctp_print_mapping_array(asoc);
2251b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2252b3f1ea41SRandall Stewart 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2253b3f1ea41SRandall Stewart 		}
2254f8829a4aSRandall Stewart 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2255830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2256f8829a4aSRandall Stewart #endif
2257f8829a4aSRandall Stewart 	}
225820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
225977acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_nr_map;
226077acdc25SRandall Stewart 	} else {
226177acdc25SRandall Stewart 		highest_tsn = asoc->highest_tsn_inside_map;
226277acdc25SRandall Stewart 	}
226377acdc25SRandall Stewart 	if ((asoc->cumulative_tsn == highest_tsn) && (at >= 8)) {
2264f8829a4aSRandall Stewart 		/* The complete array was completed by a single FR */
226577acdc25SRandall Stewart 		/* highest becomes the cum-ack */
226637f144ebSMichael Tuexen 		int clr;
226737f144ebSMichael Tuexen 
226837f144ebSMichael Tuexen #ifdef INVARIANTS
226937f144ebSMichael Tuexen 		unsigned int i;
227037f144ebSMichael Tuexen 
227137f144ebSMichael Tuexen #endif
2272f8829a4aSRandall Stewart 
2273f8829a4aSRandall Stewart 		/* clear the array */
2274b5c16493SMichael Tuexen 		clr = ((at + 7) >> 3);
2275c4739e2fSRandall Stewart 		if (clr > asoc->mapping_array_size) {
2276f8829a4aSRandall Stewart 			clr = asoc->mapping_array_size;
2277f8829a4aSRandall Stewart 		}
2278f8829a4aSRandall Stewart 		memset(asoc->mapping_array, 0, clr);
2279830d754dSRandall Stewart 		memset(asoc->nr_mapping_array, 0, clr);
228037f144ebSMichael Tuexen #ifdef INVARIANTS
2281b5c16493SMichael Tuexen 		for (i = 0; i < asoc->mapping_array_size; i++) {
2282b5c16493SMichael Tuexen 			if ((asoc->mapping_array[i]) || (asoc->nr_mapping_array[i])) {
2283cd3fd531SMichael Tuexen 				SCTP_PRINTF("Error Mapping array's not clean at clear\n");
2284b5c16493SMichael Tuexen 				sctp_print_mapping_array(asoc);
2285b5c16493SMichael Tuexen 			}
2286b5c16493SMichael Tuexen 		}
228737f144ebSMichael Tuexen #endif
228877acdc25SRandall Stewart 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
228977acdc25SRandall Stewart 		asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2290f8829a4aSRandall Stewart 	} else if (at >= 8) {
2291f8829a4aSRandall Stewart 		/* we can slide the mapping array down */
2292b3f1ea41SRandall Stewart 		/* slide_from holds where we hit the first NON 0xff byte */
2293b3f1ea41SRandall Stewart 
2294f8829a4aSRandall Stewart 		/*
2295f8829a4aSRandall Stewart 		 * now calculate the ceiling of the move using our highest
2296f8829a4aSRandall Stewart 		 * TSN value
2297f8829a4aSRandall Stewart 		 */
229877acdc25SRandall Stewart 		SCTP_CALC_TSN_TO_GAP(lgap, highest_tsn, asoc->mapping_array_base_tsn);
229977acdc25SRandall Stewart 		slide_end = (lgap >> 3);
2300f8829a4aSRandall Stewart 		if (slide_end < slide_from) {
230177acdc25SRandall Stewart 			sctp_print_mapping_array(asoc);
2302d55b0b1bSRandall Stewart #ifdef INVARIANTS
2303f8829a4aSRandall Stewart 			panic("impossible slide");
2304d55b0b1bSRandall Stewart #else
2305cd3fd531SMichael Tuexen 			SCTP_PRINTF("impossible slide lgap:%x slide_end:%x slide_from:%x? at:%d\n",
230677acdc25SRandall Stewart 			    lgap, slide_end, slide_from, at);
2307d55b0b1bSRandall Stewart 			return;
2308d55b0b1bSRandall Stewart #endif
2309f8829a4aSRandall Stewart 		}
2310b3f1ea41SRandall Stewart 		if (slide_end > asoc->mapping_array_size) {
2311b3f1ea41SRandall Stewart #ifdef INVARIANTS
2312b3f1ea41SRandall Stewart 			panic("would overrun buffer");
2313b3f1ea41SRandall Stewart #else
2314cd3fd531SMichael Tuexen 			SCTP_PRINTF("Gak, would have overrun map end:%d slide_end:%d\n",
2315b3f1ea41SRandall Stewart 			    asoc->mapping_array_size, slide_end);
2316b3f1ea41SRandall Stewart 			slide_end = asoc->mapping_array_size;
2317b3f1ea41SRandall Stewart #endif
2318b3f1ea41SRandall Stewart 		}
2319f8829a4aSRandall Stewart 		distance = (slide_end - slide_from) + 1;
2320b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2321f8829a4aSRandall Stewart 			sctp_log_map(old_base, old_cumack, old_highest,
2322f8829a4aSRandall Stewart 			    SCTP_MAP_PREPARE_SLIDE);
2323f8829a4aSRandall Stewart 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2324f8829a4aSRandall Stewart 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
232580fefe0aSRandall Stewart 		}
2326f8829a4aSRandall Stewart 		if (distance + slide_from > asoc->mapping_array_size ||
2327f8829a4aSRandall Stewart 		    distance < 0) {
2328f8829a4aSRandall Stewart 			/*
2329f8829a4aSRandall Stewart 			 * Here we do NOT slide forward the array so that
2330f8829a4aSRandall Stewart 			 * hopefully when more data comes in to fill it up
2331f8829a4aSRandall Stewart 			 * we will be able to slide it forward. Really I
2332f8829a4aSRandall Stewart 			 * don't think this should happen :-0
2333f8829a4aSRandall Stewart 			 */
2334f8829a4aSRandall Stewart 
2335b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2336f8829a4aSRandall Stewart 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2337f8829a4aSRandall Stewart 				    (uint32_t) asoc->mapping_array_size,
2338f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_NONE);
233980fefe0aSRandall Stewart 			}
2340f8829a4aSRandall Stewart 		} else {
2341f8829a4aSRandall Stewart 			int ii;
2342f8829a4aSRandall Stewart 
2343f8829a4aSRandall Stewart 			for (ii = 0; ii < distance; ii++) {
234437f144ebSMichael Tuexen 				asoc->mapping_array[ii] = asoc->mapping_array[slide_from + ii];
234537f144ebSMichael Tuexen 				asoc->nr_mapping_array[ii] = asoc->nr_mapping_array[slide_from + ii];
234677acdc25SRandall Stewart 
2347f8829a4aSRandall Stewart 			}
2348aed5947cSMichael Tuexen 			for (ii = distance; ii < asoc->mapping_array_size; ii++) {
2349f8829a4aSRandall Stewart 				asoc->mapping_array[ii] = 0;
235077acdc25SRandall Stewart 				asoc->nr_mapping_array[ii] = 0;
2351f8829a4aSRandall Stewart 			}
2352ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_map + 1 == asoc->mapping_array_base_tsn) {
2353ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_map += (slide_from << 3);
2354ee94f0a2SMichael Tuexen 			}
2355ee94f0a2SMichael Tuexen 			if (asoc->highest_tsn_inside_nr_map + 1 == asoc->mapping_array_base_tsn) {
2356ee94f0a2SMichael Tuexen 				asoc->highest_tsn_inside_nr_map += (slide_from << 3);
2357ee94f0a2SMichael Tuexen 			}
2358f8829a4aSRandall Stewart 			asoc->mapping_array_base_tsn += (slide_from << 3);
2359b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2360f8829a4aSRandall Stewart 				sctp_log_map(asoc->mapping_array_base_tsn,
2361f8829a4aSRandall Stewart 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2362f8829a4aSRandall Stewart 				    SCTP_MAP_SLIDE_RESULT);
236380fefe0aSRandall Stewart 			}
2364830d754dSRandall Stewart 		}
2365830d754dSRandall Stewart 	}
2366b5c16493SMichael Tuexen }
2367b5c16493SMichael Tuexen 
2368b5c16493SMichael Tuexen void
23697215cc1bSMichael Tuexen sctp_sack_check(struct sctp_tcb *stcb, int was_a_gap)
2370b5c16493SMichael Tuexen {
2371b5c16493SMichael Tuexen 	struct sctp_association *asoc;
2372b5c16493SMichael Tuexen 	uint32_t highest_tsn;
2373b5c16493SMichael Tuexen 
2374b5c16493SMichael Tuexen 	asoc = &stcb->asoc;
237520b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
2376b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
2377b5c16493SMichael Tuexen 	} else {
2378b5c16493SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2379b5c16493SMichael Tuexen 	}
2380b5c16493SMichael Tuexen 
2381830d754dSRandall Stewart 	/*
2382f8829a4aSRandall Stewart 	 * Now we need to see if we need to queue a sack or just start the
2383f8829a4aSRandall Stewart 	 * timer (if allowed).
2384f8829a4aSRandall Stewart 	 */
2385f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2386f8829a4aSRandall Stewart 		/*
2387b5c16493SMichael Tuexen 		 * Ok special case, in SHUTDOWN-SENT case. here we maker
2388b5c16493SMichael Tuexen 		 * sure SACK timer is off and instead send a SHUTDOWN and a
2389b5c16493SMichael Tuexen 		 * SACK
2390f8829a4aSRandall Stewart 		 */
2391139bc87fSRandall Stewart 		if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2392f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2393a5d547adSRandall Stewart 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2394f8829a4aSRandall Stewart 		}
2395ca85e948SMichael Tuexen 		sctp_send_shutdown(stcb,
2396ca85e948SMichael Tuexen 		    ((stcb->asoc.alternate) ? stcb->asoc.alternate : stcb->asoc.primary_destination));
2397689e6a5fSMichael Tuexen 		sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2398f8829a4aSRandall Stewart 	} else {
2399f8829a4aSRandall Stewart 		int is_a_gap;
2400f8829a4aSRandall Stewart 
2401f8829a4aSRandall Stewart 		/* is there a gap now ? */
240220b07a4dSMichael Tuexen 		is_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2403f8829a4aSRandall Stewart 
2404f8829a4aSRandall Stewart 		/*
2405b5c16493SMichael Tuexen 		 * CMT DAC algorithm: increase number of packets received
2406b5c16493SMichael Tuexen 		 * since last ack
2407f8829a4aSRandall Stewart 		 */
2408f8829a4aSRandall Stewart 		stcb->asoc.cmt_dac_pkts_rcvd++;
2409f8829a4aSRandall Stewart 
241042551e99SRandall Stewart 		if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
241142551e99SRandall Stewart 							 * SACK */
2412f8829a4aSRandall Stewart 		    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2413f8829a4aSRandall Stewart 							 * longer is one */
2414f8829a4aSRandall Stewart 		    (stcb->asoc.numduptsns) ||	/* we have dup's */
2415f8829a4aSRandall Stewart 		    (is_a_gap) ||	/* is still a gap */
241642551e99SRandall Stewart 		    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
241742551e99SRandall Stewart 		    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2418f8829a4aSRandall Stewart 		    ) {
2419f8829a4aSRandall Stewart 
24207c99d56fSMichael Tuexen 			if ((stcb->asoc.sctp_cmt_on_off > 0) &&
2421b3f1ea41SRandall Stewart 			    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
242242551e99SRandall Stewart 			    (stcb->asoc.send_sack == 0) &&
2423f8829a4aSRandall Stewart 			    (stcb->asoc.numduptsns == 0) &&
2424f8829a4aSRandall Stewart 			    (stcb->asoc.delayed_ack) &&
2425139bc87fSRandall Stewart 			    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2426f8829a4aSRandall Stewart 
2427f8829a4aSRandall Stewart 				/*
2428b5c16493SMichael Tuexen 				 * CMT DAC algorithm: With CMT, delay acks
2429b5c16493SMichael Tuexen 				 * even in the face of
2430f8829a4aSRandall Stewart 				 *
2431b5c16493SMichael Tuexen 				 * reordering. Therefore, if acks that do not
2432b5c16493SMichael Tuexen 				 * have to be sent because of the above
2433b5c16493SMichael Tuexen 				 * reasons, will be delayed. That is, acks
2434b5c16493SMichael Tuexen 				 * that would have been sent due to gap
2435b5c16493SMichael Tuexen 				 * reports will be delayed with DAC. Start
2436f8829a4aSRandall Stewart 				 * the delayed ack timer.
2437f8829a4aSRandall Stewart 				 */
2438f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2439f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2440f8829a4aSRandall Stewart 			} else {
2441f8829a4aSRandall Stewart 				/*
2442b5c16493SMichael Tuexen 				 * Ok we must build a SACK since the timer
2443b5c16493SMichael Tuexen 				 * is pending, we got our first packet OR
2444b5c16493SMichael Tuexen 				 * there are gaps or duplicates.
2445f8829a4aSRandall Stewart 				 */
2446ad81507eSRandall Stewart 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2447689e6a5fSMichael Tuexen 				sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
2448f8829a4aSRandall Stewart 			}
2449f8829a4aSRandall Stewart 		} else {
245042551e99SRandall Stewart 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2451f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2452f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, NULL);
2453f8829a4aSRandall Stewart 			}
2454f8829a4aSRandall Stewart 		}
2455f8829a4aSRandall Stewart 	}
2456f8829a4aSRandall Stewart }
2457f8829a4aSRandall Stewart 
2458f8829a4aSRandall Stewart void
2459f8829a4aSRandall Stewart sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2460f8829a4aSRandall Stewart {
2461f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *chk;
2462810ec536SMichael Tuexen 	uint32_t tsize, pd_point;
2463f8829a4aSRandall Stewart 	uint16_t nxt_todel;
2464f8829a4aSRandall Stewart 
2465f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2466f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
2467f8829a4aSRandall Stewart 	}
2468f8829a4aSRandall Stewart 	/* Can we proceed further, i.e. the PD-API is complete */
2469f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
2470f8829a4aSRandall Stewart 		/* no */
2471f8829a4aSRandall Stewart 		return;
2472f8829a4aSRandall Stewart 	}
2473f8829a4aSRandall Stewart 	/*
2474f8829a4aSRandall Stewart 	 * Now is there some other chunk I can deliver from the reassembly
2475f8829a4aSRandall Stewart 	 * queue.
2476f8829a4aSRandall Stewart 	 */
2477139bc87fSRandall Stewart doit_again:
2478f8829a4aSRandall Stewart 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2479f8829a4aSRandall Stewart 	if (chk == NULL) {
2480f8829a4aSRandall Stewart 		asoc->size_on_reasm_queue = 0;
2481f8829a4aSRandall Stewart 		asoc->cnt_on_reasm_queue = 0;
2482f8829a4aSRandall Stewart 		return;
2483f8829a4aSRandall Stewart 	}
2484f8829a4aSRandall Stewart 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2485f8829a4aSRandall Stewart 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2486f8829a4aSRandall Stewart 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2487f8829a4aSRandall Stewart 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2488f8829a4aSRandall Stewart 		/*
2489f8829a4aSRandall Stewart 		 * Yep the first one is here. We setup to start reception,
2490f8829a4aSRandall Stewart 		 * by backing down the TSN just in case we can't deliver.
2491f8829a4aSRandall Stewart 		 */
2492f8829a4aSRandall Stewart 
2493f8829a4aSRandall Stewart 		/*
2494f8829a4aSRandall Stewart 		 * Before we start though either all of the message should
249524ae5c4aSMichael Tuexen 		 * be here or the socket buffer max or nothing on the
2496f8829a4aSRandall Stewart 		 * delivery queue and something can be delivered.
2497f8829a4aSRandall Stewart 		 */
2498810ec536SMichael Tuexen 		if (stcb->sctp_socket) {
249924ae5c4aSMichael Tuexen 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
2500810ec536SMichael Tuexen 			    stcb->sctp_ep->partial_delivery_point);
2501810ec536SMichael Tuexen 		} else {
2502810ec536SMichael Tuexen 			pd_point = stcb->sctp_ep->partial_delivery_point;
2503810ec536SMichael Tuexen 		}
2504810ec536SMichael Tuexen 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2505f8829a4aSRandall Stewart 			asoc->fragmented_delivery_inprogress = 1;
2506f8829a4aSRandall Stewart 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2507f8829a4aSRandall Stewart 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2508f8829a4aSRandall Stewart 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2509f8829a4aSRandall Stewart 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2510f8829a4aSRandall Stewart 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2511f8829a4aSRandall Stewart 			sctp_service_reassembly(stcb, asoc);
2512139bc87fSRandall Stewart 			if (asoc->fragmented_delivery_inprogress == 0) {
2513139bc87fSRandall Stewart 				goto doit_again;
2514139bc87fSRandall Stewart 			}
2515f8829a4aSRandall Stewart 		}
2516f8829a4aSRandall Stewart 	}
2517f8829a4aSRandall Stewart }
2518f8829a4aSRandall Stewart 
2519f8829a4aSRandall Stewart int
2520f8829a4aSRandall Stewart sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2521f8829a4aSRandall Stewart     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2522f8829a4aSRandall Stewart     struct sctp_nets *net, uint32_t * high_tsn)
2523f8829a4aSRandall Stewart {
2524f8829a4aSRandall Stewart 	struct sctp_data_chunk *ch, chunk_buf;
2525f8829a4aSRandall Stewart 	struct sctp_association *asoc;
2526f8829a4aSRandall Stewart 	int num_chunks = 0;	/* number of control chunks processed */
2527f8829a4aSRandall Stewart 	int stop_proc = 0;
2528f8829a4aSRandall Stewart 	int chk_length, break_flag, last_chunk;
25298f777478SMichael Tuexen 	int abort_flag = 0, was_a_gap;
2530f8829a4aSRandall Stewart 	struct mbuf *m;
25318f777478SMichael Tuexen 	uint32_t highest_tsn;
2532f8829a4aSRandall Stewart 
2533f8829a4aSRandall Stewart 	/* set the rwnd */
2534f8829a4aSRandall Stewart 	sctp_set_rwnd(stcb, &stcb->asoc);
2535f8829a4aSRandall Stewart 
2536f8829a4aSRandall Stewart 	m = *mm;
2537f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
2538f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
253920b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->highest_tsn_inside_map)) {
25408f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_nr_map;
25418f777478SMichael Tuexen 	} else {
25428f777478SMichael Tuexen 		highest_tsn = asoc->highest_tsn_inside_map;
2543f8829a4aSRandall Stewart 	}
254420b07a4dSMichael Tuexen 	was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
2545f8829a4aSRandall Stewart 	/*
2546f8829a4aSRandall Stewart 	 * setup where we got the last DATA packet from for any SACK that
2547f8829a4aSRandall Stewart 	 * may need to go out. Don't bump the net. This is done ONLY when a
2548f8829a4aSRandall Stewart 	 * chunk is assigned.
2549f8829a4aSRandall Stewart 	 */
2550f8829a4aSRandall Stewart 	asoc->last_data_chunk_from = net;
2551f8829a4aSRandall Stewart 
2552d06c82f1SRandall Stewart 	/*-
2553f8829a4aSRandall Stewart 	 * Now before we proceed we must figure out if this is a wasted
2554f8829a4aSRandall Stewart 	 * cluster... i.e. it is a small packet sent in and yet the driver
2555f8829a4aSRandall Stewart 	 * underneath allocated a full cluster for it. If so we must copy it
2556f8829a4aSRandall Stewart 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2557d06c82f1SRandall Stewart 	 * with cluster starvation. Note for __Panda__ we don't do this
2558d06c82f1SRandall Stewart 	 * since it has clusters all the way down to 64 bytes.
2559f8829a4aSRandall Stewart 	 */
256044b7479bSRandall Stewart 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2561f8829a4aSRandall Stewart 		/* we only handle mbufs that are singletons.. not chains */
2562139bc87fSRandall Stewart 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2563f8829a4aSRandall Stewart 		if (m) {
2564f8829a4aSRandall Stewart 			/* ok lets see if we can copy the data up */
2565f8829a4aSRandall Stewart 			caddr_t *from, *to;
2566f8829a4aSRandall Stewart 
2567f8829a4aSRandall Stewart 			/* get the pointers and copy */
2568f8829a4aSRandall Stewart 			to = mtod(m, caddr_t *);
2569f8829a4aSRandall Stewart 			from = mtod((*mm), caddr_t *);
2570139bc87fSRandall Stewart 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2571f8829a4aSRandall Stewart 			/* copy the length and free up the old */
2572139bc87fSRandall Stewart 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2573f8829a4aSRandall Stewart 			sctp_m_freem(*mm);
2574f8829a4aSRandall Stewart 			/* sucess, back copy */
2575f8829a4aSRandall Stewart 			*mm = m;
2576f8829a4aSRandall Stewart 		} else {
2577f8829a4aSRandall Stewart 			/* We are in trouble in the mbuf world .. yikes */
2578f8829a4aSRandall Stewart 			m = *mm;
2579f8829a4aSRandall Stewart 		}
2580f8829a4aSRandall Stewart 	}
2581f8829a4aSRandall Stewart 	/* get pointer to the first chunk header */
2582f8829a4aSRandall Stewart 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2583f8829a4aSRandall Stewart 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2584f8829a4aSRandall Stewart 	if (ch == NULL) {
2585f8829a4aSRandall Stewart 		return (1);
2586f8829a4aSRandall Stewart 	}
2587f8829a4aSRandall Stewart 	/*
2588f8829a4aSRandall Stewart 	 * process all DATA chunks...
2589f8829a4aSRandall Stewart 	 */
2590f8829a4aSRandall Stewart 	*high_tsn = asoc->cumulative_tsn;
2591f8829a4aSRandall Stewart 	break_flag = 0;
259242551e99SRandall Stewart 	asoc->data_pkts_seen++;
2593f8829a4aSRandall Stewart 	while (stop_proc == 0) {
2594f8829a4aSRandall Stewart 		/* validate chunk length */
2595f8829a4aSRandall Stewart 		chk_length = ntohs(ch->ch.chunk_length);
2596f8829a4aSRandall Stewart 		if (length - *offset < chk_length) {
2597f8829a4aSRandall Stewart 			/* all done, mutulated chunk */
2598f8829a4aSRandall Stewart 			stop_proc = 1;
259960990c0cSMichael Tuexen 			continue;
2600f8829a4aSRandall Stewart 		}
2601f8829a4aSRandall Stewart 		if (ch->ch.chunk_type == SCTP_DATA) {
2602f8829a4aSRandall Stewart 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2603f8829a4aSRandall Stewart 				/*
2604f8829a4aSRandall Stewart 				 * Need to send an abort since we had a
2605f8829a4aSRandall Stewart 				 * invalid data chunk.
2606f8829a4aSRandall Stewart 				 */
2607f8829a4aSRandall Stewart 				struct mbuf *op_err;
2608f8829a4aSRandall Stewart 
2609139bc87fSRandall Stewart 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2610f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
2611f8829a4aSRandall Stewart 
2612f8829a4aSRandall Stewart 				if (op_err) {
2613f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
2614f8829a4aSRandall Stewart 					uint32_t *ippp;
2615f8829a4aSRandall Stewart 
2616139bc87fSRandall Stewart 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2617f8829a4aSRandall Stewart 					    (2 * sizeof(uint32_t));
2618f8829a4aSRandall Stewart 					ph = mtod(op_err, struct sctp_paramhdr *);
2619f8829a4aSRandall Stewart 					ph->param_type =
2620f8829a4aSRandall Stewart 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2621139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2622f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
2623a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2624f8829a4aSRandall Stewart 					ippp++;
2625f8829a4aSRandall Stewart 					*ippp = asoc->cumulative_tsn;
2626f8829a4aSRandall Stewart 
2627f8829a4aSRandall Stewart 				}
2628a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2629f8829a4aSRandall Stewart 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2630c54a18d2SRandall Stewart 				    op_err, 0, net->port);
2631f8829a4aSRandall Stewart 				return (2);
2632f8829a4aSRandall Stewart 			}
2633f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
2634f8829a4aSRandall Stewart 			sctp_audit_log(0xB1, 0);
2635f8829a4aSRandall Stewart #endif
2636f8829a4aSRandall Stewart 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2637f8829a4aSRandall Stewart 				last_chunk = 1;
2638f8829a4aSRandall Stewart 			} else {
2639f8829a4aSRandall Stewart 				last_chunk = 0;
2640f8829a4aSRandall Stewart 			}
2641f8829a4aSRandall Stewart 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2642f8829a4aSRandall Stewart 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2643f8829a4aSRandall Stewart 			    last_chunk)) {
2644f8829a4aSRandall Stewart 				num_chunks++;
2645f8829a4aSRandall Stewart 			}
2646f8829a4aSRandall Stewart 			if (abort_flag)
2647f8829a4aSRandall Stewart 				return (2);
2648f8829a4aSRandall Stewart 
2649f8829a4aSRandall Stewart 			if (break_flag) {
2650f8829a4aSRandall Stewart 				/*
2651f8829a4aSRandall Stewart 				 * Set because of out of rwnd space and no
2652f8829a4aSRandall Stewart 				 * drop rep space left.
2653f8829a4aSRandall Stewart 				 */
2654f8829a4aSRandall Stewart 				stop_proc = 1;
265560990c0cSMichael Tuexen 				continue;
2656f8829a4aSRandall Stewart 			}
2657f8829a4aSRandall Stewart 		} else {
2658f8829a4aSRandall Stewart 			/* not a data chunk in the data region */
2659f8829a4aSRandall Stewart 			switch (ch->ch.chunk_type) {
2660f8829a4aSRandall Stewart 			case SCTP_INITIATION:
2661f8829a4aSRandall Stewart 			case SCTP_INITIATION_ACK:
2662f8829a4aSRandall Stewart 			case SCTP_SELECTIVE_ACK:
266360990c0cSMichael Tuexen 			case SCTP_NR_SELECTIVE_ACK:
2664f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_REQUEST:
2665f8829a4aSRandall Stewart 			case SCTP_HEARTBEAT_ACK:
2666f8829a4aSRandall Stewart 			case SCTP_ABORT_ASSOCIATION:
2667f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN:
2668f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_ACK:
2669f8829a4aSRandall Stewart 			case SCTP_OPERATION_ERROR:
2670f8829a4aSRandall Stewart 			case SCTP_COOKIE_ECHO:
2671f8829a4aSRandall Stewart 			case SCTP_COOKIE_ACK:
2672f8829a4aSRandall Stewart 			case SCTP_ECN_ECHO:
2673f8829a4aSRandall Stewart 			case SCTP_ECN_CWR:
2674f8829a4aSRandall Stewart 			case SCTP_SHUTDOWN_COMPLETE:
2675f8829a4aSRandall Stewart 			case SCTP_AUTHENTICATION:
2676f8829a4aSRandall Stewart 			case SCTP_ASCONF_ACK:
2677f8829a4aSRandall Stewart 			case SCTP_PACKET_DROPPED:
2678f8829a4aSRandall Stewart 			case SCTP_STREAM_RESET:
2679f8829a4aSRandall Stewart 			case SCTP_FORWARD_CUM_TSN:
2680f8829a4aSRandall Stewart 			case SCTP_ASCONF:
2681f8829a4aSRandall Stewart 				/*
2682f8829a4aSRandall Stewart 				 * Now, what do we do with KNOWN chunks that
2683f8829a4aSRandall Stewart 				 * are NOT in the right place?
2684f8829a4aSRandall Stewart 				 *
2685f8829a4aSRandall Stewart 				 * For now, I do nothing but ignore them. We
2686f8829a4aSRandall Stewart 				 * may later want to add sysctl stuff to
2687f8829a4aSRandall Stewart 				 * switch out and do either an ABORT() or
2688f8829a4aSRandall Stewart 				 * possibly process them.
2689f8829a4aSRandall Stewart 				 */
2690b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
2691f8829a4aSRandall Stewart 					struct mbuf *op_err;
2692f8829a4aSRandall Stewart 
2693f8829a4aSRandall Stewart 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2694c54a18d2SRandall Stewart 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
2695f8829a4aSRandall Stewart 					return (2);
2696f8829a4aSRandall Stewart 				}
2697f8829a4aSRandall Stewart 				break;
2698f8829a4aSRandall Stewart 			default:
2699f8829a4aSRandall Stewart 				/* unknown chunk type, use bit rules */
2700f8829a4aSRandall Stewart 				if (ch->ch.chunk_type & 0x40) {
2701f8829a4aSRandall Stewart 					/* Add a error report to the queue */
2702d61a0ae0SRandall Stewart 					struct mbuf *merr;
2703f8829a4aSRandall Stewart 					struct sctp_paramhdr *phd;
2704f8829a4aSRandall Stewart 
2705d61a0ae0SRandall Stewart 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2706d61a0ae0SRandall Stewart 					if (merr) {
2707d61a0ae0SRandall Stewart 						phd = mtod(merr, struct sctp_paramhdr *);
2708f8829a4aSRandall Stewart 						/*
2709f8829a4aSRandall Stewart 						 * We cheat and use param
2710f8829a4aSRandall Stewart 						 * type since we did not
2711f8829a4aSRandall Stewart 						 * bother to define a error
2712f8829a4aSRandall Stewart 						 * cause struct. They are
2713f8829a4aSRandall Stewart 						 * the same basic format
2714f8829a4aSRandall Stewart 						 * with different names.
2715f8829a4aSRandall Stewart 						 */
2716f8829a4aSRandall Stewart 						phd->param_type =
2717f8829a4aSRandall Stewart 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2718f8829a4aSRandall Stewart 						phd->param_length =
2719f8829a4aSRandall Stewart 						    htons(chk_length + sizeof(*phd));
2720d61a0ae0SRandall Stewart 						SCTP_BUF_LEN(merr) = sizeof(*phd);
2721921569e2SMichael Tuexen 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset, chk_length, M_DONTWAIT);
2722d61a0ae0SRandall Stewart 						if (SCTP_BUF_NEXT(merr)) {
2723921569e2SMichael Tuexen 							if (sctp_pad_lastmbuf(SCTP_BUF_NEXT(merr), SCTP_SIZE32(chk_length) - chk_length, NULL)) {
2724921569e2SMichael Tuexen 								sctp_m_freem(merr);
2725921569e2SMichael Tuexen 							} else {
2726d61a0ae0SRandall Stewart 								sctp_queue_op_err(stcb, merr);
2727921569e2SMichael Tuexen 							}
2728f8829a4aSRandall Stewart 						} else {
2729d61a0ae0SRandall Stewart 							sctp_m_freem(merr);
2730f8829a4aSRandall Stewart 						}
2731f8829a4aSRandall Stewart 					}
2732f8829a4aSRandall Stewart 				}
2733f8829a4aSRandall Stewart 				if ((ch->ch.chunk_type & 0x80) == 0) {
2734f8829a4aSRandall Stewart 					/* discard the rest of this packet */
2735f8829a4aSRandall Stewart 					stop_proc = 1;
2736f8829a4aSRandall Stewart 				}	/* else skip this bad chunk and
2737f8829a4aSRandall Stewart 					 * continue... */
2738f8829a4aSRandall Stewart 				break;
273960990c0cSMichael Tuexen 			}	/* switch of chunk type */
2740f8829a4aSRandall Stewart 		}
2741f8829a4aSRandall Stewart 		*offset += SCTP_SIZE32(chk_length);
2742f8829a4aSRandall Stewart 		if ((*offset >= length) || stop_proc) {
2743f8829a4aSRandall Stewart 			/* no more data left in the mbuf chain */
2744f8829a4aSRandall Stewart 			stop_proc = 1;
2745f8829a4aSRandall Stewart 			continue;
2746f8829a4aSRandall Stewart 		}
2747f8829a4aSRandall Stewart 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2748f8829a4aSRandall Stewart 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2749f8829a4aSRandall Stewart 		if (ch == NULL) {
2750f8829a4aSRandall Stewart 			*offset = length;
2751f8829a4aSRandall Stewart 			stop_proc = 1;
275260990c0cSMichael Tuexen 			continue;
2753f8829a4aSRandall Stewart 		}
275460990c0cSMichael Tuexen 	}
2755f8829a4aSRandall Stewart 	if (break_flag) {
2756f8829a4aSRandall Stewart 		/*
2757f8829a4aSRandall Stewart 		 * we need to report rwnd overrun drops.
2758f8829a4aSRandall Stewart 		 */
2759f8829a4aSRandall Stewart 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2760f8829a4aSRandall Stewart 	}
2761f8829a4aSRandall Stewart 	if (num_chunks) {
2762f8829a4aSRandall Stewart 		/*
2763ceaad40aSRandall Stewart 		 * Did we get data, if so update the time for auto-close and
2764f8829a4aSRandall Stewart 		 * give peer credit for being alive.
2765f8829a4aSRandall Stewart 		 */
2766f8829a4aSRandall Stewart 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2767b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2768c4739e2fSRandall Stewart 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2769c4739e2fSRandall Stewart 			    stcb->asoc.overall_error_count,
2770c4739e2fSRandall Stewart 			    0,
2771c4739e2fSRandall Stewart 			    SCTP_FROM_SCTP_INDATA,
2772c4739e2fSRandall Stewart 			    __LINE__);
2773c4739e2fSRandall Stewart 		}
2774f8829a4aSRandall Stewart 		stcb->asoc.overall_error_count = 0;
27756e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2776f8829a4aSRandall Stewart 	}
2777f8829a4aSRandall Stewart 	/* now service all of the reassm queue if needed */
2778f8829a4aSRandall Stewart 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2779f8829a4aSRandall Stewart 		sctp_service_queues(stcb, asoc);
2780f8829a4aSRandall Stewart 
2781f8829a4aSRandall Stewart 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
278242551e99SRandall Stewart 		/* Assure that we ack right away */
278342551e99SRandall Stewart 		stcb->asoc.send_sack = 1;
2784f8829a4aSRandall Stewart 	}
2785f8829a4aSRandall Stewart 	/* Start a sack timer or QUEUE a SACK for sending */
27867215cc1bSMichael Tuexen 	sctp_sack_check(stcb, was_a_gap);
2787f8829a4aSRandall Stewart 	return (0);
2788f8829a4aSRandall Stewart }
2789f8829a4aSRandall Stewart 
27900fa753b3SRandall Stewart static int
27910fa753b3SRandall Stewart sctp_process_segment_range(struct sctp_tcb *stcb, struct sctp_tmit_chunk **p_tp1, uint32_t last_tsn,
27920fa753b3SRandall Stewart     uint16_t frag_strt, uint16_t frag_end, int nr_sacking,
27930fa753b3SRandall Stewart     int *num_frs,
27940fa753b3SRandall Stewart     uint32_t * biggest_newly_acked_tsn,
27950fa753b3SRandall Stewart     uint32_t * this_sack_lowest_newack,
27967215cc1bSMichael Tuexen     int *rto_ok)
27970fa753b3SRandall Stewart {
27980fa753b3SRandall Stewart 	struct sctp_tmit_chunk *tp1;
27990fa753b3SRandall Stewart 	unsigned int theTSN;
2800b5c16493SMichael Tuexen 	int j, wake_him = 0, circled = 0;
28010fa753b3SRandall Stewart 
28020fa753b3SRandall Stewart 	/* Recover the tp1 we last saw */
28030fa753b3SRandall Stewart 	tp1 = *p_tp1;
28040fa753b3SRandall Stewart 	if (tp1 == NULL) {
28050fa753b3SRandall Stewart 		tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
28060fa753b3SRandall Stewart 	}
28070fa753b3SRandall Stewart 	for (j = frag_strt; j <= frag_end; j++) {
28080fa753b3SRandall Stewart 		theTSN = j + last_tsn;
28090fa753b3SRandall Stewart 		while (tp1) {
28100fa753b3SRandall Stewart 			if (tp1->rec.data.doing_fast_retransmit)
28110fa753b3SRandall Stewart 				(*num_frs) += 1;
28120fa753b3SRandall Stewart 
28130fa753b3SRandall Stewart 			/*-
28140fa753b3SRandall Stewart 			 * CMT: CUCv2 algorithm. For each TSN being
28150fa753b3SRandall Stewart 			 * processed from the sent queue, track the
28160fa753b3SRandall Stewart 			 * next expected pseudo-cumack, or
28170fa753b3SRandall Stewart 			 * rtx_pseudo_cumack, if required. Separate
28180fa753b3SRandall Stewart 			 * cumack trackers for first transmissions,
28190fa753b3SRandall Stewart 			 * and retransmissions.
28200fa753b3SRandall Stewart 			 */
28210fa753b3SRandall Stewart 			if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28220fa753b3SRandall Stewart 			    (tp1->snd_count == 1)) {
28230fa753b3SRandall Stewart 				tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
28240fa753b3SRandall Stewart 				tp1->whoTo->find_pseudo_cumack = 0;
28250fa753b3SRandall Stewart 			}
28260fa753b3SRandall Stewart 			if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
28270fa753b3SRandall Stewart 			    (tp1->snd_count > 1)) {
28280fa753b3SRandall Stewart 				tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
28290fa753b3SRandall Stewart 				tp1->whoTo->find_rtx_pseudo_cumack = 0;
28300fa753b3SRandall Stewart 			}
28310fa753b3SRandall Stewart 			if (tp1->rec.data.TSN_seq == theTSN) {
28320fa753b3SRandall Stewart 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
28330fa753b3SRandall Stewart 					/*-
28340fa753b3SRandall Stewart 					 * must be held until
28350fa753b3SRandall Stewart 					 * cum-ack passes
28360fa753b3SRandall Stewart 					 */
28370fa753b3SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
28380fa753b3SRandall Stewart 						/*-
28390fa753b3SRandall Stewart 						 * If it is less than RESEND, it is
28400fa753b3SRandall Stewart 						 * now no-longer in flight.
28410fa753b3SRandall Stewart 						 * Higher values may already be set
28420fa753b3SRandall Stewart 						 * via previous Gap Ack Blocks...
28430fa753b3SRandall Stewart 						 * i.e. ACKED or RESEND.
28440fa753b3SRandall Stewart 						 */
284520b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
284620b07a4dSMichael Tuexen 						    *biggest_newly_acked_tsn)) {
28470fa753b3SRandall Stewart 							*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
28480fa753b3SRandall Stewart 						}
28490fa753b3SRandall Stewart 						/*-
28500fa753b3SRandall Stewart 						 * CMT: SFR algo (and HTNA) - set
28510fa753b3SRandall Stewart 						 * saw_newack to 1 for dest being
28520fa753b3SRandall Stewart 						 * newly acked. update
28530fa753b3SRandall Stewart 						 * this_sack_highest_newack if
28540fa753b3SRandall Stewart 						 * appropriate.
28550fa753b3SRandall Stewart 						 */
28560fa753b3SRandall Stewart 						if (tp1->rec.data.chunk_was_revoked == 0)
28570fa753b3SRandall Stewart 							tp1->whoTo->saw_newack = 1;
28580fa753b3SRandall Stewart 
285920b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
286020b07a4dSMichael Tuexen 						    tp1->whoTo->this_sack_highest_newack)) {
28610fa753b3SRandall Stewart 							tp1->whoTo->this_sack_highest_newack =
28620fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
28630fa753b3SRandall Stewart 						}
28640fa753b3SRandall Stewart 						/*-
28650fa753b3SRandall Stewart 						 * CMT DAC algo: also update
28660fa753b3SRandall Stewart 						 * this_sack_lowest_newack
28670fa753b3SRandall Stewart 						 */
28680fa753b3SRandall Stewart 						if (*this_sack_lowest_newack == 0) {
28690fa753b3SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
28700fa753b3SRandall Stewart 								sctp_log_sack(*this_sack_lowest_newack,
28710fa753b3SRandall Stewart 								    last_tsn,
28720fa753b3SRandall Stewart 								    tp1->rec.data.TSN_seq,
28730fa753b3SRandall Stewart 								    0,
28740fa753b3SRandall Stewart 								    0,
28750fa753b3SRandall Stewart 								    SCTP_LOG_TSN_ACKED);
28760fa753b3SRandall Stewart 							}
28770fa753b3SRandall Stewart 							*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
28780fa753b3SRandall Stewart 						}
28790fa753b3SRandall Stewart 						/*-
28800fa753b3SRandall Stewart 						 * CMT: CUCv2 algorithm. If (rtx-)pseudo-cumack for corresp
28810fa753b3SRandall Stewart 						 * dest is being acked, then we have a new (rtx-)pseudo-cumack. Set
28820fa753b3SRandall Stewart 						 * new_(rtx_)pseudo_cumack to TRUE so that the cwnd for this dest can be
28830fa753b3SRandall Stewart 						 * updated. Also trigger search for the next expected (rtx-)pseudo-cumack.
28840fa753b3SRandall Stewart 						 * Separate pseudo_cumack trackers for first transmissions and
28850fa753b3SRandall Stewart 						 * retransmissions.
28860fa753b3SRandall Stewart 						 */
28870fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
28880fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
28890fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
28900fa753b3SRandall Stewart 							}
28910fa753b3SRandall Stewart 							tp1->whoTo->find_pseudo_cumack = 1;
28920fa753b3SRandall Stewart 						}
28930fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
28940fa753b3SRandall Stewart 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
28950fa753b3SRandall Stewart 						}
28960fa753b3SRandall Stewart 						if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
28970fa753b3SRandall Stewart 							if (tp1->rec.data.chunk_was_revoked == 0) {
28980fa753b3SRandall Stewart 								tp1->whoTo->new_pseudo_cumack = 1;
28990fa753b3SRandall Stewart 							}
29000fa753b3SRandall Stewart 							tp1->whoTo->find_rtx_pseudo_cumack = 1;
29010fa753b3SRandall Stewart 						}
29020fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
29030fa753b3SRandall Stewart 							sctp_log_sack(*biggest_newly_acked_tsn,
29040fa753b3SRandall Stewart 							    last_tsn,
29050fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq,
29060fa753b3SRandall Stewart 							    frag_strt,
29070fa753b3SRandall Stewart 							    frag_end,
29080fa753b3SRandall Stewart 							    SCTP_LOG_TSN_ACKED);
29090fa753b3SRandall Stewart 						}
29100fa753b3SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
29110fa753b3SRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
29120fa753b3SRandall Stewart 							    tp1->whoTo->flight_size,
29130fa753b3SRandall Stewart 							    tp1->book_size,
29140fa753b3SRandall Stewart 							    (uintptr_t) tp1->whoTo,
29150fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq);
29160fa753b3SRandall Stewart 						}
29170fa753b3SRandall Stewart 						sctp_flight_size_decrease(tp1);
2918299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
2919299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
2920299108c5SRandall Stewart 							    tp1);
2921299108c5SRandall Stewart 						}
29220fa753b3SRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
29230fa753b3SRandall Stewart 
29240fa753b3SRandall Stewart 						tp1->whoTo->net_ack += tp1->send_size;
29250fa753b3SRandall Stewart 						if (tp1->snd_count < 2) {
29260fa753b3SRandall Stewart 							/*-
29270fa753b3SRandall Stewart 							 * True non-retransmited chunk
29280fa753b3SRandall Stewart 							 */
29290fa753b3SRandall Stewart 							tp1->whoTo->net_ack2 += tp1->send_size;
29300fa753b3SRandall Stewart 
29310fa753b3SRandall Stewart 							/*-
29320fa753b3SRandall Stewart 							 * update RTO too ?
29330fa753b3SRandall Stewart 							 */
29340fa753b3SRandall Stewart 							if (tp1->do_rtt) {
2935f79aab18SRandall Stewart 								if (*rto_ok) {
29360fa753b3SRandall Stewart 									tp1->whoTo->RTO =
29370fa753b3SRandall Stewart 									    sctp_calculate_rto(stcb,
29380fa753b3SRandall Stewart 									    &stcb->asoc,
29390fa753b3SRandall Stewart 									    tp1->whoTo,
29400fa753b3SRandall Stewart 									    &tp1->sent_rcv_time,
2941899288aeSRandall Stewart 									    sctp_align_safe_nocopy,
2942f79aab18SRandall Stewart 									    SCTP_RTT_FROM_DATA);
2943f79aab18SRandall Stewart 									*rto_ok = 0;
2944f79aab18SRandall Stewart 								}
2945f79aab18SRandall Stewart 								if (tp1->whoTo->rto_needed == 0) {
2946f79aab18SRandall Stewart 									tp1->whoTo->rto_needed = 1;
2947f79aab18SRandall Stewart 								}
29480fa753b3SRandall Stewart 								tp1->do_rtt = 0;
29490fa753b3SRandall Stewart 							}
29500fa753b3SRandall Stewart 						}
29510fa753b3SRandall Stewart 					}
29520fa753b3SRandall Stewart 					if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
295320b07a4dSMichael Tuexen 						if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
295420b07a4dSMichael Tuexen 						    stcb->asoc.this_sack_highest_gap)) {
29550fa753b3SRandall Stewart 							stcb->asoc.this_sack_highest_gap =
29560fa753b3SRandall Stewart 							    tp1->rec.data.TSN_seq;
29570fa753b3SRandall Stewart 						}
29580fa753b3SRandall Stewart 						if (tp1->sent == SCTP_DATAGRAM_RESEND) {
29590fa753b3SRandall Stewart 							sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
29600fa753b3SRandall Stewart #ifdef SCTP_AUDITING_ENABLED
29610fa753b3SRandall Stewart 							sctp_audit_log(0xB2,
29620fa753b3SRandall Stewart 							    (stcb->asoc.sent_queue_retran_cnt & 0x000000ff));
29630fa753b3SRandall Stewart #endif
29640fa753b3SRandall Stewart 						}
29650fa753b3SRandall Stewart 					}
29660fa753b3SRandall Stewart 					/*-
29670fa753b3SRandall Stewart 					 * All chunks NOT UNSENT fall through here and are marked
29680fa753b3SRandall Stewart 					 * (leave PR-SCTP ones that are to skip alone though)
29690fa753b3SRandall Stewart 					 */
29700fa753b3SRandall Stewart 					if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
29710fa753b3SRandall Stewart 						tp1->sent = SCTP_DATAGRAM_MARKED;
29720fa753b3SRandall Stewart 
29730fa753b3SRandall Stewart 					if (tp1->rec.data.chunk_was_revoked) {
29740fa753b3SRandall Stewart 						/* deflate the cwnd */
29750fa753b3SRandall Stewart 						tp1->whoTo->cwnd -= tp1->book_size;
29760fa753b3SRandall Stewart 						tp1->rec.data.chunk_was_revoked = 0;
29770fa753b3SRandall Stewart 					}
29780fa753b3SRandall Stewart 					/* NR Sack code here */
29790fa753b3SRandall Stewart 					if (nr_sacking) {
29800fa753b3SRandall Stewart 						if (tp1->data) {
29810fa753b3SRandall Stewart 							/*
29820fa753b3SRandall Stewart 							 * sa_ignore
29830fa753b3SRandall Stewart 							 * NO_NULL_CHK
29840fa753b3SRandall Stewart 							 */
29850fa753b3SRandall Stewart 							sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
29860fa753b3SRandall Stewart 							sctp_m_freem(tp1->data);
29870fa753b3SRandall Stewart 							tp1->data = NULL;
2988b5c16493SMichael Tuexen 						}
29890fa753b3SRandall Stewart 						wake_him++;
29900fa753b3SRandall Stewart 					}
29910fa753b3SRandall Stewart 				}
29920fa753b3SRandall Stewart 				break;
29930fa753b3SRandall Stewart 			}	/* if (tp1->TSN_seq == theTSN) */
299420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, theTSN)) {
29950fa753b3SRandall Stewart 				break;
299620b07a4dSMichael Tuexen 			}
29970fa753b3SRandall Stewart 			tp1 = TAILQ_NEXT(tp1, sctp_next);
2998b5c16493SMichael Tuexen 			if ((tp1 == NULL) && (circled == 0)) {
2999b5c16493SMichael Tuexen 				circled++;
30000fa753b3SRandall Stewart 				tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
30010fa753b3SRandall Stewart 			}
3002b5c16493SMichael Tuexen 		}		/* end while (tp1) */
3003b5c16493SMichael Tuexen 		if (tp1 == NULL) {
3004b5c16493SMichael Tuexen 			circled = 0;
3005b5c16493SMichael Tuexen 			tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
3006b5c16493SMichael Tuexen 		}
3007b5c16493SMichael Tuexen 		/* In case the fragments were not in order we must reset */
30080fa753b3SRandall Stewart 	}			/* end for (j = fragStart */
30090fa753b3SRandall Stewart 	*p_tp1 = tp1;
30100fa753b3SRandall Stewart 	return (wake_him);	/* Return value only used for nr-sack */
30110fa753b3SRandall Stewart }
30120fa753b3SRandall Stewart 
30130fa753b3SRandall Stewart 
3014cd554309SMichael Tuexen static int
3015458303daSRandall Stewart sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3016cd554309SMichael Tuexen     uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3017139bc87fSRandall Stewart     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
30187215cc1bSMichael Tuexen     int num_seg, int num_nr_seg, int *rto_ok)
3019f8829a4aSRandall Stewart {
3020458303daSRandall Stewart 	struct sctp_gap_ack_block *frag, block;
3021f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
30220fa753b3SRandall Stewart 	int i;
3023f8829a4aSRandall Stewart 	int num_frs = 0;
3024cd554309SMichael Tuexen 	int chunk_freed;
3025cd554309SMichael Tuexen 	int non_revocable;
3026d9c5cfeaSMichael Tuexen 	uint16_t frag_strt, frag_end, prev_frag_end;
3027f8829a4aSRandall Stewart 
3028d9c5cfeaSMichael Tuexen 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3029d9c5cfeaSMichael Tuexen 	prev_frag_end = 0;
3030cd554309SMichael Tuexen 	chunk_freed = 0;
3031f8829a4aSRandall Stewart 
3032cd554309SMichael Tuexen 	for (i = 0; i < (num_seg + num_nr_seg); i++) {
3033d9c5cfeaSMichael Tuexen 		if (i == num_seg) {
3034d9c5cfeaSMichael Tuexen 			prev_frag_end = 0;
3035d9c5cfeaSMichael Tuexen 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3036d9c5cfeaSMichael Tuexen 		}
3037458303daSRandall Stewart 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3038458303daSRandall Stewart 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3039458303daSRandall Stewart 		*offset += sizeof(block);
3040458303daSRandall Stewart 		if (frag == NULL) {
3041cd554309SMichael Tuexen 			return (chunk_freed);
3042458303daSRandall Stewart 		}
3043f8829a4aSRandall Stewart 		frag_strt = ntohs(frag->start);
3044f8829a4aSRandall Stewart 		frag_end = ntohs(frag->end);
3045d9c5cfeaSMichael Tuexen 
3046f8829a4aSRandall Stewart 		if (frag_strt > frag_end) {
3047d9c5cfeaSMichael Tuexen 			/* This gap report is malformed, skip it. */
3048f8829a4aSRandall Stewart 			continue;
3049f8829a4aSRandall Stewart 		}
3050d9c5cfeaSMichael Tuexen 		if (frag_strt <= prev_frag_end) {
3051d9c5cfeaSMichael Tuexen 			/* This gap report is not in order, so restart. */
3052f8829a4aSRandall Stewart 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3053f8829a4aSRandall Stewart 		}
305420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT((last_tsn + frag_end), *biggest_tsn_acked)) {
3055d9c5cfeaSMichael Tuexen 			*biggest_tsn_acked = last_tsn + frag_end;
3056f8829a4aSRandall Stewart 		}
3057cd554309SMichael Tuexen 		if (i < num_seg) {
3058cd554309SMichael Tuexen 			non_revocable = 0;
3059cd554309SMichael Tuexen 		} else {
3060cd554309SMichael Tuexen 			non_revocable = 1;
3061cd554309SMichael Tuexen 		}
3062cd554309SMichael Tuexen 		if (sctp_process_segment_range(stcb, &tp1, last_tsn, frag_strt, frag_end,
3063cd554309SMichael Tuexen 		    non_revocable, &num_frs, biggest_newly_acked_tsn,
30647215cc1bSMichael Tuexen 		    this_sack_lowest_newack, rto_ok)) {
3065cd554309SMichael Tuexen 			chunk_freed = 1;
3066458303daSRandall Stewart 		}
3067d9c5cfeaSMichael Tuexen 		prev_frag_end = frag_end;
3068f8829a4aSRandall Stewart 	}
3069b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
307080fefe0aSRandall Stewart 		if (num_frs)
307180fefe0aSRandall Stewart 			sctp_log_fr(*biggest_tsn_acked,
307280fefe0aSRandall Stewart 			    *biggest_newly_acked_tsn,
307380fefe0aSRandall Stewart 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
307480fefe0aSRandall Stewart 	}
3075cd554309SMichael Tuexen 	return (chunk_freed);
3076f8829a4aSRandall Stewart }
3077f8829a4aSRandall Stewart 
3078f8829a4aSRandall Stewart static void
3079c105859eSRandall Stewart sctp_check_for_revoked(struct sctp_tcb *stcb,
3080c105859eSRandall Stewart     struct sctp_association *asoc, uint32_t cumack,
308163eda93dSMichael Tuexen     uint32_t biggest_tsn_acked)
3082f8829a4aSRandall Stewart {
3083f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3084f8829a4aSRandall Stewart 	int tot_revoked = 0;
3085f8829a4aSRandall Stewart 
30864a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
308720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cumack)) {
3088f8829a4aSRandall Stewart 			/*
3089f8829a4aSRandall Stewart 			 * ok this guy is either ACK or MARKED. If it is
3090f8829a4aSRandall Stewart 			 * ACKED it has been previously acked but not this
3091f8829a4aSRandall Stewart 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3092f8829a4aSRandall Stewart 			 * again.
3093f8829a4aSRandall Stewart 			 */
309420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked)) {
3095d06c82f1SRandall Stewart 				break;
309620b07a4dSMichael Tuexen 			}
3097f8829a4aSRandall Stewart 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3098f8829a4aSRandall Stewart 				/* it has been revoked */
3099f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
3100f8829a4aSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
3101a5d547adSRandall Stewart 				/*
310242551e99SRandall Stewart 				 * We must add this stuff back in to assure
310342551e99SRandall Stewart 				 * timers and such get started.
3104a5d547adSRandall Stewart 				 */
3105b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3106c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3107c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
3108c105859eSRandall Stewart 					    tp1->book_size,
3109c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
3110c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
311180fefe0aSRandall Stewart 				}
3112c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
3113c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
311442551e99SRandall Stewart 				/*
311542551e99SRandall Stewart 				 * We inflate the cwnd to compensate for our
311642551e99SRandall Stewart 				 * artificial inflation of the flight_size.
311742551e99SRandall Stewart 				 */
311842551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
3119f8829a4aSRandall Stewart 				tot_revoked++;
3120b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3121f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3122f8829a4aSRandall Stewart 					    cumack,
3123f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3124f8829a4aSRandall Stewart 					    0,
3125f8829a4aSRandall Stewart 					    0,
3126f8829a4aSRandall Stewart 					    SCTP_LOG_TSN_REVOKED);
312780fefe0aSRandall Stewart 				}
3128f8829a4aSRandall Stewart 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3129f8829a4aSRandall Stewart 				/* it has been re-acked in this SACK */
3130f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3131f8829a4aSRandall Stewart 			}
3132f8829a4aSRandall Stewart 		}
3133f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3134f8829a4aSRandall Stewart 			break;
3135f8829a4aSRandall Stewart 	}
3136f8829a4aSRandall Stewart }
3137f8829a4aSRandall Stewart 
3138830d754dSRandall Stewart 
3139f8829a4aSRandall Stewart static void
3140f8829a4aSRandall Stewart sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
314163eda93dSMichael Tuexen     uint32_t biggest_tsn_acked, uint32_t biggest_tsn_newly_acked, uint32_t this_sack_lowest_newack, int accum_moved)
3142f8829a4aSRandall Stewart {
3143f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1;
3144f8829a4aSRandall Stewart 	int strike_flag = 0;
3145f8829a4aSRandall Stewart 	struct timeval now;
3146f8829a4aSRandall Stewart 	int tot_retrans = 0;
3147f8829a4aSRandall Stewart 	uint32_t sending_seq;
3148f8829a4aSRandall Stewart 	struct sctp_nets *net;
3149f8829a4aSRandall Stewart 	int num_dests_sacked = 0;
3150f8829a4aSRandall Stewart 
3151f8829a4aSRandall Stewart 	/*
3152f8829a4aSRandall Stewart 	 * select the sending_seq, this is either the next thing ready to be
3153f8829a4aSRandall Stewart 	 * sent but not transmitted, OR, the next seq we assign.
3154f8829a4aSRandall Stewart 	 */
3155f8829a4aSRandall Stewart 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3156f8829a4aSRandall Stewart 	if (tp1 == NULL) {
3157f8829a4aSRandall Stewart 		sending_seq = asoc->sending_seq;
3158f8829a4aSRandall Stewart 	} else {
3159f8829a4aSRandall Stewart 		sending_seq = tp1->rec.data.TSN_seq;
3160f8829a4aSRandall Stewart 	}
3161f8829a4aSRandall Stewart 
3162f8829a4aSRandall Stewart 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
31637c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
316420083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3165f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3166f8829a4aSRandall Stewart 			if (net->saw_newack)
3167f8829a4aSRandall Stewart 				num_dests_sacked++;
3168f8829a4aSRandall Stewart 		}
3169f8829a4aSRandall Stewart 	}
3170f8829a4aSRandall Stewart 	if (stcb->asoc.peer_supports_prsctp) {
31716e55db54SRandall Stewart 		(void)SCTP_GETTIME_TIMEVAL(&now);
3172f8829a4aSRandall Stewart 	}
31734a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
3174f8829a4aSRandall Stewart 		strike_flag = 0;
3175f8829a4aSRandall Stewart 		if (tp1->no_fr_allowed) {
3176f8829a4aSRandall Stewart 			/* this one had a timeout or something */
3177f8829a4aSRandall Stewart 			continue;
3178f8829a4aSRandall Stewart 		}
3179b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3180f8829a4aSRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3181f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3182f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3183f8829a4aSRandall Stewart 				    tp1->sent,
3184f8829a4aSRandall Stewart 				    SCTP_FR_LOG_CHECK_STRIKE);
318580fefe0aSRandall Stewart 		}
318620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, biggest_tsn_acked) ||
3187f8829a4aSRandall Stewart 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3188f8829a4aSRandall Stewart 			/* done */
3189f8829a4aSRandall Stewart 			break;
3190f8829a4aSRandall Stewart 		}
3191f8829a4aSRandall Stewart 		if (stcb->asoc.peer_supports_prsctp) {
3192f8829a4aSRandall Stewart 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3193f8829a4aSRandall Stewart 				/* Is it expired? */
319499ddc825SMichael Tuexen 				if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3195f8829a4aSRandall Stewart 					/* Yes so drop it */
3196f8829a4aSRandall Stewart 					if (tp1->data != NULL) {
3197*1edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
31980c0982b8SRandall Stewart 						    SCTP_SO_NOT_LOCKED);
3199f8829a4aSRandall Stewart 					}
3200f8829a4aSRandall Stewart 					continue;
3201f8829a4aSRandall Stewart 				}
3202f8829a4aSRandall Stewart 			}
3203f8829a4aSRandall Stewart 		}
320420b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->this_sack_highest_gap)) {
3205f8829a4aSRandall Stewart 			/* we are beyond the tsn in the sack  */
3206f8829a4aSRandall Stewart 			break;
3207f8829a4aSRandall Stewart 		}
3208f8829a4aSRandall Stewart 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3209f8829a4aSRandall Stewart 			/* either a RESEND, ACKED, or MARKED */
3210f8829a4aSRandall Stewart 			/* skip */
321144fbe462SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
321244fbe462SRandall Stewart 				/* Continue strikin FWD-TSN chunks */
321344fbe462SRandall Stewart 				tp1->rec.data.fwd_tsn_cnt++;
321444fbe462SRandall Stewart 			}
3215f8829a4aSRandall Stewart 			continue;
3216f8829a4aSRandall Stewart 		}
3217f8829a4aSRandall Stewart 		/*
3218f8829a4aSRandall Stewart 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3219f8829a4aSRandall Stewart 		 */
3220ad81507eSRandall Stewart 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3221f8829a4aSRandall Stewart 			/*
3222f8829a4aSRandall Stewart 			 * No new acks were receieved for data sent to this
3223f8829a4aSRandall Stewart 			 * dest. Therefore, according to the SFR algo for
3224f8829a4aSRandall Stewart 			 * CMT, no data sent to this dest can be marked for
3225c105859eSRandall Stewart 			 * FR using this SACK.
3226f8829a4aSRandall Stewart 			 */
3227f8829a4aSRandall Stewart 			continue;
322820b07a4dSMichael Tuexen 		} else if (tp1->whoTo && SCTP_TSN_GT(tp1->rec.data.TSN_seq,
322920b07a4dSMichael Tuexen 		    tp1->whoTo->this_sack_highest_newack)) {
3230f8829a4aSRandall Stewart 			/*
3231f8829a4aSRandall Stewart 			 * CMT: New acks were receieved for data sent to
3232f8829a4aSRandall Stewart 			 * this dest. But no new acks were seen for data
3233f8829a4aSRandall Stewart 			 * sent after tp1. Therefore, according to the SFR
3234f8829a4aSRandall Stewart 			 * algo for CMT, tp1 cannot be marked for FR using
3235f8829a4aSRandall Stewart 			 * this SACK. This step covers part of the DAC algo
3236f8829a4aSRandall Stewart 			 * and the HTNA algo as well.
3237f8829a4aSRandall Stewart 			 */
3238f8829a4aSRandall Stewart 			continue;
3239f8829a4aSRandall Stewart 		}
3240f8829a4aSRandall Stewart 		/*
3241f8829a4aSRandall Stewart 		 * Here we check to see if we were have already done a FR
3242f8829a4aSRandall Stewart 		 * and if so we see if the biggest TSN we saw in the sack is
3243f8829a4aSRandall Stewart 		 * smaller than the recovery point. If so we don't strike
3244f8829a4aSRandall Stewart 		 * the tsn... otherwise we CAN strike the TSN.
3245f8829a4aSRandall Stewart 		 */
3246f8829a4aSRandall Stewart 		/*
324742551e99SRandall Stewart 		 * @@@ JRI: Check for CMT if (accum_moved &&
324842551e99SRandall Stewart 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
324942551e99SRandall Stewart 		 * 0)) {
3250f8829a4aSRandall Stewart 		 */
325142551e99SRandall Stewart 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3252f8829a4aSRandall Stewart 			/*
3253f8829a4aSRandall Stewart 			 * Strike the TSN if in fast-recovery and cum-ack
3254f8829a4aSRandall Stewart 			 * moved.
3255f8829a4aSRandall Stewart 			 */
3256b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3257f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3258f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3259f8829a4aSRandall Stewart 				    tp1->sent,
3260f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
326180fefe0aSRandall Stewart 			}
32625e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3263f8829a4aSRandall Stewart 				tp1->sent++;
32645e54f665SRandall Stewart 			}
32657c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
326620083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3267f8829a4aSRandall Stewart 				/*
3268f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3269f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3270f8829a4aSRandall Stewart 				 * because it would have been set to the
3271f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3272f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3273f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3274c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3275c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3276c105859eSRandall Stewart 				 * indicates that two packets have been
3277c105859eSRandall Stewart 				 * received after this missing TSN.
32785e54f665SRandall Stewart 				 */
32795e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
328020b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3281b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3282f8829a4aSRandall Stewart 						sctp_log_fr(16 + num_dests_sacked,
3283f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3284f8829a4aSRandall Stewart 						    tp1->sent,
3285f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
328680fefe0aSRandall Stewart 					}
3287f8829a4aSRandall Stewart 					tp1->sent++;
3288f8829a4aSRandall Stewart 				}
3289f8829a4aSRandall Stewart 			}
329020083c2eSMichael Tuexen 		} else if ((tp1->rec.data.doing_fast_retransmit) &&
329120083c2eSMichael Tuexen 		    (asoc->sctp_cmt_on_off == 0)) {
3292f8829a4aSRandall Stewart 			/*
3293f8829a4aSRandall Stewart 			 * For those that have done a FR we must take
3294f8829a4aSRandall Stewart 			 * special consideration if we strike. I.e the
3295f8829a4aSRandall Stewart 			 * biggest_newly_acked must be higher than the
3296f8829a4aSRandall Stewart 			 * sending_seq at the time we did the FR.
3297f8829a4aSRandall Stewart 			 */
32985e54f665SRandall Stewart 			if (
3299f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3300f8829a4aSRandall Stewart 			/*
3301f8829a4aSRandall Stewart 			 * If FR's go to new networks, then we must only do
3302f8829a4aSRandall Stewart 			 * this for singly homed asoc's. However if the FR's
3303f8829a4aSRandall Stewart 			 * go to the same network (Armando's work) then its
3304f8829a4aSRandall Stewart 			 * ok to FR multiple times.
3305f8829a4aSRandall Stewart 			 */
33065e54f665SRandall Stewart 			    (asoc->numnets < 2)
3307f8829a4aSRandall Stewart #else
33085e54f665SRandall Stewart 			    (1)
3309f8829a4aSRandall Stewart #endif
33105e54f665SRandall Stewart 			    ) {
33115e54f665SRandall Stewart 
331220b07a4dSMichael Tuexen 				if (SCTP_TSN_GE(biggest_tsn_newly_acked,
3313f8829a4aSRandall Stewart 				    tp1->rec.data.fast_retran_tsn)) {
3314f8829a4aSRandall Stewart 					/*
3315f8829a4aSRandall Stewart 					 * Strike the TSN, since this ack is
3316f8829a4aSRandall Stewart 					 * beyond where things were when we
3317f8829a4aSRandall Stewart 					 * did a FR.
3318f8829a4aSRandall Stewart 					 */
3319b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3320f8829a4aSRandall Stewart 						sctp_log_fr(biggest_tsn_newly_acked,
3321f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3322f8829a4aSRandall Stewart 						    tp1->sent,
3323f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
332480fefe0aSRandall Stewart 					}
33255e54f665SRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3326f8829a4aSRandall Stewart 						tp1->sent++;
33275e54f665SRandall Stewart 					}
3328f8829a4aSRandall Stewart 					strike_flag = 1;
33297c99d56fSMichael Tuexen 					if ((asoc->sctp_cmt_on_off > 0) &&
333020083c2eSMichael Tuexen 					    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3331f8829a4aSRandall Stewart 						/*
3332f8829a4aSRandall Stewart 						 * CMT DAC algorithm: If
3333f8829a4aSRandall Stewart 						 * SACK flag is set to 0,
3334f8829a4aSRandall Stewart 						 * then lowest_newack test
3335f8829a4aSRandall Stewart 						 * will not pass because it
3336f8829a4aSRandall Stewart 						 * would have been set to
3337f8829a4aSRandall Stewart 						 * the cumack earlier. If
3338f8829a4aSRandall Stewart 						 * not already to be rtx'd,
3339f8829a4aSRandall Stewart 						 * If not a mixed sack and
3340f8829a4aSRandall Stewart 						 * if tp1 is not between two
3341f8829a4aSRandall Stewart 						 * sacked TSNs, then mark by
3342c105859eSRandall Stewart 						 * one more. NOTE that we
3343c105859eSRandall Stewart 						 * are marking by one
3344c105859eSRandall Stewart 						 * additional time since the
3345c105859eSRandall Stewart 						 * SACK DAC flag indicates
3346c105859eSRandall Stewart 						 * that two packets have
3347c105859eSRandall Stewart 						 * been received after this
3348c105859eSRandall Stewart 						 * missing TSN.
3349f8829a4aSRandall Stewart 						 */
33505e54f665SRandall Stewart 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
33515e54f665SRandall Stewart 						    (num_dests_sacked == 1) &&
335220b07a4dSMichael Tuexen 						    SCTP_TSN_GT(this_sack_lowest_newack,
335320b07a4dSMichael Tuexen 						    tp1->rec.data.TSN_seq)) {
3354b3f1ea41SRandall Stewart 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3355f8829a4aSRandall Stewart 								sctp_log_fr(32 + num_dests_sacked,
3356f8829a4aSRandall Stewart 								    tp1->rec.data.TSN_seq,
3357f8829a4aSRandall Stewart 								    tp1->sent,
3358f8829a4aSRandall Stewart 								    SCTP_FR_LOG_STRIKE_CHUNK);
335980fefe0aSRandall Stewart 							}
33605e54f665SRandall Stewart 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3361f8829a4aSRandall Stewart 								tp1->sent++;
33625e54f665SRandall Stewart 							}
3363f8829a4aSRandall Stewart 						}
3364f8829a4aSRandall Stewart 					}
3365f8829a4aSRandall Stewart 				}
3366f8829a4aSRandall Stewart 			}
3367f8829a4aSRandall Stewart 			/*
336842551e99SRandall Stewart 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
336942551e99SRandall Stewart 			 * algo covers HTNA.
3370f8829a4aSRandall Stewart 			 */
337120b07a4dSMichael Tuexen 		} else if (SCTP_TSN_GT(tp1->rec.data.TSN_seq,
337220b07a4dSMichael Tuexen 		    biggest_tsn_newly_acked)) {
3373f8829a4aSRandall Stewart 			/*
3374f8829a4aSRandall Stewart 			 * We don't strike these: This is the  HTNA
3375f8829a4aSRandall Stewart 			 * algorithm i.e. we don't strike If our TSN is
3376f8829a4aSRandall Stewart 			 * larger than the Highest TSN Newly Acked.
3377f8829a4aSRandall Stewart 			 */
3378f8829a4aSRandall Stewart 			;
3379f8829a4aSRandall Stewart 		} else {
3380f8829a4aSRandall Stewart 			/* Strike the TSN */
3381b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3382f8829a4aSRandall Stewart 				sctp_log_fr(biggest_tsn_newly_acked,
3383f8829a4aSRandall Stewart 				    tp1->rec.data.TSN_seq,
3384f8829a4aSRandall Stewart 				    tp1->sent,
3385f8829a4aSRandall Stewart 				    SCTP_FR_LOG_STRIKE_CHUNK);
338680fefe0aSRandall Stewart 			}
33875e54f665SRandall Stewart 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3388f8829a4aSRandall Stewart 				tp1->sent++;
33895e54f665SRandall Stewart 			}
33907c99d56fSMichael Tuexen 			if ((asoc->sctp_cmt_on_off > 0) &&
339120083c2eSMichael Tuexen 			    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3392f8829a4aSRandall Stewart 				/*
3393f8829a4aSRandall Stewart 				 * CMT DAC algorithm: If SACK flag is set to
3394f8829a4aSRandall Stewart 				 * 0, then lowest_newack test will not pass
3395f8829a4aSRandall Stewart 				 * because it would have been set to the
3396f8829a4aSRandall Stewart 				 * cumack earlier. If not already to be
3397f8829a4aSRandall Stewart 				 * rtx'd, If not a mixed sack and if tp1 is
3398f8829a4aSRandall Stewart 				 * not between two sacked TSNs, then mark by
3399c105859eSRandall Stewart 				 * one more. NOTE that we are marking by one
3400c105859eSRandall Stewart 				 * additional time since the SACK DAC flag
3401c105859eSRandall Stewart 				 * indicates that two packets have been
3402c105859eSRandall Stewart 				 * received after this missing TSN.
3403f8829a4aSRandall Stewart 				 */
34045e54f665SRandall Stewart 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
340520b07a4dSMichael Tuexen 				    SCTP_TSN_GT(this_sack_lowest_newack, tp1->rec.data.TSN_seq)) {
3406b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3407f8829a4aSRandall Stewart 						sctp_log_fr(48 + num_dests_sacked,
3408f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
3409f8829a4aSRandall Stewart 						    tp1->sent,
3410f8829a4aSRandall Stewart 						    SCTP_FR_LOG_STRIKE_CHUNK);
341180fefe0aSRandall Stewart 					}
3412f8829a4aSRandall Stewart 					tp1->sent++;
3413f8829a4aSRandall Stewart 				}
3414f8829a4aSRandall Stewart 			}
3415f8829a4aSRandall Stewart 		}
3416f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3417f8829a4aSRandall Stewart 			struct sctp_nets *alt;
3418f8829a4aSRandall Stewart 
3419544e35bdSRandall Stewart 			/* fix counts and things */
3420544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3421544e35bdSRandall Stewart 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3422544e35bdSRandall Stewart 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3423544e35bdSRandall Stewart 				    tp1->book_size,
3424544e35bdSRandall Stewart 				    (uintptr_t) tp1->whoTo,
3425544e35bdSRandall Stewart 				    tp1->rec.data.TSN_seq);
3426544e35bdSRandall Stewart 			}
3427544e35bdSRandall Stewart 			if (tp1->whoTo) {
3428544e35bdSRandall Stewart 				tp1->whoTo->net_ack++;
3429544e35bdSRandall Stewart 				sctp_flight_size_decrease(tp1);
3430299108c5SRandall Stewart 				if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3431299108c5SRandall Stewart 					(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3432299108c5SRandall Stewart 					    tp1);
3433299108c5SRandall Stewart 				}
3434544e35bdSRandall Stewart 			}
3435544e35bdSRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3436544e35bdSRandall Stewart 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3437544e35bdSRandall Stewart 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3438544e35bdSRandall Stewart 			}
3439544e35bdSRandall Stewart 			/* add back to the rwnd */
3440544e35bdSRandall Stewart 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3441544e35bdSRandall Stewart 
3442544e35bdSRandall Stewart 			/* remove from the total flight */
3443544e35bdSRandall Stewart 			sctp_total_flight_decrease(stcb, tp1);
3444544e35bdSRandall Stewart 
3445475d0674SMichael Tuexen 			if ((stcb->asoc.peer_supports_prsctp) &&
3446475d0674SMichael Tuexen 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3447475d0674SMichael Tuexen 				/*
3448475d0674SMichael Tuexen 				 * Has it been retransmitted tv_sec times? -
3449475d0674SMichael Tuexen 				 * we store the retran count there.
3450475d0674SMichael Tuexen 				 */
3451475d0674SMichael Tuexen 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3452475d0674SMichael Tuexen 					/* Yes, so drop it */
3453475d0674SMichael Tuexen 					if (tp1->data != NULL) {
3454*1edc9dbaSMichael Tuexen 						(void)sctp_release_pr_sctp_chunk(stcb, tp1, 1,
3455475d0674SMichael Tuexen 						    SCTP_SO_NOT_LOCKED);
3456475d0674SMichael Tuexen 					}
3457475d0674SMichael Tuexen 					/* Make sure to flag we had a FR */
3458475d0674SMichael Tuexen 					tp1->whoTo->net_ack++;
3459475d0674SMichael Tuexen 					continue;
3460475d0674SMichael Tuexen 				}
3461475d0674SMichael Tuexen 			}
3462cd3fd531SMichael Tuexen 			/*
3463cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("OK, we are now ready to FR this
3464cd3fd531SMichael Tuexen 			 * guy\n");
3465cd3fd531SMichael Tuexen 			 */
3466b3f1ea41SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3467f8829a4aSRandall Stewart 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3468f8829a4aSRandall Stewart 				    0, SCTP_FR_MARKED);
346980fefe0aSRandall Stewart 			}
3470f8829a4aSRandall Stewart 			if (strike_flag) {
3471f8829a4aSRandall Stewart 				/* This is a subsequent FR */
3472f8829a4aSRandall Stewart 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3473f8829a4aSRandall Stewart 			}
34745e54f665SRandall Stewart 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
34757c99d56fSMichael Tuexen 			if (asoc->sctp_cmt_on_off > 0) {
3476f8829a4aSRandall Stewart 				/*
3477f8829a4aSRandall Stewart 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3478f8829a4aSRandall Stewart 				 * If CMT is being used, then pick dest with
3479f8829a4aSRandall Stewart 				 * largest ssthresh for any retransmission.
3480f8829a4aSRandall Stewart 				 */
3481f8829a4aSRandall Stewart 				tp1->no_fr_allowed = 1;
3482f8829a4aSRandall Stewart 				alt = tp1->whoTo;
34833c503c28SRandall Stewart 				/* sa_ignore NO_NULL_CHK */
348420083c2eSMichael Tuexen 				if (asoc->sctp_cmt_pf > 0) {
3485b54d3a6cSRandall Stewart 					/*
3486b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If CMT PF is on,
3487b54d3a6cSRandall Stewart 					 * use the PF version of
3488b54d3a6cSRandall Stewart 					 * find_alt_net()
3489b54d3a6cSRandall Stewart 					 */
3490b54d3a6cSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 2);
3491b54d3a6cSRandall Stewart 				} else {
3492b54d3a6cSRandall Stewart 					/*
3493b54d3a6cSRandall Stewart 					 * JRS 5/18/07 - If only CMT is on,
3494b54d3a6cSRandall Stewart 					 * use the CMT version of
3495b54d3a6cSRandall Stewart 					 * find_alt_net()
3496b54d3a6cSRandall Stewart 					 */
349752be287eSRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3498f8829a4aSRandall Stewart 					alt = sctp_find_alternate_net(stcb, alt, 1);
3499b54d3a6cSRandall Stewart 				}
3500ad81507eSRandall Stewart 				if (alt == NULL) {
3501ad81507eSRandall Stewart 					alt = tp1->whoTo;
3502ad81507eSRandall Stewart 				}
3503f8829a4aSRandall Stewart 				/*
3504f8829a4aSRandall Stewart 				 * CUCv2: If a different dest is picked for
3505f8829a4aSRandall Stewart 				 * the retransmission, then new
3506f8829a4aSRandall Stewart 				 * (rtx-)pseudo_cumack needs to be tracked
3507f8829a4aSRandall Stewart 				 * for orig dest. Let CUCv2 track new (rtx-)
3508f8829a4aSRandall Stewart 				 * pseudo-cumack always.
3509f8829a4aSRandall Stewart 				 */
3510ad81507eSRandall Stewart 				if (tp1->whoTo) {
3511f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3512f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3513ad81507eSRandall Stewart 				}
3514f8829a4aSRandall Stewart 			} else {/* CMT is OFF */
3515f8829a4aSRandall Stewart 
3516f8829a4aSRandall Stewart #ifdef SCTP_FR_TO_ALTERNATE
3517f8829a4aSRandall Stewart 				/* Can we find an alternate? */
3518f8829a4aSRandall Stewart 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3519f8829a4aSRandall Stewart #else
3520f8829a4aSRandall Stewart 				/*
3521f8829a4aSRandall Stewart 				 * default behavior is to NOT retransmit
3522f8829a4aSRandall Stewart 				 * FR's to an alternate. Armando Caro's
3523f8829a4aSRandall Stewart 				 * paper details why.
3524f8829a4aSRandall Stewart 				 */
3525f8829a4aSRandall Stewart 				alt = tp1->whoTo;
3526f8829a4aSRandall Stewart #endif
3527f8829a4aSRandall Stewart 			}
3528f8829a4aSRandall Stewart 
3529f8829a4aSRandall Stewart 			tp1->rec.data.doing_fast_retransmit = 1;
3530f8829a4aSRandall Stewart 			tot_retrans++;
3531f8829a4aSRandall Stewart 			/* mark the sending seq for possible subsequent FR's */
3532f8829a4aSRandall Stewart 			/*
3533cd3fd531SMichael Tuexen 			 * SCTP_PRINTF("Marking TSN for FR new value %x\n",
3534f8829a4aSRandall Stewart 			 * (uint32_t)tpi->rec.data.TSN_seq);
3535f8829a4aSRandall Stewart 			 */
3536f8829a4aSRandall Stewart 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3537f8829a4aSRandall Stewart 				/*
3538f8829a4aSRandall Stewart 				 * If the queue of send is empty then its
3539f8829a4aSRandall Stewart 				 * the next sequence number that will be
3540f8829a4aSRandall Stewart 				 * assigned so we subtract one from this to
3541f8829a4aSRandall Stewart 				 * get the one we last sent.
3542f8829a4aSRandall Stewart 				 */
3543f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn = sending_seq;
3544f8829a4aSRandall Stewart 			} else {
3545f8829a4aSRandall Stewart 				/*
3546f8829a4aSRandall Stewart 				 * If there are chunks on the send queue
3547f8829a4aSRandall Stewart 				 * (unsent data that has made it from the
3548f8829a4aSRandall Stewart 				 * stream queues but not out the door, we
3549f8829a4aSRandall Stewart 				 * take the first one (which will have the
3550f8829a4aSRandall Stewart 				 * lowest TSN) and subtract one to get the
3551f8829a4aSRandall Stewart 				 * one we last sent.
3552f8829a4aSRandall Stewart 				 */
3553f8829a4aSRandall Stewart 				struct sctp_tmit_chunk *ttt;
3554f8829a4aSRandall Stewart 
3555f8829a4aSRandall Stewart 				ttt = TAILQ_FIRST(&asoc->send_queue);
3556f8829a4aSRandall Stewart 				tp1->rec.data.fast_retran_tsn =
3557f8829a4aSRandall Stewart 				    ttt->rec.data.TSN_seq;
3558f8829a4aSRandall Stewart 			}
3559f8829a4aSRandall Stewart 
3560f8829a4aSRandall Stewart 			if (tp1->do_rtt) {
3561f8829a4aSRandall Stewart 				/*
3562f8829a4aSRandall Stewart 				 * this guy had a RTO calculation pending on
3563f8829a4aSRandall Stewart 				 * it, cancel it
3564f8829a4aSRandall Stewart 				 */
356560990c0cSMichael Tuexen 				if ((tp1->whoTo != NULL) &&
356660990c0cSMichael Tuexen 				    (tp1->whoTo->rto_needed == 0)) {
3567f79aab18SRandall Stewart 					tp1->whoTo->rto_needed = 1;
3568f79aab18SRandall Stewart 				}
3569f8829a4aSRandall Stewart 				tp1->do_rtt = 0;
3570f8829a4aSRandall Stewart 			}
3571f8829a4aSRandall Stewart 			if (alt != tp1->whoTo) {
3572f8829a4aSRandall Stewart 				/* yes, there is an alternate. */
3573f8829a4aSRandall Stewart 				sctp_free_remote_addr(tp1->whoTo);
35743c503c28SRandall Stewart 				/* sa_ignore FREED_MEMORY */
3575f8829a4aSRandall Stewart 				tp1->whoTo = alt;
3576f8829a4aSRandall Stewart 				atomic_add_int(&alt->ref_count, 1);
3577f8829a4aSRandall Stewart 			}
3578f8829a4aSRandall Stewart 		}
35794a9ef3f8SMichael Tuexen 	}
3580f8829a4aSRandall Stewart }
3581f8829a4aSRandall Stewart 
3582f8829a4aSRandall Stewart struct sctp_tmit_chunk *
3583f8829a4aSRandall Stewart sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3584f8829a4aSRandall Stewart     struct sctp_association *asoc)
3585f8829a4aSRandall Stewart {
3586f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3587f8829a4aSRandall Stewart 	struct timeval now;
3588f8829a4aSRandall Stewart 	int now_filled = 0;
3589f8829a4aSRandall Stewart 
3590f8829a4aSRandall Stewart 	if (asoc->peer_supports_prsctp == 0) {
3591f8829a4aSRandall Stewart 		return (NULL);
3592f8829a4aSRandall Stewart 	}
35934a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
3594f8829a4aSRandall Stewart 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3595f8829a4aSRandall Stewart 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3596f8829a4aSRandall Stewart 			/* no chance to advance, out of here */
3597f8829a4aSRandall Stewart 			break;
3598f8829a4aSRandall Stewart 		}
35990c0982b8SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
36000c0982b8SRandall Stewart 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
36010c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
36020c0982b8SRandall Stewart 				    asoc->advanced_peer_ack_point,
36030c0982b8SRandall Stewart 				    tp1->rec.data.TSN_seq, 0, 0);
36040c0982b8SRandall Stewart 			}
36050c0982b8SRandall Stewart 		}
3606f8829a4aSRandall Stewart 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3607f8829a4aSRandall Stewart 			/*
3608f8829a4aSRandall Stewart 			 * We can't fwd-tsn past any that are reliable aka
3609f8829a4aSRandall Stewart 			 * retransmitted until the asoc fails.
3610f8829a4aSRandall Stewart 			 */
3611f8829a4aSRandall Stewart 			break;
3612f8829a4aSRandall Stewart 		}
3613f8829a4aSRandall Stewart 		if (!now_filled) {
36146e55db54SRandall Stewart 			(void)SCTP_GETTIME_TIMEVAL(&now);
3615f8829a4aSRandall Stewart 			now_filled = 1;
3616f8829a4aSRandall Stewart 		}
3617f8829a4aSRandall Stewart 		/*
3618f8829a4aSRandall Stewart 		 * now we got a chunk which is marked for another
3619f8829a4aSRandall Stewart 		 * retransmission to a PR-stream but has run out its chances
3620f8829a4aSRandall Stewart 		 * already maybe OR has been marked to skip now. Can we skip
3621f8829a4aSRandall Stewart 		 * it if its a resend?
3622f8829a4aSRandall Stewart 		 */
3623f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3624f8829a4aSRandall Stewart 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3625f8829a4aSRandall Stewart 			/*
3626f8829a4aSRandall Stewart 			 * Now is this one marked for resend and its time is
3627f8829a4aSRandall Stewart 			 * now up?
3628f8829a4aSRandall Stewart 			 */
3629f8829a4aSRandall Stewart 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3630f8829a4aSRandall Stewart 				/* Yes so drop it */
3631f8829a4aSRandall Stewart 				if (tp1->data) {
3632ad81507eSRandall Stewart 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3633*1edc9dbaSMichael Tuexen 					    1, SCTP_SO_NOT_LOCKED);
3634f8829a4aSRandall Stewart 				}
3635f8829a4aSRandall Stewart 			} else {
3636f8829a4aSRandall Stewart 				/*
3637f8829a4aSRandall Stewart 				 * No, we are done when hit one for resend
3638f8829a4aSRandall Stewart 				 * whos time as not expired.
3639f8829a4aSRandall Stewart 				 */
3640f8829a4aSRandall Stewart 				break;
3641f8829a4aSRandall Stewart 			}
3642f8829a4aSRandall Stewart 		}
3643f8829a4aSRandall Stewart 		/*
3644f8829a4aSRandall Stewart 		 * Ok now if this chunk is marked to drop it we can clean up
3645f8829a4aSRandall Stewart 		 * the chunk, advance our peer ack point and we can check
3646f8829a4aSRandall Stewart 		 * the next chunk.
3647f8829a4aSRandall Stewart 		 */
364844fbe462SRandall Stewart 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3649f8829a4aSRandall Stewart 			/* advance PeerAckPoint goes forward */
365020b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, asoc->advanced_peer_ack_point)) {
3651f8829a4aSRandall Stewart 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3652f8829a4aSRandall Stewart 				a_adv = tp1;
36530c0982b8SRandall Stewart 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
36540c0982b8SRandall Stewart 				/* No update but we do save the chk */
36550c0982b8SRandall Stewart 				a_adv = tp1;
36560c0982b8SRandall Stewart 			}
3657f8829a4aSRandall Stewart 		} else {
3658f8829a4aSRandall Stewart 			/*
3659f8829a4aSRandall Stewart 			 * If it is still in RESEND we can advance no
3660f8829a4aSRandall Stewart 			 * further
3661f8829a4aSRandall Stewart 			 */
3662f8829a4aSRandall Stewart 			break;
3663f8829a4aSRandall Stewart 		}
3664f8829a4aSRandall Stewart 	}
3665f8829a4aSRandall Stewart 	return (a_adv);
3666f8829a4aSRandall Stewart }
3667f8829a4aSRandall Stewart 
36680c0982b8SRandall Stewart static int
3669c105859eSRandall Stewart sctp_fs_audit(struct sctp_association *asoc)
3670bff64a4dSRandall Stewart {
3671bff64a4dSRandall Stewart 	struct sctp_tmit_chunk *chk;
3672bff64a4dSRandall Stewart 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
36730c0982b8SRandall Stewart 	int entry_flight, entry_cnt, ret;
36740c0982b8SRandall Stewart 
36750c0982b8SRandall Stewart 	entry_flight = asoc->total_flight;
36760c0982b8SRandall Stewart 	entry_cnt = asoc->total_flight_count;
36770c0982b8SRandall Stewart 	ret = 0;
36780c0982b8SRandall Stewart 
36790c0982b8SRandall Stewart 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
36800c0982b8SRandall Stewart 		return (0);
3681bff64a4dSRandall Stewart 
3682bff64a4dSRandall Stewart 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3683bff64a4dSRandall Stewart 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3684cd3fd531SMichael Tuexen 			SCTP_PRINTF("Chk TSN:%u size:%d inflight cnt:%d\n",
36850c0982b8SRandall Stewart 			    chk->rec.data.TSN_seq,
36860c0982b8SRandall Stewart 			    chk->send_size,
3687cd3fd531SMichael Tuexen 			    chk->snd_count);
3688bff64a4dSRandall Stewart 			inflight++;
3689bff64a4dSRandall Stewart 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3690bff64a4dSRandall Stewart 			resend++;
3691bff64a4dSRandall Stewart 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3692bff64a4dSRandall Stewart 			inbetween++;
3693bff64a4dSRandall Stewart 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3694bff64a4dSRandall Stewart 			above++;
3695bff64a4dSRandall Stewart 		} else {
3696bff64a4dSRandall Stewart 			acked++;
3697bff64a4dSRandall Stewart 		}
3698bff64a4dSRandall Stewart 	}
3699f1f73e57SRandall Stewart 
3700c105859eSRandall Stewart 	if ((inflight > 0) || (inbetween > 0)) {
3701f1f73e57SRandall Stewart #ifdef INVARIANTS
3702c105859eSRandall Stewart 		panic("Flight size-express incorrect? \n");
3703f1f73e57SRandall Stewart #else
3704cd3fd531SMichael Tuexen 		SCTP_PRINTF("asoc->total_flight:%d cnt:%d\n",
37050c0982b8SRandall Stewart 		    entry_flight, entry_cnt);
37060c0982b8SRandall Stewart 
37070c0982b8SRandall Stewart 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
37080c0982b8SRandall Stewart 		    inflight, inbetween, resend, above, acked);
37090c0982b8SRandall Stewart 		ret = 1;
3710f1f73e57SRandall Stewart #endif
3711bff64a4dSRandall Stewart 	}
37120c0982b8SRandall Stewart 	return (ret);
3713c105859eSRandall Stewart }
3714c105859eSRandall Stewart 
3715c105859eSRandall Stewart 
3716c105859eSRandall Stewart static void
3717c105859eSRandall Stewart sctp_window_probe_recovery(struct sctp_tcb *stcb,
3718c105859eSRandall Stewart     struct sctp_association *asoc,
3719c105859eSRandall Stewart     struct sctp_tmit_chunk *tp1)
3720c105859eSRandall Stewart {
3721dfb11ef8SRandall Stewart 	tp1->window_probe = 0;
37225171328bSRandall Stewart 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
3723dfb11ef8SRandall Stewart 		/* TSN's skipped we do NOT move back. */
3724dfb11ef8SRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
3725dfb11ef8SRandall Stewart 		    tp1->whoTo->flight_size,
3726dfb11ef8SRandall Stewart 		    tp1->book_size,
3727dfb11ef8SRandall Stewart 		    (uintptr_t) tp1->whoTo,
3728dfb11ef8SRandall Stewart 		    tp1->rec.data.TSN_seq);
3729dfb11ef8SRandall Stewart 		return;
3730dfb11ef8SRandall Stewart 	}
37315171328bSRandall Stewart 	/* First setup this by shrinking flight */
3732299108c5SRandall Stewart 	if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3733299108c5SRandall Stewart 		(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3734299108c5SRandall Stewart 		    tp1);
3735299108c5SRandall Stewart 	}
37365171328bSRandall Stewart 	sctp_flight_size_decrease(tp1);
37375171328bSRandall Stewart 	sctp_total_flight_decrease(stcb, tp1);
37385171328bSRandall Stewart 	/* Now mark for resend */
37395171328bSRandall Stewart 	tp1->sent = SCTP_DATAGRAM_RESEND;
3740791437b5SRandall Stewart 	sctp_ucount_incr(asoc->sent_queue_retran_cnt);
3741791437b5SRandall Stewart 
3742b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3743c105859eSRandall Stewart 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3744c105859eSRandall Stewart 		    tp1->whoTo->flight_size,
3745c105859eSRandall Stewart 		    tp1->book_size,
3746c105859eSRandall Stewart 		    (uintptr_t) tp1->whoTo,
3747c105859eSRandall Stewart 		    tp1->rec.data.TSN_seq);
374880fefe0aSRandall Stewart 	}
3749c105859eSRandall Stewart }
3750c105859eSRandall Stewart 
3751f8829a4aSRandall Stewart void
3752f8829a4aSRandall Stewart sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3753899288aeSRandall Stewart     uint32_t rwnd, int *abort_now, int ecne_seen)
3754f8829a4aSRandall Stewart {
3755f8829a4aSRandall Stewart 	struct sctp_nets *net;
3756f8829a4aSRandall Stewart 	struct sctp_association *asoc;
3757f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
37585e54f665SRandall Stewart 	uint32_t old_rwnd;
37595e54f665SRandall Stewart 	int win_probe_recovery = 0;
3760c105859eSRandall Stewart 	int win_probe_recovered = 0;
3761d06c82f1SRandall Stewart 	int j, done_once = 0;
3762f79aab18SRandall Stewart 	int rto_ok = 1;
3763f8829a4aSRandall Stewart 
3764b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3765d06c82f1SRandall Stewart 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3766d06c82f1SRandall Stewart 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
376780fefe0aSRandall Stewart 	}
3768f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
376918e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
377018e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
377118e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
377218e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
377318e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
377418e198d3SRandall Stewart 	}
377518e198d3SRandall Stewart #endif
3776f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
3777d06c82f1SRandall Stewart 	old_rwnd = asoc->peers_rwnd;
377820b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, cumack)) {
37795e54f665SRandall Stewart 		/* old ack */
37805e54f665SRandall Stewart 		return;
3781d06c82f1SRandall Stewart 	} else if (asoc->last_acked_seq == cumack) {
3782d06c82f1SRandall Stewart 		/* Window update sack */
3783d06c82f1SRandall Stewart 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
378444fbe462SRandall Stewart 		    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
3785d06c82f1SRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3786d06c82f1SRandall Stewart 			/* SWS sender side engages */
3787d06c82f1SRandall Stewart 			asoc->peers_rwnd = 0;
3788d06c82f1SRandall Stewart 		}
3789d06c82f1SRandall Stewart 		if (asoc->peers_rwnd > old_rwnd) {
3790d06c82f1SRandall Stewart 			goto again;
3791d06c82f1SRandall Stewart 		}
3792d06c82f1SRandall Stewart 		return;
37935e54f665SRandall Stewart 	}
3794f8829a4aSRandall Stewart 	/* First setup for CC stuff */
3795f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3796a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cumack, net->cwr_window_tsn)) {
3797a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
3798a21779f0SRandall Stewart 			net->cwr_window_tsn = cumack;
3799a21779f0SRandall Stewart 		}
3800f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
3801f8829a4aSRandall Stewart 		net->net_ack = 0;
3802f8829a4aSRandall Stewart 		net->net_ack2 = 0;
3803132dea7dSRandall Stewart 
3804132dea7dSRandall Stewart 		/*
3805132dea7dSRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
3806132dea7dSRandall Stewart 		 * SACK processing
3807132dea7dSRandall Stewart 		 */
3808132dea7dSRandall Stewart 		net->new_pseudo_cumack = 0;
3809132dea7dSRandall Stewart 		net->will_exit_fast_recovery = 0;
3810299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
3811299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
3812299108c5SRandall Stewart 		}
3813f8829a4aSRandall Stewart 	}
3814b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
3815139bc87fSRandall Stewart 		uint32_t send_s;
3816139bc87fSRandall Stewart 
3817c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3818c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
3819c105859eSRandall Stewart 			    sctpchunk_listhead);
3820c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
3821139bc87fSRandall Stewart 		} else {
3822c105859eSRandall Stewart 			send_s = asoc->sending_seq;
3823139bc87fSRandall Stewart 		}
382420b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cumack, send_s)) {
3825c105859eSRandall Stewart #ifndef INVARIANTS
3826139bc87fSRandall Stewart 			struct mbuf *oper;
3827139bc87fSRandall Stewart 
3828c105859eSRandall Stewart #endif
3829c105859eSRandall Stewart #ifdef INVARIANTS
3830c105859eSRandall Stewart 			panic("Impossible sack 1");
3831c105859eSRandall Stewart #else
3832b5c16493SMichael Tuexen 
3833139bc87fSRandall Stewart 			*abort_now = 1;
3834139bc87fSRandall Stewart 			/* XXX */
3835139bc87fSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3836139bc87fSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
3837139bc87fSRandall Stewart 			if (oper) {
3838139bc87fSRandall Stewart 				struct sctp_paramhdr *ph;
3839139bc87fSRandall Stewart 				uint32_t *ippp;
3840139bc87fSRandall Stewart 
3841139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3842139bc87fSRandall Stewart 				    sizeof(uint32_t);
3843139bc87fSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
3844139bc87fSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3845139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
3846139bc87fSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
3847139bc87fSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3848139bc87fSRandall Stewart 			}
3849139bc87fSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3850a2b42326SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
3851139bc87fSRandall Stewart 			return;
3852139bc87fSRandall Stewart #endif
3853139bc87fSRandall Stewart 		}
3854139bc87fSRandall Stewart 	}
3855f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = cumack;
3856b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3857c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3858c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
3859c4739e2fSRandall Stewart 		    0,
3860c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
3861c4739e2fSRandall Stewart 		    __LINE__);
3862c4739e2fSRandall Stewart 	}
3863f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
386420b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->last_acked_seq)) {
3865f8829a4aSRandall Stewart 		/* process the new consecutive TSN first */
38664a9ef3f8SMichael Tuexen 		TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
386720b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(cumack, tp1->rec.data.TSN_seq)) {
386818e198d3SRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3869cd3fd531SMichael Tuexen 					SCTP_PRINTF("Warning, an unsent is now acked?\n");
387018e198d3SRandall Stewart 				}
3871f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3872f8829a4aSRandall Stewart 					/*
387318e198d3SRandall Stewart 					 * If it is less than ACKED, it is
387418e198d3SRandall Stewart 					 * now no-longer in flight. Higher
387518e198d3SRandall Stewart 					 * values may occur during marking
3876f8829a4aSRandall Stewart 					 */
3877c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3878b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3879c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3880a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
3881a5d547adSRandall Stewart 							    tp1->book_size,
3882c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
3883a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
388480fefe0aSRandall Stewart 						}
3885c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
3886299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
3887299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
3888299108c5SRandall Stewart 							    tp1);
3889299108c5SRandall Stewart 						}
389004ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3891c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
3892f8829a4aSRandall Stewart 					}
3893f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
3894f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
3895f8829a4aSRandall Stewart 						/*
389618e198d3SRandall Stewart 						 * True non-retransmited
3897f8829a4aSRandall Stewart 						 * chunk
3898f8829a4aSRandall Stewart 						 */
3899f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
3900f8829a4aSRandall Stewart 						    tp1->send_size;
3901f8829a4aSRandall Stewart 
3902f8829a4aSRandall Stewart 						/* update RTO too? */
390362c1ff9cSRandall Stewart 						if (tp1->do_rtt) {
3904f79aab18SRandall Stewart 							if (rto_ok) {
3905f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
390604ee05e8SRandall Stewart 								/*
390704ee05e8SRandall Stewart 								 * sa_ignore
3908f79aab18SRandall Stewart 								 * NO_NULL_CH
3909f79aab18SRandall Stewart 								 * K
391004ee05e8SRandall Stewart 								 */
3911f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
3912f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
391318e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
3914899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
3915f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
3916f79aab18SRandall Stewart 								rto_ok = 0;
3917f79aab18SRandall Stewart 							}
3918f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
3919f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
3920f79aab18SRandall Stewart 							}
3921f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
3922f8829a4aSRandall Stewart 						}
3923f8829a4aSRandall Stewart 					}
3924132dea7dSRandall Stewart 					/*
392518e198d3SRandall Stewart 					 * CMT: CUCv2 algorithm. From the
392618e198d3SRandall Stewart 					 * cumack'd TSNs, for each TSN being
392718e198d3SRandall Stewart 					 * acked for the first time, set the
392818e198d3SRandall Stewart 					 * following variables for the
392918e198d3SRandall Stewart 					 * corresp destination.
393018e198d3SRandall Stewart 					 * new_pseudo_cumack will trigger a
393118e198d3SRandall Stewart 					 * cwnd update.
393218e198d3SRandall Stewart 					 * find_(rtx_)pseudo_cumack will
393318e198d3SRandall Stewart 					 * trigger search for the next
393418e198d3SRandall Stewart 					 * expected (rtx-)pseudo-cumack.
3935132dea7dSRandall Stewart 					 */
3936132dea7dSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
3937132dea7dSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
3938132dea7dSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3939132dea7dSRandall Stewart 
3940b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
394104ee05e8SRandall Stewart 						/* sa_ignore NO_NULL_CHK */
3942f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
394380fefe0aSRandall Stewart 					}
3944f8829a4aSRandall Stewart 				}
3945f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3946f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3947f8829a4aSRandall Stewart 				}
394842551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
394942551e99SRandall Stewart 					/* deflate the cwnd */
395042551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
395142551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
395242551e99SRandall Stewart 				}
3953f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
3954f8829a4aSRandall Stewart 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3955f8829a4aSRandall Stewart 				if (tp1->data) {
395604ee05e8SRandall Stewart 					/* sa_ignore NO_NULL_CHK */
3957f8829a4aSRandall Stewart 					sctp_free_bufspace(stcb, asoc, tp1, 1);
3958f8829a4aSRandall Stewart 					sctp_m_freem(tp1->data);
39594a9ef3f8SMichael Tuexen 					tp1->data = NULL;
3960f8829a4aSRandall Stewart 				}
3961b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3962f8829a4aSRandall Stewart 					sctp_log_sack(asoc->last_acked_seq,
3963f8829a4aSRandall Stewart 					    cumack,
3964f8829a4aSRandall Stewart 					    tp1->rec.data.TSN_seq,
3965f8829a4aSRandall Stewart 					    0,
3966f8829a4aSRandall Stewart 					    0,
3967f8829a4aSRandall Stewart 					    SCTP_LOG_FREE_SENT);
396880fefe0aSRandall Stewart 				}
3969f8829a4aSRandall Stewart 				asoc->sent_queue_cnt--;
3970689e6a5fSMichael Tuexen 				sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
397118e198d3SRandall Stewart 			} else {
397218e198d3SRandall Stewart 				break;
3973f8829a4aSRandall Stewart 			}
39745e54f665SRandall Stewart 		}
397518e198d3SRandall Stewart 
397618e198d3SRandall Stewart 	}
397704ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
3978f8829a4aSRandall Stewart 	if (stcb->sctp_socket) {
3979ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3980ceaad40aSRandall Stewart 		struct socket *so;
3981ceaad40aSRandall Stewart 
3982ceaad40aSRandall Stewart #endif
3983f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
3984b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
398504ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
39867215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_WAKESND_FROM_SACK);
398780fefe0aSRandall Stewart 		}
3988ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3989ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
3990ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
3991ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
3992ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
3993ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
3994ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
3995ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3996ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
3997ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
3998ceaad40aSRandall Stewart 			return;
3999ceaad40aSRandall Stewart 		}
4000ceaad40aSRandall Stewart #endif
4001f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4002ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4003ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4004ceaad40aSRandall Stewart #endif
4005f8829a4aSRandall Stewart 	} else {
4006b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
40077215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, 1, SCTP_NOWAKE_FROM_SACK);
400880fefe0aSRandall Stewart 		}
4009f8829a4aSRandall Stewart 	}
4010f8829a4aSRandall Stewart 
4011b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4012ca85e948SMichael Tuexen 	if ((asoc->last_acked_seq != cumack) && (ecne_seen == 0)) {
4013ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4014ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4015ca85e948SMichael Tuexen 				/*
4016ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4017ca85e948SMichael Tuexen 				 * count, this is optional.
4018ca85e948SMichael Tuexen 				 */
4019ca85e948SMichael Tuexen 				net->error_count = 0;
4020ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4021ca85e948SMichael Tuexen 					/* addr came good */
4022ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4023ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
40244b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4025ca85e948SMichael Tuexen 				}
4026ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4027ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4028ca85e948SMichael Tuexen 						/*
4029ca85e948SMichael Tuexen 						 * release the alternate,
4030ca85e948SMichael Tuexen 						 * primary is good
4031ca85e948SMichael Tuexen 						 */
4032ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4033ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4034ca85e948SMichael Tuexen 					}
4035ca85e948SMichael Tuexen 				}
4036ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4037ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4038ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4039ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4040ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4041ca85e948SMichael Tuexen 					/* Done with this net */
4042ca85e948SMichael Tuexen 					net->net_ack = 0;
4043ca85e948SMichael Tuexen 				}
4044ca85e948SMichael Tuexen 				/* restore any doubled timers */
4045ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4046ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4047ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4048ca85e948SMichael Tuexen 				}
4049ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4050ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4051ca85e948SMichael Tuexen 				}
4052ca85e948SMichael Tuexen 			}
4053ca85e948SMichael Tuexen 		}
4054b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4055ca85e948SMichael Tuexen 	}
4056f8829a4aSRandall Stewart 	asoc->last_acked_seq = cumack;
40575e54f665SRandall Stewart 
4058f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4059f8829a4aSRandall Stewart 		/* nothing left in-flight */
4060f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4061f8829a4aSRandall Stewart 			net->flight_size = 0;
4062f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4063f8829a4aSRandall Stewart 		}
4064f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4065f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4066f8829a4aSRandall Stewart 	}
4067f8829a4aSRandall Stewart 	/* RWND update */
4068f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
406944fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4070f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4071f8829a4aSRandall Stewart 		/* SWS sender side engages */
4072f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
4073f8829a4aSRandall Stewart 	}
40745e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
40755e54f665SRandall Stewart 		win_probe_recovery = 1;
40765e54f665SRandall Stewart 	}
4077f8829a4aSRandall Stewart 	/* Now assure a timer where data is queued at */
4078a5d547adSRandall Stewart again:
4079a5d547adSRandall Stewart 	j = 0;
4080f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
40815171328bSRandall Stewart 		int to_ticks;
40825171328bSRandall Stewart 
40835e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
4084c105859eSRandall Stewart 			win_probe_recovered = 1;
40855e54f665SRandall Stewart 			/*
40865e54f665SRandall Stewart 			 * Find first chunk that was used with window probe
40875e54f665SRandall Stewart 			 * and clear the sent
40885e54f665SRandall Stewart 			 */
40893c503c28SRandall Stewart 			/* sa_ignore FREED_MEMORY */
40905e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
40915e54f665SRandall Stewart 				if (tp1->window_probe) {
4092cd554309SMichael Tuexen 					/* move back to data send queue */
40937215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
40945e54f665SRandall Stewart 					break;
40955e54f665SRandall Stewart 				}
40965e54f665SRandall Stewart 			}
40975e54f665SRandall Stewart 		}
4098f8829a4aSRandall Stewart 		if (net->RTO == 0) {
4099f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4100f8829a4aSRandall Stewart 		} else {
4101f8829a4aSRandall Stewart 			to_ticks = MSEC_TO_TICKS(net->RTO);
4102f8829a4aSRandall Stewart 		}
41035171328bSRandall Stewart 		if (net->flight_size) {
4104a5d547adSRandall Stewart 			j++;
4105ad81507eSRandall Stewart 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4106f8829a4aSRandall Stewart 			    sctp_timeout_handler, &net->rxt_timer);
41075171328bSRandall Stewart 			if (net->window_probe) {
41085171328bSRandall Stewart 				net->window_probe = 0;
41095171328bSRandall Stewart 			}
4110f8829a4aSRandall Stewart 		} else {
41115171328bSRandall Stewart 			if (net->window_probe) {
41125171328bSRandall Stewart 				/*
41135171328bSRandall Stewart 				 * In window probes we must assure a timer
41145171328bSRandall Stewart 				 * is still running there
41155171328bSRandall Stewart 				 */
41165171328bSRandall Stewart 				net->window_probe = 0;
41175171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
41185171328bSRandall Stewart 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
41195171328bSRandall Stewart 					    sctp_timeout_handler, &net->rxt_timer);
41205171328bSRandall Stewart 				}
41215171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4122f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4123a5d547adSRandall Stewart 				    stcb, net,
4124a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4125f8829a4aSRandall Stewart 			}
4126f8829a4aSRandall Stewart 		}
4127f8829a4aSRandall Stewart 	}
4128bff64a4dSRandall Stewart 	if ((j == 0) &&
4129bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4130bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
4131c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
4132bff64a4dSRandall Stewart 	    (done_once == 0)) {
41330c0982b8SRandall Stewart 		/*
41340c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
41350c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
41360c0982b8SRandall Stewart 		 */
41370c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
4138a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4139a5d547adSRandall Stewart 				net->flight_size = 0;
4140a5d547adSRandall Stewart 			}
4141a5d547adSRandall Stewart 			asoc->total_flight = 0;
4142a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
4143a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4144a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4145a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4146c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
4147c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
4148a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4149791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
4150a5d547adSRandall Stewart 				}
4151a5d547adSRandall Stewart 			}
41520c0982b8SRandall Stewart 		}
4153bff64a4dSRandall Stewart 		done_once = 1;
4154a5d547adSRandall Stewart 		goto again;
4155a5d547adSRandall Stewart 	}
4156f8829a4aSRandall Stewart 	/**********************************/
4157f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4158f8829a4aSRandall Stewart 	/**********************************/
4159f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4160f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4161f8829a4aSRandall Stewart 		/* clean up */
4162f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4163f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4164f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4165f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4166f8829a4aSRandall Stewart 		    ) {
4167f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4168f8829a4aSRandall Stewart 
4169f8829a4aSRandall Stewart 			/*
4170f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4171f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4172f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4173f8829a4aSRandall Stewart 			 * The sp will be cleaned during free of the asoc.
4174f8829a4aSRandall Stewart 			 */
4175f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4176f8829a4aSRandall Stewart 			    sctp_streamhead);
41772afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
41782afb3e84SRandall Stewart 				/* Let cleanup code purge it */
41792afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
41802afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
41812afb3e84SRandall Stewart 				} else {
4182f8829a4aSRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4183f8829a4aSRandall Stewart 					asoc->locked_on_sending = NULL;
4184f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
4185f8829a4aSRandall Stewart 				}
4186f8829a4aSRandall Stewart 			}
41872afb3e84SRandall Stewart 		}
4188f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4189f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4190f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4191f8829a4aSRandall Stewart 				/* Need to abort here */
4192f8829a4aSRandall Stewart 				struct mbuf *oper;
4193f8829a4aSRandall Stewart 
4194f8829a4aSRandall Stewart 		abort_out_now:
4195f8829a4aSRandall Stewart 				*abort_now = 1;
4196f8829a4aSRandall Stewart 				/* XXX */
4197f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4198f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4199f8829a4aSRandall Stewart 				if (oper) {
4200f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4201f8829a4aSRandall Stewart 					uint32_t *ippp;
4202f8829a4aSRandall Stewart 
4203139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4204f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4205f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4206f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4207139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4208f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4209a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4210f8829a4aSRandall Stewart 				}
4211a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4212a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
4213f8829a4aSRandall Stewart 			} else {
4214ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4215ca85e948SMichael Tuexen 
4216f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4217f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4218f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4219f42a358aSRandall Stewart 				}
4220c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4221b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4222f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4223ca85e948SMichael Tuexen 				if (asoc->alternate) {
4224ca85e948SMichael Tuexen 					netp = asoc->alternate;
4225ca85e948SMichael Tuexen 				} else {
4226ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4227ca85e948SMichael Tuexen 				}
4228ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4229f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4230ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4231f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4232ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4233f8829a4aSRandall Stewart 			}
4234f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4235f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4236ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4237ca85e948SMichael Tuexen 
4238ca85e948SMichael Tuexen 			if (asoc->alternate) {
4239ca85e948SMichael Tuexen 				netp = asoc->alternate;
4240ca85e948SMichael Tuexen 			} else {
4241ca85e948SMichael Tuexen 				netp = asoc->primary_destination;
4242ca85e948SMichael Tuexen 			}
4243f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4244f8829a4aSRandall Stewart 				goto abort_out_now;
4245f8829a4aSRandall Stewart 			}
4246f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4247c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4248b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4249ca85e948SMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
425012af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4251f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4252ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4253f8829a4aSRandall Stewart 		}
4254f8829a4aSRandall Stewart 	}
4255dfb11ef8SRandall Stewart 	/*********************************************/
4256dfb11ef8SRandall Stewart 	/* Here we perform PR-SCTP procedures        */
4257dfb11ef8SRandall Stewart 	/* (section 4.2)                             */
4258dfb11ef8SRandall Stewart 	/*********************************************/
4259dfb11ef8SRandall Stewart 	/* C1. update advancedPeerAckPoint */
426020b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cumack, asoc->advanced_peer_ack_point)) {
4261dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cumack;
4262dfb11ef8SRandall Stewart 	}
4263830d754dSRandall Stewart 	/* PR-Sctp issues need to be addressed too */
4264830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4265830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
4266830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
4267830d754dSRandall Stewart 
4268830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4269830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4270830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
427120b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cumack)) {
4272830d754dSRandall Stewart 			/*
4273493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
4274830d754dSRandall Stewart 			 */
427520b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
4276830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
42770c0982b8SRandall Stewart 			} else if (lchk) {
42780c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
427944fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
42800c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
42810c0982b8SRandall Stewart 				}
4282830d754dSRandall Stewart 			}
4283830d754dSRandall Stewart 		}
4284830d754dSRandall Stewart 		if (lchk) {
4285830d754dSRandall Stewart 			/* Assure a timer is up */
4286830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4287830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
4288830d754dSRandall Stewart 		}
4289830d754dSRandall Stewart 	}
4290b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4291f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4292f8829a4aSRandall Stewart 		    rwnd,
4293f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
4294f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
4295f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
429680fefe0aSRandall Stewart 	}
4297f8829a4aSRandall Stewart }
4298f8829a4aSRandall Stewart 
4299f8829a4aSRandall Stewart void
4300cd554309SMichael Tuexen sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
43017215cc1bSMichael Tuexen     struct sctp_tcb *stcb,
4302cd554309SMichael Tuexen     uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
4303cd554309SMichael Tuexen     int *abort_now, uint8_t flags,
4304899288aeSRandall Stewart     uint32_t cum_ack, uint32_t rwnd, int ecne_seen)
4305f8829a4aSRandall Stewart {
4306f8829a4aSRandall Stewart 	struct sctp_association *asoc;
4307f8829a4aSRandall Stewart 	struct sctp_tmit_chunk *tp1, *tp2;
4308cd554309SMichael Tuexen 	uint32_t last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked, this_sack_lowest_newack;
4309f8829a4aSRandall Stewart 	uint16_t wake_him = 0;
4310c105859eSRandall Stewart 	uint32_t send_s = 0;
4311f8829a4aSRandall Stewart 	long j;
4312f8829a4aSRandall Stewart 	int accum_moved = 0;
4313f8829a4aSRandall Stewart 	int will_exit_fast_recovery = 0;
43145e54f665SRandall Stewart 	uint32_t a_rwnd, old_rwnd;
43155e54f665SRandall Stewart 	int win_probe_recovery = 0;
4316c105859eSRandall Stewart 	int win_probe_recovered = 0;
4317f8829a4aSRandall Stewart 	struct sctp_nets *net = NULL;
4318bff64a4dSRandall Stewart 	int done_once;
4319f79aab18SRandall Stewart 	int rto_ok = 1;
4320f8829a4aSRandall Stewart 	uint8_t reneged_all = 0;
4321f8829a4aSRandall Stewart 	uint8_t cmt_dac_flag;
4322f8829a4aSRandall Stewart 
4323f8829a4aSRandall Stewart 	/*
4324f8829a4aSRandall Stewart 	 * we take any chance we can to service our queues since we cannot
4325f8829a4aSRandall Stewart 	 * get awoken when the socket is read from :<
4326f8829a4aSRandall Stewart 	 */
4327f8829a4aSRandall Stewart 	/*
4328f8829a4aSRandall Stewart 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4329f8829a4aSRandall Stewart 	 * old sack, if so discard. 2) If there is nothing left in the send
4330f8829a4aSRandall Stewart 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4331f8829a4aSRandall Stewart 	 * too, update any rwnd change and verify no timers are running.
4332f8829a4aSRandall Stewart 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4333f8829a4aSRandall Stewart 	 * moved process these first and note that it moved. 4) Process any
4334f8829a4aSRandall Stewart 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4335f8829a4aSRandall Stewart 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4336f8829a4aSRandall Stewart 	 * sync up flightsizes and things, stop all timers and also check
4337f8829a4aSRandall Stewart 	 * for shutdown_pending state. If so then go ahead and send off the
4338f8829a4aSRandall Stewart 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4339f8829a4aSRandall Stewart 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4340f8829a4aSRandall Stewart 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4341f8829a4aSRandall Stewart 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4342f8829a4aSRandall Stewart 	 * if in shutdown_recv state.
4343f8829a4aSRandall Stewart 	 */
4344f8829a4aSRandall Stewart 	SCTP_TCB_LOCK_ASSERT(stcb);
4345f8829a4aSRandall Stewart 	/* CMT DAC algo */
4346f8829a4aSRandall Stewart 	this_sack_lowest_newack = 0;
4347f8829a4aSRandall Stewart 	SCTP_STAT_INCR(sctps_slowpath_sack);
4348cd554309SMichael Tuexen 	last_tsn = cum_ack;
4349cd554309SMichael Tuexen 	cmt_dac_flag = flags & SCTP_SACK_CMT_DAC;
435018e198d3SRandall Stewart #ifdef SCTP_ASOCLOG_OF_TSNS
435118e198d3SRandall Stewart 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
435218e198d3SRandall Stewart 	stcb->asoc.cumack_log_at++;
435318e198d3SRandall Stewart 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
435418e198d3SRandall Stewart 		stcb->asoc.cumack_log_at = 0;
435518e198d3SRandall Stewart 	}
435618e198d3SRandall Stewart #endif
4357d06c82f1SRandall Stewart 	a_rwnd = rwnd;
4358f8829a4aSRandall Stewart 
4359cd554309SMichael Tuexen 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4360cd554309SMichael Tuexen 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4361cd554309SMichael Tuexen 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4362cd554309SMichael Tuexen 	}
43635e54f665SRandall Stewart 	old_rwnd = stcb->asoc.peers_rwnd;
4364b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4365c4739e2fSRandall Stewart 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4366c4739e2fSRandall Stewart 		    stcb->asoc.overall_error_count,
4367c4739e2fSRandall Stewart 		    0,
4368c4739e2fSRandall Stewart 		    SCTP_FROM_SCTP_INDATA,
4369c4739e2fSRandall Stewart 		    __LINE__);
4370c4739e2fSRandall Stewart 	}
4371f8829a4aSRandall Stewart 	stcb->asoc.overall_error_count = 0;
4372f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
4373b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4374f8829a4aSRandall Stewart 		sctp_log_sack(asoc->last_acked_seq,
4375f8829a4aSRandall Stewart 		    cum_ack,
4376f8829a4aSRandall Stewart 		    0,
4377f8829a4aSRandall Stewart 		    num_seg,
4378f8829a4aSRandall Stewart 		    num_dup,
4379f8829a4aSRandall Stewart 		    SCTP_LOG_NEW_SACK);
438080fefe0aSRandall Stewart 	}
4381ca85e948SMichael Tuexen 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE)) {
4382cd554309SMichael Tuexen 		uint16_t i;
4383458303daSRandall Stewart 		uint32_t *dupdata, dblock;
4384f8829a4aSRandall Stewart 
4385cd554309SMichael Tuexen 		for (i = 0; i < num_dup; i++) {
4386cd554309SMichael Tuexen 			dupdata = (uint32_t *) sctp_m_getptr(m, offset_dup + i * sizeof(uint32_t),
4387458303daSRandall Stewart 			    sizeof(uint32_t), (uint8_t *) & dblock);
4388cd554309SMichael Tuexen 			if (dupdata == NULL) {
4389458303daSRandall Stewart 				break;
4390458303daSRandall Stewart 			}
4391cd554309SMichael Tuexen 			sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4392f8829a4aSRandall Stewart 		}
4393f8829a4aSRandall Stewart 	}
4394b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4395c105859eSRandall Stewart 		/* reality check */
4396c105859eSRandall Stewart 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4397c105859eSRandall Stewart 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4398c105859eSRandall Stewart 			    sctpchunk_listhead);
4399c105859eSRandall Stewart 			send_s = tp1->rec.data.TSN_seq + 1;
4400c105859eSRandall Stewart 		} else {
4401b5c16493SMichael Tuexen 			tp1 = NULL;
4402c105859eSRandall Stewart 			send_s = asoc->sending_seq;
4403c105859eSRandall Stewart 		}
440420b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(cum_ack, send_s)) {
4405c105859eSRandall Stewart 			struct mbuf *oper;
4406c105859eSRandall Stewart 
4407f8829a4aSRandall Stewart 			/*
4408f8829a4aSRandall Stewart 			 * no way, we have not even sent this TSN out yet.
4409f8829a4aSRandall Stewart 			 * Peer is hopelessly messed up with us.
4410f8829a4aSRandall Stewart 			 */
4411cd3fd531SMichael Tuexen 			SCTP_PRINTF("NEW cum_ack:%x send_s:%x is smaller or equal\n",
4412b5c16493SMichael Tuexen 			    cum_ack, send_s);
4413b5c16493SMichael Tuexen 			if (tp1) {
4414cd3fd531SMichael Tuexen 				SCTP_PRINTF("Got send_s from tsn:%x + 1 of tp1:%p\n",
4415b5c16493SMichael Tuexen 				    tp1->rec.data.TSN_seq, tp1);
4416b5c16493SMichael Tuexen 			}
4417f8829a4aSRandall Stewart 	hopeless_peer:
4418f8829a4aSRandall Stewart 			*abort_now = 1;
4419f8829a4aSRandall Stewart 			/* XXX */
4420f8829a4aSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4421f8829a4aSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
4422f8829a4aSRandall Stewart 			if (oper) {
4423f8829a4aSRandall Stewart 				struct sctp_paramhdr *ph;
4424f8829a4aSRandall Stewart 				uint32_t *ippp;
4425f8829a4aSRandall Stewart 
4426139bc87fSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4427f8829a4aSRandall Stewart 				    sizeof(uint32_t);
4428f8829a4aSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
4429f8829a4aSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4430139bc87fSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4431f8829a4aSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
4432a5d547adSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4433f8829a4aSRandall Stewart 			}
4434a5d547adSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4435a2b42326SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
4436f8829a4aSRandall Stewart 			return;
4437f8829a4aSRandall Stewart 		}
4438f8829a4aSRandall Stewart 	}
4439f8829a4aSRandall Stewart 	/**********************/
4440f8829a4aSRandall Stewart 	/* 1) check the range */
4441f8829a4aSRandall Stewart 	/**********************/
444220b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(asoc->last_acked_seq, last_tsn)) {
4443f8829a4aSRandall Stewart 		/* acking something behind */
4444f8829a4aSRandall Stewart 		return;
4445f8829a4aSRandall Stewart 	}
4446f8829a4aSRandall Stewart 	/* update the Rwnd of the peer */
4447f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4448f8829a4aSRandall Stewart 	    TAILQ_EMPTY(&asoc->send_queue) &&
4449cd554309SMichael Tuexen 	    (asoc->stream_queue_cnt == 0)) {
4450f8829a4aSRandall Stewart 		/* nothing left on send/sent and strmq */
4451b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4452f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4453f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
445480fefe0aSRandall Stewart 		}
4455f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4456f8829a4aSRandall Stewart 		if (asoc->sent_queue_retran_cnt) {
4457f8829a4aSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
4458f8829a4aSRandall Stewart 		}
4459f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4460f8829a4aSRandall Stewart 			/* SWS sender side engages */
4461f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4462f8829a4aSRandall Stewart 		}
4463f8829a4aSRandall Stewart 		/* stop any timers */
4464f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4465f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4466a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4467f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4468f8829a4aSRandall Stewart 			net->flight_size = 0;
4469f8829a4aSRandall Stewart 		}
4470f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4471f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4472f8829a4aSRandall Stewart 		return;
4473f8829a4aSRandall Stewart 	}
4474f8829a4aSRandall Stewart 	/*
4475f8829a4aSRandall Stewart 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4476f8829a4aSRandall Stewart 	 * things. The total byte count acked is tracked in netAckSz AND
4477f8829a4aSRandall Stewart 	 * netAck2 is used to track the total bytes acked that are un-
4478f8829a4aSRandall Stewart 	 * amibguious and were never retransmitted. We track these on a per
4479f8829a4aSRandall Stewart 	 * destination address basis.
4480f8829a4aSRandall Stewart 	 */
4481f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4482a21779f0SRandall Stewart 		if (SCTP_TSN_GT(cum_ack, net->cwr_window_tsn)) {
4483a21779f0SRandall Stewart 			/* Drag along the window_tsn for cwr's */
4484a21779f0SRandall Stewart 			net->cwr_window_tsn = cum_ack;
4485a21779f0SRandall Stewart 		}
4486f8829a4aSRandall Stewart 		net->prev_cwnd = net->cwnd;
4487f8829a4aSRandall Stewart 		net->net_ack = 0;
4488f8829a4aSRandall Stewart 		net->net_ack2 = 0;
4489f8829a4aSRandall Stewart 
4490f8829a4aSRandall Stewart 		/*
449142551e99SRandall Stewart 		 * CMT: Reset CUC and Fast recovery algo variables before
449242551e99SRandall Stewart 		 * SACK processing
4493f8829a4aSRandall Stewart 		 */
4494f8829a4aSRandall Stewart 		net->new_pseudo_cumack = 0;
4495f8829a4aSRandall Stewart 		net->will_exit_fast_recovery = 0;
4496299108c5SRandall Stewart 		if (stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) {
4497299108c5SRandall Stewart 			(*stcb->asoc.cc_functions.sctp_cwnd_prepare_net_for_sack) (stcb, net);
4498299108c5SRandall Stewart 		}
4499f8829a4aSRandall Stewart 	}
4500f8829a4aSRandall Stewart 	/* process the new consecutive TSN first */
45014a9ef3f8SMichael Tuexen 	TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
450220b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(last_tsn, tp1->rec.data.TSN_seq)) {
4503f8829a4aSRandall Stewart 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4504f8829a4aSRandall Stewart 				accum_moved = 1;
4505f8829a4aSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4506f8829a4aSRandall Stewart 					/*
4507f8829a4aSRandall Stewart 					 * If it is less than ACKED, it is
4508f8829a4aSRandall Stewart 					 * now no-longer in flight. Higher
4509f8829a4aSRandall Stewart 					 * values may occur during marking
4510f8829a4aSRandall Stewart 					 */
4511f8829a4aSRandall Stewart 					if ((tp1->whoTo->dest_state &
4512f8829a4aSRandall Stewart 					    SCTP_ADDR_UNCONFIRMED) &&
4513f8829a4aSRandall Stewart 					    (tp1->snd_count < 2)) {
4514f8829a4aSRandall Stewart 						/*
4515f8829a4aSRandall Stewart 						 * If there was no retran
4516f8829a4aSRandall Stewart 						 * and the address is
4517f8829a4aSRandall Stewart 						 * un-confirmed and we sent
4518f8829a4aSRandall Stewart 						 * there and are now
4519f8829a4aSRandall Stewart 						 * sacked.. its confirmed,
4520f8829a4aSRandall Stewart 						 * mark it so.
4521f8829a4aSRandall Stewart 						 */
4522f8829a4aSRandall Stewart 						tp1->whoTo->dest_state &=
4523f8829a4aSRandall Stewart 						    ~SCTP_ADDR_UNCONFIRMED;
4524f8829a4aSRandall Stewart 					}
4525c105859eSRandall Stewart 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4526b3f1ea41SRandall Stewart 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4527c105859eSRandall Stewart 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4528a5d547adSRandall Stewart 							    tp1->whoTo->flight_size,
4529a5d547adSRandall Stewart 							    tp1->book_size,
4530c105859eSRandall Stewart 							    (uintptr_t) tp1->whoTo,
4531a5d547adSRandall Stewart 							    tp1->rec.data.TSN_seq);
453280fefe0aSRandall Stewart 						}
4533c105859eSRandall Stewart 						sctp_flight_size_decrease(tp1);
4534c105859eSRandall Stewart 						sctp_total_flight_decrease(stcb, tp1);
4535299108c5SRandall Stewart 						if (stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) {
4536299108c5SRandall Stewart 							(*stcb->asoc.cc_functions.sctp_cwnd_update_tsn_acknowledged) (tp1->whoTo,
4537299108c5SRandall Stewart 							    tp1);
4538299108c5SRandall Stewart 						}
4539f8829a4aSRandall Stewart 					}
4540f8829a4aSRandall Stewart 					tp1->whoTo->net_ack += tp1->send_size;
4541f8829a4aSRandall Stewart 
4542f8829a4aSRandall Stewart 					/* CMT SFR and DAC algos */
4543f8829a4aSRandall Stewart 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4544f8829a4aSRandall Stewart 					tp1->whoTo->saw_newack = 1;
4545f8829a4aSRandall Stewart 
4546f8829a4aSRandall Stewart 					if (tp1->snd_count < 2) {
4547f8829a4aSRandall Stewart 						/*
4548f8829a4aSRandall Stewart 						 * True non-retransmited
4549f8829a4aSRandall Stewart 						 * chunk
4550f8829a4aSRandall Stewart 						 */
4551f8829a4aSRandall Stewart 						tp1->whoTo->net_ack2 +=
4552f8829a4aSRandall Stewart 						    tp1->send_size;
4553f8829a4aSRandall Stewart 
4554f8829a4aSRandall Stewart 						/* update RTO too? */
4555f8829a4aSRandall Stewart 						if (tp1->do_rtt) {
4556f79aab18SRandall Stewart 							if (rto_ok) {
4557f8829a4aSRandall Stewart 								tp1->whoTo->RTO =
4558f8829a4aSRandall Stewart 								    sctp_calculate_rto(stcb,
4559f8829a4aSRandall Stewart 								    asoc, tp1->whoTo,
456018e198d3SRandall Stewart 								    &tp1->sent_rcv_time,
4561899288aeSRandall Stewart 								    sctp_align_safe_nocopy,
4562f79aab18SRandall Stewart 								    SCTP_RTT_FROM_DATA);
4563f79aab18SRandall Stewart 								rto_ok = 0;
4564f79aab18SRandall Stewart 							}
4565f79aab18SRandall Stewart 							if (tp1->whoTo->rto_needed == 0) {
4566f79aab18SRandall Stewart 								tp1->whoTo->rto_needed = 1;
4567f79aab18SRandall Stewart 							}
4568f8829a4aSRandall Stewart 							tp1->do_rtt = 0;
4569f8829a4aSRandall Stewart 						}
4570f8829a4aSRandall Stewart 					}
4571f8829a4aSRandall Stewart 					/*
4572f8829a4aSRandall Stewart 					 * CMT: CUCv2 algorithm. From the
4573f8829a4aSRandall Stewart 					 * cumack'd TSNs, for each TSN being
4574f8829a4aSRandall Stewart 					 * acked for the first time, set the
4575f8829a4aSRandall Stewart 					 * following variables for the
4576f8829a4aSRandall Stewart 					 * corresp destination.
4577f8829a4aSRandall Stewart 					 * new_pseudo_cumack will trigger a
4578f8829a4aSRandall Stewart 					 * cwnd update.
4579f8829a4aSRandall Stewart 					 * find_(rtx_)pseudo_cumack will
4580f8829a4aSRandall Stewart 					 * trigger search for the next
4581f8829a4aSRandall Stewart 					 * expected (rtx-)pseudo-cumack.
4582f8829a4aSRandall Stewart 					 */
4583f8829a4aSRandall Stewart 					tp1->whoTo->new_pseudo_cumack = 1;
4584f8829a4aSRandall Stewart 					tp1->whoTo->find_pseudo_cumack = 1;
4585f8829a4aSRandall Stewart 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4586f8829a4aSRandall Stewart 
4587f8829a4aSRandall Stewart 
4588b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4589f8829a4aSRandall Stewart 						sctp_log_sack(asoc->last_acked_seq,
4590f8829a4aSRandall Stewart 						    cum_ack,
4591f8829a4aSRandall Stewart 						    tp1->rec.data.TSN_seq,
4592f8829a4aSRandall Stewart 						    0,
4593f8829a4aSRandall Stewart 						    0,
4594f8829a4aSRandall Stewart 						    SCTP_LOG_TSN_ACKED);
459580fefe0aSRandall Stewart 					}
4596b3f1ea41SRandall Stewart 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4597f8829a4aSRandall Stewart 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
459880fefe0aSRandall Stewart 					}
4599f8829a4aSRandall Stewart 				}
4600f8829a4aSRandall Stewart 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4601f8829a4aSRandall Stewart 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4602f8829a4aSRandall Stewart #ifdef SCTP_AUDITING_ENABLED
4603f8829a4aSRandall Stewart 					sctp_audit_log(0xB3,
4604f8829a4aSRandall Stewart 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4605f8829a4aSRandall Stewart #endif
4606f8829a4aSRandall Stewart 				}
460742551e99SRandall Stewart 				if (tp1->rec.data.chunk_was_revoked) {
460842551e99SRandall Stewart 					/* deflate the cwnd */
460942551e99SRandall Stewart 					tp1->whoTo->cwnd -= tp1->book_size;
461042551e99SRandall Stewart 					tp1->rec.data.chunk_was_revoked = 0;
461142551e99SRandall Stewart 				}
4612f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_ACKED;
4613f8829a4aSRandall Stewart 			}
4614f8829a4aSRandall Stewart 		} else {
4615f8829a4aSRandall Stewart 			break;
4616f8829a4aSRandall Stewart 		}
4617f8829a4aSRandall Stewart 	}
4618f8829a4aSRandall Stewart 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4619f8829a4aSRandall Stewart 	/* always set this up to cum-ack */
4620f8829a4aSRandall Stewart 	asoc->this_sack_highest_gap = last_tsn;
4621f8829a4aSRandall Stewart 
4622cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
4623f8829a4aSRandall Stewart 
4624f8829a4aSRandall Stewart 		/*
4625f8829a4aSRandall Stewart 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4626f8829a4aSRandall Stewart 		 * to be greater than the cumack. Also reset saw_newack to 0
4627f8829a4aSRandall Stewart 		 * for all dests.
4628f8829a4aSRandall Stewart 		 */
4629f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4630f8829a4aSRandall Stewart 			net->saw_newack = 0;
4631f8829a4aSRandall Stewart 			net->this_sack_highest_newack = last_tsn;
4632f8829a4aSRandall Stewart 		}
4633f8829a4aSRandall Stewart 
4634f8829a4aSRandall Stewart 		/*
4635f8829a4aSRandall Stewart 		 * thisSackHighestGap will increase while handling NEW
4636f8829a4aSRandall Stewart 		 * segments this_sack_highest_newack will increase while
4637f8829a4aSRandall Stewart 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4638f8829a4aSRandall Stewart 		 * used for CMT DAC algo. saw_newack will also change.
4639f8829a4aSRandall Stewart 		 */
4640cd554309SMichael Tuexen 		if (sctp_handle_segments(m, &offset_seg, stcb, asoc, last_tsn, &biggest_tsn_acked,
4641cd554309SMichael Tuexen 		    &biggest_tsn_newly_acked, &this_sack_lowest_newack,
46427215cc1bSMichael Tuexen 		    num_seg, num_nr_seg, &rto_ok)) {
4643cd554309SMichael Tuexen 			wake_him++;
4644cd554309SMichael Tuexen 		}
4645b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4646f8829a4aSRandall Stewart 			/*
4647f8829a4aSRandall Stewart 			 * validate the biggest_tsn_acked in the gap acks if
4648f8829a4aSRandall Stewart 			 * strict adherence is wanted.
4649f8829a4aSRandall Stewart 			 */
465020b07a4dSMichael Tuexen 			if (SCTP_TSN_GE(biggest_tsn_acked, send_s)) {
4651f8829a4aSRandall Stewart 				/*
4652f8829a4aSRandall Stewart 				 * peer is either confused or we are under
4653f8829a4aSRandall Stewart 				 * attack. We must abort.
4654f8829a4aSRandall Stewart 				 */
4655cd3fd531SMichael Tuexen 				SCTP_PRINTF("Hopeless peer! biggest_tsn_acked:%x largest seq:%x\n",
4656cd3fd531SMichael Tuexen 				    biggest_tsn_acked, send_s);
4657f8829a4aSRandall Stewart 				goto hopeless_peer;
4658f8829a4aSRandall Stewart 			}
4659f8829a4aSRandall Stewart 		}
4660f8829a4aSRandall Stewart 	}
4661f8829a4aSRandall Stewart 	/*******************************************/
4662f8829a4aSRandall Stewart 	/* cancel ALL T3-send timer if accum moved */
4663f8829a4aSRandall Stewart 	/*******************************************/
46647c99d56fSMichael Tuexen 	if (asoc->sctp_cmt_on_off > 0) {
4665f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4666f8829a4aSRandall Stewart 			if (net->new_pseudo_cumack)
4667f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4668a5d547adSRandall Stewart 				    stcb, net,
4669a5d547adSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4670f8829a4aSRandall Stewart 
4671f8829a4aSRandall Stewart 		}
4672f8829a4aSRandall Stewart 	} else {
4673f8829a4aSRandall Stewart 		if (accum_moved) {
4674f8829a4aSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4675f8829a4aSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4676a5d547adSRandall Stewart 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4677f8829a4aSRandall Stewart 			}
4678f8829a4aSRandall Stewart 		}
4679f8829a4aSRandall Stewart 	}
4680f8829a4aSRandall Stewart 	/********************************************/
4681d9c5cfeaSMichael Tuexen 	/* drop the acked chunks from the sentqueue */
4682f8829a4aSRandall Stewart 	/********************************************/
4683f8829a4aSRandall Stewart 	asoc->last_acked_seq = cum_ack;
4684f8829a4aSRandall Stewart 
46857c99d56fSMichael Tuexen 	TAILQ_FOREACH_SAFE(tp1, &asoc->sent_queue, sctp_next, tp2) {
468620b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, cum_ack)) {
4687f8829a4aSRandall Stewart 			break;
4688f8829a4aSRandall Stewart 		}
4689f8829a4aSRandall Stewart 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4690f8829a4aSRandall Stewart 			/* no more sent on list */
4691cd3fd531SMichael Tuexen 			SCTP_PRINTF("Warning, tp1->sent == %d and its now acked?\n",
469218e198d3SRandall Stewart 			    tp1->sent);
4693f8829a4aSRandall Stewart 		}
4694f8829a4aSRandall Stewart 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4695f8829a4aSRandall Stewart 		if (tp1->pr_sctp_on) {
4696f8829a4aSRandall Stewart 			if (asoc->pr_sctp_cnt != 0)
4697f8829a4aSRandall Stewart 				asoc->pr_sctp_cnt--;
4698f8829a4aSRandall Stewart 		}
46997c99d56fSMichael Tuexen 		asoc->sent_queue_cnt--;
4700f8829a4aSRandall Stewart 		if (tp1->data) {
470104ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
4702f8829a4aSRandall Stewart 			sctp_free_bufspace(stcb, asoc, tp1, 1);
4703f8829a4aSRandall Stewart 			sctp_m_freem(tp1->data);
47047c99d56fSMichael Tuexen 			tp1->data = NULL;
4705810ec536SMichael Tuexen 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
4706f8829a4aSRandall Stewart 				asoc->sent_queue_cnt_removeable--;
4707f8829a4aSRandall Stewart 			}
4708f8829a4aSRandall Stewart 		}
4709b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4710f8829a4aSRandall Stewart 			sctp_log_sack(asoc->last_acked_seq,
4711f8829a4aSRandall Stewart 			    cum_ack,
4712f8829a4aSRandall Stewart 			    tp1->rec.data.TSN_seq,
4713f8829a4aSRandall Stewart 			    0,
4714f8829a4aSRandall Stewart 			    0,
4715f8829a4aSRandall Stewart 			    SCTP_LOG_FREE_SENT);
471680fefe0aSRandall Stewart 		}
4717689e6a5fSMichael Tuexen 		sctp_free_a_chunk(stcb, tp1, SCTP_SO_NOT_LOCKED);
4718f8829a4aSRandall Stewart 		wake_him++;
47197c99d56fSMichael Tuexen 	}
47207c99d56fSMichael Tuexen 	if (TAILQ_EMPTY(&asoc->sent_queue) && (asoc->total_flight > 0)) {
47217c99d56fSMichael Tuexen #ifdef INVARIANTS
47227c99d56fSMichael Tuexen 		panic("Warning flight size is postive and should be 0");
47237c99d56fSMichael Tuexen #else
47247c99d56fSMichael Tuexen 		SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
47257c99d56fSMichael Tuexen 		    asoc->total_flight);
47267c99d56fSMichael Tuexen #endif
47277c99d56fSMichael Tuexen 		asoc->total_flight = 0;
47287c99d56fSMichael Tuexen 	}
472904ee05e8SRandall Stewart 	/* sa_ignore NO_NULL_CHK */
4730f8829a4aSRandall Stewart 	if ((wake_him) && (stcb->sctp_socket)) {
4731ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4732ceaad40aSRandall Stewart 		struct socket *so;
4733ceaad40aSRandall Stewart 
4734ceaad40aSRandall Stewart #endif
4735f8829a4aSRandall Stewart 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4736b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47377215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_WAKESND_FROM_SACK);
473880fefe0aSRandall Stewart 		}
4739ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4740ceaad40aSRandall Stewart 		so = SCTP_INP_SO(stcb->sctp_ep);
4741ceaad40aSRandall Stewart 		atomic_add_int(&stcb->asoc.refcnt, 1);
4742ceaad40aSRandall Stewart 		SCTP_TCB_UNLOCK(stcb);
4743ceaad40aSRandall Stewart 		SCTP_SOCKET_LOCK(so, 1);
4744ceaad40aSRandall Stewart 		SCTP_TCB_LOCK(stcb);
4745ceaad40aSRandall Stewart 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4746ceaad40aSRandall Stewart 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4747ceaad40aSRandall Stewart 			/* assoc was freed while we were unlocked */
4748ceaad40aSRandall Stewart 			SCTP_SOCKET_UNLOCK(so, 1);
4749ceaad40aSRandall Stewart 			return;
4750ceaad40aSRandall Stewart 		}
4751ceaad40aSRandall Stewart #endif
4752f8829a4aSRandall Stewart 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4753ceaad40aSRandall Stewart #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4754ceaad40aSRandall Stewart 		SCTP_SOCKET_UNLOCK(so, 1);
4755ceaad40aSRandall Stewart #endif
4756f8829a4aSRandall Stewart 	} else {
4757b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
47587215cc1bSMichael Tuexen 			sctp_wakeup_log(stcb, wake_him, SCTP_NOWAKE_FROM_SACK);
475980fefe0aSRandall Stewart 		}
4760f8829a4aSRandall Stewart 	}
4761f8829a4aSRandall Stewart 
476242551e99SRandall Stewart 	if (asoc->fast_retran_loss_recovery && accum_moved) {
476320b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(asoc->last_acked_seq, asoc->fast_recovery_tsn)) {
4764f8829a4aSRandall Stewart 			/* Setup so we will exit RFC2582 fast recovery */
4765f8829a4aSRandall Stewart 			will_exit_fast_recovery = 1;
4766f8829a4aSRandall Stewart 		}
4767f8829a4aSRandall Stewart 	}
4768f8829a4aSRandall Stewart 	/*
4769f8829a4aSRandall Stewart 	 * Check for revoked fragments:
4770f8829a4aSRandall Stewart 	 *
4771f8829a4aSRandall Stewart 	 * if Previous sack - Had no frags then we can't have any revoked if
4772f8829a4aSRandall Stewart 	 * Previous sack - Had frag's then - If we now have frags aka
4773f8829a4aSRandall Stewart 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4774f8829a4aSRandall Stewart 	 * some of them. else - The peer revoked all ACKED fragments, since
4775f8829a4aSRandall Stewart 	 * we had some before and now we have NONE.
4776f8829a4aSRandall Stewart 	 */
4777f8829a4aSRandall Stewart 
4778d9c5cfeaSMichael Tuexen 	if (num_seg) {
4779c105859eSRandall Stewart 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4780d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_frags = 1;
4781d9c5cfeaSMichael Tuexen 	} else if (asoc->saw_sack_with_frags) {
4782f8829a4aSRandall Stewart 		int cnt_revoked = 0;
4783f8829a4aSRandall Stewart 
4784f8829a4aSRandall Stewart 		/* Peer revoked all dg's marked or acked */
4785f8829a4aSRandall Stewart 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4786b5c16493SMichael Tuexen 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
4787f8829a4aSRandall Stewart 				tp1->sent = SCTP_DATAGRAM_SENT;
4788b3f1ea41SRandall Stewart 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4789c105859eSRandall Stewart 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4790c105859eSRandall Stewart 					    tp1->whoTo->flight_size,
4791c105859eSRandall Stewart 					    tp1->book_size,
4792c105859eSRandall Stewart 					    (uintptr_t) tp1->whoTo,
4793c105859eSRandall Stewart 					    tp1->rec.data.TSN_seq);
479480fefe0aSRandall Stewart 				}
4795c105859eSRandall Stewart 				sctp_flight_size_increase(tp1);
4796c105859eSRandall Stewart 				sctp_total_flight_increase(stcb, tp1);
4797a5d547adSRandall Stewart 				tp1->rec.data.chunk_was_revoked = 1;
479842551e99SRandall Stewart 				/*
479942551e99SRandall Stewart 				 * To ensure that this increase in
48004a9ef3f8SMichael Tuexen 				 * flightsize, which is artificial, does not
48014a9ef3f8SMichael Tuexen 				 * throttle the sender, we also increase the
48024a9ef3f8SMichael Tuexen 				 * cwnd artificially.
480342551e99SRandall Stewart 				 */
480442551e99SRandall Stewart 				tp1->whoTo->cwnd += tp1->book_size;
4805f8829a4aSRandall Stewart 				cnt_revoked++;
4806f8829a4aSRandall Stewart 			}
4807f8829a4aSRandall Stewart 		}
4808f8829a4aSRandall Stewart 		if (cnt_revoked) {
4809f8829a4aSRandall Stewart 			reneged_all = 1;
4810f8829a4aSRandall Stewart 		}
4811f8829a4aSRandall Stewart 		asoc->saw_sack_with_frags = 0;
4812f8829a4aSRandall Stewart 	}
4813d9c5cfeaSMichael Tuexen 	if (num_nr_seg > 0)
4814d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 1;
4815f8829a4aSRandall Stewart 	else
4816d9c5cfeaSMichael Tuexen 		asoc->saw_sack_with_nr_frags = 0;
4817f8829a4aSRandall Stewart 
4818b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
4819ca85e948SMichael Tuexen 	if (ecne_seen == 0) {
4820ca85e948SMichael Tuexen 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4821ca85e948SMichael Tuexen 			if (net->net_ack2 > 0) {
4822ca85e948SMichael Tuexen 				/*
4823ca85e948SMichael Tuexen 				 * Karn's rule applies to clearing error
4824ca85e948SMichael Tuexen 				 * count, this is optional.
4825ca85e948SMichael Tuexen 				 */
4826ca85e948SMichael Tuexen 				net->error_count = 0;
4827ca85e948SMichael Tuexen 				if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
4828ca85e948SMichael Tuexen 					/* addr came good */
4829ca85e948SMichael Tuexen 					net->dest_state |= SCTP_ADDR_REACHABLE;
4830ca85e948SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
48314b1f78e1SMichael Tuexen 					    0, (void *)net, SCTP_SO_NOT_LOCKED);
4832ca85e948SMichael Tuexen 				}
4833ca85e948SMichael Tuexen 				if (net == stcb->asoc.primary_destination) {
4834ca85e948SMichael Tuexen 					if (stcb->asoc.alternate) {
4835ca85e948SMichael Tuexen 						/*
4836ca85e948SMichael Tuexen 						 * release the alternate,
4837ca85e948SMichael Tuexen 						 * primary is good
4838ca85e948SMichael Tuexen 						 */
4839ca85e948SMichael Tuexen 						sctp_free_remote_addr(stcb->asoc.alternate);
4840ca85e948SMichael Tuexen 						stcb->asoc.alternate = NULL;
4841ca85e948SMichael Tuexen 					}
4842ca85e948SMichael Tuexen 				}
4843ca85e948SMichael Tuexen 				if (net->dest_state & SCTP_ADDR_PF) {
4844ca85e948SMichael Tuexen 					net->dest_state &= ~SCTP_ADDR_PF;
4845ca85e948SMichael Tuexen 					sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
4846ca85e948SMichael Tuexen 					sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
4847ca85e948SMichael Tuexen 					asoc->cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
4848ca85e948SMichael Tuexen 					/* Done with this net */
4849ca85e948SMichael Tuexen 					net->net_ack = 0;
4850ca85e948SMichael Tuexen 				}
4851ca85e948SMichael Tuexen 				/* restore any doubled timers */
4852ca85e948SMichael Tuexen 				net->RTO = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
4853ca85e948SMichael Tuexen 				if (net->RTO < stcb->asoc.minrto) {
4854ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.minrto;
4855ca85e948SMichael Tuexen 				}
4856ca85e948SMichael Tuexen 				if (net->RTO > stcb->asoc.maxrto) {
4857ca85e948SMichael Tuexen 					net->RTO = stcb->asoc.maxrto;
4858ca85e948SMichael Tuexen 				}
4859ca85e948SMichael Tuexen 			}
4860ca85e948SMichael Tuexen 		}
4861b54d3a6cSRandall Stewart 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4862ca85e948SMichael Tuexen 	}
4863f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4864f8829a4aSRandall Stewart 		/* nothing left in-flight */
4865f8829a4aSRandall Stewart 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4866f8829a4aSRandall Stewart 			/* stop all timers */
4867f8829a4aSRandall Stewart 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4868a5d547adSRandall Stewart 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4869f8829a4aSRandall Stewart 			net->flight_size = 0;
4870f8829a4aSRandall Stewart 			net->partial_bytes_acked = 0;
4871f8829a4aSRandall Stewart 		}
4872f8829a4aSRandall Stewart 		asoc->total_flight = 0;
4873f8829a4aSRandall Stewart 		asoc->total_flight_count = 0;
4874f8829a4aSRandall Stewart 	}
4875f8829a4aSRandall Stewart 	/**********************************/
4876f8829a4aSRandall Stewart 	/* Now what about shutdown issues */
4877f8829a4aSRandall Stewart 	/**********************************/
4878f8829a4aSRandall Stewart 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4879f8829a4aSRandall Stewart 		/* nothing left on sendqueue.. consider done */
4880b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4881f8829a4aSRandall Stewart 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4882f8829a4aSRandall Stewart 			    asoc->peers_rwnd, 0, 0, a_rwnd);
488380fefe0aSRandall Stewart 		}
4884f8829a4aSRandall Stewart 		asoc->peers_rwnd = a_rwnd;
4885f8829a4aSRandall Stewart 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4886f8829a4aSRandall Stewart 			/* SWS sender side engages */
4887f8829a4aSRandall Stewart 			asoc->peers_rwnd = 0;
4888f8829a4aSRandall Stewart 		}
4889f8829a4aSRandall Stewart 		/* clean up */
4890f8829a4aSRandall Stewart 		if ((asoc->stream_queue_cnt == 1) &&
4891f8829a4aSRandall Stewart 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4892f8829a4aSRandall Stewart 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4893f8829a4aSRandall Stewart 		    (asoc->locked_on_sending)
4894f8829a4aSRandall Stewart 		    ) {
4895f8829a4aSRandall Stewart 			struct sctp_stream_queue_pending *sp;
4896f8829a4aSRandall Stewart 
4897f8829a4aSRandall Stewart 			/*
4898f8829a4aSRandall Stewart 			 * I may be in a state where we got all across.. but
4899f8829a4aSRandall Stewart 			 * cannot write more due to a shutdown... we abort
4900f8829a4aSRandall Stewart 			 * since the user did not indicate EOR in this case.
4901f8829a4aSRandall Stewart 			 */
4902f8829a4aSRandall Stewart 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4903f8829a4aSRandall Stewart 			    sctp_streamhead);
49042afb3e84SRandall Stewart 			if ((sp) && (sp->length == 0)) {
4905f8829a4aSRandall Stewart 				asoc->locked_on_sending = NULL;
49062afb3e84SRandall Stewart 				if (sp->msg_is_complete) {
4907f8829a4aSRandall Stewart 					asoc->stream_queue_cnt--;
49082afb3e84SRandall Stewart 				} else {
49092afb3e84SRandall Stewart 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
49102afb3e84SRandall Stewart 					asoc->stream_queue_cnt--;
49112afb3e84SRandall Stewart 				}
4912f8829a4aSRandall Stewart 			}
4913f8829a4aSRandall Stewart 		}
4914f8829a4aSRandall Stewart 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4915f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4916f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4917f8829a4aSRandall Stewart 				/* Need to abort here */
4918f8829a4aSRandall Stewart 				struct mbuf *oper;
4919f8829a4aSRandall Stewart 
4920f8829a4aSRandall Stewart 		abort_out_now:
4921f8829a4aSRandall Stewart 				*abort_now = 1;
4922f8829a4aSRandall Stewart 				/* XXX */
4923f8829a4aSRandall Stewart 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4924f8829a4aSRandall Stewart 				    0, M_DONTWAIT, 1, MT_DATA);
4925f8829a4aSRandall Stewart 				if (oper) {
4926f8829a4aSRandall Stewart 					struct sctp_paramhdr *ph;
4927f8829a4aSRandall Stewart 					uint32_t *ippp;
4928f8829a4aSRandall Stewart 
4929139bc87fSRandall Stewart 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4930f8829a4aSRandall Stewart 					    sizeof(uint32_t);
4931f8829a4aSRandall Stewart 					ph = mtod(oper, struct sctp_paramhdr *);
4932f8829a4aSRandall Stewart 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4933139bc87fSRandall Stewart 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4934f8829a4aSRandall Stewart 					ippp = (uint32_t *) (ph + 1);
4935a5d547adSRandall Stewart 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4936f8829a4aSRandall Stewart 				}
4937a5d547adSRandall Stewart 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4938a2b42326SMichael Tuexen 				sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
4939f8829a4aSRandall Stewart 				return;
4940f8829a4aSRandall Stewart 			} else {
4941ca85e948SMichael Tuexen 				struct sctp_nets *netp;
4942ca85e948SMichael Tuexen 
4943ca85e948SMichael Tuexen 				if (asoc->alternate) {
4944ca85e948SMichael Tuexen 					netp = asoc->alternate;
4945ca85e948SMichael Tuexen 				} else {
4946ca85e948SMichael Tuexen 					netp = asoc->primary_destination;
4947ca85e948SMichael Tuexen 				}
4948f42a358aSRandall Stewart 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4949f42a358aSRandall Stewart 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4950f8829a4aSRandall Stewart 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4951f42a358aSRandall Stewart 				}
4952c4739e2fSRandall Stewart 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4953b201f536SRandall Stewart 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4954f8829a4aSRandall Stewart 				sctp_stop_timers_for_shutdown(stcb);
4955ca85e948SMichael Tuexen 				sctp_send_shutdown(stcb, netp);
4956f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4957ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4958f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4959ca85e948SMichael Tuexen 				    stcb->sctp_ep, stcb, netp);
4960f8829a4aSRandall Stewart 			}
4961f8829a4aSRandall Stewart 			return;
4962f8829a4aSRandall Stewart 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4963f8829a4aSRandall Stewart 		    (asoc->stream_queue_cnt == 0)) {
4964ca85e948SMichael Tuexen 			struct sctp_nets *netp;
4965ca85e948SMichael Tuexen 
4966ca85e948SMichael Tuexen 			if (asoc->alternate) {
4967ca85e948SMichael Tuexen 				netp = asoc->alternate;
4968ca85e948SMichael Tuexen 			} else {
4969ca85e948SMichael Tuexen 				netp = asoc->primary_destination;
4970ca85e948SMichael Tuexen 			}
4971f8829a4aSRandall Stewart 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4972f8829a4aSRandall Stewart 				goto abort_out_now;
4973f8829a4aSRandall Stewart 			}
4974f8829a4aSRandall Stewart 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4975c4739e2fSRandall Stewart 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4976b201f536SRandall Stewart 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4977ca85e948SMichael Tuexen 			sctp_send_shutdown_ack(stcb, netp);
497812af6654SMichael Tuexen 			sctp_stop_timers_for_shutdown(stcb);
4979f8829a4aSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4980ca85e948SMichael Tuexen 			    stcb->sctp_ep, stcb, netp);
4981f8829a4aSRandall Stewart 			return;
4982f8829a4aSRandall Stewart 		}
4983f8829a4aSRandall Stewart 	}
4984f8829a4aSRandall Stewart 	/*
4985f8829a4aSRandall Stewart 	 * Now here we are going to recycle net_ack for a different use...
4986f8829a4aSRandall Stewart 	 * HEADS UP.
4987f8829a4aSRandall Stewart 	 */
4988f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4989f8829a4aSRandall Stewart 		net->net_ack = 0;
4990f8829a4aSRandall Stewart 	}
4991f8829a4aSRandall Stewart 
4992f8829a4aSRandall Stewart 	/*
4993f8829a4aSRandall Stewart 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
4994f8829a4aSRandall Stewart 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
4995f8829a4aSRandall Stewart 	 * automatically ensure that.
4996f8829a4aSRandall Stewart 	 */
49977c99d56fSMichael Tuexen 	if ((asoc->sctp_cmt_on_off > 0) &&
499820083c2eSMichael Tuexen 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac) &&
499920083c2eSMichael Tuexen 	    (cmt_dac_flag == 0)) {
5000f8829a4aSRandall Stewart 		this_sack_lowest_newack = cum_ack;
5001f8829a4aSRandall Stewart 	}
5002cd554309SMichael Tuexen 	if ((num_seg > 0) || (num_nr_seg > 0)) {
5003f8829a4aSRandall Stewart 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5004f8829a4aSRandall Stewart 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5005f8829a4aSRandall Stewart 	}
5006b54d3a6cSRandall Stewart 	/* JRS - Use the congestion control given in the CC module */
5007b54d3a6cSRandall Stewart 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5008f8829a4aSRandall Stewart 
5009f8829a4aSRandall Stewart 	/* Now are we exiting loss recovery ? */
5010f8829a4aSRandall Stewart 	if (will_exit_fast_recovery) {
5011f8829a4aSRandall Stewart 		/* Ok, we must exit fast recovery */
5012f8829a4aSRandall Stewart 		asoc->fast_retran_loss_recovery = 0;
5013f8829a4aSRandall Stewart 	}
5014f8829a4aSRandall Stewart 	if ((asoc->sat_t3_loss_recovery) &&
501520b07a4dSMichael Tuexen 	    SCTP_TSN_GE(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn)) {
5016f8829a4aSRandall Stewart 		/* end satellite t3 loss recovery */
5017f8829a4aSRandall Stewart 		asoc->sat_t3_loss_recovery = 0;
5018f8829a4aSRandall Stewart 	}
501942551e99SRandall Stewart 	/*
502042551e99SRandall Stewart 	 * CMT Fast recovery
502142551e99SRandall Stewart 	 */
5022f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5023f8829a4aSRandall Stewart 		if (net->will_exit_fast_recovery) {
5024f8829a4aSRandall Stewart 			/* Ok, we must exit fast recovery */
5025f8829a4aSRandall Stewart 			net->fast_retran_loss_recovery = 0;
5026f8829a4aSRandall Stewart 		}
5027f8829a4aSRandall Stewart 	}
5028f8829a4aSRandall Stewart 
5029f8829a4aSRandall Stewart 	/* Adjust and set the new rwnd value */
5030b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5031f8829a4aSRandall Stewart 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
503244fbe462SRandall Stewart 		    asoc->peers_rwnd, asoc->total_flight, (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
503380fefe0aSRandall Stewart 	}
5034f8829a4aSRandall Stewart 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
503544fbe462SRandall Stewart 	    (uint32_t) (asoc->total_flight + (asoc->total_flight_count * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5036f8829a4aSRandall Stewart 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5037f8829a4aSRandall Stewart 		/* SWS sender side engages */
5038f8829a4aSRandall Stewart 		asoc->peers_rwnd = 0;
5039f8829a4aSRandall Stewart 	}
50405e54f665SRandall Stewart 	if (asoc->peers_rwnd > old_rwnd) {
50415e54f665SRandall Stewart 		win_probe_recovery = 1;
50425e54f665SRandall Stewart 	}
5043f8829a4aSRandall Stewart 	/*
5044f8829a4aSRandall Stewart 	 * Now we must setup so we have a timer up for anyone with
5045f8829a4aSRandall Stewart 	 * outstanding data.
5046f8829a4aSRandall Stewart 	 */
5047bff64a4dSRandall Stewart 	done_once = 0;
5048a5d547adSRandall Stewart again:
5049a5d547adSRandall Stewart 	j = 0;
5050f8829a4aSRandall Stewart 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
50515e54f665SRandall Stewart 		if (win_probe_recovery && (net->window_probe)) {
5052c105859eSRandall Stewart 			win_probe_recovered = 1;
50535e54f665SRandall Stewart 			/*-
50545e54f665SRandall Stewart 			 * Find first chunk that was used with
50555e54f665SRandall Stewart 			 * window probe and clear the event. Put
50565e54f665SRandall Stewart 			 * it back into the send queue as if has
50575e54f665SRandall Stewart 			 * not been sent.
50585e54f665SRandall Stewart 			 */
50595e54f665SRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
50605e54f665SRandall Stewart 				if (tp1->window_probe) {
50617215cc1bSMichael Tuexen 					sctp_window_probe_recovery(stcb, asoc, tp1);
50625e54f665SRandall Stewart 					break;
50635e54f665SRandall Stewart 				}
50645e54f665SRandall Stewart 			}
50655e54f665SRandall Stewart 		}
5066f8829a4aSRandall Stewart 		if (net->flight_size) {
5067a5d547adSRandall Stewart 			j++;
5068cd554309SMichael Tuexen 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5069f8829a4aSRandall Stewart 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5070f8829a4aSRandall Stewart 				    stcb->sctp_ep, stcb, net);
5071cd554309SMichael Tuexen 			}
50725171328bSRandall Stewart 			if (net->window_probe) {
5073cd554309SMichael Tuexen 				net->window_probe = 0;
50745171328bSRandall Stewart 			}
5075c105859eSRandall Stewart 		} else {
50765171328bSRandall Stewart 			if (net->window_probe) {
50775171328bSRandall Stewart 				/*
50785171328bSRandall Stewart 				 * In window probes we must assure a timer
50795171328bSRandall Stewart 				 * is still running there
50805171328bSRandall Stewart 				 */
50815171328bSRandall Stewart 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
50825171328bSRandall Stewart 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
50835171328bSRandall Stewart 					    stcb->sctp_ep, stcb, net);
50845171328bSRandall Stewart 
50855171328bSRandall Stewart 				}
50865171328bSRandall Stewart 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5087c105859eSRandall Stewart 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5088c105859eSRandall Stewart 				    stcb, net,
5089c105859eSRandall Stewart 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5090c105859eSRandall Stewart 			}
5091f8829a4aSRandall Stewart 		}
5092f8829a4aSRandall Stewart 	}
5093bff64a4dSRandall Stewart 	if ((j == 0) &&
5094bff64a4dSRandall Stewart 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5095bff64a4dSRandall Stewart 	    (asoc->sent_queue_retran_cnt == 0) &&
5096c105859eSRandall Stewart 	    (win_probe_recovered == 0) &&
5097bff64a4dSRandall Stewart 	    (done_once == 0)) {
50980c0982b8SRandall Stewart 		/*
50990c0982b8SRandall Stewart 		 * huh, this should not happen unless all packets are
51000c0982b8SRandall Stewart 		 * PR-SCTP and marked to skip of course.
51010c0982b8SRandall Stewart 		 */
51020c0982b8SRandall Stewart 		if (sctp_fs_audit(asoc)) {
5103a5d547adSRandall Stewart 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5104a5d547adSRandall Stewart 				net->flight_size = 0;
5105a5d547adSRandall Stewart 			}
5106a5d547adSRandall Stewart 			asoc->total_flight = 0;
5107a5d547adSRandall Stewart 			asoc->total_flight_count = 0;
5108a5d547adSRandall Stewart 			asoc->sent_queue_retran_cnt = 0;
5109a5d547adSRandall Stewart 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5110a5d547adSRandall Stewart 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5111c105859eSRandall Stewart 					sctp_flight_size_increase(tp1);
5112c105859eSRandall Stewart 					sctp_total_flight_increase(stcb, tp1);
5113a5d547adSRandall Stewart 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5114791437b5SRandall Stewart 					sctp_ucount_incr(asoc->sent_queue_retran_cnt);
5115a5d547adSRandall Stewart 				}
5116a5d547adSRandall Stewart 			}
51170c0982b8SRandall Stewart 		}
5118bff64a4dSRandall Stewart 		done_once = 1;
5119a5d547adSRandall Stewart 		goto again;
5120a5d547adSRandall Stewart 	}
5121cd554309SMichael Tuexen 	/*********************************************/
5122cd554309SMichael Tuexen 	/* Here we perform PR-SCTP procedures        */
5123cd554309SMichael Tuexen 	/* (section 4.2)                             */
5124cd554309SMichael Tuexen 	/*********************************************/
5125cd554309SMichael Tuexen 	/* C1. update advancedPeerAckPoint */
512620b07a4dSMichael Tuexen 	if (SCTP_TSN_GT(cum_ack, asoc->advanced_peer_ack_point)) {
5127dfb11ef8SRandall Stewart 		asoc->advanced_peer_ack_point = cum_ack;
5128dfb11ef8SRandall Stewart 	}
5129830d754dSRandall Stewart 	/* C2. try to further move advancedPeerAckPoint ahead */
5130830d754dSRandall Stewart 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5131830d754dSRandall Stewart 		struct sctp_tmit_chunk *lchk;
5132830d754dSRandall Stewart 		uint32_t old_adv_peer_ack_point;
5133830d754dSRandall Stewart 
5134830d754dSRandall Stewart 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5135830d754dSRandall Stewart 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5136830d754dSRandall Stewart 		/* C3. See if we need to send a Fwd-TSN */
513720b07a4dSMichael Tuexen 		if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, cum_ack)) {
5138830d754dSRandall Stewart 			/*
5139493d8e5aSRandall Stewart 			 * ISSUE with ECN, see FWD-TSN processing.
5140830d754dSRandall Stewart 			 */
51410c0982b8SRandall Stewart 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
51420c0982b8SRandall Stewart 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
51430c0982b8SRandall Stewart 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
51440c0982b8SRandall Stewart 				    old_adv_peer_ack_point);
51450c0982b8SRandall Stewart 			}
514620b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(asoc->advanced_peer_ack_point, old_adv_peer_ack_point)) {
5147830d754dSRandall Stewart 				send_forward_tsn(stcb, asoc);
51480c0982b8SRandall Stewart 			} else if (lchk) {
51490c0982b8SRandall Stewart 				/* try to FR fwd-tsn's that get lost too */
515044fbe462SRandall Stewart 				if (lchk->rec.data.fwd_tsn_cnt >= 3) {
51510c0982b8SRandall Stewart 					send_forward_tsn(stcb, asoc);
51520c0982b8SRandall Stewart 				}
5153830d754dSRandall Stewart 			}
5154830d754dSRandall Stewart 		}
5155830d754dSRandall Stewart 		if (lchk) {
5156830d754dSRandall Stewart 			/* Assure a timer is up */
5157830d754dSRandall Stewart 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5158830d754dSRandall Stewart 			    stcb->sctp_ep, stcb, lchk->whoTo);
5159830d754dSRandall Stewart 		}
5160830d754dSRandall Stewart 	}
5161b3f1ea41SRandall Stewart 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5162f8829a4aSRandall Stewart 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5163f8829a4aSRandall Stewart 		    a_rwnd,
5164f8829a4aSRandall Stewart 		    stcb->asoc.peers_rwnd,
5165f8829a4aSRandall Stewart 		    stcb->asoc.total_flight,
5166f8829a4aSRandall Stewart 		    stcb->asoc.total_output_queue_size);
516780fefe0aSRandall Stewart 	}
5168f8829a4aSRandall Stewart }
5169f8829a4aSRandall Stewart 
5170f8829a4aSRandall Stewart void
51717215cc1bSMichael Tuexen sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp, int *abort_flag)
5172f8829a4aSRandall Stewart {
5173f8829a4aSRandall Stewart 	/* Copy cum-ack */
5174f8829a4aSRandall Stewart 	uint32_t cum_ack, a_rwnd;
5175f8829a4aSRandall Stewart 
5176f8829a4aSRandall Stewart 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5177f8829a4aSRandall Stewart 	/* Arrange so a_rwnd does NOT change */
5178f8829a4aSRandall Stewart 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5179f8829a4aSRandall Stewart 
5180f8829a4aSRandall Stewart 	/* Now call the express sack handling */
5181899288aeSRandall Stewart 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, abort_flag, 0);
5182f8829a4aSRandall Stewart }
5183f8829a4aSRandall Stewart 
5184f8829a4aSRandall Stewart static void
5185f8829a4aSRandall Stewart sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5186f8829a4aSRandall Stewart     struct sctp_stream_in *strmin)
5187f8829a4aSRandall Stewart {
5188f8829a4aSRandall Stewart 	struct sctp_queued_to_read *ctl, *nctl;
5189f8829a4aSRandall Stewart 	struct sctp_association *asoc;
519083128708SRandall Stewart 	uint16_t tt;
5191f8829a4aSRandall Stewart 
5192f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5193f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered;
5194f8829a4aSRandall Stewart 	/*
5195f8829a4aSRandall Stewart 	 * First deliver anything prior to and including the stream no that
5196f8829a4aSRandall Stewart 	 * came in
5197f8829a4aSRandall Stewart 	 */
51984a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
519920b07a4dSMichael Tuexen 		if (SCTP_SSN_GE(tt, ctl->sinfo_ssn)) {
5200f8829a4aSRandall Stewart 			/* this is deliverable now */
5201f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5202f8829a4aSRandall Stewart 			/* subtract pending on streams */
5203f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5204f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5205f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5206f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5207b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5208f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5209f8829a4aSRandall Stewart 				    ctl,
5210cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5211f8829a4aSRandall Stewart 			}
5212f8829a4aSRandall Stewart 		} else {
5213f8829a4aSRandall Stewart 			/* no more delivery now. */
5214f8829a4aSRandall Stewart 			break;
5215f8829a4aSRandall Stewart 		}
5216f8829a4aSRandall Stewart 	}
5217f8829a4aSRandall Stewart 	/*
5218f8829a4aSRandall Stewart 	 * now we must deliver things in queue the normal way  if any are
5219f8829a4aSRandall Stewart 	 * now ready.
5220f8829a4aSRandall Stewart 	 */
5221f8829a4aSRandall Stewart 	tt = strmin->last_sequence_delivered + 1;
52224a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(ctl, &strmin->inqueue, next, nctl) {
5223f8829a4aSRandall Stewart 		if (tt == ctl->sinfo_ssn) {
5224f8829a4aSRandall Stewart 			/* this is deliverable now */
5225f8829a4aSRandall Stewart 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5226f8829a4aSRandall Stewart 			/* subtract pending on streams */
5227f8829a4aSRandall Stewart 			asoc->size_on_all_streams -= ctl->length;
5228f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5229f8829a4aSRandall Stewart 			/* deliver it to at least the delivery-q */
5230f8829a4aSRandall Stewart 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5231f8829a4aSRandall Stewart 			if (stcb->sctp_socket) {
5232b5c16493SMichael Tuexen 				sctp_mark_non_revokable(asoc, ctl->sinfo_tsn);
5233f8829a4aSRandall Stewart 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5234f8829a4aSRandall Stewart 				    ctl,
5235cfde3ff7SRandall Stewart 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
523677acdc25SRandall Stewart 
5237f8829a4aSRandall Stewart 			}
5238f8829a4aSRandall Stewart 			tt = strmin->last_sequence_delivered + 1;
5239f8829a4aSRandall Stewart 		} else {
5240f8829a4aSRandall Stewart 			break;
5241f8829a4aSRandall Stewart 		}
5242f8829a4aSRandall Stewart 	}
5243f8829a4aSRandall Stewart }
5244f8829a4aSRandall Stewart 
52458933fa13SRandall Stewart static void
52468933fa13SRandall Stewart sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
52478933fa13SRandall Stewart     struct sctp_association *asoc,
52488933fa13SRandall Stewart     uint16_t stream, uint16_t seq)
52498933fa13SRandall Stewart {
52504a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
52518933fa13SRandall Stewart 
52528933fa13SRandall Stewart 	/* For each one on here see if we need to toss it */
52538933fa13SRandall Stewart 	/*
52544a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
52554a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
52564a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
52574a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
52584a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
52594a9ef3f8SMichael Tuexen 	 * queue.
52608933fa13SRandall Stewart 	 */
52614a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
52628e71b694SMichael Tuexen 		/*
52634a9ef3f8SMichael Tuexen 		 * Do not toss it if on a different stream or marked for
52644a9ef3f8SMichael Tuexen 		 * unordered delivery in which case the stream sequence
52654a9ef3f8SMichael Tuexen 		 * number has no meaning.
52668e71b694SMichael Tuexen 		 */
52678e71b694SMichael Tuexen 		if ((chk->rec.data.stream_number != stream) ||
52688e71b694SMichael Tuexen 		    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
52698933fa13SRandall Stewart 			continue;
52708933fa13SRandall Stewart 		}
52718933fa13SRandall Stewart 		if (chk->rec.data.stream_seq == seq) {
52728933fa13SRandall Stewart 			/* It needs to be tossed */
52738933fa13SRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
527420b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
52754a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
52764a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
52774a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
52784a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
52798933fa13SRandall Stewart 			}
52808933fa13SRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
52818933fa13SRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
52828933fa13SRandall Stewart 
52838933fa13SRandall Stewart 			/* Clear up any stream problem */
528420b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
528520b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
52868933fa13SRandall Stewart 				/*
52878933fa13SRandall Stewart 				 * We must dump forward this streams
52884a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
52894a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
52904a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
52914a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
52924a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
52934a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
52944a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
52954a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
52964a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
52974a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
52984a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
52998933fa13SRandall Stewart 				 */
53004a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
53018933fa13SRandall Stewart 			}
53028933fa13SRandall Stewart 			if (chk->data) {
53038933fa13SRandall Stewart 				sctp_m_freem(chk->data);
53048933fa13SRandall Stewart 				chk->data = NULL;
53058933fa13SRandall Stewart 			}
5306689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
530720b07a4dSMichael Tuexen 		} else if (SCTP_SSN_GT(chk->rec.data.stream_seq, seq)) {
53088933fa13SRandall Stewart 			/*
53094a9ef3f8SMichael Tuexen 			 * If the stream_seq is > than the purging one, we
53104a9ef3f8SMichael Tuexen 			 * are done
53118933fa13SRandall Stewart 			 */
53128933fa13SRandall Stewart 			break;
53138933fa13SRandall Stewart 		}
53148933fa13SRandall Stewart 	}
53158933fa13SRandall Stewart }
53168933fa13SRandall Stewart 
53178933fa13SRandall Stewart 
5318f8829a4aSRandall Stewart void
5319f8829a4aSRandall Stewart sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5320b5c16493SMichael Tuexen     struct sctp_forward_tsn_chunk *fwd,
5321b5c16493SMichael Tuexen     int *abort_flag, struct mbuf *m, int offset)
5322f8829a4aSRandall Stewart {
5323f8829a4aSRandall Stewart 	/* The pr-sctp fwd tsn */
5324f8829a4aSRandall Stewart 	/*
5325f8829a4aSRandall Stewart 	 * here we will perform all the data receiver side steps for
5326f8829a4aSRandall Stewart 	 * processing FwdTSN, as required in by pr-sctp draft:
5327f8829a4aSRandall Stewart 	 *
5328f8829a4aSRandall Stewart 	 * Assume we get FwdTSN(x):
5329f8829a4aSRandall Stewart 	 *
5330f8829a4aSRandall Stewart 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5331f8829a4aSRandall Stewart 	 * others we have 3) examine and update re-ordering queue on
5332f8829a4aSRandall Stewart 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5333f8829a4aSRandall Stewart 	 * report where we are.
5334f8829a4aSRandall Stewart 	 */
5335f8829a4aSRandall Stewart 	struct sctp_association *asoc;
53367898f408SRandall Stewart 	uint32_t new_cum_tsn, gap;
53377215cc1bSMichael Tuexen 	unsigned int i, fwd_sz, m_size;
53388933fa13SRandall Stewart 	uint32_t str_seq;
5339f8829a4aSRandall Stewart 	struct sctp_stream_in *strm;
53404a9ef3f8SMichael Tuexen 	struct sctp_tmit_chunk *chk, *nchk;
53418933fa13SRandall Stewart 	struct sctp_queued_to_read *ctl, *sv;
5342f8829a4aSRandall Stewart 
5343f8829a4aSRandall Stewart 	asoc = &stcb->asoc;
5344f8829a4aSRandall Stewart 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5345ad81507eSRandall Stewart 		SCTPDBG(SCTP_DEBUG_INDATA1,
5346ad81507eSRandall Stewart 		    "Bad size too small/big fwd-tsn\n");
5347f8829a4aSRandall Stewart 		return;
5348f8829a4aSRandall Stewart 	}
5349f8829a4aSRandall Stewart 	m_size = (stcb->asoc.mapping_array_size << 3);
5350f8829a4aSRandall Stewart 	/*************************************************************/
5351f8829a4aSRandall Stewart 	/* 1. Here we update local cumTSN and shift the bitmap array */
5352f8829a4aSRandall Stewart 	/*************************************************************/
5353f8829a4aSRandall Stewart 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5354f8829a4aSRandall Stewart 
535520b07a4dSMichael Tuexen 	if (SCTP_TSN_GE(asoc->cumulative_tsn, new_cum_tsn)) {
5356f8829a4aSRandall Stewart 		/* Already got there ... */
5357f8829a4aSRandall Stewart 		return;
5358f8829a4aSRandall Stewart 	}
5359f8829a4aSRandall Stewart 	/*
5360f8829a4aSRandall Stewart 	 * now we know the new TSN is more advanced, let's find the actual
5361f8829a4aSRandall Stewart 	 * gap
5362f8829a4aSRandall Stewart 	 */
5363b5c16493SMichael Tuexen 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
536477acdc25SRandall Stewart 	asoc->cumulative_tsn = new_cum_tsn;
53652afb3e84SRandall Stewart 	if (gap >= m_size) {
5366f8829a4aSRandall Stewart 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
536717205eccSRandall Stewart 			struct mbuf *oper;
536817205eccSRandall Stewart 
5369f8829a4aSRandall Stewart 			/*
5370f8829a4aSRandall Stewart 			 * out of range (of single byte chunks in the rwnd I
537117205eccSRandall Stewart 			 * give out). This must be an attacker.
5372f8829a4aSRandall Stewart 			 */
537317205eccSRandall Stewart 			*abort_flag = 1;
537417205eccSRandall Stewart 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
537517205eccSRandall Stewart 			    0, M_DONTWAIT, 1, MT_DATA);
537617205eccSRandall Stewart 			if (oper) {
537717205eccSRandall Stewart 				struct sctp_paramhdr *ph;
537817205eccSRandall Stewart 				uint32_t *ippp;
537917205eccSRandall Stewart 
538017205eccSRandall Stewart 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
538117205eccSRandall Stewart 				    (sizeof(uint32_t) * 3);
538217205eccSRandall Stewart 				ph = mtod(oper, struct sctp_paramhdr *);
538317205eccSRandall Stewart 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
538417205eccSRandall Stewart 				ph->param_length = htons(SCTP_BUF_LEN(oper));
538517205eccSRandall Stewart 				ippp = (uint32_t *) (ph + 1);
538617205eccSRandall Stewart 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
538717205eccSRandall Stewart 				ippp++;
538817205eccSRandall Stewart 				*ippp = asoc->highest_tsn_inside_map;
538917205eccSRandall Stewart 				ippp++;
539017205eccSRandall Stewart 				*ippp = new_cum_tsn;
539117205eccSRandall Stewart 			}
539217205eccSRandall Stewart 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5393a2b42326SMichael Tuexen 			sctp_abort_an_association(stcb->sctp_ep, stcb, oper, SCTP_SO_NOT_LOCKED);
5394f8829a4aSRandall Stewart 			return;
5395f8829a4aSRandall Stewart 		}
5396207304d4SRandall Stewart 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
539777acdc25SRandall Stewart 
53982afb3e84SRandall Stewart 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
53992afb3e84SRandall Stewart 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
540077acdc25SRandall Stewart 		asoc->highest_tsn_inside_map = new_cum_tsn;
540177acdc25SRandall Stewart 
5402b5c16493SMichael Tuexen 		memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
5403830d754dSRandall Stewart 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
540477acdc25SRandall Stewart 
5405b3f1ea41SRandall Stewart 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
5406f8829a4aSRandall Stewart 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
540780fefe0aSRandall Stewart 		}
54082afb3e84SRandall Stewart 	} else {
54092afb3e84SRandall Stewart 		SCTP_TCB_LOCK_ASSERT(stcb);
54102afb3e84SRandall Stewart 		for (i = 0; i <= gap; i++) {
54117898f408SRandall Stewart 			if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) &&
54127898f408SRandall Stewart 			    !SCTP_IS_TSN_PRESENT(asoc->nr_mapping_array, i)) {
54138933fa13SRandall Stewart 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
541420b07a4dSMichael Tuexen 				if (SCTP_TSN_GT(asoc->mapping_array_base_tsn + i, asoc->highest_tsn_inside_nr_map)) {
54157898f408SRandall Stewart 					asoc->highest_tsn_inside_nr_map = asoc->mapping_array_base_tsn + i;
5416b5c16493SMichael Tuexen 				}
5417b5c16493SMichael Tuexen 			}
5418b5c16493SMichael Tuexen 		}
5419f8829a4aSRandall Stewart 	}
5420f8829a4aSRandall Stewart 	/*************************************************************/
5421f8829a4aSRandall Stewart 	/* 2. Clear up re-assembly queue                             */
5422f8829a4aSRandall Stewart 	/*************************************************************/
5423f8829a4aSRandall Stewart 	/*
5424f8829a4aSRandall Stewart 	 * First service it if pd-api is up, just in case we can progress it
5425f8829a4aSRandall Stewart 	 * forward
5426f8829a4aSRandall Stewart 	 */
5427f8829a4aSRandall Stewart 	if (asoc->fragmented_delivery_inprogress) {
5428f8829a4aSRandall Stewart 		sctp_service_reassembly(stcb, asoc);
5429f8829a4aSRandall Stewart 	}
5430f8829a4aSRandall Stewart 	/* For each one on here see if we need to toss it */
5431f8829a4aSRandall Stewart 	/*
54324a9ef3f8SMichael Tuexen 	 * For now large messages held on the reasmqueue that are complete
54334a9ef3f8SMichael Tuexen 	 * will be tossed too. We could in theory do more work to spin
54344a9ef3f8SMichael Tuexen 	 * through and stop after dumping one msg aka seeing the start of a
54354a9ef3f8SMichael Tuexen 	 * new msg at the head, and call the delivery function... to see if
54364a9ef3f8SMichael Tuexen 	 * it can be delivered... But for now we just dump everything on the
54374a9ef3f8SMichael Tuexen 	 * queue.
5438f8829a4aSRandall Stewart 	 */
54394a9ef3f8SMichael Tuexen 	TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
544020b07a4dSMichael Tuexen 		if (SCTP_TSN_GE(new_cum_tsn, chk->rec.data.TSN_seq)) {
5441f8829a4aSRandall Stewart 			/* It needs to be tossed */
5442f8829a4aSRandall Stewart 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
544320b07a4dSMichael Tuexen 			if (SCTP_TSN_GT(chk->rec.data.TSN_seq, asoc->tsn_last_delivered)) {
54444a9ef3f8SMichael Tuexen 				asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
54454a9ef3f8SMichael Tuexen 				asoc->str_of_pdapi = chk->rec.data.stream_number;
54464a9ef3f8SMichael Tuexen 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
54474a9ef3f8SMichael Tuexen 				asoc->fragment_flags = chk->rec.data.rcv_flags;
5448f8829a4aSRandall Stewart 			}
5449f8829a4aSRandall Stewart 			asoc->size_on_reasm_queue -= chk->send_size;
5450f8829a4aSRandall Stewart 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5451f8829a4aSRandall Stewart 
5452f8829a4aSRandall Stewart 			/* Clear up any stream problem */
545320b07a4dSMichael Tuexen 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) != SCTP_DATA_UNORDERED &&
545420b07a4dSMichael Tuexen 			    SCTP_SSN_GT(chk->rec.data.stream_seq, asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered)) {
5455f8829a4aSRandall Stewart 				/*
5456f8829a4aSRandall Stewart 				 * We must dump forward this streams
54574a9ef3f8SMichael Tuexen 				 * sequence number if the chunk is not
54584a9ef3f8SMichael Tuexen 				 * unordered that is being skipped. There is
54594a9ef3f8SMichael Tuexen 				 * a chance that if the peer does not
54604a9ef3f8SMichael Tuexen 				 * include the last fragment in its FWD-TSN
54614a9ef3f8SMichael Tuexen 				 * we WILL have a problem here since you
54624a9ef3f8SMichael Tuexen 				 * would have a partial chunk in queue that
54634a9ef3f8SMichael Tuexen 				 * may not be deliverable. Also if a Partial
54644a9ef3f8SMichael Tuexen 				 * delivery API as started the user may get
54654a9ef3f8SMichael Tuexen 				 * a partial chunk. The next read returning
54664a9ef3f8SMichael Tuexen 				 * a new chunk... really ugly but I see no
54674a9ef3f8SMichael Tuexen 				 * way around it! Maybe a notify??
5468f8829a4aSRandall Stewart 				 */
54694a9ef3f8SMichael Tuexen 				asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered = chk->rec.data.stream_seq;
5470f8829a4aSRandall Stewart 			}
5471f8829a4aSRandall Stewart 			if (chk->data) {
5472f8829a4aSRandall Stewart 				sctp_m_freem(chk->data);
5473f8829a4aSRandall Stewart 				chk->data = NULL;
5474f8829a4aSRandall Stewart 			}
5475689e6a5fSMichael Tuexen 			sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
5476f8829a4aSRandall Stewart 		} else {
5477f8829a4aSRandall Stewart 			/*
54784a9ef3f8SMichael Tuexen 			 * Ok we have gone beyond the end of the fwd-tsn's
54794a9ef3f8SMichael Tuexen 			 * mark.
5480f8829a4aSRandall Stewart 			 */
5481f8829a4aSRandall Stewart 			break;
5482f8829a4aSRandall Stewart 		}
5483f8829a4aSRandall Stewart 	}
54848933fa13SRandall Stewart 	/*******************************************************/
54858933fa13SRandall Stewart 	/* 3. Update the PR-stream re-ordering queues and fix  */
54868933fa13SRandall Stewart 	/* delivery issues as needed.                       */
54878933fa13SRandall Stewart 	/*******************************************************/
5488f8829a4aSRandall Stewart 	fwd_sz -= sizeof(*fwd);
5489671d309cSRandall Stewart 	if (m && fwd_sz) {
5490f8829a4aSRandall Stewart 		/* New method. */
5491d61a0ae0SRandall Stewart 		unsigned int num_str;
5492671d309cSRandall Stewart 		struct sctp_strseq *stseq, strseqbuf;
5493671d309cSRandall Stewart 
5494671d309cSRandall Stewart 		offset += sizeof(*fwd);
5495f8829a4aSRandall Stewart 
54968933fa13SRandall Stewart 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
5497f8829a4aSRandall Stewart 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5498f8829a4aSRandall Stewart 		for (i = 0; i < num_str; i++) {
5499f8829a4aSRandall Stewart 			uint16_t st;
5500f8829a4aSRandall Stewart 
5501671d309cSRandall Stewart 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5502671d309cSRandall Stewart 			    sizeof(struct sctp_strseq),
5503671d309cSRandall Stewart 			    (uint8_t *) & strseqbuf);
5504671d309cSRandall Stewart 			offset += sizeof(struct sctp_strseq);
55052afb3e84SRandall Stewart 			if (stseq == NULL) {
5506671d309cSRandall Stewart 				break;
55072afb3e84SRandall Stewart 			}
5508f8829a4aSRandall Stewart 			/* Convert */
5509851b7298SRandall Stewart 			st = ntohs(stseq->stream);
5510851b7298SRandall Stewart 			stseq->stream = st;
5511851b7298SRandall Stewart 			st = ntohs(stseq->sequence);
5512851b7298SRandall Stewart 			stseq->sequence = st;
55138933fa13SRandall Stewart 
5514f8829a4aSRandall Stewart 			/* now process */
55158933fa13SRandall Stewart 
55168933fa13SRandall Stewart 			/*
55178933fa13SRandall Stewart 			 * Ok we now look for the stream/seq on the read
55188933fa13SRandall Stewart 			 * queue where its not all delivered. If we find it
55198933fa13SRandall Stewart 			 * we transmute the read entry into a PDI_ABORTED.
55208933fa13SRandall Stewart 			 */
5521851b7298SRandall Stewart 			if (stseq->stream >= asoc->streamincnt) {
55222afb3e84SRandall Stewart 				/* screwed up streams, stop!  */
55232afb3e84SRandall Stewart 				break;
5524f8829a4aSRandall Stewart 			}
55258933fa13SRandall Stewart 			if ((asoc->str_of_pdapi == stseq->stream) &&
55268933fa13SRandall Stewart 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
55278933fa13SRandall Stewart 				/*
55288933fa13SRandall Stewart 				 * If this is the one we were partially
55298933fa13SRandall Stewart 				 * delivering now then we no longer are.
55308933fa13SRandall Stewart 				 * Note this will change with the reassembly
55318933fa13SRandall Stewart 				 * re-write.
55328933fa13SRandall Stewart 				 */
55338933fa13SRandall Stewart 				asoc->fragmented_delivery_inprogress = 0;
55348933fa13SRandall Stewart 			}
55358933fa13SRandall Stewart 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
55368933fa13SRandall Stewart 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
55378933fa13SRandall Stewart 				if ((ctl->sinfo_stream == stseq->stream) &&
55388933fa13SRandall Stewart 				    (ctl->sinfo_ssn == stseq->sequence)) {
55398933fa13SRandall Stewart 					str_seq = (stseq->stream << 16) | stseq->sequence;
55408933fa13SRandall Stewart 					ctl->end_added = 1;
55418933fa13SRandall Stewart 					ctl->pdapi_aborted = 1;
55428933fa13SRandall Stewart 					sv = stcb->asoc.control_pdapi;
55438933fa13SRandall Stewart 					stcb->asoc.control_pdapi = ctl;
5544810ec536SMichael Tuexen 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5545810ec536SMichael Tuexen 					    stcb,
55468933fa13SRandall Stewart 					    SCTP_PARTIAL_DELIVERY_ABORTED,
5547810ec536SMichael Tuexen 					    (void *)&str_seq,
5548810ec536SMichael Tuexen 					    SCTP_SO_NOT_LOCKED);
55498933fa13SRandall Stewart 					stcb->asoc.control_pdapi = sv;
55508933fa13SRandall Stewart 					break;
55518933fa13SRandall Stewart 				} else if ((ctl->sinfo_stream == stseq->stream) &&
555220b07a4dSMichael Tuexen 				    SCTP_SSN_GT(ctl->sinfo_ssn, stseq->sequence)) {
55538933fa13SRandall Stewart 					/* We are past our victim SSN */
55548933fa13SRandall Stewart 					break;
55558933fa13SRandall Stewart 				}
55568933fa13SRandall Stewart 			}
5557851b7298SRandall Stewart 			strm = &asoc->strmin[stseq->stream];
555820b07a4dSMichael Tuexen 			if (SCTP_SSN_GT(stseq->sequence, strm->last_sequence_delivered)) {
5559f8829a4aSRandall Stewart 				/* Update the sequence number */
556020b07a4dSMichael Tuexen 				strm->last_sequence_delivered = stseq->sequence;
5561f8829a4aSRandall Stewart 			}
5562f8829a4aSRandall Stewart 			/* now kick the stream the new way */
556304ee05e8SRandall Stewart 			/* sa_ignore NO_NULL_CHK */
5564f8829a4aSRandall Stewart 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5565f8829a4aSRandall Stewart 		}
55668933fa13SRandall Stewart 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
5567f8829a4aSRandall Stewart 	}
55687898f408SRandall Stewart 	/*
55697898f408SRandall Stewart 	 * Now slide thing forward.
55707898f408SRandall Stewart 	 */
55717898f408SRandall Stewart 	sctp_slide_mapping_arrays(stcb);
55727898f408SRandall Stewart 
557324f52bbdSMichael Tuexen 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5574139bc87fSRandall Stewart 		/* now lets kick out and check for more fragmented delivery */
557504ee05e8SRandall Stewart 		/* sa_ignore NO_NULL_CHK */
5576139bc87fSRandall Stewart 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5577139bc87fSRandall Stewart 	}
5578f8829a4aSRandall Stewart }
5579